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