Don't call Fset_window_buffer from C code.
[emacs.git] / src / xfns.c
blob3242af937c9fb5142a0e5d42e3831ebc14e78df6
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 <setjmp.h>
24 #include <ctype.h>
25 #include <unistd.h>
27 /* This makes the fields of a Display accessible, in Xlib header files. */
29 #define XLIB_ILLEGAL_ACCESS
31 #include "lisp.h"
32 #include "xterm.h"
33 #include "frame.h"
34 #include "window.h"
35 #include "character.h"
36 #include "buffer.h"
37 #include "intervals.h"
38 #include "dispextern.h"
39 #include "keyboard.h"
40 #include "blockinput.h"
41 #include <epaths.h>
42 #include "charset.h"
43 #include "coding.h"
44 #include "fontset.h"
45 #include "systime.h"
46 #include "termhooks.h"
47 #include "atimer.h"
48 #include "termchar.h"
49 #include "font.h"
51 #ifdef HAVE_X_WINDOWS
53 #include <ctype.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
57 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
58 #include "bitmaps/gray.xbm"
59 #else
60 #include <X11/bitmaps/gray>
61 #endif
63 #include "xsettings.h"
65 #ifdef USE_GTK
66 #include "gtkutil.h"
67 #endif
69 #ifdef USE_X_TOOLKIT
70 #include <X11/Shell.h>
72 #ifndef USE_MOTIF
73 #ifdef HAVE_XAW3D
74 #include <X11/Xaw3d/Paned.h>
75 #include <X11/Xaw3d/Label.h>
76 #else /* !HAVE_XAW3D */
77 #include <X11/Xaw/Paned.h>
78 #include <X11/Xaw/Label.h>
79 #endif /* HAVE_XAW3D */
80 #endif /* USE_MOTIF */
82 #ifdef USG
83 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
84 #include <X11/Xos.h>
85 #define USG
86 #ifdef USG /* Pacify gcc -Wunused-macros. */
87 #endif
88 #else
89 #include <X11/Xos.h>
90 #endif
92 #include "widget.h"
94 #include "../lwlib/lwlib.h"
96 #ifdef USE_MOTIF
97 #include <Xm/Xm.h>
98 #include <Xm/DialogS.h>
99 #include <Xm/FileSB.h>
100 #include <Xm/List.h>
101 #include <Xm/TextF.h>
102 #endif
104 #ifdef USE_LUCID
105 #include "../lwlib/xlwmenu.h"
106 #endif
108 #if !defined (NO_EDITRES)
109 #define HACK_EDITRES
110 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
111 #endif /* not defined NO_EDITRES */
113 /* Unique id counter for widgets created by the Lucid Widget Library. */
115 extern LWLIB_ID widget_id_tick;
117 #ifdef USE_MOTIF
119 #endif /* USE_MOTIF */
121 #endif /* USE_X_TOOLKIT */
123 #ifdef USE_GTK
125 #endif /* USE_GTK */
127 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
129 /* Nonzero if using X. */
131 int x_in_use;
133 static Lisp_Object Qsuppress_icon;
134 static Lisp_Object Qundefined_color;
135 static Lisp_Object Qcompound_text, Qcancel_timer;
136 Lisp_Object Qfont_param;
138 #ifdef GLYPH_DEBUG
139 static ptrdiff_t image_cache_refcount;
140 static int dpyinfo_refcount;
141 #endif
143 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
144 static char *x_last_font_name;
145 #endif
147 static struct x_display_info *x_display_info_for_name (Lisp_Object);
150 /* Error if we are not connected to X. */
152 void
153 check_x (void)
155 if (! x_in_use)
156 error ("X windows are not in use or not initialized");
159 /* Nonzero if we can use mouse menus.
160 You should not call this unless HAVE_MENUS is defined. */
163 have_menus_p (void)
165 return x_in_use;
168 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
169 and checking validity for X. */
171 FRAME_PTR
172 check_x_frame (Lisp_Object frame)
174 FRAME_PTR f;
176 if (NILP (frame))
177 frame = selected_frame;
178 CHECK_LIVE_FRAME (frame);
179 f = XFRAME (frame);
180 if (! FRAME_X_P (f))
181 error ("Non-X frame used");
182 return f;
185 /* Let the user specify an X display with a Lisp object.
186 OBJECT may be nil, a frame or a terminal object.
187 nil stands for the selected frame--or, if that is not an X frame,
188 the first X display on the list. */
190 struct x_display_info *
191 check_x_display_info (Lisp_Object object)
193 struct x_display_info *dpyinfo = NULL;
195 if (NILP (object))
197 struct frame *sf = XFRAME (selected_frame);
199 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
200 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
201 else if (x_display_list != 0)
202 dpyinfo = x_display_list;
203 else
204 error ("X windows are not in use or not initialized");
206 else if (TERMINALP (object))
208 struct terminal *t = get_terminal (object, 1);
210 if (t->type != output_x_window)
211 error ("Terminal %"pI"d is not an X display", XINT (object));
213 dpyinfo = t->display_info.x;
215 else if (STRINGP (object))
216 dpyinfo = x_display_info_for_name (object);
217 else
219 FRAME_PTR f = check_x_frame (object);
220 dpyinfo = FRAME_X_DISPLAY_INFO (f);
223 return dpyinfo;
227 /* Return the Emacs frame-object corresponding to an X window.
228 It could be the frame's main window or an icon window. */
230 /* This function can be called during GC, so use GC_xxx type test macros. */
232 struct frame *
233 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
235 Lisp_Object tail, frame;
236 struct frame *f;
238 if (wdesc == None) return 0;
240 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
242 frame = XCAR (tail);
243 if (!FRAMEP (frame))
244 continue;
245 f = XFRAME (frame);
246 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
247 continue;
248 if (f->output_data.x->hourglass_window == wdesc)
249 return f;
250 #ifdef USE_X_TOOLKIT
251 if ((f->output_data.x->edit_widget
252 && XtWindow (f->output_data.x->edit_widget) == wdesc)
253 /* A tooltip frame? */
254 || (!f->output_data.x->edit_widget
255 && FRAME_X_WINDOW (f) == wdesc)
256 || f->output_data.x->icon_desc == wdesc)
257 return f;
258 #else /* not USE_X_TOOLKIT */
259 #ifdef USE_GTK
260 if (f->output_data.x->edit_widget)
262 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
263 struct x_output *x = f->output_data.x;
264 if (gwdesc != 0 && gwdesc == x->edit_widget)
265 return f;
267 #endif /* USE_GTK */
268 if (FRAME_X_WINDOW (f) == wdesc
269 || f->output_data.x->icon_desc == wdesc)
270 return f;
271 #endif /* not USE_X_TOOLKIT */
273 return 0;
276 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
277 /* Like x_window_to_frame but also compares the window with the widget's
278 windows. */
280 struct frame *
281 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
283 Lisp_Object tail, frame;
284 struct frame *f, *found;
285 struct x_output *x;
287 if (wdesc == None) return NULL;
289 found = NULL;
290 for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
292 frame = XCAR (tail);
293 if (!FRAMEP (frame))
294 continue;
296 f = XFRAME (frame);
297 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
299 /* This frame matches if the window is any of its widgets. */
300 x = f->output_data.x;
301 if (x->hourglass_window == wdesc)
302 found = f;
303 else if (x->widget)
305 #ifdef USE_GTK
306 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
307 if (gwdesc != 0
308 && gtk_widget_get_toplevel (gwdesc) == x->widget)
309 found = f;
310 #else
311 if (wdesc == XtWindow (x->widget)
312 || wdesc == XtWindow (x->column_widget)
313 || wdesc == XtWindow (x->edit_widget))
314 found = f;
315 /* Match if the window is this frame's menubar. */
316 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
317 found = f;
318 #endif
320 else if (FRAME_X_WINDOW (f) == wdesc)
321 /* A tooltip frame. */
322 found = f;
326 return found;
329 /* Likewise, but consider only the menu bar widget. */
331 struct frame *
332 x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
334 Window wdesc = event->xany.window;
335 Lisp_Object tail, frame;
336 struct frame *f;
337 struct x_output *x;
339 if (wdesc == None) return 0;
341 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
343 frame = XCAR (tail);
344 if (!FRAMEP (frame))
345 continue;
346 f = XFRAME (frame);
347 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
348 continue;
349 x = f->output_data.x;
350 #ifdef USE_GTK
351 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
352 return f;
353 #else
354 /* Match if the window is this frame's menubar. */
355 if (x->menubar_widget
356 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
357 return f;
358 #endif
360 return 0;
363 /* Return the frame whose principal (outermost) window is WDESC.
364 If WDESC is some other (smaller) window, we return 0. */
366 struct frame *
367 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
369 Lisp_Object tail, frame;
370 struct frame *f;
371 struct x_output *x;
373 if (wdesc == None) return 0;
375 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
377 frame = XCAR (tail);
378 if (!FRAMEP (frame))
379 continue;
380 f = XFRAME (frame);
381 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
382 continue;
383 x = f->output_data.x;
385 if (x->widget)
387 /* This frame matches if the window is its topmost widget. */
388 #ifdef USE_GTK
389 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
390 if (gwdesc == x->widget)
391 return f;
392 #else
393 if (wdesc == XtWindow (x->widget))
394 return f;
395 #if 0 /* I don't know why it did this,
396 but it seems logically wrong,
397 and it causes trouble for MapNotify events. */
398 /* Match if the window is this frame's menubar. */
399 if (x->menubar_widget
400 && wdesc == XtWindow (x->menubar_widget))
401 return f;
402 #endif
403 #endif
405 else if (FRAME_X_WINDOW (f) == wdesc)
406 /* Tooltip frame. */
407 return f;
409 return 0;
411 #endif /* USE_X_TOOLKIT || USE_GTK */
415 /* Store the screen positions of frame F into XPTR and YPTR.
416 These are the positions of the containing window manager window,
417 not Emacs's own window. */
419 void
420 x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
422 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
423 int real_x = 0, real_y = 0;
424 int had_errors = 0;
425 Window win = f->output_data.x->parent_desc;
426 Atom actual_type;
427 unsigned long actual_size, bytes_remaining;
428 int rc, actual_format;
429 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
430 long max_len = 400;
431 Display *dpy = FRAME_X_DISPLAY (f);
432 unsigned char *tmp_data = NULL;
433 Atom target_type = XA_CARDINAL;
435 BLOCK_INPUT;
437 x_catch_errors (dpy);
439 if (win == dpyinfo->root_window)
440 win = FRAME_OUTER_WINDOW (f);
442 /* This loop traverses up the containment tree until we hit the root
443 window. Window managers may intersect many windows between our window
444 and the root window. The window we find just before the root window
445 should be the outer WM window. */
446 for (;;)
448 Window wm_window, rootw;
449 Window *tmp_children;
450 unsigned int tmp_nchildren;
451 int success;
453 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
454 &wm_window, &tmp_children, &tmp_nchildren);
456 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
458 /* Don't free tmp_children if XQueryTree failed. */
459 if (! success)
460 break;
462 XFree (tmp_children);
464 if (wm_window == rootw || had_errors)
465 break;
467 win = wm_window;
470 if (! had_errors)
472 unsigned int ign;
473 Window child, rootw;
475 /* Get the real coordinates for the WM window upper left corner */
476 XGetGeometry (FRAME_X_DISPLAY (f), win,
477 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
479 /* Translate real coordinates to coordinates relative to our
480 window. For our window, the upper left corner is 0, 0.
481 Since the upper left corner of the WM window is outside
482 our window, win_x and win_y will be negative:
484 ------------------ ---> x
485 | title |
486 | ----------------- v y
487 | | our window
489 XTranslateCoordinates (FRAME_X_DISPLAY (f),
491 /* From-window, to-window. */
492 FRAME_X_DISPLAY_INFO (f)->root_window,
493 FRAME_X_WINDOW (f),
495 /* From-position, to-position. */
496 real_x, real_y, &win_x, &win_y,
498 /* Child of win. */
499 &child);
501 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
503 outer_x = win_x;
504 outer_y = win_y;
506 else
508 XTranslateCoordinates (FRAME_X_DISPLAY (f),
510 /* From-window, to-window. */
511 FRAME_X_DISPLAY_INFO (f)->root_window,
512 FRAME_OUTER_WINDOW (f),
514 /* From-position, to-position. */
515 real_x, real_y, &outer_x, &outer_y,
517 /* Child of win. */
518 &child);
521 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
525 if (dpyinfo->root_window == f->output_data.x->parent_desc)
527 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
528 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
529 0, max_len, False, target_type,
530 &actual_type, &actual_format, &actual_size,
531 &bytes_remaining, &tmp_data);
533 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
534 && actual_size == 4 && actual_format == 32)
536 unsigned int ign;
537 Window rootw;
538 long *fe = (long *)tmp_data;
540 XGetGeometry (FRAME_X_DISPLAY (f), win,
541 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
542 outer_x = -fe[0];
543 outer_y = -fe[2];
544 real_x -= fe[0];
545 real_y -= fe[2];
549 if (tmp_data) XFree (tmp_data);
551 x_uncatch_errors ();
553 UNBLOCK_INPUT;
555 if (had_errors) return;
557 f->x_pixels_diff = -win_x;
558 f->y_pixels_diff = -win_y;
560 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
561 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
563 *xptr = real_x;
564 *yptr = real_y;
570 /* Gamma-correct COLOR on frame F. */
572 void
573 gamma_correct (struct frame *f, XColor *color)
575 if (f->gamma)
577 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
578 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
579 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
584 /* Decide if color named COLOR_NAME is valid for use on frame F. If
585 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
586 allocate the color. Value is zero if COLOR_NAME is invalid, or
587 no color could be allocated. */
590 x_defined_color (struct frame *f, const char *color_name,
591 XColor *color, int alloc_p)
593 int success_p = 0;
594 Display *dpy = FRAME_X_DISPLAY (f);
595 Colormap cmap = FRAME_X_COLORMAP (f);
597 BLOCK_INPUT;
598 #ifdef USE_GTK
599 success_p = xg_check_special_colors (f, color_name, color);
600 #endif
601 if (!success_p)
602 success_p = XParseColor (dpy, cmap, color_name, color);
603 if (success_p && alloc_p)
604 success_p = x_alloc_nearest_color (f, cmap, color);
605 UNBLOCK_INPUT;
607 return success_p;
611 /* Return the pixel color value for color COLOR_NAME on frame F. If F
612 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
613 Signal an error if color can't be allocated. */
615 static int
616 x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
618 XColor cdef;
620 CHECK_STRING (color_name);
622 #if 0 /* Don't do this. It's wrong when we're not using the default
623 colormap, it makes freeing difficult, and it's probably not
624 an important optimization. */
625 if (strcmp (SDATA (color_name), "black") == 0)
626 return BLACK_PIX_DEFAULT (f);
627 else if (strcmp (SDATA (color_name), "white") == 0)
628 return WHITE_PIX_DEFAULT (f);
629 #endif
631 /* Return MONO_COLOR for monochrome frames. */
632 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
633 return mono_color;
635 /* x_defined_color is responsible for coping with failures
636 by looking for a near-miss. */
637 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
638 return cdef.pixel;
640 signal_error ("Undefined color", color_name);
645 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
646 the previous value of that parameter, NEW_VALUE is the new value.
647 See also the comment of wait_for_wm in struct x_output. */
649 static void
650 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
652 f->output_data.x->wait_for_wm = !NILP (new_value);
655 static void
656 x_set_tool_bar_position (struct frame *f,
657 Lisp_Object new_value,
658 Lisp_Object old_value)
660 if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
661 && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
662 return;
663 if (EQ (new_value, old_value)) return;
665 #ifdef USE_GTK
666 if (xg_change_toolbar_position (f, new_value))
667 FSET (f, tool_bar_position, new_value);
668 #endif
671 #ifdef USE_GTK
673 /* Set icon from FILE for frame F. By using GTK functions the icon
674 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
677 xg_set_icon (FRAME_PTR f, Lisp_Object file)
679 int result = 0;
680 Lisp_Object found;
682 found = x_find_image_file (file);
684 if (! NILP (found))
686 GdkPixbuf *pixbuf;
687 GError *err = NULL;
688 char *filename = SSDATA (found);
689 BLOCK_INPUT;
691 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
693 if (pixbuf)
695 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
696 pixbuf);
697 g_object_unref (pixbuf);
699 result = 1;
701 else
702 g_error_free (err);
704 UNBLOCK_INPUT;
707 return result;
711 xg_set_icon_from_xpm_data (FRAME_PTR f, const char **data)
713 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
715 if (!pixbuf)
716 return 0;
718 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
719 g_object_unref (pixbuf);
720 return 1;
722 #endif /* USE_GTK */
725 /* Functions called only from `x_set_frame_param'
726 to set individual parameters.
728 If FRAME_X_WINDOW (f) is 0,
729 the frame is being created and its X-window does not exist yet.
730 In that case, just record the parameter's new value
731 in the standard place; do not attempt to change the window. */
733 static void
734 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
736 struct x_output *x = f->output_data.x;
737 unsigned long fg, old_fg;
739 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
740 old_fg = FRAME_FOREGROUND_PIXEL (f);
741 FRAME_FOREGROUND_PIXEL (f) = fg;
743 if (FRAME_X_WINDOW (f) != 0)
745 Display *dpy = FRAME_X_DISPLAY (f);
747 BLOCK_INPUT;
748 XSetForeground (dpy, x->normal_gc, fg);
749 XSetBackground (dpy, x->reverse_gc, fg);
751 if (x->cursor_pixel == old_fg)
753 unload_color (f, x->cursor_pixel);
754 x->cursor_pixel = x_copy_color (f, fg);
755 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
758 UNBLOCK_INPUT;
760 update_face_from_frame_parameter (f, Qforeground_color, arg);
762 if (FRAME_VISIBLE_P (f))
763 redraw_frame (f);
766 unload_color (f, old_fg);
769 static void
770 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
772 struct x_output *x = f->output_data.x;
773 unsigned long bg;
775 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
776 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
777 FRAME_BACKGROUND_PIXEL (f) = bg;
779 if (FRAME_X_WINDOW (f) != 0)
781 Display *dpy = FRAME_X_DISPLAY (f);
783 BLOCK_INPUT;
784 XSetBackground (dpy, x->normal_gc, bg);
785 XSetForeground (dpy, x->reverse_gc, bg);
786 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
787 XSetForeground (dpy, x->cursor_gc, bg);
789 #ifdef USE_GTK
790 xg_set_background_color (f, bg);
791 #endif
793 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
794 toolkit scroll bars. */
796 Lisp_Object bar;
797 for (bar = FRAME_SCROLL_BARS (f);
798 !NILP (bar);
799 bar = XSCROLL_BAR (bar)->next)
801 Window window = XSCROLL_BAR (bar)->x_window;
802 XSetWindowBackground (dpy, window, bg);
805 #endif /* USE_TOOLKIT_SCROLL_BARS */
807 UNBLOCK_INPUT;
808 update_face_from_frame_parameter (f, Qbackground_color, arg);
810 if (FRAME_VISIBLE_P (f))
811 redraw_frame (f);
815 static Cursor
816 make_invisible_cursor (struct frame *f)
818 Display *dpy = FRAME_X_DISPLAY (f);
819 static char const no_data[] = { 0 };
820 Pixmap pix;
821 XColor col;
822 Cursor c = 0;
824 x_catch_errors (dpy);
825 pix = XCreateBitmapFromData (dpy, FRAME_X_DISPLAY_INFO (f)->root_window,
826 no_data, 1, 1);
827 if (! x_had_errors_p (dpy) && pix != None)
829 Cursor pixc;
830 col.pixel = 0;
831 col.red = col.green = col.blue = 0;
832 col.flags = DoRed | DoGreen | DoBlue;
833 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
834 if (! x_had_errors_p (dpy) && pixc != None)
835 c = pixc;
836 XFreePixmap (dpy, pix);
839 x_uncatch_errors ();
841 return c;
844 static void
845 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
847 struct x_output *x = f->output_data.x;
848 Display *dpy = FRAME_X_DISPLAY (f);
849 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
850 Cursor hourglass_cursor, horizontal_drag_cursor;
851 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
852 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
854 /* Don't let pointers be invisible. */
855 if (mask_color == pixel)
857 x_free_colors (f, &pixel, 1);
858 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
861 unload_color (f, x->mouse_pixel);
862 x->mouse_pixel = pixel;
864 BLOCK_INPUT;
866 /* It's not okay to crash if the user selects a screwy cursor. */
867 x_catch_errors (dpy);
869 if (!NILP (Vx_pointer_shape))
871 CHECK_NUMBER (Vx_pointer_shape);
872 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
874 else
875 cursor = XCreateFontCursor (dpy, XC_xterm);
876 x_check_errors (dpy, "bad text pointer cursor: %s");
878 if (!NILP (Vx_nontext_pointer_shape))
880 CHECK_NUMBER (Vx_nontext_pointer_shape);
881 nontext_cursor
882 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
884 else
885 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
886 x_check_errors (dpy, "bad nontext pointer cursor: %s");
888 if (!NILP (Vx_hourglass_pointer_shape))
890 CHECK_NUMBER (Vx_hourglass_pointer_shape);
891 hourglass_cursor
892 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
894 else
895 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
896 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
898 if (!NILP (Vx_mode_pointer_shape))
900 CHECK_NUMBER (Vx_mode_pointer_shape);
901 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
903 else
904 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
905 x_check_errors (dpy, "bad modeline pointer cursor: %s");
907 if (!NILP (Vx_sensitive_text_pointer_shape))
909 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
910 hand_cursor
911 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
913 else
914 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
916 if (!NILP (Vx_window_horizontal_drag_shape))
918 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
919 horizontal_drag_cursor
920 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
922 else
923 horizontal_drag_cursor
924 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
926 /* Check and report errors with the above calls. */
927 x_check_errors (dpy, "can't set cursor shape: %s");
928 x_uncatch_errors ();
931 XColor fore_color, back_color;
933 fore_color.pixel = x->mouse_pixel;
934 x_query_color (f, &fore_color);
935 back_color.pixel = mask_color;
936 x_query_color (f, &back_color);
938 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
939 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
940 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
941 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
942 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
943 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
946 if (FRAME_X_WINDOW (f) != 0)
947 XDefineCursor (dpy, FRAME_X_WINDOW (f),
948 f->output_data.x->current_cursor = cursor);
950 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor == 0)
951 FRAME_X_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
953 if (cursor != x->text_cursor
954 && x->text_cursor != 0)
955 XFreeCursor (dpy, x->text_cursor);
956 x->text_cursor = cursor;
958 if (nontext_cursor != x->nontext_cursor
959 && x->nontext_cursor != 0)
960 XFreeCursor (dpy, x->nontext_cursor);
961 x->nontext_cursor = nontext_cursor;
963 if (hourglass_cursor != x->hourglass_cursor
964 && x->hourglass_cursor != 0)
965 XFreeCursor (dpy, x->hourglass_cursor);
966 x->hourglass_cursor = hourglass_cursor;
968 if (mode_cursor != x->modeline_cursor
969 && x->modeline_cursor != 0)
970 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
971 x->modeline_cursor = mode_cursor;
973 if (hand_cursor != x->hand_cursor
974 && x->hand_cursor != 0)
975 XFreeCursor (dpy, x->hand_cursor);
976 x->hand_cursor = hand_cursor;
978 if (horizontal_drag_cursor != x->horizontal_drag_cursor
979 && x->horizontal_drag_cursor != 0)
980 XFreeCursor (dpy, x->horizontal_drag_cursor);
981 x->horizontal_drag_cursor = horizontal_drag_cursor;
983 XFlush (dpy);
984 UNBLOCK_INPUT;
986 update_face_from_frame_parameter (f, Qmouse_color, arg);
989 static void
990 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
992 unsigned long fore_pixel, pixel;
993 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
994 struct x_output *x = f->output_data.x;
996 if (!NILP (Vx_cursor_fore_pixel))
998 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
999 WHITE_PIX_DEFAULT (f));
1000 fore_pixel_allocated_p = 1;
1002 else
1003 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1005 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1006 pixel_allocated_p = 1;
1008 /* Make sure that the cursor color differs from the background color. */
1009 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1011 if (pixel_allocated_p)
1013 x_free_colors (f, &pixel, 1);
1014 pixel_allocated_p = 0;
1017 pixel = x->mouse_pixel;
1018 if (pixel == fore_pixel)
1020 if (fore_pixel_allocated_p)
1022 x_free_colors (f, &fore_pixel, 1);
1023 fore_pixel_allocated_p = 0;
1025 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1029 unload_color (f, x->cursor_foreground_pixel);
1030 if (!fore_pixel_allocated_p)
1031 fore_pixel = x_copy_color (f, fore_pixel);
1032 x->cursor_foreground_pixel = fore_pixel;
1034 unload_color (f, x->cursor_pixel);
1035 if (!pixel_allocated_p)
1036 pixel = x_copy_color (f, pixel);
1037 x->cursor_pixel = pixel;
1039 if (FRAME_X_WINDOW (f) != 0)
1041 BLOCK_INPUT;
1042 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1043 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
1044 UNBLOCK_INPUT;
1046 if (FRAME_VISIBLE_P (f))
1048 x_update_cursor (f, 0);
1049 x_update_cursor (f, 1);
1053 update_face_from_frame_parameter (f, Qcursor_color, arg);
1056 /* Set the border-color of frame F to pixel value PIX.
1057 Note that this does not fully take effect if done before
1058 F has an x-window. */
1060 static void
1061 x_set_border_pixel (struct frame *f, int pix)
1063 unload_color (f, f->output_data.x->border_pixel);
1064 f->output_data.x->border_pixel = pix;
1066 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
1068 BLOCK_INPUT;
1069 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
1070 UNBLOCK_INPUT;
1072 if (FRAME_VISIBLE_P (f))
1073 redraw_frame (f);
1077 /* Set the border-color of frame F to value described by ARG.
1078 ARG can be a string naming a color.
1079 The border-color is used for the border that is drawn by the X server.
1080 Note that this does not fully take effect if done before
1081 F has an x-window; it must be redone when the window is created.
1083 Note: this is done in two routines because of the way X10 works.
1085 Note: under X11, this is normally the province of the window manager,
1086 and so emacs' border colors may be overridden. */
1088 static void
1089 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1091 int pix;
1093 CHECK_STRING (arg);
1094 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1095 x_set_border_pixel (f, pix);
1096 update_face_from_frame_parameter (f, Qborder_color, arg);
1100 static void
1101 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1103 set_frame_cursor_types (f, arg);
1105 /* Make sure the cursor gets redrawn. */
1106 cursor_type_changed = 1;
1109 static void
1110 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1112 int result;
1114 if (STRINGP (arg))
1116 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1117 return;
1119 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1120 return;
1122 BLOCK_INPUT;
1123 if (NILP (arg))
1124 result = x_text_icon (f,
1125 SSDATA ((!NILP (f->icon_name)
1126 ? f->icon_name
1127 : f->name)));
1128 else
1129 result = x_bitmap_icon (f, arg);
1131 if (result)
1133 UNBLOCK_INPUT;
1134 error ("No icon window available");
1137 XFlush (FRAME_X_DISPLAY (f));
1138 UNBLOCK_INPUT;
1141 static void
1142 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1144 int result;
1146 if (STRINGP (arg))
1148 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1149 return;
1151 else if (!NILP (arg) || NILP (oldval))
1152 return;
1154 FSET (f, icon_name, arg);
1156 if (f->output_data.x->icon_bitmap != 0)
1157 return;
1159 BLOCK_INPUT;
1161 result = x_text_icon (f,
1162 SSDATA ((!NILP (f->icon_name)
1163 ? f->icon_name
1164 : !NILP (f->title)
1165 ? f->title
1166 : f->name)));
1168 if (result)
1170 UNBLOCK_INPUT;
1171 error ("No icon window available");
1174 XFlush (FRAME_X_DISPLAY (f));
1175 UNBLOCK_INPUT;
1179 void
1180 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1182 int nlines;
1183 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1184 int olines = FRAME_MENU_BAR_LINES (f);
1185 #endif
1187 /* Right now, menu bars don't work properly in minibuf-only frames;
1188 most of the commands try to apply themselves to the minibuffer
1189 frame itself, and get an error because you can't switch buffers
1190 in or split the minibuffer window. */
1191 if (FRAME_MINIBUF_ONLY_P (f))
1192 return;
1194 if (TYPE_RANGED_INTEGERP (int, value))
1195 nlines = XINT (value);
1196 else
1197 nlines = 0;
1199 /* Make sure we redisplay all windows in this frame. */
1200 windows_or_buffers_changed++;
1202 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1203 FRAME_MENU_BAR_LINES (f) = 0;
1204 if (nlines)
1206 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1207 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1208 /* Make sure next redisplay shows the menu bar. */
1209 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1211 else
1213 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1214 free_frame_menubar (f);
1215 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1216 if (FRAME_X_P (f))
1217 f->output_data.x->menubar_widget = 0;
1219 #else /* not USE_X_TOOLKIT && not USE_GTK */
1220 FRAME_MENU_BAR_LINES (f) = nlines;
1221 resize_frame_windows (f, FRAME_LINES (f), 0);
1223 /* If the menu bar height gets changed, the internal border below
1224 the top margin has to be cleared. Also, if the menu bar gets
1225 larger, the area for the added lines has to be cleared except for
1226 the first menu bar line that is to be drawn later. */
1227 if (nlines != olines)
1229 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1230 int width = FRAME_PIXEL_WIDTH (f);
1231 int y;
1233 /* height can be zero here. */
1234 if (height > 0 && width > 0)
1236 y = FRAME_TOP_MARGIN_HEIGHT (f);
1238 BLOCK_INPUT;
1239 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1240 0, y, width, height, False);
1241 UNBLOCK_INPUT;
1244 if (nlines > 1 && nlines > olines)
1246 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1247 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1249 BLOCK_INPUT;
1250 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1251 0, y, width, height, False);
1252 UNBLOCK_INPUT;
1255 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1256 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1258 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1259 adjust_glyphs (f);
1260 run_window_configuration_change_hook (f);
1264 /* Set the number of lines used for the tool bar of frame F to VALUE.
1265 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1266 is the old number of tool bar lines. This function changes the
1267 height of all windows on frame F to match the new tool bar height.
1268 The frame's height doesn't change. */
1270 void
1271 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1273 int delta, nlines, root_height;
1274 Lisp_Object root_window;
1276 /* Treat tool bars like menu bars. */
1277 if (FRAME_MINIBUF_ONLY_P (f))
1278 return;
1280 /* Use VALUE only if an int >= 0. */
1281 if (RANGED_INTEGERP (0, value, INT_MAX))
1282 nlines = XFASTINT (value);
1283 else
1284 nlines = 0;
1286 #ifdef USE_GTK
1287 FRAME_TOOL_BAR_LINES (f) = 0;
1288 if (nlines)
1290 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1291 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1292 /* Make sure next redisplay shows the tool bar. */
1293 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1294 update_frame_tool_bar (f);
1296 else
1298 if (FRAME_EXTERNAL_TOOL_BAR (f))
1299 free_frame_tool_bar (f);
1300 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1303 return;
1304 #endif
1306 /* Make sure we redisplay all windows in this frame. */
1307 ++windows_or_buffers_changed;
1309 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1311 /* Don't resize the tool-bar to more than we have room for. */
1312 root_window = FRAME_ROOT_WINDOW (f);
1313 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1314 if (root_height - delta < 1)
1316 delta = root_height - 1;
1317 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1320 FRAME_TOOL_BAR_LINES (f) = nlines;
1321 resize_frame_windows (f, FRAME_LINES (f), 0);
1322 adjust_glyphs (f);
1324 /* We also have to make sure that the internal border at the top of
1325 the frame, below the menu bar or tool bar, is redrawn when the
1326 tool bar disappears. This is so because the internal border is
1327 below the tool bar if one is displayed, but is below the menu bar
1328 if there isn't a tool bar. The tool bar draws into the area
1329 below the menu bar. */
1330 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1332 clear_frame (f);
1333 clear_current_matrices (f);
1336 /* If the tool bar gets smaller, the internal border below it
1337 has to be cleared. It was formerly part of the display
1338 of the larger tool bar, and updating windows won't clear it. */
1339 if (delta < 0)
1341 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1342 int width = FRAME_PIXEL_WIDTH (f);
1343 int y = (FRAME_MENU_BAR_LINES (f) + nlines) * FRAME_LINE_HEIGHT (f);
1345 /* height can be zero here. */
1346 if (height > 0 && width > 0)
1348 BLOCK_INPUT;
1349 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1350 0, y, width, height, False);
1351 UNBLOCK_INPUT;
1354 if (WINDOWP (f->tool_bar_window))
1355 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1358 run_window_configuration_change_hook (f);
1363 /* Set the foreground color for scroll bars on frame F to VALUE.
1364 VALUE should be a string, a color name. If it isn't a string or
1365 isn't a valid color name, do nothing. OLDVAL is the old value of
1366 the frame parameter. */
1368 static void
1369 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1371 unsigned long pixel;
1373 if (STRINGP (value))
1374 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1375 else
1376 pixel = -1;
1378 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1379 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1381 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1382 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1384 /* Remove all scroll bars because they have wrong colors. */
1385 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1386 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1387 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1388 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1390 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1391 redraw_frame (f);
1396 /* Set the background color for scroll bars on frame F to VALUE VALUE
1397 should be a string, a color name. If it isn't a string or isn't a
1398 valid color name, do nothing. OLDVAL is the old value of the frame
1399 parameter. */
1401 static void
1402 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1404 unsigned long pixel;
1406 if (STRINGP (value))
1407 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1408 else
1409 pixel = -1;
1411 if (f->output_data.x->scroll_bar_background_pixel != -1)
1412 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1414 #ifdef USE_TOOLKIT_SCROLL_BARS
1415 /* Scrollbar shadow colors. */
1416 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1418 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1419 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1421 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1423 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1424 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1426 #endif /* USE_TOOLKIT_SCROLL_BARS */
1428 f->output_data.x->scroll_bar_background_pixel = pixel;
1429 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1431 /* Remove all scroll bars because they have wrong colors. */
1432 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1433 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1434 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1435 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1437 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1438 redraw_frame (f);
1443 /* Encode Lisp string STRING as a text in a format appropriate for
1444 XICCC (X Inter Client Communication Conventions).
1446 This can call Lisp code, so callers must GCPRO.
1448 If STRING contains only ASCII characters, do no conversion and
1449 return the string data of STRING. Otherwise, encode the text by
1450 CODING_SYSTEM, and return a newly allocated memory area which
1451 should be freed by `xfree' by a caller.
1453 SELECTIONP non-zero means the string is being encoded for an X
1454 selection, so it is safe to run pre-write conversions (which
1455 may run Lisp code).
1457 Store the byte length of resulting text in *TEXT_BYTES.
1459 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1460 which means that the `encoding' of the result can be `STRING'.
1461 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1462 the result should be `COMPOUND_TEXT'. */
1464 static unsigned char *
1465 x_encode_text (Lisp_Object string, Lisp_Object coding_system, int selectionp,
1466 ptrdiff_t *text_bytes, int *stringp, int *freep)
1468 int result = string_xstring_p (string);
1469 struct coding_system coding;
1471 if (result == 0)
1473 /* No multibyte character in OBJ. We need not encode it. */
1474 *text_bytes = SBYTES (string);
1475 *stringp = 1;
1476 *freep = 0;
1477 return SDATA (string);
1480 setup_coding_system (coding_system, &coding);
1481 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1482 /* We suppress producing escape sequences for composition. */
1483 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1484 coding.destination = xnmalloc (SCHARS (string), 2);
1485 coding.dst_bytes = SCHARS (string) * 2;
1486 encode_coding_object (&coding, string, 0, 0,
1487 SCHARS (string), SBYTES (string), Qnil);
1488 *text_bytes = coding.produced;
1489 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1490 *freep = 1;
1491 return coding.destination;
1495 /* Set the WM name to NAME for frame F. Also set the icon name.
1496 If the frame already has an icon name, use that, otherwise set the
1497 icon name to NAME. */
1499 static void
1500 x_set_name_internal (FRAME_PTR f, Lisp_Object name)
1502 if (FRAME_X_WINDOW (f))
1504 BLOCK_INPUT;
1506 XTextProperty text, icon;
1507 ptrdiff_t bytes;
1508 int stringp;
1509 int do_free_icon_value = 0, do_free_text_value = 0;
1510 Lisp_Object coding_system;
1511 Lisp_Object encoded_name;
1512 Lisp_Object encoded_icon_name;
1513 struct gcpro gcpro1;
1515 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1516 we use it before x_encode_text that may return string data. */
1517 GCPRO1 (name);
1518 encoded_name = ENCODE_UTF_8 (name);
1519 UNGCPRO;
1521 coding_system = Qcompound_text;
1522 /* Note: Encoding strategy
1524 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1525 text.encoding. But, there are non-internationalized window
1526 managers which don't support that encoding. So, if NAME
1527 contains only ASCII and 8859-1 characters, encode it by
1528 iso-latin-1, and use "STRING" in text.encoding hoping that
1529 such window managers at least analyze this format correctly,
1530 i.e. treat 8-bit bytes as 8859-1 characters.
1532 We may also be able to use "UTF8_STRING" in text.encoding
1533 in the future which can encode all Unicode characters.
1534 But, for the moment, there's no way to know that the
1535 current window manager supports it or not.
1537 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1538 properties. Per the EWMH specification, those two properties
1539 are always UTF8_STRING. This matches what gtk_window_set_title()
1540 does in the USE_GTK case. */
1541 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1542 &do_free_text_value);
1543 text.encoding = (stringp ? XA_STRING
1544 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1545 text.format = 8;
1546 text.nitems = bytes;
1547 if (text.nitems != bytes)
1548 error ("Window name too large");
1550 if (!STRINGP (f->icon_name))
1552 icon = text;
1553 encoded_icon_name = encoded_name;
1555 else
1557 /* See the above comment "Note: Encoding strategy". */
1558 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1559 &bytes, &stringp, &do_free_icon_value);
1560 icon.encoding = (stringp ? XA_STRING
1561 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1562 icon.format = 8;
1563 icon.nitems = bytes;
1564 if (icon.nitems != bytes)
1565 error ("Icon name too large");
1567 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1570 #ifdef USE_GTK
1571 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1572 SSDATA (encoded_name));
1573 #else /* not USE_GTK */
1574 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1575 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1576 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_name,
1577 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1578 8, PropModeReplace,
1579 SDATA (encoded_name),
1580 SBYTES (encoded_name));
1581 #endif /* not USE_GTK */
1583 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1584 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1585 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1586 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1587 8, PropModeReplace,
1588 SDATA (encoded_icon_name),
1589 SBYTES (encoded_icon_name));
1591 if (do_free_icon_value)
1592 xfree (icon.value);
1593 if (do_free_text_value)
1594 xfree (text.value);
1596 UNBLOCK_INPUT;
1600 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1601 x_id_name.
1603 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1604 name; if NAME is a string, set F's name to NAME and set
1605 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1607 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1608 suggesting a new name, which lisp code should override; if
1609 F->explicit_name is set, ignore the new name; otherwise, set it. */
1611 static void
1612 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1614 /* Make sure that requests from lisp code override requests from
1615 Emacs redisplay code. */
1616 if (explicit)
1618 /* If we're switching from explicit to implicit, we had better
1619 update the mode lines and thereby update the title. */
1620 if (f->explicit_name && NILP (name))
1621 update_mode_lines = 1;
1623 f->explicit_name = ! NILP (name);
1625 else if (f->explicit_name)
1626 return;
1628 /* If NAME is nil, set the name to the x_id_name. */
1629 if (NILP (name))
1631 /* Check for no change needed in this very common case
1632 before we do any consing. */
1633 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1634 SSDATA (f->name)))
1635 return;
1636 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1638 else
1639 CHECK_STRING (name);
1641 /* Don't change the name if it's already NAME. */
1642 if (! NILP (Fstring_equal (name, f->name)))
1643 return;
1645 FSET (f, name, name);
1647 /* For setting the frame title, the title parameter should override
1648 the name parameter. */
1649 if (! NILP (f->title))
1650 name = f->title;
1652 x_set_name_internal (f, name);
1655 /* This function should be called when the user's lisp code has
1656 specified a name for the frame; the name will override any set by the
1657 redisplay code. */
1658 static void
1659 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1661 x_set_name (f, arg, 1);
1664 /* This function should be called by Emacs redisplay code to set the
1665 name; names set this way will never override names set by the user's
1666 lisp code. */
1667 void
1668 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1670 x_set_name (f, arg, 0);
1673 /* Change the title of frame F to NAME.
1674 If NAME is nil, use the frame name as the title. */
1676 static void
1677 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1679 /* Don't change the title if it's already NAME. */
1680 if (EQ (name, f->title))
1681 return;
1683 update_mode_lines = 1;
1685 FSET (f, title, name);
1687 if (NILP (name))
1688 name = f->name;
1689 else
1690 CHECK_STRING (name);
1692 x_set_name_internal (f, name);
1695 void
1696 x_set_scroll_bar_default_width (struct frame *f)
1698 int wid = FRAME_COLUMN_WIDTH (f);
1699 #ifdef USE_TOOLKIT_SCROLL_BARS
1700 #ifdef USE_GTK
1701 int minw = xg_get_default_scrollbar_width ();
1702 #else
1703 int minw = 16;
1704 #endif
1705 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1706 int width = minw + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1707 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1708 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1709 #else
1710 /* Make the actual width at least 14 pixels and a multiple of a
1711 character width. */
1712 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1714 /* Use all of that space (aside from required margins) for the
1715 scroll bar. */
1716 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1717 #endif
1721 /* Record in frame F the specified or default value according to ALIST
1722 of the parameter named PROP (a Lisp symbol). If no value is
1723 specified for PROP, look for an X default for XPROP on the frame
1724 named NAME. If that is not found either, use the value DEFLT. */
1726 static Lisp_Object
1727 x_default_scroll_bar_color_parameter (struct frame *f,
1728 Lisp_Object alist, Lisp_Object prop,
1729 const char *xprop, const char *xclass,
1730 int foreground_p)
1732 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1733 Lisp_Object tem;
1735 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1736 if (EQ (tem, Qunbound))
1738 #ifdef USE_TOOLKIT_SCROLL_BARS
1740 /* See if an X resource for the scroll bar color has been
1741 specified. */
1742 tem = display_x_get_resource (dpyinfo,
1743 build_string (foreground_p
1744 ? "foreground"
1745 : "background"),
1746 empty_unibyte_string,
1747 build_string ("verticalScrollBar"),
1748 empty_unibyte_string);
1749 if (!STRINGP (tem))
1751 /* If nothing has been specified, scroll bars will use a
1752 toolkit-dependent default. Because these defaults are
1753 difficult to get at without actually creating a scroll
1754 bar, use nil to indicate that no color has been
1755 specified. */
1756 tem = Qnil;
1759 #else /* not USE_TOOLKIT_SCROLL_BARS */
1761 tem = Qnil;
1763 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1766 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
1767 return tem;
1773 #ifdef USE_X_TOOLKIT
1775 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1776 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1777 already be present because of the toolkit (Motif adds some of them,
1778 for example, but Xt doesn't). */
1780 static void
1781 hack_wm_protocols (FRAME_PTR f, Widget widget)
1783 Display *dpy = XtDisplay (widget);
1784 Window w = XtWindow (widget);
1785 int need_delete = 1;
1786 int need_focus = 1;
1787 int need_save = 1;
1789 BLOCK_INPUT;
1791 Atom type;
1792 unsigned char *catoms;
1793 int format = 0;
1794 unsigned long nitems = 0;
1795 unsigned long bytes_after;
1797 if ((XGetWindowProperty (dpy, w,
1798 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1799 (long)0, (long)100, False, XA_ATOM,
1800 &type, &format, &nitems, &bytes_after,
1801 &catoms)
1802 == Success)
1803 && format == 32 && type == XA_ATOM)
1805 Atom *atoms = (Atom *) catoms;
1806 while (nitems > 0)
1808 nitems--;
1809 if (atoms[nitems]
1810 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1811 need_delete = 0;
1812 else if (atoms[nitems]
1813 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1814 need_focus = 0;
1815 else if (atoms[nitems]
1816 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1817 need_save = 0;
1820 if (catoms)
1821 XFree (catoms);
1824 Atom props [10];
1825 int count = 0;
1826 if (need_delete)
1827 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1828 if (need_focus)
1829 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1830 if (need_save)
1831 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1832 if (count)
1833 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1834 XA_ATOM, 32, PropModeAppend,
1835 (unsigned char *) props, count);
1837 UNBLOCK_INPUT;
1839 #endif
1843 /* Support routines for XIC (X Input Context). */
1845 #ifdef HAVE_X_I18N
1847 static XFontSet xic_create_xfontset (struct frame *);
1848 static XIMStyle best_xim_style (XIMStyles *, XIMStyles *);
1851 /* Supported XIM styles, ordered by preference. */
1853 static XIMStyle supported_xim_styles[] =
1855 XIMPreeditPosition | XIMStatusArea,
1856 XIMPreeditPosition | XIMStatusNothing,
1857 XIMPreeditPosition | XIMStatusNone,
1858 XIMPreeditNothing | XIMStatusArea,
1859 XIMPreeditNothing | XIMStatusNothing,
1860 XIMPreeditNothing | XIMStatusNone,
1861 XIMPreeditNone | XIMStatusArea,
1862 XIMPreeditNone | XIMStatusNothing,
1863 XIMPreeditNone | XIMStatusNone,
1868 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1869 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1871 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1873 /* Create an Xt fontset spec from the name of a base font.
1874 If `motif' is True use the Motif syntax. */
1875 char *
1876 xic_create_fontsetname (const char *base_fontname, int motif)
1878 const char *sep = motif ? ";" : ",";
1879 char *fontsetname;
1881 /* Make a fontset name from the base font name. */
1882 if (xic_default_fontset == base_fontname)
1884 /* There is no base font name, use the default. */
1885 fontsetname = xmalloc (strlen (base_fontname) + 2);
1886 strcpy (fontsetname, base_fontname);
1888 else
1890 /* Make a fontset name from the base font name.
1891 The font set will be made of the following elements:
1892 - the base font.
1893 - the base font where the charset spec is replaced by -*-*.
1894 - the same but with the family also replaced with -*-*-. */
1895 const char *p = base_fontname;
1896 ptrdiff_t i;
1898 for (i = 0; *p; p++)
1899 if (*p == '-') i++;
1900 if (i != 14)
1902 /* As the font name doesn't conform to XLFD, we can't
1903 modify it to generalize it to allcs and allfamilies.
1904 Use the specified font plus the default. */
1905 fontsetname = xmalloc (strlen (base_fontname)
1906 + strlen (xic_default_fontset) + 3);
1907 strcpy (fontsetname, base_fontname);
1908 strcat (fontsetname, sep);
1909 strcat (fontsetname, xic_default_fontset);
1911 else
1913 ptrdiff_t len;
1914 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1915 char *font_allcs = NULL;
1916 char *font_allfamilies = NULL;
1917 char *font_all = NULL;
1918 const char *allcs = "*-*-*-*-*-*-*";
1919 const char *allfamilies = "-*-*-";
1920 const char *all = "*-*-*-*-";
1921 char *base;
1923 for (i = 0, p = base_fontname; i < 8; p++)
1925 if (*p == '-')
1927 i++;
1928 if (i == 3)
1929 p1 = p + 1;
1930 else if (i == 7)
1931 p2 = p + 1;
1932 else if (i == 6)
1933 p3 = p + 1;
1936 /* If base_fontname specifies ADSTYLE, make it a
1937 wildcard. */
1938 if (*p3 != '*')
1940 ptrdiff_t diff = (p2 - p3) - 2;
1942 base = alloca (strlen (base_fontname) + 1);
1943 memcpy (base, base_fontname, p3 - base_fontname);
1944 base[p3 - base_fontname] = '*';
1945 base[(p3 - base_fontname) + 1] = '-';
1946 strcpy (base + (p3 - base_fontname) + 2, p2);
1947 p = base + (p - base_fontname) - diff;
1948 p1 = base + (p1 - base_fontname);
1949 p2 = base + (p2 - base_fontname) - diff;
1950 base_fontname = base;
1953 /* Build the font spec that matches all charsets. */
1954 len = p - base_fontname + strlen (allcs) + 1;
1955 font_allcs = alloca (len);
1956 memcpy (font_allcs, base_fontname, p - base_fontname);
1957 strcat (font_allcs, allcs);
1959 /* Build the font spec that matches all families and
1960 add-styles. */
1961 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1962 font_allfamilies = alloca (len);
1963 strcpy (font_allfamilies, allfamilies);
1964 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1965 strcat (font_allfamilies, allcs);
1967 /* Build the font spec that matches all. */
1968 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1969 font_all = alloca (len);
1970 strcpy (font_all, allfamilies);
1971 strcat (font_all, all);
1972 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1973 strcat (font_all, allcs);
1975 /* Build the actual font set name. */
1976 len = strlen (base_fontname) + strlen (font_allcs)
1977 + strlen (font_allfamilies) + strlen (font_all) + 5;
1978 fontsetname = xmalloc (len);
1979 strcpy (fontsetname, base_fontname);
1980 strcat (fontsetname, sep);
1981 strcat (fontsetname, font_allcs);
1982 strcat (fontsetname, sep);
1983 strcat (fontsetname, font_allfamilies);
1984 strcat (fontsetname, sep);
1985 strcat (fontsetname, font_all);
1988 if (motif)
1989 strcat (fontsetname, ":");
1990 return fontsetname;
1992 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1994 #ifdef DEBUG_XIC_FONTSET
1995 static void
1996 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1997 int missing_count)
1999 if (xfs)
2000 fprintf (stderr, "XIC Fontset created: %s\n", name);
2001 else
2003 fprintf (stderr, "XIC Fontset failed: %s\n", name);
2004 while (missing_count-- > 0)
2006 fprintf (stderr, " missing: %s\n", *missing_list);
2007 missing_list++;
2012 #endif
2014 static XFontSet
2015 xic_create_xfontset (struct frame *f)
2017 XFontSet xfs = NULL;
2018 struct font *font = FRAME_FONT (f);
2019 int pixel_size = font->pixel_size;
2020 Lisp_Object rest, frame;
2022 /* See if there is another frame already using same fontset. */
2023 FOR_EACH_FRAME (rest, frame)
2025 struct frame *cf = XFRAME (frame);
2027 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2028 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2029 && FRAME_FONT (f)
2030 && FRAME_FONT (f)->pixel_size == pixel_size)
2032 xfs = FRAME_XIC_FONTSET (cf);
2033 break;
2037 if (! xfs)
2039 char buf[256];
2040 char **missing_list;
2041 int missing_count;
2042 char *def_string;
2043 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
2045 sprintf (buf, xlfd_format, pixel_size);
2046 missing_list = NULL;
2047 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2048 &missing_list, &missing_count, &def_string);
2049 #ifdef DEBUG_XIC_FONTSET
2050 print_fontset_result (xfs, buf, missing_list, missing_count);
2051 #endif
2052 if (missing_list)
2053 XFreeStringList (missing_list);
2054 if (! xfs)
2056 /* List of pixel sizes most likely available. Find one that
2057 is closest to pixel_size. */
2058 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
2059 int *smaller, *larger;
2061 for (smaller = sizes; smaller[1]; smaller++)
2062 if (smaller[1] >= pixel_size)
2063 break;
2064 larger = smaller + 1;
2065 if (*larger == pixel_size)
2066 larger++;
2067 while (*smaller || *larger)
2069 int this_size;
2071 if (! *larger)
2072 this_size = *smaller--;
2073 else if (! *smaller)
2074 this_size = *larger++;
2075 else if (pixel_size - *smaller < *larger - pixel_size)
2076 this_size = *smaller--;
2077 else
2078 this_size = *larger++;
2079 sprintf (buf, xlfd_format, this_size);
2080 missing_list = NULL;
2081 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2082 &missing_list, &missing_count, &def_string);
2083 #ifdef DEBUG_XIC_FONTSET
2084 print_fontset_result (xfs, buf, missing_list, missing_count);
2085 #endif
2086 if (missing_list)
2087 XFreeStringList (missing_list);
2088 if (xfs)
2089 break;
2092 if (! xfs)
2094 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
2096 missing_list = NULL;
2097 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
2098 &missing_list, &missing_count, &def_string);
2099 #ifdef DEBUG_XIC_FONTSET
2100 print_fontset_result (xfs, last_resort, missing_list, missing_count);
2101 #endif
2102 if (missing_list)
2103 XFreeStringList (missing_list);
2108 return xfs;
2111 /* Free the X fontset of frame F if it is the last frame using it. */
2113 void
2114 xic_free_xfontset (struct frame *f)
2116 Lisp_Object rest, frame;
2117 int shared_p = 0;
2119 if (!FRAME_XIC_FONTSET (f))
2120 return;
2122 /* See if there is another frame sharing the same fontset. */
2123 FOR_EACH_FRAME (rest, frame)
2125 struct frame *cf = XFRAME (frame);
2126 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2127 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2128 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2130 shared_p = 1;
2131 break;
2135 if (!shared_p)
2136 /* The fontset is not used anymore. It is safe to free it. */
2137 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2139 if (FRAME_XIC_BASE_FONTNAME (f))
2140 xfree (FRAME_XIC_BASE_FONTNAME (f));
2141 FRAME_XIC_BASE_FONTNAME (f) = NULL;
2142 FRAME_XIC_FONTSET (f) = NULL;
2146 /* Value is the best input style, given user preferences USER (already
2147 checked to be supported by Emacs), and styles supported by the
2148 input method XIM. */
2150 static XIMStyle
2151 best_xim_style (XIMStyles *user, XIMStyles *xim)
2153 int i, j;
2155 for (i = 0; i < user->count_styles; ++i)
2156 for (j = 0; j < xim->count_styles; ++j)
2157 if (user->supported_styles[i] == xim->supported_styles[j])
2158 return user->supported_styles[i];
2160 /* Return the default style. */
2161 return XIMPreeditNothing | XIMStatusNothing;
2164 /* Create XIC for frame F. */
2166 static XIMStyle xic_style;
2168 void
2169 create_frame_xic (struct frame *f)
2171 XIM xim;
2172 XIC xic = NULL;
2173 XFontSet xfs = NULL;
2175 if (FRAME_XIC (f))
2176 return;
2178 /* Create X fontset. */
2179 xfs = xic_create_xfontset (f);
2180 xim = FRAME_X_XIM (f);
2181 if (xim)
2183 XRectangle s_area;
2184 XPoint spot;
2185 XVaNestedList preedit_attr;
2186 XVaNestedList status_attr;
2188 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2189 spot.x = 0; spot.y = 1;
2191 /* Determine XIC style. */
2192 if (xic_style == 0)
2194 XIMStyles supported_list;
2195 supported_list.count_styles = (sizeof supported_xim_styles
2196 / sizeof supported_xim_styles[0]);
2197 supported_list.supported_styles = supported_xim_styles;
2198 xic_style = best_xim_style (&supported_list,
2199 FRAME_X_XIM_STYLES (f));
2202 preedit_attr = XVaCreateNestedList (0,
2203 XNFontSet, xfs,
2204 XNForeground,
2205 FRAME_FOREGROUND_PIXEL (f),
2206 XNBackground,
2207 FRAME_BACKGROUND_PIXEL (f),
2208 (xic_style & XIMPreeditPosition
2209 ? XNSpotLocation
2210 : NULL),
2211 &spot,
2212 NULL);
2213 status_attr = XVaCreateNestedList (0,
2214 XNArea,
2215 &s_area,
2216 XNFontSet,
2217 xfs,
2218 XNForeground,
2219 FRAME_FOREGROUND_PIXEL (f),
2220 XNBackground,
2221 FRAME_BACKGROUND_PIXEL (f),
2222 NULL);
2224 xic = XCreateIC (xim,
2225 XNInputStyle, xic_style,
2226 XNClientWindow, FRAME_X_WINDOW (f),
2227 XNFocusWindow, FRAME_X_WINDOW (f),
2228 XNStatusAttributes, status_attr,
2229 XNPreeditAttributes, preedit_attr,
2230 NULL);
2231 XFree (preedit_attr);
2232 XFree (status_attr);
2235 FRAME_XIC (f) = xic;
2236 FRAME_XIC_STYLE (f) = xic_style;
2237 FRAME_XIC_FONTSET (f) = xfs;
2241 /* Destroy XIC and free XIC fontset of frame F, if any. */
2243 void
2244 free_frame_xic (struct frame *f)
2246 if (FRAME_XIC (f) == NULL)
2247 return;
2249 XDestroyIC (FRAME_XIC (f));
2250 xic_free_xfontset (f);
2252 FRAME_XIC (f) = NULL;
2256 /* Place preedit area for XIC of window W's frame to specified
2257 pixel position X/Y. X and Y are relative to window W. */
2259 void
2260 xic_set_preeditarea (struct window *w, int x, int y)
2262 struct frame *f = XFRAME (w->frame);
2263 XVaNestedList attr;
2264 XPoint spot;
2266 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2267 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2268 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2269 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2270 XFree (attr);
2274 /* Place status area for XIC in bottom right corner of frame F.. */
2276 void
2277 xic_set_statusarea (struct frame *f)
2279 XIC xic = FRAME_XIC (f);
2280 XVaNestedList attr;
2281 XRectangle area;
2282 XRectangle *needed;
2284 /* Negotiate geometry of status area. If input method has existing
2285 status area, use its current size. */
2286 area.x = area.y = area.width = area.height = 0;
2287 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2288 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2289 XFree (attr);
2291 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2292 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2293 XFree (attr);
2295 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2297 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2298 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2299 XFree (attr);
2302 area.width = needed->width;
2303 area.height = needed->height;
2304 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2305 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2306 - FRAME_MENUBAR_HEIGHT (f)
2307 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2308 - FRAME_INTERNAL_BORDER_WIDTH (f));
2309 XFree (needed);
2311 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2312 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2313 XFree (attr);
2317 /* Set X fontset for XIC of frame F, using base font name
2318 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2320 void
2321 xic_set_xfontset (struct frame *f, const char *base_fontname)
2323 XVaNestedList attr;
2324 XFontSet xfs;
2326 xic_free_xfontset (f);
2328 xfs = xic_create_xfontset (f);
2330 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2331 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2332 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2333 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2334 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2335 XFree (attr);
2337 FRAME_XIC_FONTSET (f) = xfs;
2340 #endif /* HAVE_X_I18N */
2344 #ifdef USE_X_TOOLKIT
2346 /* Create and set up the X widget for frame F. */
2348 static void
2349 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2351 XClassHint class_hints;
2352 XSetWindowAttributes attributes;
2353 unsigned long attribute_mask;
2354 Widget shell_widget;
2355 Widget pane_widget;
2356 Widget frame_widget;
2357 Arg al [25];
2358 int ac;
2360 BLOCK_INPUT;
2362 /* Use the resource name as the top-level widget name
2363 for looking up resources. Make a non-Lisp copy
2364 for the window manager, so GC relocation won't bother it.
2366 Elsewhere we specify the window name for the window manager. */
2369 char *str = SSDATA (Vx_resource_name);
2370 f->namebuf = xmalloc (strlen (str) + 1);
2371 strcpy (f->namebuf, str);
2374 ac = 0;
2375 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2376 XtSetArg (al[ac], XtNinput, 1); ac++;
2377 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2378 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
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 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2383 applicationShellWidgetClass,
2384 FRAME_X_DISPLAY (f), al, ac);
2386 f->output_data.x->widget = shell_widget;
2387 /* maybe_set_screen_title_format (shell_widget); */
2389 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2390 (widget_value *) NULL,
2391 shell_widget, False,
2392 (lw_callback) NULL,
2393 (lw_callback) NULL,
2394 (lw_callback) NULL,
2395 (lw_callback) NULL);
2397 ac = 0;
2398 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2399 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2400 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2401 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2402 XtSetValues (pane_widget, al, ac);
2403 f->output_data.x->column_widget = pane_widget;
2405 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2406 the emacs screen when changing menubar. This reduces flickering. */
2408 ac = 0;
2409 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2410 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2411 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2412 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2413 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2414 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2415 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2416 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2417 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2418 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2419 al, ac);
2421 f->output_data.x->edit_widget = frame_widget;
2423 XtManageChild (frame_widget);
2425 /* Do some needed geometry management. */
2427 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
2428 Arg gal[10];
2429 int gac = 0;
2430 int extra_borders = 0;
2431 int menubar_size
2432 = (f->output_data.x->menubar_widget
2433 ? (f->output_data.x->menubar_widget->core.height
2434 + f->output_data.x->menubar_widget->core.border_width)
2435 : 0);
2437 #if 0 /* Experimentally, we now get the right results
2438 for -geometry -0-0 without this. 24 Aug 96, rms. */
2439 if (FRAME_EXTERNAL_MENU_BAR (f))
2441 Dimension ibw = 0;
2442 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2443 menubar_size += ibw;
2445 #endif
2447 f->output_data.x->menubar_height = menubar_size;
2449 #ifndef USE_LUCID
2450 /* Motif seems to need this amount added to the sizes
2451 specified for the shell widget. The Athena/Lucid widgets don't.
2452 Both conclusions reached experimentally. -- rms. */
2453 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2454 &extra_borders, NULL);
2455 extra_borders *= 2;
2456 #endif
2458 /* Convert our geometry parameters into a geometry string
2459 and specify it.
2460 Note that we do not specify here whether the position
2461 is a user-specified or program-specified one.
2462 We pass that information later, in x_wm_set_size_hints. */
2464 int left = f->left_pos;
2465 int xneg = window_prompting & XNegative;
2466 int top = f->top_pos;
2467 int yneg = window_prompting & YNegative;
2468 if (xneg)
2469 left = -left;
2470 if (yneg)
2471 top = -top;
2473 if (window_prompting & USPosition)
2474 sprintf (shell_position, "=%dx%d%c%d%c%d",
2475 FRAME_PIXEL_WIDTH (f) + extra_borders,
2476 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2477 (xneg ? '-' : '+'), left,
2478 (yneg ? '-' : '+'), top);
2479 else
2481 sprintf (shell_position, "=%dx%d",
2482 FRAME_PIXEL_WIDTH (f) + extra_borders,
2483 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2485 /* Setting x and y when the position is not specified in
2486 the geometry string will set program position in the WM hints.
2487 If Emacs had just one program position, we could set it in
2488 fallback resources, but since each make-frame call can specify
2489 different program positions, this is easier. */
2490 XtSetArg (gal[gac], XtNx, left); gac++;
2491 XtSetArg (gal[gac], XtNy, top); gac++;
2495 /* We don't free this because we don't know whether
2496 it is safe to free it while the frame exists.
2497 It isn't worth the trouble of arranging to free it
2498 when the frame is deleted. */
2499 tem = xstrdup (shell_position);
2500 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2501 XtSetValues (shell_widget, gal, gac);
2504 XtManageChild (pane_widget);
2505 XtRealizeWidget (shell_widget);
2507 if (FRAME_X_EMBEDDED_P (f))
2508 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2509 f->output_data.x->parent_desc, 0, 0);
2511 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2513 validate_x_resource_name ();
2515 class_hints.res_name = SSDATA (Vx_resource_name);
2516 class_hints.res_class = SSDATA (Vx_resource_class);
2517 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2519 #ifdef HAVE_X_I18N
2520 FRAME_XIC (f) = NULL;
2521 if (use_xim)
2522 create_frame_xic (f);
2523 #endif
2525 f->output_data.x->wm_hints.input = True;
2526 f->output_data.x->wm_hints.flags |= InputHint;
2527 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2528 &f->output_data.x->wm_hints);
2530 hack_wm_protocols (f, shell_widget);
2532 #ifdef HACK_EDITRES
2533 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2534 #endif
2536 /* Do a stupid property change to force the server to generate a
2537 PropertyNotify event so that the event_stream server timestamp will
2538 be initialized to something relevant to the time we created the window.
2540 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2541 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2542 XA_ATOM, 32, PropModeAppend,
2543 (unsigned char*) NULL, 0);
2545 /* Make all the standard events reach the Emacs frame. */
2546 attributes.event_mask = STANDARD_EVENT_SET;
2548 #ifdef HAVE_X_I18N
2549 if (FRAME_XIC (f))
2551 /* XIM server might require some X events. */
2552 unsigned long fevent = NoEventMask;
2553 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2554 attributes.event_mask |= fevent;
2556 #endif /* HAVE_X_I18N */
2558 attribute_mask = CWEventMask;
2559 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2560 attribute_mask, &attributes);
2562 XtMapWidget (frame_widget);
2564 /* x_set_name normally ignores requests to set the name if the
2565 requested name is the same as the current name. This is the one
2566 place where that assumption isn't correct; f->name is set, but
2567 the X server hasn't been told. */
2569 Lisp_Object name;
2570 int explicit = f->explicit_name;
2572 f->explicit_name = 0;
2573 name = f->name;
2574 FSET (f, name, Qnil);
2575 x_set_name (f, name, explicit);
2578 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2579 f->output_data.x->current_cursor
2580 = f->output_data.x->text_cursor);
2582 UNBLOCK_INPUT;
2584 /* This is a no-op, except under Motif. Make sure main areas are
2585 set to something reasonable, in case we get an error later. */
2586 lw_set_main_areas (pane_widget, 0, frame_widget);
2589 #else /* not USE_X_TOOLKIT */
2590 #ifdef USE_GTK
2591 static void
2592 x_window (FRAME_PTR f)
2594 if (! xg_create_frame_widgets (f))
2595 error ("Unable to create window");
2597 #ifdef HAVE_X_I18N
2598 FRAME_XIC (f) = NULL;
2599 if (use_xim)
2601 BLOCK_INPUT;
2602 create_frame_xic (f);
2603 if (FRAME_XIC (f))
2605 /* XIM server might require some X events. */
2606 unsigned long fevent = NoEventMask;
2607 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2609 if (fevent != NoEventMask)
2611 XSetWindowAttributes attributes;
2612 XWindowAttributes wattr;
2613 unsigned long attribute_mask;
2615 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2616 &wattr);
2617 attributes.event_mask = wattr.your_event_mask | fevent;
2618 attribute_mask = CWEventMask;
2619 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2620 attribute_mask, &attributes);
2623 UNBLOCK_INPUT;
2625 #endif
2628 #else /*! USE_GTK */
2629 /* Create and set up the X window for frame F. */
2631 static void
2632 x_window (struct frame *f)
2634 XClassHint class_hints;
2635 XSetWindowAttributes attributes;
2636 unsigned long attribute_mask;
2638 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2639 attributes.border_pixel = f->output_data.x->border_pixel;
2640 attributes.bit_gravity = StaticGravity;
2641 attributes.backing_store = NotUseful;
2642 attributes.save_under = True;
2643 attributes.event_mask = STANDARD_EVENT_SET;
2644 attributes.colormap = FRAME_X_COLORMAP (f);
2645 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2646 | CWColormap);
2648 BLOCK_INPUT;
2649 FRAME_X_WINDOW (f)
2650 = XCreateWindow (FRAME_X_DISPLAY (f),
2651 f->output_data.x->parent_desc,
2652 f->left_pos,
2653 f->top_pos,
2654 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2655 f->border_width,
2656 CopyFromParent, /* depth */
2657 InputOutput, /* class */
2658 FRAME_X_VISUAL (f),
2659 attribute_mask, &attributes);
2661 #ifdef HAVE_X_I18N
2662 if (use_xim)
2664 create_frame_xic (f);
2665 if (FRAME_XIC (f))
2667 /* XIM server might require some X events. */
2668 unsigned long fevent = NoEventMask;
2669 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2670 attributes.event_mask |= fevent;
2671 attribute_mask = CWEventMask;
2672 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2673 attribute_mask, &attributes);
2676 #endif /* HAVE_X_I18N */
2678 validate_x_resource_name ();
2680 class_hints.res_name = SSDATA (Vx_resource_name);
2681 class_hints.res_class = SSDATA (Vx_resource_class);
2682 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2684 /* The menubar is part of the ordinary display;
2685 it does not count in addition to the height of the window. */
2686 f->output_data.x->menubar_height = 0;
2688 /* This indicates that we use the "Passive Input" input model.
2689 Unless we do this, we don't get the Focus{In,Out} events that we
2690 need to draw the cursor correctly. Accursed bureaucrats.
2691 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2693 f->output_data.x->wm_hints.input = True;
2694 f->output_data.x->wm_hints.flags |= InputHint;
2695 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2696 &f->output_data.x->wm_hints);
2697 f->output_data.x->wm_hints.icon_pixmap = None;
2699 /* Request "save yourself" and "delete window" commands from wm. */
2701 Atom protocols[2];
2702 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2703 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2704 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2707 /* x_set_name normally ignores requests to set the name if the
2708 requested name is the same as the current name. This is the one
2709 place where that assumption isn't correct; f->name is set, but
2710 the X server hasn't been told. */
2712 Lisp_Object name;
2713 int explicit = f->explicit_name;
2715 f->explicit_name = 0;
2716 name = f->name;
2717 FSET (f, name, Qnil);
2718 x_set_name (f, name, explicit);
2721 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2722 f->output_data.x->current_cursor
2723 = f->output_data.x->text_cursor);
2725 UNBLOCK_INPUT;
2727 if (FRAME_X_WINDOW (f) == 0)
2728 error ("Unable to create window");
2731 #endif /* not USE_GTK */
2732 #endif /* not USE_X_TOOLKIT */
2734 /* Verify that the icon position args for this window are valid. */
2736 static void
2737 x_icon_verify (struct frame *f, Lisp_Object parms)
2739 Lisp_Object icon_x, icon_y;
2741 /* Set the position of the icon. Note that twm groups all
2742 icons in an icon window. */
2743 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2744 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2745 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2747 CHECK_NUMBER (icon_x);
2748 CHECK_NUMBER (icon_y);
2750 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2751 error ("Both left and top icon corners of icon must be specified");
2754 /* Handle the icon stuff for this window. Perhaps later we might
2755 want an x_set_icon_position which can be called interactively as
2756 well. */
2758 static void
2759 x_icon (struct frame *f, Lisp_Object parms)
2761 Lisp_Object icon_x, icon_y;
2762 #if 0
2763 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2764 #endif
2766 /* Set the position of the icon. Note that twm groups all
2767 icons in an icon window. */
2768 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2769 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2770 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2772 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2773 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2775 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2776 error ("Both left and top icon corners of icon must be specified");
2778 BLOCK_INPUT;
2780 if (! EQ (icon_x, Qunbound))
2781 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2783 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2784 but x_create_frame still needs it. */
2785 /* Start up iconic or window? */
2786 x_wm_set_window_state
2787 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2788 Qicon)
2789 ? IconicState
2790 : NormalState));
2791 #endif
2793 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2794 ? f->icon_name
2795 : f->name)));
2797 UNBLOCK_INPUT;
2800 /* Make the GCs needed for this window, setting the
2801 background, border and mouse colors; also create the
2802 mouse cursor and the gray border tile. */
2804 static void
2805 x_make_gc (struct frame *f)
2807 XGCValues gc_values;
2809 BLOCK_INPUT;
2811 /* Create the GCs of this frame.
2812 Note that many default values are used. */
2814 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2815 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2816 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2817 f->output_data.x->normal_gc
2818 = XCreateGC (FRAME_X_DISPLAY (f),
2819 FRAME_X_WINDOW (f),
2820 GCLineWidth | GCForeground | GCBackground,
2821 &gc_values);
2823 /* Reverse video style. */
2824 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2825 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2826 f->output_data.x->reverse_gc
2827 = XCreateGC (FRAME_X_DISPLAY (f),
2828 FRAME_X_WINDOW (f),
2829 GCForeground | GCBackground | GCLineWidth,
2830 &gc_values);
2832 /* Cursor has cursor-color background, background-color foreground. */
2833 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2834 gc_values.background = f->output_data.x->cursor_pixel;
2835 gc_values.fill_style = FillOpaqueStippled;
2836 f->output_data.x->cursor_gc
2837 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2838 (GCForeground | GCBackground
2839 | GCFillStyle | GCLineWidth),
2840 &gc_values);
2842 /* Reliefs. */
2843 f->output_data.x->white_relief.gc = 0;
2844 f->output_data.x->black_relief.gc = 0;
2846 /* Create the gray border tile used when the pointer is not in
2847 the frame. Since this depends on the frame's pixel values,
2848 this must be done on a per-frame basis. */
2849 f->output_data.x->border_tile
2850 = (XCreatePixmapFromBitmapData
2851 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2852 gray_bits, gray_width, gray_height,
2853 FRAME_FOREGROUND_PIXEL (f),
2854 FRAME_BACKGROUND_PIXEL (f),
2855 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2857 UNBLOCK_INPUT;
2861 /* Free what was allocated in x_make_gc. */
2863 void
2864 x_free_gcs (struct frame *f)
2866 Display *dpy = FRAME_X_DISPLAY (f);
2868 BLOCK_INPUT;
2870 if (f->output_data.x->normal_gc)
2872 XFreeGC (dpy, f->output_data.x->normal_gc);
2873 f->output_data.x->normal_gc = 0;
2876 if (f->output_data.x->reverse_gc)
2878 XFreeGC (dpy, f->output_data.x->reverse_gc);
2879 f->output_data.x->reverse_gc = 0;
2882 if (f->output_data.x->cursor_gc)
2884 XFreeGC (dpy, f->output_data.x->cursor_gc);
2885 f->output_data.x->cursor_gc = 0;
2888 if (f->output_data.x->border_tile)
2890 XFreePixmap (dpy, f->output_data.x->border_tile);
2891 f->output_data.x->border_tile = 0;
2894 UNBLOCK_INPUT;
2898 /* Handler for signals raised during x_create_frame and
2899 x_create_tip_frame. FRAME is the frame which is partially
2900 constructed. */
2902 static Lisp_Object
2903 unwind_create_frame (Lisp_Object frame)
2905 struct frame *f = XFRAME (frame);
2907 /* If frame is already dead, nothing to do. This can happen if the
2908 display is disconnected after the frame has become official, but
2909 before x_create_frame removes the unwind protect. */
2910 if (!FRAME_LIVE_P (f))
2911 return Qnil;
2913 /* If frame is ``official'', nothing to do. */
2914 if (NILP (Fmemq (frame, Vframe_list)))
2916 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2917 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2918 #endif
2920 x_free_frame_resources (f);
2921 free_glyphs (f);
2923 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2924 /* Check that reference counts are indeed correct. */
2925 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2926 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2927 #endif
2928 return Qt;
2931 return Qnil;
2934 static Lisp_Object
2935 unwind_create_frame_1 (Lisp_Object val)
2937 inhibit_lisp_code = val;
2938 return Qnil;
2941 static void
2942 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2944 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2945 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2946 RES_TYPE_STRING);
2947 Lisp_Object font = Qnil;
2948 if (EQ (font_param, Qunbound))
2949 font_param = Qnil;
2951 if (NILP (font_param))
2953 /* System font should take precedence over X resources. We suggest this
2954 regardless of font-use-system-font because .emacs may not have been
2955 read yet. */
2956 const char *system_font = xsettings_get_system_font ();
2957 if (system_font)
2958 font = font_open_by_name (f, build_unibyte_string (system_font));
2961 if (NILP (font))
2962 font = !NILP (font_param) ? font_param
2963 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2965 if (! FONTP (font) && ! STRINGP (font))
2967 const char *names[]
2969 #ifdef HAVE_XFT
2970 /* This will find the normal Xft font. */
2971 "monospace-10",
2972 #endif
2973 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2974 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2975 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2976 /* This was formerly the first thing tried, but it finds
2977 too many fonts and takes too long. */
2978 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2979 /* If those didn't work, look for something which will
2980 at least work. */
2981 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2982 "fixed",
2983 NULL };
2984 int i;
2986 for (i = 0; names[i]; i++)
2988 font = font_open_by_name (f, build_unibyte_string (names[i]));
2989 if (! NILP (font))
2990 break;
2992 if (NILP (font))
2993 error ("No suitable font was found");
2995 else if (!NILP (font_param))
2997 /* Remember the explicit font parameter, so we can re-apply it after
2998 we've applied the `default' face settings. */
2999 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
3002 /* This call will make X resources override any system font setting. */
3003 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
3007 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
3008 0, 1, 0,
3009 doc: /* Send the size hints for frame FRAME to the window manager.
3010 If FRAME is nil, use the selected frame. */)
3011 (Lisp_Object frame)
3013 struct frame *f;
3014 if (NILP (frame))
3015 frame = selected_frame;
3016 f = XFRAME (frame);
3017 BLOCK_INPUT;
3018 if (FRAME_X_P (f))
3019 x_wm_set_size_hint (f, 0, 0);
3020 UNBLOCK_INPUT;
3021 return Qnil;
3024 static void
3025 set_machine_and_pid_properties (struct frame *f)
3027 long pid = (long) getpid ();
3029 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
3030 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
3031 NULL, 0, NULL, NULL, NULL);
3032 XChangeProperty (FRAME_X_DISPLAY (f),
3033 FRAME_OUTER_WINDOW (f),
3034 XInternAtom (FRAME_X_DISPLAY (f),
3035 "_NET_WM_PID",
3036 False),
3037 XA_CARDINAL, 32, PropModeReplace,
3038 (unsigned char *) &pid, 1);
3041 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
3042 1, 1, 0,
3043 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
3044 Return an Emacs frame object.
3045 PARMS is an alist of frame parameters.
3046 If the parameters specify that the frame should not have a minibuffer,
3047 and do not specify a specific minibuffer window to use,
3048 then `default-minibuffer-frame' must be a frame whose minibuffer can
3049 be shared by the new frame.
3051 This function is an internal primitive--use `make-frame' instead. */)
3052 (Lisp_Object parms)
3054 struct frame *f;
3055 Lisp_Object frame, tem;
3056 Lisp_Object name;
3057 int minibuffer_only = 0;
3058 long window_prompting = 0;
3059 int width, height;
3060 ptrdiff_t count = SPECPDL_INDEX ();
3061 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3062 Lisp_Object display;
3063 struct x_display_info *dpyinfo = NULL;
3064 Lisp_Object parent;
3065 struct kboard *kb;
3067 parms = Fcopy_alist (parms);
3069 /* Use this general default value to start with
3070 until we know if this frame has a specified name. */
3071 Vx_resource_name = Vinvocation_name;
3073 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
3074 if (EQ (display, Qunbound))
3075 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3076 if (EQ (display, Qunbound))
3077 display = Qnil;
3078 dpyinfo = check_x_display_info (display);
3079 kb = dpyinfo->terminal->kboard;
3081 if (!dpyinfo->terminal->name)
3082 error ("Terminal is not live, can't create new frames on it");
3084 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3085 if (!STRINGP (name)
3086 && ! EQ (name, Qunbound)
3087 && ! NILP (name))
3088 error ("Invalid frame name--not a string or nil");
3090 if (STRINGP (name))
3091 Vx_resource_name = name;
3093 /* See if parent window is specified. */
3094 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3095 if (EQ (parent, Qunbound))
3096 parent = Qnil;
3097 if (! NILP (parent))
3098 CHECK_NUMBER (parent);
3100 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3101 /* No need to protect DISPLAY because that's not used after passing
3102 it to make_frame_without_minibuffer. */
3103 frame = Qnil;
3104 GCPRO4 (parms, parent, name, frame);
3105 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3106 RES_TYPE_SYMBOL);
3107 if (EQ (tem, Qnone) || NILP (tem))
3108 f = make_frame_without_minibuffer (Qnil, kb, display);
3109 else if (EQ (tem, Qonly))
3111 f = make_minibuffer_frame ();
3112 minibuffer_only = 1;
3114 else if (WINDOWP (tem))
3115 f = make_frame_without_minibuffer (tem, kb, display);
3116 else
3117 f = make_frame (1);
3119 XSETFRAME (frame, f);
3121 /* Note that X Windows does support scroll bars. */
3122 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
3124 f->terminal = dpyinfo->terminal;
3126 f->output_method = output_x_window;
3127 f->output_data.x = xzalloc (sizeof *f->output_data.x);
3128 f->output_data.x->icon_bitmap = -1;
3129 FRAME_FONTSET (f) = -1;
3130 f->output_data.x->scroll_bar_foreground_pixel = -1;
3131 f->output_data.x->scroll_bar_background_pixel = -1;
3132 #ifdef USE_TOOLKIT_SCROLL_BARS
3133 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3134 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3135 #endif /* USE_TOOLKIT_SCROLL_BARS */
3137 FSET (f, icon_name,
3138 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3139 RES_TYPE_STRING));
3140 if (! STRINGP (f->icon_name))
3141 FSET (f, icon_name, Qnil);
3143 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3145 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
3146 record_unwind_protect (unwind_create_frame, frame);
3148 /* These colors will be set anyway later, but it's important
3149 to get the color reference counts right, so initialize them! */
3151 Lisp_Object black;
3152 struct gcpro gcpro1;
3154 /* Function x_decode_color can signal an error. Make
3155 sure to initialize color slots so that we won't try
3156 to free colors we haven't allocated. */
3157 FRAME_FOREGROUND_PIXEL (f) = -1;
3158 FRAME_BACKGROUND_PIXEL (f) = -1;
3159 f->output_data.x->cursor_pixel = -1;
3160 f->output_data.x->cursor_foreground_pixel = -1;
3161 f->output_data.x->border_pixel = -1;
3162 f->output_data.x->mouse_pixel = -1;
3164 black = build_string ("black");
3165 GCPRO1 (black);
3166 FRAME_FOREGROUND_PIXEL (f)
3167 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3168 FRAME_BACKGROUND_PIXEL (f)
3169 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3170 f->output_data.x->cursor_pixel
3171 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3172 f->output_data.x->cursor_foreground_pixel
3173 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3174 f->output_data.x->border_pixel
3175 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3176 f->output_data.x->mouse_pixel
3177 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3178 UNGCPRO;
3181 /* Specify the parent under which to make this X window. */
3183 if (!NILP (parent))
3185 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3186 f->output_data.x->explicit_parent = 1;
3188 else
3190 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3191 f->output_data.x->explicit_parent = 0;
3194 /* Set the name; the functions to which we pass f expect the name to
3195 be set. */
3196 if (EQ (name, Qunbound) || NILP (name))
3198 FSET (f, name, build_string (dpyinfo->x_id_name));
3199 f->explicit_name = 0;
3201 else
3203 FSET (f, name, name);
3204 f->explicit_name = 1;
3205 /* use the frame's title when getting resources for this frame. */
3206 specbind (Qx_resource_name, name);
3209 f->resx = dpyinfo->resx;
3210 f->resy = dpyinfo->resy;
3212 #ifdef HAVE_FREETYPE
3213 #ifdef HAVE_XFT
3214 register_font_driver (&xftfont_driver, f);
3215 #else /* not HAVE_XFT */
3216 register_font_driver (&ftxfont_driver, f);
3217 #endif /* not HAVE_XFT */
3218 #endif /* HAVE_FREETYPE */
3219 register_font_driver (&xfont_driver, f);
3221 x_default_parameter (f, parms, Qfont_backend, Qnil,
3222 "fontBackend", "FontBackend", RES_TYPE_STRING);
3224 /* Extract the window parameters from the supplied values
3225 that are needed to determine window geometry. */
3226 x_default_font_parameter (f, parms);
3227 if (!FRAME_FONT (f))
3229 delete_frame (frame, Qnoelisp);
3230 error ("Invalid frame font");
3233 /* Frame contents get displaced if an embedded X window has a border. */
3234 if (! FRAME_X_EMBEDDED_P (f))
3235 x_default_parameter (f, parms, Qborder_width, make_number (0),
3236 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3238 /* This defaults to 1 in order to match xterm. We recognize either
3239 internalBorderWidth or internalBorder (which is what xterm calls
3240 it). */
3241 if (NILP (Fassq (Qinternal_border_width, parms)))
3243 Lisp_Object value;
3245 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3246 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3247 if (! EQ (value, Qunbound))
3248 parms = Fcons (Fcons (Qinternal_border_width, value),
3249 parms);
3251 x_default_parameter (f, parms, Qinternal_border_width,
3252 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3253 make_number (0),
3254 #else
3255 make_number (1),
3256 #endif
3257 "internalBorderWidth", "internalBorderWidth",
3258 RES_TYPE_NUMBER);
3259 x_default_parameter (f, parms, Qvertical_scroll_bars,
3260 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3261 Qright,
3262 #else
3263 Qleft,
3264 #endif
3265 "verticalScrollBars", "ScrollBars",
3266 RES_TYPE_SYMBOL);
3268 /* Also do the stuff which must be set before the window exists. */
3269 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3270 "foreground", "Foreground", RES_TYPE_STRING);
3271 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3272 "background", "Background", RES_TYPE_STRING);
3273 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3274 "pointerColor", "Foreground", RES_TYPE_STRING);
3275 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3276 "borderColor", "BorderColor", RES_TYPE_STRING);
3277 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3278 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3279 x_default_parameter (f, parms, Qline_spacing, Qnil,
3280 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3281 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3282 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3283 x_default_parameter (f, parms, Qright_fringe, Qnil,
3284 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3286 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3287 "scrollBarForeground",
3288 "ScrollBarForeground", 1);
3289 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3290 "scrollBarBackground",
3291 "ScrollBarBackground", 0);
3293 #ifdef GLYPH_DEBUG
3294 image_cache_refcount =
3295 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3296 dpyinfo_refcount = dpyinfo->reference_count;
3297 #endif /* GLYPH_DEBUG */
3299 /* Init faces before x_default_parameter is called for scroll-bar
3300 parameters because that function calls x_set_scroll_bar_width,
3301 which calls change_frame_size, which calls Fset_window_buffer,
3302 which runs hooks, which call Fvertical_motion. At the end, we
3303 end up in init_iterator with a null face cache, which should not
3304 happen. */
3305 init_frame_faces (f);
3307 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3308 look up the X resources controlling the menu-bar and tool-bar
3309 here; they are processed specially at startup, and reflected in
3310 the values of the mode variables.
3312 Avoid calling window-configuration-change-hook; otherwise we
3313 could get an infloop in next_frame since the frame is not yet in
3314 Vframe_list. */
3316 ptrdiff_t count2 = SPECPDL_INDEX ();
3317 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3318 inhibit_lisp_code = Qt;
3320 x_default_parameter (f, parms, Qmenu_bar_lines,
3321 NILP (Vmenu_bar_mode)
3322 ? make_number (0) : make_number (1),
3323 NULL, NULL, RES_TYPE_NUMBER);
3324 x_default_parameter (f, parms, Qtool_bar_lines,
3325 NILP (Vtool_bar_mode)
3326 ? make_number (0) : make_number (1),
3327 NULL, NULL, RES_TYPE_NUMBER);
3329 unbind_to (count2, Qnil);
3332 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3333 "bufferPredicate", "BufferPredicate",
3334 RES_TYPE_SYMBOL);
3335 x_default_parameter (f, parms, Qtitle, Qnil,
3336 "title", "Title", RES_TYPE_STRING);
3337 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3338 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3339 x_default_parameter (f, parms, Qfullscreen, Qnil,
3340 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3341 x_default_parameter (f, parms, Qtool_bar_position,
3342 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
3344 /* Compute the size of the X window. */
3345 window_prompting = x_figure_window_size (f, parms, 1);
3347 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3348 f->no_split = minibuffer_only || EQ (tem, Qt);
3350 x_icon_verify (f, parms);
3352 /* Create the X widget or window. */
3353 #ifdef USE_X_TOOLKIT
3354 x_window (f, window_prompting, minibuffer_only);
3355 #else
3356 x_window (f);
3357 #endif
3359 x_icon (f, parms);
3360 x_make_gc (f);
3362 /* Now consider the frame official. */
3363 f->terminal->reference_count++;
3364 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3365 Vframe_list = Fcons (frame, Vframe_list);
3367 /* We need to do this after creating the X window, so that the
3368 icon-creation functions can say whose icon they're describing. */
3369 x_default_parameter (f, parms, Qicon_type, Qt,
3370 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3372 x_default_parameter (f, parms, Qauto_raise, Qnil,
3373 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3374 x_default_parameter (f, parms, Qauto_lower, Qnil,
3375 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3376 x_default_parameter (f, parms, Qcursor_type, Qbox,
3377 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3378 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3379 "scrollBarWidth", "ScrollBarWidth",
3380 RES_TYPE_NUMBER);
3381 x_default_parameter (f, parms, Qalpha, Qnil,
3382 "alpha", "Alpha", RES_TYPE_NUMBER);
3384 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3385 Change will not be effected unless different from the current
3386 FRAME_LINES (f). */
3387 width = FRAME_COLS (f);
3388 height = FRAME_LINES (f);
3390 SET_FRAME_COLS (f, 0);
3391 FRAME_LINES (f) = 0;
3392 change_frame_size (f, height, width, 1, 0, 0);
3394 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3395 /* Create the menu bar. */
3396 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3398 /* If this signals an error, we haven't set size hints for the
3399 frame and we didn't make it visible. */
3400 initialize_frame_menubar (f);
3402 #ifndef USE_GTK
3403 /* This is a no-op, except under Motif where it arranges the
3404 main window for the widgets on it. */
3405 lw_set_main_areas (f->output_data.x->column_widget,
3406 f->output_data.x->menubar_widget,
3407 f->output_data.x->edit_widget);
3408 #endif /* not USE_GTK */
3410 #endif /* USE_X_TOOLKIT || USE_GTK */
3412 /* Tell the server what size and position, etc, we want, and how
3413 badly we want them. This should be done after we have the menu
3414 bar so that its size can be taken into account. */
3415 BLOCK_INPUT;
3416 x_wm_set_size_hint (f, window_prompting, 0);
3417 UNBLOCK_INPUT;
3419 /* Make the window appear on the frame and enable display, unless
3420 the caller says not to. However, with explicit parent, Emacs
3421 cannot control visibility, so don't try. */
3422 if (! f->output_data.x->explicit_parent)
3424 Lisp_Object visibility;
3426 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3427 RES_TYPE_SYMBOL);
3428 if (EQ (visibility, Qunbound))
3429 visibility = Qt;
3431 if (EQ (visibility, Qicon))
3432 x_iconify_frame (f);
3433 else if (! NILP (visibility))
3434 x_make_frame_visible (f);
3435 else
3437 /* Must have been Qnil. */
3441 BLOCK_INPUT;
3443 /* Set machine name and pid for the purpose of window managers. */
3444 set_machine_and_pid_properties (f);
3446 /* Set the WM leader property. GTK does this itself, so this is not
3447 needed when using GTK. */
3448 if (dpyinfo->client_leader_window != 0)
3450 XChangeProperty (FRAME_X_DISPLAY (f),
3451 FRAME_OUTER_WINDOW (f),
3452 dpyinfo->Xatom_wm_client_leader,
3453 XA_WINDOW, 32, PropModeReplace,
3454 (unsigned char *) &dpyinfo->client_leader_window, 1);
3457 UNBLOCK_INPUT;
3459 /* Initialize `default-minibuffer-frame' in case this is the first
3460 frame on this terminal. */
3461 if (FRAME_HAS_MINIBUF_P (f)
3462 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3463 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3464 KSET (kb, Vdefault_minibuffer_frame, frame);
3466 /* All remaining specified parameters, which have not been "used"
3467 by x_get_arg and friends, now go in the misc. alist of the frame. */
3468 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3469 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3470 FSET (f, param_alist, Fcons (XCAR (tem), f->param_alist));
3472 UNGCPRO;
3474 /* Make sure windows on this frame appear in calls to next-window
3475 and similar functions. */
3476 Vwindow_list = Qnil;
3478 return unbind_to (count, frame);
3482 /* FRAME is used only to get a handle on the X display. We don't pass the
3483 display info directly because we're called from frame.c, which doesn't
3484 know about that structure. */
3486 Lisp_Object
3487 x_get_focus_frame (struct frame *frame)
3489 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
3490 Lisp_Object xfocus;
3491 if (! dpyinfo->x_focus_frame)
3492 return Qnil;
3494 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3495 return xfocus;
3499 /* In certain situations, when the window manager follows a
3500 click-to-focus policy, there seems to be no way around calling
3501 XSetInputFocus to give another frame the input focus .
3503 In an ideal world, XSetInputFocus should generally be avoided so
3504 that applications don't interfere with the window manager's focus
3505 policy. But I think it's okay to use when it's clearly done
3506 following a user-command. */
3508 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
3509 doc: /* Set the input focus to FRAME.
3510 FRAME nil means use the selected frame. */)
3511 (Lisp_Object frame)
3513 struct frame *f = check_x_frame (frame);
3514 Display *dpy = FRAME_X_DISPLAY (f);
3516 BLOCK_INPUT;
3517 x_catch_errors (dpy);
3519 if (FRAME_X_EMBEDDED_P (f))
3521 /* For Xembedded frames, normally the embedder forwards key
3522 events. See XEmbed Protocol Specification at
3523 http://freedesktop.org/wiki/Specifications/xembed-spec */
3524 xembed_request_focus (f);
3526 else
3528 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3529 RevertToParent, CurrentTime);
3530 x_ewmh_activate_frame (f);
3533 x_uncatch_errors ();
3534 UNBLOCK_INPUT;
3536 return Qnil;
3540 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3541 doc: /* Internal function called by `color-defined-p', which see
3542 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3543 (Lisp_Object color, Lisp_Object frame)
3545 XColor foo;
3546 FRAME_PTR f = check_x_frame (frame);
3548 CHECK_STRING (color);
3550 if (x_defined_color (f, SSDATA (color), &foo, 0))
3551 return Qt;
3552 else
3553 return Qnil;
3556 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3557 doc: /* Internal function called by `color-values', which see. */)
3558 (Lisp_Object color, Lisp_Object frame)
3560 XColor foo;
3561 FRAME_PTR f = check_x_frame (frame);
3563 CHECK_STRING (color);
3565 if (x_defined_color (f, SSDATA (color), &foo, 0))
3566 return list3 (make_number (foo.red),
3567 make_number (foo.green),
3568 make_number (foo.blue));
3569 else
3570 return Qnil;
3573 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3574 doc: /* Internal function called by `display-color-p', which see. */)
3575 (Lisp_Object terminal)
3577 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3579 if (dpyinfo->n_planes <= 2)
3580 return Qnil;
3582 switch (dpyinfo->visual->class)
3584 case StaticColor:
3585 case PseudoColor:
3586 case TrueColor:
3587 case DirectColor:
3588 return Qt;
3590 default:
3591 return Qnil;
3595 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3596 0, 1, 0,
3597 doc: /* Return t if the X display supports shades of gray.
3598 Note that color displays do support shades of gray.
3599 The optional argument TERMINAL specifies which display to ask about.
3600 TERMINAL should be a terminal object, a frame or a display name (a string).
3601 If omitted or nil, that stands for the selected frame's display. */)
3602 (Lisp_Object terminal)
3604 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3606 if (dpyinfo->n_planes <= 1)
3607 return Qnil;
3609 switch (dpyinfo->visual->class)
3611 case StaticColor:
3612 case PseudoColor:
3613 case TrueColor:
3614 case DirectColor:
3615 case StaticGray:
3616 case GrayScale:
3617 return Qt;
3619 default:
3620 return Qnil;
3624 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3625 0, 1, 0,
3626 doc: /* Return the width in pixels of the X display TERMINAL.
3627 The optional argument TERMINAL specifies which display to ask about.
3628 TERMINAL should be a terminal object, a frame or a display name (a string).
3629 If omitted or nil, that stands for the selected frame's display. */)
3630 (Lisp_Object terminal)
3632 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3634 return make_number (x_display_pixel_width (dpyinfo));
3637 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3638 Sx_display_pixel_height, 0, 1, 0,
3639 doc: /* Return the height in pixels of the X display TERMINAL.
3640 The optional argument TERMINAL specifies which display to ask about.
3641 TERMINAL should be a terminal object, a frame or a display name (a string).
3642 If omitted or nil, that stands for the selected frame's display. */)
3643 (Lisp_Object terminal)
3645 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3647 return make_number (x_display_pixel_height (dpyinfo));
3650 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3651 0, 1, 0,
3652 doc: /* Return the number of bitplanes of the X display TERMINAL.
3653 The optional argument TERMINAL specifies which display to ask about.
3654 TERMINAL should be a terminal object, a frame or a display name (a string).
3655 If omitted or nil, that stands for the selected frame's display. */)
3656 (Lisp_Object terminal)
3658 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3660 return make_number (dpyinfo->n_planes);
3663 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3664 0, 1, 0,
3665 doc: /* Return the number of color cells of the X 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 int nr_planes = DisplayPlanes (dpyinfo->display,
3674 XScreenNumberOfScreen (dpyinfo->screen));
3676 /* Truncate nr_planes to 24 to avoid integer overflow.
3677 Some displays says 32, but only 24 bits are actually significant.
3678 There are only very few and rare video cards that have more than
3679 24 significant bits. Also 24 bits is more than 16 million colors,
3680 it "should be enough for everyone". */
3681 if (nr_planes > 24) nr_planes = 24;
3683 return make_number (1 << nr_planes);
3686 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3687 Sx_server_max_request_size,
3688 0, 1, 0,
3689 doc: /* Return the maximum request size of the X server of display TERMINAL.
3690 The optional argument TERMINAL specifies which display to ask about.
3691 TERMINAL should be a terminal object, a frame or a display name (a string).
3692 If omitted or nil, that stands for the selected frame's display. */)
3693 (Lisp_Object terminal)
3695 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3697 return make_number (MAXREQUEST (dpyinfo->display));
3700 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3701 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3702 \(Labeling every distributor as a "vendor" embodies the false assumption
3703 that operating systems cannot be developed and distributed noncommercially.)
3704 The optional argument TERMINAL specifies which display to ask about.
3705 TERMINAL should be a terminal object, a frame or a display name (a string).
3706 If omitted or nil, that stands for the selected frame's display. */)
3707 (Lisp_Object terminal)
3709 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3710 const char *vendor = ServerVendor (dpyinfo->display);
3712 if (! vendor) vendor = "";
3713 return build_string (vendor);
3716 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3717 doc: /* Return the version numbers of the X server of display TERMINAL.
3718 The value is a list of three integers: the major and minor
3719 version numbers of the X Protocol in use, and the distributor-specific release
3720 number. See also the function `x-server-vendor'.
3722 The optional argument TERMINAL specifies which display to ask about.
3723 TERMINAL should be a terminal object, a frame or a display name (a string).
3724 If omitted or nil, that stands for the selected frame's display. */)
3725 (Lisp_Object terminal)
3727 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3728 Display *dpy = dpyinfo->display;
3730 return Fcons (make_number (ProtocolVersion (dpy)),
3731 Fcons (make_number (ProtocolRevision (dpy)),
3732 Fcons (make_number (VendorRelease (dpy)), Qnil)));
3735 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3736 doc: /* Return the number of screens on the X server of 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 (ScreenCount (dpyinfo->display));
3747 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3748 doc: /* Return the height in millimeters of the X display TERMINAL.
3749 The optional argument TERMINAL specifies which display to ask about.
3750 TERMINAL should be a terminal object, a frame or a display name (a string).
3751 If omitted or nil, that stands for the selected frame's display. */)
3752 (Lisp_Object terminal)
3754 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3756 return make_number (HeightMMOfScreen (dpyinfo->screen));
3759 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3760 doc: /* Return the width in millimeters of the X display TERMINAL.
3761 The optional argument TERMINAL specifies which display to ask about.
3762 TERMINAL should be a terminal object, a frame or a display name (a string).
3763 If omitted or nil, that stands for the selected frame's display. */)
3764 (Lisp_Object terminal)
3766 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3768 return make_number (WidthMMOfScreen (dpyinfo->screen));
3771 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3772 Sx_display_backing_store, 0, 1, 0,
3773 doc: /* Return an indication of whether X display TERMINAL does backing store.
3774 The value may be `always', `when-mapped', or `not-useful'.
3775 The optional argument TERMINAL specifies which display to ask about.
3776 TERMINAL should be a terminal object, a frame or a display name (a string).
3777 If omitted or nil, that stands for the selected frame's display. */)
3778 (Lisp_Object terminal)
3780 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3781 Lisp_Object result;
3783 switch (DoesBackingStore (dpyinfo->screen))
3785 case Always:
3786 result = intern ("always");
3787 break;
3789 case WhenMapped:
3790 result = intern ("when-mapped");
3791 break;
3793 case NotUseful:
3794 result = intern ("not-useful");
3795 break;
3797 default:
3798 error ("Strange value for BackingStore parameter of screen");
3799 result = Qnil;
3802 return result;
3805 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3806 Sx_display_visual_class, 0, 1, 0,
3807 doc: /* Return the visual class of the X display TERMINAL.
3808 The value is one of the symbols `static-gray', `gray-scale',
3809 `static-color', `pseudo-color', `true-color', or `direct-color'.
3811 The optional argument TERMINAL specifies which display to ask about.
3812 TERMINAL should a terminal object, a frame or a display name (a string).
3813 If omitted or nil, that stands for the selected frame's display. */)
3814 (Lisp_Object terminal)
3816 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3817 Lisp_Object result;
3819 switch (dpyinfo->visual->class)
3821 case StaticGray:
3822 result = intern ("static-gray");
3823 break;
3824 case GrayScale:
3825 result = intern ("gray-scale");
3826 break;
3827 case StaticColor:
3828 result = intern ("static-color");
3829 break;
3830 case PseudoColor:
3831 result = intern ("pseudo-color");
3832 break;
3833 case TrueColor:
3834 result = intern ("true-color");
3835 break;
3836 case DirectColor:
3837 result = intern ("direct-color");
3838 break;
3839 default:
3840 error ("Display has an unknown visual class");
3841 result = Qnil;
3844 return result;
3847 DEFUN ("x-display-save-under", Fx_display_save_under,
3848 Sx_display_save_under, 0, 1, 0,
3849 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3850 The optional argument TERMINAL specifies which display to ask about.
3851 TERMINAL should be a terminal object, a frame or a display name (a string).
3852 If omitted or nil, that stands for the selected frame's display. */)
3853 (Lisp_Object terminal)
3855 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3857 if (DoesSaveUnders (dpyinfo->screen) == True)
3858 return Qt;
3859 else
3860 return Qnil;
3864 x_pixel_width (register struct frame *f)
3866 return FRAME_PIXEL_WIDTH (f);
3870 x_pixel_height (register struct frame *f)
3872 return FRAME_PIXEL_HEIGHT (f);
3876 x_char_width (register struct frame *f)
3878 return FRAME_COLUMN_WIDTH (f);
3882 x_char_height (register struct frame *f)
3884 return FRAME_LINE_HEIGHT (f);
3889 /************************************************************************
3890 X Displays
3891 ************************************************************************/
3894 /* Mapping visual names to visuals. */
3896 static struct visual_class
3898 const char *name;
3899 int class;
3901 visual_classes[] =
3903 {"StaticGray", StaticGray},
3904 {"GrayScale", GrayScale},
3905 {"StaticColor", StaticColor},
3906 {"PseudoColor", PseudoColor},
3907 {"TrueColor", TrueColor},
3908 {"DirectColor", DirectColor},
3909 {NULL, 0}
3913 #ifndef HAVE_XSCREENNUMBEROFSCREEN
3915 /* Value is the screen number of screen SCR. This is a substitute for
3916 the X function with the same name when that doesn't exist. */
3919 XScreenNumberOfScreen (scr)
3920 register Screen *scr;
3922 Display *dpy = scr->display;
3923 int i;
3925 for (i = 0; i < dpy->nscreens; ++i)
3926 if (scr == dpy->screens + i)
3927 break;
3929 return i;
3932 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
3935 /* Select the visual that should be used on display DPYINFO. Set
3936 members of DPYINFO appropriately. Called from x_term_init. */
3938 void
3939 select_visual (struct x_display_info *dpyinfo)
3941 Display *dpy = dpyinfo->display;
3942 Screen *screen = dpyinfo->screen;
3943 Lisp_Object value;
3945 /* See if a visual is specified. */
3946 value = display_x_get_resource (dpyinfo,
3947 build_string ("visualClass"),
3948 build_string ("VisualClass"),
3949 Qnil, Qnil);
3950 if (STRINGP (value))
3952 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
3953 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
3954 depth, a decimal number. NAME is compared with case ignored. */
3955 char *s = alloca (SBYTES (value) + 1);
3956 char *dash;
3957 int i, class = -1;
3958 XVisualInfo vinfo;
3960 strcpy (s, SSDATA (value));
3961 dash = strchr (s, '-');
3962 if (dash)
3964 dpyinfo->n_planes = atoi (dash + 1);
3965 *dash = '\0';
3967 else
3968 /* We won't find a matching visual with depth 0, so that
3969 an error will be printed below. */
3970 dpyinfo->n_planes = 0;
3972 /* Determine the visual class. */
3973 for (i = 0; visual_classes[i].name; ++i)
3974 if (xstrcasecmp (s, visual_classes[i].name) == 0)
3976 class = visual_classes[i].class;
3977 break;
3980 /* Look up a matching visual for the specified class. */
3981 if (class == -1
3982 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
3983 dpyinfo->n_planes, class, &vinfo))
3984 fatal ("Invalid visual specification `%s'", SDATA (value));
3986 dpyinfo->visual = vinfo.visual;
3988 else
3990 int n_visuals;
3991 XVisualInfo *vinfo, vinfo_template;
3993 dpyinfo->visual = DefaultVisualOfScreen (screen);
3995 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
3996 vinfo_template.screen = XScreenNumberOfScreen (screen);
3997 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
3998 &vinfo_template, &n_visuals);
3999 if (n_visuals <= 0)
4000 fatal ("Can't get proper X visual info");
4002 dpyinfo->n_planes = vinfo->depth;
4003 XFree (vinfo);
4008 /* Return the X display structure for the display named NAME.
4009 Open a new connection if necessary. */
4011 static struct x_display_info *
4012 x_display_info_for_name (Lisp_Object name)
4014 Lisp_Object names;
4015 struct x_display_info *dpyinfo;
4017 CHECK_STRING (name);
4019 #if 0
4020 if (! EQ (Vinitial_window_system, intern ("x")))
4021 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4022 #endif
4024 for (dpyinfo = x_display_list, names = x_display_name_list;
4025 dpyinfo;
4026 dpyinfo = dpyinfo->next, names = XCDR (names))
4028 Lisp_Object tem;
4029 tem = Fstring_equal (XCAR (XCAR (names)), name);
4030 if (!NILP (tem))
4031 return dpyinfo;
4034 /* Use this general default value to start with. */
4035 Vx_resource_name = Vinvocation_name;
4037 validate_x_resource_name ();
4039 dpyinfo = x_term_init (name, (char *)0,
4040 SSDATA (Vx_resource_name));
4042 if (dpyinfo == 0)
4043 error ("Cannot connect to X server %s", SDATA (name));
4045 x_in_use = 1;
4046 XSETFASTINT (Vwindow_system_version, 11);
4048 return dpyinfo;
4052 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4053 1, 3, 0,
4054 doc: /* Open a connection to a display server.
4055 DISPLAY is the name of the display to connect to.
4056 Optional second arg XRM-STRING is a string of resources in xrdb format.
4057 If the optional third arg MUST-SUCCEED is non-nil,
4058 terminate Emacs if we can't open the connection.
4059 \(In the Nextstep version, the last two arguments are currently ignored.) */)
4060 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
4062 char *xrm_option;
4063 struct x_display_info *dpyinfo;
4065 CHECK_STRING (display);
4066 if (! NILP (xrm_string))
4067 CHECK_STRING (xrm_string);
4069 #if 0
4070 if (! EQ (Vinitial_window_system, intern ("x")))
4071 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4072 #endif
4074 if (! NILP (xrm_string))
4075 xrm_option = SSDATA (xrm_string);
4076 else
4077 xrm_option = (char *) 0;
4079 validate_x_resource_name ();
4081 /* This is what opens the connection and sets x_current_display.
4082 This also initializes many symbols, such as those used for input. */
4083 dpyinfo = x_term_init (display, xrm_option,
4084 SSDATA (Vx_resource_name));
4086 if (dpyinfo == 0)
4088 if (!NILP (must_succeed))
4089 fatal ("Cannot connect to X server %s.\n\
4090 Check the DISPLAY environment variable or use `-d'.\n\
4091 Also use the `xauth' program to verify that you have the proper\n\
4092 authorization information needed to connect the X server.\n\
4093 An insecure way to solve the problem may be to use `xhost'.\n",
4094 SDATA (display));
4095 else
4096 error ("Cannot connect to X server %s", SDATA (display));
4099 x_in_use = 1;
4101 XSETFASTINT (Vwindow_system_version, 11);
4102 return Qnil;
4105 DEFUN ("x-close-connection", Fx_close_connection,
4106 Sx_close_connection, 1, 1, 0,
4107 doc: /* Close the connection to TERMINAL's X server.
4108 For TERMINAL, specify a terminal object, a frame or a display name (a
4109 string). If TERMINAL is nil, that stands for the selected frame's
4110 terminal. */)
4111 (Lisp_Object terminal)
4113 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4115 if (dpyinfo->reference_count > 0)
4116 error ("Display still has frames on it");
4118 x_delete_terminal (dpyinfo->terminal);
4120 return Qnil;
4123 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4124 doc: /* Return the list of display names that Emacs has connections to. */)
4125 (void)
4127 Lisp_Object tail, result;
4129 result = Qnil;
4130 for (tail = x_display_name_list; CONSP (tail); tail = XCDR (tail))
4131 result = Fcons (XCAR (XCAR (tail)), result);
4133 return result;
4136 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4137 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4138 This function only has an effect on X Windows. With MS Windows, it is
4139 defined but does nothing.
4141 If ON is nil, allow buffering of requests.
4142 Turning on synchronization prohibits the Xlib routines from buffering
4143 requests and seriously degrades performance, but makes debugging much
4144 easier.
4145 The optional second argument TERMINAL specifies which display to act on.
4146 TERMINAL should be a terminal object, a frame or a display name (a string).
4147 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4148 (Lisp_Object on, Lisp_Object terminal)
4150 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4152 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4154 return Qnil;
4157 /* Wait for responses to all X commands issued so far for frame F. */
4159 void
4160 x_sync (FRAME_PTR f)
4162 BLOCK_INPUT;
4163 XSync (FRAME_X_DISPLAY (f), False);
4164 UNBLOCK_INPUT;
4168 /***********************************************************************
4169 Window properties
4170 ***********************************************************************/
4172 DEFUN ("x-change-window-property", Fx_change_window_property,
4173 Sx_change_window_property, 2, 6, 0,
4174 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4175 PROP must be a string. VALUE may be a string or a list of conses,
4176 numbers and/or strings. If an element in the list is a string, it is
4177 converted to an atom and the value of the atom is used. If an element
4178 is a cons, it is converted to a 32 bit number where the car is the 16
4179 top bits and the cdr is the lower 16 bits.
4181 FRAME nil or omitted means use the selected frame.
4182 If TYPE is given and non-nil, it is the name of the type of VALUE.
4183 If TYPE is not given or nil, the type is STRING.
4184 FORMAT gives the size in bits of each element if VALUE is a list.
4185 It must be one of 8, 16 or 32.
4186 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4187 If OUTER-P is non-nil, the property is changed for the outer X window of
4188 FRAME. Default is to change on the edit X window. */)
4189 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4190 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4192 struct frame *f = check_x_frame (frame);
4193 Atom prop_atom;
4194 Atom target_type = XA_STRING;
4195 int element_format = 8;
4196 unsigned char *data;
4197 int nelements;
4198 Window w;
4200 CHECK_STRING (prop);
4202 if (! NILP (format))
4204 CHECK_NUMBER (format);
4206 if (XINT (format) != 8 && XINT (format) != 16
4207 && XINT (format) != 32)
4208 error ("FORMAT must be one of 8, 16 or 32");
4209 element_format = XINT (format);
4212 if (CONSP (value))
4214 ptrdiff_t elsize;
4216 nelements = x_check_property_data (value);
4217 if (nelements == -1)
4218 error ("Bad data in VALUE, must be number, string or cons");
4220 /* The man page for XChangeProperty:
4221 "If the specified format is 32, the property data must be a
4222 long array."
4223 This applies even if long is more than 32 bits. The X library
4224 converts to 32 bits before sending to the X server. */
4225 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4226 data = xnmalloc (nelements, elsize);
4228 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4230 else
4232 CHECK_STRING (value);
4233 data = SDATA (value);
4234 if (INT_MAX < SBYTES (value))
4235 error ("VALUE too long");
4236 nelements = SBYTES (value);
4239 BLOCK_INPUT;
4240 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4241 if (! NILP (type))
4243 CHECK_STRING (type);
4244 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4247 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4248 else w = FRAME_X_WINDOW (f);
4250 XChangeProperty (FRAME_X_DISPLAY (f), w,
4251 prop_atom, target_type, element_format, PropModeReplace,
4252 data, nelements);
4254 if (CONSP (value)) xfree (data);
4256 /* Make sure the property is set when we return. */
4257 XFlush (FRAME_X_DISPLAY (f));
4258 UNBLOCK_INPUT;
4260 return value;
4264 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4265 Sx_delete_window_property, 1, 2, 0,
4266 doc: /* Remove window property PROP from X window of FRAME.
4267 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4268 (Lisp_Object prop, Lisp_Object frame)
4270 struct frame *f = check_x_frame (frame);
4271 Atom prop_atom;
4273 CHECK_STRING (prop);
4274 BLOCK_INPUT;
4275 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4276 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4278 /* Make sure the property is removed when we return. */
4279 XFlush (FRAME_X_DISPLAY (f));
4280 UNBLOCK_INPUT;
4282 return prop;
4286 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4287 1, 6, 0,
4288 doc: /* Value is the value of window property PROP on FRAME.
4289 If FRAME is nil or omitted, use the selected frame.
4291 On X Windows, the following optional arguments are also accepted:
4292 If TYPE is nil or omitted, get the property as a string.
4293 Otherwise TYPE is the name of the atom that denotes the type expected.
4294 If SOURCE is non-nil, get the property on that window instead of from
4295 FRAME. The number 0 denotes the root window.
4296 If DELETE-P is non-nil, delete the property after retrieving it.
4297 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4299 On MS Windows, this function accepts but ignores those optional arguments.
4301 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4302 no value of TYPE (always string in the MS Windows case). */)
4303 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4304 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4306 struct frame *f = check_x_frame (frame);
4307 Atom prop_atom;
4308 int rc;
4309 Lisp_Object prop_value = Qnil;
4310 unsigned char *tmp_data = NULL;
4311 Atom actual_type;
4312 Atom target_type = XA_STRING;
4313 int actual_format;
4314 unsigned long actual_size, bytes_remaining;
4315 Window target_window = FRAME_X_WINDOW (f);
4316 struct gcpro gcpro1;
4318 GCPRO1 (prop_value);
4319 CHECK_STRING (prop);
4321 if (! NILP (source))
4323 CONS_TO_INTEGER (source, Window, target_window);
4324 if (! target_window)
4325 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
4328 BLOCK_INPUT;
4329 if (STRINGP (type))
4331 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4332 target_type = AnyPropertyType;
4333 else
4334 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4337 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4338 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4339 prop_atom, 0, 0, False, target_type,
4340 &actual_type, &actual_format, &actual_size,
4341 &bytes_remaining, &tmp_data);
4342 if (rc == Success)
4344 int size = bytes_remaining;
4346 XFree (tmp_data);
4347 tmp_data = NULL;
4349 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4350 prop_atom, 0, bytes_remaining,
4351 ! NILP (delete_p), target_type,
4352 &actual_type, &actual_format,
4353 &actual_size, &bytes_remaining,
4354 &tmp_data);
4355 if (rc == Success && tmp_data)
4357 /* The man page for XGetWindowProperty says:
4358 "If the returned format is 32, the returned data is represented
4359 as a long array and should be cast to that type to obtain the
4360 elements."
4361 This applies even if long is more than 32 bits, the X library
4362 converts from 32 bit elements received from the X server to long
4363 and passes the long array to us. Thus, for that case memcpy can not
4364 be used. We convert to a 32 bit type here, because so much code
4365 assume on that.
4367 The bytes and offsets passed to XGetWindowProperty refers to the
4368 property and those are indeed in 32 bit quantities if format is
4369 32. */
4371 if (32 < BITS_PER_LONG && actual_format == 32)
4373 unsigned long i;
4374 int *idata = (int *) tmp_data;
4375 long *ldata = (long *) tmp_data;
4377 for (i = 0; i < actual_size; ++i)
4378 idata[i] = (int) ldata[i];
4381 if (NILP (vector_ret_p))
4382 prop_value = make_string ((char *) tmp_data, size);
4383 else
4384 prop_value = x_property_data_to_lisp (f,
4385 tmp_data,
4386 actual_type,
4387 actual_format,
4388 actual_size);
4391 if (tmp_data) XFree (tmp_data);
4394 UNBLOCK_INPUT;
4395 UNGCPRO;
4396 return prop_value;
4401 /***********************************************************************
4402 Busy cursor
4403 ***********************************************************************/
4405 /* Timer function of hourglass_atimer. TIMER is equal to
4406 hourglass_atimer.
4408 Display an hourglass pointer on all frames by mapping the frames'
4409 hourglass_window. Set the hourglass_p flag in the frames'
4410 output_data.x structure to indicate that an hourglass cursor is
4411 shown on the frames. */
4413 void
4414 show_hourglass (struct atimer *timer)
4416 /* The timer implementation will cancel this timer automatically
4417 after this function has run. Set hourglass_atimer to null
4418 so that we know the timer doesn't have to be canceled. */
4419 hourglass_atimer = NULL;
4421 if (!hourglass_shown_p)
4423 Lisp_Object rest, frame;
4425 BLOCK_INPUT;
4427 FOR_EACH_FRAME (rest, frame)
4429 struct frame *f = XFRAME (frame);
4431 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4433 Display *dpy = FRAME_X_DISPLAY (f);
4435 #ifdef USE_X_TOOLKIT
4436 if (f->output_data.x->widget)
4437 #else
4438 if (FRAME_OUTER_WINDOW (f))
4439 #endif
4441 f->output_data.x->hourglass_p = 1;
4443 if (!f->output_data.x->hourglass_window)
4445 unsigned long mask = CWCursor;
4446 XSetWindowAttributes attrs;
4447 #ifdef USE_GTK
4448 Window parent = FRAME_X_WINDOW (f);
4449 #else
4450 Window parent = FRAME_OUTER_WINDOW (f);
4451 #endif
4452 attrs.cursor = f->output_data.x->hourglass_cursor;
4454 f->output_data.x->hourglass_window
4455 = XCreateWindow (dpy, parent,
4456 0, 0, 32000, 32000, 0, 0,
4457 InputOnly,
4458 CopyFromParent,
4459 mask, &attrs);
4462 XMapRaised (dpy, f->output_data.x->hourglass_window);
4463 XFlush (dpy);
4468 hourglass_shown_p = 1;
4469 UNBLOCK_INPUT;
4474 /* Hide the hourglass pointer on all frames, if it is currently
4475 shown. */
4477 void
4478 hide_hourglass (void)
4480 if (hourglass_shown_p)
4482 Lisp_Object rest, frame;
4484 BLOCK_INPUT;
4485 FOR_EACH_FRAME (rest, frame)
4487 struct frame *f = XFRAME (frame);
4489 if (FRAME_X_P (f)
4490 /* Watch out for newly created frames. */
4491 && f->output_data.x->hourglass_window)
4493 XUnmapWindow (FRAME_X_DISPLAY (f),
4494 f->output_data.x->hourglass_window);
4495 /* Sync here because XTread_socket looks at the
4496 hourglass_p flag that is reset to zero below. */
4497 XSync (FRAME_X_DISPLAY (f), False);
4498 f->output_data.x->hourglass_p = 0;
4502 hourglass_shown_p = 0;
4503 UNBLOCK_INPUT;
4509 /***********************************************************************
4510 Tool tips
4511 ***********************************************************************/
4513 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4514 Lisp_Object, Lisp_Object);
4515 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4516 Lisp_Object, int, int, int *, int *);
4518 /* The frame of a currently visible tooltip. */
4520 Lisp_Object tip_frame;
4522 /* If non-nil, a timer started that hides the last tooltip when it
4523 fires. */
4525 static Lisp_Object tip_timer;
4526 Window tip_window;
4528 /* If non-nil, a vector of 3 elements containing the last args
4529 with which x-show-tip was called. See there. */
4531 static Lisp_Object last_show_tip_args;
4534 static Lisp_Object
4535 unwind_create_tip_frame (Lisp_Object frame)
4537 Lisp_Object deleted;
4539 deleted = unwind_create_frame (frame);
4540 if (EQ (deleted, Qt))
4542 tip_window = None;
4543 tip_frame = Qnil;
4546 return deleted;
4550 /* Create a frame for a tooltip on the display described by DPYINFO.
4551 PARMS is a list of frame parameters. TEXT is the string to
4552 display in the tip frame. Value is the frame.
4554 Note that functions called here, esp. x_default_parameter can
4555 signal errors, for instance when a specified color name is
4556 undefined. We have to make sure that we're in a consistent state
4557 when this happens. */
4559 static Lisp_Object
4560 x_create_tip_frame (struct x_display_info *dpyinfo,
4561 Lisp_Object parms,
4562 Lisp_Object text)
4564 struct frame *f;
4565 Lisp_Object frame;
4566 Lisp_Object name;
4567 int width, height;
4568 ptrdiff_t count = SPECPDL_INDEX ();
4569 struct gcpro gcpro1, gcpro2, gcpro3;
4570 int face_change_count_before = face_change_count;
4571 Lisp_Object buffer;
4572 struct buffer *old_buffer;
4574 check_x ();
4576 if (!dpyinfo->terminal->name)
4577 error ("Terminal is not live, can't create new frames on it");
4579 parms = Fcopy_alist (parms);
4581 /* Get the name of the frame to use for resource lookup. */
4582 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4583 if (!STRINGP (name)
4584 && !EQ (name, Qunbound)
4585 && !NILP (name))
4586 error ("Invalid frame name--not a string or nil");
4588 frame = Qnil;
4589 GCPRO3 (parms, name, frame);
4590 f = make_frame (1);
4591 XSETFRAME (frame, f);
4593 buffer = Fget_buffer_create (build_string (" *tip*"));
4594 /* Use set_window_buffer instead of Fset_window_buffer (see
4595 discussion of bug#11984, bug#12025, bug#12026). */
4596 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
4597 old_buffer = current_buffer;
4598 set_buffer_internal_1 (XBUFFER (buffer));
4599 BSET (current_buffer, truncate_lines, Qnil);
4600 specbind (Qinhibit_read_only, Qt);
4601 specbind (Qinhibit_modification_hooks, Qt);
4602 Ferase_buffer ();
4603 Finsert (1, &text);
4604 set_buffer_internal_1 (old_buffer);
4606 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
4607 record_unwind_protect (unwind_create_tip_frame, frame);
4609 f->terminal = dpyinfo->terminal;
4611 /* By setting the output method, we're essentially saying that
4612 the frame is live, as per FRAME_LIVE_P. If we get a signal
4613 from this point on, x_destroy_window might screw up reference
4614 counts etc. */
4615 f->output_method = output_x_window;
4616 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4617 f->output_data.x->icon_bitmap = -1;
4618 FRAME_FONTSET (f) = -1;
4619 f->output_data.x->scroll_bar_foreground_pixel = -1;
4620 f->output_data.x->scroll_bar_background_pixel = -1;
4621 #ifdef USE_TOOLKIT_SCROLL_BARS
4622 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4623 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4624 #endif /* USE_TOOLKIT_SCROLL_BARS */
4625 FSET (f, icon_name, Qnil);
4626 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4627 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4628 f->output_data.x->explicit_parent = 0;
4630 /* These colors will be set anyway later, but it's important
4631 to get the color reference counts right, so initialize them! */
4633 Lisp_Object black;
4634 struct gcpro gcpro1;
4636 /* Function x_decode_color can signal an error. Make
4637 sure to initialize color slots so that we won't try
4638 to free colors we haven't allocated. */
4639 FRAME_FOREGROUND_PIXEL (f) = -1;
4640 FRAME_BACKGROUND_PIXEL (f) = -1;
4641 f->output_data.x->cursor_pixel = -1;
4642 f->output_data.x->cursor_foreground_pixel = -1;
4643 f->output_data.x->border_pixel = -1;
4644 f->output_data.x->mouse_pixel = -1;
4646 black = build_string ("black");
4647 GCPRO1 (black);
4648 FRAME_FOREGROUND_PIXEL (f)
4649 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4650 FRAME_BACKGROUND_PIXEL (f)
4651 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4652 f->output_data.x->cursor_pixel
4653 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4654 f->output_data.x->cursor_foreground_pixel
4655 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4656 f->output_data.x->border_pixel
4657 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4658 f->output_data.x->mouse_pixel
4659 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4660 UNGCPRO;
4663 /* Set the name; the functions to which we pass f expect the name to
4664 be set. */
4665 if (EQ (name, Qunbound) || NILP (name))
4667 FSET (f, name, build_string (dpyinfo->x_id_name));
4668 f->explicit_name = 0;
4670 else
4672 FSET (f, name, name);
4673 f->explicit_name = 1;
4674 /* use the frame's title when getting resources for this frame. */
4675 specbind (Qx_resource_name, name);
4678 f->resx = dpyinfo->resx;
4679 f->resy = dpyinfo->resy;
4681 register_font_driver (&xfont_driver, f);
4682 #ifdef HAVE_FREETYPE
4683 #ifdef HAVE_XFT
4684 register_font_driver (&xftfont_driver, f);
4685 #else /* not HAVE_XFT */
4686 register_font_driver (&ftxfont_driver, f);
4687 #endif /* not HAVE_XFT */
4688 #endif /* HAVE_FREETYPE */
4690 x_default_parameter (f, parms, Qfont_backend, Qnil,
4691 "fontBackend", "FontBackend", RES_TYPE_STRING);
4693 /* Extract the window parameters from the supplied values that are
4694 needed to determine window geometry. */
4695 x_default_font_parameter (f, parms);
4697 x_default_parameter (f, parms, Qborder_width, make_number (0),
4698 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4700 /* This defaults to 2 in order to match xterm. We recognize either
4701 internalBorderWidth or internalBorder (which is what xterm calls
4702 it). */
4703 if (NILP (Fassq (Qinternal_border_width, parms)))
4705 Lisp_Object value;
4707 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4708 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4709 if (! EQ (value, Qunbound))
4710 parms = Fcons (Fcons (Qinternal_border_width, value),
4711 parms);
4714 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4715 "internalBorderWidth", "internalBorderWidth",
4716 RES_TYPE_NUMBER);
4718 /* Also do the stuff which must be set before the window exists. */
4719 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4720 "foreground", "Foreground", RES_TYPE_STRING);
4721 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4722 "background", "Background", RES_TYPE_STRING);
4723 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4724 "pointerColor", "Foreground", RES_TYPE_STRING);
4725 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4726 "cursorColor", "Foreground", RES_TYPE_STRING);
4727 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4728 "borderColor", "BorderColor", RES_TYPE_STRING);
4730 #ifdef GLYPH_DEBUG
4731 image_cache_refcount =
4732 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4733 dpyinfo_refcount = dpyinfo->reference_count;
4734 #endif /* GLYPH_DEBUG */
4736 /* Init faces before x_default_parameter is called for scroll-bar
4737 parameters because that function calls x_set_scroll_bar_width,
4738 which calls change_frame_size, which calls Fset_window_buffer,
4739 which runs hooks, which call Fvertical_motion. At the end, we
4740 end up in init_iterator with a null face cache, which should not
4741 happen. */
4742 init_frame_faces (f);
4744 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4746 x_figure_window_size (f, parms, 0);
4749 XSetWindowAttributes attrs;
4750 unsigned long mask;
4751 Atom type = FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
4753 BLOCK_INPUT;
4754 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4755 if (DoesSaveUnders (dpyinfo->screen))
4756 mask |= CWSaveUnder;
4758 /* Window managers look at the override-redirect flag to determine
4759 whether or net to give windows a decoration (Xlib spec, chapter
4760 3.2.8). */
4761 attrs.override_redirect = True;
4762 attrs.save_under = True;
4763 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4764 /* Arrange for getting MapNotify and UnmapNotify events. */
4765 attrs.event_mask = StructureNotifyMask;
4766 tip_window
4767 = FRAME_X_WINDOW (f)
4768 = XCreateWindow (FRAME_X_DISPLAY (f),
4769 FRAME_X_DISPLAY_INFO (f)->root_window,
4770 /* x, y, width, height */
4771 0, 0, 1, 1,
4772 /* Border. */
4773 f->border_width,
4774 CopyFromParent, InputOutput, CopyFromParent,
4775 mask, &attrs);
4776 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
4777 FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type,
4778 XA_ATOM, 32, PropModeReplace,
4779 (unsigned char *)&type, 1);
4780 UNBLOCK_INPUT;
4783 x_make_gc (f);
4785 x_default_parameter (f, parms, Qauto_raise, Qnil,
4786 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4787 x_default_parameter (f, parms, Qauto_lower, Qnil,
4788 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4789 x_default_parameter (f, parms, Qcursor_type, Qbox,
4790 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4792 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4793 Change will not be effected unless different from the current
4794 FRAME_LINES (f). */
4795 width = FRAME_COLS (f);
4796 height = FRAME_LINES (f);
4797 SET_FRAME_COLS (f, 0);
4798 FRAME_LINES (f) = 0;
4799 change_frame_size (f, height, width, 1, 0, 0);
4801 /* Add `tooltip' frame parameter's default value. */
4802 if (NILP (Fframe_parameter (frame, Qtooltip)))
4803 Fmodify_frame_parameters (frame, Fcons (Fcons (Qtooltip, Qt), Qnil));
4805 /* FIXME - can this be done in a similar way to normal frames?
4806 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
4808 /* Set the `display-type' frame parameter before setting up faces. */
4810 Lisp_Object disptype;
4812 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
4813 disptype = intern ("mono");
4814 else if (FRAME_X_DISPLAY_INFO (f)->visual->class == GrayScale
4815 || FRAME_X_DISPLAY_INFO (f)->visual->class == StaticGray)
4816 disptype = intern ("grayscale");
4817 else
4818 disptype = intern ("color");
4820 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
4821 Fmodify_frame_parameters (frame, Fcons (Fcons (Qdisplay_type, disptype),
4822 Qnil));
4825 /* Set up faces after all frame parameters are known. This call
4826 also merges in face attributes specified for new frames.
4828 Frame parameters may be changed if .Xdefaults contains
4829 specifications for the default font. For example, if there is an
4830 `Emacs.default.attributeBackground: pink', the `background-color'
4831 attribute of the frame get's set, which let's the internal border
4832 of the tooltip frame appear in pink. Prevent this. */
4834 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
4836 /* Set tip_frame here, so that */
4837 tip_frame = frame;
4838 call2 (Qface_set_after_frame_default, frame, Qnil);
4840 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
4841 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
4842 Qnil));
4845 f->no_split = 1;
4847 UNGCPRO;
4849 /* Now that the frame will be official, it counts as a reference to
4850 its display and terminal. */
4851 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4852 f->terminal->reference_count++;
4854 /* It is now ok to make the frame official even if we get an error
4855 below. And the frame needs to be on Vframe_list or making it
4856 visible won't work. */
4857 Vframe_list = Fcons (frame, Vframe_list);
4860 /* Setting attributes of faces of the tooltip frame from resources
4861 and similar will increment face_change_count, which leads to the
4862 clearing of all current matrices. Since this isn't necessary
4863 here, avoid it by resetting face_change_count to the value it
4864 had before we created the tip frame. */
4865 face_change_count = face_change_count_before;
4867 /* Discard the unwind_protect. */
4868 return unbind_to (count, frame);
4872 /* Compute where to display tip frame F. PARMS is the list of frame
4873 parameters for F. DX and DY are specified offsets from the current
4874 location of the mouse. WIDTH and HEIGHT are the width and height
4875 of the tooltip. Return coordinates relative to the root window of
4876 the display in *ROOT_X, and *ROOT_Y. */
4878 static void
4879 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)
4881 Lisp_Object left, top;
4882 int win_x, win_y;
4883 Window root, child;
4884 unsigned pmask;
4886 /* User-specified position? */
4887 left = Fcdr (Fassq (Qleft, parms));
4888 top = Fcdr (Fassq (Qtop, parms));
4890 /* Move the tooltip window where the mouse pointer is. Resize and
4891 show it. */
4892 if (!INTEGERP (left) || !INTEGERP (top))
4894 BLOCK_INPUT;
4895 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
4896 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
4897 UNBLOCK_INPUT;
4900 if (INTEGERP (top))
4901 *root_y = XINT (top);
4902 else if (*root_y + XINT (dy) <= 0)
4903 *root_y = 0; /* Can happen for negative dy */
4904 else if (*root_y + XINT (dy) + height
4905 <= x_display_pixel_height (FRAME_X_DISPLAY_INFO (f)))
4906 /* It fits below the pointer */
4907 *root_y += XINT (dy);
4908 else if (height + XINT (dy) <= *root_y)
4909 /* It fits above the pointer. */
4910 *root_y -= height + XINT (dy);
4911 else
4912 /* Put it on the top. */
4913 *root_y = 0;
4915 if (INTEGERP (left))
4916 *root_x = XINT (left);
4917 else if (*root_x + XINT (dx) <= 0)
4918 *root_x = 0; /* Can happen for negative dx */
4919 else if (*root_x + XINT (dx) + width
4920 <= x_display_pixel_width (FRAME_X_DISPLAY_INFO (f)))
4921 /* It fits to the right of the pointer. */
4922 *root_x += XINT (dx);
4923 else if (width + XINT (dx) <= *root_x)
4924 /* It fits to the left of the pointer. */
4925 *root_x -= width + XINT (dx);
4926 else
4927 /* Put it left-justified on the screen--it ought to fit that way. */
4928 *root_x = 0;
4932 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
4933 doc: /* Show STRING in a "tooltip" window on frame FRAME.
4934 A tooltip window is a small X window displaying a string.
4936 This is an internal function; Lisp code should call `tooltip-show'.
4938 FRAME nil or omitted means use the selected frame.
4940 PARMS is an optional list of frame parameters which can be used to
4941 change the tooltip's appearance.
4943 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
4944 means use the default timeout of 5 seconds.
4946 If the list of frame parameters PARMS contains a `left' parameters,
4947 the tooltip is displayed at that x-position. Otherwise it is
4948 displayed at the mouse position, with offset DX added (default is 5 if
4949 DX isn't specified). Likewise for the y-position; if a `top' frame
4950 parameter is specified, it determines the y-position of the tooltip
4951 window, otherwise it is displayed at the mouse position, with offset
4952 DY added (default is -10).
4954 A tooltip's maximum size is specified by `x-max-tooltip-size'.
4955 Text larger than the specified size is clipped. */)
4956 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
4958 struct frame *f;
4959 struct window *w;
4960 int root_x, root_y;
4961 struct buffer *old_buffer;
4962 struct text_pos pos;
4963 int i, width, height, seen_reversed_p;
4964 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4965 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4966 ptrdiff_t count = SPECPDL_INDEX ();
4968 specbind (Qinhibit_redisplay, Qt);
4970 GCPRO4 (string, parms, frame, timeout);
4972 CHECK_STRING (string);
4973 if (SCHARS (string) == 0)
4974 string = make_unibyte_string (" ", 1);
4976 f = check_x_frame (frame);
4977 if (NILP (timeout))
4978 timeout = make_number (5);
4979 else
4980 CHECK_NATNUM (timeout);
4982 if (NILP (dx))
4983 dx = make_number (5);
4984 else
4985 CHECK_NUMBER (dx);
4987 if (NILP (dy))
4988 dy = make_number (-10);
4989 else
4990 CHECK_NUMBER (dy);
4992 #ifdef USE_GTK
4993 if (x_gtk_use_system_tooltips)
4995 int ok;
4997 /* Hide a previous tip, if any. */
4998 Fx_hide_tip ();
5000 BLOCK_INPUT;
5001 if ((ok = xg_prepare_tooltip (f, string, &width, &height)) != 0)
5003 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5004 xg_show_tooltip (f, root_x, root_y);
5005 /* This is used in Fx_hide_tip. */
5006 XSETFRAME (tip_frame, f);
5008 UNBLOCK_INPUT;
5009 if (ok) goto start_timer;
5011 #endif /* USE_GTK */
5013 if (NILP (last_show_tip_args))
5014 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5016 if (!NILP (tip_frame))
5018 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5019 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5020 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5022 if (EQ (frame, last_frame)
5023 && !NILP (Fequal (last_string, string))
5024 && !NILP (Fequal (last_parms, parms)))
5026 struct frame *tip_f = XFRAME (tip_frame);
5028 /* Only DX and DY have changed. */
5029 if (!NILP (tip_timer))
5031 Lisp_Object timer = tip_timer;
5032 tip_timer = Qnil;
5033 call1 (Qcancel_timer, timer);
5036 BLOCK_INPUT;
5037 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
5038 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
5039 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
5040 root_x, root_y);
5041 UNBLOCK_INPUT;
5042 goto start_timer;
5046 /* Hide a previous tip, if any. */
5047 Fx_hide_tip ();
5049 ASET (last_show_tip_args, 0, string);
5050 ASET (last_show_tip_args, 1, frame);
5051 ASET (last_show_tip_args, 2, parms);
5053 /* Add default values to frame parameters. */
5054 if (NILP (Fassq (Qname, parms)))
5055 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5056 if (NILP (Fassq (Qinternal_border_width, parms)))
5057 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5058 if (NILP (Fassq (Qborder_width, parms)))
5059 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5060 if (NILP (Fassq (Qborder_color, parms)))
5061 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5062 if (NILP (Fassq (Qbackground_color, parms)))
5063 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5064 parms);
5066 /* Create a frame for the tooltip, and record it in the global
5067 variable tip_frame. */
5068 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
5069 f = XFRAME (frame);
5071 /* Set up the frame's root window. */
5072 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5073 WSET (w, left_col, make_number (0));
5074 WSET (w, top_line, make_number (0));
5076 if (CONSP (Vx_max_tooltip_size)
5077 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
5078 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
5080 WSET (w, total_cols, XCAR (Vx_max_tooltip_size));
5081 WSET (w, total_lines, XCDR (Vx_max_tooltip_size));
5083 else
5085 WSET (w, total_cols, make_number (80));
5086 WSET (w, total_lines, make_number (40));
5089 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5090 adjust_glyphs (f);
5091 w->pseudo_window_p = 1;
5093 /* Display the tooltip text in a temporary buffer. */
5094 old_buffer = current_buffer;
5095 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5096 BSET (current_buffer, truncate_lines, Qnil);
5097 clear_glyph_matrix (w->desired_matrix);
5098 clear_glyph_matrix (w->current_matrix);
5099 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5100 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5102 /* Compute width and height of the tooltip. */
5103 width = height = seen_reversed_p = 0;
5104 for (i = 0; i < w->desired_matrix->nrows; ++i)
5106 struct glyph_row *row = &w->desired_matrix->rows[i];
5107 struct glyph *last;
5108 int row_width;
5110 /* Stop at the first empty row at the end. */
5111 if (!row->enabled_p || !row->displays_text_p)
5112 break;
5114 /* Let the row go over the full width of the frame. */
5115 row->full_width_p = 1;
5117 row_width = row->pixel_width;
5118 if (row->used[TEXT_AREA])
5120 /* There's a glyph at the end of rows that is used to place
5121 the cursor there. Don't include the width of this glyph. */
5122 if (!row->reversed_p)
5124 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5125 if (INTEGERP (last->object))
5126 row_width -= last->pixel_width;
5128 else
5130 /* There could be a stretch glyph at the beginning of R2L
5131 rows that is produced by extend_face_to_end_of_line.
5132 Don't count that glyph. */
5133 struct glyph *g = row->glyphs[TEXT_AREA];
5135 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5137 row_width -= g->pixel_width;
5138 seen_reversed_p = 1;
5143 height += row->height;
5144 width = max (width, row_width);
5147 /* If we've seen partial-length R2L rows, we need to re-adjust the
5148 tool-tip frame width and redisplay it again, to avoid over-wide
5149 tips due to the stretch glyph that extends R2L lines to full
5150 width of the frame. */
5151 if (seen_reversed_p)
5153 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5154 not in pixels. */
5155 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5156 WSET (w, total_cols, make_number (width));
5157 FRAME_TOTAL_COLS (f) = width;
5158 adjust_glyphs (f);
5159 clear_glyph_matrix (w->desired_matrix);
5160 clear_glyph_matrix (w->current_matrix);
5161 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5162 width = height = 0;
5163 /* Recompute width and height of the tooltip. */
5164 for (i = 0; i < w->desired_matrix->nrows; ++i)
5166 struct glyph_row *row = &w->desired_matrix->rows[i];
5167 struct glyph *last;
5168 int row_width;
5170 if (!row->enabled_p || !row->displays_text_p)
5171 break;
5172 row->full_width_p = 1;
5173 row_width = row->pixel_width;
5174 if (row->used[TEXT_AREA] && !row->reversed_p)
5176 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5177 if (INTEGERP (last->object))
5178 row_width -= last->pixel_width;
5181 height += row->height;
5182 width = max (width, row_width);
5186 /* Add the frame's internal border to the width and height the X
5187 window should have. */
5188 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5189 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5191 /* Move the tooltip window where the mouse pointer is. Resize and
5192 show it. */
5193 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5195 BLOCK_INPUT;
5196 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5197 root_x, root_y, width, height);
5198 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5199 UNBLOCK_INPUT;
5201 /* Draw into the window. */
5202 w->must_be_updated_p = 1;
5203 update_single_window (w, 1);
5205 /* Restore original current buffer. */
5206 set_buffer_internal_1 (old_buffer);
5207 windows_or_buffers_changed = old_windows_or_buffers_changed;
5209 start_timer:
5210 /* Let the tip disappear after timeout seconds. */
5211 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5212 intern ("x-hide-tip"));
5214 UNGCPRO;
5215 return unbind_to (count, Qnil);
5219 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5220 doc: /* Hide the current tooltip window, if there is any.
5221 Value is t if tooltip was open, nil otherwise. */)
5222 (void)
5224 ptrdiff_t count;
5225 Lisp_Object deleted, frame, timer;
5226 struct gcpro gcpro1, gcpro2;
5228 /* Return quickly if nothing to do. */
5229 if (NILP (tip_timer) && NILP (tip_frame))
5230 return Qnil;
5232 frame = tip_frame;
5233 timer = tip_timer;
5234 GCPRO2 (frame, timer);
5235 tip_frame = tip_timer = deleted = Qnil;
5237 count = SPECPDL_INDEX ();
5238 specbind (Qinhibit_redisplay, Qt);
5239 specbind (Qinhibit_quit, Qt);
5241 if (!NILP (timer))
5242 call1 (Qcancel_timer, timer);
5244 #ifdef USE_GTK
5246 /* When using system tooltip, tip_frame is the Emacs frame on which
5247 the tip is shown. */
5248 struct frame *f = XFRAME (frame);
5249 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5250 frame = Qnil;
5252 #endif
5254 if (FRAMEP (frame))
5256 delete_frame (frame, Qnil);
5257 deleted = Qt;
5259 #ifdef USE_LUCID
5260 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5261 redisplay procedure is not called when a tip frame over menu
5262 items is unmapped. Redisplay the menu manually... */
5264 Widget w;
5265 struct frame *f = SELECTED_FRAME ();
5266 w = f->output_data.x->menubar_widget;
5268 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5269 && w != NULL)
5271 BLOCK_INPUT;
5272 xlwmenu_redisplay (w);
5273 UNBLOCK_INPUT;
5276 #endif /* USE_LUCID */
5279 UNGCPRO;
5280 return unbind_to (count, deleted);
5285 /***********************************************************************
5286 File selection dialog
5287 ***********************************************************************/
5289 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5290 Sx_uses_old_gtk_dialog,
5291 0, 0, 0,
5292 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5293 (void)
5295 #ifdef USE_GTK
5296 if (use_dialog_box
5297 && use_file_dialog
5298 && have_menus_p ()
5299 && xg_uses_old_file_dialog ())
5300 return Qt;
5301 #endif
5302 return Qnil;
5306 #ifdef USE_MOTIF
5307 /* Callback for "OK" and "Cancel" on file selection dialog. */
5309 static void
5310 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5312 int *result = (int *) client_data;
5313 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5314 *result = cb->reason;
5318 /* Callback for unmapping a file selection dialog. This is used to
5319 capture the case where a dialog is closed via a window manager's
5320 closer button, for example. Using a XmNdestroyCallback didn't work
5321 in this case. */
5323 static void
5324 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5326 int *result = (int *) client_data;
5327 *result = XmCR_CANCEL;
5330 static Lisp_Object
5331 clean_up_file_dialog (Lisp_Object arg)
5333 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
5334 Widget dialog = (Widget) p->pointer;
5336 /* Clean up. */
5337 BLOCK_INPUT;
5338 XtUnmanageChild (dialog);
5339 XtDestroyWidget (dialog);
5340 x_menu_set_in_use (0);
5341 UNBLOCK_INPUT;
5343 return Qnil;
5347 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5348 doc: /* Read file name, prompting with PROMPT in directory DIR.
5349 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5350 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5351 or directory must exist.
5353 This function is only defined on MS Windows, and X Windows with the
5354 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5355 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5356 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5358 int result;
5359 struct frame *f = SELECTED_FRAME ();
5360 Lisp_Object file = Qnil;
5361 Lisp_Object decoded_file;
5362 Widget dialog, text, help;
5363 Arg al[10];
5364 int ac = 0;
5365 XmString dir_xmstring, pattern_xmstring;
5366 ptrdiff_t count = SPECPDL_INDEX ();
5367 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5369 check_x ();
5371 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5373 if (popup_activated ())
5374 error ("Trying to use a menu from within a menu-entry");
5376 CHECK_STRING (prompt);
5377 CHECK_STRING (dir);
5379 /* Prevent redisplay. */
5380 specbind (Qinhibit_redisplay, Qt);
5382 BLOCK_INPUT;
5384 /* Create the dialog with PROMPT as title, using DIR as initial
5385 directory and using "*" as pattern. */
5386 dir = Fexpand_file_name (dir, Qnil);
5387 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5388 pattern_xmstring = XmStringCreateLocalized ("*");
5390 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5391 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5392 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5393 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5394 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5395 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5396 "fsb", al, ac);
5397 XmStringFree (dir_xmstring);
5398 XmStringFree (pattern_xmstring);
5400 /* Add callbacks for OK and Cancel. */
5401 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5402 (XtPointer) &result);
5403 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5404 (XtPointer) &result);
5405 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5406 (XtPointer) &result);
5408 /* Remove the help button since we can't display help. */
5409 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5410 XtUnmanageChild (help);
5412 /* Mark OK button as default. */
5413 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5414 XmNshowAsDefault, True, NULL);
5416 /* If MUSTMATCH is non-nil, disable the file entry field of the
5417 dialog, so that the user must select a file from the files list
5418 box. We can't remove it because we wouldn't have a way to get at
5419 the result file name, then. */
5420 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5421 if (!NILP (mustmatch))
5423 Widget label;
5424 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5425 XtSetSensitive (text, False);
5426 XtSetSensitive (label, False);
5429 /* Manage the dialog, so that list boxes get filled. */
5430 XtManageChild (dialog);
5432 if (STRINGP (default_filename))
5434 XmString default_xmstring;
5435 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5436 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5438 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5439 XmTextFieldReplace (wtext, 0, last_pos,
5440 (SSDATA (Ffile_name_nondirectory (default_filename))));
5442 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5443 must include the path for this to work. */
5445 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5447 if (XmListItemExists (list, default_xmstring))
5449 int item_pos = XmListItemPos (list, default_xmstring);
5450 /* Select the item and scroll it into view. */
5451 XmListSelectPos (list, item_pos, True);
5452 XmListSetPos (list, item_pos);
5455 XmStringFree (default_xmstring);
5458 record_unwind_protect (clean_up_file_dialog, make_save_value (dialog, 0));
5460 /* Process events until the user presses Cancel or OK. */
5461 x_menu_set_in_use (1);
5462 result = 0;
5463 while (result == 0)
5465 XEvent event;
5466 x_menu_wait_for_event (0);
5467 XtAppNextEvent (Xt_app_con, &event);
5468 if (event.type == KeyPress
5469 && FRAME_X_DISPLAY (f) == event.xkey.display)
5471 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5473 /* Pop down on C-g. */
5474 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5475 XtUnmanageChild (dialog);
5478 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5481 /* Get the result. */
5482 if (result == XmCR_OK)
5484 XmString text_string;
5485 String data;
5487 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5488 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5489 XmStringFree (text_string);
5490 file = build_string (data);
5491 XtFree (data);
5493 else
5494 file = Qnil;
5496 UNBLOCK_INPUT;
5497 UNGCPRO;
5499 /* Make "Cancel" equivalent to C-g. */
5500 if (NILP (file))
5501 Fsignal (Qquit, Qnil);
5503 decoded_file = DECODE_FILE (file);
5505 return unbind_to (count, decoded_file);
5508 #endif /* USE_MOTIF */
5510 #ifdef USE_GTK
5512 static Lisp_Object
5513 clean_up_dialog (Lisp_Object arg)
5515 x_menu_set_in_use (0);
5517 return Qnil;
5520 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5521 doc: /* Read file name, prompting with PROMPT in directory DIR.
5522 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5523 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5524 or directory must exist.
5526 This function is only defined on MS Windows, and X Windows with the
5527 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5528 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5529 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5531 FRAME_PTR f = SELECTED_FRAME ();
5532 char *fn;
5533 Lisp_Object file = Qnil;
5534 Lisp_Object decoded_file;
5535 ptrdiff_t count = SPECPDL_INDEX ();
5536 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5537 char *cdef_file;
5539 check_x ();
5541 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5543 if (popup_activated ())
5544 error ("Trying to use a menu from within a menu-entry");
5546 CHECK_STRING (prompt);
5547 CHECK_STRING (dir);
5549 /* Prevent redisplay. */
5550 specbind (Qinhibit_redisplay, Qt);
5551 record_unwind_protect (clean_up_dialog, Qnil);
5553 BLOCK_INPUT;
5555 if (STRINGP (default_filename))
5556 cdef_file = SSDATA (default_filename);
5557 else
5558 cdef_file = SSDATA (dir);
5560 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5561 ! NILP (mustmatch),
5562 ! NILP (only_dir_p));
5564 if (fn)
5566 file = build_string (fn);
5567 xfree (fn);
5570 UNBLOCK_INPUT;
5571 UNGCPRO;
5573 /* Make "Cancel" equivalent to C-g. */
5574 if (NILP (file))
5575 Fsignal (Qquit, Qnil);
5577 decoded_file = DECODE_FILE (file);
5579 return unbind_to (count, decoded_file);
5583 #ifdef HAVE_FREETYPE
5585 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5586 doc: /* Read a font name using a GTK font selection dialog.
5587 Return a GTK-style font string corresponding to the selection.
5589 If FRAME is omitted or nil, it defaults to the selected frame. */)
5590 (Lisp_Object frame, Lisp_Object ignored)
5592 FRAME_PTR f = check_x_frame (frame);
5593 char *name;
5594 Lisp_Object font;
5595 Lisp_Object font_param;
5596 char *default_name = NULL;
5597 struct gcpro gcpro1, gcpro2;
5598 ptrdiff_t count = SPECPDL_INDEX ();
5600 check_x ();
5602 if (popup_activated ())
5603 error ("Trying to use a menu from within a menu-entry");
5605 /* Prevent redisplay. */
5606 specbind (Qinhibit_redisplay, Qt);
5607 record_unwind_protect (clean_up_dialog, Qnil);
5609 BLOCK_INPUT;
5611 GCPRO2 (font_param, font);
5613 XSETFONT (font, FRAME_FONT (f));
5614 font_param = Ffont_get (font, intern (":name"));
5615 if (STRINGP (font_param))
5616 default_name = xstrdup (SSDATA (font_param));
5617 else
5619 font_param = Fframe_parameter (frame, Qfont_param);
5620 if (STRINGP (font_param))
5621 default_name = xstrdup (SSDATA (font_param));
5624 if (default_name == NULL && x_last_font_name != NULL)
5625 default_name = xstrdup (x_last_font_name);
5627 /* Convert fontconfig names to Gtk names, i.e. remove - before number */
5628 if (default_name)
5630 char *p = strrchr (default_name, '-');
5631 if (p)
5633 char *ep = p+1;
5634 while (isdigit (*ep))
5635 ++ep;
5636 if (*ep == '\0') *p = ' ';
5640 name = xg_get_font_name (f, default_name);
5641 xfree (default_name);
5643 if (name)
5645 font = build_string (name);
5646 g_free (x_last_font_name);
5647 x_last_font_name = name;
5650 UNBLOCK_INPUT;
5652 if (NILP (font))
5653 Fsignal (Qquit, Qnil);
5655 return unbind_to (count, font);
5657 #endif /* HAVE_FREETYPE */
5659 #endif /* USE_GTK */
5662 /***********************************************************************
5663 Keyboard
5664 ***********************************************************************/
5666 #ifdef HAVE_XKBGETKEYBOARD
5667 #include <X11/XKBlib.h>
5668 #include <X11/keysym.h>
5669 #endif
5671 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5672 Sx_backspace_delete_keys_p, 0, 1, 0,
5673 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5674 FRAME nil means use the selected frame.
5675 Value is t if we know that both keys are present, and are mapped to the
5676 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5677 present and mapped to the usual X keysyms. */)
5678 (Lisp_Object frame)
5680 #ifdef HAVE_XKBGETKEYBOARD
5681 XkbDescPtr kb;
5682 struct frame *f = check_x_frame (frame);
5683 Display *dpy = FRAME_X_DISPLAY (f);
5684 Lisp_Object have_keys;
5685 int major, minor, op, event, error_code;
5687 BLOCK_INPUT;
5689 /* Check library version in case we're dynamically linked. */
5690 major = XkbMajorVersion;
5691 minor = XkbMinorVersion;
5692 if (!XkbLibraryVersion (&major, &minor))
5694 UNBLOCK_INPUT;
5695 return Qlambda;
5698 /* Check that the server supports XKB. */
5699 major = XkbMajorVersion;
5700 minor = XkbMinorVersion;
5701 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
5703 UNBLOCK_INPUT;
5704 return Qlambda;
5707 /* In this code we check that the keyboard has physical keys with names
5708 that start with BKSP (Backspace) and DELE (Delete), and that they
5709 generate keysym XK_BackSpace and XK_Delete respectively.
5710 This function is used to test if normal-erase-is-backspace should be
5711 turned on.
5712 An alternative approach would be to just check if XK_BackSpace and
5713 XK_Delete are mapped to any key. But if any of those are mapped to
5714 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5715 user doesn't know about it, it is better to return false here.
5716 It is more obvious to the user what to do if she/he has two keys
5717 clearly marked with names/symbols and one key does something not
5718 expected (i.e. she/he then tries the other).
5719 The cases where Backspace/Delete is mapped to some other key combination
5720 are rare, and in those cases, normal-erase-is-backspace can be turned on
5721 manually. */
5723 have_keys = Qnil;
5724 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5725 if (kb)
5727 int delete_keycode = 0, backspace_keycode = 0, i;
5729 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5731 for (i = kb->min_key_code;
5732 (i < kb->max_key_code
5733 && (delete_keycode == 0 || backspace_keycode == 0));
5734 ++i)
5736 /* The XKB symbolic key names can be seen most easily in
5737 the PS file generated by `xkbprint -label name
5738 $DISPLAY'. */
5739 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5740 delete_keycode = i;
5741 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5742 backspace_keycode = i;
5745 XkbFreeNames (kb, 0, True);
5748 XkbFreeClientMap (kb, 0, True);
5750 if (delete_keycode
5751 && backspace_keycode
5752 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5753 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5754 have_keys = Qt;
5756 UNBLOCK_INPUT;
5757 return have_keys;
5758 #else /* not HAVE_XKBGETKEYBOARD */
5759 return Qlambda;
5760 #endif /* not HAVE_XKBGETKEYBOARD */
5765 /***********************************************************************
5766 Initialization
5767 ***********************************************************************/
5769 /* Keep this list in the same order as frame_parms in frame.c.
5770 Use 0 for unsupported frame parameters. */
5772 frame_parm_handler x_frame_parm_handlers[] =
5774 x_set_autoraise,
5775 x_set_autolower,
5776 x_set_background_color,
5777 x_set_border_color,
5778 x_set_border_width,
5779 x_set_cursor_color,
5780 x_set_cursor_type,
5781 x_set_font,
5782 x_set_foreground_color,
5783 x_set_icon_name,
5784 x_set_icon_type,
5785 x_set_internal_border_width,
5786 x_set_menu_bar_lines,
5787 x_set_mouse_color,
5788 x_explicitly_set_name,
5789 x_set_scroll_bar_width,
5790 x_set_title,
5791 x_set_unsplittable,
5792 x_set_vertical_scroll_bars,
5793 x_set_visibility,
5794 x_set_tool_bar_lines,
5795 x_set_scroll_bar_foreground,
5796 x_set_scroll_bar_background,
5797 x_set_screen_gamma,
5798 x_set_line_spacing,
5799 x_set_fringe_width,
5800 x_set_fringe_width,
5801 x_set_wait_for_wm,
5802 x_set_fullscreen,
5803 x_set_font_backend,
5804 x_set_alpha,
5805 x_set_sticky,
5806 x_set_tool_bar_position,
5809 void
5810 syms_of_xfns (void)
5812 /* This is zero if not using X windows. */
5813 x_in_use = 0;
5815 /* The section below is built by the lisp expression at the top of the file,
5816 just above where these variables are declared. */
5817 /*&&& init symbols here &&&*/
5818 DEFSYM (Qsuppress_icon, "suppress-icon");
5819 DEFSYM (Qundefined_color, "undefined-color");
5820 DEFSYM (Qcompound_text, "compound-text");
5821 DEFSYM (Qcancel_timer, "cancel-timer");
5822 DEFSYM (Qfont_param, "font-parameter");
5823 /* This is the end of symbol initialization. */
5825 Fput (Qundefined_color, Qerror_conditions,
5826 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
5827 Fput (Qundefined_color, Qerror_message,
5828 build_pure_c_string ("Undefined color"));
5830 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
5831 doc: /* The shape of the pointer when over text.
5832 Changing the value does not affect existing frames
5833 unless you set the mouse color. */);
5834 Vx_pointer_shape = Qnil;
5836 #if 0 /* This doesn't really do anything. */
5837 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
5838 doc: /* The shape of the pointer when not over text.
5839 This variable takes effect when you create a new frame
5840 or when you set the mouse color. */);
5841 #endif
5842 Vx_nontext_pointer_shape = Qnil;
5844 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
5845 doc: /* The shape of the pointer when Emacs is busy.
5846 This variable takes effect when you create a new frame
5847 or when you set the mouse color. */);
5848 Vx_hourglass_pointer_shape = Qnil;
5850 #if 0 /* This doesn't really do anything. */
5851 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
5852 doc: /* The shape of the pointer when over the mode line.
5853 This variable takes effect when you create a new frame
5854 or when you set the mouse color. */);
5855 #endif
5856 Vx_mode_pointer_shape = Qnil;
5858 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
5859 Vx_sensitive_text_pointer_shape,
5860 doc: /* The shape of the pointer when over mouse-sensitive text.
5861 This variable takes effect when you create a new frame
5862 or when you set the mouse color. */);
5863 Vx_sensitive_text_pointer_shape = Qnil;
5865 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
5866 Vx_window_horizontal_drag_shape,
5867 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
5868 This variable takes effect when you create a new frame
5869 or when you set the mouse color. */);
5870 Vx_window_horizontal_drag_shape = Qnil;
5872 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
5873 doc: /* A string indicating the foreground color of the cursor box. */);
5874 Vx_cursor_fore_pixel = Qnil;
5876 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
5877 doc: /* Maximum size for tooltips.
5878 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
5879 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
5881 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
5882 doc: /* Non-nil if no X window manager is in use.
5883 Emacs doesn't try to figure this out; this is always nil
5884 unless you set it to something else. */);
5885 /* We don't have any way to find this out, so set it to nil
5886 and maybe the user would like to set it to t. */
5887 Vx_no_window_manager = Qnil;
5889 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
5890 Vx_pixel_size_width_font_regexp,
5891 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
5893 Since Emacs gets width of a font matching with this regexp from
5894 PIXEL_SIZE field of the name, font finding mechanism gets faster for
5895 such a font. This is especially effective for such large fonts as
5896 Chinese, Japanese, and Korean. */);
5897 Vx_pixel_size_width_font_regexp = Qnil;
5899 /* This is not ifdef:ed, so other builds than GTK can customize it. */
5900 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
5901 doc: /* Non-nil means prompt with the old GTK file selection dialog.
5902 If nil or if the file selection dialog is not available, the new GTK file
5903 chooser is used instead. To turn off all file dialogs set the
5904 variable `use-file-dialog'. */);
5905 x_gtk_use_old_file_dialog = 0;
5907 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
5908 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
5909 Note that this is just the default, there is a toggle button on the file
5910 chooser to show or not show hidden files on a case by case basis. */);
5911 x_gtk_show_hidden_files = 0;
5913 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
5914 doc: /* If non-nil, the GTK file chooser will show additional help text.
5915 If more space for files in the file chooser dialog is wanted, set this to nil
5916 to turn the additional text off. */);
5917 x_gtk_file_dialog_help_text = 1;
5919 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
5920 doc: /* If non-nil, a detached tool bar is shown in full.
5921 The default is to just show an arrow and pressing on that arrow shows
5922 the tool bar buttons. */);
5923 x_gtk_whole_detached_tool_bar = 0;
5925 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
5926 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
5927 Otherwise use Emacs own tooltip implementation.
5928 When using Gtk+ tooltips, the tooltip face is not used. */);
5929 x_gtk_use_system_tooltips = 1;
5931 Fprovide (intern_c_string ("x"), Qnil);
5933 #ifdef USE_X_TOOLKIT
5934 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5935 #ifdef USE_MOTIF
5936 Fprovide (intern_c_string ("motif"), Qnil);
5938 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
5939 doc: /* Version info for LessTif/Motif. */);
5940 Vmotif_version_string = build_string (XmVERSION_STRING);
5941 #endif /* USE_MOTIF */
5942 #endif /* USE_X_TOOLKIT */
5944 #ifdef USE_GTK
5945 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
5946 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
5947 But for a user it is a toolkit for X, and indeed, configure
5948 accepts --with-x-toolkit=gtk. */
5949 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5950 Fprovide (intern_c_string ("gtk"), Qnil);
5951 Fprovide (intern_c_string ("move-toolbar"), Qnil);
5953 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
5954 doc: /* Version info for GTK+. */);
5956 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
5957 int len = sprintf (gtk_version, "%d.%d.%d",
5958 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
5959 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
5961 #endif /* USE_GTK */
5963 /* X window properties. */
5964 defsubr (&Sx_change_window_property);
5965 defsubr (&Sx_delete_window_property);
5966 defsubr (&Sx_window_property);
5968 defsubr (&Sxw_display_color_p);
5969 defsubr (&Sx_display_grayscale_p);
5970 defsubr (&Sxw_color_defined_p);
5971 defsubr (&Sxw_color_values);
5972 defsubr (&Sx_server_max_request_size);
5973 defsubr (&Sx_server_vendor);
5974 defsubr (&Sx_server_version);
5975 defsubr (&Sx_display_pixel_width);
5976 defsubr (&Sx_display_pixel_height);
5977 defsubr (&Sx_display_mm_width);
5978 defsubr (&Sx_display_mm_height);
5979 defsubr (&Sx_display_screens);
5980 defsubr (&Sx_display_planes);
5981 defsubr (&Sx_display_color_cells);
5982 defsubr (&Sx_display_visual_class);
5983 defsubr (&Sx_display_backing_store);
5984 defsubr (&Sx_display_save_under);
5985 defsubr (&Sx_wm_set_size_hint);
5986 defsubr (&Sx_create_frame);
5987 defsubr (&Sx_open_connection);
5988 defsubr (&Sx_close_connection);
5989 defsubr (&Sx_display_list);
5990 defsubr (&Sx_synchronize);
5991 defsubr (&Sx_focus_frame);
5992 defsubr (&Sx_backspace_delete_keys_p);
5994 /* Setting callback functions for fontset handler. */
5995 check_window_system_func = check_x;
5997 defsubr (&Sx_show_tip);
5998 defsubr (&Sx_hide_tip);
5999 tip_timer = Qnil;
6000 staticpro (&tip_timer);
6001 tip_frame = Qnil;
6002 staticpro (&tip_frame);
6004 last_show_tip_args = Qnil;
6005 staticpro (&last_show_tip_args);
6007 defsubr (&Sx_uses_old_gtk_dialog);
6008 #if defined (USE_MOTIF) || defined (USE_GTK)
6009 defsubr (&Sx_file_dialog);
6010 #endif
6012 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6013 defsubr (&Sx_select_font);
6014 x_last_font_name = NULL;
6015 #endif
6018 #endif /* HAVE_X_WINDOWS */