1 /* Functions for the X window system.
2 Copyright (C) 1989, 92, 93, 94, 95, 96, 97, 98, 99, 2000, 01, 02, 03
3 Free Software Foundation.
5 This file is part of GNU Emacs.
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)
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.
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. */
31 /* This makes the fields of a Display accessible, in Xlib header files. */
33 #define XLIB_ILLEGAL_ACCESS
40 #include "intervals.h"
41 #include "dispextern.h"
43 #include "blockinput.h"
49 #include "termhooks.h"
55 #include <sys/types.h>
59 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
60 #include "bitmaps/gray.xbm"
62 #include <X11/bitmaps/gray>
65 #include "[.bitmaps]gray.xbm"
73 #include <X11/Shell.h>
76 #include <X11/Xaw/Paned.h>
77 #include <X11/Xaw/Label.h>
78 #endif /* USE_MOTIF */
81 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
90 #include "../lwlib/lwlib.h"
94 #include <Xm/DialogS.h>
95 #include <Xm/FileSB.h>
98 /* Do the EDITRES protocol if running X11R5
99 Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
101 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
103 extern void _XEditResCheckMessages ();
104 #endif /* R5 + Athena */
106 /* Unique id counter for widgets created by the Lucid Widget Library. */
108 extern LWLIB_ID widget_id_tick
;
111 /* This is part of a kludge--see lwlib/xlwmenu.c. */
112 extern XFontStruct
*xlwmenu_default_font
;
115 extern void free_frame_menubar ();
116 extern double atof ();
120 /* LessTif/Motif version info. */
122 static Lisp_Object Vmotif_version_string
;
124 #endif /* USE_MOTIF */
126 #endif /* USE_X_TOOLKIT */
130 /* GTK+ version info */
132 static Lisp_Object Vgtk_version_string
;
137 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
139 #define MAXREQUEST(dpy) ((dpy)->max_request_size)
142 /* The gray bitmap `bitmaps/gray'. This is done because xterm.c uses
143 it, and including `bitmaps/gray' more than once is a problem when
144 config.h defines `static' as an empty replacement string. */
146 int gray_bitmap_width
= gray_width
;
147 int gray_bitmap_height
= gray_height
;
148 char *gray_bitmap_bits
= gray_bits
;
150 /* Non-zero means we're allowed to display an hourglass cursor. */
152 int display_hourglass_p
;
154 /* The background and shape of the mouse pointer, and shape when not
155 over text or in the modeline. */
157 Lisp_Object Vx_pointer_shape
, Vx_nontext_pointer_shape
, Vx_mode_pointer_shape
;
158 Lisp_Object Vx_hourglass_pointer_shape
;
160 /* The shape when over mouse-sensitive text. */
162 Lisp_Object Vx_sensitive_text_pointer_shape
;
164 /* If non-nil, the pointer shape to indicate that windows can be
165 dragged horizontally. */
167 Lisp_Object Vx_window_horizontal_drag_shape
;
169 /* Color of chars displayed in cursor box. */
171 Lisp_Object Vx_cursor_fore_pixel
;
173 /* Nonzero if using X. */
177 /* Non nil if no window manager is in use. */
179 Lisp_Object Vx_no_window_manager
;
181 /* Search path for bitmap files. */
183 Lisp_Object Vx_bitmap_file_path
;
185 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
187 Lisp_Object Vx_pixel_size_width_font_regexp
;
190 Lisp_Object Qsuppress_icon
;
191 Lisp_Object Qundefined_color
;
193 Lisp_Object Qcompound_text
, Qcancel_timer
;
197 extern Lisp_Object Vwindow_system_version
;
199 /* The below are defined in frame.c. */
202 int image_cache_refcount
, dpyinfo_refcount
;
207 /* Error if we are not connected to X. */
213 error ("X windows are not in use or not initialized");
216 /* Nonzero if we can use mouse menus.
217 You should not call this unless HAVE_MENUS is defined. */
225 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
226 and checking validity for X. */
229 check_x_frame (frame
)
235 frame
= selected_frame
;
236 CHECK_LIVE_FRAME (frame
);
239 error ("Non-X frame used");
243 /* Let the user specify an X display with a frame.
244 nil stands for the selected frame--or, if that is not an X frame,
245 the first X display on the list. */
247 struct x_display_info
*
248 check_x_display_info (frame
)
251 struct x_display_info
*dpyinfo
= NULL
;
255 struct frame
*sf
= XFRAME (selected_frame
);
257 if (FRAME_X_P (sf
) && FRAME_LIVE_P (sf
))
258 dpyinfo
= FRAME_X_DISPLAY_INFO (sf
);
259 else if (x_display_list
!= 0)
260 dpyinfo
= x_display_list
;
262 error ("X windows are not in use or not initialized");
264 else if (STRINGP (frame
))
265 dpyinfo
= x_display_info_for_name (frame
);
268 FRAME_PTR f
= check_x_frame (frame
);
269 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
276 /* Return the Emacs frame-object corresponding to an X window.
277 It could be the frame's main window or an icon window. */
279 /* This function can be called during GC, so use GC_xxx type test macros. */
282 x_window_to_frame (dpyinfo
, wdesc
)
283 struct x_display_info
*dpyinfo
;
286 Lisp_Object tail
, frame
;
289 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
292 if (!GC_FRAMEP (frame
))
295 if (!FRAME_X_P (f
) || FRAME_X_DISPLAY_INFO (f
) != dpyinfo
)
297 if (f
->output_data
.x
->hourglass_window
== wdesc
)
300 if ((f
->output_data
.x
->edit_widget
301 && XtWindow (f
->output_data
.x
->edit_widget
) == wdesc
)
302 /* A tooltip frame? */
303 || (!f
->output_data
.x
->edit_widget
304 && FRAME_X_WINDOW (f
) == wdesc
)
305 || f
->output_data
.x
->icon_desc
== wdesc
)
307 #else /* not USE_X_TOOLKIT */
309 if (f
->output_data
.x
->edit_widget
)
311 GtkWidget
*gwdesc
= xg_win_to_widget (dpyinfo
->display
, wdesc
);
312 struct x_output
*x
= f
->output_data
.x
;
313 if (gwdesc
!= 0 && gwdesc
== x
->edit_widget
)
317 if (FRAME_X_WINDOW (f
) == wdesc
318 || f
->output_data
.x
->icon_desc
== wdesc
)
320 #endif /* not USE_X_TOOLKIT */
325 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
326 /* Like x_window_to_frame but also compares the window with the widget's
330 x_any_window_to_frame (dpyinfo
, wdesc
)
331 struct x_display_info
*dpyinfo
;
334 Lisp_Object tail
, frame
;
335 struct frame
*f
, *found
;
339 for (tail
= Vframe_list
; GC_CONSP (tail
) && !found
; tail
= XCDR (tail
))
342 if (!GC_FRAMEP (frame
))
346 if (FRAME_X_P (f
) && FRAME_X_DISPLAY_INFO (f
) == dpyinfo
)
348 /* This frame matches if the window is any of its widgets. */
349 x
= f
->output_data
.x
;
350 if (x
->hourglass_window
== wdesc
)
355 GtkWidget
*gwdesc
= xg_win_to_widget (dpyinfo
->display
, wdesc
);
357 && (gwdesc
== x
->widget
358 || gwdesc
== x
->edit_widget
359 || gwdesc
== x
->vbox_widget
360 || gwdesc
== x
->menubar_widget
))
363 if (wdesc
== XtWindow (x
->widget
)
364 || wdesc
== XtWindow (x
->column_widget
)
365 || wdesc
== XtWindow (x
->edit_widget
))
367 /* Match if the window is this frame's menubar. */
368 else if (lw_window_is_in_menubar (wdesc
, x
->menubar_widget
))
372 else if (FRAME_X_WINDOW (f
) == wdesc
)
373 /* A tooltip frame. */
381 /* Likewise, but exclude the menu bar widget. */
384 x_non_menubar_window_to_frame (dpyinfo
, wdesc
)
385 struct x_display_info
*dpyinfo
;
388 Lisp_Object tail
, frame
;
392 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
395 if (!GC_FRAMEP (frame
))
398 if (!FRAME_X_P (f
) || FRAME_X_DISPLAY_INFO (f
) != dpyinfo
)
400 x
= f
->output_data
.x
;
401 /* This frame matches if the window is any of its widgets. */
402 if (x
->hourglass_window
== wdesc
)
407 GtkWidget
*gwdesc
= xg_win_to_widget (dpyinfo
->display
, wdesc
);
409 && (gwdesc
== x
->widget
410 || gwdesc
== x
->edit_widget
411 || gwdesc
== x
->vbox_widget
))
414 if (wdesc
== XtWindow (x
->widget
)
415 || wdesc
== XtWindow (x
->column_widget
)
416 || wdesc
== XtWindow (x
->edit_widget
))
420 else if (FRAME_X_WINDOW (f
) == wdesc
)
421 /* A tooltip frame. */
427 /* Likewise, but consider only the menu bar widget. */
430 x_menubar_window_to_frame (dpyinfo
, wdesc
)
431 struct x_display_info
*dpyinfo
;
434 Lisp_Object tail
, frame
;
438 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
441 if (!GC_FRAMEP (frame
))
444 if (!FRAME_X_P (f
) || FRAME_X_DISPLAY_INFO (f
) != dpyinfo
)
446 x
= f
->output_data
.x
;
447 /* Match if the window is this frame's menubar. */
449 if (x
->menubar_widget
)
451 GtkWidget
*gwdesc
= xg_win_to_widget (dpyinfo
->display
, wdesc
);
456 && (gwdesc
== x
->menubar_widget
457 || gtk_widget_get_parent (gwdesc
) == x
->menubar_widget
))
463 if (x
->menubar_widget
464 && lw_window_is_in_menubar (wdesc
, x
->menubar_widget
))
471 /* Return the frame whose principal (outermost) window is WDESC.
472 If WDESC is some other (smaller) window, we return 0. */
475 x_top_window_to_frame (dpyinfo
, wdesc
)
476 struct x_display_info
*dpyinfo
;
479 Lisp_Object tail
, frame
;
483 for (tail
= Vframe_list
; GC_CONSP (tail
); tail
= XCDR (tail
))
486 if (!GC_FRAMEP (frame
))
489 if (!FRAME_X_P (f
) || FRAME_X_DISPLAY_INFO (f
) != dpyinfo
)
491 x
= f
->output_data
.x
;
495 /* This frame matches if the window is its topmost widget. */
497 GtkWidget
*gwdesc
= xg_win_to_widget (dpyinfo
->display
, wdesc
);
498 if (gwdesc
== x
->widget
)
501 if (wdesc
== XtWindow (x
->widget
))
503 #if 0 /* I don't know why it did this,
504 but it seems logically wrong,
505 and it causes trouble for MapNotify events. */
506 /* Match if the window is this frame's menubar. */
507 if (x
->menubar_widget
508 && wdesc
== XtWindow (x
->menubar_widget
))
513 else if (FRAME_X_WINDOW (f
) == wdesc
)
519 #endif /* USE_X_TOOLKIT || USE_GTK */
523 /* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
524 id, which is just an int that this section returns. Bitmaps are
525 reference counted so they can be shared among frames.
527 Bitmap indices are guaranteed to be > 0, so a negative number can
528 be used to indicate no bitmap.
530 If you use x_create_bitmap_from_data, then you must keep track of
531 the bitmaps yourself. That is, creating a bitmap from the same
532 data more than once will not be caught. */
535 /* Functions to access the contents of a bitmap, given an id. */
538 x_bitmap_height (f
, id
)
542 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].height
;
546 x_bitmap_width (f
, id
)
550 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].width
;
554 x_bitmap_pixmap (f
, id
)
558 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].pixmap
;
562 x_bitmap_mask (f
, id
)
566 return FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].mask
;
570 /* Allocate a new bitmap record. Returns index of new record. */
573 x_allocate_bitmap_record (f
)
576 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
579 if (dpyinfo
->bitmaps
== NULL
)
581 dpyinfo
->bitmaps_size
= 10;
583 = (struct x_bitmap_record
*) xmalloc (dpyinfo
->bitmaps_size
* sizeof (struct x_bitmap_record
));
584 dpyinfo
->bitmaps_last
= 1;
588 if (dpyinfo
->bitmaps_last
< dpyinfo
->bitmaps_size
)
589 return ++dpyinfo
->bitmaps_last
;
591 for (i
= 0; i
< dpyinfo
->bitmaps_size
; ++i
)
592 if (dpyinfo
->bitmaps
[i
].refcount
== 0)
595 dpyinfo
->bitmaps_size
*= 2;
597 = (struct x_bitmap_record
*) xrealloc (dpyinfo
->bitmaps
,
598 dpyinfo
->bitmaps_size
* sizeof (struct x_bitmap_record
));
599 return ++dpyinfo
->bitmaps_last
;
602 /* Add one reference to the reference count of the bitmap with id ID. */
605 x_reference_bitmap (f
, id
)
609 ++FRAME_X_DISPLAY_INFO (f
)->bitmaps
[id
- 1].refcount
;
612 /* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
615 x_create_bitmap_from_data (f
, bits
, width
, height
)
618 unsigned int width
, height
;
620 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
624 bitmap
= XCreateBitmapFromData (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
625 bits
, width
, height
);
632 id
= x_allocate_bitmap_record (f
);
633 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
634 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
635 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
636 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
637 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
638 dpyinfo
->bitmaps
[id
- 1].height
= height
;
639 dpyinfo
->bitmaps
[id
- 1].width
= width
;
644 /* Create bitmap from file FILE for frame F. */
647 x_create_bitmap_from_file (f
, file
)
651 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
652 unsigned int width
, height
;
654 int xhot
, yhot
, result
, id
;
659 /* Look for an existing bitmap with the same name. */
660 for (id
= 0; id
< dpyinfo
->bitmaps_last
; ++id
)
662 if (dpyinfo
->bitmaps
[id
].refcount
663 && dpyinfo
->bitmaps
[id
].file
664 && !strcmp (dpyinfo
->bitmaps
[id
].file
, (char *) SDATA (file
)))
666 ++dpyinfo
->bitmaps
[id
].refcount
;
671 /* Search bitmap-file-path for the file, if appropriate. */
672 fd
= openp (Vx_bitmap_file_path
, file
, Qnil
, &found
, Qnil
);
677 filename
= (char *) SDATA (found
);
679 result
= XReadBitmapFile (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
680 filename
, &width
, &height
, &bitmap
, &xhot
, &yhot
);
681 if (result
!= BitmapSuccess
)
684 id
= x_allocate_bitmap_record (f
);
685 dpyinfo
->bitmaps
[id
- 1].pixmap
= bitmap
;
686 dpyinfo
->bitmaps
[id
- 1].have_mask
= 0;
687 dpyinfo
->bitmaps
[id
- 1].refcount
= 1;
688 dpyinfo
->bitmaps
[id
- 1].file
689 = (char *) xmalloc (SBYTES (file
) + 1);
690 dpyinfo
->bitmaps
[id
- 1].depth
= 1;
691 dpyinfo
->bitmaps
[id
- 1].height
= height
;
692 dpyinfo
->bitmaps
[id
- 1].width
= width
;
693 strcpy (dpyinfo
->bitmaps
[id
- 1].file
, SDATA (file
));
698 /* Remove reference to bitmap with id number ID. */
701 x_destroy_bitmap (f
, id
)
705 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
709 --dpyinfo
->bitmaps
[id
- 1].refcount
;
710 if (dpyinfo
->bitmaps
[id
- 1].refcount
== 0)
713 XFreePixmap (FRAME_X_DISPLAY (f
), dpyinfo
->bitmaps
[id
- 1].pixmap
);
714 if (dpyinfo
->bitmaps
[id
- 1].have_mask
)
715 XFreePixmap (FRAME_X_DISPLAY (f
), dpyinfo
->bitmaps
[id
- 1].mask
);
716 if (dpyinfo
->bitmaps
[id
- 1].file
)
718 xfree (dpyinfo
->bitmaps
[id
- 1].file
);
719 dpyinfo
->bitmaps
[id
- 1].file
= NULL
;
726 /* Free all the bitmaps for the display specified by DPYINFO. */
729 x_destroy_all_bitmaps (dpyinfo
)
730 struct x_display_info
*dpyinfo
;
733 for (i
= 0; i
< dpyinfo
->bitmaps_last
; i
++)
734 if (dpyinfo
->bitmaps
[i
].refcount
> 0)
736 XFreePixmap (dpyinfo
->display
, dpyinfo
->bitmaps
[i
].pixmap
);
737 if (dpyinfo
->bitmaps
[i
].have_mask
)
738 XFreePixmap (dpyinfo
->display
, dpyinfo
->bitmaps
[i
].mask
);
739 if (dpyinfo
->bitmaps
[i
].file
)
740 xfree (dpyinfo
->bitmaps
[i
].file
);
742 dpyinfo
->bitmaps_last
= 0;
748 /* Useful functions defined in the section
749 `Image type independent image structures' below. */
751 static unsigned long four_corners_best
P_ ((XImage
*ximg
, unsigned long width
,
752 unsigned long height
));
754 static int x_create_x_image_and_pixmap
P_ ((struct frame
*f
, int width
, int height
,
755 int depth
, XImage
**ximg
,
758 static void x_destroy_x_image
P_ ((XImage
*ximg
));
761 /* Create a mask of a bitmap. Note is this not a perfect mask.
762 It's nicer with some borders in this context */
765 x_create_bitmap_mask (f
, id
)
770 XImage
*ximg
, *mask_img
;
771 unsigned long width
, height
;
774 unsigned long x
, y
, xp
, xm
, yp
, ym
;
777 int depth
= DefaultDepthOfScreen (FRAME_X_SCREEN (f
));
778 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
783 pixmap
= x_bitmap_pixmap (f
, id
);
784 width
= x_bitmap_width (f
, id
);
785 height
= x_bitmap_height (f
, id
);
788 ximg
= XGetImage (FRAME_X_DISPLAY (f
), pixmap
, 0, 0, width
, height
,
797 result
= x_create_x_image_and_pixmap (f
, width
, height
, 1, &mask_img
, &mask
);
802 XDestroyImage (ximg
);
806 bg
= four_corners_best (ximg
, width
, height
);
808 for (y
= 0; y
< ximg
->height
; ++y
)
810 for (x
= 0; x
< ximg
->width
; ++x
)
812 xp
= x
!= ximg
->width
- 1 ? x
+ 1 : 0;
813 xm
= x
!= 0 ? x
- 1 : ximg
->width
- 1;
814 yp
= y
!= ximg
->height
- 1 ? y
+ 1 : 0;
815 ym
= y
!= 0 ? y
- 1 : ximg
->height
- 1;
816 if (XGetPixel (ximg
, x
, y
) == bg
817 && XGetPixel (ximg
, x
, yp
) == bg
818 && XGetPixel (ximg
, x
, ym
) == bg
819 && XGetPixel (ximg
, xp
, y
) == bg
820 && XGetPixel (ximg
, xp
, yp
) == bg
821 && XGetPixel (ximg
, xp
, ym
) == bg
822 && XGetPixel (ximg
, xm
, y
) == bg
823 && XGetPixel (ximg
, xm
, yp
) == bg
824 && XGetPixel (ximg
, xm
, ym
) == bg
)
825 XPutPixel (mask_img
, x
, y
, 0);
827 XPutPixel (mask_img
, x
, y
, 1);
831 xassert (interrupt_input_blocked
);
832 gc
= XCreateGC (FRAME_X_DISPLAY (f
), mask
, 0, NULL
);
833 XPutImage (FRAME_X_DISPLAY (f
), mask
, gc
, mask_img
, 0, 0, 0, 0,
835 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
837 dpyinfo
->bitmaps
[id
- 1].have_mask
= 1;
838 dpyinfo
->bitmaps
[id
- 1].mask
= mask
;
840 XDestroyImage (ximg
);
841 x_destroy_x_image (mask_img
);
846 static Lisp_Object unwind_create_frame
P_ ((Lisp_Object
));
847 static Lisp_Object unwind_create_tip_frame
P_ ((Lisp_Object
));
848 static void x_disable_image
P_ ((struct frame
*, struct image
*));
850 void x_set_foreground_color
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
851 static void x_set_wait_for_wm
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
852 void x_set_background_color
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
853 void x_set_mouse_color
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
854 void x_set_cursor_color
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
855 void x_set_border_color
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
856 void x_set_cursor_type
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
857 void x_set_icon_type
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
858 void x_set_icon_name
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
859 void x_explicitly_set_name
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
860 void x_set_menu_bar_lines
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
861 void x_set_title
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
862 void x_set_tool_bar_lines
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
));
863 void x_set_scroll_bar_foreground
P_ ((struct frame
*, Lisp_Object
,
865 void x_set_scroll_bar_background
P_ ((struct frame
*, Lisp_Object
,
867 static Lisp_Object x_default_scroll_bar_color_parameter
P_ ((struct frame
*,
872 static void x_edge_detection
P_ ((struct frame
*, struct image
*, Lisp_Object
,
874 static void init_color_table
P_ ((void));
875 static void free_color_table
P_ ((void));
876 static unsigned long *colors_in_color_table
P_ ((int *n
));
877 static unsigned long lookup_rgb_color
P_ ((struct frame
*f
, int r
, int g
, int b
));
878 static unsigned long lookup_pixel_color
P_ ((struct frame
*f
, unsigned long p
));
884 /* Store the screen positions of frame F into XPTR and YPTR.
885 These are the positions of the containing window manager window,
886 not Emacs's own window. */
889 x_real_positions (f
, xptr
, yptr
)
893 int win_x
, win_y
, outer_x
, outer_y
;
894 int real_x
= 0, real_y
= 0;
896 Window win
= f
->output_data
.x
->parent_desc
;
902 count
= x_catch_errors (FRAME_X_DISPLAY (f
));
904 if (win
== FRAME_X_DISPLAY_INFO (f
)->root_window
)
905 win
= FRAME_OUTER_WINDOW (f
);
907 /* This loop traverses up the containment tree until we hit the root
908 window. Window managers may intersect many windows between our window
909 and the root window. The window we find just before the root window
910 should be the outer WM window. */
913 Window wm_window
, rootw
;
914 Window
*tmp_children
;
915 unsigned int tmp_nchildren
;
918 success
= XQueryTree (FRAME_X_DISPLAY (f
), win
, &rootw
,
919 &wm_window
, &tmp_children
, &tmp_nchildren
);
921 had_errors
= x_had_errors_p (FRAME_X_DISPLAY (f
));
923 /* Don't free tmp_children if XQueryTree failed. */
927 XFree ((char *) tmp_children
);
929 if (wm_window
== rootw
|| had_errors
)
940 /* Get the real coordinates for the WM window upper left corner */
941 XGetGeometry (FRAME_X_DISPLAY (f
), win
,
942 &rootw
, &real_x
, &real_y
, &ign
, &ign
, &ign
, &ign
);
944 /* Translate real coordinates to coordinates relative to our
945 window. For our window, the upper left corner is 0, 0.
946 Since the upper left corner of the WM window is outside
947 our window, win_x and win_y will be negative:
949 ------------------ ---> x
951 | ----------------- v y
954 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
956 /* From-window, to-window. */
957 FRAME_X_DISPLAY_INFO (f
)->root_window
,
960 /* From-position, to-position. */
961 real_x
, real_y
, &win_x
, &win_y
,
966 if (FRAME_X_WINDOW (f
) == FRAME_OUTER_WINDOW (f
))
973 XTranslateCoordinates (FRAME_X_DISPLAY (f
),
975 /* From-window, to-window. */
976 FRAME_X_DISPLAY_INFO (f
)->root_window
,
977 FRAME_OUTER_WINDOW (f
),
979 /* From-position, to-position. */
980 real_x
, real_y
, &outer_x
, &outer_y
,
986 had_errors
= x_had_errors_p (FRAME_X_DISPLAY (f
));
989 x_uncatch_errors (FRAME_X_DISPLAY (f
), count
);
993 if (had_errors
) return;
995 f
->x_pixels_diff
= -win_x
;
996 f
->y_pixels_diff
= -win_y
;
998 FRAME_X_OUTPUT (f
)->x_pixels_outer_diff
= -outer_x
;
999 FRAME_X_OUTPUT (f
)->y_pixels_outer_diff
= -outer_y
;
1008 /* Gamma-correct COLOR on frame F. */
1011 gamma_correct (f
, color
)
1017 color
->red
= pow (color
->red
/ 65535.0, f
->gamma
) * 65535.0 + 0.5;
1018 color
->green
= pow (color
->green
/ 65535.0, f
->gamma
) * 65535.0 + 0.5;
1019 color
->blue
= pow (color
->blue
/ 65535.0, f
->gamma
) * 65535.0 + 0.5;
1024 /* Decide if color named COLOR_NAME is valid for use on frame F. If
1025 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
1026 allocate the color. Value is zero if COLOR_NAME is invalid, or
1027 no color could be allocated. */
1030 x_defined_color (f
, color_name
, color
, alloc_p
)
1037 Display
*dpy
= FRAME_X_DISPLAY (f
);
1038 Colormap cmap
= FRAME_X_COLORMAP (f
);
1041 success_p
= XParseColor (dpy
, cmap
, color_name
, color
);
1042 if (success_p
&& alloc_p
)
1043 success_p
= x_alloc_nearest_color (f
, cmap
, color
);
1050 /* Return the pixel color value for color COLOR_NAME on frame F. If F
1051 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
1052 Signal an error if color can't be allocated. */
1055 x_decode_color (f
, color_name
, mono_color
)
1057 Lisp_Object color_name
;
1062 CHECK_STRING (color_name
);
1064 #if 0 /* Don't do this. It's wrong when we're not using the default
1065 colormap, it makes freeing difficult, and it's probably not
1066 an important optimization. */
1067 if (strcmp (SDATA (color_name
), "black") == 0)
1068 return BLACK_PIX_DEFAULT (f
);
1069 else if (strcmp (SDATA (color_name
), "white") == 0)
1070 return WHITE_PIX_DEFAULT (f
);
1073 /* Return MONO_COLOR for monochrome frames. */
1074 if (FRAME_X_DISPLAY_INFO (f
)->n_planes
== 1)
1077 /* x_defined_color is responsible for coping with failures
1078 by looking for a near-miss. */
1079 if (x_defined_color (f
, SDATA (color_name
), &cdef
, 1))
1082 Fsignal (Qerror
, Fcons (build_string ("Undefined color"),
1083 Fcons (color_name
, Qnil
)));
1089 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
1090 the previous value of that parameter, NEW_VALUE is the new value.
1091 See also the comment of wait_for_wm in struct x_output. */
1094 x_set_wait_for_wm (f
, new_value
, old_value
)
1096 Lisp_Object new_value
, old_value
;
1098 f
->output_data
.x
->wait_for_wm
= !NILP (new_value
);
1103 static Lisp_Object x_find_image_file
P_ ((Lisp_Object file
));
1105 /* Set icon from FILE for frame F. By using GTK functions the icon
1106 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
1109 xg_set_icon (f
, file
)
1113 struct gcpro gcpro1
;
1119 found
= x_find_image_file (file
);
1127 filename
= SDATA (found
);
1130 pixbuf
= gdk_pixbuf_new_from_file (filename
, &err
);
1134 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
1136 g_object_unref (pixbuf
);
1149 #endif /* USE_GTK */
1152 /* Functions called only from `x_set_frame_param'
1153 to set individual parameters.
1155 If FRAME_X_WINDOW (f) is 0,
1156 the frame is being created and its X-window does not exist yet.
1157 In that case, just record the parameter's new value
1158 in the standard place; do not attempt to change the window. */
1161 x_set_foreground_color (f
, arg
, oldval
)
1163 Lisp_Object arg
, oldval
;
1165 struct x_output
*x
= f
->output_data
.x
;
1166 unsigned long fg
, old_fg
;
1168 fg
= x_decode_color (f
, arg
, BLACK_PIX_DEFAULT (f
));
1169 old_fg
= x
->foreground_pixel
;
1170 x
->foreground_pixel
= fg
;
1172 if (FRAME_X_WINDOW (f
) != 0)
1174 Display
*dpy
= FRAME_X_DISPLAY (f
);
1177 XSetForeground (dpy
, x
->normal_gc
, fg
);
1178 XSetBackground (dpy
, x
->reverse_gc
, fg
);
1180 if (x
->cursor_pixel
== old_fg
)
1182 unload_color (f
, x
->cursor_pixel
);
1183 x
->cursor_pixel
= x_copy_color (f
, fg
);
1184 XSetBackground (dpy
, x
->cursor_gc
, x
->cursor_pixel
);
1189 update_face_from_frame_parameter (f
, Qforeground_color
, arg
);
1191 if (FRAME_VISIBLE_P (f
))
1195 unload_color (f
, old_fg
);
1199 x_set_background_color (f
, arg
, oldval
)
1201 Lisp_Object arg
, oldval
;
1203 struct x_output
*x
= f
->output_data
.x
;
1206 bg
= x_decode_color (f
, arg
, WHITE_PIX_DEFAULT (f
));
1207 unload_color (f
, x
->background_pixel
);
1208 x
->background_pixel
= bg
;
1210 if (FRAME_X_WINDOW (f
) != 0)
1212 Display
*dpy
= FRAME_X_DISPLAY (f
);
1215 XSetBackground (dpy
, x
->normal_gc
, bg
);
1216 XSetForeground (dpy
, x
->reverse_gc
, bg
);
1217 XSetWindowBackground (dpy
, FRAME_X_WINDOW (f
), bg
);
1218 XSetForeground (dpy
, x
->cursor_gc
, bg
);
1221 xg_set_background_color (f
, bg
);
1224 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
1225 toolkit scroll bars. */
1228 for (bar
= FRAME_SCROLL_BARS (f
);
1230 bar
= XSCROLL_BAR (bar
)->next
)
1232 Window window
= SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar
));
1233 XSetWindowBackground (dpy
, window
, bg
);
1236 #endif /* USE_TOOLKIT_SCROLL_BARS */
1239 update_face_from_frame_parameter (f
, Qbackground_color
, arg
);
1241 if (FRAME_VISIBLE_P (f
))
1247 x_set_mouse_color (f
, arg
, oldval
)
1249 Lisp_Object arg
, oldval
;
1251 struct x_output
*x
= f
->output_data
.x
;
1252 Display
*dpy
= FRAME_X_DISPLAY (f
);
1253 Cursor cursor
, nontext_cursor
, mode_cursor
, hand_cursor
;
1254 Cursor hourglass_cursor
, horizontal_drag_cursor
;
1256 unsigned long pixel
= x_decode_color (f
, arg
, BLACK_PIX_DEFAULT (f
));
1257 unsigned long mask_color
= x
->background_pixel
;
1259 /* Don't let pointers be invisible. */
1260 if (mask_color
== pixel
)
1262 x_free_colors (f
, &pixel
, 1);
1263 pixel
= x_copy_color (f
, x
->foreground_pixel
);
1266 unload_color (f
, x
->mouse_pixel
);
1267 x
->mouse_pixel
= pixel
;
1271 /* It's not okay to crash if the user selects a screwy cursor. */
1272 count
= x_catch_errors (dpy
);
1274 if (!NILP (Vx_pointer_shape
))
1276 CHECK_NUMBER (Vx_pointer_shape
);
1277 cursor
= XCreateFontCursor (dpy
, XINT (Vx_pointer_shape
));
1280 cursor
= XCreateFontCursor (dpy
, XC_xterm
);
1281 x_check_errors (dpy
, "bad text pointer cursor: %s");
1283 if (!NILP (Vx_nontext_pointer_shape
))
1285 CHECK_NUMBER (Vx_nontext_pointer_shape
);
1287 = XCreateFontCursor (dpy
, XINT (Vx_nontext_pointer_shape
));
1290 nontext_cursor
= XCreateFontCursor (dpy
, XC_left_ptr
);
1291 x_check_errors (dpy
, "bad nontext pointer cursor: %s");
1293 if (!NILP (Vx_hourglass_pointer_shape
))
1295 CHECK_NUMBER (Vx_hourglass_pointer_shape
);
1297 = XCreateFontCursor (dpy
, XINT (Vx_hourglass_pointer_shape
));
1300 hourglass_cursor
= XCreateFontCursor (dpy
, XC_watch
);
1301 x_check_errors (dpy
, "bad hourglass pointer cursor: %s");
1303 x_check_errors (dpy
, "bad nontext pointer cursor: %s");
1304 if (!NILP (Vx_mode_pointer_shape
))
1306 CHECK_NUMBER (Vx_mode_pointer_shape
);
1307 mode_cursor
= XCreateFontCursor (dpy
, XINT (Vx_mode_pointer_shape
));
1310 mode_cursor
= XCreateFontCursor (dpy
, XC_xterm
);
1311 x_check_errors (dpy
, "bad modeline pointer cursor: %s");
1313 if (!NILP (Vx_sensitive_text_pointer_shape
))
1315 CHECK_NUMBER (Vx_sensitive_text_pointer_shape
);
1317 = XCreateFontCursor (dpy
, XINT (Vx_sensitive_text_pointer_shape
));
1320 hand_cursor
= XCreateFontCursor (dpy
, XC_hand2
);
1322 if (!NILP (Vx_window_horizontal_drag_shape
))
1324 CHECK_NUMBER (Vx_window_horizontal_drag_shape
);
1325 horizontal_drag_cursor
1326 = XCreateFontCursor (dpy
, XINT (Vx_window_horizontal_drag_shape
));
1329 horizontal_drag_cursor
1330 = XCreateFontCursor (dpy
, XC_sb_h_double_arrow
);
1332 /* Check and report errors with the above calls. */
1333 x_check_errors (dpy
, "can't set cursor shape: %s");
1334 x_uncatch_errors (dpy
, count
);
1337 XColor fore_color
, back_color
;
1339 fore_color
.pixel
= x
->mouse_pixel
;
1340 x_query_color (f
, &fore_color
);
1341 back_color
.pixel
= mask_color
;
1342 x_query_color (f
, &back_color
);
1344 XRecolorCursor (dpy
, cursor
, &fore_color
, &back_color
);
1345 XRecolorCursor (dpy
, nontext_cursor
, &fore_color
, &back_color
);
1346 XRecolorCursor (dpy
, mode_cursor
, &fore_color
, &back_color
);
1347 XRecolorCursor (dpy
, hand_cursor
, &fore_color
, &back_color
);
1348 XRecolorCursor (dpy
, hourglass_cursor
, &fore_color
, &back_color
);
1349 XRecolorCursor (dpy
, horizontal_drag_cursor
, &fore_color
, &back_color
);
1352 if (FRAME_X_WINDOW (f
) != 0)
1353 XDefineCursor (dpy
, FRAME_X_WINDOW (f
), cursor
);
1355 if (cursor
!= x
->text_cursor
1356 && x
->text_cursor
!= 0)
1357 XFreeCursor (dpy
, x
->text_cursor
);
1358 x
->text_cursor
= cursor
;
1360 if (nontext_cursor
!= x
->nontext_cursor
1361 && x
->nontext_cursor
!= 0)
1362 XFreeCursor (dpy
, x
->nontext_cursor
);
1363 x
->nontext_cursor
= nontext_cursor
;
1365 if (hourglass_cursor
!= x
->hourglass_cursor
1366 && x
->hourglass_cursor
!= 0)
1367 XFreeCursor (dpy
, x
->hourglass_cursor
);
1368 x
->hourglass_cursor
= hourglass_cursor
;
1370 if (mode_cursor
!= x
->modeline_cursor
1371 && x
->modeline_cursor
!= 0)
1372 XFreeCursor (dpy
, f
->output_data
.x
->modeline_cursor
);
1373 x
->modeline_cursor
= mode_cursor
;
1375 if (hand_cursor
!= x
->hand_cursor
1376 && x
->hand_cursor
!= 0)
1377 XFreeCursor (dpy
, x
->hand_cursor
);
1378 x
->hand_cursor
= hand_cursor
;
1380 if (horizontal_drag_cursor
!= x
->horizontal_drag_cursor
1381 && x
->horizontal_drag_cursor
!= 0)
1382 XFreeCursor (dpy
, x
->horizontal_drag_cursor
);
1383 x
->horizontal_drag_cursor
= horizontal_drag_cursor
;
1388 update_face_from_frame_parameter (f
, Qmouse_color
, arg
);
1392 x_set_cursor_color (f
, arg
, oldval
)
1394 Lisp_Object arg
, oldval
;
1396 unsigned long fore_pixel
, pixel
;
1397 int fore_pixel_allocated_p
= 0, pixel_allocated_p
= 0;
1398 struct x_output
*x
= f
->output_data
.x
;
1400 if (!NILP (Vx_cursor_fore_pixel
))
1402 fore_pixel
= x_decode_color (f
, Vx_cursor_fore_pixel
,
1403 WHITE_PIX_DEFAULT (f
));
1404 fore_pixel_allocated_p
= 1;
1407 fore_pixel
= x
->background_pixel
;
1409 pixel
= x_decode_color (f
, arg
, BLACK_PIX_DEFAULT (f
));
1410 pixel_allocated_p
= 1;
1412 /* Make sure that the cursor color differs from the background color. */
1413 if (pixel
== x
->background_pixel
)
1415 if (pixel_allocated_p
)
1417 x_free_colors (f
, &pixel
, 1);
1418 pixel_allocated_p
= 0;
1421 pixel
= x
->mouse_pixel
;
1422 if (pixel
== fore_pixel
)
1424 if (fore_pixel_allocated_p
)
1426 x_free_colors (f
, &fore_pixel
, 1);
1427 fore_pixel_allocated_p
= 0;
1429 fore_pixel
= x
->background_pixel
;
1433 unload_color (f
, x
->cursor_foreground_pixel
);
1434 if (!fore_pixel_allocated_p
)
1435 fore_pixel
= x_copy_color (f
, fore_pixel
);
1436 x
->cursor_foreground_pixel
= fore_pixel
;
1438 unload_color (f
, x
->cursor_pixel
);
1439 if (!pixel_allocated_p
)
1440 pixel
= x_copy_color (f
, pixel
);
1441 x
->cursor_pixel
= pixel
;
1443 if (FRAME_X_WINDOW (f
) != 0)
1446 XSetBackground (FRAME_X_DISPLAY (f
), x
->cursor_gc
, x
->cursor_pixel
);
1447 XSetForeground (FRAME_X_DISPLAY (f
), x
->cursor_gc
, fore_pixel
);
1450 if (FRAME_VISIBLE_P (f
))
1452 x_update_cursor (f
, 0);
1453 x_update_cursor (f
, 1);
1457 update_face_from_frame_parameter (f
, Qcursor_color
, arg
);
1460 /* Set the border-color of frame F to pixel value PIX.
1461 Note that this does not fully take effect if done before
1462 F has an x-window. */
1465 x_set_border_pixel (f
, pix
)
1469 unload_color (f
, f
->output_data
.x
->border_pixel
);
1470 f
->output_data
.x
->border_pixel
= pix
;
1472 if (FRAME_X_WINDOW (f
) != 0 && f
->border_width
> 0)
1475 XSetWindowBorder (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
1476 (unsigned long)pix
);
1479 if (FRAME_VISIBLE_P (f
))
1484 /* Set the border-color of frame F to value described by ARG.
1485 ARG can be a string naming a color.
1486 The border-color is used for the border that is drawn by the X server.
1487 Note that this does not fully take effect if done before
1488 F has an x-window; it must be redone when the window is created.
1490 Note: this is done in two routines because of the way X10 works.
1492 Note: under X11, this is normally the province of the window manager,
1493 and so emacs' border colors may be overridden. */
1496 x_set_border_color (f
, arg
, oldval
)
1498 Lisp_Object arg
, oldval
;
1503 pix
= x_decode_color (f
, arg
, BLACK_PIX_DEFAULT (f
));
1504 x_set_border_pixel (f
, pix
);
1505 update_face_from_frame_parameter (f
, Qborder_color
, arg
);
1510 x_set_cursor_type (f
, arg
, oldval
)
1512 Lisp_Object arg
, oldval
;
1514 set_frame_cursor_types (f
, arg
);
1516 /* Make sure the cursor gets redrawn. */
1517 cursor_type_changed
= 1;
1521 x_set_icon_type (f
, arg
, oldval
)
1523 Lisp_Object arg
, oldval
;
1529 if (STRINGP (oldval
) && EQ (Fstring_equal (oldval
, arg
), Qt
))
1532 else if (!STRINGP (oldval
) && EQ (oldval
, Qnil
) == EQ (arg
, Qnil
))
1537 result
= x_text_icon (f
,
1538 (char *) SDATA ((!NILP (f
->icon_name
)
1542 result
= x_bitmap_icon (f
, arg
);
1547 error ("No icon window available");
1550 XFlush (FRAME_X_DISPLAY (f
));
1555 x_set_icon_name (f
, arg
, oldval
)
1557 Lisp_Object arg
, oldval
;
1563 if (STRINGP (oldval
) && EQ (Fstring_equal (oldval
, arg
), Qt
))
1566 else if (!STRINGP (oldval
) && EQ (oldval
, Qnil
) == EQ (arg
, Qnil
))
1571 if (f
->output_data
.x
->icon_bitmap
!= 0)
1576 result
= x_text_icon (f
,
1577 (char *) SDATA ((!NILP (f
->icon_name
)
1586 error ("No icon window available");
1589 XFlush (FRAME_X_DISPLAY (f
));
1595 x_set_menu_bar_lines (f
, value
, oldval
)
1597 Lisp_Object value
, oldval
;
1600 #ifndef USE_X_TOOLKIT
1601 int olines
= FRAME_MENU_BAR_LINES (f
);
1604 /* Right now, menu bars don't work properly in minibuf-only frames;
1605 most of the commands try to apply themselves to the minibuffer
1606 frame itself, and get an error because you can't switch buffers
1607 in or split the minibuffer window. */
1608 if (FRAME_MINIBUF_ONLY_P (f
))
1611 if (INTEGERP (value
))
1612 nlines
= XINT (value
);
1616 /* Make sure we redisplay all windows in this frame. */
1617 windows_or_buffers_changed
++;
1619 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1620 FRAME_MENU_BAR_LINES (f
) = 0;
1623 FRAME_EXTERNAL_MENU_BAR (f
) = 1;
1624 if (FRAME_X_P (f
) && f
->output_data
.x
->menubar_widget
== 0)
1625 /* Make sure next redisplay shows the menu bar. */
1626 XWINDOW (FRAME_SELECTED_WINDOW (f
))->update_mode_line
= Qt
;
1630 if (FRAME_EXTERNAL_MENU_BAR (f
) == 1)
1631 free_frame_menubar (f
);
1632 FRAME_EXTERNAL_MENU_BAR (f
) = 0;
1634 f
->output_data
.x
->menubar_widget
= 0;
1636 #else /* not USE_X_TOOLKIT && not USE_GTK */
1637 FRAME_MENU_BAR_LINES (f
) = nlines
;
1638 change_window_heights (f
->root_window
, nlines
- olines
);
1639 #endif /* not USE_X_TOOLKIT */
1644 /* Set the number of lines used for the tool bar of frame F to VALUE.
1645 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1646 is the old number of tool bar lines. This function changes the
1647 height of all windows on frame F to match the new tool bar height.
1648 The frame's height doesn't change. */
1651 x_set_tool_bar_lines (f
, value
, oldval
)
1653 Lisp_Object value
, oldval
;
1655 int delta
, nlines
, root_height
;
1656 Lisp_Object root_window
;
1658 /* Treat tool bars like menu bars. */
1659 if (FRAME_MINIBUF_ONLY_P (f
))
1662 /* Use VALUE only if an integer >= 0. */
1663 if (INTEGERP (value
) && XINT (value
) >= 0)
1664 nlines
= XFASTINT (value
);
1669 FRAME_TOOL_BAR_LINES (f
) = 0;
1672 FRAME_EXTERNAL_TOOL_BAR (f
) = 1;
1673 if (FRAME_X_P (f
) && f
->output_data
.x
->toolbar_widget
== 0)
1674 /* Make sure next redisplay shows the tool bar. */
1675 XWINDOW (FRAME_SELECTED_WINDOW (f
))->update_mode_line
= Qt
;
1676 update_frame_tool_bar (f
);
1680 if (FRAME_EXTERNAL_TOOL_BAR (f
))
1681 free_frame_tool_bar (f
);
1682 FRAME_EXTERNAL_TOOL_BAR (f
) = 0;
1688 /* Make sure we redisplay all windows in this frame. */
1689 ++windows_or_buffers_changed
;
1691 delta
= nlines
- FRAME_TOOL_BAR_LINES (f
);
1693 /* Don't resize the tool-bar to more than we have room for. */
1694 root_window
= FRAME_ROOT_WINDOW (f
);
1695 root_height
= WINDOW_TOTAL_LINES (XWINDOW (root_window
));
1696 if (root_height
- delta
< 1)
1698 delta
= root_height
- 1;
1699 nlines
= FRAME_TOOL_BAR_LINES (f
) + delta
;
1702 FRAME_TOOL_BAR_LINES (f
) = nlines
;
1703 change_window_heights (root_window
, delta
);
1706 /* We also have to make sure that the internal border at the top of
1707 the frame, below the menu bar or tool bar, is redrawn when the
1708 tool bar disappears. This is so because the internal border is
1709 below the tool bar if one is displayed, but is below the menu bar
1710 if there isn't a tool bar. The tool bar draws into the area
1711 below the menu bar. */
1712 if (FRAME_X_WINDOW (f
) && FRAME_TOOL_BAR_LINES (f
) == 0)
1716 clear_current_matrices (f
);
1717 updating_frame
= NULL
;
1720 /* If the tool bar gets smaller, the internal border below it
1721 has to be cleared. It was formerly part of the display
1722 of the larger tool bar, and updating windows won't clear it. */
1725 int height
= FRAME_INTERNAL_BORDER_WIDTH (f
);
1726 int width
= FRAME_PIXEL_WIDTH (f
);
1727 int y
= nlines
* FRAME_LINE_HEIGHT (f
);
1730 x_clear_area (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
1731 0, y
, width
, height
, False
);
1734 if (WINDOWP (f
->tool_bar_window
))
1735 clear_glyph_matrix (XWINDOW (f
->tool_bar_window
)->current_matrix
);
1740 /* Set the foreground color for scroll bars on frame F to VALUE.
1741 VALUE should be a string, a color name. If it isn't a string or
1742 isn't a valid color name, do nothing. OLDVAL is the old value of
1743 the frame parameter. */
1746 x_set_scroll_bar_foreground (f
, value
, oldval
)
1748 Lisp_Object value
, oldval
;
1750 unsigned long pixel
;
1752 if (STRINGP (value
))
1753 pixel
= x_decode_color (f
, value
, BLACK_PIX_DEFAULT (f
));
1757 if (f
->output_data
.x
->scroll_bar_foreground_pixel
!= -1)
1758 unload_color (f
, f
->output_data
.x
->scroll_bar_foreground_pixel
);
1760 f
->output_data
.x
->scroll_bar_foreground_pixel
= pixel
;
1761 if (FRAME_X_WINDOW (f
) && FRAME_VISIBLE_P (f
))
1763 /* Remove all scroll bars because they have wrong colors. */
1764 if (condemn_scroll_bars_hook
)
1765 (*condemn_scroll_bars_hook
) (f
);
1766 if (judge_scroll_bars_hook
)
1767 (*judge_scroll_bars_hook
) (f
);
1769 update_face_from_frame_parameter (f
, Qscroll_bar_foreground
, value
);
1775 /* Set the background color for scroll bars on frame F to VALUE VALUE
1776 should be a string, a color name. If it isn't a string or isn't a
1777 valid color name, do nothing. OLDVAL is the old value of the frame
1781 x_set_scroll_bar_background (f
, value
, oldval
)
1783 Lisp_Object value
, oldval
;
1785 unsigned long pixel
;
1787 if (STRINGP (value
))
1788 pixel
= x_decode_color (f
, value
, WHITE_PIX_DEFAULT (f
));
1792 if (f
->output_data
.x
->scroll_bar_background_pixel
!= -1)
1793 unload_color (f
, f
->output_data
.x
->scroll_bar_background_pixel
);
1795 #ifdef USE_TOOLKIT_SCROLL_BARS
1796 /* Scrollbar shadow colors. */
1797 if (f
->output_data
.x
->scroll_bar_top_shadow_pixel
!= -1)
1799 unload_color (f
, f
->output_data
.x
->scroll_bar_top_shadow_pixel
);
1800 f
->output_data
.x
->scroll_bar_top_shadow_pixel
= -1;
1802 if (f
->output_data
.x
->scroll_bar_bottom_shadow_pixel
!= -1)
1804 unload_color (f
, f
->output_data
.x
->scroll_bar_bottom_shadow_pixel
);
1805 f
->output_data
.x
->scroll_bar_bottom_shadow_pixel
= -1;
1807 #endif /* USE_TOOLKIT_SCROLL_BARS */
1809 f
->output_data
.x
->scroll_bar_background_pixel
= pixel
;
1810 if (FRAME_X_WINDOW (f
) && FRAME_VISIBLE_P (f
))
1812 /* Remove all scroll bars because they have wrong colors. */
1813 if (condemn_scroll_bars_hook
)
1814 (*condemn_scroll_bars_hook
) (f
);
1815 if (judge_scroll_bars_hook
)
1816 (*judge_scroll_bars_hook
) (f
);
1818 update_face_from_frame_parameter (f
, Qscroll_bar_background
, value
);
1824 /* Encode Lisp string STRING as a text in a format appropriate for
1825 XICCC (X Inter Client Communication Conventions).
1827 If STRING contains only ASCII characters, do no conversion and
1828 return the string data of STRING. Otherwise, encode the text by
1829 CODING_SYSTEM, and return a newly allocated memory area which
1830 should be freed by `xfree' by a caller.
1832 SELECTIONP non-zero means the string is being encoded for an X
1833 selection, so it is safe to run pre-write conversions (which
1836 Store the byte length of resulting text in *TEXT_BYTES.
1838 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1839 which means that the `encoding' of the result can be `STRING'.
1840 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1841 the result should be `COMPOUND_TEXT'. */
1844 x_encode_text (string
, coding_system
, selectionp
, text_bytes
, stringp
)
1845 Lisp_Object string
, coding_system
;
1846 int *text_bytes
, *stringp
;
1849 unsigned char *str
= SDATA (string
);
1850 int chars
= SCHARS (string
);
1851 int bytes
= SBYTES (string
);
1855 struct coding_system coding
;
1856 extern Lisp_Object Qcompound_text_with_extensions
;
1858 charset_info
= find_charset_in_text (str
, chars
, bytes
, NULL
, Qnil
);
1859 if (charset_info
== 0)
1861 /* No multibyte character in OBJ. We need not encode it. */
1862 *text_bytes
= bytes
;
1867 setup_coding_system (coding_system
, &coding
);
1869 && SYMBOLP (coding
.pre_write_conversion
)
1870 && !NILP (Ffboundp (coding
.pre_write_conversion
)))
1872 string
= run_pre_post_conversion_on_str (string
, &coding
, 1);
1873 str
= SDATA (string
);
1874 chars
= SCHARS (string
);
1875 bytes
= SBYTES (string
);
1877 coding
.src_multibyte
= 1;
1878 coding
.dst_multibyte
= 0;
1879 coding
.mode
|= CODING_MODE_LAST_BLOCK
;
1880 if (coding
.type
== coding_type_iso2022
)
1881 coding
.flags
|= CODING_FLAG_ISO_SAFE
;
1882 /* We suppress producing escape sequences for composition. */
1883 coding
.composing
= COMPOSITION_DISABLED
;
1884 bufsize
= encoding_buffer_size (&coding
, bytes
);
1885 buf
= (unsigned char *) xmalloc (bufsize
);
1886 encode_coding (&coding
, str
, buf
, bytes
, bufsize
);
1887 *text_bytes
= coding
.produced
;
1888 *stringp
= (charset_info
== 1
1889 || (!EQ (coding_system
, Qcompound_text
)
1890 && !EQ (coding_system
, Qcompound_text_with_extensions
)));
1895 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1898 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1899 name; if NAME is a string, set F's name to NAME and set
1900 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1902 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1903 suggesting a new name, which lisp code should override; if
1904 F->explicit_name is set, ignore the new name; otherwise, set it. */
1907 x_set_name (f
, name
, explicit)
1912 /* Make sure that requests from lisp code override requests from
1913 Emacs redisplay code. */
1916 /* If we're switching from explicit to implicit, we had better
1917 update the mode lines and thereby update the title. */
1918 if (f
->explicit_name
&& NILP (name
))
1919 update_mode_lines
= 1;
1921 f
->explicit_name
= ! NILP (name
);
1923 else if (f
->explicit_name
)
1926 /* If NAME is nil, set the name to the x_id_name. */
1929 /* Check for no change needed in this very common case
1930 before we do any consing. */
1931 if (!strcmp (FRAME_X_DISPLAY_INFO (f
)->x_id_name
,
1934 name
= build_string (FRAME_X_DISPLAY_INFO (f
)->x_id_name
);
1937 CHECK_STRING (name
);
1939 /* Don't change the name if it's already NAME. */
1940 if (! NILP (Fstring_equal (name
, f
->name
)))
1945 /* For setting the frame title, the title parameter should override
1946 the name parameter. */
1947 if (! NILP (f
->title
))
1950 if (FRAME_X_WINDOW (f
))
1955 XTextProperty text
, icon
;
1957 Lisp_Object coding_system
;
1959 /* Note: Encoding strategy
1961 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1962 text.encoding. But, there are non-internationalized window
1963 managers which don't support that encoding. So, if NAME
1964 contains only ASCII and 8859-1 characters, encode it by
1965 iso-latin-1, and use "STRING" in text.encoding hoping that
1966 such window managers at least analyze this format correctly,
1967 i.e. treat 8-bit bytes as 8859-1 characters.
1969 We may also be able to use "UTF8_STRING" in text.encoding
1970 in the future which can encode all Unicode characters.
1971 But, for the moment, there's no way to know that the
1972 current window manager supports it or not. */
1973 coding_system
= Qcompound_text
;
1974 text
.value
= x_encode_text (name
, coding_system
, 0, &bytes
, &stringp
);
1975 text
.encoding
= (stringp
? XA_STRING
1976 : FRAME_X_DISPLAY_INFO (f
)->Xatom_COMPOUND_TEXT
);
1978 text
.nitems
= bytes
;
1980 if (NILP (f
->icon_name
))
1986 /* See the above comment "Note: Encoding strategy". */
1987 icon
.value
= x_encode_text (f
->icon_name
, coding_system
, 0,
1989 icon
.encoding
= (stringp
? XA_STRING
1990 : FRAME_X_DISPLAY_INFO (f
)->Xatom_COMPOUND_TEXT
);
1992 icon
.nitems
= bytes
;
1995 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
1997 #else /* not USE_GTK */
1998 XSetWMName (FRAME_X_DISPLAY (f
), FRAME_OUTER_WINDOW (f
), &text
);
1999 #endif /* not USE_GTK */
2001 XSetWMIconName (FRAME_X_DISPLAY (f
), FRAME_OUTER_WINDOW (f
), &icon
);
2003 if (!NILP (f
->icon_name
)
2004 && icon
.value
!= (unsigned char *) SDATA (f
->icon_name
))
2006 if (text
.value
!= (unsigned char *) SDATA (name
))
2009 #else /* not HAVE_X11R4 */
2010 XSetIconName (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2012 XStoreName (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2014 #endif /* not HAVE_X11R4 */
2019 /* This function should be called when the user's lisp code has
2020 specified a name for the frame; the name will override any set by the
2023 x_explicitly_set_name (f
, arg
, oldval
)
2025 Lisp_Object arg
, oldval
;
2027 x_set_name (f
, arg
, 1);
2030 /* This function should be called by Emacs redisplay code to set the
2031 name; names set this way will never override names set by the user's
2034 x_implicitly_set_name (f
, arg
, oldval
)
2036 Lisp_Object arg
, oldval
;
2038 x_set_name (f
, arg
, 0);
2041 /* Change the title of frame F to NAME.
2042 If NAME is nil, use the frame name as the title.
2044 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2045 name; if NAME is a string, set F's name to NAME and set
2046 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2048 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2049 suggesting a new name, which lisp code should override; if
2050 F->explicit_name is set, ignore the new name; otherwise, set it. */
2053 x_set_title (f
, name
, old_name
)
2055 Lisp_Object name
, old_name
;
2057 /* Don't change the title if it's already NAME. */
2058 if (EQ (name
, f
->title
))
2061 update_mode_lines
= 1;
2068 CHECK_STRING (name
);
2070 if (FRAME_X_WINDOW (f
))
2075 XTextProperty text
, icon
;
2077 Lisp_Object coding_system
;
2079 coding_system
= Qcompound_text
;
2080 /* See the comment "Note: Encoding strategy" in x_set_name. */
2081 text
.value
= x_encode_text (name
, coding_system
, 0, &bytes
, &stringp
);
2082 text
.encoding
= (stringp
? XA_STRING
2083 : FRAME_X_DISPLAY_INFO (f
)->Xatom_COMPOUND_TEXT
);
2085 text
.nitems
= bytes
;
2087 if (NILP (f
->icon_name
))
2093 /* See the comment "Note: Encoding strategy" in x_set_name. */
2094 icon
.value
= x_encode_text (f
->icon_name
, coding_system
, 0,
2096 icon
.encoding
= (stringp
? XA_STRING
2097 : FRAME_X_DISPLAY_INFO (f
)->Xatom_COMPOUND_TEXT
);
2099 icon
.nitems
= bytes
;
2103 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f
)),
2105 #else /* not USE_GTK */
2106 XSetWMName (FRAME_X_DISPLAY (f
), FRAME_OUTER_WINDOW (f
), &text
);
2107 #endif /* not USE_GTK */
2109 XSetWMIconName (FRAME_X_DISPLAY (f
), FRAME_OUTER_WINDOW (f
),
2112 if (!NILP (f
->icon_name
)
2113 && icon
.value
!= (unsigned char *) SDATA (f
->icon_name
))
2115 if (text
.value
!= (unsigned char *) SDATA (name
))
2118 #else /* not HAVE_X11R4 */
2119 XSetIconName (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2121 XStoreName (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2123 #endif /* not HAVE_X11R4 */
2129 x_set_scroll_bar_default_width (f
)
2132 int wid
= FRAME_COLUMN_WIDTH (f
);
2134 #ifdef USE_TOOLKIT_SCROLL_BARS
2135 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
2136 int width
= 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM
;
2137 FRAME_CONFIG_SCROLL_BAR_COLS (f
) = (width
+ wid
- 1) / wid
;
2138 FRAME_CONFIG_SCROLL_BAR_WIDTH (f
) = width
;
2140 /* Make the actual width at least 14 pixels and a multiple of a
2142 FRAME_CONFIG_SCROLL_BAR_COLS (f
) = (14 + wid
- 1) / wid
;
2144 /* Use all of that space (aside from required margins) for the
2146 FRAME_CONFIG_SCROLL_BAR_WIDTH (f
) = 0;
2151 /* Record in frame F the specified or default value according to ALIST
2152 of the parameter named PROP (a Lisp symbol). If no value is
2153 specified for PROP, look for an X default for XPROP on the frame
2154 named NAME. If that is not found either, use the value DEFLT. */
2157 x_default_scroll_bar_color_parameter (f
, alist
, prop
, xprop
, xclass
,
2166 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2169 tem
= x_get_arg (dpyinfo
, alist
, prop
, xprop
, xclass
, RES_TYPE_STRING
);
2170 if (EQ (tem
, Qunbound
))
2172 #ifdef USE_TOOLKIT_SCROLL_BARS
2174 /* See if an X resource for the scroll bar color has been
2176 tem
= display_x_get_resource (dpyinfo
,
2177 build_string (foreground_p
2181 build_string ("verticalScrollBar"),
2185 /* If nothing has been specified, scroll bars will use a
2186 toolkit-dependent default. Because these defaults are
2187 difficult to get at without actually creating a scroll
2188 bar, use nil to indicate that no color has been
2193 #else /* not USE_TOOLKIT_SCROLL_BARS */
2197 #endif /* not USE_TOOLKIT_SCROLL_BARS */
2200 x_set_frame_parameters (f
, Fcons (Fcons (prop
, tem
), Qnil
));
2206 #if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
2209 XSetWMProtocols (dpy
, w
, protocols
, count
)
2216 prop
= XInternAtom (dpy
, "WM_PROTOCOLS", False
);
2217 if (prop
== None
) return False
;
2218 XChangeProperty (dpy
, w
, prop
, XA_ATOM
, 32, PropModeReplace
,
2219 (unsigned char *) protocols
, count
);
2222 #endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
2224 #ifdef USE_X_TOOLKIT
2226 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
2227 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
2228 already be present because of the toolkit (Motif adds some of them,
2229 for example, but Xt doesn't). */
2232 hack_wm_protocols (f
, widget
)
2236 Display
*dpy
= XtDisplay (widget
);
2237 Window w
= XtWindow (widget
);
2238 int need_delete
= 1;
2244 Atom type
, *atoms
= 0;
2246 unsigned long nitems
= 0;
2247 unsigned long bytes_after
;
2249 if ((XGetWindowProperty (dpy
, w
,
2250 FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_protocols
,
2251 (long)0, (long)100, False
, XA_ATOM
,
2252 &type
, &format
, &nitems
, &bytes_after
,
2253 (unsigned char **) &atoms
)
2255 && format
== 32 && type
== XA_ATOM
)
2259 if (atoms
[nitems
] == FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_delete_window
)
2261 else if (atoms
[nitems
] == FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_take_focus
)
2263 else if (atoms
[nitems
] == FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_save_yourself
)
2266 if (atoms
) XFree ((char *) atoms
);
2272 props
[count
++] = FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_delete_window
;
2274 props
[count
++] = FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_take_focus
;
2276 props
[count
++] = FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_save_yourself
;
2278 XChangeProperty (dpy
, w
, FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_protocols
,
2279 XA_ATOM
, 32, PropModeAppend
,
2280 (unsigned char *) props
, count
);
2288 /* Support routines for XIC (X Input Context). */
2292 static XFontSet xic_create_xfontset
P_ ((struct frame
*, char *));
2293 static XIMStyle best_xim_style
P_ ((XIMStyles
*, XIMStyles
*));
2296 /* Supported XIM styles, ordered by preference. */
2298 static XIMStyle supported_xim_styles
[] =
2300 XIMPreeditPosition
| XIMStatusArea
,
2301 XIMPreeditPosition
| XIMStatusNothing
,
2302 XIMPreeditPosition
| XIMStatusNone
,
2303 XIMPreeditNothing
| XIMStatusArea
,
2304 XIMPreeditNothing
| XIMStatusNothing
,
2305 XIMPreeditNothing
| XIMStatusNone
,
2306 XIMPreeditNone
| XIMStatusArea
,
2307 XIMPreeditNone
| XIMStatusNothing
,
2308 XIMPreeditNone
| XIMStatusNone
,
2313 /* Create an X fontset on frame F with base font name
2317 xic_create_xfontset (f
, base_fontname
)
2319 char *base_fontname
;
2322 char **missing_list
;
2326 xfs
= XCreateFontSet (FRAME_X_DISPLAY (f
),
2327 base_fontname
, &missing_list
,
2328 &missing_count
, &def_string
);
2330 XFreeStringList (missing_list
);
2332 /* No need to free def_string. */
2337 /* Value is the best input style, given user preferences USER (already
2338 checked to be supported by Emacs), and styles supported by the
2339 input method XIM. */
2342 best_xim_style (user
, xim
)
2348 for (i
= 0; i
< user
->count_styles
; ++i
)
2349 for (j
= 0; j
< xim
->count_styles
; ++j
)
2350 if (user
->supported_styles
[i
] == xim
->supported_styles
[j
])
2351 return user
->supported_styles
[i
];
2353 /* Return the default style. */
2354 return XIMPreeditNothing
| XIMStatusNothing
;
2357 /* Create XIC for frame F. */
2359 static XIMStyle xic_style
;
2362 create_frame_xic (f
)
2367 XFontSet xfs
= NULL
;
2372 xim
= FRAME_X_XIM (f
);
2377 XVaNestedList preedit_attr
;
2378 XVaNestedList status_attr
;
2379 char *base_fontname
;
2382 s_area
.x
= 0; s_area
.y
= 0; s_area
.width
= 1; s_area
.height
= 1;
2383 spot
.x
= 0; spot
.y
= 1;
2384 /* Create X fontset. */
2385 fontset
= FRAME_FONTSET (f
);
2387 base_fontname
= "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
2390 /* Determine the base fontname from the ASCII font name of
2392 char *ascii_font
= (char *) SDATA (fontset_ascii (fontset
));
2393 char *p
= ascii_font
;
2396 for (i
= 0; *p
; p
++)
2399 /* As the font name doesn't conform to XLFD, we can't
2400 modify it to get a suitable base fontname for the
2402 base_fontname
= "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
2405 int len
= strlen (ascii_font
) + 1;
2408 for (i
= 0, p
= ascii_font
; i
< 8; p
++)
2417 base_fontname
= (char *) alloca (len
);
2418 bzero (base_fontname
, len
);
2419 strcpy (base_fontname
, "-*-*-");
2420 bcopy (p1
, base_fontname
+ 5, p
- p1
);
2421 strcat (base_fontname
, "*-*-*-*-*-*-*");
2424 xfs
= xic_create_xfontset (f
, base_fontname
);
2426 /* Determine XIC style. */
2429 XIMStyles supported_list
;
2430 supported_list
.count_styles
= (sizeof supported_xim_styles
2431 / sizeof supported_xim_styles
[0]);
2432 supported_list
.supported_styles
= supported_xim_styles
;
2433 xic_style
= best_xim_style (&supported_list
,
2434 FRAME_X_XIM_STYLES (f
));
2437 preedit_attr
= XVaCreateNestedList (0,
2440 FRAME_FOREGROUND_PIXEL (f
),
2442 FRAME_BACKGROUND_PIXEL (f
),
2443 (xic_style
& XIMPreeditPosition
2448 status_attr
= XVaCreateNestedList (0,
2454 FRAME_FOREGROUND_PIXEL (f
),
2456 FRAME_BACKGROUND_PIXEL (f
),
2459 xic
= XCreateIC (xim
,
2460 XNInputStyle
, xic_style
,
2461 XNClientWindow
, FRAME_X_WINDOW (f
),
2462 XNFocusWindow
, FRAME_X_WINDOW (f
),
2463 XNStatusAttributes
, status_attr
,
2464 XNPreeditAttributes
, preedit_attr
,
2466 XFree (preedit_attr
);
2467 XFree (status_attr
);
2470 FRAME_XIC (f
) = xic
;
2471 FRAME_XIC_STYLE (f
) = xic_style
;
2472 FRAME_XIC_FONTSET (f
) = xfs
;
2476 /* Destroy XIC and free XIC fontset of frame F, if any. */
2482 if (FRAME_XIC (f
) == NULL
)
2485 XDestroyIC (FRAME_XIC (f
));
2486 if (FRAME_XIC_FONTSET (f
))
2487 XFreeFontSet (FRAME_X_DISPLAY (f
), FRAME_XIC_FONTSET (f
));
2489 FRAME_XIC (f
) = NULL
;
2490 FRAME_XIC_FONTSET (f
) = NULL
;
2494 /* Place preedit area for XIC of window W's frame to specified
2495 pixel position X/Y. X and Y are relative to window W. */
2498 xic_set_preeditarea (w
, x
, y
)
2502 struct frame
*f
= XFRAME (w
->frame
);
2506 spot
.x
= WINDOW_TO_FRAME_PIXEL_X (w
, x
) + WINDOW_LEFT_FRINGE_WIDTH (w
);
2507 spot
.y
= WINDOW_TO_FRAME_PIXEL_Y (w
, y
) + FONT_BASE (FRAME_FONT (f
));
2508 attr
= XVaCreateNestedList (0, XNSpotLocation
, &spot
, NULL
);
2509 XSetICValues (FRAME_XIC (f
), XNPreeditAttributes
, attr
, NULL
);
2514 /* Place status area for XIC in bottom right corner of frame F.. */
2517 xic_set_statusarea (f
)
2520 XIC xic
= FRAME_XIC (f
);
2525 /* Negotiate geometry of status area. If input method has existing
2526 status area, use its current size. */
2527 area
.x
= area
.y
= area
.width
= area
.height
= 0;
2528 attr
= XVaCreateNestedList (0, XNAreaNeeded
, &area
, NULL
);
2529 XSetICValues (xic
, XNStatusAttributes
, attr
, NULL
);
2532 attr
= XVaCreateNestedList (0, XNAreaNeeded
, &needed
, NULL
);
2533 XGetICValues (xic
, XNStatusAttributes
, attr
, NULL
);
2536 if (needed
->width
== 0) /* Use XNArea instead of XNAreaNeeded */
2538 attr
= XVaCreateNestedList (0, XNArea
, &needed
, NULL
);
2539 XGetICValues (xic
, XNStatusAttributes
, attr
, NULL
);
2543 area
.width
= needed
->width
;
2544 area
.height
= needed
->height
;
2545 area
.x
= FRAME_PIXEL_WIDTH (f
) - area
.width
- FRAME_INTERNAL_BORDER_WIDTH (f
);
2546 area
.y
= (FRAME_PIXEL_HEIGHT (f
) - area
.height
2547 - FRAME_MENUBAR_HEIGHT (f
)
2548 - FRAME_TOOLBAR_HEIGHT (f
)
2549 - FRAME_INTERNAL_BORDER_WIDTH (f
));
2552 attr
= XVaCreateNestedList (0, XNArea
, &area
, NULL
);
2553 XSetICValues (xic
, XNStatusAttributes
, attr
, NULL
);
2558 /* Set X fontset for XIC of frame F, using base font name
2559 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2562 xic_set_xfontset (f
, base_fontname
)
2564 char *base_fontname
;
2569 xfs
= xic_create_xfontset (f
, base_fontname
);
2571 attr
= XVaCreateNestedList (0, XNFontSet
, xfs
, NULL
);
2572 if (FRAME_XIC_STYLE (f
) & XIMPreeditPosition
)
2573 XSetICValues (FRAME_XIC (f
), XNPreeditAttributes
, attr
, NULL
);
2574 if (FRAME_XIC_STYLE (f
) & XIMStatusArea
)
2575 XSetICValues (FRAME_XIC (f
), XNStatusAttributes
, attr
, NULL
);
2578 if (FRAME_XIC_FONTSET (f
))
2579 XFreeFontSet (FRAME_X_DISPLAY (f
), FRAME_XIC_FONTSET (f
));
2580 FRAME_XIC_FONTSET (f
) = xfs
;
2583 #endif /* HAVE_X_I18N */
2587 #ifdef USE_X_TOOLKIT
2589 /* Create and set up the X widget for frame F. */
2592 x_window (f
, window_prompting
, minibuffer_only
)
2594 long window_prompting
;
2595 int minibuffer_only
;
2597 XClassHint class_hints
;
2598 XSetWindowAttributes attributes
;
2599 unsigned long attribute_mask
;
2600 Widget shell_widget
;
2602 Widget frame_widget
;
2608 /* Use the resource name as the top-level widget name
2609 for looking up resources. Make a non-Lisp copy
2610 for the window manager, so GC relocation won't bother it.
2612 Elsewhere we specify the window name for the window manager. */
2615 char *str
= (char *) SDATA (Vx_resource_name
);
2616 f
->namebuf
= (char *) xmalloc (strlen (str
) + 1);
2617 strcpy (f
->namebuf
, str
);
2621 XtSetArg (al
[ac
], XtNallowShellResize
, 1); ac
++;
2622 XtSetArg (al
[ac
], XtNinput
, 1); ac
++;
2623 XtSetArg (al
[ac
], XtNmappedWhenManaged
, 0); ac
++;
2624 XtSetArg (al
[ac
], XtNborderWidth
, f
->border_width
); ac
++;
2625 XtSetArg (al
[ac
], XtNvisual
, FRAME_X_VISUAL (f
)); ac
++;
2626 XtSetArg (al
[ac
], XtNdepth
, FRAME_X_DISPLAY_INFO (f
)->n_planes
); ac
++;
2627 XtSetArg (al
[ac
], XtNcolormap
, FRAME_X_COLORMAP (f
)); ac
++;
2628 shell_widget
= XtAppCreateShell (f
->namebuf
, EMACS_CLASS
,
2629 applicationShellWidgetClass
,
2630 FRAME_X_DISPLAY (f
), al
, ac
);
2632 f
->output_data
.x
->widget
= shell_widget
;
2633 /* maybe_set_screen_title_format (shell_widget); */
2635 pane_widget
= lw_create_widget ("main", "pane", widget_id_tick
++,
2636 (widget_value
*) NULL
,
2637 shell_widget
, False
,
2641 (lw_callback
) NULL
);
2644 XtSetArg (al
[ac
], XtNvisual
, FRAME_X_VISUAL (f
)); ac
++;
2645 XtSetArg (al
[ac
], XtNdepth
, FRAME_X_DISPLAY_INFO (f
)->n_planes
); ac
++;
2646 XtSetArg (al
[ac
], XtNcolormap
, FRAME_X_COLORMAP (f
)); ac
++;
2647 XtSetValues (pane_widget
, al
, ac
);
2648 f
->output_data
.x
->column_widget
= pane_widget
;
2650 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2651 the emacs screen when changing menubar. This reduces flickering. */
2654 XtSetArg (al
[ac
], XtNmappedWhenManaged
, 0); ac
++;
2655 XtSetArg (al
[ac
], XtNshowGrip
, 0); ac
++;
2656 XtSetArg (al
[ac
], XtNallowResize
, 1); ac
++;
2657 XtSetArg (al
[ac
], XtNresizeToPreferred
, 1); ac
++;
2658 XtSetArg (al
[ac
], XtNemacsFrame
, f
); ac
++;
2659 XtSetArg (al
[ac
], XtNvisual
, FRAME_X_VISUAL (f
)); ac
++;
2660 XtSetArg (al
[ac
], XtNdepth
, FRAME_X_DISPLAY_INFO (f
)->n_planes
); ac
++;
2661 XtSetArg (al
[ac
], XtNcolormap
, FRAME_X_COLORMAP (f
)); ac
++;
2662 frame_widget
= XtCreateWidget (f
->namebuf
, emacsFrameClass
, pane_widget
,
2665 f
->output_data
.x
->edit_widget
= frame_widget
;
2667 XtManageChild (frame_widget
);
2669 /* Do some needed geometry management. */
2672 char *tem
, shell_position
[32];
2675 int extra_borders
= 0;
2677 = (f
->output_data
.x
->menubar_widget
2678 ? (f
->output_data
.x
->menubar_widget
->core
.height
2679 + f
->output_data
.x
->menubar_widget
->core
.border_width
)
2682 #if 0 /* Experimentally, we now get the right results
2683 for -geometry -0-0 without this. 24 Aug 96, rms. */
2684 if (FRAME_EXTERNAL_MENU_BAR (f
))
2687 XtVaGetValues (pane_widget
, XtNinternalBorderWidth
, &ibw
, NULL
);
2688 menubar_size
+= ibw
;
2692 f
->output_data
.x
->menubar_height
= menubar_size
;
2695 /* Motif seems to need this amount added to the sizes
2696 specified for the shell widget. The Athena/Lucid widgets don't.
2697 Both conclusions reached experimentally. -- rms. */
2698 XtVaGetValues (f
->output_data
.x
->edit_widget
, XtNinternalBorderWidth
,
2699 &extra_borders
, NULL
);
2703 /* Convert our geometry parameters into a geometry string
2705 Note that we do not specify here whether the position
2706 is a user-specified or program-specified one.
2707 We pass that information later, in x_wm_set_size_hints. */
2709 int left
= f
->left_pos
;
2710 int xneg
= window_prompting
& XNegative
;
2711 int top
= f
->top_pos
;
2712 int yneg
= window_prompting
& YNegative
;
2718 if (window_prompting
& USPosition
)
2719 sprintf (shell_position
, "=%dx%d%c%d%c%d",
2720 FRAME_PIXEL_WIDTH (f
) + extra_borders
,
2721 FRAME_PIXEL_HEIGHT (f
) + menubar_size
+ extra_borders
,
2722 (xneg
? '-' : '+'), left
,
2723 (yneg
? '-' : '+'), top
);
2726 sprintf (shell_position
, "=%dx%d",
2727 FRAME_PIXEL_WIDTH (f
) + extra_borders
,
2728 FRAME_PIXEL_HEIGHT (f
) + menubar_size
+ extra_borders
);
2730 /* Setting x and y when the position is not specified in
2731 the geometry string will set program position in the WM hints.
2732 If Emacs had just one program position, we could set it in
2733 fallback resources, but since each make-frame call can specify
2734 different program positions, this is easier. */
2735 XtSetArg (al
[ac
], XtNx
, left
); ac
++;
2736 XtSetArg (al
[ac
], XtNy
, top
); ac
++;
2740 len
= strlen (shell_position
) + 1;
2741 /* We don't free this because we don't know whether
2742 it is safe to free it while the frame exists.
2743 It isn't worth the trouble of arranging to free it
2744 when the frame is deleted. */
2745 tem
= (char *) xmalloc (len
);
2746 strncpy (tem
, shell_position
, len
);
2747 XtSetArg (al
[ac
], XtNgeometry
, tem
); ac
++;
2748 XtSetValues (shell_widget
, al
, ac
);
2751 XtManageChild (pane_widget
);
2752 XtRealizeWidget (shell_widget
);
2754 FRAME_X_WINDOW (f
) = XtWindow (frame_widget
);
2756 validate_x_resource_name ();
2758 class_hints
.res_name
= (char *) SDATA (Vx_resource_name
);
2759 class_hints
.res_class
= (char *) SDATA (Vx_resource_class
);
2760 XSetClassHint (FRAME_X_DISPLAY (f
), XtWindow (shell_widget
), &class_hints
);
2763 FRAME_XIC (f
) = NULL
;
2765 create_frame_xic (f
);
2768 f
->output_data
.x
->wm_hints
.input
= True
;
2769 f
->output_data
.x
->wm_hints
.flags
|= InputHint
;
2770 XSetWMHints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2771 &f
->output_data
.x
->wm_hints
);
2773 hack_wm_protocols (f
, shell_widget
);
2776 XtAddEventHandler (shell_widget
, 0, True
, _XEditResCheckMessages
, 0);
2779 /* Do a stupid property change to force the server to generate a
2780 PropertyNotify event so that the event_stream server timestamp will
2781 be initialized to something relevant to the time we created the window.
2783 XChangeProperty (XtDisplay (frame_widget
), XtWindow (frame_widget
),
2784 FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_protocols
,
2785 XA_ATOM
, 32, PropModeAppend
,
2786 (unsigned char*) NULL
, 0);
2788 /* Make all the standard events reach the Emacs frame. */
2789 attributes
.event_mask
= STANDARD_EVENT_SET
;
2794 /* XIM server might require some X events. */
2795 unsigned long fevent
= NoEventMask
;
2796 XGetICValues (FRAME_XIC (f
), XNFilterEvents
, &fevent
, NULL
);
2797 attributes
.event_mask
|= fevent
;
2799 #endif /* HAVE_X_I18N */
2801 attribute_mask
= CWEventMask
;
2802 XChangeWindowAttributes (XtDisplay (shell_widget
), XtWindow (shell_widget
),
2803 attribute_mask
, &attributes
);
2805 XtMapWidget (frame_widget
);
2807 /* x_set_name normally ignores requests to set the name if the
2808 requested name is the same as the current name. This is the one
2809 place where that assumption isn't correct; f->name is set, but
2810 the X server hasn't been told. */
2813 int explicit = f
->explicit_name
;
2815 f
->explicit_name
= 0;
2818 x_set_name (f
, name
, explicit);
2821 XDefineCursor (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2822 f
->output_data
.x
->text_cursor
);
2826 /* This is a no-op, except under Motif. Make sure main areas are
2827 set to something reasonable, in case we get an error later. */
2828 lw_set_main_areas (pane_widget
, 0, frame_widget
);
2831 #else /* not USE_X_TOOLKIT */
2837 if (! xg_create_frame_widgets (f
))
2838 error ("Unable to create window");
2841 FRAME_XIC (f
) = NULL
;
2845 create_frame_xic (f
);
2848 /* XIM server might require some X events. */
2849 unsigned long fevent
= NoEventMask
;
2850 XGetICValues (FRAME_XIC (f
), XNFilterEvents
, &fevent
, NULL
);
2852 if (fevent
!= NoEventMask
)
2854 XSetWindowAttributes attributes
;
2855 XWindowAttributes wattr
;
2856 unsigned long attribute_mask
;
2858 XGetWindowAttributes (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2860 attributes
.event_mask
= wattr
.your_event_mask
| fevent
;
2861 attribute_mask
= CWEventMask
;
2862 XChangeWindowAttributes (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2863 attribute_mask
, &attributes
);
2871 #else /*! USE_GTK */
2872 /* Create and set up the X window for frame F. */
2879 XClassHint class_hints
;
2880 XSetWindowAttributes attributes
;
2881 unsigned long attribute_mask
;
2883 attributes
.background_pixel
= f
->output_data
.x
->background_pixel
;
2884 attributes
.border_pixel
= f
->output_data
.x
->border_pixel
;
2885 attributes
.bit_gravity
= StaticGravity
;
2886 attributes
.backing_store
= NotUseful
;
2887 attributes
.save_under
= True
;
2888 attributes
.event_mask
= STANDARD_EVENT_SET
;
2889 attributes
.colormap
= FRAME_X_COLORMAP (f
);
2890 attribute_mask
= (CWBackPixel
| CWBorderPixel
| CWBitGravity
| CWEventMask
2895 = XCreateWindow (FRAME_X_DISPLAY (f
),
2896 f
->output_data
.x
->parent_desc
,
2899 FRAME_PIXEL_WIDTH (f
), FRAME_PIXEL_HEIGHT (f
),
2901 CopyFromParent
, /* depth */
2902 InputOutput
, /* class */
2904 attribute_mask
, &attributes
);
2909 create_frame_xic (f
);
2912 /* XIM server might require some X events. */
2913 unsigned long fevent
= NoEventMask
;
2914 XGetICValues (FRAME_XIC (f
), XNFilterEvents
, &fevent
, NULL
);
2915 attributes
.event_mask
|= fevent
;
2916 attribute_mask
= CWEventMask
;
2917 XChangeWindowAttributes (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2918 attribute_mask
, &attributes
);
2921 #endif /* HAVE_X_I18N */
2923 validate_x_resource_name ();
2925 class_hints
.res_name
= (char *) SDATA (Vx_resource_name
);
2926 class_hints
.res_class
= (char *) SDATA (Vx_resource_class
);
2927 XSetClassHint (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), &class_hints
);
2929 /* The menubar is part of the ordinary display;
2930 it does not count in addition to the height of the window. */
2931 f
->output_data
.x
->menubar_height
= 0;
2933 /* This indicates that we use the "Passive Input" input model.
2934 Unless we do this, we don't get the Focus{In,Out} events that we
2935 need to draw the cursor correctly. Accursed bureaucrats.
2936 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2938 f
->output_data
.x
->wm_hints
.input
= True
;
2939 f
->output_data
.x
->wm_hints
.flags
|= InputHint
;
2940 XSetWMHints (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2941 &f
->output_data
.x
->wm_hints
);
2942 f
->output_data
.x
->wm_hints
.icon_pixmap
= None
;
2944 /* Request "save yourself" and "delete window" commands from wm. */
2947 protocols
[0] = FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_delete_window
;
2948 protocols
[1] = FRAME_X_DISPLAY_INFO (f
)->Xatom_wm_save_yourself
;
2949 XSetWMProtocols (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), protocols
, 2);
2952 /* x_set_name normally ignores requests to set the name if the
2953 requested name is the same as the current name. This is the one
2954 place where that assumption isn't correct; f->name is set, but
2955 the X server hasn't been told. */
2958 int explicit = f
->explicit_name
;
2960 f
->explicit_name
= 0;
2963 x_set_name (f
, name
, explicit);
2966 XDefineCursor (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
2967 f
->output_data
.x
->text_cursor
);
2971 if (FRAME_X_WINDOW (f
) == 0)
2972 error ("Unable to create window");
2975 #endif /* not USE_GTK */
2976 #endif /* not USE_X_TOOLKIT */
2978 /* Handle the icon stuff for this window. Perhaps later we might
2979 want an x_set_icon_position which can be called interactively as
2987 Lisp_Object icon_x
, icon_y
;
2988 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
2990 /* Set the position of the icon. Note that twm groups all
2991 icons in an icon window. */
2992 icon_x
= x_frame_get_and_record_arg (f
, parms
, Qicon_left
, 0, 0, RES_TYPE_NUMBER
);
2993 icon_y
= x_frame_get_and_record_arg (f
, parms
, Qicon_top
, 0, 0, RES_TYPE_NUMBER
);
2994 if (!EQ (icon_x
, Qunbound
) && !EQ (icon_y
, Qunbound
))
2996 CHECK_NUMBER (icon_x
);
2997 CHECK_NUMBER (icon_y
);
2999 else if (!EQ (icon_x
, Qunbound
) || !EQ (icon_y
, Qunbound
))
3000 error ("Both left and top icon corners of icon must be specified");
3004 if (! EQ (icon_x
, Qunbound
))
3005 x_wm_set_icon_position (f
, XINT (icon_x
), XINT (icon_y
));
3007 /* Start up iconic or window? */
3008 x_wm_set_window_state
3009 (f
, (EQ (x_get_arg (dpyinfo
, parms
, Qvisibility
, 0, 0, RES_TYPE_SYMBOL
),
3014 x_text_icon (f
, (char *) SDATA ((!NILP (f
->icon_name
)
3021 /* Make the GCs needed for this window, setting the
3022 background, border and mouse colors; also create the
3023 mouse cursor and the gray border tile. */
3025 static char cursor_bits
[] =
3027 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3028 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3029 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3030 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
3037 XGCValues gc_values
;
3041 /* Create the GCs of this frame.
3042 Note that many default values are used. */
3045 gc_values
.font
= FRAME_FONT (f
)->fid
;
3046 gc_values
.foreground
= f
->output_data
.x
->foreground_pixel
;
3047 gc_values
.background
= f
->output_data
.x
->background_pixel
;
3048 gc_values
.line_width
= 0; /* Means 1 using fast algorithm. */
3049 f
->output_data
.x
->normal_gc
3050 = XCreateGC (FRAME_X_DISPLAY (f
),
3052 GCLineWidth
| GCFont
| GCForeground
| GCBackground
,
3055 /* Reverse video style. */
3056 gc_values
.foreground
= f
->output_data
.x
->background_pixel
;
3057 gc_values
.background
= f
->output_data
.x
->foreground_pixel
;
3058 f
->output_data
.x
->reverse_gc
3059 = XCreateGC (FRAME_X_DISPLAY (f
),
3061 GCFont
| GCForeground
| GCBackground
| GCLineWidth
,
3064 /* Cursor has cursor-color background, background-color foreground. */
3065 gc_values
.foreground
= f
->output_data
.x
->background_pixel
;
3066 gc_values
.background
= f
->output_data
.x
->cursor_pixel
;
3067 gc_values
.fill_style
= FillOpaqueStippled
;
3069 = XCreateBitmapFromData (FRAME_X_DISPLAY (f
),
3070 FRAME_X_DISPLAY_INFO (f
)->root_window
,
3071 cursor_bits
, 16, 16);
3072 f
->output_data
.x
->cursor_gc
3073 = XCreateGC (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3074 (GCFont
| GCForeground
| GCBackground
3075 | GCFillStyle
/* | GCStipple */ | GCLineWidth
),
3079 f
->output_data
.x
->white_relief
.gc
= 0;
3080 f
->output_data
.x
->black_relief
.gc
= 0;
3082 /* Create the gray border tile used when the pointer is not in
3083 the frame. Since this depends on the frame's pixel values,
3084 this must be done on a per-frame basis. */
3085 f
->output_data
.x
->border_tile
3086 = (XCreatePixmapFromBitmapData
3087 (FRAME_X_DISPLAY (f
), FRAME_X_DISPLAY_INFO (f
)->root_window
,
3088 gray_bits
, gray_width
, gray_height
,
3089 f
->output_data
.x
->foreground_pixel
,
3090 f
->output_data
.x
->background_pixel
,
3091 DefaultDepth (FRAME_X_DISPLAY (f
), FRAME_X_SCREEN_NUMBER (f
))));
3097 /* Free what was was allocated in x_make_gc. */
3103 Display
*dpy
= FRAME_X_DISPLAY (f
);
3107 if (f
->output_data
.x
->normal_gc
)
3109 XFreeGC (dpy
, f
->output_data
.x
->normal_gc
);
3110 f
->output_data
.x
->normal_gc
= 0;
3113 if (f
->output_data
.x
->reverse_gc
)
3115 XFreeGC (dpy
, f
->output_data
.x
->reverse_gc
);
3116 f
->output_data
.x
->reverse_gc
= 0;
3119 if (f
->output_data
.x
->cursor_gc
)
3121 XFreeGC (dpy
, f
->output_data
.x
->cursor_gc
);
3122 f
->output_data
.x
->cursor_gc
= 0;
3125 if (f
->output_data
.x
->border_tile
)
3127 XFreePixmap (dpy
, f
->output_data
.x
->border_tile
);
3128 f
->output_data
.x
->border_tile
= 0;
3135 /* Handler for signals raised during x_create_frame and
3136 x_create_top_frame. FRAME is the frame which is partially
3140 unwind_create_frame (frame
)
3143 struct frame
*f
= XFRAME (frame
);
3145 /* If frame is ``official'', nothing to do. */
3146 if (!CONSP (Vframe_list
) || !EQ (XCAR (Vframe_list
), frame
))
3149 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
3152 x_free_frame_resources (f
);
3154 /* Check that reference counts are indeed correct. */
3155 xassert (dpyinfo
->reference_count
== dpyinfo_refcount
);
3156 xassert (dpyinfo
->image_cache
->refcount
== image_cache_refcount
);
3164 DEFUN ("x-create-frame", Fx_create_frame
, Sx_create_frame
,
3166 doc
: /* Make a new X window, which is called a "frame" in Emacs terms.
3167 Returns an Emacs frame object.
3168 ALIST is an alist of frame parameters.
3169 If the parameters specify that the frame should not have a minibuffer,
3170 and do not specify a specific minibuffer window to use,
3171 then `default-minibuffer-frame' must be a frame whose minibuffer can
3172 be shared by the new frame.
3174 This function is an internal primitive--use `make-frame' instead. */)
3179 Lisp_Object frame
, tem
;
3181 int minibuffer_only
= 0;
3182 long window_prompting
= 0;
3184 int count
= SPECPDL_INDEX ();
3185 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
3186 Lisp_Object display
;
3187 struct x_display_info
*dpyinfo
= NULL
;
3193 /* Use this general default value to start with
3194 until we know if this frame has a specified name. */
3195 Vx_resource_name
= Vinvocation_name
;
3197 display
= x_get_arg (dpyinfo
, parms
, Qdisplay
, 0, 0, RES_TYPE_STRING
);
3198 if (EQ (display
, Qunbound
))
3200 dpyinfo
= check_x_display_info (display
);
3202 kb
= dpyinfo
->kboard
;
3204 kb
= &the_only_kboard
;
3207 name
= x_get_arg (dpyinfo
, parms
, Qname
, "name", "Name", RES_TYPE_STRING
);
3209 && ! EQ (name
, Qunbound
)
3211 error ("Invalid frame name--not a string or nil");
3214 Vx_resource_name
= name
;
3216 /* See if parent window is specified. */
3217 parent
= x_get_arg (dpyinfo
, parms
, Qparent_id
, NULL
, NULL
, RES_TYPE_NUMBER
);
3218 if (EQ (parent
, Qunbound
))
3220 if (! NILP (parent
))
3221 CHECK_NUMBER (parent
);
3223 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3224 /* No need to protect DISPLAY because that's not used after passing
3225 it to make_frame_without_minibuffer. */
3227 GCPRO4 (parms
, parent
, name
, frame
);
3228 tem
= x_get_arg (dpyinfo
, parms
, Qminibuffer
, "minibuffer", "Minibuffer",
3230 if (EQ (tem
, Qnone
) || NILP (tem
))
3231 f
= make_frame_without_minibuffer (Qnil
, kb
, display
);
3232 else if (EQ (tem
, Qonly
))
3234 f
= make_minibuffer_frame ();
3235 minibuffer_only
= 1;
3237 else if (WINDOWP (tem
))
3238 f
= make_frame_without_minibuffer (tem
, kb
, display
);
3242 XSETFRAME (frame
, f
);
3244 /* Note that X Windows does support scroll bars. */
3245 FRAME_CAN_HAVE_SCROLL_BARS (f
) = 1;
3247 f
->output_method
= output_x_window
;
3248 f
->output_data
.x
= (struct x_output
*) xmalloc (sizeof (struct x_output
));
3249 bzero (f
->output_data
.x
, sizeof (struct x_output
));
3250 f
->output_data
.x
->icon_bitmap
= -1;
3251 FRAME_FONTSET (f
) = -1;
3252 f
->output_data
.x
->scroll_bar_foreground_pixel
= -1;
3253 f
->output_data
.x
->scroll_bar_background_pixel
= -1;
3254 #ifdef USE_TOOLKIT_SCROLL_BARS
3255 f
->output_data
.x
->scroll_bar_top_shadow_pixel
= -1;
3256 f
->output_data
.x
->scroll_bar_bottom_shadow_pixel
= -1;
3257 #endif /* USE_TOOLKIT_SCROLL_BARS */
3258 record_unwind_protect (unwind_create_frame
, frame
);
3261 = x_get_arg (dpyinfo
, parms
, Qicon_name
, "iconName", "Title",
3263 if (! STRINGP (f
->icon_name
))
3264 f
->icon_name
= Qnil
;
3266 FRAME_X_DISPLAY_INFO (f
) = dpyinfo
;
3268 image_cache_refcount
= FRAME_X_IMAGE_CACHE (f
)->refcount
;
3269 dpyinfo_refcount
= dpyinfo
->reference_count
;
3270 #endif /* GLYPH_DEBUG */
3272 FRAME_KBOARD (f
) = kb
;
3275 /* These colors will be set anyway later, but it's important
3276 to get the color reference counts right, so initialize them! */
3279 struct gcpro gcpro1
;
3281 /* Function x_decode_color can signal an error. Make
3282 sure to initialize color slots so that we won't try
3283 to free colors we haven't allocated. */
3284 f
->output_data
.x
->foreground_pixel
= -1;
3285 f
->output_data
.x
->background_pixel
= -1;
3286 f
->output_data
.x
->cursor_pixel
= -1;
3287 f
->output_data
.x
->cursor_foreground_pixel
= -1;
3288 f
->output_data
.x
->border_pixel
= -1;
3289 f
->output_data
.x
->mouse_pixel
= -1;
3291 black
= build_string ("black");
3293 f
->output_data
.x
->foreground_pixel
3294 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
3295 f
->output_data
.x
->background_pixel
3296 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
3297 f
->output_data
.x
->cursor_pixel
3298 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
3299 f
->output_data
.x
->cursor_foreground_pixel
3300 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
3301 f
->output_data
.x
->border_pixel
3302 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
3303 f
->output_data
.x
->mouse_pixel
3304 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
3308 /* Specify the parent under which to make this X window. */
3312 f
->output_data
.x
->parent_desc
= (Window
) XFASTINT (parent
);
3313 f
->output_data
.x
->explicit_parent
= 1;
3317 f
->output_data
.x
->parent_desc
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
3318 f
->output_data
.x
->explicit_parent
= 0;
3321 /* Set the name; the functions to which we pass f expect the name to
3323 if (EQ (name
, Qunbound
) || NILP (name
))
3325 f
->name
= build_string (dpyinfo
->x_id_name
);
3326 f
->explicit_name
= 0;
3331 f
->explicit_name
= 1;
3332 /* use the frame's title when getting resources for this frame. */
3333 specbind (Qx_resource_name
, name
);
3336 /* Extract the window parameters from the supplied values
3337 that are needed to determine window geometry. */
3341 font
= x_get_arg (dpyinfo
, parms
, Qfont
, "font", "Font", RES_TYPE_STRING
);
3344 /* First, try whatever font the caller has specified. */
3347 tem
= Fquery_fontset (font
, Qnil
);
3349 font
= x_new_fontset (f
, SDATA (tem
));
3351 font
= x_new_font (f
, SDATA (font
));
3354 /* Try out a font which we hope has bold and italic variations. */
3355 if (!STRINGP (font
))
3356 font
= x_new_font (f
, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
3357 if (!STRINGP (font
))
3358 font
= x_new_font (f
, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
3359 if (! STRINGP (font
))
3360 font
= x_new_font (f
, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
3361 if (! STRINGP (font
))
3362 /* This was formerly the first thing tried, but it finds too many fonts
3363 and takes too long. */
3364 font
= x_new_font (f
, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
3365 /* If those didn't work, look for something which will at least work. */
3366 if (! STRINGP (font
))
3367 font
= x_new_font (f
, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
3369 if (! STRINGP (font
))
3370 font
= build_string ("fixed");
3372 x_default_parameter (f
, parms
, Qfont
, font
,
3373 "font", "Font", RES_TYPE_STRING
);
3377 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
3378 whereby it fails to get any font. */
3379 xlwmenu_default_font
= FRAME_FONT (f
);
3382 x_default_parameter (f
, parms
, Qborder_width
, make_number (2),
3383 "borderWidth", "BorderWidth", RES_TYPE_NUMBER
);
3385 /* This defaults to 1 in order to match xterm. We recognize either
3386 internalBorderWidth or internalBorder (which is what xterm calls
3388 if (NILP (Fassq (Qinternal_border_width
, parms
)))
3392 value
= x_get_arg (dpyinfo
, parms
, Qinternal_border_width
,
3393 "internalBorder", "internalBorder", RES_TYPE_NUMBER
);
3394 if (! EQ (value
, Qunbound
))
3395 parms
= Fcons (Fcons (Qinternal_border_width
, value
),
3398 x_default_parameter (f
, parms
, Qinternal_border_width
, make_number (1),
3399 "internalBorderWidth", "internalBorderWidth",
3401 x_default_parameter (f
, parms
, Qvertical_scroll_bars
, Qleft
,
3402 "verticalScrollBars", "ScrollBars",
3405 /* Also do the stuff which must be set before the window exists. */
3406 x_default_parameter (f
, parms
, Qforeground_color
, build_string ("black"),
3407 "foreground", "Foreground", RES_TYPE_STRING
);
3408 x_default_parameter (f
, parms
, Qbackground_color
, build_string ("white"),
3409 "background", "Background", RES_TYPE_STRING
);
3410 x_default_parameter (f
, parms
, Qmouse_color
, build_string ("black"),
3411 "pointerColor", "Foreground", RES_TYPE_STRING
);
3412 x_default_parameter (f
, parms
, Qcursor_color
, build_string ("black"),
3413 "cursorColor", "Foreground", RES_TYPE_STRING
);
3414 x_default_parameter (f
, parms
, Qborder_color
, build_string ("black"),
3415 "borderColor", "BorderColor", RES_TYPE_STRING
);
3416 x_default_parameter (f
, parms
, Qscreen_gamma
, Qnil
,
3417 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT
);
3418 x_default_parameter (f
, parms
, Qline_spacing
, Qnil
,
3419 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER
);
3420 x_default_parameter (f
, parms
, Qleft_fringe
, Qnil
,
3421 "leftFringe", "LeftFringe", RES_TYPE_NUMBER
);
3422 x_default_parameter (f
, parms
, Qright_fringe
, Qnil
,
3423 "rightFringe", "RightFringe", RES_TYPE_NUMBER
);
3425 x_default_scroll_bar_color_parameter (f
, parms
, Qscroll_bar_foreground
,
3426 "scrollBarForeground",
3427 "ScrollBarForeground", 1);
3428 x_default_scroll_bar_color_parameter (f
, parms
, Qscroll_bar_background
,
3429 "scrollBarBackground",
3430 "ScrollBarBackground", 0);
3432 /* Init faces before x_default_parameter is called for scroll-bar
3433 parameters because that function calls x_set_scroll_bar_width,
3434 which calls change_frame_size, which calls Fset_window_buffer,
3435 which runs hooks, which call Fvertical_motion. At the end, we
3436 end up in init_iterator with a null face cache, which should not
3438 init_frame_faces (f
);
3440 x_default_parameter (f
, parms
, Qmenu_bar_lines
, make_number (1),
3441 "menuBar", "MenuBar", RES_TYPE_NUMBER
);
3442 x_default_parameter (f
, parms
, Qtool_bar_lines
, make_number (1),
3443 "toolBar", "ToolBar", RES_TYPE_NUMBER
);
3444 x_default_parameter (f
, parms
, Qbuffer_predicate
, Qnil
,
3445 "bufferPredicate", "BufferPredicate",
3447 x_default_parameter (f
, parms
, Qtitle
, Qnil
,
3448 "title", "Title", RES_TYPE_STRING
);
3449 x_default_parameter (f
, parms
, Qwait_for_wm
, Qt
,
3450 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN
);
3451 x_default_parameter (f
, parms
, Qfullscreen
, Qnil
,
3452 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL
);
3454 f
->output_data
.x
->parent_desc
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
3456 /* Compute the size of the X window. */
3457 window_prompting
= x_figure_window_size (f
, parms
, 1);
3459 tem
= x_get_arg (dpyinfo
, parms
, Qunsplittable
, 0, 0, RES_TYPE_BOOLEAN
);
3460 f
->no_split
= minibuffer_only
|| EQ (tem
, Qt
);
3462 /* Create the X widget or window. */
3463 #ifdef USE_X_TOOLKIT
3464 x_window (f
, window_prompting
, minibuffer_only
);
3472 /* Now consider the frame official. */
3473 FRAME_X_DISPLAY_INFO (f
)->reference_count
++;
3474 Vframe_list
= Fcons (frame
, Vframe_list
);
3476 /* We need to do this after creating the X window, so that the
3477 icon-creation functions can say whose icon they're describing. */
3478 x_default_parameter (f
, parms
, Qicon_type
, Qnil
,
3479 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL
);
3481 x_default_parameter (f
, parms
, Qauto_raise
, Qnil
,
3482 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN
);
3483 x_default_parameter (f
, parms
, Qauto_lower
, Qnil
,
3484 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN
);
3485 x_default_parameter (f
, parms
, Qcursor_type
, Qbox
,
3486 "cursorType", "CursorType", RES_TYPE_SYMBOL
);
3487 x_default_parameter (f
, parms
, Qscroll_bar_width
, Qnil
,
3488 "scrollBarWidth", "ScrollBarWidth",
3491 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3492 Change will not be effected unless different from the current
3494 width
= FRAME_COLS (f
);
3495 height
= FRAME_LINES (f
);
3497 SET_FRAME_COLS (f
, 0);
3498 FRAME_LINES (f
) = 0;
3499 change_frame_size (f
, height
, width
, 1, 0, 0);
3501 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3502 /* Create the menu bar. */
3503 if (!minibuffer_only
&& FRAME_EXTERNAL_MENU_BAR (f
))
3505 /* If this signals an error, we haven't set size hints for the
3506 frame and we didn't make it visible. */
3507 initialize_frame_menubar (f
);
3510 /* This is a no-op, except under Motif where it arranges the
3511 main window for the widgets on it. */
3512 lw_set_main_areas (f
->output_data
.x
->column_widget
,
3513 f
->output_data
.x
->menubar_widget
,
3514 f
->output_data
.x
->edit_widget
);
3515 #endif /* not USE_GTK */
3517 #endif /* USE_X_TOOLKIT || USE_GTK */
3519 /* Tell the server what size and position, etc, we want, and how
3520 badly we want them. This should be done after we have the menu
3521 bar so that its size can be taken into account. */
3523 x_wm_set_size_hint (f
, window_prompting
, 0);
3526 /* Make the window appear on the frame and enable display, unless
3527 the caller says not to. However, with explicit parent, Emacs
3528 cannot control visibility, so don't try. */
3529 if (! f
->output_data
.x
->explicit_parent
)
3531 Lisp_Object visibility
;
3533 visibility
= x_get_arg (dpyinfo
, parms
, Qvisibility
, 0, 0,
3535 if (EQ (visibility
, Qunbound
))
3538 if (EQ (visibility
, Qicon
))
3539 x_iconify_frame (f
);
3540 else if (! NILP (visibility
))
3541 x_make_frame_visible (f
);
3543 /* Must have been Qnil. */
3547 /* Set the WM leader property. GTK does this itself, so this is not
3548 needed when using GTK. */
3549 if (dpyinfo
->client_leader_window
!= 0)
3552 XChangeProperty (FRAME_X_DISPLAY (f
),
3553 FRAME_OUTER_WINDOW (f
),
3554 dpyinfo
->Xatom_wm_client_leader
,
3555 XA_WINDOW
, 32, PropModeReplace
,
3556 (char *) &dpyinfo
->client_leader_window
, 1);
3562 /* Make sure windows on this frame appear in calls to next-window
3563 and similar functions. */
3564 Vwindow_list
= Qnil
;
3566 return unbind_to (count
, frame
);
3570 /* FRAME is used only to get a handle on the X display. We don't pass the
3571 display info directly because we're called from frame.c, which doesn't
3572 know about that structure. */
3575 x_get_focus_frame (frame
)
3576 struct frame
*frame
;
3578 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (frame
);
3580 if (! dpyinfo
->x_focus_frame
)
3583 XSETFRAME (xfocus
, dpyinfo
->x_focus_frame
);
3588 /* In certain situations, when the window manager follows a
3589 click-to-focus policy, there seems to be no way around calling
3590 XSetInputFocus to give another frame the input focus .
3592 In an ideal world, XSetInputFocus should generally be avoided so
3593 that applications don't interfere with the window manager's focus
3594 policy. But I think it's okay to use when it's clearly done
3595 following a user-command. */
3597 DEFUN ("x-focus-frame", Fx_focus_frame
, Sx_focus_frame
, 1, 1, 0,
3598 doc
: /* Set the input focus to FRAME.
3599 FRAME nil means use the selected frame. */)
3603 struct frame
*f
= check_x_frame (frame
);
3604 Display
*dpy
= FRAME_X_DISPLAY (f
);
3608 count
= x_catch_errors (dpy
);
3609 XSetInputFocus (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
3610 RevertToParent
, CurrentTime
);
3611 x_uncatch_errors (dpy
, count
);
3618 DEFUN ("xw-color-defined-p", Fxw_color_defined_p
, Sxw_color_defined_p
, 1, 2, 0,
3619 doc
: /* Internal function called by `color-defined-p', which see. */)
3621 Lisp_Object color
, frame
;
3624 FRAME_PTR f
= check_x_frame (frame
);
3626 CHECK_STRING (color
);
3628 if (x_defined_color (f
, SDATA (color
), &foo
, 0))
3634 DEFUN ("xw-color-values", Fxw_color_values
, Sxw_color_values
, 1, 2, 0,
3635 doc
: /* Internal function called by `color-values', which see. */)
3637 Lisp_Object color
, frame
;
3640 FRAME_PTR f
= check_x_frame (frame
);
3642 CHECK_STRING (color
);
3644 if (x_defined_color (f
, SDATA (color
), &foo
, 0))
3648 rgb
[0] = make_number (foo
.red
);
3649 rgb
[1] = make_number (foo
.green
);
3650 rgb
[2] = make_number (foo
.blue
);
3651 return Flist (3, rgb
);
3657 DEFUN ("xw-display-color-p", Fxw_display_color_p
, Sxw_display_color_p
, 0, 1, 0,
3658 doc
: /* Internal function called by `display-color-p', which see. */)
3660 Lisp_Object display
;
3662 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3664 if (dpyinfo
->n_planes
<= 2)
3667 switch (dpyinfo
->visual
->class)
3680 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p
, Sx_display_grayscale_p
,
3682 doc
: /* Return t if the X display supports shades of gray.
3683 Note that color displays do support shades of gray.
3684 The optional argument DISPLAY specifies which display to ask about.
3685 DISPLAY should be either a frame or a display name (a string).
3686 If omitted or nil, that stands for the selected frame's display. */)
3688 Lisp_Object display
;
3690 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3692 if (dpyinfo
->n_planes
<= 1)
3695 switch (dpyinfo
->visual
->class)
3710 DEFUN ("x-display-pixel-width", Fx_display_pixel_width
, Sx_display_pixel_width
,
3712 doc
: /* Returns the width in pixels of the X display DISPLAY.
3713 The optional argument DISPLAY specifies which display to ask about.
3714 DISPLAY should be either a frame or a display name (a string).
3715 If omitted or nil, that stands for the selected frame's display. */)
3717 Lisp_Object display
;
3719 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3721 return make_number (dpyinfo
->width
);
3724 DEFUN ("x-display-pixel-height", Fx_display_pixel_height
,
3725 Sx_display_pixel_height
, 0, 1, 0,
3726 doc
: /* Returns the height in pixels of the X display DISPLAY.
3727 The optional argument DISPLAY specifies which display to ask about.
3728 DISPLAY should be either a frame or a display name (a string).
3729 If omitted or nil, that stands for the selected frame's display. */)
3731 Lisp_Object display
;
3733 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3735 return make_number (dpyinfo
->height
);
3738 DEFUN ("x-display-planes", Fx_display_planes
, Sx_display_planes
,
3740 doc
: /* Returns the number of bitplanes of the X display DISPLAY.
3741 The optional argument DISPLAY specifies which display to ask about.
3742 DISPLAY should be either a frame or a display name (a string).
3743 If omitted or nil, that stands for the selected frame's display. */)
3745 Lisp_Object display
;
3747 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3749 return make_number (dpyinfo
->n_planes
);
3752 DEFUN ("x-display-color-cells", Fx_display_color_cells
, Sx_display_color_cells
,
3754 doc
: /* Returns the number of color cells of the X display DISPLAY.
3755 The optional argument DISPLAY specifies which display to ask about.
3756 DISPLAY should be either a frame or a display name (a string).
3757 If omitted or nil, that stands for the selected frame's display. */)
3759 Lisp_Object display
;
3761 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3763 return make_number (DisplayCells (dpyinfo
->display
,
3764 XScreenNumberOfScreen (dpyinfo
->screen
)));
3767 DEFUN ("x-server-max-request-size", Fx_server_max_request_size
,
3768 Sx_server_max_request_size
,
3770 doc
: /* Returns the maximum request size of the X server of display DISPLAY.
3771 The optional argument DISPLAY specifies which display to ask about.
3772 DISPLAY should be either a frame or a display name (a string).
3773 If omitted or nil, that stands for the selected frame's display. */)
3775 Lisp_Object display
;
3777 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3779 return make_number (MAXREQUEST (dpyinfo
->display
));
3782 DEFUN ("x-server-vendor", Fx_server_vendor
, Sx_server_vendor
, 0, 1, 0,
3783 doc
: /* Returns the vendor ID string of the X server of display DISPLAY.
3784 The optional argument DISPLAY specifies which display to ask about.
3785 DISPLAY should be either a frame or a display name (a string).
3786 If omitted or nil, that stands for the selected frame's display. */)
3788 Lisp_Object display
;
3790 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3791 char *vendor
= ServerVendor (dpyinfo
->display
);
3793 if (! vendor
) vendor
= "";
3794 return build_string (vendor
);
3797 DEFUN ("x-server-version", Fx_server_version
, Sx_server_version
, 0, 1, 0,
3798 doc
: /* Returns the version numbers of the X server of display DISPLAY.
3799 The value is a list of three integers: the major and minor
3800 version numbers of the X Protocol in use, and the vendor-specific release
3801 number. See also the function `x-server-vendor'.
3803 The optional argument DISPLAY specifies which display to ask about.
3804 DISPLAY should be either a frame or a display name (a string).
3805 If omitted or nil, that stands for the selected frame's display. */)
3807 Lisp_Object display
;
3809 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3810 Display
*dpy
= dpyinfo
->display
;
3812 return Fcons (make_number (ProtocolVersion (dpy
)),
3813 Fcons (make_number (ProtocolRevision (dpy
)),
3814 Fcons (make_number (VendorRelease (dpy
)), Qnil
)));
3817 DEFUN ("x-display-screens", Fx_display_screens
, Sx_display_screens
, 0, 1, 0,
3818 doc
: /* Return the number of screens on the X server of display DISPLAY.
3819 The optional argument DISPLAY specifies which display to ask about.
3820 DISPLAY should be either a frame or a display name (a string).
3821 If omitted or nil, that stands for the selected frame's display. */)
3823 Lisp_Object display
;
3825 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3827 return make_number (ScreenCount (dpyinfo
->display
));
3830 DEFUN ("x-display-mm-height", Fx_display_mm_height
, Sx_display_mm_height
, 0, 1, 0,
3831 doc
: /* Return the height in millimeters of the X display DISPLAY.
3832 The optional argument DISPLAY specifies which display to ask about.
3833 DISPLAY should be either a frame or a display name (a string).
3834 If omitted or nil, that stands for the selected frame's display. */)
3836 Lisp_Object display
;
3838 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3840 return make_number (HeightMMOfScreen (dpyinfo
->screen
));
3843 DEFUN ("x-display-mm-width", Fx_display_mm_width
, Sx_display_mm_width
, 0, 1, 0,
3844 doc
: /* Return the width in millimeters of the X display DISPLAY.
3845 The optional argument DISPLAY specifies which display to ask about.
3846 DISPLAY should be either a frame or a display name (a string).
3847 If omitted or nil, that stands for the selected frame's display. */)
3849 Lisp_Object display
;
3851 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3853 return make_number (WidthMMOfScreen (dpyinfo
->screen
));
3856 DEFUN ("x-display-backing-store", Fx_display_backing_store
,
3857 Sx_display_backing_store
, 0, 1, 0,
3858 doc
: /* Returns an indication of whether X display DISPLAY does backing store.
3859 The value may be `always', `when-mapped', or `not-useful'.
3860 The optional argument DISPLAY specifies which display to ask about.
3861 DISPLAY should be either a frame or a display name (a string).
3862 If omitted or nil, that stands for the selected frame's display. */)
3864 Lisp_Object display
;
3866 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3869 switch (DoesBackingStore (dpyinfo
->screen
))
3872 result
= intern ("always");
3876 result
= intern ("when-mapped");
3880 result
= intern ("not-useful");
3884 error ("Strange value for BackingStore parameter of screen");
3891 DEFUN ("x-display-visual-class", Fx_display_visual_class
,
3892 Sx_display_visual_class
, 0, 1, 0,
3893 doc
: /* Return the visual class of the X display DISPLAY.
3894 The value is one of the symbols `static-gray', `gray-scale',
3895 `static-color', `pseudo-color', `true-color', or `direct-color'.
3897 The optional argument DISPLAY specifies which display to ask about.
3898 DISPLAY should be either a frame or a display name (a string).
3899 If omitted or nil, that stands for the selected frame's display. */)
3901 Lisp_Object display
;
3903 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3906 switch (dpyinfo
->visual
->class)
3909 result
= intern ("static-gray");
3912 result
= intern ("gray-scale");
3915 result
= intern ("static-color");
3918 result
= intern ("pseudo-color");
3921 result
= intern ("true-color");
3924 result
= intern ("direct-color");
3927 error ("Display has an unknown visual class");
3934 DEFUN ("x-display-save-under", Fx_display_save_under
,
3935 Sx_display_save_under
, 0, 1, 0,
3936 doc
: /* Returns t if the X display DISPLAY supports the save-under feature.
3937 The optional argument DISPLAY specifies which display to ask about.
3938 DISPLAY should be either a frame or a display name (a string).
3939 If omitted or nil, that stands for the selected frame's display. */)
3941 Lisp_Object display
;
3943 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
3945 if (DoesSaveUnders (dpyinfo
->screen
) == True
)
3953 register struct frame
*f
;
3955 return FRAME_PIXEL_WIDTH (f
);
3960 register struct frame
*f
;
3962 return FRAME_PIXEL_HEIGHT (f
);
3967 register struct frame
*f
;
3969 return FRAME_COLUMN_WIDTH (f
);
3974 register struct frame
*f
;
3976 return FRAME_LINE_HEIGHT (f
);
3981 register struct frame
*f
;
3983 return FRAME_X_DISPLAY_INFO (f
)->n_planes
;
3988 /************************************************************************
3990 ************************************************************************/
3993 /* Mapping visual names to visuals. */
3995 static struct visual_class
4002 {"StaticGray", StaticGray
},
4003 {"GrayScale", GrayScale
},
4004 {"StaticColor", StaticColor
},
4005 {"PseudoColor", PseudoColor
},
4006 {"TrueColor", TrueColor
},
4007 {"DirectColor", DirectColor
},
4012 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4014 /* Value is the screen number of screen SCR. This is a substitute for
4015 the X function with the same name when that doesn't exist. */
4018 XScreenNumberOfScreen (scr
)
4019 register Screen
*scr
;
4021 Display
*dpy
= scr
->display
;
4024 for (i
= 0; i
< dpy
->nscreens
; ++i
)
4025 if (scr
== dpy
->screens
+ i
)
4031 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4034 /* Select the visual that should be used on display DPYINFO. Set
4035 members of DPYINFO appropriately. Called from x_term_init. */
4038 select_visual (dpyinfo
)
4039 struct x_display_info
*dpyinfo
;
4041 Display
*dpy
= dpyinfo
->display
;
4042 Screen
*screen
= dpyinfo
->screen
;
4045 /* See if a visual is specified. */
4046 value
= display_x_get_resource (dpyinfo
,
4047 build_string ("visualClass"),
4048 build_string ("VisualClass"),
4050 if (STRINGP (value
))
4052 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4053 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4054 depth, a decimal number. NAME is compared with case ignored. */
4055 char *s
= (char *) alloca (SBYTES (value
) + 1);
4060 strcpy (s
, SDATA (value
));
4061 dash
= index (s
, '-');
4064 dpyinfo
->n_planes
= atoi (dash
+ 1);
4068 /* We won't find a matching visual with depth 0, so that
4069 an error will be printed below. */
4070 dpyinfo
->n_planes
= 0;
4072 /* Determine the visual class. */
4073 for (i
= 0; visual_classes
[i
].name
; ++i
)
4074 if (xstricmp (s
, visual_classes
[i
].name
) == 0)
4076 class = visual_classes
[i
].class;
4080 /* Look up a matching visual for the specified class. */
4082 || !XMatchVisualInfo (dpy
, XScreenNumberOfScreen (screen
),
4083 dpyinfo
->n_planes
, class, &vinfo
))
4084 fatal ("Invalid visual specification `%s'", SDATA (value
));
4086 dpyinfo
->visual
= vinfo
.visual
;
4091 XVisualInfo
*vinfo
, vinfo_template
;
4093 dpyinfo
->visual
= DefaultVisualOfScreen (screen
);
4096 vinfo_template
.visualid
= XVisualIDFromVisual (dpyinfo
->visual
);
4098 vinfo_template
.visualid
= dpyinfo
->visual
->visualid
;
4100 vinfo_template
.screen
= XScreenNumberOfScreen (screen
);
4101 vinfo
= XGetVisualInfo (dpy
, VisualIDMask
| VisualScreenMask
,
4102 &vinfo_template
, &n_visuals
);
4104 fatal ("Can't get proper X visual info");
4106 dpyinfo
->n_planes
= vinfo
->depth
;
4107 XFree ((char *) vinfo
);
4112 /* Return the X display structure for the display named NAME.
4113 Open a new connection if necessary. */
4115 struct x_display_info
*
4116 x_display_info_for_name (name
)
4120 struct x_display_info
*dpyinfo
;
4122 CHECK_STRING (name
);
4124 if (! EQ (Vwindow_system
, intern ("x")))
4125 error ("Not using X Windows");
4127 for (dpyinfo
= x_display_list
, names
= x_display_name_list
;
4129 dpyinfo
= dpyinfo
->next
, names
= XCDR (names
))
4132 tem
= Fstring_equal (XCAR (XCAR (names
)), name
);
4137 /* Use this general default value to start with. */
4138 Vx_resource_name
= Vinvocation_name
;
4140 validate_x_resource_name ();
4142 dpyinfo
= x_term_init (name
, (char *)0,
4143 (char *) SDATA (Vx_resource_name
));
4146 error ("Cannot connect to X server %s", SDATA (name
));
4149 XSETFASTINT (Vwindow_system_version
, 11);
4155 DEFUN ("x-open-connection", Fx_open_connection
, Sx_open_connection
,
4157 doc
: /* Open a connection to an X server.
4158 DISPLAY is the name of the display to connect to.
4159 Optional second arg XRM-STRING is a string of resources in xrdb format.
4160 If the optional third arg MUST-SUCCEED is non-nil,
4161 terminate Emacs if we can't open the connection. */)
4162 (display
, xrm_string
, must_succeed
)
4163 Lisp_Object display
, xrm_string
, must_succeed
;
4165 unsigned char *xrm_option
;
4166 struct x_display_info
*dpyinfo
;
4168 CHECK_STRING (display
);
4169 if (! NILP (xrm_string
))
4170 CHECK_STRING (xrm_string
);
4172 if (! EQ (Vwindow_system
, intern ("x")))
4173 error ("Not using X Windows");
4175 if (! NILP (xrm_string
))
4176 xrm_option
= (unsigned char *) SDATA (xrm_string
);
4178 xrm_option
= (unsigned char *) 0;
4180 validate_x_resource_name ();
4182 /* This is what opens the connection and sets x_current_display.
4183 This also initializes many symbols, such as those used for input. */
4184 dpyinfo
= x_term_init (display
, xrm_option
,
4185 (char *) SDATA (Vx_resource_name
));
4189 if (!NILP (must_succeed
))
4190 fatal ("Cannot connect to X server %s.\n\
4191 Check the DISPLAY environment variable or use `-d'.\n\
4192 Also use the `xauth' program to verify that you have the proper\n\
4193 authorization information needed to connect the X server.\n\
4194 An insecure way to solve the problem may be to use `xhost'.\n",
4197 error ("Cannot connect to X server %s", SDATA (display
));
4202 XSETFASTINT (Vwindow_system_version
, 11);
4206 DEFUN ("x-close-connection", Fx_close_connection
,
4207 Sx_close_connection
, 1, 1, 0,
4208 doc
: /* Close the connection to DISPLAY's X server.
4209 For DISPLAY, specify either a frame or a display name (a string).
4210 If DISPLAY is nil, that stands for the selected frame's display. */)
4212 Lisp_Object display
;
4214 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
4217 if (dpyinfo
->reference_count
> 0)
4218 error ("Display still has frames on it");
4221 /* Free the fonts in the font table. */
4222 for (i
= 0; i
< dpyinfo
->n_fonts
; i
++)
4223 if (dpyinfo
->font_table
[i
].name
)
4225 XFreeFont (dpyinfo
->display
, dpyinfo
->font_table
[i
].font
);
4228 x_destroy_all_bitmaps (dpyinfo
);
4229 XSetCloseDownMode (dpyinfo
->display
, DestroyAll
);
4231 #ifdef USE_X_TOOLKIT
4232 XtCloseDisplay (dpyinfo
->display
);
4234 XCloseDisplay (dpyinfo
->display
);
4237 x_delete_display (dpyinfo
);
4243 DEFUN ("x-display-list", Fx_display_list
, Sx_display_list
, 0, 0, 0,
4244 doc
: /* Return the list of display names that Emacs has connections to. */)
4247 Lisp_Object tail
, result
;
4250 for (tail
= x_display_name_list
; ! NILP (tail
); tail
= XCDR (tail
))
4251 result
= Fcons (XCAR (XCAR (tail
)), result
);
4256 DEFUN ("x-synchronize", Fx_synchronize
, Sx_synchronize
, 1, 2, 0,
4257 doc
: /* If ON is non-nil, report X errors as soon as the erring request is made.
4258 If ON is nil, allow buffering of requests.
4259 Turning on synchronization prohibits the Xlib routines from buffering
4260 requests and seriously degrades performance, but makes debugging much
4262 The optional second argument DISPLAY specifies which display to act on.
4263 DISPLAY should be either a frame or a display name (a string).
4264 If DISPLAY is omitted or nil, that stands for the selected frame's display. */)
4266 Lisp_Object display
, on
;
4268 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
4270 XSynchronize (dpyinfo
->display
, !EQ (on
, Qnil
));
4275 /* Wait for responses to all X commands issued so far for frame F. */
4282 XSync (FRAME_X_DISPLAY (f
), False
);
4287 /***********************************************************************
4288 General X functions exposed to Elisp.
4289 ***********************************************************************/
4291 DEFUN ("x-send-client-message", Fx_send_client_event
,
4292 Sx_send_client_message
, 6, 6, 0,
4293 doc
: /* Send a client message of MESSAGE-TYPE to window DEST on DISPLAY.
4295 For DISPLAY, specify either a frame or a display name (a string).
4296 If DISPLAY is nil, that stands for the selected frame's display.
4297 DEST may be an integer, in which case it is a Window id. The value 0 may
4298 be used to send to the root window of the DISPLAY.
4299 If DEST is a frame the event is sent to the outer window of that frame.
4300 Nil means the currently selected frame.
4301 If DEST is the string "PointerWindow" the event is sent to the window that
4302 contains the pointer. If DEST is the string "InputFocus" the event is
4303 sent to the window that has the input focus.
4304 FROM is the frame sending the event. Use nil for currently selected frame.
4305 MESSAGE-TYPE is the name of an Atom as a string.
4306 FORMAT must be one of 8, 16 or 32 and determines the size of the values in
4307 bits. VALUES is a list of integer and/or strings containing the values to
4308 send. If a value is a string, it is converted to an Atom and the value of
4309 the Atom is sent. If more values than fits into the event is given,
4310 the excessive values are ignored. */)
4311 (display
, dest
, from
, message_type
, format
, values
)
4312 Lisp_Object display
, dest
, from
, message_type
, format
, values
;
4314 struct x_display_info
*dpyinfo
= check_x_display_info (display
);
4319 int max_nr_values
= (int) sizeof (event
.xclient
.data
.b
);
4320 struct frame
*f
= check_x_frame (from
);
4322 CHECK_STRING (message_type
);
4323 CHECK_NUMBER (format
);
4324 CHECK_CONS (values
);
4326 for (cons
= values
; CONSP (cons
); cons
= XCDR (cons
))
4328 Lisp_Object o
= XCAR (cons
);
4330 if (! INTEGERP (o
) && ! STRINGP (o
))
4331 error ("Bad data in VALUES, must be integer or string");
4334 event
.xclient
.type
= ClientMessage
;
4335 event
.xclient
.format
= XFASTINT (format
);
4337 if (event
.xclient
.format
!= 8 && event
.xclient
.format
!= 16
4338 && event
.xclient
.format
!= 32)
4339 error ("FORMAT must be one of 8, 16 or 32");
4340 if (event
.xclient
.format
== 16) max_nr_values
/= 2;
4341 if (event
.xclient
.format
== 32) max_nr_values
/= 4;
4343 if (FRAMEP (dest
) || NILP (dest
))
4345 struct frame
*fdest
= check_x_frame (dest
);
4346 wdest
= FRAME_OUTER_WINDOW (fdest
);
4348 else if (STRINGP (dest
))
4350 if (strcmp (SDATA (dest
), "PointerWindow") == 0)
4351 wdest
= PointerWindow
;
4352 else if (strcmp (SDATA (dest
), "InputFocus") == 0)
4355 error ("DEST as a string must be one of PointerWindow or InputFocus");
4359 CHECK_NUMBER (dest
);
4360 wdest
= (Window
) XFASTINT (dest
);
4361 if (wdest
== 0) wdest
= dpyinfo
->root_window
;
4365 for (cons
= values
, i
= 0;
4366 CONSP (cons
) && i
< max_nr_values
;
4367 cons
= XCDR (cons
), ++i
)
4369 Lisp_Object o
= XCAR (cons
);
4374 else if (STRINGP (o
))
4375 val
= XInternAtom (dpyinfo
->display
, SDATA (o
), False
);
4377 if (event
.xclient
.format
== 8)
4378 event
.xclient
.data
.b
[i
] = (char) val
;
4379 else if (event
.xclient
.format
== 16)
4380 event
.xclient
.data
.s
[i
] = (short) val
;
4382 event
.xclient
.data
.l
[i
] = val
;
4385 for ( ; i
< max_nr_values
; ++i
)
4386 if (event
.xclient
.format
== 8)
4387 event
.xclient
.data
.b
[i
] = 0;
4388 else if (event
.xclient
.format
== 16)
4389 event
.xclient
.data
.s
[i
] = 0;
4391 event
.xclient
.data
.l
[i
] = 0;
4393 event
.xclient
.message_type
4394 = XInternAtom (dpyinfo
->display
, SDATA (message_type
), False
);
4395 event
.xclient
.display
= dpyinfo
->display
;
4396 event
.xclient
.window
= FRAME_OUTER_WINDOW (f
);
4398 XSendEvent (dpyinfo
->display
, wdest
, False
, 0xffff, &event
);
4400 XFlush (dpyinfo
->display
);
4406 /***********************************************************************
4408 ***********************************************************************/
4410 /* Value is the number of elements of vector VECTOR. */
4412 #define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
4414 /* List of supported image types. Use define_image_type to add new
4415 types. Use lookup_image_type to find a type for a given symbol. */
4417 static struct image_type
*image_types
;
4419 /* The symbol `image' which is the car of the lists used to represent
4422 extern Lisp_Object Qimage
;
4424 /* The symbol `xbm' which is used as the type symbol for XBM images. */
4430 extern Lisp_Object QCwidth
, QCheight
, QCforeground
, QCbackground
, QCfile
;
4431 extern Lisp_Object QCdata
, QCtype
;
4432 Lisp_Object QCascent
, QCmargin
, QCrelief
;
4433 Lisp_Object QCconversion
, QCcolor_symbols
, QCheuristic_mask
;
4434 Lisp_Object QCindex
, QCmatrix
, QCcolor_adjustment
, QCmask
;
4436 /* Other symbols. */
4438 Lisp_Object Qlaplace
, Qemboss
, Qedge_detection
, Qheuristic
;
4440 /* Time in seconds after which images should be removed from the cache
4441 if not displayed. */
4443 Lisp_Object Vimage_cache_eviction_delay
;
4445 /* Function prototypes. */
4447 static void define_image_type
P_ ((struct image_type
*type
));
4448 static struct image_type
*lookup_image_type
P_ ((Lisp_Object symbol
));
4449 static void image_error
P_ ((char *format
, Lisp_Object
, Lisp_Object
));
4450 static void x_laplace
P_ ((struct frame
*, struct image
*));
4451 static void x_emboss
P_ ((struct frame
*, struct image
*));
4452 static int x_build_heuristic_mask
P_ ((struct frame
*, struct image
*,
4456 /* Define a new image type from TYPE. This adds a copy of TYPE to
4457 image_types and adds the symbol *TYPE->type to Vimage_types. */
4460 define_image_type (type
)
4461 struct image_type
*type
;
4463 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
4464 The initialized data segment is read-only. */
4465 struct image_type
*p
= (struct image_type
*) xmalloc (sizeof *p
);
4466 bcopy (type
, p
, sizeof *p
);
4467 p
->next
= image_types
;
4469 Vimage_types
= Fcons (*p
->type
, Vimage_types
);
4473 /* Look up image type SYMBOL, and return a pointer to its image_type
4474 structure. Value is null if SYMBOL is not a known image type. */
4476 static INLINE
struct image_type
*
4477 lookup_image_type (symbol
)
4480 struct image_type
*type
;
4482 for (type
= image_types
; type
; type
= type
->next
)
4483 if (EQ (symbol
, *type
->type
))
4490 /* Value is non-zero if OBJECT is a valid Lisp image specification. A
4491 valid image specification is a list whose car is the symbol
4492 `image', and whose rest is a property list. The property list must
4493 contain a value for key `:type'. That value must be the name of a
4494 supported image type. The rest of the property list depends on the
4498 valid_image_p (object
)
4503 if (CONSP (object
) && EQ (XCAR (object
), Qimage
))
4507 for (tem
= XCDR (object
); CONSP (tem
); tem
= XCDR (tem
))
4508 if (EQ (XCAR (tem
), QCtype
))
4511 if (CONSP (tem
) && SYMBOLP (XCAR (tem
)))
4513 struct image_type
*type
;
4514 type
= lookup_image_type (XCAR (tem
));
4516 valid_p
= type
->valid_p (object
);
4527 /* Log error message with format string FORMAT and argument ARG.
4528 Signaling an error, e.g. when an image cannot be loaded, is not a
4529 good idea because this would interrupt redisplay, and the error
4530 message display would lead to another redisplay. This function
4531 therefore simply displays a message. */
4534 image_error (format
, arg1
, arg2
)
4536 Lisp_Object arg1
, arg2
;
4538 add_to_log (format
, arg1
, arg2
);
4543 /***********************************************************************
4544 Image specifications
4545 ***********************************************************************/
4547 enum image_value_type
4549 IMAGE_DONT_CHECK_VALUE_TYPE
,
4551 IMAGE_STRING_OR_NIL_VALUE
,
4553 IMAGE_POSITIVE_INTEGER_VALUE
,
4554 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
,
4555 IMAGE_NON_NEGATIVE_INTEGER_VALUE
,
4557 IMAGE_INTEGER_VALUE
,
4558 IMAGE_FUNCTION_VALUE
,
4563 /* Structure used when parsing image specifications. */
4565 struct image_keyword
4567 /* Name of keyword. */
4570 /* The type of value allowed. */
4571 enum image_value_type type
;
4573 /* Non-zero means key must be present. */
4576 /* Used to recognize duplicate keywords in a property list. */
4579 /* The value that was found. */
4584 static int parse_image_spec
P_ ((Lisp_Object
, struct image_keyword
*,
4586 static Lisp_Object image_spec_value
P_ ((Lisp_Object
, Lisp_Object
, int *));
4589 /* Parse image spec SPEC according to KEYWORDS. A valid image spec
4590 has the format (image KEYWORD VALUE ...). One of the keyword/
4591 value pairs must be `:type TYPE'. KEYWORDS is a vector of
4592 image_keywords structures of size NKEYWORDS describing other
4593 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
4596 parse_image_spec (spec
, keywords
, nkeywords
, type
)
4598 struct image_keyword
*keywords
;
4605 if (!CONSP (spec
) || !EQ (XCAR (spec
), Qimage
))
4608 plist
= XCDR (spec
);
4609 while (CONSP (plist
))
4611 Lisp_Object key
, value
;
4613 /* First element of a pair must be a symbol. */
4615 plist
= XCDR (plist
);
4619 /* There must follow a value. */
4622 value
= XCAR (plist
);
4623 plist
= XCDR (plist
);
4625 /* Find key in KEYWORDS. Error if not found. */
4626 for (i
= 0; i
< nkeywords
; ++i
)
4627 if (strcmp (keywords
[i
].name
, SDATA (SYMBOL_NAME (key
))) == 0)
4633 /* Record that we recognized the keyword. If a keywords
4634 was found more than once, it's an error. */
4635 keywords
[i
].value
= value
;
4636 ++keywords
[i
].count
;
4638 if (keywords
[i
].count
> 1)
4641 /* Check type of value against allowed type. */
4642 switch (keywords
[i
].type
)
4644 case IMAGE_STRING_VALUE
:
4645 if (!STRINGP (value
))
4649 case IMAGE_STRING_OR_NIL_VALUE
:
4650 if (!STRINGP (value
) && !NILP (value
))
4654 case IMAGE_SYMBOL_VALUE
:
4655 if (!SYMBOLP (value
))
4659 case IMAGE_POSITIVE_INTEGER_VALUE
:
4660 if (!INTEGERP (value
) || XINT (value
) <= 0)
4664 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
:
4665 if (INTEGERP (value
) && XINT (value
) >= 0)
4668 && INTEGERP (XCAR (value
)) && INTEGERP (XCDR (value
))
4669 && XINT (XCAR (value
)) >= 0 && XINT (XCDR (value
)) >= 0)
4673 case IMAGE_ASCENT_VALUE
:
4674 if (SYMBOLP (value
) && EQ (value
, Qcenter
))
4676 else if (INTEGERP (value
)
4677 && XINT (value
) >= 0
4678 && XINT (value
) <= 100)
4682 case IMAGE_NON_NEGATIVE_INTEGER_VALUE
:
4683 if (!INTEGERP (value
) || XINT (value
) < 0)
4687 case IMAGE_DONT_CHECK_VALUE_TYPE
:
4690 case IMAGE_FUNCTION_VALUE
:
4691 value
= indirect_function (value
);
4693 || COMPILEDP (value
)
4694 || (CONSP (value
) && EQ (XCAR (value
), Qlambda
)))
4698 case IMAGE_NUMBER_VALUE
:
4699 if (!INTEGERP (value
) && !FLOATP (value
))
4703 case IMAGE_INTEGER_VALUE
:
4704 if (!INTEGERP (value
))
4708 case IMAGE_BOOL_VALUE
:
4709 if (!NILP (value
) && !EQ (value
, Qt
))
4718 if (EQ (key
, QCtype
) && !EQ (type
, value
))
4722 /* Check that all mandatory fields are present. */
4723 for (i
= 0; i
< nkeywords
; ++i
)
4724 if (keywords
[i
].mandatory_p
&& keywords
[i
].count
== 0)
4727 return NILP (plist
);
4731 /* Return the value of KEY in image specification SPEC. Value is nil
4732 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
4733 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
4736 image_spec_value (spec
, key
, found
)
4737 Lisp_Object spec
, key
;
4742 xassert (valid_image_p (spec
));
4744 for (tail
= XCDR (spec
);
4745 CONSP (tail
) && CONSP (XCDR (tail
));
4746 tail
= XCDR (XCDR (tail
)))
4748 if (EQ (XCAR (tail
), key
))
4752 return XCAR (XCDR (tail
));
4762 DEFUN ("image-size", Fimage_size
, Simage_size
, 1, 3, 0,
4763 doc
: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
4764 PIXELS non-nil means return the size in pixels, otherwise return the
4765 size in canonical character units.
4766 FRAME is the frame on which the image will be displayed. FRAME nil
4767 or omitted means use the selected frame. */)
4768 (spec
, pixels
, frame
)
4769 Lisp_Object spec
, pixels
, frame
;
4774 if (valid_image_p (spec
))
4776 struct frame
*f
= check_x_frame (frame
);
4777 int id
= lookup_image (f
, spec
);
4778 struct image
*img
= IMAGE_FROM_ID (f
, id
);
4779 int width
= img
->width
+ 2 * img
->hmargin
;
4780 int height
= img
->height
+ 2 * img
->vmargin
;
4783 size
= Fcons (make_float ((double) width
/ FRAME_COLUMN_WIDTH (f
)),
4784 make_float ((double) height
/ FRAME_LINE_HEIGHT (f
)));
4786 size
= Fcons (make_number (width
), make_number (height
));
4789 error ("Invalid image specification");
4795 DEFUN ("image-mask-p", Fimage_mask_p
, Simage_mask_p
, 1, 2, 0,
4796 doc
: /* Return t if image SPEC has a mask bitmap.
4797 FRAME is the frame on which the image will be displayed. FRAME nil
4798 or omitted means use the selected frame. */)
4800 Lisp_Object spec
, frame
;
4805 if (valid_image_p (spec
))
4807 struct frame
*f
= check_x_frame (frame
);
4808 int id
= lookup_image (f
, spec
);
4809 struct image
*img
= IMAGE_FROM_ID (f
, id
);
4814 error ("Invalid image specification");
4821 /***********************************************************************
4822 Image type independent image structures
4823 ***********************************************************************/
4825 static struct image
*make_image
P_ ((Lisp_Object spec
, unsigned hash
));
4826 static void free_image
P_ ((struct frame
*f
, struct image
*img
));
4829 /* Allocate and return a new image structure for image specification
4830 SPEC. SPEC has a hash value of HASH. */
4832 static struct image
*
4833 make_image (spec
, hash
)
4837 struct image
*img
= (struct image
*) xmalloc (sizeof *img
);
4839 xassert (valid_image_p (spec
));
4840 bzero (img
, sizeof *img
);
4841 img
->type
= lookup_image_type (image_spec_value (spec
, QCtype
, NULL
));
4842 xassert (img
->type
!= NULL
);
4844 img
->data
.lisp_val
= Qnil
;
4845 img
->ascent
= DEFAULT_IMAGE_ASCENT
;
4851 /* Free image IMG which was used on frame F, including its resources. */
4860 struct image_cache
*c
= FRAME_X_IMAGE_CACHE (f
);
4862 /* Remove IMG from the hash table of its cache. */
4864 img
->prev
->next
= img
->next
;
4866 c
->buckets
[img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
] = img
->next
;
4869 img
->next
->prev
= img
->prev
;
4871 c
->images
[img
->id
] = NULL
;
4873 /* Free resources, then free IMG. */
4874 img
->type
->free (f
, img
);
4880 /* Prepare image IMG for display on frame F. Must be called before
4881 drawing an image. */
4884 prepare_image_for_display (f
, img
)
4890 /* We're about to display IMG, so set its timestamp to `now'. */
4892 img
->timestamp
= EMACS_SECS (t
);
4894 /* If IMG doesn't have a pixmap yet, load it now, using the image
4895 type dependent loader function. */
4896 if (img
->pixmap
== None
&& !img
->load_failed_p
)
4897 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
4901 /* Value is the number of pixels for the ascent of image IMG when
4902 drawn in face FACE. */
4905 image_ascent (img
, face
)
4909 int height
= img
->height
+ img
->vmargin
;
4912 if (img
->ascent
== CENTERED_IMAGE_ASCENT
)
4915 /* This expression is arranged so that if the image can't be
4916 exactly centered, it will be moved slightly up. This is
4917 because a typical font is `top-heavy' (due to the presence
4918 uppercase letters), so the image placement should err towards
4919 being top-heavy too. It also just generally looks better. */
4920 ascent
= (height
+ face
->font
->ascent
- face
->font
->descent
+ 1) / 2;
4922 ascent
= height
/ 2;
4925 ascent
= height
* img
->ascent
/ 100.0;
4931 /* Image background colors. */
4933 static unsigned long
4934 four_corners_best (ximg
, width
, height
)
4936 unsigned long width
, height
;
4938 unsigned long corners
[4], best
;
4941 /* Get the colors at the corners of ximg. */
4942 corners
[0] = XGetPixel (ximg
, 0, 0);
4943 corners
[1] = XGetPixel (ximg
, width
- 1, 0);
4944 corners
[2] = XGetPixel (ximg
, width
- 1, height
- 1);
4945 corners
[3] = XGetPixel (ximg
, 0, height
- 1);
4947 /* Choose the most frequently found color as background. */
4948 for (i
= best_count
= 0; i
< 4; ++i
)
4952 for (j
= n
= 0; j
< 4; ++j
)
4953 if (corners
[i
] == corners
[j
])
4957 best
= corners
[i
], best_count
= n
;
4963 /* Return the `background' field of IMG. If IMG doesn't have one yet,
4964 it is guessed heuristically. If non-zero, XIMG is an existing XImage
4965 object to use for the heuristic. */
4968 image_background (img
, f
, ximg
)
4973 if (! img
->background_valid
)
4974 /* IMG doesn't have a background yet, try to guess a reasonable value. */
4976 int free_ximg
= !ximg
;
4979 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
4980 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
4982 img
->background
= four_corners_best (ximg
, img
->width
, img
->height
);
4985 XDestroyImage (ximg
);
4987 img
->background_valid
= 1;
4990 return img
->background
;
4993 /* Return the `background_transparent' field of IMG. If IMG doesn't
4994 have one yet, it is guessed heuristically. If non-zero, MASK is an
4995 existing XImage object to use for the heuristic. */
4998 image_background_transparent (img
, f
, mask
)
5003 if (! img
->background_transparent_valid
)
5004 /* IMG doesn't have a background yet, try to guess a reasonable value. */
5008 int free_mask
= !mask
;
5011 mask
= XGetImage (FRAME_X_DISPLAY (f
), img
->mask
,
5012 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
5014 img
->background_transparent
5015 = !four_corners_best (mask
, img
->width
, img
->height
);
5018 XDestroyImage (mask
);
5021 img
->background_transparent
= 0;
5023 img
->background_transparent_valid
= 1;
5026 return img
->background_transparent
;
5030 /***********************************************************************
5031 Helper functions for X image types
5032 ***********************************************************************/
5034 static void x_clear_image_1
P_ ((struct frame
*, struct image
*, int,
5036 static void x_clear_image
P_ ((struct frame
*f
, struct image
*img
));
5037 static unsigned long x_alloc_image_color
P_ ((struct frame
*f
,
5039 Lisp_Object color_name
,
5040 unsigned long dflt
));
5043 /* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
5044 free the pixmap if any. MASK_P non-zero means clear the mask
5045 pixmap if any. COLORS_P non-zero means free colors allocated for
5046 the image, if any. */
5049 x_clear_image_1 (f
, img
, pixmap_p
, mask_p
, colors_p
)
5052 int pixmap_p
, mask_p
, colors_p
;
5054 if (pixmap_p
&& img
->pixmap
)
5056 XFreePixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
5058 img
->background_valid
= 0;
5061 if (mask_p
&& img
->mask
)
5063 XFreePixmap (FRAME_X_DISPLAY (f
), img
->mask
);
5065 img
->background_transparent_valid
= 0;
5068 if (colors_p
&& img
->ncolors
)
5070 x_free_colors (f
, img
->colors
, img
->ncolors
);
5071 xfree (img
->colors
);
5077 /* Free X resources of image IMG which is used on frame F. */
5080 x_clear_image (f
, img
)
5085 x_clear_image_1 (f
, img
, 1, 1, 1);
5090 /* Allocate color COLOR_NAME for image IMG on frame F. If color
5091 cannot be allocated, use DFLT. Add a newly allocated color to
5092 IMG->colors, so that it can be freed again. Value is the pixel
5095 static unsigned long
5096 x_alloc_image_color (f
, img
, color_name
, dflt
)
5099 Lisp_Object color_name
;
5103 unsigned long result
;
5105 xassert (STRINGP (color_name
));
5107 if (x_defined_color (f
, SDATA (color_name
), &color
, 1))
5109 /* This isn't called frequently so we get away with simply
5110 reallocating the color vector to the needed size, here. */
5113 (unsigned long *) xrealloc (img
->colors
,
5114 img
->ncolors
* sizeof *img
->colors
);
5115 img
->colors
[img
->ncolors
- 1] = color
.pixel
;
5116 result
= color
.pixel
;
5126 /***********************************************************************
5128 ***********************************************************************/
5130 static void cache_image
P_ ((struct frame
*f
, struct image
*img
));
5131 static void postprocess_image
P_ ((struct frame
*, struct image
*));
5134 /* Return a new, initialized image cache that is allocated from the
5135 heap. Call free_image_cache to free an image cache. */
5137 struct image_cache
*
5140 struct image_cache
*c
= (struct image_cache
*) xmalloc (sizeof *c
);
5143 bzero (c
, sizeof *c
);
5145 c
->images
= (struct image
**) xmalloc (c
->size
* sizeof *c
->images
);
5146 size
= IMAGE_CACHE_BUCKETS_SIZE
* sizeof *c
->buckets
;
5147 c
->buckets
= (struct image
**) xmalloc (size
);
5148 bzero (c
->buckets
, size
);
5153 /* Free image cache of frame F. Be aware that X frames share images
5157 free_image_cache (f
)
5160 struct image_cache
*c
= FRAME_X_IMAGE_CACHE (f
);
5165 /* Cache should not be referenced by any frame when freed. */
5166 xassert (c
->refcount
== 0);
5168 for (i
= 0; i
< c
->used
; ++i
)
5169 free_image (f
, c
->images
[i
]);
5173 FRAME_X_IMAGE_CACHE (f
) = NULL
;
5178 /* Clear image cache of frame F. FORCE_P non-zero means free all
5179 images. FORCE_P zero means clear only images that haven't been
5180 displayed for some time. Should be called from time to time to
5181 reduce the number of loaded images. If image-eviction-seconds is
5182 non-nil, this frees images in the cache which weren't displayed for
5183 at least that many seconds. */
5186 clear_image_cache (f
, force_p
)
5190 struct image_cache
*c
= FRAME_X_IMAGE_CACHE (f
);
5192 if (c
&& INTEGERP (Vimage_cache_eviction_delay
))
5199 old
= EMACS_SECS (t
) - XFASTINT (Vimage_cache_eviction_delay
);
5201 /* Block input so that we won't be interrupted by a SIGIO
5202 while being in an inconsistent state. */
5205 for (i
= nfreed
= 0; i
< c
->used
; ++i
)
5207 struct image
*img
= c
->images
[i
];
5209 && (force_p
|| img
->timestamp
< old
))
5211 free_image (f
, img
);
5216 /* We may be clearing the image cache because, for example,
5217 Emacs was iconified for a longer period of time. In that
5218 case, current matrices may still contain references to
5219 images freed above. So, clear these matrices. */
5222 Lisp_Object tail
, frame
;
5224 FOR_EACH_FRAME (tail
, frame
)
5226 struct frame
*f
= XFRAME (frame
);
5228 && FRAME_X_IMAGE_CACHE (f
) == c
)
5229 clear_current_matrices (f
);
5232 ++windows_or_buffers_changed
;
5240 DEFUN ("clear-image-cache", Fclear_image_cache
, Sclear_image_cache
,
5242 doc
: /* Clear the image cache of FRAME.
5243 FRAME nil or omitted means use the selected frame.
5244 FRAME t means clear the image caches of all frames. */)
5252 FOR_EACH_FRAME (tail
, frame
)
5253 if (FRAME_X_P (XFRAME (frame
)))
5254 clear_image_cache (XFRAME (frame
), 1);
5257 clear_image_cache (check_x_frame (frame
), 1);
5263 /* Compute masks and transform image IMG on frame F, as specified
5264 by the image's specification, */
5267 postprocess_image (f
, img
)
5271 /* Manipulation of the image's mask. */
5274 Lisp_Object conversion
, spec
;
5279 /* `:heuristic-mask t'
5281 means build a mask heuristically.
5282 `:heuristic-mask (R G B)'
5283 `:mask (heuristic (R G B))'
5284 means build a mask from color (R G B) in the
5287 means remove a mask, if any. */
5289 mask
= image_spec_value (spec
, QCheuristic_mask
, NULL
);
5291 x_build_heuristic_mask (f
, img
, mask
);
5296 mask
= image_spec_value (spec
, QCmask
, &found_p
);
5298 if (EQ (mask
, Qheuristic
))
5299 x_build_heuristic_mask (f
, img
, Qt
);
5300 else if (CONSP (mask
)
5301 && EQ (XCAR (mask
), Qheuristic
))
5303 if (CONSP (XCDR (mask
)))
5304 x_build_heuristic_mask (f
, img
, XCAR (XCDR (mask
)));
5306 x_build_heuristic_mask (f
, img
, XCDR (mask
));
5308 else if (NILP (mask
) && found_p
&& img
->mask
)
5310 XFreePixmap (FRAME_X_DISPLAY (f
), img
->mask
);
5316 /* Should we apply an image transformation algorithm? */
5317 conversion
= image_spec_value (spec
, QCconversion
, NULL
);
5318 if (EQ (conversion
, Qdisabled
))
5319 x_disable_image (f
, img
);
5320 else if (EQ (conversion
, Qlaplace
))
5322 else if (EQ (conversion
, Qemboss
))
5324 else if (CONSP (conversion
)
5325 && EQ (XCAR (conversion
), Qedge_detection
))
5328 tem
= XCDR (conversion
);
5330 x_edge_detection (f
, img
,
5331 Fplist_get (tem
, QCmatrix
),
5332 Fplist_get (tem
, QCcolor_adjustment
));
5338 /* Return the id of image with Lisp specification SPEC on frame F.
5339 SPEC must be a valid Lisp image specification (see valid_image_p). */
5342 lookup_image (f
, spec
)
5346 struct image_cache
*c
= FRAME_X_IMAGE_CACHE (f
);
5350 struct gcpro gcpro1
;
5353 /* F must be a window-system frame, and SPEC must be a valid image
5355 xassert (FRAME_WINDOW_P (f
));
5356 xassert (valid_image_p (spec
));
5360 /* Look up SPEC in the hash table of the image cache. */
5361 hash
= sxhash (spec
, 0);
5362 i
= hash
% IMAGE_CACHE_BUCKETS_SIZE
;
5364 for (img
= c
->buckets
[i
]; img
; img
= img
->next
)
5365 if (img
->hash
== hash
&& !NILP (Fequal (img
->spec
, spec
)))
5368 /* If not found, create a new image and cache it. */
5371 extern Lisp_Object Qpostscript
;
5374 img
= make_image (spec
, hash
);
5375 cache_image (f
, img
);
5376 img
->load_failed_p
= img
->type
->load (f
, img
) == 0;
5378 /* If we can't load the image, and we don't have a width and
5379 height, use some arbitrary width and height so that we can
5380 draw a rectangle for it. */
5381 if (img
->load_failed_p
)
5385 value
= image_spec_value (spec
, QCwidth
, NULL
);
5386 img
->width
= (INTEGERP (value
)
5387 ? XFASTINT (value
) : DEFAULT_IMAGE_WIDTH
);
5388 value
= image_spec_value (spec
, QCheight
, NULL
);
5389 img
->height
= (INTEGERP (value
)
5390 ? XFASTINT (value
) : DEFAULT_IMAGE_HEIGHT
);
5394 /* Handle image type independent image attributes
5395 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
5396 `:background COLOR'. */
5397 Lisp_Object ascent
, margin
, relief
, bg
;
5399 ascent
= image_spec_value (spec
, QCascent
, NULL
);
5400 if (INTEGERP (ascent
))
5401 img
->ascent
= XFASTINT (ascent
);
5402 else if (EQ (ascent
, Qcenter
))
5403 img
->ascent
= CENTERED_IMAGE_ASCENT
;
5405 margin
= image_spec_value (spec
, QCmargin
, NULL
);
5406 if (INTEGERP (margin
) && XINT (margin
) >= 0)
5407 img
->vmargin
= img
->hmargin
= XFASTINT (margin
);
5408 else if (CONSP (margin
) && INTEGERP (XCAR (margin
))
5409 && INTEGERP (XCDR (margin
)))
5411 if (XINT (XCAR (margin
)) > 0)
5412 img
->hmargin
= XFASTINT (XCAR (margin
));
5413 if (XINT (XCDR (margin
)) > 0)
5414 img
->vmargin
= XFASTINT (XCDR (margin
));
5417 relief
= image_spec_value (spec
, QCrelief
, NULL
);
5418 if (INTEGERP (relief
))
5420 img
->relief
= XINT (relief
);
5421 img
->hmargin
+= abs (img
->relief
);
5422 img
->vmargin
+= abs (img
->relief
);
5425 if (! img
->background_valid
)
5427 bg
= image_spec_value (img
->spec
, QCbackground
, NULL
);
5431 = x_alloc_image_color (f
, img
, bg
,
5432 FRAME_BACKGROUND_PIXEL (f
));
5433 img
->background_valid
= 1;
5437 /* Do image transformations and compute masks, unless we
5438 don't have the image yet. */
5439 if (!EQ (*img
->type
->type
, Qpostscript
))
5440 postprocess_image (f
, img
);
5444 xassert (!interrupt_input_blocked
);
5447 /* We're using IMG, so set its timestamp to `now'. */
5448 EMACS_GET_TIME (now
);
5449 img
->timestamp
= EMACS_SECS (now
);
5453 /* Value is the image id. */
5458 /* Cache image IMG in the image cache of frame F. */
5461 cache_image (f
, img
)
5465 struct image_cache
*c
= FRAME_X_IMAGE_CACHE (f
);
5468 /* Find a free slot in c->images. */
5469 for (i
= 0; i
< c
->used
; ++i
)
5470 if (c
->images
[i
] == NULL
)
5473 /* If no free slot found, maybe enlarge c->images. */
5474 if (i
== c
->used
&& c
->used
== c
->size
)
5477 c
->images
= (struct image
**) xrealloc (c
->images
,
5478 c
->size
* sizeof *c
->images
);
5481 /* Add IMG to c->images, and assign IMG an id. */
5487 /* Add IMG to the cache's hash table. */
5488 i
= img
->hash
% IMAGE_CACHE_BUCKETS_SIZE
;
5489 img
->next
= c
->buckets
[i
];
5491 img
->next
->prev
= img
;
5493 c
->buckets
[i
] = img
;
5497 /* Call FN on every image in the image cache of frame F. Used to mark
5498 Lisp Objects in the image cache. */
5501 forall_images_in_image_cache (f
, fn
)
5503 void (*fn
) P_ ((struct image
*img
));
5505 if (FRAME_LIVE_P (f
) && FRAME_X_P (f
))
5507 struct image_cache
*c
= FRAME_X_IMAGE_CACHE (f
);
5511 for (i
= 0; i
< c
->used
; ++i
)
5520 /***********************************************************************
5522 ***********************************************************************/
5524 static int x_create_x_image_and_pixmap
P_ ((struct frame
*, int, int, int,
5525 XImage
**, Pixmap
*));
5526 static void x_destroy_x_image
P_ ((XImage
*));
5527 static void x_put_x_image
P_ ((struct frame
*, XImage
*, Pixmap
, int, int));
5530 /* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
5531 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
5532 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
5533 via xmalloc. Print error messages via image_error if an error
5534 occurs. Value is non-zero if successful. */
5537 x_create_x_image_and_pixmap (f
, width
, height
, depth
, ximg
, pixmap
)
5539 int width
, height
, depth
;
5543 Display
*display
= FRAME_X_DISPLAY (f
);
5544 Screen
*screen
= FRAME_X_SCREEN (f
);
5545 Window window
= FRAME_X_WINDOW (f
);
5547 xassert (interrupt_input_blocked
);
5550 depth
= DefaultDepthOfScreen (screen
);
5551 *ximg
= XCreateImage (display
, DefaultVisualOfScreen (screen
),
5552 depth
, ZPixmap
, 0, NULL
, width
, height
,
5553 depth
> 16 ? 32 : depth
> 8 ? 16 : 8, 0);
5556 image_error ("Unable to allocate X image", Qnil
, Qnil
);
5560 /* Allocate image raster. */
5561 (*ximg
)->data
= (char *) xmalloc ((*ximg
)->bytes_per_line
* height
);
5563 /* Allocate a pixmap of the same size. */
5564 *pixmap
= XCreatePixmap (display
, window
, width
, height
, depth
);
5565 if (*pixmap
== None
)
5567 x_destroy_x_image (*ximg
);
5569 image_error ("Unable to create X pixmap", Qnil
, Qnil
);
5577 /* Destroy XImage XIMG. Free XIMG->data. */
5580 x_destroy_x_image (ximg
)
5583 xassert (interrupt_input_blocked
);
5588 XDestroyImage (ximg
);
5593 /* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
5594 are width and height of both the image and pixmap. */
5597 x_put_x_image (f
, ximg
, pixmap
, width
, height
)
5605 xassert (interrupt_input_blocked
);
5606 gc
= XCreateGC (FRAME_X_DISPLAY (f
), pixmap
, 0, NULL
);
5607 XPutImage (FRAME_X_DISPLAY (f
), pixmap
, gc
, ximg
, 0, 0, 0, 0, width
, height
);
5608 XFreeGC (FRAME_X_DISPLAY (f
), gc
);
5613 /***********************************************************************
5615 ***********************************************************************/
5617 static Lisp_Object x_find_image_file
P_ ((Lisp_Object
));
5618 static char *slurp_file
P_ ((char *, int *));
5621 /* Find image file FILE. Look in data-directory, then
5622 x-bitmap-file-path. Value is the full name of the file found, or
5623 nil if not found. */
5626 x_find_image_file (file
)
5629 Lisp_Object file_found
, search_path
;
5630 struct gcpro gcpro1
, gcpro2
;
5634 search_path
= Fcons (Vdata_directory
, Vx_bitmap_file_path
);
5635 GCPRO2 (file_found
, search_path
);
5637 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
5638 fd
= openp (search_path
, file
, Qnil
, &file_found
, Qnil
);
5650 /* Read FILE into memory. Value is a pointer to a buffer allocated
5651 with xmalloc holding FILE's contents. Value is null if an error
5652 occurred. *SIZE is set to the size of the file. */
5655 slurp_file (file
, size
)
5663 if (stat (file
, &st
) == 0
5664 && (fp
= fopen (file
, "r")) != NULL
5665 && (buf
= (char *) xmalloc (st
.st_size
),
5666 fread (buf
, 1, st
.st_size
, fp
) == st
.st_size
))
5687 /***********************************************************************
5689 ***********************************************************************/
5691 static int xbm_scan
P_ ((char **, char *, char *, int *));
5692 static int xbm_load
P_ ((struct frame
*f
, struct image
*img
));
5693 static int xbm_load_image
P_ ((struct frame
*f
, struct image
*img
,
5695 static int xbm_image_p
P_ ((Lisp_Object object
));
5696 static int xbm_read_bitmap_data
P_ ((char *, char *, int *, int *,
5698 static int xbm_file_p
P_ ((Lisp_Object
));
5701 /* Indices of image specification fields in xbm_format, below. */
5703 enum xbm_keyword_index
5721 /* Vector of image_keyword structures describing the format
5722 of valid XBM image specifications. */
5724 static struct image_keyword xbm_format
[XBM_LAST
] =
5726 {":type", IMAGE_SYMBOL_VALUE
, 1},
5727 {":file", IMAGE_STRING_VALUE
, 0},
5728 {":width", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
5729 {":height", IMAGE_POSITIVE_INTEGER_VALUE
, 0},
5730 {":data", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5731 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
5732 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0},
5733 {":ascent", IMAGE_ASCENT_VALUE
, 0},
5734 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
5735 {":relief", IMAGE_INTEGER_VALUE
, 0},
5736 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5737 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
5738 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0}
5741 /* Structure describing the image type XBM. */
5743 static struct image_type xbm_type
=
5752 /* Tokens returned from xbm_scan. */
5761 /* Return non-zero if OBJECT is a valid XBM-type image specification.
5762 A valid specification is a list starting with the symbol `image'
5763 The rest of the list is a property list which must contain an
5766 If the specification specifies a file to load, it must contain
5767 an entry `:file FILENAME' where FILENAME is a string.
5769 If the specification is for a bitmap loaded from memory it must
5770 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
5771 WIDTH and HEIGHT are integers > 0. DATA may be:
5773 1. a string large enough to hold the bitmap data, i.e. it must
5774 have a size >= (WIDTH + 7) / 8 * HEIGHT
5776 2. a bool-vector of size >= WIDTH * HEIGHT
5778 3. a vector of strings or bool-vectors, one for each line of the
5781 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
5782 may not be specified in this case because they are defined in the
5785 Both the file and data forms may contain the additional entries
5786 `:background COLOR' and `:foreground COLOR'. If not present,
5787 foreground and background of the frame on which the image is
5788 displayed is used. */
5791 xbm_image_p (object
)
5794 struct image_keyword kw
[XBM_LAST
];
5796 bcopy (xbm_format
, kw
, sizeof kw
);
5797 if (!parse_image_spec (object
, kw
, XBM_LAST
, Qxbm
))
5800 xassert (EQ (kw
[XBM_TYPE
].value
, Qxbm
));
5802 if (kw
[XBM_FILE
].count
)
5804 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_DATA
].count
)
5807 else if (kw
[XBM_DATA
].count
&& xbm_file_p (kw
[XBM_DATA
].value
))
5809 /* In-memory XBM file. */
5810 if (kw
[XBM_WIDTH
].count
|| kw
[XBM_HEIGHT
].count
|| kw
[XBM_FILE
].count
)
5818 /* Entries for `:width', `:height' and `:data' must be present. */
5819 if (!kw
[XBM_WIDTH
].count
5820 || !kw
[XBM_HEIGHT
].count
5821 || !kw
[XBM_DATA
].count
)
5824 data
= kw
[XBM_DATA
].value
;
5825 width
= XFASTINT (kw
[XBM_WIDTH
].value
);
5826 height
= XFASTINT (kw
[XBM_HEIGHT
].value
);
5828 /* Check type of data, and width and height against contents of
5834 /* Number of elements of the vector must be >= height. */
5835 if (XVECTOR (data
)->size
< height
)
5838 /* Each string or bool-vector in data must be large enough
5839 for one line of the image. */
5840 for (i
= 0; i
< height
; ++i
)
5842 Lisp_Object elt
= XVECTOR (data
)->contents
[i
];
5847 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
)
5850 else if (BOOL_VECTOR_P (elt
))
5852 if (XBOOL_VECTOR (elt
)->size
< width
)
5859 else if (STRINGP (data
))
5862 < (width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
* height
)
5865 else if (BOOL_VECTOR_P (data
))
5867 if (XBOOL_VECTOR (data
)->size
< width
* height
)
5878 /* Scan a bitmap file. FP is the stream to read from. Value is
5879 either an enumerator from enum xbm_token, or a character for a
5880 single-character token, or 0 at end of file. If scanning an
5881 identifier, store the lexeme of the identifier in SVAL. If
5882 scanning a number, store its value in *IVAL. */
5885 xbm_scan (s
, end
, sval
, ival
)
5894 /* Skip white space. */
5895 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
5900 else if (isdigit (c
))
5902 int value
= 0, digit
;
5904 if (c
== '0' && *s
< end
)
5907 if (c
== 'x' || c
== 'X')
5914 else if (c
>= 'a' && c
<= 'f')
5915 digit
= c
- 'a' + 10;
5916 else if (c
>= 'A' && c
<= 'F')
5917 digit
= c
- 'A' + 10;
5920 value
= 16 * value
+ digit
;
5923 else if (isdigit (c
))
5927 && (c
= *(*s
)++, isdigit (c
)))
5928 value
= 8 * value
+ c
- '0';
5935 && (c
= *(*s
)++, isdigit (c
)))
5936 value
= 10 * value
+ c
- '0';
5944 else if (isalpha (c
) || c
== '_')
5948 && (c
= *(*s
)++, (isalnum (c
) || c
== '_')))
5955 else if (c
== '/' && **s
== '*')
5957 /* C-style comment. */
5959 while (**s
&& (**s
!= '*' || *(*s
+ 1) != '/'))
5972 /* Replacement for XReadBitmapFileData which isn't available under old
5973 X versions. CONTENTS is a pointer to a buffer to parse; END is the
5974 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
5975 the image. Return in *DATA the bitmap data allocated with xmalloc.
5976 Value is non-zero if successful. DATA null means just test if
5977 CONTENTS looks like an in-memory XBM file. */
5980 xbm_read_bitmap_data (contents
, end
, width
, height
, data
)
5981 char *contents
, *end
;
5982 int *width
, *height
;
5983 unsigned char **data
;
5986 char buffer
[BUFSIZ
];
5989 int bytes_per_line
, i
, nbytes
;
5995 LA1 = xbm_scan (&s, end, buffer, &value)
5997 #define expect(TOKEN) \
5998 if (LA1 != (TOKEN)) \
6003 #define expect_ident(IDENT) \
6004 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
6009 *width
= *height
= -1;
6012 LA1
= xbm_scan (&s
, end
, buffer
, &value
);
6014 /* Parse defines for width, height and hot-spots. */
6018 expect_ident ("define");
6019 expect (XBM_TK_IDENT
);
6021 if (LA1
== XBM_TK_NUMBER
);
6023 char *p
= strrchr (buffer
, '_');
6024 p
= p
? p
+ 1 : buffer
;
6025 if (strcmp (p
, "width") == 0)
6027 else if (strcmp (p
, "height") == 0)
6030 expect (XBM_TK_NUMBER
);
6033 if (*width
< 0 || *height
< 0)
6035 else if (data
== NULL
)
6038 /* Parse bits. Must start with `static'. */
6039 expect_ident ("static");
6040 if (LA1
== XBM_TK_IDENT
)
6042 if (strcmp (buffer
, "unsigned") == 0)
6045 expect_ident ("char");
6047 else if (strcmp (buffer
, "short") == 0)
6051 if (*width
% 16 && *width
% 16 < 9)
6054 else if (strcmp (buffer
, "char") == 0)
6062 expect (XBM_TK_IDENT
);
6068 bytes_per_line
= (*width
+ 7) / 8 + padding_p
;
6069 nbytes
= bytes_per_line
* *height
;
6070 p
= *data
= (char *) xmalloc (nbytes
);
6074 for (i
= 0; i
< nbytes
; i
+= 2)
6077 expect (XBM_TK_NUMBER
);
6080 if (!padding_p
|| ((i
+ 2) % bytes_per_line
))
6083 if (LA1
== ',' || LA1
== '}')
6091 for (i
= 0; i
< nbytes
; ++i
)
6094 expect (XBM_TK_NUMBER
);
6098 if (LA1
== ',' || LA1
== '}')
6123 /* Load XBM image IMG which will be displayed on frame F from buffer
6124 CONTENTS. END is the end of the buffer. Value is non-zero if
6128 xbm_load_image (f
, img
, contents
, end
)
6131 char *contents
, *end
;
6134 unsigned char *data
;
6137 rc
= xbm_read_bitmap_data (contents
, end
, &img
->width
, &img
->height
, &data
);
6140 int depth
= DefaultDepthOfScreen (FRAME_X_SCREEN (f
));
6141 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
6142 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
6145 xassert (img
->width
> 0 && img
->height
> 0);
6147 /* Get foreground and background colors, maybe allocate colors. */
6148 value
= image_spec_value (img
->spec
, QCforeground
, NULL
);
6150 foreground
= x_alloc_image_color (f
, img
, value
, foreground
);
6151 value
= image_spec_value (img
->spec
, QCbackground
, NULL
);
6154 background
= x_alloc_image_color (f
, img
, value
, background
);
6155 img
->background
= background
;
6156 img
->background_valid
= 1;
6160 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
6163 img
->width
, img
->height
,
6164 foreground
, background
,
6168 if (img
->pixmap
== None
)
6170 x_clear_image (f
, img
);
6171 image_error ("Unable to create X pixmap for `%s'", img
->spec
, Qnil
);
6177 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
6183 /* Value is non-zero if DATA looks like an in-memory XBM file. */
6190 return (STRINGP (data
)
6191 && xbm_read_bitmap_data (SDATA (data
),
6198 /* Fill image IMG which is used on frame F with pixmap data. Value is
6199 non-zero if successful. */
6207 Lisp_Object file_name
;
6209 xassert (xbm_image_p (img
->spec
));
6211 /* If IMG->spec specifies a file name, create a non-file spec from it. */
6212 file_name
= image_spec_value (img
->spec
, QCfile
, NULL
);
6213 if (STRINGP (file_name
))
6218 struct gcpro gcpro1
;
6220 file
= x_find_image_file (file_name
);
6222 if (!STRINGP (file
))
6224 image_error ("Cannot find image file `%s'", file_name
, Qnil
);
6229 contents
= slurp_file (SDATA (file
), &size
);
6230 if (contents
== NULL
)
6232 image_error ("Error loading XBM image `%s'", img
->spec
, Qnil
);
6237 success_p
= xbm_load_image (f
, img
, contents
, contents
+ size
);
6242 struct image_keyword fmt
[XBM_LAST
];
6245 unsigned long foreground
= FRAME_FOREGROUND_PIXEL (f
);
6246 unsigned long background
= FRAME_BACKGROUND_PIXEL (f
);
6249 int in_memory_file_p
= 0;
6251 /* See if data looks like an in-memory XBM file. */
6252 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
6253 in_memory_file_p
= xbm_file_p (data
);
6255 /* Parse the image specification. */
6256 bcopy (xbm_format
, fmt
, sizeof fmt
);
6257 parsed_p
= parse_image_spec (img
->spec
, fmt
, XBM_LAST
, Qxbm
);
6260 /* Get specified width, and height. */
6261 if (!in_memory_file_p
)
6263 img
->width
= XFASTINT (fmt
[XBM_WIDTH
].value
);
6264 img
->height
= XFASTINT (fmt
[XBM_HEIGHT
].value
);
6265 xassert (img
->width
> 0 && img
->height
> 0);
6268 /* Get foreground and background colors, maybe allocate colors. */
6269 if (fmt
[XBM_FOREGROUND
].count
6270 && STRINGP (fmt
[XBM_FOREGROUND
].value
))
6271 foreground
= x_alloc_image_color (f
, img
, fmt
[XBM_FOREGROUND
].value
,
6273 if (fmt
[XBM_BACKGROUND
].count
6274 && STRINGP (fmt
[XBM_BACKGROUND
].value
))
6275 background
= x_alloc_image_color (f
, img
, fmt
[XBM_BACKGROUND
].value
,
6278 if (in_memory_file_p
)
6279 success_p
= xbm_load_image (f
, img
, SDATA (data
),
6288 int nbytes
= (img
->width
+ BITS_PER_CHAR
- 1) / BITS_PER_CHAR
;
6290 p
= bits
= (char *) alloca (nbytes
* img
->height
);
6291 for (i
= 0; i
< img
->height
; ++i
, p
+= nbytes
)
6293 Lisp_Object line
= XVECTOR (data
)->contents
[i
];
6295 bcopy (SDATA (line
), p
, nbytes
);
6297 bcopy (XBOOL_VECTOR (line
)->data
, p
, nbytes
);
6300 else if (STRINGP (data
))
6301 bits
= SDATA (data
);
6303 bits
= XBOOL_VECTOR (data
)->data
;
6305 /* Create the pixmap. */
6306 depth
= DefaultDepthOfScreen (FRAME_X_SCREEN (f
));
6308 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f
),
6311 img
->width
, img
->height
,
6312 foreground
, background
,
6318 image_error ("Unable to create pixmap for XBM image `%s'",
6320 x_clear_image (f
, img
);
6330 /***********************************************************************
6332 ***********************************************************************/
6336 static int xpm_image_p
P_ ((Lisp_Object object
));
6337 static int xpm_load
P_ ((struct frame
*f
, struct image
*img
));
6338 static int xpm_valid_color_symbols_p
P_ ((Lisp_Object
));
6340 #include "X11/xpm.h"
6342 /* The symbol `xpm' identifying XPM-format images. */
6346 /* Indices of image specification fields in xpm_format, below. */
6348 enum xpm_keyword_index
6364 /* Vector of image_keyword structures describing the format
6365 of valid XPM image specifications. */
6367 static struct image_keyword xpm_format
[XPM_LAST
] =
6369 {":type", IMAGE_SYMBOL_VALUE
, 1},
6370 {":file", IMAGE_STRING_VALUE
, 0},
6371 {":data", IMAGE_STRING_VALUE
, 0},
6372 {":ascent", IMAGE_ASCENT_VALUE
, 0},
6373 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
6374 {":relief", IMAGE_INTEGER_VALUE
, 0},
6375 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6376 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6377 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6378 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
6379 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
6382 /* Structure describing the image type XBM. */
6384 static struct image_type xpm_type
=
6394 /* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
6395 functions for allocating image colors. Our own functions handle
6396 color allocation failures more gracefully than the ones on the XPM
6399 #if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
6400 #define ALLOC_XPM_COLORS
6403 #ifdef ALLOC_XPM_COLORS
6405 static void xpm_init_color_cache
P_ ((struct frame
*, XpmAttributes
*));
6406 static void xpm_free_color_cache
P_ ((void));
6407 static int xpm_lookup_color
P_ ((struct frame
*, char *, XColor
*));
6408 static int xpm_color_bucket
P_ ((char *));
6409 static struct xpm_cached_color
*xpm_cache_color
P_ ((struct frame
*, char *,
6412 /* An entry in a hash table used to cache color definitions of named
6413 colors. This cache is necessary to speed up XPM image loading in
6414 case we do color allocations ourselves. Without it, we would need
6415 a call to XParseColor per pixel in the image. */
6417 struct xpm_cached_color
6419 /* Next in collision chain. */
6420 struct xpm_cached_color
*next
;
6422 /* Color definition (RGB and pixel color). */
6429 /* The hash table used for the color cache, and its bucket vector
6432 #define XPM_COLOR_CACHE_BUCKETS 1001
6433 struct xpm_cached_color
**xpm_color_cache
;
6435 /* Initialize the color cache. */
6438 xpm_init_color_cache (f
, attrs
)
6440 XpmAttributes
*attrs
;
6442 size_t nbytes
= XPM_COLOR_CACHE_BUCKETS
* sizeof *xpm_color_cache
;
6443 xpm_color_cache
= (struct xpm_cached_color
**) xmalloc (nbytes
);
6444 memset (xpm_color_cache
, 0, nbytes
);
6445 init_color_table ();
6447 if (attrs
->valuemask
& XpmColorSymbols
)
6452 for (i
= 0; i
< attrs
->numsymbols
; ++i
)
6453 if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
6454 attrs
->colorsymbols
[i
].value
, &color
))
6456 color
.pixel
= lookup_rgb_color (f
, color
.red
, color
.green
,
6458 xpm_cache_color (f
, attrs
->colorsymbols
[i
].name
, &color
, -1);
6464 /* Free the color cache. */
6467 xpm_free_color_cache ()
6469 struct xpm_cached_color
*p
, *next
;
6472 for (i
= 0; i
< XPM_COLOR_CACHE_BUCKETS
; ++i
)
6473 for (p
= xpm_color_cache
[i
]; p
; p
= next
)
6479 xfree (xpm_color_cache
);
6480 xpm_color_cache
= NULL
;
6481 free_color_table ();
6485 /* Return the bucket index for color named COLOR_NAME in the color
6489 xpm_color_bucket (color_name
)
6495 for (s
= color_name
; *s
; ++s
)
6497 return h
%= XPM_COLOR_CACHE_BUCKETS
;
6501 /* On frame F, cache values COLOR for color with name COLOR_NAME.
6502 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
6505 static struct xpm_cached_color
*
6506 xpm_cache_color (f
, color_name
, color
, bucket
)
6513 struct xpm_cached_color
*p
;
6516 bucket
= xpm_color_bucket (color_name
);
6518 nbytes
= sizeof *p
+ strlen (color_name
);
6519 p
= (struct xpm_cached_color
*) xmalloc (nbytes
);
6520 strcpy (p
->name
, color_name
);
6522 p
->next
= xpm_color_cache
[bucket
];
6523 xpm_color_cache
[bucket
] = p
;
6528 /* Look up color COLOR_NAME for frame F in the color cache. If found,
6529 return the cached definition in *COLOR. Otherwise, make a new
6530 entry in the cache and allocate the color. Value is zero if color
6531 allocation failed. */
6534 xpm_lookup_color (f
, color_name
, color
)
6539 struct xpm_cached_color
*p
;
6540 int h
= xpm_color_bucket (color_name
);
6542 for (p
= xpm_color_cache
[h
]; p
; p
= p
->next
)
6543 if (strcmp (p
->name
, color_name
) == 0)
6548 else if (XParseColor (FRAME_X_DISPLAY (f
), FRAME_X_COLORMAP (f
),
6551 color
->pixel
= lookup_rgb_color (f
, color
->red
, color
->green
,
6553 p
= xpm_cache_color (f
, color_name
, color
, h
);
6555 /* You get `opaque' at least from ImageMagick converting pbm to xpm
6556 with transparency, and it's useful. */
6557 else if (strcmp ("opaque", color_name
) == 0)
6559 bzero (color
, sizeof (XColor
)); /* Is this necessary/correct? */
6560 color
->pixel
= FRAME_FOREGROUND_PIXEL (f
);
6561 p
= xpm_cache_color (f
, color_name
, color
, h
);
6568 /* Callback for allocating color COLOR_NAME. Called from the XPM lib.
6569 CLOSURE is a pointer to the frame on which we allocate the
6570 color. Return in *COLOR the allocated color. Value is non-zero
6574 xpm_alloc_color (dpy
, cmap
, color_name
, color
, closure
)
6581 return xpm_lookup_color ((struct frame
*) closure
, color_name
, color
);
6585 /* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
6586 is a pointer to the frame on which we allocate the color. Value is
6587 non-zero if successful. */
6590 xpm_free_colors (dpy
, cmap
, pixels
, npixels
, closure
)
6600 #endif /* ALLOC_XPM_COLORS */
6603 /* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
6604 for XPM images. Such a list must consist of conses whose car and
6608 xpm_valid_color_symbols_p (color_symbols
)
6609 Lisp_Object color_symbols
;
6611 while (CONSP (color_symbols
))
6613 Lisp_Object sym
= XCAR (color_symbols
);
6615 || !STRINGP (XCAR (sym
))
6616 || !STRINGP (XCDR (sym
)))
6618 color_symbols
= XCDR (color_symbols
);
6621 return NILP (color_symbols
);
6625 /* Value is non-zero if OBJECT is a valid XPM image specification. */
6628 xpm_image_p (object
)
6631 struct image_keyword fmt
[XPM_LAST
];
6632 bcopy (xpm_format
, fmt
, sizeof fmt
);
6633 return (parse_image_spec (object
, fmt
, XPM_LAST
, Qxpm
)
6634 /* Either `:file' or `:data' must be present. */
6635 && fmt
[XPM_FILE
].count
+ fmt
[XPM_DATA
].count
== 1
6636 /* Either no `:color-symbols' or it's a list of conses
6637 whose car and cdr are strings. */
6638 && (fmt
[XPM_COLOR_SYMBOLS
].count
== 0
6639 || xpm_valid_color_symbols_p (fmt
[XPM_COLOR_SYMBOLS
].value
)));
6643 /* Load image IMG which will be displayed on frame F. Value is
6644 non-zero if successful. */
6652 XpmAttributes attrs
;
6653 Lisp_Object specified_file
, color_symbols
;
6655 /* Configure the XPM lib. Use the visual of frame F. Allocate
6656 close colors. Return colors allocated. */
6657 bzero (&attrs
, sizeof attrs
);
6658 attrs
.visual
= FRAME_X_VISUAL (f
);
6659 attrs
.colormap
= FRAME_X_COLORMAP (f
);
6660 attrs
.valuemask
|= XpmVisual
;
6661 attrs
.valuemask
|= XpmColormap
;
6663 #ifdef ALLOC_XPM_COLORS
6664 /* Allocate colors with our own functions which handle
6665 failing color allocation more gracefully. */
6666 attrs
.color_closure
= f
;
6667 attrs
.alloc_color
= xpm_alloc_color
;
6668 attrs
.free_colors
= xpm_free_colors
;
6669 attrs
.valuemask
|= XpmAllocColor
| XpmFreeColors
| XpmColorClosure
;
6670 #else /* not ALLOC_XPM_COLORS */
6671 /* Let the XPM lib allocate colors. */
6672 attrs
.valuemask
|= XpmReturnAllocPixels
;
6673 #ifdef XpmAllocCloseColors
6674 attrs
.alloc_close_colors
= 1;
6675 attrs
.valuemask
|= XpmAllocCloseColors
;
6676 #else /* not XpmAllocCloseColors */
6677 attrs
.closeness
= 600;
6678 attrs
.valuemask
|= XpmCloseness
;
6679 #endif /* not XpmAllocCloseColors */
6680 #endif /* ALLOC_XPM_COLORS */
6682 /* If image specification contains symbolic color definitions, add
6683 these to `attrs'. */
6684 color_symbols
= image_spec_value (img
->spec
, QCcolor_symbols
, NULL
);
6685 if (CONSP (color_symbols
))
6688 XpmColorSymbol
*xpm_syms
;
6691 attrs
.valuemask
|= XpmColorSymbols
;
6693 /* Count number of symbols. */
6694 attrs
.numsymbols
= 0;
6695 for (tail
= color_symbols
; CONSP (tail
); tail
= XCDR (tail
))
6698 /* Allocate an XpmColorSymbol array. */
6699 size
= attrs
.numsymbols
* sizeof *xpm_syms
;
6700 xpm_syms
= (XpmColorSymbol
*) alloca (size
);
6701 bzero (xpm_syms
, size
);
6702 attrs
.colorsymbols
= xpm_syms
;
6704 /* Fill the color symbol array. */
6705 for (tail
= color_symbols
, i
= 0;
6707 ++i
, tail
= XCDR (tail
))
6709 Lisp_Object name
= XCAR (XCAR (tail
));
6710 Lisp_Object color
= XCDR (XCAR (tail
));
6711 xpm_syms
[i
].name
= (char *) alloca (SCHARS (name
) + 1);
6712 strcpy (xpm_syms
[i
].name
, SDATA (name
));
6713 xpm_syms
[i
].value
= (char *) alloca (SCHARS (color
) + 1);
6714 strcpy (xpm_syms
[i
].value
, SDATA (color
));
6718 /* Create a pixmap for the image, either from a file, or from a
6719 string buffer containing data in the same format as an XPM file. */
6720 #ifdef ALLOC_XPM_COLORS
6721 xpm_init_color_cache (f
, &attrs
);
6724 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
6725 if (STRINGP (specified_file
))
6727 Lisp_Object file
= x_find_image_file (specified_file
);
6728 if (!STRINGP (file
))
6730 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
6734 rc
= XpmReadFileToPixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
6735 SDATA (file
), &img
->pixmap
, &img
->mask
,
6740 Lisp_Object buffer
= image_spec_value (img
->spec
, QCdata
, NULL
);
6741 rc
= XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
6743 &img
->pixmap
, &img
->mask
,
6747 if (rc
== XpmSuccess
)
6749 #ifdef ALLOC_XPM_COLORS
6750 img
->colors
= colors_in_color_table (&img
->ncolors
);
6751 #else /* not ALLOC_XPM_COLORS */
6754 img
->ncolors
= attrs
.nalloc_pixels
;
6755 img
->colors
= (unsigned long *) xmalloc (img
->ncolors
6756 * sizeof *img
->colors
);
6757 for (i
= 0; i
< attrs
.nalloc_pixels
; ++i
)
6759 img
->colors
[i
] = attrs
.alloc_pixels
[i
];
6760 #ifdef DEBUG_X_COLORS
6761 register_color (img
->colors
[i
]);
6764 #endif /* not ALLOC_XPM_COLORS */
6766 img
->width
= attrs
.width
;
6767 img
->height
= attrs
.height
;
6768 xassert (img
->width
> 0 && img
->height
> 0);
6770 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
6771 XpmFreeAttributes (&attrs
);
6778 image_error ("Error opening XPM file (%s)", img
->spec
, Qnil
);
6781 case XpmFileInvalid
:
6782 image_error ("Invalid XPM file (%s)", img
->spec
, Qnil
);
6786 image_error ("Out of memory (%s)", img
->spec
, Qnil
);
6789 case XpmColorFailed
:
6790 image_error ("Color allocation error (%s)", img
->spec
, Qnil
);
6794 image_error ("Unknown error (%s)", img
->spec
, Qnil
);
6799 #ifdef ALLOC_XPM_COLORS
6800 xpm_free_color_cache ();
6802 return rc
== XpmSuccess
;
6805 #endif /* HAVE_XPM != 0 */
6808 /***********************************************************************
6810 ***********************************************************************/
6812 /* An entry in the color table mapping an RGB color to a pixel color. */
6817 unsigned long pixel
;
6819 /* Next in color table collision list. */
6820 struct ct_color
*next
;
6823 /* The bucket vector size to use. Must be prime. */
6827 /* Value is a hash of the RGB color given by R, G, and B. */
6829 #define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
6831 /* The color hash table. */
6833 struct ct_color
**ct_table
;
6835 /* Number of entries in the color table. */
6837 int ct_colors_allocated
;
6839 /* Initialize the color table. */
6844 int size
= CT_SIZE
* sizeof (*ct_table
);
6845 ct_table
= (struct ct_color
**) xmalloc (size
);
6846 bzero (ct_table
, size
);
6847 ct_colors_allocated
= 0;
6851 /* Free memory associated with the color table. */
6857 struct ct_color
*p
, *next
;
6859 for (i
= 0; i
< CT_SIZE
; ++i
)
6860 for (p
= ct_table
[i
]; p
; p
= next
)
6871 /* Value is a pixel color for RGB color R, G, B on frame F. If an
6872 entry for that color already is in the color table, return the
6873 pixel color of that entry. Otherwise, allocate a new color for R,
6874 G, B, and make an entry in the color table. */
6876 static unsigned long
6877 lookup_rgb_color (f
, r
, g
, b
)
6881 unsigned hash
= CT_HASH_RGB (r
, g
, b
);
6882 int i
= hash
% CT_SIZE
;
6884 struct x_display_info
*dpyinfo
;
6886 /* Handle TrueColor visuals specially, which improves performance by
6887 two orders of magnitude. Freeing colors on TrueColor visuals is
6888 a nop, and pixel colors specify RGB values directly. See also
6889 the Xlib spec, chapter 3.1. */
6890 dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
6891 if (dpyinfo
->red_bits
> 0)
6893 unsigned long pr
, pg
, pb
;
6895 /* Apply gamma-correction like normal color allocation does. */
6899 color
.red
= r
, color
.green
= g
, color
.blue
= b
;
6900 gamma_correct (f
, &color
);
6901 r
= color
.red
, g
= color
.green
, b
= color
.blue
;
6904 /* Scale down RGB values to the visual's bits per RGB, and shift
6905 them to the right position in the pixel color. Note that the
6906 original RGB values are 16-bit values, as usual in X. */
6907 pr
= (r
>> (16 - dpyinfo
->red_bits
)) << dpyinfo
->red_offset
;
6908 pg
= (g
>> (16 - dpyinfo
->green_bits
)) << dpyinfo
->green_offset
;
6909 pb
= (b
>> (16 - dpyinfo
->blue_bits
)) << dpyinfo
->blue_offset
;
6911 /* Assemble the pixel color. */
6912 return pr
| pg
| pb
;
6915 for (p
= ct_table
[i
]; p
; p
= p
->next
)
6916 if (p
->r
== r
&& p
->g
== g
&& p
->b
== b
)
6929 cmap
= FRAME_X_COLORMAP (f
);
6930 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
6934 ++ct_colors_allocated
;
6936 p
= (struct ct_color
*) xmalloc (sizeof *p
);
6940 p
->pixel
= color
.pixel
;
6941 p
->next
= ct_table
[i
];
6945 return FRAME_FOREGROUND_PIXEL (f
);
6952 /* Look up pixel color PIXEL which is used on frame F in the color
6953 table. If not already present, allocate it. Value is PIXEL. */
6955 static unsigned long
6956 lookup_pixel_color (f
, pixel
)
6958 unsigned long pixel
;
6960 int i
= pixel
% CT_SIZE
;
6963 for (p
= ct_table
[i
]; p
; p
= p
->next
)
6964 if (p
->pixel
== pixel
)
6973 cmap
= FRAME_X_COLORMAP (f
);
6974 color
.pixel
= pixel
;
6975 x_query_color (f
, &color
);
6976 rc
= x_alloc_nearest_color (f
, cmap
, &color
);
6980 ++ct_colors_allocated
;
6982 p
= (struct ct_color
*) xmalloc (sizeof *p
);
6987 p
->next
= ct_table
[i
];
6991 return FRAME_FOREGROUND_PIXEL (f
);
6998 /* Value is a vector of all pixel colors contained in the color table,
6999 allocated via xmalloc. Set *N to the number of colors. */
7001 static unsigned long *
7002 colors_in_color_table (n
)
7007 unsigned long *colors
;
7009 if (ct_colors_allocated
== 0)
7016 colors
= (unsigned long *) xmalloc (ct_colors_allocated
7018 *n
= ct_colors_allocated
;
7020 for (i
= j
= 0; i
< CT_SIZE
; ++i
)
7021 for (p
= ct_table
[i
]; p
; p
= p
->next
)
7022 colors
[j
++] = p
->pixel
;
7030 /***********************************************************************
7032 ***********************************************************************/
7034 static XColor
*x_to_xcolors
P_ ((struct frame
*, struct image
*, int));
7035 static void x_from_xcolors
P_ ((struct frame
*, struct image
*, XColor
*));
7036 static void x_detect_edges
P_ ((struct frame
*, struct image
*, int[9], int));
7038 /* Non-zero means draw a cross on images having `:conversion
7041 int cross_disabled_images
;
7043 /* Edge detection matrices for different edge-detection
7046 static int emboss_matrix
[9] = {
7048 2, -1, 0, /* y - 1 */
7050 0, 1, -2 /* y + 1 */
7053 static int laplace_matrix
[9] = {
7055 1, 0, 0, /* y - 1 */
7057 0, 0, -1 /* y + 1 */
7060 /* Value is the intensity of the color whose red/green/blue values
7063 #define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
7066 /* On frame F, return an array of XColor structures describing image
7067 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
7068 non-zero means also fill the red/green/blue members of the XColor
7069 structures. Value is a pointer to the array of XColors structures,
7070 allocated with xmalloc; it must be freed by the caller. */
7073 x_to_xcolors (f
, img
, rgb_p
)
7082 colors
= (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *colors
);
7084 /* Get the X image IMG->pixmap. */
7085 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
7086 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
7088 /* Fill the `pixel' members of the XColor array. I wished there
7089 were an easy and portable way to circumvent XGetPixel. */
7091 for (y
= 0; y
< img
->height
; ++y
)
7095 for (x
= 0; x
< img
->width
; ++x
, ++p
)
7096 p
->pixel
= XGetPixel (ximg
, x
, y
);
7099 x_query_colors (f
, row
, img
->width
);
7102 XDestroyImage (ximg
);
7107 /* Create IMG->pixmap from an array COLORS of XColor structures, whose
7108 RGB members are set. F is the frame on which this all happens.
7109 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
7112 x_from_xcolors (f
, img
, colors
)
7122 init_color_table ();
7124 x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 0,
7127 for (y
= 0; y
< img
->height
; ++y
)
7128 for (x
= 0; x
< img
->width
; ++x
, ++p
)
7130 unsigned long pixel
;
7131 pixel
= lookup_rgb_color (f
, p
->red
, p
->green
, p
->blue
);
7132 XPutPixel (oimg
, x
, y
, pixel
);
7136 x_clear_image_1 (f
, img
, 1, 0, 1);
7138 x_put_x_image (f
, oimg
, pixmap
, img
->width
, img
->height
);
7139 x_destroy_x_image (oimg
);
7140 img
->pixmap
= pixmap
;
7141 img
->colors
= colors_in_color_table (&img
->ncolors
);
7142 free_color_table ();
7146 /* On frame F, perform edge-detection on image IMG.
7148 MATRIX is a nine-element array specifying the transformation
7149 matrix. See emboss_matrix for an example.
7151 COLOR_ADJUST is a color adjustment added to each pixel of the
7155 x_detect_edges (f
, img
, matrix
, color_adjust
)
7158 int matrix
[9], color_adjust
;
7160 XColor
*colors
= x_to_xcolors (f
, img
, 1);
7164 for (i
= sum
= 0; i
< 9; ++i
)
7165 sum
+= abs (matrix
[i
]);
7167 #define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
7169 new = (XColor
*) xmalloc (img
->width
* img
->height
* sizeof *new);
7171 for (y
= 0; y
< img
->height
; ++y
)
7173 p
= COLOR (new, 0, y
);
7174 p
->red
= p
->green
= p
->blue
= 0xffff/2;
7175 p
= COLOR (new, img
->width
- 1, y
);
7176 p
->red
= p
->green
= p
->blue
= 0xffff/2;
7179 for (x
= 1; x
< img
->width
- 1; ++x
)
7181 p
= COLOR (new, x
, 0);
7182 p
->red
= p
->green
= p
->blue
= 0xffff/2;
7183 p
= COLOR (new, x
, img
->height
- 1);
7184 p
->red
= p
->green
= p
->blue
= 0xffff/2;
7187 for (y
= 1; y
< img
->height
- 1; ++y
)
7189 p
= COLOR (new, 1, y
);
7191 for (x
= 1; x
< img
->width
- 1; ++x
, ++p
)
7193 int r
, g
, b
, y1
, x1
;
7196 for (y1
= y
- 1; y1
< y
+ 2; ++y1
)
7197 for (x1
= x
- 1; x1
< x
+ 2; ++x1
, ++i
)
7200 XColor
*t
= COLOR (colors
, x1
, y1
);
7201 r
+= matrix
[i
] * t
->red
;
7202 g
+= matrix
[i
] * t
->green
;
7203 b
+= matrix
[i
] * t
->blue
;
7206 r
= (r
/ sum
+ color_adjust
) & 0xffff;
7207 g
= (g
/ sum
+ color_adjust
) & 0xffff;
7208 b
= (b
/ sum
+ color_adjust
) & 0xffff;
7209 p
->red
= p
->green
= p
->blue
= COLOR_INTENSITY (r
, g
, b
);
7214 x_from_xcolors (f
, img
, new);
7220 /* Perform the pre-defined `emboss' edge-detection on image IMG
7228 x_detect_edges (f
, img
, emboss_matrix
, 0xffff / 2);
7232 /* Perform the pre-defined `laplace' edge-detection on image IMG
7240 x_detect_edges (f
, img
, laplace_matrix
, 45000);
7244 /* Perform edge-detection on image IMG on frame F, with specified
7245 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
7247 MATRIX must be either
7249 - a list of at least 9 numbers in row-major form
7250 - a vector of at least 9 numbers
7252 COLOR_ADJUST nil means use a default; otherwise it must be a
7256 x_edge_detection (f
, img
, matrix
, color_adjust
)
7259 Lisp_Object matrix
, color_adjust
;
7267 i
< 9 && CONSP (matrix
) && NUMBERP (XCAR (matrix
));
7268 ++i
, matrix
= XCDR (matrix
))
7269 trans
[i
] = XFLOATINT (XCAR (matrix
));
7271 else if (VECTORP (matrix
) && ASIZE (matrix
) >= 9)
7273 for (i
= 0; i
< 9 && NUMBERP (AREF (matrix
, i
)); ++i
)
7274 trans
[i
] = XFLOATINT (AREF (matrix
, i
));
7277 if (NILP (color_adjust
))
7278 color_adjust
= make_number (0xffff / 2);
7280 if (i
== 9 && NUMBERP (color_adjust
))
7281 x_detect_edges (f
, img
, trans
, (int) XFLOATINT (color_adjust
));
7285 /* Transform image IMG on frame F so that it looks disabled. */
7288 x_disable_image (f
, img
)
7292 struct x_display_info
*dpyinfo
= FRAME_X_DISPLAY_INFO (f
);
7294 if (dpyinfo
->n_planes
>= 2)
7296 /* Color (or grayscale). Convert to gray, and equalize. Just
7297 drawing such images with a stipple can look very odd, so
7298 we're using this method instead. */
7299 XColor
*colors
= x_to_xcolors (f
, img
, 1);
7301 const int h
= 15000;
7302 const int l
= 30000;
7304 for (p
= colors
, end
= colors
+ img
->width
* img
->height
;
7308 int i
= COLOR_INTENSITY (p
->red
, p
->green
, p
->blue
);
7309 int i2
= (0xffff - h
- l
) * i
/ 0xffff + l
;
7310 p
->red
= p
->green
= p
->blue
= i2
;
7313 x_from_xcolors (f
, img
, colors
);
7316 /* Draw a cross over the disabled image, if we must or if we
7318 if (dpyinfo
->n_planes
< 2 || cross_disabled_images
)
7320 Display
*dpy
= FRAME_X_DISPLAY (f
);
7323 gc
= XCreateGC (dpy
, img
->pixmap
, 0, NULL
);
7324 XSetForeground (dpy
, gc
, BLACK_PIX_DEFAULT (f
));
7325 XDrawLine (dpy
, img
->pixmap
, gc
, 0, 0,
7326 img
->width
- 1, img
->height
- 1);
7327 XDrawLine (dpy
, img
->pixmap
, gc
, 0, img
->height
- 1,
7333 gc
= XCreateGC (dpy
, img
->mask
, 0, NULL
);
7334 XSetForeground (dpy
, gc
, WHITE_PIX_DEFAULT (f
));
7335 XDrawLine (dpy
, img
->mask
, gc
, 0, 0,
7336 img
->width
- 1, img
->height
- 1);
7337 XDrawLine (dpy
, img
->mask
, gc
, 0, img
->height
- 1,
7345 /* Build a mask for image IMG which is used on frame F. FILE is the
7346 name of an image file, for error messages. HOW determines how to
7347 determine the background color of IMG. If it is a list '(R G B)',
7348 with R, G, and B being integers >= 0, take that as the color of the
7349 background. Otherwise, determine the background color of IMG
7350 heuristically. Value is non-zero if successful. */
7353 x_build_heuristic_mask (f
, img
, how
)
7358 Display
*dpy
= FRAME_X_DISPLAY (f
);
7359 XImage
*ximg
, *mask_img
;
7360 int x
, y
, rc
, use_img_background
;
7361 unsigned long bg
= 0;
7365 XFreePixmap (FRAME_X_DISPLAY (f
), img
->mask
);
7367 img
->background_transparent_valid
= 0;
7370 /* Create an image and pixmap serving as mask. */
7371 rc
= x_create_x_image_and_pixmap (f
, img
->width
, img
->height
, 1,
7372 &mask_img
, &img
->mask
);
7376 /* Get the X image of IMG->pixmap. */
7377 ximg
= XGetImage (dpy
, img
->pixmap
, 0, 0, img
->width
, img
->height
,
7380 /* Determine the background color of ximg. If HOW is `(R G B)'
7381 take that as color. Otherwise, use the image's background color. */
7382 use_img_background
= 1;
7388 for (i
= 0; i
< 3 && CONSP (how
) && NATNUMP (XCAR (how
)); ++i
)
7390 rgb
[i
] = XFASTINT (XCAR (how
)) & 0xffff;
7394 if (i
== 3 && NILP (how
))
7396 char color_name
[30];
7397 sprintf (color_name
, "#%04x%04x%04x", rgb
[0], rgb
[1], rgb
[2]);
7398 bg
= x_alloc_image_color (f
, img
, build_string (color_name
), 0);
7399 use_img_background
= 0;
7403 if (use_img_background
)
7404 bg
= four_corners_best (ximg
, img
->width
, img
->height
);
7406 /* Set all bits in mask_img to 1 whose color in ximg is different
7407 from the background color bg. */
7408 for (y
= 0; y
< img
->height
; ++y
)
7409 for (x
= 0; x
< img
->width
; ++x
)
7410 XPutPixel (mask_img
, x
, y
, XGetPixel (ximg
, x
, y
) != bg
);
7412 /* Fill in the background_transparent field while we have the mask handy. */
7413 image_background_transparent (img
, f
, mask_img
);
7415 /* Put mask_img into img->mask. */
7416 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
7417 x_destroy_x_image (mask_img
);
7418 XDestroyImage (ximg
);
7425 /***********************************************************************
7426 PBM (mono, gray, color)
7427 ***********************************************************************/
7429 static int pbm_image_p
P_ ((Lisp_Object object
));
7430 static int pbm_load
P_ ((struct frame
*f
, struct image
*img
));
7431 static int pbm_scan_number
P_ ((unsigned char **, unsigned char *));
7433 /* The symbol `pbm' identifying images of this type. */
7437 /* Indices of image specification fields in gs_format, below. */
7439 enum pbm_keyword_index
7455 /* Vector of image_keyword structures describing the format
7456 of valid user-defined image specifications. */
7458 static struct image_keyword pbm_format
[PBM_LAST
] =
7460 {":type", IMAGE_SYMBOL_VALUE
, 1},
7461 {":file", IMAGE_STRING_VALUE
, 0},
7462 {":data", IMAGE_STRING_VALUE
, 0},
7463 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7464 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7465 {":relief", IMAGE_INTEGER_VALUE
, 0},
7466 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7467 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7468 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7469 {":foreground", IMAGE_STRING_OR_NIL_VALUE
, 0},
7470 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7473 /* Structure describing the image type `pbm'. */
7475 static struct image_type pbm_type
=
7485 /* Return non-zero if OBJECT is a valid PBM image specification. */
7488 pbm_image_p (object
)
7491 struct image_keyword fmt
[PBM_LAST
];
7493 bcopy (pbm_format
, fmt
, sizeof fmt
);
7495 if (!parse_image_spec (object
, fmt
, PBM_LAST
, Qpbm
))
7498 /* Must specify either :data or :file. */
7499 return fmt
[PBM_DATA
].count
+ fmt
[PBM_FILE
].count
== 1;
7503 /* Scan a decimal number from *S and return it. Advance *S while
7504 reading the number. END is the end of the string. Value is -1 at
7508 pbm_scan_number (s
, end
)
7509 unsigned char **s
, *end
;
7511 int c
= 0, val
= -1;
7515 /* Skip white-space. */
7516 while (*s
< end
&& (c
= *(*s
)++, isspace (c
)))
7521 /* Skip comment to end of line. */
7522 while (*s
< end
&& (c
= *(*s
)++, c
!= '\n'))
7525 else if (isdigit (c
))
7527 /* Read decimal number. */
7529 while (*s
< end
&& (c
= *(*s
)++, isdigit (c
)))
7530 val
= 10 * val
+ c
- '0';
7541 /* Load PBM image IMG for use on frame F. */
7549 int width
, height
, max_color_idx
= 0;
7551 Lisp_Object file
, specified_file
;
7552 enum {PBM_MONO
, PBM_GRAY
, PBM_COLOR
} type
;
7553 struct gcpro gcpro1
;
7554 unsigned char *contents
= NULL
;
7555 unsigned char *end
, *p
;
7558 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7562 if (STRINGP (specified_file
))
7564 file
= x_find_image_file (specified_file
);
7565 if (!STRINGP (file
))
7567 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7572 contents
= slurp_file (SDATA (file
), &size
);
7573 if (contents
== NULL
)
7575 image_error ("Error reading `%s'", file
, Qnil
);
7581 end
= contents
+ size
;
7586 data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7588 end
= p
+ SBYTES (data
);
7591 /* Check magic number. */
7592 if (end
- p
< 2 || *p
++ != 'P')
7594 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
7604 raw_p
= 0, type
= PBM_MONO
;
7608 raw_p
= 0, type
= PBM_GRAY
;
7612 raw_p
= 0, type
= PBM_COLOR
;
7616 raw_p
= 1, type
= PBM_MONO
;
7620 raw_p
= 1, type
= PBM_GRAY
;
7624 raw_p
= 1, type
= PBM_COLOR
;
7628 image_error ("Not a PBM image: `%s'", img
->spec
, Qnil
);
7632 /* Read width, height, maximum color-component. Characters
7633 starting with `#' up to the end of a line are ignored. */
7634 width
= pbm_scan_number (&p
, end
);
7635 height
= pbm_scan_number (&p
, end
);
7637 if (type
!= PBM_MONO
)
7639 max_color_idx
= pbm_scan_number (&p
, end
);
7640 if (raw_p
&& max_color_idx
> 255)
7641 max_color_idx
= 255;
7646 || (type
!= PBM_MONO
&& max_color_idx
< 0))
7649 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0,
7650 &ximg
, &img
->pixmap
))
7653 /* Initialize the color hash table. */
7654 init_color_table ();
7656 if (type
== PBM_MONO
)
7659 struct image_keyword fmt
[PBM_LAST
];
7660 unsigned long fg
= FRAME_FOREGROUND_PIXEL (f
);
7661 unsigned long bg
= FRAME_BACKGROUND_PIXEL (f
);
7663 /* Parse the image specification. */
7664 bcopy (pbm_format
, fmt
, sizeof fmt
);
7665 parse_image_spec (img
->spec
, fmt
, PBM_LAST
, Qpbm
);
7667 /* Get foreground and background colors, maybe allocate colors. */
7668 if (fmt
[PBM_FOREGROUND
].count
7669 && STRINGP (fmt
[PBM_FOREGROUND
].value
))
7670 fg
= x_alloc_image_color (f
, img
, fmt
[PBM_FOREGROUND
].value
, fg
);
7671 if (fmt
[PBM_BACKGROUND
].count
7672 && STRINGP (fmt
[PBM_BACKGROUND
].value
))
7674 bg
= x_alloc_image_color (f
, img
, fmt
[PBM_BACKGROUND
].value
, bg
);
7675 img
->background
= bg
;
7676 img
->background_valid
= 1;
7679 for (y
= 0; y
< height
; ++y
)
7680 for (x
= 0; x
< width
; ++x
)
7690 g
= pbm_scan_number (&p
, end
);
7692 XPutPixel (ximg
, x
, y
, g
? fg
: bg
);
7697 for (y
= 0; y
< height
; ++y
)
7698 for (x
= 0; x
< width
; ++x
)
7702 if (type
== PBM_GRAY
)
7703 r
= g
= b
= raw_p
? *p
++ : pbm_scan_number (&p
, end
);
7712 r
= pbm_scan_number (&p
, end
);
7713 g
= pbm_scan_number (&p
, end
);
7714 b
= pbm_scan_number (&p
, end
);
7717 if (r
< 0 || g
< 0 || b
< 0)
7721 XDestroyImage (ximg
);
7722 image_error ("Invalid pixel value in image `%s'",
7727 /* RGB values are now in the range 0..max_color_idx.
7728 Scale this to the range 0..0xffff supported by X. */
7729 r
= (double) r
* 65535 / max_color_idx
;
7730 g
= (double) g
* 65535 / max_color_idx
;
7731 b
= (double) b
* 65535 / max_color_idx
;
7732 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
7736 /* Store in IMG->colors the colors allocated for the image, and
7737 free the color table. */
7738 img
->colors
= colors_in_color_table (&img
->ncolors
);
7739 free_color_table ();
7741 /* Maybe fill in the background field while we have ximg handy. */
7742 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
7743 IMAGE_BACKGROUND (img
, f
, ximg
);
7745 /* Put the image into a pixmap. */
7746 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
7747 x_destroy_x_image (ximg
);
7750 img
->height
= height
;
7759 /***********************************************************************
7761 ***********************************************************************/
7765 #if defined HAVE_LIBPNG_PNG_H
7766 # include <libpng/png.h>
7771 /* Function prototypes. */
7773 static int png_image_p
P_ ((Lisp_Object object
));
7774 static int png_load
P_ ((struct frame
*f
, struct image
*img
));
7776 /* The symbol `png' identifying images of this type. */
7780 /* Indices of image specification fields in png_format, below. */
7782 enum png_keyword_index
7797 /* Vector of image_keyword structures describing the format
7798 of valid user-defined image specifications. */
7800 static struct image_keyword png_format
[PNG_LAST
] =
7802 {":type", IMAGE_SYMBOL_VALUE
, 1},
7803 {":data", IMAGE_STRING_VALUE
, 0},
7804 {":file", IMAGE_STRING_VALUE
, 0},
7805 {":ascent", IMAGE_ASCENT_VALUE
, 0},
7806 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
7807 {":relief", IMAGE_INTEGER_VALUE
, 0},
7808 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7809 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7810 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
7811 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
7814 /* Structure describing the image type `png'. */
7816 static struct image_type png_type
=
7826 /* Return non-zero if OBJECT is a valid PNG image specification. */
7829 png_image_p (object
)
7832 struct image_keyword fmt
[PNG_LAST
];
7833 bcopy (png_format
, fmt
, sizeof fmt
);
7835 if (!parse_image_spec (object
, fmt
, PNG_LAST
, Qpng
))
7838 /* Must specify either the :data or :file keyword. */
7839 return fmt
[PNG_FILE
].count
+ fmt
[PNG_DATA
].count
== 1;
7843 /* Error and warning handlers installed when the PNG library
7847 my_png_error (png_ptr
, msg
)
7848 png_struct
*png_ptr
;
7851 xassert (png_ptr
!= NULL
);
7852 image_error ("PNG error: %s", build_string (msg
), Qnil
);
7853 longjmp (png_ptr
->jmpbuf
, 1);
7858 my_png_warning (png_ptr
, msg
)
7859 png_struct
*png_ptr
;
7862 xassert (png_ptr
!= NULL
);
7863 image_error ("PNG warning: %s", build_string (msg
), Qnil
);
7866 /* Memory source for PNG decoding. */
7868 struct png_memory_storage
7870 unsigned char *bytes
; /* The data */
7871 size_t len
; /* How big is it? */
7872 int index
; /* Where are we? */
7876 /* Function set as reader function when reading PNG image from memory.
7877 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
7878 bytes from the input to DATA. */
7881 png_read_from_memory (png_ptr
, data
, length
)
7882 png_structp png_ptr
;
7886 struct png_memory_storage
*tbr
7887 = (struct png_memory_storage
*) png_get_io_ptr (png_ptr
);
7889 if (length
> tbr
->len
- tbr
->index
)
7890 png_error (png_ptr
, "Read error");
7892 bcopy (tbr
->bytes
+ tbr
->index
, data
, length
);
7893 tbr
->index
= tbr
->index
+ length
;
7896 /* Load PNG image IMG for use on frame F. Value is non-zero if
7904 Lisp_Object file
, specified_file
;
7905 Lisp_Object specified_data
;
7907 XImage
*ximg
, *mask_img
= NULL
;
7908 struct gcpro gcpro1
;
7909 png_struct
*png_ptr
= NULL
;
7910 png_info
*info_ptr
= NULL
, *end_info
= NULL
;
7911 FILE *volatile fp
= NULL
;
7913 png_byte
* volatile pixels
= NULL
;
7914 png_byte
** volatile rows
= NULL
;
7915 png_uint_32 width
, height
;
7916 int bit_depth
, color_type
, interlace_type
;
7918 png_uint_32 row_bytes
;
7920 double screen_gamma
;
7921 struct png_memory_storage tbr
; /* Data to be read */
7923 /* Find out what file to load. */
7924 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
7925 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
7929 if (NILP (specified_data
))
7931 file
= x_find_image_file (specified_file
);
7932 if (!STRINGP (file
))
7934 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
7939 /* Open the image file. */
7940 fp
= fopen (SDATA (file
), "rb");
7943 image_error ("Cannot open image file `%s'", file
, Qnil
);
7949 /* Check PNG signature. */
7950 if (fread (sig
, 1, sizeof sig
, fp
) != sizeof sig
7951 || !png_check_sig (sig
, sizeof sig
))
7953 image_error ("Not a PNG file: `%s'", file
, Qnil
);
7961 /* Read from memory. */
7962 tbr
.bytes
= SDATA (specified_data
);
7963 tbr
.len
= SBYTES (specified_data
);
7966 /* Check PNG signature. */
7967 if (tbr
.len
< sizeof sig
7968 || !png_check_sig (tbr
.bytes
, sizeof sig
))
7970 image_error ("Not a PNG image: `%s'", img
->spec
, Qnil
);
7975 /* Need to skip past the signature. */
7976 tbr
.bytes
+= sizeof (sig
);
7979 /* Initialize read and info structs for PNG lib. */
7980 png_ptr
= png_create_read_struct (PNG_LIBPNG_VER_STRING
, NULL
,
7981 my_png_error
, my_png_warning
);
7984 if (fp
) fclose (fp
);
7989 info_ptr
= png_create_info_struct (png_ptr
);
7992 png_destroy_read_struct (&png_ptr
, NULL
, NULL
);
7993 if (fp
) fclose (fp
);
7998 end_info
= png_create_info_struct (png_ptr
);
8001 png_destroy_read_struct (&png_ptr
, &info_ptr
, NULL
);
8002 if (fp
) fclose (fp
);
8007 /* Set error jump-back. We come back here when the PNG library
8008 detects an error. */
8009 if (setjmp (png_ptr
->jmpbuf
))
8013 png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
8016 if (fp
) fclose (fp
);
8021 /* Read image info. */
8022 if (!NILP (specified_data
))
8023 png_set_read_fn (png_ptr
, (void *) &tbr
, png_read_from_memory
);
8025 png_init_io (png_ptr
, fp
);
8027 png_set_sig_bytes (png_ptr
, sizeof sig
);
8028 png_read_info (png_ptr
, info_ptr
);
8029 png_get_IHDR (png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,
8030 &interlace_type
, NULL
, NULL
);
8032 /* If image contains simply transparency data, we prefer to
8033 construct a clipping mask. */
8034 if (png_get_valid (png_ptr
, info_ptr
, PNG_INFO_tRNS
))
8039 /* This function is easier to write if we only have to handle
8040 one data format: RGB or RGBA with 8 bits per channel. Let's
8041 transform other formats into that format. */
8043 /* Strip more than 8 bits per channel. */
8044 if (bit_depth
== 16)
8045 png_set_strip_16 (png_ptr
);
8047 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
8049 png_set_expand (png_ptr
);
8051 /* Convert grayscale images to RGB. */
8052 if (color_type
== PNG_COLOR_TYPE_GRAY
8053 || color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
8054 png_set_gray_to_rgb (png_ptr
);
8056 screen_gamma
= (f
->gamma
? 1 / f
->gamma
/ 0.45455 : 2.2);
8058 #if 0 /* Avoid double gamma correction for PNG images. */
8059 { /* Tell the PNG lib to handle gamma correction for us. */
8062 #if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
8063 if (png_get_sRGB (png_ptr
, info_ptr
, &intent
))
8064 /* The libpng documentation says this is right in this case. */
8065 png_set_gamma (png_ptr
, screen_gamma
, 0.45455);
8068 if (png_get_gAMA (png_ptr
, info_ptr
, &image_gamma
))
8069 /* Image contains gamma information. */
8070 png_set_gamma (png_ptr
, screen_gamma
, image_gamma
);
8072 /* Use the standard default for the image gamma. */
8073 png_set_gamma (png_ptr
, screen_gamma
, 0.45455);
8077 /* Handle alpha channel by combining the image with a background
8078 color. Do this only if a real alpha channel is supplied. For
8079 simple transparency, we prefer a clipping mask. */
8082 png_color_16
*image_bg
;
8083 Lisp_Object specified_bg
8084 = image_spec_value (img
->spec
, QCbackground
, NULL
);
8086 if (STRINGP (specified_bg
))
8087 /* The user specified `:background', use that. */
8090 if (x_defined_color (f
, SDATA (specified_bg
), &color
, 0))
8092 png_color_16 user_bg
;
8094 bzero (&user_bg
, sizeof user_bg
);
8095 user_bg
.red
= color
.red
;
8096 user_bg
.green
= color
.green
;
8097 user_bg
.blue
= color
.blue
;
8099 png_set_background (png_ptr
, &user_bg
,
8100 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
8103 else if (png_get_bKGD (png_ptr
, info_ptr
, &image_bg
))
8104 /* Image contains a background color with which to
8105 combine the image. */
8106 png_set_background (png_ptr
, image_bg
,
8107 PNG_BACKGROUND_GAMMA_FILE
, 1, 1.0);
8110 /* Image does not contain a background color with which
8111 to combine the image data via an alpha channel. Use
8112 the frame's background instead. */
8115 png_color_16 frame_background
;
8117 cmap
= FRAME_X_COLORMAP (f
);
8118 color
.pixel
= FRAME_BACKGROUND_PIXEL (f
);
8119 x_query_color (f
, &color
);
8121 bzero (&frame_background
, sizeof frame_background
);
8122 frame_background
.red
= color
.red
;
8123 frame_background
.green
= color
.green
;
8124 frame_background
.blue
= color
.blue
;
8126 png_set_background (png_ptr
, &frame_background
,
8127 PNG_BACKGROUND_GAMMA_SCREEN
, 0, 1.0);
8131 /* Update info structure. */
8132 png_read_update_info (png_ptr
, info_ptr
);
8134 /* Get number of channels. Valid values are 1 for grayscale images
8135 and images with a palette, 2 for grayscale images with transparency
8136 information (alpha channel), 3 for RGB images, and 4 for RGB
8137 images with alpha channel, i.e. RGBA. If conversions above were
8138 sufficient we should only have 3 or 4 channels here. */
8139 channels
= png_get_channels (png_ptr
, info_ptr
);
8140 xassert (channels
== 3 || channels
== 4);
8142 /* Number of bytes needed for one row of the image. */
8143 row_bytes
= png_get_rowbytes (png_ptr
, info_ptr
);
8145 /* Allocate memory for the image. */
8146 pixels
= (png_byte
*) xmalloc (row_bytes
* height
* sizeof *pixels
);
8147 rows
= (png_byte
**) xmalloc (height
* sizeof *rows
);
8148 for (i
= 0; i
< height
; ++i
)
8149 rows
[i
] = pixels
+ i
* row_bytes
;
8151 /* Read the entire image. */
8152 png_read_image (png_ptr
, rows
);
8153 png_read_end (png_ptr
, info_ptr
);
8160 /* Create the X image and pixmap. */
8161 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
,
8165 /* Create an image and pixmap serving as mask if the PNG image
8166 contains an alpha channel. */
8169 && !x_create_x_image_and_pixmap (f
, width
, height
, 1,
8170 &mask_img
, &img
->mask
))
8172 x_destroy_x_image (ximg
);
8173 XFreePixmap (FRAME_X_DISPLAY (f
), img
->pixmap
);
8178 /* Fill the X image and mask from PNG data. */
8179 init_color_table ();
8181 for (y
= 0; y
< height
; ++y
)
8183 png_byte
*p
= rows
[y
];
8185 for (x
= 0; x
< width
; ++x
)
8192 XPutPixel (ximg
, x
, y
, lookup_rgb_color (f
, r
, g
, b
));
8194 /* An alpha channel, aka mask channel, associates variable
8195 transparency with an image. Where other image formats
8196 support binary transparency---fully transparent or fully
8197 opaque---PNG allows up to 254 levels of partial transparency.
8198 The PNG library implements partial transparency by combining
8199 the image with a specified background color.
8201 I'm not sure how to handle this here nicely: because the
8202 background on which the image is displayed may change, for
8203 real alpha channel support, it would be necessary to create
8204 a new image for each possible background.
8206 What I'm doing now is that a mask is created if we have
8207 boolean transparency information. Otherwise I'm using
8208 the frame's background color to combine the image with. */
8213 XPutPixel (mask_img
, x
, y
, *p
> 0);
8219 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
8220 /* Set IMG's background color from the PNG image, unless the user
8224 if (png_get_bKGD (png_ptr
, info_ptr
, &bg
))
8226 img
->background
= lookup_rgb_color (f
, bg
->red
, bg
->green
, bg
->blue
);
8227 img
->background_valid
= 1;
8231 /* Remember colors allocated for this image. */
8232 img
->colors
= colors_in_color_table (&img
->ncolors
);
8233 free_color_table ();
8236 png_destroy_read_struct (&png_ptr
, &info_ptr
, &end_info
);
8241 img
->height
= height
;
8243 /* Maybe fill in the background field while we have ximg handy. */
8244 IMAGE_BACKGROUND (img
, f
, ximg
);
8246 /* Put the image into the pixmap, then free the X image and its buffer. */
8247 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8248 x_destroy_x_image (ximg
);
8250 /* Same for the mask. */
8253 /* Fill in the background_transparent field while we have the mask
8255 image_background_transparent (img
, f
, mask_img
);
8257 x_put_x_image (f
, mask_img
, img
->mask
, img
->width
, img
->height
);
8258 x_destroy_x_image (mask_img
);
8265 #endif /* HAVE_PNG != 0 */
8269 /***********************************************************************
8271 ***********************************************************************/
8275 /* Work around a warning about HAVE_STDLIB_H being redefined in
8277 #ifdef HAVE_STDLIB_H
8278 #define HAVE_STDLIB_H_1
8279 #undef HAVE_STDLIB_H
8280 #endif /* HAVE_STLIB_H */
8282 #include <jpeglib.h>
8286 #ifdef HAVE_STLIB_H_1
8287 #define HAVE_STDLIB_H 1
8290 static int jpeg_image_p
P_ ((Lisp_Object object
));
8291 static int jpeg_load
P_ ((struct frame
*f
, struct image
*img
));
8293 /* The symbol `jpeg' identifying images of this type. */
8297 /* Indices of image specification fields in gs_format, below. */
8299 enum jpeg_keyword_index
8308 JPEG_HEURISTIC_MASK
,
8314 /* Vector of image_keyword structures describing the format
8315 of valid user-defined image specifications. */
8317 static struct image_keyword jpeg_format
[JPEG_LAST
] =
8319 {":type", IMAGE_SYMBOL_VALUE
, 1},
8320 {":data", IMAGE_STRING_VALUE
, 0},
8321 {":file", IMAGE_STRING_VALUE
, 0},
8322 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8323 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
8324 {":relief", IMAGE_INTEGER_VALUE
, 0},
8325 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8326 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8327 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8328 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8331 /* Structure describing the image type `jpeg'. */
8333 static struct image_type jpeg_type
=
8343 /* Return non-zero if OBJECT is a valid JPEG image specification. */
8346 jpeg_image_p (object
)
8349 struct image_keyword fmt
[JPEG_LAST
];
8351 bcopy (jpeg_format
, fmt
, sizeof fmt
);
8353 if (!parse_image_spec (object
, fmt
, JPEG_LAST
, Qjpeg
))
8356 /* Must specify either the :data or :file keyword. */
8357 return fmt
[JPEG_FILE
].count
+ fmt
[JPEG_DATA
].count
== 1;
8361 struct my_jpeg_error_mgr
8363 struct jpeg_error_mgr pub
;
8364 jmp_buf setjmp_buffer
;
8369 my_error_exit (cinfo
)
8372 struct my_jpeg_error_mgr
*mgr
= (struct my_jpeg_error_mgr
*) cinfo
->err
;
8373 longjmp (mgr
->setjmp_buffer
, 1);
8377 /* Init source method for JPEG data source manager. Called by
8378 jpeg_read_header() before any data is actually read. See
8379 libjpeg.doc from the JPEG lib distribution. */
8382 our_init_source (cinfo
)
8383 j_decompress_ptr cinfo
;
8388 /* Fill input buffer method for JPEG data source manager. Called
8389 whenever more data is needed. We read the whole image in one step,
8390 so this only adds a fake end of input marker at the end. */
8393 our_fill_input_buffer (cinfo
)
8394 j_decompress_ptr cinfo
;
8396 /* Insert a fake EOI marker. */
8397 struct jpeg_source_mgr
*src
= cinfo
->src
;
8398 static JOCTET buffer
[2];
8400 buffer
[0] = (JOCTET
) 0xFF;
8401 buffer
[1] = (JOCTET
) JPEG_EOI
;
8403 src
->next_input_byte
= buffer
;
8404 src
->bytes_in_buffer
= 2;
8409 /* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
8410 is the JPEG data source manager. */
8413 our_skip_input_data (cinfo
, num_bytes
)
8414 j_decompress_ptr cinfo
;
8417 struct jpeg_source_mgr
*src
= (struct jpeg_source_mgr
*) cinfo
->src
;
8421 if (num_bytes
> src
->bytes_in_buffer
)
8422 ERREXIT (cinfo
, JERR_INPUT_EOF
);
8424 src
->bytes_in_buffer
-= num_bytes
;
8425 src
->next_input_byte
+= num_bytes
;
8430 /* Method to terminate data source. Called by
8431 jpeg_finish_decompress() after all data has been processed. */
8434 our_term_source (cinfo
)
8435 j_decompress_ptr cinfo
;
8440 /* Set up the JPEG lib for reading an image from DATA which contains
8441 LEN bytes. CINFO is the decompression info structure created for
8442 reading the image. */
8445 jpeg_memory_src (cinfo
, data
, len
)
8446 j_decompress_ptr cinfo
;
8450 struct jpeg_source_mgr
*src
;
8452 if (cinfo
->src
== NULL
)
8454 /* First time for this JPEG object? */
8455 cinfo
->src
= (struct jpeg_source_mgr
*)
8456 (*cinfo
->mem
->alloc_small
) ((j_common_ptr
) cinfo
, JPOOL_PERMANENT
,
8457 sizeof (struct jpeg_source_mgr
));
8458 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
8459 src
->next_input_byte
= data
;
8462 src
= (struct jpeg_source_mgr
*) cinfo
->src
;
8463 src
->init_source
= our_init_source
;
8464 src
->fill_input_buffer
= our_fill_input_buffer
;
8465 src
->skip_input_data
= our_skip_input_data
;
8466 src
->resync_to_restart
= jpeg_resync_to_restart
; /* Use default method. */
8467 src
->term_source
= our_term_source
;
8468 src
->bytes_in_buffer
= len
;
8469 src
->next_input_byte
= data
;
8473 /* Load image IMG for use on frame F. Patterned after example.c
8474 from the JPEG lib. */
8481 struct jpeg_decompress_struct cinfo
;
8482 struct my_jpeg_error_mgr mgr
;
8483 Lisp_Object file
, specified_file
;
8484 Lisp_Object specified_data
;
8485 FILE * volatile fp
= NULL
;
8487 int row_stride
, x
, y
;
8488 XImage
*ximg
= NULL
;
8490 unsigned long *colors
;
8492 struct gcpro gcpro1
;
8494 /* Open the JPEG file. */
8495 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
8496 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8500 if (NILP (specified_data
))
8502 file
= x_find_image_file (specified_file
);
8503 if (!STRINGP (file
))
8505 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
8510 fp
= fopen (SDATA (file
), "r");
8513 image_error ("Cannot open `%s'", file
, Qnil
);
8519 /* Customize libjpeg's error handling to call my_error_exit when an
8520 error is detected. This function will perform a longjmp. */
8521 cinfo
.err
= jpeg_std_error (&mgr
.pub
);
8522 mgr
.pub
.error_exit
= my_error_exit
;
8524 if ((rc
= setjmp (mgr
.setjmp_buffer
)) != 0)
8528 /* Called from my_error_exit. Display a JPEG error. */
8529 char buffer
[JMSG_LENGTH_MAX
];
8530 cinfo
.err
->format_message ((j_common_ptr
) &cinfo
, buffer
);
8531 image_error ("Error reading JPEG image `%s': %s", img
->spec
,
8532 build_string (buffer
));
8535 /* Close the input file and destroy the JPEG object. */
8537 fclose ((FILE *) fp
);
8538 jpeg_destroy_decompress (&cinfo
);
8540 /* If we already have an XImage, free that. */
8541 x_destroy_x_image (ximg
);
8543 /* Free pixmap and colors. */
8544 x_clear_image (f
, img
);
8550 /* Create the JPEG decompression object. Let it read from fp.
8551 Read the JPEG image header. */
8552 jpeg_create_decompress (&cinfo
);
8554 if (NILP (specified_data
))
8555 jpeg_stdio_src (&cinfo
, (FILE *) fp
);
8557 jpeg_memory_src (&cinfo
, SDATA (specified_data
),
8558 SBYTES (specified_data
));
8560 jpeg_read_header (&cinfo
, TRUE
);
8562 /* Customize decompression so that color quantization will be used.
8563 Start decompression. */
8564 cinfo
.quantize_colors
= TRUE
;
8565 jpeg_start_decompress (&cinfo
);
8566 width
= img
->width
= cinfo
.output_width
;
8567 height
= img
->height
= cinfo
.output_height
;
8569 /* Create X image and pixmap. */
8570 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8571 longjmp (mgr
.setjmp_buffer
, 2);
8573 /* Allocate colors. When color quantization is used,
8574 cinfo.actual_number_of_colors has been set with the number of
8575 colors generated, and cinfo.colormap is a two-dimensional array
8576 of color indices in the range 0..cinfo.actual_number_of_colors.
8577 No more than 255 colors will be generated. */
8581 if (cinfo
.out_color_components
> 2)
8582 ir
= 0, ig
= 1, ib
= 2;
8583 else if (cinfo
.out_color_components
> 1)
8584 ir
= 0, ig
= 1, ib
= 0;
8586 ir
= 0, ig
= 0, ib
= 0;
8588 /* Use the color table mechanism because it handles colors that
8589 cannot be allocated nicely. Such colors will be replaced with
8590 a default color, and we don't have to care about which colors
8591 can be freed safely, and which can't. */
8592 init_color_table ();
8593 colors
= (unsigned long *) alloca (cinfo
.actual_number_of_colors
8596 for (i
= 0; i
< cinfo
.actual_number_of_colors
; ++i
)
8598 /* Multiply RGB values with 255 because X expects RGB values
8599 in the range 0..0xffff. */
8600 int r
= cinfo
.colormap
[ir
][i
] << 8;
8601 int g
= cinfo
.colormap
[ig
][i
] << 8;
8602 int b
= cinfo
.colormap
[ib
][i
] << 8;
8603 colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
8606 /* Remember those colors actually allocated. */
8607 img
->colors
= colors_in_color_table (&img
->ncolors
);
8608 free_color_table ();
8612 row_stride
= width
* cinfo
.output_components
;
8613 buffer
= cinfo
.mem
->alloc_sarray ((j_common_ptr
) &cinfo
, JPOOL_IMAGE
,
8615 for (y
= 0; y
< height
; ++y
)
8617 jpeg_read_scanlines (&cinfo
, buffer
, 1);
8618 for (x
= 0; x
< cinfo
.output_width
; ++x
)
8619 XPutPixel (ximg
, x
, y
, colors
[buffer
[0][x
]]);
8623 jpeg_finish_decompress (&cinfo
);
8624 jpeg_destroy_decompress (&cinfo
);
8626 fclose ((FILE *) fp
);
8628 /* Maybe fill in the background field while we have ximg handy. */
8629 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
8630 IMAGE_BACKGROUND (img
, f
, ximg
);
8632 /* Put the image into the pixmap. */
8633 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8634 x_destroy_x_image (ximg
);
8639 #endif /* HAVE_JPEG */
8643 /***********************************************************************
8645 ***********************************************************************/
8651 static int tiff_image_p
P_ ((Lisp_Object object
));
8652 static int tiff_load
P_ ((struct frame
*f
, struct image
*img
));
8654 /* The symbol `tiff' identifying images of this type. */
8658 /* Indices of image specification fields in tiff_format, below. */
8660 enum tiff_keyword_index
8669 TIFF_HEURISTIC_MASK
,
8675 /* Vector of image_keyword structures describing the format
8676 of valid user-defined image specifications. */
8678 static struct image_keyword tiff_format
[TIFF_LAST
] =
8680 {":type", IMAGE_SYMBOL_VALUE
, 1},
8681 {":data", IMAGE_STRING_VALUE
, 0},
8682 {":file", IMAGE_STRING_VALUE
, 0},
8683 {":ascent", IMAGE_ASCENT_VALUE
, 0},
8684 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
8685 {":relief", IMAGE_INTEGER_VALUE
, 0},
8686 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8687 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8688 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
8689 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
8692 /* Structure describing the image type `tiff'. */
8694 static struct image_type tiff_type
=
8704 /* Return non-zero if OBJECT is a valid TIFF image specification. */
8707 tiff_image_p (object
)
8710 struct image_keyword fmt
[TIFF_LAST
];
8711 bcopy (tiff_format
, fmt
, sizeof fmt
);
8713 if (!parse_image_spec (object
, fmt
, TIFF_LAST
, Qtiff
))
8716 /* Must specify either the :data or :file keyword. */
8717 return fmt
[TIFF_FILE
].count
+ fmt
[TIFF_DATA
].count
== 1;
8721 /* Reading from a memory buffer for TIFF images Based on the PNG
8722 memory source, but we have to provide a lot of extra functions.
8725 We really only need to implement read and seek, but I am not
8726 convinced that the TIFF library is smart enough not to destroy
8727 itself if we only hand it the function pointers we need to
8732 unsigned char *bytes
;
8740 tiff_read_from_memory (data
, buf
, size
)
8745 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
8747 if (size
> src
->len
- src
->index
)
8749 bcopy (src
->bytes
+ src
->index
, buf
, size
);
8756 tiff_write_from_memory (data
, buf
, size
)
8766 tiff_seek_in_memory (data
, off
, whence
)
8771 tiff_memory_source
*src
= (tiff_memory_source
*) data
;
8776 case SEEK_SET
: /* Go from beginning of source. */
8780 case SEEK_END
: /* Go from end of source. */
8781 idx
= src
->len
+ off
;
8784 case SEEK_CUR
: /* Go from current position. */
8785 idx
= src
->index
+ off
;
8788 default: /* Invalid `whence'. */
8792 if (idx
> src
->len
|| idx
< 0)
8801 tiff_close_memory (data
)
8810 tiff_mmap_memory (data
, pbase
, psize
)
8815 /* It is already _IN_ memory. */
8821 tiff_unmap_memory (data
, base
, size
)
8826 /* We don't need to do this. */
8831 tiff_size_of_memory (data
)
8834 return ((tiff_memory_source
*) data
)->len
;
8839 tiff_error_handler (title
, format
, ap
)
8840 const char *title
, *format
;
8846 len
= sprintf (buf
, "TIFF error: %s ", title
);
8847 vsprintf (buf
+ len
, format
, ap
);
8848 add_to_log (buf
, Qnil
, Qnil
);
8853 tiff_warning_handler (title
, format
, ap
)
8854 const char *title
, *format
;
8860 len
= sprintf (buf
, "TIFF warning: %s ", title
);
8861 vsprintf (buf
+ len
, format
, ap
);
8862 add_to_log (buf
, Qnil
, Qnil
);
8866 /* Load TIFF image IMG for use on frame F. Value is non-zero if
8874 Lisp_Object file
, specified_file
;
8875 Lisp_Object specified_data
;
8877 int width
, height
, x
, y
;
8881 struct gcpro gcpro1
;
8882 tiff_memory_source memsrc
;
8884 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
8885 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
8889 TIFFSetErrorHandler (tiff_error_handler
);
8890 TIFFSetWarningHandler (tiff_warning_handler
);
8892 if (NILP (specified_data
))
8894 /* Read from a file */
8895 file
= x_find_image_file (specified_file
);
8896 if (!STRINGP (file
))
8898 image_error ("Cannot find image file `%s'", file
, Qnil
);
8903 /* Try to open the image file. */
8904 tiff
= TIFFOpen (SDATA (file
), "r");
8907 image_error ("Cannot open `%s'", file
, Qnil
);
8914 /* Memory source! */
8915 memsrc
.bytes
= SDATA (specified_data
);
8916 memsrc
.len
= SBYTES (specified_data
);
8919 tiff
= TIFFClientOpen ("memory_source", "r", &memsrc
,
8920 (TIFFReadWriteProc
) tiff_read_from_memory
,
8921 (TIFFReadWriteProc
) tiff_write_from_memory
,
8922 tiff_seek_in_memory
,
8924 tiff_size_of_memory
,
8930 image_error ("Cannot open memory source for `%s'", img
->spec
, Qnil
);
8936 /* Get width and height of the image, and allocate a raster buffer
8937 of width x height 32-bit values. */
8938 TIFFGetField (tiff
, TIFFTAG_IMAGEWIDTH
, &width
);
8939 TIFFGetField (tiff
, TIFFTAG_IMAGELENGTH
, &height
);
8940 buf
= (uint32
*) xmalloc (width
* height
* sizeof *buf
);
8942 rc
= TIFFReadRGBAImage (tiff
, width
, height
, buf
, 0);
8946 image_error ("Error reading TIFF image `%s'", img
->spec
, Qnil
);
8952 /* Create the X image and pixmap. */
8953 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
8960 /* Initialize the color table. */
8961 init_color_table ();
8963 /* Process the pixel raster. Origin is in the lower-left corner. */
8964 for (y
= 0; y
< height
; ++y
)
8966 uint32
*row
= buf
+ y
* width
;
8968 for (x
= 0; x
< width
; ++x
)
8970 uint32 abgr
= row
[x
];
8971 int r
= TIFFGetR (abgr
) << 8;
8972 int g
= TIFFGetG (abgr
) << 8;
8973 int b
= TIFFGetB (abgr
) << 8;
8974 XPutPixel (ximg
, x
, height
- 1 - y
, lookup_rgb_color (f
, r
, g
, b
));
8978 /* Remember the colors allocated for the image. Free the color table. */
8979 img
->colors
= colors_in_color_table (&img
->ncolors
);
8980 free_color_table ();
8983 img
->height
= height
;
8985 /* Maybe fill in the background field while we have ximg handy. */
8986 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
8987 IMAGE_BACKGROUND (img
, f
, ximg
);
8989 /* Put the image into the pixmap, then free the X image and its buffer. */
8990 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
8991 x_destroy_x_image (ximg
);
8998 #endif /* HAVE_TIFF != 0 */
9002 /***********************************************************************
9004 ***********************************************************************/
9008 #include <gif_lib.h>
9010 static int gif_image_p
P_ ((Lisp_Object object
));
9011 static int gif_load
P_ ((struct frame
*f
, struct image
*img
));
9013 /* The symbol `gif' identifying images of this type. */
9017 /* Indices of image specification fields in gif_format, below. */
9019 enum gif_keyword_index
9035 /* Vector of image_keyword structures describing the format
9036 of valid user-defined image specifications. */
9038 static struct image_keyword gif_format
[GIF_LAST
] =
9040 {":type", IMAGE_SYMBOL_VALUE
, 1},
9041 {":data", IMAGE_STRING_VALUE
, 0},
9042 {":file", IMAGE_STRING_VALUE
, 0},
9043 {":ascent", IMAGE_ASCENT_VALUE
, 0},
9044 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
9045 {":relief", IMAGE_INTEGER_VALUE
, 0},
9046 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9047 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9048 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9049 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE
, 0},
9050 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
9053 /* Structure describing the image type `gif'. */
9055 static struct image_type gif_type
=
9065 /* Return non-zero if OBJECT is a valid GIF image specification. */
9068 gif_image_p (object
)
9071 struct image_keyword fmt
[GIF_LAST
];
9072 bcopy (gif_format
, fmt
, sizeof fmt
);
9074 if (!parse_image_spec (object
, fmt
, GIF_LAST
, Qgif
))
9077 /* Must specify either the :data or :file keyword. */
9078 return fmt
[GIF_FILE
].count
+ fmt
[GIF_DATA
].count
== 1;
9082 /* Reading a GIF image from memory
9083 Based on the PNG memory stuff to a certain extent. */
9087 unsigned char *bytes
;
9094 /* Make the current memory source available to gif_read_from_memory.
9095 It's done this way because not all versions of libungif support
9096 a UserData field in the GifFileType structure. */
9097 static gif_memory_source
*current_gif_memory_src
;
9100 gif_read_from_memory (file
, buf
, len
)
9105 gif_memory_source
*src
= current_gif_memory_src
;
9107 if (len
> src
->len
- src
->index
)
9110 bcopy (src
->bytes
+ src
->index
, buf
, len
);
9116 /* Load GIF image IMG for use on frame F. Value is non-zero if
9124 Lisp_Object file
, specified_file
;
9125 Lisp_Object specified_data
;
9126 int rc
, width
, height
, x
, y
, i
;
9128 ColorMapObject
*gif_color_map
;
9129 unsigned long pixel_colors
[256];
9131 struct gcpro gcpro1
;
9133 int ino
, image_left
, image_top
, image_width
, image_height
;
9134 gif_memory_source memsrc
;
9135 unsigned char *raster
;
9137 specified_file
= image_spec_value (img
->spec
, QCfile
, NULL
);
9138 specified_data
= image_spec_value (img
->spec
, QCdata
, NULL
);
9142 if (NILP (specified_data
))
9144 file
= x_find_image_file (specified_file
);
9145 if (!STRINGP (file
))
9147 image_error ("Cannot find image file `%s'", specified_file
, Qnil
);
9152 /* Open the GIF file. */
9153 gif
= DGifOpenFileName (SDATA (file
));
9156 image_error ("Cannot open `%s'", file
, Qnil
);
9163 /* Read from memory! */
9164 current_gif_memory_src
= &memsrc
;
9165 memsrc
.bytes
= SDATA (specified_data
);
9166 memsrc
.len
= SBYTES (specified_data
);
9169 gif
= DGifOpen (&memsrc
, gif_read_from_memory
);
9172 image_error ("Cannot open memory source `%s'", img
->spec
, Qnil
);
9178 /* Read entire contents. */
9179 rc
= DGifSlurp (gif
);
9180 if (rc
== GIF_ERROR
)
9182 image_error ("Error reading `%s'", img
->spec
, Qnil
);
9183 DGifCloseFile (gif
);
9188 image
= image_spec_value (img
->spec
, QCindex
, NULL
);
9189 ino
= INTEGERP (image
) ? XFASTINT (image
) : 0;
9190 if (ino
>= gif
->ImageCount
)
9192 image_error ("Invalid image number `%s' in image `%s'",
9194 DGifCloseFile (gif
);
9199 width
= img
->width
= max (gif
->SWidth
, gif
->Image
.Left
+ gif
->Image
.Width
);
9200 height
= img
->height
= max (gif
->SHeight
, gif
->Image
.Top
+ gif
->Image
.Height
);
9202 /* Create the X image and pixmap. */
9203 if (!x_create_x_image_and_pixmap (f
, width
, height
, 0, &ximg
, &img
->pixmap
))
9205 DGifCloseFile (gif
);
9210 /* Allocate colors. */
9211 gif_color_map
= gif
->SavedImages
[ino
].ImageDesc
.ColorMap
;
9213 gif_color_map
= gif
->SColorMap
;
9214 init_color_table ();
9215 bzero (pixel_colors
, sizeof pixel_colors
);
9217 for (i
= 0; i
< gif_color_map
->ColorCount
; ++i
)
9219 int r
= gif_color_map
->Colors
[i
].Red
<< 8;
9220 int g
= gif_color_map
->Colors
[i
].Green
<< 8;
9221 int b
= gif_color_map
->Colors
[i
].Blue
<< 8;
9222 pixel_colors
[i
] = lookup_rgb_color (f
, r
, g
, b
);
9225 img
->colors
= colors_in_color_table (&img
->ncolors
);
9226 free_color_table ();
9228 /* Clear the part of the screen image that are not covered by
9229 the image from the GIF file. Full animated GIF support
9230 requires more than can be done here (see the gif89 spec,
9231 disposal methods). Let's simply assume that the part
9232 not covered by a sub-image is in the frame's background color. */
9233 image_top
= gif
->SavedImages
[ino
].ImageDesc
.Top
;
9234 image_left
= gif
->SavedImages
[ino
].ImageDesc
.Left
;
9235 image_width
= gif
->SavedImages
[ino
].ImageDesc
.Width
;
9236 image_height
= gif
->SavedImages
[ino
].ImageDesc
.Height
;
9238 for (y
= 0; y
< image_top
; ++y
)
9239 for (x
= 0; x
< width
; ++x
)
9240 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
9242 for (y
= image_top
+ image_height
; y
< height
; ++y
)
9243 for (x
= 0; x
< width
; ++x
)
9244 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
9246 for (y
= image_top
; y
< image_top
+ image_height
; ++y
)
9248 for (x
= 0; x
< image_left
; ++x
)
9249 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
9250 for (x
= image_left
+ image_width
; x
< width
; ++x
)
9251 XPutPixel (ximg
, x
, y
, FRAME_BACKGROUND_PIXEL (f
));
9254 /* Read the GIF image into the X image. We use a local variable
9255 `raster' here because RasterBits below is a char *, and invites
9256 problems with bytes >= 0x80. */
9257 raster
= (unsigned char *) gif
->SavedImages
[ino
].RasterBits
;
9259 if (gif
->SavedImages
[ino
].ImageDesc
.Interlace
)
9261 static int interlace_start
[] = {0, 4, 2, 1};
9262 static int interlace_increment
[] = {8, 8, 4, 2};
9264 int row
= interlace_start
[0];
9268 for (y
= 0; y
< image_height
; y
++)
9270 if (row
>= image_height
)
9272 row
= interlace_start
[++pass
];
9273 while (row
>= image_height
)
9274 row
= interlace_start
[++pass
];
9277 for (x
= 0; x
< image_width
; x
++)
9279 int i
= raster
[(y
* image_width
) + x
];
9280 XPutPixel (ximg
, x
+ image_left
, row
+ image_top
,
9284 row
+= interlace_increment
[pass
];
9289 for (y
= 0; y
< image_height
; ++y
)
9290 for (x
= 0; x
< image_width
; ++x
)
9292 int i
= raster
[y
* image_width
+ x
];
9293 XPutPixel (ximg
, x
+ image_left
, y
+ image_top
, pixel_colors
[i
]);
9297 DGifCloseFile (gif
);
9299 /* Maybe fill in the background field while we have ximg handy. */
9300 if (NILP (image_spec_value (img
->spec
, QCbackground
, NULL
)))
9301 IMAGE_BACKGROUND (img
, f
, ximg
);
9303 /* Put the image into the pixmap, then free the X image and its buffer. */
9304 x_put_x_image (f
, ximg
, img
->pixmap
, width
, height
);
9305 x_destroy_x_image (ximg
);
9311 #endif /* HAVE_GIF != 0 */
9315 /***********************************************************************
9317 ***********************************************************************/
9319 static int gs_image_p
P_ ((Lisp_Object object
));
9320 static int gs_load
P_ ((struct frame
*f
, struct image
*img
));
9321 static void gs_clear_image
P_ ((struct frame
*f
, struct image
*img
));
9323 /* The symbol `postscript' identifying images of this type. */
9325 Lisp_Object Qpostscript
;
9327 /* Keyword symbols. */
9329 Lisp_Object QCloader
, QCbounding_box
, QCpt_width
, QCpt_height
;
9331 /* Indices of image specification fields in gs_format, below. */
9333 enum gs_keyword_index
9351 /* Vector of image_keyword structures describing the format
9352 of valid user-defined image specifications. */
9354 static struct image_keyword gs_format
[GS_LAST
] =
9356 {":type", IMAGE_SYMBOL_VALUE
, 1},
9357 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
9358 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE
, 1},
9359 {":file", IMAGE_STRING_VALUE
, 1},
9360 {":loader", IMAGE_FUNCTION_VALUE
, 0},
9361 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE
, 1},
9362 {":ascent", IMAGE_ASCENT_VALUE
, 0},
9363 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR
, 0},
9364 {":relief", IMAGE_INTEGER_VALUE
, 0},
9365 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9366 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9367 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE
, 0},
9368 {":background", IMAGE_STRING_OR_NIL_VALUE
, 0}
9371 /* Structure describing the image type `ghostscript'. */
9373 static struct image_type gs_type
=
9383 /* Free X resources of Ghostscript image IMG which is used on frame F. */
9386 gs_clear_image (f
, img
)
9390 /* IMG->data.ptr_val may contain a recorded colormap. */
9391 xfree (img
->data
.ptr_val
);
9392 x_clear_image (f
, img
);
9396 /* Return non-zero if OBJECT is a valid Ghostscript image
9403 struct image_keyword fmt
[GS_LAST
];
9407 bcopy (gs_format
, fmt
, sizeof fmt
);
9409 if (!parse_image_spec (object
, fmt
, GS_LAST
, Qpostscript
))
9412 /* Bounding box must be a list or vector containing 4 integers. */
9413 tem
= fmt
[GS_BOUNDING_BOX
].value
;
9416 for (i
= 0; i
< 4; ++i
, tem
= XCDR (tem
))
9417 if (!CONSP (tem
) || !INTEGERP (XCAR (tem
)))
9422 else if (VECTORP (tem
))
9424 if (XVECTOR (tem
)->size
!= 4)
9426 for (i
= 0; i
< 4; ++i
)
9427 if (!INTEGERP (XVECTOR (tem
)->contents
[i
]))
9437 /* Load Ghostscript image IMG for use on frame F. Value is non-zero
9446 Lisp_Object window_and_pixmap_id
= Qnil
, loader
, pt_height
, pt_width
;
9447 struct gcpro gcpro1
, gcpro2
;
9449 double in_width
, in_height
;
9450 Lisp_Object pixel_colors
= Qnil
;
9452 /* Compute pixel size of pixmap needed from the given size in the
9453 image specification. Sizes in the specification are in pt. 1 pt
9454 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9456 pt_width
= image_spec_value (img
->spec
, QCpt_width
, NULL
);
9457 in_width
= XFASTINT (pt_width
) / 72.0;
9458 img
->width
= in_width
* FRAME_X_DISPLAY_INFO (f
)->resx
;
9459 pt_height
= image_spec_value (img
->spec
, QCpt_height
, NULL
);
9460 in_height
= XFASTINT (pt_height
) / 72.0;
9461 img
->height
= in_height
* FRAME_X_DISPLAY_INFO (f
)->resy
;
9463 /* Create the pixmap. */
9464 xassert (img
->pixmap
== None
);
9465 img
->pixmap
= XCreatePixmap (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
9466 img
->width
, img
->height
,
9467 DefaultDepthOfScreen (FRAME_X_SCREEN (f
)));
9471 image_error ("Unable to create pixmap for `%s'", img
->spec
, Qnil
);
9475 /* Call the loader to fill the pixmap. It returns a process object
9476 if successful. We do not record_unwind_protect here because
9477 other places in redisplay like calling window scroll functions
9478 don't either. Let the Lisp loader use `unwind-protect' instead. */
9479 GCPRO2 (window_and_pixmap_id
, pixel_colors
);
9481 sprintf (buffer
, "%lu %lu",
9482 (unsigned long) FRAME_X_WINDOW (f
),
9483 (unsigned long) img
->pixmap
);
9484 window_and_pixmap_id
= build_string (buffer
);
9486 sprintf (buffer
, "%lu %lu",
9487 FRAME_FOREGROUND_PIXEL (f
),
9488 FRAME_BACKGROUND_PIXEL (f
));
9489 pixel_colors
= build_string (buffer
);
9491 XSETFRAME (frame
, f
);
9492 loader
= image_spec_value (img
->spec
, QCloader
, NULL
);
9494 loader
= intern ("gs-load-image");
9496 img
->data
.lisp_val
= call6 (loader
, frame
, img
->spec
,
9497 make_number (img
->width
),
9498 make_number (img
->height
),
9499 window_and_pixmap_id
,
9502 return PROCESSP (img
->data
.lisp_val
);
9506 /* Kill the Ghostscript process that was started to fill PIXMAP on
9507 frame F. Called from XTread_socket when receiving an event
9508 telling Emacs that Ghostscript has finished drawing. */
9511 x_kill_gs_process (pixmap
, f
)
9515 struct image_cache
*c
= FRAME_X_IMAGE_CACHE (f
);
9519 /* Find the image containing PIXMAP. */
9520 for (i
= 0; i
< c
->used
; ++i
)
9521 if (c
->images
[i
]->pixmap
== pixmap
)
9524 /* Should someone in between have cleared the image cache, for
9525 instance, give up. */
9529 /* Kill the GS process. We should have found PIXMAP in the image
9530 cache and its image should contain a process object. */
9532 xassert (PROCESSP (img
->data
.lisp_val
));
9533 Fkill_process (img
->data
.lisp_val
, Qnil
);
9534 img
->data
.lisp_val
= Qnil
;
9536 /* On displays with a mutable colormap, figure out the colors
9537 allocated for the image by looking at the pixels of an XImage for
9539 class = FRAME_X_VISUAL (f
)->class;
9540 if (class != StaticColor
&& class != StaticGray
&& class != TrueColor
)
9546 /* Try to get an XImage for img->pixmep. */
9547 ximg
= XGetImage (FRAME_X_DISPLAY (f
), img
->pixmap
,
9548 0, 0, img
->width
, img
->height
, ~0, ZPixmap
);
9553 /* Initialize the color table. */
9554 init_color_table ();
9556 /* For each pixel of the image, look its color up in the
9557 color table. After having done so, the color table will
9558 contain an entry for each color used by the image. */
9559 for (y
= 0; y
< img
->height
; ++y
)
9560 for (x
= 0; x
< img
->width
; ++x
)
9562 unsigned long pixel
= XGetPixel (ximg
, x
, y
);
9563 lookup_pixel_color (f
, pixel
);
9566 /* Record colors in the image. Free color table and XImage. */
9567 img
->colors
= colors_in_color_table (&img
->ncolors
);
9568 free_color_table ();
9569 XDestroyImage (ximg
);
9571 #if 0 /* This doesn't seem to be the case. If we free the colors
9572 here, we get a BadAccess later in x_clear_image when
9573 freeing the colors. */
9574 /* We have allocated colors once, but Ghostscript has also
9575 allocated colors on behalf of us. So, to get the
9576 reference counts right, free them once. */
9578 x_free_colors (f
, img
->colors
, img
->ncolors
);
9582 image_error ("Cannot get X image of `%s'; colors will not be freed",
9588 /* Now that we have the pixmap, compute mask and transform the
9589 image if requested. */
9591 postprocess_image (f
, img
);
9597 /***********************************************************************
9599 ***********************************************************************/
9601 DEFUN ("x-change-window-property", Fx_change_window_property
,
9602 Sx_change_window_property
, 2, 3, 0,
9603 doc
: /* Change window property PROP to VALUE on the X window of FRAME.
9604 PROP and VALUE must be strings. FRAME nil or omitted means use the
9605 selected frame. Value is VALUE. */)
9606 (prop
, value
, frame
)
9607 Lisp_Object frame
, prop
, value
;
9609 struct frame
*f
= check_x_frame (frame
);
9612 CHECK_STRING (prop
);
9613 CHECK_STRING (value
);
9616 prop_atom
= XInternAtom (FRAME_X_DISPLAY (f
), SDATA (prop
), False
);
9617 XChangeProperty (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
9618 prop_atom
, XA_STRING
, 8, PropModeReplace
,
9619 SDATA (value
), SCHARS (value
));
9621 /* Make sure the property is set when we return. */
9622 XFlush (FRAME_X_DISPLAY (f
));
9629 DEFUN ("x-delete-window-property", Fx_delete_window_property
,
9630 Sx_delete_window_property
, 1, 2, 0,
9631 doc
: /* Remove window property PROP from X window of FRAME.
9632 FRAME nil or omitted means use the selected frame. Value is PROP. */)
9634 Lisp_Object prop
, frame
;
9636 struct frame
*f
= check_x_frame (frame
);
9639 CHECK_STRING (prop
);
9641 prop_atom
= XInternAtom (FRAME_X_DISPLAY (f
), SDATA (prop
), False
);
9642 XDeleteProperty (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
), prop_atom
);
9644 /* Make sure the property is removed when we return. */
9645 XFlush (FRAME_X_DISPLAY (f
));
9652 DEFUN ("x-window-property", Fx_window_property
, Sx_window_property
,
9654 doc
: /* Value is the value of window property PROP on FRAME.
9655 If FRAME is nil or omitted, use the selected frame. Value is nil
9656 if FRAME hasn't a property with name PROP or if PROP has no string
9659 Lisp_Object prop
, frame
;
9661 struct frame
*f
= check_x_frame (frame
);
9664 Lisp_Object prop_value
= Qnil
;
9665 char *tmp_data
= NULL
;
9668 unsigned long actual_size
, bytes_remaining
;
9670 CHECK_STRING (prop
);
9672 prop_atom
= XInternAtom (FRAME_X_DISPLAY (f
), SDATA (prop
), False
);
9673 rc
= XGetWindowProperty (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
9674 prop_atom
, 0, 0, False
, XA_STRING
,
9675 &actual_type
, &actual_format
, &actual_size
,
9676 &bytes_remaining
, (unsigned char **) &tmp_data
);
9679 int size
= bytes_remaining
;
9684 rc
= XGetWindowProperty (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
9685 prop_atom
, 0, bytes_remaining
,
9687 &actual_type
, &actual_format
,
9688 &actual_size
, &bytes_remaining
,
9689 (unsigned char **) &tmp_data
);
9690 if (rc
== Success
&& tmp_data
)
9691 prop_value
= make_string (tmp_data
, size
);
9702 /***********************************************************************
9704 ***********************************************************************/
9706 /* If non-null, an asynchronous timer that, when it expires, displays
9707 an hourglass cursor on all frames. */
9709 static struct atimer
*hourglass_atimer
;
9711 /* Non-zero means an hourglass cursor is currently shown. */
9713 static int hourglass_shown_p
;
9715 /* Number of seconds to wait before displaying an hourglass cursor. */
9717 static Lisp_Object Vhourglass_delay
;
9719 /* Default number of seconds to wait before displaying an hourglass
9722 #define DEFAULT_HOURGLASS_DELAY 1
9724 /* Function prototypes. */
9726 static void show_hourglass
P_ ((struct atimer
*));
9727 static void hide_hourglass
P_ ((void));
9730 /* Cancel a currently active hourglass timer, and start a new one. */
9736 int secs
, usecs
= 0;
9738 cancel_hourglass ();
9740 if (INTEGERP (Vhourglass_delay
)
9741 && XINT (Vhourglass_delay
) > 0)
9742 secs
= XFASTINT (Vhourglass_delay
);
9743 else if (FLOATP (Vhourglass_delay
)
9744 && XFLOAT_DATA (Vhourglass_delay
) > 0)
9747 tem
= Ftruncate (Vhourglass_delay
, Qnil
);
9748 secs
= XFASTINT (tem
);
9749 usecs
= (XFLOAT_DATA (Vhourglass_delay
) - secs
) * 1000000;
9752 secs
= DEFAULT_HOURGLASS_DELAY
;
9754 EMACS_SET_SECS_USECS (delay
, secs
, usecs
);
9755 hourglass_atimer
= start_atimer (ATIMER_RELATIVE
, delay
,
9756 show_hourglass
, NULL
);
9760 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
9766 if (hourglass_atimer
)
9768 cancel_atimer (hourglass_atimer
);
9769 hourglass_atimer
= NULL
;
9772 if (hourglass_shown_p
)
9777 /* Timer function of hourglass_atimer. TIMER is equal to
9780 Display an hourglass pointer on all frames by mapping the frames'
9781 hourglass_window. Set the hourglass_p flag in the frames'
9782 output_data.x structure to indicate that an hourglass cursor is
9783 shown on the frames. */
9786 show_hourglass (timer
)
9787 struct atimer
*timer
;
9789 /* The timer implementation will cancel this timer automatically
9790 after this function has run. Set hourglass_atimer to null
9791 so that we know the timer doesn't have to be canceled. */
9792 hourglass_atimer
= NULL
;
9794 if (!hourglass_shown_p
)
9796 Lisp_Object rest
, frame
;
9800 FOR_EACH_FRAME (rest
, frame
)
9802 struct frame
*f
= XFRAME (frame
);
9804 if (FRAME_LIVE_P (f
) && FRAME_X_P (f
) && FRAME_X_DISPLAY (f
))
9806 Display
*dpy
= FRAME_X_DISPLAY (f
);
9808 #ifdef USE_X_TOOLKIT
9809 if (f
->output_data
.x
->widget
)
9811 if (FRAME_OUTER_WINDOW (f
))
9814 f
->output_data
.x
->hourglass_p
= 1;
9816 if (!f
->output_data
.x
->hourglass_window
)
9818 unsigned long mask
= CWCursor
;
9819 XSetWindowAttributes attrs
;
9821 attrs
.cursor
= f
->output_data
.x
->hourglass_cursor
;
9823 f
->output_data
.x
->hourglass_window
9824 = XCreateWindow (dpy
, FRAME_OUTER_WINDOW (f
),
9825 0, 0, 32000, 32000, 0, 0,
9831 XMapRaised (dpy
, f
->output_data
.x
->hourglass_window
);
9837 hourglass_shown_p
= 1;
9843 /* Hide the hourglass pointer on all frames, if it is currently
9849 if (hourglass_shown_p
)
9851 Lisp_Object rest
, frame
;
9854 FOR_EACH_FRAME (rest
, frame
)
9856 struct frame
*f
= XFRAME (frame
);
9859 /* Watch out for newly created frames. */
9860 && f
->output_data
.x
->hourglass_window
)
9862 XUnmapWindow (FRAME_X_DISPLAY (f
),
9863 f
->output_data
.x
->hourglass_window
);
9864 /* Sync here because XTread_socket looks at the
9865 hourglass_p flag that is reset to zero below. */
9866 XSync (FRAME_X_DISPLAY (f
), False
);
9867 f
->output_data
.x
->hourglass_p
= 0;
9871 hourglass_shown_p
= 0;
9878 /***********************************************************************
9880 ***********************************************************************/
9882 static Lisp_Object x_create_tip_frame
P_ ((struct x_display_info
*,
9883 Lisp_Object
, Lisp_Object
));
9884 static void compute_tip_xy
P_ ((struct frame
*, Lisp_Object
, Lisp_Object
,
9885 Lisp_Object
, int, int, int *, int *));
9887 /* The frame of a currently visible tooltip. */
9889 Lisp_Object tip_frame
;
9891 /* If non-nil, a timer started that hides the last tooltip when it
9894 Lisp_Object tip_timer
;
9897 /* If non-nil, a vector of 3 elements containing the last args
9898 with which x-show-tip was called. See there. */
9900 Lisp_Object last_show_tip_args
;
9902 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
9904 Lisp_Object Vx_max_tooltip_size
;
9908 unwind_create_tip_frame (frame
)
9911 Lisp_Object deleted
;
9913 deleted
= unwind_create_frame (frame
);
9914 if (EQ (deleted
, Qt
))
9924 /* Create a frame for a tooltip on the display described by DPYINFO.
9925 PARMS is a list of frame parameters. TEXT is the string to
9926 display in the tip frame. Value is the frame.
9928 Note that functions called here, esp. x_default_parameter can
9929 signal errors, for instance when a specified color name is
9930 undefined. We have to make sure that we're in a consistent state
9931 when this happens. */
9934 x_create_tip_frame (dpyinfo
, parms
, text
)
9935 struct x_display_info
*dpyinfo
;
9936 Lisp_Object parms
, text
;
9939 Lisp_Object frame
, tem
;
9941 long window_prompting
= 0;
9943 int count
= SPECPDL_INDEX ();
9944 struct gcpro gcpro1
, gcpro2
, gcpro3
;
9946 int face_change_count_before
= face_change_count
;
9948 struct buffer
*old_buffer
;
9952 /* Use this general default value to start with until we know if
9953 this frame has a specified name. */
9954 Vx_resource_name
= Vinvocation_name
;
9957 kb
= dpyinfo
->kboard
;
9959 kb
= &the_only_kboard
;
9962 /* Get the name of the frame to use for resource lookup. */
9963 name
= x_get_arg (dpyinfo
, parms
, Qname
, "name", "Name", RES_TYPE_STRING
);
9965 && !EQ (name
, Qunbound
)
9967 error ("Invalid frame name--not a string or nil");
9968 Vx_resource_name
= name
;
9971 GCPRO3 (parms
, name
, frame
);
9973 XSETFRAME (frame
, f
);
9975 buffer
= Fget_buffer_create (build_string (" *tip*"));
9976 Fset_window_buffer (FRAME_ROOT_WINDOW (f
), buffer
, Qnil
);
9977 old_buffer
= current_buffer
;
9978 set_buffer_internal_1 (XBUFFER (buffer
));
9979 current_buffer
->truncate_lines
= Qnil
;
9982 set_buffer_internal_1 (old_buffer
);
9984 FRAME_CAN_HAVE_SCROLL_BARS (f
) = 0;
9985 record_unwind_protect (unwind_create_tip_frame
, frame
);
9987 /* By setting the output method, we're essentially saying that
9988 the frame is live, as per FRAME_LIVE_P. If we get a signal
9989 from this point on, x_destroy_window might screw up reference
9991 f
->output_method
= output_x_window
;
9992 f
->output_data
.x
= (struct x_output
*) xmalloc (sizeof (struct x_output
));
9993 bzero (f
->output_data
.x
, sizeof (struct x_output
));
9994 f
->output_data
.x
->icon_bitmap
= -1;
9995 FRAME_FONTSET (f
) = -1;
9996 f
->output_data
.x
->scroll_bar_foreground_pixel
= -1;
9997 f
->output_data
.x
->scroll_bar_background_pixel
= -1;
9998 #ifdef USE_TOOLKIT_SCROLL_BARS
9999 f
->output_data
.x
->scroll_bar_top_shadow_pixel
= -1;
10000 f
->output_data
.x
->scroll_bar_bottom_shadow_pixel
= -1;
10001 #endif /* USE_TOOLKIT_SCROLL_BARS */
10002 f
->icon_name
= Qnil
;
10003 FRAME_X_DISPLAY_INFO (f
) = dpyinfo
;
10005 image_cache_refcount
= FRAME_X_IMAGE_CACHE (f
)->refcount
;
10006 dpyinfo_refcount
= dpyinfo
->reference_count
;
10007 #endif /* GLYPH_DEBUG */
10008 #ifdef MULTI_KBOARD
10009 FRAME_KBOARD (f
) = kb
;
10011 f
->output_data
.x
->parent_desc
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
10012 f
->output_data
.x
->explicit_parent
= 0;
10014 /* These colors will be set anyway later, but it's important
10015 to get the color reference counts right, so initialize them! */
10018 struct gcpro gcpro1
;
10020 black
= build_string ("black");
10022 f
->output_data
.x
->foreground_pixel
10023 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
10024 f
->output_data
.x
->background_pixel
10025 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
10026 f
->output_data
.x
->cursor_pixel
10027 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
10028 f
->output_data
.x
->cursor_foreground_pixel
10029 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
10030 f
->output_data
.x
->border_pixel
10031 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
10032 f
->output_data
.x
->mouse_pixel
10033 = x_decode_color (f
, black
, BLACK_PIX_DEFAULT (f
));
10037 /* Set the name; the functions to which we pass f expect the name to
10039 if (EQ (name
, Qunbound
) || NILP (name
))
10041 f
->name
= build_string (dpyinfo
->x_id_name
);
10042 f
->explicit_name
= 0;
10047 f
->explicit_name
= 1;
10048 /* use the frame's title when getting resources for this frame. */
10049 specbind (Qx_resource_name
, name
);
10052 /* Extract the window parameters from the supplied values that are
10053 needed to determine window geometry. */
10057 font
= x_get_arg (dpyinfo
, parms
, Qfont
, "font", "Font", RES_TYPE_STRING
);
10060 /* First, try whatever font the caller has specified. */
10061 if (STRINGP (font
))
10063 tem
= Fquery_fontset (font
, Qnil
);
10065 font
= x_new_fontset (f
, SDATA (tem
));
10067 font
= x_new_font (f
, SDATA (font
));
10070 /* Try out a font which we hope has bold and italic variations. */
10071 if (!STRINGP (font
))
10072 font
= x_new_font (f
, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
10073 if (!STRINGP (font
))
10074 font
= x_new_font (f
, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
10075 if (! STRINGP (font
))
10076 font
= x_new_font (f
, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
10077 if (! STRINGP (font
))
10078 /* This was formerly the first thing tried, but it finds too many fonts
10079 and takes too long. */
10080 font
= x_new_font (f
, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
10081 /* If those didn't work, look for something which will at least work. */
10082 if (! STRINGP (font
))
10083 font
= x_new_font (f
, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
10085 if (! STRINGP (font
))
10086 font
= build_string ("fixed");
10088 x_default_parameter (f
, parms
, Qfont
, font
,
10089 "font", "Font", RES_TYPE_STRING
);
10092 x_default_parameter (f
, parms
, Qborder_width
, make_number (2),
10093 "borderWidth", "BorderWidth", RES_TYPE_NUMBER
);
10095 /* This defaults to 2 in order to match xterm. We recognize either
10096 internalBorderWidth or internalBorder (which is what xterm calls
10098 if (NILP (Fassq (Qinternal_border_width
, parms
)))
10102 value
= x_get_arg (dpyinfo
, parms
, Qinternal_border_width
,
10103 "internalBorder", "internalBorder", RES_TYPE_NUMBER
);
10104 if (! EQ (value
, Qunbound
))
10105 parms
= Fcons (Fcons (Qinternal_border_width
, value
),
10109 x_default_parameter (f
, parms
, Qinternal_border_width
, make_number (1),
10110 "internalBorderWidth", "internalBorderWidth",
10113 /* Also do the stuff which must be set before the window exists. */
10114 x_default_parameter (f
, parms
, Qforeground_color
, build_string ("black"),
10115 "foreground", "Foreground", RES_TYPE_STRING
);
10116 x_default_parameter (f
, parms
, Qbackground_color
, build_string ("white"),
10117 "background", "Background", RES_TYPE_STRING
);
10118 x_default_parameter (f
, parms
, Qmouse_color
, build_string ("black"),
10119 "pointerColor", "Foreground", RES_TYPE_STRING
);
10120 x_default_parameter (f
, parms
, Qcursor_color
, build_string ("black"),
10121 "cursorColor", "Foreground", RES_TYPE_STRING
);
10122 x_default_parameter (f
, parms
, Qborder_color
, build_string ("black"),
10123 "borderColor", "BorderColor", RES_TYPE_STRING
);
10125 /* Init faces before x_default_parameter is called for scroll-bar
10126 parameters because that function calls x_set_scroll_bar_width,
10127 which calls change_frame_size, which calls Fset_window_buffer,
10128 which runs hooks, which call Fvertical_motion. At the end, we
10129 end up in init_iterator with a null face cache, which should not
10131 init_frame_faces (f
);
10133 f
->output_data
.x
->parent_desc
= FRAME_X_DISPLAY_INFO (f
)->root_window
;
10135 window_prompting
= x_figure_window_size (f
, parms
, 0);
10138 XSetWindowAttributes attrs
;
10139 unsigned long mask
;
10142 mask
= CWBackPixel
| CWOverrideRedirect
| CWEventMask
;
10143 if (DoesSaveUnders (dpyinfo
->screen
))
10144 mask
|= CWSaveUnder
;
10146 /* Window managers look at the override-redirect flag to determine
10147 whether or net to give windows a decoration (Xlib spec, chapter
10149 attrs
.override_redirect
= True
;
10150 attrs
.save_under
= True
;
10151 attrs
.background_pixel
= FRAME_BACKGROUND_PIXEL (f
);
10152 /* Arrange for getting MapNotify and UnmapNotify events. */
10153 attrs
.event_mask
= StructureNotifyMask
;
10155 = FRAME_X_WINDOW (f
)
10156 = XCreateWindow (FRAME_X_DISPLAY (f
),
10157 FRAME_X_DISPLAY_INFO (f
)->root_window
,
10158 /* x, y, width, height */
10162 CopyFromParent
, InputOutput
, CopyFromParent
,
10169 x_default_parameter (f
, parms
, Qauto_raise
, Qnil
,
10170 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN
);
10171 x_default_parameter (f
, parms
, Qauto_lower
, Qnil
,
10172 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN
);
10173 x_default_parameter (f
, parms
, Qcursor_type
, Qbox
,
10174 "cursorType", "CursorType", RES_TYPE_SYMBOL
);
10176 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
10177 Change will not be effected unless different from the current
10178 FRAME_LINES (f). */
10179 width
= FRAME_COLS (f
);
10180 height
= FRAME_LINES (f
);
10181 SET_FRAME_COLS (f
, 0);
10182 FRAME_LINES (f
) = 0;
10183 change_frame_size (f
, height
, width
, 1, 0, 0);
10185 /* Add `tooltip' frame parameter's default value. */
10186 if (NILP (Fframe_parameter (frame
, intern ("tooltip"))))
10187 Fmodify_frame_parameters (frame
, Fcons (Fcons (intern ("tooltip"), Qt
),
10190 /* Set up faces after all frame parameters are known. This call
10191 also merges in face attributes specified for new frames.
10193 Frame parameters may be changed if .Xdefaults contains
10194 specifications for the default font. For example, if there is an
10195 `Emacs.default.attributeBackground: pink', the `background-color'
10196 attribute of the frame get's set, which let's the internal border
10197 of the tooltip frame appear in pink. Prevent this. */
10199 Lisp_Object bg
= Fframe_parameter (frame
, Qbackground_color
);
10201 /* Set tip_frame here, so that */
10203 call1 (Qface_set_after_frame_default
, frame
);
10205 if (!EQ (bg
, Fframe_parameter (frame
, Qbackground_color
)))
10206 Fmodify_frame_parameters (frame
, Fcons (Fcons (Qbackground_color
, bg
),
10214 /* It is now ok to make the frame official even if we get an error
10215 below. And the frame needs to be on Vframe_list or making it
10216 visible won't work. */
10217 Vframe_list
= Fcons (frame
, Vframe_list
);
10219 /* Now that the frame is official, it counts as a reference to
10221 FRAME_X_DISPLAY_INFO (f
)->reference_count
++;
10223 /* Setting attributes of faces of the tooltip frame from resources
10224 and similar will increment face_change_count, which leads to the
10225 clearing of all current matrices. Since this isn't necessary
10226 here, avoid it by resetting face_change_count to the value it
10227 had before we created the tip frame. */
10228 face_change_count
= face_change_count_before
;
10230 /* Discard the unwind_protect. */
10231 return unbind_to (count
, frame
);
10235 /* Compute where to display tip frame F. PARMS is the list of frame
10236 parameters for F. DX and DY are specified offsets from the current
10237 location of the mouse. WIDTH and HEIGHT are the width and height
10238 of the tooltip. Return coordinates relative to the root window of
10239 the display in *ROOT_X, and *ROOT_Y. */
10242 compute_tip_xy (f
, parms
, dx
, dy
, width
, height
, root_x
, root_y
)
10244 Lisp_Object parms
, dx
, dy
;
10246 int *root_x
, *root_y
;
10248 Lisp_Object left
, top
;
10250 Window root
, child
;
10253 /* User-specified position? */
10254 left
= Fcdr (Fassq (Qleft
, parms
));
10255 top
= Fcdr (Fassq (Qtop
, parms
));
10257 /* Move the tooltip window where the mouse pointer is. Resize and
10259 if (!INTEGERP (left
) || !INTEGERP (top
))
10262 XQueryPointer (FRAME_X_DISPLAY (f
), FRAME_X_DISPLAY_INFO (f
)->root_window
,
10263 &root
, &child
, root_x
, root_y
, &win_x
, &win_y
, &pmask
);
10267 if (INTEGERP (top
))
10268 *root_y
= XINT (top
);
10269 else if (*root_y
+ XINT (dy
) - height
< 0)
10270 *root_y
-= XINT (dy
);
10274 *root_y
+= XINT (dy
);
10277 if (INTEGERP (left
))
10278 *root_x
= XINT (left
);
10279 else if (*root_x
+ XINT (dx
) + width
<= FRAME_X_DISPLAY_INFO (f
)->width
)
10280 /* It fits to the right of the pointer. */
10281 *root_x
+= XINT (dx
);
10282 else if (width
+ XINT (dx
) <= *root_x
)
10283 /* It fits to the left of the pointer. */
10284 *root_x
-= width
+ XINT (dx
);
10286 /* Put it left-justified on the screen--it ought to fit that way. */
10291 DEFUN ("x-show-tip", Fx_show_tip
, Sx_show_tip
, 1, 6, 0,
10292 doc
: /* Show STRING in a "tooltip" window on frame FRAME.
10293 A tooltip window is a small X window displaying a string.
10295 FRAME nil or omitted means use the selected frame.
10297 PARMS is an optional list of frame parameters which can be used to
10298 change the tooltip's appearance.
10300 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
10301 means use the default timeout of 5 seconds.
10303 If the list of frame parameters PARAMS contains a `left' parameters,
10304 the tooltip is displayed at that x-position. Otherwise it is
10305 displayed at the mouse position, with offset DX added (default is 5 if
10306 DX isn't specified). Likewise for the y-position; if a `top' frame
10307 parameter is specified, it determines the y-position of the tooltip
10308 window, otherwise it is displayed at the mouse position, with offset
10309 DY added (default is -10).
10311 A tooltip's maximum size is specified by `x-max-tooltip-size'.
10312 Text larger than the specified size is clipped. */)
10313 (string
, frame
, parms
, timeout
, dx
, dy
)
10314 Lisp_Object string
, frame
, parms
, timeout
, dx
, dy
;
10318 int root_x
, root_y
;
10319 struct buffer
*old_buffer
;
10320 struct text_pos pos
;
10321 int i
, width
, height
;
10322 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
;
10323 int old_windows_or_buffers_changed
= windows_or_buffers_changed
;
10324 int count
= SPECPDL_INDEX ();
10326 specbind (Qinhibit_redisplay
, Qt
);
10328 GCPRO4 (string
, parms
, frame
, timeout
);
10330 CHECK_STRING (string
);
10331 f
= check_x_frame (frame
);
10332 if (NILP (timeout
))
10333 timeout
= make_number (5);
10335 CHECK_NATNUM (timeout
);
10338 dx
= make_number (5);
10343 dy
= make_number (-10);
10347 if (NILP (last_show_tip_args
))
10348 last_show_tip_args
= Fmake_vector (make_number (3), Qnil
);
10350 if (!NILP (tip_frame
))
10352 Lisp_Object last_string
= AREF (last_show_tip_args
, 0);
10353 Lisp_Object last_frame
= AREF (last_show_tip_args
, 1);
10354 Lisp_Object last_parms
= AREF (last_show_tip_args
, 2);
10356 if (EQ (frame
, last_frame
)
10357 && !NILP (Fequal (last_string
, string
))
10358 && !NILP (Fequal (last_parms
, parms
)))
10360 struct frame
*f
= XFRAME (tip_frame
);
10362 /* Only DX and DY have changed. */
10363 if (!NILP (tip_timer
))
10365 Lisp_Object timer
= tip_timer
;
10367 call1 (Qcancel_timer
, timer
);
10371 compute_tip_xy (f
, parms
, dx
, dy
, FRAME_PIXEL_WIDTH (f
),
10372 FRAME_PIXEL_HEIGHT (f
), &root_x
, &root_y
);
10373 XMoveWindow (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
10380 /* Hide a previous tip, if any. */
10383 ASET (last_show_tip_args
, 0, string
);
10384 ASET (last_show_tip_args
, 1, frame
);
10385 ASET (last_show_tip_args
, 2, parms
);
10387 /* Add default values to frame parameters. */
10388 if (NILP (Fassq (Qname
, parms
)))
10389 parms
= Fcons (Fcons (Qname
, build_string ("tooltip")), parms
);
10390 if (NILP (Fassq (Qinternal_border_width
, parms
)))
10391 parms
= Fcons (Fcons (Qinternal_border_width
, make_number (3)), parms
);
10392 if (NILP (Fassq (Qborder_width
, parms
)))
10393 parms
= Fcons (Fcons (Qborder_width
, make_number (1)), parms
);
10394 if (NILP (Fassq (Qborder_color
, parms
)))
10395 parms
= Fcons (Fcons (Qborder_color
, build_string ("lightyellow")), parms
);
10396 if (NILP (Fassq (Qbackground_color
, parms
)))
10397 parms
= Fcons (Fcons (Qbackground_color
, build_string ("lightyellow")),
10400 /* Create a frame for the tooltip, and record it in the global
10401 variable tip_frame. */
10402 frame
= x_create_tip_frame (FRAME_X_DISPLAY_INFO (f
), parms
, string
);
10403 f
= XFRAME (frame
);
10405 /* Set up the frame's root window. */
10406 w
= XWINDOW (FRAME_ROOT_WINDOW (f
));
10407 w
->left_col
= w
->top_line
= make_number (0);
10409 if (CONSP (Vx_max_tooltip_size
)
10410 && INTEGERP (XCAR (Vx_max_tooltip_size
))
10411 && XINT (XCAR (Vx_max_tooltip_size
)) > 0
10412 && INTEGERP (XCDR (Vx_max_tooltip_size
))
10413 && XINT (XCDR (Vx_max_tooltip_size
)) > 0)
10415 w
->total_cols
= XCAR (Vx_max_tooltip_size
);
10416 w
->total_lines
= XCDR (Vx_max_tooltip_size
);
10420 w
->total_cols
= make_number (80);
10421 w
->total_lines
= make_number (40);
10424 FRAME_TOTAL_COLS (f
) = XINT (w
->total_cols
);
10426 w
->pseudo_window_p
= 1;
10428 /* Display the tooltip text in a temporary buffer. */
10429 old_buffer
= current_buffer
;
10430 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f
))->buffer
));
10431 current_buffer
->truncate_lines
= Qnil
;
10432 clear_glyph_matrix (w
->desired_matrix
);
10433 clear_glyph_matrix (w
->current_matrix
);
10434 SET_TEXT_POS (pos
, BEGV
, BEGV_BYTE
);
10435 try_window (FRAME_ROOT_WINDOW (f
), pos
);
10437 /* Compute width and height of the tooltip. */
10438 width
= height
= 0;
10439 for (i
= 0; i
< w
->desired_matrix
->nrows
; ++i
)
10441 struct glyph_row
*row
= &w
->desired_matrix
->rows
[i
];
10442 struct glyph
*last
;
10445 /* Stop at the first empty row at the end. */
10446 if (!row
->enabled_p
|| !row
->displays_text_p
)
10449 /* Let the row go over the full width of the frame. */
10450 row
->full_width_p
= 1;
10452 /* There's a glyph at the end of rows that is used to place
10453 the cursor there. Don't include the width of this glyph. */
10454 if (row
->used
[TEXT_AREA
])
10456 last
= &row
->glyphs
[TEXT_AREA
][row
->used
[TEXT_AREA
] - 1];
10457 row_width
= row
->pixel_width
- last
->pixel_width
;
10460 row_width
= row
->pixel_width
;
10462 height
+= row
->height
;
10463 width
= max (width
, row_width
);
10466 /* Add the frame's internal border to the width and height the X
10467 window should have. */
10468 height
+= 2 * FRAME_INTERNAL_BORDER_WIDTH (f
);
10469 width
+= 2 * FRAME_INTERNAL_BORDER_WIDTH (f
);
10471 /* Move the tooltip window where the mouse pointer is. Resize and
10473 compute_tip_xy (f
, parms
, dx
, dy
, width
, height
, &root_x
, &root_y
);
10476 XMoveResizeWindow (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
),
10477 root_x
, root_y
, width
, height
);
10478 XMapRaised (FRAME_X_DISPLAY (f
), FRAME_X_WINDOW (f
));
10481 /* Draw into the window. */
10482 w
->must_be_updated_p
= 1;
10483 update_single_window (w
, 1);
10485 /* Restore original current buffer. */
10486 set_buffer_internal_1 (old_buffer
);
10487 windows_or_buffers_changed
= old_windows_or_buffers_changed
;
10490 /* Let the tip disappear after timeout seconds. */
10491 tip_timer
= call3 (intern ("run-at-time"), timeout
, Qnil
,
10492 intern ("x-hide-tip"));
10495 return unbind_to (count
, Qnil
);
10499 DEFUN ("x-hide-tip", Fx_hide_tip
, Sx_hide_tip
, 0, 0, 0,
10500 doc
: /* Hide the current tooltip window, if there is any.
10501 Value is t if tooltip was open, nil otherwise. */)
10505 Lisp_Object deleted
, frame
, timer
;
10506 struct gcpro gcpro1
, gcpro2
;
10508 /* Return quickly if nothing to do. */
10509 if (NILP (tip_timer
) && NILP (tip_frame
))
10514 GCPRO2 (frame
, timer
);
10515 tip_frame
= tip_timer
= deleted
= Qnil
;
10517 count
= SPECPDL_INDEX ();
10518 specbind (Qinhibit_redisplay
, Qt
);
10519 specbind (Qinhibit_quit
, Qt
);
10522 call1 (Qcancel_timer
, timer
);
10524 if (FRAMEP (frame
))
10526 Fdelete_frame (frame
, Qnil
);
10530 /* Bloodcurdling hack alert: The Lucid menu bar widget's
10531 redisplay procedure is not called when a tip frame over menu
10532 items is unmapped. Redisplay the menu manually... */
10534 struct frame
*f
= SELECTED_FRAME ();
10535 Widget w
= f
->output_data
.x
->menubar_widget
;
10536 extern void xlwmenu_redisplay
P_ ((Widget
));
10538 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f
)->screen
)
10542 xlwmenu_redisplay (w
);
10546 #endif /* USE_LUCID */
10550 return unbind_to (count
, deleted
);
10555 /***********************************************************************
10556 File selection dialog
10557 ***********************************************************************/
10561 /* Callback for "OK" and "Cancel" on file selection dialog. */
10564 file_dialog_cb (widget
, client_data
, call_data
)
10566 XtPointer call_data
, client_data
;
10568 int *result
= (int *) client_data
;
10569 XmAnyCallbackStruct
*cb
= (XmAnyCallbackStruct
*) call_data
;
10570 *result
= cb
->reason
;
10574 /* Callback for unmapping a file selection dialog. This is used to
10575 capture the case where a dialog is closed via a window manager's
10576 closer button, for example. Using a XmNdestroyCallback didn't work
10580 file_dialog_unmap_cb (widget
, client_data
, call_data
)
10582 XtPointer call_data
, client_data
;
10584 int *result
= (int *) client_data
;
10585 *result
= XmCR_CANCEL
;
10589 DEFUN ("x-file-dialog", Fx_file_dialog
, Sx_file_dialog
, 2, 4, 0,
10590 doc
: /* Read file name, prompting with PROMPT in directory DIR.
10591 Use a file selection dialog.
10592 Select DEFAULT-FILENAME in the dialog's file selection box, if
10593 specified. Don't let the user enter a file name in the file
10594 selection dialog's entry field, if MUSTMATCH is non-nil. */)
10595 (prompt
, dir
, default_filename
, mustmatch
)
10596 Lisp_Object prompt
, dir
, default_filename
, mustmatch
;
10599 struct frame
*f
= SELECTED_FRAME ();
10600 Lisp_Object file
= Qnil
;
10601 Widget dialog
, text
, list
, help
;
10604 extern XtAppContext Xt_app_con
;
10605 XmString dir_xmstring
, pattern_xmstring
;
10606 int count
= SPECPDL_INDEX ();
10607 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
10609 GCPRO5 (prompt
, dir
, default_filename
, mustmatch
, file
);
10610 CHECK_STRING (prompt
);
10611 CHECK_STRING (dir
);
10613 /* Prevent redisplay. */
10614 specbind (Qinhibit_redisplay
, Qt
);
10618 /* Create the dialog with PROMPT as title, using DIR as initial
10619 directory and using "*" as pattern. */
10620 dir
= Fexpand_file_name (dir
, Qnil
);
10621 dir_xmstring
= XmStringCreateLocalized (SDATA (dir
));
10622 pattern_xmstring
= XmStringCreateLocalized ("*");
10624 XtSetArg (al
[ac
], XmNtitle
, SDATA (prompt
)); ++ac
;
10625 XtSetArg (al
[ac
], XmNdirectory
, dir_xmstring
); ++ac
;
10626 XtSetArg (al
[ac
], XmNpattern
, pattern_xmstring
); ++ac
;
10627 XtSetArg (al
[ac
], XmNresizePolicy
, XmRESIZE_GROW
); ++ac
;
10628 XtSetArg (al
[ac
], XmNdialogStyle
, XmDIALOG_APPLICATION_MODAL
); ++ac
;
10629 dialog
= XmCreateFileSelectionDialog (f
->output_data
.x
->widget
,
10631 XmStringFree (dir_xmstring
);
10632 XmStringFree (pattern_xmstring
);
10634 /* Add callbacks for OK and Cancel. */
10635 XtAddCallback (dialog
, XmNokCallback
, file_dialog_cb
,
10636 (XtPointer
) &result
);
10637 XtAddCallback (dialog
, XmNcancelCallback
, file_dialog_cb
,
10638 (XtPointer
) &result
);
10639 XtAddCallback (dialog
, XmNunmapCallback
, file_dialog_unmap_cb
,
10640 (XtPointer
) &result
);
10642 /* Disable the help button since we can't display help. */
10643 help
= XmFileSelectionBoxGetChild (dialog
, XmDIALOG_HELP_BUTTON
);
10644 XtSetSensitive (help
, False
);
10646 /* Mark OK button as default. */
10647 XtVaSetValues (XmFileSelectionBoxGetChild (dialog
, XmDIALOG_OK_BUTTON
),
10648 XmNshowAsDefault
, True
, NULL
);
10650 /* If MUSTMATCH is non-nil, disable the file entry field of the
10651 dialog, so that the user must select a file from the files list
10652 box. We can't remove it because we wouldn't have a way to get at
10653 the result file name, then. */
10654 text
= XmFileSelectionBoxGetChild (dialog
, XmDIALOG_TEXT
);
10655 if (!NILP (mustmatch
))
10658 label
= XmFileSelectionBoxGetChild (dialog
, XmDIALOG_SELECTION_LABEL
);
10659 XtSetSensitive (text
, False
);
10660 XtSetSensitive (label
, False
);
10663 /* Manage the dialog, so that list boxes get filled. */
10664 XtManageChild (dialog
);
10666 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
10667 must include the path for this to work. */
10668 list
= XmFileSelectionBoxGetChild (dialog
, XmDIALOG_LIST
);
10669 if (STRINGP (default_filename
))
10671 XmString default_xmstring
;
10675 = XmStringCreateLocalized (SDATA (default_filename
));
10677 if (!XmListItemExists (list
, default_xmstring
))
10679 /* Add a new item if DEFAULT_FILENAME is not in the list. */
10680 XmListAddItem (list
, default_xmstring
, 0);
10684 item_pos
= XmListItemPos (list
, default_xmstring
);
10685 XmStringFree (default_xmstring
);
10687 /* Select the item and scroll it into view. */
10688 XmListSelectPos (list
, item_pos
, True
);
10689 XmListSetPos (list
, item_pos
);
10692 /* Process events until the user presses Cancel or OK. */
10694 while (result
== 0)
10697 XtAppNextEvent (Xt_app_con
, &event
);
10698 (void) x_dispatch_event (&event
, FRAME_X_DISPLAY (f
) );
10701 /* Get the result. */
10702 if (result
== XmCR_OK
)
10707 XtVaGetValues (dialog
, XmNtextString
, &text
, NULL
);
10708 XmStringGetLtoR (text
, XmFONTLIST_DEFAULT_TAG
, &data
);
10709 XmStringFree (text
);
10710 file
= build_string (data
);
10717 XtUnmanageChild (dialog
);
10718 XtDestroyWidget (dialog
);
10722 /* Make "Cancel" equivalent to C-g. */
10724 Fsignal (Qquit
, Qnil
);
10726 return unbind_to (count
, file
);
10729 #endif /* USE_MOTIF */
10733 DEFUN ("x-file-dialog", Fx_file_dialog
, Sx_file_dialog
, 2, 4, 0,
10734 "Read file name, prompting with PROMPT in directory DIR.\n\
10735 Use a file selection dialog.\n\
10736 Select DEFAULT-FILENAME in the dialog's file selection box, if\n\
10737 specified. Don't let the user enter a file name in the file\n\
10738 selection dialog's entry field, if MUSTMATCH is non-nil.")
10739 (prompt
, dir
, default_filename
, mustmatch
)
10740 Lisp_Object prompt
, dir
, default_filename
, mustmatch
;
10742 FRAME_PTR f
= SELECTED_FRAME ();
10744 Lisp_Object file
= Qnil
;
10745 int count
= specpdl_ptr
- specpdl
;
10746 struct gcpro gcpro1
, gcpro2
, gcpro3
, gcpro4
, gcpro5
;
10750 GCPRO5 (prompt
, dir
, default_filename
, mustmatch
, file
);
10751 CHECK_STRING (prompt
);
10752 CHECK_STRING (dir
);
10754 /* Prevent redisplay. */
10755 specbind (Qinhibit_redisplay
, Qt
);
10759 if (STRINGP (default_filename
))
10760 cdef_file
= SDATA (default_filename
);
10762 cdef_file
= SDATA (dir
);
10764 fn
= xg_get_file_name (f
, SDATA (prompt
), cdef_file
, ! NILP (mustmatch
));
10768 file
= build_string (fn
);
10775 /* Make "Cancel" equivalent to C-g. */
10777 Fsignal (Qquit
, Qnil
);
10779 return unbind_to (count
, file
);
10782 #endif /* USE_GTK */
10785 /***********************************************************************
10787 ***********************************************************************/
10789 #ifdef HAVE_XKBGETKEYBOARD
10790 #include <X11/XKBlib.h>
10791 #include <X11/keysym.h>
10794 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p
,
10795 Sx_backspace_delete_keys_p
, 0, 1, 0,
10796 doc
: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
10797 FRAME nil means use the selected frame.
10798 Value is t if we know that both keys are present, and are mapped to the
10799 usual X keysyms. */)
10803 #ifdef HAVE_XKBGETKEYBOARD
10805 struct frame
*f
= check_x_frame (frame
);
10806 Display
*dpy
= FRAME_X_DISPLAY (f
);
10807 Lisp_Object have_keys
;
10808 int major
, minor
, op
, event
, error
;
10812 /* Check library version in case we're dynamically linked. */
10813 major
= XkbMajorVersion
;
10814 minor
= XkbMinorVersion
;
10815 if (!XkbLibraryVersion (&major
, &minor
))
10821 /* Check that the server supports XKB. */
10822 major
= XkbMajorVersion
;
10823 minor
= XkbMinorVersion
;
10824 if (!XkbQueryExtension (dpy
, &op
, &event
, &error
, &major
, &minor
))
10831 kb
= XkbGetMap (dpy
, XkbAllMapComponentsMask
, XkbUseCoreKbd
);
10834 int delete_keycode
= 0, backspace_keycode
= 0, i
;
10836 if (XkbGetNames (dpy
, XkbAllNamesMask
, kb
) == Success
)
10838 for (i
= kb
->min_key_code
;
10839 (i
< kb
->max_key_code
10840 && (delete_keycode
== 0 || backspace_keycode
== 0));
10843 /* The XKB symbolic key names can be seen most easily in
10844 the PS file generated by `xkbprint -label name
10846 if (bcmp ("DELE", kb
->names
->keys
[i
].name
, 4) == 0)
10847 delete_keycode
= i
;
10848 else if (bcmp ("BKSP", kb
->names
->keys
[i
].name
, 4) == 0)
10849 backspace_keycode
= i
;
10852 XkbFreeNames (kb
, 0, True
);
10855 XkbFreeClientMap (kb
, 0, True
);
10858 && backspace_keycode
10859 && XKeysymToKeycode (dpy
, XK_Delete
) == delete_keycode
10860 && XKeysymToKeycode (dpy
, XK_BackSpace
) == backspace_keycode
)
10865 #else /* not HAVE_XKBGETKEYBOARD */
10867 #endif /* not HAVE_XKBGETKEYBOARD */
10872 /***********************************************************************
10874 ***********************************************************************/
10876 /* Keep this list in the same order as frame_parms in frame.c.
10877 Use 0 for unsupported frame parameters. */
10879 frame_parm_handler x_frame_parm_handlers
[] =
10883 x_set_background_color
,
10884 x_set_border_color
,
10885 x_set_border_width
,
10886 x_set_cursor_color
,
10889 x_set_foreground_color
,
10892 x_set_internal_border_width
,
10893 x_set_menu_bar_lines
,
10895 x_explicitly_set_name
,
10896 x_set_scroll_bar_width
,
10898 x_set_unsplittable
,
10899 x_set_vertical_scroll_bars
,
10901 x_set_tool_bar_lines
,
10902 x_set_scroll_bar_foreground
,
10903 x_set_scroll_bar_background
,
10904 x_set_screen_gamma
,
10905 x_set_line_spacing
,
10906 x_set_fringe_width
,
10907 x_set_fringe_width
,
10915 /* This is zero if not using X windows. */
10918 /* The section below is built by the lisp expression at the top of the file,
10919 just above where these variables are declared. */
10920 /*&&& init symbols here &&&*/
10921 Qnone
= intern ("none");
10922 staticpro (&Qnone
);
10923 Qsuppress_icon
= intern ("suppress-icon");
10924 staticpro (&Qsuppress_icon
);
10925 Qundefined_color
= intern ("undefined-color");
10926 staticpro (&Qundefined_color
);
10927 Qcenter
= intern ("center");
10928 staticpro (&Qcenter
);
10929 Qcompound_text
= intern ("compound-text");
10930 staticpro (&Qcompound_text
);
10931 Qcancel_timer
= intern ("cancel-timer");
10932 staticpro (&Qcancel_timer
);
10933 /* This is the end of symbol initialization. */
10935 /* Text property `display' should be nonsticky by default. */
10936 Vtext_property_default_nonsticky
10937 = Fcons (Fcons (Qdisplay
, Qt
), Vtext_property_default_nonsticky
);
10940 Qlaplace
= intern ("laplace");
10941 staticpro (&Qlaplace
);
10942 Qemboss
= intern ("emboss");
10943 staticpro (&Qemboss
);
10944 Qedge_detection
= intern ("edge-detection");
10945 staticpro (&Qedge_detection
);
10946 Qheuristic
= intern ("heuristic");
10947 staticpro (&Qheuristic
);
10948 QCmatrix
= intern (":matrix");
10949 staticpro (&QCmatrix
);
10950 QCcolor_adjustment
= intern (":color-adjustment");
10951 staticpro (&QCcolor_adjustment
);
10952 QCmask
= intern (":mask");
10953 staticpro (&QCmask
);
10955 Fput (Qundefined_color
, Qerror_conditions
,
10956 Fcons (Qundefined_color
, Fcons (Qerror
, Qnil
)));
10957 Fput (Qundefined_color
, Qerror_message
,
10958 build_string ("Undefined color"));
10960 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images
,
10961 doc
: /* Non-nil means always draw a cross over disabled images.
10962 Disabled images are those having an `:conversion disabled' property.
10963 A cross is always drawn on black & white displays. */);
10964 cross_disabled_images
= 0;
10966 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path
,
10967 doc
: /* List of directories to search for window system bitmap files. */);
10968 Vx_bitmap_file_path
= decode_env_path ((char *) 0, PATH_BITMAPS
);
10970 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape
,
10971 doc
: /* The shape of the pointer when over text.
10972 Changing the value does not affect existing frames
10973 unless you set the mouse color. */);
10974 Vx_pointer_shape
= Qnil
;
10976 #if 0 /* This doesn't really do anything. */
10977 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape
,
10978 doc
: /* The shape of the pointer when not over text.
10979 This variable takes effect when you create a new frame
10980 or when you set the mouse color. */);
10982 Vx_nontext_pointer_shape
= Qnil
;
10984 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape
,
10985 doc
: /* The shape of the pointer when Emacs is busy.
10986 This variable takes effect when you create a new frame
10987 or when you set the mouse color. */);
10988 Vx_hourglass_pointer_shape
= Qnil
;
10990 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p
,
10991 doc
: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
10992 display_hourglass_p
= 1;
10994 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay
,
10995 doc
: /* *Seconds to wait before displaying an hourglass pointer.
10996 Value must be an integer or float. */);
10997 Vhourglass_delay
= make_number (DEFAULT_HOURGLASS_DELAY
);
10999 #if 0 /* This doesn't really do anything. */
11000 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape
,
11001 doc
: /* The shape of the pointer when over the mode line.
11002 This variable takes effect when you create a new frame
11003 or when you set the mouse color. */);
11005 Vx_mode_pointer_shape
= Qnil
;
11007 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
11008 &Vx_sensitive_text_pointer_shape
,
11009 doc
: /* The shape of the pointer when over mouse-sensitive text.
11010 This variable takes effect when you create a new frame
11011 or when you set the mouse color. */);
11012 Vx_sensitive_text_pointer_shape
= Qnil
;
11014 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
11015 &Vx_window_horizontal_drag_shape
,
11016 doc
: /* Pointer shape to use for indicating a window can be dragged horizontally.
11017 This variable takes effect when you create a new frame
11018 or when you set the mouse color. */);
11019 Vx_window_horizontal_drag_shape
= Qnil
;
11021 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel
,
11022 doc
: /* A string indicating the foreground color of the cursor box. */);
11023 Vx_cursor_fore_pixel
= Qnil
;
11025 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size
,
11026 doc
: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
11027 Text larger than this is clipped. */);
11028 Vx_max_tooltip_size
= Fcons (make_number (80), make_number (40));
11030 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager
,
11031 doc
: /* Non-nil if no X window manager is in use.
11032 Emacs doesn't try to figure this out; this is always nil
11033 unless you set it to something else. */);
11034 /* We don't have any way to find this out, so set it to nil
11035 and maybe the user would like to set it to t. */
11036 Vx_no_window_manager
= Qnil
;
11038 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
11039 &Vx_pixel_size_width_font_regexp
,
11040 doc
: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
11042 Since Emacs gets width of a font matching with this regexp from
11043 PIXEL_SIZE field of the name, font finding mechanism gets faster for
11044 such a font. This is especially effective for such large fonts as
11045 Chinese, Japanese, and Korean. */);
11046 Vx_pixel_size_width_font_regexp
= Qnil
;
11048 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay
,
11049 doc
: /* Time after which cached images are removed from the cache.
11050 When an image has not been displayed this many seconds, remove it
11051 from the image cache. Value must be an integer or nil with nil
11052 meaning don't clear the cache. */);
11053 Vimage_cache_eviction_delay
= make_number (30 * 60);
11055 #ifdef USE_X_TOOLKIT
11056 Fprovide (intern ("x-toolkit"), Qnil
);
11058 Fprovide (intern ("motif"), Qnil
);
11060 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string
,
11061 doc
: /* Version info for LessTif/Motif. */);
11062 Vmotif_version_string
= build_string (XmVERSION_STRING
);
11063 #endif /* USE_MOTIF */
11064 #endif /* USE_X_TOOLKIT */
11067 Fprovide (intern ("gtk"), Qnil
);
11069 DEFVAR_LISP ("gtk-version-string", &Vgtk_version_string
,
11070 doc
: /* Version info for GTK+. */);
11072 char gtk_version
[40];
11073 g_snprintf (gtk_version
, sizeof (gtk_version
), "%u.%u.%u",
11074 GTK_MAJOR_VERSION
, GTK_MINOR_VERSION
, GTK_MICRO_VERSION
);
11075 Vgtk_version_string
= build_string (gtk_version
);
11077 #endif /* USE_GTK */
11079 /* X window properties. */
11080 defsubr (&Sx_change_window_property
);
11081 defsubr (&Sx_delete_window_property
);
11082 defsubr (&Sx_window_property
);
11084 defsubr (&Sxw_display_color_p
);
11085 defsubr (&Sx_display_grayscale_p
);
11086 defsubr (&Sxw_color_defined_p
);
11087 defsubr (&Sxw_color_values
);
11088 defsubr (&Sx_server_max_request_size
);
11089 defsubr (&Sx_server_vendor
);
11090 defsubr (&Sx_server_version
);
11091 defsubr (&Sx_display_pixel_width
);
11092 defsubr (&Sx_display_pixel_height
);
11093 defsubr (&Sx_display_mm_width
);
11094 defsubr (&Sx_display_mm_height
);
11095 defsubr (&Sx_display_screens
);
11096 defsubr (&Sx_display_planes
);
11097 defsubr (&Sx_display_color_cells
);
11098 defsubr (&Sx_display_visual_class
);
11099 defsubr (&Sx_display_backing_store
);
11100 defsubr (&Sx_display_save_under
);
11101 defsubr (&Sx_create_frame
);
11102 defsubr (&Sx_open_connection
);
11103 defsubr (&Sx_close_connection
);
11104 defsubr (&Sx_display_list
);
11105 defsubr (&Sx_synchronize
);
11106 defsubr (&Sx_send_client_message
);
11107 defsubr (&Sx_focus_frame
);
11108 defsubr (&Sx_backspace_delete_keys_p
);
11110 /* Setting callback functions for fontset handler. */
11111 get_font_info_func
= x_get_font_info
;
11113 #if 0 /* This function pointer doesn't seem to be used anywhere.
11114 And the pointer assigned has the wrong type, anyway. */
11115 list_fonts_func
= x_list_fonts
;
11118 load_font_func
= x_load_font
;
11119 find_ccl_program_func
= x_find_ccl_program
;
11120 query_font_func
= x_query_font
;
11121 set_frame_fontset_func
= x_set_font
;
11122 check_window_system_func
= check_x
;
11125 Qxbm
= intern ("xbm");
11127 QCconversion
= intern (":conversion");
11128 staticpro (&QCconversion
);
11129 QCheuristic_mask
= intern (":heuristic-mask");
11130 staticpro (&QCheuristic_mask
);
11131 QCcolor_symbols
= intern (":color-symbols");
11132 staticpro (&QCcolor_symbols
);
11133 QCascent
= intern (":ascent");
11134 staticpro (&QCascent
);
11135 QCmargin
= intern (":margin");
11136 staticpro (&QCmargin
);
11137 QCrelief
= intern (":relief");
11138 staticpro (&QCrelief
);
11139 Qpostscript
= intern ("postscript");
11140 staticpro (&Qpostscript
);
11141 QCloader
= intern (":loader");
11142 staticpro (&QCloader
);
11143 QCbounding_box
= intern (":bounding-box");
11144 staticpro (&QCbounding_box
);
11145 QCpt_width
= intern (":pt-width");
11146 staticpro (&QCpt_width
);
11147 QCpt_height
= intern (":pt-height");
11148 staticpro (&QCpt_height
);
11149 QCindex
= intern (":index");
11150 staticpro (&QCindex
);
11151 Qpbm
= intern ("pbm");
11155 Qxpm
= intern ("xpm");
11160 Qjpeg
= intern ("jpeg");
11161 staticpro (&Qjpeg
);
11165 Qtiff
= intern ("tiff");
11166 staticpro (&Qtiff
);
11170 Qgif
= intern ("gif");
11175 Qpng
= intern ("png");
11179 defsubr (&Sclear_image_cache
);
11180 defsubr (&Simage_size
);
11181 defsubr (&Simage_mask_p
);
11183 hourglass_atimer
= NULL
;
11184 hourglass_shown_p
= 0;
11186 defsubr (&Sx_show_tip
);
11187 defsubr (&Sx_hide_tip
);
11189 staticpro (&tip_timer
);
11191 staticpro (&tip_frame
);
11193 last_show_tip_args
= Qnil
;
11194 staticpro (&last_show_tip_args
);
11197 defsubr (&Sx_file_dialog
);
11205 image_types
= NULL
;
11206 Vimage_types
= Qnil
;
11208 define_image_type (&xbm_type
);
11209 define_image_type (&gs_type
);
11210 define_image_type (&pbm_type
);
11213 define_image_type (&xpm_type
);
11217 define_image_type (&jpeg_type
);
11221 define_image_type (&tiff_type
);
11225 define_image_type (&gif_type
);
11229 define_image_type (&png_type
);
11233 #endif /* HAVE_X_WINDOWS */
11235 /* arch-tag: 55040d02-5485-4d58-8b22-95a7a05f3288
11236 (do not change this comment) */