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