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