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