1 /* Generic Objects and Functions.
2 Copyright (C) 1995 Free Software Foundation, Inc.
3 Copyright (C) 1995 Board of Trustees, University of Illinois.
4 Copyright (C) 1995, 1996 Ben Wing.
6 This file is part of SXEmacs
8 SXEmacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 SXEmacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Synched up with: Not in FSF. */
27 #include "ui/device.h"
31 #include "ui/objects.h"
32 #include "specifier.h"
33 #include "ui/window.h"
35 /* Objects that are substituted when an instantiation fails.
36 If we leave in the Qunbound value, we will probably get crashes. */
37 Lisp_Object Vthe_null_color_instance, Vthe_null_font_instance;
39 /* Authors: Ben Wing, Chuck Thompson */
41 void finalose(void *ptr)
47 ("Can't dump an emacs containing window system objects", obj);
50 /****************************************************************************
51 * Color-Instance Object *
52 ****************************************************************************/
54 Lisp_Object Qcolor_instancep;
56 static Lisp_Object mark_color_instance(Lisp_Object obj)
58 Lisp_Color_Instance *c = XCOLOR_INSTANCE(obj);
60 if (!NILP(c->device)) /* Vthe_null_color_instance */
61 MAYBE_DEVMETH(XDEVICE(c->device), mark_color_instance, (c));
67 print_color_instance(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
69 Lisp_Color_Instance *c = XCOLOR_INSTANCE(obj);
71 error("printing unreadable object #<color-instance 0x%x>",
73 write_c_string("#<color-instance ", printcharfun);
74 print_internal(c->name, printcharfun, 0);
75 write_c_string(" on ", printcharfun);
76 print_internal(c->device, printcharfun, 0);
77 if (!NILP(c->device)) /* Vthe_null_color_instance */
78 MAYBE_DEVMETH(XDEVICE(c->device), print_color_instance,
79 (c, printcharfun, escapeflag));
80 write_fmt_str(printcharfun, " 0x%x>", c->header.uid);
83 static void finalize_color_instance(void *header, int for_disksave)
85 Lisp_Color_Instance *c = (Lisp_Color_Instance *) header;
87 if (!NILP(c->device)) {
90 MAYBE_DEVMETH(XDEVICE(c->device), finalize_color_instance, (c));
94 static int color_instance_equal(Lisp_Object obj1, Lisp_Object obj2, int depth)
96 Lisp_Color_Instance *c1 = XCOLOR_INSTANCE(obj1);
97 Lisp_Color_Instance *c2 = XCOLOR_INSTANCE(obj2);
100 (EQ(c1->device, c2->device) &&
101 DEVICEP(c1->device) &&
102 HAS_DEVMETH_P(XDEVICE(c1->device), color_instance_equal) &&
103 DEVMETH(XDEVICE(c1->device), color_instance_equal,
107 static unsigned long color_instance_hash(Lisp_Object obj, int depth)
109 Lisp_Color_Instance *c = XCOLOR_INSTANCE(obj);
110 struct device *d = DEVICEP(c->device) ? XDEVICE(c->device) : 0;
112 return HASH2((unsigned long)d, !d ? LISP_HASH(obj)
113 : DEVMETH_OR_GIVEN(d, color_instance_hash, (c, depth),
117 DEFINE_LRECORD_IMPLEMENTATION("color-instance", color_instance,
118 mark_color_instance, print_color_instance,
119 finalize_color_instance, color_instance_equal,
120 color_instance_hash, 0, Lisp_Color_Instance);
122 DEFUN("make-color-instance", Fmake_color_instance, 1, 3, 0, /*
123 Return a new `color-instance' object named NAME (a string).
125 Optional argument DEVICE specifies the device this object applies to
126 and defaults to the selected device.
128 An error is signaled if the color is unknown or cannot be allocated;
129 however, if optional argument NOERROR is non-nil, nil is simply
130 returned in this case. (And if NOERROR is other than t, a warning may
133 The returned object is a normal, first-class lisp object. The way you
134 `deallocate' the color is the way you deallocate any other lisp object:
135 you drop all pointers to it and allow it to be garbage collected. When
136 these objects are GCed, the underlying window-system data (e.g. X object)
137 is deallocated as well.
139 (name, device, noerror))
141 Lisp_Color_Instance *c;
142 Lisp_Object val = Qnil;
143 Lisp_Object dev = Qnil;
145 int count = specpdl_depth();
146 struct gcpro gcpro1, gcpro2;
149 XSETDEVICE(dev, decode_device(device));
151 c = alloc_lcrecord_type(Lisp_Color_Instance, &lrecord_color_instance);
156 XSETCOLOR_INSTANCE(val, c);
158 retval = MAYBE_INT_DEVMETH(XDEVICE(dev), initialize_color_instance,
160 decode_error_behavior_flag(noerror)));
162 unbind_to(count, Qnil);
167 XSETCOLOR_INSTANCE(val, c);
168 unbind_to(count, Qnil);
173 DEFUN("color-instance-p", Fcolor_instance_p, 1, 1, 0, /*
174 Return non-nil if OBJECT is a color instance.
178 return COLOR_INSTANCEP(object) ? Qt : Qnil;
181 DEFUN("color-instance-name", Fcolor_instance_name, 1, 1, 0, /*
182 Return the name used to allocate COLOR-INSTANCE.
186 CHECK_COLOR_INSTANCE(color_instance);
187 return XCOLOR_INSTANCE(color_instance)->name;
190 DEFUN("color-instance-rgb-components", Fcolor_instance_rgb_components, 1, 1, 0, /*
191 Return a three element list containing the red, green, and blue
192 color components of COLOR-INSTANCE, or nil if unknown.
193 Component values range from 0 to 65535.
197 Lisp_Color_Instance *c;
199 CHECK_COLOR_INSTANCE(color_instance);
200 c = XCOLOR_INSTANCE(color_instance);
205 return MAYBE_LISP_DEVMETH(XDEVICE(c->device),
206 color_instance_rgb_components, (c));
209 DEFUN("valid-color-name-p", Fvalid_color_name_p, 1, 2, 0, /*
210 Return true if COLOR names a valid color for the current device.
212 Valid color names for X are listed in the file /usr/lib/X11/rgb.txt, or
213 whatever the equivalent is on your system.
215 Valid color names for TTY are those which have an ISO 6429 (ANSI) sequence.
216 In addition to being a color this may be one of a number of attributes
221 struct device *d = decode_device(device);
224 return MAYBE_INT_DEVMETH(d, valid_color_name_p, (d, color)) ? Qt : Qnil;
227 /***************************************************************************
228 * Font-Instance Object *
229 ***************************************************************************/
231 Lisp_Object Qfont_instancep;
233 static Lisp_Object font_instance_truename_internal(Lisp_Object xfont,
234 Error_behavior errb);
236 static Lisp_Object mark_font_instance(Lisp_Object obj)
238 Lisp_Font_Instance *f = XFONT_INSTANCE(obj);
240 mark_object(f->name);
241 if (!NILP(f->device)) /* Vthe_null_font_instance */
242 MAYBE_DEVMETH(XDEVICE(f->device), mark_font_instance, (f));
248 print_font_instance(Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
250 Lisp_Font_Instance *f = XFONT_INSTANCE(obj);
252 error("printing unreadable object #<font-instance 0x%x>",
254 write_c_string("#<font-instance ", printcharfun);
255 print_internal(f->name, printcharfun, 1);
256 write_c_string(" on ", printcharfun);
257 print_internal(f->device, printcharfun, 0);
258 if (!NILP(f->device))
259 MAYBE_DEVMETH(XDEVICE(f->device), print_font_instance,
260 (f, printcharfun, escapeflag));
261 write_fmt_str(printcharfun, " 0x%x>", f->header.uid);
264 static void finalize_font_instance(void *header, int for_disksave)
266 Lisp_Font_Instance *f = (Lisp_Font_Instance *) header;
268 if (!NILP(f->device)) {
271 MAYBE_DEVMETH(XDEVICE(f->device), finalize_font_instance, (f));
275 /* Fonts are equal if they resolve to the same name.
276 Since we call `font-truename' to do this, and since font-truename is lazy,
277 this means the `equal' could cause XListFonts to be run the first time.
279 static int font_instance_equal(Lisp_Object obj1, Lisp_Object obj2, int depth)
281 /* #### should this be moved into a device method? */
283 internal_equal(font_instance_truename_internal(obj1, ERROR_ME_NOT),
284 font_instance_truename_internal(obj2, ERROR_ME_NOT),
288 static unsigned long font_instance_hash(Lisp_Object obj, int depth)
290 return internal_hash(font_instance_truename_internal(obj, ERROR_ME_NOT),
294 DEFINE_LRECORD_IMPLEMENTATION("font-instance", font_instance,
295 mark_font_instance, print_font_instance,
296 finalize_font_instance, font_instance_equal,
297 font_instance_hash, 0, Lisp_Font_Instance);
299 DEFUN("make-font-instance", Fmake_font_instance, 1, 3, 0, /*
300 Return a new `font-instance' object named NAME.
301 DEVICE specifies the device this object applies to and defaults to the
302 selected device. An error is signalled if the font is unknown or cannot
303 be allocated; however, if NOERROR is non-nil, nil is simply returned in
306 The returned object is a normal, first-class lisp object. The way you
307 `deallocate' the font is the way you deallocate any other lisp object:
308 you drop all pointers to it and allow it to be garbage collected. When
309 these objects are GCed, the underlying X data is deallocated as well.
311 (name, device, noerror))
313 Lisp_Font_Instance *f;
316 Error_behavior errb = decode_error_behavior_flag(noerror);
318 if (ERRB_EQ(errb, ERROR_ME))
320 else if (!STRINGP(name))
323 XSETDEVICE(device, decode_device(device));
325 f = alloc_lcrecord_type(Lisp_Font_Instance, &lrecord_font_instance);
331 /* Stick some default values here ... */
332 f->ascent = f->height = 1;
335 f->proportional_p = 0;
337 retval = MAYBE_INT_DEVMETH(XDEVICE(device), initialize_font_instance,
338 (f, name, device, errb));
343 XSETFONT_INSTANCE(val, f);
347 DEFUN("font-instance-p", Ffont_instance_p, 1, 1, 0, /*
348 Return non-nil if OBJECT is a font instance.
352 return FONT_INSTANCEP(object) ? Qt : Qnil;
355 DEFUN("font-instance-name", Ffont_instance_name, 1, 1, 0, /*
356 Return the name used to allocate FONT-INSTANCE.
360 CHECK_FONT_INSTANCE(font_instance);
361 return XFONT_INSTANCE(font_instance)->name;
364 DEFUN("font-instance-ascent", Ffont_instance_ascent, 1, 1, 0, /*
365 Return the ascent in pixels of FONT-INSTANCE.
366 The returned value is the maximum ascent for all characters in the font,
367 where a character's ascent is the number of pixels above (and including)
372 CHECK_FONT_INSTANCE(font_instance);
373 return make_int(XFONT_INSTANCE(font_instance)->ascent);
376 DEFUN("font-instance-descent", Ffont_instance_descent, 1, 1, 0, /*
377 Return the descent in pixels of FONT-INSTANCE.
378 The returned value is the maximum descent for all characters in the font,
379 where a character's descent is the number of pixels below the baseline.
380 \(Many characters to do not have any descent. Typical characters with a
381 descent are lowercase p and lowercase g.)
385 CHECK_FONT_INSTANCE(font_instance);
386 return make_int(XFONT_INSTANCE(font_instance)->descent);
389 DEFUN("font-instance-width", Ffont_instance_width, 1, 1, 0, /*
390 Return the width in pixels of FONT-INSTANCE.
391 The returned value is the average width for all characters in the font.
395 CHECK_FONT_INSTANCE(font_instance);
396 return make_int(XFONT_INSTANCE(font_instance)->width);
399 DEFUN("font-instance-proportional-p", Ffont_instance_proportional_p, 1, 1, 0, /*
400 Return whether FONT-INSTANCE is proportional.
401 This means that different characters in the font have different widths.
405 CHECK_FONT_INSTANCE(font_instance);
406 return XFONT_INSTANCE(font_instance)->proportional_p ? Qt : Qnil;
410 font_instance_truename_internal(Lisp_Object font_instance, Error_behavior errb)
412 Lisp_Font_Instance *f = XFONT_INSTANCE(font_instance);
414 if (NILP(f->device)) {
415 maybe_signal_simple_error("Couldn't determine font truename",
416 font_instance, Qfont, errb);
420 return DEVMETH_OR_GIVEN(XDEVICE(f->device),
421 font_instance_truename, (f, errb), f->name);
424 DEFUN("font-instance-truename", Ffont_instance_truename, 1, 1, 0, /*
425 Return the canonical name of FONT-INSTANCE.
426 Font names are patterns which may match any number of fonts, of which
427 the first found is used. This returns an unambiguous name for that font
428 \(but not necessarily its only unambiguous name).
432 CHECK_FONT_INSTANCE(font_instance);
433 return font_instance_truename_internal(font_instance, ERROR_ME);
436 DEFUN("font-instance-properties", Ffont_instance_properties, 1, 1, 0, /*
437 Return the properties (an alist or nil) of FONT-INSTANCE.
441 Lisp_Font_Instance *f;
443 CHECK_FONT_INSTANCE(font_instance);
444 f = XFONT_INSTANCE(font_instance);
449 return MAYBE_LISP_DEVMETH(XDEVICE(f->device),
450 font_instance_properties, (f));
453 DEFUN("list-fonts", Flist_fonts, 1, 2, 0, /*
454 Return a list of font names matching the given pattern.
455 DEVICE specifies which device to search for names, and defaults to the
456 currently selected device.
460 CHECK_STRING(pattern);
461 XSETDEVICE(device, decode_device(device));
463 return MAYBE_LISP_DEVMETH(XDEVICE(device), list_fonts,
467 /****************************************************************************
469 ***************************************************************************/
470 DEFINE_SPECIFIER_TYPE(color);
471 /* Qcolor defined in general.c */
473 static void color_create(Lisp_Object obj)
475 Lisp_Specifier *color = XCOLOR_SPECIFIER(obj);
477 COLOR_SPECIFIER_FACE(color) = Qnil;
478 COLOR_SPECIFIER_FACE_PROPERTY(color) = Qnil;
481 static void color_mark(Lisp_Object obj)
483 Lisp_Specifier *color = XCOLOR_SPECIFIER(obj);
485 mark_object(COLOR_SPECIFIER_FACE(color));
486 mark_object(COLOR_SPECIFIER_FACE_PROPERTY(color));
489 /* No equal or hash methods; ignore the face the color is based off
493 color_instantiate(Lisp_Object specifier, Lisp_Object matchspec,
494 Lisp_Object domain, Lisp_Object instantiator,
497 /* When called, we're inside of call_with_suspended_errors(),
498 so we can freely error. */
499 Lisp_Object device = DOMAIN_DEVICE(domain);
500 struct device *d = XDEVICE(device);
502 if (COLOR_INSTANCEP(instantiator)) {
503 /* If we are on the same device then we're done. Otherwise change
504 the instantiator to the name used to generate the pixel and let the
505 STRINGP case deal with it. */
506 if (NILP(device) /* Vthe_null_color_instance */
507 ||EQ(device, XCOLOR_INSTANCE(instantiator)->device))
510 instantiator = Fcolor_instance_name(instantiator);
513 if (STRINGP(instantiator)) {
514 /* First, look to see if we can retrieve a cached value. */
515 Lisp_Object instance =
516 Fgethash(instantiator, d->color_instance_cache, Qunbound);
517 /* Otherwise, make a new one. */
518 if (UNBOUNDP(instance)) {
519 /* make sure we cache the failures, too. */
521 Fmake_color_instance(instantiator, device, Qt);
522 Fputhash(instantiator, instance,
523 d->color_instance_cache);
526 return NILP(instance) ? Qunbound : instance;
527 } else if (VECTORP(instantiator)) {
528 switch (XVECTOR_LENGTH(instantiator)) {
531 return Vthe_null_color_instance;
534 ("Color instantiator [] only valid on TTY's",
539 (COLOR_SPECIFIER_FACE(XCOLOR_SPECIFIER(specifier))))
541 ("Color specifier not attached to a face",
543 return (FACE_PROPERTY_INSTANCE_1
544 (Fget_face(XVECTOR_DATA(instantiator)[0]),
545 COLOR_SPECIFIER_FACE_PROPERTY(XCOLOR_SPECIFIER
547 domain, ERROR_ME, 0, depth));
550 return (FACE_PROPERTY_INSTANCE_1
551 (Fget_face(XVECTOR_DATA(instantiator)[0]),
552 XVECTOR_DATA(instantiator)[1], domain,
553 ERROR_ME, 0, depth));
558 } else if (NILP(instantiator)) {