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