xfns.c (set_machine_and_pid_properties): Fix parantesis
[emacs.git] / src / xfns.c
blob4ec13b7e50dc195d0dad4d1688e32fd4fc64f64a
1 /* Functions for the X window system.
2 Copyright (C) 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <math.h>
24 #include <setjmp.h>
25 #include <ctype.h>
27 #ifdef HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
31 /* This makes the fields of a Display accessible, in Xlib header files. */
33 #define XLIB_ILLEGAL_ACCESS
35 #include "lisp.h"
36 #include "xterm.h"
37 #include "frame.h"
38 #include "window.h"
39 #include "buffer.h"
40 #include "intervals.h"
41 #include "dispextern.h"
42 #include "keyboard.h"
43 #include "blockinput.h"
44 #include <epaths.h>
45 #include "character.h"
46 #include "charset.h"
47 #include "coding.h"
48 #include "fontset.h"
49 #include "systime.h"
50 #include "termhooks.h"
51 #include "atimer.h"
52 #include "termchar.h"
53 #include "font.h"
55 #ifdef HAVE_X_WINDOWS
57 #include <ctype.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
61 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
62 #include "bitmaps/gray.xbm"
63 #else
64 #include <X11/bitmaps/gray>
65 #endif
67 #include "xsettings.h"
69 #ifdef USE_GTK
70 #include "gtkutil.h"
71 #endif
73 #ifdef USE_X_TOOLKIT
74 #include <X11/Shell.h>
76 #ifndef USE_MOTIF
77 #ifdef HAVE_XAW3D
78 #include <X11/Xaw3d/Paned.h>
79 #include <X11/Xaw3d/Label.h>
80 #else /* !HAVE_XAW3D */
81 #include <X11/Xaw/Paned.h>
82 #include <X11/Xaw/Label.h>
83 #endif /* HAVE_XAW3D */
84 #endif /* USE_MOTIF */
86 #ifdef USG
87 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
88 #include <X11/Xos.h>
89 #define USG
90 #else
91 #include <X11/Xos.h>
92 #endif
94 #include "widget.h"
96 #include "../lwlib/lwlib.h"
98 #ifdef USE_MOTIF
99 #include <Xm/Xm.h>
100 #include <Xm/DialogS.h>
101 #include <Xm/FileSB.h>
102 #endif
104 #if !defined(NO_EDITRES)
105 #define HACK_EDITRES
106 extern void _XEditResCheckMessages ();
107 #endif /* not defined NO_EDITRES */
109 /* Unique id counter for widgets created by the Lucid Widget Library. */
111 extern LWLIB_ID widget_id_tick;
113 #ifdef USE_LUCID
114 /* This is part of a kludge--see lwlib/xlwmenu.c. */
115 extern XFontStruct *xlwmenu_default_font;
116 #endif
118 extern void free_frame_menubar ();
119 extern double atof ();
121 #ifdef USE_MOTIF
123 /* LessTif/Motif version info. */
125 static Lisp_Object Vmotif_version_string;
127 #endif /* USE_MOTIF */
129 #endif /* USE_X_TOOLKIT */
131 #ifdef USE_GTK
133 /* GTK+ version info */
135 static Lisp_Object Vgtk_version_string;
137 #endif /* USE_GTK */
139 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
141 /* The gray bitmap `bitmaps/gray'. This is done because xterm.c uses
142 it, and including `bitmaps/gray' more than once is a problem when
143 config.h defines `static' as an empty replacement string. */
145 int gray_bitmap_width = gray_width;
146 int gray_bitmap_height = gray_height;
147 char *gray_bitmap_bits = gray_bits;
149 /* Non-zero means prompt with the old GTK file selection dialog. */
151 int x_gtk_use_old_file_dialog;
153 /* If non-zero, by default show hidden files in the GTK file chooser. */
155 int x_gtk_show_hidden_files;
157 /* If non-zero, don't show additional help text in the GTK file chooser. */
159 int x_gtk_file_dialog_help_text;
161 /* If non-zero, don't collapse to tool bar when it is detached. */
163 int x_gtk_whole_detached_tool_bar;
165 /* The background and shape of the mouse pointer, and shape when not
166 over text or in the modeline. */
168 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
169 Lisp_Object Vx_hourglass_pointer_shape;
171 /* The shape when over mouse-sensitive text. */
173 Lisp_Object Vx_sensitive_text_pointer_shape;
175 /* If non-nil, the pointer shape to indicate that windows can be
176 dragged horizontally. */
178 Lisp_Object Vx_window_horizontal_drag_shape;
180 /* Color of chars displayed in cursor box. */
182 Lisp_Object Vx_cursor_fore_pixel;
184 /* Nonzero if using X. */
186 static int x_in_use;
188 /* Non nil if no window manager is in use. */
190 Lisp_Object Vx_no_window_manager;
192 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
194 Lisp_Object Vx_pixel_size_width_font_regexp;
196 Lisp_Object Qnone;
197 Lisp_Object Qsuppress_icon;
198 Lisp_Object Qundefined_color;
199 Lisp_Object Qcompound_text, Qcancel_timer;
200 Lisp_Object Qfont_param;
202 /* In dispnew.c */
204 extern Lisp_Object Vwindow_system_version;
206 /* In editfns.c */
208 extern Lisp_Object Vsystem_name;
210 /* The below are defined in frame.c. */
212 #if GLYPH_DEBUG
213 int image_cache_refcount, dpyinfo_refcount;
214 #endif
216 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
217 char *x_last_font_name;
218 #endif
221 /* Error if we are not connected to X. */
223 void
224 check_x ()
226 if (! x_in_use)
227 error ("X windows are not in use or not initialized");
230 /* Nonzero if we can use mouse menus.
231 You should not call this unless HAVE_MENUS is defined. */
234 have_menus_p ()
236 return x_in_use;
239 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
240 and checking validity for X. */
242 FRAME_PTR
243 check_x_frame (frame)
244 Lisp_Object frame;
246 FRAME_PTR f;
248 if (NILP (frame))
249 frame = selected_frame;
250 CHECK_LIVE_FRAME (frame);
251 f = XFRAME (frame);
252 if (! FRAME_X_P (f))
253 error ("Non-X frame used");
254 return f;
257 /* Let the user specify an X display with a Lisp object.
258 OBJECT may be nil, a frame or a terminal object.
259 nil stands for the selected frame--or, if that is not an X frame,
260 the first X display on the list. */
262 struct x_display_info *
263 check_x_display_info (object)
264 Lisp_Object object;
266 struct x_display_info *dpyinfo = NULL;
268 if (NILP (object))
270 struct frame *sf = XFRAME (selected_frame);
272 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
273 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
274 else if (x_display_list != 0)
275 dpyinfo = x_display_list;
276 else
277 error ("X windows are not in use or not initialized");
279 else if (TERMINALP (object))
281 struct terminal *t = get_terminal (object, 1);
283 if (t->type != output_x_window)
284 error ("Terminal %d is not an X display", XINT (object));
286 dpyinfo = t->display_info.x;
288 else if (STRINGP (object))
289 dpyinfo = x_display_info_for_name (object);
290 else
292 FRAME_PTR f = check_x_frame (object);
293 dpyinfo = FRAME_X_DISPLAY_INFO (f);
296 return dpyinfo;
300 /* Return the Emacs frame-object corresponding to an X window.
301 It could be the frame's main window or an icon window. */
303 /* This function can be called during GC, so use GC_xxx type test macros. */
305 struct frame *
306 x_window_to_frame (dpyinfo, wdesc)
307 struct x_display_info *dpyinfo;
308 int wdesc;
310 Lisp_Object tail, frame;
311 struct frame *f;
313 if (wdesc == None) return 0;
315 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
317 frame = XCAR (tail);
318 if (!FRAMEP (frame))
319 continue;
320 f = XFRAME (frame);
321 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
322 continue;
323 if (f->output_data.x->hourglass_window == wdesc)
324 return f;
325 #ifdef USE_X_TOOLKIT
326 if ((f->output_data.x->edit_widget
327 && XtWindow (f->output_data.x->edit_widget) == wdesc)
328 /* A tooltip frame? */
329 || (!f->output_data.x->edit_widget
330 && FRAME_X_WINDOW (f) == wdesc)
331 || f->output_data.x->icon_desc == wdesc)
332 return f;
333 #else /* not USE_X_TOOLKIT */
334 #ifdef USE_GTK
335 if (f->output_data.x->edit_widget)
337 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
338 struct x_output *x = f->output_data.x;
339 if (gwdesc != 0 && gwdesc == x->edit_widget)
340 return f;
342 #endif /* USE_GTK */
343 if (FRAME_X_WINDOW (f) == wdesc
344 || f->output_data.x->icon_desc == wdesc)
345 return f;
346 #endif /* not USE_X_TOOLKIT */
348 return 0;
351 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
352 /* Like x_window_to_frame but also compares the window with the widget's
353 windows. */
355 struct frame *
356 x_any_window_to_frame (dpyinfo, wdesc)
357 struct x_display_info *dpyinfo;
358 int wdesc;
360 Lisp_Object tail, frame;
361 struct frame *f, *found;
362 struct x_output *x;
364 if (wdesc == None) return NULL;
366 found = NULL;
367 for (tail = Vframe_list; CONSP (tail) && !found; tail = XCDR (tail))
369 frame = XCAR (tail);
370 if (!FRAMEP (frame))
371 continue;
373 f = XFRAME (frame);
374 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
376 /* This frame matches if the window is any of its widgets. */
377 x = f->output_data.x;
378 if (x->hourglass_window == wdesc)
379 found = f;
380 else if (x->widget)
382 #ifdef USE_GTK
383 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
384 if (gwdesc != 0
385 && gtk_widget_get_toplevel (gwdesc) == x->widget)
386 found = f;
387 #else
388 if (wdesc == XtWindow (x->widget)
389 || wdesc == XtWindow (x->column_widget)
390 || wdesc == XtWindow (x->edit_widget))
391 found = f;
392 /* Match if the window is this frame's menubar. */
393 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
394 found = f;
395 #endif
397 else if (FRAME_X_WINDOW (f) == wdesc)
398 /* A tooltip frame. */
399 found = f;
403 return found;
406 /* Likewise, but consider only the menu bar widget. */
408 struct frame *
409 x_menubar_window_to_frame (dpyinfo, wdesc)
410 struct x_display_info *dpyinfo;
411 int wdesc;
413 Lisp_Object tail, frame;
414 struct frame *f;
415 struct x_output *x;
417 if (wdesc == None) return 0;
419 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
421 frame = XCAR (tail);
422 if (!FRAMEP (frame))
423 continue;
424 f = XFRAME (frame);
425 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
426 continue;
427 x = f->output_data.x;
428 /* Match if the window is this frame's menubar. */
429 #ifdef USE_GTK
430 if (x->menubar_widget)
432 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
434 /* This gives false positives, but the rectangle check in xterm.c
435 where this is called takes care of that. */
436 if (gwdesc != 0
437 && (gwdesc == x->menubar_widget
438 || gtk_widget_is_ancestor (x->menubar_widget, gwdesc)
439 || gtk_widget_is_ancestor (gwdesc, x->menubar_widget)))
440 return f;
442 #else
443 if (x->menubar_widget
444 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
445 return f;
446 #endif
448 return 0;
451 /* Return the frame whose principal (outermost) window is WDESC.
452 If WDESC is some other (smaller) window, we return 0. */
454 struct frame *
455 x_top_window_to_frame (dpyinfo, wdesc)
456 struct x_display_info *dpyinfo;
457 int wdesc;
459 Lisp_Object tail, frame;
460 struct frame *f;
461 struct x_output *x;
463 if (wdesc == None) return 0;
465 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
467 frame = XCAR (tail);
468 if (!FRAMEP (frame))
469 continue;
470 f = XFRAME (frame);
471 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
472 continue;
473 x = f->output_data.x;
475 if (x->widget)
477 /* This frame matches if the window is its topmost widget. */
478 #ifdef USE_GTK
479 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
480 if (gwdesc == x->widget)
481 return f;
482 #else
483 if (wdesc == XtWindow (x->widget))
484 return f;
485 #if 0 /* I don't know why it did this,
486 but it seems logically wrong,
487 and it causes trouble for MapNotify events. */
488 /* Match if the window is this frame's menubar. */
489 if (x->menubar_widget
490 && wdesc == XtWindow (x->menubar_widget))
491 return f;
492 #endif
493 #endif
495 else if (FRAME_X_WINDOW (f) == wdesc)
496 /* Tooltip frame. */
497 return f;
499 return 0;
501 #endif /* USE_X_TOOLKIT || USE_GTK */
505 static void x_default_font_parameter P_ ((struct frame *, Lisp_Object));
507 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
508 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
510 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
511 static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
512 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
513 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
514 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
515 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
516 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
517 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
518 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
519 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
520 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
521 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
522 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
523 void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
524 Lisp_Object));
525 void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
526 Lisp_Object));
527 static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
528 Lisp_Object,
529 Lisp_Object,
530 char *, char *,
531 int));
534 /* Store the screen positions of frame F into XPTR and YPTR.
535 These are the positions of the containing window manager window,
536 not Emacs's own window. */
538 void
539 x_real_positions (f, xptr, yptr)
540 FRAME_PTR f;
541 int *xptr, *yptr;
543 int win_x, win_y, outer_x, outer_y;
544 int real_x = 0, real_y = 0;
545 int had_errors = 0;
546 Window win = f->output_data.x->parent_desc;
548 BLOCK_INPUT;
550 x_catch_errors (FRAME_X_DISPLAY (f));
552 if (win == FRAME_X_DISPLAY_INFO (f)->root_window)
553 win = FRAME_OUTER_WINDOW (f);
555 /* This loop traverses up the containment tree until we hit the root
556 window. Window managers may intersect many windows between our window
557 and the root window. The window we find just before the root window
558 should be the outer WM window. */
559 for (;;)
561 Window wm_window, rootw;
562 Window *tmp_children;
563 unsigned int tmp_nchildren;
564 int success;
566 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
567 &wm_window, &tmp_children, &tmp_nchildren);
569 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
571 /* Don't free tmp_children if XQueryTree failed. */
572 if (! success)
573 break;
575 XFree ((char *) tmp_children);
577 if (wm_window == rootw || had_errors)
578 break;
580 win = wm_window;
583 if (! had_errors)
585 unsigned int ign;
586 Window child, rootw;
588 /* Get the real coordinates for the WM window upper left corner */
589 XGetGeometry (FRAME_X_DISPLAY (f), win,
590 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
592 /* Translate real coordinates to coordinates relative to our
593 window. For our window, the upper left corner is 0, 0.
594 Since the upper left corner of the WM window is outside
595 our window, win_x and win_y will be negative:
597 ------------------ ---> x
598 | title |
599 | ----------------- v y
600 | | our window
602 XTranslateCoordinates (FRAME_X_DISPLAY (f),
604 /* From-window, to-window. */
605 FRAME_X_DISPLAY_INFO (f)->root_window,
606 FRAME_X_WINDOW (f),
608 /* From-position, to-position. */
609 real_x, real_y, &win_x, &win_y,
611 /* Child of win. */
612 &child);
614 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
616 outer_x = win_x;
617 outer_y = win_y;
619 else
621 XTranslateCoordinates (FRAME_X_DISPLAY (f),
623 /* From-window, to-window. */
624 FRAME_X_DISPLAY_INFO (f)->root_window,
625 FRAME_OUTER_WINDOW (f),
627 /* From-position, to-position. */
628 real_x, real_y, &outer_x, &outer_y,
630 /* Child of win. */
631 &child);
634 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
637 x_uncatch_errors ();
639 UNBLOCK_INPUT;
641 if (had_errors) return;
643 f->x_pixels_diff = -win_x;
644 f->y_pixels_diff = -win_y;
646 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
647 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
649 *xptr = real_x;
650 *yptr = real_y;
656 /* Gamma-correct COLOR on frame F. */
658 void
659 gamma_correct (f, color)
660 struct frame *f;
661 XColor *color;
663 if (f->gamma)
665 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
666 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
667 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
672 /* Decide if color named COLOR_NAME is valid for use on frame F. If
673 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
674 allocate the color. Value is zero if COLOR_NAME is invalid, or
675 no color could be allocated. */
678 x_defined_color (f, color_name, color, alloc_p)
679 struct frame *f;
680 char *color_name;
681 XColor *color;
682 int alloc_p;
684 int success_p;
685 Display *dpy = FRAME_X_DISPLAY (f);
686 Colormap cmap = FRAME_X_COLORMAP (f);
688 BLOCK_INPUT;
689 success_p = XParseColor (dpy, cmap, color_name, color);
690 if (success_p && alloc_p)
691 success_p = x_alloc_nearest_color (f, cmap, color);
692 UNBLOCK_INPUT;
694 return success_p;
698 /* Return the pixel color value for color COLOR_NAME on frame F. If F
699 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
700 Signal an error if color can't be allocated. */
703 x_decode_color (f, color_name, mono_color)
704 FRAME_PTR f;
705 Lisp_Object color_name;
706 int mono_color;
708 XColor cdef;
710 CHECK_STRING (color_name);
712 #if 0 /* Don't do this. It's wrong when we're not using the default
713 colormap, it makes freeing difficult, and it's probably not
714 an important optimization. */
715 if (strcmp (SDATA (color_name), "black") == 0)
716 return BLACK_PIX_DEFAULT (f);
717 else if (strcmp (SDATA (color_name), "white") == 0)
718 return WHITE_PIX_DEFAULT (f);
719 #endif
721 /* Return MONO_COLOR for monochrome frames. */
722 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
723 return mono_color;
725 /* x_defined_color is responsible for coping with failures
726 by looking for a near-miss. */
727 if (x_defined_color (f, SDATA (color_name), &cdef, 1))
728 return cdef.pixel;
730 signal_error ("Undefined color", color_name);
735 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
736 the previous value of that parameter, NEW_VALUE is the new value.
737 See also the comment of wait_for_wm in struct x_output. */
739 static void
740 x_set_wait_for_wm (f, new_value, old_value)
741 struct frame *f;
742 Lisp_Object new_value, old_value;
744 f->output_data.x->wait_for_wm = !NILP (new_value);
747 #ifdef USE_GTK
749 /* Set icon from FILE for frame F. By using GTK functions the icon
750 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
753 xg_set_icon (f, file)
754 FRAME_PTR f;
755 Lisp_Object file;
757 int result = 0;
758 Lisp_Object found;
760 found = x_find_image_file (file);
762 if (! NILP (found))
764 GdkPixbuf *pixbuf;
765 GError *err = NULL;
766 char *filename = (char *) SDATA (found);
767 BLOCK_INPUT;
769 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
771 if (pixbuf)
773 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
774 pixbuf);
775 g_object_unref (pixbuf);
777 result = 1;
779 else
780 g_error_free (err);
782 UNBLOCK_INPUT;
785 return result;
789 xg_set_icon_from_xpm_data (f, data)
790 FRAME_PTR f;
791 char **data;
793 int result = 0;
794 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
796 if (!pixbuf)
797 return 0;
799 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
800 g_object_unref (pixbuf);
801 return 1;
803 #endif /* USE_GTK */
806 /* Functions called only from `x_set_frame_param'
807 to set individual parameters.
809 If FRAME_X_WINDOW (f) is 0,
810 the frame is being created and its X-window does not exist yet.
811 In that case, just record the parameter's new value
812 in the standard place; do not attempt to change the window. */
814 void
815 x_set_foreground_color (f, arg, oldval)
816 struct frame *f;
817 Lisp_Object arg, oldval;
819 struct x_output *x = f->output_data.x;
820 unsigned long fg, old_fg;
822 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
823 old_fg = FRAME_FOREGROUND_PIXEL (f);
824 FRAME_FOREGROUND_PIXEL (f) = fg;
826 if (FRAME_X_WINDOW (f) != 0)
828 Display *dpy = FRAME_X_DISPLAY (f);
830 BLOCK_INPUT;
831 XSetForeground (dpy, x->normal_gc, fg);
832 XSetBackground (dpy, x->reverse_gc, fg);
834 if (x->cursor_pixel == old_fg)
836 unload_color (f, x->cursor_pixel);
837 x->cursor_pixel = x_copy_color (f, fg);
838 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
841 UNBLOCK_INPUT;
843 update_face_from_frame_parameter (f, Qforeground_color, arg);
845 if (FRAME_VISIBLE_P (f))
846 redraw_frame (f);
849 unload_color (f, old_fg);
852 void
853 x_set_background_color (f, arg, oldval)
854 struct frame *f;
855 Lisp_Object arg, oldval;
857 struct x_output *x = f->output_data.x;
858 unsigned long bg;
860 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
861 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
862 FRAME_BACKGROUND_PIXEL (f) = bg;
864 if (FRAME_X_WINDOW (f) != 0)
866 Display *dpy = FRAME_X_DISPLAY (f);
868 BLOCK_INPUT;
869 XSetBackground (dpy, x->normal_gc, bg);
870 XSetForeground (dpy, x->reverse_gc, bg);
871 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
872 XSetForeground (dpy, x->cursor_gc, bg);
874 #ifdef USE_GTK
875 xg_set_background_color (f, bg);
876 #endif
878 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
879 toolkit scroll bars. */
881 Lisp_Object bar;
882 for (bar = FRAME_SCROLL_BARS (f);
883 !NILP (bar);
884 bar = XSCROLL_BAR (bar)->next)
886 Window window = XSCROLL_BAR (bar)->x_window;
887 XSetWindowBackground (dpy, window, bg);
890 #endif /* USE_TOOLKIT_SCROLL_BARS */
892 UNBLOCK_INPUT;
893 update_face_from_frame_parameter (f, Qbackground_color, arg);
895 if (FRAME_VISIBLE_P (f))
896 redraw_frame (f);
900 static Cursor
901 make_invisible_cursor (f)
902 struct frame *f;
904 Display *dpy = FRAME_X_DISPLAY (f);
905 static char const no_data[] = { 0 };
906 Pixmap pix;
907 XColor col;
908 Cursor c;
910 x_catch_errors (dpy);
911 pix = XCreateBitmapFromData (dpy, FRAME_X_DISPLAY_INFO (f)->root_window,
912 no_data, 1, 1);
913 if (! x_had_errors_p (dpy) && pix != None)
915 col.pixel = 0;
916 col.red = col.green = col.blue = 0;
917 col.flags = DoRed | DoGreen | DoBlue;
918 c = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
919 if (x_had_errors_p (dpy) || c == None)
920 c = 0;
921 XFreePixmap (dpy, pix);
924 x_uncatch_errors ();
926 return c;
929 void
930 x_set_mouse_color (f, arg, oldval)
931 struct frame *f;
932 Lisp_Object arg, oldval;
934 struct x_output *x = f->output_data.x;
935 Display *dpy = FRAME_X_DISPLAY (f);
936 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
937 Cursor hourglass_cursor, horizontal_drag_cursor;
938 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
939 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
941 /* Don't let pointers be invisible. */
942 if (mask_color == pixel)
944 x_free_colors (f, &pixel, 1);
945 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
948 unload_color (f, x->mouse_pixel);
949 x->mouse_pixel = pixel;
951 BLOCK_INPUT;
953 /* It's not okay to crash if the user selects a screwy cursor. */
954 x_catch_errors (dpy);
956 if (!NILP (Vx_pointer_shape))
958 CHECK_NUMBER (Vx_pointer_shape);
959 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
961 else
962 cursor = XCreateFontCursor (dpy, XC_xterm);
963 x_check_errors (dpy, "bad text pointer cursor: %s");
965 if (!NILP (Vx_nontext_pointer_shape))
967 CHECK_NUMBER (Vx_nontext_pointer_shape);
968 nontext_cursor
969 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
971 else
972 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
973 x_check_errors (dpy, "bad nontext pointer cursor: %s");
975 if (!NILP (Vx_hourglass_pointer_shape))
977 CHECK_NUMBER (Vx_hourglass_pointer_shape);
978 hourglass_cursor
979 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
981 else
982 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
983 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
985 if (!NILP (Vx_mode_pointer_shape))
987 CHECK_NUMBER (Vx_mode_pointer_shape);
988 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
990 else
991 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
992 x_check_errors (dpy, "bad modeline pointer cursor: %s");
994 if (!NILP (Vx_sensitive_text_pointer_shape))
996 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
997 hand_cursor
998 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
1000 else
1001 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
1003 if (!NILP (Vx_window_horizontal_drag_shape))
1005 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1006 horizontal_drag_cursor
1007 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
1009 else
1010 horizontal_drag_cursor
1011 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
1013 /* Check and report errors with the above calls. */
1014 x_check_errors (dpy, "can't set cursor shape: %s");
1015 x_uncatch_errors ();
1018 XColor fore_color, back_color;
1020 fore_color.pixel = x->mouse_pixel;
1021 x_query_color (f, &fore_color);
1022 back_color.pixel = mask_color;
1023 x_query_color (f, &back_color);
1025 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
1026 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
1027 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
1028 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
1029 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
1030 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
1033 if (FRAME_X_WINDOW (f) != 0)
1034 XDefineCursor (dpy, FRAME_X_WINDOW (f),
1035 f->output_data.x->current_cursor = cursor);
1037 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor == 0)
1038 FRAME_X_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
1040 if (cursor != x->text_cursor
1041 && x->text_cursor != 0)
1042 XFreeCursor (dpy, x->text_cursor);
1043 x->text_cursor = cursor;
1045 if (nontext_cursor != x->nontext_cursor
1046 && x->nontext_cursor != 0)
1047 XFreeCursor (dpy, x->nontext_cursor);
1048 x->nontext_cursor = nontext_cursor;
1050 if (hourglass_cursor != x->hourglass_cursor
1051 && x->hourglass_cursor != 0)
1052 XFreeCursor (dpy, x->hourglass_cursor);
1053 x->hourglass_cursor = hourglass_cursor;
1055 if (mode_cursor != x->modeline_cursor
1056 && x->modeline_cursor != 0)
1057 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
1058 x->modeline_cursor = mode_cursor;
1060 if (hand_cursor != x->hand_cursor
1061 && x->hand_cursor != 0)
1062 XFreeCursor (dpy, x->hand_cursor);
1063 x->hand_cursor = hand_cursor;
1065 if (horizontal_drag_cursor != x->horizontal_drag_cursor
1066 && x->horizontal_drag_cursor != 0)
1067 XFreeCursor (dpy, x->horizontal_drag_cursor);
1068 x->horizontal_drag_cursor = horizontal_drag_cursor;
1070 XFlush (dpy);
1071 UNBLOCK_INPUT;
1073 update_face_from_frame_parameter (f, Qmouse_color, arg);
1076 void
1077 x_set_cursor_color (f, arg, oldval)
1078 struct frame *f;
1079 Lisp_Object arg, oldval;
1081 unsigned long fore_pixel, pixel;
1082 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
1083 struct x_output *x = f->output_data.x;
1085 if (!NILP (Vx_cursor_fore_pixel))
1087 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1088 WHITE_PIX_DEFAULT (f));
1089 fore_pixel_allocated_p = 1;
1091 else
1092 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1094 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1095 pixel_allocated_p = 1;
1097 /* Make sure that the cursor color differs from the background color. */
1098 if (pixel == FRAME_BACKGROUND_PIXEL (f))
1100 if (pixel_allocated_p)
1102 x_free_colors (f, &pixel, 1);
1103 pixel_allocated_p = 0;
1106 pixel = x->mouse_pixel;
1107 if (pixel == fore_pixel)
1109 if (fore_pixel_allocated_p)
1111 x_free_colors (f, &fore_pixel, 1);
1112 fore_pixel_allocated_p = 0;
1114 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
1118 unload_color (f, x->cursor_foreground_pixel);
1119 if (!fore_pixel_allocated_p)
1120 fore_pixel = x_copy_color (f, fore_pixel);
1121 x->cursor_foreground_pixel = fore_pixel;
1123 unload_color (f, x->cursor_pixel);
1124 if (!pixel_allocated_p)
1125 pixel = x_copy_color (f, pixel);
1126 x->cursor_pixel = pixel;
1128 if (FRAME_X_WINDOW (f) != 0)
1130 BLOCK_INPUT;
1131 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1132 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
1133 UNBLOCK_INPUT;
1135 if (FRAME_VISIBLE_P (f))
1137 x_update_cursor (f, 0);
1138 x_update_cursor (f, 1);
1142 update_face_from_frame_parameter (f, Qcursor_color, arg);
1145 /* Set the border-color of frame F to pixel value PIX.
1146 Note that this does not fully take effect if done before
1147 F has an x-window. */
1149 void
1150 x_set_border_pixel (f, pix)
1151 struct frame *f;
1152 int pix;
1154 unload_color (f, f->output_data.x->border_pixel);
1155 f->output_data.x->border_pixel = pix;
1157 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
1159 BLOCK_INPUT;
1160 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1161 (unsigned long)pix);
1162 UNBLOCK_INPUT;
1164 if (FRAME_VISIBLE_P (f))
1165 redraw_frame (f);
1169 /* Set the border-color of frame F to value described by ARG.
1170 ARG can be a string naming a color.
1171 The border-color is used for the border that is drawn by the X server.
1172 Note that this does not fully take effect if done before
1173 F has an x-window; it must be redone when the window is created.
1175 Note: this is done in two routines because of the way X10 works.
1177 Note: under X11, this is normally the province of the window manager,
1178 and so emacs' border colors may be overridden. */
1180 void
1181 x_set_border_color (f, arg, oldval)
1182 struct frame *f;
1183 Lisp_Object arg, oldval;
1185 int pix;
1187 CHECK_STRING (arg);
1188 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1189 x_set_border_pixel (f, pix);
1190 update_face_from_frame_parameter (f, Qborder_color, arg);
1194 void
1195 x_set_cursor_type (f, arg, oldval)
1196 FRAME_PTR f;
1197 Lisp_Object arg, oldval;
1199 set_frame_cursor_types (f, arg);
1201 /* Make sure the cursor gets redrawn. */
1202 cursor_type_changed = 1;
1205 void
1206 x_set_icon_type (f, arg, oldval)
1207 struct frame *f;
1208 Lisp_Object arg, oldval;
1210 int result;
1212 if (STRINGP (arg))
1214 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1215 return;
1217 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1218 return;
1220 BLOCK_INPUT;
1221 if (NILP (arg))
1222 result = x_text_icon (f,
1223 (char *) SDATA ((!NILP (f->icon_name)
1224 ? f->icon_name
1225 : f->name)));
1226 else
1227 result = x_bitmap_icon (f, arg);
1229 if (result)
1231 UNBLOCK_INPUT;
1232 error ("No icon window available");
1235 XFlush (FRAME_X_DISPLAY (f));
1236 UNBLOCK_INPUT;
1239 void
1240 x_set_icon_name (f, arg, oldval)
1241 struct frame *f;
1242 Lisp_Object arg, oldval;
1244 int result;
1246 if (STRINGP (arg))
1248 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1249 return;
1251 else if (!NILP (arg) || NILP (oldval))
1252 return;
1254 f->icon_name = arg;
1256 if (f->output_data.x->icon_bitmap != 0)
1257 return;
1259 BLOCK_INPUT;
1261 result = x_text_icon (f,
1262 (char *) SDATA ((!NILP (f->icon_name)
1263 ? f->icon_name
1264 : !NILP (f->title)
1265 ? f->title
1266 : f->name)));
1268 if (result)
1270 UNBLOCK_INPUT;
1271 error ("No icon window available");
1274 XFlush (FRAME_X_DISPLAY (f));
1275 UNBLOCK_INPUT;
1279 void
1280 x_set_menu_bar_lines (f, value, oldval)
1281 struct frame *f;
1282 Lisp_Object value, oldval;
1284 int nlines;
1285 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1286 int olines = FRAME_MENU_BAR_LINES (f);
1287 #endif
1289 /* Right now, menu bars don't work properly in minibuf-only frames;
1290 most of the commands try to apply themselves to the minibuffer
1291 frame itself, and get an error because you can't switch buffers
1292 in or split the minibuffer window. */
1293 if (FRAME_MINIBUF_ONLY_P (f))
1294 return;
1296 if (INTEGERP (value))
1297 nlines = XINT (value);
1298 else
1299 nlines = 0;
1301 /* Make sure we redisplay all windows in this frame. */
1302 windows_or_buffers_changed++;
1304 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1305 FRAME_MENU_BAR_LINES (f) = 0;
1306 if (nlines)
1308 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1309 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1310 /* Make sure next redisplay shows the menu bar. */
1311 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
1313 else
1315 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1316 free_frame_menubar (f);
1317 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1318 if (FRAME_X_P (f))
1319 f->output_data.x->menubar_widget = 0;
1321 #else /* not USE_X_TOOLKIT && not USE_GTK */
1322 FRAME_MENU_BAR_LINES (f) = nlines;
1323 change_window_heights (f->root_window, nlines - olines);
1325 /* If the menu bar height gets changed, the internal border below
1326 the top margin has to be cleared. Also, if the menu bar gets
1327 larger, the area for the added lines has to be cleared except for
1328 the first menu bar line that is to be drawn later. */
1329 if (nlines != olines)
1331 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1332 int width = FRAME_PIXEL_WIDTH (f);
1333 int y;
1335 /* height can be zero here. */
1336 if (height > 0 && width > 0)
1338 y = FRAME_TOP_MARGIN_HEIGHT (f);
1340 BLOCK_INPUT;
1341 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1342 0, y, width, height, False);
1343 UNBLOCK_INPUT;
1346 if (nlines > 1 && nlines > olines)
1348 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1349 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1351 BLOCK_INPUT;
1352 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1353 0, y, width, height, False);
1354 UNBLOCK_INPUT;
1357 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1358 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1360 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1361 adjust_glyphs (f);
1365 /* Set the number of lines used for the tool bar of frame F to VALUE.
1366 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1367 is the old number of tool bar lines. This function changes the
1368 height of all windows on frame F to match the new tool bar height.
1369 The frame's height doesn't change. */
1371 void
1372 x_set_tool_bar_lines (f, value, oldval)
1373 struct frame *f;
1374 Lisp_Object value, oldval;
1376 int delta, nlines, root_height;
1377 Lisp_Object root_window;
1379 /* Treat tool bars like menu bars. */
1380 if (FRAME_MINIBUF_ONLY_P (f))
1381 return;
1383 /* Use VALUE only if an integer >= 0. */
1384 if (INTEGERP (value) && XINT (value) >= 0)
1385 nlines = XFASTINT (value);
1386 else
1387 nlines = 0;
1389 #ifdef USE_GTK
1390 FRAME_TOOL_BAR_LINES (f) = 0;
1391 if (nlines)
1393 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1394 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1395 /* Make sure next redisplay shows the tool bar. */
1396 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
1397 update_frame_tool_bar (f);
1399 else
1401 if (FRAME_EXTERNAL_TOOL_BAR (f))
1402 free_frame_tool_bar (f);
1403 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1406 return;
1407 #endif
1409 /* Make sure we redisplay all windows in this frame. */
1410 ++windows_or_buffers_changed;
1412 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1414 /* Don't resize the tool-bar to more than we have room for. */
1415 root_window = FRAME_ROOT_WINDOW (f);
1416 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1417 if (root_height - delta < 1)
1419 delta = root_height - 1;
1420 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1423 FRAME_TOOL_BAR_LINES (f) = nlines;
1424 change_window_heights (root_window, delta);
1425 adjust_glyphs (f);
1427 /* We also have to make sure that the internal border at the top of
1428 the frame, below the menu bar or tool bar, is redrawn when the
1429 tool bar disappears. This is so because the internal border is
1430 below the tool bar if one is displayed, but is below the menu bar
1431 if there isn't a tool bar. The tool bar draws into the area
1432 below the menu bar. */
1433 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1435 clear_frame (f);
1436 clear_current_matrices (f);
1439 /* If the tool bar gets smaller, the internal border below it
1440 has to be cleared. It was formerly part of the display
1441 of the larger tool bar, and updating windows won't clear it. */
1442 if (delta < 0)
1444 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1445 int width = FRAME_PIXEL_WIDTH (f);
1446 int y = (FRAME_MENU_BAR_LINES (f) + nlines) * FRAME_LINE_HEIGHT (f);
1448 /* height can be zero here. */
1449 if (height > 0 && width > 0)
1451 BLOCK_INPUT;
1452 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1453 0, y, width, height, False);
1454 UNBLOCK_INPUT;
1457 if (WINDOWP (f->tool_bar_window))
1458 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1463 /* Set the foreground color for scroll bars on frame F to VALUE.
1464 VALUE should be a string, a color name. If it isn't a string or
1465 isn't a valid color name, do nothing. OLDVAL is the old value of
1466 the frame parameter. */
1468 void
1469 x_set_scroll_bar_foreground (f, value, oldval)
1470 struct frame *f;
1471 Lisp_Object value, oldval;
1473 unsigned long pixel;
1475 if (STRINGP (value))
1476 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1477 else
1478 pixel = -1;
1480 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1481 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1483 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1484 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1486 /* Remove all scroll bars because they have wrong colors. */
1487 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1488 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1489 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1490 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1492 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1493 redraw_frame (f);
1498 /* Set the background color for scroll bars on frame F to VALUE VALUE
1499 should be a string, a color name. If it isn't a string or isn't a
1500 valid color name, do nothing. OLDVAL is the old value of the frame
1501 parameter. */
1503 void
1504 x_set_scroll_bar_background (f, value, oldval)
1505 struct frame *f;
1506 Lisp_Object value, oldval;
1508 unsigned long pixel;
1510 if (STRINGP (value))
1511 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1512 else
1513 pixel = -1;
1515 if (f->output_data.x->scroll_bar_background_pixel != -1)
1516 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1518 #ifdef USE_TOOLKIT_SCROLL_BARS
1519 /* Scrollbar shadow colors. */
1520 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1522 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1523 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1525 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1527 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1528 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1530 #endif /* USE_TOOLKIT_SCROLL_BARS */
1532 f->output_data.x->scroll_bar_background_pixel = pixel;
1533 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1535 /* Remove all scroll bars because they have wrong colors. */
1536 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1537 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1538 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1539 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1541 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1542 redraw_frame (f);
1547 /* Encode Lisp string STRING as a text in a format appropriate for
1548 XICCC (X Inter Client Communication Conventions).
1550 This can call Lisp code, so callers must GCPRO.
1552 If STRING contains only ASCII characters, do no conversion and
1553 return the string data of STRING. Otherwise, encode the text by
1554 CODING_SYSTEM, and return a newly allocated memory area which
1555 should be freed by `xfree' by a caller.
1557 SELECTIONP non-zero means the string is being encoded for an X
1558 selection, so it is safe to run pre-write conversions (which
1559 may run Lisp code).
1561 Store the byte length of resulting text in *TEXT_BYTES.
1563 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1564 which means that the `encoding' of the result can be `STRING'.
1565 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1566 the result should be `COMPOUND_TEXT'. */
1568 static unsigned char *
1569 x_encode_text (string, coding_system, selectionp, text_bytes, stringp, freep)
1570 Lisp_Object string, coding_system;
1571 int *text_bytes, *stringp;
1572 int selectionp;
1573 int *freep;
1575 int result = string_xstring_p (string);
1576 struct coding_system coding;
1578 if (result == 0)
1580 /* No multibyte character in OBJ. We need not encode it. */
1581 *text_bytes = SBYTES (string);
1582 *stringp = 1;
1583 *freep = 0;
1584 return SDATA (string);
1587 setup_coding_system (coding_system, &coding);
1588 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1589 /* We suppress producing escape sequences for composition. */
1590 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1591 coding.dst_bytes = SCHARS (string) * 2;
1592 coding.destination = (unsigned char *) xmalloc (coding.dst_bytes);
1593 encode_coding_object (&coding, string, 0, 0,
1594 SCHARS (string), SBYTES (string), Qnil);
1595 *text_bytes = coding.produced;
1596 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1597 *freep = 1;
1598 return coding.destination;
1602 /* Set the WM name to NAME for frame F. Also set the icon name.
1603 If the frame already has an icon name, use that, otherwise set the
1604 icon name to NAME. */
1606 static void
1607 x_set_name_internal (f, name)
1608 FRAME_PTR f;
1609 Lisp_Object name;
1611 if (FRAME_X_WINDOW (f))
1613 BLOCK_INPUT;
1615 XTextProperty text, icon;
1616 int bytes, stringp;
1617 int do_free_icon_value = 0, do_free_text_value = 0;
1618 Lisp_Object coding_system;
1619 #ifdef USE_GTK
1620 Lisp_Object encoded_name;
1621 struct gcpro gcpro1;
1623 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1624 we use it before x_encode_text that may return string data. */
1625 GCPRO1 (name);
1626 encoded_name = ENCODE_UTF_8 (name);
1627 UNGCPRO;
1628 #endif
1630 coding_system = Qcompound_text;
1631 /* Note: Encoding strategy
1633 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1634 text.encoding. But, there are non-internationalized window
1635 managers which don't support that encoding. So, if NAME
1636 contains only ASCII and 8859-1 characters, encode it by
1637 iso-latin-1, and use "STRING" in text.encoding hoping that
1638 such window managers at least analyze this format correctly,
1639 i.e. treat 8-bit bytes as 8859-1 characters.
1641 We may also be able to use "UTF8_STRING" in text.encoding
1642 in the future which can encode all Unicode characters.
1643 But, for the moment, there's no way to know that the
1644 current window manager supports it or not. */
1645 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1646 &do_free_text_value);
1647 text.encoding = (stringp ? XA_STRING
1648 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1649 text.format = 8;
1650 text.nitems = bytes;
1652 if (!STRINGP (f->icon_name))
1654 icon = text;
1656 else
1658 /* See the above comment "Note: Encoding strategy". */
1659 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1660 &bytes, &stringp, &do_free_icon_value);
1661 icon.encoding = (stringp ? XA_STRING
1662 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1663 icon.format = 8;
1664 icon.nitems = bytes;
1667 #ifdef USE_GTK
1668 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1669 (char *) SDATA (encoded_name));
1670 #else /* not USE_GTK */
1671 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1672 #endif /* not USE_GTK */
1674 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1676 if (do_free_icon_value)
1677 xfree (icon.value);
1678 if (do_free_text_value)
1679 xfree (text.value);
1681 UNBLOCK_INPUT;
1685 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1686 x_id_name.
1688 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1689 name; if NAME is a string, set F's name to NAME and set
1690 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1692 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1693 suggesting a new name, which lisp code should override; if
1694 F->explicit_name is set, ignore the new name; otherwise, set it. */
1696 void
1697 x_set_name (f, name, explicit)
1698 struct frame *f;
1699 Lisp_Object name;
1700 int explicit;
1702 /* Make sure that requests from lisp code override requests from
1703 Emacs redisplay code. */
1704 if (explicit)
1706 /* If we're switching from explicit to implicit, we had better
1707 update the mode lines and thereby update the title. */
1708 if (f->explicit_name && NILP (name))
1709 update_mode_lines = 1;
1711 f->explicit_name = ! NILP (name);
1713 else if (f->explicit_name)
1714 return;
1716 /* If NAME is nil, set the name to the x_id_name. */
1717 if (NILP (name))
1719 /* Check for no change needed in this very common case
1720 before we do any consing. */
1721 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1722 SDATA (f->name)))
1723 return;
1724 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1726 else
1727 CHECK_STRING (name);
1729 /* Don't change the name if it's already NAME. */
1730 if (! NILP (Fstring_equal (name, f->name)))
1731 return;
1733 f->name = name;
1735 /* For setting the frame title, the title parameter should override
1736 the name parameter. */
1737 if (! NILP (f->title))
1738 name = f->title;
1740 x_set_name_internal (f, name);
1743 /* This function should be called when the user's lisp code has
1744 specified a name for the frame; the name will override any set by the
1745 redisplay code. */
1746 void
1747 x_explicitly_set_name (f, arg, oldval)
1748 FRAME_PTR f;
1749 Lisp_Object arg, oldval;
1751 x_set_name (f, arg, 1);
1754 /* This function should be called by Emacs redisplay code to set the
1755 name; names set this way will never override names set by the user's
1756 lisp code. */
1757 void
1758 x_implicitly_set_name (f, arg, oldval)
1759 FRAME_PTR f;
1760 Lisp_Object arg, oldval;
1762 x_set_name (f, arg, 0);
1765 /* Change the title of frame F to NAME.
1766 If NAME is nil, use the frame name as the title. */
1768 void
1769 x_set_title (f, name, old_name)
1770 struct frame *f;
1771 Lisp_Object name, old_name;
1773 /* Don't change the title if it's already NAME. */
1774 if (EQ (name, f->title))
1775 return;
1777 update_mode_lines = 1;
1779 f->title = name;
1781 if (NILP (name))
1782 name = f->name;
1783 else
1784 CHECK_STRING (name);
1786 x_set_name_internal (f, name);
1789 void
1790 x_set_scroll_bar_default_width (f)
1791 struct frame *f;
1793 int wid = FRAME_COLUMN_WIDTH (f);
1795 #ifdef USE_TOOLKIT_SCROLL_BARS
1796 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1797 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1798 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1799 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1800 #else
1801 /* Make the actual width at least 14 pixels and a multiple of a
1802 character width. */
1803 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1805 /* Use all of that space (aside from required margins) for the
1806 scroll bar. */
1807 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1808 #endif
1812 /* Record in frame F the specified or default value according to ALIST
1813 of the parameter named PROP (a Lisp symbol). If no value is
1814 specified for PROP, look for an X default for XPROP on the frame
1815 named NAME. If that is not found either, use the value DEFLT. */
1817 static Lisp_Object
1818 x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
1819 foreground_p)
1820 struct frame *f;
1821 Lisp_Object alist;
1822 Lisp_Object prop;
1823 char *xprop;
1824 char *xclass;
1825 int foreground_p;
1827 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1828 Lisp_Object tem;
1830 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1831 if (EQ (tem, Qunbound))
1833 #ifdef USE_TOOLKIT_SCROLL_BARS
1835 /* See if an X resource for the scroll bar color has been
1836 specified. */
1837 tem = display_x_get_resource (dpyinfo,
1838 build_string (foreground_p
1839 ? "foreground"
1840 : "background"),
1841 empty_unibyte_string,
1842 build_string ("verticalScrollBar"),
1843 empty_unibyte_string);
1844 if (!STRINGP (tem))
1846 /* If nothing has been specified, scroll bars will use a
1847 toolkit-dependent default. Because these defaults are
1848 difficult to get at without actually creating a scroll
1849 bar, use nil to indicate that no color has been
1850 specified. */
1851 tem = Qnil;
1854 #else /* not USE_TOOLKIT_SCROLL_BARS */
1856 tem = Qnil;
1858 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1861 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
1862 return tem;
1868 #ifdef USE_X_TOOLKIT
1870 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1871 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1872 already be present because of the toolkit (Motif adds some of them,
1873 for example, but Xt doesn't). */
1875 static void
1876 hack_wm_protocols (f, widget)
1877 FRAME_PTR f;
1878 Widget widget;
1880 Display *dpy = XtDisplay (widget);
1881 Window w = XtWindow (widget);
1882 int need_delete = 1;
1883 int need_focus = 1;
1884 int need_save = 1;
1886 BLOCK_INPUT;
1888 Atom type;
1889 unsigned char *catoms;
1890 int format = 0;
1891 unsigned long nitems = 0;
1892 unsigned long bytes_after;
1894 if ((XGetWindowProperty (dpy, w,
1895 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1896 (long)0, (long)100, False, XA_ATOM,
1897 &type, &format, &nitems, &bytes_after,
1898 &catoms)
1899 == Success)
1900 && format == 32 && type == XA_ATOM)
1902 Atom *atoms = (Atom *) catoms;
1903 while (nitems > 0)
1905 nitems--;
1906 if (atoms[nitems]
1907 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1908 need_delete = 0;
1909 else if (atoms[nitems]
1910 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1911 need_focus = 0;
1912 else if (atoms[nitems]
1913 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1914 need_save = 0;
1917 if (catoms)
1918 XFree (catoms);
1921 Atom props [10];
1922 int count = 0;
1923 if (need_delete)
1924 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1925 if (need_focus)
1926 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1927 if (need_save)
1928 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1929 if (count)
1930 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1931 XA_ATOM, 32, PropModeAppend,
1932 (unsigned char *) props, count);
1934 UNBLOCK_INPUT;
1936 #endif
1940 /* Support routines for XIC (X Input Context). */
1942 #ifdef HAVE_X_I18N
1944 static XFontSet xic_create_xfontset P_ ((struct frame *));
1945 static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
1948 /* Supported XIM styles, ordered by preference. */
1950 static XIMStyle supported_xim_styles[] =
1952 XIMPreeditPosition | XIMStatusArea,
1953 XIMPreeditPosition | XIMStatusNothing,
1954 XIMPreeditPosition | XIMStatusNone,
1955 XIMPreeditNothing | XIMStatusArea,
1956 XIMPreeditNothing | XIMStatusNothing,
1957 XIMPreeditNothing | XIMStatusNone,
1958 XIMPreeditNone | XIMStatusArea,
1959 XIMPreeditNone | XIMStatusNothing,
1960 XIMPreeditNone | XIMStatusNone,
1965 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1967 char xic_defaut_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1969 /* Create an Xt fontset spec from the name of a base font.
1970 If `motif' is True use the Motif syntax. */
1971 char *
1972 xic_create_fontsetname (base_fontname, motif)
1973 char *base_fontname;
1974 Bool motif;
1976 const char *sep = motif ? ";" : ",";
1977 char *fontsetname;
1979 /* Make a fontset name from the base font name. */
1980 if (xic_defaut_fontset == base_fontname)
1981 { /* There is no base font name, use the default. */
1982 int len = strlen (base_fontname) + 2;
1983 fontsetname = xmalloc (len);
1984 bzero (fontsetname, len);
1985 strcpy (fontsetname, base_fontname);
1987 else
1989 /* Make a fontset name from the base font name.
1990 The font set will be made of the following elements:
1991 - the base font.
1992 - the base font where the charset spec is replaced by -*-*.
1993 - the same but with the family also replaced with -*-*-. */
1994 char *p = base_fontname;
1995 int i;
1997 for (i = 0; *p; p++)
1998 if (*p == '-') i++;
1999 if (i != 14)
2000 { /* As the font name doesn't conform to XLFD, we can't
2001 modify it to generalize it to allcs and allfamilies.
2002 Use the specified font plus the default. */
2003 int len = strlen (base_fontname) + strlen (xic_defaut_fontset) + 3;
2004 fontsetname = xmalloc (len);
2005 bzero (fontsetname, len);
2006 strcpy (fontsetname, base_fontname);
2007 strcat (fontsetname, sep);
2008 strcat (fontsetname, xic_defaut_fontset);
2010 else
2012 int len;
2013 char *p1 = NULL, *p2 = NULL, *p3 = NULL;
2014 char *font_allcs = NULL;
2015 char *font_allfamilies = NULL;
2016 char *font_all = NULL;
2017 char *allcs = "*-*-*-*-*-*-*";
2018 char *allfamilies = "-*-*-";
2019 char *all = "*-*-*-*-";
2020 char *base;
2022 for (i = 0, p = base_fontname; i < 8; p++)
2024 if (*p == '-')
2026 i++;
2027 if (i == 3)
2028 p1 = p + 1;
2029 else if (i == 7)
2030 p2 = p + 1;
2031 else if (i == 6)
2032 p3 = p + 1;
2035 /* If base_fontname specifies ADSTYLE, make it a
2036 wildcard. */
2037 if (*p3 != '*')
2039 int diff = (p2 - p3) - 2;
2041 base = alloca (strlen (base_fontname) + 1);
2042 bcopy (base_fontname, base, p3 - base_fontname);
2043 base[p3 - base_fontname] = '*';
2044 base[(p3 - base_fontname) + 1] = '-';
2045 strcpy (base + (p3 - base_fontname) + 2, p2);
2046 p = base + (p - base_fontname) - diff;
2047 p1 = base + (p1 - base_fontname);
2048 p2 = base + (p2 - base_fontname) - diff;
2049 base_fontname = base;
2052 /* Build the font spec that matches all charsets. */
2053 len = p - base_fontname + strlen (allcs) + 1;
2054 font_allcs = (char *) alloca (len);
2055 bzero (font_allcs, len);
2056 bcopy (base_fontname, font_allcs, p - base_fontname);
2057 strcat (font_allcs, allcs);
2059 /* Build the font spec that matches all families and
2060 add-styles. */
2061 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
2062 font_allfamilies = (char *) alloca (len);
2063 bzero (font_allfamilies, len);
2064 strcpy (font_allfamilies, allfamilies);
2065 bcopy (p1, font_allfamilies + strlen (allfamilies), p - p1);
2066 strcat (font_allfamilies, allcs);
2068 /* Build the font spec that matches all. */
2069 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
2070 font_all = (char *) alloca (len);
2071 bzero (font_all, len);
2072 strcpy (font_all, allfamilies);
2073 strcat (font_all, all);
2074 bcopy (p2, font_all + strlen (all) + strlen (allfamilies), p - p2);
2075 strcat (font_all, allcs);
2077 /* Build the actual font set name. */
2078 len = strlen (base_fontname) + strlen (font_allcs)
2079 + strlen (font_allfamilies) + strlen (font_all) + 5;
2080 fontsetname = xmalloc (len);
2081 bzero (fontsetname, len);
2082 strcpy (fontsetname, base_fontname);
2083 strcat (fontsetname, sep);
2084 strcat (fontsetname, font_allcs);
2085 strcat (fontsetname, sep);
2086 strcat (fontsetname, font_allfamilies);
2087 strcat (fontsetname, sep);
2088 strcat (fontsetname, font_all);
2091 if (motif)
2092 strcat (fontsetname, ":");
2093 return fontsetname;
2096 #ifdef DEBUG_XIC_FONTSET
2097 static void
2098 print_fontset_result (xfs, name, missing_list, missing_count)
2099 XFontSet xfs;
2100 char *name;
2101 char **missing_list;
2102 int missing_count;
2104 if (xfs)
2105 fprintf (stderr, "XIC Fontset created: %s\n", name);
2106 else
2108 fprintf (stderr, "XIC Fontset failed: %s\n", name);
2109 while (missing_count-- > 0)
2111 fprintf (stderr, " missing: %s\n", *missing_list);
2112 missing_list++;
2117 #endif
2119 static XFontSet
2120 xic_create_xfontset (f)
2121 struct frame *f;
2123 XFontSet xfs = NULL;
2124 struct font *font = FRAME_FONT (f);
2125 int pixel_size = font->pixel_size;
2126 Lisp_Object rest, frame;
2128 /* See if there is another frame already using same fontset. */
2129 FOR_EACH_FRAME (rest, frame)
2131 struct frame *cf = XFRAME (frame);
2133 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2134 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2135 && FRAME_FONT (f)
2136 && FRAME_FONT (f)->pixel_size == pixel_size)
2138 xfs = FRAME_XIC_FONTSET (cf);
2139 break;
2143 if (! xfs)
2145 char buf[256];
2146 char **missing_list;
2147 int missing_count;
2148 char *def_string;
2149 char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
2151 sprintf (buf, xlfd_format, pixel_size);
2152 missing_list = NULL;
2153 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2154 &missing_list, &missing_count, &def_string);
2155 #ifdef DEBUG_XIC_FONTSET
2156 print_fontset_result (xfs, buf, missing_list, missing_count);
2157 #endif
2158 if (missing_list)
2159 XFreeStringList (missing_list);
2160 if (! xfs)
2162 /* List of pixel sizes most likely available. Find one that
2163 is closest to pixel_size. */
2164 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
2165 int *smaller, *larger;
2167 for (smaller = sizes; smaller[1]; smaller++)
2168 if (smaller[1] >= pixel_size)
2169 break;
2170 larger = smaller + 1;
2171 if (*larger == pixel_size)
2172 larger++;
2173 while (*smaller || *larger)
2175 int this_size;
2177 if (! *larger)
2178 this_size = *smaller--;
2179 else if (! *smaller)
2180 this_size = *larger++;
2181 else if (pixel_size - *smaller < *larger - pixel_size)
2182 this_size = *smaller--;
2183 else
2184 this_size = *larger++;
2185 sprintf (buf, xlfd_format, this_size);
2186 missing_list = NULL;
2187 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2188 &missing_list, &missing_count, &def_string);
2189 #ifdef DEBUG_XIC_FONTSET
2190 print_fontset_result (xfs, buf, missing_list, missing_count);
2191 #endif
2192 if (missing_list)
2193 XFreeStringList (missing_list);
2194 if (xfs)
2195 break;
2198 if (! xfs)
2200 char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
2202 missing_list = NULL;
2203 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
2204 &missing_list, &missing_count, &def_string);
2205 #ifdef DEBUG_XIC_FONTSET
2206 print_fontset_result (xfs, last_resort, missing_list, missing_count);
2207 #endif
2208 if (missing_list)
2209 XFreeStringList (missing_list);
2214 return xfs;
2217 /* Free the X fontset of frame F if it is the last frame using it. */
2219 void
2220 xic_free_xfontset (f)
2221 struct frame *f;
2223 Lisp_Object rest, frame;
2224 int shared_p = 0;
2226 if (!FRAME_XIC_FONTSET (f))
2227 return;
2229 /* See if there is another frame sharing the same fontset. */
2230 FOR_EACH_FRAME (rest, frame)
2232 struct frame *cf = XFRAME (frame);
2233 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2234 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2235 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2237 shared_p = 1;
2238 break;
2242 if (!shared_p)
2243 /* The fontset is not used anymore. It is safe to free it. */
2244 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2246 if (FRAME_XIC_BASE_FONTNAME (f))
2247 xfree (FRAME_XIC_BASE_FONTNAME (f));
2248 FRAME_XIC_BASE_FONTNAME (f) = NULL;
2249 FRAME_XIC_FONTSET (f) = NULL;
2253 /* Value is the best input style, given user preferences USER (already
2254 checked to be supported by Emacs), and styles supported by the
2255 input method XIM. */
2257 static XIMStyle
2258 best_xim_style (user, xim)
2259 XIMStyles *user;
2260 XIMStyles *xim;
2262 int i, j;
2264 for (i = 0; i < user->count_styles; ++i)
2265 for (j = 0; j < xim->count_styles; ++j)
2266 if (user->supported_styles[i] == xim->supported_styles[j])
2267 return user->supported_styles[i];
2269 /* Return the default style. */
2270 return XIMPreeditNothing | XIMStatusNothing;
2273 /* Create XIC for frame F. */
2275 static XIMStyle xic_style;
2277 void
2278 create_frame_xic (f)
2279 struct frame *f;
2281 XIM xim;
2282 XIC xic = NULL;
2283 XFontSet xfs = NULL;
2285 if (FRAME_XIC (f))
2286 return;
2288 /* Create X fontset. */
2289 xfs = xic_create_xfontset (f);
2290 xim = FRAME_X_XIM (f);
2291 if (xim)
2293 XRectangle s_area;
2294 XPoint spot;
2295 XVaNestedList preedit_attr;
2296 XVaNestedList status_attr;
2298 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2299 spot.x = 0; spot.y = 1;
2301 /* Determine XIC style. */
2302 if (xic_style == 0)
2304 XIMStyles supported_list;
2305 supported_list.count_styles = (sizeof supported_xim_styles
2306 / sizeof supported_xim_styles[0]);
2307 supported_list.supported_styles = supported_xim_styles;
2308 xic_style = best_xim_style (&supported_list,
2309 FRAME_X_XIM_STYLES (f));
2312 preedit_attr = XVaCreateNestedList (0,
2313 XNFontSet, xfs,
2314 XNForeground,
2315 FRAME_FOREGROUND_PIXEL (f),
2316 XNBackground,
2317 FRAME_BACKGROUND_PIXEL (f),
2318 (xic_style & XIMPreeditPosition
2319 ? XNSpotLocation
2320 : NULL),
2321 &spot,
2322 NULL);
2323 status_attr = XVaCreateNestedList (0,
2324 XNArea,
2325 &s_area,
2326 XNFontSet,
2327 xfs,
2328 XNForeground,
2329 FRAME_FOREGROUND_PIXEL (f),
2330 XNBackground,
2331 FRAME_BACKGROUND_PIXEL (f),
2332 NULL);
2334 xic = XCreateIC (xim,
2335 XNInputStyle, xic_style,
2336 XNClientWindow, FRAME_X_WINDOW (f),
2337 XNFocusWindow, FRAME_X_WINDOW (f),
2338 XNStatusAttributes, status_attr,
2339 XNPreeditAttributes, preedit_attr,
2340 NULL);
2341 XFree (preedit_attr);
2342 XFree (status_attr);
2345 FRAME_XIC (f) = xic;
2346 FRAME_XIC_STYLE (f) = xic_style;
2347 FRAME_XIC_FONTSET (f) = xfs;
2351 /* Destroy XIC and free XIC fontset of frame F, if any. */
2353 void
2354 free_frame_xic (f)
2355 struct frame *f;
2357 if (FRAME_XIC (f) == NULL)
2358 return;
2360 XDestroyIC (FRAME_XIC (f));
2361 xic_free_xfontset (f);
2363 FRAME_XIC (f) = NULL;
2367 /* Place preedit area for XIC of window W's frame to specified
2368 pixel position X/Y. X and Y are relative to window W. */
2370 void
2371 xic_set_preeditarea (w, x, y)
2372 struct window *w;
2373 int x, y;
2375 struct frame *f = XFRAME (w->frame);
2376 XVaNestedList attr;
2377 XPoint spot;
2379 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2380 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2381 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2382 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2383 XFree (attr);
2387 /* Place status area for XIC in bottom right corner of frame F.. */
2389 void
2390 xic_set_statusarea (f)
2391 struct frame *f;
2393 XIC xic = FRAME_XIC (f);
2394 XVaNestedList attr;
2395 XRectangle area;
2396 XRectangle *needed;
2398 /* Negotiate geometry of status area. If input method has existing
2399 status area, use its current size. */
2400 area.x = area.y = area.width = area.height = 0;
2401 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2402 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2403 XFree (attr);
2405 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2406 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2407 XFree (attr);
2409 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2411 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2412 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2413 XFree (attr);
2416 area.width = needed->width;
2417 area.height = needed->height;
2418 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2419 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2420 - FRAME_MENUBAR_HEIGHT (f)
2421 - FRAME_TOOLBAR_HEIGHT (f)
2422 - FRAME_INTERNAL_BORDER_WIDTH (f));
2423 XFree (needed);
2425 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2426 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2427 XFree (attr);
2431 /* Set X fontset for XIC of frame F, using base font name
2432 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2434 void
2435 xic_set_xfontset (f, base_fontname)
2436 struct frame *f;
2437 char *base_fontname;
2439 XVaNestedList attr;
2440 XFontSet xfs;
2442 xic_free_xfontset (f);
2444 xfs = xic_create_xfontset (f);
2446 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2447 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2448 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2449 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2450 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2451 XFree (attr);
2453 FRAME_XIC_FONTSET (f) = xfs;
2456 #endif /* HAVE_X_I18N */
2460 #ifdef USE_X_TOOLKIT
2462 /* Create and set up the X widget for frame F. */
2464 static void
2465 x_window (f, window_prompting, minibuffer_only)
2466 struct frame *f;
2467 long window_prompting;
2468 int minibuffer_only;
2470 XClassHint class_hints;
2471 XSetWindowAttributes attributes;
2472 unsigned long attribute_mask;
2473 Widget shell_widget;
2474 Widget pane_widget;
2475 Widget frame_widget;
2476 Arg al [25];
2477 int ac;
2479 BLOCK_INPUT;
2481 /* Use the resource name as the top-level widget name
2482 for looking up resources. Make a non-Lisp copy
2483 for the window manager, so GC relocation won't bother it.
2485 Elsewhere we specify the window name for the window manager. */
2488 char *str = (char *) SDATA (Vx_resource_name);
2489 f->namebuf = (char *) xmalloc (strlen (str) + 1);
2490 strcpy (f->namebuf, str);
2493 ac = 0;
2494 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2495 XtSetArg (al[ac], XtNinput, 1); ac++;
2496 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2497 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2498 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2499 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2500 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2501 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2502 applicationShellWidgetClass,
2503 FRAME_X_DISPLAY (f), al, ac);
2505 f->output_data.x->widget = shell_widget;
2506 /* maybe_set_screen_title_format (shell_widget); */
2508 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2509 (widget_value *) NULL,
2510 shell_widget, False,
2511 (lw_callback) NULL,
2512 (lw_callback) NULL,
2513 (lw_callback) NULL,
2514 (lw_callback) NULL);
2516 ac = 0;
2517 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2518 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2519 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2520 XtSetValues (pane_widget, al, ac);
2521 f->output_data.x->column_widget = pane_widget;
2523 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2524 the emacs screen when changing menubar. This reduces flickering. */
2526 ac = 0;
2527 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2528 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2529 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2530 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2531 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2532 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2533 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2534 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2535 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2536 al, ac);
2538 f->output_data.x->edit_widget = frame_widget;
2540 XtManageChild (frame_widget);
2542 /* Do some needed geometry management. */
2544 int len;
2545 char *tem, shell_position[32];
2546 Arg al[10];
2547 int ac = 0;
2548 int extra_borders = 0;
2549 int menubar_size
2550 = (f->output_data.x->menubar_widget
2551 ? (f->output_data.x->menubar_widget->core.height
2552 + f->output_data.x->menubar_widget->core.border_width)
2553 : 0);
2555 #if 0 /* Experimentally, we now get the right results
2556 for -geometry -0-0 without this. 24 Aug 96, rms. */
2557 if (FRAME_EXTERNAL_MENU_BAR (f))
2559 Dimension ibw = 0;
2560 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2561 menubar_size += ibw;
2563 #endif
2565 f->output_data.x->menubar_height = menubar_size;
2567 #ifndef USE_LUCID
2568 /* Motif seems to need this amount added to the sizes
2569 specified for the shell widget. The Athena/Lucid widgets don't.
2570 Both conclusions reached experimentally. -- rms. */
2571 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2572 &extra_borders, NULL);
2573 extra_borders *= 2;
2574 #endif
2576 /* Convert our geometry parameters into a geometry string
2577 and specify it.
2578 Note that we do not specify here whether the position
2579 is a user-specified or program-specified one.
2580 We pass that information later, in x_wm_set_size_hints. */
2582 int left = f->left_pos;
2583 int xneg = window_prompting & XNegative;
2584 int top = f->top_pos;
2585 int yneg = window_prompting & YNegative;
2586 if (xneg)
2587 left = -left;
2588 if (yneg)
2589 top = -top;
2591 if (window_prompting & USPosition)
2592 sprintf (shell_position, "=%dx%d%c%d%c%d",
2593 FRAME_PIXEL_WIDTH (f) + extra_borders,
2594 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2595 (xneg ? '-' : '+'), left,
2596 (yneg ? '-' : '+'), top);
2597 else
2599 sprintf (shell_position, "=%dx%d",
2600 FRAME_PIXEL_WIDTH (f) + extra_borders,
2601 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2603 /* Setting x and y when the position is not specified in
2604 the geometry string will set program position in the WM hints.
2605 If Emacs had just one program position, we could set it in
2606 fallback resources, but since each make-frame call can specify
2607 different program positions, this is easier. */
2608 XtSetArg (al[ac], XtNx, left); ac++;
2609 XtSetArg (al[ac], XtNy, top); ac++;
2613 len = strlen (shell_position) + 1;
2614 /* We don't free this because we don't know whether
2615 it is safe to free it while the frame exists.
2616 It isn't worth the trouble of arranging to free it
2617 when the frame is deleted. */
2618 tem = (char *) xmalloc (len);
2619 strncpy (tem, shell_position, len);
2620 XtSetArg (al[ac], XtNgeometry, tem); ac++;
2621 XtSetValues (shell_widget, al, ac);
2624 XtManageChild (pane_widget);
2625 XtRealizeWidget (shell_widget);
2627 if (FRAME_X_EMBEDDED_P (f))
2628 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2629 f->output_data.x->parent_desc, 0, 0);
2631 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2633 validate_x_resource_name ();
2635 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2636 class_hints.res_class = (char *) SDATA (Vx_resource_class);
2637 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2639 #ifdef HAVE_X_I18N
2640 FRAME_XIC (f) = NULL;
2641 if (use_xim)
2642 create_frame_xic (f);
2643 #endif
2645 f->output_data.x->wm_hints.input = True;
2646 f->output_data.x->wm_hints.flags |= InputHint;
2647 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2648 &f->output_data.x->wm_hints);
2650 hack_wm_protocols (f, shell_widget);
2652 #ifdef HACK_EDITRES
2653 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2654 #endif
2656 /* Do a stupid property change to force the server to generate a
2657 PropertyNotify event so that the event_stream server timestamp will
2658 be initialized to something relevant to the time we created the window.
2660 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2661 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2662 XA_ATOM, 32, PropModeAppend,
2663 (unsigned char*) NULL, 0);
2665 /* Make all the standard events reach the Emacs frame. */
2666 attributes.event_mask = STANDARD_EVENT_SET;
2668 #ifdef HAVE_X_I18N
2669 if (FRAME_XIC (f))
2671 /* XIM server might require some X events. */
2672 unsigned long fevent = NoEventMask;
2673 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2674 attributes.event_mask |= fevent;
2676 #endif /* HAVE_X_I18N */
2678 attribute_mask = CWEventMask;
2679 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2680 attribute_mask, &attributes);
2682 XtMapWidget (frame_widget);
2684 /* x_set_name normally ignores requests to set the name if the
2685 requested name is the same as the current name. This is the one
2686 place where that assumption isn't correct; f->name is set, but
2687 the X server hasn't been told. */
2689 Lisp_Object name;
2690 int explicit = f->explicit_name;
2692 f->explicit_name = 0;
2693 name = f->name;
2694 f->name = Qnil;
2695 x_set_name (f, name, explicit);
2698 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2699 f->output_data.x->current_cursor
2700 = f->output_data.x->text_cursor);
2702 UNBLOCK_INPUT;
2704 /* This is a no-op, except under Motif. Make sure main areas are
2705 set to something reasonable, in case we get an error later. */
2706 lw_set_main_areas (pane_widget, 0, frame_widget);
2709 #else /* not USE_X_TOOLKIT */
2710 #ifdef USE_GTK
2711 void
2712 x_window (f)
2713 FRAME_PTR f;
2715 if (! xg_create_frame_widgets (f))
2716 error ("Unable to create window");
2718 #ifdef HAVE_X_I18N
2719 FRAME_XIC (f) = NULL;
2720 if (use_xim)
2722 BLOCK_INPUT;
2723 create_frame_xic (f);
2724 if (FRAME_XIC (f))
2726 /* XIM server might require some X events. */
2727 unsigned long fevent = NoEventMask;
2728 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2730 if (fevent != NoEventMask)
2732 XSetWindowAttributes attributes;
2733 XWindowAttributes wattr;
2734 unsigned long attribute_mask;
2736 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2737 &wattr);
2738 attributes.event_mask = wattr.your_event_mask | fevent;
2739 attribute_mask = CWEventMask;
2740 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2741 attribute_mask, &attributes);
2744 UNBLOCK_INPUT;
2746 #endif
2749 #else /*! USE_GTK */
2750 /* Create and set up the X window for frame F. */
2752 void
2753 x_window (f)
2754 struct frame *f;
2757 XClassHint class_hints;
2758 XSetWindowAttributes attributes;
2759 unsigned long attribute_mask;
2761 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2762 attributes.border_pixel = f->output_data.x->border_pixel;
2763 attributes.bit_gravity = StaticGravity;
2764 attributes.backing_store = NotUseful;
2765 attributes.save_under = True;
2766 attributes.event_mask = STANDARD_EVENT_SET;
2767 attributes.colormap = FRAME_X_COLORMAP (f);
2768 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2769 | CWColormap);
2771 BLOCK_INPUT;
2772 FRAME_X_WINDOW (f)
2773 = XCreateWindow (FRAME_X_DISPLAY (f),
2774 f->output_data.x->parent_desc,
2775 f->left_pos,
2776 f->top_pos,
2777 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2778 f->border_width,
2779 CopyFromParent, /* depth */
2780 InputOutput, /* class */
2781 FRAME_X_VISUAL (f),
2782 attribute_mask, &attributes);
2784 #ifdef HAVE_X_I18N
2785 if (use_xim)
2787 create_frame_xic (f);
2788 if (FRAME_XIC (f))
2790 /* XIM server might require some X events. */
2791 unsigned long fevent = NoEventMask;
2792 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2793 attributes.event_mask |= fevent;
2794 attribute_mask = CWEventMask;
2795 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2796 attribute_mask, &attributes);
2799 #endif /* HAVE_X_I18N */
2801 validate_x_resource_name ();
2803 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2804 class_hints.res_class = (char *) SDATA (Vx_resource_class);
2805 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2807 /* The menubar is part of the ordinary display;
2808 it does not count in addition to the height of the window. */
2809 f->output_data.x->menubar_height = 0;
2811 /* This indicates that we use the "Passive Input" input model.
2812 Unless we do this, we don't get the Focus{In,Out} events that we
2813 need to draw the cursor correctly. Accursed bureaucrats.
2814 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2816 f->output_data.x->wm_hints.input = True;
2817 f->output_data.x->wm_hints.flags |= InputHint;
2818 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2819 &f->output_data.x->wm_hints);
2820 f->output_data.x->wm_hints.icon_pixmap = None;
2822 /* Request "save yourself" and "delete window" commands from wm. */
2824 Atom protocols[2];
2825 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2826 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2827 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2830 /* x_set_name normally ignores requests to set the name if the
2831 requested name is the same as the current name. This is the one
2832 place where that assumption isn't correct; f->name is set, but
2833 the X server hasn't been told. */
2835 Lisp_Object name;
2836 int explicit = f->explicit_name;
2838 f->explicit_name = 0;
2839 name = f->name;
2840 f->name = Qnil;
2841 x_set_name (f, name, explicit);
2844 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2845 f->output_data.x->current_cursor
2846 = f->output_data.x->text_cursor);
2848 UNBLOCK_INPUT;
2850 if (FRAME_X_WINDOW (f) == 0)
2851 error ("Unable to create window");
2854 #endif /* not USE_GTK */
2855 #endif /* not USE_X_TOOLKIT */
2857 /* Verify that the icon position args for this window are valid. */
2859 static void
2860 x_icon_verify (f, parms)
2861 struct frame *f;
2862 Lisp_Object parms;
2864 Lisp_Object icon_x, icon_y;
2866 /* Set the position of the icon. Note that twm groups all
2867 icons in an icon window. */
2868 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2869 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2870 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2872 CHECK_NUMBER (icon_x);
2873 CHECK_NUMBER (icon_y);
2875 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2876 error ("Both left and top icon corners of icon must be specified");
2879 /* Handle the icon stuff for this window. Perhaps later we might
2880 want an x_set_icon_position which can be called interactively as
2881 well. */
2883 static void
2884 x_icon (f, parms)
2885 struct frame *f;
2886 Lisp_Object parms;
2888 Lisp_Object icon_x, icon_y;
2889 #if 0
2890 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2891 #endif
2893 /* Set the position of the icon. Note that twm groups all
2894 icons in an icon window. */
2895 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2896 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2897 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2899 CHECK_NUMBER (icon_x);
2900 CHECK_NUMBER (icon_y);
2902 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2903 error ("Both left and top icon corners of icon must be specified");
2905 BLOCK_INPUT;
2907 if (! EQ (icon_x, Qunbound))
2908 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2910 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2911 but x_create_frame still needs it. */
2912 /* Start up iconic or window? */
2913 x_wm_set_window_state
2914 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2915 Qicon)
2916 ? IconicState
2917 : NormalState));
2918 #endif
2920 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
2921 ? f->icon_name
2922 : f->name)));
2924 UNBLOCK_INPUT;
2927 /* Make the GCs needed for this window, setting the
2928 background, border and mouse colors; also create the
2929 mouse cursor and the gray border tile. */
2931 static void
2932 x_make_gc (f)
2933 struct frame *f;
2935 XGCValues gc_values;
2937 BLOCK_INPUT;
2939 /* Create the GCs of this frame.
2940 Note that many default values are used. */
2942 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2943 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2944 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2945 f->output_data.x->normal_gc
2946 = XCreateGC (FRAME_X_DISPLAY (f),
2947 FRAME_X_WINDOW (f),
2948 GCLineWidth | GCForeground | GCBackground,
2949 &gc_values);
2951 /* Reverse video style. */
2952 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2953 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2954 f->output_data.x->reverse_gc
2955 = XCreateGC (FRAME_X_DISPLAY (f),
2956 FRAME_X_WINDOW (f),
2957 GCForeground | GCBackground | GCLineWidth,
2958 &gc_values);
2960 /* Cursor has cursor-color background, background-color foreground. */
2961 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2962 gc_values.background = f->output_data.x->cursor_pixel;
2963 gc_values.fill_style = FillOpaqueStippled;
2964 f->output_data.x->cursor_gc
2965 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2966 (GCForeground | GCBackground
2967 | GCFillStyle | GCLineWidth),
2968 &gc_values);
2970 /* Reliefs. */
2971 f->output_data.x->white_relief.gc = 0;
2972 f->output_data.x->black_relief.gc = 0;
2974 /* Create the gray border tile used when the pointer is not in
2975 the frame. Since this depends on the frame's pixel values,
2976 this must be done on a per-frame basis. */
2977 f->output_data.x->border_tile
2978 = (XCreatePixmapFromBitmapData
2979 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2980 gray_bits, gray_width, gray_height,
2981 FRAME_FOREGROUND_PIXEL (f),
2982 FRAME_BACKGROUND_PIXEL (f),
2983 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2985 UNBLOCK_INPUT;
2989 /* Free what was allocated in x_make_gc. */
2991 void
2992 x_free_gcs (f)
2993 struct frame *f;
2995 Display *dpy = FRAME_X_DISPLAY (f);
2997 BLOCK_INPUT;
2999 if (f->output_data.x->normal_gc)
3001 XFreeGC (dpy, f->output_data.x->normal_gc);
3002 f->output_data.x->normal_gc = 0;
3005 if (f->output_data.x->reverse_gc)
3007 XFreeGC (dpy, f->output_data.x->reverse_gc);
3008 f->output_data.x->reverse_gc = 0;
3011 if (f->output_data.x->cursor_gc)
3013 XFreeGC (dpy, f->output_data.x->cursor_gc);
3014 f->output_data.x->cursor_gc = 0;
3017 if (f->output_data.x->border_tile)
3019 XFreePixmap (dpy, f->output_data.x->border_tile);
3020 f->output_data.x->border_tile = 0;
3023 UNBLOCK_INPUT;
3027 /* Handler for signals raised during x_create_frame and
3028 x_create_top_frame. FRAME is the frame which is partially
3029 constructed. */
3031 static Lisp_Object
3032 unwind_create_frame (frame)
3033 Lisp_Object frame;
3035 struct frame *f = XFRAME (frame);
3037 /* If frame is already dead, nothing to do. This can happen if the
3038 display is disconnected after the frame has become official, but
3039 before x_create_frame removes the unwind protect. */
3040 if (!FRAME_LIVE_P (f))
3041 return Qnil;
3043 /* If frame is ``official'', nothing to do. */
3044 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
3046 #if GLYPH_DEBUG
3047 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3048 #endif
3050 x_free_frame_resources (f);
3052 #if GLYPH_DEBUG
3053 /* Check that reference counts are indeed correct. */
3054 xassert (dpyinfo->reference_count == dpyinfo_refcount);
3055 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
3056 #endif
3057 return Qt;
3060 return Qnil;
3064 static void
3065 x_default_font_parameter (f, parms)
3066 struct frame *f;
3067 Lisp_Object parms;
3069 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3070 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
3071 RES_TYPE_STRING);
3072 Lisp_Object font;
3073 int got_from_gconf = 0;
3074 if (EQ (font_param, Qunbound))
3075 font_param = Qnil;
3077 if (NILP (font_param))
3079 /* System font takes precedendce over X resources. We must suggest this
3080 regardless of font-use-system-font because .emacs may not have been
3081 read yet. */
3082 const char *system_font = xsettings_get_system_font ();
3083 if (system_font) font_param = make_string (system_font,
3084 strlen (system_font));
3087 font = !NILP (font_param) ? font_param
3088 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
3090 if (! STRINGP (font))
3092 char *names[]
3094 #ifdef HAVE_XFT
3095 /* This will find the normal Xft font. */
3096 "monospace-10",
3097 #endif
3098 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
3099 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
3100 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
3101 /* This was formerly the first thing tried, but it finds
3102 too many fonts and takes too long. */
3103 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
3104 /* If those didn't work, look for something which will
3105 at least work. */
3106 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
3107 "fixed",
3108 NULL };
3109 int i;
3111 for (i = 0; names[i]; i++)
3113 font = font_open_by_name (f, names[i]);
3114 if (! NILP (font))
3115 break;
3117 if (NILP (font))
3118 error ("No suitable font was found");
3120 else if (!NILP (font_param))
3122 /* Remember the explicit font parameter, so we can re-apply it after
3123 we've applied the `default' face settings. */
3124 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
3127 x_default_parameter (f, parms, Qfont, font,
3128 got_from_gconf ? NULL : "font",
3129 got_from_gconf ? NULL : "Font",
3130 RES_TYPE_STRING);
3134 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
3135 0, 1, 0,
3136 doc: /* Send the size hints for frame FRAME to the window manager.
3137 If FRAME is nil, use the selected frame. */)
3138 (frame)
3139 Lisp_Object frame;
3141 struct frame *f;
3142 if (NILP (frame))
3143 frame = selected_frame;
3144 f = XFRAME (frame);
3145 BLOCK_INPUT;
3146 if (FRAME_X_P (f))
3147 x_wm_set_size_hint (f, 0, 0);
3148 UNBLOCK_INPUT;
3149 return Qnil;
3152 static void
3153 set_machine_and_pid_properties (struct frame *f)
3155 /* See the above comment "Note: Encoding strategy". */
3156 XTextProperty text;
3157 int bytes, stringp;
3158 int do_free_text_value = 0;
3159 long pid = (long) getpid ();
3161 text.value = x_encode_text (Vsystem_name,
3162 Qcompound_text, 0, &bytes, &stringp,
3163 &do_free_text_value);
3164 text.encoding = (stringp ? XA_STRING
3165 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
3166 text.format = 8;
3167 text.nitems = bytes;
3168 XSetWMClientMachine (FRAME_X_DISPLAY (f),
3169 FRAME_OUTER_WINDOW (f),
3170 &text);
3171 if (do_free_text_value)
3172 xfree (text.value);
3174 XChangeProperty (FRAME_X_DISPLAY (f),
3175 FRAME_OUTER_WINDOW (f),
3176 XInternAtom (FRAME_X_DISPLAY (f),
3177 "_NET_WM_PID",
3178 False),
3179 XA_CARDINAL, 32, PropModeReplace,
3180 (unsigned char *) &pid, 1);
3183 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
3184 1, 1, 0,
3185 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
3186 Return an Emacs frame object.
3187 PARMS is an alist of frame parameters.
3188 If the parameters specify that the frame should not have a minibuffer,
3189 and do not specify a specific minibuffer window to use,
3190 then `default-minibuffer-frame' must be a frame whose minibuffer can
3191 be shared by the new frame.
3193 This function is an internal primitive--use `make-frame' instead. */)
3194 (parms)
3195 Lisp_Object parms;
3197 struct frame *f;
3198 Lisp_Object frame, tem;
3199 Lisp_Object name;
3200 int minibuffer_only = 0;
3201 long window_prompting = 0;
3202 int width, height;
3203 int count = SPECPDL_INDEX ();
3204 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3205 Lisp_Object display;
3206 struct x_display_info *dpyinfo = NULL;
3207 Lisp_Object parent;
3208 struct kboard *kb;
3210 parms = Fcopy_alist (parms);
3212 /* Use this general default value to start with
3213 until we know if this frame has a specified name. */
3214 Vx_resource_name = Vinvocation_name;
3216 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
3217 if (EQ (display, Qunbound))
3218 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3219 if (EQ (display, Qunbound))
3220 display = Qnil;
3221 dpyinfo = check_x_display_info (display);
3222 kb = dpyinfo->terminal->kboard;
3224 if (!dpyinfo->terminal->name)
3225 error ("Terminal is not live, can't create new frames on it");
3227 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3228 if (!STRINGP (name)
3229 && ! EQ (name, Qunbound)
3230 && ! NILP (name))
3231 error ("Invalid frame name--not a string or nil");
3233 if (STRINGP (name))
3234 Vx_resource_name = name;
3236 /* See if parent window is specified. */
3237 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3238 if (EQ (parent, Qunbound))
3239 parent = Qnil;
3240 if (! NILP (parent))
3241 CHECK_NUMBER (parent);
3243 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3244 /* No need to protect DISPLAY because that's not used after passing
3245 it to make_frame_without_minibuffer. */
3246 frame = Qnil;
3247 GCPRO4 (parms, parent, name, frame);
3248 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3249 RES_TYPE_SYMBOL);
3250 if (EQ (tem, Qnone) || NILP (tem))
3251 f = make_frame_without_minibuffer (Qnil, kb, display);
3252 else if (EQ (tem, Qonly))
3254 f = make_minibuffer_frame ();
3255 minibuffer_only = 1;
3257 else if (WINDOWP (tem))
3258 f = make_frame_without_minibuffer (tem, kb, display);
3259 else
3260 f = make_frame (1);
3262 XSETFRAME (frame, f);
3264 /* Note that X Windows does support scroll bars. */
3265 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
3267 f->terminal = dpyinfo->terminal;
3268 f->terminal->reference_count++;
3270 f->output_method = output_x_window;
3271 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
3272 bzero (f->output_data.x, sizeof (struct x_output));
3273 f->output_data.x->icon_bitmap = -1;
3274 FRAME_FONTSET (f) = -1;
3275 f->output_data.x->scroll_bar_foreground_pixel = -1;
3276 f->output_data.x->scroll_bar_background_pixel = -1;
3277 #ifdef USE_TOOLKIT_SCROLL_BARS
3278 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3279 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3280 #endif /* USE_TOOLKIT_SCROLL_BARS */
3282 f->icon_name
3283 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3284 RES_TYPE_STRING);
3285 if (! STRINGP (f->icon_name))
3286 f->icon_name = Qnil;
3288 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3290 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
3291 record_unwind_protect (unwind_create_frame, frame);
3292 #if GLYPH_DEBUG
3293 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
3294 dpyinfo_refcount = dpyinfo->reference_count;
3295 #endif /* GLYPH_DEBUG */
3297 /* These colors will be set anyway later, but it's important
3298 to get the color reference counts right, so initialize them! */
3300 Lisp_Object black;
3301 struct gcpro gcpro1;
3303 /* Function x_decode_color can signal an error. Make
3304 sure to initialize color slots so that we won't try
3305 to free colors we haven't allocated. */
3306 FRAME_FOREGROUND_PIXEL (f) = -1;
3307 FRAME_BACKGROUND_PIXEL (f) = -1;
3308 f->output_data.x->cursor_pixel = -1;
3309 f->output_data.x->cursor_foreground_pixel = -1;
3310 f->output_data.x->border_pixel = -1;
3311 f->output_data.x->mouse_pixel = -1;
3313 black = build_string ("black");
3314 GCPRO1 (black);
3315 FRAME_FOREGROUND_PIXEL (f)
3316 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3317 FRAME_BACKGROUND_PIXEL (f)
3318 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3319 f->output_data.x->cursor_pixel
3320 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3321 f->output_data.x->cursor_foreground_pixel
3322 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3323 f->output_data.x->border_pixel
3324 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3325 f->output_data.x->mouse_pixel
3326 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3327 UNGCPRO;
3330 /* Specify the parent under which to make this X window. */
3332 if (!NILP (parent))
3334 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3335 f->output_data.x->explicit_parent = 1;
3337 else
3339 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3340 f->output_data.x->explicit_parent = 0;
3343 /* Set the name; the functions to which we pass f expect the name to
3344 be set. */
3345 if (EQ (name, Qunbound) || NILP (name))
3347 f->name = build_string (dpyinfo->x_id_name);
3348 f->explicit_name = 0;
3350 else
3352 f->name = name;
3353 f->explicit_name = 1;
3354 /* use the frame's title when getting resources for this frame. */
3355 specbind (Qx_resource_name, name);
3358 f->resx = dpyinfo->resx;
3359 f->resy = dpyinfo->resy;
3361 #ifdef HAVE_FREETYPE
3362 #ifdef HAVE_XFT
3363 register_font_driver (&xftfont_driver, f);
3364 #else /* not HAVE_XFT */
3365 register_font_driver (&ftxfont_driver, f);
3366 #endif /* not HAVE_XFT */
3367 #endif /* HAVE_FREETYPE */
3368 register_font_driver (&xfont_driver, f);
3370 x_default_parameter (f, parms, Qfont_backend, Qnil,
3371 "fontBackend", "FontBackend", RES_TYPE_STRING);
3373 /* Extract the window parameters from the supplied values
3374 that are needed to determine window geometry. */
3375 x_default_font_parameter (f, parms);
3376 if (!FRAME_FONT (f))
3378 delete_frame (frame, Qnoelisp);
3379 error ("Invalid frame font");
3382 #ifdef USE_LUCID
3383 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
3384 whereby it fails to get any font. */
3385 BLOCK_INPUT;
3386 xlwmenu_default_font = XLoadQueryFont (FRAME_X_DISPLAY (f), "fixed");
3387 UNBLOCK_INPUT;
3388 #endif
3390 /* Frame contents get displaced if an embedded X window has a border. */
3391 if (! FRAME_X_EMBEDDED_P (f))
3392 x_default_parameter (f, parms, Qborder_width, make_number (2),
3393 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3395 /* This defaults to 1 in order to match xterm. We recognize either
3396 internalBorderWidth or internalBorder (which is what xterm calls
3397 it). */
3398 if (NILP (Fassq (Qinternal_border_width, parms)))
3400 Lisp_Object value;
3402 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3403 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3404 if (! EQ (value, Qunbound))
3405 parms = Fcons (Fcons (Qinternal_border_width, value),
3406 parms);
3408 x_default_parameter (f, parms, Qinternal_border_width,
3409 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3410 make_number (0),
3411 #else
3412 make_number (1),
3413 #endif
3414 "internalBorderWidth", "internalBorderWidth",
3415 RES_TYPE_NUMBER);
3416 x_default_parameter (f, parms, Qvertical_scroll_bars,
3417 #if defined(USE_GTK) && defined(USE_TOOLKIT_SCROLL_BARS)
3418 Qright,
3419 #else
3420 Qleft,
3421 #endif
3422 "verticalScrollBars", "ScrollBars",
3423 RES_TYPE_SYMBOL);
3425 /* Also do the stuff which must be set before the window exists. */
3426 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3427 "foreground", "Foreground", RES_TYPE_STRING);
3428 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3429 "background", "Background", RES_TYPE_STRING);
3430 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3431 "pointerColor", "Foreground", RES_TYPE_STRING);
3432 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
3433 "cursorColor", "Foreground", RES_TYPE_STRING);
3434 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3435 "borderColor", "BorderColor", RES_TYPE_STRING);
3436 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3437 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3438 x_default_parameter (f, parms, Qline_spacing, Qnil,
3439 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3440 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3441 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3442 x_default_parameter (f, parms, Qright_fringe, Qnil,
3443 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3445 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3446 "scrollBarForeground",
3447 "ScrollBarForeground", 1);
3448 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3449 "scrollBarBackground",
3450 "ScrollBarBackground", 0);
3452 /* Init faces before x_default_parameter is called for scroll-bar
3453 parameters because that function calls x_set_scroll_bar_width,
3454 which calls change_frame_size, which calls Fset_window_buffer,
3455 which runs hooks, which call Fvertical_motion. At the end, we
3456 end up in init_iterator with a null face cache, which should not
3457 happen. */
3458 init_frame_faces (f);
3460 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
3461 "menuBar", "MenuBar", RES_TYPE_BOOLEAN_NUMBER);
3462 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
3463 "toolBar", "ToolBar", RES_TYPE_NUMBER);
3464 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3465 "bufferPredicate", "BufferPredicate",
3466 RES_TYPE_SYMBOL);
3467 x_default_parameter (f, parms, Qtitle, Qnil,
3468 "title", "Title", RES_TYPE_STRING);
3469 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3470 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3471 x_default_parameter (f, parms, Qfullscreen, Qnil,
3472 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3474 /* Compute the size of the X window. */
3475 window_prompting = x_figure_window_size (f, parms, 1);
3477 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3478 f->no_split = minibuffer_only || EQ (tem, Qt);
3480 x_icon_verify (f, parms);
3482 /* Create the X widget or window. */
3483 #ifdef USE_X_TOOLKIT
3484 x_window (f, window_prompting, minibuffer_only);
3485 #else
3486 x_window (f);
3487 #endif
3489 x_icon (f, parms);
3490 x_make_gc (f);
3492 /* Now consider the frame official. */
3493 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3494 Vframe_list = Fcons (frame, Vframe_list);
3496 /* We need to do this after creating the X window, so that the
3497 icon-creation functions can say whose icon they're describing. */
3498 x_default_parameter (f, parms, Qicon_type, Qt,
3499 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
3501 x_default_parameter (f, parms, Qauto_raise, Qnil,
3502 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3503 x_default_parameter (f, parms, Qauto_lower, Qnil,
3504 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3505 x_default_parameter (f, parms, Qcursor_type, Qbox,
3506 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3507 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3508 "scrollBarWidth", "ScrollBarWidth",
3509 RES_TYPE_NUMBER);
3510 x_default_parameter (f, parms, Qalpha, Qnil,
3511 "alpha", "Alpha", RES_TYPE_NUMBER);
3513 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3514 Change will not be effected unless different from the current
3515 FRAME_LINES (f). */
3516 width = FRAME_COLS (f);
3517 height = FRAME_LINES (f);
3519 SET_FRAME_COLS (f, 0);
3520 FRAME_LINES (f) = 0;
3521 change_frame_size (f, height, width, 1, 0, 0);
3523 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3524 /* Create the menu bar. */
3525 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3527 /* If this signals an error, we haven't set size hints for the
3528 frame and we didn't make it visible. */
3529 initialize_frame_menubar (f);
3531 #ifndef USE_GTK
3532 /* This is a no-op, except under Motif where it arranges the
3533 main window for the widgets on it. */
3534 lw_set_main_areas (f->output_data.x->column_widget,
3535 f->output_data.x->menubar_widget,
3536 f->output_data.x->edit_widget);
3537 #endif /* not USE_GTK */
3539 #endif /* USE_X_TOOLKIT || USE_GTK */
3541 /* Tell the server what size and position, etc, we want, and how
3542 badly we want them. This should be done after we have the menu
3543 bar so that its size can be taken into account. */
3544 BLOCK_INPUT;
3545 x_wm_set_size_hint (f, window_prompting, 0);
3546 UNBLOCK_INPUT;
3548 /* Make the window appear on the frame and enable display, unless
3549 the caller says not to. However, with explicit parent, Emacs
3550 cannot control visibility, so don't try. */
3551 if (! f->output_data.x->explicit_parent)
3553 Lisp_Object visibility;
3555 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3556 RES_TYPE_SYMBOL);
3557 if (EQ (visibility, Qunbound))
3558 visibility = Qt;
3560 if (EQ (visibility, Qicon))
3561 x_iconify_frame (f);
3562 else if (! NILP (visibility))
3563 x_make_frame_visible (f);
3564 else
3565 /* Must have been Qnil. */
3569 BLOCK_INPUT;
3571 /* Set machine name and pid for the purpose of window managers. */
3572 set_machine_and_pid_properties(f);
3574 /* Set the WM leader property. GTK does this itself, so this is not
3575 needed when using GTK. */
3576 if (dpyinfo->client_leader_window != 0)
3578 XChangeProperty (FRAME_X_DISPLAY (f),
3579 FRAME_OUTER_WINDOW (f),
3580 dpyinfo->Xatom_wm_client_leader,
3581 XA_WINDOW, 32, PropModeReplace,
3582 (unsigned char *) &dpyinfo->client_leader_window, 1);
3585 UNBLOCK_INPUT;
3587 /* Initialize `default-minibuffer-frame' in case this is the first
3588 frame on this terminal. */
3589 if (FRAME_HAS_MINIBUF_P (f)
3590 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
3591 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
3592 kb->Vdefault_minibuffer_frame = frame;
3594 /* All remaining specified parameters, which have not been "used"
3595 by x_get_arg and friends, now go in the misc. alist of the frame. */
3596 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3597 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3598 f->param_alist = Fcons (XCAR (tem), f->param_alist);
3600 UNGCPRO;
3602 /* Make sure windows on this frame appear in calls to next-window
3603 and similar functions. */
3604 Vwindow_list = Qnil;
3606 return unbind_to (count, frame);
3610 /* FRAME is used only to get a handle on the X display. We don't pass the
3611 display info directly because we're called from frame.c, which doesn't
3612 know about that structure. */
3614 Lisp_Object
3615 x_get_focus_frame (frame)
3616 struct frame *frame;
3618 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
3619 Lisp_Object xfocus;
3620 if (! dpyinfo->x_focus_frame)
3621 return Qnil;
3623 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3624 return xfocus;
3628 /* In certain situations, when the window manager follows a
3629 click-to-focus policy, there seems to be no way around calling
3630 XSetInputFocus to give another frame the input focus .
3632 In an ideal world, XSetInputFocus should generally be avoided so
3633 that applications don't interfere with the window manager's focus
3634 policy. But I think it's okay to use when it's clearly done
3635 following a user-command. */
3637 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
3638 doc: /* Set the input focus to FRAME.
3639 FRAME nil means use the selected frame. */)
3640 (frame)
3641 Lisp_Object frame;
3643 struct frame *f = check_x_frame (frame);
3644 Display *dpy = FRAME_X_DISPLAY (f);
3646 BLOCK_INPUT;
3647 x_catch_errors (dpy);
3648 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3649 RevertToParent, CurrentTime);
3650 x_ewmh_activate_frame (f);
3651 x_uncatch_errors ();
3652 UNBLOCK_INPUT;
3654 return Qnil;
3658 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3659 doc: /* Internal function called by `color-defined-p', which see. */)
3660 (color, frame)
3661 Lisp_Object color, frame;
3663 XColor foo;
3664 FRAME_PTR f = check_x_frame (frame);
3666 CHECK_STRING (color);
3668 if (x_defined_color (f, SDATA (color), &foo, 0))
3669 return Qt;
3670 else
3671 return Qnil;
3674 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3675 doc: /* Internal function called by `color-values', which see. */)
3676 (color, frame)
3677 Lisp_Object color, frame;
3679 XColor foo;
3680 FRAME_PTR f = check_x_frame (frame);
3682 CHECK_STRING (color);
3684 if (x_defined_color (f, SDATA (color), &foo, 0))
3685 return list3 (make_number (foo.red),
3686 make_number (foo.green),
3687 make_number (foo.blue));
3688 else
3689 return Qnil;
3692 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3693 doc: /* Internal function called by `display-color-p', which see. */)
3694 (terminal)
3695 Lisp_Object terminal;
3697 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3699 if (dpyinfo->n_planes <= 2)
3700 return Qnil;
3702 switch (dpyinfo->visual->class)
3704 case StaticColor:
3705 case PseudoColor:
3706 case TrueColor:
3707 case DirectColor:
3708 return Qt;
3710 default:
3711 return Qnil;
3715 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3716 0, 1, 0,
3717 doc: /* Return t if the X display supports shades of gray.
3718 Note that color displays do support shades of gray.
3719 The optional argument TERMINAL specifies which display to ask about.
3720 TERMINAL should be a terminal object, a frame or a display name (a string).
3721 If omitted or nil, that stands for the selected frame's display. */)
3722 (terminal)
3723 Lisp_Object terminal;
3725 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3727 if (dpyinfo->n_planes <= 1)
3728 return Qnil;
3730 switch (dpyinfo->visual->class)
3732 case StaticColor:
3733 case PseudoColor:
3734 case TrueColor:
3735 case DirectColor:
3736 case StaticGray:
3737 case GrayScale:
3738 return Qt;
3740 default:
3741 return Qnil;
3745 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3746 0, 1, 0,
3747 doc: /* Return the width in pixels of the X display TERMINAL.
3748 The optional argument TERMINAL specifies which display to ask about.
3749 TERMINAL should be a terminal object, a frame or a display name (a string).
3750 If omitted or nil, that stands for the selected frame's display. */)
3751 (terminal)
3752 Lisp_Object terminal;
3754 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3756 return make_number (x_display_pixel_width (dpyinfo));
3759 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3760 Sx_display_pixel_height, 0, 1, 0,
3761 doc: /* Return the height in pixels of the X display TERMINAL.
3762 The optional argument TERMINAL specifies which display to ask about.
3763 TERMINAL should be a terminal object, a frame or a display name (a string).
3764 If omitted or nil, that stands for the selected frame's display. */)
3765 (terminal)
3766 Lisp_Object terminal;
3768 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3770 return make_number (x_display_pixel_height (dpyinfo));
3773 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3774 0, 1, 0,
3775 doc: /* Return the number of bitplanes of the X display TERMINAL.
3776 The optional argument TERMINAL specifies which display to ask about.
3777 TERMINAL should be a terminal object, a frame or a display name (a string).
3778 If omitted or nil, that stands for the selected frame's display. */)
3779 (terminal)
3780 Lisp_Object terminal;
3782 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3784 return make_number (dpyinfo->n_planes);
3787 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3788 0, 1, 0,
3789 doc: /* Return the number of color cells of the X display TERMINAL.
3790 The optional argument TERMINAL specifies which display to ask about.
3791 TERMINAL should be a terminal object, a frame or a display name (a string).
3792 If omitted or nil, that stands for the selected frame's display. */)
3793 (terminal)
3794 Lisp_Object terminal;
3796 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3798 int nr_planes = DisplayPlanes (dpyinfo->display,
3799 XScreenNumberOfScreen (dpyinfo->screen));
3801 /* Truncate nr_planes to 24 to avoid integer overflow.
3802 Some displays says 32, but only 24 bits are actually significant.
3803 There are only very few and rare video cards that have more than
3804 24 significant bits. Also 24 bits is more than 16 million colors,
3805 it "should be enough for everyone". */
3806 if (nr_planes > 24) nr_planes = 24;
3808 return make_number (1 << nr_planes);
3811 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3812 Sx_server_max_request_size,
3813 0, 1, 0,
3814 doc: /* Return the maximum request size of the X server of display TERMINAL.
3815 The optional argument TERMINAL specifies which display to ask about.
3816 TERMINAL should be a terminal object, a frame or a display name (a string).
3817 If omitted or nil, that stands for the selected frame's display. */)
3818 (terminal)
3819 Lisp_Object terminal;
3821 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3823 return make_number (MAXREQUEST (dpyinfo->display));
3826 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3827 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3828 \(Labelling every distributor as a "vendor" embodies the false assumption
3829 that operating systems cannot be developed and distributed noncommercially.)
3830 The optional argument TERMINAL specifies which display to ask about.
3831 TERMINAL should be a terminal object, a frame or a display name (a string).
3832 If omitted or nil, that stands for the selected frame's display. */)
3833 (terminal)
3834 Lisp_Object terminal;
3836 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3837 char *vendor = ServerVendor (dpyinfo->display);
3839 if (! vendor) vendor = "";
3840 return build_string (vendor);
3843 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3844 doc: /* Return the version numbers of the X server of display TERMINAL.
3845 The value is a list of three integers: the major and minor
3846 version numbers of the X Protocol in use, and the distributor-specific release
3847 number. See also the function `x-server-vendor'.
3849 The optional argument TERMINAL specifies which display to ask about.
3850 TERMINAL should be a terminal object, a frame or a display name (a string).
3851 If omitted or nil, that stands for the selected frame's display. */)
3852 (terminal)
3853 Lisp_Object terminal;
3855 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3856 Display *dpy = dpyinfo->display;
3858 return Fcons (make_number (ProtocolVersion (dpy)),
3859 Fcons (make_number (ProtocolRevision (dpy)),
3860 Fcons (make_number (VendorRelease (dpy)), Qnil)));
3863 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3864 doc: /* Return the number of screens on the X server of display TERMINAL.
3865 The optional argument TERMINAL specifies which display to ask about.
3866 TERMINAL should be a terminal object, a frame or a display name (a string).
3867 If omitted or nil, that stands for the selected frame's display. */)
3868 (terminal)
3869 Lisp_Object terminal;
3871 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3873 return make_number (ScreenCount (dpyinfo->display));
3876 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3877 doc: /* Return the height in millimeters of the X display TERMINAL.
3878 The optional argument TERMINAL specifies which display to ask about.
3879 TERMINAL should be a terminal object, a frame or a display name (a string).
3880 If omitted or nil, that stands for the selected frame's display. */)
3881 (terminal)
3882 Lisp_Object terminal;
3884 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3886 return make_number (HeightMMOfScreen (dpyinfo->screen));
3889 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3890 doc: /* Return the width in millimeters of the X display TERMINAL.
3891 The optional argument TERMINAL specifies which display to ask about.
3892 TERMINAL should be a terminal object, a frame or a display name (a string).
3893 If omitted or nil, that stands for the selected frame's display. */)
3894 (terminal)
3895 Lisp_Object terminal;
3897 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3899 return make_number (WidthMMOfScreen (dpyinfo->screen));
3902 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3903 Sx_display_backing_store, 0, 1, 0,
3904 doc: /* Return an indication of whether X display TERMINAL does backing store.
3905 The value may be `always', `when-mapped', or `not-useful'.
3906 The optional argument TERMINAL specifies which display to ask about.
3907 TERMINAL should be a terminal object, a frame or a display name (a string).
3908 If omitted or nil, that stands for the selected frame's display. */)
3909 (terminal)
3910 Lisp_Object terminal;
3912 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3913 Lisp_Object result;
3915 switch (DoesBackingStore (dpyinfo->screen))
3917 case Always:
3918 result = intern ("always");
3919 break;
3921 case WhenMapped:
3922 result = intern ("when-mapped");
3923 break;
3925 case NotUseful:
3926 result = intern ("not-useful");
3927 break;
3929 default:
3930 error ("Strange value for BackingStore parameter of screen");
3931 result = Qnil;
3934 return result;
3937 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3938 Sx_display_visual_class, 0, 1, 0,
3939 doc: /* Return the visual class of the X display TERMINAL.
3940 The value is one of the symbols `static-gray', `gray-scale',
3941 `static-color', `pseudo-color', `true-color', or `direct-color'.
3943 The optional argument TERMINAL specifies which display to ask about.
3944 TERMINAL should a terminal object, a frame or a display name (a string).
3945 If omitted or nil, that stands for the selected frame's display. */)
3946 (terminal)
3947 Lisp_Object terminal;
3949 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3950 Lisp_Object result;
3952 switch (dpyinfo->visual->class)
3954 case StaticGray:
3955 result = intern ("static-gray");
3956 break;
3957 case GrayScale:
3958 result = intern ("gray-scale");
3959 break;
3960 case StaticColor:
3961 result = intern ("static-color");
3962 break;
3963 case PseudoColor:
3964 result = intern ("pseudo-color");
3965 break;
3966 case TrueColor:
3967 result = intern ("true-color");
3968 break;
3969 case DirectColor:
3970 result = intern ("direct-color");
3971 break;
3972 default:
3973 error ("Display has an unknown visual class");
3974 result = Qnil;
3977 return result;
3980 DEFUN ("x-display-save-under", Fx_display_save_under,
3981 Sx_display_save_under, 0, 1, 0,
3982 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3983 The optional argument TERMINAL specifies which display to ask about.
3984 TERMINAL should be a terminal object, a frame or a display name (a string).
3985 If omitted or nil, that stands for the selected frame's display. */)
3986 (terminal)
3987 Lisp_Object terminal;
3989 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3991 if (DoesSaveUnders (dpyinfo->screen) == True)
3992 return Qt;
3993 else
3994 return Qnil;
3998 x_pixel_width (f)
3999 register struct frame *f;
4001 return FRAME_PIXEL_WIDTH (f);
4005 x_pixel_height (f)
4006 register struct frame *f;
4008 return FRAME_PIXEL_HEIGHT (f);
4012 x_char_width (f)
4013 register struct frame *f;
4015 return FRAME_COLUMN_WIDTH (f);
4019 x_char_height (f)
4020 register struct frame *f;
4022 return FRAME_LINE_HEIGHT (f);
4026 x_screen_planes (f)
4027 register struct frame *f;
4029 return FRAME_X_DISPLAY_INFO (f)->n_planes;
4034 /************************************************************************
4035 X Displays
4036 ************************************************************************/
4039 /* Mapping visual names to visuals. */
4041 static struct visual_class
4043 char *name;
4044 int class;
4046 visual_classes[] =
4048 {"StaticGray", StaticGray},
4049 {"GrayScale", GrayScale},
4050 {"StaticColor", StaticColor},
4051 {"PseudoColor", PseudoColor},
4052 {"TrueColor", TrueColor},
4053 {"DirectColor", DirectColor},
4054 {NULL, 0}
4058 #ifndef HAVE_XSCREENNUMBEROFSCREEN
4060 /* Value is the screen number of screen SCR. This is a substitute for
4061 the X function with the same name when that doesn't exist. */
4064 XScreenNumberOfScreen (scr)
4065 register Screen *scr;
4067 Display *dpy = scr->display;
4068 int i;
4070 for (i = 0; i < dpy->nscreens; ++i)
4071 if (scr == dpy->screens + i)
4072 break;
4074 return i;
4077 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
4080 /* Select the visual that should be used on display DPYINFO. Set
4081 members of DPYINFO appropriately. Called from x_term_init. */
4083 void
4084 select_visual (dpyinfo)
4085 struct x_display_info *dpyinfo;
4087 Display *dpy = dpyinfo->display;
4088 Screen *screen = dpyinfo->screen;
4089 Lisp_Object value;
4091 /* See if a visual is specified. */
4092 value = display_x_get_resource (dpyinfo,
4093 build_string ("visualClass"),
4094 build_string ("VisualClass"),
4095 Qnil, Qnil);
4096 if (STRINGP (value))
4098 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
4099 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
4100 depth, a decimal number. NAME is compared with case ignored. */
4101 char *s = (char *) alloca (SBYTES (value) + 1);
4102 char *dash;
4103 int i, class = -1;
4104 XVisualInfo vinfo;
4106 strcpy (s, SDATA (value));
4107 dash = index (s, '-');
4108 if (dash)
4110 dpyinfo->n_planes = atoi (dash + 1);
4111 *dash = '\0';
4113 else
4114 /* We won't find a matching visual with depth 0, so that
4115 an error will be printed below. */
4116 dpyinfo->n_planes = 0;
4118 /* Determine the visual class. */
4119 for (i = 0; visual_classes[i].name; ++i)
4120 if (xstrcasecmp (s, visual_classes[i].name) == 0)
4122 class = visual_classes[i].class;
4123 break;
4126 /* Look up a matching visual for the specified class. */
4127 if (class == -1
4128 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
4129 dpyinfo->n_planes, class, &vinfo))
4130 fatal ("Invalid visual specification `%s'", SDATA (value));
4132 dpyinfo->visual = vinfo.visual;
4134 else
4136 int n_visuals;
4137 XVisualInfo *vinfo, vinfo_template;
4139 dpyinfo->visual = DefaultVisualOfScreen (screen);
4141 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
4142 vinfo_template.screen = XScreenNumberOfScreen (screen);
4143 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
4144 &vinfo_template, &n_visuals);
4145 if (n_visuals != 1)
4146 fatal ("Can't get proper X visual info");
4148 dpyinfo->n_planes = vinfo->depth;
4149 XFree ((char *) vinfo);
4154 /* Return the X display structure for the display named NAME.
4155 Open a new connection if necessary. */
4157 struct x_display_info *
4158 x_display_info_for_name (name)
4159 Lisp_Object name;
4161 Lisp_Object names;
4162 struct x_display_info *dpyinfo;
4164 CHECK_STRING (name);
4166 #if 0
4167 if (! EQ (Vinitial_window_system, intern ("x")))
4168 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4169 #endif
4171 for (dpyinfo = x_display_list, names = x_display_name_list;
4172 dpyinfo;
4173 dpyinfo = dpyinfo->next, names = XCDR (names))
4175 Lisp_Object tem;
4176 tem = Fstring_equal (XCAR (XCAR (names)), name);
4177 if (!NILP (tem))
4178 return dpyinfo;
4181 /* Use this general default value to start with. */
4182 Vx_resource_name = Vinvocation_name;
4184 validate_x_resource_name ();
4186 dpyinfo = x_term_init (name, (char *)0,
4187 (char *) SDATA (Vx_resource_name));
4189 if (dpyinfo == 0)
4190 error ("Cannot connect to X server %s", SDATA (name));
4192 x_in_use = 1;
4193 XSETFASTINT (Vwindow_system_version, 11);
4195 return dpyinfo;
4199 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4200 1, 3, 0,
4201 doc: /* Open a connection to an X server.
4202 DISPLAY is the name of the display to connect to.
4203 Optional second arg XRM-STRING is a string of resources in xrdb format.
4204 If the optional third arg MUST-SUCCEED is non-nil,
4205 terminate Emacs if we can't open the connection. */)
4206 (display, xrm_string, must_succeed)
4207 Lisp_Object display, xrm_string, must_succeed;
4209 unsigned char *xrm_option;
4210 struct x_display_info *dpyinfo;
4212 CHECK_STRING (display);
4213 if (! NILP (xrm_string))
4214 CHECK_STRING (xrm_string);
4216 #if 0
4217 if (! EQ (Vinitial_window_system, intern ("x")))
4218 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4219 #endif
4221 if (! NILP (xrm_string))
4222 xrm_option = (unsigned char *) SDATA (xrm_string);
4223 else
4224 xrm_option = (unsigned char *) 0;
4226 validate_x_resource_name ();
4228 /* This is what opens the connection and sets x_current_display.
4229 This also initializes many symbols, such as those used for input. */
4230 dpyinfo = x_term_init (display, xrm_option,
4231 (char *) SDATA (Vx_resource_name));
4233 if (dpyinfo == 0)
4235 if (!NILP (must_succeed))
4236 fatal ("Cannot connect to X server %s.\n\
4237 Check the DISPLAY environment variable or use `-d'.\n\
4238 Also use the `xauth' program to verify that you have the proper\n\
4239 authorization information needed to connect the X server.\n\
4240 An insecure way to solve the problem may be to use `xhost'.\n",
4241 SDATA (display));
4242 else
4243 error ("Cannot connect to X server %s", SDATA (display));
4246 x_in_use = 1;
4248 XSETFASTINT (Vwindow_system_version, 11);
4249 return Qnil;
4252 DEFUN ("x-close-connection", Fx_close_connection,
4253 Sx_close_connection, 1, 1, 0,
4254 doc: /* Close the connection to TERMINAL's X server.
4255 For TERMINAL, specify a terminal object, a frame or a display name (a
4256 string). If TERMINAL is nil, that stands for the selected frame's
4257 terminal. */)
4258 (terminal)
4259 Lisp_Object terminal;
4261 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4263 if (dpyinfo->reference_count > 0)
4264 error ("Display still has frames on it");
4266 x_delete_terminal (dpyinfo->terminal);
4268 return Qnil;
4271 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4272 doc: /* Return the list of display names that Emacs has connections to. */)
4275 Lisp_Object tail, result;
4277 result = Qnil;
4278 for (tail = x_display_name_list; CONSP (tail); tail = XCDR (tail))
4279 result = Fcons (XCAR (XCAR (tail)), result);
4281 return result;
4284 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4285 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4286 If ON is nil, allow buffering of requests.
4287 Turning on synchronization prohibits the Xlib routines from buffering
4288 requests and seriously degrades performance, but makes debugging much
4289 easier.
4290 The optional second argument TERMINAL specifies which display to act on.
4291 TERMINAL should be a terminal object, a frame or a display name (a string).
4292 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4293 (on, terminal)
4294 Lisp_Object terminal, on;
4296 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4298 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4300 return Qnil;
4303 /* Wait for responses to all X commands issued so far for frame F. */
4305 void
4306 x_sync (f)
4307 FRAME_PTR f;
4309 BLOCK_INPUT;
4310 XSync (FRAME_X_DISPLAY (f), False);
4311 UNBLOCK_INPUT;
4315 /***********************************************************************
4316 Window properties
4317 ***********************************************************************/
4319 DEFUN ("x-change-window-property", Fx_change_window_property,
4320 Sx_change_window_property, 2, 6, 0,
4321 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4322 PROP must be a string.
4323 VALUE may be a string or a list of conses, numbers and/or strings.
4324 If an element in the list is a string, it is converted to
4325 an Atom and the value of the Atom is used. If an element is a cons,
4326 it is converted to a 32 bit number where the car is the 16 top bits and the
4327 cdr is the lower 16 bits.
4328 FRAME nil or omitted means use the selected frame.
4329 If TYPE is given and non-nil, it is the name of the type of VALUE.
4330 If TYPE is not given or nil, the type is STRING.
4331 FORMAT gives the size in bits of each element if VALUE is a list.
4332 It must be one of 8, 16 or 32.
4333 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4334 If OUTER_P is non-nil, the property is changed for the outer X window of
4335 FRAME. Default is to change on the edit X window.
4337 Value is VALUE. */)
4338 (prop, value, frame, type, format, outer_p)
4339 Lisp_Object prop, value, frame, type, format, outer_p;
4341 struct frame *f = check_x_frame (frame);
4342 Atom prop_atom;
4343 Atom target_type = XA_STRING;
4344 int element_format = 8;
4345 unsigned char *data;
4346 int nelements;
4347 Window w;
4349 CHECK_STRING (prop);
4351 if (! NILP (format))
4353 CHECK_NUMBER (format);
4354 element_format = XFASTINT (format);
4356 if (element_format != 8 && element_format != 16
4357 && element_format != 32)
4358 error ("FORMAT must be one of 8, 16 or 32");
4361 if (CONSP (value))
4363 nelements = x_check_property_data (value);
4364 if (nelements == -1)
4365 error ("Bad data in VALUE, must be number, string or cons");
4367 if (element_format == 8)
4368 data = (unsigned char *) xmalloc (nelements);
4369 else if (element_format == 16)
4370 data = (unsigned char *) xmalloc (nelements*2);
4371 else /* format == 32 */
4372 /* The man page for XChangeProperty:
4373 "If the specified format is 32, the property data must be a
4374 long array."
4375 This applies even if long is more than 64 bits. The X library
4376 converts to 32 bits before sending to the X server. */
4377 data = (unsigned char *) xmalloc (nelements * sizeof(long));
4379 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4381 else
4383 CHECK_STRING (value);
4384 data = SDATA (value);
4385 nelements = SCHARS (value);
4388 BLOCK_INPUT;
4389 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4390 if (! NILP (type))
4392 CHECK_STRING (type);
4393 target_type = XInternAtom (FRAME_X_DISPLAY (f), SDATA (type), False);
4396 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4397 else w = FRAME_X_WINDOW (f);
4399 XChangeProperty (FRAME_X_DISPLAY (f), w,
4400 prop_atom, target_type, element_format, PropModeReplace,
4401 data, nelements);
4403 if (CONSP (value)) xfree (data);
4405 /* Make sure the property is set when we return. */
4406 XFlush (FRAME_X_DISPLAY (f));
4407 UNBLOCK_INPUT;
4409 return value;
4413 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4414 Sx_delete_window_property, 1, 2, 0,
4415 doc: /* Remove window property PROP from X window of FRAME.
4416 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4417 (prop, frame)
4418 Lisp_Object prop, frame;
4420 struct frame *f = check_x_frame (frame);
4421 Atom prop_atom;
4423 CHECK_STRING (prop);
4424 BLOCK_INPUT;
4425 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4426 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4428 /* Make sure the property is removed when we return. */
4429 XFlush (FRAME_X_DISPLAY (f));
4430 UNBLOCK_INPUT;
4432 return prop;
4436 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4437 1, 6, 0,
4438 doc: /* Value is the value of window property PROP on FRAME.
4439 If FRAME is nil or omitted, use the selected frame.
4440 If TYPE is nil or omitted, get the property as a string. Otherwise TYPE
4441 is the name of the Atom that denotes the type expected.
4442 If SOURCE is non-nil, get the property on that window instead of from
4443 FRAME. The number 0 denotes the root window.
4444 If DELETE_P is non-nil, delete the property after retreiving it.
4445 If VECTOR_RET_P is non-nil, don't return a string but a vector of values.
4447 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4448 no value of TYPE. */)
4449 (prop, frame, type, source, delete_p, vector_ret_p)
4450 Lisp_Object prop, frame, type, source, delete_p, vector_ret_p;
4452 struct frame *f = check_x_frame (frame);
4453 Atom prop_atom;
4454 int rc;
4455 Lisp_Object prop_value = Qnil;
4456 unsigned char *tmp_data = NULL;
4457 Atom actual_type;
4458 Atom target_type = XA_STRING;
4459 int actual_format;
4460 unsigned long actual_size, bytes_remaining;
4461 Window target_window = FRAME_X_WINDOW (f);
4462 struct gcpro gcpro1;
4464 GCPRO1 (prop_value);
4465 CHECK_STRING (prop);
4467 if (! NILP (source))
4469 if (NUMBERP (source))
4471 if (FLOATP (source))
4472 target_window = (Window) XFLOAT (source);
4473 else
4474 target_window = XFASTINT (source);
4476 if (target_window == 0)
4477 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
4479 else if (CONSP (source))
4480 target_window = cons_to_long (source);
4483 BLOCK_INPUT;
4484 if (STRINGP (type))
4486 if (strcmp ("AnyPropertyType", SDATA (type)) == 0)
4487 target_type = AnyPropertyType;
4488 else
4489 target_type = XInternAtom (FRAME_X_DISPLAY (f), SDATA (type), False);
4492 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4493 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4494 prop_atom, 0, 0, False, target_type,
4495 &actual_type, &actual_format, &actual_size,
4496 &bytes_remaining, &tmp_data);
4497 if (rc == Success)
4499 int size = bytes_remaining;
4501 XFree (tmp_data);
4502 tmp_data = NULL;
4504 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4505 prop_atom, 0, bytes_remaining,
4506 ! NILP (delete_p), target_type,
4507 &actual_type, &actual_format,
4508 &actual_size, &bytes_remaining,
4509 &tmp_data);
4510 if (rc == Success && tmp_data)
4512 /* The man page for XGetWindowProperty says:
4513 "If the returned format is 32, the returned data is represented
4514 as a long array and should be cast to that type to obtain the
4515 elements."
4516 This applies even if long is more than 32 bits, the X library
4517 converts from 32 bit elements received from the X server to long
4518 and passes the long array to us. Thus, for that case bcopy can not
4519 be used. We convert to a 32 bit type here, because so much code
4520 assume on that.
4522 The bytes and offsets passed to XGetWindowProperty refers to the
4523 property and those are indeed in 32 bit quantities if format is
4524 32. */
4526 if (actual_format == 32 && actual_format < BITS_PER_LONG)
4528 unsigned long i;
4529 int *idata = (int *) tmp_data;
4530 long *ldata = (long *) tmp_data;
4532 for (i = 0; i < actual_size; ++i)
4533 idata[i] = (int) ldata[i];
4536 if (NILP (vector_ret_p))
4537 prop_value = make_string (tmp_data, size);
4538 else
4539 prop_value = x_property_data_to_lisp (f,
4540 tmp_data,
4541 actual_type,
4542 actual_format,
4543 actual_size);
4546 if (tmp_data) XFree (tmp_data);
4549 UNBLOCK_INPUT;
4550 UNGCPRO;
4551 return prop_value;
4556 /***********************************************************************
4557 Busy cursor
4558 ***********************************************************************/
4560 /* Timer function of hourglass_atimer. TIMER is equal to
4561 hourglass_atimer.
4563 Display an hourglass pointer on all frames by mapping the frames'
4564 hourglass_window. Set the hourglass_p flag in the frames'
4565 output_data.x structure to indicate that an hourglass cursor is
4566 shown on the frames. */
4568 void
4569 show_hourglass (timer)
4570 struct atimer *timer;
4572 /* The timer implementation will cancel this timer automatically
4573 after this function has run. Set hourglass_atimer to null
4574 so that we know the timer doesn't have to be canceled. */
4575 hourglass_atimer = NULL;
4577 if (!hourglass_shown_p)
4579 Lisp_Object rest, frame;
4581 BLOCK_INPUT;
4583 FOR_EACH_FRAME (rest, frame)
4585 struct frame *f = XFRAME (frame);
4587 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4589 Display *dpy = FRAME_X_DISPLAY (f);
4591 #ifdef USE_X_TOOLKIT
4592 if (f->output_data.x->widget)
4593 #else
4594 if (FRAME_OUTER_WINDOW (f))
4595 #endif
4597 f->output_data.x->hourglass_p = 1;
4599 if (!f->output_data.x->hourglass_window)
4601 unsigned long mask = CWCursor;
4602 XSetWindowAttributes attrs;
4603 #ifdef USE_GTK
4604 Window parent = FRAME_X_WINDOW (f);
4605 #else
4606 Window parent = FRAME_OUTER_WINDOW (f);
4607 #endif
4608 attrs.cursor = f->output_data.x->hourglass_cursor;
4610 f->output_data.x->hourglass_window
4611 = XCreateWindow (dpy, parent,
4612 0, 0, 32000, 32000, 0, 0,
4613 InputOnly,
4614 CopyFromParent,
4615 mask, &attrs);
4618 XMapRaised (dpy, f->output_data.x->hourglass_window);
4619 XFlush (dpy);
4624 hourglass_shown_p = 1;
4625 UNBLOCK_INPUT;
4630 /* Hide the hourglass pointer on all frames, if it is currently
4631 shown. */
4633 void
4634 hide_hourglass ()
4636 if (hourglass_shown_p)
4638 Lisp_Object rest, frame;
4640 BLOCK_INPUT;
4641 FOR_EACH_FRAME (rest, frame)
4643 struct frame *f = XFRAME (frame);
4645 if (FRAME_X_P (f)
4646 /* Watch out for newly created frames. */
4647 && f->output_data.x->hourglass_window)
4649 XUnmapWindow (FRAME_X_DISPLAY (f),
4650 f->output_data.x->hourglass_window);
4651 /* Sync here because XTread_socket looks at the
4652 hourglass_p flag that is reset to zero below. */
4653 XSync (FRAME_X_DISPLAY (f), False);
4654 f->output_data.x->hourglass_p = 0;
4658 hourglass_shown_p = 0;
4659 UNBLOCK_INPUT;
4665 /***********************************************************************
4666 Tool tips
4667 ***********************************************************************/
4669 static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
4670 Lisp_Object, Lisp_Object));
4671 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
4672 Lisp_Object, int, int, int *, int *));
4674 /* The frame of a currently visible tooltip. */
4676 Lisp_Object tip_frame;
4678 /* If non-nil, a timer started that hides the last tooltip when it
4679 fires. */
4681 Lisp_Object tip_timer;
4682 Window tip_window;
4684 /* If non-nil, a vector of 3 elements containing the last args
4685 with which x-show-tip was called. See there. */
4687 Lisp_Object last_show_tip_args;
4689 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
4691 Lisp_Object Vx_max_tooltip_size;
4694 static Lisp_Object
4695 unwind_create_tip_frame (frame)
4696 Lisp_Object frame;
4698 Lisp_Object deleted;
4700 deleted = unwind_create_frame (frame);
4701 if (EQ (deleted, Qt))
4703 tip_window = None;
4704 tip_frame = Qnil;
4707 return deleted;
4711 /* Create a frame for a tooltip on the display described by DPYINFO.
4712 PARMS is a list of frame parameters. TEXT is the string to
4713 display in the tip frame. Value is the frame.
4715 Note that functions called here, esp. x_default_parameter can
4716 signal errors, for instance when a specified color name is
4717 undefined. We have to make sure that we're in a consistent state
4718 when this happens. */
4720 static Lisp_Object
4721 x_create_tip_frame (dpyinfo, parms, text)
4722 struct x_display_info *dpyinfo;
4723 Lisp_Object parms, text;
4725 struct frame *f;
4726 Lisp_Object frame, tem;
4727 Lisp_Object name;
4728 long window_prompting = 0;
4729 int width, height;
4730 int count = SPECPDL_INDEX ();
4731 struct gcpro gcpro1, gcpro2, gcpro3;
4732 int face_change_count_before = face_change_count;
4733 Lisp_Object buffer;
4734 struct buffer *old_buffer;
4736 check_x ();
4738 if (!dpyinfo->terminal->name)
4739 error ("Terminal is not live, can't create new frames on it");
4741 parms = Fcopy_alist (parms);
4743 /* Get the name of the frame to use for resource lookup. */
4744 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4745 if (!STRINGP (name)
4746 && !EQ (name, Qunbound)
4747 && !NILP (name))
4748 error ("Invalid frame name--not a string or nil");
4750 frame = Qnil;
4751 GCPRO3 (parms, name, frame);
4752 f = make_frame (1);
4753 XSETFRAME (frame, f);
4755 buffer = Fget_buffer_create (build_string (" *tip*"));
4756 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
4757 old_buffer = current_buffer;
4758 set_buffer_internal_1 (XBUFFER (buffer));
4759 current_buffer->truncate_lines = Qnil;
4760 specbind (Qinhibit_read_only, Qt);
4761 specbind (Qinhibit_modification_hooks, Qt);
4762 Ferase_buffer ();
4763 Finsert (1, &text);
4764 set_buffer_internal_1 (old_buffer);
4766 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
4767 record_unwind_protect (unwind_create_tip_frame, frame);
4769 f->terminal = dpyinfo->terminal;
4770 f->terminal->reference_count++;
4772 /* By setting the output method, we're essentially saying that
4773 the frame is live, as per FRAME_LIVE_P. If we get a signal
4774 from this point on, x_destroy_window might screw up reference
4775 counts etc. */
4776 f->output_method = output_x_window;
4777 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
4778 bzero (f->output_data.x, sizeof (struct x_output));
4779 f->output_data.x->icon_bitmap = -1;
4780 FRAME_FONTSET (f) = -1;
4781 f->output_data.x->scroll_bar_foreground_pixel = -1;
4782 f->output_data.x->scroll_bar_background_pixel = -1;
4783 #ifdef USE_TOOLKIT_SCROLL_BARS
4784 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4785 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4786 #endif /* USE_TOOLKIT_SCROLL_BARS */
4787 f->icon_name = Qnil;
4788 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4789 #if GLYPH_DEBUG
4790 image_cache_refcount = FRAME_IMAGE_CACHE (f)->refcount;
4791 dpyinfo_refcount = dpyinfo->reference_count;
4792 #endif /* GLYPH_DEBUG */
4793 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4794 f->output_data.x->explicit_parent = 0;
4796 /* These colors will be set anyway later, but it's important
4797 to get the color reference counts right, so initialize them! */
4799 Lisp_Object black;
4800 struct gcpro gcpro1;
4802 /* Function x_decode_color can signal an error. Make
4803 sure to initialize color slots so that we won't try
4804 to free colors we haven't allocated. */
4805 FRAME_FOREGROUND_PIXEL (f) = -1;
4806 FRAME_BACKGROUND_PIXEL (f) = -1;
4807 f->output_data.x->cursor_pixel = -1;
4808 f->output_data.x->cursor_foreground_pixel = -1;
4809 f->output_data.x->border_pixel = -1;
4810 f->output_data.x->mouse_pixel = -1;
4812 black = build_string ("black");
4813 GCPRO1 (black);
4814 FRAME_FOREGROUND_PIXEL (f)
4815 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4816 FRAME_BACKGROUND_PIXEL (f)
4817 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4818 f->output_data.x->cursor_pixel
4819 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4820 f->output_data.x->cursor_foreground_pixel
4821 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4822 f->output_data.x->border_pixel
4823 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4824 f->output_data.x->mouse_pixel
4825 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4826 UNGCPRO;
4829 /* Set the name; the functions to which we pass f expect the name to
4830 be set. */
4831 if (EQ (name, Qunbound) || NILP (name))
4833 f->name = build_string (dpyinfo->x_id_name);
4834 f->explicit_name = 0;
4836 else
4838 f->name = name;
4839 f->explicit_name = 1;
4840 /* use the frame's title when getting resources for this frame. */
4841 specbind (Qx_resource_name, name);
4844 f->resx = dpyinfo->resx;
4845 f->resy = dpyinfo->resy;
4847 register_font_driver (&xfont_driver, f);
4848 #ifdef HAVE_FREETYPE
4849 #ifdef HAVE_XFT
4850 register_font_driver (&xftfont_driver, f);
4851 #else /* not HAVE_XFT */
4852 register_font_driver (&ftxfont_driver, f);
4853 #endif /* not HAVE_XFT */
4854 #endif /* HAVE_FREETYPE */
4856 x_default_parameter (f, parms, Qfont_backend, Qnil,
4857 "fontBackend", "FontBackend", RES_TYPE_STRING);
4859 /* Extract the window parameters from the supplied values that are
4860 needed to determine window geometry. */
4861 x_default_font_parameter (f, parms);
4863 x_default_parameter (f, parms, Qborder_width, make_number (2),
4864 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4866 /* This defaults to 2 in order to match xterm. We recognize either
4867 internalBorderWidth or internalBorder (which is what xterm calls
4868 it). */
4869 if (NILP (Fassq (Qinternal_border_width, parms)))
4871 Lisp_Object value;
4873 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4874 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4875 if (! EQ (value, Qunbound))
4876 parms = Fcons (Fcons (Qinternal_border_width, value),
4877 parms);
4880 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4881 "internalBorderWidth", "internalBorderWidth",
4882 RES_TYPE_NUMBER);
4884 /* Also do the stuff which must be set before the window exists. */
4885 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4886 "foreground", "Foreground", RES_TYPE_STRING);
4887 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4888 "background", "Background", RES_TYPE_STRING);
4889 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4890 "pointerColor", "Foreground", RES_TYPE_STRING);
4891 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4892 "cursorColor", "Foreground", RES_TYPE_STRING);
4893 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4894 "borderColor", "BorderColor", RES_TYPE_STRING);
4896 /* Init faces before x_default_parameter is called for scroll-bar
4897 parameters because that function calls x_set_scroll_bar_width,
4898 which calls change_frame_size, which calls Fset_window_buffer,
4899 which runs hooks, which call Fvertical_motion. At the end, we
4900 end up in init_iterator with a null face cache, which should not
4901 happen. */
4902 init_frame_faces (f);
4904 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4906 window_prompting = x_figure_window_size (f, parms, 0);
4909 XSetWindowAttributes attrs;
4910 unsigned long mask;
4911 Atom type = FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
4913 BLOCK_INPUT;
4914 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4915 if (DoesSaveUnders (dpyinfo->screen))
4916 mask |= CWSaveUnder;
4918 /* Window managers look at the override-redirect flag to determine
4919 whether or net to give windows a decoration (Xlib spec, chapter
4920 3.2.8). */
4921 attrs.override_redirect = True;
4922 attrs.save_under = True;
4923 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4924 /* Arrange for getting MapNotify and UnmapNotify events. */
4925 attrs.event_mask = StructureNotifyMask;
4926 tip_window
4927 = FRAME_X_WINDOW (f)
4928 = XCreateWindow (FRAME_X_DISPLAY (f),
4929 FRAME_X_DISPLAY_INFO (f)->root_window,
4930 /* x, y, width, height */
4931 0, 0, 1, 1,
4932 /* Border. */
4933 f->border_width,
4934 CopyFromParent, InputOutput, CopyFromParent,
4935 mask, &attrs);
4936 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
4937 FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type,
4938 XA_ATOM, 32, PropModeReplace,
4939 (unsigned char *)&type, 1);
4940 UNBLOCK_INPUT;
4943 x_make_gc (f);
4945 x_default_parameter (f, parms, Qauto_raise, Qnil,
4946 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4947 x_default_parameter (f, parms, Qauto_lower, Qnil,
4948 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4949 x_default_parameter (f, parms, Qcursor_type, Qbox,
4950 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4952 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4953 Change will not be effected unless different from the current
4954 FRAME_LINES (f). */
4955 width = FRAME_COLS (f);
4956 height = FRAME_LINES (f);
4957 SET_FRAME_COLS (f, 0);
4958 FRAME_LINES (f) = 0;
4959 change_frame_size (f, height, width, 1, 0, 0);
4961 /* Add `tooltip' frame parameter's default value. */
4962 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
4963 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
4964 Qnil));
4966 /* FIXME - can this be done in a similar way to normal frames?
4967 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
4969 /* Set the `display-type' frame parameter before setting up faces. */
4971 Lisp_Object disptype;
4973 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
4974 disptype = intern ("mono");
4975 else if (FRAME_X_DISPLAY_INFO (f)->visual->class == GrayScale
4976 || FRAME_X_DISPLAY_INFO (f)->visual->class == StaticGray)
4977 disptype = intern ("grayscale");
4978 else
4979 disptype = intern ("color");
4981 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
4982 Fmodify_frame_parameters (frame, Fcons (Fcons (Qdisplay_type, disptype),
4983 Qnil));
4986 /* Set up faces after all frame parameters are known. This call
4987 also merges in face attributes specified for new frames.
4989 Frame parameters may be changed if .Xdefaults contains
4990 specifications for the default font. For example, if there is an
4991 `Emacs.default.attributeBackground: pink', the `background-color'
4992 attribute of the frame get's set, which let's the internal border
4993 of the tooltip frame appear in pink. Prevent this. */
4995 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
4997 /* Set tip_frame here, so that */
4998 tip_frame = frame;
4999 call2 (Qface_set_after_frame_default, frame, Qnil);
5001 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
5002 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
5003 Qnil));
5006 f->no_split = 1;
5008 UNGCPRO;
5010 /* It is now ok to make the frame official even if we get an error
5011 below. And the frame needs to be on Vframe_list or making it
5012 visible won't work. */
5013 Vframe_list = Fcons (frame, Vframe_list);
5015 /* Now that the frame is official, it counts as a reference to
5016 its display. */
5017 FRAME_X_DISPLAY_INFO (f)->reference_count++;
5019 /* Setting attributes of faces of the tooltip frame from resources
5020 and similar will increment face_change_count, which leads to the
5021 clearing of all current matrices. Since this isn't necessary
5022 here, avoid it by resetting face_change_count to the value it
5023 had before we created the tip frame. */
5024 face_change_count = face_change_count_before;
5026 /* Discard the unwind_protect. */
5027 return unbind_to (count, frame);
5031 /* Compute where to display tip frame F. PARMS is the list of frame
5032 parameters for F. DX and DY are specified offsets from the current
5033 location of the mouse. WIDTH and HEIGHT are the width and height
5034 of the tooltip. Return coordinates relative to the root window of
5035 the display in *ROOT_X, and *ROOT_Y. */
5037 static void
5038 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
5039 struct frame *f;
5040 Lisp_Object parms, dx, dy;
5041 int width, height;
5042 int *root_x, *root_y;
5044 Lisp_Object left, top;
5045 int win_x, win_y;
5046 Window root, child;
5047 unsigned pmask;
5049 /* User-specified position? */
5050 left = Fcdr (Fassq (Qleft, parms));
5051 top = Fcdr (Fassq (Qtop, parms));
5053 /* Move the tooltip window where the mouse pointer is. Resize and
5054 show it. */
5055 if (!INTEGERP (left) || !INTEGERP (top))
5057 BLOCK_INPUT;
5058 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
5059 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
5060 UNBLOCK_INPUT;
5063 if (INTEGERP (top))
5064 *root_y = XINT (top);
5065 else if (*root_y + XINT (dy) <= 0)
5066 *root_y = 0; /* Can happen for negative dy */
5067 else if (*root_y + XINT (dy) + height
5068 <= x_display_pixel_height (FRAME_X_DISPLAY_INFO (f)))
5069 /* It fits below the pointer */
5070 *root_y += XINT (dy);
5071 else if (height + XINT (dy) <= *root_y)
5072 /* It fits above the pointer. */
5073 *root_y -= height + XINT (dy);
5074 else
5075 /* Put it on the top. */
5076 *root_y = 0;
5078 if (INTEGERP (left))
5079 *root_x = XINT (left);
5080 else if (*root_x + XINT (dx) <= 0)
5081 *root_x = 0; /* Can happen for negative dx */
5082 else if (*root_x + XINT (dx) + width
5083 <= x_display_pixel_width (FRAME_X_DISPLAY_INFO (f)))
5084 /* It fits to the right of the pointer. */
5085 *root_x += XINT (dx);
5086 else if (width + XINT (dx) <= *root_x)
5087 /* It fits to the left of the pointer. */
5088 *root_x -= width + XINT (dx);
5089 else
5090 /* Put it left-justified on the screen--it ought to fit that way. */
5091 *root_x = 0;
5095 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
5096 doc: /* Show STRING in a "tooltip" window on frame FRAME.
5097 A tooltip window is a small X window displaying a string.
5099 This is an internal function; Lisp code should call `tooltip-show'.
5101 FRAME nil or omitted means use the selected frame.
5103 PARMS is an optional list of frame parameters which can be used to
5104 change the tooltip's appearance.
5106 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5107 means use the default timeout of 5 seconds.
5109 If the list of frame parameters PARAMS contains a `left' parameters,
5110 the tooltip is displayed at that x-position. Otherwise it is
5111 displayed at the mouse position, with offset DX added (default is 5 if
5112 DX isn't specified). Likewise for the y-position; if a `top' frame
5113 parameter is specified, it determines the y-position of the tooltip
5114 window, otherwise it is displayed at the mouse position, with offset
5115 DY added (default is -10).
5117 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5118 Text larger than the specified size is clipped. */)
5119 (string, frame, parms, timeout, dx, dy)
5120 Lisp_Object string, frame, parms, timeout, dx, dy;
5122 struct frame *f;
5123 struct window *w;
5124 int root_x, root_y;
5125 struct buffer *old_buffer;
5126 struct text_pos pos;
5127 int i, width, height;
5128 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5129 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5130 int count = SPECPDL_INDEX ();
5132 specbind (Qinhibit_redisplay, Qt);
5134 GCPRO4 (string, parms, frame, timeout);
5136 CHECK_STRING (string);
5137 if (SCHARS (string) == 0)
5138 string = make_unibyte_string (" ", 1);
5140 f = check_x_frame (frame);
5141 if (NILP (timeout))
5142 timeout = make_number (5);
5143 else
5144 CHECK_NATNUM (timeout);
5146 if (NILP (dx))
5147 dx = make_number (5);
5148 else
5149 CHECK_NUMBER (dx);
5151 if (NILP (dy))
5152 dy = make_number (-10);
5153 else
5154 CHECK_NUMBER (dy);
5156 if (NILP (last_show_tip_args))
5157 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5159 if (!NILP (tip_frame))
5161 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5162 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5163 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5165 if (EQ (frame, last_frame)
5166 && !NILP (Fequal (last_string, string))
5167 && !NILP (Fequal (last_parms, parms)))
5169 struct frame *f = XFRAME (tip_frame);
5171 /* Only DX and DY have changed. */
5172 if (!NILP (tip_timer))
5174 Lisp_Object timer = tip_timer;
5175 tip_timer = Qnil;
5176 call1 (Qcancel_timer, timer);
5179 BLOCK_INPUT;
5180 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5181 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5182 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5183 root_x, root_y);
5184 UNBLOCK_INPUT;
5185 goto start_timer;
5189 /* Hide a previous tip, if any. */
5190 Fx_hide_tip ();
5192 ASET (last_show_tip_args, 0, string);
5193 ASET (last_show_tip_args, 1, frame);
5194 ASET (last_show_tip_args, 2, parms);
5196 /* Add default values to frame parameters. */
5197 if (NILP (Fassq (Qname, parms)))
5198 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5199 if (NILP (Fassq (Qinternal_border_width, parms)))
5200 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5201 if (NILP (Fassq (Qborder_width, parms)))
5202 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5203 if (NILP (Fassq (Qborder_color, parms)))
5204 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5205 if (NILP (Fassq (Qbackground_color, parms)))
5206 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5207 parms);
5209 /* Create a frame for the tooltip, and record it in the global
5210 variable tip_frame. */
5211 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
5212 f = XFRAME (frame);
5214 /* Set up the frame's root window. */
5215 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5216 w->left_col = w->top_line = make_number (0);
5218 if (CONSP (Vx_max_tooltip_size)
5219 && INTEGERP (XCAR (Vx_max_tooltip_size))
5220 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5221 && INTEGERP (XCDR (Vx_max_tooltip_size))
5222 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5224 w->total_cols = XCAR (Vx_max_tooltip_size);
5225 w->total_lines = XCDR (Vx_max_tooltip_size);
5227 else
5229 w->total_cols = make_number (80);
5230 w->total_lines = make_number (40);
5233 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5234 adjust_glyphs (f);
5235 w->pseudo_window_p = 1;
5237 /* Display the tooltip text in a temporary buffer. */
5238 old_buffer = current_buffer;
5239 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5240 current_buffer->truncate_lines = Qnil;
5241 clear_glyph_matrix (w->desired_matrix);
5242 clear_glyph_matrix (w->current_matrix);
5243 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5244 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5246 /* Compute width and height of the tooltip. */
5247 width = height = 0;
5248 for (i = 0; i < w->desired_matrix->nrows; ++i)
5250 struct glyph_row *row = &w->desired_matrix->rows[i];
5251 struct glyph *last;
5252 int row_width;
5254 /* Stop at the first empty row at the end. */
5255 if (!row->enabled_p || !row->displays_text_p)
5256 break;
5258 /* Let the row go over the full width of the frame. */
5259 row->full_width_p = 1;
5261 /* There's a glyph at the end of rows that is used to place
5262 the cursor there. Don't include the width of this glyph. */
5263 if (row->used[TEXT_AREA])
5265 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5266 row_width = row->pixel_width - last->pixel_width;
5268 else
5269 row_width = row->pixel_width;
5271 height += row->height;
5272 width = max (width, row_width);
5275 /* Add the frame's internal border to the width and height the X
5276 window should have. */
5277 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5278 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5280 /* Move the tooltip window where the mouse pointer is. Resize and
5281 show it. */
5282 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5284 BLOCK_INPUT;
5285 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5286 root_x, root_y, width, height);
5287 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5288 UNBLOCK_INPUT;
5290 /* Draw into the window. */
5291 w->must_be_updated_p = 1;
5292 update_single_window (w, 1);
5294 /* Restore original current buffer. */
5295 set_buffer_internal_1 (old_buffer);
5296 windows_or_buffers_changed = old_windows_or_buffers_changed;
5298 start_timer:
5299 /* Let the tip disappear after timeout seconds. */
5300 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5301 intern ("x-hide-tip"));
5303 UNGCPRO;
5304 return unbind_to (count, Qnil);
5308 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5309 doc: /* Hide the current tooltip window, if there is any.
5310 Value is t if tooltip was open, nil otherwise. */)
5313 int count;
5314 Lisp_Object deleted, frame, timer;
5315 struct gcpro gcpro1, gcpro2;
5317 /* Return quickly if nothing to do. */
5318 if (NILP (tip_timer) && NILP (tip_frame))
5319 return Qnil;
5321 frame = tip_frame;
5322 timer = tip_timer;
5323 GCPRO2 (frame, timer);
5324 tip_frame = tip_timer = deleted = Qnil;
5326 count = SPECPDL_INDEX ();
5327 specbind (Qinhibit_redisplay, Qt);
5328 specbind (Qinhibit_quit, Qt);
5330 if (!NILP (timer))
5331 call1 (Qcancel_timer, timer);
5333 if (FRAMEP (frame))
5335 delete_frame (frame, Qnil);
5336 deleted = Qt;
5338 #ifdef USE_LUCID
5339 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5340 redisplay procedure is not called when a tip frame over menu
5341 items is unmapped. Redisplay the menu manually... */
5343 struct frame *f = SELECTED_FRAME ();
5344 Widget w = f->output_data.x->menubar_widget;
5345 extern void xlwmenu_redisplay P_ ((Widget));
5347 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5348 && w != NULL)
5350 BLOCK_INPUT;
5351 xlwmenu_redisplay (w);
5352 UNBLOCK_INPUT;
5355 #endif /* USE_LUCID */
5358 UNGCPRO;
5359 return unbind_to (count, deleted);
5364 /***********************************************************************
5365 File selection dialog
5366 ***********************************************************************/
5368 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5369 Sx_uses_old_gtk_dialog,
5370 0, 0, 0,
5371 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5374 #ifdef USE_GTK
5375 extern int use_dialog_box;
5376 extern int use_file_dialog;
5378 if (use_dialog_box
5379 && use_file_dialog
5380 && have_menus_p ()
5381 && xg_uses_old_file_dialog ())
5382 return Qt;
5383 #endif
5384 return Qnil;
5388 #ifdef USE_MOTIF
5389 /* Callback for "OK" and "Cancel" on file selection dialog. */
5391 static void
5392 file_dialog_cb (widget, client_data, call_data)
5393 Widget widget;
5394 XtPointer call_data, client_data;
5396 int *result = (int *) client_data;
5397 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5398 *result = cb->reason;
5402 /* Callback for unmapping a file selection dialog. This is used to
5403 capture the case where a dialog is closed via a window manager's
5404 closer button, for example. Using a XmNdestroyCallback didn't work
5405 in this case. */
5407 static void
5408 file_dialog_unmap_cb (widget, client_data, call_data)
5409 Widget widget;
5410 XtPointer call_data, client_data;
5412 int *result = (int *) client_data;
5413 *result = XmCR_CANCEL;
5416 static Lisp_Object
5417 clean_up_file_dialog (arg)
5418 Lisp_Object arg;
5420 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
5421 Widget dialog = (Widget) p->pointer;
5423 /* Clean up. */
5424 BLOCK_INPUT;
5425 XtUnmanageChild (dialog);
5426 XtDestroyWidget (dialog);
5427 x_menu_set_in_use (0);
5428 UNBLOCK_INPUT;
5430 return Qnil;
5434 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5435 doc: /* Read file name, prompting with PROMPT in directory DIR.
5436 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5437 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5438 or directory must exist. ONLY-DIR-P is ignored." */)
5439 (prompt, dir, default_filename, mustmatch, only_dir_p)
5440 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
5442 int result;
5443 struct frame *f = SELECTED_FRAME ();
5444 Lisp_Object file = Qnil;
5445 Lisp_Object decoded_file;
5446 Widget dialog, text, help;
5447 Arg al[10];
5448 int ac = 0;
5449 extern XtAppContext Xt_app_con;
5450 XmString dir_xmstring, pattern_xmstring;
5451 int count = SPECPDL_INDEX ();
5452 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5454 check_x ();
5456 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5458 if (popup_activated ())
5459 error ("Trying to use a menu from within a menu-entry");
5461 CHECK_STRING (prompt);
5462 CHECK_STRING (dir);
5464 /* Prevent redisplay. */
5465 specbind (Qinhibit_redisplay, Qt);
5467 BLOCK_INPUT;
5469 /* Create the dialog with PROMPT as title, using DIR as initial
5470 directory and using "*" as pattern. */
5471 dir = Fexpand_file_name (dir, Qnil);
5472 dir_xmstring = XmStringCreateLocalized (SDATA (dir));
5473 pattern_xmstring = XmStringCreateLocalized ("*");
5475 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5476 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5477 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5478 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5479 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5480 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5481 "fsb", al, ac);
5482 XmStringFree (dir_xmstring);
5483 XmStringFree (pattern_xmstring);
5485 /* Add callbacks for OK and Cancel. */
5486 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5487 (XtPointer) &result);
5488 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5489 (XtPointer) &result);
5490 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5491 (XtPointer) &result);
5493 /* Remove the help button since we can't display help. */
5494 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5495 XtUnmanageChild (help);
5497 /* Mark OK button as default. */
5498 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5499 XmNshowAsDefault, True, NULL);
5501 /* If MUSTMATCH is non-nil, disable the file entry field of the
5502 dialog, so that the user must select a file from the files list
5503 box. We can't remove it because we wouldn't have a way to get at
5504 the result file name, then. */
5505 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5506 if (!NILP (mustmatch))
5508 Widget label;
5509 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5510 XtSetSensitive (text, False);
5511 XtSetSensitive (label, False);
5514 /* Manage the dialog, so that list boxes get filled. */
5515 XtManageChild (dialog);
5517 if (STRINGP (default_filename))
5519 XmString default_xmstring;
5520 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5521 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5523 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5524 XmTextFieldReplace (wtext, 0, last_pos,
5525 (SDATA (Ffile_name_nondirectory (default_filename))));
5527 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5528 must include the path for this to work. */
5530 default_xmstring = XmStringCreateLocalized (SDATA (default_filename));
5532 if (XmListItemExists (list, default_xmstring))
5534 int item_pos = XmListItemPos (list, default_xmstring);
5535 /* Select the item and scroll it into view. */
5536 XmListSelectPos (list, item_pos, True);
5537 XmListSetPos (list, item_pos);
5540 XmStringFree (default_xmstring);
5543 record_unwind_protect (clean_up_file_dialog, make_save_value (dialog, 0));
5545 /* Process events until the user presses Cancel or OK. */
5546 x_menu_set_in_use (1);
5547 result = 0;
5548 while (result == 0)
5550 XEvent event;
5551 x_menu_wait_for_event (0);
5552 XtAppNextEvent (Xt_app_con, &event);
5553 if (event.type == KeyPress
5554 && FRAME_X_DISPLAY (f) == event.xkey.display)
5556 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5558 /* Pop down on C-g. */
5559 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5560 XtUnmanageChild (dialog);
5563 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5566 /* Get the result. */
5567 if (result == XmCR_OK)
5569 XmString text;
5570 String data;
5572 XtVaGetValues (dialog, XmNtextString, &text, NULL);
5573 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
5574 XmStringFree (text);
5575 file = build_string (data);
5576 XtFree (data);
5578 else
5579 file = Qnil;
5581 UNBLOCK_INPUT;
5582 UNGCPRO;
5584 /* Make "Cancel" equivalent to C-g. */
5585 if (NILP (file))
5586 Fsignal (Qquit, Qnil);
5588 decoded_file = DECODE_FILE (file);
5590 return unbind_to (count, decoded_file);
5593 #endif /* USE_MOTIF */
5595 #ifdef USE_GTK
5597 static Lisp_Object
5598 clean_up_dialog (arg)
5599 Lisp_Object arg;
5601 x_menu_set_in_use (0);
5603 return Qnil;
5606 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5607 doc: /* Read file name, prompting with PROMPT in directory DIR.
5608 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5609 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5610 or directory must exist. If ONLY-DIR-P is non-nil, the user can only select
5611 directories. */)
5612 (prompt, dir, default_filename, mustmatch, only_dir_p)
5613 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
5615 FRAME_PTR f = SELECTED_FRAME ();
5616 char *fn;
5617 Lisp_Object file = Qnil;
5618 Lisp_Object decoded_file;
5619 int count = SPECPDL_INDEX ();
5620 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5621 char *cdef_file;
5623 check_x ();
5625 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5627 if (popup_activated ())
5628 error ("Trying to use a menu from within a menu-entry");
5630 CHECK_STRING (prompt);
5631 CHECK_STRING (dir);
5633 /* Prevent redisplay. */
5634 specbind (Qinhibit_redisplay, Qt);
5635 record_unwind_protect (clean_up_dialog, Qnil);
5637 BLOCK_INPUT;
5639 if (STRINGP (default_filename))
5640 cdef_file = SDATA (default_filename);
5641 else
5642 cdef_file = SDATA (dir);
5644 fn = xg_get_file_name (f, SDATA (prompt), cdef_file,
5645 ! NILP (mustmatch),
5646 ! NILP (only_dir_p));
5648 if (fn)
5650 file = build_string (fn);
5651 xfree (fn);
5654 UNBLOCK_INPUT;
5655 UNGCPRO;
5657 /* Make "Cancel" equivalent to C-g. */
5658 if (NILP (file))
5659 Fsignal (Qquit, Qnil);
5661 decoded_file = DECODE_FILE (file);
5663 return unbind_to (count, decoded_file);
5667 #ifdef HAVE_FREETYPE
5669 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5670 doc: /* Read a font name using a GTK font selection dialog.
5671 Return a GTK-style font string corresponding to the selection.
5673 If FRAME is omitted or nil, it defaults to the selected frame. */)
5674 (frame, ignored)
5675 Lisp_Object frame, ignored;
5677 FRAME_PTR f = check_x_frame (frame);
5678 char *name;
5679 Lisp_Object font;
5680 Lisp_Object font_param;
5681 char *default_name = NULL;
5682 struct gcpro gcpro1, gcpro2;
5683 int count = SPECPDL_INDEX ();
5685 check_x ();
5687 if (popup_activated ())
5688 error ("Trying to use a menu from within a menu-entry");
5690 /* Prevent redisplay. */
5691 specbind (Qinhibit_redisplay, Qt);
5692 record_unwind_protect (clean_up_dialog, Qnil);
5694 BLOCK_INPUT;
5696 GCPRO2(font_param, font);
5698 XSETFONT (font, FRAME_FONT (f));
5699 font_param = Ffont_get (font, intern (":name"));
5700 if (STRINGP (font_param))
5701 default_name = xstrdup (SDATA (font_param));
5702 else
5704 font_param = Fframe_parameter (frame, Qfont_param);
5705 if (STRINGP (font_param))
5706 default_name = xstrdup (SDATA (font_param));
5709 if (default_name == NULL && x_last_font_name != NULL)
5710 default_name = xstrdup (x_last_font_name);
5712 /* Convert fontconfig names to Gtk names, i.e. remove - before number */
5713 if (default_name)
5715 char *p = strrchr (default_name, '-');
5716 if (p)
5718 char *ep = p+1;
5719 while (isdigit (*ep))
5720 ++ep;
5721 if (*ep == '\0') *p = ' ';
5725 name = xg_get_font_name (f, default_name);
5726 xfree (default_name);
5728 if (name)
5730 font = build_string (name);
5731 g_free (x_last_font_name);
5732 x_last_font_name = name;
5735 UNBLOCK_INPUT;
5737 if (NILP (font))
5738 Fsignal (Qquit, Qnil);
5740 return unbind_to (count, font);
5742 #endif /* HAVE_FREETYPE */
5744 #endif /* USE_GTK */
5747 /***********************************************************************
5748 Keyboard
5749 ***********************************************************************/
5751 #ifdef HAVE_XKBGETKEYBOARD
5752 #include <X11/XKBlib.h>
5753 #include <X11/keysym.h>
5754 #endif
5756 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5757 Sx_backspace_delete_keys_p, 0, 1, 0,
5758 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5759 FRAME nil means use the selected frame.
5760 Value is t if we know that both keys are present, and are mapped to the
5761 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5762 present and mapped to the usual X keysyms. */)
5763 (frame)
5764 Lisp_Object frame;
5766 #ifdef HAVE_XKBGETKEYBOARD
5767 XkbDescPtr kb;
5768 struct frame *f = check_x_frame (frame);
5769 Display *dpy = FRAME_X_DISPLAY (f);
5770 Lisp_Object have_keys;
5771 int major, minor, op, event, error;
5773 BLOCK_INPUT;
5775 /* Check library version in case we're dynamically linked. */
5776 major = XkbMajorVersion;
5777 minor = XkbMinorVersion;
5778 if (!XkbLibraryVersion (&major, &minor))
5780 UNBLOCK_INPUT;
5781 return Qlambda;
5784 /* Check that the server supports XKB. */
5785 major = XkbMajorVersion;
5786 minor = XkbMinorVersion;
5787 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
5789 UNBLOCK_INPUT;
5790 return Qlambda;
5793 /* In this code we check that the keyboard has physical keys with names
5794 that start with BKSP (Backspace) and DELE (Delete), and that they
5795 generate keysym XK_BackSpace and XK_Delete respectively.
5796 This function is used to test if normal-erase-is-backspace should be
5797 turned on.
5798 An alternative approach would be to just check if XK_BackSpace and
5799 XK_Delete are mapped to any key. But if any of those are mapped to
5800 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5801 user doesn't know about it, it is better to return false here.
5802 It is more obvious to the user what to do if she/he has two keys
5803 clearly marked with names/symbols and one key does something not
5804 expected (i.e. she/he then tries the other).
5805 The cases where Backspace/Delete is mapped to some other key combination
5806 are rare, and in those cases, normal-erase-is-backspace can be turned on
5807 manually. */
5809 have_keys = Qnil;
5810 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5811 if (kb)
5813 int delete_keycode = 0, backspace_keycode = 0, i;
5815 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5817 for (i = kb->min_key_code;
5818 (i < kb->max_key_code
5819 && (delete_keycode == 0 || backspace_keycode == 0));
5820 ++i)
5822 /* The XKB symbolic key names can be seen most easily in
5823 the PS file generated by `xkbprint -label name
5824 $DISPLAY'. */
5825 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5826 delete_keycode = i;
5827 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5828 backspace_keycode = i;
5831 XkbFreeNames (kb, 0, True);
5834 XkbFreeClientMap (kb, 0, True);
5836 if (delete_keycode
5837 && backspace_keycode
5838 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5839 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5840 have_keys = Qt;
5842 UNBLOCK_INPUT;
5843 return have_keys;
5844 #else /* not HAVE_XKBGETKEYBOARD */
5845 return Qlambda;
5846 #endif /* not HAVE_XKBGETKEYBOARD */
5851 /***********************************************************************
5852 Initialization
5853 ***********************************************************************/
5855 /* Keep this list in the same order as frame_parms in frame.c.
5856 Use 0 for unsupported frame parameters. */
5858 frame_parm_handler x_frame_parm_handlers[] =
5860 x_set_autoraise,
5861 x_set_autolower,
5862 x_set_background_color,
5863 x_set_border_color,
5864 x_set_border_width,
5865 x_set_cursor_color,
5866 x_set_cursor_type,
5867 x_set_font,
5868 x_set_foreground_color,
5869 x_set_icon_name,
5870 x_set_icon_type,
5871 x_set_internal_border_width,
5872 x_set_menu_bar_lines,
5873 x_set_mouse_color,
5874 x_explicitly_set_name,
5875 x_set_scroll_bar_width,
5876 x_set_title,
5877 x_set_unsplittable,
5878 x_set_vertical_scroll_bars,
5879 x_set_visibility,
5880 x_set_tool_bar_lines,
5881 x_set_scroll_bar_foreground,
5882 x_set_scroll_bar_background,
5883 x_set_screen_gamma,
5884 x_set_line_spacing,
5885 x_set_fringe_width,
5886 x_set_fringe_width,
5887 x_set_wait_for_wm,
5888 x_set_fullscreen,
5889 x_set_font_backend,
5890 x_set_alpha,
5891 x_set_sticky,
5894 void
5895 syms_of_xfns ()
5897 /* This is zero if not using X windows. */
5898 x_in_use = 0;
5900 /* The section below is built by the lisp expression at the top of the file,
5901 just above where these variables are declared. */
5902 /*&&& init symbols here &&&*/
5903 Qnone = intern_c_string ("none");
5904 staticpro (&Qnone);
5905 Qsuppress_icon = intern_c_string ("suppress-icon");
5906 staticpro (&Qsuppress_icon);
5907 Qundefined_color = intern_c_string ("undefined-color");
5908 staticpro (&Qundefined_color);
5909 Qcompound_text = intern_c_string ("compound-text");
5910 staticpro (&Qcompound_text);
5911 Qcancel_timer = intern_c_string ("cancel-timer");
5912 staticpro (&Qcancel_timer);
5913 Qfont_param = intern_c_string ("font-parameter");
5914 staticpro (&Qfont_param);
5915 /* This is the end of symbol initialization. */
5917 /* Text property `display' should be nonsticky by default. */
5918 Vtext_property_default_nonsticky
5919 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
5922 Fput (Qundefined_color, Qerror_conditions,
5923 pure_cons (Qundefined_color, pure_cons (Qerror, Qnil)));
5924 Fput (Qundefined_color, Qerror_message,
5925 make_pure_c_string ("Undefined color"));
5927 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
5928 doc: /* The shape of the pointer when over text.
5929 Changing the value does not affect existing frames
5930 unless you set the mouse color. */);
5931 Vx_pointer_shape = Qnil;
5933 #if 0 /* This doesn't really do anything. */
5934 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
5935 doc: /* The shape of the pointer when not over text.
5936 This variable takes effect when you create a new frame
5937 or when you set the mouse color. */);
5938 #endif
5939 Vx_nontext_pointer_shape = Qnil;
5941 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
5942 doc: /* The shape of the pointer when Emacs is busy.
5943 This variable takes effect when you create a new frame
5944 or when you set the mouse color. */);
5945 Vx_hourglass_pointer_shape = Qnil;
5947 #if 0 /* This doesn't really do anything. */
5948 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
5949 doc: /* The shape of the pointer when over the mode line.
5950 This variable takes effect when you create a new frame
5951 or when you set the mouse color. */);
5952 #endif
5953 Vx_mode_pointer_shape = Qnil;
5955 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
5956 &Vx_sensitive_text_pointer_shape,
5957 doc: /* The shape of the pointer when over mouse-sensitive text.
5958 This variable takes effect when you create a new frame
5959 or when you set the mouse color. */);
5960 Vx_sensitive_text_pointer_shape = Qnil;
5962 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
5963 &Vx_window_horizontal_drag_shape,
5964 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
5965 This variable takes effect when you create a new frame
5966 or when you set the mouse color. */);
5967 Vx_window_horizontal_drag_shape = Qnil;
5969 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
5970 doc: /* A string indicating the foreground color of the cursor box. */);
5971 Vx_cursor_fore_pixel = Qnil;
5973 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
5974 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
5975 Text larger than this is clipped. */);
5976 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
5978 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
5979 doc: /* Non-nil if no X window manager is in use.
5980 Emacs doesn't try to figure this out; this is always nil
5981 unless you set it to something else. */);
5982 /* We don't have any way to find this out, so set it to nil
5983 and maybe the user would like to set it to t. */
5984 Vx_no_window_manager = Qnil;
5986 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
5987 &Vx_pixel_size_width_font_regexp,
5988 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
5990 Since Emacs gets width of a font matching with this regexp from
5991 PIXEL_SIZE field of the name, font finding mechanism gets faster for
5992 such a font. This is especially effective for such large fonts as
5993 Chinese, Japanese, and Korean. */);
5994 Vx_pixel_size_width_font_regexp = Qnil;
5996 /* This is not ifdef:ed, so other builds than GTK can customize it. */
5997 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", &x_gtk_use_old_file_dialog,
5998 doc: /* *Non-nil means prompt with the old GTK file selection dialog.
5999 If nil or if the file selection dialog is not available, the new GTK file
6000 chooser is used instead. To turn off all file dialogs set the
6001 variable `use-file-dialog'. */);
6002 x_gtk_use_old_file_dialog = 0;
6004 DEFVAR_BOOL ("x-gtk-show-hidden-files", &x_gtk_show_hidden_files,
6005 doc: /* *If non-nil, the GTK file chooser will by default show hidden files.
6006 Note that this is just the default, there is a toggle button on the file
6007 chooser to show or not show hidden files on a case by case basis. */);
6008 x_gtk_show_hidden_files = 0;
6010 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", &x_gtk_file_dialog_help_text,
6011 doc: /* *If non-nil, the GTK file chooser will show additional help text.
6012 If more space for files in the file chooser dialog is wanted, set this to nil
6013 to turn the additional text off. */);
6014 x_gtk_file_dialog_help_text = 1;
6016 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", &x_gtk_whole_detached_tool_bar,
6017 doc: /* *If non-nil, a detached tool bar is shown in full.
6018 The default is to just show an arrow and pressing on that arrow shows
6019 the tool bar buttons. */);
6020 x_gtk_whole_detached_tool_bar = 0;
6022 Fprovide (intern_c_string ("x"), Qnil);
6024 #ifdef USE_X_TOOLKIT
6025 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6026 #ifdef USE_MOTIF
6027 Fprovide (intern_c_string ("motif"), Qnil);
6029 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
6030 doc: /* Version info for LessTif/Motif. */);
6031 Vmotif_version_string = build_string (XmVERSION_STRING);
6032 #endif /* USE_MOTIF */
6033 #endif /* USE_X_TOOLKIT */
6035 #ifdef USE_GTK
6036 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
6037 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
6038 But for a user it is a toolkit for X, and indeed, configure
6039 accepts --with-x-toolkit=gtk. */
6040 Fprovide (intern_c_string ("x-toolkit"), Qnil);
6041 Fprovide (intern_c_string ("gtk"), Qnil);
6043 DEFVAR_LISP ("gtk-version-string", &Vgtk_version_string,
6044 doc: /* Version info for GTK+. */);
6046 char gtk_version[40];
6047 g_snprintf (gtk_version, sizeof (gtk_version), "%u.%u.%u",
6048 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
6049 Vgtk_version_string = make_pure_string (gtk_version, strlen (gtk_version), strlen (gtk_version), 0);
6051 #endif /* USE_GTK */
6053 /* X window properties. */
6054 defsubr (&Sx_change_window_property);
6055 defsubr (&Sx_delete_window_property);
6056 defsubr (&Sx_window_property);
6058 defsubr (&Sxw_display_color_p);
6059 defsubr (&Sx_display_grayscale_p);
6060 defsubr (&Sxw_color_defined_p);
6061 defsubr (&Sxw_color_values);
6062 defsubr (&Sx_server_max_request_size);
6063 defsubr (&Sx_server_vendor);
6064 defsubr (&Sx_server_version);
6065 defsubr (&Sx_display_pixel_width);
6066 defsubr (&Sx_display_pixel_height);
6067 defsubr (&Sx_display_mm_width);
6068 defsubr (&Sx_display_mm_height);
6069 defsubr (&Sx_display_screens);
6070 defsubr (&Sx_display_planes);
6071 defsubr (&Sx_display_color_cells);
6072 defsubr (&Sx_display_visual_class);
6073 defsubr (&Sx_display_backing_store);
6074 defsubr (&Sx_display_save_under);
6075 defsubr (&Sx_wm_set_size_hint);
6076 defsubr (&Sx_create_frame);
6077 defsubr (&Sx_open_connection);
6078 defsubr (&Sx_close_connection);
6079 defsubr (&Sx_display_list);
6080 defsubr (&Sx_synchronize);
6081 defsubr (&Sx_focus_frame);
6082 defsubr (&Sx_backspace_delete_keys_p);
6084 /* Setting callback functions for fontset handler. */
6085 check_window_system_func = check_x;
6087 defsubr (&Sx_show_tip);
6088 defsubr (&Sx_hide_tip);
6089 tip_timer = Qnil;
6090 staticpro (&tip_timer);
6091 tip_frame = Qnil;
6092 staticpro (&tip_frame);
6094 last_show_tip_args = Qnil;
6095 staticpro (&last_show_tip_args);
6097 defsubr (&Sx_uses_old_gtk_dialog);
6098 #if defined (USE_MOTIF) || defined (USE_GTK)
6099 defsubr (&Sx_file_dialog);
6100 #endif
6102 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
6103 defsubr (&Sx_select_font);
6104 x_last_font_name = NULL;
6105 #endif
6108 #endif /* HAVE_X_WINDOWS */
6110 /* arch-tag: 55040d02-5485-4d58-8b22-95a7a05f3288
6111 (do not change this comment) */