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