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