(x_window): Fix typo.
[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
1576f1ad 2123/* Supported XIM styles, ordered by preference. */
5a7df7d7
GM
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;
1576f1ad
DL
2581 if (use_xim)
2582 create_frame_xic (f);
5a7df7d7 2583#endif
64d16748 2584
7556890b
RS
2585 f->output_data.x->wm_hints.input = True;
2586 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 2587 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 2588 &f->output_data.x->wm_hints);
b8228beb 2589
c4ec904f 2590 hack_wm_protocols (f, shell_widget);
9ef48a9d 2591
6c32dd68
PR
2592#ifdef HACK_EDITRES
2593 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2594#endif
2595
9ef48a9d 2596 /* Do a stupid property change to force the server to generate a
333b20bb 2597 PropertyNotify event so that the event_stream server timestamp will
9ef48a9d
RS
2598 be initialized to something relevant to the time we created the window.
2599 */
6c32dd68 2600 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
b9dc4443
RS
2601 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2602 XA_ATOM, 32, PropModeAppend,
9ef48a9d
RS
2603 (unsigned char*) NULL, 0);
2604
5a7df7d7 2605 /* Make all the standard events reach the Emacs frame. */
31ac8d8c 2606 attributes.event_mask = STANDARD_EVENT_SET;
5a7df7d7
GM
2607
2608#ifdef HAVE_X_I18N
2609 if (FRAME_XIC (f))
2610 {
2611 /* XIM server might require some X events. */
2612 unsigned long fevent = NoEventMask;
2613 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2614 attributes.event_mask |= fevent;
2615 }
2616#endif /* HAVE_X_I18N */
177c0ea7 2617
31ac8d8c
FP
2618 attribute_mask = CWEventMask;
2619 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2620 attribute_mask, &attributes);
2621
6c32dd68 2622 XtMapWidget (frame_widget);
9ef48a9d 2623
8fc2766b
RS
2624 /* x_set_name normally ignores requests to set the name if the
2625 requested name is the same as the current name. This is the one
2626 place where that assumption isn't correct; f->name is set, but
2627 the X server hasn't been told. */
2628 {
2629 Lisp_Object name;
2630 int explicit = f->explicit_name;
2631
2632 f->explicit_name = 0;
2633 name = f->name;
2634 f->name = Qnil;
2635 x_set_name (f, name, explicit);
2636 }
2637
b9dc4443 2638 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 2639 f->output_data.x->text_cursor);
8fc2766b
RS
2640
2641 UNBLOCK_INPUT;
2642
495fa05e
GM
2643 /* This is a no-op, except under Motif. Make sure main areas are
2644 set to something reasonable, in case we get an error later. */
2645 lw_set_main_areas (pane_widget, 0, frame_widget);
8fc2766b
RS
2646}
2647
9ef48a9d 2648#else /* not USE_X_TOOLKIT */
488dd4c4
JD
2649#ifdef USE_GTK
2650void
2651x_window (f)
2652 FRAME_PTR f;
2653{
2654 if (! xg_create_frame_widgets (f))
2655 error ("Unable to create window");
1fcfb866
JD
2656
2657#ifdef HAVE_X_I18N
2658 FRAME_XIC (f) = NULL;
1576f1ad
DL
2659if (use_xim)
2660 {
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);
1fcfb866 2668
1576f1ad
DL
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 }
1fcfb866 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
4dacadcc 2724 if (use_xim)
5a7df7d7 2725 {
1576f1ad
DL
2726 create_frame_xic (f);
2727 if (FRAME_XIC (f))
2728 {
2729 /* XIM server might require some X events. */
2730 unsigned long fevent = NoEventMask;
2731 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2732 attributes.event_mask |= fevent;
2733 attribute_mask = CWEventMask;
2734 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2735 attribute_mask, &attributes);
2736 }
5a7df7d7
GM
2737 }
2738#endif /* HAVE_X_I18N */
177c0ea7 2739
d387c960 2740 validate_x_resource_name ();
b7975ee4 2741
d5db4077
KR
2742 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2743 class_hints.res_class = (char *) SDATA (Vx_resource_class);
b9dc4443 2744 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
01f1ba30 2745
00983aba
KH
2746 /* The menubar is part of the ordinary display;
2747 it does not count in addition to the height of the window. */
7556890b 2748 f->output_data.x->menubar_height = 0;
00983aba 2749
179956b9
JB
2750 /* This indicates that we use the "Passive Input" input model.
2751 Unless we do this, we don't get the Focus{In,Out} events that we
2752 need to draw the cursor correctly. Accursed bureaucrats.
b9dc4443 2753 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
179956b9 2754
7556890b
RS
2755 f->output_data.x->wm_hints.input = True;
2756 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 2757 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 2758 &f->output_data.x->wm_hints);
6d078211 2759 f->output_data.x->wm_hints.icon_pixmap = None;
179956b9 2760
032e4ebe
RS
2761 /* Request "save yourself" and "delete window" commands from wm. */
2762 {
2763 Atom protocols[2];
b9dc4443
RS
2764 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2765 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2766 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
032e4ebe 2767 }
9ef48a9d 2768
e373f201
JB
2769 /* x_set_name normally ignores requests to set the name if the
2770 requested name is the same as the current name. This is the one
2771 place where that assumption isn't correct; f->name is set, but
2772 the X server hasn't been told. */
2773 {
98381190 2774 Lisp_Object name;
cf177271 2775 int explicit = f->explicit_name;
e373f201 2776
cf177271 2777 f->explicit_name = 0;
98381190
KH
2778 name = f->name;
2779 f->name = Qnil;
cf177271 2780 x_set_name (f, name, explicit);
e373f201
JB
2781 }
2782
b9dc4443 2783 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 2784 f->output_data.x->text_cursor);
9ef48a9d 2785
01f1ba30
JB
2786 UNBLOCK_INPUT;
2787
fe24a618 2788 if (FRAME_X_WINDOW (f) == 0)
9ef48a9d 2789 error ("Unable to create window");
01f1ba30
JB
2790}
2791
488dd4c4 2792#endif /* not USE_GTK */
8fc2766b
RS
2793#endif /* not USE_X_TOOLKIT */
2794
01f1ba30
JB
2795/* Handle the icon stuff for this window. Perhaps later we might
2796 want an x_set_icon_position which can be called interactively as
b9dc4443 2797 well. */
01f1ba30
JB
2798
2799static void
f676886a
JB
2800x_icon (f, parms)
2801 struct frame *f;
01f1ba30
JB
2802 Lisp_Object parms;
2803{
f9942c9e 2804 Lisp_Object icon_x, icon_y;
abb4b7ec 2805 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
01f1ba30
JB
2806
2807 /* Set the position of the icon. Note that twm groups all
b9dc4443 2808 icons in an icon window. */
7c0d3ed8
KS
2809 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2810 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
f9942c9e 2811 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
01f1ba30 2812 {
b7826503
PJ
2813 CHECK_NUMBER (icon_x);
2814 CHECK_NUMBER (icon_y);
01f1ba30 2815 }
f9942c9e 2816 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
01f1ba30 2817 error ("Both left and top icon corners of icon must be specified");
01f1ba30 2818
f9942c9e
JB
2819 BLOCK_INPUT;
2820
fe24a618
JB
2821 if (! EQ (icon_x, Qunbound))
2822 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
f9942c9e 2823
01f1ba30 2824 /* Start up iconic or window? */
49795535 2825 x_wm_set_window_state
333b20bb
GM
2826 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2827 Qicon)
49795535
JB
2828 ? IconicState
2829 : NormalState));
01f1ba30 2830
d5db4077 2831 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
f468da95 2832 ? f->icon_name
d5db4077 2833 : f->name)));
80534dd6 2834
01f1ba30
JB
2835 UNBLOCK_INPUT;
2836}
2837
b243755a 2838/* Make the GCs needed for this window, setting the
01f1ba30
JB
2839 background, border and mouse colors; also create the
2840 mouse cursor and the gray border tile. */
2841
f945b920
JB
2842static char cursor_bits[] =
2843 {
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 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2848 };
2849
01f1ba30 2850static void
f676886a
JB
2851x_make_gc (f)
2852 struct frame *f;
01f1ba30
JB
2853{
2854 XGCValues gc_values;
01f1ba30 2855
6afb1d07
JB
2856 BLOCK_INPUT;
2857
b243755a 2858 /* Create the GCs of this frame.
9ef48a9d 2859 Note that many default values are used. */
01f1ba30
JB
2860
2861 /* Normal video */
7556890b
RS
2862 gc_values.font = f->output_data.x->font->fid;
2863 gc_values.foreground = f->output_data.x->foreground_pixel;
2864 gc_values.background = f->output_data.x->background_pixel;
9ef48a9d 2865 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
959e647d
GM
2866 f->output_data.x->normal_gc
2867 = XCreateGC (FRAME_X_DISPLAY (f),
2868 FRAME_X_WINDOW (f),
2869 GCLineWidth | GCFont | GCForeground | GCBackground,
2870 &gc_values);
01f1ba30 2871
b9dc4443 2872 /* Reverse video style. */
7556890b
RS
2873 gc_values.foreground = f->output_data.x->background_pixel;
2874 gc_values.background = f->output_data.x->foreground_pixel;
959e647d
GM
2875 f->output_data.x->reverse_gc
2876 = XCreateGC (FRAME_X_DISPLAY (f),
2877 FRAME_X_WINDOW (f),
2878 GCFont | GCForeground | GCBackground | GCLineWidth,
2879 &gc_values);
01f1ba30 2880
9ef48a9d 2881 /* Cursor has cursor-color background, background-color foreground. */
7556890b
RS
2882 gc_values.foreground = f->output_data.x->background_pixel;
2883 gc_values.background = f->output_data.x->cursor_pixel;
01f1ba30
JB
2884 gc_values.fill_style = FillOpaqueStippled;
2885 gc_values.stipple
b9dc4443
RS
2886 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
2887 FRAME_X_DISPLAY_INFO (f)->root_window,
01f1ba30 2888 cursor_bits, 16, 16);
7556890b 2889 f->output_data.x->cursor_gc
b9dc4443 2890 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
01f1ba30 2891 (GCFont | GCForeground | GCBackground
ac1f48a4 2892 | GCFillStyle /* | GCStipple */ | GCLineWidth),
01f1ba30
JB
2893 &gc_values);
2894
333b20bb
GM
2895 /* Reliefs. */
2896 f->output_data.x->white_relief.gc = 0;
2897 f->output_data.x->black_relief.gc = 0;
2898
01f1ba30 2899 /* Create the gray border tile used when the pointer is not in
f676886a 2900 the frame. Since this depends on the frame's pixel values,
9ef48a9d 2901 this must be done on a per-frame basis. */
7556890b 2902 f->output_data.x->border_tile
d043f1a4 2903 = (XCreatePixmapFromBitmapData
177c0ea7 2904 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
d043f1a4 2905 gray_bits, gray_width, gray_height,
7556890b
RS
2906 f->output_data.x->foreground_pixel,
2907 f->output_data.x->background_pixel,
ab452f99 2908 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
6afb1d07
JB
2909
2910 UNBLOCK_INPUT;
01f1ba30 2911}
01f1ba30 2912
959e647d
GM
2913
2914/* Free what was was allocated in x_make_gc. */
2915
2916void
2917x_free_gcs (f)
2918 struct frame *f;
2919{
2920 Display *dpy = FRAME_X_DISPLAY (f);
2921
2922 BLOCK_INPUT;
177c0ea7 2923
959e647d
GM
2924 if (f->output_data.x->normal_gc)
2925 {
2926 XFreeGC (dpy, f->output_data.x->normal_gc);
2927 f->output_data.x->normal_gc = 0;
2928 }
2929
2930 if (f->output_data.x->reverse_gc)
2931 {
2932 XFreeGC (dpy, f->output_data.x->reverse_gc);
2933 f->output_data.x->reverse_gc = 0;
2934 }
177c0ea7 2935
959e647d
GM
2936 if (f->output_data.x->cursor_gc)
2937 {
2938 XFreeGC (dpy, f->output_data.x->cursor_gc);
2939 f->output_data.x->cursor_gc = 0;
2940 }
2941
2942 if (f->output_data.x->border_tile)
2943 {
2944 XFreePixmap (dpy, f->output_data.x->border_tile);
2945 f->output_data.x->border_tile = 0;
2946 }
2947
2948 UNBLOCK_INPUT;
2949}
2950
2951
eaf1eea9
GM
2952/* Handler for signals raised during x_create_frame and
2953 x_create_top_frame. FRAME is the frame which is partially
2954 constructed. */
2955
2956static Lisp_Object
2957unwind_create_frame (frame)
2958 Lisp_Object frame;
2959{
2960 struct frame *f = XFRAME (frame);
2961
2962 /* If frame is ``official'', nothing to do. */
2963 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
2964 {
f1d2ce7f 2965#if GLYPH_DEBUG
eaf1eea9
GM
2966 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2967#endif
177c0ea7 2968
eaf1eea9
GM
2969 x_free_frame_resources (f);
2970
2971 /* Check that reference counts are indeed correct. */
2972 xassert (dpyinfo->reference_count == dpyinfo_refcount);
2973 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
c844a81a 2974 return Qt;
eaf1eea9 2975 }
177c0ea7 2976
eaf1eea9
GM
2977 return Qnil;
2978}
2979
2980
f676886a 2981DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
01f1ba30 2982 1, 1, 0,
7ee72033 2983 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
c061c855
GM
2984Returns an Emacs frame object.
2985ALIST is an alist of frame parameters.
2986If the parameters specify that the frame should not have a minibuffer,
2987and do not specify a specific minibuffer window to use,
2988then `default-minibuffer-frame' must be a frame whose minibuffer can
2989be shared by the new frame.
2990
7ee72033
MB
2991This function is an internal primitive--use `make-frame' instead. */)
2992 (parms)
01f1ba30
JB
2993 Lisp_Object parms;
2994{
f676886a 2995 struct frame *f;
2365c027 2996 Lisp_Object frame, tem;
01f1ba30
JB
2997 Lisp_Object name;
2998 int minibuffer_only = 0;
2999 long window_prompting = 0;
3000 int width, height;
331379bf 3001 int count = SPECPDL_INDEX ();
ecaca587 3002 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
b9dc4443 3003 Lisp_Object display;
333b20bb 3004 struct x_display_info *dpyinfo = NULL;
a59e4f3d 3005 Lisp_Object parent;
e557f19d 3006 struct kboard *kb;
01f1ba30 3007
11ae94fe 3008 check_x ();
01f1ba30 3009
b7975ee4
KH
3010 /* Use this general default value to start with
3011 until we know if this frame has a specified name. */
3012 Vx_resource_name = Vinvocation_name;
3013
333b20bb 3014 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
b9dc4443
RS
3015 if (EQ (display, Qunbound))
3016 display = Qnil;
3017 dpyinfo = check_x_display_info (display);
e557f19d
KH
3018#ifdef MULTI_KBOARD
3019 kb = dpyinfo->kboard;
3020#else
3021 kb = &the_only_kboard;
3022#endif
b9dc4443 3023
333b20bb 3024 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
6a5e54e2 3025 if (!STRINGP (name)
cf177271
JB
3026 && ! EQ (name, Qunbound)
3027 && ! NILP (name))
08a90d6a 3028 error ("Invalid frame name--not a string or nil");
01f1ba30 3029
b7975ee4
KH
3030 if (STRINGP (name))
3031 Vx_resource_name = name;
3032
a59e4f3d 3033 /* See if parent window is specified. */
333b20bb 3034 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
a59e4f3d
RS
3035 if (EQ (parent, Qunbound))
3036 parent = Qnil;
3037 if (! NILP (parent))
b7826503 3038 CHECK_NUMBER (parent);
a59e4f3d 3039
ecaca587
RS
3040 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3041 /* No need to protect DISPLAY because that's not used after passing
3042 it to make_frame_without_minibuffer. */
3043 frame = Qnil;
3044 GCPRO4 (parms, parent, name, frame);
333b20bb
GM
3045 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3046 RES_TYPE_SYMBOL);
f9942c9e 3047 if (EQ (tem, Qnone) || NILP (tem))
2526c290 3048 f = make_frame_without_minibuffer (Qnil, kb, display);
f9942c9e 3049 else if (EQ (tem, Qonly))
01f1ba30 3050 {
f676886a 3051 f = make_minibuffer_frame ();
01f1ba30
JB
3052 minibuffer_only = 1;
3053 }
6a5e54e2 3054 else if (WINDOWP (tem))
2526c290 3055 f = make_frame_without_minibuffer (tem, kb, display);
f9942c9e
JB
3056 else
3057 f = make_frame (1);
01f1ba30 3058
ecaca587
RS
3059 XSETFRAME (frame, f);
3060
a3c87d4e
JB
3061 /* Note that X Windows does support scroll bars. */
3062 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
179956b9 3063
08a90d6a 3064 f->output_method = output_x_window;
7556890b
RS
3065 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
3066 bzero (f->output_data.x, sizeof (struct x_output));
3067 f->output_data.x->icon_bitmap = -1;
0ecca023 3068 f->output_data.x->fontset = -1;
333b20bb
GM
3069 f->output_data.x->scroll_bar_foreground_pixel = -1;
3070 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
3071#ifdef USE_TOOLKIT_SCROLL_BARS
3072 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3073 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3074#endif /* USE_TOOLKIT_SCROLL_BARS */
eaf1eea9 3075 record_unwind_protect (unwind_create_frame, frame);
08a90d6a 3076
f468da95 3077 f->icon_name
333b20bb
GM
3078 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3079 RES_TYPE_STRING);
f468da95
RS
3080 if (! STRINGP (f->icon_name))
3081 f->icon_name = Qnil;
80534dd6 3082
08a90d6a 3083 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
f1d2ce7f 3084#if GLYPH_DEBUG
eaf1eea9
GM
3085 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
3086 dpyinfo_refcount = dpyinfo->reference_count;
3087#endif /* GLYPH_DEBUG */
73410c76 3088#ifdef MULTI_KBOARD
e557f19d 3089 FRAME_KBOARD (f) = kb;
73410c76 3090#endif
08a90d6a 3091
9b2956e2
GM
3092 /* These colors will be set anyway later, but it's important
3093 to get the color reference counts right, so initialize them! */
3094 {
3095 Lisp_Object black;
3096 struct gcpro gcpro1;
cefecbcf
GM
3097
3098 /* Function x_decode_color can signal an error. Make
3099 sure to initialize color slots so that we won't try
3100 to free colors we haven't allocated. */
3101 f->output_data.x->foreground_pixel = -1;
3102 f->output_data.x->background_pixel = -1;
3103 f->output_data.x->cursor_pixel = -1;
3104 f->output_data.x->cursor_foreground_pixel = -1;
3105 f->output_data.x->border_pixel = -1;
3106 f->output_data.x->mouse_pixel = -1;
177c0ea7 3107
9b2956e2
GM
3108 black = build_string ("black");
3109 GCPRO1 (black);
3110 f->output_data.x->foreground_pixel
3111 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3112 f->output_data.x->background_pixel
3113 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3114 f->output_data.x->cursor_pixel
3115 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3116 f->output_data.x->cursor_foreground_pixel
3117 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3118 f->output_data.x->border_pixel
3119 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3120 f->output_data.x->mouse_pixel
3121 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3122 UNGCPRO;
3123 }
3124
a59e4f3d
RS
3125 /* Specify the parent under which to make this X window. */
3126
3127 if (!NILP (parent))
3128 {
8c239ac3 3129 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
7556890b 3130 f->output_data.x->explicit_parent = 1;
a59e4f3d
RS
3131 }
3132 else
3133 {
7556890b
RS
3134 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3135 f->output_data.x->explicit_parent = 0;
a59e4f3d
RS
3136 }
3137
cf177271
JB
3138 /* Set the name; the functions to which we pass f expect the name to
3139 be set. */
3140 if (EQ (name, Qunbound) || NILP (name))
3141 {
08a90d6a 3142 f->name = build_string (dpyinfo->x_id_name);
cf177271
JB
3143 f->explicit_name = 0;
3144 }
3145 else
3146 {
3147 f->name = name;
3148 f->explicit_name = 1;
9ef48a9d
RS
3149 /* use the frame's title when getting resources for this frame. */
3150 specbind (Qx_resource_name, name);
cf177271 3151 }
01f1ba30 3152
01f1ba30
JB
3153 /* Extract the window parameters from the supplied values
3154 that are needed to determine window geometry. */
d387c960
JB
3155 {
3156 Lisp_Object font;
3157
333b20bb 3158 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2ee3abaa 3159
6817eab4 3160 BLOCK_INPUT;
e5e548e3
RS
3161 /* First, try whatever font the caller has specified. */
3162 if (STRINGP (font))
942ea06d 3163 {
49965a29 3164 tem = Fquery_fontset (font, Qnil);
477f8642 3165 if (STRINGP (tem))
d5db4077 3166 font = x_new_fontset (f, SDATA (tem));
942ea06d 3167 else
d5db4077 3168 font = x_new_font (f, SDATA (font));
942ea06d 3169 }
177c0ea7 3170
e5e548e3 3171 /* Try out a font which we hope has bold and italic variations. */
333b20bb
GM
3172 if (!STRINGP (font))
3173 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
e5e548e3 3174 if (!STRINGP (font))
a6ac02af 3175 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
e5e548e3 3176 if (! STRINGP (font))
a6ac02af 3177 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
e5e548e3
RS
3178 if (! STRINGP (font))
3179 /* This was formerly the first thing tried, but it finds too many fonts
3180 and takes too long. */
3181 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
3182 /* If those didn't work, look for something which will at least work. */
3183 if (! STRINGP (font))
a6ac02af 3184 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
6817eab4
JB
3185 UNBLOCK_INPUT;
3186 if (! STRINGP (font))
e5e548e3
RS
3187 font = build_string ("fixed");
3188
477f8642 3189 x_default_parameter (f, parms, Qfont, font,
333b20bb 3190 "font", "Font", RES_TYPE_STRING);
d387c960 3191 }
9ef48a9d 3192
e3881aa0 3193#ifdef USE_LUCID
82c90203
RS
3194 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
3195 whereby it fails to get any font. */
7556890b 3196 xlwmenu_default_font = f->output_data.x->font;
dd254b21 3197#endif
82c90203 3198
cf177271 3199 x_default_parameter (f, parms, Qborder_width, make_number (2),
333b20bb 3200 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
177c0ea7 3201
4e397688 3202 /* This defaults to 1 in order to match xterm. We recognize either
ddf768c3
JB
3203 internalBorderWidth or internalBorder (which is what xterm calls
3204 it). */
3205 if (NILP (Fassq (Qinternal_border_width, parms)))
3206 {
3207 Lisp_Object value;
3208
abb4b7ec 3209 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
333b20bb 3210 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
ddf768c3
JB
3211 if (! EQ (value, Qunbound))
3212 parms = Fcons (Fcons (Qinternal_border_width, value),
3213 parms);
3214 }
dca97592 3215 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
333b20bb
GM
3216 "internalBorderWidth", "internalBorderWidth",
3217 RES_TYPE_NUMBER);
1ab3d87e 3218 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
333b20bb
GM
3219 "verticalScrollBars", "ScrollBars",
3220 RES_TYPE_SYMBOL);
01f1ba30 3221
b9dc4443 3222 /* Also do the stuff which must be set before the window exists. */
cf177271 3223 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
333b20bb 3224 "foreground", "Foreground", RES_TYPE_STRING);
cf177271 3225 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
0b60fc91 3226 "background", "Background", RES_TYPE_STRING);
cf177271 3227 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
333b20bb 3228 "pointerColor", "Foreground", RES_TYPE_STRING);
cf177271 3229 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
333b20bb 3230 "cursorColor", "Foreground", RES_TYPE_STRING);
cf177271 3231 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
333b20bb 3232 "borderColor", "BorderColor", RES_TYPE_STRING);
d62c8769
GM
3233 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3234 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
563b67aa
GM
3235 x_default_parameter (f, parms, Qline_spacing, Qnil,
3236 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
b3ba0aa8
KS
3237 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3238 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3239 x_default_parameter (f, parms, Qright_fringe, Qnil,
3240 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
333b20bb
GM
3241
3242 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3243 "scrollBarForeground",
3244 "ScrollBarForeground", 1);
3245 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3246 "scrollBarBackground",
3247 "ScrollBarBackground", 0);
3248
3249 /* Init faces before x_default_parameter is called for scroll-bar
3250 parameters because that function calls x_set_scroll_bar_width,
3251 which calls change_frame_size, which calls Fset_window_buffer,
3252 which runs hooks, which call Fvertical_motion. At the end, we
3253 end up in init_iterator with a null face cache, which should not
3254 happen. */
3255 init_frame_faces (f);
177c0ea7 3256
c7bcb20d 3257 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
333b20bb 3258 "menuBar", "MenuBar", RES_TYPE_NUMBER);
e33455ca 3259 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
333b20bb 3260 "toolBar", "ToolBar", RES_TYPE_NUMBER);
79873d50 3261 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
333b20bb
GM
3262 "bufferPredicate", "BufferPredicate",
3263 RES_TYPE_SYMBOL);
c2304e02 3264 x_default_parameter (f, parms, Qtitle, Qnil,
333b20bb 3265 "title", "Title", RES_TYPE_STRING);
ea0a1f53
GM
3266 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3267 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
49d41073
EZ
3268 x_default_parameter (f, parms, Qfullscreen, Qnil,
3269 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
90eb1019 3270
7556890b 3271 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
35f59f6b 3272
35f59f6b 3273 /* Compute the size of the X window. */
7c0d3ed8 3274 window_prompting = x_figure_window_size (f, parms, 1);
38d22040 3275
495fa05e
GM
3276 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3277 f->no_split = minibuffer_only || EQ (tem, Qt);
3278
6a1bcd01 3279 /* Create the X widget or window. */
a7f7d550
FP
3280#ifdef USE_X_TOOLKIT
3281 x_window (f, window_prompting, minibuffer_only);
3282#else
f676886a 3283 x_window (f);
a7f7d550 3284#endif
177c0ea7 3285
f676886a
JB
3286 x_icon (f, parms);
3287 x_make_gc (f);
01f1ba30 3288
495fa05e
GM
3289 /* Now consider the frame official. */
3290 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3291 Vframe_list = Fcons (frame, Vframe_list);
3292
f9942c9e
JB
3293 /* We need to do this after creating the X window, so that the
3294 icon-creation functions can say whose icon they're describing. */
cf177271 3295 x_default_parameter (f, parms, Qicon_type, Qnil,
333b20bb 3296 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
f9942c9e 3297
cf177271 3298 x_default_parameter (f, parms, Qauto_raise, Qnil,
333b20bb 3299 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
cf177271 3300 x_default_parameter (f, parms, Qauto_lower, Qnil,
333b20bb 3301 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
dbc4e1c1 3302 x_default_parameter (f, parms, Qcursor_type, Qbox,
333b20bb 3303 "cursorType", "CursorType", RES_TYPE_SYMBOL);
28d7281d
GM
3304 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3305 "scrollBarWidth", "ScrollBarWidth",
3306 RES_TYPE_NUMBER);
f9942c9e 3307
f676886a 3308 /* Dimensions, especially f->height, must be done via change_frame_size.
01f1ba30 3309 Change will not be effected unless different from the current
b9dc4443 3310 f->height. */
f676886a
JB
3311 width = f->width;
3312 height = f->height;
177c0ea7 3313
1ab3d87e
RS
3314 f->height = 0;
3315 SET_FRAME_WIDTH (f, 0);
8938a4fb 3316 change_frame_size (f, height, width, 1, 0, 0);
d043f1a4 3317
4a967a9b
GM
3318 /* Set up faces after all frame parameters are known. This call
3319 also merges in face attributes specified for new frames. If we
3320 don't do this, the `menu' face for instance won't have the right
3321 colors, and the menu bar won't appear in the specified colors for
3322 new frames. */
3323 call1 (Qface_set_after_frame_default, frame);
3324
488dd4c4 3325#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
495fa05e
GM
3326 /* Create the menu bar. */
3327 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3328 {
3329 /* If this signals an error, we haven't set size hints for the
3330 frame and we didn't make it visible. */
3331 initialize_frame_menubar (f);
3332
488dd4c4 3333#ifndef USE_GTK
495fa05e
GM
3334 /* This is a no-op, except under Motif where it arranges the
3335 main window for the widgets on it. */
3336 lw_set_main_areas (f->output_data.x->column_widget,
3337 f->output_data.x->menubar_widget,
3338 f->output_data.x->edit_widget);
488dd4c4 3339#endif /* not USE_GTK */
495fa05e 3340 }
488dd4c4 3341#endif /* USE_X_TOOLKIT || USE_GTK */
495fa05e
GM
3342
3343 /* Tell the server what size and position, etc, we want, and how
3344 badly we want them. This should be done after we have the menu
3345 bar so that its size can be taken into account. */
01f1ba30 3346 BLOCK_INPUT;
7989f084 3347 x_wm_set_size_hint (f, window_prompting, 0);
01f1ba30
JB
3348 UNBLOCK_INPUT;
3349
495fa05e
GM
3350 /* Make the window appear on the frame and enable display, unless
3351 the caller says not to. However, with explicit parent, Emacs
3352 cannot control visibility, so don't try. */
7556890b 3353 if (! f->output_data.x->explicit_parent)
a59e4f3d
RS
3354 {
3355 Lisp_Object visibility;
49795535 3356
333b20bb
GM
3357 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3358 RES_TYPE_SYMBOL);
a59e4f3d
RS
3359 if (EQ (visibility, Qunbound))
3360 visibility = Qt;
49795535 3361
a59e4f3d
RS
3362 if (EQ (visibility, Qicon))
3363 x_iconify_frame (f);
3364 else if (! NILP (visibility))
3365 x_make_frame_visible (f);
3366 else
3367 /* Must have been Qnil. */
3368 ;
3369 }
01f1ba30 3370
495fa05e 3371 UNGCPRO;
9e57df62
GM
3372
3373 /* Make sure windows on this frame appear in calls to next-window
3374 and similar functions. */
3375 Vwindow_list = Qnil;
177c0ea7 3376
9ef48a9d 3377 return unbind_to (count, frame);
01f1ba30
JB
3378}
3379
eaf1eea9 3380
0d17d282
KH
3381/* FRAME is used only to get a handle on the X display. We don't pass the
3382 display info directly because we're called from frame.c, which doesn't
3383 know about that structure. */
e4f79258 3384
87498171 3385Lisp_Object
0d17d282
KH
3386x_get_focus_frame (frame)
3387 struct frame *frame;
87498171 3388{
0d17d282 3389 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
87498171 3390 Lisp_Object xfocus;
0d17d282 3391 if (! dpyinfo->x_focus_frame)
87498171
KH
3392 return Qnil;
3393
0d17d282 3394 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
87498171
KH
3395 return xfocus;
3396}
f0614854 3397
3decc1e7
GM
3398
3399/* In certain situations, when the window manager follows a
3400 click-to-focus policy, there seems to be no way around calling
3401 XSetInputFocus to give another frame the input focus .
3402
3403 In an ideal world, XSetInputFocus should generally be avoided so
3404 that applications don't interfere with the window manager's focus
3405 policy. But I think it's okay to use when it's clearly done
3406 following a user-command. */
3407
3408DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
7ee72033
MB
3409 doc: /* Set the input focus to FRAME.
3410FRAME nil means use the selected frame. */)
3411 (frame)
3decc1e7
GM
3412 Lisp_Object frame;
3413{
3414 struct frame *f = check_x_frame (frame);
3415 Display *dpy = FRAME_X_DISPLAY (f);
3416 int count;
3417
3418 BLOCK_INPUT;
3419 count = x_catch_errors (dpy);
3420 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3421 RevertToParent, CurrentTime);
3422 x_uncatch_errors (dpy, count);
3423 UNBLOCK_INPUT;
177c0ea7 3424
3decc1e7
GM
3425 return Qnil;
3426}
3427
f0614854 3428\f
2d764c78 3429DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
7ee72033
MB
3430 doc: /* Internal function called by `color-defined-p', which see. */)
3431 (color, frame)
b9dc4443 3432 Lisp_Object color, frame;
e12d55b2 3433{
b9dc4443
RS
3434 XColor foo;
3435 FRAME_PTR f = check_x_frame (frame);
e12d55b2 3436
b7826503 3437 CHECK_STRING (color);
b9dc4443 3438
d5db4077 3439 if (x_defined_color (f, SDATA (color), &foo, 0))
e12d55b2
RS
3440 return Qt;
3441 else
3442 return Qnil;
3443}
3444
2d764c78 3445DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
7ee72033
MB
3446 doc: /* Internal function called by `color-values', which see. */)
3447 (color, frame)
b9dc4443 3448 Lisp_Object color, frame;
01f1ba30 3449{
b9dc4443
RS
3450 XColor foo;
3451 FRAME_PTR f = check_x_frame (frame);
3452
b7826503 3453 CHECK_STRING (color);
01f1ba30 3454
d5db4077 3455 if (x_defined_color (f, SDATA (color), &foo, 0))
57c82a63
RS
3456 {
3457 Lisp_Object rgb[3];
3458
3459 rgb[0] = make_number (foo.red);
3460 rgb[1] = make_number (foo.green);
3461 rgb[2] = make_number (foo.blue);
3462 return Flist (3, rgb);
3463 }
01f1ba30
JB
3464 else
3465 return Qnil;
3466}
3467
2d764c78 3468DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
7ee72033
MB
3469 doc: /* Internal function called by `display-color-p', which see. */)
3470 (display)
08a90d6a 3471 Lisp_Object display;
01f1ba30 3472{
08a90d6a 3473 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 3474
b9dc4443 3475 if (dpyinfo->n_planes <= 2)
01f1ba30
JB
3476 return Qnil;
3477
b9dc4443 3478 switch (dpyinfo->visual->class)
01f1ba30
JB
3479 {
3480 case StaticColor:
3481 case PseudoColor:
3482 case TrueColor:
3483 case DirectColor:
3484 return Qt;
3485
3486 default:
3487 return Qnil;
3488 }
3489}
3490
d0c9d219 3491DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
c061c855 3492 0, 1, 0,
7ee72033 3493 doc: /* Return t if the X display supports shades of gray.
c061c855
GM
3494Note that color displays do support shades of gray.
3495The optional argument DISPLAY specifies which display to ask about.
3496DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3497If omitted or nil, that stands for the selected frame's display. */)
3498 (display)
08a90d6a 3499 Lisp_Object display;
d0c9d219 3500{
08a90d6a 3501 struct x_display_info *dpyinfo = check_x_display_info (display);
d0c9d219 3502
ae6b58f9 3503 if (dpyinfo->n_planes <= 1)
b9dc4443
RS
3504 return Qnil;
3505
ae6b58f9
RS
3506 switch (dpyinfo->visual->class)
3507 {
3508 case StaticColor:
3509 case PseudoColor:
3510 case TrueColor:
3511 case DirectColor:
3512 case StaticGray:
3513 case GrayScale:
3514 return Qt;
3515
3516 default:
3517 return Qnil;
3518 }
d0c9d219
RS
3519}
3520
41beb8fc 3521DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
c061c855 3522 0, 1, 0,
7ee72033 3523 doc: /* Returns the width in pixels of the X display DISPLAY.
c061c855
GM
3524The optional argument DISPLAY specifies which display to ask about.
3525DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3526If omitted or nil, that stands for the selected frame's display. */)
3527 (display)
08a90d6a 3528 Lisp_Object display;
41beb8fc 3529{
08a90d6a 3530 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3531
3532 return make_number (dpyinfo->width);
41beb8fc
RS
3533}
3534
3535DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
c061c855 3536 Sx_display_pixel_height, 0, 1, 0,
7ee72033 3537 doc: /* Returns the height in pixels of the X display DISPLAY.
c061c855
GM
3538The optional argument DISPLAY specifies which display to ask about.
3539DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3540If omitted or nil, that stands for the selected frame's display. */)
3541 (display)
08a90d6a 3542 Lisp_Object display;
41beb8fc 3543{
08a90d6a 3544 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3545
3546 return make_number (dpyinfo->height);
41beb8fc
RS
3547}
3548
3549DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
c061c855 3550 0, 1, 0,
7ee72033 3551 doc: /* Returns the number of bitplanes of the X display DISPLAY.
c061c855
GM
3552The optional argument DISPLAY specifies which display to ask about.
3553DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3554If omitted or nil, that stands for the selected frame's display. */)
3555 (display)
08a90d6a 3556 Lisp_Object display;
41beb8fc 3557{
08a90d6a 3558 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3559
3560 return make_number (dpyinfo->n_planes);
41beb8fc
RS
3561}
3562
3563DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
c061c855 3564 0, 1, 0,
7ee72033 3565 doc: /* Returns the number of color cells of the X display DISPLAY.
c061c855
GM
3566The optional argument DISPLAY specifies which display to ask about.
3567DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3568If omitted or nil, that stands for the selected frame's display. */)
3569 (display)
08a90d6a 3570 Lisp_Object display;
41beb8fc 3571{
08a90d6a 3572 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3573
3574 return make_number (DisplayCells (dpyinfo->display,
3575 XScreenNumberOfScreen (dpyinfo->screen)));
41beb8fc
RS
3576}
3577
9d317b2c
RS
3578DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3579 Sx_server_max_request_size,
c061c855 3580 0, 1, 0,
7ee72033 3581 doc: /* Returns the maximum request size of the X server of display DISPLAY.
c061c855
GM
3582The optional argument DISPLAY specifies which display to ask about.
3583DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3584If omitted or nil, that stands for the selected frame's display. */)
3585 (display)
08a90d6a 3586 Lisp_Object display;
9d317b2c 3587{
08a90d6a 3588 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3589
3590 return make_number (MAXREQUEST (dpyinfo->display));
9d317b2c
RS
3591}
3592
41beb8fc 3593DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
7ee72033 3594 doc: /* Returns the vendor ID string of the X server of display DISPLAY.
c061c855
GM
3595The optional argument DISPLAY specifies which display to ask about.
3596DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3597If omitted or nil, that stands for the selected frame's display. */)
3598 (display)
08a90d6a 3599 Lisp_Object display;
41beb8fc 3600{
08a90d6a 3601 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3602 char *vendor = ServerVendor (dpyinfo->display);
3603
41beb8fc
RS
3604 if (! vendor) vendor = "";
3605 return build_string (vendor);
3606}
3607
3608DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
7ee72033 3609 doc: /* Returns the version numbers of the X server of display DISPLAY.
c061c855
GM
3610The value is a list of three integers: the major and minor
3611version numbers of the X Protocol in use, and the vendor-specific release
3612number. See also the function `x-server-vendor'.
3613
3614The optional argument DISPLAY specifies which display to ask about.
3615DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3616If omitted or nil, that stands for the selected frame's display. */)
3617 (display)
08a90d6a 3618 Lisp_Object display;
41beb8fc 3619{
08a90d6a 3620 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443 3621 Display *dpy = dpyinfo->display;
11ae94fe 3622
41beb8fc
RS
3623 return Fcons (make_number (ProtocolVersion (dpy)),
3624 Fcons (make_number (ProtocolRevision (dpy)),
3625 Fcons (make_number (VendorRelease (dpy)), Qnil)));
3626}
3627
3628DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
7ee72033 3629 doc: /* Return the number of screens on the X server of display DISPLAY.
c061c855
GM
3630The optional argument DISPLAY specifies which display to ask about.
3631DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3632If omitted or nil, that stands for the selected frame's display. */)
3633 (display)
08a90d6a 3634 Lisp_Object display;
41beb8fc 3635{
08a90d6a 3636 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3637
3638 return make_number (ScreenCount (dpyinfo->display));
41beb8fc
RS
3639}
3640
3641DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
7ee72033 3642 doc: /* Return the height in millimeters of the X display DISPLAY.
c061c855
GM
3643The optional argument DISPLAY specifies which display to ask about.
3644DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3645If omitted or nil, that stands for the selected frame's display. */)
3646 (display)
08a90d6a 3647 Lisp_Object display;
41beb8fc 3648{
08a90d6a 3649 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3650
3651 return make_number (HeightMMOfScreen (dpyinfo->screen));
41beb8fc
RS
3652}
3653
3654DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
7ee72033 3655 doc: /* Return the width in millimeters of the X display DISPLAY.
c061c855
GM
3656The optional argument DISPLAY specifies which display to ask about.
3657DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3658If omitted or nil, that stands for the selected frame's display. */)
3659 (display)
08a90d6a 3660 Lisp_Object display;
41beb8fc 3661{
08a90d6a 3662 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
3663
3664 return make_number (WidthMMOfScreen (dpyinfo->screen));
41beb8fc
RS
3665}
3666
3667DEFUN ("x-display-backing-store", Fx_display_backing_store,
c061c855 3668 Sx_display_backing_store, 0, 1, 0,
7ee72033 3669 doc: /* Returns an indication of whether X display DISPLAY does backing store.
c061c855
GM
3670The value may be `always', `when-mapped', or `not-useful'.
3671The optional argument DISPLAY specifies which display to ask about.
3672DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3673If omitted or nil, that stands for the selected frame's display. */)
3674 (display)
08a90d6a 3675 Lisp_Object display;
41beb8fc 3676{
08a90d6a 3677 struct x_display_info *dpyinfo = check_x_display_info (display);
8ec8a5ec 3678 Lisp_Object result;
11ae94fe 3679
b9dc4443 3680 switch (DoesBackingStore (dpyinfo->screen))
41beb8fc
RS
3681 {
3682 case Always:
8ec8a5ec
GM
3683 result = intern ("always");
3684 break;
41beb8fc
RS
3685
3686 case WhenMapped:
8ec8a5ec
GM
3687 result = intern ("when-mapped");
3688 break;
41beb8fc
RS
3689
3690 case NotUseful:
8ec8a5ec
GM
3691 result = intern ("not-useful");
3692 break;
41beb8fc
RS
3693
3694 default:
3695 error ("Strange value for BackingStore parameter of screen");
8ec8a5ec 3696 result = Qnil;
41beb8fc 3697 }
8ec8a5ec
GM
3698
3699 return result;
41beb8fc
RS
3700}
3701
3702DEFUN ("x-display-visual-class", Fx_display_visual_class,
c061c855 3703 Sx_display_visual_class, 0, 1, 0,
7ee72033 3704 doc: /* Return the visual class of the X display DISPLAY.
c061c855
GM
3705The value is one of the symbols `static-gray', `gray-scale',
3706`static-color', `pseudo-color', `true-color', or `direct-color'.
3707
3708The optional argument DISPLAY specifies which display to ask about.
3709DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3710If omitted or nil, that stands for the selected frame's display. */)
3711 (display)
08a90d6a 3712 Lisp_Object display;
41beb8fc 3713{
08a90d6a 3714 struct x_display_info *dpyinfo = check_x_display_info (display);
8ec8a5ec 3715 Lisp_Object result;
11ae94fe 3716
b9dc4443 3717 switch (dpyinfo->visual->class)
41beb8fc 3718 {
8ec8a5ec
GM
3719 case StaticGray:
3720 result = intern ("static-gray");
3721 break;
3722 case GrayScale:
3723 result = intern ("gray-scale");
3724 break;
3725 case StaticColor:
3726 result = intern ("static-color");
3727 break;
3728 case PseudoColor:
3729 result = intern ("pseudo-color");
3730 break;
3731 case TrueColor:
3732 result = intern ("true-color");
3733 break;
3734 case DirectColor:
3735 result = intern ("direct-color");
3736 break;
41beb8fc
RS
3737 default:
3738 error ("Display has an unknown visual class");
8ec8a5ec 3739 result = Qnil;
41beb8fc 3740 }
177c0ea7 3741
8ec8a5ec 3742 return result;
41beb8fc
RS
3743}
3744
3745DEFUN ("x-display-save-under", Fx_display_save_under,
c061c855 3746 Sx_display_save_under, 0, 1, 0,
7ee72033 3747 doc: /* Returns t if the X display DISPLAY supports the save-under feature.
c061c855
GM
3748The optional argument DISPLAY specifies which display to ask about.
3749DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
3750If omitted or nil, that stands for the selected frame's display. */)
3751 (display)
08a90d6a 3752 Lisp_Object display;
41beb8fc 3753{
08a90d6a 3754 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 3755
b9dc4443 3756 if (DoesSaveUnders (dpyinfo->screen) == True)
41beb8fc
RS
3757 return Qt;
3758 else
3759 return Qnil;
3760}
3761\f
b9dc4443 3762int
55caf99c
RS
3763x_pixel_width (f)
3764 register struct frame *f;
01f1ba30 3765{
55caf99c 3766 return PIXEL_WIDTH (f);
01f1ba30
JB
3767}
3768
b9dc4443 3769int
55caf99c
RS
3770x_pixel_height (f)
3771 register struct frame *f;
01f1ba30 3772{
55caf99c
RS
3773 return PIXEL_HEIGHT (f);
3774}
3775
b9dc4443 3776int
55caf99c
RS
3777x_char_width (f)
3778 register struct frame *f;
3779{
7556890b 3780 return FONT_WIDTH (f->output_data.x->font);
55caf99c
RS
3781}
3782
b9dc4443 3783int
55caf99c
RS
3784x_char_height (f)
3785 register struct frame *f;
3786{
7556890b 3787 return f->output_data.x->line_height;
01f1ba30 3788}
b9dc4443
RS
3789
3790int
f03f2489
RS
3791x_screen_planes (f)
3792 register struct frame *f;
b9dc4443 3793{
f03f2489 3794 return FRAME_X_DISPLAY_INFO (f)->n_planes;
b9dc4443 3795}
01f1ba30 3796
a6ad00c0
GM
3797
3798\f
3799/************************************************************************
3800 X Displays
3801 ************************************************************************/
3802
01f1ba30 3803\f
a6ad00c0
GM
3804/* Mapping visual names to visuals. */
3805
3806static struct visual_class
3807{
3808 char *name;
3809 int class;
3810}
3811visual_classes[] =
3812{
3813 {"StaticGray", StaticGray},
3814 {"GrayScale", GrayScale},
3815 {"StaticColor", StaticColor},
3816 {"PseudoColor", PseudoColor},
3817 {"TrueColor", TrueColor},
3818 {"DirectColor", DirectColor},
9908a324 3819 {NULL, 0}
a6ad00c0
GM
3820};
3821
3822
404daac1 3823#ifndef HAVE_XSCREENNUMBEROFSCREEN
a6ad00c0
GM
3824
3825/* Value is the screen number of screen SCR. This is a substitute for
3826 the X function with the same name when that doesn't exist. */
3827
404daac1
RS
3828int
3829XScreenNumberOfScreen (scr)
3830 register Screen *scr;
3831{
a6ad00c0
GM
3832 Display *dpy = scr->display;
3833 int i;
3df34fdb 3834
a6ad00c0 3835 for (i = 0; i < dpy->nscreens; ++i)
fbd5ceb2 3836 if (scr == dpy->screens + i)
a6ad00c0 3837 break;
404daac1 3838
a6ad00c0 3839 return i;
404daac1 3840}
a6ad00c0 3841
404daac1
RS
3842#endif /* not HAVE_XSCREENNUMBEROFSCREEN */
3843
01f1ba30 3844
a6ad00c0
GM
3845/* Select the visual that should be used on display DPYINFO. Set
3846 members of DPYINFO appropriately. Called from x_term_init. */
fe24a618 3847
a6ad00c0
GM
3848void
3849select_visual (dpyinfo)
3850 struct x_display_info *dpyinfo;
3851{
3852 Display *dpy = dpyinfo->display;
3853 Screen *screen = dpyinfo->screen;
3854 Lisp_Object value;
fe24a618 3855
a6ad00c0
GM
3856 /* See if a visual is specified. */
3857 value = display_x_get_resource (dpyinfo,
3858 build_string ("visualClass"),
3859 build_string ("VisualClass"),
3860 Qnil, Qnil);
3861 if (STRINGP (value))
3862 {
3863 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
3864 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
3865 depth, a decimal number. NAME is compared with case ignored. */
d5db4077 3866 char *s = (char *) alloca (SBYTES (value) + 1);
a6ad00c0
GM
3867 char *dash;
3868 int i, class = -1;
3869 XVisualInfo vinfo;
3870
d5db4077 3871 strcpy (s, SDATA (value));
a6ad00c0
GM
3872 dash = index (s, '-');
3873 if (dash)
3874 {
3875 dpyinfo->n_planes = atoi (dash + 1);
3876 *dash = '\0';
3877 }
3878 else
3879 /* We won't find a matching visual with depth 0, so that
3880 an error will be printed below. */
3881 dpyinfo->n_planes = 0;
f0614854 3882
a6ad00c0
GM
3883 /* Determine the visual class. */
3884 for (i = 0; visual_classes[i].name; ++i)
3885 if (xstricmp (s, visual_classes[i].name) == 0)
3886 {
3887 class = visual_classes[i].class;
3888 break;
3889 }
01f1ba30 3890
a6ad00c0
GM
3891 /* Look up a matching visual for the specified class. */
3892 if (class == -1
3893 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
3894 dpyinfo->n_planes, class, &vinfo))
d5db4077 3895 fatal ("Invalid visual specification `%s'", SDATA (value));
177c0ea7 3896
a6ad00c0
GM
3897 dpyinfo->visual = vinfo.visual;
3898 }
01f1ba30
JB
3899 else
3900 {
a6ad00c0
GM
3901 int n_visuals;
3902 XVisualInfo *vinfo, vinfo_template;
177c0ea7 3903
a6ad00c0
GM
3904 dpyinfo->visual = DefaultVisualOfScreen (screen);
3905
3906#ifdef HAVE_X11R4
3907 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
3908#else
3909 vinfo_template.visualid = dpyinfo->visual->visualid;
3910#endif
3911 vinfo_template.screen = XScreenNumberOfScreen (screen);
3912 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
3913 &vinfo_template, &n_visuals);
3914 if (n_visuals != 1)
3915 fatal ("Can't get proper X visual info");
3916
94ac875b 3917 dpyinfo->n_planes = vinfo->depth;
a6ad00c0
GM
3918 XFree ((char *) vinfo);
3919 }
01f1ba30 3920}
01f1ba30 3921
a6ad00c0 3922
b9dc4443
RS
3923/* Return the X display structure for the display named NAME.
3924 Open a new connection if necessary. */
3925
3926struct x_display_info *
3927x_display_info_for_name (name)
3928 Lisp_Object name;
3929{
08a90d6a 3930 Lisp_Object names;
b9dc4443
RS
3931 struct x_display_info *dpyinfo;
3932
b7826503 3933 CHECK_STRING (name);
b9dc4443 3934
806048df
RS
3935 if (! EQ (Vwindow_system, intern ("x")))
3936 error ("Not using X Windows");
3937
08a90d6a
RS
3938 for (dpyinfo = x_display_list, names = x_display_name_list;
3939 dpyinfo;
8e713be6 3940 dpyinfo = dpyinfo->next, names = XCDR (names))
b9dc4443
RS
3941 {
3942 Lisp_Object tem;
8e713be6 3943 tem = Fstring_equal (XCAR (XCAR (names)), name);
08a90d6a 3944 if (!NILP (tem))
b9dc4443
RS
3945 return dpyinfo;
3946 }
3947
b7975ee4
KH
3948 /* Use this general default value to start with. */
3949 Vx_resource_name = Vinvocation_name;
3950
b9dc4443
RS
3951 validate_x_resource_name ();
3952
9b207e8e 3953 dpyinfo = x_term_init (name, (char *)0,
d5db4077 3954 (char *) SDATA (Vx_resource_name));
b9dc4443 3955
08a90d6a 3956 if (dpyinfo == 0)
d5db4077 3957 error ("Cannot connect to X server %s", SDATA (name));
08a90d6a 3958
b9dc4443
RS
3959 x_in_use = 1;
3960 XSETFASTINT (Vwindow_system_version, 11);
3961
3962 return dpyinfo;
3963}
3964
a6ad00c0 3965
01f1ba30 3966DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
c061c855 3967 1, 3, 0,
7ee72033 3968 doc: /* Open a connection to an X server.
c061c855
GM
3969DISPLAY is the name of the display to connect to.
3970Optional second arg XRM-STRING is a string of resources in xrdb format.
3971If the optional third arg MUST-SUCCEED is non-nil,
7ee72033
MB
3972terminate Emacs if we can't open the connection. */)
3973 (display, xrm_string, must_succeed)
08a90d6a 3974 Lisp_Object display, xrm_string, must_succeed;
01f1ba30 3975{
01f1ba30 3976 unsigned char *xrm_option;
b9dc4443 3977 struct x_display_info *dpyinfo;
01f1ba30 3978
b7826503 3979 CHECK_STRING (display);
d387c960 3980 if (! NILP (xrm_string))
b7826503 3981 CHECK_STRING (xrm_string);
01f1ba30 3982
806048df
RS
3983 if (! EQ (Vwindow_system, intern ("x")))
3984 error ("Not using X Windows");
3985
d387c960 3986 if (! NILP (xrm_string))
d5db4077 3987 xrm_option = (unsigned char *) SDATA (xrm_string);
01f1ba30
JB
3988 else
3989 xrm_option = (unsigned char *) 0;
d387c960
JB
3990
3991 validate_x_resource_name ();
3992
e1b1bee8 3993 /* This is what opens the connection and sets x_current_display.
b9dc4443
RS
3994 This also initializes many symbols, such as those used for input. */
3995 dpyinfo = x_term_init (display, xrm_option,
d5db4077 3996 (char *) SDATA (Vx_resource_name));
f1c16f36 3997
08a90d6a
RS
3998 if (dpyinfo == 0)
3999 {
4000 if (!NILP (must_succeed))
10ffbc14
GM
4001 fatal ("Cannot connect to X server %s.\n\
4002Check the DISPLAY environment variable or use `-d'.\n\
842a9389
JB
4003Also use the `xauth' program to verify that you have the proper\n\
4004authorization information needed to connect the X server.\n\
bf770132 4005An insecure way to solve the problem may be to use `xhost'.\n",
d5db4077 4006 SDATA (display));
08a90d6a 4007 else
d5db4077 4008 error ("Cannot connect to X server %s", SDATA (display));
08a90d6a
RS
4009 }
4010
b9dc4443 4011 x_in_use = 1;
01f1ba30 4012
b9dc4443 4013 XSETFASTINT (Vwindow_system_version, 11);
01f1ba30
JB
4014 return Qnil;
4015}
4016
08a90d6a
RS
4017DEFUN ("x-close-connection", Fx_close_connection,
4018 Sx_close_connection, 1, 1, 0,
7ee72033 4019 doc: /* Close the connection to DISPLAY's X server.
c061c855 4020For DISPLAY, specify either a frame or a display name (a string).
7ee72033
MB
4021If DISPLAY is nil, that stands for the selected frame's display. */)
4022 (display)
c061c855 4023 Lisp_Object display;
01f1ba30 4024{
08a90d6a 4025 struct x_display_info *dpyinfo = check_x_display_info (display);
08a90d6a 4026 int i;
3457bc6e 4027
08a90d6a
RS
4028 if (dpyinfo->reference_count > 0)
4029 error ("Display still has frames on it");
01f1ba30 4030
08a90d6a
RS
4031 BLOCK_INPUT;
4032 /* Free the fonts in the font table. */
4033 for (i = 0; i < dpyinfo->n_fonts; i++)
333b20bb
GM
4034 if (dpyinfo->font_table[i].name)
4035 {
6ecb43ce
KH
4036 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
4037 xfree (dpyinfo->font_table[i].full_name);
333b20bb 4038 xfree (dpyinfo->font_table[i].name);
333b20bb
GM
4039 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
4040 }
4041
08a90d6a
RS
4042 x_destroy_all_bitmaps (dpyinfo);
4043 XSetCloseDownMode (dpyinfo->display, DestroyAll);
82c90203
RS
4044
4045#ifdef USE_X_TOOLKIT
4046 XtCloseDisplay (dpyinfo->display);
4047#else
08a90d6a 4048 XCloseDisplay (dpyinfo->display);
82c90203 4049#endif
08a90d6a
RS
4050
4051 x_delete_display (dpyinfo);
4052 UNBLOCK_INPUT;
3457bc6e 4053
01f1ba30
JB
4054 return Qnil;
4055}
4056
08a90d6a 4057DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7ee72033
MB
4058 doc: /* Return the list of display names that Emacs has connections to. */)
4059 ()
08a90d6a
RS
4060{
4061 Lisp_Object tail, result;
4062
4063 result = Qnil;
8e713be6
KR
4064 for (tail = x_display_name_list; ! NILP (tail); tail = XCDR (tail))
4065 result = Fcons (XCAR (XCAR (tail)), result);
08a90d6a
RS
4066
4067 return result;
4068}
4069
4070DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7ee72033 4071 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
c061c855
GM
4072If ON is nil, allow buffering of requests.
4073Turning on synchronization prohibits the Xlib routines from buffering
4074requests and seriously degrades performance, but makes debugging much
4075easier.
4076The optional second argument DISPLAY specifies which display to act on.
4077DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4078If DISPLAY is omitted or nil, that stands for the selected frame's display. */)
4079 (on, display)
08a90d6a 4080 Lisp_Object display, on;
01f1ba30 4081{
08a90d6a 4082 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 4083
b9dc4443 4084 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
01f1ba30
JB
4085
4086 return Qnil;
4087}
4088
b9dc4443 4089/* Wait for responses to all X commands issued so far for frame F. */
6b7b1820
RS
4090
4091void
b9dc4443
RS
4092x_sync (f)
4093 FRAME_PTR f;
6b7b1820 4094{
4e87f4d2 4095 BLOCK_INPUT;
b9dc4443 4096 XSync (FRAME_X_DISPLAY (f), False);
4e87f4d2 4097 UNBLOCK_INPUT;
6b7b1820 4098}
333b20bb 4099
01f1ba30 4100\f
333b20bb
GM
4101/***********************************************************************
4102 Image types
4103 ***********************************************************************/
f1c16f36 4104
333b20bb
GM
4105/* Value is the number of elements of vector VECTOR. */
4106
4107#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
4108
4109/* List of supported image types. Use define_image_type to add new
4110 types. Use lookup_image_type to find a type for a given symbol. */
4111
4112static struct image_type *image_types;
4113
333b20bb
GM
4114/* The symbol `image' which is the car of the lists used to represent
4115 images in Lisp. */
4116
4117extern Lisp_Object Qimage;
4118
4119/* The symbol `xbm' which is used as the type symbol for XBM images. */
4120
4121Lisp_Object Qxbm;
4122
4123/* Keywords. */
4124
0fe92f72 4125extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
77814035
KS
4126extern Lisp_Object QCdata, QCtype;
4127Lisp_Object QCascent, QCmargin, QCrelief;
d2dc8167 4128Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
4a8e312c 4129Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
333b20bb
GM
4130
4131/* Other symbols. */
4132
4a8e312c 4133Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
333b20bb
GM
4134
4135/* Time in seconds after which images should be removed from the cache
4136 if not displayed. */
4137
fcf431dc 4138Lisp_Object Vimage_cache_eviction_delay;
333b20bb
GM
4139
4140/* Function prototypes. */
4141
4142static void define_image_type P_ ((struct image_type *type));
4143static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
4144static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
4145static void x_laplace P_ ((struct frame *, struct image *));
4a8e312c 4146static void x_emboss P_ ((struct frame *, struct image *));
45158a91
GM
4147static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
4148 Lisp_Object));
333b20bb
GM
4149
4150
4151/* Define a new image type from TYPE. This adds a copy of TYPE to
4152 image_types and adds the symbol *TYPE->type to Vimage_types. */
4153
4154static void
4155define_image_type (type)
4156 struct image_type *type;
4157{
4158 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
4159 The initialized data segment is read-only. */
4160 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
4161 bcopy (type, p, sizeof *p);
4162 p->next = image_types;
4163 image_types = p;
4164 Vimage_types = Fcons (*p->type, Vimage_types);
4165}
4166
4167
4168/* Look up image type SYMBOL, and return a pointer to its image_type
4169 structure. Value is null if SYMBOL is not a known image type. */
4170
4171static INLINE struct image_type *
4172lookup_image_type (symbol)
4173 Lisp_Object symbol;
4174{
4175 struct image_type *type;
4176
4177 for (type = image_types; type; type = type->next)
4178 if (EQ (symbol, *type->type))
4179 break;
4180
4181 return type;
4182}
4183
4184
4185/* Value is non-zero if OBJECT is a valid Lisp image specification. A
4186 valid image specification is a list whose car is the symbol
4187 `image', and whose rest is a property list. The property list must
4188 contain a value for key `:type'. That value must be the name of a
4189 supported image type. The rest of the property list depends on the
4190 image type. */
4191
4192int
4193valid_image_p (object)
4194 Lisp_Object object;
4195{
4196 int valid_p = 0;
177c0ea7 4197
333b20bb
GM
4198 if (CONSP (object) && EQ (XCAR (object), Qimage))
4199 {
1783ffa2
GM
4200 Lisp_Object tem;
4201
4202 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
4203 if (EQ (XCAR (tem), QCtype))
4204 {
4205 tem = XCDR (tem);
4206 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
4207 {
4208 struct image_type *type;
4209 type = lookup_image_type (XCAR (tem));
4210 if (type)
4211 valid_p = type->valid_p (object);
4212 }
177c0ea7 4213
1783ffa2
GM
4214 break;
4215 }
333b20bb
GM
4216 }
4217
4218 return valid_p;
4219}
4220
4221
7ab1745f
GM
4222/* Log error message with format string FORMAT and argument ARG.
4223 Signaling an error, e.g. when an image cannot be loaded, is not a
4224 good idea because this would interrupt redisplay, and the error
4225 message display would lead to another redisplay. This function
4226 therefore simply displays a message. */
333b20bb
GM
4227
4228static void
4229image_error (format, arg1, arg2)
4230 char *format;
4231 Lisp_Object arg1, arg2;
4232{
7ab1745f 4233 add_to_log (format, arg1, arg2);
333b20bb
GM
4234}
4235
4236
4237\f
4238/***********************************************************************
4239 Image specifications
4240 ***********************************************************************/
4241
4242enum image_value_type
4243{
4244 IMAGE_DONT_CHECK_VALUE_TYPE,
4245 IMAGE_STRING_VALUE,
6f1be3b9 4246 IMAGE_STRING_OR_NIL_VALUE,
333b20bb
GM
4247 IMAGE_SYMBOL_VALUE,
4248 IMAGE_POSITIVE_INTEGER_VALUE,
3ed61e75 4249 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
333b20bb 4250 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
7c7ff7f5 4251 IMAGE_ASCENT_VALUE,
333b20bb
GM
4252 IMAGE_INTEGER_VALUE,
4253 IMAGE_FUNCTION_VALUE,
4254 IMAGE_NUMBER_VALUE,
4255 IMAGE_BOOL_VALUE
4256};
4257
4258/* Structure used when parsing image specifications. */
4259
4260struct image_keyword
4261{
4262 /* Name of keyword. */
4263 char *name;
4264
4265 /* The type of value allowed. */
4266 enum image_value_type type;
4267
4268 /* Non-zero means key must be present. */
4269 int mandatory_p;
4270
4271 /* Used to recognize duplicate keywords in a property list. */
4272 int count;
4273
4274 /* The value that was found. */
4275 Lisp_Object value;
4276};
4277
4278
bfd2209f
GM
4279static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
4280 int, Lisp_Object));
333b20bb
GM
4281static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
4282
4283
4284/* Parse image spec SPEC according to KEYWORDS. A valid image spec
4285 has the format (image KEYWORD VALUE ...). One of the keyword/
4286 value pairs must be `:type TYPE'. KEYWORDS is a vector of
4287 image_keywords structures of size NKEYWORDS describing other
bfd2209f 4288 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
333b20bb
GM
4289
4290static int
bfd2209f 4291parse_image_spec (spec, keywords, nkeywords, type)
333b20bb
GM
4292 Lisp_Object spec;
4293 struct image_keyword *keywords;
4294 int nkeywords;
4295 Lisp_Object type;
333b20bb
GM
4296{
4297 int i;
4298 Lisp_Object plist;
4299
4300 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
4301 return 0;
4302
4303 plist = XCDR (spec);
4304 while (CONSP (plist))
4305 {
4306 Lisp_Object key, value;
4307
4308 /* First element of a pair must be a symbol. */
4309 key = XCAR (plist);
4310 plist = XCDR (plist);
4311 if (!SYMBOLP (key))
4312 return 0;
4313
4314 /* There must follow a value. */
4315 if (!CONSP (plist))
4316 return 0;
4317 value = XCAR (plist);
4318 plist = XCDR (plist);
4319
4320 /* Find key in KEYWORDS. Error if not found. */
4321 for (i = 0; i < nkeywords; ++i)
d5db4077 4322 if (strcmp (keywords[i].name, SDATA (SYMBOL_NAME (key))) == 0)
333b20bb
GM
4323 break;
4324
4325 if (i == nkeywords)
bfd2209f 4326 continue;
333b20bb
GM
4327
4328 /* Record that we recognized the keyword. If a keywords
4329 was found more than once, it's an error. */
4330 keywords[i].value = value;
4331 ++keywords[i].count;
177c0ea7 4332
333b20bb
GM
4333 if (keywords[i].count > 1)
4334 return 0;
4335
4336 /* Check type of value against allowed type. */
4337 switch (keywords[i].type)
4338 {
4339 case IMAGE_STRING_VALUE:
4340 if (!STRINGP (value))
4341 return 0;
4342 break;
4343
6f1be3b9
GM
4344 case IMAGE_STRING_OR_NIL_VALUE:
4345 if (!STRINGP (value) && !NILP (value))
4346 return 0;
4347 break;
4348
333b20bb
GM
4349 case IMAGE_SYMBOL_VALUE:
4350 if (!SYMBOLP (value))
4351 return 0;
4352 break;
4353
4354 case IMAGE_POSITIVE_INTEGER_VALUE:
4355 if (!INTEGERP (value) || XINT (value) <= 0)
4356 return 0;
4357 break;
4358
3ed61e75
GM
4359 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
4360 if (INTEGERP (value) && XINT (value) >= 0)
4361 break;
4362 if (CONSP (value)
4363 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
4364 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
4365 break;
4366 return 0;
4367
7c7ff7f5
GM
4368 case IMAGE_ASCENT_VALUE:
4369 if (SYMBOLP (value) && EQ (value, Qcenter))
4370 break;
4371 else if (INTEGERP (value)
4372 && XINT (value) >= 0
4373 && XINT (value) <= 100)
4374 break;
4375 return 0;
177c0ea7 4376
333b20bb
GM
4377 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
4378 if (!INTEGERP (value) || XINT (value) < 0)
4379 return 0;
4380 break;
4381
4382 case IMAGE_DONT_CHECK_VALUE_TYPE:
4383 break;
4384
4385 case IMAGE_FUNCTION_VALUE:
4386 value = indirect_function (value);
177c0ea7 4387 if (SUBRP (value)
333b20bb
GM
4388 || COMPILEDP (value)
4389 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
4390 break;
4391 return 0;
4392
4393 case IMAGE_NUMBER_VALUE:
4394 if (!INTEGERP (value) && !FLOATP (value))
4395 return 0;
4396 break;
4397
4398 case IMAGE_INTEGER_VALUE:
4399 if (!INTEGERP (value))
4400 return 0;
4401 break;
4402
4403 case IMAGE_BOOL_VALUE:
4404 if (!NILP (value) && !EQ (value, Qt))
4405 return 0;
4406 break;
4407
4408 default:
4409 abort ();
4410 break;
4411 }
4412
4413 if (EQ (key, QCtype) && !EQ (type, value))
4414 return 0;
4415 }
4416
4417 /* Check that all mandatory fields are present. */
4418 for (i = 0; i < nkeywords; ++i)
4419 if (keywords[i].mandatory_p && keywords[i].count == 0)
4420 return 0;
4421
4422 return NILP (plist);
4423}
4424
4425
4426/* Return the value of KEY in image specification SPEC. Value is nil
4427 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
4428 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
4429
4430static Lisp_Object
4431image_spec_value (spec, key, found)
4432 Lisp_Object spec, key;
4433 int *found;
4434{
4435 Lisp_Object tail;
177c0ea7 4436
333b20bb
GM
4437 xassert (valid_image_p (spec));
4438
4439 for (tail = XCDR (spec);
4440 CONSP (tail) && CONSP (XCDR (tail));
4441 tail = XCDR (XCDR (tail)))
4442 {
4443 if (EQ (XCAR (tail), key))
4444 {
4445 if (found)
4446 *found = 1;
4447 return XCAR (XCDR (tail));
4448 }
4449 }
177c0ea7 4450
333b20bb
GM
4451 if (found)
4452 *found = 0;
4453 return Qnil;
4454}
177c0ea7 4455
333b20bb 4456
42677916 4457DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
7ee72033 4458 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
c061c855
GM
4459PIXELS non-nil means return the size in pixels, otherwise return the
4460size in canonical character units.
4461FRAME is the frame on which the image will be displayed. FRAME nil
7ee72033
MB
4462or omitted means use the selected frame. */)
4463 (spec, pixels, frame)
42677916
GM
4464 Lisp_Object spec, pixels, frame;
4465{
4466 Lisp_Object size;
4467
4468 size = Qnil;
4469 if (valid_image_p (spec))
4470 {
4471 struct frame *f = check_x_frame (frame);
83676598 4472 int id = lookup_image (f, spec);
42677916 4473 struct image *img = IMAGE_FROM_ID (f, id);
3ed61e75
GM
4474 int width = img->width + 2 * img->hmargin;
4475 int height = img->height + 2 * img->vmargin;
177c0ea7 4476
42677916
GM
4477 if (NILP (pixels))
4478 size = Fcons (make_float ((double) width / CANON_X_UNIT (f)),
4479 make_float ((double) height / CANON_Y_UNIT (f)));
4480 else
4481 size = Fcons (make_number (width), make_number (height));
4482 }
4483 else
4484 error ("Invalid image specification");
4485
4486 return size;
4487}
4488
333b20bb 4489
b243755a 4490DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
7ee72033 4491 doc: /* Return t if image SPEC has a mask bitmap.
c061c855 4492FRAME is the frame on which the image will be displayed. FRAME nil
7ee72033
MB
4493or omitted means use the selected frame. */)
4494 (spec, frame)
b243755a
GM
4495 Lisp_Object spec, frame;
4496{
4497 Lisp_Object mask;
4498
4499 mask = Qnil;
4500 if (valid_image_p (spec))
4501 {
4502 struct frame *f = check_x_frame (frame);
83676598 4503 int id = lookup_image (f, spec);
b243755a
GM
4504 struct image *img = IMAGE_FROM_ID (f, id);
4505 if (img->mask)
4506 mask = Qt;
4507 }
4508 else
4509 error ("Invalid image specification");
4510
4511 return mask;
4512}
4513
4514
333b20bb
GM
4515\f
4516/***********************************************************************
4517 Image type independent image structures
4518 ***********************************************************************/
4519
4520static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
4521static void free_image P_ ((struct frame *f, struct image *img));
4522
4523
4524/* Allocate and return a new image structure for image specification
4525 SPEC. SPEC has a hash value of HASH. */
4526
4527static struct image *
4528make_image (spec, hash)
4529 Lisp_Object spec;
4530 unsigned hash;
4531{
4532 struct image *img = (struct image *) xmalloc (sizeof *img);
177c0ea7 4533
333b20bb
GM
4534 xassert (valid_image_p (spec));
4535 bzero (img, sizeof *img);
4536 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
4537 xassert (img->type != NULL);
4538 img->spec = spec;
4539 img->data.lisp_val = Qnil;
4540 img->ascent = DEFAULT_IMAGE_ASCENT;
4541 img->hash = hash;
4542 return img;
4543}
4544
4545
4546/* Free image IMG which was used on frame F, including its resources. */
4547
4548static void
4549free_image (f, img)
4550 struct frame *f;
4551 struct image *img;
4552{
4553 if (img)
4554 {
4555 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
4556
4557 /* Remove IMG from the hash table of its cache. */
4558 if (img->prev)
4559 img->prev->next = img->next;
4560 else
4561 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
4562
4563 if (img->next)
4564 img->next->prev = img->prev;
4565
4566 c->images[img->id] = NULL;
4567
4568 /* Free resources, then free IMG. */
4569 img->type->free (f, img);
4570 xfree (img);
4571 }
4572}
4573
4574
4575/* Prepare image IMG for display on frame F. Must be called before
4576 drawing an image. */
4577
4578void
4579prepare_image_for_display (f, img)
4580 struct frame *f;
4581 struct image *img;
4582{
4583 EMACS_TIME t;
4584
4585 /* We're about to display IMG, so set its timestamp to `now'. */
4586 EMACS_GET_TIME (t);
4587 img->timestamp = EMACS_SECS (t);
4588
4589 /* If IMG doesn't have a pixmap yet, load it now, using the image
4590 type dependent loader function. */
dd00328a 4591 if (img->pixmap == None && !img->load_failed_p)
209061be 4592 img->load_failed_p = img->type->load (f, img) == 0;
333b20bb 4593}
177c0ea7 4594
333b20bb 4595
7c7ff7f5
GM
4596/* Value is the number of pixels for the ascent of image IMG when
4597 drawn in face FACE. */
4598
4599int
4600image_ascent (img, face)
4601 struct image *img;
4602 struct face *face;
4603{
3ed61e75 4604 int height = img->height + img->vmargin;
7c7ff7f5
GM
4605 int ascent;
4606
4607 if (img->ascent == CENTERED_IMAGE_ASCENT)
4608 {
4609 if (face->font)
3694cb3f
MB
4610 /* This expression is arranged so that if the image can't be
4611 exactly centered, it will be moved slightly up. This is
4612 because a typical font is `top-heavy' (due to the presence
4613 uppercase letters), so the image placement should err towards
4614 being top-heavy too. It also just generally looks better. */
4615 ascent = (height + face->font->ascent - face->font->descent + 1) / 2;
7c7ff7f5
GM
4616 else
4617 ascent = height / 2;
4618 }
4619 else
4620 ascent = height * img->ascent / 100.0;
4621
4622 return ascent;
4623}
4624
f20a3b7a
MB
4625\f
4626/* Image background colors. */
4627
4628static unsigned long
4629four_corners_best (ximg, width, height)
4630 XImage *ximg;
4631 unsigned long width, height;
4632{
b350c2e5
GM
4633 unsigned long corners[4], best;
4634 int i, best_count;
f20a3b7a 4635
b350c2e5
GM
4636 /* Get the colors at the corners of ximg. */
4637 corners[0] = XGetPixel (ximg, 0, 0);
4638 corners[1] = XGetPixel (ximg, width - 1, 0);
4639 corners[2] = XGetPixel (ximg, width - 1, height - 1);
4640 corners[3] = XGetPixel (ximg, 0, height - 1);
f20a3b7a 4641
b350c2e5
GM
4642 /* Choose the most frequently found color as background. */
4643 for (i = best_count = 0; i < 4; ++i)
4644 {
4645 int j, n;
177c0ea7 4646
b350c2e5
GM
4647 for (j = n = 0; j < 4; ++j)
4648 if (corners[i] == corners[j])
4649 ++n;
f20a3b7a 4650
b350c2e5
GM
4651 if (n > best_count)
4652 best = corners[i], best_count = n;
4653 }
f20a3b7a 4654
b350c2e5 4655 return best;
f20a3b7a
MB
4656}
4657
4658/* Return the `background' field of IMG. If IMG doesn't have one yet,
4659 it is guessed heuristically. If non-zero, XIMG is an existing XImage
4660 object to use for the heuristic. */
4661
4662unsigned long
4663image_background (img, f, ximg)
4664 struct image *img;
4665 struct frame *f;
4666 XImage *ximg;
4667{
4668 if (! img->background_valid)
4669 /* IMG doesn't have a background yet, try to guess a reasonable value. */
4670 {
4671 int free_ximg = !ximg;
4672
4673 if (! ximg)
4674 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
4675 0, 0, img->width, img->height, ~0, ZPixmap);
4676
4677 img->background = four_corners_best (ximg, img->width, img->height);
4678
4679 if (free_ximg)
4680 XDestroyImage (ximg);
4681
4682 img->background_valid = 1;
4683 }
4684
4685 return img->background;
4686}
4687
4688/* Return the `background_transparent' field of IMG. If IMG doesn't
4689 have one yet, it is guessed heuristically. If non-zero, MASK is an
4690 existing XImage object to use for the heuristic. */
4691
4692int
4693image_background_transparent (img, f, mask)
4694 struct image *img;
4695 struct frame *f;
4696 XImage *mask;
4697{
4698 if (! img->background_transparent_valid)
4699 /* IMG doesn't have a background yet, try to guess a reasonable value. */
4700 {
4701 if (img->mask)
4702 {
4703 int free_mask = !mask;
4704
4705 if (! mask)
4706 mask = XGetImage (FRAME_X_DISPLAY (f), img->mask,
4707 0, 0, img->width, img->height, ~0, ZPixmap);
4708
4709 img->background_transparent
4710 = !four_corners_best (mask, img->width, img->height);
4711
4712 if (free_mask)
4713 XDestroyImage (mask);
4714 }
4715 else
4716 img->background_transparent = 0;
4717
4718 img->background_transparent_valid = 1;
4719 }
4720
4721 return img->background_transparent;
4722}
7c7ff7f5 4723
333b20bb
GM
4724\f
4725/***********************************************************************
4726 Helper functions for X image types
4727 ***********************************************************************/
4728
dd00328a
GM
4729static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
4730 int, int));
333b20bb
GM
4731static void x_clear_image P_ ((struct frame *f, struct image *img));
4732static unsigned long x_alloc_image_color P_ ((struct frame *f,
4733 struct image *img,
4734 Lisp_Object color_name,
4735 unsigned long dflt));
4736
dd00328a
GM
4737
4738/* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
4739 free the pixmap if any. MASK_P non-zero means clear the mask
4740 pixmap if any. COLORS_P non-zero means free colors allocated for
4741 the image, if any. */
333b20bb
GM
4742
4743static void
dd00328a 4744x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
333b20bb
GM
4745 struct frame *f;
4746 struct image *img;
dd00328a 4747 int pixmap_p, mask_p, colors_p;
333b20bb 4748{
dd00328a 4749 if (pixmap_p && img->pixmap)
333b20bb 4750 {
333b20bb 4751 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
dd00328a 4752 img->pixmap = None;
f20a3b7a 4753 img->background_valid = 0;
f4779de9
GM
4754 }
4755
dd00328a 4756 if (mask_p && img->mask)
f4779de9
GM
4757 {
4758 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
dd00328a 4759 img->mask = None;
f20a3b7a 4760 img->background_transparent_valid = 0;
333b20bb 4761 }
177c0ea7 4762
dd00328a 4763 if (colors_p && img->ncolors)
333b20bb 4764 {
462d5d40 4765 x_free_colors (f, img->colors, img->ncolors);
333b20bb
GM
4766 xfree (img->colors);
4767 img->colors = NULL;
4768 img->ncolors = 0;
4769 }
dd00328a
GM
4770}
4771
4772/* Free X resources of image IMG which is used on frame F. */
4773
4774static void
4775x_clear_image (f, img)
4776 struct frame *f;
4777 struct image *img;
4778{
4779 BLOCK_INPUT;
4780 x_clear_image_1 (f, img, 1, 1, 1);
f4779de9 4781 UNBLOCK_INPUT;
333b20bb
GM
4782}
4783
4784
4785/* Allocate color COLOR_NAME for image IMG on frame F. If color
4786 cannot be allocated, use DFLT. Add a newly allocated color to
4787 IMG->colors, so that it can be freed again. Value is the pixel
4788 color. */
4789
4790static unsigned long
4791x_alloc_image_color (f, img, color_name, dflt)
4792 struct frame *f;
4793 struct image *img;
4794 Lisp_Object color_name;
4795 unsigned long dflt;
4796{
4797 XColor color;
4798 unsigned long result;
4799
4800 xassert (STRINGP (color_name));
4801
d5db4077 4802 if (x_defined_color (f, SDATA (color_name), &color, 1))
333b20bb
GM
4803 {
4804 /* This isn't called frequently so we get away with simply
4805 reallocating the color vector to the needed size, here. */
4806 ++img->ncolors;
4807 img->colors =
4808 (unsigned long *) xrealloc (img->colors,
4809 img->ncolors * sizeof *img->colors);
4810 img->colors[img->ncolors - 1] = color.pixel;
4811 result = color.pixel;
4812 }
4813 else
4814 result = dflt;
4815
4816 return result;
4817}
4818
4819
4820\f
4821/***********************************************************************
4822 Image Cache
4823 ***********************************************************************/
4824
4825static void cache_image P_ ((struct frame *f, struct image *img));
ad18ffb1 4826static void postprocess_image P_ ((struct frame *, struct image *));
333b20bb
GM
4827
4828
4829/* Return a new, initialized image cache that is allocated from the
4830 heap. Call free_image_cache to free an image cache. */
4831
4832struct image_cache *
4833make_image_cache ()
4834{
4835 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
4836 int size;
177c0ea7 4837
333b20bb
GM
4838 bzero (c, sizeof *c);
4839 c->size = 50;
4840 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
4841 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
4842 c->buckets = (struct image **) xmalloc (size);
4843 bzero (c->buckets, size);
4844 return c;
4845}
4846
4847
4848/* Free image cache of frame F. Be aware that X frames share images
4849 caches. */
4850
4851void
4852free_image_cache (f)
4853 struct frame *f;
4854{
4855 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
4856 if (c)
4857 {
4858 int i;
4859
4860 /* Cache should not be referenced by any frame when freed. */
4861 xassert (c->refcount == 0);
177c0ea7 4862
333b20bb
GM
4863 for (i = 0; i < c->used; ++i)
4864 free_image (f, c->images[i]);
4865 xfree (c->images);
333b20bb 4866 xfree (c->buckets);
e3130015 4867 xfree (c);
333b20bb
GM
4868 FRAME_X_IMAGE_CACHE (f) = NULL;
4869 }
4870}
4871
4872
4873/* Clear image cache of frame F. FORCE_P non-zero means free all
4874 images. FORCE_P zero means clear only images that haven't been
4875 displayed for some time. Should be called from time to time to
4876 reduce the number of loaded images. If image-eviction-seconds is
4877 non-nil, this frees images in the cache which weren't displayed for
4878 at least that many seconds. */
4879
4880void
4881clear_image_cache (f, force_p)
4882 struct frame *f;
4883 int force_p;
4884{
4885 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
4886
83676598 4887 if (c && INTEGERP (Vimage_cache_eviction_delay))
333b20bb
GM
4888 {
4889 EMACS_TIME t;
4890 unsigned long old;
f4779de9 4891 int i, nfreed;
333b20bb
GM
4892
4893 EMACS_GET_TIME (t);
fcf431dc 4894 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
f4779de9
GM
4895
4896 /* Block input so that we won't be interrupted by a SIGIO
4897 while being in an inconsistent state. */
4898 BLOCK_INPUT;
177c0ea7 4899
f4779de9 4900 for (i = nfreed = 0; i < c->used; ++i)
333b20bb
GM
4901 {
4902 struct image *img = c->images[i];
4903 if (img != NULL
f4779de9 4904 && (force_p || img->timestamp < old))
333b20bb
GM
4905 {
4906 free_image (f, img);
f4779de9 4907 ++nfreed;
333b20bb
GM
4908 }
4909 }
4910
4911 /* We may be clearing the image cache because, for example,
4912 Emacs was iconified for a longer period of time. In that
4913 case, current matrices may still contain references to
4914 images freed above. So, clear these matrices. */
f4779de9 4915 if (nfreed)
333b20bb 4916 {
f4779de9 4917 Lisp_Object tail, frame;
177c0ea7 4918
f4779de9
GM
4919 FOR_EACH_FRAME (tail, frame)
4920 {
4921 struct frame *f = XFRAME (frame);
4922 if (FRAME_X_P (f)
4923 && FRAME_X_IMAGE_CACHE (f) == c)
83676598 4924 clear_current_matrices (f);
f4779de9
GM
4925 }
4926
333b20bb
GM
4927 ++windows_or_buffers_changed;
4928 }
f4779de9
GM
4929
4930 UNBLOCK_INPUT;
333b20bb
GM
4931 }
4932}
4933
4934
4935DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
4936 0, 1, 0,
7ee72033 4937 doc: /* Clear the image cache of FRAME.
c061c855 4938FRAME nil or omitted means use the selected frame.
7ee72033
MB
4939FRAME t means clear the image caches of all frames. */)
4940 (frame)
333b20bb
GM
4941 Lisp_Object frame;
4942{
4943 if (EQ (frame, Qt))
4944 {
4945 Lisp_Object tail;
177c0ea7 4946
333b20bb
GM
4947 FOR_EACH_FRAME (tail, frame)
4948 if (FRAME_X_P (XFRAME (frame)))
4949 clear_image_cache (XFRAME (frame), 1);
4950 }
4951 else
4952 clear_image_cache (check_x_frame (frame), 1);
4953
4954 return Qnil;
4955}
4956
4957
ad18ffb1
GM
4958/* Compute masks and transform image IMG on frame F, as specified
4959 by the image's specification, */
4960
4961static void
4962postprocess_image (f, img)
4963 struct frame *f;
4964 struct image *img;
4965{
4966 /* Manipulation of the image's mask. */
4967 if (img->pixmap)
4968 {
4969 Lisp_Object conversion, spec;
4970 Lisp_Object mask;
4971
4972 spec = img->spec;
177c0ea7 4973
ad18ffb1
GM
4974 /* `:heuristic-mask t'
4975 `:mask heuristic'
4976 means build a mask heuristically.
4977 `:heuristic-mask (R G B)'
4978 `:mask (heuristic (R G B))'
4979 means build a mask from color (R G B) in the
4980 image.
4981 `:mask nil'
4982 means remove a mask, if any. */
177c0ea7 4983
ad18ffb1
GM
4984 mask = image_spec_value (spec, QCheuristic_mask, NULL);
4985 if (!NILP (mask))
4986 x_build_heuristic_mask (f, img, mask);
4987 else
4988 {
4989 int found_p;
177c0ea7 4990
ad18ffb1 4991 mask = image_spec_value (spec, QCmask, &found_p);
177c0ea7 4992
ad18ffb1
GM
4993 if (EQ (mask, Qheuristic))
4994 x_build_heuristic_mask (f, img, Qt);
4995 else if (CONSP (mask)
4996 && EQ (XCAR (mask), Qheuristic))
4997 {
4998 if (CONSP (XCDR (mask)))
4999 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
5000 else
5001 x_build_heuristic_mask (f, img, XCDR (mask));
5002 }
5003 else if (NILP (mask) && found_p && img->mask)
5004 {
5005 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
5006 img->mask = None;
5007 }
5008 }
177c0ea7
JB
5009
5010
ad18ffb1
GM
5011 /* Should we apply an image transformation algorithm? */
5012 conversion = image_spec_value (spec, QCconversion, NULL);
5013 if (EQ (conversion, Qdisabled))
5014 x_disable_image (f, img);
5015 else if (EQ (conversion, Qlaplace))
5016 x_laplace (f, img);
5017 else if (EQ (conversion, Qemboss))
5018 x_emboss (f, img);
5019 else if (CONSP (conversion)
5020 && EQ (XCAR (conversion), Qedge_detection))
5021 {
5022 Lisp_Object tem;
5023 tem = XCDR (conversion);
5024 if (CONSP (tem))
5025 x_edge_detection (f, img,
5026 Fplist_get (tem, QCmatrix),
5027 Fplist_get (tem, QCcolor_adjustment));
5028 }
5029 }
5030}
5031
5032
333b20bb 5033/* Return the id of image with Lisp specification SPEC on frame F.
83676598 5034 SPEC must be a valid Lisp image specification (see valid_image_p). */
333b20bb
GM
5035
5036int
83676598 5037lookup_image (f, spec)
333b20bb
GM
5038 struct frame *f;
5039 Lisp_Object spec;
5040{
5041 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5042 struct image *img;
5043 int i;
5044 unsigned hash;
5045 struct gcpro gcpro1;
4f7ca1f1 5046 EMACS_TIME now;
333b20bb
GM
5047
5048 /* F must be a window-system frame, and SPEC must be a valid image
5049 specification. */
5050 xassert (FRAME_WINDOW_P (f));
5051 xassert (valid_image_p (spec));
177c0ea7 5052
333b20bb
GM
5053 GCPRO1 (spec);
5054
5055 /* Look up SPEC in the hash table of the image cache. */
5056 hash = sxhash (spec, 0);
5057 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
5058
5059 for (img = c->buckets[i]; img; img = img->next)
5060 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
5061 break;
5062
5063 /* If not found, create a new image and cache it. */
5064 if (img == NULL)
5065 {
ad18ffb1 5066 extern Lisp_Object Qpostscript;
177c0ea7 5067
28c7826c 5068 BLOCK_INPUT;
333b20bb
GM
5069 img = make_image (spec, hash);
5070 cache_image (f, img);
83676598 5071 img->load_failed_p = img->type->load (f, img) == 0;
333b20bb
GM
5072
5073 /* If we can't load the image, and we don't have a width and
5074 height, use some arbitrary width and height so that we can
5075 draw a rectangle for it. */
83676598 5076 if (img->load_failed_p)
333b20bb
GM
5077 {
5078 Lisp_Object value;
5079
5080 value = image_spec_value (spec, QCwidth, NULL);
5081 img->width = (INTEGERP (value)
5082 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
5083 value = image_spec_value (spec, QCheight, NULL);
5084 img->height = (INTEGERP (value)
5085 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
5086 }
5087 else
5088 {
5089 /* Handle image type independent image attributes
f20a3b7a
MB
5090 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
5091 `:background COLOR'. */
5092 Lisp_Object ascent, margin, relief, bg;
333b20bb
GM
5093
5094 ascent = image_spec_value (spec, QCascent, NULL);
5095 if (INTEGERP (ascent))
5096 img->ascent = XFASTINT (ascent);
7c7ff7f5
GM
5097 else if (EQ (ascent, Qcenter))
5098 img->ascent = CENTERED_IMAGE_ASCENT;
177c0ea7 5099
333b20bb
GM
5100 margin = image_spec_value (spec, QCmargin, NULL);
5101 if (INTEGERP (margin) && XINT (margin) >= 0)
3ed61e75
GM
5102 img->vmargin = img->hmargin = XFASTINT (margin);
5103 else if (CONSP (margin) && INTEGERP (XCAR (margin))
5104 && INTEGERP (XCDR (margin)))
5105 {
5106 if (XINT (XCAR (margin)) > 0)
5107 img->hmargin = XFASTINT (XCAR (margin));
5108 if (XINT (XCDR (margin)) > 0)
5109 img->vmargin = XFASTINT (XCDR (margin));
5110 }
177c0ea7 5111
333b20bb
GM
5112 relief = image_spec_value (spec, QCrelief, NULL);
5113 if (INTEGERP (relief))
5114 {
5115 img->relief = XINT (relief);
3ed61e75
GM
5116 img->hmargin += abs (img->relief);
5117 img->vmargin += abs (img->relief);
333b20bb
GM
5118 }
5119
f20a3b7a
MB
5120 if (! img->background_valid)
5121 {
5122 bg = image_spec_value (img->spec, QCbackground, NULL);
5123 if (!NILP (bg))
5124 {
5125 img->background
5126 = x_alloc_image_color (f, img, bg,
5127 FRAME_BACKGROUND_PIXEL (f));
5128 img->background_valid = 1;
5129 }
5130 }
5131
ad18ffb1
GM
5132 /* Do image transformations and compute masks, unless we
5133 don't have the image yet. */
5134 if (!EQ (*img->type->type, Qpostscript))
5135 postprocess_image (f, img);
333b20bb 5136 }
dd00328a 5137
28c7826c
GM
5138 UNBLOCK_INPUT;
5139 xassert (!interrupt_input_blocked);
333b20bb
GM
5140 }
5141
4f7ca1f1
GM
5142 /* We're using IMG, so set its timestamp to `now'. */
5143 EMACS_GET_TIME (now);
5144 img->timestamp = EMACS_SECS (now);
177c0ea7 5145
333b20bb 5146 UNGCPRO;
177c0ea7 5147
333b20bb
GM
5148 /* Value is the image id. */
5149 return img->id;
5150}
5151
5152
5153/* Cache image IMG in the image cache of frame F. */
5154
5155static void
5156cache_image (f, img)
5157 struct frame *f;
5158 struct image *img;
5159{
5160 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5161 int i;
5162
5163 /* Find a free slot in c->images. */
5164 for (i = 0; i < c->used; ++i)
5165 if (c->images[i] == NULL)
5166 break;
5167
5168 /* If no free slot found, maybe enlarge c->images. */
5169 if (i == c->used && c->used == c->size)
5170 {
5171 c->size *= 2;
5172 c->images = (struct image **) xrealloc (c->images,
5173 c->size * sizeof *c->images);
5174 }
5175
5176 /* Add IMG to c->images, and assign IMG an id. */
5177 c->images[i] = img;
5178 img->id = i;
5179 if (i == c->used)
5180 ++c->used;
5181
5182 /* Add IMG to the cache's hash table. */
5183 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
5184 img->next = c->buckets[i];
5185 if (img->next)
5186 img->next->prev = img;
5187 img->prev = NULL;
5188 c->buckets[i] = img;
5189}
5190
5191
5192/* Call FN on every image in the image cache of frame F. Used to mark
5193 Lisp Objects in the image cache. */
5194
5195void
5196forall_images_in_image_cache (f, fn)
5197 struct frame *f;
5198 void (*fn) P_ ((struct image *img));
5199{
5200 if (FRAME_LIVE_P (f) && FRAME_X_P (f))
5201 {
5202 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5203 if (c)
5204 {
5205 int i;
5206 for (i = 0; i < c->used; ++i)
5207 if (c->images[i])
5208 fn (c->images[i]);
5209 }
5210 }
5211}
5212
5213
5214\f
5215/***********************************************************************
5216 X support code
5217 ***********************************************************************/
5218
45158a91
GM
5219static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
5220 XImage **, Pixmap *));
333b20bb
GM
5221static void x_destroy_x_image P_ ((XImage *));
5222static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
5223
5224
5225/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
5226 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
5227 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
5228 via xmalloc. Print error messages via image_error if an error
45158a91 5229 occurs. Value is non-zero if successful. */
333b20bb
GM
5230
5231static int
45158a91 5232x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
333b20bb 5233 struct frame *f;
333b20bb
GM
5234 int width, height, depth;
5235 XImage **ximg;
5236 Pixmap *pixmap;
5237{
5238 Display *display = FRAME_X_DISPLAY (f);
5239 Screen *screen = FRAME_X_SCREEN (f);
5240 Window window = FRAME_X_WINDOW (f);
5241
5242 xassert (interrupt_input_blocked);
5243
5244 if (depth <= 0)
5245 depth = DefaultDepthOfScreen (screen);
5246 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
5247 depth, ZPixmap, 0, NULL, width, height,
5248 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
5249 if (*ximg == NULL)
5250 {
45158a91 5251 image_error ("Unable to allocate X image", Qnil, Qnil);
333b20bb
GM
5252 return 0;
5253 }
5254
5255 /* Allocate image raster. */
5256 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
5257
5258 /* Allocate a pixmap of the same size. */
5259 *pixmap = XCreatePixmap (display, window, width, height, depth);
dd00328a 5260 if (*pixmap == None)
333b20bb
GM
5261 {
5262 x_destroy_x_image (*ximg);
5263 *ximg = NULL;
45158a91 5264 image_error ("Unable to create X pixmap", Qnil, Qnil);
333b20bb
GM
5265 return 0;
5266 }
5267
5268 return 1;
5269}
5270
5271
5272/* Destroy XImage XIMG. Free XIMG->data. */
5273
5274static void
5275x_destroy_x_image (ximg)
5276 XImage *ximg;
5277{
5278 xassert (interrupt_input_blocked);
5279 if (ximg)
5280 {
5281 xfree (ximg->data);
5282 ximg->data = NULL;
5283 XDestroyImage (ximg);
5284 }
5285}
5286
5287
5288/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
5289 are width and height of both the image and pixmap. */
5290
ea6b19ca 5291static void
333b20bb
GM
5292x_put_x_image (f, ximg, pixmap, width, height)
5293 struct frame *f;
5294 XImage *ximg;
5295 Pixmap pixmap;
caeea55a 5296 int width, height;
333b20bb
GM
5297{
5298 GC gc;
177c0ea7 5299
333b20bb
GM
5300 xassert (interrupt_input_blocked);
5301 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
5302 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
5303 XFreeGC (FRAME_X_DISPLAY (f), gc);
5304}
5305
5306
5307\f
5308/***********************************************************************
5be6c3b0 5309 File Handling
333b20bb
GM
5310 ***********************************************************************/
5311
5312static Lisp_Object x_find_image_file P_ ((Lisp_Object));
5be6c3b0
GM
5313static char *slurp_file P_ ((char *, int *));
5314
333b20bb
GM
5315
5316/* Find image file FILE. Look in data-directory, then
5317 x-bitmap-file-path. Value is the full name of the file found, or
5318 nil if not found. */
5319
5320static Lisp_Object
5321x_find_image_file (file)
5322 Lisp_Object file;
5323{
5324 Lisp_Object file_found, search_path;
5325 struct gcpro gcpro1, gcpro2;
5326 int fd;
5327
5328 file_found = Qnil;
5329 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
5330 GCPRO2 (file_found, search_path);
5331
5332 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
de2413e9 5333 fd = openp (search_path, file, Qnil, &file_found, Qnil);
177c0ea7 5334
939d6465 5335 if (fd == -1)
333b20bb
GM
5336 file_found = Qnil;
5337 else
5338 close (fd);
5339
5340 UNGCPRO;
5341 return file_found;
5342}
5343
5344
5be6c3b0
GM
5345/* Read FILE into memory. Value is a pointer to a buffer allocated
5346 with xmalloc holding FILE's contents. Value is null if an error
b243755a 5347 occurred. *SIZE is set to the size of the file. */
5be6c3b0
GM
5348
5349static char *
5350slurp_file (file, size)
5351 char *file;
5352 int *size;
5353{
5354 FILE *fp = NULL;
5355 char *buf = NULL;
5356 struct stat st;
5357
5358 if (stat (file, &st) == 0
5359 && (fp = fopen (file, "r")) != NULL
5360 && (buf = (char *) xmalloc (st.st_size),
5361 fread (buf, 1, st.st_size, fp) == st.st_size))
5362 {
5363 *size = st.st_size;
5364 fclose (fp);
5365 }
5366 else
5367 {
5368 if (fp)
5369 fclose (fp);
5370 if (buf)
5371 {
5372 xfree (buf);
5373 buf = NULL;
5374 }
5375 }
177c0ea7 5376
5be6c3b0
GM
5377 return buf;
5378}
5379
5380
333b20bb
GM
5381\f
5382/***********************************************************************
5383 XBM images
5384 ***********************************************************************/
5385
5be6c3b0 5386static int xbm_scan P_ ((char **, char *, char *, int *));
333b20bb 5387static int xbm_load P_ ((struct frame *f, struct image *img));
5be6c3b0
GM
5388static int xbm_load_image P_ ((struct frame *f, struct image *img,
5389 char *, char *));
333b20bb 5390static int xbm_image_p P_ ((Lisp_Object object));
5be6c3b0
GM
5391static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
5392 unsigned char **));
5393static int xbm_file_p P_ ((Lisp_Object));
333b20bb
GM
5394
5395
5396/* Indices of image specification fields in xbm_format, below. */
5397
5398enum xbm_keyword_index
5399{
5400 XBM_TYPE,
5401 XBM_FILE,
5402 XBM_WIDTH,
5403 XBM_HEIGHT,
5404 XBM_DATA,
5405 XBM_FOREGROUND,
5406 XBM_BACKGROUND,
5407 XBM_ASCENT,
5408 XBM_MARGIN,
5409 XBM_RELIEF,
5410 XBM_ALGORITHM,
5411 XBM_HEURISTIC_MASK,
4a8e312c 5412 XBM_MASK,
333b20bb
GM
5413 XBM_LAST
5414};
5415
5416/* Vector of image_keyword structures describing the format
5417 of valid XBM image specifications. */
5418
5419static struct image_keyword xbm_format[XBM_LAST] =
5420{
5421 {":type", IMAGE_SYMBOL_VALUE, 1},
5422 {":file", IMAGE_STRING_VALUE, 0},
5423 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
5424 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
5425 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6f1be3b9
GM
5426 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
5427 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
7c7ff7f5 5428 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 5429 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 5430 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 5431 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c
GM
5432 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
5433 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
333b20bb
GM
5434};
5435
5436/* Structure describing the image type XBM. */
5437
5438static struct image_type xbm_type =
5439{
5440 &Qxbm,
5441 xbm_image_p,
5442 xbm_load,
5443 x_clear_image,
5444 NULL
5445};
5446
5447/* Tokens returned from xbm_scan. */
5448
5449enum xbm_token
5450{
5451 XBM_TK_IDENT = 256,
5452 XBM_TK_NUMBER
5453};
5454
177c0ea7 5455
333b20bb
GM
5456/* Return non-zero if OBJECT is a valid XBM-type image specification.
5457 A valid specification is a list starting with the symbol `image'
5458 The rest of the list is a property list which must contain an
5459 entry `:type xbm..
5460
5461 If the specification specifies a file to load, it must contain
5462 an entry `:file FILENAME' where FILENAME is a string.
5463
5464 If the specification is for a bitmap loaded from memory it must
5465 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
5466 WIDTH and HEIGHT are integers > 0. DATA may be:
5467
5468 1. a string large enough to hold the bitmap data, i.e. it must
5469 have a size >= (WIDTH + 7) / 8 * HEIGHT
5470
5471 2. a bool-vector of size >= WIDTH * HEIGHT
5472
5473 3. a vector of strings or bool-vectors, one for each line of the
5474 bitmap.
5475
5be6c3b0
GM
5476 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
5477 may not be specified in this case because they are defined in the
5478 XBM file.
5479
333b20bb
GM
5480 Both the file and data forms may contain the additional entries
5481 `:background COLOR' and `:foreground COLOR'. If not present,
5482 foreground and background of the frame on which the image is
e3130015 5483 displayed is used. */
333b20bb
GM
5484
5485static int
5486xbm_image_p (object)
5487 Lisp_Object object;
5488{
5489 struct image_keyword kw[XBM_LAST];
177c0ea7 5490
333b20bb 5491 bcopy (xbm_format, kw, sizeof kw);
bfd2209f 5492 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
333b20bb
GM
5493 return 0;
5494
5495 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
5496
5497 if (kw[XBM_FILE].count)
5498 {
5499 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
5500 return 0;
5501 }
5be6c3b0
GM
5502 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
5503 {
5504 /* In-memory XBM file. */
5505 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
5506 return 0;
5507 }
333b20bb
GM
5508 else
5509 {
5510 Lisp_Object data;
5511 int width, height;
5512
5513 /* Entries for `:width', `:height' and `:data' must be present. */
5514 if (!kw[XBM_WIDTH].count
5515 || !kw[XBM_HEIGHT].count
5516 || !kw[XBM_DATA].count)
5517 return 0;
5518
5519 data = kw[XBM_DATA].value;
5520 width = XFASTINT (kw[XBM_WIDTH].value);
5521 height = XFASTINT (kw[XBM_HEIGHT].value);
177c0ea7 5522
333b20bb
GM
5523 /* Check type of data, and width and height against contents of
5524 data. */
5525 if (VECTORP (data))
5526 {
5527 int i;
177c0ea7 5528
333b20bb
GM
5529 /* Number of elements of the vector must be >= height. */
5530 if (XVECTOR (data)->size < height)
5531 return 0;
5532
5533 /* Each string or bool-vector in data must be large enough
5534 for one line of the image. */
5535 for (i = 0; i < height; ++i)
5536 {
5537 Lisp_Object elt = XVECTOR (data)->contents[i];
5538
5539 if (STRINGP (elt))
5540 {
d5db4077 5541 if (SCHARS (elt)
333b20bb
GM
5542 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
5543 return 0;
5544 }
5545 else if (BOOL_VECTOR_P (elt))
5546 {
5547 if (XBOOL_VECTOR (elt)->size < width)
5548 return 0;
5549 }
5550 else
5551 return 0;
5552 }
5553 }
5554 else if (STRINGP (data))
5555 {
d5db4077 5556 if (SCHARS (data)
333b20bb
GM
5557 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
5558 return 0;
5559 }
5560 else if (BOOL_VECTOR_P (data))
5561 {
5562 if (XBOOL_VECTOR (data)->size < width * height)
5563 return 0;
5564 }
5565 else
5566 return 0;
5567 }
5568
333b20bb
GM
5569 return 1;
5570}
5571
5572
5573/* Scan a bitmap file. FP is the stream to read from. Value is
5574 either an enumerator from enum xbm_token, or a character for a
5575 single-character token, or 0 at end of file. If scanning an
5576 identifier, store the lexeme of the identifier in SVAL. If
5577 scanning a number, store its value in *IVAL. */
5578
5579static int
5be6c3b0
GM
5580xbm_scan (s, end, sval, ival)
5581 char **s, *end;
333b20bb
GM
5582 char *sval;
5583 int *ival;
5584{
5585 int c;
0a695da7
GM
5586
5587 loop:
177c0ea7 5588
333b20bb 5589 /* Skip white space. */
5be6c3b0 5590 while (*s < end && (c = *(*s)++, isspace (c)))
333b20bb
GM
5591 ;
5592
5be6c3b0 5593 if (*s >= end)
333b20bb
GM
5594 c = 0;
5595 else if (isdigit (c))
5596 {
5597 int value = 0, digit;
177c0ea7 5598
5be6c3b0 5599 if (c == '0' && *s < end)
333b20bb 5600 {
5be6c3b0 5601 c = *(*s)++;
333b20bb
GM
5602 if (c == 'x' || c == 'X')
5603 {
5be6c3b0 5604 while (*s < end)
333b20bb 5605 {
5be6c3b0 5606 c = *(*s)++;
333b20bb
GM
5607 if (isdigit (c))
5608 digit = c - '0';
5609 else if (c >= 'a' && c <= 'f')
5610 digit = c - 'a' + 10;
5611 else if (c >= 'A' && c <= 'F')
5612 digit = c - 'A' + 10;
5613 else
5614 break;
5615 value = 16 * value + digit;
5616 }
5617 }
5618 else if (isdigit (c))
5619 {
5620 value = c - '0';
5be6c3b0
GM
5621 while (*s < end
5622 && (c = *(*s)++, isdigit (c)))
333b20bb
GM
5623 value = 8 * value + c - '0';
5624 }
5625 }
5626 else
5627 {
5628 value = c - '0';
5be6c3b0
GM
5629 while (*s < end
5630 && (c = *(*s)++, isdigit (c)))
333b20bb
GM
5631 value = 10 * value + c - '0';
5632 }
5633
5be6c3b0
GM
5634 if (*s < end)
5635 *s = *s - 1;
333b20bb
GM
5636 *ival = value;
5637 c = XBM_TK_NUMBER;
5638 }
5639 else if (isalpha (c) || c == '_')
5640 {
5641 *sval++ = c;
5be6c3b0
GM
5642 while (*s < end
5643 && (c = *(*s)++, (isalnum (c) || c == '_')))
333b20bb
GM
5644 *sval++ = c;
5645 *sval = 0;
5be6c3b0
GM
5646 if (*s < end)
5647 *s = *s - 1;
333b20bb
GM
5648 c = XBM_TK_IDENT;
5649 }
0a695da7
GM
5650 else if (c == '/' && **s == '*')
5651 {
5652 /* C-style comment. */
5653 ++*s;
5654 while (**s && (**s != '*' || *(*s + 1) != '/'))
5655 ++*s;
5656 if (**s)
5657 {
5658 *s += 2;
5659 goto loop;
5660 }
5661 }
333b20bb
GM
5662
5663 return c;
5664}
5665
5666
5667/* Replacement for XReadBitmapFileData which isn't available under old
5be6c3b0
GM
5668 X versions. CONTENTS is a pointer to a buffer to parse; END is the
5669 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
5670 the image. Return in *DATA the bitmap data allocated with xmalloc.
5671 Value is non-zero if successful. DATA null means just test if
b243755a 5672 CONTENTS looks like an in-memory XBM file. */
333b20bb
GM
5673
5674static int
5be6c3b0
GM
5675xbm_read_bitmap_data (contents, end, width, height, data)
5676 char *contents, *end;
333b20bb
GM
5677 int *width, *height;
5678 unsigned char **data;
5679{
5be6c3b0 5680 char *s = contents;
333b20bb
GM
5681 char buffer[BUFSIZ];
5682 int padding_p = 0;
5683 int v10 = 0;
5684 int bytes_per_line, i, nbytes;
5685 unsigned char *p;
5686 int value;
5687 int LA1;
5688
5689#define match() \
5be6c3b0 5690 LA1 = xbm_scan (&s, end, buffer, &value)
333b20bb
GM
5691
5692#define expect(TOKEN) \
5693 if (LA1 != (TOKEN)) \
5694 goto failure; \
5695 else \
177c0ea7 5696 match ()
333b20bb
GM
5697
5698#define expect_ident(IDENT) \
5699 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
5700 match (); \
5701 else \
5702 goto failure
5703
333b20bb 5704 *width = *height = -1;
5be6c3b0
GM
5705 if (data)
5706 *data = NULL;
5707 LA1 = xbm_scan (&s, end, buffer, &value);
333b20bb
GM
5708
5709 /* Parse defines for width, height and hot-spots. */
5710 while (LA1 == '#')
5711 {
333b20bb
GM
5712 match ();
5713 expect_ident ("define");
5714 expect (XBM_TK_IDENT);
5715
5716 if (LA1 == XBM_TK_NUMBER);
5717 {
5718 char *p = strrchr (buffer, '_');
5719 p = p ? p + 1 : buffer;
5720 if (strcmp (p, "width") == 0)
5721 *width = value;
5722 else if (strcmp (p, "height") == 0)
5723 *height = value;
5724 }
5725 expect (XBM_TK_NUMBER);
5726 }
5727
5728 if (*width < 0 || *height < 0)
5729 goto failure;
5be6c3b0
GM
5730 else if (data == NULL)
5731 goto success;
333b20bb
GM
5732
5733 /* Parse bits. Must start with `static'. */
5734 expect_ident ("static");
5735 if (LA1 == XBM_TK_IDENT)
5736 {
5737 if (strcmp (buffer, "unsigned") == 0)
5738 {
177c0ea7 5739 match ();
333b20bb
GM
5740 expect_ident ("char");
5741 }
5742 else if (strcmp (buffer, "short") == 0)
5743 {
5744 match ();
5745 v10 = 1;
5746 if (*width % 16 && *width % 16 < 9)
5747 padding_p = 1;
5748 }
5749 else if (strcmp (buffer, "char") == 0)
5750 match ();
5751 else
5752 goto failure;
5753 }
177c0ea7 5754 else
333b20bb
GM
5755 goto failure;
5756
5757 expect (XBM_TK_IDENT);
5758 expect ('[');
5759 expect (']');
5760 expect ('=');
5761 expect ('{');
5762
5763 bytes_per_line = (*width + 7) / 8 + padding_p;
5764 nbytes = bytes_per_line * *height;
5765 p = *data = (char *) xmalloc (nbytes);
5766
5767 if (v10)
5768 {
333b20bb
GM
5769 for (i = 0; i < nbytes; i += 2)
5770 {
5771 int val = value;
5772 expect (XBM_TK_NUMBER);
5773
5774 *p++ = val;
5775 if (!padding_p || ((i + 2) % bytes_per_line))
5776 *p++ = value >> 8;
177c0ea7 5777
333b20bb
GM
5778 if (LA1 == ',' || LA1 == '}')
5779 match ();
5780 else
5781 goto failure;
5782 }
5783 }
5784 else
5785 {
5786 for (i = 0; i < nbytes; ++i)
5787 {
5788 int val = value;
5789 expect (XBM_TK_NUMBER);
177c0ea7 5790
333b20bb 5791 *p++ = val;
177c0ea7 5792
333b20bb
GM
5793 if (LA1 == ',' || LA1 == '}')
5794 match ();
5795 else
5796 goto failure;
5797 }
5798 }
5799
5be6c3b0 5800 success:
333b20bb
GM
5801 return 1;
5802
5803 failure:
177c0ea7 5804
5be6c3b0 5805 if (data && *data)
333b20bb
GM
5806 {
5807 xfree (*data);
5808 *data = NULL;
5809 }
5810 return 0;
5811
5812#undef match
5813#undef expect
5814#undef expect_ident
5815}
5816
5817
5be6c3b0
GM
5818/* Load XBM image IMG which will be displayed on frame F from buffer
5819 CONTENTS. END is the end of the buffer. Value is non-zero if
5820 successful. */
333b20bb
GM
5821
5822static int
5be6c3b0 5823xbm_load_image (f, img, contents, end)
333b20bb
GM
5824 struct frame *f;
5825 struct image *img;
5be6c3b0 5826 char *contents, *end;
333b20bb
GM
5827{
5828 int rc;
5829 unsigned char *data;
5830 int success_p = 0;
177c0ea7 5831
5be6c3b0 5832 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
333b20bb
GM
5833 if (rc)
5834 {
5835 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
5836 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
5837 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
5838 Lisp_Object value;
177c0ea7 5839
333b20bb
GM
5840 xassert (img->width > 0 && img->height > 0);
5841
5842 /* Get foreground and background colors, maybe allocate colors. */
5843 value = image_spec_value (img->spec, QCforeground, NULL);
5844 if (!NILP (value))
5845 foreground = x_alloc_image_color (f, img, value, foreground);
333b20bb
GM
5846 value = image_spec_value (img->spec, QCbackground, NULL);
5847 if (!NILP (value))
f20a3b7a
MB
5848 {
5849 background = x_alloc_image_color (f, img, value, background);
5850 img->background = background;
5851 img->background_valid = 1;
5852 }
333b20bb 5853
333b20bb
GM
5854 img->pixmap
5855 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
5856 FRAME_X_WINDOW (f),
5857 data,
5858 img->width, img->height,
5859 foreground, background,
5860 depth);
5861 xfree (data);
5862
dd00328a 5863 if (img->pixmap == None)
333b20bb
GM
5864 {
5865 x_clear_image (f, img);
5be6c3b0 5866 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
333b20bb
GM
5867 }
5868 else
5869 success_p = 1;
333b20bb
GM
5870 }
5871 else
45158a91 5872 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
333b20bb 5873
333b20bb
GM
5874 return success_p;
5875}
5876
5877
5be6c3b0
GM
5878/* Value is non-zero if DATA looks like an in-memory XBM file. */
5879
5880static int
5881xbm_file_p (data)
5882 Lisp_Object data;
5883{
5884 int w, h;
5885 return (STRINGP (data)
d5db4077
KR
5886 && xbm_read_bitmap_data (SDATA (data),
5887 (SDATA (data)
5888 + SBYTES (data)),
5be6c3b0
GM
5889 &w, &h, NULL));
5890}
5891
177c0ea7 5892
333b20bb
GM
5893/* Fill image IMG which is used on frame F with pixmap data. Value is
5894 non-zero if successful. */
5895
5896static int
5897xbm_load (f, img)
5898 struct frame *f;
5899 struct image *img;
5900{
5901 int success_p = 0;
5902 Lisp_Object file_name;
5903
5904 xassert (xbm_image_p (img->spec));
5905
5906 /* If IMG->spec specifies a file name, create a non-file spec from it. */
5907 file_name = image_spec_value (img->spec, QCfile, NULL);
5908 if (STRINGP (file_name))
5be6c3b0
GM
5909 {
5910 Lisp_Object file;
5911 char *contents;
5912 int size;
5913 struct gcpro gcpro1;
177c0ea7 5914
5be6c3b0
GM
5915 file = x_find_image_file (file_name);
5916 GCPRO1 (file);
5917 if (!STRINGP (file))
5918 {
5919 image_error ("Cannot find image file `%s'", file_name, Qnil);
5920 UNGCPRO;
5921 return 0;
5922 }
5923
d5db4077 5924 contents = slurp_file (SDATA (file), &size);
5be6c3b0
GM
5925 if (contents == NULL)
5926 {
5927 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
5928 UNGCPRO;
5929 return 0;
5930 }
5931
5932 success_p = xbm_load_image (f, img, contents, contents + size);
5933 UNGCPRO;
5934 }
333b20bb
GM
5935 else
5936 {
5937 struct image_keyword fmt[XBM_LAST];
5938 Lisp_Object data;
5939 int depth;
5940 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
5941 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
5942 char *bits;
9b207e8e 5943 int parsed_p;
5be6c3b0
GM
5944 int in_memory_file_p = 0;
5945
5946 /* See if data looks like an in-memory XBM file. */
5947 data = image_spec_value (img->spec, QCdata, NULL);
5948 in_memory_file_p = xbm_file_p (data);
333b20bb 5949
5be6c3b0 5950 /* Parse the image specification. */
333b20bb 5951 bcopy (xbm_format, fmt, sizeof fmt);
bfd2209f 5952 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
333b20bb
GM
5953 xassert (parsed_p);
5954
5955 /* Get specified width, and height. */
5be6c3b0
GM
5956 if (!in_memory_file_p)
5957 {
5958 img->width = XFASTINT (fmt[XBM_WIDTH].value);
5959 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
5960 xassert (img->width > 0 && img->height > 0);
5961 }
333b20bb 5962
333b20bb 5963 /* Get foreground and background colors, maybe allocate colors. */
6f1be3b9
GM
5964 if (fmt[XBM_FOREGROUND].count
5965 && STRINGP (fmt[XBM_FOREGROUND].value))
333b20bb
GM
5966 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
5967 foreground);
6f1be3b9
GM
5968 if (fmt[XBM_BACKGROUND].count
5969 && STRINGP (fmt[XBM_BACKGROUND].value))
333b20bb
GM
5970 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
5971 background);
5972
5be6c3b0 5973 if (in_memory_file_p)
d5db4077
KR
5974 success_p = xbm_load_image (f, img, SDATA (data),
5975 (SDATA (data)
5976 + SBYTES (data)));
5be6c3b0 5977 else
333b20bb 5978 {
5be6c3b0
GM
5979 if (VECTORP (data))
5980 {
5981 int i;
5982 char *p;
5983 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
177c0ea7 5984
5be6c3b0
GM
5985 p = bits = (char *) alloca (nbytes * img->height);
5986 for (i = 0; i < img->height; ++i, p += nbytes)
5987 {
5988 Lisp_Object line = XVECTOR (data)->contents[i];
5989 if (STRINGP (line))
d5db4077 5990 bcopy (SDATA (line), p, nbytes);
5be6c3b0
GM
5991 else
5992 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
5993 }
5994 }
5995 else if (STRINGP (data))
d5db4077 5996 bits = SDATA (data);
5be6c3b0
GM
5997 else
5998 bits = XBOOL_VECTOR (data)->data;
5999
6000 /* Create the pixmap. */
6001 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
6002 img->pixmap
6003 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
6004 FRAME_X_WINDOW (f),
6005 bits,
6006 img->width, img->height,
6007 foreground, background,
6008 depth);
6009 if (img->pixmap)
6010 success_p = 1;
6011 else
333b20bb 6012 {
5be6c3b0
GM
6013 image_error ("Unable to create pixmap for XBM image `%s'",
6014 img->spec, Qnil);
6015 x_clear_image (f, img);
333b20bb
GM
6016 }
6017 }
333b20bb
GM
6018 }
6019
6020 return success_p;
6021}
177c0ea7 6022
333b20bb
GM
6023
6024\f
6025/***********************************************************************
6026 XPM images
6027 ***********************************************************************/
6028
177c0ea7 6029#if HAVE_XPM
333b20bb
GM
6030
6031static int xpm_image_p P_ ((Lisp_Object object));
6032static int xpm_load P_ ((struct frame *f, struct image *img));
6033static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
6034
6035#include "X11/xpm.h"
6036
6037/* The symbol `xpm' identifying XPM-format images. */
6038
6039Lisp_Object Qxpm;
6040
6041/* Indices of image specification fields in xpm_format, below. */
6042
6043enum xpm_keyword_index
6044{
6045 XPM_TYPE,
6046 XPM_FILE,
6047 XPM_DATA,
6048 XPM_ASCENT,
6049 XPM_MARGIN,
6050 XPM_RELIEF,
6051 XPM_ALGORITHM,
6052 XPM_HEURISTIC_MASK,
4a8e312c 6053 XPM_MASK,
333b20bb 6054 XPM_COLOR_SYMBOLS,
f20a3b7a 6055 XPM_BACKGROUND,
333b20bb
GM
6056 XPM_LAST
6057};
6058
6059/* Vector of image_keyword structures describing the format
6060 of valid XPM image specifications. */
6061
6062static struct image_keyword xpm_format[XPM_LAST] =
6063{
6064 {":type", IMAGE_SYMBOL_VALUE, 1},
6065 {":file", IMAGE_STRING_VALUE, 0},
6066 {":data", IMAGE_STRING_VALUE, 0},
7c7ff7f5 6067 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 6068 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 6069 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 6070 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
333b20bb 6071 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 6072 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
6073 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6074 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
6075};
6076
6077/* Structure describing the image type XBM. */
6078
6079static struct image_type xpm_type =
6080{
6081 &Qxpm,
6082 xpm_image_p,
6083 xpm_load,
6084 x_clear_image,
6085 NULL
6086};
6087
6088
b243755a
GM
6089/* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
6090 functions for allocating image colors. Our own functions handle
6091 color allocation failures more gracefully than the ones on the XPM
6092 lib. */
6093
6094#if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
6095#define ALLOC_XPM_COLORS
6096#endif
6097
6098#ifdef ALLOC_XPM_COLORS
6099
f72c62ad 6100static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *));
b243755a
GM
6101static void xpm_free_color_cache P_ ((void));
6102static int xpm_lookup_color P_ ((struct frame *, char *, XColor *));
f72c62ad
GM
6103static int xpm_color_bucket P_ ((char *));
6104static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *,
6105 XColor *, int));
b243755a
GM
6106
6107/* An entry in a hash table used to cache color definitions of named
6108 colors. This cache is necessary to speed up XPM image loading in
6109 case we do color allocations ourselves. Without it, we would need
6110 a call to XParseColor per pixel in the image. */
6111
6112struct xpm_cached_color
6113{
6114 /* Next in collision chain. */
6115 struct xpm_cached_color *next;
6116
6117 /* Color definition (RGB and pixel color). */
6118 XColor color;
6119
6120 /* Color name. */
6121 char name[1];
6122};
6123
6124/* The hash table used for the color cache, and its bucket vector
6125 size. */
6126
6127#define XPM_COLOR_CACHE_BUCKETS 1001
6128struct xpm_cached_color **xpm_color_cache;
6129
b243755a
GM
6130/* Initialize the color cache. */
6131
6132static void
f72c62ad
GM
6133xpm_init_color_cache (f, attrs)
6134 struct frame *f;
6135 XpmAttributes *attrs;
b243755a
GM
6136{
6137 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
6138 xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes);
6139 memset (xpm_color_cache, 0, nbytes);
6140 init_color_table ();
f72c62ad
GM
6141
6142 if (attrs->valuemask & XpmColorSymbols)
6143 {
6144 int i;
6145 XColor color;
177c0ea7 6146
f72c62ad
GM
6147 for (i = 0; i < attrs->numsymbols; ++i)
6148 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
6149 attrs->colorsymbols[i].value, &color))
6150 {
6151 color.pixel = lookup_rgb_color (f, color.red, color.green,
6152 color.blue);
6153 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
6154 }
6155 }
b243755a
GM
6156}
6157
6158
6159/* Free the color cache. */
6160
6161static void
6162xpm_free_color_cache ()
6163{
6164 struct xpm_cached_color *p, *next;
6165 int i;
6166
6167 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
6168 for (p = xpm_color_cache[i]; p; p = next)
6169 {
6170 next = p->next;
6171 xfree (p);
6172 }
6173
6174 xfree (xpm_color_cache);
6175 xpm_color_cache = NULL;
6176 free_color_table ();
6177}
6178
6179
f72c62ad
GM
6180/* Return the bucket index for color named COLOR_NAME in the color
6181 cache. */
6182
6183static int
6184xpm_color_bucket (color_name)
6185 char *color_name;
6186{
6187 unsigned h = 0;
6188 char *s;
177c0ea7 6189
f72c62ad
GM
6190 for (s = color_name; *s; ++s)
6191 h = (h << 2) ^ *s;
6192 return h %= XPM_COLOR_CACHE_BUCKETS;
6193}
6194
6195
6196/* On frame F, cache values COLOR for color with name COLOR_NAME.
6197 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
6198 entry added. */
6199
6200static struct xpm_cached_color *
6201xpm_cache_color (f, color_name, color, bucket)
6202 struct frame *f;
6203 char *color_name;
6204 XColor *color;
6205 int bucket;
6206{
6207 size_t nbytes;
6208 struct xpm_cached_color *p;
177c0ea7 6209
f72c62ad
GM
6210 if (bucket < 0)
6211 bucket = xpm_color_bucket (color_name);
177c0ea7 6212
f72c62ad
GM
6213 nbytes = sizeof *p + strlen (color_name);
6214 p = (struct xpm_cached_color *) xmalloc (nbytes);
6215 strcpy (p->name, color_name);
6216 p->color = *color;
6217 p->next = xpm_color_cache[bucket];
6218 xpm_color_cache[bucket] = p;
6219 return p;
6220}
6221
6222
b243755a
GM
6223/* Look up color COLOR_NAME for frame F in the color cache. If found,
6224 return the cached definition in *COLOR. Otherwise, make a new
6225 entry in the cache and allocate the color. Value is zero if color
6226 allocation failed. */
6227
6228static int
6229xpm_lookup_color (f, color_name, color)
6230 struct frame *f;
6231 char *color_name;
6232 XColor *color;
6233{
b243755a 6234 struct xpm_cached_color *p;
83676598 6235 int h = xpm_color_bucket (color_name);
b243755a
GM
6236
6237 for (p = xpm_color_cache[h]; p; p = p->next)
6238 if (strcmp (p->name, color_name) == 0)
6239 break;
6240
6241 if (p != NULL)
6242 *color = p->color;
6243 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
6244 color_name, color))
6245 {
b243755a
GM
6246 color->pixel = lookup_rgb_color (f, color->red, color->green,
6247 color->blue);
f72c62ad 6248 p = xpm_cache_color (f, color_name, color, h);
b243755a 6249 }
9b8a0941
DL
6250 /* You get `opaque' at least from ImageMagick converting pbm to xpm
6251 with transparency, and it's useful. */
6252 else if (strcmp ("opaque", color_name) == 0)
6253 {
6254 bzero (color, sizeof (XColor)); /* Is this necessary/correct? */
6255 color->pixel = FRAME_FOREGROUND_PIXEL (f);
6256 p = xpm_cache_color (f, color_name, color, h);
6257 }
177c0ea7 6258
b243755a
GM
6259 return p != NULL;
6260}
6261
6262
6263/* Callback for allocating color COLOR_NAME. Called from the XPM lib.
6264 CLOSURE is a pointer to the frame on which we allocate the
6265 color. Return in *COLOR the allocated color. Value is non-zero
6266 if successful. */
6267
6268static int
6269xpm_alloc_color (dpy, cmap, color_name, color, closure)
6270 Display *dpy;
6271 Colormap cmap;
6272 char *color_name;
6273 XColor *color;
6274 void *closure;
6275{
6276 return xpm_lookup_color ((struct frame *) closure, color_name, color);
6277}
6278
6279
6280/* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
6281 is a pointer to the frame on which we allocate the color. Value is
6282 non-zero if successful. */
6283
6284static int
6285xpm_free_colors (dpy, cmap, pixels, npixels, closure)
6286 Display *dpy;
6287 Colormap cmap;
6288 Pixel *pixels;
6289 int npixels;
6290 void *closure;
6291{
6292 return 1;
6293}
6294
6295#endif /* ALLOC_XPM_COLORS */
6296
6297
333b20bb
GM
6298/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
6299 for XPM images. Such a list must consist of conses whose car and
6300 cdr are strings. */
6301
6302static int
6303xpm_valid_color_symbols_p (color_symbols)
6304 Lisp_Object color_symbols;
6305{
6306 while (CONSP (color_symbols))
6307 {
6308 Lisp_Object sym = XCAR (color_symbols);
6309 if (!CONSP (sym)
6310 || !STRINGP (XCAR (sym))
6311 || !STRINGP (XCDR (sym)))
6312 break;
6313 color_symbols = XCDR (color_symbols);
6314 }
6315
6316 return NILP (color_symbols);
6317}
6318
6319
6320/* Value is non-zero if OBJECT is a valid XPM image specification. */
6321
6322static int
6323xpm_image_p (object)
6324 Lisp_Object object;
6325{
6326 struct image_keyword fmt[XPM_LAST];
6327 bcopy (xpm_format, fmt, sizeof fmt);
bfd2209f 6328 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
333b20bb
GM
6329 /* Either `:file' or `:data' must be present. */
6330 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
6331 /* Either no `:color-symbols' or it's a list of conses
6332 whose car and cdr are strings. */
6333 && (fmt[XPM_COLOR_SYMBOLS].count == 0
7c7ff7f5 6334 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
333b20bb
GM
6335}
6336
6337
6338/* Load image IMG which will be displayed on frame F. Value is
6339 non-zero if successful. */
6340
6341static int
6342xpm_load (f, img)
6343 struct frame *f;
6344 struct image *img;
6345{
9b207e8e 6346 int rc;
333b20bb
GM
6347 XpmAttributes attrs;
6348 Lisp_Object specified_file, color_symbols;
6349
6350 /* Configure the XPM lib. Use the visual of frame F. Allocate
6351 close colors. Return colors allocated. */
6352 bzero (&attrs, sizeof attrs);
9b2956e2
GM
6353 attrs.visual = FRAME_X_VISUAL (f);
6354 attrs.colormap = FRAME_X_COLORMAP (f);
333b20bb 6355 attrs.valuemask |= XpmVisual;
9b2956e2 6356 attrs.valuemask |= XpmColormap;
b243755a
GM
6357
6358#ifdef ALLOC_XPM_COLORS
6359 /* Allocate colors with our own functions which handle
6360 failing color allocation more gracefully. */
6361 attrs.color_closure = f;
6362 attrs.alloc_color = xpm_alloc_color;
6363 attrs.free_colors = xpm_free_colors;
6364 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
6365#else /* not ALLOC_XPM_COLORS */
6366 /* Let the XPM lib allocate colors. */
333b20bb 6367 attrs.valuemask |= XpmReturnAllocPixels;
e4c082be 6368#ifdef XpmAllocCloseColors
333b20bb
GM
6369 attrs.alloc_close_colors = 1;
6370 attrs.valuemask |= XpmAllocCloseColors;
b243755a 6371#else /* not XpmAllocCloseColors */
e4c082be
RS
6372 attrs.closeness = 600;
6373 attrs.valuemask |= XpmCloseness;
b243755a
GM
6374#endif /* not XpmAllocCloseColors */
6375#endif /* ALLOC_XPM_COLORS */
333b20bb
GM
6376
6377 /* If image specification contains symbolic color definitions, add
6378 these to `attrs'. */
6379 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
6380 if (CONSP (color_symbols))
6381 {
6382 Lisp_Object tail;
6383 XpmColorSymbol *xpm_syms;
6384 int i, size;
177c0ea7 6385
333b20bb
GM
6386 attrs.valuemask |= XpmColorSymbols;
6387
6388 /* Count number of symbols. */
6389 attrs.numsymbols = 0;
6390 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
6391 ++attrs.numsymbols;
6392
6393 /* Allocate an XpmColorSymbol array. */
6394 size = attrs.numsymbols * sizeof *xpm_syms;
6395 xpm_syms = (XpmColorSymbol *) alloca (size);
6396 bzero (xpm_syms, size);
6397 attrs.colorsymbols = xpm_syms;
6398
6399 /* Fill the color symbol array. */
6400 for (tail = color_symbols, i = 0;
6401 CONSP (tail);
6402 ++i, tail = XCDR (tail))
6403 {
6404 Lisp_Object name = XCAR (XCAR (tail));
6405 Lisp_Object color = XCDR (XCAR (tail));
d5db4077
KR
6406 xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1);
6407 strcpy (xpm_syms[i].name, SDATA (name));
6408 xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1);
6409 strcpy (xpm_syms[i].value, SDATA (color));
333b20bb
GM
6410 }
6411 }
6412
6413 /* Create a pixmap for the image, either from a file, or from a
6414 string buffer containing data in the same format as an XPM file. */
b243755a 6415#ifdef ALLOC_XPM_COLORS
f72c62ad 6416 xpm_init_color_cache (f, &attrs);
b243755a 6417#endif
177c0ea7 6418
333b20bb
GM
6419 specified_file = image_spec_value (img->spec, QCfile, NULL);
6420 if (STRINGP (specified_file))
6421 {
6422 Lisp_Object file = x_find_image_file (specified_file);
6423 if (!STRINGP (file))
6424 {
45158a91 6425 image_error ("Cannot find image file `%s'", specified_file, Qnil);
333b20bb
GM
6426 return 0;
6427 }
177c0ea7 6428
333b20bb 6429 rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
d5db4077 6430 SDATA (file), &img->pixmap, &img->mask,
333b20bb
GM
6431 &attrs);
6432 }
6433 else
6434 {
6435 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
6436 rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
d5db4077 6437 SDATA (buffer),
333b20bb
GM
6438 &img->pixmap, &img->mask,
6439 &attrs);
6440 }
333b20bb
GM
6441
6442 if (rc == XpmSuccess)
6443 {
b243755a
GM
6444#ifdef ALLOC_XPM_COLORS
6445 img->colors = colors_in_color_table (&img->ncolors);
6446#else /* not ALLOC_XPM_COLORS */
f47a9ec4
KR
6447 int i;
6448
333b20bb
GM
6449 img->ncolors = attrs.nalloc_pixels;
6450 img->colors = (unsigned long *) xmalloc (img->ncolors
6451 * sizeof *img->colors);
6452 for (i = 0; i < attrs.nalloc_pixels; ++i)
3b4ae1cc
GM
6453 {
6454 img->colors[i] = attrs.alloc_pixels[i];
6455#ifdef DEBUG_X_COLORS
6456 register_color (img->colors[i]);
6457#endif
6458 }
b243755a 6459#endif /* not ALLOC_XPM_COLORS */
333b20bb
GM
6460
6461 img->width = attrs.width;
6462 img->height = attrs.height;
6463 xassert (img->width > 0 && img->height > 0);
6464
6465 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
333b20bb 6466 XpmFreeAttributes (&attrs);
333b20bb
GM
6467 }
6468 else
6469 {
6470 switch (rc)
6471 {
6472 case XpmOpenFailed:
6473 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
6474 break;
177c0ea7 6475
333b20bb
GM
6476 case XpmFileInvalid:
6477 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
6478 break;
177c0ea7 6479
333b20bb
GM
6480 case XpmNoMemory:
6481 image_error ("Out of memory (%s)", img->spec, Qnil);
6482 break;
177c0ea7 6483
333b20bb
GM
6484 case XpmColorFailed:
6485 image_error ("Color allocation error (%s)", img->spec, Qnil);
6486 break;
177c0ea7 6487
333b20bb
GM
6488 default:
6489 image_error ("Unknown error (%s)", img->spec, Qnil);
6490 break;
6491 }
6492 }
6493
b243755a
GM
6494#ifdef ALLOC_XPM_COLORS
6495 xpm_free_color_cache ();
6496#endif
333b20bb
GM
6497 return rc == XpmSuccess;
6498}
6499
6500#endif /* HAVE_XPM != 0 */
6501
6502\f
6503/***********************************************************************
6504 Color table
6505 ***********************************************************************/
6506
6507/* An entry in the color table mapping an RGB color to a pixel color. */
6508
6509struct ct_color
6510{
6511 int r, g, b;
6512 unsigned long pixel;
6513
6514 /* Next in color table collision list. */
6515 struct ct_color *next;
6516};
6517
6518/* The bucket vector size to use. Must be prime. */
6519
6520#define CT_SIZE 101
6521
6522/* Value is a hash of the RGB color given by R, G, and B. */
6523
6524#define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
6525
6526/* The color hash table. */
6527
6528struct ct_color **ct_table;
6529
6530/* Number of entries in the color table. */
6531
6532int ct_colors_allocated;
6533
333b20bb
GM
6534/* Initialize the color table. */
6535
6536static void
6537init_color_table ()
6538{
6539 int size = CT_SIZE * sizeof (*ct_table);
6540 ct_table = (struct ct_color **) xmalloc (size);
6541 bzero (ct_table, size);
6542 ct_colors_allocated = 0;
6543}
6544
6545
6546/* Free memory associated with the color table. */
6547
6548static void
6549free_color_table ()
6550{
6551 int i;
6552 struct ct_color *p, *next;
6553
6554 for (i = 0; i < CT_SIZE; ++i)
6555 for (p = ct_table[i]; p; p = next)
6556 {
6557 next = p->next;
6558 xfree (p);
6559 }
6560
6561 xfree (ct_table);
6562 ct_table = NULL;
6563}
6564
6565
6566/* Value is a pixel color for RGB color R, G, B on frame F. If an
6567 entry for that color already is in the color table, return the
6568 pixel color of that entry. Otherwise, allocate a new color for R,
6569 G, B, and make an entry in the color table. */
6570
6571static unsigned long
6572lookup_rgb_color (f, r, g, b)
6573 struct frame *f;
6574 int r, g, b;
6575{
6576 unsigned hash = CT_HASH_RGB (r, g, b);
6577 int i = hash % CT_SIZE;
6578 struct ct_color *p;
6579
6580 for (p = ct_table[i]; p; p = p->next)
6581 if (p->r == r && p->g == g && p->b == b)
6582 break;
6583
6584 if (p == NULL)
6585 {
6586 XColor color;
6587 Colormap cmap;
6588 int rc;
6589
6590 color.red = r;
6591 color.green = g;
6592 color.blue = b;
177c0ea7 6593
9b2956e2 6594 cmap = FRAME_X_COLORMAP (f);
d62c8769 6595 rc = x_alloc_nearest_color (f, cmap, &color);
333b20bb
GM
6596
6597 if (rc)
6598 {
6599 ++ct_colors_allocated;
177c0ea7 6600
333b20bb
GM
6601 p = (struct ct_color *) xmalloc (sizeof *p);
6602 p->r = r;
6603 p->g = g;
6604 p->b = b;
6605 p->pixel = color.pixel;
6606 p->next = ct_table[i];
6607 ct_table[i] = p;
6608 }
6609 else
6610 return FRAME_FOREGROUND_PIXEL (f);
6611 }
6612
6613 return p->pixel;
6614}
6615
6616
6617/* Look up pixel color PIXEL which is used on frame F in the color
6618 table. If not already present, allocate it. Value is PIXEL. */
6619
6620static unsigned long
6621lookup_pixel_color (f, pixel)
6622 struct frame *f;
6623 unsigned long pixel;
6624{
6625 int i = pixel % CT_SIZE;
6626 struct ct_color *p;
6627
6628 for (p = ct_table[i]; p; p = p->next)
6629 if (p->pixel == pixel)
6630 break;
6631
6632 if (p == NULL)
6633 {
6634 XColor color;
6635 Colormap cmap;
6636 int rc;
6637
9b2956e2 6638 cmap = FRAME_X_COLORMAP (f);
333b20bb 6639 color.pixel = pixel;
a31fedb7 6640 x_query_color (f, &color);
d62c8769 6641 rc = x_alloc_nearest_color (f, cmap, &color);
333b20bb
GM
6642
6643 if (rc)
6644 {
6645 ++ct_colors_allocated;
177c0ea7 6646
333b20bb
GM
6647 p = (struct ct_color *) xmalloc (sizeof *p);
6648 p->r = color.red;
6649 p->g = color.green;
6650 p->b = color.blue;
6651 p->pixel = pixel;
6652 p->next = ct_table[i];
6653 ct_table[i] = p;
6654 }
6655 else
6656 return FRAME_FOREGROUND_PIXEL (f);
6657 }
177c0ea7 6658
333b20bb
GM
6659 return p->pixel;
6660}
6661
6662
6663/* Value is a vector of all pixel colors contained in the color table,
6664 allocated via xmalloc. Set *N to the number of colors. */
6665
6666static unsigned long *
6667colors_in_color_table (n)
6668 int *n;
6669{
6670 int i, j;
6671 struct ct_color *p;
6672 unsigned long *colors;
6673
6674 if (ct_colors_allocated == 0)
6675 {
6676 *n = 0;
6677 colors = NULL;
6678 }
6679 else
6680 {
6681 colors = (unsigned long *) xmalloc (ct_colors_allocated
6682 * sizeof *colors);
6683 *n = ct_colors_allocated;
177c0ea7 6684
333b20bb
GM
6685 for (i = j = 0; i < CT_SIZE; ++i)
6686 for (p = ct_table[i]; p; p = p->next)
6687 colors[j++] = p->pixel;
6688 }
6689
6690 return colors;
6691}
6692
6693
6694\f
6695/***********************************************************************
6696 Algorithms
6697 ***********************************************************************/
6698
4a8e312c
GM
6699static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
6700static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
6701static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
6702
d2dc8167 6703/* Non-zero means draw a cross on images having `:conversion
14819cb3
GM
6704 disabled'. */
6705
6706int cross_disabled_images;
6707
4a8e312c
GM
6708/* Edge detection matrices for different edge-detection
6709 strategies. */
6710
6711static int emboss_matrix[9] = {
6712 /* x - 1 x x + 1 */
6713 2, -1, 0, /* y - 1 */
6714 -1, 0, 1, /* y */
6715 0, 1, -2 /* y + 1 */
6716};
333b20bb 6717
4a8e312c
GM
6718static int laplace_matrix[9] = {
6719 /* x - 1 x x + 1 */
6720 1, 0, 0, /* y - 1 */
6721 0, 0, 0, /* y */
6722 0, 0, -1 /* y + 1 */
6723};
333b20bb 6724
14819cb3
GM
6725/* Value is the intensity of the color whose red/green/blue values
6726 are R, G, and B. */
6727
6728#define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
6729
333b20bb 6730
4a8e312c
GM
6731/* On frame F, return an array of XColor structures describing image
6732 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
6733 non-zero means also fill the red/green/blue members of the XColor
6734 structures. Value is a pointer to the array of XColors structures,
6735 allocated with xmalloc; it must be freed by the caller. */
6736
6737static XColor *
6738x_to_xcolors (f, img, rgb_p)
333b20bb 6739 struct frame *f;
4a8e312c
GM
6740 struct image *img;
6741 int rgb_p;
333b20bb 6742{
4a8e312c
GM
6743 int x, y;
6744 XColor *colors, *p;
6745 XImage *ximg;
333b20bb 6746
4a8e312c
GM
6747 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
6748
6749 /* Get the X image IMG->pixmap. */
6750 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
6751 0, 0, img->width, img->height, ~0, ZPixmap);
333b20bb 6752
4a8e312c
GM
6753 /* Fill the `pixel' members of the XColor array. I wished there
6754 were an easy and portable way to circumvent XGetPixel. */
6755 p = colors;
6756 for (y = 0; y < img->height; ++y)
6757 {
6758 XColor *row = p;
177c0ea7 6759
4a8e312c
GM
6760 for (x = 0; x < img->width; ++x, ++p)
6761 p->pixel = XGetPixel (ximg, x, y);
6762
6763 if (rgb_p)
a31fedb7 6764 x_query_colors (f, row, img->width);
4a8e312c
GM
6765 }
6766
6767 XDestroyImage (ximg);
4a8e312c 6768 return colors;
333b20bb
GM
6769}
6770
6771
4a8e312c
GM
6772/* Create IMG->pixmap from an array COLORS of XColor structures, whose
6773 RGB members are set. F is the frame on which this all happens.
6774 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
333b20bb
GM
6775
6776static void
4a8e312c 6777x_from_xcolors (f, img, colors)
333b20bb 6778 struct frame *f;
4a8e312c
GM
6779 struct image *img;
6780 XColor *colors;
333b20bb 6781{
4a8e312c
GM
6782 int x, y;
6783 XImage *oimg;
6784 Pixmap pixmap;
6785 XColor *p;
177c0ea7 6786
4a8e312c 6787 init_color_table ();
177c0ea7 6788
4a8e312c
GM
6789 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
6790 &oimg, &pixmap);
6791 p = colors;
6792 for (y = 0; y < img->height; ++y)
6793 for (x = 0; x < img->width; ++x, ++p)
6794 {
6795 unsigned long pixel;
6796 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
6797 XPutPixel (oimg, x, y, pixel);
6798 }
6799
6800 xfree (colors);
dd00328a 6801 x_clear_image_1 (f, img, 1, 0, 1);
4a8e312c
GM
6802
6803 x_put_x_image (f, oimg, pixmap, img->width, img->height);
6804 x_destroy_x_image (oimg);
6805 img->pixmap = pixmap;
6806 img->colors = colors_in_color_table (&img->ncolors);
6807 free_color_table ();
333b20bb
GM
6808}
6809
6810
4a8e312c
GM
6811/* On frame F, perform edge-detection on image IMG.
6812
6813 MATRIX is a nine-element array specifying the transformation
6814 matrix. See emboss_matrix for an example.
177c0ea7 6815
4a8e312c
GM
6816 COLOR_ADJUST is a color adjustment added to each pixel of the
6817 outgoing image. */
333b20bb
GM
6818
6819static void
4a8e312c 6820x_detect_edges (f, img, matrix, color_adjust)
333b20bb
GM
6821 struct frame *f;
6822 struct image *img;
4a8e312c 6823 int matrix[9], color_adjust;
333b20bb 6824{
4a8e312c
GM
6825 XColor *colors = x_to_xcolors (f, img, 1);
6826 XColor *new, *p;
6827 int x, y, i, sum;
333b20bb 6828
4a8e312c
GM
6829 for (i = sum = 0; i < 9; ++i)
6830 sum += abs (matrix[i]);
333b20bb 6831
4a8e312c 6832#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
333b20bb 6833
4a8e312c 6834 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
333b20bb 6835
4a8e312c
GM
6836 for (y = 0; y < img->height; ++y)
6837 {
6838 p = COLOR (new, 0, y);
6839 p->red = p->green = p->blue = 0xffff/2;
6840 p = COLOR (new, img->width - 1, y);
6841 p->red = p->green = p->blue = 0xffff/2;
6842 }
177c0ea7 6843
4a8e312c
GM
6844 for (x = 1; x < img->width - 1; ++x)
6845 {
6846 p = COLOR (new, x, 0);
6847 p->red = p->green = p->blue = 0xffff/2;
6848 p = COLOR (new, x, img->height - 1);
6849 p->red = p->green = p->blue = 0xffff/2;
6850 }
333b20bb 6851
4a8e312c 6852 for (y = 1; y < img->height - 1; ++y)
333b20bb 6853 {
4a8e312c 6854 p = COLOR (new, 1, y);
177c0ea7 6855
4a8e312c
GM
6856 for (x = 1; x < img->width - 1; ++x, ++p)
6857 {
14819cb3 6858 int r, g, b, y1, x1;
4a8e312c
GM
6859
6860 r = g = b = i = 0;
6861 for (y1 = y - 1; y1 < y + 2; ++y1)
6862 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
6863 if (matrix[i])
6864 {
6865 XColor *t = COLOR (colors, x1, y1);
6866 r += matrix[i] * t->red;
6867 g += matrix[i] * t->green;
6868 b += matrix[i] * t->blue;
6869 }
333b20bb 6870
4a8e312c
GM
6871 r = (r / sum + color_adjust) & 0xffff;
6872 g = (g / sum + color_adjust) & 0xffff;
6873 b = (b / sum + color_adjust) & 0xffff;
14819cb3 6874 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
333b20bb 6875 }
333b20bb
GM
6876 }
6877
4a8e312c
GM
6878 xfree (colors);
6879 x_from_xcolors (f, img, new);
333b20bb 6880
4a8e312c
GM
6881#undef COLOR
6882}
6883
6884
6885/* Perform the pre-defined `emboss' edge-detection on image IMG
6886 on frame F. */
6887
6888static void
6889x_emboss (f, img)
6890 struct frame *f;
6891 struct image *img;
6892{
6893 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
6894}
6895
6896
6897/* Perform the pre-defined `laplace' edge-detection on image IMG
6898 on frame F. */
6899
6900static void
6901x_laplace (f, img)
6902 struct frame *f;
6903 struct image *img;
6904{
6905 x_detect_edges (f, img, laplace_matrix, 45000);
6906}
6907
6908
6909/* Perform edge-detection on image IMG on frame F, with specified
6910 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
6911
6912 MATRIX must be either
6913
6914 - a list of at least 9 numbers in row-major form
6915 - a vector of at least 9 numbers
6916
6917 COLOR_ADJUST nil means use a default; otherwise it must be a
6918 number. */
6919
6920static void
6921x_edge_detection (f, img, matrix, color_adjust)
6922 struct frame *f;
6923 struct image *img;
6924 Lisp_Object matrix, color_adjust;
6925{
6926 int i = 0;
6927 int trans[9];
177c0ea7 6928
4a8e312c
GM
6929 if (CONSP (matrix))
6930 {
6931 for (i = 0;
6932 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
6933 ++i, matrix = XCDR (matrix))
6934 trans[i] = XFLOATINT (XCAR (matrix));
6935 }
6936 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
6937 {
6938 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
6939 trans[i] = XFLOATINT (AREF (matrix, i));
6940 }
333b20bb 6941
4a8e312c
GM
6942 if (NILP (color_adjust))
6943 color_adjust = make_number (0xffff / 2);
333b20bb 6944
4a8e312c
GM
6945 if (i == 9 && NUMBERP (color_adjust))
6946 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
333b20bb
GM
6947}
6948
6949
14819cb3
GM
6950/* Transform image IMG on frame F so that it looks disabled. */
6951
6952static void
6953x_disable_image (f, img)
6954 struct frame *f;
6955 struct image *img;
6956{
6957 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
dd00328a 6958
14819cb3
GM
6959 if (dpyinfo->n_planes >= 2)
6960 {
6961 /* Color (or grayscale). Convert to gray, and equalize. Just
6962 drawing such images with a stipple can look very odd, so
6963 we're using this method instead. */
6964 XColor *colors = x_to_xcolors (f, img, 1);
6965 XColor *p, *end;
6966 const int h = 15000;
6967 const int l = 30000;
6968
6969 for (p = colors, end = colors + img->width * img->height;
6970 p < end;
6971 ++p)
6972 {
6973 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
6974 int i2 = (0xffff - h - l) * i / 0xffff + l;
6975 p->red = p->green = p->blue = i2;
6976 }
6977
6978 x_from_xcolors (f, img, colors);
6979 }
6980
6981 /* Draw a cross over the disabled image, if we must or if we
6982 should. */
6983 if (dpyinfo->n_planes < 2 || cross_disabled_images)
6984 {
6985 Display *dpy = FRAME_X_DISPLAY (f);
6986 GC gc;
6987
14819cb3
GM
6988 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
6989 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
6990 XDrawLine (dpy, img->pixmap, gc, 0, 0,
6991 img->width - 1, img->height - 1);
6992 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
6993 img->width - 1, 0);
6994 XFreeGC (dpy, gc);
6995
6996 if (img->mask)
6997 {
6998 gc = XCreateGC (dpy, img->mask, 0, NULL);
6999 XSetForeground (dpy, gc, WHITE_PIX_DEFAULT (f));
7000 XDrawLine (dpy, img->mask, gc, 0, 0,
7001 img->width - 1, img->height - 1);
7002 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
7003 img->width - 1, 0);
7004 XFreeGC (dpy, gc);
7005 }
14819cb3
GM
7006 }
7007}
7008
7009
333b20bb
GM
7010/* Build a mask for image IMG which is used on frame F. FILE is the
7011 name of an image file, for error messages. HOW determines how to
fcf431dc
GM
7012 determine the background color of IMG. If it is a list '(R G B)',
7013 with R, G, and B being integers >= 0, take that as the color of the
7014 background. Otherwise, determine the background color of IMG
7015 heuristically. Value is non-zero if successful. */
333b20bb
GM
7016
7017static int
45158a91 7018x_build_heuristic_mask (f, img, how)
333b20bb 7019 struct frame *f;
333b20bb
GM
7020 struct image *img;
7021 Lisp_Object how;
7022{
7023 Display *dpy = FRAME_X_DISPLAY (f);
333b20bb 7024 XImage *ximg, *mask_img;
f20a3b7a 7025 int x, y, rc, use_img_background;
8ec8a5ec 7026 unsigned long bg = 0;
333b20bb 7027
4a8e312c
GM
7028 if (img->mask)
7029 {
7030 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
dd00328a 7031 img->mask = None;
f20a3b7a 7032 img->background_transparent_valid = 0;
4a8e312c 7033 }
dd00328a 7034
333b20bb 7035 /* Create an image and pixmap serving as mask. */
45158a91 7036 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
333b20bb
GM
7037 &mask_img, &img->mask);
7038 if (!rc)
28c7826c 7039 return 0;
333b20bb
GM
7040
7041 /* Get the X image of IMG->pixmap. */
7042 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
7043 ~0, ZPixmap);
7044
fcf431dc 7045 /* Determine the background color of ximg. If HOW is `(R G B)'
f20a3b7a
MB
7046 take that as color. Otherwise, use the image's background color. */
7047 use_img_background = 1;
177c0ea7 7048
fcf431dc
GM
7049 if (CONSP (how))
7050 {
cac1daf0 7051 int rgb[3], i;
fcf431dc 7052
cac1daf0 7053 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
fcf431dc
GM
7054 {
7055 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
7056 how = XCDR (how);
7057 }
7058
7059 if (i == 3 && NILP (how))
7060 {
7061 char color_name[30];
fcf431dc 7062 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
053b3256
GM
7063 bg = x_alloc_image_color (f, img, build_string (color_name), 0);
7064 use_img_background = 0;
fcf431dc
GM
7065 }
7066 }
177c0ea7 7067
f20a3b7a 7068 if (use_img_background)
43f7c3ea 7069 bg = four_corners_best (ximg, img->width, img->height);
333b20bb
GM
7070
7071 /* Set all bits in mask_img to 1 whose color in ximg is different
7072 from the background color bg. */
7073 for (y = 0; y < img->height; ++y)
7074 for (x = 0; x < img->width; ++x)
7075 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
7076
f20a3b7a
MB
7077 /* Fill in the background_transparent field while we have the mask handy. */
7078 image_background_transparent (img, f, mask_img);
7079
333b20bb
GM
7080 /* Put mask_img into img->mask. */
7081 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
7082 x_destroy_x_image (mask_img);
7083 XDestroyImage (ximg);
177c0ea7 7084
333b20bb
GM
7085 return 1;
7086}
7087
7088
7089\f
7090/***********************************************************************
7091 PBM (mono, gray, color)
7092 ***********************************************************************/
7093
7094static int pbm_image_p P_ ((Lisp_Object object));
7095static int pbm_load P_ ((struct frame *f, struct image *img));
63cec32f 7096static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
333b20bb
GM
7097
7098/* The symbol `pbm' identifying images of this type. */
7099
7100Lisp_Object Qpbm;
7101
7102/* Indices of image specification fields in gs_format, below. */
7103
7104enum pbm_keyword_index
7105{
7106 PBM_TYPE,
7107 PBM_FILE,
63cec32f 7108 PBM_DATA,
333b20bb
GM
7109 PBM_ASCENT,
7110 PBM_MARGIN,
7111 PBM_RELIEF,
7112 PBM_ALGORITHM,
7113 PBM_HEURISTIC_MASK,
4a8e312c 7114 PBM_MASK,
be0b1fac
GM
7115 PBM_FOREGROUND,
7116 PBM_BACKGROUND,
333b20bb
GM
7117 PBM_LAST
7118};
7119
7120/* Vector of image_keyword structures describing the format
7121 of valid user-defined image specifications. */
7122
7123static struct image_keyword pbm_format[PBM_LAST] =
7124{
7125 {":type", IMAGE_SYMBOL_VALUE, 1},
63cec32f
GM
7126 {":file", IMAGE_STRING_VALUE, 0},
7127 {":data", IMAGE_STRING_VALUE, 0},
7c7ff7f5 7128 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 7129 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 7130 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 7131 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 7132 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
be0b1fac 7133 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6f1be3b9
GM
7134 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
7135 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
7136};
7137
7138/* Structure describing the image type `pbm'. */
7139
7140static struct image_type pbm_type =
7141{
7142 &Qpbm,
7143 pbm_image_p,
7144 pbm_load,
7145 x_clear_image,
7146 NULL
7147};
7148
7149
7150/* Return non-zero if OBJECT is a valid PBM image specification. */
7151
7152static int
7153pbm_image_p (object)
7154 Lisp_Object object;
7155{
7156 struct image_keyword fmt[PBM_LAST];
177c0ea7 7157
333b20bb 7158 bcopy (pbm_format, fmt, sizeof fmt);
177c0ea7 7159
7c7ff7f5 7160 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
333b20bb 7161 return 0;
63cec32f
GM
7162
7163 /* Must specify either :data or :file. */
7164 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
333b20bb
GM
7165}
7166
7167
63cec32f
GM
7168/* Scan a decimal number from *S and return it. Advance *S while
7169 reading the number. END is the end of the string. Value is -1 at
7170 end of input. */
333b20bb
GM
7171
7172static int
63cec32f
GM
7173pbm_scan_number (s, end)
7174 unsigned char **s, *end;
333b20bb 7175{
8ec8a5ec 7176 int c = 0, val = -1;
333b20bb 7177
63cec32f 7178 while (*s < end)
333b20bb
GM
7179 {
7180 /* Skip white-space. */
63cec32f 7181 while (*s < end && (c = *(*s)++, isspace (c)))
333b20bb
GM
7182 ;
7183
7184 if (c == '#')
7185 {
7186 /* Skip comment to end of line. */
63cec32f 7187 while (*s < end && (c = *(*s)++, c != '\n'))
333b20bb
GM
7188 ;
7189 }
7190 else if (isdigit (c))
7191 {
7192 /* Read decimal number. */
7193 val = c - '0';
63cec32f 7194 while (*s < end && (c = *(*s)++, isdigit (c)))
333b20bb
GM
7195 val = 10 * val + c - '0';
7196 break;
7197 }
7198 else
7199 break;
7200 }
7201
7202 return val;
7203}
7204
7205
7206/* Load PBM image IMG for use on frame F. */
7207
177c0ea7 7208static int
333b20bb
GM
7209pbm_load (f, img)
7210 struct frame *f;
7211 struct image *img;
7212{
333b20bb 7213 int raw_p, x, y;
b6d7acec 7214 int width, height, max_color_idx = 0;
333b20bb
GM
7215 XImage *ximg;
7216 Lisp_Object file, specified_file;
7217 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
7218 struct gcpro gcpro1;
63cec32f
GM
7219 unsigned char *contents = NULL;
7220 unsigned char *end, *p;
7221 int size;
333b20bb
GM
7222
7223 specified_file = image_spec_value (img->spec, QCfile, NULL);
63cec32f 7224 file = Qnil;
333b20bb 7225 GCPRO1 (file);
333b20bb 7226
63cec32f 7227 if (STRINGP (specified_file))
333b20bb 7228 {
63cec32f
GM
7229 file = x_find_image_file (specified_file);
7230 if (!STRINGP (file))
7231 {
7232 image_error ("Cannot find image file `%s'", specified_file, Qnil);
7233 UNGCPRO;
7234 return 0;
7235 }
333b20bb 7236
d5db4077 7237 contents = slurp_file (SDATA (file), &size);
63cec32f
GM
7238 if (contents == NULL)
7239 {
7240 image_error ("Error reading `%s'", file, Qnil);
7241 UNGCPRO;
7242 return 0;
7243 }
7244
7245 p = contents;
7246 end = contents + size;
7247 }
7248 else
333b20bb 7249 {
63cec32f
GM
7250 Lisp_Object data;
7251 data = image_spec_value (img->spec, QCdata, NULL);
d5db4077
KR
7252 p = SDATA (data);
7253 end = p + SBYTES (data);
333b20bb
GM
7254 }
7255
63cec32f
GM
7256 /* Check magic number. */
7257 if (end - p < 2 || *p++ != 'P')
333b20bb 7258 {
45158a91 7259 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
63cec32f
GM
7260 error:
7261 xfree (contents);
333b20bb
GM
7262 UNGCPRO;
7263 return 0;
7264 }
7265
63cec32f 7266 switch (*p++)
333b20bb
GM
7267 {
7268 case '1':
7269 raw_p = 0, type = PBM_MONO;
7270 break;
177c0ea7 7271
333b20bb
GM
7272 case '2':
7273 raw_p = 0, type = PBM_GRAY;
7274 break;
7275
7276 case '3':
7277 raw_p = 0, type = PBM_COLOR;
7278 break;
7279
7280 case '4':
7281 raw_p = 1, type = PBM_MONO;
7282 break;
177c0ea7 7283
333b20bb
GM
7284 case '5':
7285 raw_p = 1, type = PBM_GRAY;
7286 break;
177c0ea7 7287
333b20bb
GM
7288 case '6':
7289 raw_p = 1, type = PBM_COLOR;
7290 break;
7291
7292 default:
45158a91 7293 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
63cec32f 7294 goto error;
333b20bb
GM
7295 }
7296
7297 /* Read width, height, maximum color-component. Characters
7298 starting with `#' up to the end of a line are ignored. */
63cec32f
GM
7299 width = pbm_scan_number (&p, end);
7300 height = pbm_scan_number (&p, end);
333b20bb
GM
7301
7302 if (type != PBM_MONO)
7303 {
63cec32f 7304 max_color_idx = pbm_scan_number (&p, end);
333b20bb
GM
7305 if (raw_p && max_color_idx > 255)
7306 max_color_idx = 255;
7307 }
177c0ea7 7308
63cec32f
GM
7309 if (width < 0
7310 || height < 0
333b20bb 7311 || (type != PBM_MONO && max_color_idx < 0))
63cec32f 7312 goto error;
333b20bb 7313
45158a91 7314 if (!x_create_x_image_and_pixmap (f, width, height, 0,
333b20bb 7315 &ximg, &img->pixmap))
28c7826c 7316 goto error;
177c0ea7 7317
333b20bb
GM
7318 /* Initialize the color hash table. */
7319 init_color_table ();
7320
7321 if (type == PBM_MONO)
7322 {
7323 int c = 0, g;
be0b1fac
GM
7324 struct image_keyword fmt[PBM_LAST];
7325 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
7326 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
7327
7328 /* Parse the image specification. */
7329 bcopy (pbm_format, fmt, sizeof fmt);
7330 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
177c0ea7 7331
be0b1fac 7332 /* Get foreground and background colors, maybe allocate colors. */
6f1be3b9
GM
7333 if (fmt[PBM_FOREGROUND].count
7334 && STRINGP (fmt[PBM_FOREGROUND].value))
be0b1fac 7335 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
6f1be3b9
GM
7336 if (fmt[PBM_BACKGROUND].count
7337 && STRINGP (fmt[PBM_BACKGROUND].value))
f20a3b7a
MB
7338 {
7339 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
7340 img->background = bg;
7341 img->background_valid = 1;
7342 }
177c0ea7 7343
333b20bb
GM
7344 for (y = 0; y < height; ++y)
7345 for (x = 0; x < width; ++x)
7346 {
7347 if (raw_p)
7348 {
7349 if ((x & 7) == 0)
63cec32f 7350 c = *p++;
333b20bb
GM
7351 g = c & 0x80;
7352 c <<= 1;
7353 }
7354 else
63cec32f 7355 g = pbm_scan_number (&p, end);
333b20bb 7356
be0b1fac 7357 XPutPixel (ximg, x, y, g ? fg : bg);
333b20bb
GM
7358 }
7359 }
7360 else
7361 {
7362 for (y = 0; y < height; ++y)
7363 for (x = 0; x < width; ++x)
7364 {
7365 int r, g, b;
177c0ea7 7366
333b20bb 7367 if (type == PBM_GRAY)
63cec32f 7368 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
333b20bb
GM
7369 else if (raw_p)
7370 {
63cec32f
GM
7371 r = *p++;
7372 g = *p++;
7373 b = *p++;
333b20bb
GM
7374 }
7375 else
7376 {
63cec32f
GM
7377 r = pbm_scan_number (&p, end);
7378 g = pbm_scan_number (&p, end);
7379 b = pbm_scan_number (&p, end);
333b20bb 7380 }
177c0ea7 7381
333b20bb
GM
7382 if (r < 0 || g < 0 || b < 0)
7383 {
333b20bb
GM
7384 xfree (ximg->data);
7385 ximg->data = NULL;
7386 XDestroyImage (ximg);
45158a91
GM
7387 image_error ("Invalid pixel value in image `%s'",
7388 img->spec, Qnil);
63cec32f 7389 goto error;
333b20bb 7390 }
177c0ea7 7391
333b20bb
GM
7392 /* RGB values are now in the range 0..max_color_idx.
7393 Scale this to the range 0..0xffff supported by X. */
7394 r = (double) r * 65535 / max_color_idx;
7395 g = (double) g * 65535 / max_color_idx;
7396 b = (double) b * 65535 / max_color_idx;
7397 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
7398 }
7399 }
177c0ea7 7400
333b20bb
GM
7401 /* Store in IMG->colors the colors allocated for the image, and
7402 free the color table. */
7403 img->colors = colors_in_color_table (&img->ncolors);
7404 free_color_table ();
f20a3b7a
MB
7405
7406 /* Maybe fill in the background field while we have ximg handy. */
7407 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7408 IMAGE_BACKGROUND (img, f, ximg);
177c0ea7 7409
333b20bb
GM
7410 /* Put the image into a pixmap. */
7411 x_put_x_image (f, ximg, img->pixmap, width, height);
7412 x_destroy_x_image (ximg);
177c0ea7 7413
333b20bb
GM
7414 img->width = width;
7415 img->height = height;
7416
7417 UNGCPRO;
63cec32f 7418 xfree (contents);
333b20bb
GM
7419 return 1;
7420}
7421
7422
7423\f
7424/***********************************************************************
7425 PNG
7426 ***********************************************************************/
7427
7428#if HAVE_PNG
7429
7430#include <png.h>
7431
7432/* Function prototypes. */
7433
7434static int png_image_p P_ ((Lisp_Object object));
7435static int png_load P_ ((struct frame *f, struct image *img));
7436
7437/* The symbol `png' identifying images of this type. */
7438
7439Lisp_Object Qpng;
7440
7441/* Indices of image specification fields in png_format, below. */
7442
7443enum png_keyword_index
7444{
7445 PNG_TYPE,
63448a4d 7446 PNG_DATA,
333b20bb
GM
7447 PNG_FILE,
7448 PNG_ASCENT,
7449 PNG_MARGIN,
7450 PNG_RELIEF,
7451 PNG_ALGORITHM,
7452 PNG_HEURISTIC_MASK,
4a8e312c 7453 PNG_MASK,
f20a3b7a 7454 PNG_BACKGROUND,
333b20bb
GM
7455 PNG_LAST
7456};
7457
7458/* Vector of image_keyword structures describing the format
7459 of valid user-defined image specifications. */
7460
7461static struct image_keyword png_format[PNG_LAST] =
7462{
7463 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 7464 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 7465 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 7466 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 7467 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 7468 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 7469 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 7470 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f2f0a644 7471 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a 7472 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
7473};
7474
06482119 7475/* Structure describing the image type `png'. */
333b20bb
GM
7476
7477static struct image_type png_type =
7478{
7479 &Qpng,
7480 png_image_p,
7481 png_load,
7482 x_clear_image,
7483 NULL
7484};
7485
7486
7487/* Return non-zero if OBJECT is a valid PNG image specification. */
7488
7489static int
7490png_image_p (object)
7491 Lisp_Object object;
7492{
7493 struct image_keyword fmt[PNG_LAST];
7494 bcopy (png_format, fmt, sizeof fmt);
177c0ea7 7495
7c7ff7f5 7496 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
333b20bb 7497 return 0;
63448a4d 7498
63cec32f
GM
7499 /* Must specify either the :data or :file keyword. */
7500 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
333b20bb
GM
7501}
7502
7503
7504/* Error and warning handlers installed when the PNG library
7505 is initialized. */
7506
7507static void
7508my_png_error (png_ptr, msg)
7509 png_struct *png_ptr;
7510 char *msg;
7511{
7512 xassert (png_ptr != NULL);
7513 image_error ("PNG error: %s", build_string (msg), Qnil);
7514 longjmp (png_ptr->jmpbuf, 1);
7515}
7516
7517
7518static void
7519my_png_warning (png_ptr, msg)
7520 png_struct *png_ptr;
7521 char *msg;
7522{
7523 xassert (png_ptr != NULL);
7524 image_error ("PNG warning: %s", build_string (msg), Qnil);
7525}
7526
5ad6a5fb
GM
7527/* Memory source for PNG decoding. */
7528
63448a4d
WP
7529struct png_memory_storage
7530{
5ad6a5fb
GM
7531 unsigned char *bytes; /* The data */
7532 size_t len; /* How big is it? */
7533 int index; /* Where are we? */
63448a4d
WP
7534};
7535
5ad6a5fb
GM
7536
7537/* Function set as reader function when reading PNG image from memory.
7538 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
7539 bytes from the input to DATA. */
7540
63448a4d 7541static void
5ad6a5fb
GM
7542png_read_from_memory (png_ptr, data, length)
7543 png_structp png_ptr;
7544 png_bytep data;
7545 png_size_t length;
63448a4d 7546{
5ad6a5fb
GM
7547 struct png_memory_storage *tbr
7548 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
63448a4d 7549
5ad6a5fb
GM
7550 if (length > tbr->len - tbr->index)
7551 png_error (png_ptr, "Read error");
177c0ea7 7552
5ad6a5fb
GM
7553 bcopy (tbr->bytes + tbr->index, data, length);
7554 tbr->index = tbr->index + length;
63448a4d 7555}
333b20bb
GM
7556
7557/* Load PNG image IMG for use on frame F. Value is non-zero if
7558 successful. */
7559
7560static int
7561png_load (f, img)
7562 struct frame *f;
7563 struct image *img;
7564{
7565 Lisp_Object file, specified_file;
63448a4d 7566 Lisp_Object specified_data;
b6d7acec 7567 int x, y, i;
333b20bb
GM
7568 XImage *ximg, *mask_img = NULL;
7569 struct gcpro gcpro1;
7570 png_struct *png_ptr = NULL;
7571 png_info *info_ptr = NULL, *end_info = NULL;
8ec8a5ec 7572 FILE *volatile fp = NULL;
333b20bb 7573 png_byte sig[8];
8ec8a5ec
GM
7574 png_byte * volatile pixels = NULL;
7575 png_byte ** volatile rows = NULL;
333b20bb
GM
7576 png_uint_32 width, height;
7577 int bit_depth, color_type, interlace_type;
7578 png_byte channels;
7579 png_uint_32 row_bytes;
7580 int transparent_p;
333b20bb
GM
7581 double screen_gamma, image_gamma;
7582 int intent;
63448a4d 7583 struct png_memory_storage tbr; /* Data to be read */
333b20bb
GM
7584
7585 /* Find out what file to load. */
7586 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 7587 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
7588 file = Qnil;
7589 GCPRO1 (file);
333b20bb 7590
63448a4d 7591 if (NILP (specified_data))
5ad6a5fb
GM
7592 {
7593 file = x_find_image_file (specified_file);
7594 if (!STRINGP (file))
63448a4d 7595 {
45158a91 7596 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5ad6a5fb
GM
7597 UNGCPRO;
7598 return 0;
7599 }
333b20bb 7600
5ad6a5fb 7601 /* Open the image file. */
d5db4077 7602 fp = fopen (SDATA (file), "rb");
5ad6a5fb
GM
7603 if (!fp)
7604 {
45158a91 7605 image_error ("Cannot open image file `%s'", file, Qnil);
5ad6a5fb
GM
7606 UNGCPRO;
7607 fclose (fp);
7608 return 0;
7609 }
63448a4d 7610
5ad6a5fb
GM
7611 /* Check PNG signature. */
7612 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
7613 || !png_check_sig (sig, sizeof sig))
7614 {
45158a91 7615 image_error ("Not a PNG file: `%s'", file, Qnil);
5ad6a5fb
GM
7616 UNGCPRO;
7617 fclose (fp);
7618 return 0;
63448a4d 7619 }
5ad6a5fb 7620 }
63448a4d 7621 else
5ad6a5fb
GM
7622 {
7623 /* Read from memory. */
d5db4077
KR
7624 tbr.bytes = SDATA (specified_data);
7625 tbr.len = SBYTES (specified_data);
5ad6a5fb 7626 tbr.index = 0;
63448a4d 7627
5ad6a5fb
GM
7628 /* Check PNG signature. */
7629 if (tbr.len < sizeof sig
7630 || !png_check_sig (tbr.bytes, sizeof sig))
7631 {
45158a91 7632 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
5ad6a5fb
GM
7633 UNGCPRO;
7634 return 0;
63448a4d 7635 }
333b20bb 7636
5ad6a5fb
GM
7637 /* Need to skip past the signature. */
7638 tbr.bytes += sizeof (sig);
7639 }
7640
333b20bb
GM
7641 /* Initialize read and info structs for PNG lib. */
7642 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
7643 my_png_error, my_png_warning);
7644 if (!png_ptr)
7645 {
63448a4d 7646 if (fp) fclose (fp);
333b20bb
GM
7647 UNGCPRO;
7648 return 0;
7649 }
7650
7651 info_ptr = png_create_info_struct (png_ptr);
7652 if (!info_ptr)
7653 {
7654 png_destroy_read_struct (&png_ptr, NULL, NULL);
63448a4d 7655 if (fp) fclose (fp);
333b20bb
GM
7656 UNGCPRO;
7657 return 0;
7658 }
7659
7660 end_info = png_create_info_struct (png_ptr);
7661 if (!end_info)
7662 {
7663 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
63448a4d 7664 if (fp) fclose (fp);
333b20bb
GM
7665 UNGCPRO;
7666 return 0;
7667 }
7668
7669 /* Set error jump-back. We come back here when the PNG library
7670 detects an error. */
7671 if (setjmp (png_ptr->jmpbuf))
7672 {
7673 error:
7674 if (png_ptr)
7675 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
7676 xfree (pixels);
7677 xfree (rows);
63448a4d 7678 if (fp) fclose (fp);
333b20bb
GM
7679 UNGCPRO;
7680 return 0;
7681 }
7682
7683 /* Read image info. */
63448a4d 7684 if (!NILP (specified_data))
5ad6a5fb 7685 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
63448a4d 7686 else
5ad6a5fb 7687 png_init_io (png_ptr, fp);
63448a4d 7688
333b20bb
GM
7689 png_set_sig_bytes (png_ptr, sizeof sig);
7690 png_read_info (png_ptr, info_ptr);
7691 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
7692 &interlace_type, NULL, NULL);
7693
177c0ea7 7694 /* If image contains simply transparency data, we prefer to
333b20bb
GM
7695 construct a clipping mask. */
7696 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
7697 transparent_p = 1;
7698 else
7699 transparent_p = 0;
7700
177c0ea7 7701 /* This function is easier to write if we only have to handle
333b20bb
GM
7702 one data format: RGB or RGBA with 8 bits per channel. Let's
7703 transform other formats into that format. */
7704
7705 /* Strip more than 8 bits per channel. */
7706 if (bit_depth == 16)
7707 png_set_strip_16 (png_ptr);
7708
7709 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
7710 if available. */
7711 png_set_expand (png_ptr);
7712
7713 /* Convert grayscale images to RGB. */
177c0ea7 7714 if (color_type == PNG_COLOR_TYPE_GRAY
333b20bb
GM
7715 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
7716 png_set_gray_to_rgb (png_ptr);
7717
d4405ed7 7718 screen_gamma = (f->gamma ? 1 / f->gamma / 0.45455 : 2.2);
333b20bb 7719
bfa261c0 7720#if 0 /* Avoid double gamma correction for PNG images. */
333b20bb 7721 /* Tell the PNG lib to handle gamma correction for us. */
6c1aa34d 7722#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
333b20bb 7723 if (png_get_sRGB (png_ptr, info_ptr, &intent))
d4405ed7
RS
7724 /* The libpng documentation says this is right in this case. */
7725 png_set_gamma (png_ptr, screen_gamma, 0.45455);
6c1aa34d
GM
7726 else
7727#endif
7728 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
333b20bb
GM
7729 /* Image contains gamma information. */
7730 png_set_gamma (png_ptr, screen_gamma, image_gamma);
7731 else
d4405ed7
RS
7732 /* Use the standard default for the image gamma. */
7733 png_set_gamma (png_ptr, screen_gamma, 0.45455);
7273d100 7734#endif /* if 0 */
333b20bb
GM
7735
7736 /* Handle alpha channel by combining the image with a background
7737 color. Do this only if a real alpha channel is supplied. For
7738 simple transparency, we prefer a clipping mask. */
7739 if (!transparent_p)
7740 {
f20a3b7a
MB
7741 png_color_16 *image_bg;
7742 Lisp_Object specified_bg
7743 = image_spec_value (img->spec, QCbackground, NULL);
7744
f2f0a644 7745 if (STRINGP (specified_bg))
f20a3b7a
MB
7746 /* The user specified `:background', use that. */
7747 {
7748 XColor color;
d5db4077 7749 if (x_defined_color (f, SDATA (specified_bg), &color, 0))
f20a3b7a
MB
7750 {
7751 png_color_16 user_bg;
7752
7753 bzero (&user_bg, sizeof user_bg);
7754 user_bg.red = color.red;
7755 user_bg.green = color.green;
7756 user_bg.blue = color.blue;
333b20bb 7757
f20a3b7a
MB
7758 png_set_background (png_ptr, &user_bg,
7759 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
7760 }
7761 }
7762 else if (png_get_bKGD (png_ptr, info_ptr, &image_bg))
177c0ea7 7763 /* Image contains a background color with which to
333b20bb 7764 combine the image. */
f20a3b7a 7765 png_set_background (png_ptr, image_bg,
333b20bb
GM
7766 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
7767 else
7768 {
7769 /* Image does not contain a background color with which
177c0ea7 7770 to combine the image data via an alpha channel. Use
333b20bb
GM
7771 the frame's background instead. */
7772 XColor color;
7773 Colormap cmap;
7774 png_color_16 frame_background;
7775
9b2956e2 7776 cmap = FRAME_X_COLORMAP (f);
333b20bb 7777 color.pixel = FRAME_BACKGROUND_PIXEL (f);
a31fedb7 7778 x_query_color (f, &color);
333b20bb
GM
7779
7780 bzero (&frame_background, sizeof frame_background);
7781 frame_background.red = color.red;
7782 frame_background.green = color.green;
7783 frame_background.blue = color.blue;
7784
7785 png_set_background (png_ptr, &frame_background,
7786 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
7787 }
7788 }
7789
7790 /* Update info structure. */
7791 png_read_update_info (png_ptr, info_ptr);
7792
7793 /* Get number of channels. Valid values are 1 for grayscale images
7794 and images with a palette, 2 for grayscale images with transparency
7795 information (alpha channel), 3 for RGB images, and 4 for RGB
7796 images with alpha channel, i.e. RGBA. If conversions above were
7797 sufficient we should only have 3 or 4 channels here. */
7798 channels = png_get_channels (png_ptr, info_ptr);
7799 xassert (channels == 3 || channels == 4);
7800
7801 /* Number of bytes needed for one row of the image. */
7802 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
7803
7804 /* Allocate memory for the image. */
7805 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
7806 rows = (png_byte **) xmalloc (height * sizeof *rows);
7807 for (i = 0; i < height; ++i)
7808 rows[i] = pixels + i * row_bytes;
7809
7810 /* Read the entire image. */
7811 png_read_image (png_ptr, rows);
7812 png_read_end (png_ptr, info_ptr);
5ad6a5fb
GM
7813 if (fp)
7814 {
7815 fclose (fp);
7816 fp = NULL;
7817 }
177c0ea7 7818
333b20bb 7819 /* Create the X image and pixmap. */
45158a91 7820 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
333b20bb 7821 &img->pixmap))
28c7826c 7822 goto error;
177c0ea7 7823
333b20bb
GM
7824 /* Create an image and pixmap serving as mask if the PNG image
7825 contains an alpha channel. */
7826 if (channels == 4
7827 && !transparent_p
45158a91 7828 && !x_create_x_image_and_pixmap (f, width, height, 1,
333b20bb
GM
7829 &mask_img, &img->mask))
7830 {
7831 x_destroy_x_image (ximg);
7832 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
dd00328a 7833 img->pixmap = None;
333b20bb
GM
7834 goto error;
7835 }
7836
7837 /* Fill the X image and mask from PNG data. */
7838 init_color_table ();
7839
7840 for (y = 0; y < height; ++y)
7841 {
7842 png_byte *p = rows[y];
7843
7844 for (x = 0; x < width; ++x)
7845 {
7846 unsigned r, g, b;
7847
7848 r = *p++ << 8;
7849 g = *p++ << 8;
7850 b = *p++ << 8;
7851 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
7852
7853 /* An alpha channel, aka mask channel, associates variable
177c0ea7
JB
7854 transparency with an image. Where other image formats
7855 support binary transparency---fully transparent or fully
333b20bb
GM
7856 opaque---PNG allows up to 254 levels of partial transparency.
7857 The PNG library implements partial transparency by combining
7858 the image with a specified background color.
7859
7860 I'm not sure how to handle this here nicely: because the
7861 background on which the image is displayed may change, for
177c0ea7
JB
7862 real alpha channel support, it would be necessary to create
7863 a new image for each possible background.
333b20bb
GM
7864
7865 What I'm doing now is that a mask is created if we have
7866 boolean transparency information. Otherwise I'm using
7867 the frame's background color to combine the image with. */
7868
7869 if (channels == 4)
7870 {
7871 if (mask_img)
7872 XPutPixel (mask_img, x, y, *p > 0);
7873 ++p;
7874 }
7875 }
7876 }
7877
f20a3b7a
MB
7878 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
7879 /* Set IMG's background color from the PNG image, unless the user
7880 overrode it. */
7881 {
7882 png_color_16 *bg;
7883 if (png_get_bKGD (png_ptr, info_ptr, &bg))
7884 {
f2f0a644 7885 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
f20a3b7a
MB
7886 img->background_valid = 1;
7887 }
7888 }
7889
333b20bb
GM
7890 /* Remember colors allocated for this image. */
7891 img->colors = colors_in_color_table (&img->ncolors);
7892 free_color_table ();
7893
7894 /* Clean up. */
7895 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
7896 xfree (rows);
7897 xfree (pixels);
7898
7899 img->width = width;
7900 img->height = height;
7901
f20a3b7a
MB
7902 /* Maybe fill in the background field while we have ximg handy. */
7903 IMAGE_BACKGROUND (img, f, ximg);
7904
333b20bb
GM
7905 /* Put the image into the pixmap, then free the X image and its buffer. */
7906 x_put_x_image (f, ximg, img->pixmap, width, height);
7907 x_destroy_x_image (ximg);
7908
7909 /* Same for the mask. */
7910 if (mask_img)
7911 {
f20a3b7a
MB
7912 /* Fill in the background_transparent field while we have the mask
7913 handy. */
7914 image_background_transparent (img, f, mask_img);
7915
333b20bb
GM
7916 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
7917 x_destroy_x_image (mask_img);
7918 }
7919
333b20bb
GM
7920 UNGCPRO;
7921 return 1;
7922}
7923
7924#endif /* HAVE_PNG != 0 */
7925
7926
7927\f
7928/***********************************************************************
7929 JPEG
7930 ***********************************************************************/
7931
7932#if HAVE_JPEG
7933
ba06aba4
GM
7934/* Work around a warning about HAVE_STDLIB_H being redefined in
7935 jconfig.h. */
7936#ifdef HAVE_STDLIB_H
7937#define HAVE_STDLIB_H_1
7938#undef HAVE_STDLIB_H
7939#endif /* HAVE_STLIB_H */
7940
333b20bb
GM
7941#include <jpeglib.h>
7942#include <jerror.h>
7943#include <setjmp.h>
7944
ba06aba4
GM
7945#ifdef HAVE_STLIB_H_1
7946#define HAVE_STDLIB_H 1
7947#endif
7948
333b20bb
GM
7949static int jpeg_image_p P_ ((Lisp_Object object));
7950static int jpeg_load P_ ((struct frame *f, struct image *img));
7951
7952/* The symbol `jpeg' identifying images of this type. */
7953
7954Lisp_Object Qjpeg;
7955
7956/* Indices of image specification fields in gs_format, below. */
7957
7958enum jpeg_keyword_index
7959{
7960 JPEG_TYPE,
8e39770a 7961 JPEG_DATA,
333b20bb
GM
7962 JPEG_FILE,
7963 JPEG_ASCENT,
7964 JPEG_MARGIN,
7965 JPEG_RELIEF,
7966 JPEG_ALGORITHM,
7967 JPEG_HEURISTIC_MASK,
4a8e312c 7968 JPEG_MASK,
f20a3b7a 7969 JPEG_BACKGROUND,
333b20bb
GM
7970 JPEG_LAST
7971};
7972
7973/* Vector of image_keyword structures describing the format
7974 of valid user-defined image specifications. */
7975
7976static struct image_keyword jpeg_format[JPEG_LAST] =
7977{
7978 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 7979 {":data", IMAGE_STRING_VALUE, 0},
8e39770a 7980 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 7981 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 7982 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 7983 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 7984 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 7985 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
7986 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7987 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
7988};
7989
7990/* Structure describing the image type `jpeg'. */
7991
7992static struct image_type jpeg_type =
7993{
7994 &Qjpeg,
7995 jpeg_image_p,
7996 jpeg_load,
7997 x_clear_image,
7998 NULL
7999};
8000
8001
8002/* Return non-zero if OBJECT is a valid JPEG image specification. */
8003
8004static int
8005jpeg_image_p (object)
8006 Lisp_Object object;
8007{
8008 struct image_keyword fmt[JPEG_LAST];
177c0ea7 8009
333b20bb 8010 bcopy (jpeg_format, fmt, sizeof fmt);
177c0ea7 8011
7c7ff7f5 8012 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
333b20bb 8013 return 0;
8e39770a 8014
63cec32f
GM
8015 /* Must specify either the :data or :file keyword. */
8016 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
333b20bb
GM
8017}
8018
8e39770a 8019
333b20bb
GM
8020struct my_jpeg_error_mgr
8021{
8022 struct jpeg_error_mgr pub;
8023 jmp_buf setjmp_buffer;
8024};
8025
e3130015 8026
333b20bb
GM
8027static void
8028my_error_exit (cinfo)
8029 j_common_ptr cinfo;
8030{
8031 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
8032 longjmp (mgr->setjmp_buffer, 1);
8033}
8034
e3130015 8035
8e39770a
GM
8036/* Init source method for JPEG data source manager. Called by
8037 jpeg_read_header() before any data is actually read. See
8038 libjpeg.doc from the JPEG lib distribution. */
8039
8040static void
8041our_init_source (cinfo)
8042 j_decompress_ptr cinfo;
8043{
8044}
8045
8046
8047/* Fill input buffer method for JPEG data source manager. Called
8048 whenever more data is needed. We read the whole image in one step,
8049 so this only adds a fake end of input marker at the end. */
8050
8051static boolean
8052our_fill_input_buffer (cinfo)
8053 j_decompress_ptr cinfo;
8054{
8055 /* Insert a fake EOI marker. */
8056 struct jpeg_source_mgr *src = cinfo->src;
8057 static JOCTET buffer[2];
8058
8059 buffer[0] = (JOCTET) 0xFF;
8060 buffer[1] = (JOCTET) JPEG_EOI;
8061
8062 src->next_input_byte = buffer;
8063 src->bytes_in_buffer = 2;
8064 return TRUE;
8065}
8066
8067
8068/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
8069 is the JPEG data source manager. */
8070
8071static void
8072our_skip_input_data (cinfo, num_bytes)
8073 j_decompress_ptr cinfo;
8074 long num_bytes;
8075{
8076 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
8077
8078 if (src)
8079 {
8080 if (num_bytes > src->bytes_in_buffer)
5ad6a5fb 8081 ERREXIT (cinfo, JERR_INPUT_EOF);
177c0ea7 8082
8e39770a
GM
8083 src->bytes_in_buffer -= num_bytes;
8084 src->next_input_byte += num_bytes;
8085 }
8086}
8087
8088
8089/* Method to terminate data source. Called by
8090 jpeg_finish_decompress() after all data has been processed. */
8091
8092static void
8093our_term_source (cinfo)
8094 j_decompress_ptr cinfo;
8095{
8096}
8097
8098
8099/* Set up the JPEG lib for reading an image from DATA which contains
8100 LEN bytes. CINFO is the decompression info structure created for
8101 reading the image. */
8102
8103static void
8104jpeg_memory_src (cinfo, data, len)
8105 j_decompress_ptr cinfo;
8106 JOCTET *data;
8107 unsigned int len;
8108{
8109 struct jpeg_source_mgr *src;
8110
8111 if (cinfo->src == NULL)
8112 {
8113 /* First time for this JPEG object? */
8114 cinfo->src = (struct jpeg_source_mgr *)
8115 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
8116 sizeof (struct jpeg_source_mgr));
8117 src = (struct jpeg_source_mgr *) cinfo->src;
8118 src->next_input_byte = data;
8119 }
177c0ea7 8120
8e39770a
GM
8121 src = (struct jpeg_source_mgr *) cinfo->src;
8122 src->init_source = our_init_source;
8123 src->fill_input_buffer = our_fill_input_buffer;
8124 src->skip_input_data = our_skip_input_data;
8125 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
8126 src->term_source = our_term_source;
8127 src->bytes_in_buffer = len;
8128 src->next_input_byte = data;
8129}
8130
5ad6a5fb 8131
333b20bb
GM
8132/* Load image IMG for use on frame F. Patterned after example.c
8133 from the JPEG lib. */
8134
177c0ea7 8135static int
333b20bb
GM
8136jpeg_load (f, img)
8137 struct frame *f;
8138 struct image *img;
8139{
8140 struct jpeg_decompress_struct cinfo;
8141 struct my_jpeg_error_mgr mgr;
8142 Lisp_Object file, specified_file;
8e39770a 8143 Lisp_Object specified_data;
8ec8a5ec 8144 FILE * volatile fp = NULL;
333b20bb
GM
8145 JSAMPARRAY buffer;
8146 int row_stride, x, y;
8147 XImage *ximg = NULL;
b6d7acec 8148 int rc;
333b20bb
GM
8149 unsigned long *colors;
8150 int width, height;
8151 struct gcpro gcpro1;
8152
8153 /* Open the JPEG file. */
8154 specified_file = image_spec_value (img->spec, QCfile, NULL);
8e39770a 8155 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
8156 file = Qnil;
8157 GCPRO1 (file);
8e39770a 8158
8e39770a 8159 if (NILP (specified_data))
333b20bb 8160 {
8e39770a 8161 file = x_find_image_file (specified_file);
8e39770a
GM
8162 if (!STRINGP (file))
8163 {
45158a91 8164 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8e39770a
GM
8165 UNGCPRO;
8166 return 0;
8167 }
177c0ea7 8168
d5db4077 8169 fp = fopen (SDATA (file), "r");
8e39770a
GM
8170 if (fp == NULL)
8171 {
8172 image_error ("Cannot open `%s'", file, Qnil);
8173 UNGCPRO;
8174 return 0;
8175 }
333b20bb
GM
8176 }
8177
5ad6a5fb
GM
8178 /* Customize libjpeg's error handling to call my_error_exit when an
8179 error is detected. This function will perform a longjmp. */
333b20bb 8180 cinfo.err = jpeg_std_error (&mgr.pub);
14358466 8181 mgr.pub.error_exit = my_error_exit;
177c0ea7 8182
333b20bb
GM
8183 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
8184 {
5ad6a5fb
GM
8185 if (rc == 1)
8186 {
8187 /* Called from my_error_exit. Display a JPEG error. */
8188 char buffer[JMSG_LENGTH_MAX];
8189 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
45158a91 8190 image_error ("Error reading JPEG image `%s': %s", img->spec,
5ad6a5fb
GM
8191 build_string (buffer));
8192 }
177c0ea7 8193
333b20bb 8194 /* Close the input file and destroy the JPEG object. */
5ad6a5fb 8195 if (fp)
8ec8a5ec 8196 fclose ((FILE *) fp);
333b20bb
GM
8197 jpeg_destroy_decompress (&cinfo);
8198
5ad6a5fb
GM
8199 /* If we already have an XImage, free that. */
8200 x_destroy_x_image (ximg);
333b20bb 8201
5ad6a5fb
GM
8202 /* Free pixmap and colors. */
8203 x_clear_image (f, img);
177c0ea7 8204
5ad6a5fb
GM
8205 UNGCPRO;
8206 return 0;
333b20bb
GM
8207 }
8208
8209 /* Create the JPEG decompression object. Let it read from fp.
63448a4d 8210 Read the JPEG image header. */
333b20bb 8211 jpeg_create_decompress (&cinfo);
8e39770a
GM
8212
8213 if (NILP (specified_data))
8ec8a5ec 8214 jpeg_stdio_src (&cinfo, (FILE *) fp);
8e39770a 8215 else
d5db4077
KR
8216 jpeg_memory_src (&cinfo, SDATA (specified_data),
8217 SBYTES (specified_data));
63448a4d 8218
333b20bb
GM
8219 jpeg_read_header (&cinfo, TRUE);
8220
8221 /* Customize decompression so that color quantization will be used.
63448a4d 8222 Start decompression. */
333b20bb
GM
8223 cinfo.quantize_colors = TRUE;
8224 jpeg_start_decompress (&cinfo);
8225 width = img->width = cinfo.output_width;
8226 height = img->height = cinfo.output_height;
8227
333b20bb 8228 /* Create X image and pixmap. */
45158a91 8229 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
28c7826c 8230 longjmp (mgr.setjmp_buffer, 2);
333b20bb
GM
8231
8232 /* Allocate colors. When color quantization is used,
5ad6a5fb
GM
8233 cinfo.actual_number_of_colors has been set with the number of
8234 colors generated, and cinfo.colormap is a two-dimensional array
8235 of color indices in the range 0..cinfo.actual_number_of_colors.
8236 No more than 255 colors will be generated. */
333b20bb 8237 {
5ad6a5fb
GM
8238 int i, ir, ig, ib;
8239
8240 if (cinfo.out_color_components > 2)
8241 ir = 0, ig = 1, ib = 2;
8242 else if (cinfo.out_color_components > 1)
8243 ir = 0, ig = 1, ib = 0;
8244 else
8245 ir = 0, ig = 0, ib = 0;
8246
8247 /* Use the color table mechanism because it handles colors that
8248 cannot be allocated nicely. Such colors will be replaced with
8249 a default color, and we don't have to care about which colors
8250 can be freed safely, and which can't. */
8251 init_color_table ();
8252 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
8253 * sizeof *colors);
177c0ea7 8254
5ad6a5fb
GM
8255 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
8256 {
8257 /* Multiply RGB values with 255 because X expects RGB values
8258 in the range 0..0xffff. */
8259 int r = cinfo.colormap[ir][i] << 8;
8260 int g = cinfo.colormap[ig][i] << 8;
8261 int b = cinfo.colormap[ib][i] << 8;
8262 colors[i] = lookup_rgb_color (f, r, g, b);
8263 }
333b20bb 8264
5ad6a5fb
GM
8265 /* Remember those colors actually allocated. */
8266 img->colors = colors_in_color_table (&img->ncolors);
8267 free_color_table ();
333b20bb
GM
8268 }
8269
8270 /* Read pixels. */
8271 row_stride = width * cinfo.output_components;
8272 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
5ad6a5fb 8273 row_stride, 1);
333b20bb
GM
8274 for (y = 0; y < height; ++y)
8275 {
5ad6a5fb
GM
8276 jpeg_read_scanlines (&cinfo, buffer, 1);
8277 for (x = 0; x < cinfo.output_width; ++x)
8278 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
333b20bb
GM
8279 }
8280
8281 /* Clean up. */
8282 jpeg_finish_decompress (&cinfo);
8283 jpeg_destroy_decompress (&cinfo);
5ad6a5fb 8284 if (fp)
8ec8a5ec 8285 fclose ((FILE *) fp);
f20a3b7a
MB
8286
8287 /* Maybe fill in the background field while we have ximg handy. */
8288 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8289 IMAGE_BACKGROUND (img, f, ximg);
177c0ea7 8290
333b20bb
GM
8291 /* Put the image into the pixmap. */
8292 x_put_x_image (f, ximg, img->pixmap, width, height);
8293 x_destroy_x_image (ximg);
333b20bb
GM
8294 UNGCPRO;
8295 return 1;
8296}
8297
8298#endif /* HAVE_JPEG */
8299
8300
8301\f
8302/***********************************************************************
8303 TIFF
8304 ***********************************************************************/
8305
8306#if HAVE_TIFF
8307
cf4790ad 8308#include <tiffio.h>
333b20bb
GM
8309
8310static int tiff_image_p P_ ((Lisp_Object object));
8311static int tiff_load P_ ((struct frame *f, struct image *img));
8312
8313/* The symbol `tiff' identifying images of this type. */
8314
8315Lisp_Object Qtiff;
8316
8317/* Indices of image specification fields in tiff_format, below. */
8318
8319enum tiff_keyword_index
8320{
8321 TIFF_TYPE,
63448a4d 8322 TIFF_DATA,
333b20bb
GM
8323 TIFF_FILE,
8324 TIFF_ASCENT,
8325 TIFF_MARGIN,
8326 TIFF_RELIEF,
8327 TIFF_ALGORITHM,
8328 TIFF_HEURISTIC_MASK,
4a8e312c 8329 TIFF_MASK,
f20a3b7a 8330 TIFF_BACKGROUND,
333b20bb
GM
8331 TIFF_LAST
8332};
8333
8334/* Vector of image_keyword structures describing the format
8335 of valid user-defined image specifications. */
8336
8337static struct image_keyword tiff_format[TIFF_LAST] =
8338{
8339 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 8340 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 8341 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 8342 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 8343 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 8344 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 8345 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 8346 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
8347 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
8348 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
8349};
8350
8351/* Structure describing the image type `tiff'. */
8352
8353static struct image_type tiff_type =
8354{
8355 &Qtiff,
8356 tiff_image_p,
8357 tiff_load,
8358 x_clear_image,
8359 NULL
8360};
8361
8362
8363/* Return non-zero if OBJECT is a valid TIFF image specification. */
8364
8365static int
8366tiff_image_p (object)
8367 Lisp_Object object;
8368{
8369 struct image_keyword fmt[TIFF_LAST];
8370 bcopy (tiff_format, fmt, sizeof fmt);
177c0ea7 8371
7c7ff7f5 8372 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
333b20bb 8373 return 0;
177c0ea7 8374
63cec32f
GM
8375 /* Must specify either the :data or :file keyword. */
8376 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
333b20bb
GM
8377}
8378
5ad6a5fb
GM
8379
8380/* Reading from a memory buffer for TIFF images Based on the PNG
8381 memory source, but we have to provide a lot of extra functions.
8382 Blah.
63448a4d
WP
8383
8384 We really only need to implement read and seek, but I am not
8385 convinced that the TIFF library is smart enough not to destroy
8386 itself if we only hand it the function pointers we need to
5ad6a5fb
GM
8387 override. */
8388
8389typedef struct
8390{
63448a4d
WP
8391 unsigned char *bytes;
8392 size_t len;
8393 int index;
5ad6a5fb
GM
8394}
8395tiff_memory_source;
63448a4d 8396
e3130015 8397
5ad6a5fb
GM
8398static size_t
8399tiff_read_from_memory (data, buf, size)
8400 thandle_t data;
8401 tdata_t buf;
8402 tsize_t size;
63448a4d 8403{
5ad6a5fb 8404 tiff_memory_source *src = (tiff_memory_source *) data;
63448a4d
WP
8405
8406 if (size > src->len - src->index)
5ad6a5fb
GM
8407 return (size_t) -1;
8408 bcopy (src->bytes + src->index, buf, size);
63448a4d
WP
8409 src->index += size;
8410 return size;
8411}
8412
e3130015 8413
5ad6a5fb
GM
8414static size_t
8415tiff_write_from_memory (data, buf, size)
8416 thandle_t data;
8417 tdata_t buf;
8418 tsize_t size;
63448a4d
WP
8419{
8420 return (size_t) -1;
8421}
8422
e3130015 8423
5ad6a5fb
GM
8424static toff_t
8425tiff_seek_in_memory (data, off, whence)
8426 thandle_t data;
8427 toff_t off;
8428 int whence;
63448a4d 8429{
5ad6a5fb 8430 tiff_memory_source *src = (tiff_memory_source *) data;
63448a4d
WP
8431 int idx;
8432
8433 switch (whence)
5ad6a5fb
GM
8434 {
8435 case SEEK_SET: /* Go from beginning of source. */
8436 idx = off;
8437 break;
177c0ea7 8438
5ad6a5fb
GM
8439 case SEEK_END: /* Go from end of source. */
8440 idx = src->len + off;
8441 break;
177c0ea7 8442
5ad6a5fb
GM
8443 case SEEK_CUR: /* Go from current position. */
8444 idx = src->index + off;
8445 break;
177c0ea7 8446
5ad6a5fb
GM
8447 default: /* Invalid `whence'. */
8448 return -1;
8449 }
177c0ea7 8450
5ad6a5fb
GM
8451 if (idx > src->len || idx < 0)
8452 return -1;
177c0ea7 8453
63448a4d
WP
8454 src->index = idx;
8455 return src->index;
8456}
8457
e3130015 8458
5ad6a5fb
GM
8459static int
8460tiff_close_memory (data)
8461 thandle_t data;
63448a4d
WP
8462{
8463 /* NOOP */
5ad6a5fb 8464 return 0;
63448a4d
WP
8465}
8466
e3130015 8467
5ad6a5fb
GM
8468static int
8469tiff_mmap_memory (data, pbase, psize)
8470 thandle_t data;
8471 tdata_t *pbase;
8472 toff_t *psize;
63448a4d
WP
8473{
8474 /* It is already _IN_ memory. */
5ad6a5fb 8475 return 0;
63448a4d
WP
8476}
8477
e3130015 8478
5ad6a5fb
GM
8479static void
8480tiff_unmap_memory (data, base, size)
8481 thandle_t data;
8482 tdata_t base;
8483 toff_t size;
63448a4d
WP
8484{
8485 /* We don't need to do this. */
63448a4d
WP
8486}
8487
e3130015 8488
5ad6a5fb
GM
8489static toff_t
8490tiff_size_of_memory (data)
8491 thandle_t data;
63448a4d 8492{
5ad6a5fb 8493 return ((tiff_memory_source *) data)->len;
63448a4d 8494}
333b20bb 8495
e3130015 8496
c6892044
GM
8497static void
8498tiff_error_handler (title, format, ap)
8499 const char *title, *format;
8500 va_list ap;
8501{
8502 char buf[512];
8503 int len;
177c0ea7 8504
c6892044
GM
8505 len = sprintf (buf, "TIFF error: %s ", title);
8506 vsprintf (buf + len, format, ap);
8507 add_to_log (buf, Qnil, Qnil);
8508}
8509
8510
8511static void
8512tiff_warning_handler (title, format, ap)
8513 const char *title, *format;
8514 va_list ap;
8515{
8516 char buf[512];
8517 int len;
177c0ea7 8518
c6892044
GM
8519 len = sprintf (buf, "TIFF warning: %s ", title);
8520 vsprintf (buf + len, format, ap);
8521 add_to_log (buf, Qnil, Qnil);
8522}
8523
8524
333b20bb
GM
8525/* Load TIFF image IMG for use on frame F. Value is non-zero if
8526 successful. */
8527
8528static int
8529tiff_load (f, img)
8530 struct frame *f;
8531 struct image *img;
8532{
8533 Lisp_Object file, specified_file;
63448a4d 8534 Lisp_Object specified_data;
333b20bb
GM
8535 TIFF *tiff;
8536 int width, height, x, y;
8537 uint32 *buf;
8538 int rc;
8539 XImage *ximg;
8540 struct gcpro gcpro1;
63448a4d 8541 tiff_memory_source memsrc;
333b20bb
GM
8542
8543 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 8544 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
8545 file = Qnil;
8546 GCPRO1 (file);
63448a4d 8547
c6892044
GM
8548 TIFFSetErrorHandler (tiff_error_handler);
8549 TIFFSetWarningHandler (tiff_warning_handler);
8550
63448a4d 8551 if (NILP (specified_data))
5ad6a5fb
GM
8552 {
8553 /* Read from a file */
8554 file = x_find_image_file (specified_file);
8555 if (!STRINGP (file))
63448a4d 8556 {
45158a91 8557 image_error ("Cannot find image file `%s'", file, Qnil);
5ad6a5fb
GM
8558 UNGCPRO;
8559 return 0;
8560 }
177c0ea7 8561
5ad6a5fb 8562 /* Try to open the image file. */
d5db4077 8563 tiff = TIFFOpen (SDATA (file), "r");
5ad6a5fb
GM
8564 if (tiff == NULL)
8565 {
8566 image_error ("Cannot open `%s'", file, Qnil);
8567 UNGCPRO;
8568 return 0;
63448a4d 8569 }
5ad6a5fb 8570 }
63448a4d 8571 else
5ad6a5fb
GM
8572 {
8573 /* Memory source! */
d5db4077
KR
8574 memsrc.bytes = SDATA (specified_data);
8575 memsrc.len = SBYTES (specified_data);
5ad6a5fb
GM
8576 memsrc.index = 0;
8577
8578 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
8579 (TIFFReadWriteProc) tiff_read_from_memory,
8580 (TIFFReadWriteProc) tiff_write_from_memory,
8581 tiff_seek_in_memory,
8582 tiff_close_memory,
8583 tiff_size_of_memory,
8584 tiff_mmap_memory,
8585 tiff_unmap_memory);
8586
8587 if (!tiff)
63448a4d 8588 {
45158a91 8589 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
5ad6a5fb
GM
8590 UNGCPRO;
8591 return 0;
63448a4d 8592 }
5ad6a5fb 8593 }
333b20bb
GM
8594
8595 /* Get width and height of the image, and allocate a raster buffer
8596 of width x height 32-bit values. */
8597 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
8598 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
8599 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
177c0ea7 8600
333b20bb
GM
8601 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
8602 TIFFClose (tiff);
8603 if (!rc)
8604 {
45158a91 8605 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
333b20bb
GM
8606 xfree (buf);
8607 UNGCPRO;
8608 return 0;
8609 }
8610
333b20bb 8611 /* Create the X image and pixmap. */
45158a91 8612 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
333b20bb 8613 {
333b20bb
GM
8614 xfree (buf);
8615 UNGCPRO;
8616 return 0;
8617 }
8618
8619 /* Initialize the color table. */
8620 init_color_table ();
8621
8622 /* Process the pixel raster. Origin is in the lower-left corner. */
8623 for (y = 0; y < height; ++y)
8624 {
8625 uint32 *row = buf + y * width;
177c0ea7 8626
333b20bb
GM
8627 for (x = 0; x < width; ++x)
8628 {
8629 uint32 abgr = row[x];
8630 int r = TIFFGetR (abgr) << 8;
8631 int g = TIFFGetG (abgr) << 8;
8632 int b = TIFFGetB (abgr) << 8;
177c0ea7 8633 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
333b20bb
GM
8634 }
8635 }
8636
8637 /* Remember the colors allocated for the image. Free the color table. */
8638 img->colors = colors_in_color_table (&img->ncolors);
8639 free_color_table ();
177c0ea7 8640
f20a3b7a
MB
8641 img->width = width;
8642 img->height = height;
8643
8644 /* Maybe fill in the background field while we have ximg handy. */
8645 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8646 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
8647
8648 /* Put the image into the pixmap, then free the X image and its buffer. */
8649 x_put_x_image (f, ximg, img->pixmap, width, height);
8650 x_destroy_x_image (ximg);
8651 xfree (buf);
333b20bb
GM
8652
8653 UNGCPRO;
8654 return 1;
8655}
8656
8657#endif /* HAVE_TIFF != 0 */
8658
8659
8660\f
8661/***********************************************************************
8662 GIF
8663 ***********************************************************************/
8664
8665#if HAVE_GIF
8666
8667#include <gif_lib.h>
8668
8669static int gif_image_p P_ ((Lisp_Object object));
8670static int gif_load P_ ((struct frame *f, struct image *img));
8671
8672/* The symbol `gif' identifying images of this type. */
8673
8674Lisp_Object Qgif;
8675
8676/* Indices of image specification fields in gif_format, below. */
8677
8678enum gif_keyword_index
8679{
8680 GIF_TYPE,
63448a4d 8681 GIF_DATA,
333b20bb
GM
8682 GIF_FILE,
8683 GIF_ASCENT,
8684 GIF_MARGIN,
8685 GIF_RELIEF,
8686 GIF_ALGORITHM,
8687 GIF_HEURISTIC_MASK,
4a8e312c 8688 GIF_MASK,
333b20bb 8689 GIF_IMAGE,
f20a3b7a 8690 GIF_BACKGROUND,
333b20bb
GM
8691 GIF_LAST
8692};
8693
8694/* Vector of image_keyword structures describing the format
8695 of valid user-defined image specifications. */
8696
8697static struct image_keyword gif_format[GIF_LAST] =
8698{
8699 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 8700 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 8701 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 8702 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 8703 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 8704 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 8705 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
333b20bb 8706 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 8707 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f2f0a644 8708 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
f20a3b7a 8709 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
8710};
8711
8712/* Structure describing the image type `gif'. */
8713
8714static struct image_type gif_type =
8715{
8716 &Qgif,
8717 gif_image_p,
8718 gif_load,
8719 x_clear_image,
8720 NULL
8721};
8722
e3130015 8723
333b20bb
GM
8724/* Return non-zero if OBJECT is a valid GIF image specification. */
8725
8726static int
8727gif_image_p (object)
8728 Lisp_Object object;
8729{
8730 struct image_keyword fmt[GIF_LAST];
8731 bcopy (gif_format, fmt, sizeof fmt);
177c0ea7 8732
7c7ff7f5 8733 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
333b20bb 8734 return 0;
177c0ea7 8735
63cec32f
GM
8736 /* Must specify either the :data or :file keyword. */
8737 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
333b20bb
GM
8738}
8739
e3130015 8740
63448a4d
WP
8741/* Reading a GIF image from memory
8742 Based on the PNG memory stuff to a certain extent. */
8743
5ad6a5fb
GM
8744typedef struct
8745{
63448a4d
WP
8746 unsigned char *bytes;
8747 size_t len;
8748 int index;
5ad6a5fb
GM
8749}
8750gif_memory_source;
63448a4d 8751
e3130015 8752
f036834a
GM
8753/* Make the current memory source available to gif_read_from_memory.
8754 It's done this way because not all versions of libungif support
8755 a UserData field in the GifFileType structure. */
8756static gif_memory_source *current_gif_memory_src;
8757
5ad6a5fb
GM
8758static int
8759gif_read_from_memory (file, buf, len)
8760 GifFileType *file;
8761 GifByteType *buf;
8762 int len;
63448a4d 8763{
f036834a 8764 gif_memory_source *src = current_gif_memory_src;
63448a4d 8765
5ad6a5fb
GM
8766 if (len > src->len - src->index)
8767 return -1;
63448a4d 8768
5ad6a5fb 8769 bcopy (src->bytes + src->index, buf, len);
63448a4d
WP
8770 src->index += len;
8771 return len;
8772}
333b20bb 8773
5ad6a5fb 8774
333b20bb
GM
8775/* Load GIF image IMG for use on frame F. Value is non-zero if
8776 successful. */
8777
8778static int
8779gif_load (f, img)
8780 struct frame *f;
8781 struct image *img;
8782{
8783 Lisp_Object file, specified_file;
63448a4d 8784 Lisp_Object specified_data;
333b20bb
GM
8785 int rc, width, height, x, y, i;
8786 XImage *ximg;
8787 ColorMapObject *gif_color_map;
8788 unsigned long pixel_colors[256];
8789 GifFileType *gif;
8790 struct gcpro gcpro1;
8791 Lisp_Object image;
8792 int ino, image_left, image_top, image_width, image_height;
63448a4d 8793 gif_memory_source memsrc;
9b784e96 8794 unsigned char *raster;
333b20bb
GM
8795
8796 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 8797 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
8798 file = Qnil;
8799 GCPRO1 (file);
63448a4d
WP
8800
8801 if (NILP (specified_data))
5ad6a5fb
GM
8802 {
8803 file = x_find_image_file (specified_file);
8804 if (!STRINGP (file))
63448a4d 8805 {
45158a91 8806 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5ad6a5fb
GM
8807 UNGCPRO;
8808 return 0;
8809 }
177c0ea7 8810
5ad6a5fb 8811 /* Open the GIF file. */
d5db4077 8812 gif = DGifOpenFileName (SDATA (file));
5ad6a5fb
GM
8813 if (gif == NULL)
8814 {
8815 image_error ("Cannot open `%s'", file, Qnil);
8816 UNGCPRO;
8817 return 0;
63448a4d 8818 }
5ad6a5fb 8819 }
63448a4d 8820 else
5ad6a5fb
GM
8821 {
8822 /* Read from memory! */
f036834a 8823 current_gif_memory_src = &memsrc;
d5db4077
KR
8824 memsrc.bytes = SDATA (specified_data);
8825 memsrc.len = SBYTES (specified_data);
5ad6a5fb 8826 memsrc.index = 0;
63448a4d 8827
5ad6a5fb
GM
8828 gif = DGifOpen(&memsrc, gif_read_from_memory);
8829 if (!gif)
8830 {
45158a91 8831 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
5ad6a5fb
GM
8832 UNGCPRO;
8833 return 0;
63448a4d 8834 }
5ad6a5fb 8835 }
333b20bb
GM
8836
8837 /* Read entire contents. */
8838 rc = DGifSlurp (gif);
8839 if (rc == GIF_ERROR)
8840 {
45158a91 8841 image_error ("Error reading `%s'", img->spec, Qnil);
333b20bb
GM
8842 DGifCloseFile (gif);
8843 UNGCPRO;
8844 return 0;
8845 }
8846
3ccff1e3 8847 image = image_spec_value (img->spec, QCindex, NULL);
333b20bb
GM
8848 ino = INTEGERP (image) ? XFASTINT (image) : 0;
8849 if (ino >= gif->ImageCount)
8850 {
45158a91
GM
8851 image_error ("Invalid image number `%s' in image `%s'",
8852 image, img->spec);
333b20bb
GM
8853 DGifCloseFile (gif);
8854 UNGCPRO;
8855 return 0;
8856 }
8857
c7f07c4c
PJ
8858 width = img->width = max (gif->SWidth, gif->Image.Left + gif->Image.Width);
8859 height = img->height = max (gif->SHeight, gif->Image.Top + gif->Image.Height);
333b20bb 8860
333b20bb 8861 /* Create the X image and pixmap. */
45158a91 8862 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
333b20bb 8863 {
333b20bb
GM
8864 DGifCloseFile (gif);
8865 UNGCPRO;
8866 return 0;
8867 }
177c0ea7 8868
333b20bb
GM
8869 /* Allocate colors. */
8870 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
8871 if (!gif_color_map)
8872 gif_color_map = gif->SColorMap;
8873 init_color_table ();
8874 bzero (pixel_colors, sizeof pixel_colors);
177c0ea7 8875
333b20bb
GM
8876 for (i = 0; i < gif_color_map->ColorCount; ++i)
8877 {
8878 int r = gif_color_map->Colors[i].Red << 8;
8879 int g = gif_color_map->Colors[i].Green << 8;
8880 int b = gif_color_map->Colors[i].Blue << 8;
8881 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
8882 }
8883
8884 img->colors = colors_in_color_table (&img->ncolors);
8885 free_color_table ();
8886
8887 /* Clear the part of the screen image that are not covered by
177c0ea7 8888 the image from the GIF file. Full animated GIF support
333b20bb
GM
8889 requires more than can be done here (see the gif89 spec,
8890 disposal methods). Let's simply assume that the part
8891 not covered by a sub-image is in the frame's background color. */
8892 image_top = gif->SavedImages[ino].ImageDesc.Top;
8893 image_left = gif->SavedImages[ino].ImageDesc.Left;
8894 image_width = gif->SavedImages[ino].ImageDesc.Width;
8895 image_height = gif->SavedImages[ino].ImageDesc.Height;
8896
8897 for (y = 0; y < image_top; ++y)
8898 for (x = 0; x < width; ++x)
8899 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
8900
8901 for (y = image_top + image_height; y < height; ++y)
8902 for (x = 0; x < width; ++x)
8903 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
8904
8905 for (y = image_top; y < image_top + image_height; ++y)
8906 {
8907 for (x = 0; x < image_left; ++x)
8908 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
8909 for (x = image_left + image_width; x < width; ++x)
8910 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
8911 }
8912
9b784e96
GM
8913 /* Read the GIF image into the X image. We use a local variable
8914 `raster' here because RasterBits below is a char *, and invites
8915 problems with bytes >= 0x80. */
8916 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
177c0ea7 8917
333b20bb
GM
8918 if (gif->SavedImages[ino].ImageDesc.Interlace)
8919 {
8920 static int interlace_start[] = {0, 4, 2, 1};
8921 static int interlace_increment[] = {8, 8, 4, 2};
9b207e8e 8922 int pass;
06482119
GM
8923 int row = interlace_start[0];
8924
8925 pass = 0;
333b20bb 8926
06482119 8927 for (y = 0; y < image_height; y++)
333b20bb 8928 {
06482119
GM
8929 if (row >= image_height)
8930 {
8931 row = interlace_start[++pass];
8932 while (row >= image_height)
8933 row = interlace_start[++pass];
8934 }
177c0ea7 8935
06482119
GM
8936 for (x = 0; x < image_width; x++)
8937 {
9b784e96 8938 int i = raster[(y * image_width) + x];
06482119
GM
8939 XPutPixel (ximg, x + image_left, row + image_top,
8940 pixel_colors[i]);
8941 }
177c0ea7 8942
06482119 8943 row += interlace_increment[pass];
333b20bb
GM
8944 }
8945 }
8946 else
8947 {
8948 for (y = 0; y < image_height; ++y)
8949 for (x = 0; x < image_width; ++x)
8950 {
9b784e96 8951 int i = raster[y * image_width + x];
333b20bb
GM
8952 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
8953 }
8954 }
177c0ea7 8955
333b20bb 8956 DGifCloseFile (gif);
f20a3b7a
MB
8957
8958 /* Maybe fill in the background field while we have ximg handy. */
8959 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8960 IMAGE_BACKGROUND (img, f, ximg);
177c0ea7 8961
333b20bb
GM
8962 /* Put the image into the pixmap, then free the X image and its buffer. */
8963 x_put_x_image (f, ximg, img->pixmap, width, height);
8964 x_destroy_x_image (ximg);
177c0ea7 8965
333b20bb
GM
8966 UNGCPRO;
8967 return 1;
8968}
8969
8970#endif /* HAVE_GIF != 0 */
8971
8972
8973\f
8974/***********************************************************************
8975 Ghostscript
8976 ***********************************************************************/
8977
8978static int gs_image_p P_ ((Lisp_Object object));
8979static int gs_load P_ ((struct frame *f, struct image *img));
8980static void gs_clear_image P_ ((struct frame *f, struct image *img));
8981
fcf431dc 8982/* The symbol `postscript' identifying images of this type. */
333b20bb 8983
fcf431dc 8984Lisp_Object Qpostscript;
333b20bb
GM
8985
8986/* Keyword symbols. */
8987
8988Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
8989
8990/* Indices of image specification fields in gs_format, below. */
8991
8992enum gs_keyword_index
8993{
8994 GS_TYPE,
8995 GS_PT_WIDTH,
8996 GS_PT_HEIGHT,
8997 GS_FILE,
8998 GS_LOADER,
8999 GS_BOUNDING_BOX,
9000 GS_ASCENT,
9001 GS_MARGIN,
9002 GS_RELIEF,
9003 GS_ALGORITHM,
9004 GS_HEURISTIC_MASK,
4a8e312c 9005 GS_MASK,
f20a3b7a 9006 GS_BACKGROUND,
333b20bb
GM
9007 GS_LAST
9008};
9009
9010/* Vector of image_keyword structures describing the format
9011 of valid user-defined image specifications. */
9012
9013static struct image_keyword gs_format[GS_LAST] =
9014{
9015 {":type", IMAGE_SYMBOL_VALUE, 1},
9016 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9017 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
9018 {":file", IMAGE_STRING_VALUE, 1},
9019 {":loader", IMAGE_FUNCTION_VALUE, 0},
9020 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
7c7ff7f5 9021 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 9022 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 9023 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 9024 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 9025 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
9026 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9027 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
9028};
9029
9030/* Structure describing the image type `ghostscript'. */
9031
9032static struct image_type gs_type =
9033{
fcf431dc 9034 &Qpostscript,
333b20bb
GM
9035 gs_image_p,
9036 gs_load,
9037 gs_clear_image,
9038 NULL
9039};
9040
9041
9042/* Free X resources of Ghostscript image IMG which is used on frame F. */
9043
9044static void
9045gs_clear_image (f, img)
9046 struct frame *f;
9047 struct image *img;
9048{
9049 /* IMG->data.ptr_val may contain a recorded colormap. */
9050 xfree (img->data.ptr_val);
9051 x_clear_image (f, img);
9052}
9053
9054
9055/* Return non-zero if OBJECT is a valid Ghostscript image
9056 specification. */
9057
9058static int
9059gs_image_p (object)
9060 Lisp_Object object;
9061{
9062 struct image_keyword fmt[GS_LAST];
9063 Lisp_Object tem;
9064 int i;
177c0ea7 9065
333b20bb 9066 bcopy (gs_format, fmt, sizeof fmt);
177c0ea7 9067
7c7ff7f5 9068 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
333b20bb
GM
9069 return 0;
9070
9071 /* Bounding box must be a list or vector containing 4 integers. */
9072 tem = fmt[GS_BOUNDING_BOX].value;
9073 if (CONSP (tem))
9074 {
9075 for (i = 0; i < 4; ++i, tem = XCDR (tem))
9076 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
9077 return 0;
9078 if (!NILP (tem))
9079 return 0;
9080 }
9081 else if (VECTORP (tem))
9082 {
9083 if (XVECTOR (tem)->size != 4)
9084 return 0;
9085 for (i = 0; i < 4; ++i)
9086 if (!INTEGERP (XVECTOR (tem)->contents[i]))
9087 return 0;
9088 }
9089 else
9090 return 0;
9091
9092 return 1;
9093}
9094
9095
9096/* Load Ghostscript image IMG for use on frame F. Value is non-zero
9097 if successful. */
9098
9099static int
9100gs_load (f, img)
9101 struct frame *f;
9102 struct image *img;
9103{
9104 char buffer[100];
9105 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
9106 struct gcpro gcpro1, gcpro2;
9107 Lisp_Object frame;
9108 double in_width, in_height;
9109 Lisp_Object pixel_colors = Qnil;
9110
9111 /* Compute pixel size of pixmap needed from the given size in the
9112 image specification. Sizes in the specification are in pt. 1 pt
9113 = 1/72 in, xdpi and ydpi are stored in the frame's X display
9114 info. */
9115 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
9116 in_width = XFASTINT (pt_width) / 72.0;
9117 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
9118 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
9119 in_height = XFASTINT (pt_height) / 72.0;
9120 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
9121
9122 /* Create the pixmap. */
dd00328a 9123 xassert (img->pixmap == None);
333b20bb
GM
9124 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9125 img->width, img->height,
9126 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
333b20bb
GM
9127
9128 if (!img->pixmap)
9129 {
45158a91 9130 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
333b20bb
GM
9131 return 0;
9132 }
177c0ea7 9133
333b20bb
GM
9134 /* Call the loader to fill the pixmap. It returns a process object
9135 if successful. We do not record_unwind_protect here because
9136 other places in redisplay like calling window scroll functions
9137 don't either. Let the Lisp loader use `unwind-protect' instead. */
9138 GCPRO2 (window_and_pixmap_id, pixel_colors);
9139
9140 sprintf (buffer, "%lu %lu",
9141 (unsigned long) FRAME_X_WINDOW (f),
9142 (unsigned long) img->pixmap);
9143 window_and_pixmap_id = build_string (buffer);
177c0ea7 9144
333b20bb
GM
9145 sprintf (buffer, "%lu %lu",
9146 FRAME_FOREGROUND_PIXEL (f),
9147 FRAME_BACKGROUND_PIXEL (f));
9148 pixel_colors = build_string (buffer);
177c0ea7 9149
333b20bb
GM
9150 XSETFRAME (frame, f);
9151 loader = image_spec_value (img->spec, QCloader, NULL);
9152 if (NILP (loader))
9153 loader = intern ("gs-load-image");
9154
9155 img->data.lisp_val = call6 (loader, frame, img->spec,
9156 make_number (img->width),
9157 make_number (img->height),
9158 window_and_pixmap_id,
9159 pixel_colors);
9160 UNGCPRO;
9161 return PROCESSP (img->data.lisp_val);
9162}
9163
9164
9165/* Kill the Ghostscript process that was started to fill PIXMAP on
9166 frame F. Called from XTread_socket when receiving an event
9167 telling Emacs that Ghostscript has finished drawing. */
9168
9169void
9170x_kill_gs_process (pixmap, f)
9171 Pixmap pixmap;
9172 struct frame *f;
9173{
9174 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
9175 int class, i;
9176 struct image *img;
9177
9178 /* Find the image containing PIXMAP. */
9179 for (i = 0; i < c->used; ++i)
9180 if (c->images[i]->pixmap == pixmap)
9181 break;
9182
daba7643
GM
9183 /* Should someone in between have cleared the image cache, for
9184 instance, give up. */
9185 if (i == c->used)
9186 return;
177c0ea7 9187
333b20bb
GM
9188 /* Kill the GS process. We should have found PIXMAP in the image
9189 cache and its image should contain a process object. */
333b20bb
GM
9190 img = c->images[i];
9191 xassert (PROCESSP (img->data.lisp_val));
9192 Fkill_process (img->data.lisp_val, Qnil);
9193 img->data.lisp_val = Qnil;
9194
9195 /* On displays with a mutable colormap, figure out the colors
9196 allocated for the image by looking at the pixels of an XImage for
9197 img->pixmap. */
383d6ffc 9198 class = FRAME_X_VISUAL (f)->class;
333b20bb
GM
9199 if (class != StaticColor && class != StaticGray && class != TrueColor)
9200 {
9201 XImage *ximg;
9202
9203 BLOCK_INPUT;
9204
9205 /* Try to get an XImage for img->pixmep. */
9206 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
9207 0, 0, img->width, img->height, ~0, ZPixmap);
9208 if (ximg)
9209 {
9210 int x, y;
177c0ea7 9211
333b20bb
GM
9212 /* Initialize the color table. */
9213 init_color_table ();
177c0ea7 9214
333b20bb
GM
9215 /* For each pixel of the image, look its color up in the
9216 color table. After having done so, the color table will
9217 contain an entry for each color used by the image. */
9218 for (y = 0; y < img->height; ++y)
9219 for (x = 0; x < img->width; ++x)
9220 {
9221 unsigned long pixel = XGetPixel (ximg, x, y);
9222 lookup_pixel_color (f, pixel);
9223 }
9224
9225 /* Record colors in the image. Free color table and XImage. */
9226 img->colors = colors_in_color_table (&img->ncolors);
9227 free_color_table ();
9228 XDestroyImage (ximg);
9229
9230#if 0 /* This doesn't seem to be the case. If we free the colors
9231 here, we get a BadAccess later in x_clear_image when
9232 freeing the colors. */
9233 /* We have allocated colors once, but Ghostscript has also
9234 allocated colors on behalf of us. So, to get the
9235 reference counts right, free them once. */
9236 if (img->ncolors)
462d5d40 9237 x_free_colors (f, img->colors, img->ncolors);
333b20bb
GM
9238#endif
9239 }
9240 else
9241 image_error ("Cannot get X image of `%s'; colors will not be freed",
45158a91 9242 img->spec, Qnil);
177c0ea7 9243
333b20bb
GM
9244 UNBLOCK_INPUT;
9245 }
ad18ffb1
GM
9246
9247 /* Now that we have the pixmap, compute mask and transform the
9248 image if requested. */
9249 BLOCK_INPUT;
9250 postprocess_image (f, img);
9251 UNBLOCK_INPUT;
333b20bb
GM
9252}
9253
9254
9255\f
9256/***********************************************************************
9257 Window properties
9258 ***********************************************************************/
9259
9260DEFUN ("x-change-window-property", Fx_change_window_property,
9261 Sx_change_window_property, 2, 3, 0,
7ee72033 9262 doc: /* Change window property PROP to VALUE on the X window of FRAME.
c061c855 9263PROP and VALUE must be strings. FRAME nil or omitted means use the
7ee72033
MB
9264selected frame. Value is VALUE. */)
9265 (prop, value, frame)
333b20bb
GM
9266 Lisp_Object frame, prop, value;
9267{
9268 struct frame *f = check_x_frame (frame);
9269 Atom prop_atom;
9270
b7826503
PJ
9271 CHECK_STRING (prop);
9272 CHECK_STRING (value);
333b20bb
GM
9273
9274 BLOCK_INPUT;
d5db4077 9275 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
333b20bb
GM
9276 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9277 prop_atom, XA_STRING, 8, PropModeReplace,
d5db4077 9278 SDATA (value), SCHARS (value));
333b20bb
GM
9279
9280 /* Make sure the property is set when we return. */
9281 XFlush (FRAME_X_DISPLAY (f));
9282 UNBLOCK_INPUT;
9283
9284 return value;
9285}
9286
9287
9288DEFUN ("x-delete-window-property", Fx_delete_window_property,
9289 Sx_delete_window_property, 1, 2, 0,
7ee72033
MB
9290 doc: /* Remove window property PROP from X window of FRAME.
9291FRAME nil or omitted means use the selected frame. Value is PROP. */)
9292 (prop, frame)
333b20bb
GM
9293 Lisp_Object prop, frame;
9294{
9295 struct frame *f = check_x_frame (frame);
9296 Atom prop_atom;
9297
b7826503 9298 CHECK_STRING (prop);
333b20bb 9299 BLOCK_INPUT;
d5db4077 9300 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
333b20bb
GM
9301 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
9302
9303 /* Make sure the property is removed when we return. */
9304 XFlush (FRAME_X_DISPLAY (f));
9305 UNBLOCK_INPUT;
9306
9307 return prop;
9308}
9309
9310
9311DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
9312 1, 2, 0,
7ee72033 9313 doc: /* Value is the value of window property PROP on FRAME.
c061c855
GM
9314If FRAME is nil or omitted, use the selected frame. Value is nil
9315if FRAME hasn't a property with name PROP or if PROP has no string
7ee72033
MB
9316value. */)
9317 (prop, frame)
333b20bb
GM
9318 Lisp_Object prop, frame;
9319{
9320 struct frame *f = check_x_frame (frame);
9321 Atom prop_atom;
9322 int rc;
9323 Lisp_Object prop_value = Qnil;
9324 char *tmp_data = NULL;
9325 Atom actual_type;
9326 int actual_format;
9327 unsigned long actual_size, bytes_remaining;
9328
b7826503 9329 CHECK_STRING (prop);
333b20bb 9330 BLOCK_INPUT;
d5db4077 9331 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
333b20bb
GM
9332 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9333 prop_atom, 0, 0, False, XA_STRING,
9334 &actual_type, &actual_format, &actual_size,
9335 &bytes_remaining, (unsigned char **) &tmp_data);
9336 if (rc == Success)
9337 {
9338 int size = bytes_remaining;
9339
9340 XFree (tmp_data);
9341 tmp_data = NULL;
9342
9343 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9344 prop_atom, 0, bytes_remaining,
9345 False, XA_STRING,
177c0ea7
JB
9346 &actual_type, &actual_format,
9347 &actual_size, &bytes_remaining,
333b20bb 9348 (unsigned char **) &tmp_data);
4c8c7926 9349 if (rc == Success && tmp_data)
333b20bb
GM
9350 prop_value = make_string (tmp_data, size);
9351
9352 XFree (tmp_data);
9353 }
9354
9355 UNBLOCK_INPUT;
9356 return prop_value;
9357}
9358
9359
9360\f
9361/***********************************************************************
9362 Busy cursor
9363 ***********************************************************************/
9364
4ae9a85e 9365/* If non-null, an asynchronous timer that, when it expires, displays
0af913d7 9366 an hourglass cursor on all frames. */
333b20bb 9367
0af913d7 9368static struct atimer *hourglass_atimer;
333b20bb 9369
0af913d7 9370/* Non-zero means an hourglass cursor is currently shown. */
333b20bb 9371
0af913d7 9372static int hourglass_shown_p;
333b20bb 9373
0af913d7 9374/* Number of seconds to wait before displaying an hourglass cursor. */
333b20bb 9375
0af913d7 9376static Lisp_Object Vhourglass_delay;
333b20bb 9377
0af913d7 9378/* Default number of seconds to wait before displaying an hourglass
4ae9a85e
GM
9379 cursor. */
9380
0af913d7 9381#define DEFAULT_HOURGLASS_DELAY 1
4ae9a85e
GM
9382
9383/* Function prototypes. */
9384
0af913d7
GM
9385static void show_hourglass P_ ((struct atimer *));
9386static void hide_hourglass P_ ((void));
4ae9a85e
GM
9387
9388
0af913d7 9389/* Cancel a currently active hourglass timer, and start a new one. */
4ae9a85e
GM
9390
9391void
0af913d7 9392start_hourglass ()
333b20bb 9393{
4ae9a85e 9394 EMACS_TIME delay;
3caa99d3 9395 int secs, usecs = 0;
177c0ea7 9396
0af913d7 9397 cancel_hourglass ();
4ae9a85e 9398
0af913d7
GM
9399 if (INTEGERP (Vhourglass_delay)
9400 && XINT (Vhourglass_delay) > 0)
9401 secs = XFASTINT (Vhourglass_delay);
9402 else if (FLOATP (Vhourglass_delay)
9403 && XFLOAT_DATA (Vhourglass_delay) > 0)
3caa99d3
GM
9404 {
9405 Lisp_Object tem;
0af913d7 9406 tem = Ftruncate (Vhourglass_delay, Qnil);
3caa99d3 9407 secs = XFASTINT (tem);
0af913d7 9408 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
3caa99d3 9409 }
4ae9a85e 9410 else
0af913d7 9411 secs = DEFAULT_HOURGLASS_DELAY;
177c0ea7 9412
3caa99d3 9413 EMACS_SET_SECS_USECS (delay, secs, usecs);
0af913d7
GM
9414 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
9415 show_hourglass, NULL);
4ae9a85e
GM
9416}
9417
9418
0af913d7 9419/* Cancel the hourglass cursor timer if active, hide a busy cursor if
4ae9a85e
GM
9420 shown. */
9421
9422void
0af913d7 9423cancel_hourglass ()
4ae9a85e 9424{
0af913d7 9425 if (hourglass_atimer)
99f01f62 9426 {
0af913d7
GM
9427 cancel_atimer (hourglass_atimer);
9428 hourglass_atimer = NULL;
99f01f62 9429 }
177c0ea7 9430
0af913d7
GM
9431 if (hourglass_shown_p)
9432 hide_hourglass ();
4ae9a85e
GM
9433}
9434
9435
0af913d7
GM
9436/* Timer function of hourglass_atimer. TIMER is equal to
9437 hourglass_atimer.
4ae9a85e 9438
0af913d7
GM
9439 Display an hourglass pointer on all frames by mapping the frames'
9440 hourglass_window. Set the hourglass_p flag in the frames'
9441 output_data.x structure to indicate that an hourglass cursor is
9442 shown on the frames. */
4ae9a85e
GM
9443
9444static void
0af913d7 9445show_hourglass (timer)
4ae9a85e
GM
9446 struct atimer *timer;
9447{
9448 /* The timer implementation will cancel this timer automatically
0af913d7 9449 after this function has run. Set hourglass_atimer to null
4ae9a85e 9450 so that we know the timer doesn't have to be canceled. */
0af913d7 9451 hourglass_atimer = NULL;
4ae9a85e 9452
0af913d7 9453 if (!hourglass_shown_p)
333b20bb
GM
9454 {
9455 Lisp_Object rest, frame;
177c0ea7 9456
4ae9a85e 9457 BLOCK_INPUT;
177c0ea7 9458
333b20bb 9459 FOR_EACH_FRAME (rest, frame)
5f7a1890
GM
9460 {
9461 struct frame *f = XFRAME (frame);
177c0ea7 9462
5f7a1890
GM
9463 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
9464 {
9465 Display *dpy = FRAME_X_DISPLAY (f);
177c0ea7 9466
5f7a1890
GM
9467#ifdef USE_X_TOOLKIT
9468 if (f->output_data.x->widget)
9469#else
9470 if (FRAME_OUTER_WINDOW (f))
9471#endif
9472 {
0af913d7 9473 f->output_data.x->hourglass_p = 1;
177c0ea7 9474
0af913d7 9475 if (!f->output_data.x->hourglass_window)
5f7a1890
GM
9476 {
9477 unsigned long mask = CWCursor;
9478 XSetWindowAttributes attrs;
177c0ea7 9479
0af913d7 9480 attrs.cursor = f->output_data.x->hourglass_cursor;
177c0ea7 9481
0af913d7 9482 f->output_data.x->hourglass_window
5f7a1890
GM
9483 = XCreateWindow (dpy, FRAME_OUTER_WINDOW (f),
9484 0, 0, 32000, 32000, 0, 0,
9485 InputOnly,
9486 CopyFromParent,
9487 mask, &attrs);
9488 }
177c0ea7 9489
0af913d7 9490 XMapRaised (dpy, f->output_data.x->hourglass_window);
5f7a1890
GM
9491 XFlush (dpy);
9492 }
9493 }
9494 }
333b20bb 9495
0af913d7 9496 hourglass_shown_p = 1;
4ae9a85e
GM
9497 UNBLOCK_INPUT;
9498 }
333b20bb
GM
9499}
9500
9501
0af913d7
GM
9502/* Hide the hourglass pointer on all frames, if it is currently
9503 shown. */
333b20bb 9504
4ae9a85e 9505static void
0af913d7 9506hide_hourglass ()
4ae9a85e 9507{
0af913d7 9508 if (hourglass_shown_p)
333b20bb 9509 {
4ae9a85e
GM
9510 Lisp_Object rest, frame;
9511
9512 BLOCK_INPUT;
9513 FOR_EACH_FRAME (rest, frame)
333b20bb 9514 {
4ae9a85e 9515 struct frame *f = XFRAME (frame);
177c0ea7 9516
4ae9a85e
GM
9517 if (FRAME_X_P (f)
9518 /* Watch out for newly created frames. */
0af913d7 9519 && f->output_data.x->hourglass_window)
4ae9a85e 9520 {
0af913d7
GM
9521 XUnmapWindow (FRAME_X_DISPLAY (f),
9522 f->output_data.x->hourglass_window);
9523 /* Sync here because XTread_socket looks at the
9524 hourglass_p flag that is reset to zero below. */
4ae9a85e 9525 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 9526 f->output_data.x->hourglass_p = 0;
4ae9a85e 9527 }
333b20bb 9528 }
333b20bb 9529
0af913d7 9530 hourglass_shown_p = 0;
4ae9a85e
GM
9531 UNBLOCK_INPUT;
9532 }
333b20bb
GM
9533}
9534
9535
9536\f
9537/***********************************************************************
9538 Tool tips
9539 ***********************************************************************/
9540
9541static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
275841bf 9542 Lisp_Object, Lisp_Object));
06d62053 9543static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
ab452f99 9544 Lisp_Object, int, int, int *, int *));
177c0ea7 9545
44b5a125 9546/* The frame of a currently visible tooltip. */
333b20bb 9547
44b5a125 9548Lisp_Object tip_frame;
333b20bb
GM
9549
9550/* If non-nil, a timer started that hides the last tooltip when it
9551 fires. */
9552
9553Lisp_Object tip_timer;
9554Window tip_window;
9555
06d62053
GM
9556/* If non-nil, a vector of 3 elements containing the last args
9557 with which x-show-tip was called. See there. */
9558
9559Lisp_Object last_show_tip_args;
9560
d63931a2
GM
9561/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
9562
9563Lisp_Object Vx_max_tooltip_size;
9564
eaf1eea9
GM
9565
9566static Lisp_Object
9567unwind_create_tip_frame (frame)
9568 Lisp_Object frame;
9569{
c844a81a
GM
9570 Lisp_Object deleted;
9571
9572 deleted = unwind_create_frame (frame);
9573 if (EQ (deleted, Qt))
9574 {
9575 tip_window = None;
9576 tip_frame = Qnil;
9577 }
177c0ea7 9578
c844a81a 9579 return deleted;
eaf1eea9
GM
9580}
9581
9582
333b20bb 9583/* Create a frame for a tooltip on the display described by DPYINFO.
275841bf
GM
9584 PARMS is a list of frame parameters. TEXT is the string to
9585 display in the tip frame. Value is the frame.
eaf1eea9
GM
9586
9587 Note that functions called here, esp. x_default_parameter can
9588 signal errors, for instance when a specified color name is
9589 undefined. We have to make sure that we're in a consistent state
9590 when this happens. */
333b20bb
GM
9591
9592static Lisp_Object
275841bf 9593x_create_tip_frame (dpyinfo, parms, text)
333b20bb 9594 struct x_display_info *dpyinfo;
275841bf 9595 Lisp_Object parms, text;
333b20bb
GM
9596{
9597 struct frame *f;
9598 Lisp_Object frame, tem;
9599 Lisp_Object name;
333b20bb
GM
9600 long window_prompting = 0;
9601 int width, height;
331379bf 9602 int count = SPECPDL_INDEX ();
b6d7acec 9603 struct gcpro gcpro1, gcpro2, gcpro3;
333b20bb 9604 struct kboard *kb;
06d62053 9605 int face_change_count_before = face_change_count;
275841bf
GM
9606 Lisp_Object buffer;
9607 struct buffer *old_buffer;
333b20bb
GM
9608
9609 check_x ();
9610
9611 /* Use this general default value to start with until we know if
9612 this frame has a specified name. */
9613 Vx_resource_name = Vinvocation_name;
9614
9615#ifdef MULTI_KBOARD
9616 kb = dpyinfo->kboard;
9617#else
9618 kb = &the_only_kboard;
9619#endif
9620
9621 /* Get the name of the frame to use for resource lookup. */
9622 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
9623 if (!STRINGP (name)
9624 && !EQ (name, Qunbound)
9625 && !NILP (name))
9626 error ("Invalid frame name--not a string or nil");
9627 Vx_resource_name = name;
9628
9629 frame = Qnil;
9630 GCPRO3 (parms, name, frame);
44b5a125 9631 f = make_frame (1);
333b20bb 9632 XSETFRAME (frame, f);
275841bf
GM
9633
9634 buffer = Fget_buffer_create (build_string (" *tip*"));
9635 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
9636 old_buffer = current_buffer;
9637 set_buffer_internal_1 (XBUFFER (buffer));
d63931a2 9638 current_buffer->truncate_lines = Qnil;
275841bf
GM
9639 Ferase_buffer ();
9640 Finsert (1, &text);
9641 set_buffer_internal_1 (old_buffer);
177c0ea7 9642
333b20bb 9643 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
8a1a7743 9644 record_unwind_protect (unwind_create_tip_frame, frame);
333b20bb 9645
eaf1eea9
GM
9646 /* By setting the output method, we're essentially saying that
9647 the frame is live, as per FRAME_LIVE_P. If we get a signal
9648 from this point on, x_destroy_window might screw up reference
9649 counts etc. */
333b20bb
GM
9650 f->output_method = output_x_window;
9651 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
9652 bzero (f->output_data.x, sizeof (struct x_output));
9653 f->output_data.x->icon_bitmap = -1;
9654 f->output_data.x->fontset = -1;
61d461a8
GM
9655 f->output_data.x->scroll_bar_foreground_pixel = -1;
9656 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
9657#ifdef USE_TOOLKIT_SCROLL_BARS
9658 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
9659 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
9660#endif /* USE_TOOLKIT_SCROLL_BARS */
333b20bb
GM
9661 f->icon_name = Qnil;
9662 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
f1d2ce7f 9663#if GLYPH_DEBUG
eaf1eea9
GM
9664 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
9665 dpyinfo_refcount = dpyinfo->reference_count;
9666#endif /* GLYPH_DEBUG */
333b20bb
GM
9667#ifdef MULTI_KBOARD
9668 FRAME_KBOARD (f) = kb;
9669#endif
9670 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
9671 f->output_data.x->explicit_parent = 0;
9672
61d461a8
GM
9673 /* These colors will be set anyway later, but it's important
9674 to get the color reference counts right, so initialize them! */
9675 {
9676 Lisp_Object black;
9677 struct gcpro gcpro1;
177c0ea7 9678
61d461a8
GM
9679 black = build_string ("black");
9680 GCPRO1 (black);
9681 f->output_data.x->foreground_pixel
9682 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
9683 f->output_data.x->background_pixel
9684 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
9685 f->output_data.x->cursor_pixel
9686 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
9687 f->output_data.x->cursor_foreground_pixel
9688 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
9689 f->output_data.x->border_pixel
9690 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
9691 f->output_data.x->mouse_pixel
9692 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
9693 UNGCPRO;
9694 }
9695
333b20bb
GM
9696 /* Set the name; the functions to which we pass f expect the name to
9697 be set. */
9698 if (EQ (name, Qunbound) || NILP (name))
9699 {
9700 f->name = build_string (dpyinfo->x_id_name);
9701 f->explicit_name = 0;
9702 }
9703 else
9704 {
9705 f->name = name;
9706 f->explicit_name = 1;
9707 /* use the frame's title when getting resources for this frame. */
9708 specbind (Qx_resource_name, name);
9709 }
9710
eaf1eea9
GM
9711 /* Extract the window parameters from the supplied values that are
9712 needed to determine window geometry. */
333b20bb
GM
9713 {
9714 Lisp_Object font;
9715
9716 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
9717
9718 BLOCK_INPUT;
9719 /* First, try whatever font the caller has specified. */
9720 if (STRINGP (font))
9721 {
9722 tem = Fquery_fontset (font, Qnil);
9723 if (STRINGP (tem))
d5db4077 9724 font = x_new_fontset (f, SDATA (tem));
333b20bb 9725 else
d5db4077 9726 font = x_new_font (f, SDATA (font));
333b20bb 9727 }
177c0ea7 9728
333b20bb
GM
9729 /* Try out a font which we hope has bold and italic variations. */
9730 if (!STRINGP (font))
9731 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
9732 if (!STRINGP (font))
9733 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
9734 if (! STRINGP (font))
9735 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
9736 if (! STRINGP (font))
9737 /* This was formerly the first thing tried, but it finds too many fonts
9738 and takes too long. */
9739 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
9740 /* If those didn't work, look for something which will at least work. */
9741 if (! STRINGP (font))
9742 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
9743 UNBLOCK_INPUT;
9744 if (! STRINGP (font))
9745 font = build_string ("fixed");
9746
9747 x_default_parameter (f, parms, Qfont, font,
9748 "font", "Font", RES_TYPE_STRING);
9749 }
9750
9751 x_default_parameter (f, parms, Qborder_width, make_number (2),
9752 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
177c0ea7 9753
333b20bb
GM
9754 /* This defaults to 2 in order to match xterm. We recognize either
9755 internalBorderWidth or internalBorder (which is what xterm calls
9756 it). */
9757 if (NILP (Fassq (Qinternal_border_width, parms)))
9758 {
9759 Lisp_Object value;
9760
9761 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
9762 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
9763 if (! EQ (value, Qunbound))
9764 parms = Fcons (Fcons (Qinternal_border_width, value),
9765 parms);
9766 }
9767
9768 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
9769 "internalBorderWidth", "internalBorderWidth",
9770 RES_TYPE_NUMBER);
9771
9772 /* Also do the stuff which must be set before the window exists. */
9773 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
9774 "foreground", "Foreground", RES_TYPE_STRING);
9775 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
9776 "background", "Background", RES_TYPE_STRING);
9777 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
9778 "pointerColor", "Foreground", RES_TYPE_STRING);
9779 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
9780 "cursorColor", "Foreground", RES_TYPE_STRING);
9781 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
9782 "borderColor", "BorderColor", RES_TYPE_STRING);
9783
9784 /* Init faces before x_default_parameter is called for scroll-bar
9785 parameters because that function calls x_set_scroll_bar_width,
9786 which calls change_frame_size, which calls Fset_window_buffer,
9787 which runs hooks, which call Fvertical_motion. At the end, we
9788 end up in init_iterator with a null face cache, which should not
9789 happen. */
9790 init_frame_faces (f);
177c0ea7 9791
333b20bb 9792 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
333b20bb 9793
7c0d3ed8 9794 window_prompting = x_figure_window_size (f, parms, 0);
333b20bb 9795
333b20bb
GM
9796 {
9797 XSetWindowAttributes attrs;
9798 unsigned long mask;
177c0ea7 9799
333b20bb 9800 BLOCK_INPUT;
c51d2b5e
GM
9801 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
9802 if (DoesSaveUnders (dpyinfo->screen))
9803 mask |= CWSaveUnder;
177c0ea7 9804
9b2956e2
GM
9805 /* Window managers look at the override-redirect flag to determine
9806 whether or net to give windows a decoration (Xlib spec, chapter
333b20bb
GM
9807 3.2.8). */
9808 attrs.override_redirect = True;
9809 attrs.save_under = True;
9810 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
9811 /* Arrange for getting MapNotify and UnmapNotify events. */
9812 attrs.event_mask = StructureNotifyMask;
9813 tip_window
9814 = FRAME_X_WINDOW (f)
9815 = XCreateWindow (FRAME_X_DISPLAY (f),
9816 FRAME_X_DISPLAY_INFO (f)->root_window,
9817 /* x, y, width, height */
9818 0, 0, 1, 1,
9819 /* Border. */
9820 1,
9821 CopyFromParent, InputOutput, CopyFromParent,
9822 mask, &attrs);
9823 UNBLOCK_INPUT;
9824 }
9825
9826 x_make_gc (f);
9827
333b20bb
GM
9828 x_default_parameter (f, parms, Qauto_raise, Qnil,
9829 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
9830 x_default_parameter (f, parms, Qauto_lower, Qnil,
9831 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
9832 x_default_parameter (f, parms, Qcursor_type, Qbox,
9833 "cursorType", "CursorType", RES_TYPE_SYMBOL);
9834
9835 /* Dimensions, especially f->height, must be done via change_frame_size.
9836 Change will not be effected unless different from the current
9837 f->height. */
9838 width = f->width;
9839 height = f->height;
9840 f->height = 0;
9841 SET_FRAME_WIDTH (f, 0);
8938a4fb 9842 change_frame_size (f, height, width, 1, 0, 0);
177c0ea7 9843
cd1d850f
JPW
9844 /* Add `tooltip' frame parameter's default value. */
9845 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
9846 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
9847 Qnil));
177c0ea7 9848
035d5114 9849 /* Set up faces after all frame parameters are known. This call
6801a572
GM
9850 also merges in face attributes specified for new frames.
9851
9852 Frame parameters may be changed if .Xdefaults contains
9853 specifications for the default font. For example, if there is an
9854 `Emacs.default.attributeBackground: pink', the `background-color'
9855 attribute of the frame get's set, which let's the internal border
9856 of the tooltip frame appear in pink. Prevent this. */
9857 {
9858 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
9859
9860 /* Set tip_frame here, so that */
9861 tip_frame = frame;
9862 call1 (Qface_set_after_frame_default, frame);
177c0ea7 9863
6801a572
GM
9864 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
9865 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
9866 Qnil));
9867 }
177c0ea7 9868
333b20bb
GM
9869 f->no_split = 1;
9870
9871 UNGCPRO;
9872
9873 /* It is now ok to make the frame official even if we get an error
9874 below. And the frame needs to be on Vframe_list or making it
9875 visible won't work. */
9876 Vframe_list = Fcons (frame, Vframe_list);
9877
9878 /* Now that the frame is official, it counts as a reference to
9879 its display. */
9880 FRAME_X_DISPLAY_INFO (f)->reference_count++;
9881
06d62053
GM
9882 /* Setting attributes of faces of the tooltip frame from resources
9883 and similar will increment face_change_count, which leads to the
9884 clearing of all current matrices. Since this isn't necessary
9885 here, avoid it by resetting face_change_count to the value it
9886 had before we created the tip frame. */
9887 face_change_count = face_change_count_before;
9888
eaf1eea9 9889 /* Discard the unwind_protect. */
333b20bb
GM
9890 return unbind_to (count, frame);
9891}
9892
9893
06d62053
GM
9894/* Compute where to display tip frame F. PARMS is the list of frame
9895 parameters for F. DX and DY are specified offsets from the current
ab452f99
GM
9896 location of the mouse. WIDTH and HEIGHT are the width and height
9897 of the tooltip. Return coordinates relative to the root window of
9898 the display in *ROOT_X, and *ROOT_Y. */
06d62053
GM
9899
9900static void
ab452f99 9901compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
06d62053
GM
9902 struct frame *f;
9903 Lisp_Object parms, dx, dy;
ab452f99 9904 int width, height;
06d62053
GM
9905 int *root_x, *root_y;
9906{
9907 Lisp_Object left, top;
9908 int win_x, win_y;
9909 Window root, child;
9910 unsigned pmask;
177c0ea7 9911
06d62053
GM
9912 /* User-specified position? */
9913 left = Fcdr (Fassq (Qleft, parms));
9914 top = Fcdr (Fassq (Qtop, parms));
177c0ea7 9915
06d62053
GM
9916 /* Move the tooltip window where the mouse pointer is. Resize and
9917 show it. */
570d22b0 9918 if (!INTEGERP (left) || !INTEGERP (top))
ab452f99
GM
9919 {
9920 BLOCK_INPUT;
9921 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
9922 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
9923 UNBLOCK_INPUT;
9924 }
06d62053 9925
06d62053
GM
9926 if (INTEGERP (top))
9927 *root_y = XINT (top);
ab452f99
GM
9928 else if (*root_y + XINT (dy) - height < 0)
9929 *root_y -= XINT (dy);
9930 else
9931 {
9932 *root_y -= height;
9933 *root_y += XINT (dy);
9934 }
9935
9936 if (INTEGERP (left))
9937 *root_x = XINT (left);
d682d3df
RS
9938 else if (*root_x + XINT (dx) + width <= FRAME_X_DISPLAY_INFO (f)->width)
9939 /* It fits to the right of the pointer. */
9940 *root_x += XINT (dx);
9941 else if (width + XINT (dx) <= *root_x)
9942 /* It fits to the left of the pointer. */
ab452f99
GM
9943 *root_x -= width + XINT (dx);
9944 else
d682d3df
RS
9945 /* Put it left-justified on the screen--it ought to fit that way. */
9946 *root_x = 0;
06d62053
GM
9947}
9948
9949
0634ce98 9950DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7ee72033 9951 doc: /* Show STRING in a "tooltip" window on frame FRAME.
c061c855
GM
9952A tooltip window is a small X window displaying a string.
9953
9954FRAME nil or omitted means use the selected frame.
9955
9956PARMS is an optional list of frame parameters which can be used to
9957change the tooltip's appearance.
9958
9959Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
9960means use the default timeout of 5 seconds.
9961
9962If the list of frame parameters PARAMS contains a `left' parameters,
9963the tooltip is displayed at that x-position. Otherwise it is
9964displayed at the mouse position, with offset DX added (default is 5 if
9965DX isn't specified). Likewise for the y-position; if a `top' frame
9966parameter is specified, it determines the y-position of the tooltip
9967window, otherwise it is displayed at the mouse position, with offset
9968DY added (default is -10).
9969
9970A tooltip's maximum size is specified by `x-max-tooltip-size'.
7ee72033
MB
9971Text larger than the specified size is clipped. */)
9972 (string, frame, parms, timeout, dx, dy)
0634ce98 9973 Lisp_Object string, frame, parms, timeout, dx, dy;
333b20bb
GM
9974{
9975 struct frame *f;
9976 struct window *w;
06d62053 9977 int root_x, root_y;
333b20bb
GM
9978 struct buffer *old_buffer;
9979 struct text_pos pos;
9980 int i, width, height;
393f2d14 9981 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
333b20bb 9982 int old_windows_or_buffers_changed = windows_or_buffers_changed;
331379bf 9983 int count = SPECPDL_INDEX ();
177c0ea7 9984
333b20bb
GM
9985 specbind (Qinhibit_redisplay, Qt);
9986
393f2d14 9987 GCPRO4 (string, parms, frame, timeout);
333b20bb 9988
b7826503 9989 CHECK_STRING (string);
333b20bb
GM
9990 f = check_x_frame (frame);
9991 if (NILP (timeout))
9992 timeout = make_number (5);
9993 else
b7826503 9994 CHECK_NATNUM (timeout);
177c0ea7 9995
0634ce98
GM
9996 if (NILP (dx))
9997 dx = make_number (5);
9998 else
b7826503 9999 CHECK_NUMBER (dx);
177c0ea7 10000
0634ce98 10001 if (NILP (dy))
12c67a7f 10002 dy = make_number (-10);
0634ce98 10003 else
b7826503 10004 CHECK_NUMBER (dy);
333b20bb 10005
06d62053
GM
10006 if (NILP (last_show_tip_args))
10007 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
10008
10009 if (!NILP (tip_frame))
10010 {
10011 Lisp_Object last_string = AREF (last_show_tip_args, 0);
10012 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
10013 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
10014
10015 if (EQ (frame, last_frame)
10016 && !NILP (Fequal (last_string, string))
10017 && !NILP (Fequal (last_parms, parms)))
10018 {
10019 struct frame *f = XFRAME (tip_frame);
177c0ea7 10020
06d62053
GM
10021 /* Only DX and DY have changed. */
10022 if (!NILP (tip_timer))
ae782866
GM
10023 {
10024 Lisp_Object timer = tip_timer;
10025 tip_timer = Qnil;
10026 call1 (Qcancel_timer, timer);
10027 }
06d62053
GM
10028
10029 BLOCK_INPUT;
ab452f99
GM
10030 compute_tip_xy (f, parms, dx, dy, PIXEL_WIDTH (f),
10031 PIXEL_HEIGHT (f), &root_x, &root_y);
06d62053 10032 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 10033 root_x, root_y);
06d62053
GM
10034 UNBLOCK_INPUT;
10035 goto start_timer;
10036 }
10037 }
10038
333b20bb
GM
10039 /* Hide a previous tip, if any. */
10040 Fx_hide_tip ();
10041
06d62053
GM
10042 ASET (last_show_tip_args, 0, string);
10043 ASET (last_show_tip_args, 1, frame);
10044 ASET (last_show_tip_args, 2, parms);
10045
333b20bb
GM
10046 /* Add default values to frame parameters. */
10047 if (NILP (Fassq (Qname, parms)))
10048 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
10049 if (NILP (Fassq (Qinternal_border_width, parms)))
10050 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
10051 if (NILP (Fassq (Qborder_width, parms)))
10052 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
10053 if (NILP (Fassq (Qborder_color, parms)))
10054 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
10055 if (NILP (Fassq (Qbackground_color, parms)))
10056 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
10057 parms);
10058
10059 /* Create a frame for the tooltip, and record it in the global
10060 variable tip_frame. */
275841bf 10061 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
44b5a125 10062 f = XFRAME (frame);
333b20bb 10063
d63931a2 10064 /* Set up the frame's root window. */
333b20bb
GM
10065 w = XWINDOW (FRAME_ROOT_WINDOW (f));
10066 w->left = w->top = make_number (0);
177c0ea7 10067
d63931a2
GM
10068 if (CONSP (Vx_max_tooltip_size)
10069 && INTEGERP (XCAR (Vx_max_tooltip_size))
10070 && XINT (XCAR (Vx_max_tooltip_size)) > 0
10071 && INTEGERP (XCDR (Vx_max_tooltip_size))
10072 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
10073 {
10074 w->width = XCAR (Vx_max_tooltip_size);
10075 w->height = XCDR (Vx_max_tooltip_size);
10076 }
10077 else
10078 {
10079 w->width = make_number (80);
10080 w->height = make_number (40);
10081 }
177c0ea7 10082
d63931a2 10083 f->window_width = XINT (w->width);
333b20bb
GM
10084 adjust_glyphs (f);
10085 w->pseudo_window_p = 1;
10086
10087 /* Display the tooltip text in a temporary buffer. */
333b20bb 10088 old_buffer = current_buffer;
275841bf 10089 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
d63931a2 10090 current_buffer->truncate_lines = Qnil;
333b20bb
GM
10091 clear_glyph_matrix (w->desired_matrix);
10092 clear_glyph_matrix (w->current_matrix);
10093 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
10094 try_window (FRAME_ROOT_WINDOW (f), pos);
10095
10096 /* Compute width and height of the tooltip. */
10097 width = height = 0;
10098 for (i = 0; i < w->desired_matrix->nrows; ++i)
10099 {
10100 struct glyph_row *row = &w->desired_matrix->rows[i];
10101 struct glyph *last;
10102 int row_width;
10103
10104 /* Stop at the first empty row at the end. */
10105 if (!row->enabled_p || !row->displays_text_p)
10106 break;
10107
d7bf0342
GM
10108 /* Let the row go over the full width of the frame. */
10109 row->full_width_p = 1;
333b20bb 10110
e3130015 10111 /* There's a glyph at the end of rows that is used to place
333b20bb
GM
10112 the cursor there. Don't include the width of this glyph. */
10113 if (row->used[TEXT_AREA])
10114 {
10115 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
10116 row_width = row->pixel_width - last->pixel_width;
10117 }
10118 else
10119 row_width = row->pixel_width;
177c0ea7 10120
333b20bb
GM
10121 height += row->height;
10122 width = max (width, row_width);
10123 }
10124
10125 /* Add the frame's internal border to the width and height the X
10126 window should have. */
10127 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
10128 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
10129
10130 /* Move the tooltip window where the mouse pointer is. Resize and
10131 show it. */
ab452f99 10132 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
0634ce98 10133
0634ce98 10134 BLOCK_INPUT;
333b20bb 10135 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 10136 root_x, root_y, width, height);
333b20bb
GM
10137 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
10138 UNBLOCK_INPUT;
177c0ea7 10139
333b20bb
GM
10140 /* Draw into the window. */
10141 w->must_be_updated_p = 1;
10142 update_single_window (w, 1);
10143
10144 /* Restore original current buffer. */
10145 set_buffer_internal_1 (old_buffer);
10146 windows_or_buffers_changed = old_windows_or_buffers_changed;
10147
06d62053 10148 start_timer:
333b20bb
GM
10149 /* Let the tip disappear after timeout seconds. */
10150 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
10151 intern ("x-hide-tip"));
a744a2ec
DL
10152
10153 UNGCPRO;
333b20bb
GM
10154 return unbind_to (count, Qnil);
10155}
10156
10157
10158DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7ee72033
MB
10159 doc: /* Hide the current tooltip window, if there is any.
10160Value is t if tooltip was open, nil otherwise. */)
10161 ()
333b20bb 10162{
44b5a125 10163 int count;
c0006262
GM
10164 Lisp_Object deleted, frame, timer;
10165 struct gcpro gcpro1, gcpro2;
44b5a125
GM
10166
10167 /* Return quickly if nothing to do. */
c0006262 10168 if (NILP (tip_timer) && NILP (tip_frame))
44b5a125 10169 return Qnil;
177c0ea7 10170
c0006262
GM
10171 frame = tip_frame;
10172 timer = tip_timer;
10173 GCPRO2 (frame, timer);
10174 tip_frame = tip_timer = deleted = Qnil;
177c0ea7 10175
331379bf 10176 count = SPECPDL_INDEX ();
333b20bb 10177 specbind (Qinhibit_redisplay, Qt);
44b5a125 10178 specbind (Qinhibit_quit, Qt);
177c0ea7 10179
c0006262 10180 if (!NILP (timer))
ae782866 10181 call1 (Qcancel_timer, timer);
333b20bb 10182
c0006262 10183 if (FRAMEP (frame))
333b20bb 10184 {
44b5a125
GM
10185 Fdelete_frame (frame, Qnil);
10186 deleted = Qt;
f6c44811
GM
10187
10188#ifdef USE_LUCID
10189 /* Bloodcurdling hack alert: The Lucid menu bar widget's
10190 redisplay procedure is not called when a tip frame over menu
10191 items is unmapped. Redisplay the menu manually... */
10192 {
10193 struct frame *f = SELECTED_FRAME ();
10194 Widget w = f->output_data.x->menubar_widget;
10195 extern void xlwmenu_redisplay P_ ((Widget));
9180dc8c 10196
f6c44811 10197 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
dbc64aa7 10198 && w != NULL)
f6c44811
GM
10199 {
10200 BLOCK_INPUT;
10201 xlwmenu_redisplay (w);
10202 UNBLOCK_INPUT;
10203 }
10204 }
10205#endif /* USE_LUCID */
333b20bb
GM
10206 }
10207
c0006262 10208 UNGCPRO;
44b5a125 10209 return unbind_to (count, deleted);
333b20bb
GM
10210}
10211
10212
10213\f
10214/***********************************************************************
10215 File selection dialog
10216 ***********************************************************************/
10217
10218#ifdef USE_MOTIF
10219
10220/* Callback for "OK" and "Cancel" on file selection dialog. */
10221
10222static void
10223file_dialog_cb (widget, client_data, call_data)
10224 Widget widget;
10225 XtPointer call_data, client_data;
10226{
10227 int *result = (int *) client_data;
10228 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
10229 *result = cb->reason;
10230}
10231
10232
a779d213
GM
10233/* Callback for unmapping a file selection dialog. This is used to
10234 capture the case where a dialog is closed via a window manager's
10235 closer button, for example. Using a XmNdestroyCallback didn't work
10236 in this case. */
10237
10238static void
10239file_dialog_unmap_cb (widget, client_data, call_data)
10240 Widget widget;
10241 XtPointer call_data, client_data;
10242{
10243 int *result = (int *) client_data;
10244 *result = XmCR_CANCEL;
10245}
10246
10247
333b20bb 10248DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
7ee72033 10249 doc: /* Read file name, prompting with PROMPT in directory DIR.
c061c855
GM
10250Use a file selection dialog.
10251Select DEFAULT-FILENAME in the dialog's file selection box, if
10252specified. Don't let the user enter a file name in the file
7ee72033
MB
10253selection dialog's entry field, if MUSTMATCH is non-nil. */)
10254 (prompt, dir, default_filename, mustmatch)
333b20bb
GM
10255 Lisp_Object prompt, dir, default_filename, mustmatch;
10256{
10257 int result;
0fe92f72 10258 struct frame *f = SELECTED_FRAME ();
333b20bb
GM
10259 Lisp_Object file = Qnil;
10260 Widget dialog, text, list, help;
10261 Arg al[10];
10262 int ac = 0;
10263 extern XtAppContext Xt_app_con;
333b20bb 10264 XmString dir_xmstring, pattern_xmstring;
65b21658 10265 int count = SPECPDL_INDEX ();
333b20bb
GM
10266 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
10267
10268 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
b7826503
PJ
10269 CHECK_STRING (prompt);
10270 CHECK_STRING (dir);
333b20bb
GM
10271
10272 /* Prevent redisplay. */
10273 specbind (Qinhibit_redisplay, Qt);
10274
10275 BLOCK_INPUT;
10276
10277 /* Create the dialog with PROMPT as title, using DIR as initial
10278 directory and using "*" as pattern. */
10279 dir = Fexpand_file_name (dir, Qnil);
d5db4077 10280 dir_xmstring = XmStringCreateLocalized (SDATA (dir));
333b20bb 10281 pattern_xmstring = XmStringCreateLocalized ("*");
177c0ea7 10282
d5db4077 10283 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
333b20bb
GM
10284 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
10285 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
10286 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
10287 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
10288 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
10289 "fsb", al, ac);
10290 XmStringFree (dir_xmstring);
10291 XmStringFree (pattern_xmstring);
10292
10293 /* Add callbacks for OK and Cancel. */
10294 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
10295 (XtPointer) &result);
10296 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
10297 (XtPointer) &result);
a779d213
GM
10298 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
10299 (XtPointer) &result);
333b20bb
GM
10300
10301 /* Disable the help button since we can't display help. */
10302 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
10303 XtSetSensitive (help, False);
10304
177c0ea7 10305 /* Mark OK button as default. */
333b20bb
GM
10306 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
10307 XmNshowAsDefault, True, NULL);
10308
10309 /* If MUSTMATCH is non-nil, disable the file entry field of the
10310 dialog, so that the user must select a file from the files list
10311 box. We can't remove it because we wouldn't have a way to get at
10312 the result file name, then. */
10313 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
10314 if (!NILP (mustmatch))
10315 {
10316 Widget label;
10317 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
10318 XtSetSensitive (text, False);
10319 XtSetSensitive (label, False);
10320 }
10321
10322 /* Manage the dialog, so that list boxes get filled. */
10323 XtManageChild (dialog);
10324
10325 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
10326 must include the path for this to work. */
10327 list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
10328 if (STRINGP (default_filename))
10329 {
10330 XmString default_xmstring;
10331 int item_pos;
10332
10333 default_xmstring
d5db4077 10334 = XmStringCreateLocalized (SDATA (default_filename));
333b20bb
GM
10335
10336 if (!XmListItemExists (list, default_xmstring))
10337 {
10338 /* Add a new item if DEFAULT_FILENAME is not in the list. */
10339 XmListAddItem (list, default_xmstring, 0);
10340 item_pos = 0;
10341 }
10342 else
10343 item_pos = XmListItemPos (list, default_xmstring);
10344 XmStringFree (default_xmstring);
10345
10346 /* Select the item and scroll it into view. */
10347 XmListSelectPos (list, item_pos, True);
10348 XmListSetPos (list, item_pos);
10349 }
10350
bf338245 10351 /* Process events until the user presses Cancel or OK. */
03100098 10352 result = 0;
a779d213 10353 while (result == 0)
563b384d 10354 {
bf338245
JD
10355 XEvent event;
10356 XtAppNextEvent (Xt_app_con, &event);
1fcfb866 10357 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f) );
563b384d 10358 }
03100098 10359
333b20bb
GM
10360 /* Get the result. */
10361 if (result == XmCR_OK)
10362 {
10363 XmString text;
10364 String data;
177c0ea7 10365
d1670063 10366 XtVaGetValues (dialog, XmNtextString, &text, NULL);
333b20bb
GM
10367 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
10368 XmStringFree (text);
10369 file = build_string (data);
10370 XtFree (data);
10371 }
10372 else
10373 file = Qnil;
10374
10375 /* Clean up. */
10376 XtUnmanageChild (dialog);
10377 XtDestroyWidget (dialog);
10378 UNBLOCK_INPUT;
10379 UNGCPRO;
10380
10381 /* Make "Cancel" equivalent to C-g. */
10382 if (NILP (file))
10383 Fsignal (Qquit, Qnil);
177c0ea7 10384
333b20bb
GM
10385 return unbind_to (count, file);
10386}
10387
10388#endif /* USE_MOTIF */
10389
488dd4c4
JD
10390#ifdef USE_GTK
10391
10392DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
10393 "Read file name, prompting with PROMPT in directory DIR.\n\
10394Use a file selection dialog.\n\
10395Select DEFAULT-FILENAME in the dialog's file selection box, if\n\
10396specified. Don't let the user enter a file name in the file\n\
10397selection dialog's entry field, if MUSTMATCH is non-nil.")
10398 (prompt, dir, default_filename, mustmatch)
10399 Lisp_Object prompt, dir, default_filename, mustmatch;
10400{
10401 FRAME_PTR f = SELECTED_FRAME ();
10402 char *fn;
10403 Lisp_Object file = Qnil;
10404 int count = specpdl_ptr - specpdl;
10405 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
10406 char *cdef_file;
10407 char *cprompt;
177c0ea7 10408
488dd4c4
JD
10409 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
10410 CHECK_STRING (prompt);
10411 CHECK_STRING (dir);
10412
10413 /* Prevent redisplay. */
10414 specbind (Qinhibit_redisplay, Qt);
10415
10416 BLOCK_INPUT;
10417
10418 if (STRINGP (default_filename))
10419 cdef_file = SDATA (default_filename);
10420 else
10421 cdef_file = SDATA (dir);
10422
10423 fn = xg_get_file_name (f, SDATA (prompt), cdef_file, ! NILP (mustmatch));
177c0ea7 10424
488dd4c4
JD
10425 if (fn)
10426 {
10427 file = build_string (fn);
10428 xfree (fn);
10429 }
10430
10431 UNBLOCK_INPUT;
10432 UNGCPRO;
10433
10434 /* Make "Cancel" equivalent to C-g. */
10435 if (NILP (file))
10436 Fsignal (Qquit, Qnil);
177c0ea7 10437
488dd4c4
JD
10438 return unbind_to (count, file);
10439}
10440
10441#endif /* USE_GTK */
333b20bb
GM
10442
10443\f
82bab41c
GM
10444/***********************************************************************
10445 Keyboard
10446 ***********************************************************************/
10447
10448#ifdef HAVE_XKBGETKEYBOARD
10449#include <X11/XKBlib.h>
10450#include <X11/keysym.h>
10451#endif
10452
10453DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
10454 Sx_backspace_delete_keys_p, 0, 1, 0,
7ee72033 10455 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
c061c855
GM
10456FRAME nil means use the selected frame.
10457Value is t if we know that both keys are present, and are mapped to the
7ee72033
MB
10458usual X keysyms. */)
10459 (frame)
82bab41c
GM
10460 Lisp_Object frame;
10461{
10462#ifdef HAVE_XKBGETKEYBOARD
10463 XkbDescPtr kb;
10464 struct frame *f = check_x_frame (frame);
10465 Display *dpy = FRAME_X_DISPLAY (f);
10466 Lisp_Object have_keys;
46f6a258 10467 int major, minor, op, event, error;
82bab41c
GM
10468
10469 BLOCK_INPUT;
46f6a258
GM
10470
10471 /* Check library version in case we're dynamically linked. */
10472 major = XkbMajorVersion;
10473 minor = XkbMinorVersion;
10474 if (!XkbLibraryVersion (&major, &minor))
c1efd260
GM
10475 {
10476 UNBLOCK_INPUT;
10477 return Qnil;
10478 }
46f6a258
GM
10479
10480 /* Check that the server supports XKB. */
10481 major = XkbMajorVersion;
10482 minor = XkbMinorVersion;
10483 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
c1efd260
GM
10484 {
10485 UNBLOCK_INPUT;
10486 return Qnil;
10487 }
177c0ea7 10488
46f6a258 10489 have_keys = Qnil;
c1efd260 10490 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
82bab41c
GM
10491 if (kb)
10492 {
10493 int delete_keycode = 0, backspace_keycode = 0, i;
c1efd260
GM
10494
10495 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
82bab41c 10496 {
c1efd260
GM
10497 for (i = kb->min_key_code;
10498 (i < kb->max_key_code
10499 && (delete_keycode == 0 || backspace_keycode == 0));
10500 ++i)
10501 {
d63931a2
GM
10502 /* The XKB symbolic key names can be seen most easily in
10503 the PS file generated by `xkbprint -label name
10504 $DISPLAY'. */
c1efd260
GM
10505 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
10506 delete_keycode = i;
10507 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
10508 backspace_keycode = i;
10509 }
10510
10511 XkbFreeNames (kb, 0, True);
82bab41c
GM
10512 }
10513
c1efd260 10514 XkbFreeClientMap (kb, 0, True);
177c0ea7 10515
82bab41c
GM
10516 if (delete_keycode
10517 && backspace_keycode
10518 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
10519 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
10520 have_keys = Qt;
10521 }
10522 UNBLOCK_INPUT;
10523 return have_keys;
10524#else /* not HAVE_XKBGETKEYBOARD */
10525 return Qnil;
10526#endif /* not HAVE_XKBGETKEYBOARD */
10527}
10528
10529
10530\f
333b20bb
GM
10531/***********************************************************************
10532 Initialization
10533 ***********************************************************************/
10534
4dacadcc 10535/* Keep this list in the same order as frame_parms in frame.c.
7c0d3ed8
KS
10536 Use 0 for unsupported frame parameters. */
10537
10538frame_parm_handler x_frame_parm_handlers[] =
10539{
10540 x_set_autoraise,
10541 x_set_autolower,
10542 x_set_background_color,
10543 x_set_border_color,
10544 x_set_border_width,
10545 x_set_cursor_color,
10546 x_set_cursor_type,
10547 x_set_font,
10548 x_set_foreground_color,
10549 x_set_icon_name,
10550 x_set_icon_type,
10551 x_set_internal_border_width,
10552 x_set_menu_bar_lines,
10553 x_set_mouse_color,
10554 x_explicitly_set_name,
10555 x_set_scroll_bar_width,
10556 x_set_title,
10557 x_set_unsplittable,
10558 x_set_vertical_scroll_bars,
10559 x_set_visibility,
10560 x_set_tool_bar_lines,
10561 x_set_scroll_bar_foreground,
10562 x_set_scroll_bar_background,
10563 x_set_screen_gamma,
10564 x_set_line_spacing,
10565 x_set_fringe_width,
10566 x_set_fringe_width,
10567 x_set_wait_for_wm,
10568 x_set_fullscreen,
10569};
10570
333b20bb
GM
10571void
10572syms_of_xfns ()
10573{
10574 /* This is zero if not using X windows. */
10575 x_in_use = 0;
10576
10577 /* The section below is built by the lisp expression at the top of the file,
10578 just above where these variables are declared. */
10579 /*&&& init symbols here &&&*/
baaed68e
JB
10580 Qnone = intern ("none");
10581 staticpro (&Qnone);
8af1d7ca
JB
10582 Qsuppress_icon = intern ("suppress-icon");
10583 staticpro (&Qsuppress_icon);
01f1ba30 10584 Qundefined_color = intern ("undefined-color");
f9942c9e 10585 staticpro (&Qundefined_color);
7c7ff7f5
GM
10586 Qcenter = intern ("center");
10587 staticpro (&Qcenter);
96db09e4
KH
10588 Qcompound_text = intern ("compound-text");
10589 staticpro (&Qcompound_text);
ae782866
GM
10590 Qcancel_timer = intern ("cancel-timer");
10591 staticpro (&Qcancel_timer);
f9942c9e
JB
10592 /* This is the end of symbol initialization. */
10593
58cad5ed
KH
10594 /* Text property `display' should be nonsticky by default. */
10595 Vtext_property_default_nonsticky
10596 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
10597
10598
333b20bb
GM
10599 Qlaplace = intern ("laplace");
10600 staticpro (&Qlaplace);
4a8e312c
GM
10601 Qemboss = intern ("emboss");
10602 staticpro (&Qemboss);
10603 Qedge_detection = intern ("edge-detection");
10604 staticpro (&Qedge_detection);
10605 Qheuristic = intern ("heuristic");
10606 staticpro (&Qheuristic);
10607 QCmatrix = intern (":matrix");
10608 staticpro (&QCmatrix);
10609 QCcolor_adjustment = intern (":color-adjustment");
10610 staticpro (&QCcolor_adjustment);
10611 QCmask = intern (":mask");
10612 staticpro (&QCmask);
177c0ea7 10613
01f1ba30
JB
10614 Fput (Qundefined_color, Qerror_conditions,
10615 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
10616 Fput (Qundefined_color, Qerror_message,
10617 build_string ("Undefined color"));
10618
7ee72033
MB
10619 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
10620 doc: /* Non-nil means always draw a cross over disabled images.
c061c855
GM
10621Disabled images are those having an `:conversion disabled' property.
10622A cross is always drawn on black & white displays. */);
14819cb3
GM
10623 cross_disabled_images = 0;
10624
7ee72033 10625 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
c5903437 10626 doc: /* List of directories to search for window system bitmap files. */);
e241c09b 10627 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
f1c7b5a6 10628
7ee72033
MB
10629 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
10630 doc: /* The shape of the pointer when over text.
c061c855
GM
10631Changing the value does not affect existing frames
10632unless you set the mouse color. */);
01f1ba30
JB
10633 Vx_pointer_shape = Qnil;
10634
ca0ecbf5 10635#if 0 /* This doesn't really do anything. */
7ee72033
MB
10636 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
10637 doc: /* The shape of the pointer when not over text.
c061c855
GM
10638This variable takes effect when you create a new frame
10639or when you set the mouse color. */);
af01ef26 10640#endif
01f1ba30
JB
10641 Vx_nontext_pointer_shape = Qnil;
10642
7ee72033
MB
10643 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
10644 doc: /* The shape of the pointer when Emacs is busy.
c061c855
GM
10645This variable takes effect when you create a new frame
10646or when you set the mouse color. */);
0af913d7 10647 Vx_hourglass_pointer_shape = Qnil;
333b20bb 10648
7ee72033
MB
10649 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
10650 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
0af913d7 10651 display_hourglass_p = 1;
177c0ea7 10652
7ee72033
MB
10653 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
10654 doc: /* *Seconds to wait before displaying an hourglass pointer.
c061c855 10655Value must be an integer or float. */);
0af913d7 10656 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
4ae9a85e 10657
ca0ecbf5 10658#if 0 /* This doesn't really do anything. */
7ee72033
MB
10659 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
10660 doc: /* The shape of the pointer when over the mode line.
c061c855
GM
10661This variable takes effect when you create a new frame
10662or when you set the mouse color. */);
af01ef26 10663#endif
01f1ba30
JB
10664 Vx_mode_pointer_shape = Qnil;
10665
d3b06468 10666 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7ee72033
MB
10667 &Vx_sensitive_text_pointer_shape,
10668 doc: /* The shape of the pointer when over mouse-sensitive text.
c061c855
GM
10669This variable takes effect when you create a new frame
10670or when you set the mouse color. */);
ca0ecbf5 10671 Vx_sensitive_text_pointer_shape = Qnil;
95f80c78 10672
8fb4ec9c 10673 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7ee72033
MB
10674 &Vx_window_horizontal_drag_shape,
10675 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
c061c855
GM
10676This variable takes effect when you create a new frame
10677or when you set the mouse color. */);
8fb4ec9c
GM
10678 Vx_window_horizontal_drag_shape = Qnil;
10679
7ee72033
MB
10680 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
10681 doc: /* A string indicating the foreground color of the cursor box. */);
01f1ba30
JB
10682 Vx_cursor_fore_pixel = Qnil;
10683
7ee72033
MB
10684 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
10685 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
c061c855 10686Text larger than this is clipped. */);
d63931a2 10687 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
177c0ea7 10688
7ee72033
MB
10689 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
10690 doc: /* Non-nil if no X window manager is in use.
c061c855
GM
10691Emacs doesn't try to figure this out; this is always nil
10692unless you set it to something else. */);
2d38195d
RS
10693 /* We don't have any way to find this out, so set it to nil
10694 and maybe the user would like to set it to t. */
10695 Vx_no_window_manager = Qnil;
1d3dac41 10696
942ea06d 10697 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7ee72033
MB
10698 &Vx_pixel_size_width_font_regexp,
10699 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
c061c855
GM
10700
10701Since Emacs gets width of a font matching with this regexp from
10702PIXEL_SIZE field of the name, font finding mechanism gets faster for
10703such a font. This is especially effective for such large fonts as
10704Chinese, Japanese, and Korean. */);
942ea06d
KH
10705 Vx_pixel_size_width_font_regexp = Qnil;
10706
7ee72033
MB
10707 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
10708 doc: /* Time after which cached images are removed from the cache.
c061c855
GM
10709When an image has not been displayed this many seconds, remove it
10710from the image cache. Value must be an integer or nil with nil
10711meaning don't clear the cache. */);
fcf431dc 10712 Vimage_cache_eviction_delay = make_number (30 * 60);
333b20bb 10713
1d3dac41 10714#ifdef USE_X_TOOLKIT
6f3f6a8d 10715 Fprovide (intern ("x-toolkit"), Qnil);
5b827abb 10716#ifdef USE_MOTIF
6f3f6a8d 10717 Fprovide (intern ("motif"), Qnil);
fc2cdd9a 10718
7ee72033
MB
10719 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
10720 doc: /* Version info for LessTif/Motif. */);
fc2cdd9a
GM
10721 Vmotif_version_string = build_string (XmVERSION_STRING);
10722#endif /* USE_MOTIF */
10723#endif /* USE_X_TOOLKIT */
01f1ba30 10724
333b20bb
GM
10725 /* X window properties. */
10726 defsubr (&Sx_change_window_property);
10727 defsubr (&Sx_delete_window_property);
10728 defsubr (&Sx_window_property);
10729
2d764c78 10730 defsubr (&Sxw_display_color_p);
d0c9d219 10731 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
10732 defsubr (&Sxw_color_defined_p);
10733 defsubr (&Sxw_color_values);
9d317b2c 10734 defsubr (&Sx_server_max_request_size);
41beb8fc
RS
10735 defsubr (&Sx_server_vendor);
10736 defsubr (&Sx_server_version);
10737 defsubr (&Sx_display_pixel_width);
10738 defsubr (&Sx_display_pixel_height);
10739 defsubr (&Sx_display_mm_width);
10740 defsubr (&Sx_display_mm_height);
10741 defsubr (&Sx_display_screens);
10742 defsubr (&Sx_display_planes);
10743 defsubr (&Sx_display_color_cells);
10744 defsubr (&Sx_display_visual_class);
10745 defsubr (&Sx_display_backing_store);
10746 defsubr (&Sx_display_save_under);
f676886a 10747 defsubr (&Sx_create_frame);
01f1ba30 10748 defsubr (&Sx_open_connection);
08a90d6a
RS
10749 defsubr (&Sx_close_connection);
10750 defsubr (&Sx_display_list);
01f1ba30 10751 defsubr (&Sx_synchronize);
3decc1e7 10752 defsubr (&Sx_focus_frame);
82bab41c 10753 defsubr (&Sx_backspace_delete_keys_p);
177c0ea7 10754
942ea06d
KH
10755 /* Setting callback functions for fontset handler. */
10756 get_font_info_func = x_get_font_info;
333b20bb
GM
10757
10758#if 0 /* This function pointer doesn't seem to be used anywhere.
10759 And the pointer assigned has the wrong type, anyway. */
942ea06d 10760 list_fonts_func = x_list_fonts;
333b20bb 10761#endif
177c0ea7 10762
942ea06d 10763 load_font_func = x_load_font;
bc1958c4 10764 find_ccl_program_func = x_find_ccl_program;
942ea06d
KH
10765 query_font_func = x_query_font;
10766 set_frame_fontset_func = x_set_font;
10767 check_window_system_func = check_x;
333b20bb
GM
10768
10769 /* Images. */
10770 Qxbm = intern ("xbm");
10771 staticpro (&Qxbm);
d2dc8167
GM
10772 QCconversion = intern (":conversion");
10773 staticpro (&QCconversion);
333b20bb
GM
10774 QCheuristic_mask = intern (":heuristic-mask");
10775 staticpro (&QCheuristic_mask);
10776 QCcolor_symbols = intern (":color-symbols");
10777 staticpro (&QCcolor_symbols);
333b20bb
GM
10778 QCascent = intern (":ascent");
10779 staticpro (&QCascent);
10780 QCmargin = intern (":margin");
10781 staticpro (&QCmargin);
10782 QCrelief = intern (":relief");
10783 staticpro (&QCrelief);
fcf431dc
GM
10784 Qpostscript = intern ("postscript");
10785 staticpro (&Qpostscript);
333b20bb
GM
10786 QCloader = intern (":loader");
10787 staticpro (&QCloader);
10788 QCbounding_box = intern (":bounding-box");
10789 staticpro (&QCbounding_box);
10790 QCpt_width = intern (":pt-width");
10791 staticpro (&QCpt_width);
10792 QCpt_height = intern (":pt-height");
10793 staticpro (&QCpt_height);
3ccff1e3
GM
10794 QCindex = intern (":index");
10795 staticpro (&QCindex);
333b20bb
GM
10796 Qpbm = intern ("pbm");
10797 staticpro (&Qpbm);
10798
10799#if HAVE_XPM
10800 Qxpm = intern ("xpm");
10801 staticpro (&Qxpm);
10802#endif
177c0ea7 10803
333b20bb
GM
10804#if HAVE_JPEG
10805 Qjpeg = intern ("jpeg");
10806 staticpro (&Qjpeg);
177c0ea7 10807#endif
333b20bb
GM
10808
10809#if HAVE_TIFF
10810 Qtiff = intern ("tiff");
10811 staticpro (&Qtiff);
177c0ea7 10812#endif
333b20bb
GM
10813
10814#if HAVE_GIF
10815 Qgif = intern ("gif");
10816 staticpro (&Qgif);
10817#endif
10818
10819#if HAVE_PNG
10820 Qpng = intern ("png");
10821 staticpro (&Qpng);
10822#endif
10823
10824 defsubr (&Sclear_image_cache);
42677916 10825 defsubr (&Simage_size);
b243755a 10826 defsubr (&Simage_mask_p);
333b20bb 10827
0af913d7
GM
10828 hourglass_atimer = NULL;
10829 hourglass_shown_p = 0;
333b20bb
GM
10830
10831 defsubr (&Sx_show_tip);
10832 defsubr (&Sx_hide_tip);
333b20bb 10833 tip_timer = Qnil;
44b5a125
GM
10834 staticpro (&tip_timer);
10835 tip_frame = Qnil;
10836 staticpro (&tip_frame);
333b20bb 10837
06d62053
GM
10838 last_show_tip_args = Qnil;
10839 staticpro (&last_show_tip_args);
10840
333b20bb
GM
10841#ifdef USE_MOTIF
10842 defsubr (&Sx_file_dialog);
10843#endif
10844}
10845
10846
10847void
10848init_xfns ()
10849{
10850 image_types = NULL;
10851 Vimage_types = Qnil;
177c0ea7 10852
333b20bb
GM
10853 define_image_type (&xbm_type);
10854 define_image_type (&gs_type);
10855 define_image_type (&pbm_type);
177c0ea7 10856
333b20bb
GM
10857#if HAVE_XPM
10858 define_image_type (&xpm_type);
10859#endif
177c0ea7 10860
333b20bb
GM
10861#if HAVE_JPEG
10862 define_image_type (&jpeg_type);
10863#endif
177c0ea7 10864
333b20bb
GM
10865#if HAVE_TIFF
10866 define_image_type (&tiff_type);
10867#endif
177c0ea7 10868
333b20bb
GM
10869#if HAVE_GIF
10870 define_image_type (&gif_type);
10871#endif
177c0ea7 10872
333b20bb
GM
10873#if HAVE_PNG
10874 define_image_type (&png_type);
10875#endif
01f1ba30
JB
10876}
10877
10878#endif /* HAVE_X_WINDOWS */