Support resizing frames and windows pixelwise.
[bpt/emacs.git] / src / xfns.c
CommitLineData
01f1ba30 1/* Functions for the X window system.
e9bffc61 2
ab422c4d 3Copyright (C) 1989, 1992-2013 Free Software Foundation, Inc.
01f1ba30
JB
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
01f1ba30 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
01f1ba30
JB
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
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
01f1ba30 19
c389a86d 20#include <config.h>
333b20bb 21#include <stdio.h>
d62c8769 22#include <math.h>
3ecaf7e5 23#include <unistd.h>
3ecaf7e5 24
01f1ba30
JB
25#include "lisp.h"
26#include "xterm.h"
f676886a 27#include "frame.h"
01f1ba30 28#include "window.h"
e5560ff7 29#include "character.h"
01f1ba30 30#include "buffer.h"
58cad5ed 31#include "intervals.h"
01f1ba30 32#include "dispextern.h"
1f98fa48 33#include "keyboard.h"
9ac0d9e0 34#include "blockinput.h"
57bda87a 35#include <epaths.h>
942ea06d 36#include "charset.h"
96db09e4 37#include "coding.h"
942ea06d 38#include "fontset.h"
333b20bb
GM
39#include "systime.h"
40#include "termhooks.h"
4ae9a85e 41#include "atimer.h"
28d440ab 42#include "termchar.h"
02ed2ea8 43#include "font.h"
02ed2ea8 44
63cec32f
GM
45#include <sys/types.h>
46#include <sys/stat.h>
01f1ba30 47
0505a740 48#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
ef493a27
RS
49#include "bitmaps/gray.xbm"
50#else
dbc4e1c1 51#include <X11/bitmaps/gray>
ef493a27 52#endif
dbc4e1c1 53
1b9ac145
AS
54#include "xsettings.h"
55
4e3f9230
YM
56#ifdef HAVE_XRANDR
57#include <X11/extensions/Xrandr.h>
58#endif
59#ifdef HAVE_XINERAMA
60#include <X11/extensions/Xinerama.h>
61#endif
62
488dd4c4
JD
63#ifdef USE_GTK
64#include "gtkutil.h"
65#endif
66
9ef48a9d
RS
67#ifdef USE_X_TOOLKIT
68#include <X11/Shell.h>
69
398ffa92 70#ifndef USE_MOTIF
8ab9202e
CY
71#ifdef HAVE_XAW3D
72#include <X11/Xaw3d/Paned.h>
a6ec6cfb 73#include <X11/Xaw3d/Label.h>
8ab9202e 74#else /* !HAVE_XAW3D */
9ef48a9d
RS
75#include <X11/Xaw/Paned.h>
76#include <X11/Xaw/Label.h>
8ab9202e 77#endif /* HAVE_XAW3D */
398ffa92 78#endif /* USE_MOTIF */
9ef48a9d
RS
79
80#ifdef USG
81#undef USG /* ####KLUDGE for Solaris 2.2 and up */
82#include <X11/Xos.h>
83#define USG
dc5ddd85
PE
84#ifdef USG /* Pacify gcc -Wunused-macros. */
85#endif
9ef48a9d
RS
86#else
87#include <X11/Xos.h>
88#endif
89
90#include "widget.h"
91
92#include "../lwlib/lwlib.h"
93
333b20bb
GM
94#ifdef USE_MOTIF
95#include <Xm/Xm.h>
96#include <Xm/DialogS.h>
97#include <Xm/FileSB.h>
dee186b6
J
98#include <Xm/List.h>
99#include <Xm/TextF.h>
333b20bb
GM
100#endif
101
eec47d6b
DN
102#ifdef USE_LUCID
103#include "../lwlib/xlwmenu.h"
104#endif
105
5e617bc2 106#if !defined (NO_EDITRES)
6c32dd68 107#define HACK_EDITRES
764430a3 108extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
ee6bacd4 109#endif /* not defined NO_EDITRES */
6c32dd68 110
333b20bb
GM
111/* Unique id counter for widgets created by the Lucid Widget Library. */
112
6c32dd68
PR
113extern LWLIB_ID widget_id_tick;
114
fc2cdd9a
GM
115#ifdef USE_MOTIF
116
fc2cdd9a
GM
117#endif /* USE_MOTIF */
118
9ef48a9d
RS
119#endif /* USE_X_TOOLKIT */
120
6b61353c
KH
121#ifdef USE_GTK
122
6b61353c
KH
123#endif /* USE_GTK */
124
9d317b2c 125#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
9d317b2c 126
955cbe7b
PE
127static Lisp_Object Qsuppress_icon;
128static Lisp_Object Qundefined_color;
129static Lisp_Object Qcompound_text, Qcancel_timer;
581e51e8 130Lisp_Object Qfont_param;
01f1ba30 131
e509cfa6 132#ifdef GLYPH_DEBUG
13464394
PE
133static ptrdiff_t image_cache_refcount;
134static int dpyinfo_refcount;
eaf1eea9
GM
135#endif
136
e4cebfca
PE
137static struct x_display_info *x_display_info_for_name (Lisp_Object);
138
7e59217d 139/* Let the user specify an X display with a Lisp object.
708e05dc 140 OBJECT may be nil, a frame or a terminal object.
b9dc4443
RS
141 nil stands for the selected frame--or, if that is not an X frame,
142 the first X display on the list. */
143
7c0d3ed8 144struct x_display_info *
971de7fb 145check_x_display_info (Lisp_Object object)
b9dc4443 146{
8ec8a5ec 147 struct x_display_info *dpyinfo = NULL;
177c0ea7 148
7e59217d 149 if (NILP (object))
b9dc4443 150 {
0fe92f72 151 struct frame *sf = XFRAME (selected_frame);
177c0ea7 152
0fe92f72 153 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
aad3612f 154 dpyinfo = FRAME_DISPLAY_INFO (sf);
b9dc4443 155 else if (x_display_list != 0)
8ec8a5ec 156 dpyinfo = x_display_list;
b9dc4443
RS
157 else
158 error ("X windows are not in use or not initialized");
159 }
708e05dc 160 else if (TERMINALP (object))
b6660415 161 {
fd0f53a9 162 struct terminal *t = get_terminal (object, 1);
b6660415 163
6ed8eeff 164 if (t->type != output_x_window)
5ba8bf35 165 error ("Terminal %d is not an X display", t->id);
b6660415 166
6ed8eeff 167 dpyinfo = t->display_info.x;
b6660415 168 }
7e59217d
KL
169 else if (STRINGP (object))
170 dpyinfo = x_display_info_for_name (object);
b9dc4443
RS
171 else
172 {
a10c8269 173 struct frame *f = decode_window_system_frame (object);
aad3612f 174 dpyinfo = FRAME_DISPLAY_INFO (f);
b9dc4443 175 }
8ec8a5ec
GM
176
177 return dpyinfo;
b9dc4443 178}
333b20bb 179
08a90d6a 180/* Store the screen positions of frame F into XPTR and YPTR.
e9445337
RS
181 These are the positions of the containing window manager window,
182 not Emacs's own window. */
183
184void
a10c8269 185x_real_positions (struct frame *f, int *xptr, int *yptr)
e9445337 186{
8ffc96f5 187 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
49d41073
EZ
188 int real_x = 0, real_y = 0;
189 int had_errors = 0;
190 Window win = f->output_data.x->parent_desc;
d75c9992
JD
191 Atom actual_type;
192 unsigned long actual_size, bytes_remaining;
06b0c8a0 193 int rc, actual_format;
aad3612f 194 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
d75c9992
JD
195 long max_len = 400;
196 Display *dpy = FRAME_X_DISPLAY (f);
197 unsigned char *tmp_data = NULL;
198 Atom target_type = XA_CARDINAL;
e9445337 199
4d7e6e51 200 block_input ();
49d41073 201
d75c9992 202 x_catch_errors (dpy);
043835a3 203
31887d45 204 if (win == dpyinfo->root_window)
49d41073
EZ
205 win = FRAME_OUTER_WINDOW (f);
206
207 /* This loop traverses up the containment tree until we hit the root
208 window. Window managers may intersect many windows between our window
209 and the root window. The window we find just before the root window
210 should be the outer WM window. */
211 for (;;)
e9445337 212 {
49d41073
EZ
213 Window wm_window, rootw;
214 Window *tmp_children;
215 unsigned int tmp_nchildren;
e7161ad9 216 int success;
ca7bac79 217
e7161ad9
RS
218 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
219 &wm_window, &tmp_children, &tmp_nchildren);
08a90d6a 220
49d41073 221 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
08a90d6a 222
e7161ad9
RS
223 /* Don't free tmp_children if XQueryTree failed. */
224 if (! success)
225 break;
226
98c6f1e3 227 XFree (tmp_children);
e7161ad9 228
49d41073
EZ
229 if (wm_window == rootw || had_errors)
230 break;
08a90d6a 231
49d41073
EZ
232 win = wm_window;
233 }
177c0ea7 234
49d41073
EZ
235 if (! had_errors)
236 {
47c53789 237 unsigned int ign;
49d41073 238 Window child, rootw;
177c0ea7 239
49d41073
EZ
240 /* Get the real coordinates for the WM window upper left corner */
241 XGetGeometry (FRAME_X_DISPLAY (f), win,
242 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
243
244 /* Translate real coordinates to coordinates relative to our
245 window. For our window, the upper left corner is 0, 0.
246 Since the upper left corner of the WM window is outside
247 our window, win_x and win_y will be negative:
248
249 ------------------ ---> x
250 | title |
251 | ----------------- v y
252 | | our window
253 */
8a07bba0 254 XTranslateCoordinates (FRAME_X_DISPLAY (f),
e9445337 255
8a07bba0 256 /* From-window, to-window. */
aad3612f 257 FRAME_DISPLAY_INFO (f)->root_window,
49d41073 258 FRAME_X_WINDOW (f),
e9445337 259
8a07bba0 260 /* From-position, to-position. */
49d41073 261 real_x, real_y, &win_x, &win_y,
08a90d6a 262
8a07bba0
RS
263 /* Child of win. */
264 &child);
e9445337 265
49d41073 266 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
845e9d85 267 {
49d41073
EZ
268 outer_x = win_x;
269 outer_y = win_y;
845e9d85 270 }
49d41073
EZ
271 else
272 {
273 XTranslateCoordinates (FRAME_X_DISPLAY (f),
ca7bac79 274
49d41073 275 /* From-window, to-window. */
aad3612f 276 FRAME_DISPLAY_INFO (f)->root_window,
49d41073 277 FRAME_OUTER_WINDOW (f),
177c0ea7 278
49d41073
EZ
279 /* From-position, to-position. */
280 real_x, real_y, &outer_x, &outer_y,
177c0ea7 281
49d41073
EZ
282 /* Child of win. */
283 &child);
be786000 284 }
08a90d6a 285
49d41073
EZ
286 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
287 }
177c0ea7 288
31887d45
JD
289
290 if (dpyinfo->root_window == f->output_data.x->parent_desc)
291 {
292 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
293 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
294 0, max_len, False, target_type,
295 &actual_type, &actual_format, &actual_size,
296 &bytes_remaining, &tmp_data);
297
298 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
299 && actual_size == 4 && actual_format == 32)
300 {
717c30e0 301 unsigned int ign;
31887d45 302 Window rootw;
c0098065 303 long *fe = (long *)tmp_data;
31887d45
JD
304
305 XGetGeometry (FRAME_X_DISPLAY (f), win,
306 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
31887d45
JD
307 outer_x = -fe[0];
308 outer_y = -fe[2];
309 real_x -= fe[0];
310 real_y -= fe[2];
311 }
312 }
313
314 if (tmp_data) XFree (tmp_data);
315
4545fa20 316 x_uncatch_errors ();
177c0ea7 317
4d7e6e51 318 unblock_input ();
49d41073
EZ
319
320 if (had_errors) return;
177c0ea7 321
be786000
KS
322 f->x_pixels_diff = -win_x;
323 f->y_pixels_diff = -win_y;
324
325 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
326 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
49d41073
EZ
327
328 *xptr = real_x;
329 *yptr = real_y;
e9445337
RS
330}
331
01f1ba30 332\f
82978295 333
d62c8769
GM
334
335/* Gamma-correct COLOR on frame F. */
336
337void
971de7fb 338gamma_correct (struct frame *f, XColor *color)
d62c8769
GM
339{
340 if (f->gamma)
341 {
342 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
343 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
344 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
345 }
346}
347
348
7b746c38 349/* Decide if color named COLOR_NAME is valid for use on frame F. If
578098f3
PE
350 so, return the RGB values in COLOR. If ALLOC_P,
351 allocate the color. Value is false if COLOR_NAME is invalid, or
7b746c38 352 no color could be allocated. */
e12d55b2 353
578098f3 354bool
eec47d6b 355x_defined_color (struct frame *f, const char *color_name,
578098f3 356 XColor *color, bool alloc_p)
01f1ba30 357{
578098f3 358 bool success_p = 0;
7b746c38
GM
359 Display *dpy = FRAME_X_DISPLAY (f);
360 Colormap cmap = FRAME_X_COLORMAP (f);
01f1ba30 361
4d7e6e51 362 block_input ();
3a46642b
J
363#ifdef USE_GTK
364 success_p = xg_check_special_colors (f, color_name, color);
365#endif
366 if (!success_p)
578098f3 367 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
7b746c38
GM
368 if (success_p && alloc_p)
369 success_p = x_alloc_nearest_color (f, cmap, color);
4d7e6e51 370 unblock_input ();
01f1ba30 371
7b746c38 372 return success_p;
01f1ba30
JB
373}
374
9b2956e2
GM
375
376/* Return the pixel color value for color COLOR_NAME on frame F. If F
377 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
378 Signal an error if color can't be allocated. */
01f1ba30 379
f78faa98 380static int
a10c8269 381x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
01f1ba30 382{
b9dc4443 383 XColor cdef;
01f1ba30 384
b7826503 385 CHECK_STRING (color_name);
01f1ba30 386
9b2956e2
GM
387#if 0 /* Don't do this. It's wrong when we're not using the default
388 colormap, it makes freeing difficult, and it's probably not
389 an important optimization. */
d5db4077 390 if (strcmp (SDATA (color_name), "black") == 0)
b9dc4443 391 return BLACK_PIX_DEFAULT (f);
d5db4077 392 else if (strcmp (SDATA (color_name), "white") == 0)
b9dc4443 393 return WHITE_PIX_DEFAULT (f);
9b2956e2 394#endif
01f1ba30 395
9b2956e2 396 /* Return MONO_COLOR for monochrome frames. */
aad3612f 397 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
9b2956e2 398 return mono_color;
01f1ba30 399
2d764c78 400 /* x_defined_color is responsible for coping with failures
95626e11 401 by looking for a near-miss. */
42a5b22f 402 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
95626e11
RS
403 return cdef.pixel;
404
93217cef 405 signal_error ("Undefined color", color_name);
01f1ba30 406}
9b2956e2
GM
407
408
01f1ba30 409\f
ea0a1f53
GM
410/* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
411 the previous value of that parameter, NEW_VALUE is the new value.
412 See also the comment of wait_for_wm in struct x_output. */
413
414static void
971de7fb 415x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
ea0a1f53
GM
416{
417 f->output_data.x->wait_for_wm = !NILP (new_value);
418}
419
bfeabdc3
JD
420static void
421x_set_tool_bar_position (struct frame *f,
422 Lisp_Object new_value,
423 Lisp_Object old_value)
424{
425 if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
426 && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
427 return;
428 if (EQ (new_value, old_value)) return;
429
430#ifdef USE_GTK
18e27ea8
PE
431 xg_change_toolbar_position (f, new_value);
432 fset_tool_bar_position (f, new_value);
bfeabdc3
JD
433#endif
434}
435
993d0721
JB
436#ifdef USE_GTK
437
465aa50a
JD
438/* Set icon from FILE for frame F. By using GTK functions the icon
439 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
993d0721
JB
440
441int
a10c8269 442xg_set_icon (struct frame *f, Lisp_Object file)
993d0721 443{
465aa50a
JD
444 int result = 0;
445 Lisp_Object found;
993d0721 446
465aa50a
JD
447 found = x_find_image_file (file);
448
449 if (! NILP (found))
450 {
451 GdkPixbuf *pixbuf;
452 GError *err = NULL;
51b59d79 453 char *filename = SSDATA (found);
4d7e6e51 454 block_input ();
465aa50a
JD
455
456 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
457
458 if (pixbuf)
459 {
460 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
461 pixbuf);
462 g_object_unref (pixbuf);
463
464 result = 1;
465 }
466 else
467 g_error_free (err);
468
4d7e6e51 469 unblock_input ();
465aa50a
JD
470 }
471
465aa50a 472 return result;
993d0721 473}
9f7b984b
CY
474
475int
a10c8269 476xg_set_icon_from_xpm_data (struct frame *f, const char **data)
9f7b984b 477{
648801d1 478 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
9f7b984b
CY
479
480 if (!pixbuf)
47c53789 481 return 0;
9f7b984b 482
47c53789 483 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
9f7b984b
CY
484 g_object_unref (pixbuf);
485 return 1;
486}
993d0721
JB
487#endif /* USE_GTK */
488
ea0a1f53 489
f676886a 490/* Functions called only from `x_set_frame_param'
01f1ba30
JB
491 to set individual parameters.
492
fe24a618 493 If FRAME_X_WINDOW (f) is 0,
f676886a 494 the frame is being created and its X-window does not exist yet.
01f1ba30
JB
495 In that case, just record the parameter's new value
496 in the standard place; do not attempt to change the window. */
497
e4cebfca 498static void
971de7fb 499x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 500{
09393d07
GM
501 struct x_output *x = f->output_data.x;
502 unsigned long fg, old_fg;
a76206dc 503
09393d07 504 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ce593f6e
KL
505 old_fg = FRAME_FOREGROUND_PIXEL (f);
506 FRAME_FOREGROUND_PIXEL (f) = fg;
a76206dc 507
fe24a618 508 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 509 {
09393d07 510 Display *dpy = FRAME_X_DISPLAY (f);
177c0ea7 511
4d7e6e51 512 block_input ();
09393d07
GM
513 XSetForeground (dpy, x->normal_gc, fg);
514 XSetBackground (dpy, x->reverse_gc, fg);
36d42089 515
09393d07
GM
516 if (x->cursor_pixel == old_fg)
517 {
518 unload_color (f, x->cursor_pixel);
519 x->cursor_pixel = x_copy_color (f, fg);
520 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
521 }
177c0ea7 522
4d7e6e51 523 unblock_input ();
177c0ea7 524
05c8abbe 525 update_face_from_frame_parameter (f, Qforeground_color, arg);
177c0ea7 526
179956b9 527 if (FRAME_VISIBLE_P (f))
f676886a 528 redraw_frame (f);
01f1ba30 529 }
177c0ea7 530
09393d07 531 unload_color (f, old_fg);
01f1ba30
JB
532}
533
e4cebfca 534static void
971de7fb 535x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 536{
09393d07
GM
537 struct x_output *x = f->output_data.x;
538 unsigned long bg;
01f1ba30 539
09393d07 540 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
ce593f6e
KL
541 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
542 FRAME_BACKGROUND_PIXEL (f) = bg;
a76206dc 543
fe24a618 544 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 545 {
09393d07 546 Display *dpy = FRAME_X_DISPLAY (f);
177c0ea7 547
4d7e6e51 548 block_input ();
09393d07
GM
549 XSetBackground (dpy, x->normal_gc, bg);
550 XSetForeground (dpy, x->reverse_gc, bg);
551 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
552 XSetForeground (dpy, x->cursor_gc, bg);
553
488dd4c4
JD
554#ifdef USE_GTK
555 xg_set_background_color (f, bg);
556#endif
557
f76e0368
GM
558#ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
559 toolkit scroll bars. */
560 {
561 Lisp_Object bar;
562 for (bar = FRAME_SCROLL_BARS (f);
563 !NILP (bar);
564 bar = XSCROLL_BAR (bar)->next)
565 {
056ce195 566 Window window = XSCROLL_BAR (bar)->x_window;
f76e0368
GM
567 XSetWindowBackground (dpy, window, bg);
568 }
569 }
570#endif /* USE_TOOLKIT_SCROLL_BARS */
01f1ba30 571
4d7e6e51 572 unblock_input ();
05c8abbe 573 update_face_from_frame_parameter (f, Qbackground_color, arg);
ea96210c 574
179956b9 575 if (FRAME_VISIBLE_P (f))
f676886a 576 redraw_frame (f);
01f1ba30
JB
577 }
578}
579
e044e4fc 580static Cursor
971de7fb 581make_invisible_cursor (struct frame *f)
e044e4fc
JD
582{
583 Display *dpy = FRAME_X_DISPLAY (f);
584 static char const no_data[] = { 0 };
585 Pixmap pix;
586 XColor col;
8664db06 587 Cursor c = 0;
e044e4fc
JD
588
589 x_catch_errors (dpy);
aad3612f 590 pix = XCreateBitmapFromData (dpy, FRAME_DISPLAY_INFO (f)->root_window,
e044e4fc
JD
591 no_data, 1, 1);
592 if (! x_had_errors_p (dpy) && pix != None)
593 {
8664db06 594 Cursor pixc;
e044e4fc
JD
595 col.pixel = 0;
596 col.red = col.green = col.blue = 0;
597 col.flags = DoRed | DoGreen | DoBlue;
8664db06
PE
598 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
599 if (! x_had_errors_p (dpy) && pixc != None)
600 c = pixc;
e044e4fc
JD
601 XFreePixmap (dpy, pix);
602 }
603
604 x_uncatch_errors ();
605
606 return c;
607}
608
e4cebfca 609static void
971de7fb 610x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 611{
09393d07
GM
612 struct x_output *x = f->output_data.x;
613 Display *dpy = FRAME_X_DISPLAY (f);
3d970f28 614 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
880e6158 615 Cursor hourglass_cursor, horizontal_drag_cursor, vertical_drag_cursor;
51a1d2d8 616 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ce593f6e 617 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
a76206dc 618
51a1d2d8 619 /* Don't let pointers be invisible. */
09393d07 620 if (mask_color == pixel)
bcf26b38
GM
621 {
622 x_free_colors (f, &pixel, 1);
ce593f6e 623 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
bcf26b38 624 }
a76206dc 625
09393d07
GM
626 unload_color (f, x->mouse_pixel);
627 x->mouse_pixel = pixel;
01f1ba30 628
4d7e6e51 629 block_input ();
fe24a618 630
eb8c3be9 631 /* It's not okay to crash if the user selects a screwy cursor. */
9ba8e10d 632 x_catch_errors (dpy);
fe24a618 633
09393d07 634 if (!NILP (Vx_pointer_shape))
01f1ba30 635 {
b7826503 636 CHECK_NUMBER (Vx_pointer_shape);
09393d07 637 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
01f1ba30
JB
638 }
639 else
09393d07
GM
640 cursor = XCreateFontCursor (dpy, XC_xterm);
641 x_check_errors (dpy, "bad text pointer cursor: %s");
01f1ba30 642
09393d07 643 if (!NILP (Vx_nontext_pointer_shape))
01f1ba30 644 {
b7826503 645 CHECK_NUMBER (Vx_nontext_pointer_shape);
09393d07
GM
646 nontext_cursor
647 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
01f1ba30
JB
648 }
649 else
09393d07
GM
650 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
651 x_check_errors (dpy, "bad nontext pointer cursor: %s");
01f1ba30 652
09393d07 653 if (!NILP (Vx_hourglass_pointer_shape))
333b20bb 654 {
b7826503 655 CHECK_NUMBER (Vx_hourglass_pointer_shape);
09393d07
GM
656 hourglass_cursor
657 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
333b20bb
GM
658 }
659 else
09393d07
GM
660 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
661 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
177c0ea7 662
09393d07 663 if (!NILP (Vx_mode_pointer_shape))
01f1ba30 664 {
b7826503 665 CHECK_NUMBER (Vx_mode_pointer_shape);
09393d07 666 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
01f1ba30
JB
667 }
668 else
09393d07
GM
669 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
670 x_check_errors (dpy, "bad modeline pointer cursor: %s");
95f80c78 671
09393d07 672 if (!NILP (Vx_sensitive_text_pointer_shape))
95f80c78 673 {
b7826503 674 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
3d970f28 675 hand_cursor
09393d07 676 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
95f80c78
FP
677 }
678 else
3d970f28 679 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
01f1ba30 680
8fb4ec9c
GM
681 if (!NILP (Vx_window_horizontal_drag_shape))
682 {
b7826503 683 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
8fb4ec9c 684 horizontal_drag_cursor
09393d07 685 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
8fb4ec9c
GM
686 }
687 else
688 horizontal_drag_cursor
09393d07 689 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
8fb4ec9c 690
880e6158
MR
691 if (!NILP (Vx_window_vertical_drag_shape))
692 {
693 CHECK_NUMBER (Vx_window_vertical_drag_shape);
694 vertical_drag_cursor
695 = XCreateFontCursor (dpy, XINT (Vx_window_vertical_drag_shape));
696 }
697 else
698 vertical_drag_cursor
699 = XCreateFontCursor (dpy, XC_sb_v_double_arrow);
700
fe24a618 701 /* Check and report errors with the above calls. */
09393d07 702 x_check_errors (dpy, "can't set cursor shape: %s");
4545fa20 703 x_uncatch_errors ();
fe24a618 704
01f1ba30
JB
705 {
706 XColor fore_color, back_color;
707
09393d07 708 fore_color.pixel = x->mouse_pixel;
a31fedb7 709 x_query_color (f, &fore_color);
01f1ba30 710 back_color.pixel = mask_color;
a31fedb7 711 x_query_color (f, &back_color);
177c0ea7 712
09393d07
GM
713 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
714 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
715 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
3d970f28 716 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
09393d07
GM
717 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
718 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
01f1ba30 719 }
01f1ba30 720
fe24a618 721 if (FRAME_X_WINDOW (f) != 0)
e044e4fc
JD
722 XDefineCursor (dpy, FRAME_X_WINDOW (f),
723 f->output_data.x->current_cursor = cursor);
09393d07 724
aad3612f
DA
725 if (FRAME_DISPLAY_INFO (f)->invisible_cursor == 0)
726 FRAME_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
f5f6c0e0 727
09393d07
GM
728 if (cursor != x->text_cursor
729 && x->text_cursor != 0)
730 XFreeCursor (dpy, x->text_cursor);
731 x->text_cursor = cursor;
732
733 if (nontext_cursor != x->nontext_cursor
734 && x->nontext_cursor != 0)
735 XFreeCursor (dpy, x->nontext_cursor);
736 x->nontext_cursor = nontext_cursor;
737
738 if (hourglass_cursor != x->hourglass_cursor
739 && x->hourglass_cursor != 0)
740 XFreeCursor (dpy, x->hourglass_cursor);
741 x->hourglass_cursor = hourglass_cursor;
742
743 if (mode_cursor != x->modeline_cursor
744 && x->modeline_cursor != 0)
745 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
746 x->modeline_cursor = mode_cursor;
177c0ea7 747
3d970f28
KS
748 if (hand_cursor != x->hand_cursor
749 && x->hand_cursor != 0)
750 XFreeCursor (dpy, x->hand_cursor);
751 x->hand_cursor = hand_cursor;
01f1ba30 752
09393d07
GM
753 if (horizontal_drag_cursor != x->horizontal_drag_cursor
754 && x->horizontal_drag_cursor != 0)
755 XFreeCursor (dpy, x->horizontal_drag_cursor);
756 x->horizontal_drag_cursor = horizontal_drag_cursor;
8fb4ec9c 757
880e6158
MR
758 if (vertical_drag_cursor != x->vertical_drag_cursor
759 && x->vertical_drag_cursor != 0)
760 XFreeCursor (dpy, x->vertical_drag_cursor);
761 x->vertical_drag_cursor = vertical_drag_cursor;
762
09393d07 763 XFlush (dpy);
4d7e6e51 764 unblock_input ();
05c8abbe
GM
765
766 update_face_from_frame_parameter (f, Qmouse_color, arg);
01f1ba30
JB
767}
768
e4cebfca 769static void
971de7fb 770x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 771{
a76206dc 772 unsigned long fore_pixel, pixel;
d7e6881a 773 bool fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
09393d07 774 struct x_output *x = f->output_data.x;
01f1ba30 775
10168ebb
GM
776 if (!NILP (Vx_cursor_fore_pixel))
777 {
778 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
779 WHITE_PIX_DEFAULT (f));
780 fore_pixel_allocated_p = 1;
781 }
01f1ba30 782 else
ce593f6e 783 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
177c0ea7 784
a76206dc 785 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
10168ebb 786 pixel_allocated_p = 1;
a76206dc 787
f9942c9e 788 /* Make sure that the cursor color differs from the background color. */
ce593f6e 789 if (pixel == FRAME_BACKGROUND_PIXEL (f))
01f1ba30 790 {
10168ebb
GM
791 if (pixel_allocated_p)
792 {
793 x_free_colors (f, &pixel, 1);
794 pixel_allocated_p = 0;
795 }
177c0ea7 796
09393d07 797 pixel = x->mouse_pixel;
a76206dc 798 if (pixel == fore_pixel)
10168ebb
GM
799 {
800 if (fore_pixel_allocated_p)
801 {
802 x_free_colors (f, &fore_pixel, 1);
803 fore_pixel_allocated_p = 0;
804 }
ce593f6e 805 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
10168ebb 806 }
01f1ba30 807 }
a76206dc 808
09393d07 809 unload_color (f, x->cursor_foreground_pixel);
10168ebb
GM
810 if (!fore_pixel_allocated_p)
811 fore_pixel = x_copy_color (f, fore_pixel);
09393d07 812 x->cursor_foreground_pixel = fore_pixel;
01f1ba30 813
09393d07 814 unload_color (f, x->cursor_pixel);
10168ebb
GM
815 if (!pixel_allocated_p)
816 pixel = x_copy_color (f, pixel);
09393d07 817 x->cursor_pixel = pixel;
a76206dc 818
fe24a618 819 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 820 {
4d7e6e51 821 block_input ();
09393d07
GM
822 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
823 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
4d7e6e51 824 unblock_input ();
01f1ba30 825
179956b9 826 if (FRAME_VISIBLE_P (f))
01f1ba30 827 {
cedadcfa
RS
828 x_update_cursor (f, 0);
829 x_update_cursor (f, 1);
01f1ba30
JB
830 }
831 }
05c8abbe
GM
832
833 update_face_from_frame_parameter (f, Qcursor_color, arg);
01f1ba30 834}
943b580d 835\f
f676886a 836/* Set the border-color of frame F to pixel value PIX.
01f1ba30 837 Note that this does not fully take effect if done before
f676886a 838 F has an x-window. */
01f1ba30 839
e4cebfca 840static void
971de7fb 841x_set_border_pixel (struct frame *f, int pix)
01f1ba30 842{
a76206dc 843 unload_color (f, f->output_data.x->border_pixel);
7556890b 844 f->output_data.x->border_pixel = pix;
01f1ba30 845
be786000 846 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
01f1ba30 847 {
4d7e6e51 848 block_input ();
578c21e6 849 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
4d7e6e51 850 unblock_input ();
01f1ba30 851
179956b9 852 if (FRAME_VISIBLE_P (f))
f676886a 853 redraw_frame (f);
01f1ba30
JB
854 }
855}
856
7c0d3ed8
KS
857/* Set the border-color of frame F to value described by ARG.
858 ARG can be a string naming a color.
859 The border-color is used for the border that is drawn by the X server.
860 Note that this does not fully take effect if done before
861 F has an x-window; it must be redone when the window is created.
862
863 Note: this is done in two routines because of the way X10 works.
864
865 Note: under X11, this is normally the province of the window manager,
5ec0337a 866 and so emacs's border colors may be overridden. */
7c0d3ed8 867
e4cebfca 868static void
971de7fb 869x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
7c0d3ed8
KS
870{
871 int pix;
872
873 CHECK_STRING (arg);
874 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
875 x_set_border_pixel (f, pix);
876 update_face_from_frame_parameter (f, Qborder_color, arg);
877}
0d1469d6 878
0d1469d6 879
e4cebfca 880static void
a10c8269 881x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
0d1469d6 882{
33b2311e 883 set_frame_cursor_types (f, arg);
dbc4e1c1 884}
9cda66e4 885
e4cebfca 886static void
971de7fb 887x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 888{
01f1ba30
JB
889 int result;
890
203c1d73
RS
891 if (STRINGP (arg))
892 {
893 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
894 return;
895 }
896 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
01f1ba30
JB
897 return;
898
4d7e6e51 899 block_input ();
265a9e55 900 if (NILP (arg))
80534dd6 901 result = x_text_icon (f,
e69b0960
DA
902 SSDATA ((!NILP (f->icon_name)
903 ? f->icon_name
904 : f->name)));
f1c7b5a6
RS
905 else
906 result = x_bitmap_icon (f, arg);
01f1ba30
JB
907
908 if (result)
909 {
4d7e6e51 910 unblock_input ();
0fb53770 911 error ("No icon window available");
01f1ba30
JB
912 }
913
b9dc4443 914 XFlush (FRAME_X_DISPLAY (f));
4d7e6e51 915 unblock_input ();
01f1ba30
JB
916}
917
e4cebfca 918static void
971de7fb 919x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
80534dd6 920{
80534dd6
KH
921 int result;
922
923 if (STRINGP (arg))
924 {
925 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
926 return;
927 }
56f41f98 928 else if (!NILP (arg) || NILP (oldval))
80534dd6
KH
929 return;
930
f00af5b1 931 fset_icon_name (f, arg);
80534dd6 932
7556890b 933 if (f->output_data.x->icon_bitmap != 0)
80534dd6
KH
934 return;
935
4d7e6e51 936 block_input ();
80534dd6
KH
937
938 result = x_text_icon (f,
e69b0960
DA
939 SSDATA ((!NILP (f->icon_name)
940 ? f->icon_name
941 : !NILP (f->title)
942 ? f->title
943 : f->name)));
80534dd6
KH
944
945 if (result)
946 {
4d7e6e51 947 unblock_input ();
80534dd6
KH
948 error ("No icon window available");
949 }
950
80534dd6 951 XFlush (FRAME_X_DISPLAY (f));
4d7e6e51 952 unblock_input ();
80534dd6 953}
7c0d3ed8 954
943b580d 955\f
01f1ba30 956void
971de7fb 957x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
01f1ba30 958{
7c0d3ed8 959 int nlines;
6b61353c 960#if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
7c0d3ed8
KS
961 int olines = FRAME_MENU_BAR_LINES (f);
962#endif
177c0ea7 963
7c0d3ed8
KS
964 /* Right now, menu bars don't work properly in minibuf-only frames;
965 most of the commands try to apply themselves to the minibuffer
966 frame itself, and get an error because you can't switch buffers
967 in or split the minibuffer window. */
968 if (FRAME_MINIBUF_ONLY_P (f))
969 return;
177c0ea7 970
d311d28c 971 if (TYPE_RANGED_INTEGERP (int, value))
7c0d3ed8 972 nlines = XINT (value);
ea96210c 973 else
7c0d3ed8 974 nlines = 0;
a367641f 975
7c0d3ed8 976 /* Make sure we redisplay all windows in this frame. */
94ae2ad4 977 windows_or_buffers_changed = 59;
3d09b6be 978
488dd4c4 979#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9ef48a9d 980 FRAME_MENU_BAR_LINES (f) = 0;
880e6158 981 FRAME_MENU_BAR_HEIGHT (f) = 0;
9ef48a9d 982 if (nlines)
0d8ef3f4
RS
983 {
984 FRAME_EXTERNAL_MENU_BAR (f) = 1;
97a1ff91 985 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
0d8ef3f4 986 /* Make sure next redisplay shows the menu bar. */
c98ff5dd 987 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
0d8ef3f4 988 }
9ef48a9d
RS
989 else
990 {
6bc20398
FP
991 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
992 free_frame_menubar (f);
9ef48a9d 993 FRAME_EXTERNAL_MENU_BAR (f) = 0;
97a1ff91
RS
994 if (FRAME_X_P (f))
995 f->output_data.x->menubar_widget = 0;
9ef48a9d 996 }
488dd4c4 997#else /* not USE_X_TOOLKIT && not USE_GTK */
d043f1a4 998 FRAME_MENU_BAR_LINES (f) = nlines;
880e6158
MR
999 FRAME_MENU_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1000 resize_frame_windows (f, FRAME_LINES (f), 0, 0);
1df47e38
YM
1001
1002 /* If the menu bar height gets changed, the internal border below
1003 the top margin has to be cleared. Also, if the menu bar gets
1004 larger, the area for the added lines has to be cleared except for
1005 the first menu bar line that is to be drawn later. */
1006 if (nlines != olines)
1007 {
1008 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1009 int width = FRAME_PIXEL_WIDTH (f);
1010 int y;
1011
1012 /* height can be zero here. */
880e6158 1013 if (FRAME_X_WINDOW (f) && height > 0 && width > 0)
1df47e38
YM
1014 {
1015 y = FRAME_TOP_MARGIN_HEIGHT (f);
1016
4d7e6e51 1017 block_input ();
1df47e38 1018 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1f5cf200 1019 0, y, width, height);
4d7e6e51 1020 unblock_input ();
1df47e38
YM
1021 }
1022
1023 if (nlines > 1 && nlines > olines)
1024 {
1025 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1026 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1027
4d7e6e51 1028 block_input ();
1df47e38 1029 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1f5cf200 1030 0, y, width, height);
4d7e6e51 1031 unblock_input ();
1df47e38
YM
1032 }
1033
e69b0960
DA
1034 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1035 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1df47e38
YM
1036 }
1037#endif /* not USE_X_TOOLKIT && not USE_GTK */
bd0443bb 1038 adjust_frame_glyphs (f);
562dd5e9 1039 run_window_configuration_change_hook (f);
333b20bb
GM
1040}
1041
1042
1043/* Set the number of lines used for the tool bar of frame F to VALUE.
1044 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1045 is the old number of tool bar lines. This function changes the
1046 height of all windows on frame F to match the new tool bar height.
1047 The frame's height doesn't change. */
1048
1049void
971de7fb 1050x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
333b20bb 1051{
742516e0
DA
1052 int nlines;
1053#if ! defined (USE_GTK)
1054 int delta, root_height;
52de7ce9 1055 Lisp_Object root_window;
742516e0 1056#endif
333b20bb 1057
e870b7ba
GM
1058 /* Treat tool bars like menu bars. */
1059 if (FRAME_MINIBUF_ONLY_P (f))
1060 return;
1061
d311d28c
PE
1062 /* Use VALUE only if an int >= 0. */
1063 if (RANGED_INTEGERP (0, value, INT_MAX))
333b20bb
GM
1064 nlines = XFASTINT (value);
1065 else
1066 nlines = 0;
1067
488dd4c4 1068#ifdef USE_GTK
742516e0 1069
488dd4c4 1070 FRAME_TOOL_BAR_LINES (f) = 0;
880e6158 1071 FRAME_TOOL_BAR_HEIGHT (f) = 0;
488dd4c4
JD
1072 if (nlines)
1073 {
1074 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1075 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1076 /* Make sure next redisplay shows the tool bar. */
5fceba1d 1077 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
488dd4c4
JD
1078 update_frame_tool_bar (f);
1079 }
1080 else
1081 {
1082 if (FRAME_EXTERNAL_TOOL_BAR (f))
1083 free_frame_tool_bar (f);
1084 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1085 }
1086
742516e0 1087#else /* !USE_GTK */
177c0ea7 1088
94ae2ad4
SM
1089 /* Make sure we redisplay all windows in this frame. */
1090 windows_or_buffers_changed = 60;
333b20bb 1091
9ea173e8 1092 delta = nlines - FRAME_TOOL_BAR_LINES (f);
52de7ce9
GM
1093
1094 /* Don't resize the tool-bar to more than we have room for. */
1095 root_window = FRAME_ROOT_WINDOW (f);
be786000 1096 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
52de7ce9
GM
1097 if (root_height - delta < 1)
1098 {
1099 delta = root_height - 1;
1100 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1101 }
1102
9ea173e8 1103 FRAME_TOOL_BAR_LINES (f) = nlines;
880e6158
MR
1104 FRAME_TOOL_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1105 resize_frame_windows (f, FRAME_LINES (f), 0, 0);
bd0443bb 1106 adjust_frame_glyphs (f);
177c0ea7 1107
ccba751c
GM
1108 /* We also have to make sure that the internal border at the top of
1109 the frame, below the menu bar or tool bar, is redrawn when the
1110 tool bar disappears. This is so because the internal border is
1111 below the tool bar if one is displayed, but is below the menu bar
1112 if there isn't a tool bar. The tool bar draws into the area
1113 below the menu bar. */
880e6158 1114 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_HEIGHT (f) == 0)
ccba751c 1115 {
385ed61f 1116 clear_frame (f);
fb3cd89b 1117 clear_current_matrices (f);
ccba751c 1118 }
b6f91066
GM
1119
1120 /* If the tool bar gets smaller, the internal border below it
1121 has to be cleared. It was formerly part of the display
1122 of the larger tool bar, and updating windows won't clear it. */
1123 if (delta < 0)
1124 {
1125 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
be786000 1126 int width = FRAME_PIXEL_WIDTH (f);
4b00d3b1 1127 int y = (FRAME_MENU_BAR_LINES (f) + nlines) * FRAME_LINE_HEIGHT (f);
b6f91066 1128
caacfeb8
JD
1129 /* height can be zero here. */
1130 if (height > 0 && width > 0)
1131 {
4d7e6e51 1132 block_input ();
1f5cf200
DA
1133 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1134 0, y, width, height);
4d7e6e51 1135 unblock_input ();
caacfeb8 1136 }
ddc24747 1137
e69b0960
DA
1138 if (WINDOWP (f->tool_bar_window))
1139 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
b6f91066 1140 }
562dd5e9
MR
1141
1142 run_window_configuration_change_hook (f);
742516e0 1143#endif /* USE_GTK */
333b20bb
GM
1144}
1145
1146
1147/* Set the foreground color for scroll bars on frame F to VALUE.
1148 VALUE should be a string, a color name. If it isn't a string or
1149 isn't a valid color name, do nothing. OLDVAL is the old value of
1150 the frame parameter. */
1151
e4cebfca 1152static void
971de7fb 1153x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
333b20bb
GM
1154{
1155 unsigned long pixel;
177c0ea7 1156
333b20bb
GM
1157 if (STRINGP (value))
1158 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1159 else
1160 pixel = -1;
1161
1162 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1163 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
177c0ea7 1164
333b20bb
GM
1165 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1166 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1167 {
1168 /* Remove all scroll bars because they have wrong colors. */
6ed8eeff
KL
1169 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1170 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1171 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1172 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
05c8abbe
GM
1173
1174 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
333b20bb
GM
1175 redraw_frame (f);
1176 }
1177}
1178
1179
1180/* Set the background color for scroll bars on frame F to VALUE VALUE
1181 should be a string, a color name. If it isn't a string or isn't a
1182 valid color name, do nothing. OLDVAL is the old value of the frame
1183 parameter. */
1184
e4cebfca 1185static void
971de7fb 1186x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
333b20bb
GM
1187{
1188 unsigned long pixel;
1189
1190 if (STRINGP (value))
1191 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1192 else
1193 pixel = -1;
177c0ea7 1194
333b20bb
GM
1195 if (f->output_data.x->scroll_bar_background_pixel != -1)
1196 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
177c0ea7 1197
f15340b7
MB
1198#ifdef USE_TOOLKIT_SCROLL_BARS
1199 /* Scrollbar shadow colors. */
1200 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1201 {
1202 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1203 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1204 }
1205 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1206 {
1207 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1208 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1209 }
1210#endif /* USE_TOOLKIT_SCROLL_BARS */
1211
333b20bb
GM
1212 f->output_data.x->scroll_bar_background_pixel = pixel;
1213 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1214 {
1215 /* Remove all scroll bars because they have wrong colors. */
6ed8eeff
KL
1216 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1217 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1218 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1219 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
177c0ea7 1220
05c8abbe 1221 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
333b20bb
GM
1222 redraw_frame (f);
1223 }
d043f1a4 1224}
333b20bb 1225
943b580d 1226\f
3a258507 1227/* Encode Lisp string STRING as a text in a format appropriate for
96db09e4
KH
1228 XICCC (X Inter Client Communication Conventions).
1229
6f103132
RS
1230 This can call Lisp code, so callers must GCPRO.
1231
96db09e4
KH
1232 If STRING contains only ASCII characters, do no conversion and
1233 return the string data of STRING. Otherwise, encode the text by
1234 CODING_SYSTEM, and return a newly allocated memory area which
1235 should be freed by `xfree' by a caller.
1236
37323f34
EZ
1237 SELECTIONP non-zero means the string is being encoded for an X
1238 selection, so it is safe to run pre-write conversions (which
1239 may run Lisp code).
1240
96db09e4
KH
1241 Store the byte length of resulting text in *TEXT_BYTES.
1242
d60660d6 1243 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
96db09e4 1244 which means that the `encoding' of the result can be `STRING'.
d60660d6 1245 Otherwise store 0 in *STRINGP, which means that the `encoding' of
96db09e4
KH
1246 the result should be `COMPOUND_TEXT'. */
1247
df630496 1248static unsigned char *
c678c835
PE
1249x_encode_text (Lisp_Object string, Lisp_Object coding_system, int selectionp,
1250 ptrdiff_t *text_bytes, int *stringp, int *freep)
96db09e4 1251{
0f8c4c4f 1252 int result = string_xstring_p (string);
96db09e4
KH
1253 struct coding_system coding;
1254
0f8c4c4f 1255 if (result == 0)
96db09e4
KH
1256 {
1257 /* No multibyte character in OBJ. We need not encode it. */
8f924df7 1258 *text_bytes = SBYTES (string);
d60660d6 1259 *stringp = 1;
df630496 1260 *freep = 0;
8f924df7 1261 return SDATA (string);
96db09e4
KH
1262 }
1263
1264 setup_coding_system (coding_system, &coding);
0f8c4c4f 1265 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
35bc5887 1266 /* We suppress producing escape sequences for composition. */
0f8c4c4f 1267 coding.common_flags &= ~CODING_ANNOTATION_MASK;
0065d054 1268 coding.destination = xnmalloc (SCHARS (string), 2);
8f924df7 1269 coding.dst_bytes = SCHARS (string) * 2;
0f8c4c4f 1270 encode_coding_object (&coding, string, 0, 0,
8f924df7 1271 SCHARS (string), SBYTES (string), Qnil);
96db09e4 1272 *text_bytes = coding.produced;
0f8c4c4f 1273 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
df630496 1274 *freep = 1;
b954d586 1275 return coding.destination;
96db09e4
KH
1276}
1277
1278\f
b10daec7
JD
1279/* Set the WM name to NAME for frame F. Also set the icon name.
1280 If the frame already has an icon name, use that, otherwise set the
1281 icon name to NAME. */
f945b920 1282
b10daec7 1283static void
a10c8269 1284x_set_name_internal (struct frame *f, Lisp_Object name)
f945b920 1285{
fe24a618 1286 if (FRAME_X_WINDOW (f))
01f1ba30 1287 {
4d7e6e51 1288 block_input ();
fe24a618 1289 {
80534dd6 1290 XTextProperty text, icon;
c678c835
PE
1291 ptrdiff_t bytes;
1292 int stringp;
b10daec7 1293 int do_free_icon_value = 0, do_free_text_value = 0;
11270583 1294 Lisp_Object coding_system;
c2915ced 1295 Lisp_Object encoded_name;
00be444c 1296 Lisp_Object encoded_icon_name;
c2915ced
CY
1297 struct gcpro gcpro1;
1298
1299 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1300 we use it before x_encode_text that may return string data. */
1301 GCPRO1 (name);
1302 encoded_name = ENCODE_UTF_8 (name);
1303 UNGCPRO;
80534dd6 1304
b10daec7 1305 coding_system = Qcompound_text;
3201ea57
KH
1306 /* Note: Encoding strategy
1307
1308 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1309 text.encoding. But, there are non-internationalized window
1310 managers which don't support that encoding. So, if NAME
1311 contains only ASCII and 8859-1 characters, encode it by
1312 iso-latin-1, and use "STRING" in text.encoding hoping that
34e8c597 1313 such window managers at least analyze this format correctly,
3201ea57
KH
1314 i.e. treat 8-bit bytes as 8859-1 characters.
1315
1316 We may also be able to use "UTF8_STRING" in text.encoding
34e8c597 1317 in the future which can encode all Unicode characters.
3201ea57 1318 But, for the moment, there's no way to know that the
00be444c
J
1319 current window manager supports it or not.
1320
1321 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1322 properties. Per the EWMH specification, those two properties
1323 are always UTF8_STRING. This matches what gtk_window_set_title()
1324 does in the USE_GTK case. */
df630496
KS
1325 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1326 &do_free_text_value);
d60660d6 1327 text.encoding = (stringp ? XA_STRING
aad3612f 1328 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
fe24a618 1329 text.format = 8;
96db09e4 1330 text.nitems = bytes;
c678c835
PE
1331 if (text.nitems != bytes)
1332 error ("Window name too large");
80534dd6 1333
e69b0960 1334 if (!STRINGP (f->icon_name))
96db09e4
KH
1335 {
1336 icon = text;
00be444c 1337 encoded_icon_name = encoded_name;
96db09e4
KH
1338 }
1339 else
1340 {
3201ea57 1341 /* See the above comment "Note: Encoding strategy". */
e69b0960 1342 icon.value = x_encode_text (f->icon_name, coding_system, 0,
df630496 1343 &bytes, &stringp, &do_free_icon_value);
d60660d6 1344 icon.encoding = (stringp ? XA_STRING
aad3612f 1345 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
96db09e4
KH
1346 icon.format = 8;
1347 icon.nitems = bytes;
c678c835
PE
1348 if (icon.nitems != bytes)
1349 error ("Icon name too large");
00be444c 1350
e69b0960 1351 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
96db09e4 1352 }
b10daec7 1353
488dd4c4
JD
1354#ifdef USE_GTK
1355 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
51b59d79 1356 SSDATA (encoded_name));
488dd4c4 1357#else /* not USE_GTK */
2436a4e4 1358 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
00be444c 1359 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
aad3612f
DA
1360 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1361 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
00be444c 1362 8, PropModeReplace,
42a5b22f 1363 SDATA (encoded_name),
00be444c 1364 SBYTES (encoded_name));
7c0d3ed8 1365#endif /* not USE_GTK */
abb4b7ec 1366
7c0d3ed8 1367 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
00be444c 1368 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
aad3612f
DA
1369 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1370 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
00be444c 1371 8, PropModeReplace,
42a5b22f 1372 SDATA (encoded_icon_name),
00be444c 1373 SBYTES (encoded_icon_name));
abb4b7ec 1374
b10daec7 1375 if (do_free_icon_value)
7c0d3ed8 1376 xfree (icon.value);
b10daec7 1377 if (do_free_text_value)
7c0d3ed8
KS
1378 xfree (text.value);
1379 }
4d7e6e51 1380 unblock_input ();
7c0d3ed8 1381 }
abb4b7ec
RS
1382}
1383
b10daec7
JD
1384/* Change the name of frame F to NAME. If NAME is nil, set F's name to
1385 x_id_name.
1386
1387 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1388 name; if NAME is a string, set F's name to NAME and set
1389 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1390
1391 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1392 suggesting a new name, which lisp code should override; if
1393 F->explicit_name is set, ignore the new name; otherwise, set it. */
1394
f78faa98 1395static void
971de7fb 1396x_set_name (struct frame *f, Lisp_Object name, int explicit)
b10daec7
JD
1397{
1398 /* Make sure that requests from lisp code override requests from
1399 Emacs redisplay code. */
1400 if (explicit)
1401 {
1402 /* If we're switching from explicit to implicit, we had better
1403 update the mode lines and thereby update the title. */
1404 if (f->explicit_name && NILP (name))
2ec9db5d 1405 update_mode_lines = 37;
b10daec7
JD
1406
1407 f->explicit_name = ! NILP (name);
1408 }
1409 else if (f->explicit_name)
1410 return;
1411
1412 /* If NAME is nil, set the name to the x_id_name. */
1413 if (NILP (name))
1414 {
1415 /* Check for no change needed in this very common case
1416 before we do any consing. */
aad3612f 1417 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
e69b0960 1418 SSDATA (f->name)))
b10daec7 1419 return;
aad3612f 1420 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
b10daec7
JD
1421 }
1422 else
1423 CHECK_STRING (name);
1424
1425 /* Don't change the name if it's already NAME. */
e69b0960 1426 if (! NILP (Fstring_equal (name, f->name)))
b10daec7
JD
1427 return;
1428
f00af5b1 1429 fset_name (f, name);
b10daec7
JD
1430
1431 /* For setting the frame title, the title parameter should override
1432 the name parameter. */
e69b0960
DA
1433 if (! NILP (f->title))
1434 name = f->title;
b10daec7
JD
1435
1436 x_set_name_internal (f, name);
1437}
1438
7c0d3ed8
KS
1439/* This function should be called when the user's lisp code has
1440 specified a name for the frame; the name will override any set by the
1441 redisplay code. */
e4cebfca 1442static void
a10c8269 1443x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
3402e1a4 1444{
7c0d3ed8 1445 x_set_name (f, arg, 1);
3402e1a4
RS
1446}
1447
7c0d3ed8
KS
1448/* This function should be called by Emacs redisplay code to set the
1449 name; names set this way will never override names set by the user's
1450 lisp code. */
1451void
a10c8269 1452x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
333b20bb 1453{
7c0d3ed8
KS
1454 x_set_name (f, arg, 0);
1455}
1456\f
1457/* Change the title of frame F to NAME.
40aa4c27 1458 If NAME is nil, use the frame name as the title. */
01f1ba30 1459
e4cebfca 1460static void
971de7fb 1461x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
01f1ba30 1462{
7c0d3ed8 1463 /* Don't change the title if it's already NAME. */
e69b0960 1464 if (EQ (name, f->title))
7c0d3ed8 1465 return;
01f1ba30 1466
2ec9db5d 1467 update_mode_lines = 38;
60fb3ee1 1468
f00af5b1 1469 fset_title (f, name);
f9942c9e 1470
7c0d3ed8 1471 if (NILP (name))
e69b0960 1472 name = f->name;
7c0d3ed8
KS
1473 else
1474 CHECK_STRING (name);
f9942c9e 1475
b10daec7 1476 x_set_name_internal (f, name);
01f1ba30
JB
1477}
1478
7c0d3ed8 1479void
971de7fb 1480x_set_scroll_bar_default_width (struct frame *f)
e4f79258 1481{
be786000 1482 int wid = FRAME_COLUMN_WIDTH (f);
7c0d3ed8 1483#ifdef USE_TOOLKIT_SCROLL_BARS
a059fe24 1484#ifdef USE_GTK
c195f2de 1485 int minw = xg_get_default_scrollbar_width ();
4a1b9832
PE
1486#else
1487 int minw = 16;
a059fe24 1488#endif
7c0d3ed8 1489 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
0eed0355
DA
1490 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + wid - 1) / wid;
1491 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
7c0d3ed8 1492#else
880e6158 1493 /* Make the actual width 16 pixels and a multiple of a
7c0d3ed8 1494 character width. */
880e6158 1495 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (16 + wid - 1) / wid;
01f1ba30 1496
7c0d3ed8
KS
1497 /* Use all of that space (aside from required margins) for the
1498 scroll bar. */
880e6158 1499 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 16;
7c0d3ed8 1500#endif
01f1ba30 1501}
333b20bb 1502
7c0d3ed8 1503\f
333b20bb
GM
1504/* Record in frame F the specified or default value according to ALIST
1505 of the parameter named PROP (a Lisp symbol). If no value is
1506 specified for PROP, look for an X default for XPROP on the frame
1507 named NAME. If that is not found either, use the value DEFLT. */
1508
1509static Lisp_Object
d5a3eaaf
AS
1510x_default_scroll_bar_color_parameter (struct frame *f,
1511 Lisp_Object alist, Lisp_Object prop,
675e2c69 1512 const char *xprop, const char *xclass,
d5a3eaaf 1513 int foreground_p)
333b20bb 1514{
aad3612f 1515 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
333b20bb
GM
1516 Lisp_Object tem;
1517
1518 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1519 if (EQ (tem, Qunbound))
1520 {
1521#ifdef USE_TOOLKIT_SCROLL_BARS
1522
1523 /* See if an X resource for the scroll bar color has been
1524 specified. */
1525 tem = display_x_get_resource (dpyinfo,
1526 build_string (foreground_p
1527 ? "foreground"
1528 : "background"),
6ecd75be 1529 empty_unibyte_string,
333b20bb 1530 build_string ("verticalScrollBar"),
6ecd75be 1531 empty_unibyte_string);
333b20bb
GM
1532 if (!STRINGP (tem))
1533 {
1534 /* If nothing has been specified, scroll bars will use a
1535 toolkit-dependent default. Because these defaults are
1536 difficult to get at without actually creating a scroll
1537 bar, use nil to indicate that no color has been
1538 specified. */
1539 tem = Qnil;
1540 }
177c0ea7 1541
333b20bb 1542#else /* not USE_TOOLKIT_SCROLL_BARS */
177c0ea7 1543
333b20bb 1544 tem = Qnil;
177c0ea7 1545
333b20bb
GM
1546#endif /* not USE_TOOLKIT_SCROLL_BARS */
1547 }
1548
6c6f1994 1549 x_set_frame_parameters (f, list1 (Fcons (prop, tem)));
333b20bb
GM
1550 return tem;
1551}
1552
1553
01f1ba30 1554
9ef48a9d
RS
1555\f
1556#ifdef USE_X_TOOLKIT
1557
8e3d10a9
RS
1558/* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1559 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
59aa6c90
RS
1560 already be present because of the toolkit (Motif adds some of them,
1561 for example, but Xt doesn't). */
9ef48a9d
RS
1562
1563static void
a10c8269 1564hack_wm_protocols (struct frame *f, Widget widget)
9ef48a9d
RS
1565{
1566 Display *dpy = XtDisplay (widget);
1567 Window w = XtWindow (widget);
1568 int need_delete = 1;
1569 int need_focus = 1;
59aa6c90 1570 int need_save = 1;
9ef48a9d 1571
4d7e6e51 1572 block_input ();
9ef48a9d 1573 {
a3db4b26
AS
1574 Atom type;
1575 unsigned char *catoms;
9ef48a9d
RS
1576 int format = 0;
1577 unsigned long nitems = 0;
1578 unsigned long bytes_after;
1579
270958e8 1580 if ((XGetWindowProperty (dpy, w,
aad3612f 1581 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
34d5ae1e 1582 (long)0, (long)100, False, XA_ATOM,
270958e8 1583 &type, &format, &nitems, &bytes_after,
a3db4b26 1584 &catoms)
270958e8 1585 == Success)
9ef48a9d 1586 && format == 32 && type == XA_ATOM)
a3db4b26
AS
1587 {
1588 Atom *atoms = (Atom *) catoms;
1589 while (nitems > 0)
1590 {
1591 nitems--;
1592 if (atoms[nitems]
aad3612f 1593 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
a3db4b26
AS
1594 need_delete = 0;
1595 else if (atoms[nitems]
aad3612f 1596 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
a3db4b26
AS
1597 need_focus = 0;
1598 else if (atoms[nitems]
aad3612f 1599 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
a3db4b26
AS
1600 need_save = 0;
1601 }
1602 }
1603 if (catoms)
1604 XFree (catoms);
9ef48a9d
RS
1605 }
1606 {
1607 Atom props [10];
1608 int count = 0;
b9dc4443 1609 if (need_delete)
aad3612f 1610 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
b9dc4443 1611 if (need_focus)
aad3612f 1612 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
b9dc4443 1613 if (need_save)
aad3612f 1614 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
9ef48a9d 1615 if (count)
aad3612f 1616 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
b9dc4443 1617 XA_ATOM, 32, PropModeAppend,
9ef48a9d
RS
1618 (unsigned char *) props, count);
1619 }
4d7e6e51 1620 unblock_input ();
9ef48a9d
RS
1621}
1622#endif
86779fac
GM
1623
1624
5a7df7d7
GM
1625\f
1626/* Support routines for XIC (X Input Context). */
86779fac 1627
5a7df7d7
GM
1628#ifdef HAVE_X_I18N
1629
f57e2426
J
1630static XFontSet xic_create_xfontset (struct frame *);
1631static XIMStyle best_xim_style (XIMStyles *, XIMStyles *);
5a7df7d7
GM
1632
1633
1576f1ad 1634/* Supported XIM styles, ordered by preference. */
5a7df7d7
GM
1635
1636static XIMStyle supported_xim_styles[] =
1637{
1638 XIMPreeditPosition | XIMStatusArea,
1639 XIMPreeditPosition | XIMStatusNothing,
1640 XIMPreeditPosition | XIMStatusNone,
1641 XIMPreeditNothing | XIMStatusArea,
1642 XIMPreeditNothing | XIMStatusNothing,
1643 XIMPreeditNothing | XIMStatusNone,
1644 XIMPreeditNone | XIMStatusArea,
1645 XIMPreeditNone | XIMStatusNothing,
1646 XIMPreeditNone | XIMStatusNone,
1647 0,
1648};
1649
1650
e4cebfca 1651#if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
c27ed90a 1652/* Create an X fontset on frame F with base font name BASE_FONTNAME. */
5a7df7d7 1653
333f9019 1654static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
bb07fa29 1655
c28e7ae8
SM
1656/* Create an Xt fontset spec from the name of a base font.
1657 If `motif' is True use the Motif syntax. */
bb07fa29 1658char *
e6cba650 1659xic_create_fontsetname (const char *base_fontname, int motif)
bb07fa29 1660{
c28e7ae8
SM
1661 const char *sep = motif ? ";" : ",";
1662 char *fontsetname;
1663
bb07fa29 1664 /* Make a fontset name from the base font name. */
333f9019 1665 if (xic_default_fontset == base_fontname)
38182d90 1666 {
afa2ffd8
DA
1667 /* There is no base font name, use the default. */
1668 fontsetname = xmalloc (strlen (base_fontname) + 2);
c28e7ae8
SM
1669 strcpy (fontsetname, base_fontname);
1670 }
bb07fa29
SM
1671 else
1672 {
1673 /* Make a fontset name from the base font name.
1674 The font set will be made of the following elements:
1675 - the base font.
1676 - the base font where the charset spec is replaced by -*-*.
1677 - the same but with the family also replaced with -*-*-. */
e6cba650 1678 const char *p = base_fontname;
25ed6cc3 1679 ptrdiff_t i;
df630496 1680
bb07fa29
SM
1681 for (i = 0; *p; p++)
1682 if (*p == '-') i++;
1683 if (i != 14)
afa2ffd8
DA
1684 {
1685 /* As the font name doesn't conform to XLFD, we can't
bb07fa29
SM
1686 modify it to generalize it to allcs and allfamilies.
1687 Use the specified font plus the default. */
afa2ffd8
DA
1688 fontsetname = xmalloc (strlen (base_fontname)
1689 + strlen (xic_default_fontset) + 3);
bb07fa29 1690 strcpy (fontsetname, base_fontname);
c28e7ae8 1691 strcat (fontsetname, sep);
333f9019 1692 strcat (fontsetname, xic_default_fontset);
bb07fa29
SM
1693 }
1694 else
1695 {
25ed6cc3 1696 ptrdiff_t len;
e6cba650 1697 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
bb07fa29
SM
1698 char *font_allcs = NULL;
1699 char *font_allfamilies = NULL;
968a6679 1700 char *font_all = NULL;
e6cba650
DN
1701 const char *allcs = "*-*-*-*-*-*-*";
1702 const char *allfamilies = "-*-*-";
1703 const char *all = "*-*-*-*-";
cd987aaa 1704 char *base;
df630496 1705
bb07fa29
SM
1706 for (i = 0, p = base_fontname; i < 8; p++)
1707 {
1708 if (*p == '-')
1709 {
1710 i++;
1711 if (i == 3)
1712 p1 = p + 1;
968a6679
SM
1713 else if (i == 7)
1714 p2 = p + 1;
cd987aaa
KH
1715 else if (i == 6)
1716 p3 = p + 1;
bb07fa29
SM
1717 }
1718 }
cd987aaa
KH
1719 /* If base_fontname specifies ADSTYLE, make it a
1720 wildcard. */
1721 if (*p3 != '*')
1722 {
25ed6cc3 1723 ptrdiff_t diff = (p2 - p3) - 2;
cd987aaa
KH
1724
1725 base = alloca (strlen (base_fontname) + 1);
72af86bd 1726 memcpy (base, base_fontname, p3 - base_fontname);
cd987aaa
KH
1727 base[p3 - base_fontname] = '*';
1728 base[(p3 - base_fontname) + 1] = '-';
1729 strcpy (base + (p3 - base_fontname) + 2, p2);
1730 p = base + (p - base_fontname) - diff;
1731 p1 = base + (p1 - base_fontname);
1732 p2 = base + (p2 - base_fontname) - diff;
1733 base_fontname = base;
1734 }
1735
bb07fa29
SM
1736 /* Build the font spec that matches all charsets. */
1737 len = p - base_fontname + strlen (allcs) + 1;
38182d90 1738 font_allcs = alloca (len);
72af86bd 1739 memcpy (font_allcs, base_fontname, p - base_fontname);
bb07fa29
SM
1740 strcat (font_allcs, allcs);
1741
cd987aaa
KH
1742 /* Build the font spec that matches all families and
1743 add-styles. */
bb07fa29 1744 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
38182d90 1745 font_allfamilies = alloca (len);
bb07fa29 1746 strcpy (font_allfamilies, allfamilies);
72af86bd 1747 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
bb07fa29
SM
1748 strcat (font_allfamilies, allcs);
1749
968a6679
SM
1750 /* Build the font spec that matches all. */
1751 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
38182d90 1752 font_all = alloca (len);
968a6679
SM
1753 strcpy (font_all, allfamilies);
1754 strcat (font_all, all);
72af86bd 1755 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
968a6679
SM
1756 strcat (font_all, allcs);
1757
bb07fa29
SM
1758 /* Build the actual font set name. */
1759 len = strlen (base_fontname) + strlen (font_allcs)
968a6679 1760 + strlen (font_allfamilies) + strlen (font_all) + 5;
bb07fa29 1761 fontsetname = xmalloc (len);
bb07fa29 1762 strcpy (fontsetname, base_fontname);
c28e7ae8 1763 strcat (fontsetname, sep);
bb07fa29 1764 strcat (fontsetname, font_allcs);
c28e7ae8 1765 strcat (fontsetname, sep);
bb07fa29 1766 strcat (fontsetname, font_allfamilies);
968a6679
SM
1767 strcat (fontsetname, sep);
1768 strcat (fontsetname, font_all);
bb07fa29 1769 }
bb07fa29 1770 }
c28e7ae8 1771 if (motif)
d672ac3c 1772 return strcat (fontsetname, ":");
c28e7ae8 1773 return fontsetname;
bb07fa29 1774}
e4cebfca 1775#endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
bb07fa29 1776
2da34f50
KH
1777#ifdef DEBUG_XIC_FONTSET
1778static void
1dae0f0a
AS
1779print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1780 int missing_count)
2da34f50
KH
1781{
1782 if (xfs)
1783 fprintf (stderr, "XIC Fontset created: %s\n", name);
1784 else
1785 {
1786 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1787 while (missing_count-- > 0)
1788 {
1789 fprintf (stderr, " missing: %s\n", *missing_list);
1790 missing_list++;
1791 }
1792 }
1793
1794}
1795#endif
1796
5a7df7d7 1797static XFontSet
971de7fb 1798xic_create_xfontset (struct frame *f)
86779fac 1799{
c27ed90a 1800 XFontSet xfs = NULL;
b51238f5 1801 struct font *font = FRAME_FONT (f);
a32f056c
KH
1802 int pixel_size = font->pixel_size;
1803 Lisp_Object rest, frame;
1804
1805 /* See if there is another frame already using same fontset. */
1806 FOR_EACH_FRAME (rest, frame)
1807 {
1808 struct frame *cf = XFRAME (frame);
1809
1810 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
aad3612f 1811 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
b51238f5
KH
1812 && FRAME_FONT (f)
1813 && FRAME_FONT (f)->pixel_size == pixel_size)
a32f056c
KH
1814 {
1815 xfs = FRAME_XIC_FONTSET (cf);
1816 break;
1817 }
1818 }
1819
1820 if (! xfs)
1821 {
1822 char buf[256];
1823 char **missing_list;
1824 int missing_count;
1825 char *def_string;
e6cba650 1826 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
a32f056c
KH
1827
1828 sprintf (buf, xlfd_format, pixel_size);
1829 missing_list = NULL;
1830 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1831 &missing_list, &missing_count, &def_string);
d3483d71 1832#ifdef DEBUG_XIC_FONTSET
22a8f595 1833 print_fontset_result (xfs, buf, missing_list, missing_count);
d3483d71 1834#endif
a32f056c
KH
1835 if (missing_list)
1836 XFreeStringList (missing_list);
1837 if (! xfs)
1838 {
1839 /* List of pixel sizes most likely available. Find one that
1840 is closest to pixel_size. */
1841 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1842 int *smaller, *larger;
1843
1844 for (smaller = sizes; smaller[1]; smaller++)
1845 if (smaller[1] >= pixel_size)
1846 break;
1847 larger = smaller + 1;
1848 if (*larger == pixel_size)
1849 larger++;
1850 while (*smaller || *larger)
1851 {
1852 int this_size;
1853
1854 if (! *larger)
1855 this_size = *smaller--;
1856 else if (! *smaller)
1857 this_size = *larger++;
1858 else if (pixel_size - *smaller < *larger - pixel_size)
1859 this_size = *smaller--;
1860 else
1861 this_size = *larger++;
1862 sprintf (buf, xlfd_format, this_size);
1863 missing_list = NULL;
1864 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1865 &missing_list, &missing_count, &def_string);
d3483d71 1866#ifdef DEBUG_XIC_FONTSET
22a8f595 1867 print_fontset_result (xfs, buf, missing_list, missing_count);
d3483d71 1868#endif
a32f056c
KH
1869 if (missing_list)
1870 XFreeStringList (missing_list);
1871 if (xfs)
1872 break;
1873 }
1874 }
2da34f50
KH
1875 if (! xfs)
1876 {
675e2c69 1877 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
2da34f50
KH
1878
1879 missing_list = NULL;
1880 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1881 &missing_list, &missing_count, &def_string);
1882#ifdef DEBUG_XIC_FONTSET
1883 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1884#endif
1885 if (missing_list)
1886 XFreeStringList (missing_list);
1887 }
1888
a32f056c
KH
1889 }
1890
1891 return xfs;
1892}
a32f056c 1893
c27ed90a
JD
1894/* Free the X fontset of frame F if it is the last frame using it. */
1895
1896void
971de7fb 1897xic_free_xfontset (struct frame *f)
c27ed90a
JD
1898{
1899 Lisp_Object rest, frame;
d7e6881a 1900 bool shared_p = 0;
c27ed90a
JD
1901
1902 if (!FRAME_XIC_FONTSET (f))
1903 return;
1904
1905 /* See if there is another frame sharing the same fontset. */
1906 FOR_EACH_FRAME (rest, frame)
1907 {
1908 struct frame *cf = XFRAME (frame);
1909 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
aad3612f 1910 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
c27ed90a
JD
1911 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1912 {
1913 shared_p = 1;
1914 break;
1915 }
1916 }
1917
1918 if (!shared_p)
1919 /* The fontset is not used anymore. It is safe to free it. */
1920 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1921
c27ed90a
JD
1922 FRAME_XIC_FONTSET (f) = NULL;
1923}
1924
5a7df7d7
GM
1925
1926/* Value is the best input style, given user preferences USER (already
1927 checked to be supported by Emacs), and styles supported by the
1928 input method XIM. */
1929
1930static XIMStyle
971de7fb 1931best_xim_style (XIMStyles *user, XIMStyles *xim)
5a7df7d7
GM
1932{
1933 int i, j;
1934
1935 for (i = 0; i < user->count_styles; ++i)
1936 for (j = 0; j < xim->count_styles; ++j)
1937 if (user->supported_styles[i] == xim->supported_styles[j])
1938 return user->supported_styles[i];
1939
1940 /* Return the default style. */
1941 return XIMPreeditNothing | XIMStatusNothing;
1942}
1943
1944/* Create XIC for frame F. */
1945
5df79d3d
GM
1946static XIMStyle xic_style;
1947
5a7df7d7 1948void
971de7fb 1949create_frame_xic (struct frame *f)
5a7df7d7 1950{
5a7df7d7
GM
1951 XIM xim;
1952 XIC xic = NULL;
1953 XFontSet xfs = NULL;
86779fac 1954
5a7df7d7
GM
1955 if (FRAME_XIC (f))
1956 return;
177c0ea7 1957
bb07fa29 1958 /* Create X fontset. */
b51238f5 1959 xfs = xic_create_xfontset (f);
5a7df7d7
GM
1960 xim = FRAME_X_XIM (f);
1961 if (xim)
1962 {
d9d57cb2
DL
1963 XRectangle s_area;
1964 XPoint spot;
5a7df7d7
GM
1965 XVaNestedList preedit_attr;
1966 XVaNestedList status_attr;
5a7df7d7 1967
d9d57cb2
DL
1968 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
1969 spot.x = 0; spot.y = 1;
86779fac 1970
5a7df7d7
GM
1971 /* Determine XIC style. */
1972 if (xic_style == 0)
1973 {
1974 XIMStyles supported_list;
1975 supported_list.count_styles = (sizeof supported_xim_styles
1976 / sizeof supported_xim_styles[0]);
1977 supported_list.supported_styles = supported_xim_styles;
1978 xic_style = best_xim_style (&supported_list,
1979 FRAME_X_XIM_STYLES (f));
1980 }
86779fac 1981
5a7df7d7
GM
1982 preedit_attr = XVaCreateNestedList (0,
1983 XNFontSet, xfs,
1984 XNForeground,
1985 FRAME_FOREGROUND_PIXEL (f),
1986 XNBackground,
1987 FRAME_BACKGROUND_PIXEL (f),
1988 (xic_style & XIMPreeditPosition
1989 ? XNSpotLocation
1990 : NULL),
1991 &spot,
1992 NULL);
1993 status_attr = XVaCreateNestedList (0,
1994 XNArea,
1995 &s_area,
1996 XNFontSet,
1997 xfs,
1998 XNForeground,
1999 FRAME_FOREGROUND_PIXEL (f),
2000 XNBackground,
2001 FRAME_BACKGROUND_PIXEL (f),
2002 NULL);
2003
2004 xic = XCreateIC (xim,
2005 XNInputStyle, xic_style,
6b61353c
KH
2006 XNClientWindow, FRAME_X_WINDOW (f),
2007 XNFocusWindow, FRAME_X_WINDOW (f),
5a7df7d7
GM
2008 XNStatusAttributes, status_attr,
2009 XNPreeditAttributes, preedit_attr,
2010 NULL);
2011 XFree (preedit_attr);
2012 XFree (status_attr);
2013 }
177c0ea7 2014
5a7df7d7
GM
2015 FRAME_XIC (f) = xic;
2016 FRAME_XIC_STYLE (f) = xic_style;
2017 FRAME_XIC_FONTSET (f) = xfs;
86779fac
GM
2018}
2019
5a7df7d7
GM
2020
2021/* Destroy XIC and free XIC fontset of frame F, if any. */
2022
2023void
971de7fb 2024free_frame_xic (struct frame *f)
5a7df7d7
GM
2025{
2026 if (FRAME_XIC (f) == NULL)
2027 return;
177c0ea7 2028
5a7df7d7 2029 XDestroyIC (FRAME_XIC (f));
c27ed90a 2030 xic_free_xfontset (f);
5a7df7d7
GM
2031
2032 FRAME_XIC (f) = NULL;
5a7df7d7
GM
2033}
2034
2035
2036/* Place preedit area for XIC of window W's frame to specified
2037 pixel position X/Y. X and Y are relative to window W. */
2038
2039void
971de7fb 2040xic_set_preeditarea (struct window *w, int x, int y)
5a7df7d7 2041{
d3d50620 2042 struct frame *f = XFRAME (w->frame);
5a7df7d7
GM
2043 XVaNestedList attr;
2044 XPoint spot;
177c0ea7 2045
17e6d491 2046 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
5a7df7d7
GM
2047 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2048 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2049 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2050 XFree (attr);
2051}
2052
2053
2054/* Place status area for XIC in bottom right corner of frame F.. */
2055
2056void
971de7fb 2057xic_set_statusarea (struct frame *f)
5a7df7d7
GM
2058{
2059 XIC xic = FRAME_XIC (f);
2060 XVaNestedList attr;
2061 XRectangle area;
2062 XRectangle *needed;
2063
2064 /* Negotiate geometry of status area. If input method has existing
2065 status area, use its current size. */
2066 area.x = area.y = area.width = area.height = 0;
2067 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2068 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2069 XFree (attr);
177c0ea7 2070
5a7df7d7
GM
2071 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2072 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2073 XFree (attr);
2074
2075 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2076 {
2077 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2078 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2079 XFree (attr);
2080 }
2081
2082 area.width = needed->width;
2083 area.height = needed->height;
be786000
KS
2084 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2085 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
488dd4c4 2086 - FRAME_MENUBAR_HEIGHT (f)
bfeabdc3 2087 - FRAME_TOOLBAR_TOP_HEIGHT (f)
488dd4c4 2088 - FRAME_INTERNAL_BORDER_WIDTH (f));
5a7df7d7
GM
2089 XFree (needed);
2090
2091 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
6b61353c 2092 XSetICValues (xic, XNStatusAttributes, attr, NULL);
5a7df7d7
GM
2093 XFree (attr);
2094}
2095
2096
2097/* Set X fontset for XIC of frame F, using base font name
2098 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2099
2100void
eec47d6b 2101xic_set_xfontset (struct frame *f, const char *base_fontname)
5a7df7d7
GM
2102{
2103 XVaNestedList attr;
2104 XFontSet xfs;
2105
c27ed90a
JD
2106 xic_free_xfontset (f);
2107
b51238f5 2108 xfs = xic_create_xfontset (f);
5a7df7d7
GM
2109
2110 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2111 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2112 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2113 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2114 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2115 XFree (attr);
177c0ea7 2116
5a7df7d7
GM
2117 FRAME_XIC_FONTSET (f) = xfs;
2118}
2119
2120#endif /* HAVE_X_I18N */
2121
2122
9ef48a9d 2123\f
8fc2766b
RS
2124#ifdef USE_X_TOOLKIT
2125
2126/* Create and set up the X widget for frame F. */
f58534a3 2127
01f1ba30 2128static void
ebd15611 2129x_window (struct frame *f, long window_prompting, int minibuffer_only)
01f1ba30 2130{
9ef48a9d 2131 XClassHint class_hints;
31ac8d8c
FP
2132 XSetWindowAttributes attributes;
2133 unsigned long attribute_mask;
9ef48a9d
RS
2134 Widget shell_widget;
2135 Widget pane_widget;
6c32dd68 2136 Widget frame_widget;
9ef48a9d
RS
2137 Arg al [25];
2138 int ac;
2139
4d7e6e51 2140 block_input ();
9ef48a9d 2141
b7975ee4
KH
2142 /* Use the resource name as the top-level widget name
2143 for looking up resources. Make a non-Lisp copy
2144 for the window manager, so GC relocation won't bother it.
2145
2146 Elsewhere we specify the window name for the window manager. */
309f24d1 2147 f->namebuf = xstrdup (SSDATA (Vx_resource_name));
9ef48a9d
RS
2148
2149 ac = 0;
2150 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2151 XtSetArg (al[ac], XtNinput, 1); ac++;
97787173 2152 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
be786000 2153 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
9b2956e2 2154 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
aad3612f 2155 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
9b2956e2 2156 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
cca176a0 2157 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
7a994728 2158 applicationShellWidgetClass,
82c90203 2159 FRAME_X_DISPLAY (f), al, ac);
9ef48a9d 2160
7556890b 2161 f->output_data.x->widget = shell_widget;
9ef48a9d
RS
2162 /* maybe_set_screen_title_format (shell_widget); */
2163
6c32dd68 2164 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
7d652d97
PE
2165 NULL, shell_widget, False,
2166 NULL, NULL, NULL, NULL);
9ef48a9d 2167
9b2956e2
GM
2168 ac = 0;
2169 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
aad3612f 2170 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
9b2956e2 2171 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
723f5a07 2172 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
9b2956e2 2173 XtSetValues (pane_widget, al, ac);
7556890b 2174 f->output_data.x->column_widget = pane_widget;
a7f7d550 2175
177c0ea7 2176 /* mappedWhenManaged to false tells to the paned window to not map/unmap
5e65b9ab 2177 the emacs screen when changing menubar. This reduces flickering. */
9ef48a9d
RS
2178
2179 ac = 0;
2180 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2181 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2182 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2183 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2184 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
9b2956e2 2185 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
aad3612f 2186 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
9b2956e2 2187 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
723f5a07 2188 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
9b2956e2
GM
2189 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2190 al, ac);
177c0ea7 2191
7556890b 2192 f->output_data.x->edit_widget = frame_widget;
177c0ea7
JB
2193
2194 XtManageChild (frame_widget);
a7f7d550
FP
2195
2196 /* Do some needed geometry management. */
2197 {
b7163a50 2198 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
8a94ea33
PE
2199 Arg gal[10];
2200 int gac = 0;
5031cc10 2201 int extra_borders = 0;
177c0ea7 2202 int menubar_size
7556890b
RS
2203 = (f->output_data.x->menubar_widget
2204 ? (f->output_data.x->menubar_widget->core.height
2205 + f->output_data.x->menubar_widget->core.border_width)
8fc2766b 2206 : 0);
a7f7d550 2207
f7008aff
RS
2208#if 0 /* Experimentally, we now get the right results
2209 for -geometry -0-0 without this. 24 Aug 96, rms. */
01cbdba5
RS
2210 if (FRAME_EXTERNAL_MENU_BAR (f))
2211 {
dd254b21 2212 Dimension ibw = 0;
01cbdba5
RS
2213 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2214 menubar_size += ibw;
2215 }
f7008aff 2216#endif
01cbdba5 2217
7556890b 2218 f->output_data.x->menubar_height = menubar_size;
00983aba 2219
440b0bfd 2220#ifndef USE_LUCID
5031cc10
KH
2221 /* Motif seems to need this amount added to the sizes
2222 specified for the shell widget. The Athena/Lucid widgets don't.
2223 Both conclusions reached experimentally. -- rms. */
440b0bfd
RS
2224 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2225 &extra_borders, NULL);
2226 extra_borders *= 2;
2227#endif
5031cc10 2228
97787173
RS
2229 /* Convert our geometry parameters into a geometry string
2230 and specify it.
2231 Note that we do not specify here whether the position
2232 is a user-specified or program-specified one.
2233 We pass that information later, in x_wm_set_size_hints. */
2234 {
be786000 2235 int left = f->left_pos;
97787173 2236 int xneg = window_prompting & XNegative;
be786000 2237 int top = f->top_pos;
97787173
RS
2238 int yneg = window_prompting & YNegative;
2239 if (xneg)
2240 left = -left;
2241 if (yneg)
2242 top = -top;
c760f47e
KH
2243
2244 if (window_prompting & USPosition)
5031cc10 2245 sprintf (shell_position, "=%dx%d%c%d%c%d",
be786000
KS
2246 FRAME_PIXEL_WIDTH (f) + extra_borders,
2247 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
c760f47e
KH
2248 (xneg ? '-' : '+'), left,
2249 (yneg ? '-' : '+'), top);
2250 else
6b61353c
KH
2251 {
2252 sprintf (shell_position, "=%dx%d",
2253 FRAME_PIXEL_WIDTH (f) + extra_borders,
2254 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2255
2256 /* Setting x and y when the position is not specified in
2257 the geometry string will set program position in the WM hints.
2258 If Emacs had just one program position, we could set it in
2259 fallback resources, but since each make-frame call can specify
2260 different program positions, this is easier. */
8a94ea33
PE
2261 XtSetArg (gal[gac], XtNx, left); gac++;
2262 XtSetArg (gal[gac], XtNy, top); gac++;
6b61353c 2263 }
97787173
RS
2264 }
2265
77110caa
RS
2266 /* We don't free this because we don't know whether
2267 it is safe to free it while the frame exists.
2268 It isn't worth the trouble of arranging to free it
2269 when the frame is deleted. */
b3b4476b 2270 tem = xstrdup (shell_position);
8a94ea33
PE
2271 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2272 XtSetValues (shell_widget, gal, gac);
a7f7d550
FP
2273 }
2274
9ef48a9d
RS
2275 XtManageChild (pane_widget);
2276 XtRealizeWidget (shell_widget);
2277
2532f20c
JD
2278 if (FRAME_X_EMBEDDED_P (f))
2279 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2280 f->output_data.x->parent_desc, 0, 0);
2281
177c0ea7 2282 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
9ef48a9d
RS
2283
2284 validate_x_resource_name ();
b7975ee4 2285
51b59d79
PE
2286 class_hints.res_name = SSDATA (Vx_resource_name);
2287 class_hints.res_class = SSDATA (Vx_resource_class);
b9dc4443 2288 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
5a7df7d7
GM
2289
2290#ifdef HAVE_X_I18N
2291 FRAME_XIC (f) = NULL;
1576f1ad
DL
2292 if (use_xim)
2293 create_frame_xic (f);
5a7df7d7 2294#endif
64d16748 2295
7556890b
RS
2296 f->output_data.x->wm_hints.input = True;
2297 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 2298 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 2299 &f->output_data.x->wm_hints);
b8228beb 2300
c4ec904f 2301 hack_wm_protocols (f, shell_widget);
9ef48a9d 2302
6c32dd68
PR
2303#ifdef HACK_EDITRES
2304 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2305#endif
2306
9ef48a9d 2307 /* Do a stupid property change to force the server to generate a
333b20bb 2308 PropertyNotify event so that the event_stream server timestamp will
9ef48a9d
RS
2309 be initialized to something relevant to the time we created the window.
2310 */
6c32dd68 2311 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
aad3612f 2312 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
7d652d97 2313 XA_ATOM, 32, PropModeAppend, NULL, 0);
9ef48a9d 2314
5a7df7d7 2315 /* Make all the standard events reach the Emacs frame. */
31ac8d8c 2316 attributes.event_mask = STANDARD_EVENT_SET;
5a7df7d7
GM
2317
2318#ifdef HAVE_X_I18N
2319 if (FRAME_XIC (f))
2320 {
2321 /* XIM server might require some X events. */
2322 unsigned long fevent = NoEventMask;
6b61353c 2323 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
5a7df7d7
GM
2324 attributes.event_mask |= fevent;
2325 }
2326#endif /* HAVE_X_I18N */
177c0ea7 2327
31ac8d8c
FP
2328 attribute_mask = CWEventMask;
2329 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2330 attribute_mask, &attributes);
2331
6c32dd68 2332 XtMapWidget (frame_widget);
9ef48a9d 2333
8fc2766b
RS
2334 /* x_set_name normally ignores requests to set the name if the
2335 requested name is the same as the current name. This is the one
2336 place where that assumption isn't correct; f->name is set, but
2337 the X server hasn't been told. */
2338 {
2339 Lisp_Object name;
2340 int explicit = f->explicit_name;
2341
2342 f->explicit_name = 0;
e69b0960 2343 name = f->name;
f00af5b1 2344 fset_name (f, Qnil);
8fc2766b
RS
2345 x_set_name (f, name, explicit);
2346 }
2347
b9dc4443 2348 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
db3b2dc8
JD
2349 f->output_data.x->current_cursor
2350 = f->output_data.x->text_cursor);
8fc2766b 2351
4d7e6e51 2352 unblock_input ();
8fc2766b 2353
495fa05e
GM
2354 /* This is a no-op, except under Motif. Make sure main areas are
2355 set to something reasonable, in case we get an error later. */
2356 lw_set_main_areas (pane_widget, 0, frame_widget);
8fc2766b
RS
2357}
2358
9ef48a9d 2359#else /* not USE_X_TOOLKIT */
488dd4c4 2360#ifdef USE_GTK
f78faa98 2361static void
a10c8269 2362x_window (struct frame *f)
488dd4c4
JD
2363{
2364 if (! xg_create_frame_widgets (f))
2365 error ("Unable to create window");
1fcfb866
JD
2366
2367#ifdef HAVE_X_I18N
2368 FRAME_XIC (f) = NULL;
6b61353c 2369 if (use_xim)
1576f1ad 2370 {
4d7e6e51 2371 block_input ();
1576f1ad
DL
2372 create_frame_xic (f);
2373 if (FRAME_XIC (f))
2374 {
2375 /* XIM server might require some X events. */
2376 unsigned long fevent = NoEventMask;
6b61353c 2377 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
1fcfb866 2378
1576f1ad
DL
2379 if (fevent != NoEventMask)
2380 {
2381 XSetWindowAttributes attributes;
2382 XWindowAttributes wattr;
2383 unsigned long attribute_mask;
2384
2385 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2386 &wattr);
2387 attributes.event_mask = wattr.your_event_mask | fevent;
2388 attribute_mask = CWEventMask;
2389 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2390 attribute_mask, &attributes);
2391 }
2392 }
4d7e6e51 2393 unblock_input ();
1576f1ad 2394 }
1fcfb866 2395#endif
488dd4c4 2396}
9ef48a9d 2397
488dd4c4 2398#else /*! USE_GTK */
8fc2766b
RS
2399/* Create and set up the X window for frame F. */
2400
f78faa98 2401static void
0521f580 2402x_window (struct frame *f)
8fc2766b
RS
2403{
2404 XClassHint class_hints;
2405 XSetWindowAttributes attributes;
2406 unsigned long attribute_mask;
2407
5bcee7ef 2408 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
7556890b 2409 attributes.border_pixel = f->output_data.x->border_pixel;
01f1ba30
JB
2410 attributes.bit_gravity = StaticGravity;
2411 attributes.backing_store = NotUseful;
2412 attributes.save_under = True;
2413 attributes.event_mask = STANDARD_EVENT_SET;
9b2956e2
GM
2414 attributes.colormap = FRAME_X_COLORMAP (f);
2415 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2416 | CWColormap);
01f1ba30 2417
4d7e6e51 2418 block_input ();
fe24a618 2419 FRAME_X_WINDOW (f)
b9dc4443 2420 = XCreateWindow (FRAME_X_DISPLAY (f),
7556890b 2421 f->output_data.x->parent_desc,
be786000
KS
2422 f->left_pos,
2423 f->top_pos,
2424 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2425 f->border_width,
01f1ba30
JB
2426 CopyFromParent, /* depth */
2427 InputOutput, /* class */
383d6ffc 2428 FRAME_X_VISUAL (f),
01f1ba30 2429 attribute_mask, &attributes);
5a7df7d7
GM
2430
2431#ifdef HAVE_X_I18N
4dacadcc 2432 if (use_xim)
5a7df7d7 2433 {
1576f1ad
DL
2434 create_frame_xic (f);
2435 if (FRAME_XIC (f))
2436 {
2437 /* XIM server might require some X events. */
2438 unsigned long fevent = NoEventMask;
6b61353c 2439 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
1576f1ad
DL
2440 attributes.event_mask |= fevent;
2441 attribute_mask = CWEventMask;
2442 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2443 attribute_mask, &attributes);
2444 }
5a7df7d7
GM
2445 }
2446#endif /* HAVE_X_I18N */
177c0ea7 2447
d387c960 2448 validate_x_resource_name ();
b7975ee4 2449
51b59d79
PE
2450 class_hints.res_name = SSDATA (Vx_resource_name);
2451 class_hints.res_class = SSDATA (Vx_resource_class);
b9dc4443 2452 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
01f1ba30 2453
00983aba
KH
2454 /* The menubar is part of the ordinary display;
2455 it does not count in addition to the height of the window. */
7556890b 2456 f->output_data.x->menubar_height = 0;
00983aba 2457
179956b9
JB
2458 /* This indicates that we use the "Passive Input" input model.
2459 Unless we do this, we don't get the Focus{In,Out} events that we
2460 need to draw the cursor correctly. Accursed bureaucrats.
b9dc4443 2461 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
179956b9 2462
7556890b
RS
2463 f->output_data.x->wm_hints.input = True;
2464 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 2465 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 2466 &f->output_data.x->wm_hints);
6d078211 2467 f->output_data.x->wm_hints.icon_pixmap = None;
179956b9 2468
032e4ebe
RS
2469 /* Request "save yourself" and "delete window" commands from wm. */
2470 {
2471 Atom protocols[2];
aad3612f
DA
2472 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2473 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
b9dc4443 2474 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
032e4ebe 2475 }
9ef48a9d 2476
e373f201
JB
2477 /* x_set_name normally ignores requests to set the name if the
2478 requested name is the same as the current name. This is the one
2479 place where that assumption isn't correct; f->name is set, but
2480 the X server hasn't been told. */
2481 {
98381190 2482 Lisp_Object name;
cf177271 2483 int explicit = f->explicit_name;
e373f201 2484
cf177271 2485 f->explicit_name = 0;
e69b0960 2486 name = f->name;
f00af5b1 2487 fset_name (f, Qnil);
cf177271 2488 x_set_name (f, name, explicit);
e373f201
JB
2489 }
2490
b9dc4443 2491 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
72ec0c8d
JD
2492 f->output_data.x->current_cursor
2493 = f->output_data.x->text_cursor);
9ef48a9d 2494
4d7e6e51 2495 unblock_input ();
01f1ba30 2496
fe24a618 2497 if (FRAME_X_WINDOW (f) == 0)
9ef48a9d 2498 error ("Unable to create window");
01f1ba30
JB
2499}
2500
488dd4c4 2501#endif /* not USE_GTK */
8fc2766b
RS
2502#endif /* not USE_X_TOOLKIT */
2503
289978b5
LT
2504/* Verify that the icon position args for this window are valid. */
2505
2506static void
971de7fb 2507x_icon_verify (struct frame *f, Lisp_Object parms)
289978b5
LT
2508{
2509 Lisp_Object icon_x, icon_y;
2510
2511 /* Set the position of the icon. Note that twm groups all
2512 icons in an icon window. */
2513 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2514 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2515 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2516 {
2517 CHECK_NUMBER (icon_x);
2518 CHECK_NUMBER (icon_y);
2519 }
2520 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2521 error ("Both left and top icon corners of icon must be specified");
2522}
2523
01f1ba30
JB
2524/* Handle the icon stuff for this window. Perhaps later we might
2525 want an x_set_icon_position which can be called interactively as
b9dc4443 2526 well. */
01f1ba30
JB
2527
2528static void
971de7fb 2529x_icon (struct frame *f, Lisp_Object parms)
01f1ba30 2530{
f9942c9e 2531 Lisp_Object icon_x, icon_y;
fd0f53a9 2532#if 0
aad3612f 2533 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
fd0f53a9 2534#endif
01f1ba30
JB
2535
2536 /* Set the position of the icon. Note that twm groups all
b9dc4443 2537 icons in an icon window. */
7c0d3ed8
KS
2538 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2539 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
f9942c9e 2540 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
01f1ba30 2541 {
d311d28c
PE
2542 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2543 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
01f1ba30 2544 }
f9942c9e 2545 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
01f1ba30 2546 error ("Both left and top icon corners of icon must be specified");
01f1ba30 2547
4d7e6e51 2548 block_input ();
f9942c9e 2549
fe24a618
JB
2550 if (! EQ (icon_x, Qunbound))
2551 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
f9942c9e 2552
a9d54793
KL
2553#if 0 /* x_get_arg removes the visibility parameter as a side effect,
2554 but x_create_frame still needs it. */
01f1ba30 2555 /* Start up iconic or window? */
49795535 2556 x_wm_set_window_state
333b20bb
GM
2557 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2558 Qicon)
49795535
JB
2559 ? IconicState
2560 : NormalState));
a9d54793 2561#endif
01f1ba30 2562
e69b0960
DA
2563 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2564 ? f->icon_name
2565 : f->name)));
80534dd6 2566
4d7e6e51 2567 unblock_input ();
01f1ba30
JB
2568}
2569
b243755a 2570/* Make the GCs needed for this window, setting the
01f1ba30
JB
2571 background, border and mouse colors; also create the
2572 mouse cursor and the gray border tile. */
2573
2574static void
971de7fb 2575x_make_gc (struct frame *f)
01f1ba30
JB
2576{
2577 XGCValues gc_values;
01f1ba30 2578
4d7e6e51 2579 block_input ();
6afb1d07 2580
b243755a 2581 /* Create the GCs of this frame.
9ef48a9d 2582 Note that many default values are used. */
01f1ba30 2583
ce593f6e
KL
2584 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2585 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
9ef48a9d 2586 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
959e647d
GM
2587 f->output_data.x->normal_gc
2588 = XCreateGC (FRAME_X_DISPLAY (f),
2589 FRAME_X_WINDOW (f),
b51238f5 2590 GCLineWidth | GCForeground | GCBackground,
959e647d 2591 &gc_values);
01f1ba30 2592
b9dc4443 2593 /* Reverse video style. */
ce593f6e
KL
2594 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2595 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
959e647d
GM
2596 f->output_data.x->reverse_gc
2597 = XCreateGC (FRAME_X_DISPLAY (f),
2598 FRAME_X_WINDOW (f),
b51238f5 2599 GCForeground | GCBackground | GCLineWidth,
959e647d 2600 &gc_values);
01f1ba30 2601
9ef48a9d 2602 /* Cursor has cursor-color background, background-color foreground. */
ce593f6e 2603 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
7556890b 2604 gc_values.background = f->output_data.x->cursor_pixel;
01f1ba30 2605 gc_values.fill_style = FillOpaqueStippled;
7556890b 2606 f->output_data.x->cursor_gc
b9dc4443 2607 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
b51238f5 2608 (GCForeground | GCBackground
c8bcd18a 2609 | GCFillStyle | GCLineWidth),
01f1ba30
JB
2610 &gc_values);
2611
2612 /* Create the gray border tile used when the pointer is not in
f676886a 2613 the frame. Since this depends on the frame's pixel values,
9ef48a9d 2614 this must be done on a per-frame basis. */
7556890b 2615 f->output_data.x->border_tile
d043f1a4 2616 = (XCreatePixmapFromBitmapData
aad3612f 2617 (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
d043f1a4 2618 gray_bits, gray_width, gray_height,
ce593f6e
KL
2619 FRAME_FOREGROUND_PIXEL (f),
2620 FRAME_BACKGROUND_PIXEL (f),
ab452f99 2621 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
6afb1d07 2622
4d7e6e51 2623 unblock_input ();
01f1ba30 2624}
01f1ba30 2625
959e647d 2626
2b34df4e 2627/* Free what was allocated in x_make_gc. */
959e647d
GM
2628
2629void
971de7fb 2630x_free_gcs (struct frame *f)
959e647d
GM
2631{
2632 Display *dpy = FRAME_X_DISPLAY (f);
2633
4d7e6e51 2634 block_input ();
177c0ea7 2635
959e647d
GM
2636 if (f->output_data.x->normal_gc)
2637 {
2638 XFreeGC (dpy, f->output_data.x->normal_gc);
2639 f->output_data.x->normal_gc = 0;
2640 }
2641
2642 if (f->output_data.x->reverse_gc)
2643 {
2644 XFreeGC (dpy, f->output_data.x->reverse_gc);
2645 f->output_data.x->reverse_gc = 0;
2646 }
177c0ea7 2647
959e647d
GM
2648 if (f->output_data.x->cursor_gc)
2649 {
2650 XFreeGC (dpy, f->output_data.x->cursor_gc);
2651 f->output_data.x->cursor_gc = 0;
2652 }
2653
2654 if (f->output_data.x->border_tile)
2655 {
2656 XFreePixmap (dpy, f->output_data.x->border_tile);
2657 f->output_data.x->border_tile = 0;
2658 }
2659
4d7e6e51 2660 unblock_input ();
959e647d
GM
2661}
2662
2663
eaf1eea9 2664/* Handler for signals raised during x_create_frame and
c9e7db78 2665 x_create_tip_frame. FRAME is the frame which is partially
eaf1eea9
GM
2666 constructed. */
2667
2668static Lisp_Object
971de7fb 2669unwind_create_frame (Lisp_Object frame)
eaf1eea9
GM
2670{
2671 struct frame *f = XFRAME (frame);
2672
8346e08b
KL
2673 /* If frame is already dead, nothing to do. This can happen if the
2674 display is disconnected after the frame has become official, but
2675 before x_create_frame removes the unwind protect. */
2676 if (!FRAME_LIVE_P (f))
2677 return Qnil;
2678
eaf1eea9 2679 /* If frame is ``official'', nothing to do. */
97f18cc8 2680 if (NILP (Fmemq (frame, Vframe_list)))
eaf1eea9 2681 {
e509cfa6 2682#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
aad3612f 2683 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
eaf1eea9 2684#endif
177c0ea7 2685
eaf1eea9 2686 x_free_frame_resources (f);
2a58bbc1 2687 free_glyphs (f);
eaf1eea9 2688
e509cfa6 2689#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
eaf1eea9 2690 /* Check that reference counts are indeed correct. */
a54e2c05
DA
2691 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2692 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
5b3f8550 2693#endif
c844a81a 2694 return Qt;
eaf1eea9 2695 }
177c0ea7 2696
eaf1eea9
GM
2697 return Qnil;
2698}
2699
27e498e6
PE
2700static void
2701do_unwind_create_frame (Lisp_Object frame)
2702{
2703 unwind_create_frame (frame);
2704}
2705
2706static void
f01d3321
CY
2707unwind_create_frame_1 (Lisp_Object val)
2708{
d1f55f16 2709 inhibit_lisp_code = val;
f01d3321 2710}
b51238f5 2711
02ed2ea8 2712static void
971de7fb 2713x_default_font_parameter (struct frame *f, Lisp_Object parms)
02ed2ea8 2714{
aad3612f 2715 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
770e2e6e 2716 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
637fa988 2717 RES_TYPE_STRING);
e9b7ab96 2718 Lisp_Object font = Qnil;
770e2e6e 2719 if (EQ (font_param, Qunbound))
0d1d0d26
JD
2720 font_param = Qnil;
2721
2722 if (NILP (font_param))
637fa988 2723 {
e1dbe924 2724 /* System font should take precedence over X resources. We suggest this
0d1d0d26
JD
2725 regardless of font-use-system-font because .emacs may not have been
2726 read yet. */
2727 const char *system_font = xsettings_get_system_font ();
e9b7ab96 2728 if (system_font)
d7ea76b4 2729 font = font_open_by_name (f, build_unibyte_string (system_font));
637fa988 2730 }
0d1d0d26 2731
e9b7ab96
JD
2732 if (NILP (font))
2733 font = !NILP (font_param) ? font_param
2734 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2735
2736 if (! FONTP (font) && ! STRINGP (font))
02ed2ea8 2737 {
675e2c69 2738 const char *names[]
be9d013a
CY
2739 = {
2740#ifdef HAVE_XFT
d7afccca 2741 /* This will find the normal Xft font. */
c024ac08 2742 "monospace-10",
be9d013a
CY
2743#endif
2744 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2745 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
02ed2ea8
KH
2746 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2747 /* This was formerly the first thing tried, but it finds
2748 too many fonts and takes too long. */
2749 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2750 /* If those didn't work, look for something which will
2751 at least work. */
2752 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
25c25256 2753 "fixed",
02ed2ea8
KH
2754 NULL };
2755 int i;
2756
2757 for (i = 0; names[i]; i++)
2758 {
d7ea76b4 2759 font = font_open_by_name (f, build_unibyte_string (names[i]));
02ed2ea8
KH
2760 if (! NILP (font))
2761 break;
2762 }
2763 if (NILP (font))
25c25256 2764 error ("No suitable font was found");
02ed2ea8 2765 }
770e2e6e 2766 else if (!NILP (font_param))
27129af9
SM
2767 {
2768 /* Remember the explicit font parameter, so we can re-apply it after
2769 we've applied the `default' face settings. */
6c6f1994 2770 x_set_frame_parameters (f, list1 (Fcons (Qfont_param, font_param)));
770e2e6e 2771 }
637fa988 2772
5fc8e5bc
J
2773 /* This call will make X resources override any system font setting. */
2774 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
02ed2ea8 2775}
b51238f5 2776
eaf1eea9 2777
1c44e124
CY
2778DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2779 0, 1, 0,
2780 doc: /* Send the size hints for frame FRAME to the window manager.
5890e400
DA
2781If FRAME is omitted or nil, use the selected frame.
2782Signal error if FRAME is not an X frame. */)
5842a27b 2783 (Lisp_Object frame)
1c44e124 2784{
7452b7bd 2785 struct frame *f = decode_window_system_frame (frame);
5890e400 2786
4d7e6e51 2787 block_input ();
5890e400 2788 x_wm_set_size_hint (f, 0, 0);
4d7e6e51 2789 unblock_input ();
653a3150 2790 return Qnil;
1c44e124
CY
2791}
2792
3e6bec3b
JD
2793static void
2794set_machine_and_pid_properties (struct frame *f)
2795{
ac61e7e1 2796 long pid = (long) getpid ();
3e6bec3b 2797
66b16767
J
2798 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2799 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2800 NULL, 0, NULL, NULL, NULL);
3e6bec3b
JD
2801 XChangeProperty (FRAME_X_DISPLAY (f),
2802 FRAME_OUTER_WINDOW (f),
2803 XInternAtom (FRAME_X_DISPLAY (f),
2804 "_NET_WM_PID",
2805 False),
2806 XA_CARDINAL, 32, PropModeReplace,
2807 (unsigned char *) &pid, 1);
2808}
2809
f676886a 2810DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
01f1ba30 2811 1, 1, 0,
7ee72033 2812 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
d29a9848 2813Return an Emacs frame object.
e0079d39 2814PARMS is an alist of frame parameters.
c061c855
GM
2815If the parameters specify that the frame should not have a minibuffer,
2816and do not specify a specific minibuffer window to use,
2817then `default-minibuffer-frame' must be a frame whose minibuffer can
2818be shared by the new frame.
2819
7ee72033 2820This function is an internal primitive--use `make-frame' instead. */)
5842a27b 2821 (Lisp_Object parms)
01f1ba30 2822{
f676886a 2823 struct frame *f;
2365c027 2824 Lisp_Object frame, tem;
01f1ba30
JB
2825 Lisp_Object name;
2826 int minibuffer_only = 0;
2827 long window_prompting = 0;
45d45af5 2828 int width, height;
d311d28c 2829 ptrdiff_t count = SPECPDL_INDEX ();
ecaca587 2830 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
b9dc4443 2831 Lisp_Object display;
333b20bb 2832 struct x_display_info *dpyinfo = NULL;
a59e4f3d 2833 Lisp_Object parent;
e557f19d 2834 struct kboard *kb;
01f1ba30 2835
5fa98fcf
RS
2836 parms = Fcopy_alist (parms);
2837
b7975ee4
KH
2838 /* Use this general default value to start with
2839 until we know if this frame has a specified name. */
2840 Vx_resource_name = Vinvocation_name;
2841
6ed8eeff 2842 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
1705c933 2843 if (EQ (display, Qunbound))
b6660415 2844 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
b9dc4443
RS
2845 if (EQ (display, Qunbound))
2846 display = Qnil;
2847 dpyinfo = check_x_display_info (display);
6ed8eeff 2848 kb = dpyinfo->terminal->kboard;
b9dc4443 2849
89acb56d
SM
2850 if (!dpyinfo->terminal->name)
2851 error ("Terminal is not live, can't create new frames on it");
ab797f65 2852
333b20bb 2853 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
6a5e54e2 2854 if (!STRINGP (name)
cf177271
JB
2855 && ! EQ (name, Qunbound)
2856 && ! NILP (name))
08a90d6a 2857 error ("Invalid frame name--not a string or nil");
01f1ba30 2858
b7975ee4
KH
2859 if (STRINGP (name))
2860 Vx_resource_name = name;
2861
a59e4f3d 2862 /* See if parent window is specified. */
333b20bb 2863 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
a59e4f3d
RS
2864 if (EQ (parent, Qunbound))
2865 parent = Qnil;
2866 if (! NILP (parent))
b7826503 2867 CHECK_NUMBER (parent);
a59e4f3d 2868
ecaca587
RS
2869 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
2870 /* No need to protect DISPLAY because that's not used after passing
2871 it to make_frame_without_minibuffer. */
2872 frame = Qnil;
2873 GCPRO4 (parms, parent, name, frame);
333b20bb
GM
2874 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
2875 RES_TYPE_SYMBOL);
f9942c9e 2876 if (EQ (tem, Qnone) || NILP (tem))
2526c290 2877 f = make_frame_without_minibuffer (Qnil, kb, display);
f9942c9e 2878 else if (EQ (tem, Qonly))
01f1ba30 2879 {
f676886a 2880 f = make_minibuffer_frame ();
01f1ba30
JB
2881 minibuffer_only = 1;
2882 }
6a5e54e2 2883 else if (WINDOWP (tem))
2526c290 2884 f = make_frame_without_minibuffer (tem, kb, display);
f9942c9e
JB
2885 else
2886 f = make_frame (1);
01f1ba30 2887
ecaca587
RS
2888 XSETFRAME (frame, f);
2889
6ed8eeff 2890 f->terminal = dpyinfo->terminal;
428a555e 2891
08a90d6a 2892 f->output_method = output_x_window;
38182d90 2893 f->output_data.x = xzalloc (sizeof *f->output_data.x);
7556890b 2894 f->output_data.x->icon_bitmap = -1;
be786000 2895 FRAME_FONTSET (f) = -1;
333b20bb
GM
2896 f->output_data.x->scroll_bar_foreground_pixel = -1;
2897 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
2898#ifdef USE_TOOLKIT_SCROLL_BARS
2899 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2900 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2901#endif /* USE_TOOLKIT_SCROLL_BARS */
0085b9be
DA
2902 f->output_data.x->white_relief.pixel = -1;
2903 f->output_data.x->black_relief.pixel = -1;
08a90d6a 2904
f00af5b1
PE
2905 fset_icon_name (f,
2906 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
2907 RES_TYPE_STRING));
e69b0960 2908 if (! STRINGP (f->icon_name))
f00af5b1 2909 fset_icon_name (f, Qnil);
80534dd6 2910
aad3612f 2911 FRAME_DISPLAY_INFO (f) = dpyinfo;
e3d56613 2912
aad3612f 2913 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
27e498e6 2914 record_unwind_protect (do_unwind_create_frame, frame);
08a90d6a 2915
9b2956e2
GM
2916 /* These colors will be set anyway later, but it's important
2917 to get the color reference counts right, so initialize them! */
2918 {
2919 Lisp_Object black;
dbf31225 2920 struct gcpro gcpro1;
cefecbcf
GM
2921
2922 /* Function x_decode_color can signal an error. Make
2923 sure to initialize color slots so that we won't try
2924 to free colors we haven't allocated. */
ce593f6e
KL
2925 FRAME_FOREGROUND_PIXEL (f) = -1;
2926 FRAME_BACKGROUND_PIXEL (f) = -1;
cefecbcf
GM
2927 f->output_data.x->cursor_pixel = -1;
2928 f->output_data.x->cursor_foreground_pixel = -1;
2929 f->output_data.x->border_pixel = -1;
2930 f->output_data.x->mouse_pixel = -1;
177c0ea7 2931
9b2956e2 2932 black = build_string ("black");
dbf31225 2933 GCPRO1 (black);
ce593f6e 2934 FRAME_FOREGROUND_PIXEL (f)
9b2956e2 2935 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
ce593f6e 2936 FRAME_BACKGROUND_PIXEL (f)
9b2956e2
GM
2937 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2938 f->output_data.x->cursor_pixel
2939 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2940 f->output_data.x->cursor_foreground_pixel
2941 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2942 f->output_data.x->border_pixel
2943 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2944 f->output_data.x->mouse_pixel
2945 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
dbf31225 2946 UNGCPRO;
9b2956e2
GM
2947 }
2948
a59e4f3d
RS
2949 /* Specify the parent under which to make this X window. */
2950
2951 if (!NILP (parent))
2952 {
8c239ac3 2953 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
7556890b 2954 f->output_data.x->explicit_parent = 1;
a59e4f3d
RS
2955 }
2956 else
2957 {
aad3612f 2958 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
7556890b 2959 f->output_data.x->explicit_parent = 0;
a59e4f3d
RS
2960 }
2961
cf177271
JB
2962 /* Set the name; the functions to which we pass f expect the name to
2963 be set. */
2964 if (EQ (name, Qunbound) || NILP (name))
2965 {
f00af5b1 2966 fset_name (f, build_string (dpyinfo->x_id_name));
cf177271
JB
2967 f->explicit_name = 0;
2968 }
2969 else
2970 {
f00af5b1 2971 fset_name (f, name);
cf177271 2972 f->explicit_name = 1;
9ef48a9d
RS
2973 /* use the frame's title when getting resources for this frame. */
2974 specbind (Qx_resource_name, name);
cf177271 2975 }
01f1ba30 2976
02ed2ea8
KH
2977#ifdef HAVE_FREETYPE
2978#ifdef HAVE_XFT
b51238f5 2979 register_font_driver (&xftfont_driver, f);
02ed2ea8 2980#else /* not HAVE_XFT */
b51238f5 2981 register_font_driver (&ftxfont_driver, f);
02ed2ea8
KH
2982#endif /* not HAVE_XFT */
2983#endif /* HAVE_FREETYPE */
6332668d 2984 register_font_driver (&xfont_driver, f);
297cc20a 2985
b51238f5
KH
2986 x_default_parameter (f, parms, Qfont_backend, Qnil,
2987 "fontBackend", "FontBackend", RES_TYPE_STRING);
02ed2ea8 2988
01f1ba30
JB
2989 /* Extract the window parameters from the supplied values
2990 that are needed to determine window geometry. */
b51238f5 2991 x_default_font_parameter (f, parms);
62d168d8
CY
2992 if (!FRAME_FONT (f))
2993 {
2994 delete_frame (frame, Qnoelisp);
2995 error ("Invalid frame font");
2996 }
9ef48a9d 2997
2532f20c
JD
2998 /* Frame contents get displaced if an embedded X window has a border. */
2999 if (! FRAME_X_EMBEDDED_P (f))
4bef8d26 3000 x_default_parameter (f, parms, Qborder_width, make_number (0),
2532f20c 3001 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
177c0ea7 3002
4e397688 3003 /* This defaults to 1 in order to match xterm. We recognize either
ddf768c3
JB
3004 internalBorderWidth or internalBorder (which is what xterm calls
3005 it). */
3006 if (NILP (Fassq (Qinternal_border_width, parms)))
3007 {
3008 Lisp_Object value;
3009
abb4b7ec 3010 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
333b20bb 3011 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
ddf768c3
JB
3012 if (! EQ (value, Qunbound))
3013 parms = Fcons (Fcons (Qinternal_border_width, value),
3014 parms);
3015 }
a099c27a
SM
3016 x_default_parameter (f, parms, Qinternal_border_width,
3017#ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3018 make_number (0),
3019#else
3020 make_number (1),
3021#endif
333b20bb
GM
3022 "internalBorderWidth", "internalBorderWidth",
3023 RES_TYPE_NUMBER);
303500aa 3024 x_default_parameter (f, parms, Qvertical_scroll_bars,
5e617bc2 3025#if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
303500aa
CY
3026 Qright,
3027#else
3028 Qleft,
3029#endif
333b20bb
GM
3030 "verticalScrollBars", "ScrollBars",
3031 RES_TYPE_SYMBOL);
01f1ba30 3032
b9dc4443 3033 /* Also do the stuff which must be set before the window exists. */
cf177271 3034 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
333b20bb 3035 "foreground", "Foreground", RES_TYPE_STRING);
cf177271 3036 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
0b60fc91 3037 "background", "Background", RES_TYPE_STRING);
cf177271 3038 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
333b20bb 3039 "pointerColor", "Foreground", RES_TYPE_STRING);
cf177271 3040 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
333b20bb 3041 "borderColor", "BorderColor", RES_TYPE_STRING);
d62c8769
GM
3042 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3043 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
563b67aa
GM
3044 x_default_parameter (f, parms, Qline_spacing, Qnil,
3045 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
b3ba0aa8
KS
3046 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3047 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3048 x_default_parameter (f, parms, Qright_fringe, Qnil,
3049 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
333b20bb
GM
3050
3051 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3052 "scrollBarForeground",
3053 "ScrollBarForeground", 1);
3054 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3055 "scrollBarBackground",
3056 "ScrollBarBackground", 0);
3057
e509cfa6 3058#ifdef GLYPH_DEBUG
c9e7db78
JD
3059 image_cache_refcount =
3060 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3061 dpyinfo_refcount = dpyinfo->reference_count;
3062#endif /* GLYPH_DEBUG */
3063
333b20bb
GM
3064 /* Init faces before x_default_parameter is called for scroll-bar
3065 parameters because that function calls x_set_scroll_bar_width,
3066 which calls change_frame_size, which calls Fset_window_buffer,
3067 which runs hooks, which call Fvertical_motion. At the end, we
3068 end up in init_iterator with a null face cache, which should not
3069 happen. */
3070 init_frame_faces (f);
177c0ea7 3071
f01d3321
CY
3072 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3073 look up the X resources controlling the menu-bar and tool-bar
3074 here; they are processed specially at startup, and reflected in
3075 the values of the mode variables.
3076
3077 Avoid calling window-configuration-change-hook; otherwise we
3078 could get an infloop in next_frame since the frame is not yet in
3079 Vframe_list. */
3080 {
a89654f8 3081 ptrdiff_t count2 = SPECPDL_INDEX ();
d1f55f16
CY
3082 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3083 inhibit_lisp_code = Qt;
f01d3321
CY
3084
3085 x_default_parameter (f, parms, Qmenu_bar_lines,
3086 NILP (Vmenu_bar_mode)
3087 ? make_number (0) : make_number (1),
3088 NULL, NULL, RES_TYPE_NUMBER);
3089 x_default_parameter (f, parms, Qtool_bar_lines,
3090 NILP (Vtool_bar_mode)
3091 ? make_number (0) : make_number (1),
3092 NULL, NULL, RES_TYPE_NUMBER);
3093
3094 unbind_to (count2, Qnil);
3095 }
6431f2e6 3096
79873d50 3097 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
333b20bb
GM
3098 "bufferPredicate", "BufferPredicate",
3099 RES_TYPE_SYMBOL);
c2304e02 3100 x_default_parameter (f, parms, Qtitle, Qnil,
333b20bb 3101 "title", "Title", RES_TYPE_STRING);
ea0a1f53
GM
3102 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3103 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
49d41073
EZ
3104 x_default_parameter (f, parms, Qfullscreen, Qnil,
3105 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
2b4e6277 3106 x_default_parameter (f, parms, Qtool_bar_position,
e69b0960 3107 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
90eb1019 3108
35f59f6b 3109 /* Compute the size of the X window. */
7c0d3ed8 3110 window_prompting = x_figure_window_size (f, parms, 1);
38d22040 3111
495fa05e
GM
3112 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3113 f->no_split = minibuffer_only || EQ (tem, Qt);
3114
289978b5
LT
3115 x_icon_verify (f, parms);
3116
6a1bcd01 3117 /* Create the X widget or window. */
a7f7d550
FP
3118#ifdef USE_X_TOOLKIT
3119 x_window (f, window_prompting, minibuffer_only);
3120#else
f676886a 3121 x_window (f);
a7f7d550 3122#endif
177c0ea7 3123
f676886a
JB
3124 x_icon (f, parms);
3125 x_make_gc (f);
01f1ba30 3126
495fa05e 3127 /* Now consider the frame official. */
c9e7db78 3128 f->terminal->reference_count++;
aad3612f 3129 FRAME_DISPLAY_INFO (f)->reference_count++;
495fa05e
GM
3130 Vframe_list = Fcons (frame, Vframe_list);
3131
f9942c9e
JB
3132 /* We need to do this after creating the X window, so that the
3133 icon-creation functions can say whose icon they're describing. */
84f25880 3134 x_default_parameter (f, parms, Qicon_type, Qt,
33ed493b 3135 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
f9942c9e 3136
cf177271 3137 x_default_parameter (f, parms, Qauto_raise, Qnil,
333b20bb 3138 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
cf177271 3139 x_default_parameter (f, parms, Qauto_lower, Qnil,
333b20bb 3140 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
dbc4e1c1 3141 x_default_parameter (f, parms, Qcursor_type, Qbox,
333b20bb 3142 "cursorType", "CursorType", RES_TYPE_SYMBOL);
28d7281d
GM
3143 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3144 "scrollBarWidth", "ScrollBarWidth",
3145 RES_TYPE_NUMBER);
271a71c7
GM
3146 x_default_parameter (f, parms, Qalpha, Qnil,
3147 "alpha", "Alpha", RES_TYPE_NUMBER);
f9942c9e 3148
be786000 3149 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
01f1ba30 3150 Change will not be effected unless different from the current
be786000 3151 FRAME_LINES (f). */
880e6158
MR
3152 width = FRAME_TEXT_WIDTH (f);
3153 height = FRAME_TEXT_HEIGHT (f);
3154 FRAME_TEXT_HEIGHT (f) = 0;
3155 SET_FRAME_WIDTH (f, 0);
3156 change_frame_size (f, width, height, 1, 0, 0, 1);
d043f1a4 3157
488dd4c4 3158#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
495fa05e
GM
3159 /* Create the menu bar. */
3160 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3161 {
3162 /* If this signals an error, we haven't set size hints for the
3163 frame and we didn't make it visible. */
3164 initialize_frame_menubar (f);
3165
488dd4c4 3166#ifndef USE_GTK
495fa05e
GM
3167 /* This is a no-op, except under Motif where it arranges the
3168 main window for the widgets on it. */
3169 lw_set_main_areas (f->output_data.x->column_widget,
3170 f->output_data.x->menubar_widget,
3171 f->output_data.x->edit_widget);
488dd4c4 3172#endif /* not USE_GTK */
495fa05e 3173 }
488dd4c4 3174#endif /* USE_X_TOOLKIT || USE_GTK */
495fa05e
GM
3175
3176 /* Tell the server what size and position, etc, we want, and how
3177 badly we want them. This should be done after we have the menu
3178 bar so that its size can be taken into account. */
4d7e6e51 3179 block_input ();
7989f084 3180 x_wm_set_size_hint (f, window_prompting, 0);
4d7e6e51 3181 unblock_input ();
01f1ba30 3182
495fa05e
GM
3183 /* Make the window appear on the frame and enable display, unless
3184 the caller says not to. However, with explicit parent, Emacs
3185 cannot control visibility, so don't try. */
7556890b 3186 if (! f->output_data.x->explicit_parent)
a59e4f3d
RS
3187 {
3188 Lisp_Object visibility;
49795535 3189
333b20bb
GM
3190 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3191 RES_TYPE_SYMBOL);
a59e4f3d
RS
3192 if (EQ (visibility, Qunbound))
3193 visibility = Qt;
49795535 3194
a59e4f3d
RS
3195 if (EQ (visibility, Qicon))
3196 x_iconify_frame (f);
3197 else if (! NILP (visibility))
3198 x_make_frame_visible (f);
3199 else
6b437900
PE
3200 {
3201 /* Must have been Qnil. */
3202 }
a59e4f3d 3203 }
01f1ba30 3204
4d7e6e51 3205 block_input ();
717c30e0 3206
3e6bec3b 3207 /* Set machine name and pid for the purpose of window managers. */
5e617bc2 3208 set_machine_and_pid_properties (f);
3e6bec3b 3209
6b61353c
KH
3210 /* Set the WM leader property. GTK does this itself, so this is not
3211 needed when using GTK. */
3212 if (dpyinfo->client_leader_window != 0)
3213 {
6b61353c
KH
3214 XChangeProperty (FRAME_X_DISPLAY (f),
3215 FRAME_OUTER_WINDOW (f),
3216 dpyinfo->Xatom_wm_client_leader,
3217 XA_WINDOW, 32, PropModeReplace,
47c53789 3218 (unsigned char *) &dpyinfo->client_leader_window, 1);
6b61353c
KH
3219 }
3220
4d7e6e51 3221 unblock_input ();
3e6bec3b 3222
225c13a5 3223 /* Initialize `default-minibuffer-frame' in case this is the first
6ed8eeff 3224 frame on this terminal. */
225c13a5 3225 if (FRAME_HAS_MINIBUF_P (f)
1344aad4
TT
3226 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3227 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
15dbb4d6 3228 kset_default_minibuffer_frame (kb, frame);
225c13a5 3229
5fa98fcf
RS
3230 /* All remaining specified parameters, which have not been "used"
3231 by x_get_arg and friends, now go in the misc. alist of the frame. */
9beb8baa 3232 for (tem = parms; CONSP (tem); tem = XCDR (tem))
5fa98fcf 3233 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
f00af5b1 3234 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
5fa98fcf 3235
495fa05e 3236 UNGCPRO;
9e57df62
GM
3237
3238 /* Make sure windows on this frame appear in calls to next-window
3239 and similar functions. */
3240 Vwindow_list = Qnil;
177c0ea7 3241
9ef48a9d 3242 return unbind_to (count, frame);
01f1ba30
JB
3243}
3244
eaf1eea9 3245
0d17d282
KH
3246/* FRAME is used only to get a handle on the X display. We don't pass the
3247 display info directly because we're called from frame.c, which doesn't
3248 know about that structure. */
e4f79258 3249
87498171 3250Lisp_Object
971de7fb 3251x_get_focus_frame (struct frame *frame)
87498171 3252{
aad3612f 3253 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
87498171 3254 Lisp_Object xfocus;
0d17d282 3255 if (! dpyinfo->x_focus_frame)
87498171
KH
3256 return Qnil;
3257
0d17d282 3258 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
87498171
KH
3259 return xfocus;
3260}
f0614854 3261
3decc1e7
GM
3262
3263/* In certain situations, when the window manager follows a
3264 click-to-focus policy, there seems to be no way around calling
3265 XSetInputFocus to give another frame the input focus .
3266
3267 In an ideal world, XSetInputFocus should generally be avoided so
3268 that applications don't interfere with the window manager's focus
3269 policy. But I think it's okay to use when it's clearly done
3270 following a user-command. */
3271
fcd42c11
DA
3272void
3273x_focus_frame (struct frame *f)
3decc1e7 3274{
3decc1e7 3275 Display *dpy = FRAME_X_DISPLAY (f);
3decc1e7 3276
4d7e6e51 3277 block_input ();
9ba8e10d 3278 x_catch_errors (dpy);
75bf0d33
DB
3279
3280 if (FRAME_X_EMBEDDED_P (f))
3281 {
3282 /* For Xembedded frames, normally the embedder forwards key
3283 events. See XEmbed Protocol Specification at
3284 http://freedesktop.org/wiki/Specifications/xembed-spec */
3285 xembed_request_focus (f);
3286 }
3287 else
3288 {
3289 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3290 RevertToParent, CurrentTime);
3291 x_ewmh_activate_frame (f);
3292 }
3293
4545fa20 3294 x_uncatch_errors ();
4d7e6e51 3295 unblock_input ();
3decc1e7
GM
3296}
3297
f0614854 3298\f
2d764c78 3299DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4f4f2973
GM
3300 doc: /* Internal function called by `color-defined-p', which see
3301.\(Note that the Nextstep version of this function ignores FRAME.) */)
5842a27b 3302 (Lisp_Object color, Lisp_Object frame)
e12d55b2 3303{
b9dc4443 3304 XColor foo;
a10c8269 3305 struct frame *f = decode_window_system_frame (frame);
e12d55b2 3306
b7826503 3307 CHECK_STRING (color);
b9dc4443 3308
42a5b22f 3309 if (x_defined_color (f, SSDATA (color), &foo, 0))
e12d55b2
RS
3310 return Qt;
3311 else
3312 return Qnil;
3313}
3314
2d764c78 3315DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
7ee72033 3316 doc: /* Internal function called by `color-values', which see. */)
5842a27b 3317 (Lisp_Object color, Lisp_Object frame)
01f1ba30 3318{
b9dc4443 3319 XColor foo;
a10c8269 3320 struct frame *f = decode_window_system_frame (frame);
b9dc4443 3321
b7826503 3322 CHECK_STRING (color);
01f1ba30 3323
42a5b22f 3324 if (x_defined_color (f, SSDATA (color), &foo, 0))
3de717bd 3325 return list3i (foo.red, foo.green, foo.blue);
01f1ba30
JB
3326 else
3327 return Qnil;
3328}
3329
a7ca3326 3330DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
7ee72033 3331 doc: /* Internal function called by `display-color-p', which see. */)
5842a27b 3332 (Lisp_Object terminal)
01f1ba30 3333{
6ed8eeff 3334 struct x_display_info *dpyinfo = check_x_display_info (terminal);
11ae94fe 3335
b9dc4443 3336 if (dpyinfo->n_planes <= 2)
01f1ba30
JB
3337 return Qnil;
3338
b9dc4443 3339 switch (dpyinfo->visual->class)
01f1ba30
JB
3340 {
3341 case StaticColor:
3342 case PseudoColor:
3343 case TrueColor:
3344 case DirectColor:
3345 return Qt;
3346
3347 default:
3348 return Qnil;
3349 }
3350}
3351
a7ca3326 3352DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
c061c855 3353 0, 1, 0,
7ee72033 3354 doc: /* Return t if the X display supports shades of gray.
c061c855 3355Note that color displays do support shades of gray.
6ed8eeff 3356The optional argument TERMINAL specifies which display to ask about.
708e05dc 3357TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3358If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3359 (Lisp_Object terminal)
d0c9d219 3360{
6ed8eeff 3361 struct x_display_info *dpyinfo = check_x_display_info (terminal);
d0c9d219 3362
ae6b58f9 3363 if (dpyinfo->n_planes <= 1)
b9dc4443
RS
3364 return Qnil;
3365
ae6b58f9
RS
3366 switch (dpyinfo->visual->class)
3367 {
3368 case StaticColor:
3369 case PseudoColor:
3370 case TrueColor:
3371 case DirectColor:
3372 case StaticGray:
3373 case GrayScale:
3374 return Qt;
3375
3376 default:
3377 return Qnil;
3378 }
d0c9d219
RS
3379}
3380
41beb8fc 3381DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
c061c855 3382 0, 1, 0,
d29a9848 3383 doc: /* Return the width in pixels of the X display TERMINAL.
6ed8eeff 3384The optional argument TERMINAL specifies which display to ask about.
708e05dc 3385TERMINAL should be a terminal object, a frame or a display name (a string).
d6635ba2
YM
3386If omitted or nil, that stands for the selected frame's display.
3387
3388On \"multi-monitor\" setups this refers to the pixel width for all
3389physical monitors associated with TERMINAL. To get information for
3390each physical monitor, use `display-monitor-attributes-list'. */)
5842a27b 3391 (Lisp_Object terminal)
41beb8fc 3392{
6ed8eeff 3393 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443 3394
abe9d29c 3395 return make_number (x_display_pixel_width (dpyinfo));
41beb8fc
RS
3396}
3397
3398DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
c061c855 3399 Sx_display_pixel_height, 0, 1, 0,
d29a9848 3400 doc: /* Return the height in pixels of the X display TERMINAL.
6ed8eeff 3401The optional argument TERMINAL specifies which display to ask about.
708e05dc 3402TERMINAL should be a terminal object, a frame or a display name (a string).
d6635ba2
YM
3403If omitted or nil, that stands for the selected frame's display.
3404
3405On \"multi-monitor\" setups this refers to the pixel height for all
3406physical monitors associated with TERMINAL. To get information for
3407each physical monitor, use `display-monitor-attributes-list'. */)
5842a27b 3408 (Lisp_Object terminal)
41beb8fc 3409{
6ed8eeff 3410 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443 3411
abe9d29c 3412 return make_number (x_display_pixel_height (dpyinfo));
41beb8fc
RS
3413}
3414
3415DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
c061c855 3416 0, 1, 0,
d29a9848 3417 doc: /* Return the number of bitplanes of the X display TERMINAL.
6ed8eeff 3418The optional argument TERMINAL specifies which display to ask about.
708e05dc 3419TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3420If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3421 (Lisp_Object terminal)
41beb8fc 3422{
6ed8eeff 3423 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3424
3425 return make_number (dpyinfo->n_planes);
41beb8fc
RS
3426}
3427
3428DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
c061c855 3429 0, 1, 0,
d29a9848 3430 doc: /* Return the number of color cells of the X display TERMINAL.
6ed8eeff 3431The optional argument TERMINAL specifies which display to ask about.
708e05dc 3432TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3433If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3434 (Lisp_Object terminal)
41beb8fc 3435{
6ed8eeff 3436 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443 3437
6b61353c
KH
3438 int nr_planes = DisplayPlanes (dpyinfo->display,
3439 XScreenNumberOfScreen (dpyinfo->screen));
3440
3441 /* Truncate nr_planes to 24 to avoid integer overflow.
3442 Some displays says 32, but only 24 bits are actually significant.
3443 There are only very few and rare video cards that have more than
3444 24 significant bits. Also 24 bits is more than 16 million colors,
3445 it "should be enough for everyone". */
3446 if (nr_planes > 24) nr_planes = 24;
3447
3448 return make_number (1 << nr_planes);
41beb8fc
RS
3449}
3450
9d317b2c
RS
3451DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3452 Sx_server_max_request_size,
c061c855 3453 0, 1, 0,
d29a9848 3454 doc: /* Return the maximum request size of the X server of display TERMINAL.
6ed8eeff 3455The optional argument TERMINAL specifies which display to ask about.
708e05dc 3456TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3457If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3458 (Lisp_Object terminal)
9d317b2c 3459{
6ed8eeff 3460 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3461
3462 return make_number (MAXREQUEST (dpyinfo->display));
9d317b2c
RS
3463}
3464
41beb8fc 3465DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
d29a9848 3466 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
09e80d9f 3467\(Labeling every distributor as a "vendor" embodies the false assumption
95d62d8a 3468that operating systems cannot be developed and distributed noncommercially.)
6ed8eeff 3469The optional argument TERMINAL specifies which display to ask about.
708e05dc 3470TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3471If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3472 (Lisp_Object terminal)
41beb8fc 3473{
6ed8eeff 3474 struct x_display_info *dpyinfo = check_x_display_info (terminal);
675e2c69 3475 const char *vendor = ServerVendor (dpyinfo->display);
b9dc4443 3476
41beb8fc
RS
3477 if (! vendor) vendor = "";
3478 return build_string (vendor);
3479}
3480
3481DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
d29a9848 3482 doc: /* Return the version numbers of the X server of display TERMINAL.
c061c855 3483The value is a list of three integers: the major and minor
95d62d8a 3484version numbers of the X Protocol in use, and the distributor-specific release
c061c855
GM
3485number. See also the function `x-server-vendor'.
3486
6ed8eeff 3487The optional argument TERMINAL specifies which display to ask about.
708e05dc 3488TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3489If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3490 (Lisp_Object terminal)
41beb8fc 3491{
6ed8eeff 3492 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443 3493 Display *dpy = dpyinfo->display;
11ae94fe 3494
3de717bd
DA
3495 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3496 VendorRelease (dpy));
41beb8fc
RS
3497}
3498
3499DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6ed8eeff
KL
3500 doc: /* Return the number of screens on the X server of display TERMINAL.
3501The optional argument TERMINAL specifies which display to ask about.
708e05dc 3502TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3503If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3504 (Lisp_Object terminal)
41beb8fc 3505{
6ed8eeff 3506 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3507
3508 return make_number (ScreenCount (dpyinfo->display));
41beb8fc
RS
3509}
3510
3511DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
6ed8eeff
KL
3512 doc: /* Return the height in millimeters of the X display TERMINAL.
3513The optional argument TERMINAL specifies which display to ask about.
708e05dc 3514TERMINAL should be a terminal object, a frame or a display name (a string).
d6635ba2
YM
3515If omitted or nil, that stands for the selected frame's display.
3516
3517On \"multi-monitor\" setups this refers to the height in millimeters for
3518all physical monitors associated with TERMINAL. To get information
3519for each physical monitor, use `display-monitor-attributes-list'. */)
5842a27b 3520 (Lisp_Object terminal)
41beb8fc 3521{
6ed8eeff 3522 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3523
3524 return make_number (HeightMMOfScreen (dpyinfo->screen));
41beb8fc
RS
3525}
3526
3527DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
6ed8eeff
KL
3528 doc: /* Return the width in millimeters of the X display TERMINAL.
3529The optional argument TERMINAL specifies which display to ask about.
708e05dc 3530TERMINAL should be a terminal object, a frame or a display name (a string).
d6635ba2
YM
3531If omitted or nil, that stands for the selected frame's display.
3532
3533On \"multi-monitor\" setups this refers to the width in millimeters for
3534all physical monitors associated with TERMINAL. To get information
3535for each physical monitor, use `display-monitor-attributes-list'. */)
5842a27b 3536 (Lisp_Object terminal)
41beb8fc 3537{
6ed8eeff 3538 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3539
3540 return make_number (WidthMMOfScreen (dpyinfo->screen));
41beb8fc
RS
3541}
3542
3543DEFUN ("x-display-backing-store", Fx_display_backing_store,
c061c855 3544 Sx_display_backing_store, 0, 1, 0,
d29a9848 3545 doc: /* Return an indication of whether X display TERMINAL does backing store.
c061c855 3546The value may be `always', `when-mapped', or `not-useful'.
6ed8eeff 3547The optional argument TERMINAL specifies which display to ask about.
708e05dc 3548TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3549If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3550 (Lisp_Object terminal)
41beb8fc 3551{
6ed8eeff 3552 struct x_display_info *dpyinfo = check_x_display_info (terminal);
8ec8a5ec 3553 Lisp_Object result;
11ae94fe 3554
b9dc4443 3555 switch (DoesBackingStore (dpyinfo->screen))
41beb8fc
RS
3556 {
3557 case Always:
8ec8a5ec
GM
3558 result = intern ("always");
3559 break;
41beb8fc
RS
3560
3561 case WhenMapped:
8ec8a5ec
GM
3562 result = intern ("when-mapped");
3563 break;
41beb8fc
RS
3564
3565 case NotUseful:
8ec8a5ec
GM
3566 result = intern ("not-useful");
3567 break;
41beb8fc
RS
3568
3569 default:
3570 error ("Strange value for BackingStore parameter of screen");
3571 }
8ec8a5ec
GM
3572
3573 return result;
41beb8fc
RS
3574}
3575
3576DEFUN ("x-display-visual-class", Fx_display_visual_class,
c061c855 3577 Sx_display_visual_class, 0, 1, 0,
6ed8eeff 3578 doc: /* Return the visual class of the X display TERMINAL.
c061c855
GM
3579The value is one of the symbols `static-gray', `gray-scale',
3580`static-color', `pseudo-color', `true-color', or `direct-color'.
3581
6ed8eeff 3582The optional argument TERMINAL specifies which display to ask about.
708e05dc 3583TERMINAL should a terminal object, a frame or a display name (a string).
7ee72033 3584If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3585 (Lisp_Object terminal)
41beb8fc 3586{
6ed8eeff 3587 struct x_display_info *dpyinfo = check_x_display_info (terminal);
8ec8a5ec 3588 Lisp_Object result;
11ae94fe 3589
b9dc4443 3590 switch (dpyinfo->visual->class)
41beb8fc 3591 {
8ec8a5ec
GM
3592 case StaticGray:
3593 result = intern ("static-gray");
3594 break;
3595 case GrayScale:
3596 result = intern ("gray-scale");
3597 break;
3598 case StaticColor:
3599 result = intern ("static-color");
3600 break;
3601 case PseudoColor:
3602 result = intern ("pseudo-color");
3603 break;
3604 case TrueColor:
3605 result = intern ("true-color");
3606 break;
3607 case DirectColor:
3608 result = intern ("direct-color");
3609 break;
41beb8fc
RS
3610 default:
3611 error ("Display has an unknown visual class");
3612 }
177c0ea7 3613
8ec8a5ec 3614 return result;
41beb8fc
RS
3615}
3616
3617DEFUN ("x-display-save-under", Fx_display_save_under,
c061c855 3618 Sx_display_save_under, 0, 1, 0,
d29a9848 3619 doc: /* Return t if the X display TERMINAL supports the save-under feature.
6ed8eeff 3620The optional argument TERMINAL specifies which display to ask about.
708e05dc 3621TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3622If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3623 (Lisp_Object terminal)
41beb8fc 3624{
6ed8eeff 3625 struct x_display_info *dpyinfo = check_x_display_info (terminal);
11ae94fe 3626
b9dc4443 3627 if (DoesSaveUnders (dpyinfo->screen) == True)
41beb8fc
RS
3628 return Qt;
3629 else
3630 return Qnil;
3631}
4e3f9230
YM
3632
3633/* Store the geometry of the workarea on display DPYINFO into *RECT.
3634 Return false if and only if the workarea information cannot be
3635 obtained via the _NET_WORKAREA root window property. */
3636
7583e2a0 3637#if ! GTK_CHECK_VERSION (3, 4, 0)
4e3f9230
YM
3638static bool
3639x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3640{
3641 Display *dpy = dpyinfo->display;
3642 long offset, max_len;
3643 Atom target_type, actual_type;
3644 unsigned long actual_size, bytes_remaining;
3645 int rc, actual_format;
3646 unsigned char *tmp_data = NULL;
3647 bool result = false;
3648
3649 x_catch_errors (dpy);
3650 offset = 0;
3651 max_len = 1;
3652 target_type = XA_CARDINAL;
3653 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3654 dpyinfo->Xatom_net_current_desktop,
3655 offset, max_len, False, target_type,
3656 &actual_type, &actual_format, &actual_size,
3657 &bytes_remaining, &tmp_data);
3658 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3659 && actual_format == 32 && actual_size == max_len)
3660 {
3661 long current_desktop = ((long *) tmp_data)[0];
3662
3663 XFree (tmp_data);
3664 tmp_data = NULL;
3665
3666 offset = 4 * current_desktop;
3667 max_len = 4;
3668 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3669 dpyinfo->Xatom_net_workarea,
3670 offset, max_len, False, target_type,
3671 &actual_type, &actual_format, &actual_size,
3672 &bytes_remaining, &tmp_data);
3673 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3674 && actual_format == 32 && actual_size == max_len)
3675 {
3676 long *values = (long *) tmp_data;
3677
3678 rect->x = values[0];
3679 rect->y = values[1];
3680 rect->width = values[2];
3681 rect->height = values[3];
3682
3683 XFree (tmp_data);
3684 tmp_data = NULL;
3685
3686 result = true;
3687 }
3688 }
3689 if (tmp_data)
3690 XFree (tmp_data);
3691 x_uncatch_errors ();
3692
3693 return result;
3694}
2b66427d 3695#endif
4e3f9230 3696
5ea03bf5
JD
3697#ifndef USE_GTK
3698
4e3f9230
YM
3699/* Return monitor number where F is "most" or closest to. */
3700static int
3701x_get_monitor_for_frame (struct frame *f,
3702 struct MonitorInfo *monitors,
3703 int n_monitors)
3704{
3705 XRectangle frect;
3706 int area = 0, dist = -1;
3707 int best_area = -1, best_dist = -1;
3708 int i;
3709
3710 if (n_monitors == 1) return 0;
3711 frect.x = f->left_pos;
3712 frect.y = f->top_pos;
3713 frect.width = FRAME_PIXEL_WIDTH (f);
3714 frect.height = FRAME_PIXEL_HEIGHT (f);
3715
3716 for (i = 0; i < n_monitors; ++i)
3717 {
3718 struct MonitorInfo *mi = &monitors[i];
3719 XRectangle res;
3720 int a = 0;
3721
3722 if (mi->geom.width == 0) continue;
3723
3724 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3725 {
3726 a = res.width * res.height;
e0c9d565
PE
3727 if (a > area)
3728 {
3729 area = a;
3730 best_area = i;
3731 }
4e3f9230
YM
3732 }
3733
3734 if (a == 0 && area == 0)
3735 {
3736 int dx, dy, d;
3737 if (frect.x + frect.width < mi->geom.x)
3738 dx = mi->geom.x - frect.x + frect.width;
3739 else if (frect.x > mi->geom.x + mi->geom.width)
3740 dx = frect.x - mi->geom.x + mi->geom.width;
3741 else
3742 dx = 0;
3743 if (frect.y + frect.height < mi->geom.y)
3744 dy = mi->geom.y - frect.y + frect.height;
3745 else if (frect.y > mi->geom.y + mi->geom.height)
3746 dy = frect.y - mi->geom.y + mi->geom.height;
3747 else
3748 dy = 0;
3749
3750 d = dx*dx + dy*dy;
3751 if (dist == -1 || dist > d)
3752 {
3753 dist = d;
3754 best_dist = i;
3755 }
3756 }
3757 }
3758
3759 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3760}
3761
3762static Lisp_Object
3763x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3764 int n_monitors,
3765 int primary_monitor,
3766 struct x_display_info *dpyinfo,
3767 const char *source)
3768{
3769 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
6799bb26 3770 Lisp_Object frame, rest;
4e3f9230
YM
3771
3772 FOR_EACH_FRAME (rest, frame)
3773 {
3774 struct frame *f = XFRAME (frame);
3775
aad3612f 3776 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4e3f9230
YM
3777 && !EQ (frame, tip_frame))
3778 {
6799bb26 3779 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
4e3f9230
YM
3780 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3781 }
3782 }
3783
6799bb26
JD
3784 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3785 monitor_frames, source);
4e3f9230
YM
3786}
3787
3788static Lisp_Object
3789x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3790{
3791 struct MonitorInfo monitor;
4e3f9230
YM
3792 XRectangle workarea_r;
3793
3794 /* Fallback: treat (possibly) multiple physical monitors as if they
3795 formed a single monitor as a whole. This should provide a
3796 consistent result at least on single monitor environments. */
3797 monitor.geom.x = monitor.geom.y = 0;
3798 monitor.geom.width = x_display_pixel_width (dpyinfo);
3799 monitor.geom.height = x_display_pixel_height (dpyinfo);
3800 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3801 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3802 monitor.name = xstrdup ("combined screen");
3803
3804 if (x_get_net_workarea (dpyinfo, &workarea_r))
3805 monitor.work = workarea_r;
3806 else
3807 monitor.work = monitor.geom;
3808 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3809}
3810
3811
3812#ifdef HAVE_XINERAMA
3813static Lisp_Object
3814x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3815{
3816 int n_monitors, i;
3817 Lisp_Object attributes_list = Qnil;
3818 Display *dpy = dpyinfo->display;
3819 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3820 struct MonitorInfo *monitors;
e0c9d565 3821 double mm_width_per_pixel, mm_height_per_pixel;
4e3f9230
YM
3822
3823 if (! info || n_monitors == 0)
3824 {
3825 if (info)
3826 XFree (info);
3827 return attributes_list;
3828 }
3829
e0c9d565 3830 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4e3f9230 3831 / x_display_pixel_width (dpyinfo));
e0c9d565 3832 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4e3f9230 3833 / x_display_pixel_height (dpyinfo));
7d652d97 3834 monitors = xzalloc (n_monitors * sizeof *monitors);
4e3f9230
YM
3835 for (i = 0; i < n_monitors; ++i)
3836 {
3837 struct MonitorInfo *mi = &monitors[i];
3838 XRectangle workarea_r;
3839
3840 mi->geom.x = info[i].x_org;
3841 mi->geom.y = info[i].y_org;
3842 mi->geom.width = info[i].width;
3843 mi->geom.height = info[i].height;
3844 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3845 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3846 mi->name = 0;
3847
3848 /* Xinerama usually have primary monitor first, just use that. */
3849 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3850 {
3851 mi->work = workarea_r;
3852 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3853 mi->work = mi->geom;
3854 }
3855 else
3856 mi->work = mi->geom;
3857 }
3858 XFree (info);
3859
3860 attributes_list = x_make_monitor_attribute_list (monitors,
3861 n_monitors,
3862 0,
3863 dpyinfo,
3864 "Xinerama");
3865 free_monitors (monitors, n_monitors);
3866 return attributes_list;
3867}
3868#endif /* HAVE_XINERAMA */
3869
3870
3871#ifdef HAVE_XRANDR
3872static Lisp_Object
3873x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3874{
3875 Lisp_Object attributes_list = Qnil;
3876 XRRScreenResources *resources;
3877 Display *dpy = dpyinfo->display;
3878 int i, n_monitors, primary = -1;
3879 RROutput pxid = None;
3880 struct MonitorInfo *monitors;
3881
3882#ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3883 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3884#else
3885 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3886#endif
3887 if (! resources || resources->noutput == 0)
3888 {
3889 if (resources)
3890 XRRFreeScreenResources (resources);
3891 return Qnil;
3892 }
3893 n_monitors = resources->noutput;
7d652d97 3894 monitors = xzalloc (n_monitors * sizeof *monitors);
4e3f9230
YM
3895
3896#ifdef HAVE_XRRGETOUTPUTPRIMARY
3897 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3898#endif
3899
3900 for (i = 0; i < n_monitors; ++i)
3901 {
3902 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3903 resources->outputs[i]);
3904 Connection conn = info ? info->connection : RR_Disconnected;
3905 RRCrtc id = info ? info->crtc : None;
3906
3907 if (strcmp (info->name, "default") == 0)
3908 {
3909 /* Non XRandr 1.2 driver, does not give useful data. */
3910 XRRFreeOutputInfo (info);
3911 XRRFreeScreenResources (resources);
3912 free_monitors (monitors, n_monitors);
3913 return Qnil;
3914 }
3915
3916 if (conn != RR_Disconnected && id != None)
3917 {
3918 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
3919 struct MonitorInfo *mi = &monitors[i];
3920 XRectangle workarea_r;
3921
3922 if (! crtc)
3923 {
3924 XRRFreeOutputInfo (info);
3925 continue;
3926 }
3927
3928 mi->geom.x = crtc->x;
3929 mi->geom.y = crtc->y;
3930 mi->geom.width = crtc->width;
3931 mi->geom.height = crtc->height;
3932 mi->mm_width = info->mm_width;
3933 mi->mm_height = info->mm_height;
3934 mi->name = xstrdup (info->name);
3935
3936 if (pxid != None && pxid == resources->outputs[i])
3937 primary = i;
3938 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
3939 primary = i;
3940
3941 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
3942 {
3943 mi->work= workarea_r;
3944 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3945 mi->work = mi->geom;
3946 }
3947 else
3948 mi->work = mi->geom;
3949
3950 XRRFreeCrtcInfo (crtc);
3951 }
3952 XRRFreeOutputInfo (info);
3953 }
3954 XRRFreeScreenResources (resources);
3955
3956 attributes_list = x_make_monitor_attribute_list (monitors,
3957 n_monitors,
3958 primary,
3959 dpyinfo,
3960 "XRandr");
3961 free_monitors (monitors, n_monitors);
3962 return attributes_list;
3963}
3964#endif /* HAVE_XRANDR */
3965
3966static Lisp_Object
3967x_get_monitor_attributes (struct x_display_info *dpyinfo)
3968{
3969 Lisp_Object attributes_list = Qnil;
3970 Display *dpy = dpyinfo->display;
3971
cd9356f2
DC
3972 (void) dpy; /* Suppress unused variable warning. */
3973
4e3f9230
YM
3974#ifdef HAVE_XRANDR
3975 int xrr_event_base, xrr_error_base;
3976 bool xrr_ok = false;
3977 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
3978 if (xrr_ok)
3979 {
3980 int xrr_major, xrr_minor;
3981 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
3982 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
3983 }
3984
3985 if (xrr_ok)
3986 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
3987#endif /* HAVE_XRANDR */
3988
3989#ifdef HAVE_XINERAMA
3990 if (NILP (attributes_list))
3991 {
3992 int xin_event_base, xin_error_base;
3993 bool xin_ok = false;
3994 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
3995 if (xin_ok && XineramaIsActive (dpy))
3996 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
3997 }
3998#endif /* HAVE_XINERAMA */
3999
4000 if (NILP (attributes_list))
4001 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4002
4003 return attributes_list;
4004}
4005
e0c9d565
PE
4006#endif /* !USE_GTK */
4007
4e3f9230
YM
4008DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4009 Sx_display_monitor_attributes_list,
4010 0, 1, 0,
4011 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4012
4013The optional argument TERMINAL specifies which display to ask about.
4014TERMINAL should be a terminal object, a frame or a display name (a string).
4015If omitted or nil, that stands for the selected frame's display.
4016
4017In addition to the standard attribute keys listed in
4018`display-monitor-attributes-list', the following keys are contained in
4019the attributes:
4020
4021 source -- String describing the source from which multi-monitor
4022 information is obtained, one of \"Gdk\", \"XRandr\",
4023 \"Xinerama\", or \"fallback\"
4024
4025Internal use only, use `display-monitor-attributes-list' instead. */)
4026 (Lisp_Object terminal)
4027{
4028 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4029 Lisp_Object attributes_list = Qnil;
4030
4031#ifdef USE_GTK
e0c9d565 4032 double mm_width_per_pixel, mm_height_per_pixel;
4e3f9230
YM
4033 GdkDisplay *gdpy;
4034 GdkScreen *gscreen;
4035 gint primary_monitor = 0, n_monitors, i;
4e3f9230
YM
4036 Lisp_Object monitor_frames, rest, frame;
4037 static const char *source = "Gdk";
6799bb26 4038 struct MonitorInfo *monitors;
4e3f9230
YM
4039
4040 block_input ();
e0c9d565 4041 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4e3f9230 4042 / x_display_pixel_width (dpyinfo));
e0c9d565 4043 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4e3f9230
YM
4044 / x_display_pixel_height (dpyinfo));
4045 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4046 gscreen = gdk_display_get_default_screen (gdpy);
7583e2a0 4047#if GTK_CHECK_VERSION (2, 20, 0)
4e3f9230
YM
4048 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4049#endif
4050 n_monitors = gdk_screen_get_n_monitors (gscreen);
4051 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
7d652d97 4052 monitors = xzalloc (n_monitors * sizeof *monitors);
6799bb26 4053
4e3f9230
YM
4054 FOR_EACH_FRAME (rest, frame)
4055 {
4056 struct frame *f = XFRAME (frame);
4057
aad3612f 4058 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4e3f9230
YM
4059 && !EQ (frame, tip_frame))
4060 {
4061 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4062
4063 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4064 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4065 }
4066 }
4067
6799bb26 4068 for (i = 0; i < n_monitors; ++i)
4e3f9230 4069 {
4e3f9230 4070 gint width_mm = -1, height_mm = -1;
6799bb26
JD
4071 GdkRectangle rec, work;
4072 struct MonitorInfo *mi = &monitors[i];
4e3f9230
YM
4073
4074 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4e3f9230 4075
7583e2a0 4076#if GTK_CHECK_VERSION (2, 14, 0)
4e3f9230
YM
4077 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4078 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4079#endif
4080 if (width_mm < 0)
4081 width_mm = rec.width * mm_width_per_pixel + 0.5;
4082 if (height_mm < 0)
4083 height_mm = rec.height * mm_height_per_pixel + 0.5;
4e3f9230 4084
7583e2a0 4085#if GTK_CHECK_VERSION (3, 4, 0)
6799bb26 4086 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4e3f9230
YM
4087#else
4088 /* Emulate the behavior of GTK+ 3.4. */
4089 {
4090 XRectangle workarea_r;
4091
4e3f9230
YM
4092 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4093 {
4e3f9230
YM
4094 work.x = workarea_r.x;
4095 work.y = workarea_r.y;
4096 work.width = workarea_r.width;
4097 work.height = workarea_r.height;
6799bb26
JD
4098 if (! gdk_rectangle_intersect (&rec, &work, &work))
4099 work = rec;
4100 }
4101 else
4102 work = rec;
4e3f9230
YM
4103 }
4104#endif
4e3f9230 4105
6799bb26
JD
4106
4107 mi->geom.x = rec.x;
4108 mi->geom.y = rec.y;
4109 mi->geom.width = rec.width;
4110 mi->geom.height = rec.height;
4111 mi->work.x = work.x;
4112 mi->work.y = work.y;
4113 mi->work.width = work.width;
4114 mi->work.height = work.height;
4115 mi->mm_width = width_mm;
4116 mi->mm_height = height_mm;
4117
7583e2a0 4118#if GTK_CHECK_VERSION (2, 14, 0)
6799bb26 4119 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4e3f9230 4120#endif
4e3f9230
YM
4121 }
4122
6799bb26
JD
4123 attributes_list = make_monitor_attribute_list (monitors,
4124 n_monitors,
4125 primary_monitor,
4126 monitor_frames,
4127 source);
4e3f9230
YM
4128 unblock_input ();
4129#else /* not USE_GTK */
4130
4131 block_input ();
4132 attributes_list = x_get_monitor_attributes (dpyinfo);
4133 unblock_input ();
4134
4135#endif /* not USE_GTK */
4136
4137 return attributes_list;
4138}
4139
a6ad00c0
GM
4140/************************************************************************
4141 X Displays
4142 ************************************************************************/
4143
01f1ba30 4144\f
a6ad00c0
GM
4145/* Mapping visual names to visuals. */
4146
4147static struct visual_class
4148{
675e2c69 4149 const char *name;
a6ad00c0
GM
4150 int class;
4151}
4152visual_classes[] =
4153{
4154 {"StaticGray", StaticGray},
4155 {"GrayScale", GrayScale},
4156 {"StaticColor", StaticColor},
4157 {"PseudoColor", PseudoColor},
4158 {"TrueColor", TrueColor},
4159 {"DirectColor", DirectColor},
9908a324 4160 {NULL, 0}
a6ad00c0
GM
4161};
4162
4163
404daac1 4164#ifndef HAVE_XSCREENNUMBEROFSCREEN
a6ad00c0
GM
4165
4166/* Value is the screen number of screen SCR. This is a substitute for
4167 the X function with the same name when that doesn't exist. */
4168
404daac1
RS
4169int
4170XScreenNumberOfScreen (scr)
4171 register Screen *scr;
4172{
a6ad00c0
GM
4173 Display *dpy = scr->display;
4174 int i;
3df34fdb 4175
a6ad00c0 4176 for (i = 0; i < dpy->nscreens; ++i)
fbd5ceb2 4177 if (scr == dpy->screens + i)
a6ad00c0 4178 break;
404daac1 4179
a6ad00c0 4180 return i;
404daac1 4181}
a6ad00c0 4182
404daac1
RS
4183#endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4184
01f1ba30 4185
a6ad00c0
GM
4186/* Select the visual that should be used on display DPYINFO. Set
4187 members of DPYINFO appropriately. Called from x_term_init. */
fe24a618 4188
a6ad00c0 4189void
971de7fb 4190select_visual (struct x_display_info *dpyinfo)
a6ad00c0
GM
4191{
4192 Display *dpy = dpyinfo->display;
4193 Screen *screen = dpyinfo->screen;
4194 Lisp_Object value;
fe24a618 4195
a6ad00c0
GM
4196 /* See if a visual is specified. */
4197 value = display_x_get_resource (dpyinfo,
4198 build_string ("visualClass"),
4199 build_string ("VisualClass"),
4200 Qnil, Qnil);
4201 if (STRINGP (value))
4202 {
4203 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4204 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4205 depth, a decimal number. NAME is compared with case ignored. */
38182d90 4206 char *s = alloca (SBYTES (value) + 1);
a6ad00c0
GM
4207 char *dash;
4208 int i, class = -1;
4209 XVisualInfo vinfo;
4210
42a5b22f 4211 strcpy (s, SSDATA (value));
8966b757 4212 dash = strchr (s, '-');
a6ad00c0
GM
4213 if (dash)
4214 {
4215 dpyinfo->n_planes = atoi (dash + 1);
4216 *dash = '\0';
4217 }
4218 else
4219 /* We won't find a matching visual with depth 0, so that
4220 an error will be printed below. */
4221 dpyinfo->n_planes = 0;
f0614854 4222
a6ad00c0
GM
4223 /* Determine the visual class. */
4224 for (i = 0; visual_classes[i].name; ++i)
05131107 4225 if (xstrcasecmp (s, visual_classes[i].name) == 0)
a6ad00c0
GM
4226 {
4227 class = visual_classes[i].class;
4228 break;
4229 }
01f1ba30 4230
a6ad00c0
GM
4231 /* Look up a matching visual for the specified class. */
4232 if (class == -1
4233 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4234 dpyinfo->n_planes, class, &vinfo))
d5db4077 4235 fatal ("Invalid visual specification `%s'", SDATA (value));
177c0ea7 4236
a6ad00c0
GM
4237 dpyinfo->visual = vinfo.visual;
4238 }
01f1ba30
JB
4239 else
4240 {
a6ad00c0
GM
4241 int n_visuals;
4242 XVisualInfo *vinfo, vinfo_template;
177c0ea7 4243
a6ad00c0
GM
4244 dpyinfo->visual = DefaultVisualOfScreen (screen);
4245
a6ad00c0 4246 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
a6ad00c0
GM
4247 vinfo_template.screen = XScreenNumberOfScreen (screen);
4248 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4249 &vinfo_template, &n_visuals);
83725342 4250 if (n_visuals <= 0)
a6ad00c0
GM
4251 fatal ("Can't get proper X visual info");
4252
94ac875b 4253 dpyinfo->n_planes = vinfo->depth;
98c6f1e3 4254 XFree (vinfo);
a6ad00c0 4255 }
01f1ba30 4256}
01f1ba30 4257
a6ad00c0 4258
b9dc4443
RS
4259/* Return the X display structure for the display named NAME.
4260 Open a new connection if necessary. */
4261
e4cebfca 4262static struct x_display_info *
971de7fb 4263x_display_info_for_name (Lisp_Object name)
b9dc4443
RS
4264{
4265 struct x_display_info *dpyinfo;
4266
b7826503 4267 CHECK_STRING (name);
b9dc4443 4268
d141d701
DA
4269 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4270 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4271 return dpyinfo;
b9dc4443 4272
b7975ee4
KH
4273 /* Use this general default value to start with. */
4274 Vx_resource_name = Vinvocation_name;
4275
b9dc4443
RS
4276 validate_x_resource_name ();
4277
7d652d97 4278 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
b9dc4443 4279
08a90d6a 4280 if (dpyinfo == 0)
d5db4077 4281 error ("Cannot connect to X server %s", SDATA (name));
08a90d6a 4282
b9dc4443
RS
4283 XSETFASTINT (Vwindow_system_version, 11);
4284
4285 return dpyinfo;
4286}
4287
a6ad00c0 4288
01f1ba30 4289DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
c061c855 4290 1, 3, 0,
4f4f2973 4291 doc: /* Open a connection to a display server.
c061c855
GM
4292DISPLAY is the name of the display to connect to.
4293Optional second arg XRM-STRING is a string of resources in xrdb format.
4294If the optional third arg MUST-SUCCEED is non-nil,
4f4f2973
GM
4295terminate Emacs if we can't open the connection.
4296\(In the Nextstep version, the last two arguments are currently ignored.) */)
5842a27b 4297 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
01f1ba30 4298{
5f742c1b 4299 char *xrm_option;
b9dc4443 4300 struct x_display_info *dpyinfo;
01f1ba30 4301
b7826503 4302 CHECK_STRING (display);
d387c960 4303 if (! NILP (xrm_string))
b7826503 4304 CHECK_STRING (xrm_string);
01f1ba30 4305
7d652d97 4306 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
d387c960
JB
4307
4308 validate_x_resource_name ();
4309
e1b1bee8 4310 /* This is what opens the connection and sets x_current_display.
b9dc4443
RS
4311 This also initializes many symbols, such as those used for input. */
4312 dpyinfo = x_term_init (display, xrm_option,
51b59d79 4313 SSDATA (Vx_resource_name));
f1c16f36 4314
08a90d6a
RS
4315 if (dpyinfo == 0)
4316 {
4317 if (!NILP (must_succeed))
10ffbc14
GM
4318 fatal ("Cannot connect to X server %s.\n\
4319Check the DISPLAY environment variable or use `-d'.\n\
842a9389
JB
4320Also use the `xauth' program to verify that you have the proper\n\
4321authorization information needed to connect the X server.\n\
bf770132 4322An insecure way to solve the problem may be to use `xhost'.\n",
d5db4077 4323 SDATA (display));
08a90d6a 4324 else
d5db4077 4325 error ("Cannot connect to X server %s", SDATA (display));
08a90d6a
RS
4326 }
4327
b9dc4443 4328 XSETFASTINT (Vwindow_system_version, 11);
01f1ba30
JB
4329 return Qnil;
4330}
4331
08a90d6a
RS
4332DEFUN ("x-close-connection", Fx_close_connection,
4333 Sx_close_connection, 1, 1, 0,
00d385b0 4334 doc: /* Close the connection to TERMINAL's X server.
708e05dc 4335For TERMINAL, specify a terminal object, a frame or a display name (a
00d385b0
KL
4336string). If TERMINAL is nil, that stands for the selected frame's
4337terminal. */)
5842a27b 4338 (Lisp_Object terminal)
01f1ba30 4339{
00d385b0 4340 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3457bc6e 4341
08a90d6a
RS
4342 if (dpyinfo->reference_count > 0)
4343 error ("Display still has frames on it");
01f1ba30 4344
6ed8eeff 4345 x_delete_terminal (dpyinfo->terminal);
3457bc6e 4346
01f1ba30
JB
4347 return Qnil;
4348}
4349
08a90d6a 4350DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7ee72033 4351 doc: /* Return the list of display names that Emacs has connections to. */)
5842a27b 4352 (void)
08a90d6a 4353{
d141d701
DA
4354 Lisp_Object result = Qnil;
4355 struct x_display_info *xdi;
08a90d6a 4356
d141d701
DA
4357 for (xdi = x_display_list; xdi; xdi = xdi->next)
4358 result = Fcons (XCAR (xdi->name_list_element), result);
08a90d6a
RS
4359
4360 return result;
4361}
4362
4363DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7ee72033 4364 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
7c051dd8
GM
4365This function only has an effect on X Windows. With MS Windows, it is
4366defined but does nothing.
4367
c061c855
GM
4368If ON is nil, allow buffering of requests.
4369Turning on synchronization prohibits the Xlib routines from buffering
4370requests and seriously degrades performance, but makes debugging much
4371easier.
00d385b0 4372The optional second argument TERMINAL specifies which display to act on.
708e05dc 4373TERMINAL should be a terminal object, a frame or a display name (a string).
00d385b0 4374If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
5842a27b 4375 (Lisp_Object on, Lisp_Object terminal)
01f1ba30 4376{
00d385b0 4377 struct x_display_info *dpyinfo = check_x_display_info (terminal);
11ae94fe 4378
b9dc4443 4379 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
01f1ba30
JB
4380
4381 return Qnil;
4382}
4383
b9dc4443 4384/* Wait for responses to all X commands issued so far for frame F. */
6b7b1820
RS
4385
4386void
a10c8269 4387x_sync (struct frame *f)
6b7b1820 4388{
4d7e6e51 4389 block_input ();
b9dc4443 4390 XSync (FRAME_X_DISPLAY (f), False);
4d7e6e51 4391 unblock_input ();
6b7b1820 4392}
333b20bb 4393
01f1ba30 4394\f
333b20bb 4395/***********************************************************************
6b61353c 4396 Window properties
333b20bb 4397 ***********************************************************************/
f1c16f36 4398
6b61353c
KH
4399DEFUN ("x-change-window-property", Fx_change_window_property,
4400 Sx_change_window_property, 2, 6, 0,
4401 doc: /* Change window property PROP to VALUE on the X window of FRAME.
7c051dd8
GM
4402PROP must be a string. VALUE may be a string or a list of conses,
4403numbers and/or strings. If an element in the list is a string, it is
4404converted to an atom and the value of the atom is used. If an element
4405is a cons, it is converted to a 32 bit number where the car is the 16
4406top bits and the cdr is the lower 16 bits.
4407
6b61353c
KH
4408FRAME nil or omitted means use the selected frame.
4409If TYPE is given and non-nil, it is the name of the type of VALUE.
4410If TYPE is not given or nil, the type is STRING.
4411FORMAT gives the size in bits of each element if VALUE is a list.
4412It must be one of 8, 16 or 32.
4413If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
fca8d6b6 4414If OUTER-P is non-nil, the property is changed for the outer X window of
7c051dd8 4415FRAME. Default is to change on the edit X window. */)
fca8d6b6
AS
4416 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4417 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
6b61353c 4418{
7452b7bd 4419 struct frame *f = decode_window_system_frame (frame);
6b61353c
KH
4420 Atom prop_atom;
4421 Atom target_type = XA_STRING;
4422 int element_format = 8;
4423 unsigned char *data;
4424 int nelements;
4425 Window w;
333b20bb 4426
6b61353c 4427 CHECK_STRING (prop);
333b20bb 4428
6b61353c
KH
4429 if (! NILP (format))
4430 {
4431 CHECK_NUMBER (format);
333b20bb 4432
d311d28c
PE
4433 if (XINT (format) != 8 && XINT (format) != 16
4434 && XINT (format) != 32)
6b61353c 4435 error ("FORMAT must be one of 8, 16 or 32");
d311d28c 4436 element_format = XINT (format);
6b61353c 4437 }
333b20bb 4438
6b61353c
KH
4439 if (CONSP (value))
4440 {
c678c835
PE
4441 ptrdiff_t elsize;
4442
6b61353c
KH
4443 nelements = x_check_property_data (value);
4444 if (nelements == -1)
4445 error ("Bad data in VALUE, must be number, string or cons");
333b20bb 4446
c678c835
PE
4447 /* The man page for XChangeProperty:
4448 "If the specified format is 32, the property data must be a
4449 long array."
4450 This applies even if long is more than 32 bits. The X library
4451 converts to 32 bits before sending to the X server. */
4452 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
0065d054 4453 data = xnmalloc (nelements, elsize);
333b20bb 4454
6b61353c
KH
4455 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4456 }
4457 else
4458 {
4459 CHECK_STRING (value);
4460 data = SDATA (value);
c678c835
PE
4461 if (INT_MAX < SBYTES (value))
4462 error ("VALUE too long");
4463 nelements = SBYTES (value);
6b61353c 4464 }
333b20bb 4465
4d7e6e51 4466 block_input ();
42a5b22f 4467 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
6b61353c
KH
4468 if (! NILP (type))
4469 {
4470 CHECK_STRING (type);
42a5b22f 4471 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
6b61353c 4472 }
333b20bb 4473
6b61353c
KH
4474 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4475 else w = FRAME_X_WINDOW (f);
5b3f8550 4476
6b61353c
KH
4477 XChangeProperty (FRAME_X_DISPLAY (f), w,
4478 prop_atom, target_type, element_format, PropModeReplace,
4479 data, nelements);
333b20bb 4480
6b61353c 4481 if (CONSP (value)) xfree (data);
333b20bb 4482
6b61353c
KH
4483 /* Make sure the property is set when we return. */
4484 XFlush (FRAME_X_DISPLAY (f));
4d7e6e51 4485 unblock_input ();
333b20bb 4486
6b61353c 4487 return value;
333b20bb
GM
4488}
4489
4490
6b61353c
KH
4491DEFUN ("x-delete-window-property", Fx_delete_window_property,
4492 Sx_delete_window_property, 1, 2, 0,
4493 doc: /* Remove window property PROP from X window of FRAME.
4494FRAME nil or omitted means use the selected frame. Value is PROP. */)
5842a27b 4495 (Lisp_Object prop, Lisp_Object frame)
333b20bb 4496{
7452b7bd 4497 struct frame *f = decode_window_system_frame (frame);
6b61353c 4498 Atom prop_atom;
333b20bb 4499
6b61353c 4500 CHECK_STRING (prop);
4d7e6e51 4501 block_input ();
42a5b22f 4502 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
6b61353c 4503 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
333b20bb 4504
6b61353c
KH
4505 /* Make sure the property is removed when we return. */
4506 XFlush (FRAME_X_DISPLAY (f));
4d7e6e51 4507 unblock_input ();
333b20bb 4508
6b61353c
KH
4509 return prop;
4510}
333b20bb 4511
333b20bb 4512
6b61353c
KH
4513DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4514 1, 6, 0,
4515 doc: /* Value is the value of window property PROP on FRAME.
4516If FRAME is nil or omitted, use the selected frame.
7c051dd8 4517
7c051dd8
GM
4518On X Windows, the following optional arguments are also accepted:
4519If TYPE is nil or omitted, get the property as a string.
4520Otherwise TYPE is the name of the atom that denotes the type expected.
6b61353c
KH
4521If SOURCE is non-nil, get the property on that window instead of from
4522FRAME. The number 0 denotes the root window.
ddadbc0e
AS
4523If DELETE-P is non-nil, delete the property after retrieving it.
4524If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4525
4526On MS Windows, this function accepts but ignores those optional arguments.
6b61353c
KH
4527
4528Value is nil if FRAME hasn't a property with name PROP or if PROP has
7c051dd8 4529no value of TYPE (always string in the MS Windows case). */)
ddadbc0e
AS
4530 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4531 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
333b20bb 4532{
7452b7bd 4533 struct frame *f = decode_window_system_frame (frame);
6b61353c
KH
4534 Atom prop_atom;
4535 int rc;
4536 Lisp_Object prop_value = Qnil;
a3db4b26 4537 unsigned char *tmp_data = NULL;
6b61353c
KH
4538 Atom actual_type;
4539 Atom target_type = XA_STRING;
4540 int actual_format;
4541 unsigned long actual_size, bytes_remaining;
4542 Window target_window = FRAME_X_WINDOW (f);
4543 struct gcpro gcpro1;
177c0ea7 4544
6b61353c
KH
4545 GCPRO1 (prop_value);
4546 CHECK_STRING (prop);
1783ffa2 4547
6b61353c
KH
4548 if (! NILP (source))
4549 {
be44ca6c
PE
4550 CONS_TO_INTEGER (source, Window, target_window);
4551 if (! target_window)
aad3612f 4552 target_window = FRAME_DISPLAY_INFO (f)->root_window;
333b20bb
GM
4553 }
4554
4d7e6e51 4555 block_input ();
6b61353c
KH
4556 if (STRINGP (type))
4557 {
42a5b22f 4558 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
6b61353c
KH
4559 target_type = AnyPropertyType;
4560 else
42a5b22f 4561 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
6b61353c 4562 }
333b20bb 4563
42a5b22f 4564 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
6b61353c
KH
4565 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4566 prop_atom, 0, 0, False, target_type,
4567 &actual_type, &actual_format, &actual_size,
a3db4b26 4568 &bytes_remaining, &tmp_data);
6b61353c
KH
4569 if (rc == Success)
4570 {
4571 int size = bytes_remaining;
333b20bb 4572
6b61353c
KH
4573 XFree (tmp_data);
4574 tmp_data = NULL;
333b20bb 4575
6b61353c 4576 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
333b20bb 4577 prop_atom, 0, bytes_remaining,
6b61353c 4578 ! NILP (delete_p), target_type,
177c0ea7
JB
4579 &actual_type, &actual_format,
4580 &actual_size, &bytes_remaining,
a3db4b26 4581 &tmp_data);
4c8c7926 4582 if (rc == Success && tmp_data)
6b61353c 4583 {
fede04ef
JD
4584 /* The man page for XGetWindowProperty says:
4585 "If the returned format is 32, the returned data is represented
4586 as a long array and should be cast to that type to obtain the
4587 elements."
4588 This applies even if long is more than 32 bits, the X library
4589 converts from 32 bit elements received from the X server to long
72af86bd 4590 and passes the long array to us. Thus, for that case memcpy can not
fede04ef
JD
4591 be used. We convert to a 32 bit type here, because so much code
4592 assume on that.
4593
4594 The bytes and offsets passed to XGetWindowProperty refers to the
4595 property and those are indeed in 32 bit quantities if format is
4596 32. */
4597
908589fd 4598 if (BITS_PER_LONG > 32 && actual_format == 32)
fede04ef
JD
4599 {
4600 unsigned long i;
4601 int *idata = (int *) tmp_data;
4602 long *ldata = (long *) tmp_data;
4603
4604 for (i = 0; i < actual_size; ++i)
a3db4b26 4605 idata[i] = (int) ldata[i];
fede04ef
JD
4606 }
4607
6b61353c 4608 if (NILP (vector_ret_p))
5f742c1b 4609 prop_value = make_string ((char *) tmp_data, size);
6b61353c
KH
4610 else
4611 prop_value = x_property_data_to_lisp (f,
a3db4b26 4612 tmp_data,
6b61353c
KH
4613 actual_type,
4614 actual_format,
4615 actual_size);
4616 }
333b20bb 4617
6b61353c 4618 if (tmp_data) XFree (tmp_data);
333b20bb
GM
4619 }
4620
4d7e6e51 4621 unblock_input ();
6b61353c 4622 UNGCPRO;
333b20bb
GM
4623 return prop_value;
4624}
4625
4626
4627\f
4628/***********************************************************************
4629 Busy cursor
4630 ***********************************************************************/
4631
0af913d7
GM
4632/* Timer function of hourglass_atimer. TIMER is equal to
4633 hourglass_atimer.
4ae9a85e 4634
0af913d7
GM
4635 Display an hourglass pointer on all frames by mapping the frames'
4636 hourglass_window. Set the hourglass_p flag in the frames'
4637 output_data.x structure to indicate that an hourglass cursor is
4638 shown on the frames. */
4ae9a85e 4639
1885ab29 4640void
971de7fb 4641show_hourglass (struct atimer *timer)
4ae9a85e
GM
4642{
4643 /* The timer implementation will cancel this timer automatically
0af913d7 4644 after this function has run. Set hourglass_atimer to null
4ae9a85e 4645 so that we know the timer doesn't have to be canceled. */
0af913d7 4646 hourglass_atimer = NULL;
4ae9a85e 4647
0af913d7 4648 if (!hourglass_shown_p)
333b20bb
GM
4649 {
4650 Lisp_Object rest, frame;
177c0ea7 4651
4d7e6e51 4652 block_input ();
177c0ea7 4653
333b20bb 4654 FOR_EACH_FRAME (rest, frame)
5f7a1890
GM
4655 {
4656 struct frame *f = XFRAME (frame);
177c0ea7 4657
5f7a1890
GM
4658 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4659 {
4660 Display *dpy = FRAME_X_DISPLAY (f);
177c0ea7 4661
5f7a1890
GM
4662#ifdef USE_X_TOOLKIT
4663 if (f->output_data.x->widget)
4664#else
4665 if (FRAME_OUTER_WINDOW (f))
4666#endif
4667 {
0af913d7 4668 f->output_data.x->hourglass_p = 1;
177c0ea7 4669
0af913d7 4670 if (!f->output_data.x->hourglass_window)
5f7a1890
GM
4671 {
4672 unsigned long mask = CWCursor;
4673 XSetWindowAttributes attrs;
49b8b5dc
JD
4674#ifdef USE_GTK
4675 Window parent = FRAME_X_WINDOW (f);
4676#else
4677 Window parent = FRAME_OUTER_WINDOW (f);
4678#endif
0af913d7 4679 attrs.cursor = f->output_data.x->hourglass_cursor;
177c0ea7 4680
0af913d7 4681 f->output_data.x->hourglass_window
49b8b5dc 4682 = XCreateWindow (dpy, parent,
5f7a1890
GM
4683 0, 0, 32000, 32000, 0, 0,
4684 InputOnly,
4685 CopyFromParent,
4686 mask, &attrs);
4687 }
177c0ea7 4688
0af913d7 4689 XMapRaised (dpy, f->output_data.x->hourglass_window);
5f7a1890
GM
4690 XFlush (dpy);
4691 }
4692 }
4693 }
333b20bb 4694
0af913d7 4695 hourglass_shown_p = 1;
4d7e6e51 4696 unblock_input ();
4ae9a85e 4697 }
333b20bb
GM
4698}
4699
4700
0af913d7
GM
4701/* Hide the hourglass pointer on all frames, if it is currently
4702 shown. */
333b20bb 4703
1885ab29 4704void
971de7fb 4705hide_hourglass (void)
4ae9a85e 4706{
0af913d7 4707 if (hourglass_shown_p)
333b20bb 4708 {
4ae9a85e
GM
4709 Lisp_Object rest, frame;
4710
4d7e6e51 4711 block_input ();
4ae9a85e 4712 FOR_EACH_FRAME (rest, frame)
333b20bb 4713 {
4ae9a85e 4714 struct frame *f = XFRAME (frame);
177c0ea7 4715
4ae9a85e
GM
4716 if (FRAME_X_P (f)
4717 /* Watch out for newly created frames. */
0af913d7 4718 && f->output_data.x->hourglass_window)
4ae9a85e 4719 {
0af913d7
GM
4720 XUnmapWindow (FRAME_X_DISPLAY (f),
4721 f->output_data.x->hourglass_window);
4722 /* Sync here because XTread_socket looks at the
4723 hourglass_p flag that is reset to zero below. */
4ae9a85e 4724 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 4725 f->output_data.x->hourglass_p = 0;
4ae9a85e 4726 }
333b20bb 4727 }
333b20bb 4728
0af913d7 4729 hourglass_shown_p = 0;
4d7e6e51 4730 unblock_input ();
4ae9a85e 4731 }
333b20bb
GM
4732}
4733
4734
4735\f
4736/***********************************************************************
4737 Tool tips
4738 ***********************************************************************/
4739
f57e2426
J
4740static Lisp_Object x_create_tip_frame (struct x_display_info *,
4741 Lisp_Object, Lisp_Object);
4742static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4743 Lisp_Object, int, int, int *, int *);
177c0ea7 4744
44b5a125 4745/* The frame of a currently visible tooltip. */
333b20bb 4746
44b5a125 4747Lisp_Object tip_frame;
333b20bb
GM
4748
4749/* If non-nil, a timer started that hides the last tooltip when it
4750 fires. */
4751
e4cebfca 4752static Lisp_Object tip_timer;
333b20bb
GM
4753Window tip_window;
4754
06d62053
GM
4755/* If non-nil, a vector of 3 elements containing the last args
4756 with which x-show-tip was called. See there. */
4757
e4cebfca 4758static Lisp_Object last_show_tip_args;
06d62053 4759
eaf1eea9 4760
27e498e6 4761static void
971de7fb 4762unwind_create_tip_frame (Lisp_Object frame)
eaf1eea9 4763{
c844a81a
GM
4764 Lisp_Object deleted;
4765
4766 deleted = unwind_create_frame (frame);
4767 if (EQ (deleted, Qt))
4768 {
4769 tip_window = None;
4770 tip_frame = Qnil;
4771 }
eaf1eea9
GM
4772}
4773
4774
333b20bb 4775/* Create a frame for a tooltip on the display described by DPYINFO.
275841bf
GM
4776 PARMS is a list of frame parameters. TEXT is the string to
4777 display in the tip frame. Value is the frame.
eaf1eea9
GM
4778
4779 Note that functions called here, esp. x_default_parameter can
4780 signal errors, for instance when a specified color name is
4781 undefined. We have to make sure that we're in a consistent state
4782 when this happens. */
333b20bb
GM
4783
4784static Lisp_Object
aa1859f5
J
4785x_create_tip_frame (struct x_display_info *dpyinfo,
4786 Lisp_Object parms,
4787 Lisp_Object text)
333b20bb
GM
4788{
4789 struct frame *f;
06b0c8a0 4790 Lisp_Object frame;
333b20bb 4791 Lisp_Object name;
333b20bb 4792 int width, height;
d311d28c 4793 ptrdiff_t count = SPECPDL_INDEX ();
b6d7acec 4794 struct gcpro gcpro1, gcpro2, gcpro3;
06d62053 4795 int face_change_count_before = face_change_count;
275841bf
GM
4796 Lisp_Object buffer;
4797 struct buffer *old_buffer;
333b20bb 4798
89acb56d
SM
4799 if (!dpyinfo->terminal->name)
4800 error ("Terminal is not live, can't create new frames on it");
333b20bb 4801
47f3c6b4 4802 parms = Fcopy_alist (parms);
333b20bb
GM
4803
4804 /* Get the name of the frame to use for resource lookup. */
4805 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4806 if (!STRINGP (name)
4807 && !EQ (name, Qunbound)
4808 && !NILP (name))
4809 error ("Invalid frame name--not a string or nil");
333b20bb
GM
4810
4811 frame = Qnil;
4812 GCPRO3 (parms, name, frame);
44b5a125 4813 f = make_frame (1);
333b20bb 4814 XSETFRAME (frame, f);
275841bf
GM
4815
4816 buffer = Fget_buffer_create (build_string (" *tip*"));
08908aca
MR
4817 /* Use set_window_buffer instead of Fset_window_buffer (see
4818 discussion of bug#11984, bug#12025, bug#12026). */
4819 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
275841bf
GM
4820 old_buffer = current_buffer;
4821 set_buffer_internal_1 (XBUFFER (buffer));
39eb03f1 4822 bset_truncate_lines (current_buffer, Qnil);
5c2a995d
KH
4823 specbind (Qinhibit_read_only, Qt);
4824 specbind (Qinhibit_modification_hooks, Qt);
275841bf
GM
4825 Ferase_buffer ();
4826 Finsert (1, &text);
4827 set_buffer_internal_1 (old_buffer);
177c0ea7 4828
8a1a7743 4829 record_unwind_protect (unwind_create_tip_frame, frame);
333b20bb 4830
6ed8eeff 4831 f->terminal = dpyinfo->terminal;
daf01701 4832
eaf1eea9
GM
4833 /* By setting the output method, we're essentially saying that
4834 the frame is live, as per FRAME_LIVE_P. If we get a signal
4835 from this point on, x_destroy_window might screw up reference
4836 counts etc. */
333b20bb 4837 f->output_method = output_x_window;
38182d90 4838 f->output_data.x = xzalloc (sizeof *f->output_data.x);
333b20bb 4839 f->output_data.x->icon_bitmap = -1;
be786000 4840 FRAME_FONTSET (f) = -1;
61d461a8
GM
4841 f->output_data.x->scroll_bar_foreground_pixel = -1;
4842 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
4843#ifdef USE_TOOLKIT_SCROLL_BARS
4844 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4845 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4846#endif /* USE_TOOLKIT_SCROLL_BARS */
0085b9be
DA
4847 f->output_data.x->white_relief.pixel = -1;
4848 f->output_data.x->black_relief.pixel = -1;
4849
f00af5b1 4850 fset_icon_name (f, Qnil);
aad3612f
DA
4851 FRAME_DISPLAY_INFO (f) = dpyinfo;
4852 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
333b20bb
GM
4853 f->output_data.x->explicit_parent = 0;
4854
61d461a8
GM
4855 /* These colors will be set anyway later, but it's important
4856 to get the color reference counts right, so initialize them! */
4857 {
4858 Lisp_Object black;
dbf31225 4859 struct gcpro gcpro1;
177c0ea7 4860
9d3d44ba
KL
4861 /* Function x_decode_color can signal an error. Make
4862 sure to initialize color slots so that we won't try
4863 to free colors we haven't allocated. */
ce593f6e
KL
4864 FRAME_FOREGROUND_PIXEL (f) = -1;
4865 FRAME_BACKGROUND_PIXEL (f) = -1;
9d3d44ba
KL
4866 f->output_data.x->cursor_pixel = -1;
4867 f->output_data.x->cursor_foreground_pixel = -1;
4868 f->output_data.x->border_pixel = -1;
4869 f->output_data.x->mouse_pixel = -1;
4870
61d461a8 4871 black = build_string ("black");
dbf31225 4872 GCPRO1 (black);
ce593f6e 4873 FRAME_FOREGROUND_PIXEL (f)
61d461a8 4874 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
ce593f6e 4875 FRAME_BACKGROUND_PIXEL (f)
61d461a8
GM
4876 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4877 f->output_data.x->cursor_pixel
4878 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4879 f->output_data.x->cursor_foreground_pixel
4880 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4881 f->output_data.x->border_pixel
4882 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4883 f->output_data.x->mouse_pixel
4884 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
dbf31225 4885 UNGCPRO;
61d461a8
GM
4886 }
4887
333b20bb
GM
4888 /* Set the name; the functions to which we pass f expect the name to
4889 be set. */
4890 if (EQ (name, Qunbound) || NILP (name))
4891 {
f00af5b1 4892 fset_name (f, build_string (dpyinfo->x_id_name));
333b20bb
GM
4893 f->explicit_name = 0;
4894 }
4895 else
4896 {
f00af5b1 4897 fset_name (f, name);
333b20bb
GM
4898 f->explicit_name = 1;
4899 /* use the frame's title when getting resources for this frame. */
4900 specbind (Qx_resource_name, name);
4901 }
4902
bf970f2b 4903 register_font_driver (&xfont_driver, f);
02ed2ea8
KH
4904#ifdef HAVE_FREETYPE
4905#ifdef HAVE_XFT
b51238f5 4906 register_font_driver (&xftfont_driver, f);
02ed2ea8 4907#else /* not HAVE_XFT */
b51238f5 4908 register_font_driver (&ftxfont_driver, f);
02ed2ea8
KH
4909#endif /* not HAVE_XFT */
4910#endif /* HAVE_FREETYPE */
d43f6b74 4911
b51238f5
KH
4912 x_default_parameter (f, parms, Qfont_backend, Qnil,
4913 "fontBackend", "FontBackend", RES_TYPE_STRING);
02ed2ea8 4914
eaf1eea9
GM
4915 /* Extract the window parameters from the supplied values that are
4916 needed to determine window geometry. */
b51238f5 4917 x_default_font_parameter (f, parms);
333b20bb 4918
4bef8d26 4919 x_default_parameter (f, parms, Qborder_width, make_number (0),
333b20bb 4920 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
177c0ea7 4921
333b20bb
GM
4922 /* This defaults to 2 in order to match xterm. We recognize either
4923 internalBorderWidth or internalBorder (which is what xterm calls
4924 it). */
4925 if (NILP (Fassq (Qinternal_border_width, parms)))
4926 {
4927 Lisp_Object value;
4928
4929 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4930 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4931 if (! EQ (value, Qunbound))
4932 parms = Fcons (Fcons (Qinternal_border_width, value),
4933 parms);
4934 }
4935
4936 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4937 "internalBorderWidth", "internalBorderWidth",
4938 RES_TYPE_NUMBER);
880e6158
MR
4939 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
4940 NULL, NULL, RES_TYPE_NUMBER);
4941 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
4942 NULL, NULL, RES_TYPE_NUMBER);
333b20bb
GM
4943
4944 /* Also do the stuff which must be set before the window exists. */
4945 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4946 "foreground", "Foreground", RES_TYPE_STRING);
4947 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4948 "background", "Background", RES_TYPE_STRING);
4949 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4950 "pointerColor", "Foreground", RES_TYPE_STRING);
4951 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4952 "cursorColor", "Foreground", RES_TYPE_STRING);
4953 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4954 "borderColor", "BorderColor", RES_TYPE_STRING);
4955
e509cfa6 4956#ifdef GLYPH_DEBUG
c9e7db78
JD
4957 image_cache_refcount =
4958 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4959 dpyinfo_refcount = dpyinfo->reference_count;
4960#endif /* GLYPH_DEBUG */
4961
333b20bb
GM
4962 /* Init faces before x_default_parameter is called for scroll-bar
4963 parameters because that function calls x_set_scroll_bar_width,
4964 which calls change_frame_size, which calls Fset_window_buffer,
4965 which runs hooks, which call Fvertical_motion. At the end, we
4966 end up in init_iterator with a null face cache, which should not
4967 happen. */
4968 init_frame_faces (f);
177c0ea7 4969
aad3612f 4970 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
333b20bb 4971
0ce7538d 4972 x_figure_window_size (f, parms, 0);
333b20bb 4973
333b20bb
GM
4974 {
4975 XSetWindowAttributes attrs;
4976 unsigned long mask;
aad3612f 4977 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
177c0ea7 4978
4d7e6e51 4979 block_input ();
c51d2b5e
GM
4980 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4981 if (DoesSaveUnders (dpyinfo->screen))
4982 mask |= CWSaveUnder;
177c0ea7 4983
9b2956e2
GM
4984 /* Window managers look at the override-redirect flag to determine
4985 whether or net to give windows a decoration (Xlib spec, chapter
333b20bb
GM
4986 3.2.8). */
4987 attrs.override_redirect = True;
4988 attrs.save_under = True;
4989 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4990 /* Arrange for getting MapNotify and UnmapNotify events. */
4991 attrs.event_mask = StructureNotifyMask;
4992 tip_window
4993 = FRAME_X_WINDOW (f)
4994 = XCreateWindow (FRAME_X_DISPLAY (f),
aad3612f 4995 FRAME_DISPLAY_INFO (f)->root_window,
333b20bb
GM
4996 /* x, y, width, height */
4997 0, 0, 1, 1,
4998 /* Border. */
a979dde1 4999 f->border_width,
333b20bb
GM
5000 CopyFromParent, InputOutput, CopyFromParent,
5001 mask, &attrs);
36acb2a7 5002 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
aad3612f 5003 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
36acb2a7
JD
5004 XA_ATOM, 32, PropModeReplace,
5005 (unsigned char *)&type, 1);
4d7e6e51 5006 unblock_input ();
333b20bb
GM
5007 }
5008
5009 x_make_gc (f);
5010
333b20bb
GM
5011 x_default_parameter (f, parms, Qauto_raise, Qnil,
5012 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5013 x_default_parameter (f, parms, Qauto_lower, Qnil,
5014 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5015 x_default_parameter (f, parms, Qcursor_type, Qbox,
5016 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5017
be786000 5018 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
333b20bb 5019 Change will not be effected unless different from the current
be786000
KS
5020 FRAME_LINES (f). */
5021 width = FRAME_COLS (f);
5022 height = FRAME_LINES (f);
5023 SET_FRAME_COLS (f, 0);
5024 FRAME_LINES (f) = 0;
880e6158 5025 change_frame_size (f, width, height, 1, 0, 0, 0);
177c0ea7 5026
cd1d850f 5027 /* Add `tooltip' frame parameter's default value. */
54ee7410 5028 if (NILP (Fframe_parameter (frame, Qtooltip)))
6c6f1994 5029 Fmodify_frame_parameters (frame, list1 (Fcons (Qtooltip, Qt)));
177c0ea7 5030
cedb4d8b
GM
5031 /* FIXME - can this be done in a similar way to normal frames?
5032 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5033
35fdaa62
GM
5034 /* Set the `display-type' frame parameter before setting up faces. */
5035 {
5036 Lisp_Object disptype;
5037
aad3612f 5038 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
35fdaa62 5039 disptype = intern ("mono");
aad3612f
DA
5040 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5041 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
35fdaa62
GM
5042 disptype = intern ("grayscale");
5043 else
5044 disptype = intern ("color");
5045
5046 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
6c6f1994 5047 Fmodify_frame_parameters (frame, list1 (Fcons (Qdisplay_type, disptype)));
35fdaa62
GM
5048 }
5049
035d5114 5050 /* Set up faces after all frame parameters are known. This call
6801a572
GM
5051 also merges in face attributes specified for new frames.
5052
5053 Frame parameters may be changed if .Xdefaults contains
5054 specifications for the default font. For example, if there is an
5055 `Emacs.default.attributeBackground: pink', the `background-color'
5056 attribute of the frame get's set, which let's the internal border
5057 of the tooltip frame appear in pink. Prevent this. */
5058 {
5059 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5060
5061 /* Set tip_frame here, so that */
5062 tip_frame = frame;
45cebfd9 5063 call2 (Qface_set_after_frame_default, frame, Qnil);
177c0ea7 5064
6801a572 5065 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
6c6f1994 5066 Fmodify_frame_parameters (frame, list1 (Fcons (Qbackground_color, bg)));
6801a572 5067 }
177c0ea7 5068
333b20bb
GM
5069 f->no_split = 1;
5070
5071 UNGCPRO;
5072
c9e7db78
JD
5073 /* Now that the frame will be official, it counts as a reference to
5074 its display and terminal. */
aad3612f 5075 FRAME_DISPLAY_INFO (f)->reference_count++;
c9e7db78
JD
5076 f->terminal->reference_count++;
5077
333b20bb
GM
5078 /* It is now ok to make the frame official even if we get an error
5079 below. And the frame needs to be on Vframe_list or making it
5080 visible won't work. */
5081 Vframe_list = Fcons (frame, Vframe_list);
5082
333b20bb 5083
06d62053
GM
5084 /* Setting attributes of faces of the tooltip frame from resources
5085 and similar will increment face_change_count, which leads to the
5086 clearing of all current matrices. Since this isn't necessary
5087 here, avoid it by resetting face_change_count to the value it
5088 had before we created the tip frame. */
5089 face_change_count = face_change_count_before;
5090
eaf1eea9 5091 /* Discard the unwind_protect. */
333b20bb
GM
5092 return unbind_to (count, frame);
5093}
5094
5095
06d62053
GM
5096/* Compute where to display tip frame F. PARMS is the list of frame
5097 parameters for F. DX and DY are specified offsets from the current
ab452f99
GM
5098 location of the mouse. WIDTH and HEIGHT are the width and height
5099 of the tooltip. Return coordinates relative to the root window of
5100 the display in *ROOT_X, and *ROOT_Y. */
06d62053
GM
5101
5102static void
971de7fb 5103compute_tip_xy (struct frame *f, Lisp_Object parms, Lisp_Object dx, Lisp_Object dy, int width, int height, int *root_x, int *root_y)
06d62053
GM
5104{
5105 Lisp_Object left, top;
5106 int win_x, win_y;
5107 Window root, child;
5108 unsigned pmask;
177c0ea7 5109
06d62053
GM
5110 /* User-specified position? */
5111 left = Fcdr (Fassq (Qleft, parms));
5112 top = Fcdr (Fassq (Qtop, parms));
177c0ea7 5113
06d62053
GM
5114 /* Move the tooltip window where the mouse pointer is. Resize and
5115 show it. */
570d22b0 5116 if (!INTEGERP (left) || !INTEGERP (top))
ab452f99 5117 {
4d7e6e51 5118 block_input ();
aad3612f 5119 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
ab452f99 5120 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
4d7e6e51 5121 unblock_input ();
ab452f99 5122 }
06d62053 5123
06d62053
GM
5124 if (INTEGERP (top))
5125 *root_y = XINT (top);
bf63eb69
JD
5126 else if (*root_y + XINT (dy) <= 0)
5127 *root_y = 0; /* Can happen for negative dy */
abe9d29c 5128 else if (*root_y + XINT (dy) + height
aad3612f 5129 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
7e8410d1 5130 /* It fits below the pointer */
abe9d29c 5131 *root_y += XINT (dy);
7e8410d1
JD
5132 else if (height + XINT (dy) <= *root_y)
5133 /* It fits above the pointer. */
5134 *root_y -= height + XINT (dy);
5135 else
5136 /* Put it on the top. */
5137 *root_y = 0;
ab452f99
GM
5138
5139 if (INTEGERP (left))
5140 *root_x = XINT (left);
bf63eb69
JD
5141 else if (*root_x + XINT (dx) <= 0)
5142 *root_x = 0; /* Can happen for negative dx */
abe9d29c 5143 else if (*root_x + XINT (dx) + width
aad3612f 5144 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
d682d3df
RS
5145 /* It fits to the right of the pointer. */
5146 *root_x += XINT (dx);
5147 else if (width + XINT (dx) <= *root_x)
5148 /* It fits to the left of the pointer. */
ab452f99
GM
5149 *root_x -= width + XINT (dx);
5150 else
d682d3df
RS
5151 /* Put it left-justified on the screen--it ought to fit that way. */
5152 *root_x = 0;
06d62053
GM
5153}
5154
5155
0634ce98 5156DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7ee72033 5157 doc: /* Show STRING in a "tooltip" window on frame FRAME.
c061c855
GM
5158A tooltip window is a small X window displaying a string.
5159
87c891c7
EZ
5160This is an internal function; Lisp code should call `tooltip-show'.
5161
c061c855
GM
5162FRAME nil or omitted means use the selected frame.
5163
5164PARMS is an optional list of frame parameters which can be used to
5165change the tooltip's appearance.
5166
5167Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5168means use the default timeout of 5 seconds.
5169
c869cc37 5170If the list of frame parameters PARMS contains a `left' parameters,
c061c855
GM
5171the tooltip is displayed at that x-position. Otherwise it is
5172displayed at the mouse position, with offset DX added (default is 5 if
5173DX isn't specified). Likewise for the y-position; if a `top' frame
5174parameter is specified, it determines the y-position of the tooltip
5175window, otherwise it is displayed at the mouse position, with offset
5176DY added (default is -10).
5177
5178A tooltip's maximum size is specified by `x-max-tooltip-size'.
7ee72033 5179Text larger than the specified size is clipped. */)
5842a27b 5180 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
333b20bb
GM
5181{
5182 struct frame *f;
5183 struct window *w;
06d62053 5184 int root_x, root_y;
333b20bb
GM
5185 struct buffer *old_buffer;
5186 struct text_pos pos;
a971c0a7 5187 int i, width, height, seen_reversed_p;
393f2d14 5188 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
333b20bb 5189 int old_windows_or_buffers_changed = windows_or_buffers_changed;
d311d28c 5190 ptrdiff_t count = SPECPDL_INDEX ();
177c0ea7 5191
333b20bb
GM
5192 specbind (Qinhibit_redisplay, Qt);
5193
393f2d14 5194 GCPRO4 (string, parms, frame, timeout);
333b20bb 5195
b7826503 5196 CHECK_STRING (string);
2d7117fe
JD
5197 if (SCHARS (string) == 0)
5198 string = make_unibyte_string (" ", 1);
5199
7452b7bd 5200 f = decode_window_system_frame (frame);
333b20bb
GM
5201 if (NILP (timeout))
5202 timeout = make_number (5);
5203 else
b7826503 5204 CHECK_NATNUM (timeout);
177c0ea7 5205
0634ce98
GM
5206 if (NILP (dx))
5207 dx = make_number (5);
5208 else
b7826503 5209 CHECK_NUMBER (dx);
177c0ea7 5210
0634ce98 5211 if (NILP (dy))
12c67a7f 5212 dy = make_number (-10);
0634ce98 5213 else
b7826503 5214 CHECK_NUMBER (dy);
333b20bb 5215
aa1859f5
J
5216#ifdef USE_GTK
5217 if (x_gtk_use_system_tooltips)
5218 {
18e27ea8 5219 bool ok;
aa1859f5
J
5220
5221 /* Hide a previous tip, if any. */
5222 Fx_hide_tip ();
5223
4d7e6e51 5224 block_input ();
18e27ea8
PE
5225 ok = xg_prepare_tooltip (f, string, &width, &height);
5226 if (ok)
aa1859f5
J
5227 {
5228 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5229 xg_show_tooltip (f, root_x, root_y);
5230 /* This is used in Fx_hide_tip. */
5231 XSETFRAME (tip_frame, f);
5232 }
4d7e6e51 5233 unblock_input ();
aa1859f5
J
5234 if (ok) goto start_timer;
5235 }
5236#endif /* USE_GTK */
5237
06d62053
GM
5238 if (NILP (last_show_tip_args))
5239 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5240
5241 if (!NILP (tip_frame))
5242 {
5243 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5244 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5245 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5246
5247 if (EQ (frame, last_frame)
5248 && !NILP (Fequal (last_string, string))
5249 && !NILP (Fequal (last_parms, parms)))
5250 {
58d2d479 5251 struct frame *tip_f = XFRAME (tip_frame);
177c0ea7 5252
06d62053
GM
5253 /* Only DX and DY have changed. */
5254 if (!NILP (tip_timer))
ae782866
GM
5255 {
5256 Lisp_Object timer = tip_timer;
5257 tip_timer = Qnil;
5258 call1 (Qcancel_timer, timer);
5259 }
06d62053 5260
4d7e6e51 5261 block_input ();
58d2d479
PE
5262 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5263 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5264 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
ab452f99 5265 root_x, root_y);
4d7e6e51 5266 unblock_input ();
06d62053
GM
5267 goto start_timer;
5268 }
5269 }
5270
333b20bb
GM
5271 /* Hide a previous tip, if any. */
5272 Fx_hide_tip ();
5273
06d62053
GM
5274 ASET (last_show_tip_args, 0, string);
5275 ASET (last_show_tip_args, 1, frame);
5276 ASET (last_show_tip_args, 2, parms);
5277
333b20bb
GM
5278 /* Add default values to frame parameters. */
5279 if (NILP (Fassq (Qname, parms)))
5280 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5281 if (NILP (Fassq (Qinternal_border_width, parms)))
5282 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5283 if (NILP (Fassq (Qborder_width, parms)))
5284 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
880e6158
MR
5285 if (NILP (Fassq (Qbottom_divider_width, parms)))
5286 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5287 if (NILP (Fassq (Qright_divider_width, parms)))
5288 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
333b20bb
GM
5289 if (NILP (Fassq (Qborder_color, parms)))
5290 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5291 if (NILP (Fassq (Qbackground_color, parms)))
5292 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5293 parms);
5294
5295 /* Create a frame for the tooltip, and record it in the global
5296 variable tip_frame. */
aad3612f 5297 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
44b5a125 5298 f = XFRAME (frame);
333b20bb 5299
d63931a2 5300 /* Set up the frame's root window. */
333b20bb 5301 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5f24fa51
DA
5302 w->left_col = 0;
5303 w->top_line = 0;
880e6158
MR
5304 w->pixel_left = 0;
5305 w->pixel_top = 0;
177c0ea7 5306
d63931a2 5307 if (CONSP (Vx_max_tooltip_size)
d311d28c
PE
5308 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5309 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
d63931a2 5310 {
5f24fa51
DA
5311 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5312 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
d63931a2
GM
5313 }
5314 else
5315 {
5f24fa51
DA
5316 w->total_cols = 80;
5317 w->total_lines = 40;
d63931a2 5318 }
177c0ea7 5319
880e6158
MR
5320 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5321 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5322
5f24fa51 5323 FRAME_TOTAL_COLS (f) = w->total_cols;
bd0443bb 5324 adjust_frame_glyphs (f);
333b20bb
GM
5325 w->pseudo_window_p = 1;
5326
5327 /* Display the tooltip text in a temporary buffer. */
333b20bb 5328 old_buffer = current_buffer;
e74aeda8 5329 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
39eb03f1 5330 bset_truncate_lines (current_buffer, Qnil);
5634ff85
YM
5331 clear_glyph_matrix (w->desired_matrix);
5332 clear_glyph_matrix (w->current_matrix);
5333 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
6eff5c3d 5334 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
333b20bb
GM
5335
5336 /* Compute width and height of the tooltip. */
a971c0a7 5337 width = height = seen_reversed_p = 0;
333b20bb
GM
5338 for (i = 0; i < w->desired_matrix->nrows; ++i)
5339 {
5340 struct glyph_row *row = &w->desired_matrix->rows[i];
5341 struct glyph *last;
5342 int row_width;
5343
5344 /* Stop at the first empty row at the end. */
138c0ae8 5345 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
333b20bb
GM
5346 break;
5347
d7bf0342
GM
5348 /* Let the row go over the full width of the frame. */
5349 row->full_width_p = 1;
333b20bb 5350
5b253e9c 5351 row_width = row->pixel_width;
333b20bb
GM
5352 if (row->used[TEXT_AREA])
5353 {
a971c0a7
EZ
5354 /* There's a glyph at the end of rows that is used to place
5355 the cursor there. Don't include the width of this glyph. */
5356 if (!row->reversed_p)
5357 {
5358 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5359 if (INTEGERP (last->object))
5360 row_width -= last->pixel_width;
5361 }
5362 else
5363 {
5364 /* There could be a stretch glyph at the beginning of R2L
5365 rows that is produced by extend_face_to_end_of_line.
5366 Don't count that glyph. */
5367 struct glyph *g = row->glyphs[TEXT_AREA];
5368
5369 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5370 {
5371 row_width -= g->pixel_width;
5372 seen_reversed_p = 1;
5373 }
5374 }
333b20bb 5375 }
177c0ea7 5376
333b20bb
GM
5377 height += row->height;
5378 width = max (width, row_width);
5379 }
5380
a971c0a7
EZ
5381 /* If we've seen partial-length R2L rows, we need to re-adjust the
5382 tool-tip frame width and redisplay it again, to avoid over-wide
5383 tips due to the stretch glyph that extends R2L lines to full
5384 width of the frame. */
5385 if (seen_reversed_p)
5386 {
5387 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5388 not in pixels. */
880e6158 5389 w->pixel_width = width;
a971c0a7 5390 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5f24fa51 5391 w->total_cols = width;
a971c0a7 5392 FRAME_TOTAL_COLS (f) = width;
880e6158 5393 SET_FRAME_WIDTH (f, width);
bd0443bb 5394 adjust_frame_glyphs (f);
a971c0a7
EZ
5395 clear_glyph_matrix (w->desired_matrix);
5396 clear_glyph_matrix (w->current_matrix);
5397 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5398 width = height = 0;
5399 /* Recompute width and height of the tooltip. */
5400 for (i = 0; i < w->desired_matrix->nrows; ++i)
5401 {
5402 struct glyph_row *row = &w->desired_matrix->rows[i];
5403 struct glyph *last;
5404 int row_width;
5405
138c0ae8 5406 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
a971c0a7
EZ
5407 break;
5408 row->full_width_p = 1;
5409 row_width = row->pixel_width;
5410 if (row->used[TEXT_AREA] && !row->reversed_p)
5411 {
5412 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5413 if (INTEGERP (last->object))
5414 row_width -= last->pixel_width;
5415 }
5416
5417 height += row->height;
5418 width = max (width, row_width);
5419 }
5420 }
5421
333b20bb
GM
5422 /* Add the frame's internal border to the width and height the X
5423 window should have. */
5424 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5425 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5426
5427 /* Move the tooltip window where the mouse pointer is. Resize and
5428 show it. */
ab452f99 5429 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
0634ce98 5430
4d7e6e51 5431 block_input ();
333b20bb 5432 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 5433 root_x, root_y, width, height);
333b20bb 5434 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4d7e6e51 5435 unblock_input ();
177c0ea7 5436
333b20bb
GM
5437 /* Draw into the window. */
5438 w->must_be_updated_p = 1;
5439 update_single_window (w, 1);
5440
5441 /* Restore original current buffer. */
5442 set_buffer_internal_1 (old_buffer);
5443 windows_or_buffers_changed = old_windows_or_buffers_changed;
5444
06d62053 5445 start_timer:
333b20bb
GM
5446 /* Let the tip disappear after timeout seconds. */
5447 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5448 intern ("x-hide-tip"));
a744a2ec
DL
5449
5450 UNGCPRO;
333b20bb
GM
5451 return unbind_to (count, Qnil);
5452}
5453
5454
a7ca3326 5455DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7ee72033
MB
5456 doc: /* Hide the current tooltip window, if there is any.
5457Value is t if tooltip was open, nil otherwise. */)
5842a27b 5458 (void)
333b20bb 5459{
d311d28c 5460 ptrdiff_t count;
c0006262
GM
5461 Lisp_Object deleted, frame, timer;
5462 struct gcpro gcpro1, gcpro2;
44b5a125
GM
5463
5464 /* Return quickly if nothing to do. */
c0006262 5465 if (NILP (tip_timer) && NILP (tip_frame))
44b5a125 5466 return Qnil;
177c0ea7 5467
c0006262
GM
5468 frame = tip_frame;
5469 timer = tip_timer;
5470 GCPRO2 (frame, timer);
5471 tip_frame = tip_timer = deleted = Qnil;
177c0ea7 5472
331379bf 5473 count = SPECPDL_INDEX ();
333b20bb 5474 specbind (Qinhibit_redisplay, Qt);
44b5a125 5475 specbind (Qinhibit_quit, Qt);
177c0ea7 5476
c0006262 5477 if (!NILP (timer))
ae782866 5478 call1 (Qcancel_timer, timer);
333b20bb 5479
aa1859f5 5480#ifdef USE_GTK
1fe72bf8
PE
5481 {
5482 /* When using system tooltip, tip_frame is the Emacs frame on which
5483 the tip is shown. */
5484 struct frame *f = XFRAME (frame);
5485 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5486 frame = Qnil;
5487 }
aa1859f5
J
5488#endif
5489
c0006262 5490 if (FRAMEP (frame))
333b20bb 5491 {
56f2de10 5492 delete_frame (frame, Qnil);
44b5a125 5493 deleted = Qt;
f6c44811
GM
5494
5495#ifdef USE_LUCID
5496 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5497 redisplay procedure is not called when a tip frame over menu
5498 items is unmapped. Redisplay the menu manually... */
5499 {
aa1859f5 5500 Widget w;
1fe72bf8 5501 struct frame *f = SELECTED_FRAME ();
aa1859f5 5502 w = f->output_data.x->menubar_widget;
9180dc8c 5503
aad3612f 5504 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
dbc64aa7 5505 && w != NULL)
f6c44811 5506 {
4d7e6e51 5507 block_input ();
f6c44811 5508 xlwmenu_redisplay (w);
4d7e6e51 5509 unblock_input ();
f6c44811
GM
5510 }
5511 }
5512#endif /* USE_LUCID */
333b20bb
GM
5513 }
5514
c0006262 5515 UNGCPRO;
44b5a125 5516 return unbind_to (count, deleted);
333b20bb
GM
5517}
5518
5519
5520\f
5521/***********************************************************************
5522 File selection dialog
5523 ***********************************************************************/
5524
90f2e16b
JD
5525DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5526 Sx_uses_old_gtk_dialog,
5527 0, 0, 0,
5528 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5842a27b 5529 (void)
90f2e16b
JD
5530{
5531#ifdef USE_GTK
90f2e16b
JD
5532 if (use_dialog_box
5533 && use_file_dialog
7452b7bd 5534 && window_system_available (SELECTED_FRAME ())
90f2e16b
JD
5535 && xg_uses_old_file_dialog ())
5536 return Qt;
5537#endif
5538 return Qnil;
5539}
5540
333b20bb 5541
90f2e16b 5542#ifdef USE_MOTIF
333b20bb
GM
5543/* Callback for "OK" and "Cancel" on file selection dialog. */
5544
5545static void
dee186b6 5546file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
333b20bb 5547{
7d652d97
PE
5548 int *result = client_data;
5549 XmAnyCallbackStruct *cb = call_data;
333b20bb
GM
5550 *result = cb->reason;
5551}
5552
5553
a779d213
GM
5554/* Callback for unmapping a file selection dialog. This is used to
5555 capture the case where a dialog is closed via a window manager's
5556 closer button, for example. Using a XmNdestroyCallback didn't work
5557 in this case. */
5558
5559static void
dee186b6 5560file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
a779d213 5561{
7d652d97 5562 int *result = client_data;
a779d213
GM
5563 *result = XmCR_CANCEL;
5564}
5565
27e498e6
PE
5566static void
5567clean_up_file_dialog (void *arg)
24ca7c5a 5568{
27e498e6 5569 Widget dialog = arg;
24ca7c5a
JD
5570
5571 /* Clean up. */
4d7e6e51 5572 block_input ();
24ca7c5a
JD
5573 XtUnmanageChild (dialog);
5574 XtDestroyWidget (dialog);
5575 x_menu_set_in_use (0);
4d7e6e51 5576 unblock_input ();
24ca7c5a
JD
5577}
5578
a779d213 5579
f9d64bb3 5580DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
7ee72033 5581 doc: /* Read file name, prompting with PROMPT in directory DIR.
f9d64bb3
JD
5582Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5583selection box, if specified. If MUSTMATCH is non-nil, the returned file
7c051dd8
GM
5584or directory must exist.
5585
d7e642cc 5586This function is only defined on NS, MS Windows, and X Windows with the
7c051dd8
GM
5587Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5588Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
a10c8269
DA
5589 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5590 Lisp_Object mustmatch, Lisp_Object only_dir_p)
333b20bb
GM
5591{
5592 int result;
0fe92f72 5593 struct frame *f = SELECTED_FRAME ();
333b20bb 5594 Lisp_Object file = Qnil;
a8f4bdc6 5595 Lisp_Object decoded_file;
f9d64bb3 5596 Widget dialog, text, help;
333b20bb
GM
5597 Arg al[10];
5598 int ac = 0;
333b20bb 5599 XmString dir_xmstring, pattern_xmstring;
d311d28c 5600 ptrdiff_t count = SPECPDL_INDEX ();
f9d64bb3 5601 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
333b20bb 5602
7452b7bd 5603 check_window_system (f);
6eeee4d6 5604
f9d64bb3 5605 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
24ca7c5a 5606
5ae53dcf 5607 if (popup_activated ())
24ca7c5a
JD
5608 error ("Trying to use a menu from within a menu-entry");
5609
b7826503
PJ
5610 CHECK_STRING (prompt);
5611 CHECK_STRING (dir);
333b20bb
GM
5612
5613 /* Prevent redisplay. */
5614 specbind (Qinhibit_redisplay, Qt);
5615
4d7e6e51 5616 block_input ();
333b20bb
GM
5617
5618 /* Create the dialog with PROMPT as title, using DIR as initial
5619 directory and using "*" as pattern. */
5620 dir = Fexpand_file_name (dir, Qnil);
c09bfb2f 5621 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
333b20bb 5622 pattern_xmstring = XmStringCreateLocalized ("*");
177c0ea7 5623
d5db4077 5624 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
333b20bb
GM
5625 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5626 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5627 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5628 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5629 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5630 "fsb", al, ac);
5631 XmStringFree (dir_xmstring);
5632 XmStringFree (pattern_xmstring);
5633
5634 /* Add callbacks for OK and Cancel. */
5635 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5636 (XtPointer) &result);
5637 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5638 (XtPointer) &result);
a779d213
GM
5639 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5640 (XtPointer) &result);
333b20bb 5641
f9d64bb3 5642 /* Remove the help button since we can't display help. */
333b20bb 5643 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
f9d64bb3 5644 XtUnmanageChild (help);
333b20bb 5645
177c0ea7 5646 /* Mark OK button as default. */
333b20bb
GM
5647 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5648 XmNshowAsDefault, True, NULL);
5649
5650 /* If MUSTMATCH is non-nil, disable the file entry field of the
5651 dialog, so that the user must select a file from the files list
5652 box. We can't remove it because we wouldn't have a way to get at
5653 the result file name, then. */
5654 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5655 if (!NILP (mustmatch))
5656 {
5657 Widget label;
5658 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5659 XtSetSensitive (text, False);
5660 XtSetSensitive (label, False);
5661 }
5662
5663 /* Manage the dialog, so that list boxes get filled. */
5664 XtManageChild (dialog);
5665
333b20bb
GM
5666 if (STRINGP (default_filename))
5667 {
5668 XmString default_xmstring;
f9d64bb3
JD
5669 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5670 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
333b20bb 5671
f9d64bb3
JD
5672 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5673 XmTextFieldReplace (wtext, 0, last_pos,
c09bfb2f 5674 (SSDATA (Ffile_name_nondirectory (default_filename))));
333b20bb 5675
f9d64bb3
JD
5676 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5677 must include the path for this to work. */
5678
c09bfb2f 5679 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
333b20bb 5680
f9d64bb3
JD
5681 if (XmListItemExists (list, default_xmstring))
5682 {
5683 int item_pos = XmListItemPos (list, default_xmstring);
5684 /* Select the item and scroll it into view. */
5685 XmListSelectPos (list, item_pos, True);
5686 XmListSetPos (list, item_pos);
5687 }
5688
5689 XmStringFree (default_xmstring);
333b20bb
GM
5690 }
5691
27e498e6 5692 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
24ca7c5a 5693
bf338245 5694 /* Process events until the user presses Cancel or OK. */
5ae53dcf 5695 x_menu_set_in_use (1);
03100098 5696 result = 0;
a779d213 5697 while (result == 0)
563b384d 5698 {
bf338245 5699 XEvent event;
24ca7c5a 5700 x_menu_wait_for_event (0);
bf338245 5701 XtAppNextEvent (Xt_app_con, &event);
9f6fcdc5
JD
5702 if (event.type == KeyPress
5703 && FRAME_X_DISPLAY (f) == event.xkey.display)
5704 {
5705 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5706
5707 /* Pop down on C-g. */
5708 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5709 XtUnmanageChild (dialog);
5710 }
5b3f8550 5711
24ca7c5a 5712 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
563b384d 5713 }
03100098 5714
333b20bb
GM
5715 /* Get the result. */
5716 if (result == XmCR_OK)
5717 {
91a3e27b 5718 XmString text_string;
333b20bb 5719 String data;
177c0ea7 5720
91a3e27b
PE
5721 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5722 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5723 XmStringFree (text_string);
333b20bb
GM
5724 file = build_string (data);
5725 XtFree (data);
5726 }
5727 else
5728 file = Qnil;
5729
4d7e6e51 5730 unblock_input ();
333b20bb
GM
5731 UNGCPRO;
5732
5733 /* Make "Cancel" equivalent to C-g. */
5734 if (NILP (file))
5735 Fsignal (Qquit, Qnil);
177c0ea7 5736
a8f4bdc6
JD
5737 decoded_file = DECODE_FILE (file);
5738
5739 return unbind_to (count, decoded_file);
333b20bb
GM
5740}
5741
5742#endif /* USE_MOTIF */
5743
488dd4c4
JD
5744#ifdef USE_GTK
5745
27e498e6
PE
5746static void
5747clean_up_dialog (void)
24ca7c5a
JD
5748{
5749 x_menu_set_in_use (0);
24ca7c5a
JD
5750}
5751
f9d64bb3
JD
5752DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5753 doc: /* Read file name, prompting with PROMPT in directory DIR.
5754Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5755selection box, if specified. If MUSTMATCH is non-nil, the returned file
7c051dd8
GM
5756or directory must exist.
5757
d7e642cc 5758This function is only defined on NS, MS Windows, and X Windows with the
7c051dd8
GM
5759Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5760Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5842a27b 5761 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
488dd4c4 5762{
a10c8269 5763 struct frame *f = SELECTED_FRAME ();
488dd4c4
JD
5764 char *fn;
5765 Lisp_Object file = Qnil;
a8f4bdc6 5766 Lisp_Object decoded_file;
d311d28c 5767 ptrdiff_t count = SPECPDL_INDEX ();
f9d64bb3 5768 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
488dd4c4 5769 char *cdef_file;
177c0ea7 5770
7452b7bd 5771 check_window_system (f);
6eeee4d6 5772
f9d64bb3 5773 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
24ca7c5a 5774
5ae53dcf 5775 if (popup_activated ())
24ca7c5a
JD
5776 error ("Trying to use a menu from within a menu-entry");
5777
488dd4c4
JD
5778 CHECK_STRING (prompt);
5779 CHECK_STRING (dir);
5780
5781 /* Prevent redisplay. */
5782 specbind (Qinhibit_redisplay, Qt);
27e498e6 5783 record_unwind_protect_void (clean_up_dialog);
488dd4c4 5784
4d7e6e51 5785 block_input ();
488dd4c4
JD
5786
5787 if (STRINGP (default_filename))
c0951e53 5788 cdef_file = SSDATA (default_filename);
488dd4c4 5789 else
c0951e53 5790 cdef_file = SSDATA (dir);
488dd4c4 5791
c0951e53 5792 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
f9d64bb3
JD
5793 ! NILP (mustmatch),
5794 ! NILP (only_dir_p));
177c0ea7 5795
488dd4c4
JD
5796 if (fn)
5797 {
5798 file = build_string (fn);
5799 xfree (fn);
5800 }
5801
4d7e6e51 5802 unblock_input ();
488dd4c4
JD
5803 UNGCPRO;
5804
5805 /* Make "Cancel" equivalent to C-g. */
5806 if (NILP (file))
5807 Fsignal (Qquit, Qnil);
177c0ea7 5808
a8f4bdc6
JD
5809 decoded_file = DECODE_FILE (file);
5810
5811 return unbind_to (count, decoded_file);
488dd4c4
JD
5812}
5813
9bf80974
CY
5814
5815#ifdef HAVE_FREETYPE
5816
f82605c6 5817DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
f2045622
CY
5818 doc: /* Read a font using a GTK dialog.
5819Return either a font spec (for GTK versions >= 3.2) or a string
5820containing a GTK-style font name.
f82605c6 5821
f2045622
CY
5822FRAME is the frame on which to pop up the font chooser. If omitted or
5823nil, it defaults to the selected frame. */)
5842a27b 5824 (Lisp_Object frame, Lisp_Object ignored)
9bf80974 5825{
a10c8269 5826 struct frame *f = decode_window_system_frame (frame);
637fa988 5827 Lisp_Object font;
e0ab5fcf
JD
5828 Lisp_Object font_param;
5829 char *default_name = NULL;
637fa988 5830 struct gcpro gcpro1, gcpro2;
d311d28c 5831 ptrdiff_t count = SPECPDL_INDEX ();
9bf80974 5832
9bf80974
CY
5833 if (popup_activated ())
5834 error ("Trying to use a menu from within a menu-entry");
5835
5836 /* Prevent redisplay. */
5837 specbind (Qinhibit_redisplay, Qt);
27e498e6 5838 record_unwind_protect_void (clean_up_dialog);
9bf80974 5839
4d7e6e51 5840 block_input ();
9bf80974 5841
5e617bc2 5842 GCPRO2 (font_param, font);
e0ab5fcf 5843
637fa988 5844 XSETFONT (font, FRAME_FONT (f));
581e51e8 5845 font_param = Ffont_get (font, intern (":name"));
637fa988 5846 if (STRINGP (font_param))
c0951e53 5847 default_name = xstrdup (SSDATA (font_param));
f5f6c0e0 5848 else
53c7540d 5849 {
637fa988
JD
5850 font_param = Fframe_parameter (frame, Qfont_param);
5851 if (STRINGP (font_param))
c0951e53 5852 default_name = xstrdup (SSDATA (font_param));
53c7540d 5853 }
e0ab5fcf 5854
f2045622 5855 font = xg_get_font (f, default_name);
581e51e8 5856 xfree (default_name);
9bf80974 5857
4d7e6e51 5858 unblock_input ();
9bf80974
CY
5859
5860 if (NILP (font))
5861 Fsignal (Qquit, Qnil);
5862
5863 return unbind_to (count, font);
5864}
5865#endif /* HAVE_FREETYPE */
5866
488dd4c4 5867#endif /* USE_GTK */
333b20bb
GM
5868
5869\f
82bab41c
GM
5870/***********************************************************************
5871 Keyboard
5872 ***********************************************************************/
5873
afeee3e5 5874#ifdef HAVE_XKB
82bab41c
GM
5875#include <X11/XKBlib.h>
5876#include <X11/keysym.h>
5877#endif
5878
5879DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5880 Sx_backspace_delete_keys_p, 0, 1, 0,
7ee72033 5881 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
c061c855
GM
5882FRAME nil means use the selected frame.
5883Value is t if we know that both keys are present, and are mapped to the
20d1c5c5
DN
5884usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5885present and mapped to the usual X keysyms. */)
5842a27b 5886 (Lisp_Object frame)
82bab41c 5887{
afeee3e5
PE
5888#ifndef HAVE_XKB
5889 return Qlambda;
5890#else
82bab41c 5891 XkbDescPtr kb;
7452b7bd 5892 struct frame *f = decode_window_system_frame (frame);
82bab41c
GM
5893 Display *dpy = FRAME_X_DISPLAY (f);
5894 Lisp_Object have_keys;
58d2d479 5895 int major, minor, op, event, error_code;
82bab41c 5896
4d7e6e51 5897 block_input ();
46f6a258
GM
5898
5899 /* Check library version in case we're dynamically linked. */
5900 major = XkbMajorVersion;
5901 minor = XkbMinorVersion;
5902 if (!XkbLibraryVersion (&major, &minor))
c1efd260 5903 {
4d7e6e51 5904 unblock_input ();
20d1c5c5 5905 return Qlambda;
c1efd260 5906 }
46f6a258
GM
5907
5908 /* Check that the server supports XKB. */
5909 major = XkbMajorVersion;
5910 minor = XkbMinorVersion;
58d2d479 5911 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
c1efd260 5912 {
4d7e6e51 5913 unblock_input ();
20d1c5c5 5914 return Qlambda;
c1efd260 5915 }
177c0ea7 5916
3a441526
JD
5917 /* In this code we check that the keyboard has physical keys with names
5918 that start with BKSP (Backspace) and DELE (Delete), and that they
5919 generate keysym XK_BackSpace and XK_Delete respectively.
5920 This function is used to test if normal-erase-is-backspace should be
5921 turned on.
5922 An alternative approach would be to just check if XK_BackSpace and
5923 XK_Delete are mapped to any key. But if any of those are mapped to
5924 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5925 user doesn't know about it, it is better to return false here.
5926 It is more obvious to the user what to do if she/he has two keys
5927 clearly marked with names/symbols and one key does something not
5928 expected (i.e. she/he then tries the other).
5929 The cases where Backspace/Delete is mapped to some other key combination
5930 are rare, and in those cases, normal-erase-is-backspace can be turned on
5931 manually. */
5932
46f6a258 5933 have_keys = Qnil;
c1efd260 5934 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
82bab41c
GM
5935 if (kb)
5936 {
5937 int delete_keycode = 0, backspace_keycode = 0, i;
c1efd260
GM
5938
5939 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
82bab41c 5940 {
c1efd260
GM
5941 for (i = kb->min_key_code;
5942 (i < kb->max_key_code
5943 && (delete_keycode == 0 || backspace_keycode == 0));
5944 ++i)
5945 {
d63931a2
GM
5946 /* The XKB symbolic key names can be seen most easily in
5947 the PS file generated by `xkbprint -label name
5948 $DISPLAY'. */
72af86bd 5949 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
c1efd260 5950 delete_keycode = i;
72af86bd 5951 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
c1efd260
GM
5952 backspace_keycode = i;
5953 }
5954
5955 XkbFreeNames (kb, 0, True);
82bab41c
GM
5956 }
5957
c1efd260 5958 XkbFreeClientMap (kb, 0, True);
177c0ea7 5959
82bab41c
GM
5960 if (delete_keycode
5961 && backspace_keycode
5962 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5963 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5964 have_keys = Qt;
5965 }
4d7e6e51 5966 unblock_input ();
82bab41c 5967 return have_keys;
afeee3e5 5968#endif
82bab41c
GM
5969}
5970
5971
5972\f
333b20bb
GM
5973/***********************************************************************
5974 Initialization
5975 ***********************************************************************/
5976
4dacadcc 5977/* Keep this list in the same order as frame_parms in frame.c.
7c0d3ed8
KS
5978 Use 0 for unsupported frame parameters. */
5979
5980frame_parm_handler x_frame_parm_handlers[] =
5981{
5982 x_set_autoraise,
5983 x_set_autolower,
5984 x_set_background_color,
5985 x_set_border_color,
5986 x_set_border_width,
5987 x_set_cursor_color,
5988 x_set_cursor_type,
5989 x_set_font,
5990 x_set_foreground_color,
5991 x_set_icon_name,
5992 x_set_icon_type,
5993 x_set_internal_border_width,
880e6158
MR
5994 x_set_right_divider_width,
5995 x_set_bottom_divider_width,
7c0d3ed8
KS
5996 x_set_menu_bar_lines,
5997 x_set_mouse_color,
5998 x_explicitly_set_name,
5999 x_set_scroll_bar_width,
6000 x_set_title,
6001 x_set_unsplittable,
6002 x_set_vertical_scroll_bars,
6003 x_set_visibility,
6004 x_set_tool_bar_lines,
6005 x_set_scroll_bar_foreground,
6006 x_set_scroll_bar_background,
6007 x_set_screen_gamma,
6008 x_set_line_spacing,
6009 x_set_fringe_width,
6010 x_set_fringe_width,
6011 x_set_wait_for_wm,
6012 x_set_fullscreen,
271a71c7 6013 x_set_font_backend,
cad9ef74
JD
6014 x_set_alpha,
6015 x_set_sticky,
bfeabdc3 6016 x_set_tool_bar_position,
7c0d3ed8
KS
6017};
6018
333b20bb 6019void
971de7fb 6020syms_of_xfns (void)
333b20bb 6021{
333b20bb
GM
6022 /* The section below is built by the lisp expression at the top of the file,
6023 just above where these variables are declared. */
6024 /*&&& init symbols here &&&*/
cd3520a4
JB
6025 DEFSYM (Qsuppress_icon, "suppress-icon");
6026 DEFSYM (Qundefined_color, "undefined-color");
6027 DEFSYM (Qcompound_text, "compound-text");
6028 DEFSYM (Qcancel_timer, "cancel-timer");
6029 DEFSYM (Qfont_param, "font-parameter");
f9942c9e
JB
6030 /* This is the end of symbol initialization. */
6031
01f1ba30 6032 Fput (Qundefined_color, Qerror_conditions,
3438fe21 6033 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
01f1ba30 6034 Fput (Qundefined_color, Qerror_message,
2a0213a6 6035 build_pure_c_string ("Undefined color"));
01f1ba30 6036
29208e82 6037 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
7ee72033 6038 doc: /* The shape of the pointer when over text.
c061c855
GM
6039Changing the value does not affect existing frames
6040unless you set the mouse color. */);
01f1ba30
JB
6041 Vx_pointer_shape = Qnil;
6042
ca0ecbf5 6043#if 0 /* This doesn't really do anything. */
29208e82 6044 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
7ee72033 6045 doc: /* The shape of the pointer when not over text.
c061c855
GM
6046This variable takes effect when you create a new frame
6047or when you set the mouse color. */);
af01ef26 6048#endif
01f1ba30
JB
6049 Vx_nontext_pointer_shape = Qnil;
6050
29208e82 6051 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
7ee72033 6052 doc: /* The shape of the pointer when Emacs is busy.
c061c855
GM
6053This variable takes effect when you create a new frame
6054or when you set the mouse color. */);
0af913d7 6055 Vx_hourglass_pointer_shape = Qnil;
333b20bb 6056
ca0ecbf5 6057#if 0 /* This doesn't really do anything. */
29208e82 6058 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
7ee72033 6059 doc: /* The shape of the pointer when over the mode line.
c061c855
GM
6060This variable takes effect when you create a new frame
6061or when you set the mouse color. */);
af01ef26 6062#endif
01f1ba30
JB
6063 Vx_mode_pointer_shape = Qnil;
6064
d3b06468 6065 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
29208e82 6066 Vx_sensitive_text_pointer_shape,
7ee72033 6067 doc: /* The shape of the pointer when over mouse-sensitive text.
c061c855
GM
6068This variable takes effect when you create a new frame
6069or when you set the mouse color. */);
ca0ecbf5 6070 Vx_sensitive_text_pointer_shape = Qnil;
95f80c78 6071
8fb4ec9c 6072 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
29208e82 6073 Vx_window_horizontal_drag_shape,
7ee72033 6074 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
c061c855
GM
6075This variable takes effect when you create a new frame
6076or when you set the mouse color. */);
8fb4ec9c
GM
6077 Vx_window_horizontal_drag_shape = Qnil;
6078
880e6158
MR
6079 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6080 Vx_window_vertical_drag_shape,
6081 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6082This variable takes effect when you create a new frame
6083or when you set the mouse color. */);
6084 Vx_window_vertical_drag_shape = Qnil;
6085
29208e82 6086 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
7ee72033 6087 doc: /* A string indicating the foreground color of the cursor box. */);
01f1ba30
JB
6088 Vx_cursor_fore_pixel = Qnil;
6089
29208e82 6090 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
f5f6c0e0
JB
6091 doc: /* Maximum size for tooltips.
6092Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
d63931a2 6093 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
177c0ea7 6094
29208e82 6095 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
7ee72033 6096 doc: /* Non-nil if no X window manager is in use.
c061c855
GM
6097Emacs doesn't try to figure this out; this is always nil
6098unless you set it to something else. */);
2d38195d
RS
6099 /* We don't have any way to find this out, so set it to nil
6100 and maybe the user would like to set it to t. */
6101 Vx_no_window_manager = Qnil;
1d3dac41 6102
942ea06d 6103 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
29208e82 6104 Vx_pixel_size_width_font_regexp,
7ee72033 6105 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
c061c855
GM
6106
6107Since Emacs gets width of a font matching with this regexp from
6108PIXEL_SIZE field of the name, font finding mechanism gets faster for
6109such a font. This is especially effective for such large fonts as
6110Chinese, Japanese, and Korean. */);
942ea06d
KH
6111 Vx_pixel_size_width_font_regexp = Qnil;
6112
255e4140 6113/* This is not ifdef:ed, so other builds than GTK can customize it. */
29208e82 6114 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
fb7ada5f 6115 doc: /* Non-nil means prompt with the old GTK file selection dialog.
255e4140
JD
6116If nil or if the file selection dialog is not available, the new GTK file
6117chooser is used instead. To turn off all file dialogs set the
6118variable `use-file-dialog'. */);
fe2b14b7 6119 x_gtk_use_old_file_dialog = 0;
255e4140 6120
29208e82 6121 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
fb7ada5f 6122 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
a8a35720
JD
6123Note that this is just the default, there is a toggle button on the file
6124chooser to show or not show hidden files on a case by case basis. */);
6125 x_gtk_show_hidden_files = 0;
6126
29208e82 6127 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
fb7ada5f 6128 doc: /* If non-nil, the GTK file chooser will show additional help text.
641c0105
JD
6129If more space for files in the file chooser dialog is wanted, set this to nil
6130to turn the additional text off. */);
159f8da7 6131 x_gtk_file_dialog_help_text = 1;
641c0105 6132
29208e82 6133 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
fb7ada5f 6134 doc: /* If non-nil, a detached tool bar is shown in full.
e0f467cb
JD
6135The default is to just show an arrow and pressing on that arrow shows
6136the tool bar buttons. */);
6137 x_gtk_whole_detached_tool_bar = 0;
6138
29208e82 6139 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
fb7ada5f 6140 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
aa1859f5
J
6141Otherwise use Emacs own tooltip implementation.
6142When using Gtk+ tooltips, the tooltip face is not used. */);
6143 x_gtk_use_system_tooltips = 1;
6144
8d2db8be
DA
6145 /* Tell Emacs about this window system. */
6146 Fprovide (Qx, Qnil);
49be9d28 6147
1d3dac41 6148#ifdef USE_X_TOOLKIT
d67b4f80 6149 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5b827abb 6150#ifdef USE_MOTIF
d67b4f80 6151 Fprovide (intern_c_string ("motif"), Qnil);
fc2cdd9a 6152
29208e82 6153 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
7ee72033 6154 doc: /* Version info for LessTif/Motif. */);
fc2cdd9a
GM
6155 Vmotif_version_string = build_string (XmVERSION_STRING);
6156#endif /* USE_MOTIF */
6157#endif /* USE_X_TOOLKIT */
01f1ba30 6158
6b61353c 6159#ifdef USE_GTK
1ec2a572
JD
6160 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6161 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6162 But for a user it is a toolkit for X, and indeed, configure
6163 accepts --with-x-toolkit=gtk. */
d67b4f80
DN
6164 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6165 Fprovide (intern_c_string ("gtk"), Qnil);
bfeabdc3 6166 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6b61353c 6167
29208e82 6168 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6b61353c
KH
6169 doc: /* Version info for GTK+. */);
6170 {
99027bdd
PE
6171 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6172 int len = sprintf (gtk_version, "%d.%d.%d",
6173 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6174 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6b61353c
KH
6175 }
6176#endif /* USE_GTK */
6177
333b20bb
GM
6178 /* X window properties. */
6179 defsubr (&Sx_change_window_property);
6180 defsubr (&Sx_delete_window_property);
6181 defsubr (&Sx_window_property);
6182
2d764c78 6183 defsubr (&Sxw_display_color_p);
d0c9d219 6184 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
6185 defsubr (&Sxw_color_defined_p);
6186 defsubr (&Sxw_color_values);
9d317b2c 6187 defsubr (&Sx_server_max_request_size);
41beb8fc
RS
6188 defsubr (&Sx_server_vendor);
6189 defsubr (&Sx_server_version);
6190 defsubr (&Sx_display_pixel_width);
6191 defsubr (&Sx_display_pixel_height);
6192 defsubr (&Sx_display_mm_width);
6193 defsubr (&Sx_display_mm_height);
6194 defsubr (&Sx_display_screens);
6195 defsubr (&Sx_display_planes);
6196 defsubr (&Sx_display_color_cells);
6197 defsubr (&Sx_display_visual_class);
6198 defsubr (&Sx_display_backing_store);
6199 defsubr (&Sx_display_save_under);
4e3f9230 6200 defsubr (&Sx_display_monitor_attributes_list);
1c44e124 6201 defsubr (&Sx_wm_set_size_hint);
f676886a 6202 defsubr (&Sx_create_frame);
01f1ba30 6203 defsubr (&Sx_open_connection);
08a90d6a
RS
6204 defsubr (&Sx_close_connection);
6205 defsubr (&Sx_display_list);
01f1ba30 6206 defsubr (&Sx_synchronize);
82bab41c 6207 defsubr (&Sx_backspace_delete_keys_p);
177c0ea7 6208
333b20bb
GM
6209 defsubr (&Sx_show_tip);
6210 defsubr (&Sx_hide_tip);
333b20bb 6211 tip_timer = Qnil;
44b5a125
GM
6212 staticpro (&tip_timer);
6213 tip_frame = Qnil;
6214 staticpro (&tip_frame);
333b20bb 6215
06d62053
GM
6216 last_show_tip_args = Qnil;
6217 staticpro (&last_show_tip_args);
6218
90f2e16b 6219 defsubr (&Sx_uses_old_gtk_dialog);
29fabd8c 6220#if defined (USE_MOTIF) || defined (USE_GTK)
333b20bb
GM
6221 defsubr (&Sx_file_dialog);
6222#endif
9bf80974 6223
946598bf 6224#if defined (USE_GTK) && defined (HAVE_FREETYPE)
f82605c6 6225 defsubr (&Sx_select_font);
9bf80974 6226#endif
333b20bb 6227}