XEmacs 21.2.46 "Urania".
[chise/xemacs-chise.git.1] / src / glyphs.c
1 /* Generic glyph/image implementation + display tables
2    Copyright (C) 1994, 1995 Board of Trustees, University of Illinois.
3    Copyright (C) 1995 Tinker Systems
4    Copyright (C) 1995, 1996, 2000 Ben Wing
5    Copyright (C) 1995 Sun Microsystems
6    Copyright (C) 1998, 1999, 2000 Andy Piper
7
8 This file is part of XEmacs.
9
10 XEmacs is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
13 later version.
14
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with XEmacs; see the file COPYING.  If not, write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.  */
24
25 /* Synched up with: Not in FSF. */
26
27 /* Written by Ben Wing and Chuck Thompson. Heavily modified /
28    rewritten by Andy Piper. */
29
30 #include <config.h>
31 #include "lisp.h"
32
33 #include "blocktype.h"
34 #include "buffer.h"
35 #include "chartab.h"
36 #include "device.h"
37 #include "elhash.h"
38 #include "faces.h"
39 #include "frame.h"
40 #include "glyphs.h"
41 #include "insdel.h"
42 #include "objects.h"
43 #include "opaque.h"
44 #include "rangetab.h"
45 #include "redisplay.h"
46 #include "specifier.h"
47 #include "window.h"
48
49 #if defined (HAVE_XPM) && !defined (HAVE_GTK)
50 #include <X11/xpm.h>
51 #endif
52
53 Lisp_Object Qimage_conversion_error;
54
55 Lisp_Object Qglyphp, Qcontrib_p, Qbaseline;
56 Lisp_Object Qbuffer_glyph_p, Qpointer_glyph_p, Qicon_glyph_p;
57 Lisp_Object Qnothing_image_instance_p, Qtext_image_instance_p;
58 Lisp_Object Qmono_pixmap_image_instance_p;
59 Lisp_Object Qcolor_pixmap_image_instance_p;
60 Lisp_Object Qpointer_image_instance_p;
61 Lisp_Object Qsubwindow_image_instance_p;
62 Lisp_Object Qwidget_image_instance_p;
63 Lisp_Object Qconst_glyph_variable;
64 Lisp_Object Qmono_pixmap, Qcolor_pixmap, Qsubwindow;
65 Lisp_Object Q_file, Q_data, Q_face, Q_pixel_width, Q_pixel_height;
66 Lisp_Object Qformatted_string;
67 Lisp_Object Vcurrent_display_table;
68 Lisp_Object Vtruncation_glyph, Vcontinuation_glyph, Voctal_escape_glyph;
69 Lisp_Object Vcontrol_arrow_glyph, Vinvisible_text_glyph, Vhscroll_glyph;
70 Lisp_Object Vxemacs_logo;
71 Lisp_Object Vthe_nothing_vector;
72 Lisp_Object Vimage_instantiator_format_list;
73 Lisp_Object Vimage_instance_type_list;
74 Lisp_Object Vglyph_type_list;
75
76 int disable_animated_pixmaps;
77
78 DEFINE_IMAGE_INSTANTIATOR_FORMAT (nothing);
79 DEFINE_IMAGE_INSTANTIATOR_FORMAT (inherit);
80 DEFINE_IMAGE_INSTANTIATOR_FORMAT (string);
81 DEFINE_IMAGE_INSTANTIATOR_FORMAT (formatted_string);
82 DEFINE_IMAGE_INSTANTIATOR_FORMAT (subwindow);
83 DEFINE_IMAGE_INSTANTIATOR_FORMAT (text);
84 DEFINE_IMAGE_INSTANTIATOR_FORMAT (pointer);
85
86 #ifdef HAVE_WINDOW_SYSTEM
87 DEFINE_IMAGE_INSTANTIATOR_FORMAT (xbm);
88 Lisp_Object Qxbm;
89
90 Lisp_Object Q_mask_file, Q_mask_data, Q_hotspot_x, Q_hotspot_y;
91 Lisp_Object Q_foreground, Q_background;
92 #ifndef BitmapSuccess
93 #define BitmapSuccess           0
94 #define BitmapOpenFailed        1
95 #define BitmapFileInvalid       2
96 #define BitmapNoMemory          3
97 #endif
98 #endif
99
100 #ifdef HAVE_XFACE
101 DEFINE_IMAGE_INSTANTIATOR_FORMAT (xface);
102 Lisp_Object Qxface;
103 #endif
104
105 #ifdef HAVE_XPM
106 DEFINE_IMAGE_INSTANTIATOR_FORMAT (xpm);
107 Lisp_Object Qxpm;
108 Lisp_Object Q_color_symbols;
109 #endif
110
111 typedef struct image_instantiator_format_entry image_instantiator_format_entry;
112 struct image_instantiator_format_entry
113 {
114   Lisp_Object symbol;
115   Lisp_Object device;
116   struct image_instantiator_methods *meths;
117 };
118
119 typedef struct
120 {
121   Dynarr_declare (struct image_instantiator_format_entry);
122 } image_instantiator_format_entry_dynarr;
123
124 /* This contains one entry per format, per device it's defined on. */
125 image_instantiator_format_entry_dynarr *
126   the_image_instantiator_format_entry_dynarr;
127
128 static Lisp_Object allocate_image_instance (Lisp_Object governing_domain,
129                                             Lisp_Object parent,
130                                             Lisp_Object instantiator);
131 static void image_validate (Lisp_Object instantiator);
132 static void glyph_property_was_changed (Lisp_Object glyph,
133                                         Lisp_Object property,
134                                         Lisp_Object locale);
135 static void set_image_instance_dirty_p (Lisp_Object instance, int dirty);
136 static void register_ignored_expose (struct frame* f, int x, int y, int width, int height);
137 static void cache_subwindow_instance_in_frame_maybe (Lisp_Object instance);
138 static void update_image_instance (Lisp_Object image_instance,
139                                    Lisp_Object instantiator);
140 /* Unfortunately windows and X are different. In windows BeginPaint()
141    will prevent WM_PAINT messages being generated so it is unnecessary
142    to register exposures as they will not occur. Under X they will
143    always occur. */
144 int hold_ignored_expose_registration;
145
146 EXFUN (Fimage_instance_type, 1);
147 EXFUN (Fglyph_type, 1);
148 EXFUN (Fnext_window, 4);
149
150 \f
151 /****************************************************************************
152  *                          Image Instantiators                             *
153  ****************************************************************************/
154
155 struct image_instantiator_methods *
156 decode_device_ii_format (Lisp_Object device, Lisp_Object format,
157                          Error_behavior errb)
158 {
159   int i;
160
161   if (!SYMBOLP (format))
162     {
163       if (ERRB_EQ (errb, ERROR_ME))
164         CHECK_SYMBOL (format);
165       return 0;
166     }
167
168   for (i = 0; i < Dynarr_length (the_image_instantiator_format_entry_dynarr);
169        i++)
170     {
171       if ( EQ (format,
172                Dynarr_at (the_image_instantiator_format_entry_dynarr, i).
173                symbol) )
174         {
175           Lisp_Object d = Dynarr_at (the_image_instantiator_format_entry_dynarr, i).
176             device;
177           if ((NILP (d) && NILP (device))
178               ||
179               (!NILP (device) &&
180                EQ (CONSOLE_TYPE (XCONSOLE
181                                  (DEVICE_CONSOLE (XDEVICE (device)))), d)))
182             return Dynarr_at (the_image_instantiator_format_entry_dynarr, i).meths;
183         }
184     }
185
186   maybe_signal_simple_error ("Invalid image-instantiator format", format,
187                              Qimage, errb);
188
189   return 0;
190 }
191
192 struct image_instantiator_methods *
193 decode_image_instantiator_format (Lisp_Object format, Error_behavior errb)
194 {
195   return decode_device_ii_format (Qnil, format, errb);
196 }
197
198 static int
199 valid_image_instantiator_format_p (Lisp_Object format, Lisp_Object locale)
200 {
201   int i;
202   struct image_instantiator_methods* meths =
203     decode_image_instantiator_format (format, ERROR_ME_NOT);
204   Lisp_Object contype = Qnil;
205   /* mess with the locale */
206   if (!NILP (locale) && SYMBOLP (locale))
207     contype = locale;
208   else
209     {
210       struct console* console = decode_console (locale);
211       contype = console ? CONSOLE_TYPE (console) : locale;
212     }
213   /* nothing is valid in all locales */
214   if (EQ (format, Qnothing))
215     return 1;
216   /* reject unknown formats */
217   else if (NILP (contype) || !meths)
218     return 0;
219
220   for (i = 0; i < Dynarr_length (meths->consoles); i++)
221     if (EQ (contype, Dynarr_at (meths->consoles, i).symbol))
222       return 1;
223   return 0;
224 }
225
226 DEFUN ("valid-image-instantiator-format-p", Fvalid_image_instantiator_format_p,
227        1, 2, 0, /*
228 Given an IMAGE-INSTANTIATOR-FORMAT, return non-nil if it is valid.
229 If LOCALE is non-nil then the format is checked in that locale.
230 If LOCALE is nil the current console is used.
231
232 Valid formats are some subset of 'nothing, 'string, 'formatted-string,
233 'xpm, 'xbm, 'xface, 'gif, 'jpeg, 'png, 'tiff, 'cursor-font, 'font,
234 'autodetect, 'subwindow, 'inherit, 'mswindows-resource, 'bmp,
235 'native-layout, 'layout, 'label, 'tab-control, 'tree-view,
236 'progress-gauge, 'scrollbar, 'combo-box, 'edit-field, 'button,
237 'widget, 'pointer, and 'text, depending on how XEmacs was compiled.
238 */
239        (image_instantiator_format, locale))
240 {
241   return valid_image_instantiator_format_p (image_instantiator_format,
242                                             locale) ?
243     Qt : Qnil;
244 }
245
246 DEFUN ("image-instantiator-format-list", Fimage_instantiator_format_list,
247        0, 0, 0, /*
248 Return a list of valid image-instantiator formats.
249 */
250        ())
251 {
252   return Fcopy_sequence (Vimage_instantiator_format_list);
253 }
254
255 void
256 add_entry_to_device_ii_format_list (Lisp_Object device, Lisp_Object symbol,
257                                     struct image_instantiator_methods *meths)
258 {
259   struct image_instantiator_format_entry entry;
260
261   entry.symbol = symbol;
262   entry.device = device;
263   entry.meths = meths;
264   Dynarr_add (the_image_instantiator_format_entry_dynarr, entry);
265   if (NILP (memq_no_quit (symbol, Vimage_instantiator_format_list)))
266     Vimage_instantiator_format_list =
267       Fcons (symbol, Vimage_instantiator_format_list);
268 }
269
270 void
271 add_entry_to_image_instantiator_format_list (Lisp_Object symbol,
272                                              struct
273                                              image_instantiator_methods *meths)
274 {
275   add_entry_to_device_ii_format_list (Qnil, symbol, meths);
276 }
277
278 static Lisp_Object *
279 get_image_conversion_list (Lisp_Object console_type)
280 {
281   return &decode_console_type (console_type, ERROR_ME)->image_conversion_list;
282 }
283
284 DEFUN ("set-console-type-image-conversion-list", Fset_console_type_image_conversion_list,
285        2, 2, 0, /*
286 Set the image-conversion-list for consoles of the given CONSOLE-TYPE.
287 The image-conversion-list specifies how image instantiators that
288 are strings should be interpreted.  Each element of the list should be
289 a list of two elements (a regular expression string and a vector) or
290 a list of three elements (the preceding two plus an integer index into
291 the vector).  The string is converted to the vector associated with the
292 first matching regular expression.  If a vector index is specified, the
293 string itself is substituted into that position in the vector.
294
295 Note: The conversion above is applied when the image instantiator is
296 added to an image specifier, not when the specifier is actually
297 instantiated.  Therefore, changing the image-conversion-list only affects
298 newly-added instantiators.  Existing instantiators in glyphs and image
299 specifiers will not be affected.
300 */
301        (console_type, list))
302 {
303   Lisp_Object tail;
304   Lisp_Object *imlist = get_image_conversion_list (console_type);
305
306   /* Check the list to make sure that it only has valid entries. */
307
308   EXTERNAL_LIST_LOOP (tail, list)
309     {
310       Lisp_Object mapping = XCAR (tail);
311
312       /* Mapping form should be (STRING VECTOR) or (STRING VECTOR INTEGER) */
313       if (!CONSP (mapping) ||
314           !CONSP (XCDR (mapping)) ||
315           (!NILP (XCDR (XCDR (mapping))) &&
316            (!CONSP (XCDR (XCDR (mapping))) ||
317             !NILP (XCDR (XCDR (XCDR (mapping)))))))
318         signal_simple_error ("Invalid mapping form", mapping);
319       else
320         {
321           Lisp_Object exp = XCAR (mapping);
322           Lisp_Object typevec = XCAR (XCDR (mapping));
323           Lisp_Object pos = Qnil;
324           Lisp_Object newvec;
325           struct gcpro gcpro1;
326
327           CHECK_STRING (exp);
328           CHECK_VECTOR (typevec);
329           if (!NILP (XCDR (XCDR (mapping))))
330             {
331               pos = XCAR (XCDR (XCDR (mapping)));
332               CHECK_INT (pos);
333               if (XINT (pos) < 0 ||
334                   XINT (pos) >= XVECTOR_LENGTH (typevec))
335                 args_out_of_range_3
336                   (pos, Qzero, make_int (XVECTOR_LENGTH (typevec) - 1));
337             }
338
339           newvec = Fcopy_sequence (typevec);
340           if (INTP (pos))
341             XVECTOR_DATA (newvec)[XINT (pos)] = exp;
342           GCPRO1 (newvec);
343           image_validate (newvec);
344           UNGCPRO;
345         }
346     }
347
348   *imlist = Fcopy_tree (list, Qt);
349   return list;
350 }
351
352 DEFUN ("console-type-image-conversion-list", Fconsole_type_image_conversion_list,
353        1, 1, 0, /*
354 Return the image-conversion-list for devices of the given CONSOLE-TYPE.
355 The image-conversion-list specifies how to interpret image string
356 instantiators for the specified console type.  See
357 `set-console-type-image-conversion-list' for a description of its syntax.
358 */
359        (console_type))
360 {
361   return Fcopy_tree (*get_image_conversion_list (console_type), Qt);
362 }
363
364 /* Process a string instantiator according to the image-conversion-list for
365    CONSOLE_TYPE.  Returns a vector. */
366
367 static Lisp_Object
368 process_image_string_instantiator (Lisp_Object data,
369                                    Lisp_Object console_type,
370                                    int dest_mask)
371 {
372   Lisp_Object tail;
373
374   LIST_LOOP (tail, *get_image_conversion_list (console_type))
375     {
376       Lisp_Object mapping = XCAR (tail);
377       Lisp_Object exp = XCAR (mapping);
378       Lisp_Object typevec = XCAR (XCDR (mapping));
379
380       /* if the result is of a type that can't be instantiated
381          (e.g. a string when we're dealing with a pointer glyph),
382          skip it. */
383       if (!(dest_mask &
384             IIFORMAT_METH (decode_image_instantiator_format
385                            (INSTANTIATOR_TYPE (typevec), ERROR_ME),
386                            possible_dest_types, ())))
387         continue;
388       if (fast_string_match (exp, 0, data, 0, -1, 0, ERROR_ME, 0) >= 0)
389         {
390           if (!NILP (XCDR (XCDR (mapping))))
391             {
392               int pos = XINT (XCAR (XCDR (XCDR (mapping))));
393               Lisp_Object newvec = Fcopy_sequence (typevec);
394               XVECTOR_DATA (newvec)[pos] = data;
395               return newvec;
396             }
397           else
398             return typevec;
399         }
400     }
401
402   /* Oh well. */
403   signal_simple_error ("Unable to interpret glyph instantiator",
404                        data);
405
406   return Qnil;
407 }
408
409 Lisp_Object
410 find_keyword_in_vector_or_given (Lisp_Object vector, Lisp_Object keyword,
411                                  Lisp_Object default_)
412 {
413   Lisp_Object *elt;
414   int instantiator_len;
415
416   elt = XVECTOR_DATA (vector);
417   instantiator_len = XVECTOR_LENGTH (vector);
418
419   elt++;
420   instantiator_len--;
421
422   while (instantiator_len > 0)
423     {
424       if (EQ (elt[0], keyword))
425         return elt[1];
426       elt += 2;
427       instantiator_len -= 2;
428     }
429
430   return default_;
431 }
432
433 Lisp_Object
434 find_keyword_in_vector (Lisp_Object vector, Lisp_Object keyword)
435 {
436   return find_keyword_in_vector_or_given (vector, keyword, Qnil);
437 }
438
439 static Lisp_Object
440 find_instantiator_differences (Lisp_Object new, Lisp_Object old)
441 {
442   Lisp_Object alist = Qnil;
443   Lisp_Object *elt = XVECTOR_DATA (new);
444   Lisp_Object *old_elt = XVECTOR_DATA (old);
445   int len = XVECTOR_LENGTH (new);
446   struct gcpro gcpro1;
447
448   /* If the vector length has changed then consider everything
449      changed. We could try and figure out what properties have
450      disappeared or been added, but this code is only used as an
451      optimization anyway so lets not bother. */
452   if (len != XVECTOR_LENGTH (old))
453     return new;
454
455   GCPRO1 (alist);
456
457   for (len -= 2; len >= 1; len -= 2)
458     {
459       /* Keyword comparisons can be done with eq, the value must be
460          done with equal.
461          #### Note that this does not optimize re-ordering. */
462       if (!EQ (elt[len], old_elt[len])
463           || !internal_equal (elt[len+1], old_elt[len+1], 0))
464         alist = Fcons (Fcons (elt[len], elt[len+1]), alist);
465      }
466
467   {
468     Lisp_Object result = alist_to_tagged_vector (elt[0], alist);
469     free_alist (alist);
470     RETURN_UNGCPRO (result);
471   }
472 }
473
474 DEFUN ("set-instantiator-property", Fset_instantiator_property,
475        3, 3, 0, /*
476 Destructively set the property KEYWORD of INSTANTIATOR to VALUE.
477 If the property is not set then it is added to a copy of the
478 instantiator and the new instantiator returned.
479 Use `set-glyph-image' on glyphs to register instantiator changes.  */
480        (instantiator, keyword, value))
481 {
482   Lisp_Object *elt;
483   int len;
484
485   CHECK_VECTOR (instantiator);
486   if (!KEYWORDP (keyword))
487     signal_simple_error ("instantiator property must be a keyword", keyword);
488
489   elt = XVECTOR_DATA (instantiator);
490   len = XVECTOR_LENGTH (instantiator);
491
492   for (len -= 2; len >= 1; len -= 2)
493     {
494       if (EQ (elt[len], keyword))
495         {
496           elt[len+1] = value;
497           break;
498         }
499     }
500
501   /* Didn't find it so add it. */
502   if (len < 1)
503   {
504     Lisp_Object alist = Qnil, result;
505     struct gcpro gcpro1;
506
507     GCPRO1 (alist);
508     alist = tagged_vector_to_alist (instantiator);
509     alist = Fcons (Fcons (keyword, value), alist);
510     result = alist_to_tagged_vector (elt[0], alist);
511     free_alist (alist);
512     RETURN_UNGCPRO (result);
513   }
514
515   return instantiator;
516 }
517
518 void
519 check_valid_string (Lisp_Object data)
520 {
521   CHECK_STRING (data);
522 }
523
524 void
525 check_valid_vector (Lisp_Object data)
526 {
527   CHECK_VECTOR (data);
528 }
529
530 void
531 check_valid_face (Lisp_Object data)
532 {
533   Fget_face (data);
534 }
535
536 void
537 check_valid_int (Lisp_Object data)
538 {
539   CHECK_INT (data);
540 }
541
542 void
543 file_or_data_must_be_present (Lisp_Object instantiator)
544 {
545   if (NILP (find_keyword_in_vector (instantiator, Q_file)) &&
546       NILP (find_keyword_in_vector (instantiator, Q_data)))
547     signal_simple_error ("Must supply either :file or :data",
548                          instantiator);
549 }
550
551 void
552 data_must_be_present (Lisp_Object instantiator)
553 {
554   if (NILP (find_keyword_in_vector (instantiator, Q_data)))
555     signal_simple_error ("Must supply :data", instantiator);
556 }
557
558 static void
559 face_must_be_present (Lisp_Object instantiator)
560 {
561   if (NILP (find_keyword_in_vector (instantiator, Q_face)))
562     signal_simple_error ("Must supply :face", instantiator);
563 }
564
565 /* utility function useful in retrieving data from a file. */
566
567 Lisp_Object
568 make_string_from_file (Lisp_Object file)
569 {
570   /* This function can call lisp */
571   int count = specpdl_depth ();
572   Lisp_Object temp_buffer;
573   struct gcpro gcpro1;
574   Lisp_Object data;
575
576   specbind (Qinhibit_quit, Qt);
577   record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
578   temp_buffer = Fget_buffer_create (build_string (" *pixmap conversion*"));
579   GCPRO1 (temp_buffer);
580   set_buffer_internal (XBUFFER (temp_buffer));
581   Ferase_buffer (Qnil);
582   specbind (intern ("format-alist"), Qnil);
583   Finsert_file_contents_internal (file, Qnil, Qnil, Qnil, Qnil, Qnil, Qnil);
584   data = Fbuffer_substring (Qnil, Qnil, Qnil);
585   unbind_to (count, Qnil);
586   UNGCPRO;
587   return data;
588 }
589
590 /* The following two functions are provided to make it easier for
591    the normalize methods to work with keyword-value vectors.
592    Hash tables are kind of heavyweight for this purpose.
593    (If vectors were resizable, we could avoid this problem;
594    but they're not.) An alternative approach that might be
595    more efficient but require more work is to use a type of
596    assoc-Dynarr and provide primitives for deleting elements out
597    of it. (However, you'd also have to add an unwind-protect
598    to make sure the Dynarr got freed in case of an error in
599    the normalization process.) */
600
601 Lisp_Object
602 tagged_vector_to_alist (Lisp_Object vector)
603 {
604   Lisp_Object *elt = XVECTOR_DATA (vector);
605   int len = XVECTOR_LENGTH (vector);
606   Lisp_Object result = Qnil;
607
608   assert (len & 1);
609   for (len -= 2; len >= 1; len -= 2)
610     result = Fcons (Fcons (elt[len], elt[len+1]), result);
611
612   return result;
613 }
614
615 Lisp_Object
616 alist_to_tagged_vector (Lisp_Object tag, Lisp_Object alist)
617 {
618   int len = 1 + 2 * XINT (Flength (alist));
619   Lisp_Object *elt = alloca_array (Lisp_Object, len);
620   int i;
621   Lisp_Object rest;
622
623   i = 0;
624   elt[i++] = tag;
625   LIST_LOOP (rest, alist)
626     {
627       Lisp_Object pair = XCAR (rest);
628       elt[i] = XCAR (pair);
629       elt[i+1] = XCDR (pair);
630       i += 2;
631     }
632
633   return Fvector (len, elt);
634 }
635
636 #ifdef ERROR_CHECK_GLYPHS
637 static int
638 check_instance_cache_mapper (Lisp_Object key, Lisp_Object value,
639                              void *flag_closure)
640 {
641   /* This function can GC */
642   /* value can be nil; we cache failures as well as successes */
643   if (!NILP (value))
644     {
645       Lisp_Object window;
646       VOID_TO_LISP (window, flag_closure);
647       assert (EQ (XIMAGE_INSTANCE_DOMAIN (value), window));
648     }
649
650   return 0;
651 }
652
653 void
654 check_window_subwindow_cache (struct window* w)
655 {
656   Lisp_Object window;
657
658   XSETWINDOW (window, w);
659
660   assert (!NILP (w->subwindow_instance_cache));
661   elisp_maphash (check_instance_cache_mapper,
662                  w->subwindow_instance_cache,
663                  LISP_TO_VOID (window));
664 }
665
666 void
667 check_image_instance_structure (Lisp_Object instance)
668 {
669   /* Weird nothing images exist at startup when the console is
670      deleted. */
671   if (!NOTHING_IMAGE_INSTANCEP (instance))
672     {
673       assert (DOMAIN_LIVE_P (instance));
674       assert (VECTORP (XIMAGE_INSTANCE_INSTANTIATOR (instance)));
675     }
676   if (WINDOWP (XIMAGE_INSTANCE_DOMAIN (instance)))
677     check_window_subwindow_cache
678       (XWINDOW (XIMAGE_INSTANCE_DOMAIN (instance)));
679 }
680 #endif
681
682 /* Determine what kind of domain governs the image instance.
683    Verify that the given domain is at least as specific, and extract
684    the governing domain from it. */
685 static Lisp_Object
686 get_image_instantiator_governing_domain (Lisp_Object instantiator,
687                                          Lisp_Object domain)
688 {
689   int governing_domain;
690
691   struct image_instantiator_methods *meths =
692     decode_image_instantiator_format (INSTANTIATOR_TYPE (instantiator),
693                                       ERROR_ME);
694   governing_domain = IIFORMAT_METH_OR_GIVEN (meths, governing_domain, (),
695                                              GOVERNING_DOMAIN_DEVICE);
696
697   if (governing_domain == GOVERNING_DOMAIN_WINDOW
698       && NILP (DOMAIN_WINDOW (domain)))
699     signal_simple_error_2 ("Domain for this instantiator must be resolvable to a window",
700                            instantiator, domain);
701   else if (governing_domain == GOVERNING_DOMAIN_FRAME
702            && NILP (DOMAIN_FRAME (domain)))
703     signal_simple_error_2
704       ("Domain for this instantiator must be resolvable to a frame",
705        instantiator, domain);
706
707   if (governing_domain == GOVERNING_DOMAIN_WINDOW)
708     domain = DOMAIN_WINDOW (domain);
709   else if (governing_domain == GOVERNING_DOMAIN_FRAME)
710     domain = DOMAIN_FRAME (domain);
711   else if (governing_domain == GOVERNING_DOMAIN_DEVICE)
712     domain = DOMAIN_DEVICE (domain);
713   else
714     abort ();
715
716   return domain;
717 }
718
719 Lisp_Object
720 normalize_image_instantiator (Lisp_Object instantiator,
721                               Lisp_Object contype,
722                               Lisp_Object dest_mask)
723 {
724   if (IMAGE_INSTANCEP (instantiator))
725     return instantiator;
726
727   if (STRINGP (instantiator))
728     instantiator = process_image_string_instantiator (instantiator, contype,
729                                                       XINT (dest_mask));
730   /* Subsequent validation will pick this up. */
731   if (!VECTORP (instantiator))
732     return instantiator;
733   /* We have to always store the actual pixmap data and not the
734      filename even though this is a potential memory pig.  We have to
735      do this because it is quite possible that we will need to
736      instantiate a new instance of the pixmap and the file will no
737      longer exist (e.g. w3 pixmaps are almost always from temporary
738      files). */
739   {
740     struct gcpro gcpro1;
741     struct image_instantiator_methods *meths;
742
743     GCPRO1 (instantiator);
744
745     meths = decode_image_instantiator_format (INSTANTIATOR_TYPE (instantiator),
746                                               ERROR_ME);
747     RETURN_UNGCPRO (IIFORMAT_METH_OR_GIVEN (meths, normalize,
748                                             (instantiator, contype, dest_mask),
749                                             instantiator));
750   }
751 }
752
753 static Lisp_Object
754 instantiate_image_instantiator (Lisp_Object governing_domain,
755                                 Lisp_Object domain,
756                                 Lisp_Object instantiator,
757                                 Lisp_Object pointer_fg, Lisp_Object pointer_bg,
758                                 int dest_mask, Lisp_Object glyph)
759 {
760   Lisp_Object ii = allocate_image_instance (governing_domain,
761                                             IMAGE_INSTANCEP (domain) ?
762                                             domain : glyph, instantiator);
763   Lisp_Image_Instance* p = XIMAGE_INSTANCE (ii);
764   struct image_instantiator_methods *meths, *device_meths;
765   struct gcpro gcpro1;
766
767   GCPRO1 (ii);
768   if (!valid_image_instantiator_format_p (INSTANTIATOR_TYPE (instantiator),
769                                           DOMAIN_DEVICE (governing_domain)))
770     signal_simple_error
771       ("Image instantiator format is invalid in this locale.",
772        instantiator);
773
774   meths = decode_image_instantiator_format (INSTANTIATOR_TYPE (instantiator),
775                                             ERROR_ME);
776   MAYBE_IIFORMAT_METH (meths, instantiate, (ii, instantiator, pointer_fg,
777                                             pointer_bg, dest_mask, domain));
778
779   /* Now do device specific instantiation. */
780   device_meths = decode_device_ii_format (DOMAIN_DEVICE (governing_domain),
781                                           INSTANTIATOR_TYPE (instantiator),
782                                           ERROR_ME_NOT);
783
784   if (!HAS_IIFORMAT_METH_P (meths, instantiate)
785       && (!device_meths || !HAS_IIFORMAT_METH_P (device_meths, instantiate)))
786     signal_simple_error
787       ("Don't know how to instantiate this image instantiator?",
788        instantiator);
789
790   /* In general native window system methods will require sane
791      geometry values, thus the instance needs to have been laid-out
792      before they get called. */
793   image_instance_layout (ii, XIMAGE_INSTANCE_WIDTH (ii),
794                          XIMAGE_INSTANCE_HEIGHT (ii),
795                          IMAGE_UNCHANGED_GEOMETRY,
796                          IMAGE_UNCHANGED_GEOMETRY, domain);
797
798   MAYBE_IIFORMAT_METH (device_meths, instantiate, (ii, instantiator, pointer_fg,
799                                                    pointer_bg, dest_mask, domain));
800   /* Do post instantiation. */
801   MAYBE_IIFORMAT_METH (meths, post_instantiate, (ii, instantiator, domain));
802   MAYBE_IIFORMAT_METH (device_meths, post_instantiate, (ii, instantiator, domain));
803
804   /* We're done. */
805   IMAGE_INSTANCE_INITIALIZED (p) = 1;
806   /* Now that we're done verify that we really are laid out. */
807   if (IMAGE_INSTANCE_LAYOUT_CHANGED (p))
808     image_instance_layout (ii, XIMAGE_INSTANCE_WIDTH (ii),
809                            XIMAGE_INSTANCE_HEIGHT (ii),
810                            IMAGE_UNCHANGED_GEOMETRY,
811                            IMAGE_UNCHANGED_GEOMETRY, domain);
812
813   /* We *must* have a clean image at this point. */
814   IMAGE_INSTANCE_TEXT_CHANGED (p) = 0;
815   IMAGE_INSTANCE_SIZE_CHANGED (p) = 0;
816   IMAGE_INSTANCE_LAYOUT_CHANGED (p) = 0;
817   IMAGE_INSTANCE_DIRTYP (p) = 0;
818
819   assert ( XIMAGE_INSTANCE_HEIGHT (ii) >= 0
820            && XIMAGE_INSTANCE_WIDTH (ii) >= 0 );
821
822   ERROR_CHECK_IMAGE_INSTANCE (ii);
823
824   RETURN_UNGCPRO (ii);
825 }
826
827 \f
828 /****************************************************************************
829  *                          Image-Instance Object                           *
830  ****************************************************************************/
831
832 Lisp_Object Qimage_instancep;
833
834 static Lisp_Object
835 mark_image_instance (Lisp_Object obj)
836 {
837   Lisp_Image_Instance *i = XIMAGE_INSTANCE (obj);
838
839   /* #### I want to check the instance here, but there are way too
840      many instances of the instance being marked while the domain is
841      dead. For instance you can get marked through an event when using
842      callback_ex.*/
843 #if 0
844   ERROR_CHECK_IMAGE_INSTANCE (obj);
845 #endif
846
847   mark_object (i->name);
848   mark_object (i->instantiator);
849   /* Is this legal in marking? We may get in the situation where the
850      domain has been deleted - making the instance unusable. It seems
851      better to remove the domain so that it can be finalized. */
852   if (!DOMAIN_LIVE_P (i->domain))
853     i->domain = Qnil;
854   else
855     mark_object (i->domain);
856
857   /* We don't mark the glyph reference since that would create a
858      circularity preventing GC. Ditto the instantiator. */
859   switch (IMAGE_INSTANCE_TYPE (i))
860     {
861     case IMAGE_TEXT:
862       mark_object (IMAGE_INSTANCE_TEXT_STRING (i));
863       break;
864     case IMAGE_MONO_PIXMAP:
865     case IMAGE_COLOR_PIXMAP:
866       mark_object (IMAGE_INSTANCE_PIXMAP_FILENAME (i));
867       mark_object (IMAGE_INSTANCE_PIXMAP_MASK_FILENAME (i));
868       mark_object (IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (i));
869       mark_object (IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (i));
870       mark_object (IMAGE_INSTANCE_PIXMAP_FG (i));
871       mark_object (IMAGE_INSTANCE_PIXMAP_BG (i));
872       break;
873
874     case IMAGE_WIDGET:
875       mark_object (IMAGE_INSTANCE_WIDGET_TYPE (i));
876       mark_object (IMAGE_INSTANCE_WIDGET_PROPS (i));
877       mark_object (IMAGE_INSTANCE_SUBWINDOW_FACE (i));
878       mark_object (IMAGE_INSTANCE_WIDGET_ITEMS (i));
879       mark_object (IMAGE_INSTANCE_LAYOUT_CHILDREN (i));
880       mark_object (IMAGE_INSTANCE_WIDGET_PENDING_ITEMS (i));
881       mark_object (IMAGE_INSTANCE_WIDGET_HEIGHT_SUBR (i));
882       mark_object (IMAGE_INSTANCE_WIDGET_WIDTH_SUBR (i));
883     case IMAGE_SUBWINDOW:
884       break;
885
886     default:
887       break;
888     }
889
890   /* The image may have been previously finalized (yes that's weird,
891      see Fdelete_frame() and mark_window_as_deleted()), in which case
892      the domain will be nil, so cope with this. */
893   if (!NILP (IMAGE_INSTANCE_DEVICE (i)))
894     MAYBE_DEVMETH (XDEVICE (IMAGE_INSTANCE_DEVICE (i)),
895                    mark_image_instance, (i));
896
897   return i->device;
898 }
899
900 static void
901 print_image_instance (Lisp_Object obj, Lisp_Object printcharfun,
902                       int escapeflag)
903 {
904   char buf[100];
905   Lisp_Image_Instance *ii = XIMAGE_INSTANCE (obj);
906
907   if (print_readably)
908     error ("printing unreadable object #<image-instance 0x%x>",
909            ii->header.uid);
910   write_c_string ("#<image-instance (", printcharfun);
911   print_internal (Fimage_instance_type (obj), printcharfun, 0);
912   write_c_string (") ", printcharfun);
913   if (!NILP (ii->name))
914     {
915       print_internal (ii->name, printcharfun, 1);
916       write_c_string (" ", printcharfun);
917     }
918   write_c_string ("on ", printcharfun);
919   print_internal (ii->domain, printcharfun, 0);
920   write_c_string (" ", printcharfun);
921   switch (IMAGE_INSTANCE_TYPE (ii))
922     {
923     case IMAGE_NOTHING:
924       break;
925
926     case IMAGE_TEXT:
927       print_internal (IMAGE_INSTANCE_TEXT_STRING (ii), printcharfun, 1);
928       break;
929
930     case IMAGE_MONO_PIXMAP:
931     case IMAGE_COLOR_PIXMAP:
932     case IMAGE_POINTER:
933       if (STRINGP (IMAGE_INSTANCE_PIXMAP_FILENAME (ii)))
934         {
935           char *s;
936           Lisp_Object filename = IMAGE_INSTANCE_PIXMAP_FILENAME (ii);
937           s = strrchr ((char *) XSTRING_DATA (filename), '/');
938           if (s)
939             print_internal (build_string (s + 1), printcharfun, 1);
940           else
941             print_internal (filename, printcharfun, 1);
942         }
943       if (IMAGE_INSTANCE_PIXMAP_DEPTH (ii) > 1)
944         sprintf (buf, " %dx%dx%d", IMAGE_INSTANCE_PIXMAP_WIDTH (ii),
945                  IMAGE_INSTANCE_PIXMAP_HEIGHT (ii),
946                  IMAGE_INSTANCE_PIXMAP_DEPTH (ii));
947       else
948         sprintf (buf, " %dx%d", IMAGE_INSTANCE_PIXMAP_WIDTH (ii),
949                  IMAGE_INSTANCE_PIXMAP_HEIGHT (ii));
950       write_c_string (buf, printcharfun);
951       if (!NILP (IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (ii)) ||
952           !NILP (IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (ii)))
953         {
954           write_c_string (" @", printcharfun);
955           if (!NILP (IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (ii)))
956             {
957               long_to_string (buf, XINT (IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (ii)));
958               write_c_string (buf, printcharfun);
959             }
960           else
961             write_c_string ("??", printcharfun);
962           write_c_string (",", printcharfun);
963           if (!NILP (IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (ii)))
964             {
965               long_to_string (buf, XINT (IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (ii)));
966               write_c_string (buf, printcharfun);
967             }
968           else
969             write_c_string ("??", printcharfun);
970         }
971       if (!NILP (IMAGE_INSTANCE_PIXMAP_FG (ii)) ||
972           !NILP (IMAGE_INSTANCE_PIXMAP_BG (ii)))
973         {
974           write_c_string (" (", printcharfun);
975           if (!NILP (IMAGE_INSTANCE_PIXMAP_FG (ii)))
976             {
977               print_internal
978                 (XCOLOR_INSTANCE
979                  (IMAGE_INSTANCE_PIXMAP_FG (ii))->name, printcharfun, 0);
980             }
981           write_c_string ("/", printcharfun);
982           if (!NILP (IMAGE_INSTANCE_PIXMAP_BG (ii)))
983             {
984               print_internal
985                 (XCOLOR_INSTANCE
986                  (IMAGE_INSTANCE_PIXMAP_BG (ii))->name, printcharfun, 0);
987             }
988           write_c_string (")", printcharfun);
989         }
990       break;
991
992     case IMAGE_WIDGET:
993       print_internal (IMAGE_INSTANCE_WIDGET_TYPE (ii), printcharfun, 0);
994
995       if (GUI_ITEMP (IMAGE_INSTANCE_WIDGET_ITEM (ii)))
996         {
997           write_c_string (" ", printcharfun);
998           print_internal (IMAGE_INSTANCE_WIDGET_TEXT (ii), printcharfun, 1);
999         }
1000
1001       if (!NILP (IMAGE_INSTANCE_WIDGET_FACE (ii)))
1002         {
1003           write_c_string (" face=", printcharfun);
1004           print_internal
1005             (IMAGE_INSTANCE_WIDGET_FACE (ii), printcharfun, 0);
1006         }
1007       /* fallthrough */
1008
1009     case IMAGE_SUBWINDOW:
1010       sprintf (buf, " %dx%d", IMAGE_INSTANCE_WIDTH (ii),
1011                IMAGE_INSTANCE_HEIGHT (ii));
1012       write_c_string (buf, printcharfun);
1013
1014       /* This is stolen from frame.c.  Subwindows are strange in that they
1015          are specific to a particular frame so we want to print in their
1016          description what that frame is. */
1017
1018       write_c_string (" on #<", printcharfun);
1019       {
1020         struct frame* f  = XFRAME (IMAGE_INSTANCE_FRAME (ii));
1021
1022         if (!FRAME_LIVE_P (f))
1023           write_c_string ("dead", printcharfun);
1024         else
1025           write_c_string (DEVICE_TYPE_NAME (XDEVICE (FRAME_DEVICE (f))),
1026                           printcharfun);
1027       }
1028       write_c_string ("-frame>", printcharfun);
1029       sprintf (buf, " 0x%p", IMAGE_INSTANCE_SUBWINDOW_ID (ii));
1030       write_c_string (buf, printcharfun);
1031
1032       break;
1033
1034     default:
1035       abort ();
1036     }
1037
1038   MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain), print_image_instance,
1039                  (ii, printcharfun, escapeflag));
1040   sprintf (buf, " 0x%x>", ii->header.uid);
1041   write_c_string (buf, printcharfun);
1042 }
1043
1044 static void
1045 finalize_image_instance (void *header, int for_disksave)
1046 {
1047   Lisp_Image_Instance *i = (Lisp_Image_Instance *) header;
1048
1049   /* objects like this exist at dump time, so don't bomb out. */
1050   if (IMAGE_INSTANCE_TYPE (i) == IMAGE_NOTHING
1051       ||
1052       NILP (IMAGE_INSTANCE_DEVICE (i)))
1053     return;
1054   if (for_disksave) finalose (i);
1055
1056   /* We can't use the domain here, because it might have
1057      disappeared. */
1058   MAYBE_DEVMETH (XDEVICE (IMAGE_INSTANCE_DEVICE (i)),
1059                  finalize_image_instance, (i));
1060
1061   /* Make sure we don't try this twice. */
1062   IMAGE_INSTANCE_DEVICE (i) = Qnil;
1063 }
1064
1065 static int
1066 image_instance_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
1067 {
1068   Lisp_Image_Instance *i1 = XIMAGE_INSTANCE (obj1);
1069   Lisp_Image_Instance *i2 = XIMAGE_INSTANCE (obj2);
1070
1071   ERROR_CHECK_IMAGE_INSTANCE (obj1);
1072   ERROR_CHECK_IMAGE_INSTANCE (obj2);
1073
1074   if (!EQ (IMAGE_INSTANCE_DOMAIN (i1),
1075            IMAGE_INSTANCE_DOMAIN (i2))
1076       || IMAGE_INSTANCE_TYPE (i1) != IMAGE_INSTANCE_TYPE (i2)
1077       || IMAGE_INSTANCE_WIDTH (i1) != IMAGE_INSTANCE_WIDTH (i2)
1078       || IMAGE_INSTANCE_MARGIN_WIDTH (i1) !=
1079       IMAGE_INSTANCE_MARGIN_WIDTH (i2)
1080       || IMAGE_INSTANCE_HEIGHT (i1) != IMAGE_INSTANCE_HEIGHT (i2)
1081       || IMAGE_INSTANCE_XOFFSET (i1) != IMAGE_INSTANCE_XOFFSET (i2)
1082       || IMAGE_INSTANCE_YOFFSET (i1) != IMAGE_INSTANCE_YOFFSET (i2))
1083     return 0;
1084   if (!internal_equal (IMAGE_INSTANCE_NAME (i1), IMAGE_INSTANCE_NAME (i2),
1085                        depth + 1))
1086     return 0;
1087   if (!internal_equal (IMAGE_INSTANCE_INSTANTIATOR (i1),
1088                        IMAGE_INSTANCE_INSTANTIATOR (i2),
1089                        depth + 1))
1090     return 0;
1091
1092   switch (IMAGE_INSTANCE_TYPE (i1))
1093     {
1094     case IMAGE_NOTHING:
1095       break;
1096
1097     case IMAGE_TEXT:
1098       if (!internal_equal (IMAGE_INSTANCE_TEXT_STRING (i1),
1099                            IMAGE_INSTANCE_TEXT_STRING (i2),
1100                            depth + 1))
1101         return 0;
1102       break;
1103
1104     case IMAGE_MONO_PIXMAP:
1105     case IMAGE_COLOR_PIXMAP:
1106     case IMAGE_POINTER:
1107       if (!(IMAGE_INSTANCE_PIXMAP_DEPTH (i1) ==
1108             IMAGE_INSTANCE_PIXMAP_DEPTH (i2) &&
1109             IMAGE_INSTANCE_PIXMAP_SLICE (i1) ==
1110             IMAGE_INSTANCE_PIXMAP_SLICE (i2) &&
1111             EQ (IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (i1),
1112                 IMAGE_INSTANCE_PIXMAP_HOTSPOT_X (i2)) &&
1113             EQ (IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (i1),
1114                 IMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (i2)) &&
1115             internal_equal (IMAGE_INSTANCE_PIXMAP_FILENAME (i1),
1116                             IMAGE_INSTANCE_PIXMAP_FILENAME (i2),
1117                             depth + 1) &&
1118             internal_equal (IMAGE_INSTANCE_PIXMAP_MASK_FILENAME (i1),
1119                             IMAGE_INSTANCE_PIXMAP_MASK_FILENAME (i2),
1120                             depth + 1)))
1121         return 0;
1122       break;
1123
1124     case IMAGE_WIDGET:
1125       if (!(EQ (IMAGE_INSTANCE_WIDGET_TYPE (i1),
1126                 IMAGE_INSTANCE_WIDGET_TYPE (i2))
1127             && IMAGE_INSTANCE_SUBWINDOW_ID (i1) ==
1128             IMAGE_INSTANCE_SUBWINDOW_ID (i2)
1129             &&
1130             EQ (IMAGE_INSTANCE_WIDGET_FACE (i1),
1131                 IMAGE_INSTANCE_WIDGET_TYPE (i2))
1132             && internal_equal (IMAGE_INSTANCE_WIDGET_ITEMS (i1),
1133                                IMAGE_INSTANCE_WIDGET_ITEMS (i2),
1134                                depth + 1)
1135             && internal_equal (IMAGE_INSTANCE_LAYOUT_CHILDREN (i1),
1136                                IMAGE_INSTANCE_LAYOUT_CHILDREN (i2),
1137                                depth + 1)
1138             && internal_equal (IMAGE_INSTANCE_WIDGET_PROPS (i1),
1139                                IMAGE_INSTANCE_WIDGET_PROPS (i2),
1140                                depth + 1)
1141             && internal_equal (IMAGE_INSTANCE_WIDGET_WIDTH_SUBR (i1),
1142                                IMAGE_INSTANCE_WIDGET_WIDTH_SUBR (i2),
1143                                depth + 1)
1144             && internal_equal (IMAGE_INSTANCE_WIDGET_HEIGHT_SUBR (i1),
1145                                IMAGE_INSTANCE_WIDGET_HEIGHT_SUBR (i2),
1146                                depth + 1)
1147             ))
1148         return 0;
1149       break;
1150
1151     case IMAGE_SUBWINDOW:
1152       if (!(IMAGE_INSTANCE_SUBWINDOW_ID (i1) ==
1153             IMAGE_INSTANCE_SUBWINDOW_ID (i2)))
1154         return 0;
1155       break;
1156
1157     default:
1158       abort ();
1159     }
1160
1161   return DEVMETH_OR_GIVEN (DOMAIN_XDEVICE (i1->domain),
1162                            image_instance_equal, (i1, i2, depth), 1);
1163 }
1164
1165 /* Image instance domain manipulators. We can't error check in these
1166    otherwise we get into infinite recursion. */
1167 Lisp_Object
1168 image_instance_device (Lisp_Object instance)
1169 {
1170   return XIMAGE_INSTANCE_DEVICE (instance);
1171 }
1172
1173 Lisp_Object
1174 image_instance_frame (Lisp_Object instance)
1175 {
1176   return XIMAGE_INSTANCE_FRAME (instance);
1177 }
1178
1179 Lisp_Object
1180 image_instance_window (Lisp_Object instance)
1181 {
1182   return DOMAIN_WINDOW (XIMAGE_INSTANCE_DOMAIN (instance));
1183 }
1184
1185 int
1186 image_instance_live_p (Lisp_Object instance)
1187 {
1188   return DOMAIN_LIVE_P (XIMAGE_INSTANCE_DOMAIN (instance));
1189 }
1190
1191 static unsigned long
1192 image_instance_hash (Lisp_Object obj, int depth)
1193 {
1194   Lisp_Image_Instance *i = XIMAGE_INSTANCE (obj);
1195   unsigned long hash = HASH5 (LISP_HASH (IMAGE_INSTANCE_DOMAIN (i)),
1196                               IMAGE_INSTANCE_WIDTH (i),
1197                               IMAGE_INSTANCE_MARGIN_WIDTH (i),
1198                               IMAGE_INSTANCE_HEIGHT (i),
1199                               internal_hash (IMAGE_INSTANCE_INSTANTIATOR (i),
1200                                              depth + 1));
1201
1202   ERROR_CHECK_IMAGE_INSTANCE (obj);
1203
1204   switch (IMAGE_INSTANCE_TYPE (i))
1205     {
1206     case IMAGE_NOTHING:
1207       break;
1208
1209     case IMAGE_TEXT:
1210       hash = HASH2 (hash, internal_hash (IMAGE_INSTANCE_TEXT_STRING (i),
1211                                          depth + 1));
1212       break;
1213
1214     case IMAGE_MONO_PIXMAP:
1215     case IMAGE_COLOR_PIXMAP:
1216     case IMAGE_POINTER:
1217       hash = HASH4 (hash, IMAGE_INSTANCE_PIXMAP_DEPTH (i),
1218                     IMAGE_INSTANCE_PIXMAP_SLICE (i),
1219                     internal_hash (IMAGE_INSTANCE_PIXMAP_FILENAME (i),
1220                                    depth + 1));
1221       break;
1222
1223     case IMAGE_WIDGET:
1224       /* We need the hash to be equivalent to what should be
1225          displayed. */
1226       hash = HASH5 (hash,
1227                     LISP_HASH (IMAGE_INSTANCE_WIDGET_TYPE (i)),
1228                     internal_hash (IMAGE_INSTANCE_WIDGET_PROPS (i), depth + 1),
1229                     internal_hash (IMAGE_INSTANCE_WIDGET_ITEMS (i), depth + 1),
1230                     internal_hash (IMAGE_INSTANCE_LAYOUT_CHILDREN (i),
1231                                    depth + 1));
1232     case IMAGE_SUBWINDOW:
1233       hash = HASH2 (hash, (EMACS_INT) IMAGE_INSTANCE_SUBWINDOW_ID (i));
1234       break;
1235
1236     default:
1237       abort ();
1238     }
1239
1240   return HASH2 (hash, DEVMETH_OR_GIVEN
1241                 (XDEVICE (image_instance_device (obj)),
1242                  image_instance_hash, (i, depth),
1243                  0));
1244 }
1245
1246 DEFINE_LRECORD_IMPLEMENTATION ("image-instance", image_instance,
1247                                mark_image_instance, print_image_instance,
1248                                finalize_image_instance, image_instance_equal,
1249                                image_instance_hash, 0,
1250                                Lisp_Image_Instance);
1251
1252 static Lisp_Object
1253 allocate_image_instance (Lisp_Object governing_domain, Lisp_Object parent,
1254                          Lisp_Object instantiator)
1255 {
1256   Lisp_Image_Instance *lp =
1257     alloc_lcrecord_type (Lisp_Image_Instance, &lrecord_image_instance);
1258   Lisp_Object val;
1259
1260   zero_lcrecord (lp);
1261   /* It's not possible to simply keep a record of the domain in which
1262      the instance was instantiated. This is because caching may mean
1263      that the domain becomes invalid but the instance remains
1264      valid. However, the only truly relevant domain is the domain in
1265      which the instance is cached since this is the one that will be
1266      common to the instances. */
1267   lp->domain = governing_domain;
1268   /* The cache domain is not quite sufficient since the domain can get
1269      deleted before the image instance does. We need to know the
1270      domain device in order to finalize the image instance
1271      properly. We therefore record the device also. */
1272   lp->device = DOMAIN_DEVICE (governing_domain);
1273   lp->type = IMAGE_NOTHING;
1274   lp->name = Qnil;
1275   lp->x_offset = 0;
1276   lp->y_offset = 0;
1277   lp->width = IMAGE_UNSPECIFIED_GEOMETRY;
1278   lp->margin_width = 0;
1279   lp->height = IMAGE_UNSPECIFIED_GEOMETRY;
1280   lp->parent = parent;
1281   lp->instantiator = instantiator;
1282   /* So that layouts get done. */
1283   lp->layout_changed = 1;
1284   lp->initialized = 0;
1285
1286   XSETIMAGE_INSTANCE (val, lp);
1287   MARK_GLYPHS_CHANGED;
1288
1289   return val;
1290 }
1291
1292 static enum image_instance_type
1293 decode_image_instance_type (Lisp_Object type, Error_behavior errb)
1294 {
1295   if (ERRB_EQ (errb, ERROR_ME))
1296     CHECK_SYMBOL (type);
1297
1298   if (EQ (type, Qnothing))      return IMAGE_NOTHING;
1299   if (EQ (type, Qtext))         return IMAGE_TEXT;
1300   if (EQ (type, Qmono_pixmap))  return IMAGE_MONO_PIXMAP;
1301   if (EQ (type, Qcolor_pixmap)) return IMAGE_COLOR_PIXMAP;
1302   if (EQ (type, Qpointer))      return IMAGE_POINTER;
1303   if (EQ (type, Qsubwindow))    return IMAGE_SUBWINDOW;
1304   if (EQ (type, Qwidget))    return IMAGE_WIDGET;
1305
1306   maybe_signal_simple_error ("Invalid image-instance type", type,
1307                              Qimage, errb);
1308
1309   return IMAGE_UNKNOWN; /* not reached */
1310 }
1311
1312 static Lisp_Object
1313 encode_image_instance_type (enum image_instance_type type)
1314 {
1315   switch (type)
1316     {
1317     case IMAGE_NOTHING:      return Qnothing;
1318     case IMAGE_TEXT:         return Qtext;
1319     case IMAGE_MONO_PIXMAP:  return Qmono_pixmap;
1320     case IMAGE_COLOR_PIXMAP: return Qcolor_pixmap;
1321     case IMAGE_POINTER:      return Qpointer;
1322     case IMAGE_SUBWINDOW:    return Qsubwindow;
1323     case IMAGE_WIDGET:    return Qwidget;
1324     default:
1325       abort ();
1326     }
1327
1328   return Qnil; /* not reached */
1329 }
1330
1331 static int
1332 decode_image_instance_type_list (Lisp_Object list)
1333 {
1334   Lisp_Object rest;
1335   int mask = 0;
1336
1337   if (NILP (list))
1338     return ~0;
1339
1340   if (!CONSP (list))
1341     {
1342       enum image_instance_type type =
1343         decode_image_instance_type (list, ERROR_ME);
1344       return image_instance_type_to_mask (type);
1345     }
1346
1347   EXTERNAL_LIST_LOOP (rest, list)
1348     {
1349       enum image_instance_type type =
1350         decode_image_instance_type (XCAR (rest), ERROR_ME);
1351       mask |= image_instance_type_to_mask (type);
1352     }
1353
1354   return mask;
1355 }
1356
1357 static Lisp_Object
1358 encode_image_instance_type_list (int mask)
1359 {
1360   int count = 0;
1361   Lisp_Object result = Qnil;
1362
1363   while (mask)
1364     {
1365       count++;
1366       if (mask & 1)
1367         result = Fcons (encode_image_instance_type
1368                         ((enum image_instance_type) count), result);
1369       mask >>= 1;
1370     }
1371
1372   return Fnreverse (result);
1373 }
1374
1375 DOESNT_RETURN
1376 incompatible_image_types (Lisp_Object instantiator, int given_dest_mask,
1377                           int desired_dest_mask)
1378 {
1379   signal_error
1380     (Qerror,
1381      list2
1382      (emacs_doprnt_string_lisp_2
1383       ((const Bufbyte *)
1384        "No compatible image-instance types given: wanted one of %s, got %s",
1385        Qnil, -1, 2,
1386        encode_image_instance_type_list (desired_dest_mask),
1387        encode_image_instance_type_list (given_dest_mask)),
1388       instantiator));
1389 }
1390
1391 static int
1392 valid_image_instance_type_p (Lisp_Object type)
1393 {
1394   return !NILP (memq_no_quit (type, Vimage_instance_type_list));
1395 }
1396
1397 DEFUN ("valid-image-instance-type-p", Fvalid_image_instance_type_p, 1, 1, 0, /*
1398 Given an IMAGE-INSTANCE-TYPE, return non-nil if it is valid.
1399 Valid types are some subset of 'nothing, 'text, 'mono-pixmap, 'color-pixmap,
1400 'pointer, 'subwindow, and 'widget, depending on how XEmacs was compiled.
1401 */
1402        (image_instance_type))
1403 {
1404   return valid_image_instance_type_p (image_instance_type) ? Qt : Qnil;
1405 }
1406
1407 DEFUN ("image-instance-type-list", Fimage_instance_type_list, 0, 0, 0, /*
1408 Return a list of valid image-instance types.
1409 */
1410        ())
1411 {
1412   return Fcopy_sequence (Vimage_instance_type_list);
1413 }
1414
1415 Error_behavior
1416 decode_error_behavior_flag (Lisp_Object noerror)
1417 {
1418   if (NILP (noerror))        return ERROR_ME;
1419   else if (EQ (noerror, Qt)) return ERROR_ME_NOT;
1420   else                       return ERROR_ME_WARN;
1421 }
1422
1423 Lisp_Object
1424 encode_error_behavior_flag (Error_behavior errb)
1425 {
1426   if (ERRB_EQ (errb, ERROR_ME))
1427     return Qnil;
1428   else if (ERRB_EQ (errb, ERROR_ME_NOT))
1429     return Qt;
1430   else
1431     {
1432       assert (ERRB_EQ (errb, ERROR_ME_WARN));
1433       return Qwarning;
1434     }
1435 }
1436
1437 /* Recurse up the hierarchy looking for the topmost glyph. This means
1438    that instances in layouts will inherit face properties from their
1439    parent. */
1440 Lisp_Object image_instance_parent_glyph (Lisp_Image_Instance* ii)
1441 {
1442   if (IMAGE_INSTANCEP (IMAGE_INSTANCE_PARENT (ii)))
1443     {
1444       return image_instance_parent_glyph
1445         (XIMAGE_INSTANCE (IMAGE_INSTANCE_PARENT (ii)));
1446     }
1447   return IMAGE_INSTANCE_PARENT (ii);
1448 }
1449
1450 static Lisp_Object
1451 make_image_instance_1 (Lisp_Object data, Lisp_Object domain,
1452                        Lisp_Object dest_types)
1453 {
1454   Lisp_Object ii;
1455   struct gcpro gcpro1;
1456   int dest_mask;
1457   Lisp_Object governing_domain;
1458
1459   if (IMAGE_INSTANCEP (data))
1460     signal_simple_error ("Image instances not allowed here", data);
1461   image_validate (data);
1462   domain = decode_domain (domain);
1463   /* instantiate_image_instantiator() will abort if given an
1464      image instance ... */
1465   dest_mask = decode_image_instance_type_list (dest_types);
1466   data = normalize_image_instantiator (data,
1467                                        DEVICE_TYPE (DOMAIN_XDEVICE (domain)),
1468                                        make_int (dest_mask));
1469   GCPRO1 (data);
1470   /* After normalizing the data, it's always either an image instance (which
1471      we filtered out above) or a vector. */
1472   if (EQ (INSTANTIATOR_TYPE (data), Qinherit))
1473     signal_simple_error ("Inheritance not allowed here", data);
1474   governing_domain =
1475     get_image_instantiator_governing_domain (data, domain);
1476   ii = instantiate_image_instantiator (governing_domain, domain, data,
1477                                        Qnil, Qnil, dest_mask, Qnil);
1478   RETURN_UNGCPRO (ii);
1479 }
1480
1481 DEFUN ("make-image-instance", Fmake_image_instance, 1, 4, 0, /*
1482 Return a new `image-instance' object.
1483
1484 Image-instance objects encapsulate the way a particular image (pixmap,
1485 etc.) is displayed on a particular device.  In most circumstances, you
1486 do not need to directly create image instances; use a glyph instead.
1487 However, it may occasionally be useful to explicitly create image
1488 instances, if you want more control over the instantiation process.
1489
1490 DATA is an image instantiator, which describes the image; see
1491 `make-image-specifier' for a description of the allowed values.
1492
1493 DEST-TYPES should be a list of allowed image instance types that can
1494 be generated.  The recognized image instance types are
1495
1496 'nothing
1497   Nothing is displayed.
1498 'text
1499   Displayed as text.  The foreground and background colors and the
1500   font of the text are specified independent of the pixmap.  Typically
1501   these attributes will come from the face of the surrounding text,
1502   unless a face is specified for the glyph in which the image appears.
1503 'mono-pixmap
1504   Displayed as a mono pixmap (a pixmap with only two colors where the
1505   foreground and background can be specified independent of the pixmap;
1506   typically the pixmap assumes the foreground and background colors of
1507   the text around it, unless a face is specified for the glyph in which
1508   the image appears).
1509 'color-pixmap
1510   Displayed as a color pixmap.
1511 'pointer
1512   Used as the mouse pointer for a window.
1513 'subwindow
1514   A child window that is treated as an image.  This allows (e.g.)
1515   another program to be responsible for drawing into the window.
1516 'widget
1517   A child window that contains a window-system widget, e.g. a push
1518   button, text field, or slider.
1519
1520 The DEST-TYPES list is unordered.  If multiple destination types are
1521 possible for a given instantiator, the "most natural" type for the
1522 instantiator's format is chosen. (For XBM, the most natural types are
1523 `mono-pixmap', followed by `color-pixmap', followed by `pointer'.  For
1524 the other normal image formats, the most natural types are
1525 `color-pixmap', followed by `mono-pixmap', followed by `pointer'.  For
1526 the string and formatted-string formats, the most natural types are
1527 `text', followed by `mono-pixmap' (not currently implemented),
1528 followed by `color-pixmap' (not currently implemented).  For MS
1529 Windows resources, the most natural type for pointer resources is
1530 `pointer', and for the others it's `color-pixmap'.  The other formats
1531 can only be instantiated as one type. (If you want to control more
1532 specifically the order of the types into which an image is
1533 instantiated, just call `make-image-instance' repeatedly until it
1534 succeeds, passing less and less preferred destination types each
1535 time.)
1536
1537 See `make-image-specifier' for a description of the different image
1538 instantiator formats.
1539
1540 If DEST-TYPES is omitted, all possible types are allowed.
1541
1542 DOMAIN specifies the domain to which the image instance will be attached.
1543 This domain is termed the \"governing domain\".  The type of the governing
1544 domain depends on the image instantiator format. (Although, more correctly,
1545 it should probably depend on the image instance type.) For example, pixmap
1546 image instances are specific to a device, but widget image instances are
1547 specific to a particular XEmacs window because in order to display such a
1548 widget when two windows onto the same buffer want to display the widget,
1549 two separate underlying widgets must be created. (That's because a widget
1550 is actually a child window-system window, and all window-system windows have
1551 a unique existence on the screen.) This means that the governing domain for
1552 a pixmap image instance will be some device (most likely, the only existing
1553 device), whereas the governing domain for a widget image instance will be
1554 some XEmacs window.
1555
1556 If you specify an overly general DOMAIN (e.g. a frame when a window was
1557 wanted), an error is signaled.  If you specify an overly specific DOMAIN
1558 \(e.g. a window when a device was wanted), the corresponding general domain
1559 is fetched and used instead.  For `make-image-instance', it makes no
1560 difference whether you specify an overly specific domain or the properly
1561 general domain derived from it.  However, it does matter when creating an
1562 image instance by instantiating a specifier or glyph (e.g. with
1563 `glyph-image-instance'), because the more specific domain causes spec lookup
1564 to start there and proceed to more general domains. (It would also matter
1565 when creating an image instance with an instantiator format of `inherit',
1566 but we currently disallow this. #### We should fix this.)
1567
1568 If omitted, DOMAIN defaults to the selected window.
1569
1570 NOERROR controls what happens when the image cannot be generated.
1571 If nil, an error message is generated.  If t, no messages are
1572 generated and this function returns nil.  If anything else, a warning
1573 message is generated and this function returns nil.
1574 */
1575        (data, domain, dest_types, noerror))
1576 {
1577   Error_behavior errb = decode_error_behavior_flag (noerror);
1578
1579   return call_with_suspended_errors ((lisp_fn_t) make_image_instance_1,
1580                                      Qnil, Qimage, errb,
1581                                      3, data, domain, dest_types);
1582 }
1583
1584 DEFUN ("image-instance-p", Fimage_instance_p, 1, 1, 0, /*
1585 Return non-nil if OBJECT is an image instance.
1586 */
1587        (object))
1588 {
1589   return IMAGE_INSTANCEP (object) ? Qt : Qnil;
1590 }
1591
1592 DEFUN ("image-instance-type", Fimage_instance_type, 1, 1, 0, /*
1593 Return the type of the given image instance.
1594 The return value will be one of 'nothing, 'text, 'mono-pixmap,
1595 'color-pixmap, 'pointer, or 'subwindow.
1596 */
1597        (image_instance))
1598 {
1599   CHECK_IMAGE_INSTANCE (image_instance);
1600   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1601   return encode_image_instance_type (XIMAGE_INSTANCE_TYPE (image_instance));
1602 }
1603
1604 DEFUN ("image-instance-name", Fimage_instance_name, 1, 1, 0, /*
1605 Return the name of the given image instance.
1606 */
1607        (image_instance))
1608 {
1609   CHECK_IMAGE_INSTANCE (image_instance);
1610   return XIMAGE_INSTANCE_NAME (image_instance);
1611 }
1612
1613 DEFUN ("image-instance-domain", Fimage_instance_domain, 1, 1, 0, /*
1614 Return the governing domain of the given image instance.
1615 The governing domain of an image instance is the domain that the image
1616 instance is specific to.  It is NOT necessarily the domain that was
1617 given to the call to `specifier-instance' that resulted in the creation
1618 of this image instance.  See `make-image-instance' for more information
1619 on governing domains.
1620 */
1621        (image_instance))
1622 {
1623   CHECK_IMAGE_INSTANCE (image_instance);
1624   return XIMAGE_INSTANCE_DOMAIN (image_instance);
1625 }
1626
1627 DEFUN ("image-instance-string", Fimage_instance_string, 1, 1, 0, /*
1628 Return the string of the given image instance.
1629 This will only be non-nil for text image instances and widgets.
1630 */
1631        (image_instance))
1632 {
1633   CHECK_IMAGE_INSTANCE (image_instance);
1634   if (XIMAGE_INSTANCE_TYPE (image_instance) == IMAGE_TEXT)
1635     return XIMAGE_INSTANCE_TEXT_STRING (image_instance);
1636   else if (XIMAGE_INSTANCE_TYPE (image_instance) == IMAGE_WIDGET)
1637     return XIMAGE_INSTANCE_WIDGET_TEXT (image_instance);
1638   else
1639     return Qnil;
1640 }
1641
1642 DEFUN ("image-instance-property", Fimage_instance_property, 2, 2, 0, /*
1643 Return the given property of the given image instance.
1644 Returns nil if the property or the property method do not exist for
1645 the image instance in the domain.
1646 */
1647        (image_instance, prop))
1648 {
1649   Lisp_Image_Instance* ii;
1650   Lisp_Object type, ret;
1651   struct image_instantiator_methods* meths;
1652
1653   CHECK_IMAGE_INSTANCE (image_instance);
1654   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1655   CHECK_SYMBOL (prop);
1656   ii = XIMAGE_INSTANCE (image_instance);
1657
1658   /* ... then try device specific methods ... */
1659   type = encode_image_instance_type (IMAGE_INSTANCE_TYPE (ii));
1660   meths = decode_device_ii_format (image_instance_device (image_instance),
1661                                    type, ERROR_ME_NOT);
1662   if (meths && HAS_IIFORMAT_METH_P (meths, property)
1663       &&
1664       !UNBOUNDP (ret =  IIFORMAT_METH (meths, property, (image_instance, prop))))
1665     {
1666       return ret;
1667     }
1668   /* ... then format specific methods ... */
1669   meths = decode_device_ii_format (Qnil, type, ERROR_ME_NOT);
1670   if (meths && HAS_IIFORMAT_METH_P (meths, property)
1671       &&
1672       !UNBOUNDP (ret = IIFORMAT_METH (meths, property, (image_instance, prop))))
1673     {
1674       return ret;
1675     }
1676   /* ... then fail */
1677   return Qnil;
1678 }
1679
1680 DEFUN ("image-instance-file-name", Fimage_instance_file_name, 1, 1, 0, /*
1681 Return the file name from which IMAGE-INSTANCE was read, if known.
1682 */
1683        (image_instance))
1684 {
1685   CHECK_IMAGE_INSTANCE (image_instance);
1686   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1687
1688   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1689     {
1690     case IMAGE_MONO_PIXMAP:
1691     case IMAGE_COLOR_PIXMAP:
1692     case IMAGE_POINTER:
1693       return XIMAGE_INSTANCE_PIXMAP_FILENAME (image_instance);
1694
1695     default:
1696       return Qnil;
1697     }
1698 }
1699
1700 DEFUN ("image-instance-mask-file-name", Fimage_instance_mask_file_name, 1, 1, 0, /*
1701 Return the file name from which IMAGE-INSTANCE's mask was read, if known.
1702 */
1703        (image_instance))
1704 {
1705   CHECK_IMAGE_INSTANCE (image_instance);
1706   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1707
1708   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1709     {
1710     case IMAGE_MONO_PIXMAP:
1711     case IMAGE_COLOR_PIXMAP:
1712     case IMAGE_POINTER:
1713       return XIMAGE_INSTANCE_PIXMAP_MASK_FILENAME (image_instance);
1714
1715     default:
1716       return Qnil;
1717     }
1718 }
1719
1720 DEFUN ("image-instance-depth", Fimage_instance_depth, 1, 1, 0, /*
1721 Return the depth of the image instance.
1722 This is 0 for a bitmap, or a positive integer for a pixmap.
1723 */
1724        (image_instance))
1725 {
1726   CHECK_IMAGE_INSTANCE (image_instance);
1727   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1728
1729   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1730     {
1731     case IMAGE_MONO_PIXMAP:
1732     case IMAGE_COLOR_PIXMAP:
1733     case IMAGE_POINTER:
1734       return make_int (XIMAGE_INSTANCE_PIXMAP_DEPTH (image_instance));
1735
1736     default:
1737       return Qnil;
1738     }
1739 }
1740
1741 DEFUN ("image-instance-height", Fimage_instance_height, 1, 1, 0, /*
1742 Return the height of the image instance, in pixels.
1743 */
1744        (image_instance))
1745 {
1746   CHECK_IMAGE_INSTANCE (image_instance);
1747   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1748
1749   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1750     {
1751     case IMAGE_MONO_PIXMAP:
1752     case IMAGE_COLOR_PIXMAP:
1753     case IMAGE_POINTER:
1754     case IMAGE_SUBWINDOW:
1755     case IMAGE_WIDGET:
1756       return make_int (XIMAGE_INSTANCE_HEIGHT (image_instance));
1757
1758     default:
1759       return Qnil;
1760     }
1761 }
1762
1763 DEFUN ("image-instance-width", Fimage_instance_width, 1, 1, 0, /*
1764 Return the width of the image instance, in pixels.
1765 */
1766        (image_instance))
1767 {
1768   CHECK_IMAGE_INSTANCE (image_instance);
1769   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1770
1771   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1772     {
1773     case IMAGE_MONO_PIXMAP:
1774     case IMAGE_COLOR_PIXMAP:
1775     case IMAGE_POINTER:
1776     case IMAGE_SUBWINDOW:
1777     case IMAGE_WIDGET:
1778       return make_int (XIMAGE_INSTANCE_WIDTH (image_instance));
1779
1780     default:
1781       return Qnil;
1782     }
1783 }
1784
1785 DEFUN ("image-instance-hotspot-x", Fimage_instance_hotspot_x, 1, 1, 0, /*
1786 Return the X coordinate of the image instance's hotspot, if known.
1787 This is a point relative to the origin of the pixmap.  When an image is
1788  used as a mouse pointer, the hotspot is the point on the image that sits
1789  over the location that the pointer points to.  This is, for example, the
1790  tip of the arrow or the center of the crosshairs.
1791 This will always be nil for a non-pointer image instance.
1792 */
1793        (image_instance))
1794 {
1795   CHECK_IMAGE_INSTANCE (image_instance);
1796   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1797
1798   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1799     {
1800     case IMAGE_MONO_PIXMAP:
1801     case IMAGE_COLOR_PIXMAP:
1802     case IMAGE_POINTER:
1803       return XIMAGE_INSTANCE_PIXMAP_HOTSPOT_X (image_instance);
1804
1805     default:
1806       return Qnil;
1807     }
1808 }
1809
1810 DEFUN ("image-instance-hotspot-y", Fimage_instance_hotspot_y, 1, 1, 0, /*
1811 Return the Y coordinate of the image instance's hotspot, if known.
1812 This is a point relative to the origin of the pixmap.  When an image is
1813  used as a mouse pointer, the hotspot is the point on the image that sits
1814  over the location that the pointer points to.  This is, for example, the
1815  tip of the arrow or the center of the crosshairs.
1816 This will always be nil for a non-pointer image instance.
1817 */
1818        (image_instance))
1819 {
1820   CHECK_IMAGE_INSTANCE (image_instance);
1821   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1822
1823   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1824     {
1825     case IMAGE_MONO_PIXMAP:
1826     case IMAGE_COLOR_PIXMAP:
1827     case IMAGE_POINTER:
1828       return XIMAGE_INSTANCE_PIXMAP_HOTSPOT_Y (image_instance);
1829
1830     default:
1831       return Qnil;
1832     }
1833 }
1834
1835 DEFUN ("image-instance-foreground", Fimage_instance_foreground, 1, 1, 0, /*
1836 Return the foreground color of IMAGE-INSTANCE, if applicable.
1837 This will be a color instance or nil. (It will only be non-nil for
1838 colorized mono pixmaps and for pointers.)
1839 */
1840        (image_instance))
1841 {
1842   CHECK_IMAGE_INSTANCE (image_instance);
1843   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1844
1845   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1846     {
1847     case IMAGE_MONO_PIXMAP:
1848     case IMAGE_COLOR_PIXMAP:
1849     case IMAGE_POINTER:
1850       return XIMAGE_INSTANCE_PIXMAP_FG (image_instance);
1851
1852     case IMAGE_WIDGET:
1853       return FACE_FOREGROUND (
1854                               XIMAGE_INSTANCE_WIDGET_FACE (image_instance),
1855                               XIMAGE_INSTANCE_FRAME
1856                               (image_instance));
1857
1858     default:
1859       return Qnil;
1860     }
1861 }
1862
1863 DEFUN ("image-instance-background", Fimage_instance_background, 1, 1, 0, /*
1864 Return the background color of IMAGE-INSTANCE, if applicable.
1865 This will be a color instance or nil. (It will only be non-nil for
1866 colorized mono pixmaps and for pointers.)
1867 */
1868        (image_instance))
1869 {
1870   CHECK_IMAGE_INSTANCE (image_instance);
1871   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1872
1873   switch (XIMAGE_INSTANCE_TYPE (image_instance))
1874     {
1875     case IMAGE_MONO_PIXMAP:
1876     case IMAGE_COLOR_PIXMAP:
1877     case IMAGE_POINTER:
1878       return XIMAGE_INSTANCE_PIXMAP_BG (image_instance);
1879
1880     case IMAGE_WIDGET:
1881       return FACE_BACKGROUND (
1882                               XIMAGE_INSTANCE_WIDGET_FACE (image_instance),
1883                               XIMAGE_INSTANCE_FRAME
1884                               (image_instance));
1885
1886     default:
1887       return Qnil;
1888     }
1889 }
1890
1891
1892 DEFUN ("colorize-image-instance", Fcolorize_image_instance, 3, 3, 0, /*
1893 Make the image instance be displayed in the given colors.
1894 This function returns a new image instance that is exactly like the
1895 specified one except that (if possible) the foreground and background
1896 colors and as specified.  Currently, this only does anything if the image
1897 instance is a mono pixmap; otherwise, the same image instance is returned.
1898 */
1899        (image_instance, foreground, background))
1900 {
1901   Lisp_Object new;
1902   Lisp_Object device;
1903
1904   CHECK_IMAGE_INSTANCE (image_instance);
1905   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1906   CHECK_COLOR_INSTANCE (foreground);
1907   CHECK_COLOR_INSTANCE (background);
1908
1909   device = image_instance_device (image_instance);
1910   if (!HAS_DEVMETH_P (XDEVICE (device), colorize_image_instance))
1911     return image_instance;
1912
1913   /* #### There should be a copy_image_instance(), which calls a
1914      device-specific method to copy the window-system subobject. */
1915   new = allocate_image_instance (XIMAGE_INSTANCE_DOMAIN (image_instance),
1916                                  Qnil, Qnil);
1917   copy_lcrecord (XIMAGE_INSTANCE (new), XIMAGE_INSTANCE (image_instance));
1918   /* note that if this method returns non-zero, this method MUST
1919      copy any window-system resources, so that when one image instance is
1920      freed, the other one is not hosed. */
1921   if (!DEVMETH (XDEVICE (device), colorize_image_instance, (new, foreground,
1922                                                             background)))
1923     return image_instance;
1924   return new;
1925 }
1926
1927
1928 /************************************************************************/
1929 /*                              Geometry calculations                   */
1930 /************************************************************************/
1931
1932 /* Find out desired geometry of the image instance. If there is no
1933    special function then just return the width and / or height. */
1934 void
1935 image_instance_query_geometry (Lisp_Object image_instance,
1936                                int* width, int* height,
1937                                enum image_instance_geometry disp,
1938                                Lisp_Object domain)
1939 {
1940   Lisp_Image_Instance* ii = XIMAGE_INSTANCE (image_instance);
1941   Lisp_Object type;
1942   struct image_instantiator_methods* meths;
1943   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1944
1945   type = encode_image_instance_type (IMAGE_INSTANCE_TYPE (ii));
1946   meths = decode_device_ii_format (Qnil, type, ERROR_ME_NOT);
1947
1948   if (meths && HAS_IIFORMAT_METH_P (meths, query_geometry))
1949     {
1950       IIFORMAT_METH (meths, query_geometry, (image_instance, width, height,
1951                                              disp, domain));
1952     }
1953   else
1954     {
1955       if (width)
1956         *width = IMAGE_INSTANCE_WIDTH (ii);
1957       if (height)
1958         *height = IMAGE_INSTANCE_HEIGHT (ii);
1959     }
1960 }
1961
1962 /* Layout the image instance using the provided dimensions. Layout
1963    widgets are going to do different kinds of calculations to
1964    determine what size to give things so we could make the layout
1965    function relatively simple to take account of that. An alternative
1966    approach is to consider separately the two cases, one where you
1967    don't mind what size you have (normal widgets) and one where you
1968    want to specify something (layout widgets). */
1969 void
1970 image_instance_layout (Lisp_Object image_instance,
1971                        int width, int height,
1972                        int xoffset, int yoffset,
1973                        Lisp_Object domain)
1974 {
1975   Lisp_Image_Instance* ii = XIMAGE_INSTANCE (image_instance);
1976   Lisp_Object type;
1977   struct image_instantiator_methods* meths;
1978
1979   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
1980
1981   /* Nothing is as nothing does. */
1982   if (NOTHING_IMAGE_INSTANCEP (image_instance))
1983     return;
1984
1985   /* We don't want carefully calculated offsets to be mucked up by
1986      random layouts. */
1987   if (xoffset != IMAGE_UNCHANGED_GEOMETRY)
1988     XIMAGE_INSTANCE_XOFFSET (image_instance) = xoffset;
1989   if (yoffset != IMAGE_UNCHANGED_GEOMETRY)
1990     XIMAGE_INSTANCE_YOFFSET (image_instance) = yoffset;
1991
1992   assert (XIMAGE_INSTANCE_YOFFSET (image_instance) >= 0
1993           && XIMAGE_INSTANCE_XOFFSET (image_instance) >= 0);
1994
1995   /* If geometry is unspecified then get some reasonable values for it. */
1996   if (width == IMAGE_UNSPECIFIED_GEOMETRY
1997       ||
1998       height == IMAGE_UNSPECIFIED_GEOMETRY)
1999     {
2000       int dwidth = IMAGE_UNSPECIFIED_GEOMETRY;
2001       int dheight = IMAGE_UNSPECIFIED_GEOMETRY;
2002       /* Get the desired geometry. */
2003       image_instance_query_geometry (image_instance,
2004                                      &dwidth, &dheight,
2005                                      IMAGE_DESIRED_GEOMETRY,
2006                                      domain);
2007       /* Compare with allowed geometry. */
2008       if (width == IMAGE_UNSPECIFIED_GEOMETRY)
2009         width = dwidth;
2010       if (height == IMAGE_UNSPECIFIED_GEOMETRY)
2011         height = dheight;
2012     }
2013
2014   /* If we don't have sane values then we cannot layout at this point and
2015      must just return. */
2016   if (width == IMAGE_UNSPECIFIED_GEOMETRY
2017       ||
2018       height == IMAGE_UNSPECIFIED_GEOMETRY)
2019       return;
2020
2021   /* At this point width and height should contain sane values. Thus
2022      we set the glyph geometry and lay it out. */
2023   if (IMAGE_INSTANCE_WIDTH (ii) != width
2024       ||
2025       IMAGE_INSTANCE_HEIGHT (ii) != height)
2026     {
2027       IMAGE_INSTANCE_SIZE_CHANGED (ii) = 1;
2028     }
2029
2030   IMAGE_INSTANCE_WIDTH (ii) = width;
2031   IMAGE_INSTANCE_HEIGHT (ii) = height;
2032
2033   type = encode_image_instance_type (IMAGE_INSTANCE_TYPE (ii));
2034   meths = decode_device_ii_format (Qnil, type, ERROR_ME_NOT);
2035
2036   MAYBE_IIFORMAT_METH (meths, layout,
2037                        (image_instance, width, height, xoffset, yoffset,
2038                         domain));
2039   /* Do not clear the dirty flag here - redisplay will do this for
2040      us at the end. */
2041   IMAGE_INSTANCE_LAYOUT_CHANGED (ii) = 0;
2042 }
2043
2044 /* Update an image instance from its changed instantiator. */
2045 static void
2046 update_image_instance (Lisp_Object image_instance,
2047                        Lisp_Object instantiator)
2048 {
2049   struct image_instantiator_methods* meths;
2050   Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
2051
2052   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
2053
2054   if (NOTHING_IMAGE_INSTANCEP (image_instance))
2055     return;
2056
2057   assert (!internal_equal (IMAGE_INSTANCE_INSTANTIATOR (ii), instantiator, 0)
2058           || (internal_equal (IMAGE_INSTANCE_INSTANTIATOR (ii), instantiator, 0)
2059            && internal_equal (IMAGE_INSTANCE_INSTANTIATOR (ii), instantiator, -10)));
2060
2061   /* If the instantiator is identical then do nothing. We must use
2062      equal here because the specifier code copies the instantiator. */
2063   if (!internal_equal (IMAGE_INSTANCE_INSTANTIATOR (ii), instantiator, 0))
2064     {
2065       /* Extract the changed properties so that device / format
2066          methods only have to cope with these. We assume that
2067          normalization has already been done. */
2068       Lisp_Object diffs = find_instantiator_differences
2069         (instantiator,
2070          IMAGE_INSTANCE_INSTANTIATOR (ii));
2071       Lisp_Object type = encode_image_instance_type
2072         (IMAGE_INSTANCE_TYPE (ii));
2073       struct gcpro gcpro1;
2074       GCPRO1 (diffs);
2075
2076       /* try device specific methods first ... */
2077       meths = decode_device_ii_format (image_instance_device (image_instance),
2078                                        type, ERROR_ME_NOT);
2079       MAYBE_IIFORMAT_METH (meths, update, (image_instance, diffs));
2080       /* ... then format specific methods ... */
2081       meths = decode_device_ii_format (Qnil, type, ERROR_ME_NOT);
2082       MAYBE_IIFORMAT_METH (meths, update, (image_instance, diffs));
2083
2084       /* Instance and therefore glyph has changed so mark as dirty.
2085          If we don't do this output optimizations will assume the
2086          glyph is unchanged. */
2087       set_image_instance_dirty_p (image_instance, 1);
2088       /* Structure has changed. */
2089       IMAGE_INSTANCE_LAYOUT_CHANGED (ii) = 1;
2090
2091       UNGCPRO;
2092     }
2093   /* We should now have a consistent instantiator so keep a record of
2094      it. It is important that we don't actually update the window
2095      system widgets here - we must do that when redisplay tells us
2096      to.
2097
2098      #### should we delay doing this until the display is up-to-date
2099      also? */
2100   IMAGE_INSTANCE_INSTANTIATOR (ii) = instantiator;
2101 }
2102
2103 /*
2104  * Mark image instance in W as dirty if (a) W's faces have changed and
2105  * (b) GLYPH_OR_II instance in W is a string.
2106  *
2107  * Return non-zero if instance has been marked dirty.
2108  */
2109 int
2110 invalidate_glyph_geometry_maybe (Lisp_Object glyph_or_ii, struct window* w)
2111 {
2112   if (XFRAME(WINDOW_FRAME(w))->faces_changed)
2113     {
2114       Lisp_Object image = glyph_or_ii;
2115
2116       if (GLYPHP (glyph_or_ii))
2117         {
2118           Lisp_Object window;
2119           XSETWINDOW (window, w);
2120           image = glyph_image_instance (glyph_or_ii, window, ERROR_ME_NOT, 1);
2121         }
2122
2123       if (TEXT_IMAGE_INSTANCEP (image))
2124         {
2125           Lisp_Image_Instance* ii = XIMAGE_INSTANCE (image);
2126           IMAGE_INSTANCE_DIRTYP (ii) = 1;
2127           IMAGE_INSTANCE_LAYOUT_CHANGED (ii) = 1;
2128           if (GLYPHP (glyph_or_ii))
2129             XGLYPH_DIRTYP (glyph_or_ii) = 1;
2130           return 1;
2131         }
2132     }
2133
2134   return 0;
2135 }
2136
2137 \f
2138 /************************************************************************/
2139 /*                              error helpers                           */
2140 /************************************************************************/
2141 DOESNT_RETURN
2142 signal_image_error (const char *reason, Lisp_Object frob)
2143 {
2144   signal_error (Qimage_conversion_error,
2145                 list2 (build_translated_string (reason), frob));
2146 }
2147
2148 DOESNT_RETURN
2149 signal_image_error_2 (const char *reason, Lisp_Object frob0, Lisp_Object frob1)
2150 {
2151   signal_error (Qimage_conversion_error,
2152                 list3 (build_translated_string (reason), frob0, frob1));
2153 }
2154
2155 /****************************************************************************
2156  *                                  nothing                                 *
2157  ****************************************************************************/
2158
2159 static int
2160 nothing_possible_dest_types (void)
2161 {
2162   return IMAGE_NOTHING_MASK;
2163 }
2164
2165 static void
2166 nothing_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
2167                      Lisp_Object pointer_fg, Lisp_Object pointer_bg,
2168                      int dest_mask, Lisp_Object domain)
2169 {
2170   Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
2171
2172   if (dest_mask & IMAGE_NOTHING_MASK)
2173     {
2174       IMAGE_INSTANCE_TYPE (ii) = IMAGE_NOTHING;
2175       IMAGE_INSTANCE_HEIGHT (ii) = 0;
2176       IMAGE_INSTANCE_WIDTH (ii) = 0;
2177     }
2178   else
2179     incompatible_image_types (instantiator, dest_mask, IMAGE_NOTHING_MASK);
2180 }
2181
2182 \f
2183 /****************************************************************************
2184  *                                  inherit                                 *
2185  ****************************************************************************/
2186
2187 static void
2188 inherit_validate (Lisp_Object instantiator)
2189 {
2190   face_must_be_present (instantiator);
2191 }
2192
2193 static Lisp_Object
2194 inherit_normalize (Lisp_Object inst, Lisp_Object console_type,
2195                    Lisp_Object dest_mask)
2196 {
2197   Lisp_Object face;
2198
2199   assert (XVECTOR_LENGTH (inst) == 3);
2200   face = XVECTOR_DATA (inst)[2];
2201   if (!FACEP (face))
2202     inst = vector3 (Qinherit, Q_face, Fget_face (face));
2203   return inst;
2204 }
2205
2206 static int
2207 inherit_possible_dest_types (void)
2208 {
2209   return IMAGE_MONO_PIXMAP_MASK;
2210 }
2211
2212 static void
2213 inherit_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
2214                      Lisp_Object pointer_fg, Lisp_Object pointer_bg,
2215                      int dest_mask, Lisp_Object domain)
2216 {
2217   /* handled specially in image_instantiate */
2218   abort ();
2219 }
2220
2221 \f
2222 /****************************************************************************
2223  *                                  string                                  *
2224  ****************************************************************************/
2225
2226 static void
2227 string_validate (Lisp_Object instantiator)
2228 {
2229   data_must_be_present (instantiator);
2230 }
2231
2232 static int
2233 string_possible_dest_types (void)
2234 {
2235   return IMAGE_TEXT_MASK;
2236 }
2237
2238 /* Called from autodetect_instantiate() */
2239 void
2240 string_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
2241                     Lisp_Object pointer_fg, Lisp_Object pointer_bg,
2242                     int dest_mask, Lisp_Object domain)
2243 {
2244   Lisp_Object string = find_keyword_in_vector (instantiator, Q_data);
2245   Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
2246
2247   /* Should never get here with a domain other than a window. */
2248   assert (!NILP (string) && WINDOWP (DOMAIN_WINDOW (domain)));
2249   if (dest_mask & IMAGE_TEXT_MASK)
2250     {
2251       IMAGE_INSTANCE_TYPE (ii) = IMAGE_TEXT;
2252       IMAGE_INSTANCE_TEXT_STRING (ii) = string;
2253     }
2254   else
2255     incompatible_image_types (instantiator, dest_mask, IMAGE_TEXT_MASK);
2256 }
2257
2258 /* Sort out the size of the text that is being displayed. Calculating
2259    it dynamically allows us to change the text and still see
2260    everything. Note that the following methods are for text not string
2261    since that is what the instantiated type is. The first method is a
2262    helper that is used elsewhere for calculating text geometry. */
2263 void
2264 query_string_geometry (Lisp_Object string, Lisp_Object face,
2265                        int* width, int* height, int* descent, Lisp_Object domain)
2266 {
2267   struct font_metric_info fm;
2268   unsigned char charsets[NUM_LEADING_BYTES];
2269   struct face_cachel frame_cachel;
2270   struct face_cachel *cachel;
2271   Lisp_Object frame = DOMAIN_FRAME (domain);
2272
2273   /* Compute height */
2274   if (height)
2275     {
2276       /* Compute string metric info */
2277       find_charsets_in_bufbyte_string (charsets,
2278                                        XSTRING_DATA   (string),
2279                                        XSTRING_LENGTH (string));
2280
2281       /* Fallback to the default face if none was provided. */
2282       if (!NILP (face))
2283         {
2284           reset_face_cachel (&frame_cachel);
2285           update_face_cachel_data (&frame_cachel, frame, face);
2286           cachel = &frame_cachel;
2287         }
2288       else
2289         {
2290           cachel = WINDOW_FACE_CACHEL (DOMAIN_XWINDOW (domain),
2291                                        DEFAULT_INDEX);
2292         }
2293
2294       ensure_face_cachel_complete (cachel, domain, charsets);
2295       face_cachel_charset_font_metric_info (cachel, charsets, &fm);
2296
2297       *height = fm.ascent + fm.descent;
2298       /* #### descent only gets set if we query the height as well. */
2299       if (descent)
2300         *descent = fm.descent;
2301     }
2302
2303   /* Compute width */
2304   if (width)
2305     {
2306       if (!NILP (face))
2307         *width = redisplay_frame_text_width_string (XFRAME (frame),
2308                                                     face,
2309                                                     0, string, 0, -1);
2310       else
2311         *width = redisplay_frame_text_width_string (XFRAME (frame),
2312                                                     Vdefault_face,
2313                                                     0, string, 0, -1);
2314     }
2315 }
2316
2317 Lisp_Object
2318 query_string_font (Lisp_Object string, Lisp_Object face, Lisp_Object domain)
2319 {
2320   unsigned char charsets[NUM_LEADING_BYTES];
2321   struct face_cachel frame_cachel;
2322   struct face_cachel *cachel;
2323   int i;
2324   Lisp_Object frame = DOMAIN_FRAME (domain);
2325
2326   /* Compute string font info */
2327   find_charsets_in_bufbyte_string (charsets,
2328                                    XSTRING_DATA   (string),
2329                                    XSTRING_LENGTH (string));
2330
2331   reset_face_cachel (&frame_cachel);
2332   update_face_cachel_data (&frame_cachel, frame, face);
2333   cachel = &frame_cachel;
2334
2335   ensure_face_cachel_complete (cachel, domain, charsets);
2336
2337   for (i = 0; i < NUM_LEADING_BYTES; i++)
2338     {
2339       if (charsets[i])
2340         {
2341           return FACE_CACHEL_FONT (cachel,
2342                                    CHARSET_BY_LEADING_BYTE (i +
2343                                                             MIN_LEADING_BYTE));
2344
2345         }
2346     }
2347
2348   return Qnil;                  /* NOT REACHED */
2349 }
2350
2351 static void
2352 text_query_geometry (Lisp_Object image_instance,
2353                      int* width, int* height,
2354                      enum image_instance_geometry disp, Lisp_Object domain)
2355 {
2356   Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
2357   int descent = 0;
2358
2359   query_string_geometry (IMAGE_INSTANCE_TEXT_STRING (ii),
2360                          IMAGE_INSTANCE_FACE (ii),
2361                          width, height, &descent, domain);
2362
2363   /* The descent gets set as a side effect of querying the
2364      geometry. */
2365   IMAGE_INSTANCE_TEXT_DESCENT (ii) = descent;
2366 }
2367
2368 /* set the properties of a string */
2369 static void
2370 text_update (Lisp_Object image_instance, Lisp_Object instantiator)
2371 {
2372   Lisp_Object val = find_keyword_in_vector (instantiator, Q_data);
2373
2374   if (!NILP (val))
2375     {
2376       CHECK_STRING (val);
2377       XIMAGE_INSTANCE_TEXT_STRING (image_instance) = val;
2378     }
2379 }
2380
2381 \f
2382 /****************************************************************************
2383  *                             formatted-string                             *
2384  ****************************************************************************/
2385
2386 static void
2387 formatted_string_validate (Lisp_Object instantiator)
2388 {
2389   data_must_be_present (instantiator);
2390 }
2391
2392 static int
2393 formatted_string_possible_dest_types (void)
2394 {
2395   return IMAGE_TEXT_MASK;
2396 }
2397
2398 static void
2399 formatted_string_instantiate (Lisp_Object image_instance,
2400                               Lisp_Object instantiator,
2401                               Lisp_Object pointer_fg, Lisp_Object pointer_bg,
2402                               int dest_mask, Lisp_Object domain)
2403 {
2404   /* #### implement this */
2405   warn_when_safe (Qunimplemented, Qnotice,
2406                   "`formatted-string' not yet implemented; assuming `string'");
2407
2408   string_instantiate (image_instance, instantiator,
2409                       pointer_fg, pointer_bg, dest_mask, domain);
2410 }
2411
2412 \f
2413 /************************************************************************/
2414 /*                        pixmap file functions                         */
2415 /************************************************************************/
2416
2417 /* If INSTANTIATOR refers to inline data, return Qnil.
2418    If INSTANTIATOR refers to data in a file, return the full filename
2419    if it exists; otherwise, return a cons of (filename).
2420
2421    FILE_KEYWORD and DATA_KEYWORD are symbols specifying the
2422    keywords used to look up the file and inline data,
2423    respectively, in the instantiator.  Normally these would
2424    be Q_file and Q_data, but might be different for mask data. */
2425
2426 Lisp_Object
2427 potential_pixmap_file_instantiator (Lisp_Object instantiator,
2428                                     Lisp_Object file_keyword,
2429                                     Lisp_Object data_keyword,
2430                                     Lisp_Object console_type)
2431 {
2432   Lisp_Object file;
2433   Lisp_Object data;
2434
2435   assert (VECTORP (instantiator));
2436
2437   data = find_keyword_in_vector (instantiator, data_keyword);
2438   file = find_keyword_in_vector (instantiator, file_keyword);
2439
2440   if (!NILP (file) && NILP (data))
2441     {
2442       Lisp_Object retval = MAYBE_LISP_CONTYPE_METH
2443         (decode_console_type(console_type, ERROR_ME),
2444          locate_pixmap_file, (file));
2445
2446       if (!NILP (retval))
2447         return retval;
2448       else
2449         return Fcons (file, Qnil); /* should have been file */
2450     }
2451
2452   return Qnil;
2453 }
2454
2455 Lisp_Object
2456 simple_image_type_normalize (Lisp_Object inst, Lisp_Object console_type,
2457                              Lisp_Object image_type_tag)
2458 {
2459   /* This function can call lisp */
2460   Lisp_Object file = Qnil;
2461   struct gcpro gcpro1, gcpro2;
2462   Lisp_Object alist = Qnil;
2463
2464   GCPRO2 (file, alist);
2465
2466   /* Now, convert any file data into inline data.  At the end of this,
2467      `data' will contain the inline data (if any) or Qnil, and `file'
2468      will contain the name this data was derived from (if known) or
2469      Qnil.
2470
2471      Note that if we cannot generate any regular inline data, we
2472      skip out. */
2473
2474   file = potential_pixmap_file_instantiator (inst, Q_file, Q_data,
2475                                              console_type);
2476
2477   if (CONSP (file)) /* failure locating filename */
2478     signal_double_file_error ("Opening pixmap file",
2479                               "no such file or directory",
2480                               Fcar (file));
2481
2482   if (NILP (file)) /* no conversion necessary */
2483     RETURN_UNGCPRO (inst);
2484
2485   alist = tagged_vector_to_alist (inst);
2486
2487   {
2488     Lisp_Object data = make_string_from_file (file);
2489     alist = remassq_no_quit (Q_file, alist);
2490     /* there can't be a :data at this point. */
2491     alist = Fcons (Fcons (Q_file, file),
2492                    Fcons (Fcons (Q_data, data), alist));
2493   }
2494
2495   {
2496     Lisp_Object result = alist_to_tagged_vector (image_type_tag, alist);
2497     free_alist (alist);
2498     RETURN_UNGCPRO (result);
2499   }
2500 }
2501
2502 \f
2503 #ifdef HAVE_WINDOW_SYSTEM
2504 /**********************************************************************
2505  *                             XBM                                    *
2506  **********************************************************************/
2507
2508 /* Check if DATA represents a valid inline XBM spec (i.e. a list
2509    of (width height bits), with checking done on the dimensions).
2510    If not, signal an error. */
2511
2512 static void
2513 check_valid_xbm_inline (Lisp_Object data)
2514 {
2515   Lisp_Object width, height, bits;
2516
2517   if (!CONSP (data) ||
2518       !CONSP (XCDR (data)) ||
2519       !CONSP (XCDR (XCDR (data))) ||
2520       !NILP (XCDR (XCDR (XCDR (data)))))
2521     signal_simple_error ("Must be list of 3 elements", data);
2522
2523   width  = XCAR (data);
2524   height = XCAR (XCDR (data));
2525   bits   = XCAR (XCDR (XCDR (data)));
2526
2527   CHECK_STRING (bits);
2528
2529   if (!NATNUMP (width))
2530     signal_simple_error ("Width must be a natural number", width);
2531
2532   if (!NATNUMP (height))
2533     signal_simple_error ("Height must be a natural number", height);
2534
2535   if (((XINT (width) * XINT (height)) / 8) > XSTRING_CHAR_LENGTH (bits))
2536     signal_simple_error ("data is too short for width and height",
2537                          vector3 (width, height, bits));
2538 }
2539
2540 /* Validate method for XBM's. */
2541
2542 static void
2543 xbm_validate (Lisp_Object instantiator)
2544 {
2545   file_or_data_must_be_present (instantiator);
2546 }
2547
2548 /* Given a filename that is supposed to contain XBM data, return
2549    the inline representation of it as (width height bits).  Return
2550    the hotspot through XHOT and YHOT, if those pointers are not 0.
2551    If there is no hotspot, XHOT and YHOT will contain -1.
2552
2553    If the function fails:
2554
2555    -- if OK_IF_DATA_INVALID is set and the data was invalid,
2556       return Qt.
2557    -- maybe return an error, or return Qnil.
2558  */
2559
2560 #ifdef HAVE_X_WINDOWS
2561 #include <X11/Xlib.h>
2562 #else
2563 #define XFree(data) free(data)
2564 #endif
2565
2566 Lisp_Object
2567 bitmap_to_lisp_data (Lisp_Object name, int *xhot, int *yhot,
2568                      int ok_if_data_invalid)
2569 {
2570   unsigned int w, h;
2571   Extbyte *data;
2572   int result;
2573   const char *filename_ext;
2574
2575   LISP_STRING_TO_EXTERNAL (name, filename_ext, Qfile_name);
2576   result = read_bitmap_data_from_file (filename_ext, &w, &h,
2577                                        (unsigned char **) &data, xhot, yhot);
2578
2579   if (result == BitmapSuccess)
2580     {
2581       Lisp_Object retval;
2582       int len = (w + 7) / 8 * h;
2583
2584       retval = list3 (make_int (w), make_int (h),
2585                       make_ext_string (data, len, Qbinary));
2586       XFree (data);
2587       return retval;
2588     }
2589
2590   switch (result)
2591     {
2592     case BitmapOpenFailed:
2593       {
2594         /* should never happen */
2595         signal_double_file_error ("Opening bitmap file",
2596                                   "no such file or directory",
2597                                   name);
2598       }
2599     case BitmapFileInvalid:
2600       {
2601         if (ok_if_data_invalid)
2602           return Qt;
2603         signal_double_file_error ("Reading bitmap file",
2604                                   "invalid data in file",
2605                                   name);
2606       }
2607     case BitmapNoMemory:
2608       {
2609         signal_double_file_error ("Reading bitmap file",
2610                                   "out of memory",
2611                                   name);
2612       }
2613     default:
2614       {
2615         signal_double_file_error_2 ("Reading bitmap file",
2616                                     "unknown error code",
2617                                     make_int (result), name);
2618       }
2619     }
2620
2621   return Qnil; /* not reached */
2622 }
2623
2624 Lisp_Object
2625 xbm_mask_file_munging (Lisp_Object alist, Lisp_Object file,
2626                        Lisp_Object mask_file, Lisp_Object console_type)
2627 {
2628   /* This is unclean but it's fairly standard -- a number of the
2629      bitmaps in /usr/include/X11/bitmaps use it -- so we support
2630      it. */
2631   if (NILP (mask_file)
2632       /* don't override explicitly specified mask data. */
2633       && NILP (assq_no_quit (Q_mask_data, alist))
2634       && !NILP (file))
2635     {
2636       mask_file = MAYBE_LISP_CONTYPE_METH
2637         (decode_console_type(console_type, ERROR_ME),
2638          locate_pixmap_file, (concat2 (file, build_string ("Mask"))));
2639       if (NILP (mask_file))
2640         mask_file = MAYBE_LISP_CONTYPE_METH
2641           (decode_console_type(console_type, ERROR_ME),
2642            locate_pixmap_file, (concat2 (file, build_string ("msk"))));
2643     }
2644
2645   if (!NILP (mask_file))
2646     {
2647       Lisp_Object mask_data =
2648         bitmap_to_lisp_data (mask_file, 0, 0, 0);
2649       alist = remassq_no_quit (Q_mask_file, alist);
2650       /* there can't be a :mask-data at this point. */
2651       alist = Fcons (Fcons (Q_mask_file, mask_file),
2652                      Fcons (Fcons (Q_mask_data, mask_data), alist));
2653     }
2654
2655   return alist;
2656 }
2657
2658 /* Normalize method for XBM's. */
2659
2660 static Lisp_Object
2661 xbm_normalize (Lisp_Object inst, Lisp_Object console_type,
2662                Lisp_Object dest_mask)
2663 {
2664   Lisp_Object file = Qnil, mask_file = Qnil;
2665   struct gcpro gcpro1, gcpro2, gcpro3;
2666   Lisp_Object alist = Qnil;
2667
2668   GCPRO3 (file, mask_file, alist);
2669
2670   /* Now, convert any file data into inline data for both the regular
2671      data and the mask data.  At the end of this, `data' will contain
2672      the inline data (if any) or Qnil, and `file' will contain
2673      the name this data was derived from (if known) or Qnil.
2674      Likewise for `mask_file' and `mask_data'.
2675
2676      Note that if we cannot generate any regular inline data, we
2677      skip out. */
2678
2679   file = potential_pixmap_file_instantiator (inst, Q_file, Q_data,
2680                                              console_type);
2681   mask_file = potential_pixmap_file_instantiator (inst, Q_mask_file,
2682                                                   Q_mask_data, console_type);
2683
2684   if (CONSP (file)) /* failure locating filename */
2685     signal_double_file_error ("Opening bitmap file",
2686                               "no such file or directory",
2687                               Fcar (file));
2688
2689   if (NILP (file) && NILP (mask_file)) /* no conversion necessary */
2690     RETURN_UNGCPRO (inst);
2691
2692   alist = tagged_vector_to_alist (inst);
2693
2694   if (!NILP (file))
2695     {
2696       int xhot, yhot;
2697       Lisp_Object data = bitmap_to_lisp_data (file, &xhot, &yhot, 0);
2698       alist = remassq_no_quit (Q_file, alist);
2699       /* there can't be a :data at this point. */
2700       alist = Fcons (Fcons (Q_file, file),
2701                      Fcons (Fcons (Q_data, data), alist));
2702
2703       if (xhot != -1 && NILP (assq_no_quit (Q_hotspot_x, alist)))
2704         alist = Fcons (Fcons (Q_hotspot_x, make_int (xhot)),
2705                        alist);
2706       if (yhot != -1 && NILP (assq_no_quit (Q_hotspot_y, alist)))
2707         alist = Fcons (Fcons (Q_hotspot_y, make_int (yhot)),
2708                        alist);
2709     }
2710
2711   alist = xbm_mask_file_munging (alist, file, mask_file, console_type);
2712
2713   {
2714     Lisp_Object result = alist_to_tagged_vector (Qxbm, alist);
2715     free_alist (alist);
2716     RETURN_UNGCPRO (result);
2717   }
2718 }
2719
2720 \f
2721 static int
2722 xbm_possible_dest_types (void)
2723 {
2724   return
2725     IMAGE_MONO_PIXMAP_MASK  |
2726     IMAGE_COLOR_PIXMAP_MASK |
2727     IMAGE_POINTER_MASK;
2728 }
2729
2730 #endif
2731
2732 \f
2733 #ifdef HAVE_XFACE
2734 /**********************************************************************
2735  *                             X-Face                                 *
2736  **********************************************************************/
2737
2738 static void
2739 xface_validate (Lisp_Object instantiator)
2740 {
2741   file_or_data_must_be_present (instantiator);
2742 }
2743
2744 static Lisp_Object
2745 xface_normalize (Lisp_Object inst, Lisp_Object console_type,
2746                  Lisp_Object dest_mask)
2747 {
2748   /* This function can call lisp */
2749   Lisp_Object file = Qnil, mask_file = Qnil;
2750   struct gcpro gcpro1, gcpro2, gcpro3;
2751   Lisp_Object alist = Qnil;
2752
2753   GCPRO3 (file, mask_file, alist);
2754
2755   /* Now, convert any file data into inline data for both the regular
2756      data and the mask data.  At the end of this, `data' will contain
2757      the inline data (if any) or Qnil, and `file' will contain
2758      the name this data was derived from (if known) or Qnil.
2759      Likewise for `mask_file' and `mask_data'.
2760
2761      Note that if we cannot generate any regular inline data, we
2762      skip out. */
2763
2764   file = potential_pixmap_file_instantiator (inst, Q_file, Q_data,
2765                                              console_type);
2766   mask_file = potential_pixmap_file_instantiator (inst, Q_mask_file,
2767                                                   Q_mask_data, console_type);
2768
2769   if (CONSP (file)) /* failure locating filename */
2770     signal_double_file_error ("Opening bitmap file",
2771                               "no such file or directory",
2772                               Fcar (file));
2773
2774   if (NILP (file) && NILP (mask_file)) /* no conversion necessary */
2775     RETURN_UNGCPRO (inst);
2776
2777   alist = tagged_vector_to_alist (inst);
2778
2779   {
2780     Lisp_Object data = make_string_from_file (file);
2781     alist = remassq_no_quit (Q_file, alist);
2782     /* there can't be a :data at this point. */
2783     alist = Fcons (Fcons (Q_file, file),
2784                    Fcons (Fcons (Q_data, data), alist));
2785   }
2786
2787   alist = xbm_mask_file_munging (alist, file, mask_file, console_type);
2788
2789   {
2790     Lisp_Object result = alist_to_tagged_vector (Qxface, alist);
2791     free_alist (alist);
2792     RETURN_UNGCPRO (result);
2793   }
2794 }
2795
2796 static int
2797 xface_possible_dest_types (void)
2798 {
2799   return
2800     IMAGE_MONO_PIXMAP_MASK  |
2801     IMAGE_COLOR_PIXMAP_MASK |
2802     IMAGE_POINTER_MASK;
2803 }
2804
2805 #endif /* HAVE_XFACE */
2806
2807 \f
2808 #ifdef HAVE_XPM
2809
2810 /**********************************************************************
2811  *                             XPM                                    *
2812  **********************************************************************/
2813
2814 #ifdef HAVE_GTK
2815 /* Gtk has to be gratuitously different, eh? */
2816 Lisp_Object
2817 pixmap_to_lisp_data (Lisp_Object name, int ok_if_data_invalid)
2818 {
2819   return (make_string_from_file (name));
2820 }
2821 #else
2822 Lisp_Object
2823 pixmap_to_lisp_data (Lisp_Object name, int ok_if_data_invalid)
2824 {
2825   char **data;
2826   int result;
2827   char *fname = 0;
2828
2829   LISP_STRING_TO_EXTERNAL (name, fname, Qfile_name);
2830   result = XpmReadFileToData (fname, &data);
2831
2832   if (result == XpmSuccess)
2833     {
2834       Lisp_Object retval = Qnil;
2835       struct buffer *old_buffer = current_buffer;
2836       Lisp_Object temp_buffer =
2837         Fget_buffer_create (build_string (" *pixmap conversion*"));
2838       int elt;
2839       int height, width, ncolors;
2840       struct gcpro gcpro1, gcpro2, gcpro3;
2841       int speccount = specpdl_depth ();
2842
2843       GCPRO3 (name, retval, temp_buffer);
2844
2845       specbind (Qinhibit_quit, Qt);
2846       set_buffer_internal (XBUFFER (temp_buffer));
2847       Ferase_buffer (Qnil);
2848
2849       buffer_insert_c_string (current_buffer, "/* XPM */\r");
2850       buffer_insert_c_string (current_buffer, "static char *pixmap[] = {\r");
2851
2852       sscanf (data[0], "%d %d %d", &height, &width, &ncolors);
2853       for (elt = 0; elt <= width + ncolors; elt++)
2854         {
2855           buffer_insert_c_string (current_buffer, "\"");
2856           buffer_insert_c_string (current_buffer, data[elt]);
2857
2858           if (elt < width + ncolors)
2859             buffer_insert_c_string (current_buffer, "\",\r");
2860           else
2861             buffer_insert_c_string (current_buffer, "\"};\r");
2862         }
2863
2864       retval = Fbuffer_substring (Qnil, Qnil, Qnil);
2865       XpmFree (data);
2866
2867       set_buffer_internal (old_buffer);
2868       unbind_to (speccount, Qnil);
2869
2870       RETURN_UNGCPRO (retval);
2871     }
2872
2873   switch (result)
2874     {
2875     case XpmFileInvalid:
2876       {
2877         if (ok_if_data_invalid)
2878           return Qt;
2879         signal_image_error ("invalid XPM data in file", name);
2880       }
2881     case XpmNoMemory:
2882       {
2883         signal_double_file_error ("Reading pixmap file",
2884                                   "out of memory", name);
2885       }
2886     case XpmOpenFailed:
2887       {
2888         /* should never happen? */
2889         signal_double_file_error ("Opening pixmap file",
2890                                   "no such file or directory", name);
2891       }
2892     default:
2893       {
2894         signal_double_file_error_2 ("Parsing pixmap file",
2895                                     "unknown error code",
2896                                     make_int (result), name);
2897         break;
2898       }
2899     }
2900
2901   return Qnil; /* not reached */
2902 }
2903 #endif /* !HAVE_GTK */
2904
2905 static void
2906 check_valid_xpm_color_symbols (Lisp_Object data)
2907 {
2908   Lisp_Object rest;
2909
2910   for (rest = data; !NILP (rest); rest = XCDR (rest))
2911     {
2912       if (!CONSP (rest) ||
2913           !CONSP (XCAR (rest)) ||
2914           !STRINGP (XCAR (XCAR (rest))) ||
2915           (!STRINGP (XCDR (XCAR (rest))) &&
2916            !COLOR_SPECIFIERP (XCDR (XCAR (rest)))))
2917         signal_simple_error ("Invalid color symbol alist", data);
2918     }
2919 }
2920
2921 static void
2922 xpm_validate (Lisp_Object instantiator)
2923 {
2924   file_or_data_must_be_present (instantiator);
2925 }
2926
2927 Lisp_Object Vxpm_color_symbols;
2928
2929 Lisp_Object
2930 evaluate_xpm_color_symbols (void)
2931 {
2932   Lisp_Object rest, results = Qnil;
2933   struct gcpro gcpro1, gcpro2;
2934
2935   GCPRO2 (rest, results);
2936   for (rest = Vxpm_color_symbols; !NILP (rest); rest = XCDR (rest))
2937     {
2938       Lisp_Object name, value, cons;
2939
2940       CHECK_CONS (rest);
2941       cons = XCAR (rest);
2942       CHECK_CONS (cons);
2943       name = XCAR (cons);
2944       CHECK_STRING (name);
2945       value = XCDR (cons);
2946       CHECK_CONS (value);
2947       value = XCAR (value);
2948       value = Feval (value);
2949       if (NILP (value))
2950         continue;
2951       if (!STRINGP (value) && !COLOR_SPECIFIERP (value))
2952         signal_simple_error
2953           ("Result from xpm-color-symbols eval must be nil, string, or color",
2954            value);
2955       results = Fcons (Fcons (name, value), results);
2956     }
2957   UNGCPRO;                      /* no more evaluation */
2958   return results;
2959 }
2960
2961 static Lisp_Object
2962 xpm_normalize (Lisp_Object inst, Lisp_Object console_type,
2963                Lisp_Object dest_mask)
2964 {
2965   Lisp_Object file = Qnil;
2966   Lisp_Object color_symbols;
2967   struct gcpro gcpro1, gcpro2;
2968   Lisp_Object alist = Qnil;
2969
2970   GCPRO2 (file, alist);
2971
2972   /* Now, convert any file data into inline data.  At the end of this,
2973      `data' will contain the inline data (if any) or Qnil, and
2974      `file' will contain the name this data was derived from (if
2975      known) or Qnil.
2976
2977      Note that if we cannot generate any regular inline data, we
2978      skip out. */
2979
2980   file = potential_pixmap_file_instantiator (inst, Q_file, Q_data,
2981                                              console_type);
2982
2983   if (CONSP (file)) /* failure locating filename */
2984     signal_double_file_error ("Opening pixmap file",
2985                               "no such file or directory",
2986                               Fcar (file));
2987
2988   color_symbols = find_keyword_in_vector_or_given (inst, Q_color_symbols,
2989                                                    Qunbound);
2990
2991   if (NILP (file) && !UNBOUNDP (color_symbols))
2992     /* no conversion necessary */
2993     RETURN_UNGCPRO (inst);
2994
2995   alist = tagged_vector_to_alist (inst);
2996
2997   if (!NILP (file))
2998     {
2999       Lisp_Object data = pixmap_to_lisp_data (file, 0);
3000       alist = remassq_no_quit (Q_file, alist);
3001       /* there can't be a :data at this point. */
3002       alist = Fcons (Fcons (Q_file, file),
3003                      Fcons (Fcons (Q_data, data), alist));
3004     }
3005
3006   if (UNBOUNDP (color_symbols))
3007     {
3008       color_symbols = evaluate_xpm_color_symbols ();
3009       alist = Fcons (Fcons (Q_color_symbols, color_symbols),
3010                      alist);
3011     }
3012
3013   {
3014     Lisp_Object result = alist_to_tagged_vector (Qxpm, alist);
3015     free_alist (alist);
3016     RETURN_UNGCPRO (result);
3017   }
3018 }
3019
3020 static int
3021 xpm_possible_dest_types (void)
3022 {
3023   return
3024     IMAGE_MONO_PIXMAP_MASK  |
3025     IMAGE_COLOR_PIXMAP_MASK |
3026     IMAGE_POINTER_MASK;
3027 }
3028
3029 #endif /* HAVE_XPM */
3030
3031 \f
3032 /****************************************************************************
3033  *                         Image Specifier Object                           *
3034  ****************************************************************************/
3035
3036 DEFINE_SPECIFIER_TYPE (image);
3037
3038 static void
3039 image_create (Lisp_Object obj)
3040 {
3041   Lisp_Specifier *image = XIMAGE_SPECIFIER (obj);
3042
3043   IMAGE_SPECIFIER_ALLOWED (image) = ~0; /* all are allowed */
3044   IMAGE_SPECIFIER_ATTACHEE (image) = Qnil;
3045   IMAGE_SPECIFIER_ATTACHEE_PROPERTY (image) = Qnil;
3046 }
3047
3048 static void
3049 image_mark (Lisp_Object obj)
3050 {
3051   Lisp_Specifier *image = XIMAGE_SPECIFIER (obj);
3052
3053   mark_object (IMAGE_SPECIFIER_ATTACHEE (image));
3054   mark_object (IMAGE_SPECIFIER_ATTACHEE_PROPERTY (image));
3055 }
3056
3057 static int
3058 instantiator_eq_equal (Lisp_Object obj1, Lisp_Object obj2)
3059 {
3060   if (EQ (obj1, obj2))
3061     return 1;
3062
3063   else if (CONSP (obj1) && CONSP (obj2))
3064     {
3065       return instantiator_eq_equal (XCAR (obj1), XCAR (obj2))
3066         &&
3067         instantiator_eq_equal (XCDR (obj1), XCDR (obj2));
3068     }
3069   return 0;
3070 }
3071
3072 static hashcode_t
3073 instantiator_eq_hash (Lisp_Object obj)
3074 {
3075   if (CONSP (obj))
3076     {
3077       /* no point in worrying about tail recursion, since we're not
3078          going very deep */
3079       return HASH2 (instantiator_eq_hash (XCAR (obj)),
3080                     instantiator_eq_hash (XCDR (obj)));
3081     }
3082   return LISP_HASH (obj);
3083 }
3084
3085 /* We need a special hash table for storing image instances. */
3086 Lisp_Object
3087 make_image_instance_cache_hash_table (void)
3088 {
3089   return make_general_lisp_hash_table
3090     (instantiator_eq_hash, instantiator_eq_equal,
3091      30, -1.0, -1.0,
3092      HASH_TABLE_KEY_CAR_VALUE_WEAK);
3093 }
3094
3095 static Lisp_Object
3096 image_instantiate_cache_result (Lisp_Object locative)
3097 {
3098   /* locative = (instance instantiator . subtable)
3099
3100      So we are using the instantiator as the key and the instance as
3101      the value. Since the hashtable is key-weak this means that the
3102      image instance will stay around as long as the instantiator stays
3103      around. The instantiator is stored in the `image' slot of the
3104      glyph, so as long as the glyph is marked the instantiator will be
3105      as well and hence the cached image instance also.*/
3106   Fputhash (XCAR (XCDR (locative)), XCAR (locative), XCDR (XCDR (locative)));
3107   free_cons (XCONS (XCDR (locative)));
3108   free_cons (XCONS (locative));
3109   return Qnil;
3110 }
3111
3112 /* Given a specification for an image, return an instance of
3113    the image which matches the given instantiator and which can be
3114    displayed in the given domain. */
3115
3116 static Lisp_Object
3117 image_instantiate (Lisp_Object specifier, Lisp_Object matchspec,
3118                    Lisp_Object domain, Lisp_Object instantiator,
3119                    Lisp_Object depth)
3120 {
3121   Lisp_Object glyph = IMAGE_SPECIFIER_ATTACHEE (XIMAGE_SPECIFIER (specifier));
3122   int dest_mask = XIMAGE_SPECIFIER_ALLOWED (specifier);
3123   int pointerp = dest_mask & image_instance_type_to_mask (IMAGE_POINTER);
3124
3125   if (IMAGE_INSTANCEP (instantiator))
3126     {
3127       /* make sure that the image instance's governing domain and type are
3128          matching. */
3129       Lisp_Object governing_domain = XIMAGE_INSTANCE_DOMAIN (instantiator);
3130
3131       if ((DEVICEP (governing_domain)
3132            && EQ (governing_domain, DOMAIN_DEVICE (domain)))
3133           || (FRAMEP (governing_domain)
3134               && EQ (governing_domain, DOMAIN_FRAME (domain)))
3135           || (WINDOWP (governing_domain)
3136               && EQ (governing_domain, DOMAIN_WINDOW (domain))))
3137         {
3138           int mask =
3139             image_instance_type_to_mask (XIMAGE_INSTANCE_TYPE (instantiator));
3140           if (mask & dest_mask)
3141             return instantiator;
3142           else
3143             signal_simple_error ("Type of image instance not allowed here",
3144                                  instantiator);
3145         }
3146       else
3147         signal_simple_error_2 ("Wrong domain for image instance",
3148                                instantiator, domain);
3149     }
3150   /* How ugly !! An image instanciator that uses a kludgy syntax to snarf in
3151      face properties. There's a design flaw here. -- didier */
3152   else if (VECTORP (instantiator)
3153            && EQ (INSTANTIATOR_TYPE (instantiator), Qinherit))
3154     {
3155       assert (XVECTOR_LENGTH (instantiator) == 3);
3156       return (FACE_PROPERTY_INSTANCE
3157               (Fget_face (XVECTOR_DATA (instantiator)[2]),
3158                Qbackground_pixmap, domain, 1, depth));
3159     }
3160   else
3161     {
3162       Lisp_Object instance = Qnil;
3163       Lisp_Object subtable = Qnil;
3164       /* #### Should this be GCPRO'd? */
3165       Lisp_Object hash_key = Qnil;
3166       Lisp_Object pointer_fg = Qnil;
3167       Lisp_Object pointer_bg = Qnil;
3168       Lisp_Object governing_domain =
3169         get_image_instantiator_governing_domain (instantiator, domain);
3170       struct gcpro gcpro1;
3171
3172       GCPRO1 (instance);
3173
3174       /* We have to put subwindow, widget and text image instances in
3175          a per-window cache so that we can see the same glyph in
3176          different windows. We use governing_domain to determine the type
3177          of image_instance that will be created. */
3178
3179       if (pointerp)
3180         {
3181           pointer_fg = FACE_FOREGROUND (Vpointer_face, domain);
3182           pointer_bg = FACE_BACKGROUND (Vpointer_face, domain);
3183           hash_key = list4 (glyph, INSTANTIATOR_TYPE (instantiator),
3184                             pointer_fg, pointer_bg);
3185         }
3186       else
3187         /* We cannot simply key on the glyph since fallbacks could use
3188            the same glyph but have a totally different instantiator
3189            type. Thus we key on the glyph and the type (but not any
3190            other parts of the instantiator. */
3191         hash_key = list2 (glyph, INSTANTIATOR_TYPE (instantiator));
3192
3193       /* First look in the device cache. */
3194       if (DEVICEP (governing_domain))
3195         {
3196           subtable = Fgethash (make_int (dest_mask),
3197                                XDEVICE (governing_domain)->
3198                                image_instance_cache,
3199                                Qunbound);
3200           if (UNBOUNDP (subtable))
3201             {
3202               /* For the image instance cache, we do comparisons with
3203                  EQ rather than with EQUAL, as we do for color and
3204                  font names.  The reasons are:
3205
3206                  1) pixmap data can be very long, and thus the hashing
3207                  and comparing will take awhile.
3208
3209                  2) It's not so likely that we'll run into things that
3210                  are EQUAL but not EQ (that can happen a lot with
3211                  faces, because their specifiers are copied around);
3212                  but pixmaps tend not to be in faces.
3213
3214                  However, if the image-instance could be a pointer, we
3215                  have to use EQUAL because we massaged the
3216                  instantiator into a cons3 also containing the
3217                  foreground and background of the pointer face.  */
3218               subtable = make_image_instance_cache_hash_table ();
3219
3220               Fputhash (make_int (dest_mask), subtable,
3221                         XDEVICE (governing_domain)->image_instance_cache);
3222               instance = Qunbound;
3223             }
3224           else
3225             {
3226               instance = Fgethash (hash_key, subtable, Qunbound);
3227             }
3228         }
3229       else if (WINDOWP (governing_domain))
3230         {
3231           /* Subwindows have a per-window cache and have to be treated
3232              differently. */
3233           instance =
3234             Fgethash (hash_key,
3235                       XWINDOW (governing_domain)->subwindow_instance_cache,
3236                       Qunbound);
3237         }
3238       else
3239         abort ();       /* We're not allowed anything else currently. */
3240
3241       /* If we don't have an instance at this point then create
3242          one. */
3243       if (UNBOUNDP (instance))
3244         {
3245           Lisp_Object locative =
3246             noseeum_cons (Qnil,
3247                           noseeum_cons (hash_key,
3248                                         DEVICEP (governing_domain) ? subtable
3249                                         : XWINDOW (governing_domain)
3250                                         ->subwindow_instance_cache));
3251           int speccount = specpdl_depth ();
3252
3253           /* Make sure we cache the failures, too.  Use an
3254              unwind-protect to catch such errors.  If we fail, the
3255              unwind-protect records nil in the hash table.  If we
3256              succeed, we change the car of the locative to the
3257              resulting instance, which gets recorded instead. */
3258           record_unwind_protect (image_instantiate_cache_result,
3259                                  locative);
3260           instance =
3261             instantiate_image_instantiator (governing_domain,
3262                                             domain, instantiator,
3263                                             pointer_fg, pointer_bg,
3264                                             dest_mask, glyph);
3265
3266           /* We need a per-frame cache for redisplay. */
3267           cache_subwindow_instance_in_frame_maybe (instance);
3268
3269           Fsetcar (locative, instance);
3270 #ifdef ERROR_CHECK_GLYPHS
3271           if (image_instance_type_to_mask (XIMAGE_INSTANCE_TYPE (instance))
3272               & (IMAGE_SUBWINDOW_MASK | IMAGE_WIDGET_MASK))
3273               assert (EQ (XIMAGE_INSTANCE_FRAME (instance),
3274                           DOMAIN_FRAME (domain)));
3275 #endif
3276           unbind_to (speccount, Qnil);
3277 #ifdef ERROR_CHECK_GLYPHS
3278           if (image_instance_type_to_mask (XIMAGE_INSTANCE_TYPE (instance))
3279               & (IMAGE_SUBWINDOW_MASK | IMAGE_WIDGET_MASK))
3280             assert (EQ (Fgethash (hash_key,
3281                                   XWINDOW (governing_domain)
3282                                   ->subwindow_instance_cache,
3283                                   Qunbound), instance));
3284 #endif
3285         }
3286       else if (NILP (instance))
3287         signal_simple_error ("Can't instantiate image (probably cached)",
3288                              instantiator);
3289       /* We found an instance. However, because we are using the glyph
3290          as the hash key instead of the instantiator, the current
3291          instantiator may not be the same as the original. Thus we
3292          must update the instance based on the new
3293          instantiator. Preserving instance identity like this is
3294          important to stop excessive window system widget creation and
3295          deletion - and hence flashing. */
3296       else
3297         {
3298           /* #### This function should be able to cope with *all*
3299              changes to the instantiator, but currently only copes
3300              with the most used properties. This means that it is
3301              possible to make changes that don't get reflected in the
3302              display. */
3303           update_image_instance (instance, instantiator);
3304           free_list (hash_key);
3305         }
3306
3307 #ifdef ERROR_CHECK_GLYPHS
3308       if (image_instance_type_to_mask (XIMAGE_INSTANCE_TYPE (instance))
3309           & (IMAGE_SUBWINDOW_MASK | IMAGE_WIDGET_MASK))
3310         assert (EQ (XIMAGE_INSTANCE_FRAME (instance),
3311                     DOMAIN_FRAME (domain)));
3312 #endif
3313       ERROR_CHECK_IMAGE_INSTANCE (instance);
3314       RETURN_UNGCPRO (instance);
3315     }
3316
3317   abort ();
3318   return Qnil; /* not reached */
3319 }
3320
3321 /* Validate an image instantiator. */
3322
3323 static void
3324 image_validate (Lisp_Object instantiator)
3325 {
3326   if (IMAGE_INSTANCEP (instantiator) || STRINGP (instantiator))
3327     return;
3328   else if (VECTORP (instantiator))
3329     {
3330       Lisp_Object *elt = XVECTOR_DATA (instantiator);
3331       int instantiator_len = XVECTOR_LENGTH (instantiator);
3332       struct image_instantiator_methods *meths;
3333       Lisp_Object already_seen = Qnil;
3334       struct gcpro gcpro1;
3335       int i;
3336
3337       if (instantiator_len < 1)
3338         signal_simple_error ("Vector length must be at least 1",
3339                              instantiator);
3340
3341       meths = decode_image_instantiator_format (elt[0], ERROR_ME);
3342       if (!(instantiator_len & 1))
3343         signal_simple_error
3344           ("Must have alternating keyword/value pairs", instantiator);
3345
3346       GCPRO1 (already_seen);
3347
3348       for (i = 1; i < instantiator_len; i += 2)
3349         {
3350           Lisp_Object keyword = elt[i];
3351           Lisp_Object value = elt[i+1];
3352           int j;
3353
3354           CHECK_SYMBOL (keyword);
3355           if (!SYMBOL_IS_KEYWORD (keyword))
3356             signal_simple_error ("Symbol must begin with a colon", keyword);
3357
3358           for (j = 0; j < Dynarr_length (meths->keywords); j++)
3359             if (EQ (keyword, Dynarr_at (meths->keywords, j).keyword))
3360               break;
3361
3362           if (j == Dynarr_length (meths->keywords))
3363             signal_simple_error ("Unrecognized keyword", keyword);
3364
3365           if (!Dynarr_at (meths->keywords, j).multiple_p)
3366             {
3367               if (!NILP (memq_no_quit (keyword, already_seen)))
3368                 signal_simple_error
3369                   ("Keyword may not appear more than once", keyword);
3370               already_seen = Fcons (keyword, already_seen);
3371             }
3372
3373           (Dynarr_at (meths->keywords, j).validate) (value);
3374         }
3375
3376       UNGCPRO;
3377
3378       MAYBE_IIFORMAT_METH (meths, validate, (instantiator));
3379     }
3380   else
3381     signal_simple_error ("Must be string or vector", instantiator);
3382 }
3383
3384 static void
3385 image_after_change (Lisp_Object specifier, Lisp_Object locale)
3386 {
3387   Lisp_Object attachee =
3388     IMAGE_SPECIFIER_ATTACHEE (XIMAGE_SPECIFIER (specifier));
3389   Lisp_Object property =
3390     IMAGE_SPECIFIER_ATTACHEE_PROPERTY (XIMAGE_SPECIFIER (specifier));
3391   if (FACEP (attachee))
3392     {
3393       face_property_was_changed (attachee, property, locale);
3394       if (BUFFERP (locale))
3395         XBUFFER (locale)->buffer_local_face_property = 1;
3396     }
3397   else if (GLYPHP (attachee))
3398     glyph_property_was_changed (attachee, property, locale);
3399 }
3400
3401 void
3402 set_image_attached_to (Lisp_Object obj, Lisp_Object face_or_glyph,
3403                        Lisp_Object property)
3404 {
3405   Lisp_Specifier *image = XIMAGE_SPECIFIER (obj);
3406
3407   IMAGE_SPECIFIER_ATTACHEE (image) = face_or_glyph;
3408   IMAGE_SPECIFIER_ATTACHEE_PROPERTY (image) = property;
3409 }
3410
3411 static Lisp_Object
3412 image_going_to_add (Lisp_Object specifier, Lisp_Object locale,
3413                     Lisp_Object tag_set, Lisp_Object instantiator)
3414 {
3415   Lisp_Object possible_console_types = Qnil;
3416   Lisp_Object rest;
3417   Lisp_Object retlist = Qnil;
3418   struct gcpro gcpro1, gcpro2;
3419
3420   LIST_LOOP (rest, Vconsole_type_list)
3421     {
3422       Lisp_Object contype = XCAR (rest);
3423       if (!NILP (memq_no_quit (contype, tag_set)))
3424         possible_console_types = Fcons (contype, possible_console_types);
3425     }
3426
3427   if (XINT (Flength (possible_console_types)) > 1)
3428     /* two conflicting console types specified */
3429     return Qnil;
3430
3431   if (NILP (possible_console_types))
3432     possible_console_types = Vconsole_type_list;
3433
3434   GCPRO2 (retlist, possible_console_types);
3435
3436   LIST_LOOP (rest, possible_console_types)
3437     {
3438       Lisp_Object contype = XCAR (rest);
3439       Lisp_Object newinst = call_with_suspended_errors
3440         ((lisp_fn_t) normalize_image_instantiator,
3441          Qnil, Qimage, ERROR_ME_NOT, 3, instantiator, contype,
3442          make_int (XIMAGE_SPECIFIER_ALLOWED (specifier)));
3443
3444       if (!NILP (newinst))
3445         {
3446           Lisp_Object newtag;
3447           if (NILP (memq_no_quit (contype, tag_set)))
3448             newtag = Fcons (contype, tag_set);
3449           else
3450             newtag = tag_set;
3451           retlist = Fcons (Fcons (newtag, newinst), retlist);
3452         }
3453     }
3454
3455   UNGCPRO;
3456
3457   return retlist;
3458 }
3459
3460 /* Copy an image instantiator. We can't use Fcopy_tree since widgets
3461    may contain circular references which would send Fcopy_tree into
3462    infloop death. */
3463 static Lisp_Object
3464 image_copy_vector_instantiator (Lisp_Object instantiator)
3465 {
3466   int i;
3467   struct image_instantiator_methods *meths;
3468   Lisp_Object *elt;
3469   int instantiator_len;
3470
3471   CHECK_VECTOR (instantiator);
3472
3473   instantiator = Fcopy_sequence (instantiator);
3474   elt = XVECTOR_DATA (instantiator);
3475   instantiator_len = XVECTOR_LENGTH (instantiator);
3476
3477   meths = decode_image_instantiator_format (elt[0], ERROR_ME);
3478
3479   for (i = 1; i < instantiator_len; i += 2)
3480     {
3481       int j;
3482       Lisp_Object keyword = elt[i];
3483       Lisp_Object value = elt[i+1];
3484
3485       /* Find the keyword entry. */
3486       for (j = 0; j < Dynarr_length (meths->keywords); j++)
3487         {
3488           if (EQ (keyword, Dynarr_at (meths->keywords, j).keyword))
3489             break;
3490         }
3491
3492       /* Only copy keyword values that should be copied. */
3493       if (Dynarr_at (meths->keywords, j).copy_p
3494           &&
3495           (CONSP (value) || VECTORP (value)))
3496         {
3497           elt [i+1] = Fcopy_tree (value, Qt);
3498         }
3499     }
3500
3501   return instantiator;
3502 }
3503
3504 static Lisp_Object
3505 image_copy_instantiator (Lisp_Object arg)
3506 {
3507   if (CONSP (arg))
3508     {
3509       Lisp_Object rest;
3510       rest = arg = Fcopy_sequence (arg);
3511       while (CONSP (rest))
3512         {
3513           Lisp_Object elt = XCAR (rest);
3514           if (CONSP (elt))
3515             XCAR (rest) = Fcopy_tree (elt, Qt);
3516           else if (VECTORP (elt))
3517             XCAR (rest) = image_copy_vector_instantiator (elt);
3518           if (VECTORP (XCDR (rest))) /* hack for (a b . [c d]) */
3519             XCDR (rest) = Fcopy_tree (XCDR (rest), Qt);
3520           rest = XCDR (rest);
3521         }
3522     }
3523   else if (VECTORP (arg))
3524     {
3525       arg = image_copy_vector_instantiator (arg);
3526     }
3527   return arg;
3528 }
3529
3530 DEFUN ("image-specifier-p", Fimage_specifier_p, 1, 1, 0, /*
3531 Return non-nil if OBJECT is an image specifier.
3532 See `make-image-specifier' for a description of image instantiators.
3533 */
3534        (object))
3535 {
3536   return IMAGE_SPECIFIERP (object) ? Qt : Qnil;
3537 }
3538
3539 \f
3540 /****************************************************************************
3541  *                             Glyph Object                                 *
3542  ****************************************************************************/
3543
3544 static Lisp_Object
3545 mark_glyph (Lisp_Object obj)
3546 {
3547   Lisp_Glyph *glyph = XGLYPH (obj);
3548
3549   mark_object (glyph->image);
3550   mark_object (glyph->contrib_p);
3551   mark_object (glyph->baseline);
3552   mark_object (glyph->face);
3553
3554   return glyph->plist;
3555 }
3556
3557 static void
3558 print_glyph (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
3559 {
3560   Lisp_Glyph *glyph = XGLYPH (obj);
3561   char buf[20];
3562
3563   if (print_readably)
3564     error ("printing unreadable object #<glyph 0x%x>", glyph->header.uid);
3565
3566   write_c_string ("#<glyph (", printcharfun);
3567   print_internal (Fglyph_type (obj), printcharfun, 0);
3568   write_c_string (") ", printcharfun);
3569   print_internal (glyph->image, printcharfun, 1);
3570   sprintf (buf, "0x%x>", glyph->header.uid);
3571   write_c_string (buf, printcharfun);
3572 }
3573
3574 /* Glyphs are equal if all of their display attributes are equal.  We
3575    don't compare names or doc-strings, because that would make equal
3576    be eq.
3577
3578    This isn't concerned with "unspecified" attributes, that's what
3579    #'glyph-differs-from-default-p is for. */
3580 static int
3581 glyph_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
3582 {
3583   Lisp_Glyph *g1 = XGLYPH (obj1);
3584   Lisp_Glyph *g2 = XGLYPH (obj2);
3585
3586   depth++;
3587
3588   return (internal_equal (g1->image,     g2->image,     depth) &&
3589           internal_equal (g1->contrib_p, g2->contrib_p, depth) &&
3590           internal_equal (g1->baseline,  g2->baseline,  depth) &&
3591           internal_equal (g1->face,      g2->face,      depth) &&
3592           !plists_differ (g1->plist,     g2->plist, 0, 0, depth + 1));
3593 }
3594
3595 static unsigned long
3596 glyph_hash (Lisp_Object obj, int depth)
3597 {
3598   depth++;
3599
3600   /* No need to hash all of the elements; that would take too long.
3601      Just hash the most common ones. */
3602   return HASH2 (internal_hash (XGLYPH (obj)->image, depth),
3603                 internal_hash (XGLYPH (obj)->face,  depth));
3604 }
3605
3606 static Lisp_Object
3607 glyph_getprop (Lisp_Object obj, Lisp_Object prop)
3608 {
3609   Lisp_Glyph *g = XGLYPH (obj);
3610
3611   if (EQ (prop, Qimage))     return g->image;
3612   if (EQ (prop, Qcontrib_p)) return g->contrib_p;
3613   if (EQ (prop, Qbaseline))  return g->baseline;
3614   if (EQ (prop, Qface))      return g->face;
3615
3616   return external_plist_get (&g->plist, prop, 0, ERROR_ME);
3617 }
3618
3619 static int
3620 glyph_putprop (Lisp_Object obj, Lisp_Object prop, Lisp_Object value)
3621 {
3622   if (EQ (prop, Qimage)     ||
3623       EQ (prop, Qcontrib_p) ||
3624       EQ (prop, Qbaseline))
3625     return 0;
3626
3627   if (EQ (prop, Qface))
3628     {
3629       XGLYPH (obj)->face = Fget_face (value);
3630       return 1;
3631     }
3632
3633   external_plist_put (&XGLYPH (obj)->plist, prop, value, 0, ERROR_ME);
3634   return 1;
3635 }
3636
3637 static int
3638 glyph_remprop (Lisp_Object obj, Lisp_Object prop)
3639 {
3640   if (EQ (prop, Qimage)     ||
3641       EQ (prop, Qcontrib_p) ||
3642       EQ (prop, Qbaseline))
3643     return -1;
3644
3645   if (EQ (prop, Qface))
3646     {
3647       XGLYPH (obj)->face = Qnil;
3648       return 1;
3649     }
3650
3651   return external_remprop (&XGLYPH (obj)->plist, prop, 0, ERROR_ME);
3652 }
3653
3654 static Lisp_Object
3655 glyph_plist (Lisp_Object obj)
3656 {
3657   Lisp_Glyph *glyph = XGLYPH (obj);
3658   Lisp_Object result = glyph->plist;
3659
3660   result = cons3 (Qface,      glyph->face,      result);
3661   result = cons3 (Qbaseline,  glyph->baseline,  result);
3662   result = cons3 (Qcontrib_p, glyph->contrib_p, result);
3663   result = cons3 (Qimage,     glyph->image,     result);
3664
3665   return result;
3666 }
3667
3668 static const struct lrecord_description glyph_description[] = {
3669   { XD_LISP_OBJECT, offsetof (Lisp_Glyph, image) },
3670   { XD_LISP_OBJECT, offsetof (Lisp_Glyph, contrib_p) },
3671   { XD_LISP_OBJECT, offsetof (Lisp_Glyph, baseline) },
3672   { XD_LISP_OBJECT, offsetof (Lisp_Glyph, face) },
3673   { XD_LISP_OBJECT, offsetof (Lisp_Glyph, plist) },
3674   { XD_END }
3675 };
3676
3677 DEFINE_LRECORD_IMPLEMENTATION_WITH_PROPS ("glyph", glyph,
3678                                           mark_glyph, print_glyph, 0,
3679                                           glyph_equal, glyph_hash, glyph_description,
3680                                           glyph_getprop, glyph_putprop,
3681                                           glyph_remprop, glyph_plist,
3682                                           Lisp_Glyph);
3683 \f
3684 Lisp_Object
3685 allocate_glyph (enum glyph_type type,
3686                 void (*after_change) (Lisp_Object glyph, Lisp_Object property,
3687                                       Lisp_Object locale))
3688 {
3689   /* This function can GC */
3690   Lisp_Object obj = Qnil;
3691   Lisp_Glyph *g = alloc_lcrecord_type (Lisp_Glyph, &lrecord_glyph);
3692
3693   g->type = type;
3694   g->image = Fmake_specifier (Qimage); /* This function can GC */
3695   g->dirty = 0;
3696   switch (g->type)
3697     {
3698     case GLYPH_BUFFER:
3699       XIMAGE_SPECIFIER_ALLOWED (g->image) =
3700         IMAGE_NOTHING_MASK | IMAGE_TEXT_MASK
3701         | IMAGE_MONO_PIXMAP_MASK | IMAGE_COLOR_PIXMAP_MASK
3702         | IMAGE_SUBWINDOW_MASK | IMAGE_WIDGET_MASK;
3703       break;
3704     case GLYPH_POINTER:
3705       XIMAGE_SPECIFIER_ALLOWED (g->image) =
3706         IMAGE_NOTHING_MASK | IMAGE_POINTER_MASK;
3707       break;
3708     case GLYPH_ICON:
3709       XIMAGE_SPECIFIER_ALLOWED (g->image) =
3710         IMAGE_NOTHING_MASK | IMAGE_MONO_PIXMAP_MASK
3711         | IMAGE_COLOR_PIXMAP_MASK;
3712       break;
3713     default:
3714       abort ();
3715     }
3716
3717   /* I think Fmake_specifier can GC.  I think set_specifier_fallback can GC. */
3718   /* We're getting enough reports of odd behavior in this area it seems */
3719   /* best to GCPRO everything. */
3720   {
3721     Lisp_Object tem1 = list1 (Fcons (Qnil, Vthe_nothing_vector));
3722     Lisp_Object tem2 = list1 (Fcons (Qnil, Qt));
3723     Lisp_Object tem3 = list1 (Fcons (Qnil, Qnil));
3724     struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3725
3726     GCPRO4 (obj, tem1, tem2, tem3);
3727
3728     set_specifier_fallback (g->image, tem1);
3729     g->contrib_p = Fmake_specifier (Qboolean);
3730     set_specifier_fallback (g->contrib_p, tem2);
3731     /* #### should have a specifier for the following */
3732     g->baseline = Fmake_specifier (Qgeneric);
3733     set_specifier_fallback (g->baseline, tem3);
3734     g->face = Qnil;
3735     g->plist = Qnil;
3736     g->after_change = after_change;
3737     XSETGLYPH (obj, g);
3738
3739     set_image_attached_to (g->image, obj, Qimage);
3740     UNGCPRO;
3741   }
3742
3743   return obj;
3744 }
3745
3746 static enum glyph_type
3747 decode_glyph_type (Lisp_Object type, Error_behavior errb)
3748 {
3749   if (NILP (type))
3750     return GLYPH_BUFFER;
3751
3752   if (ERRB_EQ (errb, ERROR_ME))
3753     CHECK_SYMBOL (type);
3754
3755   if (EQ (type, Qbuffer))  return GLYPH_BUFFER;
3756   if (EQ (type, Qpointer)) return GLYPH_POINTER;
3757   if (EQ (type, Qicon))    return GLYPH_ICON;
3758
3759   maybe_signal_simple_error ("Invalid glyph type", type, Qimage, errb);
3760
3761   return GLYPH_UNKNOWN;
3762 }
3763
3764 static int
3765 valid_glyph_type_p (Lisp_Object type)
3766 {
3767   return !NILP (memq_no_quit (type, Vglyph_type_list));
3768 }
3769
3770 DEFUN ("valid-glyph-type-p", Fvalid_glyph_type_p, 1, 1, 0, /*
3771 Given a GLYPH-TYPE, return non-nil if it is valid.
3772 Valid types are `buffer', `pointer', and `icon'.
3773 */
3774        (glyph_type))
3775 {
3776   return valid_glyph_type_p (glyph_type) ? Qt : Qnil;
3777 }
3778
3779 DEFUN ("glyph-type-list", Fglyph_type_list, 0, 0, 0, /*
3780 Return a list of valid glyph types.
3781 */
3782        ())
3783 {
3784   return Fcopy_sequence (Vglyph_type_list);
3785 }
3786
3787 DEFUN ("make-glyph-internal", Fmake_glyph_internal, 0, 1, 0, /*
3788 Create and return a new uninitialized glyph of type TYPE.
3789
3790 TYPE specifies the type of the glyph; this should be one of `buffer',
3791 `pointer', or `icon', and defaults to `buffer'.  The type of the glyph
3792 specifies in which contexts the glyph can be used, and controls the
3793 allowable image types into which the glyph's image can be
3794 instantiated.
3795
3796 `buffer' glyphs can be used as the begin-glyph or end-glyph of an
3797 extent, in the modeline, and in the toolbar.  Their image can be
3798 instantiated as `nothing', `mono-pixmap', `color-pixmap', `text',
3799 and `subwindow'.
3800
3801 `pointer' glyphs can be used to specify the mouse pointer.  Their
3802 image can be instantiated as `pointer'.
3803
3804 `icon' glyphs can be used to specify the icon used when a frame is
3805 iconified.  Their image can be instantiated as `mono-pixmap' and
3806 `color-pixmap'.
3807 */
3808        (type))
3809 {
3810   enum glyph_type typeval = decode_glyph_type (type, ERROR_ME);
3811   return allocate_glyph (typeval, 0);
3812 }
3813
3814 DEFUN ("glyphp", Fglyphp, 1, 1, 0, /*
3815 Return non-nil if OBJECT is a glyph.
3816
3817 A glyph is an object used for pixmaps, widgets and the like.  It is used
3818 in begin-glyphs and end-glyphs attached to extents, in marginal and textual
3819 annotations, in overlay arrows (overlay-arrow-* variables), in toolbar
3820 buttons, and the like.  Much more detailed information can be found at
3821 `make-glyph'.  Its image is described using an image specifier --
3822 see `make-image-specifier'.  See also `make-image-instance' for further
3823 information.
3824 */
3825        (object))
3826 {
3827   return GLYPHP (object) ? Qt : Qnil;
3828 }
3829
3830 DEFUN ("glyph-type", Fglyph_type, 1, 1, 0, /*
3831 Return the type of the given glyph.
3832 The return value will be one of 'buffer, 'pointer, or 'icon.
3833 */
3834        (glyph))
3835 {
3836   CHECK_GLYPH (glyph);
3837   switch (XGLYPH_TYPE (glyph))
3838     {
3839     default: abort ();
3840     case GLYPH_BUFFER:  return Qbuffer;
3841     case GLYPH_POINTER: return Qpointer;
3842     case GLYPH_ICON:    return Qicon;
3843     }
3844 }
3845
3846 Lisp_Object
3847 glyph_image_instance (Lisp_Object glyph, Lisp_Object domain,
3848                       Error_behavior errb, int no_quit)
3849 {
3850   Lisp_Object specifier = GLYPH_IMAGE (XGLYPH (glyph));
3851
3852   /* This can never return Qunbound.  All glyphs have 'nothing as
3853      a fallback. */
3854   Lisp_Object image_instance = specifier_instance (specifier, Qunbound,
3855                                                    domain, errb, no_quit, 0,
3856                                                    Qzero);
3857   assert (!UNBOUNDP (image_instance));
3858   ERROR_CHECK_IMAGE_INSTANCE (image_instance);
3859
3860   return image_instance;
3861 }
3862
3863 static Lisp_Object
3864 glyph_image_instance_maybe (Lisp_Object glyph_or_image, Lisp_Object window)
3865 {
3866   Lisp_Object instance = glyph_or_image;
3867
3868   if (GLYPHP (glyph_or_image))
3869     instance = glyph_image_instance (glyph_or_image, window, ERROR_ME_NOT, 1);
3870
3871   return instance;
3872 }
3873
3874 /*****************************************************************************
3875  glyph_width
3876
3877  Return the width of the given GLYPH on the given WINDOW.
3878  Calculations are done based on recursively querying the geometry of
3879  the associated image instances.
3880  ****************************************************************************/
3881 unsigned short
3882 glyph_width (Lisp_Object glyph_or_image, Lisp_Object domain)
3883 {
3884   Lisp_Object instance = glyph_image_instance_maybe (glyph_or_image,
3885                                                      domain);
3886   if (!IMAGE_INSTANCEP (instance))
3887     return 0;
3888
3889   if (XIMAGE_INSTANCE_NEEDS_LAYOUT (instance))
3890     image_instance_layout (instance, IMAGE_UNSPECIFIED_GEOMETRY,
3891                            IMAGE_UNSPECIFIED_GEOMETRY,
3892                            IMAGE_UNCHANGED_GEOMETRY,
3893                            IMAGE_UNCHANGED_GEOMETRY, domain);
3894
3895   return XIMAGE_INSTANCE_WIDTH (instance);
3896 }
3897
3898 DEFUN ("glyph-width", Fglyph_width, 1, 2, 0, /*
3899 Return the width of GLYPH on WINDOW.
3900 This may not be exact as it does not take into account all of the context
3901 that redisplay will.
3902 */
3903        (glyph, window))
3904 {
3905   XSETWINDOW (window, decode_window (window));
3906   CHECK_GLYPH (glyph);
3907
3908   return make_int (glyph_width (glyph, window));
3909 }
3910
3911 unsigned short
3912 glyph_ascent (Lisp_Object glyph_or_image, Lisp_Object domain)
3913 {
3914   Lisp_Object instance = glyph_image_instance_maybe (glyph_or_image,
3915                                                      domain);
3916   if (!IMAGE_INSTANCEP (instance))
3917     return 0;
3918
3919   if (XIMAGE_INSTANCE_NEEDS_LAYOUT (instance))
3920     image_instance_layout (instance, IMAGE_UNSPECIFIED_GEOMETRY,
3921                            IMAGE_UNSPECIFIED_GEOMETRY,
3922                            IMAGE_UNCHANGED_GEOMETRY,
3923                            IMAGE_UNCHANGED_GEOMETRY, domain);
3924
3925   if (XIMAGE_INSTANCE_TYPE (instance) == IMAGE_TEXT)
3926     return XIMAGE_INSTANCE_TEXT_ASCENT (instance);
3927   else
3928     return XIMAGE_INSTANCE_HEIGHT (instance);
3929 }
3930
3931 unsigned short
3932 glyph_descent (Lisp_Object glyph_or_image, Lisp_Object domain)
3933 {
3934   Lisp_Object instance = glyph_image_instance_maybe (glyph_or_image,
3935                                                      domain);
3936   if (!IMAGE_INSTANCEP (instance))
3937     return 0;
3938
3939   if (XIMAGE_INSTANCE_NEEDS_LAYOUT (instance))
3940     image_instance_layout (instance, IMAGE_UNSPECIFIED_GEOMETRY,
3941                            IMAGE_UNSPECIFIED_GEOMETRY,
3942                            IMAGE_UNCHANGED_GEOMETRY,
3943                            IMAGE_UNCHANGED_GEOMETRY, domain);
3944
3945   if (XIMAGE_INSTANCE_TYPE (instance) ==  IMAGE_TEXT)
3946     return XIMAGE_INSTANCE_TEXT_DESCENT (instance);
3947   else
3948     return 0;
3949 }
3950
3951 /* strictly a convenience function. */
3952 unsigned short
3953 glyph_height (Lisp_Object glyph_or_image, Lisp_Object domain)
3954 {
3955   Lisp_Object instance = glyph_image_instance_maybe (glyph_or_image,
3956                                                      domain);
3957
3958   if (!IMAGE_INSTANCEP (instance))
3959     return 0;
3960
3961   if (XIMAGE_INSTANCE_NEEDS_LAYOUT (instance))
3962     image_instance_layout (instance, IMAGE_UNSPECIFIED_GEOMETRY,
3963                            IMAGE_UNSPECIFIED_GEOMETRY,
3964                            IMAGE_UNCHANGED_GEOMETRY,
3965                            IMAGE_UNCHANGED_GEOMETRY, domain);
3966
3967   return XIMAGE_INSTANCE_HEIGHT (instance);
3968 }
3969
3970 DEFUN ("glyph-ascent", Fglyph_ascent, 1, 2, 0, /*
3971 Return the ascent value of GLYPH on WINDOW.
3972 This may not be exact as it does not take into account all of the context
3973 that redisplay will.
3974 */
3975        (glyph, window))
3976 {
3977   XSETWINDOW (window, decode_window (window));
3978   CHECK_GLYPH (glyph);
3979
3980   return make_int (glyph_ascent (glyph, window));
3981 }
3982
3983 DEFUN ("glyph-descent", Fglyph_descent, 1, 2, 0, /*
3984 Return the descent value of GLYPH on WINDOW.
3985 This may not be exact as it does not take into account all of the context
3986 that redisplay will.
3987 */
3988        (glyph, window))
3989 {
3990   XSETWINDOW (window, decode_window (window));
3991   CHECK_GLYPH (glyph);
3992
3993   return make_int (glyph_descent (glyph, window));
3994 }
3995
3996 /* This is redundant but I bet a lot of people expect it to exist. */
3997 DEFUN ("glyph-height", Fglyph_height, 1, 2, 0, /*
3998 Return the height of GLYPH on WINDOW.
3999 This may not be exact as it does not take into account all of the context
4000 that redisplay will.
4001 */
4002        (glyph, window))
4003 {
4004   XSETWINDOW (window, decode_window (window));
4005   CHECK_GLYPH (glyph);
4006
4007   return make_int (glyph_height (glyph, window));
4008 }
4009
4010 static void
4011 set_glyph_dirty_p (Lisp_Object glyph_or_image, Lisp_Object window, int dirty)
4012 {
4013   Lisp_Object instance = glyph_or_image;
4014
4015   if (!NILP (glyph_or_image))
4016     {
4017       if (GLYPHP (glyph_or_image))
4018         {
4019           instance = glyph_image_instance (glyph_or_image, window,
4020                                            ERROR_ME_NOT, 1);
4021           XGLYPH_DIRTYP (glyph_or_image) = dirty;
4022         }
4023
4024       if (!IMAGE_INSTANCEP (instance))
4025         return;
4026
4027       XIMAGE_INSTANCE_DIRTYP (instance) = dirty;
4028     }
4029 }
4030
4031 static void
4032 set_image_instance_dirty_p (Lisp_Object instance, int dirty)
4033 {
4034   if (IMAGE_INSTANCEP (instance))
4035     {
4036       XIMAGE_INSTANCE_DIRTYP (instance) = dirty;
4037       /* Now cascade up the hierarchy. */
4038       set_image_instance_dirty_p (XIMAGE_INSTANCE_PARENT (instance),
4039                                   dirty);
4040     }
4041   else if (GLYPHP (instance))
4042     {
4043       XGLYPH_DIRTYP (instance) = dirty;
4044     }
4045 }
4046
4047 /* #### do we need to cache this info to speed things up? */
4048
4049 Lisp_Object
4050 glyph_baseline (Lisp_Object glyph, Lisp_Object domain)
4051 {
4052   if (!GLYPHP (glyph))
4053     return Qnil;
4054   else
4055     {
4056       Lisp_Object retval =
4057         specifier_instance_no_quit (GLYPH_BASELINE (XGLYPH (glyph)),
4058                                     /* #### look into ERROR_ME_NOT */
4059                                     Qunbound, domain, ERROR_ME_NOT,
4060                                     0, Qzero);
4061       if (!NILP (retval) && !INTP (retval))
4062         retval = Qnil;
4063       else if (INTP (retval))
4064         {
4065           if (XINT (retval) < 0)
4066             retval = Qzero;
4067           if (XINT (retval) > 100)
4068             retval = make_int (100);
4069         }
4070       return retval;
4071     }
4072 }
4073
4074 Lisp_Object
4075 glyph_face (Lisp_Object glyph, Lisp_Object domain)
4076 {
4077   /* #### Domain parameter not currently used but it will be */
4078   return GLYPHP (glyph) ? GLYPH_FACE (XGLYPH (glyph)) : Qnil;
4079 }
4080
4081 int
4082 glyph_contrib_p (Lisp_Object glyph, Lisp_Object domain)
4083 {
4084   if (!GLYPHP (glyph))
4085     return 0;
4086   else
4087     return !NILP (specifier_instance_no_quit
4088                   (GLYPH_CONTRIB_P (XGLYPH (glyph)), Qunbound, domain,
4089                    /* #### look into ERROR_ME_NOT */
4090                    ERROR_ME_NOT, 0, Qzero));
4091 }
4092
4093 static void
4094 glyph_property_was_changed (Lisp_Object glyph, Lisp_Object property,
4095                             Lisp_Object locale)
4096 {
4097   if (XGLYPH (glyph)->after_change)
4098     (XGLYPH (glyph)->after_change) (glyph, property, locale);
4099 }
4100
4101 void
4102 glyph_query_geometry (Lisp_Object glyph_or_image, int* width, int* height,
4103                       enum image_instance_geometry disp, Lisp_Object domain)
4104 {
4105   Lisp_Object instance = glyph_or_image;
4106
4107   if (GLYPHP (glyph_or_image))
4108     instance = glyph_image_instance (glyph_or_image, domain, ERROR_ME_NOT, 1);
4109
4110   image_instance_query_geometry (instance, width, height, disp, domain);
4111 }
4112
4113 void
4114 glyph_do_layout (Lisp_Object glyph_or_image, int width, int height,
4115               int xoffset, int yoffset, Lisp_Object domain)
4116 {
4117   Lisp_Object instance = glyph_or_image;
4118
4119   if (GLYPHP (glyph_or_image))
4120     instance = glyph_image_instance (glyph_or_image, domain, ERROR_ME_NOT, 1);
4121
4122   image_instance_layout (instance, width, height, xoffset, yoffset, domain);
4123 }
4124
4125 \f
4126 /*****************************************************************************
4127  *                     glyph cachel functions                                *
4128  *****************************************************************************/
4129
4130 /* #### All of this is 95% copied from face cachels.  Consider
4131   consolidating.
4132
4133   Why do we need glyph_cachels? Simply because a glyph_cachel captures
4134   per-window information about a particular glyph. A glyph itself is
4135   not created in any particular context, so if we were to rely on a
4136   glyph to tell us about its dirtiness we would not be able to reset
4137   the dirty flag after redisplaying it as it may exist in other
4138   contexts. When we have redisplayed we need to know which glyphs to
4139   reset the dirty flags on - the glyph_cachels give us a nice list we
4140   can iterate through doing this.  */
4141 void
4142 mark_glyph_cachels (glyph_cachel_dynarr *elements)
4143 {
4144   int elt;
4145
4146   if (!elements)
4147     return;
4148
4149   for (elt = 0; elt < Dynarr_length (elements); elt++)
4150     {
4151       struct glyph_cachel *cachel = Dynarr_atp (elements, elt);
4152       mark_object (cachel->glyph);
4153     }
4154 }
4155
4156 static void
4157 update_glyph_cachel_data (struct window *w, Lisp_Object glyph,
4158                           struct glyph_cachel *cachel)
4159 {
4160   if (!cachel->updated || NILP (cachel->glyph) || !EQ (cachel->glyph, glyph)
4161       || XGLYPH_DIRTYP (cachel->glyph)
4162       || XFRAME(WINDOW_FRAME(w))->faces_changed)
4163     {
4164       Lisp_Object window, instance;
4165
4166       XSETWINDOW (window, w);
4167
4168       cachel->glyph   = glyph;
4169       /* Speed things up slightly by grabbing the glyph instantiation
4170          and passing it to the size functions. */
4171       instance = glyph_image_instance (glyph, window, ERROR_ME_NOT, 1);
4172
4173       if (!IMAGE_INSTANCEP (instance))
4174         return;
4175
4176       /* Mark text instance of the glyph dirty if faces have changed,
4177          because its geometry might have changed. */
4178       invalidate_glyph_geometry_maybe (instance, w);
4179
4180       /* #### Do the following 2 lines buy us anything? --kkm */
4181       XGLYPH_DIRTYP (glyph) = XIMAGE_INSTANCE_DIRTYP (instance);
4182       cachel->dirty   = XGLYPH_DIRTYP (glyph);
4183       cachel->width   = glyph_width   (instance, window);
4184       cachel->ascent  = glyph_ascent  (instance, window);
4185       cachel->descent = glyph_descent (instance, window);
4186     }
4187
4188   cachel->updated = 1;
4189 }
4190
4191 static void
4192 add_glyph_cachel (struct window *w, Lisp_Object glyph)
4193 {
4194   struct glyph_cachel new_cachel;
4195
4196   xzero (new_cachel);
4197   new_cachel.glyph = Qnil;
4198
4199   update_glyph_cachel_data (w, glyph, &new_cachel);
4200   Dynarr_add (w->glyph_cachels, new_cachel);
4201 }
4202
4203 glyph_index
4204 get_glyph_cachel_index (struct window *w, Lisp_Object glyph)
4205 {
4206   int elt;
4207
4208   if (noninteractive)
4209     return 0;
4210
4211   for (elt = 0; elt < Dynarr_length (w->glyph_cachels); elt++)
4212     {
4213       struct glyph_cachel *cachel =
4214         Dynarr_atp (w->glyph_cachels, elt);
4215
4216       if (EQ (cachel->glyph, glyph) && !NILP (glyph))
4217         {
4218           update_glyph_cachel_data (w, glyph, cachel);
4219           return elt;
4220         }
4221     }
4222
4223   /* If we didn't find the glyph, add it and then return its index. */
4224   add_glyph_cachel (w, glyph);
4225   return elt;
4226 }
4227
4228 void
4229 reset_glyph_cachels (struct window *w)
4230 {
4231   Dynarr_reset (w->glyph_cachels);
4232   get_glyph_cachel_index (w, Vcontinuation_glyph);
4233   get_glyph_cachel_index (w, Vtruncation_glyph);
4234   get_glyph_cachel_index (w, Vhscroll_glyph);
4235   get_glyph_cachel_index (w, Vcontrol_arrow_glyph);
4236   get_glyph_cachel_index (w, Voctal_escape_glyph);
4237   get_glyph_cachel_index (w, Vinvisible_text_glyph);
4238 }
4239
4240 void
4241 mark_glyph_cachels_as_not_updated (struct window *w)
4242 {
4243   int elt;
4244
4245   /* We need to have a dirty flag to tell if the glyph has changed.
4246      We can check to see if each glyph variable is actually a
4247      completely different glyph, though. */
4248 #define FROB(glyph_obj, gindex)                                         \
4249   update_glyph_cachel_data (w, glyph_obj,                               \
4250                               Dynarr_atp (w->glyph_cachels, gindex))
4251
4252   FROB (Vcontinuation_glyph, CONT_GLYPH_INDEX);
4253   FROB (Vtruncation_glyph, TRUN_GLYPH_INDEX);
4254   FROB (Vhscroll_glyph, HSCROLL_GLYPH_INDEX);
4255   FROB (Vcontrol_arrow_glyph, CONTROL_GLYPH_INDEX);
4256   FROB (Voctal_escape_glyph, OCT_ESC_GLYPH_INDEX);
4257   FROB (Vinvisible_text_glyph, INVIS_GLYPH_INDEX);
4258 #undef FROB
4259
4260   for (elt = 0; elt < Dynarr_length (w->glyph_cachels); elt++)
4261     {
4262       Dynarr_atp (w->glyph_cachels, elt)->updated = 0;
4263     }
4264 }
4265
4266 /* Unset the dirty bit on all the glyph cachels that have it. */
4267 void
4268 mark_glyph_cachels_as_clean (struct window* w)
4269 {
4270   int elt;
4271   Lisp_Object window;
4272   XSETWINDOW (window, w);
4273   for (elt = 0; elt < Dynarr_length (w->glyph_cachels); elt++)
4274     {
4275       struct glyph_cachel *cachel = Dynarr_atp (w->glyph_cachels, elt);
4276       cachel->dirty = 0;
4277       set_glyph_dirty_p (cachel->glyph, window, 0);
4278     }
4279 }
4280
4281 #ifdef MEMORY_USAGE_STATS
4282
4283 int
4284 compute_glyph_cachel_usage (glyph_cachel_dynarr *glyph_cachels,
4285                             struct overhead_stats *ovstats)
4286 {
4287   int total = 0;
4288
4289   if (glyph_cachels)
4290     total += Dynarr_memory_usage (glyph_cachels, ovstats);
4291
4292   return total;
4293 }
4294
4295 #endif /* MEMORY_USAGE_STATS */
4296
4297
4298 \f
4299 /*****************************************************************************
4300  *                     subwindow cachel functions                                    *
4301  *****************************************************************************/
4302 /* Subwindows are curious in that you have to physically unmap them to
4303    not display them. It is problematic deciding what to do in
4304    redisplay. We have two caches - a per-window instance cache that
4305    keeps track of subwindows on a window, these are linked to their
4306    instantiator in the hashtable and when the instantiator goes away
4307    we want the instance to go away also. However we also have a
4308    per-frame instance cache that we use to determine if a subwindow is
4309    obscuring an area that we want to clear. We need to be able to flip
4310    through this quickly so a hashtable is not suitable hence the
4311    subwindow_cachels. This is a weak list so unreference instances
4312    will get deleted properly. */
4313
4314 /* redisplay in general assumes that drawing something will erase
4315    what was there before. unfortunately this does not apply to
4316    subwindows that need to be specifically unmapped in order to
4317    disappear. we take a brute force approach - on the basis that its
4318    cheap - and unmap all subwindows in a display line */
4319
4320 /* Put new instances in the frame subwindow cache. This is less costly than
4321    doing it every time something gets mapped, and deleted instances will be
4322    removed automatically. */
4323 static void
4324 cache_subwindow_instance_in_frame_maybe (Lisp_Object instance)
4325 {
4326   Lisp_Image_Instance* ii = XIMAGE_INSTANCE (instance);
4327   if (!NILP (DOMAIN_FRAME (IMAGE_INSTANCE_DOMAIN (ii))))
4328     {
4329       struct frame* f = DOMAIN_XFRAME (IMAGE_INSTANCE_DOMAIN (ii));
4330       XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f))
4331         = Fcons (instance, XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f)));
4332     }
4333 }
4334
4335 /* Unmap and finalize all subwindow instances in the frame cache. This
4336    is necessary because GC will not guarantee the order things get
4337    deleted in and moreover, frame finalization deletes the window
4338    system windows before deleting XEmacs windows, and hence
4339    subwindows.  */
4340 int
4341 unmap_subwindow_instance_cache_mapper (Lisp_Object key, Lisp_Object value,
4342                                        void* finalize)
4343 {
4344   /* value can be nil; we cache failures as well as successes */
4345   if (!NILP (value))
4346     {
4347       struct frame* f = XFRAME (XIMAGE_INSTANCE_FRAME (value));
4348       unmap_subwindow (value);
4349       if (finalize)
4350         {
4351           /* In case GC doesn't catch up fast enough, remove from the frame
4352              cache also. Otherwise code that checks the sanity of the instance
4353              will fail. */
4354           XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f))
4355             = delq_no_quit (value,
4356                             XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f)));
4357           finalize_image_instance (XIMAGE_INSTANCE (value), 0);
4358         }
4359     }
4360   return 0;
4361 }
4362
4363 static void
4364 finalize_all_subwindow_instances (struct window *w)
4365 {
4366   if (!NILP (w->next))   finalize_all_subwindow_instances (XWINDOW (w->next));
4367   if (!NILP (w->vchild)) finalize_all_subwindow_instances (XWINDOW (w->vchild));
4368   if (!NILP (w->hchild)) finalize_all_subwindow_instances (XWINDOW (w->hchild));
4369
4370   elisp_maphash (unmap_subwindow_instance_cache_mapper,
4371                  w->subwindow_instance_cache, (void*)1);
4372 }
4373
4374 void
4375 free_frame_subwindow_instances (struct frame* f)
4376 {
4377   /* Make sure all instances are finalized. We have to do this via the
4378      instance cache since some instances may be extant but not
4379      displayed (and hence not in the frame cache). */
4380   finalize_all_subwindow_instances (XWINDOW (f->root_window));
4381 }
4382
4383 /* Unmap all instances in the frame cache. */
4384 void
4385 reset_frame_subwindow_instance_cache (struct frame* f)
4386 {
4387   Lisp_Object rest;
4388
4389   LIST_LOOP (rest, XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f)))
4390     {
4391       Lisp_Object value = XCAR (rest);
4392       unmap_subwindow (value);
4393     }
4394 }
4395
4396 /*****************************************************************************
4397  *                              subwindow exposure ignorance                    *
4398  *****************************************************************************/
4399 /* when we unmap subwindows the associated window system will generate
4400    expose events. This we do not want as redisplay already copes with
4401    the repainting necessary. Worse, we can get in an endless cycle of
4402    redisplay if we are not careful. Thus we keep a per-frame list of
4403    expose events that are going to come and ignore them as
4404    required. */
4405
4406 struct expose_ignore_blocktype
4407 {
4408   Blocktype_declare (struct expose_ignore);
4409 } *the_expose_ignore_blocktype;
4410
4411 int
4412 check_for_ignored_expose (struct frame* f, int x, int y, int width, int height)
4413 {
4414   struct expose_ignore *ei, *prev;
4415   /* the ignore list is FIFO so we should generally get a match with
4416      the first element in the list */
4417   for (ei = f->subwindow_exposures, prev = 0; ei; ei = ei->next)
4418     {
4419       /* Checking for exact matches just isn't good enough as we
4420          might get exposures for partially obscured subwindows, thus
4421          we have to check for overlaps. Being conservative, we will
4422          check for exposures wholly contained by the subwindow - this
4423          might give us what we want.*/
4424       if (ei->x <= x && ei->y <= y
4425           && ei->x + ei->width >= x + width
4426           && ei->y + ei->height >= y + height)
4427         {
4428 #ifdef DEBUG_WIDGETS
4429           stderr_out ("ignored %d+%d, %dx%d for exposure %d+%d, %dx%d\n",
4430                       x, y, width, height, ei->x, ei->y, ei->width, ei->height);
4431 #endif
4432           if (!prev)
4433             f->subwindow_exposures = ei->next;
4434           else
4435             prev->next = ei->next;
4436
4437           if (ei == f->subwindow_exposures_tail)
4438             f->subwindow_exposures_tail = prev;
4439
4440           Blocktype_free (the_expose_ignore_blocktype, ei);
4441           return 1;
4442         }
4443       prev = ei;
4444     }
4445   return 0;
4446 }
4447
4448 static void
4449 register_ignored_expose (struct frame* f, int x, int y, int width, int height)
4450 {
4451   if (!hold_ignored_expose_registration)
4452     {
4453       struct expose_ignore *ei;
4454
4455       ei = Blocktype_alloc (the_expose_ignore_blocktype);
4456
4457       ei->next = NULL;
4458       ei->x = x;
4459       ei->y = y;
4460       ei->width = width;
4461       ei->height = height;
4462
4463       /* we have to add the exposure to the end of the list, since we
4464          want to check the oldest events first. for speed we keep a record
4465          of the end so that we can add right to it. */
4466       if (f->subwindow_exposures_tail)
4467         {
4468           f->subwindow_exposures_tail->next = ei;
4469         }
4470       if (!f->subwindow_exposures)
4471         {
4472           f->subwindow_exposures = ei;
4473         }
4474       f->subwindow_exposures_tail = ei;
4475     }
4476 }
4477
4478 /****************************************************************************
4479  find_matching_subwindow
4480
4481  See if there is a subwindow that completely encloses the requested
4482  area.
4483  ****************************************************************************/
4484 int find_matching_subwindow (struct frame* f, int x, int y, int width, int height)
4485 {
4486   Lisp_Object rest;
4487
4488   LIST_LOOP (rest, XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f)))
4489     {
4490       Lisp_Image_Instance *ii = XIMAGE_INSTANCE (XCAR (rest));
4491
4492       if (IMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (ii)
4493           &&
4494           IMAGE_INSTANCE_DISPLAY_X (ii) <= x
4495           &&
4496           IMAGE_INSTANCE_DISPLAY_Y (ii) <= y
4497           &&
4498           IMAGE_INSTANCE_DISPLAY_X (ii)
4499           + IMAGE_INSTANCE_DISPLAY_WIDTH (ii) >= x + width
4500           &&
4501           IMAGE_INSTANCE_DISPLAY_Y (ii)
4502           + IMAGE_INSTANCE_DISPLAY_HEIGHT (ii) >= y + height)
4503         {
4504           return 1;
4505         }
4506     }
4507   return 0;
4508 }
4509
4510 \f
4511 /*****************************************************************************
4512  *                              subwindow functions                          *
4513  *****************************************************************************/
4514
4515 /* Update the displayed characteristics of a subwindow. This function
4516    should generally only get called if the subwindow is actually
4517    dirty. */
4518 void
4519 redisplay_subwindow (Lisp_Object subwindow)
4520 {
4521   Lisp_Image_Instance* ii = XIMAGE_INSTANCE (subwindow);
4522   int count = specpdl_depth ();
4523
4524   /* The update method is allowed to call eval.  Since it is quite
4525      common for this function to get called from somewhere in
4526      redisplay we need to make sure that quits are ignored.  Otherwise
4527      Fsignal will abort. */
4528   specbind (Qinhibit_quit, Qt);
4529
4530   ERROR_CHECK_IMAGE_INSTANCE (subwindow);
4531
4532   if (WIDGET_IMAGE_INSTANCEP (subwindow))
4533     {
4534       if (image_instance_changed (subwindow))
4535         redisplay_widget (subwindow);
4536       /* Reset the changed flags. */
4537       IMAGE_INSTANCE_WIDGET_FACE_CHANGED (ii) = 0;
4538       IMAGE_INSTANCE_WIDGET_ITEMS_CHANGED (ii) = 0;
4539       IMAGE_INSTANCE_WIDGET_ACTION_OCCURRED (ii) = 0;
4540       IMAGE_INSTANCE_TEXT_CHANGED (ii) = 0;
4541     }
4542   else if (IMAGE_INSTANCE_TYPE (ii) == IMAGE_SUBWINDOW
4543            &&
4544            !NILP (IMAGE_INSTANCE_FRAME (ii)))
4545     {
4546       MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
4547                      redisplay_subwindow, (ii));
4548     }
4549
4550   IMAGE_INSTANCE_SIZE_CHANGED (ii) = 0;
4551   /* This function is typically called by redisplay just before
4552      outputting the information to the screen. Thus we record a hash
4553      of the output to determine whether on-screen is the same as
4554      recorded structure. This approach has limitations in there is a
4555      good chance that hash values will be different for the same
4556      visual appearance. However, we would rather that then the other
4557      way round - it simply means that we will get more displays than
4558      we might need. We can get better hashing by making the depth
4559      negative - currently it will recurse down 7 levels.*/
4560   IMAGE_INSTANCE_DISPLAY_HASH (ii) = internal_hash (subwindow,
4561                                                     IMAGE_INSTANCE_HASH_DEPTH);
4562
4563   unbind_to (count, Qnil);
4564 }
4565
4566 /* Determine whether an image_instance has changed structurally and
4567    hence needs redisplaying in some way.
4568
4569    #### This should just look at the instantiator differences when we
4570    get rid of the stored items altogether. In fact we should probably
4571    store the new instantiator as well as the old - as we do with
4572    gui_items currently - and then pick-up the new on the next
4573    redisplay. This would obviate the need for any of this trickery
4574    with hashcodes. */
4575 int
4576 image_instance_changed (Lisp_Object subwindow)
4577 {
4578   Lisp_Image_Instance* ii = XIMAGE_INSTANCE (subwindow);
4579
4580   if (internal_hash (subwindow, IMAGE_INSTANCE_HASH_DEPTH) !=
4581       IMAGE_INSTANCE_DISPLAY_HASH (ii))
4582     return 1;
4583   /* #### I think there is probably a bug here. This gets called for
4584      layouts - and yet the pending items are always nil for
4585      layouts. We are saved by layout optimization, but I'm undecided
4586      as to what the correct fix is. */
4587   else if (WIDGET_IMAGE_INSTANCEP (subwindow)
4588            && (!internal_equal (IMAGE_INSTANCE_WIDGET_ITEMS (ii),
4589                                 IMAGE_INSTANCE_WIDGET_PENDING_ITEMS (ii), 0)
4590                || !NILP (IMAGE_INSTANCE_LAYOUT_CHILDREN (ii))
4591                || IMAGE_INSTANCE_WIDGET_ACTION_OCCURRED (ii)))
4592     return 1;
4593   else
4594     return 0;
4595 }
4596
4597 /* Update all the subwindows on a frame. */
4598 void
4599 update_widget_instances (Lisp_Object frame)
4600 {
4601   struct frame* f;
4602   Lisp_Object rest;
4603
4604   /* Its possible for the preceding callback to have deleted the
4605      frame, so cope with this. */
4606   if (!FRAMEP (frame) || !FRAME_LIVE_P (XFRAME (frame)))
4607     return;
4608
4609   CHECK_FRAME (frame);
4610   f = XFRAME (frame);
4611
4612   /* If we get called we know something has changed. */
4613   LIST_LOOP (rest, XWEAK_LIST_LIST (FRAME_SUBWINDOW_CACHE (f)))
4614     {
4615       Lisp_Object widget = XCAR (rest);
4616
4617       if (XIMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (widget)
4618           &&
4619           image_instance_changed (widget))
4620         {
4621           set_image_instance_dirty_p (widget, 1);
4622           MARK_FRAME_GLYPHS_CHANGED (f);
4623         }
4624     }
4625 }
4626
4627 /* remove a subwindow from its frame */
4628 void unmap_subwindow (Lisp_Object subwindow)
4629 {
4630   Lisp_Image_Instance* ii = XIMAGE_INSTANCE (subwindow);
4631   struct frame* f;
4632
4633   ERROR_CHECK_IMAGE_INSTANCE (subwindow);
4634
4635   if (!image_instance_type_to_mask (IMAGE_INSTANCE_TYPE (ii))
4636       & (IMAGE_WIDGET_MASK | IMAGE_SUBWINDOW_MASK)
4637       ||
4638       !IMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (ii))
4639     return;
4640
4641 #ifdef DEBUG_WIDGETS
4642   stderr_out ("unmapping subwindow %p\n", IMAGE_INSTANCE_SUBWINDOW_ID (ii));
4643 #endif
4644   f = XFRAME (IMAGE_INSTANCE_FRAME (ii));
4645
4646   /* make sure we don't get expose events */
4647   register_ignored_expose (f, IMAGE_INSTANCE_DISPLAY_X (ii),
4648                            IMAGE_INSTANCE_DISPLAY_Y (ii),
4649                            IMAGE_INSTANCE_DISPLAY_WIDTH (ii),
4650                            IMAGE_INSTANCE_DISPLAY_HEIGHT (ii));
4651   IMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (ii) = 0;
4652
4653   MAYBE_DEVMETH (XDEVICE (IMAGE_INSTANCE_DEVICE (ii)),
4654                  unmap_subwindow, (ii));
4655 }
4656
4657 /* show a subwindow in its frame */
4658 void map_subwindow (Lisp_Object subwindow, int x, int y,
4659                     struct display_glyph_area *dga)
4660 {
4661   Lisp_Image_Instance* ii = XIMAGE_INSTANCE (subwindow);
4662   struct frame* f;
4663
4664   ERROR_CHECK_IMAGE_INSTANCE (subwindow);
4665
4666   if (!image_instance_type_to_mask (IMAGE_INSTANCE_TYPE (ii))
4667       & (IMAGE_WIDGET_MASK | IMAGE_SUBWINDOW_MASK))
4668     return;
4669
4670 #ifdef DEBUG_WIDGETS
4671   stderr_out ("mapping subwindow %p, %dx%d@%d+%d\n",
4672               IMAGE_INSTANCE_SUBWINDOW_ID (ii),
4673               dga->width, dga->height, x, y);
4674 #endif
4675   f = XFRAME (IMAGE_INSTANCE_FRAME (ii));
4676   IMAGE_INSTANCE_DISPLAY_X (ii) = x;
4677   IMAGE_INSTANCE_DISPLAY_Y (ii) = y;
4678   IMAGE_INSTANCE_DISPLAY_WIDTH (ii) = dga->width;
4679   IMAGE_INSTANCE_DISPLAY_HEIGHT (ii) = dga->height;
4680
4681   MAYBE_DEVMETH (DOMAIN_XDEVICE (ii->domain),
4682                  map_subwindow, (ii, x, y, dga));
4683   IMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (ii) = 1;
4684 }
4685
4686 static int
4687 subwindow_possible_dest_types (void)
4688 {
4689   return IMAGE_SUBWINDOW_MASK;
4690 }
4691
4692 int
4693 subwindow_governing_domain (void)
4694 {
4695   return GOVERNING_DOMAIN_WINDOW;
4696 }
4697
4698 /* Partially instantiate a subwindow. */
4699 void
4700 subwindow_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
4701                        Lisp_Object pointer_fg, Lisp_Object pointer_bg,
4702                        int dest_mask, Lisp_Object domain)
4703 {
4704   Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
4705   Lisp_Object device = image_instance_device (image_instance);
4706   Lisp_Object frame = DOMAIN_FRAME (domain);
4707   Lisp_Object width = find_keyword_in_vector (instantiator, Q_pixel_width);
4708   Lisp_Object height = find_keyword_in_vector (instantiator, Q_pixel_height);
4709
4710   if (NILP (frame))
4711     signal_simple_error ("No selected frame", device);
4712
4713   if (!(dest_mask & IMAGE_SUBWINDOW_MASK))
4714     incompatible_image_types (instantiator, dest_mask, IMAGE_SUBWINDOW_MASK);
4715
4716   ii->data = 0;
4717   IMAGE_INSTANCE_SUBWINDOW_ID (ii) = 0;
4718   IMAGE_INSTANCE_SUBWINDOW_DISPLAYEDP (ii) = 0;
4719
4720   if (INTP (width))
4721     {
4722       int w = 1;
4723       if (XINT (width) > 1)
4724         w = XINT (width);
4725       IMAGE_INSTANCE_WIDTH (ii) = w;
4726       IMAGE_INSTANCE_SUBWINDOW_H_RESIZEP (ii) = 0;
4727     }
4728
4729   if (INTP (height))
4730     {
4731       int h = 1;
4732       if (XINT (height) > 1)
4733         h = XINT (height);
4734       IMAGE_INSTANCE_HEIGHT (ii) = h;
4735       IMAGE_INSTANCE_SUBWINDOW_V_RESIZEP (ii) = 0;
4736     }
4737 }
4738
4739 /* This is just a backup in case no-one has assigned a suitable geometry.
4740    #### It should really query the enclose window for geometry. */
4741 static void
4742 subwindow_query_geometry (Lisp_Object image_instance, int* width,
4743                           int* height, enum image_instance_geometry disp,
4744                           Lisp_Object domain)
4745 {
4746   if (width)    *width = 20;
4747   if (height)   *height = 20;
4748 }
4749
4750 DEFUN ("subwindowp", Fsubwindowp, 1, 1, 0, /*
4751 Return non-nil if OBJECT is a subwindow.
4752 */
4753        (object))
4754 {
4755   CHECK_IMAGE_INSTANCE (object);
4756   return (XIMAGE_INSTANCE_TYPE (object) == IMAGE_SUBWINDOW) ? Qt : Qnil;
4757 }
4758
4759 DEFUN ("image-instance-subwindow-id", Fimage_instance_subwindow_id, 1, 1, 0, /*
4760 Return the window id of SUBWINDOW as a number.
4761 */
4762        (subwindow))
4763 {
4764   CHECK_SUBWINDOW_IMAGE_INSTANCE (subwindow);
4765   return make_int ((EMACS_INT) XIMAGE_INSTANCE_SUBWINDOW_ID (subwindow));
4766 }
4767
4768 DEFUN ("resize-subwindow", Fresize_subwindow, 1, 3, 0, /*
4769 Resize SUBWINDOW to WIDTH x HEIGHT.
4770 If a value is nil that parameter is not changed.
4771 */
4772        (subwindow, width, height))
4773 {
4774   int neww, newh;
4775   Lisp_Image_Instance* ii;
4776
4777   CHECK_SUBWINDOW_IMAGE_INSTANCE (subwindow);
4778   ii = XIMAGE_INSTANCE (subwindow);
4779
4780   if (NILP (width))
4781     neww = IMAGE_INSTANCE_WIDTH (ii);
4782   else
4783     neww = XINT (width);
4784
4785   if (NILP (height))
4786     newh = IMAGE_INSTANCE_HEIGHT (ii);
4787   else
4788     newh = XINT (height);
4789
4790   /* The actual resizing gets done asynchronously by
4791      update_subwindow. */
4792   IMAGE_INSTANCE_HEIGHT (ii) = newh;
4793   IMAGE_INSTANCE_WIDTH (ii) = neww;
4794   IMAGE_INSTANCE_SIZE_CHANGED (ii) = 1;
4795
4796   return subwindow;
4797 }
4798
4799 DEFUN ("force-subwindow-map", Fforce_subwindow_map, 1, 1, 0, /*
4800 Generate a Map event for SUBWINDOW.
4801 */
4802        (subwindow))
4803 {
4804   CHECK_SUBWINDOW_IMAGE_INSTANCE (subwindow);
4805 #if 0
4806   map_subwindow (subwindow, 0, 0);
4807 #endif
4808   return subwindow;
4809 }
4810
4811 \f
4812 /*****************************************************************************
4813  *                              display tables                               *
4814  *****************************************************************************/
4815
4816 /* Get the display tables for use currently on window W with face
4817    FACE.  #### This will have to be redone.  */
4818
4819 void
4820 get_display_tables (struct window *w, face_index findex,
4821                     Lisp_Object *face_table, Lisp_Object *window_table)
4822 {
4823   Lisp_Object tem;
4824   tem = WINDOW_FACE_CACHEL_DISPLAY_TABLE (w, findex);
4825   if (UNBOUNDP (tem))
4826     tem = Qnil;
4827   if (!LISTP (tem))
4828     tem = noseeum_cons (tem, Qnil);
4829   *face_table = tem;
4830   tem = w->display_table;
4831   if (UNBOUNDP (tem))
4832     tem = Qnil;
4833   if (!LISTP (tem))
4834     tem = noseeum_cons (tem, Qnil);
4835   *window_table = tem;
4836 }
4837
4838 Lisp_Object
4839 display_table_entry (Emchar ch, Lisp_Object face_table,
4840                      Lisp_Object window_table)
4841 {
4842   Lisp_Object tail;
4843
4844   /* Loop over FACE_TABLE, and then over WINDOW_TABLE. */
4845   for (tail = face_table; 1; tail = XCDR (tail))
4846     {
4847       Lisp_Object table;
4848       if (NILP (tail))
4849         {
4850           if (!NILP (window_table))
4851             {
4852               tail = window_table;
4853               window_table = Qnil;
4854             }
4855           else
4856             return Qnil;
4857         }
4858       table = XCAR (tail);
4859
4860       if (VECTORP (table))
4861         {
4862           if (ch < XVECTOR_LENGTH (table) && !NILP (XVECTOR_DATA (table)[ch]))
4863             return XVECTOR_DATA (table)[ch];
4864           else
4865             continue;
4866         }
4867       else if (CHAR_TABLEP (table)
4868                && XCHAR_TABLE_TYPE (table) == CHAR_TABLE_TYPE_CHAR)
4869         {
4870           return get_char_table (ch, XCHAR_TABLE (table));
4871         }
4872       else if (CHAR_TABLEP (table)
4873                && XCHAR_TABLE_TYPE (table) == CHAR_TABLE_TYPE_GENERIC)
4874         {
4875           Lisp_Object gotit = get_char_table (ch, XCHAR_TABLE (table));
4876           if (!NILP (gotit))
4877             return gotit;
4878           else
4879             continue;
4880         }
4881       else if (RANGE_TABLEP (table))
4882         {
4883           Lisp_Object gotit = Fget_range_table (make_char (ch), table, Qnil);
4884           if (!NILP (gotit))
4885             return gotit;
4886           else
4887             continue;
4888         }
4889       else
4890         abort ();
4891     }
4892 }
4893
4894 /*****************************************************************************
4895  *                              timeouts for animated glyphs                      *
4896  *****************************************************************************/
4897 static Lisp_Object Qglyph_animated_timeout_handler;
4898
4899 DEFUN ("glyph-animated-timeout-handler", Fglyph_animated_timeout_handler, 1, 1, 0, /*
4900 Callback function for updating animated images.
4901 Don't use this.
4902 */
4903        (arg))
4904 {
4905   CHECK_WEAK_LIST (arg);
4906
4907   if (!NILP (XWEAK_LIST_LIST (arg)) && !NILP (XCAR (XWEAK_LIST_LIST (arg))))
4908     {
4909       Lisp_Object value = XCAR (XWEAK_LIST_LIST (arg));
4910
4911       if (IMAGE_INSTANCEP (value))
4912         {
4913           Lisp_Image_Instance* ii = XIMAGE_INSTANCE (value);
4914
4915           if (COLOR_PIXMAP_IMAGE_INSTANCEP (value)
4916               &&
4917               IMAGE_INSTANCE_PIXMAP_MAXSLICE (ii) > 1
4918               &&
4919               !disable_animated_pixmaps)
4920             {
4921               /* Increment the index of the image slice we are currently
4922                  viewing. */
4923               IMAGE_INSTANCE_PIXMAP_SLICE (ii) =
4924                 (IMAGE_INSTANCE_PIXMAP_SLICE (ii) + 1)
4925                 % IMAGE_INSTANCE_PIXMAP_MAXSLICE (ii);
4926               /* We might need to kick redisplay at this point - but we
4927                  also might not. */
4928               MARK_DEVICE_FRAMES_GLYPHS_CHANGED
4929                 (XDEVICE (image_instance_device (value)));
4930               /* Cascade dirtiness so that we can have an animated glyph in a layout
4931                  for instance. */
4932               set_image_instance_dirty_p (value, 1);
4933             }
4934         }
4935     }
4936   return Qnil;
4937 }
4938
4939 Lisp_Object add_glyph_animated_timeout (EMACS_INT tickms, Lisp_Object image)
4940 {
4941   Lisp_Object ret = Qnil;
4942
4943   if (tickms > 0 && IMAGE_INSTANCEP (image))
4944     {
4945       double ms = ((double)tickms) / 1000.0;
4946       struct gcpro gcpro1;
4947       Lisp_Object holder = make_weak_list (WEAK_LIST_SIMPLE);
4948
4949       GCPRO1 (holder);
4950       XWEAK_LIST_LIST (holder) = Fcons (image, Qnil);
4951
4952       ret = Fadd_timeout (make_float (ms),
4953                           Qglyph_animated_timeout_handler,
4954                           holder, make_float (ms));
4955
4956       UNGCPRO;
4957     }
4958   return ret;
4959 }
4960
4961 void disable_glyph_animated_timeout (int i)
4962 {
4963   Lisp_Object id;
4964   XSETINT (id, i);
4965
4966   Fdisable_timeout (id);
4967 }
4968
4969 \f
4970 /*****************************************************************************
4971  *                              initialization                               *
4972  *****************************************************************************/
4973
4974 void
4975 syms_of_glyphs (void)
4976 {
4977   INIT_LRECORD_IMPLEMENTATION (glyph);
4978   INIT_LRECORD_IMPLEMENTATION (image_instance);
4979
4980   /* image instantiators */
4981
4982   DEFSUBR (Fimage_instantiator_format_list);
4983   DEFSUBR (Fvalid_image_instantiator_format_p);
4984   DEFSUBR (Fset_console_type_image_conversion_list);
4985   DEFSUBR (Fconsole_type_image_conversion_list);
4986
4987   DEFKEYWORD (Q_file);
4988   DEFKEYWORD (Q_data);
4989   DEFKEYWORD (Q_face);
4990   DEFKEYWORD (Q_pixel_height);
4991   DEFKEYWORD (Q_pixel_width);
4992
4993 #ifdef HAVE_XPM
4994   DEFKEYWORD (Q_color_symbols);
4995 #endif
4996 #ifdef HAVE_WINDOW_SYSTEM
4997   DEFKEYWORD (Q_mask_file);
4998   DEFKEYWORD (Q_mask_data);
4999   DEFKEYWORD (Q_hotspot_x);
5000   DEFKEYWORD (Q_hotspot_y);
5001   DEFKEYWORD (Q_foreground);
5002   DEFKEYWORD (Q_background);
5003 #endif
5004   /* image specifiers */
5005
5006   DEFSUBR (Fimage_specifier_p);
5007   /* Qimage in general.c */
5008
5009   /* image instances */
5010
5011   defsymbol (&Qimage_instancep, "image-instance-p");
5012
5013   DEFSYMBOL (Qnothing_image_instance_p);
5014   DEFSYMBOL (Qtext_image_instance_p);
5015   DEFSYMBOL (Qmono_pixmap_image_instance_p);
5016   DEFSYMBOL (Qcolor_pixmap_image_instance_p);
5017   DEFSYMBOL (Qpointer_image_instance_p);
5018   DEFSYMBOL (Qwidget_image_instance_p);
5019   DEFSYMBOL (Qsubwindow_image_instance_p);
5020
5021   DEFSUBR (Fmake_image_instance);
5022   DEFSUBR (Fimage_instance_p);
5023   DEFSUBR (Fimage_instance_type);
5024   DEFSUBR (Fvalid_image_instance_type_p);
5025   DEFSUBR (Fimage_instance_type_list);
5026   DEFSUBR (Fimage_instance_name);
5027   DEFSUBR (Fimage_instance_domain);
5028   DEFSUBR (Fimage_instance_string);
5029   DEFSUBR (Fimage_instance_file_name);
5030   DEFSUBR (Fimage_instance_mask_file_name);
5031   DEFSUBR (Fimage_instance_depth);
5032   DEFSUBR (Fimage_instance_height);
5033   DEFSUBR (Fimage_instance_width);
5034   DEFSUBR (Fimage_instance_hotspot_x);
5035   DEFSUBR (Fimage_instance_hotspot_y);
5036   DEFSUBR (Fimage_instance_foreground);
5037   DEFSUBR (Fimage_instance_background);
5038   DEFSUBR (Fimage_instance_property);
5039   DEFSUBR (Fcolorize_image_instance);
5040   /* subwindows */
5041   DEFSUBR (Fsubwindowp);
5042   DEFSUBR (Fimage_instance_subwindow_id);
5043   DEFSUBR (Fresize_subwindow);
5044   DEFSUBR (Fforce_subwindow_map);
5045
5046   /* Qnothing defined as part of the "nothing" image-instantiator
5047      type. */
5048   /* Qtext defined in general.c */
5049   DEFSYMBOL (Qmono_pixmap);
5050   DEFSYMBOL (Qcolor_pixmap);
5051   /* Qpointer defined in general.c */
5052
5053   /* glyphs */
5054
5055   DEFSYMBOL (Qglyphp);
5056   DEFSYMBOL (Qcontrib_p);
5057   DEFSYMBOL (Qbaseline);
5058
5059   DEFSYMBOL (Qbuffer_glyph_p);
5060   DEFSYMBOL (Qpointer_glyph_p);
5061   DEFSYMBOL (Qicon_glyph_p);
5062
5063   DEFSYMBOL (Qconst_glyph_variable);
5064
5065   DEFSUBR (Fglyph_type);
5066   DEFSUBR (Fvalid_glyph_type_p);
5067   DEFSUBR (Fglyph_type_list);
5068   DEFSUBR (Fglyphp);
5069   DEFSUBR (Fmake_glyph_internal);
5070   DEFSUBR (Fglyph_width);
5071   DEFSUBR (Fglyph_ascent);
5072   DEFSUBR (Fglyph_descent);
5073   DEFSUBR (Fglyph_height);
5074   DEFSUBR (Fset_instantiator_property);
5075
5076   /* Qbuffer defined in general.c. */
5077   /* Qpointer defined above */
5078
5079   /* Unfortunately, timeout handlers must be lisp functions. This is
5080      for animated glyphs. */
5081   DEFSYMBOL (Qglyph_animated_timeout_handler);
5082   DEFSUBR (Fglyph_animated_timeout_handler);
5083
5084   /* Errors */
5085   DEFERROR_STANDARD (Qimage_conversion_error, Qio_error);
5086 }
5087
5088 static const struct lrecord_description image_specifier_description[] = {
5089   { XD_LISP_OBJECT, specifier_data_offset + offsetof (struct image_specifier, attachee) },
5090   { XD_LISP_OBJECT, specifier_data_offset + offsetof (struct image_specifier, attachee_property) },
5091   { XD_END }
5092 };
5093
5094 void
5095 specifier_type_create_image (void)
5096 {
5097   /* image specifiers */
5098
5099   INITIALIZE_SPECIFIER_TYPE_WITH_DATA (image, "image", "imagep");
5100
5101   SPECIFIER_HAS_METHOD (image, create);
5102   SPECIFIER_HAS_METHOD (image, mark);
5103   SPECIFIER_HAS_METHOD (image, instantiate);
5104   SPECIFIER_HAS_METHOD (image, validate);
5105   SPECIFIER_HAS_METHOD (image, after_change);
5106   SPECIFIER_HAS_METHOD (image, going_to_add);
5107   SPECIFIER_HAS_METHOD (image, copy_instantiator);
5108 }
5109
5110 void
5111 reinit_specifier_type_create_image (void)
5112 {
5113   REINITIALIZE_SPECIFIER_TYPE (image);
5114 }
5115
5116
5117 static const struct lrecord_description iike_description_1[] = {
5118   { XD_LISP_OBJECT, offsetof (ii_keyword_entry, keyword) },
5119   { XD_END }
5120 };
5121
5122 static const struct struct_description iike_description = {
5123   sizeof (ii_keyword_entry),
5124   iike_description_1
5125 };
5126
5127 static const struct lrecord_description iiked_description_1[] = {
5128   XD_DYNARR_DESC (ii_keyword_entry_dynarr, &iike_description),
5129   { XD_END }
5130 };
5131
5132 static const struct struct_description iiked_description = {
5133   sizeof (ii_keyword_entry_dynarr),
5134   iiked_description_1
5135 };
5136
5137 static const struct lrecord_description iife_description_1[] = {
5138   { XD_LISP_OBJECT, offsetof (image_instantiator_format_entry, symbol) },
5139   { XD_LISP_OBJECT, offsetof (image_instantiator_format_entry, device) },
5140   { XD_STRUCT_PTR,  offsetof (image_instantiator_format_entry, meths),  1, &iim_description },
5141   { XD_END }
5142 };
5143
5144 static const struct struct_description iife_description = {
5145   sizeof (image_instantiator_format_entry),
5146   iife_description_1
5147 };
5148
5149 static const struct lrecord_description iifed_description_1[] = {
5150   XD_DYNARR_DESC (image_instantiator_format_entry_dynarr, &iife_description),
5151   { XD_END }
5152 };
5153
5154 static const struct struct_description iifed_description = {
5155   sizeof (image_instantiator_format_entry_dynarr),
5156   iifed_description_1
5157 };
5158
5159 static const struct lrecord_description iim_description_1[] = {
5160   { XD_LISP_OBJECT, offsetof (struct image_instantiator_methods, symbol) },
5161   { XD_LISP_OBJECT, offsetof (struct image_instantiator_methods, device) },
5162   { XD_STRUCT_PTR,  offsetof (struct image_instantiator_methods, keywords), 1, &iiked_description },
5163   { XD_STRUCT_PTR,  offsetof (struct image_instantiator_methods, consoles), 1, &cted_description },
5164   { XD_END }
5165 };
5166
5167 const struct struct_description iim_description = {
5168   sizeof (struct image_instantiator_methods),
5169   iim_description_1
5170 };
5171
5172 void
5173 image_instantiator_format_create (void)
5174 {
5175   /* image instantiators */
5176
5177   the_image_instantiator_format_entry_dynarr =
5178     Dynarr_new (image_instantiator_format_entry);
5179
5180   Vimage_instantiator_format_list = Qnil;
5181   staticpro (&Vimage_instantiator_format_list);
5182
5183   dump_add_root_struct_ptr (&the_image_instantiator_format_entry_dynarr, &iifed_description);
5184
5185   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (nothing, "nothing");
5186
5187   IIFORMAT_HAS_METHOD (nothing, possible_dest_types);
5188   IIFORMAT_HAS_METHOD (nothing, instantiate);
5189
5190   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (inherit, "inherit");
5191
5192   IIFORMAT_HAS_METHOD (inherit, validate);
5193   IIFORMAT_HAS_METHOD (inherit, normalize);
5194   IIFORMAT_HAS_METHOD (inherit, possible_dest_types);
5195   IIFORMAT_HAS_METHOD (inherit, instantiate);
5196
5197   IIFORMAT_VALID_KEYWORD (inherit, Q_face, check_valid_face);
5198
5199   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (string, "string");
5200
5201   IIFORMAT_HAS_METHOD (string, validate);
5202   IIFORMAT_HAS_SHARED_METHOD (string, governing_domain, subwindow);
5203   IIFORMAT_HAS_METHOD (string, possible_dest_types);
5204   IIFORMAT_HAS_METHOD (string, instantiate);
5205
5206   IIFORMAT_VALID_KEYWORD (string, Q_data, check_valid_string);
5207   /* Do this so we can set strings. */
5208   /* #### Andy, what is this?  This is a bogus format and should not be
5209      visible to the user. */
5210   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (text, "text");
5211   IIFORMAT_HAS_METHOD (text, update);
5212   IIFORMAT_HAS_METHOD (text, query_geometry);
5213
5214   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (formatted_string, "formatted-string");
5215
5216   IIFORMAT_HAS_METHOD (formatted_string, validate);
5217   IIFORMAT_HAS_METHOD (formatted_string, possible_dest_types);
5218   IIFORMAT_HAS_METHOD (formatted_string, instantiate);
5219   IIFORMAT_VALID_KEYWORD (formatted_string, Q_data, check_valid_string);
5220
5221   /* Do this so pointers have geometry. */
5222   /* #### Andy, what is this?  This is a bogus format and should not be
5223      visible to the user. */
5224   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (pointer, "pointer");
5225   IIFORMAT_HAS_SHARED_METHOD (pointer, query_geometry, subwindow);
5226
5227   /* subwindows */
5228   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (subwindow, "subwindow");
5229   IIFORMAT_HAS_METHOD (subwindow, possible_dest_types);
5230   IIFORMAT_HAS_METHOD (subwindow, governing_domain);
5231   IIFORMAT_HAS_METHOD (subwindow, instantiate);
5232   IIFORMAT_HAS_METHOD (subwindow, query_geometry);
5233   IIFORMAT_VALID_KEYWORD (subwindow, Q_pixel_width, check_valid_int);
5234   IIFORMAT_VALID_KEYWORD (subwindow, Q_pixel_height, check_valid_int);
5235
5236 #ifdef HAVE_WINDOW_SYSTEM
5237   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (xbm, "xbm");
5238
5239   IIFORMAT_HAS_METHOD (xbm, validate);
5240   IIFORMAT_HAS_METHOD (xbm, normalize);
5241   IIFORMAT_HAS_METHOD (xbm, possible_dest_types);
5242
5243   IIFORMAT_VALID_KEYWORD (xbm, Q_data, check_valid_xbm_inline);
5244   IIFORMAT_VALID_KEYWORD (xbm, Q_file, check_valid_string);
5245   IIFORMAT_VALID_KEYWORD (xbm, Q_mask_data, check_valid_xbm_inline);
5246   IIFORMAT_VALID_KEYWORD (xbm, Q_mask_file, check_valid_string);
5247   IIFORMAT_VALID_KEYWORD (xbm, Q_hotspot_x, check_valid_int);
5248   IIFORMAT_VALID_KEYWORD (xbm, Q_hotspot_y, check_valid_int);
5249   IIFORMAT_VALID_KEYWORD (xbm, Q_foreground, check_valid_string);
5250   IIFORMAT_VALID_KEYWORD (xbm, Q_background, check_valid_string);
5251 #endif /* HAVE_WINDOW_SYSTEM */
5252
5253 #ifdef HAVE_XFACE
5254   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (xface, "xface");
5255
5256   IIFORMAT_HAS_METHOD (xface, validate);
5257   IIFORMAT_HAS_METHOD (xface, normalize);
5258   IIFORMAT_HAS_METHOD (xface, possible_dest_types);
5259
5260   IIFORMAT_VALID_KEYWORD (xface, Q_data, check_valid_string);
5261   IIFORMAT_VALID_KEYWORD (xface, Q_file, check_valid_string);
5262   IIFORMAT_VALID_KEYWORD (xface, Q_hotspot_x, check_valid_int);
5263   IIFORMAT_VALID_KEYWORD (xface, Q_hotspot_y, check_valid_int);
5264   IIFORMAT_VALID_KEYWORD (xface, Q_foreground, check_valid_string);
5265   IIFORMAT_VALID_KEYWORD (xface, Q_background, check_valid_string);
5266 #endif
5267
5268 #ifdef HAVE_XPM
5269   INITIALIZE_IMAGE_INSTANTIATOR_FORMAT (xpm, "xpm");
5270
5271   IIFORMAT_HAS_METHOD (xpm, validate);
5272   IIFORMAT_HAS_METHOD (xpm, normalize);
5273   IIFORMAT_HAS_METHOD (xpm, possible_dest_types);
5274
5275   IIFORMAT_VALID_KEYWORD (xpm, Q_data, check_valid_string);
5276   IIFORMAT_VALID_KEYWORD (xpm, Q_file, check_valid_string);
5277   IIFORMAT_VALID_KEYWORD (xpm, Q_color_symbols, check_valid_xpm_color_symbols);
5278 #endif /* HAVE_XPM */
5279 }
5280
5281 void
5282 reinit_vars_of_glyphs (void)
5283 {
5284   the_expose_ignore_blocktype =
5285     Blocktype_new (struct expose_ignore_blocktype);
5286
5287   hold_ignored_expose_registration = 0;
5288 }
5289
5290
5291 void
5292 vars_of_glyphs (void)
5293 {
5294   reinit_vars_of_glyphs ();
5295
5296   Vthe_nothing_vector = vector1 (Qnothing);
5297   staticpro (&Vthe_nothing_vector);
5298
5299   /* image instances */
5300
5301   Vimage_instance_type_list = Fcons (Qnothing,
5302                                      list6 (Qtext, Qmono_pixmap, Qcolor_pixmap,
5303                                             Qpointer, Qsubwindow, Qwidget));
5304   staticpro (&Vimage_instance_type_list);
5305
5306   /* glyphs */
5307
5308   Vglyph_type_list = list3 (Qbuffer, Qpointer, Qicon);
5309   staticpro (&Vglyph_type_list);
5310
5311   /* The octal-escape glyph, control-arrow-glyph and
5312      invisible-text-glyph are completely initialized in glyphs.el */
5313
5314   DEFVAR_LISP ("octal-escape-glyph", &Voctal_escape_glyph /*
5315 What to prefix character codes displayed in octal with.
5316 */);
5317   Voctal_escape_glyph = allocate_glyph (GLYPH_BUFFER, redisplay_glyph_changed);
5318
5319   DEFVAR_LISP ("control-arrow-glyph", &Vcontrol_arrow_glyph /*
5320 What to use as an arrow for control characters.
5321 */);
5322   Vcontrol_arrow_glyph = allocate_glyph (GLYPH_BUFFER,
5323                                          redisplay_glyph_changed);
5324
5325   DEFVAR_LISP ("invisible-text-glyph", &Vinvisible_text_glyph /*
5326 What to use to indicate the presence of invisible text.
5327 This is the glyph that is displayed when an ellipsis is called for
5328 \(see `selective-display-ellipses' and `buffer-invisibility-spec').
5329 Normally this is three dots ("...").
5330 */);
5331   Vinvisible_text_glyph = allocate_glyph (GLYPH_BUFFER,
5332                                           redisplay_glyph_changed);
5333
5334   /* Partially initialized in glyphs.el */
5335   DEFVAR_LISP ("hscroll-glyph", &Vhscroll_glyph /*
5336 What to display at the beginning of horizontally scrolled lines.
5337 */);
5338   Vhscroll_glyph = allocate_glyph (GLYPH_BUFFER, redisplay_glyph_changed);
5339 #ifdef HAVE_WINDOW_SYSTEM
5340   Fprovide (Qxbm);
5341 #endif
5342 #ifdef HAVE_XPM
5343   Fprovide (Qxpm);
5344
5345   DEFVAR_LISP ("xpm-color-symbols", &Vxpm_color_symbols /*
5346 Definitions of logical color-names used when reading XPM files.
5347 Elements of this list should be of the form (COLOR-NAME FORM-TO-EVALUATE).
5348 The COLOR-NAME should be a string, which is the name of the color to define;
5349 the FORM should evaluate to a `color' specifier object, or a string to be
5350 passed to `make-color-instance'.  If a loaded XPM file references a symbolic
5351 color called COLOR-NAME, it will display as the computed color instead.
5352
5353 The default value of this variable defines the logical color names
5354 \"foreground\" and \"background\" to be the colors of the `default' face.
5355 */ );
5356   Vxpm_color_symbols = Qnil; /* initialized in x-faces.el */
5357 #endif /* HAVE_XPM */
5358 #ifdef HAVE_XFACE
5359   Fprovide (Qxface);
5360 #endif
5361
5362   DEFVAR_BOOL ("disable-animated-pixmaps", &disable_animated_pixmaps /*
5363 Whether animated pixmaps should be animated.
5364 Default is t.
5365 */);
5366   disable_animated_pixmaps = 0;
5367 }
5368
5369 void
5370 specifier_vars_of_glyphs (void)
5371 {
5372   /* #### Can we GC here? The set_specifier_* calls definitely need */
5373   /* protection. */
5374   /* display tables */
5375
5376   DEFVAR_SPECIFIER ("current-display-table", &Vcurrent_display_table /*
5377 *The display table currently in use.
5378 This is a specifier; use `set-specifier' to change it.
5379
5380 Display tables are used to control how characters are displayed.  Each
5381 time that redisplay processes a character, it is looked up in all the
5382 display tables that apply (obtained by calling `specifier-instance' on
5383 `current-display-table' and any overriding display tables specified in
5384 currently active faces).  The first entry found that matches the
5385 character determines how the character is displayed.  If there is no
5386 matching entry, the default display method is used. (Non-control
5387 characters are displayed as themselves and control characters are
5388 displayed according to the buffer-local variable `ctl-arrow'.  Control
5389 characters are further affected by `control-arrow-glyph' and
5390 `octal-escape-glyph'.)
5391
5392 Each instantiator in this specifier and the display-table specifiers
5393 in faces is a display table or a list of such tables.  If a list, each
5394 table will be searched in turn for an entry matching a particular
5395 character.  Each display table is one of
5396
5397 -- a vector, specifying values for characters starting at 0
5398 -- a char table, either of type `char' or `generic'
5399 -- a range table
5400
5401 Each entry in a display table should be one of
5402
5403 -- nil (this entry is ignored and the search continues)
5404 -- a character (use this character; if it happens to be the same as
5405    the original character, default processing happens, otherwise
5406    redisplay attempts to display this character directly;
5407    #### At some point recursive display-table lookup will be
5408    implemented.)
5409 -- a string (display each character in the string directly;
5410    #### At some point recursive display-table lookup will be
5411    implemented.)
5412 -- a glyph (display the glyph;
5413    #### At some point recursive display-table lookup will be
5414    implemented when a string glyph is being processed.)
5415 -- a cons of the form (format "STRING") where STRING is a printf-like
5416    spec used to process the character. #### Unfortunately no
5417    formatting directives other than %% are implemented.
5418 -- a vector (each element of the vector is processed recursively;
5419    in such a case, nil elements in the vector are simply ignored)
5420
5421 #### At some point in the near future, display tables are likely to
5422 be expanded to include other features, such as referencing characters
5423 in particular fonts and allowing the character search to continue
5424 all the way up the chain of specifier instantiators.  These features
5425 are necessary to properly display Unicode characters.
5426 */ );
5427   Vcurrent_display_table = Fmake_specifier (Qdisplay_table);
5428   set_specifier_fallback (Vcurrent_display_table,
5429                           list1 (Fcons (Qnil, Qnil)));
5430   set_specifier_caching (Vcurrent_display_table,
5431                          offsetof (struct window, display_table),
5432                          some_window_value_changed,
5433                          0, 0, 0);
5434 }
5435
5436 void
5437 complex_vars_of_glyphs (void)
5438 {
5439   /* Partially initialized in glyphs-x.c, glyphs.el */
5440   DEFVAR_LISP ("truncation-glyph", &Vtruncation_glyph /*
5441 What to display at the end of truncated lines.
5442 */ );
5443   Vtruncation_glyph = allocate_glyph (GLYPH_BUFFER, redisplay_glyph_changed);
5444
5445   /* Partially initialized in glyphs-x.c, glyphs.el */
5446   DEFVAR_LISP ("continuation-glyph", &Vcontinuation_glyph /*
5447 What to display at the end of wrapped lines.
5448 */ );
5449   Vcontinuation_glyph = allocate_glyph (GLYPH_BUFFER, redisplay_glyph_changed);
5450
5451   /* Partially initialized in glyphs-x.c, glyphs.el */
5452   DEFVAR_LISP ("xemacs-logo", &Vxemacs_logo /*
5453 The glyph used to display the XEmacs logo at startup.
5454 */ );
5455   Vxemacs_logo = allocate_glyph (GLYPH_BUFFER, 0);
5456 }