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