Get rid of some platform-specific functions examining window
[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
40e6f148 25/* This makes the fields of a Display accessible, in Xlib header files. */
333b20bb 26
40e6f148
RS
27#define XLIB_ILLEGAL_ACCESS
28
01f1ba30
JB
29#include "lisp.h"
30#include "xterm.h"
f676886a 31#include "frame.h"
01f1ba30 32#include "window.h"
e5560ff7 33#include "character.h"
01f1ba30 34#include "buffer.h"
58cad5ed 35#include "intervals.h"
01f1ba30 36#include "dispextern.h"
1f98fa48 37#include "keyboard.h"
9ac0d9e0 38#include "blockinput.h"
57bda87a 39#include <epaths.h>
942ea06d 40#include "charset.h"
96db09e4 41#include "coding.h"
942ea06d 42#include "fontset.h"
333b20bb
GM
43#include "systime.h"
44#include "termhooks.h"
4ae9a85e 45#include "atimer.h"
28d440ab 46#include "termchar.h"
02ed2ea8 47#include "font.h"
02ed2ea8 48
01f1ba30 49#ifdef HAVE_X_WINDOWS
67ba84d1 50
63cec32f
GM
51#include <sys/types.h>
52#include <sys/stat.h>
01f1ba30 53
0505a740 54#if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
ef493a27
RS
55#include "bitmaps/gray.xbm"
56#else
dbc4e1c1 57#include <X11/bitmaps/gray>
ef493a27 58#endif
dbc4e1c1 59
1b9ac145
AS
60#include "xsettings.h"
61
488dd4c4
JD
62#ifdef USE_GTK
63#include "gtkutil.h"
64#endif
65
9ef48a9d
RS
66#ifdef USE_X_TOOLKIT
67#include <X11/Shell.h>
68
398ffa92 69#ifndef USE_MOTIF
8ab9202e
CY
70#ifdef HAVE_XAW3D
71#include <X11/Xaw3d/Paned.h>
a6ec6cfb 72#include <X11/Xaw3d/Label.h>
8ab9202e 73#else /* !HAVE_XAW3D */
9ef48a9d
RS
74#include <X11/Xaw/Paned.h>
75#include <X11/Xaw/Label.h>
8ab9202e 76#endif /* HAVE_XAW3D */
398ffa92 77#endif /* USE_MOTIF */
9ef48a9d
RS
78
79#ifdef USG
80#undef USG /* ####KLUDGE for Solaris 2.2 and up */
81#include <X11/Xos.h>
82#define USG
dc5ddd85
PE
83#ifdef USG /* Pacify gcc -Wunused-macros. */
84#endif
9ef48a9d
RS
85#else
86#include <X11/Xos.h>
87#endif
88
89#include "widget.h"
90
91#include "../lwlib/lwlib.h"
92
333b20bb
GM
93#ifdef USE_MOTIF
94#include <Xm/Xm.h>
95#include <Xm/DialogS.h>
96#include <Xm/FileSB.h>
dee186b6
J
97#include <Xm/List.h>
98#include <Xm/TextF.h>
333b20bb
GM
99#endif
100
eec47d6b
DN
101#ifdef USE_LUCID
102#include "../lwlib/xlwmenu.h"
103#endif
104
5e617bc2 105#if !defined (NO_EDITRES)
6c32dd68 106#define HACK_EDITRES
764430a3 107extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
ee6bacd4 108#endif /* not defined NO_EDITRES */
6c32dd68 109
333b20bb
GM
110/* Unique id counter for widgets created by the Lucid Widget Library. */
111
6c32dd68
PR
112extern LWLIB_ID widget_id_tick;
113
fc2cdd9a
GM
114#ifdef USE_MOTIF
115
fc2cdd9a
GM
116#endif /* USE_MOTIF */
117
9ef48a9d
RS
118#endif /* USE_X_TOOLKIT */
119
6b61353c
KH
120#ifdef USE_GTK
121
6b61353c
KH
122#endif /* USE_GTK */
123
9d317b2c 124#define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
9d317b2c 125
955cbe7b
PE
126static Lisp_Object Qsuppress_icon;
127static Lisp_Object Qundefined_color;
128static Lisp_Object Qcompound_text, Qcancel_timer;
581e51e8 129Lisp_Object Qfont_param;
01f1ba30 130
e509cfa6 131#ifdef GLYPH_DEBUG
13464394
PE
132static ptrdiff_t image_cache_refcount;
133static int dpyinfo_refcount;
eaf1eea9
GM
134#endif
135
e4cebfca
PE
136static struct x_display_info *x_display_info_for_name (Lisp_Object);
137
7e59217d 138/* Let the user specify an X display with a Lisp object.
708e05dc 139 OBJECT may be nil, a frame or a terminal object.
b9dc4443
RS
140 nil stands for the selected frame--or, if that is not an X frame,
141 the first X display on the list. */
142
7c0d3ed8 143struct x_display_info *
971de7fb 144check_x_display_info (Lisp_Object object)
b9dc4443 145{
8ec8a5ec 146 struct x_display_info *dpyinfo = NULL;
177c0ea7 147
7e59217d 148 if (NILP (object))
b9dc4443 149 {
0fe92f72 150 struct frame *sf = XFRAME (selected_frame);
177c0ea7 151
0fe92f72 152 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
8ec8a5ec 153 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
b9dc4443 154 else if (x_display_list != 0)
8ec8a5ec 155 dpyinfo = x_display_list;
b9dc4443
RS
156 else
157 error ("X windows are not in use or not initialized");
158 }
708e05dc 159 else if (TERMINALP (object))
b6660415 160 {
fd0f53a9 161 struct terminal *t = get_terminal (object, 1);
b6660415 162
6ed8eeff 163 if (t->type != output_x_window)
c2982e87 164 error ("Terminal %"pI"d is not an X display", XINT (object));
b6660415 165
6ed8eeff 166 dpyinfo = t->display_info.x;
b6660415 167 }
7e59217d
KL
168 else if (STRINGP (object))
169 dpyinfo = x_display_info_for_name (object);
b9dc4443
RS
170 else
171 {
7452b7bd 172 FRAME_PTR f = decode_window_system_frame (object);
8ec8a5ec 173 dpyinfo = FRAME_X_DISPLAY_INFO (f);
b9dc4443 174 }
8ec8a5ec
GM
175
176 return dpyinfo;
b9dc4443 177}
333b20bb 178
b9dc4443 179\f
f676886a
JB
180/* Return the Emacs frame-object corresponding to an X window.
181 It could be the frame's main window or an icon window. */
01f1ba30 182
34ca5317 183/* This function can be called during GC, so use GC_xxx type test macros. */
bcb2db92 184
f676886a 185struct frame *
971de7fb 186x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
01f1ba30 187{
f676886a
JB
188 Lisp_Object tail, frame;
189 struct frame *f;
01f1ba30 190
5b04e9f9
DA
191 if (wdesc == None)
192 return NULL;
10b43ac3 193
5b04e9f9 194 FOR_EACH_FRAME (tail, frame)
01f1ba30 195 {
f676886a 196 f = XFRAME (frame);
2d764c78 197 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
2d271e2e 198 continue;
0af913d7 199 if (f->output_data.x->hourglass_window == wdesc)
17cbbf95 200 return f;
9ef48a9d 201#ifdef USE_X_TOOLKIT
177c0ea7 202 if ((f->output_data.x->edit_widget
7556890b 203 && XtWindow (f->output_data.x->edit_widget) == wdesc)
333b20bb
GM
204 /* A tooltip frame? */
205 || (!f->output_data.x->edit_widget
206 && FRAME_X_WINDOW (f) == wdesc)
7556890b 207 || f->output_data.x->icon_desc == wdesc)
9ef48a9d
RS
208 return f;
209#else /* not USE_X_TOOLKIT */
488dd4c4
JD
210#ifdef USE_GTK
211 if (f->output_data.x->edit_widget)
212 {
6b61353c 213 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
488dd4c4
JD
214 struct x_output *x = f->output_data.x;
215 if (gwdesc != 0 && gwdesc == x->edit_widget)
216 return f;
217 }
218#endif /* USE_GTK */
fe24a618 219 if (FRAME_X_WINDOW (f) == wdesc
7556890b 220 || f->output_data.x->icon_desc == wdesc)
f676886a 221 return f;
9ef48a9d
RS
222#endif /* not USE_X_TOOLKIT */
223 }
224 return 0;
225}
226
488dd4c4 227#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
9ef48a9d
RS
228/* Like x_window_to_frame but also compares the window with the widget's
229 windows. */
230
231struct frame *
971de7fb 232x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
9ef48a9d
RS
233{
234 Lisp_Object tail, frame;
5b04e9f9 235 struct frame *f, *found = NULL;
7556890b 236 struct x_output *x;
9ef48a9d 237
5b04e9f9
DA
238 if (wdesc == None)
239 return NULL;
10b43ac3 240
5b04e9f9 241 FOR_EACH_FRAME (tail, frame)
9ef48a9d 242 {
5b04e9f9
DA
243 if (found)
244 break;
9ef48a9d 245 f = XFRAME (frame);
17cbbf95 246 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
333b20bb 247 {
17cbbf95
GM
248 /* This frame matches if the window is any of its widgets. */
249 x = f->output_data.x;
0af913d7 250 if (x->hourglass_window == wdesc)
17cbbf95
GM
251 found = f;
252 else if (x->widget)
253 {
488dd4c4 254#ifdef USE_GTK
6b61353c 255 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
488dd4c4 256 if (gwdesc != 0
a54fa5b7 257 && gtk_widget_get_toplevel (gwdesc) == x->widget)
488dd4c4
JD
258 found = f;
259#else
177c0ea7
JB
260 if (wdesc == XtWindow (x->widget)
261 || wdesc == XtWindow (x->column_widget)
17cbbf95
GM
262 || wdesc == XtWindow (x->edit_widget))
263 found = f;
264 /* Match if the window is this frame's menubar. */
265 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
266 found = f;
488dd4c4 267#endif
17cbbf95
GM
268 }
269 else if (FRAME_X_WINDOW (f) == wdesc)
270 /* A tooltip frame. */
271 found = f;
333b20bb 272 }
01f1ba30 273 }
177c0ea7 274
17cbbf95 275 return found;
01f1ba30 276}
5e65b9ab 277
fd3a3022
RS
278/* Likewise, but consider only the menu bar widget. */
279
280struct frame *
fea9cabd 281x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
fd3a3022 282{
b78f9767 283 Window wdesc = event->xany.window;
fd3a3022
RS
284 Lisp_Object tail, frame;
285 struct frame *f;
7556890b 286 struct x_output *x;
fd3a3022 287
5b04e9f9
DA
288 if (wdesc == None)
289 return NULL;
10b43ac3 290
5b04e9f9 291 FOR_EACH_FRAME (tail, frame)
fd3a3022 292 {
fd3a3022 293 f = XFRAME (frame);
2d764c78 294 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
fd3a3022 295 continue;
7556890b 296 x = f->output_data.x;
488dd4c4 297#ifdef USE_GTK
b78f9767
J
298 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
299 return f;
488dd4c4 300#else
b78f9767 301 /* Match if the window is this frame's menubar. */
333b20bb
GM
302 if (x->menubar_widget
303 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
fd3a3022 304 return f;
488dd4c4 305#endif
fd3a3022
RS
306 }
307 return 0;
308}
309
5e65b9ab
RS
310/* Return the frame whose principal (outermost) window is WDESC.
311 If WDESC is some other (smaller) window, we return 0. */
312
313struct frame *
971de7fb 314x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
5e65b9ab
RS
315{
316 Lisp_Object tail, frame;
317 struct frame *f;
7556890b 318 struct x_output *x;
5e65b9ab 319
5b04e9f9
DA
320 if (wdesc == None)
321 return NULL;
10b43ac3 322
5b04e9f9 323 FOR_EACH_FRAME (tail, frame)
5e65b9ab 324 {
5e65b9ab 325 f = XFRAME (frame);
2d764c78 326 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
2d271e2e 327 continue;
7556890b 328 x = f->output_data.x;
333b20bb
GM
329
330 if (x->widget)
331 {
332 /* This frame matches if the window is its topmost widget. */
488dd4c4 333#ifdef USE_GTK
6b61353c 334 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
488dd4c4
JD
335 if (gwdesc == x->widget)
336 return f;
337#else
333b20bb
GM
338 if (wdesc == XtWindow (x->widget))
339 return f;
7a994728
KH
340#if 0 /* I don't know why it did this,
341 but it seems logically wrong,
342 and it causes trouble for MapNotify events. */
333b20bb 343 /* Match if the window is this frame's menubar. */
177c0ea7 344 if (x->menubar_widget
333b20bb
GM
345 && wdesc == XtWindow (x->menubar_widget))
346 return f;
488dd4c4 347#endif
7a994728 348#endif
333b20bb
GM
349 }
350 else if (FRAME_X_WINDOW (f) == wdesc)
351 /* Tooltip frame. */
352 return f;
5e65b9ab
RS
353 }
354 return 0;
355}
488dd4c4 356#endif /* USE_X_TOOLKIT || USE_GTK */
01f1ba30 357
01f1ba30 358\f
203c1d73 359
08a90d6a 360/* Store the screen positions of frame F into XPTR and YPTR.
e9445337
RS
361 These are the positions of the containing window manager window,
362 not Emacs's own window. */
363
364void
971de7fb 365x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
e9445337 366{
8ffc96f5 367 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
49d41073
EZ
368 int real_x = 0, real_y = 0;
369 int had_errors = 0;
370 Window win = f->output_data.x->parent_desc;
d75c9992
JD
371 Atom actual_type;
372 unsigned long actual_size, bytes_remaining;
06b0c8a0 373 int rc, actual_format;
d75c9992
JD
374 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
375 long max_len = 400;
376 Display *dpy = FRAME_X_DISPLAY (f);
377 unsigned char *tmp_data = NULL;
378 Atom target_type = XA_CARDINAL;
e9445337 379
4d7e6e51 380 block_input ();
49d41073 381
d75c9992 382 x_catch_errors (dpy);
043835a3 383
31887d45 384 if (win == dpyinfo->root_window)
49d41073
EZ
385 win = FRAME_OUTER_WINDOW (f);
386
387 /* This loop traverses up the containment tree until we hit the root
388 window. Window managers may intersect many windows between our window
389 and the root window. The window we find just before the root window
390 should be the outer WM window. */
391 for (;;)
e9445337 392 {
49d41073
EZ
393 Window wm_window, rootw;
394 Window *tmp_children;
395 unsigned int tmp_nchildren;
e7161ad9 396 int success;
ca7bac79 397
e7161ad9
RS
398 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
399 &wm_window, &tmp_children, &tmp_nchildren);
08a90d6a 400
49d41073 401 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
08a90d6a 402
e7161ad9
RS
403 /* Don't free tmp_children if XQueryTree failed. */
404 if (! success)
405 break;
406
98c6f1e3 407 XFree (tmp_children);
e7161ad9 408
49d41073
EZ
409 if (wm_window == rootw || had_errors)
410 break;
08a90d6a 411
49d41073
EZ
412 win = wm_window;
413 }
177c0ea7 414
49d41073
EZ
415 if (! had_errors)
416 {
47c53789 417 unsigned int ign;
49d41073 418 Window child, rootw;
177c0ea7 419
49d41073
EZ
420 /* Get the real coordinates for the WM window upper left corner */
421 XGetGeometry (FRAME_X_DISPLAY (f), win,
422 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
423
424 /* Translate real coordinates to coordinates relative to our
425 window. For our window, the upper left corner is 0, 0.
426 Since the upper left corner of the WM window is outside
427 our window, win_x and win_y will be negative:
428
429 ------------------ ---> x
430 | title |
431 | ----------------- v y
432 | | our window
433 */
8a07bba0 434 XTranslateCoordinates (FRAME_X_DISPLAY (f),
e9445337 435
8a07bba0 436 /* From-window, to-window. */
8a07bba0 437 FRAME_X_DISPLAY_INFO (f)->root_window,
49d41073 438 FRAME_X_WINDOW (f),
e9445337 439
8a07bba0 440 /* From-position, to-position. */
49d41073 441 real_x, real_y, &win_x, &win_y,
08a90d6a 442
8a07bba0
RS
443 /* Child of win. */
444 &child);
e9445337 445
49d41073 446 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
845e9d85 447 {
49d41073
EZ
448 outer_x = win_x;
449 outer_y = win_y;
845e9d85 450 }
49d41073
EZ
451 else
452 {
453 XTranslateCoordinates (FRAME_X_DISPLAY (f),
ca7bac79 454
49d41073
EZ
455 /* From-window, to-window. */
456 FRAME_X_DISPLAY_INFO (f)->root_window,
457 FRAME_OUTER_WINDOW (f),
177c0ea7 458
49d41073
EZ
459 /* From-position, to-position. */
460 real_x, real_y, &outer_x, &outer_y,
177c0ea7 461
49d41073
EZ
462 /* Child of win. */
463 &child);
be786000 464 }
08a90d6a 465
49d41073
EZ
466 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
467 }
177c0ea7 468
31887d45
JD
469
470 if (dpyinfo->root_window == f->output_data.x->parent_desc)
471 {
472 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
473 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
474 0, max_len, False, target_type,
475 &actual_type, &actual_format, &actual_size,
476 &bytes_remaining, &tmp_data);
477
478 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
479 && actual_size == 4 && actual_format == 32)
480 {
717c30e0 481 unsigned int ign;
31887d45 482 Window rootw;
c0098065 483 long *fe = (long *)tmp_data;
31887d45
JD
484
485 XGetGeometry (FRAME_X_DISPLAY (f), win,
486 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
31887d45
JD
487 outer_x = -fe[0];
488 outer_y = -fe[2];
489 real_x -= fe[0];
490 real_y -= fe[2];
491 }
492 }
493
494 if (tmp_data) XFree (tmp_data);
495
4545fa20 496 x_uncatch_errors ();
177c0ea7 497
4d7e6e51 498 unblock_input ();
49d41073
EZ
499
500 if (had_errors) return;
177c0ea7 501
be786000
KS
502 f->x_pixels_diff = -win_x;
503 f->y_pixels_diff = -win_y;
504
505 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
506 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
49d41073
EZ
507
508 *xptr = real_x;
509 *yptr = real_y;
e9445337
RS
510}
511
01f1ba30 512\f
82978295 513
d62c8769
GM
514
515/* Gamma-correct COLOR on frame F. */
516
517void
971de7fb 518gamma_correct (struct frame *f, XColor *color)
d62c8769
GM
519{
520 if (f->gamma)
521 {
522 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
523 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
524 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
525 }
526}
527
528
7b746c38 529/* Decide if color named COLOR_NAME is valid for use on frame F. If
578098f3
PE
530 so, return the RGB values in COLOR. If ALLOC_P,
531 allocate the color. Value is false if COLOR_NAME is invalid, or
7b746c38 532 no color could be allocated. */
e12d55b2 533
578098f3 534bool
eec47d6b 535x_defined_color (struct frame *f, const char *color_name,
578098f3 536 XColor *color, bool alloc_p)
01f1ba30 537{
578098f3 538 bool success_p = 0;
7b746c38
GM
539 Display *dpy = FRAME_X_DISPLAY (f);
540 Colormap cmap = FRAME_X_COLORMAP (f);
01f1ba30 541
4d7e6e51 542 block_input ();
3a46642b
J
543#ifdef USE_GTK
544 success_p = xg_check_special_colors (f, color_name, color);
545#endif
546 if (!success_p)
578098f3 547 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
7b746c38
GM
548 if (success_p && alloc_p)
549 success_p = x_alloc_nearest_color (f, cmap, color);
4d7e6e51 550 unblock_input ();
01f1ba30 551
7b746c38 552 return success_p;
01f1ba30
JB
553}
554
9b2956e2
GM
555
556/* Return the pixel color value for color COLOR_NAME on frame F. If F
557 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
558 Signal an error if color can't be allocated. */
01f1ba30 559
f78faa98 560static int
971de7fb 561x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
01f1ba30 562{
b9dc4443 563 XColor cdef;
01f1ba30 564
b7826503 565 CHECK_STRING (color_name);
01f1ba30 566
9b2956e2
GM
567#if 0 /* Don't do this. It's wrong when we're not using the default
568 colormap, it makes freeing difficult, and it's probably not
569 an important optimization. */
d5db4077 570 if (strcmp (SDATA (color_name), "black") == 0)
b9dc4443 571 return BLACK_PIX_DEFAULT (f);
d5db4077 572 else if (strcmp (SDATA (color_name), "white") == 0)
b9dc4443 573 return WHITE_PIX_DEFAULT (f);
9b2956e2 574#endif
01f1ba30 575
9b2956e2 576 /* Return MONO_COLOR for monochrome frames. */
b9dc4443 577 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
9b2956e2 578 return mono_color;
01f1ba30 579
2d764c78 580 /* x_defined_color is responsible for coping with failures
95626e11 581 by looking for a near-miss. */
42a5b22f 582 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
95626e11
RS
583 return cdef.pixel;
584
93217cef 585 signal_error ("Undefined color", color_name);
01f1ba30 586}
9b2956e2
GM
587
588
01f1ba30 589\f
ea0a1f53
GM
590/* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
591 the previous value of that parameter, NEW_VALUE is the new value.
592 See also the comment of wait_for_wm in struct x_output. */
593
594static void
971de7fb 595x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
ea0a1f53
GM
596{
597 f->output_data.x->wait_for_wm = !NILP (new_value);
598}
599
bfeabdc3
JD
600static void
601x_set_tool_bar_position (struct frame *f,
602 Lisp_Object new_value,
603 Lisp_Object old_value)
604{
605 if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
606 && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
607 return;
608 if (EQ (new_value, old_value)) return;
609
610#ifdef USE_GTK
18e27ea8
PE
611 xg_change_toolbar_position (f, new_value);
612 fset_tool_bar_position (f, new_value);
bfeabdc3
JD
613#endif
614}
615
993d0721
JB
616#ifdef USE_GTK
617
465aa50a
JD
618/* Set icon from FILE for frame F. By using GTK functions the icon
619 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
993d0721
JB
620
621int
971de7fb 622xg_set_icon (FRAME_PTR f, Lisp_Object file)
993d0721 623{
465aa50a
JD
624 int result = 0;
625 Lisp_Object found;
993d0721 626
465aa50a
JD
627 found = x_find_image_file (file);
628
629 if (! NILP (found))
630 {
631 GdkPixbuf *pixbuf;
632 GError *err = NULL;
51b59d79 633 char *filename = SSDATA (found);
4d7e6e51 634 block_input ();
465aa50a
JD
635
636 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
637
638 if (pixbuf)
639 {
640 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
641 pixbuf);
642 g_object_unref (pixbuf);
643
644 result = 1;
645 }
646 else
647 g_error_free (err);
648
4d7e6e51 649 unblock_input ();
465aa50a
JD
650 }
651
465aa50a 652 return result;
993d0721 653}
9f7b984b
CY
654
655int
648801d1 656xg_set_icon_from_xpm_data (FRAME_PTR f, const char **data)
9f7b984b 657{
648801d1 658 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
9f7b984b
CY
659
660 if (!pixbuf)
47c53789 661 return 0;
9f7b984b 662
47c53789 663 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
9f7b984b
CY
664 g_object_unref (pixbuf);
665 return 1;
666}
993d0721
JB
667#endif /* USE_GTK */
668
ea0a1f53 669
f676886a 670/* Functions called only from `x_set_frame_param'
01f1ba30
JB
671 to set individual parameters.
672
fe24a618 673 If FRAME_X_WINDOW (f) is 0,
f676886a 674 the frame is being created and its X-window does not exist yet.
01f1ba30
JB
675 In that case, just record the parameter's new value
676 in the standard place; do not attempt to change the window. */
677
e4cebfca 678static void
971de7fb 679x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 680{
09393d07
GM
681 struct x_output *x = f->output_data.x;
682 unsigned long fg, old_fg;
a76206dc 683
09393d07 684 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ce593f6e
KL
685 old_fg = FRAME_FOREGROUND_PIXEL (f);
686 FRAME_FOREGROUND_PIXEL (f) = fg;
a76206dc 687
fe24a618 688 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 689 {
09393d07 690 Display *dpy = FRAME_X_DISPLAY (f);
177c0ea7 691
4d7e6e51 692 block_input ();
09393d07
GM
693 XSetForeground (dpy, x->normal_gc, fg);
694 XSetBackground (dpy, x->reverse_gc, fg);
36d42089 695
09393d07
GM
696 if (x->cursor_pixel == old_fg)
697 {
698 unload_color (f, x->cursor_pixel);
699 x->cursor_pixel = x_copy_color (f, fg);
700 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
701 }
177c0ea7 702
4d7e6e51 703 unblock_input ();
177c0ea7 704
05c8abbe 705 update_face_from_frame_parameter (f, Qforeground_color, arg);
177c0ea7 706
179956b9 707 if (FRAME_VISIBLE_P (f))
f676886a 708 redraw_frame (f);
01f1ba30 709 }
177c0ea7 710
09393d07 711 unload_color (f, old_fg);
01f1ba30
JB
712}
713
e4cebfca 714static void
971de7fb 715x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 716{
09393d07
GM
717 struct x_output *x = f->output_data.x;
718 unsigned long bg;
01f1ba30 719
09393d07 720 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
ce593f6e
KL
721 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
722 FRAME_BACKGROUND_PIXEL (f) = bg;
a76206dc 723
fe24a618 724 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 725 {
09393d07 726 Display *dpy = FRAME_X_DISPLAY (f);
177c0ea7 727
4d7e6e51 728 block_input ();
09393d07
GM
729 XSetBackground (dpy, x->normal_gc, bg);
730 XSetForeground (dpy, x->reverse_gc, bg);
731 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
732 XSetForeground (dpy, x->cursor_gc, bg);
733
488dd4c4
JD
734#ifdef USE_GTK
735 xg_set_background_color (f, bg);
736#endif
737
f76e0368
GM
738#ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
739 toolkit scroll bars. */
740 {
741 Lisp_Object bar;
742 for (bar = FRAME_SCROLL_BARS (f);
743 !NILP (bar);
744 bar = XSCROLL_BAR (bar)->next)
745 {
056ce195 746 Window window = XSCROLL_BAR (bar)->x_window;
f76e0368
GM
747 XSetWindowBackground (dpy, window, bg);
748 }
749 }
750#endif /* USE_TOOLKIT_SCROLL_BARS */
01f1ba30 751
4d7e6e51 752 unblock_input ();
05c8abbe 753 update_face_from_frame_parameter (f, Qbackground_color, arg);
ea96210c 754
179956b9 755 if (FRAME_VISIBLE_P (f))
f676886a 756 redraw_frame (f);
01f1ba30
JB
757 }
758}
759
e044e4fc 760static Cursor
971de7fb 761make_invisible_cursor (struct frame *f)
e044e4fc
JD
762{
763 Display *dpy = FRAME_X_DISPLAY (f);
764 static char const no_data[] = { 0 };
765 Pixmap pix;
766 XColor col;
8664db06 767 Cursor c = 0;
e044e4fc
JD
768
769 x_catch_errors (dpy);
770 pix = XCreateBitmapFromData (dpy, FRAME_X_DISPLAY_INFO (f)->root_window,
771 no_data, 1, 1);
772 if (! x_had_errors_p (dpy) && pix != None)
773 {
8664db06 774 Cursor pixc;
e044e4fc
JD
775 col.pixel = 0;
776 col.red = col.green = col.blue = 0;
777 col.flags = DoRed | DoGreen | DoBlue;
8664db06
PE
778 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
779 if (! x_had_errors_p (dpy) && pixc != None)
780 c = pixc;
e044e4fc
JD
781 XFreePixmap (dpy, pix);
782 }
783
784 x_uncatch_errors ();
785
786 return c;
787}
788
e4cebfca 789static void
971de7fb 790x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 791{
09393d07
GM
792 struct x_output *x = f->output_data.x;
793 Display *dpy = FRAME_X_DISPLAY (f);
3d970f28 794 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
0af913d7 795 Cursor hourglass_cursor, horizontal_drag_cursor;
51a1d2d8 796 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
ce593f6e 797 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
a76206dc 798
51a1d2d8 799 /* Don't let pointers be invisible. */
09393d07 800 if (mask_color == pixel)
bcf26b38
GM
801 {
802 x_free_colors (f, &pixel, 1);
ce593f6e 803 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
bcf26b38 804 }
a76206dc 805
09393d07
GM
806 unload_color (f, x->mouse_pixel);
807 x->mouse_pixel = pixel;
01f1ba30 808
4d7e6e51 809 block_input ();
fe24a618 810
eb8c3be9 811 /* It's not okay to crash if the user selects a screwy cursor. */
9ba8e10d 812 x_catch_errors (dpy);
fe24a618 813
09393d07 814 if (!NILP (Vx_pointer_shape))
01f1ba30 815 {
b7826503 816 CHECK_NUMBER (Vx_pointer_shape);
09393d07 817 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
01f1ba30
JB
818 }
819 else
09393d07
GM
820 cursor = XCreateFontCursor (dpy, XC_xterm);
821 x_check_errors (dpy, "bad text pointer cursor: %s");
01f1ba30 822
09393d07 823 if (!NILP (Vx_nontext_pointer_shape))
01f1ba30 824 {
b7826503 825 CHECK_NUMBER (Vx_nontext_pointer_shape);
09393d07
GM
826 nontext_cursor
827 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
01f1ba30
JB
828 }
829 else
09393d07
GM
830 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
831 x_check_errors (dpy, "bad nontext pointer cursor: %s");
01f1ba30 832
09393d07 833 if (!NILP (Vx_hourglass_pointer_shape))
333b20bb 834 {
b7826503 835 CHECK_NUMBER (Vx_hourglass_pointer_shape);
09393d07
GM
836 hourglass_cursor
837 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
333b20bb
GM
838 }
839 else
09393d07
GM
840 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
841 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
177c0ea7 842
09393d07 843 if (!NILP (Vx_mode_pointer_shape))
01f1ba30 844 {
b7826503 845 CHECK_NUMBER (Vx_mode_pointer_shape);
09393d07 846 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
01f1ba30
JB
847 }
848 else
09393d07
GM
849 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
850 x_check_errors (dpy, "bad modeline pointer cursor: %s");
95f80c78 851
09393d07 852 if (!NILP (Vx_sensitive_text_pointer_shape))
95f80c78 853 {
b7826503 854 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
3d970f28 855 hand_cursor
09393d07 856 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
95f80c78
FP
857 }
858 else
3d970f28 859 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
01f1ba30 860
8fb4ec9c
GM
861 if (!NILP (Vx_window_horizontal_drag_shape))
862 {
b7826503 863 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
8fb4ec9c 864 horizontal_drag_cursor
09393d07 865 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
8fb4ec9c
GM
866 }
867 else
868 horizontal_drag_cursor
09393d07 869 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
8fb4ec9c 870
fe24a618 871 /* Check and report errors with the above calls. */
09393d07 872 x_check_errors (dpy, "can't set cursor shape: %s");
4545fa20 873 x_uncatch_errors ();
fe24a618 874
01f1ba30
JB
875 {
876 XColor fore_color, back_color;
877
09393d07 878 fore_color.pixel = x->mouse_pixel;
a31fedb7 879 x_query_color (f, &fore_color);
01f1ba30 880 back_color.pixel = mask_color;
a31fedb7 881 x_query_color (f, &back_color);
177c0ea7 882
09393d07
GM
883 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
884 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
885 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
3d970f28 886 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
09393d07
GM
887 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
888 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
01f1ba30 889 }
01f1ba30 890
fe24a618 891 if (FRAME_X_WINDOW (f) != 0)
e044e4fc
JD
892 XDefineCursor (dpy, FRAME_X_WINDOW (f),
893 f->output_data.x->current_cursor = cursor);
09393d07 894
e044e4fc
JD
895 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor == 0)
896 FRAME_X_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
f5f6c0e0 897
09393d07
GM
898 if (cursor != x->text_cursor
899 && x->text_cursor != 0)
900 XFreeCursor (dpy, x->text_cursor);
901 x->text_cursor = cursor;
902
903 if (nontext_cursor != x->nontext_cursor
904 && x->nontext_cursor != 0)
905 XFreeCursor (dpy, x->nontext_cursor);
906 x->nontext_cursor = nontext_cursor;
907
908 if (hourglass_cursor != x->hourglass_cursor
909 && x->hourglass_cursor != 0)
910 XFreeCursor (dpy, x->hourglass_cursor);
911 x->hourglass_cursor = hourglass_cursor;
912
913 if (mode_cursor != x->modeline_cursor
914 && x->modeline_cursor != 0)
915 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
916 x->modeline_cursor = mode_cursor;
177c0ea7 917
3d970f28
KS
918 if (hand_cursor != x->hand_cursor
919 && x->hand_cursor != 0)
920 XFreeCursor (dpy, x->hand_cursor);
921 x->hand_cursor = hand_cursor;
01f1ba30 922
09393d07
GM
923 if (horizontal_drag_cursor != x->horizontal_drag_cursor
924 && x->horizontal_drag_cursor != 0)
925 XFreeCursor (dpy, x->horizontal_drag_cursor);
926 x->horizontal_drag_cursor = horizontal_drag_cursor;
8fb4ec9c 927
09393d07 928 XFlush (dpy);
4d7e6e51 929 unblock_input ();
05c8abbe
GM
930
931 update_face_from_frame_parameter (f, Qmouse_color, arg);
01f1ba30
JB
932}
933
e4cebfca 934static void
971de7fb 935x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
01f1ba30 936{
a76206dc 937 unsigned long fore_pixel, pixel;
10168ebb 938 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
09393d07 939 struct x_output *x = f->output_data.x;
01f1ba30 940
10168ebb
GM
941 if (!NILP (Vx_cursor_fore_pixel))
942 {
943 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
944 WHITE_PIX_DEFAULT (f));
945 fore_pixel_allocated_p = 1;
946 }
01f1ba30 947 else
ce593f6e 948 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
177c0ea7 949
a76206dc 950 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
10168ebb 951 pixel_allocated_p = 1;
a76206dc 952
f9942c9e 953 /* Make sure that the cursor color differs from the background color. */
ce593f6e 954 if (pixel == FRAME_BACKGROUND_PIXEL (f))
01f1ba30 955 {
10168ebb
GM
956 if (pixel_allocated_p)
957 {
958 x_free_colors (f, &pixel, 1);
959 pixel_allocated_p = 0;
960 }
177c0ea7 961
09393d07 962 pixel = x->mouse_pixel;
a76206dc 963 if (pixel == fore_pixel)
10168ebb
GM
964 {
965 if (fore_pixel_allocated_p)
966 {
967 x_free_colors (f, &fore_pixel, 1);
968 fore_pixel_allocated_p = 0;
969 }
ce593f6e 970 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
10168ebb 971 }
01f1ba30 972 }
a76206dc 973
09393d07 974 unload_color (f, x->cursor_foreground_pixel);
10168ebb
GM
975 if (!fore_pixel_allocated_p)
976 fore_pixel = x_copy_color (f, fore_pixel);
09393d07 977 x->cursor_foreground_pixel = fore_pixel;
01f1ba30 978
09393d07 979 unload_color (f, x->cursor_pixel);
10168ebb
GM
980 if (!pixel_allocated_p)
981 pixel = x_copy_color (f, pixel);
09393d07 982 x->cursor_pixel = pixel;
a76206dc 983
fe24a618 984 if (FRAME_X_WINDOW (f) != 0)
01f1ba30 985 {
4d7e6e51 986 block_input ();
09393d07
GM
987 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
988 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
4d7e6e51 989 unblock_input ();
01f1ba30 990
179956b9 991 if (FRAME_VISIBLE_P (f))
01f1ba30 992 {
cedadcfa
RS
993 x_update_cursor (f, 0);
994 x_update_cursor (f, 1);
01f1ba30
JB
995 }
996 }
05c8abbe
GM
997
998 update_face_from_frame_parameter (f, Qcursor_color, arg);
01f1ba30 999}
943b580d 1000\f
f676886a 1001/* Set the border-color of frame F to pixel value PIX.
01f1ba30 1002 Note that this does not fully take effect if done before
f676886a 1003 F has an x-window. */
01f1ba30 1004
e4cebfca 1005static void
971de7fb 1006x_set_border_pixel (struct frame *f, int pix)
01f1ba30 1007{
a76206dc 1008 unload_color (f, f->output_data.x->border_pixel);
7556890b 1009 f->output_data.x->border_pixel = pix;
01f1ba30 1010
be786000 1011 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
01f1ba30 1012 {
4d7e6e51 1013 block_input ();
578c21e6 1014 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
4d7e6e51 1015 unblock_input ();
01f1ba30 1016
179956b9 1017 if (FRAME_VISIBLE_P (f))
f676886a 1018 redraw_frame (f);
01f1ba30
JB
1019 }
1020}
1021
7c0d3ed8
KS
1022/* Set the border-color of frame F to value described by ARG.
1023 ARG can be a string naming a color.
1024 The border-color is used for the border that is drawn by the X server.
1025 Note that this does not fully take effect if done before
1026 F has an x-window; it must be redone when the window is created.
1027
1028 Note: this is done in two routines because of the way X10 works.
1029
1030 Note: under X11, this is normally the province of the window manager,
1031 and so emacs' border colors may be overridden. */
1032
e4cebfca 1033static void
971de7fb 1034x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
7c0d3ed8
KS
1035{
1036 int pix;
1037
1038 CHECK_STRING (arg);
1039 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1040 x_set_border_pixel (f, pix);
1041 update_face_from_frame_parameter (f, Qborder_color, arg);
1042}
0d1469d6 1043
0d1469d6 1044
e4cebfca 1045static void
971de7fb 1046x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
0d1469d6 1047{
33b2311e 1048 set_frame_cursor_types (f, arg);
dbc4e1c1 1049
75691005
RS
1050 /* Make sure the cursor gets redrawn. */
1051 cursor_type_changed = 1;
dbc4e1c1 1052}
943b580d 1053\f
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
YM
1184 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1185 0, y, width, height, False);
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
YM
1195 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1196 0, y, width, height, False);
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 ();
caacfeb8
JD
1294 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1295 0, y, width, height, False);
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
971de7fb 1445x_set_name_internal (FRAME_PTR 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
971de7fb 1604x_explicitly_set_name (FRAME_PTR 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
971de7fb 1613x_implicitly_set_name (FRAME_PTR 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
1711 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
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
ebd15611 1726hack_wm_protocols (FRAME_PTR 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;
2062 int shared_p = 0;
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
2084 if (FRAME_XIC_BASE_FONTNAME (f))
2085 xfree (FRAME_XIC_BASE_FONTNAME (f));
2086 FRAME_XIC_BASE_FONTNAME (f) = NULL;
2087 FRAME_XIC_FONTSET (f) = NULL;
2088}
2089
5a7df7d7
GM
2090
2091/* Value is the best input style, given user preferences USER (already
2092 checked to be supported by Emacs), and styles supported by the
2093 input method XIM. */
2094
2095static XIMStyle
971de7fb 2096best_xim_style (XIMStyles *user, XIMStyles *xim)
5a7df7d7
GM
2097{
2098 int i, j;
2099
2100 for (i = 0; i < user->count_styles; ++i)
2101 for (j = 0; j < xim->count_styles; ++j)
2102 if (user->supported_styles[i] == xim->supported_styles[j])
2103 return user->supported_styles[i];
2104
2105 /* Return the default style. */
2106 return XIMPreeditNothing | XIMStatusNothing;
2107}
2108
2109/* Create XIC for frame F. */
2110
5df79d3d
GM
2111static XIMStyle xic_style;
2112
5a7df7d7 2113void
971de7fb 2114create_frame_xic (struct frame *f)
5a7df7d7 2115{
5a7df7d7
GM
2116 XIM xim;
2117 XIC xic = NULL;
2118 XFontSet xfs = NULL;
86779fac 2119
5a7df7d7
GM
2120 if (FRAME_XIC (f))
2121 return;
177c0ea7 2122
bb07fa29 2123 /* Create X fontset. */
b51238f5 2124 xfs = xic_create_xfontset (f);
5a7df7d7
GM
2125 xim = FRAME_X_XIM (f);
2126 if (xim)
2127 {
d9d57cb2
DL
2128 XRectangle s_area;
2129 XPoint spot;
5a7df7d7
GM
2130 XVaNestedList preedit_attr;
2131 XVaNestedList status_attr;
5a7df7d7 2132
d9d57cb2
DL
2133 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2134 spot.x = 0; spot.y = 1;
86779fac 2135
5a7df7d7
GM
2136 /* Determine XIC style. */
2137 if (xic_style == 0)
2138 {
2139 XIMStyles supported_list;
2140 supported_list.count_styles = (sizeof supported_xim_styles
2141 / sizeof supported_xim_styles[0]);
2142 supported_list.supported_styles = supported_xim_styles;
2143 xic_style = best_xim_style (&supported_list,
2144 FRAME_X_XIM_STYLES (f));
2145 }
86779fac 2146
5a7df7d7
GM
2147 preedit_attr = XVaCreateNestedList (0,
2148 XNFontSet, xfs,
2149 XNForeground,
2150 FRAME_FOREGROUND_PIXEL (f),
2151 XNBackground,
2152 FRAME_BACKGROUND_PIXEL (f),
2153 (xic_style & XIMPreeditPosition
2154 ? XNSpotLocation
2155 : NULL),
2156 &spot,
2157 NULL);
2158 status_attr = XVaCreateNestedList (0,
2159 XNArea,
2160 &s_area,
2161 XNFontSet,
2162 xfs,
2163 XNForeground,
2164 FRAME_FOREGROUND_PIXEL (f),
2165 XNBackground,
2166 FRAME_BACKGROUND_PIXEL (f),
2167 NULL);
2168
2169 xic = XCreateIC (xim,
2170 XNInputStyle, xic_style,
6b61353c
KH
2171 XNClientWindow, FRAME_X_WINDOW (f),
2172 XNFocusWindow, FRAME_X_WINDOW (f),
5a7df7d7
GM
2173 XNStatusAttributes, status_attr,
2174 XNPreeditAttributes, preedit_attr,
2175 NULL);
2176 XFree (preedit_attr);
2177 XFree (status_attr);
2178 }
177c0ea7 2179
5a7df7d7
GM
2180 FRAME_XIC (f) = xic;
2181 FRAME_XIC_STYLE (f) = xic_style;
2182 FRAME_XIC_FONTSET (f) = xfs;
86779fac
GM
2183}
2184
5a7df7d7
GM
2185
2186/* Destroy XIC and free XIC fontset of frame F, if any. */
2187
2188void
971de7fb 2189free_frame_xic (struct frame *f)
5a7df7d7
GM
2190{
2191 if (FRAME_XIC (f) == NULL)
2192 return;
177c0ea7 2193
5a7df7d7 2194 XDestroyIC (FRAME_XIC (f));
c27ed90a 2195 xic_free_xfontset (f);
5a7df7d7
GM
2196
2197 FRAME_XIC (f) = NULL;
5a7df7d7
GM
2198}
2199
2200
2201/* Place preedit area for XIC of window W's frame to specified
2202 pixel position X/Y. X and Y are relative to window W. */
2203
2204void
971de7fb 2205xic_set_preeditarea (struct window *w, int x, int y)
5a7df7d7 2206{
d3d50620 2207 struct frame *f = XFRAME (w->frame);
5a7df7d7
GM
2208 XVaNestedList attr;
2209 XPoint spot;
177c0ea7 2210
17e6d491 2211 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
5a7df7d7
GM
2212 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2213 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2214 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2215 XFree (attr);
2216}
2217
2218
2219/* Place status area for XIC in bottom right corner of frame F.. */
2220
2221void
971de7fb 2222xic_set_statusarea (struct frame *f)
5a7df7d7
GM
2223{
2224 XIC xic = FRAME_XIC (f);
2225 XVaNestedList attr;
2226 XRectangle area;
2227 XRectangle *needed;
2228
2229 /* Negotiate geometry of status area. If input method has existing
2230 status area, use its current size. */
2231 area.x = area.y = area.width = area.height = 0;
2232 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2233 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2234 XFree (attr);
177c0ea7 2235
5a7df7d7
GM
2236 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2237 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2238 XFree (attr);
2239
2240 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2241 {
2242 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2243 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2244 XFree (attr);
2245 }
2246
2247 area.width = needed->width;
2248 area.height = needed->height;
be786000
KS
2249 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2250 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
488dd4c4 2251 - FRAME_MENUBAR_HEIGHT (f)
bfeabdc3 2252 - FRAME_TOOLBAR_TOP_HEIGHT (f)
488dd4c4 2253 - FRAME_INTERNAL_BORDER_WIDTH (f));
5a7df7d7
GM
2254 XFree (needed);
2255
2256 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
6b61353c 2257 XSetICValues (xic, XNStatusAttributes, attr, NULL);
5a7df7d7
GM
2258 XFree (attr);
2259}
2260
2261
2262/* Set X fontset for XIC of frame F, using base font name
2263 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2264
2265void
eec47d6b 2266xic_set_xfontset (struct frame *f, const char *base_fontname)
5a7df7d7
GM
2267{
2268 XVaNestedList attr;
2269 XFontSet xfs;
2270
c27ed90a
JD
2271 xic_free_xfontset (f);
2272
b51238f5 2273 xfs = xic_create_xfontset (f);
5a7df7d7
GM
2274
2275 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2276 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2277 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2278 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2279 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2280 XFree (attr);
177c0ea7 2281
5a7df7d7
GM
2282 FRAME_XIC_FONTSET (f) = xfs;
2283}
2284
2285#endif /* HAVE_X_I18N */
2286
2287
9ef48a9d 2288\f
8fc2766b
RS
2289#ifdef USE_X_TOOLKIT
2290
2291/* Create and set up the X widget for frame F. */
f58534a3 2292
01f1ba30 2293static void
ebd15611 2294x_window (struct frame *f, long window_prompting, int minibuffer_only)
01f1ba30 2295{
9ef48a9d 2296 XClassHint class_hints;
31ac8d8c
FP
2297 XSetWindowAttributes attributes;
2298 unsigned long attribute_mask;
9ef48a9d
RS
2299 Widget shell_widget;
2300 Widget pane_widget;
6c32dd68 2301 Widget frame_widget;
9ef48a9d
RS
2302 Arg al [25];
2303 int ac;
2304
4d7e6e51 2305 block_input ();
9ef48a9d 2306
b7975ee4
KH
2307 /* Use the resource name as the top-level widget name
2308 for looking up resources. Make a non-Lisp copy
2309 for the window manager, so GC relocation won't bother it.
2310
2311 Elsewhere we specify the window name for the window manager. */
177c0ea7 2312
cca176a0 2313 {
51b59d79 2314 char *str = SSDATA (Vx_resource_name);
23f86fce 2315 f->namebuf = xmalloc (strlen (str) + 1);
cca176a0
KH
2316 strcpy (f->namebuf, str);
2317 }
9ef48a9d
RS
2318
2319 ac = 0;
2320 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2321 XtSetArg (al[ac], XtNinput, 1); ac++;
97787173 2322 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
be786000 2323 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
9b2956e2
GM
2324 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2325 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2326 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
cca176a0 2327 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
7a994728 2328 applicationShellWidgetClass,
82c90203 2329 FRAME_X_DISPLAY (f), al, ac);
9ef48a9d 2330
7556890b 2331 f->output_data.x->widget = shell_widget;
9ef48a9d
RS
2332 /* maybe_set_screen_title_format (shell_widget); */
2333
6c32dd68
PR
2334 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2335 (widget_value *) NULL,
2336 shell_widget, False,
2337 (lw_callback) NULL,
2338 (lw_callback) NULL,
b6e11efd 2339 (lw_callback) NULL,
6c32dd68 2340 (lw_callback) NULL);
9ef48a9d 2341
9b2956e2
GM
2342 ac = 0;
2343 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2344 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2345 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
723f5a07 2346 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
9b2956e2 2347 XtSetValues (pane_widget, al, ac);
7556890b 2348 f->output_data.x->column_widget = pane_widget;
a7f7d550 2349
177c0ea7 2350 /* mappedWhenManaged to false tells to the paned window to not map/unmap
5e65b9ab 2351 the emacs screen when changing menubar. This reduces flickering. */
9ef48a9d
RS
2352
2353 ac = 0;
2354 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2355 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2356 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2357 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2358 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
9b2956e2
GM
2359 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2360 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2361 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
723f5a07 2362 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
9b2956e2
GM
2363 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2364 al, ac);
177c0ea7 2365
7556890b 2366 f->output_data.x->edit_widget = frame_widget;
177c0ea7
JB
2367
2368 XtManageChild (frame_widget);
a7f7d550
FP
2369
2370 /* Do some needed geometry management. */
2371 {
b7163a50 2372 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
8a94ea33
PE
2373 Arg gal[10];
2374 int gac = 0;
5031cc10 2375 int extra_borders = 0;
177c0ea7 2376 int menubar_size
7556890b
RS
2377 = (f->output_data.x->menubar_widget
2378 ? (f->output_data.x->menubar_widget->core.height
2379 + f->output_data.x->menubar_widget->core.border_width)
8fc2766b 2380 : 0);
a7f7d550 2381
f7008aff
RS
2382#if 0 /* Experimentally, we now get the right results
2383 for -geometry -0-0 without this. 24 Aug 96, rms. */
01cbdba5
RS
2384 if (FRAME_EXTERNAL_MENU_BAR (f))
2385 {
dd254b21 2386 Dimension ibw = 0;
01cbdba5
RS
2387 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2388 menubar_size += ibw;
2389 }
f7008aff 2390#endif
01cbdba5 2391
7556890b 2392 f->output_data.x->menubar_height = menubar_size;
00983aba 2393
440b0bfd 2394#ifndef USE_LUCID
5031cc10
KH
2395 /* Motif seems to need this amount added to the sizes
2396 specified for the shell widget. The Athena/Lucid widgets don't.
2397 Both conclusions reached experimentally. -- rms. */
440b0bfd
RS
2398 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2399 &extra_borders, NULL);
2400 extra_borders *= 2;
2401#endif
5031cc10 2402
97787173
RS
2403 /* Convert our geometry parameters into a geometry string
2404 and specify it.
2405 Note that we do not specify here whether the position
2406 is a user-specified or program-specified one.
2407 We pass that information later, in x_wm_set_size_hints. */
2408 {
be786000 2409 int left = f->left_pos;
97787173 2410 int xneg = window_prompting & XNegative;
be786000 2411 int top = f->top_pos;
97787173
RS
2412 int yneg = window_prompting & YNegative;
2413 if (xneg)
2414 left = -left;
2415 if (yneg)
2416 top = -top;
c760f47e
KH
2417
2418 if (window_prompting & USPosition)
5031cc10 2419 sprintf (shell_position, "=%dx%d%c%d%c%d",
be786000
KS
2420 FRAME_PIXEL_WIDTH (f) + extra_borders,
2421 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
c760f47e
KH
2422 (xneg ? '-' : '+'), left,
2423 (yneg ? '-' : '+'), top);
2424 else
6b61353c
KH
2425 {
2426 sprintf (shell_position, "=%dx%d",
2427 FRAME_PIXEL_WIDTH (f) + extra_borders,
2428 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2429
2430 /* Setting x and y when the position is not specified in
2431 the geometry string will set program position in the WM hints.
2432 If Emacs had just one program position, we could set it in
2433 fallback resources, but since each make-frame call can specify
2434 different program positions, this is easier. */
8a94ea33
PE
2435 XtSetArg (gal[gac], XtNx, left); gac++;
2436 XtSetArg (gal[gac], XtNy, top); gac++;
6b61353c 2437 }
97787173
RS
2438 }
2439
77110caa
RS
2440 /* We don't free this because we don't know whether
2441 it is safe to free it while the frame exists.
2442 It isn't worth the trouble of arranging to free it
2443 when the frame is deleted. */
b3b4476b 2444 tem = xstrdup (shell_position);
8a94ea33
PE
2445 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2446 XtSetValues (shell_widget, gal, gac);
a7f7d550
FP
2447 }
2448
9ef48a9d
RS
2449 XtManageChild (pane_widget);
2450 XtRealizeWidget (shell_widget);
2451
2532f20c
JD
2452 if (FRAME_X_EMBEDDED_P (f))
2453 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2454 f->output_data.x->parent_desc, 0, 0);
2455
177c0ea7 2456 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
9ef48a9d
RS
2457
2458 validate_x_resource_name ();
b7975ee4 2459
51b59d79
PE
2460 class_hints.res_name = SSDATA (Vx_resource_name);
2461 class_hints.res_class = SSDATA (Vx_resource_class);
b9dc4443 2462 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
5a7df7d7
GM
2463
2464#ifdef HAVE_X_I18N
2465 FRAME_XIC (f) = NULL;
1576f1ad
DL
2466 if (use_xim)
2467 create_frame_xic (f);
5a7df7d7 2468#endif
64d16748 2469
7556890b
RS
2470 f->output_data.x->wm_hints.input = True;
2471 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 2472 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 2473 &f->output_data.x->wm_hints);
b8228beb 2474
c4ec904f 2475 hack_wm_protocols (f, shell_widget);
9ef48a9d 2476
6c32dd68
PR
2477#ifdef HACK_EDITRES
2478 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2479#endif
2480
9ef48a9d 2481 /* Do a stupid property change to force the server to generate a
333b20bb 2482 PropertyNotify event so that the event_stream server timestamp will
9ef48a9d
RS
2483 be initialized to something relevant to the time we created the window.
2484 */
6c32dd68 2485 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
b9dc4443
RS
2486 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2487 XA_ATOM, 32, PropModeAppend,
9ef48a9d
RS
2488 (unsigned char*) NULL, 0);
2489
5a7df7d7 2490 /* Make all the standard events reach the Emacs frame. */
31ac8d8c 2491 attributes.event_mask = STANDARD_EVENT_SET;
5a7df7d7
GM
2492
2493#ifdef HAVE_X_I18N
2494 if (FRAME_XIC (f))
2495 {
2496 /* XIM server might require some X events. */
2497 unsigned long fevent = NoEventMask;
6b61353c 2498 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
5a7df7d7
GM
2499 attributes.event_mask |= fevent;
2500 }
2501#endif /* HAVE_X_I18N */
177c0ea7 2502
31ac8d8c
FP
2503 attribute_mask = CWEventMask;
2504 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2505 attribute_mask, &attributes);
2506
6c32dd68 2507 XtMapWidget (frame_widget);
9ef48a9d 2508
8fc2766b
RS
2509 /* x_set_name normally ignores requests to set the name if the
2510 requested name is the same as the current name. This is the one
2511 place where that assumption isn't correct; f->name is set, but
2512 the X server hasn't been told. */
2513 {
2514 Lisp_Object name;
2515 int explicit = f->explicit_name;
2516
2517 f->explicit_name = 0;
e69b0960 2518 name = f->name;
f00af5b1 2519 fset_name (f, Qnil);
8fc2766b
RS
2520 x_set_name (f, name, explicit);
2521 }
2522
b9dc4443 2523 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
db3b2dc8
JD
2524 f->output_data.x->current_cursor
2525 = f->output_data.x->text_cursor);
8fc2766b 2526
4d7e6e51 2527 unblock_input ();
8fc2766b 2528
495fa05e
GM
2529 /* This is a no-op, except under Motif. Make sure main areas are
2530 set to something reasonable, in case we get an error later. */
2531 lw_set_main_areas (pane_widget, 0, frame_widget);
8fc2766b
RS
2532}
2533
9ef48a9d 2534#else /* not USE_X_TOOLKIT */
488dd4c4 2535#ifdef USE_GTK
f78faa98 2536static void
971de7fb 2537x_window (FRAME_PTR f)
488dd4c4
JD
2538{
2539 if (! xg_create_frame_widgets (f))
2540 error ("Unable to create window");
1fcfb866
JD
2541
2542#ifdef HAVE_X_I18N
2543 FRAME_XIC (f) = NULL;
6b61353c 2544 if (use_xim)
1576f1ad 2545 {
4d7e6e51 2546 block_input ();
1576f1ad
DL
2547 create_frame_xic (f);
2548 if (FRAME_XIC (f))
2549 {
2550 /* XIM server might require some X events. */
2551 unsigned long fevent = NoEventMask;
6b61353c 2552 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
1fcfb866 2553
1576f1ad
DL
2554 if (fevent != NoEventMask)
2555 {
2556 XSetWindowAttributes attributes;
2557 XWindowAttributes wattr;
2558 unsigned long attribute_mask;
2559
2560 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2561 &wattr);
2562 attributes.event_mask = wattr.your_event_mask | fevent;
2563 attribute_mask = CWEventMask;
2564 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2565 attribute_mask, &attributes);
2566 }
2567 }
4d7e6e51 2568 unblock_input ();
1576f1ad 2569 }
1fcfb866 2570#endif
488dd4c4 2571}
9ef48a9d 2572
488dd4c4 2573#else /*! USE_GTK */
8fc2766b
RS
2574/* Create and set up the X window for frame F. */
2575
f78faa98 2576static void
0521f580 2577x_window (struct frame *f)
8fc2766b
RS
2578{
2579 XClassHint class_hints;
2580 XSetWindowAttributes attributes;
2581 unsigned long attribute_mask;
2582
5bcee7ef 2583 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
7556890b 2584 attributes.border_pixel = f->output_data.x->border_pixel;
01f1ba30
JB
2585 attributes.bit_gravity = StaticGravity;
2586 attributes.backing_store = NotUseful;
2587 attributes.save_under = True;
2588 attributes.event_mask = STANDARD_EVENT_SET;
9b2956e2
GM
2589 attributes.colormap = FRAME_X_COLORMAP (f);
2590 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2591 | CWColormap);
01f1ba30 2592
4d7e6e51 2593 block_input ();
fe24a618 2594 FRAME_X_WINDOW (f)
b9dc4443 2595 = XCreateWindow (FRAME_X_DISPLAY (f),
7556890b 2596 f->output_data.x->parent_desc,
be786000
KS
2597 f->left_pos,
2598 f->top_pos,
2599 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2600 f->border_width,
01f1ba30
JB
2601 CopyFromParent, /* depth */
2602 InputOutput, /* class */
383d6ffc 2603 FRAME_X_VISUAL (f),
01f1ba30 2604 attribute_mask, &attributes);
5a7df7d7
GM
2605
2606#ifdef HAVE_X_I18N
4dacadcc 2607 if (use_xim)
5a7df7d7 2608 {
1576f1ad
DL
2609 create_frame_xic (f);
2610 if (FRAME_XIC (f))
2611 {
2612 /* XIM server might require some X events. */
2613 unsigned long fevent = NoEventMask;
6b61353c 2614 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
1576f1ad
DL
2615 attributes.event_mask |= fevent;
2616 attribute_mask = CWEventMask;
2617 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2618 attribute_mask, &attributes);
2619 }
5a7df7d7
GM
2620 }
2621#endif /* HAVE_X_I18N */
177c0ea7 2622
d387c960 2623 validate_x_resource_name ();
b7975ee4 2624
51b59d79
PE
2625 class_hints.res_name = SSDATA (Vx_resource_name);
2626 class_hints.res_class = SSDATA (Vx_resource_class);
b9dc4443 2627 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
01f1ba30 2628
00983aba
KH
2629 /* The menubar is part of the ordinary display;
2630 it does not count in addition to the height of the window. */
7556890b 2631 f->output_data.x->menubar_height = 0;
00983aba 2632
179956b9
JB
2633 /* This indicates that we use the "Passive Input" input model.
2634 Unless we do this, we don't get the Focus{In,Out} events that we
2635 need to draw the cursor correctly. Accursed bureaucrats.
b9dc4443 2636 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
179956b9 2637
7556890b
RS
2638 f->output_data.x->wm_hints.input = True;
2639 f->output_data.x->wm_hints.flags |= InputHint;
b9dc4443 2640 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7556890b 2641 &f->output_data.x->wm_hints);
6d078211 2642 f->output_data.x->wm_hints.icon_pixmap = None;
179956b9 2643
032e4ebe
RS
2644 /* Request "save yourself" and "delete window" commands from wm. */
2645 {
2646 Atom protocols[2];
b9dc4443
RS
2647 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2648 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2649 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
032e4ebe 2650 }
9ef48a9d 2651
e373f201
JB
2652 /* x_set_name normally ignores requests to set the name if the
2653 requested name is the same as the current name. This is the one
2654 place where that assumption isn't correct; f->name is set, but
2655 the X server hasn't been told. */
2656 {
98381190 2657 Lisp_Object name;
cf177271 2658 int explicit = f->explicit_name;
e373f201 2659
cf177271 2660 f->explicit_name = 0;
e69b0960 2661 name = f->name;
f00af5b1 2662 fset_name (f, Qnil);
cf177271 2663 x_set_name (f, name, explicit);
e373f201
JB
2664 }
2665
b9dc4443 2666 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
72ec0c8d
JD
2667 f->output_data.x->current_cursor
2668 = f->output_data.x->text_cursor);
9ef48a9d 2669
4d7e6e51 2670 unblock_input ();
01f1ba30 2671
fe24a618 2672 if (FRAME_X_WINDOW (f) == 0)
9ef48a9d 2673 error ("Unable to create window");
01f1ba30
JB
2674}
2675
488dd4c4 2676#endif /* not USE_GTK */
8fc2766b
RS
2677#endif /* not USE_X_TOOLKIT */
2678
289978b5
LT
2679/* Verify that the icon position args for this window are valid. */
2680
2681static void
971de7fb 2682x_icon_verify (struct frame *f, Lisp_Object parms)
289978b5
LT
2683{
2684 Lisp_Object icon_x, icon_y;
2685
2686 /* Set the position of the icon. Note that twm groups all
2687 icons in an icon window. */
2688 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2689 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2690 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2691 {
2692 CHECK_NUMBER (icon_x);
2693 CHECK_NUMBER (icon_y);
2694 }
2695 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2696 error ("Both left and top icon corners of icon must be specified");
2697}
2698
01f1ba30
JB
2699/* Handle the icon stuff for this window. Perhaps later we might
2700 want an x_set_icon_position which can be called interactively as
b9dc4443 2701 well. */
01f1ba30
JB
2702
2703static void
971de7fb 2704x_icon (struct frame *f, Lisp_Object parms)
01f1ba30 2705{
f9942c9e 2706 Lisp_Object icon_x, icon_y;
fd0f53a9 2707#if 0
abb4b7ec 2708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
fd0f53a9 2709#endif
01f1ba30
JB
2710
2711 /* Set the position of the icon. Note that twm groups all
b9dc4443 2712 icons in an icon window. */
7c0d3ed8
KS
2713 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2714 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
f9942c9e 2715 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
01f1ba30 2716 {
d311d28c
PE
2717 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2718 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
01f1ba30 2719 }
f9942c9e 2720 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
01f1ba30 2721 error ("Both left and top icon corners of icon must be specified");
01f1ba30 2722
4d7e6e51 2723 block_input ();
f9942c9e 2724
fe24a618
JB
2725 if (! EQ (icon_x, Qunbound))
2726 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
f9942c9e 2727
a9d54793
KL
2728#if 0 /* x_get_arg removes the visibility parameter as a side effect,
2729 but x_create_frame still needs it. */
01f1ba30 2730 /* Start up iconic or window? */
49795535 2731 x_wm_set_window_state
333b20bb
GM
2732 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2733 Qicon)
49795535
JB
2734 ? IconicState
2735 : NormalState));
a9d54793 2736#endif
01f1ba30 2737
e69b0960
DA
2738 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2739 ? f->icon_name
2740 : f->name)));
80534dd6 2741
4d7e6e51 2742 unblock_input ();
01f1ba30
JB
2743}
2744
b243755a 2745/* Make the GCs needed for this window, setting the
01f1ba30
JB
2746 background, border and mouse colors; also create the
2747 mouse cursor and the gray border tile. */
2748
2749static void
971de7fb 2750x_make_gc (struct frame *f)
01f1ba30
JB
2751{
2752 XGCValues gc_values;
01f1ba30 2753
4d7e6e51 2754 block_input ();
6afb1d07 2755
b243755a 2756 /* Create the GCs of this frame.
9ef48a9d 2757 Note that many default values are used. */
01f1ba30 2758
ce593f6e
KL
2759 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2760 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
9ef48a9d 2761 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
959e647d
GM
2762 f->output_data.x->normal_gc
2763 = XCreateGC (FRAME_X_DISPLAY (f),
2764 FRAME_X_WINDOW (f),
b51238f5 2765 GCLineWidth | GCForeground | GCBackground,
959e647d 2766 &gc_values);
01f1ba30 2767
b9dc4443 2768 /* Reverse video style. */
ce593f6e
KL
2769 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2770 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
959e647d
GM
2771 f->output_data.x->reverse_gc
2772 = XCreateGC (FRAME_X_DISPLAY (f),
2773 FRAME_X_WINDOW (f),
b51238f5 2774 GCForeground | GCBackground | GCLineWidth,
959e647d 2775 &gc_values);
01f1ba30 2776
9ef48a9d 2777 /* Cursor has cursor-color background, background-color foreground. */
ce593f6e 2778 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
7556890b 2779 gc_values.background = f->output_data.x->cursor_pixel;
01f1ba30 2780 gc_values.fill_style = FillOpaqueStippled;
7556890b 2781 f->output_data.x->cursor_gc
b9dc4443 2782 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
b51238f5 2783 (GCForeground | GCBackground
c8bcd18a 2784 | GCFillStyle | GCLineWidth),
01f1ba30
JB
2785 &gc_values);
2786
333b20bb
GM
2787 /* Reliefs. */
2788 f->output_data.x->white_relief.gc = 0;
2789 f->output_data.x->black_relief.gc = 0;
2790
01f1ba30 2791 /* Create the gray border tile used when the pointer is not in
f676886a 2792 the frame. Since this depends on the frame's pixel values,
9ef48a9d 2793 this must be done on a per-frame basis. */
7556890b 2794 f->output_data.x->border_tile
d043f1a4 2795 = (XCreatePixmapFromBitmapData
177c0ea7 2796 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
d043f1a4 2797 gray_bits, gray_width, gray_height,
ce593f6e
KL
2798 FRAME_FOREGROUND_PIXEL (f),
2799 FRAME_BACKGROUND_PIXEL (f),
ab452f99 2800 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
6afb1d07 2801
4d7e6e51 2802 unblock_input ();
01f1ba30 2803}
01f1ba30 2804
959e647d 2805
2b34df4e 2806/* Free what was allocated in x_make_gc. */
959e647d
GM
2807
2808void
971de7fb 2809x_free_gcs (struct frame *f)
959e647d
GM
2810{
2811 Display *dpy = FRAME_X_DISPLAY (f);
2812
4d7e6e51 2813 block_input ();
177c0ea7 2814
959e647d
GM
2815 if (f->output_data.x->normal_gc)
2816 {
2817 XFreeGC (dpy, f->output_data.x->normal_gc);
2818 f->output_data.x->normal_gc = 0;
2819 }
2820
2821 if (f->output_data.x->reverse_gc)
2822 {
2823 XFreeGC (dpy, f->output_data.x->reverse_gc);
2824 f->output_data.x->reverse_gc = 0;
2825 }
177c0ea7 2826
959e647d
GM
2827 if (f->output_data.x->cursor_gc)
2828 {
2829 XFreeGC (dpy, f->output_data.x->cursor_gc);
2830 f->output_data.x->cursor_gc = 0;
2831 }
2832
2833 if (f->output_data.x->border_tile)
2834 {
2835 XFreePixmap (dpy, f->output_data.x->border_tile);
2836 f->output_data.x->border_tile = 0;
2837 }
2838
4d7e6e51 2839 unblock_input ();
959e647d
GM
2840}
2841
2842
eaf1eea9 2843/* Handler for signals raised during x_create_frame and
c9e7db78 2844 x_create_tip_frame. FRAME is the frame which is partially
eaf1eea9
GM
2845 constructed. */
2846
2847static Lisp_Object
971de7fb 2848unwind_create_frame (Lisp_Object frame)
eaf1eea9
GM
2849{
2850 struct frame *f = XFRAME (frame);
2851
8346e08b
KL
2852 /* If frame is already dead, nothing to do. This can happen if the
2853 display is disconnected after the frame has become official, but
2854 before x_create_frame removes the unwind protect. */
2855 if (!FRAME_LIVE_P (f))
2856 return Qnil;
2857
eaf1eea9 2858 /* If frame is ``official'', nothing to do. */
97f18cc8 2859 if (NILP (Fmemq (frame, Vframe_list)))
eaf1eea9 2860 {
e509cfa6 2861#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
eaf1eea9
GM
2862 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2863#endif
177c0ea7 2864
eaf1eea9 2865 x_free_frame_resources (f);
2a58bbc1 2866 free_glyphs (f);
eaf1eea9 2867
e509cfa6 2868#if defined GLYPH_DEBUG && defined ENABLE_CHECKING
eaf1eea9 2869 /* Check that reference counts are indeed correct. */
a54e2c05
DA
2870 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2871 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
5b3f8550 2872#endif
c844a81a 2873 return Qt;
eaf1eea9 2874 }
177c0ea7 2875
eaf1eea9
GM
2876 return Qnil;
2877}
2878
f01d3321
CY
2879static Lisp_Object
2880unwind_create_frame_1 (Lisp_Object val)
2881{
d1f55f16 2882 inhibit_lisp_code = val;
f01d3321
CY
2883 return Qnil;
2884}
b51238f5 2885
02ed2ea8 2886static void
971de7fb 2887x_default_font_parameter (struct frame *f, Lisp_Object parms)
02ed2ea8
KH
2888{
2889 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
770e2e6e 2890 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
637fa988 2891 RES_TYPE_STRING);
e9b7ab96 2892 Lisp_Object font = Qnil;
770e2e6e 2893 if (EQ (font_param, Qunbound))
0d1d0d26
JD
2894 font_param = Qnil;
2895
2896 if (NILP (font_param))
637fa988 2897 {
e1dbe924 2898 /* System font should take precedence over X resources. We suggest this
0d1d0d26
JD
2899 regardless of font-use-system-font because .emacs may not have been
2900 read yet. */
2901 const char *system_font = xsettings_get_system_font ();
e9b7ab96 2902 if (system_font)
d7ea76b4 2903 font = font_open_by_name (f, build_unibyte_string (system_font));
637fa988 2904 }
0d1d0d26 2905
e9b7ab96
JD
2906 if (NILP (font))
2907 font = !NILP (font_param) ? font_param
2908 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2909
2910 if (! FONTP (font) && ! STRINGP (font))
02ed2ea8 2911 {
675e2c69 2912 const char *names[]
be9d013a
CY
2913 = {
2914#ifdef HAVE_XFT
d7afccca 2915 /* This will find the normal Xft font. */
c024ac08 2916 "monospace-10",
be9d013a
CY
2917#endif
2918 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2919 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
02ed2ea8
KH
2920 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2921 /* This was formerly the first thing tried, but it finds
2922 too many fonts and takes too long. */
2923 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2924 /* If those didn't work, look for something which will
2925 at least work. */
2926 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
25c25256 2927 "fixed",
02ed2ea8
KH
2928 NULL };
2929 int i;
2930
2931 for (i = 0; names[i]; i++)
2932 {
d7ea76b4 2933 font = font_open_by_name (f, build_unibyte_string (names[i]));
02ed2ea8
KH
2934 if (! NILP (font))
2935 break;
2936 }
2937 if (NILP (font))
25c25256 2938 error ("No suitable font was found");
02ed2ea8 2939 }
770e2e6e 2940 else if (!NILP (font_param))
27129af9
SM
2941 {
2942 /* Remember the explicit font parameter, so we can re-apply it after
2943 we've applied the `default' face settings. */
770e2e6e
SM
2944 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
2945 }
637fa988 2946
5fc8e5bc
J
2947 /* This call will make X resources override any system font setting. */
2948 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
02ed2ea8 2949}
b51238f5 2950
eaf1eea9 2951
1c44e124
CY
2952DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2953 0, 1, 0,
2954 doc: /* Send the size hints for frame FRAME to the window manager.
5890e400
DA
2955If FRAME is omitted or nil, use the selected frame.
2956Signal error if FRAME is not an X frame. */)
5842a27b 2957 (Lisp_Object frame)
1c44e124 2958{
7452b7bd 2959 struct frame *f = decode_window_system_frame (frame);
5890e400 2960
4d7e6e51 2961 block_input ();
5890e400 2962 x_wm_set_size_hint (f, 0, 0);
4d7e6e51 2963 unblock_input ();
653a3150 2964 return Qnil;
1c44e124
CY
2965}
2966
3e6bec3b
JD
2967static void
2968set_machine_and_pid_properties (struct frame *f)
2969{
ac61e7e1 2970 long pid = (long) getpid ();
3e6bec3b 2971
66b16767
J
2972 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2973 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2974 NULL, 0, NULL, NULL, NULL);
3e6bec3b
JD
2975 XChangeProperty (FRAME_X_DISPLAY (f),
2976 FRAME_OUTER_WINDOW (f),
2977 XInternAtom (FRAME_X_DISPLAY (f),
2978 "_NET_WM_PID",
2979 False),
2980 XA_CARDINAL, 32, PropModeReplace,
2981 (unsigned char *) &pid, 1);
2982}
2983
f676886a 2984DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
01f1ba30 2985 1, 1, 0,
7ee72033 2986 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
d29a9848 2987Return an Emacs frame object.
e0079d39 2988PARMS is an alist of frame parameters.
c061c855
GM
2989If the parameters specify that the frame should not have a minibuffer,
2990and do not specify a specific minibuffer window to use,
2991then `default-minibuffer-frame' must be a frame whose minibuffer can
2992be shared by the new frame.
2993
7ee72033 2994This function is an internal primitive--use `make-frame' instead. */)
5842a27b 2995 (Lisp_Object parms)
01f1ba30 2996{
f676886a 2997 struct frame *f;
2365c027 2998 Lisp_Object frame, tem;
01f1ba30
JB
2999 Lisp_Object name;
3000 int minibuffer_only = 0;
3001 long window_prompting = 0;
45d45af5 3002 int width, height;
d311d28c 3003 ptrdiff_t count = SPECPDL_INDEX ();
ecaca587 3004 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
b9dc4443 3005 Lisp_Object display;
333b20bb 3006 struct x_display_info *dpyinfo = NULL;
a59e4f3d 3007 Lisp_Object parent;
e557f19d 3008 struct kboard *kb;
01f1ba30 3009
5fa98fcf
RS
3010 parms = Fcopy_alist (parms);
3011
b7975ee4
KH
3012 /* Use this general default value to start with
3013 until we know if this frame has a specified name. */
3014 Vx_resource_name = Vinvocation_name;
3015
6ed8eeff 3016 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
1705c933 3017 if (EQ (display, Qunbound))
b6660415 3018 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
b9dc4443
RS
3019 if (EQ (display, Qunbound))
3020 display = Qnil;
3021 dpyinfo = check_x_display_info (display);
6ed8eeff 3022 kb = dpyinfo->terminal->kboard;
b9dc4443 3023
89acb56d
SM
3024 if (!dpyinfo->terminal->name)
3025 error ("Terminal is not live, can't create new frames on it");
ab797f65 3026
333b20bb 3027 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
6a5e54e2 3028 if (!STRINGP (name)
cf177271
JB
3029 && ! EQ (name, Qunbound)
3030 && ! NILP (name))
08a90d6a 3031 error ("Invalid frame name--not a string or nil");
01f1ba30 3032
b7975ee4
KH
3033 if (STRINGP (name))
3034 Vx_resource_name = name;
3035
a59e4f3d 3036 /* See if parent window is specified. */
333b20bb 3037 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
a59e4f3d
RS
3038 if (EQ (parent, Qunbound))
3039 parent = Qnil;
3040 if (! NILP (parent))
b7826503 3041 CHECK_NUMBER (parent);
a59e4f3d 3042
ecaca587
RS
3043 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3044 /* No need to protect DISPLAY because that's not used after passing
3045 it to make_frame_without_minibuffer. */
3046 frame = Qnil;
3047 GCPRO4 (parms, parent, name, frame);
333b20bb
GM
3048 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3049 RES_TYPE_SYMBOL);
f9942c9e 3050 if (EQ (tem, Qnone) || NILP (tem))
2526c290 3051 f = make_frame_without_minibuffer (Qnil, kb, display);
f9942c9e 3052 else if (EQ (tem, Qonly))
01f1ba30 3053 {
f676886a 3054 f = make_minibuffer_frame ();
01f1ba30
JB
3055 minibuffer_only = 1;
3056 }
6a5e54e2 3057 else if (WINDOWP (tem))
2526c290 3058 f = make_frame_without_minibuffer (tem, kb, display);
f9942c9e
JB
3059 else
3060 f = make_frame (1);
01f1ba30 3061
ecaca587
RS
3062 XSETFRAME (frame, f);
3063
6ed8eeff 3064 f->terminal = dpyinfo->terminal;
428a555e 3065
08a90d6a 3066 f->output_method = output_x_window;
38182d90 3067 f->output_data.x = xzalloc (sizeof *f->output_data.x);
7556890b 3068 f->output_data.x->icon_bitmap = -1;
be786000 3069 FRAME_FONTSET (f) = -1;
333b20bb
GM
3070 f->output_data.x->scroll_bar_foreground_pixel = -1;
3071 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
3072#ifdef USE_TOOLKIT_SCROLL_BARS
3073 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3074 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3075#endif /* USE_TOOLKIT_SCROLL_BARS */
08a90d6a 3076
f00af5b1
PE
3077 fset_icon_name (f,
3078 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3079 RES_TYPE_STRING));
e69b0960 3080 if (! STRINGP (f->icon_name))
f00af5b1 3081 fset_icon_name (f, Qnil);
80534dd6 3082
08a90d6a 3083 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
e3d56613
RS
3084
3085 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
3086 record_unwind_protect (unwind_create_frame, frame);
08a90d6a 3087
9b2956e2
GM
3088 /* These colors will be set anyway later, but it's important
3089 to get the color reference counts right, so initialize them! */
3090 {
3091 Lisp_Object black;
dbf31225 3092 struct gcpro gcpro1;
cefecbcf
GM
3093
3094 /* Function x_decode_color can signal an error. Make
3095 sure to initialize color slots so that we won't try
3096 to free colors we haven't allocated. */
ce593f6e
KL
3097 FRAME_FOREGROUND_PIXEL (f) = -1;
3098 FRAME_BACKGROUND_PIXEL (f) = -1;
cefecbcf
GM
3099 f->output_data.x->cursor_pixel = -1;
3100 f->output_data.x->cursor_foreground_pixel = -1;
3101 f->output_data.x->border_pixel = -1;
3102 f->output_data.x->mouse_pixel = -1;
177c0ea7 3103
9b2956e2 3104 black = build_string ("black");
dbf31225 3105 GCPRO1 (black);
ce593f6e 3106 FRAME_FOREGROUND_PIXEL (f)
9b2956e2 3107 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
ce593f6e 3108 FRAME_BACKGROUND_PIXEL (f)
9b2956e2
GM
3109 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3110 f->output_data.x->cursor_pixel
3111 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3112 f->output_data.x->cursor_foreground_pixel
3113 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3114 f->output_data.x->border_pixel
3115 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3116 f->output_data.x->mouse_pixel
3117 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
dbf31225 3118 UNGCPRO;
9b2956e2
GM
3119 }
3120
a59e4f3d
RS
3121 /* Specify the parent under which to make this X window. */
3122
3123 if (!NILP (parent))
3124 {
8c239ac3 3125 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
7556890b 3126 f->output_data.x->explicit_parent = 1;
a59e4f3d
RS
3127 }
3128 else
3129 {
7556890b
RS
3130 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3131 f->output_data.x->explicit_parent = 0;
a59e4f3d
RS
3132 }
3133
cf177271
JB
3134 /* Set the name; the functions to which we pass f expect the name to
3135 be set. */
3136 if (EQ (name, Qunbound) || NILP (name))
3137 {
f00af5b1 3138 fset_name (f, build_string (dpyinfo->x_id_name));
cf177271
JB
3139 f->explicit_name = 0;
3140 }
3141 else
3142 {
f00af5b1 3143 fset_name (f, name);
cf177271 3144 f->explicit_name = 1;
9ef48a9d
RS
3145 /* use the frame's title when getting resources for this frame. */
3146 specbind (Qx_resource_name, name);
cf177271 3147 }
01f1ba30 3148
02ed2ea8
KH
3149#ifdef HAVE_FREETYPE
3150#ifdef HAVE_XFT
b51238f5 3151 register_font_driver (&xftfont_driver, f);
02ed2ea8 3152#else /* not HAVE_XFT */
b51238f5 3153 register_font_driver (&ftxfont_driver, f);
02ed2ea8
KH
3154#endif /* not HAVE_XFT */
3155#endif /* HAVE_FREETYPE */
6332668d 3156 register_font_driver (&xfont_driver, f);
297cc20a 3157
b51238f5
KH
3158 x_default_parameter (f, parms, Qfont_backend, Qnil,
3159 "fontBackend", "FontBackend", RES_TYPE_STRING);
02ed2ea8 3160
01f1ba30
JB
3161 /* Extract the window parameters from the supplied values
3162 that are needed to determine window geometry. */
b51238f5 3163 x_default_font_parameter (f, parms);
62d168d8
CY
3164 if (!FRAME_FONT (f))
3165 {
3166 delete_frame (frame, Qnoelisp);
3167 error ("Invalid frame font");
3168 }
9ef48a9d 3169
2532f20c
JD
3170 /* Frame contents get displaced if an embedded X window has a border. */
3171 if (! FRAME_X_EMBEDDED_P (f))
4bef8d26 3172 x_default_parameter (f, parms, Qborder_width, make_number (0),
2532f20c 3173 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
177c0ea7 3174
4e397688 3175 /* This defaults to 1 in order to match xterm. We recognize either
ddf768c3
JB
3176 internalBorderWidth or internalBorder (which is what xterm calls
3177 it). */
3178 if (NILP (Fassq (Qinternal_border_width, parms)))
3179 {
3180 Lisp_Object value;
3181
abb4b7ec 3182 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
333b20bb 3183 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
ddf768c3
JB
3184 if (! EQ (value, Qunbound))
3185 parms = Fcons (Fcons (Qinternal_border_width, value),
3186 parms);
3187 }
a099c27a
SM
3188 x_default_parameter (f, parms, Qinternal_border_width,
3189#ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3190 make_number (0),
3191#else
3192 make_number (1),
3193#endif
333b20bb
GM
3194 "internalBorderWidth", "internalBorderWidth",
3195 RES_TYPE_NUMBER);
303500aa 3196 x_default_parameter (f, parms, Qvertical_scroll_bars,
5e617bc2 3197#if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
303500aa
CY
3198 Qright,
3199#else
3200 Qleft,
3201#endif
333b20bb
GM
3202 "verticalScrollBars", "ScrollBars",
3203 RES_TYPE_SYMBOL);
01f1ba30 3204
b9dc4443 3205 /* Also do the stuff which must be set before the window exists. */
cf177271 3206 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
333b20bb 3207 "foreground", "Foreground", RES_TYPE_STRING);
cf177271 3208 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
0b60fc91 3209 "background", "Background", RES_TYPE_STRING);
cf177271 3210 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
333b20bb 3211 "pointerColor", "Foreground", RES_TYPE_STRING);
cf177271 3212 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
333b20bb 3213 "borderColor", "BorderColor", RES_TYPE_STRING);
d62c8769
GM
3214 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3215 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
563b67aa
GM
3216 x_default_parameter (f, parms, Qline_spacing, Qnil,
3217 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
b3ba0aa8
KS
3218 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3219 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3220 x_default_parameter (f, parms, Qright_fringe, Qnil,
3221 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
333b20bb
GM
3222
3223 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3224 "scrollBarForeground",
3225 "ScrollBarForeground", 1);
3226 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3227 "scrollBarBackground",
3228 "ScrollBarBackground", 0);
3229
e509cfa6 3230#ifdef GLYPH_DEBUG
c9e7db78
JD
3231 image_cache_refcount =
3232 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3233 dpyinfo_refcount = dpyinfo->reference_count;
3234#endif /* GLYPH_DEBUG */
3235
333b20bb
GM
3236 /* Init faces before x_default_parameter is called for scroll-bar
3237 parameters because that function calls x_set_scroll_bar_width,
3238 which calls change_frame_size, which calls Fset_window_buffer,
3239 which runs hooks, which call Fvertical_motion. At the end, we
3240 end up in init_iterator with a null face cache, which should not
3241 happen. */
3242 init_frame_faces (f);
177c0ea7 3243
f01d3321
CY
3244 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3245 look up the X resources controlling the menu-bar and tool-bar
3246 here; they are processed specially at startup, and reflected in
3247 the values of the mode variables.
3248
3249 Avoid calling window-configuration-change-hook; otherwise we
3250 could get an infloop in next_frame since the frame is not yet in
3251 Vframe_list. */
3252 {
a89654f8 3253 ptrdiff_t count2 = SPECPDL_INDEX ();
d1f55f16
CY
3254 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3255 inhibit_lisp_code = Qt;
f01d3321
CY
3256
3257 x_default_parameter (f, parms, Qmenu_bar_lines,
3258 NILP (Vmenu_bar_mode)
3259 ? make_number (0) : make_number (1),
3260 NULL, NULL, RES_TYPE_NUMBER);
3261 x_default_parameter (f, parms, Qtool_bar_lines,
3262 NILP (Vtool_bar_mode)
3263 ? make_number (0) : make_number (1),
3264 NULL, NULL, RES_TYPE_NUMBER);
3265
3266 unbind_to (count2, Qnil);
3267 }
6431f2e6 3268
79873d50 3269 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
333b20bb
GM
3270 "bufferPredicate", "BufferPredicate",
3271 RES_TYPE_SYMBOL);
c2304e02 3272 x_default_parameter (f, parms, Qtitle, Qnil,
333b20bb 3273 "title", "Title", RES_TYPE_STRING);
ea0a1f53
GM
3274 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3275 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
49d41073
EZ
3276 x_default_parameter (f, parms, Qfullscreen, Qnil,
3277 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
2b4e6277 3278 x_default_parameter (f, parms, Qtool_bar_position,
e69b0960 3279 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
90eb1019 3280
35f59f6b 3281 /* Compute the size of the X window. */
7c0d3ed8 3282 window_prompting = x_figure_window_size (f, parms, 1);
38d22040 3283
495fa05e
GM
3284 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3285 f->no_split = minibuffer_only || EQ (tem, Qt);
3286
289978b5
LT
3287 x_icon_verify (f, parms);
3288
6a1bcd01 3289 /* Create the X widget or window. */
a7f7d550
FP
3290#ifdef USE_X_TOOLKIT
3291 x_window (f, window_prompting, minibuffer_only);
3292#else
f676886a 3293 x_window (f);
a7f7d550 3294#endif
177c0ea7 3295
f676886a
JB
3296 x_icon (f, parms);
3297 x_make_gc (f);
01f1ba30 3298
495fa05e 3299 /* Now consider the frame official. */
c9e7db78 3300 f->terminal->reference_count++;
495fa05e
GM
3301 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3302 Vframe_list = Fcons (frame, Vframe_list);
3303
f9942c9e
JB
3304 /* We need to do this after creating the X window, so that the
3305 icon-creation functions can say whose icon they're describing. */
84f25880 3306 x_default_parameter (f, parms, Qicon_type, Qt,
33ed493b 3307 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
f9942c9e 3308
cf177271 3309 x_default_parameter (f, parms, Qauto_raise, Qnil,
333b20bb 3310 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
cf177271 3311 x_default_parameter (f, parms, Qauto_lower, Qnil,
333b20bb 3312 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
dbc4e1c1 3313 x_default_parameter (f, parms, Qcursor_type, Qbox,
333b20bb 3314 "cursorType", "CursorType", RES_TYPE_SYMBOL);
28d7281d
GM
3315 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3316 "scrollBarWidth", "ScrollBarWidth",
3317 RES_TYPE_NUMBER);
271a71c7
GM
3318 x_default_parameter (f, parms, Qalpha, Qnil,
3319 "alpha", "Alpha", RES_TYPE_NUMBER);
f9942c9e 3320
be786000 3321 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
01f1ba30 3322 Change will not be effected unless different from the current
be786000
KS
3323 FRAME_LINES (f). */
3324 width = FRAME_COLS (f);
3325 height = FRAME_LINES (f);
177c0ea7 3326
be786000
KS
3327 SET_FRAME_COLS (f, 0);
3328 FRAME_LINES (f) = 0;
8938a4fb 3329 change_frame_size (f, height, width, 1, 0, 0);
d043f1a4 3330
488dd4c4 3331#if defined (USE_X_TOOLKIT) || defined (USE_GTK)
495fa05e
GM
3332 /* Create the menu bar. */
3333 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3334 {
3335 /* If this signals an error, we haven't set size hints for the
3336 frame and we didn't make it visible. */
3337 initialize_frame_menubar (f);
3338
488dd4c4 3339#ifndef USE_GTK
495fa05e
GM
3340 /* This is a no-op, except under Motif where it arranges the
3341 main window for the widgets on it. */
3342 lw_set_main_areas (f->output_data.x->column_widget,
3343 f->output_data.x->menubar_widget,
3344 f->output_data.x->edit_widget);
488dd4c4 3345#endif /* not USE_GTK */
495fa05e 3346 }
488dd4c4 3347#endif /* USE_X_TOOLKIT || USE_GTK */
495fa05e
GM
3348
3349 /* Tell the server what size and position, etc, we want, and how
3350 badly we want them. This should be done after we have the menu
3351 bar so that its size can be taken into account. */
4d7e6e51 3352 block_input ();
7989f084 3353 x_wm_set_size_hint (f, window_prompting, 0);
4d7e6e51 3354 unblock_input ();
01f1ba30 3355
495fa05e
GM
3356 /* Make the window appear on the frame and enable display, unless
3357 the caller says not to. However, with explicit parent, Emacs
3358 cannot control visibility, so don't try. */
7556890b 3359 if (! f->output_data.x->explicit_parent)
a59e4f3d
RS
3360 {
3361 Lisp_Object visibility;
49795535 3362
333b20bb
GM
3363 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3364 RES_TYPE_SYMBOL);
a59e4f3d
RS
3365 if (EQ (visibility, Qunbound))
3366 visibility = Qt;
49795535 3367
a59e4f3d
RS
3368 if (EQ (visibility, Qicon))
3369 x_iconify_frame (f);
3370 else if (! NILP (visibility))
3371 x_make_frame_visible (f);
3372 else
6b437900
PE
3373 {
3374 /* Must have been Qnil. */
3375 }
a59e4f3d 3376 }
01f1ba30 3377
4d7e6e51 3378 block_input ();
717c30e0 3379
3e6bec3b 3380 /* Set machine name and pid for the purpose of window managers. */
5e617bc2 3381 set_machine_and_pid_properties (f);
3e6bec3b 3382
6b61353c
KH
3383 /* Set the WM leader property. GTK does this itself, so this is not
3384 needed when using GTK. */
3385 if (dpyinfo->client_leader_window != 0)
3386 {
6b61353c
KH
3387 XChangeProperty (FRAME_X_DISPLAY (f),
3388 FRAME_OUTER_WINDOW (f),
3389 dpyinfo->Xatom_wm_client_leader,
3390 XA_WINDOW, 32, PropModeReplace,
47c53789 3391 (unsigned char *) &dpyinfo->client_leader_window, 1);
6b61353c
KH
3392 }
3393
4d7e6e51 3394 unblock_input ();
3e6bec3b 3395
225c13a5 3396 /* Initialize `default-minibuffer-frame' in case this is the first
6ed8eeff 3397 frame on this terminal. */
225c13a5 3398 if (FRAME_HAS_MINIBUF_P (f)
1344aad4
TT
3399 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3400 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
15dbb4d6 3401 kset_default_minibuffer_frame (kb, frame);
225c13a5 3402
5fa98fcf
RS
3403 /* All remaining specified parameters, which have not been "used"
3404 by x_get_arg and friends, now go in the misc. alist of the frame. */
9beb8baa 3405 for (tem = parms; CONSP (tem); tem = XCDR (tem))
5fa98fcf 3406 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
f00af5b1 3407 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
5fa98fcf 3408
495fa05e 3409 UNGCPRO;
9e57df62
GM
3410
3411 /* Make sure windows on this frame appear in calls to next-window
3412 and similar functions. */
3413 Vwindow_list = Qnil;
177c0ea7 3414
9ef48a9d 3415 return unbind_to (count, frame);
01f1ba30
JB
3416}
3417
eaf1eea9 3418
0d17d282
KH
3419/* FRAME is used only to get a handle on the X display. We don't pass the
3420 display info directly because we're called from frame.c, which doesn't
3421 know about that structure. */
e4f79258 3422
87498171 3423Lisp_Object
971de7fb 3424x_get_focus_frame (struct frame *frame)
87498171 3425{
0d17d282 3426 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
87498171 3427 Lisp_Object xfocus;
0d17d282 3428 if (! dpyinfo->x_focus_frame)
87498171
KH
3429 return Qnil;
3430
0d17d282 3431 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
87498171
KH
3432 return xfocus;
3433}
f0614854 3434
3decc1e7
GM
3435
3436/* In certain situations, when the window manager follows a
3437 click-to-focus policy, there seems to be no way around calling
3438 XSetInputFocus to give another frame the input focus .
3439
3440 In an ideal world, XSetInputFocus should generally be avoided so
3441 that applications don't interfere with the window manager's focus
3442 policy. But I think it's okay to use when it's clearly done
3443 following a user-command. */
3444
a7ca3326 3445DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
7ee72033
MB
3446 doc: /* Set the input focus to FRAME.
3447FRAME nil means use the selected frame. */)
5842a27b 3448 (Lisp_Object frame)
3decc1e7 3449{
7452b7bd 3450 struct frame *f = decode_window_system_frame (frame);
3decc1e7 3451 Display *dpy = FRAME_X_DISPLAY (f);
3decc1e7 3452
4d7e6e51 3453 block_input ();
9ba8e10d 3454 x_catch_errors (dpy);
75bf0d33
DB
3455
3456 if (FRAME_X_EMBEDDED_P (f))
3457 {
3458 /* For Xembedded frames, normally the embedder forwards key
3459 events. See XEmbed Protocol Specification at
3460 http://freedesktop.org/wiki/Specifications/xembed-spec */
3461 xembed_request_focus (f);
3462 }
3463 else
3464 {
3465 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3466 RevertToParent, CurrentTime);
3467 x_ewmh_activate_frame (f);
3468 }
3469
4545fa20 3470 x_uncatch_errors ();
4d7e6e51 3471 unblock_input ();
177c0ea7 3472
3decc1e7
GM
3473 return Qnil;
3474}
3475
f0614854 3476\f
2d764c78 3477DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
4f4f2973
GM
3478 doc: /* Internal function called by `color-defined-p', which see
3479.\(Note that the Nextstep version of this function ignores FRAME.) */)
5842a27b 3480 (Lisp_Object color, Lisp_Object frame)
e12d55b2 3481{
b9dc4443 3482 XColor foo;
7452b7bd 3483 FRAME_PTR f = decode_window_system_frame (frame);
e12d55b2 3484
b7826503 3485 CHECK_STRING (color);
b9dc4443 3486
42a5b22f 3487 if (x_defined_color (f, SSDATA (color), &foo, 0))
e12d55b2
RS
3488 return Qt;
3489 else
3490 return Qnil;
3491}
3492
2d764c78 3493DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
7ee72033 3494 doc: /* Internal function called by `color-values', which see. */)
5842a27b 3495 (Lisp_Object color, Lisp_Object frame)
01f1ba30 3496{
b9dc4443 3497 XColor foo;
7452b7bd 3498 FRAME_PTR f = decode_window_system_frame (frame);
b9dc4443 3499
b7826503 3500 CHECK_STRING (color);
01f1ba30 3501
42a5b22f 3502 if (x_defined_color (f, SSDATA (color), &foo, 0))
3de717bd 3503 return list3i (foo.red, foo.green, foo.blue);
01f1ba30
JB
3504 else
3505 return Qnil;
3506}
3507
a7ca3326 3508DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
7ee72033 3509 doc: /* Internal function called by `display-color-p', which see. */)
5842a27b 3510 (Lisp_Object terminal)
01f1ba30 3511{
6ed8eeff 3512 struct x_display_info *dpyinfo = check_x_display_info (terminal);
11ae94fe 3513
b9dc4443 3514 if (dpyinfo->n_planes <= 2)
01f1ba30
JB
3515 return Qnil;
3516
b9dc4443 3517 switch (dpyinfo->visual->class)
01f1ba30
JB
3518 {
3519 case StaticColor:
3520 case PseudoColor:
3521 case TrueColor:
3522 case DirectColor:
3523 return Qt;
3524
3525 default:
3526 return Qnil;
3527 }
3528}
3529
a7ca3326 3530DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
c061c855 3531 0, 1, 0,
7ee72033 3532 doc: /* Return t if the X display supports shades of gray.
c061c855 3533Note that color displays do support shades of gray.
6ed8eeff 3534The optional argument TERMINAL specifies which display to ask about.
708e05dc 3535TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3536If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3537 (Lisp_Object terminal)
d0c9d219 3538{
6ed8eeff 3539 struct x_display_info *dpyinfo = check_x_display_info (terminal);
d0c9d219 3540
ae6b58f9 3541 if (dpyinfo->n_planes <= 1)
b9dc4443
RS
3542 return Qnil;
3543
ae6b58f9
RS
3544 switch (dpyinfo->visual->class)
3545 {
3546 case StaticColor:
3547 case PseudoColor:
3548 case TrueColor:
3549 case DirectColor:
3550 case StaticGray:
3551 case GrayScale:
3552 return Qt;
3553
3554 default:
3555 return Qnil;
3556 }
d0c9d219
RS
3557}
3558
41beb8fc 3559DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
c061c855 3560 0, 1, 0,
d29a9848 3561 doc: /* Return the width in pixels of the X display TERMINAL.
6ed8eeff 3562The optional argument TERMINAL specifies which display to ask about.
708e05dc 3563TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3564If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3565 (Lisp_Object terminal)
41beb8fc 3566{
6ed8eeff 3567 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443 3568
abe9d29c 3569 return make_number (x_display_pixel_width (dpyinfo));
41beb8fc
RS
3570}
3571
3572DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
c061c855 3573 Sx_display_pixel_height, 0, 1, 0,
d29a9848 3574 doc: /* Return the height in pixels of the X display TERMINAL.
6ed8eeff 3575The optional argument TERMINAL specifies which display to ask about.
708e05dc 3576TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3577If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3578 (Lisp_Object terminal)
41beb8fc 3579{
6ed8eeff 3580 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443 3581
abe9d29c 3582 return make_number (x_display_pixel_height (dpyinfo));
41beb8fc
RS
3583}
3584
3585DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
c061c855 3586 0, 1, 0,
d29a9848 3587 doc: /* Return the number of bitplanes of the X display TERMINAL.
6ed8eeff 3588The optional argument TERMINAL specifies which display to ask about.
708e05dc 3589TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3590If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3591 (Lisp_Object terminal)
41beb8fc 3592{
6ed8eeff 3593 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3594
3595 return make_number (dpyinfo->n_planes);
41beb8fc
RS
3596}
3597
3598DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
c061c855 3599 0, 1, 0,
d29a9848 3600 doc: /* Return the number of color cells of the X display TERMINAL.
6ed8eeff 3601The optional argument TERMINAL specifies which display to ask about.
708e05dc 3602TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3603If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3604 (Lisp_Object terminal)
41beb8fc 3605{
6ed8eeff 3606 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443 3607
6b61353c
KH
3608 int nr_planes = DisplayPlanes (dpyinfo->display,
3609 XScreenNumberOfScreen (dpyinfo->screen));
3610
3611 /* Truncate nr_planes to 24 to avoid integer overflow.
3612 Some displays says 32, but only 24 bits are actually significant.
3613 There are only very few and rare video cards that have more than
3614 24 significant bits. Also 24 bits is more than 16 million colors,
3615 it "should be enough for everyone". */
3616 if (nr_planes > 24) nr_planes = 24;
3617
3618 return make_number (1 << nr_planes);
41beb8fc
RS
3619}
3620
9d317b2c
RS
3621DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3622 Sx_server_max_request_size,
c061c855 3623 0, 1, 0,
d29a9848 3624 doc: /* Return the maximum request size of the X server of display TERMINAL.
6ed8eeff 3625The optional argument TERMINAL specifies which display to ask about.
708e05dc 3626TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3627If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3628 (Lisp_Object terminal)
9d317b2c 3629{
6ed8eeff 3630 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3631
3632 return make_number (MAXREQUEST (dpyinfo->display));
9d317b2c
RS
3633}
3634
41beb8fc 3635DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
d29a9848 3636 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
09e80d9f 3637\(Labeling every distributor as a "vendor" embodies the false assumption
95d62d8a 3638that operating systems cannot be developed and distributed noncommercially.)
6ed8eeff 3639The optional argument TERMINAL specifies which display to ask about.
708e05dc 3640TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3641If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3642 (Lisp_Object terminal)
41beb8fc 3643{
6ed8eeff 3644 struct x_display_info *dpyinfo = check_x_display_info (terminal);
675e2c69 3645 const char *vendor = ServerVendor (dpyinfo->display);
b9dc4443 3646
41beb8fc
RS
3647 if (! vendor) vendor = "";
3648 return build_string (vendor);
3649}
3650
3651DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
d29a9848 3652 doc: /* Return the version numbers of the X server of display TERMINAL.
c061c855 3653The value is a list of three integers: the major and minor
95d62d8a 3654version numbers of the X Protocol in use, and the distributor-specific release
c061c855
GM
3655number. See also the function `x-server-vendor'.
3656
6ed8eeff 3657The optional argument TERMINAL specifies which display to ask about.
708e05dc 3658TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3659If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3660 (Lisp_Object terminal)
41beb8fc 3661{
6ed8eeff 3662 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443 3663 Display *dpy = dpyinfo->display;
11ae94fe 3664
3de717bd
DA
3665 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3666 VendorRelease (dpy));
41beb8fc
RS
3667}
3668
3669DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
6ed8eeff
KL
3670 doc: /* Return the number of screens on the X server of display TERMINAL.
3671The optional argument TERMINAL specifies which display to ask about.
708e05dc 3672TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3673If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3674 (Lisp_Object terminal)
41beb8fc 3675{
6ed8eeff 3676 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3677
3678 return make_number (ScreenCount (dpyinfo->display));
41beb8fc
RS
3679}
3680
3681DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
6ed8eeff
KL
3682 doc: /* Return the height in millimeters of the X display TERMINAL.
3683The optional argument TERMINAL specifies which display to ask about.
708e05dc 3684TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3685If omitted or nil, that stands for the selected frame's display. */)
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).
7ee72033 3697If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3698 (Lisp_Object terminal)
41beb8fc 3699{
6ed8eeff 3700 struct x_display_info *dpyinfo = check_x_display_info (terminal);
b9dc4443
RS
3701
3702 return make_number (WidthMMOfScreen (dpyinfo->screen));
41beb8fc
RS
3703}
3704
3705DEFUN ("x-display-backing-store", Fx_display_backing_store,
c061c855 3706 Sx_display_backing_store, 0, 1, 0,
d29a9848 3707 doc: /* Return an indication of whether X display TERMINAL does backing store.
c061c855 3708The value may be `always', `when-mapped', or `not-useful'.
6ed8eeff 3709The optional argument TERMINAL specifies which display to ask about.
708e05dc 3710TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3711If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3712 (Lisp_Object terminal)
41beb8fc 3713{
6ed8eeff 3714 struct x_display_info *dpyinfo = check_x_display_info (terminal);
8ec8a5ec 3715 Lisp_Object result;
11ae94fe 3716
b9dc4443 3717 switch (DoesBackingStore (dpyinfo->screen))
41beb8fc
RS
3718 {
3719 case Always:
8ec8a5ec
GM
3720 result = intern ("always");
3721 break;
41beb8fc
RS
3722
3723 case WhenMapped:
8ec8a5ec
GM
3724 result = intern ("when-mapped");
3725 break;
41beb8fc
RS
3726
3727 case NotUseful:
8ec8a5ec
GM
3728 result = intern ("not-useful");
3729 break;
41beb8fc
RS
3730
3731 default:
3732 error ("Strange value for BackingStore parameter of screen");
3733 }
8ec8a5ec
GM
3734
3735 return result;
41beb8fc
RS
3736}
3737
3738DEFUN ("x-display-visual-class", Fx_display_visual_class,
c061c855 3739 Sx_display_visual_class, 0, 1, 0,
6ed8eeff 3740 doc: /* Return the visual class of the X display TERMINAL.
c061c855
GM
3741The value is one of the symbols `static-gray', `gray-scale',
3742`static-color', `pseudo-color', `true-color', or `direct-color'.
3743
6ed8eeff 3744The optional argument TERMINAL specifies which display to ask about.
708e05dc 3745TERMINAL should a terminal object, a frame or a display name (a string).
7ee72033 3746If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3747 (Lisp_Object terminal)
41beb8fc 3748{
6ed8eeff 3749 struct x_display_info *dpyinfo = check_x_display_info (terminal);
8ec8a5ec 3750 Lisp_Object result;
11ae94fe 3751
b9dc4443 3752 switch (dpyinfo->visual->class)
41beb8fc 3753 {
8ec8a5ec
GM
3754 case StaticGray:
3755 result = intern ("static-gray");
3756 break;
3757 case GrayScale:
3758 result = intern ("gray-scale");
3759 break;
3760 case StaticColor:
3761 result = intern ("static-color");
3762 break;
3763 case PseudoColor:
3764 result = intern ("pseudo-color");
3765 break;
3766 case TrueColor:
3767 result = intern ("true-color");
3768 break;
3769 case DirectColor:
3770 result = intern ("direct-color");
3771 break;
41beb8fc
RS
3772 default:
3773 error ("Display has an unknown visual class");
3774 }
177c0ea7 3775
8ec8a5ec 3776 return result;
41beb8fc
RS
3777}
3778
3779DEFUN ("x-display-save-under", Fx_display_save_under,
c061c855 3780 Sx_display_save_under, 0, 1, 0,
d29a9848 3781 doc: /* Return t if the X display TERMINAL supports the save-under feature.
6ed8eeff 3782The optional argument TERMINAL specifies which display to ask about.
708e05dc 3783TERMINAL should be a terminal object, a frame or a display name (a string).
7ee72033 3784If omitted or nil, that stands for the selected frame's display. */)
5842a27b 3785 (Lisp_Object terminal)
41beb8fc 3786{
6ed8eeff 3787 struct x_display_info *dpyinfo = check_x_display_info (terminal);
11ae94fe 3788
b9dc4443 3789 if (DoesSaveUnders (dpyinfo->screen) == True)
41beb8fc
RS
3790 return Qt;
3791 else
3792 return Qnil;
3793}
3794\f
b9dc4443 3795int
971de7fb 3796x_pixel_width (register struct frame *f)
01f1ba30 3797{
be786000 3798 return FRAME_PIXEL_WIDTH (f);
01f1ba30
JB
3799}
3800
b9dc4443 3801int
971de7fb 3802x_pixel_height (register struct frame *f)
01f1ba30 3803{
be786000 3804 return FRAME_PIXEL_HEIGHT (f);
55caf99c
RS
3805}
3806
a6ad00c0
GM
3807/************************************************************************
3808 X Displays
3809 ************************************************************************/
3810
01f1ba30 3811\f
a6ad00c0
GM
3812/* Mapping visual names to visuals. */
3813
3814static struct visual_class
3815{
675e2c69 3816 const char *name;
a6ad00c0
GM
3817 int class;
3818}
3819visual_classes[] =
3820{
3821 {"StaticGray", StaticGray},
3822 {"GrayScale", GrayScale},
3823 {"StaticColor", StaticColor},
3824 {"PseudoColor", PseudoColor},
3825 {"TrueColor", TrueColor},
3826 {"DirectColor", DirectColor},
9908a324 3827 {NULL, 0}
a6ad00c0
GM
3828};
3829
3830
404daac1 3831#ifndef HAVE_XSCREENNUMBEROFSCREEN
a6ad00c0
GM
3832
3833/* Value is the screen number of screen SCR. This is a substitute for
3834 the X function with the same name when that doesn't exist. */
3835
404daac1
RS
3836int
3837XScreenNumberOfScreen (scr)
3838 register Screen *scr;
3839{
a6ad00c0
GM
3840 Display *dpy = scr->display;
3841 int i;
3df34fdb 3842
a6ad00c0 3843 for (i = 0; i < dpy->nscreens; ++i)
fbd5ceb2 3844 if (scr == dpy->screens + i)
a6ad00c0 3845 break;
404daac1 3846
a6ad00c0 3847 return i;
404daac1 3848}
a6ad00c0 3849
404daac1
RS
3850#endif /* not HAVE_XSCREENNUMBEROFSCREEN */
3851
01f1ba30 3852
a6ad00c0
GM
3853/* Select the visual that should be used on display DPYINFO. Set
3854 members of DPYINFO appropriately. Called from x_term_init. */
fe24a618 3855
a6ad00c0 3856void
971de7fb 3857select_visual (struct x_display_info *dpyinfo)
a6ad00c0
GM
3858{
3859 Display *dpy = dpyinfo->display;
3860 Screen *screen = dpyinfo->screen;
3861 Lisp_Object value;
fe24a618 3862
a6ad00c0
GM
3863 /* See if a visual is specified. */
3864 value = display_x_get_resource (dpyinfo,
3865 build_string ("visualClass"),
3866 build_string ("VisualClass"),
3867 Qnil, Qnil);
3868 if (STRINGP (value))
3869 {
3870 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
3871 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
3872 depth, a decimal number. NAME is compared with case ignored. */
38182d90 3873 char *s = alloca (SBYTES (value) + 1);
a6ad00c0
GM
3874 char *dash;
3875 int i, class = -1;
3876 XVisualInfo vinfo;
3877
42a5b22f 3878 strcpy (s, SSDATA (value));
8966b757 3879 dash = strchr (s, '-');
a6ad00c0
GM
3880 if (dash)
3881 {
3882 dpyinfo->n_planes = atoi (dash + 1);
3883 *dash = '\0';
3884 }
3885 else
3886 /* We won't find a matching visual with depth 0, so that
3887 an error will be printed below. */
3888 dpyinfo->n_planes = 0;
f0614854 3889
a6ad00c0
GM
3890 /* Determine the visual class. */
3891 for (i = 0; visual_classes[i].name; ++i)
05131107 3892 if (xstrcasecmp (s, visual_classes[i].name) == 0)
a6ad00c0
GM
3893 {
3894 class = visual_classes[i].class;
3895 break;
3896 }
01f1ba30 3897
a6ad00c0
GM
3898 /* Look up a matching visual for the specified class. */
3899 if (class == -1
3900 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
3901 dpyinfo->n_planes, class, &vinfo))
d5db4077 3902 fatal ("Invalid visual specification `%s'", SDATA (value));
177c0ea7 3903
a6ad00c0
GM
3904 dpyinfo->visual = vinfo.visual;
3905 }
01f1ba30
JB
3906 else
3907 {
a6ad00c0
GM
3908 int n_visuals;
3909 XVisualInfo *vinfo, vinfo_template;
177c0ea7 3910
a6ad00c0
GM
3911 dpyinfo->visual = DefaultVisualOfScreen (screen);
3912
a6ad00c0 3913 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
a6ad00c0
GM
3914 vinfo_template.screen = XScreenNumberOfScreen (screen);
3915 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
3916 &vinfo_template, &n_visuals);
83725342 3917 if (n_visuals <= 0)
a6ad00c0
GM
3918 fatal ("Can't get proper X visual info");
3919
94ac875b 3920 dpyinfo->n_planes = vinfo->depth;
98c6f1e3 3921 XFree (vinfo);
a6ad00c0 3922 }
01f1ba30 3923}
01f1ba30 3924
a6ad00c0 3925
b9dc4443
RS
3926/* Return the X display structure for the display named NAME.
3927 Open a new connection if necessary. */
3928
e4cebfca 3929static struct x_display_info *
971de7fb 3930x_display_info_for_name (Lisp_Object name)
b9dc4443 3931{
08a90d6a 3932 Lisp_Object names;
b9dc4443
RS
3933 struct x_display_info *dpyinfo;
3934
b7826503 3935 CHECK_STRING (name);
b9dc4443 3936
2246281f
KL
3937#if 0
3938 if (! EQ (Vinitial_window_system, intern ("x")))
3939 error ("Not using X Windows"); /* That doesn't stop us anymore. */
3940#endif
806048df 3941
08a90d6a
RS
3942 for (dpyinfo = x_display_list, names = x_display_name_list;
3943 dpyinfo;
8e713be6 3944 dpyinfo = dpyinfo->next, names = XCDR (names))
b9dc4443
RS
3945 {
3946 Lisp_Object tem;
8e713be6 3947 tem = Fstring_equal (XCAR (XCAR (names)), name);
08a90d6a 3948 if (!NILP (tem))
b9dc4443
RS
3949 return dpyinfo;
3950 }
3951
b7975ee4
KH
3952 /* Use this general default value to start with. */
3953 Vx_resource_name = Vinvocation_name;
3954
b9dc4443
RS
3955 validate_x_resource_name ();
3956
9b207e8e 3957 dpyinfo = x_term_init (name, (char *)0,
51b59d79 3958 SSDATA (Vx_resource_name));
b9dc4443 3959
08a90d6a 3960 if (dpyinfo == 0)
d5db4077 3961 error ("Cannot connect to X server %s", SDATA (name));
08a90d6a 3962
b9dc4443
RS
3963 XSETFASTINT (Vwindow_system_version, 11);
3964
3965 return dpyinfo;
3966}
3967
a6ad00c0 3968
01f1ba30 3969DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
c061c855 3970 1, 3, 0,
4f4f2973 3971 doc: /* Open a connection to a display server.
c061c855
GM
3972DISPLAY is the name of the display to connect to.
3973Optional second arg XRM-STRING is a string of resources in xrdb format.
3974If the optional third arg MUST-SUCCEED is non-nil,
4f4f2973
GM
3975terminate Emacs if we can't open the connection.
3976\(In the Nextstep version, the last two arguments are currently ignored.) */)
5842a27b 3977 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
01f1ba30 3978{
5f742c1b 3979 char *xrm_option;
b9dc4443 3980 struct x_display_info *dpyinfo;
01f1ba30 3981
b7826503 3982 CHECK_STRING (display);
d387c960 3983 if (! NILP (xrm_string))
b7826503 3984 CHECK_STRING (xrm_string);
01f1ba30 3985
2246281f
KL
3986#if 0
3987 if (! EQ (Vinitial_window_system, intern ("x")))
3988 error ("Not using X Windows"); /* That doesn't stop us anymore. */
3989#endif
806048df 3990
d387c960 3991 if (! NILP (xrm_string))
5f742c1b 3992 xrm_option = SSDATA (xrm_string);
01f1ba30 3993 else
5f742c1b 3994 xrm_option = (char *) 0;
d387c960
JB
3995
3996 validate_x_resource_name ();
3997
e1b1bee8 3998 /* This is what opens the connection and sets x_current_display.
b9dc4443
RS
3999 This also initializes many symbols, such as those used for input. */
4000 dpyinfo = x_term_init (display, xrm_option,
51b59d79 4001 SSDATA (Vx_resource_name));
f1c16f36 4002
08a90d6a
RS
4003 if (dpyinfo == 0)
4004 {
4005 if (!NILP (must_succeed))
10ffbc14
GM
4006 fatal ("Cannot connect to X server %s.\n\
4007Check the DISPLAY environment variable or use `-d'.\n\
842a9389
JB
4008Also use the `xauth' program to verify that you have the proper\n\
4009authorization information needed to connect the X server.\n\
bf770132 4010An insecure way to solve the problem may be to use `xhost'.\n",
d5db4077 4011 SDATA (display));
08a90d6a 4012 else
d5db4077 4013 error ("Cannot connect to X server %s", SDATA (display));
08a90d6a
RS
4014 }
4015
b9dc4443 4016 XSETFASTINT (Vwindow_system_version, 11);
01f1ba30
JB
4017 return Qnil;
4018}
4019
08a90d6a
RS
4020DEFUN ("x-close-connection", Fx_close_connection,
4021 Sx_close_connection, 1, 1, 0,
00d385b0 4022 doc: /* Close the connection to TERMINAL's X server.
708e05dc 4023For TERMINAL, specify a terminal object, a frame or a display name (a
00d385b0
KL
4024string). If TERMINAL is nil, that stands for the selected frame's
4025terminal. */)
5842a27b 4026 (Lisp_Object terminal)
01f1ba30 4027{
00d385b0 4028 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3457bc6e 4029
08a90d6a
RS
4030 if (dpyinfo->reference_count > 0)
4031 error ("Display still has frames on it");
01f1ba30 4032
6ed8eeff 4033 x_delete_terminal (dpyinfo->terminal);
3457bc6e 4034
01f1ba30
JB
4035 return Qnil;
4036}
4037
08a90d6a 4038DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
7ee72033 4039 doc: /* Return the list of display names that Emacs has connections to. */)
5842a27b 4040 (void)
08a90d6a
RS
4041{
4042 Lisp_Object tail, result;
4043
4044 result = Qnil;
9beb8baa 4045 for (tail = x_display_name_list; CONSP (tail); tail = XCDR (tail))
8e713be6 4046 result = Fcons (XCAR (XCAR (tail)), result);
08a90d6a
RS
4047
4048 return result;
4049}
4050
4051DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
7ee72033 4052 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
7c051dd8
GM
4053This function only has an effect on X Windows. With MS Windows, it is
4054defined but does nothing.
4055
c061c855
GM
4056If ON is nil, allow buffering of requests.
4057Turning on synchronization prohibits the Xlib routines from buffering
4058requests and seriously degrades performance, but makes debugging much
4059easier.
00d385b0 4060The optional second argument TERMINAL specifies which display to act on.
708e05dc 4061TERMINAL should be a terminal object, a frame or a display name (a string).
00d385b0 4062If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
5842a27b 4063 (Lisp_Object on, Lisp_Object terminal)
01f1ba30 4064{
00d385b0 4065 struct x_display_info *dpyinfo = check_x_display_info (terminal);
11ae94fe 4066
b9dc4443 4067 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
01f1ba30
JB
4068
4069 return Qnil;
4070}
4071
b9dc4443 4072/* Wait for responses to all X commands issued so far for frame F. */
6b7b1820
RS
4073
4074void
971de7fb 4075x_sync (FRAME_PTR f)
6b7b1820 4076{
4d7e6e51 4077 block_input ();
b9dc4443 4078 XSync (FRAME_X_DISPLAY (f), False);
4d7e6e51 4079 unblock_input ();
6b7b1820 4080}
333b20bb 4081
01f1ba30 4082\f
333b20bb 4083/***********************************************************************
6b61353c 4084 Window properties
333b20bb 4085 ***********************************************************************/
f1c16f36 4086
6b61353c
KH
4087DEFUN ("x-change-window-property", Fx_change_window_property,
4088 Sx_change_window_property, 2, 6, 0,
4089 doc: /* Change window property PROP to VALUE on the X window of FRAME.
7c051dd8
GM
4090PROP must be a string. VALUE may be a string or a list of conses,
4091numbers and/or strings. If an element in the list is a string, it is
4092converted to an atom and the value of the atom is used. If an element
4093is a cons, it is converted to a 32 bit number where the car is the 16
4094top bits and the cdr is the lower 16 bits.
4095
6b61353c
KH
4096FRAME nil or omitted means use the selected frame.
4097If TYPE is given and non-nil, it is the name of the type of VALUE.
4098If TYPE is not given or nil, the type is STRING.
4099FORMAT gives the size in bits of each element if VALUE is a list.
4100It must be one of 8, 16 or 32.
4101If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
fca8d6b6 4102If OUTER-P is non-nil, the property is changed for the outer X window of
7c051dd8 4103FRAME. Default is to change on the edit X window. */)
fca8d6b6
AS
4104 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4105 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
6b61353c 4106{
7452b7bd 4107 struct frame *f = decode_window_system_frame (frame);
6b61353c
KH
4108 Atom prop_atom;
4109 Atom target_type = XA_STRING;
4110 int element_format = 8;
4111 unsigned char *data;
4112 int nelements;
4113 Window w;
333b20bb 4114
6b61353c 4115 CHECK_STRING (prop);
333b20bb 4116
6b61353c
KH
4117 if (! NILP (format))
4118 {
4119 CHECK_NUMBER (format);
333b20bb 4120
d311d28c
PE
4121 if (XINT (format) != 8 && XINT (format) != 16
4122 && XINT (format) != 32)
6b61353c 4123 error ("FORMAT must be one of 8, 16 or 32");
d311d28c 4124 element_format = XINT (format);
6b61353c 4125 }
333b20bb 4126
6b61353c
KH
4127 if (CONSP (value))
4128 {
c678c835
PE
4129 ptrdiff_t elsize;
4130
6b61353c
KH
4131 nelements = x_check_property_data (value);
4132 if (nelements == -1)
4133 error ("Bad data in VALUE, must be number, string or cons");
333b20bb 4134
c678c835
PE
4135 /* The man page for XChangeProperty:
4136 "If the specified format is 32, the property data must be a
4137 long array."
4138 This applies even if long is more than 32 bits. The X library
4139 converts to 32 bits before sending to the X server. */
4140 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
0065d054 4141 data = xnmalloc (nelements, elsize);
333b20bb 4142
6b61353c
KH
4143 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4144 }
4145 else
4146 {
4147 CHECK_STRING (value);
4148 data = SDATA (value);
c678c835
PE
4149 if (INT_MAX < SBYTES (value))
4150 error ("VALUE too long");
4151 nelements = SBYTES (value);
6b61353c 4152 }
333b20bb 4153
4d7e6e51 4154 block_input ();
42a5b22f 4155 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
6b61353c
KH
4156 if (! NILP (type))
4157 {
4158 CHECK_STRING (type);
42a5b22f 4159 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
6b61353c 4160 }
333b20bb 4161
6b61353c
KH
4162 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4163 else w = FRAME_X_WINDOW (f);
5b3f8550 4164
6b61353c
KH
4165 XChangeProperty (FRAME_X_DISPLAY (f), w,
4166 prop_atom, target_type, element_format, PropModeReplace,
4167 data, nelements);
333b20bb 4168
6b61353c 4169 if (CONSP (value)) xfree (data);
333b20bb 4170
6b61353c
KH
4171 /* Make sure the property is set when we return. */
4172 XFlush (FRAME_X_DISPLAY (f));
4d7e6e51 4173 unblock_input ();
333b20bb 4174
6b61353c 4175 return value;
333b20bb
GM
4176}
4177
4178
6b61353c
KH
4179DEFUN ("x-delete-window-property", Fx_delete_window_property,
4180 Sx_delete_window_property, 1, 2, 0,
4181 doc: /* Remove window property PROP from X window of FRAME.
4182FRAME nil or omitted means use the selected frame. Value is PROP. */)
5842a27b 4183 (Lisp_Object prop, Lisp_Object frame)
333b20bb 4184{
7452b7bd 4185 struct frame *f = decode_window_system_frame (frame);
6b61353c 4186 Atom prop_atom;
333b20bb 4187
6b61353c 4188 CHECK_STRING (prop);
4d7e6e51 4189 block_input ();
42a5b22f 4190 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
6b61353c 4191 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
333b20bb 4192
6b61353c
KH
4193 /* Make sure the property is removed when we return. */
4194 XFlush (FRAME_X_DISPLAY (f));
4d7e6e51 4195 unblock_input ();
333b20bb 4196
6b61353c
KH
4197 return prop;
4198}
333b20bb 4199
333b20bb 4200
6b61353c
KH
4201DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4202 1, 6, 0,
4203 doc: /* Value is the value of window property PROP on FRAME.
4204If FRAME is nil or omitted, use the selected frame.
7c051dd8 4205
7c051dd8
GM
4206On X Windows, the following optional arguments are also accepted:
4207If TYPE is nil or omitted, get the property as a string.
4208Otherwise TYPE is the name of the atom that denotes the type expected.
6b61353c
KH
4209If SOURCE is non-nil, get the property on that window instead of from
4210FRAME. The number 0 denotes the root window.
ddadbc0e
AS
4211If DELETE-P is non-nil, delete the property after retrieving it.
4212If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4213
4214On MS Windows, this function accepts but ignores those optional arguments.
6b61353c
KH
4215
4216Value is nil if FRAME hasn't a property with name PROP or if PROP has
7c051dd8 4217no value of TYPE (always string in the MS Windows case). */)
ddadbc0e
AS
4218 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4219 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
333b20bb 4220{
7452b7bd 4221 struct frame *f = decode_window_system_frame (frame);
6b61353c
KH
4222 Atom prop_atom;
4223 int rc;
4224 Lisp_Object prop_value = Qnil;
a3db4b26 4225 unsigned char *tmp_data = NULL;
6b61353c
KH
4226 Atom actual_type;
4227 Atom target_type = XA_STRING;
4228 int actual_format;
4229 unsigned long actual_size, bytes_remaining;
4230 Window target_window = FRAME_X_WINDOW (f);
4231 struct gcpro gcpro1;
177c0ea7 4232
6b61353c
KH
4233 GCPRO1 (prop_value);
4234 CHECK_STRING (prop);
1783ffa2 4235
6b61353c
KH
4236 if (! NILP (source))
4237 {
be44ca6c
PE
4238 CONS_TO_INTEGER (source, Window, target_window);
4239 if (! target_window)
4240 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
333b20bb
GM
4241 }
4242
4d7e6e51 4243 block_input ();
6b61353c
KH
4244 if (STRINGP (type))
4245 {
42a5b22f 4246 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
6b61353c
KH
4247 target_type = AnyPropertyType;
4248 else
42a5b22f 4249 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
6b61353c 4250 }
333b20bb 4251
42a5b22f 4252 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
6b61353c
KH
4253 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4254 prop_atom, 0, 0, False, target_type,
4255 &actual_type, &actual_format, &actual_size,
a3db4b26 4256 &bytes_remaining, &tmp_data);
6b61353c
KH
4257 if (rc == Success)
4258 {
4259 int size = bytes_remaining;
333b20bb 4260
6b61353c
KH
4261 XFree (tmp_data);
4262 tmp_data = NULL;
333b20bb 4263
6b61353c 4264 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
333b20bb 4265 prop_atom, 0, bytes_remaining,
6b61353c 4266 ! NILP (delete_p), target_type,
177c0ea7
JB
4267 &actual_type, &actual_format,
4268 &actual_size, &bytes_remaining,
a3db4b26 4269 &tmp_data);
4c8c7926 4270 if (rc == Success && tmp_data)
6b61353c 4271 {
fede04ef
JD
4272 /* The man page for XGetWindowProperty says:
4273 "If the returned format is 32, the returned data is represented
4274 as a long array and should be cast to that type to obtain the
4275 elements."
4276 This applies even if long is more than 32 bits, the X library
4277 converts from 32 bit elements received from the X server to long
72af86bd 4278 and passes the long array to us. Thus, for that case memcpy can not
fede04ef
JD
4279 be used. We convert to a 32 bit type here, because so much code
4280 assume on that.
4281
4282 The bytes and offsets passed to XGetWindowProperty refers to the
4283 property and those are indeed in 32 bit quantities if format is
4284 32. */
4285
908589fd 4286 if (BITS_PER_LONG > 32 && actual_format == 32)
fede04ef
JD
4287 {
4288 unsigned long i;
4289 int *idata = (int *) tmp_data;
4290 long *ldata = (long *) tmp_data;
4291
4292 for (i = 0; i < actual_size; ++i)
a3db4b26 4293 idata[i] = (int) ldata[i];
fede04ef
JD
4294 }
4295
6b61353c 4296 if (NILP (vector_ret_p))
5f742c1b 4297 prop_value = make_string ((char *) tmp_data, size);
6b61353c
KH
4298 else
4299 prop_value = x_property_data_to_lisp (f,
a3db4b26 4300 tmp_data,
6b61353c
KH
4301 actual_type,
4302 actual_format,
4303 actual_size);
4304 }
333b20bb 4305
6b61353c 4306 if (tmp_data) XFree (tmp_data);
333b20bb
GM
4307 }
4308
4d7e6e51 4309 unblock_input ();
6b61353c 4310 UNGCPRO;
333b20bb
GM
4311 return prop_value;
4312}
4313
4314
4315\f
4316/***********************************************************************
4317 Busy cursor
4318 ***********************************************************************/
4319
0af913d7
GM
4320/* Timer function of hourglass_atimer. TIMER is equal to
4321 hourglass_atimer.
4ae9a85e 4322
0af913d7
GM
4323 Display an hourglass pointer on all frames by mapping the frames'
4324 hourglass_window. Set the hourglass_p flag in the frames'
4325 output_data.x structure to indicate that an hourglass cursor is
4326 shown on the frames. */
4ae9a85e 4327
1885ab29 4328void
971de7fb 4329show_hourglass (struct atimer *timer)
4ae9a85e
GM
4330{
4331 /* The timer implementation will cancel this timer automatically
0af913d7 4332 after this function has run. Set hourglass_atimer to null
4ae9a85e 4333 so that we know the timer doesn't have to be canceled. */
0af913d7 4334 hourglass_atimer = NULL;
4ae9a85e 4335
0af913d7 4336 if (!hourglass_shown_p)
333b20bb
GM
4337 {
4338 Lisp_Object rest, frame;
177c0ea7 4339
4d7e6e51 4340 block_input ();
177c0ea7 4341
333b20bb 4342 FOR_EACH_FRAME (rest, frame)
5f7a1890
GM
4343 {
4344 struct frame *f = XFRAME (frame);
177c0ea7 4345
5f7a1890
GM
4346 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4347 {
4348 Display *dpy = FRAME_X_DISPLAY (f);
177c0ea7 4349
5f7a1890
GM
4350#ifdef USE_X_TOOLKIT
4351 if (f->output_data.x->widget)
4352#else
4353 if (FRAME_OUTER_WINDOW (f))
4354#endif
4355 {
0af913d7 4356 f->output_data.x->hourglass_p = 1;
177c0ea7 4357
0af913d7 4358 if (!f->output_data.x->hourglass_window)
5f7a1890
GM
4359 {
4360 unsigned long mask = CWCursor;
4361 XSetWindowAttributes attrs;
49b8b5dc
JD
4362#ifdef USE_GTK
4363 Window parent = FRAME_X_WINDOW (f);
4364#else
4365 Window parent = FRAME_OUTER_WINDOW (f);
4366#endif
0af913d7 4367 attrs.cursor = f->output_data.x->hourglass_cursor;
177c0ea7 4368
0af913d7 4369 f->output_data.x->hourglass_window
49b8b5dc 4370 = XCreateWindow (dpy, parent,
5f7a1890
GM
4371 0, 0, 32000, 32000, 0, 0,
4372 InputOnly,
4373 CopyFromParent,
4374 mask, &attrs);
4375 }
177c0ea7 4376
0af913d7 4377 XMapRaised (dpy, f->output_data.x->hourglass_window);
5f7a1890
GM
4378 XFlush (dpy);
4379 }
4380 }
4381 }
333b20bb 4382
0af913d7 4383 hourglass_shown_p = 1;
4d7e6e51 4384 unblock_input ();
4ae9a85e 4385 }
333b20bb
GM
4386}
4387
4388
0af913d7
GM
4389/* Hide the hourglass pointer on all frames, if it is currently
4390 shown. */
333b20bb 4391
1885ab29 4392void
971de7fb 4393hide_hourglass (void)
4ae9a85e 4394{
0af913d7 4395 if (hourglass_shown_p)
333b20bb 4396 {
4ae9a85e
GM
4397 Lisp_Object rest, frame;
4398
4d7e6e51 4399 block_input ();
4ae9a85e 4400 FOR_EACH_FRAME (rest, frame)
333b20bb 4401 {
4ae9a85e 4402 struct frame *f = XFRAME (frame);
177c0ea7 4403
4ae9a85e
GM
4404 if (FRAME_X_P (f)
4405 /* Watch out for newly created frames. */
0af913d7 4406 && f->output_data.x->hourglass_window)
4ae9a85e 4407 {
0af913d7
GM
4408 XUnmapWindow (FRAME_X_DISPLAY (f),
4409 f->output_data.x->hourglass_window);
4410 /* Sync here because XTread_socket looks at the
4411 hourglass_p flag that is reset to zero below. */
4ae9a85e 4412 XSync (FRAME_X_DISPLAY (f), False);
0af913d7 4413 f->output_data.x->hourglass_p = 0;
4ae9a85e 4414 }
333b20bb 4415 }
333b20bb 4416
0af913d7 4417 hourglass_shown_p = 0;
4d7e6e51 4418 unblock_input ();
4ae9a85e 4419 }
333b20bb
GM
4420}
4421
4422
4423\f
4424/***********************************************************************
4425 Tool tips
4426 ***********************************************************************/
4427
f57e2426
J
4428static Lisp_Object x_create_tip_frame (struct x_display_info *,
4429 Lisp_Object, Lisp_Object);
4430static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4431 Lisp_Object, int, int, int *, int *);
177c0ea7 4432
44b5a125 4433/* The frame of a currently visible tooltip. */
333b20bb 4434
44b5a125 4435Lisp_Object tip_frame;
333b20bb
GM
4436
4437/* If non-nil, a timer started that hides the last tooltip when it
4438 fires. */
4439
e4cebfca 4440static Lisp_Object tip_timer;
333b20bb
GM
4441Window tip_window;
4442
06d62053
GM
4443/* If non-nil, a vector of 3 elements containing the last args
4444 with which x-show-tip was called. See there. */
4445
e4cebfca 4446static Lisp_Object last_show_tip_args;
06d62053 4447
eaf1eea9
GM
4448
4449static Lisp_Object
971de7fb 4450unwind_create_tip_frame (Lisp_Object frame)
eaf1eea9 4451{
c844a81a
GM
4452 Lisp_Object deleted;
4453
4454 deleted = unwind_create_frame (frame);
4455 if (EQ (deleted, Qt))
4456 {
4457 tip_window = None;
4458 tip_frame = Qnil;
4459 }
177c0ea7 4460
c844a81a 4461 return deleted;
eaf1eea9
GM
4462}
4463
4464
333b20bb 4465/* Create a frame for a tooltip on the display described by DPYINFO.
275841bf
GM
4466 PARMS is a list of frame parameters. TEXT is the string to
4467 display in the tip frame. Value is the frame.
eaf1eea9
GM
4468
4469 Note that functions called here, esp. x_default_parameter can
4470 signal errors, for instance when a specified color name is
4471 undefined. We have to make sure that we're in a consistent state
4472 when this happens. */
333b20bb
GM
4473
4474static Lisp_Object
aa1859f5
J
4475x_create_tip_frame (struct x_display_info *dpyinfo,
4476 Lisp_Object parms,
4477 Lisp_Object text)
333b20bb
GM
4478{
4479 struct frame *f;
06b0c8a0 4480 Lisp_Object frame;
333b20bb 4481 Lisp_Object name;
333b20bb 4482 int width, height;
d311d28c 4483 ptrdiff_t count = SPECPDL_INDEX ();
b6d7acec 4484 struct gcpro gcpro1, gcpro2, gcpro3;
06d62053 4485 int face_change_count_before = face_change_count;
275841bf
GM
4486 Lisp_Object buffer;
4487 struct buffer *old_buffer;
333b20bb 4488
89acb56d
SM
4489 if (!dpyinfo->terminal->name)
4490 error ("Terminal is not live, can't create new frames on it");
333b20bb 4491
47f3c6b4 4492 parms = Fcopy_alist (parms);
333b20bb
GM
4493
4494 /* Get the name of the frame to use for resource lookup. */
4495 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4496 if (!STRINGP (name)
4497 && !EQ (name, Qunbound)
4498 && !NILP (name))
4499 error ("Invalid frame name--not a string or nil");
333b20bb
GM
4500
4501 frame = Qnil;
4502 GCPRO3 (parms, name, frame);
44b5a125 4503 f = make_frame (1);
333b20bb 4504 XSETFRAME (frame, f);
275841bf
GM
4505
4506 buffer = Fget_buffer_create (build_string (" *tip*"));
08908aca
MR
4507 /* Use set_window_buffer instead of Fset_window_buffer (see
4508 discussion of bug#11984, bug#12025, bug#12026). */
4509 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
275841bf
GM
4510 old_buffer = current_buffer;
4511 set_buffer_internal_1 (XBUFFER (buffer));
39eb03f1 4512 bset_truncate_lines (current_buffer, Qnil);
5c2a995d
KH
4513 specbind (Qinhibit_read_only, Qt);
4514 specbind (Qinhibit_modification_hooks, Qt);
275841bf
GM
4515 Ferase_buffer ();
4516 Finsert (1, &text);
4517 set_buffer_internal_1 (old_buffer);
177c0ea7 4518
8a1a7743 4519 record_unwind_protect (unwind_create_tip_frame, frame);
333b20bb 4520
6ed8eeff 4521 f->terminal = dpyinfo->terminal;
daf01701 4522
eaf1eea9
GM
4523 /* By setting the output method, we're essentially saying that
4524 the frame is live, as per FRAME_LIVE_P. If we get a signal
4525 from this point on, x_destroy_window might screw up reference
4526 counts etc. */
333b20bb 4527 f->output_method = output_x_window;
38182d90 4528 f->output_data.x = xzalloc (sizeof *f->output_data.x);
333b20bb 4529 f->output_data.x->icon_bitmap = -1;
be786000 4530 FRAME_FONTSET (f) = -1;
61d461a8
GM
4531 f->output_data.x->scroll_bar_foreground_pixel = -1;
4532 f->output_data.x->scroll_bar_background_pixel = -1;
f15340b7
MB
4533#ifdef USE_TOOLKIT_SCROLL_BARS
4534 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4535 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4536#endif /* USE_TOOLKIT_SCROLL_BARS */
f00af5b1 4537 fset_icon_name (f, Qnil);
333b20bb 4538 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
333b20bb
GM
4539 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4540 f->output_data.x->explicit_parent = 0;
4541
61d461a8
GM
4542 /* These colors will be set anyway later, but it's important
4543 to get the color reference counts right, so initialize them! */
4544 {
4545 Lisp_Object black;
dbf31225 4546 struct gcpro gcpro1;
177c0ea7 4547
9d3d44ba
KL
4548 /* Function x_decode_color can signal an error. Make
4549 sure to initialize color slots so that we won't try
4550 to free colors we haven't allocated. */
ce593f6e
KL
4551 FRAME_FOREGROUND_PIXEL (f) = -1;
4552 FRAME_BACKGROUND_PIXEL (f) = -1;
9d3d44ba
KL
4553 f->output_data.x->cursor_pixel = -1;
4554 f->output_data.x->cursor_foreground_pixel = -1;
4555 f->output_data.x->border_pixel = -1;
4556 f->output_data.x->mouse_pixel = -1;
4557
61d461a8 4558 black = build_string ("black");
dbf31225 4559 GCPRO1 (black);
ce593f6e 4560 FRAME_FOREGROUND_PIXEL (f)
61d461a8 4561 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
ce593f6e 4562 FRAME_BACKGROUND_PIXEL (f)
61d461a8
GM
4563 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4564 f->output_data.x->cursor_pixel
4565 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4566 f->output_data.x->cursor_foreground_pixel
4567 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4568 f->output_data.x->border_pixel
4569 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4570 f->output_data.x->mouse_pixel
4571 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
dbf31225 4572 UNGCPRO;
61d461a8
GM
4573 }
4574
333b20bb
GM
4575 /* Set the name; the functions to which we pass f expect the name to
4576 be set. */
4577 if (EQ (name, Qunbound) || NILP (name))
4578 {
f00af5b1 4579 fset_name (f, build_string (dpyinfo->x_id_name));
333b20bb
GM
4580 f->explicit_name = 0;
4581 }
4582 else
4583 {
f00af5b1 4584 fset_name (f, name);
333b20bb
GM
4585 f->explicit_name = 1;
4586 /* use the frame's title when getting resources for this frame. */
4587 specbind (Qx_resource_name, name);
4588 }
4589
bf970f2b 4590 register_font_driver (&xfont_driver, f);
02ed2ea8
KH
4591#ifdef HAVE_FREETYPE
4592#ifdef HAVE_XFT
b51238f5 4593 register_font_driver (&xftfont_driver, f);
02ed2ea8 4594#else /* not HAVE_XFT */
b51238f5 4595 register_font_driver (&ftxfont_driver, f);
02ed2ea8
KH
4596#endif /* not HAVE_XFT */
4597#endif /* HAVE_FREETYPE */
d43f6b74 4598
b51238f5
KH
4599 x_default_parameter (f, parms, Qfont_backend, Qnil,
4600 "fontBackend", "FontBackend", RES_TYPE_STRING);
02ed2ea8 4601
eaf1eea9
GM
4602 /* Extract the window parameters from the supplied values that are
4603 needed to determine window geometry. */
b51238f5 4604 x_default_font_parameter (f, parms);
333b20bb 4605
4bef8d26 4606 x_default_parameter (f, parms, Qborder_width, make_number (0),
333b20bb 4607 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
177c0ea7 4608
333b20bb
GM
4609 /* This defaults to 2 in order to match xterm. We recognize either
4610 internalBorderWidth or internalBorder (which is what xterm calls
4611 it). */
4612 if (NILP (Fassq (Qinternal_border_width, parms)))
4613 {
4614 Lisp_Object value;
4615
4616 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4617 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4618 if (! EQ (value, Qunbound))
4619 parms = Fcons (Fcons (Qinternal_border_width, value),
4620 parms);
4621 }
4622
4623 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4624 "internalBorderWidth", "internalBorderWidth",
4625 RES_TYPE_NUMBER);
4626
4627 /* Also do the stuff which must be set before the window exists. */
4628 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4629 "foreground", "Foreground", RES_TYPE_STRING);
4630 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4631 "background", "Background", RES_TYPE_STRING);
4632 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4633 "pointerColor", "Foreground", RES_TYPE_STRING);
4634 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4635 "cursorColor", "Foreground", RES_TYPE_STRING);
4636 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4637 "borderColor", "BorderColor", RES_TYPE_STRING);
4638
e509cfa6 4639#ifdef GLYPH_DEBUG
c9e7db78
JD
4640 image_cache_refcount =
4641 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4642 dpyinfo_refcount = dpyinfo->reference_count;
4643#endif /* GLYPH_DEBUG */
4644
333b20bb
GM
4645 /* Init faces before x_default_parameter is called for scroll-bar
4646 parameters because that function calls x_set_scroll_bar_width,
4647 which calls change_frame_size, which calls Fset_window_buffer,
4648 which runs hooks, which call Fvertical_motion. At the end, we
4649 end up in init_iterator with a null face cache, which should not
4650 happen. */
4651 init_frame_faces (f);
177c0ea7 4652
333b20bb 4653 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
333b20bb 4654
0ce7538d 4655 x_figure_window_size (f, parms, 0);
333b20bb 4656
333b20bb
GM
4657 {
4658 XSetWindowAttributes attrs;
4659 unsigned long mask;
36acb2a7 4660 Atom type = FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
177c0ea7 4661
4d7e6e51 4662 block_input ();
c51d2b5e
GM
4663 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4664 if (DoesSaveUnders (dpyinfo->screen))
4665 mask |= CWSaveUnder;
177c0ea7 4666
9b2956e2
GM
4667 /* Window managers look at the override-redirect flag to determine
4668 whether or net to give windows a decoration (Xlib spec, chapter
333b20bb
GM
4669 3.2.8). */
4670 attrs.override_redirect = True;
4671 attrs.save_under = True;
4672 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4673 /* Arrange for getting MapNotify and UnmapNotify events. */
4674 attrs.event_mask = StructureNotifyMask;
4675 tip_window
4676 = FRAME_X_WINDOW (f)
4677 = XCreateWindow (FRAME_X_DISPLAY (f),
4678 FRAME_X_DISPLAY_INFO (f)->root_window,
4679 /* x, y, width, height */
4680 0, 0, 1, 1,
4681 /* Border. */
a979dde1 4682 f->border_width,
333b20bb
GM
4683 CopyFromParent, InputOutput, CopyFromParent,
4684 mask, &attrs);
36acb2a7
JD
4685 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
4686 FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type,
4687 XA_ATOM, 32, PropModeReplace,
4688 (unsigned char *)&type, 1);
4d7e6e51 4689 unblock_input ();
333b20bb
GM
4690 }
4691
4692 x_make_gc (f);
4693
333b20bb
GM
4694 x_default_parameter (f, parms, Qauto_raise, Qnil,
4695 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4696 x_default_parameter (f, parms, Qauto_lower, Qnil,
4697 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4698 x_default_parameter (f, parms, Qcursor_type, Qbox,
4699 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4700
be786000 4701 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
333b20bb 4702 Change will not be effected unless different from the current
be786000
KS
4703 FRAME_LINES (f). */
4704 width = FRAME_COLS (f);
4705 height = FRAME_LINES (f);
4706 SET_FRAME_COLS (f, 0);
4707 FRAME_LINES (f) = 0;
8938a4fb 4708 change_frame_size (f, height, width, 1, 0, 0);
177c0ea7 4709
cd1d850f 4710 /* Add `tooltip' frame parameter's default value. */
54ee7410
CY
4711 if (NILP (Fframe_parameter (frame, Qtooltip)))
4712 Fmodify_frame_parameters (frame, Fcons (Fcons (Qtooltip, Qt), Qnil));
177c0ea7 4713
cedb4d8b
GM
4714 /* FIXME - can this be done in a similar way to normal frames?
4715 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
4716
35fdaa62
GM
4717 /* Set the `display-type' frame parameter before setting up faces. */
4718 {
4719 Lisp_Object disptype;
4720
4721 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
4722 disptype = intern ("mono");
219ccf1b
RS
4723 else if (FRAME_X_DISPLAY_INFO (f)->visual->class == GrayScale
4724 || FRAME_X_DISPLAY_INFO (f)->visual->class == StaticGray)
35fdaa62
GM
4725 disptype = intern ("grayscale");
4726 else
4727 disptype = intern ("color");
4728
4729 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
4730 Fmodify_frame_parameters (frame, Fcons (Fcons (Qdisplay_type, disptype),
4731 Qnil));
4732 }
4733
035d5114 4734 /* Set up faces after all frame parameters are known. This call
6801a572
GM
4735 also merges in face attributes specified for new frames.
4736
4737 Frame parameters may be changed if .Xdefaults contains
4738 specifications for the default font. For example, if there is an
4739 `Emacs.default.attributeBackground: pink', the `background-color'
4740 attribute of the frame get's set, which let's the internal border
4741 of the tooltip frame appear in pink. Prevent this. */
4742 {
4743 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
4744
4745 /* Set tip_frame here, so that */
4746 tip_frame = frame;
45cebfd9 4747 call2 (Qface_set_after_frame_default, frame, Qnil);
177c0ea7 4748
6801a572
GM
4749 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
4750 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
4751 Qnil));
4752 }
177c0ea7 4753
333b20bb
GM
4754 f->no_split = 1;
4755
4756 UNGCPRO;
4757
c9e7db78
JD
4758 /* Now that the frame will be official, it counts as a reference to
4759 its display and terminal. */
4760 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4761 f->terminal->reference_count++;
4762
333b20bb
GM
4763 /* It is now ok to make the frame official even if we get an error
4764 below. And the frame needs to be on Vframe_list or making it
4765 visible won't work. */
4766 Vframe_list = Fcons (frame, Vframe_list);
4767
333b20bb 4768
06d62053
GM
4769 /* Setting attributes of faces of the tooltip frame from resources
4770 and similar will increment face_change_count, which leads to the
4771 clearing of all current matrices. Since this isn't necessary
4772 here, avoid it by resetting face_change_count to the value it
4773 had before we created the tip frame. */
4774 face_change_count = face_change_count_before;
4775
eaf1eea9 4776 /* Discard the unwind_protect. */
333b20bb
GM
4777 return unbind_to (count, frame);
4778}
4779
4780
06d62053
GM
4781/* Compute where to display tip frame F. PARMS is the list of frame
4782 parameters for F. DX and DY are specified offsets from the current
ab452f99
GM
4783 location of the mouse. WIDTH and HEIGHT are the width and height
4784 of the tooltip. Return coordinates relative to the root window of
4785 the display in *ROOT_X, and *ROOT_Y. */
06d62053
GM
4786
4787static void
971de7fb 4788compute_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
4789{
4790 Lisp_Object left, top;
4791 int win_x, win_y;
4792 Window root, child;
4793 unsigned pmask;
177c0ea7 4794
06d62053
GM
4795 /* User-specified position? */
4796 left = Fcdr (Fassq (Qleft, parms));
4797 top = Fcdr (Fassq (Qtop, parms));
177c0ea7 4798
06d62053
GM
4799 /* Move the tooltip window where the mouse pointer is. Resize and
4800 show it. */
570d22b0 4801 if (!INTEGERP (left) || !INTEGERP (top))
ab452f99 4802 {
4d7e6e51 4803 block_input ();
ab452f99
GM
4804 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
4805 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
4d7e6e51 4806 unblock_input ();
ab452f99 4807 }
06d62053 4808
06d62053
GM
4809 if (INTEGERP (top))
4810 *root_y = XINT (top);
bf63eb69
JD
4811 else if (*root_y + XINT (dy) <= 0)
4812 *root_y = 0; /* Can happen for negative dy */
abe9d29c
CY
4813 else if (*root_y + XINT (dy) + height
4814 <= x_display_pixel_height (FRAME_X_DISPLAY_INFO (f)))
7e8410d1 4815 /* It fits below the pointer */
abe9d29c 4816 *root_y += XINT (dy);
7e8410d1
JD
4817 else if (height + XINT (dy) <= *root_y)
4818 /* It fits above the pointer. */
4819 *root_y -= height + XINT (dy);
4820 else
4821 /* Put it on the top. */
4822 *root_y = 0;
ab452f99
GM
4823
4824 if (INTEGERP (left))
4825 *root_x = XINT (left);
bf63eb69
JD
4826 else if (*root_x + XINT (dx) <= 0)
4827 *root_x = 0; /* Can happen for negative dx */
abe9d29c
CY
4828 else if (*root_x + XINT (dx) + width
4829 <= x_display_pixel_width (FRAME_X_DISPLAY_INFO (f)))
d682d3df
RS
4830 /* It fits to the right of the pointer. */
4831 *root_x += XINT (dx);
4832 else if (width + XINT (dx) <= *root_x)
4833 /* It fits to the left of the pointer. */
ab452f99
GM
4834 *root_x -= width + XINT (dx);
4835 else
d682d3df
RS
4836 /* Put it left-justified on the screen--it ought to fit that way. */
4837 *root_x = 0;
06d62053
GM
4838}
4839
4840
0634ce98 4841DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
7ee72033 4842 doc: /* Show STRING in a "tooltip" window on frame FRAME.
c061c855
GM
4843A tooltip window is a small X window displaying a string.
4844
87c891c7
EZ
4845This is an internal function; Lisp code should call `tooltip-show'.
4846
c061c855
GM
4847FRAME nil or omitted means use the selected frame.
4848
4849PARMS is an optional list of frame parameters which can be used to
4850change the tooltip's appearance.
4851
4852Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
4853means use the default timeout of 5 seconds.
4854
c869cc37 4855If the list of frame parameters PARMS contains a `left' parameters,
c061c855
GM
4856the tooltip is displayed at that x-position. Otherwise it is
4857displayed at the mouse position, with offset DX added (default is 5 if
4858DX isn't specified). Likewise for the y-position; if a `top' frame
4859parameter is specified, it determines the y-position of the tooltip
4860window, otherwise it is displayed at the mouse position, with offset
4861DY added (default is -10).
4862
4863A tooltip's maximum size is specified by `x-max-tooltip-size'.
7ee72033 4864Text larger than the specified size is clipped. */)
5842a27b 4865 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
333b20bb
GM
4866{
4867 struct frame *f;
4868 struct window *w;
06d62053 4869 int root_x, root_y;
333b20bb
GM
4870 struct buffer *old_buffer;
4871 struct text_pos pos;
a971c0a7 4872 int i, width, height, seen_reversed_p;
393f2d14 4873 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
333b20bb 4874 int old_windows_or_buffers_changed = windows_or_buffers_changed;
d311d28c 4875 ptrdiff_t count = SPECPDL_INDEX ();
177c0ea7 4876
333b20bb
GM
4877 specbind (Qinhibit_redisplay, Qt);
4878
393f2d14 4879 GCPRO4 (string, parms, frame, timeout);
333b20bb 4880
b7826503 4881 CHECK_STRING (string);
2d7117fe
JD
4882 if (SCHARS (string) == 0)
4883 string = make_unibyte_string (" ", 1);
4884
7452b7bd 4885 f = decode_window_system_frame (frame);
333b20bb
GM
4886 if (NILP (timeout))
4887 timeout = make_number (5);
4888 else
b7826503 4889 CHECK_NATNUM (timeout);
177c0ea7 4890
0634ce98
GM
4891 if (NILP (dx))
4892 dx = make_number (5);
4893 else
b7826503 4894 CHECK_NUMBER (dx);
177c0ea7 4895
0634ce98 4896 if (NILP (dy))
12c67a7f 4897 dy = make_number (-10);
0634ce98 4898 else
b7826503 4899 CHECK_NUMBER (dy);
333b20bb 4900
aa1859f5
J
4901#ifdef USE_GTK
4902 if (x_gtk_use_system_tooltips)
4903 {
18e27ea8 4904 bool ok;
aa1859f5
J
4905
4906 /* Hide a previous tip, if any. */
4907 Fx_hide_tip ();
4908
4d7e6e51 4909 block_input ();
18e27ea8
PE
4910 ok = xg_prepare_tooltip (f, string, &width, &height);
4911 if (ok)
aa1859f5
J
4912 {
4913 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
4914 xg_show_tooltip (f, root_x, root_y);
4915 /* This is used in Fx_hide_tip. */
4916 XSETFRAME (tip_frame, f);
4917 }
4d7e6e51 4918 unblock_input ();
aa1859f5
J
4919 if (ok) goto start_timer;
4920 }
4921#endif /* USE_GTK */
4922
06d62053
GM
4923 if (NILP (last_show_tip_args))
4924 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
4925
4926 if (!NILP (tip_frame))
4927 {
4928 Lisp_Object last_string = AREF (last_show_tip_args, 0);
4929 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
4930 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
4931
4932 if (EQ (frame, last_frame)
4933 && !NILP (Fequal (last_string, string))
4934 && !NILP (Fequal (last_parms, parms)))
4935 {
58d2d479 4936 struct frame *tip_f = XFRAME (tip_frame);
177c0ea7 4937
06d62053
GM
4938 /* Only DX and DY have changed. */
4939 if (!NILP (tip_timer))
ae782866
GM
4940 {
4941 Lisp_Object timer = tip_timer;
4942 tip_timer = Qnil;
4943 call1 (Qcancel_timer, timer);
4944 }
06d62053 4945
4d7e6e51 4946 block_input ();
58d2d479
PE
4947 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
4948 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
4949 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
ab452f99 4950 root_x, root_y);
4d7e6e51 4951 unblock_input ();
06d62053
GM
4952 goto start_timer;
4953 }
4954 }
4955
333b20bb
GM
4956 /* Hide a previous tip, if any. */
4957 Fx_hide_tip ();
4958
06d62053
GM
4959 ASET (last_show_tip_args, 0, string);
4960 ASET (last_show_tip_args, 1, frame);
4961 ASET (last_show_tip_args, 2, parms);
4962
333b20bb
GM
4963 /* Add default values to frame parameters. */
4964 if (NILP (Fassq (Qname, parms)))
4965 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
4966 if (NILP (Fassq (Qinternal_border_width, parms)))
4967 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
4968 if (NILP (Fassq (Qborder_width, parms)))
4969 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
4970 if (NILP (Fassq (Qborder_color, parms)))
4971 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
4972 if (NILP (Fassq (Qbackground_color, parms)))
4973 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
4974 parms);
4975
4976 /* Create a frame for the tooltip, and record it in the global
4977 variable tip_frame. */
275841bf 4978 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
44b5a125 4979 f = XFRAME (frame);
333b20bb 4980
d63931a2 4981 /* Set up the frame's root window. */
333b20bb 4982 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5f24fa51
DA
4983 w->left_col = 0;
4984 w->top_line = 0;
177c0ea7 4985
d63931a2 4986 if (CONSP (Vx_max_tooltip_size)
d311d28c
PE
4987 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
4988 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
d63931a2 4989 {
5f24fa51
DA
4990 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
4991 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
d63931a2
GM
4992 }
4993 else
4994 {
5f24fa51
DA
4995 w->total_cols = 80;
4996 w->total_lines = 40;
d63931a2 4997 }
177c0ea7 4998
5f24fa51 4999 FRAME_TOTAL_COLS (f) = w->total_cols;
333b20bb
GM
5000 adjust_glyphs (f);
5001 w->pseudo_window_p = 1;
5002
5003 /* Display the tooltip text in a temporary buffer. */
333b20bb 5004 old_buffer = current_buffer;
e74aeda8 5005 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
39eb03f1 5006 bset_truncate_lines (current_buffer, Qnil);
5634ff85
YM
5007 clear_glyph_matrix (w->desired_matrix);
5008 clear_glyph_matrix (w->current_matrix);
5009 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
6eff5c3d 5010 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
333b20bb
GM
5011
5012 /* Compute width and height of the tooltip. */
a971c0a7 5013 width = height = seen_reversed_p = 0;
333b20bb
GM
5014 for (i = 0; i < w->desired_matrix->nrows; ++i)
5015 {
5016 struct glyph_row *row = &w->desired_matrix->rows[i];
5017 struct glyph *last;
5018 int row_width;
5019
5020 /* Stop at the first empty row at the end. */
138c0ae8 5021 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
333b20bb
GM
5022 break;
5023
d7bf0342
GM
5024 /* Let the row go over the full width of the frame. */
5025 row->full_width_p = 1;
333b20bb 5026
5b253e9c 5027 row_width = row->pixel_width;
333b20bb
GM
5028 if (row->used[TEXT_AREA])
5029 {
a971c0a7
EZ
5030 /* There's a glyph at the end of rows that is used to place
5031 the cursor there. Don't include the width of this glyph. */
5032 if (!row->reversed_p)
5033 {
5034 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5035 if (INTEGERP (last->object))
5036 row_width -= last->pixel_width;
5037 }
5038 else
5039 {
5040 /* There could be a stretch glyph at the beginning of R2L
5041 rows that is produced by extend_face_to_end_of_line.
5042 Don't count that glyph. */
5043 struct glyph *g = row->glyphs[TEXT_AREA];
5044
5045 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5046 {
5047 row_width -= g->pixel_width;
5048 seen_reversed_p = 1;
5049 }
5050 }
333b20bb 5051 }
177c0ea7 5052
333b20bb
GM
5053 height += row->height;
5054 width = max (width, row_width);
5055 }
5056
a971c0a7
EZ
5057 /* If we've seen partial-length R2L rows, we need to re-adjust the
5058 tool-tip frame width and redisplay it again, to avoid over-wide
5059 tips due to the stretch glyph that extends R2L lines to full
5060 width of the frame. */
5061 if (seen_reversed_p)
5062 {
5063 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5064 not in pixels. */
5065 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5f24fa51 5066 w->total_cols = width;
a971c0a7
EZ
5067 FRAME_TOTAL_COLS (f) = width;
5068 adjust_glyphs (f);
5069 clear_glyph_matrix (w->desired_matrix);
5070 clear_glyph_matrix (w->current_matrix);
5071 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5072 width = height = 0;
5073 /* Recompute width and height of the tooltip. */
5074 for (i = 0; i < w->desired_matrix->nrows; ++i)
5075 {
5076 struct glyph_row *row = &w->desired_matrix->rows[i];
5077 struct glyph *last;
5078 int row_width;
5079
138c0ae8 5080 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
a971c0a7
EZ
5081 break;
5082 row->full_width_p = 1;
5083 row_width = row->pixel_width;
5084 if (row->used[TEXT_AREA] && !row->reversed_p)
5085 {
5086 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5087 if (INTEGERP (last->object))
5088 row_width -= last->pixel_width;
5089 }
5090
5091 height += row->height;
5092 width = max (width, row_width);
5093 }
5094 }
5095
333b20bb
GM
5096 /* Add the frame's internal border to the width and height the X
5097 window should have. */
5098 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5099 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5100
5101 /* Move the tooltip window where the mouse pointer is. Resize and
5102 show it. */
ab452f99 5103 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
0634ce98 5104
4d7e6e51 5105 block_input ();
333b20bb 5106 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
ab452f99 5107 root_x, root_y, width, height);
333b20bb 5108 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4d7e6e51 5109 unblock_input ();
177c0ea7 5110
333b20bb
GM
5111 /* Draw into the window. */
5112 w->must_be_updated_p = 1;
5113 update_single_window (w, 1);
5114
5115 /* Restore original current buffer. */
5116 set_buffer_internal_1 (old_buffer);
5117 windows_or_buffers_changed = old_windows_or_buffers_changed;
5118
06d62053 5119 start_timer:
333b20bb
GM
5120 /* Let the tip disappear after timeout seconds. */
5121 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5122 intern ("x-hide-tip"));
a744a2ec
DL
5123
5124 UNGCPRO;
333b20bb
GM
5125 return unbind_to (count, Qnil);
5126}
5127
5128
a7ca3326 5129DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
7ee72033
MB
5130 doc: /* Hide the current tooltip window, if there is any.
5131Value is t if tooltip was open, nil otherwise. */)
5842a27b 5132 (void)
333b20bb 5133{
d311d28c 5134 ptrdiff_t count;
c0006262
GM
5135 Lisp_Object deleted, frame, timer;
5136 struct gcpro gcpro1, gcpro2;
44b5a125
GM
5137
5138 /* Return quickly if nothing to do. */
c0006262 5139 if (NILP (tip_timer) && NILP (tip_frame))
44b5a125 5140 return Qnil;
177c0ea7 5141
c0006262
GM
5142 frame = tip_frame;
5143 timer = tip_timer;
5144 GCPRO2 (frame, timer);
5145 tip_frame = tip_timer = deleted = Qnil;
177c0ea7 5146
331379bf 5147 count = SPECPDL_INDEX ();
333b20bb 5148 specbind (Qinhibit_redisplay, Qt);
44b5a125 5149 specbind (Qinhibit_quit, Qt);
177c0ea7 5150
c0006262 5151 if (!NILP (timer))
ae782866 5152 call1 (Qcancel_timer, timer);
333b20bb 5153
aa1859f5 5154#ifdef USE_GTK
1fe72bf8
PE
5155 {
5156 /* When using system tooltip, tip_frame is the Emacs frame on which
5157 the tip is shown. */
5158 struct frame *f = XFRAME (frame);
5159 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5160 frame = Qnil;
5161 }
aa1859f5
J
5162#endif
5163
c0006262 5164 if (FRAMEP (frame))
333b20bb 5165 {
56f2de10 5166 delete_frame (frame, Qnil);
44b5a125 5167 deleted = Qt;
f6c44811
GM
5168
5169#ifdef USE_LUCID
5170 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5171 redisplay procedure is not called when a tip frame over menu
5172 items is unmapped. Redisplay the menu manually... */
5173 {
aa1859f5 5174 Widget w;
1fe72bf8 5175 struct frame *f = SELECTED_FRAME ();
aa1859f5 5176 w = f->output_data.x->menubar_widget;
9180dc8c 5177
f6c44811 5178 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
dbc64aa7 5179 && w != NULL)
f6c44811 5180 {
4d7e6e51 5181 block_input ();
f6c44811 5182 xlwmenu_redisplay (w);
4d7e6e51 5183 unblock_input ();
f6c44811
GM
5184 }
5185 }
5186#endif /* USE_LUCID */
333b20bb
GM
5187 }
5188
c0006262 5189 UNGCPRO;
44b5a125 5190 return unbind_to (count, deleted);
333b20bb
GM
5191}
5192
5193
5194\f
5195/***********************************************************************
5196 File selection dialog
5197 ***********************************************************************/
5198
90f2e16b
JD
5199DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5200 Sx_uses_old_gtk_dialog,
5201 0, 0, 0,
5202 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5842a27b 5203 (void)
90f2e16b
JD
5204{
5205#ifdef USE_GTK
90f2e16b
JD
5206 if (use_dialog_box
5207 && use_file_dialog
7452b7bd 5208 && window_system_available (SELECTED_FRAME ())
90f2e16b
JD
5209 && xg_uses_old_file_dialog ())
5210 return Qt;
5211#endif
5212 return Qnil;
5213}
5214
333b20bb 5215
90f2e16b 5216#ifdef USE_MOTIF
333b20bb
GM
5217/* Callback for "OK" and "Cancel" on file selection dialog. */
5218
5219static void
dee186b6 5220file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
333b20bb
GM
5221{
5222 int *result = (int *) client_data;
5223 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5224 *result = cb->reason;
5225}
5226
5227
a779d213
GM
5228/* Callback for unmapping a file selection dialog. This is used to
5229 capture the case where a dialog is closed via a window manager's
5230 closer button, for example. Using a XmNdestroyCallback didn't work
5231 in this case. */
5232
5233static void
dee186b6 5234file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
a779d213
GM
5235{
5236 int *result = (int *) client_data;
5237 *result = XmCR_CANCEL;
5238}
5239
24ca7c5a 5240static Lisp_Object
dee186b6 5241clean_up_file_dialog (Lisp_Object arg)
24ca7c5a 5242{
2b30549c 5243 Widget dialog = XSAVE_POINTER (arg, 0);
24ca7c5a
JD
5244
5245 /* Clean up. */
4d7e6e51 5246 block_input ();
24ca7c5a
JD
5247 XtUnmanageChild (dialog);
5248 XtDestroyWidget (dialog);
5249 x_menu_set_in_use (0);
4d7e6e51 5250 unblock_input ();
24ca7c5a
JD
5251
5252 return Qnil;
5253}
5254
a779d213 5255
f9d64bb3 5256DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
7ee72033 5257 doc: /* Read file name, prompting with PROMPT in directory DIR.
f9d64bb3
JD
5258Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5259selection box, if specified. If MUSTMATCH is non-nil, the returned file
7c051dd8
GM
5260or directory must exist.
5261
d7e642cc 5262This function is only defined on NS, MS Windows, and X Windows with the
7c051dd8
GM
5263Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5264Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5842a27b 5265 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
333b20bb
GM
5266{
5267 int result;
0fe92f72 5268 struct frame *f = SELECTED_FRAME ();
333b20bb 5269 Lisp_Object file = Qnil;
a8f4bdc6 5270 Lisp_Object decoded_file;
f9d64bb3 5271 Widget dialog, text, help;
333b20bb
GM
5272 Arg al[10];
5273 int ac = 0;
333b20bb 5274 XmString dir_xmstring, pattern_xmstring;
d311d28c 5275 ptrdiff_t count = SPECPDL_INDEX ();
f9d64bb3 5276 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
333b20bb 5277
7452b7bd 5278 check_window_system (f);
6eeee4d6 5279
f9d64bb3 5280 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
24ca7c5a 5281
5ae53dcf 5282 if (popup_activated ())
24ca7c5a
JD
5283 error ("Trying to use a menu from within a menu-entry");
5284
b7826503
PJ
5285 CHECK_STRING (prompt);
5286 CHECK_STRING (dir);
333b20bb
GM
5287
5288 /* Prevent redisplay. */
5289 specbind (Qinhibit_redisplay, Qt);
5290
4d7e6e51 5291 block_input ();
333b20bb
GM
5292
5293 /* Create the dialog with PROMPT as title, using DIR as initial
5294 directory and using "*" as pattern. */
5295 dir = Fexpand_file_name (dir, Qnil);
c09bfb2f 5296 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
333b20bb 5297 pattern_xmstring = XmStringCreateLocalized ("*");
177c0ea7 5298
d5db4077 5299 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
333b20bb
GM
5300 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5301 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5302 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5303 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5304 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5305 "fsb", al, ac);
5306 XmStringFree (dir_xmstring);
5307 XmStringFree (pattern_xmstring);
5308
5309 /* Add callbacks for OK and Cancel. */
5310 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5311 (XtPointer) &result);
5312 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5313 (XtPointer) &result);
a779d213
GM
5314 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5315 (XtPointer) &result);
333b20bb 5316
f9d64bb3 5317 /* Remove the help button since we can't display help. */
333b20bb 5318 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
f9d64bb3 5319 XtUnmanageChild (help);
333b20bb 5320
177c0ea7 5321 /* Mark OK button as default. */
333b20bb
GM
5322 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5323 XmNshowAsDefault, True, NULL);
5324
5325 /* If MUSTMATCH is non-nil, disable the file entry field of the
5326 dialog, so that the user must select a file from the files list
5327 box. We can't remove it because we wouldn't have a way to get at
5328 the result file name, then. */
5329 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5330 if (!NILP (mustmatch))
5331 {
5332 Widget label;
5333 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5334 XtSetSensitive (text, False);
5335 XtSetSensitive (label, False);
5336 }
5337
5338 /* Manage the dialog, so that list boxes get filled. */
5339 XtManageChild (dialog);
5340
333b20bb
GM
5341 if (STRINGP (default_filename))
5342 {
5343 XmString default_xmstring;
f9d64bb3
JD
5344 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5345 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
333b20bb 5346
f9d64bb3
JD
5347 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5348 XmTextFieldReplace (wtext, 0, last_pos,
c09bfb2f 5349 (SSDATA (Ffile_name_nondirectory (default_filename))));
333b20bb 5350
f9d64bb3
JD
5351 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5352 must include the path for this to work. */
5353
c09bfb2f 5354 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
333b20bb 5355
f9d64bb3
JD
5356 if (XmListItemExists (list, default_xmstring))
5357 {
5358 int item_pos = XmListItemPos (list, default_xmstring);
5359 /* Select the item and scroll it into view. */
5360 XmListSelectPos (list, item_pos, True);
5361 XmListSetPos (list, item_pos);
5362 }
5363
5364 XmStringFree (default_xmstring);
333b20bb
GM
5365 }
5366
468afbac 5367 record_unwind_protect (clean_up_file_dialog, make_save_pointer (dialog));
24ca7c5a 5368
bf338245 5369 /* Process events until the user presses Cancel or OK. */
5ae53dcf 5370 x_menu_set_in_use (1);
03100098 5371 result = 0;
a779d213 5372 while (result == 0)
563b384d 5373 {
bf338245 5374 XEvent event;
24ca7c5a 5375 x_menu_wait_for_event (0);
bf338245 5376 XtAppNextEvent (Xt_app_con, &event);
9f6fcdc5
JD
5377 if (event.type == KeyPress
5378 && FRAME_X_DISPLAY (f) == event.xkey.display)
5379 {
5380 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5381
5382 /* Pop down on C-g. */
5383 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5384 XtUnmanageChild (dialog);
5385 }
5b3f8550 5386
24ca7c5a 5387 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
563b384d 5388 }
03100098 5389
333b20bb
GM
5390 /* Get the result. */
5391 if (result == XmCR_OK)
5392 {
91a3e27b 5393 XmString text_string;
333b20bb 5394 String data;
177c0ea7 5395
91a3e27b
PE
5396 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5397 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5398 XmStringFree (text_string);
333b20bb
GM
5399 file = build_string (data);
5400 XtFree (data);
5401 }
5402 else
5403 file = Qnil;
5404
4d7e6e51 5405 unblock_input ();
333b20bb
GM
5406 UNGCPRO;
5407
5408 /* Make "Cancel" equivalent to C-g. */
5409 if (NILP (file))
5410 Fsignal (Qquit, Qnil);
177c0ea7 5411
a8f4bdc6
JD
5412 decoded_file = DECODE_FILE (file);
5413
5414 return unbind_to (count, decoded_file);
333b20bb
GM
5415}
5416
5417#endif /* USE_MOTIF */
5418
488dd4c4
JD
5419#ifdef USE_GTK
5420
24ca7c5a 5421static Lisp_Object
971de7fb 5422clean_up_dialog (Lisp_Object arg)
24ca7c5a
JD
5423{
5424 x_menu_set_in_use (0);
5425
5426 return Qnil;
5427}
5428
f9d64bb3
JD
5429DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5430 doc: /* Read file name, prompting with PROMPT in directory DIR.
5431Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5432selection box, if specified. If MUSTMATCH is non-nil, the returned file
7c051dd8
GM
5433or directory must exist.
5434
d7e642cc 5435This function is only defined on NS, MS Windows, and X Windows with the
7c051dd8
GM
5436Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5437Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5842a27b 5438 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
488dd4c4
JD
5439{
5440 FRAME_PTR f = SELECTED_FRAME ();
5441 char *fn;
5442 Lisp_Object file = Qnil;
a8f4bdc6 5443 Lisp_Object decoded_file;
d311d28c 5444 ptrdiff_t count = SPECPDL_INDEX ();
f9d64bb3 5445 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
488dd4c4 5446 char *cdef_file;
177c0ea7 5447
7452b7bd 5448 check_window_system (f);
6eeee4d6 5449
f9d64bb3 5450 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
24ca7c5a 5451
5ae53dcf 5452 if (popup_activated ())
24ca7c5a
JD
5453 error ("Trying to use a menu from within a menu-entry");
5454
488dd4c4
JD
5455 CHECK_STRING (prompt);
5456 CHECK_STRING (dir);
5457
5458 /* Prevent redisplay. */
5459 specbind (Qinhibit_redisplay, Qt);
24ca7c5a 5460 record_unwind_protect (clean_up_dialog, Qnil);
488dd4c4 5461
4d7e6e51 5462 block_input ();
488dd4c4
JD
5463
5464 if (STRINGP (default_filename))
c0951e53 5465 cdef_file = SSDATA (default_filename);
488dd4c4 5466 else
c0951e53 5467 cdef_file = SSDATA (dir);
488dd4c4 5468
c0951e53 5469 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
f9d64bb3
JD
5470 ! NILP (mustmatch),
5471 ! NILP (only_dir_p));
177c0ea7 5472
488dd4c4
JD
5473 if (fn)
5474 {
5475 file = build_string (fn);
5476 xfree (fn);
5477 }
5478
4d7e6e51 5479 unblock_input ();
488dd4c4
JD
5480 UNGCPRO;
5481
5482 /* Make "Cancel" equivalent to C-g. */
5483 if (NILP (file))
5484 Fsignal (Qquit, Qnil);
177c0ea7 5485
a8f4bdc6
JD
5486 decoded_file = DECODE_FILE (file);
5487
5488 return unbind_to (count, decoded_file);
488dd4c4
JD
5489}
5490
9bf80974
CY
5491
5492#ifdef HAVE_FREETYPE
5493
f82605c6 5494DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
f2045622
CY
5495 doc: /* Read a font using a GTK dialog.
5496Return either a font spec (for GTK versions >= 3.2) or a string
5497containing a GTK-style font name.
f82605c6 5498
f2045622
CY
5499FRAME is the frame on which to pop up the font chooser. If omitted or
5500nil, it defaults to the selected frame. */)
5842a27b 5501 (Lisp_Object frame, Lisp_Object ignored)
9bf80974 5502{
7452b7bd 5503 FRAME_PTR f = decode_window_system_frame (frame);
637fa988 5504 Lisp_Object font;
e0ab5fcf
JD
5505 Lisp_Object font_param;
5506 char *default_name = NULL;
637fa988 5507 struct gcpro gcpro1, gcpro2;
d311d28c 5508 ptrdiff_t count = SPECPDL_INDEX ();
9bf80974 5509
9bf80974
CY
5510 if (popup_activated ())
5511 error ("Trying to use a menu from within a menu-entry");
5512
5513 /* Prevent redisplay. */
5514 specbind (Qinhibit_redisplay, Qt);
5515 record_unwind_protect (clean_up_dialog, Qnil);
5516
4d7e6e51 5517 block_input ();
9bf80974 5518
5e617bc2 5519 GCPRO2 (font_param, font);
e0ab5fcf 5520
637fa988 5521 XSETFONT (font, FRAME_FONT (f));
581e51e8 5522 font_param = Ffont_get (font, intern (":name"));
637fa988 5523 if (STRINGP (font_param))
c0951e53 5524 default_name = xstrdup (SSDATA (font_param));
f5f6c0e0 5525 else
53c7540d 5526 {
637fa988
JD
5527 font_param = Fframe_parameter (frame, Qfont_param);
5528 if (STRINGP (font_param))
c0951e53 5529 default_name = xstrdup (SSDATA (font_param));
53c7540d 5530 }
e0ab5fcf 5531
f2045622 5532 font = xg_get_font (f, default_name);
581e51e8 5533 xfree (default_name);
9bf80974 5534
4d7e6e51 5535 unblock_input ();
9bf80974
CY
5536
5537 if (NILP (font))
5538 Fsignal (Qquit, Qnil);
5539
5540 return unbind_to (count, font);
5541}
5542#endif /* HAVE_FREETYPE */
5543
488dd4c4 5544#endif /* USE_GTK */
333b20bb
GM
5545
5546\f
82bab41c
GM
5547/***********************************************************************
5548 Keyboard
5549 ***********************************************************************/
5550
afeee3e5 5551#ifdef HAVE_XKB
82bab41c
GM
5552#include <X11/XKBlib.h>
5553#include <X11/keysym.h>
5554#endif
5555
5556DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5557 Sx_backspace_delete_keys_p, 0, 1, 0,
7ee72033 5558 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
c061c855
GM
5559FRAME nil means use the selected frame.
5560Value is t if we know that both keys are present, and are mapped to the
20d1c5c5
DN
5561usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5562present and mapped to the usual X keysyms. */)
5842a27b 5563 (Lisp_Object frame)
82bab41c 5564{
afeee3e5
PE
5565#ifndef HAVE_XKB
5566 return Qlambda;
5567#else
82bab41c 5568 XkbDescPtr kb;
7452b7bd 5569 struct frame *f = decode_window_system_frame (frame);
82bab41c
GM
5570 Display *dpy = FRAME_X_DISPLAY (f);
5571 Lisp_Object have_keys;
58d2d479 5572 int major, minor, op, event, error_code;
82bab41c 5573
4d7e6e51 5574 block_input ();
46f6a258
GM
5575
5576 /* Check library version in case we're dynamically linked. */
5577 major = XkbMajorVersion;
5578 minor = XkbMinorVersion;
5579 if (!XkbLibraryVersion (&major, &minor))
c1efd260 5580 {
4d7e6e51 5581 unblock_input ();
20d1c5c5 5582 return Qlambda;
c1efd260 5583 }
46f6a258
GM
5584
5585 /* Check that the server supports XKB. */
5586 major = XkbMajorVersion;
5587 minor = XkbMinorVersion;
58d2d479 5588 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
c1efd260 5589 {
4d7e6e51 5590 unblock_input ();
20d1c5c5 5591 return Qlambda;
c1efd260 5592 }
177c0ea7 5593
3a441526
JD
5594 /* In this code we check that the keyboard has physical keys with names
5595 that start with BKSP (Backspace) and DELE (Delete), and that they
5596 generate keysym XK_BackSpace and XK_Delete respectively.
5597 This function is used to test if normal-erase-is-backspace should be
5598 turned on.
5599 An alternative approach would be to just check if XK_BackSpace and
5600 XK_Delete are mapped to any key. But if any of those are mapped to
5601 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5602 user doesn't know about it, it is better to return false here.
5603 It is more obvious to the user what to do if she/he has two keys
5604 clearly marked with names/symbols and one key does something not
5605 expected (i.e. she/he then tries the other).
5606 The cases where Backspace/Delete is mapped to some other key combination
5607 are rare, and in those cases, normal-erase-is-backspace can be turned on
5608 manually. */
5609
46f6a258 5610 have_keys = Qnil;
c1efd260 5611 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
82bab41c
GM
5612 if (kb)
5613 {
5614 int delete_keycode = 0, backspace_keycode = 0, i;
c1efd260
GM
5615
5616 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
82bab41c 5617 {
c1efd260
GM
5618 for (i = kb->min_key_code;
5619 (i < kb->max_key_code
5620 && (delete_keycode == 0 || backspace_keycode == 0));
5621 ++i)
5622 {
d63931a2
GM
5623 /* The XKB symbolic key names can be seen most easily in
5624 the PS file generated by `xkbprint -label name
5625 $DISPLAY'. */
72af86bd 5626 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
c1efd260 5627 delete_keycode = i;
72af86bd 5628 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
c1efd260
GM
5629 backspace_keycode = i;
5630 }
5631
5632 XkbFreeNames (kb, 0, True);
82bab41c
GM
5633 }
5634
c1efd260 5635 XkbFreeClientMap (kb, 0, True);
177c0ea7 5636
82bab41c
GM
5637 if (delete_keycode
5638 && backspace_keycode
5639 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5640 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5641 have_keys = Qt;
5642 }
4d7e6e51 5643 unblock_input ();
82bab41c 5644 return have_keys;
afeee3e5 5645#endif
82bab41c
GM
5646}
5647
5648
5649\f
333b20bb
GM
5650/***********************************************************************
5651 Initialization
5652 ***********************************************************************/
5653
4dacadcc 5654/* Keep this list in the same order as frame_parms in frame.c.
7c0d3ed8
KS
5655 Use 0 for unsupported frame parameters. */
5656
5657frame_parm_handler x_frame_parm_handlers[] =
5658{
5659 x_set_autoraise,
5660 x_set_autolower,
5661 x_set_background_color,
5662 x_set_border_color,
5663 x_set_border_width,
5664 x_set_cursor_color,
5665 x_set_cursor_type,
5666 x_set_font,
5667 x_set_foreground_color,
5668 x_set_icon_name,
5669 x_set_icon_type,
5670 x_set_internal_border_width,
5671 x_set_menu_bar_lines,
5672 x_set_mouse_color,
5673 x_explicitly_set_name,
5674 x_set_scroll_bar_width,
5675 x_set_title,
5676 x_set_unsplittable,
5677 x_set_vertical_scroll_bars,
5678 x_set_visibility,
5679 x_set_tool_bar_lines,
5680 x_set_scroll_bar_foreground,
5681 x_set_scroll_bar_background,
5682 x_set_screen_gamma,
5683 x_set_line_spacing,
5684 x_set_fringe_width,
5685 x_set_fringe_width,
5686 x_set_wait_for_wm,
5687 x_set_fullscreen,
271a71c7 5688 x_set_font_backend,
cad9ef74
JD
5689 x_set_alpha,
5690 x_set_sticky,
bfeabdc3 5691 x_set_tool_bar_position,
7c0d3ed8
KS
5692};
5693
333b20bb 5694void
971de7fb 5695syms_of_xfns (void)
333b20bb 5696{
333b20bb
GM
5697 /* The section below is built by the lisp expression at the top of the file,
5698 just above where these variables are declared. */
5699 /*&&& init symbols here &&&*/
cd3520a4
JB
5700 DEFSYM (Qsuppress_icon, "suppress-icon");
5701 DEFSYM (Qundefined_color, "undefined-color");
5702 DEFSYM (Qcompound_text, "compound-text");
5703 DEFSYM (Qcancel_timer, "cancel-timer");
5704 DEFSYM (Qfont_param, "font-parameter");
f9942c9e
JB
5705 /* This is the end of symbol initialization. */
5706
01f1ba30 5707 Fput (Qundefined_color, Qerror_conditions,
3438fe21 5708 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
01f1ba30 5709 Fput (Qundefined_color, Qerror_message,
2a0213a6 5710 build_pure_c_string ("Undefined color"));
01f1ba30 5711
29208e82 5712 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
7ee72033 5713 doc: /* The shape of the pointer when over text.
c061c855
GM
5714Changing the value does not affect existing frames
5715unless you set the mouse color. */);
01f1ba30
JB
5716 Vx_pointer_shape = Qnil;
5717
ca0ecbf5 5718#if 0 /* This doesn't really do anything. */
29208e82 5719 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
7ee72033 5720 doc: /* The shape of the pointer when not over text.
c061c855
GM
5721This variable takes effect when you create a new frame
5722or when you set the mouse color. */);
af01ef26 5723#endif
01f1ba30
JB
5724 Vx_nontext_pointer_shape = Qnil;
5725
29208e82 5726 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
7ee72033 5727 doc: /* The shape of the pointer when Emacs is busy.
c061c855
GM
5728This variable takes effect when you create a new frame
5729or when you set the mouse color. */);
0af913d7 5730 Vx_hourglass_pointer_shape = Qnil;
333b20bb 5731
ca0ecbf5 5732#if 0 /* This doesn't really do anything. */
29208e82 5733 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
7ee72033 5734 doc: /* The shape of the pointer when over the mode line.
c061c855
GM
5735This variable takes effect when you create a new frame
5736or when you set the mouse color. */);
af01ef26 5737#endif
01f1ba30
JB
5738 Vx_mode_pointer_shape = Qnil;
5739
d3b06468 5740 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
29208e82 5741 Vx_sensitive_text_pointer_shape,
7ee72033 5742 doc: /* The shape of the pointer when over mouse-sensitive text.
c061c855
GM
5743This variable takes effect when you create a new frame
5744or when you set the mouse color. */);
ca0ecbf5 5745 Vx_sensitive_text_pointer_shape = Qnil;
95f80c78 5746
8fb4ec9c 5747 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
29208e82 5748 Vx_window_horizontal_drag_shape,
7ee72033 5749 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
c061c855
GM
5750This variable takes effect when you create a new frame
5751or when you set the mouse color. */);
8fb4ec9c
GM
5752 Vx_window_horizontal_drag_shape = Qnil;
5753
29208e82 5754 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
7ee72033 5755 doc: /* A string indicating the foreground color of the cursor box. */);
01f1ba30
JB
5756 Vx_cursor_fore_pixel = Qnil;
5757
29208e82 5758 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
f5f6c0e0
JB
5759 doc: /* Maximum size for tooltips.
5760Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
d63931a2 5761 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
177c0ea7 5762
29208e82 5763 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
7ee72033 5764 doc: /* Non-nil if no X window manager is in use.
c061c855
GM
5765Emacs doesn't try to figure this out; this is always nil
5766unless you set it to something else. */);
2d38195d
RS
5767 /* We don't have any way to find this out, so set it to nil
5768 and maybe the user would like to set it to t. */
5769 Vx_no_window_manager = Qnil;
1d3dac41 5770
942ea06d 5771 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
29208e82 5772 Vx_pixel_size_width_font_regexp,
7ee72033 5773 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
c061c855
GM
5774
5775Since Emacs gets width of a font matching with this regexp from
5776PIXEL_SIZE field of the name, font finding mechanism gets faster for
5777such a font. This is especially effective for such large fonts as
5778Chinese, Japanese, and Korean. */);
942ea06d
KH
5779 Vx_pixel_size_width_font_regexp = Qnil;
5780
255e4140 5781/* This is not ifdef:ed, so other builds than GTK can customize it. */
29208e82 5782 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
fb7ada5f 5783 doc: /* Non-nil means prompt with the old GTK file selection dialog.
255e4140
JD
5784If nil or if the file selection dialog is not available, the new GTK file
5785chooser is used instead. To turn off all file dialogs set the
5786variable `use-file-dialog'. */);
fe2b14b7 5787 x_gtk_use_old_file_dialog = 0;
255e4140 5788
29208e82 5789 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
fb7ada5f 5790 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
a8a35720
JD
5791Note that this is just the default, there is a toggle button on the file
5792chooser to show or not show hidden files on a case by case basis. */);
5793 x_gtk_show_hidden_files = 0;
5794
29208e82 5795 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
fb7ada5f 5796 doc: /* If non-nil, the GTK file chooser will show additional help text.
641c0105
JD
5797If more space for files in the file chooser dialog is wanted, set this to nil
5798to turn the additional text off. */);
159f8da7 5799 x_gtk_file_dialog_help_text = 1;
641c0105 5800
29208e82 5801 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
fb7ada5f 5802 doc: /* If non-nil, a detached tool bar is shown in full.
e0f467cb
JD
5803The default is to just show an arrow and pressing on that arrow shows
5804the tool bar buttons. */);
5805 x_gtk_whole_detached_tool_bar = 0;
5806
29208e82 5807 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
fb7ada5f 5808 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
aa1859f5
J
5809Otherwise use Emacs own tooltip implementation.
5810When using Gtk+ tooltips, the tooltip face is not used. */);
5811 x_gtk_use_system_tooltips = 1;
5812
d67b4f80 5813 Fprovide (intern_c_string ("x"), Qnil);
49be9d28 5814
1d3dac41 5815#ifdef USE_X_TOOLKIT
d67b4f80 5816 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5b827abb 5817#ifdef USE_MOTIF
d67b4f80 5818 Fprovide (intern_c_string ("motif"), Qnil);
fc2cdd9a 5819
29208e82 5820 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
7ee72033 5821 doc: /* Version info for LessTif/Motif. */);
fc2cdd9a
GM
5822 Vmotif_version_string = build_string (XmVERSION_STRING);
5823#endif /* USE_MOTIF */
5824#endif /* USE_X_TOOLKIT */
01f1ba30 5825
6b61353c 5826#ifdef USE_GTK
1ec2a572
JD
5827 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
5828 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
5829 But for a user it is a toolkit for X, and indeed, configure
5830 accepts --with-x-toolkit=gtk. */
d67b4f80
DN
5831 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5832 Fprovide (intern_c_string ("gtk"), Qnil);
bfeabdc3 5833 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6b61353c 5834
29208e82 5835 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6b61353c
KH
5836 doc: /* Version info for GTK+. */);
5837 {
99027bdd
PE
5838 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
5839 int len = sprintf (gtk_version, "%d.%d.%d",
5840 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
5841 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6b61353c
KH
5842 }
5843#endif /* USE_GTK */
5844
333b20bb
GM
5845 /* X window properties. */
5846 defsubr (&Sx_change_window_property);
5847 defsubr (&Sx_delete_window_property);
5848 defsubr (&Sx_window_property);
5849
2d764c78 5850 defsubr (&Sxw_display_color_p);
d0c9d219 5851 defsubr (&Sx_display_grayscale_p);
2d764c78
EZ
5852 defsubr (&Sxw_color_defined_p);
5853 defsubr (&Sxw_color_values);
9d317b2c 5854 defsubr (&Sx_server_max_request_size);
41beb8fc
RS
5855 defsubr (&Sx_server_vendor);
5856 defsubr (&Sx_server_version);
5857 defsubr (&Sx_display_pixel_width);
5858 defsubr (&Sx_display_pixel_height);
5859 defsubr (&Sx_display_mm_width);
5860 defsubr (&Sx_display_mm_height);
5861 defsubr (&Sx_display_screens);
5862 defsubr (&Sx_display_planes);
5863 defsubr (&Sx_display_color_cells);
5864 defsubr (&Sx_display_visual_class);
5865 defsubr (&Sx_display_backing_store);
5866 defsubr (&Sx_display_save_under);
1c44e124 5867 defsubr (&Sx_wm_set_size_hint);
f676886a 5868 defsubr (&Sx_create_frame);
01f1ba30 5869 defsubr (&Sx_open_connection);
08a90d6a
RS
5870 defsubr (&Sx_close_connection);
5871 defsubr (&Sx_display_list);
01f1ba30 5872 defsubr (&Sx_synchronize);
3decc1e7 5873 defsubr (&Sx_focus_frame);
82bab41c 5874 defsubr (&Sx_backspace_delete_keys_p);
177c0ea7 5875
333b20bb
GM
5876 defsubr (&Sx_show_tip);
5877 defsubr (&Sx_hide_tip);
333b20bb 5878 tip_timer = Qnil;
44b5a125
GM
5879 staticpro (&tip_timer);
5880 tip_frame = Qnil;
5881 staticpro (&tip_frame);
333b20bb 5882
06d62053
GM
5883 last_show_tip_args = Qnil;
5884 staticpro (&last_show_tip_args);
5885
90f2e16b 5886 defsubr (&Sx_uses_old_gtk_dialog);
29fabd8c 5887#if defined (USE_MOTIF) || defined (USE_GTK)
333b20bb
GM
5888 defsubr (&Sx_file_dialog);
5889#endif
9bf80974 5890
946598bf 5891#if defined (USE_GTK) && defined (HAVE_FREETYPE)
f82605c6 5892 defsubr (&Sx_select_font);
9bf80974 5893#endif
333b20bb
GM
5894}
5895
01f1ba30 5896#endif /* HAVE_X_WINDOWS */