Clean up initialization and customization of horizontal scroll bars.
[emacs.git] / src / xfns.c
blobec915a69f80b0e97aef3b7a35aeb34a4bf28c678
1 /* Functions for the X window system.
3 Copyright (C) 1989, 1992-2014 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
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.
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.
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/>. */
20 #include <config.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <unistd.h>
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"
46 #include <sys/types.h>
47 #include <sys/stat.h>
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
55 #include "xsettings.h"
57 #ifdef HAVE_XRANDR
58 #include <X11/extensions/Xrandr.h>
59 #endif
60 #ifdef HAVE_XINERAMA
61 #include <X11/extensions/Xinerama.h>
62 #endif
64 #ifdef USE_GTK
65 #include "gtkutil.h"
66 #endif
68 #ifdef USE_X_TOOLKIT
69 #include <X11/Shell.h>
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 */
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
91 #include "widget.h"
93 #include "../lwlib/lwlib.h"
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
103 #ifdef USE_LUCID
104 #include "../lwlib/xlwmenu.h"
105 #endif
107 #if !defined (NO_EDITRES)
108 #define HACK_EDITRES
109 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
110 #endif /* not defined NO_EDITRES */
112 /* Unique id counter for widgets created by the Lucid Widget Library. */
114 extern LWLIB_ID widget_id_tick;
116 #ifdef USE_MOTIF
118 #endif /* USE_MOTIF */
120 #endif /* USE_X_TOOLKIT */
122 #ifdef USE_GTK
124 #endif /* USE_GTK */
126 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
128 static Lisp_Object Qundefined_color;
129 static Lisp_Object Qcompound_text, Qcancel_timer;
130 Lisp_Object Qfont_param;
132 #ifdef GLYPH_DEBUG
133 static ptrdiff_t image_cache_refcount;
134 static int dpyinfo_refcount;
135 #endif
137 static struct x_display_info *x_display_info_for_name (Lisp_Object);
139 /* Let the user specify an X display with a Lisp object.
140 OBJECT may be nil, a frame or a terminal object.
141 nil stands for the selected frame--or, if that is not an X frame,
142 the first X display on the list. */
144 struct x_display_info *
145 check_x_display_info (Lisp_Object object)
147 struct x_display_info *dpyinfo = NULL;
149 if (NILP (object))
151 struct frame *sf = XFRAME (selected_frame);
153 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
154 dpyinfo = FRAME_DISPLAY_INFO (sf);
155 else if (x_display_list != 0)
156 dpyinfo = x_display_list;
157 else
158 error ("X windows are not in use or not initialized");
160 else if (TERMINALP (object))
162 struct terminal *t = get_terminal (object, 1);
164 if (t->type != output_x_window)
165 error ("Terminal %d is not an X display", t->id);
167 dpyinfo = t->display_info.x;
169 else if (STRINGP (object))
170 dpyinfo = x_display_info_for_name (object);
171 else
173 struct frame *f = decode_window_system_frame (object);
174 dpyinfo = FRAME_DISPLAY_INFO (f);
177 return dpyinfo;
180 /* Store the screen positions of frame F into XPTR and YPTR.
181 These are the positions of the containing window manager window,
182 not Emacs's own window. */
184 void
185 x_real_positions (struct frame *f, int *xptr, int *yptr)
187 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
188 int real_x = 0, real_y = 0;
189 int had_errors = 0;
190 Window win = f->output_data.x->parent_desc;
191 Atom actual_type;
192 unsigned long actual_size, bytes_remaining;
193 int rc, actual_format;
194 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
195 long max_len = 400;
196 Display *dpy = FRAME_X_DISPLAY (f);
197 unsigned char *tmp_data = NULL;
198 Atom target_type = XA_CARDINAL;
200 block_input ();
202 x_catch_errors (dpy);
204 if (win == dpyinfo->root_window)
205 win = FRAME_OUTER_WINDOW (f);
207 /* This loop traverses up the containment tree until we hit the root
208 window. Window managers may intersect many windows between our window
209 and the root window. The window we find just before the root window
210 should be the outer WM window. */
211 for (;;)
213 Window wm_window, rootw;
214 Window *tmp_children;
215 unsigned int tmp_nchildren;
216 int success;
218 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
219 &wm_window, &tmp_children, &tmp_nchildren);
221 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
223 /* Don't free tmp_children if XQueryTree failed. */
224 if (! success)
225 break;
227 XFree (tmp_children);
229 if (wm_window == rootw || had_errors)
230 break;
232 win = wm_window;
235 if (! had_errors)
237 unsigned int ign;
238 Window child, rootw;
240 /* Get the real coordinates for the WM window upper left corner */
241 XGetGeometry (FRAME_X_DISPLAY (f), win,
242 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
244 /* Translate real coordinates to coordinates relative to our
245 window. For our window, the upper left corner is 0, 0.
246 Since the upper left corner of the WM window is outside
247 our window, win_x and win_y will be negative:
249 ------------------ ---> x
250 | title |
251 | ----------------- v y
252 | | our window
254 XTranslateCoordinates (FRAME_X_DISPLAY (f),
256 /* From-window, to-window. */
257 FRAME_DISPLAY_INFO (f)->root_window,
258 FRAME_X_WINDOW (f),
260 /* From-position, to-position. */
261 real_x, real_y, &win_x, &win_y,
263 /* Child of win. */
264 &child);
266 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
268 outer_x = win_x;
269 outer_y = win_y;
271 else
273 XTranslateCoordinates (FRAME_X_DISPLAY (f),
275 /* From-window, to-window. */
276 FRAME_DISPLAY_INFO (f)->root_window,
277 FRAME_OUTER_WINDOW (f),
279 /* From-position, to-position. */
280 real_x, real_y, &outer_x, &outer_y,
282 /* Child of win. */
283 &child);
286 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
290 if (dpyinfo->root_window == f->output_data.x->parent_desc)
292 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
293 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
294 0, max_len, False, target_type,
295 &actual_type, &actual_format, &actual_size,
296 &bytes_remaining, &tmp_data);
298 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
299 && actual_size == 4 && actual_format == 32)
301 unsigned int ign;
302 Window rootw;
303 long *fe = (long *)tmp_data;
305 XGetGeometry (FRAME_X_DISPLAY (f), win,
306 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
307 outer_x = -fe[0];
308 outer_y = -fe[2];
309 real_x -= fe[0];
310 real_y -= fe[2];
314 if (tmp_data) XFree (tmp_data);
316 x_uncatch_errors ();
318 unblock_input ();
320 if (had_errors) return;
322 f->x_pixels_diff = -win_x;
323 f->y_pixels_diff = -win_y;
325 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
326 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
328 *xptr = real_x;
329 *yptr = real_y;
332 /* Get the mouse position in frame relative coordinates. */
334 void
335 x_relative_mouse_position (struct frame *f, int *x, int *y)
337 Window root, dummy_window;
338 int dummy;
340 eassert (FRAME_X_P (f));
342 block_input ();
344 XQueryPointer (FRAME_X_DISPLAY (f),
345 DefaultRootWindow (FRAME_X_DISPLAY (f)),
347 /* The root window which contains the pointer. */
348 &root,
350 /* Window pointer is on, not used */
351 &dummy_window,
353 /* The position on that root window. */
354 x, y,
356 /* x/y in dummy_window coordinates, not used. */
357 &dummy, &dummy,
359 /* Modifier keys and pointer buttons, about which
360 we don't care. */
361 (unsigned int *) &dummy);
363 unblock_input ();
365 /* Translate root window coordinates to window coordinates. */
366 *x -= f->left_pos + FRAME_OUTER_TO_INNER_DIFF_X (f);
367 *y -= f->top_pos + FRAME_OUTER_TO_INNER_DIFF_Y (f);
370 /* Gamma-correct COLOR on frame F. */
372 void
373 gamma_correct (struct frame *f, XColor *color)
375 if (f->gamma)
377 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
378 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
379 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
384 /* Decide if color named COLOR_NAME is valid for use on frame F. If
385 so, return the RGB values in COLOR. If ALLOC_P,
386 allocate the color. Value is false if COLOR_NAME is invalid, or
387 no color could be allocated. */
389 bool
390 x_defined_color (struct frame *f, const char *color_name,
391 XColor *color, bool alloc_p)
393 bool success_p = 0;
394 Display *dpy = FRAME_X_DISPLAY (f);
395 Colormap cmap = FRAME_X_COLORMAP (f);
397 block_input ();
398 #ifdef USE_GTK
399 success_p = xg_check_special_colors (f, color_name, color);
400 #endif
401 if (!success_p)
402 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
403 if (success_p && alloc_p)
404 success_p = x_alloc_nearest_color (f, cmap, color);
405 unblock_input ();
407 return success_p;
411 /* Return the pixel color value for color COLOR_NAME on frame F. If F
412 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
413 Signal an error if color can't be allocated. */
415 static int
416 x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
418 XColor cdef;
420 CHECK_STRING (color_name);
422 #if 0 /* Don't do this. It's wrong when we're not using the default
423 colormap, it makes freeing difficult, and it's probably not
424 an important optimization. */
425 if (strcmp (SDATA (color_name), "black") == 0)
426 return BLACK_PIX_DEFAULT (f);
427 else if (strcmp (SDATA (color_name), "white") == 0)
428 return WHITE_PIX_DEFAULT (f);
429 #endif
431 /* Return MONO_COLOR for monochrome frames. */
432 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
433 return mono_color;
435 /* x_defined_color is responsible for coping with failures
436 by looking for a near-miss. */
437 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
438 return cdef.pixel;
440 signal_error ("Undefined color", color_name);
445 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
446 the previous value of that parameter, NEW_VALUE is the new value.
447 See also the comment of wait_for_wm in struct x_output. */
449 static void
450 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
452 f->output_data.x->wait_for_wm = !NILP (new_value);
455 static void
456 x_set_tool_bar_position (struct frame *f,
457 Lisp_Object new_value,
458 Lisp_Object old_value)
460 Lisp_Object choice = list4 (Qleft, Qright, Qtop, Qbottom);
462 if (!NILP (Fmemq (new_value, choice)))
464 #ifdef USE_GTK
465 if (!EQ (new_value, old_value))
467 xg_change_toolbar_position (f, new_value);
468 fset_tool_bar_position (f, new_value);
470 #else
471 if (!EQ (new_value, Qtop))
472 error ("The only supported tool bar position is top");
473 #endif
475 else
476 wrong_choice (choice, new_value);
479 #ifdef USE_GTK
481 /* Set icon from FILE for frame F. By using GTK functions the icon
482 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
485 xg_set_icon (struct frame *f, Lisp_Object file)
487 int result = 0;
488 Lisp_Object found;
490 found = x_find_image_file (file);
492 if (! NILP (found))
494 GdkPixbuf *pixbuf;
495 GError *err = NULL;
496 char *filename = SSDATA (found);
497 block_input ();
499 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
501 if (pixbuf)
503 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
504 pixbuf);
505 g_object_unref (pixbuf);
507 result = 1;
509 else
510 g_error_free (err);
512 unblock_input ();
515 return result;
519 xg_set_icon_from_xpm_data (struct frame *f, const char **data)
521 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
523 if (!pixbuf)
524 return 0;
526 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
527 g_object_unref (pixbuf);
528 return 1;
530 #endif /* USE_GTK */
533 /* Functions called only from `x_set_frame_param'
534 to set individual parameters.
536 If FRAME_X_WINDOW (f) is 0,
537 the frame is being created and its X-window does not exist yet.
538 In that case, just record the parameter's new value
539 in the standard place; do not attempt to change the window. */
541 static void
542 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
544 struct x_output *x = f->output_data.x;
545 unsigned long fg, old_fg;
547 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
548 old_fg = FRAME_FOREGROUND_PIXEL (f);
549 FRAME_FOREGROUND_PIXEL (f) = fg;
551 if (FRAME_X_WINDOW (f) != 0)
553 Display *dpy = FRAME_X_DISPLAY (f);
555 block_input ();
556 XSetForeground (dpy, x->normal_gc, fg);
557 XSetBackground (dpy, x->reverse_gc, fg);
559 if (x->cursor_pixel == old_fg)
561 unload_color (f, x->cursor_pixel);
562 x->cursor_pixel = x_copy_color (f, fg);
563 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
566 unblock_input ();
568 update_face_from_frame_parameter (f, Qforeground_color, arg);
570 if (FRAME_VISIBLE_P (f))
571 redraw_frame (f);
574 unload_color (f, old_fg);
577 static void
578 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
580 struct x_output *x = f->output_data.x;
581 unsigned long bg;
583 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
584 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
585 FRAME_BACKGROUND_PIXEL (f) = bg;
587 if (FRAME_X_WINDOW (f) != 0)
589 Display *dpy = FRAME_X_DISPLAY (f);
591 block_input ();
592 XSetBackground (dpy, x->normal_gc, bg);
593 XSetForeground (dpy, x->reverse_gc, bg);
594 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
595 XSetForeground (dpy, x->cursor_gc, bg);
597 #ifdef USE_GTK
598 xg_set_background_color (f, bg);
599 #endif
601 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
602 toolkit scroll bars. */
604 Lisp_Object bar;
605 for (bar = FRAME_SCROLL_BARS (f);
606 !NILP (bar);
607 bar = XSCROLL_BAR (bar)->next)
609 Window window = XSCROLL_BAR (bar)->x_window;
610 XSetWindowBackground (dpy, window, bg);
613 #endif /* USE_TOOLKIT_SCROLL_BARS */
615 unblock_input ();
616 update_face_from_frame_parameter (f, Qbackground_color, arg);
618 if (FRAME_VISIBLE_P (f))
619 redraw_frame (f);
623 static void
624 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
626 struct x_output *x = f->output_data.x;
627 Display *dpy = FRAME_X_DISPLAY (f);
628 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
629 Cursor hourglass_cursor, horizontal_drag_cursor, vertical_drag_cursor;
630 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
631 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
633 /* Don't let pointers be invisible. */
634 if (mask_color == pixel)
636 x_free_colors (f, &pixel, 1);
637 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
640 unload_color (f, x->mouse_pixel);
641 x->mouse_pixel = pixel;
643 block_input ();
645 /* It's not okay to crash if the user selects a screwy cursor. */
646 x_catch_errors (dpy);
648 if (!NILP (Vx_pointer_shape))
650 CHECK_NUMBER (Vx_pointer_shape);
651 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
653 else
654 cursor = XCreateFontCursor (dpy, XC_xterm);
655 x_check_errors (dpy, "bad text pointer cursor: %s");
657 if (!NILP (Vx_nontext_pointer_shape))
659 CHECK_NUMBER (Vx_nontext_pointer_shape);
660 nontext_cursor
661 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
663 else
664 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
665 x_check_errors (dpy, "bad nontext pointer cursor: %s");
667 if (!NILP (Vx_hourglass_pointer_shape))
669 CHECK_NUMBER (Vx_hourglass_pointer_shape);
670 hourglass_cursor
671 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
673 else
674 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
675 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
677 if (!NILP (Vx_mode_pointer_shape))
679 CHECK_NUMBER (Vx_mode_pointer_shape);
680 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
682 else
683 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
684 x_check_errors (dpy, "bad modeline pointer cursor: %s");
686 if (!NILP (Vx_sensitive_text_pointer_shape))
688 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
689 hand_cursor
690 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
692 else
693 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
695 if (!NILP (Vx_window_horizontal_drag_shape))
697 CHECK_TYPE_RANGED_INTEGER (unsigned, Vx_window_horizontal_drag_shape);
698 horizontal_drag_cursor
699 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
701 else
702 horizontal_drag_cursor
703 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
705 if (!NILP (Vx_window_vertical_drag_shape))
707 CHECK_NUMBER (Vx_window_vertical_drag_shape);
708 vertical_drag_cursor
709 = XCreateFontCursor (dpy, XINT (Vx_window_vertical_drag_shape));
711 else
712 vertical_drag_cursor
713 = XCreateFontCursor (dpy, XC_sb_v_double_arrow);
715 /* Check and report errors with the above calls. */
716 x_check_errors (dpy, "can't set cursor shape: %s");
717 x_uncatch_errors ();
720 XColor fore_color, back_color;
722 fore_color.pixel = x->mouse_pixel;
723 x_query_color (f, &fore_color);
724 back_color.pixel = mask_color;
725 x_query_color (f, &back_color);
727 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
728 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
729 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
730 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
731 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
732 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
733 XRecolorCursor (dpy, vertical_drag_cursor, &fore_color, &back_color);
736 if (FRAME_X_WINDOW (f) != 0)
737 XDefineCursor (dpy, FRAME_X_WINDOW (f),
738 f->output_data.x->current_cursor = cursor);
740 if (cursor != x->text_cursor
741 && x->text_cursor != 0)
742 XFreeCursor (dpy, x->text_cursor);
743 x->text_cursor = cursor;
745 if (nontext_cursor != x->nontext_cursor
746 && x->nontext_cursor != 0)
747 XFreeCursor (dpy, x->nontext_cursor);
748 x->nontext_cursor = nontext_cursor;
750 if (hourglass_cursor != x->hourglass_cursor
751 && x->hourglass_cursor != 0)
752 XFreeCursor (dpy, x->hourglass_cursor);
753 x->hourglass_cursor = hourglass_cursor;
755 if (mode_cursor != x->modeline_cursor
756 && x->modeline_cursor != 0)
757 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
758 x->modeline_cursor = mode_cursor;
760 if (hand_cursor != x->hand_cursor
761 && x->hand_cursor != 0)
762 XFreeCursor (dpy, x->hand_cursor);
763 x->hand_cursor = hand_cursor;
765 if (horizontal_drag_cursor != x->horizontal_drag_cursor
766 && x->horizontal_drag_cursor != 0)
767 XFreeCursor (dpy, x->horizontal_drag_cursor);
768 x->horizontal_drag_cursor = horizontal_drag_cursor;
770 if (vertical_drag_cursor != x->vertical_drag_cursor
771 && x->vertical_drag_cursor != 0)
772 XFreeCursor (dpy, x->vertical_drag_cursor);
773 x->vertical_drag_cursor = vertical_drag_cursor;
775 XFlush (dpy);
776 unblock_input ();
778 update_face_from_frame_parameter (f, Qmouse_color, arg);
781 static void
782 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
784 unsigned long fore_pixel, pixel;
785 bool fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
786 struct x_output *x = f->output_data.x;
788 if (!NILP (Vx_cursor_fore_pixel))
790 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
791 WHITE_PIX_DEFAULT (f));
792 fore_pixel_allocated_p = 1;
794 else
795 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
797 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
798 pixel_allocated_p = 1;
800 /* Make sure that the cursor color differs from the background color. */
801 if (pixel == FRAME_BACKGROUND_PIXEL (f))
803 if (pixel_allocated_p)
805 x_free_colors (f, &pixel, 1);
806 pixel_allocated_p = 0;
809 pixel = x->mouse_pixel;
810 if (pixel == fore_pixel)
812 if (fore_pixel_allocated_p)
814 x_free_colors (f, &fore_pixel, 1);
815 fore_pixel_allocated_p = 0;
817 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
821 unload_color (f, x->cursor_foreground_pixel);
822 if (!fore_pixel_allocated_p)
823 fore_pixel = x_copy_color (f, fore_pixel);
824 x->cursor_foreground_pixel = fore_pixel;
826 unload_color (f, x->cursor_pixel);
827 if (!pixel_allocated_p)
828 pixel = x_copy_color (f, pixel);
829 x->cursor_pixel = pixel;
831 if (FRAME_X_WINDOW (f) != 0)
833 block_input ();
834 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
835 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
836 unblock_input ();
838 if (FRAME_VISIBLE_P (f))
840 x_update_cursor (f, 0);
841 x_update_cursor (f, 1);
845 update_face_from_frame_parameter (f, Qcursor_color, arg);
848 /* Set the border-color of frame F to pixel value PIX.
849 Note that this does not fully take effect if done before
850 F has an x-window. */
852 static void
853 x_set_border_pixel (struct frame *f, int pix)
855 unload_color (f, f->output_data.x->border_pixel);
856 f->output_data.x->border_pixel = pix;
858 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
860 block_input ();
861 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
862 unblock_input ();
864 if (FRAME_VISIBLE_P (f))
865 redraw_frame (f);
869 /* Set the border-color of frame F to value described by ARG.
870 ARG can be a string naming a color.
871 The border-color is used for the border that is drawn by the X server.
872 Note that this does not fully take effect if done before
873 F has an x-window; it must be redone when the window is created.
875 Note: this is done in two routines because of the way X10 works.
877 Note: under X11, this is normally the province of the window manager,
878 and so emacs's border colors may be overridden. */
880 static void
881 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
883 int pix;
885 CHECK_STRING (arg);
886 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
887 x_set_border_pixel (f, pix);
888 update_face_from_frame_parameter (f, Qborder_color, arg);
892 static void
893 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
895 set_frame_cursor_types (f, arg);
898 static void
899 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
901 int result;
903 if (STRINGP (arg))
905 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
906 return;
908 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
909 return;
911 block_input ();
912 if (NILP (arg))
913 result = x_text_icon (f,
914 SSDATA ((!NILP (f->icon_name)
915 ? f->icon_name
916 : f->name)));
917 else
918 result = x_bitmap_icon (f, arg);
920 if (result)
922 unblock_input ();
923 error ("No icon window available");
926 XFlush (FRAME_X_DISPLAY (f));
927 unblock_input ();
930 static void
931 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
933 int result;
935 if (STRINGP (arg))
937 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
938 return;
940 else if (!NILP (arg) || NILP (oldval))
941 return;
943 fset_icon_name (f, arg);
945 if (f->output_data.x->icon_bitmap != 0)
946 return;
948 block_input ();
950 result = x_text_icon (f,
951 SSDATA ((!NILP (f->icon_name)
952 ? f->icon_name
953 : !NILP (f->title)
954 ? f->title
955 : f->name)));
957 if (result)
959 unblock_input ();
960 error ("No icon window available");
963 XFlush (FRAME_X_DISPLAY (f));
964 unblock_input ();
968 static void
969 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
971 int nlines;
972 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
973 int olines = FRAME_MENU_BAR_LINES (f);
974 #endif
976 /* Right now, menu bars don't work properly in minibuf-only frames;
977 most of the commands try to apply themselves to the minibuffer
978 frame itself, and get an error because you can't switch buffers
979 in or split the minibuffer window. */
980 if (FRAME_MINIBUF_ONLY_P (f))
981 return;
983 if (TYPE_RANGED_INTEGERP (int, value))
984 nlines = XINT (value);
985 else
986 nlines = 0;
988 /* Make sure we redisplay all windows in this frame. */
989 windows_or_buffers_changed = 59;
991 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
992 FRAME_MENU_BAR_LINES (f) = 0;
993 FRAME_MENU_BAR_HEIGHT (f) = 0;
994 if (nlines)
996 FRAME_EXTERNAL_MENU_BAR (f) = 1;
997 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
998 /* Make sure next redisplay shows the menu bar. */
999 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1001 else
1003 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1004 free_frame_menubar (f);
1005 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1006 if (FRAME_X_P (f))
1007 f->output_data.x->menubar_widget = 0;
1009 #else /* not USE_X_TOOLKIT && not USE_GTK */
1010 FRAME_MENU_BAR_LINES (f) = nlines;
1011 FRAME_MENU_BAR_HEIGHT (f) = nlines * FRAME_LINE_HEIGHT (f);
1012 adjust_frame_size (f, -1, -1, 2, 1);
1013 if (FRAME_X_WINDOW (f))
1014 x_clear_under_internal_border (f);
1016 /* If the menu bar height gets changed, the internal border below
1017 the top margin has to be cleared. Also, if the menu bar gets
1018 larger, the area for the added lines has to be cleared except for
1019 the first menu bar line that is to be drawn later. */
1020 if (nlines != olines)
1022 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1023 int width = FRAME_PIXEL_WIDTH (f);
1024 int y;
1026 /* height can be zero here. */
1027 if (FRAME_X_WINDOW (f) && height > 0 && width > 0)
1029 y = FRAME_TOP_MARGIN_HEIGHT (f);
1031 block_input ();
1032 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1033 0, y, width, height);
1034 unblock_input ();
1037 if (nlines > 1 && nlines > olines)
1039 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1040 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1042 block_input ();
1043 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1044 0, y, width, height);
1045 unblock_input ();
1048 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1049 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1051 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1052 adjust_frame_glyphs (f);
1053 run_window_configuration_change_hook (f);
1057 /* Set the number of lines used for the tool bar of frame F to VALUE.
1058 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1059 is the old number of tool bar lines. This function changes the
1060 height of all windows on frame F to match the new tool bar height.
1061 The frame's height doesn't change. */
1063 static void
1064 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1066 int nlines;
1068 /* Treat tool bars like menu bars. */
1069 if (FRAME_MINIBUF_ONLY_P (f))
1070 return;
1072 /* Use VALUE only if an int >= 0. */
1073 if (RANGED_INTEGERP (0, value, INT_MAX))
1074 nlines = XFASTINT (value);
1075 else
1076 nlines = 0;
1078 x_change_tool_bar_height (f, nlines * FRAME_LINE_HEIGHT (f));
1082 /* Set the pixel height of the tool bar of frame F to HEIGHT. */
1083 void
1084 x_change_tool_bar_height (struct frame *f, int height)
1086 #ifdef USE_GTK
1087 FRAME_TOOL_BAR_LINES (f) = 0;
1088 FRAME_TOOL_BAR_HEIGHT (f) = 0;
1089 if (height)
1091 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1092 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1093 /* Make sure next redisplay shows the tool bar. */
1094 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1095 update_frame_tool_bar (f);
1097 else
1099 if (FRAME_EXTERNAL_TOOL_BAR (f))
1100 free_frame_tool_bar (f);
1101 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1103 #else /* !USE_GTK */
1104 int unit = FRAME_LINE_HEIGHT (f);
1105 int old_height = FRAME_TOOL_BAR_HEIGHT (f);
1106 int lines = (height + unit - 1) / unit;
1108 /* Make sure we redisplay all windows in this frame. */
1109 windows_or_buffers_changed = 60;
1112 /* Recalculate tool bar and frame text sizes. */
1113 FRAME_TOOL_BAR_HEIGHT (f) = height;
1114 FRAME_TOOL_BAR_LINES (f) = lines;
1115 FRAME_TEXT_HEIGHT (f)
1116 = FRAME_PIXEL_TO_TEXT_HEIGHT (f, FRAME_PIXEL_HEIGHT (f));
1117 FRAME_LINES (f)
1118 = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, FRAME_PIXEL_HEIGHT (f));
1119 /* Store the `tool-bar-lines' and `height' frame parameters. */
1120 store_frame_param (f, Qtool_bar_lines, make_number (lines));
1121 store_frame_param (f, Qheight, make_number (FRAME_LINES (f)));
1123 /* We also have to make sure that the internal border at the top of
1124 the frame, below the menu bar or tool bar, is redrawn when the
1125 tool bar disappears. This is so because the internal border is
1126 below the tool bar if one is displayed, but is below the menu bar
1127 if there isn't a tool bar. The tool bar draws into the area
1128 below the menu bar. */
1129 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_HEIGHT (f) == 0)
1131 clear_frame (f);
1132 clear_current_matrices (f);
1135 if ((height < old_height) && WINDOWP (f->tool_bar_window))
1136 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1138 /* Recalculate toolbar height. */
1139 f->n_tool_bar_rows = 0;
1141 adjust_frame_size (f, -1, -1, 4, 0);
1143 /** #if !defined USE_X_TOOLKIT **/
1144 if (FRAME_X_WINDOW (f))
1145 x_clear_under_internal_border (f);
1146 /** #endif **/
1148 #endif /* USE_GTK */
1152 static void
1153 x_set_internal_border_width (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1155 int border;
1157 CHECK_TYPE_RANGED_INTEGER (int, arg);
1158 border = max (XINT (arg), 0);
1160 if (border != FRAME_INTERNAL_BORDER_WIDTH (f))
1162 FRAME_INTERNAL_BORDER_WIDTH (f) = border;
1164 #ifdef USE_X_TOOLKIT
1165 if (FRAME_X_OUTPUT (f)->edit_widget)
1166 widget_store_internal_border (FRAME_X_OUTPUT (f)->edit_widget);
1167 #endif
1169 if (FRAME_X_WINDOW (f) != 0)
1171 adjust_frame_size (f, -1, -1, 3, 0);
1173 #ifdef USE_GTK
1174 xg_clear_under_internal_border (f);
1175 #else
1176 x_clear_under_internal_border (f);
1177 #endif
1184 /* Set the foreground color for scroll bars on frame F to VALUE.
1185 VALUE should be a string, a color name. If it isn't a string or
1186 isn't a valid color name, do nothing. OLDVAL is the old value of
1187 the frame parameter. */
1189 static void
1190 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1192 unsigned long pixel;
1194 if (STRINGP (value))
1195 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1196 else
1197 pixel = -1;
1199 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1200 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1202 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1203 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1205 /* Remove all scroll bars because they have wrong colors. */
1206 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1207 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1208 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1209 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1211 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1212 redraw_frame (f);
1217 /* Set the background color for scroll bars on frame F to VALUE VALUE
1218 should be a string, a color name. If it isn't a string or isn't a
1219 valid color name, do nothing. OLDVAL is the old value of the frame
1220 parameter. */
1222 static void
1223 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1225 unsigned long pixel;
1227 if (STRINGP (value))
1228 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1229 else
1230 pixel = -1;
1232 if (f->output_data.x->scroll_bar_background_pixel != -1)
1233 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1235 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
1236 /* Scrollbar shadow colors. */
1237 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1239 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1240 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1242 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1244 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1245 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1247 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
1249 f->output_data.x->scroll_bar_background_pixel = pixel;
1250 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1252 /* Remove all scroll bars because they have wrong colors. */
1253 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1254 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1255 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1256 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1258 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1259 redraw_frame (f);
1264 /* Encode Lisp string STRING as a text in a format appropriate for
1265 XICCC (X Inter Client Communication Conventions).
1267 This can call Lisp code, so callers must GCPRO.
1269 If STRING contains only ASCII characters, do no conversion and
1270 return the string data of STRING. Otherwise, encode the text by
1271 CODING_SYSTEM, and return a newly allocated memory area which
1272 should be freed by `xfree' by a caller.
1274 Store the byte length of resulting text in *TEXT_BYTES.
1276 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1277 which means that the `encoding' of the result can be `STRING'.
1278 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1279 the result should be `COMPOUND_TEXT'. */
1281 static unsigned char *
1282 x_encode_text (Lisp_Object string, Lisp_Object coding_system,
1283 ptrdiff_t *text_bytes, int *stringp, bool *freep)
1285 int result = string_xstring_p (string);
1286 struct coding_system coding;
1288 if (result == 0)
1290 /* No multibyte character in OBJ. We need not encode it. */
1291 *text_bytes = SBYTES (string);
1292 *stringp = 1;
1293 *freep = 0;
1294 return SDATA (string);
1297 setup_coding_system (coding_system, &coding);
1298 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1299 /* We suppress producing escape sequences for composition. */
1300 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1301 coding.destination = xnmalloc (SCHARS (string), 2);
1302 coding.dst_bytes = SCHARS (string) * 2;
1303 encode_coding_object (&coding, string, 0, 0,
1304 SCHARS (string), SBYTES (string), Qnil);
1305 *text_bytes = coding.produced;
1306 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1307 *freep = 1;
1308 return coding.destination;
1312 /* Set the WM name to NAME for frame F. Also set the icon name.
1313 If the frame already has an icon name, use that, otherwise set the
1314 icon name to NAME. */
1316 static void
1317 x_set_name_internal (struct frame *f, Lisp_Object name)
1319 if (FRAME_X_WINDOW (f))
1321 block_input ();
1323 XTextProperty text, icon;
1324 ptrdiff_t bytes;
1325 int stringp;
1326 bool do_free_icon_value = 0, do_free_text_value = 0;
1327 Lisp_Object coding_system;
1328 Lisp_Object encoded_name;
1329 Lisp_Object encoded_icon_name;
1330 struct gcpro gcpro1;
1332 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1333 we use it before x_encode_text that may return string data. */
1334 GCPRO1 (name);
1335 encoded_name = ENCODE_UTF_8 (name);
1336 UNGCPRO;
1338 coding_system = Qcompound_text;
1339 /* Note: Encoding strategy
1341 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1342 text.encoding. But, there are non-internationalized window
1343 managers which don't support that encoding. So, if NAME
1344 contains only ASCII and 8859-1 characters, encode it by
1345 iso-latin-1, and use "STRING" in text.encoding hoping that
1346 such window managers at least analyze this format correctly,
1347 i.e. treat 8-bit bytes as 8859-1 characters.
1349 We may also be able to use "UTF8_STRING" in text.encoding
1350 in the future which can encode all Unicode characters.
1351 But, for the moment, there's no way to know that the
1352 current window manager supports it or not.
1354 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1355 properties. Per the EWMH specification, those two properties
1356 are always UTF8_STRING. This matches what gtk_window_set_title()
1357 does in the USE_GTK case. */
1358 text.value = x_encode_text (name, coding_system, &bytes,
1359 &stringp, &do_free_text_value);
1360 text.encoding = (stringp ? XA_STRING
1361 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1362 text.format = 8;
1363 text.nitems = bytes;
1365 if (!STRINGP (f->icon_name))
1367 icon = text;
1368 encoded_icon_name = encoded_name;
1370 else
1372 /* See the above comment "Note: Encoding strategy". */
1373 icon.value = x_encode_text (f->icon_name, coding_system, &bytes,
1374 &stringp, &do_free_icon_value);
1375 icon.encoding = (stringp ? XA_STRING
1376 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1377 icon.format = 8;
1378 icon.nitems = bytes;
1380 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1383 #ifdef USE_GTK
1384 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1385 SSDATA (encoded_name));
1386 #else /* not USE_GTK */
1387 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1388 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1389 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1390 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1391 8, PropModeReplace,
1392 SDATA (encoded_name),
1393 SBYTES (encoded_name));
1394 #endif /* not USE_GTK */
1396 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1397 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1398 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1399 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1400 8, PropModeReplace,
1401 SDATA (encoded_icon_name),
1402 SBYTES (encoded_icon_name));
1404 if (do_free_icon_value)
1405 xfree (icon.value);
1406 if (do_free_text_value)
1407 xfree (text.value);
1409 unblock_input ();
1413 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1414 x_id_name.
1416 If EXPLICIT is true, that indicates that lisp code is setting the
1417 name; if NAME is a string, set F's name to NAME and set
1418 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1420 If EXPLICIT is false, that indicates that Emacs redisplay code is
1421 suggesting a new name, which lisp code should override; if
1422 F->explicit_name is set, ignore the new name; otherwise, set it. */
1424 static void
1425 x_set_name (struct frame *f, Lisp_Object name, bool explicit)
1427 /* Make sure that requests from lisp code override requests from
1428 Emacs redisplay code. */
1429 if (explicit)
1431 /* If we're switching from explicit to implicit, we had better
1432 update the mode lines and thereby update the title. */
1433 if (f->explicit_name && NILP (name))
1434 update_mode_lines = 37;
1436 f->explicit_name = ! NILP (name);
1438 else if (f->explicit_name)
1439 return;
1441 /* If NAME is nil, set the name to the x_id_name. */
1442 if (NILP (name))
1444 /* Check for no change needed in this very common case
1445 before we do any consing. */
1446 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
1447 SSDATA (f->name)))
1448 return;
1449 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
1451 else
1452 CHECK_STRING (name);
1454 /* Don't change the name if it's already NAME. */
1455 if (! NILP (Fstring_equal (name, f->name)))
1456 return;
1458 fset_name (f, name);
1460 /* For setting the frame title, the title parameter should override
1461 the name parameter. */
1462 if (! NILP (f->title))
1463 name = f->title;
1465 x_set_name_internal (f, name);
1468 /* This function should be called when the user's lisp code has
1469 specified a name for the frame; the name will override any set by the
1470 redisplay code. */
1471 static void
1472 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1474 x_set_name (f, arg, 1);
1477 /* This function should be called by Emacs redisplay code to set the
1478 name; names set this way will never override names set by the user's
1479 lisp code. */
1480 void
1481 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1483 x_set_name (f, arg, 0);
1486 /* Change the title of frame F to NAME.
1487 If NAME is nil, use the frame name as the title. */
1489 static void
1490 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1492 /* Don't change the title if it's already NAME. */
1493 if (EQ (name, f->title))
1494 return;
1496 update_mode_lines = 38;
1498 fset_title (f, name);
1500 if (NILP (name))
1501 name = f->name;
1502 else
1503 CHECK_STRING (name);
1505 x_set_name_internal (f, name);
1508 void
1509 x_set_scroll_bar_default_width (struct frame *f)
1511 int unit = FRAME_COLUMN_WIDTH (f);
1512 #ifdef USE_TOOLKIT_SCROLL_BARS
1513 #ifdef USE_GTK
1514 int minw = xg_get_default_scrollbar_width ();
1515 #else
1516 int minw = 16;
1517 #endif
1518 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1519 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + unit - 1) / unit;
1520 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
1521 #else
1522 /* The width of a non-toolkit scrollbar is 14 pixels. */
1523 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + unit - 1) / unit;
1524 FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
1525 = FRAME_CONFIG_SCROLL_BAR_COLS (f) * unit;
1526 #endif
1529 void
1530 x_set_scroll_bar_default_height (struct frame *f)
1532 int height = FRAME_LINE_HEIGHT (f);
1533 #ifdef USE_TOOLKIT_SCROLL_BARS
1534 #ifdef USE_GTK
1535 int min_height = xg_get_default_scrollbar_height ();
1536 #else
1537 int min_height = 16;
1538 #endif
1539 /* A minimum height of 14 doesn't look good for toolkit scroll bars. */
1540 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = min_height;
1541 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (min_height + height - 1) / height;
1542 #else
1543 /* The height of a non-toolkit scrollbar is 14 pixels. */
1544 FRAME_CONFIG_SCROLL_BAR_LINES (f) = (14 + height - 1) / height;
1546 /* Use all of that space (aside from required margins) for the
1547 scroll bar. */
1548 FRAME_CONFIG_SCROLL_BAR_HEIGHT (f) = 14;
1549 #endif
1553 /* Record in frame F the specified or default value according to ALIST
1554 of the parameter named PROP (a Lisp symbol). If no value is
1555 specified for PROP, look for an X default for XPROP on the frame
1556 named NAME. If that is not found either, use the value DEFLT. */
1558 static Lisp_Object
1559 x_default_scroll_bar_color_parameter (struct frame *f,
1560 Lisp_Object alist, Lisp_Object prop,
1561 const char *xprop, const char *xclass,
1562 int foreground_p)
1564 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1565 Lisp_Object tem;
1567 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1568 if (EQ (tem, Qunbound))
1570 #ifdef USE_TOOLKIT_SCROLL_BARS
1572 /* See if an X resource for the scroll bar color has been
1573 specified. */
1574 tem = display_x_get_resource (dpyinfo,
1575 build_string (foreground_p
1576 ? "foreground"
1577 : "background"),
1578 empty_unibyte_string,
1579 build_string ("verticalScrollBar"),
1580 empty_unibyte_string);
1581 if (!STRINGP (tem))
1583 /* If nothing has been specified, scroll bars will use a
1584 toolkit-dependent default. Because these defaults are
1585 difficult to get at without actually creating a scroll
1586 bar, use nil to indicate that no color has been
1587 specified. */
1588 tem = Qnil;
1591 #else /* not USE_TOOLKIT_SCROLL_BARS */
1593 tem = Qnil;
1595 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1598 x_set_frame_parameters (f, list1 (Fcons (prop, tem)));
1599 return tem;
1605 #ifdef USE_X_TOOLKIT
1607 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1608 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1609 already be present because of the toolkit (Motif adds some of them,
1610 for example, but Xt doesn't). */
1612 static void
1613 hack_wm_protocols (struct frame *f, Widget widget)
1615 Display *dpy = XtDisplay (widget);
1616 Window w = XtWindow (widget);
1617 int need_delete = 1;
1618 int need_focus = 1;
1619 int need_save = 1;
1621 block_input ();
1623 Atom type;
1624 unsigned char *catoms;
1625 int format = 0;
1626 unsigned long nitems = 0;
1627 unsigned long bytes_after;
1629 if ((XGetWindowProperty (dpy, w,
1630 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1631 0, 100, False, XA_ATOM,
1632 &type, &format, &nitems, &bytes_after,
1633 &catoms)
1634 == Success)
1635 && format == 32 && type == XA_ATOM)
1637 Atom *atoms = (Atom *) catoms;
1638 while (nitems > 0)
1640 nitems--;
1641 if (atoms[nitems]
1642 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1643 need_delete = 0;
1644 else if (atoms[nitems]
1645 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1646 need_focus = 0;
1647 else if (atoms[nitems]
1648 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1649 need_save = 0;
1652 if (catoms)
1653 XFree (catoms);
1656 Atom props [10];
1657 int count = 0;
1658 if (need_delete)
1659 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1660 if (need_focus)
1661 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1662 if (need_save)
1663 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1664 if (count)
1665 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1666 XA_ATOM, 32, PropModeAppend,
1667 (unsigned char *) props, count);
1669 unblock_input ();
1671 #endif
1675 /* Support routines for XIC (X Input Context). */
1677 #ifdef HAVE_X_I18N
1679 static XFontSet xic_create_xfontset (struct frame *);
1680 static XIMStyle best_xim_style (XIMStyles *);
1683 /* Supported XIM styles, ordered by preference. */
1685 static const XIMStyle supported_xim_styles[] =
1687 XIMPreeditPosition | XIMStatusArea,
1688 XIMPreeditPosition | XIMStatusNothing,
1689 XIMPreeditPosition | XIMStatusNone,
1690 XIMPreeditNothing | XIMStatusArea,
1691 XIMPreeditNothing | XIMStatusNothing,
1692 XIMPreeditNothing | XIMStatusNone,
1693 XIMPreeditNone | XIMStatusArea,
1694 XIMPreeditNone | XIMStatusNothing,
1695 XIMPreeditNone | XIMStatusNone,
1700 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1701 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1703 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1705 /* Create an Xt fontset spec from the name of a base font.
1706 If `motif' is True use the Motif syntax. */
1707 char *
1708 xic_create_fontsetname (const char *base_fontname, int motif)
1710 const char *sep = motif ? ";" : ",";
1711 char *fontsetname;
1713 /* Make a fontset name from the base font name. */
1714 if (xic_default_fontset == base_fontname)
1716 /* There is no base font name, use the default. */
1717 fontsetname = xmalloc (strlen (base_fontname) + 2);
1718 strcpy (fontsetname, base_fontname);
1720 else
1722 /* Make a fontset name from the base font name.
1723 The font set will be made of the following elements:
1724 - the base font.
1725 - the base font where the charset spec is replaced by -*-*.
1726 - the same but with the family also replaced with -*-*-. */
1727 const char *p = base_fontname;
1728 ptrdiff_t i;
1730 for (i = 0; *p; p++)
1731 if (*p == '-') i++;
1732 if (i != 14)
1734 /* As the font name doesn't conform to XLFD, we can't
1735 modify it to generalize it to allcs and allfamilies.
1736 Use the specified font plus the default. */
1737 fontsetname = xmalloc (strlen (base_fontname)
1738 + strlen (xic_default_fontset) + 3);
1739 strcpy (fontsetname, base_fontname);
1740 strcat (fontsetname, sep);
1741 strcat (fontsetname, xic_default_fontset);
1743 else
1745 ptrdiff_t len;
1746 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1747 char *font_allcs = NULL;
1748 char *font_allfamilies = NULL;
1749 char *font_all = NULL;
1750 const char *allcs = "*-*-*-*-*-*-*";
1751 const char *allfamilies = "-*-*-";
1752 const char *all = "*-*-*-*-";
1753 char *base;
1755 for (i = 0, p = base_fontname; i < 8; p++)
1757 if (*p == '-')
1759 i++;
1760 if (i == 3)
1761 p1 = p + 1;
1762 else if (i == 7)
1763 p2 = p + 1;
1764 else if (i == 6)
1765 p3 = p + 1;
1768 /* If base_fontname specifies ADSTYLE, make it a
1769 wildcard. */
1770 if (*p3 != '*')
1772 ptrdiff_t diff = (p2 - p3) - 2;
1774 base = alloca (strlen (base_fontname) + 1);
1775 memcpy (base, base_fontname, p3 - base_fontname);
1776 base[p3 - base_fontname] = '*';
1777 base[(p3 - base_fontname) + 1] = '-';
1778 strcpy (base + (p3 - base_fontname) + 2, p2);
1779 p = base + (p - base_fontname) - diff;
1780 p1 = base + (p1 - base_fontname);
1781 p2 = base + (p2 - base_fontname) - diff;
1782 base_fontname = base;
1785 /* Build the font spec that matches all charsets. */
1786 len = p - base_fontname + strlen (allcs) + 1;
1787 font_allcs = alloca (len);
1788 memcpy (font_allcs, base_fontname, p - base_fontname);
1789 strcat (font_allcs, allcs);
1791 /* Build the font spec that matches all families and
1792 add-styles. */
1793 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1794 font_allfamilies = alloca (len);
1795 strcpy (font_allfamilies, allfamilies);
1796 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1797 strcat (font_allfamilies, allcs);
1799 /* Build the font spec that matches all. */
1800 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1801 font_all = alloca (len);
1802 strcpy (font_all, allfamilies);
1803 strcat (font_all, all);
1804 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1805 strcat (font_all, allcs);
1807 /* Build the actual font set name. */
1808 len = strlen (base_fontname) + strlen (font_allcs)
1809 + strlen (font_allfamilies) + strlen (font_all) + 5;
1810 fontsetname = xmalloc (len);
1811 strcpy (fontsetname, base_fontname);
1812 strcat (fontsetname, sep);
1813 strcat (fontsetname, font_allcs);
1814 strcat (fontsetname, sep);
1815 strcat (fontsetname, font_allfamilies);
1816 strcat (fontsetname, sep);
1817 strcat (fontsetname, font_all);
1820 if (motif)
1821 return strcat (fontsetname, ":");
1822 return fontsetname;
1824 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1826 #ifdef DEBUG_XIC_FONTSET
1827 static void
1828 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1829 int missing_count)
1831 if (xfs)
1832 fprintf (stderr, "XIC Fontset created: %s\n", name);
1833 else
1835 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1836 while (missing_count-- > 0)
1838 fprintf (stderr, " missing: %s\n", *missing_list);
1839 missing_list++;
1844 #endif
1846 static XFontSet
1847 xic_create_xfontset (struct frame *f)
1849 XFontSet xfs = NULL;
1850 struct font *font = FRAME_FONT (f);
1851 int pixel_size = font->pixel_size;
1852 Lisp_Object rest, frame;
1854 /* See if there is another frame already using same fontset. */
1855 FOR_EACH_FRAME (rest, frame)
1857 struct frame *cf = XFRAME (frame);
1859 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1860 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1861 && FRAME_FONT (f)
1862 && FRAME_FONT (f)->pixel_size == pixel_size)
1864 xfs = FRAME_XIC_FONTSET (cf);
1865 break;
1869 if (! xfs)
1871 char buf[256];
1872 char **missing_list;
1873 int missing_count;
1874 char *def_string;
1875 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1877 sprintf (buf, xlfd_format, pixel_size);
1878 missing_list = NULL;
1879 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1880 &missing_list, &missing_count, &def_string);
1881 #ifdef DEBUG_XIC_FONTSET
1882 print_fontset_result (xfs, buf, missing_list, missing_count);
1883 #endif
1884 if (missing_list)
1885 XFreeStringList (missing_list);
1886 if (! xfs)
1888 /* List of pixel sizes most likely available. Find one that
1889 is closest to pixel_size. */
1890 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1891 int *smaller, *larger;
1893 for (smaller = sizes; smaller[1]; smaller++)
1894 if (smaller[1] >= pixel_size)
1895 break;
1896 larger = smaller + 1;
1897 if (*larger == pixel_size)
1898 larger++;
1899 while (*smaller || *larger)
1901 int this_size;
1903 if (! *larger)
1904 this_size = *smaller--;
1905 else if (! *smaller)
1906 this_size = *larger++;
1907 else if (pixel_size - *smaller < *larger - pixel_size)
1908 this_size = *smaller--;
1909 else
1910 this_size = *larger++;
1911 sprintf (buf, xlfd_format, this_size);
1912 missing_list = NULL;
1913 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1914 &missing_list, &missing_count, &def_string);
1915 #ifdef DEBUG_XIC_FONTSET
1916 print_fontset_result (xfs, buf, missing_list, missing_count);
1917 #endif
1918 if (missing_list)
1919 XFreeStringList (missing_list);
1920 if (xfs)
1921 break;
1924 if (! xfs)
1926 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
1928 missing_list = NULL;
1929 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1930 &missing_list, &missing_count, &def_string);
1931 #ifdef DEBUG_XIC_FONTSET
1932 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1933 #endif
1934 if (missing_list)
1935 XFreeStringList (missing_list);
1940 return xfs;
1943 /* Free the X fontset of frame F if it is the last frame using it. */
1945 void
1946 xic_free_xfontset (struct frame *f)
1948 Lisp_Object rest, frame;
1949 bool shared_p = 0;
1951 if (!FRAME_XIC_FONTSET (f))
1952 return;
1954 /* See if there is another frame sharing the same fontset. */
1955 FOR_EACH_FRAME (rest, frame)
1957 struct frame *cf = XFRAME (frame);
1958 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1959 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1960 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1962 shared_p = 1;
1963 break;
1967 if (!shared_p)
1968 /* The fontset is not used anymore. It is safe to free it. */
1969 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1971 FRAME_XIC_FONTSET (f) = NULL;
1975 /* Value is the best input style, given user preferences USER (already
1976 checked to be supported by Emacs), and styles supported by the
1977 input method XIM. */
1979 static XIMStyle
1980 best_xim_style (XIMStyles *xim)
1982 int i, j;
1983 int nr_supported = ARRAYELTS (supported_xim_styles);
1985 for (i = 0; i < nr_supported; ++i)
1986 for (j = 0; j < xim->count_styles; ++j)
1987 if (supported_xim_styles[i] == xim->supported_styles[j])
1988 return supported_xim_styles[i];
1990 /* Return the default style. */
1991 return XIMPreeditNothing | XIMStatusNothing;
1994 /* Create XIC for frame F. */
1996 void
1997 create_frame_xic (struct frame *f)
1999 XIM xim;
2000 XIC xic = NULL;
2001 XFontSet xfs = NULL;
2002 XVaNestedList status_attr = NULL;
2003 XVaNestedList preedit_attr = NULL;
2004 XRectangle s_area;
2005 XPoint spot;
2006 XIMStyle xic_style;
2008 if (FRAME_XIC (f))
2009 goto out;
2011 xim = FRAME_X_XIM (f);
2012 if (!xim)
2013 goto out;
2015 /* Determine XIC style. */
2016 xic_style = best_xim_style (FRAME_X_XIM_STYLES (f));
2018 /* Create X fontset. */
2019 if (xic_style & (XIMPreeditPosition | XIMStatusArea))
2021 xfs = xic_create_xfontset (f);
2022 if (!xfs)
2023 goto out;
2025 FRAME_XIC_FONTSET (f) = xfs;
2028 if (xic_style & XIMPreeditPosition)
2030 spot.x = 0; spot.y = 1;
2031 preedit_attr = XVaCreateNestedList (0,
2032 XNFontSet, xfs,
2033 XNForeground,
2034 FRAME_FOREGROUND_PIXEL (f),
2035 XNBackground,
2036 FRAME_BACKGROUND_PIXEL (f),
2037 (xic_style & XIMPreeditPosition
2038 ? XNSpotLocation
2039 : NULL),
2040 &spot,
2041 NULL);
2043 if (!preedit_attr)
2044 goto out;
2047 if (xic_style & XIMStatusArea)
2049 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2050 status_attr = XVaCreateNestedList (0,
2051 XNArea,
2052 &s_area,
2053 XNFontSet,
2054 xfs,
2055 XNForeground,
2056 FRAME_FOREGROUND_PIXEL (f),
2057 XNBackground,
2058 FRAME_BACKGROUND_PIXEL (f),
2059 NULL);
2061 if (!status_attr)
2062 goto out;
2065 if (preedit_attr && status_attr)
2066 xic = XCreateIC (xim,
2067 XNInputStyle, xic_style,
2068 XNClientWindow, FRAME_X_WINDOW (f),
2069 XNFocusWindow, FRAME_X_WINDOW (f),
2070 XNStatusAttributes, status_attr,
2071 XNPreeditAttributes, preedit_attr,
2072 NULL);
2073 else if (preedit_attr)
2074 xic = XCreateIC (xim,
2075 XNInputStyle, xic_style,
2076 XNClientWindow, FRAME_X_WINDOW (f),
2077 XNFocusWindow, FRAME_X_WINDOW (f),
2078 XNPreeditAttributes, preedit_attr,
2079 NULL);
2080 else if (status_attr)
2081 xic = XCreateIC (xim,
2082 XNInputStyle, xic_style,
2083 XNClientWindow, FRAME_X_WINDOW (f),
2084 XNFocusWindow, FRAME_X_WINDOW (f),
2085 XNStatusAttributes, status_attr,
2086 NULL);
2087 else
2088 xic = XCreateIC (xim,
2089 XNInputStyle, xic_style,
2090 XNClientWindow, FRAME_X_WINDOW (f),
2091 XNFocusWindow, FRAME_X_WINDOW (f),
2092 NULL);
2094 if (!xic)
2095 goto out;
2097 FRAME_XIC (f) = xic;
2098 FRAME_XIC_STYLE (f) = xic_style;
2099 xfs = NULL; /* Don't free below. */
2101 out:
2103 if (xfs)
2104 free_frame_xic (f);
2106 if (preedit_attr)
2107 XFree (preedit_attr);
2109 if (status_attr)
2110 XFree (status_attr);
2114 /* Destroy XIC and free XIC fontset of frame F, if any. */
2116 void
2117 free_frame_xic (struct frame *f)
2119 if (FRAME_XIC (f) == NULL)
2120 return;
2122 XDestroyIC (FRAME_XIC (f));
2123 xic_free_xfontset (f);
2125 FRAME_XIC (f) = NULL;
2129 /* Place preedit area for XIC of window W's frame to specified
2130 pixel position X/Y. X and Y are relative to window W. */
2132 void
2133 xic_set_preeditarea (struct window *w, int x, int y)
2135 struct frame *f = XFRAME (w->frame);
2136 XVaNestedList attr;
2137 XPoint spot;
2139 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2140 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2141 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2142 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2143 XFree (attr);
2147 /* Place status area for XIC in bottom right corner of frame F.. */
2149 void
2150 xic_set_statusarea (struct frame *f)
2152 XIC xic = FRAME_XIC (f);
2153 XVaNestedList attr;
2154 XRectangle area;
2155 XRectangle *needed;
2157 /* Negotiate geometry of status area. If input method has existing
2158 status area, use its current size. */
2159 area.x = area.y = area.width = area.height = 0;
2160 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2161 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2162 XFree (attr);
2164 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2165 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2166 XFree (attr);
2168 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2170 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2171 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2172 XFree (attr);
2175 area.width = needed->width;
2176 area.height = needed->height;
2177 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2178 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2179 - FRAME_MENUBAR_HEIGHT (f)
2180 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2181 - FRAME_INTERNAL_BORDER_WIDTH (f));
2182 XFree (needed);
2184 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2185 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2186 XFree (attr);
2190 /* Set X fontset for XIC of frame F, using base font name
2191 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2193 void
2194 xic_set_xfontset (struct frame *f, const char *base_fontname)
2196 XVaNestedList attr;
2197 XFontSet xfs;
2199 xic_free_xfontset (f);
2201 xfs = xic_create_xfontset (f);
2203 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2204 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2205 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2206 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2207 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2208 XFree (attr);
2210 FRAME_XIC_FONTSET (f) = xfs;
2213 #endif /* HAVE_X_I18N */
2217 #ifdef USE_X_TOOLKIT
2219 /* Create and set up the X widget for frame F. */
2221 static void
2222 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2224 XClassHint class_hints;
2225 XSetWindowAttributes attributes;
2226 unsigned long attribute_mask;
2227 Widget shell_widget;
2228 Widget pane_widget;
2229 Widget frame_widget;
2230 Arg al [25];
2231 int ac;
2233 block_input ();
2235 /* Use the resource name as the top-level widget name
2236 for looking up resources. Make a non-Lisp copy
2237 for the window manager, so GC relocation won't bother it.
2239 Elsewhere we specify the window name for the window manager. */
2240 f->namebuf = xstrdup (SSDATA (Vx_resource_name));
2242 ac = 0;
2243 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2244 XtSetArg (al[ac], XtNinput, 1); ac++;
2245 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2246 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2247 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2248 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2249 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2250 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2251 applicationShellWidgetClass,
2252 FRAME_X_DISPLAY (f), al, ac);
2254 f->output_data.x->widget = shell_widget;
2255 /* maybe_set_screen_title_format (shell_widget); */
2257 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2258 NULL, shell_widget, False,
2259 NULL, NULL, NULL, NULL);
2261 ac = 0;
2262 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2263 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2264 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2265 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2266 XtSetValues (pane_widget, al, ac);
2267 f->output_data.x->column_widget = pane_widget;
2269 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2270 the emacs screen when changing menubar. This reduces flickering. */
2272 ac = 0;
2273 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2274 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2275 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2276 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2277 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2278 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2279 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2280 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2281 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2282 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2283 al, ac);
2285 f->output_data.x->edit_widget = frame_widget;
2287 XtManageChild (frame_widget);
2289 /* Do some needed geometry management. */
2291 Arg gal[3];
2292 int gac = 0;
2293 int extra_borders = 0;
2294 int menubar_size
2295 = (f->output_data.x->menubar_widget
2296 ? (f->output_data.x->menubar_widget->core.height
2297 + f->output_data.x->menubar_widget->core.border_width)
2298 : 0);
2300 #if 0 /* Experimentally, we now get the right results
2301 for -geometry -0-0 without this. 24 Aug 96, rms. */
2302 if (FRAME_EXTERNAL_MENU_BAR (f))
2304 Dimension ibw = 0;
2305 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2306 menubar_size += ibw;
2308 #endif
2310 FRAME_MENUBAR_HEIGHT (f) = menubar_size;
2312 #ifndef USE_LUCID
2313 /* Motif seems to need this amount added to the sizes
2314 specified for the shell widget. The Athena/Lucid widgets don't.
2315 Both conclusions reached experimentally. -- rms. */
2316 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2317 &extra_borders, NULL);
2318 extra_borders *= 2;
2319 #endif
2321 f->shell_position = xmalloc (sizeof "=x++" + 4 * INT_STRLEN_BOUND (int));
2323 /* Convert our geometry parameters into a geometry string
2324 and specify it.
2325 Note that we do not specify here whether the position
2326 is a user-specified or program-specified one.
2327 We pass that information later, in x_wm_set_size_hints. */
2329 int left = f->left_pos;
2330 int xneg = window_prompting & XNegative;
2331 int top = f->top_pos;
2332 int yneg = window_prompting & YNegative;
2333 if (xneg)
2334 left = -left;
2335 if (yneg)
2336 top = -top;
2338 if (window_prompting & USPosition)
2339 sprintf (f->shell_position, "=%dx%d%c%d%c%d",
2340 FRAME_PIXEL_WIDTH (f) + extra_borders,
2341 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2342 (xneg ? '-' : '+'), left,
2343 (yneg ? '-' : '+'), top);
2344 else
2346 sprintf (f->shell_position, "=%dx%d",
2347 FRAME_PIXEL_WIDTH (f) + extra_borders,
2348 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2350 /* Setting x and y when the position is not specified in
2351 the geometry string will set program position in the WM hints.
2352 If Emacs had just one program position, we could set it in
2353 fallback resources, but since each make-frame call can specify
2354 different program positions, this is easier. */
2355 XtSetArg (gal[gac], XtNx, left); gac++;
2356 XtSetArg (gal[gac], XtNy, top); gac++;
2360 XtSetArg (gal[gac], XtNgeometry, f->shell_position); gac++;
2361 XtSetValues (shell_widget, gal, gac);
2364 XtManageChild (pane_widget);
2365 XtRealizeWidget (shell_widget);
2367 if (FRAME_X_EMBEDDED_P (f))
2368 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2369 f->output_data.x->parent_desc, 0, 0);
2371 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2373 validate_x_resource_name ();
2375 class_hints.res_name = SSDATA (Vx_resource_name);
2376 class_hints.res_class = SSDATA (Vx_resource_class);
2377 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2379 #ifdef HAVE_X_I18N
2380 FRAME_XIC (f) = NULL;
2381 if (use_xim)
2382 create_frame_xic (f);
2383 #endif
2385 f->output_data.x->wm_hints.input = True;
2386 f->output_data.x->wm_hints.flags |= InputHint;
2387 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2388 &f->output_data.x->wm_hints);
2390 hack_wm_protocols (f, shell_widget);
2392 #ifdef HACK_EDITRES
2393 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2394 #endif
2396 /* Do a stupid property change to force the server to generate a
2397 PropertyNotify event so that the event_stream server timestamp will
2398 be initialized to something relevant to the time we created the window.
2400 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2401 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
2402 XA_ATOM, 32, PropModeAppend, NULL, 0);
2404 /* Make all the standard events reach the Emacs frame. */
2405 attributes.event_mask = STANDARD_EVENT_SET;
2407 #ifdef HAVE_X_I18N
2408 if (FRAME_XIC (f))
2410 /* XIM server might require some X events. */
2411 unsigned long fevent = NoEventMask;
2412 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2413 attributes.event_mask |= fevent;
2415 #endif /* HAVE_X_I18N */
2417 attribute_mask = CWEventMask;
2418 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2419 attribute_mask, &attributes);
2421 XtMapWidget (frame_widget);
2423 /* x_set_name normally ignores requests to set the name if the
2424 requested name is the same as the current name. This is the one
2425 place where that assumption isn't correct; f->name is set, but
2426 the X server hasn't been told. */
2428 Lisp_Object name;
2429 bool explicit = f->explicit_name;
2431 f->explicit_name = 0;
2432 name = f->name;
2433 fset_name (f, Qnil);
2434 x_set_name (f, name, explicit);
2437 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2438 f->output_data.x->current_cursor
2439 = f->output_data.x->text_cursor);
2441 unblock_input ();
2443 /* This is a no-op, except under Motif. Make sure main areas are
2444 set to something reasonable, in case we get an error later. */
2445 lw_set_main_areas (pane_widget, 0, frame_widget);
2448 #else /* not USE_X_TOOLKIT */
2449 #ifdef USE_GTK
2450 static void
2451 x_window (struct frame *f)
2453 if (! xg_create_frame_widgets (f))
2454 error ("Unable to create window");
2456 #ifdef HAVE_X_I18N
2457 FRAME_XIC (f) = NULL;
2458 if (use_xim)
2460 block_input ();
2461 create_frame_xic (f);
2462 if (FRAME_XIC (f))
2464 /* XIM server might require some X events. */
2465 unsigned long fevent = NoEventMask;
2466 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2468 if (fevent != NoEventMask)
2470 XSetWindowAttributes attributes;
2471 XWindowAttributes wattr;
2472 unsigned long attribute_mask;
2474 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2475 &wattr);
2476 attributes.event_mask = wattr.your_event_mask | fevent;
2477 attribute_mask = CWEventMask;
2478 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2479 attribute_mask, &attributes);
2482 unblock_input ();
2484 #endif
2487 #else /*! USE_GTK */
2488 /* Create and set up the X window for frame F. */
2490 static void
2491 x_window (struct frame *f)
2493 XClassHint class_hints;
2494 XSetWindowAttributes attributes;
2495 unsigned long attribute_mask;
2497 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2498 attributes.border_pixel = f->output_data.x->border_pixel;
2499 attributes.bit_gravity = StaticGravity;
2500 attributes.backing_store = NotUseful;
2501 attributes.save_under = True;
2502 attributes.event_mask = STANDARD_EVENT_SET;
2503 attributes.colormap = FRAME_X_COLORMAP (f);
2504 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2505 | CWColormap);
2507 block_input ();
2508 FRAME_X_WINDOW (f)
2509 = XCreateWindow (FRAME_X_DISPLAY (f),
2510 f->output_data.x->parent_desc,
2511 f->left_pos,
2512 f->top_pos,
2513 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2514 f->border_width,
2515 CopyFromParent, /* depth */
2516 InputOutput, /* class */
2517 FRAME_X_VISUAL (f),
2518 attribute_mask, &attributes);
2520 #ifdef HAVE_X_I18N
2521 if (use_xim)
2523 create_frame_xic (f);
2524 if (FRAME_XIC (f))
2526 /* XIM server might require some X events. */
2527 unsigned long fevent = NoEventMask;
2528 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2529 attributes.event_mask |= fevent;
2530 attribute_mask = CWEventMask;
2531 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2532 attribute_mask, &attributes);
2535 #endif /* HAVE_X_I18N */
2537 validate_x_resource_name ();
2539 class_hints.res_name = SSDATA (Vx_resource_name);
2540 class_hints.res_class = SSDATA (Vx_resource_class);
2541 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2543 /* This indicates that we use the "Passive Input" input model.
2544 Unless we do this, we don't get the Focus{In,Out} events that we
2545 need to draw the cursor correctly. Accursed bureaucrats.
2546 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2548 f->output_data.x->wm_hints.input = True;
2549 f->output_data.x->wm_hints.flags |= InputHint;
2550 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2551 &f->output_data.x->wm_hints);
2552 f->output_data.x->wm_hints.icon_pixmap = None;
2554 /* Request "save yourself" and "delete window" commands from wm. */
2556 Atom protocols[2];
2557 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2558 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2559 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2562 /* x_set_name normally ignores requests to set the name if the
2563 requested name is the same as the current name. This is the one
2564 place where that assumption isn't correct; f->name is set, but
2565 the X server hasn't been told. */
2567 Lisp_Object name;
2568 bool explicit = f->explicit_name;
2570 f->explicit_name = 0;
2571 name = f->name;
2572 fset_name (f, Qnil);
2573 x_set_name (f, name, explicit);
2576 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2577 f->output_data.x->current_cursor
2578 = f->output_data.x->text_cursor);
2580 unblock_input ();
2582 if (FRAME_X_WINDOW (f) == 0)
2583 error ("Unable to create window");
2586 #endif /* not USE_GTK */
2587 #endif /* not USE_X_TOOLKIT */
2589 /* Verify that the icon position args for this window are valid. */
2591 static void
2592 x_icon_verify (struct frame *f, Lisp_Object parms)
2594 Lisp_Object icon_x, icon_y;
2596 /* Set the position of the icon. Note that twm groups all
2597 icons in an icon window. */
2598 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2599 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2600 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2602 CHECK_NUMBER (icon_x);
2603 CHECK_NUMBER (icon_y);
2605 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2606 error ("Both left and top icon corners of icon must be specified");
2609 /* Handle the icon stuff for this window. Perhaps later we might
2610 want an x_set_icon_position which can be called interactively as
2611 well. */
2613 static void
2614 x_icon (struct frame *f, Lisp_Object parms)
2616 Lisp_Object icon_x, icon_y;
2617 #if 0
2618 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2619 #endif
2621 /* Set the position of the icon. Note that twm groups all
2622 icons in an icon window. */
2623 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2624 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2625 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2627 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2628 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2630 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2631 error ("Both left and top icon corners of icon must be specified");
2633 block_input ();
2635 if (! EQ (icon_x, Qunbound))
2636 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2638 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2639 but x_create_frame still needs it. */
2640 /* Start up iconic or window? */
2641 x_wm_set_window_state
2642 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2643 Qicon)
2644 ? IconicState
2645 : NormalState));
2646 #endif
2648 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2649 ? f->icon_name
2650 : f->name)));
2652 unblock_input ();
2655 /* Make the GCs needed for this window, setting the
2656 background, border and mouse colors; also create the
2657 mouse cursor and the gray border tile. */
2659 static void
2660 x_make_gc (struct frame *f)
2662 XGCValues gc_values;
2664 block_input ();
2666 /* Create the GCs of this frame.
2667 Note that many default values are used. */
2669 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2670 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2671 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2672 f->output_data.x->normal_gc
2673 = XCreateGC (FRAME_X_DISPLAY (f),
2674 FRAME_X_WINDOW (f),
2675 GCLineWidth | GCForeground | GCBackground,
2676 &gc_values);
2678 /* Reverse video style. */
2679 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2680 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2681 f->output_data.x->reverse_gc
2682 = XCreateGC (FRAME_X_DISPLAY (f),
2683 FRAME_X_WINDOW (f),
2684 GCForeground | GCBackground | GCLineWidth,
2685 &gc_values);
2687 /* Cursor has cursor-color background, background-color foreground. */
2688 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2689 gc_values.background = f->output_data.x->cursor_pixel;
2690 gc_values.fill_style = FillOpaqueStippled;
2691 f->output_data.x->cursor_gc
2692 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2693 (GCForeground | GCBackground
2694 | GCFillStyle | GCLineWidth),
2695 &gc_values);
2697 /* Create the gray border tile used when the pointer is not in
2698 the frame. Since this depends on the frame's pixel values,
2699 this must be done on a per-frame basis. */
2700 f->output_data.x->border_tile
2701 = (XCreatePixmapFromBitmapData
2702 (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
2703 gray_bits, gray_width, gray_height,
2704 FRAME_FOREGROUND_PIXEL (f),
2705 FRAME_BACKGROUND_PIXEL (f),
2706 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2708 unblock_input ();
2712 /* Free what was allocated in x_make_gc. */
2714 void
2715 x_free_gcs (struct frame *f)
2717 Display *dpy = FRAME_X_DISPLAY (f);
2719 block_input ();
2721 if (f->output_data.x->normal_gc)
2723 XFreeGC (dpy, f->output_data.x->normal_gc);
2724 f->output_data.x->normal_gc = 0;
2727 if (f->output_data.x->reverse_gc)
2729 XFreeGC (dpy, f->output_data.x->reverse_gc);
2730 f->output_data.x->reverse_gc = 0;
2733 if (f->output_data.x->cursor_gc)
2735 XFreeGC (dpy, f->output_data.x->cursor_gc);
2736 f->output_data.x->cursor_gc = 0;
2739 if (f->output_data.x->border_tile)
2741 XFreePixmap (dpy, f->output_data.x->border_tile);
2742 f->output_data.x->border_tile = 0;
2745 unblock_input ();
2749 /* Handler for signals raised during x_create_frame and
2750 x_create_tip_frame. FRAME is the frame which is partially
2751 constructed. */
2753 static Lisp_Object
2754 unwind_create_frame (Lisp_Object frame)
2756 struct frame *f = XFRAME (frame);
2758 /* If frame is already dead, nothing to do. This can happen if the
2759 display is disconnected after the frame has become official, but
2760 before x_create_frame removes the unwind protect. */
2761 if (!FRAME_LIVE_P (f))
2762 return Qnil;
2764 /* If frame is ``official'', nothing to do. */
2765 if (NILP (Fmemq (frame, Vframe_list)))
2767 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2768 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2769 #endif
2771 x_free_frame_resources (f);
2772 free_glyphs (f);
2774 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2775 /* Check that reference counts are indeed correct. */
2776 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2777 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2778 #endif
2779 return Qt;
2782 return Qnil;
2785 static void
2786 do_unwind_create_frame (Lisp_Object frame)
2788 unwind_create_frame (frame);
2791 static void
2792 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2794 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2795 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2796 RES_TYPE_STRING);
2797 Lisp_Object font = Qnil;
2798 if (EQ (font_param, Qunbound))
2799 font_param = Qnil;
2801 if (NILP (font_param))
2803 /* System font should take precedence over X resources. We suggest this
2804 regardless of font-use-system-font because .emacs may not have been
2805 read yet. */
2806 const char *system_font = xsettings_get_system_font ();
2807 if (system_font)
2808 font = font_open_by_name (f, build_unibyte_string (system_font));
2811 if (NILP (font))
2812 font = !NILP (font_param) ? font_param
2813 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2815 if (! FONTP (font) && ! STRINGP (font))
2817 const char *names[]
2819 #ifdef HAVE_XFT
2820 /* This will find the normal Xft font. */
2821 "monospace-10",
2822 #endif
2823 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2824 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2825 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2826 /* This was formerly the first thing tried, but it finds
2827 too many fonts and takes too long. */
2828 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2829 /* If those didn't work, look for something which will
2830 at least work. */
2831 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2832 "fixed",
2833 NULL };
2834 int i;
2836 for (i = 0; names[i]; i++)
2838 font = font_open_by_name (f, build_unibyte_string (names[i]));
2839 if (! NILP (font))
2840 break;
2842 if (NILP (font))
2843 error ("No suitable font was found");
2845 else if (!NILP (font_param))
2847 /* Remember the explicit font parameter, so we can re-apply it after
2848 we've applied the `default' face settings. */
2849 x_set_frame_parameters (f, list1 (Fcons (Qfont_param, font_param)));
2852 /* This call will make X resources override any system font setting. */
2853 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2857 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2858 0, 1, 0,
2859 doc: /* Send the size hints for frame FRAME to the window manager.
2860 If FRAME is omitted or nil, use the selected frame.
2861 Signal error if FRAME is not an X frame. */)
2862 (Lisp_Object frame)
2864 struct frame *f = decode_window_system_frame (frame);
2866 block_input ();
2867 x_wm_set_size_hint (f, 0, 0);
2868 unblock_input ();
2869 return Qnil;
2872 static void
2873 set_machine_and_pid_properties (struct frame *f)
2875 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2876 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2877 NULL, 0, NULL, NULL, NULL);
2878 pid_t pid = getpid ();
2879 if (pid <= 0xffffffffu)
2881 unsigned long xpid = pid;
2882 XChangeProperty (FRAME_X_DISPLAY (f),
2883 FRAME_OUTER_WINDOW (f),
2884 XInternAtom (FRAME_X_DISPLAY (f),
2885 "_NET_WM_PID",
2886 False),
2887 XA_CARDINAL, 32, PropModeReplace,
2888 (unsigned char *) &xpid, 1);
2892 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2893 1, 1, 0,
2894 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2895 Return an Emacs frame object. PARMS is an alist of frame parameters.
2896 If the parameters specify that the frame should not have a minibuffer,
2897 and do not specify a specific minibuffer window to use, then
2898 `default-minibuffer-frame' must be a frame whose minibuffer can be
2899 shared by the new frame.
2901 This function is an internal primitive--use `make-frame' instead. */)
2902 (Lisp_Object parms)
2904 struct frame *f;
2905 Lisp_Object frame, tem;
2906 Lisp_Object name;
2907 int minibuffer_only = 0;
2908 long window_prompting = 0;
2909 ptrdiff_t count = SPECPDL_INDEX ();
2910 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2911 Lisp_Object display;
2912 struct x_display_info *dpyinfo = NULL;
2913 Lisp_Object parent;
2914 struct kboard *kb;
2916 parms = Fcopy_alist (parms);
2918 /* Use this general default value to start with
2919 until we know if this frame has a specified name. */
2920 Vx_resource_name = Vinvocation_name;
2922 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
2923 if (EQ (display, Qunbound))
2924 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
2925 if (EQ (display, Qunbound))
2926 display = Qnil;
2927 dpyinfo = check_x_display_info (display);
2928 kb = dpyinfo->terminal->kboard;
2930 if (!dpyinfo->terminal->name)
2931 error ("Terminal is not live, can't create new frames on it");
2933 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
2934 if (!STRINGP (name)
2935 && ! EQ (name, Qunbound)
2936 && ! NILP (name))
2937 error ("Invalid frame name--not a string or nil");
2939 if (STRINGP (name))
2940 Vx_resource_name = name;
2942 /* See if parent window is specified. */
2943 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
2944 if (EQ (parent, Qunbound))
2945 parent = Qnil;
2946 if (! NILP (parent))
2947 CHECK_NUMBER (parent);
2949 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
2950 /* No need to protect DISPLAY because that's not used after passing
2951 it to make_frame_without_minibuffer. */
2952 frame = Qnil;
2953 GCPRO4 (parms, parent, name, frame);
2954 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
2955 RES_TYPE_SYMBOL);
2956 if (EQ (tem, Qnone) || NILP (tem))
2957 f = make_frame_without_minibuffer (Qnil, kb, display);
2958 else if (EQ (tem, Qonly))
2960 f = make_minibuffer_frame ();
2961 minibuffer_only = 1;
2963 else if (WINDOWP (tem))
2964 f = make_frame_without_minibuffer (tem, kb, display);
2965 else
2966 f = make_frame (1);
2968 XSETFRAME (frame, f);
2970 f->terminal = dpyinfo->terminal;
2972 f->output_method = output_x_window;
2973 f->output_data.x = xzalloc (sizeof *f->output_data.x);
2974 f->output_data.x->icon_bitmap = -1;
2975 FRAME_FONTSET (f) = -1;
2976 f->output_data.x->scroll_bar_foreground_pixel = -1;
2977 f->output_data.x->scroll_bar_background_pixel = -1;
2978 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
2979 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2980 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2981 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
2982 f->output_data.x->white_relief.pixel = -1;
2983 f->output_data.x->black_relief.pixel = -1;
2985 fset_icon_name (f,
2986 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
2987 RES_TYPE_STRING));
2988 if (! STRINGP (f->icon_name))
2989 fset_icon_name (f, Qnil);
2991 FRAME_DISPLAY_INFO (f) = dpyinfo;
2993 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
2994 record_unwind_protect (do_unwind_create_frame, frame);
2996 /* These colors will be set anyway later, but it's important
2997 to get the color reference counts right, so initialize them! */
2999 Lisp_Object black;
3000 struct gcpro gcpro1;
3002 /* Function x_decode_color can signal an error. Make
3003 sure to initialize color slots so that we won't try
3004 to free colors we haven't allocated. */
3005 FRAME_FOREGROUND_PIXEL (f) = -1;
3006 FRAME_BACKGROUND_PIXEL (f) = -1;
3007 f->output_data.x->cursor_pixel = -1;
3008 f->output_data.x->cursor_foreground_pixel = -1;
3009 f->output_data.x->border_pixel = -1;
3010 f->output_data.x->mouse_pixel = -1;
3012 black = build_string ("black");
3013 GCPRO1 (black);
3014 FRAME_FOREGROUND_PIXEL (f)
3015 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3016 FRAME_BACKGROUND_PIXEL (f)
3017 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3018 f->output_data.x->cursor_pixel
3019 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3020 f->output_data.x->cursor_foreground_pixel
3021 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3022 f->output_data.x->border_pixel
3023 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3024 f->output_data.x->mouse_pixel
3025 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3026 UNGCPRO;
3029 /* Specify the parent under which to make this X window. */
3030 if (!NILP (parent))
3032 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3033 f->output_data.x->explicit_parent = 1;
3035 else
3037 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
3038 f->output_data.x->explicit_parent = 0;
3041 /* Set the name; the functions to which we pass f expect the name to
3042 be set. */
3043 if (EQ (name, Qunbound) || NILP (name))
3045 fset_name (f, build_string (dpyinfo->x_id_name));
3046 f->explicit_name = 0;
3048 else
3050 fset_name (f, name);
3051 f->explicit_name = 1;
3052 /* Use the frame's title when getting resources for this frame. */
3053 specbind (Qx_resource_name, name);
3056 #ifdef HAVE_FREETYPE
3057 #ifdef HAVE_XFT
3058 register_font_driver (&xftfont_driver, f);
3059 #else /* not HAVE_XFT */
3060 register_font_driver (&ftxfont_driver, f);
3061 #endif /* not HAVE_XFT */
3062 #endif /* HAVE_FREETYPE */
3063 register_font_driver (&xfont_driver, f);
3065 x_default_parameter (f, parms, Qfont_backend, Qnil,
3066 "fontBackend", "FontBackend", RES_TYPE_STRING);
3068 /* Extract the window parameters from the supplied values
3069 that are needed to determine window geometry. */
3070 x_default_font_parameter (f, parms);
3071 if (!FRAME_FONT (f))
3073 delete_frame (frame, Qnoelisp);
3074 error ("Invalid frame font");
3077 /* Frame contents get displaced if an embedded X window has a border. */
3078 if (! FRAME_X_EMBEDDED_P (f))
3079 x_default_parameter (f, parms, Qborder_width, make_number (0),
3080 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3082 /* This defaults to 1 in order to match xterm. We recognize either
3083 internalBorderWidth or internalBorder (which is what xterm calls
3084 it). */
3085 if (NILP (Fassq (Qinternal_border_width, parms)))
3087 Lisp_Object value;
3089 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3090 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3091 if (! EQ (value, Qunbound))
3092 parms = Fcons (Fcons (Qinternal_border_width, value),
3093 parms);
3095 x_default_parameter (f, parms, Qinternal_border_width,
3096 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3097 make_number (0),
3098 #else
3099 make_number (1),
3100 #endif
3101 "internalBorderWidth", "internalBorderWidth",
3102 RES_TYPE_NUMBER);
3103 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
3104 NULL, NULL, RES_TYPE_NUMBER);
3105 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
3106 NULL, NULL, RES_TYPE_NUMBER);
3107 x_default_parameter (f, parms, Qvertical_scroll_bars,
3108 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3109 Qright,
3110 #else
3111 Qleft,
3112 #endif
3113 "verticalScrollBars", "ScrollBars",
3114 RES_TYPE_SYMBOL);
3115 x_default_parameter (f, parms, Qhorizontal_scroll_bars, Qnil,
3116 "horizontalScrollBars", "ScrollBars",
3117 RES_TYPE_SYMBOL);
3118 /* Also do the stuff which must be set before the window exists. */
3119 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3120 "foreground", "Foreground", RES_TYPE_STRING);
3121 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3122 "background", "Background", RES_TYPE_STRING);
3123 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3124 "pointerColor", "Foreground", RES_TYPE_STRING);
3125 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3126 "borderColor", "BorderColor", RES_TYPE_STRING);
3127 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3128 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3129 x_default_parameter (f, parms, Qline_spacing, Qnil,
3130 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3131 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3132 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3133 x_default_parameter (f, parms, Qright_fringe, Qnil,
3134 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3136 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3137 "scrollBarForeground",
3138 "ScrollBarForeground", 1);
3139 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3140 "scrollBarBackground",
3141 "ScrollBarBackground", 0);
3143 #ifdef GLYPH_DEBUG
3144 image_cache_refcount =
3145 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3146 dpyinfo_refcount = dpyinfo->reference_count;
3147 #endif /* GLYPH_DEBUG */
3149 /* Init faces before x_default_parameter is called for the
3150 scroll-bar-width parameter because otherwise we end up in
3151 init_iterator with a null face cache, which should not happen. */
3152 init_frame_faces (f);
3154 /* The following call of change_frame_size is needed since otherwise
3155 x_set_tool_bar_lines will already work with the character sizes
3156 installed by init_frame_faces while the frame's pixel size is
3157 still calculated from a character size of 1 and we subsequently
3158 hit the (height >= 0) assertion in window_box_height.
3160 The non-pixelwise code apparently worked around this because it
3161 had one frame line vs one toolbar line which left us with a zero
3162 root window height which was obviously wrong as well ... */
3163 adjust_frame_size (f, FRAME_COLS (f) * FRAME_COLUMN_WIDTH (f),
3164 FRAME_LINES (f) * FRAME_LINE_HEIGHT (f), 5, 1);
3166 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3167 look up the X resources controlling the menu-bar and tool-bar
3168 here; they are processed specially at startup, and reflected in
3169 the values of the mode variables. */
3171 x_default_parameter (f, parms, Qmenu_bar_lines,
3172 NILP (Vmenu_bar_mode)
3173 ? make_number (0) : make_number (1),
3174 NULL, NULL, RES_TYPE_NUMBER);
3175 x_default_parameter (f, parms, Qtool_bar_lines,
3176 NILP (Vtool_bar_mode)
3177 ? make_number (0) : make_number (1),
3178 NULL, NULL, RES_TYPE_NUMBER);
3180 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3181 "bufferPredicate", "BufferPredicate",
3182 RES_TYPE_SYMBOL);
3183 x_default_parameter (f, parms, Qtitle, Qnil,
3184 "title", "Title", RES_TYPE_STRING);
3185 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3186 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3187 x_default_parameter (f, parms, Qfullscreen, Qnil,
3188 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3189 x_default_parameter (f, parms, Qtool_bar_position,
3190 FRAME_TOOL_BAR_POSITION (f), 0, 0, RES_TYPE_SYMBOL);
3192 /* Compute the size of the X window. */
3193 window_prompting = x_figure_window_size (f, parms, 1);
3195 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3196 f->no_split = minibuffer_only || EQ (tem, Qt);
3198 x_icon_verify (f, parms);
3200 /* Create the X widget or window. */
3201 #ifdef USE_X_TOOLKIT
3202 x_window (f, window_prompting, minibuffer_only);
3203 #else
3204 x_window (f);
3205 #endif
3207 x_icon (f, parms);
3208 x_make_gc (f);
3210 /* Now consider the frame official. */
3211 f->terminal->reference_count++;
3212 FRAME_DISPLAY_INFO (f)->reference_count++;
3213 Vframe_list = Fcons (frame, Vframe_list);
3215 /* We need to do this after creating the X window, so that the
3216 icon-creation functions can say whose icon they're describing. */
3217 x_default_parameter (f, parms, Qicon_type, Qt,
3218 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3220 x_default_parameter (f, parms, Qauto_raise, Qnil,
3221 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3222 x_default_parameter (f, parms, Qauto_lower, Qnil,
3223 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3224 x_default_parameter (f, parms, Qcursor_type, Qbox,
3225 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3226 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3227 "scrollBarWidth", "ScrollBarWidth",
3228 RES_TYPE_NUMBER);
3229 x_default_parameter (f, parms, Qscroll_bar_height, Qnil,
3230 "scrollBarHeight", "ScrollBarHeight",
3231 RES_TYPE_NUMBER);
3232 x_default_parameter (f, parms, Qalpha, Qnil,
3233 "alpha", "Alpha", RES_TYPE_NUMBER);
3235 /* Consider frame official, now. */
3236 f->official = true;
3238 adjust_frame_size (f, FRAME_TEXT_WIDTH (f), FRAME_TEXT_HEIGHT (f), 0, 1);
3240 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3241 /* Create the menu bar. */
3242 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3244 /* If this signals an error, we haven't set size hints for the
3245 frame and we didn't make it visible. */
3246 initialize_frame_menubar (f);
3248 #ifndef USE_GTK
3249 /* This is a no-op, except under Motif where it arranges the
3250 main window for the widgets on it. */
3251 lw_set_main_areas (f->output_data.x->column_widget,
3252 f->output_data.x->menubar_widget,
3253 f->output_data.x->edit_widget);
3254 #endif /* not USE_GTK */
3256 #endif /* USE_X_TOOLKIT || USE_GTK */
3258 /* Tell the server what size and position, etc, we want, and how
3259 badly we want them. This should be done after we have the menu
3260 bar so that its size can be taken into account. */
3261 block_input ();
3262 x_wm_set_size_hint (f, window_prompting, 0);
3263 unblock_input ();
3265 /* Make the window appear on the frame and enable display, unless
3266 the caller says not to. However, with explicit parent, Emacs
3267 cannot control visibility, so don't try. */
3268 if (! f->output_data.x->explicit_parent)
3270 Lisp_Object visibility;
3272 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3273 RES_TYPE_SYMBOL);
3274 if (EQ (visibility, Qunbound))
3275 visibility = Qt;
3277 if (EQ (visibility, Qicon))
3278 x_iconify_frame (f);
3279 else if (! NILP (visibility))
3280 x_make_frame_visible (f);
3281 else
3283 /* Must have been Qnil. */
3287 block_input ();
3289 /* Set machine name and pid for the purpose of window managers. */
3290 set_machine_and_pid_properties (f);
3292 /* Set the WM leader property. GTK does this itself, so this is not
3293 needed when using GTK. */
3294 if (dpyinfo->client_leader_window != 0)
3296 XChangeProperty (FRAME_X_DISPLAY (f),
3297 FRAME_OUTER_WINDOW (f),
3298 dpyinfo->Xatom_wm_client_leader,
3299 XA_WINDOW, 32, PropModeReplace,
3300 (unsigned char *) &dpyinfo->client_leader_window, 1);
3303 unblock_input ();
3305 /* Initialize `default-minibuffer-frame' in case this is the first
3306 frame on this terminal. */
3307 if (FRAME_HAS_MINIBUF_P (f)
3308 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3309 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3310 kset_default_minibuffer_frame (kb, frame);
3312 /* All remaining specified parameters, which have not been "used"
3313 by x_get_arg and friends, now go in the misc. alist of the frame. */
3314 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3315 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3316 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3318 UNGCPRO;
3320 /* Make sure windows on this frame appear in calls to next-window
3321 and similar functions. */
3322 Vwindow_list = Qnil;
3324 return unbind_to (count, frame);
3328 /* FRAME is used only to get a handle on the X display. We don't pass the
3329 display info directly because we're called from frame.c, which doesn't
3330 know about that structure. */
3332 Lisp_Object
3333 x_get_focus_frame (struct frame *frame)
3335 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
3336 Lisp_Object xfocus;
3337 if (! dpyinfo->x_focus_frame)
3338 return Qnil;
3340 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3341 return xfocus;
3345 /* In certain situations, when the window manager follows a
3346 click-to-focus policy, there seems to be no way around calling
3347 XSetInputFocus to give another frame the input focus .
3349 In an ideal world, XSetInputFocus should generally be avoided so
3350 that applications don't interfere with the window manager's focus
3351 policy. But I think it's okay to use when it's clearly done
3352 following a user-command. */
3354 void
3355 x_focus_frame (struct frame *f)
3357 Display *dpy = FRAME_X_DISPLAY (f);
3359 block_input ();
3360 x_catch_errors (dpy);
3362 if (FRAME_X_EMBEDDED_P (f))
3364 /* For Xembedded frames, normally the embedder forwards key
3365 events. See XEmbed Protocol Specification at
3366 http://freedesktop.org/wiki/Specifications/xembed-spec */
3367 xembed_request_focus (f);
3369 else
3371 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3372 RevertToParent, CurrentTime);
3373 x_ewmh_activate_frame (f);
3376 x_uncatch_errors ();
3377 unblock_input ();
3381 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3382 doc: /* Internal function called by `color-defined-p', which see
3383 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3384 (Lisp_Object color, Lisp_Object frame)
3386 XColor foo;
3387 struct frame *f = decode_window_system_frame (frame);
3389 CHECK_STRING (color);
3391 if (x_defined_color (f, SSDATA (color), &foo, 0))
3392 return Qt;
3393 else
3394 return Qnil;
3397 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3398 doc: /* Internal function called by `color-values', which see. */)
3399 (Lisp_Object color, Lisp_Object frame)
3401 XColor foo;
3402 struct frame *f = decode_window_system_frame (frame);
3404 CHECK_STRING (color);
3406 if (x_defined_color (f, SSDATA (color), &foo, 0))
3407 return list3i (foo.red, foo.green, foo.blue);
3408 else
3409 return Qnil;
3412 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3413 doc: /* Internal function called by `display-color-p', which see. */)
3414 (Lisp_Object terminal)
3416 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3418 if (dpyinfo->n_planes <= 2)
3419 return Qnil;
3421 switch (dpyinfo->visual->class)
3423 case StaticColor:
3424 case PseudoColor:
3425 case TrueColor:
3426 case DirectColor:
3427 return Qt;
3429 default:
3430 return Qnil;
3434 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3435 0, 1, 0,
3436 doc: /* Return t if the X display supports shades of gray.
3437 Note that color displays do support shades of gray.
3438 The optional argument TERMINAL specifies which display to ask about.
3439 TERMINAL should be a terminal object, a frame or a display name (a string).
3440 If omitted or nil, that stands for the selected frame's display. */)
3441 (Lisp_Object terminal)
3443 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3445 if (dpyinfo->n_planes <= 1)
3446 return Qnil;
3448 switch (dpyinfo->visual->class)
3450 case StaticColor:
3451 case PseudoColor:
3452 case TrueColor:
3453 case DirectColor:
3454 case StaticGray:
3455 case GrayScale:
3456 return Qt;
3458 default:
3459 return Qnil;
3463 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3464 0, 1, 0,
3465 doc: /* Return the width in pixels of the X display TERMINAL.
3466 The optional argument TERMINAL specifies which display to ask about.
3467 TERMINAL should be a terminal object, a frame or a display name (a string).
3468 If omitted or nil, that stands for the selected frame's display.
3470 On \"multi-monitor\" setups this refers to the pixel width for all
3471 physical monitors associated with TERMINAL. To get information for
3472 each physical monitor, use `display-monitor-attributes-list'. */)
3473 (Lisp_Object terminal)
3475 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3477 return make_number (x_display_pixel_width (dpyinfo));
3480 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3481 Sx_display_pixel_height, 0, 1, 0,
3482 doc: /* Return the height in pixels of the X display TERMINAL.
3483 The optional argument TERMINAL specifies which display to ask about.
3484 TERMINAL should be a terminal object, a frame or a display name (a string).
3485 If omitted or nil, that stands for the selected frame's display.
3487 On \"multi-monitor\" setups this refers to the pixel height for all
3488 physical monitors associated with TERMINAL. To get information for
3489 each physical monitor, use `display-monitor-attributes-list'. */)
3490 (Lisp_Object terminal)
3492 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3494 return make_number (x_display_pixel_height (dpyinfo));
3497 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3498 0, 1, 0,
3499 doc: /* Return the number of bitplanes of the X display TERMINAL.
3500 The optional argument TERMINAL specifies which display to ask about.
3501 TERMINAL should be a terminal object, a frame or a display name (a string).
3502 If omitted or nil, that stands for the selected frame's display. */)
3503 (Lisp_Object terminal)
3505 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3507 return make_number (dpyinfo->n_planes);
3510 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3511 0, 1, 0,
3512 doc: /* Return the number of color cells of the X display TERMINAL.
3513 The optional argument TERMINAL specifies which display to ask about.
3514 TERMINAL should be a terminal object, a frame or a display name (a string).
3515 If omitted or nil, that stands for the selected frame's display. */)
3516 (Lisp_Object terminal)
3518 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3520 int nr_planes = DisplayPlanes (dpyinfo->display,
3521 XScreenNumberOfScreen (dpyinfo->screen));
3523 /* Truncate nr_planes to 24 to avoid integer overflow.
3524 Some displays says 32, but only 24 bits are actually significant.
3525 There are only very few and rare video cards that have more than
3526 24 significant bits. Also 24 bits is more than 16 million colors,
3527 it "should be enough for everyone". */
3528 if (nr_planes > 24) nr_planes = 24;
3530 return make_number (1 << nr_planes);
3533 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3534 Sx_server_max_request_size,
3535 0, 1, 0,
3536 doc: /* Return the maximum request size of the X server of display TERMINAL.
3537 The optional argument TERMINAL specifies which display to ask about.
3538 TERMINAL should be a terminal object, a frame or a display name (a string).
3539 If omitted or nil, that stands for the selected frame's display. */)
3540 (Lisp_Object terminal)
3542 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3544 return make_number (MAXREQUEST (dpyinfo->display));
3547 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3548 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3549 \(Labeling every distributor as a "vendor" embodies the false assumption
3550 that operating systems cannot be developed and distributed noncommercially.)
3551 The optional argument TERMINAL specifies which display to ask about.
3552 TERMINAL should be a terminal object, a frame or a display name (a string).
3553 If omitted or nil, that stands for the selected frame's display. */)
3554 (Lisp_Object terminal)
3556 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3557 const char *vendor = ServerVendor (dpyinfo->display);
3559 if (! vendor) vendor = "";
3560 return build_string (vendor);
3563 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3564 doc: /* Return the version numbers of the X server of display TERMINAL.
3565 The value is a list of three integers: the major and minor
3566 version numbers of the X Protocol in use, and the distributor-specific release
3567 number. See also the function `x-server-vendor'.
3569 The optional argument TERMINAL specifies which display to ask about.
3570 TERMINAL should be a terminal object, a frame or a display name (a string).
3571 If omitted or nil, that stands for the selected frame's display. */)
3572 (Lisp_Object terminal)
3574 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3575 Display *dpy = dpyinfo->display;
3577 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3578 VendorRelease (dpy));
3581 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3582 doc: /* Return the number of screens on the X server of display TERMINAL.
3583 The optional argument TERMINAL specifies which display to ask about.
3584 TERMINAL should be a terminal object, a frame or a display name (a string).
3585 If omitted or nil, that stands for the selected frame's display. */)
3586 (Lisp_Object terminal)
3588 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3590 return make_number (ScreenCount (dpyinfo->display));
3593 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3594 doc: /* Return the height in millimeters of the X display TERMINAL.
3595 The optional argument TERMINAL specifies which display to ask about.
3596 TERMINAL should be a terminal object, a frame or a display name (a string).
3597 If omitted or nil, that stands for the selected frame's display.
3599 On \"multi-monitor\" setups this refers to the height in millimeters for
3600 all physical monitors associated with TERMINAL. To get information
3601 for each physical monitor, use `display-monitor-attributes-list'. */)
3602 (Lisp_Object terminal)
3604 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3606 return make_number (HeightMMOfScreen (dpyinfo->screen));
3609 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3610 doc: /* Return the width in millimeters of the X display TERMINAL.
3611 The optional argument TERMINAL specifies which display to ask about.
3612 TERMINAL should be a terminal object, a frame or a display name (a string).
3613 If omitted or nil, that stands for the selected frame's display.
3615 On \"multi-monitor\" setups this refers to the width in millimeters for
3616 all physical monitors associated with TERMINAL. To get information
3617 for each physical monitor, use `display-monitor-attributes-list'. */)
3618 (Lisp_Object terminal)
3620 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3622 return make_number (WidthMMOfScreen (dpyinfo->screen));
3625 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3626 Sx_display_backing_store, 0, 1, 0,
3627 doc: /* Return an indication of whether X display TERMINAL does backing store.
3628 The value may be `always', `when-mapped', or `not-useful'.
3629 The optional argument TERMINAL specifies which display to ask about.
3630 TERMINAL should be a terminal object, a frame or a display name (a string).
3631 If omitted or nil, that stands for the selected frame's display. */)
3632 (Lisp_Object terminal)
3634 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3635 Lisp_Object result;
3637 switch (DoesBackingStore (dpyinfo->screen))
3639 case Always:
3640 result = intern ("always");
3641 break;
3643 case WhenMapped:
3644 result = intern ("when-mapped");
3645 break;
3647 case NotUseful:
3648 result = intern ("not-useful");
3649 break;
3651 default:
3652 error ("Strange value for BackingStore parameter of screen");
3655 return result;
3658 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3659 Sx_display_visual_class, 0, 1, 0,
3660 doc: /* Return the visual class of the X display TERMINAL.
3661 The value is one of the symbols `static-gray', `gray-scale',
3662 `static-color', `pseudo-color', `true-color', or `direct-color'.
3664 The optional argument TERMINAL specifies which display to ask about.
3665 TERMINAL should a terminal object, a frame or a display name (a string).
3666 If omitted or nil, that stands for the selected frame's display. */)
3667 (Lisp_Object terminal)
3669 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3670 Lisp_Object result;
3672 switch (dpyinfo->visual->class)
3674 case StaticGray:
3675 result = intern ("static-gray");
3676 break;
3677 case GrayScale:
3678 result = intern ("gray-scale");
3679 break;
3680 case StaticColor:
3681 result = intern ("static-color");
3682 break;
3683 case PseudoColor:
3684 result = intern ("pseudo-color");
3685 break;
3686 case TrueColor:
3687 result = intern ("true-color");
3688 break;
3689 case DirectColor:
3690 result = intern ("direct-color");
3691 break;
3692 default:
3693 error ("Display has an unknown visual class");
3696 return result;
3699 DEFUN ("x-display-save-under", Fx_display_save_under,
3700 Sx_display_save_under, 0, 1, 0,
3701 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3702 The optional argument TERMINAL specifies which display to ask about.
3703 TERMINAL should be a terminal object, a frame or a display name (a string).
3704 If omitted or nil, that stands for the selected frame's display. */)
3705 (Lisp_Object terminal)
3707 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3709 if (DoesSaveUnders (dpyinfo->screen) == True)
3710 return Qt;
3711 else
3712 return Qnil;
3715 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3716 Return false if and only if the workarea information cannot be
3717 obtained via the _NET_WORKAREA root window property. */
3719 #if ! GTK_CHECK_VERSION (3, 4, 0)
3720 static bool
3721 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3723 Display *dpy = dpyinfo->display;
3724 long offset, max_len;
3725 Atom target_type, actual_type;
3726 unsigned long actual_size, bytes_remaining;
3727 int rc, actual_format;
3728 unsigned char *tmp_data = NULL;
3729 bool result = false;
3731 x_catch_errors (dpy);
3732 offset = 0;
3733 max_len = 1;
3734 target_type = XA_CARDINAL;
3735 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3736 dpyinfo->Xatom_net_current_desktop,
3737 offset, max_len, False, target_type,
3738 &actual_type, &actual_format, &actual_size,
3739 &bytes_remaining, &tmp_data);
3740 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3741 && actual_format == 32 && actual_size == max_len)
3743 long current_desktop = ((long *) tmp_data)[0];
3745 XFree (tmp_data);
3746 tmp_data = NULL;
3748 offset = 4 * current_desktop;
3749 max_len = 4;
3750 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3751 dpyinfo->Xatom_net_workarea,
3752 offset, max_len, False, target_type,
3753 &actual_type, &actual_format, &actual_size,
3754 &bytes_remaining, &tmp_data);
3755 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3756 && actual_format == 32 && actual_size == max_len)
3758 long *values = (long *) tmp_data;
3760 rect->x = values[0];
3761 rect->y = values[1];
3762 rect->width = values[2];
3763 rect->height = values[3];
3765 XFree (tmp_data);
3766 tmp_data = NULL;
3768 result = true;
3771 if (tmp_data)
3772 XFree (tmp_data);
3773 x_uncatch_errors ();
3775 return result;
3777 #endif
3779 #ifndef USE_GTK
3781 /* Return monitor number where F is "most" or closest to. */
3782 static int
3783 x_get_monitor_for_frame (struct frame *f,
3784 struct MonitorInfo *monitors,
3785 int n_monitors)
3787 XRectangle frect;
3788 int area = 0, dist = -1;
3789 int best_area = -1, best_dist = -1;
3790 int i;
3792 if (n_monitors == 1) return 0;
3793 frect.x = f->left_pos;
3794 frect.y = f->top_pos;
3795 frect.width = FRAME_PIXEL_WIDTH (f);
3796 frect.height = FRAME_PIXEL_HEIGHT (f);
3798 for (i = 0; i < n_monitors; ++i)
3800 struct MonitorInfo *mi = &monitors[i];
3801 XRectangle res;
3802 int a = 0;
3804 if (mi->geom.width == 0) continue;
3806 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3808 a = res.width * res.height;
3809 if (a > area)
3811 area = a;
3812 best_area = i;
3816 if (a == 0 && area == 0)
3818 int dx, dy, d;
3819 if (frect.x + frect.width < mi->geom.x)
3820 dx = mi->geom.x - frect.x + frect.width;
3821 else if (frect.x > mi->geom.x + mi->geom.width)
3822 dx = frect.x - mi->geom.x + mi->geom.width;
3823 else
3824 dx = 0;
3825 if (frect.y + frect.height < mi->geom.y)
3826 dy = mi->geom.y - frect.y + frect.height;
3827 else if (frect.y > mi->geom.y + mi->geom.height)
3828 dy = frect.y - mi->geom.y + mi->geom.height;
3829 else
3830 dy = 0;
3832 d = dx*dx + dy*dy;
3833 if (dist == -1 || dist > d)
3835 dist = d;
3836 best_dist = i;
3841 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3844 static Lisp_Object
3845 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3846 int n_monitors,
3847 int primary_monitor,
3848 struct x_display_info *dpyinfo,
3849 const char *source)
3851 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3852 Lisp_Object frame, rest;
3854 FOR_EACH_FRAME (rest, frame)
3856 struct frame *f = XFRAME (frame);
3858 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3859 && !EQ (frame, tip_frame))
3861 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3862 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3866 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3867 monitor_frames, source);
3870 static Lisp_Object
3871 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3873 struct MonitorInfo monitor;
3874 XRectangle workarea_r;
3876 /* Fallback: treat (possibly) multiple physical monitors as if they
3877 formed a single monitor as a whole. This should provide a
3878 consistent result at least on single monitor environments. */
3879 monitor.geom.x = monitor.geom.y = 0;
3880 monitor.geom.width = x_display_pixel_width (dpyinfo);
3881 monitor.geom.height = x_display_pixel_height (dpyinfo);
3882 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3883 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3884 monitor.name = xstrdup ("combined screen");
3886 if (x_get_net_workarea (dpyinfo, &workarea_r))
3887 monitor.work = workarea_r;
3888 else
3889 monitor.work = monitor.geom;
3890 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3894 #ifdef HAVE_XINERAMA
3895 static Lisp_Object
3896 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3898 int n_monitors, i;
3899 Lisp_Object attributes_list = Qnil;
3900 Display *dpy = dpyinfo->display;
3901 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3902 struct MonitorInfo *monitors;
3903 double mm_width_per_pixel, mm_height_per_pixel;
3905 if (! info || n_monitors == 0)
3907 if (info)
3908 XFree (info);
3909 return attributes_list;
3912 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3913 / x_display_pixel_width (dpyinfo));
3914 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3915 / x_display_pixel_height (dpyinfo));
3916 monitors = xzalloc (n_monitors * sizeof *monitors);
3917 for (i = 0; i < n_monitors; ++i)
3919 struct MonitorInfo *mi = &monitors[i];
3920 XRectangle workarea_r;
3922 mi->geom.x = info[i].x_org;
3923 mi->geom.y = info[i].y_org;
3924 mi->geom.width = info[i].width;
3925 mi->geom.height = info[i].height;
3926 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3927 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3928 mi->name = 0;
3930 /* Xinerama usually have primary monitor first, just use that. */
3931 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3933 mi->work = workarea_r;
3934 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3935 mi->work = mi->geom;
3937 else
3938 mi->work = mi->geom;
3940 XFree (info);
3942 attributes_list = x_make_monitor_attribute_list (monitors,
3943 n_monitors,
3945 dpyinfo,
3946 "Xinerama");
3947 free_monitors (monitors, n_monitors);
3948 return attributes_list;
3950 #endif /* HAVE_XINERAMA */
3953 #ifdef HAVE_XRANDR
3954 static Lisp_Object
3955 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3957 Lisp_Object attributes_list = Qnil;
3958 XRRScreenResources *resources;
3959 Display *dpy = dpyinfo->display;
3960 int i, n_monitors, primary = -1;
3961 RROutput pxid = None;
3962 struct MonitorInfo *monitors;
3964 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3965 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3966 #else
3967 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3968 #endif
3969 if (! resources || resources->noutput == 0)
3971 if (resources)
3972 XRRFreeScreenResources (resources);
3973 return Qnil;
3975 n_monitors = resources->noutput;
3976 monitors = xzalloc (n_monitors * sizeof *monitors);
3978 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3979 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3980 #endif
3982 for (i = 0; i < n_monitors; ++i)
3984 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3985 resources->outputs[i]);
3986 Connection conn = info ? info->connection : RR_Disconnected;
3987 RRCrtc id = info ? info->crtc : None;
3989 if (strcmp (info->name, "default") == 0)
3991 /* Non XRandr 1.2 driver, does not give useful data. */
3992 XRRFreeOutputInfo (info);
3993 XRRFreeScreenResources (resources);
3994 free_monitors (monitors, n_monitors);
3995 return Qnil;
3998 if (conn != RR_Disconnected && id != None)
4000 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
4001 struct MonitorInfo *mi = &monitors[i];
4002 XRectangle workarea_r;
4004 if (! crtc)
4006 XRRFreeOutputInfo (info);
4007 continue;
4010 mi->geom.x = crtc->x;
4011 mi->geom.y = crtc->y;
4012 mi->geom.width = crtc->width;
4013 mi->geom.height = crtc->height;
4014 mi->mm_width = info->mm_width;
4015 mi->mm_height = info->mm_height;
4016 mi->name = xstrdup (info->name);
4018 if (pxid != None && pxid == resources->outputs[i])
4019 primary = i;
4020 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
4021 primary = i;
4023 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
4025 mi->work= workarea_r;
4026 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
4027 mi->work = mi->geom;
4029 else
4030 mi->work = mi->geom;
4032 XRRFreeCrtcInfo (crtc);
4034 XRRFreeOutputInfo (info);
4036 XRRFreeScreenResources (resources);
4038 attributes_list = x_make_monitor_attribute_list (monitors,
4039 n_monitors,
4040 primary,
4041 dpyinfo,
4042 "XRandr");
4043 free_monitors (monitors, n_monitors);
4044 return attributes_list;
4046 #endif /* HAVE_XRANDR */
4048 static Lisp_Object
4049 x_get_monitor_attributes (struct x_display_info *dpyinfo)
4051 Lisp_Object attributes_list = Qnil;
4052 Display *dpy = dpyinfo->display;
4054 (void) dpy; /* Suppress unused variable warning. */
4056 #ifdef HAVE_XRANDR
4057 int xrr_event_base, xrr_error_base;
4058 bool xrr_ok = false;
4059 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
4060 if (xrr_ok)
4062 int xrr_major, xrr_minor;
4063 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
4064 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
4067 if (xrr_ok)
4068 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
4069 #endif /* HAVE_XRANDR */
4071 #ifdef HAVE_XINERAMA
4072 if (NILP (attributes_list))
4074 int xin_event_base, xin_error_base;
4075 bool xin_ok = false;
4076 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
4077 if (xin_ok && XineramaIsActive (dpy))
4078 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
4080 #endif /* HAVE_XINERAMA */
4082 if (NILP (attributes_list))
4083 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
4085 return attributes_list;
4088 #endif /* !USE_GTK */
4090 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
4091 Sx_display_monitor_attributes_list,
4092 0, 1, 0,
4093 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
4095 The optional argument TERMINAL specifies which display to ask about.
4096 TERMINAL should be a terminal object, a frame or a display name (a string).
4097 If omitted or nil, that stands for the selected frame's display.
4099 In addition to the standard attribute keys listed in
4100 `display-monitor-attributes-list', the following keys are contained in
4101 the attributes:
4103 source -- String describing the source from which multi-monitor
4104 information is obtained, one of \"Gdk\", \"XRandr\",
4105 \"Xinerama\", or \"fallback\"
4107 Internal use only, use `display-monitor-attributes-list' instead. */)
4108 (Lisp_Object terminal)
4110 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4111 Lisp_Object attributes_list = Qnil;
4113 #ifdef USE_GTK
4114 double mm_width_per_pixel, mm_height_per_pixel;
4115 GdkDisplay *gdpy;
4116 GdkScreen *gscreen;
4117 gint primary_monitor = 0, n_monitors, i;
4118 Lisp_Object monitor_frames, rest, frame;
4119 static const char *source = "Gdk";
4120 struct MonitorInfo *monitors;
4122 block_input ();
4123 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4124 / x_display_pixel_width (dpyinfo));
4125 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4126 / x_display_pixel_height (dpyinfo));
4127 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4128 gscreen = gdk_display_get_default_screen (gdpy);
4129 #if GTK_CHECK_VERSION (2, 20, 0)
4130 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4131 #endif
4132 n_monitors = gdk_screen_get_n_monitors (gscreen);
4133 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4134 monitors = xzalloc (n_monitors * sizeof *monitors);
4136 FOR_EACH_FRAME (rest, frame)
4138 struct frame *f = XFRAME (frame);
4140 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4141 && !EQ (frame, tip_frame))
4143 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4145 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4146 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4150 for (i = 0; i < n_monitors; ++i)
4152 gint width_mm = -1, height_mm = -1;
4153 GdkRectangle rec, work;
4154 struct MonitorInfo *mi = &monitors[i];
4156 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4158 #if GTK_CHECK_VERSION (2, 14, 0)
4159 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4160 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4161 #endif
4162 if (width_mm < 0)
4163 width_mm = rec.width * mm_width_per_pixel + 0.5;
4164 if (height_mm < 0)
4165 height_mm = rec.height * mm_height_per_pixel + 0.5;
4167 #if GTK_CHECK_VERSION (3, 4, 0)
4168 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4169 #else
4170 /* Emulate the behavior of GTK+ 3.4. */
4172 XRectangle workarea_r;
4174 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4176 work.x = workarea_r.x;
4177 work.y = workarea_r.y;
4178 work.width = workarea_r.width;
4179 work.height = workarea_r.height;
4180 if (! gdk_rectangle_intersect (&rec, &work, &work))
4181 work = rec;
4183 else
4184 work = rec;
4186 #endif
4189 mi->geom.x = rec.x;
4190 mi->geom.y = rec.y;
4191 mi->geom.width = rec.width;
4192 mi->geom.height = rec.height;
4193 mi->work.x = work.x;
4194 mi->work.y = work.y;
4195 mi->work.width = work.width;
4196 mi->work.height = work.height;
4197 mi->mm_width = width_mm;
4198 mi->mm_height = height_mm;
4200 #if GTK_CHECK_VERSION (2, 14, 0)
4201 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4202 #endif
4205 attributes_list = make_monitor_attribute_list (monitors,
4206 n_monitors,
4207 primary_monitor,
4208 monitor_frames,
4209 source);
4210 unblock_input ();
4211 #else /* not USE_GTK */
4213 block_input ();
4214 attributes_list = x_get_monitor_attributes (dpyinfo);
4215 unblock_input ();
4217 #endif /* not USE_GTK */
4219 return attributes_list;
4222 /************************************************************************
4223 X Displays
4224 ************************************************************************/
4227 /* Mapping visual names to visuals. */
4229 static struct visual_class
4231 const char *name;
4232 int class;
4234 visual_classes[] =
4236 {"StaticGray", StaticGray},
4237 {"GrayScale", GrayScale},
4238 {"StaticColor", StaticColor},
4239 {"PseudoColor", PseudoColor},
4240 {"TrueColor", TrueColor},
4241 {"DirectColor", DirectColor},
4242 {NULL, 0}
4246 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4248 /* Value is the screen number of screen SCR. This is a substitute for
4249 the X function with the same name when that doesn't exist. */
4252 XScreenNumberOfScreen (scr)
4253 register Screen *scr;
4255 Display *dpy = scr->display;
4256 int i;
4258 for (i = 0; i < dpy->nscreens; ++i)
4259 if (scr == dpy->screens + i)
4260 break;
4262 return i;
4265 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4268 /* Select the visual that should be used on display DPYINFO. Set
4269 members of DPYINFO appropriately. Called from x_term_init. */
4271 void
4272 select_visual (struct x_display_info *dpyinfo)
4274 Display *dpy = dpyinfo->display;
4275 Screen *screen = dpyinfo->screen;
4276 Lisp_Object value;
4278 /* See if a visual is specified. */
4279 value = display_x_get_resource (dpyinfo,
4280 build_string ("visualClass"),
4281 build_string ("VisualClass"),
4282 Qnil, Qnil);
4283 if (STRINGP (value))
4285 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4286 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4287 depth, a decimal number. NAME is compared with case ignored. */
4288 char *s = alloca (SBYTES (value) + 1);
4289 char *dash;
4290 int i, class = -1;
4291 XVisualInfo vinfo;
4293 strcpy (s, SSDATA (value));
4294 dash = strchr (s, '-');
4295 if (dash)
4297 dpyinfo->n_planes = atoi (dash + 1);
4298 *dash = '\0';
4300 else
4301 /* We won't find a matching visual with depth 0, so that
4302 an error will be printed below. */
4303 dpyinfo->n_planes = 0;
4305 /* Determine the visual class. */
4306 for (i = 0; visual_classes[i].name; ++i)
4307 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4309 class = visual_classes[i].class;
4310 break;
4313 /* Look up a matching visual for the specified class. */
4314 if (class == -1
4315 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4316 dpyinfo->n_planes, class, &vinfo))
4317 fatal ("Invalid visual specification `%s'", SDATA (value));
4319 dpyinfo->visual = vinfo.visual;
4321 else
4323 int n_visuals;
4324 XVisualInfo *vinfo, vinfo_template;
4326 dpyinfo->visual = DefaultVisualOfScreen (screen);
4328 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4329 vinfo_template.screen = XScreenNumberOfScreen (screen);
4330 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4331 &vinfo_template, &n_visuals);
4332 if (n_visuals <= 0)
4333 fatal ("Can't get proper X visual info");
4335 dpyinfo->n_planes = vinfo->depth;
4336 XFree (vinfo);
4341 /* Return the X display structure for the display named NAME.
4342 Open a new connection if necessary. */
4344 static struct x_display_info *
4345 x_display_info_for_name (Lisp_Object name)
4347 struct x_display_info *dpyinfo;
4349 CHECK_STRING (name);
4351 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
4352 if (!NILP (Fstring_equal (XCAR (dpyinfo->name_list_element), name)))
4353 return dpyinfo;
4355 /* Use this general default value to start with. */
4356 Vx_resource_name = Vinvocation_name;
4358 validate_x_resource_name ();
4360 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4362 if (dpyinfo == 0)
4363 error ("Cannot connect to X server %s", SDATA (name));
4365 XSETFASTINT (Vwindow_system_version, 11);
4367 return dpyinfo;
4371 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4372 1, 3, 0,
4373 doc: /* Open a connection to a display server.
4374 DISPLAY is the name of the display to connect to.
4375 Optional second arg XRM-STRING is a string of resources in xrdb format.
4376 If the optional third arg MUST-SUCCEED is non-nil,
4377 terminate Emacs if we can't open the connection.
4378 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4379 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4381 char *xrm_option;
4382 struct x_display_info *dpyinfo;
4384 CHECK_STRING (display);
4385 if (! NILP (xrm_string))
4386 CHECK_STRING (xrm_string);
4388 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4390 validate_x_resource_name ();
4392 /* This is what opens the connection and sets x_current_display.
4393 This also initializes many symbols, such as those used for input. */
4394 dpyinfo = x_term_init (display, xrm_option,
4395 SSDATA (Vx_resource_name));
4397 if (dpyinfo == 0)
4399 if (!NILP (must_succeed))
4400 fatal ("Cannot connect to X server %s.\n\
4401 Check the DISPLAY environment variable or use `-d'.\n\
4402 Also use the `xauth' program to verify that you have the proper\n\
4403 authorization information needed to connect the X server.\n\
4404 An insecure way to solve the problem may be to use `xhost'.\n",
4405 SDATA (display));
4406 else
4407 error ("Cannot connect to X server %s", SDATA (display));
4410 XSETFASTINT (Vwindow_system_version, 11);
4411 return Qnil;
4414 DEFUN ("x-close-connection", Fx_close_connection,
4415 Sx_close_connection, 1, 1, 0,
4416 doc: /* Close the connection to TERMINAL's X server.
4417 For TERMINAL, specify a terminal object, a frame or a display name (a
4418 string). If TERMINAL is nil, that stands for the selected frame's
4419 terminal. */)
4420 (Lisp_Object terminal)
4422 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4424 if (dpyinfo->reference_count > 0)
4425 error ("Display still has frames on it");
4427 x_delete_terminal (dpyinfo->terminal);
4429 return Qnil;
4432 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4433 doc: /* Return the list of display names that Emacs has connections to. */)
4434 (void)
4436 Lisp_Object result = Qnil;
4437 struct x_display_info *xdi;
4439 for (xdi = x_display_list; xdi; xdi = xdi->next)
4440 result = Fcons (XCAR (xdi->name_list_element), result);
4442 return result;
4445 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4446 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4447 This function only has an effect on X Windows. With MS Windows, it is
4448 defined but does nothing.
4450 If ON is nil, allow buffering of requests.
4451 Turning on synchronization prohibits the Xlib routines from buffering
4452 requests and seriously degrades performance, but makes debugging much
4453 easier.
4454 The optional second argument TERMINAL specifies which display to act on.
4455 TERMINAL should be a terminal object, a frame or a display name (a string).
4456 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4457 (Lisp_Object on, Lisp_Object terminal)
4459 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4461 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4463 return Qnil;
4466 /* Wait for responses to all X commands issued so far for frame F. */
4468 void
4469 x_sync (struct frame *f)
4471 block_input ();
4472 XSync (FRAME_X_DISPLAY (f), False);
4473 unblock_input ();
4477 /***********************************************************************
4478 Window properties
4479 ***********************************************************************/
4481 DEFUN ("x-change-window-property", Fx_change_window_property,
4482 Sx_change_window_property, 2, 6, 0,
4483 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4484 PROP must be a string. VALUE may be a string or a list of conses,
4485 numbers and/or strings. If an element in the list is a string, it is
4486 converted to an atom and the value of the atom is used. If an element
4487 is a cons, it is converted to a 32 bit number where the car is the 16
4488 top bits and the cdr is the lower 16 bits.
4490 FRAME nil or omitted means use the selected frame.
4491 If TYPE is given and non-nil, it is the name of the type of VALUE.
4492 If TYPE is not given or nil, the type is STRING.
4493 FORMAT gives the size in bits of each element if VALUE is a list.
4494 It must be one of 8, 16 or 32.
4495 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4496 If OUTER-P is non-nil, the property is changed for the outer X window of
4497 FRAME. Default is to change on the edit X window. */)
4498 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4499 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4501 struct frame *f = decode_window_system_frame (frame);
4502 Atom prop_atom;
4503 Atom target_type = XA_STRING;
4504 int element_format = 8;
4505 unsigned char *data;
4506 int nelements;
4507 Window w;
4509 CHECK_STRING (prop);
4511 if (! NILP (format))
4513 CHECK_NUMBER (format);
4515 if (XINT (format) != 8 && XINT (format) != 16
4516 && XINT (format) != 32)
4517 error ("FORMAT must be one of 8, 16 or 32");
4518 element_format = XINT (format);
4521 if (CONSP (value))
4523 ptrdiff_t elsize;
4525 nelements = x_check_property_data (value);
4526 if (nelements == -1)
4527 error ("Bad data in VALUE, must be number, string or cons");
4529 /* The man page for XChangeProperty:
4530 "If the specified format is 32, the property data must be a
4531 long array."
4532 This applies even if long is more than 32 bits. The X library
4533 converts to 32 bits before sending to the X server. */
4534 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4535 data = xnmalloc (nelements, elsize);
4537 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4539 else
4541 CHECK_STRING (value);
4542 data = SDATA (value);
4543 if (INT_MAX < SBYTES (value))
4544 error ("VALUE too long");
4545 nelements = SBYTES (value);
4548 block_input ();
4549 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4550 if (! NILP (type))
4552 CHECK_STRING (type);
4553 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4556 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4557 else w = FRAME_X_WINDOW (f);
4559 XChangeProperty (FRAME_X_DISPLAY (f), w,
4560 prop_atom, target_type, element_format, PropModeReplace,
4561 data, nelements);
4563 if (CONSP (value)) xfree (data);
4565 /* Make sure the property is set when we return. */
4566 XFlush (FRAME_X_DISPLAY (f));
4567 unblock_input ();
4569 return value;
4573 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4574 Sx_delete_window_property, 1, 2, 0,
4575 doc: /* Remove window property PROP from X window of FRAME.
4576 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4577 (Lisp_Object prop, Lisp_Object frame)
4579 struct frame *f = decode_window_system_frame (frame);
4580 Atom prop_atom;
4582 CHECK_STRING (prop);
4583 block_input ();
4584 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4585 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4587 /* Make sure the property is removed when we return. */
4588 XFlush (FRAME_X_DISPLAY (f));
4589 unblock_input ();
4591 return prop;
4595 static Lisp_Object
4596 x_window_property_intern (struct frame *f,
4597 Window target_window,
4598 Atom prop_atom,
4599 Atom target_type,
4600 Lisp_Object delete_p,
4601 Lisp_Object vector_ret_p,
4602 bool *found)
4604 unsigned char *tmp_data = NULL;
4605 Lisp_Object prop_value = Qnil;
4606 Atom actual_type;
4607 int actual_format;
4608 unsigned long actual_size, bytes_remaining;
4609 int rc;
4610 struct gcpro gcpro1;
4612 GCPRO1 (prop_value);
4614 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4615 prop_atom, 0, 0, False, target_type,
4616 &actual_type, &actual_format, &actual_size,
4617 &bytes_remaining, &tmp_data);
4619 *found = actual_format != 0;
4621 if (rc == Success && *found)
4623 XFree (tmp_data);
4624 tmp_data = NULL;
4626 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4627 prop_atom, 0, bytes_remaining,
4628 ! NILP (delete_p), target_type,
4629 &actual_type, &actual_format,
4630 &actual_size, &bytes_remaining,
4631 &tmp_data);
4632 if (rc == Success && tmp_data)
4634 /* The man page for XGetWindowProperty says:
4635 "If the returned format is 32, the returned data is represented
4636 as a long array and should be cast to that type to obtain the
4637 elements."
4638 This applies even if long is more than 32 bits, the X library
4639 converts from 32 bit elements received from the X server to long
4640 and passes the long array to us. Thus, for that case memcpy can not
4641 be used. We convert to a 32 bit type here, because so much code
4642 assume on that.
4644 The bytes and offsets passed to XGetWindowProperty refers to the
4645 property and those are indeed in 32 bit quantities if format is
4646 32. */
4648 if (BITS_PER_LONG > 32 && actual_format == 32)
4650 unsigned long i;
4651 int *idata = (int *) tmp_data;
4652 long *ldata = (long *) tmp_data;
4654 for (i = 0; i < actual_size; ++i)
4655 idata[i] = (int) ldata[i];
4658 if (NILP (vector_ret_p))
4659 prop_value = make_string ((char *) tmp_data, actual_size);
4660 else
4661 prop_value = x_property_data_to_lisp (f,
4662 tmp_data,
4663 actual_type,
4664 actual_format,
4665 actual_size);
4668 if (tmp_data) XFree (tmp_data);
4671 UNGCPRO;
4672 return prop_value;
4675 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4676 1, 6, 0,
4677 doc: /* Value is the value of window property PROP on FRAME.
4678 If FRAME is nil or omitted, use the selected frame.
4680 On X Windows, the following optional arguments are also accepted:
4681 If TYPE is nil or omitted, get the property as a string.
4682 Otherwise TYPE is the name of the atom that denotes the type expected.
4683 If SOURCE is non-nil, get the property on that window instead of from
4684 FRAME. The number 0 denotes the root window.
4685 If DELETE-P is non-nil, delete the property after retrieving it.
4686 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4688 On MS Windows, this function accepts but ignores those optional arguments.
4690 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4691 no value of TYPE (always string in the MS Windows case). */)
4692 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4693 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4695 struct frame *f = decode_window_system_frame (frame);
4696 Atom prop_atom;
4697 Lisp_Object prop_value = Qnil;
4698 Atom target_type = XA_STRING;
4699 Window target_window = FRAME_X_WINDOW (f);
4700 struct gcpro gcpro1;
4701 bool found;
4703 GCPRO1 (prop_value);
4704 CHECK_STRING (prop);
4706 if (! NILP (source))
4708 CONS_TO_INTEGER (source, Window, target_window);
4709 if (! target_window)
4710 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4713 block_input ();
4714 if (STRINGP (type))
4716 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4717 target_type = AnyPropertyType;
4718 else
4719 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4722 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4723 prop_value = x_window_property_intern (f,
4724 target_window,
4725 prop_atom,
4726 target_type,
4727 delete_p,
4728 vector_ret_p,
4729 &found);
4730 if (NILP (prop_value)
4731 && ! found
4732 && NILP (source)
4733 && target_window != FRAME_OUTER_WINDOW (f))
4735 prop_value = x_window_property_intern (f,
4736 FRAME_OUTER_WINDOW (f),
4737 prop_atom,
4738 target_type,
4739 delete_p,
4740 vector_ret_p,
4741 &found);
4745 unblock_input ();
4746 UNGCPRO;
4747 return prop_value;
4750 /***********************************************************************
4751 Tool tips
4752 ***********************************************************************/
4754 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4755 Lisp_Object, Lisp_Object);
4756 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4757 Lisp_Object, int, int, int *, int *);
4759 /* The frame of a currently visible tooltip. */
4761 Lisp_Object tip_frame;
4763 /* If non-nil, a timer started that hides the last tooltip when it
4764 fires. */
4766 static Lisp_Object tip_timer;
4767 Window tip_window;
4769 /* If non-nil, a vector of 3 elements containing the last args
4770 with which x-show-tip was called. See there. */
4772 static Lisp_Object last_show_tip_args;
4775 static void
4776 unwind_create_tip_frame (Lisp_Object frame)
4778 Lisp_Object deleted;
4780 deleted = unwind_create_frame (frame);
4781 if (EQ (deleted, Qt))
4783 tip_window = None;
4784 tip_frame = Qnil;
4789 /* Create a frame for a tooltip on the display described by DPYINFO.
4790 PARMS is a list of frame parameters. TEXT is the string to
4791 display in the tip frame. Value is the frame.
4793 Note that functions called here, esp. x_default_parameter can
4794 signal errors, for instance when a specified color name is
4795 undefined. We have to make sure that we're in a consistent state
4796 when this happens. */
4798 static Lisp_Object
4799 x_create_tip_frame (struct x_display_info *dpyinfo,
4800 Lisp_Object parms,
4801 Lisp_Object text)
4803 struct frame *f;
4804 Lisp_Object frame;
4805 Lisp_Object name;
4806 int width, height;
4807 ptrdiff_t count = SPECPDL_INDEX ();
4808 struct gcpro gcpro1, gcpro2, gcpro3;
4809 int face_change_count_before = face_change_count;
4810 Lisp_Object buffer;
4811 struct buffer *old_buffer;
4813 if (!dpyinfo->terminal->name)
4814 error ("Terminal is not live, can't create new frames on it");
4816 parms = Fcopy_alist (parms);
4818 /* Get the name of the frame to use for resource lookup. */
4819 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4820 if (!STRINGP (name)
4821 && !EQ (name, Qunbound)
4822 && !NILP (name))
4823 error ("Invalid frame name--not a string or nil");
4825 frame = Qnil;
4826 GCPRO3 (parms, name, frame);
4827 f = make_frame (1);
4828 XSETFRAME (frame, f);
4830 buffer = Fget_buffer_create (build_string (" *tip*"));
4831 /* Use set_window_buffer instead of Fset_window_buffer (see
4832 discussion of bug#11984, bug#12025, bug#12026). */
4833 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
4834 old_buffer = current_buffer;
4835 set_buffer_internal_1 (XBUFFER (buffer));
4836 bset_truncate_lines (current_buffer, Qnil);
4837 specbind (Qinhibit_read_only, Qt);
4838 specbind (Qinhibit_modification_hooks, Qt);
4839 Ferase_buffer ();
4840 Finsert (1, &text);
4841 set_buffer_internal_1 (old_buffer);
4843 record_unwind_protect (unwind_create_tip_frame, frame);
4845 f->terminal = dpyinfo->terminal;
4847 /* By setting the output method, we're essentially saying that
4848 the frame is live, as per FRAME_LIVE_P. If we get a signal
4849 from this point on, x_destroy_window might screw up reference
4850 counts etc. */
4851 f->output_method = output_x_window;
4852 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4853 f->output_data.x->icon_bitmap = -1;
4854 FRAME_FONTSET (f) = -1;
4855 f->output_data.x->scroll_bar_foreground_pixel = -1;
4856 f->output_data.x->scroll_bar_background_pixel = -1;
4857 #if defined (USE_LUCID) && defined (USE_TOOLKIT_SCROLL_BARS)
4858 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4859 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4860 #endif /* USE_LUCID && USE_TOOLKIT_SCROLL_BARS */
4861 f->output_data.x->white_relief.pixel = -1;
4862 f->output_data.x->black_relief.pixel = -1;
4864 fset_icon_name (f, Qnil);
4865 FRAME_DISPLAY_INFO (f) = dpyinfo;
4866 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4867 f->output_data.x->explicit_parent = 0;
4869 /* These colors will be set anyway later, but it's important
4870 to get the color reference counts right, so initialize them! */
4872 Lisp_Object black;
4873 struct gcpro gcpro1;
4875 /* Function x_decode_color can signal an error. Make
4876 sure to initialize color slots so that we won't try
4877 to free colors we haven't allocated. */
4878 FRAME_FOREGROUND_PIXEL (f) = -1;
4879 FRAME_BACKGROUND_PIXEL (f) = -1;
4880 f->output_data.x->cursor_pixel = -1;
4881 f->output_data.x->cursor_foreground_pixel = -1;
4882 f->output_data.x->border_pixel = -1;
4883 f->output_data.x->mouse_pixel = -1;
4885 black = build_string ("black");
4886 GCPRO1 (black);
4887 FRAME_FOREGROUND_PIXEL (f)
4888 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4889 FRAME_BACKGROUND_PIXEL (f)
4890 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4891 f->output_data.x->cursor_pixel
4892 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4893 f->output_data.x->cursor_foreground_pixel
4894 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4895 f->output_data.x->border_pixel
4896 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4897 f->output_data.x->mouse_pixel
4898 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4899 UNGCPRO;
4902 /* Set the name; the functions to which we pass f expect the name to
4903 be set. */
4904 if (EQ (name, Qunbound) || NILP (name))
4906 fset_name (f, build_string (dpyinfo->x_id_name));
4907 f->explicit_name = 0;
4909 else
4911 fset_name (f, name);
4912 f->explicit_name = 1;
4913 /* use the frame's title when getting resources for this frame. */
4914 specbind (Qx_resource_name, name);
4917 register_font_driver (&xfont_driver, f);
4918 #ifdef HAVE_FREETYPE
4919 #ifdef HAVE_XFT
4920 register_font_driver (&xftfont_driver, f);
4921 #else /* not HAVE_XFT */
4922 register_font_driver (&ftxfont_driver, f);
4923 #endif /* not HAVE_XFT */
4924 #endif /* HAVE_FREETYPE */
4926 x_default_parameter (f, parms, Qfont_backend, Qnil,
4927 "fontBackend", "FontBackend", RES_TYPE_STRING);
4929 /* Extract the window parameters from the supplied values that are
4930 needed to determine window geometry. */
4931 x_default_font_parameter (f, parms);
4933 x_default_parameter (f, parms, Qborder_width, make_number (0),
4934 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4936 /* This defaults to 2 in order to match xterm. We recognize either
4937 internalBorderWidth or internalBorder (which is what xterm calls
4938 it). */
4939 if (NILP (Fassq (Qinternal_border_width, parms)))
4941 Lisp_Object value;
4943 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4944 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4945 if (! EQ (value, Qunbound))
4946 parms = Fcons (Fcons (Qinternal_border_width, value),
4947 parms);
4950 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4951 "internalBorderWidth", "internalBorderWidth",
4952 RES_TYPE_NUMBER);
4953 x_default_parameter (f, parms, Qright_divider_width, make_number (0),
4954 NULL, NULL, RES_TYPE_NUMBER);
4955 x_default_parameter (f, parms, Qbottom_divider_width, make_number (0),
4956 NULL, NULL, RES_TYPE_NUMBER);
4958 /* Also do the stuff which must be set before the window exists. */
4959 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4960 "foreground", "Foreground", RES_TYPE_STRING);
4961 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4962 "background", "Background", RES_TYPE_STRING);
4963 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4964 "pointerColor", "Foreground", RES_TYPE_STRING);
4965 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4966 "cursorColor", "Foreground", RES_TYPE_STRING);
4967 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4968 "borderColor", "BorderColor", RES_TYPE_STRING);
4970 #ifdef GLYPH_DEBUG
4971 image_cache_refcount =
4972 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4973 dpyinfo_refcount = dpyinfo->reference_count;
4974 #endif /* GLYPH_DEBUG */
4976 /* Init faces before x_default_parameter is called for the
4977 scroll-bar-width parameter because otherwise we end up in
4978 init_iterator with a null face cache, which should not happen. */
4979 init_frame_faces (f);
4981 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4983 x_figure_window_size (f, parms, 0);
4986 XSetWindowAttributes attrs;
4987 unsigned long mask;
4988 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
4990 block_input ();
4991 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4992 if (DoesSaveUnders (dpyinfo->screen))
4993 mask |= CWSaveUnder;
4995 /* Window managers look at the override-redirect flag to determine
4996 whether or net to give windows a decoration (Xlib spec, chapter
4997 3.2.8). */
4998 attrs.override_redirect = True;
4999 attrs.save_under = True;
5000 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
5001 /* Arrange for getting MapNotify and UnmapNotify events. */
5002 attrs.event_mask = StructureNotifyMask;
5003 tip_window
5004 = FRAME_X_WINDOW (f)
5005 = XCreateWindow (FRAME_X_DISPLAY (f),
5006 FRAME_DISPLAY_INFO (f)->root_window,
5007 /* x, y, width, height */
5008 0, 0, 1, 1,
5009 /* Border. */
5010 f->border_width,
5011 CopyFromParent, InputOutput, CopyFromParent,
5012 mask, &attrs);
5013 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
5014 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
5015 XA_ATOM, 32, PropModeReplace,
5016 (unsigned char *)&type, 1);
5017 unblock_input ();
5020 x_make_gc (f);
5022 x_default_parameter (f, parms, Qauto_raise, Qnil,
5023 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5024 x_default_parameter (f, parms, Qauto_lower, Qnil,
5025 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
5026 x_default_parameter (f, parms, Qcursor_type, Qbox,
5027 "cursorType", "CursorType", RES_TYPE_SYMBOL);
5029 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
5030 Change will not be effected unless different from the current
5031 FRAME_LINES (f). */
5032 width = FRAME_COLS (f);
5033 height = FRAME_LINES (f);
5034 SET_FRAME_COLS (f, 0);
5035 SET_FRAME_LINES (f, 0);
5036 change_frame_size (f, width, height, 1, 0, 0, 0);
5038 /* Add `tooltip' frame parameter's default value. */
5039 if (NILP (Fframe_parameter (frame, Qtooltip)))
5040 Fmodify_frame_parameters (frame, list1 (Fcons (Qtooltip, Qt)));
5042 /* FIXME - can this be done in a similar way to normal frames?
5043 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5045 /* Set the `display-type' frame parameter before setting up faces. */
5047 Lisp_Object disptype;
5049 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5050 disptype = intern ("mono");
5051 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5052 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5053 disptype = intern ("grayscale");
5054 else
5055 disptype = intern ("color");
5057 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5058 Fmodify_frame_parameters (frame, list1 (Fcons (Qdisplay_type, disptype)));
5061 /* Set up faces after all frame parameters are known. This call
5062 also merges in face attributes specified for new frames.
5064 Frame parameters may be changed if .Xdefaults contains
5065 specifications for the default font. For example, if there is an
5066 `Emacs.default.attributeBackground: pink', the `background-color'
5067 attribute of the frame get's set, which let's the internal border
5068 of the tooltip frame appear in pink. Prevent this. */
5070 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5072 /* Set tip_frame here, so that */
5073 tip_frame = frame;
5074 call2 (Qface_set_after_frame_default, frame, Qnil);
5076 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5077 Fmodify_frame_parameters (frame, list1 (Fcons (Qbackground_color, bg)));
5080 f->no_split = 1;
5082 UNGCPRO;
5084 /* Now that the frame will be official, it counts as a reference to
5085 its display and terminal. */
5086 FRAME_DISPLAY_INFO (f)->reference_count++;
5087 f->terminal->reference_count++;
5089 /* It is now ok to make the frame official even if we get an error
5090 below. And the frame needs to be on Vframe_list or making it
5091 visible won't work. */
5092 Vframe_list = Fcons (frame, Vframe_list);
5093 f->official = true;
5095 /* Setting attributes of faces of the tooltip frame from resources
5096 and similar will increment face_change_count, which leads to the
5097 clearing of all current matrices. Since this isn't necessary
5098 here, avoid it by resetting face_change_count to the value it
5099 had before we created the tip frame. */
5100 face_change_count = face_change_count_before;
5102 /* Discard the unwind_protect. */
5103 return unbind_to (count, frame);
5107 /* Compute where to display tip frame F. PARMS is the list of frame
5108 parameters for F. DX and DY are specified offsets from the current
5109 location of the mouse. WIDTH and HEIGHT are the width and height
5110 of the tooltip. Return coordinates relative to the root window of
5111 the display in *ROOT_X, and *ROOT_Y. */
5113 static void
5114 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)
5116 Lisp_Object left, top;
5117 int win_x, win_y;
5118 Window root, child;
5119 unsigned pmask;
5121 /* User-specified position? */
5122 left = Fcdr (Fassq (Qleft, parms));
5123 top = Fcdr (Fassq (Qtop, parms));
5125 /* Move the tooltip window where the mouse pointer is. Resize and
5126 show it. */
5127 if (!INTEGERP (left) || !INTEGERP (top))
5129 block_input ();
5130 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5131 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5132 unblock_input ();
5135 if (INTEGERP (top))
5136 *root_y = XINT (top);
5137 else if (*root_y + XINT (dy) <= 0)
5138 *root_y = 0; /* Can happen for negative dy */
5139 else if (*root_y + XINT (dy) + height
5140 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5141 /* It fits below the pointer */
5142 *root_y += XINT (dy);
5143 else if (height + XINT (dy) <= *root_y)
5144 /* It fits above the pointer. */
5145 *root_y -= height + XINT (dy);
5146 else
5147 /* Put it on the top. */
5148 *root_y = 0;
5150 if (INTEGERP (left))
5151 *root_x = XINT (left);
5152 else if (*root_x + XINT (dx) <= 0)
5153 *root_x = 0; /* Can happen for negative dx */
5154 else if (*root_x + XINT (dx) + width
5155 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5156 /* It fits to the right of the pointer. */
5157 *root_x += XINT (dx);
5158 else if (width + XINT (dx) <= *root_x)
5159 /* It fits to the left of the pointer. */
5160 *root_x -= width + XINT (dx);
5161 else
5162 /* Put it left-justified on the screen--it ought to fit that way. */
5163 *root_x = 0;
5167 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5168 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5169 A tooltip window is a small X window displaying a string.
5171 This is an internal function; Lisp code should call `tooltip-show'.
5173 FRAME nil or omitted means use the selected frame.
5175 PARMS is an optional list of frame parameters which can be used to
5176 change the tooltip's appearance.
5178 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5179 means use the default timeout of 5 seconds.
5181 If the list of frame parameters PARMS contains a `left' parameters,
5182 the tooltip is displayed at that x-position. Otherwise it is
5183 displayed at the mouse position, with offset DX added (default is 5 if
5184 DX isn't specified). Likewise for the y-position; if a `top' frame
5185 parameter is specified, it determines the y-position of the tooltip
5186 window, otherwise it is displayed at the mouse position, with offset
5187 DY added (default is -10).
5189 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5190 Text larger than the specified size is clipped. */)
5191 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5193 struct frame *f;
5194 struct window *w;
5195 int root_x, root_y;
5196 struct buffer *old_buffer;
5197 struct text_pos pos;
5198 int i, width, height, seen_reversed_p;
5199 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5200 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5201 ptrdiff_t count = SPECPDL_INDEX ();
5203 specbind (Qinhibit_redisplay, Qt);
5205 GCPRO4 (string, parms, frame, timeout);
5207 CHECK_STRING (string);
5208 if (SCHARS (string) == 0)
5209 string = make_unibyte_string (" ", 1);
5211 f = decode_window_system_frame (frame);
5212 if (NILP (timeout))
5213 timeout = make_number (5);
5214 else
5215 CHECK_NATNUM (timeout);
5217 if (NILP (dx))
5218 dx = make_number (5);
5219 else
5220 CHECK_NUMBER (dx);
5222 if (NILP (dy))
5223 dy = make_number (-10);
5224 else
5225 CHECK_NUMBER (dy);
5227 #ifdef USE_GTK
5228 if (x_gtk_use_system_tooltips)
5230 bool ok;
5232 /* Hide a previous tip, if any. */
5233 Fx_hide_tip ();
5235 block_input ();
5236 ok = xg_prepare_tooltip (f, string, &width, &height);
5237 if (ok)
5239 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5240 xg_show_tooltip (f, root_x, root_y);
5241 /* This is used in Fx_hide_tip. */
5242 XSETFRAME (tip_frame, f);
5244 unblock_input ();
5245 if (ok) goto start_timer;
5247 #endif /* USE_GTK */
5249 if (NILP (last_show_tip_args))
5250 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5252 if (!NILP (tip_frame))
5254 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5255 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5256 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5258 if (EQ (frame, last_frame)
5259 && !NILP (Fequal (last_string, string))
5260 && !NILP (Fequal (last_parms, parms)))
5262 struct frame *tip_f = XFRAME (tip_frame);
5264 /* Only DX and DY have changed. */
5265 if (!NILP (tip_timer))
5267 Lisp_Object timer = tip_timer;
5268 tip_timer = Qnil;
5269 call1 (Qcancel_timer, timer);
5272 block_input ();
5273 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5274 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5275 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5276 root_x, root_y);
5277 unblock_input ();
5278 goto start_timer;
5282 /* Hide a previous tip, if any. */
5283 Fx_hide_tip ();
5285 ASET (last_show_tip_args, 0, string);
5286 ASET (last_show_tip_args, 1, frame);
5287 ASET (last_show_tip_args, 2, parms);
5289 /* Add default values to frame parameters. */
5290 if (NILP (Fassq (Qname, parms)))
5291 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5292 if (NILP (Fassq (Qinternal_border_width, parms)))
5293 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5294 if (NILP (Fassq (Qborder_width, parms)))
5295 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5296 if (NILP (Fassq (Qbottom_divider_width, parms)))
5297 parms = Fcons (Fcons (Qbottom_divider_width, make_number (0)), parms);
5298 if (NILP (Fassq (Qright_divider_width, parms)))
5299 parms = Fcons (Fcons (Qright_divider_width, make_number (0)), parms);
5300 if (NILP (Fassq (Qborder_color, parms)))
5301 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5302 if (NILP (Fassq (Qbackground_color, parms)))
5303 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5304 parms);
5306 /* Create a frame for the tooltip, and record it in the global
5307 variable tip_frame. */
5308 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5309 f = XFRAME (frame);
5311 /* Set up the frame's root window. */
5312 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5313 w->left_col = 0;
5314 w->top_line = 0;
5315 w->pixel_left = 0;
5316 w->pixel_top = 0;
5318 if (CONSP (Vx_max_tooltip_size)
5319 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5320 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5322 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5323 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5325 else
5327 w->total_cols = 80;
5328 w->total_lines = 40;
5331 w->pixel_width = w->total_cols * FRAME_COLUMN_WIDTH (f);
5332 w->pixel_height = w->total_lines * FRAME_LINE_HEIGHT (f);
5334 FRAME_TOTAL_COLS (f) = w->total_cols;
5335 adjust_frame_glyphs (f);
5336 w->pseudo_window_p = 1;
5338 /* Display the tooltip text in a temporary buffer. */
5339 old_buffer = current_buffer;
5340 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5341 bset_truncate_lines (current_buffer, Qnil);
5342 clear_glyph_matrix (w->desired_matrix);
5343 clear_glyph_matrix (w->current_matrix);
5344 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5345 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5347 /* Compute width and height of the tooltip. */
5348 width = height = seen_reversed_p = 0;
5349 for (i = 0; i < w->desired_matrix->nrows; ++i)
5351 struct glyph_row *row = &w->desired_matrix->rows[i];
5352 struct glyph *last;
5353 int row_width;
5355 /* Stop at the first empty row at the end. */
5356 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5357 break;
5359 /* Let the row go over the full width of the frame. */
5360 row->full_width_p = 1;
5362 row_width = row->pixel_width;
5363 if (row->used[TEXT_AREA])
5365 /* There's a glyph at the end of rows that is used to place
5366 the cursor there. Don't include the width of this glyph. */
5367 if (!row->reversed_p)
5369 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5370 if (INTEGERP (last->object))
5371 row_width -= last->pixel_width;
5373 else
5375 /* There could be a stretch glyph at the beginning of R2L
5376 rows that is produced by extend_face_to_end_of_line.
5377 Don't count that glyph. */
5378 struct glyph *g = row->glyphs[TEXT_AREA];
5380 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5382 row_width -= g->pixel_width;
5383 seen_reversed_p = 1;
5388 height += row->height;
5389 width = max (width, row_width);
5392 /* If we've seen partial-length R2L rows, we need to re-adjust the
5393 tool-tip frame width and redisplay it again, to avoid over-wide
5394 tips due to the stretch glyph that extends R2L lines to full
5395 width of the frame. */
5396 if (seen_reversed_p)
5398 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5399 not in pixels. */
5400 w->pixel_width = width;
5401 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5402 w->total_cols = width;
5403 FRAME_TOTAL_COLS (f) = width;
5404 SET_FRAME_WIDTH (f, width);
5405 adjust_frame_glyphs (f);
5406 clear_glyph_matrix (w->desired_matrix);
5407 clear_glyph_matrix (w->current_matrix);
5408 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5409 width = height = 0;
5410 /* Recompute width and height of the tooltip. */
5411 for (i = 0; i < w->desired_matrix->nrows; ++i)
5413 struct glyph_row *row = &w->desired_matrix->rows[i];
5414 struct glyph *last;
5415 int row_width;
5417 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5418 break;
5419 row->full_width_p = 1;
5420 row_width = row->pixel_width;
5421 if (row->used[TEXT_AREA] && !row->reversed_p)
5423 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5424 if (INTEGERP (last->object))
5425 row_width -= last->pixel_width;
5428 height += row->height;
5429 width = max (width, row_width);
5433 /* Add the frame's internal border to the width and height the X
5434 window should have. */
5435 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5436 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5438 /* Move the tooltip window where the mouse pointer is. Resize and
5439 show it. */
5440 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5442 block_input ();
5443 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5444 root_x, root_y, width, height);
5445 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5446 unblock_input ();
5448 /* Draw into the window. */
5449 w->must_be_updated_p = 1;
5450 update_single_window (w, 1);
5452 /* Restore original current buffer. */
5453 set_buffer_internal_1 (old_buffer);
5454 windows_or_buffers_changed = old_windows_or_buffers_changed;
5456 start_timer:
5457 /* Let the tip disappear after timeout seconds. */
5458 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5459 intern ("x-hide-tip"));
5461 UNGCPRO;
5462 return unbind_to (count, Qnil);
5466 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5467 doc: /* Hide the current tooltip window, if there is any.
5468 Value is t if tooltip was open, nil otherwise. */)
5469 (void)
5471 ptrdiff_t count;
5472 Lisp_Object deleted, frame, timer;
5473 struct gcpro gcpro1, gcpro2;
5475 /* Return quickly if nothing to do. */
5476 if (NILP (tip_timer) && NILP (tip_frame))
5477 return Qnil;
5479 frame = tip_frame;
5480 timer = tip_timer;
5481 GCPRO2 (frame, timer);
5482 tip_frame = tip_timer = deleted = Qnil;
5484 count = SPECPDL_INDEX ();
5485 specbind (Qinhibit_redisplay, Qt);
5486 specbind (Qinhibit_quit, Qt);
5488 if (!NILP (timer))
5489 call1 (Qcancel_timer, timer);
5491 #ifdef USE_GTK
5493 /* When using system tooltip, tip_frame is the Emacs frame on which
5494 the tip is shown. */
5495 struct frame *f = XFRAME (frame);
5496 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5497 frame = Qnil;
5499 #endif
5501 if (FRAMEP (frame))
5503 delete_frame (frame, Qnil);
5504 deleted = Qt;
5506 #ifdef USE_LUCID
5507 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5508 redisplay procedure is not called when a tip frame over menu
5509 items is unmapped. Redisplay the menu manually... */
5511 Widget w;
5512 struct frame *f = SELECTED_FRAME ();
5513 w = f->output_data.x->menubar_widget;
5515 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5516 && w != NULL)
5518 block_input ();
5519 xlwmenu_redisplay (w);
5520 unblock_input ();
5523 #endif /* USE_LUCID */
5526 UNGCPRO;
5527 return unbind_to (count, deleted);
5532 /***********************************************************************
5533 File selection dialog
5534 ***********************************************************************/
5536 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5537 Sx_uses_old_gtk_dialog,
5538 0, 0, 0,
5539 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5540 (void)
5542 #ifdef USE_GTK
5543 if (use_dialog_box
5544 && use_file_dialog
5545 && window_system_available (SELECTED_FRAME ())
5546 && xg_uses_old_file_dialog ())
5547 return Qt;
5548 #endif
5549 return Qnil;
5553 #ifdef USE_MOTIF
5554 /* Callback for "OK" and "Cancel" on file selection dialog. */
5556 static void
5557 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5559 int *result = client_data;
5560 XmAnyCallbackStruct *cb = call_data;
5561 *result = cb->reason;
5565 /* Callback for unmapping a file selection dialog. This is used to
5566 capture the case where a dialog is closed via a window manager's
5567 closer button, for example. Using a XmNdestroyCallback didn't work
5568 in this case. */
5570 static void
5571 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5573 int *result = client_data;
5574 *result = XmCR_CANCEL;
5577 static void
5578 clean_up_file_dialog (void *arg)
5580 Widget dialog = arg;
5582 /* Clean up. */
5583 block_input ();
5584 XtUnmanageChild (dialog);
5585 XtDestroyWidget (dialog);
5586 x_menu_set_in_use (0);
5587 unblock_input ();
5591 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5592 doc: /* Read file name, prompting with PROMPT in directory DIR.
5593 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5594 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5595 or directory must exist.
5597 This function is only defined on NS, MS Windows, and X Windows with the
5598 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5599 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5600 On Windows 7 and later, the file selection dialog "remembers" the last
5601 directory where the user selected a file, and will open that directory
5602 instead of DIR on subsequent invocations of this function with the same
5603 value of DIR as in previous invocations; this is standard Windows behavior. */)
5604 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5605 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5607 int result;
5608 struct frame *f = SELECTED_FRAME ();
5609 Lisp_Object file = Qnil;
5610 Lisp_Object decoded_file;
5611 Widget dialog, text, help;
5612 Arg al[10];
5613 int ac = 0;
5614 XmString dir_xmstring, pattern_xmstring;
5615 ptrdiff_t count = SPECPDL_INDEX ();
5616 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5618 check_window_system (f);
5620 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5622 if (popup_activated ())
5623 error ("Trying to use a menu from within a menu-entry");
5625 CHECK_STRING (prompt);
5626 CHECK_STRING (dir);
5628 /* Prevent redisplay. */
5629 specbind (Qinhibit_redisplay, Qt);
5631 block_input ();
5633 /* Create the dialog with PROMPT as title, using DIR as initial
5634 directory and using "*" as pattern. */
5635 dir = Fexpand_file_name (dir, Qnil);
5636 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5637 pattern_xmstring = XmStringCreateLocalized ("*");
5639 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5640 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5641 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5642 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5643 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5644 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5645 "fsb", al, ac);
5646 XmStringFree (dir_xmstring);
5647 XmStringFree (pattern_xmstring);
5649 /* Add callbacks for OK and Cancel. */
5650 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5651 (XtPointer) &result);
5652 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5653 (XtPointer) &result);
5654 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5655 (XtPointer) &result);
5657 /* Remove the help button since we can't display help. */
5658 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5659 XtUnmanageChild (help);
5661 /* Mark OK button as default. */
5662 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5663 XmNshowAsDefault, True, NULL);
5665 /* If MUSTMATCH is non-nil, disable the file entry field of the
5666 dialog, so that the user must select a file from the files list
5667 box. We can't remove it because we wouldn't have a way to get at
5668 the result file name, then. */
5669 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5670 if (!NILP (mustmatch))
5672 Widget label;
5673 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5674 XtSetSensitive (text, False);
5675 XtSetSensitive (label, False);
5678 /* Manage the dialog, so that list boxes get filled. */
5679 XtManageChild (dialog);
5681 if (STRINGP (default_filename))
5683 XmString default_xmstring;
5684 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5685 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5687 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5688 XmTextFieldReplace (wtext, 0, last_pos,
5689 (SSDATA (Ffile_name_nondirectory (default_filename))));
5691 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5692 must include the path for this to work. */
5694 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5696 if (XmListItemExists (list, default_xmstring))
5698 int item_pos = XmListItemPos (list, default_xmstring);
5699 /* Select the item and scroll it into view. */
5700 XmListSelectPos (list, item_pos, True);
5701 XmListSetPos (list, item_pos);
5704 XmStringFree (default_xmstring);
5707 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5709 /* Process events until the user presses Cancel or OK. */
5710 x_menu_set_in_use (1);
5711 result = 0;
5712 while (result == 0)
5714 XEvent event;
5715 x_menu_wait_for_event (0);
5716 XtAppNextEvent (Xt_app_con, &event);
5717 if (event.type == KeyPress
5718 && FRAME_X_DISPLAY (f) == event.xkey.display)
5720 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5722 /* Pop down on C-g. */
5723 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5724 XtUnmanageChild (dialog);
5727 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5730 /* Get the result. */
5731 if (result == XmCR_OK)
5733 XmString text_string;
5734 String data;
5736 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5737 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5738 XmStringFree (text_string);
5739 file = build_string (data);
5740 XtFree (data);
5742 else
5743 file = Qnil;
5745 unblock_input ();
5746 UNGCPRO;
5748 /* Make "Cancel" equivalent to C-g. */
5749 if (NILP (file))
5750 Fsignal (Qquit, Qnil);
5752 decoded_file = DECODE_FILE (file);
5754 return unbind_to (count, decoded_file);
5757 #endif /* USE_MOTIF */
5759 #ifdef USE_GTK
5761 static void
5762 clean_up_dialog (void)
5764 x_menu_set_in_use (0);
5767 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5768 doc: /* Read file name, prompting with PROMPT in directory DIR.
5769 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5770 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5771 or directory must exist.
5773 This function is only defined on NS, MS Windows, and X Windows with the
5774 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5775 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories.
5776 On Windows 7 and later, the file selection dialog "remembers" the last
5777 directory where the user selected a file, and will open that directory
5778 instead of DIR on subsequent invocations of this function with the same
5779 value of DIR as in previous invocations; this is standard Windows behavior. */)
5780 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5782 struct frame *f = SELECTED_FRAME ();
5783 char *fn;
5784 Lisp_Object file = Qnil;
5785 Lisp_Object decoded_file;
5786 ptrdiff_t count = SPECPDL_INDEX ();
5787 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5788 char *cdef_file;
5790 check_window_system (f);
5792 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5794 if (popup_activated ())
5795 error ("Trying to use a menu from within a menu-entry");
5797 CHECK_STRING (prompt);
5798 CHECK_STRING (dir);
5800 /* Prevent redisplay. */
5801 specbind (Qinhibit_redisplay, Qt);
5802 record_unwind_protect_void (clean_up_dialog);
5804 block_input ();
5806 if (STRINGP (default_filename))
5807 cdef_file = SSDATA (default_filename);
5808 else
5809 cdef_file = SSDATA (dir);
5811 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5812 ! NILP (mustmatch),
5813 ! NILP (only_dir_p));
5815 if (fn)
5817 file = build_string (fn);
5818 xfree (fn);
5821 unblock_input ();
5822 UNGCPRO;
5824 /* Make "Cancel" equivalent to C-g. */
5825 if (NILP (file))
5826 Fsignal (Qquit, Qnil);
5828 decoded_file = DECODE_FILE (file);
5830 return unbind_to (count, decoded_file);
5834 #ifdef HAVE_FREETYPE
5836 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5837 doc: /* Read a font using a GTK dialog.
5838 Return either a font spec (for GTK versions >= 3.2) or a string
5839 containing a GTK-style font name.
5841 FRAME is the frame on which to pop up the font chooser. If omitted or
5842 nil, it defaults to the selected frame. */)
5843 (Lisp_Object frame, Lisp_Object ignored)
5845 struct frame *f = decode_window_system_frame (frame);
5846 Lisp_Object font;
5847 Lisp_Object font_param;
5848 char *default_name = NULL;
5849 struct gcpro gcpro1, gcpro2;
5850 ptrdiff_t count = SPECPDL_INDEX ();
5852 if (popup_activated ())
5853 error ("Trying to use a menu from within a menu-entry");
5855 /* Prevent redisplay. */
5856 specbind (Qinhibit_redisplay, Qt);
5857 record_unwind_protect_void (clean_up_dialog);
5859 block_input ();
5861 GCPRO2 (font_param, font);
5863 XSETFONT (font, FRAME_FONT (f));
5864 font_param = Ffont_get (font, intern (":name"));
5865 if (STRINGP (font_param))
5866 default_name = xstrdup (SSDATA (font_param));
5867 else
5869 font_param = Fframe_parameter (frame, Qfont_param);
5870 if (STRINGP (font_param))
5871 default_name = xstrdup (SSDATA (font_param));
5874 font = xg_get_font (f, default_name);
5875 xfree (default_name);
5877 unblock_input ();
5879 if (NILP (font))
5880 Fsignal (Qquit, Qnil);
5882 return unbind_to (count, font);
5884 #endif /* HAVE_FREETYPE */
5886 #endif /* USE_GTK */
5889 /***********************************************************************
5890 Keyboard
5891 ***********************************************************************/
5893 #ifdef HAVE_XKB
5894 #include <X11/XKBlib.h>
5895 #include <X11/keysym.h>
5896 #endif
5898 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5899 Sx_backspace_delete_keys_p, 0, 1, 0,
5900 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5901 FRAME nil means use the selected frame.
5902 Value is t if we know that both keys are present, and are mapped to the
5903 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5904 present and mapped to the usual X keysyms. */)
5905 (Lisp_Object frame)
5907 #ifndef HAVE_XKB
5908 return Qlambda;
5909 #else
5910 XkbDescPtr kb;
5911 struct frame *f = decode_window_system_frame (frame);
5912 Display *dpy = FRAME_X_DISPLAY (f);
5913 Lisp_Object have_keys;
5914 int major, minor, op, event, error_code;
5916 block_input ();
5918 /* Check library version in case we're dynamically linked. */
5919 major = XkbMajorVersion;
5920 minor = XkbMinorVersion;
5921 if (!XkbLibraryVersion (&major, &minor))
5923 unblock_input ();
5924 return Qlambda;
5927 /* Check that the server supports XKB. */
5928 major = XkbMajorVersion;
5929 minor = XkbMinorVersion;
5930 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
5932 unblock_input ();
5933 return Qlambda;
5936 /* In this code we check that the keyboard has physical keys with names
5937 that start with BKSP (Backspace) and DELE (Delete), and that they
5938 generate keysym XK_BackSpace and XK_Delete respectively.
5939 This function is used to test if normal-erase-is-backspace should be
5940 turned on.
5941 An alternative approach would be to just check if XK_BackSpace and
5942 XK_Delete are mapped to any key. But if any of those are mapped to
5943 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5944 user doesn't know about it, it is better to return false here.
5945 It is more obvious to the user what to do if she/he has two keys
5946 clearly marked with names/symbols and one key does something not
5947 expected (i.e. she/he then tries the other).
5948 The cases where Backspace/Delete is mapped to some other key combination
5949 are rare, and in those cases, normal-erase-is-backspace can be turned on
5950 manually. */
5952 have_keys = Qnil;
5953 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5954 if (kb)
5956 int delete_keycode = 0, backspace_keycode = 0, i;
5958 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5960 for (i = kb->min_key_code;
5961 (i < kb->max_key_code
5962 && (delete_keycode == 0 || backspace_keycode == 0));
5963 ++i)
5965 /* The XKB symbolic key names can be seen most easily in
5966 the PS file generated by `xkbprint -label name
5967 $DISPLAY'. */
5968 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5969 delete_keycode = i;
5970 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5971 backspace_keycode = i;
5974 XkbFreeNames (kb, 0, True);
5977 /* As of libX11-1.6.2, XkbGetMap manual says that you should use
5978 XkbFreeClientMap to free the data returned by XkbGetMap. But
5979 this function just frees the data referenced from KB and not
5980 KB itself. To free KB as well, call XkbFreeKeyboard. */
5981 XkbFreeKeyboard (kb, XkbAllMapComponentsMask, True);
5983 if (delete_keycode
5984 && backspace_keycode
5985 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5986 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5987 have_keys = Qt;
5989 unblock_input ();
5990 return have_keys;
5991 #endif
5996 /***********************************************************************
5997 Initialization
5998 ***********************************************************************/
6000 /* Keep this list in the same order as frame_parms in frame.c.
6001 Use 0 for unsupported frame parameters. */
6003 frame_parm_handler x_frame_parm_handlers[] =
6005 x_set_autoraise,
6006 x_set_autolower,
6007 x_set_background_color,
6008 x_set_border_color,
6009 x_set_border_width,
6010 x_set_cursor_color,
6011 x_set_cursor_type,
6012 x_set_font,
6013 x_set_foreground_color,
6014 x_set_icon_name,
6015 x_set_icon_type,
6016 x_set_internal_border_width,
6017 x_set_right_divider_width,
6018 x_set_bottom_divider_width,
6019 x_set_menu_bar_lines,
6020 x_set_mouse_color,
6021 x_explicitly_set_name,
6022 x_set_scroll_bar_width,
6023 x_set_scroll_bar_height,
6024 x_set_title,
6025 x_set_unsplittable,
6026 x_set_vertical_scroll_bars,
6027 x_set_horizontal_scroll_bars,
6028 x_set_visibility,
6029 x_set_tool_bar_lines,
6030 x_set_scroll_bar_foreground,
6031 x_set_scroll_bar_background,
6032 x_set_screen_gamma,
6033 x_set_line_spacing,
6034 x_set_left_fringe,
6035 x_set_right_fringe,
6036 x_set_wait_for_wm,
6037 x_set_fullscreen,
6038 x_set_font_backend,
6039 x_set_alpha,
6040 x_set_sticky,
6041 x_set_tool_bar_position,
6044 void
6045 syms_of_xfns (void)
6047 DEFSYM (Qundefined_color, "undefined-color");
6048 DEFSYM (Qcompound_text, "compound-text");
6049 DEFSYM (Qcancel_timer, "cancel-timer");
6050 DEFSYM (Qfont_param, "font-parameter");
6052 Fput (Qundefined_color, Qerror_conditions,
6053 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6054 Fput (Qundefined_color, Qerror_message,
6055 build_pure_c_string ("Undefined color"));
6057 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6058 doc: /* The shape of the pointer when over text.
6059 Changing the value does not affect existing frames
6060 unless you set the mouse color. */);
6061 Vx_pointer_shape = Qnil;
6063 #if 0 /* This doesn't really do anything. */
6064 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6065 doc: /* The shape of the pointer when not over text.
6066 This variable takes effect when you create a new frame
6067 or when you set the mouse color. */);
6068 #endif
6069 Vx_nontext_pointer_shape = Qnil;
6071 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6072 doc: /* The shape of the pointer when Emacs is busy.
6073 This variable takes effect when you create a new frame
6074 or when you set the mouse color. */);
6075 Vx_hourglass_pointer_shape = Qnil;
6077 #if 0 /* This doesn't really do anything. */
6078 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6079 doc: /* The shape of the pointer when over the mode line.
6080 This variable takes effect when you create a new frame
6081 or when you set the mouse color. */);
6082 #endif
6083 Vx_mode_pointer_shape = Qnil;
6085 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6086 Vx_sensitive_text_pointer_shape,
6087 doc: /* The shape of the pointer when over mouse-sensitive text.
6088 This variable takes effect when you create a new frame
6089 or when you set the mouse color. */);
6090 Vx_sensitive_text_pointer_shape = Qnil;
6092 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6093 Vx_window_horizontal_drag_shape,
6094 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6095 This variable takes effect when you create a new frame
6096 or when you set the mouse color. */);
6097 Vx_window_horizontal_drag_shape = Qnil;
6099 DEFVAR_LISP ("x-window-vertical-drag-cursor",
6100 Vx_window_vertical_drag_shape,
6101 doc: /* Pointer shape to use for indicating a window can be dragged vertically.
6102 This variable takes effect when you create a new frame
6103 or when you set the mouse color. */);
6104 Vx_window_vertical_drag_shape = Qnil;
6106 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6107 doc: /* A string indicating the foreground color of the cursor box. */);
6108 Vx_cursor_fore_pixel = Qnil;
6110 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6111 doc: /* Maximum size for tooltips.
6112 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6113 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6115 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6116 doc: /* Non-nil if no X window manager is in use.
6117 Emacs doesn't try to figure this out; this is always nil
6118 unless you set it to something else. */);
6119 /* We don't have any way to find this out, so set it to nil
6120 and maybe the user would like to set it to t. */
6121 Vx_no_window_manager = Qnil;
6123 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6124 Vx_pixel_size_width_font_regexp,
6125 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6127 Since Emacs gets width of a font matching with this regexp from
6128 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6129 such a font. This is especially effective for such large fonts as
6130 Chinese, Japanese, and Korean. */);
6131 Vx_pixel_size_width_font_regexp = Qnil;
6133 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6134 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6135 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6136 If nil or if the file selection dialog is not available, the new GTK file
6137 chooser is used instead. To turn off all file dialogs set the
6138 variable `use-file-dialog'. */);
6139 x_gtk_use_old_file_dialog = 0;
6141 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6142 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6143 Note that this is just the default, there is a toggle button on the file
6144 chooser to show or not show hidden files on a case by case basis. */);
6145 x_gtk_show_hidden_files = 0;
6147 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6148 doc: /* If non-nil, the GTK file chooser will show additional help text.
6149 If more space for files in the file chooser dialog is wanted, set this to nil
6150 to turn the additional text off. */);
6151 x_gtk_file_dialog_help_text = 1;
6153 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
6154 doc: /* If non-nil, a detached tool bar is shown in full.
6155 The default is to just show an arrow and pressing on that arrow shows
6156 the tool bar buttons. */);
6157 x_gtk_whole_detached_tool_bar = 0;
6159 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6160 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6161 Otherwise use Emacs own tooltip implementation.
6162 When using Gtk+ tooltips, the tooltip face is not used. */);
6163 x_gtk_use_system_tooltips = 1;
6165 /* Tell Emacs about this window system. */
6166 Fprovide (Qx, Qnil);
6168 #ifdef USE_X_TOOLKIT
6169 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6170 #ifdef USE_MOTIF
6171 Fprovide (intern_c_string ("motif"), Qnil);
6173 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6174 doc: /* Version info for LessTif/Motif. */);
6175 Vmotif_version_string = build_string (XmVERSION_STRING);
6176 #endif /* USE_MOTIF */
6177 #endif /* USE_X_TOOLKIT */
6179 #ifdef USE_GTK
6180 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6181 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6182 But for a user it is a toolkit for X, and indeed, configure
6183 accepts --with-x-toolkit=gtk. */
6184 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6185 Fprovide (intern_c_string ("gtk"), Qnil);
6186 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6188 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6189 doc: /* Version info for GTK+. */);
6191 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6192 int len = sprintf (gtk_version, "%d.%d.%d",
6193 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6194 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6196 #endif /* USE_GTK */
6198 /* X window properties. */
6199 defsubr (&Sx_change_window_property);
6200 defsubr (&Sx_delete_window_property);
6201 defsubr (&Sx_window_property);
6203 defsubr (&Sxw_display_color_p);
6204 defsubr (&Sx_display_grayscale_p);
6205 defsubr (&Sxw_color_defined_p);
6206 defsubr (&Sxw_color_values);
6207 defsubr (&Sx_server_max_request_size);
6208 defsubr (&Sx_server_vendor);
6209 defsubr (&Sx_server_version);
6210 defsubr (&Sx_display_pixel_width);
6211 defsubr (&Sx_display_pixel_height);
6212 defsubr (&Sx_display_mm_width);
6213 defsubr (&Sx_display_mm_height);
6214 defsubr (&Sx_display_screens);
6215 defsubr (&Sx_display_planes);
6216 defsubr (&Sx_display_color_cells);
6217 defsubr (&Sx_display_visual_class);
6218 defsubr (&Sx_display_backing_store);
6219 defsubr (&Sx_display_save_under);
6220 defsubr (&Sx_display_monitor_attributes_list);
6221 defsubr (&Sx_wm_set_size_hint);
6222 defsubr (&Sx_create_frame);
6223 defsubr (&Sx_open_connection);
6224 defsubr (&Sx_close_connection);
6225 defsubr (&Sx_display_list);
6226 defsubr (&Sx_synchronize);
6227 defsubr (&Sx_backspace_delete_keys_p);
6229 defsubr (&Sx_show_tip);
6230 defsubr (&Sx_hide_tip);
6231 tip_timer = Qnil;
6232 staticpro (&tip_timer);
6233 tip_frame = Qnil;
6234 staticpro (&tip_frame);
6236 last_show_tip_args = Qnil;
6237 staticpro (&last_show_tip_args);
6239 defsubr (&Sx_uses_old_gtk_dialog);
6240 #if defined (USE_MOTIF) || defined (USE_GTK)
6241 defsubr (&Sx_file_dialog);
6242 #endif
6244 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6245 defsubr (&Sx_select_font);
6246 #endif