Merged from emacs@sv.gnu.org
[emacs.git] / src / xfns.c
blobb9b1a8fe3fcee8a32777172c7a23f6bec3baf822
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 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include <config.h>
23 #include <stdio.h>
24 #include <math.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
30 /* This makes the fields of a Display accessible, in Xlib header files. */
32 #define XLIB_ILLEGAL_ACCESS
34 #include "lisp.h"
35 #include "xterm.h"
36 #include "frame.h"
37 #include "window.h"
38 #include "buffer.h"
39 #include "intervals.h"
40 #include "dispextern.h"
41 #include "keyboard.h"
42 #include "blockinput.h"
43 #include <epaths.h>
44 #include "charset.h"
45 #include "coding.h"
46 #include "fontset.h"
47 #include "systime.h"
48 #include "termhooks.h"
49 #include "atimer.h"
50 #include "termchar.h"
52 #ifdef HAVE_X_WINDOWS
54 #include <ctype.h>
55 #include <sys/types.h>
56 #include <sys/stat.h>
58 #ifndef VMS
59 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
60 #include "bitmaps/gray.xbm"
61 #else
62 #include <X11/bitmaps/gray>
63 #endif
64 #else
65 #include "[.bitmaps]gray.xbm"
66 #endif
68 #ifdef USE_GTK
69 #include "gtkutil.h"
70 #endif
72 #ifdef USE_X_TOOLKIT
73 #include <X11/Shell.h>
75 #ifndef USE_MOTIF
76 #include <X11/Xaw/Paned.h>
77 #include <X11/Xaw/Label.h>
78 #endif /* USE_MOTIF */
80 #ifdef USG
81 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
82 #include <X11/Xos.h>
83 #define USG
84 #else
85 #include <X11/Xos.h>
86 #endif
88 #include "widget.h"
90 #include "../lwlib/lwlib.h"
92 #ifdef USE_MOTIF
93 #include <Xm/Xm.h>
94 #include <Xm/DialogS.h>
95 #include <Xm/FileSB.h>
96 #endif
98 /* Do the EDITRES protocol if running X11R5
99 Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
101 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
102 #define HACK_EDITRES
103 extern void _XEditResCheckMessages ();
104 #endif /* R5 + Athena */
106 /* Unique id counter for widgets created by the Lucid Widget Library. */
108 extern LWLIB_ID widget_id_tick;
110 #ifdef USE_LUCID
111 /* This is part of a kludge--see lwlib/xlwmenu.c. */
112 extern XFontStruct *xlwmenu_default_font;
113 #endif
115 extern void free_frame_menubar ();
116 extern double atof ();
118 #ifdef USE_MOTIF
120 /* LessTif/Motif version info. */
122 static Lisp_Object Vmotif_version_string;
124 #endif /* USE_MOTIF */
126 #endif /* USE_X_TOOLKIT */
128 #ifdef USE_GTK
130 /* GTK+ version info */
132 static Lisp_Object Vgtk_version_string;
134 #endif /* USE_GTK */
136 #ifdef HAVE_X11R4
137 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
138 #else
139 #define MAXREQUEST(dpy) ((dpy)->max_request_size)
140 #endif
142 /* The gray bitmap `bitmaps/gray'. This is done because xterm.c uses
143 it, and including `bitmaps/gray' more than once is a problem when
144 config.h defines `static' as an empty replacement string. */
146 int gray_bitmap_width = gray_width;
147 int gray_bitmap_height = gray_height;
148 char *gray_bitmap_bits = gray_bits;
150 /* Non-zero means we're allowed to display an hourglass cursor. */
152 int display_hourglass_p;
154 /* Non-zero means prompt with the old GTK file selection dialog. */
156 int x_use_old_gtk_file_dialog;
158 /* If non-zero, by default show hidden files in the GTK file chooser. */
160 int x_gtk_show_hidden_files;
162 /* If non-zero, don't collapse to tool bar when it is detached. */
164 int x_gtk_whole_detached_tool_bar;
166 /* The background and shape of the mouse pointer, and shape when not
167 over text or in the modeline. */
169 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
170 Lisp_Object Vx_hourglass_pointer_shape;
172 /* The shape when over mouse-sensitive text. */
174 Lisp_Object Vx_sensitive_text_pointer_shape;
176 /* If non-nil, the pointer shape to indicate that windows can be
177 dragged horizontally. */
179 Lisp_Object Vx_window_horizontal_drag_shape;
181 /* Color of chars displayed in cursor box. */
183 Lisp_Object Vx_cursor_fore_pixel;
185 /* Nonzero if using X. */
187 static int x_in_use;
189 /* Non nil if no window manager is in use. */
191 Lisp_Object Vx_no_window_manager;
193 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
195 Lisp_Object Vx_pixel_size_width_font_regexp;
197 Lisp_Object Qnone;
198 Lisp_Object Qsuppress_icon;
199 Lisp_Object Qundefined_color;
200 Lisp_Object Qcompound_text, Qcancel_timer;
202 /* In dispnew.c */
204 extern Lisp_Object Vwindow_system_version;
206 /* The below are defined in frame.c. */
208 #if GLYPH_DEBUG
209 int image_cache_refcount, dpyinfo_refcount;
210 #endif
214 /* Error if we are not connected to X. */
216 void
217 check_x ()
219 if (! x_in_use)
220 error ("X windows are not in use or not initialized");
223 /* Nonzero if we can use mouse menus.
224 You should not call this unless HAVE_MENUS is defined. */
227 have_menus_p ()
229 return x_in_use;
232 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
233 and checking validity for X. */
235 FRAME_PTR
236 check_x_frame (frame)
237 Lisp_Object frame;
239 FRAME_PTR f;
241 if (NILP (frame))
242 frame = selected_frame;
243 CHECK_LIVE_FRAME (frame);
244 f = XFRAME (frame);
245 if (! FRAME_X_P (f))
246 error ("Non-X frame used");
247 return f;
250 /* Let the user specify an X display with a Lisp object.
251 OBJECT may be nil, a frame or a terminal id.
252 nil stands for the selected frame--or, if that is not an X frame,
253 the first X display on the list. */
255 struct x_display_info *
256 check_x_display_info (object)
257 Lisp_Object object;
259 struct x_display_info *dpyinfo = NULL;
261 if (NILP (object))
263 struct frame *sf = XFRAME (selected_frame);
265 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
266 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
267 else if (x_display_list != 0)
268 dpyinfo = x_display_list;
269 else
270 error ("X windows are not in use or not initialized");
272 else if (INTEGERP (object))
274 struct terminal *t = get_terminal (XINT (object), 1);
276 if (t->type != output_x_window)
277 error ("Terminal %d is not an X display", XINT (object));
279 dpyinfo = t->display_info.x;
281 else if (STRINGP (object))
282 dpyinfo = x_display_info_for_name (object);
283 else
285 FRAME_PTR f = check_x_frame (object);
286 dpyinfo = FRAME_X_DISPLAY_INFO (f);
289 return dpyinfo;
293 /* Return the Emacs frame-object corresponding to an X window.
294 It could be the frame's main window or an icon window. */
296 /* This function can be called during GC, so use GC_xxx type test macros. */
298 struct frame *
299 x_window_to_frame (dpyinfo, wdesc)
300 struct x_display_info *dpyinfo;
301 int wdesc;
303 Lisp_Object tail, frame;
304 struct frame *f;
306 if (wdesc == None) return 0;
308 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
310 frame = XCAR (tail);
311 if (!GC_FRAMEP (frame))
312 continue;
313 f = XFRAME (frame);
314 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
315 continue;
316 if (f->output_data.x->hourglass_window == wdesc)
317 return f;
318 #ifdef USE_X_TOOLKIT
319 if ((f->output_data.x->edit_widget
320 && XtWindow (f->output_data.x->edit_widget) == wdesc)
321 /* A tooltip frame? */
322 || (!f->output_data.x->edit_widget
323 && FRAME_X_WINDOW (f) == wdesc)
324 || f->output_data.x->icon_desc == wdesc)
325 return f;
326 #else /* not USE_X_TOOLKIT */
327 #ifdef USE_GTK
328 if (f->output_data.x->edit_widget)
330 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
331 struct x_output *x = f->output_data.x;
332 if (gwdesc != 0 && gwdesc == x->edit_widget)
333 return f;
335 #endif /* USE_GTK */
336 if (FRAME_X_WINDOW (f) == wdesc
337 || f->output_data.x->icon_desc == wdesc)
338 return f;
339 #endif /* not USE_X_TOOLKIT */
341 return 0;
344 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
345 /* Like x_window_to_frame but also compares the window with the widget's
346 windows. */
348 struct frame *
349 x_any_window_to_frame (dpyinfo, wdesc)
350 struct x_display_info *dpyinfo;
351 int wdesc;
353 Lisp_Object tail, frame;
354 struct frame *f, *found;
355 struct x_output *x;
357 if (wdesc == None) return NULL;
359 found = NULL;
360 for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
362 frame = XCAR (tail);
363 if (!GC_FRAMEP (frame))
364 continue;
366 f = XFRAME (frame);
367 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
369 /* This frame matches if the window is any of its widgets. */
370 x = f->output_data.x;
371 if (x->hourglass_window == wdesc)
372 found = f;
373 else if (x->widget)
375 #ifdef USE_GTK
376 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
377 if (gwdesc != 0
378 && (gwdesc == x->widget
379 || gwdesc == x->edit_widget
380 || gwdesc == x->vbox_widget
381 || gwdesc == x->menubar_widget))
382 found = f;
383 #else
384 if (wdesc == XtWindow (x->widget)
385 || wdesc == XtWindow (x->column_widget)
386 || wdesc == XtWindow (x->edit_widget))
387 found = f;
388 /* Match if the window is this frame's menubar. */
389 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
390 found = f;
391 #endif
393 else if (FRAME_X_WINDOW (f) == wdesc)
394 /* A tooltip frame. */
395 found = f;
399 return found;
402 /* Likewise, but exclude the menu bar widget. */
404 struct frame *
405 x_non_menubar_window_to_frame (dpyinfo, wdesc)
406 struct x_display_info *dpyinfo;
407 int wdesc;
409 Lisp_Object tail, frame;
410 struct frame *f;
411 struct x_output *x;
413 if (wdesc == None) return 0;
415 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
417 frame = XCAR (tail);
418 if (!GC_FRAMEP (frame))
419 continue;
420 f = XFRAME (frame);
421 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
422 continue;
423 x = f->output_data.x;
424 /* This frame matches if the window is any of its widgets. */
425 if (x->hourglass_window == wdesc)
426 return f;
427 else if (x->widget)
429 #ifdef USE_GTK
430 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
431 if (gwdesc != 0
432 && (gwdesc == x->widget
433 || gwdesc == x->edit_widget
434 || gwdesc == x->vbox_widget))
435 return f;
436 #else
437 if (wdesc == XtWindow (x->widget)
438 || wdesc == XtWindow (x->column_widget)
439 || wdesc == XtWindow (x->edit_widget))
440 return f;
441 #endif
443 else if (FRAME_X_WINDOW (f) == wdesc)
444 /* A tooltip frame. */
445 return f;
447 return 0;
450 /* Likewise, but consider only the menu bar widget. */
452 struct frame *
453 x_menubar_window_to_frame (dpyinfo, wdesc)
454 struct x_display_info *dpyinfo;
455 int wdesc;
457 Lisp_Object tail, frame;
458 struct frame *f;
459 struct x_output *x;
461 if (wdesc == None) return 0;
463 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
465 frame = XCAR (tail);
466 if (!GC_FRAMEP (frame))
467 continue;
468 f = XFRAME (frame);
469 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
470 continue;
471 x = f->output_data.x;
472 /* Match if the window is this frame's menubar. */
473 #ifdef USE_GTK
474 if (x->menubar_widget)
476 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
477 int found = 0;
479 BLOCK_INPUT;
480 if (gwdesc != 0
481 && (gwdesc == x->menubar_widget
482 || gtk_widget_get_parent (gwdesc) == x->menubar_widget))
483 found = 1;
484 UNBLOCK_INPUT;
485 if (found) return f;
487 #else
488 if (x->menubar_widget
489 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
490 return f;
491 #endif
493 return 0;
496 /* Return the frame whose principal (outermost) window is WDESC.
497 If WDESC is some other (smaller) window, we return 0. */
499 struct frame *
500 x_top_window_to_frame (dpyinfo, wdesc)
501 struct x_display_info *dpyinfo;
502 int wdesc;
504 Lisp_Object tail, frame;
505 struct frame *f;
506 struct x_output *x;
508 if (wdesc == None) return 0;
510 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
512 frame = XCAR (tail);
513 if (!GC_FRAMEP (frame))
514 continue;
515 f = XFRAME (frame);
516 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
517 continue;
518 x = f->output_data.x;
520 if (x->widget)
522 /* This frame matches if the window is its topmost widget. */
523 #ifdef USE_GTK
524 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
525 if (gwdesc == x->widget)
526 return f;
527 #else
528 if (wdesc == XtWindow (x->widget))
529 return f;
530 #if 0 /* I don't know why it did this,
531 but it seems logically wrong,
532 and it causes trouble for MapNotify events. */
533 /* Match if the window is this frame's menubar. */
534 if (x->menubar_widget
535 && wdesc == XtWindow (x->menubar_widget))
536 return f;
537 #endif
538 #endif
540 else if (FRAME_X_WINDOW (f) == wdesc)
541 /* Tooltip frame. */
542 return f;
544 return 0;
546 #endif /* USE_X_TOOLKIT || USE_GTK */
550 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
551 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
553 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
554 static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
555 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
556 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
557 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
558 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
559 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
560 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
561 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
562 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
563 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
564 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
565 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
566 void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
567 Lisp_Object));
568 void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
569 Lisp_Object));
570 static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
571 Lisp_Object,
572 Lisp_Object,
573 char *, char *,
574 int));
577 /* Store the screen positions of frame F into XPTR and YPTR.
578 These are the positions of the containing window manager window,
579 not Emacs's own window. */
581 void
582 x_real_positions (f, xptr, yptr)
583 FRAME_PTR f;
584 int *xptr, *yptr;
586 int win_x, win_y, outer_x, outer_y;
587 int real_x = 0, real_y = 0;
588 int had_errors = 0;
589 Window win = f->output_data.x->parent_desc;
591 int count;
593 BLOCK_INPUT;
595 count = x_catch_errors (FRAME_X_DISPLAY (f));
597 if (win == FRAME_X_DISPLAY_INFO (f)->root_window)
598 win = FRAME_OUTER_WINDOW (f);
600 /* This loop traverses up the containment tree until we hit the root
601 window. Window managers may intersect many windows between our window
602 and the root window. The window we find just before the root window
603 should be the outer WM window. */
604 for (;;)
606 Window wm_window, rootw;
607 Window *tmp_children;
608 unsigned int tmp_nchildren;
609 int success;
611 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
612 &wm_window, &tmp_children, &tmp_nchildren);
614 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
616 /* Don't free tmp_children if XQueryTree failed. */
617 if (! success)
618 break;
620 XFree ((char *) tmp_children);
622 if (wm_window == rootw || had_errors)
623 break;
625 win = wm_window;
628 if (! had_errors)
630 unsigned int ign;
631 Window child, rootw;
633 /* Get the real coordinates for the WM window upper left corner */
634 XGetGeometry (FRAME_X_DISPLAY (f), win,
635 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
637 /* Translate real coordinates to coordinates relative to our
638 window. For our window, the upper left corner is 0, 0.
639 Since the upper left corner of the WM window is outside
640 our window, win_x and win_y will be negative:
642 ------------------ ---> x
643 | title |
644 | ----------------- v y
645 | | our window
647 XTranslateCoordinates (FRAME_X_DISPLAY (f),
649 /* From-window, to-window. */
650 FRAME_X_DISPLAY_INFO (f)->root_window,
651 FRAME_X_WINDOW (f),
653 /* From-position, to-position. */
654 real_x, real_y, &win_x, &win_y,
656 /* Child of win. */
657 &child);
659 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
661 outer_x = win_x;
662 outer_y = win_y;
664 else
666 XTranslateCoordinates (FRAME_X_DISPLAY (f),
668 /* From-window, to-window. */
669 FRAME_X_DISPLAY_INFO (f)->root_window,
670 FRAME_OUTER_WINDOW (f),
672 /* From-position, to-position. */
673 real_x, real_y, &outer_x, &outer_y,
675 /* Child of win. */
676 &child);
679 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
682 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
684 UNBLOCK_INPUT;
686 if (had_errors) return;
688 f->x_pixels_diff = -win_x;
689 f->y_pixels_diff = -win_y;
691 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
692 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
694 *xptr = real_x;
695 *yptr = real_y;
701 /* Gamma-correct COLOR on frame F. */
703 void
704 gamma_correct (f, color)
705 struct frame *f;
706 XColor *color;
708 if (f->gamma)
710 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
711 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
712 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
717 /* Decide if color named COLOR_NAME is valid for use on frame F. If
718 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
719 allocate the color. Value is zero if COLOR_NAME is invalid, or
720 no color could be allocated. */
723 x_defined_color (f, color_name, color, alloc_p)
724 struct frame *f;
725 char *color_name;
726 XColor *color;
727 int alloc_p;
729 int success_p;
730 Display *dpy = FRAME_X_DISPLAY (f);
731 Colormap cmap = FRAME_X_COLORMAP (f);
733 BLOCK_INPUT;
734 success_p = XParseColor (dpy, cmap, color_name, color);
735 if (success_p && alloc_p)
736 success_p = x_alloc_nearest_color (f, cmap, color);
737 UNBLOCK_INPUT;
739 return success_p;
743 /* Return the pixel color value for color COLOR_NAME on frame F. If F
744 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
745 Signal an error if color can't be allocated. */
748 x_decode_color (f, color_name, mono_color)
749 FRAME_PTR f;
750 Lisp_Object color_name;
751 int mono_color;
753 XColor cdef;
755 CHECK_STRING (color_name);
757 #if 0 /* Don't do this. It's wrong when we're not using the default
758 colormap, it makes freeing difficult, and it's probably not
759 an important optimization. */
760 if (strcmp (SDATA (color_name), "black") == 0)
761 return BLACK_PIX_DEFAULT (f);
762 else if (strcmp (SDATA (color_name), "white") == 0)
763 return WHITE_PIX_DEFAULT (f);
764 #endif
766 /* Return MONO_COLOR for monochrome frames. */
767 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
768 return mono_color;
770 /* x_defined_color is responsible for coping with failures
771 by looking for a near-miss. */
772 if (x_defined_color (f, SDATA (color_name), &cdef, 1))
773 return cdef.pixel;
775 Fsignal (Qerror, Fcons (build_string ("Undefined color"),
776 Fcons (color_name, Qnil)));
777 return 0;
782 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
783 the previous value of that parameter, NEW_VALUE is the new value.
784 See also the comment of wait_for_wm in struct x_output. */
786 static void
787 x_set_wait_for_wm (f, new_value, old_value)
788 struct frame *f;
789 Lisp_Object new_value, old_value;
791 f->output_data.x->wait_for_wm = !NILP (new_value);
794 #ifdef USE_GTK
796 /* Set icon from FILE for frame F. By using GTK functions the icon
797 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
800 xg_set_icon (f, file)
801 FRAME_PTR f;
802 Lisp_Object file;
804 struct gcpro gcpro1;
805 int result = 0;
806 Lisp_Object found;
808 GCPRO1 (found);
810 found = x_find_image_file (file);
812 if (! NILP (found))
814 GdkPixbuf *pixbuf;
815 GError *err = NULL;
816 char *filename = (char *) SDATA (found);
817 BLOCK_INPUT;
819 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
821 if (pixbuf)
823 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
824 pixbuf);
825 g_object_unref (pixbuf);
827 result = 1;
829 else
830 g_error_free (err);
832 UNBLOCK_INPUT;
835 UNGCPRO;
836 return result;
840 xg_set_icon_from_xpm_data (f, data)
841 FRAME_PTR f;
842 char **data;
844 int result = 0;
845 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
847 if (!pixbuf)
848 return 0;
850 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
851 g_object_unref (pixbuf);
852 return 1;
854 #endif /* USE_GTK */
857 /* Functions called only from `x_set_frame_param'
858 to set individual parameters.
860 If FRAME_X_WINDOW (f) is 0,
861 the frame is being created and its X-window does not exist yet.
862 In that case, just record the parameter's new value
863 in the standard place; do not attempt to change the window. */
865 void
866 x_set_foreground_color (f, arg, oldval)
867 struct frame *f;
868 Lisp_Object arg, oldval;
870 struct x_output *x = f->output_data.x;
871 unsigned long fg, old_fg;
873 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
874 old_fg = x->foreground_pixel;
875 x->foreground_pixel = fg;
877 if (FRAME_X_WINDOW (f) != 0)
879 Display *dpy = FRAME_X_DISPLAY (f);
881 BLOCK_INPUT;
882 XSetForeground (dpy, x->normal_gc, fg);
883 XSetBackground (dpy, x->reverse_gc, fg);
885 if (x->cursor_pixel == old_fg)
887 unload_color (f, x->cursor_pixel);
888 x->cursor_pixel = x_copy_color (f, fg);
889 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
892 UNBLOCK_INPUT;
894 update_face_from_frame_parameter (f, Qforeground_color, arg);
896 if (FRAME_VISIBLE_P (f))
897 redraw_frame (f);
900 unload_color (f, old_fg);
903 void
904 x_set_background_color (f, arg, oldval)
905 struct frame *f;
906 Lisp_Object arg, oldval;
908 struct x_output *x = f->output_data.x;
909 unsigned long bg;
911 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
912 unload_color (f, x->background_pixel);
913 x->background_pixel = bg;
915 if (FRAME_X_WINDOW (f) != 0)
917 Display *dpy = FRAME_X_DISPLAY (f);
919 BLOCK_INPUT;
920 XSetBackground (dpy, x->normal_gc, bg);
921 XSetForeground (dpy, x->reverse_gc, bg);
922 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
923 XSetForeground (dpy, x->cursor_gc, bg);
925 #ifdef USE_GTK
926 xg_set_background_color (f, bg);
927 #endif
929 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
930 toolkit scroll bars. */
932 Lisp_Object bar;
933 for (bar = FRAME_SCROLL_BARS (f);
934 !NILP (bar);
935 bar = XSCROLL_BAR (bar)->next)
937 Window window = SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar));
938 XSetWindowBackground (dpy, window, bg);
941 #endif /* USE_TOOLKIT_SCROLL_BARS */
943 UNBLOCK_INPUT;
944 update_face_from_frame_parameter (f, Qbackground_color, arg);
946 if (FRAME_VISIBLE_P (f))
947 redraw_frame (f);
951 void
952 x_set_mouse_color (f, arg, oldval)
953 struct frame *f;
954 Lisp_Object arg, oldval;
956 struct x_output *x = f->output_data.x;
957 Display *dpy = FRAME_X_DISPLAY (f);
958 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
959 Cursor hourglass_cursor, horizontal_drag_cursor;
960 int count;
961 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
962 unsigned long mask_color = x->background_pixel;
964 /* Don't let pointers be invisible. */
965 if (mask_color == pixel)
967 x_free_colors (f, &pixel, 1);
968 pixel = x_copy_color (f, x->foreground_pixel);
971 unload_color (f, x->mouse_pixel);
972 x->mouse_pixel = pixel;
974 BLOCK_INPUT;
976 /* It's not okay to crash if the user selects a screwy cursor. */
977 count = x_catch_errors (dpy);
979 if (!NILP (Vx_pointer_shape))
981 CHECK_NUMBER (Vx_pointer_shape);
982 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
984 else
985 cursor = XCreateFontCursor (dpy, XC_xterm);
986 x_check_errors (dpy, "bad text pointer cursor: %s");
988 if (!NILP (Vx_nontext_pointer_shape))
990 CHECK_NUMBER (Vx_nontext_pointer_shape);
991 nontext_cursor
992 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
994 else
995 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
996 x_check_errors (dpy, "bad nontext pointer cursor: %s");
998 if (!NILP (Vx_hourglass_pointer_shape))
1000 CHECK_NUMBER (Vx_hourglass_pointer_shape);
1001 hourglass_cursor
1002 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
1004 else
1005 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
1006 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
1008 if (!NILP (Vx_mode_pointer_shape))
1010 CHECK_NUMBER (Vx_mode_pointer_shape);
1011 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
1013 else
1014 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
1015 x_check_errors (dpy, "bad modeline pointer cursor: %s");
1017 if (!NILP (Vx_sensitive_text_pointer_shape))
1019 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1020 hand_cursor
1021 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
1023 else
1024 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
1026 if (!NILP (Vx_window_horizontal_drag_shape))
1028 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1029 horizontal_drag_cursor
1030 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
1032 else
1033 horizontal_drag_cursor
1034 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
1036 /* Check and report errors with the above calls. */
1037 x_check_errors (dpy, "can't set cursor shape: %s");
1038 x_uncatch_errors (dpy, count);
1041 XColor fore_color, back_color;
1043 fore_color.pixel = x->mouse_pixel;
1044 x_query_color (f, &fore_color);
1045 back_color.pixel = mask_color;
1046 x_query_color (f, &back_color);
1048 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
1049 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
1050 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
1051 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
1052 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
1053 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
1056 if (FRAME_X_WINDOW (f) != 0)
1057 XDefineCursor (dpy, FRAME_X_WINDOW (f), cursor);
1059 if (cursor != x->text_cursor
1060 && x->text_cursor != 0)
1061 XFreeCursor (dpy, x->text_cursor);
1062 x->text_cursor = cursor;
1064 if (nontext_cursor != x->nontext_cursor
1065 && x->nontext_cursor != 0)
1066 XFreeCursor (dpy, x->nontext_cursor);
1067 x->nontext_cursor = nontext_cursor;
1069 if (hourglass_cursor != x->hourglass_cursor
1070 && x->hourglass_cursor != 0)
1071 XFreeCursor (dpy, x->hourglass_cursor);
1072 x->hourglass_cursor = hourglass_cursor;
1074 if (mode_cursor != x->modeline_cursor
1075 && x->modeline_cursor != 0)
1076 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
1077 x->modeline_cursor = mode_cursor;
1079 if (hand_cursor != x->hand_cursor
1080 && x->hand_cursor != 0)
1081 XFreeCursor (dpy, x->hand_cursor);
1082 x->hand_cursor = hand_cursor;
1084 if (horizontal_drag_cursor != x->horizontal_drag_cursor
1085 && x->horizontal_drag_cursor != 0)
1086 XFreeCursor (dpy, x->horizontal_drag_cursor);
1087 x->horizontal_drag_cursor = horizontal_drag_cursor;
1089 XFlush (dpy);
1090 UNBLOCK_INPUT;
1092 update_face_from_frame_parameter (f, Qmouse_color, arg);
1095 void
1096 x_set_cursor_color (f, arg, oldval)
1097 struct frame *f;
1098 Lisp_Object arg, oldval;
1100 unsigned long fore_pixel, pixel;
1101 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
1102 struct x_output *x = f->output_data.x;
1104 if (!NILP (Vx_cursor_fore_pixel))
1106 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1107 WHITE_PIX_DEFAULT (f));
1108 fore_pixel_allocated_p = 1;
1110 else
1111 fore_pixel = x->background_pixel;
1113 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1114 pixel_allocated_p = 1;
1116 /* Make sure that the cursor color differs from the background color. */
1117 if (pixel == x->background_pixel)
1119 if (pixel_allocated_p)
1121 x_free_colors (f, &pixel, 1);
1122 pixel_allocated_p = 0;
1125 pixel = x->mouse_pixel;
1126 if (pixel == fore_pixel)
1128 if (fore_pixel_allocated_p)
1130 x_free_colors (f, &fore_pixel, 1);
1131 fore_pixel_allocated_p = 0;
1133 fore_pixel = x->background_pixel;
1137 unload_color (f, x->cursor_foreground_pixel);
1138 if (!fore_pixel_allocated_p)
1139 fore_pixel = x_copy_color (f, fore_pixel);
1140 x->cursor_foreground_pixel = fore_pixel;
1142 unload_color (f, x->cursor_pixel);
1143 if (!pixel_allocated_p)
1144 pixel = x_copy_color (f, pixel);
1145 x->cursor_pixel = pixel;
1147 if (FRAME_X_WINDOW (f) != 0)
1149 BLOCK_INPUT;
1150 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1151 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
1152 UNBLOCK_INPUT;
1154 if (FRAME_VISIBLE_P (f))
1156 x_update_cursor (f, 0);
1157 x_update_cursor (f, 1);
1161 update_face_from_frame_parameter (f, Qcursor_color, arg);
1164 /* Set the border-color of frame F to pixel value PIX.
1165 Note that this does not fully take effect if done before
1166 F has an x-window. */
1168 void
1169 x_set_border_pixel (f, pix)
1170 struct frame *f;
1171 int pix;
1173 unload_color (f, f->output_data.x->border_pixel);
1174 f->output_data.x->border_pixel = pix;
1176 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
1178 BLOCK_INPUT;
1179 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1180 (unsigned long)pix);
1181 UNBLOCK_INPUT;
1183 if (FRAME_VISIBLE_P (f))
1184 redraw_frame (f);
1188 /* Set the border-color of frame F to value described by ARG.
1189 ARG can be a string naming a color.
1190 The border-color is used for the border that is drawn by the X server.
1191 Note that this does not fully take effect if done before
1192 F has an x-window; it must be redone when the window is created.
1194 Note: this is done in two routines because of the way X10 works.
1196 Note: under X11, this is normally the province of the window manager,
1197 and so emacs' border colors may be overridden. */
1199 void
1200 x_set_border_color (f, arg, oldval)
1201 struct frame *f;
1202 Lisp_Object arg, oldval;
1204 int pix;
1206 CHECK_STRING (arg);
1207 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1208 x_set_border_pixel (f, pix);
1209 update_face_from_frame_parameter (f, Qborder_color, arg);
1213 void
1214 x_set_cursor_type (f, arg, oldval)
1215 FRAME_PTR f;
1216 Lisp_Object arg, oldval;
1218 set_frame_cursor_types (f, arg);
1220 /* Make sure the cursor gets redrawn. */
1221 cursor_type_changed = 1;
1224 void
1225 x_set_icon_type (f, arg, oldval)
1226 struct frame *f;
1227 Lisp_Object arg, oldval;
1229 int result;
1231 if (STRINGP (arg))
1233 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1234 return;
1236 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1237 return;
1239 BLOCK_INPUT;
1240 if (NILP (arg))
1241 result = x_text_icon (f,
1242 (char *) SDATA ((!NILP (f->icon_name)
1243 ? f->icon_name
1244 : f->name)));
1245 else
1246 result = x_bitmap_icon (f, arg);
1248 if (result)
1250 UNBLOCK_INPUT;
1251 error ("No icon window available");
1254 XFlush (FRAME_X_DISPLAY (f));
1255 UNBLOCK_INPUT;
1258 void
1259 x_set_icon_name (f, arg, oldval)
1260 struct frame *f;
1261 Lisp_Object arg, oldval;
1263 int result;
1265 if (STRINGP (arg))
1267 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1268 return;
1270 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1271 return;
1273 f->icon_name = arg;
1275 if (f->output_data.x->icon_bitmap != 0)
1276 return;
1278 BLOCK_INPUT;
1280 result = x_text_icon (f,
1281 (char *) SDATA ((!NILP (f->icon_name)
1282 ? f->icon_name
1283 : !NILP (f->title)
1284 ? f->title
1285 : f->name)));
1287 if (result)
1289 UNBLOCK_INPUT;
1290 error ("No icon window available");
1293 XFlush (FRAME_X_DISPLAY (f));
1294 UNBLOCK_INPUT;
1298 void
1299 x_set_menu_bar_lines (f, value, oldval)
1300 struct frame *f;
1301 Lisp_Object value, oldval;
1303 int nlines;
1304 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1305 int olines = FRAME_MENU_BAR_LINES (f);
1306 #endif
1308 /* Right now, menu bars don't work properly in minibuf-only frames;
1309 most of the commands try to apply themselves to the minibuffer
1310 frame itself, and get an error because you can't switch buffers
1311 in or split the minibuffer window. */
1312 if (FRAME_MINIBUF_ONLY_P (f))
1313 return;
1315 if (INTEGERP (value))
1316 nlines = XINT (value);
1317 else
1318 nlines = 0;
1320 /* Make sure we redisplay all windows in this frame. */
1321 windows_or_buffers_changed++;
1323 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1324 FRAME_MENU_BAR_LINES (f) = 0;
1325 if (nlines)
1327 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1328 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1329 /* Make sure next redisplay shows the menu bar. */
1330 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
1332 else
1334 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1335 free_frame_menubar (f);
1336 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1337 if (FRAME_X_P (f))
1338 f->output_data.x->menubar_widget = 0;
1340 #else /* not USE_X_TOOLKIT && not USE_GTK */
1341 FRAME_MENU_BAR_LINES (f) = nlines;
1342 change_window_heights (f->root_window, nlines - olines);
1343 #endif /* not USE_X_TOOLKIT */
1344 adjust_glyphs (f);
1348 /* Set the number of lines used for the tool bar of frame F to VALUE.
1349 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1350 is the old number of tool bar lines. This function changes the
1351 height of all windows on frame F to match the new tool bar height.
1352 The frame's height doesn't change. */
1354 void
1355 x_set_tool_bar_lines (f, value, oldval)
1356 struct frame *f;
1357 Lisp_Object value, oldval;
1359 int delta, nlines, root_height;
1360 Lisp_Object root_window;
1362 /* Treat tool bars like menu bars. */
1363 if (FRAME_MINIBUF_ONLY_P (f))
1364 return;
1366 /* Use VALUE only if an integer >= 0. */
1367 if (INTEGERP (value) && XINT (value) >= 0)
1368 nlines = XFASTINT (value);
1369 else
1370 nlines = 0;
1372 #ifdef USE_GTK
1373 FRAME_TOOL_BAR_LINES (f) = 0;
1374 if (nlines)
1376 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1377 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1378 /* Make sure next redisplay shows the tool bar. */
1379 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
1380 update_frame_tool_bar (f);
1382 else
1384 if (FRAME_EXTERNAL_TOOL_BAR (f))
1385 free_frame_tool_bar (f);
1386 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1389 return;
1390 #endif
1392 /* Make sure we redisplay all windows in this frame. */
1393 ++windows_or_buffers_changed;
1395 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1397 /* Don't resize the tool-bar to more than we have room for. */
1398 root_window = FRAME_ROOT_WINDOW (f);
1399 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1400 if (root_height - delta < 1)
1402 delta = root_height - 1;
1403 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1406 FRAME_TOOL_BAR_LINES (f) = nlines;
1407 change_window_heights (root_window, delta);
1408 adjust_glyphs (f);
1410 /* We also have to make sure that the internal border at the top of
1411 the frame, below the menu bar or tool bar, is redrawn when the
1412 tool bar disappears. This is so because the internal border is
1413 below the tool bar if one is displayed, but is below the menu bar
1414 if there isn't a tool bar. The tool bar draws into the area
1415 below the menu bar. */
1416 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1418 clear_frame (f);
1419 clear_current_matrices (f);
1422 /* If the tool bar gets smaller, the internal border below it
1423 has to be cleared. It was formerly part of the display
1424 of the larger tool bar, and updating windows won't clear it. */
1425 if (delta < 0)
1427 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1428 int width = FRAME_PIXEL_WIDTH (f);
1429 int y = nlines * FRAME_LINE_HEIGHT (f);
1431 /* height can be zero here. */
1432 if (height > 0 && width > 0)
1434 BLOCK_INPUT;
1435 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1436 0, y, width, height, False);
1437 UNBLOCK_INPUT;
1440 if (WINDOWP (f->tool_bar_window))
1441 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1446 /* Set the foreground color for scroll bars on frame F to VALUE.
1447 VALUE should be a string, a color name. If it isn't a string or
1448 isn't a valid color name, do nothing. OLDVAL is the old value of
1449 the frame parameter. */
1451 void
1452 x_set_scroll_bar_foreground (f, value, oldval)
1453 struct frame *f;
1454 Lisp_Object value, oldval;
1456 unsigned long pixel;
1458 if (STRINGP (value))
1459 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1460 else
1461 pixel = -1;
1463 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1464 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1466 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1467 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1469 /* Remove all scroll bars because they have wrong colors. */
1470 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1471 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1472 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1473 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1475 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1476 redraw_frame (f);
1481 /* Set the background color for scroll bars on frame F to VALUE VALUE
1482 should be a string, a color name. If it isn't a string or isn't a
1483 valid color name, do nothing. OLDVAL is the old value of the frame
1484 parameter. */
1486 void
1487 x_set_scroll_bar_background (f, value, oldval)
1488 struct frame *f;
1489 Lisp_Object value, oldval;
1491 unsigned long pixel;
1493 if (STRINGP (value))
1494 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1495 else
1496 pixel = -1;
1498 if (f->output_data.x->scroll_bar_background_pixel != -1)
1499 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1501 #ifdef USE_TOOLKIT_SCROLL_BARS
1502 /* Scrollbar shadow colors. */
1503 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1505 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1506 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1508 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1510 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1511 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1513 #endif /* USE_TOOLKIT_SCROLL_BARS */
1515 f->output_data.x->scroll_bar_background_pixel = pixel;
1516 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1518 /* Remove all scroll bars because they have wrong colors. */
1519 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1520 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1521 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1522 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1524 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1525 redraw_frame (f);
1530 /* Encode Lisp string STRING as a text in a format appropriate for
1531 XICCC (X Inter Client Communication Conventions).
1533 If STRING contains only ASCII characters, do no conversion and
1534 return the string data of STRING. Otherwise, encode the text by
1535 CODING_SYSTEM, and return a newly allocated memory area which
1536 should be freed by `xfree' by a caller.
1538 SELECTIONP non-zero means the string is being encoded for an X
1539 selection, so it is safe to run pre-write conversions (which
1540 may run Lisp code).
1542 Store the byte length of resulting text in *TEXT_BYTES.
1544 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1545 which means that the `encoding' of the result can be `STRING'.
1546 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1547 the result should be `COMPOUND_TEXT'. */
1549 static unsigned char *
1550 x_encode_text (string, coding_system, selectionp, text_bytes, stringp, freep)
1551 Lisp_Object string, coding_system;
1552 int *text_bytes, *stringp;
1553 int selectionp;
1554 int *freep;
1556 unsigned char *str = SDATA (string);
1557 int chars = SCHARS (string);
1558 int bytes = SBYTES (string);
1559 int charset_info;
1560 int bufsize;
1561 unsigned char *buf;
1562 struct coding_system coding;
1563 extern Lisp_Object Qcompound_text_with_extensions;
1565 charset_info = find_charset_in_text (str, chars, bytes, NULL, Qnil);
1566 if (charset_info == 0)
1568 /* No multibyte character in OBJ. We need not encode it. */
1569 *text_bytes = bytes;
1570 *stringp = 1;
1571 *freep = 0;
1572 return str;
1575 setup_coding_system (coding_system, &coding);
1576 if (selectionp
1577 && SYMBOLP (coding.pre_write_conversion)
1578 && !NILP (Ffboundp (coding.pre_write_conversion)))
1580 string = run_pre_post_conversion_on_str (string, &coding, 1);
1581 str = SDATA (string);
1582 chars = SCHARS (string);
1583 bytes = SBYTES (string);
1585 coding.src_multibyte = 1;
1586 coding.dst_multibyte = 0;
1587 coding.mode |= CODING_MODE_LAST_BLOCK;
1588 if (coding.type == coding_type_iso2022)
1589 coding.flags |= CODING_FLAG_ISO_SAFE;
1590 /* We suppress producing escape sequences for composition. */
1591 coding.composing = COMPOSITION_DISABLED;
1592 bufsize = encoding_buffer_size (&coding, bytes);
1593 buf = (unsigned char *) xmalloc (bufsize);
1594 encode_coding (&coding, str, buf, bytes, bufsize);
1595 *text_bytes = coding.produced;
1596 *stringp = (charset_info == 1
1597 || (!EQ (coding_system, Qcompound_text)
1598 && !EQ (coding_system, Qcompound_text_with_extensions)));
1599 *freep = 1;
1600 return buf;
1604 /* Set the WM name to NAME for frame F. Also set the icon name.
1605 If the frame already has an icon name, use that, otherwise set the
1606 icon name to NAME. */
1608 static void
1609 x_set_name_internal (f, name)
1610 FRAME_PTR f;
1611 Lisp_Object name;
1613 if (FRAME_X_WINDOW (f))
1615 BLOCK_INPUT;
1616 #ifdef HAVE_X11R4
1618 XTextProperty text, icon;
1619 int bytes, stringp;
1620 int do_free_icon_value = 0, do_free_text_value = 0;
1621 Lisp_Object coding_system;
1623 coding_system = Qcompound_text;
1624 /* Note: Encoding strategy
1626 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1627 text.encoding. But, there are non-internationalized window
1628 managers which don't support that encoding. So, if NAME
1629 contains only ASCII and 8859-1 characters, encode it by
1630 iso-latin-1, and use "STRING" in text.encoding hoping that
1631 such window managers at least analyze this format correctly,
1632 i.e. treat 8-bit bytes as 8859-1 characters.
1634 We may also be able to use "UTF8_STRING" in text.encoding
1635 in the future which can encode all Unicode characters.
1636 But, for the moment, there's no way to know that the
1637 current window manager supports it or not. */
1638 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1639 &do_free_text_value);
1640 text.encoding = (stringp ? XA_STRING
1641 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1642 text.format = 8;
1643 text.nitems = bytes;
1645 if (NILP (f->icon_name))
1647 icon = text;
1649 else
1651 /* See the above comment "Note: Encoding strategy". */
1652 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1653 &bytes, &stringp, &do_free_icon_value);
1654 icon.encoding = (stringp ? XA_STRING
1655 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1656 icon.format = 8;
1657 icon.nitems = bytes;
1660 #ifdef USE_GTK
1661 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1662 (char *) SDATA (ENCODE_UTF_8 (name)));
1663 #else /* not USE_GTK */
1664 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1665 #endif /* not USE_GTK */
1667 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1669 if (do_free_icon_value)
1670 xfree (icon.value);
1671 if (do_free_text_value)
1672 xfree (text.value);
1674 #else /* not HAVE_X11R4 */
1675 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1676 SDATA (name));
1677 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1678 SDATA (name));
1679 #endif /* not HAVE_X11R4 */
1680 UNBLOCK_INPUT;
1684 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1685 x_id_name.
1687 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1688 name; if NAME is a string, set F's name to NAME and set
1689 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1691 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1692 suggesting a new name, which lisp code should override; if
1693 F->explicit_name is set, ignore the new name; otherwise, set it. */
1695 void
1696 x_set_name (f, name, explicit)
1697 struct frame *f;
1698 Lisp_Object name;
1699 int explicit;
1701 /* Make sure that requests from lisp code override requests from
1702 Emacs redisplay code. */
1703 if (explicit)
1705 /* If we're switching from explicit to implicit, we had better
1706 update the mode lines and thereby update the title. */
1707 if (f->explicit_name && NILP (name))
1708 update_mode_lines = 1;
1710 f->explicit_name = ! NILP (name);
1712 else if (f->explicit_name)
1713 return;
1715 /* If NAME is nil, set the name to the x_id_name. */
1716 if (NILP (name))
1718 /* Check for no change needed in this very common case
1719 before we do any consing. */
1720 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1721 SDATA (f->name)))
1722 return;
1723 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1725 else
1726 CHECK_STRING (name);
1728 /* Don't change the name if it's already NAME. */
1729 if (! NILP (Fstring_equal (name, f->name)))
1730 return;
1732 f->name = name;
1734 /* For setting the frame title, the title parameter should override
1735 the name parameter. */
1736 if (! NILP (f->title))
1737 name = f->title;
1739 x_set_name_internal (f, name);
1742 /* This function should be called when the user's lisp code has
1743 specified a name for the frame; the name will override any set by the
1744 redisplay code. */
1745 void
1746 x_explicitly_set_name (f, arg, oldval)
1747 FRAME_PTR f;
1748 Lisp_Object arg, oldval;
1750 x_set_name (f, arg, 1);
1753 /* This function should be called by Emacs redisplay code to set the
1754 name; names set this way will never override names set by the user's
1755 lisp code. */
1756 void
1757 x_implicitly_set_name (f, arg, oldval)
1758 FRAME_PTR f;
1759 Lisp_Object arg, oldval;
1761 x_set_name (f, arg, 0);
1764 /* Change the title of frame F to NAME.
1765 If NAME is nil, use the frame name as the title.
1767 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1768 name; if NAME is a string, set F's name to NAME and set
1769 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1771 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1772 suggesting a new name, which lisp code should override; if
1773 F->explicit_name is set, ignore the new name; otherwise, set it. */
1775 void
1776 x_set_title (f, name, old_name)
1777 struct frame *f;
1778 Lisp_Object name, old_name;
1780 /* Don't change the title if it's already NAME. */
1781 if (EQ (name, f->title))
1782 return;
1784 update_mode_lines = 1;
1786 f->title = name;
1788 if (NILP (name))
1789 name = f->name;
1790 else
1791 CHECK_STRING (name);
1793 x_set_name_internal (f, name);
1796 void
1797 x_set_scroll_bar_default_width (f)
1798 struct frame *f;
1800 int wid = FRAME_COLUMN_WIDTH (f);
1802 #ifdef USE_TOOLKIT_SCROLL_BARS
1803 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1804 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1805 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1806 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1807 #else
1808 /* Make the actual width at least 14 pixels and a multiple of a
1809 character width. */
1810 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1812 /* Use all of that space (aside from required margins) for the
1813 scroll bar. */
1814 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1815 #endif
1819 /* Record in frame F the specified or default value according to ALIST
1820 of the parameter named PROP (a Lisp symbol). If no value is
1821 specified for PROP, look for an X default for XPROP on the frame
1822 named NAME. If that is not found either, use the value DEFLT. */
1824 static Lisp_Object
1825 x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
1826 foreground_p)
1827 struct frame *f;
1828 Lisp_Object alist;
1829 Lisp_Object prop;
1830 char *xprop;
1831 char *xclass;
1832 int foreground_p;
1834 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1835 Lisp_Object tem;
1837 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1838 if (EQ (tem, Qunbound))
1840 #ifdef USE_TOOLKIT_SCROLL_BARS
1842 /* See if an X resource for the scroll bar color has been
1843 specified. */
1844 tem = display_x_get_resource (dpyinfo,
1845 build_string (foreground_p
1846 ? "foreground"
1847 : "background"),
1848 empty_string,
1849 build_string ("verticalScrollBar"),
1850 empty_string);
1851 if (!STRINGP (tem))
1853 /* If nothing has been specified, scroll bars will use a
1854 toolkit-dependent default. Because these defaults are
1855 difficult to get at without actually creating a scroll
1856 bar, use nil to indicate that no color has been
1857 specified. */
1858 tem = Qnil;
1861 #else /* not USE_TOOLKIT_SCROLL_BARS */
1863 tem = Qnil;
1865 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1868 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
1869 return tem;
1874 #if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
1876 Status
1877 XSetWMProtocols (dpy, w, protocols, count)
1878 Display *dpy;
1879 Window w;
1880 Atom *protocols;
1881 int count;
1883 Atom prop;
1884 prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
1885 if (prop == None) return False;
1886 XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
1887 (unsigned char *) protocols, count);
1888 return True;
1890 #endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
1892 #ifdef USE_X_TOOLKIT
1894 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1895 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1896 already be present because of the toolkit (Motif adds some of them,
1897 for example, but Xt doesn't). */
1899 static void
1900 hack_wm_protocols (f, widget)
1901 FRAME_PTR f;
1902 Widget widget;
1904 Display *dpy = XtDisplay (widget);
1905 Window w = XtWindow (widget);
1906 int need_delete = 1;
1907 int need_focus = 1;
1908 int need_save = 1;
1910 BLOCK_INPUT;
1912 Atom type;
1913 unsigned char *catoms;
1914 int format = 0;
1915 unsigned long nitems = 0;
1916 unsigned long bytes_after;
1918 if ((XGetWindowProperty (dpy, w,
1919 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1920 (long)0, (long)100, False, XA_ATOM,
1921 &type, &format, &nitems, &bytes_after,
1922 &catoms)
1923 == Success)
1924 && format == 32 && type == XA_ATOM)
1926 Atom *atoms = (Atom *) catoms;
1927 while (nitems > 0)
1929 nitems--;
1930 if (atoms[nitems]
1931 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1932 need_delete = 0;
1933 else if (atoms[nitems]
1934 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1935 need_focus = 0;
1936 else if (atoms[nitems]
1937 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1938 need_save = 0;
1941 if (catoms)
1942 XFree (catoms);
1945 Atom props [10];
1946 int count = 0;
1947 if (need_delete)
1948 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1949 if (need_focus)
1950 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1951 if (need_save)
1952 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1953 if (count)
1954 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1955 XA_ATOM, 32, PropModeAppend,
1956 (unsigned char *) props, count);
1958 UNBLOCK_INPUT;
1960 #endif
1964 /* Support routines for XIC (X Input Context). */
1966 #ifdef HAVE_X_I18N
1968 static XFontSet xic_create_xfontset P_ ((struct frame *, char *));
1969 static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
1972 /* Supported XIM styles, ordered by preference. */
1974 static XIMStyle supported_xim_styles[] =
1976 XIMPreeditPosition | XIMStatusArea,
1977 XIMPreeditPosition | XIMStatusNothing,
1978 XIMPreeditPosition | XIMStatusNone,
1979 XIMPreeditNothing | XIMStatusArea,
1980 XIMPreeditNothing | XIMStatusNothing,
1981 XIMPreeditNothing | XIMStatusNone,
1982 XIMPreeditNone | XIMStatusArea,
1983 XIMPreeditNone | XIMStatusNothing,
1984 XIMPreeditNone | XIMStatusNone,
1989 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1991 char xic_defaut_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1993 /* Create an Xt fontset spec from the name of a base font.
1994 If `motif' is True use the Motif syntax. */
1995 char *
1996 xic_create_fontsetname (base_fontname, motif)
1997 char *base_fontname;
1998 Bool motif;
2000 const char *sep = motif ? ";" : ",";
2001 char *fontsetname;
2003 /* Make a fontset name from the base font name. */
2004 if (xic_defaut_fontset == base_fontname)
2005 { /* There is no base font name, use the default. */
2006 int len = strlen (base_fontname) + 2;
2007 fontsetname = xmalloc (len);
2008 bzero (fontsetname, len);
2009 strcpy (fontsetname, base_fontname);
2011 else
2013 /* Make a fontset name from the base font name.
2014 The font set will be made of the following elements:
2015 - the base font.
2016 - the base font where the charset spec is replaced by -*-*.
2017 - the same but with the family also replaced with -*-*-. */
2018 char *p = base_fontname;
2019 int i;
2021 for (i = 0; *p; p++)
2022 if (*p == '-') i++;
2023 if (i != 14)
2024 { /* As the font name doesn't conform to XLFD, we can't
2025 modify it to generalize it to allcs and allfamilies.
2026 Use the specified font plus the default. */
2027 int len = strlen (base_fontname) + strlen (xic_defaut_fontset) + 3;
2028 fontsetname = xmalloc (len);
2029 bzero (fontsetname, len);
2030 strcpy (fontsetname, base_fontname);
2031 strcat (fontsetname, sep);
2032 strcat (fontsetname, xic_defaut_fontset);
2034 else
2036 int len;
2037 char *p1 = NULL, *p2 = NULL;
2038 char *font_allcs = NULL;
2039 char *font_allfamilies = NULL;
2040 char *font_all = NULL;
2041 char *allcs = "*-*-*-*-*-*-*";
2042 char *allfamilies = "-*-*-";
2043 char *all = "*-*-*-*-";
2045 for (i = 0, p = base_fontname; i < 8; p++)
2047 if (*p == '-')
2049 i++;
2050 if (i == 3)
2051 p1 = p + 1;
2052 else if (i == 7)
2053 p2 = p + 1;
2056 /* Build the font spec that matches all charsets. */
2057 len = p - base_fontname + strlen (allcs) + 1;
2058 font_allcs = (char *) alloca (len);
2059 bzero (font_allcs, len);
2060 bcopy (base_fontname, font_allcs, p - base_fontname);
2061 strcat (font_allcs, allcs);
2063 /* Build the font spec that matches all families. */
2064 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
2065 font_allfamilies = (char *) alloca (len);
2066 bzero (font_allfamilies, len);
2067 strcpy (font_allfamilies, allfamilies);
2068 bcopy (p1, font_allfamilies + strlen (allfamilies), p - p1);
2069 strcat (font_allfamilies, allcs);
2071 /* Build the font spec that matches all. */
2072 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
2073 font_all = (char *) alloca (len);
2074 bzero (font_all, len);
2075 strcpy (font_all, allfamilies);
2076 strcat (font_all, all);
2077 bcopy (p2, font_all + strlen (all) + strlen (allfamilies), p - p2);
2078 strcat (font_all, allcs);
2080 /* Build the actual font set name. */
2081 len = strlen (base_fontname) + strlen (font_allcs)
2082 + strlen (font_allfamilies) + strlen (font_all) + 5;
2083 fontsetname = xmalloc (len);
2084 bzero (fontsetname, len);
2085 strcpy (fontsetname, base_fontname);
2086 strcat (fontsetname, sep);
2087 strcat (fontsetname, font_allcs);
2088 strcat (fontsetname, sep);
2089 strcat (fontsetname, font_allfamilies);
2090 strcat (fontsetname, sep);
2091 strcat (fontsetname, font_all);
2094 if (motif)
2095 strcat (fontsetname, ":");
2096 return fontsetname;
2099 static XFontSet
2100 xic_create_xfontset (f, base_fontname)
2101 struct frame *f;
2102 char *base_fontname;
2104 XFontSet xfs = NULL;
2105 char **missing_list = NULL;
2106 int missing_count;
2107 char *def_string;
2108 Lisp_Object rest, frame;
2110 if (!base_fontname)
2111 base_fontname = xic_defaut_fontset;
2113 /* See if there is another frame already using same fontset. */
2114 FOR_EACH_FRAME (rest, frame)
2116 struct frame *cf = XFRAME (frame);
2117 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2118 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2119 && FRAME_XIC_BASE_FONTNAME (cf)
2120 && !strcmp (FRAME_XIC_BASE_FONTNAME (cf), base_fontname))
2122 xfs = FRAME_XIC_FONTSET (cf);
2123 break;
2127 if (!xfs)
2129 char *fontsetname = xic_create_fontsetname (base_fontname, False);
2131 /* New fontset. */
2132 xfs = XCreateFontSet (FRAME_X_DISPLAY (f),
2133 fontsetname, &missing_list,
2134 &missing_count, &def_string);
2135 if (missing_list)
2136 XFreeStringList (missing_list);
2137 xfree (fontsetname);
2140 if (FRAME_XIC_BASE_FONTNAME (f))
2141 xfree (FRAME_XIC_BASE_FONTNAME (f));
2142 FRAME_XIC_BASE_FONTNAME (f) = xstrdup (base_fontname);
2144 /* No need to free def_string. */
2145 return xfs;
2148 /* Free the X fontset of frame F if it is the last frame using it. */
2150 void
2151 xic_free_xfontset (f)
2152 struct frame *f;
2154 Lisp_Object rest, frame;
2155 int shared_p = 0;
2157 if (!FRAME_XIC_FONTSET (f))
2158 return;
2160 /* See if there is another frame sharing the same fontset. */
2161 FOR_EACH_FRAME (rest, frame)
2163 struct frame *cf = XFRAME (frame);
2164 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2165 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2166 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2168 shared_p = 1;
2169 break;
2173 if (!shared_p)
2174 /* The fontset is not used anymore. It is safe to free it. */
2175 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2177 if (FRAME_XIC_BASE_FONTNAME (f))
2178 xfree (FRAME_XIC_BASE_FONTNAME (f));
2179 FRAME_XIC_BASE_FONTNAME (f) = NULL;
2180 FRAME_XIC_FONTSET (f) = NULL;
2184 /* Value is the best input style, given user preferences USER (already
2185 checked to be supported by Emacs), and styles supported by the
2186 input method XIM. */
2188 static XIMStyle
2189 best_xim_style (user, xim)
2190 XIMStyles *user;
2191 XIMStyles *xim;
2193 int i, j;
2195 for (i = 0; i < user->count_styles; ++i)
2196 for (j = 0; j < xim->count_styles; ++j)
2197 if (user->supported_styles[i] == xim->supported_styles[j])
2198 return user->supported_styles[i];
2200 /* Return the default style. */
2201 return XIMPreeditNothing | XIMStatusNothing;
2204 /* Create XIC for frame F. */
2206 static XIMStyle xic_style;
2208 void
2209 create_frame_xic (f)
2210 struct frame *f;
2212 XIM xim;
2213 XIC xic = NULL;
2214 XFontSet xfs = NULL;
2216 if (FRAME_XIC (f))
2217 return;
2219 /* Create X fontset. */
2220 xfs = xic_create_xfontset
2221 (f, (FRAME_FONTSET (f) < 0) ? NULL
2222 : (char *) SDATA (fontset_ascii (FRAME_FONTSET (f))));
2224 xim = FRAME_X_XIM (f);
2225 if (xim)
2227 XRectangle s_area;
2228 XPoint spot;
2229 XVaNestedList preedit_attr;
2230 XVaNestedList status_attr;
2232 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2233 spot.x = 0; spot.y = 1;
2235 /* Determine XIC style. */
2236 if (xic_style == 0)
2238 XIMStyles supported_list;
2239 supported_list.count_styles = (sizeof supported_xim_styles
2240 / sizeof supported_xim_styles[0]);
2241 supported_list.supported_styles = supported_xim_styles;
2242 xic_style = best_xim_style (&supported_list,
2243 FRAME_X_XIM_STYLES (f));
2246 preedit_attr = XVaCreateNestedList (0,
2247 XNFontSet, xfs,
2248 XNForeground,
2249 FRAME_FOREGROUND_PIXEL (f),
2250 XNBackground,
2251 FRAME_BACKGROUND_PIXEL (f),
2252 (xic_style & XIMPreeditPosition
2253 ? XNSpotLocation
2254 : NULL),
2255 &spot,
2256 NULL);
2257 status_attr = XVaCreateNestedList (0,
2258 XNArea,
2259 &s_area,
2260 XNFontSet,
2261 xfs,
2262 XNForeground,
2263 FRAME_FOREGROUND_PIXEL (f),
2264 XNBackground,
2265 FRAME_BACKGROUND_PIXEL (f),
2266 NULL);
2268 xic = XCreateIC (xim,
2269 XNInputStyle, xic_style,
2270 XNClientWindow, FRAME_X_WINDOW (f),
2271 XNFocusWindow, FRAME_X_WINDOW (f),
2272 XNStatusAttributes, status_attr,
2273 XNPreeditAttributes, preedit_attr,
2274 NULL);
2275 XFree (preedit_attr);
2276 XFree (status_attr);
2279 FRAME_XIC (f) = xic;
2280 FRAME_XIC_STYLE (f) = xic_style;
2281 FRAME_XIC_FONTSET (f) = xfs;
2285 /* Destroy XIC and free XIC fontset of frame F, if any. */
2287 void
2288 free_frame_xic (f)
2289 struct frame *f;
2291 if (FRAME_XIC (f) == NULL)
2292 return;
2294 XDestroyIC (FRAME_XIC (f));
2295 xic_free_xfontset (f);
2297 FRAME_XIC (f) = NULL;
2301 /* Place preedit area for XIC of window W's frame to specified
2302 pixel position X/Y. X and Y are relative to window W. */
2304 void
2305 xic_set_preeditarea (w, x, y)
2306 struct window *w;
2307 int x, y;
2309 struct frame *f = XFRAME (w->frame);
2310 XVaNestedList attr;
2311 XPoint spot;
2313 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2314 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2315 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2316 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2317 XFree (attr);
2321 /* Place status area for XIC in bottom right corner of frame F.. */
2323 void
2324 xic_set_statusarea (f)
2325 struct frame *f;
2327 XIC xic = FRAME_XIC (f);
2328 XVaNestedList attr;
2329 XRectangle area;
2330 XRectangle *needed;
2332 /* Negotiate geometry of status area. If input method has existing
2333 status area, use its current size. */
2334 area.x = area.y = area.width = area.height = 0;
2335 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2336 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2337 XFree (attr);
2339 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2340 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2341 XFree (attr);
2343 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2345 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2346 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2347 XFree (attr);
2350 area.width = needed->width;
2351 area.height = needed->height;
2352 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2353 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2354 - FRAME_MENUBAR_HEIGHT (f)
2355 - FRAME_TOOLBAR_HEIGHT (f)
2356 - FRAME_INTERNAL_BORDER_WIDTH (f));
2357 XFree (needed);
2359 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2360 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2361 XFree (attr);
2365 /* Set X fontset for XIC of frame F, using base font name
2366 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2368 void
2369 xic_set_xfontset (f, base_fontname)
2370 struct frame *f;
2371 char *base_fontname;
2373 XVaNestedList attr;
2374 XFontSet xfs;
2376 xic_free_xfontset (f);
2378 xfs = xic_create_xfontset (f, base_fontname);
2380 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2381 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2382 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2383 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2384 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2385 XFree (attr);
2387 FRAME_XIC_FONTSET (f) = xfs;
2390 #endif /* HAVE_X_I18N */
2394 #ifdef USE_X_TOOLKIT
2396 /* Create and set up the X widget for frame F. */
2398 static void
2399 x_window (f, window_prompting, minibuffer_only)
2400 struct frame *f;
2401 long window_prompting;
2402 int minibuffer_only;
2404 XClassHint class_hints;
2405 XSetWindowAttributes attributes;
2406 unsigned long attribute_mask;
2407 Widget shell_widget;
2408 Widget pane_widget;
2409 Widget frame_widget;
2410 Arg al [25];
2411 int ac;
2413 BLOCK_INPUT;
2415 /* Use the resource name as the top-level widget name
2416 for looking up resources. Make a non-Lisp copy
2417 for the window manager, so GC relocation won't bother it.
2419 Elsewhere we specify the window name for the window manager. */
2422 char *str = (char *) SDATA (Vx_resource_name);
2423 f->namebuf = (char *) xmalloc (strlen (str) + 1);
2424 strcpy (f->namebuf, str);
2427 ac = 0;
2428 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2429 XtSetArg (al[ac], XtNinput, 1); ac++;
2430 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2431 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2432 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2433 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2434 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2435 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2436 applicationShellWidgetClass,
2437 FRAME_X_DISPLAY (f), al, ac);
2439 f->output_data.x->widget = shell_widget;
2440 /* maybe_set_screen_title_format (shell_widget); */
2442 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2443 (widget_value *) NULL,
2444 shell_widget, False,
2445 (lw_callback) NULL,
2446 (lw_callback) NULL,
2447 (lw_callback) NULL,
2448 (lw_callback) NULL);
2450 ac = 0;
2451 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2452 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2453 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2454 XtSetValues (pane_widget, al, ac);
2455 f->output_data.x->column_widget = pane_widget;
2457 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2458 the emacs screen when changing menubar. This reduces flickering. */
2460 ac = 0;
2461 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2462 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2463 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2464 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2465 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2466 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2467 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2468 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2469 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2470 al, ac);
2472 f->output_data.x->edit_widget = frame_widget;
2474 XtManageChild (frame_widget);
2476 /* Do some needed geometry management. */
2478 int len;
2479 char *tem, shell_position[32];
2480 Arg al[10];
2481 int ac = 0;
2482 int extra_borders = 0;
2483 int menubar_size
2484 = (f->output_data.x->menubar_widget
2485 ? (f->output_data.x->menubar_widget->core.height
2486 + f->output_data.x->menubar_widget->core.border_width)
2487 : 0);
2489 #if 0 /* Experimentally, we now get the right results
2490 for -geometry -0-0 without this. 24 Aug 96, rms. */
2491 if (FRAME_EXTERNAL_MENU_BAR (f))
2493 Dimension ibw = 0;
2494 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2495 menubar_size += ibw;
2497 #endif
2499 f->output_data.x->menubar_height = menubar_size;
2501 #ifndef USE_LUCID
2502 /* Motif seems to need this amount added to the sizes
2503 specified for the shell widget. The Athena/Lucid widgets don't.
2504 Both conclusions reached experimentally. -- rms. */
2505 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2506 &extra_borders, NULL);
2507 extra_borders *= 2;
2508 #endif
2510 /* Convert our geometry parameters into a geometry string
2511 and specify it.
2512 Note that we do not specify here whether the position
2513 is a user-specified or program-specified one.
2514 We pass that information later, in x_wm_set_size_hints. */
2516 int left = f->left_pos;
2517 int xneg = window_prompting & XNegative;
2518 int top = f->top_pos;
2519 int yneg = window_prompting & YNegative;
2520 if (xneg)
2521 left = -left;
2522 if (yneg)
2523 top = -top;
2525 if (window_prompting & USPosition)
2526 sprintf (shell_position, "=%dx%d%c%d%c%d",
2527 FRAME_PIXEL_WIDTH (f) + extra_borders,
2528 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2529 (xneg ? '-' : '+'), left,
2530 (yneg ? '-' : '+'), top);
2531 else
2533 sprintf (shell_position, "=%dx%d",
2534 FRAME_PIXEL_WIDTH (f) + extra_borders,
2535 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2537 /* Setting x and y when the position is not specified in
2538 the geometry string will set program position in the WM hints.
2539 If Emacs had just one program position, we could set it in
2540 fallback resources, but since each make-frame call can specify
2541 different program positions, this is easier. */
2542 XtSetArg (al[ac], XtNx, left); ac++;
2543 XtSetArg (al[ac], XtNy, top); ac++;
2547 len = strlen (shell_position) + 1;
2548 /* We don't free this because we don't know whether
2549 it is safe to free it while the frame exists.
2550 It isn't worth the trouble of arranging to free it
2551 when the frame is deleted. */
2552 tem = (char *) xmalloc (len);
2553 strncpy (tem, shell_position, len);
2554 XtSetArg (al[ac], XtNgeometry, tem); ac++;
2555 XtSetValues (shell_widget, al, ac);
2558 XtManageChild (pane_widget);
2559 XtRealizeWidget (shell_widget);
2561 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2563 validate_x_resource_name ();
2565 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2566 class_hints.res_class = (char *) SDATA (Vx_resource_class);
2567 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2569 #ifdef HAVE_X_I18N
2570 FRAME_XIC (f) = NULL;
2571 if (use_xim)
2572 create_frame_xic (f);
2573 #endif
2575 f->output_data.x->wm_hints.input = True;
2576 f->output_data.x->wm_hints.flags |= InputHint;
2577 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2578 &f->output_data.x->wm_hints);
2580 hack_wm_protocols (f, shell_widget);
2582 #ifdef HACK_EDITRES
2583 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2584 #endif
2586 /* Do a stupid property change to force the server to generate a
2587 PropertyNotify event so that the event_stream server timestamp will
2588 be initialized to something relevant to the time we created the window.
2590 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2591 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2592 XA_ATOM, 32, PropModeAppend,
2593 (unsigned char*) NULL, 0);
2595 /* Make all the standard events reach the Emacs frame. */
2596 attributes.event_mask = STANDARD_EVENT_SET;
2598 #ifdef HAVE_X_I18N
2599 if (FRAME_XIC (f))
2601 /* XIM server might require some X events. */
2602 unsigned long fevent = NoEventMask;
2603 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2604 attributes.event_mask |= fevent;
2606 #endif /* HAVE_X_I18N */
2608 attribute_mask = CWEventMask;
2609 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2610 attribute_mask, &attributes);
2612 XtMapWidget (frame_widget);
2614 /* x_set_name normally ignores requests to set the name if the
2615 requested name is the same as the current name. This is the one
2616 place where that assumption isn't correct; f->name is set, but
2617 the X server hasn't been told. */
2619 Lisp_Object name;
2620 int explicit = f->explicit_name;
2622 f->explicit_name = 0;
2623 name = f->name;
2624 f->name = Qnil;
2625 x_set_name (f, name, explicit);
2628 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2629 f->output_data.x->text_cursor);
2631 UNBLOCK_INPUT;
2633 /* This is a no-op, except under Motif. Make sure main areas are
2634 set to something reasonable, in case we get an error later. */
2635 lw_set_main_areas (pane_widget, 0, frame_widget);
2638 #else /* not USE_X_TOOLKIT */
2639 #ifdef USE_GTK
2640 void
2641 x_window (f)
2642 FRAME_PTR f;
2644 if (! xg_create_frame_widgets (f))
2645 error ("Unable to create window");
2647 #ifdef HAVE_X_I18N
2648 FRAME_XIC (f) = NULL;
2649 if (use_xim)
2651 BLOCK_INPUT;
2652 create_frame_xic (f);
2653 if (FRAME_XIC (f))
2655 /* XIM server might require some X events. */
2656 unsigned long fevent = NoEventMask;
2657 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2659 if (fevent != NoEventMask)
2661 XSetWindowAttributes attributes;
2662 XWindowAttributes wattr;
2663 unsigned long attribute_mask;
2665 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2666 &wattr);
2667 attributes.event_mask = wattr.your_event_mask | fevent;
2668 attribute_mask = CWEventMask;
2669 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2670 attribute_mask, &attributes);
2673 UNBLOCK_INPUT;
2675 #endif
2678 #else /*! USE_GTK */
2679 /* Create and set up the X window for frame F. */
2681 void
2682 x_window (f)
2683 struct frame *f;
2686 XClassHint class_hints;
2687 XSetWindowAttributes attributes;
2688 unsigned long attribute_mask;
2690 attributes.background_pixel = f->output_data.x->background_pixel;
2691 attributes.border_pixel = f->output_data.x->border_pixel;
2692 attributes.bit_gravity = StaticGravity;
2693 attributes.backing_store = NotUseful;
2694 attributes.save_under = True;
2695 attributes.event_mask = STANDARD_EVENT_SET;
2696 attributes.colormap = FRAME_X_COLORMAP (f);
2697 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2698 | CWColormap);
2700 BLOCK_INPUT;
2701 FRAME_X_WINDOW (f)
2702 = XCreateWindow (FRAME_X_DISPLAY (f),
2703 f->output_data.x->parent_desc,
2704 f->left_pos,
2705 f->top_pos,
2706 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2707 f->border_width,
2708 CopyFromParent, /* depth */
2709 InputOutput, /* class */
2710 FRAME_X_VISUAL (f),
2711 attribute_mask, &attributes);
2713 #ifdef HAVE_X_I18N
2714 if (use_xim)
2716 create_frame_xic (f);
2717 if (FRAME_XIC (f))
2719 /* XIM server might require some X events. */
2720 unsigned long fevent = NoEventMask;
2721 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2722 attributes.event_mask |= fevent;
2723 attribute_mask = CWEventMask;
2724 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2725 attribute_mask, &attributes);
2728 #endif /* HAVE_X_I18N */
2730 validate_x_resource_name ();
2732 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2733 class_hints.res_class = (char *) SDATA (Vx_resource_class);
2734 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2736 /* The menubar is part of the ordinary display;
2737 it does not count in addition to the height of the window. */
2738 f->output_data.x->menubar_height = 0;
2740 /* This indicates that we use the "Passive Input" input model.
2741 Unless we do this, we don't get the Focus{In,Out} events that we
2742 need to draw the cursor correctly. Accursed bureaucrats.
2743 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2745 f->output_data.x->wm_hints.input = True;
2746 f->output_data.x->wm_hints.flags |= InputHint;
2747 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2748 &f->output_data.x->wm_hints);
2749 f->output_data.x->wm_hints.icon_pixmap = None;
2751 /* Request "save yourself" and "delete window" commands from wm. */
2753 Atom protocols[2];
2754 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2755 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2756 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2759 /* x_set_name normally ignores requests to set the name if the
2760 requested name is the same as the current name. This is the one
2761 place where that assumption isn't correct; f->name is set, but
2762 the X server hasn't been told. */
2764 Lisp_Object name;
2765 int explicit = f->explicit_name;
2767 f->explicit_name = 0;
2768 name = f->name;
2769 f->name = Qnil;
2770 x_set_name (f, name, explicit);
2773 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2774 f->output_data.x->text_cursor);
2776 UNBLOCK_INPUT;
2778 if (FRAME_X_WINDOW (f) == 0)
2779 error ("Unable to create window");
2782 #endif /* not USE_GTK */
2783 #endif /* not USE_X_TOOLKIT */
2785 /* Verify that the icon position args for this window are valid. */
2787 static void
2788 x_icon_verify (f, parms)
2789 struct frame *f;
2790 Lisp_Object parms;
2792 Lisp_Object icon_x, icon_y;
2794 /* Set the position of the icon. Note that twm groups all
2795 icons in an icon window. */
2796 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2797 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2798 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2800 CHECK_NUMBER (icon_x);
2801 CHECK_NUMBER (icon_y);
2803 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2804 error ("Both left and top icon corners of icon must be specified");
2807 /* Handle the icon stuff for this window. Perhaps later we might
2808 want an x_set_icon_position which can be called interactively as
2809 well. */
2811 static void
2812 x_icon (f, parms)
2813 struct frame *f;
2814 Lisp_Object parms;
2816 Lisp_Object icon_x, icon_y;
2817 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2819 /* Set the position of the icon. Note that twm groups all
2820 icons in an icon window. */
2821 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2822 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2823 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2825 CHECK_NUMBER (icon_x);
2826 CHECK_NUMBER (icon_y);
2828 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2829 error ("Both left and top icon corners of icon must be specified");
2831 BLOCK_INPUT;
2833 if (! EQ (icon_x, Qunbound))
2834 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2836 /* Start up iconic or window? */
2837 x_wm_set_window_state
2838 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2839 Qicon)
2840 ? IconicState
2841 : NormalState));
2843 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
2844 ? f->icon_name
2845 : f->name)));
2847 UNBLOCK_INPUT;
2850 /* Make the GCs needed for this window, setting the
2851 background, border and mouse colors; also create the
2852 mouse cursor and the gray border tile. */
2854 static char cursor_bits[] =
2856 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2857 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2858 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2859 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2862 static void
2863 x_make_gc (f)
2864 struct frame *f;
2866 XGCValues gc_values;
2868 BLOCK_INPUT;
2870 /* Create the GCs of this frame.
2871 Note that many default values are used. */
2873 /* Normal video */
2874 gc_values.font = FRAME_FONT (f)->fid;
2875 gc_values.foreground = f->output_data.x->foreground_pixel;
2876 gc_values.background = f->output_data.x->background_pixel;
2877 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2878 f->output_data.x->normal_gc
2879 = XCreateGC (FRAME_X_DISPLAY (f),
2880 FRAME_X_WINDOW (f),
2881 GCLineWidth | GCFont | GCForeground | GCBackground,
2882 &gc_values);
2884 /* Reverse video style. */
2885 gc_values.foreground = f->output_data.x->background_pixel;
2886 gc_values.background = f->output_data.x->foreground_pixel;
2887 f->output_data.x->reverse_gc
2888 = XCreateGC (FRAME_X_DISPLAY (f),
2889 FRAME_X_WINDOW (f),
2890 GCFont | GCForeground | GCBackground | GCLineWidth,
2891 &gc_values);
2893 /* Cursor has cursor-color background, background-color foreground. */
2894 gc_values.foreground = f->output_data.x->background_pixel;
2895 gc_values.background = f->output_data.x->cursor_pixel;
2896 gc_values.fill_style = FillOpaqueStippled;
2897 gc_values.stipple
2898 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
2899 FRAME_X_DISPLAY_INFO (f)->root_window,
2900 cursor_bits, 16, 16);
2901 f->output_data.x->cursor_gc
2902 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2903 (GCFont | GCForeground | GCBackground
2904 | GCFillStyle /* | GCStipple */ | GCLineWidth),
2905 &gc_values);
2907 /* Reliefs. */
2908 f->output_data.x->white_relief.gc = 0;
2909 f->output_data.x->black_relief.gc = 0;
2911 /* Create the gray border tile used when the pointer is not in
2912 the frame. Since this depends on the frame's pixel values,
2913 this must be done on a per-frame basis. */
2914 f->output_data.x->border_tile
2915 = (XCreatePixmapFromBitmapData
2916 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2917 gray_bits, gray_width, gray_height,
2918 f->output_data.x->foreground_pixel,
2919 f->output_data.x->background_pixel,
2920 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2922 UNBLOCK_INPUT;
2926 /* Free what was was allocated in x_make_gc. */
2928 void
2929 x_free_gcs (f)
2930 struct frame *f;
2932 Display *dpy = FRAME_X_DISPLAY (f);
2934 BLOCK_INPUT;
2936 if (f->output_data.x->normal_gc)
2938 XFreeGC (dpy, f->output_data.x->normal_gc);
2939 f->output_data.x->normal_gc = 0;
2942 if (f->output_data.x->reverse_gc)
2944 XFreeGC (dpy, f->output_data.x->reverse_gc);
2945 f->output_data.x->reverse_gc = 0;
2948 if (f->output_data.x->cursor_gc)
2950 XFreeGC (dpy, f->output_data.x->cursor_gc);
2951 f->output_data.x->cursor_gc = 0;
2954 if (f->output_data.x->border_tile)
2956 XFreePixmap (dpy, f->output_data.x->border_tile);
2957 f->output_data.x->border_tile = 0;
2960 UNBLOCK_INPUT;
2964 /* Handler for signals raised during x_create_frame and
2965 x_create_top_frame. FRAME is the frame which is partially
2966 constructed. */
2968 static Lisp_Object
2969 unwind_create_frame (frame)
2970 Lisp_Object frame;
2972 struct frame *f = XFRAME (frame);
2974 /* If frame is already dead, nothing to do. This can happen if the
2975 display is disconnected after the frame has become official, but
2976 before x_create_frame removes the unwind protect. */
2977 if (!FRAME_LIVE_P (f))
2978 return Qnil;
2980 /* If frame is ``official'', nothing to do. */
2981 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
2983 #if GLYPH_DEBUG
2984 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2985 #endif
2987 x_free_frame_resources (f);
2989 #if GLYPH_DEBUG
2990 /* Check that reference counts are indeed correct. */
2991 xassert (dpyinfo->reference_count == dpyinfo_refcount);
2992 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
2993 #endif
2994 return Qt;
2997 return Qnil;
3001 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
3002 1, 1, 0,
3003 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
3004 Returns an Emacs frame object.
3005 ALIST is an alist of frame parameters.
3006 If the parameters specify that the frame should not have a minibuffer,
3007 and do not specify a specific minibuffer window to use,
3008 then `default-minibuffer-frame' must be a frame whose minibuffer can
3009 be shared by the new frame.
3011 This function is an internal primitive--use `make-frame' instead. */)
3012 (parms)
3013 Lisp_Object parms;
3015 struct frame *f;
3016 Lisp_Object frame, tem;
3017 Lisp_Object name;
3018 int minibuffer_only = 0;
3019 long window_prompting = 0;
3020 int width, height;
3021 int count = SPECPDL_INDEX ();
3022 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3023 Lisp_Object display;
3024 struct x_display_info *dpyinfo = NULL;
3025 Lisp_Object parent;
3026 struct kboard *kb;
3028 parms = Fcopy_alist (parms);
3030 /* Use this general default value to start with
3031 until we know if this frame has a specified name. */
3032 Vx_resource_name = Vinvocation_name;
3034 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
3035 if (EQ (display, Qunbound))
3036 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3037 if (EQ (display, Qunbound))
3038 display = Qnil;
3039 dpyinfo = check_x_display_info (display);
3040 #ifdef MULTI_KBOARD
3041 kb = dpyinfo->terminal->kboard;
3042 #else
3043 kb = &the_only_kboard;
3044 #endif
3046 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3047 if (!STRINGP (name)
3048 && ! EQ (name, Qunbound)
3049 && ! NILP (name))
3050 error ("Invalid frame name--not a string or nil");
3052 if (STRINGP (name))
3053 Vx_resource_name = name;
3055 /* See if parent window is specified. */
3056 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3057 if (EQ (parent, Qunbound))
3058 parent = Qnil;
3059 if (! NILP (parent))
3060 CHECK_NUMBER (parent);
3062 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3063 /* No need to protect DISPLAY because that's not used after passing
3064 it to make_frame_without_minibuffer. */
3065 frame = Qnil;
3066 GCPRO4 (parms, parent, name, frame);
3067 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3068 RES_TYPE_SYMBOL);
3069 if (EQ (tem, Qnone) || NILP (tem))
3070 f = make_frame_without_minibuffer (Qnil, kb, display);
3071 else if (EQ (tem, Qonly))
3073 f = make_minibuffer_frame ();
3074 minibuffer_only = 1;
3076 else if (WINDOWP (tem))
3077 f = make_frame_without_minibuffer (tem, kb, display);
3078 else
3079 f = make_frame (1);
3081 XSETFRAME (frame, f);
3083 /* Note that X Windows does support scroll bars. */
3084 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
3086 f->terminal = dpyinfo->terminal;
3087 f->terminal->reference_count++;
3089 f->output_method = output_x_window;
3090 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
3091 bzero (f->output_data.x, sizeof (struct x_output));
3092 f->output_data.x->icon_bitmap = -1;
3093 FRAME_FONTSET (f) = -1;
3094 f->output_data.x->scroll_bar_foreground_pixel = -1;
3095 f->output_data.x->scroll_bar_background_pixel = -1;
3096 #ifdef USE_TOOLKIT_SCROLL_BARS
3097 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3098 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3099 #endif /* USE_TOOLKIT_SCROLL_BARS */
3100 record_unwind_protect (unwind_create_frame, frame);
3102 f->icon_name
3103 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3104 RES_TYPE_STRING);
3105 if (! STRINGP (f->icon_name))
3106 f->icon_name = Qnil;
3108 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3109 #if GLYPH_DEBUG
3110 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
3111 dpyinfo_refcount = dpyinfo->reference_count;
3112 #endif /* GLYPH_DEBUG */
3114 /* These colors will be set anyway later, but it's important
3115 to get the color reference counts right, so initialize them! */
3117 Lisp_Object black;
3118 struct gcpro gcpro1;
3120 /* Function x_decode_color can signal an error. Make
3121 sure to initialize color slots so that we won't try
3122 to free colors we haven't allocated. */
3123 f->output_data.x->foreground_pixel = -1;
3124 f->output_data.x->background_pixel = -1;
3125 f->output_data.x->cursor_pixel = -1;
3126 f->output_data.x->cursor_foreground_pixel = -1;
3127 f->output_data.x->border_pixel = -1;
3128 f->output_data.x->mouse_pixel = -1;
3130 black = build_string ("black");
3131 GCPRO1 (black);
3132 f->output_data.x->foreground_pixel
3133 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3134 f->output_data.x->background_pixel
3135 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3136 f->output_data.x->cursor_pixel
3137 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3138 f->output_data.x->cursor_foreground_pixel
3139 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3140 f->output_data.x->border_pixel
3141 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3142 f->output_data.x->mouse_pixel
3143 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3144 UNGCPRO;
3147 /* Specify the parent under which to make this X window. */
3149 if (!NILP (parent))
3151 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3152 f->output_data.x->explicit_parent = 1;
3154 else
3156 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3157 f->output_data.x->explicit_parent = 0;
3160 /* Set the name; the functions to which we pass f expect the name to
3161 be set. */
3162 if (EQ (name, Qunbound) || NILP (name))
3164 f->name = build_string (dpyinfo->x_id_name);
3165 f->explicit_name = 0;
3167 else
3169 f->name = name;
3170 f->explicit_name = 1;
3171 /* use the frame's title when getting resources for this frame. */
3172 specbind (Qx_resource_name, name);
3175 Fmodify_frame_parameters (frame, Fcons (Fcons (Qwindow_system, Qx), Qnil));
3177 /* Extract the window parameters from the supplied values
3178 that are needed to determine window geometry. */
3180 Lisp_Object font;
3182 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
3184 BLOCK_INPUT;
3185 /* First, try whatever font the caller has specified. */
3186 if (STRINGP (font))
3188 tem = Fquery_fontset (font, Qnil);
3189 if (STRINGP (tem))
3190 font = x_new_fontset (f, SDATA (tem));
3191 else
3192 font = x_new_font (f, SDATA (font));
3195 /* Try out a font which we hope has bold and italic variations. */
3196 if (!STRINGP (font))
3197 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
3198 if (!STRINGP (font))
3199 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
3200 if (! STRINGP (font))
3201 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
3202 if (! STRINGP (font))
3203 /* This was formerly the first thing tried, but it finds too many fonts
3204 and takes too long. */
3205 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
3206 /* If those didn't work, look for something which will at least work. */
3207 if (! STRINGP (font))
3208 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
3209 UNBLOCK_INPUT;
3210 if (! STRINGP (font))
3211 font = build_string ("fixed");
3213 x_default_parameter (f, parms, Qfont, font,
3214 "font", "Font", RES_TYPE_STRING);
3217 #ifdef USE_LUCID
3218 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
3219 whereby it fails to get any font. */
3220 xlwmenu_default_font = FRAME_FONT (f);
3221 #endif
3223 x_default_parameter (f, parms, Qborder_width, make_number (2),
3224 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3226 /* This defaults to 1 in order to match xterm. We recognize either
3227 internalBorderWidth or internalBorder (which is what xterm calls
3228 it). */
3229 if (NILP (Fassq (Qinternal_border_width, parms)))
3231 Lisp_Object value;
3233 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3234 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3235 if (! EQ (value, Qunbound))
3236 parms = Fcons (Fcons (Qinternal_border_width, value),
3237 parms);
3239 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
3240 "internalBorderWidth", "internalBorderWidth",
3241 RES_TYPE_NUMBER);
3242 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
3243 "verticalScrollBars", "ScrollBars",
3244 RES_TYPE_SYMBOL);
3246 /* Also do the stuff which must be set before the window exists. */
3247 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3248 "foreground", "Foreground", RES_TYPE_STRING);
3249 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3250 "background", "Background", RES_TYPE_STRING);
3251 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3252 "pointerColor", "Foreground", RES_TYPE_STRING);
3253 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
3254 "cursorColor", "Foreground", RES_TYPE_STRING);
3255 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3256 "borderColor", "BorderColor", RES_TYPE_STRING);
3257 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3258 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3259 x_default_parameter (f, parms, Qline_spacing, Qnil,
3260 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3261 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3262 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3263 x_default_parameter (f, parms, Qright_fringe, Qnil,
3264 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3266 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3267 "scrollBarForeground",
3268 "ScrollBarForeground", 1);
3269 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3270 "scrollBarBackground",
3271 "ScrollBarBackground", 0);
3273 /* Init faces before x_default_parameter is called for scroll-bar
3274 parameters because that function calls x_set_scroll_bar_width,
3275 which calls change_frame_size, which calls Fset_window_buffer,
3276 which runs hooks, which call Fvertical_motion. At the end, we
3277 end up in init_iterator with a null face cache, which should not
3278 happen. */
3279 init_frame_faces (f);
3281 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
3282 "menuBar", "MenuBar", RES_TYPE_NUMBER);
3283 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
3284 "toolBar", "ToolBar", RES_TYPE_NUMBER);
3285 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3286 "bufferPredicate", "BufferPredicate",
3287 RES_TYPE_SYMBOL);
3288 x_default_parameter (f, parms, Qtitle, Qnil,
3289 "title", "Title", RES_TYPE_STRING);
3290 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3291 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3292 x_default_parameter (f, parms, Qfullscreen, Qnil,
3293 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3295 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3297 /* Compute the size of the X window. */
3298 window_prompting = x_figure_window_size (f, parms, 1);
3300 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3301 f->no_split = minibuffer_only || EQ (tem, Qt);
3303 x_icon_verify (f, parms);
3305 /* Create the X widget or window. */
3306 #ifdef USE_X_TOOLKIT
3307 x_window (f, window_prompting, minibuffer_only);
3308 #else
3309 x_window (f);
3310 #endif
3312 x_icon (f, parms);
3313 x_make_gc (f);
3315 /* Now consider the frame official. */
3316 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3317 Vframe_list = Fcons (frame, Vframe_list);
3319 /* We need to do this after creating the X window, so that the
3320 icon-creation functions can say whose icon they're describing. */
3321 x_default_parameter (f, parms, Qicon_type, Qt,
3322 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
3324 x_default_parameter (f, parms, Qauto_raise, Qnil,
3325 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3326 x_default_parameter (f, parms, Qauto_lower, Qnil,
3327 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3328 x_default_parameter (f, parms, Qcursor_type, Qbox,
3329 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3330 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3331 "scrollBarWidth", "ScrollBarWidth",
3332 RES_TYPE_NUMBER);
3334 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3335 Change will not be effected unless different from the current
3336 FRAME_LINES (f). */
3337 width = FRAME_COLS (f);
3338 height = FRAME_LINES (f);
3340 SET_FRAME_COLS (f, 0);
3341 FRAME_LINES (f) = 0;
3342 change_frame_size (f, height, width, 1, 0, 0);
3344 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3345 /* Create the menu bar. */
3346 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3348 /* If this signals an error, we haven't set size hints for the
3349 frame and we didn't make it visible. */
3350 initialize_frame_menubar (f);
3352 #ifndef USE_GTK
3353 /* This is a no-op, except under Motif where it arranges the
3354 main window for the widgets on it. */
3355 lw_set_main_areas (f->output_data.x->column_widget,
3356 f->output_data.x->menubar_widget,
3357 f->output_data.x->edit_widget);
3358 #endif /* not USE_GTK */
3360 #endif /* USE_X_TOOLKIT || USE_GTK */
3362 /* Tell the server what size and position, etc, we want, and how
3363 badly we want them. This should be done after we have the menu
3364 bar so that its size can be taken into account. */
3365 BLOCK_INPUT;
3366 x_wm_set_size_hint (f, window_prompting, 0);
3367 UNBLOCK_INPUT;
3369 /* Make the window appear on the frame and enable display, unless
3370 the caller says not to. However, with explicit parent, Emacs
3371 cannot control visibility, so don't try. */
3372 if (! f->output_data.x->explicit_parent)
3374 Lisp_Object visibility;
3376 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3377 RES_TYPE_SYMBOL);
3378 if (EQ (visibility, Qunbound))
3379 visibility = Qt;
3381 if (EQ (visibility, Qicon))
3382 x_iconify_frame (f);
3383 else if (! NILP (visibility))
3384 x_make_frame_visible (f);
3385 else
3386 /* Must have been Qnil. */
3390 /* Set the WM leader property. GTK does this itself, so this is not
3391 needed when using GTK. */
3392 if (dpyinfo->client_leader_window != 0)
3394 BLOCK_INPUT;
3395 XChangeProperty (FRAME_X_DISPLAY (f),
3396 FRAME_OUTER_WINDOW (f),
3397 dpyinfo->Xatom_wm_client_leader,
3398 XA_WINDOW, 32, PropModeReplace,
3399 (unsigned char *) &dpyinfo->client_leader_window, 1);
3400 UNBLOCK_INPUT;
3403 /* Initialize `default-minibuffer-frame' in case this is the first
3404 frame on this terminal. */
3405 if (FRAME_HAS_MINIBUF_P (f)
3406 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
3407 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
3408 kb->Vdefault_minibuffer_frame = frame;
3410 /* All remaining specified parameters, which have not been "used"
3411 by x_get_arg and friends, now go in the misc. alist of the frame. */
3412 for (tem = parms; !NILP (tem); tem = XCDR (tem))
3413 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3414 f->param_alist = Fcons (XCAR (tem), f->param_alist);
3416 UNGCPRO;
3418 /* Make sure windows on this frame appear in calls to next-window
3419 and similar functions. */
3420 Vwindow_list = Qnil;
3422 return unbind_to (count, frame);
3426 /* FRAME is used only to get a handle on the X display. We don't pass the
3427 display info directly because we're called from frame.c, which doesn't
3428 know about that structure. */
3430 Lisp_Object
3431 x_get_focus_frame (frame)
3432 struct frame *frame;
3434 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
3435 Lisp_Object xfocus;
3436 if (! dpyinfo->x_focus_frame)
3437 return Qnil;
3439 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3440 return xfocus;
3444 /* In certain situations, when the window manager follows a
3445 click-to-focus policy, there seems to be no way around calling
3446 XSetInputFocus to give another frame the input focus .
3448 In an ideal world, XSetInputFocus should generally be avoided so
3449 that applications don't interfere with the window manager's focus
3450 policy. But I think it's okay to use when it's clearly done
3451 following a user-command. */
3453 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
3454 doc: /* Set the input focus to FRAME.
3455 FRAME nil means use the selected frame. */)
3456 (frame)
3457 Lisp_Object frame;
3459 struct frame *f = check_x_frame (frame);
3460 Display *dpy = FRAME_X_DISPLAY (f);
3461 int count;
3463 BLOCK_INPUT;
3464 count = x_catch_errors (dpy);
3465 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3466 RevertToParent, CurrentTime);
3467 x_uncatch_errors (dpy, count);
3468 UNBLOCK_INPUT;
3470 return Qnil;
3474 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3475 doc: /* Internal function called by `color-defined-p', which see. */)
3476 (color, frame)
3477 Lisp_Object color, frame;
3479 XColor foo;
3480 FRAME_PTR f = check_x_frame (frame);
3482 CHECK_STRING (color);
3484 if (x_defined_color (f, SDATA (color), &foo, 0))
3485 return Qt;
3486 else
3487 return Qnil;
3490 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3491 doc: /* Internal function called by `color-values', which see. */)
3492 (color, frame)
3493 Lisp_Object color, frame;
3495 XColor foo;
3496 FRAME_PTR f = check_x_frame (frame);
3498 CHECK_STRING (color);
3500 if (x_defined_color (f, SDATA (color), &foo, 0))
3502 Lisp_Object rgb[3];
3504 rgb[0] = make_number (foo.red);
3505 rgb[1] = make_number (foo.green);
3506 rgb[2] = make_number (foo.blue);
3507 return Flist (3, rgb);
3509 else
3510 return Qnil;
3513 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3514 doc: /* Internal function called by `display-color-p', which see. */)
3515 (terminal)
3516 Lisp_Object terminal;
3518 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3520 if (dpyinfo->n_planes <= 2)
3521 return Qnil;
3523 switch (dpyinfo->visual->class)
3525 case StaticColor:
3526 case PseudoColor:
3527 case TrueColor:
3528 case DirectColor:
3529 return Qt;
3531 default:
3532 return Qnil;
3536 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3537 0, 1, 0,
3538 doc: /* Return t if the X display supports shades of gray.
3539 Note that color displays do support shades of gray.
3540 The optional argument TERMINAL specifies which display to ask about.
3541 TERMINAL should be a terminal id, a frame or a display name (a string).
3542 If omitted or nil, that stands for the selected frame's display. */)
3543 (terminal)
3544 Lisp_Object terminal;
3546 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3548 if (dpyinfo->n_planes <= 1)
3549 return Qnil;
3551 switch (dpyinfo->visual->class)
3553 case StaticColor:
3554 case PseudoColor:
3555 case TrueColor:
3556 case DirectColor:
3557 case StaticGray:
3558 case GrayScale:
3559 return Qt;
3561 default:
3562 return Qnil;
3566 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3567 0, 1, 0,
3568 doc: /* Returns the width in pixels of the X display TERMINAL.
3569 The optional argument TERMINAL specifies which display to ask about.
3570 TERMINAL should be a terminal id, a frame or a display name (a string).
3571 If omitted or nil, that stands for the selected frame's display. */)
3572 (terminal)
3573 Lisp_Object terminal;
3575 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3577 return make_number (dpyinfo->width);
3580 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3581 Sx_display_pixel_height, 0, 1, 0,
3582 doc: /* Returns the height in pixels of the X display TERMINAL.
3583 The optional argument TERMINAL specifies which display to ask about.
3584 TERMINAL should be a terminal id, a frame or a display name (a string).
3585 If omitted or nil, that stands for the selected frame's display. */)
3586 (terminal)
3587 Lisp_Object terminal;
3589 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3591 return make_number (dpyinfo->height);
3594 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3595 0, 1, 0,
3596 doc: /* Returns the number of bitplanes of the X display TERMINAL.
3597 The optional argument TERMINAL specifies which display to ask about.
3598 TERMINAL should be a terminal id, a frame or a display name (a string).
3599 If omitted or nil, that stands for the selected frame's display. */)
3600 (terminal)
3601 Lisp_Object terminal;
3603 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3605 return make_number (dpyinfo->n_planes);
3608 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3609 0, 1, 0,
3610 doc: /* Returns the number of color cells of the X display TERMINAL.
3611 The optional argument TERMINAL specifies which display to ask about.
3612 TERMINAL should be a terminal id, a frame or a display name (a string).
3613 If omitted or nil, that stands for the selected frame's display. */)
3614 (terminal)
3615 Lisp_Object terminal;
3617 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3619 int nr_planes = DisplayPlanes (dpyinfo->display,
3620 XScreenNumberOfScreen (dpyinfo->screen));
3622 /* Truncate nr_planes to 24 to avoid integer overflow.
3623 Some displays says 32, but only 24 bits are actually significant.
3624 There are only very few and rare video cards that have more than
3625 24 significant bits. Also 24 bits is more than 16 million colors,
3626 it "should be enough for everyone". */
3627 if (nr_planes > 24) nr_planes = 24;
3629 return make_number (1 << nr_planes);
3632 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3633 Sx_server_max_request_size,
3634 0, 1, 0,
3635 doc: /* Returns the maximum request size of the X server of display TERMINAL.
3636 The optional argument TERMINAL specifies which display to ask about.
3637 TERMINAL should be a terminal id, a frame or a display name (a string).
3638 If omitted or nil, that stands for the selected frame's display. */)
3639 (terminal)
3640 Lisp_Object terminal;
3642 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3644 return make_number (MAXREQUEST (dpyinfo->display));
3647 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3648 doc: /* Returns the "vendor ID" string of the X server of display TERMINAL.
3649 \(Labelling every distributor as a "vendor" embodies the false assumption
3650 that operating systems cannot be developed and distributed noncommercially.)
3651 The optional argument TERMINAL specifies which display to ask about.
3652 TERMINAL should be a terminal id, a frame or a display name (a string).
3653 If omitted or nil, that stands for the selected frame's display. */)
3654 (terminal)
3655 Lisp_Object terminal;
3657 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3658 char *vendor = ServerVendor (dpyinfo->display);
3660 if (! vendor) vendor = "";
3661 return build_string (vendor);
3664 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3665 doc: /* Returns the version numbers of the X server of display TERMINAL.
3666 The value is a list of three integers: the major and minor
3667 version numbers of the X Protocol in use, and the distributor-specific release
3668 number. See also the function `x-server-vendor'.
3670 The optional argument TERMINAL specifies which display to ask about.
3671 TERMINAL should be a terminal id, a frame or a display name (a string).
3672 If omitted or nil, that stands for the selected frame's display. */)
3673 (terminal)
3674 Lisp_Object terminal;
3676 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3677 Display *dpy = dpyinfo->display;
3679 return Fcons (make_number (ProtocolVersion (dpy)),
3680 Fcons (make_number (ProtocolRevision (dpy)),
3681 Fcons (make_number (VendorRelease (dpy)), Qnil)));
3684 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3685 doc: /* Return the number of screens on the X server of display TERMINAL.
3686 The optional argument TERMINAL specifies which display to ask about.
3687 TERMINAL should be a terminal id, a frame or a display name (a string).
3688 If omitted or nil, that stands for the selected frame's display. */)
3689 (terminal)
3690 Lisp_Object terminal;
3692 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3694 return make_number (ScreenCount (dpyinfo->display));
3697 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3698 doc: /* Return the height in millimeters of the X display TERMINAL.
3699 The optional argument TERMINAL specifies which display to ask about.
3700 TERMINAL should be a terminal id, a frame or a display name (a string).
3701 If omitted or nil, that stands for the selected frame's display. */)
3702 (terminal)
3703 Lisp_Object terminal;
3705 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3707 return make_number (HeightMMOfScreen (dpyinfo->screen));
3710 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3711 doc: /* Return the width in millimeters of the X display TERMINAL.
3712 The optional argument TERMINAL specifies which display to ask about.
3713 TERMINAL should be a terminal id, a frame or a display name (a string).
3714 If omitted or nil, that stands for the selected frame's display. */)
3715 (terminal)
3716 Lisp_Object terminal;
3718 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3720 return make_number (WidthMMOfScreen (dpyinfo->screen));
3723 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3724 Sx_display_backing_store, 0, 1, 0,
3725 doc: /* Returns an indication of whether X display TERMINAL does backing store.
3726 The value may be `always', `when-mapped', or `not-useful'.
3727 The optional argument TERMINAL specifies which display to ask about.
3728 TERMINAL should be a terminal id, a frame or a display name (a string).
3729 If omitted or nil, that stands for the selected frame's display. */)
3730 (terminal)
3731 Lisp_Object terminal;
3733 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3734 Lisp_Object result;
3736 switch (DoesBackingStore (dpyinfo->screen))
3738 case Always:
3739 result = intern ("always");
3740 break;
3742 case WhenMapped:
3743 result = intern ("when-mapped");
3744 break;
3746 case NotUseful:
3747 result = intern ("not-useful");
3748 break;
3750 default:
3751 error ("Strange value for BackingStore parameter of screen");
3752 result = Qnil;
3755 return result;
3758 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3759 Sx_display_visual_class, 0, 1, 0,
3760 doc: /* Return the visual class of the X display TERMINAL.
3761 The value is one of the symbols `static-gray', `gray-scale',
3762 `static-color', `pseudo-color', `true-color', or `direct-color'.
3764 The optional argument TERMINAL specifies which display to ask about.
3765 TERMINAL should a terminal id, a frame or a display name (a string).
3766 If omitted or nil, that stands for the selected frame's display. */)
3767 (terminal)
3768 Lisp_Object terminal;
3770 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3771 Lisp_Object result;
3773 switch (dpyinfo->visual->class)
3775 case StaticGray:
3776 result = intern ("static-gray");
3777 break;
3778 case GrayScale:
3779 result = intern ("gray-scale");
3780 break;
3781 case StaticColor:
3782 result = intern ("static-color");
3783 break;
3784 case PseudoColor:
3785 result = intern ("pseudo-color");
3786 break;
3787 case TrueColor:
3788 result = intern ("true-color");
3789 break;
3790 case DirectColor:
3791 result = intern ("direct-color");
3792 break;
3793 default:
3794 error ("Display has an unknown visual class");
3795 result = Qnil;
3798 return result;
3801 DEFUN ("x-display-save-under", Fx_display_save_under,
3802 Sx_display_save_under, 0, 1, 0,
3803 doc: /* Returns t if the X display TERMINAL supports the save-under feature.
3804 The optional argument TERMINAL specifies which display to ask about.
3805 TERMINAL should be a terminal id, a frame or a display name (a string).
3806 If omitted or nil, that stands for the selected frame's display. */)
3807 (terminal)
3808 Lisp_Object terminal;
3810 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3812 if (DoesSaveUnders (dpyinfo->screen) == True)
3813 return Qt;
3814 else
3815 return Qnil;
3819 x_pixel_width (f)
3820 register struct frame *f;
3822 return FRAME_PIXEL_WIDTH (f);
3826 x_pixel_height (f)
3827 register struct frame *f;
3829 return FRAME_PIXEL_HEIGHT (f);
3833 x_char_width (f)
3834 register struct frame *f;
3836 return FRAME_COLUMN_WIDTH (f);
3840 x_char_height (f)
3841 register struct frame *f;
3843 return FRAME_LINE_HEIGHT (f);
3847 x_screen_planes (f)
3848 register struct frame *f;
3850 return FRAME_X_DISPLAY_INFO (f)->n_planes;
3855 /************************************************************************
3856 X Displays
3857 ************************************************************************/
3860 /* Mapping visual names to visuals. */
3862 static struct visual_class
3864 char *name;
3865 int class;
3867 visual_classes[] =
3869 {"StaticGray", StaticGray},
3870 {"GrayScale", GrayScale},
3871 {"StaticColor", StaticColor},
3872 {"PseudoColor", PseudoColor},
3873 {"TrueColor", TrueColor},
3874 {"DirectColor", DirectColor},
3875 {NULL, 0}
3879 #ifndef HAVE_XSCREENNUMBEROFSCREEN
3881 /* Value is the screen number of screen SCR. This is a substitute for
3882 the X function with the same name when that doesn't exist. */
3885 XScreenNumberOfScreen (scr)
3886 register Screen *scr;
3888 Display *dpy = scr->display;
3889 int i;
3891 for (i = 0; i < dpy->nscreens; ++i)
3892 if (scr == dpy->screens + i)
3893 break;
3895 return i;
3898 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
3901 /* Select the visual that should be used on display DPYINFO. Set
3902 members of DPYINFO appropriately. Called from x_term_init. */
3904 void
3905 select_visual (dpyinfo)
3906 struct x_display_info *dpyinfo;
3908 Display *dpy = dpyinfo->display;
3909 Screen *screen = dpyinfo->screen;
3910 Lisp_Object value;
3912 /* See if a visual is specified. */
3913 value = display_x_get_resource (dpyinfo,
3914 build_string ("visualClass"),
3915 build_string ("VisualClass"),
3916 Qnil, Qnil);
3917 if (STRINGP (value))
3919 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
3920 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
3921 depth, a decimal number. NAME is compared with case ignored. */
3922 char *s = (char *) alloca (SBYTES (value) + 1);
3923 char *dash;
3924 int i, class = -1;
3925 XVisualInfo vinfo;
3927 strcpy (s, SDATA (value));
3928 dash = index (s, '-');
3929 if (dash)
3931 dpyinfo->n_planes = atoi (dash + 1);
3932 *dash = '\0';
3934 else
3935 /* We won't find a matching visual with depth 0, so that
3936 an error will be printed below. */
3937 dpyinfo->n_planes = 0;
3939 /* Determine the visual class. */
3940 for (i = 0; visual_classes[i].name; ++i)
3941 if (xstricmp (s, visual_classes[i].name) == 0)
3943 class = visual_classes[i].class;
3944 break;
3947 /* Look up a matching visual for the specified class. */
3948 if (class == -1
3949 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
3950 dpyinfo->n_planes, class, &vinfo))
3951 fatal ("Invalid visual specification `%s'", SDATA (value));
3953 dpyinfo->visual = vinfo.visual;
3955 else
3957 int n_visuals;
3958 XVisualInfo *vinfo, vinfo_template;
3960 dpyinfo->visual = DefaultVisualOfScreen (screen);
3962 #ifdef HAVE_X11R4
3963 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
3964 #else
3965 vinfo_template.visualid = dpyinfo->visual->visualid;
3966 #endif
3967 vinfo_template.screen = XScreenNumberOfScreen (screen);
3968 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
3969 &vinfo_template, &n_visuals);
3970 if (n_visuals != 1)
3971 fatal ("Can't get proper X visual info");
3973 dpyinfo->n_planes = vinfo->depth;
3974 XFree ((char *) vinfo);
3979 /* Return the X display structure for the display named NAME.
3980 Open a new connection if necessary. */
3982 struct x_display_info *
3983 x_display_info_for_name (name)
3984 Lisp_Object name;
3986 Lisp_Object names;
3987 struct x_display_info *dpyinfo;
3989 CHECK_STRING (name);
3991 #if 0
3992 if (! EQ (Vinitial_window_system, intern ("x")))
3993 error ("Not using X Windows"); /* That doesn't stop us anymore. */
3994 #endif
3996 for (dpyinfo = x_display_list, names = x_display_name_list;
3997 dpyinfo;
3998 dpyinfo = dpyinfo->next, names = XCDR (names))
4000 Lisp_Object tem;
4001 tem = Fstring_equal (XCAR (XCAR (names)), name);
4002 if (!NILP (tem))
4003 return dpyinfo;
4006 /* Use this general default value to start with. */
4007 Vx_resource_name = Vinvocation_name;
4009 validate_x_resource_name ();
4011 dpyinfo = x_term_init (name, (char *)0,
4012 (char *) SDATA (Vx_resource_name));
4014 if (dpyinfo == 0)
4015 error ("Cannot connect to X server %s", SDATA (name));
4017 x_in_use = 1;
4018 XSETFASTINT (Vwindow_system_version, 11);
4020 return dpyinfo;
4024 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
4025 1, 3, 0,
4026 doc: /* Open a connection to an X server.
4027 DISPLAY is the name of the display to connect to.
4028 Optional second arg XRM-STRING is a string of resources in xrdb format.
4029 If the optional third arg MUST-SUCCEED is non-nil,
4030 terminate Emacs if we can't open the connection. */)
4031 (display, xrm_string, must_succeed)
4032 Lisp_Object display, xrm_string, must_succeed;
4034 unsigned char *xrm_option;
4035 struct x_display_info *dpyinfo;
4037 CHECK_STRING (display);
4038 if (! NILP (xrm_string))
4039 CHECK_STRING (xrm_string);
4041 #if 0
4042 if (! EQ (Vinitial_window_system, intern ("x")))
4043 error ("Not using X Windows"); /* That doesn't stop us anymore. */
4044 #endif
4046 if (! NILP (xrm_string))
4047 xrm_option = (unsigned char *) SDATA (xrm_string);
4048 else
4049 xrm_option = (unsigned char *) 0;
4051 validate_x_resource_name ();
4053 /* This is what opens the connection and sets x_current_display.
4054 This also initializes many symbols, such as those used for input. */
4055 dpyinfo = x_term_init (display, xrm_option,
4056 (char *) SDATA (Vx_resource_name));
4058 if (dpyinfo == 0)
4060 if (!NILP (must_succeed))
4061 fatal ("Cannot connect to X server %s.\n\
4062 Check the DISPLAY environment variable or use `-d'.\n\
4063 Also use the `xauth' program to verify that you have the proper\n\
4064 authorization information needed to connect the X server.\n\
4065 An insecure way to solve the problem may be to use `xhost'.\n",
4066 SDATA (display));
4067 else
4068 error ("Cannot connect to X server %s", SDATA (display));
4071 x_in_use = 1;
4073 XSETFASTINT (Vwindow_system_version, 11);
4074 return Qnil;
4077 DEFUN ("x-close-connection", Fx_close_connection,
4078 Sx_close_connection, 1, 1, 0,
4079 doc: /* Close the connection to TERMINAL's X server.
4080 For TERMINAL, specify a terminal id, a frame or a display name (a
4081 string). If TERMINAL is nil, that stands for the selected frame's
4082 terminal. */)
4083 (terminal)
4084 Lisp_Object terminal;
4086 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4088 if (dpyinfo->reference_count > 0)
4089 error ("Display still has frames on it");
4091 x_delete_terminal (dpyinfo->terminal);
4093 return Qnil;
4096 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4097 doc: /* Return the list of display names that Emacs has connections to. */)
4100 Lisp_Object tail, result;
4102 result = Qnil;
4103 for (tail = x_display_name_list; ! NILP (tail); tail = XCDR (tail))
4104 result = Fcons (XCAR (XCAR (tail)), result);
4106 return result;
4109 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4110 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4111 If ON is nil, allow buffering of requests.
4112 Turning on synchronization prohibits the Xlib routines from buffering
4113 requests and seriously degrades performance, but makes debugging much
4114 easier.
4115 The optional second argument TERMINAL specifies which display to act on.
4116 TERMINAL should be a terminal id, a frame or a display name (a string).
4117 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4118 (on, terminal)
4119 Lisp_Object terminal, on;
4121 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4123 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4125 return Qnil;
4128 /* Wait for responses to all X commands issued so far for frame F. */
4130 void
4131 x_sync (f)
4132 FRAME_PTR f;
4134 BLOCK_INPUT;
4135 XSync (FRAME_X_DISPLAY (f), False);
4136 UNBLOCK_INPUT;
4140 /***********************************************************************
4141 Window properties
4142 ***********************************************************************/
4144 DEFUN ("x-change-window-property", Fx_change_window_property,
4145 Sx_change_window_property, 2, 6, 0,
4146 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4147 PROP must be a string.
4148 VALUE may be a string or a list of conses, numbers and/or strings.
4149 If an element in the list is a string, it is converted to
4150 an Atom and the value of the Atom is used. If an element is a cons,
4151 it is converted to a 32 bit number where the car is the 16 top bits and the
4152 cdr is the lower 16 bits.
4153 FRAME nil or omitted means use the selected frame.
4154 If TYPE is given and non-nil, it is the name of the type of VALUE.
4155 If TYPE is not given or nil, the type is STRING.
4156 FORMAT gives the size in bits of each element if VALUE is a list.
4157 It must be one of 8, 16 or 32.
4158 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4159 If OUTER_P is non-nil, the property is changed for the outer X window of
4160 FRAME. Default is to change on the edit X window.
4162 Value is VALUE. */)
4163 (prop, value, frame, type, format, outer_p)
4164 Lisp_Object prop, value, frame, type, format, outer_p;
4166 struct frame *f = check_x_frame (frame);
4167 Atom prop_atom;
4168 Atom target_type = XA_STRING;
4169 int element_format = 8;
4170 unsigned char *data;
4171 int nelements;
4172 Window w;
4174 CHECK_STRING (prop);
4176 if (! NILP (format))
4178 CHECK_NUMBER (format);
4179 element_format = XFASTINT (format);
4181 if (element_format != 8 && element_format != 16
4182 && element_format != 32)
4183 error ("FORMAT must be one of 8, 16 or 32");
4186 if (CONSP (value))
4188 nelements = x_check_property_data (value);
4189 if (nelements == -1)
4190 error ("Bad data in VALUE, must be number, string or cons");
4192 if (element_format == 8)
4193 data = (unsigned char *) xmalloc (nelements);
4194 else if (element_format == 16)
4195 data = (unsigned char *) xmalloc (nelements*2);
4196 else /* format == 32 */
4197 /* The man page for XChangeProperty:
4198 "If the specified format is 32, the property data must be a
4199 long array."
4200 This applies even if long is more than 64 bits. The X library
4201 converts to 32 bits before sending to the X server. */
4202 data = (unsigned char *) xmalloc (nelements * sizeof(long));
4204 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4206 else
4208 CHECK_STRING (value);
4209 data = SDATA (value);
4210 nelements = SCHARS (value);
4213 BLOCK_INPUT;
4214 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4215 if (! NILP (type))
4217 CHECK_STRING (type);
4218 target_type = XInternAtom (FRAME_X_DISPLAY (f), SDATA (type), False);
4221 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4222 else w = FRAME_X_WINDOW (f);
4224 XChangeProperty (FRAME_X_DISPLAY (f), w,
4225 prop_atom, target_type, element_format, PropModeReplace,
4226 data, nelements);
4228 if (CONSP (value)) xfree (data);
4230 /* Make sure the property is set when we return. */
4231 XFlush (FRAME_X_DISPLAY (f));
4232 UNBLOCK_INPUT;
4234 return value;
4238 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4239 Sx_delete_window_property, 1, 2, 0,
4240 doc: /* Remove window property PROP from X window of FRAME.
4241 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4242 (prop, frame)
4243 Lisp_Object prop, frame;
4245 struct frame *f = check_x_frame (frame);
4246 Atom prop_atom;
4248 CHECK_STRING (prop);
4249 BLOCK_INPUT;
4250 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4251 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4253 /* Make sure the property is removed when we return. */
4254 XFlush (FRAME_X_DISPLAY (f));
4255 UNBLOCK_INPUT;
4257 return prop;
4261 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4262 1, 6, 0,
4263 doc: /* Value is the value of window property PROP on FRAME.
4264 If FRAME is nil or omitted, use the selected frame.
4265 If TYPE is nil or omitted, get the property as a string. Otherwise TYPE
4266 is the name of the Atom that denotes the type expected.
4267 If SOURCE is non-nil, get the property on that window instead of from
4268 FRAME. The number 0 denotes the root window.
4269 If DELETE_P is non-nil, delete the property after retreiving it.
4270 If VECTOR_RET_P is non-nil, don't return a string but a vector of values.
4272 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4273 no value of TYPE. */)
4274 (prop, frame, type, source, delete_p, vector_ret_p)
4275 Lisp_Object prop, frame, type, source, delete_p, vector_ret_p;
4277 struct frame *f = check_x_frame (frame);
4278 Atom prop_atom;
4279 int rc;
4280 Lisp_Object prop_value = Qnil;
4281 unsigned char *tmp_data = NULL;
4282 Atom actual_type;
4283 Atom target_type = XA_STRING;
4284 int actual_format;
4285 unsigned long actual_size, bytes_remaining;
4286 Window target_window = FRAME_X_WINDOW (f);
4287 struct gcpro gcpro1;
4289 GCPRO1 (prop_value);
4290 CHECK_STRING (prop);
4292 if (! NILP (source))
4294 if (NUMBERP (source))
4296 if (FLOATP (source))
4297 target_window = (Window) XFLOAT (source);
4298 else
4299 target_window = XFASTINT (source);
4301 if (target_window == 0)
4302 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
4304 else if (CONSP (source))
4305 target_window = cons_to_long (source);
4308 BLOCK_INPUT;
4309 if (STRINGP (type))
4311 if (strcmp ("AnyPropertyType", SDATA (type)) == 0)
4312 target_type = AnyPropertyType;
4313 else
4314 target_type = XInternAtom (FRAME_X_DISPLAY (f), SDATA (type), False);
4317 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4318 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4319 prop_atom, 0, 0, False, target_type,
4320 &actual_type, &actual_format, &actual_size,
4321 &bytes_remaining, &tmp_data);
4322 if (rc == Success)
4324 int size = bytes_remaining;
4326 XFree (tmp_data);
4327 tmp_data = NULL;
4329 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4330 prop_atom, 0, bytes_remaining,
4331 ! NILP (delete_p), target_type,
4332 &actual_type, &actual_format,
4333 &actual_size, &bytes_remaining,
4334 &tmp_data);
4335 if (rc == Success && tmp_data)
4337 /* The man page for XGetWindowProperty says:
4338 "If the returned format is 32, the returned data is represented
4339 as a long array and should be cast to that type to obtain the
4340 elements."
4341 This applies even if long is more than 32 bits, the X library
4342 converts from 32 bit elements received from the X server to long
4343 and passes the long array to us. Thus, for that case bcopy can not
4344 be used. We convert to a 32 bit type here, because so much code
4345 assume on that.
4347 The bytes and offsets passed to XGetWindowProperty refers to the
4348 property and those are indeed in 32 bit quantities if format is
4349 32. */
4351 if (actual_format == 32 && actual_format < BITS_PER_LONG)
4353 unsigned long i;
4354 int *idata = (int *) tmp_data;
4355 long *ldata = (long *) tmp_data;
4357 for (i = 0; i < actual_size; ++i)
4358 idata[i] = (int) ldata[i];
4361 if (NILP (vector_ret_p))
4362 prop_value = make_string (tmp_data, size);
4363 else
4364 prop_value = x_property_data_to_lisp (f,
4365 tmp_data,
4366 actual_type,
4367 actual_format,
4368 actual_size);
4371 if (tmp_data) XFree (tmp_data);
4374 UNBLOCK_INPUT;
4375 UNGCPRO;
4376 return prop_value;
4381 /***********************************************************************
4382 Busy cursor
4383 ***********************************************************************/
4385 /* If non-null, an asynchronous timer that, when it expires, displays
4386 an hourglass cursor on all frames. */
4388 static struct atimer *hourglass_atimer;
4390 /* Non-zero means an hourglass cursor is currently shown. */
4392 static int hourglass_shown_p;
4394 /* Number of seconds to wait before displaying an hourglass cursor. */
4396 static Lisp_Object Vhourglass_delay;
4398 /* Default number of seconds to wait before displaying an hourglass
4399 cursor. */
4401 #define DEFAULT_HOURGLASS_DELAY 1
4403 /* Function prototypes. */
4405 static void show_hourglass P_ ((struct atimer *));
4406 static void hide_hourglass P_ ((void));
4408 /* Return non-zero if houglass timer has been started or hourglass is shown. */
4411 hourglass_started ()
4413 return hourglass_shown_p || hourglass_atimer != NULL;
4417 /* Cancel a currently active hourglass timer, and start a new one. */
4419 void
4420 start_hourglass ()
4422 EMACS_TIME delay;
4423 int secs, usecs = 0;
4425 cancel_hourglass ();
4427 if (INTEGERP (Vhourglass_delay)
4428 && XINT (Vhourglass_delay) > 0)
4429 secs = XFASTINT (Vhourglass_delay);
4430 else if (FLOATP (Vhourglass_delay)
4431 && XFLOAT_DATA (Vhourglass_delay) > 0)
4433 Lisp_Object tem;
4434 tem = Ftruncate (Vhourglass_delay, Qnil);
4435 secs = XFASTINT (tem);
4436 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
4438 else
4439 secs = DEFAULT_HOURGLASS_DELAY;
4441 EMACS_SET_SECS_USECS (delay, secs, usecs);
4442 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
4443 show_hourglass, NULL);
4447 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
4448 shown. */
4450 void
4451 cancel_hourglass ()
4453 if (hourglass_atimer)
4455 cancel_atimer (hourglass_atimer);
4456 hourglass_atimer = NULL;
4459 if (hourglass_shown_p)
4460 hide_hourglass ();
4464 /* Timer function of hourglass_atimer. TIMER is equal to
4465 hourglass_atimer.
4467 Display an hourglass pointer on all frames by mapping the frames'
4468 hourglass_window. Set the hourglass_p flag in the frames'
4469 output_data.x structure to indicate that an hourglass cursor is
4470 shown on the frames. */
4472 static void
4473 show_hourglass (timer)
4474 struct atimer *timer;
4476 /* The timer implementation will cancel this timer automatically
4477 after this function has run. Set hourglass_atimer to null
4478 so that we know the timer doesn't have to be canceled. */
4479 hourglass_atimer = NULL;
4481 if (!hourglass_shown_p)
4483 Lisp_Object rest, frame;
4485 BLOCK_INPUT;
4487 FOR_EACH_FRAME (rest, frame)
4489 struct frame *f = XFRAME (frame);
4491 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4493 Display *dpy = FRAME_X_DISPLAY (f);
4495 #ifdef USE_X_TOOLKIT
4496 if (f->output_data.x->widget)
4497 #else
4498 if (FRAME_OUTER_WINDOW (f))
4499 #endif
4501 f->output_data.x->hourglass_p = 1;
4503 if (!f->output_data.x->hourglass_window)
4505 unsigned long mask = CWCursor;
4506 XSetWindowAttributes attrs;
4507 #ifdef USE_GTK
4508 Window parent = FRAME_X_WINDOW (f);
4509 #else
4510 Window parent = FRAME_OUTER_WINDOW (f);
4511 #endif
4512 attrs.cursor = f->output_data.x->hourglass_cursor;
4514 f->output_data.x->hourglass_window
4515 = XCreateWindow (dpy, parent,
4516 0, 0, 32000, 32000, 0, 0,
4517 InputOnly,
4518 CopyFromParent,
4519 mask, &attrs);
4522 XMapRaised (dpy, f->output_data.x->hourglass_window);
4523 XFlush (dpy);
4528 hourglass_shown_p = 1;
4529 UNBLOCK_INPUT;
4534 /* Hide the hourglass pointer on all frames, if it is currently
4535 shown. */
4537 static void
4538 hide_hourglass ()
4540 if (hourglass_shown_p)
4542 Lisp_Object rest, frame;
4544 BLOCK_INPUT;
4545 FOR_EACH_FRAME (rest, frame)
4547 struct frame *f = XFRAME (frame);
4549 if (FRAME_X_P (f)
4550 /* Watch out for newly created frames. */
4551 && f->output_data.x->hourglass_window)
4553 XUnmapWindow (FRAME_X_DISPLAY (f),
4554 f->output_data.x->hourglass_window);
4555 /* Sync here because XTread_socket looks at the
4556 hourglass_p flag that is reset to zero below. */
4557 XSync (FRAME_X_DISPLAY (f), False);
4558 f->output_data.x->hourglass_p = 0;
4562 hourglass_shown_p = 0;
4563 UNBLOCK_INPUT;
4569 /***********************************************************************
4570 Tool tips
4571 ***********************************************************************/
4573 static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
4574 Lisp_Object, Lisp_Object));
4575 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
4576 Lisp_Object, int, int, int *, int *));
4578 /* The frame of a currently visible tooltip. */
4580 Lisp_Object tip_frame;
4582 /* If non-nil, a timer started that hides the last tooltip when it
4583 fires. */
4585 Lisp_Object tip_timer;
4586 Window tip_window;
4588 /* If non-nil, a vector of 3 elements containing the last args
4589 with which x-show-tip was called. See there. */
4591 Lisp_Object last_show_tip_args;
4593 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
4595 Lisp_Object Vx_max_tooltip_size;
4598 static Lisp_Object
4599 unwind_create_tip_frame (frame)
4600 Lisp_Object frame;
4602 Lisp_Object deleted;
4604 deleted = unwind_create_frame (frame);
4605 if (EQ (deleted, Qt))
4607 tip_window = None;
4608 tip_frame = Qnil;
4611 return deleted;
4615 /* Create a frame for a tooltip on the display described by DPYINFO.
4616 PARMS is a list of frame parameters. TEXT is the string to
4617 display in the tip frame. Value is the frame.
4619 Note that functions called here, esp. x_default_parameter can
4620 signal errors, for instance when a specified color name is
4621 undefined. We have to make sure that we're in a consistent state
4622 when this happens. */
4624 static Lisp_Object
4625 x_create_tip_frame (dpyinfo, parms, text)
4626 struct x_display_info *dpyinfo;
4627 Lisp_Object parms, text;
4629 struct frame *f;
4630 Lisp_Object frame, tem;
4631 Lisp_Object name;
4632 long window_prompting = 0;
4633 int width, height;
4634 int count = SPECPDL_INDEX ();
4635 struct gcpro gcpro1, gcpro2, gcpro3;
4636 int face_change_count_before = face_change_count;
4637 Lisp_Object buffer;
4638 struct buffer *old_buffer;
4640 check_x ();
4642 /* Get the name of the frame to use for resource lookup. */
4643 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4644 if (!STRINGP (name)
4645 && !EQ (name, Qunbound)
4646 && !NILP (name))
4647 error ("Invalid frame name--not a string or nil");
4649 frame = Qnil;
4650 GCPRO3 (parms, name, frame);
4651 f = make_frame (1);
4652 XSETFRAME (frame, f);
4654 buffer = Fget_buffer_create (build_string (" *tip*"));
4655 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
4656 old_buffer = current_buffer;
4657 set_buffer_internal_1 (XBUFFER (buffer));
4658 current_buffer->truncate_lines = Qnil;
4659 specbind (Qinhibit_read_only, Qt);
4660 specbind (Qinhibit_modification_hooks, Qt);
4661 Ferase_buffer ();
4662 Finsert (1, &text);
4663 set_buffer_internal_1 (old_buffer);
4665 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
4666 record_unwind_protect (unwind_create_tip_frame, frame);
4668 f->terminal = dpyinfo->terminal;
4669 f->terminal->reference_count++;
4671 /* By setting the output method, we're essentially saying that
4672 the frame is live, as per FRAME_LIVE_P. If we get a signal
4673 from this point on, x_destroy_window might screw up reference
4674 counts etc. */
4675 f->output_method = output_x_window;
4676 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
4677 bzero (f->output_data.x, sizeof (struct x_output));
4678 f->output_data.x->icon_bitmap = -1;
4679 FRAME_FONTSET (f) = -1;
4680 f->output_data.x->scroll_bar_foreground_pixel = -1;
4681 f->output_data.x->scroll_bar_background_pixel = -1;
4682 #ifdef USE_TOOLKIT_SCROLL_BARS
4683 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4684 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4685 #endif /* USE_TOOLKIT_SCROLL_BARS */
4686 f->icon_name = Qnil;
4687 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4688 #if GLYPH_DEBUG
4689 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
4690 dpyinfo_refcount = dpyinfo->reference_count;
4691 #endif /* GLYPH_DEBUG */
4692 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4693 f->output_data.x->explicit_parent = 0;
4695 /* These colors will be set anyway later, but it's important
4696 to get the color reference counts right, so initialize them! */
4698 Lisp_Object black;
4699 struct gcpro gcpro1;
4701 /* Function x_decode_color can signal an error. Make
4702 sure to initialize color slots so that we won't try
4703 to free colors we haven't allocated. */
4704 f->output_data.x->foreground_pixel = -1;
4705 f->output_data.x->background_pixel = -1;
4706 f->output_data.x->cursor_pixel = -1;
4707 f->output_data.x->cursor_foreground_pixel = -1;
4708 f->output_data.x->border_pixel = -1;
4709 f->output_data.x->mouse_pixel = -1;
4711 black = build_string ("black");
4712 GCPRO1 (black);
4713 f->output_data.x->foreground_pixel
4714 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4715 f->output_data.x->background_pixel
4716 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4717 f->output_data.x->cursor_pixel
4718 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4719 f->output_data.x->cursor_foreground_pixel
4720 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4721 f->output_data.x->border_pixel
4722 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4723 f->output_data.x->mouse_pixel
4724 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4725 UNGCPRO;
4728 /* Set the name; the functions to which we pass f expect the name to
4729 be set. */
4730 if (EQ (name, Qunbound) || NILP (name))
4732 f->name = build_string (dpyinfo->x_id_name);
4733 f->explicit_name = 0;
4735 else
4737 f->name = name;
4738 f->explicit_name = 1;
4739 /* use the frame's title when getting resources for this frame. */
4740 specbind (Qx_resource_name, name);
4743 /* Extract the window parameters from the supplied values that are
4744 needed to determine window geometry. */
4746 Lisp_Object font;
4748 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4750 BLOCK_INPUT;
4751 /* First, try whatever font the caller has specified. */
4752 if (STRINGP (font))
4754 tem = Fquery_fontset (font, Qnil);
4755 if (STRINGP (tem))
4756 font = x_new_fontset (f, SDATA (tem));
4757 else
4758 font = x_new_font (f, SDATA (font));
4761 /* Try out a font which we hope has bold and italic variations. */
4762 if (!STRINGP (font))
4763 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
4764 if (!STRINGP (font))
4765 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4766 if (! STRINGP (font))
4767 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4768 if (! STRINGP (font))
4769 /* This was formerly the first thing tried, but it finds too many fonts
4770 and takes too long. */
4771 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
4772 /* If those didn't work, look for something which will at least work. */
4773 if (! STRINGP (font))
4774 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
4775 UNBLOCK_INPUT;
4776 if (! STRINGP (font))
4777 font = build_string ("fixed");
4779 x_default_parameter (f, parms, Qfont, font,
4780 "font", "Font", RES_TYPE_STRING);
4783 x_default_parameter (f, parms, Qborder_width, make_number (2),
4784 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4786 /* This defaults to 2 in order to match xterm. We recognize either
4787 internalBorderWidth or internalBorder (which is what xterm calls
4788 it). */
4789 if (NILP (Fassq (Qinternal_border_width, parms)))
4791 Lisp_Object value;
4793 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4794 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4795 if (! EQ (value, Qunbound))
4796 parms = Fcons (Fcons (Qinternal_border_width, value),
4797 parms);
4800 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4801 "internalBorderWidth", "internalBorderWidth",
4802 RES_TYPE_NUMBER);
4804 /* Also do the stuff which must be set before the window exists. */
4805 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4806 "foreground", "Foreground", RES_TYPE_STRING);
4807 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4808 "background", "Background", RES_TYPE_STRING);
4809 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4810 "pointerColor", "Foreground", RES_TYPE_STRING);
4811 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4812 "cursorColor", "Foreground", RES_TYPE_STRING);
4813 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4814 "borderColor", "BorderColor", RES_TYPE_STRING);
4816 /* Init faces before x_default_parameter is called for scroll-bar
4817 parameters because that function calls x_set_scroll_bar_width,
4818 which calls change_frame_size, which calls Fset_window_buffer,
4819 which runs hooks, which call Fvertical_motion. At the end, we
4820 end up in init_iterator with a null face cache, which should not
4821 happen. */
4822 init_frame_faces (f);
4824 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4826 window_prompting = x_figure_window_size (f, parms, 0);
4829 XSetWindowAttributes attrs;
4830 unsigned long mask;
4832 BLOCK_INPUT;
4833 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4834 if (DoesSaveUnders (dpyinfo->screen))
4835 mask |= CWSaveUnder;
4837 /* Window managers look at the override-redirect flag to determine
4838 whether or net to give windows a decoration (Xlib spec, chapter
4839 3.2.8). */
4840 attrs.override_redirect = True;
4841 attrs.save_under = True;
4842 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4843 /* Arrange for getting MapNotify and UnmapNotify events. */
4844 attrs.event_mask = StructureNotifyMask;
4845 tip_window
4846 = FRAME_X_WINDOW (f)
4847 = XCreateWindow (FRAME_X_DISPLAY (f),
4848 FRAME_X_DISPLAY_INFO (f)->root_window,
4849 /* x, y, width, height */
4850 0, 0, 1, 1,
4851 /* Border. */
4853 CopyFromParent, InputOutput, CopyFromParent,
4854 mask, &attrs);
4855 UNBLOCK_INPUT;
4858 x_make_gc (f);
4860 x_default_parameter (f, parms, Qauto_raise, Qnil,
4861 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4862 x_default_parameter (f, parms, Qauto_lower, Qnil,
4863 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4864 x_default_parameter (f, parms, Qcursor_type, Qbox,
4865 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4867 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4868 Change will not be effected unless different from the current
4869 FRAME_LINES (f). */
4870 width = FRAME_COLS (f);
4871 height = FRAME_LINES (f);
4872 SET_FRAME_COLS (f, 0);
4873 FRAME_LINES (f) = 0;
4874 change_frame_size (f, height, width, 1, 0, 0);
4876 /* Add `tooltip' frame parameter's default value. */
4877 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
4878 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
4879 Qnil));
4881 /* Set up faces after all frame parameters are known. This call
4882 also merges in face attributes specified for new frames.
4884 Frame parameters may be changed if .Xdefaults contains
4885 specifications for the default font. For example, if there is an
4886 `Emacs.default.attributeBackground: pink', the `background-color'
4887 attribute of the frame get's set, which let's the internal border
4888 of the tooltip frame appear in pink. Prevent this. */
4890 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
4892 /* Set tip_frame here, so that */
4893 tip_frame = frame;
4894 call1 (Qface_set_after_frame_default, frame);
4896 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
4897 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
4898 Qnil));
4901 Fmodify_frame_parameters (frame, Fcons (Fcons (Qwindow_system, Qx), Qnil));
4903 f->no_split = 1;
4905 UNGCPRO;
4907 /* It is now ok to make the frame official even if we get an error
4908 below. And the frame needs to be on Vframe_list or making it
4909 visible won't work. */
4910 Vframe_list = Fcons (frame, Vframe_list);
4912 /* Now that the frame is official, it counts as a reference to
4913 its display. */
4914 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4916 /* Setting attributes of faces of the tooltip frame from resources
4917 and similar will increment face_change_count, which leads to the
4918 clearing of all current matrices. Since this isn't necessary
4919 here, avoid it by resetting face_change_count to the value it
4920 had before we created the tip frame. */
4921 face_change_count = face_change_count_before;
4923 /* Discard the unwind_protect. */
4924 return unbind_to (count, frame);
4928 /* Compute where to display tip frame F. PARMS is the list of frame
4929 parameters for F. DX and DY are specified offsets from the current
4930 location of the mouse. WIDTH and HEIGHT are the width and height
4931 of the tooltip. Return coordinates relative to the root window of
4932 the display in *ROOT_X, and *ROOT_Y. */
4934 static void
4935 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
4936 struct frame *f;
4937 Lisp_Object parms, dx, dy;
4938 int width, height;
4939 int *root_x, *root_y;
4941 Lisp_Object left, top;
4942 int win_x, win_y;
4943 Window root, child;
4944 unsigned pmask;
4946 /* User-specified position? */
4947 left = Fcdr (Fassq (Qleft, parms));
4948 top = Fcdr (Fassq (Qtop, parms));
4950 /* Move the tooltip window where the mouse pointer is. Resize and
4951 show it. */
4952 if (!INTEGERP (left) || !INTEGERP (top))
4954 BLOCK_INPUT;
4955 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
4956 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
4957 UNBLOCK_INPUT;
4960 if (INTEGERP (top))
4961 *root_y = XINT (top);
4962 else if (*root_y + XINT (dy) <= 0)
4963 *root_y = 0; /* Can happen for negative dy */
4964 else if (*root_y + XINT (dy) + height <= FRAME_X_DISPLAY_INFO (f)->height)
4965 /* It fits below the pointer */
4966 *root_y += XINT (dy);
4967 else if (height + XINT (dy) <= *root_y)
4968 /* It fits above the pointer. */
4969 *root_y -= height + XINT (dy);
4970 else
4971 /* Put it on the top. */
4972 *root_y = 0;
4974 if (INTEGERP (left))
4975 *root_x = XINT (left);
4976 else if (*root_x + XINT (dx) <= 0)
4977 *root_x = 0; /* Can happen for negative dx */
4978 else if (*root_x + XINT (dx) + width <= FRAME_X_DISPLAY_INFO (f)->width)
4979 /* It fits to the right of the pointer. */
4980 *root_x += XINT (dx);
4981 else if (width + XINT (dx) <= *root_x)
4982 /* It fits to the left of the pointer. */
4983 *root_x -= width + XINT (dx);
4984 else
4985 /* Put it left-justified on the screen--it ought to fit that way. */
4986 *root_x = 0;
4990 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
4991 doc: /* Show STRING in a "tooltip" window on frame FRAME.
4992 A tooltip window is a small X window displaying a string.
4994 FRAME nil or omitted means use the selected frame.
4996 PARMS is an optional list of frame parameters which can be used to
4997 change the tooltip's appearance.
4999 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
5000 means use the default timeout of 5 seconds.
5002 If the list of frame parameters PARAMS contains a `left' parameters,
5003 the tooltip is displayed at that x-position. Otherwise it is
5004 displayed at the mouse position, with offset DX added (default is 5 if
5005 DX isn't specified). Likewise for the y-position; if a `top' frame
5006 parameter is specified, it determines the y-position of the tooltip
5007 window, otherwise it is displayed at the mouse position, with offset
5008 DY added (default is -10).
5010 A tooltip's maximum size is specified by `x-max-tooltip-size'.
5011 Text larger than the specified size is clipped. */)
5012 (string, frame, parms, timeout, dx, dy)
5013 Lisp_Object string, frame, parms, timeout, dx, dy;
5015 struct frame *f;
5016 struct window *w;
5017 int root_x, root_y;
5018 struct buffer *old_buffer;
5019 struct text_pos pos;
5020 int i, width, height;
5021 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5022 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5023 int count = SPECPDL_INDEX ();
5025 specbind (Qinhibit_redisplay, Qt);
5027 GCPRO4 (string, parms, frame, timeout);
5029 CHECK_STRING (string);
5030 f = check_x_frame (frame);
5031 if (NILP (timeout))
5032 timeout = make_number (5);
5033 else
5034 CHECK_NATNUM (timeout);
5036 if (NILP (dx))
5037 dx = make_number (5);
5038 else
5039 CHECK_NUMBER (dx);
5041 if (NILP (dy))
5042 dy = make_number (-10);
5043 else
5044 CHECK_NUMBER (dy);
5046 if (NILP (last_show_tip_args))
5047 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5049 if (!NILP (tip_frame))
5051 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5052 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5053 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5055 if (EQ (frame, last_frame)
5056 && !NILP (Fequal (last_string, string))
5057 && !NILP (Fequal (last_parms, parms)))
5059 struct frame *f = XFRAME (tip_frame);
5061 /* Only DX and DY have changed. */
5062 if (!NILP (tip_timer))
5064 Lisp_Object timer = tip_timer;
5065 tip_timer = Qnil;
5066 call1 (Qcancel_timer, timer);
5069 BLOCK_INPUT;
5070 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5071 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5072 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5073 root_x, root_y);
5074 UNBLOCK_INPUT;
5075 goto start_timer;
5079 /* Hide a previous tip, if any. */
5080 Fx_hide_tip ();
5082 ASET (last_show_tip_args, 0, string);
5083 ASET (last_show_tip_args, 1, frame);
5084 ASET (last_show_tip_args, 2, parms);
5086 /* Add default values to frame parameters. */
5087 if (NILP (Fassq (Qname, parms)))
5088 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5089 if (NILP (Fassq (Qinternal_border_width, parms)))
5090 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5091 if (NILP (Fassq (Qborder_width, parms)))
5092 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5093 if (NILP (Fassq (Qborder_color, parms)))
5094 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5095 if (NILP (Fassq (Qbackground_color, parms)))
5096 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5097 parms);
5099 /* Create a frame for the tooltip, and record it in the global
5100 variable tip_frame. */
5101 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
5102 f = XFRAME (frame);
5104 /* Set up the frame's root window. */
5105 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5106 w->left_col = w->top_line = make_number (0);
5108 if (CONSP (Vx_max_tooltip_size)
5109 && INTEGERP (XCAR (Vx_max_tooltip_size))
5110 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5111 && INTEGERP (XCDR (Vx_max_tooltip_size))
5112 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5114 w->total_cols = XCAR (Vx_max_tooltip_size);
5115 w->total_lines = XCDR (Vx_max_tooltip_size);
5117 else
5119 w->total_cols = make_number (80);
5120 w->total_lines = make_number (40);
5123 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5124 adjust_glyphs (f);
5125 w->pseudo_window_p = 1;
5127 /* Display the tooltip text in a temporary buffer. */
5128 old_buffer = current_buffer;
5129 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5130 current_buffer->truncate_lines = Qnil;
5131 clear_glyph_matrix (w->desired_matrix);
5132 clear_glyph_matrix (w->current_matrix);
5133 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5134 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5136 /* Compute width and height of the tooltip. */
5137 width = height = 0;
5138 for (i = 0; i < w->desired_matrix->nrows; ++i)
5140 struct glyph_row *row = &w->desired_matrix->rows[i];
5141 struct glyph *last;
5142 int row_width;
5144 /* Stop at the first empty row at the end. */
5145 if (!row->enabled_p || !row->displays_text_p)
5146 break;
5148 /* Let the row go over the full width of the frame. */
5149 row->full_width_p = 1;
5151 /* There's a glyph at the end of rows that is used to place
5152 the cursor there. Don't include the width of this glyph. */
5153 if (row->used[TEXT_AREA])
5155 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5156 row_width = row->pixel_width - last->pixel_width;
5158 else
5159 row_width = row->pixel_width;
5161 height += row->height;
5162 width = max (width, row_width);
5165 /* Add the frame's internal border to the width and height the X
5166 window should have. */
5167 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5168 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5170 /* Move the tooltip window where the mouse pointer is. Resize and
5171 show it. */
5172 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5174 BLOCK_INPUT;
5175 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5176 root_x, root_y, width, height);
5177 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5178 UNBLOCK_INPUT;
5180 /* Draw into the window. */
5181 w->must_be_updated_p = 1;
5182 update_single_window (w, 1);
5184 /* Restore original current buffer. */
5185 set_buffer_internal_1 (old_buffer);
5186 windows_or_buffers_changed = old_windows_or_buffers_changed;
5188 start_timer:
5189 /* Let the tip disappear after timeout seconds. */
5190 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5191 intern ("x-hide-tip"));
5193 UNGCPRO;
5194 return unbind_to (count, Qnil);
5198 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5199 doc: /* Hide the current tooltip window, if there is any.
5200 Value is t if tooltip was open, nil otherwise. */)
5203 int count;
5204 Lisp_Object deleted, frame, timer;
5205 struct gcpro gcpro1, gcpro2;
5207 /* Return quickly if nothing to do. */
5208 if (NILP (tip_timer) && NILP (tip_frame))
5209 return Qnil;
5211 frame = tip_frame;
5212 timer = tip_timer;
5213 GCPRO2 (frame, timer);
5214 tip_frame = tip_timer = deleted = Qnil;
5216 count = SPECPDL_INDEX ();
5217 specbind (Qinhibit_redisplay, Qt);
5218 specbind (Qinhibit_quit, Qt);
5220 if (!NILP (timer))
5221 call1 (Qcancel_timer, timer);
5223 if (FRAMEP (frame))
5225 Fdelete_frame (frame, Qnil);
5226 deleted = Qt;
5228 #ifdef USE_LUCID
5229 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5230 redisplay procedure is not called when a tip frame over menu
5231 items is unmapped. Redisplay the menu manually... */
5233 struct frame *f = SELECTED_FRAME ();
5234 Widget w = f->output_data.x->menubar_widget;
5235 extern void xlwmenu_redisplay P_ ((Widget));
5237 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5238 && w != NULL)
5240 BLOCK_INPUT;
5241 xlwmenu_redisplay (w);
5242 UNBLOCK_INPUT;
5245 #endif /* USE_LUCID */
5248 UNGCPRO;
5249 return unbind_to (count, deleted);
5254 /***********************************************************************
5255 File selection dialog
5256 ***********************************************************************/
5258 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5259 Sx_uses_old_gtk_dialog,
5260 0, 0, 0,
5261 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5264 #ifdef USE_GTK
5265 extern int use_dialog_box;
5266 extern int use_file_dialog;
5268 if (use_dialog_box
5269 && use_file_dialog
5270 && have_menus_p ()
5271 && xg_uses_old_file_dialog ())
5272 return Qt;
5273 #endif
5274 return Qnil;
5278 #ifdef USE_MOTIF
5279 /* Callback for "OK" and "Cancel" on file selection dialog. */
5281 static void
5282 file_dialog_cb (widget, client_data, call_data)
5283 Widget widget;
5284 XtPointer call_data, client_data;
5286 int *result = (int *) client_data;
5287 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5288 *result = cb->reason;
5292 /* Callback for unmapping a file selection dialog. This is used to
5293 capture the case where a dialog is closed via a window manager's
5294 closer button, for example. Using a XmNdestroyCallback didn't work
5295 in this case. */
5297 static void
5298 file_dialog_unmap_cb (widget, client_data, call_data)
5299 Widget widget;
5300 XtPointer call_data, client_data;
5302 int *result = (int *) client_data;
5303 *result = XmCR_CANCEL;
5306 static Lisp_Object
5307 clean_up_file_dialog (arg)
5308 Lisp_Object arg;
5310 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
5311 Widget dialog = (Widget) p->pointer;
5313 /* Clean up. */
5314 BLOCK_INPUT;
5315 XtUnmanageChild (dialog);
5316 XtDestroyWidget (dialog);
5317 x_menu_set_in_use (0);
5318 UNBLOCK_INPUT;
5320 return Qnil;
5324 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5325 doc: /* Read file name, prompting with PROMPT in directory DIR.
5326 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5327 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5328 or directory must exist. ONLY-DIR-P is ignored." */)
5329 (prompt, dir, default_filename, mustmatch, only_dir_p)
5330 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
5332 int result;
5333 struct frame *f = SELECTED_FRAME ();
5334 Lisp_Object file = Qnil;
5335 Lisp_Object decoded_file;
5336 Widget dialog, text, help;
5337 Arg al[10];
5338 int ac = 0;
5339 extern XtAppContext Xt_app_con;
5340 XmString dir_xmstring, pattern_xmstring;
5341 int count = SPECPDL_INDEX ();
5342 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5344 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5346 if (popup_activated ())
5347 error ("Trying to use a menu from within a menu-entry");
5349 CHECK_STRING (prompt);
5350 CHECK_STRING (dir);
5352 /* Prevent redisplay. */
5353 specbind (Qinhibit_redisplay, Qt);
5355 BLOCK_INPUT;
5357 /* Create the dialog with PROMPT as title, using DIR as initial
5358 directory and using "*" as pattern. */
5359 dir = Fexpand_file_name (dir, Qnil);
5360 dir_xmstring = XmStringCreateLocalized (SDATA (dir));
5361 pattern_xmstring = XmStringCreateLocalized ("*");
5363 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5364 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5365 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5366 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5367 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5368 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5369 "fsb", al, ac);
5370 XmStringFree (dir_xmstring);
5371 XmStringFree (pattern_xmstring);
5373 /* Add callbacks for OK and Cancel. */
5374 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5375 (XtPointer) &result);
5376 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5377 (XtPointer) &result);
5378 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5379 (XtPointer) &result);
5381 /* Remove the help button since we can't display help. */
5382 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5383 XtUnmanageChild (help);
5385 /* Mark OK button as default. */
5386 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5387 XmNshowAsDefault, True, NULL);
5389 /* If MUSTMATCH is non-nil, disable the file entry field of the
5390 dialog, so that the user must select a file from the files list
5391 box. We can't remove it because we wouldn't have a way to get at
5392 the result file name, then. */
5393 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5394 if (!NILP (mustmatch))
5396 Widget label;
5397 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5398 XtSetSensitive (text, False);
5399 XtSetSensitive (label, False);
5402 /* Manage the dialog, so that list boxes get filled. */
5403 XtManageChild (dialog);
5405 if (STRINGP (default_filename))
5407 XmString default_xmstring;
5408 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5409 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5411 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5412 XmTextFieldReplace (wtext, 0, last_pos,
5413 (SDATA (Ffile_name_nondirectory (default_filename))));
5415 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5416 must include the path for this to work. */
5418 default_xmstring = XmStringCreateLocalized (SDATA (default_filename));
5420 if (XmListItemExists (list, default_xmstring))
5422 int item_pos = XmListItemPos (list, default_xmstring);
5423 /* Select the item and scroll it into view. */
5424 XmListSelectPos (list, item_pos, True);
5425 XmListSetPos (list, item_pos);
5428 XmStringFree (default_xmstring);
5431 record_unwind_protect (clean_up_file_dialog, make_save_value (dialog, 0));
5433 /* Process events until the user presses Cancel or OK. */
5434 x_menu_set_in_use (1);
5435 result = 0;
5436 while (result == 0)
5438 XEvent event;
5439 x_menu_wait_for_event (0);
5440 XtAppNextEvent (Xt_app_con, &event);
5441 if (event.type == KeyPress
5442 && FRAME_X_DISPLAY (f) == event.xkey.display)
5444 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5446 /* Pop down on C-g. */
5447 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5448 XtUnmanageChild (dialog);
5451 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5454 /* Get the result. */
5455 if (result == XmCR_OK)
5457 XmString text;
5458 String data;
5460 XtVaGetValues (dialog, XmNtextString, &text, NULL);
5461 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
5462 XmStringFree (text);
5463 file = build_string (data);
5464 XtFree (data);
5466 else
5467 file = Qnil;
5469 UNBLOCK_INPUT;
5470 UNGCPRO;
5472 /* Make "Cancel" equivalent to C-g. */
5473 if (NILP (file))
5474 Fsignal (Qquit, Qnil);
5476 decoded_file = DECODE_FILE (file);
5478 return unbind_to (count, decoded_file);
5481 #endif /* USE_MOTIF */
5483 #ifdef USE_GTK
5485 static Lisp_Object
5486 clean_up_dialog (arg)
5487 Lisp_Object arg;
5489 x_menu_set_in_use (0);
5491 return Qnil;
5494 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5495 doc: /* Read file name, prompting with PROMPT in directory DIR.
5496 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5497 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5498 or directory must exist. If ONLY-DIR-P is non-nil, the user can only select
5499 directories. */)
5500 (prompt, dir, default_filename, mustmatch, only_dir_p)
5501 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
5503 FRAME_PTR f = SELECTED_FRAME ();
5504 char *fn;
5505 Lisp_Object file = Qnil;
5506 Lisp_Object decoded_file;
5507 int count = SPECPDL_INDEX ();
5508 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5509 char *cdef_file;
5511 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5513 if (popup_activated ())
5514 error ("Trying to use a menu from within a menu-entry");
5516 CHECK_STRING (prompt);
5517 CHECK_STRING (dir);
5519 /* Prevent redisplay. */
5520 specbind (Qinhibit_redisplay, Qt);
5521 record_unwind_protect (clean_up_dialog, Qnil);
5523 BLOCK_INPUT;
5525 if (STRINGP (default_filename))
5526 cdef_file = SDATA (default_filename);
5527 else
5528 cdef_file = SDATA (dir);
5530 fn = xg_get_file_name (f, SDATA (prompt), cdef_file,
5531 ! NILP (mustmatch),
5532 ! NILP (only_dir_p));
5534 if (fn)
5536 file = build_string (fn);
5537 xfree (fn);
5540 UNBLOCK_INPUT;
5541 UNGCPRO;
5543 /* Make "Cancel" equivalent to C-g. */
5544 if (NILP (file))
5545 Fsignal (Qquit, Qnil);
5547 decoded_file = DECODE_FILE (file);
5549 return unbind_to (count, decoded_file);
5552 #endif /* USE_GTK */
5555 /***********************************************************************
5556 Keyboard
5557 ***********************************************************************/
5559 #ifdef HAVE_XKBGETKEYBOARD
5560 #include <X11/XKBlib.h>
5561 #include <X11/keysym.h>
5562 #endif
5564 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5565 Sx_backspace_delete_keys_p, 0, 1, 0,
5566 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5567 FRAME nil means use the selected frame.
5568 Value is t if we know that both keys are present, and are mapped to the
5569 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5570 present and mapped to the usual X keysyms. */)
5571 (frame)
5572 Lisp_Object frame;
5574 #ifdef HAVE_XKBGETKEYBOARD
5575 XkbDescPtr kb;
5576 struct frame *f = check_x_frame (frame);
5577 Display *dpy = FRAME_X_DISPLAY (f);
5578 Lisp_Object have_keys;
5579 int major, minor, op, event, error;
5581 BLOCK_INPUT;
5583 /* Check library version in case we're dynamically linked. */
5584 major = XkbMajorVersion;
5585 minor = XkbMinorVersion;
5586 if (!XkbLibraryVersion (&major, &minor))
5588 UNBLOCK_INPUT;
5589 return Qlambda;
5592 /* Check that the server supports XKB. */
5593 major = XkbMajorVersion;
5594 minor = XkbMinorVersion;
5595 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
5597 UNBLOCK_INPUT;
5598 return Qlambda;
5601 /* In this code we check that the keyboard has physical keys with names
5602 that start with BKSP (Backspace) and DELE (Delete), and that they
5603 generate keysym XK_BackSpace and XK_Delete respectively.
5604 This function is used to test if normal-erase-is-backspace should be
5605 turned on.
5606 An alternative approach would be to just check if XK_BackSpace and
5607 XK_Delete are mapped to any key. But if any of those are mapped to
5608 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5609 user doesn't know about it, it is better to return false here.
5610 It is more obvious to the user what to do if she/he has two keys
5611 clearly marked with names/symbols and one key does something not
5612 expected (i.e. she/he then tries the other).
5613 The cases where Backspace/Delete is mapped to some other key combination
5614 are rare, and in those cases, normal-erase-is-backspace can be turned on
5615 manually. */
5617 have_keys = Qnil;
5618 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5619 if (kb)
5621 int delete_keycode = 0, backspace_keycode = 0, i;
5623 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5625 for (i = kb->min_key_code;
5626 (i < kb->max_key_code
5627 && (delete_keycode == 0 || backspace_keycode == 0));
5628 ++i)
5630 /* The XKB symbolic key names can be seen most easily in
5631 the PS file generated by `xkbprint -label name
5632 $DISPLAY'. */
5633 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5634 delete_keycode = i;
5635 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5636 backspace_keycode = i;
5639 XkbFreeNames (kb, 0, True);
5642 XkbFreeClientMap (kb, 0, True);
5644 if (delete_keycode
5645 && backspace_keycode
5646 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5647 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5648 have_keys = Qt;
5650 UNBLOCK_INPUT;
5651 return have_keys;
5652 #else /* not HAVE_XKBGETKEYBOARD */
5653 return Qlambda;
5654 #endif /* not HAVE_XKBGETKEYBOARD */
5659 /***********************************************************************
5660 Initialization
5661 ***********************************************************************/
5663 /* Keep this list in the same order as frame_parms in frame.c.
5664 Use 0 for unsupported frame parameters. */
5666 frame_parm_handler x_frame_parm_handlers[] =
5668 x_set_autoraise,
5669 x_set_autolower,
5670 x_set_background_color,
5671 x_set_border_color,
5672 x_set_border_width,
5673 x_set_cursor_color,
5674 x_set_cursor_type,
5675 x_set_font,
5676 x_set_foreground_color,
5677 x_set_icon_name,
5678 x_set_icon_type,
5679 x_set_internal_border_width,
5680 x_set_menu_bar_lines,
5681 x_set_mouse_color,
5682 x_explicitly_set_name,
5683 x_set_scroll_bar_width,
5684 x_set_title,
5685 x_set_unsplittable,
5686 x_set_vertical_scroll_bars,
5687 x_set_visibility,
5688 x_set_tool_bar_lines,
5689 x_set_scroll_bar_foreground,
5690 x_set_scroll_bar_background,
5691 x_set_screen_gamma,
5692 x_set_line_spacing,
5693 x_set_fringe_width,
5694 x_set_fringe_width,
5695 x_set_wait_for_wm,
5696 x_set_fullscreen,
5699 void
5700 syms_of_xfns ()
5702 /* This is zero if not using X windows. */
5703 x_in_use = 0;
5705 /* The section below is built by the lisp expression at the top of the file,
5706 just above where these variables are declared. */
5707 /*&&& init symbols here &&&*/
5708 Qnone = intern ("none");
5709 staticpro (&Qnone);
5710 Qsuppress_icon = intern ("suppress-icon");
5711 staticpro (&Qsuppress_icon);
5712 Qundefined_color = intern ("undefined-color");
5713 staticpro (&Qundefined_color);
5714 Qcompound_text = intern ("compound-text");
5715 staticpro (&Qcompound_text);
5716 Qcancel_timer = intern ("cancel-timer");
5717 staticpro (&Qcancel_timer);
5718 /* This is the end of symbol initialization. */
5720 /* Text property `display' should be nonsticky by default. */
5721 Vtext_property_default_nonsticky
5722 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
5725 Fput (Qundefined_color, Qerror_conditions,
5726 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
5727 Fput (Qundefined_color, Qerror_message,
5728 build_string ("Undefined color"));
5730 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
5731 doc: /* The shape of the pointer when over text.
5732 Changing the value does not affect existing frames
5733 unless you set the mouse color. */);
5734 Vx_pointer_shape = Qnil;
5736 #if 0 /* This doesn't really do anything. */
5737 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
5738 doc: /* The shape of the pointer when not over text.
5739 This variable takes effect when you create a new frame
5740 or when you set the mouse color. */);
5741 #endif
5742 Vx_nontext_pointer_shape = Qnil;
5744 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
5745 doc: /* The shape of the pointer when Emacs is busy.
5746 This variable takes effect when you create a new frame
5747 or when you set the mouse color. */);
5748 Vx_hourglass_pointer_shape = Qnil;
5750 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
5751 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
5752 display_hourglass_p = 1;
5754 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
5755 doc: /* *Seconds to wait before displaying an hourglass pointer.
5756 Value must be an integer or float. */);
5757 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
5759 #if 0 /* This doesn't really do anything. */
5760 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
5761 doc: /* The shape of the pointer when over the mode line.
5762 This variable takes effect when you create a new frame
5763 or when you set the mouse color. */);
5764 #endif
5765 Vx_mode_pointer_shape = Qnil;
5767 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
5768 &Vx_sensitive_text_pointer_shape,
5769 doc: /* The shape of the pointer when over mouse-sensitive text.
5770 This variable takes effect when you create a new frame
5771 or when you set the mouse color. */);
5772 Vx_sensitive_text_pointer_shape = Qnil;
5774 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
5775 &Vx_window_horizontal_drag_shape,
5776 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
5777 This variable takes effect when you create a new frame
5778 or when you set the mouse color. */);
5779 Vx_window_horizontal_drag_shape = Qnil;
5781 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
5782 doc: /* A string indicating the foreground color of the cursor box. */);
5783 Vx_cursor_fore_pixel = Qnil;
5785 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
5786 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
5787 Text larger than this is clipped. */);
5788 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
5790 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
5791 doc: /* Non-nil if no X window manager is in use.
5792 Emacs doesn't try to figure this out; this is always nil
5793 unless you set it to something else. */);
5794 /* We don't have any way to find this out, so set it to nil
5795 and maybe the user would like to set it to t. */
5796 Vx_no_window_manager = Qnil;
5798 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
5799 &Vx_pixel_size_width_font_regexp,
5800 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
5802 Since Emacs gets width of a font matching with this regexp from
5803 PIXEL_SIZE field of the name, font finding mechanism gets faster for
5804 such a font. This is especially effective for such large fonts as
5805 Chinese, Japanese, and Korean. */);
5806 Vx_pixel_size_width_font_regexp = Qnil;
5808 /* This is not ifdef:ed, so other builds than GTK can customize it. */
5809 DEFVAR_BOOL ("x-use-old-gtk-file-dialog", &x_use_old_gtk_file_dialog,
5810 doc: /* *Non-nil means prompt with the old GTK file selection dialog.
5811 If nil or if the file selection dialog is not available, the new GTK file
5812 chooser is used instead. To turn off all file dialogs set the
5813 variable `use-file-dialog'. */);
5814 x_use_old_gtk_file_dialog = 0;
5816 DEFVAR_BOOL ("x-gtk-show-hidden-files", &x_gtk_show_hidden_files,
5817 doc: /* *If non-nil, the GTK file chooser will by default show hidden files.
5818 Note that this is just the default, there is a toggle button on the file
5819 chooser to show or not show hidden files on a case by case basis. */);
5820 x_gtk_show_hidden_files = 0;
5822 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", &x_gtk_whole_detached_tool_bar,
5823 doc: /* *If non-nil, a detached tool bar is shown in full.
5824 The default is to just show an arrow and pressing on that arrow shows
5825 the tool bar buttons. */);
5826 x_gtk_whole_detached_tool_bar = 0;
5828 Fprovide (intern ("x"), Qnil);
5830 #ifdef USE_X_TOOLKIT
5831 Fprovide (intern ("x-toolkit"), Qnil);
5832 #ifdef USE_MOTIF
5833 Fprovide (intern ("motif"), Qnil);
5835 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
5836 doc: /* Version info for LessTif/Motif. */);
5837 Vmotif_version_string = build_string (XmVERSION_STRING);
5838 #endif /* USE_MOTIF */
5839 #endif /* USE_X_TOOLKIT */
5841 #ifdef USE_GTK
5842 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
5843 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
5844 But for a user it is a toolkit for X, and indeed, configure
5845 accepts --with-x-toolkit=gtk. */
5846 Fprovide (intern ("x-toolkit"), Qnil);
5847 Fprovide (intern ("gtk"), Qnil);
5849 DEFVAR_LISP ("gtk-version-string", &Vgtk_version_string,
5850 doc: /* Version info for GTK+. */);
5852 char gtk_version[40];
5853 g_snprintf (gtk_version, sizeof (gtk_version), "%u.%u.%u",
5854 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
5855 Vgtk_version_string = build_string (gtk_version);
5857 #endif /* USE_GTK */
5859 /* X window properties. */
5860 defsubr (&Sx_change_window_property);
5861 defsubr (&Sx_delete_window_property);
5862 defsubr (&Sx_window_property);
5864 defsubr (&Sxw_display_color_p);
5865 defsubr (&Sx_display_grayscale_p);
5866 defsubr (&Sxw_color_defined_p);
5867 defsubr (&Sxw_color_values);
5868 defsubr (&Sx_server_max_request_size);
5869 defsubr (&Sx_server_vendor);
5870 defsubr (&Sx_server_version);
5871 defsubr (&Sx_display_pixel_width);
5872 defsubr (&Sx_display_pixel_height);
5873 defsubr (&Sx_display_mm_width);
5874 defsubr (&Sx_display_mm_height);
5875 defsubr (&Sx_display_screens);
5876 defsubr (&Sx_display_planes);
5877 defsubr (&Sx_display_color_cells);
5878 defsubr (&Sx_display_visual_class);
5879 defsubr (&Sx_display_backing_store);
5880 defsubr (&Sx_display_save_under);
5881 defsubr (&Sx_create_frame);
5882 defsubr (&Sx_open_connection);
5883 defsubr (&Sx_close_connection);
5884 defsubr (&Sx_display_list);
5885 defsubr (&Sx_synchronize);
5886 defsubr (&Sx_focus_frame);
5887 defsubr (&Sx_backspace_delete_keys_p);
5889 /* Setting callback functions for fontset handler. */
5890 get_font_info_func = x_get_font_info;
5892 #if 0 /* This function pointer doesn't seem to be used anywhere.
5893 And the pointer assigned has the wrong type, anyway. */
5894 list_fonts_func = x_list_fonts;
5895 #endif
5897 load_font_func = x_load_font;
5898 find_ccl_program_func = x_find_ccl_program;
5899 query_font_func = x_query_font;
5900 set_frame_fontset_func = x_set_font;
5901 check_window_system_func = check_x;
5903 hourglass_atimer = NULL;
5904 hourglass_shown_p = 0;
5906 defsubr (&Sx_show_tip);
5907 defsubr (&Sx_hide_tip);
5908 tip_timer = Qnil;
5909 staticpro (&tip_timer);
5910 tip_frame = Qnil;
5911 staticpro (&tip_frame);
5913 last_show_tip_args = Qnil;
5914 staticpro (&last_show_tip_args);
5916 defsubr (&Sx_uses_old_gtk_dialog);
5917 #if defined (USE_MOTIF) || defined (USE_GTK)
5918 defsubr (&Sx_file_dialog);
5919 #endif
5922 #endif /* HAVE_X_WINDOWS */
5924 /* arch-tag: 55040d02-5485-4d58-8b22-95a7a05f3288
5925 (do not change this comment) */