Fix last change.
[emacs.git] / src / xfns.c
blob1f98e9fc8c77ccfece5cd6ab9e5ae2ca358c9e57
1 /* Functions for the X window system.
3 Copyright (C) 1989, 1992-2012 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 /* This makes the fields of a Display accessible, in Xlib header files. */
27 #define XLIB_ILLEGAL_ACCESS
29 #include "lisp.h"
30 #include "xterm.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "character.h"
34 #include "buffer.h"
35 #include "intervals.h"
36 #include "dispextern.h"
37 #include "keyboard.h"
38 #include "blockinput.h"
39 #include <epaths.h>
40 #include "charset.h"
41 #include "coding.h"
42 #include "fontset.h"
43 #include "systime.h"
44 #include "termhooks.h"
45 #include "atimer.h"
46 #include "termchar.h"
47 #include "font.h"
49 #ifdef HAVE_X_WINDOWS
51 #include <sys/types.h>
52 #include <sys/stat.h>
54 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
55 #include "bitmaps/gray.xbm"
56 #else
57 #include <X11/bitmaps/gray>
58 #endif
60 #include "xsettings.h"
62 #ifdef USE_GTK
63 #include "gtkutil.h"
64 #endif
66 #ifdef USE_X_TOOLKIT
67 #include <X11/Shell.h>
69 #ifndef USE_MOTIF
70 #ifdef HAVE_XAW3D
71 #include <X11/Xaw3d/Paned.h>
72 #include <X11/Xaw3d/Label.h>
73 #else /* !HAVE_XAW3D */
74 #include <X11/Xaw/Paned.h>
75 #include <X11/Xaw/Label.h>
76 #endif /* HAVE_XAW3D */
77 #endif /* USE_MOTIF */
79 #ifdef USG
80 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
81 #include <X11/Xos.h>
82 #define USG
83 #ifdef USG /* Pacify gcc -Wunused-macros. */
84 #endif
85 #else
86 #include <X11/Xos.h>
87 #endif
89 #include "widget.h"
91 #include "../lwlib/lwlib.h"
93 #ifdef USE_MOTIF
94 #include <Xm/Xm.h>
95 #include <Xm/DialogS.h>
96 #include <Xm/FileSB.h>
97 #include <Xm/List.h>
98 #include <Xm/TextF.h>
99 #endif
101 #ifdef USE_LUCID
102 #include "../lwlib/xlwmenu.h"
103 #endif
105 #if !defined (NO_EDITRES)
106 #define HACK_EDITRES
107 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
108 #endif /* not defined NO_EDITRES */
110 /* Unique id counter for widgets created by the Lucid Widget Library. */
112 extern LWLIB_ID widget_id_tick;
114 #ifdef USE_MOTIF
116 #endif /* USE_MOTIF */
118 #endif /* USE_X_TOOLKIT */
120 #ifdef USE_GTK
122 #endif /* USE_GTK */
124 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
126 /* Nonzero if using X. */
128 int x_in_use;
130 static Lisp_Object Qsuppress_icon;
131 static Lisp_Object Qundefined_color;
132 static Lisp_Object Qcompound_text, Qcancel_timer;
133 Lisp_Object Qfont_param;
135 #ifdef GLYPH_DEBUG
136 static ptrdiff_t image_cache_refcount;
137 static int dpyinfo_refcount;
138 #endif
140 static struct x_display_info *x_display_info_for_name (Lisp_Object);
143 /* Error if we are not connected to X. */
145 void
146 check_x (void)
148 if (! x_in_use)
149 error ("X windows are not in use or not initialized");
152 /* Nonzero if we can use mouse menus.
153 You should not call this unless HAVE_MENUS is defined. */
156 have_menus_p (void)
158 return x_in_use;
161 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
162 and checking validity for X. */
164 FRAME_PTR
165 check_x_frame (Lisp_Object frame)
167 struct frame *f = decode_live_frame (frame);
169 if (! FRAME_X_P (f))
170 error ("Non-X frame used");
171 return f;
174 /* Let the user specify an X display with a Lisp object.
175 OBJECT may be nil, a frame or a terminal object.
176 nil stands for the selected frame--or, if that is not an X frame,
177 the first X display on the list. */
179 struct x_display_info *
180 check_x_display_info (Lisp_Object object)
182 struct x_display_info *dpyinfo = NULL;
184 if (NILP (object))
186 struct frame *sf = XFRAME (selected_frame);
188 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
189 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
190 else if (x_display_list != 0)
191 dpyinfo = x_display_list;
192 else
193 error ("X windows are not in use or not initialized");
195 else if (TERMINALP (object))
197 struct terminal *t = get_terminal (object, 1);
199 if (t->type != output_x_window)
200 error ("Terminal %"pI"d is not an X display", XINT (object));
202 dpyinfo = t->display_info.x;
204 else if (STRINGP (object))
205 dpyinfo = x_display_info_for_name (object);
206 else
208 FRAME_PTR f = check_x_frame (object);
209 dpyinfo = FRAME_X_DISPLAY_INFO (f);
212 return dpyinfo;
216 /* Return the Emacs frame-object corresponding to an X window.
217 It could be the frame's main window or an icon window. */
219 /* This function can be called during GC, so use GC_xxx type test macros. */
221 struct frame *
222 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
224 Lisp_Object tail, frame;
225 struct frame *f;
227 if (wdesc == None)
228 return NULL;
230 FOR_EACH_FRAME (tail, frame)
232 f = XFRAME (frame);
233 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
234 continue;
235 if (f->output_data.x->hourglass_window == wdesc)
236 return f;
237 #ifdef USE_X_TOOLKIT
238 if ((f->output_data.x->edit_widget
239 && XtWindow (f->output_data.x->edit_widget) == wdesc)
240 /* A tooltip frame? */
241 || (!f->output_data.x->edit_widget
242 && FRAME_X_WINDOW (f) == wdesc)
243 || f->output_data.x->icon_desc == wdesc)
244 return f;
245 #else /* not USE_X_TOOLKIT */
246 #ifdef USE_GTK
247 if (f->output_data.x->edit_widget)
249 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
250 struct x_output *x = f->output_data.x;
251 if (gwdesc != 0 && gwdesc == x->edit_widget)
252 return f;
254 #endif /* USE_GTK */
255 if (FRAME_X_WINDOW (f) == wdesc
256 || f->output_data.x->icon_desc == wdesc)
257 return f;
258 #endif /* not USE_X_TOOLKIT */
260 return 0;
263 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
264 /* Like x_window_to_frame but also compares the window with the widget's
265 windows. */
267 struct frame *
268 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
270 Lisp_Object tail, frame;
271 struct frame *f, *found = NULL;
272 struct x_output *x;
274 if (wdesc == None)
275 return NULL;
277 FOR_EACH_FRAME (tail, frame)
279 if (found)
280 break;
281 f = XFRAME (frame);
282 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
284 /* This frame matches if the window is any of its widgets. */
285 x = f->output_data.x;
286 if (x->hourglass_window == wdesc)
287 found = f;
288 else if (x->widget)
290 #ifdef USE_GTK
291 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
292 if (gwdesc != 0
293 && gtk_widget_get_toplevel (gwdesc) == x->widget)
294 found = f;
295 #else
296 if (wdesc == XtWindow (x->widget)
297 || wdesc == XtWindow (x->column_widget)
298 || wdesc == XtWindow (x->edit_widget))
299 found = f;
300 /* Match if the window is this frame's menubar. */
301 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
302 found = f;
303 #endif
305 else if (FRAME_X_WINDOW (f) == wdesc)
306 /* A tooltip frame. */
307 found = f;
311 return found;
314 /* Likewise, but consider only the menu bar widget. */
316 struct frame *
317 x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
319 Window wdesc = event->xany.window;
320 Lisp_Object tail, frame;
321 struct frame *f;
322 struct x_output *x;
324 if (wdesc == None)
325 return NULL;
327 FOR_EACH_FRAME (tail, frame)
329 f = XFRAME (frame);
330 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
331 continue;
332 x = f->output_data.x;
333 #ifdef USE_GTK
334 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
335 return f;
336 #else
337 /* Match if the window is this frame's menubar. */
338 if (x->menubar_widget
339 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
340 return f;
341 #endif
343 return 0;
346 /* Return the frame whose principal (outermost) window is WDESC.
347 If WDESC is some other (smaller) window, we return 0. */
349 struct frame *
350 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
352 Lisp_Object tail, frame;
353 struct frame *f;
354 struct x_output *x;
356 if (wdesc == None)
357 return NULL;
359 FOR_EACH_FRAME (tail, frame)
361 f = XFRAME (frame);
362 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
363 continue;
364 x = f->output_data.x;
366 if (x->widget)
368 /* This frame matches if the window is its topmost widget. */
369 #ifdef USE_GTK
370 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
371 if (gwdesc == x->widget)
372 return f;
373 #else
374 if (wdesc == XtWindow (x->widget))
375 return f;
376 #if 0 /* I don't know why it did this,
377 but it seems logically wrong,
378 and it causes trouble for MapNotify events. */
379 /* Match if the window is this frame's menubar. */
380 if (x->menubar_widget
381 && wdesc == XtWindow (x->menubar_widget))
382 return f;
383 #endif
384 #endif
386 else if (FRAME_X_WINDOW (f) == wdesc)
387 /* Tooltip frame. */
388 return f;
390 return 0;
392 #endif /* USE_X_TOOLKIT || USE_GTK */
396 /* Store the screen positions of frame F into XPTR and YPTR.
397 These are the positions of the containing window manager window,
398 not Emacs's own window. */
400 void
401 x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
403 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
404 int real_x = 0, real_y = 0;
405 int had_errors = 0;
406 Window win = f->output_data.x->parent_desc;
407 Atom actual_type;
408 unsigned long actual_size, bytes_remaining;
409 int rc, actual_format;
410 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
411 long max_len = 400;
412 Display *dpy = FRAME_X_DISPLAY (f);
413 unsigned char *tmp_data = NULL;
414 Atom target_type = XA_CARDINAL;
416 block_input ();
418 x_catch_errors (dpy);
420 if (win == dpyinfo->root_window)
421 win = FRAME_OUTER_WINDOW (f);
423 /* This loop traverses up the containment tree until we hit the root
424 window. Window managers may intersect many windows between our window
425 and the root window. The window we find just before the root window
426 should be the outer WM window. */
427 for (;;)
429 Window wm_window, rootw;
430 Window *tmp_children;
431 unsigned int tmp_nchildren;
432 int success;
434 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
435 &wm_window, &tmp_children, &tmp_nchildren);
437 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
439 /* Don't free tmp_children if XQueryTree failed. */
440 if (! success)
441 break;
443 XFree (tmp_children);
445 if (wm_window == rootw || had_errors)
446 break;
448 win = wm_window;
451 if (! had_errors)
453 unsigned int ign;
454 Window child, rootw;
456 /* Get the real coordinates for the WM window upper left corner */
457 XGetGeometry (FRAME_X_DISPLAY (f), win,
458 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
460 /* Translate real coordinates to coordinates relative to our
461 window. For our window, the upper left corner is 0, 0.
462 Since the upper left corner of the WM window is outside
463 our window, win_x and win_y will be negative:
465 ------------------ ---> x
466 | title |
467 | ----------------- v y
468 | | our window
470 XTranslateCoordinates (FRAME_X_DISPLAY (f),
472 /* From-window, to-window. */
473 FRAME_X_DISPLAY_INFO (f)->root_window,
474 FRAME_X_WINDOW (f),
476 /* From-position, to-position. */
477 real_x, real_y, &win_x, &win_y,
479 /* Child of win. */
480 &child);
482 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
484 outer_x = win_x;
485 outer_y = win_y;
487 else
489 XTranslateCoordinates (FRAME_X_DISPLAY (f),
491 /* From-window, to-window. */
492 FRAME_X_DISPLAY_INFO (f)->root_window,
493 FRAME_OUTER_WINDOW (f),
495 /* From-position, to-position. */
496 real_x, real_y, &outer_x, &outer_y,
498 /* Child of win. */
499 &child);
502 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
506 if (dpyinfo->root_window == f->output_data.x->parent_desc)
508 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
509 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
510 0, max_len, False, target_type,
511 &actual_type, &actual_format, &actual_size,
512 &bytes_remaining, &tmp_data);
514 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
515 && actual_size == 4 && actual_format == 32)
517 unsigned int ign;
518 Window rootw;
519 long *fe = (long *)tmp_data;
521 XGetGeometry (FRAME_X_DISPLAY (f), win,
522 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
523 outer_x = -fe[0];
524 outer_y = -fe[2];
525 real_x -= fe[0];
526 real_y -= fe[2];
530 if (tmp_data) XFree (tmp_data);
532 x_uncatch_errors ();
534 unblock_input ();
536 if (had_errors) return;
538 f->x_pixels_diff = -win_x;
539 f->y_pixels_diff = -win_y;
541 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
542 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
544 *xptr = real_x;
545 *yptr = real_y;
551 /* Gamma-correct COLOR on frame F. */
553 void
554 gamma_correct (struct frame *f, XColor *color)
556 if (f->gamma)
558 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
559 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
560 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
565 /* Decide if color named COLOR_NAME is valid for use on frame F. If
566 so, return the RGB values in COLOR. If ALLOC_P,
567 allocate the color. Value is false if COLOR_NAME is invalid, or
568 no color could be allocated. */
570 bool
571 x_defined_color (struct frame *f, const char *color_name,
572 XColor *color, bool alloc_p)
574 bool success_p = 0;
575 Display *dpy = FRAME_X_DISPLAY (f);
576 Colormap cmap = FRAME_X_COLORMAP (f);
578 block_input ();
579 #ifdef USE_GTK
580 success_p = xg_check_special_colors (f, color_name, color);
581 #endif
582 if (!success_p)
583 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
584 if (success_p && alloc_p)
585 success_p = x_alloc_nearest_color (f, cmap, color);
586 unblock_input ();
588 return success_p;
592 /* Return the pixel color value for color COLOR_NAME on frame F. If F
593 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
594 Signal an error if color can't be allocated. */
596 static int
597 x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
599 XColor cdef;
601 CHECK_STRING (color_name);
603 #if 0 /* Don't do this. It's wrong when we're not using the default
604 colormap, it makes freeing difficult, and it's probably not
605 an important optimization. */
606 if (strcmp (SDATA (color_name), "black") == 0)
607 return BLACK_PIX_DEFAULT (f);
608 else if (strcmp (SDATA (color_name), "white") == 0)
609 return WHITE_PIX_DEFAULT (f);
610 #endif
612 /* Return MONO_COLOR for monochrome frames. */
613 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
614 return mono_color;
616 /* x_defined_color is responsible for coping with failures
617 by looking for a near-miss. */
618 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
619 return cdef.pixel;
621 signal_error ("Undefined color", color_name);
626 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
627 the previous value of that parameter, NEW_VALUE is the new value.
628 See also the comment of wait_for_wm in struct x_output. */
630 static void
631 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
633 f->output_data.x->wait_for_wm = !NILP (new_value);
636 static void
637 x_set_tool_bar_position (struct frame *f,
638 Lisp_Object new_value,
639 Lisp_Object old_value)
641 if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
642 && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
643 return;
644 if (EQ (new_value, old_value)) return;
646 #ifdef USE_GTK
647 xg_change_toolbar_position (f, new_value);
648 fset_tool_bar_position (f, new_value);
649 #endif
652 #ifdef USE_GTK
654 /* Set icon from FILE for frame F. By using GTK functions the icon
655 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
658 xg_set_icon (FRAME_PTR f, Lisp_Object file)
660 int result = 0;
661 Lisp_Object found;
663 found = x_find_image_file (file);
665 if (! NILP (found))
667 GdkPixbuf *pixbuf;
668 GError *err = NULL;
669 char *filename = SSDATA (found);
670 block_input ();
672 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
674 if (pixbuf)
676 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
677 pixbuf);
678 g_object_unref (pixbuf);
680 result = 1;
682 else
683 g_error_free (err);
685 unblock_input ();
688 return result;
692 xg_set_icon_from_xpm_data (FRAME_PTR f, const char **data)
694 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
696 if (!pixbuf)
697 return 0;
699 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
700 g_object_unref (pixbuf);
701 return 1;
703 #endif /* USE_GTK */
706 /* Functions called only from `x_set_frame_param'
707 to set individual parameters.
709 If FRAME_X_WINDOW (f) is 0,
710 the frame is being created and its X-window does not exist yet.
711 In that case, just record the parameter's new value
712 in the standard place; do not attempt to change the window. */
714 static void
715 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
717 struct x_output *x = f->output_data.x;
718 unsigned long fg, old_fg;
720 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
721 old_fg = FRAME_FOREGROUND_PIXEL (f);
722 FRAME_FOREGROUND_PIXEL (f) = fg;
724 if (FRAME_X_WINDOW (f) != 0)
726 Display *dpy = FRAME_X_DISPLAY (f);
728 block_input ();
729 XSetForeground (dpy, x->normal_gc, fg);
730 XSetBackground (dpy, x->reverse_gc, fg);
732 if (x->cursor_pixel == old_fg)
734 unload_color (f, x->cursor_pixel);
735 x->cursor_pixel = x_copy_color (f, fg);
736 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
739 unblock_input ();
741 update_face_from_frame_parameter (f, Qforeground_color, arg);
743 if (FRAME_VISIBLE_P (f))
744 redraw_frame (f);
747 unload_color (f, old_fg);
750 static void
751 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
753 struct x_output *x = f->output_data.x;
754 unsigned long bg;
756 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
757 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
758 FRAME_BACKGROUND_PIXEL (f) = bg;
760 if (FRAME_X_WINDOW (f) != 0)
762 Display *dpy = FRAME_X_DISPLAY (f);
764 block_input ();
765 XSetBackground (dpy, x->normal_gc, bg);
766 XSetForeground (dpy, x->reverse_gc, bg);
767 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
768 XSetForeground (dpy, x->cursor_gc, bg);
770 #ifdef USE_GTK
771 xg_set_background_color (f, bg);
772 #endif
774 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
775 toolkit scroll bars. */
777 Lisp_Object bar;
778 for (bar = FRAME_SCROLL_BARS (f);
779 !NILP (bar);
780 bar = XSCROLL_BAR (bar)->next)
782 Window window = XSCROLL_BAR (bar)->x_window;
783 XSetWindowBackground (dpy, window, bg);
786 #endif /* USE_TOOLKIT_SCROLL_BARS */
788 unblock_input ();
789 update_face_from_frame_parameter (f, Qbackground_color, arg);
791 if (FRAME_VISIBLE_P (f))
792 redraw_frame (f);
796 static Cursor
797 make_invisible_cursor (struct frame *f)
799 Display *dpy = FRAME_X_DISPLAY (f);
800 static char const no_data[] = { 0 };
801 Pixmap pix;
802 XColor col;
803 Cursor c = 0;
805 x_catch_errors (dpy);
806 pix = XCreateBitmapFromData (dpy, FRAME_X_DISPLAY_INFO (f)->root_window,
807 no_data, 1, 1);
808 if (! x_had_errors_p (dpy) && pix != None)
810 Cursor pixc;
811 col.pixel = 0;
812 col.red = col.green = col.blue = 0;
813 col.flags = DoRed | DoGreen | DoBlue;
814 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
815 if (! x_had_errors_p (dpy) && pixc != None)
816 c = pixc;
817 XFreePixmap (dpy, pix);
820 x_uncatch_errors ();
822 return c;
825 static void
826 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
828 struct x_output *x = f->output_data.x;
829 Display *dpy = FRAME_X_DISPLAY (f);
830 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
831 Cursor hourglass_cursor, horizontal_drag_cursor;
832 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
833 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
835 /* Don't let pointers be invisible. */
836 if (mask_color == pixel)
838 x_free_colors (f, &pixel, 1);
839 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
842 unload_color (f, x->mouse_pixel);
843 x->mouse_pixel = pixel;
845 block_input ();
847 /* It's not okay to crash if the user selects a screwy cursor. */
848 x_catch_errors (dpy);
850 if (!NILP (Vx_pointer_shape))
852 CHECK_NUMBER (Vx_pointer_shape);
853 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
855 else
856 cursor = XCreateFontCursor (dpy, XC_xterm);
857 x_check_errors (dpy, "bad text pointer cursor: %s");
859 if (!NILP (Vx_nontext_pointer_shape))
861 CHECK_NUMBER (Vx_nontext_pointer_shape);
862 nontext_cursor
863 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
865 else
866 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
867 x_check_errors (dpy, "bad nontext pointer cursor: %s");
869 if (!NILP (Vx_hourglass_pointer_shape))
871 CHECK_NUMBER (Vx_hourglass_pointer_shape);
872 hourglass_cursor
873 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
875 else
876 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
877 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
879 if (!NILP (Vx_mode_pointer_shape))
881 CHECK_NUMBER (Vx_mode_pointer_shape);
882 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
884 else
885 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
886 x_check_errors (dpy, "bad modeline pointer cursor: %s");
888 if (!NILP (Vx_sensitive_text_pointer_shape))
890 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
891 hand_cursor
892 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
894 else
895 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
897 if (!NILP (Vx_window_horizontal_drag_shape))
899 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
900 horizontal_drag_cursor
901 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
903 else
904 horizontal_drag_cursor
905 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
907 /* Check and report errors with the above calls. */
908 x_check_errors (dpy, "can't set cursor shape: %s");
909 x_uncatch_errors ();
912 XColor fore_color, back_color;
914 fore_color.pixel = x->mouse_pixel;
915 x_query_color (f, &fore_color);
916 back_color.pixel = mask_color;
917 x_query_color (f, &back_color);
919 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
920 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
921 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
922 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
923 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
924 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
927 if (FRAME_X_WINDOW (f) != 0)
928 XDefineCursor (dpy, FRAME_X_WINDOW (f),
929 f->output_data.x->current_cursor = cursor);
931 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor == 0)
932 FRAME_X_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
934 if (cursor != x->text_cursor
935 && x->text_cursor != 0)
936 XFreeCursor (dpy, x->text_cursor);
937 x->text_cursor = cursor;
939 if (nontext_cursor != x->nontext_cursor
940 && x->nontext_cursor != 0)
941 XFreeCursor (dpy, x->nontext_cursor);
942 x->nontext_cursor = nontext_cursor;
944 if (hourglass_cursor != x->hourglass_cursor
945 && x->hourglass_cursor != 0)
946 XFreeCursor (dpy, x->hourglass_cursor);
947 x->hourglass_cursor = hourglass_cursor;
949 if (mode_cursor != x->modeline_cursor
950 && x->modeline_cursor != 0)
951 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
952 x->modeline_cursor = mode_cursor;
954 if (hand_cursor != x->hand_cursor
955 && x->hand_cursor != 0)
956 XFreeCursor (dpy, x->hand_cursor);
957 x->hand_cursor = hand_cursor;
959 if (horizontal_drag_cursor != x->horizontal_drag_cursor
960 && x->horizontal_drag_cursor != 0)
961 XFreeCursor (dpy, x->horizontal_drag_cursor);
962 x->horizontal_drag_cursor = horizontal_drag_cursor;
964 XFlush (dpy);
965 unblock_input ();
967 update_face_from_frame_parameter (f, Qmouse_color, arg);
970 static void
971 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
973 unsigned long fore_pixel, pixel;
974 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
975 struct x_output *x = f->output_data.x;
977 if (!NILP (Vx_cursor_fore_pixel))
979 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
980 WHITE_PIX_DEFAULT (f));
981 fore_pixel_allocated_p = 1;
983 else
984 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
986 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
987 pixel_allocated_p = 1;
989 /* Make sure that the cursor color differs from the background color. */
990 if (pixel == FRAME_BACKGROUND_PIXEL (f))
992 if (pixel_allocated_p)
994 x_free_colors (f, &pixel, 1);
995 pixel_allocated_p = 0;
998 pixel = x->mouse_pixel;
999 if (pixel == fore_pixel)
1001 if (fore_pixel_allocated_p)
1003 x_free_colors (f, &fore_pixel, 1);
1004 fore_pixel_allocated_p = 0;
1006 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1010 unload_color (f, x->cursor_foreground_pixel);
1011 if (!fore_pixel_allocated_p)
1012 fore_pixel = x_copy_color (f, fore_pixel);
1013 x->cursor_foreground_pixel = fore_pixel;
1015 unload_color (f, x->cursor_pixel);
1016 if (!pixel_allocated_p)
1017 pixel = x_copy_color (f, pixel);
1018 x->cursor_pixel = pixel;
1020 if (FRAME_X_WINDOW (f) != 0)
1022 block_input ();
1023 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1024 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
1025 unblock_input ();
1027 if (FRAME_VISIBLE_P (f))
1029 x_update_cursor (f, 0);
1030 x_update_cursor (f, 1);
1034 update_face_from_frame_parameter (f, Qcursor_color, arg);
1037 /* Set the border-color of frame F to pixel value PIX.
1038 Note that this does not fully take effect if done before
1039 F has an x-window. */
1041 static void
1042 x_set_border_pixel (struct frame *f, int pix)
1044 unload_color (f, f->output_data.x->border_pixel);
1045 f->output_data.x->border_pixel = pix;
1047 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
1049 block_input ();
1050 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
1051 unblock_input ();
1053 if (FRAME_VISIBLE_P (f))
1054 redraw_frame (f);
1058 /* Set the border-color of frame F to value described by ARG.
1059 ARG can be a string naming a color.
1060 The border-color is used for the border that is drawn by the X server.
1061 Note that this does not fully take effect if done before
1062 F has an x-window; it must be redone when the window is created.
1064 Note: this is done in two routines because of the way X10 works.
1066 Note: under X11, this is normally the province of the window manager,
1067 and so emacs' border colors may be overridden. */
1069 static void
1070 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1072 int pix;
1074 CHECK_STRING (arg);
1075 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1076 x_set_border_pixel (f, pix);
1077 update_face_from_frame_parameter (f, Qborder_color, arg);
1081 static void
1082 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1084 set_frame_cursor_types (f, arg);
1086 /* Make sure the cursor gets redrawn. */
1087 cursor_type_changed = 1;
1090 static void
1091 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1093 int result;
1095 if (STRINGP (arg))
1097 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1098 return;
1100 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1101 return;
1103 block_input ();
1104 if (NILP (arg))
1105 result = x_text_icon (f,
1106 SSDATA ((!NILP (f->icon_name)
1107 ? f->icon_name
1108 : f->name)));
1109 else
1110 result = x_bitmap_icon (f, arg);
1112 if (result)
1114 unblock_input ();
1115 error ("No icon window available");
1118 XFlush (FRAME_X_DISPLAY (f));
1119 unblock_input ();
1122 static void
1123 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1125 int result;
1127 if (STRINGP (arg))
1129 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1130 return;
1132 else if (!NILP (arg) || NILP (oldval))
1133 return;
1135 fset_icon_name (f, arg);
1137 if (f->output_data.x->icon_bitmap != 0)
1138 return;
1140 block_input ();
1142 result = x_text_icon (f,
1143 SSDATA ((!NILP (f->icon_name)
1144 ? f->icon_name
1145 : !NILP (f->title)
1146 ? f->title
1147 : f->name)));
1149 if (result)
1151 unblock_input ();
1152 error ("No icon window available");
1155 XFlush (FRAME_X_DISPLAY (f));
1156 unblock_input ();
1160 void
1161 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1163 int nlines;
1164 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1165 int olines = FRAME_MENU_BAR_LINES (f);
1166 #endif
1168 /* Right now, menu bars don't work properly in minibuf-only frames;
1169 most of the commands try to apply themselves to the minibuffer
1170 frame itself, and get an error because you can't switch buffers
1171 in or split the minibuffer window. */
1172 if (FRAME_MINIBUF_ONLY_P (f))
1173 return;
1175 if (TYPE_RANGED_INTEGERP (int, value))
1176 nlines = XINT (value);
1177 else
1178 nlines = 0;
1180 /* Make sure we redisplay all windows in this frame. */
1181 windows_or_buffers_changed++;
1183 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1184 FRAME_MENU_BAR_LINES (f) = 0;
1185 if (nlines)
1187 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1188 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1189 /* Make sure next redisplay shows the menu bar. */
1190 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1192 else
1194 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1195 free_frame_menubar (f);
1196 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1197 if (FRAME_X_P (f))
1198 f->output_data.x->menubar_widget = 0;
1200 #else /* not USE_X_TOOLKIT && not USE_GTK */
1201 FRAME_MENU_BAR_LINES (f) = nlines;
1202 resize_frame_windows (f, FRAME_LINES (f), 0);
1204 /* If the menu bar height gets changed, the internal border below
1205 the top margin has to be cleared. Also, if the menu bar gets
1206 larger, the area for the added lines has to be cleared except for
1207 the first menu bar line that is to be drawn later. */
1208 if (nlines != olines)
1210 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1211 int width = FRAME_PIXEL_WIDTH (f);
1212 int y;
1214 /* height can be zero here. */
1215 if (height > 0 && width > 0)
1217 y = FRAME_TOP_MARGIN_HEIGHT (f);
1219 block_input ();
1220 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1221 0, y, width, height, False);
1222 unblock_input ();
1225 if (nlines > 1 && nlines > olines)
1227 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1228 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1230 block_input ();
1231 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1232 0, y, width, height, False);
1233 unblock_input ();
1236 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1237 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1239 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1240 adjust_glyphs (f);
1241 run_window_configuration_change_hook (f);
1245 /* Set the number of lines used for the tool bar of frame F to VALUE.
1246 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1247 is the old number of tool bar lines. This function changes the
1248 height of all windows on frame F to match the new tool bar height.
1249 The frame's height doesn't change. */
1251 void
1252 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1254 int delta, nlines, root_height;
1255 Lisp_Object root_window;
1257 /* Treat tool bars like menu bars. */
1258 if (FRAME_MINIBUF_ONLY_P (f))
1259 return;
1261 /* Use VALUE only if an int >= 0. */
1262 if (RANGED_INTEGERP (0, value, INT_MAX))
1263 nlines = XFASTINT (value);
1264 else
1265 nlines = 0;
1267 #ifdef USE_GTK
1268 FRAME_TOOL_BAR_LINES (f) = 0;
1269 if (nlines)
1271 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1272 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1273 /* Make sure next redisplay shows the tool bar. */
1274 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1275 update_frame_tool_bar (f);
1277 else
1279 if (FRAME_EXTERNAL_TOOL_BAR (f))
1280 free_frame_tool_bar (f);
1281 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1284 return;
1285 #endif
1287 /* Make sure we redisplay all windows in this frame. */
1288 ++windows_or_buffers_changed;
1290 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1292 /* Don't resize the tool-bar to more than we have room for. */
1293 root_window = FRAME_ROOT_WINDOW (f);
1294 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1295 if (root_height - delta < 1)
1297 delta = root_height - 1;
1298 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1301 FRAME_TOOL_BAR_LINES (f) = nlines;
1302 resize_frame_windows (f, FRAME_LINES (f), 0);
1303 adjust_glyphs (f);
1305 /* We also have to make sure that the internal border at the top of
1306 the frame, below the menu bar or tool bar, is redrawn when the
1307 tool bar disappears. This is so because the internal border is
1308 below the tool bar if one is displayed, but is below the menu bar
1309 if there isn't a tool bar. The tool bar draws into the area
1310 below the menu bar. */
1311 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1313 clear_frame (f);
1314 clear_current_matrices (f);
1317 /* If the tool bar gets smaller, the internal border below it
1318 has to be cleared. It was formerly part of the display
1319 of the larger tool bar, and updating windows won't clear it. */
1320 if (delta < 0)
1322 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1323 int width = FRAME_PIXEL_WIDTH (f);
1324 int y = (FRAME_MENU_BAR_LINES (f) + nlines) * FRAME_LINE_HEIGHT (f);
1326 /* height can be zero here. */
1327 if (height > 0 && width > 0)
1329 block_input ();
1330 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1331 0, y, width, height, False);
1332 unblock_input ();
1335 if (WINDOWP (f->tool_bar_window))
1336 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1339 run_window_configuration_change_hook (f);
1344 /* Set the foreground color for scroll bars on frame F to VALUE.
1345 VALUE should be a string, a color name. If it isn't a string or
1346 isn't a valid color name, do nothing. OLDVAL is the old value of
1347 the frame parameter. */
1349 static void
1350 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1352 unsigned long pixel;
1354 if (STRINGP (value))
1355 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1356 else
1357 pixel = -1;
1359 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1360 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1362 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1363 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1365 /* Remove all scroll bars because they have wrong colors. */
1366 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1367 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1368 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1369 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1371 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1372 redraw_frame (f);
1377 /* Set the background color for scroll bars on frame F to VALUE VALUE
1378 should be a string, a color name. If it isn't a string or isn't a
1379 valid color name, do nothing. OLDVAL is the old value of the frame
1380 parameter. */
1382 static void
1383 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1385 unsigned long pixel;
1387 if (STRINGP (value))
1388 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1389 else
1390 pixel = -1;
1392 if (f->output_data.x->scroll_bar_background_pixel != -1)
1393 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1395 #ifdef USE_TOOLKIT_SCROLL_BARS
1396 /* Scrollbar shadow colors. */
1397 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1399 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1400 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1402 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1404 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1405 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1407 #endif /* USE_TOOLKIT_SCROLL_BARS */
1409 f->output_data.x->scroll_bar_background_pixel = pixel;
1410 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1412 /* Remove all scroll bars because they have wrong colors. */
1413 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1414 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1415 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1416 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1418 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1419 redraw_frame (f);
1424 /* Encode Lisp string STRING as a text in a format appropriate for
1425 XICCC (X Inter Client Communication Conventions).
1427 This can call Lisp code, so callers must GCPRO.
1429 If STRING contains only ASCII characters, do no conversion and
1430 return the string data of STRING. Otherwise, encode the text by
1431 CODING_SYSTEM, and return a newly allocated memory area which
1432 should be freed by `xfree' by a caller.
1434 SELECTIONP non-zero means the string is being encoded for an X
1435 selection, so it is safe to run pre-write conversions (which
1436 may run Lisp code).
1438 Store the byte length of resulting text in *TEXT_BYTES.
1440 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1441 which means that the `encoding' of the result can be `STRING'.
1442 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1443 the result should be `COMPOUND_TEXT'. */
1445 static unsigned char *
1446 x_encode_text (Lisp_Object string, Lisp_Object coding_system, int selectionp,
1447 ptrdiff_t *text_bytes, int *stringp, int *freep)
1449 int result = string_xstring_p (string);
1450 struct coding_system coding;
1452 if (result == 0)
1454 /* No multibyte character in OBJ. We need not encode it. */
1455 *text_bytes = SBYTES (string);
1456 *stringp = 1;
1457 *freep = 0;
1458 return SDATA (string);
1461 setup_coding_system (coding_system, &coding);
1462 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1463 /* We suppress producing escape sequences for composition. */
1464 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1465 coding.destination = xnmalloc (SCHARS (string), 2);
1466 coding.dst_bytes = SCHARS (string) * 2;
1467 encode_coding_object (&coding, string, 0, 0,
1468 SCHARS (string), SBYTES (string), Qnil);
1469 *text_bytes = coding.produced;
1470 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1471 *freep = 1;
1472 return coding.destination;
1476 /* Set the WM name to NAME for frame F. Also set the icon name.
1477 If the frame already has an icon name, use that, otherwise set the
1478 icon name to NAME. */
1480 static void
1481 x_set_name_internal (FRAME_PTR f, Lisp_Object name)
1483 if (FRAME_X_WINDOW (f))
1485 block_input ();
1487 XTextProperty text, icon;
1488 ptrdiff_t bytes;
1489 int stringp;
1490 int do_free_icon_value = 0, do_free_text_value = 0;
1491 Lisp_Object coding_system;
1492 Lisp_Object encoded_name;
1493 Lisp_Object encoded_icon_name;
1494 struct gcpro gcpro1;
1496 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1497 we use it before x_encode_text that may return string data. */
1498 GCPRO1 (name);
1499 encoded_name = ENCODE_UTF_8 (name);
1500 UNGCPRO;
1502 coding_system = Qcompound_text;
1503 /* Note: Encoding strategy
1505 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1506 text.encoding. But, there are non-internationalized window
1507 managers which don't support that encoding. So, if NAME
1508 contains only ASCII and 8859-1 characters, encode it by
1509 iso-latin-1, and use "STRING" in text.encoding hoping that
1510 such window managers at least analyze this format correctly,
1511 i.e. treat 8-bit bytes as 8859-1 characters.
1513 We may also be able to use "UTF8_STRING" in text.encoding
1514 in the future which can encode all Unicode characters.
1515 But, for the moment, there's no way to know that the
1516 current window manager supports it or not.
1518 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1519 properties. Per the EWMH specification, those two properties
1520 are always UTF8_STRING. This matches what gtk_window_set_title()
1521 does in the USE_GTK case. */
1522 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1523 &do_free_text_value);
1524 text.encoding = (stringp ? XA_STRING
1525 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1526 text.format = 8;
1527 text.nitems = bytes;
1528 if (text.nitems != bytes)
1529 error ("Window name too large");
1531 if (!STRINGP (f->icon_name))
1533 icon = text;
1534 encoded_icon_name = encoded_name;
1536 else
1538 /* See the above comment "Note: Encoding strategy". */
1539 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1540 &bytes, &stringp, &do_free_icon_value);
1541 icon.encoding = (stringp ? XA_STRING
1542 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1543 icon.format = 8;
1544 icon.nitems = bytes;
1545 if (icon.nitems != bytes)
1546 error ("Icon name too large");
1548 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1551 #ifdef USE_GTK
1552 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1553 SSDATA (encoded_name));
1554 #else /* not USE_GTK */
1555 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1556 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1557 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_name,
1558 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1559 8, PropModeReplace,
1560 SDATA (encoded_name),
1561 SBYTES (encoded_name));
1562 #endif /* not USE_GTK */
1564 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1565 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1566 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1567 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1568 8, PropModeReplace,
1569 SDATA (encoded_icon_name),
1570 SBYTES (encoded_icon_name));
1572 if (do_free_icon_value)
1573 xfree (icon.value);
1574 if (do_free_text_value)
1575 xfree (text.value);
1577 unblock_input ();
1581 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1582 x_id_name.
1584 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1585 name; if NAME is a string, set F's name to NAME and set
1586 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1588 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1589 suggesting a new name, which lisp code should override; if
1590 F->explicit_name is set, ignore the new name; otherwise, set it. */
1592 static void
1593 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1595 /* Make sure that requests from lisp code override requests from
1596 Emacs redisplay code. */
1597 if (explicit)
1599 /* If we're switching from explicit to implicit, we had better
1600 update the mode lines and thereby update the title. */
1601 if (f->explicit_name && NILP (name))
1602 update_mode_lines = 1;
1604 f->explicit_name = ! NILP (name);
1606 else if (f->explicit_name)
1607 return;
1609 /* If NAME is nil, set the name to the x_id_name. */
1610 if (NILP (name))
1612 /* Check for no change needed in this very common case
1613 before we do any consing. */
1614 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1615 SSDATA (f->name)))
1616 return;
1617 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1619 else
1620 CHECK_STRING (name);
1622 /* Don't change the name if it's already NAME. */
1623 if (! NILP (Fstring_equal (name, f->name)))
1624 return;
1626 fset_name (f, name);
1628 /* For setting the frame title, the title parameter should override
1629 the name parameter. */
1630 if (! NILP (f->title))
1631 name = f->title;
1633 x_set_name_internal (f, name);
1636 /* This function should be called when the user's lisp code has
1637 specified a name for the frame; the name will override any set by the
1638 redisplay code. */
1639 static void
1640 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1642 x_set_name (f, arg, 1);
1645 /* This function should be called by Emacs redisplay code to set the
1646 name; names set this way will never override names set by the user's
1647 lisp code. */
1648 void
1649 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1651 x_set_name (f, arg, 0);
1654 /* Change the title of frame F to NAME.
1655 If NAME is nil, use the frame name as the title. */
1657 static void
1658 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1660 /* Don't change the title if it's already NAME. */
1661 if (EQ (name, f->title))
1662 return;
1664 update_mode_lines = 1;
1666 fset_title (f, name);
1668 if (NILP (name))
1669 name = f->name;
1670 else
1671 CHECK_STRING (name);
1673 x_set_name_internal (f, name);
1676 void
1677 x_set_scroll_bar_default_width (struct frame *f)
1679 int wid = FRAME_COLUMN_WIDTH (f);
1680 #ifdef USE_TOOLKIT_SCROLL_BARS
1681 #ifdef USE_GTK
1682 int minw = xg_get_default_scrollbar_width ();
1683 #else
1684 int minw = 16;
1685 #endif
1686 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1687 int width = minw + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1688 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1689 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1690 #else
1691 /* Make the actual width at least 14 pixels and a multiple of a
1692 character width. */
1693 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1695 /* Use all of that space (aside from required margins) for the
1696 scroll bar. */
1697 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1698 #endif
1702 /* Record in frame F the specified or default value according to ALIST
1703 of the parameter named PROP (a Lisp symbol). If no value is
1704 specified for PROP, look for an X default for XPROP on the frame
1705 named NAME. If that is not found either, use the value DEFLT. */
1707 static Lisp_Object
1708 x_default_scroll_bar_color_parameter (struct frame *f,
1709 Lisp_Object alist, Lisp_Object prop,
1710 const char *xprop, const char *xclass,
1711 int foreground_p)
1713 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1714 Lisp_Object tem;
1716 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1717 if (EQ (tem, Qunbound))
1719 #ifdef USE_TOOLKIT_SCROLL_BARS
1721 /* See if an X resource for the scroll bar color has been
1722 specified. */
1723 tem = display_x_get_resource (dpyinfo,
1724 build_string (foreground_p
1725 ? "foreground"
1726 : "background"),
1727 empty_unibyte_string,
1728 build_string ("verticalScrollBar"),
1729 empty_unibyte_string);
1730 if (!STRINGP (tem))
1732 /* If nothing has been specified, scroll bars will use a
1733 toolkit-dependent default. Because these defaults are
1734 difficult to get at without actually creating a scroll
1735 bar, use nil to indicate that no color has been
1736 specified. */
1737 tem = Qnil;
1740 #else /* not USE_TOOLKIT_SCROLL_BARS */
1742 tem = Qnil;
1744 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1747 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
1748 return tem;
1754 #ifdef USE_X_TOOLKIT
1756 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1757 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1758 already be present because of the toolkit (Motif adds some of them,
1759 for example, but Xt doesn't). */
1761 static void
1762 hack_wm_protocols (FRAME_PTR f, Widget widget)
1764 Display *dpy = XtDisplay (widget);
1765 Window w = XtWindow (widget);
1766 int need_delete = 1;
1767 int need_focus = 1;
1768 int need_save = 1;
1770 block_input ();
1772 Atom type;
1773 unsigned char *catoms;
1774 int format = 0;
1775 unsigned long nitems = 0;
1776 unsigned long bytes_after;
1778 if ((XGetWindowProperty (dpy, w,
1779 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1780 (long)0, (long)100, False, XA_ATOM,
1781 &type, &format, &nitems, &bytes_after,
1782 &catoms)
1783 == Success)
1784 && format == 32 && type == XA_ATOM)
1786 Atom *atoms = (Atom *) catoms;
1787 while (nitems > 0)
1789 nitems--;
1790 if (atoms[nitems]
1791 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1792 need_delete = 0;
1793 else if (atoms[nitems]
1794 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1795 need_focus = 0;
1796 else if (atoms[nitems]
1797 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1798 need_save = 0;
1801 if (catoms)
1802 XFree (catoms);
1805 Atom props [10];
1806 int count = 0;
1807 if (need_delete)
1808 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1809 if (need_focus)
1810 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1811 if (need_save)
1812 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1813 if (count)
1814 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1815 XA_ATOM, 32, PropModeAppend,
1816 (unsigned char *) props, count);
1818 unblock_input ();
1820 #endif
1824 /* Support routines for XIC (X Input Context). */
1826 #ifdef HAVE_X_I18N
1828 static XFontSet xic_create_xfontset (struct frame *);
1829 static XIMStyle best_xim_style (XIMStyles *, XIMStyles *);
1832 /* Supported XIM styles, ordered by preference. */
1834 static XIMStyle supported_xim_styles[] =
1836 XIMPreeditPosition | XIMStatusArea,
1837 XIMPreeditPosition | XIMStatusNothing,
1838 XIMPreeditPosition | XIMStatusNone,
1839 XIMPreeditNothing | XIMStatusArea,
1840 XIMPreeditNothing | XIMStatusNothing,
1841 XIMPreeditNothing | XIMStatusNone,
1842 XIMPreeditNone | XIMStatusArea,
1843 XIMPreeditNone | XIMStatusNothing,
1844 XIMPreeditNone | XIMStatusNone,
1849 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1850 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1852 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1854 /* Create an Xt fontset spec from the name of a base font.
1855 If `motif' is True use the Motif syntax. */
1856 char *
1857 xic_create_fontsetname (const char *base_fontname, int motif)
1859 const char *sep = motif ? ";" : ",";
1860 char *fontsetname;
1862 /* Make a fontset name from the base font name. */
1863 if (xic_default_fontset == base_fontname)
1865 /* There is no base font name, use the default. */
1866 fontsetname = xmalloc (strlen (base_fontname) + 2);
1867 strcpy (fontsetname, base_fontname);
1869 else
1871 /* Make a fontset name from the base font name.
1872 The font set will be made of the following elements:
1873 - the base font.
1874 - the base font where the charset spec is replaced by -*-*.
1875 - the same but with the family also replaced with -*-*-. */
1876 const char *p = base_fontname;
1877 ptrdiff_t i;
1879 for (i = 0; *p; p++)
1880 if (*p == '-') i++;
1881 if (i != 14)
1883 /* As the font name doesn't conform to XLFD, we can't
1884 modify it to generalize it to allcs and allfamilies.
1885 Use the specified font plus the default. */
1886 fontsetname = xmalloc (strlen (base_fontname)
1887 + strlen (xic_default_fontset) + 3);
1888 strcpy (fontsetname, base_fontname);
1889 strcat (fontsetname, sep);
1890 strcat (fontsetname, xic_default_fontset);
1892 else
1894 ptrdiff_t len;
1895 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1896 char *font_allcs = NULL;
1897 char *font_allfamilies = NULL;
1898 char *font_all = NULL;
1899 const char *allcs = "*-*-*-*-*-*-*";
1900 const char *allfamilies = "-*-*-";
1901 const char *all = "*-*-*-*-";
1902 char *base;
1904 for (i = 0, p = base_fontname; i < 8; p++)
1906 if (*p == '-')
1908 i++;
1909 if (i == 3)
1910 p1 = p + 1;
1911 else if (i == 7)
1912 p2 = p + 1;
1913 else if (i == 6)
1914 p3 = p + 1;
1917 /* If base_fontname specifies ADSTYLE, make it a
1918 wildcard. */
1919 if (*p3 != '*')
1921 ptrdiff_t diff = (p2 - p3) - 2;
1923 base = alloca (strlen (base_fontname) + 1);
1924 memcpy (base, base_fontname, p3 - base_fontname);
1925 base[p3 - base_fontname] = '*';
1926 base[(p3 - base_fontname) + 1] = '-';
1927 strcpy (base + (p3 - base_fontname) + 2, p2);
1928 p = base + (p - base_fontname) - diff;
1929 p1 = base + (p1 - base_fontname);
1930 p2 = base + (p2 - base_fontname) - diff;
1931 base_fontname = base;
1934 /* Build the font spec that matches all charsets. */
1935 len = p - base_fontname + strlen (allcs) + 1;
1936 font_allcs = alloca (len);
1937 memcpy (font_allcs, base_fontname, p - base_fontname);
1938 strcat (font_allcs, allcs);
1940 /* Build the font spec that matches all families and
1941 add-styles. */
1942 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1943 font_allfamilies = alloca (len);
1944 strcpy (font_allfamilies, allfamilies);
1945 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1946 strcat (font_allfamilies, allcs);
1948 /* Build the font spec that matches all. */
1949 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1950 font_all = alloca (len);
1951 strcpy (font_all, allfamilies);
1952 strcat (font_all, all);
1953 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1954 strcat (font_all, allcs);
1956 /* Build the actual font set name. */
1957 len = strlen (base_fontname) + strlen (font_allcs)
1958 + strlen (font_allfamilies) + strlen (font_all) + 5;
1959 fontsetname = xmalloc (len);
1960 strcpy (fontsetname, base_fontname);
1961 strcat (fontsetname, sep);
1962 strcat (fontsetname, font_allcs);
1963 strcat (fontsetname, sep);
1964 strcat (fontsetname, font_allfamilies);
1965 strcat (fontsetname, sep);
1966 strcat (fontsetname, font_all);
1969 if (motif)
1970 strcat (fontsetname, ":");
1971 return fontsetname;
1973 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1975 #ifdef DEBUG_XIC_FONTSET
1976 static void
1977 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1978 int missing_count)
1980 if (xfs)
1981 fprintf (stderr, "XIC Fontset created: %s\n", name);
1982 else
1984 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1985 while (missing_count-- > 0)
1987 fprintf (stderr, " missing: %s\n", *missing_list);
1988 missing_list++;
1993 #endif
1995 static XFontSet
1996 xic_create_xfontset (struct frame *f)
1998 XFontSet xfs = NULL;
1999 struct font *font = FRAME_FONT (f);
2000 int pixel_size = font->pixel_size;
2001 Lisp_Object rest, frame;
2003 /* See if there is another frame already using same fontset. */
2004 FOR_EACH_FRAME (rest, frame)
2006 struct frame *cf = XFRAME (frame);
2008 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2009 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2010 && FRAME_FONT (f)
2011 && FRAME_FONT (f)->pixel_size == pixel_size)
2013 xfs = FRAME_XIC_FONTSET (cf);
2014 break;
2018 if (! xfs)
2020 char buf[256];
2021 char **missing_list;
2022 int missing_count;
2023 char *def_string;
2024 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
2026 sprintf (buf, xlfd_format, pixel_size);
2027 missing_list = NULL;
2028 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2029 &missing_list, &missing_count, &def_string);
2030 #ifdef DEBUG_XIC_FONTSET
2031 print_fontset_result (xfs, buf, missing_list, missing_count);
2032 #endif
2033 if (missing_list)
2034 XFreeStringList (missing_list);
2035 if (! xfs)
2037 /* List of pixel sizes most likely available. Find one that
2038 is closest to pixel_size. */
2039 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
2040 int *smaller, *larger;
2042 for (smaller = sizes; smaller[1]; smaller++)
2043 if (smaller[1] >= pixel_size)
2044 break;
2045 larger = smaller + 1;
2046 if (*larger == pixel_size)
2047 larger++;
2048 while (*smaller || *larger)
2050 int this_size;
2052 if (! *larger)
2053 this_size = *smaller--;
2054 else if (! *smaller)
2055 this_size = *larger++;
2056 else if (pixel_size - *smaller < *larger - pixel_size)
2057 this_size = *smaller--;
2058 else
2059 this_size = *larger++;
2060 sprintf (buf, xlfd_format, this_size);
2061 missing_list = NULL;
2062 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2063 &missing_list, &missing_count, &def_string);
2064 #ifdef DEBUG_XIC_FONTSET
2065 print_fontset_result (xfs, buf, missing_list, missing_count);
2066 #endif
2067 if (missing_list)
2068 XFreeStringList (missing_list);
2069 if (xfs)
2070 break;
2073 if (! xfs)
2075 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
2077 missing_list = NULL;
2078 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
2079 &missing_list, &missing_count, &def_string);
2080 #ifdef DEBUG_XIC_FONTSET
2081 print_fontset_result (xfs, last_resort, missing_list, missing_count);
2082 #endif
2083 if (missing_list)
2084 XFreeStringList (missing_list);
2089 return xfs;
2092 /* Free the X fontset of frame F if it is the last frame using it. */
2094 void
2095 xic_free_xfontset (struct frame *f)
2097 Lisp_Object rest, frame;
2098 int shared_p = 0;
2100 if (!FRAME_XIC_FONTSET (f))
2101 return;
2103 /* See if there is another frame sharing the same fontset. */
2104 FOR_EACH_FRAME (rest, frame)
2106 struct frame *cf = XFRAME (frame);
2107 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2108 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2109 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2111 shared_p = 1;
2112 break;
2116 if (!shared_p)
2117 /* The fontset is not used anymore. It is safe to free it. */
2118 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2120 if (FRAME_XIC_BASE_FONTNAME (f))
2121 xfree (FRAME_XIC_BASE_FONTNAME (f));
2122 FRAME_XIC_BASE_FONTNAME (f) = NULL;
2123 FRAME_XIC_FONTSET (f) = NULL;
2127 /* Value is the best input style, given user preferences USER (already
2128 checked to be supported by Emacs), and styles supported by the
2129 input method XIM. */
2131 static XIMStyle
2132 best_xim_style (XIMStyles *user, XIMStyles *xim)
2134 int i, j;
2136 for (i = 0; i < user->count_styles; ++i)
2137 for (j = 0; j < xim->count_styles; ++j)
2138 if (user->supported_styles[i] == xim->supported_styles[j])
2139 return user->supported_styles[i];
2141 /* Return the default style. */
2142 return XIMPreeditNothing | XIMStatusNothing;
2145 /* Create XIC for frame F. */
2147 static XIMStyle xic_style;
2149 void
2150 create_frame_xic (struct frame *f)
2152 XIM xim;
2153 XIC xic = NULL;
2154 XFontSet xfs = NULL;
2156 if (FRAME_XIC (f))
2157 return;
2159 /* Create X fontset. */
2160 xfs = xic_create_xfontset (f);
2161 xim = FRAME_X_XIM (f);
2162 if (xim)
2164 XRectangle s_area;
2165 XPoint spot;
2166 XVaNestedList preedit_attr;
2167 XVaNestedList status_attr;
2169 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2170 spot.x = 0; spot.y = 1;
2172 /* Determine XIC style. */
2173 if (xic_style == 0)
2175 XIMStyles supported_list;
2176 supported_list.count_styles = (sizeof supported_xim_styles
2177 / sizeof supported_xim_styles[0]);
2178 supported_list.supported_styles = supported_xim_styles;
2179 xic_style = best_xim_style (&supported_list,
2180 FRAME_X_XIM_STYLES (f));
2183 preedit_attr = XVaCreateNestedList (0,
2184 XNFontSet, xfs,
2185 XNForeground,
2186 FRAME_FOREGROUND_PIXEL (f),
2187 XNBackground,
2188 FRAME_BACKGROUND_PIXEL (f),
2189 (xic_style & XIMPreeditPosition
2190 ? XNSpotLocation
2191 : NULL),
2192 &spot,
2193 NULL);
2194 status_attr = XVaCreateNestedList (0,
2195 XNArea,
2196 &s_area,
2197 XNFontSet,
2198 xfs,
2199 XNForeground,
2200 FRAME_FOREGROUND_PIXEL (f),
2201 XNBackground,
2202 FRAME_BACKGROUND_PIXEL (f),
2203 NULL);
2205 xic = XCreateIC (xim,
2206 XNInputStyle, xic_style,
2207 XNClientWindow, FRAME_X_WINDOW (f),
2208 XNFocusWindow, FRAME_X_WINDOW (f),
2209 XNStatusAttributes, status_attr,
2210 XNPreeditAttributes, preedit_attr,
2211 NULL);
2212 XFree (preedit_attr);
2213 XFree (status_attr);
2216 FRAME_XIC (f) = xic;
2217 FRAME_XIC_STYLE (f) = xic_style;
2218 FRAME_XIC_FONTSET (f) = xfs;
2222 /* Destroy XIC and free XIC fontset of frame F, if any. */
2224 void
2225 free_frame_xic (struct frame *f)
2227 if (FRAME_XIC (f) == NULL)
2228 return;
2230 XDestroyIC (FRAME_XIC (f));
2231 xic_free_xfontset (f);
2233 FRAME_XIC (f) = NULL;
2237 /* Place preedit area for XIC of window W's frame to specified
2238 pixel position X/Y. X and Y are relative to window W. */
2240 void
2241 xic_set_preeditarea (struct window *w, int x, int y)
2243 struct frame *f = XFRAME (w->frame);
2244 XVaNestedList attr;
2245 XPoint spot;
2247 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2248 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2249 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2250 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2251 XFree (attr);
2255 /* Place status area for XIC in bottom right corner of frame F.. */
2257 void
2258 xic_set_statusarea (struct frame *f)
2260 XIC xic = FRAME_XIC (f);
2261 XVaNestedList attr;
2262 XRectangle area;
2263 XRectangle *needed;
2265 /* Negotiate geometry of status area. If input method has existing
2266 status area, use its current size. */
2267 area.x = area.y = area.width = area.height = 0;
2268 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2269 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2270 XFree (attr);
2272 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2273 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2274 XFree (attr);
2276 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2278 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2279 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2280 XFree (attr);
2283 area.width = needed->width;
2284 area.height = needed->height;
2285 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2286 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2287 - FRAME_MENUBAR_HEIGHT (f)
2288 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2289 - FRAME_INTERNAL_BORDER_WIDTH (f));
2290 XFree (needed);
2292 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2293 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2294 XFree (attr);
2298 /* Set X fontset for XIC of frame F, using base font name
2299 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2301 void
2302 xic_set_xfontset (struct frame *f, const char *base_fontname)
2304 XVaNestedList attr;
2305 XFontSet xfs;
2307 xic_free_xfontset (f);
2309 xfs = xic_create_xfontset (f);
2311 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2312 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2313 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2314 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2315 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2316 XFree (attr);
2318 FRAME_XIC_FONTSET (f) = xfs;
2321 #endif /* HAVE_X_I18N */
2325 #ifdef USE_X_TOOLKIT
2327 /* Create and set up the X widget for frame F. */
2329 static void
2330 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2332 XClassHint class_hints;
2333 XSetWindowAttributes attributes;
2334 unsigned long attribute_mask;
2335 Widget shell_widget;
2336 Widget pane_widget;
2337 Widget frame_widget;
2338 Arg al [25];
2339 int ac;
2341 block_input ();
2343 /* Use the resource name as the top-level widget name
2344 for looking up resources. Make a non-Lisp copy
2345 for the window manager, so GC relocation won't bother it.
2347 Elsewhere we specify the window name for the window manager. */
2350 char *str = SSDATA (Vx_resource_name);
2351 f->namebuf = xmalloc (strlen (str) + 1);
2352 strcpy (f->namebuf, str);
2355 ac = 0;
2356 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2357 XtSetArg (al[ac], XtNinput, 1); ac++;
2358 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2359 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2360 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2361 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2362 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2363 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2364 applicationShellWidgetClass,
2365 FRAME_X_DISPLAY (f), al, ac);
2367 f->output_data.x->widget = shell_widget;
2368 /* maybe_set_screen_title_format (shell_widget); */
2370 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2371 (widget_value *) NULL,
2372 shell_widget, False,
2373 (lw_callback) NULL,
2374 (lw_callback) NULL,
2375 (lw_callback) NULL,
2376 (lw_callback) NULL);
2378 ac = 0;
2379 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2380 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2381 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2382 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2383 XtSetValues (pane_widget, al, ac);
2384 f->output_data.x->column_widget = pane_widget;
2386 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2387 the emacs screen when changing menubar. This reduces flickering. */
2389 ac = 0;
2390 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2391 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2392 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2393 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2394 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2395 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2396 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2397 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2398 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2399 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2400 al, ac);
2402 f->output_data.x->edit_widget = frame_widget;
2404 XtManageChild (frame_widget);
2406 /* Do some needed geometry management. */
2408 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
2409 Arg gal[10];
2410 int gac = 0;
2411 int extra_borders = 0;
2412 int menubar_size
2413 = (f->output_data.x->menubar_widget
2414 ? (f->output_data.x->menubar_widget->core.height
2415 + f->output_data.x->menubar_widget->core.border_width)
2416 : 0);
2418 #if 0 /* Experimentally, we now get the right results
2419 for -geometry -0-0 without this. 24 Aug 96, rms. */
2420 if (FRAME_EXTERNAL_MENU_BAR (f))
2422 Dimension ibw = 0;
2423 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2424 menubar_size += ibw;
2426 #endif
2428 f->output_data.x->menubar_height = menubar_size;
2430 #ifndef USE_LUCID
2431 /* Motif seems to need this amount added to the sizes
2432 specified for the shell widget. The Athena/Lucid widgets don't.
2433 Both conclusions reached experimentally. -- rms. */
2434 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2435 &extra_borders, NULL);
2436 extra_borders *= 2;
2437 #endif
2439 /* Convert our geometry parameters into a geometry string
2440 and specify it.
2441 Note that we do not specify here whether the position
2442 is a user-specified or program-specified one.
2443 We pass that information later, in x_wm_set_size_hints. */
2445 int left = f->left_pos;
2446 int xneg = window_prompting & XNegative;
2447 int top = f->top_pos;
2448 int yneg = window_prompting & YNegative;
2449 if (xneg)
2450 left = -left;
2451 if (yneg)
2452 top = -top;
2454 if (window_prompting & USPosition)
2455 sprintf (shell_position, "=%dx%d%c%d%c%d",
2456 FRAME_PIXEL_WIDTH (f) + extra_borders,
2457 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2458 (xneg ? '-' : '+'), left,
2459 (yneg ? '-' : '+'), top);
2460 else
2462 sprintf (shell_position, "=%dx%d",
2463 FRAME_PIXEL_WIDTH (f) + extra_borders,
2464 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2466 /* Setting x and y when the position is not specified in
2467 the geometry string will set program position in the WM hints.
2468 If Emacs had just one program position, we could set it in
2469 fallback resources, but since each make-frame call can specify
2470 different program positions, this is easier. */
2471 XtSetArg (gal[gac], XtNx, left); gac++;
2472 XtSetArg (gal[gac], XtNy, top); gac++;
2476 /* We don't free this because we don't know whether
2477 it is safe to free it while the frame exists.
2478 It isn't worth the trouble of arranging to free it
2479 when the frame is deleted. */
2480 tem = xstrdup (shell_position);
2481 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2482 XtSetValues (shell_widget, gal, gac);
2485 XtManageChild (pane_widget);
2486 XtRealizeWidget (shell_widget);
2488 if (FRAME_X_EMBEDDED_P (f))
2489 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2490 f->output_data.x->parent_desc, 0, 0);
2492 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2494 validate_x_resource_name ();
2496 class_hints.res_name = SSDATA (Vx_resource_name);
2497 class_hints.res_class = SSDATA (Vx_resource_class);
2498 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2500 #ifdef HAVE_X_I18N
2501 FRAME_XIC (f) = NULL;
2502 if (use_xim)
2503 create_frame_xic (f);
2504 #endif
2506 f->output_data.x->wm_hints.input = True;
2507 f->output_data.x->wm_hints.flags |= InputHint;
2508 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2509 &f->output_data.x->wm_hints);
2511 hack_wm_protocols (f, shell_widget);
2513 #ifdef HACK_EDITRES
2514 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2515 #endif
2517 /* Do a stupid property change to force the server to generate a
2518 PropertyNotify event so that the event_stream server timestamp will
2519 be initialized to something relevant to the time we created the window.
2521 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2522 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2523 XA_ATOM, 32, PropModeAppend,
2524 (unsigned char*) NULL, 0);
2526 /* Make all the standard events reach the Emacs frame. */
2527 attributes.event_mask = STANDARD_EVENT_SET;
2529 #ifdef HAVE_X_I18N
2530 if (FRAME_XIC (f))
2532 /* XIM server might require some X events. */
2533 unsigned long fevent = NoEventMask;
2534 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2535 attributes.event_mask |= fevent;
2537 #endif /* HAVE_X_I18N */
2539 attribute_mask = CWEventMask;
2540 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2541 attribute_mask, &attributes);
2543 XtMapWidget (frame_widget);
2545 /* x_set_name normally ignores requests to set the name if the
2546 requested name is the same as the current name. This is the one
2547 place where that assumption isn't correct; f->name is set, but
2548 the X server hasn't been told. */
2550 Lisp_Object name;
2551 int explicit = f->explicit_name;
2553 f->explicit_name = 0;
2554 name = f->name;
2555 fset_name (f, Qnil);
2556 x_set_name (f, name, explicit);
2559 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2560 f->output_data.x->current_cursor
2561 = f->output_data.x->text_cursor);
2563 unblock_input ();
2565 /* This is a no-op, except under Motif. Make sure main areas are
2566 set to something reasonable, in case we get an error later. */
2567 lw_set_main_areas (pane_widget, 0, frame_widget);
2570 #else /* not USE_X_TOOLKIT */
2571 #ifdef USE_GTK
2572 static void
2573 x_window (FRAME_PTR f)
2575 if (! xg_create_frame_widgets (f))
2576 error ("Unable to create window");
2578 #ifdef HAVE_X_I18N
2579 FRAME_XIC (f) = NULL;
2580 if (use_xim)
2582 block_input ();
2583 create_frame_xic (f);
2584 if (FRAME_XIC (f))
2586 /* XIM server might require some X events. */
2587 unsigned long fevent = NoEventMask;
2588 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2590 if (fevent != NoEventMask)
2592 XSetWindowAttributes attributes;
2593 XWindowAttributes wattr;
2594 unsigned long attribute_mask;
2596 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2597 &wattr);
2598 attributes.event_mask = wattr.your_event_mask | fevent;
2599 attribute_mask = CWEventMask;
2600 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2601 attribute_mask, &attributes);
2604 unblock_input ();
2606 #endif
2609 #else /*! USE_GTK */
2610 /* Create and set up the X window for frame F. */
2612 static void
2613 x_window (struct frame *f)
2615 XClassHint class_hints;
2616 XSetWindowAttributes attributes;
2617 unsigned long attribute_mask;
2619 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2620 attributes.border_pixel = f->output_data.x->border_pixel;
2621 attributes.bit_gravity = StaticGravity;
2622 attributes.backing_store = NotUseful;
2623 attributes.save_under = True;
2624 attributes.event_mask = STANDARD_EVENT_SET;
2625 attributes.colormap = FRAME_X_COLORMAP (f);
2626 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2627 | CWColormap);
2629 block_input ();
2630 FRAME_X_WINDOW (f)
2631 = XCreateWindow (FRAME_X_DISPLAY (f),
2632 f->output_data.x->parent_desc,
2633 f->left_pos,
2634 f->top_pos,
2635 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2636 f->border_width,
2637 CopyFromParent, /* depth */
2638 InputOutput, /* class */
2639 FRAME_X_VISUAL (f),
2640 attribute_mask, &attributes);
2642 #ifdef HAVE_X_I18N
2643 if (use_xim)
2645 create_frame_xic (f);
2646 if (FRAME_XIC (f))
2648 /* XIM server might require some X events. */
2649 unsigned long fevent = NoEventMask;
2650 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2651 attributes.event_mask |= fevent;
2652 attribute_mask = CWEventMask;
2653 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2654 attribute_mask, &attributes);
2657 #endif /* HAVE_X_I18N */
2659 validate_x_resource_name ();
2661 class_hints.res_name = SSDATA (Vx_resource_name);
2662 class_hints.res_class = SSDATA (Vx_resource_class);
2663 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2665 /* The menubar is part of the ordinary display;
2666 it does not count in addition to the height of the window. */
2667 f->output_data.x->menubar_height = 0;
2669 /* This indicates that we use the "Passive Input" input model.
2670 Unless we do this, we don't get the Focus{In,Out} events that we
2671 need to draw the cursor correctly. Accursed bureaucrats.
2672 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2674 f->output_data.x->wm_hints.input = True;
2675 f->output_data.x->wm_hints.flags |= InputHint;
2676 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2677 &f->output_data.x->wm_hints);
2678 f->output_data.x->wm_hints.icon_pixmap = None;
2680 /* Request "save yourself" and "delete window" commands from wm. */
2682 Atom protocols[2];
2683 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2684 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2685 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2688 /* x_set_name normally ignores requests to set the name if the
2689 requested name is the same as the current name. This is the one
2690 place where that assumption isn't correct; f->name is set, but
2691 the X server hasn't been told. */
2693 Lisp_Object name;
2694 int explicit = f->explicit_name;
2696 f->explicit_name = 0;
2697 name = f->name;
2698 fset_name (f, Qnil);
2699 x_set_name (f, name, explicit);
2702 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2703 f->output_data.x->current_cursor
2704 = f->output_data.x->text_cursor);
2706 unblock_input ();
2708 if (FRAME_X_WINDOW (f) == 0)
2709 error ("Unable to create window");
2712 #endif /* not USE_GTK */
2713 #endif /* not USE_X_TOOLKIT */
2715 /* Verify that the icon position args for this window are valid. */
2717 static void
2718 x_icon_verify (struct frame *f, Lisp_Object parms)
2720 Lisp_Object icon_x, icon_y;
2722 /* Set the position of the icon. Note that twm groups all
2723 icons in an icon window. */
2724 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2725 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2726 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2728 CHECK_NUMBER (icon_x);
2729 CHECK_NUMBER (icon_y);
2731 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2732 error ("Both left and top icon corners of icon must be specified");
2735 /* Handle the icon stuff for this window. Perhaps later we might
2736 want an x_set_icon_position which can be called interactively as
2737 well. */
2739 static void
2740 x_icon (struct frame *f, Lisp_Object parms)
2742 Lisp_Object icon_x, icon_y;
2743 #if 0
2744 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2745 #endif
2747 /* Set the position of the icon. Note that twm groups all
2748 icons in an icon window. */
2749 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2750 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2751 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2753 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2754 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2756 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2757 error ("Both left and top icon corners of icon must be specified");
2759 block_input ();
2761 if (! EQ (icon_x, Qunbound))
2762 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2764 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2765 but x_create_frame still needs it. */
2766 /* Start up iconic or window? */
2767 x_wm_set_window_state
2768 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2769 Qicon)
2770 ? IconicState
2771 : NormalState));
2772 #endif
2774 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2775 ? f->icon_name
2776 : f->name)));
2778 unblock_input ();
2781 /* Make the GCs needed for this window, setting the
2782 background, border and mouse colors; also create the
2783 mouse cursor and the gray border tile. */
2785 static void
2786 x_make_gc (struct frame *f)
2788 XGCValues gc_values;
2790 block_input ();
2792 /* Create the GCs of this frame.
2793 Note that many default values are used. */
2795 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2796 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2797 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2798 f->output_data.x->normal_gc
2799 = XCreateGC (FRAME_X_DISPLAY (f),
2800 FRAME_X_WINDOW (f),
2801 GCLineWidth | GCForeground | GCBackground,
2802 &gc_values);
2804 /* Reverse video style. */
2805 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2806 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2807 f->output_data.x->reverse_gc
2808 = XCreateGC (FRAME_X_DISPLAY (f),
2809 FRAME_X_WINDOW (f),
2810 GCForeground | GCBackground | GCLineWidth,
2811 &gc_values);
2813 /* Cursor has cursor-color background, background-color foreground. */
2814 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2815 gc_values.background = f->output_data.x->cursor_pixel;
2816 gc_values.fill_style = FillOpaqueStippled;
2817 f->output_data.x->cursor_gc
2818 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2819 (GCForeground | GCBackground
2820 | GCFillStyle | GCLineWidth),
2821 &gc_values);
2823 /* Reliefs. */
2824 f->output_data.x->white_relief.gc = 0;
2825 f->output_data.x->black_relief.gc = 0;
2827 /* Create the gray border tile used when the pointer is not in
2828 the frame. Since this depends on the frame's pixel values,
2829 this must be done on a per-frame basis. */
2830 f->output_data.x->border_tile
2831 = (XCreatePixmapFromBitmapData
2832 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2833 gray_bits, gray_width, gray_height,
2834 FRAME_FOREGROUND_PIXEL (f),
2835 FRAME_BACKGROUND_PIXEL (f),
2836 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2838 unblock_input ();
2842 /* Free what was allocated in x_make_gc. */
2844 void
2845 x_free_gcs (struct frame *f)
2847 Display *dpy = FRAME_X_DISPLAY (f);
2849 block_input ();
2851 if (f->output_data.x->normal_gc)
2853 XFreeGC (dpy, f->output_data.x->normal_gc);
2854 f->output_data.x->normal_gc = 0;
2857 if (f->output_data.x->reverse_gc)
2859 XFreeGC (dpy, f->output_data.x->reverse_gc);
2860 f->output_data.x->reverse_gc = 0;
2863 if (f->output_data.x->cursor_gc)
2865 XFreeGC (dpy, f->output_data.x->cursor_gc);
2866 f->output_data.x->cursor_gc = 0;
2869 if (f->output_data.x->border_tile)
2871 XFreePixmap (dpy, f->output_data.x->border_tile);
2872 f->output_data.x->border_tile = 0;
2875 unblock_input ();
2879 /* Handler for signals raised during x_create_frame and
2880 x_create_tip_frame. FRAME is the frame which is partially
2881 constructed. */
2883 static Lisp_Object
2884 unwind_create_frame (Lisp_Object frame)
2886 struct frame *f = XFRAME (frame);
2888 /* If frame is already dead, nothing to do. This can happen if the
2889 display is disconnected after the frame has become official, but
2890 before x_create_frame removes the unwind protect. */
2891 if (!FRAME_LIVE_P (f))
2892 return Qnil;
2894 /* If frame is ``official'', nothing to do. */
2895 if (NILP (Fmemq (frame, Vframe_list)))
2897 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2898 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2899 #endif
2901 x_free_frame_resources (f);
2902 free_glyphs (f);
2904 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2905 /* Check that reference counts are indeed correct. */
2906 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2907 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2908 #endif
2909 return Qt;
2912 return Qnil;
2915 static Lisp_Object
2916 unwind_create_frame_1 (Lisp_Object val)
2918 inhibit_lisp_code = val;
2919 return Qnil;
2922 static void
2923 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2925 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2926 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2927 RES_TYPE_STRING);
2928 Lisp_Object font = Qnil;
2929 if (EQ (font_param, Qunbound))
2930 font_param = Qnil;
2932 if (NILP (font_param))
2934 /* System font should take precedence over X resources. We suggest this
2935 regardless of font-use-system-font because .emacs may not have been
2936 read yet. */
2937 const char *system_font = xsettings_get_system_font ();
2938 if (system_font)
2939 font = font_open_by_name (f, build_unibyte_string (system_font));
2942 if (NILP (font))
2943 font = !NILP (font_param) ? font_param
2944 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2946 if (! FONTP (font) && ! STRINGP (font))
2948 const char *names[]
2950 #ifdef HAVE_XFT
2951 /* This will find the normal Xft font. */
2952 "monospace-10",
2953 #endif
2954 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2955 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2956 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2957 /* This was formerly the first thing tried, but it finds
2958 too many fonts and takes too long. */
2959 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2960 /* If those didn't work, look for something which will
2961 at least work. */
2962 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2963 "fixed",
2964 NULL };
2965 int i;
2967 for (i = 0; names[i]; i++)
2969 font = font_open_by_name (f, build_unibyte_string (names[i]));
2970 if (! NILP (font))
2971 break;
2973 if (NILP (font))
2974 error ("No suitable font was found");
2976 else if (!NILP (font_param))
2978 /* Remember the explicit font parameter, so we can re-apply it after
2979 we've applied the `default' face settings. */
2980 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
2983 /* This call will make X resources override any system font setting. */
2984 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2988 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2989 0, 1, 0,
2990 doc: /* Send the size hints for frame FRAME to the window manager.
2991 If FRAME is omitted or nil, use the selected frame.
2992 Signal error if FRAME is not an X frame. */)
2993 (Lisp_Object frame)
2995 struct frame *f = check_x_frame (frame);
2997 block_input ();
2998 x_wm_set_size_hint (f, 0, 0);
2999 unblock_input ();
3000 return Qnil;
3003 static void
3004 set_machine_and_pid_properties (struct frame *f)
3006 long pid = (long) getpid ();
3008 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
3009 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
3010 NULL, 0, NULL, NULL, NULL);
3011 XChangeProperty (FRAME_X_DISPLAY (f),
3012 FRAME_OUTER_WINDOW (f),
3013 XInternAtom (FRAME_X_DISPLAY (f),
3014 "_NET_WM_PID",
3015 False),
3016 XA_CARDINAL, 32, PropModeReplace,
3017 (unsigned char *) &pid, 1);
3020 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
3021 1, 1, 0,
3022 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
3023 Return an Emacs frame object.
3024 PARMS is an alist of frame parameters.
3025 If the parameters specify that the frame should not have a minibuffer,
3026 and do not specify a specific minibuffer window to use,
3027 then `default-minibuffer-frame' must be a frame whose minibuffer can
3028 be shared by the new frame.
3030 This function is an internal primitive--use `make-frame' instead. */)
3031 (Lisp_Object parms)
3033 struct frame *f;
3034 Lisp_Object frame, tem;
3035 Lisp_Object name;
3036 int minibuffer_only = 0;
3037 long window_prompting = 0;
3038 int width, height;
3039 ptrdiff_t count = SPECPDL_INDEX ();
3040 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3041 Lisp_Object display;
3042 struct x_display_info *dpyinfo = NULL;
3043 Lisp_Object parent;
3044 struct kboard *kb;
3046 parms = Fcopy_alist (parms);
3048 /* Use this general default value to start with
3049 until we know if this frame has a specified name. */
3050 Vx_resource_name = Vinvocation_name;
3052 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
3053 if (EQ (display, Qunbound))
3054 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3055 if (EQ (display, Qunbound))
3056 display = Qnil;
3057 dpyinfo = check_x_display_info (display);
3058 kb = dpyinfo->terminal->kboard;
3060 if (!dpyinfo->terminal->name)
3061 error ("Terminal is not live, can't create new frames on it");
3063 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3064 if (!STRINGP (name)
3065 && ! EQ (name, Qunbound)
3066 && ! NILP (name))
3067 error ("Invalid frame name--not a string or nil");
3069 if (STRINGP (name))
3070 Vx_resource_name = name;
3072 /* See if parent window is specified. */
3073 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3074 if (EQ (parent, Qunbound))
3075 parent = Qnil;
3076 if (! NILP (parent))
3077 CHECK_NUMBER (parent);
3079 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3080 /* No need to protect DISPLAY because that's not used after passing
3081 it to make_frame_without_minibuffer. */
3082 frame = Qnil;
3083 GCPRO4 (parms, parent, name, frame);
3084 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3085 RES_TYPE_SYMBOL);
3086 if (EQ (tem, Qnone) || NILP (tem))
3087 f = make_frame_without_minibuffer (Qnil, kb, display);
3088 else if (EQ (tem, Qonly))
3090 f = make_minibuffer_frame ();
3091 minibuffer_only = 1;
3093 else if (WINDOWP (tem))
3094 f = make_frame_without_minibuffer (tem, kb, display);
3095 else
3096 f = make_frame (1);
3098 XSETFRAME (frame, f);
3100 f->terminal = dpyinfo->terminal;
3102 f->output_method = output_x_window;
3103 f->output_data.x = xzalloc (sizeof *f->output_data.x);
3104 f->output_data.x->icon_bitmap = -1;
3105 FRAME_FONTSET (f) = -1;
3106 f->output_data.x->scroll_bar_foreground_pixel = -1;
3107 f->output_data.x->scroll_bar_background_pixel = -1;
3108 #ifdef USE_TOOLKIT_SCROLL_BARS
3109 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3110 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3111 #endif /* USE_TOOLKIT_SCROLL_BARS */
3113 fset_icon_name (f,
3114 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3115 RES_TYPE_STRING));
3116 if (! STRINGP (f->icon_name))
3117 fset_icon_name (f, Qnil);
3119 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3121 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
3122 record_unwind_protect (unwind_create_frame, frame);
3124 /* These colors will be set anyway later, but it's important
3125 to get the color reference counts right, so initialize them! */
3127 Lisp_Object black;
3128 struct gcpro gcpro1;
3130 /* Function x_decode_color can signal an error. Make
3131 sure to initialize color slots so that we won't try
3132 to free colors we haven't allocated. */
3133 FRAME_FOREGROUND_PIXEL (f) = -1;
3134 FRAME_BACKGROUND_PIXEL (f) = -1;
3135 f->output_data.x->cursor_pixel = -1;
3136 f->output_data.x->cursor_foreground_pixel = -1;
3137 f->output_data.x->border_pixel = -1;
3138 f->output_data.x->mouse_pixel = -1;
3140 black = build_string ("black");
3141 GCPRO1 (black);
3142 FRAME_FOREGROUND_PIXEL (f)
3143 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3144 FRAME_BACKGROUND_PIXEL (f)
3145 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3146 f->output_data.x->cursor_pixel
3147 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3148 f->output_data.x->cursor_foreground_pixel
3149 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3150 f->output_data.x->border_pixel
3151 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3152 f->output_data.x->mouse_pixel
3153 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3154 UNGCPRO;
3157 /* Specify the parent under which to make this X window. */
3159 if (!NILP (parent))
3161 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3162 f->output_data.x->explicit_parent = 1;
3164 else
3166 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3167 f->output_data.x->explicit_parent = 0;
3170 /* Set the name; the functions to which we pass f expect the name to
3171 be set. */
3172 if (EQ (name, Qunbound) || NILP (name))
3174 fset_name (f, build_string (dpyinfo->x_id_name));
3175 f->explicit_name = 0;
3177 else
3179 fset_name (f, name);
3180 f->explicit_name = 1;
3181 /* use the frame's title when getting resources for this frame. */
3182 specbind (Qx_resource_name, name);
3185 f->resx = dpyinfo->resx;
3186 f->resy = dpyinfo->resy;
3188 #ifdef HAVE_FREETYPE
3189 #ifdef HAVE_XFT
3190 register_font_driver (&xftfont_driver, f);
3191 #else /* not HAVE_XFT */
3192 register_font_driver (&ftxfont_driver, f);
3193 #endif /* not HAVE_XFT */
3194 #endif /* HAVE_FREETYPE */
3195 register_font_driver (&xfont_driver, f);
3197 x_default_parameter (f, parms, Qfont_backend, Qnil,
3198 "fontBackend", "FontBackend", RES_TYPE_STRING);
3200 /* Extract the window parameters from the supplied values
3201 that are needed to determine window geometry. */
3202 x_default_font_parameter (f, parms);
3203 if (!FRAME_FONT (f))
3205 delete_frame (frame, Qnoelisp);
3206 error ("Invalid frame font");
3209 /* Frame contents get displaced if an embedded X window has a border. */
3210 if (! FRAME_X_EMBEDDED_P (f))
3211 x_default_parameter (f, parms, Qborder_width, make_number (0),
3212 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3214 /* This defaults to 1 in order to match xterm. We recognize either
3215 internalBorderWidth or internalBorder (which is what xterm calls
3216 it). */
3217 if (NILP (Fassq (Qinternal_border_width, parms)))
3219 Lisp_Object value;
3221 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3222 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3223 if (! EQ (value, Qunbound))
3224 parms = Fcons (Fcons (Qinternal_border_width, value),
3225 parms);
3227 x_default_parameter (f, parms, Qinternal_border_width,
3228 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3229 make_number (0),
3230 #else
3231 make_number (1),
3232 #endif
3233 "internalBorderWidth", "internalBorderWidth",
3234 RES_TYPE_NUMBER);
3235 x_default_parameter (f, parms, Qvertical_scroll_bars,
3236 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3237 Qright,
3238 #else
3239 Qleft,
3240 #endif
3241 "verticalScrollBars", "ScrollBars",
3242 RES_TYPE_SYMBOL);
3244 /* Also do the stuff which must be set before the window exists. */
3245 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3246 "foreground", "Foreground", RES_TYPE_STRING);
3247 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3248 "background", "Background", RES_TYPE_STRING);
3249 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3250 "pointerColor", "Foreground", RES_TYPE_STRING);
3251 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3252 "borderColor", "BorderColor", RES_TYPE_STRING);
3253 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3254 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3255 x_default_parameter (f, parms, Qline_spacing, Qnil,
3256 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3257 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3258 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3259 x_default_parameter (f, parms, Qright_fringe, Qnil,
3260 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3262 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3263 "scrollBarForeground",
3264 "ScrollBarForeground", 1);
3265 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3266 "scrollBarBackground",
3267 "ScrollBarBackground", 0);
3269 #ifdef GLYPH_DEBUG
3270 image_cache_refcount =
3271 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3272 dpyinfo_refcount = dpyinfo->reference_count;
3273 #endif /* GLYPH_DEBUG */
3275 /* Init faces before x_default_parameter is called for scroll-bar
3276 parameters because that function calls x_set_scroll_bar_width,
3277 which calls change_frame_size, which calls Fset_window_buffer,
3278 which runs hooks, which call Fvertical_motion. At the end, we
3279 end up in init_iterator with a null face cache, which should not
3280 happen. */
3281 init_frame_faces (f);
3283 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3284 look up the X resources controlling the menu-bar and tool-bar
3285 here; they are processed specially at startup, and reflected in
3286 the values of the mode variables.
3288 Avoid calling window-configuration-change-hook; otherwise we
3289 could get an infloop in next_frame since the frame is not yet in
3290 Vframe_list. */
3292 ptrdiff_t count2 = SPECPDL_INDEX ();
3293 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3294 inhibit_lisp_code = Qt;
3296 x_default_parameter (f, parms, Qmenu_bar_lines,
3297 NILP (Vmenu_bar_mode)
3298 ? make_number (0) : make_number (1),
3299 NULL, NULL, RES_TYPE_NUMBER);
3300 x_default_parameter (f, parms, Qtool_bar_lines,
3301 NILP (Vtool_bar_mode)
3302 ? make_number (0) : make_number (1),
3303 NULL, NULL, RES_TYPE_NUMBER);
3305 unbind_to (count2, Qnil);
3308 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3309 "bufferPredicate", "BufferPredicate",
3310 RES_TYPE_SYMBOL);
3311 x_default_parameter (f, parms, Qtitle, Qnil,
3312 "title", "Title", RES_TYPE_STRING);
3313 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3314 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3315 x_default_parameter (f, parms, Qfullscreen, Qnil,
3316 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3317 x_default_parameter (f, parms, Qtool_bar_position,
3318 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
3320 /* Compute the size of the X window. */
3321 window_prompting = x_figure_window_size (f, parms, 1);
3323 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3324 f->no_split = minibuffer_only || EQ (tem, Qt);
3326 x_icon_verify (f, parms);
3328 /* Create the X widget or window. */
3329 #ifdef USE_X_TOOLKIT
3330 x_window (f, window_prompting, minibuffer_only);
3331 #else
3332 x_window (f);
3333 #endif
3335 x_icon (f, parms);
3336 x_make_gc (f);
3338 /* Now consider the frame official. */
3339 f->terminal->reference_count++;
3340 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3341 Vframe_list = Fcons (frame, Vframe_list);
3343 /* We need to do this after creating the X window, so that the
3344 icon-creation functions can say whose icon they're describing. */
3345 x_default_parameter (f, parms, Qicon_type, Qt,
3346 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3348 x_default_parameter (f, parms, Qauto_raise, Qnil,
3349 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3350 x_default_parameter (f, parms, Qauto_lower, Qnil,
3351 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3352 x_default_parameter (f, parms, Qcursor_type, Qbox,
3353 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3354 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3355 "scrollBarWidth", "ScrollBarWidth",
3356 RES_TYPE_NUMBER);
3357 x_default_parameter (f, parms, Qalpha, Qnil,
3358 "alpha", "Alpha", RES_TYPE_NUMBER);
3360 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3361 Change will not be effected unless different from the current
3362 FRAME_LINES (f). */
3363 width = FRAME_COLS (f);
3364 height = FRAME_LINES (f);
3366 SET_FRAME_COLS (f, 0);
3367 FRAME_LINES (f) = 0;
3368 change_frame_size (f, height, width, 1, 0, 0);
3370 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3371 /* Create the menu bar. */
3372 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3374 /* If this signals an error, we haven't set size hints for the
3375 frame and we didn't make it visible. */
3376 initialize_frame_menubar (f);
3378 #ifndef USE_GTK
3379 /* This is a no-op, except under Motif where it arranges the
3380 main window for the widgets on it. */
3381 lw_set_main_areas (f->output_data.x->column_widget,
3382 f->output_data.x->menubar_widget,
3383 f->output_data.x->edit_widget);
3384 #endif /* not USE_GTK */
3386 #endif /* USE_X_TOOLKIT || USE_GTK */
3388 /* Tell the server what size and position, etc, we want, and how
3389 badly we want them. This should be done after we have the menu
3390 bar so that its size can be taken into account. */
3391 block_input ();
3392 x_wm_set_size_hint (f, window_prompting, 0);
3393 unblock_input ();
3395 /* Make the window appear on the frame and enable display, unless
3396 the caller says not to. However, with explicit parent, Emacs
3397 cannot control visibility, so don't try. */
3398 if (! f->output_data.x->explicit_parent)
3400 Lisp_Object visibility;
3402 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3403 RES_TYPE_SYMBOL);
3404 if (EQ (visibility, Qunbound))
3405 visibility = Qt;
3407 if (EQ (visibility, Qicon))
3408 x_iconify_frame (f);
3409 else if (! NILP (visibility))
3410 x_make_frame_visible (f);
3411 else
3413 /* Must have been Qnil. */
3417 block_input ();
3419 /* Set machine name and pid for the purpose of window managers. */
3420 set_machine_and_pid_properties (f);
3422 /* Set the WM leader property. GTK does this itself, so this is not
3423 needed when using GTK. */
3424 if (dpyinfo->client_leader_window != 0)
3426 XChangeProperty (FRAME_X_DISPLAY (f),
3427 FRAME_OUTER_WINDOW (f),
3428 dpyinfo->Xatom_wm_client_leader,
3429 XA_WINDOW, 32, PropModeReplace,
3430 (unsigned char *) &dpyinfo->client_leader_window, 1);
3433 unblock_input ();
3435 /* Initialize `default-minibuffer-frame' in case this is the first
3436 frame on this terminal. */
3437 if (FRAME_HAS_MINIBUF_P (f)
3438 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3439 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3440 kset_default_minibuffer_frame (kb, frame);
3442 /* All remaining specified parameters, which have not been "used"
3443 by x_get_arg and friends, now go in the misc. alist of the frame. */
3444 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3445 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3446 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3448 UNGCPRO;
3450 /* Make sure windows on this frame appear in calls to next-window
3451 and similar functions. */
3452 Vwindow_list = Qnil;
3454 return unbind_to (count, frame);
3458 /* FRAME is used only to get a handle on the X display. We don't pass the
3459 display info directly because we're called from frame.c, which doesn't
3460 know about that structure. */
3462 Lisp_Object
3463 x_get_focus_frame (struct frame *frame)
3465 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
3466 Lisp_Object xfocus;
3467 if (! dpyinfo->x_focus_frame)
3468 return Qnil;
3470 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3471 return xfocus;
3475 /* In certain situations, when the window manager follows a
3476 click-to-focus policy, there seems to be no way around calling
3477 XSetInputFocus to give another frame the input focus .
3479 In an ideal world, XSetInputFocus should generally be avoided so
3480 that applications don't interfere with the window manager's focus
3481 policy. But I think it's okay to use when it's clearly done
3482 following a user-command. */
3484 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
3485 doc: /* Set the input focus to FRAME.
3486 FRAME nil means use the selected frame. */)
3487 (Lisp_Object frame)
3489 struct frame *f = check_x_frame (frame);
3490 Display *dpy = FRAME_X_DISPLAY (f);
3492 block_input ();
3493 x_catch_errors (dpy);
3495 if (FRAME_X_EMBEDDED_P (f))
3497 /* For Xembedded frames, normally the embedder forwards key
3498 events. See XEmbed Protocol Specification at
3499 http://freedesktop.org/wiki/Specifications/xembed-spec */
3500 xembed_request_focus (f);
3502 else
3504 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3505 RevertToParent, CurrentTime);
3506 x_ewmh_activate_frame (f);
3509 x_uncatch_errors ();
3510 unblock_input ();
3512 return Qnil;
3516 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3517 doc: /* Internal function called by `color-defined-p', which see
3518 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3519 (Lisp_Object color, Lisp_Object frame)
3521 XColor foo;
3522 FRAME_PTR f = check_x_frame (frame);
3524 CHECK_STRING (color);
3526 if (x_defined_color (f, SSDATA (color), &foo, 0))
3527 return Qt;
3528 else
3529 return Qnil;
3532 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3533 doc: /* Internal function called by `color-values', which see. */)
3534 (Lisp_Object color, Lisp_Object frame)
3536 XColor foo;
3537 FRAME_PTR f = check_x_frame (frame);
3539 CHECK_STRING (color);
3541 if (x_defined_color (f, SSDATA (color), &foo, 0))
3542 return list3 (make_number (foo.red),
3543 make_number (foo.green),
3544 make_number (foo.blue));
3545 else
3546 return Qnil;
3549 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3550 doc: /* Internal function called by `display-color-p', which see. */)
3551 (Lisp_Object terminal)
3553 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3555 if (dpyinfo->n_planes <= 2)
3556 return Qnil;
3558 switch (dpyinfo->visual->class)
3560 case StaticColor:
3561 case PseudoColor:
3562 case TrueColor:
3563 case DirectColor:
3564 return Qt;
3566 default:
3567 return Qnil;
3571 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3572 0, 1, 0,
3573 doc: /* Return t if the X display supports shades of gray.
3574 Note that color displays do support shades of gray.
3575 The optional argument TERMINAL specifies which display to ask about.
3576 TERMINAL should be a terminal object, a frame or a display name (a string).
3577 If omitted or nil, that stands for the selected frame's display. */)
3578 (Lisp_Object terminal)
3580 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3582 if (dpyinfo->n_planes <= 1)
3583 return Qnil;
3585 switch (dpyinfo->visual->class)
3587 case StaticColor:
3588 case PseudoColor:
3589 case TrueColor:
3590 case DirectColor:
3591 case StaticGray:
3592 case GrayScale:
3593 return Qt;
3595 default:
3596 return Qnil;
3600 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3601 0, 1, 0,
3602 doc: /* Return the width in pixels of the X display TERMINAL.
3603 The optional argument TERMINAL specifies which display to ask about.
3604 TERMINAL should be a terminal object, a frame or a display name (a string).
3605 If omitted or nil, that stands for the selected frame's display. */)
3606 (Lisp_Object terminal)
3608 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3610 return make_number (x_display_pixel_width (dpyinfo));
3613 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3614 Sx_display_pixel_height, 0, 1, 0,
3615 doc: /* Return the height in pixels of the X display TERMINAL.
3616 The optional argument TERMINAL specifies which display to ask about.
3617 TERMINAL should be a terminal object, a frame or a display name (a string).
3618 If omitted or nil, that stands for the selected frame's display. */)
3619 (Lisp_Object terminal)
3621 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3623 return make_number (x_display_pixel_height (dpyinfo));
3626 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3627 0, 1, 0,
3628 doc: /* Return the number of bitplanes of the X display TERMINAL.
3629 The optional argument TERMINAL specifies which display to ask about.
3630 TERMINAL should be a terminal object, a frame or a display name (a string).
3631 If omitted or nil, that stands for the selected frame's display. */)
3632 (Lisp_Object terminal)
3634 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3636 return make_number (dpyinfo->n_planes);
3639 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3640 0, 1, 0,
3641 doc: /* Return the number of color cells of the X display TERMINAL.
3642 The optional argument TERMINAL specifies which display to ask about.
3643 TERMINAL should be a terminal object, a frame or a display name (a string).
3644 If omitted or nil, that stands for the selected frame's display. */)
3645 (Lisp_Object terminal)
3647 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3649 int nr_planes = DisplayPlanes (dpyinfo->display,
3650 XScreenNumberOfScreen (dpyinfo->screen));
3652 /* Truncate nr_planes to 24 to avoid integer overflow.
3653 Some displays says 32, but only 24 bits are actually significant.
3654 There are only very few and rare video cards that have more than
3655 24 significant bits. Also 24 bits is more than 16 million colors,
3656 it "should be enough for everyone". */
3657 if (nr_planes > 24) nr_planes = 24;
3659 return make_number (1 << nr_planes);
3662 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3663 Sx_server_max_request_size,
3664 0, 1, 0,
3665 doc: /* Return the maximum request size of the X server of display TERMINAL.
3666 The optional argument TERMINAL specifies which display to ask about.
3667 TERMINAL should be a terminal object, a frame or a display name (a string).
3668 If omitted or nil, that stands for the selected frame's display. */)
3669 (Lisp_Object terminal)
3671 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3673 return make_number (MAXREQUEST (dpyinfo->display));
3676 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3677 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3678 \(Labeling every distributor as a "vendor" embodies the false assumption
3679 that operating systems cannot be developed and distributed noncommercially.)
3680 The optional argument TERMINAL specifies which display to ask about.
3681 TERMINAL should be a terminal object, a frame or a display name (a string).
3682 If omitted or nil, that stands for the selected frame's display. */)
3683 (Lisp_Object terminal)
3685 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3686 const char *vendor = ServerVendor (dpyinfo->display);
3688 if (! vendor) vendor = "";
3689 return build_string (vendor);
3692 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3693 doc: /* Return the version numbers of the X server of display TERMINAL.
3694 The value is a list of three integers: the major and minor
3695 version numbers of the X Protocol in use, and the distributor-specific release
3696 number. See also the function `x-server-vendor'.
3698 The optional argument TERMINAL specifies which display to ask about.
3699 TERMINAL should be a terminal object, a frame or a display name (a string).
3700 If omitted or nil, that stands for the selected frame's display. */)
3701 (Lisp_Object terminal)
3703 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3704 Display *dpy = dpyinfo->display;
3706 return Fcons (make_number (ProtocolVersion (dpy)),
3707 Fcons (make_number (ProtocolRevision (dpy)),
3708 Fcons (make_number (VendorRelease (dpy)), Qnil)));
3711 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3712 doc: /* Return the number of screens on the X server of display TERMINAL.
3713 The optional argument TERMINAL specifies which display to ask about.
3714 TERMINAL should be a terminal object, a frame or a display name (a string).
3715 If omitted or nil, that stands for the selected frame's display. */)
3716 (Lisp_Object terminal)
3718 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3720 return make_number (ScreenCount (dpyinfo->display));
3723 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3724 doc: /* Return the height in millimeters of the X display TERMINAL.
3725 The optional argument TERMINAL specifies which display to ask about.
3726 TERMINAL should be a terminal object, a frame or a display name (a string).
3727 If omitted or nil, that stands for the selected frame's display. */)
3728 (Lisp_Object terminal)
3730 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3732 return make_number (HeightMMOfScreen (dpyinfo->screen));
3735 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3736 doc: /* Return the width in millimeters of the X display TERMINAL.
3737 The optional argument TERMINAL specifies which display to ask about.
3738 TERMINAL should be a terminal object, a frame or a display name (a string).
3739 If omitted or nil, that stands for the selected frame's display. */)
3740 (Lisp_Object terminal)
3742 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3744 return make_number (WidthMMOfScreen (dpyinfo->screen));
3747 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3748 Sx_display_backing_store, 0, 1, 0,
3749 doc: /* Return an indication of whether X display TERMINAL does backing store.
3750 The value may be `always', `when-mapped', or `not-useful'.
3751 The optional argument TERMINAL specifies which display to ask about.
3752 TERMINAL should be a terminal object, a frame or a display name (a string).
3753 If omitted or nil, that stands for the selected frame's display. */)
3754 (Lisp_Object terminal)
3756 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3757 Lisp_Object result;
3759 switch (DoesBackingStore (dpyinfo->screen))
3761 case Always:
3762 result = intern ("always");
3763 break;
3765 case WhenMapped:
3766 result = intern ("when-mapped");
3767 break;
3769 case NotUseful:
3770 result = intern ("not-useful");
3771 break;
3773 default:
3774 error ("Strange value for BackingStore parameter of screen");
3775 result = Qnil;
3778 return result;
3781 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3782 Sx_display_visual_class, 0, 1, 0,
3783 doc: /* Return the visual class of the X display TERMINAL.
3784 The value is one of the symbols `static-gray', `gray-scale',
3785 `static-color', `pseudo-color', `true-color', or `direct-color'.
3787 The optional argument TERMINAL specifies which display to ask about.
3788 TERMINAL should a terminal object, a frame or a display name (a string).
3789 If omitted or nil, that stands for the selected frame's display. */)
3790 (Lisp_Object terminal)
3792 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3793 Lisp_Object result;
3795 switch (dpyinfo->visual->class)
3797 case StaticGray:
3798 result = intern ("static-gray");
3799 break;
3800 case GrayScale:
3801 result = intern ("gray-scale");
3802 break;
3803 case StaticColor:
3804 result = intern ("static-color");
3805 break;
3806 case PseudoColor:
3807 result = intern ("pseudo-color");
3808 break;
3809 case TrueColor:
3810 result = intern ("true-color");
3811 break;
3812 case DirectColor:
3813 result = intern ("direct-color");
3814 break;
3815 default:
3816 error ("Display has an unknown visual class");
3817 result = Qnil;
3820 return result;
3823 DEFUN ("x-display-save-under", Fx_display_save_under,
3824 Sx_display_save_under, 0, 1, 0,
3825 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3826 The optional argument TERMINAL specifies which display to ask about.
3827 TERMINAL should be a terminal object, a frame or a display name (a string).
3828 If omitted or nil, that stands for the selected frame's display. */)
3829 (Lisp_Object terminal)
3831 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3833 if (DoesSaveUnders (dpyinfo->screen) == True)
3834 return Qt;
3835 else
3836 return Qnil;
3840 x_pixel_width (register struct frame *f)
3842 return FRAME_PIXEL_WIDTH (f);
3846 x_pixel_height (register struct frame *f)
3848 return FRAME_PIXEL_HEIGHT (f);
3852 x_char_width (register struct frame *f)
3854 return FRAME_COLUMN_WIDTH (f);
3858 x_char_height (register struct frame *f)
3860 return FRAME_LINE_HEIGHT (f);
3865 /************************************************************************
3866 X Displays
3867 ************************************************************************/
3870 /* Mapping visual names to visuals. */
3872 static struct visual_class
3874 const char *name;
3875 int class;
3877 visual_classes[] =
3879 {"StaticGray", StaticGray},
3880 {"GrayScale", GrayScale},
3881 {"StaticColor", StaticColor},
3882 {"PseudoColor", PseudoColor},
3883 {"TrueColor", TrueColor},
3884 {"DirectColor", DirectColor},
3885 {NULL, 0}
3889 #ifndef HAVE_XSCREENNUMBEROFSCREEN
3891 /* Value is the screen number of screen SCR. This is a substitute for
3892 the X function with the same name when that doesn't exist. */
3895 XScreenNumberOfScreen (scr)
3896 register Screen *scr;
3898 Display *dpy = scr->display;
3899 int i;
3901 for (i = 0; i < dpy->nscreens; ++i)
3902 if (scr == dpy->screens + i)
3903 break;
3905 return i;
3908 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
3911 /* Select the visual that should be used on display DPYINFO. Set
3912 members of DPYINFO appropriately. Called from x_term_init. */
3914 void
3915 select_visual (struct x_display_info *dpyinfo)
3917 Display *dpy = dpyinfo->display;
3918 Screen *screen = dpyinfo->screen;
3919 Lisp_Object value;
3921 /* See if a visual is specified. */
3922 value = display_x_get_resource (dpyinfo,
3923 build_string ("visualClass"),
3924 build_string ("VisualClass"),
3925 Qnil, Qnil);
3926 if (STRINGP (value))
3928 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
3929 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
3930 depth, a decimal number. NAME is compared with case ignored. */
3931 char *s = alloca (SBYTES (value) + 1);
3932 char *dash;
3933 int i, class = -1;
3934 XVisualInfo vinfo;
3936 strcpy (s, SSDATA (value));
3937 dash = strchr (s, '-');
3938 if (dash)
3940 dpyinfo->n_planes = atoi (dash + 1);
3941 *dash = '\0';
3943 else
3944 /* We won't find a matching visual with depth 0, so that
3945 an error will be printed below. */
3946 dpyinfo->n_planes = 0;
3948 /* Determine the visual class. */
3949 for (i = 0; visual_classes[i].name; ++i)
3950 if (xstrcasecmp (s, visual_classes[i].name) == 0)
3952 class = visual_classes[i].class;
3953 break;
3956 /* Look up a matching visual for the specified class. */
3957 if (class == -1
3958 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
3959 dpyinfo->n_planes, class, &vinfo))
3960 fatal ("Invalid visual specification `%s'", SDATA (value));
3962 dpyinfo->visual = vinfo.visual;
3964 else
3966 int n_visuals;
3967 XVisualInfo *vinfo, vinfo_template;
3969 dpyinfo->visual = DefaultVisualOfScreen (screen);
3971 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
3972 vinfo_template.screen = XScreenNumberOfScreen (screen);
3973 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
3974 &vinfo_template, &n_visuals);
3975 if (n_visuals <= 0)
3976 fatal ("Can't get proper X visual info");
3978 dpyinfo->n_planes = vinfo->depth;
3979 XFree (vinfo);
3984 /* Return the X display structure for the display named NAME.
3985 Open a new connection if necessary. */
3987 static struct x_display_info *
3988 x_display_info_for_name (Lisp_Object name)
3990 Lisp_Object names;
3991 struct x_display_info *dpyinfo;
3993 CHECK_STRING (name);
3995 #if 0
3996 if (! EQ (Vinitial_window_system, intern ("x")))
3997 error ("Not using X Windows"); /* That doesn't stop us anymore. */
3998 #endif
4000 for (dpyinfo = x_display_list, names = x_display_name_list;
4001 dpyinfo;
4002 dpyinfo = dpyinfo->next, names = XCDR (names))
4004 Lisp_Object tem;
4005 tem = Fstring_equal (XCAR (XCAR (names)), name);
4006 if (!NILP (tem))
4007 return dpyinfo;
4010 /* Use this general default value to start with. */
4011 Vx_resource_name = Vinvocation_name;
4013 validate_x_resource_name ();
4015 dpyinfo = x_term_init (name, (char *)0,
4016 SSDATA (Vx_resource_name));
4018 if (dpyinfo == 0)
4019 error ("Cannot connect to X server %s", SDATA (name));
4021 x_in_use = 1;
4022 XSETFASTINT (Vwindow_system_version, 11);
4024 return dpyinfo;
4028 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4029 1, 3, 0,
4030 doc: /* Open a connection to a display server.
4031 DISPLAY is the name of the display to connect to.
4032 Optional second arg XRM-STRING is a string of resources in xrdb format.
4033 If the optional third arg MUST-SUCCEED is non-nil,
4034 terminate Emacs if we can't open the connection.
4035 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4036 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4038 char *xrm_option;
4039 struct x_display_info *dpyinfo;
4041 CHECK_STRING (display);
4042 if (! NILP (xrm_string))
4043 CHECK_STRING (xrm_string);
4045 #if 0
4046 if (! EQ (Vinitial_window_system, intern ("x")))
4047 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4048 #endif
4050 if (! NILP (xrm_string))
4051 xrm_option = SSDATA (xrm_string);
4052 else
4053 xrm_option = (char *) 0;
4055 validate_x_resource_name ();
4057 /* This is what opens the connection and sets x_current_display.
4058 This also initializes many symbols, such as those used for input. */
4059 dpyinfo = x_term_init (display, xrm_option,
4060 SSDATA (Vx_resource_name));
4062 if (dpyinfo == 0)
4064 if (!NILP (must_succeed))
4065 fatal ("Cannot connect to X server %s.\n\
4066 Check the DISPLAY environment variable or use `-d'.\n\
4067 Also use the `xauth' program to verify that you have the proper\n\
4068 authorization information needed to connect the X server.\n\
4069 An insecure way to solve the problem may be to use `xhost'.\n",
4070 SDATA (display));
4071 else
4072 error ("Cannot connect to X server %s", SDATA (display));
4075 x_in_use = 1;
4077 XSETFASTINT (Vwindow_system_version, 11);
4078 return Qnil;
4081 DEFUN ("x-close-connection", Fx_close_connection,
4082 Sx_close_connection, 1, 1, 0,
4083 doc: /* Close the connection to TERMINAL's X server.
4084 For TERMINAL, specify a terminal object, a frame or a display name (a
4085 string). If TERMINAL is nil, that stands for the selected frame's
4086 terminal. */)
4087 (Lisp_Object terminal)
4089 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4091 if (dpyinfo->reference_count > 0)
4092 error ("Display still has frames on it");
4094 x_delete_terminal (dpyinfo->terminal);
4096 return Qnil;
4099 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4100 doc: /* Return the list of display names that Emacs has connections to. */)
4101 (void)
4103 Lisp_Object tail, result;
4105 result = Qnil;
4106 for (tail = x_display_name_list; CONSP (tail); tail = XCDR (tail))
4107 result = Fcons (XCAR (XCAR (tail)), result);
4109 return result;
4112 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4113 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4114 This function only has an effect on X Windows. With MS Windows, it is
4115 defined but does nothing.
4117 If ON is nil, allow buffering of requests.
4118 Turning on synchronization prohibits the Xlib routines from buffering
4119 requests and seriously degrades performance, but makes debugging much
4120 easier.
4121 The optional second argument TERMINAL specifies which display to act on.
4122 TERMINAL should be a terminal object, a frame or a display name (a string).
4123 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4124 (Lisp_Object on, Lisp_Object terminal)
4126 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4128 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4130 return Qnil;
4133 /* Wait for responses to all X commands issued so far for frame F. */
4135 void
4136 x_sync (FRAME_PTR f)
4138 block_input ();
4139 XSync (FRAME_X_DISPLAY (f), False);
4140 unblock_input ();
4144 /***********************************************************************
4145 Window properties
4146 ***********************************************************************/
4148 DEFUN ("x-change-window-property", Fx_change_window_property,
4149 Sx_change_window_property, 2, 6, 0,
4150 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4151 PROP must be a string. VALUE may be a string or a list of conses,
4152 numbers and/or strings. If an element in the list is a string, it is
4153 converted to an atom and the value of the atom is used. If an element
4154 is a cons, it is converted to a 32 bit number where the car is the 16
4155 top bits and the cdr is the lower 16 bits.
4157 FRAME nil or omitted means use the selected frame.
4158 If TYPE is given and non-nil, it is the name of the type of VALUE.
4159 If TYPE is not given or nil, the type is STRING.
4160 FORMAT gives the size in bits of each element if VALUE is a list.
4161 It must be one of 8, 16 or 32.
4162 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4163 If OUTER-P is non-nil, the property is changed for the outer X window of
4164 FRAME. Default is to change on the edit X window. */)
4165 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4166 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4168 struct frame *f = check_x_frame (frame);
4169 Atom prop_atom;
4170 Atom target_type = XA_STRING;
4171 int element_format = 8;
4172 unsigned char *data;
4173 int nelements;
4174 Window w;
4176 CHECK_STRING (prop);
4178 if (! NILP (format))
4180 CHECK_NUMBER (format);
4182 if (XINT (format) != 8 && XINT (format) != 16
4183 && XINT (format) != 32)
4184 error ("FORMAT must be one of 8, 16 or 32");
4185 element_format = XINT (format);
4188 if (CONSP (value))
4190 ptrdiff_t elsize;
4192 nelements = x_check_property_data (value);
4193 if (nelements == -1)
4194 error ("Bad data in VALUE, must be number, string or cons");
4196 /* The man page for XChangeProperty:
4197 "If the specified format is 32, the property data must be a
4198 long array."
4199 This applies even if long is more than 32 bits. The X library
4200 converts to 32 bits before sending to the X server. */
4201 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4202 data = xnmalloc (nelements, elsize);
4204 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4206 else
4208 CHECK_STRING (value);
4209 data = SDATA (value);
4210 if (INT_MAX < SBYTES (value))
4211 error ("VALUE too long");
4212 nelements = SBYTES (value);
4215 block_input ();
4216 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4217 if (! NILP (type))
4219 CHECK_STRING (type);
4220 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4223 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4224 else w = FRAME_X_WINDOW (f);
4226 XChangeProperty (FRAME_X_DISPLAY (f), w,
4227 prop_atom, target_type, element_format, PropModeReplace,
4228 data, nelements);
4230 if (CONSP (value)) xfree (data);
4232 /* Make sure the property is set when we return. */
4233 XFlush (FRAME_X_DISPLAY (f));
4234 unblock_input ();
4236 return value;
4240 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4241 Sx_delete_window_property, 1, 2, 0,
4242 doc: /* Remove window property PROP from X window of FRAME.
4243 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4244 (Lisp_Object prop, Lisp_Object frame)
4246 struct frame *f = check_x_frame (frame);
4247 Atom prop_atom;
4249 CHECK_STRING (prop);
4250 block_input ();
4251 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4252 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4254 /* Make sure the property is removed when we return. */
4255 XFlush (FRAME_X_DISPLAY (f));
4256 unblock_input ();
4258 return prop;
4262 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4263 1, 6, 0,
4264 doc: /* Value is the value of window property PROP on FRAME.
4265 If FRAME is nil or omitted, use the selected frame.
4267 On X Windows, the following optional arguments are also accepted:
4268 If TYPE is nil or omitted, get the property as a string.
4269 Otherwise TYPE is the name of the atom that denotes the type expected.
4270 If SOURCE is non-nil, get the property on that window instead of from
4271 FRAME. The number 0 denotes the root window.
4272 If DELETE-P is non-nil, delete the property after retrieving it.
4273 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4275 On MS Windows, this function accepts but ignores those optional arguments.
4277 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4278 no value of TYPE (always string in the MS Windows case). */)
4279 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4280 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4282 struct frame *f = check_x_frame (frame);
4283 Atom prop_atom;
4284 int rc;
4285 Lisp_Object prop_value = Qnil;
4286 unsigned char *tmp_data = NULL;
4287 Atom actual_type;
4288 Atom target_type = XA_STRING;
4289 int actual_format;
4290 unsigned long actual_size, bytes_remaining;
4291 Window target_window = FRAME_X_WINDOW (f);
4292 struct gcpro gcpro1;
4294 GCPRO1 (prop_value);
4295 CHECK_STRING (prop);
4297 if (! NILP (source))
4299 CONS_TO_INTEGER (source, Window, target_window);
4300 if (! target_window)
4301 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
4304 block_input ();
4305 if (STRINGP (type))
4307 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4308 target_type = AnyPropertyType;
4309 else
4310 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4313 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4314 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4315 prop_atom, 0, 0, False, target_type,
4316 &actual_type, &actual_format, &actual_size,
4317 &bytes_remaining, &tmp_data);
4318 if (rc == Success)
4320 int size = bytes_remaining;
4322 XFree (tmp_data);
4323 tmp_data = NULL;
4325 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4326 prop_atom, 0, bytes_remaining,
4327 ! NILP (delete_p), target_type,
4328 &actual_type, &actual_format,
4329 &actual_size, &bytes_remaining,
4330 &tmp_data);
4331 if (rc == Success && tmp_data)
4333 /* The man page for XGetWindowProperty says:
4334 "If the returned format is 32, the returned data is represented
4335 as a long array and should be cast to that type to obtain the
4336 elements."
4337 This applies even if long is more than 32 bits, the X library
4338 converts from 32 bit elements received from the X server to long
4339 and passes the long array to us. Thus, for that case memcpy can not
4340 be used. We convert to a 32 bit type here, because so much code
4341 assume on that.
4343 The bytes and offsets passed to XGetWindowProperty refers to the
4344 property and those are indeed in 32 bit quantities if format is
4345 32. */
4347 if (32 < BITS_PER_LONG && actual_format == 32)
4349 unsigned long i;
4350 int *idata = (int *) tmp_data;
4351 long *ldata = (long *) tmp_data;
4353 for (i = 0; i < actual_size; ++i)
4354 idata[i] = (int) ldata[i];
4357 if (NILP (vector_ret_p))
4358 prop_value = make_string ((char *) tmp_data, size);
4359 else
4360 prop_value = x_property_data_to_lisp (f,
4361 tmp_data,
4362 actual_type,
4363 actual_format,
4364 actual_size);
4367 if (tmp_data) XFree (tmp_data);
4370 unblock_input ();
4371 UNGCPRO;
4372 return prop_value;
4377 /***********************************************************************
4378 Busy cursor
4379 ***********************************************************************/
4381 /* Timer function of hourglass_atimer. TIMER is equal to
4382 hourglass_atimer.
4384 Display an hourglass pointer on all frames by mapping the frames'
4385 hourglass_window. Set the hourglass_p flag in the frames'
4386 output_data.x structure to indicate that an hourglass cursor is
4387 shown on the frames. */
4389 void
4390 show_hourglass (struct atimer *timer)
4392 /* The timer implementation will cancel this timer automatically
4393 after this function has run. Set hourglass_atimer to null
4394 so that we know the timer doesn't have to be canceled. */
4395 hourglass_atimer = NULL;
4397 if (!hourglass_shown_p)
4399 Lisp_Object rest, frame;
4401 block_input ();
4403 FOR_EACH_FRAME (rest, frame)
4405 struct frame *f = XFRAME (frame);
4407 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4409 Display *dpy = FRAME_X_DISPLAY (f);
4411 #ifdef USE_X_TOOLKIT
4412 if (f->output_data.x->widget)
4413 #else
4414 if (FRAME_OUTER_WINDOW (f))
4415 #endif
4417 f->output_data.x->hourglass_p = 1;
4419 if (!f->output_data.x->hourglass_window)
4421 unsigned long mask = CWCursor;
4422 XSetWindowAttributes attrs;
4423 #ifdef USE_GTK
4424 Window parent = FRAME_X_WINDOW (f);
4425 #else
4426 Window parent = FRAME_OUTER_WINDOW (f);
4427 #endif
4428 attrs.cursor = f->output_data.x->hourglass_cursor;
4430 f->output_data.x->hourglass_window
4431 = XCreateWindow (dpy, parent,
4432 0, 0, 32000, 32000, 0, 0,
4433 InputOnly,
4434 CopyFromParent,
4435 mask, &attrs);
4438 XMapRaised (dpy, f->output_data.x->hourglass_window);
4439 XFlush (dpy);
4444 hourglass_shown_p = 1;
4445 unblock_input ();
4450 /* Hide the hourglass pointer on all frames, if it is currently
4451 shown. */
4453 void
4454 hide_hourglass (void)
4456 if (hourglass_shown_p)
4458 Lisp_Object rest, frame;
4460 block_input ();
4461 FOR_EACH_FRAME (rest, frame)
4463 struct frame *f = XFRAME (frame);
4465 if (FRAME_X_P (f)
4466 /* Watch out for newly created frames. */
4467 && f->output_data.x->hourglass_window)
4469 XUnmapWindow (FRAME_X_DISPLAY (f),
4470 f->output_data.x->hourglass_window);
4471 /* Sync here because XTread_socket looks at the
4472 hourglass_p flag that is reset to zero below. */
4473 XSync (FRAME_X_DISPLAY (f), False);
4474 f->output_data.x->hourglass_p = 0;
4478 hourglass_shown_p = 0;
4479 unblock_input ();
4485 /***********************************************************************
4486 Tool tips
4487 ***********************************************************************/
4489 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4490 Lisp_Object, Lisp_Object);
4491 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4492 Lisp_Object, int, int, int *, int *);
4494 /* The frame of a currently visible tooltip. */
4496 Lisp_Object tip_frame;
4498 /* If non-nil, a timer started that hides the last tooltip when it
4499 fires. */
4501 static Lisp_Object tip_timer;
4502 Window tip_window;
4504 /* If non-nil, a vector of 3 elements containing the last args
4505 with which x-show-tip was called. See there. */
4507 static Lisp_Object last_show_tip_args;
4510 static Lisp_Object
4511 unwind_create_tip_frame (Lisp_Object frame)
4513 Lisp_Object deleted;
4515 deleted = unwind_create_frame (frame);
4516 if (EQ (deleted, Qt))
4518 tip_window = None;
4519 tip_frame = Qnil;
4522 return deleted;
4526 /* Create a frame for a tooltip on the display described by DPYINFO.
4527 PARMS is a list of frame parameters. TEXT is the string to
4528 display in the tip frame. Value is the frame.
4530 Note that functions called here, esp. x_default_parameter can
4531 signal errors, for instance when a specified color name is
4532 undefined. We have to make sure that we're in a consistent state
4533 when this happens. */
4535 static Lisp_Object
4536 x_create_tip_frame (struct x_display_info *dpyinfo,
4537 Lisp_Object parms,
4538 Lisp_Object text)
4540 struct frame *f;
4541 Lisp_Object frame;
4542 Lisp_Object name;
4543 int width, height;
4544 ptrdiff_t count = SPECPDL_INDEX ();
4545 struct gcpro gcpro1, gcpro2, gcpro3;
4546 int face_change_count_before = face_change_count;
4547 Lisp_Object buffer;
4548 struct buffer *old_buffer;
4550 check_x ();
4552 if (!dpyinfo->terminal->name)
4553 error ("Terminal is not live, can't create new frames on it");
4555 parms = Fcopy_alist (parms);
4557 /* Get the name of the frame to use for resource lookup. */
4558 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4559 if (!STRINGP (name)
4560 && !EQ (name, Qunbound)
4561 && !NILP (name))
4562 error ("Invalid frame name--not a string or nil");
4564 frame = Qnil;
4565 GCPRO3 (parms, name, frame);
4566 f = make_frame (1);
4567 XSETFRAME (frame, f);
4569 buffer = Fget_buffer_create (build_string (" *tip*"));
4570 /* Use set_window_buffer instead of Fset_window_buffer (see
4571 discussion of bug#11984, bug#12025, bug#12026). */
4572 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
4573 old_buffer = current_buffer;
4574 set_buffer_internal_1 (XBUFFER (buffer));
4575 bset_truncate_lines (current_buffer, Qnil);
4576 specbind (Qinhibit_read_only, Qt);
4577 specbind (Qinhibit_modification_hooks, Qt);
4578 Ferase_buffer ();
4579 Finsert (1, &text);
4580 set_buffer_internal_1 (old_buffer);
4582 record_unwind_protect (unwind_create_tip_frame, frame);
4584 f->terminal = dpyinfo->terminal;
4586 /* By setting the output method, we're essentially saying that
4587 the frame is live, as per FRAME_LIVE_P. If we get a signal
4588 from this point on, x_destroy_window might screw up reference
4589 counts etc. */
4590 f->output_method = output_x_window;
4591 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4592 f->output_data.x->icon_bitmap = -1;
4593 FRAME_FONTSET (f) = -1;
4594 f->output_data.x->scroll_bar_foreground_pixel = -1;
4595 f->output_data.x->scroll_bar_background_pixel = -1;
4596 #ifdef USE_TOOLKIT_SCROLL_BARS
4597 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4598 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4599 #endif /* USE_TOOLKIT_SCROLL_BARS */
4600 fset_icon_name (f, Qnil);
4601 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4602 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4603 f->output_data.x->explicit_parent = 0;
4605 /* These colors will be set anyway later, but it's important
4606 to get the color reference counts right, so initialize them! */
4608 Lisp_Object black;
4609 struct gcpro gcpro1;
4611 /* Function x_decode_color can signal an error. Make
4612 sure to initialize color slots so that we won't try
4613 to free colors we haven't allocated. */
4614 FRAME_FOREGROUND_PIXEL (f) = -1;
4615 FRAME_BACKGROUND_PIXEL (f) = -1;
4616 f->output_data.x->cursor_pixel = -1;
4617 f->output_data.x->cursor_foreground_pixel = -1;
4618 f->output_data.x->border_pixel = -1;
4619 f->output_data.x->mouse_pixel = -1;
4621 black = build_string ("black");
4622 GCPRO1 (black);
4623 FRAME_FOREGROUND_PIXEL (f)
4624 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4625 FRAME_BACKGROUND_PIXEL (f)
4626 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4627 f->output_data.x->cursor_pixel
4628 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4629 f->output_data.x->cursor_foreground_pixel
4630 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4631 f->output_data.x->border_pixel
4632 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4633 f->output_data.x->mouse_pixel
4634 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4635 UNGCPRO;
4638 /* Set the name; the functions to which we pass f expect the name to
4639 be set. */
4640 if (EQ (name, Qunbound) || NILP (name))
4642 fset_name (f, build_string (dpyinfo->x_id_name));
4643 f->explicit_name = 0;
4645 else
4647 fset_name (f, name);
4648 f->explicit_name = 1;
4649 /* use the frame's title when getting resources for this frame. */
4650 specbind (Qx_resource_name, name);
4653 f->resx = dpyinfo->resx;
4654 f->resy = dpyinfo->resy;
4656 register_font_driver (&xfont_driver, f);
4657 #ifdef HAVE_FREETYPE
4658 #ifdef HAVE_XFT
4659 register_font_driver (&xftfont_driver, f);
4660 #else /* not HAVE_XFT */
4661 register_font_driver (&ftxfont_driver, f);
4662 #endif /* not HAVE_XFT */
4663 #endif /* HAVE_FREETYPE */
4665 x_default_parameter (f, parms, Qfont_backend, Qnil,
4666 "fontBackend", "FontBackend", RES_TYPE_STRING);
4668 /* Extract the window parameters from the supplied values that are
4669 needed to determine window geometry. */
4670 x_default_font_parameter (f, parms);
4672 x_default_parameter (f, parms, Qborder_width, make_number (0),
4673 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4675 /* This defaults to 2 in order to match xterm. We recognize either
4676 internalBorderWidth or internalBorder (which is what xterm calls
4677 it). */
4678 if (NILP (Fassq (Qinternal_border_width, parms)))
4680 Lisp_Object value;
4682 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4683 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4684 if (! EQ (value, Qunbound))
4685 parms = Fcons (Fcons (Qinternal_border_width, value),
4686 parms);
4689 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4690 "internalBorderWidth", "internalBorderWidth",
4691 RES_TYPE_NUMBER);
4693 /* Also do the stuff which must be set before the window exists. */
4694 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4695 "foreground", "Foreground", RES_TYPE_STRING);
4696 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4697 "background", "Background", RES_TYPE_STRING);
4698 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4699 "pointerColor", "Foreground", RES_TYPE_STRING);
4700 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4701 "cursorColor", "Foreground", RES_TYPE_STRING);
4702 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4703 "borderColor", "BorderColor", RES_TYPE_STRING);
4705 #ifdef GLYPH_DEBUG
4706 image_cache_refcount =
4707 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4708 dpyinfo_refcount = dpyinfo->reference_count;
4709 #endif /* GLYPH_DEBUG */
4711 /* Init faces before x_default_parameter is called for scroll-bar
4712 parameters because that function calls x_set_scroll_bar_width,
4713 which calls change_frame_size, which calls Fset_window_buffer,
4714 which runs hooks, which call Fvertical_motion. At the end, we
4715 end up in init_iterator with a null face cache, which should not
4716 happen. */
4717 init_frame_faces (f);
4719 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4721 x_figure_window_size (f, parms, 0);
4724 XSetWindowAttributes attrs;
4725 unsigned long mask;
4726 Atom type = FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
4728 block_input ();
4729 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4730 if (DoesSaveUnders (dpyinfo->screen))
4731 mask |= CWSaveUnder;
4733 /* Window managers look at the override-redirect flag to determine
4734 whether or net to give windows a decoration (Xlib spec, chapter
4735 3.2.8). */
4736 attrs.override_redirect = True;
4737 attrs.save_under = True;
4738 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4739 /* Arrange for getting MapNotify and UnmapNotify events. */
4740 attrs.event_mask = StructureNotifyMask;
4741 tip_window
4742 = FRAME_X_WINDOW (f)
4743 = XCreateWindow (FRAME_X_DISPLAY (f),
4744 FRAME_X_DISPLAY_INFO (f)->root_window,
4745 /* x, y, width, height */
4746 0, 0, 1, 1,
4747 /* Border. */
4748 f->border_width,
4749 CopyFromParent, InputOutput, CopyFromParent,
4750 mask, &attrs);
4751 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
4752 FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type,
4753 XA_ATOM, 32, PropModeReplace,
4754 (unsigned char *)&type, 1);
4755 unblock_input ();
4758 x_make_gc (f);
4760 x_default_parameter (f, parms, Qauto_raise, Qnil,
4761 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4762 x_default_parameter (f, parms, Qauto_lower, Qnil,
4763 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4764 x_default_parameter (f, parms, Qcursor_type, Qbox,
4765 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4767 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4768 Change will not be effected unless different from the current
4769 FRAME_LINES (f). */
4770 width = FRAME_COLS (f);
4771 height = FRAME_LINES (f);
4772 SET_FRAME_COLS (f, 0);
4773 FRAME_LINES (f) = 0;
4774 change_frame_size (f, height, width, 1, 0, 0);
4776 /* Add `tooltip' frame parameter's default value. */
4777 if (NILP (Fframe_parameter (frame, Qtooltip)))
4778 Fmodify_frame_parameters (frame, Fcons (Fcons (Qtooltip, Qt), Qnil));
4780 /* FIXME - can this be done in a similar way to normal frames?
4781 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
4783 /* Set the `display-type' frame parameter before setting up faces. */
4785 Lisp_Object disptype;
4787 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
4788 disptype = intern ("mono");
4789 else if (FRAME_X_DISPLAY_INFO (f)->visual->class == GrayScale
4790 || FRAME_X_DISPLAY_INFO (f)->visual->class == StaticGray)
4791 disptype = intern ("grayscale");
4792 else
4793 disptype = intern ("color");
4795 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
4796 Fmodify_frame_parameters (frame, Fcons (Fcons (Qdisplay_type, disptype),
4797 Qnil));
4800 /* Set up faces after all frame parameters are known. This call
4801 also merges in face attributes specified for new frames.
4803 Frame parameters may be changed if .Xdefaults contains
4804 specifications for the default font. For example, if there is an
4805 `Emacs.default.attributeBackground: pink', the `background-color'
4806 attribute of the frame get's set, which let's the internal border
4807 of the tooltip frame appear in pink. Prevent this. */
4809 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
4811 /* Set tip_frame here, so that */
4812 tip_frame = frame;
4813 call2 (Qface_set_after_frame_default, frame, Qnil);
4815 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
4816 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
4817 Qnil));
4820 f->no_split = 1;
4822 UNGCPRO;
4824 /* Now that the frame will be official, it counts as a reference to
4825 its display and terminal. */
4826 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4827 f->terminal->reference_count++;
4829 /* It is now ok to make the frame official even if we get an error
4830 below. And the frame needs to be on Vframe_list or making it
4831 visible won't work. */
4832 Vframe_list = Fcons (frame, Vframe_list);
4835 /* Setting attributes of faces of the tooltip frame from resources
4836 and similar will increment face_change_count, which leads to the
4837 clearing of all current matrices. Since this isn't necessary
4838 here, avoid it by resetting face_change_count to the value it
4839 had before we created the tip frame. */
4840 face_change_count = face_change_count_before;
4842 /* Discard the unwind_protect. */
4843 return unbind_to (count, frame);
4847 /* Compute where to display tip frame F. PARMS is the list of frame
4848 parameters for F. DX and DY are specified offsets from the current
4849 location of the mouse. WIDTH and HEIGHT are the width and height
4850 of the tooltip. Return coordinates relative to the root window of
4851 the display in *ROOT_X, and *ROOT_Y. */
4853 static void
4854 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)
4856 Lisp_Object left, top;
4857 int win_x, win_y;
4858 Window root, child;
4859 unsigned pmask;
4861 /* User-specified position? */
4862 left = Fcdr (Fassq (Qleft, parms));
4863 top = Fcdr (Fassq (Qtop, parms));
4865 /* Move the tooltip window where the mouse pointer is. Resize and
4866 show it. */
4867 if (!INTEGERP (left) || !INTEGERP (top))
4869 block_input ();
4870 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
4871 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
4872 unblock_input ();
4875 if (INTEGERP (top))
4876 *root_y = XINT (top);
4877 else if (*root_y + XINT (dy) <= 0)
4878 *root_y = 0; /* Can happen for negative dy */
4879 else if (*root_y + XINT (dy) + height
4880 <= x_display_pixel_height (FRAME_X_DISPLAY_INFO (f)))
4881 /* It fits below the pointer */
4882 *root_y += XINT (dy);
4883 else if (height + XINT (dy) <= *root_y)
4884 /* It fits above the pointer. */
4885 *root_y -= height + XINT (dy);
4886 else
4887 /* Put it on the top. */
4888 *root_y = 0;
4890 if (INTEGERP (left))
4891 *root_x = XINT (left);
4892 else if (*root_x + XINT (dx) <= 0)
4893 *root_x = 0; /* Can happen for negative dx */
4894 else if (*root_x + XINT (dx) + width
4895 <= x_display_pixel_width (FRAME_X_DISPLAY_INFO (f)))
4896 /* It fits to the right of the pointer. */
4897 *root_x += XINT (dx);
4898 else if (width + XINT (dx) <= *root_x)
4899 /* It fits to the left of the pointer. */
4900 *root_x -= width + XINT (dx);
4901 else
4902 /* Put it left-justified on the screen--it ought to fit that way. */
4903 *root_x = 0;
4907 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
4908 doc: /* Show STRING in a "tooltip" window on frame FRAME.
4909 A tooltip window is a small X window displaying a string.
4911 This is an internal function; Lisp code should call `tooltip-show'.
4913 FRAME nil or omitted means use the selected frame.
4915 PARMS is an optional list of frame parameters which can be used to
4916 change the tooltip's appearance.
4918 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
4919 means use the default timeout of 5 seconds.
4921 If the list of frame parameters PARMS contains a `left' parameters,
4922 the tooltip is displayed at that x-position. Otherwise it is
4923 displayed at the mouse position, with offset DX added (default is 5 if
4924 DX isn't specified). Likewise for the y-position; if a `top' frame
4925 parameter is specified, it determines the y-position of the tooltip
4926 window, otherwise it is displayed at the mouse position, with offset
4927 DY added (default is -10).
4929 A tooltip's maximum size is specified by `x-max-tooltip-size'.
4930 Text larger than the specified size is clipped. */)
4931 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
4933 struct frame *f;
4934 struct window *w;
4935 int root_x, root_y;
4936 struct buffer *old_buffer;
4937 struct text_pos pos;
4938 int i, width, height, seen_reversed_p;
4939 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4940 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4941 ptrdiff_t count = SPECPDL_INDEX ();
4943 specbind (Qinhibit_redisplay, Qt);
4945 GCPRO4 (string, parms, frame, timeout);
4947 CHECK_STRING (string);
4948 if (SCHARS (string) == 0)
4949 string = make_unibyte_string (" ", 1);
4951 f = check_x_frame (frame);
4952 if (NILP (timeout))
4953 timeout = make_number (5);
4954 else
4955 CHECK_NATNUM (timeout);
4957 if (NILP (dx))
4958 dx = make_number (5);
4959 else
4960 CHECK_NUMBER (dx);
4962 if (NILP (dy))
4963 dy = make_number (-10);
4964 else
4965 CHECK_NUMBER (dy);
4967 #ifdef USE_GTK
4968 if (x_gtk_use_system_tooltips)
4970 bool ok;
4972 /* Hide a previous tip, if any. */
4973 Fx_hide_tip ();
4975 block_input ();
4976 ok = xg_prepare_tooltip (f, string, &width, &height);
4977 if (ok)
4979 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
4980 xg_show_tooltip (f, root_x, root_y);
4981 /* This is used in Fx_hide_tip. */
4982 XSETFRAME (tip_frame, f);
4984 unblock_input ();
4985 if (ok) goto start_timer;
4987 #endif /* USE_GTK */
4989 if (NILP (last_show_tip_args))
4990 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
4992 if (!NILP (tip_frame))
4994 Lisp_Object last_string = AREF (last_show_tip_args, 0);
4995 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
4996 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
4998 if (EQ (frame, last_frame)
4999 && !NILP (Fequal (last_string, string))
5000 && !NILP (Fequal (last_parms, parms)))
5002 struct frame *tip_f = XFRAME (tip_frame);
5004 /* Only DX and DY have changed. */
5005 if (!NILP (tip_timer))
5007 Lisp_Object timer = tip_timer;
5008 tip_timer = Qnil;
5009 call1 (Qcancel_timer, timer);
5012 block_input ();
5013 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5014 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5015 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5016 root_x, root_y);
5017 unblock_input ();
5018 goto start_timer;
5022 /* Hide a previous tip, if any. */
5023 Fx_hide_tip ();
5025 ASET (last_show_tip_args, 0, string);
5026 ASET (last_show_tip_args, 1, frame);
5027 ASET (last_show_tip_args, 2, parms);
5029 /* Add default values to frame parameters. */
5030 if (NILP (Fassq (Qname, parms)))
5031 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5032 if (NILP (Fassq (Qinternal_border_width, parms)))
5033 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5034 if (NILP (Fassq (Qborder_width, parms)))
5035 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5036 if (NILP (Fassq (Qborder_color, parms)))
5037 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5038 if (NILP (Fassq (Qbackground_color, parms)))
5039 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5040 parms);
5042 /* Create a frame for the tooltip, and record it in the global
5043 variable tip_frame. */
5044 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
5045 f = XFRAME (frame);
5047 /* Set up the frame's root window. */
5048 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5049 wset_left_col (w, make_number (0));
5050 wset_top_line (w, make_number (0));
5052 if (CONSP (Vx_max_tooltip_size)
5053 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5054 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5056 wset_total_cols (w, XCAR (Vx_max_tooltip_size));
5057 wset_total_lines (w, XCDR (Vx_max_tooltip_size));
5059 else
5061 wset_total_cols (w, make_number (80));
5062 wset_total_lines (w, make_number (40));
5065 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5066 adjust_glyphs (f);
5067 w->pseudo_window_p = 1;
5069 /* Display the tooltip text in a temporary buffer. */
5070 old_buffer = current_buffer;
5071 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5072 bset_truncate_lines (current_buffer, Qnil);
5073 clear_glyph_matrix (w->desired_matrix);
5074 clear_glyph_matrix (w->current_matrix);
5075 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5076 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5078 /* Compute width and height of the tooltip. */
5079 width = height = seen_reversed_p = 0;
5080 for (i = 0; i < w->desired_matrix->nrows; ++i)
5082 struct glyph_row *row = &w->desired_matrix->rows[i];
5083 struct glyph *last;
5084 int row_width;
5086 /* Stop at the first empty row at the end. */
5087 if (!row->enabled_p || !row->displays_text_p)
5088 break;
5090 /* Let the row go over the full width of the frame. */
5091 row->full_width_p = 1;
5093 row_width = row->pixel_width;
5094 if (row->used[TEXT_AREA])
5096 /* There's a glyph at the end of rows that is used to place
5097 the cursor there. Don't include the width of this glyph. */
5098 if (!row->reversed_p)
5100 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5101 if (INTEGERP (last->object))
5102 row_width -= last->pixel_width;
5104 else
5106 /* There could be a stretch glyph at the beginning of R2L
5107 rows that is produced by extend_face_to_end_of_line.
5108 Don't count that glyph. */
5109 struct glyph *g = row->glyphs[TEXT_AREA];
5111 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5113 row_width -= g->pixel_width;
5114 seen_reversed_p = 1;
5119 height += row->height;
5120 width = max (width, row_width);
5123 /* If we've seen partial-length R2L rows, we need to re-adjust the
5124 tool-tip frame width and redisplay it again, to avoid over-wide
5125 tips due to the stretch glyph that extends R2L lines to full
5126 width of the frame. */
5127 if (seen_reversed_p)
5129 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5130 not in pixels. */
5131 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5132 wset_total_cols (w, make_number (width));
5133 FRAME_TOTAL_COLS (f) = width;
5134 adjust_glyphs (f);
5135 clear_glyph_matrix (w->desired_matrix);
5136 clear_glyph_matrix (w->current_matrix);
5137 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5138 width = height = 0;
5139 /* Recompute width and height of the tooltip. */
5140 for (i = 0; i < w->desired_matrix->nrows; ++i)
5142 struct glyph_row *row = &w->desired_matrix->rows[i];
5143 struct glyph *last;
5144 int row_width;
5146 if (!row->enabled_p || !row->displays_text_p)
5147 break;
5148 row->full_width_p = 1;
5149 row_width = row->pixel_width;
5150 if (row->used[TEXT_AREA] && !row->reversed_p)
5152 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5153 if (INTEGERP (last->object))
5154 row_width -= last->pixel_width;
5157 height += row->height;
5158 width = max (width, row_width);
5162 /* Add the frame's internal border to the width and height the X
5163 window should have. */
5164 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5165 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5167 /* Move the tooltip window where the mouse pointer is. Resize and
5168 show it. */
5169 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5171 block_input ();
5172 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5173 root_x, root_y, width, height);
5174 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5175 unblock_input ();
5177 /* Draw into the window. */
5178 w->must_be_updated_p = 1;
5179 update_single_window (w, 1);
5181 /* Restore original current buffer. */
5182 set_buffer_internal_1 (old_buffer);
5183 windows_or_buffers_changed = old_windows_or_buffers_changed;
5185 start_timer:
5186 /* Let the tip disappear after timeout seconds. */
5187 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5188 intern ("x-hide-tip"));
5190 UNGCPRO;
5191 return unbind_to (count, Qnil);
5195 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5196 doc: /* Hide the current tooltip window, if there is any.
5197 Value is t if tooltip was open, nil otherwise. */)
5198 (void)
5200 ptrdiff_t count;
5201 Lisp_Object deleted, frame, timer;
5202 struct gcpro gcpro1, gcpro2;
5204 /* Return quickly if nothing to do. */
5205 if (NILP (tip_timer) && NILP (tip_frame))
5206 return Qnil;
5208 frame = tip_frame;
5209 timer = tip_timer;
5210 GCPRO2 (frame, timer);
5211 tip_frame = tip_timer = deleted = Qnil;
5213 count = SPECPDL_INDEX ();
5214 specbind (Qinhibit_redisplay, Qt);
5215 specbind (Qinhibit_quit, Qt);
5217 if (!NILP (timer))
5218 call1 (Qcancel_timer, timer);
5220 #ifdef USE_GTK
5222 /* When using system tooltip, tip_frame is the Emacs frame on which
5223 the tip is shown. */
5224 struct frame *f = XFRAME (frame);
5225 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5226 frame = Qnil;
5228 #endif
5230 if (FRAMEP (frame))
5232 delete_frame (frame, Qnil);
5233 deleted = Qt;
5235 #ifdef USE_LUCID
5236 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5237 redisplay procedure is not called when a tip frame over menu
5238 items is unmapped. Redisplay the menu manually... */
5240 Widget w;
5241 struct frame *f = SELECTED_FRAME ();
5242 w = f->output_data.x->menubar_widget;
5244 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5245 && w != NULL)
5247 block_input ();
5248 xlwmenu_redisplay (w);
5249 unblock_input ();
5252 #endif /* USE_LUCID */
5255 UNGCPRO;
5256 return unbind_to (count, deleted);
5261 /***********************************************************************
5262 File selection dialog
5263 ***********************************************************************/
5265 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5266 Sx_uses_old_gtk_dialog,
5267 0, 0, 0,
5268 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5269 (void)
5271 #ifdef USE_GTK
5272 if (use_dialog_box
5273 && use_file_dialog
5274 && have_menus_p ()
5275 && xg_uses_old_file_dialog ())
5276 return Qt;
5277 #endif
5278 return Qnil;
5282 #ifdef USE_MOTIF
5283 /* Callback for "OK" and "Cancel" on file selection dialog. */
5285 static void
5286 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5288 int *result = (int *) client_data;
5289 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5290 *result = cb->reason;
5294 /* Callback for unmapping a file selection dialog. This is used to
5295 capture the case where a dialog is closed via a window manager's
5296 closer button, for example. Using a XmNdestroyCallback didn't work
5297 in this case. */
5299 static void
5300 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5302 int *result = (int *) client_data;
5303 *result = XmCR_CANCEL;
5306 static Lisp_Object
5307 clean_up_file_dialog (Lisp_Object arg)
5309 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
5310 Widget dialog = (Widget) p->pointer;
5312 /* Clean up. */
5313 block_input ();
5314 XtUnmanageChild (dialog);
5315 XtDestroyWidget (dialog);
5316 x_menu_set_in_use (0);
5317 unblock_input ();
5319 return Qnil;
5323 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5324 doc: /* Read file name, prompting with PROMPT in directory DIR.
5325 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5326 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5327 or directory must exist.
5329 This function is only defined on NS, MS Windows, and X Windows with the
5330 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5331 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5332 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5334 int result;
5335 struct frame *f = SELECTED_FRAME ();
5336 Lisp_Object file = Qnil;
5337 Lisp_Object decoded_file;
5338 Widget dialog, text, help;
5339 Arg al[10];
5340 int ac = 0;
5341 XmString dir_xmstring, pattern_xmstring;
5342 ptrdiff_t count = SPECPDL_INDEX ();
5343 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5345 check_x ();
5347 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5349 if (popup_activated ())
5350 error ("Trying to use a menu from within a menu-entry");
5352 CHECK_STRING (prompt);
5353 CHECK_STRING (dir);
5355 /* Prevent redisplay. */
5356 specbind (Qinhibit_redisplay, Qt);
5358 block_input ();
5360 /* Create the dialog with PROMPT as title, using DIR as initial
5361 directory and using "*" as pattern. */
5362 dir = Fexpand_file_name (dir, Qnil);
5363 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5364 pattern_xmstring = XmStringCreateLocalized ("*");
5366 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5367 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5368 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5369 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5370 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5371 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5372 "fsb", al, ac);
5373 XmStringFree (dir_xmstring);
5374 XmStringFree (pattern_xmstring);
5376 /* Add callbacks for OK and Cancel. */
5377 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5378 (XtPointer) &result);
5379 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5380 (XtPointer) &result);
5381 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5382 (XtPointer) &result);
5384 /* Remove the help button since we can't display help. */
5385 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5386 XtUnmanageChild (help);
5388 /* Mark OK button as default. */
5389 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5390 XmNshowAsDefault, True, NULL);
5392 /* If MUSTMATCH is non-nil, disable the file entry field of the
5393 dialog, so that the user must select a file from the files list
5394 box. We can't remove it because we wouldn't have a way to get at
5395 the result file name, then. */
5396 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5397 if (!NILP (mustmatch))
5399 Widget label;
5400 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5401 XtSetSensitive (text, False);
5402 XtSetSensitive (label, False);
5405 /* Manage the dialog, so that list boxes get filled. */
5406 XtManageChild (dialog);
5408 if (STRINGP (default_filename))
5410 XmString default_xmstring;
5411 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5412 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5414 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5415 XmTextFieldReplace (wtext, 0, last_pos,
5416 (SSDATA (Ffile_name_nondirectory (default_filename))));
5418 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5419 must include the path for this to work. */
5421 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5423 if (XmListItemExists (list, default_xmstring))
5425 int item_pos = XmListItemPos (list, default_xmstring);
5426 /* Select the item and scroll it into view. */
5427 XmListSelectPos (list, item_pos, True);
5428 XmListSetPos (list, item_pos);
5431 XmStringFree (default_xmstring);
5434 record_unwind_protect (clean_up_file_dialog, make_save_value (dialog, 0));
5436 /* Process events until the user presses Cancel or OK. */
5437 x_menu_set_in_use (1);
5438 result = 0;
5439 while (result == 0)
5441 XEvent event;
5442 x_menu_wait_for_event (0);
5443 XtAppNextEvent (Xt_app_con, &event);
5444 if (event.type == KeyPress
5445 && FRAME_X_DISPLAY (f) == event.xkey.display)
5447 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5449 /* Pop down on C-g. */
5450 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5451 XtUnmanageChild (dialog);
5454 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5457 /* Get the result. */
5458 if (result == XmCR_OK)
5460 XmString text_string;
5461 String data;
5463 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5464 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5465 XmStringFree (text_string);
5466 file = build_string (data);
5467 XtFree (data);
5469 else
5470 file = Qnil;
5472 unblock_input ();
5473 UNGCPRO;
5475 /* Make "Cancel" equivalent to C-g. */
5476 if (NILP (file))
5477 Fsignal (Qquit, Qnil);
5479 decoded_file = DECODE_FILE (file);
5481 return unbind_to (count, decoded_file);
5484 #endif /* USE_MOTIF */
5486 #ifdef USE_GTK
5488 static Lisp_Object
5489 clean_up_dialog (Lisp_Object arg)
5491 x_menu_set_in_use (0);
5493 return Qnil;
5496 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5497 doc: /* Read file name, prompting with PROMPT in directory DIR.
5498 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5499 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5500 or directory must exist.
5502 This function is only defined on NS, MS Windows, and X Windows with the
5503 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5504 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5505 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5507 FRAME_PTR f = SELECTED_FRAME ();
5508 char *fn;
5509 Lisp_Object file = Qnil;
5510 Lisp_Object decoded_file;
5511 ptrdiff_t count = SPECPDL_INDEX ();
5512 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5513 char *cdef_file;
5515 check_x ();
5517 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5519 if (popup_activated ())
5520 error ("Trying to use a menu from within a menu-entry");
5522 CHECK_STRING (prompt);
5523 CHECK_STRING (dir);
5525 /* Prevent redisplay. */
5526 specbind (Qinhibit_redisplay, Qt);
5527 record_unwind_protect (clean_up_dialog, Qnil);
5529 block_input ();
5531 if (STRINGP (default_filename))
5532 cdef_file = SSDATA (default_filename);
5533 else
5534 cdef_file = SSDATA (dir);
5536 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5537 ! NILP (mustmatch),
5538 ! NILP (only_dir_p));
5540 if (fn)
5542 file = build_string (fn);
5543 xfree (fn);
5546 unblock_input ();
5547 UNGCPRO;
5549 /* Make "Cancel" equivalent to C-g. */
5550 if (NILP (file))
5551 Fsignal (Qquit, Qnil);
5553 decoded_file = DECODE_FILE (file);
5555 return unbind_to (count, decoded_file);
5559 #ifdef HAVE_FREETYPE
5561 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5562 doc: /* Read a font using a GTK dialog.
5563 Return either a font spec (for GTK versions >= 3.2) or a string
5564 containing a GTK-style font name.
5566 FRAME is the frame on which to pop up the font chooser. If omitted or
5567 nil, it defaults to the selected frame. */)
5568 (Lisp_Object frame, Lisp_Object ignored)
5570 FRAME_PTR f = check_x_frame (frame);
5571 Lisp_Object font;
5572 Lisp_Object font_param;
5573 char *default_name = NULL;
5574 struct gcpro gcpro1, gcpro2;
5575 ptrdiff_t count = SPECPDL_INDEX ();
5577 check_x ();
5579 if (popup_activated ())
5580 error ("Trying to use a menu from within a menu-entry");
5582 /* Prevent redisplay. */
5583 specbind (Qinhibit_redisplay, Qt);
5584 record_unwind_protect (clean_up_dialog, Qnil);
5586 block_input ();
5588 GCPRO2 (font_param, font);
5590 XSETFONT (font, FRAME_FONT (f));
5591 font_param = Ffont_get (font, intern (":name"));
5592 if (STRINGP (font_param))
5593 default_name = xstrdup (SSDATA (font_param));
5594 else
5596 font_param = Fframe_parameter (frame, Qfont_param);
5597 if (STRINGP (font_param))
5598 default_name = xstrdup (SSDATA (font_param));
5601 font = xg_get_font (f, default_name);
5602 xfree (default_name);
5604 unblock_input ();
5606 if (NILP (font))
5607 Fsignal (Qquit, Qnil);
5609 return unbind_to (count, font);
5611 #endif /* HAVE_FREETYPE */
5613 #endif /* USE_GTK */
5616 /***********************************************************************
5617 Keyboard
5618 ***********************************************************************/
5620 #ifdef HAVE_XKBGETKEYBOARD
5621 #include <X11/XKBlib.h>
5622 #include <X11/keysym.h>
5623 #endif
5625 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5626 Sx_backspace_delete_keys_p, 0, 1, 0,
5627 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5628 FRAME nil means use the selected frame.
5629 Value is t if we know that both keys are present, and are mapped to the
5630 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5631 present and mapped to the usual X keysyms. */)
5632 (Lisp_Object frame)
5634 #ifdef HAVE_XKBGETKEYBOARD
5635 XkbDescPtr kb;
5636 struct frame *f = check_x_frame (frame);
5637 Display *dpy = FRAME_X_DISPLAY (f);
5638 Lisp_Object have_keys;
5639 int major, minor, op, event, error_code;
5641 block_input ();
5643 /* Check library version in case we're dynamically linked. */
5644 major = XkbMajorVersion;
5645 minor = XkbMinorVersion;
5646 if (!XkbLibraryVersion (&major, &minor))
5648 unblock_input ();
5649 return Qlambda;
5652 /* Check that the server supports XKB. */
5653 major = XkbMajorVersion;
5654 minor = XkbMinorVersion;
5655 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
5657 unblock_input ();
5658 return Qlambda;
5661 /* In this code we check that the keyboard has physical keys with names
5662 that start with BKSP (Backspace) and DELE (Delete), and that they
5663 generate keysym XK_BackSpace and XK_Delete respectively.
5664 This function is used to test if normal-erase-is-backspace should be
5665 turned on.
5666 An alternative approach would be to just check if XK_BackSpace and
5667 XK_Delete are mapped to any key. But if any of those are mapped to
5668 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5669 user doesn't know about it, it is better to return false here.
5670 It is more obvious to the user what to do if she/he has two keys
5671 clearly marked with names/symbols and one key does something not
5672 expected (i.e. she/he then tries the other).
5673 The cases where Backspace/Delete is mapped to some other key combination
5674 are rare, and in those cases, normal-erase-is-backspace can be turned on
5675 manually. */
5677 have_keys = Qnil;
5678 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5679 if (kb)
5681 int delete_keycode = 0, backspace_keycode = 0, i;
5683 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5685 for (i = kb->min_key_code;
5686 (i < kb->max_key_code
5687 && (delete_keycode == 0 || backspace_keycode == 0));
5688 ++i)
5690 /* The XKB symbolic key names can be seen most easily in
5691 the PS file generated by `xkbprint -label name
5692 $DISPLAY'. */
5693 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5694 delete_keycode = i;
5695 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5696 backspace_keycode = i;
5699 XkbFreeNames (kb, 0, True);
5702 XkbFreeClientMap (kb, 0, True);
5704 if (delete_keycode
5705 && backspace_keycode
5706 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5707 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5708 have_keys = Qt;
5710 unblock_input ();
5711 return have_keys;
5712 #else /* not HAVE_XKBGETKEYBOARD */
5713 return Qlambda;
5714 #endif /* not HAVE_XKBGETKEYBOARD */
5719 /***********************************************************************
5720 Initialization
5721 ***********************************************************************/
5723 /* Keep this list in the same order as frame_parms in frame.c.
5724 Use 0 for unsupported frame parameters. */
5726 frame_parm_handler x_frame_parm_handlers[] =
5728 x_set_autoraise,
5729 x_set_autolower,
5730 x_set_background_color,
5731 x_set_border_color,
5732 x_set_border_width,
5733 x_set_cursor_color,
5734 x_set_cursor_type,
5735 x_set_font,
5736 x_set_foreground_color,
5737 x_set_icon_name,
5738 x_set_icon_type,
5739 x_set_internal_border_width,
5740 x_set_menu_bar_lines,
5741 x_set_mouse_color,
5742 x_explicitly_set_name,
5743 x_set_scroll_bar_width,
5744 x_set_title,
5745 x_set_unsplittable,
5746 x_set_vertical_scroll_bars,
5747 x_set_visibility,
5748 x_set_tool_bar_lines,
5749 x_set_scroll_bar_foreground,
5750 x_set_scroll_bar_background,
5751 x_set_screen_gamma,
5752 x_set_line_spacing,
5753 x_set_fringe_width,
5754 x_set_fringe_width,
5755 x_set_wait_for_wm,
5756 x_set_fullscreen,
5757 x_set_font_backend,
5758 x_set_alpha,
5759 x_set_sticky,
5760 x_set_tool_bar_position,
5763 void
5764 syms_of_xfns (void)
5766 /* This is zero if not using X windows. */
5767 x_in_use = 0;
5769 /* The section below is built by the lisp expression at the top of the file,
5770 just above where these variables are declared. */
5771 /*&&& init symbols here &&&*/
5772 DEFSYM (Qsuppress_icon, "suppress-icon");
5773 DEFSYM (Qundefined_color, "undefined-color");
5774 DEFSYM (Qcompound_text, "compound-text");
5775 DEFSYM (Qcancel_timer, "cancel-timer");
5776 DEFSYM (Qfont_param, "font-parameter");
5777 /* This is the end of symbol initialization. */
5779 Fput (Qundefined_color, Qerror_conditions,
5780 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
5781 Fput (Qundefined_color, Qerror_message,
5782 build_pure_c_string ("Undefined color"));
5784 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
5785 doc: /* The shape of the pointer when over text.
5786 Changing the value does not affect existing frames
5787 unless you set the mouse color. */);
5788 Vx_pointer_shape = Qnil;
5790 #if 0 /* This doesn't really do anything. */
5791 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
5792 doc: /* The shape of the pointer when not over text.
5793 This variable takes effect when you create a new frame
5794 or when you set the mouse color. */);
5795 #endif
5796 Vx_nontext_pointer_shape = Qnil;
5798 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
5799 doc: /* The shape of the pointer when Emacs is busy.
5800 This variable takes effect when you create a new frame
5801 or when you set the mouse color. */);
5802 Vx_hourglass_pointer_shape = Qnil;
5804 #if 0 /* This doesn't really do anything. */
5805 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
5806 doc: /* The shape of the pointer when over the mode line.
5807 This variable takes effect when you create a new frame
5808 or when you set the mouse color. */);
5809 #endif
5810 Vx_mode_pointer_shape = Qnil;
5812 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
5813 Vx_sensitive_text_pointer_shape,
5814 doc: /* The shape of the pointer when over mouse-sensitive text.
5815 This variable takes effect when you create a new frame
5816 or when you set the mouse color. */);
5817 Vx_sensitive_text_pointer_shape = Qnil;
5819 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
5820 Vx_window_horizontal_drag_shape,
5821 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
5822 This variable takes effect when you create a new frame
5823 or when you set the mouse color. */);
5824 Vx_window_horizontal_drag_shape = Qnil;
5826 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
5827 doc: /* A string indicating the foreground color of the cursor box. */);
5828 Vx_cursor_fore_pixel = Qnil;
5830 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
5831 doc: /* Maximum size for tooltips.
5832 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
5833 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
5835 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
5836 doc: /* Non-nil if no X window manager is in use.
5837 Emacs doesn't try to figure this out; this is always nil
5838 unless you set it to something else. */);
5839 /* We don't have any way to find this out, so set it to nil
5840 and maybe the user would like to set it to t. */
5841 Vx_no_window_manager = Qnil;
5843 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
5844 Vx_pixel_size_width_font_regexp,
5845 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
5847 Since Emacs gets width of a font matching with this regexp from
5848 PIXEL_SIZE field of the name, font finding mechanism gets faster for
5849 such a font. This is especially effective for such large fonts as
5850 Chinese, Japanese, and Korean. */);
5851 Vx_pixel_size_width_font_regexp = Qnil;
5853 /* This is not ifdef:ed, so other builds than GTK can customize it. */
5854 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
5855 doc: /* Non-nil means prompt with the old GTK file selection dialog.
5856 If nil or if the file selection dialog is not available, the new GTK file
5857 chooser is used instead. To turn off all file dialogs set the
5858 variable `use-file-dialog'. */);
5859 x_gtk_use_old_file_dialog = 0;
5861 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
5862 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
5863 Note that this is just the default, there is a toggle button on the file
5864 chooser to show or not show hidden files on a case by case basis. */);
5865 x_gtk_show_hidden_files = 0;
5867 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
5868 doc: /* If non-nil, the GTK file chooser will show additional help text.
5869 If more space for files in the file chooser dialog is wanted, set this to nil
5870 to turn the additional text off. */);
5871 x_gtk_file_dialog_help_text = 1;
5873 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
5874 doc: /* If non-nil, a detached tool bar is shown in full.
5875 The default is to just show an arrow and pressing on that arrow shows
5876 the tool bar buttons. */);
5877 x_gtk_whole_detached_tool_bar = 0;
5879 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
5880 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
5881 Otherwise use Emacs own tooltip implementation.
5882 When using Gtk+ tooltips, the tooltip face is not used. */);
5883 x_gtk_use_system_tooltips = 1;
5885 Fprovide (intern_c_string ("x"), Qnil);
5887 #ifdef USE_X_TOOLKIT
5888 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5889 #ifdef USE_MOTIF
5890 Fprovide (intern_c_string ("motif"), Qnil);
5892 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
5893 doc: /* Version info for LessTif/Motif. */);
5894 Vmotif_version_string = build_string (XmVERSION_STRING);
5895 #endif /* USE_MOTIF */
5896 #endif /* USE_X_TOOLKIT */
5898 #ifdef USE_GTK
5899 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
5900 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
5901 But for a user it is a toolkit for X, and indeed, configure
5902 accepts --with-x-toolkit=gtk. */
5903 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5904 Fprovide (intern_c_string ("gtk"), Qnil);
5905 Fprovide (intern_c_string ("move-toolbar"), Qnil);
5907 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
5908 doc: /* Version info for GTK+. */);
5910 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
5911 int len = sprintf (gtk_version, "%d.%d.%d",
5912 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
5913 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
5915 #endif /* USE_GTK */
5917 /* X window properties. */
5918 defsubr (&Sx_change_window_property);
5919 defsubr (&Sx_delete_window_property);
5920 defsubr (&Sx_window_property);
5922 defsubr (&Sxw_display_color_p);
5923 defsubr (&Sx_display_grayscale_p);
5924 defsubr (&Sxw_color_defined_p);
5925 defsubr (&Sxw_color_values);
5926 defsubr (&Sx_server_max_request_size);
5927 defsubr (&Sx_server_vendor);
5928 defsubr (&Sx_server_version);
5929 defsubr (&Sx_display_pixel_width);
5930 defsubr (&Sx_display_pixel_height);
5931 defsubr (&Sx_display_mm_width);
5932 defsubr (&Sx_display_mm_height);
5933 defsubr (&Sx_display_screens);
5934 defsubr (&Sx_display_planes);
5935 defsubr (&Sx_display_color_cells);
5936 defsubr (&Sx_display_visual_class);
5937 defsubr (&Sx_display_backing_store);
5938 defsubr (&Sx_display_save_under);
5939 defsubr (&Sx_wm_set_size_hint);
5940 defsubr (&Sx_create_frame);
5941 defsubr (&Sx_open_connection);
5942 defsubr (&Sx_close_connection);
5943 defsubr (&Sx_display_list);
5944 defsubr (&Sx_synchronize);
5945 defsubr (&Sx_focus_frame);
5946 defsubr (&Sx_backspace_delete_keys_p);
5948 /* Setting callback functions for fontset handler. */
5949 check_window_system_func = check_x;
5951 defsubr (&Sx_show_tip);
5952 defsubr (&Sx_hide_tip);
5953 tip_timer = Qnil;
5954 staticpro (&tip_timer);
5955 tip_frame = Qnil;
5956 staticpro (&tip_frame);
5958 last_show_tip_args = Qnil;
5959 staticpro (&last_show_tip_args);
5961 defsubr (&Sx_uses_old_gtk_dialog);
5962 #if defined (USE_MOTIF) || defined (USE_GTK)
5963 defsubr (&Sx_file_dialog);
5964 #endif
5966 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
5967 defsubr (&Sx_select_font);
5968 #endif
5971 #endif /* HAVE_X_WINDOWS */