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