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