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