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