462
|
1 /* X-specific Lisp objects.
|
|
2 Copyright (C) 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Board of Trustees, University of Illinois.
|
|
4 Copyright (C) 1995 Tinker Systems.
|
872
|
5 Copyright (C) 1995, 1996, 2002 Ben Wing.
|
462
|
6 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
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 /* Authors: Jamie Zawinski, Chuck Thompson, Ben Wing */
|
|
28 /* Gtk version by William Perry */
|
|
29
|
|
30 #include <config.h>
|
|
31 #include "lisp.h"
|
|
32
|
872
|
33 #include "buffer.h"
|
|
34 #include "device-impl.h"
|
|
35 #include "insdel.h"
|
462
|
36
|
872
|
37 #include "console-gtk-impl.h"
|
|
38 #include "objects-gtk-impl.h"
|
462
|
39
|
|
40 /* sigh */
|
|
41 #include <gdk/gdkx.h>
|
|
42
|
3659
|
43 /* XListFonts doesn't allocate memory unconditionally based on this. (For
|
|
44 XFree86 in 2005, at least. */
|
|
45 #define MAX_FONT_COUNT INT_MAX
|
|
46
|
|
47 #ifdef DEBUG_XEMACS
|
|
48 Fixnum debug_x_objects;
|
|
49 #endif /* DEBUG_XEMACS */
|
|
50
|
462
|
51
|
|
52 /************************************************************************/
|
|
53 /* color instances */
|
|
54 /************************************************************************/
|
|
55
|
|
56 /* Replacement for XAllocColor() that tries to return the nearest
|
|
57 available color if the colormap is full. Original was from FSFmacs,
|
|
58 but rewritten by Jareth Hein <jareth@camelot-soft.com> 97/11/25
|
|
59 Modified by Lee Kindness <lkindness@csl.co.uk> 31/08/99 to handle previous
|
|
60 total failure which was due to a read/write colorcell being the nearest
|
|
61 match - tries the next nearest...
|
|
62
|
|
63 Gdk takes care of all this behind the scenes, so we don't need to
|
|
64 worry about it.
|
|
65
|
|
66 Return value is 1 for normal success, 2 for nearest color success,
|
|
67 3 for Non-deallocable sucess. */
|
|
68 int
|
2286
|
69 allocate_nearest_color (GdkColormap *colormap, GdkVisual *UNUSED (visual),
|
462
|
70 GdkColor *color_def)
|
|
71 {
|
|
72 int rc;
|
|
73
|
|
74 rc = gdk_colormap_alloc_color (colormap, color_def, FALSE, TRUE);
|
|
75
|
|
76 if (rc == TRUE)
|
|
77 return (1);
|
|
78
|
|
79 return (0);
|
|
80 }
|
|
81
|
|
82 int
|
867
|
83 gtk_parse_nearest_color (struct device *d, GdkColor *color, Ibyte *name,
|
578
|
84 Bytecount len, Error_Behavior errb)
|
462
|
85 {
|
|
86 GdkColormap *cmap;
|
|
87 GdkVisual *visual;
|
|
88 int result;
|
|
89
|
|
90 cmap = DEVICE_GTK_COLORMAP(d);
|
|
91 visual = DEVICE_GTK_VISUAL (d);
|
|
92
|
|
93 xzero (*color);
|
|
94 {
|
|
95 const Extbyte *extname;
|
665
|
96 Bytecount extnamelen;
|
462
|
97
|
|
98 TO_EXTERNAL_FORMAT (DATA, (name, len), ALLOCA, (extname, extnamelen), Qbinary);
|
|
99
|
|
100 result = gdk_color_parse (extname, color);
|
|
101 }
|
|
102
|
|
103 if (result == FALSE)
|
|
104 {
|
563
|
105 maybe_invalid_argument ("unrecognized color", make_string (name, len),
|
|
106 Qcolor, errb);
|
462
|
107 return 0;
|
|
108 }
|
|
109 result = allocate_nearest_color (cmap, visual, color);
|
|
110 if (!result)
|
|
111 {
|
563
|
112 maybe_signal_error (Qgui_error, "couldn't allocate color",
|
|
113 make_string (name, len), Qcolor, errb);
|
462
|
114 return 0;
|
|
115 }
|
|
116
|
|
117 return result;
|
|
118 }
|
|
119
|
|
120 static int
|
|
121 gtk_initialize_color_instance (struct Lisp_Color_Instance *c, Lisp_Object name,
|
578
|
122 Lisp_Object device, Error_Behavior errb)
|
462
|
123 {
|
|
124 GdkColor color;
|
|
125 int result;
|
|
126
|
|
127 result = gtk_parse_nearest_color (XDEVICE (device), &color,
|
|
128 XSTRING_DATA (name),
|
|
129 XSTRING_LENGTH (name),
|
|
130 errb);
|
|
131
|
|
132 if (!result)
|
|
133 return 0;
|
|
134
|
|
135 /* Don't allocate the data until we're sure that we will succeed,
|
|
136 or the finalize method may get fucked. */
|
|
137 c->data = xnew (struct gtk_color_instance_data);
|
|
138 if (result == 3)
|
|
139 COLOR_INSTANCE_GTK_DEALLOC (c) = 0;
|
|
140 else
|
|
141 COLOR_INSTANCE_GTK_DEALLOC (c) = 1;
|
|
142 COLOR_INSTANCE_GTK_COLOR (c) = gdk_color_copy (&color);
|
|
143 return 1;
|
|
144 }
|
|
145
|
|
146 static void
|
|
147 gtk_print_color_instance (struct Lisp_Color_Instance *c,
|
|
148 Lisp_Object printcharfun,
|
2286
|
149 int UNUSED (escapeflag))
|
462
|
150 {
|
|
151 GdkColor *color = COLOR_INSTANCE_GTK_COLOR (c);
|
800
|
152 write_fmt_string (printcharfun, " %ld=(%X,%X,%X)",
|
|
153 color->pixel, color->red, color->green, color->blue);
|
462
|
154 }
|
|
155
|
|
156 static void
|
|
157 gtk_finalize_color_instance (struct Lisp_Color_Instance *c)
|
|
158 {
|
|
159 if (c->data)
|
|
160 {
|
|
161 if (DEVICE_LIVE_P (XDEVICE (c->device)))
|
|
162 {
|
|
163 if (COLOR_INSTANCE_GTK_DEALLOC (c))
|
|
164 {
|
|
165 gdk_colormap_free_colors (DEVICE_GTK_COLORMAP (XDEVICE (c->device)),
|
|
166 COLOR_INSTANCE_GTK_COLOR (c), 1);
|
|
167 }
|
|
168 gdk_color_free (COLOR_INSTANCE_GTK_COLOR (c));
|
|
169 }
|
1726
|
170 xfree (c->data, void *);
|
462
|
171 c->data = 0;
|
|
172 }
|
|
173 }
|
|
174
|
|
175 /* Color instances are equal if they resolve to the same color on the
|
|
176 screen (have the same RGB values). I imagine that
|
|
177 "same RGB values" == "same cell in the colormap." Arguably we should
|
|
178 be comparing their names or pixel values instead. */
|
|
179
|
|
180 static int
|
|
181 gtk_color_instance_equal (struct Lisp_Color_Instance *c1,
|
|
182 struct Lisp_Color_Instance *c2,
|
2286
|
183 int UNUSED (depth))
|
462
|
184 {
|
|
185 return (gdk_color_equal (COLOR_INSTANCE_GTK_COLOR (c1),
|
|
186 COLOR_INSTANCE_GTK_COLOR (c2)));
|
|
187 }
|
|
188
|
2515
|
189 static Hashcode
|
2286
|
190 gtk_color_instance_hash (struct Lisp_Color_Instance *c, int UNUSED (depth))
|
462
|
191 {
|
|
192 return (gdk_color_hash (COLOR_INSTANCE_GTK_COLOR (c), NULL));
|
|
193 }
|
|
194
|
|
195 static Lisp_Object
|
|
196 gtk_color_instance_rgb_components (struct Lisp_Color_Instance *c)
|
|
197 {
|
|
198 GdkColor *color = COLOR_INSTANCE_GTK_COLOR (c);
|
|
199 return (list3 (make_int (color->red),
|
|
200 make_int (color->green),
|
|
201 make_int (color->blue)));
|
|
202 }
|
|
203
|
|
204 static int
|
2286
|
205 gtk_valid_color_name_p (struct device *UNUSED (d), Lisp_Object color)
|
462
|
206 {
|
|
207 GdkColor c;
|
|
208 const char *extname;
|
|
209
|
|
210 TO_EXTERNAL_FORMAT (LISP_STRING, color, C_STRING_ALLOCA, extname, Qctext);
|
|
211
|
|
212 if (gdk_color_parse (extname, &c) != TRUE)
|
|
213 return(0);
|
|
214 return (1);
|
|
215 }
|
|
216
|
2527
|
217 static Lisp_Object
|
|
218 gtk_color_list (void)
|
|
219 {
|
|
220 /* #### BILL!!!
|
|
221 Is this correct? */
|
|
222 return call0 (intern ("x-color-list-internal"));
|
|
223 }
|
|
224
|
462
|
225
|
|
226 /************************************************************************/
|
|
227 /* font instances */
|
|
228 /************************************************************************/
|
|
229
|
|
230 static int
|
2286
|
231 gtk_initialize_font_instance (struct Lisp_Font_Instance *f,
|
|
232 Lisp_Object UNUSED (name),
|
|
233 Lisp_Object UNUSED (device), Error_Behavior errb)
|
462
|
234 {
|
|
235 GdkFont *gf;
|
|
236 XFontStruct *xf;
|
|
237 const char *extname;
|
|
238
|
|
239 TO_EXTERNAL_FORMAT (LISP_STRING, f->name, C_STRING_ALLOCA, extname, Qctext);
|
|
240
|
|
241 gf = gdk_font_load (extname);
|
|
242
|
|
243 if (!gf)
|
|
244 {
|
563
|
245 maybe_signal_error (Qgui_error, "couldn't load font", f->name,
|
|
246 Qfont, errb);
|
462
|
247 return 0;
|
|
248 }
|
|
249
|
2054
|
250 xf = (XFontStruct*) GDK_FONT_XFONT (gf);
|
462
|
251
|
|
252 /* Don't allocate the data until we're sure that we will succeed,
|
|
253 or the finalize method may get fucked. */
|
|
254 f->data = xnew (struct gtk_font_instance_data);
|
|
255 FONT_INSTANCE_GTK_FONT (f) = gf;
|
|
256 f->ascent = gf->ascent;
|
|
257 f->descent = gf->descent;
|
|
258 f->height = gf->ascent + gf->descent;
|
|
259
|
|
260 /* Now lets figure out the width of the font */
|
|
261 {
|
|
262 /* following change suggested by Ted Phelps <phelps@dstc.edu.au> */
|
|
263 unsigned int def_char = 'n'; /*xf->default_char;*/
|
|
264 unsigned int byte1, byte2;
|
|
265
|
|
266 once_more:
|
|
267 byte1 = def_char >> 8;
|
|
268 byte2 = def_char & 0xFF;
|
|
269
|
|
270 if (xf->per_char)
|
|
271 {
|
|
272 /* Old versions of the R5 font server have garbage (>63k) as
|
|
273 def_char. 'n' might not be a valid character. */
|
|
274 if (byte1 < xf->min_byte1 ||
|
|
275 byte1 > xf->max_byte1 ||
|
|
276 byte2 < xf->min_char_or_byte2 ||
|
|
277 byte2 > xf->max_char_or_byte2)
|
|
278 f->width = 0;
|
|
279 else
|
|
280 f->width = xf->per_char[(byte1 - xf->min_byte1) *
|
|
281 (xf->max_char_or_byte2 -
|
|
282 xf->min_char_or_byte2 + 1) +
|
|
283 (byte2 - xf->min_char_or_byte2)].width;
|
|
284 }
|
|
285 else
|
|
286 f->width = xf->max_bounds.width;
|
|
287
|
|
288 /* Some fonts have a default char whose width is 0. This is no good.
|
|
289 If that's the case, first try 'n' as the default char, and if n has
|
|
290 0 width too (unlikely) then just use the max width. */
|
|
291 if (f->width == 0)
|
|
292 {
|
|
293 if (def_char == xf->default_char)
|
|
294 f->width = xf->max_bounds.width;
|
|
295 else
|
|
296 {
|
|
297 def_char = xf->default_char;
|
|
298 goto once_more;
|
|
299 }
|
|
300 }
|
|
301 }
|
|
302
|
|
303 /* If all characters don't exist then there could potentially be
|
|
304 0-width characters lurking out there. Not setting this flag
|
|
305 trips an optimization that would make them appear to have width
|
|
306 to redisplay. This is bad. So we set it if not all characters
|
|
307 have the same width or if not all characters are defined.
|
|
308 */
|
|
309 /* #### This sucks. There is a measurable performance increase
|
|
310 when using proportional width fonts if this flag is not set.
|
|
311 Unfortunately so many of the fucking X fonts are not fully
|
|
312 defined that we could almost just get rid of this damn flag and
|
|
313 make it an assertion. */
|
|
314 f->proportional_p = (xf->min_bounds.width != xf->max_bounds.width ||
|
|
315 (/* x_handle_non_fully_specified_fonts */ 0 &&
|
|
316 !xf->all_chars_exist));
|
|
317 #if 0
|
|
318 f->width = gdk_char_width (gf, 'n');
|
|
319 f->proportional_p = (gdk_char_width (gf, '|') != gdk_char_width (gf, 'W')) ? 1 : 0;
|
|
320 #endif
|
|
321 return 1;
|
|
322 }
|
|
323
|
|
324 static void
|
|
325 gtk_print_font_instance (struct Lisp_Font_Instance *f,
|
|
326 Lisp_Object printcharfun,
|
2286
|
327 int UNUSED (escapeflag))
|
462
|
328 {
|
800
|
329 write_fmt_string (printcharfun, " 0x%lx",
|
|
330 (unsigned long) gdk_font_id (FONT_INSTANCE_GTK_FONT (f)));
|
462
|
331 }
|
|
332
|
|
333 static void
|
|
334 gtk_finalize_font_instance (struct Lisp_Font_Instance *f)
|
|
335 {
|
|
336 if (f->data)
|
|
337 {
|
|
338 if (DEVICE_LIVE_P (XDEVICE (f->device)))
|
|
339 {
|
|
340 gdk_font_unref (FONT_INSTANCE_GTK_FONT (f));
|
|
341 }
|
1726
|
342 xfree (f->data, void *);
|
462
|
343 f->data = 0;
|
|
344 }
|
|
345 }
|
|
346
|
|
347 /* Forward declarations for X specific functions at the end of the file */
|
|
348 Lisp_Object __get_gtk_font_truename (GdkFont *gdk_font, int expandp);
|
2527
|
349 static Lisp_Object __gtk_font_list_internal (const char *pattern);
|
462
|
350
|
|
351 static Lisp_Object
|
2286
|
352 gtk_font_instance_truename (struct Lisp_Font_Instance *f,
|
|
353 Error_Behavior UNUSED (errb))
|
462
|
354 {
|
872
|
355 if (NILP (FONT_INSTANCE_TRUENAME (f)))
|
462
|
356 {
|
872
|
357 FONT_INSTANCE_TRUENAME (f) = __get_gtk_font_truename (FONT_INSTANCE_GTK_FONT (f), 1);
|
462
|
358
|
872
|
359 if (NILP (FONT_INSTANCE_TRUENAME (f)))
|
462
|
360 {
|
|
361 /* Ok, just this once, return the font name as the truename.
|
|
362 (This is only used by Fequal() right now.) */
|
|
363 return f->name;
|
|
364 }
|
|
365 }
|
872
|
366 return (FONT_INSTANCE_TRUENAME (f));
|
462
|
367 }
|
|
368
|
|
369 static Lisp_Object
|
2286
|
370 gtk_font_instance_properties (struct Lisp_Font_Instance *UNUSED (f))
|
462
|
371 {
|
|
372 Lisp_Object result = Qnil;
|
|
373
|
|
374 /* #### BILL!!! */
|
|
375 /* There seems to be no way to get this information under Gtk */
|
|
376 return result;
|
|
377 }
|
|
378
|
|
379 static Lisp_Object
|
2527
|
380 gtk_font_list (Lisp_Object pattern, Lisp_Object UNUSED (device),
|
2286
|
381 Lisp_Object UNUSED (maxnumber))
|
462
|
382 {
|
|
383 const char *patternext;
|
|
384
|
|
385 TO_EXTERNAL_FORMAT (LISP_STRING, pattern, C_STRING_ALLOCA, patternext, Qbinary);
|
|
386
|
2527
|
387 return (__gtk_font_list_internal (patternext));
|
462
|
388 }
|
|
389
|
3659
|
390 /* Include the charset support, shared, for the moment, with X11. */
|
|
391 #define THIS_IS_GTK
|
|
392 #include "objects-xlike-inc.c"
|
462
|
393
|
|
394
|
|
395 /************************************************************************/
|
|
396 /* initialization */
|
|
397 /************************************************************************/
|
|
398
|
|
399 void
|
|
400 syms_of_objects_gtk (void)
|
|
401 {
|
|
402 }
|
|
403
|
|
404 void
|
|
405 console_type_create_objects_gtk (void)
|
|
406 {
|
|
407 /* object methods */
|
|
408
|
|
409 CONSOLE_HAS_METHOD (gtk, initialize_color_instance);
|
|
410 CONSOLE_HAS_METHOD (gtk, print_color_instance);
|
|
411 CONSOLE_HAS_METHOD (gtk, finalize_color_instance);
|
|
412 CONSOLE_HAS_METHOD (gtk, color_instance_equal);
|
|
413 CONSOLE_HAS_METHOD (gtk, color_instance_hash);
|
|
414 CONSOLE_HAS_METHOD (gtk, color_instance_rgb_components);
|
|
415 CONSOLE_HAS_METHOD (gtk, valid_color_name_p);
|
2527
|
416 CONSOLE_HAS_METHOD (gtk, color_list);
|
462
|
417
|
|
418 CONSOLE_HAS_METHOD (gtk, initialize_font_instance);
|
|
419 CONSOLE_HAS_METHOD (gtk, print_font_instance);
|
|
420 CONSOLE_HAS_METHOD (gtk, finalize_font_instance);
|
|
421 CONSOLE_HAS_METHOD (gtk, font_instance_truename);
|
|
422 CONSOLE_HAS_METHOD (gtk, font_instance_properties);
|
2527
|
423 CONSOLE_HAS_METHOD (gtk, font_list);
|
462
|
424 #ifdef MULE
|
|
425 CONSOLE_HAS_METHOD (gtk, find_charset_font);
|
|
426 CONSOLE_HAS_METHOD (gtk, font_spec_matches_charset);
|
|
427 #endif
|
|
428 }
|
|
429
|
|
430 void
|
|
431 vars_of_objects_gtk (void)
|
|
432 {
|
3659
|
433 #ifdef DEBUG_XEMACS
|
|
434 DEFVAR_INT ("debug-x-objects", &debug_x_objects /*
|
|
435 If non-zero, display debug information about X objects
|
|
436 */ );
|
|
437 debug_x_objects = 0;
|
|
438 #endif
|
462
|
439 }
|
|
440
|
|
441 static int
|
|
442 valid_font_name_p (Display *dpy, char *name)
|
|
443 {
|
|
444 /* Maybe this should be implemented by callign XLoadFont and trapping
|
|
445 the error. That would be a lot of work, and wasteful as hell, but
|
|
446 might be more correct.
|
|
447 */
|
|
448 int nnames = 0;
|
|
449 char **names = 0;
|
|
450 if (! name)
|
|
451 return 0;
|
|
452 names = XListFonts (dpy, name, 1, &nnames);
|
|
453 if (names)
|
|
454 XFreeFontNames (names);
|
|
455 return (nnames != 0);
|
|
456 }
|
|
457
|
|
458 Lisp_Object
|
|
459 __get_gtk_font_truename (GdkFont *gdk_font, int expandp)
|
|
460 {
|
|
461 Display *dpy = GDK_FONT_XDISPLAY (gdk_font);
|
|
462 GSList *names = ((GdkFontPrivate *) gdk_font)->names;
|
|
463 Lisp_Object font_name = Qnil;
|
|
464
|
|
465 while (names)
|
|
466 {
|
|
467 if (names->data)
|
|
468 {
|
2054
|
469 if (valid_font_name_p (dpy, (char*) names->data))
|
462
|
470 {
|
|
471 if (!expandp)
|
|
472 {
|
|
473 /* They want the wildcarded version */
|
2054
|
474 font_name = build_string ((char*) names->data);
|
462
|
475 }
|
|
476 else
|
|
477 {
|
|
478 /* Need to expand out */
|
|
479 int nnames = 0;
|
|
480 char **x_font_names = 0;
|
|
481
|
2054
|
482 x_font_names = XListFonts (dpy, (char*) names->data, 1, &nnames);
|
462
|
483 if (x_font_names)
|
|
484 {
|
|
485 font_name = build_string (x_font_names[0]);
|
|
486 XFreeFontNames (x_font_names);
|
|
487 }
|
|
488 }
|
|
489 break;
|
|
490 }
|
|
491 }
|
|
492 names = names->next;
|
|
493 }
|
|
494 return (font_name);
|
|
495 }
|
|
496
|
2527
|
497 static Lisp_Object __gtk_font_list_internal (const char *pattern)
|
462
|
498 {
|
|
499 char **names;
|
|
500 int count = 0;
|
|
501 Lisp_Object result = Qnil;
|
|
502
|
|
503 names = XListFonts (GDK_DISPLAY (), pattern, MAX_FONT_COUNT, &count);
|
|
504 while (count--)
|
|
505 result = Fcons (build_ext_string (names [count], Qbinary), result);
|
|
506 if (names)
|
|
507 XFreeFontNames (names);
|
|
508
|
|
509 return result;
|
|
510 }
|