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