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