* net/eww.el (eww-render): Always set eww-current-url, and update header line.
[emacs.git] / src / xfns.c
blob2b895642942f02d698b752c2d64b38930dce95dd
1 /* Functions for the X window system.
3 Copyright (C) 1989, 1992-2013 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 "frame.h"
28 #include "window.h"
29 #include "character.h"
30 #include "buffer.h"
31 #include "intervals.h"
32 #include "dispextern.h"
33 #include "keyboard.h"
34 #include "blockinput.h"
35 #include <epaths.h>
36 #include "charset.h"
37 #include "coding.h"
38 #include "fontset.h"
39 #include "systime.h"
40 #include "termhooks.h"
41 #include "atimer.h"
42 #include "termchar.h"
43 #include "font.h"
45 #include <sys/types.h>
46 #include <sys/stat.h>
48 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
49 #include "bitmaps/gray.xbm"
50 #else
51 #include <X11/bitmaps/gray>
52 #endif
54 #include "xsettings.h"
56 #ifdef HAVE_XRANDR
57 #include <X11/extensions/Xrandr.h>
58 #endif
59 #ifdef HAVE_XINERAMA
60 #include <X11/extensions/Xinerama.h>
61 #endif
63 #ifdef USE_GTK
64 #include "gtkutil.h"
65 #endif
67 #ifdef USE_X_TOOLKIT
68 #include <X11/Shell.h>
70 #ifndef USE_MOTIF
71 #ifdef HAVE_XAW3D
72 #include <X11/Xaw3d/Paned.h>
73 #include <X11/Xaw3d/Label.h>
74 #else /* !HAVE_XAW3D */
75 #include <X11/Xaw/Paned.h>
76 #include <X11/Xaw/Label.h>
77 #endif /* HAVE_XAW3D */
78 #endif /* USE_MOTIF */
80 #ifdef USG
81 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
82 #include <X11/Xos.h>
83 #define USG
84 #ifdef USG /* Pacify gcc -Wunused-macros. */
85 #endif
86 #else
87 #include <X11/Xos.h>
88 #endif
90 #include "widget.h"
92 #include "../lwlib/lwlib.h"
94 #ifdef USE_MOTIF
95 #include <Xm/Xm.h>
96 #include <Xm/DialogS.h>
97 #include <Xm/FileSB.h>
98 #include <Xm/List.h>
99 #include <Xm/TextF.h>
100 #endif
102 #ifdef USE_LUCID
103 #include "../lwlib/xlwmenu.h"
104 #endif
106 #if !defined (NO_EDITRES)
107 #define HACK_EDITRES
108 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
109 #endif /* not defined NO_EDITRES */
111 /* Unique id counter for widgets created by the Lucid Widget Library. */
113 extern LWLIB_ID widget_id_tick;
115 #ifdef USE_MOTIF
117 #endif /* USE_MOTIF */
119 #endif /* USE_X_TOOLKIT */
121 #ifdef USE_GTK
123 #endif /* USE_GTK */
125 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
127 static Lisp_Object Qsuppress_icon;
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;
335 /* Gamma-correct COLOR on frame F. */
337 void
338 gamma_correct (struct frame *f, XColor *color)
340 if (f->gamma)
342 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
343 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
344 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
349 /* Decide if color named COLOR_NAME is valid for use on frame F. If
350 so, return the RGB values in COLOR. If ALLOC_P,
351 allocate the color. Value is false if COLOR_NAME is invalid, or
352 no color could be allocated. */
354 bool
355 x_defined_color (struct frame *f, const char *color_name,
356 XColor *color, bool alloc_p)
358 bool success_p = 0;
359 Display *dpy = FRAME_X_DISPLAY (f);
360 Colormap cmap = FRAME_X_COLORMAP (f);
362 block_input ();
363 #ifdef USE_GTK
364 success_p = xg_check_special_colors (f, color_name, color);
365 #endif
366 if (!success_p)
367 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
368 if (success_p && alloc_p)
369 success_p = x_alloc_nearest_color (f, cmap, color);
370 unblock_input ();
372 return success_p;
376 /* Return the pixel color value for color COLOR_NAME on frame F. If F
377 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
378 Signal an error if color can't be allocated. */
380 static int
381 x_decode_color (struct frame *f, Lisp_Object color_name, int mono_color)
383 XColor cdef;
385 CHECK_STRING (color_name);
387 #if 0 /* Don't do this. It's wrong when we're not using the default
388 colormap, it makes freeing difficult, and it's probably not
389 an important optimization. */
390 if (strcmp (SDATA (color_name), "black") == 0)
391 return BLACK_PIX_DEFAULT (f);
392 else if (strcmp (SDATA (color_name), "white") == 0)
393 return WHITE_PIX_DEFAULT (f);
394 #endif
396 /* Return MONO_COLOR for monochrome frames. */
397 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
398 return mono_color;
400 /* x_defined_color is responsible for coping with failures
401 by looking for a near-miss. */
402 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
403 return cdef.pixel;
405 signal_error ("Undefined color", color_name);
410 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
411 the previous value of that parameter, NEW_VALUE is the new value.
412 See also the comment of wait_for_wm in struct x_output. */
414 static void
415 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
417 f->output_data.x->wait_for_wm = !NILP (new_value);
420 static void
421 x_set_tool_bar_position (struct frame *f,
422 Lisp_Object new_value,
423 Lisp_Object old_value)
425 if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
426 && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
427 return;
428 if (EQ (new_value, old_value)) return;
430 #ifdef USE_GTK
431 xg_change_toolbar_position (f, new_value);
432 fset_tool_bar_position (f, new_value);
433 #endif
436 #ifdef USE_GTK
438 /* Set icon from FILE for frame F. By using GTK functions the icon
439 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
442 xg_set_icon (struct frame *f, Lisp_Object file)
444 int result = 0;
445 Lisp_Object found;
447 found = x_find_image_file (file);
449 if (! NILP (found))
451 GdkPixbuf *pixbuf;
452 GError *err = NULL;
453 char *filename = SSDATA (found);
454 block_input ();
456 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
458 if (pixbuf)
460 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
461 pixbuf);
462 g_object_unref (pixbuf);
464 result = 1;
466 else
467 g_error_free (err);
469 unblock_input ();
472 return result;
476 xg_set_icon_from_xpm_data (struct frame *f, const char **data)
478 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
480 if (!pixbuf)
481 return 0;
483 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
484 g_object_unref (pixbuf);
485 return 1;
487 #endif /* USE_GTK */
490 /* Functions called only from `x_set_frame_param'
491 to set individual parameters.
493 If FRAME_X_WINDOW (f) is 0,
494 the frame is being created and its X-window does not exist yet.
495 In that case, just record the parameter's new value
496 in the standard place; do not attempt to change the window. */
498 static void
499 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
501 struct x_output *x = f->output_data.x;
502 unsigned long fg, old_fg;
504 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
505 old_fg = FRAME_FOREGROUND_PIXEL (f);
506 FRAME_FOREGROUND_PIXEL (f) = fg;
508 if (FRAME_X_WINDOW (f) != 0)
510 Display *dpy = FRAME_X_DISPLAY (f);
512 block_input ();
513 XSetForeground (dpy, x->normal_gc, fg);
514 XSetBackground (dpy, x->reverse_gc, fg);
516 if (x->cursor_pixel == old_fg)
518 unload_color (f, x->cursor_pixel);
519 x->cursor_pixel = x_copy_color (f, fg);
520 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
523 unblock_input ();
525 update_face_from_frame_parameter (f, Qforeground_color, arg);
527 if (FRAME_VISIBLE_P (f))
528 redraw_frame (f);
531 unload_color (f, old_fg);
534 static void
535 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
537 struct x_output *x = f->output_data.x;
538 unsigned long bg;
540 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
541 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
542 FRAME_BACKGROUND_PIXEL (f) = bg;
544 if (FRAME_X_WINDOW (f) != 0)
546 Display *dpy = FRAME_X_DISPLAY (f);
548 block_input ();
549 XSetBackground (dpy, x->normal_gc, bg);
550 XSetForeground (dpy, x->reverse_gc, bg);
551 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
552 XSetForeground (dpy, x->cursor_gc, bg);
554 #ifdef USE_GTK
555 xg_set_background_color (f, bg);
556 #endif
558 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
559 toolkit scroll bars. */
561 Lisp_Object bar;
562 for (bar = FRAME_SCROLL_BARS (f);
563 !NILP (bar);
564 bar = XSCROLL_BAR (bar)->next)
566 Window window = XSCROLL_BAR (bar)->x_window;
567 XSetWindowBackground (dpy, window, bg);
570 #endif /* USE_TOOLKIT_SCROLL_BARS */
572 unblock_input ();
573 update_face_from_frame_parameter (f, Qbackground_color, arg);
575 if (FRAME_VISIBLE_P (f))
576 redraw_frame (f);
580 static Cursor
581 make_invisible_cursor (struct frame *f)
583 Display *dpy = FRAME_X_DISPLAY (f);
584 static char const no_data[] = { 0 };
585 Pixmap pix;
586 XColor col;
587 Cursor c = 0;
589 x_catch_errors (dpy);
590 pix = XCreateBitmapFromData (dpy, FRAME_DISPLAY_INFO (f)->root_window,
591 no_data, 1, 1);
592 if (! x_had_errors_p (dpy) && pix != None)
594 Cursor pixc;
595 col.pixel = 0;
596 col.red = col.green = col.blue = 0;
597 col.flags = DoRed | DoGreen | DoBlue;
598 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
599 if (! x_had_errors_p (dpy) && pixc != None)
600 c = pixc;
601 XFreePixmap (dpy, pix);
604 x_uncatch_errors ();
606 return c;
609 static void
610 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
612 struct x_output *x = f->output_data.x;
613 Display *dpy = FRAME_X_DISPLAY (f);
614 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
615 Cursor hourglass_cursor, horizontal_drag_cursor;
616 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
617 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
619 /* Don't let pointers be invisible. */
620 if (mask_color == pixel)
622 x_free_colors (f, &pixel, 1);
623 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
626 unload_color (f, x->mouse_pixel);
627 x->mouse_pixel = pixel;
629 block_input ();
631 /* It's not okay to crash if the user selects a screwy cursor. */
632 x_catch_errors (dpy);
634 if (!NILP (Vx_pointer_shape))
636 CHECK_NUMBER (Vx_pointer_shape);
637 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
639 else
640 cursor = XCreateFontCursor (dpy, XC_xterm);
641 x_check_errors (dpy, "bad text pointer cursor: %s");
643 if (!NILP (Vx_nontext_pointer_shape))
645 CHECK_NUMBER (Vx_nontext_pointer_shape);
646 nontext_cursor
647 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
649 else
650 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
651 x_check_errors (dpy, "bad nontext pointer cursor: %s");
653 if (!NILP (Vx_hourglass_pointer_shape))
655 CHECK_NUMBER (Vx_hourglass_pointer_shape);
656 hourglass_cursor
657 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
659 else
660 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
661 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
663 if (!NILP (Vx_mode_pointer_shape))
665 CHECK_NUMBER (Vx_mode_pointer_shape);
666 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
668 else
669 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
670 x_check_errors (dpy, "bad modeline pointer cursor: %s");
672 if (!NILP (Vx_sensitive_text_pointer_shape))
674 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
675 hand_cursor
676 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
678 else
679 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
681 if (!NILP (Vx_window_horizontal_drag_shape))
683 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
684 horizontal_drag_cursor
685 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
687 else
688 horizontal_drag_cursor
689 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
691 /* Check and report errors with the above calls. */
692 x_check_errors (dpy, "can't set cursor shape: %s");
693 x_uncatch_errors ();
696 XColor fore_color, back_color;
698 fore_color.pixel = x->mouse_pixel;
699 x_query_color (f, &fore_color);
700 back_color.pixel = mask_color;
701 x_query_color (f, &back_color);
703 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
704 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
705 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
706 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
707 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
708 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
711 if (FRAME_X_WINDOW (f) != 0)
712 XDefineCursor (dpy, FRAME_X_WINDOW (f),
713 f->output_data.x->current_cursor = cursor);
715 if (FRAME_DISPLAY_INFO (f)->invisible_cursor == 0)
716 FRAME_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
718 if (cursor != x->text_cursor
719 && x->text_cursor != 0)
720 XFreeCursor (dpy, x->text_cursor);
721 x->text_cursor = cursor;
723 if (nontext_cursor != x->nontext_cursor
724 && x->nontext_cursor != 0)
725 XFreeCursor (dpy, x->nontext_cursor);
726 x->nontext_cursor = nontext_cursor;
728 if (hourglass_cursor != x->hourglass_cursor
729 && x->hourglass_cursor != 0)
730 XFreeCursor (dpy, x->hourglass_cursor);
731 x->hourglass_cursor = hourglass_cursor;
733 if (mode_cursor != x->modeline_cursor
734 && x->modeline_cursor != 0)
735 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
736 x->modeline_cursor = mode_cursor;
738 if (hand_cursor != x->hand_cursor
739 && x->hand_cursor != 0)
740 XFreeCursor (dpy, x->hand_cursor);
741 x->hand_cursor = hand_cursor;
743 if (horizontal_drag_cursor != x->horizontal_drag_cursor
744 && x->horizontal_drag_cursor != 0)
745 XFreeCursor (dpy, x->horizontal_drag_cursor);
746 x->horizontal_drag_cursor = horizontal_drag_cursor;
748 XFlush (dpy);
749 unblock_input ();
751 update_face_from_frame_parameter (f, Qmouse_color, arg);
754 static void
755 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
757 unsigned long fore_pixel, pixel;
758 bool fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
759 struct x_output *x = f->output_data.x;
761 if (!NILP (Vx_cursor_fore_pixel))
763 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
764 WHITE_PIX_DEFAULT (f));
765 fore_pixel_allocated_p = 1;
767 else
768 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
770 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
771 pixel_allocated_p = 1;
773 /* Make sure that the cursor color differs from the background color. */
774 if (pixel == FRAME_BACKGROUND_PIXEL (f))
776 if (pixel_allocated_p)
778 x_free_colors (f, &pixel, 1);
779 pixel_allocated_p = 0;
782 pixel = x->mouse_pixel;
783 if (pixel == fore_pixel)
785 if (fore_pixel_allocated_p)
787 x_free_colors (f, &fore_pixel, 1);
788 fore_pixel_allocated_p = 0;
790 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
794 unload_color (f, x->cursor_foreground_pixel);
795 if (!fore_pixel_allocated_p)
796 fore_pixel = x_copy_color (f, fore_pixel);
797 x->cursor_foreground_pixel = fore_pixel;
799 unload_color (f, x->cursor_pixel);
800 if (!pixel_allocated_p)
801 pixel = x_copy_color (f, pixel);
802 x->cursor_pixel = pixel;
804 if (FRAME_X_WINDOW (f) != 0)
806 block_input ();
807 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
808 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
809 unblock_input ();
811 if (FRAME_VISIBLE_P (f))
813 x_update_cursor (f, 0);
814 x_update_cursor (f, 1);
818 update_face_from_frame_parameter (f, Qcursor_color, arg);
821 /* Set the border-color of frame F to pixel value PIX.
822 Note that this does not fully take effect if done before
823 F has an x-window. */
825 static void
826 x_set_border_pixel (struct frame *f, int pix)
828 unload_color (f, f->output_data.x->border_pixel);
829 f->output_data.x->border_pixel = pix;
831 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
833 block_input ();
834 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
835 unblock_input ();
837 if (FRAME_VISIBLE_P (f))
838 redraw_frame (f);
842 /* Set the border-color of frame F to value described by ARG.
843 ARG can be a string naming a color.
844 The border-color is used for the border that is drawn by the X server.
845 Note that this does not fully take effect if done before
846 F has an x-window; it must be redone when the window is created.
848 Note: this is done in two routines because of the way X10 works.
850 Note: under X11, this is normally the province of the window manager,
851 and so emacs's border colors may be overridden. */
853 static void
854 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
856 int pix;
858 CHECK_STRING (arg);
859 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
860 x_set_border_pixel (f, pix);
861 update_face_from_frame_parameter (f, Qborder_color, arg);
865 static void
866 x_set_cursor_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
868 set_frame_cursor_types (f, arg);
871 static void
872 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
874 int result;
876 if (STRINGP (arg))
878 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
879 return;
881 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
882 return;
884 block_input ();
885 if (NILP (arg))
886 result = x_text_icon (f,
887 SSDATA ((!NILP (f->icon_name)
888 ? f->icon_name
889 : f->name)));
890 else
891 result = x_bitmap_icon (f, arg);
893 if (result)
895 unblock_input ();
896 error ("No icon window available");
899 XFlush (FRAME_X_DISPLAY (f));
900 unblock_input ();
903 static void
904 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
906 int result;
908 if (STRINGP (arg))
910 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
911 return;
913 else if (!NILP (arg) || NILP (oldval))
914 return;
916 fset_icon_name (f, arg);
918 if (f->output_data.x->icon_bitmap != 0)
919 return;
921 block_input ();
923 result = x_text_icon (f,
924 SSDATA ((!NILP (f->icon_name)
925 ? f->icon_name
926 : !NILP (f->title)
927 ? f->title
928 : f->name)));
930 if (result)
932 unblock_input ();
933 error ("No icon window available");
936 XFlush (FRAME_X_DISPLAY (f));
937 unblock_input ();
941 void
942 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
944 int nlines;
945 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
946 int olines = FRAME_MENU_BAR_LINES (f);
947 #endif
949 /* Right now, menu bars don't work properly in minibuf-only frames;
950 most of the commands try to apply themselves to the minibuffer
951 frame itself, and get an error because you can't switch buffers
952 in or split the minibuffer window. */
953 if (FRAME_MINIBUF_ONLY_P (f))
954 return;
956 if (TYPE_RANGED_INTEGERP (int, value))
957 nlines = XINT (value);
958 else
959 nlines = 0;
961 /* Make sure we redisplay all windows in this frame. */
962 windows_or_buffers_changed++;
964 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
965 FRAME_MENU_BAR_LINES (f) = 0;
966 if (nlines)
968 FRAME_EXTERNAL_MENU_BAR (f) = 1;
969 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
970 /* Make sure next redisplay shows the menu bar. */
971 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
973 else
975 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
976 free_frame_menubar (f);
977 FRAME_EXTERNAL_MENU_BAR (f) = 0;
978 if (FRAME_X_P (f))
979 f->output_data.x->menubar_widget = 0;
981 #else /* not USE_X_TOOLKIT && not USE_GTK */
982 FRAME_MENU_BAR_LINES (f) = nlines;
983 resize_frame_windows (f, FRAME_LINES (f), 0);
985 /* If the menu bar height gets changed, the internal border below
986 the top margin has to be cleared. Also, if the menu bar gets
987 larger, the area for the added lines has to be cleared except for
988 the first menu bar line that is to be drawn later. */
989 if (nlines != olines)
991 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
992 int width = FRAME_PIXEL_WIDTH (f);
993 int y;
995 /* height can be zero here. */
996 if (height > 0 && width > 0)
998 y = FRAME_TOP_MARGIN_HEIGHT (f);
1000 block_input ();
1001 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1002 0, y, width, height);
1003 unblock_input ();
1006 if (nlines > 1 && nlines > olines)
1008 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1009 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1011 block_input ();
1012 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1013 0, y, width, height);
1014 unblock_input ();
1017 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1018 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1020 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1021 adjust_frame_glyphs (f);
1022 run_window_configuration_change_hook (f);
1026 /* Set the number of lines used for the tool bar of frame F to VALUE.
1027 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1028 is the old number of tool bar lines. This function changes the
1029 height of all windows on frame F to match the new tool bar height.
1030 The frame's height doesn't change. */
1032 void
1033 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1035 int nlines;
1036 #if ! defined (USE_GTK)
1037 int delta, root_height;
1038 Lisp_Object root_window;
1039 #endif
1041 /* Treat tool bars like menu bars. */
1042 if (FRAME_MINIBUF_ONLY_P (f))
1043 return;
1045 /* Use VALUE only if an int >= 0. */
1046 if (RANGED_INTEGERP (0, value, INT_MAX))
1047 nlines = XFASTINT (value);
1048 else
1049 nlines = 0;
1051 #ifdef USE_GTK
1053 FRAME_TOOL_BAR_LINES (f) = 0;
1054 if (nlines)
1056 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1057 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1058 /* Make sure next redisplay shows the tool bar. */
1059 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1060 update_frame_tool_bar (f);
1062 else
1064 if (FRAME_EXTERNAL_TOOL_BAR (f))
1065 free_frame_tool_bar (f);
1066 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1069 #else /* !USE_GTK */
1071 /* Make sure we redisplay all windows in this frame. */
1072 ++windows_or_buffers_changed;
1074 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1076 /* Don't resize the tool-bar to more than we have room for. */
1077 root_window = FRAME_ROOT_WINDOW (f);
1078 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1079 if (root_height - delta < 1)
1081 delta = root_height - 1;
1082 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1085 FRAME_TOOL_BAR_LINES (f) = nlines;
1086 resize_frame_windows (f, FRAME_LINES (f), 0);
1087 adjust_frame_glyphs (f);
1089 /* We also have to make sure that the internal border at the top of
1090 the frame, below the menu bar or tool bar, is redrawn when the
1091 tool bar disappears. This is so because the internal border is
1092 below the tool bar if one is displayed, but is below the menu bar
1093 if there isn't a tool bar. The tool bar draws into the area
1094 below the menu bar. */
1095 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1097 clear_frame (f);
1098 clear_current_matrices (f);
1101 /* If the tool bar gets smaller, the internal border below it
1102 has to be cleared. It was formerly part of the display
1103 of the larger tool bar, and updating windows won't clear it. */
1104 if (delta < 0)
1106 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1107 int width = FRAME_PIXEL_WIDTH (f);
1108 int y = (FRAME_MENU_BAR_LINES (f) + nlines) * FRAME_LINE_HEIGHT (f);
1110 /* height can be zero here. */
1111 if (height > 0 && width > 0)
1113 block_input ();
1114 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1115 0, y, width, height);
1116 unblock_input ();
1119 if (WINDOWP (f->tool_bar_window))
1120 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1123 run_window_configuration_change_hook (f);
1124 #endif /* USE_GTK */
1128 /* Set the foreground color for scroll bars on frame F to VALUE.
1129 VALUE should be a string, a color name. If it isn't a string or
1130 isn't a valid color name, do nothing. OLDVAL is the old value of
1131 the frame parameter. */
1133 static void
1134 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1136 unsigned long pixel;
1138 if (STRINGP (value))
1139 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1140 else
1141 pixel = -1;
1143 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1144 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1146 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1147 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1149 /* Remove all scroll bars because they have wrong colors. */
1150 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1151 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1152 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1153 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1155 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1156 redraw_frame (f);
1161 /* Set the background color for scroll bars on frame F to VALUE VALUE
1162 should be a string, a color name. If it isn't a string or isn't a
1163 valid color name, do nothing. OLDVAL is the old value of the frame
1164 parameter. */
1166 static void
1167 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1169 unsigned long pixel;
1171 if (STRINGP (value))
1172 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1173 else
1174 pixel = -1;
1176 if (f->output_data.x->scroll_bar_background_pixel != -1)
1177 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1179 #ifdef USE_TOOLKIT_SCROLL_BARS
1180 /* Scrollbar shadow colors. */
1181 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1183 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1184 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1186 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1188 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1189 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1191 #endif /* USE_TOOLKIT_SCROLL_BARS */
1193 f->output_data.x->scroll_bar_background_pixel = pixel;
1194 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1196 /* Remove all scroll bars because they have wrong colors. */
1197 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1198 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1199 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1200 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1202 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1203 redraw_frame (f);
1208 /* Encode Lisp string STRING as a text in a format appropriate for
1209 XICCC (X Inter Client Communication Conventions).
1211 This can call Lisp code, so callers must GCPRO.
1213 If STRING contains only ASCII characters, do no conversion and
1214 return the string data of STRING. Otherwise, encode the text by
1215 CODING_SYSTEM, and return a newly allocated memory area which
1216 should be freed by `xfree' by a caller.
1218 SELECTIONP non-zero means the string is being encoded for an X
1219 selection, so it is safe to run pre-write conversions (which
1220 may run Lisp code).
1222 Store the byte length of resulting text in *TEXT_BYTES.
1224 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1225 which means that the `encoding' of the result can be `STRING'.
1226 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1227 the result should be `COMPOUND_TEXT'. */
1229 static unsigned char *
1230 x_encode_text (Lisp_Object string, Lisp_Object coding_system, int selectionp,
1231 ptrdiff_t *text_bytes, int *stringp, int *freep)
1233 int result = string_xstring_p (string);
1234 struct coding_system coding;
1236 if (result == 0)
1238 /* No multibyte character in OBJ. We need not encode it. */
1239 *text_bytes = SBYTES (string);
1240 *stringp = 1;
1241 *freep = 0;
1242 return SDATA (string);
1245 setup_coding_system (coding_system, &coding);
1246 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1247 /* We suppress producing escape sequences for composition. */
1248 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1249 coding.destination = xnmalloc (SCHARS (string), 2);
1250 coding.dst_bytes = SCHARS (string) * 2;
1251 encode_coding_object (&coding, string, 0, 0,
1252 SCHARS (string), SBYTES (string), Qnil);
1253 *text_bytes = coding.produced;
1254 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1255 *freep = 1;
1256 return coding.destination;
1260 /* Set the WM name to NAME for frame F. Also set the icon name.
1261 If the frame already has an icon name, use that, otherwise set the
1262 icon name to NAME. */
1264 static void
1265 x_set_name_internal (struct frame *f, Lisp_Object name)
1267 if (FRAME_X_WINDOW (f))
1269 block_input ();
1271 XTextProperty text, icon;
1272 ptrdiff_t bytes;
1273 int stringp;
1274 int do_free_icon_value = 0, do_free_text_value = 0;
1275 Lisp_Object coding_system;
1276 Lisp_Object encoded_name;
1277 Lisp_Object encoded_icon_name;
1278 struct gcpro gcpro1;
1280 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1281 we use it before x_encode_text that may return string data. */
1282 GCPRO1 (name);
1283 encoded_name = ENCODE_UTF_8 (name);
1284 UNGCPRO;
1286 coding_system = Qcompound_text;
1287 /* Note: Encoding strategy
1289 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1290 text.encoding. But, there are non-internationalized window
1291 managers which don't support that encoding. So, if NAME
1292 contains only ASCII and 8859-1 characters, encode it by
1293 iso-latin-1, and use "STRING" in text.encoding hoping that
1294 such window managers at least analyze this format correctly,
1295 i.e. treat 8-bit bytes as 8859-1 characters.
1297 We may also be able to use "UTF8_STRING" in text.encoding
1298 in the future which can encode all Unicode characters.
1299 But, for the moment, there's no way to know that the
1300 current window manager supports it or not.
1302 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1303 properties. Per the EWMH specification, those two properties
1304 are always UTF8_STRING. This matches what gtk_window_set_title()
1305 does in the USE_GTK case. */
1306 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1307 &do_free_text_value);
1308 text.encoding = (stringp ? XA_STRING
1309 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1310 text.format = 8;
1311 text.nitems = bytes;
1312 if (text.nitems != bytes)
1313 error ("Window name too large");
1315 if (!STRINGP (f->icon_name))
1317 icon = text;
1318 encoded_icon_name = encoded_name;
1320 else
1322 /* See the above comment "Note: Encoding strategy". */
1323 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1324 &bytes, &stringp, &do_free_icon_value);
1325 icon.encoding = (stringp ? XA_STRING
1326 : FRAME_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1327 icon.format = 8;
1328 icon.nitems = bytes;
1329 if (icon.nitems != bytes)
1330 error ("Icon name too large");
1332 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1335 #ifdef USE_GTK
1336 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1337 SSDATA (encoded_name));
1338 #else /* not USE_GTK */
1339 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1340 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1341 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_name,
1342 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1343 8, PropModeReplace,
1344 SDATA (encoded_name),
1345 SBYTES (encoded_name));
1346 #endif /* not USE_GTK */
1348 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1349 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1350 FRAME_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1351 FRAME_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1352 8, PropModeReplace,
1353 SDATA (encoded_icon_name),
1354 SBYTES (encoded_icon_name));
1356 if (do_free_icon_value)
1357 xfree (icon.value);
1358 if (do_free_text_value)
1359 xfree (text.value);
1361 unblock_input ();
1365 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1366 x_id_name.
1368 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1369 name; if NAME is a string, set F's name to NAME and set
1370 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1372 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1373 suggesting a new name, which lisp code should override; if
1374 F->explicit_name is set, ignore the new name; otherwise, set it. */
1376 static void
1377 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1379 /* Make sure that requests from lisp code override requests from
1380 Emacs redisplay code. */
1381 if (explicit)
1383 /* If we're switching from explicit to implicit, we had better
1384 update the mode lines and thereby update the title. */
1385 if (f->explicit_name && NILP (name))
1386 update_mode_lines = 1;
1388 f->explicit_name = ! NILP (name);
1390 else if (f->explicit_name)
1391 return;
1393 /* If NAME is nil, set the name to the x_id_name. */
1394 if (NILP (name))
1396 /* Check for no change needed in this very common case
1397 before we do any consing. */
1398 if (!strcmp (FRAME_DISPLAY_INFO (f)->x_id_name,
1399 SSDATA (f->name)))
1400 return;
1401 name = build_string (FRAME_DISPLAY_INFO (f)->x_id_name);
1403 else
1404 CHECK_STRING (name);
1406 /* Don't change the name if it's already NAME. */
1407 if (! NILP (Fstring_equal (name, f->name)))
1408 return;
1410 fset_name (f, name);
1412 /* For setting the frame title, the title parameter should override
1413 the name parameter. */
1414 if (! NILP (f->title))
1415 name = f->title;
1417 x_set_name_internal (f, name);
1420 /* This function should be called when the user's lisp code has
1421 specified a name for the frame; the name will override any set by the
1422 redisplay code. */
1423 static void
1424 x_explicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1426 x_set_name (f, arg, 1);
1429 /* This function should be called by Emacs redisplay code to set the
1430 name; names set this way will never override names set by the user's
1431 lisp code. */
1432 void
1433 x_implicitly_set_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1435 x_set_name (f, arg, 0);
1438 /* Change the title of frame F to NAME.
1439 If NAME is nil, use the frame name as the title. */
1441 static void
1442 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1444 /* Don't change the title if it's already NAME. */
1445 if (EQ (name, f->title))
1446 return;
1448 update_mode_lines = 1;
1450 fset_title (f, name);
1452 if (NILP (name))
1453 name = f->name;
1454 else
1455 CHECK_STRING (name);
1457 x_set_name_internal (f, name);
1460 void
1461 x_set_scroll_bar_default_width (struct frame *f)
1463 int wid = FRAME_COLUMN_WIDTH (f);
1464 #ifdef USE_TOOLKIT_SCROLL_BARS
1465 #ifdef USE_GTK
1466 int minw = xg_get_default_scrollbar_width ();
1467 #else
1468 int minw = 16;
1469 #endif
1470 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1471 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (minw + wid - 1) / wid;
1472 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = minw;
1473 #else
1474 /* Make the actual width at least 14 pixels and a multiple of a
1475 character width. */
1476 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1478 /* Use all of that space (aside from required margins) for the
1479 scroll bar. */
1480 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1481 #endif
1485 /* Record in frame F the specified or default value according to ALIST
1486 of the parameter named PROP (a Lisp symbol). If no value is
1487 specified for PROP, look for an X default for XPROP on the frame
1488 named NAME. If that is not found either, use the value DEFLT. */
1490 static Lisp_Object
1491 x_default_scroll_bar_color_parameter (struct frame *f,
1492 Lisp_Object alist, Lisp_Object prop,
1493 const char *xprop, const char *xclass,
1494 int foreground_p)
1496 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
1497 Lisp_Object tem;
1499 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1500 if (EQ (tem, Qunbound))
1502 #ifdef USE_TOOLKIT_SCROLL_BARS
1504 /* See if an X resource for the scroll bar color has been
1505 specified. */
1506 tem = display_x_get_resource (dpyinfo,
1507 build_string (foreground_p
1508 ? "foreground"
1509 : "background"),
1510 empty_unibyte_string,
1511 build_string ("verticalScrollBar"),
1512 empty_unibyte_string);
1513 if (!STRINGP (tem))
1515 /* If nothing has been specified, scroll bars will use a
1516 toolkit-dependent default. Because these defaults are
1517 difficult to get at without actually creating a scroll
1518 bar, use nil to indicate that no color has been
1519 specified. */
1520 tem = Qnil;
1523 #else /* not USE_TOOLKIT_SCROLL_BARS */
1525 tem = Qnil;
1527 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1530 x_set_frame_parameters (f, list1 (Fcons (prop, tem)));
1531 return tem;
1537 #ifdef USE_X_TOOLKIT
1539 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1540 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1541 already be present because of the toolkit (Motif adds some of them,
1542 for example, but Xt doesn't). */
1544 static void
1545 hack_wm_protocols (struct frame *f, Widget widget)
1547 Display *dpy = XtDisplay (widget);
1548 Window w = XtWindow (widget);
1549 int need_delete = 1;
1550 int need_focus = 1;
1551 int need_save = 1;
1553 block_input ();
1555 Atom type;
1556 unsigned char *catoms;
1557 int format = 0;
1558 unsigned long nitems = 0;
1559 unsigned long bytes_after;
1561 if ((XGetWindowProperty (dpy, w,
1562 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1563 (long)0, (long)100, False, XA_ATOM,
1564 &type, &format, &nitems, &bytes_after,
1565 &catoms)
1566 == Success)
1567 && format == 32 && type == XA_ATOM)
1569 Atom *atoms = (Atom *) catoms;
1570 while (nitems > 0)
1572 nitems--;
1573 if (atoms[nitems]
1574 == FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1575 need_delete = 0;
1576 else if (atoms[nitems]
1577 == FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1578 need_focus = 0;
1579 else if (atoms[nitems]
1580 == FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1581 need_save = 0;
1584 if (catoms)
1585 XFree (catoms);
1588 Atom props [10];
1589 int count = 0;
1590 if (need_delete)
1591 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1592 if (need_focus)
1593 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1594 if (need_save)
1595 props[count++] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1596 if (count)
1597 XChangeProperty (dpy, w, FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
1598 XA_ATOM, 32, PropModeAppend,
1599 (unsigned char *) props, count);
1601 unblock_input ();
1603 #endif
1607 /* Support routines for XIC (X Input Context). */
1609 #ifdef HAVE_X_I18N
1611 static XFontSet xic_create_xfontset (struct frame *);
1612 static XIMStyle best_xim_style (XIMStyles *, XIMStyles *);
1615 /* Supported XIM styles, ordered by preference. */
1617 static XIMStyle supported_xim_styles[] =
1619 XIMPreeditPosition | XIMStatusArea,
1620 XIMPreeditPosition | XIMStatusNothing,
1621 XIMPreeditPosition | XIMStatusNone,
1622 XIMPreeditNothing | XIMStatusArea,
1623 XIMPreeditNothing | XIMStatusNothing,
1624 XIMPreeditNothing | XIMStatusNone,
1625 XIMPreeditNone | XIMStatusArea,
1626 XIMPreeditNone | XIMStatusNothing,
1627 XIMPreeditNone | XIMStatusNone,
1632 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1633 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1635 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1637 /* Create an Xt fontset spec from the name of a base font.
1638 If `motif' is True use the Motif syntax. */
1639 char *
1640 xic_create_fontsetname (const char *base_fontname, int motif)
1642 const char *sep = motif ? ";" : ",";
1643 char *fontsetname;
1645 /* Make a fontset name from the base font name. */
1646 if (xic_default_fontset == base_fontname)
1648 /* There is no base font name, use the default. */
1649 fontsetname = xmalloc (strlen (base_fontname) + 2);
1650 strcpy (fontsetname, base_fontname);
1652 else
1654 /* Make a fontset name from the base font name.
1655 The font set will be made of the following elements:
1656 - the base font.
1657 - the base font where the charset spec is replaced by -*-*.
1658 - the same but with the family also replaced with -*-*-. */
1659 const char *p = base_fontname;
1660 ptrdiff_t i;
1662 for (i = 0; *p; p++)
1663 if (*p == '-') i++;
1664 if (i != 14)
1666 /* As the font name doesn't conform to XLFD, we can't
1667 modify it to generalize it to allcs and allfamilies.
1668 Use the specified font plus the default. */
1669 fontsetname = xmalloc (strlen (base_fontname)
1670 + strlen (xic_default_fontset) + 3);
1671 strcpy (fontsetname, base_fontname);
1672 strcat (fontsetname, sep);
1673 strcat (fontsetname, xic_default_fontset);
1675 else
1677 ptrdiff_t len;
1678 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1679 char *font_allcs = NULL;
1680 char *font_allfamilies = NULL;
1681 char *font_all = NULL;
1682 const char *allcs = "*-*-*-*-*-*-*";
1683 const char *allfamilies = "-*-*-";
1684 const char *all = "*-*-*-*-";
1685 char *base;
1687 for (i = 0, p = base_fontname; i < 8; p++)
1689 if (*p == '-')
1691 i++;
1692 if (i == 3)
1693 p1 = p + 1;
1694 else if (i == 7)
1695 p2 = p + 1;
1696 else if (i == 6)
1697 p3 = p + 1;
1700 /* If base_fontname specifies ADSTYLE, make it a
1701 wildcard. */
1702 if (*p3 != '*')
1704 ptrdiff_t diff = (p2 - p3) - 2;
1706 base = alloca (strlen (base_fontname) + 1);
1707 memcpy (base, base_fontname, p3 - base_fontname);
1708 base[p3 - base_fontname] = '*';
1709 base[(p3 - base_fontname) + 1] = '-';
1710 strcpy (base + (p3 - base_fontname) + 2, p2);
1711 p = base + (p - base_fontname) - diff;
1712 p1 = base + (p1 - base_fontname);
1713 p2 = base + (p2 - base_fontname) - diff;
1714 base_fontname = base;
1717 /* Build the font spec that matches all charsets. */
1718 len = p - base_fontname + strlen (allcs) + 1;
1719 font_allcs = alloca (len);
1720 memcpy (font_allcs, base_fontname, p - base_fontname);
1721 strcat (font_allcs, allcs);
1723 /* Build the font spec that matches all families and
1724 add-styles. */
1725 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1726 font_allfamilies = alloca (len);
1727 strcpy (font_allfamilies, allfamilies);
1728 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1729 strcat (font_allfamilies, allcs);
1731 /* Build the font spec that matches all. */
1732 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1733 font_all = alloca (len);
1734 strcpy (font_all, allfamilies);
1735 strcat (font_all, all);
1736 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1737 strcat (font_all, allcs);
1739 /* Build the actual font set name. */
1740 len = strlen (base_fontname) + strlen (font_allcs)
1741 + strlen (font_allfamilies) + strlen (font_all) + 5;
1742 fontsetname = xmalloc (len);
1743 strcpy (fontsetname, base_fontname);
1744 strcat (fontsetname, sep);
1745 strcat (fontsetname, font_allcs);
1746 strcat (fontsetname, sep);
1747 strcat (fontsetname, font_allfamilies);
1748 strcat (fontsetname, sep);
1749 strcat (fontsetname, font_all);
1752 if (motif)
1753 strcat (fontsetname, ":");
1754 return fontsetname;
1756 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1758 #ifdef DEBUG_XIC_FONTSET
1759 static void
1760 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1761 int missing_count)
1763 if (xfs)
1764 fprintf (stderr, "XIC Fontset created: %s\n", name);
1765 else
1767 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1768 while (missing_count-- > 0)
1770 fprintf (stderr, " missing: %s\n", *missing_list);
1771 missing_list++;
1776 #endif
1778 static XFontSet
1779 xic_create_xfontset (struct frame *f)
1781 XFontSet xfs = NULL;
1782 struct font *font = FRAME_FONT (f);
1783 int pixel_size = font->pixel_size;
1784 Lisp_Object rest, frame;
1786 /* See if there is another frame already using same fontset. */
1787 FOR_EACH_FRAME (rest, frame)
1789 struct frame *cf = XFRAME (frame);
1791 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1792 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1793 && FRAME_FONT (f)
1794 && FRAME_FONT (f)->pixel_size == pixel_size)
1796 xfs = FRAME_XIC_FONTSET (cf);
1797 break;
1801 if (! xfs)
1803 char buf[256];
1804 char **missing_list;
1805 int missing_count;
1806 char *def_string;
1807 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1809 sprintf (buf, xlfd_format, pixel_size);
1810 missing_list = NULL;
1811 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1812 &missing_list, &missing_count, &def_string);
1813 #ifdef DEBUG_XIC_FONTSET
1814 print_fontset_result (xfs, buf, missing_list, missing_count);
1815 #endif
1816 if (missing_list)
1817 XFreeStringList (missing_list);
1818 if (! xfs)
1820 /* List of pixel sizes most likely available. Find one that
1821 is closest to pixel_size. */
1822 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
1823 int *smaller, *larger;
1825 for (smaller = sizes; smaller[1]; smaller++)
1826 if (smaller[1] >= pixel_size)
1827 break;
1828 larger = smaller + 1;
1829 if (*larger == pixel_size)
1830 larger++;
1831 while (*smaller || *larger)
1833 int this_size;
1835 if (! *larger)
1836 this_size = *smaller--;
1837 else if (! *smaller)
1838 this_size = *larger++;
1839 else if (pixel_size - *smaller < *larger - pixel_size)
1840 this_size = *smaller--;
1841 else
1842 this_size = *larger++;
1843 sprintf (buf, xlfd_format, this_size);
1844 missing_list = NULL;
1845 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1846 &missing_list, &missing_count, &def_string);
1847 #ifdef DEBUG_XIC_FONTSET
1848 print_fontset_result (xfs, buf, missing_list, missing_count);
1849 #endif
1850 if (missing_list)
1851 XFreeStringList (missing_list);
1852 if (xfs)
1853 break;
1856 if (! xfs)
1858 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
1860 missing_list = NULL;
1861 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
1862 &missing_list, &missing_count, &def_string);
1863 #ifdef DEBUG_XIC_FONTSET
1864 print_fontset_result (xfs, last_resort, missing_list, missing_count);
1865 #endif
1866 if (missing_list)
1867 XFreeStringList (missing_list);
1872 return xfs;
1875 /* Free the X fontset of frame F if it is the last frame using it. */
1877 void
1878 xic_free_xfontset (struct frame *f)
1880 Lisp_Object rest, frame;
1881 bool shared_p = 0;
1883 if (!FRAME_XIC_FONTSET (f))
1884 return;
1886 /* See if there is another frame sharing the same fontset. */
1887 FOR_EACH_FRAME (rest, frame)
1889 struct frame *cf = XFRAME (frame);
1890 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1891 && FRAME_DISPLAY_INFO (cf) == FRAME_DISPLAY_INFO (f)
1892 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
1894 shared_p = 1;
1895 break;
1899 if (!shared_p)
1900 /* The fontset is not used anymore. It is safe to free it. */
1901 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
1903 FRAME_XIC_FONTSET (f) = NULL;
1907 /* Value is the best input style, given user preferences USER (already
1908 checked to be supported by Emacs), and styles supported by the
1909 input method XIM. */
1911 static XIMStyle
1912 best_xim_style (XIMStyles *user, XIMStyles *xim)
1914 int i, j;
1916 for (i = 0; i < user->count_styles; ++i)
1917 for (j = 0; j < xim->count_styles; ++j)
1918 if (user->supported_styles[i] == xim->supported_styles[j])
1919 return user->supported_styles[i];
1921 /* Return the default style. */
1922 return XIMPreeditNothing | XIMStatusNothing;
1925 /* Create XIC for frame F. */
1927 static XIMStyle xic_style;
1929 void
1930 create_frame_xic (struct frame *f)
1932 XIM xim;
1933 XIC xic = NULL;
1934 XFontSet xfs = NULL;
1936 if (FRAME_XIC (f))
1937 return;
1939 /* Create X fontset. */
1940 xfs = xic_create_xfontset (f);
1941 xim = FRAME_X_XIM (f);
1942 if (xim)
1944 XRectangle s_area;
1945 XPoint spot;
1946 XVaNestedList preedit_attr;
1947 XVaNestedList status_attr;
1949 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
1950 spot.x = 0; spot.y = 1;
1952 /* Determine XIC style. */
1953 if (xic_style == 0)
1955 XIMStyles supported_list;
1956 supported_list.count_styles = (sizeof supported_xim_styles
1957 / sizeof supported_xim_styles[0]);
1958 supported_list.supported_styles = supported_xim_styles;
1959 xic_style = best_xim_style (&supported_list,
1960 FRAME_X_XIM_STYLES (f));
1963 preedit_attr = XVaCreateNestedList (0,
1964 XNFontSet, xfs,
1965 XNForeground,
1966 FRAME_FOREGROUND_PIXEL (f),
1967 XNBackground,
1968 FRAME_BACKGROUND_PIXEL (f),
1969 (xic_style & XIMPreeditPosition
1970 ? XNSpotLocation
1971 : NULL),
1972 &spot,
1973 NULL);
1974 status_attr = XVaCreateNestedList (0,
1975 XNArea,
1976 &s_area,
1977 XNFontSet,
1978 xfs,
1979 XNForeground,
1980 FRAME_FOREGROUND_PIXEL (f),
1981 XNBackground,
1982 FRAME_BACKGROUND_PIXEL (f),
1983 NULL);
1985 xic = XCreateIC (xim,
1986 XNInputStyle, xic_style,
1987 XNClientWindow, FRAME_X_WINDOW (f),
1988 XNFocusWindow, FRAME_X_WINDOW (f),
1989 XNStatusAttributes, status_attr,
1990 XNPreeditAttributes, preedit_attr,
1991 NULL);
1992 XFree (preedit_attr);
1993 XFree (status_attr);
1996 FRAME_XIC (f) = xic;
1997 FRAME_XIC_STYLE (f) = xic_style;
1998 FRAME_XIC_FONTSET (f) = xfs;
2002 /* Destroy XIC and free XIC fontset of frame F, if any. */
2004 void
2005 free_frame_xic (struct frame *f)
2007 if (FRAME_XIC (f) == NULL)
2008 return;
2010 XDestroyIC (FRAME_XIC (f));
2011 xic_free_xfontset (f);
2013 FRAME_XIC (f) = NULL;
2017 /* Place preedit area for XIC of window W's frame to specified
2018 pixel position X/Y. X and Y are relative to window W. */
2020 void
2021 xic_set_preeditarea (struct window *w, int x, int y)
2023 struct frame *f = XFRAME (w->frame);
2024 XVaNestedList attr;
2025 XPoint spot;
2027 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2028 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2029 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2030 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2031 XFree (attr);
2035 /* Place status area for XIC in bottom right corner of frame F.. */
2037 void
2038 xic_set_statusarea (struct frame *f)
2040 XIC xic = FRAME_XIC (f);
2041 XVaNestedList attr;
2042 XRectangle area;
2043 XRectangle *needed;
2045 /* Negotiate geometry of status area. If input method has existing
2046 status area, use its current size. */
2047 area.x = area.y = area.width = area.height = 0;
2048 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2049 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2050 XFree (attr);
2052 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2053 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2054 XFree (attr);
2056 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2058 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2059 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2060 XFree (attr);
2063 area.width = needed->width;
2064 area.height = needed->height;
2065 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2066 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2067 - FRAME_MENUBAR_HEIGHT (f)
2068 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2069 - FRAME_INTERNAL_BORDER_WIDTH (f));
2070 XFree (needed);
2072 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2073 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2074 XFree (attr);
2078 /* Set X fontset for XIC of frame F, using base font name
2079 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2081 void
2082 xic_set_xfontset (struct frame *f, const char *base_fontname)
2084 XVaNestedList attr;
2085 XFontSet xfs;
2087 xic_free_xfontset (f);
2089 xfs = xic_create_xfontset (f);
2091 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2092 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2093 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2094 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2095 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2096 XFree (attr);
2098 FRAME_XIC_FONTSET (f) = xfs;
2101 #endif /* HAVE_X_I18N */
2105 #ifdef USE_X_TOOLKIT
2107 /* Create and set up the X widget for frame F. */
2109 static void
2110 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2112 XClassHint class_hints;
2113 XSetWindowAttributes attributes;
2114 unsigned long attribute_mask;
2115 Widget shell_widget;
2116 Widget pane_widget;
2117 Widget frame_widget;
2118 Arg al [25];
2119 int ac;
2121 block_input ();
2123 /* Use the resource name as the top-level widget name
2124 for looking up resources. Make a non-Lisp copy
2125 for the window manager, so GC relocation won't bother it.
2127 Elsewhere we specify the window name for the window manager. */
2128 f->namebuf = xstrdup (SSDATA (Vx_resource_name));
2130 ac = 0;
2131 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2132 XtSetArg (al[ac], XtNinput, 1); ac++;
2133 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2134 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2135 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2136 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2137 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2138 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2139 applicationShellWidgetClass,
2140 FRAME_X_DISPLAY (f), al, ac);
2142 f->output_data.x->widget = shell_widget;
2143 /* maybe_set_screen_title_format (shell_widget); */
2145 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2146 NULL, shell_widget, False,
2147 NULL, NULL, NULL, NULL);
2149 ac = 0;
2150 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2151 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2152 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2153 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2154 XtSetValues (pane_widget, al, ac);
2155 f->output_data.x->column_widget = pane_widget;
2157 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2158 the emacs screen when changing menubar. This reduces flickering. */
2160 ac = 0;
2161 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2162 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2163 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2164 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2165 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2166 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2167 XtSetArg (al[ac], XtNdepth, FRAME_DISPLAY_INFO (f)->n_planes); ac++;
2168 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2169 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2170 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2171 al, ac);
2173 f->output_data.x->edit_widget = frame_widget;
2175 XtManageChild (frame_widget);
2177 /* Do some needed geometry management. */
2179 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
2180 Arg gal[10];
2181 int gac = 0;
2182 int extra_borders = 0;
2183 int menubar_size
2184 = (f->output_data.x->menubar_widget
2185 ? (f->output_data.x->menubar_widget->core.height
2186 + f->output_data.x->menubar_widget->core.border_width)
2187 : 0);
2189 #if 0 /* Experimentally, we now get the right results
2190 for -geometry -0-0 without this. 24 Aug 96, rms. */
2191 if (FRAME_EXTERNAL_MENU_BAR (f))
2193 Dimension ibw = 0;
2194 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2195 menubar_size += ibw;
2197 #endif
2199 f->output_data.x->menubar_height = menubar_size;
2201 #ifndef USE_LUCID
2202 /* Motif seems to need this amount added to the sizes
2203 specified for the shell widget. The Athena/Lucid widgets don't.
2204 Both conclusions reached experimentally. -- rms. */
2205 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2206 &extra_borders, NULL);
2207 extra_borders *= 2;
2208 #endif
2210 /* Convert our geometry parameters into a geometry string
2211 and specify it.
2212 Note that we do not specify here whether the position
2213 is a user-specified or program-specified one.
2214 We pass that information later, in x_wm_set_size_hints. */
2216 int left = f->left_pos;
2217 int xneg = window_prompting & XNegative;
2218 int top = f->top_pos;
2219 int yneg = window_prompting & YNegative;
2220 if (xneg)
2221 left = -left;
2222 if (yneg)
2223 top = -top;
2225 if (window_prompting & USPosition)
2226 sprintf (shell_position, "=%dx%d%c%d%c%d",
2227 FRAME_PIXEL_WIDTH (f) + extra_borders,
2228 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2229 (xneg ? '-' : '+'), left,
2230 (yneg ? '-' : '+'), top);
2231 else
2233 sprintf (shell_position, "=%dx%d",
2234 FRAME_PIXEL_WIDTH (f) + extra_borders,
2235 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2237 /* Setting x and y when the position is not specified in
2238 the geometry string will set program position in the WM hints.
2239 If Emacs had just one program position, we could set it in
2240 fallback resources, but since each make-frame call can specify
2241 different program positions, this is easier. */
2242 XtSetArg (gal[gac], XtNx, left); gac++;
2243 XtSetArg (gal[gac], XtNy, top); gac++;
2247 /* We don't free this because we don't know whether
2248 it is safe to free it while the frame exists.
2249 It isn't worth the trouble of arranging to free it
2250 when the frame is deleted. */
2251 tem = xstrdup (shell_position);
2252 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2253 XtSetValues (shell_widget, gal, gac);
2256 XtManageChild (pane_widget);
2257 XtRealizeWidget (shell_widget);
2259 if (FRAME_X_EMBEDDED_P (f))
2260 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2261 f->output_data.x->parent_desc, 0, 0);
2263 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2265 validate_x_resource_name ();
2267 class_hints.res_name = SSDATA (Vx_resource_name);
2268 class_hints.res_class = SSDATA (Vx_resource_class);
2269 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2271 #ifdef HAVE_X_I18N
2272 FRAME_XIC (f) = NULL;
2273 if (use_xim)
2274 create_frame_xic (f);
2275 #endif
2277 f->output_data.x->wm_hints.input = True;
2278 f->output_data.x->wm_hints.flags |= InputHint;
2279 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2280 &f->output_data.x->wm_hints);
2282 hack_wm_protocols (f, shell_widget);
2284 #ifdef HACK_EDITRES
2285 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2286 #endif
2288 /* Do a stupid property change to force the server to generate a
2289 PropertyNotify event so that the event_stream server timestamp will
2290 be initialized to something relevant to the time we created the window.
2292 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2293 FRAME_DISPLAY_INFO (f)->Xatom_wm_protocols,
2294 XA_ATOM, 32, PropModeAppend, NULL, 0);
2296 /* Make all the standard events reach the Emacs frame. */
2297 attributes.event_mask = STANDARD_EVENT_SET;
2299 #ifdef HAVE_X_I18N
2300 if (FRAME_XIC (f))
2302 /* XIM server might require some X events. */
2303 unsigned long fevent = NoEventMask;
2304 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2305 attributes.event_mask |= fevent;
2307 #endif /* HAVE_X_I18N */
2309 attribute_mask = CWEventMask;
2310 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2311 attribute_mask, &attributes);
2313 XtMapWidget (frame_widget);
2315 /* x_set_name normally ignores requests to set the name if the
2316 requested name is the same as the current name. This is the one
2317 place where that assumption isn't correct; f->name is set, but
2318 the X server hasn't been told. */
2320 Lisp_Object name;
2321 int explicit = f->explicit_name;
2323 f->explicit_name = 0;
2324 name = f->name;
2325 fset_name (f, Qnil);
2326 x_set_name (f, name, explicit);
2329 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2330 f->output_data.x->current_cursor
2331 = f->output_data.x->text_cursor);
2333 unblock_input ();
2335 /* This is a no-op, except under Motif. Make sure main areas are
2336 set to something reasonable, in case we get an error later. */
2337 lw_set_main_areas (pane_widget, 0, frame_widget);
2340 #else /* not USE_X_TOOLKIT */
2341 #ifdef USE_GTK
2342 static void
2343 x_window (struct frame *f)
2345 if (! xg_create_frame_widgets (f))
2346 error ("Unable to create window");
2348 #ifdef HAVE_X_I18N
2349 FRAME_XIC (f) = NULL;
2350 if (use_xim)
2352 block_input ();
2353 create_frame_xic (f);
2354 if (FRAME_XIC (f))
2356 /* XIM server might require some X events. */
2357 unsigned long fevent = NoEventMask;
2358 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2360 if (fevent != NoEventMask)
2362 XSetWindowAttributes attributes;
2363 XWindowAttributes wattr;
2364 unsigned long attribute_mask;
2366 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2367 &wattr);
2368 attributes.event_mask = wattr.your_event_mask | fevent;
2369 attribute_mask = CWEventMask;
2370 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2371 attribute_mask, &attributes);
2374 unblock_input ();
2376 #endif
2379 #else /*! USE_GTK */
2380 /* Create and set up the X window for frame F. */
2382 static void
2383 x_window (struct frame *f)
2385 XClassHint class_hints;
2386 XSetWindowAttributes attributes;
2387 unsigned long attribute_mask;
2389 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2390 attributes.border_pixel = f->output_data.x->border_pixel;
2391 attributes.bit_gravity = StaticGravity;
2392 attributes.backing_store = NotUseful;
2393 attributes.save_under = True;
2394 attributes.event_mask = STANDARD_EVENT_SET;
2395 attributes.colormap = FRAME_X_COLORMAP (f);
2396 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2397 | CWColormap);
2399 block_input ();
2400 FRAME_X_WINDOW (f)
2401 = XCreateWindow (FRAME_X_DISPLAY (f),
2402 f->output_data.x->parent_desc,
2403 f->left_pos,
2404 f->top_pos,
2405 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2406 f->border_width,
2407 CopyFromParent, /* depth */
2408 InputOutput, /* class */
2409 FRAME_X_VISUAL (f),
2410 attribute_mask, &attributes);
2412 #ifdef HAVE_X_I18N
2413 if (use_xim)
2415 create_frame_xic (f);
2416 if (FRAME_XIC (f))
2418 /* XIM server might require some X events. */
2419 unsigned long fevent = NoEventMask;
2420 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2421 attributes.event_mask |= fevent;
2422 attribute_mask = CWEventMask;
2423 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2424 attribute_mask, &attributes);
2427 #endif /* HAVE_X_I18N */
2429 validate_x_resource_name ();
2431 class_hints.res_name = SSDATA (Vx_resource_name);
2432 class_hints.res_class = SSDATA (Vx_resource_class);
2433 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2435 /* The menubar is part of the ordinary display;
2436 it does not count in addition to the height of the window. */
2437 f->output_data.x->menubar_height = 0;
2439 /* This indicates that we use the "Passive Input" input model.
2440 Unless we do this, we don't get the Focus{In,Out} events that we
2441 need to draw the cursor correctly. Accursed bureaucrats.
2442 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2444 f->output_data.x->wm_hints.input = True;
2445 f->output_data.x->wm_hints.flags |= InputHint;
2446 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2447 &f->output_data.x->wm_hints);
2448 f->output_data.x->wm_hints.icon_pixmap = None;
2450 /* Request "save yourself" and "delete window" commands from wm. */
2452 Atom protocols[2];
2453 protocols[0] = FRAME_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2454 protocols[1] = FRAME_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2455 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2458 /* x_set_name normally ignores requests to set the name if the
2459 requested name is the same as the current name. This is the one
2460 place where that assumption isn't correct; f->name is set, but
2461 the X server hasn't been told. */
2463 Lisp_Object name;
2464 int explicit = f->explicit_name;
2466 f->explicit_name = 0;
2467 name = f->name;
2468 fset_name (f, Qnil);
2469 x_set_name (f, name, explicit);
2472 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2473 f->output_data.x->current_cursor
2474 = f->output_data.x->text_cursor);
2476 unblock_input ();
2478 if (FRAME_X_WINDOW (f) == 0)
2479 error ("Unable to create window");
2482 #endif /* not USE_GTK */
2483 #endif /* not USE_X_TOOLKIT */
2485 /* Verify that the icon position args for this window are valid. */
2487 static void
2488 x_icon_verify (struct frame *f, Lisp_Object parms)
2490 Lisp_Object icon_x, icon_y;
2492 /* Set the position of the icon. Note that twm groups all
2493 icons in an icon window. */
2494 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2495 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2496 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2498 CHECK_NUMBER (icon_x);
2499 CHECK_NUMBER (icon_y);
2501 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2502 error ("Both left and top icon corners of icon must be specified");
2505 /* Handle the icon stuff for this window. Perhaps later we might
2506 want an x_set_icon_position which can be called interactively as
2507 well. */
2509 static void
2510 x_icon (struct frame *f, Lisp_Object parms)
2512 Lisp_Object icon_x, icon_y;
2513 #if 0
2514 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2515 #endif
2517 /* Set the position of the icon. Note that twm groups all
2518 icons in an icon window. */
2519 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2520 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2521 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2523 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2524 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2526 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2527 error ("Both left and top icon corners of icon must be specified");
2529 block_input ();
2531 if (! EQ (icon_x, Qunbound))
2532 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2534 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2535 but x_create_frame still needs it. */
2536 /* Start up iconic or window? */
2537 x_wm_set_window_state
2538 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2539 Qicon)
2540 ? IconicState
2541 : NormalState));
2542 #endif
2544 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2545 ? f->icon_name
2546 : f->name)));
2548 unblock_input ();
2551 /* Make the GCs needed for this window, setting the
2552 background, border and mouse colors; also create the
2553 mouse cursor and the gray border tile. */
2555 static void
2556 x_make_gc (struct frame *f)
2558 XGCValues gc_values;
2560 block_input ();
2562 /* Create the GCs of this frame.
2563 Note that many default values are used. */
2565 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2566 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2567 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2568 f->output_data.x->normal_gc
2569 = XCreateGC (FRAME_X_DISPLAY (f),
2570 FRAME_X_WINDOW (f),
2571 GCLineWidth | GCForeground | GCBackground,
2572 &gc_values);
2574 /* Reverse video style. */
2575 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2576 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2577 f->output_data.x->reverse_gc
2578 = XCreateGC (FRAME_X_DISPLAY (f),
2579 FRAME_X_WINDOW (f),
2580 GCForeground | GCBackground | GCLineWidth,
2581 &gc_values);
2583 /* Cursor has cursor-color background, background-color foreground. */
2584 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2585 gc_values.background = f->output_data.x->cursor_pixel;
2586 gc_values.fill_style = FillOpaqueStippled;
2587 f->output_data.x->cursor_gc
2588 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2589 (GCForeground | GCBackground
2590 | GCFillStyle | GCLineWidth),
2591 &gc_values);
2593 /* Create the gray border tile used when the pointer is not in
2594 the frame. Since this depends on the frame's pixel values,
2595 this must be done on a per-frame basis. */
2596 f->output_data.x->border_tile
2597 = (XCreatePixmapFromBitmapData
2598 (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
2599 gray_bits, gray_width, gray_height,
2600 FRAME_FOREGROUND_PIXEL (f),
2601 FRAME_BACKGROUND_PIXEL (f),
2602 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2604 unblock_input ();
2608 /* Free what was allocated in x_make_gc. */
2610 void
2611 x_free_gcs (struct frame *f)
2613 Display *dpy = FRAME_X_DISPLAY (f);
2615 block_input ();
2617 if (f->output_data.x->normal_gc)
2619 XFreeGC (dpy, f->output_data.x->normal_gc);
2620 f->output_data.x->normal_gc = 0;
2623 if (f->output_data.x->reverse_gc)
2625 XFreeGC (dpy, f->output_data.x->reverse_gc);
2626 f->output_data.x->reverse_gc = 0;
2629 if (f->output_data.x->cursor_gc)
2631 XFreeGC (dpy, f->output_data.x->cursor_gc);
2632 f->output_data.x->cursor_gc = 0;
2635 if (f->output_data.x->border_tile)
2637 XFreePixmap (dpy, f->output_data.x->border_tile);
2638 f->output_data.x->border_tile = 0;
2641 unblock_input ();
2645 /* Handler for signals raised during x_create_frame and
2646 x_create_tip_frame. FRAME is the frame which is partially
2647 constructed. */
2649 static Lisp_Object
2650 unwind_create_frame (Lisp_Object frame)
2652 struct frame *f = XFRAME (frame);
2654 /* If frame is already dead, nothing to do. This can happen if the
2655 display is disconnected after the frame has become official, but
2656 before x_create_frame removes the unwind protect. */
2657 if (!FRAME_LIVE_P (f))
2658 return Qnil;
2660 /* If frame is ``official'', nothing to do. */
2661 if (NILP (Fmemq (frame, Vframe_list)))
2663 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2664 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2665 #endif
2667 x_free_frame_resources (f);
2668 free_glyphs (f);
2670 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2671 /* Check that reference counts are indeed correct. */
2672 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2673 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2674 #endif
2675 return Qt;
2678 return Qnil;
2681 static void
2682 do_unwind_create_frame (Lisp_Object frame)
2684 unwind_create_frame (frame);
2687 static void
2688 unwind_create_frame_1 (Lisp_Object val)
2690 inhibit_lisp_code = val;
2693 static void
2694 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2696 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (f);
2697 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2698 RES_TYPE_STRING);
2699 Lisp_Object font = Qnil;
2700 if (EQ (font_param, Qunbound))
2701 font_param = Qnil;
2703 if (NILP (font_param))
2705 /* System font should take precedence over X resources. We suggest this
2706 regardless of font-use-system-font because .emacs may not have been
2707 read yet. */
2708 const char *system_font = xsettings_get_system_font ();
2709 if (system_font)
2710 font = font_open_by_name (f, build_unibyte_string (system_font));
2713 if (NILP (font))
2714 font = !NILP (font_param) ? font_param
2715 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2717 if (! FONTP (font) && ! STRINGP (font))
2719 const char *names[]
2721 #ifdef HAVE_XFT
2722 /* This will find the normal Xft font. */
2723 "monospace-10",
2724 #endif
2725 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2726 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2727 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2728 /* This was formerly the first thing tried, but it finds
2729 too many fonts and takes too long. */
2730 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2731 /* If those didn't work, look for something which will
2732 at least work. */
2733 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2734 "fixed",
2735 NULL };
2736 int i;
2738 for (i = 0; names[i]; i++)
2740 font = font_open_by_name (f, build_unibyte_string (names[i]));
2741 if (! NILP (font))
2742 break;
2744 if (NILP (font))
2745 error ("No suitable font was found");
2747 else if (!NILP (font_param))
2749 /* Remember the explicit font parameter, so we can re-apply it after
2750 we've applied the `default' face settings. */
2751 x_set_frame_parameters (f, list1 (Fcons (Qfont_param, font_param)));
2754 /* This call will make X resources override any system font setting. */
2755 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2759 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2760 0, 1, 0,
2761 doc: /* Send the size hints for frame FRAME to the window manager.
2762 If FRAME is omitted or nil, use the selected frame.
2763 Signal error if FRAME is not an X frame. */)
2764 (Lisp_Object frame)
2766 struct frame *f = decode_window_system_frame (frame);
2768 block_input ();
2769 x_wm_set_size_hint (f, 0, 0);
2770 unblock_input ();
2771 return Qnil;
2774 static void
2775 set_machine_and_pid_properties (struct frame *f)
2777 long pid = (long) getpid ();
2779 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2780 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2781 NULL, 0, NULL, NULL, NULL);
2782 XChangeProperty (FRAME_X_DISPLAY (f),
2783 FRAME_OUTER_WINDOW (f),
2784 XInternAtom (FRAME_X_DISPLAY (f),
2785 "_NET_WM_PID",
2786 False),
2787 XA_CARDINAL, 32, PropModeReplace,
2788 (unsigned char *) &pid, 1);
2791 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2792 1, 1, 0,
2793 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2794 Return an Emacs frame object.
2795 PARMS is an alist of frame parameters.
2796 If the parameters specify that the frame should not have a minibuffer,
2797 and do not specify a specific minibuffer window to use,
2798 then `default-minibuffer-frame' must be a frame whose minibuffer can
2799 be shared by the new frame.
2801 This function is an internal primitive--use `make-frame' instead. */)
2802 (Lisp_Object parms)
2804 struct frame *f;
2805 Lisp_Object frame, tem;
2806 Lisp_Object name;
2807 int minibuffer_only = 0;
2808 long window_prompting = 0;
2809 int width, height;
2810 ptrdiff_t count = SPECPDL_INDEX ();
2811 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
2812 Lisp_Object display;
2813 struct x_display_info *dpyinfo = NULL;
2814 Lisp_Object parent;
2815 struct kboard *kb;
2817 parms = Fcopy_alist (parms);
2819 /* Use this general default value to start with
2820 until we know if this frame has a specified name. */
2821 Vx_resource_name = Vinvocation_name;
2823 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
2824 if (EQ (display, Qunbound))
2825 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
2826 if (EQ (display, Qunbound))
2827 display = Qnil;
2828 dpyinfo = check_x_display_info (display);
2829 kb = dpyinfo->terminal->kboard;
2831 if (!dpyinfo->terminal->name)
2832 error ("Terminal is not live, can't create new frames on it");
2834 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
2835 if (!STRINGP (name)
2836 && ! EQ (name, Qunbound)
2837 && ! NILP (name))
2838 error ("Invalid frame name--not a string or nil");
2840 if (STRINGP (name))
2841 Vx_resource_name = name;
2843 /* See if parent window is specified. */
2844 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
2845 if (EQ (parent, Qunbound))
2846 parent = Qnil;
2847 if (! NILP (parent))
2848 CHECK_NUMBER (parent);
2850 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
2851 /* No need to protect DISPLAY because that's not used after passing
2852 it to make_frame_without_minibuffer. */
2853 frame = Qnil;
2854 GCPRO4 (parms, parent, name, frame);
2855 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
2856 RES_TYPE_SYMBOL);
2857 if (EQ (tem, Qnone) || NILP (tem))
2858 f = make_frame_without_minibuffer (Qnil, kb, display);
2859 else if (EQ (tem, Qonly))
2861 f = make_minibuffer_frame ();
2862 minibuffer_only = 1;
2864 else if (WINDOWP (tem))
2865 f = make_frame_without_minibuffer (tem, kb, display);
2866 else
2867 f = make_frame (1);
2869 XSETFRAME (frame, f);
2871 f->terminal = dpyinfo->terminal;
2873 f->output_method = output_x_window;
2874 f->output_data.x = xzalloc (sizeof *f->output_data.x);
2875 f->output_data.x->icon_bitmap = -1;
2876 FRAME_FONTSET (f) = -1;
2877 f->output_data.x->scroll_bar_foreground_pixel = -1;
2878 f->output_data.x->scroll_bar_background_pixel = -1;
2879 #ifdef USE_TOOLKIT_SCROLL_BARS
2880 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
2881 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
2882 #endif /* USE_TOOLKIT_SCROLL_BARS */
2884 fset_icon_name (f,
2885 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
2886 RES_TYPE_STRING));
2887 if (! STRINGP (f->icon_name))
2888 fset_icon_name (f, Qnil);
2890 FRAME_DISPLAY_INFO (f) = dpyinfo;
2892 /* With FRAME_DISPLAY_INFO set up, this unwind-protect is safe. */
2893 record_unwind_protect (do_unwind_create_frame, frame);
2895 /* These colors will be set anyway later, but it's important
2896 to get the color reference counts right, so initialize them! */
2898 Lisp_Object black;
2899 struct gcpro gcpro1;
2901 /* Function x_decode_color can signal an error. Make
2902 sure to initialize color slots so that we won't try
2903 to free colors we haven't allocated. */
2904 FRAME_FOREGROUND_PIXEL (f) = -1;
2905 FRAME_BACKGROUND_PIXEL (f) = -1;
2906 f->output_data.x->cursor_pixel = -1;
2907 f->output_data.x->cursor_foreground_pixel = -1;
2908 f->output_data.x->border_pixel = -1;
2909 f->output_data.x->mouse_pixel = -1;
2911 black = build_string ("black");
2912 GCPRO1 (black);
2913 FRAME_FOREGROUND_PIXEL (f)
2914 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2915 FRAME_BACKGROUND_PIXEL (f)
2916 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2917 f->output_data.x->cursor_pixel
2918 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2919 f->output_data.x->cursor_foreground_pixel
2920 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2921 f->output_data.x->border_pixel
2922 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2923 f->output_data.x->mouse_pixel
2924 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
2925 UNGCPRO;
2928 /* Specify the parent under which to make this X window. */
2930 if (!NILP (parent))
2932 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
2933 f->output_data.x->explicit_parent = 1;
2935 else
2937 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
2938 f->output_data.x->explicit_parent = 0;
2941 /* Set the name; the functions to which we pass f expect the name to
2942 be set. */
2943 if (EQ (name, Qunbound) || NILP (name))
2945 fset_name (f, build_string (dpyinfo->x_id_name));
2946 f->explicit_name = 0;
2948 else
2950 fset_name (f, name);
2951 f->explicit_name = 1;
2952 /* use the frame's title when getting resources for this frame. */
2953 specbind (Qx_resource_name, name);
2956 #ifdef HAVE_FREETYPE
2957 #ifdef HAVE_XFT
2958 register_font_driver (&xftfont_driver, f);
2959 #else /* not HAVE_XFT */
2960 register_font_driver (&ftxfont_driver, f);
2961 #endif /* not HAVE_XFT */
2962 #endif /* HAVE_FREETYPE */
2963 register_font_driver (&xfont_driver, f);
2965 x_default_parameter (f, parms, Qfont_backend, Qnil,
2966 "fontBackend", "FontBackend", RES_TYPE_STRING);
2968 /* Extract the window parameters from the supplied values
2969 that are needed to determine window geometry. */
2970 x_default_font_parameter (f, parms);
2971 if (!FRAME_FONT (f))
2973 delete_frame (frame, Qnoelisp);
2974 error ("Invalid frame font");
2977 /* Frame contents get displaced if an embedded X window has a border. */
2978 if (! FRAME_X_EMBEDDED_P (f))
2979 x_default_parameter (f, parms, Qborder_width, make_number (0),
2980 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
2982 /* This defaults to 1 in order to match xterm. We recognize either
2983 internalBorderWidth or internalBorder (which is what xterm calls
2984 it). */
2985 if (NILP (Fassq (Qinternal_border_width, parms)))
2987 Lisp_Object value;
2989 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
2990 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
2991 if (! EQ (value, Qunbound))
2992 parms = Fcons (Fcons (Qinternal_border_width, value),
2993 parms);
2995 x_default_parameter (f, parms, Qinternal_border_width,
2996 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
2997 make_number (0),
2998 #else
2999 make_number (1),
3000 #endif
3001 "internalBorderWidth", "internalBorderWidth",
3002 RES_TYPE_NUMBER);
3003 x_default_parameter (f, parms, Qvertical_scroll_bars,
3004 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3005 Qright,
3006 #else
3007 Qleft,
3008 #endif
3009 "verticalScrollBars", "ScrollBars",
3010 RES_TYPE_SYMBOL);
3012 /* Also do the stuff which must be set before the window exists. */
3013 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3014 "foreground", "Foreground", RES_TYPE_STRING);
3015 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3016 "background", "Background", RES_TYPE_STRING);
3017 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3018 "pointerColor", "Foreground", RES_TYPE_STRING);
3019 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3020 "borderColor", "BorderColor", RES_TYPE_STRING);
3021 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3022 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3023 x_default_parameter (f, parms, Qline_spacing, Qnil,
3024 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3025 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3026 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3027 x_default_parameter (f, parms, Qright_fringe, Qnil,
3028 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3030 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3031 "scrollBarForeground",
3032 "ScrollBarForeground", 1);
3033 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3034 "scrollBarBackground",
3035 "ScrollBarBackground", 0);
3037 #ifdef GLYPH_DEBUG
3038 image_cache_refcount =
3039 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3040 dpyinfo_refcount = dpyinfo->reference_count;
3041 #endif /* GLYPH_DEBUG */
3043 /* Init faces before x_default_parameter is called for scroll-bar
3044 parameters because that function calls x_set_scroll_bar_width,
3045 which calls change_frame_size, which calls Fset_window_buffer,
3046 which runs hooks, which call Fvertical_motion. At the end, we
3047 end up in init_iterator with a null face cache, which should not
3048 happen. */
3049 init_frame_faces (f);
3051 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3052 look up the X resources controlling the menu-bar and tool-bar
3053 here; they are processed specially at startup, and reflected in
3054 the values of the mode variables.
3056 Avoid calling window-configuration-change-hook; otherwise we
3057 could get an infloop in next_frame since the frame is not yet in
3058 Vframe_list. */
3060 ptrdiff_t count2 = SPECPDL_INDEX ();
3061 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3062 inhibit_lisp_code = Qt;
3064 x_default_parameter (f, parms, Qmenu_bar_lines,
3065 NILP (Vmenu_bar_mode)
3066 ? make_number (0) : make_number (1),
3067 NULL, NULL, RES_TYPE_NUMBER);
3068 x_default_parameter (f, parms, Qtool_bar_lines,
3069 NILP (Vtool_bar_mode)
3070 ? make_number (0) : make_number (1),
3071 NULL, NULL, RES_TYPE_NUMBER);
3073 unbind_to (count2, Qnil);
3076 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3077 "bufferPredicate", "BufferPredicate",
3078 RES_TYPE_SYMBOL);
3079 x_default_parameter (f, parms, Qtitle, Qnil,
3080 "title", "Title", RES_TYPE_STRING);
3081 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3082 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3083 x_default_parameter (f, parms, Qfullscreen, Qnil,
3084 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3085 x_default_parameter (f, parms, Qtool_bar_position,
3086 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
3088 /* Compute the size of the X window. */
3089 window_prompting = x_figure_window_size (f, parms, 1);
3091 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3092 f->no_split = minibuffer_only || EQ (tem, Qt);
3094 x_icon_verify (f, parms);
3096 /* Create the X widget or window. */
3097 #ifdef USE_X_TOOLKIT
3098 x_window (f, window_prompting, minibuffer_only);
3099 #else
3100 x_window (f);
3101 #endif
3103 x_icon (f, parms);
3104 x_make_gc (f);
3106 /* Now consider the frame official. */
3107 f->terminal->reference_count++;
3108 FRAME_DISPLAY_INFO (f)->reference_count++;
3109 Vframe_list = Fcons (frame, Vframe_list);
3111 /* We need to do this after creating the X window, so that the
3112 icon-creation functions can say whose icon they're describing. */
3113 x_default_parameter (f, parms, Qicon_type, Qt,
3114 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3116 x_default_parameter (f, parms, Qauto_raise, Qnil,
3117 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3118 x_default_parameter (f, parms, Qauto_lower, Qnil,
3119 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3120 x_default_parameter (f, parms, Qcursor_type, Qbox,
3121 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3122 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3123 "scrollBarWidth", "ScrollBarWidth",
3124 RES_TYPE_NUMBER);
3125 x_default_parameter (f, parms, Qalpha, Qnil,
3126 "alpha", "Alpha", RES_TYPE_NUMBER);
3128 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3129 Change will not be effected unless different from the current
3130 FRAME_LINES (f). */
3131 width = FRAME_COLS (f);
3132 height = FRAME_LINES (f);
3134 SET_FRAME_COLS (f, 0);
3135 FRAME_LINES (f) = 0;
3136 change_frame_size (f, height, width, 1, 0, 0);
3138 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3139 /* Create the menu bar. */
3140 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3142 /* If this signals an error, we haven't set size hints for the
3143 frame and we didn't make it visible. */
3144 initialize_frame_menubar (f);
3146 #ifndef USE_GTK
3147 /* This is a no-op, except under Motif where it arranges the
3148 main window for the widgets on it. */
3149 lw_set_main_areas (f->output_data.x->column_widget,
3150 f->output_data.x->menubar_widget,
3151 f->output_data.x->edit_widget);
3152 #endif /* not USE_GTK */
3154 #endif /* USE_X_TOOLKIT || USE_GTK */
3156 /* Tell the server what size and position, etc, we want, and how
3157 badly we want them. This should be done after we have the menu
3158 bar so that its size can be taken into account. */
3159 block_input ();
3160 x_wm_set_size_hint (f, window_prompting, 0);
3161 unblock_input ();
3163 /* Make the window appear on the frame and enable display, unless
3164 the caller says not to. However, with explicit parent, Emacs
3165 cannot control visibility, so don't try. */
3166 if (! f->output_data.x->explicit_parent)
3168 Lisp_Object visibility;
3170 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3171 RES_TYPE_SYMBOL);
3172 if (EQ (visibility, Qunbound))
3173 visibility = Qt;
3175 if (EQ (visibility, Qicon))
3176 x_iconify_frame (f);
3177 else if (! NILP (visibility))
3178 x_make_frame_visible (f);
3179 else
3181 /* Must have been Qnil. */
3185 block_input ();
3187 /* Set machine name and pid for the purpose of window managers. */
3188 set_machine_and_pid_properties (f);
3190 /* Set the WM leader property. GTK does this itself, so this is not
3191 needed when using GTK. */
3192 if (dpyinfo->client_leader_window != 0)
3194 XChangeProperty (FRAME_X_DISPLAY (f),
3195 FRAME_OUTER_WINDOW (f),
3196 dpyinfo->Xatom_wm_client_leader,
3197 XA_WINDOW, 32, PropModeReplace,
3198 (unsigned char *) &dpyinfo->client_leader_window, 1);
3201 unblock_input ();
3203 /* Initialize `default-minibuffer-frame' in case this is the first
3204 frame on this terminal. */
3205 if (FRAME_HAS_MINIBUF_P (f)
3206 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3207 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3208 kset_default_minibuffer_frame (kb, frame);
3210 /* All remaining specified parameters, which have not been "used"
3211 by x_get_arg and friends, now go in the misc. alist of the frame. */
3212 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3213 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3214 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3216 UNGCPRO;
3218 /* Make sure windows on this frame appear in calls to next-window
3219 and similar functions. */
3220 Vwindow_list = Qnil;
3222 return unbind_to (count, frame);
3226 /* FRAME is used only to get a handle on the X display. We don't pass the
3227 display info directly because we're called from frame.c, which doesn't
3228 know about that structure. */
3230 Lisp_Object
3231 x_get_focus_frame (struct frame *frame)
3233 struct x_display_info *dpyinfo = FRAME_DISPLAY_INFO (frame);
3234 Lisp_Object xfocus;
3235 if (! dpyinfo->x_focus_frame)
3236 return Qnil;
3238 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3239 return xfocus;
3243 /* In certain situations, when the window manager follows a
3244 click-to-focus policy, there seems to be no way around calling
3245 XSetInputFocus to give another frame the input focus .
3247 In an ideal world, XSetInputFocus should generally be avoided so
3248 that applications don't interfere with the window manager's focus
3249 policy. But I think it's okay to use when it's clearly done
3250 following a user-command. */
3252 void
3253 x_focus_frame (struct frame *f)
3255 Display *dpy = FRAME_X_DISPLAY (f);
3257 block_input ();
3258 x_catch_errors (dpy);
3260 if (FRAME_X_EMBEDDED_P (f))
3262 /* For Xembedded frames, normally the embedder forwards key
3263 events. See XEmbed Protocol Specification at
3264 http://freedesktop.org/wiki/Specifications/xembed-spec */
3265 xembed_request_focus (f);
3267 else
3269 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3270 RevertToParent, CurrentTime);
3271 x_ewmh_activate_frame (f);
3274 x_uncatch_errors ();
3275 unblock_input ();
3279 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3280 doc: /* Internal function called by `color-defined-p', which see
3281 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3282 (Lisp_Object color, Lisp_Object frame)
3284 XColor foo;
3285 struct frame *f = decode_window_system_frame (frame);
3287 CHECK_STRING (color);
3289 if (x_defined_color (f, SSDATA (color), &foo, 0))
3290 return Qt;
3291 else
3292 return Qnil;
3295 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3296 doc: /* Internal function called by `color-values', which see. */)
3297 (Lisp_Object color, Lisp_Object frame)
3299 XColor foo;
3300 struct frame *f = decode_window_system_frame (frame);
3302 CHECK_STRING (color);
3304 if (x_defined_color (f, SSDATA (color), &foo, 0))
3305 return list3i (foo.red, foo.green, foo.blue);
3306 else
3307 return Qnil;
3310 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3311 doc: /* Internal function called by `display-color-p', which see. */)
3312 (Lisp_Object terminal)
3314 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3316 if (dpyinfo->n_planes <= 2)
3317 return Qnil;
3319 switch (dpyinfo->visual->class)
3321 case StaticColor:
3322 case PseudoColor:
3323 case TrueColor:
3324 case DirectColor:
3325 return Qt;
3327 default:
3328 return Qnil;
3332 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3333 0, 1, 0,
3334 doc: /* Return t if the X display supports shades of gray.
3335 Note that color displays do support shades of gray.
3336 The optional argument TERMINAL specifies which display to ask about.
3337 TERMINAL should be a terminal object, a frame or a display name (a string).
3338 If omitted or nil, that stands for the selected frame's display. */)
3339 (Lisp_Object terminal)
3341 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3343 if (dpyinfo->n_planes <= 1)
3344 return Qnil;
3346 switch (dpyinfo->visual->class)
3348 case StaticColor:
3349 case PseudoColor:
3350 case TrueColor:
3351 case DirectColor:
3352 case StaticGray:
3353 case GrayScale:
3354 return Qt;
3356 default:
3357 return Qnil;
3361 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3362 0, 1, 0,
3363 doc: /* Return the width in pixels of the X display TERMINAL.
3364 The optional argument TERMINAL specifies which display to ask about.
3365 TERMINAL should be a terminal object, a frame or a display name (a string).
3366 If omitted or nil, that stands for the selected frame's display.
3368 On \"multi-monitor\" setups this refers to the pixel width for all
3369 physical monitors associated with TERMINAL. To get information for
3370 each physical monitor, use `display-monitor-attributes-list'. */)
3371 (Lisp_Object terminal)
3373 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3375 return make_number (x_display_pixel_width (dpyinfo));
3378 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3379 Sx_display_pixel_height, 0, 1, 0,
3380 doc: /* Return the height in pixels of the X display TERMINAL.
3381 The optional argument TERMINAL specifies which display to ask about.
3382 TERMINAL should be a terminal object, a frame or a display name (a string).
3383 If omitted or nil, that stands for the selected frame's display.
3385 On \"multi-monitor\" setups this refers to the pixel height for all
3386 physical monitors associated with TERMINAL. To get information for
3387 each physical monitor, use `display-monitor-attributes-list'. */)
3388 (Lisp_Object terminal)
3390 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3392 return make_number (x_display_pixel_height (dpyinfo));
3395 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3396 0, 1, 0,
3397 doc: /* Return the number of bitplanes of the X display TERMINAL.
3398 The optional argument TERMINAL specifies which display to ask about.
3399 TERMINAL should be a terminal object, a frame or a display name (a string).
3400 If omitted or nil, that stands for the selected frame's display. */)
3401 (Lisp_Object terminal)
3403 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3405 return make_number (dpyinfo->n_planes);
3408 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3409 0, 1, 0,
3410 doc: /* Return the number of color cells of the X display TERMINAL.
3411 The optional argument TERMINAL specifies which display to ask about.
3412 TERMINAL should be a terminal object, a frame or a display name (a string).
3413 If omitted or nil, that stands for the selected frame's display. */)
3414 (Lisp_Object terminal)
3416 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3418 int nr_planes = DisplayPlanes (dpyinfo->display,
3419 XScreenNumberOfScreen (dpyinfo->screen));
3421 /* Truncate nr_planes to 24 to avoid integer overflow.
3422 Some displays says 32, but only 24 bits are actually significant.
3423 There are only very few and rare video cards that have more than
3424 24 significant bits. Also 24 bits is more than 16 million colors,
3425 it "should be enough for everyone". */
3426 if (nr_planes > 24) nr_planes = 24;
3428 return make_number (1 << nr_planes);
3431 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3432 Sx_server_max_request_size,
3433 0, 1, 0,
3434 doc: /* Return the maximum request size of the X server of display TERMINAL.
3435 The optional argument TERMINAL specifies which display to ask about.
3436 TERMINAL should be a terminal object, a frame or a display name (a string).
3437 If omitted or nil, that stands for the selected frame's display. */)
3438 (Lisp_Object terminal)
3440 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3442 return make_number (MAXREQUEST (dpyinfo->display));
3445 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3446 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3447 \(Labeling every distributor as a "vendor" embodies the false assumption
3448 that operating systems cannot be developed and distributed noncommercially.)
3449 The optional argument TERMINAL specifies which display to ask about.
3450 TERMINAL should be a terminal object, a frame or a display name (a string).
3451 If omitted or nil, that stands for the selected frame's display. */)
3452 (Lisp_Object terminal)
3454 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3455 const char *vendor = ServerVendor (dpyinfo->display);
3457 if (! vendor) vendor = "";
3458 return build_string (vendor);
3461 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3462 doc: /* Return the version numbers of the X server of display TERMINAL.
3463 The value is a list of three integers: the major and minor
3464 version numbers of the X Protocol in use, and the distributor-specific release
3465 number. See also the function `x-server-vendor'.
3467 The optional argument TERMINAL specifies which display to ask about.
3468 TERMINAL should be a terminal object, a frame or a display name (a string).
3469 If omitted or nil, that stands for the selected frame's display. */)
3470 (Lisp_Object terminal)
3472 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3473 Display *dpy = dpyinfo->display;
3475 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3476 VendorRelease (dpy));
3479 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3480 doc: /* Return the number of screens on the X server of display TERMINAL.
3481 The optional argument TERMINAL specifies which display to ask about.
3482 TERMINAL should be a terminal object, a frame or a display name (a string).
3483 If omitted or nil, that stands for the selected frame's display. */)
3484 (Lisp_Object terminal)
3486 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3488 return make_number (ScreenCount (dpyinfo->display));
3491 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3492 doc: /* Return the height in millimeters of the X display TERMINAL.
3493 The optional argument TERMINAL specifies which display to ask about.
3494 TERMINAL should be a terminal object, a frame or a display name (a string).
3495 If omitted or nil, that stands for the selected frame's display.
3497 On \"multi-monitor\" setups this refers to the height in millimeters for
3498 all physical monitors associated with TERMINAL. To get information
3499 for each physical monitor, use `display-monitor-attributes-list'. */)
3500 (Lisp_Object terminal)
3502 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3504 return make_number (HeightMMOfScreen (dpyinfo->screen));
3507 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3508 doc: /* Return the width in millimeters of the X display TERMINAL.
3509 The optional argument TERMINAL specifies which display to ask about.
3510 TERMINAL should be a terminal object, a frame or a display name (a string).
3511 If omitted or nil, that stands for the selected frame's display.
3513 On \"multi-monitor\" setups this refers to the width in millimeters for
3514 all physical monitors associated with TERMINAL. To get information
3515 for each physical monitor, use `display-monitor-attributes-list'. */)
3516 (Lisp_Object terminal)
3518 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3520 return make_number (WidthMMOfScreen (dpyinfo->screen));
3523 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3524 Sx_display_backing_store, 0, 1, 0,
3525 doc: /* Return an indication of whether X display TERMINAL does backing store.
3526 The value may be `always', `when-mapped', or `not-useful'.
3527 The optional argument TERMINAL specifies which display to ask about.
3528 TERMINAL should be a terminal object, a frame or a display name (a string).
3529 If omitted or nil, that stands for the selected frame's display. */)
3530 (Lisp_Object terminal)
3532 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3533 Lisp_Object result;
3535 switch (DoesBackingStore (dpyinfo->screen))
3537 case Always:
3538 result = intern ("always");
3539 break;
3541 case WhenMapped:
3542 result = intern ("when-mapped");
3543 break;
3545 case NotUseful:
3546 result = intern ("not-useful");
3547 break;
3549 default:
3550 error ("Strange value for BackingStore parameter of screen");
3553 return result;
3556 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3557 Sx_display_visual_class, 0, 1, 0,
3558 doc: /* Return the visual class of the X display TERMINAL.
3559 The value is one of the symbols `static-gray', `gray-scale',
3560 `static-color', `pseudo-color', `true-color', or `direct-color'.
3562 The optional argument TERMINAL specifies which display to ask about.
3563 TERMINAL should a terminal object, a frame or a display name (a string).
3564 If omitted or nil, that stands for the selected frame's display. */)
3565 (Lisp_Object terminal)
3567 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3568 Lisp_Object result;
3570 switch (dpyinfo->visual->class)
3572 case StaticGray:
3573 result = intern ("static-gray");
3574 break;
3575 case GrayScale:
3576 result = intern ("gray-scale");
3577 break;
3578 case StaticColor:
3579 result = intern ("static-color");
3580 break;
3581 case PseudoColor:
3582 result = intern ("pseudo-color");
3583 break;
3584 case TrueColor:
3585 result = intern ("true-color");
3586 break;
3587 case DirectColor:
3588 result = intern ("direct-color");
3589 break;
3590 default:
3591 error ("Display has an unknown visual class");
3594 return result;
3597 DEFUN ("x-display-save-under", Fx_display_save_under,
3598 Sx_display_save_under, 0, 1, 0,
3599 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3600 The optional argument TERMINAL specifies which display to ask about.
3601 TERMINAL should be a terminal object, a frame or a display name (a string).
3602 If omitted or nil, that stands for the selected frame's display. */)
3603 (Lisp_Object terminal)
3605 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3607 if (DoesSaveUnders (dpyinfo->screen) == True)
3608 return Qt;
3609 else
3610 return Qnil;
3613 /* Store the geometry of the workarea on display DPYINFO into *RECT.
3614 Return false if and only if the workarea information cannot be
3615 obtained via the _NET_WORKAREA root window property. */
3617 #if ! GTK_CHECK_VERSION (3, 4, 0)
3618 static bool
3619 x_get_net_workarea (struct x_display_info *dpyinfo, XRectangle *rect)
3621 Display *dpy = dpyinfo->display;
3622 long offset, max_len;
3623 Atom target_type, actual_type;
3624 unsigned long actual_size, bytes_remaining;
3625 int rc, actual_format;
3626 unsigned char *tmp_data = NULL;
3627 bool result = false;
3629 x_catch_errors (dpy);
3630 offset = 0;
3631 max_len = 1;
3632 target_type = XA_CARDINAL;
3633 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3634 dpyinfo->Xatom_net_current_desktop,
3635 offset, max_len, False, target_type,
3636 &actual_type, &actual_format, &actual_size,
3637 &bytes_remaining, &tmp_data);
3638 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3639 && actual_format == 32 && actual_size == max_len)
3641 long current_desktop = ((long *) tmp_data)[0];
3643 XFree (tmp_data);
3644 tmp_data = NULL;
3646 offset = 4 * current_desktop;
3647 max_len = 4;
3648 rc = XGetWindowProperty (dpy, dpyinfo->root_window,
3649 dpyinfo->Xatom_net_workarea,
3650 offset, max_len, False, target_type,
3651 &actual_type, &actual_format, &actual_size,
3652 &bytes_remaining, &tmp_data);
3653 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
3654 && actual_format == 32 && actual_size == max_len)
3656 long *values = (long *) tmp_data;
3658 rect->x = values[0];
3659 rect->y = values[1];
3660 rect->width = values[2];
3661 rect->height = values[3];
3663 XFree (tmp_data);
3664 tmp_data = NULL;
3666 result = true;
3669 if (tmp_data)
3670 XFree (tmp_data);
3671 x_uncatch_errors ();
3673 return result;
3675 #endif
3677 #ifndef USE_GTK
3679 /* Return monitor number where F is "most" or closest to. */
3680 static int
3681 x_get_monitor_for_frame (struct frame *f,
3682 struct MonitorInfo *monitors,
3683 int n_monitors)
3685 XRectangle frect;
3686 int area = 0, dist = -1;
3687 int best_area = -1, best_dist = -1;
3688 int i;
3690 if (n_monitors == 1) return 0;
3691 frect.x = f->left_pos;
3692 frect.y = f->top_pos;
3693 frect.width = FRAME_PIXEL_WIDTH (f);
3694 frect.height = FRAME_PIXEL_HEIGHT (f);
3696 for (i = 0; i < n_monitors; ++i)
3698 struct MonitorInfo *mi = &monitors[i];
3699 XRectangle res;
3700 int a = 0;
3702 if (mi->geom.width == 0) continue;
3704 if (x_intersect_rectangles (&mi->geom, &frect, &res))
3706 a = res.width * res.height;
3707 if (a > area)
3709 area = a;
3710 best_area = i;
3714 if (a == 0 && area == 0)
3716 int dx, dy, d;
3717 if (frect.x + frect.width < mi->geom.x)
3718 dx = mi->geom.x - frect.x + frect.width;
3719 else if (frect.x > mi->geom.x + mi->geom.width)
3720 dx = frect.x - mi->geom.x + mi->geom.width;
3721 else
3722 dx = 0;
3723 if (frect.y + frect.height < mi->geom.y)
3724 dy = mi->geom.y - frect.y + frect.height;
3725 else if (frect.y > mi->geom.y + mi->geom.height)
3726 dy = frect.y - mi->geom.y + mi->geom.height;
3727 else
3728 dy = 0;
3730 d = dx*dx + dy*dy;
3731 if (dist == -1 || dist > d)
3733 dist = d;
3734 best_dist = i;
3739 return best_area != -1 ? best_area : (best_dist != -1 ? best_dist : 0);
3742 static Lisp_Object
3743 x_make_monitor_attribute_list (struct MonitorInfo *monitors,
3744 int n_monitors,
3745 int primary_monitor,
3746 struct x_display_info *dpyinfo,
3747 const char *source)
3749 Lisp_Object monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
3750 Lisp_Object frame, rest;
3752 FOR_EACH_FRAME (rest, frame)
3754 struct frame *f = XFRAME (frame);
3756 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
3757 && !EQ (frame, tip_frame))
3759 int i = x_get_monitor_for_frame (f, monitors, n_monitors);
3760 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
3764 return make_monitor_attribute_list (monitors, n_monitors, primary_monitor,
3765 monitor_frames, source);
3768 static Lisp_Object
3769 x_get_monitor_attributes_fallback (struct x_display_info *dpyinfo)
3771 struct MonitorInfo monitor;
3772 XRectangle workarea_r;
3774 /* Fallback: treat (possibly) multiple physical monitors as if they
3775 formed a single monitor as a whole. This should provide a
3776 consistent result at least on single monitor environments. */
3777 monitor.geom.x = monitor.geom.y = 0;
3778 monitor.geom.width = x_display_pixel_width (dpyinfo);
3779 monitor.geom.height = x_display_pixel_height (dpyinfo);
3780 monitor.mm_width = WidthMMOfScreen (dpyinfo->screen);
3781 monitor.mm_height = HeightMMOfScreen (dpyinfo->screen);
3782 monitor.name = xstrdup ("combined screen");
3784 if (x_get_net_workarea (dpyinfo, &workarea_r))
3785 monitor.work = workarea_r;
3786 else
3787 monitor.work = monitor.geom;
3788 return x_make_monitor_attribute_list (&monitor, 1, 0, dpyinfo, "fallback");
3792 #ifdef HAVE_XINERAMA
3793 static Lisp_Object
3794 x_get_monitor_attributes_xinerama (struct x_display_info *dpyinfo)
3796 int n_monitors, i;
3797 Lisp_Object attributes_list = Qnil;
3798 Display *dpy = dpyinfo->display;
3799 XineramaScreenInfo *info = XineramaQueryScreens (dpy, &n_monitors);
3800 struct MonitorInfo *monitors;
3801 double mm_width_per_pixel, mm_height_per_pixel;
3803 if (! info || n_monitors == 0)
3805 if (info)
3806 XFree (info);
3807 return attributes_list;
3810 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
3811 / x_display_pixel_width (dpyinfo));
3812 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
3813 / x_display_pixel_height (dpyinfo));
3814 monitors = xzalloc (n_monitors * sizeof *monitors);
3815 for (i = 0; i < n_monitors; ++i)
3817 struct MonitorInfo *mi = &monitors[i];
3818 XRectangle workarea_r;
3820 mi->geom.x = info[i].x_org;
3821 mi->geom.y = info[i].y_org;
3822 mi->geom.width = info[i].width;
3823 mi->geom.height = info[i].height;
3824 mi->mm_width = mi->geom.width * mm_width_per_pixel + 0.5;
3825 mi->mm_height = mi->geom.height * mm_height_per_pixel + 0.5;
3826 mi->name = 0;
3828 /* Xinerama usually have primary monitor first, just use that. */
3829 if (i == 0 && x_get_net_workarea (dpyinfo, &workarea_r))
3831 mi->work = workarea_r;
3832 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3833 mi->work = mi->geom;
3835 else
3836 mi->work = mi->geom;
3838 XFree (info);
3840 attributes_list = x_make_monitor_attribute_list (monitors,
3841 n_monitors,
3843 dpyinfo,
3844 "Xinerama");
3845 free_monitors (monitors, n_monitors);
3846 return attributes_list;
3848 #endif /* HAVE_XINERAMA */
3851 #ifdef HAVE_XRANDR
3852 static Lisp_Object
3853 x_get_monitor_attributes_xrandr (struct x_display_info *dpyinfo)
3855 Lisp_Object attributes_list = Qnil;
3856 XRRScreenResources *resources;
3857 Display *dpy = dpyinfo->display;
3858 int i, n_monitors, primary = -1;
3859 RROutput pxid = None;
3860 struct MonitorInfo *monitors;
3862 #ifdef HAVE_XRRGETSCREENRESOURCESCURRENT
3863 resources = XRRGetScreenResourcesCurrent (dpy, dpyinfo->root_window);
3864 #else
3865 resources = XRRGetScreenResources (dpy, dpyinfo->root_window);
3866 #endif
3867 if (! resources || resources->noutput == 0)
3869 if (resources)
3870 XRRFreeScreenResources (resources);
3871 return Qnil;
3873 n_monitors = resources->noutput;
3874 monitors = xzalloc (n_monitors * sizeof *monitors);
3876 #ifdef HAVE_XRRGETOUTPUTPRIMARY
3877 pxid = XRRGetOutputPrimary (dpy, dpyinfo->root_window);
3878 #endif
3880 for (i = 0; i < n_monitors; ++i)
3882 XRROutputInfo *info = XRRGetOutputInfo (dpy, resources,
3883 resources->outputs[i]);
3884 Connection conn = info ? info->connection : RR_Disconnected;
3885 RRCrtc id = info ? info->crtc : None;
3887 if (strcmp (info->name, "default") == 0)
3889 /* Non XRandr 1.2 driver, does not give useful data. */
3890 XRRFreeOutputInfo (info);
3891 XRRFreeScreenResources (resources);
3892 free_monitors (monitors, n_monitors);
3893 return Qnil;
3896 if (conn != RR_Disconnected && id != None)
3898 XRRCrtcInfo *crtc = XRRGetCrtcInfo (dpy, resources, id);
3899 struct MonitorInfo *mi = &monitors[i];
3900 XRectangle workarea_r;
3902 if (! crtc)
3904 XRRFreeOutputInfo (info);
3905 continue;
3908 mi->geom.x = crtc->x;
3909 mi->geom.y = crtc->y;
3910 mi->geom.width = crtc->width;
3911 mi->geom.height = crtc->height;
3912 mi->mm_width = info->mm_width;
3913 mi->mm_height = info->mm_height;
3914 mi->name = xstrdup (info->name);
3916 if (pxid != None && pxid == resources->outputs[i])
3917 primary = i;
3918 else if (primary == -1 && strcmp (info->name, "LVDS") == 0)
3919 primary = i;
3921 if (i == primary && x_get_net_workarea (dpyinfo, &workarea_r))
3923 mi->work= workarea_r;
3924 if (! x_intersect_rectangles (&mi->geom, &mi->work, &mi->work))
3925 mi->work = mi->geom;
3927 else
3928 mi->work = mi->geom;
3930 XRRFreeCrtcInfo (crtc);
3932 XRRFreeOutputInfo (info);
3934 XRRFreeScreenResources (resources);
3936 attributes_list = x_make_monitor_attribute_list (monitors,
3937 n_monitors,
3938 primary,
3939 dpyinfo,
3940 "XRandr");
3941 free_monitors (monitors, n_monitors);
3942 return attributes_list;
3944 #endif /* HAVE_XRANDR */
3946 static Lisp_Object
3947 x_get_monitor_attributes (struct x_display_info *dpyinfo)
3949 Lisp_Object attributes_list = Qnil;
3950 Display *dpy = dpyinfo->display;
3952 (void) dpy; /* Suppress unused variable warning. */
3954 #ifdef HAVE_XRANDR
3955 int xrr_event_base, xrr_error_base;
3956 bool xrr_ok = false;
3957 xrr_ok = XRRQueryExtension (dpy, &xrr_event_base, &xrr_error_base);
3958 if (xrr_ok)
3960 int xrr_major, xrr_minor;
3961 XRRQueryVersion (dpy, &xrr_major, &xrr_minor);
3962 xrr_ok = (xrr_major == 1 && xrr_minor >= 2) || xrr_major > 1;
3965 if (xrr_ok)
3966 attributes_list = x_get_monitor_attributes_xrandr (dpyinfo);
3967 #endif /* HAVE_XRANDR */
3969 #ifdef HAVE_XINERAMA
3970 if (NILP (attributes_list))
3972 int xin_event_base, xin_error_base;
3973 bool xin_ok = false;
3974 xin_ok = XineramaQueryExtension (dpy, &xin_event_base, &xin_error_base);
3975 if (xin_ok && XineramaIsActive (dpy))
3976 attributes_list = x_get_monitor_attributes_xinerama (dpyinfo);
3978 #endif /* HAVE_XINERAMA */
3980 if (NILP (attributes_list))
3981 attributes_list = x_get_monitor_attributes_fallback (dpyinfo);
3983 return attributes_list;
3986 #endif /* !USE_GTK */
3988 DEFUN ("x-display-monitor-attributes-list", Fx_display_monitor_attributes_list,
3989 Sx_display_monitor_attributes_list,
3990 0, 1, 0,
3991 doc: /* Return a list of physical monitor attributes on the X display TERMINAL.
3993 The optional argument TERMINAL specifies which display to ask about.
3994 TERMINAL should be a terminal object, a frame or a display name (a string).
3995 If omitted or nil, that stands for the selected frame's display.
3997 In addition to the standard attribute keys listed in
3998 `display-monitor-attributes-list', the following keys are contained in
3999 the attributes:
4001 source -- String describing the source from which multi-monitor
4002 information is obtained, one of \"Gdk\", \"XRandr\",
4003 \"Xinerama\", or \"fallback\"
4005 Internal use only, use `display-monitor-attributes-list' instead. */)
4006 (Lisp_Object terminal)
4008 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4009 Lisp_Object attributes_list = Qnil;
4011 #ifdef USE_GTK
4012 double mm_width_per_pixel, mm_height_per_pixel;
4013 GdkDisplay *gdpy;
4014 GdkScreen *gscreen;
4015 gint primary_monitor = 0, n_monitors, i;
4016 Lisp_Object monitor_frames, rest, frame;
4017 static const char *source = "Gdk";
4018 struct MonitorInfo *monitors;
4020 block_input ();
4021 mm_width_per_pixel = ((double) WidthMMOfScreen (dpyinfo->screen)
4022 / x_display_pixel_width (dpyinfo));
4023 mm_height_per_pixel = ((double) HeightMMOfScreen (dpyinfo->screen)
4024 / x_display_pixel_height (dpyinfo));
4025 gdpy = gdk_x11_lookup_xdisplay (dpyinfo->display);
4026 gscreen = gdk_display_get_default_screen (gdpy);
4027 #if GTK_CHECK_VERSION (2, 20, 0)
4028 primary_monitor = gdk_screen_get_primary_monitor (gscreen);
4029 #endif
4030 n_monitors = gdk_screen_get_n_monitors (gscreen);
4031 monitor_frames = Fmake_vector (make_number (n_monitors), Qnil);
4032 monitors = xzalloc (n_monitors * sizeof *monitors);
4034 FOR_EACH_FRAME (rest, frame)
4036 struct frame *f = XFRAME (frame);
4038 if (FRAME_X_P (f) && FRAME_DISPLAY_INFO (f) == dpyinfo
4039 && !EQ (frame, tip_frame))
4041 GdkWindow *gwin = gtk_widget_get_window (FRAME_GTK_WIDGET (f));
4043 i = gdk_screen_get_monitor_at_window (gscreen, gwin);
4044 ASET (monitor_frames, i, Fcons (frame, AREF (monitor_frames, i)));
4048 for (i = 0; i < n_monitors; ++i)
4050 gint width_mm = -1, height_mm = -1;
4051 GdkRectangle rec, work;
4052 struct MonitorInfo *mi = &monitors[i];
4054 gdk_screen_get_monitor_geometry (gscreen, i, &rec);
4056 #if GTK_CHECK_VERSION (2, 14, 0)
4057 width_mm = gdk_screen_get_monitor_width_mm (gscreen, i);
4058 height_mm = gdk_screen_get_monitor_height_mm (gscreen, i);
4059 #endif
4060 if (width_mm < 0)
4061 width_mm = rec.width * mm_width_per_pixel + 0.5;
4062 if (height_mm < 0)
4063 height_mm = rec.height * mm_height_per_pixel + 0.5;
4065 #if GTK_CHECK_VERSION (3, 4, 0)
4066 gdk_screen_get_monitor_workarea (gscreen, i, &work);
4067 #else
4068 /* Emulate the behavior of GTK+ 3.4. */
4070 XRectangle workarea_r;
4072 if (i == primary_monitor && x_get_net_workarea (dpyinfo, &workarea_r))
4074 work.x = workarea_r.x;
4075 work.y = workarea_r.y;
4076 work.width = workarea_r.width;
4077 work.height = workarea_r.height;
4078 if (! gdk_rectangle_intersect (&rec, &work, &work))
4079 work = rec;
4081 else
4082 work = rec;
4084 #endif
4087 mi->geom.x = rec.x;
4088 mi->geom.y = rec.y;
4089 mi->geom.width = rec.width;
4090 mi->geom.height = rec.height;
4091 mi->work.x = work.x;
4092 mi->work.y = work.y;
4093 mi->work.width = work.width;
4094 mi->work.height = work.height;
4095 mi->mm_width = width_mm;
4096 mi->mm_height = height_mm;
4098 #if GTK_CHECK_VERSION (2, 14, 0)
4099 mi->name = gdk_screen_get_monitor_plug_name (gscreen, i);
4100 #endif
4103 attributes_list = make_monitor_attribute_list (monitors,
4104 n_monitors,
4105 primary_monitor,
4106 monitor_frames,
4107 source);
4108 unblock_input ();
4109 #else /* not USE_GTK */
4111 block_input ();
4112 attributes_list = x_get_monitor_attributes (dpyinfo);
4113 unblock_input ();
4115 #endif /* not USE_GTK */
4117 return attributes_list;
4120 /************************************************************************
4121 X Displays
4122 ************************************************************************/
4125 /* Mapping visual names to visuals. */
4127 static struct visual_class
4129 const char *name;
4130 int class;
4132 visual_classes[] =
4134 {"StaticGray", StaticGray},
4135 {"GrayScale", GrayScale},
4136 {"StaticColor", StaticColor},
4137 {"PseudoColor", PseudoColor},
4138 {"TrueColor", TrueColor},
4139 {"DirectColor", DirectColor},
4140 {NULL, 0}
4144 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4146 /* Value is the screen number of screen SCR. This is a substitute for
4147 the X function with the same name when that doesn't exist. */
4150 XScreenNumberOfScreen (scr)
4151 register Screen *scr;
4153 Display *dpy = scr->display;
4154 int i;
4156 for (i = 0; i < dpy->nscreens; ++i)
4157 if (scr == dpy->screens + i)
4158 break;
4160 return i;
4163 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4166 /* Select the visual that should be used on display DPYINFO. Set
4167 members of DPYINFO appropriately. Called from x_term_init. */
4169 void
4170 select_visual (struct x_display_info *dpyinfo)
4172 Display *dpy = dpyinfo->display;
4173 Screen *screen = dpyinfo->screen;
4174 Lisp_Object value;
4176 /* See if a visual is specified. */
4177 value = display_x_get_resource (dpyinfo,
4178 build_string ("visualClass"),
4179 build_string ("VisualClass"),
4180 Qnil, Qnil);
4181 if (STRINGP (value))
4183 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4184 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4185 depth, a decimal number. NAME is compared with case ignored. */
4186 char *s = alloca (SBYTES (value) + 1);
4187 char *dash;
4188 int i, class = -1;
4189 XVisualInfo vinfo;
4191 strcpy (s, SSDATA (value));
4192 dash = strchr (s, '-');
4193 if (dash)
4195 dpyinfo->n_planes = atoi (dash + 1);
4196 *dash = '\0';
4198 else
4199 /* We won't find a matching visual with depth 0, so that
4200 an error will be printed below. */
4201 dpyinfo->n_planes = 0;
4203 /* Determine the visual class. */
4204 for (i = 0; visual_classes[i].name; ++i)
4205 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4207 class = visual_classes[i].class;
4208 break;
4211 /* Look up a matching visual for the specified class. */
4212 if (class == -1
4213 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4214 dpyinfo->n_planes, class, &vinfo))
4215 fatal ("Invalid visual specification `%s'", SDATA (value));
4217 dpyinfo->visual = vinfo.visual;
4219 else
4221 int n_visuals;
4222 XVisualInfo *vinfo, vinfo_template;
4224 dpyinfo->visual = DefaultVisualOfScreen (screen);
4226 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4227 vinfo_template.screen = XScreenNumberOfScreen (screen);
4228 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4229 &vinfo_template, &n_visuals);
4230 if (n_visuals <= 0)
4231 fatal ("Can't get proper X visual info");
4233 dpyinfo->n_planes = vinfo->depth;
4234 XFree (vinfo);
4239 /* Return the X display structure for the display named NAME.
4240 Open a new connection if necessary. */
4242 static struct x_display_info *
4243 x_display_info_for_name (Lisp_Object name)
4245 Lisp_Object names;
4246 struct x_display_info *dpyinfo;
4248 CHECK_STRING (name);
4250 for (dpyinfo = x_display_list, names = x_display_name_list;
4251 dpyinfo;
4252 dpyinfo = dpyinfo->next, names = XCDR (names))
4254 Lisp_Object tem;
4255 tem = Fstring_equal (XCAR (XCAR (names)), name);
4256 if (!NILP (tem))
4257 return dpyinfo;
4260 /* Use this general default value to start with. */
4261 Vx_resource_name = Vinvocation_name;
4263 validate_x_resource_name ();
4265 dpyinfo = x_term_init (name, 0, SSDATA (Vx_resource_name));
4267 if (dpyinfo == 0)
4268 error ("Cannot connect to X server %s", SDATA (name));
4270 XSETFASTINT (Vwindow_system_version, 11);
4272 return dpyinfo;
4276 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4277 1, 3, 0,
4278 doc: /* Open a connection to a display server.
4279 DISPLAY is the name of the display to connect to.
4280 Optional second arg XRM-STRING is a string of resources in xrdb format.
4281 If the optional third arg MUST-SUCCEED is non-nil,
4282 terminate Emacs if we can't open the connection.
4283 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4284 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4286 char *xrm_option;
4287 struct x_display_info *dpyinfo;
4289 CHECK_STRING (display);
4290 if (! NILP (xrm_string))
4291 CHECK_STRING (xrm_string);
4293 xrm_option = NILP (xrm_string) ? 0 : SSDATA (xrm_string);
4295 validate_x_resource_name ();
4297 /* This is what opens the connection and sets x_current_display.
4298 This also initializes many symbols, such as those used for input. */
4299 dpyinfo = x_term_init (display, xrm_option,
4300 SSDATA (Vx_resource_name));
4302 if (dpyinfo == 0)
4304 if (!NILP (must_succeed))
4305 fatal ("Cannot connect to X server %s.\n\
4306 Check the DISPLAY environment variable or use `-d'.\n\
4307 Also use the `xauth' program to verify that you have the proper\n\
4308 authorization information needed to connect the X server.\n\
4309 An insecure way to solve the problem may be to use `xhost'.\n",
4310 SDATA (display));
4311 else
4312 error ("Cannot connect to X server %s", SDATA (display));
4315 XSETFASTINT (Vwindow_system_version, 11);
4316 return Qnil;
4319 DEFUN ("x-close-connection", Fx_close_connection,
4320 Sx_close_connection, 1, 1, 0,
4321 doc: /* Close the connection to TERMINAL's X server.
4322 For TERMINAL, specify a terminal object, a frame or a display name (a
4323 string). If TERMINAL is nil, that stands for the selected frame's
4324 terminal. */)
4325 (Lisp_Object terminal)
4327 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4329 if (dpyinfo->reference_count > 0)
4330 error ("Display still has frames on it");
4332 x_delete_terminal (dpyinfo->terminal);
4334 return Qnil;
4337 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4338 doc: /* Return the list of display names that Emacs has connections to. */)
4339 (void)
4341 Lisp_Object tail, result;
4343 result = Qnil;
4344 for (tail = x_display_name_list; CONSP (tail); tail = XCDR (tail))
4345 result = Fcons (XCAR (XCAR (tail)), result);
4347 return result;
4350 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4351 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4352 This function only has an effect on X Windows. With MS Windows, it is
4353 defined but does nothing.
4355 If ON is nil, allow buffering of requests.
4356 Turning on synchronization prohibits the Xlib routines from buffering
4357 requests and seriously degrades performance, but makes debugging much
4358 easier.
4359 The optional second argument TERMINAL specifies which display to act on.
4360 TERMINAL should be a terminal object, a frame or a display name (a string).
4361 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4362 (Lisp_Object on, Lisp_Object terminal)
4364 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4366 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4368 return Qnil;
4371 /* Wait for responses to all X commands issued so far for frame F. */
4373 void
4374 x_sync (struct frame *f)
4376 block_input ();
4377 XSync (FRAME_X_DISPLAY (f), False);
4378 unblock_input ();
4382 /***********************************************************************
4383 Window properties
4384 ***********************************************************************/
4386 DEFUN ("x-change-window-property", Fx_change_window_property,
4387 Sx_change_window_property, 2, 6, 0,
4388 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4389 PROP must be a string. VALUE may be a string or a list of conses,
4390 numbers and/or strings. If an element in the list is a string, it is
4391 converted to an atom and the value of the atom is used. If an element
4392 is a cons, it is converted to a 32 bit number where the car is the 16
4393 top bits and the cdr is the lower 16 bits.
4395 FRAME nil or omitted means use the selected frame.
4396 If TYPE is given and non-nil, it is the name of the type of VALUE.
4397 If TYPE is not given or nil, the type is STRING.
4398 FORMAT gives the size in bits of each element if VALUE is a list.
4399 It must be one of 8, 16 or 32.
4400 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4401 If OUTER-P is non-nil, the property is changed for the outer X window of
4402 FRAME. Default is to change on the edit X window. */)
4403 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4404 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4406 struct frame *f = decode_window_system_frame (frame);
4407 Atom prop_atom;
4408 Atom target_type = XA_STRING;
4409 int element_format = 8;
4410 unsigned char *data;
4411 int nelements;
4412 Window w;
4414 CHECK_STRING (prop);
4416 if (! NILP (format))
4418 CHECK_NUMBER (format);
4420 if (XINT (format) != 8 && XINT (format) != 16
4421 && XINT (format) != 32)
4422 error ("FORMAT must be one of 8, 16 or 32");
4423 element_format = XINT (format);
4426 if (CONSP (value))
4428 ptrdiff_t elsize;
4430 nelements = x_check_property_data (value);
4431 if (nelements == -1)
4432 error ("Bad data in VALUE, must be number, string or cons");
4434 /* The man page for XChangeProperty:
4435 "If the specified format is 32, the property data must be a
4436 long array."
4437 This applies even if long is more than 32 bits. The X library
4438 converts to 32 bits before sending to the X server. */
4439 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4440 data = xnmalloc (nelements, elsize);
4442 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4444 else
4446 CHECK_STRING (value);
4447 data = SDATA (value);
4448 if (INT_MAX < SBYTES (value))
4449 error ("VALUE too long");
4450 nelements = SBYTES (value);
4453 block_input ();
4454 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4455 if (! NILP (type))
4457 CHECK_STRING (type);
4458 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4461 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4462 else w = FRAME_X_WINDOW (f);
4464 XChangeProperty (FRAME_X_DISPLAY (f), w,
4465 prop_atom, target_type, element_format, PropModeReplace,
4466 data, nelements);
4468 if (CONSP (value)) xfree (data);
4470 /* Make sure the property is set when we return. */
4471 XFlush (FRAME_X_DISPLAY (f));
4472 unblock_input ();
4474 return value;
4478 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4479 Sx_delete_window_property, 1, 2, 0,
4480 doc: /* Remove window property PROP from X window of FRAME.
4481 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4482 (Lisp_Object prop, Lisp_Object frame)
4484 struct frame *f = decode_window_system_frame (frame);
4485 Atom prop_atom;
4487 CHECK_STRING (prop);
4488 block_input ();
4489 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4490 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4492 /* Make sure the property is removed when we return. */
4493 XFlush (FRAME_X_DISPLAY (f));
4494 unblock_input ();
4496 return prop;
4500 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4501 1, 6, 0,
4502 doc: /* Value is the value of window property PROP on FRAME.
4503 If FRAME is nil or omitted, use the selected frame.
4505 On X Windows, the following optional arguments are also accepted:
4506 If TYPE is nil or omitted, get the property as a string.
4507 Otherwise TYPE is the name of the atom that denotes the type expected.
4508 If SOURCE is non-nil, get the property on that window instead of from
4509 FRAME. The number 0 denotes the root window.
4510 If DELETE-P is non-nil, delete the property after retrieving it.
4511 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4513 On MS Windows, this function accepts but ignores those optional arguments.
4515 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4516 no value of TYPE (always string in the MS Windows case). */)
4517 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4518 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4520 struct frame *f = decode_window_system_frame (frame);
4521 Atom prop_atom;
4522 int rc;
4523 Lisp_Object prop_value = Qnil;
4524 unsigned char *tmp_data = NULL;
4525 Atom actual_type;
4526 Atom target_type = XA_STRING;
4527 int actual_format;
4528 unsigned long actual_size, bytes_remaining;
4529 Window target_window = FRAME_X_WINDOW (f);
4530 struct gcpro gcpro1;
4532 GCPRO1 (prop_value);
4533 CHECK_STRING (prop);
4535 if (! NILP (source))
4537 CONS_TO_INTEGER (source, Window, target_window);
4538 if (! target_window)
4539 target_window = FRAME_DISPLAY_INFO (f)->root_window;
4542 block_input ();
4543 if (STRINGP (type))
4545 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4546 target_type = AnyPropertyType;
4547 else
4548 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4551 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4552 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4553 prop_atom, 0, 0, False, target_type,
4554 &actual_type, &actual_format, &actual_size,
4555 &bytes_remaining, &tmp_data);
4556 if (rc == Success)
4558 int size = bytes_remaining;
4560 XFree (tmp_data);
4561 tmp_data = NULL;
4563 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4564 prop_atom, 0, bytes_remaining,
4565 ! NILP (delete_p), target_type,
4566 &actual_type, &actual_format,
4567 &actual_size, &bytes_remaining,
4568 &tmp_data);
4569 if (rc == Success && tmp_data)
4571 /* The man page for XGetWindowProperty says:
4572 "If the returned format is 32, the returned data is represented
4573 as a long array and should be cast to that type to obtain the
4574 elements."
4575 This applies even if long is more than 32 bits, the X library
4576 converts from 32 bit elements received from the X server to long
4577 and passes the long array to us. Thus, for that case memcpy can not
4578 be used. We convert to a 32 bit type here, because so much code
4579 assume on that.
4581 The bytes and offsets passed to XGetWindowProperty refers to the
4582 property and those are indeed in 32 bit quantities if format is
4583 32. */
4585 if (BITS_PER_LONG > 32 && actual_format == 32)
4587 unsigned long i;
4588 int *idata = (int *) tmp_data;
4589 long *ldata = (long *) tmp_data;
4591 for (i = 0; i < actual_size; ++i)
4592 idata[i] = (int) ldata[i];
4595 if (NILP (vector_ret_p))
4596 prop_value = make_string ((char *) tmp_data, size);
4597 else
4598 prop_value = x_property_data_to_lisp (f,
4599 tmp_data,
4600 actual_type,
4601 actual_format,
4602 actual_size);
4605 if (tmp_data) XFree (tmp_data);
4608 unblock_input ();
4609 UNGCPRO;
4610 return prop_value;
4615 /***********************************************************************
4616 Busy cursor
4617 ***********************************************************************/
4619 /* Timer function of hourglass_atimer. TIMER is equal to
4620 hourglass_atimer.
4622 Display an hourglass pointer on all frames by mapping the frames'
4623 hourglass_window. Set the hourglass_p flag in the frames'
4624 output_data.x structure to indicate that an hourglass cursor is
4625 shown on the frames. */
4627 void
4628 show_hourglass (struct atimer *timer)
4630 /* The timer implementation will cancel this timer automatically
4631 after this function has run. Set hourglass_atimer to null
4632 so that we know the timer doesn't have to be canceled. */
4633 hourglass_atimer = NULL;
4635 if (!hourglass_shown_p)
4637 Lisp_Object rest, frame;
4639 block_input ();
4641 FOR_EACH_FRAME (rest, frame)
4643 struct frame *f = XFRAME (frame);
4645 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4647 Display *dpy = FRAME_X_DISPLAY (f);
4649 #ifdef USE_X_TOOLKIT
4650 if (f->output_data.x->widget)
4651 #else
4652 if (FRAME_OUTER_WINDOW (f))
4653 #endif
4655 f->output_data.x->hourglass_p = 1;
4657 if (!f->output_data.x->hourglass_window)
4659 unsigned long mask = CWCursor;
4660 XSetWindowAttributes attrs;
4661 #ifdef USE_GTK
4662 Window parent = FRAME_X_WINDOW (f);
4663 #else
4664 Window parent = FRAME_OUTER_WINDOW (f);
4665 #endif
4666 attrs.cursor = f->output_data.x->hourglass_cursor;
4668 f->output_data.x->hourglass_window
4669 = XCreateWindow (dpy, parent,
4670 0, 0, 32000, 32000, 0, 0,
4671 InputOnly,
4672 CopyFromParent,
4673 mask, &attrs);
4676 XMapRaised (dpy, f->output_data.x->hourglass_window);
4677 XFlush (dpy);
4682 hourglass_shown_p = 1;
4683 unblock_input ();
4688 /* Hide the hourglass pointer on all frames, if it is currently
4689 shown. */
4691 void
4692 hide_hourglass (void)
4694 if (hourglass_shown_p)
4696 Lisp_Object rest, frame;
4698 block_input ();
4699 FOR_EACH_FRAME (rest, frame)
4701 struct frame *f = XFRAME (frame);
4703 if (FRAME_X_P (f)
4704 /* Watch out for newly created frames. */
4705 && f->output_data.x->hourglass_window)
4707 XUnmapWindow (FRAME_X_DISPLAY (f),
4708 f->output_data.x->hourglass_window);
4709 /* Sync here because XTread_socket looks at the
4710 hourglass_p flag that is reset to zero below. */
4711 XSync (FRAME_X_DISPLAY (f), False);
4712 f->output_data.x->hourglass_p = 0;
4716 hourglass_shown_p = 0;
4717 unblock_input ();
4723 /***********************************************************************
4724 Tool tips
4725 ***********************************************************************/
4727 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4728 Lisp_Object, Lisp_Object);
4729 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4730 Lisp_Object, int, int, int *, int *);
4732 /* The frame of a currently visible tooltip. */
4734 Lisp_Object tip_frame;
4736 /* If non-nil, a timer started that hides the last tooltip when it
4737 fires. */
4739 static Lisp_Object tip_timer;
4740 Window tip_window;
4742 /* If non-nil, a vector of 3 elements containing the last args
4743 with which x-show-tip was called. See there. */
4745 static Lisp_Object last_show_tip_args;
4748 static void
4749 unwind_create_tip_frame (Lisp_Object frame)
4751 Lisp_Object deleted;
4753 deleted = unwind_create_frame (frame);
4754 if (EQ (deleted, Qt))
4756 tip_window = None;
4757 tip_frame = Qnil;
4762 /* Create a frame for a tooltip on the display described by DPYINFO.
4763 PARMS is a list of frame parameters. TEXT is the string to
4764 display in the tip frame. Value is the frame.
4766 Note that functions called here, esp. x_default_parameter can
4767 signal errors, for instance when a specified color name is
4768 undefined. We have to make sure that we're in a consistent state
4769 when this happens. */
4771 static Lisp_Object
4772 x_create_tip_frame (struct x_display_info *dpyinfo,
4773 Lisp_Object parms,
4774 Lisp_Object text)
4776 struct frame *f;
4777 Lisp_Object frame;
4778 Lisp_Object name;
4779 int width, height;
4780 ptrdiff_t count = SPECPDL_INDEX ();
4781 struct gcpro gcpro1, gcpro2, gcpro3;
4782 int face_change_count_before = face_change_count;
4783 Lisp_Object buffer;
4784 struct buffer *old_buffer;
4786 if (!dpyinfo->terminal->name)
4787 error ("Terminal is not live, can't create new frames on it");
4789 parms = Fcopy_alist (parms);
4791 /* Get the name of the frame to use for resource lookup. */
4792 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4793 if (!STRINGP (name)
4794 && !EQ (name, Qunbound)
4795 && !NILP (name))
4796 error ("Invalid frame name--not a string or nil");
4798 frame = Qnil;
4799 GCPRO3 (parms, name, frame);
4800 f = make_frame (1);
4801 XSETFRAME (frame, f);
4803 buffer = Fget_buffer_create (build_string (" *tip*"));
4804 /* Use set_window_buffer instead of Fset_window_buffer (see
4805 discussion of bug#11984, bug#12025, bug#12026). */
4806 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
4807 old_buffer = current_buffer;
4808 set_buffer_internal_1 (XBUFFER (buffer));
4809 bset_truncate_lines (current_buffer, Qnil);
4810 specbind (Qinhibit_read_only, Qt);
4811 specbind (Qinhibit_modification_hooks, Qt);
4812 Ferase_buffer ();
4813 Finsert (1, &text);
4814 set_buffer_internal_1 (old_buffer);
4816 record_unwind_protect (unwind_create_tip_frame, frame);
4818 f->terminal = dpyinfo->terminal;
4820 /* By setting the output method, we're essentially saying that
4821 the frame is live, as per FRAME_LIVE_P. If we get a signal
4822 from this point on, x_destroy_window might screw up reference
4823 counts etc. */
4824 f->output_method = output_x_window;
4825 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4826 f->output_data.x->icon_bitmap = -1;
4827 FRAME_FONTSET (f) = -1;
4828 f->output_data.x->scroll_bar_foreground_pixel = -1;
4829 f->output_data.x->scroll_bar_background_pixel = -1;
4830 #ifdef USE_TOOLKIT_SCROLL_BARS
4831 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4832 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4833 #endif /* USE_TOOLKIT_SCROLL_BARS */
4834 fset_icon_name (f, Qnil);
4835 FRAME_DISPLAY_INFO (f) = dpyinfo;
4836 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4837 f->output_data.x->explicit_parent = 0;
4839 /* These colors will be set anyway later, but it's important
4840 to get the color reference counts right, so initialize them! */
4842 Lisp_Object black;
4843 struct gcpro gcpro1;
4845 /* Function x_decode_color can signal an error. Make
4846 sure to initialize color slots so that we won't try
4847 to free colors we haven't allocated. */
4848 FRAME_FOREGROUND_PIXEL (f) = -1;
4849 FRAME_BACKGROUND_PIXEL (f) = -1;
4850 f->output_data.x->cursor_pixel = -1;
4851 f->output_data.x->cursor_foreground_pixel = -1;
4852 f->output_data.x->border_pixel = -1;
4853 f->output_data.x->mouse_pixel = -1;
4855 black = build_string ("black");
4856 GCPRO1 (black);
4857 FRAME_FOREGROUND_PIXEL (f)
4858 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4859 FRAME_BACKGROUND_PIXEL (f)
4860 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4861 f->output_data.x->cursor_pixel
4862 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4863 f->output_data.x->cursor_foreground_pixel
4864 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4865 f->output_data.x->border_pixel
4866 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4867 f->output_data.x->mouse_pixel
4868 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4869 UNGCPRO;
4872 /* Set the name; the functions to which we pass f expect the name to
4873 be set. */
4874 if (EQ (name, Qunbound) || NILP (name))
4876 fset_name (f, build_string (dpyinfo->x_id_name));
4877 f->explicit_name = 0;
4879 else
4881 fset_name (f, name);
4882 f->explicit_name = 1;
4883 /* use the frame's title when getting resources for this frame. */
4884 specbind (Qx_resource_name, name);
4887 register_font_driver (&xfont_driver, f);
4888 #ifdef HAVE_FREETYPE
4889 #ifdef HAVE_XFT
4890 register_font_driver (&xftfont_driver, f);
4891 #else /* not HAVE_XFT */
4892 register_font_driver (&ftxfont_driver, f);
4893 #endif /* not HAVE_XFT */
4894 #endif /* HAVE_FREETYPE */
4896 x_default_parameter (f, parms, Qfont_backend, Qnil,
4897 "fontBackend", "FontBackend", RES_TYPE_STRING);
4899 /* Extract the window parameters from the supplied values that are
4900 needed to determine window geometry. */
4901 x_default_font_parameter (f, parms);
4903 x_default_parameter (f, parms, Qborder_width, make_number (0),
4904 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4906 /* This defaults to 2 in order to match xterm. We recognize either
4907 internalBorderWidth or internalBorder (which is what xterm calls
4908 it). */
4909 if (NILP (Fassq (Qinternal_border_width, parms)))
4911 Lisp_Object value;
4913 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4914 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4915 if (! EQ (value, Qunbound))
4916 parms = Fcons (Fcons (Qinternal_border_width, value),
4917 parms);
4920 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4921 "internalBorderWidth", "internalBorderWidth",
4922 RES_TYPE_NUMBER);
4924 /* Also do the stuff which must be set before the window exists. */
4925 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4926 "foreground", "Foreground", RES_TYPE_STRING);
4927 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4928 "background", "Background", RES_TYPE_STRING);
4929 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4930 "pointerColor", "Foreground", RES_TYPE_STRING);
4931 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4932 "cursorColor", "Foreground", RES_TYPE_STRING);
4933 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4934 "borderColor", "BorderColor", RES_TYPE_STRING);
4936 #ifdef GLYPH_DEBUG
4937 image_cache_refcount =
4938 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4939 dpyinfo_refcount = dpyinfo->reference_count;
4940 #endif /* GLYPH_DEBUG */
4942 /* Init faces before x_default_parameter is called for scroll-bar
4943 parameters because that function calls x_set_scroll_bar_width,
4944 which calls change_frame_size, which calls Fset_window_buffer,
4945 which runs hooks, which call Fvertical_motion. At the end, we
4946 end up in init_iterator with a null face cache, which should not
4947 happen. */
4948 init_frame_faces (f);
4950 f->output_data.x->parent_desc = FRAME_DISPLAY_INFO (f)->root_window;
4952 x_figure_window_size (f, parms, 0);
4955 XSetWindowAttributes attrs;
4956 unsigned long mask;
4957 Atom type = FRAME_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
4959 block_input ();
4960 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4961 if (DoesSaveUnders (dpyinfo->screen))
4962 mask |= CWSaveUnder;
4964 /* Window managers look at the override-redirect flag to determine
4965 whether or net to give windows a decoration (Xlib spec, chapter
4966 3.2.8). */
4967 attrs.override_redirect = True;
4968 attrs.save_under = True;
4969 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4970 /* Arrange for getting MapNotify and UnmapNotify events. */
4971 attrs.event_mask = StructureNotifyMask;
4972 tip_window
4973 = FRAME_X_WINDOW (f)
4974 = XCreateWindow (FRAME_X_DISPLAY (f),
4975 FRAME_DISPLAY_INFO (f)->root_window,
4976 /* x, y, width, height */
4977 0, 0, 1, 1,
4978 /* Border. */
4979 f->border_width,
4980 CopyFromParent, InputOutput, CopyFromParent,
4981 mask, &attrs);
4982 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
4983 FRAME_DISPLAY_INFO (f)->Xatom_net_window_type,
4984 XA_ATOM, 32, PropModeReplace,
4985 (unsigned char *)&type, 1);
4986 unblock_input ();
4989 x_make_gc (f);
4991 x_default_parameter (f, parms, Qauto_raise, Qnil,
4992 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4993 x_default_parameter (f, parms, Qauto_lower, Qnil,
4994 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4995 x_default_parameter (f, parms, Qcursor_type, Qbox,
4996 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4998 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4999 Change will not be effected unless different from the current
5000 FRAME_LINES (f). */
5001 width = FRAME_COLS (f);
5002 height = FRAME_LINES (f);
5003 SET_FRAME_COLS (f, 0);
5004 FRAME_LINES (f) = 0;
5005 change_frame_size (f, height, width, 1, 0, 0);
5007 /* Add `tooltip' frame parameter's default value. */
5008 if (NILP (Fframe_parameter (frame, Qtooltip)))
5009 Fmodify_frame_parameters (frame, list1 (Fcons (Qtooltip, Qt)));
5011 /* FIXME - can this be done in a similar way to normal frames?
5012 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
5014 /* Set the `display-type' frame parameter before setting up faces. */
5016 Lisp_Object disptype;
5018 if (FRAME_DISPLAY_INFO (f)->n_planes == 1)
5019 disptype = intern ("mono");
5020 else if (FRAME_DISPLAY_INFO (f)->visual->class == GrayScale
5021 || FRAME_DISPLAY_INFO (f)->visual->class == StaticGray)
5022 disptype = intern ("grayscale");
5023 else
5024 disptype = intern ("color");
5026 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
5027 Fmodify_frame_parameters (frame, list1 (Fcons (Qdisplay_type, disptype)));
5030 /* Set up faces after all frame parameters are known. This call
5031 also merges in face attributes specified for new frames.
5033 Frame parameters may be changed if .Xdefaults contains
5034 specifications for the default font. For example, if there is an
5035 `Emacs.default.attributeBackground: pink', the `background-color'
5036 attribute of the frame get's set, which let's the internal border
5037 of the tooltip frame appear in pink. Prevent this. */
5039 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
5041 /* Set tip_frame here, so that */
5042 tip_frame = frame;
5043 call2 (Qface_set_after_frame_default, frame, Qnil);
5045 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5046 Fmodify_frame_parameters (frame, list1 (Fcons (Qbackground_color, bg)));
5049 f->no_split = 1;
5051 UNGCPRO;
5053 /* Now that the frame will be official, it counts as a reference to
5054 its display and terminal. */
5055 FRAME_DISPLAY_INFO (f)->reference_count++;
5056 f->terminal->reference_count++;
5058 /* It is now ok to make the frame official even if we get an error
5059 below. And the frame needs to be on Vframe_list or making it
5060 visible won't work. */
5061 Vframe_list = Fcons (frame, Vframe_list);
5064 /* Setting attributes of faces of the tooltip frame from resources
5065 and similar will increment face_change_count, which leads to the
5066 clearing of all current matrices. Since this isn't necessary
5067 here, avoid it by resetting face_change_count to the value it
5068 had before we created the tip frame. */
5069 face_change_count = face_change_count_before;
5071 /* Discard the unwind_protect. */
5072 return unbind_to (count, frame);
5076 /* Compute where to display tip frame F. PARMS is the list of frame
5077 parameters for F. DX and DY are specified offsets from the current
5078 location of the mouse. WIDTH and HEIGHT are the width and height
5079 of the tooltip. Return coordinates relative to the root window of
5080 the display in *ROOT_X, and *ROOT_Y. */
5082 static void
5083 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)
5085 Lisp_Object left, top;
5086 int win_x, win_y;
5087 Window root, child;
5088 unsigned pmask;
5090 /* User-specified position? */
5091 left = Fcdr (Fassq (Qleft, parms));
5092 top = Fcdr (Fassq (Qtop, parms));
5094 /* Move the tooltip window where the mouse pointer is. Resize and
5095 show it. */
5096 if (!INTEGERP (left) || !INTEGERP (top))
5098 block_input ();
5099 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_DISPLAY_INFO (f)->root_window,
5100 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5101 unblock_input ();
5104 if (INTEGERP (top))
5105 *root_y = XINT (top);
5106 else if (*root_y + XINT (dy) <= 0)
5107 *root_y = 0; /* Can happen for negative dy */
5108 else if (*root_y + XINT (dy) + height
5109 <= x_display_pixel_height (FRAME_DISPLAY_INFO (f)))
5110 /* It fits below the pointer */
5111 *root_y += XINT (dy);
5112 else if (height + XINT (dy) <= *root_y)
5113 /* It fits above the pointer. */
5114 *root_y -= height + XINT (dy);
5115 else
5116 /* Put it on the top. */
5117 *root_y = 0;
5119 if (INTEGERP (left))
5120 *root_x = XINT (left);
5121 else if (*root_x + XINT (dx) <= 0)
5122 *root_x = 0; /* Can happen for negative dx */
5123 else if (*root_x + XINT (dx) + width
5124 <= x_display_pixel_width (FRAME_DISPLAY_INFO (f)))
5125 /* It fits to the right of the pointer. */
5126 *root_x += XINT (dx);
5127 else if (width + XINT (dx) <= *root_x)
5128 /* It fits to the left of the pointer. */
5129 *root_x -= width + XINT (dx);
5130 else
5131 /* Put it left-justified on the screen--it ought to fit that way. */
5132 *root_x = 0;
5136 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5137 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5138 A tooltip window is a small X window displaying a string.
5140 This is an internal function; Lisp code should call `tooltip-show'.
5142 FRAME nil or omitted means use the selected frame.
5144 PARMS is an optional list of frame parameters which can be used to
5145 change the tooltip's appearance.
5147 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5148 means use the default timeout of 5 seconds.
5150 If the list of frame parameters PARMS contains a `left' parameters,
5151 the tooltip is displayed at that x-position. Otherwise it is
5152 displayed at the mouse position, with offset DX added (default is 5 if
5153 DX isn't specified). Likewise for the y-position; if a `top' frame
5154 parameter is specified, it determines the y-position of the tooltip
5155 window, otherwise it is displayed at the mouse position, with offset
5156 DY added (default is -10).
5158 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5159 Text larger than the specified size is clipped. */)
5160 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
5162 struct frame *f;
5163 struct window *w;
5164 int root_x, root_y;
5165 struct buffer *old_buffer;
5166 struct text_pos pos;
5167 int i, width, height, seen_reversed_p;
5168 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5169 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5170 ptrdiff_t count = SPECPDL_INDEX ();
5172 specbind (Qinhibit_redisplay, Qt);
5174 GCPRO4 (string, parms, frame, timeout);
5176 CHECK_STRING (string);
5177 if (SCHARS (string) == 0)
5178 string = make_unibyte_string (" ", 1);
5180 f = decode_window_system_frame (frame);
5181 if (NILP (timeout))
5182 timeout = make_number (5);
5183 else
5184 CHECK_NATNUM (timeout);
5186 if (NILP (dx))
5187 dx = make_number (5);
5188 else
5189 CHECK_NUMBER (dx);
5191 if (NILP (dy))
5192 dy = make_number (-10);
5193 else
5194 CHECK_NUMBER (dy);
5196 #ifdef USE_GTK
5197 if (x_gtk_use_system_tooltips)
5199 bool ok;
5201 /* Hide a previous tip, if any. */
5202 Fx_hide_tip ();
5204 block_input ();
5205 ok = xg_prepare_tooltip (f, string, &width, &height);
5206 if (ok)
5208 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5209 xg_show_tooltip (f, root_x, root_y);
5210 /* This is used in Fx_hide_tip. */
5211 XSETFRAME (tip_frame, f);
5213 unblock_input ();
5214 if (ok) goto start_timer;
5216 #endif /* USE_GTK */
5218 if (NILP (last_show_tip_args))
5219 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5221 if (!NILP (tip_frame))
5223 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5224 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5225 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5227 if (EQ (frame, last_frame)
5228 && !NILP (Fequal (last_string, string))
5229 && !NILP (Fequal (last_parms, parms)))
5231 struct frame *tip_f = XFRAME (tip_frame);
5233 /* Only DX and DY have changed. */
5234 if (!NILP (tip_timer))
5236 Lisp_Object timer = tip_timer;
5237 tip_timer = Qnil;
5238 call1 (Qcancel_timer, timer);
5241 block_input ();
5242 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5243 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5244 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5245 root_x, root_y);
5246 unblock_input ();
5247 goto start_timer;
5251 /* Hide a previous tip, if any. */
5252 Fx_hide_tip ();
5254 ASET (last_show_tip_args, 0, string);
5255 ASET (last_show_tip_args, 1, frame);
5256 ASET (last_show_tip_args, 2, parms);
5258 /* Add default values to frame parameters. */
5259 if (NILP (Fassq (Qname, parms)))
5260 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5261 if (NILP (Fassq (Qinternal_border_width, parms)))
5262 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5263 if (NILP (Fassq (Qborder_width, parms)))
5264 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5265 if (NILP (Fassq (Qborder_color, parms)))
5266 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5267 if (NILP (Fassq (Qbackground_color, parms)))
5268 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5269 parms);
5271 /* Create a frame for the tooltip, and record it in the global
5272 variable tip_frame. */
5273 frame = x_create_tip_frame (FRAME_DISPLAY_INFO (f), parms, string);
5274 f = XFRAME (frame);
5276 /* Set up the frame's root window. */
5277 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5278 w->left_col = 0;
5279 w->top_line = 0;
5281 if (CONSP (Vx_max_tooltip_size)
5282 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5283 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5285 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
5286 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
5288 else
5290 w->total_cols = 80;
5291 w->total_lines = 40;
5294 FRAME_TOTAL_COLS (f) = w->total_cols;
5295 adjust_frame_glyphs (f);
5296 w->pseudo_window_p = 1;
5298 /* Display the tooltip text in a temporary buffer. */
5299 old_buffer = current_buffer;
5300 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5301 bset_truncate_lines (current_buffer, Qnil);
5302 clear_glyph_matrix (w->desired_matrix);
5303 clear_glyph_matrix (w->current_matrix);
5304 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5305 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5307 /* Compute width and height of the tooltip. */
5308 width = height = seen_reversed_p = 0;
5309 for (i = 0; i < w->desired_matrix->nrows; ++i)
5311 struct glyph_row *row = &w->desired_matrix->rows[i];
5312 struct glyph *last;
5313 int row_width;
5315 /* Stop at the first empty row at the end. */
5316 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5317 break;
5319 /* Let the row go over the full width of the frame. */
5320 row->full_width_p = 1;
5322 row_width = row->pixel_width;
5323 if (row->used[TEXT_AREA])
5325 /* There's a glyph at the end of rows that is used to place
5326 the cursor there. Don't include the width of this glyph. */
5327 if (!row->reversed_p)
5329 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5330 if (INTEGERP (last->object))
5331 row_width -= last->pixel_width;
5333 else
5335 /* There could be a stretch glyph at the beginning of R2L
5336 rows that is produced by extend_face_to_end_of_line.
5337 Don't count that glyph. */
5338 struct glyph *g = row->glyphs[TEXT_AREA];
5340 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5342 row_width -= g->pixel_width;
5343 seen_reversed_p = 1;
5348 height += row->height;
5349 width = max (width, row_width);
5352 /* If we've seen partial-length R2L rows, we need to re-adjust the
5353 tool-tip frame width and redisplay it again, to avoid over-wide
5354 tips due to the stretch glyph that extends R2L lines to full
5355 width of the frame. */
5356 if (seen_reversed_p)
5358 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5359 not in pixels. */
5360 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5361 w->total_cols = width;
5362 FRAME_TOTAL_COLS (f) = width;
5363 adjust_frame_glyphs (f);
5364 clear_glyph_matrix (w->desired_matrix);
5365 clear_glyph_matrix (w->current_matrix);
5366 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5367 width = height = 0;
5368 /* Recompute width and height of the tooltip. */
5369 for (i = 0; i < w->desired_matrix->nrows; ++i)
5371 struct glyph_row *row = &w->desired_matrix->rows[i];
5372 struct glyph *last;
5373 int row_width;
5375 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5376 break;
5377 row->full_width_p = 1;
5378 row_width = row->pixel_width;
5379 if (row->used[TEXT_AREA] && !row->reversed_p)
5381 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5382 if (INTEGERP (last->object))
5383 row_width -= last->pixel_width;
5386 height += row->height;
5387 width = max (width, row_width);
5391 /* Add the frame's internal border to the width and height the X
5392 window should have. */
5393 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5394 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5396 /* Move the tooltip window where the mouse pointer is. Resize and
5397 show it. */
5398 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5400 block_input ();
5401 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5402 root_x, root_y, width, height);
5403 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5404 unblock_input ();
5406 /* Draw into the window. */
5407 w->must_be_updated_p = 1;
5408 update_single_window (w, 1);
5410 /* Restore original current buffer. */
5411 set_buffer_internal_1 (old_buffer);
5412 windows_or_buffers_changed = old_windows_or_buffers_changed;
5414 start_timer:
5415 /* Let the tip disappear after timeout seconds. */
5416 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5417 intern ("x-hide-tip"));
5419 UNGCPRO;
5420 return unbind_to (count, Qnil);
5424 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5425 doc: /* Hide the current tooltip window, if there is any.
5426 Value is t if tooltip was open, nil otherwise. */)
5427 (void)
5429 ptrdiff_t count;
5430 Lisp_Object deleted, frame, timer;
5431 struct gcpro gcpro1, gcpro2;
5433 /* Return quickly if nothing to do. */
5434 if (NILP (tip_timer) && NILP (tip_frame))
5435 return Qnil;
5437 frame = tip_frame;
5438 timer = tip_timer;
5439 GCPRO2 (frame, timer);
5440 tip_frame = tip_timer = deleted = Qnil;
5442 count = SPECPDL_INDEX ();
5443 specbind (Qinhibit_redisplay, Qt);
5444 specbind (Qinhibit_quit, Qt);
5446 if (!NILP (timer))
5447 call1 (Qcancel_timer, timer);
5449 #ifdef USE_GTK
5451 /* When using system tooltip, tip_frame is the Emacs frame on which
5452 the tip is shown. */
5453 struct frame *f = XFRAME (frame);
5454 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5455 frame = Qnil;
5457 #endif
5459 if (FRAMEP (frame))
5461 delete_frame (frame, Qnil);
5462 deleted = Qt;
5464 #ifdef USE_LUCID
5465 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5466 redisplay procedure is not called when a tip frame over menu
5467 items is unmapped. Redisplay the menu manually... */
5469 Widget w;
5470 struct frame *f = SELECTED_FRAME ();
5471 w = f->output_data.x->menubar_widget;
5473 if (!DoesSaveUnders (FRAME_DISPLAY_INFO (f)->screen)
5474 && w != NULL)
5476 block_input ();
5477 xlwmenu_redisplay (w);
5478 unblock_input ();
5481 #endif /* USE_LUCID */
5484 UNGCPRO;
5485 return unbind_to (count, deleted);
5490 /***********************************************************************
5491 File selection dialog
5492 ***********************************************************************/
5494 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5495 Sx_uses_old_gtk_dialog,
5496 0, 0, 0,
5497 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5498 (void)
5500 #ifdef USE_GTK
5501 if (use_dialog_box
5502 && use_file_dialog
5503 && window_system_available (SELECTED_FRAME ())
5504 && xg_uses_old_file_dialog ())
5505 return Qt;
5506 #endif
5507 return Qnil;
5511 #ifdef USE_MOTIF
5512 /* Callback for "OK" and "Cancel" on file selection dialog. */
5514 static void
5515 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5517 int *result = client_data;
5518 XmAnyCallbackStruct *cb = call_data;
5519 *result = cb->reason;
5523 /* Callback for unmapping a file selection dialog. This is used to
5524 capture the case where a dialog is closed via a window manager's
5525 closer button, for example. Using a XmNdestroyCallback didn't work
5526 in this case. */
5528 static void
5529 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5531 int *result = client_data;
5532 *result = XmCR_CANCEL;
5535 static void
5536 clean_up_file_dialog (void *arg)
5538 Widget dialog = arg;
5540 /* Clean up. */
5541 block_input ();
5542 XtUnmanageChild (dialog);
5543 XtDestroyWidget (dialog);
5544 x_menu_set_in_use (0);
5545 unblock_input ();
5549 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5550 doc: /* Read file name, prompting with PROMPT in directory DIR.
5551 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5552 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5553 or directory must exist.
5555 This function is only defined on NS, MS Windows, and X Windows with the
5556 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5557 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5558 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename,
5559 Lisp_Object mustmatch, Lisp_Object only_dir_p)
5561 int result;
5562 struct frame *f = SELECTED_FRAME ();
5563 Lisp_Object file = Qnil;
5564 Lisp_Object decoded_file;
5565 Widget dialog, text, help;
5566 Arg al[10];
5567 int ac = 0;
5568 XmString dir_xmstring, pattern_xmstring;
5569 ptrdiff_t count = SPECPDL_INDEX ();
5570 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5572 check_window_system (f);
5574 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5576 if (popup_activated ())
5577 error ("Trying to use a menu from within a menu-entry");
5579 CHECK_STRING (prompt);
5580 CHECK_STRING (dir);
5582 /* Prevent redisplay. */
5583 specbind (Qinhibit_redisplay, Qt);
5585 block_input ();
5587 /* Create the dialog with PROMPT as title, using DIR as initial
5588 directory and using "*" as pattern. */
5589 dir = Fexpand_file_name (dir, Qnil);
5590 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5591 pattern_xmstring = XmStringCreateLocalized ("*");
5593 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5594 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5595 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5596 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5597 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5598 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5599 "fsb", al, ac);
5600 XmStringFree (dir_xmstring);
5601 XmStringFree (pattern_xmstring);
5603 /* Add callbacks for OK and Cancel. */
5604 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5605 (XtPointer) &result);
5606 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5607 (XtPointer) &result);
5608 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5609 (XtPointer) &result);
5611 /* Remove the help button since we can't display help. */
5612 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5613 XtUnmanageChild (help);
5615 /* Mark OK button as default. */
5616 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5617 XmNshowAsDefault, True, NULL);
5619 /* If MUSTMATCH is non-nil, disable the file entry field of the
5620 dialog, so that the user must select a file from the files list
5621 box. We can't remove it because we wouldn't have a way to get at
5622 the result file name, then. */
5623 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5624 if (!NILP (mustmatch))
5626 Widget label;
5627 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5628 XtSetSensitive (text, False);
5629 XtSetSensitive (label, False);
5632 /* Manage the dialog, so that list boxes get filled. */
5633 XtManageChild (dialog);
5635 if (STRINGP (default_filename))
5637 XmString default_xmstring;
5638 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5639 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5641 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5642 XmTextFieldReplace (wtext, 0, last_pos,
5643 (SSDATA (Ffile_name_nondirectory (default_filename))));
5645 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5646 must include the path for this to work. */
5648 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5650 if (XmListItemExists (list, default_xmstring))
5652 int item_pos = XmListItemPos (list, default_xmstring);
5653 /* Select the item and scroll it into view. */
5654 XmListSelectPos (list, item_pos, True);
5655 XmListSetPos (list, item_pos);
5658 XmStringFree (default_xmstring);
5661 record_unwind_protect_ptr (clean_up_file_dialog, dialog);
5663 /* Process events until the user presses Cancel or OK. */
5664 x_menu_set_in_use (1);
5665 result = 0;
5666 while (result == 0)
5668 XEvent event;
5669 x_menu_wait_for_event (0);
5670 XtAppNextEvent (Xt_app_con, &event);
5671 if (event.type == KeyPress
5672 && FRAME_X_DISPLAY (f) == event.xkey.display)
5674 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5676 /* Pop down on C-g. */
5677 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5678 XtUnmanageChild (dialog);
5681 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5684 /* Get the result. */
5685 if (result == XmCR_OK)
5687 XmString text_string;
5688 String data;
5690 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5691 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5692 XmStringFree (text_string);
5693 file = build_string (data);
5694 XtFree (data);
5696 else
5697 file = Qnil;
5699 unblock_input ();
5700 UNGCPRO;
5702 /* Make "Cancel" equivalent to C-g. */
5703 if (NILP (file))
5704 Fsignal (Qquit, Qnil);
5706 decoded_file = DECODE_FILE (file);
5708 return unbind_to (count, decoded_file);
5711 #endif /* USE_MOTIF */
5713 #ifdef USE_GTK
5715 static void
5716 clean_up_dialog (void)
5718 x_menu_set_in_use (0);
5721 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5722 doc: /* Read file name, prompting with PROMPT in directory DIR.
5723 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5724 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5725 or directory must exist.
5727 This function is only defined on NS, MS Windows, and X Windows with the
5728 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5729 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5730 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5732 struct frame *f = SELECTED_FRAME ();
5733 char *fn;
5734 Lisp_Object file = Qnil;
5735 Lisp_Object decoded_file;
5736 ptrdiff_t count = SPECPDL_INDEX ();
5737 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5738 char *cdef_file;
5740 check_window_system (f);
5742 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5744 if (popup_activated ())
5745 error ("Trying to use a menu from within a menu-entry");
5747 CHECK_STRING (prompt);
5748 CHECK_STRING (dir);
5750 /* Prevent redisplay. */
5751 specbind (Qinhibit_redisplay, Qt);
5752 record_unwind_protect_void (clean_up_dialog);
5754 block_input ();
5756 if (STRINGP (default_filename))
5757 cdef_file = SSDATA (default_filename);
5758 else
5759 cdef_file = SSDATA (dir);
5761 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5762 ! NILP (mustmatch),
5763 ! NILP (only_dir_p));
5765 if (fn)
5767 file = build_string (fn);
5768 xfree (fn);
5771 unblock_input ();
5772 UNGCPRO;
5774 /* Make "Cancel" equivalent to C-g. */
5775 if (NILP (file))
5776 Fsignal (Qquit, Qnil);
5778 decoded_file = DECODE_FILE (file);
5780 return unbind_to (count, decoded_file);
5784 #ifdef HAVE_FREETYPE
5786 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5787 doc: /* Read a font using a GTK dialog.
5788 Return either a font spec (for GTK versions >= 3.2) or a string
5789 containing a GTK-style font name.
5791 FRAME is the frame on which to pop up the font chooser. If omitted or
5792 nil, it defaults to the selected frame. */)
5793 (Lisp_Object frame, Lisp_Object ignored)
5795 struct frame *f = decode_window_system_frame (frame);
5796 Lisp_Object font;
5797 Lisp_Object font_param;
5798 char *default_name = NULL;
5799 struct gcpro gcpro1, gcpro2;
5800 ptrdiff_t count = SPECPDL_INDEX ();
5802 if (popup_activated ())
5803 error ("Trying to use a menu from within a menu-entry");
5805 /* Prevent redisplay. */
5806 specbind (Qinhibit_redisplay, Qt);
5807 record_unwind_protect_void (clean_up_dialog);
5809 block_input ();
5811 GCPRO2 (font_param, font);
5813 XSETFONT (font, FRAME_FONT (f));
5814 font_param = Ffont_get (font, intern (":name"));
5815 if (STRINGP (font_param))
5816 default_name = xstrdup (SSDATA (font_param));
5817 else
5819 font_param = Fframe_parameter (frame, Qfont_param);
5820 if (STRINGP (font_param))
5821 default_name = xstrdup (SSDATA (font_param));
5824 font = xg_get_font (f, default_name);
5825 xfree (default_name);
5827 unblock_input ();
5829 if (NILP (font))
5830 Fsignal (Qquit, Qnil);
5832 return unbind_to (count, font);
5834 #endif /* HAVE_FREETYPE */
5836 #endif /* USE_GTK */
5839 /***********************************************************************
5840 Keyboard
5841 ***********************************************************************/
5843 #ifdef HAVE_XKB
5844 #include <X11/XKBlib.h>
5845 #include <X11/keysym.h>
5846 #endif
5848 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5849 Sx_backspace_delete_keys_p, 0, 1, 0,
5850 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5851 FRAME nil means use the selected frame.
5852 Value is t if we know that both keys are present, and are mapped to the
5853 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5854 present and mapped to the usual X keysyms. */)
5855 (Lisp_Object frame)
5857 #ifndef HAVE_XKB
5858 return Qlambda;
5859 #else
5860 XkbDescPtr kb;
5861 struct frame *f = decode_window_system_frame (frame);
5862 Display *dpy = FRAME_X_DISPLAY (f);
5863 Lisp_Object have_keys;
5864 int major, minor, op, event, error_code;
5866 block_input ();
5868 /* Check library version in case we're dynamically linked. */
5869 major = XkbMajorVersion;
5870 minor = XkbMinorVersion;
5871 if (!XkbLibraryVersion (&major, &minor))
5873 unblock_input ();
5874 return Qlambda;
5877 /* Check that the server supports XKB. */
5878 major = XkbMajorVersion;
5879 minor = XkbMinorVersion;
5880 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
5882 unblock_input ();
5883 return Qlambda;
5886 /* In this code we check that the keyboard has physical keys with names
5887 that start with BKSP (Backspace) and DELE (Delete), and that they
5888 generate keysym XK_BackSpace and XK_Delete respectively.
5889 This function is used to test if normal-erase-is-backspace should be
5890 turned on.
5891 An alternative approach would be to just check if XK_BackSpace and
5892 XK_Delete are mapped to any key. But if any of those are mapped to
5893 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5894 user doesn't know about it, it is better to return false here.
5895 It is more obvious to the user what to do if she/he has two keys
5896 clearly marked with names/symbols and one key does something not
5897 expected (i.e. she/he then tries the other).
5898 The cases where Backspace/Delete is mapped to some other key combination
5899 are rare, and in those cases, normal-erase-is-backspace can be turned on
5900 manually. */
5902 have_keys = Qnil;
5903 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5904 if (kb)
5906 int delete_keycode = 0, backspace_keycode = 0, i;
5908 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5910 for (i = kb->min_key_code;
5911 (i < kb->max_key_code
5912 && (delete_keycode == 0 || backspace_keycode == 0));
5913 ++i)
5915 /* The XKB symbolic key names can be seen most easily in
5916 the PS file generated by `xkbprint -label name
5917 $DISPLAY'. */
5918 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5919 delete_keycode = i;
5920 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5921 backspace_keycode = i;
5924 XkbFreeNames (kb, 0, True);
5927 XkbFreeClientMap (kb, 0, True);
5929 if (delete_keycode
5930 && backspace_keycode
5931 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5932 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5933 have_keys = Qt;
5935 unblock_input ();
5936 return have_keys;
5937 #endif
5942 /***********************************************************************
5943 Initialization
5944 ***********************************************************************/
5946 /* Keep this list in the same order as frame_parms in frame.c.
5947 Use 0 for unsupported frame parameters. */
5949 frame_parm_handler x_frame_parm_handlers[] =
5951 x_set_autoraise,
5952 x_set_autolower,
5953 x_set_background_color,
5954 x_set_border_color,
5955 x_set_border_width,
5956 x_set_cursor_color,
5957 x_set_cursor_type,
5958 x_set_font,
5959 x_set_foreground_color,
5960 x_set_icon_name,
5961 x_set_icon_type,
5962 x_set_internal_border_width,
5963 x_set_menu_bar_lines,
5964 x_set_mouse_color,
5965 x_explicitly_set_name,
5966 x_set_scroll_bar_width,
5967 x_set_title,
5968 x_set_unsplittable,
5969 x_set_vertical_scroll_bars,
5970 x_set_visibility,
5971 x_set_tool_bar_lines,
5972 x_set_scroll_bar_foreground,
5973 x_set_scroll_bar_background,
5974 x_set_screen_gamma,
5975 x_set_line_spacing,
5976 x_set_fringe_width,
5977 x_set_fringe_width,
5978 x_set_wait_for_wm,
5979 x_set_fullscreen,
5980 x_set_font_backend,
5981 x_set_alpha,
5982 x_set_sticky,
5983 x_set_tool_bar_position,
5986 void
5987 syms_of_xfns (void)
5989 /* The section below is built by the lisp expression at the top of the file,
5990 just above where these variables are declared. */
5991 /*&&& init symbols here &&&*/
5992 DEFSYM (Qsuppress_icon, "suppress-icon");
5993 DEFSYM (Qundefined_color, "undefined-color");
5994 DEFSYM (Qcompound_text, "compound-text");
5995 DEFSYM (Qcancel_timer, "cancel-timer");
5996 DEFSYM (Qfont_param, "font-parameter");
5997 /* This is the end of symbol initialization. */
5999 Fput (Qundefined_color, Qerror_conditions,
6000 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
6001 Fput (Qundefined_color, Qerror_message,
6002 build_pure_c_string ("Undefined color"));
6004 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
6005 doc: /* The shape of the pointer when over text.
6006 Changing the value does not affect existing frames
6007 unless you set the mouse color. */);
6008 Vx_pointer_shape = Qnil;
6010 #if 0 /* This doesn't really do anything. */
6011 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
6012 doc: /* The shape of the pointer when not over text.
6013 This variable takes effect when you create a new frame
6014 or when you set the mouse color. */);
6015 #endif
6016 Vx_nontext_pointer_shape = Qnil;
6018 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
6019 doc: /* The shape of the pointer when Emacs is busy.
6020 This variable takes effect when you create a new frame
6021 or when you set the mouse color. */);
6022 Vx_hourglass_pointer_shape = Qnil;
6024 #if 0 /* This doesn't really do anything. */
6025 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
6026 doc: /* The shape of the pointer when over the mode line.
6027 This variable takes effect when you create a new frame
6028 or when you set the mouse color. */);
6029 #endif
6030 Vx_mode_pointer_shape = Qnil;
6032 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
6033 Vx_sensitive_text_pointer_shape,
6034 doc: /* The shape of the pointer when over mouse-sensitive text.
6035 This variable takes effect when you create a new frame
6036 or when you set the mouse color. */);
6037 Vx_sensitive_text_pointer_shape = Qnil;
6039 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
6040 Vx_window_horizontal_drag_shape,
6041 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
6042 This variable takes effect when you create a new frame
6043 or when you set the mouse color. */);
6044 Vx_window_horizontal_drag_shape = Qnil;
6046 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
6047 doc: /* A string indicating the foreground color of the cursor box. */);
6048 Vx_cursor_fore_pixel = Qnil;
6050 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
6051 doc: /* Maximum size for tooltips.
6052 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
6053 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
6055 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
6056 doc: /* Non-nil if no X window manager is in use.
6057 Emacs doesn't try to figure this out; this is always nil
6058 unless you set it to something else. */);
6059 /* We don't have any way to find this out, so set it to nil
6060 and maybe the user would like to set it to t. */
6061 Vx_no_window_manager = Qnil;
6063 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
6064 Vx_pixel_size_width_font_regexp,
6065 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
6067 Since Emacs gets width of a font matching with this regexp from
6068 PIXEL_SIZE field of the name, font finding mechanism gets faster for
6069 such a font. This is especially effective for such large fonts as
6070 Chinese, Japanese, and Korean. */);
6071 Vx_pixel_size_width_font_regexp = Qnil;
6073 /* This is not ifdef:ed, so other builds than GTK can customize it. */
6074 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
6075 doc: /* Non-nil means prompt with the old GTK file selection dialog.
6076 If nil or if the file selection dialog is not available, the new GTK file
6077 chooser is used instead. To turn off all file dialogs set the
6078 variable `use-file-dialog'. */);
6079 x_gtk_use_old_file_dialog = 0;
6081 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
6082 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
6083 Note that this is just the default, there is a toggle button on the file
6084 chooser to show or not show hidden files on a case by case basis. */);
6085 x_gtk_show_hidden_files = 0;
6087 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
6088 doc: /* If non-nil, the GTK file chooser will show additional help text.
6089 If more space for files in the file chooser dialog is wanted, set this to nil
6090 to turn the additional text off. */);
6091 x_gtk_file_dialog_help_text = 1;
6093 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
6094 doc: /* If non-nil, a detached tool bar is shown in full.
6095 The default is to just show an arrow and pressing on that arrow shows
6096 the tool bar buttons. */);
6097 x_gtk_whole_detached_tool_bar = 0;
6099 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
6100 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
6101 Otherwise use Emacs own tooltip implementation.
6102 When using Gtk+ tooltips, the tooltip face is not used. */);
6103 x_gtk_use_system_tooltips = 1;
6105 /* Tell Emacs about this window system. */
6106 Fprovide (Qx, Qnil);
6108 #ifdef USE_X_TOOLKIT
6109 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6110 #ifdef USE_MOTIF
6111 Fprovide (intern_c_string ("motif"), Qnil);
6113 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
6114 doc: /* Version info for LessTif/Motif. */);
6115 Vmotif_version_string = build_string (XmVERSION_STRING);
6116 #endif /* USE_MOTIF */
6117 #endif /* USE_X_TOOLKIT */
6119 #ifdef USE_GTK
6120 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6121 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6122 But for a user it is a toolkit for X, and indeed, configure
6123 accepts --with-x-toolkit=gtk. */
6124 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6125 Fprovide (intern_c_string ("gtk"), Qnil);
6126 Fprovide (intern_c_string ("move-toolbar"), Qnil);
6128 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
6129 doc: /* Version info for GTK+. */);
6131 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
6132 int len = sprintf (gtk_version, "%d.%d.%d",
6133 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6134 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
6136 #endif /* USE_GTK */
6138 /* X window properties. */
6139 defsubr (&Sx_change_window_property);
6140 defsubr (&Sx_delete_window_property);
6141 defsubr (&Sx_window_property);
6143 defsubr (&Sxw_display_color_p);
6144 defsubr (&Sx_display_grayscale_p);
6145 defsubr (&Sxw_color_defined_p);
6146 defsubr (&Sxw_color_values);
6147 defsubr (&Sx_server_max_request_size);
6148 defsubr (&Sx_server_vendor);
6149 defsubr (&Sx_server_version);
6150 defsubr (&Sx_display_pixel_width);
6151 defsubr (&Sx_display_pixel_height);
6152 defsubr (&Sx_display_mm_width);
6153 defsubr (&Sx_display_mm_height);
6154 defsubr (&Sx_display_screens);
6155 defsubr (&Sx_display_planes);
6156 defsubr (&Sx_display_color_cells);
6157 defsubr (&Sx_display_visual_class);
6158 defsubr (&Sx_display_backing_store);
6159 defsubr (&Sx_display_save_under);
6160 defsubr (&Sx_display_monitor_attributes_list);
6161 defsubr (&Sx_wm_set_size_hint);
6162 defsubr (&Sx_create_frame);
6163 defsubr (&Sx_open_connection);
6164 defsubr (&Sx_close_connection);
6165 defsubr (&Sx_display_list);
6166 defsubr (&Sx_synchronize);
6167 defsubr (&Sx_backspace_delete_keys_p);
6169 defsubr (&Sx_show_tip);
6170 defsubr (&Sx_hide_tip);
6171 tip_timer = Qnil;
6172 staticpro (&tip_timer);
6173 tip_frame = Qnil;
6174 staticpro (&tip_frame);
6176 last_show_tip_args = Qnil;
6177 staticpro (&last_show_tip_args);
6179 defsubr (&Sx_uses_old_gtk_dialog);
6180 #if defined (USE_MOTIF) || defined (USE_GTK)
6181 defsubr (&Sx_file_dialog);
6182 #endif
6184 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6185 defsubr (&Sx_select_font);
6186 #endif