(png_load): Avoid double gamma correction for PNG images.
[bpt/emacs.git] / src / xfns.c
CommitLineData
01f1ba30 1/* Functions for the X window system.
edf36fe6 2 Copyright (C) 1989, 92, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
333b20bb 3 Free Software Foundation.
01f1ba30
JB
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
1113d9db 9the Free Software Foundation; either version 2, or (at your option)
01f1ba30
JB
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
01f1ba30 21
c389a86d 22#include <config.h>
68c45bf0 23#include <signal.h>
333b20bb 24#include <stdio.h>
d62c8769 25#include <math.h>
c389a86d 26
3ecaf7e5
RS
27#ifdef HAVE_UNISTD_H
28#include <unistd.h>
29#endif
30
40e6f148 31/* This makes the fields of a Display accessible, in Xlib header files. */
333b20bb 32
40e6f148
RS
33#define XLIB_ILLEGAL_ACCESS
34
01f1ba30
JB
35#include "lisp.h"
36#include "xterm.h"
f676886a 37#include "frame.h"
01f1ba30
JB
38#include "window.h"
39#include "buffer.h"
58cad5ed 40#include "intervals.h"
01f1ba30 41#include "dispextern.h"
1f98fa48 42#include "keyboard.h"
9ac0d9e0 43#include "blockinput.h"
57bda87a 44#include <epaths.h>
942ea06d 45#include "charset.h"
96db09e4 46#include "coding.h"
942ea06d 47#include "fontset.h"
333b20bb
GM
48#include "systime.h"
49#include "termhooks.h"
4ae9a85e 50#include "atimer.h"
01f1ba30
JB
51
52#ifdef HAVE_X_WINDOWS
67ba84d1 53
67ba84d1 54#include <ctype.h>
63cec32f
GM
55#include <sys/types.h>
56#include <sys/stat.h>
01f1ba30 57
0a93081c 58#ifndef VMS
0505a740 59#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
ef493a27
RS
60#include "bitmaps/gray.xbm"
61#else
dbc4e1c1 62#include <X11/bitmaps/gray>
ef493a27 63#endif
0a93081c
JB
64#else
65#include "[.bitmaps]gray.xbm"
66#endif
dbc4e1c1 67
9ef48a9d
RS
68#ifdef USE_X_TOOLKIT
69#include <X11/Shell.h>
70
398ffa92 71#ifndef USE_MOTIF
9ef48a9d
RS
72#include <X11/Xaw/Paned.h>
73#include <X11/Xaw/Label.h>
398ffa92 74#endif /* USE_MOTIF */
9ef48a9d
RS
75
76#ifdef USG
77#undef USG /* ####KLUDGE for Solaris 2.2 and up */
78#include <X11/Xos.h>
79#define USG
80#else
81#include <X11/Xos.h>
82#endif
83
84#include "widget.h"
85
86#include "../lwlib/lwlib.h"
87
333b20bb
GM
88#ifdef USE_MOTIF
89#include <Xm/Xm.h>
90#include <Xm/DialogS.h>
91#include <Xm/FileSB.h>
92#endif
93
3b882b1d
RS
94/* Do the EDITRES protocol if running X11R5
95 Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
333b20bb 96
3b882b1d 97#if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
6c32dd68 98#define HACK_EDITRES
b9dc4443 99extern void _XEditResCheckMessages ();
6c32dd68
PR
100#endif /* R5 + Athena */
101
333b20bb
GM
102/* Unique id counter for widgets created by the Lucid Widget Library. */
103
6c32dd68
PR
104extern LWLIB_ID widget_id_tick;
105
e3881aa0 106#ifdef USE_LUCID
82c90203 107/* This is part of a kludge--see lwlib/xlwmenu.c. */
03e2c340 108extern XFontStruct *xlwmenu_default_font;
e3881aa0 109#endif
9ef48a9d 110
6bc20398 111extern void free_frame_menubar ();
d62c8769 112extern double atof ();
333b20bb 113
fc2cdd9a
GM
114#ifdef USE_MOTIF
115
116/* LessTif/Motif version info. */
117
118static Lisp_Object Vmotif_version_string;
119
120#endif /* USE_MOTIF */
121
9ef48a9d
RS
122#endif /* USE_X_TOOLKIT */
123
9d317b2c
RS
124#ifdef HAVE_X11R4
125#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
126#else
127#define MAXREQUEST(dpy) ((dpy)->max_request_size)
128#endif
129
333b20bb
GM
130/* The gray bitmap `bitmaps/gray'. This is done because xterm.c uses
131 it, and including `bitmaps/gray' more than once is a problem when
132 config.h defines `static' as an empty replacement string. */
133
134int gray_bitmap_width = gray_width;
135int gray_bitmap_height = gray_height;
62906360 136char *gray_bitmap_bits = gray_bits;
333b20bb 137
498e9ac3 138/* The name we're using in resource queries. Most often "emacs". */
333b20bb 139
d387c960 140Lisp_Object Vx_resource_name;
ac63d3d6 141
498e9ac3
RS
142/* The application class we're using in resource queries.
143 Normally "Emacs". */
333b20bb 144
498e9ac3
RS
145Lisp_Object Vx_resource_class;
146
0af913d7 147/* Non-zero means we're allowed to display an hourglass cursor. */
333b20bb 148
0af913d7 149int display_hourglass_p;
333b20bb 150
01f1ba30 151/* The background and shape of the mouse pointer, and shape when not
b9dc4443 152 over text or in the modeline. */
333b20bb 153
01f1ba30 154Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
0af913d7 155Lisp_Object Vx_hourglass_pointer_shape;
333b20bb 156
ca0ecbf5 157/* The shape when over mouse-sensitive text. */
333b20bb 158
ca0ecbf5 159Lisp_Object Vx_sensitive_text_pointer_shape;
01f1ba30 160
8fb4ec9c
GM
161/* If non-nil, the pointer shape to indicate that windows can be
162 dragged horizontally. */
163
164Lisp_Object Vx_window_horizontal_drag_shape;
165
b9dc4443 166/* Color of chars displayed in cursor box. */
333b20bb 167
01f1ba30
JB
168Lisp_Object Vx_cursor_fore_pixel;
169
b9dc4443 170/* Nonzero if using X. */
333b20bb 171
b9dc4443 172static int x_in_use;
01f1ba30 173
b9dc4443 174/* Non nil if no window manager is in use. */
333b20bb 175
01f1ba30
JB
176Lisp_Object Vx_no_window_manager;
177
f1c7b5a6 178/* Search path for bitmap files. */
333b20bb 179
f1c7b5a6
RS
180Lisp_Object Vx_bitmap_file_path;
181
942ea06d 182/* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
333b20bb 183
942ea06d
KH
184Lisp_Object Vx_pixel_size_width_font_regexp;
185
f9942c9e
JB
186Lisp_Object Qauto_raise;
187Lisp_Object Qauto_lower;
f9942c9e
JB
188Lisp_Object Qborder_color;
189Lisp_Object Qborder_width;
133cfefd 190extern Lisp_Object Qbox;
f9942c9e 191Lisp_Object Qcursor_color;
dbc4e1c1 192Lisp_Object Qcursor_type;
f9942c9e 193Lisp_Object Qgeometry;
f9942c9e
JB
194Lisp_Object Qicon_left;
195Lisp_Object Qicon_top;
196Lisp_Object Qicon_type;
80534dd6 197Lisp_Object Qicon_name;
f9942c9e
JB
198Lisp_Object Qinternal_border_width;
199Lisp_Object Qleft;
1ab3d87e 200Lisp_Object Qright;
f9942c9e 201Lisp_Object Qmouse_color;
baaed68e 202Lisp_Object Qnone;
2cbebefb 203Lisp_Object Qouter_window_id;
f9942c9e 204Lisp_Object Qparent_id;
4701395c 205Lisp_Object Qscroll_bar_width;
8af1d7ca 206Lisp_Object Qsuppress_icon;
333b20bb 207extern Lisp_Object Qtop;
01f1ba30 208Lisp_Object Qundefined_color;
a3c87d4e 209Lisp_Object Qvertical_scroll_bars;
49795535 210Lisp_Object Qvisibility;
f9942c9e 211Lisp_Object Qwindow_id;
f676886a 212Lisp_Object Qx_frame_parameter;
9ef48a9d 213Lisp_Object Qx_resource_name;
4fe1de12
RS
214Lisp_Object Quser_position;
215Lisp_Object Quser_size;
0cafb359 216extern Lisp_Object Qdisplay;
333b20bb 217Lisp_Object Qscroll_bar_foreground, Qscroll_bar_background;
7c7ff7f5 218Lisp_Object Qscreen_gamma, Qline_spacing, Qcenter;
ae782866 219Lisp_Object Qcompound_text, Qcancel_timer;
ea0a1f53 220Lisp_Object Qwait_for_wm;
49d41073
EZ
221Lisp_Object Qfullscreen;
222Lisp_Object Qfullwidth;
223Lisp_Object Qfullheight;
224Lisp_Object Qfullboth;
01f1ba30 225
b9dc4443 226/* The below are defined in frame.c. */
333b20bb 227
baaed68e 228extern Lisp_Object Qheight, Qminibuffer, Qname, Qonly, Qwidth;
c2304e02 229extern Lisp_Object Qunsplittable, Qmenu_bar_lines, Qbuffer_predicate, Qtitle;
9ea173e8 230extern Lisp_Object Qtool_bar_lines;
f9942c9e 231
01f1ba30
JB
232extern Lisp_Object Vwindow_system_version;
233
a367641f 234Lisp_Object Qface_set_after_frame_default;
333b20bb 235
f1d2ce7f 236#if GLYPH_DEBUG
eaf1eea9
GM
237int image_cache_refcount, dpyinfo_refcount;
238#endif
239
240
01f1ba30 241\f
11ae94fe 242/* Error if we are not connected to X. */
333b20bb 243
7fc9de26 244void
11ae94fe
RS
245check_x ()
246{
b9dc4443 247 if (! x_in_use)
11ae94fe
RS
248 error ("X windows are not in use or not initialized");
249}
250
1c59f5df
RS
251/* Nonzero if we can use mouse menus.
252 You should not call this unless HAVE_MENUS is defined. */
75cc8ee5
RS
253
254int
1c59f5df 255have_menus_p ()
75cc8ee5 256{
b9dc4443
RS
257 return x_in_use;
258}
259
260/* Extract a frame as a FRAME_PTR, defaulting to the selected frame
261 and checking validity for X. */
262
263FRAME_PTR
264check_x_frame (frame)
265 Lisp_Object frame;
266{
267 FRAME_PTR f;
268
269 if (NILP (frame))
0fe92f72 270 frame = selected_frame;
b7826503 271 CHECK_LIVE_FRAME (frame);
0fe92f72 272 f = XFRAME (frame);
b9dc4443 273 if (! FRAME_X_P (f))
1c59f5df 274 error ("Non-X frame used");
b9dc4443 275 return f;
75cc8ee5
RS
276}
277
b9dc4443
RS
278/* Let the user specify an X display with a frame.
279 nil stands for the selected frame--or, if that is not an X frame,
280 the first X display on the list. */
281
282static struct x_display_info *
283check_x_display_info (frame)
284 Lisp_Object frame;
285{
8ec8a5ec 286 struct x_display_info *dpyinfo = NULL;
7273d100 287
b9dc4443
RS
288 if (NILP (frame))
289 {
0fe92f72 290 struct frame *sf = XFRAME (selected_frame);
7273d100 291
0fe92f72 292 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
8ec8a5ec 293 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
b9dc4443 294 else if (x_display_list != 0)
8ec8a5ec 295 dpyinfo = x_display_list;
b9dc4443
RS
296 else
297 error ("X windows are not in use or not initialized");
298 }
299 else if (STRINGP (frame))
8ec8a5ec 300 dpyinfo = x_display_info_for_name (frame);
b9dc4443
RS
301 else
302 {
ba4c10fd 303 FRAME_PTR f = check_x_frame (frame);
8ec8a5ec 304 dpyinfo = FRAME_X_DISPLAY_INFO (f);
b9dc4443 305 }
8ec8a5ec
GM
306
307 return dpyinfo;
b9dc4443 308}
333b20bb 309
b9dc4443 310\f
f676886a
JB
311/* Return the Emacs frame-object corresponding to an X window.
312 It could be the frame's main window or an icon window. */
01f1ba30 313
34ca5317 314/* This function can be called during GC, so use GC_xxx type test macros. */
bcb2db92 315
f676886a 316struct frame *
2d271e2e
KH
317x_window_to_frame (dpyinfo, wdesc)
318 struct x_display_info *dpyinfo;
01f1ba30
JB
319 int wdesc;
320{
f676886a
JB
321 Lisp_Object tail, frame;
322 struct frame *f;
01f1ba30 323
8e713be6 324 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
01f1ba30 325 {
8e713be6 326 frame = XCAR (tail);
34ca5317 327 if (!GC_FRAMEP (frame))
01f1ba30 328 continue;
f676886a 329 f = XFRAME (frame);
2d764c78 330 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
2d271e2e 331 continue;
0af913d7 332 if (f->output_data.x->hourglass_window == wdesc)
17cbbf95 333 return f;
9ef48a9d 334#ifdef USE_X_TOOLKIT
7273d100 335 if ((f->output_data.x->edit_widget
7556890b 336 && XtWindow (f->output_data.x->edit_widget) == wdesc)
333b20bb
GM
337 /* A tooltip frame? */
338 || (!f->output_data.x->edit_widget
339 && FRAME_X_WINDOW (f) == wdesc)
7556890b 340 || f->output_data.x->icon_desc == wdesc)
9ef48a9d
RS
341 return f;
342#else /* not USE_X_TOOLKIT */
fe24a618 343 if (FRAME_X_WINDOW (f) == wdesc
7556890b 344 || f->output_data.x->icon_desc == wdesc)
f676886a 345 return f;
9ef48a9d
RS
346#endif /* not USE_X_TOOLKIT */
347 }
348 return 0;
349}
350
351#ifdef USE_X_TOOLKIT
352/* Like x_window_to_frame but also compares the window with the widget's
353 windows. */
354
355struct frame *
2d271e2e
KH
356x_any_window_to_frame (dpyinfo, wdesc)
357 struct x_display_info *dpyinfo;
9ef48a9d
RS
358 int wdesc;
359{
360 Lisp_Object tail, frame;
17cbbf95 361 struct frame *f, *found;
7556890b 362 struct x_output *x;
9ef48a9d 363
17cbbf95
GM
364 found = NULL;
365 for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
9ef48a9d 366 {
8e713be6 367 frame = XCAR (tail);
34ca5317 368 if (!GC_FRAMEP (frame))
9ef48a9d 369 continue;
7273d100 370
9ef48a9d 371 f = XFRAME (frame);
17cbbf95 372 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
333b20bb 373 {
17cbbf95
GM
374 /* This frame matches if the window is any of its widgets. */
375 x = f->output_data.x;
0af913d7 376 if (x->hourglass_window == wdesc)
17cbbf95
GM
377 found = f;
378 else if (x->widget)
379 {
7273d100
FP
380 if (wdesc == XtWindow (x->widget)
381 || wdesc == XtWindow (x->column_widget)
17cbbf95
GM
382 || wdesc == XtWindow (x->edit_widget))
383 found = f;
384 /* Match if the window is this frame's menubar. */
385 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
386 found = f;
387 }
388 else if (FRAME_X_WINDOW (f) == wdesc)
389 /* A tooltip frame. */
390 found = f;
333b20bb 391 }
01f1ba30 392 }
7273d100 393
17cbbf95 394 return found;
01f1ba30 395}
5e65b9ab 396
5fbc3f3a
KH
397/* Likewise, but exclude the menu bar widget. */
398
399struct frame *
400x_non_menubar_window_to_frame (dpyinfo, wdesc)
401 struct x_display_info *dpyinfo;
402 int wdesc;
403{
404 Lisp_Object tail, frame;
405 struct frame *f;
7556890b 406 struct x_output *x;
5fbc3f3a 407
8e713be6 408 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
5fbc3f3a 409 {
8e713be6 410 frame = XCAR (tail);
5fbc3f3a
KH
411 if (!GC_FRAMEP (frame))
412 continue;
413 f = XFRAME (frame);
2d764c78 414 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
5fbc3f3a 415 continue;
7556890b 416 x = f->output_data.x;
5fbc3f3a 417 /* This frame matches if the window is any of its widgets. */
0af913d7 418 if (x->hourglass_window == wdesc)
17cbbf95
GM
419 return f;
420 else if (x->widget)
333b20bb 421 {
7273d100
FP
422 if (wdesc == XtWindow (x->widget)
423 || wdesc == XtWindow (x->column_widget)
333b20bb
GM
424 || wdesc == XtWindow (x->edit_widget))
425 return f;
426 }
427 else if (FRAME_X_WINDOW (f) == wdesc)
428 /* A tooltip frame. */
5fbc3f3a
KH
429 return f;
430 }
431 return 0;
432}
433
fd3a3022
RS
434/* Likewise, but consider only the menu bar widget. */
435
436struct frame *
437x_menubar_window_to_frame (dpyinfo, wdesc)
438 struct x_display_info *dpyinfo;
439 int wdesc;
440{
441 Lisp_Object tail, frame;
442 struct frame *f;
7556890b 443 struct x_output *x;
fd3a3022 444
8e713be6 445 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
fd3a3022 446 {
8e713be6 447 frame = XCAR (tail);
fd3a3022
RS
448 if (!GC_FRAMEP (frame))
449 continue;
450 f = XFRAME (frame);
2d764c78 451 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
fd3a3022 452 continue;
7556890b 453 x = f->output_data.x;
fd3a3022 454 /* Match if the window is this frame's menubar. */
333b20bb
GM
455 if (x->menubar_widget
456 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
fd3a3022
RS
457 return f;
458 }
459 return 0;
460}
461
5e65b9ab
RS
462/* Return the frame whose principal (outermost) window is WDESC.
463 If WDESC is some other (smaller) window, we return 0. */
464
465struct frame *
2d271e2e
KH
466x_top_window_to_frame (dpyinfo, wdesc)
467 struct x_display_info *dpyinfo;
5e65b9ab
RS
468 int wdesc;
469{
470 Lisp_Object tail, frame;
471 struct frame *f;
7556890b 472 struct x_output *x;
5e65b9ab 473
8e713be6 474 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
5e65b9ab 475 {
8e713be6 476 frame = XCAR (tail);
34ca5317 477 if (!GC_FRAMEP (frame))
5e65b9ab
RS
478 continue;
479 f = XFRAME (frame);
2d764c78 480 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
2d271e2e 481 continue;
7556890b 482 x = f->output_data.x;
333b20bb
GM
483
484 if (x->widget)
485 {
486 /* This frame matches if the window is its topmost widget. */
487 if (wdesc == XtWindow (x->widget))
488 return f;
7a994728
KH
489#if 0 /* I don't know why it did this,
490 but it seems logically wrong,
491 and it causes trouble for MapNotify events. */
333b20bb 492 /* Match if the window is this frame's menubar. */
7273d100 493 if (x->menubar_widget
333b20bb
GM
494 && wdesc == XtWindow (x->menubar_widget))
495 return f;
7a994728 496#endif
333b20bb
GM
497 }
498 else if (FRAME_X_WINDOW (f) == wdesc)
499 /* Tooltip frame. */
500 return f;
5e65b9ab
RS
501 }
502 return 0;
503}
9ef48a9d 504#endif /* USE_X_TOOLKIT */
01f1ba30 505
01f1ba30 506\f
203c1d73
RS
507
508/* Code to deal with bitmaps. Bitmaps are referenced by their bitmap
509 id, which is just an int that this section returns. Bitmaps are
510 reference counted so they can be shared among frames.
511
512 Bitmap indices are guaranteed to be > 0, so a negative number can
513 be used to indicate no bitmap.
514
515 If you use x_create_bitmap_from_data, then you must keep track of
516 the bitmaps yourself. That is, creating a bitmap from the same
b9dc4443 517 data more than once will not be caught. */
203c1d73
RS
518
519
f1c7b5a6
RS
520/* Functions to access the contents of a bitmap, given an id. */
521
522int
523x_bitmap_height (f, id)
524 FRAME_PTR f;
525 int id;
526{
08a90d6a 527 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].height;
f1c7b5a6
RS
528}
529
530int
531x_bitmap_width (f, id)
532 FRAME_PTR f;
533 int id;
534{
08a90d6a 535 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].width;
f1c7b5a6
RS
536}
537
538int
539x_bitmap_pixmap (f, id)
540 FRAME_PTR f;
541 int id;
542{
08a90d6a 543 return FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].pixmap;
f1c7b5a6
RS
544}
545
546
203c1d73
RS
547/* Allocate a new bitmap record. Returns index of new record. */
548
549static int
08a90d6a
RS
550x_allocate_bitmap_record (f)
551 FRAME_PTR f;
203c1d73 552{
08a90d6a
RS
553 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
554 int i;
555
556 if (dpyinfo->bitmaps == NULL)
203c1d73 557 {
08a90d6a
RS
558 dpyinfo->bitmaps_size = 10;
559 dpyinfo->bitmaps
560 = (struct x_bitmap_record *) xmalloc (dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
561 dpyinfo->bitmaps_last = 1;
203c1d73
RS
562 return 1;
563 }
564
08a90d6a
RS
565 if (dpyinfo->bitmaps_last < dpyinfo->bitmaps_size)
566 return ++dpyinfo->bitmaps_last;
203c1d73 567
08a90d6a
RS
568 for (i = 0; i < dpyinfo->bitmaps_size; ++i)
569 if (dpyinfo->bitmaps[i].refcount == 0)
570 return i + 1;
203c1d73 571
08a90d6a
RS
572 dpyinfo->bitmaps_size *= 2;
573 dpyinfo->bitmaps
574 = (struct x_bitmap_record *) xrealloc (dpyinfo->bitmaps,
575 dpyinfo->bitmaps_size * sizeof (struct x_bitmap_record));
576 return ++dpyinfo->bitmaps_last;
203c1d73
RS
577}
578
579/* Add one reference to the reference count of the bitmap with id ID. */
580
581void
f1c7b5a6
RS
582x_reference_bitmap (f, id)
583 FRAME_PTR f;
203c1d73
RS
584 int id;
585{
08a90d6a 586 ++FRAME_X_DISPLAY_INFO (f)->bitmaps[id - 1].refcount;
203c1d73
RS
587}
588
589/* Create a bitmap for frame F from a HEIGHT x WIDTH array of bits at BITS. */
590
591int
592x_create_bitmap_from_data (f, bits, width, height)
593 struct frame *f;
594 char *bits;
595 unsigned int width, height;
596{
08a90d6a 597 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
203c1d73
RS
598 Pixmap bitmap;
599 int id;
600
b9dc4443 601 bitmap = XCreateBitmapFromData (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
203c1d73
RS
602 bits, width, height);
603
604 if (! bitmap)
605 return -1;
606
08a90d6a
RS
607 id = x_allocate_bitmap_record (f);
608 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
609 dpyinfo->bitmaps[id - 1].file = NULL;
610 dpyinfo->bitmaps[id - 1].refcount = 1;
611 dpyinfo->bitmaps[id - 1].depth = 1;
612 dpyinfo->bitmaps[id - 1].height = height;
613 dpyinfo->bitmaps[id - 1].width = width;
203c1d73
RS
614
615 return id;
616}
617
618/* Create bitmap from file FILE for frame F. */
619
620int
621x_create_bitmap_from_file (f, file)
622 struct frame *f;
f1c7b5a6 623 Lisp_Object file;
203c1d73 624{
08a90d6a 625 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
203c1d73
RS
626 unsigned int width, height;
627 Pixmap bitmap;
628 int xhot, yhot, result, id;
f1c7b5a6
RS
629 Lisp_Object found;
630 int fd;
631 char *filename;
203c1d73
RS
632
633 /* Look for an existing bitmap with the same name. */
08a90d6a 634 for (id = 0; id < dpyinfo->bitmaps_last; ++id)
203c1d73 635 {
08a90d6a
RS
636 if (dpyinfo->bitmaps[id].refcount
637 && dpyinfo->bitmaps[id].file
d5db4077 638 && !strcmp (dpyinfo->bitmaps[id].file, (char *) SDATA (file)))
203c1d73 639 {
08a90d6a 640 ++dpyinfo->bitmaps[id].refcount;
203c1d73
RS
641 return id + 1;
642 }
643 }
644
f1c7b5a6 645 /* Search bitmap-file-path for the file, if appropriate. */
de2413e9 646 fd = openp (Vx_bitmap_file_path, file, Qnil, &found, Qnil);
f1c7b5a6
RS
647 if (fd < 0)
648 return -1;
68c45bf0 649 emacs_close (fd);
f1c7b5a6 650
d5db4077 651 filename = (char *) SDATA (found);
f1c7b5a6 652
b9dc4443 653 result = XReadBitmapFile (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
f1c7b5a6 654 filename, &width, &height, &bitmap, &xhot, &yhot);
203c1d73
RS
655 if (result != BitmapSuccess)
656 return -1;
657
08a90d6a
RS
658 id = x_allocate_bitmap_record (f);
659 dpyinfo->bitmaps[id - 1].pixmap = bitmap;
660 dpyinfo->bitmaps[id - 1].refcount = 1;
9f2a85b2 661 dpyinfo->bitmaps[id - 1].file
d5db4077 662 = (char *) xmalloc (SBYTES (file) + 1);
08a90d6a
RS
663 dpyinfo->bitmaps[id - 1].depth = 1;
664 dpyinfo->bitmaps[id - 1].height = height;
665 dpyinfo->bitmaps[id - 1].width = width;
d5db4077 666 strcpy (dpyinfo->bitmaps[id - 1].file, SDATA (file));
203c1d73
RS
667
668 return id;
669}
670
671/* Remove reference to bitmap with id number ID. */
672
968b1234 673void
f1c7b5a6
RS
674x_destroy_bitmap (f, id)
675 FRAME_PTR f;
203c1d73
RS
676 int id;
677{
08a90d6a
RS
678 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
679
203c1d73
RS
680 if (id > 0)
681 {
08a90d6a
RS
682 --dpyinfo->bitmaps[id - 1].refcount;
683 if (dpyinfo->bitmaps[id - 1].refcount == 0)
203c1d73 684 {
ed662bdd 685 BLOCK_INPUT;
08a90d6a
RS
686 XFreePixmap (FRAME_X_DISPLAY (f), dpyinfo->bitmaps[id - 1].pixmap);
687 if (dpyinfo->bitmaps[id - 1].file)
203c1d73 688 {
333b20bb 689 xfree (dpyinfo->bitmaps[id - 1].file);
08a90d6a 690 dpyinfo->bitmaps[id - 1].file = NULL;
203c1d73 691 }
ed662bdd 692 UNBLOCK_INPUT;
203c1d73
RS
693 }
694 }
695}
696
08a90d6a 697/* Free all the bitmaps for the display specified by DPYINFO. */
203c1d73 698
08a90d6a
RS
699static void
700x_destroy_all_bitmaps (dpyinfo)
701 struct x_display_info *dpyinfo;
203c1d73 702{
08a90d6a
RS
703 int i;
704 for (i = 0; i < dpyinfo->bitmaps_last; i++)
705 if (dpyinfo->bitmaps[i].refcount > 0)
706 {
707 XFreePixmap (dpyinfo->display, dpyinfo->bitmaps[i].pixmap);
708 if (dpyinfo->bitmaps[i].file)
333b20bb 709 xfree (dpyinfo->bitmaps[i].file);
08a90d6a
RS
710 }
711 dpyinfo->bitmaps_last = 0;
203c1d73
RS
712}
713\f
f676886a 714/* Connect the frame-parameter names for X frames
01f1ba30
JB
715 to the ways of passing the parameter values to the window system.
716
717 The name of a parameter, as a Lisp symbol,
f676886a 718 has an `x-frame-parameter' property which is an integer in Lisp
9fb026ab 719 that is an index in this table. */
01f1ba30 720
f676886a 721struct x_frame_parm_table
01f1ba30
JB
722{
723 char *name;
d62c8769 724 void (*setter) P_ ((struct frame *, Lisp_Object, Lisp_Object));
01f1ba30
JB
725};
726
eaf1eea9
GM
727static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
728static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
52de7ce9 729static void x_change_window_heights P_ ((Lisp_Object, int));
14819cb3 730static void x_disable_image P_ ((struct frame *, struct image *));
d62c8769 731void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
563b67aa 732static void x_set_line_spacing P_ ((struct frame *, Lisp_Object, Lisp_Object));
ea0a1f53 733static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
49d41073 734static void x_set_fullscreen P_ ((struct frame *, Lisp_Object, Lisp_Object));
d62c8769
GM
735void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
736void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
737void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
738void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
739void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
740void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
741void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
b3ba0aa8 742static void x_set_fringe_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
d62c8769
GM
743void x_set_font P_ ((struct frame *, Lisp_Object, Lisp_Object));
744void x_set_border_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
745void x_set_internal_border_width P_ ((struct frame *, Lisp_Object,
746 Lisp_Object));
747void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
748void x_set_autoraise P_ ((struct frame *, Lisp_Object, Lisp_Object));
749void x_set_autolower P_ ((struct frame *, Lisp_Object, Lisp_Object));
750void x_set_vertical_scroll_bars P_ ((struct frame *, Lisp_Object,
751 Lisp_Object));
752void x_set_visibility P_ ((struct frame *, Lisp_Object, Lisp_Object));
753void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
754void x_set_scroll_bar_width P_ ((struct frame *, Lisp_Object, Lisp_Object));
755void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
756void x_set_unsplittable P_ ((struct frame *, Lisp_Object, Lisp_Object));
9ea173e8 757void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
333b20bb
GM
758void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
759 Lisp_Object));
760void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
761 Lisp_Object));
762static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
763 Lisp_Object,
764 Lisp_Object,
765 char *, char *,
766 int));
d62c8769 767static void x_set_screen_gamma P_ ((struct frame *, Lisp_Object, Lisp_Object));
4a8e312c
GM
768static void x_edge_detection P_ ((struct frame *, struct image *, Lisp_Object,
769 Lisp_Object));
b243755a
GM
770static void init_color_table P_ ((void));
771static void free_color_table P_ ((void));
772static unsigned long *colors_in_color_table P_ ((int *n));
773static unsigned long lookup_rgb_color P_ ((struct frame *f, int r, int g, int b));
774static unsigned long lookup_pixel_color P_ ((struct frame *f, unsigned long p));
775
776
01f1ba30 777
f676886a 778static struct x_frame_parm_table x_frame_parms[] =
01f1ba30 779{
9908a324
PJ
780 {"auto-raise", x_set_autoraise},
781 {"auto-lower", x_set_autolower},
782 {"background-color", x_set_background_color},
783 {"border-color", x_set_border_color},
784 {"border-width", x_set_border_width},
785 {"cursor-color", x_set_cursor_color},
786 {"cursor-type", x_set_cursor_type},
787 {"font", x_set_font},
788 {"foreground-color", x_set_foreground_color},
789 {"icon-name", x_set_icon_name},
790 {"icon-type", x_set_icon_type},
791 {"internal-border-width", x_set_internal_border_width},
792 {"menu-bar-lines", x_set_menu_bar_lines},
793 {"mouse-color", x_set_mouse_color},
794 {"name", x_explicitly_set_name},
795 {"scroll-bar-width", x_set_scroll_bar_width},
796 {"title", x_set_title},
797 {"unsplittable", x_set_unsplittable},
798 {"vertical-scroll-bars", x_set_vertical_scroll_bars},
799 {"visibility", x_set_visibility},
800 {"tool-bar-lines", x_set_tool_bar_lines},
801 {"scroll-bar-foreground", x_set_scroll_bar_foreground},
802 {"scroll-bar-background", x_set_scroll_bar_background},
803 {"screen-gamma", x_set_screen_gamma},
804 {"line-spacing", x_set_line_spacing},
805 {"left-fringe", x_set_fringe_width},
806 {"right-fringe", x_set_fringe_width},
49d41073
EZ
807 {"wait-for-wm", x_set_wait_for_wm},
808 {"fullscreen", x_set_fullscreen},
7273d100 809
01f1ba30
JB
810};
811
f676886a 812/* Attach the `x-frame-parameter' properties to
01f1ba30
JB
813 the Lisp symbol names of parameters relevant to X. */
814
201d8c78 815void
01f1ba30
JB
816init_x_parm_symbols ()
817{
818 int i;
819
d043f1a4 820 for (i = 0; i < sizeof (x_frame_parms) / sizeof (x_frame_parms[0]); i++)
f676886a 821 Fput (intern (x_frame_parms[i].name), Qx_frame_parameter,
01f1ba30
JB
822 make_number (i));
823}
824\f
49d41073
EZ
825
826/* Really try to move where we want to be in case of fullscreen. Some WMs
827 moves the window where we tell them. Some (mwm, twm) moves the outer
828 window manager window there instead.
829 Try to compensate for those WM here. */
830static void
831x_fullscreen_move (f, new_top, new_left)
832 struct frame *f;
833 int new_top;
834 int new_left;
835{
836 if (new_top != f->output_data.x->top_pos
837 || new_left != f->output_data.x->left_pos)
838 {
839 int move_x = new_left + f->output_data.x->x_pixels_outer_diff;
840 int move_y = new_top + f->output_data.x->y_pixels_outer_diff;
841
842 f->output_data.x->want_fullscreen |= FULLSCREEN_MOVE_WAIT;
843 x_set_offset (f, move_x, move_y, 1);
844 }
845}
846
e8cc313b 847/* Change the parameters of frame F as specified by ALIST.
64362cd4
GM
848 If a parameter is not specially recognized, do nothing special;
849 otherwise call the `x_set_...' function for that parameter.
850 Except for certain geometry properties, always call store_frame_param
851 to store the new value in the parameter alist. */
d043f1a4 852
f9942c9e
JB
853void
854x_set_frame_parameters (f, alist)
855 FRAME_PTR f;
856 Lisp_Object alist;
857{
858 Lisp_Object tail;
859
860 /* If both of these parameters are present, it's more efficient to
861 set them both at once. So we wait until we've looked at the
862 entire list before we set them. */
e4f79258 863 int width, height;
f9942c9e
JB
864
865 /* Same here. */
866 Lisp_Object left, top;
f9942c9e 867
a59e4f3d
RS
868 /* Same with these. */
869 Lisp_Object icon_left, icon_top;
870
f5e70acd
RS
871 /* Record in these vectors all the parms specified. */
872 Lisp_Object *parms;
873 Lisp_Object *values;
a797a73d 874 int i, p;
e1d962d7 875 int left_no_change = 0, top_no_change = 0;
a59e4f3d 876 int icon_left_no_change = 0, icon_top_no_change = 0;
5f9338d5 877 int fullscreen_is_being_set = 0;
203c1d73 878
7589a1d9
RS
879 struct gcpro gcpro1, gcpro2;
880
f5e70acd
RS
881 i = 0;
882 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
883 i++;
884
885 parms = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
886 values = (Lisp_Object *) alloca (i * sizeof (Lisp_Object));
f9942c9e 887
f5e70acd
RS
888 /* Extract parm names and values into those vectors. */
889
890 i = 0;
f9942c9e
JB
891 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
892 {
333b20bb 893 Lisp_Object elt;
f9942c9e
JB
894
895 elt = Fcar (tail);
f5e70acd
RS
896 parms[i] = Fcar (elt);
897 values[i] = Fcdr (elt);
898 i++;
899 }
7589a1d9
RS
900 /* TAIL and ALIST are not used again below here. */
901 alist = tail = Qnil;
902
903 GCPRO2 (*parms, *values);
904 gcpro1.nvars = i;
905 gcpro2.nvars = i;
f5e70acd 906
7589a1d9
RS
907 /* There is no need to gcpro LEFT, TOP, ICON_LEFT, or ICON_TOP,
908 because their values appear in VALUES and strings are not valid. */
e4f79258 909 top = left = Qunbound;
a59e4f3d 910 icon_left = icon_top = Qunbound;
f9942c9e 911
e4f79258
RS
912 /* Provide default values for HEIGHT and WIDTH. */
913 if (FRAME_NEW_WIDTH (f))
914 width = FRAME_NEW_WIDTH (f);
915 else
916 width = FRAME_WIDTH (f);
917
918 if (FRAME_NEW_HEIGHT (f))
919 height = FRAME_NEW_HEIGHT (f);
920 else
921 height = FRAME_HEIGHT (f);
922
a797a73d
GV
923 /* Process foreground_color and background_color before anything else.
924 They are independent of other properties, but other properties (e.g.,
925 cursor_color) are dependent upon them. */
b3ba0aa8 926 /* Process default font as well, since fringe widths depends on it. */
49d41073 927 /* Also, process fullscreen, width and height depend upon that */
7273d100 928 for (p = 0; p < i; p++)
a797a73d
GV
929 {
930 Lisp_Object prop, val;
931
932 prop = parms[p];
933 val = values[p];
b3ba0aa8
KS
934 if (EQ (prop, Qforeground_color)
935 || EQ (prop, Qbackground_color)
49d41073
EZ
936 || EQ (prop, Qfont)
937 || EQ (prop, Qfullscreen))
a797a73d
GV
938 {
939 register Lisp_Object param_index, old_value;
940
a797a73d 941 old_value = get_frame_param (f, prop);
f0b9a067 942 fullscreen_is_being_set |= EQ (prop, Qfullscreen);
7273d100 943
c7e609d5
MB
944 if (NILP (Fequal (val, old_value)))
945 {
946 store_frame_param (f, prop, val);
947
948 param_index = Fget (prop, Qx_frame_parameter);
949 if (NATNUMP (param_index)
950 && (XFASTINT (param_index)
951 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
952 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
953 }
a797a73d
GV
954 }
955 }
956
f5e70acd
RS
957 /* Now process them in reverse of specified order. */
958 for (i--; i >= 0; i--)
959 {
960 Lisp_Object prop, val;
961
962 prop = parms[i];
963 val = values[i];
964
e4f79258
RS
965 if (EQ (prop, Qwidth) && NUMBERP (val))
966 width = XFASTINT (val);
967 else if (EQ (prop, Qheight) && NUMBERP (val))
968 height = XFASTINT (val);
f5e70acd 969 else if (EQ (prop, Qtop))
f9942c9e 970 top = val;
f5e70acd 971 else if (EQ (prop, Qleft))
f9942c9e 972 left = val;
a59e4f3d
RS
973 else if (EQ (prop, Qicon_top))
974 icon_top = val;
975 else if (EQ (prop, Qicon_left))
976 icon_left = val;
b3ba0aa8
KS
977 else if (EQ (prop, Qforeground_color)
978 || EQ (prop, Qbackground_color)
49d41073
EZ
979 || EQ (prop, Qfont)
980 || EQ (prop, Qfullscreen))
a797a73d
GV
981 /* Processed above. */
982 continue;
f9942c9e
JB
983 else
984 {
98381190 985 register Lisp_Object param_index, old_value;
ea96210c 986
98381190 987 old_value = get_frame_param (f, prop);
c7e609d5 988
9f7e52b4 989 store_frame_param (f, prop, val);
c7e609d5 990
9f7e52b4
GM
991 param_index = Fget (prop, Qx_frame_parameter);
992 if (NATNUMP (param_index)
993 && (XFASTINT (param_index)
994 < sizeof (x_frame_parms)/sizeof (x_frame_parms[0])))
995 (*x_frame_parms[XINT (param_index)].setter)(f, val, old_value);
f9942c9e
JB
996 }
997 }
998
11378c41
RS
999 /* Don't die if just one of these was set. */
1000 if (EQ (left, Qunbound))
e1d962d7
RS
1001 {
1002 left_no_change = 1;
7556890b
RS
1003 if (f->output_data.x->left_pos < 0)
1004 left = Fcons (Qplus, Fcons (make_number (f->output_data.x->left_pos), Qnil));
e1d962d7 1005 else
7556890b 1006 XSETINT (left, f->output_data.x->left_pos);
e1d962d7 1007 }
11378c41 1008 if (EQ (top, Qunbound))
e1d962d7
RS
1009 {
1010 top_no_change = 1;
7556890b
RS
1011 if (f->output_data.x->top_pos < 0)
1012 top = Fcons (Qplus, Fcons (make_number (f->output_data.x->top_pos), Qnil));
e1d962d7 1013 else
7556890b 1014 XSETINT (top, f->output_data.x->top_pos);
e1d962d7 1015 }
11378c41 1016
a59e4f3d
RS
1017 /* If one of the icon positions was not set, preserve or default it. */
1018 if (EQ (icon_left, Qunbound) || ! INTEGERP (icon_left))
1019 {
1020 icon_left_no_change = 1;
1021 icon_left = Fcdr (Fassq (Qicon_left, f->param_alist));
1022 if (NILP (icon_left))
1023 XSETINT (icon_left, 0);
1024 }
1025 if (EQ (icon_top, Qunbound) || ! INTEGERP (icon_top))
1026 {
1027 icon_top_no_change = 1;
1028 icon_top = Fcdr (Fassq (Qicon_top, f->param_alist));
1029 if (NILP (icon_top))
1030 XSETINT (icon_top, 0);
1031 }
1032
5f9338d5 1033 if (FRAME_VISIBLE_P (f) && fullscreen_is_being_set)
49d41073
EZ
1034 {
1035 /* If the frame is visible already and the fullscreen parameter is
1036 being set, it is too late to set WM manager hints to specify
1037 size and position.
1038 Here we first get the width, height and position that applies to
1039 fullscreen. We then move the frame to the appropriate
1040 position. Resize of the frame is taken care of in the code after
5f9338d5 1041 this if-statement. */
49d41073 1042 int new_left, new_top;
7273d100 1043
49d41073
EZ
1044 x_fullscreen_adjust (f, &width, &height, &new_top, &new_left);
1045 x_fullscreen_move (f, new_top, new_left);
1046 }
7273d100 1047
499ea23b 1048 /* Don't set these parameters unless they've been explicitly
d387c960
JB
1049 specified. The window might be mapped or resized while we're in
1050 this function, and we don't want to override that unless the lisp
1051 code has asked for it.
1052
1053 Don't set these parameters unless they actually differ from the
1054 window's current parameters; the window may not actually exist
1055 yet. */
f9942c9e
JB
1056 {
1057 Lisp_Object frame;
1058
1f11a5ca
RS
1059 check_frame_size (f, &height, &width);
1060
191ed777 1061 XSETFRAME (frame, f);
11378c41 1062
e4f79258
RS
1063 if (width != FRAME_WIDTH (f)
1064 || height != FRAME_HEIGHT (f)
d6f80ae9 1065 || FRAME_NEW_HEIGHT (f) || FRAME_NEW_WIDTH (f))
e4f79258 1066 Fset_frame_size (frame, make_number (width), make_number (height));
f10f0b79
RS
1067
1068 if ((!NILP (left) || !NILP (top))
e1d962d7 1069 && ! (left_no_change && top_no_change)
7556890b
RS
1070 && ! (NUMBERP (left) && XINT (left) == f->output_data.x->left_pos
1071 && NUMBERP (top) && XINT (top) == f->output_data.x->top_pos))
f10f0b79 1072 {
e1d962d7
RS
1073 int leftpos = 0;
1074 int toppos = 0;
f10f0b79
RS
1075
1076 /* Record the signs. */
7556890b 1077 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
e1d962d7 1078 if (EQ (left, Qminus))
7556890b 1079 f->output_data.x->size_hint_flags |= XNegative;
e1d962d7
RS
1080 else if (INTEGERP (left))
1081 {
1082 leftpos = XINT (left);
1083 if (leftpos < 0)
7556890b 1084 f->output_data.x->size_hint_flags |= XNegative;
e1d962d7 1085 }
8e713be6
KR
1086 else if (CONSP (left) && EQ (XCAR (left), Qminus)
1087 && CONSP (XCDR (left))
1088 && INTEGERP (XCAR (XCDR (left))))
e1d962d7 1089 {
8e713be6 1090 leftpos = - XINT (XCAR (XCDR (left)));
7556890b 1091 f->output_data.x->size_hint_flags |= XNegative;
e1d962d7 1092 }
8e713be6
KR
1093 else if (CONSP (left) && EQ (XCAR (left), Qplus)
1094 && CONSP (XCDR (left))
1095 && INTEGERP (XCAR (XCDR (left))))
e1d962d7 1096 {
8e713be6 1097 leftpos = XINT (XCAR (XCDR (left)));
e1d962d7
RS
1098 }
1099
1100 if (EQ (top, Qminus))
7556890b 1101 f->output_data.x->size_hint_flags |= YNegative;
e1d962d7
RS
1102 else if (INTEGERP (top))
1103 {
1104 toppos = XINT (top);
1105 if (toppos < 0)
7556890b 1106 f->output_data.x->size_hint_flags |= YNegative;
e1d962d7 1107 }
8e713be6
KR
1108 else if (CONSP (top) && EQ (XCAR (top), Qminus)
1109 && CONSP (XCDR (top))
1110 && INTEGERP (XCAR (XCDR (top))))
e1d962d7 1111 {
8e713be6 1112 toppos = - XINT (XCAR (XCDR (top)));
7556890b 1113 f->output_data.x->size_hint_flags |= YNegative;
e1d962d7 1114 }
8e713be6
KR
1115 else if (CONSP (top) && EQ (XCAR (top), Qplus)
1116 && CONSP (XCDR (top))
1117 && INTEGERP (XCAR (XCDR (top))))
e1d962d7 1118 {
8e713be6 1119 toppos = XINT (XCAR (XCDR (top)));
e1d962d7
RS
1120 }
1121
1122
1123 /* Store the numeric value of the position. */
7556890b
RS
1124 f->output_data.x->top_pos = toppos;
1125 f->output_data.x->left_pos = leftpos;
e1d962d7 1126
7556890b 1127 f->output_data.x->win_gravity = NorthWestGravity;
f10f0b79
RS
1128
1129 /* Actually set that position, and convert to absolute. */
f0e72e79 1130 x_set_offset (f, leftpos, toppos, -1);
f10f0b79 1131 }
a59e4f3d
RS
1132
1133 if ((!NILP (icon_left) || !NILP (icon_top))
1134 && ! (icon_left_no_change && icon_top_no_change))
1135 x_wm_set_icon_position (f, XINT (icon_left), XINT (icon_top));
f9942c9e 1136 }
7589a1d9
RS
1137
1138 UNGCPRO;
f9942c9e 1139}
01f1ba30 1140
08a90d6a 1141/* Store the screen positions of frame F into XPTR and YPTR.
e9445337
RS
1142 These are the positions of the containing window manager window,
1143 not Emacs's own window. */
1144
1145void
1146x_real_positions (f, xptr, yptr)
1147 FRAME_PTR f;
1148 int *xptr, *yptr;
1149{
49d41073
EZ
1150 int win_x, win_y, outer_x, outer_y;
1151 int real_x = 0, real_y = 0;
1152 int had_errors = 0;
1153 Window win = f->output_data.x->parent_desc;
e9445337 1154
49d41073 1155 int count;
043835a3 1156
49d41073
EZ
1157 BLOCK_INPUT;
1158
1159 count = x_catch_errors (FRAME_X_DISPLAY (f));
043835a3 1160
49d41073
EZ
1161 if (win == FRAME_X_DISPLAY_INFO (f)->root_window)
1162 win = FRAME_OUTER_WINDOW (f);
1163
1164 /* This loop traverses up the containment tree until we hit the root
1165 window. Window managers may intersect many windows between our window
1166 and the root window. The window we find just before the root window
1167 should be the outer WM window. */
1168 for (;;)
e9445337 1169 {
49d41073
EZ
1170 Window wm_window, rootw;
1171 Window *tmp_children;
1172 unsigned int tmp_nchildren;
e7161ad9 1173 int success;
ca7bac79 1174
e7161ad9
RS
1175 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
1176 &wm_window, &tmp_children, &tmp_nchildren);
08a90d6a 1177
49d41073 1178 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
08a90d6a 1179
e7161ad9
RS
1180 /* Don't free tmp_children if XQueryTree failed. */
1181 if (! success)
1182 break;
1183
1184 XFree ((char *) tmp_children);
1185
49d41073
EZ
1186 if (wm_window == rootw || had_errors)
1187 break;
08a90d6a 1188
49d41073
EZ
1189 win = wm_window;
1190 }
7273d100 1191
49d41073
EZ
1192 if (! had_errors)
1193 {
1194 int ign;
1195 Window child, rootw;
7273d100 1196
49d41073
EZ
1197 /* Get the real coordinates for the WM window upper left corner */
1198 XGetGeometry (FRAME_X_DISPLAY (f), win,
1199 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
1200
1201 /* Translate real coordinates to coordinates relative to our
1202 window. For our window, the upper left corner is 0, 0.
1203 Since the upper left corner of the WM window is outside
1204 our window, win_x and win_y will be negative:
1205
1206 ------------------ ---> x
1207 | title |
1208 | ----------------- v y
1209 | | our window
1210 */
8a07bba0 1211 XTranslateCoordinates (FRAME_X_DISPLAY (f),
e9445337 1212
8a07bba0 1213 /* From-window, to-window. */
8a07bba0 1214 FRAME_X_DISPLAY_INFO (f)->root_window,
49d41073 1215 FRAME_X_WINDOW (f),
e9445337 1216
8a07bba0 1217 /* From-position, to-position. */
49d41073 1218 real_x, real_y, &win_x, &win_y,
08a90d6a 1219
8a07bba0
RS
1220 /* Child of win. */
1221 &child);
e9445337 1222
49d41073 1223 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
845e9d85 1224 {
49d41073
EZ
1225 outer_x = win_x;
1226 outer_y = win_y;
845e9d85 1227 }
49d41073
EZ
1228 else
1229 {
1230 XTranslateCoordinates (FRAME_X_DISPLAY (f),
ca7bac79 1231
49d41073
EZ
1232 /* From-window, to-window. */
1233 FRAME_X_DISPLAY_INFO (f)->root_window,
1234 FRAME_OUTER_WINDOW (f),
7273d100 1235
49d41073
EZ
1236 /* From-position, to-position. */
1237 real_x, real_y, &outer_x, &outer_y,
7273d100 1238
49d41073
EZ
1239 /* Child of win. */
1240 &child);
e9445337 1241 }
08a90d6a 1242
49d41073
EZ
1243 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
1244 }
7273d100 1245
49d41073 1246 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
7273d100 1247
49d41073
EZ
1248 UNBLOCK_INPUT;
1249
1250 if (had_errors) return;
7273d100 1251
49d41073
EZ
1252 f->output_data.x->x_pixels_diff = -win_x;
1253 f->output_data.x->y_pixels_diff = -win_y;
1254 f->output_data.x->x_pixels_outer_diff = -outer_x;
1255 f->output_data.x->y_pixels_outer_diff = -outer_y;
1256
1257 *xptr = real_x;
1258 *yptr = real_y;
e9445337
RS
1259}
1260
f676886a 1261/* Insert a description of internally-recorded parameters of frame X
01f1ba30
JB
1262 into the parameter alist *ALISTPTR that is to be given to the user.
1263 Only parameters that are specific to the X window system
f676886a 1264 and whose values are not correctly recorded in the frame's
01f1ba30
JB
1265 param_alist need to be considered here. */
1266
968b1234 1267void
f676886a
JB
1268x_report_frame_params (f, alistptr)
1269 struct frame *f;
01f1ba30
JB
1270 Lisp_Object *alistptr;
1271{
1272 char buf[16];
9b002b8d
KH
1273 Lisp_Object tem;
1274
1275 /* Represent negative positions (off the top or left screen edge)
1276 in a way that Fmodify_frame_parameters will understand correctly. */
7556890b
RS
1277 XSETINT (tem, f->output_data.x->left_pos);
1278 if (f->output_data.x->left_pos >= 0)
9b002b8d
KH
1279 store_in_alist (alistptr, Qleft, tem);
1280 else
1281 store_in_alist (alistptr, Qleft, Fcons (Qplus, Fcons (tem, Qnil)));
1282
7556890b
RS
1283 XSETINT (tem, f->output_data.x->top_pos);
1284 if (f->output_data.x->top_pos >= 0)
9b002b8d
KH
1285 store_in_alist (alistptr, Qtop, tem);
1286 else
1287 store_in_alist (alistptr, Qtop, Fcons (Qplus, Fcons (tem, Qnil)));
01f1ba30 1288
f9942c9e 1289 store_in_alist (alistptr, Qborder_width,
7556890b 1290 make_number (f->output_data.x->border_width));
f9942c9e 1291 store_in_alist (alistptr, Qinternal_border_width,
7556890b 1292 make_number (f->output_data.x->internal_border_width));
30bf44e0
KS
1293 store_in_alist (alistptr, Qleft_fringe,
1294 make_number (f->output_data.x->left_fringe_width));
1295 store_in_alist (alistptr, Qright_fringe,
1296 make_number (f->output_data.x->right_fringe_width));
99f7c77f 1297 store_in_alist (alistptr, Qscroll_bar_width,
6155205e
RS
1298 (! FRAME_HAS_VERTICAL_SCROLL_BARS (f)
1299 ? make_number (0)
1300 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
1301 ? make_number (FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
1302 /* nil means "use default width"
398101a6
RS
1303 for non-toolkit scroll bar.
1304 ruler-mode.el depends on this. */
6155205e 1305 : Qnil));
7c118b57 1306 sprintf (buf, "%ld", (long) FRAME_X_WINDOW (f));
f9942c9e 1307 store_in_alist (alistptr, Qwindow_id,
01f1ba30 1308 build_string (buf));
333b20bb
GM
1309#ifdef USE_X_TOOLKIT
1310 /* Tooltip frame may not have this widget. */
1311 if (f->output_data.x->widget)
1312#endif
1313 sprintf (buf, "%ld", (long) FRAME_OUTER_WINDOW (f));
2cbebefb
RS
1314 store_in_alist (alistptr, Qouter_window_id,
1315 build_string (buf));
f468da95 1316 store_in_alist (alistptr, Qicon_name, f->icon_name);
a8ccd803 1317 FRAME_SAMPLE_VISIBILITY (f);
d043f1a4
RS
1318 store_in_alist (alistptr, Qvisibility,
1319 (FRAME_VISIBLE_P (f) ? Qt
1320 : FRAME_ICONIFIED_P (f) ? Qicon : Qnil));
34ae77b5 1321 store_in_alist (alistptr, Qdisplay,
8e713be6 1322 XCAR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
e4f79258 1323
8c239ac3
RS
1324 if (f->output_data.x->parent_desc == FRAME_X_DISPLAY_INFO (f)->root_window)
1325 tem = Qnil;
1326 else
1327 XSETFASTINT (tem, f->output_data.x->parent_desc);
1328 store_in_alist (alistptr, Qparent_id, tem);
01f1ba30
JB
1329}
1330\f
82978295 1331
d62c8769
GM
1332
1333/* Gamma-correct COLOR on frame F. */
1334
1335void
1336gamma_correct (f, color)
1337 struct frame *f;
1338 XColor *color;
1339{
1340 if (f->gamma)
1341 {
1342 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
1343 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
1344 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
1345 }
1346}
1347
1348
7b746c38
GM
1349/* Decide if color named COLOR_NAME is valid for use on frame F. If
1350 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
1351 allocate the color. Value is zero if COLOR_NAME is invalid, or
1352 no color could be allocated. */
e12d55b2 1353
01f1ba30 1354int
7b746c38
GM
1355x_defined_color (f, color_name, color, alloc_p)
1356 struct frame *f;
1357 char *color_name;
1358 XColor *color;
1359 int alloc_p;
01f1ba30 1360{
7b746c38
GM
1361 int success_p;
1362 Display *dpy = FRAME_X_DISPLAY (f);
1363 Colormap cmap = FRAME_X_COLORMAP (f);
01f1ba30
JB
1364
1365 BLOCK_INPUT;
7b746c38
GM
1366 success_p = XParseColor (dpy, cmap, color_name, color);
1367 if (success_p && alloc_p)
1368 success_p = x_alloc_nearest_color (f, cmap, color);
01f1ba30
JB
1369 UNBLOCK_INPUT;
1370
7b746c38 1371 return success_p;
01f1ba30
JB
1372}
1373
9b2956e2
GM
1374
1375/* Return the pixel color value for color COLOR_NAME on frame F. If F
1376 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
1377 Signal an error if color can't be allocated. */
01f1ba30
JB
1378
1379int
9b2956e2 1380x_decode_color (f, color_name, mono_color)
b9dc4443 1381 FRAME_PTR f;
9b2956e2
GM
1382 Lisp_Object color_name;
1383 int mono_color;
01f1ba30 1384{
b9dc4443 1385 XColor cdef;
01f1ba30 1386
b7826503 1387 CHECK_STRING (color_name);
01f1ba30 1388
9b2956e2
GM
1389#if 0 /* Don't do this. It's wrong when we're not using the default
1390 colormap, it makes freeing difficult, and it's probably not
1391 an important optimization. */
d5db4077 1392 if (strcmp (SDATA (color_name), "black") == 0)
b9dc4443 1393 return BLACK_PIX_DEFAULT (f);
d5db4077 1394 else if (strcmp (SDATA (color_name), "white") == 0)
b9dc4443 1395 return WHITE_PIX_DEFAULT (f);
9b2956e2 1396#endif
01f1ba30 1397
9b2956e2 1398 /* Return MONO_COLOR for monochrome frames. */
b9dc4443 1399 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
9b2956e2 1400 return mono_color;
01f1ba30 1401
2d764c78 1402 /* x_defined_color is responsible for coping with failures
95626e11 1403 by looking for a near-miss. */
d5db4077 1404 if (x_defined_color (f, SDATA (color_name), &cdef, 1))
95626e11
RS
1405 return cdef.pixel;
1406
c301be26
GM
1407 Fsignal (Qerror, Fcons (build_string ("Undefined color"),
1408 Fcons (color_name, Qnil)));
1409 return 0;
01f1ba30 1410}
9b2956e2
GM
1411
1412
01f1ba30 1413\f
563b67aa
GM
1414/* Change the `line-spacing' frame parameter of frame F. OLD_VALUE is
1415 the previous value of that parameter, NEW_VALUE is the new value. */
1416
1417static void
1418x_set_line_spacing (f, new_value, old_value)
1419 struct frame *f;
1420 Lisp_Object new_value, old_value;
1421{
1422 if (NILP (new_value))
1423 f->extra_line_spacing = 0;
1424 else if (NATNUMP (new_value))
1425 f->extra_line_spacing = XFASTINT (new_value);
1426 else
1a948b17 1427 Fsignal (Qerror, Fcons (build_string ("Invalid line-spacing"),
563b67aa
GM
1428 Fcons (new_value, Qnil)));
1429 if (FRAME_VISIBLE_P (f))
1430 redraw_frame (f);
1431}
1432
1433
ea0a1f53
GM
1434/* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
1435 the previous value of that parameter, NEW_VALUE is the new value.
1436 See also the comment of wait_for_wm in struct x_output. */
1437
1438static void
1439x_set_wait_for_wm (f, new_value, old_value)
1440 struct frame *f;
1441 Lisp_Object new_value, old_value;
1442{
1443 f->output_data.x->wait_for_wm = !NILP (new_value);
1444}
1445
1446
49d41073
EZ
1447/* Change the `fullscreen' frame parameter of frame F. OLD_VALUE is
1448 the previous value of that parameter, NEW_VALUE is the new value. */
1449
1450static void
1451x_set_fullscreen (f, new_value, old_value)
1452 struct frame *f;
1453 Lisp_Object new_value, old_value;
1454{
1455 if (NILP (new_value))
1456 f->output_data.x->want_fullscreen = FULLSCREEN_NONE;
1457 else if (EQ (new_value, Qfullboth))
1458 f->output_data.x->want_fullscreen = FULLSCREEN_BOTH;
1459 else if (EQ (new_value, Qfullwidth))
1460 f->output_data.x->want_fullscreen = FULLSCREEN_WIDTH;
1461 else if (EQ (new_value, Qfullheight))
1462 f->output_data.x->want_fullscreen = FULLSCREEN_HEIGHT;
1463}
1464
1465
d62c8769 1466/* Change the `screen-gamma' frame parameter of frame F. OLD_VALUE is
ea0a1f53
GM
1467 the previous value of that parameter, NEW_VALUE is the new
1468 value. */
d62c8769
GM
1469
1470static void
1471x_set_screen_gamma (f, new_value, old_value)
1472 struct frame *f;
1473 Lisp_Object new_value, old_value;
1474{
1475 if (NILP (new_value))
1476 f->gamma = 0;
1477 else if (NUMBERP (new_value) && XFLOATINT (new_value) > 0)
1478 /* The value 0.4545 is the normal viewing gamma. */
1479 f->gamma = 1.0 / (0.4545 * XFLOATINT (new_value));
1480 else
1a948b17 1481 Fsignal (Qerror, Fcons (build_string ("Invalid screen-gamma"),
d62c8769
GM
1482 Fcons (new_value, Qnil)));
1483
1484 clear_face_cache (0);
1485}
1486
1487
f676886a 1488/* Functions called only from `x_set_frame_param'
01f1ba30
JB
1489 to set individual parameters.
1490
fe24a618 1491 If FRAME_X_WINDOW (f) is 0,
f676886a 1492 the frame is being created and its X-window does not exist yet.
01f1ba30
JB
1493 In that case, just record the parameter's new value
1494 in the standard place; do not attempt to change the window. */
1495
1496void
f676886a
JB
1497x_set_foreground_color (f, arg, oldval)
1498 struct frame *f;
01f1ba30
JB
1499 Lisp_Object arg, oldval;
1500{
09393d07
GM
1501 struct x_output *x = f->output_data.x;
1502 unsigned long fg, old_fg;
a76206dc 1503
09393d07
GM
1504 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1505 old_fg = x->foreground_pixel;
1506 x->foreground_pixel = fg;
a76206dc 1507
fe24a618 1508 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 1509 {
09393d07 1510 Display *dpy = FRAME_X_DISPLAY (f);
7273d100 1511
09393d07
GM
1512 BLOCK_INPUT;
1513 XSetForeground (dpy, x->normal_gc, fg);
1514 XSetBackground (dpy, x->reverse_gc, fg);
36d42089 1515
09393d07
GM
1516 if (x->cursor_pixel == old_fg)
1517 {
1518 unload_color (f, x->cursor_pixel);
1519 x->cursor_pixel = x_copy_color (f, fg);
1520 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
1521 }
7273d100 1522
01f1ba30 1523 UNBLOCK_INPUT;
7273d100 1524
05c8abbe 1525 update_face_from_frame_parameter (f, Qforeground_color, arg);
7273d100 1526
179956b9 1527 if (FRAME_VISIBLE_P (f))
f676886a 1528 redraw_frame (f);
01f1ba30 1529 }
7273d100 1530
09393d07 1531 unload_color (f, old_fg);
01f1ba30
JB
1532}
1533
1534void
f676886a
JB
1535x_set_background_color (f, arg, oldval)
1536 struct frame *f;
01f1ba30
JB
1537 Lisp_Object arg, oldval;
1538{
09393d07
GM
1539 struct x_output *x = f->output_data.x;
1540 unsigned long bg;
01f1ba30 1541
09393d07
GM
1542 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
1543 unload_color (f, x->background_pixel);
1544 x->background_pixel = bg;
a76206dc 1545
fe24a618 1546 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 1547 {
09393d07 1548 Display *dpy = FRAME_X_DISPLAY (f);
7273d100 1549
09393d07
GM
1550 BLOCK_INPUT;
1551 XSetBackground (dpy, x->normal_gc, bg);
1552 XSetForeground (dpy, x->reverse_gc, bg);
1553 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
1554 XSetForeground (dpy, x->cursor_gc, bg);
1555
f76e0368
GM
1556#ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
1557 toolkit scroll bars. */
1558 {
1559 Lisp_Object bar;
1560 for (bar = FRAME_SCROLL_BARS (f);
1561 !NILP (bar);
1562 bar = XSCROLL_BAR (bar)->next)
1563 {
1564 Window window = SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar));
1565 XSetWindowBackground (dpy, window, bg);
1566 }
1567 }
1568#endif /* USE_TOOLKIT_SCROLL_BARS */
01f1ba30 1569
09393d07 1570 UNBLOCK_INPUT;
05c8abbe 1571 update_face_from_frame_parameter (f, Qbackground_color, arg);
ea96210c 1572
179956b9 1573 if (FRAME_VISIBLE_P (f))
f676886a 1574 redraw_frame (f);
01f1ba30
JB
1575 }
1576}
1577
1578void
f676886a
JB
1579x_set_mouse_color (f, arg, oldval)
1580 struct frame *f;
01f1ba30
JB
1581 Lisp_Object arg, oldval;
1582{
09393d07
GM
1583 struct x_output *x = f->output_data.x;
1584 Display *dpy = FRAME_X_DISPLAY (f);
95f80c78 1585 Cursor cursor, nontext_cursor, mode_cursor, cross_cursor;
0af913d7 1586 Cursor hourglass_cursor, horizontal_drag_cursor;
1dc6cfa6 1587 int count;
51a1d2d8 1588 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
09393d07 1589 unsigned long mask_color = x->background_pixel;
a76206dc 1590
51a1d2d8 1591 /* Don't let pointers be invisible. */
09393d07 1592 if (mask_color == pixel)
bcf26b38
GM
1593 {
1594 x_free_colors (f, &pixel, 1);
09393d07 1595 pixel = x_copy_color (f, x->foreground_pixel);
bcf26b38 1596 }
a76206dc 1597
09393d07
GM
1598 unload_color (f, x->mouse_pixel);
1599 x->mouse_pixel = pixel;
01f1ba30
JB
1600
1601 BLOCK_INPUT;
fe24a618 1602
eb8c3be9 1603 /* It's not okay to crash if the user selects a screwy cursor. */
09393d07 1604 count = x_catch_errors (dpy);
fe24a618 1605
09393d07 1606 if (!NILP (Vx_pointer_shape))
01f1ba30 1607 {
b7826503 1608 CHECK_NUMBER (Vx_pointer_shape);
09393d07 1609 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
01f1ba30
JB
1610 }
1611 else
09393d07
GM
1612 cursor = XCreateFontCursor (dpy, XC_xterm);
1613 x_check_errors (dpy, "bad text pointer cursor: %s");
01f1ba30 1614
09393d07 1615 if (!NILP (Vx_nontext_pointer_shape))
01f1ba30 1616 {
b7826503 1617 CHECK_NUMBER (Vx_nontext_pointer_shape);
09393d07
GM
1618 nontext_cursor
1619 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
01f1ba30
JB
1620 }
1621 else
09393d07
GM
1622 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
1623 x_check_errors (dpy, "bad nontext pointer cursor: %s");
01f1ba30 1624
09393d07 1625 if (!NILP (Vx_hourglass_pointer_shape))
333b20bb 1626 {
b7826503 1627 CHECK_NUMBER (Vx_hourglass_pointer_shape);
09393d07
GM
1628 hourglass_cursor
1629 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
333b20bb
GM
1630 }
1631 else
09393d07
GM
1632 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
1633 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
7273d100 1634
09393d07
GM
1635 x_check_errors (dpy, "bad nontext pointer cursor: %s");
1636 if (!NILP (Vx_mode_pointer_shape))
01f1ba30 1637 {
b7826503 1638 CHECK_NUMBER (Vx_mode_pointer_shape);
09393d07 1639 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
01f1ba30
JB
1640 }
1641 else
09393d07
GM
1642 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
1643 x_check_errors (dpy, "bad modeline pointer cursor: %s");
95f80c78 1644
09393d07 1645 if (!NILP (Vx_sensitive_text_pointer_shape))
95f80c78 1646 {
b7826503 1647 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
ca0ecbf5 1648 cross_cursor
09393d07 1649 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
95f80c78
FP
1650 }
1651 else
5d449b17 1652 cross_cursor = XCreateFontCursor (dpy, XC_hand2);
01f1ba30 1653
8fb4ec9c
GM
1654 if (!NILP (Vx_window_horizontal_drag_shape))
1655 {
b7826503 1656 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
8fb4ec9c 1657 horizontal_drag_cursor
09393d07 1658 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
8fb4ec9c
GM
1659 }
1660 else
1661 horizontal_drag_cursor
09393d07 1662 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
8fb4ec9c 1663
fe24a618 1664 /* Check and report errors with the above calls. */
09393d07
GM
1665 x_check_errors (dpy, "can't set cursor shape: %s");
1666 x_uncatch_errors (dpy, count);
fe24a618 1667
01f1ba30
JB
1668 {
1669 XColor fore_color, back_color;
1670
09393d07 1671 fore_color.pixel = x->mouse_pixel;
a31fedb7 1672 x_query_color (f, &fore_color);
01f1ba30 1673 back_color.pixel = mask_color;
a31fedb7 1674 x_query_color (f, &back_color);
7273d100 1675
09393d07
GM
1676 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
1677 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
1678 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
1679 XRecolorCursor (dpy, cross_cursor, &fore_color, &back_color);
1680 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
1681 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
01f1ba30 1682 }
01f1ba30 1683
fe24a618 1684 if (FRAME_X_WINDOW (f) != 0)
09393d07
GM
1685 XDefineCursor (dpy, FRAME_X_WINDOW (f), cursor);
1686
1687 if (cursor != x->text_cursor
1688 && x->text_cursor != 0)
1689 XFreeCursor (dpy, x->text_cursor);
1690 x->text_cursor = cursor;
1691
1692 if (nontext_cursor != x->nontext_cursor
1693 && x->nontext_cursor != 0)
1694 XFreeCursor (dpy, x->nontext_cursor);
1695 x->nontext_cursor = nontext_cursor;
1696
1697 if (hourglass_cursor != x->hourglass_cursor
1698 && x->hourglass_cursor != 0)
1699 XFreeCursor (dpy, x->hourglass_cursor);
1700 x->hourglass_cursor = hourglass_cursor;
1701
1702 if (mode_cursor != x->modeline_cursor
1703 && x->modeline_cursor != 0)
1704 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
1705 x->modeline_cursor = mode_cursor;
7273d100 1706
09393d07
GM
1707 if (cross_cursor != x->cross_cursor
1708 && x->cross_cursor != 0)
1709 XFreeCursor (dpy, x->cross_cursor);
1710 x->cross_cursor = cross_cursor;
01f1ba30 1711
09393d07
GM
1712 if (horizontal_drag_cursor != x->horizontal_drag_cursor
1713 && x->horizontal_drag_cursor != 0)
1714 XFreeCursor (dpy, x->horizontal_drag_cursor);
1715 x->horizontal_drag_cursor = horizontal_drag_cursor;
8fb4ec9c 1716
09393d07 1717 XFlush (dpy);
01f1ba30 1718 UNBLOCK_INPUT;
05c8abbe
GM
1719
1720 update_face_from_frame_parameter (f, Qmouse_color, arg);
01f1ba30
JB
1721}
1722
1723void
f676886a
JB
1724x_set_cursor_color (f, arg, oldval)
1725 struct frame *f;
01f1ba30
JB
1726 Lisp_Object arg, oldval;
1727{
a76206dc 1728 unsigned long fore_pixel, pixel;
10168ebb 1729 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
09393d07 1730 struct x_output *x = f->output_data.x;
01f1ba30 1731
10168ebb
GM
1732 if (!NILP (Vx_cursor_fore_pixel))
1733 {
1734 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1735 WHITE_PIX_DEFAULT (f));
1736 fore_pixel_allocated_p = 1;
1737 }
01f1ba30 1738 else
09393d07 1739 fore_pixel = x->background_pixel;
7273d100 1740
a76206dc 1741 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
10168ebb 1742 pixel_allocated_p = 1;
a76206dc 1743
f9942c9e 1744 /* Make sure that the cursor color differs from the background color. */
09393d07 1745 if (pixel == x->background_pixel)
01f1ba30 1746 {
10168ebb
GM
1747 if (pixel_allocated_p)
1748 {
1749 x_free_colors (f, &pixel, 1);
1750 pixel_allocated_p = 0;
1751 }
7273d100 1752
09393d07 1753 pixel = x->mouse_pixel;
a76206dc 1754 if (pixel == fore_pixel)
10168ebb
GM
1755 {
1756 if (fore_pixel_allocated_p)
1757 {
1758 x_free_colors (f, &fore_pixel, 1);
1759 fore_pixel_allocated_p = 0;
1760 }
09393d07 1761 fore_pixel = x->background_pixel;
10168ebb 1762 }
01f1ba30 1763 }
a76206dc 1764
09393d07 1765 unload_color (f, x->cursor_foreground_pixel);
10168ebb
GM
1766 if (!fore_pixel_allocated_p)
1767 fore_pixel = x_copy_color (f, fore_pixel);
09393d07 1768 x->cursor_foreground_pixel = fore_pixel;
01f1ba30 1769
09393d07 1770 unload_color (f, x->cursor_pixel);
10168ebb
GM
1771 if (!pixel_allocated_p)
1772 pixel = x_copy_color (f, pixel);
09393d07 1773 x->cursor_pixel = pixel;
a76206dc 1774
fe24a618 1775 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 1776 {
01f1ba30 1777 BLOCK_INPUT;
09393d07
GM
1778 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1779 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
01f1ba30 1780 UNBLOCK_INPUT;
01f1ba30 1781
179956b9 1782 if (FRAME_VISIBLE_P (f))
01f1ba30 1783 {
cedadcfa
RS
1784 x_update_cursor (f, 0);
1785 x_update_cursor (f, 1);
01f1ba30
JB
1786 }
1787 }
05c8abbe
GM
1788
1789 update_face_from_frame_parameter (f, Qcursor_color, arg);
01f1ba30 1790}
943b580d 1791\f
f676886a 1792/* Set the border-color of frame F to value described by ARG.
01f1ba30
JB
1793 ARG can be a string naming a color.
1794 The border-color is used for the border that is drawn by the X server.
1795 Note that this does not fully take effect if done before
f676886a 1796 F has an x-window; it must be redone when the window is created.
01f1ba30
JB
1797
1798 Note: this is done in two routines because of the way X10 works.
1799
1800 Note: under X11, this is normally the province of the window manager,
b9dc4443 1801 and so emacs' border colors may be overridden. */
01f1ba30
JB
1802
1803void
f676886a
JB
1804x_set_border_color (f, arg, oldval)
1805 struct frame *f;
01f1ba30
JB
1806 Lisp_Object arg, oldval;
1807{
01f1ba30
JB
1808 int pix;
1809
b7826503 1810 CHECK_STRING (arg);
b9dc4443 1811 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
f676886a 1812 x_set_border_pixel (f, pix);
05c8abbe 1813 update_face_from_frame_parameter (f, Qborder_color, arg);
01f1ba30
JB
1814}
1815
f676886a 1816/* Set the border-color of frame F to pixel value PIX.
01f1ba30 1817 Note that this does not fully take effect if done before
f676886a 1818 F has an x-window. */
01f1ba30 1819
968b1234 1820void
f676886a
JB
1821x_set_border_pixel (f, pix)
1822 struct frame *f;
01f1ba30
JB
1823 int pix;
1824{
a76206dc 1825 unload_color (f, f->output_data.x->border_pixel);
7556890b 1826 f->output_data.x->border_pixel = pix;
01f1ba30 1827
7556890b 1828 if (FRAME_X_WINDOW (f) != 0 && f->output_data.x->border_width > 0)
01f1ba30 1829 {
01f1ba30 1830 BLOCK_INPUT;
b9dc4443 1831 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
270958e8 1832 (unsigned long)pix);
01f1ba30
JB
1833 UNBLOCK_INPUT;
1834
179956b9 1835 if (FRAME_VISIBLE_P (f))
f676886a 1836 redraw_frame (f);
01f1ba30
JB
1837 }
1838}
1839
0d1469d6 1840
0d1469d6
GM
1841
1842void
1843x_set_cursor_type (f, arg, oldval)
1844 FRAME_PTR f;
1845 Lisp_Object arg, oldval;
1846{
33b2311e 1847 set_frame_cursor_types (f, arg);
dbc4e1c1 1848
75691005
RS
1849 /* Make sure the cursor gets redrawn. */
1850 cursor_type_changed = 1;
dbc4e1c1 1851}
943b580d 1852\f
01f1ba30 1853void
f676886a
JB
1854x_set_icon_type (f, arg, oldval)
1855 struct frame *f;
01f1ba30
JB
1856 Lisp_Object arg, oldval;
1857{
01f1ba30
JB
1858 int result;
1859
203c1d73
RS
1860 if (STRINGP (arg))
1861 {
1862 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1863 return;
1864 }
1865 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
01f1ba30
JB
1866 return;
1867
1868 BLOCK_INPUT;
265a9e55 1869 if (NILP (arg))
80534dd6 1870 result = x_text_icon (f,
d5db4077 1871 (char *) SDATA ((!NILP (f->icon_name)
f468da95 1872 ? f->icon_name
d5db4077 1873 : f->name)));
f1c7b5a6
RS
1874 else
1875 result = x_bitmap_icon (f, arg);
01f1ba30
JB
1876
1877 if (result)
1878 {
01f1ba30 1879 UNBLOCK_INPUT;
0fb53770 1880 error ("No icon window available");
01f1ba30
JB
1881 }
1882
b9dc4443 1883 XFlush (FRAME_X_DISPLAY (f));
01f1ba30
JB
1884 UNBLOCK_INPUT;
1885}
1886
f1c7b5a6 1887/* Return non-nil if frame F wants a bitmap icon. */
0fb53770 1888
f1c7b5a6 1889Lisp_Object
0fb53770
RS
1890x_icon_type (f)
1891 FRAME_PTR f;
1892{
1893 Lisp_Object tem;
1894
1895 tem = assq_no_quit (Qicon_type, f->param_alist);
f1c7b5a6 1896 if (CONSP (tem))
8e713be6 1897 return XCDR (tem);
f1c7b5a6
RS
1898 else
1899 return Qnil;
0fb53770
RS
1900}
1901
80534dd6
KH
1902void
1903x_set_icon_name (f, arg, oldval)
1904 struct frame *f;
1905 Lisp_Object arg, oldval;
1906{
80534dd6
KH
1907 int result;
1908
1909 if (STRINGP (arg))
1910 {
1911 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1912 return;
1913 }
1914 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1915 return;
1916
f468da95 1917 f->icon_name = arg;
80534dd6 1918
7556890b 1919 if (f->output_data.x->icon_bitmap != 0)
80534dd6
KH
1920 return;
1921
1922 BLOCK_INPUT;
1923
1924 result = x_text_icon (f,
d5db4077
KR
1925 (char *) SDATA ((!NILP (f->icon_name)
1926 ? f->icon_name
1927 : !NILP (f->title)
1928 ? f->title
1929 : f->name)));
80534dd6
KH
1930
1931 if (result)
1932 {
1933 UNBLOCK_INPUT;
1934 error ("No icon window available");
1935 }
1936
80534dd6
KH
1937 XFlush (FRAME_X_DISPLAY (f));
1938 UNBLOCK_INPUT;
1939}
943b580d 1940\f
01f1ba30 1941void
f676886a
JB
1942x_set_font (f, arg, oldval)
1943 struct frame *f;
01f1ba30
JB
1944 Lisp_Object arg, oldval;
1945{
ea96210c 1946 Lisp_Object result;
942ea06d 1947 Lisp_Object fontset_name;
a367641f 1948 Lisp_Object frame;
57c5889c 1949 int old_fontset = f->output_data.x->fontset;
01f1ba30 1950
b7826503 1951 CHECK_STRING (arg);
01f1ba30 1952
49965a29 1953 fontset_name = Fquery_fontset (arg, Qnil);
942ea06d 1954
01f1ba30 1955 BLOCK_INPUT;
942ea06d 1956 result = (STRINGP (fontset_name)
d5db4077
KR
1957 ? x_new_fontset (f, SDATA (fontset_name))
1958 : x_new_font (f, SDATA (arg)));
01f1ba30 1959 UNBLOCK_INPUT;
7273d100 1960
ea96210c 1961 if (EQ (result, Qnil))
d5db4077 1962 error ("Font `%s' is not defined", SDATA (arg));
ea96210c 1963 else if (EQ (result, Qt))
26e18ed9 1964 error ("The characters of the given font have varying widths");
ea96210c
JB
1965 else if (STRINGP (result))
1966 {
57c5889c
GM
1967 if (STRINGP (fontset_name))
1968 {
1969 /* Fontset names are built from ASCII font names, so the
1970 names may be equal despite there was a change. */
1971 if (old_fontset == f->output_data.x->fontset)
1972 return;
1973 }
1974 else if (!NILP (Fequal (result, oldval)))
1d090605 1975 return;
7273d100 1976
ea96210c 1977 store_frame_param (f, Qfont, result);
333b20bb 1978 recompute_basic_faces (f);
ea96210c
JB
1979 }
1980 else
1981 abort ();
a367641f 1982
8938a4fb 1983 do_pending_window_change (0);
95aa0336 1984
333b20bb
GM
1985 /* Don't call `face-set-after-frame-default' when faces haven't been
1986 initialized yet. This is the case when called from
1987 Fx_create_frame. In that case, the X widget or window doesn't
1988 exist either, and we can end up in x_report_frame_params with a
1989 null widget which gives a segfault. */
1990 if (FRAME_FACE_CACHE (f))
1991 {
1992 XSETFRAME (frame, f);
1993 call1 (Qface_set_after_frame_default, frame);
1994 }
01f1ba30
JB
1995}
1996
b3ba0aa8
KS
1997static void
1998x_set_fringe_width (f, new_value, old_value)
1999 struct frame *f;
2000 Lisp_Object new_value, old_value;
2001{
2002 x_compute_fringe_widths (f, 1);
2003}
2004
01f1ba30 2005void
f676886a
JB
2006x_set_border_width (f, arg, oldval)
2007 struct frame *f;
01f1ba30
JB
2008 Lisp_Object arg, oldval;
2009{
b7826503 2010 CHECK_NUMBER (arg);
01f1ba30 2011
7556890b 2012 if (XINT (arg) == f->output_data.x->border_width)
01f1ba30
JB
2013 return;
2014
fe24a618 2015 if (FRAME_X_WINDOW (f) != 0)
01f1ba30
JB
2016 error ("Cannot change the border width of a window");
2017
7556890b 2018 f->output_data.x->border_width = XINT (arg);
01f1ba30
JB
2019}
2020
2021void
f676886a
JB
2022x_set_internal_border_width (f, arg, oldval)
2023 struct frame *f;
01f1ba30
JB
2024 Lisp_Object arg, oldval;
2025{
7556890b 2026 int old = f->output_data.x->internal_border_width;
01f1ba30 2027
b7826503 2028 CHECK_NUMBER (arg);
7556890b
RS
2029 f->output_data.x->internal_border_width = XINT (arg);
2030 if (f->output_data.x->internal_border_width < 0)
2031 f->output_data.x->internal_border_width = 0;
01f1ba30 2032
d3b06468 2033#ifdef USE_X_TOOLKIT
2a8a07d4 2034 if (f->output_data.x->edit_widget)
968b1234 2035 widget_store_internal_border (f->output_data.x->edit_widget);
d3b06468 2036#endif
2a8a07d4 2037
7556890b 2038 if (f->output_data.x->internal_border_width == old)
01f1ba30
JB
2039 return;
2040
fe24a618 2041 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 2042 {
363f7e15 2043 x_set_window_size (f, 0, f->width, f->height);
f676886a 2044 SET_FRAME_GARBAGED (f);
8938a4fb 2045 do_pending_window_change (0);
01f1ba30 2046 }
ea42193a
GM
2047 else
2048 SET_FRAME_GARBAGED (f);
01f1ba30
JB
2049}
2050
d043f1a4
RS
2051void
2052x_set_visibility (f, value, oldval)
2053 struct frame *f;
2054 Lisp_Object value, oldval;
2055{
2056 Lisp_Object frame;
191ed777 2057 XSETFRAME (frame, f);
d043f1a4
RS
2058
2059 if (NILP (value))
363f7e15 2060 Fmake_frame_invisible (frame, Qt);
49795535 2061 else if (EQ (value, Qicon))
d043f1a4 2062 Ficonify_frame (frame);
49795535
JB
2063 else
2064 Fmake_frame_visible (frame);
d043f1a4 2065}
52de7ce9 2066
943b580d 2067\f
52de7ce9
GM
2068/* Change window heights in windows rooted in WINDOW by N lines. */
2069
d043f1a4 2070static void
52de7ce9 2071x_change_window_heights (window, n)
d043f1a4
RS
2072 Lisp_Object window;
2073 int n;
2074{
47c0f58b 2075 struct window *w = XWINDOW (window);
d043f1a4 2076
e33f7330
KH
2077 XSETFASTINT (w->top, XFASTINT (w->top) + n);
2078 XSETFASTINT (w->height, XFASTINT (w->height) - n);
d043f1a4 2079
4336c705
GM
2080 if (INTEGERP (w->orig_top))
2081 XSETFASTINT (w->orig_top, XFASTINT (w->orig_top) + n);
2082 if (INTEGERP (w->orig_height))
2083 XSETFASTINT (w->orig_height, XFASTINT (w->orig_height) - n);
2084
47c0f58b
RS
2085 /* Handle just the top child in a vertical split. */
2086 if (!NILP (w->vchild))
52de7ce9 2087 x_change_window_heights (w->vchild, n);
d043f1a4 2088
47c0f58b
RS
2089 /* Adjust all children in a horizontal split. */
2090 for (window = w->hchild; !NILP (window); window = w->next)
2091 {
2092 w = XWINDOW (window);
52de7ce9 2093 x_change_window_heights (window, n);
d043f1a4
RS
2094 }
2095}
2096
2097void
2098x_set_menu_bar_lines (f, value, oldval)
2099 struct frame *f;
2100 Lisp_Object value, oldval;
2101{
2102 int nlines;
b6d7acec 2103#ifndef USE_X_TOOLKIT
d043f1a4 2104 int olines = FRAME_MENU_BAR_LINES (f);
b6d7acec 2105#endif
d043f1a4 2106
f64ba6ea
JB
2107 /* Right now, menu bars don't work properly in minibuf-only frames;
2108 most of the commands try to apply themselves to the minibuffer
333b20bb 2109 frame itself, and get an error because you can't switch buffers
f64ba6ea 2110 in or split the minibuffer window. */
519066d2 2111 if (FRAME_MINIBUF_ONLY_P (f))
f64ba6ea
JB
2112 return;
2113
6a5e54e2 2114 if (INTEGERP (value))
d043f1a4
RS
2115 nlines = XINT (value);
2116 else
2117 nlines = 0;
2118
3d09b6be
RS
2119 /* Make sure we redisplay all windows in this frame. */
2120 windows_or_buffers_changed++;
2121
9ef48a9d
RS
2122#ifdef USE_X_TOOLKIT
2123 FRAME_MENU_BAR_LINES (f) = 0;
2124 if (nlines)
0d8ef3f4
RS
2125 {
2126 FRAME_EXTERNAL_MENU_BAR (f) = 1;
97a1ff91 2127 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
0d8ef3f4
RS
2128 /* Make sure next redisplay shows the menu bar. */
2129 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
2130 }
9ef48a9d
RS
2131 else
2132 {
6bc20398
FP
2133 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
2134 free_frame_menubar (f);
9ef48a9d 2135 FRAME_EXTERNAL_MENU_BAR (f) = 0;
97a1ff91
RS
2136 if (FRAME_X_P (f))
2137 f->output_data.x->menubar_widget = 0;
9ef48a9d
RS
2138 }
2139#else /* not USE_X_TOOLKIT */
d043f1a4 2140 FRAME_MENU_BAR_LINES (f) = nlines;
52de7ce9 2141 x_change_window_heights (f->root_window, nlines - olines);
9ef48a9d 2142#endif /* not USE_X_TOOLKIT */
333b20bb
GM
2143 adjust_glyphs (f);
2144}
2145
2146
2147/* Set the number of lines used for the tool bar of frame F to VALUE.
2148 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
2149 is the old number of tool bar lines. This function changes the
2150 height of all windows on frame F to match the new tool bar height.
2151 The frame's height doesn't change. */
2152
2153void
9ea173e8 2154x_set_tool_bar_lines (f, value, oldval)
333b20bb
GM
2155 struct frame *f;
2156 Lisp_Object value, oldval;
2157{
52de7ce9
GM
2158 int delta, nlines, root_height;
2159 Lisp_Object root_window;
333b20bb 2160
e870b7ba
GM
2161 /* Treat tool bars like menu bars. */
2162 if (FRAME_MINIBUF_ONLY_P (f))
2163 return;
2164
333b20bb
GM
2165 /* Use VALUE only if an integer >= 0. */
2166 if (INTEGERP (value) && XINT (value) >= 0)
2167 nlines = XFASTINT (value);
2168 else
2169 nlines = 0;
2170
2171 /* Make sure we redisplay all windows in this frame. */
2172 ++windows_or_buffers_changed;
2173
9ea173e8 2174 delta = nlines - FRAME_TOOL_BAR_LINES (f);
52de7ce9
GM
2175
2176 /* Don't resize the tool-bar to more than we have room for. */
2177 root_window = FRAME_ROOT_WINDOW (f);
2178 root_height = XINT (XWINDOW (root_window)->height);
2179 if (root_height - delta < 1)
2180 {
2181 delta = root_height - 1;
2182 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
2183 }
2184
9ea173e8 2185 FRAME_TOOL_BAR_LINES (f) = nlines;
52de7ce9 2186 x_change_window_heights (root_window, delta);
333b20bb 2187 adjust_glyphs (f);
7273d100 2188
ccba751c
GM
2189 /* We also have to make sure that the internal border at the top of
2190 the frame, below the menu bar or tool bar, is redrawn when the
2191 tool bar disappears. This is so because the internal border is
2192 below the tool bar if one is displayed, but is below the menu bar
2193 if there isn't a tool bar. The tool bar draws into the area
2194 below the menu bar. */
2195 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
2196 {
2197 updating_frame = f;
2198 clear_frame ();
fb3cd89b 2199 clear_current_matrices (f);
ccba751c
GM
2200 updating_frame = NULL;
2201 }
b6f91066
GM
2202
2203 /* If the tool bar gets smaller, the internal border below it
2204 has to be cleared. It was formerly part of the display
2205 of the larger tool bar, and updating windows won't clear it. */
2206 if (delta < 0)
2207 {
2208 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
2209 int width = PIXEL_WIDTH (f);
2210 int y = nlines * CANON_Y_UNIT (f);
2211
2212 BLOCK_INPUT;
161d30fd
GM
2213 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2214 0, y, width, height, False);
b6f91066 2215 UNBLOCK_INPUT;
ddc24747
GM
2216
2217 if (WINDOWP (f->tool_bar_window))
2218 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
b6f91066 2219 }
333b20bb
GM
2220}
2221
2222
2223/* Set the foreground color for scroll bars on frame F to VALUE.
2224 VALUE should be a string, a color name. If it isn't a string or
2225 isn't a valid color name, do nothing. OLDVAL is the old value of
2226 the frame parameter. */
2227
2228void
2229x_set_scroll_bar_foreground (f, value, oldval)
2230 struct frame *f;
2231 Lisp_Object value, oldval;
2232{
2233 unsigned long pixel;
7273d100 2234
333b20bb
GM
2235 if (STRINGP (value))
2236 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
2237 else
2238 pixel = -1;
2239
2240 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
2241 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
7273d100 2242
333b20bb
GM
2243 f->output_data.x->scroll_bar_foreground_pixel = pixel;
2244 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2245 {
2246 /* Remove all scroll bars because they have wrong colors. */
2247 if (condemn_scroll_bars_hook)
2248 (*condemn_scroll_bars_hook) (f);
2249 if (judge_scroll_bars_hook)
2250 (*judge_scroll_bars_hook) (f);
05c8abbe
GM
2251
2252 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
333b20bb
GM
2253 redraw_frame (f);
2254 }
2255}
2256
2257
2258/* Set the background color for scroll bars on frame F to VALUE VALUE
2259 should be a string, a color name. If it isn't a string or isn't a
2260 valid color name, do nothing. OLDVAL is the old value of the frame
2261 parameter. */
2262
2263void
2264x_set_scroll_bar_background (f, value, oldval)
2265 struct frame *f;
2266 Lisp_Object value, oldval;
2267{
2268 unsigned long pixel;
2269
2270 if (STRINGP (value))
2271 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
2272 else
2273 pixel = -1;
7273d100 2274
333b20bb
GM
2275 if (f->output_data.x->scroll_bar_background_pixel != -1)
2276 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
7273d100 2277
f15340b7
MB
2278#ifdef USE_TOOLKIT_SCROLL_BARS
2279 /* Scrollbar shadow colors. */
2280 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
2281 {
2282 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
2283 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2284 }
2285 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
2286 {
2287 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
2288 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2289 }
2290#endif /* USE_TOOLKIT_SCROLL_BARS */
2291
333b20bb
GM
2292 f->output_data.x->scroll_bar_background_pixel = pixel;
2293 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
2294 {
2295 /* Remove all scroll bars because they have wrong colors. */
2296 if (condemn_scroll_bars_hook)
2297 (*condemn_scroll_bars_hook) (f);
2298 if (judge_scroll_bars_hook)
2299 (*judge_scroll_bars_hook) (f);
7273d100 2300
05c8abbe 2301 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
333b20bb
GM
2302 redraw_frame (f);
2303 }
d043f1a4 2304}
333b20bb 2305
943b580d 2306\f
3a258507 2307/* Encode Lisp string STRING as a text in a format appropriate for
96db09e4
KH
2308 XICCC (X Inter Client Communication Conventions).
2309
2310 If STRING contains only ASCII characters, do no conversion and
2311 return the string data of STRING. Otherwise, encode the text by
2312 CODING_SYSTEM, and return a newly allocated memory area which
2313 should be freed by `xfree' by a caller.
2314
37323f34
EZ
2315 SELECTIONP non-zero means the string is being encoded for an X
2316 selection, so it is safe to run pre-write conversions (which
2317 may run Lisp code).
2318
96db09e4
KH
2319 Store the byte length of resulting text in *TEXT_BYTES.
2320
d60660d6 2321 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
96db09e4 2322 which means that the `encoding' of the result can be `STRING'.
d60660d6 2323 Otherwise store 0 in *STRINGP, which means that the `encoding' of
96db09e4
KH
2324 the result should be `COMPOUND_TEXT'. */
2325
2326unsigned char *
37323f34 2327x_encode_text (string, coding_system, selectionp, text_bytes, stringp)
96db09e4 2328 Lisp_Object string, coding_system;
d60660d6 2329 int *text_bytes, *stringp;
37323f34 2330 int selectionp;
96db09e4 2331{
d5db4077
KR
2332 unsigned char *str = SDATA (string);
2333 int chars = SCHARS (string);
2334 int bytes = SBYTES (string);
96db09e4
KH
2335 int charset_info;
2336 int bufsize;
2337 unsigned char *buf;
2338 struct coding_system coding;
43dc73f1 2339 extern Lisp_Object Qcompound_text_with_extensions;
96db09e4
KH
2340
2341 charset_info = find_charset_in_text (str, chars, bytes, NULL, Qnil);
2342 if (charset_info == 0)
2343 {
2344 /* No multibyte character in OBJ. We need not encode it. */
2345 *text_bytes = bytes;
d60660d6 2346 *stringp = 1;
96db09e4
KH
2347 return str;
2348 }
2349
2350 setup_coding_system (coding_system, &coding);
37323f34
EZ
2351 if (selectionp
2352 && SYMBOLP (coding.pre_write_conversion)
2353 && !NILP (Ffboundp (coding.pre_write_conversion)))
2354 {
2355 string = run_pre_post_conversion_on_str (string, &coding, 1);
d5db4077
KR
2356 str = SDATA (string);
2357 chars = SCHARS (string);
2358 bytes = SBYTES (string);
37323f34 2359 }
96db09e4
KH
2360 coding.src_multibyte = 1;
2361 coding.dst_multibyte = 0;
2362 coding.mode |= CODING_MODE_LAST_BLOCK;
d60660d6
KH
2363 if (coding.type == coding_type_iso2022)
2364 coding.flags |= CODING_FLAG_ISO_SAFE;
35bc5887
KH
2365 /* We suppress producing escape sequences for composition. */
2366 coding.composing = COMPOSITION_DISABLED;
96db09e4
KH
2367 bufsize = encoding_buffer_size (&coding, bytes);
2368 buf = (unsigned char *) xmalloc (bufsize);
2369 encode_coding (&coding, str, buf, bytes, bufsize);
2370 *text_bytes = coding.produced;
43dc73f1
EZ
2371 *stringp = (charset_info == 1
2372 || (!EQ (coding_system, Qcompound_text)
2373 && !EQ (coding_system, Qcompound_text_with_extensions)));
96db09e4
KH
2374 return buf;
2375}
2376
2377\f
75f9d625 2378/* Change the name of frame F to NAME. If NAME is nil, set F's name to
f945b920
JB
2379 x_id_name.
2380
2381 If EXPLICIT is non-zero, that indicates that lisp code is setting the
75f9d625
DM
2382 name; if NAME is a string, set F's name to NAME and set
2383 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
f945b920
JB
2384
2385 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2386 suggesting a new name, which lisp code should override; if
2387 F->explicit_name is set, ignore the new name; otherwise, set it. */
2388
2389void
2390x_set_name (f, name, explicit)
2391 struct frame *f;
2392 Lisp_Object name;
2393 int explicit;
2394{
7273d100 2395 /* Make sure that requests from lisp code override requests from
f945b920
JB
2396 Emacs redisplay code. */
2397 if (explicit)
2398 {
2399 /* If we're switching from explicit to implicit, we had better
2400 update the mode lines and thereby update the title. */
2401 if (f->explicit_name && NILP (name))
cf177271 2402 update_mode_lines = 1;
f945b920
JB
2403
2404 f->explicit_name = ! NILP (name);
2405 }
2406 else if (f->explicit_name)
2407 return;
2408
2409 /* If NAME is nil, set the name to the x_id_name. */
2410 if (NILP (name))
f10f0b79
RS
2411 {
2412 /* Check for no change needed in this very common case
2413 before we do any consing. */
08a90d6a 2414 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
d5db4077 2415 SDATA (f->name)))
f10f0b79 2416 return;
08a90d6a 2417 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
f10f0b79 2418 }
62265f1c 2419 else
b7826503 2420 CHECK_STRING (name);
01f1ba30 2421
f945b920
JB
2422 /* Don't change the name if it's already NAME. */
2423 if (! NILP (Fstring_equal (name, f->name)))
daa37602
JB
2424 return;
2425
943b580d
RS
2426 f->name = name;
2427
2428 /* For setting the frame title, the title parameter should override
2429 the name parameter. */
2430 if (! NILP (f->title))
2431 name = f->title;
2432
fe24a618 2433 if (FRAME_X_WINDOW (f))
01f1ba30 2434 {
01f1ba30 2435 BLOCK_INPUT;
fe24a618
JB
2436#ifdef HAVE_X11R4
2437 {
80534dd6 2438 XTextProperty text, icon;
d60660d6 2439 int bytes, stringp;
11270583 2440 Lisp_Object coding_system;
80534dd6 2441
869331ee 2442 coding_system = Qcompound_text;
37323f34 2443 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp);
d60660d6 2444 text.encoding = (stringp ? XA_STRING
96db09e4 2445 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
fe24a618 2446 text.format = 8;
96db09e4 2447 text.nitems = bytes;
80534dd6 2448
96db09e4
KH
2449 if (NILP (f->icon_name))
2450 {
2451 icon = text;
2452 }
2453 else
2454 {
37323f34 2455 icon.value = x_encode_text (f->icon_name, coding_system, 0,
d60660d6
KH
2456 &bytes, &stringp);
2457 icon.encoding = (stringp ? XA_STRING
96db09e4
KH
2458 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2459 icon.format = 8;
2460 icon.nitems = bytes;
2461 }
9ef48a9d 2462#ifdef USE_X_TOOLKIT
b9dc4443 2463 XSetWMName (FRAME_X_DISPLAY (f),
7556890b
RS
2464 XtWindow (f->output_data.x->widget), &text);
2465 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
80534dd6 2466 &icon);
9ef48a9d 2467#else /* not USE_X_TOOLKIT */
b9dc4443 2468 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
80534dd6 2469 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
9ef48a9d 2470#endif /* not USE_X_TOOLKIT */
96db09e4 2471 if (!NILP (f->icon_name)
1b49bf99 2472 && icon.value != (unsigned char *) SDATA (f->icon_name))
96db09e4 2473 xfree (icon.value);
1b49bf99 2474 if (text.value != (unsigned char *) SDATA (name))
96db09e4 2475 xfree (text.value);
fe24a618 2476 }
9ef48a9d 2477#else /* not HAVE_X11R4 */
b9dc4443 2478 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
d5db4077 2479 SDATA (name));
b9dc4443 2480 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
d5db4077 2481 SDATA (name));
9ef48a9d 2482#endif /* not HAVE_X11R4 */
01f1ba30
JB
2483 UNBLOCK_INPUT;
2484 }
f945b920
JB
2485}
2486
2487/* This function should be called when the user's lisp code has
2488 specified a name for the frame; the name will override any set by the
2489 redisplay code. */
2490void
2491x_explicitly_set_name (f, arg, oldval)
2492 FRAME_PTR f;
2493 Lisp_Object arg, oldval;
2494{
2495 x_set_name (f, arg, 1);
2496}
2497
2498/* This function should be called by Emacs redisplay code to set the
2499 name; names set this way will never override names set by the user's
2500 lisp code. */
25250031 2501void
f945b920
JB
2502x_implicitly_set_name (f, arg, oldval)
2503 FRAME_PTR f;
2504 Lisp_Object arg, oldval;
2505{
2506 x_set_name (f, arg, 0);
01f1ba30 2507}
943b580d
RS
2508\f
2509/* Change the title of frame F to NAME.
2510 If NAME is nil, use the frame name as the title.
01f1ba30 2511
943b580d
RS
2512 If EXPLICIT is non-zero, that indicates that lisp code is setting the
2513 name; if NAME is a string, set F's name to NAME and set
2514 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
2515
2516 If EXPLICIT is zero, that indicates that Emacs redisplay code is
2517 suggesting a new name, which lisp code should override; if
2518 F->explicit_name is set, ignore the new name; otherwise, set it. */
2519
2520void
d62c8769 2521x_set_title (f, name, old_name)
943b580d 2522 struct frame *f;
d62c8769 2523 Lisp_Object name, old_name;
943b580d
RS
2524{
2525 /* Don't change the title if it's already NAME. */
2526 if (EQ (name, f->title))
2527 return;
2528
2529 update_mode_lines = 1;
2530
2531 f->title = name;
2532
2533 if (NILP (name))
2534 name = f->name;
beb403b3 2535 else
b7826503 2536 CHECK_STRING (name);
943b580d
RS
2537
2538 if (FRAME_X_WINDOW (f))
2539 {
2540 BLOCK_INPUT;
2541#ifdef HAVE_X11R4
2542 {
2543 XTextProperty text, icon;
d60660d6 2544 int bytes, stringp;
11270583 2545 Lisp_Object coding_system;
943b580d 2546
869331ee 2547 coding_system = Qcompound_text;
37323f34 2548 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp);
d60660d6 2549 text.encoding = (stringp ? XA_STRING
96db09e4 2550 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
943b580d 2551 text.format = 8;
96db09e4 2552 text.nitems = bytes;
943b580d 2553
96db09e4
KH
2554 if (NILP (f->icon_name))
2555 {
2556 icon = text;
2557 }
2558 else
2559 {
37323f34 2560 icon.value = x_encode_text (f->icon_name, coding_system, 0,
d60660d6
KH
2561 &bytes, &stringp);
2562 icon.encoding = (stringp ? XA_STRING
96db09e4
KH
2563 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
2564 icon.format = 8;
2565 icon.nitems = bytes;
2566 }
943b580d
RS
2567#ifdef USE_X_TOOLKIT
2568 XSetWMName (FRAME_X_DISPLAY (f),
2569 XtWindow (f->output_data.x->widget), &text);
2570 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
2571 &icon);
2572#else /* not USE_X_TOOLKIT */
2573 XSetWMName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
2574 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &icon);
2575#endif /* not USE_X_TOOLKIT */
96db09e4 2576 if (!NILP (f->icon_name)
1b49bf99 2577 && icon.value != (unsigned char *) SDATA (f->icon_name))
96db09e4 2578 xfree (icon.value);
1b49bf99 2579 if (text.value != (unsigned char *) SDATA (name))
96db09e4 2580 xfree (text.value);
943b580d
RS
2581 }
2582#else /* not HAVE_X11R4 */
2583 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
d5db4077 2584 SDATA (name));
943b580d 2585 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
d5db4077 2586 SDATA (name));
943b580d
RS
2587#endif /* not HAVE_X11R4 */
2588 UNBLOCK_INPUT;
2589 }
2590}
2591\f
01f1ba30 2592void
f676886a
JB
2593x_set_autoraise (f, arg, oldval)
2594 struct frame *f;
01f1ba30
JB
2595 Lisp_Object arg, oldval;
2596{
f676886a 2597 f->auto_raise = !EQ (Qnil, arg);
01f1ba30
JB
2598}
2599
2600void
f676886a
JB
2601x_set_autolower (f, arg, oldval)
2602 struct frame *f;
01f1ba30
JB
2603 Lisp_Object arg, oldval;
2604{
f676886a 2605 f->auto_lower = !EQ (Qnil, arg);
01f1ba30 2606}
179956b9 2607
eac358ef
KH
2608void
2609x_set_unsplittable (f, arg, oldval)
2610 struct frame *f;
2611 Lisp_Object arg, oldval;
2612{
2613 f->no_split = !NILP (arg);
2614}
2615
179956b9 2616void
a3c87d4e 2617x_set_vertical_scroll_bars (f, arg, oldval)
179956b9
JB
2618 struct frame *f;
2619 Lisp_Object arg, oldval;
2620{
1ab3d87e
RS
2621 if ((EQ (arg, Qleft) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
2622 || (EQ (arg, Qright) && FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
2623 || (NILP (arg) && FRAME_HAS_VERTICAL_SCROLL_BARS (f))
2624 || (!NILP (arg) && ! FRAME_HAS_VERTICAL_SCROLL_BARS (f)))
179956b9 2625 {
1ab3d87e
RS
2626 FRAME_VERTICAL_SCROLL_BAR_TYPE (f)
2627 = (NILP (arg)
2628 ? vertical_scroll_bar_none
2629 : EQ (Qright, arg)
7273d100 2630 ? vertical_scroll_bar_right
1ab3d87e 2631 : vertical_scroll_bar_left);
179956b9 2632
cf177271
JB
2633 /* We set this parameter before creating the X window for the
2634 frame, so we can get the geometry right from the start.
2635 However, if the window hasn't been created yet, we shouldn't
2636 call x_set_window_size. */
2637 if (FRAME_X_WINDOW (f))
363f7e15 2638 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
8938a4fb 2639 do_pending_window_change (0);
179956b9
JB
2640 }
2641}
4701395c
KH
2642
2643void
2644x_set_scroll_bar_width (f, arg, oldval)
2645 struct frame *f;
2646 Lisp_Object arg, oldval;
2647{
a672c74d
RS
2648 int wid = FONT_WIDTH (f->output_data.x->font);
2649
dff9a538
KH
2650 if (NILP (arg))
2651 {
c6e9d03b
GM
2652#ifdef USE_TOOLKIT_SCROLL_BARS
2653 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
333b20bb
GM
2654 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
2655 FRAME_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
2656 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = width;
2657#else
2658 /* Make the actual width at least 14 pixels and a multiple of a
2659 character width. */
a672c74d 2660 FRAME_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
7273d100 2661
333b20bb
GM
2662 /* Use all of that space (aside from required margins) for the
2663 scroll bar. */
dff9a538 2664 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = 0;
333b20bb 2665#endif
a672c74d 2666
a90ab372
RS
2667 if (FRAME_X_WINDOW (f))
2668 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
8938a4fb 2669 do_pending_window_change (0);
dff9a538
KH
2670 }
2671 else if (INTEGERP (arg) && XINT (arg) > 0
2672 && XFASTINT (arg) != FRAME_SCROLL_BAR_PIXEL_WIDTH (f))
4701395c 2673 {
09d8c7ac
RS
2674 if (XFASTINT (arg) <= 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM)
2675 XSETINT (arg, 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM + 1);
0a26b136 2676
4701395c
KH
2677 FRAME_SCROLL_BAR_PIXEL_WIDTH (f) = XFASTINT (arg);
2678 FRAME_SCROLL_BAR_COLS (f) = (XFASTINT (arg) + wid-1) / wid;
2679 if (FRAME_X_WINDOW (f))
2680 x_set_window_size (f, 0, FRAME_WIDTH (f), FRAME_HEIGHT (f));
2681 }
dca97592 2682
8938a4fb 2683 change_frame_size (f, 0, FRAME_WIDTH (f), 0, 0, 0);
333b20bb
GM
2684 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.hpos = 0;
2685 XWINDOW (FRAME_SELECTED_WINDOW (f))->cursor.x = 0;
4701395c 2686}
333b20bb
GM
2687
2688
01f1ba30 2689\f
f676886a 2690/* Subroutines of creating an X frame. */
01f1ba30 2691
b7975ee4
KH
2692/* Make sure that Vx_resource_name is set to a reasonable value.
2693 Fix it up, or set it to `emacs' if it is too hopeless. */
2694
d387c960
JB
2695static void
2696validate_x_resource_name ()
2697{
333b20bb 2698 int len = 0;
0e78b377
RS
2699 /* Number of valid characters in the resource name. */
2700 int good_count = 0;
2701 /* Number of invalid characters in the resource name. */
2702 int bad_count = 0;
2703 Lisp_Object new;
2704 int i;
2705
498e9ac3
RS
2706 if (!STRINGP (Vx_resource_class))
2707 Vx_resource_class = build_string (EMACS_CLASS);
2708
cf204347
RS
2709 if (STRINGP (Vx_resource_name))
2710 {
d5db4077 2711 unsigned char *p = SDATA (Vx_resource_name);
cf204347
RS
2712 int i;
2713
d5db4077 2714 len = SBYTES (Vx_resource_name);
0e78b377
RS
2715
2716 /* Only letters, digits, - and _ are valid in resource names.
2717 Count the valid characters and count the invalid ones. */
cf204347
RS
2718 for (i = 0; i < len; i++)
2719 {
2720 int c = p[i];
2721 if (! ((c >= 'a' && c <= 'z')
2722 || (c >= 'A' && c <= 'Z')
2723 || (c >= '0' && c <= '9')
2724 || c == '-' || c == '_'))
0e78b377
RS
2725 bad_count++;
2726 else
2727 good_count++;
cf204347
RS
2728 }
2729 }
2730 else
0e78b377
RS
2731 /* Not a string => completely invalid. */
2732 bad_count = 5, good_count = 0;
2733
2734 /* If name is valid already, return. */
2735 if (bad_count == 0)
2736 return;
2737
2738 /* If name is entirely invalid, or nearly so, use `emacs'. */
2739 if (good_count == 0
2740 || (good_count == 1 && bad_count > 0))
2741 {
b7975ee4 2742 Vx_resource_name = build_string ("emacs");
0e78b377
RS
2743 return;
2744 }
2745
2746 /* Name is partly valid. Copy it and replace the invalid characters
2747 with underscores. */
2748
2749 Vx_resource_name = new = Fcopy_sequence (Vx_resource_name);
2750
2751 for (i = 0; i < len; i++)
2752 {
d5db4077 2753 int c = SREF (new, i);
0e78b377
RS
2754 if (! ((c >= 'a' && c <= 'z')
2755 || (c >= 'A' && c <= 'Z')
2756 || (c >= '0' && c <= '9')
2757 || c == '-' || c == '_'))
b06a00fb 2758 SSET (new, i, '_');
0e78b377 2759 }
d387c960
JB
2760}
2761
2762
01f1ba30 2763extern char *x_get_string_resource ();
01f1ba30 2764
cf177271 2765DEFUN ("x-get-resource", Fx_get_resource, Sx_get_resource, 2, 4, 0,
03265352 2766 doc: /* Return the value of ATTRIBUTE, of class CLASS, from the X defaults database.
c061c855
GM
2767This uses `INSTANCE.ATTRIBUTE' as the key and `Emacs.CLASS' as the
2768class, where INSTANCE is the name under which Emacs was invoked, or
2769the name specified by the `-name' or `-rn' command-line arguments.
2770
2771The optional arguments COMPONENT and SUBCLASS add to the key and the
2772class, respectively. You must specify both of them or neither.
2773If you specify them, the key is `INSTANCE.COMPONENT.ATTRIBUTE'
7ee72033
MB
2774and the class is `Emacs.CLASS.SUBCLASS'. */)
2775 (attribute, class, component, subclass)
cf177271 2776 Lisp_Object attribute, class, component, subclass;
01f1ba30
JB
2777{
2778 register char *value;
2779 char *name_key;
2780 char *class_key;
2781
11ae94fe
RS
2782 check_x ();
2783
b7826503
PJ
2784 CHECK_STRING (attribute);
2785 CHECK_STRING (class);
cf177271 2786
8fabe6f4 2787 if (!NILP (component))
b7826503 2788 CHECK_STRING (component);
8fabe6f4 2789 if (!NILP (subclass))
b7826503 2790 CHECK_STRING (subclass);
8fabe6f4
RS
2791 if (NILP (component) != NILP (subclass))
2792 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2793
d387c960
JB
2794 validate_x_resource_name ();
2795
b7975ee4
KH
2796 /* Allocate space for the components, the dots which separate them,
2797 and the final '\0'. Make them big enough for the worst case. */
d5db4077 2798 name_key = (char *) alloca (SBYTES (Vx_resource_name)
b7975ee4 2799 + (STRINGP (component)
d5db4077
KR
2800 ? SBYTES (component) : 0)
2801 + SBYTES (attribute)
b7975ee4
KH
2802 + 3);
2803
d5db4077
KR
2804 class_key = (char *) alloca (SBYTES (Vx_resource_class)
2805 + SBYTES (class)
b7975ee4 2806 + (STRINGP (subclass)
d5db4077 2807 ? SBYTES (subclass) : 0)
b7975ee4
KH
2808 + 3);
2809
2810 /* Start with emacs.FRAMENAME for the name (the specific one)
2811 and with `Emacs' for the class key (the general one). */
d5db4077
KR
2812 strcpy (name_key, SDATA (Vx_resource_name));
2813 strcpy (class_key, SDATA (Vx_resource_class));
b7975ee4
KH
2814
2815 strcat (class_key, ".");
d5db4077 2816 strcat (class_key, SDATA (class));
b7975ee4
KH
2817
2818 if (!NILP (component))
01f1ba30 2819 {
b7975ee4 2820 strcat (class_key, ".");
d5db4077 2821 strcat (class_key, SDATA (subclass));
b7975ee4
KH
2822
2823 strcat (name_key, ".");
d5db4077 2824 strcat (name_key, SDATA (component));
01f1ba30
JB
2825 }
2826
b7975ee4 2827 strcat (name_key, ".");
d5db4077 2828 strcat (name_key, SDATA (attribute));
b7975ee4 2829
b9dc4443
RS
2830 value = x_get_string_resource (check_x_display_info (Qnil)->xrdb,
2831 name_key, class_key);
01f1ba30
JB
2832
2833 if (value != (char *) 0)
2834 return build_string (value);
2835 else
2836 return Qnil;
2837}
2838
abb4b7ec
RS
2839/* Get an X resource, like Fx_get_resource, but for display DPYINFO. */
2840
333b20bb 2841Lisp_Object
abb4b7ec
RS
2842display_x_get_resource (dpyinfo, attribute, class, component, subclass)
2843 struct x_display_info *dpyinfo;
2844 Lisp_Object attribute, class, component, subclass;
2845{
2846 register char *value;
2847 char *name_key;
2848 char *class_key;
2849
b7826503
PJ
2850 CHECK_STRING (attribute);
2851 CHECK_STRING (class);
abb4b7ec
RS
2852
2853 if (!NILP (component))
b7826503 2854 CHECK_STRING (component);
abb4b7ec 2855 if (!NILP (subclass))
b7826503 2856 CHECK_STRING (subclass);
abb4b7ec
RS
2857 if (NILP (component) != NILP (subclass))
2858 error ("x-get-resource: must specify both COMPONENT and SUBCLASS or neither");
2859
2860 validate_x_resource_name ();
2861
2862 /* Allocate space for the components, the dots which separate them,
2863 and the final '\0'. Make them big enough for the worst case. */
d5db4077 2864 name_key = (char *) alloca (SBYTES (Vx_resource_name)
abb4b7ec 2865 + (STRINGP (component)
d5db4077
KR
2866 ? SBYTES (component) : 0)
2867 + SBYTES (attribute)
abb4b7ec
RS
2868 + 3);
2869
d5db4077
KR
2870 class_key = (char *) alloca (SBYTES (Vx_resource_class)
2871 + SBYTES (class)
abb4b7ec 2872 + (STRINGP (subclass)
d5db4077 2873 ? SBYTES (subclass) : 0)
abb4b7ec
RS
2874 + 3);
2875
2876 /* Start with emacs.FRAMENAME for the name (the specific one)
2877 and with `Emacs' for the class key (the general one). */
d5db4077
KR
2878 strcpy (name_key, SDATA (Vx_resource_name));
2879 strcpy (class_key, SDATA (Vx_resource_class));
abb4b7ec
RS
2880
2881 strcat (class_key, ".");
d5db4077 2882 strcat (class_key, SDATA (class));
abb4b7ec
RS
2883
2884 if (!NILP (component))
2885 {
2886 strcat (class_key, ".");
d5db4077 2887 strcat (class_key, SDATA (subclass));
abb4b7ec
RS
2888
2889 strcat (name_key, ".");
d5db4077 2890 strcat (name_key, SDATA (component));
abb4b7ec
RS
2891 }
2892
2893 strcat (name_key, ".");
d5db4077 2894 strcat (name_key, SDATA (attribute));
abb4b7ec
RS
2895
2896 value = x_get_string_resource (dpyinfo->xrdb, name_key, class_key);
2897
2898 if (value != (char *) 0)
2899 return build_string (value);
2900 else
2901 return Qnil;
2902}
2903
3402e1a4
RS
2904/* Used when C code wants a resource value. */
2905
2906char *
2907x_get_resource_string (attribute, class)
2908 char *attribute, *class;
2909{
3402e1a4
RS
2910 char *name_key;
2911 char *class_key;
0fe92f72 2912 struct frame *sf = SELECTED_FRAME ();
3402e1a4
RS
2913
2914 /* Allocate space for the components, the dots which separate them,
2915 and the final '\0'. */
d5db4077 2916 name_key = (char *) alloca (SBYTES (Vinvocation_name)
3402e1a4
RS
2917 + strlen (attribute) + 2);
2918 class_key = (char *) alloca ((sizeof (EMACS_CLASS) - 1)
2919 + strlen (class) + 2);
2920
2921 sprintf (name_key, "%s.%s",
d5db4077 2922 SDATA (Vinvocation_name),
3402e1a4
RS
2923 attribute);
2924 sprintf (class_key, "%s.%s", EMACS_CLASS, class);
2925
0fe92f72 2926 return x_get_string_resource (FRAME_X_DISPLAY_INFO (sf)->xrdb,
b9dc4443 2927 name_key, class_key);
3402e1a4
RS
2928}
2929
60fb3ee1
JB
2930/* Types we might convert a resource string into. */
2931enum resource_types
333b20bb
GM
2932{
2933 RES_TYPE_NUMBER,
d62c8769 2934 RES_TYPE_FLOAT,
333b20bb
GM
2935 RES_TYPE_BOOLEAN,
2936 RES_TYPE_STRING,
2937 RES_TYPE_SYMBOL
2938};
60fb3ee1 2939
01f1ba30 2940/* Return the value of parameter PARAM.
60fb3ee1 2941
f676886a 2942 First search ALIST, then Vdefault_frame_alist, then the X defaults
cf177271 2943 database, using ATTRIBUTE as the attribute name and CLASS as its class.
60fb3ee1
JB
2944
2945 Convert the resource to the type specified by desired_type.
2946
f9942c9e
JB
2947 If no default is specified, return Qunbound. If you call
2948 x_get_arg, make sure you deal with Qunbound in a reasonable way,
a59e4f3d 2949 and don't let it get stored in any Lisp-visible variables! */
01f1ba30
JB
2950
2951static Lisp_Object
abb4b7ec
RS
2952x_get_arg (dpyinfo, alist, param, attribute, class, type)
2953 struct x_display_info *dpyinfo;
3c254570 2954 Lisp_Object alist, param;
60fb3ee1 2955 char *attribute;
cf177271 2956 char *class;
60fb3ee1 2957 enum resource_types type;
01f1ba30
JB
2958{
2959 register Lisp_Object tem;
2960
2961 tem = Fassq (param, alist);
2962 if (EQ (tem, Qnil))
f676886a 2963 tem = Fassq (param, Vdefault_frame_alist);
f9942c9e 2964 if (EQ (tem, Qnil))
01f1ba30 2965 {
60fb3ee1 2966
f9942c9e 2967 if (attribute)
60fb3ee1 2968 {
abb4b7ec
RS
2969 tem = display_x_get_resource (dpyinfo,
2970 build_string (attribute),
2971 build_string (class),
2972 Qnil, Qnil);
f9942c9e
JB
2973
2974 if (NILP (tem))
2975 return Qunbound;
2976
2977 switch (type)
2978 {
333b20bb 2979 case RES_TYPE_NUMBER:
d5db4077 2980 return make_number (atoi (SDATA (tem)));
f9942c9e 2981
d62c8769 2982 case RES_TYPE_FLOAT:
d5db4077 2983 return make_float (atof (SDATA (tem)));
d62c8769 2984
333b20bb 2985 case RES_TYPE_BOOLEAN:
f9942c9e 2986 tem = Fdowncase (tem);
d5db4077
KR
2987 if (!strcmp (SDATA (tem), "on")
2988 || !strcmp (SDATA (tem), "true"))
f9942c9e 2989 return Qt;
7273d100 2990 else
f9942c9e
JB
2991 return Qnil;
2992
333b20bb 2993 case RES_TYPE_STRING:
f9942c9e
JB
2994 return tem;
2995
333b20bb 2996 case RES_TYPE_SYMBOL:
49795535
JB
2997 /* As a special case, we map the values `true' and `on'
2998 to Qt, and `false' and `off' to Qnil. */
2999 {
98381190
KH
3000 Lisp_Object lower;
3001 lower = Fdowncase (tem);
d5db4077
KR
3002 if (!strcmp (SDATA (lower), "on")
3003 || !strcmp (SDATA (lower), "true"))
49795535 3004 return Qt;
d5db4077
KR
3005 else if (!strcmp (SDATA (lower), "off")
3006 || !strcmp (SDATA (lower), "false"))
49795535
JB
3007 return Qnil;
3008 else
89032215 3009 return Fintern (tem, Qnil);
49795535 3010 }
f945b920 3011
f9942c9e
JB
3012 default:
3013 abort ();
3014 }
60fb3ee1 3015 }
f9942c9e
JB
3016 else
3017 return Qunbound;
01f1ba30
JB
3018 }
3019 return Fcdr (tem);
3020}
3021
e4f79258
RS
3022/* Like x_get_arg, but also record the value in f->param_alist. */
3023
3024static Lisp_Object
3025x_get_and_record_arg (f, alist, param, attribute, class, type)
3026 struct frame *f;
3027 Lisp_Object alist, param;
3028 char *attribute;
3029 char *class;
3030 enum resource_types type;
3031{
3032 Lisp_Object value;
3033
abb4b7ec
RS
3034 value = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, param,
3035 attribute, class, type);
e4f79258
RS
3036 if (! NILP (value))
3037 store_frame_param (f, param, value);
3038
3039 return value;
3040}
3041
f676886a 3042/* Record in frame F the specified or default value according to ALIST
e8cc313b
KH
3043 of the parameter named PROP (a Lisp symbol).
3044 If no value is specified for PROP, look for an X default for XPROP
f676886a 3045 on the frame named NAME.
01f1ba30
JB
3046 If that is not found either, use the value DEFLT. */
3047
3048static Lisp_Object
cf177271 3049x_default_parameter (f, alist, prop, deflt, xprop, xclass, type)
f676886a 3050 struct frame *f;
01f1ba30 3051 Lisp_Object alist;
f9942c9e 3052 Lisp_Object prop;
01f1ba30
JB
3053 Lisp_Object deflt;
3054 char *xprop;
cf177271 3055 char *xclass;
60fb3ee1 3056 enum resource_types type;
01f1ba30 3057{
01f1ba30
JB
3058 Lisp_Object tem;
3059
abb4b7ec 3060 tem = x_get_arg (FRAME_X_DISPLAY_INFO (f), alist, prop, xprop, xclass, type);
f9942c9e 3061 if (EQ (tem, Qunbound))
01f1ba30 3062 tem = deflt;
f9942c9e 3063 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
01f1ba30
JB
3064 return tem;
3065}
333b20bb
GM
3066
3067
3068/* Record in frame F the specified or default value according to ALIST
3069 of the parameter named PROP (a Lisp symbol). If no value is
3070 specified for PROP, look for an X default for XPROP on the frame
3071 named NAME. If that is not found either, use the value DEFLT. */
3072
3073static Lisp_Object
3074x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
3075 foreground_p)
3076 struct frame *f;
3077 Lisp_Object alist;
3078 Lisp_Object prop;
3079 char *xprop;
3080 char *xclass;
3081 int foreground_p;
3082{
3083 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3084 Lisp_Object tem;
3085
3086 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
3087 if (EQ (tem, Qunbound))
3088 {
3089#ifdef USE_TOOLKIT_SCROLL_BARS
3090
3091 /* See if an X resource for the scroll bar color has been
3092 specified. */
3093 tem = display_x_get_resource (dpyinfo,
3094 build_string (foreground_p
3095 ? "foreground"
3096 : "background"),
c0ec53ad 3097 empty_string,
333b20bb 3098 build_string ("verticalScrollBar"),
c0ec53ad 3099 empty_string);
333b20bb
GM
3100 if (!STRINGP (tem))
3101 {
3102 /* If nothing has been specified, scroll bars will use a
3103 toolkit-dependent default. Because these defaults are
3104 difficult to get at without actually creating a scroll
3105 bar, use nil to indicate that no color has been
3106 specified. */
3107 tem = Qnil;
3108 }
7273d100 3109
333b20bb 3110#else /* not USE_TOOLKIT_SCROLL_BARS */
7273d100 3111
333b20bb 3112 tem = Qnil;
7273d100 3113
333b20bb
GM
3114#endif /* not USE_TOOLKIT_SCROLL_BARS */
3115 }
3116
3117 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
3118 return tem;
3119}
3120
3121
01f1ba30 3122\f
8af1d7ca 3123DEFUN ("x-parse-geometry", Fx_parse_geometry, Sx_parse_geometry, 1, 1, 0,
7ee72033 3124 doc: /* Parse an X-style geometry string STRING.
c061c855
GM
3125Returns an alist of the form ((top . TOP), (left . LEFT) ... ).
3126The properties returned may include `top', `left', `height', and `width'.
3127The value of `left' or `top' may be an integer,
3128or a list (+ N) meaning N pixels relative to top/left corner,
7ee72033
MB
3129or a list (- N) meaning -N pixels relative to bottom/right corner. */)
3130 (string)
a6605e5c 3131 Lisp_Object string;
01f1ba30
JB
3132{
3133 int geometry, x, y;
3134 unsigned int width, height;
f83f10ba 3135 Lisp_Object result;
01f1ba30 3136
b7826503 3137 CHECK_STRING (string);
01f1ba30 3138
d5db4077 3139 geometry = XParseGeometry ((char *) SDATA (string),
01f1ba30
JB
3140 &x, &y, &width, &height);
3141
f83f10ba
RS
3142#if 0
3143 if (!!(geometry & XValue) != !!(geometry & YValue))
3144 error ("Must specify both x and y position, or neither");
3145#endif
3146
3147 result = Qnil;
3148 if (geometry & XValue)
01f1ba30 3149 {
f83f10ba
RS
3150 Lisp_Object element;
3151
e1d962d7
RS
3152 if (x >= 0 && (geometry & XNegative))
3153 element = Fcons (Qleft, Fcons (Qminus, Fcons (make_number (-x), Qnil)));
3154 else if (x < 0 && ! (geometry & XNegative))
3155 element = Fcons (Qleft, Fcons (Qplus, Fcons (make_number (x), Qnil)));
f83f10ba
RS
3156 else
3157 element = Fcons (Qleft, make_number (x));
3158 result = Fcons (element, result);
3159 }
3160
3161 if (geometry & YValue)
3162 {
3163 Lisp_Object element;
3164
e1d962d7
RS
3165 if (y >= 0 && (geometry & YNegative))
3166 element = Fcons (Qtop, Fcons (Qminus, Fcons (make_number (-y), Qnil)));
3167 else if (y < 0 && ! (geometry & YNegative))
3168 element = Fcons (Qtop, Fcons (Qplus, Fcons (make_number (y), Qnil)));
f83f10ba
RS
3169 else
3170 element = Fcons (Qtop, make_number (y));
3171 result = Fcons (element, result);
01f1ba30 3172 }
f83f10ba
RS
3173
3174 if (geometry & WidthValue)
3175 result = Fcons (Fcons (Qwidth, make_number (width)), result);
3176 if (geometry & HeightValue)
3177 result = Fcons (Fcons (Qheight, make_number (height)), result);
3178
3179 return result;
01f1ba30
JB
3180}
3181
01f1ba30 3182/* Calculate the desired size and position of this window,
f83f10ba 3183 and return the flags saying which aspects were specified.
8fc2766b
RS
3184
3185 This function does not make the coordinates positive. */
01f1ba30
JB
3186
3187#define DEFAULT_ROWS 40
3188#define DEFAULT_COLS 80
3189
f9942c9e 3190static int
f676886a
JB
3191x_figure_window_size (f, parms)
3192 struct frame *f;
01f1ba30
JB
3193 Lisp_Object parms;
3194{
4fe1de12 3195 register Lisp_Object tem0, tem1, tem2;
01f1ba30 3196 long window_prompting = 0;
abb4b7ec 3197 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
01f1ba30
JB
3198
3199 /* Default values if we fall through.
3200 Actually, if that happens we should get
b9dc4443 3201 window manager prompting. */
1ab3d87e 3202 SET_FRAME_WIDTH (f, DEFAULT_COLS);
f676886a 3203 f->height = DEFAULT_ROWS;
bd0b85c3
RS
3204 /* Window managers expect that if program-specified
3205 positions are not (0,0), they're intentional, not defaults. */
7556890b
RS
3206 f->output_data.x->top_pos = 0;
3207 f->output_data.x->left_pos = 0;
01f1ba30 3208
333b20bb
GM
3209 tem0 = x_get_arg (dpyinfo, parms, Qheight, 0, 0, RES_TYPE_NUMBER);
3210 tem1 = x_get_arg (dpyinfo, parms, Qwidth, 0, 0, RES_TYPE_NUMBER);
3211 tem2 = x_get_arg (dpyinfo, parms, Quser_size, 0, 0, RES_TYPE_NUMBER);
f83f10ba 3212 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
01f1ba30 3213 {
f83f10ba
RS
3214 if (!EQ (tem0, Qunbound))
3215 {
b7826503 3216 CHECK_NUMBER (tem0);
f83f10ba
RS
3217 f->height = XINT (tem0);
3218 }
3219 if (!EQ (tem1, Qunbound))
3220 {
b7826503 3221 CHECK_NUMBER (tem1);
1ab3d87e 3222 SET_FRAME_WIDTH (f, XINT (tem1));
f83f10ba
RS
3223 }
3224 if (!NILP (tem2) && !EQ (tem2, Qunbound))
4fe1de12
RS
3225 window_prompting |= USSize;
3226 else
3227 window_prompting |= PSize;
01f1ba30 3228 }
01f1ba30 3229
7556890b 3230 f->output_data.x->vertical_scroll_bar_extra
a444c70b
KH
3231 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
3232 ? 0
7556890b 3233 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
b3ba0aa8
KS
3234
3235 x_compute_fringe_widths (f, 0);
3236
7556890b
RS
3237 f->output_data.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3238 f->output_data.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
01f1ba30 3239
333b20bb
GM
3240 tem0 = x_get_arg (dpyinfo, parms, Qtop, 0, 0, RES_TYPE_NUMBER);
3241 tem1 = x_get_arg (dpyinfo, parms, Qleft, 0, 0, RES_TYPE_NUMBER);
3242 tem2 = x_get_arg (dpyinfo, parms, Quser_position, 0, 0, RES_TYPE_NUMBER);
f83f10ba 3243 if (! EQ (tem0, Qunbound) || ! EQ (tem1, Qunbound))
01f1ba30 3244 {
f83f10ba
RS
3245 if (EQ (tem0, Qminus))
3246 {
7556890b 3247 f->output_data.x->top_pos = 0;
f83f10ba
RS
3248 window_prompting |= YNegative;
3249 }
8e713be6
KR
3250 else if (CONSP (tem0) && EQ (XCAR (tem0), Qminus)
3251 && CONSP (XCDR (tem0))
3252 && INTEGERP (XCAR (XCDR (tem0))))
e1d962d7 3253 {
8e713be6 3254 f->output_data.x->top_pos = - XINT (XCAR (XCDR (tem0)));
e1d962d7
RS
3255 window_prompting |= YNegative;
3256 }
8e713be6
KR
3257 else if (CONSP (tem0) && EQ (XCAR (tem0), Qplus)
3258 && CONSP (XCDR (tem0))
3259 && INTEGERP (XCAR (XCDR (tem0))))
e1d962d7 3260 {
8e713be6 3261 f->output_data.x->top_pos = XINT (XCAR (XCDR (tem0)));
e1d962d7 3262 }
f83f10ba 3263 else if (EQ (tem0, Qunbound))
7556890b 3264 f->output_data.x->top_pos = 0;
f83f10ba
RS
3265 else
3266 {
b7826503 3267 CHECK_NUMBER (tem0);
7556890b
RS
3268 f->output_data.x->top_pos = XINT (tem0);
3269 if (f->output_data.x->top_pos < 0)
f83f10ba
RS
3270 window_prompting |= YNegative;
3271 }
3272
3273 if (EQ (tem1, Qminus))
3274 {
7556890b 3275 f->output_data.x->left_pos = 0;
f83f10ba
RS
3276 window_prompting |= XNegative;
3277 }
8e713be6
KR
3278 else if (CONSP (tem1) && EQ (XCAR (tem1), Qminus)
3279 && CONSP (XCDR (tem1))
3280 && INTEGERP (XCAR (XCDR (tem1))))
e1d962d7 3281 {
8e713be6 3282 f->output_data.x->left_pos = - XINT (XCAR (XCDR (tem1)));
e1d962d7
RS
3283 window_prompting |= XNegative;
3284 }
8e713be6
KR
3285 else if (CONSP (tem1) && EQ (XCAR (tem1), Qplus)
3286 && CONSP (XCDR (tem1))
3287 && INTEGERP (XCAR (XCDR (tem1))))
e1d962d7 3288 {
8e713be6 3289 f->output_data.x->left_pos = XINT (XCAR (XCDR (tem1)));
e1d962d7 3290 }
f83f10ba 3291 else if (EQ (tem1, Qunbound))
7556890b 3292 f->output_data.x->left_pos = 0;
f83f10ba
RS
3293 else
3294 {
b7826503 3295 CHECK_NUMBER (tem1);
7556890b
RS
3296 f->output_data.x->left_pos = XINT (tem1);
3297 if (f->output_data.x->left_pos < 0)
f83f10ba
RS
3298 window_prompting |= XNegative;
3299 }
3300
c3724dc2 3301 if (!NILP (tem2) && ! EQ (tem2, Qunbound))
4fe1de12
RS
3302 window_prompting |= USPosition;
3303 else
3304 window_prompting |= PPosition;
01f1ba30 3305 }
f83f10ba 3306
49d41073
EZ
3307 if (f->output_data.x->want_fullscreen != FULLSCREEN_NONE)
3308 {
3309 int left, top;
3310 int width, height;
7273d100 3311
49d41073
EZ
3312 /* It takes both for some WM:s to place it where we want */
3313 window_prompting = USPosition | PPosition;
3314 x_fullscreen_adjust (f, &width, &height, &top, &left);
3315 f->width = width;
3316 f->height = height;
3317 f->output_data.x->pixel_width = CHAR_TO_PIXEL_WIDTH (f, f->width);
3318 f->output_data.x->pixel_height = CHAR_TO_PIXEL_HEIGHT (f, f->height);
3319 f->output_data.x->left_pos = left;
3320 f->output_data.x->top_pos = top;
3321 }
7273d100 3322
739f2f53 3323 return window_prompting;
01f1ba30
JB
3324}
3325
f58534a3
RS
3326#if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
3327
3328Status
3329XSetWMProtocols (dpy, w, protocols, count)
3330 Display *dpy;
3331 Window w;
3332 Atom *protocols;
3333 int count;
3334{
3335 Atom prop;
3336 prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
3337 if (prop == None) return False;
3338 XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
3339 (unsigned char *) protocols, count);
3340 return True;
3341}
9ef48a9d
RS
3342#endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
3343\f
3344#ifdef USE_X_TOOLKIT
3345
8e3d10a9
RS
3346/* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
3347 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
59aa6c90
RS
3348 already be present because of the toolkit (Motif adds some of them,
3349 for example, but Xt doesn't). */
9ef48a9d
RS
3350
3351static void
b9dc4443
RS
3352hack_wm_protocols (f, widget)
3353 FRAME_PTR f;
9ef48a9d
RS
3354 Widget widget;
3355{
3356 Display *dpy = XtDisplay (widget);
3357 Window w = XtWindow (widget);
3358 int need_delete = 1;
3359 int need_focus = 1;
59aa6c90 3360 int need_save = 1;
9ef48a9d
RS
3361
3362 BLOCK_INPUT;
3363 {
3364 Atom type, *atoms = 0;
3365 int format = 0;
3366 unsigned long nitems = 0;
3367 unsigned long bytes_after;
3368
270958e8
KH
3369 if ((XGetWindowProperty (dpy, w,
3370 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
34d5ae1e 3371 (long)0, (long)100, False, XA_ATOM,
270958e8
KH
3372 &type, &format, &nitems, &bytes_after,
3373 (unsigned char **) &atoms)
3374 == Success)
9ef48a9d
RS
3375 && format == 32 && type == XA_ATOM)
3376 while (nitems > 0)
3377 {
3378 nitems--;
b9dc4443
RS
3379 if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
3380 need_delete = 0;
3381 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
3382 need_focus = 0;
3383 else if (atoms[nitems] == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
3384 need_save = 0;
9ef48a9d
RS
3385 }
3386 if (atoms) XFree ((char *) atoms);
3387 }
3388 {
3389 Atom props [10];
3390 int count = 0;
b9dc4443
RS
3391 if (need_delete)
3392 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
3393 if (need_focus)
3394 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
3395 if (need_save)
3396 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
9ef48a9d 3397 if (count)
b9dc4443
RS
3398 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3399 XA_ATOM, 32, PropModeAppend,
9ef48a9d
RS
3400 (unsigned char *) props, count);
3401 }
3402 UNBLOCK_INPUT;
3403}
3404#endif
86779fac
GM
3405
3406
5a7df7d7
GM
3407\f
3408/* Support routines for XIC (X Input Context). */
86779fac 3409
5a7df7d7
GM
3410#ifdef HAVE_X_I18N
3411
3412static XFontSet xic_create_xfontset P_ ((struct frame *, char *));
3413static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
3414
3415
3416/* Supported XIM styles, ordered by preferenc. */
3417
3418static XIMStyle supported_xim_styles[] =
3419{
3420 XIMPreeditPosition | XIMStatusArea,
3421 XIMPreeditPosition | XIMStatusNothing,
3422 XIMPreeditPosition | XIMStatusNone,
3423 XIMPreeditNothing | XIMStatusArea,
3424 XIMPreeditNothing | XIMStatusNothing,
3425 XIMPreeditNothing | XIMStatusNone,
3426 XIMPreeditNone | XIMStatusArea,
3427 XIMPreeditNone | XIMStatusNothing,
3428 XIMPreeditNone | XIMStatusNone,
3429 0,
3430};
3431
3432
3433/* Create an X fontset on frame F with base font name
3434 BASE_FONTNAME.. */
3435
3436static XFontSet
3437xic_create_xfontset (f, base_fontname)
86779fac 3438 struct frame *f;
5a7df7d7 3439 char *base_fontname;
86779fac 3440{
5a7df7d7
GM
3441 XFontSet xfs;
3442 char **missing_list;
3443 int missing_count;
3444 char *def_string;
7273d100 3445
5a7df7d7
GM
3446 xfs = XCreateFontSet (FRAME_X_DISPLAY (f),
3447 base_fontname, &missing_list,
3448 &missing_count, &def_string);
3449 if (missing_list)
3450 XFreeStringList (missing_list);
7273d100 3451
5a7df7d7
GM
3452 /* No need to free def_string. */
3453 return xfs;
3454}
3455
3456
3457/* Value is the best input style, given user preferences USER (already
3458 checked to be supported by Emacs), and styles supported by the
3459 input method XIM. */
3460
3461static XIMStyle
3462best_xim_style (user, xim)
3463 XIMStyles *user;
3464 XIMStyles *xim;
3465{
3466 int i, j;
3467
3468 for (i = 0; i < user->count_styles; ++i)
3469 for (j = 0; j < xim->count_styles; ++j)
3470 if (user->supported_styles[i] == xim->supported_styles[j])
3471 return user->supported_styles[i];
3472
3473 /* Return the default style. */
3474 return XIMPreeditNothing | XIMStatusNothing;
3475}
3476
3477/* Create XIC for frame F. */
3478
5df79d3d
GM
3479static XIMStyle xic_style;
3480
5a7df7d7
GM
3481void
3482create_frame_xic (f)
3483 struct frame *f;
3484{
5a7df7d7
GM
3485 XIM xim;
3486 XIC xic = NULL;
3487 XFontSet xfs = NULL;
86779fac 3488
5a7df7d7
GM
3489 if (FRAME_XIC (f))
3490 return;
7273d100 3491
5a7df7d7
GM
3492 xim = FRAME_X_XIM (f);
3493 if (xim)
3494 {
d9d57cb2
DL
3495 XRectangle s_area;
3496 XPoint spot;
5a7df7d7
GM
3497 XVaNestedList preedit_attr;
3498 XVaNestedList status_attr;
3499 char *base_fontname;
3500 int fontset;
3501
d9d57cb2
DL
3502 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
3503 spot.x = 0; spot.y = 1;
5a7df7d7
GM
3504 /* Create X fontset. */
3505 fontset = FRAME_FONTSET (f);
3506 if (fontset < 0)
3507 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3508 else
3509 {
6ecb43ce
KH
3510 /* Determine the base fontname from the ASCII font name of
3511 FONTSET. */
d5db4077 3512 char *ascii_font = (char *) SDATA (fontset_ascii (fontset));
6ecb43ce 3513 char *p = ascii_font;
5a7df7d7 3514 int i;
6ecb43ce
KH
3515
3516 for (i = 0; *p; p++)
3517 if (*p == '-') i++;
3518 if (i != 14)
3519 /* As the font name doesn't conform to XLFD, we can't
3520 modify it to get a suitable base fontname for the
3521 frame. */
3522 base_fontname = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
3523 else
3524 {
3525 int len = strlen (ascii_font) + 1;
8ec8a5ec 3526 char *p1 = NULL;
6ecb43ce
KH
3527
3528 for (i = 0, p = ascii_font; i < 8; p++)
3529 {
3530 if (*p == '-')
3531 {
3532 i++;
3533 if (i == 3)
3534 p1 = p + 1;
3535 }
3536 }
3537 base_fontname = (char *) alloca (len);
3538 bzero (base_fontname, len);
3539 strcpy (base_fontname, "-*-*-");
3540 bcopy (p1, base_fontname + 5, p - p1);
3541 strcat (base_fontname, "*-*-*-*-*-*-*");
3542 }
5a7df7d7
GM
3543 }
3544 xfs = xic_create_xfontset (f, base_fontname);
86779fac 3545
5a7df7d7
GM
3546 /* Determine XIC style. */
3547 if (xic_style == 0)
3548 {
3549 XIMStyles supported_list;
3550 supported_list.count_styles = (sizeof supported_xim_styles
3551 / sizeof supported_xim_styles[0]);
3552 supported_list.supported_styles = supported_xim_styles;
3553 xic_style = best_xim_style (&supported_list,
3554 FRAME_X_XIM_STYLES (f));
3555 }
86779fac 3556
5a7df7d7
GM
3557 preedit_attr = XVaCreateNestedList (0,
3558 XNFontSet, xfs,
3559 XNForeground,
3560 FRAME_FOREGROUND_PIXEL (f),
3561 XNBackground,
3562 FRAME_BACKGROUND_PIXEL (f),
3563 (xic_style & XIMPreeditPosition
3564 ? XNSpotLocation
3565 : NULL),
3566 &spot,
3567 NULL);
3568 status_attr = XVaCreateNestedList (0,
3569 XNArea,
3570 &s_area,
3571 XNFontSet,
3572 xfs,
3573 XNForeground,
3574 FRAME_FOREGROUND_PIXEL (f),
3575 XNBackground,
3576 FRAME_BACKGROUND_PIXEL (f),
3577 NULL);
3578
3579 xic = XCreateIC (xim,
3580 XNInputStyle, xic_style,
3581 XNClientWindow, FRAME_X_WINDOW(f),
3582 XNFocusWindow, FRAME_X_WINDOW(f),
3583 XNStatusAttributes, status_attr,
3584 XNPreeditAttributes, preedit_attr,
3585 NULL);
3586 XFree (preedit_attr);
3587 XFree (status_attr);
3588 }
7273d100 3589
5a7df7d7
GM
3590 FRAME_XIC (f) = xic;
3591 FRAME_XIC_STYLE (f) = xic_style;
3592 FRAME_XIC_FONTSET (f) = xfs;
86779fac
GM
3593}
3594
5a7df7d7
GM
3595
3596/* Destroy XIC and free XIC fontset of frame F, if any. */
3597
3598void
3599free_frame_xic (f)
3600 struct frame *f;
3601{
3602 if (FRAME_XIC (f) == NULL)
3603 return;
7273d100 3604
5a7df7d7
GM
3605 XDestroyIC (FRAME_XIC (f));
3606 if (FRAME_XIC_FONTSET (f))
3607 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3608
3609 FRAME_XIC (f) = NULL;
3610 FRAME_XIC_FONTSET (f) = NULL;
3611}
3612
3613
3614/* Place preedit area for XIC of window W's frame to specified
3615 pixel position X/Y. X and Y are relative to window W. */
3616
3617void
3618xic_set_preeditarea (w, x, y)
3619 struct window *w;
3620 int x, y;
3621{
3622 struct frame *f = XFRAME (w->frame);
3623 XVaNestedList attr;
3624 XPoint spot;
7273d100 3625
5a7df7d7
GM
3626 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x);
3627 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
3628 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
3629 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3630 XFree (attr);
3631}
3632
3633
3634/* Place status area for XIC in bottom right corner of frame F.. */
3635
3636void
3637xic_set_statusarea (f)
3638 struct frame *f;
3639{
3640 XIC xic = FRAME_XIC (f);
3641 XVaNestedList attr;
3642 XRectangle area;
3643 XRectangle *needed;
3644
3645 /* Negotiate geometry of status area. If input method has existing
3646 status area, use its current size. */
3647 area.x = area.y = area.width = area.height = 0;
3648 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
3649 XSetICValues (xic, XNStatusAttributes, attr, NULL);
3650 XFree (attr);
7273d100 3651
5a7df7d7
GM
3652 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
3653 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3654 XFree (attr);
3655
3656 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
3657 {
3658 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
3659 XGetICValues (xic, XNStatusAttributes, attr, NULL);
3660 XFree (attr);
3661 }
3662
3663 area.width = needed->width;
3664 area.height = needed->height;
3665 area.x = PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
3666 area.y = (PIXEL_HEIGHT (f) - area.height
3667 - FRAME_MENUBAR_HEIGHT (f) - FRAME_INTERNAL_BORDER_WIDTH (f));
3668 XFree (needed);
3669
3670 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
3671 XSetICValues(xic, XNStatusAttributes, attr, NULL);
3672 XFree (attr);
3673}
3674
3675
3676/* Set X fontset for XIC of frame F, using base font name
3677 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
3678
3679void
3680xic_set_xfontset (f, base_fontname)
3681 struct frame *f;
3682 char *base_fontname;
3683{
3684 XVaNestedList attr;
3685 XFontSet xfs;
3686
3687 xfs = xic_create_xfontset (f, base_fontname);
3688
3689 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
3690 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
3691 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
3692 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
3693 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
3694 XFree (attr);
7273d100 3695
5a7df7d7
GM
3696 if (FRAME_XIC_FONTSET (f))
3697 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
3698 FRAME_XIC_FONTSET (f) = xfs;
3699}
3700
3701#endif /* HAVE_X_I18N */
3702
3703
9ef48a9d 3704\f
8fc2766b
RS
3705#ifdef USE_X_TOOLKIT
3706
3707/* Create and set up the X widget for frame F. */
f58534a3 3708
01f1ba30 3709static void
a7f7d550
FP
3710x_window (f, window_prompting, minibuffer_only)
3711 struct frame *f;
3712 long window_prompting;
3713 int minibuffer_only;
01f1ba30 3714{
9ef48a9d 3715 XClassHint class_hints;
31ac8d8c
FP
3716 XSetWindowAttributes attributes;
3717 unsigned long attribute_mask;
9ef48a9d
RS
3718 Widget shell_widget;
3719 Widget pane_widget;
6c32dd68 3720 Widget frame_widget;
9ef48a9d
RS
3721 Arg al [25];
3722 int ac;
3723
3724 BLOCK_INPUT;
3725
b7975ee4
KH
3726 /* Use the resource name as the top-level widget name
3727 for looking up resources. Make a non-Lisp copy
3728 for the window manager, so GC relocation won't bother it.
3729
3730 Elsewhere we specify the window name for the window manager. */
7273d100 3731
cca176a0 3732 {
d5db4077 3733 char *str = (char *) SDATA (Vx_resource_name);
b7975ee4 3734 f->namebuf = (char *) xmalloc (strlen (str) + 1);
cca176a0
KH
3735 strcpy (f->namebuf, str);
3736 }
9ef48a9d
RS
3737
3738 ac = 0;
3739 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
3740 XtSetArg (al[ac], XtNinput, 1); ac++;
97787173 3741 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
7556890b 3742 XtSetArg (al[ac], XtNborderWidth, f->output_data.x->border_width); ac++;
9b2956e2
GM
3743 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3744 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3745 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
cca176a0 3746 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
7a994728 3747 applicationShellWidgetClass,
82c90203 3748 FRAME_X_DISPLAY (f), al, ac);
9ef48a9d 3749
7556890b 3750 f->output_data.x->widget = shell_widget;
9ef48a9d
RS
3751 /* maybe_set_screen_title_format (shell_widget); */
3752
6c32dd68
PR
3753 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
3754 (widget_value *) NULL,
3755 shell_widget, False,
3756 (lw_callback) NULL,
3757 (lw_callback) NULL,
b6e11efd 3758 (lw_callback) NULL,
6c32dd68 3759 (lw_callback) NULL);
9ef48a9d 3760
9b2956e2
GM
3761 ac = 0;
3762 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3763 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3764 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3765 XtSetValues (pane_widget, al, ac);
7556890b 3766 f->output_data.x->column_widget = pane_widget;
a7f7d550 3767
7273d100 3768 /* mappedWhenManaged to false tells to the paned window to not map/unmap
5e65b9ab 3769 the emacs screen when changing menubar. This reduces flickering. */
9ef48a9d
RS
3770
3771 ac = 0;
3772 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
3773 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
3774 XtSetArg (al[ac], XtNallowResize, 1); ac++;
3775 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
3776 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
9b2956e2
GM
3777 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
3778 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
3779 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
3780 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
3781 al, ac);
7273d100 3782
7556890b 3783 f->output_data.x->edit_widget = frame_widget;
7273d100
FP
3784
3785 XtManageChild (frame_widget);
a7f7d550
FP
3786
3787 /* Do some needed geometry management. */
3788 {
3789 int len;
3790 char *tem, shell_position[32];
3791 Arg al[2];
3792 int ac = 0;
5031cc10 3793 int extra_borders = 0;
7273d100 3794 int menubar_size
7556890b
RS
3795 = (f->output_data.x->menubar_widget
3796 ? (f->output_data.x->menubar_widget->core.height
3797 + f->output_data.x->menubar_widget->core.border_width)
8fc2766b 3798 : 0);
a7f7d550 3799
f7008aff
RS
3800#if 0 /* Experimentally, we now get the right results
3801 for -geometry -0-0 without this. 24 Aug 96, rms. */
01cbdba5
RS
3802 if (FRAME_EXTERNAL_MENU_BAR (f))
3803 {
dd254b21 3804 Dimension ibw = 0;
01cbdba5
RS
3805 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
3806 menubar_size += ibw;
3807 }
f7008aff 3808#endif
01cbdba5 3809
7556890b 3810 f->output_data.x->menubar_height = menubar_size;
00983aba 3811
440b0bfd 3812#ifndef USE_LUCID
5031cc10
KH
3813 /* Motif seems to need this amount added to the sizes
3814 specified for the shell widget. The Athena/Lucid widgets don't.
3815 Both conclusions reached experimentally. -- rms. */
440b0bfd
RS
3816 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
3817 &extra_borders, NULL);
3818 extra_borders *= 2;
3819#endif
5031cc10 3820
97787173
RS
3821 /* Convert our geometry parameters into a geometry string
3822 and specify it.
3823 Note that we do not specify here whether the position
3824 is a user-specified or program-specified one.
3825 We pass that information later, in x_wm_set_size_hints. */
3826 {
7556890b 3827 int left = f->output_data.x->left_pos;
97787173 3828 int xneg = window_prompting & XNegative;
7556890b 3829 int top = f->output_data.x->top_pos;
97787173
RS
3830 int yneg = window_prompting & YNegative;
3831 if (xneg)
3832 left = -left;
3833 if (yneg)
3834 top = -top;
c760f47e
KH
3835
3836 if (window_prompting & USPosition)
5031cc10 3837 sprintf (shell_position, "=%dx%d%c%d%c%d",
7273d100 3838 PIXEL_WIDTH (f) + extra_borders,
5031cc10 3839 PIXEL_HEIGHT (f) + menubar_size + extra_borders,
c760f47e
KH
3840 (xneg ? '-' : '+'), left,
3841 (yneg ? '-' : '+'), top);
3842 else
5031cc10 3843 sprintf (shell_position, "=%dx%d",
7273d100 3844 PIXEL_WIDTH (f) + extra_borders,
5031cc10 3845 PIXEL_HEIGHT (f) + menubar_size + extra_borders);
97787173
RS
3846 }
3847
a7f7d550 3848 len = strlen (shell_position) + 1;
77110caa
RS
3849 /* We don't free this because we don't know whether
3850 it is safe to free it while the frame exists.
3851 It isn't worth the trouble of arranging to free it
3852 when the frame is deleted. */
a7f7d550
FP
3853 tem = (char *) xmalloc (len);
3854 strncpy (tem, shell_position, len);
3855 XtSetArg (al[ac], XtNgeometry, tem); ac++;
3856 XtSetValues (shell_widget, al, ac);
3857 }
3858
9ef48a9d
RS
3859 XtManageChild (pane_widget);
3860 XtRealizeWidget (shell_widget);
3861
7273d100 3862 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
9ef48a9d
RS
3863
3864 validate_x_resource_name ();
b7975ee4 3865
d5db4077
KR
3866 class_hints.res_name = (char *) SDATA (Vx_resource_name);
3867 class_hints.res_class = (char *) SDATA (Vx_resource_class);
b9dc4443 3868 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
5a7df7d7
GM
3869
3870#ifdef HAVE_X_I18N
3871 FRAME_XIC (f) = NULL;
4bd777b8 3872#ifdef USE_XIM
5a7df7d7 3873 create_frame_xic (f);
4bd777b8 3874#endif
5a7df7d7 3875#endif
64d16748 3876
7556890b
RS
3877 f->output_data.x->wm_hints.input = True;
3878 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 3879 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 3880 &f->output_data.x->wm_hints);
b8228beb 3881
c4ec904f 3882 hack_wm_protocols (f, shell_widget);
9ef48a9d 3883
6c32dd68
PR
3884#ifdef HACK_EDITRES
3885 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
3886#endif
3887
9ef48a9d 3888 /* Do a stupid property change to force the server to generate a
333b20bb 3889 PropertyNotify event so that the event_stream server timestamp will
9ef48a9d
RS
3890 be initialized to something relevant to the time we created the window.
3891 */
6c32dd68 3892 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
b9dc4443
RS
3893 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
3894 XA_ATOM, 32, PropModeAppend,
9ef48a9d
RS
3895 (unsigned char*) NULL, 0);
3896
5a7df7d7 3897 /* Make all the standard events reach the Emacs frame. */
31ac8d8c 3898 attributes.event_mask = STANDARD_EVENT_SET;
5a7df7d7
GM
3899
3900#ifdef HAVE_X_I18N
3901 if (FRAME_XIC (f))
3902 {
3903 /* XIM server might require some X events. */
3904 unsigned long fevent = NoEventMask;
3905 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3906 attributes.event_mask |= fevent;
3907 }
3908#endif /* HAVE_X_I18N */
7273d100 3909
31ac8d8c
FP
3910 attribute_mask = CWEventMask;
3911 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
3912 attribute_mask, &attributes);
3913
6c32dd68 3914 XtMapWidget (frame_widget);
9ef48a9d 3915
8fc2766b
RS
3916 /* x_set_name normally ignores requests to set the name if the
3917 requested name is the same as the current name. This is the one
3918 place where that assumption isn't correct; f->name is set, but
3919 the X server hasn't been told. */
3920 {
3921 Lisp_Object name;
3922 int explicit = f->explicit_name;
3923
3924 f->explicit_name = 0;
3925 name = f->name;
3926 f->name = Qnil;
3927 x_set_name (f, name, explicit);
3928 }
3929
b9dc4443 3930 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 3931 f->output_data.x->text_cursor);
8fc2766b
RS
3932
3933 UNBLOCK_INPUT;
3934
495fa05e
GM
3935 /* This is a no-op, except under Motif. Make sure main areas are
3936 set to something reasonable, in case we get an error later. */
3937 lw_set_main_areas (pane_widget, 0, frame_widget);
8fc2766b
RS
3938}
3939
9ef48a9d
RS
3940#else /* not USE_X_TOOLKIT */
3941
8fc2766b
RS
3942/* Create and set up the X window for frame F. */
3943
201d8c78 3944void
8fc2766b
RS
3945x_window (f)
3946 struct frame *f;
3947
3948{
3949 XClassHint class_hints;
3950 XSetWindowAttributes attributes;
3951 unsigned long attribute_mask;
3952
7556890b
RS
3953 attributes.background_pixel = f->output_data.x->background_pixel;
3954 attributes.border_pixel = f->output_data.x->border_pixel;
01f1ba30
JB
3955 attributes.bit_gravity = StaticGravity;
3956 attributes.backing_store = NotUseful;
3957 attributes.save_under = True;
3958 attributes.event_mask = STANDARD_EVENT_SET;
9b2956e2
GM
3959 attributes.colormap = FRAME_X_COLORMAP (f);
3960 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
3961 | CWColormap);
01f1ba30
JB
3962
3963 BLOCK_INPUT;
fe24a618 3964 FRAME_X_WINDOW (f)
b9dc4443 3965 = XCreateWindow (FRAME_X_DISPLAY (f),
7556890b
RS
3966 f->output_data.x->parent_desc,
3967 f->output_data.x->left_pos,
3968 f->output_data.x->top_pos,
f676886a 3969 PIXEL_WIDTH (f), PIXEL_HEIGHT (f),
7556890b 3970 f->output_data.x->border_width,
01f1ba30
JB
3971 CopyFromParent, /* depth */
3972 InputOutput, /* class */
383d6ffc 3973 FRAME_X_VISUAL (f),
01f1ba30 3974 attribute_mask, &attributes);
5a7df7d7
GM
3975
3976#ifdef HAVE_X_I18N
4bd777b8 3977#ifdef USE_XIM
5a7df7d7
GM
3978 create_frame_xic (f);
3979 if (FRAME_XIC (f))
3980 {
3981 /* XIM server might require some X events. */
3982 unsigned long fevent = NoEventMask;
3983 XGetICValues(FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
3984 attributes.event_mask |= fevent;
3985 attribute_mask = CWEventMask;
3986 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3987 attribute_mask, &attributes);
3988 }
4bd777b8 3989#endif
5a7df7d7 3990#endif /* HAVE_X_I18N */
7273d100 3991
d387c960 3992 validate_x_resource_name ();
b7975ee4 3993
d5db4077
KR
3994 class_hints.res_name = (char *) SDATA (Vx_resource_name);
3995 class_hints.res_class = (char *) SDATA (Vx_resource_class);
b9dc4443 3996 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
01f1ba30 3997
00983aba
KH
3998 /* The menubar is part of the ordinary display;
3999 it does not count in addition to the height of the window. */
7556890b 4000 f->output_data.x->menubar_height = 0;
00983aba 4001
179956b9
JB
4002 /* This indicates that we use the "Passive Input" input model.
4003 Unless we do this, we don't get the Focus{In,Out} events that we
4004 need to draw the cursor correctly. Accursed bureaucrats.
b9dc4443 4005 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
179956b9 4006
7556890b
RS
4007 f->output_data.x->wm_hints.input = True;
4008 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 4009 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 4010 &f->output_data.x->wm_hints);
6d078211 4011 f->output_data.x->wm_hints.icon_pixmap = None;
179956b9 4012
032e4ebe
RS
4013 /* Request "save yourself" and "delete window" commands from wm. */
4014 {
4015 Atom protocols[2];
b9dc4443
RS
4016 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
4017 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
4018 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
032e4ebe 4019 }
9ef48a9d 4020
e373f201
JB
4021 /* x_set_name normally ignores requests to set the name if the
4022 requested name is the same as the current name. This is the one
4023 place where that assumption isn't correct; f->name is set, but
4024 the X server hasn't been told. */
4025 {
98381190 4026 Lisp_Object name;
cf177271 4027 int explicit = f->explicit_name;
e373f201 4028
cf177271 4029 f->explicit_name = 0;
98381190
KH
4030 name = f->name;
4031 f->name = Qnil;
cf177271 4032 x_set_name (f, name, explicit);
e373f201
JB
4033 }
4034
b9dc4443 4035 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 4036 f->output_data.x->text_cursor);
9ef48a9d 4037
01f1ba30
JB
4038 UNBLOCK_INPUT;
4039
fe24a618 4040 if (FRAME_X_WINDOW (f) == 0)
9ef48a9d 4041 error ("Unable to create window");
01f1ba30
JB
4042}
4043
8fc2766b
RS
4044#endif /* not USE_X_TOOLKIT */
4045
01f1ba30
JB
4046/* Handle the icon stuff for this window. Perhaps later we might
4047 want an x_set_icon_position which can be called interactively as
b9dc4443 4048 well. */
01f1ba30
JB
4049
4050static void
f676886a
JB
4051x_icon (f, parms)
4052 struct frame *f;
01f1ba30
JB
4053 Lisp_Object parms;
4054{
f9942c9e 4055 Lisp_Object icon_x, icon_y;
abb4b7ec 4056 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
01f1ba30
JB
4057
4058 /* Set the position of the icon. Note that twm groups all
b9dc4443 4059 icons in an icon window. */
333b20bb
GM
4060 icon_x = x_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
4061 icon_y = x_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
f9942c9e 4062 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
01f1ba30 4063 {
b7826503
PJ
4064 CHECK_NUMBER (icon_x);
4065 CHECK_NUMBER (icon_y);
01f1ba30 4066 }
f9942c9e 4067 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
01f1ba30 4068 error ("Both left and top icon corners of icon must be specified");
01f1ba30 4069
f9942c9e
JB
4070 BLOCK_INPUT;
4071
fe24a618
JB
4072 if (! EQ (icon_x, Qunbound))
4073 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
f9942c9e 4074
01f1ba30 4075 /* Start up iconic or window? */
49795535 4076 x_wm_set_window_state
333b20bb
GM
4077 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
4078 Qicon)
49795535
JB
4079 ? IconicState
4080 : NormalState));
01f1ba30 4081
d5db4077 4082 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
f468da95 4083 ? f->icon_name
d5db4077 4084 : f->name)));
80534dd6 4085
01f1ba30
JB
4086 UNBLOCK_INPUT;
4087}
4088
b243755a 4089/* Make the GCs needed for this window, setting the
01f1ba30
JB
4090 background, border and mouse colors; also create the
4091 mouse cursor and the gray border tile. */
4092
f945b920
JB
4093static char cursor_bits[] =
4094 {
4095 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4096 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4097 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4098 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4099 };
4100
01f1ba30 4101static void
f676886a
JB
4102x_make_gc (f)
4103 struct frame *f;
01f1ba30
JB
4104{
4105 XGCValues gc_values;
01f1ba30 4106
6afb1d07
JB
4107 BLOCK_INPUT;
4108
b243755a 4109 /* Create the GCs of this frame.
9ef48a9d 4110 Note that many default values are used. */
01f1ba30
JB
4111
4112 /* Normal video */
7556890b
RS
4113 gc_values.font = f->output_data.x->font->fid;
4114 gc_values.foreground = f->output_data.x->foreground_pixel;
4115 gc_values.background = f->output_data.x->background_pixel;
9ef48a9d 4116 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
959e647d
GM
4117 f->output_data.x->normal_gc
4118 = XCreateGC (FRAME_X_DISPLAY (f),
4119 FRAME_X_WINDOW (f),
4120 GCLineWidth | GCFont | GCForeground | GCBackground,
4121 &gc_values);
01f1ba30 4122
b9dc4443 4123 /* Reverse video style. */
7556890b
RS
4124 gc_values.foreground = f->output_data.x->background_pixel;
4125 gc_values.background = f->output_data.x->foreground_pixel;
959e647d
GM
4126 f->output_data.x->reverse_gc
4127 = XCreateGC (FRAME_X_DISPLAY (f),
4128 FRAME_X_WINDOW (f),
4129 GCFont | GCForeground | GCBackground | GCLineWidth,
4130 &gc_values);
01f1ba30 4131
9ef48a9d 4132 /* Cursor has cursor-color background, background-color foreground. */
7556890b
RS
4133 gc_values.foreground = f->output_data.x->background_pixel;
4134 gc_values.background = f->output_data.x->cursor_pixel;
01f1ba30
JB
4135 gc_values.fill_style = FillOpaqueStippled;
4136 gc_values.stipple
b9dc4443
RS
4137 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
4138 FRAME_X_DISPLAY_INFO (f)->root_window,
01f1ba30 4139 cursor_bits, 16, 16);
7556890b 4140 f->output_data.x->cursor_gc
b9dc4443 4141 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
01f1ba30 4142 (GCFont | GCForeground | GCBackground
ac1f48a4 4143 | GCFillStyle /* | GCStipple */ | GCLineWidth),
01f1ba30
JB
4144 &gc_values);
4145
333b20bb
GM
4146 /* Reliefs. */
4147 f->output_data.x->white_relief.gc = 0;
4148 f->output_data.x->black_relief.gc = 0;
4149
01f1ba30 4150 /* Create the gray border tile used when the pointer is not in
f676886a 4151 the frame. Since this depends on the frame's pixel values,
9ef48a9d 4152 this must be done on a per-frame basis. */
7556890b 4153 f->output_data.x->border_tile
d043f1a4 4154 = (XCreatePixmapFromBitmapData
7273d100 4155 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
d043f1a4 4156 gray_bits, gray_width, gray_height,
7556890b
RS
4157 f->output_data.x->foreground_pixel,
4158 f->output_data.x->background_pixel,
ab452f99 4159 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
6afb1d07
JB
4160
4161 UNBLOCK_INPUT;
01f1ba30 4162}
01f1ba30 4163
959e647d
GM
4164
4165/* Free what was was allocated in x_make_gc. */
4166
4167void
4168x_free_gcs (f)
4169 struct frame *f;
4170{
4171 Display *dpy = FRAME_X_DISPLAY (f);
4172
4173 BLOCK_INPUT;
7273d100 4174
959e647d
GM
4175 if (f->output_data.x->normal_gc)
4176 {
4177 XFreeGC (dpy, f->output_data.x->normal_gc);
4178 f->output_data.x->normal_gc = 0;
4179 }
4180
4181 if (f->output_data.x->reverse_gc)
4182 {
4183 XFreeGC (dpy, f->output_data.x->reverse_gc);
4184 f->output_data.x->reverse_gc = 0;
4185 }
7273d100 4186
959e647d
GM
4187 if (f->output_data.x->cursor_gc)
4188 {
4189 XFreeGC (dpy, f->output_data.x->cursor_gc);
4190 f->output_data.x->cursor_gc = 0;
4191 }
4192
4193 if (f->output_data.x->border_tile)
4194 {
4195 XFreePixmap (dpy, f->output_data.x->border_tile);
4196 f->output_data.x->border_tile = 0;
4197 }
4198
4199 UNBLOCK_INPUT;
4200}
4201
4202
eaf1eea9
GM
4203/* Handler for signals raised during x_create_frame and
4204 x_create_top_frame. FRAME is the frame which is partially
4205 constructed. */
4206
4207static Lisp_Object
4208unwind_create_frame (frame)
4209 Lisp_Object frame;
4210{
4211 struct frame *f = XFRAME (frame);
4212
4213 /* If frame is ``official'', nothing to do. */
4214 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
4215 {
f1d2ce7f 4216#if GLYPH_DEBUG
eaf1eea9
GM
4217 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
4218#endif
7273d100 4219
eaf1eea9
GM
4220 x_free_frame_resources (f);
4221
4222 /* Check that reference counts are indeed correct. */
4223 xassert (dpyinfo->reference_count == dpyinfo_refcount);
4224 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
c844a81a 4225 return Qt;
eaf1eea9 4226 }
7273d100 4227
eaf1eea9
GM
4228 return Qnil;
4229}
4230
4231
f676886a 4232DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
01f1ba30 4233 1, 1, 0,
7ee72033 4234 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
c061c855
GM
4235Returns an Emacs frame object.
4236ALIST is an alist of frame parameters.
4237If the parameters specify that the frame should not have a minibuffer,
4238and do not specify a specific minibuffer window to use,
4239then `default-minibuffer-frame' must be a frame whose minibuffer can
4240be shared by the new frame.
4241
7ee72033
MB
4242This function is an internal primitive--use `make-frame' instead. */)
4243 (parms)
01f1ba30
JB
4244 Lisp_Object parms;
4245{
f676886a 4246 struct frame *f;
2365c027 4247 Lisp_Object frame, tem;
01f1ba30
JB
4248 Lisp_Object name;
4249 int minibuffer_only = 0;
4250 long window_prompting = 0;
4251 int width, height;
331379bf 4252 int count = SPECPDL_INDEX ();
ecaca587 4253 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
b9dc4443 4254 Lisp_Object display;
333b20bb 4255 struct x_display_info *dpyinfo = NULL;
a59e4f3d 4256 Lisp_Object parent;
e557f19d 4257 struct kboard *kb;
01f1ba30 4258
11ae94fe 4259 check_x ();
01f1ba30 4260
b7975ee4
KH
4261 /* Use this general default value to start with
4262 until we know if this frame has a specified name. */
4263 Vx_resource_name = Vinvocation_name;
4264
333b20bb 4265 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
b9dc4443
RS
4266 if (EQ (display, Qunbound))
4267 display = Qnil;
4268 dpyinfo = check_x_display_info (display);
e557f19d
KH
4269#ifdef MULTI_KBOARD
4270 kb = dpyinfo->kboard;
4271#else
4272 kb = &the_only_kboard;
4273#endif
b9dc4443 4274
333b20bb 4275 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
6a5e54e2 4276 if (!STRINGP (name)
cf177271
JB
4277 && ! EQ (name, Qunbound)
4278 && ! NILP (name))
08a90d6a 4279 error ("Invalid frame name--not a string or nil");
01f1ba30 4280
b7975ee4
KH
4281 if (STRINGP (name))
4282 Vx_resource_name = name;
4283
a59e4f3d 4284 /* See if parent window is specified. */
333b20bb 4285 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
a59e4f3d
RS
4286 if (EQ (parent, Qunbound))
4287 parent = Qnil;
4288 if (! NILP (parent))
b7826503 4289 CHECK_NUMBER (parent);
a59e4f3d 4290
ecaca587
RS
4291 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
4292 /* No need to protect DISPLAY because that's not used after passing
4293 it to make_frame_without_minibuffer. */
4294 frame = Qnil;
4295 GCPRO4 (parms, parent, name, frame);
333b20bb
GM
4296 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
4297 RES_TYPE_SYMBOL);
f9942c9e 4298 if (EQ (tem, Qnone) || NILP (tem))
2526c290 4299 f = make_frame_without_minibuffer (Qnil, kb, display);
f9942c9e 4300 else if (EQ (tem, Qonly))
01f1ba30 4301 {
f676886a 4302 f = make_minibuffer_frame ();
01f1ba30
JB
4303 minibuffer_only = 1;
4304 }
6a5e54e2 4305 else if (WINDOWP (tem))
2526c290 4306 f = make_frame_without_minibuffer (tem, kb, display);
f9942c9e
JB
4307 else
4308 f = make_frame (1);
01f1ba30 4309
ecaca587
RS
4310 XSETFRAME (frame, f);
4311
a3c87d4e
JB
4312 /* Note that X Windows does support scroll bars. */
4313 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
179956b9 4314
08a90d6a 4315 f->output_method = output_x_window;
7556890b
RS
4316 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
4317 bzero (f->output_data.x, sizeof (struct x_output));
4318 f->output_data.x->icon_bitmap = -1;
0ecca023 4319 f->output_data.x->fontset = -1;
333b20bb
GM
4320 f->output_data.x->scroll_bar_foreground_pixel = -1;
4321 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
4322#ifdef USE_TOOLKIT_SCROLL_BARS
4323 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4324 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4325#endif /* USE_TOOLKIT_SCROLL_BARS */
eaf1eea9 4326 record_unwind_protect (unwind_create_frame, frame);
08a90d6a 4327
f468da95 4328 f->icon_name
333b20bb
GM
4329 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
4330 RES_TYPE_STRING);
f468da95
RS
4331 if (! STRINGP (f->icon_name))
4332 f->icon_name = Qnil;
80534dd6 4333
08a90d6a 4334 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
f1d2ce7f 4335#if GLYPH_DEBUG
eaf1eea9
GM
4336 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
4337 dpyinfo_refcount = dpyinfo->reference_count;
4338#endif /* GLYPH_DEBUG */
73410c76 4339#ifdef MULTI_KBOARD
e557f19d 4340 FRAME_KBOARD (f) = kb;
73410c76 4341#endif
08a90d6a 4342
9b2956e2
GM
4343 /* These colors will be set anyway later, but it's important
4344 to get the color reference counts right, so initialize them! */
4345 {
4346 Lisp_Object black;
4347 struct gcpro gcpro1;
cefecbcf
GM
4348
4349 /* Function x_decode_color can signal an error. Make
4350 sure to initialize color slots so that we won't try
4351 to free colors we haven't allocated. */
4352 f->output_data.x->foreground_pixel = -1;
4353 f->output_data.x->background_pixel = -1;
4354 f->output_data.x->cursor_pixel = -1;
4355 f->output_data.x->cursor_foreground_pixel = -1;
4356 f->output_data.x->border_pixel = -1;
4357 f->output_data.x->mouse_pixel = -1;
7273d100 4358
9b2956e2
GM
4359 black = build_string ("black");
4360 GCPRO1 (black);
4361 f->output_data.x->foreground_pixel
4362 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4363 f->output_data.x->background_pixel
4364 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4365 f->output_data.x->cursor_pixel
4366 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4367 f->output_data.x->cursor_foreground_pixel
4368 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4369 f->output_data.x->border_pixel
4370 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4371 f->output_data.x->mouse_pixel
4372 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4373 UNGCPRO;
4374 }
4375
a59e4f3d
RS
4376 /* Specify the parent under which to make this X window. */
4377
4378 if (!NILP (parent))
4379 {
8c239ac3 4380 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
7556890b 4381 f->output_data.x->explicit_parent = 1;
a59e4f3d
RS
4382 }
4383 else
4384 {
7556890b
RS
4385 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4386 f->output_data.x->explicit_parent = 0;
a59e4f3d
RS
4387 }
4388
cf177271
JB
4389 /* Set the name; the functions to which we pass f expect the name to
4390 be set. */
4391 if (EQ (name, Qunbound) || NILP (name))
4392 {
08a90d6a 4393 f->name = build_string (dpyinfo->x_id_name);
cf177271
JB
4394 f->explicit_name = 0;
4395 }
4396 else
4397 {
4398 f->name = name;
4399 f->explicit_name = 1;
9ef48a9d
RS
4400 /* use the frame's title when getting resources for this frame. */
4401 specbind (Qx_resource_name, name);
cf177271 4402 }
01f1ba30 4403
01f1ba30
JB
4404 /* Extract the window parameters from the supplied values
4405 that are needed to determine window geometry. */
d387c960
JB
4406 {
4407 Lisp_Object font;
4408
333b20bb 4409 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2ee3abaa 4410
6817eab4 4411 BLOCK_INPUT;
e5e548e3
RS
4412 /* First, try whatever font the caller has specified. */
4413 if (STRINGP (font))
942ea06d 4414 {
49965a29 4415 tem = Fquery_fontset (font, Qnil);
477f8642 4416 if (STRINGP (tem))
d5db4077 4417 font = x_new_fontset (f, SDATA (tem));
942ea06d 4418 else
d5db4077 4419 font = x_new_font (f, SDATA (font));
942ea06d 4420 }
7273d100 4421
e5e548e3 4422 /* Try out a font which we hope has bold and italic variations. */
333b20bb
GM
4423 if (!STRINGP (font))
4424 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
e5e548e3 4425 if (!STRINGP (font))
a6ac02af 4426 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
e5e548e3 4427 if (! STRINGP (font))
a6ac02af 4428 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
e5e548e3
RS
4429 if (! STRINGP (font))
4430 /* This was formerly the first thing tried, but it finds too many fonts
4431 and takes too long. */
4432 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
4433 /* If those didn't work, look for something which will at least work. */
4434 if (! STRINGP (font))
a6ac02af 4435 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
6817eab4
JB
4436 UNBLOCK_INPUT;
4437 if (! STRINGP (font))
e5e548e3
RS
4438 font = build_string ("fixed");
4439
477f8642 4440 x_default_parameter (f, parms, Qfont, font,
333b20bb 4441 "font", "Font", RES_TYPE_STRING);
d387c960 4442 }
9ef48a9d 4443
e3881aa0 4444#ifdef USE_LUCID
82c90203
RS
4445 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
4446 whereby it fails to get any font. */
7556890b 4447 xlwmenu_default_font = f->output_data.x->font;
dd254b21 4448#endif
82c90203 4449
cf177271 4450 x_default_parameter (f, parms, Qborder_width, make_number (2),
333b20bb 4451 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7273d100 4452
4e397688 4453 /* This defaults to 1 in order to match xterm. We recognize either
ddf768c3
JB
4454 internalBorderWidth or internalBorder (which is what xterm calls
4455 it). */
4456 if (NILP (Fassq (Qinternal_border_width, parms)))
4457 {
4458 Lisp_Object value;
4459
abb4b7ec 4460 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
333b20bb 4461 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
ddf768c3
JB
4462 if (! EQ (value, Qunbound))
4463 parms = Fcons (Fcons (Qinternal_border_width, value),
4464 parms);
4465 }
dca97592 4466 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
333b20bb
GM
4467 "internalBorderWidth", "internalBorderWidth",
4468 RES_TYPE_NUMBER);
1ab3d87e 4469 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
333b20bb
GM
4470 "verticalScrollBars", "ScrollBars",
4471 RES_TYPE_SYMBOL);
01f1ba30 4472
b9dc4443 4473 /* Also do the stuff which must be set before the window exists. */
cf177271 4474 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
333b20bb 4475 "foreground", "Foreground", RES_TYPE_STRING);
cf177271 4476 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
0b60fc91 4477 "background", "Background", RES_TYPE_STRING);
cf177271 4478 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
333b20bb 4479 "pointerColor", "Foreground", RES_TYPE_STRING);
cf177271 4480 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
333b20bb 4481 "cursorColor", "Foreground", RES_TYPE_STRING);
cf177271 4482 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
333b20bb 4483 "borderColor", "BorderColor", RES_TYPE_STRING);
d62c8769
GM
4484 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
4485 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
563b67aa
GM
4486 x_default_parameter (f, parms, Qline_spacing, Qnil,
4487 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
b3ba0aa8
KS
4488 x_default_parameter (f, parms, Qleft_fringe, Qnil,
4489 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
4490 x_default_parameter (f, parms, Qright_fringe, Qnil,
4491 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
333b20bb
GM
4492
4493 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
4494 "scrollBarForeground",
4495 "ScrollBarForeground", 1);
4496 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
4497 "scrollBarBackground",
4498 "ScrollBarBackground", 0);
4499
4500 /* Init faces before x_default_parameter is called for scroll-bar
4501 parameters because that function calls x_set_scroll_bar_width,
4502 which calls change_frame_size, which calls Fset_window_buffer,
4503 which runs hooks, which call Fvertical_motion. At the end, we
4504 end up in init_iterator with a null face cache, which should not
4505 happen. */
4506 init_frame_faces (f);
7273d100 4507
c7bcb20d 4508 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
333b20bb 4509 "menuBar", "MenuBar", RES_TYPE_NUMBER);
e33455ca 4510 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
333b20bb 4511 "toolBar", "ToolBar", RES_TYPE_NUMBER);
79873d50 4512 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
333b20bb
GM
4513 "bufferPredicate", "BufferPredicate",
4514 RES_TYPE_SYMBOL);
c2304e02 4515 x_default_parameter (f, parms, Qtitle, Qnil,
333b20bb 4516 "title", "Title", RES_TYPE_STRING);
ea0a1f53
GM
4517 x_default_parameter (f, parms, Qwait_for_wm, Qt,
4518 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
49d41073
EZ
4519 x_default_parameter (f, parms, Qfullscreen, Qnil,
4520 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
90eb1019 4521
7556890b 4522 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
35f59f6b
GM
4523
4524 /* Add the tool-bar height to the initial frame height so that the
4525 user gets a text display area of the size he specified with -g or
4526 via .Xdefaults. Later changes of the tool-bar height don't
4527 change the frame size. This is done so that users can create
4528 tall Emacs frames without having to guess how tall the tool-bar
4529 will get. */
4530 if (FRAME_TOOL_BAR_LINES (f))
4531 {
4532 int margin, relief, bar_height;
7273d100 4533
8ed86491 4534 relief = (tool_bar_button_relief >= 0
35f59f6b
GM
4535 ? tool_bar_button_relief
4536 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
4537
4538 if (INTEGERP (Vtool_bar_button_margin)
4539 && XINT (Vtool_bar_button_margin) > 0)
4540 margin = XFASTINT (Vtool_bar_button_margin);
4541 else if (CONSP (Vtool_bar_button_margin)
4542 && INTEGERP (XCDR (Vtool_bar_button_margin))
4543 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
4544 margin = XFASTINT (XCDR (Vtool_bar_button_margin));
4545 else
4546 margin = 0;
7273d100 4547
35f59f6b
GM
4548 bar_height = DEFAULT_TOOL_BAR_IMAGE_HEIGHT + 2 * margin + 2 * relief;
4549 f->height += (bar_height + CANON_Y_UNIT (f) - 1) / CANON_Y_UNIT (f);
4550 }
4551
4552 /* Compute the size of the X window. */
f676886a 4553 window_prompting = x_figure_window_size (f, parms);
01f1ba30 4554
f83f10ba 4555 if (window_prompting & XNegative)
2365c027 4556 {
f83f10ba 4557 if (window_prompting & YNegative)
7556890b 4558 f->output_data.x->win_gravity = SouthEastGravity;
f83f10ba 4559 else
7556890b 4560 f->output_data.x->win_gravity = NorthEastGravity;
f83f10ba
RS
4561 }
4562 else
4563 {
4564 if (window_prompting & YNegative)
7556890b 4565 f->output_data.x->win_gravity = SouthWestGravity;
f83f10ba 4566 else
7556890b 4567 f->output_data.x->win_gravity = NorthWestGravity;
2365c027
RS
4568 }
4569
7556890b 4570 f->output_data.x->size_hint_flags = window_prompting;
38d22040 4571
495fa05e
GM
4572 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
4573 f->no_split = minibuffer_only || EQ (tem, Qt);
4574
6a1bcd01 4575 /* Create the X widget or window. */
a7f7d550
FP
4576#ifdef USE_X_TOOLKIT
4577 x_window (f, window_prompting, minibuffer_only);
4578#else
f676886a 4579 x_window (f);
a7f7d550 4580#endif
7273d100 4581
f676886a
JB
4582 x_icon (f, parms);
4583 x_make_gc (f);
01f1ba30 4584
495fa05e
GM
4585 /* Now consider the frame official. */
4586 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4587 Vframe_list = Fcons (frame, Vframe_list);
4588
f9942c9e
JB
4589 /* We need to do this after creating the X window, so that the
4590 icon-creation functions can say whose icon they're describing. */
cf177271 4591 x_default_parameter (f, parms, Qicon_type, Qnil,
333b20bb 4592 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
f9942c9e 4593
cf177271 4594 x_default_parameter (f, parms, Qauto_raise, Qnil,
333b20bb 4595 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
cf177271 4596 x_default_parameter (f, parms, Qauto_lower, Qnil,
333b20bb 4597 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
dbc4e1c1 4598 x_default_parameter (f, parms, Qcursor_type, Qbox,
333b20bb 4599 "cursorType", "CursorType", RES_TYPE_SYMBOL);
28d7281d
GM
4600 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
4601 "scrollBarWidth", "ScrollBarWidth",
4602 RES_TYPE_NUMBER);
f9942c9e 4603
f676886a 4604 /* Dimensions, especially f->height, must be done via change_frame_size.
01f1ba30 4605 Change will not be effected unless different from the current
b9dc4443 4606 f->height. */
f676886a
JB
4607 width = f->width;
4608 height = f->height;
7273d100 4609
1ab3d87e
RS
4610 f->height = 0;
4611 SET_FRAME_WIDTH (f, 0);
8938a4fb 4612 change_frame_size (f, height, width, 1, 0, 0);
d043f1a4 4613
4a967a9b
GM
4614 /* Set up faces after all frame parameters are known. This call
4615 also merges in face attributes specified for new frames. If we
4616 don't do this, the `menu' face for instance won't have the right
4617 colors, and the menu bar won't appear in the specified colors for
4618 new frames. */
4619 call1 (Qface_set_after_frame_default, frame);
4620
495fa05e
GM
4621#ifdef USE_X_TOOLKIT
4622 /* Create the menu bar. */
4623 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
4624 {
4625 /* If this signals an error, we haven't set size hints for the
4626 frame and we didn't make it visible. */
4627 initialize_frame_menubar (f);
4628
4629 /* This is a no-op, except under Motif where it arranges the
4630 main window for the widgets on it. */
4631 lw_set_main_areas (f->output_data.x->column_widget,
4632 f->output_data.x->menubar_widget,
4633 f->output_data.x->edit_widget);
4634 }
4635#endif /* USE_X_TOOLKIT */
4636
4637 /* Tell the server what size and position, etc, we want, and how
4638 badly we want them. This should be done after we have the menu
4639 bar so that its size can be taken into account. */
01f1ba30 4640 BLOCK_INPUT;
7989f084 4641 x_wm_set_size_hint (f, window_prompting, 0);
01f1ba30
JB
4642 UNBLOCK_INPUT;
4643
495fa05e
GM
4644 /* Make the window appear on the frame and enable display, unless
4645 the caller says not to. However, with explicit parent, Emacs
4646 cannot control visibility, so don't try. */
7556890b 4647 if (! f->output_data.x->explicit_parent)
a59e4f3d
RS
4648 {
4649 Lisp_Object visibility;
49795535 4650
333b20bb
GM
4651 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
4652 RES_TYPE_SYMBOL);
a59e4f3d
RS
4653 if (EQ (visibility, Qunbound))
4654 visibility = Qt;
49795535 4655
a59e4f3d
RS
4656 if (EQ (visibility, Qicon))
4657 x_iconify_frame (f);
4658 else if (! NILP (visibility))
4659 x_make_frame_visible (f);
4660 else
4661 /* Must have been Qnil. */
4662 ;
4663 }
01f1ba30 4664
495fa05e 4665 UNGCPRO;
9e57df62
GM
4666
4667 /* Make sure windows on this frame appear in calls to next-window
4668 and similar functions. */
4669 Vwindow_list = Qnil;
7273d100 4670
9ef48a9d 4671 return unbind_to (count, frame);
01f1ba30
JB
4672}
4673
eaf1eea9 4674
0d17d282
KH
4675/* FRAME is used only to get a handle on the X display. We don't pass the
4676 display info directly because we're called from frame.c, which doesn't
4677 know about that structure. */
e4f79258 4678
87498171 4679Lisp_Object
0d17d282
KH
4680x_get_focus_frame (frame)
4681 struct frame *frame;
87498171 4682{
0d17d282 4683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
87498171 4684 Lisp_Object xfocus;
0d17d282 4685 if (! dpyinfo->x_focus_frame)
87498171
KH
4686 return Qnil;
4687
0d17d282 4688 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
87498171
KH
4689 return xfocus;
4690}
f0614854 4691
3decc1e7
GM
4692
4693/* In certain situations, when the window manager follows a
4694 click-to-focus policy, there seems to be no way around calling
4695 XSetInputFocus to give another frame the input focus .
4696
4697 In an ideal world, XSetInputFocus should generally be avoided so
4698 that applications don't interfere with the window manager's focus
4699 policy. But I think it's okay to use when it's clearly done
4700 following a user-command. */
4701
4702DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
7ee72033
MB
4703 doc: /* Set the input focus to FRAME.
4704FRAME nil means use the selected frame. */)
4705 (frame)
3decc1e7
GM
4706 Lisp_Object frame;
4707{
4708 struct frame *f = check_x_frame (frame);
4709 Display *dpy = FRAME_X_DISPLAY (f);
4710 int count;
4711
4712 BLOCK_INPUT;
4713 count = x_catch_errors (dpy);
4714 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4715 RevertToParent, CurrentTime);
4716 x_uncatch_errors (dpy, count);
4717 UNBLOCK_INPUT;
7273d100 4718
3decc1e7
GM
4719 return Qnil;
4720}
4721
f0614854 4722\f
2d764c78 4723DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
7ee72033
MB
4724 doc: /* Internal function called by `color-defined-p', which see. */)
4725 (color, frame)
b9dc4443 4726 Lisp_Object color, frame;
e12d55b2 4727{
b9dc4443
RS
4728 XColor foo;
4729 FRAME_PTR f = check_x_frame (frame);
e12d55b2 4730
b7826503 4731 CHECK_STRING (color);
b9dc4443 4732
d5db4077 4733 if (x_defined_color (f, SDATA (color), &foo, 0))
e12d55b2
RS
4734 return Qt;
4735 else
4736 return Qnil;
4737}
4738
2d764c78 4739DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
7ee72033
MB
4740 doc: /* Internal function called by `color-values', which see. */)
4741 (color, frame)
b9dc4443 4742 Lisp_Object color, frame;
01f1ba30 4743{
b9dc4443
RS
4744 XColor foo;
4745 FRAME_PTR f = check_x_frame (frame);
4746
b7826503 4747 CHECK_STRING (color);
01f1ba30 4748
d5db4077 4749 if (x_defined_color (f, SDATA (color), &foo, 0))
57c82a63
RS
4750 {
4751 Lisp_Object rgb[3];
4752
4753 rgb[0] = make_number (foo.red);
4754 rgb[1] = make_number (foo.green);
4755 rgb[2] = make_number (foo.blue);
4756 return Flist (3, rgb);
4757 }
01f1ba30
JB
4758 else
4759 return Qnil;
4760}
4761
2d764c78 4762DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
7ee72033
MB
4763 doc: /* Internal function called by `display-color-p', which see. */)
4764 (display)
08a90d6a 4765 Lisp_Object display;
01f1ba30 4766{
08a90d6a 4767 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 4768
b9dc4443 4769 if (dpyinfo->n_planes <= 2)
01f1ba30
JB
4770 return Qnil;
4771
b9dc4443 4772 switch (dpyinfo->visual->class)
01f1ba30
JB
4773 {
4774 case StaticColor:
4775 case PseudoColor:
4776 case TrueColor:
4777 case DirectColor:
4778 return Qt;
4779
4780 default:
4781 return Qnil;
4782 }
4783}
4784
d0c9d219 4785DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
c061c855 4786 0, 1, 0,
7ee72033 4787 doc: /* Return t if the X display supports shades of gray.
c061c855
GM
4788Note that color displays do support shades of gray.
4789The optional argument DISPLAY specifies which display to ask about.
4790DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4791If omitted or nil, that stands for the selected frame's display. */)
4792 (display)
08a90d6a 4793 Lisp_Object display;
d0c9d219 4794{
08a90d6a 4795 struct x_display_info *dpyinfo = check_x_display_info (display);
d0c9d219 4796
ae6b58f9 4797 if (dpyinfo->n_planes <= 1)
b9dc4443
RS
4798 return Qnil;
4799
ae6b58f9
RS
4800 switch (dpyinfo->visual->class)
4801 {
4802 case StaticColor:
4803 case PseudoColor:
4804 case TrueColor:
4805 case DirectColor:
4806 case StaticGray:
4807 case GrayScale:
4808 return Qt;
4809
4810 default:
4811 return Qnil;
4812 }
d0c9d219
RS
4813}
4814
41beb8fc 4815DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
c061c855 4816 0, 1, 0,
7ee72033 4817 doc: /* Returns the width in pixels of the X display DISPLAY.
c061c855
GM
4818The optional argument DISPLAY specifies which display to ask about.
4819DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4820If omitted or nil, that stands for the selected frame's display. */)
4821 (display)
08a90d6a 4822 Lisp_Object display;
41beb8fc 4823{
08a90d6a 4824 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4825
4826 return make_number (dpyinfo->width);
41beb8fc
RS
4827}
4828
4829DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
c061c855 4830 Sx_display_pixel_height, 0, 1, 0,
7ee72033 4831 doc: /* Returns the height in pixels of the X display DISPLAY.
c061c855
GM
4832The optional argument DISPLAY specifies which display to ask about.
4833DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4834If omitted or nil, that stands for the selected frame's display. */)
4835 (display)
08a90d6a 4836 Lisp_Object display;
41beb8fc 4837{
08a90d6a 4838 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4839
4840 return make_number (dpyinfo->height);
41beb8fc
RS
4841}
4842
4843DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
c061c855 4844 0, 1, 0,
7ee72033 4845 doc: /* Returns the number of bitplanes of the X display DISPLAY.
c061c855
GM
4846The optional argument DISPLAY specifies which display to ask about.
4847DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4848If omitted or nil, that stands for the selected frame's display. */)
4849 (display)
08a90d6a 4850 Lisp_Object display;
41beb8fc 4851{
08a90d6a 4852 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4853
4854 return make_number (dpyinfo->n_planes);
41beb8fc
RS
4855}
4856
4857DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
c061c855 4858 0, 1, 0,
7ee72033 4859 doc: /* Returns the number of color cells of the X display DISPLAY.
c061c855
GM
4860The optional argument DISPLAY specifies which display to ask about.
4861DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4862If omitted or nil, that stands for the selected frame's display. */)
4863 (display)
08a90d6a 4864 Lisp_Object display;
41beb8fc 4865{
08a90d6a 4866 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4867
4868 return make_number (DisplayCells (dpyinfo->display,
4869 XScreenNumberOfScreen (dpyinfo->screen)));
41beb8fc
RS
4870}
4871
9d317b2c
RS
4872DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
4873 Sx_server_max_request_size,
c061c855 4874 0, 1, 0,
7ee72033 4875 doc: /* Returns the maximum request size of the X server of display DISPLAY.
c061c855
GM
4876The optional argument DISPLAY specifies which display to ask about.
4877DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4878If omitted or nil, that stands for the selected frame's display. */)
4879 (display)
08a90d6a 4880 Lisp_Object display;
9d317b2c 4881{
08a90d6a 4882 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4883
4884 return make_number (MAXREQUEST (dpyinfo->display));
9d317b2c
RS
4885}
4886
41beb8fc 4887DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
7ee72033 4888 doc: /* Returns the vendor ID string of the X server of display DISPLAY.
c061c855
GM
4889The optional argument DISPLAY specifies which display to ask about.
4890DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4891If omitted or nil, that stands for the selected frame's display. */)
4892 (display)
08a90d6a 4893 Lisp_Object display;
41beb8fc 4894{
08a90d6a 4895 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4896 char *vendor = ServerVendor (dpyinfo->display);
4897
41beb8fc
RS
4898 if (! vendor) vendor = "";
4899 return build_string (vendor);
4900}
4901
4902DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
7ee72033 4903 doc: /* Returns the version numbers of the X server of display DISPLAY.
c061c855
GM
4904The value is a list of three integers: the major and minor
4905version numbers of the X Protocol in use, and the vendor-specific release
4906number. See also the function `x-server-vendor'.
4907
4908The optional argument DISPLAY specifies which display to ask about.
4909DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4910If omitted or nil, that stands for the selected frame's display. */)
4911 (display)
08a90d6a 4912 Lisp_Object display;
41beb8fc 4913{
08a90d6a 4914 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443 4915 Display *dpy = dpyinfo->display;
11ae94fe 4916
41beb8fc
RS
4917 return Fcons (make_number (ProtocolVersion (dpy)),
4918 Fcons (make_number (ProtocolRevision (dpy)),
4919 Fcons (make_number (VendorRelease (dpy)), Qnil)));
4920}
4921
4922DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
7ee72033 4923 doc: /* Return the number of screens on the X server of display DISPLAY.
c061c855
GM
4924The optional argument DISPLAY specifies which display to ask about.
4925DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4926If omitted or nil, that stands for the selected frame's display. */)
4927 (display)
08a90d6a 4928 Lisp_Object display;
41beb8fc 4929{
08a90d6a 4930 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4931
4932 return make_number (ScreenCount (dpyinfo->display));
41beb8fc
RS
4933}
4934
4935DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
7ee72033 4936 doc: /* Return the height in millimeters of the X display DISPLAY.
c061c855
GM
4937The optional argument DISPLAY specifies which display to ask about.
4938DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4939If omitted or nil, that stands for the selected frame's display. */)
4940 (display)
08a90d6a 4941 Lisp_Object display;
41beb8fc 4942{
08a90d6a 4943 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4944
4945 return make_number (HeightMMOfScreen (dpyinfo->screen));
41beb8fc
RS
4946}
4947
4948DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
7ee72033 4949 doc: /* Return the width in millimeters of the X display DISPLAY.
c061c855
GM
4950The optional argument DISPLAY specifies which display to ask about.
4951DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4952If omitted or nil, that stands for the selected frame's display. */)
4953 (display)
08a90d6a 4954 Lisp_Object display;
41beb8fc 4955{
08a90d6a 4956 struct x_display_info *dpyinfo = check_x_display_info (display);
b9dc4443
RS
4957
4958 return make_number (WidthMMOfScreen (dpyinfo->screen));
41beb8fc
RS
4959}
4960
4961DEFUN ("x-display-backing-store", Fx_display_backing_store,
c061c855 4962 Sx_display_backing_store, 0, 1, 0,
7ee72033 4963 doc: /* Returns an indication of whether X display DISPLAY does backing store.
c061c855
GM
4964The value may be `always', `when-mapped', or `not-useful'.
4965The optional argument DISPLAY specifies which display to ask about.
4966DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
4967If omitted or nil, that stands for the selected frame's display. */)
4968 (display)
08a90d6a 4969 Lisp_Object display;
41beb8fc 4970{
08a90d6a 4971 struct x_display_info *dpyinfo = check_x_display_info (display);
8ec8a5ec 4972 Lisp_Object result;
11ae94fe 4973
b9dc4443 4974 switch (DoesBackingStore (dpyinfo->screen))
41beb8fc
RS
4975 {
4976 case Always:
8ec8a5ec
GM
4977 result = intern ("always");
4978 break;
41beb8fc
RS
4979
4980 case WhenMapped:
8ec8a5ec
GM
4981 result = intern ("when-mapped");
4982 break;
41beb8fc
RS
4983
4984 case NotUseful:
8ec8a5ec
GM
4985 result = intern ("not-useful");
4986 break;
41beb8fc
RS
4987
4988 default:
4989 error ("Strange value for BackingStore parameter of screen");
8ec8a5ec 4990 result = Qnil;
41beb8fc 4991 }
8ec8a5ec
GM
4992
4993 return result;
41beb8fc
RS
4994}
4995
4996DEFUN ("x-display-visual-class", Fx_display_visual_class,
c061c855 4997 Sx_display_visual_class, 0, 1, 0,
7ee72033 4998 doc: /* Return the visual class of the X display DISPLAY.
c061c855
GM
4999The value is one of the symbols `static-gray', `gray-scale',
5000`static-color', `pseudo-color', `true-color', or `direct-color'.
5001
5002The optional argument DISPLAY specifies which display to ask about.
5003DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
5004If omitted or nil, that stands for the selected frame's display. */)
5005 (display)
08a90d6a 5006 Lisp_Object display;
41beb8fc 5007{
08a90d6a 5008 struct x_display_info *dpyinfo = check_x_display_info (display);
8ec8a5ec 5009 Lisp_Object result;
11ae94fe 5010
b9dc4443 5011 switch (dpyinfo->visual->class)
41beb8fc 5012 {
8ec8a5ec
GM
5013 case StaticGray:
5014 result = intern ("static-gray");
5015 break;
5016 case GrayScale:
5017 result = intern ("gray-scale");
5018 break;
5019 case StaticColor:
5020 result = intern ("static-color");
5021 break;
5022 case PseudoColor:
5023 result = intern ("pseudo-color");
5024 break;
5025 case TrueColor:
5026 result = intern ("true-color");
5027 break;
5028 case DirectColor:
5029 result = intern ("direct-color");
5030 break;
41beb8fc
RS
5031 default:
5032 error ("Display has an unknown visual class");
8ec8a5ec 5033 result = Qnil;
41beb8fc 5034 }
7273d100 5035
8ec8a5ec 5036 return result;
41beb8fc
RS
5037}
5038
5039DEFUN ("x-display-save-under", Fx_display_save_under,
c061c855 5040 Sx_display_save_under, 0, 1, 0,
7ee72033 5041 doc: /* Returns t if the X display DISPLAY supports the save-under feature.
c061c855
GM
5042The optional argument DISPLAY specifies which display to ask about.
5043DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
5044If omitted or nil, that stands for the selected frame's display. */)
5045 (display)
08a90d6a 5046 Lisp_Object display;
41beb8fc 5047{
08a90d6a 5048 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 5049
b9dc4443 5050 if (DoesSaveUnders (dpyinfo->screen) == True)
41beb8fc
RS
5051 return Qt;
5052 else
5053 return Qnil;
5054}
5055\f
b9dc4443 5056int
55caf99c
RS
5057x_pixel_width (f)
5058 register struct frame *f;
01f1ba30 5059{
55caf99c 5060 return PIXEL_WIDTH (f);
01f1ba30
JB
5061}
5062
b9dc4443 5063int
55caf99c
RS
5064x_pixel_height (f)
5065 register struct frame *f;
01f1ba30 5066{
55caf99c
RS
5067 return PIXEL_HEIGHT (f);
5068}
5069
b9dc4443 5070int
55caf99c
RS
5071x_char_width (f)
5072 register struct frame *f;
5073{
7556890b 5074 return FONT_WIDTH (f->output_data.x->font);
55caf99c
RS
5075}
5076
b9dc4443 5077int
55caf99c
RS
5078x_char_height (f)
5079 register struct frame *f;
5080{
7556890b 5081 return f->output_data.x->line_height;
01f1ba30 5082}
b9dc4443
RS
5083
5084int
f03f2489
RS
5085x_screen_planes (f)
5086 register struct frame *f;
b9dc4443 5087{
f03f2489 5088 return FRAME_X_DISPLAY_INFO (f)->n_planes;
b9dc4443 5089}
01f1ba30 5090
a6ad00c0
GM
5091
5092\f
5093/************************************************************************
5094 X Displays
5095 ************************************************************************/
5096
01f1ba30 5097\f
a6ad00c0
GM
5098/* Mapping visual names to visuals. */
5099
5100static struct visual_class
5101{
5102 char *name;
5103 int class;
5104}
5105visual_classes[] =
5106{
5107 {"StaticGray", StaticGray},
5108 {"GrayScale", GrayScale},
5109 {"StaticColor", StaticColor},
5110 {"PseudoColor", PseudoColor},
5111 {"TrueColor", TrueColor},
5112 {"DirectColor", DirectColor},
9908a324 5113 {NULL, 0}
a6ad00c0
GM
5114};
5115
5116
404daac1 5117#ifndef HAVE_XSCREENNUMBEROFSCREEN
a6ad00c0
GM
5118
5119/* Value is the screen number of screen SCR. This is a substitute for
5120 the X function with the same name when that doesn't exist. */
5121
404daac1
RS
5122int
5123XScreenNumberOfScreen (scr)
5124 register Screen *scr;
5125{
a6ad00c0
GM
5126 Display *dpy = scr->display;
5127 int i;
3df34fdb 5128
a6ad00c0 5129 for (i = 0; i < dpy->nscreens; ++i)
fbd5ceb2 5130 if (scr == dpy->screens + i)
a6ad00c0 5131 break;
404daac1 5132
a6ad00c0 5133 return i;
404daac1 5134}
a6ad00c0 5135
404daac1
RS
5136#endif /* not HAVE_XSCREENNUMBEROFSCREEN */
5137
01f1ba30 5138
a6ad00c0
GM
5139/* Select the visual that should be used on display DPYINFO. Set
5140 members of DPYINFO appropriately. Called from x_term_init. */
fe24a618 5141
a6ad00c0
GM
5142void
5143select_visual (dpyinfo)
5144 struct x_display_info *dpyinfo;
5145{
5146 Display *dpy = dpyinfo->display;
5147 Screen *screen = dpyinfo->screen;
5148 Lisp_Object value;
fe24a618 5149
a6ad00c0
GM
5150 /* See if a visual is specified. */
5151 value = display_x_get_resource (dpyinfo,
5152 build_string ("visualClass"),
5153 build_string ("VisualClass"),
5154 Qnil, Qnil);
5155 if (STRINGP (value))
5156 {
5157 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
5158 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
5159 depth, a decimal number. NAME is compared with case ignored. */
d5db4077 5160 char *s = (char *) alloca (SBYTES (value) + 1);
a6ad00c0
GM
5161 char *dash;
5162 int i, class = -1;
5163 XVisualInfo vinfo;
5164
d5db4077 5165 strcpy (s, SDATA (value));
a6ad00c0
GM
5166 dash = index (s, '-');
5167 if (dash)
5168 {
5169 dpyinfo->n_planes = atoi (dash + 1);
5170 *dash = '\0';
5171 }
5172 else
5173 /* We won't find a matching visual with depth 0, so that
5174 an error will be printed below. */
5175 dpyinfo->n_planes = 0;
f0614854 5176
a6ad00c0
GM
5177 /* Determine the visual class. */
5178 for (i = 0; visual_classes[i].name; ++i)
5179 if (xstricmp (s, visual_classes[i].name) == 0)
5180 {
5181 class = visual_classes[i].class;
5182 break;
5183 }
01f1ba30 5184
a6ad00c0
GM
5185 /* Look up a matching visual for the specified class. */
5186 if (class == -1
5187 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
5188 dpyinfo->n_planes, class, &vinfo))
d5db4077 5189 fatal ("Invalid visual specification `%s'", SDATA (value));
7273d100 5190
a6ad00c0
GM
5191 dpyinfo->visual = vinfo.visual;
5192 }
01f1ba30
JB
5193 else
5194 {
a6ad00c0
GM
5195 int n_visuals;
5196 XVisualInfo *vinfo, vinfo_template;
7273d100 5197
a6ad00c0
GM
5198 dpyinfo->visual = DefaultVisualOfScreen (screen);
5199
5200#ifdef HAVE_X11R4
5201 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
5202#else
5203 vinfo_template.visualid = dpyinfo->visual->visualid;
5204#endif
5205 vinfo_template.screen = XScreenNumberOfScreen (screen);
5206 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
5207 &vinfo_template, &n_visuals);
5208 if (n_visuals != 1)
5209 fatal ("Can't get proper X visual info");
5210
94ac875b 5211 dpyinfo->n_planes = vinfo->depth;
a6ad00c0
GM
5212 XFree ((char *) vinfo);
5213 }
01f1ba30 5214}
01f1ba30 5215
a6ad00c0 5216
b9dc4443
RS
5217/* Return the X display structure for the display named NAME.
5218 Open a new connection if necessary. */
5219
5220struct x_display_info *
5221x_display_info_for_name (name)
5222 Lisp_Object name;
5223{
08a90d6a 5224 Lisp_Object names;
b9dc4443
RS
5225 struct x_display_info *dpyinfo;
5226
b7826503 5227 CHECK_STRING (name);
b9dc4443 5228
806048df
RS
5229 if (! EQ (Vwindow_system, intern ("x")))
5230 error ("Not using X Windows");
5231
08a90d6a
RS
5232 for (dpyinfo = x_display_list, names = x_display_name_list;
5233 dpyinfo;
8e713be6 5234 dpyinfo = dpyinfo->next, names = XCDR (names))
b9dc4443
RS
5235 {
5236 Lisp_Object tem;
8e713be6 5237 tem = Fstring_equal (XCAR (XCAR (names)), name);
08a90d6a 5238 if (!NILP (tem))
b9dc4443
RS
5239 return dpyinfo;
5240 }
5241
b7975ee4
KH
5242 /* Use this general default value to start with. */
5243 Vx_resource_name = Vinvocation_name;
5244
b9dc4443
RS
5245 validate_x_resource_name ();
5246
9b207e8e 5247 dpyinfo = x_term_init (name, (char *)0,
d5db4077 5248 (char *) SDATA (Vx_resource_name));
b9dc4443 5249
08a90d6a 5250 if (dpyinfo == 0)
d5db4077 5251 error ("Cannot connect to X server %s", SDATA (name));
08a90d6a 5252
b9dc4443
RS
5253 x_in_use = 1;
5254 XSETFASTINT (Vwindow_system_version, 11);
5255
5256 return dpyinfo;
5257}
5258
a6ad00c0 5259
01f1ba30 5260DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
c061c855 5261 1, 3, 0,
7ee72033 5262 doc: /* Open a connection to an X server.
c061c855
GM
5263DISPLAY is the name of the display to connect to.
5264Optional second arg XRM-STRING is a string of resources in xrdb format.
5265If the optional third arg MUST-SUCCEED is non-nil,
7ee72033
MB
5266terminate Emacs if we can't open the connection. */)
5267 (display, xrm_string, must_succeed)
08a90d6a 5268 Lisp_Object display, xrm_string, must_succeed;
01f1ba30 5269{
01f1ba30 5270 unsigned char *xrm_option;
b9dc4443 5271 struct x_display_info *dpyinfo;
01f1ba30 5272
b7826503 5273 CHECK_STRING (display);
d387c960 5274 if (! NILP (xrm_string))
b7826503 5275 CHECK_STRING (xrm_string);
01f1ba30 5276
806048df
RS
5277 if (! EQ (Vwindow_system, intern ("x")))
5278 error ("Not using X Windows");
5279
d387c960 5280 if (! NILP (xrm_string))
d5db4077 5281 xrm_option = (unsigned char *) SDATA (xrm_string);
01f1ba30
JB
5282 else
5283 xrm_option = (unsigned char *) 0;
d387c960
JB
5284
5285 validate_x_resource_name ();
5286
e1b1bee8 5287 /* This is what opens the connection and sets x_current_display.
b9dc4443
RS
5288 This also initializes many symbols, such as those used for input. */
5289 dpyinfo = x_term_init (display, xrm_option,
d5db4077 5290 (char *) SDATA (Vx_resource_name));
f1c16f36 5291
08a90d6a
RS
5292 if (dpyinfo == 0)
5293 {
5294 if (!NILP (must_succeed))
10ffbc14
GM
5295 fatal ("Cannot connect to X server %s.\n\
5296Check the DISPLAY environment variable or use `-d'.\n\
842a9389
JB
5297Also use the `xauth' program to verify that you have the proper\n\
5298authorization information needed to connect the X server.\n\
bf770132 5299An insecure way to solve the problem may be to use `xhost'.\n",
d5db4077 5300 SDATA (display));
08a90d6a 5301 else
d5db4077 5302 error ("Cannot connect to X server %s", SDATA (display));
08a90d6a
RS
5303 }
5304
b9dc4443 5305 x_in_use = 1;
01f1ba30 5306
b9dc4443 5307 XSETFASTINT (Vwindow_system_version, 11);
01f1ba30
JB
5308 return Qnil;
5309}
5310
08a90d6a
RS
5311DEFUN ("x-close-connection", Fx_close_connection,
5312 Sx_close_connection, 1, 1, 0,
7ee72033 5313 doc: /* Close the connection to DISPLAY's X server.
c061c855 5314For DISPLAY, specify either a frame or a display name (a string).
7ee72033
MB
5315If DISPLAY is nil, that stands for the selected frame's display. */)
5316 (display)
c061c855 5317 Lisp_Object display;
01f1ba30 5318{
08a90d6a 5319 struct x_display_info *dpyinfo = check_x_display_info (display);
08a90d6a 5320 int i;
3457bc6e 5321
08a90d6a
RS
5322 if (dpyinfo->reference_count > 0)
5323 error ("Display still has frames on it");
01f1ba30 5324
08a90d6a
RS
5325 BLOCK_INPUT;
5326 /* Free the fonts in the font table. */
5327 for (i = 0; i < dpyinfo->n_fonts; i++)
333b20bb
GM
5328 if (dpyinfo->font_table[i].name)
5329 {
6ecb43ce
KH
5330 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
5331 xfree (dpyinfo->font_table[i].full_name);
333b20bb 5332 xfree (dpyinfo->font_table[i].name);
333b20bb
GM
5333 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
5334 }
5335
08a90d6a
RS
5336 x_destroy_all_bitmaps (dpyinfo);
5337 XSetCloseDownMode (dpyinfo->display, DestroyAll);
82c90203
RS
5338
5339#ifdef USE_X_TOOLKIT
5340 XtCloseDisplay (dpyinfo->display);
5341#else
08a90d6a 5342 XCloseDisplay (dpyinfo->display);
82c90203 5343#endif
08a90d6a
RS
5344
5345 x_delete_display (dpyinfo);
5346 UNBLOCK_INPUT;
3457bc6e 5347
01f1ba30
JB
5348 return Qnil;
5349}
5350
08a90d6a 5351DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7ee72033
MB
5352 doc: /* Return the list of display names that Emacs has connections to. */)
5353 ()
08a90d6a
RS
5354{
5355 Lisp_Object tail, result;
5356
5357 result = Qnil;
8e713be6
KR
5358 for (tail = x_display_name_list; ! NILP (tail); tail = XCDR (tail))
5359 result = Fcons (XCAR (XCAR (tail)), result);
08a90d6a
RS
5360
5361 return result;
5362}
5363
5364DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7ee72033 5365 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
c061c855
GM
5366If ON is nil, allow buffering of requests.
5367Turning on synchronization prohibits the Xlib routines from buffering
5368requests and seriously degrades performance, but makes debugging much
5369easier.
5370The optional second argument DISPLAY specifies which display to act on.
5371DISPLAY should be either a frame or a display name (a string).
7ee72033
MB
5372If DISPLAY is omitted or nil, that stands for the selected frame's display. */)
5373 (on, display)
08a90d6a 5374 Lisp_Object display, on;
01f1ba30 5375{
08a90d6a 5376 struct x_display_info *dpyinfo = check_x_display_info (display);
11ae94fe 5377
b9dc4443 5378 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
01f1ba30
JB
5379
5380 return Qnil;
5381}
5382
b9dc4443 5383/* Wait for responses to all X commands issued so far for frame F. */
6b7b1820
RS
5384
5385void
b9dc4443
RS
5386x_sync (f)
5387 FRAME_PTR f;
6b7b1820 5388{
4e87f4d2 5389 BLOCK_INPUT;
b9dc4443 5390 XSync (FRAME_X_DISPLAY (f), False);
4e87f4d2 5391 UNBLOCK_INPUT;
6b7b1820 5392}
333b20bb 5393
01f1ba30 5394\f
333b20bb
GM
5395/***********************************************************************
5396 Image types
5397 ***********************************************************************/
f1c16f36 5398
333b20bb
GM
5399/* Value is the number of elements of vector VECTOR. */
5400
5401#define DIM(VECTOR) (sizeof (VECTOR) / sizeof *(VECTOR))
5402
5403/* List of supported image types. Use define_image_type to add new
5404 types. Use lookup_image_type to find a type for a given symbol. */
5405
5406static struct image_type *image_types;
5407
333b20bb
GM
5408/* The symbol `image' which is the car of the lists used to represent
5409 images in Lisp. */
5410
5411extern Lisp_Object Qimage;
5412
5413/* The symbol `xbm' which is used as the type symbol for XBM images. */
5414
5415Lisp_Object Qxbm;
5416
5417/* Keywords. */
5418
0fe92f72 5419extern Lisp_Object QCwidth, QCheight, QCforeground, QCbackground, QCfile;
77814035
KS
5420extern Lisp_Object QCdata, QCtype;
5421Lisp_Object QCascent, QCmargin, QCrelief;
d2dc8167 5422Lisp_Object QCconversion, QCcolor_symbols, QCheuristic_mask;
4a8e312c 5423Lisp_Object QCindex, QCmatrix, QCcolor_adjustment, QCmask;
333b20bb
GM
5424
5425/* Other symbols. */
5426
4a8e312c 5427Lisp_Object Qlaplace, Qemboss, Qedge_detection, Qheuristic;
333b20bb
GM
5428
5429/* Time in seconds after which images should be removed from the cache
5430 if not displayed. */
5431
fcf431dc 5432Lisp_Object Vimage_cache_eviction_delay;
333b20bb
GM
5433
5434/* Function prototypes. */
5435
5436static void define_image_type P_ ((struct image_type *type));
5437static struct image_type *lookup_image_type P_ ((Lisp_Object symbol));
5438static void image_error P_ ((char *format, Lisp_Object, Lisp_Object));
5439static void x_laplace P_ ((struct frame *, struct image *));
4a8e312c 5440static void x_emboss P_ ((struct frame *, struct image *));
45158a91
GM
5441static int x_build_heuristic_mask P_ ((struct frame *, struct image *,
5442 Lisp_Object));
333b20bb
GM
5443
5444
5445/* Define a new image type from TYPE. This adds a copy of TYPE to
5446 image_types and adds the symbol *TYPE->type to Vimage_types. */
5447
5448static void
5449define_image_type (type)
5450 struct image_type *type;
5451{
5452 /* Make a copy of TYPE to avoid a bus error in a dumped Emacs.
5453 The initialized data segment is read-only. */
5454 struct image_type *p = (struct image_type *) xmalloc (sizeof *p);
5455 bcopy (type, p, sizeof *p);
5456 p->next = image_types;
5457 image_types = p;
5458 Vimage_types = Fcons (*p->type, Vimage_types);
5459}
5460
5461
5462/* Look up image type SYMBOL, and return a pointer to its image_type
5463 structure. Value is null if SYMBOL is not a known image type. */
5464
5465static INLINE struct image_type *
5466lookup_image_type (symbol)
5467 Lisp_Object symbol;
5468{
5469 struct image_type *type;
5470
5471 for (type = image_types; type; type = type->next)
5472 if (EQ (symbol, *type->type))
5473 break;
5474
5475 return type;
5476}
5477
5478
5479/* Value is non-zero if OBJECT is a valid Lisp image specification. A
5480 valid image specification is a list whose car is the symbol
5481 `image', and whose rest is a property list. The property list must
5482 contain a value for key `:type'. That value must be the name of a
5483 supported image type. The rest of the property list depends on the
5484 image type. */
5485
5486int
5487valid_image_p (object)
5488 Lisp_Object object;
5489{
5490 int valid_p = 0;
7273d100 5491
333b20bb
GM
5492 if (CONSP (object) && EQ (XCAR (object), Qimage))
5493 {
1783ffa2
GM
5494 Lisp_Object tem;
5495
5496 for (tem = XCDR (object); CONSP (tem); tem = XCDR (tem))
5497 if (EQ (XCAR (tem), QCtype))
5498 {
5499 tem = XCDR (tem);
5500 if (CONSP (tem) && SYMBOLP (XCAR (tem)))
5501 {
5502 struct image_type *type;
5503 type = lookup_image_type (XCAR (tem));
5504 if (type)
5505 valid_p = type->valid_p (object);
5506 }
7273d100 5507
1783ffa2
GM
5508 break;
5509 }
333b20bb
GM
5510 }
5511
5512 return valid_p;
5513}
5514
5515
7ab1745f
GM
5516/* Log error message with format string FORMAT and argument ARG.
5517 Signaling an error, e.g. when an image cannot be loaded, is not a
5518 good idea because this would interrupt redisplay, and the error
5519 message display would lead to another redisplay. This function
5520 therefore simply displays a message. */
333b20bb
GM
5521
5522static void
5523image_error (format, arg1, arg2)
5524 char *format;
5525 Lisp_Object arg1, arg2;
5526{
7ab1745f 5527 add_to_log (format, arg1, arg2);
333b20bb
GM
5528}
5529
5530
5531\f
5532/***********************************************************************
5533 Image specifications
5534 ***********************************************************************/
5535
5536enum image_value_type
5537{
5538 IMAGE_DONT_CHECK_VALUE_TYPE,
5539 IMAGE_STRING_VALUE,
6f1be3b9 5540 IMAGE_STRING_OR_NIL_VALUE,
333b20bb
GM
5541 IMAGE_SYMBOL_VALUE,
5542 IMAGE_POSITIVE_INTEGER_VALUE,
3ed61e75 5543 IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR,
333b20bb 5544 IMAGE_NON_NEGATIVE_INTEGER_VALUE,
7c7ff7f5 5545 IMAGE_ASCENT_VALUE,
333b20bb
GM
5546 IMAGE_INTEGER_VALUE,
5547 IMAGE_FUNCTION_VALUE,
5548 IMAGE_NUMBER_VALUE,
5549 IMAGE_BOOL_VALUE
5550};
5551
5552/* Structure used when parsing image specifications. */
5553
5554struct image_keyword
5555{
5556 /* Name of keyword. */
5557 char *name;
5558
5559 /* The type of value allowed. */
5560 enum image_value_type type;
5561
5562 /* Non-zero means key must be present. */
5563 int mandatory_p;
5564
5565 /* Used to recognize duplicate keywords in a property list. */
5566 int count;
5567
5568 /* The value that was found. */
5569 Lisp_Object value;
5570};
5571
5572
bfd2209f
GM
5573static int parse_image_spec P_ ((Lisp_Object, struct image_keyword *,
5574 int, Lisp_Object));
333b20bb
GM
5575static Lisp_Object image_spec_value P_ ((Lisp_Object, Lisp_Object, int *));
5576
5577
5578/* Parse image spec SPEC according to KEYWORDS. A valid image spec
5579 has the format (image KEYWORD VALUE ...). One of the keyword/
5580 value pairs must be `:type TYPE'. KEYWORDS is a vector of
5581 image_keywords structures of size NKEYWORDS describing other
bfd2209f 5582 allowed keyword/value pairs. Value is non-zero if SPEC is valid. */
333b20bb
GM
5583
5584static int
bfd2209f 5585parse_image_spec (spec, keywords, nkeywords, type)
333b20bb
GM
5586 Lisp_Object spec;
5587 struct image_keyword *keywords;
5588 int nkeywords;
5589 Lisp_Object type;
333b20bb
GM
5590{
5591 int i;
5592 Lisp_Object plist;
5593
5594 if (!CONSP (spec) || !EQ (XCAR (spec), Qimage))
5595 return 0;
5596
5597 plist = XCDR (spec);
5598 while (CONSP (plist))
5599 {
5600 Lisp_Object key, value;
5601
5602 /* First element of a pair must be a symbol. */
5603 key = XCAR (plist);
5604 plist = XCDR (plist);
5605 if (!SYMBOLP (key))
5606 return 0;
5607
5608 /* There must follow a value. */
5609 if (!CONSP (plist))
5610 return 0;
5611 value = XCAR (plist);
5612 plist = XCDR (plist);
5613
5614 /* Find key in KEYWORDS. Error if not found. */
5615 for (i = 0; i < nkeywords; ++i)
d5db4077 5616 if (strcmp (keywords[i].name, SDATA (SYMBOL_NAME (key))) == 0)
333b20bb
GM
5617 break;
5618
5619 if (i == nkeywords)
bfd2209f 5620 continue;
333b20bb
GM
5621
5622 /* Record that we recognized the keyword. If a keywords
5623 was found more than once, it's an error. */
5624 keywords[i].value = value;
5625 ++keywords[i].count;
7273d100 5626
333b20bb
GM
5627 if (keywords[i].count > 1)
5628 return 0;
5629
5630 /* Check type of value against allowed type. */
5631 switch (keywords[i].type)
5632 {
5633 case IMAGE_STRING_VALUE:
5634 if (!STRINGP (value))
5635 return 0;
5636 break;
5637
6f1be3b9
GM
5638 case IMAGE_STRING_OR_NIL_VALUE:
5639 if (!STRINGP (value) && !NILP (value))
5640 return 0;
5641 break;
5642
333b20bb
GM
5643 case IMAGE_SYMBOL_VALUE:
5644 if (!SYMBOLP (value))
5645 return 0;
5646 break;
5647
5648 case IMAGE_POSITIVE_INTEGER_VALUE:
5649 if (!INTEGERP (value) || XINT (value) <= 0)
5650 return 0;
5651 break;
5652
3ed61e75
GM
5653 case IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR:
5654 if (INTEGERP (value) && XINT (value) >= 0)
5655 break;
5656 if (CONSP (value)
5657 && INTEGERP (XCAR (value)) && INTEGERP (XCDR (value))
5658 && XINT (XCAR (value)) >= 0 && XINT (XCDR (value)) >= 0)
5659 break;
5660 return 0;
5661
7c7ff7f5
GM
5662 case IMAGE_ASCENT_VALUE:
5663 if (SYMBOLP (value) && EQ (value, Qcenter))
5664 break;
5665 else if (INTEGERP (value)
5666 && XINT (value) >= 0
5667 && XINT (value) <= 100)
5668 break;
5669 return 0;
7273d100 5670
333b20bb
GM
5671 case IMAGE_NON_NEGATIVE_INTEGER_VALUE:
5672 if (!INTEGERP (value) || XINT (value) < 0)
5673 return 0;
5674 break;
5675
5676 case IMAGE_DONT_CHECK_VALUE_TYPE:
5677 break;
5678
5679 case IMAGE_FUNCTION_VALUE:
5680 value = indirect_function (value);
7273d100 5681 if (SUBRP (value)
333b20bb
GM
5682 || COMPILEDP (value)
5683 || (CONSP (value) && EQ (XCAR (value), Qlambda)))
5684 break;
5685 return 0;
5686
5687 case IMAGE_NUMBER_VALUE:
5688 if (!INTEGERP (value) && !FLOATP (value))
5689 return 0;
5690 break;
5691
5692 case IMAGE_INTEGER_VALUE:
5693 if (!INTEGERP (value))
5694 return 0;
5695 break;
5696
5697 case IMAGE_BOOL_VALUE:
5698 if (!NILP (value) && !EQ (value, Qt))
5699 return 0;
5700 break;
5701
5702 default:
5703 abort ();
5704 break;
5705 }
5706
5707 if (EQ (key, QCtype) && !EQ (type, value))
5708 return 0;
5709 }
5710
5711 /* Check that all mandatory fields are present. */
5712 for (i = 0; i < nkeywords; ++i)
5713 if (keywords[i].mandatory_p && keywords[i].count == 0)
5714 return 0;
5715
5716 return NILP (plist);
5717}
5718
5719
5720/* Return the value of KEY in image specification SPEC. Value is nil
5721 if KEY is not present in SPEC. if FOUND is not null, set *FOUND
5722 to 1 if KEY was found in SPEC, set it to 0 otherwise. */
5723
5724static Lisp_Object
5725image_spec_value (spec, key, found)
5726 Lisp_Object spec, key;
5727 int *found;
5728{
5729 Lisp_Object tail;
7273d100 5730
333b20bb
GM
5731 xassert (valid_image_p (spec));
5732
5733 for (tail = XCDR (spec);
5734 CONSP (tail) && CONSP (XCDR (tail));
5735 tail = XCDR (XCDR (tail)))
5736 {
5737 if (EQ (XCAR (tail), key))
5738 {
5739 if (found)
5740 *found = 1;
5741 return XCAR (XCDR (tail));
5742 }
5743 }
7273d100 5744
333b20bb
GM
5745 if (found)
5746 *found = 0;
5747 return Qnil;
5748}
7273d100 5749
333b20bb 5750
42677916 5751DEFUN ("image-size", Fimage_size, Simage_size, 1, 3, 0,
7ee72033 5752 doc: /* Return the size of image SPEC as pair (WIDTH . HEIGHT).
c061c855
GM
5753PIXELS non-nil means return the size in pixels, otherwise return the
5754size in canonical character units.
5755FRAME is the frame on which the image will be displayed. FRAME nil
7ee72033
MB
5756or omitted means use the selected frame. */)
5757 (spec, pixels, frame)
42677916
GM
5758 Lisp_Object spec, pixels, frame;
5759{
5760 Lisp_Object size;
5761
5762 size = Qnil;
5763 if (valid_image_p (spec))
5764 {
5765 struct frame *f = check_x_frame (frame);
83676598 5766 int id = lookup_image (f, spec);
42677916 5767 struct image *img = IMAGE_FROM_ID (f, id);
3ed61e75
GM
5768 int width = img->width + 2 * img->hmargin;
5769 int height = img->height + 2 * img->vmargin;
7273d100 5770
42677916
GM
5771 if (NILP (pixels))
5772 size = Fcons (make_float ((double) width / CANON_X_UNIT (f)),
5773 make_float ((double) height / CANON_Y_UNIT (f)));
5774 else
5775 size = Fcons (make_number (width), make_number (height));
5776 }
5777 else
5778 error ("Invalid image specification");
5779
5780 return size;
5781}
5782
333b20bb 5783
b243755a 5784DEFUN ("image-mask-p", Fimage_mask_p, Simage_mask_p, 1, 2, 0,
7ee72033 5785 doc: /* Return t if image SPEC has a mask bitmap.
c061c855 5786FRAME is the frame on which the image will be displayed. FRAME nil
7ee72033
MB
5787or omitted means use the selected frame. */)
5788 (spec, frame)
b243755a
GM
5789 Lisp_Object spec, frame;
5790{
5791 Lisp_Object mask;
5792
5793 mask = Qnil;
5794 if (valid_image_p (spec))
5795 {
5796 struct frame *f = check_x_frame (frame);
83676598 5797 int id = lookup_image (f, spec);
b243755a
GM
5798 struct image *img = IMAGE_FROM_ID (f, id);
5799 if (img->mask)
5800 mask = Qt;
5801 }
5802 else
5803 error ("Invalid image specification");
5804
5805 return mask;
5806}
5807
5808
333b20bb
GM
5809\f
5810/***********************************************************************
5811 Image type independent image structures
5812 ***********************************************************************/
5813
5814static struct image *make_image P_ ((Lisp_Object spec, unsigned hash));
5815static void free_image P_ ((struct frame *f, struct image *img));
5816
5817
5818/* Allocate and return a new image structure for image specification
5819 SPEC. SPEC has a hash value of HASH. */
5820
5821static struct image *
5822make_image (spec, hash)
5823 Lisp_Object spec;
5824 unsigned hash;
5825{
5826 struct image *img = (struct image *) xmalloc (sizeof *img);
7273d100 5827
333b20bb
GM
5828 xassert (valid_image_p (spec));
5829 bzero (img, sizeof *img);
5830 img->type = lookup_image_type (image_spec_value (spec, QCtype, NULL));
5831 xassert (img->type != NULL);
5832 img->spec = spec;
5833 img->data.lisp_val = Qnil;
5834 img->ascent = DEFAULT_IMAGE_ASCENT;
5835 img->hash = hash;
5836 return img;
5837}
5838
5839
5840/* Free image IMG which was used on frame F, including its resources. */
5841
5842static void
5843free_image (f, img)
5844 struct frame *f;
5845 struct image *img;
5846{
5847 if (img)
5848 {
5849 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
5850
5851 /* Remove IMG from the hash table of its cache. */
5852 if (img->prev)
5853 img->prev->next = img->next;
5854 else
5855 c->buckets[img->hash % IMAGE_CACHE_BUCKETS_SIZE] = img->next;
5856
5857 if (img->next)
5858 img->next->prev = img->prev;
5859
5860 c->images[img->id] = NULL;
5861
5862 /* Free resources, then free IMG. */
5863 img->type->free (f, img);
5864 xfree (img);
5865 }
5866}
5867
5868
5869/* Prepare image IMG for display on frame F. Must be called before
5870 drawing an image. */
5871
5872void
5873prepare_image_for_display (f, img)
5874 struct frame *f;
5875 struct image *img;
5876{
5877 EMACS_TIME t;
5878
5879 /* We're about to display IMG, so set its timestamp to `now'. */
5880 EMACS_GET_TIME (t);
5881 img->timestamp = EMACS_SECS (t);
5882
5883 /* If IMG doesn't have a pixmap yet, load it now, using the image
5884 type dependent loader function. */
dd00328a 5885 if (img->pixmap == None && !img->load_failed_p)
209061be 5886 img->load_failed_p = img->type->load (f, img) == 0;
333b20bb 5887}
7273d100 5888
333b20bb 5889
7c7ff7f5
GM
5890/* Value is the number of pixels for the ascent of image IMG when
5891 drawn in face FACE. */
5892
5893int
5894image_ascent (img, face)
5895 struct image *img;
5896 struct face *face;
5897{
3ed61e75 5898 int height = img->height + img->vmargin;
7c7ff7f5
GM
5899 int ascent;
5900
5901 if (img->ascent == CENTERED_IMAGE_ASCENT)
5902 {
5903 if (face->font)
3694cb3f
MB
5904 /* This expression is arranged so that if the image can't be
5905 exactly centered, it will be moved slightly up. This is
5906 because a typical font is `top-heavy' (due to the presence
5907 uppercase letters), so the image placement should err towards
5908 being top-heavy too. It also just generally looks better. */
5909 ascent = (height + face->font->ascent - face->font->descent + 1) / 2;
7c7ff7f5
GM
5910 else
5911 ascent = height / 2;
5912 }
5913 else
5914 ascent = height * img->ascent / 100.0;
5915
5916 return ascent;
5917}
5918
f20a3b7a
MB
5919\f
5920/* Image background colors. */
5921
5922static unsigned long
5923four_corners_best (ximg, width, height)
5924 XImage *ximg;
5925 unsigned long width, height;
5926{
b350c2e5
GM
5927 unsigned long corners[4], best;
5928 int i, best_count;
f20a3b7a 5929
b350c2e5
GM
5930 /* Get the colors at the corners of ximg. */
5931 corners[0] = XGetPixel (ximg, 0, 0);
5932 corners[1] = XGetPixel (ximg, width - 1, 0);
5933 corners[2] = XGetPixel (ximg, width - 1, height - 1);
5934 corners[3] = XGetPixel (ximg, 0, height - 1);
f20a3b7a 5935
b350c2e5
GM
5936 /* Choose the most frequently found color as background. */
5937 for (i = best_count = 0; i < 4; ++i)
5938 {
5939 int j, n;
7273d100 5940
b350c2e5
GM
5941 for (j = n = 0; j < 4; ++j)
5942 if (corners[i] == corners[j])
5943 ++n;
f20a3b7a 5944
b350c2e5
GM
5945 if (n > best_count)
5946 best = corners[i], best_count = n;
5947 }
f20a3b7a 5948
b350c2e5 5949 return best;
f20a3b7a
MB
5950}
5951
5952/* Return the `background' field of IMG. If IMG doesn't have one yet,
5953 it is guessed heuristically. If non-zero, XIMG is an existing XImage
5954 object to use for the heuristic. */
5955
5956unsigned long
5957image_background (img, f, ximg)
5958 struct image *img;
5959 struct frame *f;
5960 XImage *ximg;
5961{
5962 if (! img->background_valid)
5963 /* IMG doesn't have a background yet, try to guess a reasonable value. */
5964 {
5965 int free_ximg = !ximg;
5966
5967 if (! ximg)
5968 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
5969 0, 0, img->width, img->height, ~0, ZPixmap);
5970
5971 img->background = four_corners_best (ximg, img->width, img->height);
5972
5973 if (free_ximg)
5974 XDestroyImage (ximg);
5975
5976 img->background_valid = 1;
5977 }
5978
5979 return img->background;
5980}
5981
5982/* Return the `background_transparent' field of IMG. If IMG doesn't
5983 have one yet, it is guessed heuristically. If non-zero, MASK is an
5984 existing XImage object to use for the heuristic. */
5985
5986int
5987image_background_transparent (img, f, mask)
5988 struct image *img;
5989 struct frame *f;
5990 XImage *mask;
5991{
5992 if (! img->background_transparent_valid)
5993 /* IMG doesn't have a background yet, try to guess a reasonable value. */
5994 {
5995 if (img->mask)
5996 {
5997 int free_mask = !mask;
5998
5999 if (! mask)
6000 mask = XGetImage (FRAME_X_DISPLAY (f), img->mask,
6001 0, 0, img->width, img->height, ~0, ZPixmap);
6002
6003 img->background_transparent
6004 = !four_corners_best (mask, img->width, img->height);
6005
6006 if (free_mask)
6007 XDestroyImage (mask);
6008 }
6009 else
6010 img->background_transparent = 0;
6011
6012 img->background_transparent_valid = 1;
6013 }
6014
6015 return img->background_transparent;
6016}
7c7ff7f5 6017
333b20bb
GM
6018\f
6019/***********************************************************************
6020 Helper functions for X image types
6021 ***********************************************************************/
6022
dd00328a
GM
6023static void x_clear_image_1 P_ ((struct frame *, struct image *, int,
6024 int, int));
333b20bb
GM
6025static void x_clear_image P_ ((struct frame *f, struct image *img));
6026static unsigned long x_alloc_image_color P_ ((struct frame *f,
6027 struct image *img,
6028 Lisp_Object color_name,
6029 unsigned long dflt));
6030
dd00328a
GM
6031
6032/* Clear X resources of image IMG on frame F. PIXMAP_P non-zero means
6033 free the pixmap if any. MASK_P non-zero means clear the mask
6034 pixmap if any. COLORS_P non-zero means free colors allocated for
6035 the image, if any. */
333b20bb
GM
6036
6037static void
dd00328a 6038x_clear_image_1 (f, img, pixmap_p, mask_p, colors_p)
333b20bb
GM
6039 struct frame *f;
6040 struct image *img;
dd00328a 6041 int pixmap_p, mask_p, colors_p;
333b20bb 6042{
dd00328a 6043 if (pixmap_p && img->pixmap)
333b20bb 6044 {
333b20bb 6045 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
dd00328a 6046 img->pixmap = None;
f20a3b7a 6047 img->background_valid = 0;
f4779de9
GM
6048 }
6049
dd00328a 6050 if (mask_p && img->mask)
f4779de9
GM
6051 {
6052 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
dd00328a 6053 img->mask = None;
f20a3b7a 6054 img->background_transparent_valid = 0;
333b20bb 6055 }
7273d100 6056
dd00328a 6057 if (colors_p && img->ncolors)
333b20bb 6058 {
462d5d40 6059 x_free_colors (f, img->colors, img->ncolors);
333b20bb
GM
6060 xfree (img->colors);
6061 img->colors = NULL;
6062 img->ncolors = 0;
6063 }
dd00328a
GM
6064}
6065
6066/* Free X resources of image IMG which is used on frame F. */
6067
6068static void
6069x_clear_image (f, img)
6070 struct frame *f;
6071 struct image *img;
6072{
6073 BLOCK_INPUT;
6074 x_clear_image_1 (f, img, 1, 1, 1);
f4779de9 6075 UNBLOCK_INPUT;
333b20bb
GM
6076}
6077
6078
6079/* Allocate color COLOR_NAME for image IMG on frame F. If color
6080 cannot be allocated, use DFLT. Add a newly allocated color to
6081 IMG->colors, so that it can be freed again. Value is the pixel
6082 color. */
6083
6084static unsigned long
6085x_alloc_image_color (f, img, color_name, dflt)
6086 struct frame *f;
6087 struct image *img;
6088 Lisp_Object color_name;
6089 unsigned long dflt;
6090{
6091 XColor color;
6092 unsigned long result;
6093
6094 xassert (STRINGP (color_name));
6095
d5db4077 6096 if (x_defined_color (f, SDATA (color_name), &color, 1))
333b20bb
GM
6097 {
6098 /* This isn't called frequently so we get away with simply
6099 reallocating the color vector to the needed size, here. */
6100 ++img->ncolors;
6101 img->colors =
6102 (unsigned long *) xrealloc (img->colors,
6103 img->ncolors * sizeof *img->colors);
6104 img->colors[img->ncolors - 1] = color.pixel;
6105 result = color.pixel;
6106 }
6107 else
6108 result = dflt;
6109
6110 return result;
6111}
6112
6113
6114\f
6115/***********************************************************************
6116 Image Cache
6117 ***********************************************************************/
6118
6119static void cache_image P_ ((struct frame *f, struct image *img));
ad18ffb1 6120static void postprocess_image P_ ((struct frame *, struct image *));
333b20bb
GM
6121
6122
6123/* Return a new, initialized image cache that is allocated from the
6124 heap. Call free_image_cache to free an image cache. */
6125
6126struct image_cache *
6127make_image_cache ()
6128{
6129 struct image_cache *c = (struct image_cache *) xmalloc (sizeof *c);
6130 int size;
7273d100 6131
333b20bb
GM
6132 bzero (c, sizeof *c);
6133 c->size = 50;
6134 c->images = (struct image **) xmalloc (c->size * sizeof *c->images);
6135 size = IMAGE_CACHE_BUCKETS_SIZE * sizeof *c->buckets;
6136 c->buckets = (struct image **) xmalloc (size);
6137 bzero (c->buckets, size);
6138 return c;
6139}
6140
6141
6142/* Free image cache of frame F. Be aware that X frames share images
6143 caches. */
6144
6145void
6146free_image_cache (f)
6147 struct frame *f;
6148{
6149 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6150 if (c)
6151 {
6152 int i;
6153
6154 /* Cache should not be referenced by any frame when freed. */
6155 xassert (c->refcount == 0);
7273d100 6156
333b20bb
GM
6157 for (i = 0; i < c->used; ++i)
6158 free_image (f, c->images[i]);
6159 xfree (c->images);
333b20bb 6160 xfree (c->buckets);
e3130015 6161 xfree (c);
333b20bb
GM
6162 FRAME_X_IMAGE_CACHE (f) = NULL;
6163 }
6164}
6165
6166
6167/* Clear image cache of frame F. FORCE_P non-zero means free all
6168 images. FORCE_P zero means clear only images that haven't been
6169 displayed for some time. Should be called from time to time to
6170 reduce the number of loaded images. If image-eviction-seconds is
6171 non-nil, this frees images in the cache which weren't displayed for
6172 at least that many seconds. */
6173
6174void
6175clear_image_cache (f, force_p)
6176 struct frame *f;
6177 int force_p;
6178{
6179 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6180
83676598 6181 if (c && INTEGERP (Vimage_cache_eviction_delay))
333b20bb
GM
6182 {
6183 EMACS_TIME t;
6184 unsigned long old;
f4779de9 6185 int i, nfreed;
333b20bb
GM
6186
6187 EMACS_GET_TIME (t);
fcf431dc 6188 old = EMACS_SECS (t) - XFASTINT (Vimage_cache_eviction_delay);
f4779de9
GM
6189
6190 /* Block input so that we won't be interrupted by a SIGIO
6191 while being in an inconsistent state. */
6192 BLOCK_INPUT;
7273d100 6193
f4779de9 6194 for (i = nfreed = 0; i < c->used; ++i)
333b20bb
GM
6195 {
6196 struct image *img = c->images[i];
6197 if (img != NULL
f4779de9 6198 && (force_p || img->timestamp < old))
333b20bb
GM
6199 {
6200 free_image (f, img);
f4779de9 6201 ++nfreed;
333b20bb
GM
6202 }
6203 }
6204
6205 /* We may be clearing the image cache because, for example,
6206 Emacs was iconified for a longer period of time. In that
6207 case, current matrices may still contain references to
6208 images freed above. So, clear these matrices. */
f4779de9 6209 if (nfreed)
333b20bb 6210 {
f4779de9 6211 Lisp_Object tail, frame;
7273d100 6212
f4779de9
GM
6213 FOR_EACH_FRAME (tail, frame)
6214 {
6215 struct frame *f = XFRAME (frame);
6216 if (FRAME_X_P (f)
6217 && FRAME_X_IMAGE_CACHE (f) == c)
83676598 6218 clear_current_matrices (f);
f4779de9
GM
6219 }
6220
333b20bb
GM
6221 ++windows_or_buffers_changed;
6222 }
f4779de9
GM
6223
6224 UNBLOCK_INPUT;
333b20bb
GM
6225 }
6226}
6227
6228
6229DEFUN ("clear-image-cache", Fclear_image_cache, Sclear_image_cache,
6230 0, 1, 0,
7ee72033 6231 doc: /* Clear the image cache of FRAME.
c061c855 6232FRAME nil or omitted means use the selected frame.
7ee72033
MB
6233FRAME t means clear the image caches of all frames. */)
6234 (frame)
333b20bb
GM
6235 Lisp_Object frame;
6236{
6237 if (EQ (frame, Qt))
6238 {
6239 Lisp_Object tail;
7273d100 6240
333b20bb
GM
6241 FOR_EACH_FRAME (tail, frame)
6242 if (FRAME_X_P (XFRAME (frame)))
6243 clear_image_cache (XFRAME (frame), 1);
6244 }
6245 else
6246 clear_image_cache (check_x_frame (frame), 1);
6247
6248 return Qnil;
6249}
6250
6251
ad18ffb1
GM
6252/* Compute masks and transform image IMG on frame F, as specified
6253 by the image's specification, */
6254
6255static void
6256postprocess_image (f, img)
6257 struct frame *f;
6258 struct image *img;
6259{
6260 /* Manipulation of the image's mask. */
6261 if (img->pixmap)
6262 {
6263 Lisp_Object conversion, spec;
6264 Lisp_Object mask;
6265
6266 spec = img->spec;
7273d100 6267
ad18ffb1
GM
6268 /* `:heuristic-mask t'
6269 `:mask heuristic'
6270 means build a mask heuristically.
6271 `:heuristic-mask (R G B)'
6272 `:mask (heuristic (R G B))'
6273 means build a mask from color (R G B) in the
6274 image.
6275 `:mask nil'
6276 means remove a mask, if any. */
7273d100 6277
ad18ffb1
GM
6278 mask = image_spec_value (spec, QCheuristic_mask, NULL);
6279 if (!NILP (mask))
6280 x_build_heuristic_mask (f, img, mask);
6281 else
6282 {
6283 int found_p;
7273d100 6284
ad18ffb1 6285 mask = image_spec_value (spec, QCmask, &found_p);
7273d100 6286
ad18ffb1
GM
6287 if (EQ (mask, Qheuristic))
6288 x_build_heuristic_mask (f, img, Qt);
6289 else if (CONSP (mask)
6290 && EQ (XCAR (mask), Qheuristic))
6291 {
6292 if (CONSP (XCDR (mask)))
6293 x_build_heuristic_mask (f, img, XCAR (XCDR (mask)));
6294 else
6295 x_build_heuristic_mask (f, img, XCDR (mask));
6296 }
6297 else if (NILP (mask) && found_p && img->mask)
6298 {
6299 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
6300 img->mask = None;
6301 }
6302 }
7273d100
FP
6303
6304
ad18ffb1
GM
6305 /* Should we apply an image transformation algorithm? */
6306 conversion = image_spec_value (spec, QCconversion, NULL);
6307 if (EQ (conversion, Qdisabled))
6308 x_disable_image (f, img);
6309 else if (EQ (conversion, Qlaplace))
6310 x_laplace (f, img);
6311 else if (EQ (conversion, Qemboss))
6312 x_emboss (f, img);
6313 else if (CONSP (conversion)
6314 && EQ (XCAR (conversion), Qedge_detection))
6315 {
6316 Lisp_Object tem;
6317 tem = XCDR (conversion);
6318 if (CONSP (tem))
6319 x_edge_detection (f, img,
6320 Fplist_get (tem, QCmatrix),
6321 Fplist_get (tem, QCcolor_adjustment));
6322 }
6323 }
6324}
6325
6326
333b20bb 6327/* Return the id of image with Lisp specification SPEC on frame F.
83676598 6328 SPEC must be a valid Lisp image specification (see valid_image_p). */
333b20bb
GM
6329
6330int
83676598 6331lookup_image (f, spec)
333b20bb
GM
6332 struct frame *f;
6333 Lisp_Object spec;
6334{
6335 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6336 struct image *img;
6337 int i;
6338 unsigned hash;
6339 struct gcpro gcpro1;
4f7ca1f1 6340 EMACS_TIME now;
333b20bb
GM
6341
6342 /* F must be a window-system frame, and SPEC must be a valid image
6343 specification. */
6344 xassert (FRAME_WINDOW_P (f));
6345 xassert (valid_image_p (spec));
7273d100 6346
333b20bb
GM
6347 GCPRO1 (spec);
6348
6349 /* Look up SPEC in the hash table of the image cache. */
6350 hash = sxhash (spec, 0);
6351 i = hash % IMAGE_CACHE_BUCKETS_SIZE;
6352
6353 for (img = c->buckets[i]; img; img = img->next)
6354 if (img->hash == hash && !NILP (Fequal (img->spec, spec)))
6355 break;
6356
6357 /* If not found, create a new image and cache it. */
6358 if (img == NULL)
6359 {
ad18ffb1 6360 extern Lisp_Object Qpostscript;
7273d100 6361
28c7826c 6362 BLOCK_INPUT;
333b20bb
GM
6363 img = make_image (spec, hash);
6364 cache_image (f, img);
83676598 6365 img->load_failed_p = img->type->load (f, img) == 0;
333b20bb
GM
6366
6367 /* If we can't load the image, and we don't have a width and
6368 height, use some arbitrary width and height so that we can
6369 draw a rectangle for it. */
83676598 6370 if (img->load_failed_p)
333b20bb
GM
6371 {
6372 Lisp_Object value;
6373
6374 value = image_spec_value (spec, QCwidth, NULL);
6375 img->width = (INTEGERP (value)
6376 ? XFASTINT (value) : DEFAULT_IMAGE_WIDTH);
6377 value = image_spec_value (spec, QCheight, NULL);
6378 img->height = (INTEGERP (value)
6379 ? XFASTINT (value) : DEFAULT_IMAGE_HEIGHT);
6380 }
6381 else
6382 {
6383 /* Handle image type independent image attributes
f20a3b7a
MB
6384 `:ascent ASCENT', `:margin MARGIN', `:relief RELIEF',
6385 `:background COLOR'. */
6386 Lisp_Object ascent, margin, relief, bg;
333b20bb
GM
6387
6388 ascent = image_spec_value (spec, QCascent, NULL);
6389 if (INTEGERP (ascent))
6390 img->ascent = XFASTINT (ascent);
7c7ff7f5
GM
6391 else if (EQ (ascent, Qcenter))
6392 img->ascent = CENTERED_IMAGE_ASCENT;
7273d100 6393
333b20bb
GM
6394 margin = image_spec_value (spec, QCmargin, NULL);
6395 if (INTEGERP (margin) && XINT (margin) >= 0)
3ed61e75
GM
6396 img->vmargin = img->hmargin = XFASTINT (margin);
6397 else if (CONSP (margin) && INTEGERP (XCAR (margin))
6398 && INTEGERP (XCDR (margin)))
6399 {
6400 if (XINT (XCAR (margin)) > 0)
6401 img->hmargin = XFASTINT (XCAR (margin));
6402 if (XINT (XCDR (margin)) > 0)
6403 img->vmargin = XFASTINT (XCDR (margin));
6404 }
7273d100 6405
333b20bb
GM
6406 relief = image_spec_value (spec, QCrelief, NULL);
6407 if (INTEGERP (relief))
6408 {
6409 img->relief = XINT (relief);
3ed61e75
GM
6410 img->hmargin += abs (img->relief);
6411 img->vmargin += abs (img->relief);
333b20bb
GM
6412 }
6413
f20a3b7a
MB
6414 if (! img->background_valid)
6415 {
6416 bg = image_spec_value (img->spec, QCbackground, NULL);
6417 if (!NILP (bg))
6418 {
6419 img->background
6420 = x_alloc_image_color (f, img, bg,
6421 FRAME_BACKGROUND_PIXEL (f));
6422 img->background_valid = 1;
6423 }
6424 }
6425
ad18ffb1
GM
6426 /* Do image transformations and compute masks, unless we
6427 don't have the image yet. */
6428 if (!EQ (*img->type->type, Qpostscript))
6429 postprocess_image (f, img);
333b20bb 6430 }
dd00328a 6431
28c7826c
GM
6432 UNBLOCK_INPUT;
6433 xassert (!interrupt_input_blocked);
333b20bb
GM
6434 }
6435
4f7ca1f1
GM
6436 /* We're using IMG, so set its timestamp to `now'. */
6437 EMACS_GET_TIME (now);
6438 img->timestamp = EMACS_SECS (now);
7273d100 6439
333b20bb 6440 UNGCPRO;
7273d100 6441
333b20bb
GM
6442 /* Value is the image id. */
6443 return img->id;
6444}
6445
6446
6447/* Cache image IMG in the image cache of frame F. */
6448
6449static void
6450cache_image (f, img)
6451 struct frame *f;
6452 struct image *img;
6453{
6454 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6455 int i;
6456
6457 /* Find a free slot in c->images. */
6458 for (i = 0; i < c->used; ++i)
6459 if (c->images[i] == NULL)
6460 break;
6461
6462 /* If no free slot found, maybe enlarge c->images. */
6463 if (i == c->used && c->used == c->size)
6464 {
6465 c->size *= 2;
6466 c->images = (struct image **) xrealloc (c->images,
6467 c->size * sizeof *c->images);
6468 }
6469
6470 /* Add IMG to c->images, and assign IMG an id. */
6471 c->images[i] = img;
6472 img->id = i;
6473 if (i == c->used)
6474 ++c->used;
6475
6476 /* Add IMG to the cache's hash table. */
6477 i = img->hash % IMAGE_CACHE_BUCKETS_SIZE;
6478 img->next = c->buckets[i];
6479 if (img->next)
6480 img->next->prev = img;
6481 img->prev = NULL;
6482 c->buckets[i] = img;
6483}
6484
6485
6486/* Call FN on every image in the image cache of frame F. Used to mark
6487 Lisp Objects in the image cache. */
6488
6489void
6490forall_images_in_image_cache (f, fn)
6491 struct frame *f;
6492 void (*fn) P_ ((struct image *img));
6493{
6494 if (FRAME_LIVE_P (f) && FRAME_X_P (f))
6495 {
6496 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
6497 if (c)
6498 {
6499 int i;
6500 for (i = 0; i < c->used; ++i)
6501 if (c->images[i])
6502 fn (c->images[i]);
6503 }
6504 }
6505}
6506
6507
6508\f
6509/***********************************************************************
6510 X support code
6511 ***********************************************************************/
6512
45158a91
GM
6513static int x_create_x_image_and_pixmap P_ ((struct frame *, int, int, int,
6514 XImage **, Pixmap *));
333b20bb
GM
6515static void x_destroy_x_image P_ ((XImage *));
6516static void x_put_x_image P_ ((struct frame *, XImage *, Pixmap, int, int));
6517
6518
6519/* Create an XImage and a pixmap of size WIDTH x HEIGHT for use on
6520 frame F. Set *XIMG and *PIXMAP to the XImage and Pixmap created.
6521 Set (*XIMG)->data to a raster of WIDTH x HEIGHT pixels allocated
6522 via xmalloc. Print error messages via image_error if an error
45158a91 6523 occurs. Value is non-zero if successful. */
333b20bb
GM
6524
6525static int
45158a91 6526x_create_x_image_and_pixmap (f, width, height, depth, ximg, pixmap)
333b20bb 6527 struct frame *f;
333b20bb
GM
6528 int width, height, depth;
6529 XImage **ximg;
6530 Pixmap *pixmap;
6531{
6532 Display *display = FRAME_X_DISPLAY (f);
6533 Screen *screen = FRAME_X_SCREEN (f);
6534 Window window = FRAME_X_WINDOW (f);
6535
6536 xassert (interrupt_input_blocked);
6537
6538 if (depth <= 0)
6539 depth = DefaultDepthOfScreen (screen);
6540 *ximg = XCreateImage (display, DefaultVisualOfScreen (screen),
6541 depth, ZPixmap, 0, NULL, width, height,
6542 depth > 16 ? 32 : depth > 8 ? 16 : 8, 0);
6543 if (*ximg == NULL)
6544 {
45158a91 6545 image_error ("Unable to allocate X image", Qnil, Qnil);
333b20bb
GM
6546 return 0;
6547 }
6548
6549 /* Allocate image raster. */
6550 (*ximg)->data = (char *) xmalloc ((*ximg)->bytes_per_line * height);
6551
6552 /* Allocate a pixmap of the same size. */
6553 *pixmap = XCreatePixmap (display, window, width, height, depth);
dd00328a 6554 if (*pixmap == None)
333b20bb
GM
6555 {
6556 x_destroy_x_image (*ximg);
6557 *ximg = NULL;
45158a91 6558 image_error ("Unable to create X pixmap", Qnil, Qnil);
333b20bb
GM
6559 return 0;
6560 }
6561
6562 return 1;
6563}
6564
6565
6566/* Destroy XImage XIMG. Free XIMG->data. */
6567
6568static void
6569x_destroy_x_image (ximg)
6570 XImage *ximg;
6571{
6572 xassert (interrupt_input_blocked);
6573 if (ximg)
6574 {
6575 xfree (ximg->data);
6576 ximg->data = NULL;
6577 XDestroyImage (ximg);
6578 }
6579}
6580
6581
6582/* Put XImage XIMG into pixmap PIXMAP on frame F. WIDTH and HEIGHT
6583 are width and height of both the image and pixmap. */
6584
ea6b19ca 6585static void
333b20bb
GM
6586x_put_x_image (f, ximg, pixmap, width, height)
6587 struct frame *f;
6588 XImage *ximg;
6589 Pixmap pixmap;
caeea55a 6590 int width, height;
333b20bb
GM
6591{
6592 GC gc;
7273d100 6593
333b20bb
GM
6594 xassert (interrupt_input_blocked);
6595 gc = XCreateGC (FRAME_X_DISPLAY (f), pixmap, 0, NULL);
6596 XPutImage (FRAME_X_DISPLAY (f), pixmap, gc, ximg, 0, 0, 0, 0, width, height);
6597 XFreeGC (FRAME_X_DISPLAY (f), gc);
6598}
6599
6600
6601\f
6602/***********************************************************************
5be6c3b0 6603 File Handling
333b20bb
GM
6604 ***********************************************************************/
6605
6606static Lisp_Object x_find_image_file P_ ((Lisp_Object));
5be6c3b0
GM
6607static char *slurp_file P_ ((char *, int *));
6608
333b20bb
GM
6609
6610/* Find image file FILE. Look in data-directory, then
6611 x-bitmap-file-path. Value is the full name of the file found, or
6612 nil if not found. */
6613
6614static Lisp_Object
6615x_find_image_file (file)
6616 Lisp_Object file;
6617{
6618 Lisp_Object file_found, search_path;
6619 struct gcpro gcpro1, gcpro2;
6620 int fd;
6621
6622 file_found = Qnil;
6623 search_path = Fcons (Vdata_directory, Vx_bitmap_file_path);
6624 GCPRO2 (file_found, search_path);
6625
6626 /* Try to find FILE in data-directory, then x-bitmap-file-path. */
de2413e9 6627 fd = openp (search_path, file, Qnil, &file_found, Qnil);
7273d100 6628
939d6465 6629 if (fd == -1)
333b20bb
GM
6630 file_found = Qnil;
6631 else
6632 close (fd);
6633
6634 UNGCPRO;
6635 return file_found;
6636}
6637
6638
5be6c3b0
GM
6639/* Read FILE into memory. Value is a pointer to a buffer allocated
6640 with xmalloc holding FILE's contents. Value is null if an error
b243755a 6641 occurred. *SIZE is set to the size of the file. */
5be6c3b0
GM
6642
6643static char *
6644slurp_file (file, size)
6645 char *file;
6646 int *size;
6647{
6648 FILE *fp = NULL;
6649 char *buf = NULL;
6650 struct stat st;
6651
6652 if (stat (file, &st) == 0
6653 && (fp = fopen (file, "r")) != NULL
6654 && (buf = (char *) xmalloc (st.st_size),
6655 fread (buf, 1, st.st_size, fp) == st.st_size))
6656 {
6657 *size = st.st_size;
6658 fclose (fp);
6659 }
6660 else
6661 {
6662 if (fp)
6663 fclose (fp);
6664 if (buf)
6665 {
6666 xfree (buf);
6667 buf = NULL;
6668 }
6669 }
7273d100 6670
5be6c3b0
GM
6671 return buf;
6672}
6673
6674
333b20bb
GM
6675\f
6676/***********************************************************************
6677 XBM images
6678 ***********************************************************************/
6679
5be6c3b0 6680static int xbm_scan P_ ((char **, char *, char *, int *));
333b20bb 6681static int xbm_load P_ ((struct frame *f, struct image *img));
5be6c3b0
GM
6682static int xbm_load_image P_ ((struct frame *f, struct image *img,
6683 char *, char *));
333b20bb 6684static int xbm_image_p P_ ((Lisp_Object object));
5be6c3b0
GM
6685static int xbm_read_bitmap_data P_ ((char *, char *, int *, int *,
6686 unsigned char **));
6687static int xbm_file_p P_ ((Lisp_Object));
333b20bb
GM
6688
6689
6690/* Indices of image specification fields in xbm_format, below. */
6691
6692enum xbm_keyword_index
6693{
6694 XBM_TYPE,
6695 XBM_FILE,
6696 XBM_WIDTH,
6697 XBM_HEIGHT,
6698 XBM_DATA,
6699 XBM_FOREGROUND,
6700 XBM_BACKGROUND,
6701 XBM_ASCENT,
6702 XBM_MARGIN,
6703 XBM_RELIEF,
6704 XBM_ALGORITHM,
6705 XBM_HEURISTIC_MASK,
4a8e312c 6706 XBM_MASK,
333b20bb
GM
6707 XBM_LAST
6708};
6709
6710/* Vector of image_keyword structures describing the format
6711 of valid XBM image specifications. */
6712
6713static struct image_keyword xbm_format[XBM_LAST] =
6714{
6715 {":type", IMAGE_SYMBOL_VALUE, 1},
6716 {":file", IMAGE_STRING_VALUE, 0},
6717 {":width", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6718 {":height", IMAGE_POSITIVE_INTEGER_VALUE, 0},
6719 {":data", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6f1be3b9
GM
6720 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
6721 {":background", IMAGE_STRING_OR_NIL_VALUE, 0},
7c7ff7f5 6722 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 6723 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 6724 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 6725 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c
GM
6726 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6727 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0}
333b20bb
GM
6728};
6729
6730/* Structure describing the image type XBM. */
6731
6732static struct image_type xbm_type =
6733{
6734 &Qxbm,
6735 xbm_image_p,
6736 xbm_load,
6737 x_clear_image,
6738 NULL
6739};
6740
6741/* Tokens returned from xbm_scan. */
6742
6743enum xbm_token
6744{
6745 XBM_TK_IDENT = 256,
6746 XBM_TK_NUMBER
6747};
6748
7273d100 6749
333b20bb
GM
6750/* Return non-zero if OBJECT is a valid XBM-type image specification.
6751 A valid specification is a list starting with the symbol `image'
6752 The rest of the list is a property list which must contain an
6753 entry `:type xbm..
6754
6755 If the specification specifies a file to load, it must contain
6756 an entry `:file FILENAME' where FILENAME is a string.
6757
6758 If the specification is for a bitmap loaded from memory it must
6759 contain `:width WIDTH', `:height HEIGHT', and `:data DATA', where
6760 WIDTH and HEIGHT are integers > 0. DATA may be:
6761
6762 1. a string large enough to hold the bitmap data, i.e. it must
6763 have a size >= (WIDTH + 7) / 8 * HEIGHT
6764
6765 2. a bool-vector of size >= WIDTH * HEIGHT
6766
6767 3. a vector of strings or bool-vectors, one for each line of the
6768 bitmap.
6769
5be6c3b0
GM
6770 4. A string containing an in-memory XBM file. WIDTH and HEIGHT
6771 may not be specified in this case because they are defined in the
6772 XBM file.
6773
333b20bb
GM
6774 Both the file and data forms may contain the additional entries
6775 `:background COLOR' and `:foreground COLOR'. If not present,
6776 foreground and background of the frame on which the image is
e3130015 6777 displayed is used. */
333b20bb
GM
6778
6779static int
6780xbm_image_p (object)
6781 Lisp_Object object;
6782{
6783 struct image_keyword kw[XBM_LAST];
7273d100 6784
333b20bb 6785 bcopy (xbm_format, kw, sizeof kw);
bfd2209f 6786 if (!parse_image_spec (object, kw, XBM_LAST, Qxbm))
333b20bb
GM
6787 return 0;
6788
6789 xassert (EQ (kw[XBM_TYPE].value, Qxbm));
6790
6791 if (kw[XBM_FILE].count)
6792 {
6793 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_DATA].count)
6794 return 0;
6795 }
5be6c3b0
GM
6796 else if (kw[XBM_DATA].count && xbm_file_p (kw[XBM_DATA].value))
6797 {
6798 /* In-memory XBM file. */
6799 if (kw[XBM_WIDTH].count || kw[XBM_HEIGHT].count || kw[XBM_FILE].count)
6800 return 0;
6801 }
333b20bb
GM
6802 else
6803 {
6804 Lisp_Object data;
6805 int width, height;
6806
6807 /* Entries for `:width', `:height' and `:data' must be present. */
6808 if (!kw[XBM_WIDTH].count
6809 || !kw[XBM_HEIGHT].count
6810 || !kw[XBM_DATA].count)
6811 return 0;
6812
6813 data = kw[XBM_DATA].value;
6814 width = XFASTINT (kw[XBM_WIDTH].value);
6815 height = XFASTINT (kw[XBM_HEIGHT].value);
7273d100 6816
333b20bb
GM
6817 /* Check type of data, and width and height against contents of
6818 data. */
6819 if (VECTORP (data))
6820 {
6821 int i;
7273d100 6822
333b20bb
GM
6823 /* Number of elements of the vector must be >= height. */
6824 if (XVECTOR (data)->size < height)
6825 return 0;
6826
6827 /* Each string or bool-vector in data must be large enough
6828 for one line of the image. */
6829 for (i = 0; i < height; ++i)
6830 {
6831 Lisp_Object elt = XVECTOR (data)->contents[i];
6832
6833 if (STRINGP (elt))
6834 {
d5db4077 6835 if (SCHARS (elt)
333b20bb
GM
6836 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR)
6837 return 0;
6838 }
6839 else if (BOOL_VECTOR_P (elt))
6840 {
6841 if (XBOOL_VECTOR (elt)->size < width)
6842 return 0;
6843 }
6844 else
6845 return 0;
6846 }
6847 }
6848 else if (STRINGP (data))
6849 {
d5db4077 6850 if (SCHARS (data)
333b20bb
GM
6851 < (width + BITS_PER_CHAR - 1) / BITS_PER_CHAR * height)
6852 return 0;
6853 }
6854 else if (BOOL_VECTOR_P (data))
6855 {
6856 if (XBOOL_VECTOR (data)->size < width * height)
6857 return 0;
6858 }
6859 else
6860 return 0;
6861 }
6862
333b20bb
GM
6863 return 1;
6864}
6865
6866
6867/* Scan a bitmap file. FP is the stream to read from. Value is
6868 either an enumerator from enum xbm_token, or a character for a
6869 single-character token, or 0 at end of file. If scanning an
6870 identifier, store the lexeme of the identifier in SVAL. If
6871 scanning a number, store its value in *IVAL. */
6872
6873static int
5be6c3b0
GM
6874xbm_scan (s, end, sval, ival)
6875 char **s, *end;
333b20bb
GM
6876 char *sval;
6877 int *ival;
6878{
6879 int c;
0a695da7
GM
6880
6881 loop:
7273d100 6882
333b20bb 6883 /* Skip white space. */
5be6c3b0 6884 while (*s < end && (c = *(*s)++, isspace (c)))
333b20bb
GM
6885 ;
6886
5be6c3b0 6887 if (*s >= end)
333b20bb
GM
6888 c = 0;
6889 else if (isdigit (c))
6890 {
6891 int value = 0, digit;
7273d100 6892
5be6c3b0 6893 if (c == '0' && *s < end)
333b20bb 6894 {
5be6c3b0 6895 c = *(*s)++;
333b20bb
GM
6896 if (c == 'x' || c == 'X')
6897 {
5be6c3b0 6898 while (*s < end)
333b20bb 6899 {
5be6c3b0 6900 c = *(*s)++;
333b20bb
GM
6901 if (isdigit (c))
6902 digit = c - '0';
6903 else if (c >= 'a' && c <= 'f')
6904 digit = c - 'a' + 10;
6905 else if (c >= 'A' && c <= 'F')
6906 digit = c - 'A' + 10;
6907 else
6908 break;
6909 value = 16 * value + digit;
6910 }
6911 }
6912 else if (isdigit (c))
6913 {
6914 value = c - '0';
5be6c3b0
GM
6915 while (*s < end
6916 && (c = *(*s)++, isdigit (c)))
333b20bb
GM
6917 value = 8 * value + c - '0';
6918 }
6919 }
6920 else
6921 {
6922 value = c - '0';
5be6c3b0
GM
6923 while (*s < end
6924 && (c = *(*s)++, isdigit (c)))
333b20bb
GM
6925 value = 10 * value + c - '0';
6926 }
6927
5be6c3b0
GM
6928 if (*s < end)
6929 *s = *s - 1;
333b20bb
GM
6930 *ival = value;
6931 c = XBM_TK_NUMBER;
6932 }
6933 else if (isalpha (c) || c == '_')
6934 {
6935 *sval++ = c;
5be6c3b0
GM
6936 while (*s < end
6937 && (c = *(*s)++, (isalnum (c) || c == '_')))
333b20bb
GM
6938 *sval++ = c;
6939 *sval = 0;
5be6c3b0
GM
6940 if (*s < end)
6941 *s = *s - 1;
333b20bb
GM
6942 c = XBM_TK_IDENT;
6943 }
0a695da7
GM
6944 else if (c == '/' && **s == '*')
6945 {
6946 /* C-style comment. */
6947 ++*s;
6948 while (**s && (**s != '*' || *(*s + 1) != '/'))
6949 ++*s;
6950 if (**s)
6951 {
6952 *s += 2;
6953 goto loop;
6954 }
6955 }
333b20bb
GM
6956
6957 return c;
6958}
6959
6960
6961/* Replacement for XReadBitmapFileData which isn't available under old
5be6c3b0
GM
6962 X versions. CONTENTS is a pointer to a buffer to parse; END is the
6963 buffer's end. Set *WIDTH and *HEIGHT to the width and height of
6964 the image. Return in *DATA the bitmap data allocated with xmalloc.
6965 Value is non-zero if successful. DATA null means just test if
b243755a 6966 CONTENTS looks like an in-memory XBM file. */
333b20bb
GM
6967
6968static int
5be6c3b0
GM
6969xbm_read_bitmap_data (contents, end, width, height, data)
6970 char *contents, *end;
333b20bb
GM
6971 int *width, *height;
6972 unsigned char **data;
6973{
5be6c3b0 6974 char *s = contents;
333b20bb
GM
6975 char buffer[BUFSIZ];
6976 int padding_p = 0;
6977 int v10 = 0;
6978 int bytes_per_line, i, nbytes;
6979 unsigned char *p;
6980 int value;
6981 int LA1;
6982
6983#define match() \
5be6c3b0 6984 LA1 = xbm_scan (&s, end, buffer, &value)
333b20bb
GM
6985
6986#define expect(TOKEN) \
6987 if (LA1 != (TOKEN)) \
6988 goto failure; \
6989 else \
7273d100 6990 match ()
333b20bb
GM
6991
6992#define expect_ident(IDENT) \
6993 if (LA1 == XBM_TK_IDENT && strcmp (buffer, (IDENT)) == 0) \
6994 match (); \
6995 else \
6996 goto failure
6997
333b20bb 6998 *width = *height = -1;
5be6c3b0
GM
6999 if (data)
7000 *data = NULL;
7001 LA1 = xbm_scan (&s, end, buffer, &value);
333b20bb
GM
7002
7003 /* Parse defines for width, height and hot-spots. */
7004 while (LA1 == '#')
7005 {
333b20bb
GM
7006 match ();
7007 expect_ident ("define");
7008 expect (XBM_TK_IDENT);
7009
7010 if (LA1 == XBM_TK_NUMBER);
7011 {
7012 char *p = strrchr (buffer, '_');
7013 p = p ? p + 1 : buffer;
7014 if (strcmp (p, "width") == 0)
7015 *width = value;
7016 else if (strcmp (p, "height") == 0)
7017 *height = value;
7018 }
7019 expect (XBM_TK_NUMBER);
7020 }
7021
7022 if (*width < 0 || *height < 0)
7023 goto failure;
5be6c3b0
GM
7024 else if (data == NULL)
7025 goto success;
333b20bb
GM
7026
7027 /* Parse bits. Must start with `static'. */
7028 expect_ident ("static");
7029 if (LA1 == XBM_TK_IDENT)
7030 {
7031 if (strcmp (buffer, "unsigned") == 0)
7032 {
7273d100 7033 match ();
333b20bb
GM
7034 expect_ident ("char");
7035 }
7036 else if (strcmp (buffer, "short") == 0)
7037 {
7038 match ();
7039 v10 = 1;
7040 if (*width % 16 && *width % 16 < 9)
7041 padding_p = 1;
7042 }
7043 else if (strcmp (buffer, "char") == 0)
7044 match ();
7045 else
7046 goto failure;
7047 }
7273d100 7048 else
333b20bb
GM
7049 goto failure;
7050
7051 expect (XBM_TK_IDENT);
7052 expect ('[');
7053 expect (']');
7054 expect ('=');
7055 expect ('{');
7056
7057 bytes_per_line = (*width + 7) / 8 + padding_p;
7058 nbytes = bytes_per_line * *height;
7059 p = *data = (char *) xmalloc (nbytes);
7060
7061 if (v10)
7062 {
333b20bb
GM
7063 for (i = 0; i < nbytes; i += 2)
7064 {
7065 int val = value;
7066 expect (XBM_TK_NUMBER);
7067
7068 *p++ = val;
7069 if (!padding_p || ((i + 2) % bytes_per_line))
7070 *p++ = value >> 8;
7273d100 7071
333b20bb
GM
7072 if (LA1 == ',' || LA1 == '}')
7073 match ();
7074 else
7075 goto failure;
7076 }
7077 }
7078 else
7079 {
7080 for (i = 0; i < nbytes; ++i)
7081 {
7082 int val = value;
7083 expect (XBM_TK_NUMBER);
7273d100 7084
333b20bb 7085 *p++ = val;
7273d100 7086
333b20bb
GM
7087 if (LA1 == ',' || LA1 == '}')
7088 match ();
7089 else
7090 goto failure;
7091 }
7092 }
7093
5be6c3b0 7094 success:
333b20bb
GM
7095 return 1;
7096
7097 failure:
7273d100 7098
5be6c3b0 7099 if (data && *data)
333b20bb
GM
7100 {
7101 xfree (*data);
7102 *data = NULL;
7103 }
7104 return 0;
7105
7106#undef match
7107#undef expect
7108#undef expect_ident
7109}
7110
7111
5be6c3b0
GM
7112/* Load XBM image IMG which will be displayed on frame F from buffer
7113 CONTENTS. END is the end of the buffer. Value is non-zero if
7114 successful. */
333b20bb
GM
7115
7116static int
5be6c3b0 7117xbm_load_image (f, img, contents, end)
333b20bb
GM
7118 struct frame *f;
7119 struct image *img;
5be6c3b0 7120 char *contents, *end;
333b20bb
GM
7121{
7122 int rc;
7123 unsigned char *data;
7124 int success_p = 0;
7273d100 7125
5be6c3b0 7126 rc = xbm_read_bitmap_data (contents, end, &img->width, &img->height, &data);
333b20bb
GM
7127 if (rc)
7128 {
7129 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
7130 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
7131 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
7132 Lisp_Object value;
7273d100 7133
333b20bb
GM
7134 xassert (img->width > 0 && img->height > 0);
7135
7136 /* Get foreground and background colors, maybe allocate colors. */
7137 value = image_spec_value (img->spec, QCforeground, NULL);
7138 if (!NILP (value))
7139 foreground = x_alloc_image_color (f, img, value, foreground);
333b20bb
GM
7140 value = image_spec_value (img->spec, QCbackground, NULL);
7141 if (!NILP (value))
f20a3b7a
MB
7142 {
7143 background = x_alloc_image_color (f, img, value, background);
7144 img->background = background;
7145 img->background_valid = 1;
7146 }
333b20bb 7147
333b20bb
GM
7148 img->pixmap
7149 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
7150 FRAME_X_WINDOW (f),
7151 data,
7152 img->width, img->height,
7153 foreground, background,
7154 depth);
7155 xfree (data);
7156
dd00328a 7157 if (img->pixmap == None)
333b20bb
GM
7158 {
7159 x_clear_image (f, img);
5be6c3b0 7160 image_error ("Unable to create X pixmap for `%s'", img->spec, Qnil);
333b20bb
GM
7161 }
7162 else
7163 success_p = 1;
333b20bb
GM
7164 }
7165 else
45158a91 7166 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
333b20bb 7167
333b20bb
GM
7168 return success_p;
7169}
7170
7171
5be6c3b0
GM
7172/* Value is non-zero if DATA looks like an in-memory XBM file. */
7173
7174static int
7175xbm_file_p (data)
7176 Lisp_Object data;
7177{
7178 int w, h;
7179 return (STRINGP (data)
d5db4077
KR
7180 && xbm_read_bitmap_data (SDATA (data),
7181 (SDATA (data)
7182 + SBYTES (data)),
5be6c3b0
GM
7183 &w, &h, NULL));
7184}
7185
7273d100 7186
333b20bb
GM
7187/* Fill image IMG which is used on frame F with pixmap data. Value is
7188 non-zero if successful. */
7189
7190static int
7191xbm_load (f, img)
7192 struct frame *f;
7193 struct image *img;
7194{
7195 int success_p = 0;
7196 Lisp_Object file_name;
7197
7198 xassert (xbm_image_p (img->spec));
7199
7200 /* If IMG->spec specifies a file name, create a non-file spec from it. */
7201 file_name = image_spec_value (img->spec, QCfile, NULL);
7202 if (STRINGP (file_name))
5be6c3b0
GM
7203 {
7204 Lisp_Object file;
7205 char *contents;
7206 int size;
7207 struct gcpro gcpro1;
7273d100 7208
5be6c3b0
GM
7209 file = x_find_image_file (file_name);
7210 GCPRO1 (file);
7211 if (!STRINGP (file))
7212 {
7213 image_error ("Cannot find image file `%s'", file_name, Qnil);
7214 UNGCPRO;
7215 return 0;
7216 }
7217
d5db4077 7218 contents = slurp_file (SDATA (file), &size);
5be6c3b0
GM
7219 if (contents == NULL)
7220 {
7221 image_error ("Error loading XBM image `%s'", img->spec, Qnil);
7222 UNGCPRO;
7223 return 0;
7224 }
7225
7226 success_p = xbm_load_image (f, img, contents, contents + size);
7227 UNGCPRO;
7228 }
333b20bb
GM
7229 else
7230 {
7231 struct image_keyword fmt[XBM_LAST];
7232 Lisp_Object data;
7233 int depth;
7234 unsigned long foreground = FRAME_FOREGROUND_PIXEL (f);
7235 unsigned long background = FRAME_BACKGROUND_PIXEL (f);
7236 char *bits;
9b207e8e 7237 int parsed_p;
5be6c3b0
GM
7238 int in_memory_file_p = 0;
7239
7240 /* See if data looks like an in-memory XBM file. */
7241 data = image_spec_value (img->spec, QCdata, NULL);
7242 in_memory_file_p = xbm_file_p (data);
333b20bb 7243
5be6c3b0 7244 /* Parse the image specification. */
333b20bb 7245 bcopy (xbm_format, fmt, sizeof fmt);
bfd2209f 7246 parsed_p = parse_image_spec (img->spec, fmt, XBM_LAST, Qxbm);
333b20bb
GM
7247 xassert (parsed_p);
7248
7249 /* Get specified width, and height. */
5be6c3b0
GM
7250 if (!in_memory_file_p)
7251 {
7252 img->width = XFASTINT (fmt[XBM_WIDTH].value);
7253 img->height = XFASTINT (fmt[XBM_HEIGHT].value);
7254 xassert (img->width > 0 && img->height > 0);
7255 }
333b20bb 7256
333b20bb 7257 /* Get foreground and background colors, maybe allocate colors. */
6f1be3b9
GM
7258 if (fmt[XBM_FOREGROUND].count
7259 && STRINGP (fmt[XBM_FOREGROUND].value))
333b20bb
GM
7260 foreground = x_alloc_image_color (f, img, fmt[XBM_FOREGROUND].value,
7261 foreground);
6f1be3b9
GM
7262 if (fmt[XBM_BACKGROUND].count
7263 && STRINGP (fmt[XBM_BACKGROUND].value))
333b20bb
GM
7264 background = x_alloc_image_color (f, img, fmt[XBM_BACKGROUND].value,
7265 background);
7266
5be6c3b0 7267 if (in_memory_file_p)
d5db4077
KR
7268 success_p = xbm_load_image (f, img, SDATA (data),
7269 (SDATA (data)
7270 + SBYTES (data)));
5be6c3b0 7271 else
333b20bb 7272 {
5be6c3b0
GM
7273 if (VECTORP (data))
7274 {
7275 int i;
7276 char *p;
7277 int nbytes = (img->width + BITS_PER_CHAR - 1) / BITS_PER_CHAR;
7273d100 7278
5be6c3b0
GM
7279 p = bits = (char *) alloca (nbytes * img->height);
7280 for (i = 0; i < img->height; ++i, p += nbytes)
7281 {
7282 Lisp_Object line = XVECTOR (data)->contents[i];
7283 if (STRINGP (line))
d5db4077 7284 bcopy (SDATA (line), p, nbytes);
5be6c3b0
GM
7285 else
7286 bcopy (XBOOL_VECTOR (line)->data, p, nbytes);
7287 }
7288 }
7289 else if (STRINGP (data))
d5db4077 7290 bits = SDATA (data);
5be6c3b0
GM
7291 else
7292 bits = XBOOL_VECTOR (data)->data;
7293
7294 /* Create the pixmap. */
7295 depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
7296 img->pixmap
7297 = XCreatePixmapFromBitmapData (FRAME_X_DISPLAY (f),
7298 FRAME_X_WINDOW (f),
7299 bits,
7300 img->width, img->height,
7301 foreground, background,
7302 depth);
7303 if (img->pixmap)
7304 success_p = 1;
7305 else
333b20bb 7306 {
5be6c3b0
GM
7307 image_error ("Unable to create pixmap for XBM image `%s'",
7308 img->spec, Qnil);
7309 x_clear_image (f, img);
333b20bb
GM
7310 }
7311 }
333b20bb
GM
7312 }
7313
7314 return success_p;
7315}
7273d100 7316
333b20bb
GM
7317
7318\f
7319/***********************************************************************
7320 XPM images
7321 ***********************************************************************/
7322
7273d100 7323#if HAVE_XPM
333b20bb
GM
7324
7325static int xpm_image_p P_ ((Lisp_Object object));
7326static int xpm_load P_ ((struct frame *f, struct image *img));
7327static int xpm_valid_color_symbols_p P_ ((Lisp_Object));
7328
7329#include "X11/xpm.h"
7330
7331/* The symbol `xpm' identifying XPM-format images. */
7332
7333Lisp_Object Qxpm;
7334
7335/* Indices of image specification fields in xpm_format, below. */
7336
7337enum xpm_keyword_index
7338{
7339 XPM_TYPE,
7340 XPM_FILE,
7341 XPM_DATA,
7342 XPM_ASCENT,
7343 XPM_MARGIN,
7344 XPM_RELIEF,
7345 XPM_ALGORITHM,
7346 XPM_HEURISTIC_MASK,
4a8e312c 7347 XPM_MASK,
333b20bb 7348 XPM_COLOR_SYMBOLS,
f20a3b7a 7349 XPM_BACKGROUND,
333b20bb
GM
7350 XPM_LAST
7351};
7352
7353/* Vector of image_keyword structures describing the format
7354 of valid XPM image specifications. */
7355
7356static struct image_keyword xpm_format[XPM_LAST] =
7357{
7358 {":type", IMAGE_SYMBOL_VALUE, 1},
7359 {":file", IMAGE_STRING_VALUE, 0},
7360 {":data", IMAGE_STRING_VALUE, 0},
7c7ff7f5 7361 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 7362 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 7363 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 7364 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
333b20bb 7365 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 7366 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
7367 {":color-symbols", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
7368 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
7369};
7370
7371/* Structure describing the image type XBM. */
7372
7373static struct image_type xpm_type =
7374{
7375 &Qxpm,
7376 xpm_image_p,
7377 xpm_load,
7378 x_clear_image,
7379 NULL
7380};
7381
7382
b243755a
GM
7383/* Define ALLOC_XPM_COLORS if we can use Emacs' own color allocation
7384 functions for allocating image colors. Our own functions handle
7385 color allocation failures more gracefully than the ones on the XPM
7386 lib. */
7387
7388#if defined XpmAllocColor && defined XpmFreeColors && defined XpmColorClosure
7389#define ALLOC_XPM_COLORS
7390#endif
7391
7392#ifdef ALLOC_XPM_COLORS
7393
f72c62ad 7394static void xpm_init_color_cache P_ ((struct frame *, XpmAttributes *));
b243755a
GM
7395static void xpm_free_color_cache P_ ((void));
7396static int xpm_lookup_color P_ ((struct frame *, char *, XColor *));
f72c62ad
GM
7397static int xpm_color_bucket P_ ((char *));
7398static struct xpm_cached_color *xpm_cache_color P_ ((struct frame *, char *,
7399 XColor *, int));
b243755a
GM
7400
7401/* An entry in a hash table used to cache color definitions of named
7402 colors. This cache is necessary to speed up XPM image loading in
7403 case we do color allocations ourselves. Without it, we would need
7404 a call to XParseColor per pixel in the image. */
7405
7406struct xpm_cached_color
7407{
7408 /* Next in collision chain. */
7409 struct xpm_cached_color *next;
7410
7411 /* Color definition (RGB and pixel color). */
7412 XColor color;
7413
7414 /* Color name. */
7415 char name[1];
7416};
7417
7418/* The hash table used for the color cache, and its bucket vector
7419 size. */
7420
7421#define XPM_COLOR_CACHE_BUCKETS 1001
7422struct xpm_cached_color **xpm_color_cache;
7423
b243755a
GM
7424/* Initialize the color cache. */
7425
7426static void
f72c62ad
GM
7427xpm_init_color_cache (f, attrs)
7428 struct frame *f;
7429 XpmAttributes *attrs;
b243755a
GM
7430{
7431 size_t nbytes = XPM_COLOR_CACHE_BUCKETS * sizeof *xpm_color_cache;
7432 xpm_color_cache = (struct xpm_cached_color **) xmalloc (nbytes);
7433 memset (xpm_color_cache, 0, nbytes);
7434 init_color_table ();
f72c62ad
GM
7435
7436 if (attrs->valuemask & XpmColorSymbols)
7437 {
7438 int i;
7439 XColor color;
7273d100 7440
f72c62ad
GM
7441 for (i = 0; i < attrs->numsymbols; ++i)
7442 if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7443 attrs->colorsymbols[i].value, &color))
7444 {
7445 color.pixel = lookup_rgb_color (f, color.red, color.green,
7446 color.blue);
7447 xpm_cache_color (f, attrs->colorsymbols[i].name, &color, -1);
7448 }
7449 }
b243755a
GM
7450}
7451
7452
7453/* Free the color cache. */
7454
7455static void
7456xpm_free_color_cache ()
7457{
7458 struct xpm_cached_color *p, *next;
7459 int i;
7460
7461 for (i = 0; i < XPM_COLOR_CACHE_BUCKETS; ++i)
7462 for (p = xpm_color_cache[i]; p; p = next)
7463 {
7464 next = p->next;
7465 xfree (p);
7466 }
7467
7468 xfree (xpm_color_cache);
7469 xpm_color_cache = NULL;
7470 free_color_table ();
7471}
7472
7473
f72c62ad
GM
7474/* Return the bucket index for color named COLOR_NAME in the color
7475 cache. */
7476
7477static int
7478xpm_color_bucket (color_name)
7479 char *color_name;
7480{
7481 unsigned h = 0;
7482 char *s;
7273d100 7483
f72c62ad
GM
7484 for (s = color_name; *s; ++s)
7485 h = (h << 2) ^ *s;
7486 return h %= XPM_COLOR_CACHE_BUCKETS;
7487}
7488
7489
7490/* On frame F, cache values COLOR for color with name COLOR_NAME.
7491 BUCKET, if >= 0, is a precomputed bucket index. Value is the cache
7492 entry added. */
7493
7494static struct xpm_cached_color *
7495xpm_cache_color (f, color_name, color, bucket)
7496 struct frame *f;
7497 char *color_name;
7498 XColor *color;
7499 int bucket;
7500{
7501 size_t nbytes;
7502 struct xpm_cached_color *p;
7273d100 7503
f72c62ad
GM
7504 if (bucket < 0)
7505 bucket = xpm_color_bucket (color_name);
7273d100 7506
f72c62ad
GM
7507 nbytes = sizeof *p + strlen (color_name);
7508 p = (struct xpm_cached_color *) xmalloc (nbytes);
7509 strcpy (p->name, color_name);
7510 p->color = *color;
7511 p->next = xpm_color_cache[bucket];
7512 xpm_color_cache[bucket] = p;
7513 return p;
7514}
7515
7516
b243755a
GM
7517/* Look up color COLOR_NAME for frame F in the color cache. If found,
7518 return the cached definition in *COLOR. Otherwise, make a new
7519 entry in the cache and allocate the color. Value is zero if color
7520 allocation failed. */
7521
7522static int
7523xpm_lookup_color (f, color_name, color)
7524 struct frame *f;
7525 char *color_name;
7526 XColor *color;
7527{
b243755a 7528 struct xpm_cached_color *p;
83676598 7529 int h = xpm_color_bucket (color_name);
b243755a
GM
7530
7531 for (p = xpm_color_cache[h]; p; p = p->next)
7532 if (strcmp (p->name, color_name) == 0)
7533 break;
7534
7535 if (p != NULL)
7536 *color = p->color;
7537 else if (XParseColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
7538 color_name, color))
7539 {
b243755a
GM
7540 color->pixel = lookup_rgb_color (f, color->red, color->green,
7541 color->blue);
f72c62ad 7542 p = xpm_cache_color (f, color_name, color, h);
b243755a 7543 }
7273d100 7544
b243755a
GM
7545 return p != NULL;
7546}
7547
7548
7549/* Callback for allocating color COLOR_NAME. Called from the XPM lib.
7550 CLOSURE is a pointer to the frame on which we allocate the
7551 color. Return in *COLOR the allocated color. Value is non-zero
7552 if successful. */
7553
7554static int
7555xpm_alloc_color (dpy, cmap, color_name, color, closure)
7556 Display *dpy;
7557 Colormap cmap;
7558 char *color_name;
7559 XColor *color;
7560 void *closure;
7561{
7562 return xpm_lookup_color ((struct frame *) closure, color_name, color);
7563}
7564
7565
7566/* Callback for freeing NPIXELS colors contained in PIXELS. CLOSURE
7567 is a pointer to the frame on which we allocate the color. Value is
7568 non-zero if successful. */
7569
7570static int
7571xpm_free_colors (dpy, cmap, pixels, npixels, closure)
7572 Display *dpy;
7573 Colormap cmap;
7574 Pixel *pixels;
7575 int npixels;
7576 void *closure;
7577{
7578 return 1;
7579}
7580
7581#endif /* ALLOC_XPM_COLORS */
7582
7583
333b20bb
GM
7584/* Value is non-zero if COLOR_SYMBOLS is a valid color symbols list
7585 for XPM images. Such a list must consist of conses whose car and
7586 cdr are strings. */
7587
7588static int
7589xpm_valid_color_symbols_p (color_symbols)
7590 Lisp_Object color_symbols;
7591{
7592 while (CONSP (color_symbols))
7593 {
7594 Lisp_Object sym = XCAR (color_symbols);
7595 if (!CONSP (sym)
7596 || !STRINGP (XCAR (sym))
7597 || !STRINGP (XCDR (sym)))
7598 break;
7599 color_symbols = XCDR (color_symbols);
7600 }
7601
7602 return NILP (color_symbols);
7603}
7604
7605
7606/* Value is non-zero if OBJECT is a valid XPM image specification. */
7607
7608static int
7609xpm_image_p (object)
7610 Lisp_Object object;
7611{
7612 struct image_keyword fmt[XPM_LAST];
7613 bcopy (xpm_format, fmt, sizeof fmt);
bfd2209f 7614 return (parse_image_spec (object, fmt, XPM_LAST, Qxpm)
333b20bb
GM
7615 /* Either `:file' or `:data' must be present. */
7616 && fmt[XPM_FILE].count + fmt[XPM_DATA].count == 1
7617 /* Either no `:color-symbols' or it's a list of conses
7618 whose car and cdr are strings. */
7619 && (fmt[XPM_COLOR_SYMBOLS].count == 0
7c7ff7f5 7620 || xpm_valid_color_symbols_p (fmt[XPM_COLOR_SYMBOLS].value)));
333b20bb
GM
7621}
7622
7623
7624/* Load image IMG which will be displayed on frame F. Value is
7625 non-zero if successful. */
7626
7627static int
7628xpm_load (f, img)
7629 struct frame *f;
7630 struct image *img;
7631{
9b207e8e 7632 int rc;
333b20bb
GM
7633 XpmAttributes attrs;
7634 Lisp_Object specified_file, color_symbols;
7635
7636 /* Configure the XPM lib. Use the visual of frame F. Allocate
7637 close colors. Return colors allocated. */
7638 bzero (&attrs, sizeof attrs);
9b2956e2
GM
7639 attrs.visual = FRAME_X_VISUAL (f);
7640 attrs.colormap = FRAME_X_COLORMAP (f);
333b20bb 7641 attrs.valuemask |= XpmVisual;
9b2956e2 7642 attrs.valuemask |= XpmColormap;
b243755a
GM
7643
7644#ifdef ALLOC_XPM_COLORS
7645 /* Allocate colors with our own functions which handle
7646 failing color allocation more gracefully. */
7647 attrs.color_closure = f;
7648 attrs.alloc_color = xpm_alloc_color;
7649 attrs.free_colors = xpm_free_colors;
7650 attrs.valuemask |= XpmAllocColor | XpmFreeColors | XpmColorClosure;
7651#else /* not ALLOC_XPM_COLORS */
7652 /* Let the XPM lib allocate colors. */
333b20bb 7653 attrs.valuemask |= XpmReturnAllocPixels;
e4c082be 7654#ifdef XpmAllocCloseColors
333b20bb
GM
7655 attrs.alloc_close_colors = 1;
7656 attrs.valuemask |= XpmAllocCloseColors;
b243755a 7657#else /* not XpmAllocCloseColors */
e4c082be
RS
7658 attrs.closeness = 600;
7659 attrs.valuemask |= XpmCloseness;
b243755a
GM
7660#endif /* not XpmAllocCloseColors */
7661#endif /* ALLOC_XPM_COLORS */
333b20bb
GM
7662
7663 /* If image specification contains symbolic color definitions, add
7664 these to `attrs'. */
7665 color_symbols = image_spec_value (img->spec, QCcolor_symbols, NULL);
7666 if (CONSP (color_symbols))
7667 {
7668 Lisp_Object tail;
7669 XpmColorSymbol *xpm_syms;
7670 int i, size;
7273d100 7671
333b20bb
GM
7672 attrs.valuemask |= XpmColorSymbols;
7673
7674 /* Count number of symbols. */
7675 attrs.numsymbols = 0;
7676 for (tail = color_symbols; CONSP (tail); tail = XCDR (tail))
7677 ++attrs.numsymbols;
7678
7679 /* Allocate an XpmColorSymbol array. */
7680 size = attrs.numsymbols * sizeof *xpm_syms;
7681 xpm_syms = (XpmColorSymbol *) alloca (size);
7682 bzero (xpm_syms, size);
7683 attrs.colorsymbols = xpm_syms;
7684
7685 /* Fill the color symbol array. */
7686 for (tail = color_symbols, i = 0;
7687 CONSP (tail);
7688 ++i, tail = XCDR (tail))
7689 {
7690 Lisp_Object name = XCAR (XCAR (tail));
7691 Lisp_Object color = XCDR (XCAR (tail));
d5db4077
KR
7692 xpm_syms[i].name = (char *) alloca (SCHARS (name) + 1);
7693 strcpy (xpm_syms[i].name, SDATA (name));
7694 xpm_syms[i].value = (char *) alloca (SCHARS (color) + 1);
7695 strcpy (xpm_syms[i].value, SDATA (color));
333b20bb
GM
7696 }
7697 }
7698
7699 /* Create a pixmap for the image, either from a file, or from a
7700 string buffer containing data in the same format as an XPM file. */
b243755a 7701#ifdef ALLOC_XPM_COLORS
f72c62ad 7702 xpm_init_color_cache (f, &attrs);
b243755a 7703#endif
7273d100 7704
333b20bb
GM
7705 specified_file = image_spec_value (img->spec, QCfile, NULL);
7706 if (STRINGP (specified_file))
7707 {
7708 Lisp_Object file = x_find_image_file (specified_file);
7709 if (!STRINGP (file))
7710 {
45158a91 7711 image_error ("Cannot find image file `%s'", specified_file, Qnil);
333b20bb
GM
7712 return 0;
7713 }
7273d100 7714
333b20bb 7715 rc = XpmReadFileToPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
d5db4077 7716 SDATA (file), &img->pixmap, &img->mask,
333b20bb
GM
7717 &attrs);
7718 }
7719 else
7720 {
7721 Lisp_Object buffer = image_spec_value (img->spec, QCdata, NULL);
7722 rc = XpmCreatePixmapFromBuffer (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
d5db4077 7723 SDATA (buffer),
333b20bb
GM
7724 &img->pixmap, &img->mask,
7725 &attrs);
7726 }
333b20bb
GM
7727
7728 if (rc == XpmSuccess)
7729 {
b243755a
GM
7730#ifdef ALLOC_XPM_COLORS
7731 img->colors = colors_in_color_table (&img->ncolors);
7732#else /* not ALLOC_XPM_COLORS */
f47a9ec4
KR
7733 int i;
7734
333b20bb
GM
7735 img->ncolors = attrs.nalloc_pixels;
7736 img->colors = (unsigned long *) xmalloc (img->ncolors
7737 * sizeof *img->colors);
7738 for (i = 0; i < attrs.nalloc_pixels; ++i)
3b4ae1cc
GM
7739 {
7740 img->colors[i] = attrs.alloc_pixels[i];
7741#ifdef DEBUG_X_COLORS
7742 register_color (img->colors[i]);
7743#endif
7744 }
b243755a 7745#endif /* not ALLOC_XPM_COLORS */
333b20bb
GM
7746
7747 img->width = attrs.width;
7748 img->height = attrs.height;
7749 xassert (img->width > 0 && img->height > 0);
7750
7751 /* The call to XpmFreeAttributes below frees attrs.alloc_pixels. */
333b20bb 7752 XpmFreeAttributes (&attrs);
333b20bb
GM
7753 }
7754 else
7755 {
7756 switch (rc)
7757 {
7758 case XpmOpenFailed:
7759 image_error ("Error opening XPM file (%s)", img->spec, Qnil);
7760 break;
7273d100 7761
333b20bb
GM
7762 case XpmFileInvalid:
7763 image_error ("Invalid XPM file (%s)", img->spec, Qnil);
7764 break;
7273d100 7765
333b20bb
GM
7766 case XpmNoMemory:
7767 image_error ("Out of memory (%s)", img->spec, Qnil);
7768 break;
7273d100 7769
333b20bb
GM
7770 case XpmColorFailed:
7771 image_error ("Color allocation error (%s)", img->spec, Qnil);
7772 break;
7273d100 7773
333b20bb
GM
7774 default:
7775 image_error ("Unknown error (%s)", img->spec, Qnil);
7776 break;
7777 }
7778 }
7779
b243755a
GM
7780#ifdef ALLOC_XPM_COLORS
7781 xpm_free_color_cache ();
7782#endif
333b20bb
GM
7783 return rc == XpmSuccess;
7784}
7785
7786#endif /* HAVE_XPM != 0 */
7787
7788\f
7789/***********************************************************************
7790 Color table
7791 ***********************************************************************/
7792
7793/* An entry in the color table mapping an RGB color to a pixel color. */
7794
7795struct ct_color
7796{
7797 int r, g, b;
7798 unsigned long pixel;
7799
7800 /* Next in color table collision list. */
7801 struct ct_color *next;
7802};
7803
7804/* The bucket vector size to use. Must be prime. */
7805
7806#define CT_SIZE 101
7807
7808/* Value is a hash of the RGB color given by R, G, and B. */
7809
7810#define CT_HASH_RGB(R, G, B) (((R) << 16) ^ ((G) << 8) ^ (B))
7811
7812/* The color hash table. */
7813
7814struct ct_color **ct_table;
7815
7816/* Number of entries in the color table. */
7817
7818int ct_colors_allocated;
7819
333b20bb
GM
7820/* Initialize the color table. */
7821
7822static void
7823init_color_table ()
7824{
7825 int size = CT_SIZE * sizeof (*ct_table);
7826 ct_table = (struct ct_color **) xmalloc (size);
7827 bzero (ct_table, size);
7828 ct_colors_allocated = 0;
7829}
7830
7831
7832/* Free memory associated with the color table. */
7833
7834static void
7835free_color_table ()
7836{
7837 int i;
7838 struct ct_color *p, *next;
7839
7840 for (i = 0; i < CT_SIZE; ++i)
7841 for (p = ct_table[i]; p; p = next)
7842 {
7843 next = p->next;
7844 xfree (p);
7845 }
7846
7847 xfree (ct_table);
7848 ct_table = NULL;
7849}
7850
7851
7852/* Value is a pixel color for RGB color R, G, B on frame F. If an
7853 entry for that color already is in the color table, return the
7854 pixel color of that entry. Otherwise, allocate a new color for R,
7855 G, B, and make an entry in the color table. */
7856
7857static unsigned long
7858lookup_rgb_color (f, r, g, b)
7859 struct frame *f;
7860 int r, g, b;
7861{
7862 unsigned hash = CT_HASH_RGB (r, g, b);
7863 int i = hash % CT_SIZE;
7864 struct ct_color *p;
7865
7866 for (p = ct_table[i]; p; p = p->next)
7867 if (p->r == r && p->g == g && p->b == b)
7868 break;
7869
7870 if (p == NULL)
7871 {
7872 XColor color;
7873 Colormap cmap;
7874 int rc;
7875
7876 color.red = r;
7877 color.green = g;
7878 color.blue = b;
7273d100 7879
9b2956e2 7880 cmap = FRAME_X_COLORMAP (f);
d62c8769 7881 rc = x_alloc_nearest_color (f, cmap, &color);
333b20bb
GM
7882
7883 if (rc)
7884 {
7885 ++ct_colors_allocated;
7273d100 7886
333b20bb
GM
7887 p = (struct ct_color *) xmalloc (sizeof *p);
7888 p->r = r;
7889 p->g = g;
7890 p->b = b;
7891 p->pixel = color.pixel;
7892 p->next = ct_table[i];
7893 ct_table[i] = p;
7894 }
7895 else
7896 return FRAME_FOREGROUND_PIXEL (f);
7897 }
7898
7899 return p->pixel;
7900}
7901
7902
7903/* Look up pixel color PIXEL which is used on frame F in the color
7904 table. If not already present, allocate it. Value is PIXEL. */
7905
7906static unsigned long
7907lookup_pixel_color (f, pixel)
7908 struct frame *f;
7909 unsigned long pixel;
7910{
7911 int i = pixel % CT_SIZE;
7912 struct ct_color *p;
7913
7914 for (p = ct_table[i]; p; p = p->next)
7915 if (p->pixel == pixel)
7916 break;
7917
7918 if (p == NULL)
7919 {
7920 XColor color;
7921 Colormap cmap;
7922 int rc;
7923
9b2956e2 7924 cmap = FRAME_X_COLORMAP (f);
333b20bb 7925 color.pixel = pixel;
a31fedb7 7926 x_query_color (f, &color);
d62c8769 7927 rc = x_alloc_nearest_color (f, cmap, &color);
333b20bb
GM
7928
7929 if (rc)
7930 {
7931 ++ct_colors_allocated;
7273d100 7932
333b20bb
GM
7933 p = (struct ct_color *) xmalloc (sizeof *p);
7934 p->r = color.red;
7935 p->g = color.green;
7936 p->b = color.blue;
7937 p->pixel = pixel;
7938 p->next = ct_table[i];
7939 ct_table[i] = p;
7940 }
7941 else
7942 return FRAME_FOREGROUND_PIXEL (f);
7943 }
7273d100 7944
333b20bb
GM
7945 return p->pixel;
7946}
7947
7948
7949/* Value is a vector of all pixel colors contained in the color table,
7950 allocated via xmalloc. Set *N to the number of colors. */
7951
7952static unsigned long *
7953colors_in_color_table (n)
7954 int *n;
7955{
7956 int i, j;
7957 struct ct_color *p;
7958 unsigned long *colors;
7959
7960 if (ct_colors_allocated == 0)
7961 {
7962 *n = 0;
7963 colors = NULL;
7964 }
7965 else
7966 {
7967 colors = (unsigned long *) xmalloc (ct_colors_allocated
7968 * sizeof *colors);
7969 *n = ct_colors_allocated;
7273d100 7970
333b20bb
GM
7971 for (i = j = 0; i < CT_SIZE; ++i)
7972 for (p = ct_table[i]; p; p = p->next)
7973 colors[j++] = p->pixel;
7974 }
7975
7976 return colors;
7977}
7978
7979
7980\f
7981/***********************************************************************
7982 Algorithms
7983 ***********************************************************************/
7984
4a8e312c
GM
7985static XColor *x_to_xcolors P_ ((struct frame *, struct image *, int));
7986static void x_from_xcolors P_ ((struct frame *, struct image *, XColor *));
7987static void x_detect_edges P_ ((struct frame *, struct image *, int[9], int));
7988
d2dc8167 7989/* Non-zero means draw a cross on images having `:conversion
14819cb3
GM
7990 disabled'. */
7991
7992int cross_disabled_images;
7993
4a8e312c
GM
7994/* Edge detection matrices for different edge-detection
7995 strategies. */
7996
7997static int emboss_matrix[9] = {
7998 /* x - 1 x x + 1 */
7999 2, -1, 0, /* y - 1 */
8000 -1, 0, 1, /* y */
8001 0, 1, -2 /* y + 1 */
8002};
333b20bb 8003
4a8e312c
GM
8004static int laplace_matrix[9] = {
8005 /* x - 1 x x + 1 */
8006 1, 0, 0, /* y - 1 */
8007 0, 0, 0, /* y */
8008 0, 0, -1 /* y + 1 */
8009};
333b20bb 8010
14819cb3
GM
8011/* Value is the intensity of the color whose red/green/blue values
8012 are R, G, and B. */
8013
8014#define COLOR_INTENSITY(R, G, B) ((2 * (R) + 3 * (G) + (B)) / 6)
8015
333b20bb 8016
4a8e312c
GM
8017/* On frame F, return an array of XColor structures describing image
8018 IMG->pixmap. Each XColor structure has its pixel color set. RGB_P
8019 non-zero means also fill the red/green/blue members of the XColor
8020 structures. Value is a pointer to the array of XColors structures,
8021 allocated with xmalloc; it must be freed by the caller. */
8022
8023static XColor *
8024x_to_xcolors (f, img, rgb_p)
333b20bb 8025 struct frame *f;
4a8e312c
GM
8026 struct image *img;
8027 int rgb_p;
333b20bb 8028{
4a8e312c
GM
8029 int x, y;
8030 XColor *colors, *p;
8031 XImage *ximg;
333b20bb 8032
4a8e312c
GM
8033 colors = (XColor *) xmalloc (img->width * img->height * sizeof *colors);
8034
8035 /* Get the X image IMG->pixmap. */
8036 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
8037 0, 0, img->width, img->height, ~0, ZPixmap);
333b20bb 8038
4a8e312c
GM
8039 /* Fill the `pixel' members of the XColor array. I wished there
8040 were an easy and portable way to circumvent XGetPixel. */
8041 p = colors;
8042 for (y = 0; y < img->height; ++y)
8043 {
8044 XColor *row = p;
7273d100 8045
4a8e312c
GM
8046 for (x = 0; x < img->width; ++x, ++p)
8047 p->pixel = XGetPixel (ximg, x, y);
8048
8049 if (rgb_p)
a31fedb7 8050 x_query_colors (f, row, img->width);
4a8e312c
GM
8051 }
8052
8053 XDestroyImage (ximg);
4a8e312c 8054 return colors;
333b20bb
GM
8055}
8056
8057
4a8e312c
GM
8058/* Create IMG->pixmap from an array COLORS of XColor structures, whose
8059 RGB members are set. F is the frame on which this all happens.
8060 COLORS will be freed; an existing IMG->pixmap will be freed, too. */
333b20bb
GM
8061
8062static void
4a8e312c 8063x_from_xcolors (f, img, colors)
333b20bb 8064 struct frame *f;
4a8e312c
GM
8065 struct image *img;
8066 XColor *colors;
333b20bb 8067{
4a8e312c
GM
8068 int x, y;
8069 XImage *oimg;
8070 Pixmap pixmap;
8071 XColor *p;
7273d100 8072
4a8e312c 8073 init_color_table ();
7273d100 8074
4a8e312c
GM
8075 x_create_x_image_and_pixmap (f, img->width, img->height, 0,
8076 &oimg, &pixmap);
8077 p = colors;
8078 for (y = 0; y < img->height; ++y)
8079 for (x = 0; x < img->width; ++x, ++p)
8080 {
8081 unsigned long pixel;
8082 pixel = lookup_rgb_color (f, p->red, p->green, p->blue);
8083 XPutPixel (oimg, x, y, pixel);
8084 }
8085
8086 xfree (colors);
dd00328a 8087 x_clear_image_1 (f, img, 1, 0, 1);
4a8e312c
GM
8088
8089 x_put_x_image (f, oimg, pixmap, img->width, img->height);
8090 x_destroy_x_image (oimg);
8091 img->pixmap = pixmap;
8092 img->colors = colors_in_color_table (&img->ncolors);
8093 free_color_table ();
333b20bb
GM
8094}
8095
8096
4a8e312c
GM
8097/* On frame F, perform edge-detection on image IMG.
8098
8099 MATRIX is a nine-element array specifying the transformation
8100 matrix. See emboss_matrix for an example.
7273d100 8101
4a8e312c
GM
8102 COLOR_ADJUST is a color adjustment added to each pixel of the
8103 outgoing image. */
333b20bb
GM
8104
8105static void
4a8e312c 8106x_detect_edges (f, img, matrix, color_adjust)
333b20bb
GM
8107 struct frame *f;
8108 struct image *img;
4a8e312c 8109 int matrix[9], color_adjust;
333b20bb 8110{
4a8e312c
GM
8111 XColor *colors = x_to_xcolors (f, img, 1);
8112 XColor *new, *p;
8113 int x, y, i, sum;
333b20bb 8114
4a8e312c
GM
8115 for (i = sum = 0; i < 9; ++i)
8116 sum += abs (matrix[i]);
333b20bb 8117
4a8e312c 8118#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
333b20bb 8119
4a8e312c 8120 new = (XColor *) xmalloc (img->width * img->height * sizeof *new);
333b20bb 8121
4a8e312c
GM
8122 for (y = 0; y < img->height; ++y)
8123 {
8124 p = COLOR (new, 0, y);
8125 p->red = p->green = p->blue = 0xffff/2;
8126 p = COLOR (new, img->width - 1, y);
8127 p->red = p->green = p->blue = 0xffff/2;
8128 }
7273d100 8129
4a8e312c
GM
8130 for (x = 1; x < img->width - 1; ++x)
8131 {
8132 p = COLOR (new, x, 0);
8133 p->red = p->green = p->blue = 0xffff/2;
8134 p = COLOR (new, x, img->height - 1);
8135 p->red = p->green = p->blue = 0xffff/2;
8136 }
333b20bb 8137
4a8e312c 8138 for (y = 1; y < img->height - 1; ++y)
333b20bb 8139 {
4a8e312c 8140 p = COLOR (new, 1, y);
7273d100 8141
4a8e312c
GM
8142 for (x = 1; x < img->width - 1; ++x, ++p)
8143 {
14819cb3 8144 int r, g, b, y1, x1;
4a8e312c
GM
8145
8146 r = g = b = i = 0;
8147 for (y1 = y - 1; y1 < y + 2; ++y1)
8148 for (x1 = x - 1; x1 < x + 2; ++x1, ++i)
8149 if (matrix[i])
8150 {
8151 XColor *t = COLOR (colors, x1, y1);
8152 r += matrix[i] * t->red;
8153 g += matrix[i] * t->green;
8154 b += matrix[i] * t->blue;
8155 }
333b20bb 8156
4a8e312c
GM
8157 r = (r / sum + color_adjust) & 0xffff;
8158 g = (g / sum + color_adjust) & 0xffff;
8159 b = (b / sum + color_adjust) & 0xffff;
14819cb3 8160 p->red = p->green = p->blue = COLOR_INTENSITY (r, g, b);
333b20bb 8161 }
333b20bb
GM
8162 }
8163
4a8e312c
GM
8164 xfree (colors);
8165 x_from_xcolors (f, img, new);
333b20bb 8166
4a8e312c
GM
8167#undef COLOR
8168}
8169
8170
8171/* Perform the pre-defined `emboss' edge-detection on image IMG
8172 on frame F. */
8173
8174static void
8175x_emboss (f, img)
8176 struct frame *f;
8177 struct image *img;
8178{
8179 x_detect_edges (f, img, emboss_matrix, 0xffff / 2);
8180}
8181
8182
8183/* Perform the pre-defined `laplace' edge-detection on image IMG
8184 on frame F. */
8185
8186static void
8187x_laplace (f, img)
8188 struct frame *f;
8189 struct image *img;
8190{
8191 x_detect_edges (f, img, laplace_matrix, 45000);
8192}
8193
8194
8195/* Perform edge-detection on image IMG on frame F, with specified
8196 transformation matrix MATRIX and color-adjustment COLOR_ADJUST.
8197
8198 MATRIX must be either
8199
8200 - a list of at least 9 numbers in row-major form
8201 - a vector of at least 9 numbers
8202
8203 COLOR_ADJUST nil means use a default; otherwise it must be a
8204 number. */
8205
8206static void
8207x_edge_detection (f, img, matrix, color_adjust)
8208 struct frame *f;
8209 struct image *img;
8210 Lisp_Object matrix, color_adjust;
8211{
8212 int i = 0;
8213 int trans[9];
7273d100 8214
4a8e312c
GM
8215 if (CONSP (matrix))
8216 {
8217 for (i = 0;
8218 i < 9 && CONSP (matrix) && NUMBERP (XCAR (matrix));
8219 ++i, matrix = XCDR (matrix))
8220 trans[i] = XFLOATINT (XCAR (matrix));
8221 }
8222 else if (VECTORP (matrix) && ASIZE (matrix) >= 9)
8223 {
8224 for (i = 0; i < 9 && NUMBERP (AREF (matrix, i)); ++i)
8225 trans[i] = XFLOATINT (AREF (matrix, i));
8226 }
333b20bb 8227
4a8e312c
GM
8228 if (NILP (color_adjust))
8229 color_adjust = make_number (0xffff / 2);
333b20bb 8230
4a8e312c
GM
8231 if (i == 9 && NUMBERP (color_adjust))
8232 x_detect_edges (f, img, trans, (int) XFLOATINT (color_adjust));
333b20bb
GM
8233}
8234
8235
14819cb3
GM
8236/* Transform image IMG on frame F so that it looks disabled. */
8237
8238static void
8239x_disable_image (f, img)
8240 struct frame *f;
8241 struct image *img;
8242{
8243 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
dd00328a 8244
14819cb3
GM
8245 if (dpyinfo->n_planes >= 2)
8246 {
8247 /* Color (or grayscale). Convert to gray, and equalize. Just
8248 drawing such images with a stipple can look very odd, so
8249 we're using this method instead. */
8250 XColor *colors = x_to_xcolors (f, img, 1);
8251 XColor *p, *end;
8252 const int h = 15000;
8253 const int l = 30000;
8254
8255 for (p = colors, end = colors + img->width * img->height;
8256 p < end;
8257 ++p)
8258 {
8259 int i = COLOR_INTENSITY (p->red, p->green, p->blue);
8260 int i2 = (0xffff - h - l) * i / 0xffff + l;
8261 p->red = p->green = p->blue = i2;
8262 }
8263
8264 x_from_xcolors (f, img, colors);
8265 }
8266
8267 /* Draw a cross over the disabled image, if we must or if we
8268 should. */
8269 if (dpyinfo->n_planes < 2 || cross_disabled_images)
8270 {
8271 Display *dpy = FRAME_X_DISPLAY (f);
8272 GC gc;
8273
14819cb3
GM
8274 gc = XCreateGC (dpy, img->pixmap, 0, NULL);
8275 XSetForeground (dpy, gc, BLACK_PIX_DEFAULT (f));
8276 XDrawLine (dpy, img->pixmap, gc, 0, 0,
8277 img->width - 1, img->height - 1);
8278 XDrawLine (dpy, img->pixmap, gc, 0, img->height - 1,
8279 img->width - 1, 0);
8280 XFreeGC (dpy, gc);
8281
8282 if (img->mask)
8283 {
8284 gc = XCreateGC (dpy, img->mask, 0, NULL);
8285 XSetForeground (dpy, gc, WHITE_PIX_DEFAULT (f));
8286 XDrawLine (dpy, img->mask, gc, 0, 0,
8287 img->width - 1, img->height - 1);
8288 XDrawLine (dpy, img->mask, gc, 0, img->height - 1,
8289 img->width - 1, 0);
8290 XFreeGC (dpy, gc);
8291 }
14819cb3
GM
8292 }
8293}
8294
8295
333b20bb
GM
8296/* Build a mask for image IMG which is used on frame F. FILE is the
8297 name of an image file, for error messages. HOW determines how to
fcf431dc
GM
8298 determine the background color of IMG. If it is a list '(R G B)',
8299 with R, G, and B being integers >= 0, take that as the color of the
8300 background. Otherwise, determine the background color of IMG
8301 heuristically. Value is non-zero if successful. */
333b20bb
GM
8302
8303static int
45158a91 8304x_build_heuristic_mask (f, img, how)
333b20bb 8305 struct frame *f;
333b20bb
GM
8306 struct image *img;
8307 Lisp_Object how;
8308{
8309 Display *dpy = FRAME_X_DISPLAY (f);
333b20bb 8310 XImage *ximg, *mask_img;
f20a3b7a 8311 int x, y, rc, use_img_background;
8ec8a5ec 8312 unsigned long bg = 0;
333b20bb 8313
4a8e312c
GM
8314 if (img->mask)
8315 {
8316 XFreePixmap (FRAME_X_DISPLAY (f), img->mask);
dd00328a 8317 img->mask = None;
f20a3b7a 8318 img->background_transparent_valid = 0;
4a8e312c 8319 }
dd00328a 8320
333b20bb 8321 /* Create an image and pixmap serving as mask. */
45158a91 8322 rc = x_create_x_image_and_pixmap (f, img->width, img->height, 1,
333b20bb
GM
8323 &mask_img, &img->mask);
8324 if (!rc)
28c7826c 8325 return 0;
333b20bb
GM
8326
8327 /* Get the X image of IMG->pixmap. */
8328 ximg = XGetImage (dpy, img->pixmap, 0, 0, img->width, img->height,
8329 ~0, ZPixmap);
8330
fcf431dc 8331 /* Determine the background color of ximg. If HOW is `(R G B)'
f20a3b7a
MB
8332 take that as color. Otherwise, use the image's background color. */
8333 use_img_background = 1;
7273d100 8334
fcf431dc
GM
8335 if (CONSP (how))
8336 {
cac1daf0 8337 int rgb[3], i;
fcf431dc 8338
cac1daf0 8339 for (i = 0; i < 3 && CONSP (how) && NATNUMP (XCAR (how)); ++i)
fcf431dc
GM
8340 {
8341 rgb[i] = XFASTINT (XCAR (how)) & 0xffff;
8342 how = XCDR (how);
8343 }
8344
8345 if (i == 3 && NILP (how))
8346 {
8347 char color_name[30];
fcf431dc 8348 sprintf (color_name, "#%04x%04x%04x", rgb[0], rgb[1], rgb[2]);
053b3256
GM
8349 bg = x_alloc_image_color (f, img, build_string (color_name), 0);
8350 use_img_background = 0;
fcf431dc
GM
8351 }
8352 }
7273d100 8353
f20a3b7a 8354 if (use_img_background)
43f7c3ea 8355 bg = four_corners_best (ximg, img->width, img->height);
333b20bb
GM
8356
8357 /* Set all bits in mask_img to 1 whose color in ximg is different
8358 from the background color bg. */
8359 for (y = 0; y < img->height; ++y)
8360 for (x = 0; x < img->width; ++x)
8361 XPutPixel (mask_img, x, y, XGetPixel (ximg, x, y) != bg);
8362
f20a3b7a
MB
8363 /* Fill in the background_transparent field while we have the mask handy. */
8364 image_background_transparent (img, f, mask_img);
8365
333b20bb
GM
8366 /* Put mask_img into img->mask. */
8367 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
8368 x_destroy_x_image (mask_img);
8369 XDestroyImage (ximg);
7273d100 8370
333b20bb
GM
8371 return 1;
8372}
8373
8374
8375\f
8376/***********************************************************************
8377 PBM (mono, gray, color)
8378 ***********************************************************************/
8379
8380static int pbm_image_p P_ ((Lisp_Object object));
8381static int pbm_load P_ ((struct frame *f, struct image *img));
63cec32f 8382static int pbm_scan_number P_ ((unsigned char **, unsigned char *));
333b20bb
GM
8383
8384/* The symbol `pbm' identifying images of this type. */
8385
8386Lisp_Object Qpbm;
8387
8388/* Indices of image specification fields in gs_format, below. */
8389
8390enum pbm_keyword_index
8391{
8392 PBM_TYPE,
8393 PBM_FILE,
63cec32f 8394 PBM_DATA,
333b20bb
GM
8395 PBM_ASCENT,
8396 PBM_MARGIN,
8397 PBM_RELIEF,
8398 PBM_ALGORITHM,
8399 PBM_HEURISTIC_MASK,
4a8e312c 8400 PBM_MASK,
be0b1fac
GM
8401 PBM_FOREGROUND,
8402 PBM_BACKGROUND,
333b20bb
GM
8403 PBM_LAST
8404};
8405
8406/* Vector of image_keyword structures describing the format
8407 of valid user-defined image specifications. */
8408
8409static struct image_keyword pbm_format[PBM_LAST] =
8410{
8411 {":type", IMAGE_SYMBOL_VALUE, 1},
63cec32f
GM
8412 {":file", IMAGE_STRING_VALUE, 0},
8413 {":data", IMAGE_STRING_VALUE, 0},
7c7ff7f5 8414 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 8415 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 8416 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 8417 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 8418 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
be0b1fac 8419 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
6f1be3b9
GM
8420 {":foreground", IMAGE_STRING_OR_NIL_VALUE, 0},
8421 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
8422};
8423
8424/* Structure describing the image type `pbm'. */
8425
8426static struct image_type pbm_type =
8427{
8428 &Qpbm,
8429 pbm_image_p,
8430 pbm_load,
8431 x_clear_image,
8432 NULL
8433};
8434
8435
8436/* Return non-zero if OBJECT is a valid PBM image specification. */
8437
8438static int
8439pbm_image_p (object)
8440 Lisp_Object object;
8441{
8442 struct image_keyword fmt[PBM_LAST];
7273d100 8443
333b20bb 8444 bcopy (pbm_format, fmt, sizeof fmt);
7273d100 8445
7c7ff7f5 8446 if (!parse_image_spec (object, fmt, PBM_LAST, Qpbm))
333b20bb 8447 return 0;
63cec32f
GM
8448
8449 /* Must specify either :data or :file. */
8450 return fmt[PBM_DATA].count + fmt[PBM_FILE].count == 1;
333b20bb
GM
8451}
8452
8453
63cec32f
GM
8454/* Scan a decimal number from *S and return it. Advance *S while
8455 reading the number. END is the end of the string. Value is -1 at
8456 end of input. */
333b20bb
GM
8457
8458static int
63cec32f
GM
8459pbm_scan_number (s, end)
8460 unsigned char **s, *end;
333b20bb 8461{
8ec8a5ec 8462 int c = 0, val = -1;
333b20bb 8463
63cec32f 8464 while (*s < end)
333b20bb
GM
8465 {
8466 /* Skip white-space. */
63cec32f 8467 while (*s < end && (c = *(*s)++, isspace (c)))
333b20bb
GM
8468 ;
8469
8470 if (c == '#')
8471 {
8472 /* Skip comment to end of line. */
63cec32f 8473 while (*s < end && (c = *(*s)++, c != '\n'))
333b20bb
GM
8474 ;
8475 }
8476 else if (isdigit (c))
8477 {
8478 /* Read decimal number. */
8479 val = c - '0';
63cec32f 8480 while (*s < end && (c = *(*s)++, isdigit (c)))
333b20bb
GM
8481 val = 10 * val + c - '0';
8482 break;
8483 }
8484 else
8485 break;
8486 }
8487
8488 return val;
8489}
8490
8491
8492/* Load PBM image IMG for use on frame F. */
8493
7273d100 8494static int
333b20bb
GM
8495pbm_load (f, img)
8496 struct frame *f;
8497 struct image *img;
8498{
333b20bb 8499 int raw_p, x, y;
b6d7acec 8500 int width, height, max_color_idx = 0;
333b20bb
GM
8501 XImage *ximg;
8502 Lisp_Object file, specified_file;
8503 enum {PBM_MONO, PBM_GRAY, PBM_COLOR} type;
8504 struct gcpro gcpro1;
63cec32f
GM
8505 unsigned char *contents = NULL;
8506 unsigned char *end, *p;
8507 int size;
333b20bb
GM
8508
8509 specified_file = image_spec_value (img->spec, QCfile, NULL);
63cec32f 8510 file = Qnil;
333b20bb 8511 GCPRO1 (file);
333b20bb 8512
63cec32f 8513 if (STRINGP (specified_file))
333b20bb 8514 {
63cec32f
GM
8515 file = x_find_image_file (specified_file);
8516 if (!STRINGP (file))
8517 {
8518 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8519 UNGCPRO;
8520 return 0;
8521 }
333b20bb 8522
d5db4077 8523 contents = slurp_file (SDATA (file), &size);
63cec32f
GM
8524 if (contents == NULL)
8525 {
8526 image_error ("Error reading `%s'", file, Qnil);
8527 UNGCPRO;
8528 return 0;
8529 }
8530
8531 p = contents;
8532 end = contents + size;
8533 }
8534 else
333b20bb 8535 {
63cec32f
GM
8536 Lisp_Object data;
8537 data = image_spec_value (img->spec, QCdata, NULL);
d5db4077
KR
8538 p = SDATA (data);
8539 end = p + SBYTES (data);
333b20bb
GM
8540 }
8541
63cec32f
GM
8542 /* Check magic number. */
8543 if (end - p < 2 || *p++ != 'P')
333b20bb 8544 {
45158a91 8545 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
63cec32f
GM
8546 error:
8547 xfree (contents);
333b20bb
GM
8548 UNGCPRO;
8549 return 0;
8550 }
8551
63cec32f 8552 switch (*p++)
333b20bb
GM
8553 {
8554 case '1':
8555 raw_p = 0, type = PBM_MONO;
8556 break;
7273d100 8557
333b20bb
GM
8558 case '2':
8559 raw_p = 0, type = PBM_GRAY;
8560 break;
8561
8562 case '3':
8563 raw_p = 0, type = PBM_COLOR;
8564 break;
8565
8566 case '4':
8567 raw_p = 1, type = PBM_MONO;
8568 break;
7273d100 8569
333b20bb
GM
8570 case '5':
8571 raw_p = 1, type = PBM_GRAY;
8572 break;
7273d100 8573
333b20bb
GM
8574 case '6':
8575 raw_p = 1, type = PBM_COLOR;
8576 break;
8577
8578 default:
45158a91 8579 image_error ("Not a PBM image: `%s'", img->spec, Qnil);
63cec32f 8580 goto error;
333b20bb
GM
8581 }
8582
8583 /* Read width, height, maximum color-component. Characters
8584 starting with `#' up to the end of a line are ignored. */
63cec32f
GM
8585 width = pbm_scan_number (&p, end);
8586 height = pbm_scan_number (&p, end);
333b20bb
GM
8587
8588 if (type != PBM_MONO)
8589 {
63cec32f 8590 max_color_idx = pbm_scan_number (&p, end);
333b20bb
GM
8591 if (raw_p && max_color_idx > 255)
8592 max_color_idx = 255;
8593 }
7273d100 8594
63cec32f
GM
8595 if (width < 0
8596 || height < 0
333b20bb 8597 || (type != PBM_MONO && max_color_idx < 0))
63cec32f 8598 goto error;
333b20bb 8599
45158a91 8600 if (!x_create_x_image_and_pixmap (f, width, height, 0,
333b20bb 8601 &ximg, &img->pixmap))
28c7826c 8602 goto error;
7273d100 8603
333b20bb
GM
8604 /* Initialize the color hash table. */
8605 init_color_table ();
8606
8607 if (type == PBM_MONO)
8608 {
8609 int c = 0, g;
be0b1fac
GM
8610 struct image_keyword fmt[PBM_LAST];
8611 unsigned long fg = FRAME_FOREGROUND_PIXEL (f);
8612 unsigned long bg = FRAME_BACKGROUND_PIXEL (f);
8613
8614 /* Parse the image specification. */
8615 bcopy (pbm_format, fmt, sizeof fmt);
8616 parse_image_spec (img->spec, fmt, PBM_LAST, Qpbm);
7273d100 8617
be0b1fac 8618 /* Get foreground and background colors, maybe allocate colors. */
6f1be3b9
GM
8619 if (fmt[PBM_FOREGROUND].count
8620 && STRINGP (fmt[PBM_FOREGROUND].value))
be0b1fac 8621 fg = x_alloc_image_color (f, img, fmt[PBM_FOREGROUND].value, fg);
6f1be3b9
GM
8622 if (fmt[PBM_BACKGROUND].count
8623 && STRINGP (fmt[PBM_BACKGROUND].value))
f20a3b7a
MB
8624 {
8625 bg = x_alloc_image_color (f, img, fmt[PBM_BACKGROUND].value, bg);
8626 img->background = bg;
8627 img->background_valid = 1;
8628 }
7273d100 8629
333b20bb
GM
8630 for (y = 0; y < height; ++y)
8631 for (x = 0; x < width; ++x)
8632 {
8633 if (raw_p)
8634 {
8635 if ((x & 7) == 0)
63cec32f 8636 c = *p++;
333b20bb
GM
8637 g = c & 0x80;
8638 c <<= 1;
8639 }
8640 else
63cec32f 8641 g = pbm_scan_number (&p, end);
333b20bb 8642
be0b1fac 8643 XPutPixel (ximg, x, y, g ? fg : bg);
333b20bb
GM
8644 }
8645 }
8646 else
8647 {
8648 for (y = 0; y < height; ++y)
8649 for (x = 0; x < width; ++x)
8650 {
8651 int r, g, b;
7273d100 8652
333b20bb 8653 if (type == PBM_GRAY)
63cec32f 8654 r = g = b = raw_p ? *p++ : pbm_scan_number (&p, end);
333b20bb
GM
8655 else if (raw_p)
8656 {
63cec32f
GM
8657 r = *p++;
8658 g = *p++;
8659 b = *p++;
333b20bb
GM
8660 }
8661 else
8662 {
63cec32f
GM
8663 r = pbm_scan_number (&p, end);
8664 g = pbm_scan_number (&p, end);
8665 b = pbm_scan_number (&p, end);
333b20bb 8666 }
7273d100 8667
333b20bb
GM
8668 if (r < 0 || g < 0 || b < 0)
8669 {
333b20bb
GM
8670 xfree (ximg->data);
8671 ximg->data = NULL;
8672 XDestroyImage (ximg);
45158a91
GM
8673 image_error ("Invalid pixel value in image `%s'",
8674 img->spec, Qnil);
63cec32f 8675 goto error;
333b20bb 8676 }
7273d100 8677
333b20bb
GM
8678 /* RGB values are now in the range 0..max_color_idx.
8679 Scale this to the range 0..0xffff supported by X. */
8680 r = (double) r * 65535 / max_color_idx;
8681 g = (double) g * 65535 / max_color_idx;
8682 b = (double) b * 65535 / max_color_idx;
8683 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
8684 }
8685 }
7273d100 8686
333b20bb
GM
8687 /* Store in IMG->colors the colors allocated for the image, and
8688 free the color table. */
8689 img->colors = colors_in_color_table (&img->ncolors);
8690 free_color_table ();
f20a3b7a
MB
8691
8692 /* Maybe fill in the background field while we have ximg handy. */
8693 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
8694 IMAGE_BACKGROUND (img, f, ximg);
7273d100 8695
333b20bb
GM
8696 /* Put the image into a pixmap. */
8697 x_put_x_image (f, ximg, img->pixmap, width, height);
8698 x_destroy_x_image (ximg);
7273d100 8699
333b20bb
GM
8700 img->width = width;
8701 img->height = height;
8702
8703 UNGCPRO;
63cec32f 8704 xfree (contents);
333b20bb
GM
8705 return 1;
8706}
8707
8708
8709\f
8710/***********************************************************************
8711 PNG
8712 ***********************************************************************/
8713
8714#if HAVE_PNG
8715
8716#include <png.h>
8717
8718/* Function prototypes. */
8719
8720static int png_image_p P_ ((Lisp_Object object));
8721static int png_load P_ ((struct frame *f, struct image *img));
8722
8723/* The symbol `png' identifying images of this type. */
8724
8725Lisp_Object Qpng;
8726
8727/* Indices of image specification fields in png_format, below. */
8728
8729enum png_keyword_index
8730{
8731 PNG_TYPE,
63448a4d 8732 PNG_DATA,
333b20bb
GM
8733 PNG_FILE,
8734 PNG_ASCENT,
8735 PNG_MARGIN,
8736 PNG_RELIEF,
8737 PNG_ALGORITHM,
8738 PNG_HEURISTIC_MASK,
4a8e312c 8739 PNG_MASK,
f20a3b7a 8740 PNG_BACKGROUND,
333b20bb
GM
8741 PNG_LAST
8742};
8743
8744/* Vector of image_keyword structures describing the format
8745 of valid user-defined image specifications. */
8746
8747static struct image_keyword png_format[PNG_LAST] =
8748{
8749 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 8750 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 8751 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 8752 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 8753 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 8754 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 8755 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 8756 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f2f0a644 8757 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a 8758 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
8759};
8760
06482119 8761/* Structure describing the image type `png'. */
333b20bb
GM
8762
8763static struct image_type png_type =
8764{
8765 &Qpng,
8766 png_image_p,
8767 png_load,
8768 x_clear_image,
8769 NULL
8770};
8771
8772
8773/* Return non-zero if OBJECT is a valid PNG image specification. */
8774
8775static int
8776png_image_p (object)
8777 Lisp_Object object;
8778{
8779 struct image_keyword fmt[PNG_LAST];
8780 bcopy (png_format, fmt, sizeof fmt);
7273d100 8781
7c7ff7f5 8782 if (!parse_image_spec (object, fmt, PNG_LAST, Qpng))
333b20bb 8783 return 0;
63448a4d 8784
63cec32f
GM
8785 /* Must specify either the :data or :file keyword. */
8786 return fmt[PNG_FILE].count + fmt[PNG_DATA].count == 1;
333b20bb
GM
8787}
8788
8789
8790/* Error and warning handlers installed when the PNG library
8791 is initialized. */
8792
8793static void
8794my_png_error (png_ptr, msg)
8795 png_struct *png_ptr;
8796 char *msg;
8797{
8798 xassert (png_ptr != NULL);
8799 image_error ("PNG error: %s", build_string (msg), Qnil);
8800 longjmp (png_ptr->jmpbuf, 1);
8801}
8802
8803
8804static void
8805my_png_warning (png_ptr, msg)
8806 png_struct *png_ptr;
8807 char *msg;
8808{
8809 xassert (png_ptr != NULL);
8810 image_error ("PNG warning: %s", build_string (msg), Qnil);
8811}
8812
5ad6a5fb
GM
8813/* Memory source for PNG decoding. */
8814
63448a4d
WP
8815struct png_memory_storage
8816{
5ad6a5fb
GM
8817 unsigned char *bytes; /* The data */
8818 size_t len; /* How big is it? */
8819 int index; /* Where are we? */
63448a4d
WP
8820};
8821
5ad6a5fb
GM
8822
8823/* Function set as reader function when reading PNG image from memory.
8824 PNG_PTR is a pointer to the PNG control structure. Copy LENGTH
8825 bytes from the input to DATA. */
8826
63448a4d 8827static void
5ad6a5fb
GM
8828png_read_from_memory (png_ptr, data, length)
8829 png_structp png_ptr;
8830 png_bytep data;
8831 png_size_t length;
63448a4d 8832{
5ad6a5fb
GM
8833 struct png_memory_storage *tbr
8834 = (struct png_memory_storage *) png_get_io_ptr (png_ptr);
63448a4d 8835
5ad6a5fb
GM
8836 if (length > tbr->len - tbr->index)
8837 png_error (png_ptr, "Read error");
7273d100 8838
5ad6a5fb
GM
8839 bcopy (tbr->bytes + tbr->index, data, length);
8840 tbr->index = tbr->index + length;
63448a4d 8841}
333b20bb
GM
8842
8843/* Load PNG image IMG for use on frame F. Value is non-zero if
8844 successful. */
8845
8846static int
8847png_load (f, img)
8848 struct frame *f;
8849 struct image *img;
8850{
8851 Lisp_Object file, specified_file;
63448a4d 8852 Lisp_Object specified_data;
b6d7acec 8853 int x, y, i;
333b20bb
GM
8854 XImage *ximg, *mask_img = NULL;
8855 struct gcpro gcpro1;
8856 png_struct *png_ptr = NULL;
8857 png_info *info_ptr = NULL, *end_info = NULL;
8ec8a5ec 8858 FILE *volatile fp = NULL;
333b20bb 8859 png_byte sig[8];
8ec8a5ec
GM
8860 png_byte * volatile pixels = NULL;
8861 png_byte ** volatile rows = NULL;
333b20bb
GM
8862 png_uint_32 width, height;
8863 int bit_depth, color_type, interlace_type;
8864 png_byte channels;
8865 png_uint_32 row_bytes;
8866 int transparent_p;
333b20bb
GM
8867 double screen_gamma, image_gamma;
8868 int intent;
63448a4d 8869 struct png_memory_storage tbr; /* Data to be read */
333b20bb
GM
8870
8871 /* Find out what file to load. */
8872 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 8873 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
8874 file = Qnil;
8875 GCPRO1 (file);
333b20bb 8876
63448a4d 8877 if (NILP (specified_data))
5ad6a5fb
GM
8878 {
8879 file = x_find_image_file (specified_file);
8880 if (!STRINGP (file))
63448a4d 8881 {
45158a91 8882 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5ad6a5fb
GM
8883 UNGCPRO;
8884 return 0;
8885 }
333b20bb 8886
5ad6a5fb 8887 /* Open the image file. */
d5db4077 8888 fp = fopen (SDATA (file), "rb");
5ad6a5fb
GM
8889 if (!fp)
8890 {
45158a91 8891 image_error ("Cannot open image file `%s'", file, Qnil);
5ad6a5fb
GM
8892 UNGCPRO;
8893 fclose (fp);
8894 return 0;
8895 }
63448a4d 8896
5ad6a5fb
GM
8897 /* Check PNG signature. */
8898 if (fread (sig, 1, sizeof sig, fp) != sizeof sig
8899 || !png_check_sig (sig, sizeof sig))
8900 {
45158a91 8901 image_error ("Not a PNG file: `%s'", file, Qnil);
5ad6a5fb
GM
8902 UNGCPRO;
8903 fclose (fp);
8904 return 0;
63448a4d 8905 }
5ad6a5fb 8906 }
63448a4d 8907 else
5ad6a5fb
GM
8908 {
8909 /* Read from memory. */
d5db4077
KR
8910 tbr.bytes = SDATA (specified_data);
8911 tbr.len = SBYTES (specified_data);
5ad6a5fb 8912 tbr.index = 0;
63448a4d 8913
5ad6a5fb
GM
8914 /* Check PNG signature. */
8915 if (tbr.len < sizeof sig
8916 || !png_check_sig (tbr.bytes, sizeof sig))
8917 {
45158a91 8918 image_error ("Not a PNG image: `%s'", img->spec, Qnil);
5ad6a5fb
GM
8919 UNGCPRO;
8920 return 0;
63448a4d 8921 }
333b20bb 8922
5ad6a5fb
GM
8923 /* Need to skip past the signature. */
8924 tbr.bytes += sizeof (sig);
8925 }
8926
333b20bb
GM
8927 /* Initialize read and info structs for PNG lib. */
8928 png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL,
8929 my_png_error, my_png_warning);
8930 if (!png_ptr)
8931 {
63448a4d 8932 if (fp) fclose (fp);
333b20bb
GM
8933 UNGCPRO;
8934 return 0;
8935 }
8936
8937 info_ptr = png_create_info_struct (png_ptr);
8938 if (!info_ptr)
8939 {
8940 png_destroy_read_struct (&png_ptr, NULL, NULL);
63448a4d 8941 if (fp) fclose (fp);
333b20bb
GM
8942 UNGCPRO;
8943 return 0;
8944 }
8945
8946 end_info = png_create_info_struct (png_ptr);
8947 if (!end_info)
8948 {
8949 png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
63448a4d 8950 if (fp) fclose (fp);
333b20bb
GM
8951 UNGCPRO;
8952 return 0;
8953 }
8954
8955 /* Set error jump-back. We come back here when the PNG library
8956 detects an error. */
8957 if (setjmp (png_ptr->jmpbuf))
8958 {
8959 error:
8960 if (png_ptr)
8961 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
8962 xfree (pixels);
8963 xfree (rows);
63448a4d 8964 if (fp) fclose (fp);
333b20bb
GM
8965 UNGCPRO;
8966 return 0;
8967 }
8968
8969 /* Read image info. */
63448a4d 8970 if (!NILP (specified_data))
5ad6a5fb 8971 png_set_read_fn (png_ptr, (void *) &tbr, png_read_from_memory);
63448a4d 8972 else
5ad6a5fb 8973 png_init_io (png_ptr, fp);
63448a4d 8974
333b20bb
GM
8975 png_set_sig_bytes (png_ptr, sizeof sig);
8976 png_read_info (png_ptr, info_ptr);
8977 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
8978 &interlace_type, NULL, NULL);
8979
7273d100 8980 /* If image contains simply transparency data, we prefer to
333b20bb
GM
8981 construct a clipping mask. */
8982 if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
8983 transparent_p = 1;
8984 else
8985 transparent_p = 0;
8986
7273d100 8987 /* This function is easier to write if we only have to handle
333b20bb
GM
8988 one data format: RGB or RGBA with 8 bits per channel. Let's
8989 transform other formats into that format. */
8990
8991 /* Strip more than 8 bits per channel. */
8992 if (bit_depth == 16)
8993 png_set_strip_16 (png_ptr);
8994
8995 /* Expand data to 24 bit RGB, or 8 bit grayscale, with alpha channel
8996 if available. */
8997 png_set_expand (png_ptr);
8998
8999 /* Convert grayscale images to RGB. */
7273d100 9000 if (color_type == PNG_COLOR_TYPE_GRAY
333b20bb
GM
9001 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
9002 png_set_gray_to_rgb (png_ptr);
9003
d4405ed7 9004 screen_gamma = (f->gamma ? 1 / f->gamma / 0.45455 : 2.2);
333b20bb 9005
7273d100 9006#if 0 /* avoid double gamma correction for PNG images */
333b20bb 9007 /* Tell the PNG lib to handle gamma correction for us. */
6c1aa34d 9008#if defined(PNG_READ_sRGB_SUPPORTED) || defined(PNG_WRITE_sRGB_SUPPORTED)
333b20bb 9009 if (png_get_sRGB (png_ptr, info_ptr, &intent))
d4405ed7
RS
9010 /* The libpng documentation says this is right in this case. */
9011 png_set_gamma (png_ptr, screen_gamma, 0.45455);
6c1aa34d
GM
9012 else
9013#endif
9014 if (png_get_gAMA (png_ptr, info_ptr, &image_gamma))
333b20bb
GM
9015 /* Image contains gamma information. */
9016 png_set_gamma (png_ptr, screen_gamma, image_gamma);
9017 else
d4405ed7
RS
9018 /* Use the standard default for the image gamma. */
9019 png_set_gamma (png_ptr, screen_gamma, 0.45455);
7273d100 9020#endif /* if 0 */
333b20bb
GM
9021
9022 /* Handle alpha channel by combining the image with a background
9023 color. Do this only if a real alpha channel is supplied. For
9024 simple transparency, we prefer a clipping mask. */
9025 if (!transparent_p)
9026 {
f20a3b7a
MB
9027 png_color_16 *image_bg;
9028 Lisp_Object specified_bg
9029 = image_spec_value (img->spec, QCbackground, NULL);
9030
f2f0a644 9031 if (STRINGP (specified_bg))
f20a3b7a
MB
9032 /* The user specified `:background', use that. */
9033 {
9034 XColor color;
d5db4077 9035 if (x_defined_color (f, SDATA (specified_bg), &color, 0))
f20a3b7a
MB
9036 {
9037 png_color_16 user_bg;
9038
9039 bzero (&user_bg, sizeof user_bg);
9040 user_bg.red = color.red;
9041 user_bg.green = color.green;
9042 user_bg.blue = color.blue;
333b20bb 9043
f20a3b7a
MB
9044 png_set_background (png_ptr, &user_bg,
9045 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
9046 }
9047 }
9048 else if (png_get_bKGD (png_ptr, info_ptr, &image_bg))
7273d100 9049 /* Image contains a background color with which to
333b20bb 9050 combine the image. */
f20a3b7a 9051 png_set_background (png_ptr, image_bg,
333b20bb
GM
9052 PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
9053 else
9054 {
9055 /* Image does not contain a background color with which
7273d100 9056 to combine the image data via an alpha channel. Use
333b20bb
GM
9057 the frame's background instead. */
9058 XColor color;
9059 Colormap cmap;
9060 png_color_16 frame_background;
9061
9b2956e2 9062 cmap = FRAME_X_COLORMAP (f);
333b20bb 9063 color.pixel = FRAME_BACKGROUND_PIXEL (f);
a31fedb7 9064 x_query_color (f, &color);
333b20bb
GM
9065
9066 bzero (&frame_background, sizeof frame_background);
9067 frame_background.red = color.red;
9068 frame_background.green = color.green;
9069 frame_background.blue = color.blue;
9070
9071 png_set_background (png_ptr, &frame_background,
9072 PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
9073 }
9074 }
9075
9076 /* Update info structure. */
9077 png_read_update_info (png_ptr, info_ptr);
9078
9079 /* Get number of channels. Valid values are 1 for grayscale images
9080 and images with a palette, 2 for grayscale images with transparency
9081 information (alpha channel), 3 for RGB images, and 4 for RGB
9082 images with alpha channel, i.e. RGBA. If conversions above were
9083 sufficient we should only have 3 or 4 channels here. */
9084 channels = png_get_channels (png_ptr, info_ptr);
9085 xassert (channels == 3 || channels == 4);
9086
9087 /* Number of bytes needed for one row of the image. */
9088 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
9089
9090 /* Allocate memory for the image. */
9091 pixels = (png_byte *) xmalloc (row_bytes * height * sizeof *pixels);
9092 rows = (png_byte **) xmalloc (height * sizeof *rows);
9093 for (i = 0; i < height; ++i)
9094 rows[i] = pixels + i * row_bytes;
9095
9096 /* Read the entire image. */
9097 png_read_image (png_ptr, rows);
9098 png_read_end (png_ptr, info_ptr);
5ad6a5fb
GM
9099 if (fp)
9100 {
9101 fclose (fp);
9102 fp = NULL;
9103 }
7273d100 9104
333b20bb 9105 /* Create the X image and pixmap. */
45158a91 9106 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg,
333b20bb 9107 &img->pixmap))
28c7826c 9108 goto error;
7273d100 9109
333b20bb
GM
9110 /* Create an image and pixmap serving as mask if the PNG image
9111 contains an alpha channel. */
9112 if (channels == 4
9113 && !transparent_p
45158a91 9114 && !x_create_x_image_and_pixmap (f, width, height, 1,
333b20bb
GM
9115 &mask_img, &img->mask))
9116 {
9117 x_destroy_x_image (ximg);
9118 XFreePixmap (FRAME_X_DISPLAY (f), img->pixmap);
dd00328a 9119 img->pixmap = None;
333b20bb
GM
9120 goto error;
9121 }
9122
9123 /* Fill the X image and mask from PNG data. */
9124 init_color_table ();
9125
9126 for (y = 0; y < height; ++y)
9127 {
9128 png_byte *p = rows[y];
9129
9130 for (x = 0; x < width; ++x)
9131 {
9132 unsigned r, g, b;
9133
9134 r = *p++ << 8;
9135 g = *p++ << 8;
9136 b = *p++ << 8;
9137 XPutPixel (ximg, x, y, lookup_rgb_color (f, r, g, b));
9138
9139 /* An alpha channel, aka mask channel, associates variable
7273d100
FP
9140 transparency with an image. Where other image formats
9141 support binary transparency---fully transparent or fully
333b20bb
GM
9142 opaque---PNG allows up to 254 levels of partial transparency.
9143 The PNG library implements partial transparency by combining
9144 the image with a specified background color.
9145
9146 I'm not sure how to handle this here nicely: because the
9147 background on which the image is displayed may change, for
7273d100
FP
9148 real alpha channel support, it would be necessary to create
9149 a new image for each possible background.
333b20bb
GM
9150
9151 What I'm doing now is that a mask is created if we have
9152 boolean transparency information. Otherwise I'm using
9153 the frame's background color to combine the image with. */
9154
9155 if (channels == 4)
9156 {
9157 if (mask_img)
9158 XPutPixel (mask_img, x, y, *p > 0);
9159 ++p;
9160 }
9161 }
9162 }
9163
f20a3b7a
MB
9164 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9165 /* Set IMG's background color from the PNG image, unless the user
9166 overrode it. */
9167 {
9168 png_color_16 *bg;
9169 if (png_get_bKGD (png_ptr, info_ptr, &bg))
9170 {
f2f0a644 9171 img->background = lookup_rgb_color (f, bg->red, bg->green, bg->blue);
f20a3b7a
MB
9172 img->background_valid = 1;
9173 }
9174 }
9175
333b20bb
GM
9176 /* Remember colors allocated for this image. */
9177 img->colors = colors_in_color_table (&img->ncolors);
9178 free_color_table ();
9179
9180 /* Clean up. */
9181 png_destroy_read_struct (&png_ptr, &info_ptr, &end_info);
9182 xfree (rows);
9183 xfree (pixels);
9184
9185 img->width = width;
9186 img->height = height;
9187
f20a3b7a
MB
9188 /* Maybe fill in the background field while we have ximg handy. */
9189 IMAGE_BACKGROUND (img, f, ximg);
9190
333b20bb
GM
9191 /* Put the image into the pixmap, then free the X image and its buffer. */
9192 x_put_x_image (f, ximg, img->pixmap, width, height);
9193 x_destroy_x_image (ximg);
9194
9195 /* Same for the mask. */
9196 if (mask_img)
9197 {
f20a3b7a
MB
9198 /* Fill in the background_transparent field while we have the mask
9199 handy. */
9200 image_background_transparent (img, f, mask_img);
9201
333b20bb
GM
9202 x_put_x_image (f, mask_img, img->mask, img->width, img->height);
9203 x_destroy_x_image (mask_img);
9204 }
9205
333b20bb
GM
9206 UNGCPRO;
9207 return 1;
9208}
9209
9210#endif /* HAVE_PNG != 0 */
9211
9212
9213\f
9214/***********************************************************************
9215 JPEG
9216 ***********************************************************************/
9217
9218#if HAVE_JPEG
9219
ba06aba4
GM
9220/* Work around a warning about HAVE_STDLIB_H being redefined in
9221 jconfig.h. */
9222#ifdef HAVE_STDLIB_H
9223#define HAVE_STDLIB_H_1
9224#undef HAVE_STDLIB_H
9225#endif /* HAVE_STLIB_H */
9226
333b20bb
GM
9227#include <jpeglib.h>
9228#include <jerror.h>
9229#include <setjmp.h>
9230
ba06aba4
GM
9231#ifdef HAVE_STLIB_H_1
9232#define HAVE_STDLIB_H 1
9233#endif
9234
333b20bb
GM
9235static int jpeg_image_p P_ ((Lisp_Object object));
9236static int jpeg_load P_ ((struct frame *f, struct image *img));
9237
9238/* The symbol `jpeg' identifying images of this type. */
9239
9240Lisp_Object Qjpeg;
9241
9242/* Indices of image specification fields in gs_format, below. */
9243
9244enum jpeg_keyword_index
9245{
9246 JPEG_TYPE,
8e39770a 9247 JPEG_DATA,
333b20bb
GM
9248 JPEG_FILE,
9249 JPEG_ASCENT,
9250 JPEG_MARGIN,
9251 JPEG_RELIEF,
9252 JPEG_ALGORITHM,
9253 JPEG_HEURISTIC_MASK,
4a8e312c 9254 JPEG_MASK,
f20a3b7a 9255 JPEG_BACKGROUND,
333b20bb
GM
9256 JPEG_LAST
9257};
9258
9259/* Vector of image_keyword structures describing the format
9260 of valid user-defined image specifications. */
9261
9262static struct image_keyword jpeg_format[JPEG_LAST] =
9263{
9264 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9265 {":data", IMAGE_STRING_VALUE, 0},
8e39770a 9266 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 9267 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 9268 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 9269 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 9270 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 9271 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
9272 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9273 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
9274};
9275
9276/* Structure describing the image type `jpeg'. */
9277
9278static struct image_type jpeg_type =
9279{
9280 &Qjpeg,
9281 jpeg_image_p,
9282 jpeg_load,
9283 x_clear_image,
9284 NULL
9285};
9286
9287
9288/* Return non-zero if OBJECT is a valid JPEG image specification. */
9289
9290static int
9291jpeg_image_p (object)
9292 Lisp_Object object;
9293{
9294 struct image_keyword fmt[JPEG_LAST];
7273d100 9295
333b20bb 9296 bcopy (jpeg_format, fmt, sizeof fmt);
7273d100 9297
7c7ff7f5 9298 if (!parse_image_spec (object, fmt, JPEG_LAST, Qjpeg))
333b20bb 9299 return 0;
8e39770a 9300
63cec32f
GM
9301 /* Must specify either the :data or :file keyword. */
9302 return fmt[JPEG_FILE].count + fmt[JPEG_DATA].count == 1;
333b20bb
GM
9303}
9304
8e39770a 9305
333b20bb
GM
9306struct my_jpeg_error_mgr
9307{
9308 struct jpeg_error_mgr pub;
9309 jmp_buf setjmp_buffer;
9310};
9311
e3130015 9312
333b20bb
GM
9313static void
9314my_error_exit (cinfo)
9315 j_common_ptr cinfo;
9316{
9317 struct my_jpeg_error_mgr *mgr = (struct my_jpeg_error_mgr *) cinfo->err;
9318 longjmp (mgr->setjmp_buffer, 1);
9319}
9320
e3130015 9321
8e39770a
GM
9322/* Init source method for JPEG data source manager. Called by
9323 jpeg_read_header() before any data is actually read. See
9324 libjpeg.doc from the JPEG lib distribution. */
9325
9326static void
9327our_init_source (cinfo)
9328 j_decompress_ptr cinfo;
9329{
9330}
9331
9332
9333/* Fill input buffer method for JPEG data source manager. Called
9334 whenever more data is needed. We read the whole image in one step,
9335 so this only adds a fake end of input marker at the end. */
9336
9337static boolean
9338our_fill_input_buffer (cinfo)
9339 j_decompress_ptr cinfo;
9340{
9341 /* Insert a fake EOI marker. */
9342 struct jpeg_source_mgr *src = cinfo->src;
9343 static JOCTET buffer[2];
9344
9345 buffer[0] = (JOCTET) 0xFF;
9346 buffer[1] = (JOCTET) JPEG_EOI;
9347
9348 src->next_input_byte = buffer;
9349 src->bytes_in_buffer = 2;
9350 return TRUE;
9351}
9352
9353
9354/* Method to skip over NUM_BYTES bytes in the image data. CINFO->src
9355 is the JPEG data source manager. */
9356
9357static void
9358our_skip_input_data (cinfo, num_bytes)
9359 j_decompress_ptr cinfo;
9360 long num_bytes;
9361{
9362 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *) cinfo->src;
9363
9364 if (src)
9365 {
9366 if (num_bytes > src->bytes_in_buffer)
5ad6a5fb 9367 ERREXIT (cinfo, JERR_INPUT_EOF);
7273d100 9368
8e39770a
GM
9369 src->bytes_in_buffer -= num_bytes;
9370 src->next_input_byte += num_bytes;
9371 }
9372}
9373
9374
9375/* Method to terminate data source. Called by
9376 jpeg_finish_decompress() after all data has been processed. */
9377
9378static void
9379our_term_source (cinfo)
9380 j_decompress_ptr cinfo;
9381{
9382}
9383
9384
9385/* Set up the JPEG lib for reading an image from DATA which contains
9386 LEN bytes. CINFO is the decompression info structure created for
9387 reading the image. */
9388
9389static void
9390jpeg_memory_src (cinfo, data, len)
9391 j_decompress_ptr cinfo;
9392 JOCTET *data;
9393 unsigned int len;
9394{
9395 struct jpeg_source_mgr *src;
9396
9397 if (cinfo->src == NULL)
9398 {
9399 /* First time for this JPEG object? */
9400 cinfo->src = (struct jpeg_source_mgr *)
9401 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
9402 sizeof (struct jpeg_source_mgr));
9403 src = (struct jpeg_source_mgr *) cinfo->src;
9404 src->next_input_byte = data;
9405 }
7273d100 9406
8e39770a
GM
9407 src = (struct jpeg_source_mgr *) cinfo->src;
9408 src->init_source = our_init_source;
9409 src->fill_input_buffer = our_fill_input_buffer;
9410 src->skip_input_data = our_skip_input_data;
9411 src->resync_to_restart = jpeg_resync_to_restart; /* Use default method. */
9412 src->term_source = our_term_source;
9413 src->bytes_in_buffer = len;
9414 src->next_input_byte = data;
9415}
9416
5ad6a5fb 9417
333b20bb
GM
9418/* Load image IMG for use on frame F. Patterned after example.c
9419 from the JPEG lib. */
9420
7273d100 9421static int
333b20bb
GM
9422jpeg_load (f, img)
9423 struct frame *f;
9424 struct image *img;
9425{
9426 struct jpeg_decompress_struct cinfo;
9427 struct my_jpeg_error_mgr mgr;
9428 Lisp_Object file, specified_file;
8e39770a 9429 Lisp_Object specified_data;
8ec8a5ec 9430 FILE * volatile fp = NULL;
333b20bb
GM
9431 JSAMPARRAY buffer;
9432 int row_stride, x, y;
9433 XImage *ximg = NULL;
b6d7acec 9434 int rc;
333b20bb
GM
9435 unsigned long *colors;
9436 int width, height;
9437 struct gcpro gcpro1;
9438
9439 /* Open the JPEG file. */
9440 specified_file = image_spec_value (img->spec, QCfile, NULL);
8e39770a 9441 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
9442 file = Qnil;
9443 GCPRO1 (file);
8e39770a 9444
8e39770a 9445 if (NILP (specified_data))
333b20bb 9446 {
8e39770a 9447 file = x_find_image_file (specified_file);
8e39770a
GM
9448 if (!STRINGP (file))
9449 {
45158a91 9450 image_error ("Cannot find image file `%s'", specified_file, Qnil);
8e39770a
GM
9451 UNGCPRO;
9452 return 0;
9453 }
7273d100 9454
d5db4077 9455 fp = fopen (SDATA (file), "r");
8e39770a
GM
9456 if (fp == NULL)
9457 {
9458 image_error ("Cannot open `%s'", file, Qnil);
9459 UNGCPRO;
9460 return 0;
9461 }
333b20bb
GM
9462 }
9463
5ad6a5fb
GM
9464 /* Customize libjpeg's error handling to call my_error_exit when an
9465 error is detected. This function will perform a longjmp. */
333b20bb 9466 cinfo.err = jpeg_std_error (&mgr.pub);
14358466 9467 mgr.pub.error_exit = my_error_exit;
7273d100 9468
333b20bb
GM
9469 if ((rc = setjmp (mgr.setjmp_buffer)) != 0)
9470 {
5ad6a5fb
GM
9471 if (rc == 1)
9472 {
9473 /* Called from my_error_exit. Display a JPEG error. */
9474 char buffer[JMSG_LENGTH_MAX];
9475 cinfo.err->format_message ((j_common_ptr) &cinfo, buffer);
45158a91 9476 image_error ("Error reading JPEG image `%s': %s", img->spec,
5ad6a5fb
GM
9477 build_string (buffer));
9478 }
7273d100 9479
333b20bb 9480 /* Close the input file and destroy the JPEG object. */
5ad6a5fb 9481 if (fp)
8ec8a5ec 9482 fclose ((FILE *) fp);
333b20bb
GM
9483 jpeg_destroy_decompress (&cinfo);
9484
5ad6a5fb
GM
9485 /* If we already have an XImage, free that. */
9486 x_destroy_x_image (ximg);
333b20bb 9487
5ad6a5fb
GM
9488 /* Free pixmap and colors. */
9489 x_clear_image (f, img);
7273d100 9490
5ad6a5fb
GM
9491 UNGCPRO;
9492 return 0;
333b20bb
GM
9493 }
9494
9495 /* Create the JPEG decompression object. Let it read from fp.
63448a4d 9496 Read the JPEG image header. */
333b20bb 9497 jpeg_create_decompress (&cinfo);
8e39770a
GM
9498
9499 if (NILP (specified_data))
8ec8a5ec 9500 jpeg_stdio_src (&cinfo, (FILE *) fp);
8e39770a 9501 else
d5db4077
KR
9502 jpeg_memory_src (&cinfo, SDATA (specified_data),
9503 SBYTES (specified_data));
63448a4d 9504
333b20bb
GM
9505 jpeg_read_header (&cinfo, TRUE);
9506
9507 /* Customize decompression so that color quantization will be used.
63448a4d 9508 Start decompression. */
333b20bb
GM
9509 cinfo.quantize_colors = TRUE;
9510 jpeg_start_decompress (&cinfo);
9511 width = img->width = cinfo.output_width;
9512 height = img->height = cinfo.output_height;
9513
333b20bb 9514 /* Create X image and pixmap. */
45158a91 9515 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
28c7826c 9516 longjmp (mgr.setjmp_buffer, 2);
333b20bb
GM
9517
9518 /* Allocate colors. When color quantization is used,
5ad6a5fb
GM
9519 cinfo.actual_number_of_colors has been set with the number of
9520 colors generated, and cinfo.colormap is a two-dimensional array
9521 of color indices in the range 0..cinfo.actual_number_of_colors.
9522 No more than 255 colors will be generated. */
333b20bb 9523 {
5ad6a5fb
GM
9524 int i, ir, ig, ib;
9525
9526 if (cinfo.out_color_components > 2)
9527 ir = 0, ig = 1, ib = 2;
9528 else if (cinfo.out_color_components > 1)
9529 ir = 0, ig = 1, ib = 0;
9530 else
9531 ir = 0, ig = 0, ib = 0;
9532
9533 /* Use the color table mechanism because it handles colors that
9534 cannot be allocated nicely. Such colors will be replaced with
9535 a default color, and we don't have to care about which colors
9536 can be freed safely, and which can't. */
9537 init_color_table ();
9538 colors = (unsigned long *) alloca (cinfo.actual_number_of_colors
9539 * sizeof *colors);
7273d100 9540
5ad6a5fb
GM
9541 for (i = 0; i < cinfo.actual_number_of_colors; ++i)
9542 {
9543 /* Multiply RGB values with 255 because X expects RGB values
9544 in the range 0..0xffff. */
9545 int r = cinfo.colormap[ir][i] << 8;
9546 int g = cinfo.colormap[ig][i] << 8;
9547 int b = cinfo.colormap[ib][i] << 8;
9548 colors[i] = lookup_rgb_color (f, r, g, b);
9549 }
333b20bb 9550
5ad6a5fb
GM
9551 /* Remember those colors actually allocated. */
9552 img->colors = colors_in_color_table (&img->ncolors);
9553 free_color_table ();
333b20bb
GM
9554 }
9555
9556 /* Read pixels. */
9557 row_stride = width * cinfo.output_components;
9558 buffer = cinfo.mem->alloc_sarray ((j_common_ptr) &cinfo, JPOOL_IMAGE,
5ad6a5fb 9559 row_stride, 1);
333b20bb
GM
9560 for (y = 0; y < height; ++y)
9561 {
5ad6a5fb
GM
9562 jpeg_read_scanlines (&cinfo, buffer, 1);
9563 for (x = 0; x < cinfo.output_width; ++x)
9564 XPutPixel (ximg, x, y, colors[buffer[0][x]]);
333b20bb
GM
9565 }
9566
9567 /* Clean up. */
9568 jpeg_finish_decompress (&cinfo);
9569 jpeg_destroy_decompress (&cinfo);
5ad6a5fb 9570 if (fp)
8ec8a5ec 9571 fclose ((FILE *) fp);
f20a3b7a
MB
9572
9573 /* Maybe fill in the background field while we have ximg handy. */
9574 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9575 IMAGE_BACKGROUND (img, f, ximg);
7273d100 9576
333b20bb
GM
9577 /* Put the image into the pixmap. */
9578 x_put_x_image (f, ximg, img->pixmap, width, height);
9579 x_destroy_x_image (ximg);
333b20bb
GM
9580 UNGCPRO;
9581 return 1;
9582}
9583
9584#endif /* HAVE_JPEG */
9585
9586
9587\f
9588/***********************************************************************
9589 TIFF
9590 ***********************************************************************/
9591
9592#if HAVE_TIFF
9593
cf4790ad 9594#include <tiffio.h>
333b20bb
GM
9595
9596static int tiff_image_p P_ ((Lisp_Object object));
9597static int tiff_load P_ ((struct frame *f, struct image *img));
9598
9599/* The symbol `tiff' identifying images of this type. */
9600
9601Lisp_Object Qtiff;
9602
9603/* Indices of image specification fields in tiff_format, below. */
9604
9605enum tiff_keyword_index
9606{
9607 TIFF_TYPE,
63448a4d 9608 TIFF_DATA,
333b20bb
GM
9609 TIFF_FILE,
9610 TIFF_ASCENT,
9611 TIFF_MARGIN,
9612 TIFF_RELIEF,
9613 TIFF_ALGORITHM,
9614 TIFF_HEURISTIC_MASK,
4a8e312c 9615 TIFF_MASK,
f20a3b7a 9616 TIFF_BACKGROUND,
333b20bb
GM
9617 TIFF_LAST
9618};
9619
9620/* Vector of image_keyword structures describing the format
9621 of valid user-defined image specifications. */
9622
9623static struct image_keyword tiff_format[TIFF_LAST] =
9624{
9625 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9626 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 9627 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 9628 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 9629 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 9630 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 9631 {":conversions", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 9632 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
9633 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
9634 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
9635};
9636
9637/* Structure describing the image type `tiff'. */
9638
9639static struct image_type tiff_type =
9640{
9641 &Qtiff,
9642 tiff_image_p,
9643 tiff_load,
9644 x_clear_image,
9645 NULL
9646};
9647
9648
9649/* Return non-zero if OBJECT is a valid TIFF image specification. */
9650
9651static int
9652tiff_image_p (object)
9653 Lisp_Object object;
9654{
9655 struct image_keyword fmt[TIFF_LAST];
9656 bcopy (tiff_format, fmt, sizeof fmt);
7273d100 9657
7c7ff7f5 9658 if (!parse_image_spec (object, fmt, TIFF_LAST, Qtiff))
333b20bb 9659 return 0;
7273d100 9660
63cec32f
GM
9661 /* Must specify either the :data or :file keyword. */
9662 return fmt[TIFF_FILE].count + fmt[TIFF_DATA].count == 1;
333b20bb
GM
9663}
9664
5ad6a5fb
GM
9665
9666/* Reading from a memory buffer for TIFF images Based on the PNG
9667 memory source, but we have to provide a lot of extra functions.
9668 Blah.
63448a4d
WP
9669
9670 We really only need to implement read and seek, but I am not
9671 convinced that the TIFF library is smart enough not to destroy
9672 itself if we only hand it the function pointers we need to
5ad6a5fb
GM
9673 override. */
9674
9675typedef struct
9676{
63448a4d
WP
9677 unsigned char *bytes;
9678 size_t len;
9679 int index;
5ad6a5fb
GM
9680}
9681tiff_memory_source;
63448a4d 9682
e3130015 9683
5ad6a5fb
GM
9684static size_t
9685tiff_read_from_memory (data, buf, size)
9686 thandle_t data;
9687 tdata_t buf;
9688 tsize_t size;
63448a4d 9689{
5ad6a5fb 9690 tiff_memory_source *src = (tiff_memory_source *) data;
63448a4d
WP
9691
9692 if (size > src->len - src->index)
5ad6a5fb
GM
9693 return (size_t) -1;
9694 bcopy (src->bytes + src->index, buf, size);
63448a4d
WP
9695 src->index += size;
9696 return size;
9697}
9698
e3130015 9699
5ad6a5fb
GM
9700static size_t
9701tiff_write_from_memory (data, buf, size)
9702 thandle_t data;
9703 tdata_t buf;
9704 tsize_t size;
63448a4d
WP
9705{
9706 return (size_t) -1;
9707}
9708
e3130015 9709
5ad6a5fb
GM
9710static toff_t
9711tiff_seek_in_memory (data, off, whence)
9712 thandle_t data;
9713 toff_t off;
9714 int whence;
63448a4d 9715{
5ad6a5fb 9716 tiff_memory_source *src = (tiff_memory_source *) data;
63448a4d
WP
9717 int idx;
9718
9719 switch (whence)
5ad6a5fb
GM
9720 {
9721 case SEEK_SET: /* Go from beginning of source. */
9722 idx = off;
9723 break;
7273d100 9724
5ad6a5fb
GM
9725 case SEEK_END: /* Go from end of source. */
9726 idx = src->len + off;
9727 break;
7273d100 9728
5ad6a5fb
GM
9729 case SEEK_CUR: /* Go from current position. */
9730 idx = src->index + off;
9731 break;
7273d100 9732
5ad6a5fb
GM
9733 default: /* Invalid `whence'. */
9734 return -1;
9735 }
7273d100 9736
5ad6a5fb
GM
9737 if (idx > src->len || idx < 0)
9738 return -1;
7273d100 9739
63448a4d
WP
9740 src->index = idx;
9741 return src->index;
9742}
9743
e3130015 9744
5ad6a5fb
GM
9745static int
9746tiff_close_memory (data)
9747 thandle_t data;
63448a4d
WP
9748{
9749 /* NOOP */
5ad6a5fb 9750 return 0;
63448a4d
WP
9751}
9752
e3130015 9753
5ad6a5fb
GM
9754static int
9755tiff_mmap_memory (data, pbase, psize)
9756 thandle_t data;
9757 tdata_t *pbase;
9758 toff_t *psize;
63448a4d
WP
9759{
9760 /* It is already _IN_ memory. */
5ad6a5fb 9761 return 0;
63448a4d
WP
9762}
9763
e3130015 9764
5ad6a5fb
GM
9765static void
9766tiff_unmap_memory (data, base, size)
9767 thandle_t data;
9768 tdata_t base;
9769 toff_t size;
63448a4d
WP
9770{
9771 /* We don't need to do this. */
63448a4d
WP
9772}
9773
e3130015 9774
5ad6a5fb
GM
9775static toff_t
9776tiff_size_of_memory (data)
9777 thandle_t data;
63448a4d 9778{
5ad6a5fb 9779 return ((tiff_memory_source *) data)->len;
63448a4d 9780}
333b20bb 9781
e3130015 9782
c6892044
GM
9783static void
9784tiff_error_handler (title, format, ap)
9785 const char *title, *format;
9786 va_list ap;
9787{
9788 char buf[512];
9789 int len;
7273d100 9790
c6892044
GM
9791 len = sprintf (buf, "TIFF error: %s ", title);
9792 vsprintf (buf + len, format, ap);
9793 add_to_log (buf, Qnil, Qnil);
9794}
9795
9796
9797static void
9798tiff_warning_handler (title, format, ap)
9799 const char *title, *format;
9800 va_list ap;
9801{
9802 char buf[512];
9803 int len;
7273d100 9804
c6892044
GM
9805 len = sprintf (buf, "TIFF warning: %s ", title);
9806 vsprintf (buf + len, format, ap);
9807 add_to_log (buf, Qnil, Qnil);
9808}
9809
9810
333b20bb
GM
9811/* Load TIFF image IMG for use on frame F. Value is non-zero if
9812 successful. */
9813
9814static int
9815tiff_load (f, img)
9816 struct frame *f;
9817 struct image *img;
9818{
9819 Lisp_Object file, specified_file;
63448a4d 9820 Lisp_Object specified_data;
333b20bb
GM
9821 TIFF *tiff;
9822 int width, height, x, y;
9823 uint32 *buf;
9824 int rc;
9825 XImage *ximg;
9826 struct gcpro gcpro1;
63448a4d 9827 tiff_memory_source memsrc;
333b20bb
GM
9828
9829 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 9830 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
9831 file = Qnil;
9832 GCPRO1 (file);
63448a4d 9833
c6892044
GM
9834 TIFFSetErrorHandler (tiff_error_handler);
9835 TIFFSetWarningHandler (tiff_warning_handler);
9836
63448a4d 9837 if (NILP (specified_data))
5ad6a5fb
GM
9838 {
9839 /* Read from a file */
9840 file = x_find_image_file (specified_file);
9841 if (!STRINGP (file))
63448a4d 9842 {
45158a91 9843 image_error ("Cannot find image file `%s'", file, Qnil);
5ad6a5fb
GM
9844 UNGCPRO;
9845 return 0;
9846 }
7273d100 9847
5ad6a5fb 9848 /* Try to open the image file. */
d5db4077 9849 tiff = TIFFOpen (SDATA (file), "r");
5ad6a5fb
GM
9850 if (tiff == NULL)
9851 {
9852 image_error ("Cannot open `%s'", file, Qnil);
9853 UNGCPRO;
9854 return 0;
63448a4d 9855 }
5ad6a5fb 9856 }
63448a4d 9857 else
5ad6a5fb
GM
9858 {
9859 /* Memory source! */
d5db4077
KR
9860 memsrc.bytes = SDATA (specified_data);
9861 memsrc.len = SBYTES (specified_data);
5ad6a5fb
GM
9862 memsrc.index = 0;
9863
9864 tiff = TIFFClientOpen ("memory_source", "r", &memsrc,
9865 (TIFFReadWriteProc) tiff_read_from_memory,
9866 (TIFFReadWriteProc) tiff_write_from_memory,
9867 tiff_seek_in_memory,
9868 tiff_close_memory,
9869 tiff_size_of_memory,
9870 tiff_mmap_memory,
9871 tiff_unmap_memory);
9872
9873 if (!tiff)
63448a4d 9874 {
45158a91 9875 image_error ("Cannot open memory source for `%s'", img->spec, Qnil);
5ad6a5fb
GM
9876 UNGCPRO;
9877 return 0;
63448a4d 9878 }
5ad6a5fb 9879 }
333b20bb
GM
9880
9881 /* Get width and height of the image, and allocate a raster buffer
9882 of width x height 32-bit values. */
9883 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width);
9884 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height);
9885 buf = (uint32 *) xmalloc (width * height * sizeof *buf);
7273d100 9886
333b20bb
GM
9887 rc = TIFFReadRGBAImage (tiff, width, height, buf, 0);
9888 TIFFClose (tiff);
9889 if (!rc)
9890 {
45158a91 9891 image_error ("Error reading TIFF image `%s'", img->spec, Qnil);
333b20bb
GM
9892 xfree (buf);
9893 UNGCPRO;
9894 return 0;
9895 }
9896
333b20bb 9897 /* Create the X image and pixmap. */
45158a91 9898 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
333b20bb 9899 {
333b20bb
GM
9900 xfree (buf);
9901 UNGCPRO;
9902 return 0;
9903 }
9904
9905 /* Initialize the color table. */
9906 init_color_table ();
9907
9908 /* Process the pixel raster. Origin is in the lower-left corner. */
9909 for (y = 0; y < height; ++y)
9910 {
9911 uint32 *row = buf + y * width;
7273d100 9912
333b20bb
GM
9913 for (x = 0; x < width; ++x)
9914 {
9915 uint32 abgr = row[x];
9916 int r = TIFFGetR (abgr) << 8;
9917 int g = TIFFGetG (abgr) << 8;
9918 int b = TIFFGetB (abgr) << 8;
7273d100 9919 XPutPixel (ximg, x, height - 1 - y, lookup_rgb_color (f, r, g, b));
333b20bb
GM
9920 }
9921 }
9922
9923 /* Remember the colors allocated for the image. Free the color table. */
9924 img->colors = colors_in_color_table (&img->ncolors);
9925 free_color_table ();
7273d100 9926
f20a3b7a
MB
9927 img->width = width;
9928 img->height = height;
9929
9930 /* Maybe fill in the background field while we have ximg handy. */
9931 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
9932 IMAGE_BACKGROUND (img, f, ximg);
333b20bb
GM
9933
9934 /* Put the image into the pixmap, then free the X image and its buffer. */
9935 x_put_x_image (f, ximg, img->pixmap, width, height);
9936 x_destroy_x_image (ximg);
9937 xfree (buf);
333b20bb
GM
9938
9939 UNGCPRO;
9940 return 1;
9941}
9942
9943#endif /* HAVE_TIFF != 0 */
9944
9945
9946\f
9947/***********************************************************************
9948 GIF
9949 ***********************************************************************/
9950
9951#if HAVE_GIF
9952
9953#include <gif_lib.h>
9954
9955static int gif_image_p P_ ((Lisp_Object object));
9956static int gif_load P_ ((struct frame *f, struct image *img));
9957
9958/* The symbol `gif' identifying images of this type. */
9959
9960Lisp_Object Qgif;
9961
9962/* Indices of image specification fields in gif_format, below. */
9963
9964enum gif_keyword_index
9965{
9966 GIF_TYPE,
63448a4d 9967 GIF_DATA,
333b20bb
GM
9968 GIF_FILE,
9969 GIF_ASCENT,
9970 GIF_MARGIN,
9971 GIF_RELIEF,
9972 GIF_ALGORITHM,
9973 GIF_HEURISTIC_MASK,
4a8e312c 9974 GIF_MASK,
333b20bb 9975 GIF_IMAGE,
f20a3b7a 9976 GIF_BACKGROUND,
333b20bb
GM
9977 GIF_LAST
9978};
9979
9980/* Vector of image_keyword structures describing the format
9981 of valid user-defined image specifications. */
9982
9983static struct image_keyword gif_format[GIF_LAST] =
9984{
9985 {":type", IMAGE_SYMBOL_VALUE, 1},
5ad6a5fb 9986 {":data", IMAGE_STRING_VALUE, 0},
63448a4d 9987 {":file", IMAGE_STRING_VALUE, 0},
7c7ff7f5 9988 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 9989 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 9990 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 9991 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
333b20bb 9992 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 9993 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f2f0a644 9994 {":image", IMAGE_NON_NEGATIVE_INTEGER_VALUE, 0},
f20a3b7a 9995 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
9996};
9997
9998/* Structure describing the image type `gif'. */
9999
10000static struct image_type gif_type =
10001{
10002 &Qgif,
10003 gif_image_p,
10004 gif_load,
10005 x_clear_image,
10006 NULL
10007};
10008
e3130015 10009
333b20bb
GM
10010/* Return non-zero if OBJECT is a valid GIF image specification. */
10011
10012static int
10013gif_image_p (object)
10014 Lisp_Object object;
10015{
10016 struct image_keyword fmt[GIF_LAST];
10017 bcopy (gif_format, fmt, sizeof fmt);
7273d100 10018
7c7ff7f5 10019 if (!parse_image_spec (object, fmt, GIF_LAST, Qgif))
333b20bb 10020 return 0;
7273d100 10021
63cec32f
GM
10022 /* Must specify either the :data or :file keyword. */
10023 return fmt[GIF_FILE].count + fmt[GIF_DATA].count == 1;
333b20bb
GM
10024}
10025
e3130015 10026
63448a4d
WP
10027/* Reading a GIF image from memory
10028 Based on the PNG memory stuff to a certain extent. */
10029
5ad6a5fb
GM
10030typedef struct
10031{
63448a4d
WP
10032 unsigned char *bytes;
10033 size_t len;
10034 int index;
5ad6a5fb
GM
10035}
10036gif_memory_source;
63448a4d 10037
e3130015 10038
f036834a
GM
10039/* Make the current memory source available to gif_read_from_memory.
10040 It's done this way because not all versions of libungif support
10041 a UserData field in the GifFileType structure. */
10042static gif_memory_source *current_gif_memory_src;
10043
5ad6a5fb
GM
10044static int
10045gif_read_from_memory (file, buf, len)
10046 GifFileType *file;
10047 GifByteType *buf;
10048 int len;
63448a4d 10049{
f036834a 10050 gif_memory_source *src = current_gif_memory_src;
63448a4d 10051
5ad6a5fb
GM
10052 if (len > src->len - src->index)
10053 return -1;
63448a4d 10054
5ad6a5fb 10055 bcopy (src->bytes + src->index, buf, len);
63448a4d
WP
10056 src->index += len;
10057 return len;
10058}
333b20bb 10059
5ad6a5fb 10060
333b20bb
GM
10061/* Load GIF image IMG for use on frame F. Value is non-zero if
10062 successful. */
10063
10064static int
10065gif_load (f, img)
10066 struct frame *f;
10067 struct image *img;
10068{
10069 Lisp_Object file, specified_file;
63448a4d 10070 Lisp_Object specified_data;
333b20bb
GM
10071 int rc, width, height, x, y, i;
10072 XImage *ximg;
10073 ColorMapObject *gif_color_map;
10074 unsigned long pixel_colors[256];
10075 GifFileType *gif;
10076 struct gcpro gcpro1;
10077 Lisp_Object image;
10078 int ino, image_left, image_top, image_width, image_height;
63448a4d 10079 gif_memory_source memsrc;
9b784e96 10080 unsigned char *raster;
333b20bb
GM
10081
10082 specified_file = image_spec_value (img->spec, QCfile, NULL);
63448a4d 10083 specified_data = image_spec_value (img->spec, QCdata, NULL);
5ad6a5fb
GM
10084 file = Qnil;
10085 GCPRO1 (file);
63448a4d
WP
10086
10087 if (NILP (specified_data))
5ad6a5fb
GM
10088 {
10089 file = x_find_image_file (specified_file);
10090 if (!STRINGP (file))
63448a4d 10091 {
45158a91 10092 image_error ("Cannot find image file `%s'", specified_file, Qnil);
5ad6a5fb
GM
10093 UNGCPRO;
10094 return 0;
10095 }
7273d100 10096
5ad6a5fb 10097 /* Open the GIF file. */
d5db4077 10098 gif = DGifOpenFileName (SDATA (file));
5ad6a5fb
GM
10099 if (gif == NULL)
10100 {
10101 image_error ("Cannot open `%s'", file, Qnil);
10102 UNGCPRO;
10103 return 0;
63448a4d 10104 }
5ad6a5fb 10105 }
63448a4d 10106 else
5ad6a5fb
GM
10107 {
10108 /* Read from memory! */
f036834a 10109 current_gif_memory_src = &memsrc;
d5db4077
KR
10110 memsrc.bytes = SDATA (specified_data);
10111 memsrc.len = SBYTES (specified_data);
5ad6a5fb 10112 memsrc.index = 0;
63448a4d 10113
5ad6a5fb
GM
10114 gif = DGifOpen(&memsrc, gif_read_from_memory);
10115 if (!gif)
10116 {
45158a91 10117 image_error ("Cannot open memory source `%s'", img->spec, Qnil);
5ad6a5fb
GM
10118 UNGCPRO;
10119 return 0;
63448a4d 10120 }
5ad6a5fb 10121 }
333b20bb
GM
10122
10123 /* Read entire contents. */
10124 rc = DGifSlurp (gif);
10125 if (rc == GIF_ERROR)
10126 {
45158a91 10127 image_error ("Error reading `%s'", img->spec, Qnil);
333b20bb
GM
10128 DGifCloseFile (gif);
10129 UNGCPRO;
10130 return 0;
10131 }
10132
3ccff1e3 10133 image = image_spec_value (img->spec, QCindex, NULL);
333b20bb
GM
10134 ino = INTEGERP (image) ? XFASTINT (image) : 0;
10135 if (ino >= gif->ImageCount)
10136 {
45158a91
GM
10137 image_error ("Invalid image number `%s' in image `%s'",
10138 image, img->spec);
333b20bb
GM
10139 DGifCloseFile (gif);
10140 UNGCPRO;
10141 return 0;
10142 }
10143
c7f07c4c
PJ
10144 width = img->width = max (gif->SWidth, gif->Image.Left + gif->Image.Width);
10145 height = img->height = max (gif->SHeight, gif->Image.Top + gif->Image.Height);
333b20bb 10146
333b20bb 10147 /* Create the X image and pixmap. */
45158a91 10148 if (!x_create_x_image_and_pixmap (f, width, height, 0, &ximg, &img->pixmap))
333b20bb 10149 {
333b20bb
GM
10150 DGifCloseFile (gif);
10151 UNGCPRO;
10152 return 0;
10153 }
7273d100 10154
333b20bb
GM
10155 /* Allocate colors. */
10156 gif_color_map = gif->SavedImages[ino].ImageDesc.ColorMap;
10157 if (!gif_color_map)
10158 gif_color_map = gif->SColorMap;
10159 init_color_table ();
10160 bzero (pixel_colors, sizeof pixel_colors);
7273d100 10161
333b20bb
GM
10162 for (i = 0; i < gif_color_map->ColorCount; ++i)
10163 {
10164 int r = gif_color_map->Colors[i].Red << 8;
10165 int g = gif_color_map->Colors[i].Green << 8;
10166 int b = gif_color_map->Colors[i].Blue << 8;
10167 pixel_colors[i] = lookup_rgb_color (f, r, g, b);
10168 }
10169
10170 img->colors = colors_in_color_table (&img->ncolors);
10171 free_color_table ();
10172
10173 /* Clear the part of the screen image that are not covered by
7273d100 10174 the image from the GIF file. Full animated GIF support
333b20bb
GM
10175 requires more than can be done here (see the gif89 spec,
10176 disposal methods). Let's simply assume that the part
10177 not covered by a sub-image is in the frame's background color. */
10178 image_top = gif->SavedImages[ino].ImageDesc.Top;
10179 image_left = gif->SavedImages[ino].ImageDesc.Left;
10180 image_width = gif->SavedImages[ino].ImageDesc.Width;
10181 image_height = gif->SavedImages[ino].ImageDesc.Height;
10182
10183 for (y = 0; y < image_top; ++y)
10184 for (x = 0; x < width; ++x)
10185 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10186
10187 for (y = image_top + image_height; y < height; ++y)
10188 for (x = 0; x < width; ++x)
10189 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10190
10191 for (y = image_top; y < image_top + image_height; ++y)
10192 {
10193 for (x = 0; x < image_left; ++x)
10194 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10195 for (x = image_left + image_width; x < width; ++x)
10196 XPutPixel (ximg, x, y, FRAME_BACKGROUND_PIXEL (f));
10197 }
10198
9b784e96
GM
10199 /* Read the GIF image into the X image. We use a local variable
10200 `raster' here because RasterBits below is a char *, and invites
10201 problems with bytes >= 0x80. */
10202 raster = (unsigned char *) gif->SavedImages[ino].RasterBits;
7273d100 10203
333b20bb
GM
10204 if (gif->SavedImages[ino].ImageDesc.Interlace)
10205 {
10206 static int interlace_start[] = {0, 4, 2, 1};
10207 static int interlace_increment[] = {8, 8, 4, 2};
9b207e8e 10208 int pass;
06482119
GM
10209 int row = interlace_start[0];
10210
10211 pass = 0;
333b20bb 10212
06482119 10213 for (y = 0; y < image_height; y++)
333b20bb 10214 {
06482119
GM
10215 if (row >= image_height)
10216 {
10217 row = interlace_start[++pass];
10218 while (row >= image_height)
10219 row = interlace_start[++pass];
10220 }
7273d100 10221
06482119
GM
10222 for (x = 0; x < image_width; x++)
10223 {
9b784e96 10224 int i = raster[(y * image_width) + x];
06482119
GM
10225 XPutPixel (ximg, x + image_left, row + image_top,
10226 pixel_colors[i]);
10227 }
7273d100 10228
06482119 10229 row += interlace_increment[pass];
333b20bb
GM
10230 }
10231 }
10232 else
10233 {
10234 for (y = 0; y < image_height; ++y)
10235 for (x = 0; x < image_width; ++x)
10236 {
9b784e96 10237 int i = raster[y * image_width + x];
333b20bb
GM
10238 XPutPixel (ximg, x + image_left, y + image_top, pixel_colors[i]);
10239 }
10240 }
7273d100 10241
333b20bb 10242 DGifCloseFile (gif);
f20a3b7a
MB
10243
10244 /* Maybe fill in the background field while we have ximg handy. */
10245 if (NILP (image_spec_value (img->spec, QCbackground, NULL)))
10246 IMAGE_BACKGROUND (img, f, ximg);
7273d100 10247
333b20bb
GM
10248 /* Put the image into the pixmap, then free the X image and its buffer. */
10249 x_put_x_image (f, ximg, img->pixmap, width, height);
10250 x_destroy_x_image (ximg);
7273d100 10251
333b20bb
GM
10252 UNGCPRO;
10253 return 1;
10254}
10255
10256#endif /* HAVE_GIF != 0 */
10257
10258
10259\f
10260/***********************************************************************
10261 Ghostscript
10262 ***********************************************************************/
10263
10264static int gs_image_p P_ ((Lisp_Object object));
10265static int gs_load P_ ((struct frame *f, struct image *img));
10266static void gs_clear_image P_ ((struct frame *f, struct image *img));
10267
fcf431dc 10268/* The symbol `postscript' identifying images of this type. */
333b20bb 10269
fcf431dc 10270Lisp_Object Qpostscript;
333b20bb
GM
10271
10272/* Keyword symbols. */
10273
10274Lisp_Object QCloader, QCbounding_box, QCpt_width, QCpt_height;
10275
10276/* Indices of image specification fields in gs_format, below. */
10277
10278enum gs_keyword_index
10279{
10280 GS_TYPE,
10281 GS_PT_WIDTH,
10282 GS_PT_HEIGHT,
10283 GS_FILE,
10284 GS_LOADER,
10285 GS_BOUNDING_BOX,
10286 GS_ASCENT,
10287 GS_MARGIN,
10288 GS_RELIEF,
10289 GS_ALGORITHM,
10290 GS_HEURISTIC_MASK,
4a8e312c 10291 GS_MASK,
f20a3b7a 10292 GS_BACKGROUND,
333b20bb
GM
10293 GS_LAST
10294};
10295
10296/* Vector of image_keyword structures describing the format
10297 of valid user-defined image specifications. */
10298
10299static struct image_keyword gs_format[GS_LAST] =
10300{
10301 {":type", IMAGE_SYMBOL_VALUE, 1},
10302 {":pt-width", IMAGE_POSITIVE_INTEGER_VALUE, 1},
10303 {":pt-height", IMAGE_POSITIVE_INTEGER_VALUE, 1},
10304 {":file", IMAGE_STRING_VALUE, 1},
10305 {":loader", IMAGE_FUNCTION_VALUE, 0},
10306 {":bounding-box", IMAGE_DONT_CHECK_VALUE_TYPE, 1},
7c7ff7f5 10307 {":ascent", IMAGE_ASCENT_VALUE, 0},
3ed61e75 10308 {":margin", IMAGE_POSITIVE_INTEGER_VALUE_OR_PAIR, 0},
333b20bb 10309 {":relief", IMAGE_INTEGER_VALUE, 0},
d2dc8167 10310 {":conversion", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
4a8e312c 10311 {":heuristic-mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
f20a3b7a
MB
10312 {":mask", IMAGE_DONT_CHECK_VALUE_TYPE, 0},
10313 {":background", IMAGE_STRING_OR_NIL_VALUE, 0}
333b20bb
GM
10314};
10315
10316/* Structure describing the image type `ghostscript'. */
10317
10318static struct image_type gs_type =
10319{
fcf431dc 10320 &Qpostscript,
333b20bb
GM
10321 gs_image_p,
10322 gs_load,
10323 gs_clear_image,
10324 NULL
10325};
10326
10327
10328/* Free X resources of Ghostscript image IMG which is used on frame F. */
10329
10330static void
10331gs_clear_image (f, img)
10332 struct frame *f;
10333 struct image *img;
10334{
10335 /* IMG->data.ptr_val may contain a recorded colormap. */
10336 xfree (img->data.ptr_val);
10337 x_clear_image (f, img);
10338}
10339
10340
10341/* Return non-zero if OBJECT is a valid Ghostscript image
10342 specification. */
10343
10344static int
10345gs_image_p (object)
10346 Lisp_Object object;
10347{
10348 struct image_keyword fmt[GS_LAST];
10349 Lisp_Object tem;
10350 int i;
7273d100 10351
333b20bb 10352 bcopy (gs_format, fmt, sizeof fmt);
7273d100 10353
7c7ff7f5 10354 if (!parse_image_spec (object, fmt, GS_LAST, Qpostscript))
333b20bb
GM
10355 return 0;
10356
10357 /* Bounding box must be a list or vector containing 4 integers. */
10358 tem = fmt[GS_BOUNDING_BOX].value;
10359 if (CONSP (tem))
10360 {
10361 for (i = 0; i < 4; ++i, tem = XCDR (tem))
10362 if (!CONSP (tem) || !INTEGERP (XCAR (tem)))
10363 return 0;
10364 if (!NILP (tem))
10365 return 0;
10366 }
10367 else if (VECTORP (tem))
10368 {
10369 if (XVECTOR (tem)->size != 4)
10370 return 0;
10371 for (i = 0; i < 4; ++i)
10372 if (!INTEGERP (XVECTOR (tem)->contents[i]))
10373 return 0;
10374 }
10375 else
10376 return 0;
10377
10378 return 1;
10379}
10380
10381
10382/* Load Ghostscript image IMG for use on frame F. Value is non-zero
10383 if successful. */
10384
10385static int
10386gs_load (f, img)
10387 struct frame *f;
10388 struct image *img;
10389{
10390 char buffer[100];
10391 Lisp_Object window_and_pixmap_id = Qnil, loader, pt_height, pt_width;
10392 struct gcpro gcpro1, gcpro2;
10393 Lisp_Object frame;
10394 double in_width, in_height;
10395 Lisp_Object pixel_colors = Qnil;
10396
10397 /* Compute pixel size of pixmap needed from the given size in the
10398 image specification. Sizes in the specification are in pt. 1 pt
10399 = 1/72 in, xdpi and ydpi are stored in the frame's X display
10400 info. */
10401 pt_width = image_spec_value (img->spec, QCpt_width, NULL);
10402 in_width = XFASTINT (pt_width) / 72.0;
10403 img->width = in_width * FRAME_X_DISPLAY_INFO (f)->resx;
10404 pt_height = image_spec_value (img->spec, QCpt_height, NULL);
10405 in_height = XFASTINT (pt_height) / 72.0;
10406 img->height = in_height * FRAME_X_DISPLAY_INFO (f)->resy;
10407
10408 /* Create the pixmap. */
dd00328a 10409 xassert (img->pixmap == None);
333b20bb
GM
10410 img->pixmap = XCreatePixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10411 img->width, img->height,
10412 DefaultDepthOfScreen (FRAME_X_SCREEN (f)));
333b20bb
GM
10413
10414 if (!img->pixmap)
10415 {
45158a91 10416 image_error ("Unable to create pixmap for `%s'", img->spec, Qnil);
333b20bb
GM
10417 return 0;
10418 }
7273d100 10419
333b20bb
GM
10420 /* Call the loader to fill the pixmap. It returns a process object
10421 if successful. We do not record_unwind_protect here because
10422 other places in redisplay like calling window scroll functions
10423 don't either. Let the Lisp loader use `unwind-protect' instead. */
10424 GCPRO2 (window_and_pixmap_id, pixel_colors);
10425
10426 sprintf (buffer, "%lu %lu",
10427 (unsigned long) FRAME_X_WINDOW (f),
10428 (unsigned long) img->pixmap);
10429 window_and_pixmap_id = build_string (buffer);
7273d100 10430
333b20bb
GM
10431 sprintf (buffer, "%lu %lu",
10432 FRAME_FOREGROUND_PIXEL (f),
10433 FRAME_BACKGROUND_PIXEL (f));
10434 pixel_colors = build_string (buffer);
7273d100 10435
333b20bb
GM
10436 XSETFRAME (frame, f);
10437 loader = image_spec_value (img->spec, QCloader, NULL);
10438 if (NILP (loader))
10439 loader = intern ("gs-load-image");
10440
10441 img->data.lisp_val = call6 (loader, frame, img->spec,
10442 make_number (img->width),
10443 make_number (img->height),
10444 window_and_pixmap_id,
10445 pixel_colors);
10446 UNGCPRO;
10447 return PROCESSP (img->data.lisp_val);
10448}
10449
10450
10451/* Kill the Ghostscript process that was started to fill PIXMAP on
10452 frame F. Called from XTread_socket when receiving an event
10453 telling Emacs that Ghostscript has finished drawing. */
10454
10455void
10456x_kill_gs_process (pixmap, f)
10457 Pixmap pixmap;
10458 struct frame *f;
10459{
10460 struct image_cache *c = FRAME_X_IMAGE_CACHE (f);
10461 int class, i;
10462 struct image *img;
10463
10464 /* Find the image containing PIXMAP. */
10465 for (i = 0; i < c->used; ++i)
10466 if (c->images[i]->pixmap == pixmap)
10467 break;
10468
daba7643
GM
10469 /* Should someone in between have cleared the image cache, for
10470 instance, give up. */
10471 if (i == c->used)
10472 return;
7273d100 10473
333b20bb
GM
10474 /* Kill the GS process. We should have found PIXMAP in the image
10475 cache and its image should contain a process object. */
333b20bb
GM
10476 img = c->images[i];
10477 xassert (PROCESSP (img->data.lisp_val));
10478 Fkill_process (img->data.lisp_val, Qnil);
10479 img->data.lisp_val = Qnil;
10480
10481 /* On displays with a mutable colormap, figure out the colors
10482 allocated for the image by looking at the pixels of an XImage for
10483 img->pixmap. */
383d6ffc 10484 class = FRAME_X_VISUAL (f)->class;
333b20bb
GM
10485 if (class != StaticColor && class != StaticGray && class != TrueColor)
10486 {
10487 XImage *ximg;
10488
10489 BLOCK_INPUT;
10490
10491 /* Try to get an XImage for img->pixmep. */
10492 ximg = XGetImage (FRAME_X_DISPLAY (f), img->pixmap,
10493 0, 0, img->width, img->height, ~0, ZPixmap);
10494 if (ximg)
10495 {
10496 int x, y;
7273d100 10497
333b20bb
GM
10498 /* Initialize the color table. */
10499 init_color_table ();
7273d100 10500
333b20bb
GM
10501 /* For each pixel of the image, look its color up in the
10502 color table. After having done so, the color table will
10503 contain an entry for each color used by the image. */
10504 for (y = 0; y < img->height; ++y)
10505 for (x = 0; x < img->width; ++x)
10506 {
10507 unsigned long pixel = XGetPixel (ximg, x, y);
10508 lookup_pixel_color (f, pixel);
10509 }
10510
10511 /* Record colors in the image. Free color table and XImage. */
10512 img->colors = colors_in_color_table (&img->ncolors);
10513 free_color_table ();
10514 XDestroyImage (ximg);
10515
10516#if 0 /* This doesn't seem to be the case. If we free the colors
10517 here, we get a BadAccess later in x_clear_image when
10518 freeing the colors. */
10519 /* We have allocated colors once, but Ghostscript has also
10520 allocated colors on behalf of us. So, to get the
10521 reference counts right, free them once. */
10522 if (img->ncolors)
462d5d40 10523 x_free_colors (f, img->colors, img->ncolors);
333b20bb
GM
10524#endif
10525 }
10526 else
10527 image_error ("Cannot get X image of `%s'; colors will not be freed",
45158a91 10528 img->spec, Qnil);
7273d100 10529
333b20bb
GM
10530 UNBLOCK_INPUT;
10531 }
ad18ffb1
GM
10532
10533 /* Now that we have the pixmap, compute mask and transform the
10534 image if requested. */
10535 BLOCK_INPUT;
10536 postprocess_image (f, img);
10537 UNBLOCK_INPUT;
333b20bb
GM
10538}
10539
10540
10541\f
10542/***********************************************************************
10543 Window properties
10544 ***********************************************************************/
10545
10546DEFUN ("x-change-window-property", Fx_change_window_property,
10547 Sx_change_window_property, 2, 3, 0,
7ee72033 10548 doc: /* Change window property PROP to VALUE on the X window of FRAME.
c061c855 10549PROP and VALUE must be strings. FRAME nil or omitted means use the
7ee72033
MB
10550selected frame. Value is VALUE. */)
10551 (prop, value, frame)
333b20bb
GM
10552 Lisp_Object frame, prop, value;
10553{
10554 struct frame *f = check_x_frame (frame);
10555 Atom prop_atom;
10556
b7826503
PJ
10557 CHECK_STRING (prop);
10558 CHECK_STRING (value);
333b20bb
GM
10559
10560 BLOCK_INPUT;
d5db4077 10561 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
333b20bb
GM
10562 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10563 prop_atom, XA_STRING, 8, PropModeReplace,
d5db4077 10564 SDATA (value), SCHARS (value));
333b20bb
GM
10565
10566 /* Make sure the property is set when we return. */
10567 XFlush (FRAME_X_DISPLAY (f));
10568 UNBLOCK_INPUT;
10569
10570 return value;
10571}
10572
10573
10574DEFUN ("x-delete-window-property", Fx_delete_window_property,
10575 Sx_delete_window_property, 1, 2, 0,
7ee72033
MB
10576 doc: /* Remove window property PROP from X window of FRAME.
10577FRAME nil or omitted means use the selected frame. Value is PROP. */)
10578 (prop, frame)
333b20bb
GM
10579 Lisp_Object prop, frame;
10580{
10581 struct frame *f = check_x_frame (frame);
10582 Atom prop_atom;
10583
b7826503 10584 CHECK_STRING (prop);
333b20bb 10585 BLOCK_INPUT;
d5db4077 10586 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
333b20bb
GM
10587 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
10588
10589 /* Make sure the property is removed when we return. */
10590 XFlush (FRAME_X_DISPLAY (f));
10591 UNBLOCK_INPUT;
10592
10593 return prop;
10594}
10595
10596
10597DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
10598 1, 2, 0,
7ee72033 10599 doc: /* Value is the value of window property PROP on FRAME.
c061c855
GM
10600If FRAME is nil or omitted, use the selected frame. Value is nil
10601if FRAME hasn't a property with name PROP or if PROP has no string
7ee72033
MB
10602value. */)
10603 (prop, frame)
333b20bb
GM
10604 Lisp_Object prop, frame;
10605{
10606 struct frame *f = check_x_frame (frame);
10607 Atom prop_atom;
10608 int rc;
10609 Lisp_Object prop_value = Qnil;
10610 char *tmp_data = NULL;
10611 Atom actual_type;
10612 int actual_format;
10613 unsigned long actual_size, bytes_remaining;
10614
b7826503 10615 CHECK_STRING (prop);
333b20bb 10616 BLOCK_INPUT;
d5db4077 10617 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
333b20bb
GM
10618 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10619 prop_atom, 0, 0, False, XA_STRING,
10620 &actual_type, &actual_format, &actual_size,
10621 &bytes_remaining, (unsigned char **) &tmp_data);
10622 if (rc == Success)
10623 {
10624 int size = bytes_remaining;
10625
10626 XFree (tmp_data);
10627 tmp_data = NULL;
10628
10629 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10630 prop_atom, 0, bytes_remaining,
10631 False, XA_STRING,
7273d100
FP
10632 &actual_type, &actual_format,
10633 &actual_size, &bytes_remaining,
333b20bb 10634 (unsigned char **) &tmp_data);
4c8c7926 10635 if (rc == Success && tmp_data)
333b20bb
GM
10636 prop_value = make_string (tmp_data, size);
10637
10638 XFree (tmp_data);
10639 }
10640
10641 UNBLOCK_INPUT;
10642 return prop_value;
10643}
10644
10645
10646\f
10647/***********************************************************************
10648 Busy cursor
10649 ***********************************************************************/
10650
4ae9a85e 10651/* If non-null, an asynchronous timer that, when it expires, displays
0af913d7 10652 an hourglass cursor on all frames. */
333b20bb 10653
0af913d7 10654static struct atimer *hourglass_atimer;
333b20bb 10655
0af913d7 10656/* Non-zero means an hourglass cursor is currently shown. */
333b20bb 10657
0af913d7 10658static int hourglass_shown_p;
333b20bb 10659
0af913d7 10660/* Number of seconds to wait before displaying an hourglass cursor. */
333b20bb 10661
0af913d7 10662static Lisp_Object Vhourglass_delay;
333b20bb 10663
0af913d7 10664/* Default number of seconds to wait before displaying an hourglass
4ae9a85e
GM
10665 cursor. */
10666
0af913d7 10667#define DEFAULT_HOURGLASS_DELAY 1
4ae9a85e
GM
10668
10669/* Function prototypes. */
10670
0af913d7
GM
10671static void show_hourglass P_ ((struct atimer *));
10672static void hide_hourglass P_ ((void));
4ae9a85e
GM
10673
10674
0af913d7 10675/* Cancel a currently active hourglass timer, and start a new one. */
4ae9a85e
GM
10676
10677void
0af913d7 10678start_hourglass ()
333b20bb 10679{
4ae9a85e 10680 EMACS_TIME delay;
3caa99d3 10681 int secs, usecs = 0;
7273d100 10682
0af913d7 10683 cancel_hourglass ();
4ae9a85e 10684
0af913d7
GM
10685 if (INTEGERP (Vhourglass_delay)
10686 && XINT (Vhourglass_delay) > 0)
10687 secs = XFASTINT (Vhourglass_delay);
10688 else if (FLOATP (Vhourglass_delay)
10689 && XFLOAT_DATA (Vhourglass_delay) > 0)
3caa99d3
GM
10690 {
10691 Lisp_Object tem;
0af913d7 10692 tem = Ftruncate (Vhourglass_delay, Qnil);
3caa99d3 10693 secs = XFASTINT (tem);
0af913d7 10694 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
3caa99d3 10695 }
4ae9a85e 10696 else
0af913d7 10697 secs = DEFAULT_HOURGLASS_DELAY;
7273d100 10698
3caa99d3 10699 EMACS_SET_SECS_USECS (delay, secs, usecs);
0af913d7
GM
10700 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
10701 show_hourglass, NULL);
4ae9a85e
GM
10702}
10703
10704
0af913d7 10705/* Cancel the hourglass cursor timer if active, hide a busy cursor if
4ae9a85e
GM
10706 shown. */
10707
10708void
0af913d7 10709cancel_hourglass ()
4ae9a85e 10710{
0af913d7 10711 if (hourglass_atimer)
99f01f62 10712 {
0af913d7
GM
10713 cancel_atimer (hourglass_atimer);
10714 hourglass_atimer = NULL;
99f01f62 10715 }
7273d100 10716
0af913d7
GM
10717 if (hourglass_shown_p)
10718 hide_hourglass ();
4ae9a85e
GM
10719}
10720
10721
0af913d7
GM
10722/* Timer function of hourglass_atimer. TIMER is equal to
10723 hourglass_atimer.
4ae9a85e 10724
0af913d7
GM
10725 Display an hourglass pointer on all frames by mapping the frames'
10726 hourglass_window. Set the hourglass_p flag in the frames'
10727 output_data.x structure to indicate that an hourglass cursor is
10728 shown on the frames. */
4ae9a85e
GM
10729
10730static void
0af913d7 10731show_hourglass (timer)
4ae9a85e
GM
10732 struct atimer *timer;
10733{
10734 /* The timer implementation will cancel this timer automatically
0af913d7 10735 after this function has run. Set hourglass_atimer to null
4ae9a85e 10736 so that we know the timer doesn't have to be canceled. */
0af913d7 10737 hourglass_atimer = NULL;
4ae9a85e 10738
0af913d7 10739 if (!hourglass_shown_p)
333b20bb
GM
10740 {
10741 Lisp_Object rest, frame;
7273d100 10742
4ae9a85e 10743 BLOCK_INPUT;
7273d100 10744
333b20bb 10745 FOR_EACH_FRAME (rest, frame)
5f7a1890
GM
10746 {
10747 struct frame *f = XFRAME (frame);
7273d100 10748
5f7a1890
GM
10749 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
10750 {
10751 Display *dpy = FRAME_X_DISPLAY (f);
7273d100 10752
5f7a1890
GM
10753#ifdef USE_X_TOOLKIT
10754 if (f->output_data.x->widget)
10755#else
10756 if (FRAME_OUTER_WINDOW (f))
10757#endif
10758 {
0af913d7 10759 f->output_data.x->hourglass_p = 1;
7273d100 10760
0af913d7 10761 if (!f->output_data.x->hourglass_window)
5f7a1890
GM
10762 {
10763 unsigned long mask = CWCursor;
10764 XSetWindowAttributes attrs;
7273d100 10765
0af913d7 10766 attrs.cursor = f->output_data.x->hourglass_cursor;
7273d100 10767
0af913d7 10768 f->output_data.x->hourglass_window
5f7a1890
GM
10769 = XCreateWindow (dpy, FRAME_OUTER_WINDOW (f),
10770 0, 0, 32000, 32000, 0, 0,
10771 InputOnly,
10772 CopyFromParent,
10773 mask, &attrs);
10774 }
7273d100 10775
0af913d7 10776 XMapRaised (dpy, f->output_data.x->hourglass_window);
5f7a1890
GM
10777 XFlush (dpy);
10778 }
10779 }
10780 }
333b20bb 10781
0af913d7 10782 hourglass_shown_p = 1;
4ae9a85e
GM
10783 UNBLOCK_INPUT;
10784 }
333b20bb
GM
10785}
10786
10787
0af913d7
GM
10788/* Hide the hourglass pointer on all frames, if it is currently
10789 shown. */
333b20bb 10790
4ae9a85e 10791static void
0af913d7 10792hide_hourglass ()
4ae9a85e 10793{
0af913d7 10794 if (hourglass_shown_p)
333b20bb 10795 {
4ae9a85e
GM
10796 Lisp_Object rest, frame;
10797
10798 BLOCK_INPUT;
10799 FOR_EACH_FRAME (rest, frame)
333b20bb 10800 {
4ae9a85e 10801 struct frame *f = XFRAME (frame);
7273d100 10802
4ae9a85e
GM
10803 if (FRAME_X_P (f)
10804 /* Watch out for newly created frames. */
0af913d7 10805 && f->output_data.x->hourglass_window)
4ae9a85e 10806 {
0af913d7
GM
10807 XUnmapWindow (FRAME_X_DISPLAY (f),
10808 f->output_data.x->hourglass_window);
10809 /* Sync here because XTread_socket looks at the
10810 hourglass_p flag that is reset to zero below. */
4ae9a85e 10811 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 10812 f->output_data.x->hourglass_p = 0;
4ae9a85e 10813 }
333b20bb 10814 }
333b20bb 10815
0af913d7 10816 hourglass_shown_p = 0;
4ae9a85e
GM
10817 UNBLOCK_INPUT;
10818 }
333b20bb
GM
10819}
10820
10821
10822\f
10823/***********************************************************************
10824 Tool tips
10825 ***********************************************************************/
10826
10827static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
275841bf 10828 Lisp_Object, Lisp_Object));
06d62053 10829static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
ab452f99 10830 Lisp_Object, int, int, int *, int *));
7273d100 10831
44b5a125 10832/* The frame of a currently visible tooltip. */
333b20bb 10833
44b5a125 10834Lisp_Object tip_frame;
333b20bb
GM
10835
10836/* If non-nil, a timer started that hides the last tooltip when it
10837 fires. */
10838
10839Lisp_Object tip_timer;
10840Window tip_window;
10841
06d62053
GM
10842/* If non-nil, a vector of 3 elements containing the last args
10843 with which x-show-tip was called. See there. */
10844
10845Lisp_Object last_show_tip_args;
10846
d63931a2
GM
10847/* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
10848
10849Lisp_Object Vx_max_tooltip_size;
10850
eaf1eea9
GM
10851
10852static Lisp_Object
10853unwind_create_tip_frame (frame)
10854 Lisp_Object frame;
10855{
c844a81a
GM
10856 Lisp_Object deleted;
10857
10858 deleted = unwind_create_frame (frame);
10859 if (EQ (deleted, Qt))
10860 {
10861 tip_window = None;
10862 tip_frame = Qnil;
10863 }
7273d100 10864
c844a81a 10865 return deleted;
eaf1eea9
GM
10866}
10867
10868
333b20bb 10869/* Create a frame for a tooltip on the display described by DPYINFO.
275841bf
GM
10870 PARMS is a list of frame parameters. TEXT is the string to
10871 display in the tip frame. Value is the frame.
eaf1eea9
GM
10872
10873 Note that functions called here, esp. x_default_parameter can
10874 signal errors, for instance when a specified color name is
10875 undefined. We have to make sure that we're in a consistent state
10876 when this happens. */
333b20bb
GM
10877
10878static Lisp_Object
275841bf 10879x_create_tip_frame (dpyinfo, parms, text)
333b20bb 10880 struct x_display_info *dpyinfo;
275841bf 10881 Lisp_Object parms, text;
333b20bb
GM
10882{
10883 struct frame *f;
10884 Lisp_Object frame, tem;
10885 Lisp_Object name;
333b20bb
GM
10886 long window_prompting = 0;
10887 int width, height;
331379bf 10888 int count = SPECPDL_INDEX ();
b6d7acec 10889 struct gcpro gcpro1, gcpro2, gcpro3;
333b20bb 10890 struct kboard *kb;
06d62053 10891 int face_change_count_before = face_change_count;
275841bf
GM
10892 Lisp_Object buffer;
10893 struct buffer *old_buffer;
333b20bb
GM
10894
10895 check_x ();
10896
10897 /* Use this general default value to start with until we know if
10898 this frame has a specified name. */
10899 Vx_resource_name = Vinvocation_name;
10900
10901#ifdef MULTI_KBOARD
10902 kb = dpyinfo->kboard;
10903#else
10904 kb = &the_only_kboard;
10905#endif
10906
10907 /* Get the name of the frame to use for resource lookup. */
10908 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
10909 if (!STRINGP (name)
10910 && !EQ (name, Qunbound)
10911 && !NILP (name))
10912 error ("Invalid frame name--not a string or nil");
10913 Vx_resource_name = name;
10914
10915 frame = Qnil;
10916 GCPRO3 (parms, name, frame);
44b5a125 10917 f = make_frame (1);
333b20bb 10918 XSETFRAME (frame, f);
275841bf
GM
10919
10920 buffer = Fget_buffer_create (build_string (" *tip*"));
10921 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer);
10922 old_buffer = current_buffer;
10923 set_buffer_internal_1 (XBUFFER (buffer));
d63931a2 10924 current_buffer->truncate_lines = Qnil;
275841bf
GM
10925 Ferase_buffer ();
10926 Finsert (1, &text);
10927 set_buffer_internal_1 (old_buffer);
7273d100 10928
333b20bb 10929 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
8a1a7743 10930 record_unwind_protect (unwind_create_tip_frame, frame);
333b20bb 10931
eaf1eea9
GM
10932 /* By setting the output method, we're essentially saying that
10933 the frame is live, as per FRAME_LIVE_P. If we get a signal
10934 from this point on, x_destroy_window might screw up reference
10935 counts etc. */
333b20bb
GM
10936 f->output_method = output_x_window;
10937 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
10938 bzero (f->output_data.x, sizeof (struct x_output));
10939 f->output_data.x->icon_bitmap = -1;
10940 f->output_data.x->fontset = -1;
61d461a8
GM
10941 f->output_data.x->scroll_bar_foreground_pixel = -1;
10942 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
10943#ifdef USE_TOOLKIT_SCROLL_BARS
10944 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
10945 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
10946#endif /* USE_TOOLKIT_SCROLL_BARS */
333b20bb
GM
10947 f->icon_name = Qnil;
10948 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
f1d2ce7f 10949#if GLYPH_DEBUG
eaf1eea9
GM
10950 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
10951 dpyinfo_refcount = dpyinfo->reference_count;
10952#endif /* GLYPH_DEBUG */
333b20bb
GM
10953#ifdef MULTI_KBOARD
10954 FRAME_KBOARD (f) = kb;
10955#endif
10956 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
10957 f->output_data.x->explicit_parent = 0;
10958
61d461a8
GM
10959 /* These colors will be set anyway later, but it's important
10960 to get the color reference counts right, so initialize them! */
10961 {
10962 Lisp_Object black;
10963 struct gcpro gcpro1;
7273d100 10964
61d461a8
GM
10965 black = build_string ("black");
10966 GCPRO1 (black);
10967 f->output_data.x->foreground_pixel
10968 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10969 f->output_data.x->background_pixel
10970 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10971 f->output_data.x->cursor_pixel
10972 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10973 f->output_data.x->cursor_foreground_pixel
10974 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10975 f->output_data.x->border_pixel
10976 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10977 f->output_data.x->mouse_pixel
10978 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
10979 UNGCPRO;
10980 }
10981
333b20bb
GM
10982 /* Set the name; the functions to which we pass f expect the name to
10983 be set. */
10984 if (EQ (name, Qunbound) || NILP (name))
10985 {
10986 f->name = build_string (dpyinfo->x_id_name);
10987 f->explicit_name = 0;
10988 }
10989 else
10990 {
10991 f->name = name;
10992 f->explicit_name = 1;
10993 /* use the frame's title when getting resources for this frame. */
10994 specbind (Qx_resource_name, name);
10995 }
10996
eaf1eea9
GM
10997 /* Extract the window parameters from the supplied values that are
10998 needed to determine window geometry. */
333b20bb
GM
10999 {
11000 Lisp_Object font;
11001
11002 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
11003
11004 BLOCK_INPUT;
11005 /* First, try whatever font the caller has specified. */
11006 if (STRINGP (font))
11007 {
11008 tem = Fquery_fontset (font, Qnil);
11009 if (STRINGP (tem))
d5db4077 11010 font = x_new_fontset (f, SDATA (tem));
333b20bb 11011 else
d5db4077 11012 font = x_new_font (f, SDATA (font));
333b20bb 11013 }
7273d100 11014
333b20bb
GM
11015 /* Try out a font which we hope has bold and italic variations. */
11016 if (!STRINGP (font))
11017 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
11018 if (!STRINGP (font))
11019 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
11020 if (! STRINGP (font))
11021 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
11022 if (! STRINGP (font))
11023 /* This was formerly the first thing tried, but it finds too many fonts
11024 and takes too long. */
11025 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
11026 /* If those didn't work, look for something which will at least work. */
11027 if (! STRINGP (font))
11028 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
11029 UNBLOCK_INPUT;
11030 if (! STRINGP (font))
11031 font = build_string ("fixed");
11032
11033 x_default_parameter (f, parms, Qfont, font,
11034 "font", "Font", RES_TYPE_STRING);
11035 }
11036
11037 x_default_parameter (f, parms, Qborder_width, make_number (2),
11038 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
7273d100 11039
333b20bb
GM
11040 /* This defaults to 2 in order to match xterm. We recognize either
11041 internalBorderWidth or internalBorder (which is what xterm calls
11042 it). */
11043 if (NILP (Fassq (Qinternal_border_width, parms)))
11044 {
11045 Lisp_Object value;
11046
11047 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
11048 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
11049 if (! EQ (value, Qunbound))
11050 parms = Fcons (Fcons (Qinternal_border_width, value),
11051 parms);
11052 }
11053
11054 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
11055 "internalBorderWidth", "internalBorderWidth",
11056 RES_TYPE_NUMBER);
11057
11058 /* Also do the stuff which must be set before the window exists. */
11059 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
11060 "foreground", "Foreground", RES_TYPE_STRING);
11061 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
11062 "background", "Background", RES_TYPE_STRING);
11063 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
11064 "pointerColor", "Foreground", RES_TYPE_STRING);
11065 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
11066 "cursorColor", "Foreground", RES_TYPE_STRING);
11067 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
11068 "borderColor", "BorderColor", RES_TYPE_STRING);
11069
11070 /* Init faces before x_default_parameter is called for scroll-bar
11071 parameters because that function calls x_set_scroll_bar_width,
11072 which calls change_frame_size, which calls Fset_window_buffer,
11073 which runs hooks, which call Fvertical_motion. At the end, we
11074 end up in init_iterator with a null face cache, which should not
11075 happen. */
11076 init_frame_faces (f);
7273d100 11077
333b20bb
GM
11078 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
11079 window_prompting = x_figure_window_size (f, parms);
11080
11081 if (window_prompting & XNegative)
11082 {
11083 if (window_prompting & YNegative)
11084 f->output_data.x->win_gravity = SouthEastGravity;
11085 else
11086 f->output_data.x->win_gravity = NorthEastGravity;
11087 }
11088 else
11089 {
11090 if (window_prompting & YNegative)
11091 f->output_data.x->win_gravity = SouthWestGravity;
11092 else
11093 f->output_data.x->win_gravity = NorthWestGravity;
11094 }
11095
11096 f->output_data.x->size_hint_flags = window_prompting;
11097 {
11098 XSetWindowAttributes attrs;
11099 unsigned long mask;
7273d100 11100
333b20bb 11101 BLOCK_INPUT;
c51d2b5e
GM
11102 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
11103 if (DoesSaveUnders (dpyinfo->screen))
11104 mask |= CWSaveUnder;
7273d100 11105
9b2956e2
GM
11106 /* Window managers look at the override-redirect flag to determine
11107 whether or net to give windows a decoration (Xlib spec, chapter
333b20bb
GM
11108 3.2.8). */
11109 attrs.override_redirect = True;
11110 attrs.save_under = True;
11111 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
11112 /* Arrange for getting MapNotify and UnmapNotify events. */
11113 attrs.event_mask = StructureNotifyMask;
11114 tip_window
11115 = FRAME_X_WINDOW (f)
11116 = XCreateWindow (FRAME_X_DISPLAY (f),
11117 FRAME_X_DISPLAY_INFO (f)->root_window,
11118 /* x, y, width, height */
11119 0, 0, 1, 1,
11120 /* Border. */
11121 1,
11122 CopyFromParent, InputOutput, CopyFromParent,
11123 mask, &attrs);
11124 UNBLOCK_INPUT;
11125 }
11126
11127 x_make_gc (f);
11128
333b20bb
GM
11129 x_default_parameter (f, parms, Qauto_raise, Qnil,
11130 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
11131 x_default_parameter (f, parms, Qauto_lower, Qnil,
11132 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
11133 x_default_parameter (f, parms, Qcursor_type, Qbox,
11134 "cursorType", "CursorType", RES_TYPE_SYMBOL);
11135
11136 /* Dimensions, especially f->height, must be done via change_frame_size.
11137 Change will not be effected unless different from the current
11138 f->height. */
11139 width = f->width;
11140 height = f->height;
11141 f->height = 0;
11142 SET_FRAME_WIDTH (f, 0);
8938a4fb 11143 change_frame_size (f, height, width, 1, 0, 0);
7273d100 11144
cd1d850f
JPW
11145 /* Add `tooltip' frame parameter's default value. */
11146 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
11147 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
11148 Qnil));
7273d100 11149
035d5114 11150 /* Set up faces after all frame parameters are known. This call
6801a572
GM
11151 also merges in face attributes specified for new frames.
11152
11153 Frame parameters may be changed if .Xdefaults contains
11154 specifications for the default font. For example, if there is an
11155 `Emacs.default.attributeBackground: pink', the `background-color'
11156 attribute of the frame get's set, which let's the internal border
11157 of the tooltip frame appear in pink. Prevent this. */
11158 {
11159 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
11160
11161 /* Set tip_frame here, so that */
11162 tip_frame = frame;
11163 call1 (Qface_set_after_frame_default, frame);
7273d100 11164
6801a572
GM
11165 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
11166 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
11167 Qnil));
11168 }
7273d100 11169
333b20bb
GM
11170 f->no_split = 1;
11171
11172 UNGCPRO;
11173
11174 /* It is now ok to make the frame official even if we get an error
11175 below. And the frame needs to be on Vframe_list or making it
11176 visible won't work. */
11177 Vframe_list = Fcons (frame, Vframe_list);
11178
11179 /* Now that the frame is official, it counts as a reference to
11180 its display. */
11181 FRAME_X_DISPLAY_INFO (f)->reference_count++;
11182
06d62053
GM
11183 /* Setting attributes of faces of the tooltip frame from resources
11184 and similar will increment face_change_count, which leads to the
11185 clearing of all current matrices. Since this isn't necessary
11186 here, avoid it by resetting face_change_count to the value it
11187 had before we created the tip frame. */
11188 face_change_count = face_change_count_before;
11189
eaf1eea9 11190 /* Discard the unwind_protect. */
333b20bb
GM
11191 return unbind_to (count, frame);
11192}
11193
11194
06d62053
GM
11195/* Compute where to display tip frame F. PARMS is the list of frame
11196 parameters for F. DX and DY are specified offsets from the current
ab452f99
GM
11197 location of the mouse. WIDTH and HEIGHT are the width and height
11198 of the tooltip. Return coordinates relative to the root window of
11199 the display in *ROOT_X, and *ROOT_Y. */
06d62053
GM
11200
11201static void
ab452f99 11202compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
06d62053
GM
11203 struct frame *f;
11204 Lisp_Object parms, dx, dy;
ab452f99 11205 int width, height;
06d62053
GM
11206 int *root_x, *root_y;
11207{
11208 Lisp_Object left, top;
11209 int win_x, win_y;
11210 Window root, child;
11211 unsigned pmask;
7273d100 11212
06d62053
GM
11213 /* User-specified position? */
11214 left = Fcdr (Fassq (Qleft, parms));
11215 top = Fcdr (Fassq (Qtop, parms));
7273d100 11216
06d62053
GM
11217 /* Move the tooltip window where the mouse pointer is. Resize and
11218 show it. */
570d22b0 11219 if (!INTEGERP (left) || !INTEGERP (top))
ab452f99
GM
11220 {
11221 BLOCK_INPUT;
11222 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
11223 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
11224 UNBLOCK_INPUT;
11225 }
06d62053 11226
06d62053
GM
11227 if (INTEGERP (top))
11228 *root_y = XINT (top);
ab452f99
GM
11229 else if (*root_y + XINT (dy) - height < 0)
11230 *root_y -= XINT (dy);
11231 else
11232 {
11233 *root_y -= height;
11234 *root_y += XINT (dy);
11235 }
11236
11237 if (INTEGERP (left))
11238 *root_x = XINT (left);
d682d3df
RS
11239 else if (*root_x + XINT (dx) + width <= FRAME_X_DISPLAY_INFO (f)->width)
11240 /* It fits to the right of the pointer. */
11241 *root_x += XINT (dx);
11242 else if (width + XINT (dx) <= *root_x)
11243 /* It fits to the left of the pointer. */
ab452f99
GM
11244 *root_x -= width + XINT (dx);
11245 else
d682d3df
RS
11246 /* Put it left-justified on the screen--it ought to fit that way. */
11247 *root_x = 0;
06d62053
GM
11248}
11249
11250
0634ce98 11251DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7ee72033 11252 doc: /* Show STRING in a "tooltip" window on frame FRAME.
c061c855
GM
11253A tooltip window is a small X window displaying a string.
11254
11255FRAME nil or omitted means use the selected frame.
11256
11257PARMS is an optional list of frame parameters which can be used to
11258change the tooltip's appearance.
11259
11260Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
11261means use the default timeout of 5 seconds.
11262
11263If the list of frame parameters PARAMS contains a `left' parameters,
11264the tooltip is displayed at that x-position. Otherwise it is
11265displayed at the mouse position, with offset DX added (default is 5 if
11266DX isn't specified). Likewise for the y-position; if a `top' frame
11267parameter is specified, it determines the y-position of the tooltip
11268window, otherwise it is displayed at the mouse position, with offset
11269DY added (default is -10).
11270
11271A tooltip's maximum size is specified by `x-max-tooltip-size'.
7ee72033
MB
11272Text larger than the specified size is clipped. */)
11273 (string, frame, parms, timeout, dx, dy)
0634ce98 11274 Lisp_Object string, frame, parms, timeout, dx, dy;
333b20bb
GM
11275{
11276 struct frame *f;
11277 struct window *w;
06d62053 11278 int root_x, root_y;
333b20bb
GM
11279 struct buffer *old_buffer;
11280 struct text_pos pos;
11281 int i, width, height;
393f2d14 11282 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
333b20bb 11283 int old_windows_or_buffers_changed = windows_or_buffers_changed;
331379bf 11284 int count = SPECPDL_INDEX ();
7273d100 11285
333b20bb
GM
11286 specbind (Qinhibit_redisplay, Qt);
11287
393f2d14 11288 GCPRO4 (string, parms, frame, timeout);
333b20bb 11289
b7826503 11290 CHECK_STRING (string);
333b20bb
GM
11291 f = check_x_frame (frame);
11292 if (NILP (timeout))
11293 timeout = make_number (5);
11294 else
b7826503 11295 CHECK_NATNUM (timeout);
7273d100 11296
0634ce98
GM
11297 if (NILP (dx))
11298 dx = make_number (5);
11299 else
b7826503 11300 CHECK_NUMBER (dx);
7273d100 11301
0634ce98 11302 if (NILP (dy))
12c67a7f 11303 dy = make_number (-10);
0634ce98 11304 else
b7826503 11305 CHECK_NUMBER (dy);
333b20bb 11306
06d62053
GM
11307 if (NILP (last_show_tip_args))
11308 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
11309
11310 if (!NILP (tip_frame))
11311 {
11312 Lisp_Object last_string = AREF (last_show_tip_args, 0);
11313 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
11314 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
11315
11316 if (EQ (frame, last_frame)
11317 && !NILP (Fequal (last_string, string))
11318 && !NILP (Fequal (last_parms, parms)))
11319 {
11320 struct frame *f = XFRAME (tip_frame);
7273d100 11321
06d62053
GM
11322 /* Only DX and DY have changed. */
11323 if (!NILP (tip_timer))
ae782866
GM
11324 {
11325 Lisp_Object timer = tip_timer;
11326 tip_timer = Qnil;
11327 call1 (Qcancel_timer, timer);
11328 }
06d62053
GM
11329
11330 BLOCK_INPUT;
ab452f99
GM
11331 compute_tip_xy (f, parms, dx, dy, PIXEL_WIDTH (f),
11332 PIXEL_HEIGHT (f), &root_x, &root_y);
06d62053 11333 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 11334 root_x, root_y);
06d62053
GM
11335 UNBLOCK_INPUT;
11336 goto start_timer;
11337 }
11338 }
11339
333b20bb
GM
11340 /* Hide a previous tip, if any. */
11341 Fx_hide_tip ();
11342
06d62053
GM
11343 ASET (last_show_tip_args, 0, string);
11344 ASET (last_show_tip_args, 1, frame);
11345 ASET (last_show_tip_args, 2, parms);
11346
333b20bb
GM
11347 /* Add default values to frame parameters. */
11348 if (NILP (Fassq (Qname, parms)))
11349 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
11350 if (NILP (Fassq (Qinternal_border_width, parms)))
11351 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
11352 if (NILP (Fassq (Qborder_width, parms)))
11353 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
11354 if (NILP (Fassq (Qborder_color, parms)))
11355 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
11356 if (NILP (Fassq (Qbackground_color, parms)))
11357 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
11358 parms);
11359
11360 /* Create a frame for the tooltip, and record it in the global
11361 variable tip_frame. */
275841bf 11362 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
44b5a125 11363 f = XFRAME (frame);
333b20bb 11364
d63931a2 11365 /* Set up the frame's root window. */
333b20bb
GM
11366 w = XWINDOW (FRAME_ROOT_WINDOW (f));
11367 w->left = w->top = make_number (0);
7273d100 11368
d63931a2
GM
11369 if (CONSP (Vx_max_tooltip_size)
11370 && INTEGERP (XCAR (Vx_max_tooltip_size))
11371 && XINT (XCAR (Vx_max_tooltip_size)) > 0
11372 && INTEGERP (XCDR (Vx_max_tooltip_size))
11373 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
11374 {
11375 w->width = XCAR (Vx_max_tooltip_size);
11376 w->height = XCDR (Vx_max_tooltip_size);
11377 }
11378 else
11379 {
11380 w->width = make_number (80);
11381 w->height = make_number (40);
11382 }
7273d100 11383
d63931a2 11384 f->window_width = XINT (w->width);
333b20bb
GM
11385 adjust_glyphs (f);
11386 w->pseudo_window_p = 1;
11387
11388 /* Display the tooltip text in a temporary buffer. */
333b20bb 11389 old_buffer = current_buffer;
275841bf 11390 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
d63931a2 11391 current_buffer->truncate_lines = Qnil;
333b20bb
GM
11392 clear_glyph_matrix (w->desired_matrix);
11393 clear_glyph_matrix (w->current_matrix);
11394 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
11395 try_window (FRAME_ROOT_WINDOW (f), pos);
11396
11397 /* Compute width and height of the tooltip. */
11398 width = height = 0;
11399 for (i = 0; i < w->desired_matrix->nrows; ++i)
11400 {
11401 struct glyph_row *row = &w->desired_matrix->rows[i];
11402 struct glyph *last;
11403 int row_width;
11404
11405 /* Stop at the first empty row at the end. */
11406 if (!row->enabled_p || !row->displays_text_p)
11407 break;
11408
d7bf0342
GM
11409 /* Let the row go over the full width of the frame. */
11410 row->full_width_p = 1;
333b20bb 11411
e3130015 11412 /* There's a glyph at the end of rows that is used to place
333b20bb
GM
11413 the cursor there. Don't include the width of this glyph. */
11414 if (row->used[TEXT_AREA])
11415 {
11416 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
11417 row_width = row->pixel_width - last->pixel_width;
11418 }
11419 else
11420 row_width = row->pixel_width;
7273d100 11421
333b20bb
GM
11422 height += row->height;
11423 width = max (width, row_width);
11424 }
11425
11426 /* Add the frame's internal border to the width and height the X
11427 window should have. */
11428 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
11429 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
11430
11431 /* Move the tooltip window where the mouse pointer is. Resize and
11432 show it. */
ab452f99 11433 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
0634ce98 11434
0634ce98 11435 BLOCK_INPUT;
333b20bb 11436 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 11437 root_x, root_y, width, height);
333b20bb
GM
11438 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11439 UNBLOCK_INPUT;
7273d100 11440
333b20bb
GM
11441 /* Draw into the window. */
11442 w->must_be_updated_p = 1;
11443 update_single_window (w, 1);
11444
11445 /* Restore original current buffer. */
11446 set_buffer_internal_1 (old_buffer);
11447 windows_or_buffers_changed = old_windows_or_buffers_changed;
11448
06d62053 11449 start_timer:
333b20bb
GM
11450 /* Let the tip disappear after timeout seconds. */
11451 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
11452 intern ("x-hide-tip"));
a744a2ec
DL
11453
11454 UNGCPRO;
333b20bb
GM
11455 return unbind_to (count, Qnil);
11456}
11457
11458
11459DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7ee72033
MB
11460 doc: /* Hide the current tooltip window, if there is any.
11461Value is t if tooltip was open, nil otherwise. */)
11462 ()
333b20bb 11463{
44b5a125 11464 int count;
c0006262
GM
11465 Lisp_Object deleted, frame, timer;
11466 struct gcpro gcpro1, gcpro2;
44b5a125
GM
11467
11468 /* Return quickly if nothing to do. */
c0006262 11469 if (NILP (tip_timer) && NILP (tip_frame))
44b5a125 11470 return Qnil;
7273d100 11471
c0006262
GM
11472 frame = tip_frame;
11473 timer = tip_timer;
11474 GCPRO2 (frame, timer);
11475 tip_frame = tip_timer = deleted = Qnil;
7273d100 11476
331379bf 11477 count = SPECPDL_INDEX ();
333b20bb 11478 specbind (Qinhibit_redisplay, Qt);
44b5a125 11479 specbind (Qinhibit_quit, Qt);
7273d100 11480
c0006262 11481 if (!NILP (timer))
ae782866 11482 call1 (Qcancel_timer, timer);
333b20bb 11483
c0006262 11484 if (FRAMEP (frame))
333b20bb 11485 {
44b5a125
GM
11486 Fdelete_frame (frame, Qnil);
11487 deleted = Qt;
f6c44811
GM
11488
11489#ifdef USE_LUCID
11490 /* Bloodcurdling hack alert: The Lucid menu bar widget's
11491 redisplay procedure is not called when a tip frame over menu
11492 items is unmapped. Redisplay the menu manually... */
11493 {
11494 struct frame *f = SELECTED_FRAME ();
11495 Widget w = f->output_data.x->menubar_widget;
11496 extern void xlwmenu_redisplay P_ ((Widget));
9180dc8c 11497
f6c44811 11498 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
dbc64aa7 11499 && w != NULL)
f6c44811
GM
11500 {
11501 BLOCK_INPUT;
11502 xlwmenu_redisplay (w);
11503 UNBLOCK_INPUT;
11504 }
11505 }
11506#endif /* USE_LUCID */
333b20bb
GM
11507 }
11508
c0006262 11509 UNGCPRO;
44b5a125 11510 return unbind_to (count, deleted);
333b20bb
GM
11511}
11512
11513
11514\f
11515/***********************************************************************
11516 File selection dialog
11517 ***********************************************************************/
11518
11519#ifdef USE_MOTIF
11520
11521/* Callback for "OK" and "Cancel" on file selection dialog. */
11522
11523static void
11524file_dialog_cb (widget, client_data, call_data)
11525 Widget widget;
11526 XtPointer call_data, client_data;
11527{
11528 int *result = (int *) client_data;
11529 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
11530 *result = cb->reason;
11531}
11532
11533
a779d213
GM
11534/* Callback for unmapping a file selection dialog. This is used to
11535 capture the case where a dialog is closed via a window manager's
11536 closer button, for example. Using a XmNdestroyCallback didn't work
11537 in this case. */
11538
11539static void
11540file_dialog_unmap_cb (widget, client_data, call_data)
11541 Widget widget;
11542 XtPointer call_data, client_data;
11543{
11544 int *result = (int *) client_data;
11545 *result = XmCR_CANCEL;
11546}
11547
11548
333b20bb 11549DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 4, 0,
7ee72033 11550 doc: /* Read file name, prompting with PROMPT in directory DIR.
c061c855
GM
11551Use a file selection dialog.
11552Select DEFAULT-FILENAME in the dialog's file selection box, if
11553specified. Don't let the user enter a file name in the file
7ee72033
MB
11554selection dialog's entry field, if MUSTMATCH is non-nil. */)
11555 (prompt, dir, default_filename, mustmatch)
333b20bb
GM
11556 Lisp_Object prompt, dir, default_filename, mustmatch;
11557{
11558 int result;
0fe92f72 11559 struct frame *f = SELECTED_FRAME ();
333b20bb
GM
11560 Lisp_Object file = Qnil;
11561 Widget dialog, text, list, help;
11562 Arg al[10];
11563 int ac = 0;
11564 extern XtAppContext Xt_app_con;
333b20bb 11565 XmString dir_xmstring, pattern_xmstring;
65b21658 11566 int count = SPECPDL_INDEX ();
333b20bb
GM
11567 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5;
11568
11569 GCPRO5 (prompt, dir, default_filename, mustmatch, file);
b7826503
PJ
11570 CHECK_STRING (prompt);
11571 CHECK_STRING (dir);
333b20bb
GM
11572
11573 /* Prevent redisplay. */
11574 specbind (Qinhibit_redisplay, Qt);
11575
11576 BLOCK_INPUT;
11577
11578 /* Create the dialog with PROMPT as title, using DIR as initial
11579 directory and using "*" as pattern. */
11580 dir = Fexpand_file_name (dir, Qnil);
d5db4077 11581 dir_xmstring = XmStringCreateLocalized (SDATA (dir));
333b20bb 11582 pattern_xmstring = XmStringCreateLocalized ("*");
7273d100 11583
d5db4077 11584 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
333b20bb
GM
11585 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
11586 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
11587 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
11588 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
11589 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
11590 "fsb", al, ac);
11591 XmStringFree (dir_xmstring);
11592 XmStringFree (pattern_xmstring);
11593
11594 /* Add callbacks for OK and Cancel. */
11595 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
11596 (XtPointer) &result);
11597 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
11598 (XtPointer) &result);
a779d213
GM
11599 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
11600 (XtPointer) &result);
333b20bb
GM
11601
11602 /* Disable the help button since we can't display help. */
11603 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
11604 XtSetSensitive (help, False);
11605
7273d100 11606 /* Mark OK button as default. */
333b20bb
GM
11607 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
11608 XmNshowAsDefault, True, NULL);
11609
11610 /* If MUSTMATCH is non-nil, disable the file entry field of the
11611 dialog, so that the user must select a file from the files list
11612 box. We can't remove it because we wouldn't have a way to get at
11613 the result file name, then. */
11614 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
11615 if (!NILP (mustmatch))
11616 {
11617 Widget label;
11618 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
11619 XtSetSensitive (text, False);
11620 XtSetSensitive (label, False);
11621 }
11622
11623 /* Manage the dialog, so that list boxes get filled. */
11624 XtManageChild (dialog);
11625
11626 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
11627 must include the path for this to work. */
11628 list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
11629 if (STRINGP (default_filename))
11630 {
11631 XmString default_xmstring;
11632 int item_pos;
11633
11634 default_xmstring
d5db4077 11635 = XmStringCreateLocalized (SDATA (default_filename));
333b20bb
GM
11636
11637 if (!XmListItemExists (list, default_xmstring))
11638 {
11639 /* Add a new item if DEFAULT_FILENAME is not in the list. */
11640 XmListAddItem (list, default_xmstring, 0);
11641 item_pos = 0;
11642 }
11643 else
11644 item_pos = XmListItemPos (list, default_xmstring);
11645 XmStringFree (default_xmstring);
11646
11647 /* Select the item and scroll it into view. */
11648 XmListSelectPos (list, item_pos, True);
11649 XmListSetPos (list, item_pos);
11650 }
11651
563b384d
GM
11652 /* Process events until the user presses Cancel or OK. Block
11653 and unblock input here so that we get a chance of processing
11654 expose events. */
11655 UNBLOCK_INPUT;
03100098 11656 result = 0;
a779d213 11657 while (result == 0)
563b384d
GM
11658 {
11659 BLOCK_INPUT;
11660 XtAppProcessEvent (Xt_app_con, XtIMAll);
11661 UNBLOCK_INPUT;
11662 }
11663 BLOCK_INPUT;
03100098 11664
333b20bb
GM
11665 /* Get the result. */
11666 if (result == XmCR_OK)
11667 {
11668 XmString text;
11669 String data;
7273d100 11670
d1670063 11671 XtVaGetValues (dialog, XmNtextString, &text, NULL);
333b20bb
GM
11672 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
11673 XmStringFree (text);
11674 file = build_string (data);
11675 XtFree (data);
11676 }
11677 else
11678 file = Qnil;
11679
11680 /* Clean up. */
11681 XtUnmanageChild (dialog);
11682 XtDestroyWidget (dialog);
11683 UNBLOCK_INPUT;
11684 UNGCPRO;
11685
11686 /* Make "Cancel" equivalent to C-g. */
11687 if (NILP (file))
11688 Fsignal (Qquit, Qnil);
7273d100 11689
333b20bb
GM
11690 return unbind_to (count, file);
11691}
11692
11693#endif /* USE_MOTIF */
11694
333b20bb
GM
11695
11696\f
82bab41c
GM
11697/***********************************************************************
11698 Keyboard
11699 ***********************************************************************/
11700
11701#ifdef HAVE_XKBGETKEYBOARD
11702#include <X11/XKBlib.h>
11703#include <X11/keysym.h>
11704#endif
11705
11706DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
11707 Sx_backspace_delete_keys_p, 0, 1, 0,
7ee72033 11708 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
c061c855
GM
11709FRAME nil means use the selected frame.
11710Value is t if we know that both keys are present, and are mapped to the
7ee72033
MB
11711usual X keysyms. */)
11712 (frame)
82bab41c
GM
11713 Lisp_Object frame;
11714{
11715#ifdef HAVE_XKBGETKEYBOARD
11716 XkbDescPtr kb;
11717 struct frame *f = check_x_frame (frame);
11718 Display *dpy = FRAME_X_DISPLAY (f);
11719 Lisp_Object have_keys;
46f6a258 11720 int major, minor, op, event, error;
82bab41c
GM
11721
11722 BLOCK_INPUT;
46f6a258
GM
11723
11724 /* Check library version in case we're dynamically linked. */
11725 major = XkbMajorVersion;
11726 minor = XkbMinorVersion;
11727 if (!XkbLibraryVersion (&major, &minor))
c1efd260
GM
11728 {
11729 UNBLOCK_INPUT;
11730 return Qnil;
11731 }
46f6a258
GM
11732
11733 /* Check that the server supports XKB. */
11734 major = XkbMajorVersion;
11735 minor = XkbMinorVersion;
11736 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
c1efd260
GM
11737 {
11738 UNBLOCK_INPUT;
11739 return Qnil;
11740 }
7273d100 11741
46f6a258 11742 have_keys = Qnil;
c1efd260 11743 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
82bab41c
GM
11744 if (kb)
11745 {
11746 int delete_keycode = 0, backspace_keycode = 0, i;
c1efd260
GM
11747
11748 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
82bab41c 11749 {
c1efd260
GM
11750 for (i = kb->min_key_code;
11751 (i < kb->max_key_code
11752 && (delete_keycode == 0 || backspace_keycode == 0));
11753 ++i)
11754 {
d63931a2
GM
11755 /* The XKB symbolic key names can be seen most easily in
11756 the PS file generated by `xkbprint -label name
11757 $DISPLAY'. */
c1efd260
GM
11758 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
11759 delete_keycode = i;
11760 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
11761 backspace_keycode = i;
11762 }
11763
11764 XkbFreeNames (kb, 0, True);
82bab41c
GM
11765 }
11766
c1efd260 11767 XkbFreeClientMap (kb, 0, True);
7273d100 11768
82bab41c
GM
11769 if (delete_keycode
11770 && backspace_keycode
11771 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
11772 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
11773 have_keys = Qt;
11774 }
11775 UNBLOCK_INPUT;
11776 return have_keys;
11777#else /* not HAVE_XKBGETKEYBOARD */
11778 return Qnil;
11779#endif /* not HAVE_XKBGETKEYBOARD */
11780}
11781
11782
11783\f
333b20bb
GM
11784/***********************************************************************
11785 Initialization
11786 ***********************************************************************/
11787
11788void
11789syms_of_xfns ()
11790{
11791 /* This is zero if not using X windows. */
11792 x_in_use = 0;
11793
11794 /* The section below is built by the lisp expression at the top of the file,
11795 just above where these variables are declared. */
11796 /*&&& init symbols here &&&*/
11797 Qauto_raise = intern ("auto-raise");
11798 staticpro (&Qauto_raise);
11799 Qauto_lower = intern ("auto-lower");
11800 staticpro (&Qauto_lower);
f9942c9e
JB
11801 Qborder_color = intern ("border-color");
11802 staticpro (&Qborder_color);
11803 Qborder_width = intern ("border-width");
11804 staticpro (&Qborder_width);
11805 Qcursor_color = intern ("cursor-color");
11806 staticpro (&Qcursor_color);
dbc4e1c1
JB
11807 Qcursor_type = intern ("cursor-type");
11808 staticpro (&Qcursor_type);
f9942c9e
JB
11809 Qgeometry = intern ("geometry");
11810 staticpro (&Qgeometry);
f9942c9e
JB
11811 Qicon_left = intern ("icon-left");
11812 staticpro (&Qicon_left);
11813 Qicon_top = intern ("icon-top");
11814 staticpro (&Qicon_top);
11815 Qicon_type = intern ("icon-type");
11816 staticpro (&Qicon_type);
80534dd6
KH
11817 Qicon_name = intern ("icon-name");
11818 staticpro (&Qicon_name);
f9942c9e
JB
11819 Qinternal_border_width = intern ("internal-border-width");
11820 staticpro (&Qinternal_border_width);
11821 Qleft = intern ("left");
11822 staticpro (&Qleft);
1ab3d87e
RS
11823 Qright = intern ("right");
11824 staticpro (&Qright);
f9942c9e
JB
11825 Qmouse_color = intern ("mouse-color");
11826 staticpro (&Qmouse_color);
baaed68e
JB
11827 Qnone = intern ("none");
11828 staticpro (&Qnone);
f9942c9e
JB
11829 Qparent_id = intern ("parent-id");
11830 staticpro (&Qparent_id);
4701395c
KH
11831 Qscroll_bar_width = intern ("scroll-bar-width");
11832 staticpro (&Qscroll_bar_width);
8af1d7ca
JB
11833 Qsuppress_icon = intern ("suppress-icon");
11834 staticpro (&Qsuppress_icon);
01f1ba30 11835 Qundefined_color = intern ("undefined-color");
f9942c9e 11836 staticpro (&Qundefined_color);
a3c87d4e
JB
11837 Qvertical_scroll_bars = intern ("vertical-scroll-bars");
11838 staticpro (&Qvertical_scroll_bars);
49795535
JB
11839 Qvisibility = intern ("visibility");
11840 staticpro (&Qvisibility);
f9942c9e
JB
11841 Qwindow_id = intern ("window-id");
11842 staticpro (&Qwindow_id);
2cbebefb
RS
11843 Qouter_window_id = intern ("outer-window-id");
11844 staticpro (&Qouter_window_id);
f9942c9e
JB
11845 Qx_frame_parameter = intern ("x-frame-parameter");
11846 staticpro (&Qx_frame_parameter);
9ef48a9d
RS
11847 Qx_resource_name = intern ("x-resource-name");
11848 staticpro (&Qx_resource_name);
4fe1de12
RS
11849 Quser_position = intern ("user-position");
11850 staticpro (&Quser_position);
11851 Quser_size = intern ("user-size");
11852 staticpro (&Quser_size);
333b20bb
GM
11853 Qscroll_bar_foreground = intern ("scroll-bar-foreground");
11854 staticpro (&Qscroll_bar_foreground);
11855 Qscroll_bar_background = intern ("scroll-bar-background");
11856 staticpro (&Qscroll_bar_background);
d62c8769
GM
11857 Qscreen_gamma = intern ("screen-gamma");
11858 staticpro (&Qscreen_gamma);
563b67aa
GM
11859 Qline_spacing = intern ("line-spacing");
11860 staticpro (&Qline_spacing);
7c7ff7f5
GM
11861 Qcenter = intern ("center");
11862 staticpro (&Qcenter);
96db09e4
KH
11863 Qcompound_text = intern ("compound-text");
11864 staticpro (&Qcompound_text);
ae782866
GM
11865 Qcancel_timer = intern ("cancel-timer");
11866 staticpro (&Qcancel_timer);
ea0a1f53
GM
11867 Qwait_for_wm = intern ("wait-for-wm");
11868 staticpro (&Qwait_for_wm);
49d41073
EZ
11869 Qfullscreen = intern ("fullscreen");
11870 staticpro (&Qfullscreen);
11871 Qfullwidth = intern ("fullwidth");
11872 staticpro (&Qfullwidth);
11873 Qfullheight = intern ("fullheight");
11874 staticpro (&Qfullheight);
11875 Qfullboth = intern ("fullboth");
11876 staticpro (&Qfullboth);
f9942c9e
JB
11877 /* This is the end of symbol initialization. */
11878
58cad5ed
KH
11879 /* Text property `display' should be nonsticky by default. */
11880 Vtext_property_default_nonsticky
11881 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
11882
11883
333b20bb
GM
11884 Qlaplace = intern ("laplace");
11885 staticpro (&Qlaplace);
4a8e312c
GM
11886 Qemboss = intern ("emboss");
11887 staticpro (&Qemboss);
11888 Qedge_detection = intern ("edge-detection");
11889 staticpro (&Qedge_detection);
11890 Qheuristic = intern ("heuristic");
11891 staticpro (&Qheuristic);
11892 QCmatrix = intern (":matrix");
11893 staticpro (&QCmatrix);
11894 QCcolor_adjustment = intern (":color-adjustment");
11895 staticpro (&QCcolor_adjustment);
11896 QCmask = intern (":mask");
11897 staticpro (&QCmask);
7273d100 11898
a367641f
RS
11899 Qface_set_after_frame_default = intern ("face-set-after-frame-default");
11900 staticpro (&Qface_set_after_frame_default);
11901
01f1ba30
JB
11902 Fput (Qundefined_color, Qerror_conditions,
11903 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
11904 Fput (Qundefined_color, Qerror_message,
11905 build_string ("Undefined color"));
11906
f9942c9e
JB
11907 init_x_parm_symbols ();
11908
7ee72033
MB
11909 DEFVAR_BOOL ("cross-disabled-images", &cross_disabled_images,
11910 doc: /* Non-nil means always draw a cross over disabled images.
c061c855
GM
11911Disabled images are those having an `:conversion disabled' property.
11912A cross is always drawn on black & white displays. */);
14819cb3
GM
11913 cross_disabled_images = 0;
11914
7ee72033
MB
11915 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
11916 doc: /* List of directories to search for bitmap files for X. */);
e241c09b 11917 Vx_bitmap_file_path = decode_env_path ((char *) 0, PATH_BITMAPS);
f1c7b5a6 11918
7ee72033
MB
11919 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
11920 doc: /* The shape of the pointer when over text.
c061c855
GM
11921Changing the value does not affect existing frames
11922unless you set the mouse color. */);
01f1ba30
JB
11923 Vx_pointer_shape = Qnil;
11924
7ee72033
MB
11925 DEFVAR_LISP ("x-resource-name", &Vx_resource_name,
11926 doc: /* The name Emacs uses to look up X resources.
c061c855
GM
11927`x-get-resource' uses this as the first component of the instance name
11928when requesting resource values.
11929Emacs initially sets `x-resource-name' to the name under which Emacs
11930was invoked, or to the value specified with the `-name' or `-rn'
11931switches, if present.
11932
11933It may be useful to bind this variable locally around a call
11934to `x-get-resource'. See also the variable `x-resource-class'. */);
d387c960 11935 Vx_resource_name = Qnil;
ac63d3d6 11936
7ee72033
MB
11937 DEFVAR_LISP ("x-resource-class", &Vx_resource_class,
11938 doc: /* The class Emacs uses to look up X resources.
c061c855
GM
11939`x-get-resource' uses this as the first component of the instance class
11940when requesting resource values.
11941
11942Emacs initially sets `x-resource-class' to "Emacs".
11943
11944Setting this variable permanently is not a reasonable thing to do,
11945but binding this variable locally around a call to `x-get-resource'
11946is a reasonable practice. See also the variable `x-resource-name'. */);
498e9ac3
RS
11947 Vx_resource_class = build_string (EMACS_CLASS);
11948
ca0ecbf5 11949#if 0 /* This doesn't really do anything. */
7ee72033
MB
11950 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
11951 doc: /* The shape of the pointer when not over text.
c061c855
GM
11952This variable takes effect when you create a new frame
11953or when you set the mouse color. */);
af01ef26 11954#endif
01f1ba30
JB
11955 Vx_nontext_pointer_shape = Qnil;
11956
7ee72033
MB
11957 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
11958 doc: /* The shape of the pointer when Emacs is busy.
c061c855
GM
11959This variable takes effect when you create a new frame
11960or when you set the mouse color. */);
0af913d7 11961 Vx_hourglass_pointer_shape = Qnil;
333b20bb 11962
7ee72033
MB
11963 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
11964 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
0af913d7 11965 display_hourglass_p = 1;
7273d100 11966
7ee72033
MB
11967 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
11968 doc: /* *Seconds to wait before displaying an hourglass pointer.
c061c855 11969Value must be an integer or float. */);
0af913d7 11970 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
4ae9a85e 11971
ca0ecbf5 11972#if 0 /* This doesn't really do anything. */
7ee72033
MB
11973 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
11974 doc: /* The shape of the pointer when over the mode line.
c061c855
GM
11975This variable takes effect when you create a new frame
11976or when you set the mouse color. */);
af01ef26 11977#endif
01f1ba30
JB
11978 Vx_mode_pointer_shape = Qnil;
11979
d3b06468 11980 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
7ee72033
MB
11981 &Vx_sensitive_text_pointer_shape,
11982 doc: /* The shape of the pointer when over mouse-sensitive text.
c061c855
GM
11983This variable takes effect when you create a new frame
11984or when you set the mouse color. */);
ca0ecbf5 11985 Vx_sensitive_text_pointer_shape = Qnil;
95f80c78 11986
8fb4ec9c 11987 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
7ee72033
MB
11988 &Vx_window_horizontal_drag_shape,
11989 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
c061c855
GM
11990This variable takes effect when you create a new frame
11991or when you set the mouse color. */);
8fb4ec9c
GM
11992 Vx_window_horizontal_drag_shape = Qnil;
11993
7ee72033
MB
11994 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
11995 doc: /* A string indicating the foreground color of the cursor box. */);
01f1ba30
JB
11996 Vx_cursor_fore_pixel = Qnil;
11997
7ee72033
MB
11998 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
11999 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
c061c855 12000Text larger than this is clipped. */);
d63931a2 12001 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
7273d100 12002
7ee72033
MB
12003 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
12004 doc: /* Non-nil if no X window manager is in use.
c061c855
GM
12005Emacs doesn't try to figure this out; this is always nil
12006unless you set it to something else. */);
2d38195d
RS
12007 /* We don't have any way to find this out, so set it to nil
12008 and maybe the user would like to set it to t. */
12009 Vx_no_window_manager = Qnil;
1d3dac41 12010
942ea06d 12011 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
7ee72033
MB
12012 &Vx_pixel_size_width_font_regexp,
12013 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
c061c855
GM
12014
12015Since Emacs gets width of a font matching with this regexp from
12016PIXEL_SIZE field of the name, font finding mechanism gets faster for
12017such a font. This is especially effective for such large fonts as
12018Chinese, Japanese, and Korean. */);
942ea06d
KH
12019 Vx_pixel_size_width_font_regexp = Qnil;
12020
7ee72033
MB
12021 DEFVAR_LISP ("image-cache-eviction-delay", &Vimage_cache_eviction_delay,
12022 doc: /* Time after which cached images are removed from the cache.
c061c855
GM
12023When an image has not been displayed this many seconds, remove it
12024from the image cache. Value must be an integer or nil with nil
12025meaning don't clear the cache. */);
fcf431dc 12026 Vimage_cache_eviction_delay = make_number (30 * 60);
333b20bb 12027
1d3dac41 12028#ifdef USE_X_TOOLKIT
6f3f6a8d 12029 Fprovide (intern ("x-toolkit"), Qnil);
5b827abb 12030#ifdef USE_MOTIF
6f3f6a8d 12031 Fprovide (intern ("motif"), Qnil);
fc2cdd9a 12032
7ee72033
MB
12033 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
12034 doc: /* Version info for LessTif/Motif. */);
fc2cdd9a
GM
12035 Vmotif_version_string = build_string (XmVERSION_STRING);
12036#endif /* USE_MOTIF */
12037#endif /* USE_X_TOOLKIT */
01f1ba30 12038
01f1ba30 12039 defsubr (&Sx_get_resource);
333b20bb
GM
12040
12041 /* X window properties. */
12042 defsubr (&Sx_change_window_property);
12043 defsubr (&Sx_delete_window_property);
12044 defsubr (&Sx_window_property);
12045
2d764c78 12046 defsubr (&Sxw_display_color_p);
d0c9d219 12047 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
12048 defsubr (&Sxw_color_defined_p);
12049 defsubr (&Sxw_color_values);
9d317b2c 12050 defsubr (&Sx_server_max_request_size);
41beb8fc
RS
12051 defsubr (&Sx_server_vendor);
12052 defsubr (&Sx_server_version);
12053 defsubr (&Sx_display_pixel_width);
12054 defsubr (&Sx_display_pixel_height);
12055 defsubr (&Sx_display_mm_width);
12056 defsubr (&Sx_display_mm_height);
12057 defsubr (&Sx_display_screens);
12058 defsubr (&Sx_display_planes);
12059 defsubr (&Sx_display_color_cells);
12060 defsubr (&Sx_display_visual_class);
12061 defsubr (&Sx_display_backing_store);
12062 defsubr (&Sx_display_save_under);
8af1d7ca 12063 defsubr (&Sx_parse_geometry);
f676886a 12064 defsubr (&Sx_create_frame);
01f1ba30 12065 defsubr (&Sx_open_connection);
08a90d6a
RS
12066 defsubr (&Sx_close_connection);
12067 defsubr (&Sx_display_list);
01f1ba30 12068 defsubr (&Sx_synchronize);
3decc1e7 12069 defsubr (&Sx_focus_frame);
82bab41c 12070 defsubr (&Sx_backspace_delete_keys_p);
7273d100 12071
942ea06d
KH
12072 /* Setting callback functions for fontset handler. */
12073 get_font_info_func = x_get_font_info;
333b20bb
GM
12074
12075#if 0 /* This function pointer doesn't seem to be used anywhere.
12076 And the pointer assigned has the wrong type, anyway. */
942ea06d 12077 list_fonts_func = x_list_fonts;
333b20bb 12078#endif
7273d100 12079
942ea06d 12080 load_font_func = x_load_font;
bc1958c4 12081 find_ccl_program_func = x_find_ccl_program;
942ea06d
KH
12082 query_font_func = x_query_font;
12083 set_frame_fontset_func = x_set_font;
12084 check_window_system_func = check_x;
333b20bb
GM
12085
12086 /* Images. */
12087 Qxbm = intern ("xbm");
12088 staticpro (&Qxbm);
d2dc8167
GM
12089 QCconversion = intern (":conversion");
12090 staticpro (&QCconversion);
333b20bb
GM
12091 QCheuristic_mask = intern (":heuristic-mask");
12092 staticpro (&QCheuristic_mask);
12093 QCcolor_symbols = intern (":color-symbols");
12094 staticpro (&QCcolor_symbols);
333b20bb
GM
12095 QCascent = intern (":ascent");
12096 staticpro (&QCascent);
12097 QCmargin = intern (":margin");
12098 staticpro (&QCmargin);
12099 QCrelief = intern (":relief");
12100 staticpro (&QCrelief);
fcf431dc
GM
12101 Qpostscript = intern ("postscript");
12102 staticpro (&Qpostscript);
333b20bb
GM
12103 QCloader = intern (":loader");
12104 staticpro (&QCloader);
12105 QCbounding_box = intern (":bounding-box");
12106 staticpro (&QCbounding_box);
12107 QCpt_width = intern (":pt-width");
12108 staticpro (&QCpt_width);
12109 QCpt_height = intern (":pt-height");
12110 staticpro (&QCpt_height);
3ccff1e3
GM
12111 QCindex = intern (":index");
12112 staticpro (&QCindex);
333b20bb
GM
12113 Qpbm = intern ("pbm");
12114 staticpro (&Qpbm);
12115
12116#if HAVE_XPM
12117 Qxpm = intern ("xpm");
12118 staticpro (&Qxpm);
12119#endif
7273d100 12120
333b20bb
GM
12121#if HAVE_JPEG
12122 Qjpeg = intern ("jpeg");
12123 staticpro (&Qjpeg);
7273d100 12124#endif
333b20bb
GM
12125
12126#if HAVE_TIFF
12127 Qtiff = intern ("tiff");
12128 staticpro (&Qtiff);
7273d100 12129#endif
333b20bb
GM
12130
12131#if HAVE_GIF
12132 Qgif = intern ("gif");
12133 staticpro (&Qgif);
12134#endif
12135
12136#if HAVE_PNG
12137 Qpng = intern ("png");
12138 staticpro (&Qpng);
12139#endif
12140
12141 defsubr (&Sclear_image_cache);
42677916 12142 defsubr (&Simage_size);
b243755a 12143 defsubr (&Simage_mask_p);
333b20bb 12144
0af913d7
GM
12145 hourglass_atimer = NULL;
12146 hourglass_shown_p = 0;
333b20bb
GM
12147
12148 defsubr (&Sx_show_tip);
12149 defsubr (&Sx_hide_tip);
333b20bb 12150 tip_timer = Qnil;
44b5a125
GM
12151 staticpro (&tip_timer);
12152 tip_frame = Qnil;
12153 staticpro (&tip_frame);
333b20bb 12154
06d62053
GM
12155 last_show_tip_args = Qnil;
12156 staticpro (&last_show_tip_args);
12157
333b20bb
GM
12158#ifdef USE_MOTIF
12159 defsubr (&Sx_file_dialog);
12160#endif
12161}
12162
12163
12164void
12165init_xfns ()
12166{
12167 image_types = NULL;
12168 Vimage_types = Qnil;
7273d100 12169
333b20bb
GM
12170 define_image_type (&xbm_type);
12171 define_image_type (&gs_type);
12172 define_image_type (&pbm_type);
7273d100 12173
333b20bb
GM
12174#if HAVE_XPM
12175 define_image_type (&xpm_type);
12176#endif
7273d100 12177
333b20bb
GM
12178#if HAVE_JPEG
12179 define_image_type (&jpeg_type);
12180#endif
7273d100 12181
333b20bb
GM
12182#if HAVE_TIFF
12183 define_image_type (&tiff_type);
12184#endif
7273d100 12185
333b20bb
GM
12186#if HAVE_GIF
12187 define_image_type (&gif_type);
12188#endif
7273d100 12189
333b20bb
GM
12190#if HAVE_PNG
12191 define_image_type (&png_type);
12192#endif
01f1ba30
JB
12193}
12194
12195#endif /* HAVE_X_WINDOWS */