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