]> code.delx.au - gnu-emacs/blob - src/xfns.c
2002-08-11 Andrew Choi <akochoi@shaw.ca>
[gnu-emacs] / src / xfns.c
1 /* Functions for the X window system.
2 Copyright (C) 1989, 92, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include <config.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <math.h>
26
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 /* This makes the fields of a Display accessible, in Xlib header files. */
32
33 #define XLIB_ILLEGAL_ACCESS
34
35 #include "lisp.h"
36 #include "xterm.h"
37 #include "frame.h"
38 #include "window.h"
39 #include "buffer.h"
40 #include "intervals.h"
41 #include "dispextern.h"
42 #include "keyboard.h"
43 #include "blockinput.h"
44 #include <epaths.h>
45 #include "charset.h"
46 #include "coding.h"
47 #include "fontset.h"
48 #include "systime.h"
49 #include "termhooks.h"
50 #include "atimer.h"
51
52 #ifdef HAVE_X_WINDOWS
53
54 #include <ctype.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
57
58 #ifndef VMS
59 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
60 #include "bitmaps/gray.xbm"
61 #else
62 #include <X11/bitmaps/gray>
63 #endif
64 #else
65 #include "[.bitmaps]gray.xbm"
66 #endif
67
68 #ifdef USE_X_TOOLKIT
69 #include <X11/Shell.h>
70
71 #ifndef USE_MOTIF
72 #include <X11/Xaw/Paned.h>
73 #include <X11/Xaw/Label.h>
74 #endif /* USE_MOTIF */
75
76 #ifdef USG
77 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
78 #include <X11/Xos.h>
79 #define USG
80 #else
81 #include <X11/Xos.h>
82 #endif
83
84 #include "widget.h"
85
86 #include "../lwlib/lwlib.h"
87
88 #ifdef USE_MOTIF
89 #include <Xm/Xm.h>
90 #include <Xm/DialogS.h>
91 #include <Xm/FileSB.h>
92 #endif
93
94 /* Do the EDITRES protocol if running X11R5
95 Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
96
97 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
98 #define HACK_EDITRES
99 extern void _XEditResCheckMessages ();
100 #endif /* R5 + Athena */
101
102 /* Unique id counter for widgets created by the Lucid Widget Library. */
103
104 extern LWLIB_ID widget_id_tick;
105
106 #ifdef USE_LUCID
107 /* This is part of a kludge--see lwlib/xlwmenu.c. */
108 extern XFontStruct *xlwmenu_default_font;
109 #endif
110
111 extern void free_frame_menubar ();
112 extern double atof ();
113
114 #ifdef USE_MOTIF
115
116 /* LessTif/Motif version info. */
117
118 static Lisp_Object Vmotif_version_string;
119
120 #endif /* USE_MOTIF */
121
122 #endif /* USE_X_TOOLKIT */
123
124 #ifdef HAVE_X11R4
125 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
126 #else
127 #define MAXREQUEST(dpy) ((dpy)->max_request_size)
128 #endif
129
130 /* The gray bitmap `bitmaps/gray'. This is done because xterm.c uses
131 it, and including `bitmaps/gray' more than once is a problem when
132 config.h defines `static' as an empty replacement string. */
133
134 int gray_bitmap_width = gray_width;
135 int gray_bitmap_height = gray_height;
136 char *gray_bitmap_bits = gray_bits;
137
138 /* The name we're using in resource queries. Most often "emacs". */
139
140 Lisp_Object Vx_resource_name;
141
142 /* The application class we're using in resource queries.
143 Normally "Emacs". */
144
145 Lisp_Object Vx_resource_class;
146
147 /* Non-zero means we're allowed to display an hourglass cursor. */
148
149 int display_hourglass_p;
150
151 /* The background and shape of the mouse pointer, and shape when not
152 over text or in the modeline. */
153
154 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
155 Lisp_Object Vx_hourglass_pointer_shape;
156
157 /* The shape when over mouse-sensitive text. */
158
159 Lisp_Object Vx_sensitive_text_pointer_shape;
160
161 /* If non-nil, the pointer shape to indicate that windows can be
162 dragged horizontally. */
163
164 Lisp_Object Vx_window_horizontal_drag_shape;
165
166 /* Color of chars displayed in cursor box. */
167
168 Lisp_Object Vx_cursor_fore_pixel;
169
170 /* Nonzero if using X. */
171
172 static int x_in_use;
173
174 /* Non nil if no window manager is in use. */
175
176 Lisp_Object Vx_no_window_manager;
177
178 /* Search path for bitmap files. */
179
180 Lisp_Object Vx_bitmap_file_path;
181
182 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
183
184 Lisp_Object Vx_pixel_size_width_font_regexp;
185
186 Lisp_Object Qauto_raise;
187 Lisp_Object Qauto_lower;
188 Lisp_Object Qbar, Qhbar;
189 Lisp_Object Qborder_color;
190 Lisp_Object Qborder_width;
191 Lisp_Object Qbox;
192 Lisp_Object Qcursor_color;
193 Lisp_Object Qcursor_type;
194 Lisp_Object Qgeometry;
195 Lisp_Object Qicon_left;
196 Lisp_Object Qicon_top;
197 Lisp_Object Qicon_type;
198 Lisp_Object Qicon_name;
199 Lisp_Object Qinternal_border_width;
200 Lisp_Object Qleft;
201 Lisp_Object Qright;
202 Lisp_Object Qmouse_color;
203 Lisp_Object Qnone;
204 Lisp_Object Qouter_window_id;
205 Lisp_Object Qparent_id;
206 Lisp_Object Qscroll_bar_width;
207 Lisp_Object Qsuppress_icon;
208 extern Lisp_Object Qtop;
209 Lisp_Object Qundefined_color;
210 Lisp_Object Qvertical_scroll_bars;
211 Lisp_Object Qvisibility;
212 Lisp_Object Qwindow_id;
213 Lisp_Object Qx_frame_parameter;
214 Lisp_Object Qx_resource_name;
215 Lisp_Object Quser_position;
216 Lisp_Object Quser_size;
217 extern Lisp_Object Qdisplay;
218 Lisp_Object Qscroll_bar_foreground, Qscroll_bar_background;
219 Lisp_Object Qscreen_gamma, Qline_spacing, Qcenter;
220 Lisp_Object Qcompound_text, Qcancel_timer;
221 Lisp_Object Qwait_for_wm;
222 Lisp_Object Qfullscreen;
223 Lisp_Object Qfullwidth;
224 Lisp_Object Qfullheight;
225 Lisp_Object Qfullboth;
226
227 /* The below are defined in frame.c. */
228
229 extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
230 extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
231 extern Lisp_Object Qtool_bar_lines;
232
233 extern Lisp_Object Vwindow_system_version;
234
235 Lisp_Object Qface_set_after_frame_default;
236
237 #if GLYPH_DEBUG
238 int image_cache_refcount, dpyinfo_refcount;
239 #endif
240
241
242 \f
243 /* Error if we are not connected to X. */
244
245 void
246 check_x ()
247 {
248 if (! x_in_use)
249 error ("X windows are not in use or not initialized");
250 }
251
252 /* Nonzero if we can use mouse menus.
253 You should not call this unless HAVE_MENUS is defined. */
254
255 int
256 have_menus_p ()
257 {
258 return x_in_use;
259 }
260
261 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
262 and checking validity for X. */
263
264 FRAME_PTR
265 check_x_frame (frame)
266 Lisp_Object frame;
267 {
268 FRAME_PTR f;
269
270 if (NILP (frame))
271 frame = selected_frame;
272 CHECK_LIVE_FRAME (frame);
273 f = XFRAME (frame);
274 if (! FRAME_X_P (f))
275 error ("Non-X frame used");
276 return f;
277 }
278
279 /* Let the user specify an X display with a frame.
280 nil stands for the selected frame--or, if that is not an X frame,
281 the first X display on the list. */
282
283 static struct x_display_info *
284 check_x_display_info (frame)
285 Lisp_Object frame;
286 {
287 struct x_display_info *dpyinfo = NULL;
288
289 if (NILP (frame))
290 {
291 struct frame *sf = XFRAME (selected_frame);
292
293 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
294 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
295 else if (x_display_list != 0)
296 dpyinfo = x_display_list;
297 else
298 error ("X windows are not in use or not initialized");
299 }
300 else if (STRINGP (frame))
301 dpyinfo = x_display_info_for_name (frame);
302 else
303 {
304 FRAME_PTR f = check_x_frame (frame);
305 dpyinfo = FRAME_X_DISPLAY_INFO (f);
306 }
307
308 return dpyinfo;
309 }
310
311 \f
312 /* Return the Emacs frame-object corresponding to an X window.
313 It could be the frame's main window or an icon window. */
314
315 /* This function can be called during GC, so use GC_xxx type test macros. */
316
317 struct frame *
318 x_window_to_frame (dpyinfo, wdesc)
319 struct x_display_info *dpyinfo;
320 int wdesc;
321 {
322 Lisp_Object tail, frame;
323 struct frame *f;
324
325 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
326 {
327 frame = XCAR (tail);
328 if (!GC_FRAMEP (frame))
329 continue;
330 f = XFRAME (frame);
331 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
332 continue;
333 if (f->output_data.x->hourglass_window == wdesc)
334 return f;
335 #ifdef USE_X_TOOLKIT
336 if ((f->output_data.x->edit_widget
337 && XtWindow (f->output_data.x->edit_widget) == wdesc)
338 /* A tooltip frame? */
339 || (!f->output_data.x->edit_widget
340 && FRAME_X_WINDOW (f) == wdesc)
341 || f->output_data.x->icon_desc == wdesc)
342 return f;
343 #else /* not USE_X_TOOLKIT */
344 if (FRAME_X_WINDOW (f) == wdesc
345 || f->output_data.x->icon_desc == wdesc)
346 return f;
347 #endif /* not USE_X_TOOLKIT */
348 }
349 return 0;
350 }
351
352 #ifdef USE_X_TOOLKIT
353 /* Like x_window_to_frame but also compares the window with the widget's
354 windows. */
355
356 struct frame *
357 x_any_window_to_frame (dpyinfo, wdesc)
358 struct x_display_info *dpyinfo;
359 int wdesc;
360 {
361 Lisp_Object tail, frame;
362 struct frame *f, *found;
363 struct x_output *x;
364
365 found = NULL;
366 for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
367 {
368 frame = XCAR (tail);
369 if (!GC_FRAMEP (frame))
370 continue;
371
372 f = XFRAME (frame);
373 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
374 {
375 /* This frame matches if the window is any of its widgets. */
376 x = f->output_data.x;
377 if (x->hourglass_window == wdesc)
378 found = f;
379 else if (x->widget)
380 {
381 if (wdesc == XtWindow (x->widget)
382 || wdesc == XtWindow (x->column_widget)
383 || wdesc == XtWindow (x->edit_widget))
384 found = f;
385 /* Match if the window is this frame's menubar. */
386 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
387 found = f;
388 }
389 else if (FRAME_X_WINDOW (f) == wdesc)
390 /* A tooltip frame. */
391 found = f;
392 }
393 }
394
395 return found;
396 }
397
398 /* Likewise, but exclude the menu bar widget. */
399
400 struct frame *
401 x_non_menubar_window_to_frame (dpyinfo, wdesc)
402 struct x_display_info *dpyinfo;
403 int wdesc;
404 {
405 Lisp_Object tail, frame;
406 struct frame *f;
407 struct x_output *x;
408
409 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
410 {
411 frame = XCAR (tail);
412 if (!GC_FRAMEP (frame))
413 continue;
414 f = XFRAME (frame);
415 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
416 continue;
417 x = f->output_data.x;
418 /* This frame matches if the window is any of its widgets. */
419 if (x->hourglass_window == wdesc)
420 return f;
421 else if (x->widget)
422 {
423 if (wdesc == XtWindow (x->widget)
424 || wdesc == XtWindow (x->column_widget)
425 || wdesc == XtWindow (x->edit_widget))
426 return f;
427 }
428 else if (FRAME_X_WINDOW (f) == wdesc)
429 /* A tooltip frame. */
430 return f;
431 }
432 return 0;
433 }
434
435 /* Likewise, but consider only the menu bar widget. */
436
437 struct frame *
438 x_menubar_window_to_frame (dpyinfo, wdesc)
439 struct x_display_info *dpyinfo;
440 int wdesc;
441 {
442 Lisp_Object tail, frame;
443 struct frame *f;
444 struct x_output *x;
445
446 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
447 {
448 frame = XCAR (tail);
449 if (!GC_FRAMEP (frame))
450 continue;
451 f = XFRAME (frame);
452 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
453 continue;
454 x = f->output_data.x;
455 /* Match if the window is this frame's menubar. */
456 if (x->menubar_widget
457 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
458 return f;
459 }
460 return 0;
461 }
462
463 /* Return the frame whose principal (outermost) window is WDESC.
464 If WDESC is some other (smaller) window, we return 0. */
465
466 struct frame *
467 x_top_window_to_frame (dpyinfo, wdesc)
468 struct x_display_info *dpyinfo;
469 int wdesc;
470 {
471 Lisp_Object tail, frame;
472 struct frame *f;
473 struct x_output *x;
474
475 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
476 {
477 frame = XCAR (tail);
478 if (!GC_FRAMEP (frame))
479 continue;
480 f = XFRAME (frame);
481 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
482 continue;
483 x = f->output_data.x;
484
485 if (x->widget)
486 {
487 /* This frame matches if the window is its topmost widget. */
488 if (wdesc == XtWindow (x->widget))
489 return f;
490 #if 0 /* I don't know why it did this,
491 but it seems logically wrong,
492 and it causes trouble for MapNotify events. */
493 /* Match if the window is this frame's menubar. */
494 if (x->menubar_widget
495 && wdesc == XtWindow (x->menubar_widget))
496 return f;
497 #endif
498 }
499 else if (FRAME_X_WINDOW (f) == wdesc)
500 /* Tooltip frame. */
501 return f;
502 }
503 return 0;
504 }
505 #endif /* USE_X_TOOLKIT */
506
507 \f
508
509 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
510 id, which is just an int that this section returns. Bitmaps are
511 reference counted so they can be shared among frames.
512
513 Bitmap indices are guaranteed to be > 0, so a negative number can
514 be used to indicate no bitmap.
515
516 If you use x_create_bitmap_from_data, then you must keep track of
517 the bitmaps yourself. That is, creating a bitmap from the same
518 data more than once will not be caught. */
519
520
521 /* Functions to access the contents of a bitmap, given an id. */
522
523 int
524 x_bitmap_height (f, id)
525 FRAME_PTR f;
526 int id;
527 {
528 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
529 }
530
531 int
532 x_bitmap_width (f, id)
533 FRAME_PTR f;
534 int id;
535 {
536 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
537 }
538
539 int
540 x_bitmap_pixmap (f, id)
541 FRAME_PTR f;
542 int id;
543 {
544 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
545 }
546
547
548 /* Allocate a new bitmap record. Returns index of new record. */
549
550 static int
551 x_allocate_bitmap_record (f)
552 FRAME_PTR f;
553 {
554 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
555 int i;
556
557 if (dpyinfo->bitmaps == NULL)
558 {
559 dpyinfo->bitmaps_size = 10;
560 dpyinfo->bitmaps
561 = (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
562 dpyinfo->bitmaps_last = 1;
563 return 1;
564 }
565
566 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
567 return ++dpyinfo->bitmaps_last;
568
569 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
570 if (dpyinfo->bitmaps[i].refcount == 0)
571 return i + 1;
572
573 dpyinfo->bitmaps_size *= 2;
574 dpyinfo->bitmaps
575 = (struct x_bitmap_record *) xrealloc (dpyinfo->bitmaps,
576 dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
577 return ++dpyinfo->bitmaps_last;
578 }
579
580 /* Add one reference to the reference count of the bitmap with id ID. */
581
582 void
583 x_reference_bitmap (f, id)
584 FRAME_PTR f;
585 int id;
586 {
587 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
588 }
589
590 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
591
592 int
593 x_create_bitmap_from_data (f, bits, width, height)
594 struct frame *f;
595 char *bits;
596 unsigned int width, height;
597 {
598 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
599 Pixmap bitmap;
600 int id;
601
602 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
603 bits, width, height);
604
605 if (! bitmap)
606 return -1;
607
608 id = x_allocate_bitmap_record (f);
609 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
610 dpyinfo->bitmaps[id - 1].file = NULL;
611 dpyinfo->bitmaps[id - 1].refcount = 1;
612 dpyinfo->bitmaps[id - 1].depth = 1;
613 dpyinfo->bitmaps[id - 1].height = height;
614 dpyinfo->bitmaps[id - 1].width = width;
615
616 return id;
617 }
618
619 /* Create bitmap from file FILE for frame F. */
620
621 int
622 x_create_bitmap_from_file (f, file)
623 struct frame *f;
624 Lisp_Object file;
625 {
626 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
627 unsigned int width, height;
628 Pixmap bitmap;
629 int xhot, yhot, result, id;
630 Lisp_Object found;
631 int fd;
632 char *filename;
633
634 /* Look for an existing bitmap with the same name. */
635 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
636 {
637 if (dpyinfo->bitmaps[id].refcount
638 && dpyinfo->bitmaps[id].file
639 && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file)))
640 {
641 ++dpyinfo->bitmaps[id].refcount;
642 return id + 1;
643 }
644 }
645
646 /* Search bitmap-file-path for the file, if appropriate. */
647 fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil);
648 if (fd < 0)
649 return -1;
650 emacs_close (fd);
651
652 filename = (char *) SDATA (found);
653
654 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
655 filename, &width, &height, &bitmap, &xhot, &yhot);
656 if (result != BitmapSuccess)
657 return -1;
658
659 id = x_allocate_bitmap_record (f);
660 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
661 dpyinfo->bitmaps[id - 1].refcount = 1;
662 dpyinfo->bitmaps[id - 1].file
663 = (char *) xmalloc (SBYTES (file) + 1);
664 dpyinfo->bitmaps[id - 1].depth = 1;
665 dpyinfo->bitmaps[id - 1].height = height;
666 dpyinfo->bitmaps[id - 1].width = width;
667 strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file));
668
669 return id;
670 }
671
672 /* Remove reference to bitmap with id number ID. */
673
674 void
675 x_destroy_bitmap (f, id)
676 FRAME_PTR f;
677 int id;
678 {
679 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
680
681 if (id > 0)
682 {
683 --dpyinfo->bitmaps[id - 1].refcount;
684 if (dpyinfo->bitmaps[id - 1].refcount == 0)
685 {
686 BLOCK_INPUT;
687 XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
688 if (dpyinfo->bitmaps[id - 1].file)
689 {
690 xfree (dpyinfo->bitmaps[id - 1].file);
691 dpyinfo->bitmaps[id - 1].file = NULL;
692 }
693 UNBLOCK_INPUT;
694 }
695 }
696 }
697
698 /* Free all the bitmaps for the display specified by DPYINFO. */
699
700 static void
701 x_destroy_all_bitmaps (dpyinfo)
702 struct x_display_info *dpyinfo;
703 {
704 int i;
705 for (i = 0; i < dpyinfo->bitmaps_last; i++)
706 if (dpyinfo->bitmaps[i].refcount > 0)
707 {
708 XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].pixmap);
709 if (dpyinfo->bitmaps[i].file)
710 xfree (dpyinfo->bitmaps[i].file);
711 }
712 dpyinfo->bitmaps_last = 0;
713 }
714 \f
715 /* Connect the frame-parameter names for X frames
716 to the ways of passing the parameter values to the window system.
717
718 The name of a parameter, as a Lisp symbol,
719 has an `x-frame-parameter' property which is an integer in Lisp
720 that is an index in this table. */
721
722 struct x_frame_parm_table
723 {
724 char *name;
725 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
726 };
727
728 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
729 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
730 static void x_change_window_heights P_ ((Lisp_Object, int));
731 static void x_disable_image P_ ((struct frame *, struct image *));
732 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
733 static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
734 static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
735 static void x_set_fullscreen P_ ((struct frame *, Lisp_Object, Lisp_Object));
736 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
737 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
738 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
739 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
740 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
741 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
742 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
743 static void x_set_fringe_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
744 void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
745 void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
746 void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
747 Lisp_Object));
748 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
749 void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
750 void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
751 void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
752 Lisp_Object));
753 void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
754 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
755 void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
756 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
757 void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
758 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
759 void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
760 Lisp_Object));
761 void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
762 Lisp_Object));
763 static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
764 Lisp_Object,
765 Lisp_Object,
766 char *, char *,
767 int));
768 static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
769 static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
770 Lisp_Object));
771 static void init_color_table P_ ((void));
772 static void free_color_table P_ ((void));
773 static unsigned long *colors_in_color_table P_ ((int *n));
774 static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
775 static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
776
777
778
779 static struct x_frame_parm_table x_frame_parms[] =
780 {
781 {"auto-raise", x_set_autoraise},
782 {"auto-lower", x_set_autolower},
783 {"background-color", x_set_background_color},
784 {"border-color", x_set_border_color},
785 {"border-width", x_set_border_width},
786 {"cursor-color", x_set_cursor_color},
787 {"cursor-type", x_set_cursor_type},
788 {"font", x_set_font},
789 {"foreground-color", x_set_foreground_color},
790 {"icon-name", x_set_icon_name},
791 {"icon-type", x_set_icon_type},
792 {"internal-border-width", x_set_internal_border_width},
793 {"menu-bar-lines", x_set_menu_bar_lines},
794 {"mouse-color", x_set_mouse_color},
795 {"name", x_explicitly_set_name},
796 {"scroll-bar-width", x_set_scroll_bar_width},
797 {"title", x_set_title},
798 {"unsplittable", x_set_unsplittable},
799 {"vertical-scroll-bars", x_set_vertical_scroll_bars},
800 {"visibility", x_set_visibility},
801 {"tool-bar-lines", x_set_tool_bar_lines},
802 {"scroll-bar-foreground", x_set_scroll_bar_foreground},
803 {"scroll-bar-background", x_set_scroll_bar_background},
804 {"screen-gamma", x_set_screen_gamma},
805 {"line-spacing", x_set_line_spacing},
806 {"left-fringe", x_set_fringe_width},
807 {"right-fringe", x_set_fringe_width},
808 {"wait-for-wm", x_set_wait_for_wm},
809 {"fullscreen", x_set_fullscreen},
810
811 };
812
813 /* Attach the `x-frame-parameter' properties to
814 the Lisp symbol names of parameters relevant to X. */
815
816 void
817 init_x_parm_symbols ()
818 {
819 int i;
820
821 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
822 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
823 make_number (i));
824 }
825 \f
826
827 /* Really try to move where we want to be in case of fullscreen. Some WMs
828 moves the window where we tell them. Some (mwm, twm) moves the outer
829 window manager window there instead.
830 Try to compensate for those WM here. */
831 static void
832 x_fullscreen_move (f, new_top, new_left)
833 struct frame *f;
834 int new_top;
835 int new_left;
836 {
837 if (new_top != f->output_data.x->top_pos
838 || new_left != f->output_data.x->left_pos)
839 {
840 int move_x = new_left + f->output_data.x->x_pixels_outer_diff;
841 int move_y = new_top + f->output_data.x->y_pixels_outer_diff;
842
843 f->output_data.x->want_fullscreen |= FULLSCREEN_MOVE_WAIT;
844 x_set_offset (f, move_x, move_y, 1);
845 }
846 }
847
848 /* Change the parameters of frame F as specified by ALIST.
849 If a parameter is not specially recognized, do nothing special;
850 otherwise call the `x_set_...' function for that parameter.
851 Except for certain geometry properties, always call store_frame_param
852 to store the new value in the parameter alist. */
853
854 void
855 x_set_frame_parameters (f, alist)
856 FRAME_PTR f;
857 Lisp_Object alist;
858 {
859 Lisp_Object tail;
860
861 /* If both of these parameters are present, it's more efficient to
862 set them both at once. So we wait until we've looked at the
863 entire list before we set them. */
864 int width, height;
865
866 /* Same here. */
867 Lisp_Object left, top;
868
869 /* Same with these. */
870 Lisp_Object icon_left, icon_top;
871
872 /* Record in these vectors all the parms specified. */
873 Lisp_Object *parms;
874 Lisp_Object *values;
875 int i, p;
876 int left_no_change = 0, top_no_change = 0;
877 int icon_left_no_change = 0, icon_top_no_change = 0;
878 int fullscreen_is_being_set = 0;
879
880 struct gcpro gcpro1, gcpro2;
881
882 i = 0;
883 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
884 i++;
885
886 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
887 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
888
889 /* Extract parm names and values into those vectors. */
890
891 i = 0;
892 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
893 {
894 Lisp_Object elt;
895
896 elt = Fcar (tail);
897 parms[i] = Fcar (elt);
898 values[i] = Fcdr (elt);
899 i++;
900 }
901 /* TAIL and ALIST are not used again below here. */
902 alist = tail = Qnil;
903
904 GCPRO2 (*parms, *values);
905 gcpro1.nvars = i;
906 gcpro2.nvars = i;
907
908 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
909 because their values appear in VALUES and strings are not valid. */
910 top = left = Qunbound;
911 icon_left = icon_top = Qunbound;
912
913 /* Provide default values for HEIGHT and WIDTH. */
914 if (FRAME_NEW_WIDTH (f))
915 width = FRAME_NEW_WIDTH (f);
916 else
917 width = FRAME_WIDTH (f);
918
919 if (FRAME_NEW_HEIGHT (f))
920 height = FRAME_NEW_HEIGHT (f);
921 else
922 height = FRAME_HEIGHT (f);
923
924 /* Process foreground_color and background_color before anything else.
925 They are independent of other properties, but other properties (e.g.,
926 cursor_color) are dependent upon them. */
927 /* Process default font as well, since fringe widths depends on it. */
928 /* Also, process fullscreen, width and height depend upon that */
929 for (p = 0; p < i; p++)
930 {
931 Lisp_Object prop, val;
932
933 prop = parms[p];
934 val = values[p];
935 if (EQ (prop, Qforeground_color)
936 || EQ (prop, Qbackground_color)
937 || EQ (prop, Qfont)
938 || EQ (prop, Qfullscreen))
939 {
940 register Lisp_Object param_index, old_value;
941
942 old_value = get_frame_param (f, prop);
943 fullscreen_is_being_set |= EQ (prop, Qfullscreen);
944
945 if (NILP (Fequal (val, old_value)))
946 {
947 store_frame_param (f, prop, val);
948
949 param_index = Fget (prop, Qx_frame_parameter);
950 if (NATNUMP (param_index)
951 && (XFASTINT (param_index)
952 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
953 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
954 }
955 }
956 }
957
958 /* Now process them in reverse of specified order. */
959 for (i--; i >= 0; i--)
960 {
961 Lisp_Object prop, val;
962
963 prop = parms[i];
964 val = values[i];
965
966 if (EQ (prop, Qwidth) && NUMBERP (val))
967 width = XFASTINT (val);
968 else if (EQ (prop, Qheight) && NUMBERP (val))
969 height = XFASTINT (val);
970 else if (EQ (prop, Qtop))
971 top = val;
972 else if (EQ (prop, Qleft))
973 left = val;
974 else if (EQ (prop, Qicon_top))
975 icon_top = val;
976 else if (EQ (prop, Qicon_left))
977 icon_left = val;
978 else if (EQ (prop, Qforeground_color)
979 || EQ (prop, Qbackground_color)
980 || EQ (prop, Qfont)
981 || EQ (prop, Qfullscreen))
982 /* Processed above. */
983 continue;
984 else
985 {
986 register Lisp_Object param_index, old_value;
987
988 old_value = get_frame_param (f, prop);
989
990 store_frame_param (f, prop, val);
991
992 param_index = Fget (prop, Qx_frame_parameter);
993 if (NATNUMP (param_index)
994 && (XFASTINT (param_index)
995 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
996 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
997 }
998 }
999
1000 /* Don't die if just one of these was set. */
1001 if (EQ (left, Qunbound))
1002 {
1003 left_no_change = 1;
1004 if (f->output_data.x->left_pos < 0)
1005 left = Fcons (Qplus, Fcons (make_number (f->output_data.x->left_pos), Qnil));
1006 else
1007 XSETINT (left, f->output_data.x->left_pos);
1008 }
1009 if (EQ (top, Qunbound))
1010 {
1011 top_no_change = 1;
1012 if (f->output_data.x->top_pos < 0)
1013 top = Fcons (Qplus, Fcons (make_number (f->output_data.x->top_pos), Qnil));
1014 else
1015 XSETINT (top, f->output_data.x->top_pos);
1016 }
1017
1018 /* If one of the icon positions was not set, preserve or default it. */
1019 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
1020 {
1021 icon_left_no_change = 1;
1022 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
1023 if (NILP (icon_left))
1024 XSETINT (icon_left, 0);
1025 }
1026 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
1027 {
1028 icon_top_no_change = 1;
1029 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
1030 if (NILP (icon_top))
1031 XSETINT (icon_top, 0);
1032 }
1033
1034 if (FRAME_VISIBLE_P (f) && fullscreen_is_being_set)
1035 {
1036 /* If the frame is visible already and the fullscreen parameter is
1037 being set, it is too late to set WM manager hints to specify
1038 size and position.
1039 Here we first get the width, height and position that applies to
1040 fullscreen. We then move the frame to the appropriate
1041 position. Resize of the frame is taken care of in the code after
1042 this if-statement. */
1043 int new_left, new_top;
1044
1045 x_fullscreen_adjust (f, &width, &height, &new_top, &new_left);
1046 x_fullscreen_move (f, new_top, new_left);
1047 }
1048
1049 /* Don't set these parameters unless they've been explicitly
1050 specified. The window might be mapped or resized while we're in
1051 this function, and we don't want to override that unless the lisp
1052 code has asked for it.
1053
1054 Don't set these parameters unless they actually differ from the
1055 window's current parameters; the window may not actually exist
1056 yet. */
1057 {
1058 Lisp_Object frame;
1059
1060 check_frame_size (f, &height, &width);
1061
1062 XSETFRAME (frame, f);
1063
1064 if (width != FRAME_WIDTH (f)
1065 || height != FRAME_HEIGHT (f)
1066 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
1067 Fset_frame_size (frame, make_number (width), make_number (height));
1068
1069 if ((!NILP (left) || !NILP (top))
1070 && ! (left_no_change && top_no_change)
1071 && ! (NUMBERP (left) && XINT (left) == f->output_data.x->left_pos
1072 && NUMBERP (top) && XINT (top) == f->output_data.x->top_pos))
1073 {
1074 int leftpos = 0;
1075 int toppos = 0;
1076
1077 /* Record the signs. */
1078 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
1079 if (EQ (left, Qminus))
1080 f->output_data.x->size_hint_flags |= XNegative;
1081 else if (INTEGERP (left))
1082 {
1083 leftpos = XINT (left);
1084 if (leftpos < 0)
1085 f->output_data.x->size_hint_flags |= XNegative;
1086 }
1087 else if (CONSP (left) && EQ (XCAR (left), Qminus)
1088 && CONSP (XCDR (left))
1089 && INTEGERP (XCAR (XCDR (left))))
1090 {
1091 leftpos = - XINT (XCAR (XCDR (left)));
1092 f->output_data.x->size_hint_flags |= XNegative;
1093 }
1094 else if (CONSP (left) && EQ (XCAR (left), Qplus)
1095 && CONSP (XCDR (left))
1096 && INTEGERP (XCAR (XCDR (left))))
1097 {
1098 leftpos = XINT (XCAR (XCDR (left)));
1099 }
1100
1101 if (EQ (top, Qminus))
1102 f->output_data.x->size_hint_flags |= YNegative;
1103 else if (INTEGERP (top))
1104 {
1105 toppos = XINT (top);
1106 if (toppos < 0)
1107 f->output_data.x->size_hint_flags |= YNegative;
1108 }
1109 else if (CONSP (top) && EQ (XCAR (top), Qminus)
1110 && CONSP (XCDR (top))
1111 && INTEGERP (XCAR (XCDR (top))))
1112 {
1113 toppos = - XINT (XCAR (XCDR (top)));
1114 f->output_data.x->size_hint_flags |= YNegative;
1115 }
1116 else if (CONSP (top) && EQ (XCAR (top), Qplus)
1117 && CONSP (XCDR (top))
1118 && INTEGERP (XCAR (XCDR (top))))
1119 {
1120 toppos = XINT (XCAR (XCDR (top)));
1121 }
1122
1123
1124 /* Store the numeric value of the position. */
1125 f->output_data.x->top_pos = toppos;
1126 f->output_data.x->left_pos = leftpos;
1127
1128 f->output_data.x->win_gravity = NorthWestGravity;
1129
1130 /* Actually set that position, and convert to absolute. */
1131 x_set_offset (f, leftpos, toppos, -1);
1132 }
1133
1134 if ((!NILP (icon_left) || !NILP (icon_top))
1135 && ! (icon_left_no_change && icon_top_no_change))
1136 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
1137 }
1138
1139 UNGCPRO;
1140 }
1141
1142 /* Store the screen positions of frame F into XPTR and YPTR.
1143 These are the positions of the containing window manager window,
1144 not Emacs's own window. */
1145
1146 void
1147 x_real_positions (f, xptr, yptr)
1148 FRAME_PTR f;
1149 int *xptr, *yptr;
1150 {
1151 int win_x, win_y, outer_x, outer_y;
1152 int real_x = 0, real_y = 0;
1153 int had_errors = 0;
1154 Window win = f->output_data.x->parent_desc;
1155
1156 int count;
1157
1158 BLOCK_INPUT;
1159
1160 count = x_catch_errors (FRAME_X_DISPLAY (f));
1161
1162 if (win == FRAME_X_DISPLAY_INFO (f)->root_window)
1163 win = FRAME_OUTER_WINDOW (f);
1164
1165 /* This loop traverses up the containment tree until we hit the root
1166 window. Window managers may intersect many windows between our window
1167 and the root window. The window we find just before the root window
1168 should be the outer WM window. */
1169 for (;;)
1170 {
1171 Window wm_window, rootw;
1172 Window *tmp_children;
1173 unsigned int tmp_nchildren;
1174 int success;
1175
1176 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
1177 &wm_window, &tmp_children, &tmp_nchildren);
1178
1179 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
1180
1181 /* Don't free tmp_children if XQueryTree failed. */
1182 if (! success)
1183 break;
1184
1185 XFree ((char *) tmp_children);
1186
1187 if (wm_window == rootw || had_errors)
1188 break;
1189
1190 win = wm_window;
1191 }
1192
1193 if (! had_errors)
1194 {
1195 int ign;
1196 Window child, rootw;
1197
1198 /* Get the real coordinates for the WM window upper left corner */
1199 XGetGeometry (FRAME_X_DISPLAY (f), win,
1200 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
1201
1202 /* Translate real coordinates to coordinates relative to our
1203 window. For our window, the upper left corner is 0, 0.
1204 Since the upper left corner of the WM window is outside
1205 our window, win_x and win_y will be negative:
1206
1207 ------------------ ---> x
1208 | title |
1209 | ----------------- v y
1210 | | our window
1211 */
1212 XTranslateCoordinates (FRAME_X_DISPLAY (f),
1213
1214 /* From-window, to-window. */
1215 FRAME_X_DISPLAY_INFO (f)->root_window,
1216 FRAME_X_WINDOW (f),
1217
1218 /* From-position, to-position. */
1219 real_x, real_y, &win_x, &win_y,
1220
1221 /* Child of win. */
1222 &child);
1223
1224 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
1225 {
1226 outer_x = win_x;
1227 outer_y = win_y;
1228 }
1229 else
1230 {
1231 XTranslateCoordinates (FRAME_X_DISPLAY (f),
1232
1233 /* From-window, to-window. */
1234 FRAME_X_DISPLAY_INFO (f)->root_window,
1235 FRAME_OUTER_WINDOW (f),
1236
1237 /* From-position, to-position. */
1238 real_x, real_y, &outer_x, &outer_y,
1239
1240 /* Child of win. */
1241 &child);
1242 }
1243
1244 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
1245 }
1246
1247 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
1248
1249 UNBLOCK_INPUT;
1250
1251 if (had_errors) return;
1252
1253 f->output_data.x->x_pixels_diff = -win_x;
1254 f->output_data.x->y_pixels_diff = -win_y;
1255 f->output_data.x->x_pixels_outer_diff = -outer_x;
1256 f->output_data.x->y_pixels_outer_diff = -outer_y;
1257
1258 *xptr = real_x;
1259 *yptr = real_y;
1260 }
1261
1262 /* Insert a description of internally-recorded parameters of frame X
1263 into the parameter alist *ALISTPTR that is to be given to the user.
1264 Only parameters that are specific to the X window system
1265 and whose values are not correctly recorded in the frame's
1266 param_alist need to be considered here. */
1267
1268 void
1269 x_report_frame_params (f, alistptr)
1270 struct frame *f;
1271 Lisp_Object *alistptr;
1272 {
1273 char buf[16];
1274 Lisp_Object tem;
1275
1276 /* Represent negative positions (off the top or left screen edge)
1277 in a way that Fmodify_frame_parameters will understand correctly. */
1278 XSETINT (tem, f->output_data.x->left_pos);
1279 if (f->output_data.x->left_pos >= 0)
1280 store_in_alist (alistptr, Qleft, tem);
1281 else
1282 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1283
1284 XSETINT (tem, f->output_data.x->top_pos);
1285 if (f->output_data.x->top_pos >= 0)
1286 store_in_alist (alistptr, Qtop, tem);
1287 else
1288 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
1289
1290 store_in_alist (alistptr, Qborder_width,
1291 make_number (f->output_data.x->border_width));
1292 store_in_alist (alistptr, Qinternal_border_width,
1293 make_number (f->output_data.x->internal_border_width));
1294 store_in_alist (alistptr, Qleft_fringe,
1295 make_number (f->output_data.x->left_fringe_width));
1296 store_in_alist (alistptr, Qright_fringe,
1297 make_number (f->output_data.x->right_fringe_width));
1298 store_in_alist (alistptr, Qscroll_bar_width,
1299 make_number (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
1300 ? FRAME_SCROLL_BAR_PIXEL_WIDTH(f)
1301 : 0));
1302 sprintf (buf, "%ld", (long) FRAME_X_WINDOW (f));
1303 store_in_alist (alistptr, Qwindow_id,
1304 build_string (buf));
1305 #ifdef USE_X_TOOLKIT
1306 /* Tooltip frame may not have this widget. */
1307 if (f->output_data.x->widget)
1308 #endif
1309 sprintf (buf, "%ld", (long) FRAME_OUTER_WINDOW (f));
1310 store_in_alist (alistptr, Qouter_window_id,
1311 build_string (buf));
1312 store_in_alist (alistptr, Qicon_name, f->icon_name);
1313 FRAME_SAMPLE_VISIBILITY (f);
1314 store_in_alist (alistptr, Qvisibility,
1315 (FRAME_VISIBLE_P (f) ? Qt
1316 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
1317 store_in_alist (alistptr, Qdisplay,
1318 XCAR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
1319
1320 if (f->output_data.x->parent_desc == FRAME_X_DISPLAY_INFO (f)->root_window)
1321 tem = Qnil;
1322 else
1323 XSETFASTINT (tem, f->output_data.x->parent_desc);
1324 store_in_alist (alistptr, Qparent_id, tem);
1325 }
1326 \f
1327
1328
1329 /* Gamma-correct COLOR on frame F. */
1330
1331 void
1332 gamma_correct (f, color)
1333 struct frame *f;
1334 XColor *color;
1335 {
1336 if (f->gamma)
1337 {
1338 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
1339 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
1340 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
1341 }
1342 }
1343
1344
1345 /* Decide if color named COLOR_NAME is valid for use on frame F. If
1346 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
1347 allocate the color. Value is zero if COLOR_NAME is invalid, or
1348 no color could be allocated. */
1349
1350 int
1351 x_defined_color (f, color_name, color, alloc_p)
1352 struct frame *f;
1353 char *color_name;
1354 XColor *color;
1355 int alloc_p;
1356 {
1357 int success_p;
1358 Display *dpy = FRAME_X_DISPLAY (f);
1359 Colormap cmap = FRAME_X_COLORMAP (f);
1360
1361 BLOCK_INPUT;
1362 success_p = XParseColor (dpy, cmap, color_name, color);
1363 if (success_p && alloc_p)
1364 success_p = x_alloc_nearest_color (f, cmap, color);
1365 UNBLOCK_INPUT;
1366
1367 return success_p;
1368 }
1369
1370
1371 /* Return the pixel color value for color COLOR_NAME on frame F. If F
1372 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
1373 Signal an error if color can't be allocated. */
1374
1375 int
1376 x_decode_color (f, color_name, mono_color)
1377 FRAME_PTR f;
1378 Lisp_Object color_name;
1379 int mono_color;
1380 {
1381 XColor cdef;
1382
1383 CHECK_STRING (color_name);
1384
1385 #if 0 /* Don't do this. It's wrong when we're not using the default
1386 colormap, it makes freeing difficult, and it's probably not
1387 an important optimization. */
1388 if (strcmp (SDATA (color_name), "black") == 0)
1389 return BLACK_PIX_DEFAULT (f);
1390 else if (strcmp (SDATA (color_name), "white") == 0)
1391 return WHITE_PIX_DEFAULT (f);
1392 #endif
1393
1394 /* Return MONO_COLOR for monochrome frames. */
1395 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
1396 return mono_color;
1397
1398 /* x_defined_color is responsible for coping with failures
1399 by looking for a near-miss. */
1400 if (x_defined_color (f, SDATA (color_name), &cdef, 1))
1401 return cdef.pixel;
1402
1403 Fsignal (Qerror, Fcons (build_string ("Undefined color"),
1404 Fcons (color_name, Qnil)));
1405 return 0;
1406 }
1407
1408
1409 \f
1410 /* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
1411 the previous value of that parameter, NEW_VALUE is the new value. */
1412
1413 static void
1414 x_set_line_spacing (f, new_value, old_value)
1415 struct frame *f;
1416 Lisp_Object new_value, old_value;
1417 {
1418 if (NILP (new_value))
1419 f->extra_line_spacing = 0;
1420 else if (NATNUMP (new_value))
1421 f->extra_line_spacing = XFASTINT (new_value);
1422 else
1423 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
1424 Fcons (new_value, Qnil)));
1425 if (FRAME_VISIBLE_P (f))
1426 redraw_frame (f);
1427 }
1428
1429
1430 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
1431 the previous value of that parameter, NEW_VALUE is the new value.
1432 See also the comment of wait_for_wm in struct x_output. */
1433
1434 static void
1435 x_set_wait_for_wm (f, new_value, old_value)
1436 struct frame *f;
1437 Lisp_Object new_value, old_value;
1438 {
1439 f->output_data.x->wait_for_wm = !NILP (new_value);
1440 }
1441
1442
1443 /* Change the `fullscreen' frame parameter of frame F. OLD_VALUE is
1444 the previous value of that parameter, NEW_VALUE is the new value. */
1445
1446 static void
1447 x_set_fullscreen (f, new_value, old_value)
1448 struct frame *f;
1449 Lisp_Object new_value, old_value;
1450 {
1451 if (NILP (new_value))
1452 f->output_data.x->want_fullscreen = FULLSCREEN_NONE;
1453 else if (EQ (new_value, Qfullboth))
1454 f->output_data.x->want_fullscreen = FULLSCREEN_BOTH;
1455 else if (EQ (new_value, Qfullwidth))
1456 f->output_data.x->want_fullscreen = FULLSCREEN_WIDTH;
1457 else if (EQ (new_value, Qfullheight))
1458 f->output_data.x->want_fullscreen = FULLSCREEN_HEIGHT;
1459 }
1460
1461
1462 /* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
1463 the previous value of that parameter, NEW_VALUE is the new
1464 value. */
1465
1466 static void
1467 x_set_screen_gamma (f, new_value, old_value)
1468 struct frame *f;
1469 Lisp_Object new_value, old_value;
1470 {
1471 if (NILP (new_value))
1472 f->gamma = 0;
1473 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
1474 /* The value 0.4545 is the normal viewing gamma. */
1475 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
1476 else
1477 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
1478 Fcons (new_value, Qnil)));
1479
1480 clear_face_cache (0);
1481 }
1482
1483
1484 /* Functions called only from `x_set_frame_param'
1485 to set individual parameters.
1486
1487 If FRAME_X_WINDOW (f) is 0,
1488 the frame is being created and its X-window does not exist yet.
1489 In that case, just record the parameter's new value
1490 in the standard place; do not attempt to change the window. */
1491
1492 void
1493 x_set_foreground_color (f, arg, oldval)
1494 struct frame *f;
1495 Lisp_Object arg, oldval;
1496 {
1497 struct x_output *x = f->output_data.x;
1498 unsigned long fg, old_fg;
1499
1500 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1501 old_fg = x->foreground_pixel;
1502 x->foreground_pixel = fg;
1503
1504 if (FRAME_X_WINDOW (f) != 0)
1505 {
1506 Display *dpy = FRAME_X_DISPLAY (f);
1507
1508 BLOCK_INPUT;
1509 XSetForeground (dpy, x->normal_gc, fg);
1510 XSetBackground (dpy, x->reverse_gc, fg);
1511
1512 if (x->cursor_pixel == old_fg)
1513 {
1514 unload_color (f, x->cursor_pixel);
1515 x->cursor_pixel = x_copy_color (f, fg);
1516 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
1517 }
1518
1519 UNBLOCK_INPUT;
1520
1521 update_face_from_frame_parameter (f, Qforeground_color, arg);
1522
1523 if (FRAME_VISIBLE_P (f))
1524 redraw_frame (f);
1525 }
1526
1527 unload_color (f, old_fg);
1528 }
1529
1530 void
1531 x_set_background_color (f, arg, oldval)
1532 struct frame *f;
1533 Lisp_Object arg, oldval;
1534 {
1535 struct x_output *x = f->output_data.x;
1536 unsigned long bg;
1537
1538 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1539 unload_color (f, x->background_pixel);
1540 x->background_pixel = bg;
1541
1542 if (FRAME_X_WINDOW (f) != 0)
1543 {
1544 Display *dpy = FRAME_X_DISPLAY (f);
1545
1546 BLOCK_INPUT;
1547 XSetBackground (dpy, x->normal_gc, bg);
1548 XSetForeground (dpy, x->reverse_gc, bg);
1549 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
1550 XSetForeground (dpy, x->cursor_gc, bg);
1551
1552 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
1553 toolkit scroll bars. */
1554 {
1555 Lisp_Object bar;
1556 for (bar = FRAME_SCROLL_BARS (f);
1557 !NILP (bar);
1558 bar = XSCROLL_BAR (bar)->next)
1559 {
1560 Window window = SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar));
1561 XSetWindowBackground (dpy, window, bg);
1562 }
1563 }
1564 #endif /* USE_TOOLKIT_SCROLL_BARS */
1565
1566 UNBLOCK_INPUT;
1567 update_face_from_frame_parameter (f, Qbackground_color, arg);
1568
1569 if (FRAME_VISIBLE_P (f))
1570 redraw_frame (f);
1571 }
1572 }
1573
1574 void
1575 x_set_mouse_color (f, arg, oldval)
1576 struct frame *f;
1577 Lisp_Object arg, oldval;
1578 {
1579 struct x_output *x = f->output_data.x;
1580 Display *dpy = FRAME_X_DISPLAY (f);
1581 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
1582 Cursor hourglass_cursor, horizontal_drag_cursor;
1583 int count;
1584 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1585 unsigned long mask_color = x->background_pixel;
1586
1587 /* Don't let pointers be invisible. */
1588 if (mask_color == pixel)
1589 {
1590 x_free_colors (f, &pixel, 1);
1591 pixel = x_copy_color (f, x->foreground_pixel);
1592 }
1593
1594 unload_color (f, x->mouse_pixel);
1595 x->mouse_pixel = pixel;
1596
1597 BLOCK_INPUT;
1598
1599 /* It's not okay to crash if the user selects a screwy cursor. */
1600 count = x_catch_errors (dpy);
1601
1602 if (!NILP (Vx_pointer_shape))
1603 {
1604 CHECK_NUMBER (Vx_pointer_shape);
1605 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
1606 }
1607 else
1608 cursor = XCreateFontCursor (dpy, XC_xterm);
1609 x_check_errors (dpy, "bad text pointer cursor: %s");
1610
1611 if (!NILP (Vx_nontext_pointer_shape))
1612 {
1613 CHECK_NUMBER (Vx_nontext_pointer_shape);
1614 nontext_cursor
1615 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
1616 }
1617 else
1618 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
1619 x_check_errors (dpy, "bad nontext pointer cursor: %s");
1620
1621 if (!NILP (Vx_hourglass_pointer_shape))
1622 {
1623 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1624 hourglass_cursor
1625 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
1626 }
1627 else
1628 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
1629 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
1630
1631 x_check_errors (dpy, "bad nontext pointer cursor: %s");
1632 if (!NILP (Vx_mode_pointer_shape))
1633 {
1634 CHECK_NUMBER (Vx_mode_pointer_shape);
1635 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
1636 }
1637 else
1638 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
1639 x_check_errors (dpy, "bad modeline pointer cursor: %s");
1640
1641 if (!NILP (Vx_sensitive_text_pointer_shape))
1642 {
1643 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1644 cross_cursor
1645 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
1646 }
1647 else
1648 cross_cursor = XCreateFontCursor (dpy, XC_hand2);
1649
1650 if (!NILP (Vx_window_horizontal_drag_shape))
1651 {
1652 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1653 horizontal_drag_cursor
1654 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
1655 }
1656 else
1657 horizontal_drag_cursor
1658 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
1659
1660 /* Check and report errors with the above calls. */
1661 x_check_errors (dpy, "can't set cursor shape: %s");
1662 x_uncatch_errors (dpy, count);
1663
1664 {
1665 XColor fore_color, back_color;
1666
1667 fore_color.pixel = x->mouse_pixel;
1668 x_query_color (f, &fore_color);
1669 back_color.pixel = mask_color;
1670 x_query_color (f, &back_color);
1671
1672 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
1673 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
1674 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
1675 XRecolorCursor (dpy, cross_cursor, &fore_color, &back_color);
1676 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
1677 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
1678 }
1679
1680 if (FRAME_X_WINDOW (f) != 0)
1681 XDefineCursor (dpy, FRAME_X_WINDOW (f), cursor);
1682
1683 if (cursor != x->text_cursor
1684 && x->text_cursor != 0)
1685 XFreeCursor (dpy, x->text_cursor);
1686 x->text_cursor = cursor;
1687
1688 if (nontext_cursor != x->nontext_cursor
1689 && x->nontext_cursor != 0)
1690 XFreeCursor (dpy, x->nontext_cursor);
1691 x->nontext_cursor = nontext_cursor;
1692
1693 if (hourglass_cursor != x->hourglass_cursor
1694 && x->hourglass_cursor != 0)
1695 XFreeCursor (dpy, x->hourglass_cursor);
1696 x->hourglass_cursor = hourglass_cursor;
1697
1698 if (mode_cursor != x->modeline_cursor
1699 && x->modeline_cursor != 0)
1700 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
1701 x->modeline_cursor = mode_cursor;
1702
1703 if (cross_cursor != x->cross_cursor
1704 && x->cross_cursor != 0)
1705 XFreeCursor (dpy, x->cross_cursor);
1706 x->cross_cursor = cross_cursor;
1707
1708 if (horizontal_drag_cursor != x->horizontal_drag_cursor
1709 && x->horizontal_drag_cursor != 0)
1710 XFreeCursor (dpy, x->horizontal_drag_cursor);
1711 x->horizontal_drag_cursor = horizontal_drag_cursor;
1712
1713 XFlush (dpy);
1714 UNBLOCK_INPUT;
1715
1716 update_face_from_frame_parameter (f, Qmouse_color, arg);
1717 }
1718
1719 void
1720 x_set_cursor_color (f, arg, oldval)
1721 struct frame *f;
1722 Lisp_Object arg, oldval;
1723 {
1724 unsigned long fore_pixel, pixel;
1725 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
1726 struct x_output *x = f->output_data.x;
1727
1728 if (!NILP (Vx_cursor_fore_pixel))
1729 {
1730 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1731 WHITE_PIX_DEFAULT (f));
1732 fore_pixel_allocated_p = 1;
1733 }
1734 else
1735 fore_pixel = x->background_pixel;
1736
1737 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1738 pixel_allocated_p = 1;
1739
1740 /* Make sure that the cursor color differs from the background color. */
1741 if (pixel == x->background_pixel)
1742 {
1743 if (pixel_allocated_p)
1744 {
1745 x_free_colors (f, &pixel, 1);
1746 pixel_allocated_p = 0;
1747 }
1748
1749 pixel = x->mouse_pixel;
1750 if (pixel == fore_pixel)
1751 {
1752 if (fore_pixel_allocated_p)
1753 {
1754 x_free_colors (f, &fore_pixel, 1);
1755 fore_pixel_allocated_p = 0;
1756 }
1757 fore_pixel = x->background_pixel;
1758 }
1759 }
1760
1761 unload_color (f, x->cursor_foreground_pixel);
1762 if (!fore_pixel_allocated_p)
1763 fore_pixel = x_copy_color (f, fore_pixel);
1764 x->cursor_foreground_pixel = fore_pixel;
1765
1766 unload_color (f, x->cursor_pixel);
1767 if (!pixel_allocated_p)
1768 pixel = x_copy_color (f, pixel);
1769 x->cursor_pixel = pixel;
1770
1771 if (FRAME_X_WINDOW (f) != 0)
1772 {
1773 BLOCK_INPUT;
1774 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1775 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
1776 UNBLOCK_INPUT;
1777
1778 if (FRAME_VISIBLE_P (f))
1779 {
1780 x_update_cursor (f, 0);
1781 x_update_cursor (f, 1);
1782 }
1783 }
1784
1785 update_face_from_frame_parameter (f, Qcursor_color, arg);
1786 }
1787 \f
1788 /* Set the border-color of frame F to value described by ARG.
1789 ARG can be a string naming a color.
1790 The border-color is used for the border that is drawn by the X server.
1791 Note that this does not fully take effect if done before
1792 F has an x-window; it must be redone when the window is created.
1793
1794 Note: this is done in two routines because of the way X10 works.
1795
1796 Note: under X11, this is normally the province of the window manager,
1797 and so emacs' border colors may be overridden. */
1798
1799 void
1800 x_set_border_color (f, arg, oldval)
1801 struct frame *f;
1802 Lisp_Object arg, oldval;
1803 {
1804 int pix;
1805
1806 CHECK_STRING (arg);
1807 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1808 x_set_border_pixel (f, pix);
1809 update_face_from_frame_parameter (f, Qborder_color, arg);
1810 }
1811
1812 /* Set the border-color of frame F to pixel value PIX.
1813 Note that this does not fully take effect if done before
1814 F has an x-window. */
1815
1816 void
1817 x_set_border_pixel (f, pix)
1818 struct frame *f;
1819 int pix;
1820 {
1821 unload_color (f, f->output_data.x->border_pixel);
1822 f->output_data.x->border_pixel = pix;
1823
1824 if (FRAME_X_WINDOW (f) != 0 && f->output_data.x->border_width > 0)
1825 {
1826 BLOCK_INPUT;
1827 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1828 (unsigned long)pix);
1829 UNBLOCK_INPUT;
1830
1831 if (FRAME_VISIBLE_P (f))
1832 redraw_frame (f);
1833 }
1834 }
1835
1836
1837 /* Value is the internal representation of the specified cursor type
1838 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
1839 of the bar cursor. */
1840
1841 enum text_cursor_kinds
1842 x_specified_cursor_type (arg, width)
1843 Lisp_Object arg;
1844 int *width;
1845 {
1846 enum text_cursor_kinds type;
1847
1848 if (EQ (arg, Qbar))
1849 {
1850 type = BAR_CURSOR;
1851 *width = 2;
1852 }
1853 else if (CONSP (arg)
1854 && EQ (XCAR (arg), Qbar)
1855 && INTEGERP (XCDR (arg))
1856 && XINT (XCDR (arg)) >= 0)
1857 {
1858 type = BAR_CURSOR;
1859 *width = XINT (XCDR (arg));
1860 }
1861 else if (EQ (arg, Qhbar))
1862 {
1863 type = HBAR_CURSOR;
1864 *width = 2;
1865 }
1866 else if (CONSP (arg)
1867 && EQ (XCAR (arg), Qhbar)
1868 && INTEGERP (XCDR (arg))
1869 && XINT (XCDR (arg)) >= 0)
1870 {
1871 type = HBAR_CURSOR;
1872 *width = XINT (XCDR (arg));
1873 }
1874 else if (NILP (arg))
1875 type = NO_CURSOR;
1876 else
1877 /* Treat anything unknown as "box cursor".
1878 It was bad to signal an error; people have trouble fixing
1879 .Xdefaults with Emacs, when it has something bad in it. */
1880 type = FILLED_BOX_CURSOR;
1881
1882 return type;
1883 }
1884
1885 void
1886 x_set_cursor_type (f, arg, oldval)
1887 FRAME_PTR f;
1888 Lisp_Object arg, oldval;
1889 {
1890 int width;
1891
1892 FRAME_DESIRED_CURSOR (f) = x_specified_cursor_type (arg, &width);
1893 f->output_data.x->cursor_width = width;
1894
1895 /* Make sure the cursor gets redrawn. */
1896 cursor_type_changed = 1;
1897 }
1898 \f
1899 void
1900 x_set_icon_type (f, arg, oldval)
1901 struct frame *f;
1902 Lisp_Object arg, oldval;
1903 {
1904 int result;
1905
1906 if (STRINGP (arg))
1907 {
1908 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1909 return;
1910 }
1911 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1912 return;
1913
1914 BLOCK_INPUT;
1915 if (NILP (arg))
1916 result = x_text_icon (f,
1917 (char *) SDATA ((!NILP (f->icon_name)
1918 ? f->icon_name
1919 : f->name)));
1920 else
1921 result = x_bitmap_icon (f, arg);
1922
1923 if (result)
1924 {
1925 UNBLOCK_INPUT;
1926 error ("No icon window available");
1927 }
1928
1929 XFlush (FRAME_X_DISPLAY (f));
1930 UNBLOCK_INPUT;
1931 }
1932
1933 /* Return non-nil if frame F wants a bitmap icon. */
1934
1935 Lisp_Object
1936 x_icon_type (f)
1937 FRAME_PTR f;
1938 {
1939 Lisp_Object tem;
1940
1941 tem = assq_no_quit (Qicon_type, f->param_alist);
1942 if (CONSP (tem))
1943 return XCDR (tem);
1944 else
1945 return Qnil;
1946 }
1947
1948 void
1949 x_set_icon_name (f, arg, oldval)
1950 struct frame *f;
1951 Lisp_Object arg, oldval;
1952 {
1953 int result;
1954
1955 if (STRINGP (arg))
1956 {
1957 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1958 return;
1959 }
1960 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1961 return;
1962
1963 f->icon_name = arg;
1964
1965 if (f->output_data.x->icon_bitmap != 0)
1966 return;
1967
1968 BLOCK_INPUT;
1969
1970 result = x_text_icon (f,
1971 (char *) SDATA ((!NILP (f->icon_name)
1972 ? f->icon_name
1973 : !NILP (f->title)
1974 ? f->title
1975 : f->name)));
1976
1977 if (result)
1978 {
1979 UNBLOCK_INPUT;
1980 error ("No icon window available");
1981 }
1982
1983 XFlush (FRAME_X_DISPLAY (f));
1984 UNBLOCK_INPUT;
1985 }
1986 \f
1987 void
1988 x_set_font (f, arg, oldval)
1989 struct frame *f;
1990 Lisp_Object arg, oldval;
1991 {
1992 Lisp_Object result;
1993 Lisp_Object fontset_name;
1994 Lisp_Object frame;
1995 int old_fontset = f->output_data.x->fontset;
1996
1997 CHECK_STRING (arg);
1998
1999 fontset_name = Fquery_fontset (arg, Qnil);
2000
2001 BLOCK_INPUT;
2002 result = (STRINGP (fontset_name)
2003 ? x_new_fontset (f, SDATA (fontset_name))
2004 : x_new_font (f, SDATA (arg)));
2005 UNBLOCK_INPUT;
2006
2007 if (EQ (result, Qnil))
2008 error ("Font `%s' is not defined", SDATA (arg));
2009 else if (EQ (result, Qt))
2010 error ("The characters of the given font have varying widths");
2011 else if (STRINGP (result))
2012 {
2013 if (STRINGP (fontset_name))
2014 {
2015 /* Fontset names are built from ASCII font names, so the
2016 names may be equal despite there was a change. */
2017 if (old_fontset == f->output_data.x->fontset)
2018 return;
2019 }
2020 else if (!NILP (Fequal (result, oldval)))
2021 return;
2022
2023 store_frame_param (f, Qfont, result);
2024 recompute_basic_faces (f);
2025 }
2026 else
2027 abort ();
2028
2029 do_pending_window_change (0);
2030
2031 /* Don't call `face-set-after-frame-default' when faces haven't been
2032 initialized yet. This is the case when called from
2033 Fx_create_frame. In that case, the X widget or window doesn't
2034 exist either, and we can end up in x_report_frame_params with a
2035 null widget which gives a segfault. */
2036 if (FRAME_FACE_CACHE (f))
2037 {
2038 XSETFRAME (frame, f);
2039 call1 (Qface_set_after_frame_default, frame);
2040 }
2041 }
2042
2043 static void
2044 x_set_fringe_width (f, new_value, old_value)
2045 struct frame *f;
2046 Lisp_Object new_value, old_value;
2047 {
2048 x_compute_fringe_widths (f, 1);
2049 }
2050
2051 void
2052 x_set_border_width (f, arg, oldval)
2053 struct frame *f;
2054 Lisp_Object arg, oldval;
2055 {
2056 CHECK_NUMBER (arg);
2057
2058 if (XINT (arg) == f->output_data.x->border_width)
2059 return;
2060
2061 if (FRAME_X_WINDOW (f) != 0)
2062 error ("Cannot change the border width of a window");
2063
2064 f->output_data.x->border_width = XINT (arg);
2065 }
2066
2067 void
2068 x_set_internal_border_width (f, arg, oldval)
2069 struct frame *f;
2070 Lisp_Object arg, oldval;
2071 {
2072 int old = f->output_data.x->internal_border_width;
2073
2074 CHECK_NUMBER (arg);
2075 f->output_data.x->internal_border_width = XINT (arg);
2076 if (f->output_data.x->internal_border_width < 0)
2077 f->output_data.x->internal_border_width = 0;
2078
2079 #ifdef USE_X_TOOLKIT
2080 if (f->output_data.x->edit_widget)
2081 widget_store_internal_border (f->output_data.x->edit_widget);
2082 #endif
2083
2084 if (f->output_data.x->internal_border_width == old)
2085 return;
2086
2087 if (FRAME_X_WINDOW (f) != 0)
2088 {
2089 x_set_window_size (f, 0, f->width, f->height);
2090 SET_FRAME_GARBAGED (f);
2091 do_pending_window_change (0);
2092 }
2093 else
2094 SET_FRAME_GARBAGED (f);
2095 }
2096
2097 void
2098 x_set_visibility (f, value, oldval)
2099 struct frame *f;
2100 Lisp_Object value, oldval;
2101 {
2102 Lisp_Object frame;
2103 XSETFRAME (frame, f);
2104
2105 if (NILP (value))
2106 Fmake_frame_invisible (frame, Qt);
2107 else if (EQ (value, Qicon))
2108 Ficonify_frame (frame);
2109 else
2110 Fmake_frame_visible (frame);
2111 }
2112
2113 \f
2114 /* Change window heights in windows rooted in WINDOW by N lines. */
2115
2116 static void
2117 x_change_window_heights (window, n)
2118 Lisp_Object window;
2119 int n;
2120 {
2121 struct window *w = XWINDOW (window);
2122
2123 XSETFASTINT (w->top, XFASTINT (w->top) + n);
2124 XSETFASTINT (w->height, XFASTINT (w->height) - n);
2125
2126 if (INTEGERP (w->orig_top))
2127 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
2128 if (INTEGERP (w->orig_height))
2129 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
2130
2131 /* Handle just the top child in a vertical split. */
2132 if (!NILP (w->vchild))
2133 x_change_window_heights (w->vchild, n);
2134
2135 /* Adjust all children in a horizontal split. */
2136 for (window = w->hchild; !NILP (window); window = w->next)
2137 {
2138 w = XWINDOW (window);
2139 x_change_window_heights (window, n);
2140 }
2141 }
2142
2143 void
2144 x_set_menu_bar_lines (f, value, oldval)
2145 struct frame *f;
2146 Lisp_Object value, oldval;
2147 {
2148 int nlines;
2149 #ifndef USE_X_TOOLKIT
2150 int olines = FRAME_MENU_BAR_LINES (f);
2151 #endif
2152
2153 /* Right now, menu bars don't work properly in minibuf-only frames;
2154 most of the commands try to apply themselves to the minibuffer
2155 frame itself, and get an error because you can't switch buffers
2156 in or split the minibuffer window. */
2157 if (FRAME_MINIBUF_ONLY_P (f))
2158 return;
2159
2160 if (INTEGERP (value))
2161 nlines = XINT (value);
2162 else
2163 nlines = 0;
2164
2165 /* Make sure we redisplay all windows in this frame. */
2166 windows_or_buffers_changed++;
2167
2168 #ifdef USE_X_TOOLKIT
2169 FRAME_MENU_BAR_LINES (f) = 0;
2170 if (nlines)
2171 {
2172 FRAME_EXTERNAL_MENU_BAR (f) = 1;
2173 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
2174 /* Make sure next redisplay shows the menu bar. */
2175 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
2176 }
2177 else
2178 {
2179 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2180 free_frame_menubar (f);
2181 FRAME_EXTERNAL_MENU_BAR (f) = 0;
2182 if (FRAME_X_P (f))
2183 f->output_data.x->menubar_widget = 0;
2184 }
2185 #else /* not USE_X_TOOLKIT */
2186 FRAME_MENU_BAR_LINES (f) = nlines;
2187 x_change_window_heights (f->root_window, nlines - olines);
2188 #endif /* not USE_X_TOOLKIT */
2189 adjust_glyphs (f);
2190 }
2191
2192
2193 /* Set the number of lines used for the tool bar of frame F to VALUE.
2194 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2195 is the old number of tool bar lines. This function changes the
2196 height of all windows on frame F to match the new tool bar height.
2197 The frame's height doesn't change. */
2198
2199 void
2200 x_set_tool_bar_lines (f, value, oldval)
2201 struct frame *f;
2202 Lisp_Object value, oldval;
2203 {
2204 int delta, nlines, root_height;
2205 Lisp_Object root_window;
2206
2207 /* Treat tool bars like menu bars. */
2208 if (FRAME_MINIBUF_ONLY_P (f))
2209 return;
2210
2211 /* Use VALUE only if an integer >= 0. */
2212 if (INTEGERP (value) && XINT (value) >= 0)
2213 nlines = XFASTINT (value);
2214 else
2215 nlines = 0;
2216
2217 /* Make sure we redisplay all windows in this frame. */
2218 ++windows_or_buffers_changed;
2219
2220 delta = nlines - FRAME_TOOL_BAR_LINES (f);
2221
2222 /* Don't resize the tool-bar to more than we have room for. */
2223 root_window = FRAME_ROOT_WINDOW (f);
2224 root_height = XINT (XWINDOW (root_window)->height);
2225 if (root_height - delta < 1)
2226 {
2227 delta = root_height - 1;
2228 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2229 }
2230
2231 FRAME_TOOL_BAR_LINES (f) = nlines;
2232 x_change_window_heights (root_window, delta);
2233 adjust_glyphs (f);
2234
2235 /* We also have to make sure that the internal border at the top of
2236 the frame, below the menu bar or tool bar, is redrawn when the
2237 tool bar disappears. This is so because the internal border is
2238 below the tool bar if one is displayed, but is below the menu bar
2239 if there isn't a tool bar. The tool bar draws into the area
2240 below the menu bar. */
2241 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2242 {
2243 updating_frame = f;
2244 clear_frame ();
2245 clear_current_matrices (f);
2246 updating_frame = NULL;
2247 }
2248
2249 /* If the tool bar gets smaller, the internal border below it
2250 has to be cleared. It was formerly part of the display
2251 of the larger tool bar, and updating windows won't clear it. */
2252 if (delta < 0)
2253 {
2254 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2255 int width = PIXEL_WIDTH (f);
2256 int y = nlines * CANON_Y_UNIT (f);
2257
2258 BLOCK_INPUT;
2259 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2260 0, y, width, height, False);
2261 UNBLOCK_INPUT;
2262
2263 if (WINDOWP (f->tool_bar_window))
2264 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
2265 }
2266 }
2267
2268
2269 /* Set the foreground color for scroll bars on frame F to VALUE.
2270 VALUE should be a string, a color name. If it isn't a string or
2271 isn't a valid color name, do nothing. OLDVAL is the old value of
2272 the frame parameter. */
2273
2274 void
2275 x_set_scroll_bar_foreground (f, value, oldval)
2276 struct frame *f;
2277 Lisp_Object value, oldval;
2278 {
2279 unsigned long pixel;
2280
2281 if (STRINGP (value))
2282 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
2283 else
2284 pixel = -1;
2285
2286 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
2287 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
2288
2289 f->output_data.x->scroll_bar_foreground_pixel = pixel;
2290 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2291 {
2292 /* Remove all scroll bars because they have wrong colors. */
2293 if (condemn_scroll_bars_hook)
2294 (*condemn_scroll_bars_hook) (f);
2295 if (judge_scroll_bars_hook)
2296 (*judge_scroll_bars_hook) (f);
2297
2298 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
2299 redraw_frame (f);
2300 }
2301 }
2302
2303
2304 /* Set the background color for scroll bars on frame F to VALUE VALUE
2305 should be a string, a color name. If it isn't a string or isn't a
2306 valid color name, do nothing. OLDVAL is the old value of the frame
2307 parameter. */
2308
2309 void
2310 x_set_scroll_bar_background (f, value, oldval)
2311 struct frame *f;
2312 Lisp_Object value, oldval;
2313 {
2314 unsigned long pixel;
2315
2316 if (STRINGP (value))
2317 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
2318 else
2319 pixel = -1;
2320
2321 if (f->output_data.x->scroll_bar_background_pixel != -1)
2322 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
2323
2324 #ifdef USE_TOOLKIT_SCROLL_BARS
2325 /* Scrollbar shadow colors. */
2326 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
2327 {
2328 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
2329 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2330 }
2331 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
2332 {
2333 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
2334 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2335 }
2336 #endif /* USE_TOOLKIT_SCROLL_BARS */
2337
2338 f->output_data.x->scroll_bar_background_pixel = pixel;
2339 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2340 {
2341 /* Remove all scroll bars because they have wrong colors. */
2342 if (condemn_scroll_bars_hook)
2343 (*condemn_scroll_bars_hook) (f);
2344 if (judge_scroll_bars_hook)
2345 (*judge_scroll_bars_hook) (f);
2346
2347 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
2348 redraw_frame (f);
2349 }
2350 }
2351
2352 \f
2353 /* Encode Lisp string STRING as a text in a format appropriate for
2354 XICCC (X Inter Client Communication Conventions).
2355
2356 If STRING contains only ASCII characters, do no conversion and
2357 return the string data of STRING. Otherwise, encode the text by
2358 CODING_SYSTEM, and return a newly allocated memory area which
2359 should be freed by `xfree' by a caller.
2360
2361 SELECTIONP non-zero means the string is being encoded for an X
2362 selection, so it is safe to run pre-write conversions (which
2363 may run Lisp code).
2364
2365 Store the byte length of resulting text in *TEXT_BYTES.
2366
2367 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
2368 which means that the `encoding' of the result can be `STRING'.
2369 Otherwise store 0 in *STRINGP, which means that the `encoding' of
2370 the result should be `COMPOUND_TEXT'. */
2371
2372 unsigned char *
2373 x_encode_text (string, coding_system, selectionp, text_bytes, stringp)
2374 Lisp_Object string, coding_system;
2375 int *text_bytes, *stringp;
2376 int selectionp;
2377 {
2378 unsigned char *str = SDATA (string);
2379 int chars = SCHARS (string);
2380 int bytes = SBYTES (string);
2381 int charset_info;
2382 int bufsize;
2383 unsigned char *buf;
2384 struct coding_system coding;
2385 extern Lisp_Object Qcompound_text_with_extensions;
2386
2387 charset_info = find_charset_in_text (str, chars, bytes, NULL, Qnil);
2388 if (charset_info == 0)
2389 {
2390 /* No multibyte character in OBJ. We need not encode it. */
2391 *text_bytes = bytes;
2392 *stringp = 1;
2393 return str;
2394 }
2395
2396 setup_coding_system (coding_system, &coding);
2397 if (selectionp
2398 && SYMBOLP (coding.pre_write_conversion)
2399 && !NILP (Ffboundp (coding.pre_write_conversion)))
2400 {
2401 string = run_pre_post_conversion_on_str (string, &coding, 1);
2402 str = SDATA (string);
2403 chars = SCHARS (string);
2404 bytes = SBYTES (string);
2405 }
2406 coding.src_multibyte = 1;
2407 coding.dst_multibyte = 0;
2408 coding.mode |= CODING_MODE_LAST_BLOCK;
2409 if (coding.type == coding_type_iso2022)
2410 coding.flags |= CODING_FLAG_ISO_SAFE;
2411 /* We suppress producing escape sequences for composition. */
2412 coding.composing = COMPOSITION_DISABLED;
2413 bufsize = encoding_buffer_size (&coding, bytes);
2414 buf = (unsigned char *) xmalloc (bufsize);
2415 encode_coding (&coding, str, buf, bytes, bufsize);
2416 *text_bytes = coding.produced;
2417 *stringp = (charset_info == 1
2418 || (!EQ (coding_system, Qcompound_text)
2419 && !EQ (coding_system, Qcompound_text_with_extensions)));
2420 return buf;
2421 }
2422
2423 \f
2424 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
2425 x_id_name.
2426
2427 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2428 name; if NAME is a string, set F's name to NAME and set
2429 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2430
2431 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2432 suggesting a new name, which lisp code should override; if
2433 F->explicit_name is set, ignore the new name; otherwise, set it. */
2434
2435 void
2436 x_set_name (f, name, explicit)
2437 struct frame *f;
2438 Lisp_Object name;
2439 int explicit;
2440 {
2441 /* Make sure that requests from lisp code override requests from
2442 Emacs redisplay code. */
2443 if (explicit)
2444 {
2445 /* If we're switching from explicit to implicit, we had better
2446 update the mode lines and thereby update the title. */
2447 if (f->explicit_name && NILP (name))
2448 update_mode_lines = 1;
2449
2450 f->explicit_name = ! NILP (name);
2451 }
2452 else if (f->explicit_name)
2453 return;
2454
2455 /* If NAME is nil, set the name to the x_id_name. */
2456 if (NILP (name))
2457 {
2458 /* Check for no change needed in this very common case
2459 before we do any consing. */
2460 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
2461 SDATA (f->name)))
2462 return;
2463 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
2464 }
2465 else
2466 CHECK_STRING (name);
2467
2468 /* Don't change the name if it's already NAME. */
2469 if (! NILP (Fstring_equal (name, f->name)))
2470 return;
2471
2472 f->name = name;
2473
2474 /* For setting the frame title, the title parameter should override
2475 the name parameter. */
2476 if (! NILP (f->title))
2477 name = f->title;
2478
2479 if (FRAME_X_WINDOW (f))
2480 {
2481 BLOCK_INPUT;
2482 #ifdef HAVE_X11R4
2483 {
2484 XTextProperty text, icon;
2485 int bytes, stringp;
2486 Lisp_Object coding_system;
2487
2488 coding_system = Vlocale_coding_system;
2489 if (NILP (coding_system))
2490 coding_system = Qcompound_text;
2491 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp);
2492 text.encoding = (stringp ? XA_STRING
2493 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2494 text.format = 8;
2495 text.nitems = bytes;
2496
2497 if (NILP (f->icon_name))
2498 {
2499 icon = text;
2500 }
2501 else
2502 {
2503 icon.value = x_encode_text (f->icon_name, coding_system, 0,
2504 &bytes, &stringp);
2505 icon.encoding = (stringp ? XA_STRING
2506 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2507 icon.format = 8;
2508 icon.nitems = bytes;
2509 }
2510 #ifdef USE_X_TOOLKIT
2511 XSetWMName (FRAME_X_DISPLAY (f),
2512 XtWindow (f->output_data.x->widget), &text);
2513 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
2514 &icon);
2515 #else /* not USE_X_TOOLKIT */
2516 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
2517 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
2518 #endif /* not USE_X_TOOLKIT */
2519 if (!NILP (f->icon_name)
2520 && icon.value != SDATA (f->icon_name))
2521 xfree (icon.value);
2522 if (text.value != SDATA (name))
2523 xfree (text.value);
2524 }
2525 #else /* not HAVE_X11R4 */
2526 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2527 SDATA (name));
2528 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2529 SDATA (name));
2530 #endif /* not HAVE_X11R4 */
2531 UNBLOCK_INPUT;
2532 }
2533 }
2534
2535 /* This function should be called when the user's lisp code has
2536 specified a name for the frame; the name will override any set by the
2537 redisplay code. */
2538 void
2539 x_explicitly_set_name (f, arg, oldval)
2540 FRAME_PTR f;
2541 Lisp_Object arg, oldval;
2542 {
2543 x_set_name (f, arg, 1);
2544 }
2545
2546 /* This function should be called by Emacs redisplay code to set the
2547 name; names set this way will never override names set by the user's
2548 lisp code. */
2549 void
2550 x_implicitly_set_name (f, arg, oldval)
2551 FRAME_PTR f;
2552 Lisp_Object arg, oldval;
2553 {
2554 x_set_name (f, arg, 0);
2555 }
2556 \f
2557 /* Change the title of frame F to NAME.
2558 If NAME is nil, use the frame name as the title.
2559
2560 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2561 name; if NAME is a string, set F's name to NAME and set
2562 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2563
2564 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2565 suggesting a new name, which lisp code should override; if
2566 F->explicit_name is set, ignore the new name; otherwise, set it. */
2567
2568 void
2569 x_set_title (f, name, old_name)
2570 struct frame *f;
2571 Lisp_Object name, old_name;
2572 {
2573 /* Don't change the title if it's already NAME. */
2574 if (EQ (name, f->title))
2575 return;
2576
2577 update_mode_lines = 1;
2578
2579 f->title = name;
2580
2581 if (NILP (name))
2582 name = f->name;
2583 else
2584 CHECK_STRING (name);
2585
2586 if (FRAME_X_WINDOW (f))
2587 {
2588 BLOCK_INPUT;
2589 #ifdef HAVE_X11R4
2590 {
2591 XTextProperty text, icon;
2592 int bytes, stringp;
2593 Lisp_Object coding_system;
2594
2595 coding_system = Vlocale_coding_system;
2596 if (NILP (coding_system))
2597 coding_system = Qcompound_text;
2598 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp);
2599 text.encoding = (stringp ? XA_STRING
2600 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2601 text.format = 8;
2602 text.nitems = bytes;
2603
2604 if (NILP (f->icon_name))
2605 {
2606 icon = text;
2607 }
2608 else
2609 {
2610 icon.value = x_encode_text (f->icon_name, coding_system, 0,
2611 &bytes, &stringp);
2612 icon.encoding = (stringp ? XA_STRING
2613 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2614 icon.format = 8;
2615 icon.nitems = bytes;
2616 }
2617 #ifdef USE_X_TOOLKIT
2618 XSetWMName (FRAME_X_DISPLAY (f),
2619 XtWindow (f->output_data.x->widget), &text);
2620 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
2621 &icon);
2622 #else /* not USE_X_TOOLKIT */
2623 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
2624 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
2625 #endif /* not USE_X_TOOLKIT */
2626 if (!NILP (f->icon_name)
2627 && icon.value != SDATA (f->icon_name))
2628 xfree (icon.value);
2629 if (text.value != SDATA (name))
2630 xfree (text.value);
2631 }
2632 #else /* not HAVE_X11R4 */
2633 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2634 SDATA (name));
2635 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2636 SDATA (name));
2637 #endif /* not HAVE_X11R4 */
2638 UNBLOCK_INPUT;
2639 }
2640 }
2641 \f
2642 void
2643 x_set_autoraise (f, arg, oldval)
2644 struct frame *f;
2645 Lisp_Object arg, oldval;
2646 {
2647 f->auto_raise = !EQ (Qnil, arg);
2648 }
2649
2650 void
2651 x_set_autolower (f, arg, oldval)
2652 struct frame *f;
2653 Lisp_Object arg, oldval;
2654 {
2655 f->auto_lower = !EQ (Qnil, arg);
2656 }
2657
2658 void
2659 x_set_unsplittable (f, arg, oldval)
2660 struct frame *f;
2661 Lisp_Object arg, oldval;
2662 {
2663 f->no_split = !NILP (arg);
2664 }
2665
2666 void
2667 x_set_vertical_scroll_bars (f, arg, oldval)
2668 struct frame *f;
2669 Lisp_Object arg, oldval;
2670 {
2671 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2672 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2673 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2674 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
2675 {
2676 FRAME_VERTICAL_SCROLL_BAR_TYPE (f)
2677 = (NILP (arg)
2678 ? vertical_scroll_bar_none
2679 : EQ (Qright, arg)
2680 ? vertical_scroll_bar_right
2681 : vertical_scroll_bar_left);
2682
2683 /* We set this parameter before creating the X window for the
2684 frame, so we can get the geometry right from the start.
2685 However, if the window hasn't been created yet, we shouldn't
2686 call x_set_window_size. */
2687 if (FRAME_X_WINDOW (f))
2688 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2689 do_pending_window_change (0);
2690 }
2691 }
2692
2693 void
2694 x_set_scroll_bar_width (f, arg, oldval)
2695 struct frame *f;
2696 Lisp_Object arg, oldval;
2697 {
2698 int wid = FONT_WIDTH (f->output_data.x->font);
2699
2700 if (NILP (arg))
2701 {
2702 #ifdef USE_TOOLKIT_SCROLL_BARS
2703 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
2704 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2705 FRAME_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
2706 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = width;
2707 #else
2708 /* Make the actual width at least 14 pixels and a multiple of a
2709 character width. */
2710 FRAME_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
2711
2712 /* Use all of that space (aside from required margins) for the
2713 scroll bar. */
2714 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = 0;
2715 #endif
2716
2717 if (FRAME_X_WINDOW (f))
2718 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2719 do_pending_window_change (0);
2720 }
2721 else if (INTEGERP (arg) && XINT (arg) > 0
2722 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
2723 {
2724 if (XFASTINT (arg) <= 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM)
2725 XSETINT (arg, 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM + 1);
2726
2727 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
2728 FRAME_SCROLL_BAR_COLS (f) = (XFASTINT (arg) + wid-1) / wid;
2729 if (FRAME_X_WINDOW (f))
2730 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2731 }
2732
2733 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
2734 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2735 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
2736 }
2737
2738
2739 \f
2740 /* Subroutines of creating an X frame. */
2741
2742 /* Make sure that Vx_resource_name is set to a reasonable value.
2743 Fix it up, or set it to `emacs' if it is too hopeless. */
2744
2745 static void
2746 validate_x_resource_name ()
2747 {
2748 int len = 0;
2749 /* Number of valid characters in the resource name. */
2750 int good_count = 0;
2751 /* Number of invalid characters in the resource name. */
2752 int bad_count = 0;
2753 Lisp_Object new;
2754 int i;
2755
2756 if (!STRINGP (Vx_resource_class))
2757 Vx_resource_class = build_string (EMACS_CLASS);
2758
2759 if (STRINGP (Vx_resource_name))
2760 {
2761 unsigned char *p = SDATA (Vx_resource_name);
2762 int i;
2763
2764 len = SBYTES (Vx_resource_name);
2765
2766 /* Only letters, digits, - and _ are valid in resource names.
2767 Count the valid characters and count the invalid ones. */
2768 for (i = 0; i < len; i++)
2769 {
2770 int c = p[i];
2771 if (! ((c >= 'a' && c <= 'z')
2772 || (c >= 'A' && c <= 'Z')
2773 || (c >= '0' && c <= '9')
2774 || c == '-' || c == '_'))
2775 bad_count++;
2776 else
2777 good_count++;
2778 }
2779 }
2780 else
2781 /* Not a string => completely invalid. */
2782 bad_count = 5, good_count = 0;
2783
2784 /* If name is valid already, return. */
2785 if (bad_count == 0)
2786 return;
2787
2788 /* If name is entirely invalid, or nearly so, use `emacs'. */
2789 if (good_count == 0
2790 || (good_count == 1 && bad_count > 0))
2791 {
2792 Vx_resource_name = build_string ("emacs");
2793 return;
2794 }
2795
2796 /* Name is partly valid. Copy it and replace the invalid characters
2797 with underscores. */
2798
2799 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2800
2801 for (i = 0; i < len; i++)
2802 {
2803 int c = SREF (new, i);
2804 if (! ((c >= 'a' && c <= 'z')
2805 || (c >= 'A' && c <= 'Z')
2806 || (c >= '0' && c <= '9')
2807 || c == '-' || c == '_'))
2808 SSET (new, i, '_');
2809 }
2810 }
2811
2812
2813 extern char *x_get_string_resource ();
2814
2815 DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
2816 doc: /* Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.
2817 This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the
2818 class, where INSTANCE is the name under which Emacs was invoked, or
2819 the name specified by the `-name' or `-rn' command-line arguments.
2820
2821 The optional arguments COMPONENT and SUBCLASS add to the key and the
2822 class, respectively. You must specify both of them or neither.
2823 If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'
2824 and the class is `Emacs.CLASS.SUBCLASS'. */)
2825 (attribute, class, component, subclass)
2826 Lisp_Object attribute, class, component, subclass;
2827 {
2828 register char *value;
2829 char *name_key;
2830 char *class_key;
2831
2832 check_x ();
2833
2834 CHECK_STRING (attribute);
2835 CHECK_STRING (class);
2836
2837 if (!NILP (component))
2838 CHECK_STRING (component);
2839 if (!NILP (subclass))
2840 CHECK_STRING (subclass);
2841 if (NILP (component) != NILP (subclass))
2842 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2843
2844 validate_x_resource_name ();
2845
2846 /* Allocate space for the components, the dots which separate them,
2847 and the final '\0'. Make them big enough for the worst case. */
2848 name_key = (char *) alloca (SBYTES (Vx_resource_name)
2849 + (STRINGP (component)
2850 ? SBYTES (component) : 0)
2851 + SBYTES (attribute)
2852 + 3);
2853
2854 class_key = (char *) alloca (SBYTES (Vx_resource_class)
2855 + SBYTES (class)
2856 + (STRINGP (subclass)
2857 ? SBYTES (subclass) : 0)
2858 + 3);
2859
2860 /* Start with emacs.FRAMENAME for the name (the specific one)
2861 and with `Emacs' for the class key (the general one). */
2862 strcpy (name_key, SDATA (Vx_resource_name));
2863 strcpy (class_key, SDATA (Vx_resource_class));
2864
2865 strcat (class_key, ".");
2866 strcat (class_key, SDATA (class));
2867
2868 if (!NILP (component))
2869 {
2870 strcat (class_key, ".");
2871 strcat (class_key, SDATA (subclass));
2872
2873 strcat (name_key, ".");
2874 strcat (name_key, SDATA (component));
2875 }
2876
2877 strcat (name_key, ".");
2878 strcat (name_key, SDATA (attribute));
2879
2880 value = x_get_string_resource (check_x_display_info (Qnil)->xrdb,
2881 name_key, class_key);
2882
2883 if (value != (char *) 0)
2884 return build_string (value);
2885 else
2886 return Qnil;
2887 }
2888
2889 /* Get an X resource, like Fx_get_resource, but for display DPYINFO. */
2890
2891 Lisp_Object
2892 display_x_get_resource (dpyinfo, attribute, class, component, subclass)
2893 struct x_display_info *dpyinfo;
2894 Lisp_Object attribute, class, component, subclass;
2895 {
2896 register char *value;
2897 char *name_key;
2898 char *class_key;
2899
2900 CHECK_STRING (attribute);
2901 CHECK_STRING (class);
2902
2903 if (!NILP (component))
2904 CHECK_STRING (component);
2905 if (!NILP (subclass))
2906 CHECK_STRING (subclass);
2907 if (NILP (component) != NILP (subclass))
2908 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2909
2910 validate_x_resource_name ();
2911
2912 /* Allocate space for the components, the dots which separate them,
2913 and the final '\0'. Make them big enough for the worst case. */
2914 name_key = (char *) alloca (SBYTES (Vx_resource_name)
2915 + (STRINGP (component)
2916 ? SBYTES (component) : 0)
2917 + SBYTES (attribute)
2918 + 3);
2919
2920 class_key = (char *) alloca (SBYTES (Vx_resource_class)
2921 + SBYTES (class)
2922 + (STRINGP (subclass)
2923 ? SBYTES (subclass) : 0)
2924 + 3);
2925
2926 /* Start with emacs.FRAMENAME for the name (the specific one)
2927 and with `Emacs' for the class key (the general one). */
2928 strcpy (name_key, SDATA (Vx_resource_name));
2929 strcpy (class_key, SDATA (Vx_resource_class));
2930
2931 strcat (class_key, ".");
2932 strcat (class_key, SDATA (class));
2933
2934 if (!NILP (component))
2935 {
2936 strcat (class_key, ".");
2937 strcat (class_key, SDATA (subclass));
2938
2939 strcat (name_key, ".");
2940 strcat (name_key, SDATA (component));
2941 }
2942
2943 strcat (name_key, ".");
2944 strcat (name_key, SDATA (attribute));
2945
2946 value = x_get_string_resource (dpyinfo->xrdb, name_key, class_key);
2947
2948 if (value != (char *) 0)
2949 return build_string (value);
2950 else
2951 return Qnil;
2952 }
2953
2954 /* Used when C code wants a resource value. */
2955
2956 char *
2957 x_get_resource_string (attribute, class)
2958 char *attribute, *class;
2959 {
2960 char *name_key;
2961 char *class_key;
2962 struct frame *sf = SELECTED_FRAME ();
2963
2964 /* Allocate space for the components, the dots which separate them,
2965 and the final '\0'. */
2966 name_key = (char *) alloca (SBYTES (Vinvocation_name)
2967 + strlen (attribute) + 2);
2968 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
2969 + strlen (class) + 2);
2970
2971 sprintf (name_key, "%s.%s",
2972 SDATA (Vinvocation_name),
2973 attribute);
2974 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
2975
2976 return x_get_string_resource (FRAME_X_DISPLAY_INFO (sf)->xrdb,
2977 name_key, class_key);
2978 }
2979
2980 /* Types we might convert a resource string into. */
2981 enum resource_types
2982 {
2983 RES_TYPE_NUMBER,
2984 RES_TYPE_FLOAT,
2985 RES_TYPE_BOOLEAN,
2986 RES_TYPE_STRING,
2987 RES_TYPE_SYMBOL
2988 };
2989
2990 /* Return the value of parameter PARAM.
2991
2992 First search ALIST, then Vdefault_frame_alist, then the X defaults
2993 database, using ATTRIBUTE as the attribute name and CLASS as its class.
2994
2995 Convert the resource to the type specified by desired_type.
2996
2997 If no default is specified, return Qunbound. If you call
2998 x_get_arg, make sure you deal with Qunbound in a reasonable way,
2999 and don't let it get stored in any Lisp-visible variables! */
3000
3001 static Lisp_Object
3002 x_get_arg (dpyinfo, alist, param, attribute, class, type)
3003 struct x_display_info *dpyinfo;
3004 Lisp_Object alist, param;
3005 char *attribute;
3006 char *class;
3007 enum resource_types type;
3008 {
3009 register Lisp_Object tem;
3010
3011 tem = Fassq (param, alist);
3012 if (EQ (tem, Qnil))
3013 tem = Fassq (param, Vdefault_frame_alist);
3014 if (EQ (tem, Qnil))
3015 {
3016
3017 if (attribute)
3018 {
3019 tem = display_x_get_resource (dpyinfo,
3020 build_string (attribute),
3021 build_string (class),
3022 Qnil, Qnil);
3023
3024 if (NILP (tem))
3025 return Qunbound;
3026
3027 switch (type)
3028 {
3029 case RES_TYPE_NUMBER:
3030 return make_number (atoi (SDATA (tem)));
3031
3032 case RES_TYPE_FLOAT:
3033 return make_float (atof (SDATA (tem)));
3034
3035 case RES_TYPE_BOOLEAN:
3036 tem = Fdowncase (tem);
3037 if (!strcmp (SDATA (tem), "on")
3038 || !strcmp (SDATA (tem), "true"))
3039 return Qt;
3040 else
3041 return Qnil;
3042
3043 case RES_TYPE_STRING:
3044 return tem;
3045
3046 case RES_TYPE_SYMBOL:
3047 /* As a special case, we map the values `true' and `on'
3048 to Qt, and `false' and `off' to Qnil. */
3049 {
3050 Lisp_Object lower;
3051 lower = Fdowncase (tem);
3052 if (!strcmp (SDATA (lower), "on")
3053 || !strcmp (SDATA (lower), "true"))
3054 return Qt;
3055 else if (!strcmp (SDATA (lower), "off")
3056 || !strcmp (SDATA (lower), "false"))
3057 return Qnil;
3058 else
3059 return Fintern (tem, Qnil);
3060 }
3061
3062 default:
3063 abort ();
3064 }
3065 }
3066 else
3067 return Qunbound;
3068 }
3069 return Fcdr (tem);
3070 }
3071
3072 /* Like x_get_arg, but also record the value in f->param_alist. */
3073
3074 static Lisp_Object
3075 x_get_and_record_arg (f, alist, param, attribute, class, type)
3076 struct frame *f;
3077 Lisp_Object alist, param;
3078 char *attribute;
3079 char *class;
3080 enum resource_types type;
3081 {
3082 Lisp_Object value;
3083
3084 value = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, param,
3085 attribute, class, type);
3086 if (! NILP (value))
3087 store_frame_param (f, param, value);
3088
3089 return value;
3090 }
3091
3092 /* Record in frame F the specified or default value according to ALIST
3093 of the parameter named PROP (a Lisp symbol).
3094 If no value is specified for PROP, look for an X default for XPROP
3095 on the frame named NAME.
3096 If that is not found either, use the value DEFLT. */
3097
3098 static Lisp_Object
3099 x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
3100 struct frame *f;
3101 Lisp_Object alist;
3102 Lisp_Object prop;
3103 Lisp_Object deflt;
3104 char *xprop;
3105 char *xclass;
3106 enum resource_types type;
3107 {
3108 Lisp_Object tem;
3109
3110 tem = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, prop, xprop, xclass, type);
3111 if (EQ (tem, Qunbound))
3112 tem = deflt;
3113 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3114 return tem;
3115 }
3116
3117
3118 /* Record in frame F the specified or default value according to ALIST
3119 of the parameter named PROP (a Lisp symbol). If no value is
3120 specified for PROP, look for an X default for XPROP on the frame
3121 named NAME. If that is not found either, use the value DEFLT. */
3122
3123 static Lisp_Object
3124 x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
3125 foreground_p)
3126 struct frame *f;
3127 Lisp_Object alist;
3128 Lisp_Object prop;
3129 char *xprop;
3130 char *xclass;
3131 int foreground_p;
3132 {
3133 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3134 Lisp_Object tem;
3135
3136 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
3137 if (EQ (tem, Qunbound))
3138 {
3139 #ifdef USE_TOOLKIT_SCROLL_BARS
3140
3141 /* See if an X resource for the scroll bar color has been
3142 specified. */
3143 tem = display_x_get_resource (dpyinfo,
3144 build_string (foreground_p
3145 ? "foreground"
3146 : "background"),
3147 empty_string,
3148 build_string ("verticalScrollBar"),
3149 empty_string);
3150 if (!STRINGP (tem))
3151 {
3152 /* If nothing has been specified, scroll bars will use a
3153 toolkit-dependent default. Because these defaults are
3154 difficult to get at without actually creating a scroll
3155 bar, use nil to indicate that no color has been
3156 specified. */
3157 tem = Qnil;
3158 }
3159
3160 #else /* not USE_TOOLKIT_SCROLL_BARS */
3161
3162 tem = Qnil;
3163
3164 #endif /* not USE_TOOLKIT_SCROLL_BARS */
3165 }
3166
3167 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3168 return tem;
3169 }
3170
3171
3172 \f
3173 DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
3174 doc: /* Parse an X-style geometry string STRING.
3175 Returns an alist of the form ((top . TOP), (left . LEFT) ... ).
3176 The properties returned may include `top', `left', `height', and `width'.
3177 The value of `left' or `top' may be an integer,
3178 or a list (+ N) meaning N pixels relative to top/left corner,
3179 or a list (- N) meaning -N pixels relative to bottom/right corner. */)
3180 (string)
3181 Lisp_Object string;
3182 {
3183 int geometry, x, y;
3184 unsigned int width, height;
3185 Lisp_Object result;
3186
3187 CHECK_STRING (string);
3188
3189 geometry = XParseGeometry ((char *) SDATA (string),
3190 &x, &y, &width, &height);
3191
3192 #if 0
3193 if (!!(geometry & XValue) != !!(geometry & YValue))
3194 error ("Must specify both x and y position, or neither");
3195 #endif
3196
3197 result = Qnil;
3198 if (geometry & XValue)
3199 {
3200 Lisp_Object element;
3201
3202 if (x >= 0 && (geometry & XNegative))
3203 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3204 else if (x < 0 && ! (geometry & XNegative))
3205 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
3206 else
3207 element = Fcons (Qleft, make_number (x));
3208 result = Fcons (element, result);
3209 }
3210
3211 if (geometry & YValue)
3212 {
3213 Lisp_Object element;
3214
3215 if (y >= 0 && (geometry & YNegative))
3216 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3217 else if (y < 0 && ! (geometry & YNegative))
3218 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
3219 else
3220 element = Fcons (Qtop, make_number (y));
3221 result = Fcons (element, result);
3222 }
3223
3224 if (geometry & WidthValue)
3225 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3226 if (geometry & HeightValue)
3227 result = Fcons (Fcons (Qheight, make_number (height)), result);
3228
3229 return result;
3230 }
3231
3232 /* Calculate the desired size and position of this window,
3233 and return the flags saying which aspects were specified.
3234
3235 This function does not make the coordinates positive. */
3236
3237 #define DEFAULT_ROWS 40
3238 #define DEFAULT_COLS 80
3239
3240 static int
3241 x_figure_window_size (f, parms)
3242 struct frame *f;
3243 Lisp_Object parms;
3244 {
3245 register Lisp_Object tem0, tem1, tem2;
3246 long window_prompting = 0;
3247 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3248
3249 /* Default values if we fall through.
3250 Actually, if that happens we should get
3251 window manager prompting. */
3252 SET_FRAME_WIDTH (f, DEFAULT_COLS);
3253 f->height = DEFAULT_ROWS;
3254 /* Window managers expect that if program-specified
3255 positions are not (0,0), they're intentional, not defaults. */
3256 f->output_data.x->top_pos = 0;
3257 f->output_data.x->left_pos = 0;
3258
3259 tem0 = x_get_arg (dpyinfo, parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3260 tem1 = x_get_arg (dpyinfo, parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3261 tem2 = x_get_arg (dpyinfo, parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
3262 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3263 {
3264 if (!EQ (tem0, Qunbound))
3265 {
3266 CHECK_NUMBER (tem0);
3267 f->height = XINT (tem0);
3268 }
3269 if (!EQ (tem1, Qunbound))
3270 {
3271 CHECK_NUMBER (tem1);
3272 SET_FRAME_WIDTH (f, XINT (tem1));
3273 }
3274 if (!NILP (tem2) && !EQ (tem2, Qunbound))
3275 window_prompting |= USSize;
3276 else
3277 window_prompting |= PSize;
3278 }
3279
3280 f->output_data.x->vertical_scroll_bar_extra
3281 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3282 ? 0
3283 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
3284
3285 x_compute_fringe_widths (f, 0);
3286
3287 f->output_data.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3288 f->output_data.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3289
3290 tem0 = x_get_arg (dpyinfo, parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3291 tem1 = x_get_arg (dpyinfo, parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3292 tem2 = x_get_arg (dpyinfo, parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
3293 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
3294 {
3295 if (EQ (tem0, Qminus))
3296 {
3297 f->output_data.x->top_pos = 0;
3298 window_prompting |= YNegative;
3299 }
3300 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3301 && CONSP (XCDR (tem0))
3302 && INTEGERP (XCAR (XCDR (tem0))))
3303 {
3304 f->output_data.x->top_pos = - XINT (XCAR (XCDR (tem0)));
3305 window_prompting |= YNegative;
3306 }
3307 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3308 && CONSP (XCDR (tem0))
3309 && INTEGERP (XCAR (XCDR (tem0))))
3310 {
3311 f->output_data.x->top_pos = XINT (XCAR (XCDR (tem0)));
3312 }
3313 else if (EQ (tem0, Qunbound))
3314 f->output_data.x->top_pos = 0;
3315 else
3316 {
3317 CHECK_NUMBER (tem0);
3318 f->output_data.x->top_pos = XINT (tem0);
3319 if (f->output_data.x->top_pos < 0)
3320 window_prompting |= YNegative;
3321 }
3322
3323 if (EQ (tem1, Qminus))
3324 {
3325 f->output_data.x->left_pos = 0;
3326 window_prompting |= XNegative;
3327 }
3328 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3329 && CONSP (XCDR (tem1))
3330 && INTEGERP (XCAR (XCDR (tem1))))
3331 {
3332 f->output_data.x->left_pos = - XINT (XCAR (XCDR (tem1)));
3333 window_prompting |= XNegative;
3334 }
3335 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3336 && CONSP (XCDR (tem1))
3337 && INTEGERP (XCAR (XCDR (tem1))))
3338 {
3339 f->output_data.x->left_pos = XINT (XCAR (XCDR (tem1)));
3340 }
3341 else if (EQ (tem1, Qunbound))
3342 f->output_data.x->left_pos = 0;
3343 else
3344 {
3345 CHECK_NUMBER (tem1);
3346 f->output_data.x->left_pos = XINT (tem1);
3347 if (f->output_data.x->left_pos < 0)
3348 window_prompting |= XNegative;
3349 }
3350
3351 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
3352 window_prompting |= USPosition;
3353 else
3354 window_prompting |= PPosition;
3355 }
3356
3357 if (f->output_data.x->want_fullscreen != FULLSCREEN_NONE)
3358 {
3359 int left, top;
3360 int width, height;
3361
3362 /* It takes both for some WM:s to place it where we want */
3363 window_prompting = USPosition | PPosition;
3364 x_fullscreen_adjust (f, &width, &height, &top, &left);
3365 f->width = width;
3366 f->height = height;
3367 f->output_data.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3368 f->output_data.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3369 f->output_data.x->left_pos = left;
3370 f->output_data.x->top_pos = top;
3371 }
3372
3373 return window_prompting;
3374 }
3375
3376 #if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
3377
3378 Status
3379 XSetWMProtocols (dpy, w, protocols, count)
3380 Display *dpy;
3381 Window w;
3382 Atom *protocols;
3383 int count;
3384 {
3385 Atom prop;
3386 prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
3387 if (prop == None) return False;
3388 XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
3389 (unsigned char *) protocols, count);
3390 return True;
3391 }
3392 #endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
3393 \f
3394 #ifdef USE_X_TOOLKIT
3395
3396 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
3397 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
3398 already be present because of the toolkit (Motif adds some of them,
3399 for example, but Xt doesn't). */
3400
3401 static void
3402 hack_wm_protocols (f, widget)
3403 FRAME_PTR f;
3404 Widget widget;
3405 {
3406 Display *dpy = XtDisplay (widget);
3407 Window w = XtWindow (widget);
3408 int need_delete = 1;
3409 int need_focus = 1;
3410 int need_save = 1;
3411
3412 BLOCK_INPUT;
3413 {
3414 Atom type, *atoms = 0;
3415 int format = 0;
3416 unsigned long nitems = 0;
3417 unsigned long bytes_after;
3418
3419 if ((XGetWindowProperty (dpy, w,
3420 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3421 (long)0, (long)100, False, XA_ATOM,
3422 &type, &format, &nitems, &bytes_after,
3423 (unsigned char **) &atoms)
3424 == Success)
3425 && format == 32 && type == XA_ATOM)
3426 while (nitems > 0)
3427 {
3428 nitems--;
3429 if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
3430 need_delete = 0;
3431 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
3432 need_focus = 0;
3433 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
3434 need_save = 0;
3435 }
3436 if (atoms) XFree ((char *) atoms);
3437 }
3438 {
3439 Atom props [10];
3440 int count = 0;
3441 if (need_delete)
3442 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
3443 if (need_focus)
3444 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
3445 if (need_save)
3446 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
3447 if (count)
3448 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3449 XA_ATOM, 32, PropModeAppend,
3450 (unsigned char *) props, count);
3451 }
3452 UNBLOCK_INPUT;
3453 }
3454 #endif
3455
3456
3457 \f
3458 /* Support routines for XIC (X Input Context). */
3459
3460 #ifdef HAVE_X_I18N
3461
3462 static XFontSet xic_create_xfontset P_ ((struct frame *, char *));
3463 static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
3464
3465
3466 /* Supported XIM styles, ordered by preferenc. */
3467
3468 static XIMStyle supported_xim_styles[] =
3469 {
3470 XIMPreeditPosition | XIMStatusArea,
3471 XIMPreeditPosition | XIMStatusNothing,
3472 XIMPreeditPosition | XIMStatusNone,
3473 XIMPreeditNothing | XIMStatusArea,
3474 XIMPreeditNothing | XIMStatusNothing,
3475 XIMPreeditNothing | XIMStatusNone,
3476 XIMPreeditNone | XIMStatusArea,
3477 XIMPreeditNone | XIMStatusNothing,
3478 XIMPreeditNone | XIMStatusNone,
3479 0,
3480 };
3481
3482
3483 /* Create an X fontset on frame F with base font name
3484 BASE_FONTNAME.. */
3485
3486 static XFontSet
3487 xic_create_xfontset (f, base_fontname)
3488 struct frame *f;
3489 char *base_fontname;
3490 {
3491 XFontSet xfs;
3492 char **missing_list;
3493 int missing_count;
3494 char *def_string;
3495
3496 xfs = XCreateFontSet (FRAME_X_DISPLAY (f),
3497 base_fontname, &missing_list,
3498 &missing_count, &def_string);
3499 if (missing_list)
3500 XFreeStringList (missing_list);
3501
3502 /* No need to free def_string. */
3503 return xfs;
3504 }
3505
3506
3507 /* Value is the best input style, given user preferences USER (already
3508 checked to be supported by Emacs), and styles supported by the
3509 input method XIM. */
3510
3511 static XIMStyle
3512 best_xim_style (user, xim)
3513 XIMStyles *user;
3514 XIMStyles *xim;
3515 {
3516 int i, j;
3517
3518 for (i = 0; i < user->count_styles; ++i)
3519 for (j = 0; j < xim->count_styles; ++j)
3520 if (user->supported_styles[i] == xim->supported_styles[j])
3521 return user->supported_styles[i];
3522
3523 /* Return the default style. */
3524 return XIMPreeditNothing | XIMStatusNothing;
3525 }
3526
3527 /* Create XIC for frame F. */
3528
3529 static XIMStyle xic_style;
3530
3531 void
3532 create_frame_xic (f)
3533 struct frame *f;
3534 {
3535 XIM xim;
3536 XIC xic = NULL;
3537 XFontSet xfs = NULL;
3538
3539 if (FRAME_XIC (f))
3540 return;
3541
3542 xim = FRAME_X_XIM (f);
3543 if (xim)
3544 {
3545 XRectangle s_area;
3546 XPoint spot;
3547 XVaNestedList preedit_attr;
3548 XVaNestedList status_attr;
3549 char *base_fontname;
3550 int fontset;
3551
3552 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
3553 spot.x = 0; spot.y = 1;
3554 /* Create X fontset. */
3555 fontset = FRAME_FONTSET (f);
3556 if (fontset < 0)
3557 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3558 else
3559 {
3560 /* Determine the base fontname from the ASCII font name of
3561 FONTSET. */
3562 char *ascii_font = (char *) SDATA (fontset_ascii (fontset));
3563 char *p = ascii_font;
3564 int i;
3565
3566 for (i = 0; *p; p++)
3567 if (*p == '-') i++;
3568 if (i != 14)
3569 /* As the font name doesn't conform to XLFD, we can't
3570 modify it to get a suitable base fontname for the
3571 frame. */
3572 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3573 else
3574 {
3575 int len = strlen (ascii_font) + 1;
3576 char *p1 = NULL;
3577
3578 for (i = 0, p = ascii_font; i < 8; p++)
3579 {
3580 if (*p == '-')
3581 {
3582 i++;
3583 if (i == 3)
3584 p1 = p + 1;
3585 }
3586 }
3587 base_fontname = (char *) alloca (len);
3588 bzero (base_fontname, len);
3589 strcpy (base_fontname, "-*-*-");
3590 bcopy (p1, base_fontname + 5, p - p1);
3591 strcat (base_fontname, "*-*-*-*-*-*-*");
3592 }
3593 }
3594 xfs = xic_create_xfontset (f, base_fontname);
3595
3596 /* Determine XIC style. */
3597 if (xic_style == 0)
3598 {
3599 XIMStyles supported_list;
3600 supported_list.count_styles = (sizeof supported_xim_styles
3601 / sizeof supported_xim_styles[0]);
3602 supported_list.supported_styles = supported_xim_styles;
3603 xic_style = best_xim_style (&supported_list,
3604 FRAME_X_XIM_STYLES (f));
3605 }
3606
3607 preedit_attr = XVaCreateNestedList (0,
3608 XNFontSet, xfs,
3609 XNForeground,
3610 FRAME_FOREGROUND_PIXEL (f),
3611 XNBackground,
3612 FRAME_BACKGROUND_PIXEL (f),
3613 (xic_style & XIMPreeditPosition
3614 ? XNSpotLocation
3615 : NULL),
3616 &spot,
3617 NULL);
3618 status_attr = XVaCreateNestedList (0,
3619 XNArea,
3620 &s_area,
3621 XNFontSet,
3622 xfs,
3623 XNForeground,
3624 FRAME_FOREGROUND_PIXEL (f),
3625 XNBackground,
3626 FRAME_BACKGROUND_PIXEL (f),
3627 NULL);
3628
3629 xic = XCreateIC (xim,
3630 XNInputStyle, xic_style,
3631 XNClientWindow, FRAME_X_WINDOW(f),
3632 XNFocusWindow, FRAME_X_WINDOW(f),
3633 XNStatusAttributes, status_attr,
3634 XNPreeditAttributes, preedit_attr,
3635 NULL);
3636 XFree (preedit_attr);
3637 XFree (status_attr);
3638 }
3639
3640 FRAME_XIC (f) = xic;
3641 FRAME_XIC_STYLE (f) = xic_style;
3642 FRAME_XIC_FONTSET (f) = xfs;
3643 }
3644
3645
3646 /* Destroy XIC and free XIC fontset of frame F, if any. */
3647
3648 void
3649 free_frame_xic (f)
3650 struct frame *f;
3651 {
3652 if (FRAME_XIC (f) == NULL)
3653 return;
3654
3655 XDestroyIC (FRAME_XIC (f));
3656 if (FRAME_XIC_FONTSET (f))
3657 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3658
3659 FRAME_XIC (f) = NULL;
3660 FRAME_XIC_FONTSET (f) = NULL;
3661 }
3662
3663
3664 /* Place preedit area for XIC of window W's frame to specified
3665 pixel position X/Y. X and Y are relative to window W. */
3666
3667 void
3668 xic_set_preeditarea (w, x, y)
3669 struct window *w;
3670 int x, y;
3671 {
3672 struct frame *f = XFRAME (w->frame);
3673 XVaNestedList attr;
3674 XPoint spot;
3675
3676 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x);
3677 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
3678 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
3679 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3680 XFree (attr);
3681 }
3682
3683
3684 /* Place status area for XIC in bottom right corner of frame F.. */
3685
3686 void
3687 xic_set_statusarea (f)
3688 struct frame *f;
3689 {
3690 XIC xic = FRAME_XIC (f);
3691 XVaNestedList attr;
3692 XRectangle area;
3693 XRectangle *needed;
3694
3695 /* Negotiate geometry of status area. If input method has existing
3696 status area, use its current size. */
3697 area.x = area.y = area.width = area.height = 0;
3698 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
3699 XSetICValues (xic, XNStatusAttributes, attr, NULL);
3700 XFree (attr);
3701
3702 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
3703 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3704 XFree (attr);
3705
3706 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
3707 {
3708 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
3709 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3710 XFree (attr);
3711 }
3712
3713 area.width = needed->width;
3714 area.height = needed->height;
3715 area.x = PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
3716 area.y = (PIXEL_HEIGHT (f) - area.height
3717 - FRAME_MENUBAR_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f));
3718 XFree (needed);
3719
3720 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
3721 XSetICValues(xic, XNStatusAttributes, attr, NULL);
3722 XFree (attr);
3723 }
3724
3725
3726 /* Set X fontset for XIC of frame F, using base font name
3727 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
3728
3729 void
3730 xic_set_xfontset (f, base_fontname)
3731 struct frame *f;
3732 char *base_fontname;
3733 {
3734 XVaNestedList attr;
3735 XFontSet xfs;
3736
3737 xfs = xic_create_xfontset (f, base_fontname);
3738
3739 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
3740 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
3741 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3742 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
3743 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
3744 XFree (attr);
3745
3746 if (FRAME_XIC_FONTSET (f))
3747 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3748 FRAME_XIC_FONTSET (f) = xfs;
3749 }
3750
3751 #endif /* HAVE_X_I18N */
3752
3753
3754 \f
3755 #ifdef USE_X_TOOLKIT
3756
3757 /* Create and set up the X widget for frame F. */
3758
3759 static void
3760 x_window (f, window_prompting, minibuffer_only)
3761 struct frame *f;
3762 long window_prompting;
3763 int minibuffer_only;
3764 {
3765 XClassHint class_hints;
3766 XSetWindowAttributes attributes;
3767 unsigned long attribute_mask;
3768 Widget shell_widget;
3769 Widget pane_widget;
3770 Widget frame_widget;
3771 Arg al [25];
3772 int ac;
3773
3774 BLOCK_INPUT;
3775
3776 /* Use the resource name as the top-level widget name
3777 for looking up resources. Make a non-Lisp copy
3778 for the window manager, so GC relocation won't bother it.
3779
3780 Elsewhere we specify the window name for the window manager. */
3781
3782 {
3783 char *str = (char *) SDATA (Vx_resource_name);
3784 f->namebuf = (char *) xmalloc (strlen (str) + 1);
3785 strcpy (f->namebuf, str);
3786 }
3787
3788 ac = 0;
3789 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
3790 XtSetArg (al[ac], XtNinput, 1); ac++;
3791 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
3792 XtSetArg (al[ac], XtNborderWidth, f->output_data.x->border_width); ac++;
3793 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3794 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3795 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3796 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
3797 applicationShellWidgetClass,
3798 FRAME_X_DISPLAY (f), al, ac);
3799
3800 f->output_data.x->widget = shell_widget;
3801 /* maybe_set_screen_title_format (shell_widget); */
3802
3803 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
3804 (widget_value *) NULL,
3805 shell_widget, False,
3806 (lw_callback) NULL,
3807 (lw_callback) NULL,
3808 (lw_callback) NULL,
3809 (lw_callback) NULL);
3810
3811 ac = 0;
3812 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3813 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3814 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3815 XtSetValues (pane_widget, al, ac);
3816 f->output_data.x->column_widget = pane_widget;
3817
3818 /* mappedWhenManaged to false tells to the paned window to not map/unmap
3819 the emacs screen when changing menubar. This reduces flickering. */
3820
3821 ac = 0;
3822 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
3823 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
3824 XtSetArg (al[ac], XtNallowResize, 1); ac++;
3825 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
3826 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
3827 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3828 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3829 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3830 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
3831 al, ac);
3832
3833 f->output_data.x->edit_widget = frame_widget;
3834
3835 XtManageChild (frame_widget);
3836
3837 /* Do some needed geometry management. */
3838 {
3839 int len;
3840 char *tem, shell_position[32];
3841 Arg al[2];
3842 int ac = 0;
3843 int extra_borders = 0;
3844 int menubar_size
3845 = (f->output_data.x->menubar_widget
3846 ? (f->output_data.x->menubar_widget->core.height
3847 + f->output_data.x->menubar_widget->core.border_width)
3848 : 0);
3849
3850 #if 0 /* Experimentally, we now get the right results
3851 for -geometry -0-0 without this. 24 Aug 96, rms. */
3852 if (FRAME_EXTERNAL_MENU_BAR (f))
3853 {
3854 Dimension ibw = 0;
3855 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
3856 menubar_size += ibw;
3857 }
3858 #endif
3859
3860 f->output_data.x->menubar_height = menubar_size;
3861
3862 #ifndef USE_LUCID
3863 /* Motif seems to need this amount added to the sizes
3864 specified for the shell widget. The Athena/Lucid widgets don't.
3865 Both conclusions reached experimentally. -- rms. */
3866 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
3867 &extra_borders, NULL);
3868 extra_borders *= 2;
3869 #endif
3870
3871 /* Convert our geometry parameters into a geometry string
3872 and specify it.
3873 Note that we do not specify here whether the position
3874 is a user-specified or program-specified one.
3875 We pass that information later, in x_wm_set_size_hints. */
3876 {
3877 int left = f->output_data.x->left_pos;
3878 int xneg = window_prompting & XNegative;
3879 int top = f->output_data.x->top_pos;
3880 int yneg = window_prompting & YNegative;
3881 if (xneg)
3882 left = -left;
3883 if (yneg)
3884 top = -top;
3885
3886 if (window_prompting & USPosition)
3887 sprintf (shell_position, "=%dx%d%c%d%c%d",
3888 PIXEL_WIDTH (f) + extra_borders,
3889 PIXEL_HEIGHT (f) + menubar_size + extra_borders,
3890 (xneg ? '-' : '+'), left,
3891 (yneg ? '-' : '+'), top);
3892 else
3893 sprintf (shell_position, "=%dx%d",
3894 PIXEL_WIDTH (f) + extra_borders,
3895 PIXEL_HEIGHT (f) + menubar_size + extra_borders);
3896 }
3897
3898 len = strlen (shell_position) + 1;
3899 /* We don't free this because we don't know whether
3900 it is safe to free it while the frame exists.
3901 It isn't worth the trouble of arranging to free it
3902 when the frame is deleted. */
3903 tem = (char *) xmalloc (len);
3904 strncpy (tem, shell_position, len);
3905 XtSetArg (al[ac], XtNgeometry, tem); ac++;
3906 XtSetValues (shell_widget, al, ac);
3907 }
3908
3909 XtManageChild (pane_widget);
3910 XtRealizeWidget (shell_widget);
3911
3912 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
3913
3914 validate_x_resource_name ();
3915
3916 class_hints.res_name = (char *) SDATA (Vx_resource_name);
3917 class_hints.res_class = (char *) SDATA (Vx_resource_class);
3918 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
3919
3920 #ifdef HAVE_X_I18N
3921 FRAME_XIC (f) = NULL;
3922 #ifdef USE_XIM
3923 create_frame_xic (f);
3924 #endif
3925 #endif
3926
3927 f->output_data.x->wm_hints.input = True;
3928 f->output_data.x->wm_hints.flags |= InputHint;
3929 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3930 &f->output_data.x->wm_hints);
3931
3932 hack_wm_protocols (f, shell_widget);
3933
3934 #ifdef HACK_EDITRES
3935 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
3936 #endif
3937
3938 /* Do a stupid property change to force the server to generate a
3939 PropertyNotify event so that the event_stream server timestamp will
3940 be initialized to something relevant to the time we created the window.
3941 */
3942 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
3943 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3944 XA_ATOM, 32, PropModeAppend,
3945 (unsigned char*) NULL, 0);
3946
3947 /* Make all the standard events reach the Emacs frame. */
3948 attributes.event_mask = STANDARD_EVENT_SET;
3949
3950 #ifdef HAVE_X_I18N
3951 if (FRAME_XIC (f))
3952 {
3953 /* XIM server might require some X events. */
3954 unsigned long fevent = NoEventMask;
3955 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3956 attributes.event_mask |= fevent;
3957 }
3958 #endif /* HAVE_X_I18N */
3959
3960 attribute_mask = CWEventMask;
3961 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
3962 attribute_mask, &attributes);
3963
3964 XtMapWidget (frame_widget);
3965
3966 /* x_set_name normally ignores requests to set the name if the
3967 requested name is the same as the current name. This is the one
3968 place where that assumption isn't correct; f->name is set, but
3969 the X server hasn't been told. */
3970 {
3971 Lisp_Object name;
3972 int explicit = f->explicit_name;
3973
3974 f->explicit_name = 0;
3975 name = f->name;
3976 f->name = Qnil;
3977 x_set_name (f, name, explicit);
3978 }
3979
3980 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3981 f->output_data.x->text_cursor);
3982
3983 UNBLOCK_INPUT;
3984
3985 /* This is a no-op, except under Motif. Make sure main areas are
3986 set to something reasonable, in case we get an error later. */
3987 lw_set_main_areas (pane_widget, 0, frame_widget);
3988 }
3989
3990 #else /* not USE_X_TOOLKIT */
3991
3992 /* Create and set up the X window for frame F. */
3993
3994 void
3995 x_window (f)
3996 struct frame *f;
3997
3998 {
3999 XClassHint class_hints;
4000 XSetWindowAttributes attributes;
4001 unsigned long attribute_mask;
4002
4003 attributes.background_pixel = f->output_data.x->background_pixel;
4004 attributes.border_pixel = f->output_data.x->border_pixel;
4005 attributes.bit_gravity = StaticGravity;
4006 attributes.backing_store = NotUseful;
4007 attributes.save_under = True;
4008 attributes.event_mask = STANDARD_EVENT_SET;
4009 attributes.colormap = FRAME_X_COLORMAP (f);
4010 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
4011 | CWColormap);
4012
4013 BLOCK_INPUT;
4014 FRAME_X_WINDOW (f)
4015 = XCreateWindow (FRAME_X_DISPLAY (f),
4016 f->output_data.x->parent_desc,
4017 f->output_data.x->left_pos,
4018 f->output_data.x->top_pos,
4019 PIXEL_WIDTH (f), PIXEL_HEIGHT (f),
4020 f->output_data.x->border_width,
4021 CopyFromParent, /* depth */
4022 InputOutput, /* class */
4023 FRAME_X_VISUAL (f),
4024 attribute_mask, &attributes);
4025
4026 #ifdef HAVE_X_I18N
4027 #ifdef USE_XIM
4028 create_frame_xic (f);
4029 if (FRAME_XIC (f))
4030 {
4031 /* XIM server might require some X events. */
4032 unsigned long fevent = NoEventMask;
4033 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
4034 attributes.event_mask |= fevent;
4035 attribute_mask = CWEventMask;
4036 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4037 attribute_mask, &attributes);
4038 }
4039 #endif
4040 #endif /* HAVE_X_I18N */
4041
4042 validate_x_resource_name ();
4043
4044 class_hints.res_name = (char *) SDATA (Vx_resource_name);
4045 class_hints.res_class = (char *) SDATA (Vx_resource_class);
4046 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
4047
4048 /* The menubar is part of the ordinary display;
4049 it does not count in addition to the height of the window. */
4050 f->output_data.x->menubar_height = 0;
4051
4052 /* This indicates that we use the "Passive Input" input model.
4053 Unless we do this, we don't get the Focus{In,Out} events that we
4054 need to draw the cursor correctly. Accursed bureaucrats.
4055 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
4056
4057 f->output_data.x->wm_hints.input = True;
4058 f->output_data.x->wm_hints.flags |= InputHint;
4059 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4060 &f->output_data.x->wm_hints);
4061 f->output_data.x->wm_hints.icon_pixmap = None;
4062
4063 /* Request "save yourself" and "delete window" commands from wm. */
4064 {
4065 Atom protocols[2];
4066 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
4067 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
4068 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
4069 }
4070
4071 /* x_set_name normally ignores requests to set the name if the
4072 requested name is the same as the current name. This is the one
4073 place where that assumption isn't correct; f->name is set, but
4074 the X server hasn't been told. */
4075 {
4076 Lisp_Object name;
4077 int explicit = f->explicit_name;
4078
4079 f->explicit_name = 0;
4080 name = f->name;
4081 f->name = Qnil;
4082 x_set_name (f, name, explicit);
4083 }
4084
4085 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4086 f->output_data.x->text_cursor);
4087
4088 UNBLOCK_INPUT;
4089
4090 if (FRAME_X_WINDOW (f) == 0)
4091 error ("Unable to create window");
4092 }
4093
4094 #endif /* not USE_X_TOOLKIT */
4095
4096 /* Handle the icon stuff for this window. Perhaps later we might
4097 want an x_set_icon_position which can be called interactively as
4098 well. */
4099
4100 static void
4101 x_icon (f, parms)
4102 struct frame *f;
4103 Lisp_Object parms;
4104 {
4105 Lisp_Object icon_x, icon_y;
4106 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4107
4108 /* Set the position of the icon. Note that twm groups all
4109 icons in an icon window. */
4110 icon_x = x_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4111 icon_y = x_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
4112 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
4113 {
4114 CHECK_NUMBER (icon_x);
4115 CHECK_NUMBER (icon_y);
4116 }
4117 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
4118 error ("Both left and top icon corners of icon must be specified");
4119
4120 BLOCK_INPUT;
4121
4122 if (! EQ (icon_x, Qunbound))
4123 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
4124
4125 /* Start up iconic or window? */
4126 x_wm_set_window_state
4127 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
4128 Qicon)
4129 ? IconicState
4130 : NormalState));
4131
4132 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
4133 ? f->icon_name
4134 : f->name)));
4135
4136 UNBLOCK_INPUT;
4137 }
4138
4139 /* Make the GCs needed for this window, setting the
4140 background, border and mouse colors; also create the
4141 mouse cursor and the gray border tile. */
4142
4143 static char cursor_bits[] =
4144 {
4145 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4146 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4149 };
4150
4151 static void
4152 x_make_gc (f)
4153 struct frame *f;
4154 {
4155 XGCValues gc_values;
4156
4157 BLOCK_INPUT;
4158
4159 /* Create the GCs of this frame.
4160 Note that many default values are used. */
4161
4162 /* Normal video */
4163 gc_values.font = f->output_data.x->font->fid;
4164 gc_values.foreground = f->output_data.x->foreground_pixel;
4165 gc_values.background = f->output_data.x->background_pixel;
4166 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
4167 f->output_data.x->normal_gc
4168 = XCreateGC (FRAME_X_DISPLAY (f),
4169 FRAME_X_WINDOW (f),
4170 GCLineWidth | GCFont | GCForeground | GCBackground,
4171 &gc_values);
4172
4173 /* Reverse video style. */
4174 gc_values.foreground = f->output_data.x->background_pixel;
4175 gc_values.background = f->output_data.x->foreground_pixel;
4176 f->output_data.x->reverse_gc
4177 = XCreateGC (FRAME_X_DISPLAY (f),
4178 FRAME_X_WINDOW (f),
4179 GCFont | GCForeground | GCBackground | GCLineWidth,
4180 &gc_values);
4181
4182 /* Cursor has cursor-color background, background-color foreground. */
4183 gc_values.foreground = f->output_data.x->background_pixel;
4184 gc_values.background = f->output_data.x->cursor_pixel;
4185 gc_values.fill_style = FillOpaqueStippled;
4186 gc_values.stipple
4187 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
4188 FRAME_X_DISPLAY_INFO (f)->root_window,
4189 cursor_bits, 16, 16);
4190 f->output_data.x->cursor_gc
4191 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4192 (GCFont | GCForeground | GCBackground
4193 | GCFillStyle /* | GCStipple */ | GCLineWidth),
4194 &gc_values);
4195
4196 /* Reliefs. */
4197 f->output_data.x->white_relief.gc = 0;
4198 f->output_data.x->black_relief.gc = 0;
4199
4200 /* Create the gray border tile used when the pointer is not in
4201 the frame. Since this depends on the frame's pixel values,
4202 this must be done on a per-frame basis. */
4203 f->output_data.x->border_tile
4204 = (XCreatePixmapFromBitmapData
4205 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
4206 gray_bits, gray_width, gray_height,
4207 f->output_data.x->foreground_pixel,
4208 f->output_data.x->background_pixel,
4209 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
4210
4211 UNBLOCK_INPUT;
4212 }
4213
4214
4215 /* Free what was was allocated in x_make_gc. */
4216
4217 void
4218 x_free_gcs (f)
4219 struct frame *f;
4220 {
4221 Display *dpy = FRAME_X_DISPLAY (f);
4222
4223 BLOCK_INPUT;
4224
4225 if (f->output_data.x->normal_gc)
4226 {
4227 XFreeGC (dpy, f->output_data.x->normal_gc);
4228 f->output_data.x->normal_gc = 0;
4229 }
4230
4231 if (f->output_data.x->reverse_gc)
4232 {
4233 XFreeGC (dpy, f->output_data.x->reverse_gc);
4234 f->output_data.x->reverse_gc = 0;
4235 }
4236
4237 if (f->output_data.x->cursor_gc)
4238 {
4239 XFreeGC (dpy, f->output_data.x->cursor_gc);
4240 f->output_data.x->cursor_gc = 0;
4241 }
4242
4243 if (f->output_data.x->border_tile)
4244 {
4245 XFreePixmap (dpy, f->output_data.x->border_tile);
4246 f->output_data.x->border_tile = 0;
4247 }
4248
4249 UNBLOCK_INPUT;
4250 }
4251
4252
4253 /* Handler for signals raised during x_create_frame and
4254 x_create_top_frame. FRAME is the frame which is partially
4255 constructed. */
4256
4257 static Lisp_Object
4258 unwind_create_frame (frame)
4259 Lisp_Object frame;
4260 {
4261 struct frame *f = XFRAME (frame);
4262
4263 /* If frame is ``official'', nothing to do. */
4264 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4265 {
4266 #if GLYPH_DEBUG
4267 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4268 #endif
4269
4270 x_free_frame_resources (f);
4271
4272 /* Check that reference counts are indeed correct. */
4273 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4274 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
4275 return Qt;
4276 }
4277
4278 return Qnil;
4279 }
4280
4281
4282 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
4283 1, 1, 0,
4284 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
4285 Returns an Emacs frame object.
4286 ALIST is an alist of frame parameters.
4287 If the parameters specify that the frame should not have a minibuffer,
4288 and do not specify a specific minibuffer window to use,
4289 then `default-minibuffer-frame' must be a frame whose minibuffer can
4290 be shared by the new frame.
4291
4292 This function is an internal primitive--use `make-frame' instead. */)
4293 (parms)
4294 Lisp_Object parms;
4295 {
4296 struct frame *f;
4297 Lisp_Object frame, tem;
4298 Lisp_Object name;
4299 int minibuffer_only = 0;
4300 long window_prompting = 0;
4301 int width, height;
4302 int count = SPECPDL_INDEX ();
4303 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4304 Lisp_Object display;
4305 struct x_display_info *dpyinfo = NULL;
4306 Lisp_Object parent;
4307 struct kboard *kb;
4308
4309 check_x ();
4310
4311 /* Use this general default value to start with
4312 until we know if this frame has a specified name. */
4313 Vx_resource_name = Vinvocation_name;
4314
4315 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
4316 if (EQ (display, Qunbound))
4317 display = Qnil;
4318 dpyinfo = check_x_display_info (display);
4319 #ifdef MULTI_KBOARD
4320 kb = dpyinfo->kboard;
4321 #else
4322 kb = &the_only_kboard;
4323 #endif
4324
4325 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4326 if (!STRINGP (name)
4327 && ! EQ (name, Qunbound)
4328 && ! NILP (name))
4329 error ("Invalid frame name--not a string or nil");
4330
4331 if (STRINGP (name))
4332 Vx_resource_name = name;
4333
4334 /* See if parent window is specified. */
4335 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
4336 if (EQ (parent, Qunbound))
4337 parent = Qnil;
4338 if (! NILP (parent))
4339 CHECK_NUMBER (parent);
4340
4341 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4342 /* No need to protect DISPLAY because that's not used after passing
4343 it to make_frame_without_minibuffer. */
4344 frame = Qnil;
4345 GCPRO4 (parms, parent, name, frame);
4346 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
4347 RES_TYPE_SYMBOL);
4348 if (EQ (tem, Qnone) || NILP (tem))
4349 f = make_frame_without_minibuffer (Qnil, kb, display);
4350 else if (EQ (tem, Qonly))
4351 {
4352 f = make_minibuffer_frame ();
4353 minibuffer_only = 1;
4354 }
4355 else if (WINDOWP (tem))
4356 f = make_frame_without_minibuffer (tem, kb, display);
4357 else
4358 f = make_frame (1);
4359
4360 XSETFRAME (frame, f);
4361
4362 /* Note that X Windows does support scroll bars. */
4363 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
4364
4365 f->output_method = output_x_window;
4366 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
4367 bzero (f->output_data.x, sizeof (struct x_output));
4368 f->output_data.x->icon_bitmap = -1;
4369 f->output_data.x->fontset = -1;
4370 f->output_data.x->scroll_bar_foreground_pixel = -1;
4371 f->output_data.x->scroll_bar_background_pixel = -1;
4372 #ifdef USE_TOOLKIT_SCROLL_BARS
4373 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4374 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4375 #endif /* USE_TOOLKIT_SCROLL_BARS */
4376 record_unwind_protect (unwind_create_frame, frame);
4377
4378 f->icon_name
4379 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
4380 RES_TYPE_STRING);
4381 if (! STRINGP (f->icon_name))
4382 f->icon_name = Qnil;
4383
4384 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4385 #if GLYPH_DEBUG
4386 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
4387 dpyinfo_refcount = dpyinfo->reference_count;
4388 #endif /* GLYPH_DEBUG */
4389 #ifdef MULTI_KBOARD
4390 FRAME_KBOARD (f) = kb;
4391 #endif
4392
4393 /* These colors will be set anyway later, but it's important
4394 to get the color reference counts right, so initialize them! */
4395 {
4396 Lisp_Object black;
4397 struct gcpro gcpro1;
4398
4399 /* Function x_decode_color can signal an error. Make
4400 sure to initialize color slots so that we won't try
4401 to free colors we haven't allocated. */
4402 f->output_data.x->foreground_pixel = -1;
4403 f->output_data.x->background_pixel = -1;
4404 f->output_data.x->cursor_pixel = -1;
4405 f->output_data.x->cursor_foreground_pixel = -1;
4406 f->output_data.x->border_pixel = -1;
4407 f->output_data.x->mouse_pixel = -1;
4408
4409 black = build_string ("black");
4410 GCPRO1 (black);
4411 f->output_data.x->foreground_pixel
4412 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4413 f->output_data.x->background_pixel
4414 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4415 f->output_data.x->cursor_pixel
4416 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4417 f->output_data.x->cursor_foreground_pixel
4418 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4419 f->output_data.x->border_pixel
4420 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4421 f->output_data.x->mouse_pixel
4422 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4423 UNGCPRO;
4424 }
4425
4426 /* Specify the parent under which to make this X window. */
4427
4428 if (!NILP (parent))
4429 {
4430 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
4431 f->output_data.x->explicit_parent = 1;
4432 }
4433 else
4434 {
4435 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4436 f->output_data.x->explicit_parent = 0;
4437 }
4438
4439 /* Set the name; the functions to which we pass f expect the name to
4440 be set. */
4441 if (EQ (name, Qunbound) || NILP (name))
4442 {
4443 f->name = build_string (dpyinfo->x_id_name);
4444 f->explicit_name = 0;
4445 }
4446 else
4447 {
4448 f->name = name;
4449 f->explicit_name = 1;
4450 /* use the frame's title when getting resources for this frame. */
4451 specbind (Qx_resource_name, name);
4452 }
4453
4454 /* Extract the window parameters from the supplied values
4455 that are needed to determine window geometry. */
4456 {
4457 Lisp_Object font;
4458
4459 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4460
4461 BLOCK_INPUT;
4462 /* First, try whatever font the caller has specified. */
4463 if (STRINGP (font))
4464 {
4465 tem = Fquery_fontset (font, Qnil);
4466 if (STRINGP (tem))
4467 font = x_new_fontset (f, SDATA (tem));
4468 else
4469 font = x_new_font (f, SDATA (font));
4470 }
4471
4472 /* Try out a font which we hope has bold and italic variations. */
4473 if (!STRINGP (font))
4474 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
4475 if (!STRINGP (font))
4476 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4477 if (! STRINGP (font))
4478 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4479 if (! STRINGP (font))
4480 /* This was formerly the first thing tried, but it finds too many fonts
4481 and takes too long. */
4482 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
4483 /* If those didn't work, look for something which will at least work. */
4484 if (! STRINGP (font))
4485 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
4486 UNBLOCK_INPUT;
4487 if (! STRINGP (font))
4488 font = build_string ("fixed");
4489
4490 x_default_parameter (f, parms, Qfont, font,
4491 "font", "Font", RES_TYPE_STRING);
4492 }
4493
4494 #ifdef USE_LUCID
4495 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
4496 whereby it fails to get any font. */
4497 xlwmenu_default_font = f->output_data.x->font;
4498 #endif
4499
4500 x_default_parameter (f, parms, Qborder_width, make_number (2),
4501 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4502
4503 /* This defaults to 1 in order to match xterm. We recognize either
4504 internalBorderWidth or internalBorder (which is what xterm calls
4505 it). */
4506 if (NILP (Fassq (Qinternal_border_width, parms)))
4507 {
4508 Lisp_Object value;
4509
4510 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4511 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4512 if (! EQ (value, Qunbound))
4513 parms = Fcons (Fcons (Qinternal_border_width, value),
4514 parms);
4515 }
4516 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4517 "internalBorderWidth", "internalBorderWidth",
4518 RES_TYPE_NUMBER);
4519 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
4520 "verticalScrollBars", "ScrollBars",
4521 RES_TYPE_SYMBOL);
4522
4523 /* Also do the stuff which must be set before the window exists. */
4524 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4525 "foreground", "Foreground", RES_TYPE_STRING);
4526 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4527 "background", "Background", RES_TYPE_STRING);
4528 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4529 "pointerColor", "Foreground", RES_TYPE_STRING);
4530 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4531 "cursorColor", "Foreground", RES_TYPE_STRING);
4532 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4533 "borderColor", "BorderColor", RES_TYPE_STRING);
4534 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
4535 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
4536 x_default_parameter (f, parms, Qline_spacing, Qnil,
4537 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
4538 x_default_parameter (f, parms, Qleft_fringe, Qnil,
4539 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4540 x_default_parameter (f, parms, Qright_fringe, Qnil,
4541 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
4542
4543 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
4544 "scrollBarForeground",
4545 "ScrollBarForeground", 1);
4546 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
4547 "scrollBarBackground",
4548 "ScrollBarBackground", 0);
4549
4550 /* Init faces before x_default_parameter is called for scroll-bar
4551 parameters because that function calls x_set_scroll_bar_width,
4552 which calls change_frame_size, which calls Fset_window_buffer,
4553 which runs hooks, which call Fvertical_motion. At the end, we
4554 end up in init_iterator with a null face cache, which should not
4555 happen. */
4556 init_frame_faces (f);
4557
4558 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
4559 "menuBar", "MenuBar", RES_TYPE_NUMBER);
4560 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
4561 "toolBar", "ToolBar", RES_TYPE_NUMBER);
4562 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
4563 "bufferPredicate", "BufferPredicate",
4564 RES_TYPE_SYMBOL);
4565 x_default_parameter (f, parms, Qtitle, Qnil,
4566 "title", "Title", RES_TYPE_STRING);
4567 x_default_parameter (f, parms, Qwait_for_wm, Qt,
4568 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
4569 x_default_parameter (f, parms, Qfullscreen, Qnil,
4570 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
4571
4572 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4573
4574 /* Add the tool-bar height to the initial frame height so that the
4575 user gets a text display area of the size he specified with -g or
4576 via .Xdefaults. Later changes of the tool-bar height don't
4577 change the frame size. This is done so that users can create
4578 tall Emacs frames without having to guess how tall the tool-bar
4579 will get. */
4580 if (FRAME_TOOL_BAR_LINES (f))
4581 {
4582 int margin, relief, bar_height;
4583
4584 relief = (tool_bar_button_relief >= 0
4585 ? tool_bar_button_relief
4586 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
4587
4588 if (INTEGERP (Vtool_bar_button_margin)
4589 && XINT (Vtool_bar_button_margin) > 0)
4590 margin = XFASTINT (Vtool_bar_button_margin);
4591 else if (CONSP (Vtool_bar_button_margin)
4592 && INTEGERP (XCDR (Vtool_bar_button_margin))
4593 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
4594 margin = XFASTINT (XCDR (Vtool_bar_button_margin));
4595 else
4596 margin = 0;
4597
4598 bar_height = DEFAULT_TOOL_BAR_IMAGE_HEIGHT + 2 * margin + 2 * relief;
4599 f->height += (bar_height + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
4600 }
4601
4602 /* Compute the size of the X window. */
4603 window_prompting = x_figure_window_size (f, parms);
4604
4605 if (window_prompting & XNegative)
4606 {
4607 if (window_prompting & YNegative)
4608 f->output_data.x->win_gravity = SouthEastGravity;
4609 else
4610 f->output_data.x->win_gravity = NorthEastGravity;
4611 }
4612 else
4613 {
4614 if (window_prompting & YNegative)
4615 f->output_data.x->win_gravity = SouthWestGravity;
4616 else
4617 f->output_data.x->win_gravity = NorthWestGravity;
4618 }
4619
4620 f->output_data.x->size_hint_flags = window_prompting;
4621
4622 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4623 f->no_split = minibuffer_only || EQ (tem, Qt);
4624
4625 /* Create the X widget or window. */
4626 #ifdef USE_X_TOOLKIT
4627 x_window (f, window_prompting, minibuffer_only);
4628 #else
4629 x_window (f);
4630 #endif
4631
4632 x_icon (f, parms);
4633 x_make_gc (f);
4634
4635 /* Now consider the frame official. */
4636 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4637 Vframe_list = Fcons (frame, Vframe_list);
4638
4639 /* We need to do this after creating the X window, so that the
4640 icon-creation functions can say whose icon they're describing. */
4641 x_default_parameter (f, parms, Qicon_type, Qnil,
4642 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
4643
4644 x_default_parameter (f, parms, Qauto_raise, Qnil,
4645 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4646 x_default_parameter (f, parms, Qauto_lower, Qnil,
4647 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4648 x_default_parameter (f, parms, Qcursor_type, Qbox,
4649 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4650 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
4651 "scrollBarWidth", "ScrollBarWidth",
4652 RES_TYPE_NUMBER);
4653
4654 /* Dimensions, especially f->height, must be done via change_frame_size.
4655 Change will not be effected unless different from the current
4656 f->height. */
4657 width = f->width;
4658 height = f->height;
4659
4660 f->height = 0;
4661 SET_FRAME_WIDTH (f, 0);
4662 change_frame_size (f, height, width, 1, 0, 0);
4663
4664 /* Set up faces after all frame parameters are known. This call
4665 also merges in face attributes specified for new frames. If we
4666 don't do this, the `menu' face for instance won't have the right
4667 colors, and the menu bar won't appear in the specified colors for
4668 new frames. */
4669 call1 (Qface_set_after_frame_default, frame);
4670
4671 #ifdef USE_X_TOOLKIT
4672 /* Create the menu bar. */
4673 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4674 {
4675 /* If this signals an error, we haven't set size hints for the
4676 frame and we didn't make it visible. */
4677 initialize_frame_menubar (f);
4678
4679 /* This is a no-op, except under Motif where it arranges the
4680 main window for the widgets on it. */
4681 lw_set_main_areas (f->output_data.x->column_widget,
4682 f->output_data.x->menubar_widget,
4683 f->output_data.x->edit_widget);
4684 }
4685 #endif /* USE_X_TOOLKIT */
4686
4687 /* Tell the server what size and position, etc, we want, and how
4688 badly we want them. This should be done after we have the menu
4689 bar so that its size can be taken into account. */
4690 BLOCK_INPUT;
4691 x_wm_set_size_hint (f, window_prompting, 0);
4692 UNBLOCK_INPUT;
4693
4694 /* Make the window appear on the frame and enable display, unless
4695 the caller says not to. However, with explicit parent, Emacs
4696 cannot control visibility, so don't try. */
4697 if (! f->output_data.x->explicit_parent)
4698 {
4699 Lisp_Object visibility;
4700
4701 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
4702 RES_TYPE_SYMBOL);
4703 if (EQ (visibility, Qunbound))
4704 visibility = Qt;
4705
4706 if (EQ (visibility, Qicon))
4707 x_iconify_frame (f);
4708 else if (! NILP (visibility))
4709 x_make_frame_visible (f);
4710 else
4711 /* Must have been Qnil. */
4712 ;
4713 }
4714
4715 UNGCPRO;
4716
4717 /* Make sure windows on this frame appear in calls to next-window
4718 and similar functions. */
4719 Vwindow_list = Qnil;
4720
4721 return unbind_to (count, frame);
4722 }
4723
4724
4725 /* FRAME is used only to get a handle on the X display. We don't pass the
4726 display info directly because we're called from frame.c, which doesn't
4727 know about that structure. */
4728
4729 Lisp_Object
4730 x_get_focus_frame (frame)
4731 struct frame *frame;
4732 {
4733 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
4734 Lisp_Object xfocus;
4735 if (! dpyinfo->x_focus_frame)
4736 return Qnil;
4737
4738 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
4739 return xfocus;
4740 }
4741
4742
4743 /* In certain situations, when the window manager follows a
4744 click-to-focus policy, there seems to be no way around calling
4745 XSetInputFocus to give another frame the input focus .
4746
4747 In an ideal world, XSetInputFocus should generally be avoided so
4748 that applications don't interfere with the window manager's focus
4749 policy. But I think it's okay to use when it's clearly done
4750 following a user-command. */
4751
4752 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
4753 doc: /* Set the input focus to FRAME.
4754 FRAME nil means use the selected frame. */)
4755 (frame)
4756 Lisp_Object frame;
4757 {
4758 struct frame *f = check_x_frame (frame);
4759 Display *dpy = FRAME_X_DISPLAY (f);
4760 int count;
4761
4762 BLOCK_INPUT;
4763 count = x_catch_errors (dpy);
4764 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4765 RevertToParent, CurrentTime);
4766 x_uncatch_errors (dpy, count);
4767 UNBLOCK_INPUT;
4768
4769 return Qnil;
4770 }
4771
4772 \f
4773 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4774 doc: /* Internal function called by `color-defined-p', which see. */)
4775 (color, frame)
4776 Lisp_Object color, frame;
4777 {
4778 XColor foo;
4779 FRAME_PTR f = check_x_frame (frame);
4780
4781 CHECK_STRING (color);
4782
4783 if (x_defined_color (f, SDATA (color), &foo, 0))
4784 return Qt;
4785 else
4786 return Qnil;
4787 }
4788
4789 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
4790 doc: /* Internal function called by `color-values', which see. */)
4791 (color, frame)
4792 Lisp_Object color, frame;
4793 {
4794 XColor foo;
4795 FRAME_PTR f = check_x_frame (frame);
4796
4797 CHECK_STRING (color);
4798
4799 if (x_defined_color (f, SDATA (color), &foo, 0))
4800 {
4801 Lisp_Object rgb[3];
4802
4803 rgb[0] = make_number (foo.red);
4804 rgb[1] = make_number (foo.green);
4805 rgb[2] = make_number (foo.blue);
4806 return Flist (3, rgb);
4807 }
4808 else
4809 return Qnil;
4810 }
4811
4812 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
4813 doc: /* Internal function called by `display-color-p', which see. */)
4814 (display)
4815 Lisp_Object display;
4816 {
4817 struct x_display_info *dpyinfo = check_x_display_info (display);
4818
4819 if (dpyinfo->n_planes <= 2)
4820 return Qnil;
4821
4822 switch (dpyinfo->visual->class)
4823 {
4824 case StaticColor:
4825 case PseudoColor:
4826 case TrueColor:
4827 case DirectColor:
4828 return Qt;
4829
4830 default:
4831 return Qnil;
4832 }
4833 }
4834
4835 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
4836 0, 1, 0,
4837 doc: /* Return t if the X display supports shades of gray.
4838 Note that color displays do support shades of gray.
4839 The optional argument DISPLAY specifies which display to ask about.
4840 DISPLAY should be either a frame or a display name (a string).
4841 If omitted or nil, that stands for the selected frame's display. */)
4842 (display)
4843 Lisp_Object display;
4844 {
4845 struct x_display_info *dpyinfo = check_x_display_info (display);
4846
4847 if (dpyinfo->n_planes <= 1)
4848 return Qnil;
4849
4850 switch (dpyinfo->visual->class)
4851 {
4852 case StaticColor:
4853 case PseudoColor:
4854 case TrueColor:
4855 case DirectColor:
4856 case StaticGray:
4857 case GrayScale:
4858 return Qt;
4859
4860 default:
4861 return Qnil;
4862 }
4863 }
4864
4865 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
4866 0, 1, 0,
4867 doc: /* Returns the width in pixels of the X display DISPLAY.
4868 The optional argument DISPLAY specifies which display to ask about.
4869 DISPLAY should be either a frame or a display name (a string).
4870 If omitted or nil, that stands for the selected frame's display. */)
4871 (display)
4872 Lisp_Object display;
4873 {
4874 struct x_display_info *dpyinfo = check_x_display_info (display);
4875
4876 return make_number (dpyinfo->width);
4877 }
4878
4879 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
4880 Sx_display_pixel_height, 0, 1, 0,
4881 doc: /* Returns the height in pixels of the X display DISPLAY.
4882 The optional argument DISPLAY specifies which display to ask about.
4883 DISPLAY should be either a frame or a display name (a string).
4884 If omitted or nil, that stands for the selected frame's display. */)
4885 (display)
4886 Lisp_Object display;
4887 {
4888 struct x_display_info *dpyinfo = check_x_display_info (display);
4889
4890 return make_number (dpyinfo->height);
4891 }
4892
4893 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
4894 0, 1, 0,
4895 doc: /* Returns the number of bitplanes of the X display DISPLAY.
4896 The optional argument DISPLAY specifies which display to ask about.
4897 DISPLAY should be either a frame or a display name (a string).
4898 If omitted or nil, that stands for the selected frame's display. */)
4899 (display)
4900 Lisp_Object display;
4901 {
4902 struct x_display_info *dpyinfo = check_x_display_info (display);
4903
4904 return make_number (dpyinfo->n_planes);
4905 }
4906
4907 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
4908 0, 1, 0,
4909 doc: /* Returns the number of color cells of the X display DISPLAY.
4910 The optional argument DISPLAY specifies which display to ask about.
4911 DISPLAY should be either a frame or a display name (a string).
4912 If omitted or nil, that stands for the selected frame's display. */)
4913 (display)
4914 Lisp_Object display;
4915 {
4916 struct x_display_info *dpyinfo = check_x_display_info (display);
4917
4918 return make_number (DisplayCells (dpyinfo->display,
4919 XScreenNumberOfScreen (dpyinfo->screen)));
4920 }
4921
4922 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4923 Sx_server_max_request_size,
4924 0, 1, 0,
4925 doc: /* Returns the maximum request size of the X server of display DISPLAY.
4926 The optional argument DISPLAY specifies which display to ask about.
4927 DISPLAY should be either a frame or a display name (a string).
4928 If omitted or nil, that stands for the selected frame's display. */)
4929 (display)
4930 Lisp_Object display;
4931 {
4932 struct x_display_info *dpyinfo = check_x_display_info (display);
4933
4934 return make_number (MAXREQUEST (dpyinfo->display));
4935 }
4936
4937 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
4938 doc: /* Returns the vendor ID string of the X server of display DISPLAY.
4939 The optional argument DISPLAY specifies which display to ask about.
4940 DISPLAY should be either a frame or a display name (a string).
4941 If omitted or nil, that stands for the selected frame's display. */)
4942 (display)
4943 Lisp_Object display;
4944 {
4945 struct x_display_info *dpyinfo = check_x_display_info (display);
4946 char *vendor = ServerVendor (dpyinfo->display);
4947
4948 if (! vendor) vendor = "";
4949 return build_string (vendor);
4950 }
4951
4952 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
4953 doc: /* Returns the version numbers of the X server of display DISPLAY.
4954 The value is a list of three integers: the major and minor
4955 version numbers of the X Protocol in use, and the vendor-specific release
4956 number. See also the function `x-server-vendor'.
4957
4958 The optional argument DISPLAY specifies which display to ask about.
4959 DISPLAY should be either a frame or a display name (a string).
4960 If omitted or nil, that stands for the selected frame's display. */)
4961 (display)
4962 Lisp_Object display;
4963 {
4964 struct x_display_info *dpyinfo = check_x_display_info (display);
4965 Display *dpy = dpyinfo->display;
4966
4967 return Fcons (make_number (ProtocolVersion (dpy)),
4968 Fcons (make_number (ProtocolRevision (dpy)),
4969 Fcons (make_number (VendorRelease (dpy)), Qnil)));
4970 }
4971
4972 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
4973 doc: /* Return the number of screens on the X server of display DISPLAY.
4974 The optional argument DISPLAY specifies which display to ask about.
4975 DISPLAY should be either a frame or a display name (a string).
4976 If omitted or nil, that stands for the selected frame's display. */)
4977 (display)
4978 Lisp_Object display;
4979 {
4980 struct x_display_info *dpyinfo = check_x_display_info (display);
4981
4982 return make_number (ScreenCount (dpyinfo->display));
4983 }
4984
4985 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
4986 doc: /* Return the height in millimeters of the X display DISPLAY.
4987 The optional argument DISPLAY specifies which display to ask about.
4988 DISPLAY should be either a frame or a display name (a string).
4989 If omitted or nil, that stands for the selected frame's display. */)
4990 (display)
4991 Lisp_Object display;
4992 {
4993 struct x_display_info *dpyinfo = check_x_display_info (display);
4994
4995 return make_number (HeightMMOfScreen (dpyinfo->screen));
4996 }
4997
4998 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
4999 doc: /* Return the width in millimeters of the X display DISPLAY.
5000 The optional argument DISPLAY specifies which display to ask about.
5001 DISPLAY should be either a frame or a display name (a string).
5002 If omitted or nil, that stands for the selected frame's display. */)
5003 (display)
5004 Lisp_Object display;
5005 {
5006 struct x_display_info *dpyinfo = check_x_display_info (display);
5007
5008 return make_number (WidthMMOfScreen (dpyinfo->screen));
5009 }
5010
5011 DEFUN ("x-display-backing-store", Fx_display_backing_store,
5012 Sx_display_backing_store, 0, 1, 0,
5013 doc: /* Returns an indication of whether X display DISPLAY does backing store.
5014 The value may be `always', `when-mapped', or `not-useful'.
5015 The optional argument DISPLAY specifies which display to ask about.
5016 DISPLAY should be either a frame or a display name (a string).
5017 If omitted or nil, that stands for the selected frame's display. */)
5018 (display)
5019 Lisp_Object display;
5020 {
5021 struct x_display_info *dpyinfo = check_x_display_info (display);
5022 Lisp_Object result;
5023
5024 switch (DoesBackingStore (dpyinfo->screen))
5025 {
5026 case Always:
5027 result = intern ("always");
5028 break;
5029
5030 case WhenMapped:
5031 result = intern ("when-mapped");
5032 break;
5033
5034 case NotUseful:
5035 result = intern ("not-useful");
5036 break;
5037
5038 default:
5039 error ("Strange value for BackingStore parameter of screen");
5040 result = Qnil;
5041 }
5042
5043 return result;
5044 }
5045
5046 DEFUN ("x-display-visual-class", Fx_display_visual_class,
5047 Sx_display_visual_class, 0, 1, 0,
5048 doc: /* Return the visual class of the X display DISPLAY.
5049 The value is one of the symbols `static-gray', `gray-scale',
5050 `static-color', `pseudo-color', `true-color', or `direct-color'.
5051
5052 The optional argument DISPLAY specifies which display to ask about.
5053 DISPLAY should be either a frame or a display name (a string).
5054 If omitted or nil, that stands for the selected frame's display. */)
5055 (display)
5056 Lisp_Object display;
5057 {
5058 struct x_display_info *dpyinfo = check_x_display_info (display);
5059 Lisp_Object result;
5060
5061 switch (dpyinfo->visual->class)
5062 {
5063 case StaticGray:
5064 result = intern ("static-gray");
5065 break;
5066 case GrayScale:
5067 result = intern ("gray-scale");
5068 break;
5069 case StaticColor:
5070 result = intern ("static-color");
5071 break;
5072 case PseudoColor:
5073 result = intern ("pseudo-color");
5074 break;
5075 case TrueColor:
5076 result = intern ("true-color");
5077 break;
5078 case DirectColor:
5079 result = intern ("direct-color");
5080 break;
5081 default:
5082 error ("Display has an unknown visual class");
5083 result = Qnil;
5084 }
5085
5086 return result;
5087 }
5088
5089 DEFUN ("x-display-save-under", Fx_display_save_under,
5090 Sx_display_save_under, 0, 1, 0,
5091 doc: /* Returns t if the X display DISPLAY supports the save-under feature.
5092 The optional argument DISPLAY specifies which display to ask about.
5093 DISPLAY should be either a frame or a display name (a string).
5094 If omitted or nil, that stands for the selected frame's display. */)
5095 (display)
5096 Lisp_Object display;
5097 {
5098 struct x_display_info *dpyinfo = check_x_display_info (display);
5099
5100 if (DoesSaveUnders (dpyinfo->screen) == True)
5101 return Qt;
5102 else
5103 return Qnil;
5104 }
5105 \f
5106 int
5107 x_pixel_width (f)
5108 register struct frame *f;
5109 {
5110 return PIXEL_WIDTH (f);
5111 }
5112
5113 int
5114 x_pixel_height (f)
5115 register struct frame *f;
5116 {
5117 return PIXEL_HEIGHT (f);
5118 }
5119
5120 int
5121 x_char_width (f)
5122 register struct frame *f;
5123 {
5124 return FONT_WIDTH (f->output_data.x->font);
5125 }
5126
5127 int
5128 x_char_height (f)
5129 register struct frame *f;
5130 {
5131 return f->output_data.x->line_height;
5132 }
5133
5134 int
5135 x_screen_planes (f)
5136 register struct frame *f;
5137 {
5138 return FRAME_X_DISPLAY_INFO (f)->n_planes;
5139 }
5140
5141
5142 \f
5143 /************************************************************************
5144 X Displays
5145 ************************************************************************/
5146
5147 \f
5148 /* Mapping visual names to visuals. */
5149
5150 static struct visual_class
5151 {
5152 char *name;
5153 int class;
5154 }
5155 visual_classes[] =
5156 {
5157 {"StaticGray", StaticGray},
5158 {"GrayScale", GrayScale},
5159 {"StaticColor", StaticColor},
5160 {"PseudoColor", PseudoColor},
5161 {"TrueColor", TrueColor},
5162 {"DirectColor", DirectColor},
5163 {NULL, 0}
5164 };
5165
5166
5167 #ifndef HAVE_XSCREENNUMBEROFSCREEN
5168
5169 /* Value is the screen number of screen SCR. This is a substitute for
5170 the X function with the same name when that doesn't exist. */
5171
5172 int
5173 XScreenNumberOfScreen (scr)
5174 register Screen *scr;
5175 {
5176 Display *dpy = scr->display;
5177 int i;
5178
5179 for (i = 0; i < dpy->nscreens; ++i)
5180 if (scr == dpy->screens + i)
5181 break;
5182
5183 return i;
5184 }
5185
5186 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
5187
5188
5189 /* Select the visual that should be used on display DPYINFO. Set
5190 members of DPYINFO appropriately. Called from x_term_init. */
5191
5192 void
5193 select_visual (dpyinfo)
5194 struct x_display_info *dpyinfo;
5195 {
5196 Display *dpy = dpyinfo->display;
5197 Screen *screen = dpyinfo->screen;
5198 Lisp_Object value;
5199
5200 /* See if a visual is specified. */
5201 value = display_x_get_resource (dpyinfo,
5202 build_string ("visualClass"),
5203 build_string ("VisualClass"),
5204 Qnil, Qnil);
5205 if (STRINGP (value))
5206 {
5207 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
5208 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
5209 depth, a decimal number. NAME is compared with case ignored. */
5210 char *s = (char *) alloca (SBYTES (value) + 1);
5211 char *dash;
5212 int i, class = -1;
5213 XVisualInfo vinfo;
5214
5215 strcpy (s, SDATA (value));
5216 dash = index (s, '-');
5217 if (dash)
5218 {
5219 dpyinfo->n_planes = atoi (dash + 1);
5220 *dash = '\0';
5221 }
5222 else
5223 /* We won't find a matching visual with depth 0, so that
5224 an error will be printed below. */
5225 dpyinfo->n_planes = 0;
5226
5227 /* Determine the visual class. */
5228 for (i = 0; visual_classes[i].name; ++i)
5229 if (xstricmp (s, visual_classes[i].name) == 0)
5230 {
5231 class = visual_classes[i].class;
5232 break;
5233 }
5234
5235 /* Look up a matching visual for the specified class. */
5236 if (class == -1
5237 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
5238 dpyinfo->n_planes, class, &vinfo))
5239 fatal ("Invalid visual specification `%s'", SDATA (value));
5240
5241 dpyinfo->visual = vinfo.visual;
5242 }
5243 else
5244 {
5245 int n_visuals;
5246 XVisualInfo *vinfo, vinfo_template;
5247
5248 dpyinfo->visual = DefaultVisualOfScreen (screen);
5249
5250 #ifdef HAVE_X11R4
5251 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
5252 #else
5253 vinfo_template.visualid = dpyinfo->visual->visualid;
5254 #endif
5255 vinfo_template.screen = XScreenNumberOfScreen (screen);
5256 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
5257 &vinfo_template, &n_visuals);
5258 if (n_visuals != 1)
5259 fatal ("Can't get proper X visual info");
5260
5261 dpyinfo->n_planes = vinfo->depth;
5262 XFree ((char *) vinfo);
5263 }
5264 }
5265
5266
5267 /* Return the X display structure for the display named NAME.
5268 Open a new connection if necessary. */
5269
5270 struct x_display_info *
5271 x_display_info_for_name (name)
5272 Lisp_Object name;
5273 {
5274 Lisp_Object names;
5275 struct x_display_info *dpyinfo;
5276
5277 CHECK_STRING (name);
5278
5279 if (! EQ (Vwindow_system, intern ("x")))
5280 error ("Not using X Windows");
5281
5282 for (dpyinfo = x_display_list, names = x_display_name_list;
5283 dpyinfo;
5284 dpyinfo = dpyinfo->next, names = XCDR (names))
5285 {
5286 Lisp_Object tem;
5287 tem = Fstring_equal (XCAR (XCAR (names)), name);
5288 if (!NILP (tem))
5289 return dpyinfo;
5290 }
5291
5292 /* Use this general default value to start with. */
5293 Vx_resource_name = Vinvocation_name;
5294
5295 validate_x_resource_name ();
5296
5297 dpyinfo = x_term_init (name, (char *)0,
5298 (char *) SDATA (Vx_resource_name));
5299
5300 if (dpyinfo == 0)
5301 error ("Cannot connect to X server %s", SDATA (name));
5302
5303 x_in_use = 1;
5304 XSETFASTINT (Vwindow_system_version, 11);
5305
5306 return dpyinfo;
5307 }
5308
5309
5310 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
5311 1, 3, 0,
5312 doc: /* Open a connection to an X server.
5313 DISPLAY is the name of the display to connect to.
5314 Optional second arg XRM-STRING is a string of resources in xrdb format.
5315 If the optional third arg MUST-SUCCEED is non-nil,
5316 terminate Emacs if we can't open the connection. */)
5317 (display, xrm_string, must_succeed)
5318 Lisp_Object display, xrm_string, must_succeed;
5319 {
5320 unsigned char *xrm_option;
5321 struct x_display_info *dpyinfo;
5322
5323 CHECK_STRING (display);
5324 if (! NILP (xrm_string))
5325 CHECK_STRING (xrm_string);
5326
5327 if (! EQ (Vwindow_system, intern ("x")))
5328 error ("Not using X Windows");
5329
5330 if (! NILP (xrm_string))
5331 xrm_option = (unsigned char *) SDATA (xrm_string);
5332 else
5333 xrm_option = (unsigned char *) 0;
5334
5335 validate_x_resource_name ();
5336
5337 /* This is what opens the connection and sets x_current_display.
5338 This also initializes many symbols, such as those used for input. */
5339 dpyinfo = x_term_init (display, xrm_option,
5340 (char *) SDATA (Vx_resource_name));
5341
5342 if (dpyinfo == 0)
5343 {
5344 if (!NILP (must_succeed))
5345 fatal ("Cannot connect to X server %s.\n\
5346 Check the DISPLAY environment variable or use `-d'.\n\
5347 Also use the `xhost' program to verify that it is set to permit\n\
5348 connections from your machine.\n",
5349 SDATA (display));
5350 else
5351 error ("Cannot connect to X server %s", SDATA (display));
5352 }
5353
5354 x_in_use = 1;
5355
5356 XSETFASTINT (Vwindow_system_version, 11);
5357 return Qnil;
5358 }
5359
5360 DEFUN ("x-close-connection", Fx_close_connection,
5361 Sx_close_connection, 1, 1, 0,
5362 doc: /* Close the connection to DISPLAY's X server.
5363 For DISPLAY, specify either a frame or a display name (a string).
5364 If DISPLAY is nil, that stands for the selected frame's display. */)
5365 (display)
5366 Lisp_Object display;
5367 {
5368 struct x_display_info *dpyinfo = check_x_display_info (display);
5369 int i;
5370
5371 if (dpyinfo->reference_count > 0)
5372 error ("Display still has frames on it");
5373
5374 BLOCK_INPUT;
5375 /* Free the fonts in the font table. */
5376 for (i = 0; i < dpyinfo->n_fonts; i++)
5377 if (dpyinfo->font_table[i].name)
5378 {
5379 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
5380 xfree (dpyinfo->font_table[i].full_name);
5381 xfree (dpyinfo->font_table[i].name);
5382 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
5383 }
5384
5385 x_destroy_all_bitmaps (dpyinfo);
5386 XSetCloseDownMode (dpyinfo->display, DestroyAll);
5387
5388 #ifdef USE_X_TOOLKIT
5389 XtCloseDisplay (dpyinfo->display);
5390 #else
5391 XCloseDisplay (dpyinfo->display);
5392 #endif
5393
5394 x_delete_display (dpyinfo);
5395 UNBLOCK_INPUT;
5396
5397 return Qnil;
5398 }
5399
5400 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
5401 doc: /* Return the list of display names that Emacs has connections to. */)
5402 ()
5403 {
5404 Lisp_Object tail, result;
5405
5406 result = Qnil;
5407 for (tail = x_display_name_list; ! NILP (tail); tail = XCDR (tail))
5408 result = Fcons (XCAR (XCAR (tail)), result);
5409
5410 return result;
5411 }
5412
5413 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
5414 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
5415 If ON is nil, allow buffering of requests.
5416 Turning on synchronization prohibits the Xlib routines from buffering
5417 requests and seriously degrades performance, but makes debugging much
5418 easier.
5419 The optional second argument DISPLAY specifies which display to act on.
5420 DISPLAY should be either a frame or a display name (a string).
5421 If DISPLAY is omitted or nil, that stands for the selected frame's display. */)
5422 (on, display)
5423 Lisp_Object display, on;
5424 {
5425 struct x_display_info *dpyinfo = check_x_display_info (display);
5426
5427 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
5428
5429 return Qnil;
5430 }
5431
5432 /* Wait for responses to all X commands issued so far for frame F. */
5433
5434 void
5435 x_sync (f)
5436 FRAME_PTR f;
5437 {
5438 BLOCK_INPUT;
5439 XSync (FRAME_X_DISPLAY (f), False);
5440 UNBLOCK_INPUT;
5441 }
5442
5443 \f
5444 /***********************************************************************
5445 Image types
5446 ***********************************************************************/
5447
5448 /* Value is the number of elements of vector VECTOR. */
5449
5450 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
5451
5452 /* List of supported image types. Use define_image_type to add new
5453 types. Use lookup_image_type to find a type for a given symbol. */
5454
5455 static struct image_type *image_types;
5456
5457 /* The symbol `image' which is the car of the lists used to represent
5458 images in Lisp. */
5459
5460 extern Lisp_Object Qimage;
5461
5462 /* The symbol `xbm' which is used as the type symbol for XBM images. */
5463
5464 Lisp_Object Qxbm;
5465
5466 /* Keywords. */
5467
5468 extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
5469 extern Lisp_Object QCdata, QCtype;
5470 Lisp_Object QCascent, QCmargin, QCrelief;
5471 Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
5472 Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
5473
5474 /* Other symbols. */
5475
5476 Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
5477
5478 /* Time in seconds after which images should be removed from the cache
5479 if not displayed. */
5480
5481 Lisp_Object Vimage_cache_eviction_delay;
5482
5483 /* Function prototypes. */
5484
5485 static void define_image_type P_ ((struct image_type *type));
5486 static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
5487 static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
5488 static void x_laplace P_ ((struct frame *, struct image *));
5489 static void x_emboss P_ ((struct frame *, struct image *));
5490 static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
5491 Lisp_Object));
5492
5493
5494 /* Define a new image type from TYPE. This adds a copy of TYPE to
5495 image_types and adds the symbol *TYPE->type to Vimage_types. */
5496
5497 static void
5498 define_image_type (type)
5499 struct image_type *type;
5500 {
5501 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
5502 The initialized data segment is read-only. */
5503 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
5504 bcopy (type, p, sizeof *p);
5505 p->next = image_types;
5506 image_types = p;
5507 Vimage_types = Fcons (*p->type, Vimage_types);
5508 }
5509
5510
5511 /* Look up image type SYMBOL, and return a pointer to its image_type
5512 structure. Value is null if SYMBOL is not a known image type. */
5513
5514 static INLINE struct image_type *
5515 lookup_image_type (symbol)
5516 Lisp_Object symbol;
5517 {
5518 struct image_type *type;
5519
5520 for (type = image_types; type; type = type->next)
5521 if (EQ (symbol, *type->type))
5522 break;
5523
5524 return type;
5525 }
5526
5527
5528 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
5529 valid image specification is a list whose car is the symbol
5530 `image', and whose rest is a property list. The property list must
5531 contain a value for key `:type'. That value must be the name of a
5532 supported image type. The rest of the property list depends on the
5533 image type. */
5534
5535 int
5536 valid_image_p (object)
5537 Lisp_Object object;
5538 {
5539 int valid_p = 0;
5540
5541 if (CONSP (object) && EQ (XCAR (object), Qimage))
5542 {
5543 Lisp_Object tem;
5544
5545 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
5546 if (EQ (XCAR (tem), QCtype))
5547 {
5548 tem = XCDR (tem);
5549 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
5550 {
5551 struct image_type *type;
5552 type = lookup_image_type (XCAR (tem));
5553 if (type)
5554 valid_p = type->valid_p (object);
5555 }
5556
5557 break;
5558 }
5559 }
5560
5561 return valid_p;
5562 }
5563
5564
5565 /* Log error message with format string FORMAT and argument ARG.
5566 Signaling an error, e.g. when an image cannot be loaded, is not a
5567 good idea because this would interrupt redisplay, and the error
5568 message display would lead to another redisplay. This function
5569 therefore simply displays a message. */
5570
5571 static void
5572 image_error (format, arg1, arg2)
5573 char *format;
5574 Lisp_Object arg1, arg2;
5575 {
5576 add_to_log (format, arg1, arg2);
5577 }
5578
5579
5580 \f
5581 /***********************************************************************
5582 Image specifications
5583 ***********************************************************************/
5584
5585 enum image_value_type
5586 {
5587 IMAGE_DONT_CHECK_VALUE_TYPE,
5588 IMAGE_STRING_VALUE,
5589 IMAGE_STRING_OR_NIL_VALUE,
5590 IMAGE_SYMBOL_VALUE,
5591 IMAGE_POSITIVE_INTEGER_VALUE,
5592 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
5593 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
5594 IMAGE_ASCENT_VALUE,
5595 IMAGE_INTEGER_VALUE,
5596 IMAGE_FUNCTION_VALUE,
5597 IMAGE_NUMBER_VALUE,
5598 IMAGE_BOOL_VALUE
5599 };
5600
5601 /* Structure used when parsing image specifications. */
5602
5603 struct image_keyword
5604 {
5605 /* Name of keyword. */
5606 char *name;
5607
5608 /* The type of value allowed. */
5609 enum image_value_type type;
5610
5611 /* Non-zero means key must be present. */
5612 int mandatory_p;
5613
5614 /* Used to recognize duplicate keywords in a property list. */
5615 int count;
5616
5617 /* The value that was found. */
5618 Lisp_Object value;
5619 };
5620
5621
5622 static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
5623 int, Lisp_Object));
5624 static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
5625
5626
5627 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
5628 has the format (image KEYWORD VALUE ...). One of the keyword/
5629 value pairs must be `:type TYPE'. KEYWORDS is a vector of
5630 image_keywords structures of size NKEYWORDS describing other
5631 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
5632
5633 static int
5634 parse_image_spec (spec, keywords, nkeywords, type)
5635 Lisp_Object spec;
5636 struct image_keyword *keywords;
5637 int nkeywords;
5638 Lisp_Object type;
5639 {
5640 int i;
5641 Lisp_Object plist;
5642
5643 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
5644 return 0;
5645
5646 plist = XCDR (spec);
5647 while (CONSP (plist))
5648 {
5649 Lisp_Object key, value;
5650
5651 /* First element of a pair must be a symbol. */
5652 key = XCAR (plist);
5653 plist = XCDR (plist);
5654 if (!SYMBOLP (key))
5655 return 0;
5656
5657 /* There must follow a value. */
5658 if (!CONSP (plist))
5659 return 0;
5660 value = XCAR (plist);
5661 plist = XCDR (plist);
5662
5663 /* Find key in KEYWORDS. Error if not found. */
5664 for (i = 0; i < nkeywords; ++i)
5665 if (strcmp (keywords[i].name, SDATA (SYMBOL_NAME (key))) == 0)
5666 break;
5667
5668 if (i == nkeywords)
5669 continue;
5670
5671 /* Record that we recognized the keyword. If a keywords
5672 was found more than once, it's an error. */
5673 keywords[i].value = value;
5674 ++keywords[i].count;
5675
5676 if (keywords[i].count > 1)
5677 return 0;
5678
5679 /* Check type of value against allowed type. */
5680 switch (keywords[i].type)
5681 {
5682 case IMAGE_STRING_VALUE:
5683 if (!STRINGP (value))
5684 return 0;
5685 break;
5686
5687 case IMAGE_STRING_OR_NIL_VALUE:
5688 if (!STRINGP (value) && !NILP (value))
5689 return 0;
5690 break;
5691
5692 case IMAGE_SYMBOL_VALUE:
5693 if (!SYMBOLP (value))
5694 return 0;
5695 break;
5696
5697 case IMAGE_POSITIVE_INTEGER_VALUE:
5698 if (!INTEGERP (value) || XINT (value) <= 0)
5699 return 0;
5700 break;
5701
5702 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
5703 if (INTEGERP (value) && XINT (value) >= 0)
5704 break;
5705 if (CONSP (value)
5706 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
5707 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
5708 break;
5709 return 0;
5710
5711 case IMAGE_ASCENT_VALUE:
5712 if (SYMBOLP (value) && EQ (value, Qcenter))
5713 break;
5714 else if (INTEGERP (value)
5715 && XINT (value) >= 0
5716 && XINT (value) <= 100)
5717 break;
5718 return 0;
5719
5720 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
5721 if (!INTEGERP (value) || XINT (value) < 0)
5722 return 0;
5723 break;
5724
5725 case IMAGE_DONT_CHECK_VALUE_TYPE:
5726 break;
5727
5728 case IMAGE_FUNCTION_VALUE:
5729 value = indirect_function (value);
5730 if (SUBRP (value)
5731 || COMPILEDP (value)
5732 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
5733 break;
5734 return 0;
5735
5736 case IMAGE_NUMBER_VALUE:
5737 if (!INTEGERP (value) && !FLOATP (value))
5738 return 0;
5739 break;
5740
5741 case IMAGE_INTEGER_VALUE:
5742 if (!INTEGERP (value))
5743 return 0;
5744 break;
5745
5746 case IMAGE_BOOL_VALUE:
5747 if (!NILP (value) && !EQ (value, Qt))
5748 return 0;
5749 break;
5750
5751 default:
5752 abort ();
5753 break;
5754 }
5755
5756 if (EQ (key, QCtype) && !EQ (type, value))
5757 return 0;
5758 }
5759
5760 /* Check that all mandatory fields are present. */
5761 for (i = 0; i < nkeywords; ++i)
5762 if (keywords[i].mandatory_p && keywords[i].count == 0)
5763 return 0;
5764
5765 return NILP (plist);
5766 }
5767
5768
5769 /* Return the value of KEY in image specification SPEC. Value is nil
5770 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
5771 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
5772
5773 static Lisp_Object
5774 image_spec_value (spec, key, found)
5775 Lisp_Object spec, key;
5776 int *found;
5777 {
5778 Lisp_Object tail;
5779
5780 xassert (valid_image_p (spec));
5781
5782 for (tail = XCDR (spec);
5783 CONSP (tail) && CONSP (XCDR (tail));
5784 tail = XCDR (XCDR (tail)))
5785 {
5786 if (EQ (XCAR (tail), key))
5787 {
5788 if (found)
5789 *found = 1;
5790 return XCAR (XCDR (tail));
5791 }
5792 }
5793
5794 if (found)
5795 *found = 0;
5796 return Qnil;
5797 }
5798
5799
5800 DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
5801 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
5802 PIXELS non-nil means return the size in pixels, otherwise return the
5803 size in canonical character units.
5804 FRAME is the frame on which the image will be displayed. FRAME nil
5805 or omitted means use the selected frame. */)
5806 (spec, pixels, frame)
5807 Lisp_Object spec, pixels, frame;
5808 {
5809 Lisp_Object size;
5810
5811 size = Qnil;
5812 if (valid_image_p (spec))
5813 {
5814 struct frame *f = check_x_frame (frame);
5815 int id = lookup_image (f, spec);
5816 struct image *img = IMAGE_FROM_ID (f, id);
5817 int width = img->width + 2 * img->hmargin;
5818 int height = img->height + 2 * img->vmargin;
5819
5820 if (NILP (pixels))
5821 size = Fcons (make_float ((double) width / CANON_X_UNIT (f)),
5822 make_float ((double) height / CANON_Y_UNIT (f)));
5823 else
5824 size = Fcons (make_number (width), make_number (height));
5825 }
5826 else
5827 error ("Invalid image specification");
5828
5829 return size;
5830 }
5831
5832
5833 DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
5834 doc: /* Return t if image SPEC has a mask bitmap.
5835 FRAME is the frame on which the image will be displayed. FRAME nil
5836 or omitted means use the selected frame. */)
5837 (spec, frame)
5838 Lisp_Object spec, frame;
5839 {
5840 Lisp_Object mask;
5841
5842 mask = Qnil;
5843 if (valid_image_p (spec))
5844 {
5845 struct frame *f = check_x_frame (frame);
5846 int id = lookup_image (f, spec);
5847 struct image *img = IMAGE_FROM_ID (f, id);
5848 if (img->mask)
5849 mask = Qt;
5850 }
5851 else
5852 error ("Invalid image specification");
5853
5854 return mask;
5855 }
5856
5857
5858 \f
5859 /***********************************************************************
5860 Image type independent image structures
5861 ***********************************************************************/
5862
5863 static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
5864 static void free_image P_ ((struct frame *f, struct image *img));
5865
5866
5867 /* Allocate and return a new image structure for image specification
5868 SPEC. SPEC has a hash value of HASH. */
5869
5870 static struct image *
5871 make_image (spec, hash)
5872 Lisp_Object spec;
5873 unsigned hash;
5874 {
5875 struct image *img = (struct image *) xmalloc (sizeof *img);
5876
5877 xassert (valid_image_p (spec));
5878 bzero (img, sizeof *img);
5879 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
5880 xassert (img->type != NULL);
5881 img->spec = spec;
5882 img->data.lisp_val = Qnil;
5883 img->ascent = DEFAULT_IMAGE_ASCENT;
5884 img->hash = hash;
5885 return img;
5886 }
5887
5888
5889 /* Free image IMG which was used on frame F, including its resources. */
5890
5891 static void
5892 free_image (f, img)
5893 struct frame *f;
5894 struct image *img;
5895 {
5896 if (img)
5897 {
5898 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5899
5900 /* Remove IMG from the hash table of its cache. */
5901 if (img->prev)
5902 img->prev->next = img->next;
5903 else
5904 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
5905
5906 if (img->next)
5907 img->next->prev = img->prev;
5908
5909 c->images[img->id] = NULL;
5910
5911 /* Free resources, then free IMG. */
5912 img->type->free (f, img);
5913 xfree (img);
5914 }
5915 }
5916
5917
5918 /* Prepare image IMG for display on frame F. Must be called before
5919 drawing an image. */
5920
5921 void
5922 prepare_image_for_display (f, img)
5923 struct frame *f;
5924 struct image *img;
5925 {
5926 EMACS_TIME t;
5927
5928 /* We're about to display IMG, so set its timestamp to `now'. */
5929 EMACS_GET_TIME (t);
5930 img->timestamp = EMACS_SECS (t);
5931
5932 /* If IMG doesn't have a pixmap yet, load it now, using the image
5933 type dependent loader function. */
5934 if (img->pixmap == None && !img->load_failed_p)
5935 img->load_failed_p = img->type->load (f, img) == 0;
5936 }
5937
5938
5939 /* Value is the number of pixels for the ascent of image IMG when
5940 drawn in face FACE. */
5941
5942 int
5943 image_ascent (img, face)
5944 struct image *img;
5945 struct face *face;
5946 {
5947 int height = img->height + img->vmargin;
5948 int ascent;
5949
5950 if (img->ascent == CENTERED_IMAGE_ASCENT)
5951 {
5952 if (face->font)
5953 /* This expression is arranged so that if the image can't be
5954 exactly centered, it will be moved slightly up. This is
5955 because a typical font is `top-heavy' (due to the presence
5956 uppercase letters), so the image placement should err towards
5957 being top-heavy too. It also just generally looks better. */
5958 ascent = (height + face->font->ascent - face->font->descent + 1) / 2;
5959 else
5960 ascent = height / 2;
5961 }
5962 else
5963 ascent = height * img->ascent / 100.0;
5964
5965 return ascent;
5966 }
5967
5968 \f
5969 /* Image background colors. */
5970
5971 static unsigned long
5972 four_corners_best (ximg, width, height)
5973 XImage *ximg;
5974 unsigned long width, height;
5975 {
5976 unsigned long corners[4], best;
5977 int i, best_count;
5978
5979 /* Get the colors at the corners of ximg. */
5980 corners[0] = XGetPixel (ximg, 0, 0);
5981 corners[1] = XGetPixel (ximg, width - 1, 0);
5982 corners[2] = XGetPixel (ximg, width - 1, height - 1);
5983 corners[3] = XGetPixel (ximg, 0, height - 1);
5984
5985 /* Choose the most frequently found color as background. */
5986 for (i = best_count = 0; i < 4; ++i)
5987 {
5988 int j, n;
5989
5990 for (j = n = 0; j < 4; ++j)
5991 if (corners[i] == corners[j])
5992 ++n;
5993
5994 if (n > best_count)
5995 best = corners[i], best_count = n;
5996 }
5997
5998 return best;
5999 }
6000
6001 /* Return the `background' field of IMG. If IMG doesn't have one yet,
6002 it is guessed heuristically. If non-zero, XIMG is an existing XImage
6003 object to use for the heuristic. */
6004
6005 unsigned long
6006 image_background (img, f, ximg)
6007 struct image *img;
6008 struct frame *f;
6009 XImage *ximg;
6010 {
6011 if (! img->background_valid)
6012 /* IMG doesn't have a background yet, try to guess a reasonable value. */
6013 {
6014 int free_ximg = !ximg;
6015
6016 if (! ximg)
6017 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
6018 0, 0, img->width, img->height, ~0, ZPixmap);
6019
6020 img->background = four_corners_best (ximg, img->width, img->height);
6021
6022 if (free_ximg)
6023 XDestroyImage (ximg);
6024
6025 img->background_valid = 1;
6026 }
6027
6028 return img->background;
6029 }
6030
6031 /* Return the `background_transparent' field of IMG. If IMG doesn't
6032 have one yet, it is guessed heuristically. If non-zero, MASK is an
6033 existing XImage object to use for the heuristic. */
6034
6035 int
6036 image_background_transparent (img, f, mask)
6037 struct image *img;
6038 struct frame *f;
6039 XImage *mask;
6040 {
6041 if (! img->background_transparent_valid)
6042 /* IMG doesn't have a background yet, try to guess a reasonable value. */
6043 {
6044 if (img->mask)
6045 {
6046 int free_mask = !mask;
6047
6048 if (! mask)
6049 mask = XGetImage (FRAME_X_DISPLAY (f), img->mask,
6050 0, 0, img->width, img->height, ~0, ZPixmap);
6051
6052 img->background_transparent
6053 = !four_corners_best (mask, img->width, img->height);
6054
6055 if (free_mask)
6056 XDestroyImage (mask);
6057 }
6058 else
6059 img->background_transparent = 0;
6060
6061 img->background_transparent_valid = 1;
6062 }
6063
6064 return img->background_transparent;
6065 }
6066
6067 \f
6068 /***********************************************************************
6069 Helper functions for X image types
6070 ***********************************************************************/
6071
6072 static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
6073 int, int));
6074 static void x_clear_image P_ ((struct frame *f, struct image *img));
6075 static unsigned long x_alloc_image_color P_ ((struct frame *f,
6076 struct image *img,
6077 Lisp_Object color_name,
6078 unsigned long dflt));
6079
6080
6081 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
6082 free the pixmap if any. MASK_P non-zero means clear the mask
6083 pixmap if any. COLORS_P non-zero means free colors allocated for
6084 the image, if any. */
6085
6086 static void
6087 x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
6088 struct frame *f;
6089 struct image *img;
6090 int pixmap_p, mask_p, colors_p;
6091 {
6092 if (pixmap_p && img->pixmap)
6093 {
6094 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
6095 img->pixmap = None;
6096 img->background_valid = 0;
6097 }
6098
6099 if (mask_p && img->mask)
6100 {
6101 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
6102 img->mask = None;
6103 img->background_transparent_valid = 0;
6104 }
6105
6106 if (colors_p && img->ncolors)
6107 {
6108 x_free_colors (f, img->colors, img->ncolors);
6109 xfree (img->colors);
6110 img->colors = NULL;
6111 img->ncolors = 0;
6112 }
6113 }
6114
6115 /* Free X resources of image IMG which is used on frame F. */
6116
6117 static void
6118 x_clear_image (f, img)
6119 struct frame *f;
6120 struct image *img;
6121 {
6122 BLOCK_INPUT;
6123 x_clear_image_1 (f, img, 1, 1, 1);
6124 UNBLOCK_INPUT;
6125 }
6126
6127
6128 /* Allocate color COLOR_NAME for image IMG on frame F. If color
6129 cannot be allocated, use DFLT. Add a newly allocated color to
6130 IMG->colors, so that it can be freed again. Value is the pixel
6131 color. */
6132
6133 static unsigned long
6134 x_alloc_image_color (f, img, color_name, dflt)
6135 struct frame *f;
6136 struct image *img;
6137 Lisp_Object color_name;
6138 unsigned long dflt;
6139 {
6140 XColor color;
6141 unsigned long result;
6142
6143 xassert (STRINGP (color_name));
6144
6145 if (x_defined_color (f, SDATA (color_name), &color, 1))
6146 {
6147 /* This isn't called frequently so we get away with simply
6148 reallocating the color vector to the needed size, here. */
6149 ++img->ncolors;
6150 img->colors =
6151 (unsigned long *) xrealloc (img->colors,
6152 img->ncolors * sizeof *img->colors);
6153 img->colors[img->ncolors - 1] = color.pixel;
6154 result = color.pixel;
6155 }
6156 else
6157 result = dflt;
6158
6159 return result;
6160 }
6161
6162
6163 \f
6164 /***********************************************************************
6165 Image Cache
6166 ***********************************************************************/
6167
6168 static void cache_image P_ ((struct frame *f, struct image *img));
6169 static void postprocess_image P_ ((struct frame *, struct image *));
6170
6171
6172 /* Return a new, initialized image cache that is allocated from the
6173 heap. Call free_image_cache to free an image cache. */
6174
6175 struct image_cache *
6176 make_image_cache ()
6177 {
6178 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
6179 int size;
6180
6181 bzero (c, sizeof *c);
6182 c->size = 50;
6183 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
6184 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
6185 c->buckets = (struct image **) xmalloc (size);
6186 bzero (c->buckets, size);
6187 return c;
6188 }
6189
6190
6191 /* Free image cache of frame F. Be aware that X frames share images
6192 caches. */
6193
6194 void
6195 free_image_cache (f)
6196 struct frame *f;
6197 {
6198 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6199 if (c)
6200 {
6201 int i;
6202
6203 /* Cache should not be referenced by any frame when freed. */
6204 xassert (c->refcount == 0);
6205
6206 for (i = 0; i < c->used; ++i)
6207 free_image (f, c->images[i]);
6208 xfree (c->images);
6209 xfree (c->buckets);
6210 xfree (c);
6211 FRAME_X_IMAGE_CACHE (f) = NULL;
6212 }
6213 }
6214
6215
6216 /* Clear image cache of frame F. FORCE_P non-zero means free all
6217 images. FORCE_P zero means clear only images that haven't been
6218 displayed for some time. Should be called from time to time to
6219 reduce the number of loaded images. If image-eviction-seconds is
6220 non-nil, this frees images in the cache which weren't displayed for
6221 at least that many seconds. */
6222
6223 void
6224 clear_image_cache (f, force_p)
6225 struct frame *f;
6226 int force_p;
6227 {
6228 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6229
6230 if (c && INTEGERP (Vimage_cache_eviction_delay))
6231 {
6232 EMACS_TIME t;
6233 unsigned long old;
6234 int i, nfreed;
6235
6236 EMACS_GET_TIME (t);
6237 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
6238
6239 /* Block input so that we won't be interrupted by a SIGIO
6240 while being in an inconsistent state. */
6241 BLOCK_INPUT;
6242
6243 for (i = nfreed = 0; i < c->used; ++i)
6244 {
6245 struct image *img = c->images[i];
6246 if (img != NULL
6247 && (force_p || img->timestamp < old))
6248 {
6249 free_image (f, img);
6250 ++nfreed;
6251 }
6252 }
6253
6254 /* We may be clearing the image cache because, for example,
6255 Emacs was iconified for a longer period of time. In that
6256 case, current matrices may still contain references to
6257 images freed above. So, clear these matrices. */
6258 if (nfreed)
6259 {
6260 Lisp_Object tail, frame;
6261
6262 FOR_EACH_FRAME (tail, frame)
6263 {
6264 struct frame *f = XFRAME (frame);
6265 if (FRAME_X_P (f)
6266 && FRAME_X_IMAGE_CACHE (f) == c)
6267 clear_current_matrices (f);
6268 }
6269
6270 ++windows_or_buffers_changed;
6271 }
6272
6273 UNBLOCK_INPUT;
6274 }
6275 }
6276
6277
6278 DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
6279 0, 1, 0,
6280 doc: /* Clear the image cache of FRAME.
6281 FRAME nil or omitted means use the selected frame.
6282 FRAME t means clear the image caches of all frames. */)
6283 (frame)
6284 Lisp_Object frame;
6285 {
6286 if (EQ (frame, Qt))
6287 {
6288 Lisp_Object tail;
6289
6290 FOR_EACH_FRAME (tail, frame)
6291 if (FRAME_X_P (XFRAME (frame)))
6292 clear_image_cache (XFRAME (frame), 1);
6293 }
6294 else
6295 clear_image_cache (check_x_frame (frame), 1);
6296
6297 return Qnil;
6298 }
6299
6300
6301 /* Compute masks and transform image IMG on frame F, as specified
6302 by the image's specification, */
6303
6304 static void
6305 postprocess_image (f, img)
6306 struct frame *f;
6307 struct image *img;
6308 {
6309 /* Manipulation of the image's mask. */
6310 if (img->pixmap)
6311 {
6312 Lisp_Object conversion, spec;
6313 Lisp_Object mask;
6314
6315 spec = img->spec;
6316
6317 /* `:heuristic-mask t'
6318 `:mask heuristic'
6319 means build a mask heuristically.
6320 `:heuristic-mask (R G B)'
6321 `:mask (heuristic (R G B))'
6322 means build a mask from color (R G B) in the
6323 image.
6324 `:mask nil'
6325 means remove a mask, if any. */
6326
6327 mask = image_spec_value (spec, QCheuristic_mask, NULL);
6328 if (!NILP (mask))
6329 x_build_heuristic_mask (f, img, mask);
6330 else
6331 {
6332 int found_p;
6333
6334 mask = image_spec_value (spec, QCmask, &found_p);
6335
6336 if (EQ (mask, Qheuristic))
6337 x_build_heuristic_mask (f, img, Qt);
6338 else if (CONSP (mask)
6339 && EQ (XCAR (mask), Qheuristic))
6340 {
6341 if (CONSP (XCDR (mask)))
6342 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
6343 else
6344 x_build_heuristic_mask (f, img, XCDR (mask));
6345 }
6346 else if (NILP (mask) && found_p && img->mask)
6347 {
6348 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
6349 img->mask = None;
6350 }
6351 }
6352
6353
6354 /* Should we apply an image transformation algorithm? */
6355 conversion = image_spec_value (spec, QCconversion, NULL);
6356 if (EQ (conversion, Qdisabled))
6357 x_disable_image (f, img);
6358 else if (EQ (conversion, Qlaplace))
6359 x_laplace (f, img);
6360 else if (EQ (conversion, Qemboss))
6361 x_emboss (f, img);
6362 else if (CONSP (conversion)
6363 && EQ (XCAR (conversion), Qedge_detection))
6364 {
6365 Lisp_Object tem;
6366 tem = XCDR (conversion);
6367 if (CONSP (tem))
6368 x_edge_detection (f, img,
6369 Fplist_get (tem, QCmatrix),
6370 Fplist_get (tem, QCcolor_adjustment));
6371 }
6372 }
6373 }
6374
6375
6376 /* Return the id of image with Lisp specification SPEC on frame F.
6377 SPEC must be a valid Lisp image specification (see valid_image_p). */
6378
6379 int
6380 lookup_image (f, spec)
6381 struct frame *f;
6382 Lisp_Object spec;
6383 {
6384 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6385 struct image *img;
6386 int i;
6387 unsigned hash;
6388 struct gcpro gcpro1;
6389 EMACS_TIME now;
6390
6391 /* F must be a window-system frame, and SPEC must be a valid image
6392 specification. */
6393 xassert (FRAME_WINDOW_P (f));
6394 xassert (valid_image_p (spec));
6395
6396 GCPRO1 (spec);
6397
6398 /* Look up SPEC in the hash table of the image cache. */
6399 hash = sxhash (spec, 0);
6400 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
6401
6402 for (img = c->buckets[i]; img; img = img->next)
6403 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
6404 break;
6405
6406 /* If not found, create a new image and cache it. */
6407 if (img == NULL)
6408 {
6409 extern Lisp_Object Qpostscript;
6410
6411 BLOCK_INPUT;
6412 img = make_image (spec, hash);
6413 cache_image (f, img);
6414 img->load_failed_p = img->type->load (f, img) == 0;
6415
6416 /* If we can't load the image, and we don't have a width and
6417 height, use some arbitrary width and height so that we can
6418 draw a rectangle for it. */
6419 if (img->load_failed_p)
6420 {
6421 Lisp_Object value;
6422
6423 value = image_spec_value (spec, QCwidth, NULL);
6424 img->width = (INTEGERP (value)
6425 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
6426 value = image_spec_value (spec, QCheight, NULL);
6427 img->height = (INTEGERP (value)
6428 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
6429 }
6430 else
6431 {
6432 /* Handle image type independent image attributes
6433 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
6434 `:background COLOR'. */
6435 Lisp_Object ascent, margin, relief, bg;
6436
6437 ascent = image_spec_value (spec, QCascent, NULL);
6438 if (INTEGERP (ascent))
6439 img->ascent = XFASTINT (ascent);
6440 else if (EQ (ascent, Qcenter))
6441 img->ascent = CENTERED_IMAGE_ASCENT;
6442
6443 margin = image_spec_value (spec, QCmargin, NULL);
6444 if (INTEGERP (margin) && XINT (margin) >= 0)
6445 img->vmargin = img->hmargin = XFASTINT (margin);
6446 else if (CONSP (margin) && INTEGERP (XCAR (margin))
6447 && INTEGERP (XCDR (margin)))
6448 {
6449 if (XINT (XCAR (margin)) > 0)
6450 img->hmargin = XFASTINT (XCAR (margin));
6451 if (XINT (XCDR (margin)) > 0)
6452 img->vmargin = XFASTINT (XCDR (margin));
6453 }
6454
6455 relief = image_spec_value (spec, QCrelief, NULL);
6456 if (INTEGERP (relief))
6457 {
6458 img->relief = XINT (relief);
6459 img->hmargin += abs (img->relief);
6460 img->vmargin += abs (img->relief);
6461 }
6462
6463 if (! img->background_valid)
6464 {
6465 bg = image_spec_value (img->spec, QCbackground, NULL);
6466 if (!NILP (bg))
6467 {
6468 img->background
6469 = x_alloc_image_color (f, img, bg,
6470 FRAME_BACKGROUND_PIXEL (f));
6471 img->background_valid = 1;
6472 }
6473 }
6474
6475 /* Do image transformations and compute masks, unless we
6476 don't have the image yet. */
6477 if (!EQ (*img->type->type, Qpostscript))
6478 postprocess_image (f, img);
6479 }
6480
6481 UNBLOCK_INPUT;
6482 xassert (!interrupt_input_blocked);
6483 }
6484
6485 /* We're using IMG, so set its timestamp to `now'. */
6486 EMACS_GET_TIME (now);
6487 img->timestamp = EMACS_SECS (now);
6488
6489 UNGCPRO;
6490
6491 /* Value is the image id. */
6492 return img->id;
6493 }
6494
6495
6496 /* Cache image IMG in the image cache of frame F. */
6497
6498 static void
6499 cache_image (f, img)
6500 struct frame *f;
6501 struct image *img;
6502 {
6503 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6504 int i;
6505
6506 /* Find a free slot in c->images. */
6507 for (i = 0; i < c->used; ++i)
6508 if (c->images[i] == NULL)
6509 break;
6510
6511 /* If no free slot found, maybe enlarge c->images. */
6512 if (i == c->used && c->used == c->size)
6513 {
6514 c->size *= 2;
6515 c->images = (struct image **) xrealloc (c->images,
6516 c->size * sizeof *c->images);
6517 }
6518
6519 /* Add IMG to c->images, and assign IMG an id. */
6520 c->images[i] = img;
6521 img->id = i;
6522 if (i == c->used)
6523 ++c->used;
6524
6525 /* Add IMG to the cache's hash table. */
6526 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
6527 img->next = c->buckets[i];
6528 if (img->next)
6529 img->next->prev = img;
6530 img->prev = NULL;
6531 c->buckets[i] = img;
6532 }
6533
6534
6535 /* Call FN on every image in the image cache of frame F. Used to mark
6536 Lisp Objects in the image cache. */
6537
6538 void
6539 forall_images_in_image_cache (f, fn)
6540 struct frame *f;
6541 void (*fn) P_ ((struct image *img));
6542 {
6543 if (FRAME_LIVE_P (f) && FRAME_X_P (f))
6544 {
6545 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6546 if (c)
6547 {
6548 int i;
6549 for (i = 0; i < c->used; ++i)
6550 if (c->images[i])
6551 fn (c->images[i]);
6552 }
6553 }
6554 }
6555
6556
6557 \f
6558 /***********************************************************************
6559 X support code
6560 ***********************************************************************/
6561
6562 static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
6563 XImage **, Pixmap *));
6564 static void x_destroy_x_image P_ ((XImage *));
6565 static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
6566
6567
6568 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
6569 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
6570 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
6571 via xmalloc. Print error messages via image_error if an error
6572 occurs. Value is non-zero if successful. */
6573
6574 static int
6575 x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
6576 struct frame *f;
6577 int width, height, depth;
6578 XImage **ximg;
6579 Pixmap *pixmap;
6580 {
6581 Display *display = FRAME_X_DISPLAY (f);
6582 Screen *screen = FRAME_X_SCREEN (f);
6583 Window window = FRAME_X_WINDOW (f);
6584
6585 xassert (interrupt_input_blocked);
6586
6587 if (depth <= 0)
6588 depth = DefaultDepthOfScreen (screen);
6589 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
6590 depth, ZPixmap, 0, NULL, width, height,
6591 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
6592 if (*ximg == NULL)
6593 {
6594 image_error ("Unable to allocate X image", Qnil, Qnil);
6595 return 0;
6596 }
6597
6598 /* Allocate image raster. */
6599 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
6600
6601 /* Allocate a pixmap of the same size. */
6602 *pixmap = XCreatePixmap (display, window, width, height, depth);
6603 if (*pixmap == None)
6604 {
6605 x_destroy_x_image (*ximg);
6606 *ximg = NULL;
6607 image_error ("Unable to create X pixmap", Qnil, Qnil);
6608 return 0;
6609 }
6610
6611 return 1;
6612 }
6613
6614
6615 /* Destroy XImage XIMG. Free XIMG->data. */
6616
6617 static void
6618 x_destroy_x_image (ximg)
6619 XImage *ximg;
6620 {
6621 xassert (interrupt_input_blocked);
6622 if (ximg)
6623 {
6624 xfree (ximg->data);
6625 ximg->data = NULL;
6626 XDestroyImage (ximg);
6627 }
6628 }
6629
6630
6631 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
6632 are width and height of both the image and pixmap. */
6633
6634 static void
6635 x_put_x_image (f, ximg, pixmap, width, height)
6636 struct frame *f;
6637 XImage *ximg;
6638 Pixmap pixmap;
6639 {
6640 GC gc;
6641
6642 xassert (interrupt_input_blocked);
6643 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
6644 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
6645 XFreeGC (FRAME_X_DISPLAY (f), gc);
6646 }
6647
6648
6649 \f
6650 /***********************************************************************
6651 File Handling
6652 ***********************************************************************/
6653
6654 static Lisp_Object x_find_image_file P_ ((Lisp_Object));
6655 static char *slurp_file P_ ((char *, int *));
6656
6657
6658 /* Find image file FILE. Look in data-directory, then
6659 x-bitmap-file-path. Value is the full name of the file found, or
6660 nil if not found. */
6661
6662 static Lisp_Object
6663 x_find_image_file (file)
6664 Lisp_Object file;
6665 {
6666 Lisp_Object file_found, search_path;
6667 struct gcpro gcpro1, gcpro2;
6668 int fd;
6669
6670 file_found = Qnil;
6671 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
6672 GCPRO2 (file_found, search_path);
6673
6674 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
6675 fd = openp (search_path, file, Qnil, &file_found, Qnil);
6676
6677 if (fd == -1)
6678 file_found = Qnil;
6679 else
6680 close (fd);
6681
6682 UNGCPRO;
6683 return file_found;
6684 }
6685
6686
6687 /* Read FILE into memory. Value is a pointer to a buffer allocated
6688 with xmalloc holding FILE's contents. Value is null if an error
6689 occurred. *SIZE is set to the size of the file. */
6690
6691 static char *
6692 slurp_file (file, size)
6693 char *file;
6694 int *size;
6695 {
6696 FILE *fp = NULL;
6697 char *buf = NULL;
6698 struct stat st;
6699
6700 if (stat (file, &st) == 0
6701 && (fp = fopen (file, "r")) != NULL
6702 && (buf = (char *) xmalloc (st.st_size),
6703 fread (buf, 1, st.st_size, fp) == st.st_size))
6704 {
6705 *size = st.st_size;
6706 fclose (fp);
6707 }
6708 else
6709 {
6710 if (fp)
6711 fclose (fp);
6712 if (buf)
6713 {
6714 xfree (buf);
6715 buf = NULL;
6716 }
6717 }
6718
6719 return buf;
6720 }
6721
6722
6723 \f
6724 /***********************************************************************
6725 XBM images
6726 ***********************************************************************/
6727
6728 static int xbm_scan P_ ((char **, char *, char *, int *));
6729 static int xbm_load P_ ((struct frame *f, struct image *img));
6730 static int xbm_load_image P_ ((struct frame *f, struct image *img,
6731 char *, char *));
6732 static int xbm_image_p P_ ((Lisp_Object object));
6733 static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
6734 unsigned char **));
6735 static int xbm_file_p P_ ((Lisp_Object));
6736
6737
6738 /* Indices of image specification fields in xbm_format, below. */
6739
6740 enum xbm_keyword_index
6741 {
6742 XBM_TYPE,
6743 XBM_FILE,
6744 XBM_WIDTH,
6745 XBM_HEIGHT,
6746 XBM_DATA,
6747 XBM_FOREGROUND,
6748 XBM_BACKGROUND,
6749 XBM_ASCENT,
6750 XBM_MARGIN,
6751 XBM_RELIEF,
6752 XBM_ALGORITHM,
6753 XBM_HEURISTIC_MASK,
6754 XBM_MASK,
6755 XBM_LAST
6756 };
6757
6758 /* Vector of image_keyword structures describing the format
6759 of valid XBM image specifications. */
6760
6761 static struct image_keyword xbm_format[XBM_LAST] =
6762 {
6763 {":type", IMAGE_SYMBOL_VALUE, 1},
6764 {":file", IMAGE_STRING_VALUE, 0},
6765 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6766 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6767 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6768 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
6769 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
6770 {":ascent", IMAGE_ASCENT_VALUE, 0},
6771 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
6772 {":relief", IMAGE_INTEGER_VALUE, 0},
6773 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6774 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6775 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
6776 };
6777
6778 /* Structure describing the image type XBM. */
6779
6780 static struct image_type xbm_type =
6781 {
6782 &Qxbm,
6783 xbm_image_p,
6784 xbm_load,
6785 x_clear_image,
6786 NULL
6787 };
6788
6789 /* Tokens returned from xbm_scan. */
6790
6791 enum xbm_token
6792 {
6793 XBM_TK_IDENT = 256,
6794 XBM_TK_NUMBER
6795 };
6796
6797
6798 /* Return non-zero if OBJECT is a valid XBM-type image specification.
6799 A valid specification is a list starting with the symbol `image'
6800 The rest of the list is a property list which must contain an
6801 entry `:type xbm..
6802
6803 If the specification specifies a file to load, it must contain
6804 an entry `:file FILENAME' where FILENAME is a string.
6805
6806 If the specification is for a bitmap loaded from memory it must
6807 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
6808 WIDTH and HEIGHT are integers > 0. DATA may be:
6809
6810 1. a string large enough to hold the bitmap data, i.e. it must
6811 have a size >= (WIDTH + 7) / 8 * HEIGHT
6812
6813 2. a bool-vector of size >= WIDTH * HEIGHT
6814
6815 3. a vector of strings or bool-vectors, one for each line of the
6816 bitmap.
6817
6818 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
6819 may not be specified in this case because they are defined in the
6820 XBM file.
6821
6822 Both the file and data forms may contain the additional entries
6823 `:background COLOR' and `:foreground COLOR'. If not present,
6824 foreground and background of the frame on which the image is
6825 displayed is used. */
6826
6827 static int
6828 xbm_image_p (object)
6829 Lisp_Object object;
6830 {
6831 struct image_keyword kw[XBM_LAST];
6832
6833 bcopy (xbm_format, kw, sizeof kw);
6834 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
6835 return 0;
6836
6837 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
6838
6839 if (kw[XBM_FILE].count)
6840 {
6841 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
6842 return 0;
6843 }
6844 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
6845 {
6846 /* In-memory XBM file. */
6847 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
6848 return 0;
6849 }
6850 else
6851 {
6852 Lisp_Object data;
6853 int width, height;
6854
6855 /* Entries for `:width', `:height' and `:data' must be present. */
6856 if (!kw[XBM_WIDTH].count
6857 || !kw[XBM_HEIGHT].count
6858 || !kw[XBM_DATA].count)
6859 return 0;
6860
6861 data = kw[XBM_DATA].value;
6862 width = XFASTINT (kw[XBM_WIDTH].value);
6863 height = XFASTINT (kw[XBM_HEIGHT].value);
6864
6865 /* Check type of data, and width and height against contents of
6866 data. */
6867 if (VECTORP (data))
6868 {
6869 int i;
6870
6871 /* Number of elements of the vector must be >= height. */
6872 if (XVECTOR (data)->size < height)
6873 return 0;
6874
6875 /* Each string or bool-vector in data must be large enough
6876 for one line of the image. */
6877 for (i = 0; i < height; ++i)
6878 {
6879 Lisp_Object elt = XVECTOR (data)->contents[i];
6880
6881 if (STRINGP (elt))
6882 {
6883 if (SCHARS (elt)
6884 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
6885 return 0;
6886 }
6887 else if (BOOL_VECTOR_P (elt))
6888 {
6889 if (XBOOL_VECTOR (elt)->size < width)
6890 return 0;
6891 }
6892 else
6893 return 0;
6894 }
6895 }
6896 else if (STRINGP (data))
6897 {
6898 if (SCHARS (data)
6899 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
6900 return 0;
6901 }
6902 else if (BOOL_VECTOR_P (data))
6903 {
6904 if (XBOOL_VECTOR (data)->size < width * height)
6905 return 0;
6906 }
6907 else
6908 return 0;
6909 }
6910
6911 return 1;
6912 }
6913
6914
6915 /* Scan a bitmap file. FP is the stream to read from. Value is
6916 either an enumerator from enum xbm_token, or a character for a
6917 single-character token, or 0 at end of file. If scanning an
6918 identifier, store the lexeme of the identifier in SVAL. If
6919 scanning a number, store its value in *IVAL. */
6920
6921 static int
6922 xbm_scan (s, end, sval, ival)
6923 char **s, *end;
6924 char *sval;
6925 int *ival;
6926 {
6927 int c;
6928
6929 loop:
6930
6931 /* Skip white space. */
6932 while (*s < end && (c = *(*s)++, isspace (c)))
6933 ;
6934
6935 if (*s >= end)
6936 c = 0;
6937 else if (isdigit (c))
6938 {
6939 int value = 0, digit;
6940
6941 if (c == '0' && *s < end)
6942 {
6943 c = *(*s)++;
6944 if (c == 'x' || c == 'X')
6945 {
6946 while (*s < end)
6947 {
6948 c = *(*s)++;
6949 if (isdigit (c))
6950 digit = c - '0';
6951 else if (c >= 'a' && c <= 'f')
6952 digit = c - 'a' + 10;
6953 else if (c >= 'A' && c <= 'F')
6954 digit = c - 'A' + 10;
6955 else
6956 break;
6957 value = 16 * value + digit;
6958 }
6959 }
6960 else if (isdigit (c))
6961 {
6962 value = c - '0';
6963 while (*s < end
6964 && (c = *(*s)++, isdigit (c)))
6965 value = 8 * value + c - '0';
6966 }
6967 }
6968 else
6969 {
6970 value = c - '0';
6971 while (*s < end
6972 && (c = *(*s)++, isdigit (c)))
6973 value = 10 * value + c - '0';
6974 }
6975
6976 if (*s < end)
6977 *s = *s - 1;
6978 *ival = value;
6979 c = XBM_TK_NUMBER;
6980 }
6981 else if (isalpha (c) || c == '_')
6982 {
6983 *sval++ = c;
6984 while (*s < end
6985 && (c = *(*s)++, (isalnum (c) || c == '_')))
6986 *sval++ = c;
6987 *sval = 0;
6988 if (*s < end)
6989 *s = *s - 1;
6990 c = XBM_TK_IDENT;
6991 }
6992 else if (c == '/' && **s == '*')
6993 {
6994 /* C-style comment. */
6995 ++*s;
6996 while (**s && (**s != '*' || *(*s + 1) != '/'))
6997 ++*s;
6998 if (**s)
6999 {
7000 *s += 2;
7001 goto loop;
7002 }
7003 }
7004
7005 return c;
7006 }
7007
7008
7009 /* Replacement for XReadBitmapFileData which isn't available under old
7010 X versions. CONTENTS is a pointer to a buffer to parse; END is the
7011 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
7012 the image. Return in *DATA the bitmap data allocated with xmalloc.
7013 Value is non-zero if successful. DATA null means just test if
7014 CONTENTS looks like an in-memory XBM file. */
7015
7016 static int
7017 xbm_read_bitmap_data (contents, end, width, height, data)
7018 char *contents, *end;
7019 int *width, *height;
7020 unsigned char **data;
7021 {
7022 char *s = contents;
7023 char buffer[BUFSIZ];
7024 int padding_p = 0;
7025 int v10 = 0;
7026 int bytes_per_line, i, nbytes;
7027 unsigned char *p;
7028 int value;
7029 int LA1;
7030
7031 #define match() \
7032 LA1 = xbm_scan (&s, end, buffer, &value)
7033
7034 #define expect(TOKEN) \
7035 if (LA1 != (TOKEN)) \
7036 goto failure; \
7037 else \
7038 match ()
7039
7040 #define expect_ident(IDENT) \
7041 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
7042 match (); \
7043 else \
7044 goto failure
7045
7046 *width = *height = -1;
7047 if (data)
7048 *data = NULL;
7049 LA1 = xbm_scan (&s, end, buffer, &value);
7050
7051 /* Parse defines for width, height and hot-spots. */
7052 while (LA1 == '#')
7053 {
7054 match ();
7055 expect_ident ("define");
7056 expect (XBM_TK_IDENT);
7057
7058 if (LA1 == XBM_TK_NUMBER);
7059 {
7060 char *p = strrchr (buffer, '_');
7061 p = p ? p + 1 : buffer;
7062 if (strcmp (p, "width") == 0)
7063 *width = value;
7064 else if (strcmp (p, "height") == 0)
7065 *height = value;
7066 }
7067 expect (XBM_TK_NUMBER);
7068 }
7069
7070 if (*width < 0 || *height < 0)
7071 goto failure;
7072 else if (data == NULL)
7073 goto success;
7074
7075 /* Parse bits. Must start with `static'. */
7076 expect_ident ("static");
7077 if (LA1 == XBM_TK_IDENT)
7078 {
7079 if (strcmp (buffer, "unsigned") == 0)
7080 {
7081 match ();
7082 expect_ident ("char");
7083 }
7084 else if (strcmp (buffer, "short") == 0)
7085 {
7086 match ();
7087 v10 = 1;
7088 if (*width % 16 && *width % 16 < 9)
7089 padding_p = 1;
7090 }
7091 else if (strcmp (buffer, "char") == 0)
7092 match ();
7093 else
7094 goto failure;
7095 }
7096 else
7097 goto failure;
7098
7099 expect (XBM_TK_IDENT);
7100 expect ('[');
7101 expect (']');
7102 expect ('=');
7103 expect ('{');
7104
7105 bytes_per_line = (*width + 7) / 8 + padding_p;
7106 nbytes = bytes_per_line * *height;
7107 p = *data = (char *) xmalloc (nbytes);
7108
7109 if (v10)
7110 {
7111 for (i = 0; i < nbytes; i += 2)
7112 {
7113 int val = value;
7114 expect (XBM_TK_NUMBER);
7115
7116 *p++ = val;
7117 if (!padding_p || ((i + 2) % bytes_per_line))
7118 *p++ = value >> 8;
7119
7120 if (LA1 == ',' || LA1 == '}')
7121 match ();
7122 else
7123 goto failure;
7124 }
7125 }
7126 else
7127 {
7128 for (i = 0; i < nbytes; ++i)
7129 {
7130 int val = value;
7131 expect (XBM_TK_NUMBER);
7132
7133 *p++ = val;
7134
7135 if (LA1 == ',' || LA1 == '}')
7136 match ();
7137 else
7138 goto failure;
7139 }
7140 }
7141
7142 success:
7143 return 1;
7144
7145 failure:
7146
7147 if (data && *data)
7148 {
7149 xfree (*data);
7150 *data = NULL;
7151 }
7152 return 0;
7153
7154 #undef match
7155 #undef expect
7156 #undef expect_ident
7157 }
7158
7159
7160 /* Load XBM image IMG which will be displayed on frame F from buffer
7161 CONTENTS. END is the end of the buffer. Value is non-zero if
7162 successful. */
7163
7164 static int
7165 xbm_load_image (f, img, contents, end)
7166 struct frame *f;
7167 struct image *img;
7168 char *contents, *end;
7169 {
7170 int rc;
7171 unsigned char *data;
7172 int success_p = 0;
7173
7174 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
7175 if (rc)
7176 {
7177 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
7178 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
7179 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
7180 Lisp_Object value;
7181
7182 xassert (img->width > 0 && img->height > 0);
7183
7184 /* Get foreground and background colors, maybe allocate colors. */
7185 value = image_spec_value (img->spec, QCforeground, NULL);
7186 if (!NILP (value))
7187 foreground = x_alloc_image_color (f, img, value, foreground);
7188 value = image_spec_value (img->spec, QCbackground, NULL);
7189 if (!NILP (value))
7190 {
7191 background = x_alloc_image_color (f, img, value, background);
7192 img->background = background;
7193 img->background_valid = 1;
7194 }
7195
7196 img->pixmap
7197 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
7198 FRAME_X_WINDOW (f),
7199 data,
7200 img->width, img->height,
7201 foreground, background,
7202 depth);
7203 xfree (data);
7204
7205 if (img->pixmap == None)
7206 {
7207 x_clear_image (f, img);
7208 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
7209 }
7210 else
7211 success_p = 1;
7212 }
7213 else
7214 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
7215
7216 return success_p;
7217 }
7218
7219
7220 /* Value is non-zero if DATA looks like an in-memory XBM file. */
7221
7222 static int
7223 xbm_file_p (data)
7224 Lisp_Object data;
7225 {
7226 int w, h;
7227 return (STRINGP (data)
7228 && xbm_read_bitmap_data (SDATA (data),
7229 (SDATA (data)
7230 + SBYTES (data)),
7231 &w, &h, NULL));
7232 }
7233
7234
7235 /* Fill image IMG which is used on frame F with pixmap data. Value is
7236 non-zero if successful. */
7237
7238 static int
7239 xbm_load (f, img)
7240 struct frame *f;
7241 struct image *img;
7242 {
7243 int success_p = 0;
7244 Lisp_Object file_name;
7245
7246 xassert (xbm_image_p (img->spec));
7247
7248 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7249 file_name = image_spec_value (img->spec, QCfile, NULL);
7250 if (STRINGP (file_name))
7251 {
7252 Lisp_Object file;
7253 char *contents;
7254 int size;
7255 struct gcpro gcpro1;
7256
7257 file = x_find_image_file (file_name);
7258 GCPRO1 (file);
7259 if (!STRINGP (file))
7260 {
7261 image_error ("Cannot find image file `%s'", file_name, Qnil);
7262 UNGCPRO;
7263 return 0;
7264 }
7265
7266 contents = slurp_file (SDATA (file), &size);
7267 if (contents == NULL)
7268 {
7269 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
7270 UNGCPRO;
7271 return 0;
7272 }
7273
7274 success_p = xbm_load_image (f, img, contents, contents + size);
7275 UNGCPRO;
7276 }
7277 else
7278 {
7279 struct image_keyword fmt[XBM_LAST];
7280 Lisp_Object data;
7281 int depth;
7282 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
7283 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
7284 char *bits;
7285 int parsed_p;
7286 int in_memory_file_p = 0;
7287
7288 /* See if data looks like an in-memory XBM file. */
7289 data = image_spec_value (img->spec, QCdata, NULL);
7290 in_memory_file_p = xbm_file_p (data);
7291
7292 /* Parse the image specification. */
7293 bcopy (xbm_format, fmt, sizeof fmt);
7294 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
7295 xassert (parsed_p);
7296
7297 /* Get specified width, and height. */
7298 if (!in_memory_file_p)
7299 {
7300 img->width = XFASTINT (fmt[XBM_WIDTH].value);
7301 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
7302 xassert (img->width > 0 && img->height > 0);
7303 }
7304
7305 /* Get foreground and background colors, maybe allocate colors. */
7306 if (fmt[XBM_FOREGROUND].count
7307 && STRINGP (fmt[XBM_FOREGROUND].value))
7308 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
7309 foreground);
7310 if (fmt[XBM_BACKGROUND].count
7311 && STRINGP (fmt[XBM_BACKGROUND].value))
7312 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
7313 background);
7314
7315 if (in_memory_file_p)
7316 success_p = xbm_load_image (f, img, SDATA (data),
7317 (SDATA (data)
7318 + SBYTES (data)));
7319 else
7320 {
7321 if (VECTORP (data))
7322 {
7323 int i;
7324 char *p;
7325 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
7326
7327 p = bits = (char *) alloca (nbytes * img->height);
7328 for (i = 0; i < img->height; ++i, p += nbytes)
7329 {
7330 Lisp_Object line = XVECTOR (data)->contents[i];
7331 if (STRINGP (line))
7332 bcopy (SDATA (line), p, nbytes);
7333 else
7334 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
7335 }
7336 }
7337 else if (STRINGP (data))
7338 bits = SDATA (data);
7339 else
7340 bits = XBOOL_VECTOR (data)->data;
7341
7342 /* Create the pixmap. */
7343 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
7344 img->pixmap
7345 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
7346 FRAME_X_WINDOW (f),
7347 bits,
7348 img->width, img->height,
7349 foreground, background,
7350 depth);
7351 if (img->pixmap)
7352 success_p = 1;
7353 else
7354 {
7355 image_error ("Unable to create pixmap for XBM image `%s'",
7356 img->spec, Qnil);
7357 x_clear_image (f, img);
7358 }
7359 }
7360 }
7361
7362 return success_p;
7363 }
7364
7365
7366 \f
7367 /***********************************************************************
7368 XPM images
7369 ***********************************************************************/
7370
7371 #if HAVE_XPM
7372
7373 static int xpm_image_p P_ ((Lisp_Object object));
7374 static int xpm_load P_ ((struct frame *f, struct image *img));
7375 static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
7376
7377 #include "X11/xpm.h"
7378
7379 /* The symbol `xpm' identifying XPM-format images. */
7380
7381 Lisp_Object Qxpm;
7382
7383 /* Indices of image specification fields in xpm_format, below. */
7384
7385 enum xpm_keyword_index
7386 {
7387 XPM_TYPE,
7388 XPM_FILE,
7389 XPM_DATA,
7390 XPM_ASCENT,
7391 XPM_MARGIN,
7392 XPM_RELIEF,
7393 XPM_ALGORITHM,
7394 XPM_HEURISTIC_MASK,
7395 XPM_MASK,
7396 XPM_COLOR_SYMBOLS,
7397 XPM_BACKGROUND,
7398 XPM_LAST
7399 };
7400
7401 /* Vector of image_keyword structures describing the format
7402 of valid XPM image specifications. */
7403
7404 static struct image_keyword xpm_format[XPM_LAST] =
7405 {
7406 {":type", IMAGE_SYMBOL_VALUE, 1},
7407 {":file", IMAGE_STRING_VALUE, 0},
7408 {":data", IMAGE_STRING_VALUE, 0},
7409 {":ascent", IMAGE_ASCENT_VALUE, 0},
7410 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
7411 {":relief", IMAGE_INTEGER_VALUE, 0},
7412 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7413 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7414 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7415 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7416 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
7417 };
7418
7419 /* Structure describing the image type XBM. */
7420
7421 static struct image_type xpm_type =
7422 {
7423 &Qxpm,
7424 xpm_image_p,
7425 xpm_load,
7426 x_clear_image,
7427 NULL
7428 };
7429
7430
7431 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
7432 functions for allocating image colors. Our own functions handle
7433 color allocation failures more gracefully than the ones on the XPM
7434 lib. */
7435
7436 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
7437 #define ALLOC_XPM_COLORS
7438 #endif
7439
7440 #ifdef ALLOC_XPM_COLORS
7441
7442 static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *));
7443 static void xpm_free_color_cache P_ ((void));
7444 static int xpm_lookup_color P_ ((struct frame *, char *, XColor *));
7445 static int xpm_color_bucket P_ ((char *));
7446 static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *,
7447 XColor *, int));
7448
7449 /* An entry in a hash table used to cache color definitions of named
7450 colors. This cache is necessary to speed up XPM image loading in
7451 case we do color allocations ourselves. Without it, we would need
7452 a call to XParseColor per pixel in the image. */
7453
7454 struct xpm_cached_color
7455 {
7456 /* Next in collision chain. */
7457 struct xpm_cached_color *next;
7458
7459 /* Color definition (RGB and pixel color). */
7460 XColor color;
7461
7462 /* Color name. */
7463 char name[1];
7464 };
7465
7466 /* The hash table used for the color cache, and its bucket vector
7467 size. */
7468
7469 #define XPM_COLOR_CACHE_BUCKETS 1001
7470 struct xpm_cached_color **xpm_color_cache;
7471
7472 /* Initialize the color cache. */
7473
7474 static void
7475 xpm_init_color_cache (f, attrs)
7476 struct frame *f;
7477 XpmAttributes *attrs;
7478 {
7479 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
7480 xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes);
7481 memset (xpm_color_cache, 0, nbytes);
7482 init_color_table ();
7483
7484 if (attrs->valuemask & XpmColorSymbols)
7485 {
7486 int i;
7487 XColor color;
7488
7489 for (i = 0; i < attrs->numsymbols; ++i)
7490 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7491 attrs->colorsymbols[i].value, &color))
7492 {
7493 color.pixel = lookup_rgb_color (f, color.red, color.green,
7494 color.blue);
7495 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
7496 }
7497 }
7498 }
7499
7500
7501 /* Free the color cache. */
7502
7503 static void
7504 xpm_free_color_cache ()
7505 {
7506 struct xpm_cached_color *p, *next;
7507 int i;
7508
7509 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
7510 for (p = xpm_color_cache[i]; p; p = next)
7511 {
7512 next = p->next;
7513 xfree (p);
7514 }
7515
7516 xfree (xpm_color_cache);
7517 xpm_color_cache = NULL;
7518 free_color_table ();
7519 }
7520
7521
7522 /* Return the bucket index for color named COLOR_NAME in the color
7523 cache. */
7524
7525 static int
7526 xpm_color_bucket (color_name)
7527 char *color_name;
7528 {
7529 unsigned h = 0;
7530 char *s;
7531
7532 for (s = color_name; *s; ++s)
7533 h = (h << 2) ^ *s;
7534 return h %= XPM_COLOR_CACHE_BUCKETS;
7535 }
7536
7537
7538 /* On frame F, cache values COLOR for color with name COLOR_NAME.
7539 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
7540 entry added. */
7541
7542 static struct xpm_cached_color *
7543 xpm_cache_color (f, color_name, color, bucket)
7544 struct frame *f;
7545 char *color_name;
7546 XColor *color;
7547 int bucket;
7548 {
7549 size_t nbytes;
7550 struct xpm_cached_color *p;
7551
7552 if (bucket < 0)
7553 bucket = xpm_color_bucket (color_name);
7554
7555 nbytes = sizeof *p + strlen (color_name);
7556 p = (struct xpm_cached_color *) xmalloc (nbytes);
7557 strcpy (p->name, color_name);
7558 p->color = *color;
7559 p->next = xpm_color_cache[bucket];
7560 xpm_color_cache[bucket] = p;
7561 return p;
7562 }
7563
7564
7565 /* Look up color COLOR_NAME for frame F in the color cache. If found,
7566 return the cached definition in *COLOR. Otherwise, make a new
7567 entry in the cache and allocate the color. Value is zero if color
7568 allocation failed. */
7569
7570 static int
7571 xpm_lookup_color (f, color_name, color)
7572 struct frame *f;
7573 char *color_name;
7574 XColor *color;
7575 {
7576 struct xpm_cached_color *p;
7577 int h = xpm_color_bucket (color_name);
7578
7579 for (p = xpm_color_cache[h]; p; p = p->next)
7580 if (strcmp (p->name, color_name) == 0)
7581 break;
7582
7583 if (p != NULL)
7584 *color = p->color;
7585 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7586 color_name, color))
7587 {
7588 color->pixel = lookup_rgb_color (f, color->red, color->green,
7589 color->blue);
7590 p = xpm_cache_color (f, color_name, color, h);
7591 }
7592
7593 return p != NULL;
7594 }
7595
7596
7597 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
7598 CLOSURE is a pointer to the frame on which we allocate the
7599 color. Return in *COLOR the allocated color. Value is non-zero
7600 if successful. */
7601
7602 static int
7603 xpm_alloc_color (dpy, cmap, color_name, color, closure)
7604 Display *dpy;
7605 Colormap cmap;
7606 char *color_name;
7607 XColor *color;
7608 void *closure;
7609 {
7610 return xpm_lookup_color ((struct frame *) closure, color_name, color);
7611 }
7612
7613
7614 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
7615 is a pointer to the frame on which we allocate the color. Value is
7616 non-zero if successful. */
7617
7618 static int
7619 xpm_free_colors (dpy, cmap, pixels, npixels, closure)
7620 Display *dpy;
7621 Colormap cmap;
7622 Pixel *pixels;
7623 int npixels;
7624 void *closure;
7625 {
7626 return 1;
7627 }
7628
7629 #endif /* ALLOC_XPM_COLORS */
7630
7631
7632 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
7633 for XPM images. Such a list must consist of conses whose car and
7634 cdr are strings. */
7635
7636 static int
7637 xpm_valid_color_symbols_p (color_symbols)
7638 Lisp_Object color_symbols;
7639 {
7640 while (CONSP (color_symbols))
7641 {
7642 Lisp_Object sym = XCAR (color_symbols);
7643 if (!CONSP (sym)
7644 || !STRINGP (XCAR (sym))
7645 || !STRINGP (XCDR (sym)))
7646 break;
7647 color_symbols = XCDR (color_symbols);
7648 }
7649
7650 return NILP (color_symbols);
7651 }
7652
7653
7654 /* Value is non-zero if OBJECT is a valid XPM image specification. */
7655
7656 static int
7657 xpm_image_p (object)
7658 Lisp_Object object;
7659 {
7660 struct image_keyword fmt[XPM_LAST];
7661 bcopy (xpm_format, fmt, sizeof fmt);
7662 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
7663 /* Either `:file' or `:data' must be present. */
7664 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
7665 /* Either no `:color-symbols' or it's a list of conses
7666 whose car and cdr are strings. */
7667 && (fmt[XPM_COLOR_SYMBOLS].count == 0
7668 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
7669 }
7670
7671
7672 /* Load image IMG which will be displayed on frame F. Value is
7673 non-zero if successful. */
7674
7675 static int
7676 xpm_load (f, img)
7677 struct frame *f;
7678 struct image *img;
7679 {
7680 int rc;
7681 XpmAttributes attrs;
7682 Lisp_Object specified_file, color_symbols;
7683
7684 /* Configure the XPM lib. Use the visual of frame F. Allocate
7685 close colors. Return colors allocated. */
7686 bzero (&attrs, sizeof attrs);
7687 attrs.visual = FRAME_X_VISUAL (f);
7688 attrs.colormap = FRAME_X_COLORMAP (f);
7689 attrs.valuemask |= XpmVisual;
7690 attrs.valuemask |= XpmColormap;
7691
7692 #ifdef ALLOC_XPM_COLORS
7693 /* Allocate colors with our own functions which handle
7694 failing color allocation more gracefully. */
7695 attrs.color_closure = f;
7696 attrs.alloc_color = xpm_alloc_color;
7697 attrs.free_colors = xpm_free_colors;
7698 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
7699 #else /* not ALLOC_XPM_COLORS */
7700 /* Let the XPM lib allocate colors. */
7701 attrs.valuemask |= XpmReturnAllocPixels;
7702 #ifdef XpmAllocCloseColors
7703 attrs.alloc_close_colors = 1;
7704 attrs.valuemask |= XpmAllocCloseColors;
7705 #else /* not XpmAllocCloseColors */
7706 attrs.closeness = 600;
7707 attrs.valuemask |= XpmCloseness;
7708 #endif /* not XpmAllocCloseColors */
7709 #endif /* ALLOC_XPM_COLORS */
7710
7711 /* If image specification contains symbolic color definitions, add
7712 these to `attrs'. */
7713 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
7714 if (CONSP (color_symbols))
7715 {
7716 Lisp_Object tail;
7717 XpmColorSymbol *xpm_syms;
7718 int i, size;
7719
7720 attrs.valuemask |= XpmColorSymbols;
7721
7722 /* Count number of symbols. */
7723 attrs.numsymbols = 0;
7724 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
7725 ++attrs.numsymbols;
7726
7727 /* Allocate an XpmColorSymbol array. */
7728 size = attrs.numsymbols * sizeof *xpm_syms;
7729 xpm_syms = (XpmColorSymbol *) alloca (size);
7730 bzero (xpm_syms, size);
7731 attrs.colorsymbols = xpm_syms;
7732
7733 /* Fill the color symbol array. */
7734 for (tail = color_symbols, i = 0;
7735 CONSP (tail);
7736 ++i, tail = XCDR (tail))
7737 {
7738 Lisp_Object name = XCAR (XCAR (tail));
7739 Lisp_Object color = XCDR (XCAR (tail));
7740 xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1);
7741 strcpy (xpm_syms[i].name, SDATA (name));
7742 xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1);
7743 strcpy (xpm_syms[i].value, SDATA (color));
7744 }
7745 }
7746
7747 /* Create a pixmap for the image, either from a file, or from a
7748 string buffer containing data in the same format as an XPM file. */
7749 #ifdef ALLOC_XPM_COLORS
7750 xpm_init_color_cache (f, &attrs);
7751 #endif
7752
7753 specified_file = image_spec_value (img->spec, QCfile, NULL);
7754 if (STRINGP (specified_file))
7755 {
7756 Lisp_Object file = x_find_image_file (specified_file);
7757 if (!STRINGP (file))
7758 {
7759 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7760 return 0;
7761 }
7762
7763 rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7764 SDATA (file), &img->pixmap, &img->mask,
7765 &attrs);
7766 }
7767 else
7768 {
7769 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
7770 rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7771 SDATA (buffer),
7772 &img->pixmap, &img->mask,
7773 &attrs);
7774 }
7775
7776 if (rc == XpmSuccess)
7777 {
7778 #ifdef ALLOC_XPM_COLORS
7779 img->colors = colors_in_color_table (&img->ncolors);
7780 #else /* not ALLOC_XPM_COLORS */
7781 int i;
7782
7783 img->ncolors = attrs.nalloc_pixels;
7784 img->colors = (unsigned long *) xmalloc (img->ncolors
7785 * sizeof *img->colors);
7786 for (i = 0; i < attrs.nalloc_pixels; ++i)
7787 {
7788 img->colors[i] = attrs.alloc_pixels[i];
7789 #ifdef DEBUG_X_COLORS
7790 register_color (img->colors[i]);
7791 #endif
7792 }
7793 #endif /* not ALLOC_XPM_COLORS */
7794
7795 img->width = attrs.width;
7796 img->height = attrs.height;
7797 xassert (img->width > 0 && img->height > 0);
7798
7799 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
7800 XpmFreeAttributes (&attrs);
7801 }
7802 else
7803 {
7804 switch (rc)
7805 {
7806 case XpmOpenFailed:
7807 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
7808 break;
7809
7810 case XpmFileInvalid:
7811 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
7812 break;
7813
7814 case XpmNoMemory:
7815 image_error ("Out of memory (%s)", img->spec, Qnil);
7816 break;
7817
7818 case XpmColorFailed:
7819 image_error ("Color allocation error (%s)", img->spec, Qnil);
7820 break;
7821
7822 default:
7823 image_error ("Unknown error (%s)", img->spec, Qnil);
7824 break;
7825 }
7826 }
7827
7828 #ifdef ALLOC_XPM_COLORS
7829 xpm_free_color_cache ();
7830 #endif
7831 return rc == XpmSuccess;
7832 }
7833
7834 #endif /* HAVE_XPM != 0 */
7835
7836 \f
7837 /***********************************************************************
7838 Color table
7839 ***********************************************************************/
7840
7841 /* An entry in the color table mapping an RGB color to a pixel color. */
7842
7843 struct ct_color
7844 {
7845 int r, g, b;
7846 unsigned long pixel;
7847
7848 /* Next in color table collision list. */
7849 struct ct_color *next;
7850 };
7851
7852 /* The bucket vector size to use. Must be prime. */
7853
7854 #define CT_SIZE 101
7855
7856 /* Value is a hash of the RGB color given by R, G, and B. */
7857
7858 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
7859
7860 /* The color hash table. */
7861
7862 struct ct_color **ct_table;
7863
7864 /* Number of entries in the color table. */
7865
7866 int ct_colors_allocated;
7867
7868 /* Initialize the color table. */
7869
7870 static void
7871 init_color_table ()
7872 {
7873 int size = CT_SIZE * sizeof (*ct_table);
7874 ct_table = (struct ct_color **) xmalloc (size);
7875 bzero (ct_table, size);
7876 ct_colors_allocated = 0;
7877 }
7878
7879
7880 /* Free memory associated with the color table. */
7881
7882 static void
7883 free_color_table ()
7884 {
7885 int i;
7886 struct ct_color *p, *next;
7887
7888 for (i = 0; i < CT_SIZE; ++i)
7889 for (p = ct_table[i]; p; p = next)
7890 {
7891 next = p->next;
7892 xfree (p);
7893 }
7894
7895 xfree (ct_table);
7896 ct_table = NULL;
7897 }
7898
7899
7900 /* Value is a pixel color for RGB color R, G, B on frame F. If an
7901 entry for that color already is in the color table, return the
7902 pixel color of that entry. Otherwise, allocate a new color for R,
7903 G, B, and make an entry in the color table. */
7904
7905 static unsigned long
7906 lookup_rgb_color (f, r, g, b)
7907 struct frame *f;
7908 int r, g, b;
7909 {
7910 unsigned hash = CT_HASH_RGB (r, g, b);
7911 int i = hash % CT_SIZE;
7912 struct ct_color *p;
7913
7914 for (p = ct_table[i]; p; p = p->next)
7915 if (p->r == r && p->g == g && p->b == b)
7916 break;
7917
7918 if (p == NULL)
7919 {
7920 XColor color;
7921 Colormap cmap;
7922 int rc;
7923
7924 color.red = r;
7925 color.green = g;
7926 color.blue = b;
7927
7928 cmap = FRAME_X_COLORMAP (f);
7929 rc = x_alloc_nearest_color (f, cmap, &color);
7930
7931 if (rc)
7932 {
7933 ++ct_colors_allocated;
7934
7935 p = (struct ct_color *) xmalloc (sizeof *p);
7936 p->r = r;
7937 p->g = g;
7938 p->b = b;
7939 p->pixel = color.pixel;
7940 p->next = ct_table[i];
7941 ct_table[i] = p;
7942 }
7943 else
7944 return FRAME_FOREGROUND_PIXEL (f);
7945 }
7946
7947 return p->pixel;
7948 }
7949
7950
7951 /* Look up pixel color PIXEL which is used on frame F in the color
7952 table. If not already present, allocate it. Value is PIXEL. */
7953
7954 static unsigned long
7955 lookup_pixel_color (f, pixel)
7956 struct frame *f;
7957 unsigned long pixel;
7958 {
7959 int i = pixel % CT_SIZE;
7960 struct ct_color *p;
7961
7962 for (p = ct_table[i]; p; p = p->next)
7963 if (p->pixel == pixel)
7964 break;
7965
7966 if (p == NULL)
7967 {
7968 XColor color;
7969 Colormap cmap;
7970 int rc;
7971
7972 cmap = FRAME_X_COLORMAP (f);
7973 color.pixel = pixel;
7974 x_query_color (f, &color);
7975 rc = x_alloc_nearest_color (f, cmap, &color);
7976
7977 if (rc)
7978 {
7979 ++ct_colors_allocated;
7980
7981 p = (struct ct_color *) xmalloc (sizeof *p);
7982 p->r = color.red;
7983 p->g = color.green;
7984 p->b = color.blue;
7985 p->pixel = pixel;
7986 p->next = ct_table[i];
7987 ct_table[i] = p;
7988 }
7989 else
7990 return FRAME_FOREGROUND_PIXEL (f);
7991 }
7992
7993 return p->pixel;
7994 }
7995
7996
7997 /* Value is a vector of all pixel colors contained in the color table,
7998 allocated via xmalloc. Set *N to the number of colors. */
7999
8000 static unsigned long *
8001 colors_in_color_table (n)
8002 int *n;
8003 {
8004 int i, j;
8005 struct ct_color *p;
8006 unsigned long *colors;
8007
8008 if (ct_colors_allocated == 0)
8009 {
8010 *n = 0;
8011 colors = NULL;
8012 }
8013 else
8014 {
8015 colors = (unsigned long *) xmalloc (ct_colors_allocated
8016 * sizeof *colors);
8017 *n = ct_colors_allocated;
8018
8019 for (i = j = 0; i < CT_SIZE; ++i)
8020 for (p = ct_table[i]; p; p = p->next)
8021 colors[j++] = p->pixel;
8022 }
8023
8024 return colors;
8025 }
8026
8027
8028 \f
8029 /***********************************************************************
8030 Algorithms
8031 ***********************************************************************/
8032
8033 static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
8034 static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
8035 static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
8036
8037 /* Non-zero means draw a cross on images having `:conversion
8038 disabled'. */
8039
8040 int cross_disabled_images;
8041
8042 /* Edge detection matrices for different edge-detection
8043 strategies. */
8044
8045 static int emboss_matrix[9] = {
8046 /* x - 1 x x + 1 */
8047 2, -1, 0, /* y - 1 */
8048 -1, 0, 1, /* y */
8049 0, 1, -2 /* y + 1 */
8050 };
8051
8052 static int laplace_matrix[9] = {
8053 /* x - 1 x x + 1 */
8054 1, 0, 0, /* y - 1 */
8055 0, 0, 0, /* y */
8056 0, 0, -1 /* y + 1 */
8057 };
8058
8059 /* Value is the intensity of the color whose red/green/blue values
8060 are R, G, and B. */
8061
8062 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
8063
8064
8065 /* On frame F, return an array of XColor structures describing image
8066 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
8067 non-zero means also fill the red/green/blue members of the XColor
8068 structures. Value is a pointer to the array of XColors structures,
8069 allocated with xmalloc; it must be freed by the caller. */
8070
8071 static XColor *
8072 x_to_xcolors (f, img, rgb_p)
8073 struct frame *f;
8074 struct image *img;
8075 int rgb_p;
8076 {
8077 int x, y;
8078 XColor *colors, *p;
8079 XImage *ximg;
8080
8081 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
8082
8083 /* Get the X image IMG->pixmap. */
8084 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
8085 0, 0, img->width, img->height, ~0, ZPixmap);
8086
8087 /* Fill the `pixel' members of the XColor array. I wished there
8088 were an easy and portable way to circumvent XGetPixel. */
8089 p = colors;
8090 for (y = 0; y < img->height; ++y)
8091 {
8092 XColor *row = p;
8093
8094 for (x = 0; x < img->width; ++x, ++p)
8095 p->pixel = XGetPixel (ximg, x, y);
8096
8097 if (rgb_p)
8098 x_query_colors (f, row, img->width);
8099 }
8100
8101 XDestroyImage (ximg);
8102 return colors;
8103 }
8104
8105
8106 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
8107 RGB members are set. F is the frame on which this all happens.
8108 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
8109
8110 static void
8111 x_from_xcolors (f, img, colors)
8112 struct frame *f;
8113 struct image *img;
8114 XColor *colors;
8115 {
8116 int x, y;
8117 XImage *oimg;
8118 Pixmap pixmap;
8119 XColor *p;
8120
8121 init_color_table ();
8122
8123 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
8124 &oimg, &pixmap);
8125 p = colors;
8126 for (y = 0; y < img->height; ++y)
8127 for (x = 0; x < img->width; ++x, ++p)
8128 {
8129 unsigned long pixel;
8130 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
8131 XPutPixel (oimg, x, y, pixel);
8132 }
8133
8134 xfree (colors);
8135 x_clear_image_1 (f, img, 1, 0, 1);
8136
8137 x_put_x_image (f, oimg, pixmap, img->width, img->height);
8138 x_destroy_x_image (oimg);
8139 img->pixmap = pixmap;
8140 img->colors = colors_in_color_table (&img->ncolors);
8141 free_color_table ();
8142 }
8143
8144
8145 /* On frame F, perform edge-detection on image IMG.
8146
8147 MATRIX is a nine-element array specifying the transformation
8148 matrix. See emboss_matrix for an example.
8149
8150 COLOR_ADJUST is a color adjustment added to each pixel of the
8151 outgoing image. */
8152
8153 static void
8154 x_detect_edges (f, img, matrix, color_adjust)
8155 struct frame *f;
8156 struct image *img;
8157 int matrix[9], color_adjust;
8158 {
8159 XColor *colors = x_to_xcolors (f, img, 1);
8160 XColor *new, *p;
8161 int x, y, i, sum;
8162
8163 for (i = sum = 0; i < 9; ++i)
8164 sum += abs (matrix[i]);
8165
8166 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
8167
8168 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
8169
8170 for (y = 0; y < img->height; ++y)
8171 {
8172 p = COLOR (new, 0, y);
8173 p->red = p->green = p->blue = 0xffff/2;
8174 p = COLOR (new, img->width - 1, y);
8175 p->red = p->green = p->blue = 0xffff/2;
8176 }
8177
8178 for (x = 1; x < img->width - 1; ++x)
8179 {
8180 p = COLOR (new, x, 0);
8181 p->red = p->green = p->blue = 0xffff/2;
8182 p = COLOR (new, x, img->height - 1);
8183 p->red = p->green = p->blue = 0xffff/2;
8184 }
8185
8186 for (y = 1; y < img->height - 1; ++y)
8187 {
8188 p = COLOR (new, 1, y);
8189
8190 for (x = 1; x < img->width - 1; ++x, ++p)
8191 {
8192 int r, g, b, y1, x1;
8193
8194 r = g = b = i = 0;
8195 for (y1 = y - 1; y1 < y + 2; ++y1)
8196 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
8197 if (matrix[i])
8198 {
8199 XColor *t = COLOR (colors, x1, y1);
8200 r += matrix[i] * t->red;
8201 g += matrix[i] * t->green;
8202 b += matrix[i] * t->blue;
8203 }
8204
8205 r = (r / sum + color_adjust) & 0xffff;
8206 g = (g / sum + color_adjust) & 0xffff;
8207 b = (b / sum + color_adjust) & 0xffff;
8208 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
8209 }
8210 }
8211
8212 xfree (colors);
8213 x_from_xcolors (f, img, new);
8214
8215 #undef COLOR
8216 }
8217
8218
8219 /* Perform the pre-defined `emboss' edge-detection on image IMG
8220 on frame F. */
8221
8222 static void
8223 x_emboss (f, img)
8224 struct frame *f;
8225 struct image *img;
8226 {
8227 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
8228 }
8229
8230
8231 /* Perform the pre-defined `laplace' edge-detection on image IMG
8232 on frame F. */
8233
8234 static void
8235 x_laplace (f, img)
8236 struct frame *f;
8237 struct image *img;
8238 {
8239 x_detect_edges (f, img, laplace_matrix, 45000);
8240 }
8241
8242
8243 /* Perform edge-detection on image IMG on frame F, with specified
8244 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
8245
8246 MATRIX must be either
8247
8248 - a list of at least 9 numbers in row-major form
8249 - a vector of at least 9 numbers
8250
8251 COLOR_ADJUST nil means use a default; otherwise it must be a
8252 number. */
8253
8254 static void
8255 x_edge_detection (f, img, matrix, color_adjust)
8256 struct frame *f;
8257 struct image *img;
8258 Lisp_Object matrix, color_adjust;
8259 {
8260 int i = 0;
8261 int trans[9];
8262
8263 if (CONSP (matrix))
8264 {
8265 for (i = 0;
8266 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
8267 ++i, matrix = XCDR (matrix))
8268 trans[i] = XFLOATINT (XCAR (matrix));
8269 }
8270 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
8271 {
8272 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
8273 trans[i] = XFLOATINT (AREF (matrix, i));
8274 }
8275
8276 if (NILP (color_adjust))
8277 color_adjust = make_number (0xffff / 2);
8278
8279 if (i == 9 && NUMBERP (color_adjust))
8280 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
8281 }
8282
8283
8284 /* Transform image IMG on frame F so that it looks disabled. */
8285
8286 static void
8287 x_disable_image (f, img)
8288 struct frame *f;
8289 struct image *img;
8290 {
8291 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8292
8293 if (dpyinfo->n_planes >= 2)
8294 {
8295 /* Color (or grayscale). Convert to gray, and equalize. Just
8296 drawing such images with a stipple can look very odd, so
8297 we're using this method instead. */
8298 XColor *colors = x_to_xcolors (f, img, 1);
8299 XColor *p, *end;
8300 const int h = 15000;
8301 const int l = 30000;
8302
8303 for (p = colors, end = colors + img->width * img->height;
8304 p < end;
8305 ++p)
8306 {
8307 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
8308 int i2 = (0xffff - h - l) * i / 0xffff + l;
8309 p->red = p->green = p->blue = i2;
8310 }
8311
8312 x_from_xcolors (f, img, colors);
8313 }
8314
8315 /* Draw a cross over the disabled image, if we must or if we
8316 should. */
8317 if (dpyinfo->n_planes < 2 || cross_disabled_images)
8318 {
8319 Display *dpy = FRAME_X_DISPLAY (f);
8320 GC gc;
8321
8322 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
8323 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
8324 XDrawLine (dpy, img->pixmap, gc, 0, 0,
8325 img->width - 1, img->height - 1);
8326 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
8327 img->width - 1, 0);
8328 XFreeGC (dpy, gc);
8329
8330 if (img->mask)
8331 {
8332 gc = XCreateGC (dpy, img->mask, 0, NULL);
8333 XSetForeground (dpy, gc, WHITE_PIX_DEFAULT (f));
8334 XDrawLine (dpy, img->mask, gc, 0, 0,
8335 img->width - 1, img->height - 1);
8336 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
8337 img->width - 1, 0);
8338 XFreeGC (dpy, gc);
8339 }
8340 }
8341 }
8342
8343
8344 /* Build a mask for image IMG which is used on frame F. FILE is the
8345 name of an image file, for error messages. HOW determines how to
8346 determine the background color of IMG. If it is a list '(R G B)',
8347 with R, G, and B being integers >= 0, take that as the color of the
8348 background. Otherwise, determine the background color of IMG
8349 heuristically. Value is non-zero if successful. */
8350
8351 static int
8352 x_build_heuristic_mask (f, img, how)
8353 struct frame *f;
8354 struct image *img;
8355 Lisp_Object how;
8356 {
8357 Display *dpy = FRAME_X_DISPLAY (f);
8358 XImage *ximg, *mask_img;
8359 int x, y, rc, use_img_background;
8360 unsigned long bg = 0;
8361
8362 if (img->mask)
8363 {
8364 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
8365 img->mask = None;
8366 img->background_transparent_valid = 0;
8367 }
8368
8369 /* Create an image and pixmap serving as mask. */
8370 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
8371 &mask_img, &img->mask);
8372 if (!rc)
8373 return 0;
8374
8375 /* Get the X image of IMG->pixmap. */
8376 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
8377 ~0, ZPixmap);
8378
8379 /* Determine the background color of ximg. If HOW is `(R G B)'
8380 take that as color. Otherwise, use the image's background color. */
8381 use_img_background = 1;
8382
8383 if (CONSP (how))
8384 {
8385 int rgb[3], i;
8386
8387 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
8388 {
8389 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
8390 how = XCDR (how);
8391 }
8392
8393 if (i == 3 && NILP (how))
8394 {
8395 char color_name[30];
8396 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
8397 bg = x_alloc_image_color (f, img, build_string (color_name), 0);
8398 use_img_background = 0;
8399 }
8400 }
8401
8402 if (use_img_background)
8403 bg = four_corners_best (ximg, img->width, img->height);
8404
8405 /* Set all bits in mask_img to 1 whose color in ximg is different
8406 from the background color bg. */
8407 for (y = 0; y < img->height; ++y)
8408 for (x = 0; x < img->width; ++x)
8409 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
8410
8411 /* Fill in the background_transparent field while we have the mask handy. */
8412 image_background_transparent (img, f, mask_img);
8413
8414 /* Put mask_img into img->mask. */
8415 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
8416 x_destroy_x_image (mask_img);
8417 XDestroyImage (ximg);
8418
8419 return 1;
8420 }
8421
8422
8423 \f
8424 /***********************************************************************
8425 PBM (mono, gray, color)
8426 ***********************************************************************/
8427
8428 static int pbm_image_p P_ ((Lisp_Object object));
8429 static int pbm_load P_ ((struct frame *f, struct image *img));
8430 static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
8431
8432 /* The symbol `pbm' identifying images of this type. */
8433
8434 Lisp_Object Qpbm;
8435
8436 /* Indices of image specification fields in gs_format, below. */
8437
8438 enum pbm_keyword_index
8439 {
8440 PBM_TYPE,
8441 PBM_FILE,
8442 PBM_DATA,
8443 PBM_ASCENT,
8444 PBM_MARGIN,
8445 PBM_RELIEF,
8446 PBM_ALGORITHM,
8447 PBM_HEURISTIC_MASK,
8448 PBM_MASK,
8449 PBM_FOREGROUND,
8450 PBM_BACKGROUND,
8451 PBM_LAST
8452 };
8453
8454 /* Vector of image_keyword structures describing the format
8455 of valid user-defined image specifications. */
8456
8457 static struct image_keyword pbm_format[PBM_LAST] =
8458 {
8459 {":type", IMAGE_SYMBOL_VALUE, 1},
8460 {":file", IMAGE_STRING_VALUE, 0},
8461 {":data", IMAGE_STRING_VALUE, 0},
8462 {":ascent", IMAGE_ASCENT_VALUE, 0},
8463 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
8464 {":relief", IMAGE_INTEGER_VALUE, 0},
8465 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8466 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8467 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8468 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
8469 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8470 };
8471
8472 /* Structure describing the image type `pbm'. */
8473
8474 static struct image_type pbm_type =
8475 {
8476 &Qpbm,
8477 pbm_image_p,
8478 pbm_load,
8479 x_clear_image,
8480 NULL
8481 };
8482
8483
8484 /* Return non-zero if OBJECT is a valid PBM image specification. */
8485
8486 static int
8487 pbm_image_p (object)
8488 Lisp_Object object;
8489 {
8490 struct image_keyword fmt[PBM_LAST];
8491
8492 bcopy (pbm_format, fmt, sizeof fmt);
8493
8494 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
8495 return 0;
8496
8497 /* Must specify either :data or :file. */
8498 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
8499 }
8500
8501
8502 /* Scan a decimal number from *S and return it. Advance *S while
8503 reading the number. END is the end of the string. Value is -1 at
8504 end of input. */
8505
8506 static int
8507 pbm_scan_number (s, end)
8508 unsigned char **s, *end;
8509 {
8510 int c = 0, val = -1;
8511
8512 while (*s < end)
8513 {
8514 /* Skip white-space. */
8515 while (*s < end && (c = *(*s)++, isspace (c)))
8516 ;
8517
8518 if (c == '#')
8519 {
8520 /* Skip comment to end of line. */
8521 while (*s < end && (c = *(*s)++, c != '\n'))
8522 ;
8523 }
8524 else if (isdigit (c))
8525 {
8526 /* Read decimal number. */
8527 val = c - '0';
8528 while (*s < end && (c = *(*s)++, isdigit (c)))
8529 val = 10 * val + c - '0';
8530 break;
8531 }
8532 else
8533 break;
8534 }
8535
8536 return val;
8537 }
8538
8539
8540 /* Load PBM image IMG for use on frame F. */
8541
8542 static int
8543 pbm_load (f, img)
8544 struct frame *f;
8545 struct image *img;
8546 {
8547 int raw_p, x, y;
8548 int width, height, max_color_idx = 0;
8549 XImage *ximg;
8550 Lisp_Object file, specified_file;
8551 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
8552 struct gcpro gcpro1;
8553 unsigned char *contents = NULL;
8554 unsigned char *end, *p;
8555 int size;
8556
8557 specified_file = image_spec_value (img->spec, QCfile, NULL);
8558 file = Qnil;
8559 GCPRO1 (file);
8560
8561 if (STRINGP (specified_file))
8562 {
8563 file = x_find_image_file (specified_file);
8564 if (!STRINGP (file))
8565 {
8566 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8567 UNGCPRO;
8568 return 0;
8569 }
8570
8571 contents = slurp_file (SDATA (file), &size);
8572 if (contents == NULL)
8573 {
8574 image_error ("Error reading `%s'", file, Qnil);
8575 UNGCPRO;
8576 return 0;
8577 }
8578
8579 p = contents;
8580 end = contents + size;
8581 }
8582 else
8583 {
8584 Lisp_Object data;
8585 data = image_spec_value (img->spec, QCdata, NULL);
8586 p = SDATA (data);
8587 end = p + SBYTES (data);
8588 }
8589
8590 /* Check magic number. */
8591 if (end - p < 2 || *p++ != 'P')
8592 {
8593 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
8594 error:
8595 xfree (contents);
8596 UNGCPRO;
8597 return 0;
8598 }
8599
8600 switch (*p++)
8601 {
8602 case '1':
8603 raw_p = 0, type = PBM_MONO;
8604 break;
8605
8606 case '2':
8607 raw_p = 0, type = PBM_GRAY;
8608 break;
8609
8610 case '3':
8611 raw_p = 0, type = PBM_COLOR;
8612 break;
8613
8614 case '4':
8615 raw_p = 1, type = PBM_MONO;
8616 break;
8617
8618 case '5':
8619 raw_p = 1, type = PBM_GRAY;
8620 break;
8621
8622 case '6':
8623 raw_p = 1, type = PBM_COLOR;
8624 break;
8625
8626 default:
8627 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
8628 goto error;
8629 }
8630
8631 /* Read width, height, maximum color-component. Characters
8632 starting with `#' up to the end of a line are ignored. */
8633 width = pbm_scan_number (&p, end);
8634 height = pbm_scan_number (&p, end);
8635
8636 if (type != PBM_MONO)
8637 {
8638 max_color_idx = pbm_scan_number (&p, end);
8639 if (raw_p && max_color_idx > 255)
8640 max_color_idx = 255;
8641 }
8642
8643 if (width < 0
8644 || height < 0
8645 || (type != PBM_MONO && max_color_idx < 0))
8646 goto error;
8647
8648 if (!x_create_x_image_and_pixmap (f, width, height, 0,
8649 &ximg, &img->pixmap))
8650 goto error;
8651
8652 /* Initialize the color hash table. */
8653 init_color_table ();
8654
8655 if (type == PBM_MONO)
8656 {
8657 int c = 0, g;
8658 struct image_keyword fmt[PBM_LAST];
8659 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
8660 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
8661
8662 /* Parse the image specification. */
8663 bcopy (pbm_format, fmt, sizeof fmt);
8664 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
8665
8666 /* Get foreground and background colors, maybe allocate colors. */
8667 if (fmt[PBM_FOREGROUND].count
8668 && STRINGP (fmt[PBM_FOREGROUND].value))
8669 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
8670 if (fmt[PBM_BACKGROUND].count
8671 && STRINGP (fmt[PBM_BACKGROUND].value))
8672 {
8673 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
8674 img->background = bg;
8675 img->background_valid = 1;
8676 }
8677
8678 for (y = 0; y < height; ++y)
8679 for (x = 0; x < width; ++x)
8680 {
8681 if (raw_p)
8682 {
8683 if ((x & 7) == 0)
8684 c = *p++;
8685 g = c & 0x80;
8686 c <<= 1;
8687 }
8688 else
8689 g = pbm_scan_number (&p, end);
8690
8691 XPutPixel (ximg, x, y, g ? fg : bg);
8692 }
8693 }
8694 else
8695 {
8696 for (y = 0; y < height; ++y)
8697 for (x = 0; x < width; ++x)
8698 {
8699 int r, g, b;
8700
8701 if (type == PBM_GRAY)
8702 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
8703 else if (raw_p)
8704 {
8705 r = *p++;
8706 g = *p++;
8707 b = *p++;
8708 }
8709 else
8710 {
8711 r = pbm_scan_number (&p, end);
8712 g = pbm_scan_number (&p, end);
8713 b = pbm_scan_number (&p, end);
8714 }
8715
8716 if (r < 0 || g < 0 || b < 0)
8717 {
8718 xfree (ximg->data);
8719 ximg->data = NULL;
8720 XDestroyImage (ximg);
8721 image_error ("Invalid pixel value in image `%s'",
8722 img->spec, Qnil);
8723 goto error;
8724 }
8725
8726 /* RGB values are now in the range 0..max_color_idx.
8727 Scale this to the range 0..0xffff supported by X. */
8728 r = (double) r * 65535 / max_color_idx;
8729 g = (double) g * 65535 / max_color_idx;
8730 b = (double) b * 65535 / max_color_idx;
8731 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
8732 }
8733 }
8734
8735 /* Store in IMG->colors the colors allocated for the image, and
8736 free the color table. */
8737 img->colors = colors_in_color_table (&img->ncolors);
8738 free_color_table ();
8739
8740 /* Maybe fill in the background field while we have ximg handy. */
8741 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8742 IMAGE_BACKGROUND (img, f, ximg);
8743
8744 /* Put the image into a pixmap. */
8745 x_put_x_image (f, ximg, img->pixmap, width, height);
8746 x_destroy_x_image (ximg);
8747
8748 img->width = width;
8749 img->height = height;
8750
8751 UNGCPRO;
8752 xfree (contents);
8753 return 1;
8754 }
8755
8756
8757 \f
8758 /***********************************************************************
8759 PNG
8760 ***********************************************************************/
8761
8762 #if HAVE_PNG
8763
8764 #include <png.h>
8765
8766 /* Function prototypes. */
8767
8768 static int png_image_p P_ ((Lisp_Object object));
8769 static int png_load P_ ((struct frame *f, struct image *img));
8770
8771 /* The symbol `png' identifying images of this type. */
8772
8773 Lisp_Object Qpng;
8774
8775 /* Indices of image specification fields in png_format, below. */
8776
8777 enum png_keyword_index
8778 {
8779 PNG_TYPE,
8780 PNG_DATA,
8781 PNG_FILE,
8782 PNG_ASCENT,
8783 PNG_MARGIN,
8784 PNG_RELIEF,
8785 PNG_ALGORITHM,
8786 PNG_HEURISTIC_MASK,
8787 PNG_MASK,
8788 PNG_BACKGROUND,
8789 PNG_LAST
8790 };
8791
8792 /* Vector of image_keyword structures describing the format
8793 of valid user-defined image specifications. */
8794
8795 static struct image_keyword png_format[PNG_LAST] =
8796 {
8797 {":type", IMAGE_SYMBOL_VALUE, 1},
8798 {":data", IMAGE_STRING_VALUE, 0},
8799 {":file", IMAGE_STRING_VALUE, 0},
8800 {":ascent", IMAGE_ASCENT_VALUE, 0},
8801 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
8802 {":relief", IMAGE_INTEGER_VALUE, 0},
8803 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8804 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8805 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8806 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
8807 };
8808
8809 /* Structure describing the image type `png'. */
8810
8811 static struct image_type png_type =
8812 {
8813 &Qpng,
8814 png_image_p,
8815 png_load,
8816 x_clear_image,
8817 NULL
8818 };
8819
8820
8821 /* Return non-zero if OBJECT is a valid PNG image specification. */
8822
8823 static int
8824 png_image_p (object)
8825 Lisp_Object object;
8826 {
8827 struct image_keyword fmt[PNG_LAST];
8828 bcopy (png_format, fmt, sizeof fmt);
8829
8830 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
8831 return 0;
8832
8833 /* Must specify either the :data or :file keyword. */
8834 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
8835 }
8836
8837
8838 /* Error and warning handlers installed when the PNG library
8839 is initialized. */
8840
8841 static void
8842 my_png_error (png_ptr, msg)
8843 png_struct *png_ptr;
8844 char *msg;
8845 {
8846 xassert (png_ptr != NULL);
8847 image_error ("PNG error: %s", build_string (msg), Qnil);
8848 longjmp (png_ptr->jmpbuf, 1);
8849 }
8850
8851
8852 static void
8853 my_png_warning (png_ptr, msg)
8854 png_struct *png_ptr;
8855 char *msg;
8856 {
8857 xassert (png_ptr != NULL);
8858 image_error ("PNG warning: %s", build_string (msg), Qnil);
8859 }
8860
8861 /* Memory source for PNG decoding. */
8862
8863 struct png_memory_storage
8864 {
8865 unsigned char *bytes; /* The data */
8866 size_t len; /* How big is it? */
8867 int index; /* Where are we? */
8868 };
8869
8870
8871 /* Function set as reader function when reading PNG image from memory.
8872 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
8873 bytes from the input to DATA. */
8874
8875 static void
8876 png_read_from_memory (png_ptr, data, length)
8877 png_structp png_ptr;
8878 png_bytep data;
8879 png_size_t length;
8880 {
8881 struct png_memory_storage *tbr
8882 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
8883
8884 if (length > tbr->len - tbr->index)
8885 png_error (png_ptr, "Read error");
8886
8887 bcopy (tbr->bytes + tbr->index, data, length);
8888 tbr->index = tbr->index + length;
8889 }
8890
8891 /* Load PNG image IMG for use on frame F. Value is non-zero if
8892 successful. */
8893
8894 static int
8895 png_load (f, img)
8896 struct frame *f;
8897 struct image *img;
8898 {
8899 Lisp_Object file, specified_file;
8900 Lisp_Object specified_data;
8901 int x, y, i;
8902 XImage *ximg, *mask_img = NULL;
8903 struct gcpro gcpro1;
8904 png_struct *png_ptr = NULL;
8905 png_info *info_ptr = NULL, *end_info = NULL;
8906 FILE *volatile fp = NULL;
8907 png_byte sig[8];
8908 png_byte * volatile pixels = NULL;
8909 png_byte ** volatile rows = NULL;
8910 png_uint_32 width, height;
8911 int bit_depth, color_type, interlace_type;
8912 png_byte channels;
8913 png_uint_32 row_bytes;
8914 int transparent_p;
8915 double screen_gamma, image_gamma;
8916 int intent;
8917 struct png_memory_storage tbr; /* Data to be read */
8918
8919 /* Find out what file to load. */
8920 specified_file = image_spec_value (img->spec, QCfile, NULL);
8921 specified_data = image_spec_value (img->spec, QCdata, NULL);
8922 file = Qnil;
8923 GCPRO1 (file);
8924
8925 if (NILP (specified_data))
8926 {
8927 file = x_find_image_file (specified_file);
8928 if (!STRINGP (file))
8929 {
8930 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8931 UNGCPRO;
8932 return 0;
8933 }
8934
8935 /* Open the image file. */
8936 fp = fopen (SDATA (file), "rb");
8937 if (!fp)
8938 {
8939 image_error ("Cannot open image file `%s'", file, Qnil);
8940 UNGCPRO;
8941 fclose (fp);
8942 return 0;
8943 }
8944
8945 /* Check PNG signature. */
8946 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
8947 || !png_check_sig (sig, sizeof sig))
8948 {
8949 image_error ("Not a PNG file: `%s'", file, Qnil);
8950 UNGCPRO;
8951 fclose (fp);
8952 return 0;
8953 }
8954 }
8955 else
8956 {
8957 /* Read from memory. */
8958 tbr.bytes = SDATA (specified_data);
8959 tbr.len = SBYTES (specified_data);
8960 tbr.index = 0;
8961
8962 /* Check PNG signature. */
8963 if (tbr.len < sizeof sig
8964 || !png_check_sig (tbr.bytes, sizeof sig))
8965 {
8966 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
8967 UNGCPRO;
8968 return 0;
8969 }
8970
8971 /* Need to skip past the signature. */
8972 tbr.bytes += sizeof (sig);
8973 }
8974
8975 /* Initialize read and info structs for PNG lib. */
8976 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
8977 my_png_error, my_png_warning);
8978 if (!png_ptr)
8979 {
8980 if (fp) fclose (fp);
8981 UNGCPRO;
8982 return 0;
8983 }
8984
8985 info_ptr = png_create_info_struct (png_ptr);
8986 if (!info_ptr)
8987 {
8988 png_destroy_read_struct (&png_ptr, NULL, NULL);
8989 if (fp) fclose (fp);
8990 UNGCPRO;
8991 return 0;
8992 }
8993
8994 end_info = png_create_info_struct (png_ptr);
8995 if (!end_info)
8996 {
8997 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
8998 if (fp) fclose (fp);
8999 UNGCPRO;
9000 return 0;
9001 }
9002
9003 /* Set error jump-back. We come back here when the PNG library
9004 detects an error. */
9005 if (setjmp (png_ptr->jmpbuf))
9006 {
9007 error:
9008 if (png_ptr)
9009 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
9010 xfree (pixels);
9011 xfree (rows);
9012 if (fp) fclose (fp);
9013 UNGCPRO;
9014 return 0;
9015 }
9016
9017 /* Read image info. */
9018 if (!NILP (specified_data))
9019 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
9020 else
9021 png_init_io (png_ptr, fp);
9022
9023 png_set_sig_bytes (png_ptr, sizeof sig);
9024 png_read_info (png_ptr, info_ptr);
9025 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
9026 &interlace_type, NULL, NULL);
9027
9028 /* If image contains simply transparency data, we prefer to
9029 construct a clipping mask. */
9030 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
9031 transparent_p = 1;
9032 else
9033 transparent_p = 0;
9034
9035 /* This function is easier to write if we only have to handle
9036 one data format: RGB or RGBA with 8 bits per channel. Let's
9037 transform other formats into that format. */
9038
9039 /* Strip more than 8 bits per channel. */
9040 if (bit_depth == 16)
9041 png_set_strip_16 (png_ptr);
9042
9043 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
9044 if available. */
9045 png_set_expand (png_ptr);
9046
9047 /* Convert grayscale images to RGB. */
9048 if (color_type == PNG_COLOR_TYPE_GRAY
9049 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
9050 png_set_gray_to_rgb (png_ptr);
9051
9052 screen_gamma = (f->gamma ? 1 / f->gamma / 0.45455 : 2.2);
9053
9054 /* Tell the PNG lib to handle gamma correction for us. */
9055
9056 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
9057 if (png_get_sRGB (png_ptr, info_ptr, &intent))
9058 /* The libpng documentation says this is right in this case. */
9059 png_set_gamma (png_ptr, screen_gamma, 0.45455);
9060 else
9061 #endif
9062 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
9063 /* Image contains gamma information. */
9064 png_set_gamma (png_ptr, screen_gamma, image_gamma);
9065 else
9066 /* Use the standard default for the image gamma. */
9067 png_set_gamma (png_ptr, screen_gamma, 0.45455);
9068
9069 /* Handle alpha channel by combining the image with a background
9070 color. Do this only if a real alpha channel is supplied. For
9071 simple transparency, we prefer a clipping mask. */
9072 if (!transparent_p)
9073 {
9074 png_color_16 *image_bg;
9075 Lisp_Object specified_bg
9076 = image_spec_value (img->spec, QCbackground, NULL);
9077
9078 if (STRINGP (specified_bg))
9079 /* The user specified `:background', use that. */
9080 {
9081 XColor color;
9082 if (x_defined_color (f, SDATA (specified_bg), &color, 0))
9083 {
9084 png_color_16 user_bg;
9085
9086 bzero (&user_bg, sizeof user_bg);
9087 user_bg.red = color.red;
9088 user_bg.green = color.green;
9089 user_bg.blue = color.blue;
9090
9091 png_set_background (png_ptr, &user_bg,
9092 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
9093 }
9094 }
9095 else if (png_get_bKGD (png_ptr, info_ptr, &image_bg))
9096 /* Image contains a background color with which to
9097 combine the image. */
9098 png_set_background (png_ptr, image_bg,
9099 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
9100 else
9101 {
9102 /* Image does not contain a background color with which
9103 to combine the image data via an alpha channel. Use
9104 the frame's background instead. */
9105 XColor color;
9106 Colormap cmap;
9107 png_color_16 frame_background;
9108
9109 cmap = FRAME_X_COLORMAP (f);
9110 color.pixel = FRAME_BACKGROUND_PIXEL (f);
9111 x_query_color (f, &color);
9112
9113 bzero (&frame_background, sizeof frame_background);
9114 frame_background.red = color.red;
9115 frame_background.green = color.green;
9116 frame_background.blue = color.blue;
9117
9118 png_set_background (png_ptr, &frame_background,
9119 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
9120 }
9121 }
9122
9123 /* Update info structure. */
9124 png_read_update_info (png_ptr, info_ptr);
9125
9126 /* Get number of channels. Valid values are 1 for grayscale images
9127 and images with a palette, 2 for grayscale images with transparency
9128 information (alpha channel), 3 for RGB images, and 4 for RGB
9129 images with alpha channel, i.e. RGBA. If conversions above were
9130 sufficient we should only have 3 or 4 channels here. */
9131 channels = png_get_channels (png_ptr, info_ptr);
9132 xassert (channels == 3 || channels == 4);
9133
9134 /* Number of bytes needed for one row of the image. */
9135 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
9136
9137 /* Allocate memory for the image. */
9138 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
9139 rows = (png_byte **) xmalloc (height * sizeof *rows);
9140 for (i = 0; i < height; ++i)
9141 rows[i] = pixels + i * row_bytes;
9142
9143 /* Read the entire image. */
9144 png_read_image (png_ptr, rows);
9145 png_read_end (png_ptr, info_ptr);
9146 if (fp)
9147 {
9148 fclose (fp);
9149 fp = NULL;
9150 }
9151
9152 /* Create the X image and pixmap. */
9153 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
9154 &img->pixmap))
9155 goto error;
9156
9157 /* Create an image and pixmap serving as mask if the PNG image
9158 contains an alpha channel. */
9159 if (channels == 4
9160 && !transparent_p
9161 && !x_create_x_image_and_pixmap (f, width, height, 1,
9162 &mask_img, &img->mask))
9163 {
9164 x_destroy_x_image (ximg);
9165 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
9166 img->pixmap = None;
9167 goto error;
9168 }
9169
9170 /* Fill the X image and mask from PNG data. */
9171 init_color_table ();
9172
9173 for (y = 0; y < height; ++y)
9174 {
9175 png_byte *p = rows[y];
9176
9177 for (x = 0; x < width; ++x)
9178 {
9179 unsigned r, g, b;
9180
9181 r = *p++ << 8;
9182 g = *p++ << 8;
9183 b = *p++ << 8;
9184 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
9185
9186 /* An alpha channel, aka mask channel, associates variable
9187 transparency with an image. Where other image formats
9188 support binary transparency---fully transparent or fully
9189 opaque---PNG allows up to 254 levels of partial transparency.
9190 The PNG library implements partial transparency by combining
9191 the image with a specified background color.
9192
9193 I'm not sure how to handle this here nicely: because the
9194 background on which the image is displayed may change, for
9195 real alpha channel support, it would be necessary to create
9196 a new image for each possible background.
9197
9198 What I'm doing now is that a mask is created if we have
9199 boolean transparency information. Otherwise I'm using
9200 the frame's background color to combine the image with. */
9201
9202 if (channels == 4)
9203 {
9204 if (mask_img)
9205 XPutPixel (mask_img, x, y, *p > 0);
9206 ++p;
9207 }
9208 }
9209 }
9210
9211 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9212 /* Set IMG's background color from the PNG image, unless the user
9213 overrode it. */
9214 {
9215 png_color_16 *bg;
9216 if (png_get_bKGD (png_ptr, info_ptr, &bg))
9217 {
9218 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
9219 img->background_valid = 1;
9220 }
9221 }
9222
9223 /* Remember colors allocated for this image. */
9224 img->colors = colors_in_color_table (&img->ncolors);
9225 free_color_table ();
9226
9227 /* Clean up. */
9228 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
9229 xfree (rows);
9230 xfree (pixels);
9231
9232 img->width = width;
9233 img->height = height;
9234
9235 /* Maybe fill in the background field while we have ximg handy. */
9236 IMAGE_BACKGROUND (img, f, ximg);
9237
9238 /* Put the image into the pixmap, then free the X image and its buffer. */
9239 x_put_x_image (f, ximg, img->pixmap, width, height);
9240 x_destroy_x_image (ximg);
9241
9242 /* Same for the mask. */
9243 if (mask_img)
9244 {
9245 /* Fill in the background_transparent field while we have the mask
9246 handy. */
9247 image_background_transparent (img, f, mask_img);
9248
9249 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
9250 x_destroy_x_image (mask_img);
9251 }
9252
9253 UNGCPRO;
9254 return 1;
9255 }
9256
9257 #endif /* HAVE_PNG != 0 */
9258
9259
9260 \f
9261 /***********************************************************************
9262 JPEG
9263 ***********************************************************************/
9264
9265 #if HAVE_JPEG
9266
9267 /* Work around a warning about HAVE_STDLIB_H being redefined in
9268 jconfig.h. */
9269 #ifdef HAVE_STDLIB_H
9270 #define HAVE_STDLIB_H_1
9271 #undef HAVE_STDLIB_H
9272 #endif /* HAVE_STLIB_H */
9273
9274 #include <jpeglib.h>
9275 #include <jerror.h>
9276 #include <setjmp.h>
9277
9278 #ifdef HAVE_STLIB_H_1
9279 #define HAVE_STDLIB_H 1
9280 #endif
9281
9282 static int jpeg_image_p P_ ((Lisp_Object object));
9283 static int jpeg_load P_ ((struct frame *f, struct image *img));
9284
9285 /* The symbol `jpeg' identifying images of this type. */
9286
9287 Lisp_Object Qjpeg;
9288
9289 /* Indices of image specification fields in gs_format, below. */
9290
9291 enum jpeg_keyword_index
9292 {
9293 JPEG_TYPE,
9294 JPEG_DATA,
9295 JPEG_FILE,
9296 JPEG_ASCENT,
9297 JPEG_MARGIN,
9298 JPEG_RELIEF,
9299 JPEG_ALGORITHM,
9300 JPEG_HEURISTIC_MASK,
9301 JPEG_MASK,
9302 JPEG_BACKGROUND,
9303 JPEG_LAST
9304 };
9305
9306 /* Vector of image_keyword structures describing the format
9307 of valid user-defined image specifications. */
9308
9309 static struct image_keyword jpeg_format[JPEG_LAST] =
9310 {
9311 {":type", IMAGE_SYMBOL_VALUE, 1},
9312 {":data", IMAGE_STRING_VALUE, 0},
9313 {":file", IMAGE_STRING_VALUE, 0},
9314 {":ascent", IMAGE_ASCENT_VALUE, 0},
9315 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
9316 {":relief", IMAGE_INTEGER_VALUE, 0},
9317 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9318 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9319 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9320 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
9321 };
9322
9323 /* Structure describing the image type `jpeg'. */
9324
9325 static struct image_type jpeg_type =
9326 {
9327 &Qjpeg,
9328 jpeg_image_p,
9329 jpeg_load,
9330 x_clear_image,
9331 NULL
9332 };
9333
9334
9335 /* Return non-zero if OBJECT is a valid JPEG image specification. */
9336
9337 static int
9338 jpeg_image_p (object)
9339 Lisp_Object object;
9340 {
9341 struct image_keyword fmt[JPEG_LAST];
9342
9343 bcopy (jpeg_format, fmt, sizeof fmt);
9344
9345 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
9346 return 0;
9347
9348 /* Must specify either the :data or :file keyword. */
9349 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
9350 }
9351
9352
9353 struct my_jpeg_error_mgr
9354 {
9355 struct jpeg_error_mgr pub;
9356 jmp_buf setjmp_buffer;
9357 };
9358
9359
9360 static void
9361 my_error_exit (cinfo)
9362 j_common_ptr cinfo;
9363 {
9364 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
9365 longjmp (mgr->setjmp_buffer, 1);
9366 }
9367
9368
9369 /* Init source method for JPEG data source manager. Called by
9370 jpeg_read_header() before any data is actually read. See
9371 libjpeg.doc from the JPEG lib distribution. */
9372
9373 static void
9374 our_init_source (cinfo)
9375 j_decompress_ptr cinfo;
9376 {
9377 }
9378
9379
9380 /* Fill input buffer method for JPEG data source manager. Called
9381 whenever more data is needed. We read the whole image in one step,
9382 so this only adds a fake end of input marker at the end. */
9383
9384 static boolean
9385 our_fill_input_buffer (cinfo)
9386 j_decompress_ptr cinfo;
9387 {
9388 /* Insert a fake EOI marker. */
9389 struct jpeg_source_mgr *src = cinfo->src;
9390 static JOCTET buffer[2];
9391
9392 buffer[0] = (JOCTET) 0xFF;
9393 buffer[1] = (JOCTET) JPEG_EOI;
9394
9395 src->next_input_byte = buffer;
9396 src->bytes_in_buffer = 2;
9397 return TRUE;
9398 }
9399
9400
9401 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
9402 is the JPEG data source manager. */
9403
9404 static void
9405 our_skip_input_data (cinfo, num_bytes)
9406 j_decompress_ptr cinfo;
9407 long num_bytes;
9408 {
9409 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
9410
9411 if (src)
9412 {
9413 if (num_bytes > src->bytes_in_buffer)
9414 ERREXIT (cinfo, JERR_INPUT_EOF);
9415
9416 src->bytes_in_buffer -= num_bytes;
9417 src->next_input_byte += num_bytes;
9418 }
9419 }
9420
9421
9422 /* Method to terminate data source. Called by
9423 jpeg_finish_decompress() after all data has been processed. */
9424
9425 static void
9426 our_term_source (cinfo)
9427 j_decompress_ptr cinfo;
9428 {
9429 }
9430
9431
9432 /* Set up the JPEG lib for reading an image from DATA which contains
9433 LEN bytes. CINFO is the decompression info structure created for
9434 reading the image. */
9435
9436 static void
9437 jpeg_memory_src (cinfo, data, len)
9438 j_decompress_ptr cinfo;
9439 JOCTET *data;
9440 unsigned int len;
9441 {
9442 struct jpeg_source_mgr *src;
9443
9444 if (cinfo->src == NULL)
9445 {
9446 /* First time for this JPEG object? */
9447 cinfo->src = (struct jpeg_source_mgr *)
9448 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
9449 sizeof (struct jpeg_source_mgr));
9450 src = (struct jpeg_source_mgr *) cinfo->src;
9451 src->next_input_byte = data;
9452 }
9453
9454 src = (struct jpeg_source_mgr *) cinfo->src;
9455 src->init_source = our_init_source;
9456 src->fill_input_buffer = our_fill_input_buffer;
9457 src->skip_input_data = our_skip_input_data;
9458 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
9459 src->term_source = our_term_source;
9460 src->bytes_in_buffer = len;
9461 src->next_input_byte = data;
9462 }
9463
9464
9465 /* Load image IMG for use on frame F. Patterned after example.c
9466 from the JPEG lib. */
9467
9468 static int
9469 jpeg_load (f, img)
9470 struct frame *f;
9471 struct image *img;
9472 {
9473 struct jpeg_decompress_struct cinfo;
9474 struct my_jpeg_error_mgr mgr;
9475 Lisp_Object file, specified_file;
9476 Lisp_Object specified_data;
9477 FILE * volatile fp = NULL;
9478 JSAMPARRAY buffer;
9479 int row_stride, x, y;
9480 XImage *ximg = NULL;
9481 int rc;
9482 unsigned long *colors;
9483 int width, height;
9484 struct gcpro gcpro1;
9485
9486 /* Open the JPEG file. */
9487 specified_file = image_spec_value (img->spec, QCfile, NULL);
9488 specified_data = image_spec_value (img->spec, QCdata, NULL);
9489 file = Qnil;
9490 GCPRO1 (file);
9491
9492 if (NILP (specified_data))
9493 {
9494 file = x_find_image_file (specified_file);
9495 if (!STRINGP (file))
9496 {
9497 image_error ("Cannot find image file `%s'", specified_file, Qnil);
9498 UNGCPRO;
9499 return 0;
9500 }
9501
9502 fp = fopen (SDATA (file), "r");
9503 if (fp == NULL)
9504 {
9505 image_error ("Cannot open `%s'", file, Qnil);
9506 UNGCPRO;
9507 return 0;
9508 }
9509 }
9510
9511 /* Customize libjpeg's error handling to call my_error_exit when an
9512 error is detected. This function will perform a longjmp. */
9513 cinfo.err = jpeg_std_error (&mgr.pub);
9514 mgr.pub.error_exit = my_error_exit;
9515
9516 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
9517 {
9518 if (rc == 1)
9519 {
9520 /* Called from my_error_exit. Display a JPEG error. */
9521 char buffer[JMSG_LENGTH_MAX];
9522 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
9523 image_error ("Error reading JPEG image `%s': %s", img->spec,
9524 build_string (buffer));
9525 }
9526
9527 /* Close the input file and destroy the JPEG object. */
9528 if (fp)
9529 fclose ((FILE *) fp);
9530 jpeg_destroy_decompress (&cinfo);
9531
9532 /* If we already have an XImage, free that. */
9533 x_destroy_x_image (ximg);
9534
9535 /* Free pixmap and colors. */
9536 x_clear_image (f, img);
9537
9538 UNGCPRO;
9539 return 0;
9540 }
9541
9542 /* Create the JPEG decompression object. Let it read from fp.
9543 Read the JPEG image header. */
9544 jpeg_create_decompress (&cinfo);
9545
9546 if (NILP (specified_data))
9547 jpeg_stdio_src (&cinfo, (FILE *) fp);
9548 else
9549 jpeg_memory_src (&cinfo, SDATA (specified_data),
9550 SBYTES (specified_data));
9551
9552 jpeg_read_header (&cinfo, TRUE);
9553
9554 /* Customize decompression so that color quantization will be used.
9555 Start decompression. */
9556 cinfo.quantize_colors = TRUE;
9557 jpeg_start_decompress (&cinfo);
9558 width = img->width = cinfo.output_width;
9559 height = img->height = cinfo.output_height;
9560
9561 /* Create X image and pixmap. */
9562 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9563 longjmp (mgr.setjmp_buffer, 2);
9564
9565 /* Allocate colors. When color quantization is used,
9566 cinfo.actual_number_of_colors has been set with the number of
9567 colors generated, and cinfo.colormap is a two-dimensional array
9568 of color indices in the range 0..cinfo.actual_number_of_colors.
9569 No more than 255 colors will be generated. */
9570 {
9571 int i, ir, ig, ib;
9572
9573 if (cinfo.out_color_components > 2)
9574 ir = 0, ig = 1, ib = 2;
9575 else if (cinfo.out_color_components > 1)
9576 ir = 0, ig = 1, ib = 0;
9577 else
9578 ir = 0, ig = 0, ib = 0;
9579
9580 /* Use the color table mechanism because it handles colors that
9581 cannot be allocated nicely. Such colors will be replaced with
9582 a default color, and we don't have to care about which colors
9583 can be freed safely, and which can't. */
9584 init_color_table ();
9585 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
9586 * sizeof *colors);
9587
9588 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
9589 {
9590 /* Multiply RGB values with 255 because X expects RGB values
9591 in the range 0..0xffff. */
9592 int r = cinfo.colormap[ir][i] << 8;
9593 int g = cinfo.colormap[ig][i] << 8;
9594 int b = cinfo.colormap[ib][i] << 8;
9595 colors[i] = lookup_rgb_color (f, r, g, b);
9596 }
9597
9598 /* Remember those colors actually allocated. */
9599 img->colors = colors_in_color_table (&img->ncolors);
9600 free_color_table ();
9601 }
9602
9603 /* Read pixels. */
9604 row_stride = width * cinfo.output_components;
9605 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
9606 row_stride, 1);
9607 for (y = 0; y < height; ++y)
9608 {
9609 jpeg_read_scanlines (&cinfo, buffer, 1);
9610 for (x = 0; x < cinfo.output_width; ++x)
9611 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
9612 }
9613
9614 /* Clean up. */
9615 jpeg_finish_decompress (&cinfo);
9616 jpeg_destroy_decompress (&cinfo);
9617 if (fp)
9618 fclose ((FILE *) fp);
9619
9620 /* Maybe fill in the background field while we have ximg handy. */
9621 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9622 IMAGE_BACKGROUND (img, f, ximg);
9623
9624 /* Put the image into the pixmap. */
9625 x_put_x_image (f, ximg, img->pixmap, width, height);
9626 x_destroy_x_image (ximg);
9627 UNGCPRO;
9628 return 1;
9629 }
9630
9631 #endif /* HAVE_JPEG */
9632
9633
9634 \f
9635 /***********************************************************************
9636 TIFF
9637 ***********************************************************************/
9638
9639 #if HAVE_TIFF
9640
9641 #include <tiffio.h>
9642
9643 static int tiff_image_p P_ ((Lisp_Object object));
9644 static int tiff_load P_ ((struct frame *f, struct image *img));
9645
9646 /* The symbol `tiff' identifying images of this type. */
9647
9648 Lisp_Object Qtiff;
9649
9650 /* Indices of image specification fields in tiff_format, below. */
9651
9652 enum tiff_keyword_index
9653 {
9654 TIFF_TYPE,
9655 TIFF_DATA,
9656 TIFF_FILE,
9657 TIFF_ASCENT,
9658 TIFF_MARGIN,
9659 TIFF_RELIEF,
9660 TIFF_ALGORITHM,
9661 TIFF_HEURISTIC_MASK,
9662 TIFF_MASK,
9663 TIFF_BACKGROUND,
9664 TIFF_LAST
9665 };
9666
9667 /* Vector of image_keyword structures describing the format
9668 of valid user-defined image specifications. */
9669
9670 static struct image_keyword tiff_format[TIFF_LAST] =
9671 {
9672 {":type", IMAGE_SYMBOL_VALUE, 1},
9673 {":data", IMAGE_STRING_VALUE, 0},
9674 {":file", IMAGE_STRING_VALUE, 0},
9675 {":ascent", IMAGE_ASCENT_VALUE, 0},
9676 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
9677 {":relief", IMAGE_INTEGER_VALUE, 0},
9678 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9679 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9680 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9681 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
9682 };
9683
9684 /* Structure describing the image type `tiff'. */
9685
9686 static struct image_type tiff_type =
9687 {
9688 &Qtiff,
9689 tiff_image_p,
9690 tiff_load,
9691 x_clear_image,
9692 NULL
9693 };
9694
9695
9696 /* Return non-zero if OBJECT is a valid TIFF image specification. */
9697
9698 static int
9699 tiff_image_p (object)
9700 Lisp_Object object;
9701 {
9702 struct image_keyword fmt[TIFF_LAST];
9703 bcopy (tiff_format, fmt, sizeof fmt);
9704
9705 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
9706 return 0;
9707
9708 /* Must specify either the :data or :file keyword. */
9709 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
9710 }
9711
9712
9713 /* Reading from a memory buffer for TIFF images Based on the PNG
9714 memory source, but we have to provide a lot of extra functions.
9715 Blah.
9716
9717 We really only need to implement read and seek, but I am not
9718 convinced that the TIFF library is smart enough not to destroy
9719 itself if we only hand it the function pointers we need to
9720 override. */
9721
9722 typedef struct
9723 {
9724 unsigned char *bytes;
9725 size_t len;
9726 int index;
9727 }
9728 tiff_memory_source;
9729
9730
9731 static size_t
9732 tiff_read_from_memory (data, buf, size)
9733 thandle_t data;
9734 tdata_t buf;
9735 tsize_t size;
9736 {
9737 tiff_memory_source *src = (tiff_memory_source *) data;
9738
9739 if (size > src->len - src->index)
9740 return (size_t) -1;
9741 bcopy (src->bytes + src->index, buf, size);
9742 src->index += size;
9743 return size;
9744 }
9745
9746
9747 static size_t
9748 tiff_write_from_memory (data, buf, size)
9749 thandle_t data;
9750 tdata_t buf;
9751 tsize_t size;
9752 {
9753 return (size_t) -1;
9754 }
9755
9756
9757 static toff_t
9758 tiff_seek_in_memory (data, off, whence)
9759 thandle_t data;
9760 toff_t off;
9761 int whence;
9762 {
9763 tiff_memory_source *src = (tiff_memory_source *) data;
9764 int idx;
9765
9766 switch (whence)
9767 {
9768 case SEEK_SET: /* Go from beginning of source. */
9769 idx = off;
9770 break;
9771
9772 case SEEK_END: /* Go from end of source. */
9773 idx = src->len + off;
9774 break;
9775
9776 case SEEK_CUR: /* Go from current position. */
9777 idx = src->index + off;
9778 break;
9779
9780 default: /* Invalid `whence'. */
9781 return -1;
9782 }
9783
9784 if (idx > src->len || idx < 0)
9785 return -1;
9786
9787 src->index = idx;
9788 return src->index;
9789 }
9790
9791
9792 static int
9793 tiff_close_memory (data)
9794 thandle_t data;
9795 {
9796 /* NOOP */
9797 return 0;
9798 }
9799
9800
9801 static int
9802 tiff_mmap_memory (data, pbase, psize)
9803 thandle_t data;
9804 tdata_t *pbase;
9805 toff_t *psize;
9806 {
9807 /* It is already _IN_ memory. */
9808 return 0;
9809 }
9810
9811
9812 static void
9813 tiff_unmap_memory (data, base, size)
9814 thandle_t data;
9815 tdata_t base;
9816 toff_t size;
9817 {
9818 /* We don't need to do this. */
9819 }
9820
9821
9822 static toff_t
9823 tiff_size_of_memory (data)
9824 thandle_t data;
9825 {
9826 return ((tiff_memory_source *) data)->len;
9827 }
9828
9829
9830 static void
9831 tiff_error_handler (title, format, ap)
9832 const char *title, *format;
9833 va_list ap;
9834 {
9835 char buf[512];
9836 int len;
9837
9838 len = sprintf (buf, "TIFF error: %s ", title);
9839 vsprintf (buf + len, format, ap);
9840 add_to_log (buf, Qnil, Qnil);
9841 }
9842
9843
9844 static void
9845 tiff_warning_handler (title, format, ap)
9846 const char *title, *format;
9847 va_list ap;
9848 {
9849 char buf[512];
9850 int len;
9851
9852 len = sprintf (buf, "TIFF warning: %s ", title);
9853 vsprintf (buf + len, format, ap);
9854 add_to_log (buf, Qnil, Qnil);
9855 }
9856
9857
9858 /* Load TIFF image IMG for use on frame F. Value is non-zero if
9859 successful. */
9860
9861 static int
9862 tiff_load (f, img)
9863 struct frame *f;
9864 struct image *img;
9865 {
9866 Lisp_Object file, specified_file;
9867 Lisp_Object specified_data;
9868 TIFF *tiff;
9869 int width, height, x, y;
9870 uint32 *buf;
9871 int rc;
9872 XImage *ximg;
9873 struct gcpro gcpro1;
9874 tiff_memory_source memsrc;
9875
9876 specified_file = image_spec_value (img->spec, QCfile, NULL);
9877 specified_data = image_spec_value (img->spec, QCdata, NULL);
9878 file = Qnil;
9879 GCPRO1 (file);
9880
9881 TIFFSetErrorHandler (tiff_error_handler);
9882 TIFFSetWarningHandler (tiff_warning_handler);
9883
9884 if (NILP (specified_data))
9885 {
9886 /* Read from a file */
9887 file = x_find_image_file (specified_file);
9888 if (!STRINGP (file))
9889 {
9890 image_error ("Cannot find image file `%s'", file, Qnil);
9891 UNGCPRO;
9892 return 0;
9893 }
9894
9895 /* Try to open the image file. */
9896 tiff = TIFFOpen (SDATA (file), "r");
9897 if (tiff == NULL)
9898 {
9899 image_error ("Cannot open `%s'", file, Qnil);
9900 UNGCPRO;
9901 return 0;
9902 }
9903 }
9904 else
9905 {
9906 /* Memory source! */
9907 memsrc.bytes = SDATA (specified_data);
9908 memsrc.len = SBYTES (specified_data);
9909 memsrc.index = 0;
9910
9911 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
9912 (TIFFReadWriteProc) tiff_read_from_memory,
9913 (TIFFReadWriteProc) tiff_write_from_memory,
9914 tiff_seek_in_memory,
9915 tiff_close_memory,
9916 tiff_size_of_memory,
9917 tiff_mmap_memory,
9918 tiff_unmap_memory);
9919
9920 if (!tiff)
9921 {
9922 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
9923 UNGCPRO;
9924 return 0;
9925 }
9926 }
9927
9928 /* Get width and height of the image, and allocate a raster buffer
9929 of width x height 32-bit values. */
9930 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
9931 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
9932 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
9933
9934 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
9935 TIFFClose (tiff);
9936 if (!rc)
9937 {
9938 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
9939 xfree (buf);
9940 UNGCPRO;
9941 return 0;
9942 }
9943
9944 /* Create the X image and pixmap. */
9945 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
9946 {
9947 xfree (buf);
9948 UNGCPRO;
9949 return 0;
9950 }
9951
9952 /* Initialize the color table. */
9953 init_color_table ();
9954
9955 /* Process the pixel raster. Origin is in the lower-left corner. */
9956 for (y = 0; y < height; ++y)
9957 {
9958 uint32 *row = buf + y * width;
9959
9960 for (x = 0; x < width; ++x)
9961 {
9962 uint32 abgr = row[x];
9963 int r = TIFFGetR (abgr) << 8;
9964 int g = TIFFGetG (abgr) << 8;
9965 int b = TIFFGetB (abgr) << 8;
9966 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
9967 }
9968 }
9969
9970 /* Remember the colors allocated for the image. Free the color table. */
9971 img->colors = colors_in_color_table (&img->ncolors);
9972 free_color_table ();
9973
9974 img->width = width;
9975 img->height = height;
9976
9977 /* Maybe fill in the background field while we have ximg handy. */
9978 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9979 IMAGE_BACKGROUND (img, f, ximg);
9980
9981 /* Put the image into the pixmap, then free the X image and its buffer. */
9982 x_put_x_image (f, ximg, img->pixmap, width, height);
9983 x_destroy_x_image (ximg);
9984 xfree (buf);
9985
9986 UNGCPRO;
9987 return 1;
9988 }
9989
9990 #endif /* HAVE_TIFF != 0 */
9991
9992
9993 \f
9994 /***********************************************************************
9995 GIF
9996 ***********************************************************************/
9997
9998 #if HAVE_GIF
9999
10000 #include <gif_lib.h>
10001
10002 static int gif_image_p P_ ((Lisp_Object object));
10003 static int gif_load P_ ((struct frame *f, struct image *img));
10004
10005 /* The symbol `gif' identifying images of this type. */
10006
10007 Lisp_Object Qgif;
10008
10009 /* Indices of image specification fields in gif_format, below. */
10010
10011 enum gif_keyword_index
10012 {
10013 GIF_TYPE,
10014 GIF_DATA,
10015 GIF_FILE,
10016 GIF_ASCENT,
10017 GIF_MARGIN,
10018 GIF_RELIEF,
10019 GIF_ALGORITHM,
10020 GIF_HEURISTIC_MASK,
10021 GIF_MASK,
10022 GIF_IMAGE,
10023 GIF_BACKGROUND,
10024 GIF_LAST
10025 };
10026
10027 /* Vector of image_keyword structures describing the format
10028 of valid user-defined image specifications. */
10029
10030 static struct image_keyword gif_format[GIF_LAST] =
10031 {
10032 {":type", IMAGE_SYMBOL_VALUE, 1},
10033 {":data", IMAGE_STRING_VALUE, 0},
10034 {":file", IMAGE_STRING_VALUE, 0},
10035 {":ascent", IMAGE_ASCENT_VALUE, 0},
10036 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
10037 {":relief", IMAGE_INTEGER_VALUE, 0},
10038 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10039 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10040 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10041 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
10042 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
10043 };
10044
10045 /* Structure describing the image type `gif'. */
10046
10047 static struct image_type gif_type =
10048 {
10049 &Qgif,
10050 gif_image_p,
10051 gif_load,
10052 x_clear_image,
10053 NULL
10054 };
10055
10056
10057 /* Return non-zero if OBJECT is a valid GIF image specification. */
10058
10059 static int
10060 gif_image_p (object)
10061 Lisp_Object object;
10062 {
10063 struct image_keyword fmt[GIF_LAST];
10064 bcopy (gif_format, fmt, sizeof fmt);
10065
10066 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
10067 return 0;
10068
10069 /* Must specify either the :data or :file keyword. */
10070 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
10071 }
10072
10073
10074 /* Reading a GIF image from memory
10075 Based on the PNG memory stuff to a certain extent. */
10076
10077 typedef struct
10078 {
10079 unsigned char *bytes;
10080 size_t len;
10081 int index;
10082 }
10083 gif_memory_source;
10084
10085
10086 /* Make the current memory source available to gif_read_from_memory.
10087 It's done this way because not all versions of libungif support
10088 a UserData field in the GifFileType structure. */
10089 static gif_memory_source *current_gif_memory_src;
10090
10091 static int
10092 gif_read_from_memory (file, buf, len)
10093 GifFileType *file;
10094 GifByteType *buf;
10095 int len;
10096 {
10097 gif_memory_source *src = current_gif_memory_src;
10098
10099 if (len > src->len - src->index)
10100 return -1;
10101
10102 bcopy (src->bytes + src->index, buf, len);
10103 src->index += len;
10104 return len;
10105 }
10106
10107
10108 /* Load GIF image IMG for use on frame F. Value is non-zero if
10109 successful. */
10110
10111 static int
10112 gif_load (f, img)
10113 struct frame *f;
10114 struct image *img;
10115 {
10116 Lisp_Object file, specified_file;
10117 Lisp_Object specified_data;
10118 int rc, width, height, x, y, i;
10119 XImage *ximg;
10120 ColorMapObject *gif_color_map;
10121 unsigned long pixel_colors[256];
10122 GifFileType *gif;
10123 struct gcpro gcpro1;
10124 Lisp_Object image;
10125 int ino, image_left, image_top, image_width, image_height;
10126 gif_memory_source memsrc;
10127 unsigned char *raster;
10128
10129 specified_file = image_spec_value (img->spec, QCfile, NULL);
10130 specified_data = image_spec_value (img->spec, QCdata, NULL);
10131 file = Qnil;
10132 GCPRO1 (file);
10133
10134 if (NILP (specified_data))
10135 {
10136 file = x_find_image_file (specified_file);
10137 if (!STRINGP (file))
10138 {
10139 image_error ("Cannot find image file `%s'", specified_file, Qnil);
10140 UNGCPRO;
10141 return 0;
10142 }
10143
10144 /* Open the GIF file. */
10145 gif = DGifOpenFileName (SDATA (file));
10146 if (gif == NULL)
10147 {
10148 image_error ("Cannot open `%s'", file, Qnil);
10149 UNGCPRO;
10150 return 0;
10151 }
10152 }
10153 else
10154 {
10155 /* Read from memory! */
10156 current_gif_memory_src = &memsrc;
10157 memsrc.bytes = SDATA (specified_data);
10158 memsrc.len = SBYTES (specified_data);
10159 memsrc.index = 0;
10160
10161 gif = DGifOpen(&memsrc, gif_read_from_memory);
10162 if (!gif)
10163 {
10164 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
10165 UNGCPRO;
10166 return 0;
10167 }
10168 }
10169
10170 /* Read entire contents. */
10171 rc = DGifSlurp (gif);
10172 if (rc == GIF_ERROR)
10173 {
10174 image_error ("Error reading `%s'", img->spec, Qnil);
10175 DGifCloseFile (gif);
10176 UNGCPRO;
10177 return 0;
10178 }
10179
10180 image = image_spec_value (img->spec, QCindex, NULL);
10181 ino = INTEGERP (image) ? XFASTINT (image) : 0;
10182 if (ino >= gif->ImageCount)
10183 {
10184 image_error ("Invalid image number `%s' in image `%s'",
10185 image, img->spec);
10186 DGifCloseFile (gif);
10187 UNGCPRO;
10188 return 0;
10189 }
10190
10191 width = img->width = max (gif->SWidth, gif->Image.Left + gif->Image.Width);
10192 height = img->height = max (gif->SHeight, gif->Image.Top + gif->Image.Height);
10193
10194 /* Create the X image and pixmap. */
10195 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
10196 {
10197 DGifCloseFile (gif);
10198 UNGCPRO;
10199 return 0;
10200 }
10201
10202 /* Allocate colors. */
10203 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
10204 if (!gif_color_map)
10205 gif_color_map = gif->SColorMap;
10206 init_color_table ();
10207 bzero (pixel_colors, sizeof pixel_colors);
10208
10209 for (i = 0; i < gif_color_map->ColorCount; ++i)
10210 {
10211 int r = gif_color_map->Colors[i].Red << 8;
10212 int g = gif_color_map->Colors[i].Green << 8;
10213 int b = gif_color_map->Colors[i].Blue << 8;
10214 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
10215 }
10216
10217 img->colors = colors_in_color_table (&img->ncolors);
10218 free_color_table ();
10219
10220 /* Clear the part of the screen image that are not covered by
10221 the image from the GIF file. Full animated GIF support
10222 requires more than can be done here (see the gif89 spec,
10223 disposal methods). Let's simply assume that the part
10224 not covered by a sub-image is in the frame's background color. */
10225 image_top = gif->SavedImages[ino].ImageDesc.Top;
10226 image_left = gif->SavedImages[ino].ImageDesc.Left;
10227 image_width = gif->SavedImages[ino].ImageDesc.Width;
10228 image_height = gif->SavedImages[ino].ImageDesc.Height;
10229
10230 for (y = 0; y < image_top; ++y)
10231 for (x = 0; x < width; ++x)
10232 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10233
10234 for (y = image_top + image_height; y < height; ++y)
10235 for (x = 0; x < width; ++x)
10236 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10237
10238 for (y = image_top; y < image_top + image_height; ++y)
10239 {
10240 for (x = 0; x < image_left; ++x)
10241 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10242 for (x = image_left + image_width; x < width; ++x)
10243 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10244 }
10245
10246 /* Read the GIF image into the X image. We use a local variable
10247 `raster' here because RasterBits below is a char *, and invites
10248 problems with bytes >= 0x80. */
10249 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
10250
10251 if (gif->SavedImages[ino].ImageDesc.Interlace)
10252 {
10253 static int interlace_start[] = {0, 4, 2, 1};
10254 static int interlace_increment[] = {8, 8, 4, 2};
10255 int pass;
10256 int row = interlace_start[0];
10257
10258 pass = 0;
10259
10260 for (y = 0; y < image_height; y++)
10261 {
10262 if (row >= image_height)
10263 {
10264 row = interlace_start[++pass];
10265 while (row >= image_height)
10266 row = interlace_start[++pass];
10267 }
10268
10269 for (x = 0; x < image_width; x++)
10270 {
10271 int i = raster[(y * image_width) + x];
10272 XPutPixel (ximg, x + image_left, row + image_top,
10273 pixel_colors[i]);
10274 }
10275
10276 row += interlace_increment[pass];
10277 }
10278 }
10279 else
10280 {
10281 for (y = 0; y < image_height; ++y)
10282 for (x = 0; x < image_width; ++x)
10283 {
10284 int i = raster[y * image_width + x];
10285 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
10286 }
10287 }
10288
10289 DGifCloseFile (gif);
10290
10291 /* Maybe fill in the background field while we have ximg handy. */
10292 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
10293 IMAGE_BACKGROUND (img, f, ximg);
10294
10295 /* Put the image into the pixmap, then free the X image and its buffer. */
10296 x_put_x_image (f, ximg, img->pixmap, width, height);
10297 x_destroy_x_image (ximg);
10298
10299 UNGCPRO;
10300 return 1;
10301 }
10302
10303 #endif /* HAVE_GIF != 0 */
10304
10305
10306 \f
10307 /***********************************************************************
10308 Ghostscript
10309 ***********************************************************************/
10310
10311 static int gs_image_p P_ ((Lisp_Object object));
10312 static int gs_load P_ ((struct frame *f, struct image *img));
10313 static void gs_clear_image P_ ((struct frame *f, struct image *img));
10314
10315 /* The symbol `postscript' identifying images of this type. */
10316
10317 Lisp_Object Qpostscript;
10318
10319 /* Keyword symbols. */
10320
10321 Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
10322
10323 /* Indices of image specification fields in gs_format, below. */
10324
10325 enum gs_keyword_index
10326 {
10327 GS_TYPE,
10328 GS_PT_WIDTH,
10329 GS_PT_HEIGHT,
10330 GS_FILE,
10331 GS_LOADER,
10332 GS_BOUNDING_BOX,
10333 GS_ASCENT,
10334 GS_MARGIN,
10335 GS_RELIEF,
10336 GS_ALGORITHM,
10337 GS_HEURISTIC_MASK,
10338 GS_MASK,
10339 GS_BACKGROUND,
10340 GS_LAST
10341 };
10342
10343 /* Vector of image_keyword structures describing the format
10344 of valid user-defined image specifications. */
10345
10346 static struct image_keyword gs_format[GS_LAST] =
10347 {
10348 {":type", IMAGE_SYMBOL_VALUE, 1},
10349 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
10350 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
10351 {":file", IMAGE_STRING_VALUE, 1},
10352 {":loader", IMAGE_FUNCTION_VALUE, 0},
10353 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
10354 {":ascent", IMAGE_ASCENT_VALUE, 0},
10355 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
10356 {":relief", IMAGE_INTEGER_VALUE, 0},
10357 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10358 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10359 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10360 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
10361 };
10362
10363 /* Structure describing the image type `ghostscript'. */
10364
10365 static struct image_type gs_type =
10366 {
10367 &Qpostscript,
10368 gs_image_p,
10369 gs_load,
10370 gs_clear_image,
10371 NULL
10372 };
10373
10374
10375 /* Free X resources of Ghostscript image IMG which is used on frame F. */
10376
10377 static void
10378 gs_clear_image (f, img)
10379 struct frame *f;
10380 struct image *img;
10381 {
10382 /* IMG->data.ptr_val may contain a recorded colormap. */
10383 xfree (img->data.ptr_val);
10384 x_clear_image (f, img);
10385 }
10386
10387
10388 /* Return non-zero if OBJECT is a valid Ghostscript image
10389 specification. */
10390
10391 static int
10392 gs_image_p (object)
10393 Lisp_Object object;
10394 {
10395 struct image_keyword fmt[GS_LAST];
10396 Lisp_Object tem;
10397 int i;
10398
10399 bcopy (gs_format, fmt, sizeof fmt);
10400
10401 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
10402 return 0;
10403
10404 /* Bounding box must be a list or vector containing 4 integers. */
10405 tem = fmt[GS_BOUNDING_BOX].value;
10406 if (CONSP (tem))
10407 {
10408 for (i = 0; i < 4; ++i, tem = XCDR (tem))
10409 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
10410 return 0;
10411 if (!NILP (tem))
10412 return 0;
10413 }
10414 else if (VECTORP (tem))
10415 {
10416 if (XVECTOR (tem)->size != 4)
10417 return 0;
10418 for (i = 0; i < 4; ++i)
10419 if (!INTEGERP (XVECTOR (tem)->contents[i]))
10420 return 0;
10421 }
10422 else
10423 return 0;
10424
10425 return 1;
10426 }
10427
10428
10429 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
10430 if successful. */
10431
10432 static int
10433 gs_load (f, img)
10434 struct frame *f;
10435 struct image *img;
10436 {
10437 char buffer[100];
10438 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
10439 struct gcpro gcpro1, gcpro2;
10440 Lisp_Object frame;
10441 double in_width, in_height;
10442 Lisp_Object pixel_colors = Qnil;
10443
10444 /* Compute pixel size of pixmap needed from the given size in the
10445 image specification. Sizes in the specification are in pt. 1 pt
10446 = 1/72 in, xdpi and ydpi are stored in the frame's X display
10447 info. */
10448 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
10449 in_width = XFASTINT (pt_width) / 72.0;
10450 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
10451 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
10452 in_height = XFASTINT (pt_height) / 72.0;
10453 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
10454
10455 /* Create the pixmap. */
10456 xassert (img->pixmap == None);
10457 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10458 img->width, img->height,
10459 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
10460
10461 if (!img->pixmap)
10462 {
10463 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
10464 return 0;
10465 }
10466
10467 /* Call the loader to fill the pixmap. It returns a process object
10468 if successful. We do not record_unwind_protect here because
10469 other places in redisplay like calling window scroll functions
10470 don't either. Let the Lisp loader use `unwind-protect' instead. */
10471 GCPRO2 (window_and_pixmap_id, pixel_colors);
10472
10473 sprintf (buffer, "%lu %lu",
10474 (unsigned long) FRAME_X_WINDOW (f),
10475 (unsigned long) img->pixmap);
10476 window_and_pixmap_id = build_string (buffer);
10477
10478 sprintf (buffer, "%lu %lu",
10479 FRAME_FOREGROUND_PIXEL (f),
10480 FRAME_BACKGROUND_PIXEL (f));
10481 pixel_colors = build_string (buffer);
10482
10483 XSETFRAME (frame, f);
10484 loader = image_spec_value (img->spec, QCloader, NULL);
10485 if (NILP (loader))
10486 loader = intern ("gs-load-image");
10487
10488 img->data.lisp_val = call6 (loader, frame, img->spec,
10489 make_number (img->width),
10490 make_number (img->height),
10491 window_and_pixmap_id,
10492 pixel_colors);
10493 UNGCPRO;
10494 return PROCESSP (img->data.lisp_val);
10495 }
10496
10497
10498 /* Kill the Ghostscript process that was started to fill PIXMAP on
10499 frame F. Called from XTread_socket when receiving an event
10500 telling Emacs that Ghostscript has finished drawing. */
10501
10502 void
10503 x_kill_gs_process (pixmap, f)
10504 Pixmap pixmap;
10505 struct frame *f;
10506 {
10507 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
10508 int class, i;
10509 struct image *img;
10510
10511 /* Find the image containing PIXMAP. */
10512 for (i = 0; i < c->used; ++i)
10513 if (c->images[i]->pixmap == pixmap)
10514 break;
10515
10516 /* Should someone in between have cleared the image cache, for
10517 instance, give up. */
10518 if (i == c->used)
10519 return;
10520
10521 /* Kill the GS process. We should have found PIXMAP in the image
10522 cache and its image should contain a process object. */
10523 img = c->images[i];
10524 xassert (PROCESSP (img->data.lisp_val));
10525 Fkill_process (img->data.lisp_val, Qnil);
10526 img->data.lisp_val = Qnil;
10527
10528 /* On displays with a mutable colormap, figure out the colors
10529 allocated for the image by looking at the pixels of an XImage for
10530 img->pixmap. */
10531 class = FRAME_X_VISUAL (f)->class;
10532 if (class != StaticColor && class != StaticGray && class != TrueColor)
10533 {
10534 XImage *ximg;
10535
10536 BLOCK_INPUT;
10537
10538 /* Try to get an XImage for img->pixmep. */
10539 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
10540 0, 0, img->width, img->height, ~0, ZPixmap);
10541 if (ximg)
10542 {
10543 int x, y;
10544
10545 /* Initialize the color table. */
10546 init_color_table ();
10547
10548 /* For each pixel of the image, look its color up in the
10549 color table. After having done so, the color table will
10550 contain an entry for each color used by the image. */
10551 for (y = 0; y < img->height; ++y)
10552 for (x = 0; x < img->width; ++x)
10553 {
10554 unsigned long pixel = XGetPixel (ximg, x, y);
10555 lookup_pixel_color (f, pixel);
10556 }
10557
10558 /* Record colors in the image. Free color table and XImage. */
10559 img->colors = colors_in_color_table (&img->ncolors);
10560 free_color_table ();
10561 XDestroyImage (ximg);
10562
10563 #if 0 /* This doesn't seem to be the case. If we free the colors
10564 here, we get a BadAccess later in x_clear_image when
10565 freeing the colors. */
10566 /* We have allocated colors once, but Ghostscript has also
10567 allocated colors on behalf of us. So, to get the
10568 reference counts right, free them once. */
10569 if (img->ncolors)
10570 x_free_colors (f, img->colors, img->ncolors);
10571 #endif
10572 }
10573 else
10574 image_error ("Cannot get X image of `%s'; colors will not be freed",
10575 img->spec, Qnil);
10576
10577 UNBLOCK_INPUT;
10578 }
10579
10580 /* Now that we have the pixmap, compute mask and transform the
10581 image if requested. */
10582 BLOCK_INPUT;
10583 postprocess_image (f, img);
10584 UNBLOCK_INPUT;
10585 }
10586
10587
10588 \f
10589 /***********************************************************************
10590 Window properties
10591 ***********************************************************************/
10592
10593 DEFUN ("x-change-window-property", Fx_change_window_property,
10594 Sx_change_window_property, 2, 3, 0,
10595 doc: /* Change window property PROP to VALUE on the X window of FRAME.
10596 PROP and VALUE must be strings. FRAME nil or omitted means use the
10597 selected frame. Value is VALUE. */)
10598 (prop, value, frame)
10599 Lisp_Object frame, prop, value;
10600 {
10601 struct frame *f = check_x_frame (frame);
10602 Atom prop_atom;
10603
10604 CHECK_STRING (prop);
10605 CHECK_STRING (value);
10606
10607 BLOCK_INPUT;
10608 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
10609 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10610 prop_atom, XA_STRING, 8, PropModeReplace,
10611 SDATA (value), SCHARS (value));
10612
10613 /* Make sure the property is set when we return. */
10614 XFlush (FRAME_X_DISPLAY (f));
10615 UNBLOCK_INPUT;
10616
10617 return value;
10618 }
10619
10620
10621 DEFUN ("x-delete-window-property", Fx_delete_window_property,
10622 Sx_delete_window_property, 1, 2, 0,
10623 doc: /* Remove window property PROP from X window of FRAME.
10624 FRAME nil or omitted means use the selected frame. Value is PROP. */)
10625 (prop, frame)
10626 Lisp_Object prop, frame;
10627 {
10628 struct frame *f = check_x_frame (frame);
10629 Atom prop_atom;
10630
10631 CHECK_STRING (prop);
10632 BLOCK_INPUT;
10633 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
10634 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
10635
10636 /* Make sure the property is removed when we return. */
10637 XFlush (FRAME_X_DISPLAY (f));
10638 UNBLOCK_INPUT;
10639
10640 return prop;
10641 }
10642
10643
10644 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
10645 1, 2, 0,
10646 doc: /* Value is the value of window property PROP on FRAME.
10647 If FRAME is nil or omitted, use the selected frame. Value is nil
10648 if FRAME hasn't a property with name PROP or if PROP has no string
10649 value. */)
10650 (prop, frame)
10651 Lisp_Object prop, frame;
10652 {
10653 struct frame *f = check_x_frame (frame);
10654 Atom prop_atom;
10655 int rc;
10656 Lisp_Object prop_value = Qnil;
10657 char *tmp_data = NULL;
10658 Atom actual_type;
10659 int actual_format;
10660 unsigned long actual_size, bytes_remaining;
10661
10662 CHECK_STRING (prop);
10663 BLOCK_INPUT;
10664 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
10665 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10666 prop_atom, 0, 0, False, XA_STRING,
10667 &actual_type, &actual_format, &actual_size,
10668 &bytes_remaining, (unsigned char **) &tmp_data);
10669 if (rc == Success)
10670 {
10671 int size = bytes_remaining;
10672
10673 XFree (tmp_data);
10674 tmp_data = NULL;
10675
10676 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10677 prop_atom, 0, bytes_remaining,
10678 False, XA_STRING,
10679 &actual_type, &actual_format,
10680 &actual_size, &bytes_remaining,
10681 (unsigned char **) &tmp_data);
10682 if (rc == Success && tmp_data)
10683 prop_value = make_string (tmp_data, size);
10684
10685 XFree (tmp_data);
10686 }
10687
10688 UNBLOCK_INPUT;
10689 return prop_value;
10690 }
10691
10692
10693 \f
10694 /***********************************************************************
10695 Busy cursor
10696 ***********************************************************************/
10697
10698 /* If non-null, an asynchronous timer that, when it expires, displays
10699 an hourglass cursor on all frames. */
10700
10701 static struct atimer *hourglass_atimer;
10702
10703 /* Non-zero means an hourglass cursor is currently shown. */
10704
10705 static int hourglass_shown_p;
10706
10707 /* Number of seconds to wait before displaying an hourglass cursor. */
10708
10709 static Lisp_Object Vhourglass_delay;
10710
10711 /* Default number of seconds to wait before displaying an hourglass
10712 cursor. */
10713
10714 #define DEFAULT_HOURGLASS_DELAY 1
10715
10716 /* Function prototypes. */
10717
10718 static void show_hourglass P_ ((struct atimer *));
10719 static void hide_hourglass P_ ((void));
10720
10721
10722 /* Cancel a currently active hourglass timer, and start a new one. */
10723
10724 void
10725 start_hourglass ()
10726 {
10727 EMACS_TIME delay;
10728 int secs, usecs = 0;
10729
10730 cancel_hourglass ();
10731
10732 if (INTEGERP (Vhourglass_delay)
10733 && XINT (Vhourglass_delay) > 0)
10734 secs = XFASTINT (Vhourglass_delay);
10735 else if (FLOATP (Vhourglass_delay)
10736 && XFLOAT_DATA (Vhourglass_delay) > 0)
10737 {
10738 Lisp_Object tem;
10739 tem = Ftruncate (Vhourglass_delay, Qnil);
10740 secs = XFASTINT (tem);
10741 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
10742 }
10743 else
10744 secs = DEFAULT_HOURGLASS_DELAY;
10745
10746 EMACS_SET_SECS_USECS (delay, secs, usecs);
10747 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
10748 show_hourglass, NULL);
10749 }
10750
10751
10752 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
10753 shown. */
10754
10755 void
10756 cancel_hourglass ()
10757 {
10758 if (hourglass_atimer)
10759 {
10760 cancel_atimer (hourglass_atimer);
10761 hourglass_atimer = NULL;
10762 }
10763
10764 if (hourglass_shown_p)
10765 hide_hourglass ();
10766 }
10767
10768
10769 /* Timer function of hourglass_atimer. TIMER is equal to
10770 hourglass_atimer.
10771
10772 Display an hourglass pointer on all frames by mapping the frames'
10773 hourglass_window. Set the hourglass_p flag in the frames'
10774 output_data.x structure to indicate that an hourglass cursor is
10775 shown on the frames. */
10776
10777 static void
10778 show_hourglass (timer)
10779 struct atimer *timer;
10780 {
10781 /* The timer implementation will cancel this timer automatically
10782 after this function has run. Set hourglass_atimer to null
10783 so that we know the timer doesn't have to be canceled. */
10784 hourglass_atimer = NULL;
10785
10786 if (!hourglass_shown_p)
10787 {
10788 Lisp_Object rest, frame;
10789
10790 BLOCK_INPUT;
10791
10792 FOR_EACH_FRAME (rest, frame)
10793 {
10794 struct frame *f = XFRAME (frame);
10795
10796 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
10797 {
10798 Display *dpy = FRAME_X_DISPLAY (f);
10799
10800 #ifdef USE_X_TOOLKIT
10801 if (f->output_data.x->widget)
10802 #else
10803 if (FRAME_OUTER_WINDOW (f))
10804 #endif
10805 {
10806 f->output_data.x->hourglass_p = 1;
10807
10808 if (!f->output_data.x->hourglass_window)
10809 {
10810 unsigned long mask = CWCursor;
10811 XSetWindowAttributes attrs;
10812
10813 attrs.cursor = f->output_data.x->hourglass_cursor;
10814
10815 f->output_data.x->hourglass_window
10816 = XCreateWindow (dpy, FRAME_OUTER_WINDOW (f),
10817 0, 0, 32000, 32000, 0, 0,
10818 InputOnly,
10819 CopyFromParent,
10820 mask, &attrs);
10821 }
10822
10823 XMapRaised (dpy, f->output_data.x->hourglass_window);
10824 XFlush (dpy);
10825 }
10826 }
10827 }
10828
10829 hourglass_shown_p = 1;
10830 UNBLOCK_INPUT;
10831 }
10832 }
10833
10834
10835 /* Hide the hourglass pointer on all frames, if it is currently
10836 shown. */
10837
10838 static void
10839 hide_hourglass ()
10840 {
10841 if (hourglass_shown_p)
10842 {
10843 Lisp_Object rest, frame;
10844
10845 BLOCK_INPUT;
10846 FOR_EACH_FRAME (rest, frame)
10847 {
10848 struct frame *f = XFRAME (frame);
10849
10850 if (FRAME_X_P (f)
10851 /* Watch out for newly created frames. */
10852 && f->output_data.x->hourglass_window)
10853 {
10854 XUnmapWindow (FRAME_X_DISPLAY (f),
10855 f->output_data.x->hourglass_window);
10856 /* Sync here because XTread_socket looks at the
10857 hourglass_p flag that is reset to zero below. */
10858 XSync (FRAME_X_DISPLAY (f), False);
10859 f->output_data.x->hourglass_p = 0;
10860 }
10861 }
10862
10863 hourglass_shown_p = 0;
10864 UNBLOCK_INPUT;
10865 }
10866 }
10867
10868
10869 \f
10870 /***********************************************************************
10871 Tool tips
10872 ***********************************************************************/
10873
10874 static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
10875 Lisp_Object, Lisp_Object));
10876 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
10877 Lisp_Object, int, int, int *, int *));
10878
10879 /* The frame of a currently visible tooltip. */
10880
10881 Lisp_Object tip_frame;
10882
10883 /* If non-nil, a timer started that hides the last tooltip when it
10884 fires. */
10885
10886 Lisp_Object tip_timer;
10887 Window tip_window;
10888
10889 /* If non-nil, a vector of 3 elements containing the last args
10890 with which x-show-tip was called. See there. */
10891
10892 Lisp_Object last_show_tip_args;
10893
10894 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
10895
10896 Lisp_Object Vx_max_tooltip_size;
10897
10898
10899 static Lisp_Object
10900 unwind_create_tip_frame (frame)
10901 Lisp_Object frame;
10902 {
10903 Lisp_Object deleted;
10904
10905 deleted = unwind_create_frame (frame);
10906 if (EQ (deleted, Qt))
10907 {
10908 tip_window = None;
10909 tip_frame = Qnil;
10910 }
10911
10912 return deleted;
10913 }
10914
10915
10916 /* Create a frame for a tooltip on the display described by DPYINFO.
10917 PARMS is a list of frame parameters. TEXT is the string to
10918 display in the tip frame. Value is the frame.
10919
10920 Note that functions called here, esp. x_default_parameter can
10921 signal errors, for instance when a specified color name is
10922 undefined. We have to make sure that we're in a consistent state
10923 when this happens. */
10924
10925 static Lisp_Object
10926 x_create_tip_frame (dpyinfo, parms, text)
10927 struct x_display_info *dpyinfo;
10928 Lisp_Object parms, text;
10929 {
10930 struct frame *f;
10931 Lisp_Object frame, tem;
10932 Lisp_Object name;
10933 long window_prompting = 0;
10934 int width, height;
10935 int count = SPECPDL_INDEX ();
10936 struct gcpro gcpro1, gcpro2, gcpro3;
10937 struct kboard *kb;
10938 int face_change_count_before = face_change_count;
10939 Lisp_Object buffer;
10940 struct buffer *old_buffer;
10941
10942 check_x ();
10943
10944 /* Use this general default value to start with until we know if
10945 this frame has a specified name. */
10946 Vx_resource_name = Vinvocation_name;
10947
10948 #ifdef MULTI_KBOARD
10949 kb = dpyinfo->kboard;
10950 #else
10951 kb = &the_only_kboard;
10952 #endif
10953
10954 /* Get the name of the frame to use for resource lookup. */
10955 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
10956 if (!STRINGP (name)
10957 && !EQ (name, Qunbound)
10958 && !NILP (name))
10959 error ("Invalid frame name--not a string or nil");
10960 Vx_resource_name = name;
10961
10962 frame = Qnil;
10963 GCPRO3 (parms, name, frame);
10964 f = make_frame (1);
10965 XSETFRAME (frame, f);
10966
10967 buffer = Fget_buffer_create (build_string (" *tip*"));
10968 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
10969 old_buffer = current_buffer;
10970 set_buffer_internal_1 (XBUFFER (buffer));
10971 current_buffer->truncate_lines = Qnil;
10972 Ferase_buffer ();
10973 Finsert (1, &text);
10974 set_buffer_internal_1 (old_buffer);
10975
10976 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
10977 record_unwind_protect (unwind_create_tip_frame, frame);
10978
10979 /* By setting the output method, we're essentially saying that
10980 the frame is live, as per FRAME_LIVE_P. If we get a signal
10981 from this point on, x_destroy_window might screw up reference
10982 counts etc. */
10983 f->output_method = output_x_window;
10984 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
10985 bzero (f->output_data.x, sizeof (struct x_output));
10986 f->output_data.x->icon_bitmap = -1;
10987 f->output_data.x->fontset = -1;
10988 f->output_data.x->scroll_bar_foreground_pixel = -1;
10989 f->output_data.x->scroll_bar_background_pixel = -1;
10990 #ifdef USE_TOOLKIT_SCROLL_BARS
10991 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
10992 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
10993 #endif /* USE_TOOLKIT_SCROLL_BARS */
10994 f->icon_name = Qnil;
10995 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
10996 #if GLYPH_DEBUG
10997 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
10998 dpyinfo_refcount = dpyinfo->reference_count;
10999 #endif /* GLYPH_DEBUG */
11000 #ifdef MULTI_KBOARD
11001 FRAME_KBOARD (f) = kb;
11002 #endif
11003 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
11004 f->output_data.x->explicit_parent = 0;
11005
11006 /* These colors will be set anyway later, but it's important
11007 to get the color reference counts right, so initialize them! */
11008 {
11009 Lisp_Object black;
11010 struct gcpro gcpro1;
11011
11012 black = build_string ("black");
11013 GCPRO1 (black);
11014 f->output_data.x->foreground_pixel
11015 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
11016 f->output_data.x->background_pixel
11017 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
11018 f->output_data.x->cursor_pixel
11019 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
11020 f->output_data.x->cursor_foreground_pixel
11021 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
11022 f->output_data.x->border_pixel
11023 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
11024 f->output_data.x->mouse_pixel
11025 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
11026 UNGCPRO;
11027 }
11028
11029 /* Set the name; the functions to which we pass f expect the name to
11030 be set. */
11031 if (EQ (name, Qunbound) || NILP (name))
11032 {
11033 f->name = build_string (dpyinfo->x_id_name);
11034 f->explicit_name = 0;
11035 }
11036 else
11037 {
11038 f->name = name;
11039 f->explicit_name = 1;
11040 /* use the frame's title when getting resources for this frame. */
11041 specbind (Qx_resource_name, name);
11042 }
11043
11044 /* Extract the window parameters from the supplied values that are
11045 needed to determine window geometry. */
11046 {
11047 Lisp_Object font;
11048
11049 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
11050
11051 BLOCK_INPUT;
11052 /* First, try whatever font the caller has specified. */
11053 if (STRINGP (font))
11054 {
11055 tem = Fquery_fontset (font, Qnil);
11056 if (STRINGP (tem))
11057 font = x_new_fontset (f, SDATA (tem));
11058 else
11059 font = x_new_font (f, SDATA (font));
11060 }
11061
11062 /* Try out a font which we hope has bold and italic variations. */
11063 if (!STRINGP (font))
11064 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
11065 if (!STRINGP (font))
11066 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
11067 if (! STRINGP (font))
11068 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
11069 if (! STRINGP (font))
11070 /* This was formerly the first thing tried, but it finds too many fonts
11071 and takes too long. */
11072 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
11073 /* If those didn't work, look for something which will at least work. */
11074 if (! STRINGP (font))
11075 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
11076 UNBLOCK_INPUT;
11077 if (! STRINGP (font))
11078 font = build_string ("fixed");
11079
11080 x_default_parameter (f, parms, Qfont, font,
11081 "font", "Font", RES_TYPE_STRING);
11082 }
11083
11084 x_default_parameter (f, parms, Qborder_width, make_number (2),
11085 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
11086
11087 /* This defaults to 2 in order to match xterm. We recognize either
11088 internalBorderWidth or internalBorder (which is what xterm calls
11089 it). */
11090 if (NILP (Fassq (Qinternal_border_width, parms)))
11091 {
11092 Lisp_Object value;
11093
11094 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
11095 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
11096 if (! EQ (value, Qunbound))
11097 parms = Fcons (Fcons (Qinternal_border_width, value),
11098 parms);
11099 }
11100
11101 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
11102 "internalBorderWidth", "internalBorderWidth",
11103 RES_TYPE_NUMBER);
11104
11105 /* Also do the stuff which must be set before the window exists. */
11106 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
11107 "foreground", "Foreground", RES_TYPE_STRING);
11108 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
11109 "background", "Background", RES_TYPE_STRING);
11110 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
11111 "pointerColor", "Foreground", RES_TYPE_STRING);
11112 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
11113 "cursorColor", "Foreground", RES_TYPE_STRING);
11114 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
11115 "borderColor", "BorderColor", RES_TYPE_STRING);
11116
11117 /* Init faces before x_default_parameter is called for scroll-bar
11118 parameters because that function calls x_set_scroll_bar_width,
11119 which calls change_frame_size, which calls Fset_window_buffer,
11120 which runs hooks, which call Fvertical_motion. At the end, we
11121 end up in init_iterator with a null face cache, which should not
11122 happen. */
11123 init_frame_faces (f);
11124
11125 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
11126 window_prompting = x_figure_window_size (f, parms);
11127
11128 if (window_prompting & XNegative)
11129 {
11130 if (window_prompting & YNegative)
11131 f->output_data.x->win_gravity = SouthEastGravity;
11132 else
11133 f->output_data.x->win_gravity = NorthEastGravity;
11134 }
11135 else
11136 {
11137 if (window_prompting & YNegative)
11138 f->output_data.x->win_gravity = SouthWestGravity;
11139 else
11140 f->output_data.x->win_gravity = NorthWestGravity;
11141 }
11142
11143 f->output_data.x->size_hint_flags = window_prompting;
11144 {
11145 XSetWindowAttributes attrs;
11146 unsigned long mask;
11147
11148 BLOCK_INPUT;
11149 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
11150 if (DoesSaveUnders (dpyinfo->screen))
11151 mask |= CWSaveUnder;
11152
11153 /* Window managers look at the override-redirect flag to determine
11154 whether or net to give windows a decoration (Xlib spec, chapter
11155 3.2.8). */
11156 attrs.override_redirect = True;
11157 attrs.save_under = True;
11158 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
11159 /* Arrange for getting MapNotify and UnmapNotify events. */
11160 attrs.event_mask = StructureNotifyMask;
11161 tip_window
11162 = FRAME_X_WINDOW (f)
11163 = XCreateWindow (FRAME_X_DISPLAY (f),
11164 FRAME_X_DISPLAY_INFO (f)->root_window,
11165 /* x, y, width, height */
11166 0, 0, 1, 1,
11167 /* Border. */
11168 1,
11169 CopyFromParent, InputOutput, CopyFromParent,
11170 mask, &attrs);
11171 UNBLOCK_INPUT;
11172 }
11173
11174 x_make_gc (f);
11175
11176 x_default_parameter (f, parms, Qauto_raise, Qnil,
11177 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
11178 x_default_parameter (f, parms, Qauto_lower, Qnil,
11179 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
11180 x_default_parameter (f, parms, Qcursor_type, Qbox,
11181 "cursorType", "CursorType", RES_TYPE_SYMBOL);
11182
11183 /* Dimensions, especially f->height, must be done via change_frame_size.
11184 Change will not be effected unless different from the current
11185 f->height. */
11186 width = f->width;
11187 height = f->height;
11188 f->height = 0;
11189 SET_FRAME_WIDTH (f, 0);
11190 change_frame_size (f, height, width, 1, 0, 0);
11191
11192 /* Set up faces after all frame parameters are known. This call
11193 also merges in face attributes specified for new frames.
11194
11195 Frame parameters may be changed if .Xdefaults contains
11196 specifications for the default font. For example, if there is an
11197 `Emacs.default.attributeBackground: pink', the `background-color'
11198 attribute of the frame get's set, which let's the internal border
11199 of the tooltip frame appear in pink. Prevent this. */
11200 {
11201 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
11202
11203 /* Set tip_frame here, so that */
11204 tip_frame = frame;
11205 call1 (Qface_set_after_frame_default, frame);
11206
11207 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
11208 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
11209 Qnil));
11210 }
11211
11212 f->no_split = 1;
11213
11214 UNGCPRO;
11215
11216 /* It is now ok to make the frame official even if we get an error
11217 below. And the frame needs to be on Vframe_list or making it
11218 visible won't work. */
11219 Vframe_list = Fcons (frame, Vframe_list);
11220
11221 /* Now that the frame is official, it counts as a reference to
11222 its display. */
11223 FRAME_X_DISPLAY_INFO (f)->reference_count++;
11224
11225 /* Setting attributes of faces of the tooltip frame from resources
11226 and similar will increment face_change_count, which leads to the
11227 clearing of all current matrices. Since this isn't necessary
11228 here, avoid it by resetting face_change_count to the value it
11229 had before we created the tip frame. */
11230 face_change_count = face_change_count_before;
11231
11232 /* Discard the unwind_protect. */
11233 return unbind_to (count, frame);
11234 }
11235
11236
11237 /* Compute where to display tip frame F. PARMS is the list of frame
11238 parameters for F. DX and DY are specified offsets from the current
11239 location of the mouse. WIDTH and HEIGHT are the width and height
11240 of the tooltip. Return coordinates relative to the root window of
11241 the display in *ROOT_X, and *ROOT_Y. */
11242
11243 static void
11244 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
11245 struct frame *f;
11246 Lisp_Object parms, dx, dy;
11247 int width, height;
11248 int *root_x, *root_y;
11249 {
11250 Lisp_Object left, top;
11251 int win_x, win_y;
11252 Window root, child;
11253 unsigned pmask;
11254
11255 /* User-specified position? */
11256 left = Fcdr (Fassq (Qleft, parms));
11257 top = Fcdr (Fassq (Qtop, parms));
11258
11259 /* Move the tooltip window where the mouse pointer is. Resize and
11260 show it. */
11261 if (!INTEGERP (left) || !INTEGERP (top))
11262 {
11263 BLOCK_INPUT;
11264 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
11265 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
11266 UNBLOCK_INPUT;
11267 }
11268
11269 if (INTEGERP (top))
11270 *root_y = XINT (top);
11271 else if (*root_y + XINT (dy) - height < 0)
11272 *root_y -= XINT (dy);
11273 else
11274 {
11275 *root_y -= height;
11276 *root_y += XINT (dy);
11277 }
11278
11279 if (INTEGERP (left))
11280 *root_x = XINT (left);
11281 else if (*root_x + XINT (dx) + width <= FRAME_X_DISPLAY_INFO (f)->width)
11282 /* It fits to the right of the pointer. */
11283 *root_x += XINT (dx);
11284 else if (width + XINT (dx) <= *root_x)
11285 /* It fits to the left of the pointer. */
11286 *root_x -= width + XINT (dx);
11287 else
11288 /* Put it left-justified on the screen--it ought to fit that way. */
11289 *root_x = 0;
11290 }
11291
11292
11293 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
11294 doc: /* Show STRING in a "tooltip" window on frame FRAME.
11295 A tooltip window is a small X window displaying a string.
11296
11297 FRAME nil or omitted means use the selected frame.
11298
11299 PARMS is an optional list of frame parameters which can be used to
11300 change the tooltip's appearance.
11301
11302 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
11303 means use the default timeout of 5 seconds.
11304
11305 If the list of frame parameters PARAMS contains a `left' parameters,
11306 the tooltip is displayed at that x-position. Otherwise it is
11307 displayed at the mouse position, with offset DX added (default is 5 if
11308 DX isn't specified). Likewise for the y-position; if a `top' frame
11309 parameter is specified, it determines the y-position of the tooltip
11310 window, otherwise it is displayed at the mouse position, with offset
11311 DY added (default is -10).
11312
11313 A tooltip's maximum size is specified by `x-max-tooltip-size'.
11314 Text larger than the specified size is clipped. */)
11315 (string, frame, parms, timeout, dx, dy)
11316 Lisp_Object string, frame, parms, timeout, dx, dy;
11317 {
11318 struct frame *f;
11319 struct window *w;
11320 int root_x, root_y;
11321 struct buffer *old_buffer;
11322 struct text_pos pos;
11323 int i, width, height;
11324 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
11325 int old_windows_or_buffers_changed = windows_or_buffers_changed;
11326 int count = SPECPDL_INDEX ();
11327
11328 specbind (Qinhibit_redisplay, Qt);
11329
11330 GCPRO4 (string, parms, frame, timeout);
11331
11332 CHECK_STRING (string);
11333 f = check_x_frame (frame);
11334 if (NILP (timeout))
11335 timeout = make_number (5);
11336 else
11337 CHECK_NATNUM (timeout);
11338
11339 if (NILP (dx))
11340 dx = make_number (5);
11341 else
11342 CHECK_NUMBER (dx);
11343
11344 if (NILP (dy))
11345 dy = make_number (-10);
11346 else
11347 CHECK_NUMBER (dy);
11348
11349 if (NILP (last_show_tip_args))
11350 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
11351
11352 if (!NILP (tip_frame))
11353 {
11354 Lisp_Object last_string = AREF (last_show_tip_args, 0);
11355 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
11356 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
11357
11358 if (EQ (frame, last_frame)
11359 && !NILP (Fequal (last_string, string))
11360 && !NILP (Fequal (last_parms, parms)))
11361 {
11362 struct frame *f = XFRAME (tip_frame);
11363
11364 /* Only DX and DY have changed. */
11365 if (!NILP (tip_timer))
11366 {
11367 Lisp_Object timer = tip_timer;
11368 tip_timer = Qnil;
11369 call1 (Qcancel_timer, timer);
11370 }
11371
11372 BLOCK_INPUT;
11373 compute_tip_xy (f, parms, dx, dy, PIXEL_WIDTH (f),
11374 PIXEL_HEIGHT (f), &root_x, &root_y);
11375 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11376 root_x, root_y);
11377 UNBLOCK_INPUT;
11378 goto start_timer;
11379 }
11380 }
11381
11382 /* Hide a previous tip, if any. */
11383 Fx_hide_tip ();
11384
11385 ASET (last_show_tip_args, 0, string);
11386 ASET (last_show_tip_args, 1, frame);
11387 ASET (last_show_tip_args, 2, parms);
11388
11389 /* Add default values to frame parameters. */
11390 if (NILP (Fassq (Qname, parms)))
11391 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
11392 if (NILP (Fassq (Qinternal_border_width, parms)))
11393 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
11394 if (NILP (Fassq (Qborder_width, parms)))
11395 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
11396 if (NILP (Fassq (Qborder_color, parms)))
11397 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
11398 if (NILP (Fassq (Qbackground_color, parms)))
11399 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
11400 parms);
11401
11402 /* Create a frame for the tooltip, and record it in the global
11403 variable tip_frame. */
11404 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
11405 f = XFRAME (frame);
11406
11407 /* Set up the frame's root window. */
11408 w = XWINDOW (FRAME_ROOT_WINDOW (f));
11409 w->left = w->top = make_number (0);
11410
11411 if (CONSP (Vx_max_tooltip_size)
11412 && INTEGERP (XCAR (Vx_max_tooltip_size))
11413 && XINT (XCAR (Vx_max_tooltip_size)) > 0
11414 && INTEGERP (XCDR (Vx_max_tooltip_size))
11415 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
11416 {
11417 w->width = XCAR (Vx_max_tooltip_size);
11418 w->height = XCDR (Vx_max_tooltip_size);
11419 }
11420 else
11421 {
11422 w->width = make_number (80);
11423 w->height = make_number (40);
11424 }
11425
11426 f->window_width = XINT (w->width);
11427 adjust_glyphs (f);
11428 w->pseudo_window_p = 1;
11429
11430 /* Display the tooltip text in a temporary buffer. */
11431 old_buffer = current_buffer;
11432 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
11433 current_buffer->truncate_lines = Qnil;
11434 clear_glyph_matrix (w->desired_matrix);
11435 clear_glyph_matrix (w->current_matrix);
11436 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
11437 try_window (FRAME_ROOT_WINDOW (f), pos);
11438
11439 /* Compute width and height of the tooltip. */
11440 width = height = 0;
11441 for (i = 0; i < w->desired_matrix->nrows; ++i)
11442 {
11443 struct glyph_row *row = &w->desired_matrix->rows[i];
11444 struct glyph *last;
11445 int row_width;
11446
11447 /* Stop at the first empty row at the end. */
11448 if (!row->enabled_p || !row->displays_text_p)
11449 break;
11450
11451 /* Let the row go over the full width of the frame. */
11452 row->full_width_p = 1;
11453
11454 /* There's a glyph at the end of rows that is used to place
11455 the cursor there. Don't include the width of this glyph. */
11456 if (row->used[TEXT_AREA])
11457 {
11458 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
11459 row_width = row->pixel_width - last->pixel_width;
11460 }
11461 else
11462 row_width = row->pixel_width;
11463
11464 height += row->height;
11465 width = max (width, row_width);
11466 }
11467
11468 /* Add the frame's internal border to the width and height the X
11469 window should have. */
11470 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
11471 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
11472
11473 /* Move the tooltip window where the mouse pointer is. Resize and
11474 show it. */
11475 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
11476
11477 BLOCK_INPUT;
11478 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11479 root_x, root_y, width, height);
11480 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11481 UNBLOCK_INPUT;
11482
11483 /* Draw into the window. */
11484 w->must_be_updated_p = 1;
11485 update_single_window (w, 1);
11486
11487 /* Restore original current buffer. */
11488 set_buffer_internal_1 (old_buffer);
11489 windows_or_buffers_changed = old_windows_or_buffers_changed;
11490
11491 start_timer:
11492 /* Let the tip disappear after timeout seconds. */
11493 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
11494 intern ("x-hide-tip"));
11495
11496 UNGCPRO;
11497 return unbind_to (count, Qnil);
11498 }
11499
11500
11501 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
11502 doc: /* Hide the current tooltip window, if there is any.
11503 Value is t if tooltip was open, nil otherwise. */)
11504 ()
11505 {
11506 int count;
11507 Lisp_Object deleted, frame, timer;
11508 struct gcpro gcpro1, gcpro2;
11509
11510 /* Return quickly if nothing to do. */
11511 if (NILP (tip_timer) && NILP (tip_frame))
11512 return Qnil;
11513
11514 frame = tip_frame;
11515 timer = tip_timer;
11516 GCPRO2 (frame, timer);
11517 tip_frame = tip_timer = deleted = Qnil;
11518
11519 count = SPECPDL_INDEX ();
11520 specbind (Qinhibit_redisplay, Qt);
11521 specbind (Qinhibit_quit, Qt);
11522
11523 if (!NILP (timer))
11524 call1 (Qcancel_timer, timer);
11525
11526 if (FRAMEP (frame))
11527 {
11528 Fdelete_frame (frame, Qnil);
11529 deleted = Qt;
11530
11531 #ifdef USE_LUCID
11532 /* Bloodcurdling hack alert: The Lucid menu bar widget's
11533 redisplay procedure is not called when a tip frame over menu
11534 items is unmapped. Redisplay the menu manually... */
11535 {
11536 struct frame *f = SELECTED_FRAME ();
11537 Widget w = f->output_data.x->menubar_widget;
11538 extern void xlwmenu_redisplay P_ ((Widget));
11539
11540 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
11541 && w != NULL)
11542 {
11543 BLOCK_INPUT;
11544 xlwmenu_redisplay (w);
11545 UNBLOCK_INPUT;
11546 }
11547 }
11548 #endif /* USE_LUCID */
11549 }
11550
11551 UNGCPRO;
11552 return unbind_to (count, deleted);
11553 }
11554
11555
11556 \f
11557 /***********************************************************************
11558 File selection dialog
11559 ***********************************************************************/
11560
11561 #ifdef USE_MOTIF
11562
11563 /* Callback for "OK" and "Cancel" on file selection dialog. */
11564
11565 static void
11566 file_dialog_cb (widget, client_data, call_data)
11567 Widget widget;
11568 XtPointer call_data, client_data;
11569 {
11570 int *result = (int *) client_data;
11571 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
11572 *result = cb->reason;
11573 }
11574
11575
11576 /* Callback for unmapping a file selection dialog. This is used to
11577 capture the case where a dialog is closed via a window manager's
11578 closer button, for example. Using a XmNdestroyCallback didn't work
11579 in this case. */
11580
11581 static void
11582 file_dialog_unmap_cb (widget, client_data, call_data)
11583 Widget widget;
11584 XtPointer call_data, client_data;
11585 {
11586 int *result = (int *) client_data;
11587 *result = XmCR_CANCEL;
11588 }
11589
11590
11591 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
11592 doc: /* Read file name, prompting with PROMPT in directory DIR.
11593 Use a file selection dialog.
11594 Select DEFAULT-FILENAME in the dialog's file selection box, if
11595 specified. Don't let the user enter a file name in the file
11596 selection dialog's entry field, if MUSTMATCH is non-nil. */)
11597 (prompt, dir, default_filename, mustmatch)
11598 Lisp_Object prompt, dir, default_filename, mustmatch;
11599 {
11600 int result;
11601 struct frame *f = SELECTED_FRAME ();
11602 Lisp_Object file = Qnil;
11603 Widget dialog, text, list, help;
11604 Arg al[10];
11605 int ac = 0;
11606 extern XtAppContext Xt_app_con;
11607 XmString dir_xmstring, pattern_xmstring;
11608 int count = SPECPDL_INDEX ();
11609 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
11610
11611 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
11612 CHECK_STRING (prompt);
11613 CHECK_STRING (dir);
11614
11615 /* Prevent redisplay. */
11616 specbind (Qinhibit_redisplay, Qt);
11617
11618 BLOCK_INPUT;
11619
11620 /* Create the dialog with PROMPT as title, using DIR as initial
11621 directory and using "*" as pattern. */
11622 dir = Fexpand_file_name (dir, Qnil);
11623 dir_xmstring = XmStringCreateLocalized (SDATA (dir));
11624 pattern_xmstring = XmStringCreateLocalized ("*");
11625
11626 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
11627 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
11628 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
11629 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
11630 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
11631 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
11632 "fsb", al, ac);
11633 XmStringFree (dir_xmstring);
11634 XmStringFree (pattern_xmstring);
11635
11636 /* Add callbacks for OK and Cancel. */
11637 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
11638 (XtPointer) &result);
11639 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
11640 (XtPointer) &result);
11641 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
11642 (XtPointer) &result);
11643
11644 /* Disable the help button since we can't display help. */
11645 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
11646 XtSetSensitive (help, False);
11647
11648 /* Mark OK button as default. */
11649 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
11650 XmNshowAsDefault, True, NULL);
11651
11652 /* If MUSTMATCH is non-nil, disable the file entry field of the
11653 dialog, so that the user must select a file from the files list
11654 box. We can't remove it because we wouldn't have a way to get at
11655 the result file name, then. */
11656 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
11657 if (!NILP (mustmatch))
11658 {
11659 Widget label;
11660 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
11661 XtSetSensitive (text, False);
11662 XtSetSensitive (label, False);
11663 }
11664
11665 /* Manage the dialog, so that list boxes get filled. */
11666 XtManageChild (dialog);
11667
11668 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
11669 must include the path for this to work. */
11670 list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
11671 if (STRINGP (default_filename))
11672 {
11673 XmString default_xmstring;
11674 int item_pos;
11675
11676 default_xmstring
11677 = XmStringCreateLocalized (SDATA (default_filename));
11678
11679 if (!XmListItemExists (list, default_xmstring))
11680 {
11681 /* Add a new item if DEFAULT_FILENAME is not in the list. */
11682 XmListAddItem (list, default_xmstring, 0);
11683 item_pos = 0;
11684 }
11685 else
11686 item_pos = XmListItemPos (list, default_xmstring);
11687 XmStringFree (default_xmstring);
11688
11689 /* Select the item and scroll it into view. */
11690 XmListSelectPos (list, item_pos, True);
11691 XmListSetPos (list, item_pos);
11692 }
11693
11694 /* Process events until the user presses Cancel or OK. Block
11695 and unblock input here so that we get a chance of processing
11696 expose events. */
11697 UNBLOCK_INPUT;
11698 result = 0;
11699 while (result == 0)
11700 {
11701 BLOCK_INPUT;
11702 XtAppProcessEvent (Xt_app_con, XtIMAll);
11703 UNBLOCK_INPUT;
11704 }
11705 BLOCK_INPUT;
11706
11707 /* Get the result. */
11708 if (result == XmCR_OK)
11709 {
11710 XmString text;
11711 String data;
11712
11713 XtVaGetValues (dialog, XmNtextString, &text, NULL);
11714 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
11715 XmStringFree (text);
11716 file = build_string (data);
11717 XtFree (data);
11718 }
11719 else
11720 file = Qnil;
11721
11722 /* Clean up. */
11723 XtUnmanageChild (dialog);
11724 XtDestroyWidget (dialog);
11725 UNBLOCK_INPUT;
11726 UNGCPRO;
11727
11728 /* Make "Cancel" equivalent to C-g. */
11729 if (NILP (file))
11730 Fsignal (Qquit, Qnil);
11731
11732 return unbind_to (count, file);
11733 }
11734
11735 #endif /* USE_MOTIF */
11736
11737
11738 \f
11739 /***********************************************************************
11740 Keyboard
11741 ***********************************************************************/
11742
11743 #ifdef HAVE_XKBGETKEYBOARD
11744 #include <X11/XKBlib.h>
11745 #include <X11/keysym.h>
11746 #endif
11747
11748 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
11749 Sx_backspace_delete_keys_p, 0, 1, 0,
11750 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
11751 FRAME nil means use the selected frame.
11752 Value is t if we know that both keys are present, and are mapped to the
11753 usual X keysyms. */)
11754 (frame)
11755 Lisp_Object frame;
11756 {
11757 #ifdef HAVE_XKBGETKEYBOARD
11758 XkbDescPtr kb;
11759 struct frame *f = check_x_frame (frame);
11760 Display *dpy = FRAME_X_DISPLAY (f);
11761 Lisp_Object have_keys;
11762 int major, minor, op, event, error;
11763
11764 BLOCK_INPUT;
11765
11766 /* Check library version in case we're dynamically linked. */
11767 major = XkbMajorVersion;
11768 minor = XkbMinorVersion;
11769 if (!XkbLibraryVersion (&major, &minor))
11770 {
11771 UNBLOCK_INPUT;
11772 return Qnil;
11773 }
11774
11775 /* Check that the server supports XKB. */
11776 major = XkbMajorVersion;
11777 minor = XkbMinorVersion;
11778 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
11779 {
11780 UNBLOCK_INPUT;
11781 return Qnil;
11782 }
11783
11784 have_keys = Qnil;
11785 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
11786 if (kb)
11787 {
11788 int delete_keycode = 0, backspace_keycode = 0, i;
11789
11790 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
11791 {
11792 for (i = kb->min_key_code;
11793 (i < kb->max_key_code
11794 && (delete_keycode == 0 || backspace_keycode == 0));
11795 ++i)
11796 {
11797 /* The XKB symbolic key names can be seen most easily in
11798 the PS file generated by `xkbprint -label name
11799 $DISPLAY'. */
11800 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
11801 delete_keycode = i;
11802 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
11803 backspace_keycode = i;
11804 }
11805
11806 XkbFreeNames (kb, 0, True);
11807 }
11808
11809 XkbFreeClientMap (kb, 0, True);
11810
11811 if (delete_keycode
11812 && backspace_keycode
11813 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
11814 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
11815 have_keys = Qt;
11816 }
11817 UNBLOCK_INPUT;
11818 return have_keys;
11819 #else /* not HAVE_XKBGETKEYBOARD */
11820 return Qnil;
11821 #endif /* not HAVE_XKBGETKEYBOARD */
11822 }
11823
11824
11825 \f
11826 /***********************************************************************
11827 Initialization
11828 ***********************************************************************/
11829
11830 void
11831 syms_of_xfns ()
11832 {
11833 /* This is zero if not using X windows. */
11834 x_in_use = 0;
11835
11836 /* The section below is built by the lisp expression at the top of the file,
11837 just above where these variables are declared. */
11838 /*&&& init symbols here &&&*/
11839 Qauto_raise = intern ("auto-raise");
11840 staticpro (&Qauto_raise);
11841 Qauto_lower = intern ("auto-lower");
11842 staticpro (&Qauto_lower);
11843 Qbar = intern ("bar");
11844 staticpro (&Qbar);
11845 Qhbar = intern ("hbar");
11846 staticpro (&Qhbar);
11847 Qborder_color = intern ("border-color");
11848 staticpro (&Qborder_color);
11849 Qborder_width = intern ("border-width");
11850 staticpro (&Qborder_width);
11851 Qbox = intern ("box");
11852 staticpro (&Qbox);
11853 Qcursor_color = intern ("cursor-color");
11854 staticpro (&Qcursor_color);
11855 Qcursor_type = intern ("cursor-type");
11856 staticpro (&Qcursor_type);
11857 Qgeometry = intern ("geometry");
11858 staticpro (&Qgeometry);
11859 Qicon_left = intern ("icon-left");
11860 staticpro (&Qicon_left);
11861 Qicon_top = intern ("icon-top");
11862 staticpro (&Qicon_top);
11863 Qicon_type = intern ("icon-type");
11864 staticpro (&Qicon_type);
11865 Qicon_name = intern ("icon-name");
11866 staticpro (&Qicon_name);
11867 Qinternal_border_width = intern ("internal-border-width");
11868 staticpro (&Qinternal_border_width);
11869 Qleft = intern ("left");
11870 staticpro (&Qleft);
11871 Qright = intern ("right");
11872 staticpro (&Qright);
11873 Qmouse_color = intern ("mouse-color");
11874 staticpro (&Qmouse_color);
11875 Qnone = intern ("none");
11876 staticpro (&Qnone);
11877 Qparent_id = intern ("parent-id");
11878 staticpro (&Qparent_id);
11879 Qscroll_bar_width = intern ("scroll-bar-width");
11880 staticpro (&Qscroll_bar_width);
11881 Qsuppress_icon = intern ("suppress-icon");
11882 staticpro (&Qsuppress_icon);
11883 Qundefined_color = intern ("undefined-color");
11884 staticpro (&Qundefined_color);
11885 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
11886 staticpro (&Qvertical_scroll_bars);
11887 Qvisibility = intern ("visibility");
11888 staticpro (&Qvisibility);
11889 Qwindow_id = intern ("window-id");
11890 staticpro (&Qwindow_id);
11891 Qouter_window_id = intern ("outer-window-id");
11892 staticpro (&Qouter_window_id);
11893 Qx_frame_parameter = intern ("x-frame-parameter");
11894 staticpro (&Qx_frame_parameter);
11895 Qx_resource_name = intern ("x-resource-name");
11896 staticpro (&Qx_resource_name);
11897 Quser_position = intern ("user-position");
11898 staticpro (&Quser_position);
11899 Quser_size = intern ("user-size");
11900 staticpro (&Quser_size);
11901 Qscroll_bar_foreground = intern ("scroll-bar-foreground");
11902 staticpro (&Qscroll_bar_foreground);
11903 Qscroll_bar_background = intern ("scroll-bar-background");
11904 staticpro (&Qscroll_bar_background);
11905 Qscreen_gamma = intern ("screen-gamma");
11906 staticpro (&Qscreen_gamma);
11907 Qline_spacing = intern ("line-spacing");
11908 staticpro (&Qline_spacing);
11909 Qcenter = intern ("center");
11910 staticpro (&Qcenter);
11911 Qcompound_text = intern ("compound-text");
11912 staticpro (&Qcompound_text);
11913 Qcancel_timer = intern ("cancel-timer");
11914 staticpro (&Qcancel_timer);
11915 Qwait_for_wm = intern ("wait-for-wm");
11916 staticpro (&Qwait_for_wm);
11917 Qfullscreen = intern ("fullscreen");
11918 staticpro (&Qfullscreen);
11919 Qfullwidth = intern ("fullwidth");
11920 staticpro (&Qfullwidth);
11921 Qfullheight = intern ("fullheight");
11922 staticpro (&Qfullheight);
11923 Qfullboth = intern ("fullboth");
11924 staticpro (&Qfullboth);
11925 /* This is the end of symbol initialization. */
11926
11927 /* Text property `display' should be nonsticky by default. */
11928 Vtext_property_default_nonsticky
11929 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
11930
11931
11932 Qlaplace = intern ("laplace");
11933 staticpro (&Qlaplace);
11934 Qemboss = intern ("emboss");
11935 staticpro (&Qemboss);
11936 Qedge_detection = intern ("edge-detection");
11937 staticpro (&Qedge_detection);
11938 Qheuristic = intern ("heuristic");
11939 staticpro (&Qheuristic);
11940 QCmatrix = intern (":matrix");
11941 staticpro (&QCmatrix);
11942 QCcolor_adjustment = intern (":color-adjustment");
11943 staticpro (&QCcolor_adjustment);
11944 QCmask = intern (":mask");
11945 staticpro (&QCmask);
11946
11947 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
11948 staticpro (&Qface_set_after_frame_default);
11949
11950 Fput (Qundefined_color, Qerror_conditions,
11951 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
11952 Fput (Qundefined_color, Qerror_message,
11953 build_string ("Undefined color"));
11954
11955 init_x_parm_symbols ();
11956
11957 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
11958 doc: /* Non-nil means always draw a cross over disabled images.
11959 Disabled images are those having an `:conversion disabled' property.
11960 A cross is always drawn on black & white displays. */);
11961 cross_disabled_images = 0;
11962
11963 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
11964 doc: /* List of directories to search for bitmap files for X. */);
11965 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
11966
11967 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
11968 doc: /* The shape of the pointer when over text.
11969 Changing the value does not affect existing frames
11970 unless you set the mouse color. */);
11971 Vx_pointer_shape = Qnil;
11972
11973 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
11974 doc: /* The name Emacs uses to look up X resources.
11975 `x-get-resource' uses this as the first component of the instance name
11976 when requesting resource values.
11977 Emacs initially sets `x-resource-name' to the name under which Emacs
11978 was invoked, or to the value specified with the `-name' or `-rn'
11979 switches, if present.
11980
11981 It may be useful to bind this variable locally around a call
11982 to `x-get-resource'. See also the variable `x-resource-class'. */);
11983 Vx_resource_name = Qnil;
11984
11985 DEFVAR_LISP ("x-resource-class", &Vx_resource_class,
11986 doc: /* The class Emacs uses to look up X resources.
11987 `x-get-resource' uses this as the first component of the instance class
11988 when requesting resource values.
11989
11990 Emacs initially sets `x-resource-class' to "Emacs".
11991
11992 Setting this variable permanently is not a reasonable thing to do,
11993 but binding this variable locally around a call to `x-get-resource'
11994 is a reasonable practice. See also the variable `x-resource-name'. */);
11995 Vx_resource_class = build_string (EMACS_CLASS);
11996
11997 #if 0 /* This doesn't really do anything. */
11998 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
11999 doc: /* The shape of the pointer when not over text.
12000 This variable takes effect when you create a new frame
12001 or when you set the mouse color. */);
12002 #endif
12003 Vx_nontext_pointer_shape = Qnil;
12004
12005 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
12006 doc: /* The shape of the pointer when Emacs is busy.
12007 This variable takes effect when you create a new frame
12008 or when you set the mouse color. */);
12009 Vx_hourglass_pointer_shape = Qnil;
12010
12011 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
12012 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
12013 display_hourglass_p = 1;
12014
12015 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
12016 doc: /* *Seconds to wait before displaying an hourglass pointer.
12017 Value must be an integer or float. */);
12018 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
12019
12020 #if 0 /* This doesn't really do anything. */
12021 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
12022 doc: /* The shape of the pointer when over the mode line.
12023 This variable takes effect when you create a new frame
12024 or when you set the mouse color. */);
12025 #endif
12026 Vx_mode_pointer_shape = Qnil;
12027
12028 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
12029 &Vx_sensitive_text_pointer_shape,
12030 doc: /* The shape of the pointer when over mouse-sensitive text.
12031 This variable takes effect when you create a new frame
12032 or when you set the mouse color. */);
12033 Vx_sensitive_text_pointer_shape = Qnil;
12034
12035 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
12036 &Vx_window_horizontal_drag_shape,
12037 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
12038 This variable takes effect when you create a new frame
12039 or when you set the mouse color. */);
12040 Vx_window_horizontal_drag_shape = Qnil;
12041
12042 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
12043 doc: /* A string indicating the foreground color of the cursor box. */);
12044 Vx_cursor_fore_pixel = Qnil;
12045
12046 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
12047 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
12048 Text larger than this is clipped. */);
12049 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
12050
12051 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
12052 doc: /* Non-nil if no X window manager is in use.
12053 Emacs doesn't try to figure this out; this is always nil
12054 unless you set it to something else. */);
12055 /* We don't have any way to find this out, so set it to nil
12056 and maybe the user would like to set it to t. */
12057 Vx_no_window_manager = Qnil;
12058
12059 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
12060 &Vx_pixel_size_width_font_regexp,
12061 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
12062
12063 Since Emacs gets width of a font matching with this regexp from
12064 PIXEL_SIZE field of the name, font finding mechanism gets faster for
12065 such a font. This is especially effective for such large fonts as
12066 Chinese, Japanese, and Korean. */);
12067 Vx_pixel_size_width_font_regexp = Qnil;
12068
12069 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
12070 doc: /* Time after which cached images are removed from the cache.
12071 When an image has not been displayed this many seconds, remove it
12072 from the image cache. Value must be an integer or nil with nil
12073 meaning don't clear the cache. */);
12074 Vimage_cache_eviction_delay = make_number (30 * 60);
12075
12076 #ifdef USE_X_TOOLKIT
12077 Fprovide (intern ("x-toolkit"), Qnil);
12078 #ifdef USE_MOTIF
12079 Fprovide (intern ("motif"), Qnil);
12080
12081 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
12082 doc: /* Version info for LessTif/Motif. */);
12083 Vmotif_version_string = build_string (XmVERSION_STRING);
12084 #endif /* USE_MOTIF */
12085 #endif /* USE_X_TOOLKIT */
12086
12087 defsubr (&Sx_get_resource);
12088
12089 /* X window properties. */
12090 defsubr (&Sx_change_window_property);
12091 defsubr (&Sx_delete_window_property);
12092 defsubr (&Sx_window_property);
12093
12094 defsubr (&Sxw_display_color_p);
12095 defsubr (&Sx_display_grayscale_p);
12096 defsubr (&Sxw_color_defined_p);
12097 defsubr (&Sxw_color_values);
12098 defsubr (&Sx_server_max_request_size);
12099 defsubr (&Sx_server_vendor);
12100 defsubr (&Sx_server_version);
12101 defsubr (&Sx_display_pixel_width);
12102 defsubr (&Sx_display_pixel_height);
12103 defsubr (&Sx_display_mm_width);
12104 defsubr (&Sx_display_mm_height);
12105 defsubr (&Sx_display_screens);
12106 defsubr (&Sx_display_planes);
12107 defsubr (&Sx_display_color_cells);
12108 defsubr (&Sx_display_visual_class);
12109 defsubr (&Sx_display_backing_store);
12110 defsubr (&Sx_display_save_under);
12111 defsubr (&Sx_parse_geometry);
12112 defsubr (&Sx_create_frame);
12113 defsubr (&Sx_open_connection);
12114 defsubr (&Sx_close_connection);
12115 defsubr (&Sx_display_list);
12116 defsubr (&Sx_synchronize);
12117 defsubr (&Sx_focus_frame);
12118 defsubr (&Sx_backspace_delete_keys_p);
12119
12120 /* Setting callback functions for fontset handler. */
12121 get_font_info_func = x_get_font_info;
12122
12123 #if 0 /* This function pointer doesn't seem to be used anywhere.
12124 And the pointer assigned has the wrong type, anyway. */
12125 list_fonts_func = x_list_fonts;
12126 #endif
12127
12128 load_font_func = x_load_font;
12129 find_ccl_program_func = x_find_ccl_program;
12130 query_font_func = x_query_font;
12131 set_frame_fontset_func = x_set_font;
12132 check_window_system_func = check_x;
12133
12134 /* Images. */
12135 Qxbm = intern ("xbm");
12136 staticpro (&Qxbm);
12137 QCconversion = intern (":conversion");
12138 staticpro (&QCconversion);
12139 QCheuristic_mask = intern (":heuristic-mask");
12140 staticpro (&QCheuristic_mask);
12141 QCcolor_symbols = intern (":color-symbols");
12142 staticpro (&QCcolor_symbols);
12143 QCascent = intern (":ascent");
12144 staticpro (&QCascent);
12145 QCmargin = intern (":margin");
12146 staticpro (&QCmargin);
12147 QCrelief = intern (":relief");
12148 staticpro (&QCrelief);
12149 Qpostscript = intern ("postscript");
12150 staticpro (&Qpostscript);
12151 QCloader = intern (":loader");
12152 staticpro (&QCloader);
12153 QCbounding_box = intern (":bounding-box");
12154 staticpro (&QCbounding_box);
12155 QCpt_width = intern (":pt-width");
12156 staticpro (&QCpt_width);
12157 QCpt_height = intern (":pt-height");
12158 staticpro (&QCpt_height);
12159 QCindex = intern (":index");
12160 staticpro (&QCindex);
12161 Qpbm = intern ("pbm");
12162 staticpro (&Qpbm);
12163
12164 #if HAVE_XPM
12165 Qxpm = intern ("xpm");
12166 staticpro (&Qxpm);
12167 #endif
12168
12169 #if HAVE_JPEG
12170 Qjpeg = intern ("jpeg");
12171 staticpro (&Qjpeg);
12172 #endif
12173
12174 #if HAVE_TIFF
12175 Qtiff = intern ("tiff");
12176 staticpro (&Qtiff);
12177 #endif
12178
12179 #if HAVE_GIF
12180 Qgif = intern ("gif");
12181 staticpro (&Qgif);
12182 #endif
12183
12184 #if HAVE_PNG
12185 Qpng = intern ("png");
12186 staticpro (&Qpng);
12187 #endif
12188
12189 defsubr (&Sclear_image_cache);
12190 defsubr (&Simage_size);
12191 defsubr (&Simage_mask_p);
12192
12193 hourglass_atimer = NULL;
12194 hourglass_shown_p = 0;
12195
12196 defsubr (&Sx_show_tip);
12197 defsubr (&Sx_hide_tip);
12198 tip_timer = Qnil;
12199 staticpro (&tip_timer);
12200 tip_frame = Qnil;
12201 staticpro (&tip_frame);
12202
12203 last_show_tip_args = Qnil;
12204 staticpro (&last_show_tip_args);
12205
12206 #ifdef USE_MOTIF
12207 defsubr (&Sx_file_dialog);
12208 #endif
12209 }
12210
12211
12212 void
12213 init_xfns ()
12214 {
12215 image_types = NULL;
12216 Vimage_types = Qnil;
12217
12218 define_image_type (&xbm_type);
12219 define_image_type (&gs_type);
12220 define_image_type (&pbm_type);
12221
12222 #if HAVE_XPM
12223 define_image_type (&xpm_type);
12224 #endif
12225
12226 #if HAVE_JPEG
12227 define_image_type (&jpeg_type);
12228 #endif
12229
12230 #if HAVE_TIFF
12231 define_image_type (&tiff_type);
12232 #endif
12233
12234 #if HAVE_GIF
12235 define_image_type (&gif_type);
12236 #endif
12237
12238 #if HAVE_PNG
12239 define_image_type (&png_type);
12240 #endif
12241 }
12242
12243 #endif /* HAVE_X_WINDOWS */