(make-autoload): Handle defgroup specially.
[emacs.git] / src / xfns.c
blobc3eb173b4f9738d2871052fe1468aa1b88d2db7a
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 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"
51 #ifdef HAVE_X_WINDOWS
53 #include <ctype.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
57 #ifndef VMS
58 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
59 #include "bitmaps/gray.xbm"
60 #else
61 #include <X11/bitmaps/gray>
62 #endif
63 #else
64 #include "[.bitmaps]gray.xbm"
65 #endif
67 #ifdef USE_GTK
68 #include "gtkutil.h"
69 #endif
71 #ifdef USE_X_TOOLKIT
72 #include <X11/Shell.h>
74 #ifndef USE_MOTIF
75 #include <X11/Xaw/Paned.h>
76 #include <X11/Xaw/Label.h>
77 #endif /* USE_MOTIF */
79 #ifdef USG
80 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
81 #include <X11/Xos.h>
82 #define USG
83 #else
84 #include <X11/Xos.h>
85 #endif
87 #include "widget.h"
89 #include "../lwlib/lwlib.h"
91 #ifdef USE_MOTIF
92 #include <Xm/Xm.h>
93 #include <Xm/DialogS.h>
94 #include <Xm/FileSB.h>
95 #endif
97 /* Do the EDITRES protocol if running X11R5
98 Exception: HP-UX (at least version A.09.05) has X11R5 without EditRes */
100 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
101 #define HACK_EDITRES
102 extern void _XEditResCheckMessages ();
103 #endif /* R5 + Athena */
105 /* Unique id counter for widgets created by the Lucid Widget Library. */
107 extern LWLIB_ID widget_id_tick;
109 #ifdef USE_LUCID
110 /* This is part of a kludge--see lwlib/xlwmenu.c. */
111 extern XFontStruct *xlwmenu_default_font;
112 #endif
114 extern void free_frame_menubar ();
115 extern double atof ();
117 #ifdef USE_MOTIF
119 /* LessTif/Motif version info. */
121 static Lisp_Object Vmotif_version_string;
123 #endif /* USE_MOTIF */
125 #endif /* USE_X_TOOLKIT */
127 #ifdef USE_GTK
129 /* GTK+ version info */
131 static Lisp_Object Vgtk_version_string;
133 #endif /* USE_GTK */
135 #ifdef HAVE_X11R4
136 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
137 #else
138 #define MAXREQUEST(dpy) ((dpy)->max_request_size)
139 #endif
141 /* The gray bitmap `bitmaps/gray'. This is done because xterm.c uses
142 it, and including `bitmaps/gray' more than once is a problem when
143 config.h defines `static' as an empty replacement string. */
145 int gray_bitmap_width = gray_width;
146 int gray_bitmap_height = gray_height;
147 char *gray_bitmap_bits = gray_bits;
149 /* Non-zero means we're allowed to display an hourglass cursor. */
151 int display_hourglass_p;
153 /* Non-zero means prompt with the old GTK file selection dialog. */
155 int x_use_old_gtk_file_dialog;
157 /* The background and shape of the mouse pointer, and shape when not
158 over text or in the modeline. */
160 Lisp_Object Vx_pointer_shape, Vx_nontext_pointer_shape, Vx_mode_pointer_shape;
161 Lisp_Object Vx_hourglass_pointer_shape;
163 /* The shape when over mouse-sensitive text. */
165 Lisp_Object Vx_sensitive_text_pointer_shape;
167 /* If non-nil, the pointer shape to indicate that windows can be
168 dragged horizontally. */
170 Lisp_Object Vx_window_horizontal_drag_shape;
172 /* Color of chars displayed in cursor box. */
174 Lisp_Object Vx_cursor_fore_pixel;
176 /* Nonzero if using X. */
178 static int x_in_use;
180 /* Non nil if no window manager is in use. */
182 Lisp_Object Vx_no_window_manager;
184 /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'. */
186 Lisp_Object Vx_pixel_size_width_font_regexp;
188 Lisp_Object Qnone;
189 Lisp_Object Qsuppress_icon;
190 Lisp_Object Qundefined_color;
191 Lisp_Object Qcompound_text, Qcancel_timer;
193 /* In dispnew.c */
195 extern Lisp_Object Vwindow_system_version;
197 /* The below are defined in frame.c. */
199 #if GLYPH_DEBUG
200 int image_cache_refcount, dpyinfo_refcount;
201 #endif
205 /* Error if we are not connected to X. */
207 void
208 check_x ()
210 if (! x_in_use)
211 error ("X windows are not in use or not initialized");
214 /* Nonzero if we can use mouse menus.
215 You should not call this unless HAVE_MENUS is defined. */
218 have_menus_p ()
220 return x_in_use;
223 /* Extract a frame as a FRAME_PTR, defaulting to the selected frame
224 and checking validity for X. */
226 FRAME_PTR
227 check_x_frame (frame)
228 Lisp_Object frame;
230 FRAME_PTR f;
232 if (NILP (frame))
233 frame = selected_frame;
234 CHECK_LIVE_FRAME (frame);
235 f = XFRAME (frame);
236 if (! FRAME_X_P (f))
237 error ("Non-X frame used");
238 return f;
241 /* Let the user specify an X display with a frame.
242 nil stands for the selected frame--or, if that is not an X frame,
243 the first X display on the list. */
245 struct x_display_info *
246 check_x_display_info (frame)
247 Lisp_Object frame;
249 struct x_display_info *dpyinfo = NULL;
251 if (NILP (frame))
253 struct frame *sf = XFRAME (selected_frame);
255 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
256 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
257 else if (x_display_list != 0)
258 dpyinfo = x_display_list;
259 else
260 error ("X windows are not in use or not initialized");
262 else if (STRINGP (frame))
263 dpyinfo = x_display_info_for_name (frame);
264 else
266 FRAME_PTR f = check_x_frame (frame);
267 dpyinfo = FRAME_X_DISPLAY_INFO (f);
270 return dpyinfo;
274 /* Return the Emacs frame-object corresponding to an X window.
275 It could be the frame's main window or an icon window. */
277 /* This function can be called during GC, so use GC_xxx type test macros. */
279 struct frame *
280 x_window_to_frame (dpyinfo, wdesc)
281 struct x_display_info *dpyinfo;
282 int wdesc;
284 Lisp_Object tail, frame;
285 struct frame *f;
287 if (wdesc == None) return 0;
289 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
291 frame = XCAR (tail);
292 if (!GC_FRAMEP (frame))
293 continue;
294 f = XFRAME (frame);
295 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
296 continue;
297 if (f->output_data.x->hourglass_window == wdesc)
298 return f;
299 #ifdef USE_X_TOOLKIT
300 if ((f->output_data.x->edit_widget
301 && XtWindow (f->output_data.x->edit_widget) == wdesc)
302 /* A tooltip frame? */
303 || (!f->output_data.x->edit_widget
304 && FRAME_X_WINDOW (f) == wdesc)
305 || f->output_data.x->icon_desc == wdesc)
306 return f;
307 #else /* not USE_X_TOOLKIT */
308 #ifdef USE_GTK
309 if (f->output_data.x->edit_widget)
311 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
312 struct x_output *x = f->output_data.x;
313 if (gwdesc != 0 && gwdesc == x->edit_widget)
314 return f;
316 #endif /* USE_GTK */
317 if (FRAME_X_WINDOW (f) == wdesc
318 || f->output_data.x->icon_desc == wdesc)
319 return f;
320 #endif /* not USE_X_TOOLKIT */
322 return 0;
325 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
326 /* Like x_window_to_frame but also compares the window with the widget's
327 windows. */
329 struct frame *
330 x_any_window_to_frame (dpyinfo, wdesc)
331 struct x_display_info *dpyinfo;
332 int wdesc;
334 Lisp_Object tail, frame;
335 struct frame *f, *found;
336 struct x_output *x;
338 if (wdesc == None) return NULL;
340 found = NULL;
341 for (tail = Vframe_list; GC_CONSP (tail) && !found; tail = XCDR (tail))
343 frame = XCAR (tail);
344 if (!GC_FRAMEP (frame))
345 continue;
347 f = XFRAME (frame);
348 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
350 /* This frame matches if the window is any of its widgets. */
351 x = f->output_data.x;
352 if (x->hourglass_window == wdesc)
353 found = f;
354 else if (x->widget)
356 #ifdef USE_GTK
357 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
358 if (gwdesc != 0
359 && (gwdesc == x->widget
360 || gwdesc == x->edit_widget
361 || gwdesc == x->vbox_widget
362 || gwdesc == x->menubar_widget))
363 found = f;
364 #else
365 if (wdesc == XtWindow (x->widget)
366 || wdesc == XtWindow (x->column_widget)
367 || wdesc == XtWindow (x->edit_widget))
368 found = f;
369 /* Match if the window is this frame's menubar. */
370 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
371 found = f;
372 #endif
374 else if (FRAME_X_WINDOW (f) == wdesc)
375 /* A tooltip frame. */
376 found = f;
380 return found;
383 /* Likewise, but exclude the menu bar widget. */
385 struct frame *
386 x_non_menubar_window_to_frame (dpyinfo, wdesc)
387 struct x_display_info *dpyinfo;
388 int wdesc;
390 Lisp_Object tail, frame;
391 struct frame *f;
392 struct x_output *x;
394 if (wdesc == None) return 0;
396 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
398 frame = XCAR (tail);
399 if (!GC_FRAMEP (frame))
400 continue;
401 f = XFRAME (frame);
402 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
403 continue;
404 x = f->output_data.x;
405 /* This frame matches if the window is any of its widgets. */
406 if (x->hourglass_window == wdesc)
407 return f;
408 else if (x->widget)
410 #ifdef USE_GTK
411 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
412 if (gwdesc != 0
413 && (gwdesc == x->widget
414 || gwdesc == x->edit_widget
415 || gwdesc == x->vbox_widget))
416 return f;
417 #else
418 if (wdesc == XtWindow (x->widget)
419 || wdesc == XtWindow (x->column_widget)
420 || wdesc == XtWindow (x->edit_widget))
421 return f;
422 #endif
424 else if (FRAME_X_WINDOW (f) == wdesc)
425 /* A tooltip frame. */
426 return f;
428 return 0;
431 /* Likewise, but consider only the menu bar widget. */
433 struct frame *
434 x_menubar_window_to_frame (dpyinfo, wdesc)
435 struct x_display_info *dpyinfo;
436 int wdesc;
438 Lisp_Object tail, frame;
439 struct frame *f;
440 struct x_output *x;
442 if (wdesc == None) return 0;
444 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
446 frame = XCAR (tail);
447 if (!GC_FRAMEP (frame))
448 continue;
449 f = XFRAME (frame);
450 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
451 continue;
452 x = f->output_data.x;
453 /* Match if the window is this frame's menubar. */
454 #ifdef USE_GTK
455 if (x->menubar_widget)
457 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
458 int found = 0;
460 BLOCK_INPUT;
461 if (gwdesc != 0
462 && (gwdesc == x->menubar_widget
463 || gtk_widget_get_parent (gwdesc) == x->menubar_widget))
464 found = 1;
465 UNBLOCK_INPUT;
466 if (found) return f;
468 #else
469 if (x->menubar_widget
470 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
471 return f;
472 #endif
474 return 0;
477 /* Return the frame whose principal (outermost) window is WDESC.
478 If WDESC is some other (smaller) window, we return 0. */
480 struct frame *
481 x_top_window_to_frame (dpyinfo, wdesc)
482 struct x_display_info *dpyinfo;
483 int wdesc;
485 Lisp_Object tail, frame;
486 struct frame *f;
487 struct x_output *x;
489 if (wdesc == None) return 0;
491 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
493 frame = XCAR (tail);
494 if (!GC_FRAMEP (frame))
495 continue;
496 f = XFRAME (frame);
497 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
498 continue;
499 x = f->output_data.x;
501 if (x->widget)
503 /* This frame matches if the window is its topmost widget. */
504 #ifdef USE_GTK
505 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
506 if (gwdesc == x->widget)
507 return f;
508 #else
509 if (wdesc == XtWindow (x->widget))
510 return f;
511 #if 0 /* I don't know why it did this,
512 but it seems logically wrong,
513 and it causes trouble for MapNotify events. */
514 /* Match if the window is this frame's menubar. */
515 if (x->menubar_widget
516 && wdesc == XtWindow (x->menubar_widget))
517 return f;
518 #endif
519 #endif
521 else if (FRAME_X_WINDOW (f) == wdesc)
522 /* Tooltip frame. */
523 return f;
525 return 0;
527 #endif /* USE_X_TOOLKIT || USE_GTK */
531 static Lisp_Object unwind_create_frame P_ ((Lisp_Object));
532 static Lisp_Object unwind_create_tip_frame P_ ((Lisp_Object));
534 void x_set_foreground_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
535 static void x_set_wait_for_wm P_ ((struct frame *, Lisp_Object, Lisp_Object));
536 void x_set_background_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
537 void x_set_mouse_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
538 void x_set_cursor_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
539 void x_set_border_color P_ ((struct frame *, Lisp_Object, Lisp_Object));
540 void x_set_cursor_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
541 void x_set_icon_type P_ ((struct frame *, Lisp_Object, Lisp_Object));
542 void x_set_icon_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
543 void x_explicitly_set_name P_ ((struct frame *, Lisp_Object, Lisp_Object));
544 void x_set_menu_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
545 void x_set_title P_ ((struct frame *, Lisp_Object, Lisp_Object));
546 void x_set_tool_bar_lines P_ ((struct frame *, Lisp_Object, Lisp_Object));
547 void x_set_scroll_bar_foreground P_ ((struct frame *, Lisp_Object,
548 Lisp_Object));
549 void x_set_scroll_bar_background P_ ((struct frame *, Lisp_Object,
550 Lisp_Object));
551 static Lisp_Object x_default_scroll_bar_color_parameter P_ ((struct frame *,
552 Lisp_Object,
553 Lisp_Object,
554 char *, char *,
555 int));
558 /* Store the screen positions of frame F into XPTR and YPTR.
559 These are the positions of the containing window manager window,
560 not Emacs's own window. */
562 void
563 x_real_positions (f, xptr, yptr)
564 FRAME_PTR f;
565 int *xptr, *yptr;
567 int win_x, win_y, outer_x, outer_y;
568 int real_x = 0, real_y = 0;
569 int had_errors = 0;
570 Window win = f->output_data.x->parent_desc;
572 int count;
574 BLOCK_INPUT;
576 count = x_catch_errors (FRAME_X_DISPLAY (f));
578 if (win == FRAME_X_DISPLAY_INFO (f)->root_window)
579 win = FRAME_OUTER_WINDOW (f);
581 /* This loop traverses up the containment tree until we hit the root
582 window. Window managers may intersect many windows between our window
583 and the root window. The window we find just before the root window
584 should be the outer WM window. */
585 for (;;)
587 Window wm_window, rootw;
588 Window *tmp_children;
589 unsigned int tmp_nchildren;
590 int success;
592 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
593 &wm_window, &tmp_children, &tmp_nchildren);
595 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
597 /* Don't free tmp_children if XQueryTree failed. */
598 if (! success)
599 break;
601 XFree ((char *) tmp_children);
603 if (wm_window == rootw || had_errors)
604 break;
606 win = wm_window;
609 if (! had_errors)
611 int ign;
612 Window child, rootw;
614 /* Get the real coordinates for the WM window upper left corner */
615 XGetGeometry (FRAME_X_DISPLAY (f), win,
616 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
618 /* Translate real coordinates to coordinates relative to our
619 window. For our window, the upper left corner is 0, 0.
620 Since the upper left corner of the WM window is outside
621 our window, win_x and win_y will be negative:
623 ------------------ ---> x
624 | title |
625 | ----------------- v y
626 | | our window
628 XTranslateCoordinates (FRAME_X_DISPLAY (f),
630 /* From-window, to-window. */
631 FRAME_X_DISPLAY_INFO (f)->root_window,
632 FRAME_X_WINDOW (f),
634 /* From-position, to-position. */
635 real_x, real_y, &win_x, &win_y,
637 /* Child of win. */
638 &child);
640 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
642 outer_x = win_x;
643 outer_y = win_y;
645 else
647 XTranslateCoordinates (FRAME_X_DISPLAY (f),
649 /* From-window, to-window. */
650 FRAME_X_DISPLAY_INFO (f)->root_window,
651 FRAME_OUTER_WINDOW (f),
653 /* From-position, to-position. */
654 real_x, real_y, &outer_x, &outer_y,
656 /* Child of win. */
657 &child);
660 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
663 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
665 UNBLOCK_INPUT;
667 if (had_errors) return;
669 f->x_pixels_diff = -win_x;
670 f->y_pixels_diff = -win_y;
672 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
673 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
675 *xptr = real_x;
676 *yptr = real_y;
682 /* Gamma-correct COLOR on frame F. */
684 void
685 gamma_correct (f, color)
686 struct frame *f;
687 XColor *color;
689 if (f->gamma)
691 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
692 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
693 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
698 /* Decide if color named COLOR_NAME is valid for use on frame F. If
699 so, return the RGB values in COLOR. If ALLOC_P is non-zero,
700 allocate the color. Value is zero if COLOR_NAME is invalid, or
701 no color could be allocated. */
704 x_defined_color (f, color_name, color, alloc_p)
705 struct frame *f;
706 char *color_name;
707 XColor *color;
708 int alloc_p;
710 int success_p;
711 Display *dpy = FRAME_X_DISPLAY (f);
712 Colormap cmap = FRAME_X_COLORMAP (f);
714 BLOCK_INPUT;
715 success_p = XParseColor (dpy, cmap, color_name, color);
716 if (success_p && alloc_p)
717 success_p = x_alloc_nearest_color (f, cmap, color);
718 UNBLOCK_INPUT;
720 return success_p;
724 /* Return the pixel color value for color COLOR_NAME on frame F. If F
725 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
726 Signal an error if color can't be allocated. */
729 x_decode_color (f, color_name, mono_color)
730 FRAME_PTR f;
731 Lisp_Object color_name;
732 int mono_color;
734 XColor cdef;
736 CHECK_STRING (color_name);
738 #if 0 /* Don't do this. It's wrong when we're not using the default
739 colormap, it makes freeing difficult, and it's probably not
740 an important optimization. */
741 if (strcmp (SDATA (color_name), "black") == 0)
742 return BLACK_PIX_DEFAULT (f);
743 else if (strcmp (SDATA (color_name), "white") == 0)
744 return WHITE_PIX_DEFAULT (f);
745 #endif
747 /* Return MONO_COLOR for monochrome frames. */
748 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
749 return mono_color;
751 /* x_defined_color is responsible for coping with failures
752 by looking for a near-miss. */
753 if (x_defined_color (f, SDATA (color_name), &cdef, 1))
754 return cdef.pixel;
756 Fsignal (Qerror, Fcons (build_string ("Undefined color"),
757 Fcons (color_name, Qnil)));
758 return 0;
763 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
764 the previous value of that parameter, NEW_VALUE is the new value.
765 See also the comment of wait_for_wm in struct x_output. */
767 static void
768 x_set_wait_for_wm (f, new_value, old_value)
769 struct frame *f;
770 Lisp_Object new_value, old_value;
772 f->output_data.x->wait_for_wm = !NILP (new_value);
775 #ifdef USE_GTK
777 /* Set icon from FILE for frame F. By using GTK functions the icon
778 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
781 xg_set_icon (f, file)
782 FRAME_PTR f;
783 Lisp_Object file;
785 struct gcpro gcpro1;
786 int result = 0;
787 Lisp_Object found;
789 GCPRO1 (found);
791 found = x_find_image_file (file);
793 if (! NILP (found))
795 GdkPixbuf *pixbuf;
796 GError *err = NULL;
797 char *filename;
799 filename = SDATA (found);
800 BLOCK_INPUT;
802 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
804 if (pixbuf)
806 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
807 pixbuf);
808 g_object_unref (pixbuf);
810 result = 1;
812 else
813 g_error_free (err);
815 UNBLOCK_INPUT;
818 UNGCPRO;
819 return result;
823 xg_set_icon_from_xpm_data (f, data)
824 FRAME_PTR f;
825 char **data;
827 int result = 0;
828 GError *err = NULL;
829 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
831 if (!pixbuf)
833 g_error_free (err);
834 return 0;
837 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
838 pixbuf);
839 g_object_unref (pixbuf);
840 return 1;
842 #endif /* USE_GTK */
845 /* Functions called only from `x_set_frame_param'
846 to set individual parameters.
848 If FRAME_X_WINDOW (f) is 0,
849 the frame is being created and its X-window does not exist yet.
850 In that case, just record the parameter's new value
851 in the standard place; do not attempt to change the window. */
853 void
854 x_set_foreground_color (f, arg, oldval)
855 struct frame *f;
856 Lisp_Object arg, oldval;
858 struct x_output *x = f->output_data.x;
859 unsigned long fg, old_fg;
861 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
862 old_fg = x->foreground_pixel;
863 x->foreground_pixel = fg;
865 if (FRAME_X_WINDOW (f) != 0)
867 Display *dpy = FRAME_X_DISPLAY (f);
869 BLOCK_INPUT;
870 XSetForeground (dpy, x->normal_gc, fg);
871 XSetBackground (dpy, x->reverse_gc, fg);
873 if (x->cursor_pixel == old_fg)
875 unload_color (f, x->cursor_pixel);
876 x->cursor_pixel = x_copy_color (f, fg);
877 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
880 UNBLOCK_INPUT;
882 update_face_from_frame_parameter (f, Qforeground_color, arg);
884 if (FRAME_VISIBLE_P (f))
885 redraw_frame (f);
888 unload_color (f, old_fg);
891 void
892 x_set_background_color (f, arg, oldval)
893 struct frame *f;
894 Lisp_Object arg, oldval;
896 struct x_output *x = f->output_data.x;
897 unsigned long bg;
899 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
900 unload_color (f, x->background_pixel);
901 x->background_pixel = bg;
903 if (FRAME_X_WINDOW (f) != 0)
905 Display *dpy = FRAME_X_DISPLAY (f);
907 BLOCK_INPUT;
908 XSetBackground (dpy, x->normal_gc, bg);
909 XSetForeground (dpy, x->reverse_gc, bg);
910 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
911 XSetForeground (dpy, x->cursor_gc, bg);
913 #ifdef USE_GTK
914 xg_set_background_color (f, bg);
915 #endif
917 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
918 toolkit scroll bars. */
920 Lisp_Object bar;
921 for (bar = FRAME_SCROLL_BARS (f);
922 !NILP (bar);
923 bar = XSCROLL_BAR (bar)->next)
925 Window window = SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar));
926 XSetWindowBackground (dpy, window, bg);
929 #endif /* USE_TOOLKIT_SCROLL_BARS */
931 UNBLOCK_INPUT;
932 update_face_from_frame_parameter (f, Qbackground_color, arg);
934 if (FRAME_VISIBLE_P (f))
935 redraw_frame (f);
939 void
940 x_set_mouse_color (f, arg, oldval)
941 struct frame *f;
942 Lisp_Object arg, oldval;
944 struct x_output *x = f->output_data.x;
945 Display *dpy = FRAME_X_DISPLAY (f);
946 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
947 Cursor hourglass_cursor, horizontal_drag_cursor;
948 int count;
949 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
950 unsigned long mask_color = x->background_pixel;
952 /* Don't let pointers be invisible. */
953 if (mask_color == pixel)
955 x_free_colors (f, &pixel, 1);
956 pixel = x_copy_color (f, x->foreground_pixel);
959 unload_color (f, x->mouse_pixel);
960 x->mouse_pixel = pixel;
962 BLOCK_INPUT;
964 /* It's not okay to crash if the user selects a screwy cursor. */
965 count = x_catch_errors (dpy);
967 if (!NILP (Vx_pointer_shape))
969 CHECK_NUMBER (Vx_pointer_shape);
970 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
972 else
973 cursor = XCreateFontCursor (dpy, XC_xterm);
974 x_check_errors (dpy, "bad text pointer cursor: %s");
976 if (!NILP (Vx_nontext_pointer_shape))
978 CHECK_NUMBER (Vx_nontext_pointer_shape);
979 nontext_cursor
980 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
982 else
983 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
984 x_check_errors (dpy, "bad nontext pointer cursor: %s");
986 if (!NILP (Vx_hourglass_pointer_shape))
988 CHECK_NUMBER (Vx_hourglass_pointer_shape);
989 hourglass_cursor
990 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
992 else
993 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
994 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
996 if (!NILP (Vx_mode_pointer_shape))
998 CHECK_NUMBER (Vx_mode_pointer_shape);
999 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
1001 else
1002 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
1003 x_check_errors (dpy, "bad modeline pointer cursor: %s");
1005 if (!NILP (Vx_sensitive_text_pointer_shape))
1007 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1008 hand_cursor
1009 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
1011 else
1012 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
1014 if (!NILP (Vx_window_horizontal_drag_shape))
1016 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1017 horizontal_drag_cursor
1018 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
1020 else
1021 horizontal_drag_cursor
1022 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
1024 /* Check and report errors with the above calls. */
1025 x_check_errors (dpy, "can't set cursor shape: %s");
1026 x_uncatch_errors (dpy, count);
1029 XColor fore_color, back_color;
1031 fore_color.pixel = x->mouse_pixel;
1032 x_query_color (f, &fore_color);
1033 back_color.pixel = mask_color;
1034 x_query_color (f, &back_color);
1036 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
1037 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
1038 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
1039 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
1040 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
1041 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
1044 if (FRAME_X_WINDOW (f) != 0)
1045 XDefineCursor (dpy, FRAME_X_WINDOW (f), cursor);
1047 if (cursor != x->text_cursor
1048 && x->text_cursor != 0)
1049 XFreeCursor (dpy, x->text_cursor);
1050 x->text_cursor = cursor;
1052 if (nontext_cursor != x->nontext_cursor
1053 && x->nontext_cursor != 0)
1054 XFreeCursor (dpy, x->nontext_cursor);
1055 x->nontext_cursor = nontext_cursor;
1057 if (hourglass_cursor != x->hourglass_cursor
1058 && x->hourglass_cursor != 0)
1059 XFreeCursor (dpy, x->hourglass_cursor);
1060 x->hourglass_cursor = hourglass_cursor;
1062 if (mode_cursor != x->modeline_cursor
1063 && x->modeline_cursor != 0)
1064 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
1065 x->modeline_cursor = mode_cursor;
1067 if (hand_cursor != x->hand_cursor
1068 && x->hand_cursor != 0)
1069 XFreeCursor (dpy, x->hand_cursor);
1070 x->hand_cursor = hand_cursor;
1072 if (horizontal_drag_cursor != x->horizontal_drag_cursor
1073 && x->horizontal_drag_cursor != 0)
1074 XFreeCursor (dpy, x->horizontal_drag_cursor);
1075 x->horizontal_drag_cursor = horizontal_drag_cursor;
1077 XFlush (dpy);
1078 UNBLOCK_INPUT;
1080 update_face_from_frame_parameter (f, Qmouse_color, arg);
1083 void
1084 x_set_cursor_color (f, arg, oldval)
1085 struct frame *f;
1086 Lisp_Object arg, oldval;
1088 unsigned long fore_pixel, pixel;
1089 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
1090 struct x_output *x = f->output_data.x;
1092 if (!NILP (Vx_cursor_fore_pixel))
1094 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1095 WHITE_PIX_DEFAULT (f));
1096 fore_pixel_allocated_p = 1;
1098 else
1099 fore_pixel = x->background_pixel;
1101 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1102 pixel_allocated_p = 1;
1104 /* Make sure that the cursor color differs from the background color. */
1105 if (pixel == x->background_pixel)
1107 if (pixel_allocated_p)
1109 x_free_colors (f, &pixel, 1);
1110 pixel_allocated_p = 0;
1113 pixel = x->mouse_pixel;
1114 if (pixel == fore_pixel)
1116 if (fore_pixel_allocated_p)
1118 x_free_colors (f, &fore_pixel, 1);
1119 fore_pixel_allocated_p = 0;
1121 fore_pixel = x->background_pixel;
1125 unload_color (f, x->cursor_foreground_pixel);
1126 if (!fore_pixel_allocated_p)
1127 fore_pixel = x_copy_color (f, fore_pixel);
1128 x->cursor_foreground_pixel = fore_pixel;
1130 unload_color (f, x->cursor_pixel);
1131 if (!pixel_allocated_p)
1132 pixel = x_copy_color (f, pixel);
1133 x->cursor_pixel = pixel;
1135 if (FRAME_X_WINDOW (f) != 0)
1137 BLOCK_INPUT;
1138 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1139 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
1140 UNBLOCK_INPUT;
1142 if (FRAME_VISIBLE_P (f))
1144 x_update_cursor (f, 0);
1145 x_update_cursor (f, 1);
1149 update_face_from_frame_parameter (f, Qcursor_color, arg);
1152 /* Set the border-color of frame F to pixel value PIX.
1153 Note that this does not fully take effect if done before
1154 F has an x-window. */
1156 void
1157 x_set_border_pixel (f, pix)
1158 struct frame *f;
1159 int pix;
1161 unload_color (f, f->output_data.x->border_pixel);
1162 f->output_data.x->border_pixel = pix;
1164 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
1166 BLOCK_INPUT;
1167 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1168 (unsigned long)pix);
1169 UNBLOCK_INPUT;
1171 if (FRAME_VISIBLE_P (f))
1172 redraw_frame (f);
1176 /* Set the border-color of frame F to value described by ARG.
1177 ARG can be a string naming a color.
1178 The border-color is used for the border that is drawn by the X server.
1179 Note that this does not fully take effect if done before
1180 F has an x-window; it must be redone when the window is created.
1182 Note: this is done in two routines because of the way X10 works.
1184 Note: under X11, this is normally the province of the window manager,
1185 and so emacs' border colors may be overridden. */
1187 void
1188 x_set_border_color (f, arg, oldval)
1189 struct frame *f;
1190 Lisp_Object arg, oldval;
1192 int pix;
1194 CHECK_STRING (arg);
1195 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1196 x_set_border_pixel (f, pix);
1197 update_face_from_frame_parameter (f, Qborder_color, arg);
1201 void
1202 x_set_cursor_type (f, arg, oldval)
1203 FRAME_PTR f;
1204 Lisp_Object arg, oldval;
1206 set_frame_cursor_types (f, arg);
1208 /* Make sure the cursor gets redrawn. */
1209 cursor_type_changed = 1;
1212 void
1213 x_set_icon_type (f, arg, oldval)
1214 struct frame *f;
1215 Lisp_Object arg, oldval;
1217 int result;
1219 if (STRINGP (arg))
1221 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1222 return;
1224 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1225 return;
1227 BLOCK_INPUT;
1228 if (NILP (arg))
1229 result = x_text_icon (f,
1230 (char *) SDATA ((!NILP (f->icon_name)
1231 ? f->icon_name
1232 : f->name)));
1233 else
1234 result = x_bitmap_icon (f, arg);
1236 if (result)
1238 UNBLOCK_INPUT;
1239 error ("No icon window available");
1242 XFlush (FRAME_X_DISPLAY (f));
1243 UNBLOCK_INPUT;
1246 void
1247 x_set_icon_name (f, arg, oldval)
1248 struct frame *f;
1249 Lisp_Object arg, oldval;
1251 int result;
1253 if (STRINGP (arg))
1255 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1256 return;
1258 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1259 return;
1261 f->icon_name = arg;
1263 if (f->output_data.x->icon_bitmap != 0)
1264 return;
1266 BLOCK_INPUT;
1268 result = x_text_icon (f,
1269 (char *) SDATA ((!NILP (f->icon_name)
1270 ? f->icon_name
1271 : !NILP (f->title)
1272 ? f->title
1273 : f->name)));
1275 if (result)
1277 UNBLOCK_INPUT;
1278 error ("No icon window available");
1281 XFlush (FRAME_X_DISPLAY (f));
1282 UNBLOCK_INPUT;
1286 void
1287 x_set_menu_bar_lines (f, value, oldval)
1288 struct frame *f;
1289 Lisp_Object value, oldval;
1291 int nlines;
1292 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1293 int olines = FRAME_MENU_BAR_LINES (f);
1294 #endif
1296 /* Right now, menu bars don't work properly in minibuf-only frames;
1297 most of the commands try to apply themselves to the minibuffer
1298 frame itself, and get an error because you can't switch buffers
1299 in or split the minibuffer window. */
1300 if (FRAME_MINIBUF_ONLY_P (f))
1301 return;
1303 if (INTEGERP (value))
1304 nlines = XINT (value);
1305 else
1306 nlines = 0;
1308 /* Make sure we redisplay all windows in this frame. */
1309 windows_or_buffers_changed++;
1311 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1312 FRAME_MENU_BAR_LINES (f) = 0;
1313 if (nlines)
1315 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1316 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1317 /* Make sure next redisplay shows the menu bar. */
1318 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
1320 else
1322 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1323 free_frame_menubar (f);
1324 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1325 if (FRAME_X_P (f))
1326 f->output_data.x->menubar_widget = 0;
1328 #else /* not USE_X_TOOLKIT && not USE_GTK */
1329 FRAME_MENU_BAR_LINES (f) = nlines;
1330 change_window_heights (f->root_window, nlines - olines);
1331 #endif /* not USE_X_TOOLKIT */
1332 adjust_glyphs (f);
1336 /* Set the number of lines used for the tool bar of frame F to VALUE.
1337 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1338 is the old number of tool bar lines. This function changes the
1339 height of all windows on frame F to match the new tool bar height.
1340 The frame's height doesn't change. */
1342 void
1343 x_set_tool_bar_lines (f, value, oldval)
1344 struct frame *f;
1345 Lisp_Object value, oldval;
1347 int delta, nlines, root_height;
1348 Lisp_Object root_window;
1350 /* Treat tool bars like menu bars. */
1351 if (FRAME_MINIBUF_ONLY_P (f))
1352 return;
1354 /* Use VALUE only if an integer >= 0. */
1355 if (INTEGERP (value) && XINT (value) >= 0)
1356 nlines = XFASTINT (value);
1357 else
1358 nlines = 0;
1360 #ifdef USE_GTK
1361 FRAME_TOOL_BAR_LINES (f) = 0;
1362 if (nlines)
1364 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1365 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1366 /* Make sure next redisplay shows the tool bar. */
1367 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
1368 update_frame_tool_bar (f);
1370 else
1372 if (FRAME_EXTERNAL_TOOL_BAR (f))
1373 free_frame_tool_bar (f);
1374 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1377 return;
1378 #endif
1380 /* Make sure we redisplay all windows in this frame. */
1381 ++windows_or_buffers_changed;
1383 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1385 /* Don't resize the tool-bar to more than we have room for. */
1386 root_window = FRAME_ROOT_WINDOW (f);
1387 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1388 if (root_height - delta < 1)
1390 delta = root_height - 1;
1391 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1394 FRAME_TOOL_BAR_LINES (f) = nlines;
1395 change_window_heights (root_window, delta);
1396 adjust_glyphs (f);
1398 /* We also have to make sure that the internal border at the top of
1399 the frame, below the menu bar or tool bar, is redrawn when the
1400 tool bar disappears. This is so because the internal border is
1401 below the tool bar if one is displayed, but is below the menu bar
1402 if there isn't a tool bar. The tool bar draws into the area
1403 below the menu bar. */
1404 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1406 updating_frame = f;
1407 clear_frame ();
1408 clear_current_matrices (f);
1409 updating_frame = NULL;
1412 /* If the tool bar gets smaller, the internal border below it
1413 has to be cleared. It was formerly part of the display
1414 of the larger tool bar, and updating windows won't clear it. */
1415 if (delta < 0)
1417 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1418 int width = FRAME_PIXEL_WIDTH (f);
1419 int y = nlines * FRAME_LINE_HEIGHT (f);
1421 /* height can be zero here. */
1422 if (height > 0 && width > 0)
1424 BLOCK_INPUT;
1425 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1426 0, y, width, height, False);
1427 UNBLOCK_INPUT;
1430 if (WINDOWP (f->tool_bar_window))
1431 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1436 /* Set the foreground color for scroll bars on frame F to VALUE.
1437 VALUE should be a string, a color name. If it isn't a string or
1438 isn't a valid color name, do nothing. OLDVAL is the old value of
1439 the frame parameter. */
1441 void
1442 x_set_scroll_bar_foreground (f, value, oldval)
1443 struct frame *f;
1444 Lisp_Object value, oldval;
1446 unsigned long pixel;
1448 if (STRINGP (value))
1449 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1450 else
1451 pixel = -1;
1453 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1454 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1456 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1457 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1459 /* Remove all scroll bars because they have wrong colors. */
1460 if (condemn_scroll_bars_hook)
1461 (*condemn_scroll_bars_hook) (f);
1462 if (judge_scroll_bars_hook)
1463 (*judge_scroll_bars_hook) (f);
1465 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1466 redraw_frame (f);
1471 /* Set the background color for scroll bars on frame F to VALUE VALUE
1472 should be a string, a color name. If it isn't a string or isn't a
1473 valid color name, do nothing. OLDVAL is the old value of the frame
1474 parameter. */
1476 void
1477 x_set_scroll_bar_background (f, value, oldval)
1478 struct frame *f;
1479 Lisp_Object value, oldval;
1481 unsigned long pixel;
1483 if (STRINGP (value))
1484 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1485 else
1486 pixel = -1;
1488 if (f->output_data.x->scroll_bar_background_pixel != -1)
1489 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1491 #ifdef USE_TOOLKIT_SCROLL_BARS
1492 /* Scrollbar shadow colors. */
1493 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1495 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1496 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1498 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1500 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1501 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1503 #endif /* USE_TOOLKIT_SCROLL_BARS */
1505 f->output_data.x->scroll_bar_background_pixel = pixel;
1506 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1508 /* Remove all scroll bars because they have wrong colors. */
1509 if (condemn_scroll_bars_hook)
1510 (*condemn_scroll_bars_hook) (f);
1511 if (judge_scroll_bars_hook)
1512 (*judge_scroll_bars_hook) (f);
1514 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1515 redraw_frame (f);
1520 /* Encode Lisp string STRING as a text in a format appropriate for
1521 XICCC (X Inter Client Communication Conventions).
1523 If STRING contains only ASCII characters, do no conversion and
1524 return the string data of STRING. Otherwise, encode the text by
1525 CODING_SYSTEM, and return a newly allocated memory area which
1526 should be freed by `xfree' by a caller.
1528 SELECTIONP non-zero means the string is being encoded for an X
1529 selection, so it is safe to run pre-write conversions (which
1530 may run Lisp code).
1532 Store the byte length of resulting text in *TEXT_BYTES.
1534 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1535 which means that the `encoding' of the result can be `STRING'.
1536 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1537 the result should be `COMPOUND_TEXT'. */
1539 static unsigned char *
1540 x_encode_text (string, coding_system, selectionp, text_bytes, stringp, freep)
1541 Lisp_Object string, coding_system;
1542 int *text_bytes, *stringp;
1543 int selectionp;
1544 int *freep;
1546 unsigned char *str = SDATA (string);
1547 int chars = SCHARS (string);
1548 int bytes = SBYTES (string);
1549 int charset_info;
1550 int bufsize;
1551 unsigned char *buf;
1552 struct coding_system coding;
1553 extern Lisp_Object Qcompound_text_with_extensions;
1555 charset_info = find_charset_in_text (str, chars, bytes, NULL, Qnil);
1556 if (charset_info == 0)
1558 /* No multibyte character in OBJ. We need not encode it. */
1559 *text_bytes = bytes;
1560 *stringp = 1;
1561 *freep = 0;
1562 return str;
1565 setup_coding_system (coding_system, &coding);
1566 if (selectionp
1567 && SYMBOLP (coding.pre_write_conversion)
1568 && !NILP (Ffboundp (coding.pre_write_conversion)))
1570 string = run_pre_post_conversion_on_str (string, &coding, 1);
1571 str = SDATA (string);
1572 chars = SCHARS (string);
1573 bytes = SBYTES (string);
1575 coding.src_multibyte = 1;
1576 coding.dst_multibyte = 0;
1577 coding.mode |= CODING_MODE_LAST_BLOCK;
1578 if (coding.type == coding_type_iso2022)
1579 coding.flags |= CODING_FLAG_ISO_SAFE;
1580 /* We suppress producing escape sequences for composition. */
1581 coding.composing = COMPOSITION_DISABLED;
1582 bufsize = encoding_buffer_size (&coding, bytes);
1583 buf = (unsigned char *) xmalloc (bufsize);
1584 encode_coding (&coding, str, buf, bytes, bufsize);
1585 *text_bytes = coding.produced;
1586 *stringp = (charset_info == 1
1587 || (!EQ (coding_system, Qcompound_text)
1588 && !EQ (coding_system, Qcompound_text_with_extensions)));
1589 *freep = 1;
1590 return buf;
1594 /* Set the WM name to NAME for frame F. Also set the icon name.
1595 If the frame already has an icon name, use that, otherwise set the
1596 icon name to NAME. */
1598 static void
1599 x_set_name_internal (f, name)
1600 FRAME_PTR f;
1601 Lisp_Object name;
1603 if (FRAME_X_WINDOW (f))
1605 BLOCK_INPUT;
1606 #ifdef HAVE_X11R4
1608 XTextProperty text, icon;
1609 int bytes, stringp;
1610 int do_free_icon_value = 0, do_free_text_value = 0;
1611 Lisp_Object coding_system;
1613 coding_system = Qcompound_text;
1614 /* Note: Encoding strategy
1616 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1617 text.encoding. But, there are non-internationalized window
1618 managers which don't support that encoding. So, if NAME
1619 contains only ASCII and 8859-1 characters, encode it by
1620 iso-latin-1, and use "STRING" in text.encoding hoping that
1621 such window managers at least analyze this format correctly,
1622 i.e. treat 8-bit bytes as 8859-1 characters.
1624 We may also be able to use "UTF8_STRING" in text.encoding
1625 in the future which can encode all Unicode characters.
1626 But, for the moment, there's no way to know that the
1627 current window manager supports it or not. */
1628 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1629 &do_free_text_value);
1630 text.encoding = (stringp ? XA_STRING
1631 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1632 text.format = 8;
1633 text.nitems = bytes;
1635 if (NILP (f->icon_name))
1637 icon = text;
1639 else
1641 /* See the above comment "Note: Encoding strategy". */
1642 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1643 &bytes, &stringp, &do_free_icon_value);
1644 icon.encoding = (stringp ? XA_STRING
1645 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1646 icon.format = 8;
1647 icon.nitems = bytes;
1650 #ifdef USE_GTK
1651 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1652 SDATA (ENCODE_UTF_8 (name)));
1653 #else /* not USE_GTK */
1654 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1655 #endif /* not USE_GTK */
1657 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1659 if (do_free_icon_value)
1660 xfree (icon.value);
1661 if (do_free_text_value)
1662 xfree (text.value);
1664 #else /* not HAVE_X11R4 */
1665 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1666 SDATA (name));
1667 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1668 SDATA (name));
1669 #endif /* not HAVE_X11R4 */
1670 UNBLOCK_INPUT;
1674 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1675 x_id_name.
1677 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1678 name; if NAME is a string, set F's name to NAME and set
1679 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1681 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1682 suggesting a new name, which lisp code should override; if
1683 F->explicit_name is set, ignore the new name; otherwise, set it. */
1685 void
1686 x_set_name (f, name, explicit)
1687 struct frame *f;
1688 Lisp_Object name;
1689 int explicit;
1691 /* Make sure that requests from lisp code override requests from
1692 Emacs redisplay code. */
1693 if (explicit)
1695 /* If we're switching from explicit to implicit, we had better
1696 update the mode lines and thereby update the title. */
1697 if (f->explicit_name && NILP (name))
1698 update_mode_lines = 1;
1700 f->explicit_name = ! NILP (name);
1702 else if (f->explicit_name)
1703 return;
1705 /* If NAME is nil, set the name to the x_id_name. */
1706 if (NILP (name))
1708 /* Check for no change needed in this very common case
1709 before we do any consing. */
1710 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1711 SDATA (f->name)))
1712 return;
1713 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1715 else
1716 CHECK_STRING (name);
1718 /* Don't change the name if it's already NAME. */
1719 if (! NILP (Fstring_equal (name, f->name)))
1720 return;
1722 f->name = name;
1724 /* For setting the frame title, the title parameter should override
1725 the name parameter. */
1726 if (! NILP (f->title))
1727 name = f->title;
1729 x_set_name_internal (f, name);
1732 /* This function should be called when the user's lisp code has
1733 specified a name for the frame; the name will override any set by the
1734 redisplay code. */
1735 void
1736 x_explicitly_set_name (f, arg, oldval)
1737 FRAME_PTR f;
1738 Lisp_Object arg, oldval;
1740 x_set_name (f, arg, 1);
1743 /* This function should be called by Emacs redisplay code to set the
1744 name; names set this way will never override names set by the user's
1745 lisp code. */
1746 void
1747 x_implicitly_set_name (f, arg, oldval)
1748 FRAME_PTR f;
1749 Lisp_Object arg, oldval;
1751 x_set_name (f, arg, 0);
1754 /* Change the title of frame F to NAME.
1755 If NAME is nil, use the frame name as the title.
1757 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1758 name; if NAME is a string, set F's name to NAME and set
1759 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1761 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1762 suggesting a new name, which lisp code should override; if
1763 F->explicit_name is set, ignore the new name; otherwise, set it. */
1765 void
1766 x_set_title (f, name, old_name)
1767 struct frame *f;
1768 Lisp_Object name, old_name;
1770 /* Don't change the title if it's already NAME. */
1771 if (EQ (name, f->title))
1772 return;
1774 update_mode_lines = 1;
1776 f->title = name;
1778 if (NILP (name))
1779 name = f->name;
1780 else
1781 CHECK_STRING (name);
1783 x_set_name_internal (f, name);
1786 void
1787 x_set_scroll_bar_default_width (f)
1788 struct frame *f;
1790 int wid = FRAME_COLUMN_WIDTH (f);
1792 #ifdef USE_TOOLKIT_SCROLL_BARS
1793 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1794 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1795 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1796 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1797 #else
1798 /* Make the actual width at least 14 pixels and a multiple of a
1799 character width. */
1800 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1802 /* Use all of that space (aside from required margins) for the
1803 scroll bar. */
1804 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1805 #endif
1809 /* Record in frame F the specified or default value according to ALIST
1810 of the parameter named PROP (a Lisp symbol). If no value is
1811 specified for PROP, look for an X default for XPROP on the frame
1812 named NAME. If that is not found either, use the value DEFLT. */
1814 static Lisp_Object
1815 x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
1816 foreground_p)
1817 struct frame *f;
1818 Lisp_Object alist;
1819 Lisp_Object prop;
1820 char *xprop;
1821 char *xclass;
1822 int foreground_p;
1824 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1825 Lisp_Object tem;
1827 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1828 if (EQ (tem, Qunbound))
1830 #ifdef USE_TOOLKIT_SCROLL_BARS
1832 /* See if an X resource for the scroll bar color has been
1833 specified. */
1834 tem = display_x_get_resource (dpyinfo,
1835 build_string (foreground_p
1836 ? "foreground"
1837 : "background"),
1838 empty_string,
1839 build_string ("verticalScrollBar"),
1840 empty_string);
1841 if (!STRINGP (tem))
1843 /* If nothing has been specified, scroll bars will use a
1844 toolkit-dependent default. Because these defaults are
1845 difficult to get at without actually creating a scroll
1846 bar, use nil to indicate that no color has been
1847 specified. */
1848 tem = Qnil;
1851 #else /* not USE_TOOLKIT_SCROLL_BARS */
1853 tem = Qnil;
1855 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1858 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
1859 return tem;
1864 #if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
1866 Status
1867 XSetWMProtocols (dpy, w, protocols, count)
1868 Display *dpy;
1869 Window w;
1870 Atom *protocols;
1871 int count;
1873 Atom prop;
1874 prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
1875 if (prop == None) return False;
1876 XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
1877 (unsigned char *) protocols, count);
1878 return True;
1880 #endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
1882 #ifdef USE_X_TOOLKIT
1884 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1885 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1886 already be present because of the toolkit (Motif adds some of them,
1887 for example, but Xt doesn't). */
1889 static void
1890 hack_wm_protocols (f, widget)
1891 FRAME_PTR f;
1892 Widget widget;
1894 Display *dpy = XtDisplay (widget);
1895 Window w = XtWindow (widget);
1896 int need_delete = 1;
1897 int need_focus = 1;
1898 int need_save = 1;
1900 BLOCK_INPUT;
1902 Atom type;
1903 unsigned char *catoms;
1904 int format = 0;
1905 unsigned long nitems = 0;
1906 unsigned long bytes_after;
1908 if ((XGetWindowProperty (dpy, w,
1909 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1910 (long)0, (long)100, False, XA_ATOM,
1911 &type, &format, &nitems, &bytes_after,
1912 &catoms)
1913 == Success)
1914 && format == 32 && type == XA_ATOM)
1916 Atom *atoms = (Atom *) catoms;
1917 while (nitems > 0)
1919 nitems--;
1920 if (atoms[nitems]
1921 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1922 need_delete = 0;
1923 else if (atoms[nitems]
1924 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1925 need_focus = 0;
1926 else if (atoms[nitems]
1927 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1928 need_save = 0;
1931 if (catoms)
1932 XFree (catoms);
1935 Atom props [10];
1936 int count = 0;
1937 if (need_delete)
1938 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1939 if (need_focus)
1940 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1941 if (need_save)
1942 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1943 if (count)
1944 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1945 XA_ATOM, 32, PropModeAppend,
1946 (unsigned char *) props, count);
1948 UNBLOCK_INPUT;
1950 #endif
1954 /* Support routines for XIC (X Input Context). */
1956 #ifdef HAVE_X_I18N
1958 static XFontSet xic_create_xfontset P_ ((struct frame *, char *));
1959 static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
1962 /* Supported XIM styles, ordered by preference. */
1964 static XIMStyle supported_xim_styles[] =
1966 XIMPreeditPosition | XIMStatusArea,
1967 XIMPreeditPosition | XIMStatusNothing,
1968 XIMPreeditPosition | XIMStatusNone,
1969 XIMPreeditNothing | XIMStatusArea,
1970 XIMPreeditNothing | XIMStatusNothing,
1971 XIMPreeditNothing | XIMStatusNone,
1972 XIMPreeditNone | XIMStatusArea,
1973 XIMPreeditNone | XIMStatusNothing,
1974 XIMPreeditNone | XIMStatusNone,
1979 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1981 char xic_defaut_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1983 /* Create an Xt fontset spec from the name of a base font.
1984 If `motif' is True use the Motif syntax. */
1985 char *
1986 xic_create_fontsetname (base_fontname, motif)
1987 char *base_fontname;
1988 Bool motif;
1990 const char *sep = motif ? ";" : ",";
1991 char *fontsetname;
1993 /* Make a fontset name from the base font name. */
1994 if (xic_defaut_fontset == base_fontname)
1995 { /* There is no base font name, use the default. */
1996 int len = strlen (base_fontname) + 2;
1997 fontsetname = xmalloc (len);
1998 bzero (fontsetname, len);
1999 strcpy (fontsetname, base_fontname);
2001 else
2003 /* Make a fontset name from the base font name.
2004 The font set will be made of the following elements:
2005 - the base font.
2006 - the base font where the charset spec is replaced by -*-*.
2007 - the same but with the family also replaced with -*-*-. */
2008 char *p = base_fontname;
2009 int i;
2011 for (i = 0; *p; p++)
2012 if (*p == '-') i++;
2013 if (i != 14)
2014 { /* As the font name doesn't conform to XLFD, we can't
2015 modify it to generalize it to allcs and allfamilies.
2016 Use the specified font plus the default. */
2017 int len = strlen (base_fontname) + strlen (xic_defaut_fontset) + 3;
2018 fontsetname = xmalloc (len);
2019 bzero (fontsetname, len);
2020 strcpy (fontsetname, base_fontname);
2021 strcat (fontsetname, sep);
2022 strcat (fontsetname, xic_defaut_fontset);
2024 else
2026 int len;
2027 char *p1 = NULL, *p2 = NULL;
2028 char *font_allcs = NULL;
2029 char *font_allfamilies = NULL;
2030 char *font_all = NULL;
2031 char *allcs = "*-*-*-*-*-*-*";
2032 char *allfamilies = "-*-*-";
2033 char *all = "*-*-*-*-";
2035 for (i = 0, p = base_fontname; i < 8; p++)
2037 if (*p == '-')
2039 i++;
2040 if (i == 3)
2041 p1 = p + 1;
2042 else if (i == 7)
2043 p2 = p + 1;
2046 /* Build the font spec that matches all charsets. */
2047 len = p - base_fontname + strlen (allcs) + 1;
2048 font_allcs = (char *) alloca (len);
2049 bzero (font_allcs, len);
2050 bcopy (base_fontname, font_allcs, p - base_fontname);
2051 strcat (font_allcs, allcs);
2053 /* Build the font spec that matches all families. */
2054 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
2055 font_allfamilies = (char *) alloca (len);
2056 bzero (font_allfamilies, len);
2057 strcpy (font_allfamilies, allfamilies);
2058 bcopy (p1, font_allfamilies + strlen (allfamilies), p - p1);
2059 strcat (font_allfamilies, allcs);
2061 /* Build the font spec that matches all. */
2062 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
2063 font_all = (char *) alloca (len);
2064 bzero (font_all, len);
2065 strcpy (font_all, allfamilies);
2066 strcat (font_all, all);
2067 bcopy (p2, font_all + strlen (all) + strlen (allfamilies), p - p2);
2068 strcat (font_all, allcs);
2070 /* Build the actual font set name. */
2071 len = strlen (base_fontname) + strlen (font_allcs)
2072 + strlen (font_allfamilies) + strlen (font_all) + 5;
2073 fontsetname = xmalloc (len);
2074 bzero (fontsetname, len);
2075 strcpy (fontsetname, base_fontname);
2076 strcat (fontsetname, sep);
2077 strcat (fontsetname, font_allcs);
2078 strcat (fontsetname, sep);
2079 strcat (fontsetname, font_allfamilies);
2080 strcat (fontsetname, sep);
2081 strcat (fontsetname, font_all);
2084 if (motif)
2085 strcat (fontsetname, ":");
2086 return fontsetname;
2089 static XFontSet
2090 xic_create_xfontset (f, base_fontname)
2091 struct frame *f;
2092 char *base_fontname;
2094 XFontSet xfs = NULL;
2095 char **missing_list = NULL;
2096 int missing_count;
2097 char *def_string;
2098 Lisp_Object rest, frame;
2100 if (!base_fontname)
2101 base_fontname = xic_defaut_fontset;
2103 /* See if there is another frame already using same fontset. */
2104 FOR_EACH_FRAME (rest, frame)
2106 struct frame *cf = XFRAME (frame);
2107 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2108 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2109 && FRAME_XIC_BASE_FONTNAME (cf)
2110 && !strcmp (FRAME_XIC_BASE_FONTNAME (cf), base_fontname))
2112 xfs = FRAME_XIC_FONTSET (cf);
2113 break;
2117 if (!xfs)
2119 char *fontsetname = xic_create_fontsetname (base_fontname, False);
2121 /* New fontset. */
2122 xfs = XCreateFontSet (FRAME_X_DISPLAY (f),
2123 fontsetname, &missing_list,
2124 &missing_count, &def_string);
2125 if (missing_list)
2126 XFreeStringList (missing_list);
2127 xfree (fontsetname);
2130 if (FRAME_XIC_BASE_FONTNAME (f))
2131 xfree (FRAME_XIC_BASE_FONTNAME (f));
2132 FRAME_XIC_BASE_FONTNAME (f) = xstrdup (base_fontname);
2134 /* No need to free def_string. */
2135 return xfs;
2138 /* Free the X fontset of frame F if it is the last frame using it. */
2140 void
2141 xic_free_xfontset (f)
2142 struct frame *f;
2144 Lisp_Object rest, frame;
2145 int shared_p = 0;
2147 if (!FRAME_XIC_FONTSET (f))
2148 return;
2150 /* See if there is another frame sharing the same fontset. */
2151 FOR_EACH_FRAME (rest, frame)
2153 struct frame *cf = XFRAME (frame);
2154 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2155 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2156 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2158 shared_p = 1;
2159 break;
2163 if (!shared_p)
2164 /* The fontset is not used anymore. It is safe to free it. */
2165 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2167 if (FRAME_XIC_BASE_FONTNAME (f))
2168 xfree (FRAME_XIC_BASE_FONTNAME (f));
2169 FRAME_XIC_BASE_FONTNAME (f) = NULL;
2170 FRAME_XIC_FONTSET (f) = NULL;
2174 /* Value is the best input style, given user preferences USER (already
2175 checked to be supported by Emacs), and styles supported by the
2176 input method XIM. */
2178 static XIMStyle
2179 best_xim_style (user, xim)
2180 XIMStyles *user;
2181 XIMStyles *xim;
2183 int i, j;
2185 for (i = 0; i < user->count_styles; ++i)
2186 for (j = 0; j < xim->count_styles; ++j)
2187 if (user->supported_styles[i] == xim->supported_styles[j])
2188 return user->supported_styles[i];
2190 /* Return the default style. */
2191 return XIMPreeditNothing | XIMStatusNothing;
2194 /* Create XIC for frame F. */
2196 static XIMStyle xic_style;
2198 void
2199 create_frame_xic (f)
2200 struct frame *f;
2202 XIM xim;
2203 XIC xic = NULL;
2204 XFontSet xfs = NULL;
2206 if (FRAME_XIC (f))
2207 return;
2209 /* Create X fontset. */
2210 xfs = xic_create_xfontset
2211 (f, (FRAME_FONTSET (f) < 0) ? NULL
2212 : (char *) SDATA (fontset_ascii (FRAME_FONTSET (f))));
2214 xim = FRAME_X_XIM (f);
2215 if (xim)
2217 XRectangle s_area;
2218 XPoint spot;
2219 XVaNestedList preedit_attr;
2220 XVaNestedList status_attr;
2222 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2223 spot.x = 0; spot.y = 1;
2225 /* Determine XIC style. */
2226 if (xic_style == 0)
2228 XIMStyles supported_list;
2229 supported_list.count_styles = (sizeof supported_xim_styles
2230 / sizeof supported_xim_styles[0]);
2231 supported_list.supported_styles = supported_xim_styles;
2232 xic_style = best_xim_style (&supported_list,
2233 FRAME_X_XIM_STYLES (f));
2236 preedit_attr = XVaCreateNestedList (0,
2237 XNFontSet, xfs,
2238 XNForeground,
2239 FRAME_FOREGROUND_PIXEL (f),
2240 XNBackground,
2241 FRAME_BACKGROUND_PIXEL (f),
2242 (xic_style & XIMPreeditPosition
2243 ? XNSpotLocation
2244 : NULL),
2245 &spot,
2246 NULL);
2247 status_attr = XVaCreateNestedList (0,
2248 XNArea,
2249 &s_area,
2250 XNFontSet,
2251 xfs,
2252 XNForeground,
2253 FRAME_FOREGROUND_PIXEL (f),
2254 XNBackground,
2255 FRAME_BACKGROUND_PIXEL (f),
2256 NULL);
2258 xic = XCreateIC (xim,
2259 XNInputStyle, xic_style,
2260 XNClientWindow, FRAME_X_WINDOW (f),
2261 XNFocusWindow, FRAME_X_WINDOW (f),
2262 XNStatusAttributes, status_attr,
2263 XNPreeditAttributes, preedit_attr,
2264 NULL);
2265 XFree (preedit_attr);
2266 XFree (status_attr);
2269 FRAME_XIC (f) = xic;
2270 FRAME_XIC_STYLE (f) = xic_style;
2271 FRAME_XIC_FONTSET (f) = xfs;
2275 /* Destroy XIC and free XIC fontset of frame F, if any. */
2277 void
2278 free_frame_xic (f)
2279 struct frame *f;
2281 if (FRAME_XIC (f) == NULL)
2282 return;
2284 XDestroyIC (FRAME_XIC (f));
2285 xic_free_xfontset (f);
2287 FRAME_XIC (f) = NULL;
2291 /* Place preedit area for XIC of window W's frame to specified
2292 pixel position X/Y. X and Y are relative to window W. */
2294 void
2295 xic_set_preeditarea (w, x, y)
2296 struct window *w;
2297 int x, y;
2299 struct frame *f = XFRAME (w->frame);
2300 XVaNestedList attr;
2301 XPoint spot;
2303 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2304 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2305 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2306 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2307 XFree (attr);
2311 /* Place status area for XIC in bottom right corner of frame F.. */
2313 void
2314 xic_set_statusarea (f)
2315 struct frame *f;
2317 XIC xic = FRAME_XIC (f);
2318 XVaNestedList attr;
2319 XRectangle area;
2320 XRectangle *needed;
2322 /* Negotiate geometry of status area. If input method has existing
2323 status area, use its current size. */
2324 area.x = area.y = area.width = area.height = 0;
2325 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2326 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2327 XFree (attr);
2329 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2330 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2331 XFree (attr);
2333 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2335 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2336 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2337 XFree (attr);
2340 area.width = needed->width;
2341 area.height = needed->height;
2342 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2343 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2344 - FRAME_MENUBAR_HEIGHT (f)
2345 - FRAME_TOOLBAR_HEIGHT (f)
2346 - FRAME_INTERNAL_BORDER_WIDTH (f));
2347 XFree (needed);
2349 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2350 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2351 XFree (attr);
2355 /* Set X fontset for XIC of frame F, using base font name
2356 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2358 void
2359 xic_set_xfontset (f, base_fontname)
2360 struct frame *f;
2361 char *base_fontname;
2363 XVaNestedList attr;
2364 XFontSet xfs;
2366 xic_free_xfontset (f);
2368 xfs = xic_create_xfontset (f, base_fontname);
2370 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2371 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2372 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2373 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2374 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2375 XFree (attr);
2377 FRAME_XIC_FONTSET (f) = xfs;
2380 #endif /* HAVE_X_I18N */
2384 #ifdef USE_X_TOOLKIT
2386 /* Create and set up the X widget for frame F. */
2388 static void
2389 x_window (f, window_prompting, minibuffer_only)
2390 struct frame *f;
2391 long window_prompting;
2392 int minibuffer_only;
2394 XClassHint class_hints;
2395 XSetWindowAttributes attributes;
2396 unsigned long attribute_mask;
2397 Widget shell_widget;
2398 Widget pane_widget;
2399 Widget frame_widget;
2400 Arg al [25];
2401 int ac;
2403 BLOCK_INPUT;
2405 /* Use the resource name as the top-level widget name
2406 for looking up resources. Make a non-Lisp copy
2407 for the window manager, so GC relocation won't bother it.
2409 Elsewhere we specify the window name for the window manager. */
2412 char *str = (char *) SDATA (Vx_resource_name);
2413 f->namebuf = (char *) xmalloc (strlen (str) + 1);
2414 strcpy (f->namebuf, str);
2417 ac = 0;
2418 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2419 XtSetArg (al[ac], XtNinput, 1); ac++;
2420 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2421 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2422 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2423 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2424 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2425 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2426 applicationShellWidgetClass,
2427 FRAME_X_DISPLAY (f), al, ac);
2429 f->output_data.x->widget = shell_widget;
2430 /* maybe_set_screen_title_format (shell_widget); */
2432 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2433 (widget_value *) NULL,
2434 shell_widget, False,
2435 (lw_callback) NULL,
2436 (lw_callback) NULL,
2437 (lw_callback) NULL,
2438 (lw_callback) NULL);
2440 ac = 0;
2441 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2442 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2443 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2444 XtSetValues (pane_widget, al, ac);
2445 f->output_data.x->column_widget = pane_widget;
2447 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2448 the emacs screen when changing menubar. This reduces flickering. */
2450 ac = 0;
2451 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2452 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2453 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2454 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2455 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2456 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2457 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2458 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2459 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2460 al, ac);
2462 f->output_data.x->edit_widget = frame_widget;
2464 XtManageChild (frame_widget);
2466 /* Do some needed geometry management. */
2468 int len;
2469 char *tem, shell_position[32];
2470 Arg al[10];
2471 int ac = 0;
2472 int extra_borders = 0;
2473 int menubar_size
2474 = (f->output_data.x->menubar_widget
2475 ? (f->output_data.x->menubar_widget->core.height
2476 + f->output_data.x->menubar_widget->core.border_width)
2477 : 0);
2479 #if 0 /* Experimentally, we now get the right results
2480 for -geometry -0-0 without this. 24 Aug 96, rms. */
2481 if (FRAME_EXTERNAL_MENU_BAR (f))
2483 Dimension ibw = 0;
2484 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2485 menubar_size += ibw;
2487 #endif
2489 f->output_data.x->menubar_height = menubar_size;
2491 #ifndef USE_LUCID
2492 /* Motif seems to need this amount added to the sizes
2493 specified for the shell widget. The Athena/Lucid widgets don't.
2494 Both conclusions reached experimentally. -- rms. */
2495 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2496 &extra_borders, NULL);
2497 extra_borders *= 2;
2498 #endif
2500 /* Convert our geometry parameters into a geometry string
2501 and specify it.
2502 Note that we do not specify here whether the position
2503 is a user-specified or program-specified one.
2504 We pass that information later, in x_wm_set_size_hints. */
2506 int left = f->left_pos;
2507 int xneg = window_prompting & XNegative;
2508 int top = f->top_pos;
2509 int yneg = window_prompting & YNegative;
2510 if (xneg)
2511 left = -left;
2512 if (yneg)
2513 top = -top;
2515 if (window_prompting & USPosition)
2516 sprintf (shell_position, "=%dx%d%c%d%c%d",
2517 FRAME_PIXEL_WIDTH (f) + extra_borders,
2518 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2519 (xneg ? '-' : '+'), left,
2520 (yneg ? '-' : '+'), top);
2521 else
2523 sprintf (shell_position, "=%dx%d",
2524 FRAME_PIXEL_WIDTH (f) + extra_borders,
2525 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2527 /* Setting x and y when the position is not specified in
2528 the geometry string will set program position in the WM hints.
2529 If Emacs had just one program position, we could set it in
2530 fallback resources, but since each make-frame call can specify
2531 different program positions, this is easier. */
2532 XtSetArg (al[ac], XtNx, left); ac++;
2533 XtSetArg (al[ac], XtNy, top); ac++;
2537 len = strlen (shell_position) + 1;
2538 /* We don't free this because we don't know whether
2539 it is safe to free it while the frame exists.
2540 It isn't worth the trouble of arranging to free it
2541 when the frame is deleted. */
2542 tem = (char *) xmalloc (len);
2543 strncpy (tem, shell_position, len);
2544 XtSetArg (al[ac], XtNgeometry, tem); ac++;
2545 XtSetValues (shell_widget, al, ac);
2548 XtManageChild (pane_widget);
2549 XtRealizeWidget (shell_widget);
2551 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2553 validate_x_resource_name ();
2555 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2556 class_hints.res_class = (char *) SDATA (Vx_resource_class);
2557 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2559 #ifdef HAVE_X_I18N
2560 FRAME_XIC (f) = NULL;
2561 if (use_xim)
2562 create_frame_xic (f);
2563 #endif
2565 f->output_data.x->wm_hints.input = True;
2566 f->output_data.x->wm_hints.flags |= InputHint;
2567 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2568 &f->output_data.x->wm_hints);
2570 hack_wm_protocols (f, shell_widget);
2572 #ifdef HACK_EDITRES
2573 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2574 #endif
2576 /* Do a stupid property change to force the server to generate a
2577 PropertyNotify event so that the event_stream server timestamp will
2578 be initialized to something relevant to the time we created the window.
2580 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2581 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2582 XA_ATOM, 32, PropModeAppend,
2583 (unsigned char*) NULL, 0);
2585 /* Make all the standard events reach the Emacs frame. */
2586 attributes.event_mask = STANDARD_EVENT_SET;
2588 #ifdef HAVE_X_I18N
2589 if (FRAME_XIC (f))
2591 /* XIM server might require some X events. */
2592 unsigned long fevent = NoEventMask;
2593 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2594 attributes.event_mask |= fevent;
2596 #endif /* HAVE_X_I18N */
2598 attribute_mask = CWEventMask;
2599 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2600 attribute_mask, &attributes);
2602 XtMapWidget (frame_widget);
2604 /* x_set_name normally ignores requests to set the name if the
2605 requested name is the same as the current name. This is the one
2606 place where that assumption isn't correct; f->name is set, but
2607 the X server hasn't been told. */
2609 Lisp_Object name;
2610 int explicit = f->explicit_name;
2612 f->explicit_name = 0;
2613 name = f->name;
2614 f->name = Qnil;
2615 x_set_name (f, name, explicit);
2618 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2619 f->output_data.x->text_cursor);
2621 UNBLOCK_INPUT;
2623 /* This is a no-op, except under Motif. Make sure main areas are
2624 set to something reasonable, in case we get an error later. */
2625 lw_set_main_areas (pane_widget, 0, frame_widget);
2628 #else /* not USE_X_TOOLKIT */
2629 #ifdef USE_GTK
2630 void
2631 x_window (f)
2632 FRAME_PTR f;
2634 if (! xg_create_frame_widgets (f))
2635 error ("Unable to create window");
2637 #ifdef HAVE_X_I18N
2638 FRAME_XIC (f) = NULL;
2639 if (use_xim)
2641 BLOCK_INPUT;
2642 create_frame_xic (f);
2643 if (FRAME_XIC (f))
2645 /* XIM server might require some X events. */
2646 unsigned long fevent = NoEventMask;
2647 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2649 if (fevent != NoEventMask)
2651 XSetWindowAttributes attributes;
2652 XWindowAttributes wattr;
2653 unsigned long attribute_mask;
2655 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2656 &wattr);
2657 attributes.event_mask = wattr.your_event_mask | fevent;
2658 attribute_mask = CWEventMask;
2659 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2660 attribute_mask, &attributes);
2663 UNBLOCK_INPUT;
2665 #endif
2668 #else /*! USE_GTK */
2669 /* Create and set up the X window for frame F. */
2671 void
2672 x_window (f)
2673 struct frame *f;
2676 XClassHint class_hints;
2677 XSetWindowAttributes attributes;
2678 unsigned long attribute_mask;
2680 attributes.background_pixel = f->output_data.x->background_pixel;
2681 attributes.border_pixel = f->output_data.x->border_pixel;
2682 attributes.bit_gravity = StaticGravity;
2683 attributes.backing_store = NotUseful;
2684 attributes.save_under = True;
2685 attributes.event_mask = STANDARD_EVENT_SET;
2686 attributes.colormap = FRAME_X_COLORMAP (f);
2687 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2688 | CWColormap);
2690 BLOCK_INPUT;
2691 FRAME_X_WINDOW (f)
2692 = XCreateWindow (FRAME_X_DISPLAY (f),
2693 f->output_data.x->parent_desc,
2694 f->left_pos,
2695 f->top_pos,
2696 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2697 f->border_width,
2698 CopyFromParent, /* depth */
2699 InputOutput, /* class */
2700 FRAME_X_VISUAL (f),
2701 attribute_mask, &attributes);
2703 #ifdef HAVE_X_I18N
2704 if (use_xim)
2706 create_frame_xic (f);
2707 if (FRAME_XIC (f))
2709 /* XIM server might require some X events. */
2710 unsigned long fevent = NoEventMask;
2711 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2712 attributes.event_mask |= fevent;
2713 attribute_mask = CWEventMask;
2714 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2715 attribute_mask, &attributes);
2718 #endif /* HAVE_X_I18N */
2720 validate_x_resource_name ();
2722 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2723 class_hints.res_class = (char *) SDATA (Vx_resource_class);
2724 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2726 /* The menubar is part of the ordinary display;
2727 it does not count in addition to the height of the window. */
2728 f->output_data.x->menubar_height = 0;
2730 /* This indicates that we use the "Passive Input" input model.
2731 Unless we do this, we don't get the Focus{In,Out} events that we
2732 need to draw the cursor correctly. Accursed bureaucrats.
2733 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2735 f->output_data.x->wm_hints.input = True;
2736 f->output_data.x->wm_hints.flags |= InputHint;
2737 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2738 &f->output_data.x->wm_hints);
2739 f->output_data.x->wm_hints.icon_pixmap = None;
2741 /* Request "save yourself" and "delete window" commands from wm. */
2743 Atom protocols[2];
2744 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2745 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2746 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2749 /* x_set_name normally ignores requests to set the name if the
2750 requested name is the same as the current name. This is the one
2751 place where that assumption isn't correct; f->name is set, but
2752 the X server hasn't been told. */
2754 Lisp_Object name;
2755 int explicit = f->explicit_name;
2757 f->explicit_name = 0;
2758 name = f->name;
2759 f->name = Qnil;
2760 x_set_name (f, name, explicit);
2763 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2764 f->output_data.x->text_cursor);
2766 UNBLOCK_INPUT;
2768 if (FRAME_X_WINDOW (f) == 0)
2769 error ("Unable to create window");
2772 #endif /* not USE_GTK */
2773 #endif /* not USE_X_TOOLKIT */
2775 /* Verify that the icon position args for this window are valid. */
2777 static void
2778 x_icon_verify (f, parms)
2779 struct frame *f;
2780 Lisp_Object parms;
2782 Lisp_Object icon_x, icon_y;
2784 /* Set the position of the icon. Note that twm groups all
2785 icons in an icon window. */
2786 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2787 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2788 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2790 CHECK_NUMBER (icon_x);
2791 CHECK_NUMBER (icon_y);
2793 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2794 error ("Both left and top icon corners of icon must be specified");
2797 /* Handle the icon stuff for this window. Perhaps later we might
2798 want an x_set_icon_position which can be called interactively as
2799 well. */
2801 static void
2802 x_icon (f, parms)
2803 struct frame *f;
2804 Lisp_Object parms;
2806 Lisp_Object icon_x, icon_y;
2807 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2809 /* Set the position of the icon. Note that twm groups all
2810 icons in an icon window. */
2811 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2812 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2813 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2815 CHECK_NUMBER (icon_x);
2816 CHECK_NUMBER (icon_y);
2818 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2819 error ("Both left and top icon corners of icon must be specified");
2821 BLOCK_INPUT;
2823 if (! EQ (icon_x, Qunbound))
2824 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2826 /* Start up iconic or window? */
2827 x_wm_set_window_state
2828 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2829 Qicon)
2830 ? IconicState
2831 : NormalState));
2833 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
2834 ? f->icon_name
2835 : f->name)));
2837 UNBLOCK_INPUT;
2840 /* Make the GCs needed for this window, setting the
2841 background, border and mouse colors; also create the
2842 mouse cursor and the gray border tile. */
2844 static char cursor_bits[] =
2846 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2847 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2848 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2849 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2852 static void
2853 x_make_gc (f)
2854 struct frame *f;
2856 XGCValues gc_values;
2858 BLOCK_INPUT;
2860 /* Create the GCs of this frame.
2861 Note that many default values are used. */
2863 /* Normal video */
2864 gc_values.font = FRAME_FONT (f)->fid;
2865 gc_values.foreground = f->output_data.x->foreground_pixel;
2866 gc_values.background = f->output_data.x->background_pixel;
2867 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2868 f->output_data.x->normal_gc
2869 = XCreateGC (FRAME_X_DISPLAY (f),
2870 FRAME_X_WINDOW (f),
2871 GCLineWidth | GCFont | GCForeground | GCBackground,
2872 &gc_values);
2874 /* Reverse video style. */
2875 gc_values.foreground = f->output_data.x->background_pixel;
2876 gc_values.background = f->output_data.x->foreground_pixel;
2877 f->output_data.x->reverse_gc
2878 = XCreateGC (FRAME_X_DISPLAY (f),
2879 FRAME_X_WINDOW (f),
2880 GCFont | GCForeground | GCBackground | GCLineWidth,
2881 &gc_values);
2883 /* Cursor has cursor-color background, background-color foreground. */
2884 gc_values.foreground = f->output_data.x->background_pixel;
2885 gc_values.background = f->output_data.x->cursor_pixel;
2886 gc_values.fill_style = FillOpaqueStippled;
2887 gc_values.stipple
2888 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
2889 FRAME_X_DISPLAY_INFO (f)->root_window,
2890 cursor_bits, 16, 16);
2891 f->output_data.x->cursor_gc
2892 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2893 (GCFont | GCForeground | GCBackground
2894 | GCFillStyle /* | GCStipple */ | GCLineWidth),
2895 &gc_values);
2897 /* Reliefs. */
2898 f->output_data.x->white_relief.gc = 0;
2899 f->output_data.x->black_relief.gc = 0;
2901 /* Create the gray border tile used when the pointer is not in
2902 the frame. Since this depends on the frame's pixel values,
2903 this must be done on a per-frame basis. */
2904 f->output_data.x->border_tile
2905 = (XCreatePixmapFromBitmapData
2906 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2907 gray_bits, gray_width, gray_height,
2908 f->output_data.x->foreground_pixel,
2909 f->output_data.x->background_pixel,
2910 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2912 UNBLOCK_INPUT;
2916 /* Free what was was allocated in x_make_gc. */
2918 void
2919 x_free_gcs (f)
2920 struct frame *f;
2922 Display *dpy = FRAME_X_DISPLAY (f);
2924 BLOCK_INPUT;
2926 if (f->output_data.x->normal_gc)
2928 XFreeGC (dpy, f->output_data.x->normal_gc);
2929 f->output_data.x->normal_gc = 0;
2932 if (f->output_data.x->reverse_gc)
2934 XFreeGC (dpy, f->output_data.x->reverse_gc);
2935 f->output_data.x->reverse_gc = 0;
2938 if (f->output_data.x->cursor_gc)
2940 XFreeGC (dpy, f->output_data.x->cursor_gc);
2941 f->output_data.x->cursor_gc = 0;
2944 if (f->output_data.x->border_tile)
2946 XFreePixmap (dpy, f->output_data.x->border_tile);
2947 f->output_data.x->border_tile = 0;
2950 UNBLOCK_INPUT;
2954 /* Handler for signals raised during x_create_frame and
2955 x_create_top_frame. FRAME is the frame which is partially
2956 constructed. */
2958 static Lisp_Object
2959 unwind_create_frame (frame)
2960 Lisp_Object frame;
2962 struct frame *f = XFRAME (frame);
2964 /* If frame is ``official'', nothing to do. */
2965 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
2967 #if GLYPH_DEBUG
2968 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2969 #endif
2971 x_free_frame_resources (f);
2973 #if GLYPH_DEBUG
2974 /* Check that reference counts are indeed correct. */
2975 xassert (dpyinfo->reference_count == dpyinfo_refcount);
2976 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
2977 #endif
2978 return Qt;
2981 return Qnil;
2985 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2986 1, 1, 0,
2987 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2988 Returns an Emacs frame object.
2989 ALIST is an alist of frame parameters.
2990 If the parameters specify that the frame should not have a minibuffer,
2991 and do not specify a specific minibuffer window to use,
2992 then `default-minibuffer-frame' must be a frame whose minibuffer can
2993 be shared by the new frame.
2995 This function is an internal primitive--use `make-frame' instead. */)
2996 (parms)
2997 Lisp_Object parms;
2999 struct frame *f;
3000 Lisp_Object frame, tem;
3001 Lisp_Object name;
3002 int minibuffer_only = 0;
3003 long window_prompting = 0;
3004 int width, height;
3005 int count = SPECPDL_INDEX ();
3006 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3007 Lisp_Object display;
3008 struct x_display_info *dpyinfo = NULL;
3009 Lisp_Object parent;
3010 struct kboard *kb;
3012 check_x ();
3014 /* Use this general default value to start with
3015 until we know if this frame has a specified name. */
3016 Vx_resource_name = Vinvocation_name;
3018 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3019 if (EQ (display, Qunbound))
3020 display = Qnil;
3021 dpyinfo = check_x_display_info (display);
3022 #ifdef MULTI_KBOARD
3023 kb = dpyinfo->kboard;
3024 #else
3025 kb = &the_only_kboard;
3026 #endif
3028 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3029 if (!STRINGP (name)
3030 && ! EQ (name, Qunbound)
3031 && ! NILP (name))
3032 error ("Invalid frame name--not a string or nil");
3034 if (STRINGP (name))
3035 Vx_resource_name = name;
3037 /* See if parent window is specified. */
3038 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3039 if (EQ (parent, Qunbound))
3040 parent = Qnil;
3041 if (! NILP (parent))
3042 CHECK_NUMBER (parent);
3044 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3045 /* No need to protect DISPLAY because that's not used after passing
3046 it to make_frame_without_minibuffer. */
3047 frame = Qnil;
3048 GCPRO4 (parms, parent, name, frame);
3049 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3050 RES_TYPE_SYMBOL);
3051 if (EQ (tem, Qnone) || NILP (tem))
3052 f = make_frame_without_minibuffer (Qnil, kb, display);
3053 else if (EQ (tem, Qonly))
3055 f = make_minibuffer_frame ();
3056 minibuffer_only = 1;
3058 else if (WINDOWP (tem))
3059 f = make_frame_without_minibuffer (tem, kb, display);
3060 else
3061 f = make_frame (1);
3063 XSETFRAME (frame, f);
3065 /* Note that X Windows does support scroll bars. */
3066 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
3068 f->output_method = output_x_window;
3069 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
3070 bzero (f->output_data.x, sizeof (struct x_output));
3071 f->output_data.x->icon_bitmap = -1;
3072 FRAME_FONTSET (f) = -1;
3073 f->output_data.x->scroll_bar_foreground_pixel = -1;
3074 f->output_data.x->scroll_bar_background_pixel = -1;
3075 #ifdef USE_TOOLKIT_SCROLL_BARS
3076 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3077 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3078 #endif /* USE_TOOLKIT_SCROLL_BARS */
3079 record_unwind_protect (unwind_create_frame, frame);
3081 f->icon_name
3082 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3083 RES_TYPE_STRING);
3084 if (! STRINGP (f->icon_name))
3085 f->icon_name = Qnil;
3087 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3088 #if GLYPH_DEBUG
3089 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
3090 dpyinfo_refcount = dpyinfo->reference_count;
3091 #endif /* GLYPH_DEBUG */
3092 #ifdef MULTI_KBOARD
3093 FRAME_KBOARD (f) = kb;
3094 #endif
3096 /* These colors will be set anyway later, but it's important
3097 to get the color reference counts right, so initialize them! */
3099 Lisp_Object black;
3100 struct gcpro gcpro1;
3102 /* Function x_decode_color can signal an error. Make
3103 sure to initialize color slots so that we won't try
3104 to free colors we haven't allocated. */
3105 f->output_data.x->foreground_pixel = -1;
3106 f->output_data.x->background_pixel = -1;
3107 f->output_data.x->cursor_pixel = -1;
3108 f->output_data.x->cursor_foreground_pixel = -1;
3109 f->output_data.x->border_pixel = -1;
3110 f->output_data.x->mouse_pixel = -1;
3112 black = build_string ("black");
3113 GCPRO1 (black);
3114 f->output_data.x->foreground_pixel
3115 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3116 f->output_data.x->background_pixel
3117 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3118 f->output_data.x->cursor_pixel
3119 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3120 f->output_data.x->cursor_foreground_pixel
3121 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3122 f->output_data.x->border_pixel
3123 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3124 f->output_data.x->mouse_pixel
3125 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3126 UNGCPRO;
3129 /* Specify the parent under which to make this X window. */
3131 if (!NILP (parent))
3133 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3134 f->output_data.x->explicit_parent = 1;
3136 else
3138 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3139 f->output_data.x->explicit_parent = 0;
3142 /* Set the name; the functions to which we pass f expect the name to
3143 be set. */
3144 if (EQ (name, Qunbound) || NILP (name))
3146 f->name = build_string (dpyinfo->x_id_name);
3147 f->explicit_name = 0;
3149 else
3151 f->name = name;
3152 f->explicit_name = 1;
3153 /* use the frame's title when getting resources for this frame. */
3154 specbind (Qx_resource_name, name);
3157 /* Extract the window parameters from the supplied values
3158 that are needed to determine window geometry. */
3160 Lisp_Object font;
3162 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
3164 BLOCK_INPUT;
3165 /* First, try whatever font the caller has specified. */
3166 if (STRINGP (font))
3168 tem = Fquery_fontset (font, Qnil);
3169 if (STRINGP (tem))
3170 font = x_new_fontset (f, SDATA (tem));
3171 else
3172 font = x_new_font (f, SDATA (font));
3175 /* Try out a font which we hope has bold and italic variations. */
3176 if (!STRINGP (font))
3177 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
3178 if (!STRINGP (font))
3179 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
3180 if (! STRINGP (font))
3181 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
3182 if (! STRINGP (font))
3183 /* This was formerly the first thing tried, but it finds too many fonts
3184 and takes too long. */
3185 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
3186 /* If those didn't work, look for something which will at least work. */
3187 if (! STRINGP (font))
3188 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
3189 UNBLOCK_INPUT;
3190 if (! STRINGP (font))
3191 font = build_string ("fixed");
3193 x_default_parameter (f, parms, Qfont, font,
3194 "font", "Font", RES_TYPE_STRING);
3197 #ifdef USE_LUCID
3198 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
3199 whereby it fails to get any font. */
3200 xlwmenu_default_font = FRAME_FONT (f);
3201 #endif
3203 x_default_parameter (f, parms, Qborder_width, make_number (2),
3204 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3206 /* This defaults to 1 in order to match xterm. We recognize either
3207 internalBorderWidth or internalBorder (which is what xterm calls
3208 it). */
3209 if (NILP (Fassq (Qinternal_border_width, parms)))
3211 Lisp_Object value;
3213 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3214 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3215 if (! EQ (value, Qunbound))
3216 parms = Fcons (Fcons (Qinternal_border_width, value),
3217 parms);
3219 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
3220 "internalBorderWidth", "internalBorderWidth",
3221 RES_TYPE_NUMBER);
3222 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
3223 "verticalScrollBars", "ScrollBars",
3224 RES_TYPE_SYMBOL);
3226 /* Also do the stuff which must be set before the window exists. */
3227 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3228 "foreground", "Foreground", RES_TYPE_STRING);
3229 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3230 "background", "Background", RES_TYPE_STRING);
3231 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3232 "pointerColor", "Foreground", RES_TYPE_STRING);
3233 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
3234 "cursorColor", "Foreground", RES_TYPE_STRING);
3235 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3236 "borderColor", "BorderColor", RES_TYPE_STRING);
3237 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3238 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3239 x_default_parameter (f, parms, Qline_spacing, Qnil,
3240 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3241 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3242 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3243 x_default_parameter (f, parms, Qright_fringe, Qnil,
3244 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3246 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3247 "scrollBarForeground",
3248 "ScrollBarForeground", 1);
3249 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3250 "scrollBarBackground",
3251 "ScrollBarBackground", 0);
3253 /* Init faces before x_default_parameter is called for scroll-bar
3254 parameters because that function calls x_set_scroll_bar_width,
3255 which calls change_frame_size, which calls Fset_window_buffer,
3256 which runs hooks, which call Fvertical_motion. At the end, we
3257 end up in init_iterator with a null face cache, which should not
3258 happen. */
3259 init_frame_faces (f);
3261 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
3262 "menuBar", "MenuBar", RES_TYPE_NUMBER);
3263 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
3264 "toolBar", "ToolBar", RES_TYPE_NUMBER);
3265 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3266 "bufferPredicate", "BufferPredicate",
3267 RES_TYPE_SYMBOL);
3268 x_default_parameter (f, parms, Qtitle, Qnil,
3269 "title", "Title", RES_TYPE_STRING);
3270 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3271 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3272 x_default_parameter (f, parms, Qfullscreen, Qnil,
3273 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3275 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3277 /* Compute the size of the X window. */
3278 window_prompting = x_figure_window_size (f, parms, 1);
3280 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3281 f->no_split = minibuffer_only || EQ (tem, Qt);
3283 x_icon_verify (f, parms);
3285 /* Create the X widget or window. */
3286 #ifdef USE_X_TOOLKIT
3287 x_window (f, window_prompting, minibuffer_only);
3288 #else
3289 x_window (f);
3290 #endif
3292 x_icon (f, parms);
3293 x_make_gc (f);
3295 /* Now consider the frame official. */
3296 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3297 Vframe_list = Fcons (frame, Vframe_list);
3299 /* We need to do this after creating the X window, so that the
3300 icon-creation functions can say whose icon they're describing. */
3301 x_default_parameter (f, parms, Qicon_type, Qt,
3302 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
3304 x_default_parameter (f, parms, Qauto_raise, Qnil,
3305 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3306 x_default_parameter (f, parms, Qauto_lower, Qnil,
3307 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3308 x_default_parameter (f, parms, Qcursor_type, Qbox,
3309 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3310 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3311 "scrollBarWidth", "ScrollBarWidth",
3312 RES_TYPE_NUMBER);
3314 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3315 Change will not be effected unless different from the current
3316 FRAME_LINES (f). */
3317 width = FRAME_COLS (f);
3318 height = FRAME_LINES (f);
3320 SET_FRAME_COLS (f, 0);
3321 FRAME_LINES (f) = 0;
3322 change_frame_size (f, height, width, 1, 0, 0);
3324 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3325 /* Create the menu bar. */
3326 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3328 /* If this signals an error, we haven't set size hints for the
3329 frame and we didn't make it visible. */
3330 initialize_frame_menubar (f);
3332 #ifndef USE_GTK
3333 /* This is a no-op, except under Motif where it arranges the
3334 main window for the widgets on it. */
3335 lw_set_main_areas (f->output_data.x->column_widget,
3336 f->output_data.x->menubar_widget,
3337 f->output_data.x->edit_widget);
3338 #endif /* not USE_GTK */
3340 #endif /* USE_X_TOOLKIT || USE_GTK */
3342 /* Tell the server what size and position, etc, we want, and how
3343 badly we want them. This should be done after we have the menu
3344 bar so that its size can be taken into account. */
3345 BLOCK_INPUT;
3346 x_wm_set_size_hint (f, window_prompting, 0);
3347 UNBLOCK_INPUT;
3349 /* Make the window appear on the frame and enable display, unless
3350 the caller says not to. However, with explicit parent, Emacs
3351 cannot control visibility, so don't try. */
3352 if (! f->output_data.x->explicit_parent)
3354 Lisp_Object visibility;
3356 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3357 RES_TYPE_SYMBOL);
3358 if (EQ (visibility, Qunbound))
3359 visibility = Qt;
3361 if (EQ (visibility, Qicon))
3362 x_iconify_frame (f);
3363 else if (! NILP (visibility))
3364 x_make_frame_visible (f);
3365 else
3366 /* Must have been Qnil. */
3370 /* Set the WM leader property. GTK does this itself, so this is not
3371 needed when using GTK. */
3372 if (dpyinfo->client_leader_window != 0)
3374 BLOCK_INPUT;
3375 XChangeProperty (FRAME_X_DISPLAY (f),
3376 FRAME_OUTER_WINDOW (f),
3377 dpyinfo->Xatom_wm_client_leader,
3378 XA_WINDOW, 32, PropModeReplace,
3379 (char *) &dpyinfo->client_leader_window, 1);
3380 UNBLOCK_INPUT;
3383 UNGCPRO;
3385 /* Make sure windows on this frame appear in calls to next-window
3386 and similar functions. */
3387 Vwindow_list = Qnil;
3389 return unbind_to (count, frame);
3393 /* FRAME is used only to get a handle on the X display. We don't pass the
3394 display info directly because we're called from frame.c, which doesn't
3395 know about that structure. */
3397 Lisp_Object
3398 x_get_focus_frame (frame)
3399 struct frame *frame;
3401 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
3402 Lisp_Object xfocus;
3403 if (! dpyinfo->x_focus_frame)
3404 return Qnil;
3406 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3407 return xfocus;
3411 /* In certain situations, when the window manager follows a
3412 click-to-focus policy, there seems to be no way around calling
3413 XSetInputFocus to give another frame the input focus .
3415 In an ideal world, XSetInputFocus should generally be avoided so
3416 that applications don't interfere with the window manager's focus
3417 policy. But I think it's okay to use when it's clearly done
3418 following a user-command. */
3420 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
3421 doc: /* Set the input focus to FRAME.
3422 FRAME nil means use the selected frame. */)
3423 (frame)
3424 Lisp_Object frame;
3426 struct frame *f = check_x_frame (frame);
3427 Display *dpy = FRAME_X_DISPLAY (f);
3428 int count;
3430 BLOCK_INPUT;
3431 count = x_catch_errors (dpy);
3432 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3433 RevertToParent, CurrentTime);
3434 x_uncatch_errors (dpy, count);
3435 UNBLOCK_INPUT;
3437 return Qnil;
3441 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3442 doc: /* Internal function called by `color-defined-p', which see. */)
3443 (color, frame)
3444 Lisp_Object color, frame;
3446 XColor foo;
3447 FRAME_PTR f = check_x_frame (frame);
3449 CHECK_STRING (color);
3451 if (x_defined_color (f, SDATA (color), &foo, 0))
3452 return Qt;
3453 else
3454 return Qnil;
3457 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3458 doc: /* Internal function called by `color-values', which see. */)
3459 (color, frame)
3460 Lisp_Object color, frame;
3462 XColor foo;
3463 FRAME_PTR f = check_x_frame (frame);
3465 CHECK_STRING (color);
3467 if (x_defined_color (f, SDATA (color), &foo, 0))
3469 Lisp_Object rgb[3];
3471 rgb[0] = make_number (foo.red);
3472 rgb[1] = make_number (foo.green);
3473 rgb[2] = make_number (foo.blue);
3474 return Flist (3, rgb);
3476 else
3477 return Qnil;
3480 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3481 doc: /* Internal function called by `display-color-p', which see. */)
3482 (display)
3483 Lisp_Object display;
3485 struct x_display_info *dpyinfo = check_x_display_info (display);
3487 if (dpyinfo->n_planes <= 2)
3488 return Qnil;
3490 switch (dpyinfo->visual->class)
3492 case StaticColor:
3493 case PseudoColor:
3494 case TrueColor:
3495 case DirectColor:
3496 return Qt;
3498 default:
3499 return Qnil;
3503 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3504 0, 1, 0,
3505 doc: /* Return t if the X display supports shades of gray.
3506 Note that color displays do support shades of gray.
3507 The optional argument DISPLAY specifies which display to ask about.
3508 DISPLAY should be either a frame or a display name (a string).
3509 If omitted or nil, that stands for the selected frame's display. */)
3510 (display)
3511 Lisp_Object display;
3513 struct x_display_info *dpyinfo = check_x_display_info (display);
3515 if (dpyinfo->n_planes <= 1)
3516 return Qnil;
3518 switch (dpyinfo->visual->class)
3520 case StaticColor:
3521 case PseudoColor:
3522 case TrueColor:
3523 case DirectColor:
3524 case StaticGray:
3525 case GrayScale:
3526 return Qt;
3528 default:
3529 return Qnil;
3533 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3534 0, 1, 0,
3535 doc: /* Returns the width in pixels of the X display DISPLAY.
3536 The optional argument DISPLAY specifies which display to ask about.
3537 DISPLAY should be either a frame or a display name (a string).
3538 If omitted or nil, that stands for the selected frame's display. */)
3539 (display)
3540 Lisp_Object display;
3542 struct x_display_info *dpyinfo = check_x_display_info (display);
3544 return make_number (dpyinfo->width);
3547 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3548 Sx_display_pixel_height, 0, 1, 0,
3549 doc: /* Returns the height in pixels of the X display DISPLAY.
3550 The optional argument DISPLAY specifies which display to ask about.
3551 DISPLAY should be either a frame or a display name (a string).
3552 If omitted or nil, that stands for the selected frame's display. */)
3553 (display)
3554 Lisp_Object display;
3556 struct x_display_info *dpyinfo = check_x_display_info (display);
3558 return make_number (dpyinfo->height);
3561 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3562 0, 1, 0,
3563 doc: /* Returns the number of bitplanes of the X display DISPLAY.
3564 The optional argument DISPLAY specifies which display to ask about.
3565 DISPLAY should be either a frame or a display name (a string).
3566 If omitted or nil, that stands for the selected frame's display. */)
3567 (display)
3568 Lisp_Object display;
3570 struct x_display_info *dpyinfo = check_x_display_info (display);
3572 return make_number (dpyinfo->n_planes);
3575 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3576 0, 1, 0,
3577 doc: /* Returns the number of color cells of the X display DISPLAY.
3578 The optional argument DISPLAY specifies which display to ask about.
3579 DISPLAY should be either a frame or a display name (a string).
3580 If omitted or nil, that stands for the selected frame's display. */)
3581 (display)
3582 Lisp_Object display;
3584 struct x_display_info *dpyinfo = check_x_display_info (display);
3586 int nr_planes = DisplayPlanes (dpyinfo->display,
3587 XScreenNumberOfScreen (dpyinfo->screen));
3589 /* Truncate nr_planes to 24 to avoid integer overflow.
3590 Some displays says 32, but only 24 bits are actually significant.
3591 There are only very few and rare video cards that have more than
3592 24 significant bits. Also 24 bits is more than 16 million colors,
3593 it "should be enough for everyone". */
3594 if (nr_planes > 24) nr_planes = 24;
3596 return make_number (1 << nr_planes);
3599 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3600 Sx_server_max_request_size,
3601 0, 1, 0,
3602 doc: /* Returns the maximum request size of the X server of display DISPLAY.
3603 The optional argument DISPLAY specifies which display to ask about.
3604 DISPLAY should be either a frame or a display name (a string).
3605 If omitted or nil, that stands for the selected frame's display. */)
3606 (display)
3607 Lisp_Object display;
3609 struct x_display_info *dpyinfo = check_x_display_info (display);
3611 return make_number (MAXREQUEST (dpyinfo->display));
3614 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3615 doc: /* Returns the "vendor ID" string of the X server of display DISPLAY.
3616 \(Labelling every distributor as a "vendor" embodies the false assumption
3617 that operating systems cannot be developed and distributed noncommercially.)
3618 The optional argument DISPLAY specifies which display to ask about.
3619 DISPLAY should be either a frame or a display name (a string).
3620 If omitted or nil, that stands for the selected frame's display. */)
3621 (display)
3622 Lisp_Object display;
3624 struct x_display_info *dpyinfo = check_x_display_info (display);
3625 char *vendor = ServerVendor (dpyinfo->display);
3627 if (! vendor) vendor = "";
3628 return build_string (vendor);
3631 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3632 doc: /* Returns the version numbers of the X server of display DISPLAY.
3633 The value is a list of three integers: the major and minor
3634 version numbers of the X Protocol in use, and the distributor-specific release
3635 number. See also the function `x-server-vendor'.
3637 The optional argument DISPLAY specifies which display to ask about.
3638 DISPLAY should be either a frame or a display name (a string).
3639 If omitted or nil, that stands for the selected frame's display. */)
3640 (display)
3641 Lisp_Object display;
3643 struct x_display_info *dpyinfo = check_x_display_info (display);
3644 Display *dpy = dpyinfo->display;
3646 return Fcons (make_number (ProtocolVersion (dpy)),
3647 Fcons (make_number (ProtocolRevision (dpy)),
3648 Fcons (make_number (VendorRelease (dpy)), Qnil)));
3651 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3652 doc: /* Return the number of screens on the X server of display DISPLAY.
3653 The optional argument DISPLAY specifies which display to ask about.
3654 DISPLAY should be either a frame or a display name (a string).
3655 If omitted or nil, that stands for the selected frame's display. */)
3656 (display)
3657 Lisp_Object display;
3659 struct x_display_info *dpyinfo = check_x_display_info (display);
3661 return make_number (ScreenCount (dpyinfo->display));
3664 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3665 doc: /* Return the height in millimeters of the X display DISPLAY.
3666 The optional argument DISPLAY specifies which display to ask about.
3667 DISPLAY should be either a frame or a display name (a string).
3668 If omitted or nil, that stands for the selected frame's display. */)
3669 (display)
3670 Lisp_Object display;
3672 struct x_display_info *dpyinfo = check_x_display_info (display);
3674 return make_number (HeightMMOfScreen (dpyinfo->screen));
3677 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3678 doc: /* Return the width in millimeters of the X display DISPLAY.
3679 The optional argument DISPLAY specifies which display to ask about.
3680 DISPLAY should be either a frame or a display name (a string).
3681 If omitted or nil, that stands for the selected frame's display. */)
3682 (display)
3683 Lisp_Object display;
3685 struct x_display_info *dpyinfo = check_x_display_info (display);
3687 return make_number (WidthMMOfScreen (dpyinfo->screen));
3690 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3691 Sx_display_backing_store, 0, 1, 0,
3692 doc: /* Returns an indication of whether X display DISPLAY does backing store.
3693 The value may be `always', `when-mapped', or `not-useful'.
3694 The optional argument DISPLAY specifies which display to ask about.
3695 DISPLAY should be either a frame or a display name (a string).
3696 If omitted or nil, that stands for the selected frame's display. */)
3697 (display)
3698 Lisp_Object display;
3700 struct x_display_info *dpyinfo = check_x_display_info (display);
3701 Lisp_Object result;
3703 switch (DoesBackingStore (dpyinfo->screen))
3705 case Always:
3706 result = intern ("always");
3707 break;
3709 case WhenMapped:
3710 result = intern ("when-mapped");
3711 break;
3713 case NotUseful:
3714 result = intern ("not-useful");
3715 break;
3717 default:
3718 error ("Strange value for BackingStore parameter of screen");
3719 result = Qnil;
3722 return result;
3725 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3726 Sx_display_visual_class, 0, 1, 0,
3727 doc: /* Return the visual class of the X display DISPLAY.
3728 The value is one of the symbols `static-gray', `gray-scale',
3729 `static-color', `pseudo-color', `true-color', or `direct-color'.
3731 The optional argument DISPLAY specifies which display to ask about.
3732 DISPLAY should be either a frame or a display name (a string).
3733 If omitted or nil, that stands for the selected frame's display. */)
3734 (display)
3735 Lisp_Object display;
3737 struct x_display_info *dpyinfo = check_x_display_info (display);
3738 Lisp_Object result;
3740 switch (dpyinfo->visual->class)
3742 case StaticGray:
3743 result = intern ("static-gray");
3744 break;
3745 case GrayScale:
3746 result = intern ("gray-scale");
3747 break;
3748 case StaticColor:
3749 result = intern ("static-color");
3750 break;
3751 case PseudoColor:
3752 result = intern ("pseudo-color");
3753 break;
3754 case TrueColor:
3755 result = intern ("true-color");
3756 break;
3757 case DirectColor:
3758 result = intern ("direct-color");
3759 break;
3760 default:
3761 error ("Display has an unknown visual class");
3762 result = Qnil;
3765 return result;
3768 DEFUN ("x-display-save-under", Fx_display_save_under,
3769 Sx_display_save_under, 0, 1, 0,
3770 doc: /* Returns t if the X display DISPLAY supports the save-under feature.
3771 The optional argument DISPLAY specifies which display to ask about.
3772 DISPLAY should be either a frame or a display name (a string).
3773 If omitted or nil, that stands for the selected frame's display. */)
3774 (display)
3775 Lisp_Object display;
3777 struct x_display_info *dpyinfo = check_x_display_info (display);
3779 if (DoesSaveUnders (dpyinfo->screen) == True)
3780 return Qt;
3781 else
3782 return Qnil;
3786 x_pixel_width (f)
3787 register struct frame *f;
3789 return FRAME_PIXEL_WIDTH (f);
3793 x_pixel_height (f)
3794 register struct frame *f;
3796 return FRAME_PIXEL_HEIGHT (f);
3800 x_char_width (f)
3801 register struct frame *f;
3803 return FRAME_COLUMN_WIDTH (f);
3807 x_char_height (f)
3808 register struct frame *f;
3810 return FRAME_LINE_HEIGHT (f);
3814 x_screen_planes (f)
3815 register struct frame *f;
3817 return FRAME_X_DISPLAY_INFO (f)->n_planes;
3822 /************************************************************************
3823 X Displays
3824 ************************************************************************/
3827 /* Mapping visual names to visuals. */
3829 static struct visual_class
3831 char *name;
3832 int class;
3834 visual_classes[] =
3836 {"StaticGray", StaticGray},
3837 {"GrayScale", GrayScale},
3838 {"StaticColor", StaticColor},
3839 {"PseudoColor", PseudoColor},
3840 {"TrueColor", TrueColor},
3841 {"DirectColor", DirectColor},
3842 {NULL, 0}
3846 #ifndef HAVE_XSCREENNUMBEROFSCREEN
3848 /* Value is the screen number of screen SCR. This is a substitute for
3849 the X function with the same name when that doesn't exist. */
3852 XScreenNumberOfScreen (scr)
3853 register Screen *scr;
3855 Display *dpy = scr->display;
3856 int i;
3858 for (i = 0; i < dpy->nscreens; ++i)
3859 if (scr == dpy->screens + i)
3860 break;
3862 return i;
3865 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
3868 /* Select the visual that should be used on display DPYINFO. Set
3869 members of DPYINFO appropriately. Called from x_term_init. */
3871 void
3872 select_visual (dpyinfo)
3873 struct x_display_info *dpyinfo;
3875 Display *dpy = dpyinfo->display;
3876 Screen *screen = dpyinfo->screen;
3877 Lisp_Object value;
3879 /* See if a visual is specified. */
3880 value = display_x_get_resource (dpyinfo,
3881 build_string ("visualClass"),
3882 build_string ("VisualClass"),
3883 Qnil, Qnil);
3884 if (STRINGP (value))
3886 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
3887 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
3888 depth, a decimal number. NAME is compared with case ignored. */
3889 char *s = (char *) alloca (SBYTES (value) + 1);
3890 char *dash;
3891 int i, class = -1;
3892 XVisualInfo vinfo;
3894 strcpy (s, SDATA (value));
3895 dash = index (s, '-');
3896 if (dash)
3898 dpyinfo->n_planes = atoi (dash + 1);
3899 *dash = '\0';
3901 else
3902 /* We won't find a matching visual with depth 0, so that
3903 an error will be printed below. */
3904 dpyinfo->n_planes = 0;
3906 /* Determine the visual class. */
3907 for (i = 0; visual_classes[i].name; ++i)
3908 if (xstricmp (s, visual_classes[i].name) == 0)
3910 class = visual_classes[i].class;
3911 break;
3914 /* Look up a matching visual for the specified class. */
3915 if (class == -1
3916 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
3917 dpyinfo->n_planes, class, &vinfo))
3918 fatal ("Invalid visual specification `%s'", SDATA (value));
3920 dpyinfo->visual = vinfo.visual;
3922 else
3924 int n_visuals;
3925 XVisualInfo *vinfo, vinfo_template;
3927 dpyinfo->visual = DefaultVisualOfScreen (screen);
3929 #ifdef HAVE_X11R4
3930 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
3931 #else
3932 vinfo_template.visualid = dpyinfo->visual->visualid;
3933 #endif
3934 vinfo_template.screen = XScreenNumberOfScreen (screen);
3935 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
3936 &vinfo_template, &n_visuals);
3937 if (n_visuals != 1)
3938 fatal ("Can't get proper X visual info");
3940 dpyinfo->n_planes = vinfo->depth;
3941 XFree ((char *) vinfo);
3946 /* Return the X display structure for the display named NAME.
3947 Open a new connection if necessary. */
3949 struct x_display_info *
3950 x_display_info_for_name (name)
3951 Lisp_Object name;
3953 Lisp_Object names;
3954 struct x_display_info *dpyinfo;
3956 CHECK_STRING (name);
3958 if (! EQ (Vwindow_system, intern ("x")))
3959 error ("Not using X Windows");
3961 for (dpyinfo = x_display_list, names = x_display_name_list;
3962 dpyinfo;
3963 dpyinfo = dpyinfo->next, names = XCDR (names))
3965 Lisp_Object tem;
3966 tem = Fstring_equal (XCAR (XCAR (names)), name);
3967 if (!NILP (tem))
3968 return dpyinfo;
3971 /* Use this general default value to start with. */
3972 Vx_resource_name = Vinvocation_name;
3974 validate_x_resource_name ();
3976 dpyinfo = x_term_init (name, (char *)0,
3977 (char *) SDATA (Vx_resource_name));
3979 if (dpyinfo == 0)
3980 error ("Cannot connect to X server %s", SDATA (name));
3982 x_in_use = 1;
3983 XSETFASTINT (Vwindow_system_version, 11);
3985 return dpyinfo;
3989 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
3990 1, 3, 0,
3991 doc: /* Open a connection to an X server.
3992 DISPLAY is the name of the display to connect to.
3993 Optional second arg XRM-STRING is a string of resources in xrdb format.
3994 If the optional third arg MUST-SUCCEED is non-nil,
3995 terminate Emacs if we can't open the connection. */)
3996 (display, xrm_string, must_succeed)
3997 Lisp_Object display, xrm_string, must_succeed;
3999 unsigned char *xrm_option;
4000 struct x_display_info *dpyinfo;
4002 CHECK_STRING (display);
4003 if (! NILP (xrm_string))
4004 CHECK_STRING (xrm_string);
4006 if (! EQ (Vwindow_system, intern ("x")))
4007 error ("Not using X Windows");
4009 if (! NILP (xrm_string))
4010 xrm_option = (unsigned char *) SDATA (xrm_string);
4011 else
4012 xrm_option = (unsigned char *) 0;
4014 validate_x_resource_name ();
4016 /* This is what opens the connection and sets x_current_display.
4017 This also initializes many symbols, such as those used for input. */
4018 dpyinfo = x_term_init (display, xrm_option,
4019 (char *) SDATA (Vx_resource_name));
4021 if (dpyinfo == 0)
4023 if (!NILP (must_succeed))
4024 fatal ("Cannot connect to X server %s.\n\
4025 Check the DISPLAY environment variable or use `-d'.\n\
4026 Also use the `xauth' program to verify that you have the proper\n\
4027 authorization information needed to connect the X server.\n\
4028 An insecure way to solve the problem may be to use `xhost'.\n",
4029 SDATA (display));
4030 else
4031 error ("Cannot connect to X server %s", SDATA (display));
4034 x_in_use = 1;
4036 XSETFASTINT (Vwindow_system_version, 11);
4037 return Qnil;
4040 DEFUN ("x-close-connection", Fx_close_connection,
4041 Sx_close_connection, 1, 1, 0,
4042 doc: /* Close the connection to DISPLAY's X server.
4043 For DISPLAY, specify either a frame or a display name (a string).
4044 If DISPLAY is nil, that stands for the selected frame's display. */)
4045 (display)
4046 Lisp_Object display;
4048 struct x_display_info *dpyinfo = check_x_display_info (display);
4049 int i;
4051 if (dpyinfo->reference_count > 0)
4052 error ("Display still has frames on it");
4054 BLOCK_INPUT;
4055 /* Free the fonts in the font table. */
4056 for (i = 0; i < dpyinfo->n_fonts; i++)
4057 if (dpyinfo->font_table[i].name)
4059 XFreeFont (dpyinfo->display, dpyinfo->font_table[i].font);
4062 x_destroy_all_bitmaps (dpyinfo);
4063 XSetCloseDownMode (dpyinfo->display, DestroyAll);
4065 #ifdef USE_X_TOOLKIT
4066 XtCloseDisplay (dpyinfo->display);
4067 #else
4068 XCloseDisplay (dpyinfo->display);
4069 #endif
4071 x_delete_display (dpyinfo);
4072 UNBLOCK_INPUT;
4074 return Qnil;
4077 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4078 doc: /* Return the list of display names that Emacs has connections to. */)
4081 Lisp_Object tail, result;
4083 result = Qnil;
4084 for (tail = x_display_name_list; ! NILP (tail); tail = XCDR (tail))
4085 result = Fcons (XCAR (XCAR (tail)), result);
4087 return result;
4090 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4091 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4092 If ON is nil, allow buffering of requests.
4093 Turning on synchronization prohibits the Xlib routines from buffering
4094 requests and seriously degrades performance, but makes debugging much
4095 easier.
4096 The optional second argument DISPLAY specifies which display to act on.
4097 DISPLAY should be either a frame or a display name (a string).
4098 If DISPLAY is omitted or nil, that stands for the selected frame's display. */)
4099 (on, display)
4100 Lisp_Object display, on;
4102 struct x_display_info *dpyinfo = check_x_display_info (display);
4104 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4106 return Qnil;
4109 /* Wait for responses to all X commands issued so far for frame F. */
4111 void
4112 x_sync (f)
4113 FRAME_PTR f;
4115 BLOCK_INPUT;
4116 XSync (FRAME_X_DISPLAY (f), False);
4117 UNBLOCK_INPUT;
4121 /***********************************************************************
4122 Window properties
4123 ***********************************************************************/
4125 DEFUN ("x-change-window-property", Fx_change_window_property,
4126 Sx_change_window_property, 2, 6, 0,
4127 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4128 PROP must be a string.
4129 VALUE may be a string or a list of conses, numbers and/or strings.
4130 If an element in the list is a string, it is converted to
4131 an Atom and the value of the Atom is used. If an element is a cons,
4132 it is converted to a 32 bit number where the car is the 16 top bits and the
4133 cdr is the lower 16 bits.
4134 FRAME nil or omitted means use the selected frame.
4135 If TYPE is given and non-nil, it is the name of the type of VALUE.
4136 If TYPE is not given or nil, the type is STRING.
4137 FORMAT gives the size in bits of each element if VALUE is a list.
4138 It must be one of 8, 16 or 32.
4139 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4140 If OUTER_P is non-nil, the property is changed for the outer X window of
4141 FRAME. Default is to change on the edit X window.
4143 Value is VALUE. */)
4144 (prop, value, frame, type, format, outer_p)
4145 Lisp_Object prop, value, frame, type, format, outer_p;
4147 struct frame *f = check_x_frame (frame);
4148 Atom prop_atom;
4149 Atom target_type = XA_STRING;
4150 int element_format = 8;
4151 unsigned char *data;
4152 int nelements;
4153 Window w;
4155 CHECK_STRING (prop);
4157 if (! NILP (format))
4159 CHECK_NUMBER (format);
4160 element_format = XFASTINT (format);
4162 if (element_format != 8 && element_format != 16
4163 && element_format != 32)
4164 error ("FORMAT must be one of 8, 16 or 32");
4167 if (CONSP (value))
4169 nelements = x_check_property_data (value);
4170 if (nelements == -1)
4171 error ("Bad data in VALUE, must be number, string or cons");
4173 if (element_format == 8)
4174 data = (unsigned char *) xmalloc (nelements);
4175 else if (element_format == 16)
4176 data = (unsigned char *) xmalloc (nelements*2);
4177 else /* format == 32 */
4178 /* The man page for XChangeProperty:
4179 "If the specified format is 32, the property data must be a
4180 long array."
4181 This applies even if long is more than 64 bits. The X library
4182 converts to 32 bits before sending to the X server. */
4183 data = (unsigned char *) xmalloc (nelements * sizeof(long));
4185 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4187 else
4189 CHECK_STRING (value);
4190 data = SDATA (value);
4191 nelements = SCHARS (value);
4194 BLOCK_INPUT;
4195 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4196 if (! NILP (type))
4198 CHECK_STRING (type);
4199 target_type = XInternAtom (FRAME_X_DISPLAY (f), SDATA (type), False);
4202 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4203 else w = FRAME_X_WINDOW (f);
4205 XChangeProperty (FRAME_X_DISPLAY (f), w,
4206 prop_atom, target_type, element_format, PropModeReplace,
4207 data, nelements);
4209 if (CONSP (value)) xfree (data);
4211 /* Make sure the property is set when we return. */
4212 XFlush (FRAME_X_DISPLAY (f));
4213 UNBLOCK_INPUT;
4215 return value;
4219 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4220 Sx_delete_window_property, 1, 2, 0,
4221 doc: /* Remove window property PROP from X window of FRAME.
4222 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4223 (prop, frame)
4224 Lisp_Object prop, frame;
4226 struct frame *f = check_x_frame (frame);
4227 Atom prop_atom;
4229 CHECK_STRING (prop);
4230 BLOCK_INPUT;
4231 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4232 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4234 /* Make sure the property is removed when we return. */
4235 XFlush (FRAME_X_DISPLAY (f));
4236 UNBLOCK_INPUT;
4238 return prop;
4242 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4243 1, 6, 0,
4244 doc: /* Value is the value of window property PROP on FRAME.
4245 If FRAME is nil or omitted, use the selected frame.
4246 If TYPE is nil or omitted, get the property as a string. Otherwise TYPE
4247 is the name of the Atom that denotes the type expected.
4248 If SOURCE is non-nil, get the property on that window instead of from
4249 FRAME. The number 0 denotes the root window.
4250 If DELETE_P is non-nil, delete the property after retreiving it.
4251 If VECTOR_RET_P is non-nil, don't return a string but a vector of values.
4253 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4254 no value of TYPE. */)
4255 (prop, frame, type, source, delete_p, vector_ret_p)
4256 Lisp_Object prop, frame, type, source, delete_p, vector_ret_p;
4258 struct frame *f = check_x_frame (frame);
4259 Atom prop_atom;
4260 int rc;
4261 Lisp_Object prop_value = Qnil;
4262 unsigned char *tmp_data = NULL;
4263 Atom actual_type;
4264 Atom target_type = XA_STRING;
4265 int actual_format;
4266 unsigned long actual_size, bytes_remaining;
4267 Window target_window = FRAME_X_WINDOW (f);
4268 struct gcpro gcpro1;
4270 GCPRO1 (prop_value);
4271 CHECK_STRING (prop);
4273 if (! NILP (source))
4275 if (NUMBERP (source))
4277 if (FLOATP (source))
4278 target_window = (Window) XFLOAT (source);
4279 else
4280 target_window = XFASTINT (source);
4282 if (target_window == 0)
4283 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
4285 else if (CONSP (source))
4286 target_window = cons_to_long (source);
4289 BLOCK_INPUT;
4290 if (STRINGP (type))
4292 if (strcmp ("AnyPropertyType", SDATA (type)) == 0)
4293 target_type = AnyPropertyType;
4294 else
4295 target_type = XInternAtom (FRAME_X_DISPLAY (f), SDATA (type), False);
4298 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SDATA (prop), False);
4299 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4300 prop_atom, 0, 0, False, target_type,
4301 &actual_type, &actual_format, &actual_size,
4302 &bytes_remaining, &tmp_data);
4303 if (rc == Success)
4305 int size = bytes_remaining;
4307 XFree (tmp_data);
4308 tmp_data = NULL;
4310 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4311 prop_atom, 0, bytes_remaining,
4312 ! NILP (delete_p), target_type,
4313 &actual_type, &actual_format,
4314 &actual_size, &bytes_remaining,
4315 &tmp_data);
4316 if (rc == Success && tmp_data)
4318 /* The man page for XGetWindowProperty says:
4319 "If the returned format is 32, the returned data is represented
4320 as a long array and should be cast to that type to obtain the
4321 elements."
4322 This applies even if long is more than 32 bits, the X library
4323 converts from 32 bit elements received from the X server to long
4324 and passes the long array to us. Thus, for that case bcopy can not
4325 be used. We convert to a 32 bit type here, because so much code
4326 assume on that.
4328 The bytes and offsets passed to XGetWindowProperty refers to the
4329 property and those are indeed in 32 bit quantities if format is
4330 32. */
4332 if (actual_format == 32 && actual_format < BITS_PER_LONG)
4334 unsigned long i;
4335 int *idata = (int *) tmp_data;
4336 long *ldata = (long *) tmp_data;
4338 for (i = 0; i < actual_size; ++i)
4339 idata[i] = (int) ldata[i];
4342 if (NILP (vector_ret_p))
4343 prop_value = make_string (tmp_data, size);
4344 else
4345 prop_value = x_property_data_to_lisp (f,
4346 tmp_data,
4347 actual_type,
4348 actual_format,
4349 actual_size);
4352 if (tmp_data) XFree (tmp_data);
4355 UNBLOCK_INPUT;
4356 UNGCPRO;
4357 return prop_value;
4362 /***********************************************************************
4363 Busy cursor
4364 ***********************************************************************/
4366 /* If non-null, an asynchronous timer that, when it expires, displays
4367 an hourglass cursor on all frames. */
4369 static struct atimer *hourglass_atimer;
4371 /* Non-zero means an hourglass cursor is currently shown. */
4373 static int hourglass_shown_p;
4375 /* Number of seconds to wait before displaying an hourglass cursor. */
4377 static Lisp_Object Vhourglass_delay;
4379 /* Default number of seconds to wait before displaying an hourglass
4380 cursor. */
4382 #define DEFAULT_HOURGLASS_DELAY 1
4384 /* Function prototypes. */
4386 static void show_hourglass P_ ((struct atimer *));
4387 static void hide_hourglass P_ ((void));
4389 /* Return non-zero if houglass timer has been started or hourglass is shown. */
4392 hourglass_started ()
4394 return hourglass_shown_p || hourglass_atimer != NULL;
4398 /* Cancel a currently active hourglass timer, and start a new one. */
4400 void
4401 start_hourglass ()
4403 EMACS_TIME delay;
4404 int secs, usecs = 0;
4406 /* Don't bother for ttys. */
4407 if (NILP (Vwindow_system))
4408 return;
4410 cancel_hourglass ();
4412 if (INTEGERP (Vhourglass_delay)
4413 && XINT (Vhourglass_delay) > 0)
4414 secs = XFASTINT (Vhourglass_delay);
4415 else if (FLOATP (Vhourglass_delay)
4416 && XFLOAT_DATA (Vhourglass_delay) > 0)
4418 Lisp_Object tem;
4419 tem = Ftruncate (Vhourglass_delay, Qnil);
4420 secs = XFASTINT (tem);
4421 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
4423 else
4424 secs = DEFAULT_HOURGLASS_DELAY;
4426 EMACS_SET_SECS_USECS (delay, secs, usecs);
4427 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
4428 show_hourglass, NULL);
4432 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
4433 shown. */
4435 void
4436 cancel_hourglass ()
4438 if (hourglass_atimer)
4440 cancel_atimer (hourglass_atimer);
4441 hourglass_atimer = NULL;
4444 if (hourglass_shown_p)
4445 hide_hourglass ();
4449 /* Timer function of hourglass_atimer. TIMER is equal to
4450 hourglass_atimer.
4452 Display an hourglass pointer on all frames by mapping the frames'
4453 hourglass_window. Set the hourglass_p flag in the frames'
4454 output_data.x structure to indicate that an hourglass cursor is
4455 shown on the frames. */
4457 static void
4458 show_hourglass (timer)
4459 struct atimer *timer;
4461 /* The timer implementation will cancel this timer automatically
4462 after this function has run. Set hourglass_atimer to null
4463 so that we know the timer doesn't have to be canceled. */
4464 hourglass_atimer = NULL;
4466 if (!hourglass_shown_p)
4468 Lisp_Object rest, frame;
4470 BLOCK_INPUT;
4472 FOR_EACH_FRAME (rest, frame)
4474 struct frame *f = XFRAME (frame);
4476 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4478 Display *dpy = FRAME_X_DISPLAY (f);
4480 #ifdef USE_X_TOOLKIT
4481 if (f->output_data.x->widget)
4482 #else
4483 if (FRAME_OUTER_WINDOW (f))
4484 #endif
4486 f->output_data.x->hourglass_p = 1;
4488 if (!f->output_data.x->hourglass_window)
4490 unsigned long mask = CWCursor;
4491 XSetWindowAttributes attrs;
4492 #ifdef USE_GTK
4493 Window parent = FRAME_X_WINDOW (f);
4494 #else
4495 Window parent = FRAME_OUTER_WINDOW (f);
4496 #endif
4497 attrs.cursor = f->output_data.x->hourglass_cursor;
4499 f->output_data.x->hourglass_window
4500 = XCreateWindow (dpy, parent,
4501 0, 0, 32000, 32000, 0, 0,
4502 InputOnly,
4503 CopyFromParent,
4504 mask, &attrs);
4507 XMapRaised (dpy, f->output_data.x->hourglass_window);
4508 XFlush (dpy);
4513 hourglass_shown_p = 1;
4514 UNBLOCK_INPUT;
4519 /* Hide the hourglass pointer on all frames, if it is currently
4520 shown. */
4522 static void
4523 hide_hourglass ()
4525 if (hourglass_shown_p)
4527 Lisp_Object rest, frame;
4529 BLOCK_INPUT;
4530 FOR_EACH_FRAME (rest, frame)
4532 struct frame *f = XFRAME (frame);
4534 if (FRAME_X_P (f)
4535 /* Watch out for newly created frames. */
4536 && f->output_data.x->hourglass_window)
4538 XUnmapWindow (FRAME_X_DISPLAY (f),
4539 f->output_data.x->hourglass_window);
4540 /* Sync here because XTread_socket looks at the
4541 hourglass_p flag that is reset to zero below. */
4542 XSync (FRAME_X_DISPLAY (f), False);
4543 f->output_data.x->hourglass_p = 0;
4547 hourglass_shown_p = 0;
4548 UNBLOCK_INPUT;
4554 /***********************************************************************
4555 Tool tips
4556 ***********************************************************************/
4558 static Lisp_Object x_create_tip_frame P_ ((struct x_display_info *,
4559 Lisp_Object, Lisp_Object));
4560 static void compute_tip_xy P_ ((struct frame *, Lisp_Object, Lisp_Object,
4561 Lisp_Object, int, int, int *, int *));
4563 /* The frame of a currently visible tooltip. */
4565 Lisp_Object tip_frame;
4567 /* If non-nil, a timer started that hides the last tooltip when it
4568 fires. */
4570 Lisp_Object tip_timer;
4571 Window tip_window;
4573 /* If non-nil, a vector of 3 elements containing the last args
4574 with which x-show-tip was called. See there. */
4576 Lisp_Object last_show_tip_args;
4578 /* Maximum size for tooltips; a cons (COLUMNS . ROWS). */
4580 Lisp_Object Vx_max_tooltip_size;
4583 static Lisp_Object
4584 unwind_create_tip_frame (frame)
4585 Lisp_Object frame;
4587 Lisp_Object deleted;
4589 deleted = unwind_create_frame (frame);
4590 if (EQ (deleted, Qt))
4592 tip_window = None;
4593 tip_frame = Qnil;
4596 return deleted;
4600 /* Create a frame for a tooltip on the display described by DPYINFO.
4601 PARMS is a list of frame parameters. TEXT is the string to
4602 display in the tip frame. Value is the frame.
4604 Note that functions called here, esp. x_default_parameter can
4605 signal errors, for instance when a specified color name is
4606 undefined. We have to make sure that we're in a consistent state
4607 when this happens. */
4609 static Lisp_Object
4610 x_create_tip_frame (dpyinfo, parms, text)
4611 struct x_display_info *dpyinfo;
4612 Lisp_Object parms, text;
4614 struct frame *f;
4615 Lisp_Object frame, tem;
4616 Lisp_Object name;
4617 long window_prompting = 0;
4618 int width, height;
4619 int count = SPECPDL_INDEX ();
4620 struct gcpro gcpro1, gcpro2, gcpro3;
4621 struct kboard *kb;
4622 int face_change_count_before = face_change_count;
4623 Lisp_Object buffer;
4624 struct buffer *old_buffer;
4626 check_x ();
4629 #ifdef MULTI_KBOARD
4630 kb = dpyinfo->kboard;
4631 #else
4632 kb = &the_only_kboard;
4633 #endif
4635 /* Get the name of the frame to use for resource lookup. */
4636 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4637 if (!STRINGP (name)
4638 && !EQ (name, Qunbound)
4639 && !NILP (name))
4640 error ("Invalid frame name--not a string or nil");
4642 frame = Qnil;
4643 GCPRO3 (parms, name, frame);
4644 f = make_frame (1);
4645 XSETFRAME (frame, f);
4647 buffer = Fget_buffer_create (build_string (" *tip*"));
4648 Fset_window_buffer (FRAME_ROOT_WINDOW (f), buffer, Qnil);
4649 old_buffer = current_buffer;
4650 set_buffer_internal_1 (XBUFFER (buffer));
4651 current_buffer->truncate_lines = Qnil;
4652 specbind (Qinhibit_read_only, Qt);
4653 specbind (Qinhibit_modification_hooks, Qt);
4654 Ferase_buffer ();
4655 Finsert (1, &text);
4656 set_buffer_internal_1 (old_buffer);
4658 FRAME_CAN_HAVE_SCROLL_BARS (f) = 0;
4659 record_unwind_protect (unwind_create_tip_frame, frame);
4661 /* By setting the output method, we're essentially saying that
4662 the frame is live, as per FRAME_LIVE_P. If we get a signal
4663 from this point on, x_destroy_window might screw up reference
4664 counts etc. */
4665 f->output_method = output_x_window;
4666 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
4667 bzero (f->output_data.x, sizeof (struct x_output));
4668 f->output_data.x->icon_bitmap = -1;
4669 FRAME_FONTSET (f) = -1;
4670 f->output_data.x->scroll_bar_foreground_pixel = -1;
4671 f->output_data.x->scroll_bar_background_pixel = -1;
4672 #ifdef USE_TOOLKIT_SCROLL_BARS
4673 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4674 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4675 #endif /* USE_TOOLKIT_SCROLL_BARS */
4676 f->icon_name = Qnil;
4677 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4678 #if GLYPH_DEBUG
4679 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
4680 dpyinfo_refcount = dpyinfo->reference_count;
4681 #endif /* GLYPH_DEBUG */
4682 #ifdef MULTI_KBOARD
4683 FRAME_KBOARD (f) = kb;
4684 #endif
4685 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4686 f->output_data.x->explicit_parent = 0;
4688 /* These colors will be set anyway later, but it's important
4689 to get the color reference counts right, so initialize them! */
4691 Lisp_Object black;
4692 struct gcpro gcpro1;
4694 black = build_string ("black");
4695 GCPRO1 (black);
4696 f->output_data.x->foreground_pixel
4697 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4698 f->output_data.x->background_pixel
4699 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4700 f->output_data.x->cursor_pixel
4701 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4702 f->output_data.x->cursor_foreground_pixel
4703 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4704 f->output_data.x->border_pixel
4705 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4706 f->output_data.x->mouse_pixel
4707 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4708 UNGCPRO;
4711 /* Set the name; the functions to which we pass f expect the name to
4712 be set. */
4713 if (EQ (name, Qunbound) || NILP (name))
4715 f->name = build_string (dpyinfo->x_id_name);
4716 f->explicit_name = 0;
4718 else
4720 f->name = name;
4721 f->explicit_name = 1;
4722 /* use the frame's title when getting resources for this frame. */
4723 specbind (Qx_resource_name, name);
4726 /* Extract the window parameters from the supplied values that are
4727 needed to determine window geometry. */
4729 Lisp_Object font;
4731 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
4733 BLOCK_INPUT;
4734 /* First, try whatever font the caller has specified. */
4735 if (STRINGP (font))
4737 tem = Fquery_fontset (font, Qnil);
4738 if (STRINGP (tem))
4739 font = x_new_fontset (f, SDATA (tem));
4740 else
4741 font = x_new_font (f, SDATA (font));
4744 /* Try out a font which we hope has bold and italic variations. */
4745 if (!STRINGP (font))
4746 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
4747 if (!STRINGP (font))
4748 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4749 if (! STRINGP (font))
4750 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
4751 if (! STRINGP (font))
4752 /* This was formerly the first thing tried, but it finds too many fonts
4753 and takes too long. */
4754 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
4755 /* If those didn't work, look for something which will at least work. */
4756 if (! STRINGP (font))
4757 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
4758 UNBLOCK_INPUT;
4759 if (! STRINGP (font))
4760 font = build_string ("fixed");
4762 x_default_parameter (f, parms, Qfont, font,
4763 "font", "Font", RES_TYPE_STRING);
4766 x_default_parameter (f, parms, Qborder_width, make_number (2),
4767 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4769 /* This defaults to 2 in order to match xterm. We recognize either
4770 internalBorderWidth or internalBorder (which is what xterm calls
4771 it). */
4772 if (NILP (Fassq (Qinternal_border_width, parms)))
4774 Lisp_Object value;
4776 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4777 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4778 if (! EQ (value, Qunbound))
4779 parms = Fcons (Fcons (Qinternal_border_width, value),
4780 parms);
4783 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4784 "internalBorderWidth", "internalBorderWidth",
4785 RES_TYPE_NUMBER);
4787 /* Also do the stuff which must be set before the window exists. */
4788 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4789 "foreground", "Foreground", RES_TYPE_STRING);
4790 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4791 "background", "Background", RES_TYPE_STRING);
4792 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4793 "pointerColor", "Foreground", RES_TYPE_STRING);
4794 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4795 "cursorColor", "Foreground", RES_TYPE_STRING);
4796 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4797 "borderColor", "BorderColor", RES_TYPE_STRING);
4799 /* Init faces before x_default_parameter is called for scroll-bar
4800 parameters because that function calls x_set_scroll_bar_width,
4801 which calls change_frame_size, which calls Fset_window_buffer,
4802 which runs hooks, which call Fvertical_motion. At the end, we
4803 end up in init_iterator with a null face cache, which should not
4804 happen. */
4805 init_frame_faces (f);
4807 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4809 window_prompting = x_figure_window_size (f, parms, 0);
4812 XSetWindowAttributes attrs;
4813 unsigned long mask;
4815 BLOCK_INPUT;
4816 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4817 if (DoesSaveUnders (dpyinfo->screen))
4818 mask |= CWSaveUnder;
4820 /* Window managers look at the override-redirect flag to determine
4821 whether or net to give windows a decoration (Xlib spec, chapter
4822 3.2.8). */
4823 attrs.override_redirect = True;
4824 attrs.save_under = True;
4825 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4826 /* Arrange for getting MapNotify and UnmapNotify events. */
4827 attrs.event_mask = StructureNotifyMask;
4828 tip_window
4829 = FRAME_X_WINDOW (f)
4830 = XCreateWindow (FRAME_X_DISPLAY (f),
4831 FRAME_X_DISPLAY_INFO (f)->root_window,
4832 /* x, y, width, height */
4833 0, 0, 1, 1,
4834 /* Border. */
4836 CopyFromParent, InputOutput, CopyFromParent,
4837 mask, &attrs);
4838 UNBLOCK_INPUT;
4841 x_make_gc (f);
4843 x_default_parameter (f, parms, Qauto_raise, Qnil,
4844 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4845 x_default_parameter (f, parms, Qauto_lower, Qnil,
4846 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4847 x_default_parameter (f, parms, Qcursor_type, Qbox,
4848 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4850 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4851 Change will not be effected unless different from the current
4852 FRAME_LINES (f). */
4853 width = FRAME_COLS (f);
4854 height = FRAME_LINES (f);
4855 SET_FRAME_COLS (f, 0);
4856 FRAME_LINES (f) = 0;
4857 change_frame_size (f, height, width, 1, 0, 0);
4859 /* Add `tooltip' frame parameter's default value. */
4860 if (NILP (Fframe_parameter (frame, intern ("tooltip"))))
4861 Fmodify_frame_parameters (frame, Fcons (Fcons (intern ("tooltip"), Qt),
4862 Qnil));
4864 /* Set up faces after all frame parameters are known. This call
4865 also merges in face attributes specified for new frames.
4867 Frame parameters may be changed if .Xdefaults contains
4868 specifications for the default font. For example, if there is an
4869 `Emacs.default.attributeBackground: pink', the `background-color'
4870 attribute of the frame get's set, which let's the internal border
4871 of the tooltip frame appear in pink. Prevent this. */
4873 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
4875 /* Set tip_frame here, so that */
4876 tip_frame = frame;
4877 call1 (Qface_set_after_frame_default, frame);
4879 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
4880 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
4881 Qnil));
4884 f->no_split = 1;
4886 UNGCPRO;
4888 /* It is now ok to make the frame official even if we get an error
4889 below. And the frame needs to be on Vframe_list or making it
4890 visible won't work. */
4891 Vframe_list = Fcons (frame, Vframe_list);
4893 /* Now that the frame is official, it counts as a reference to
4894 its display. */
4895 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4897 /* Setting attributes of faces of the tooltip frame from resources
4898 and similar will increment face_change_count, which leads to the
4899 clearing of all current matrices. Since this isn't necessary
4900 here, avoid it by resetting face_change_count to the value it
4901 had before we created the tip frame. */
4902 face_change_count = face_change_count_before;
4904 /* Discard the unwind_protect. */
4905 return unbind_to (count, frame);
4909 /* Compute where to display tip frame F. PARMS is the list of frame
4910 parameters for F. DX and DY are specified offsets from the current
4911 location of the mouse. WIDTH and HEIGHT are the width and height
4912 of the tooltip. Return coordinates relative to the root window of
4913 the display in *ROOT_X, and *ROOT_Y. */
4915 static void
4916 compute_tip_xy (f, parms, dx, dy, width, height, root_x, root_y)
4917 struct frame *f;
4918 Lisp_Object parms, dx, dy;
4919 int width, height;
4920 int *root_x, *root_y;
4922 Lisp_Object left, top;
4923 int win_x, win_y;
4924 Window root, child;
4925 unsigned pmask;
4927 /* User-specified position? */
4928 left = Fcdr (Fassq (Qleft, parms));
4929 top = Fcdr (Fassq (Qtop, parms));
4931 /* Move the tooltip window where the mouse pointer is. Resize and
4932 show it. */
4933 if (!INTEGERP (left) || !INTEGERP (top))
4935 BLOCK_INPUT;
4936 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
4937 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
4938 UNBLOCK_INPUT;
4941 if (INTEGERP (top))
4942 *root_y = XINT (top);
4943 else if (*root_y + XINT (dy) - height < 0)
4944 *root_y -= XINT (dy);
4945 else
4947 *root_y -= height;
4948 *root_y += XINT (dy);
4951 if (INTEGERP (left))
4952 *root_x = XINT (left);
4953 else if (*root_x + XINT (dx) + width <= FRAME_X_DISPLAY_INFO (f)->width)
4954 /* It fits to the right of the pointer. */
4955 *root_x += XINT (dx);
4956 else if (width + XINT (dx) <= *root_x)
4957 /* It fits to the left of the pointer. */
4958 *root_x -= width + XINT (dx);
4959 else
4960 /* Put it left-justified on the screen--it ought to fit that way. */
4961 *root_x = 0;
4965 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
4966 doc: /* Show STRING in a "tooltip" window on frame FRAME.
4967 A tooltip window is a small X window displaying a string.
4969 FRAME nil or omitted means use the selected frame.
4971 PARMS is an optional list of frame parameters which can be used to
4972 change the tooltip's appearance.
4974 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
4975 means use the default timeout of 5 seconds.
4977 If the list of frame parameters PARAMS contains a `left' parameters,
4978 the tooltip is displayed at that x-position. Otherwise it is
4979 displayed at the mouse position, with offset DX added (default is 5 if
4980 DX isn't specified). Likewise for the y-position; if a `top' frame
4981 parameter is specified, it determines the y-position of the tooltip
4982 window, otherwise it is displayed at the mouse position, with offset
4983 DY added (default is -10).
4985 A tooltip's maximum size is specified by `x-max-tooltip-size'.
4986 Text larger than the specified size is clipped. */)
4987 (string, frame, parms, timeout, dx, dy)
4988 Lisp_Object string, frame, parms, timeout, dx, dy;
4990 struct frame *f;
4991 struct window *w;
4992 int root_x, root_y;
4993 struct buffer *old_buffer;
4994 struct text_pos pos;
4995 int i, width, height;
4996 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4997 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4998 int count = SPECPDL_INDEX ();
5000 specbind (Qinhibit_redisplay, Qt);
5002 GCPRO4 (string, parms, frame, timeout);
5004 CHECK_STRING (string);
5005 f = check_x_frame (frame);
5006 if (NILP (timeout))
5007 timeout = make_number (5);
5008 else
5009 CHECK_NATNUM (timeout);
5011 if (NILP (dx))
5012 dx = make_number (5);
5013 else
5014 CHECK_NUMBER (dx);
5016 if (NILP (dy))
5017 dy = make_number (-10);
5018 else
5019 CHECK_NUMBER (dy);
5021 if (NILP (last_show_tip_args))
5022 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5024 if (!NILP (tip_frame))
5026 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5027 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5028 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5030 if (EQ (frame, last_frame)
5031 && !NILP (Fequal (last_string, string))
5032 && !NILP (Fequal (last_parms, parms)))
5034 struct frame *f = XFRAME (tip_frame);
5036 /* Only DX and DY have changed. */
5037 if (!NILP (tip_timer))
5039 Lisp_Object timer = tip_timer;
5040 tip_timer = Qnil;
5041 call1 (Qcancel_timer, timer);
5044 BLOCK_INPUT;
5045 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5046 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5047 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5048 root_x, root_y);
5049 UNBLOCK_INPUT;
5050 goto start_timer;
5054 /* Hide a previous tip, if any. */
5055 Fx_hide_tip ();
5057 ASET (last_show_tip_args, 0, string);
5058 ASET (last_show_tip_args, 1, frame);
5059 ASET (last_show_tip_args, 2, parms);
5061 /* Add default values to frame parameters. */
5062 if (NILP (Fassq (Qname, parms)))
5063 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5064 if (NILP (Fassq (Qinternal_border_width, parms)))
5065 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5066 if (NILP (Fassq (Qborder_width, parms)))
5067 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5068 if (NILP (Fassq (Qborder_color, parms)))
5069 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5070 if (NILP (Fassq (Qbackground_color, parms)))
5071 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5072 parms);
5074 /* Create a frame for the tooltip, and record it in the global
5075 variable tip_frame. */
5076 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
5077 f = XFRAME (frame);
5079 /* Set up the frame's root window. */
5080 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5081 w->left_col = w->top_line = make_number (0);
5083 if (CONSP (Vx_max_tooltip_size)
5084 && INTEGERP (XCAR (Vx_max_tooltip_size))
5085 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5086 && INTEGERP (XCDR (Vx_max_tooltip_size))
5087 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5089 w->total_cols = XCAR (Vx_max_tooltip_size);
5090 w->total_lines = XCDR (Vx_max_tooltip_size);
5092 else
5094 w->total_cols = make_number (80);
5095 w->total_lines = make_number (40);
5098 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5099 adjust_glyphs (f);
5100 w->pseudo_window_p = 1;
5102 /* Display the tooltip text in a temporary buffer. */
5103 old_buffer = current_buffer;
5104 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5105 current_buffer->truncate_lines = Qnil;
5106 clear_glyph_matrix (w->desired_matrix);
5107 clear_glyph_matrix (w->current_matrix);
5108 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5109 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5111 /* Compute width and height of the tooltip. */
5112 width = height = 0;
5113 for (i = 0; i < w->desired_matrix->nrows; ++i)
5115 struct glyph_row *row = &w->desired_matrix->rows[i];
5116 struct glyph *last;
5117 int row_width;
5119 /* Stop at the first empty row at the end. */
5120 if (!row->enabled_p || !row->displays_text_p)
5121 break;
5123 /* Let the row go over the full width of the frame. */
5124 row->full_width_p = 1;
5126 /* There's a glyph at the end of rows that is used to place
5127 the cursor there. Don't include the width of this glyph. */
5128 if (row->used[TEXT_AREA])
5130 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5131 row_width = row->pixel_width - last->pixel_width;
5133 else
5134 row_width = row->pixel_width;
5136 height += row->height;
5137 width = max (width, row_width);
5140 /* Add the frame's internal border to the width and height the X
5141 window should have. */
5142 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5143 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5145 /* Move the tooltip window where the mouse pointer is. Resize and
5146 show it. */
5147 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5149 BLOCK_INPUT;
5150 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5151 root_x, root_y, width, height);
5152 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5153 UNBLOCK_INPUT;
5155 /* Draw into the window. */
5156 w->must_be_updated_p = 1;
5157 update_single_window (w, 1);
5159 /* Restore original current buffer. */
5160 set_buffer_internal_1 (old_buffer);
5161 windows_or_buffers_changed = old_windows_or_buffers_changed;
5163 start_timer:
5164 /* Let the tip disappear after timeout seconds. */
5165 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5166 intern ("x-hide-tip"));
5168 UNGCPRO;
5169 return unbind_to (count, Qnil);
5173 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5174 doc: /* Hide the current tooltip window, if there is any.
5175 Value is t if tooltip was open, nil otherwise. */)
5178 int count;
5179 Lisp_Object deleted, frame, timer;
5180 struct gcpro gcpro1, gcpro2;
5182 /* Return quickly if nothing to do. */
5183 if (NILP (tip_timer) && NILP (tip_frame))
5184 return Qnil;
5186 frame = tip_frame;
5187 timer = tip_timer;
5188 GCPRO2 (frame, timer);
5189 tip_frame = tip_timer = deleted = Qnil;
5191 count = SPECPDL_INDEX ();
5192 specbind (Qinhibit_redisplay, Qt);
5193 specbind (Qinhibit_quit, Qt);
5195 if (!NILP (timer))
5196 call1 (Qcancel_timer, timer);
5198 if (FRAMEP (frame))
5200 Fdelete_frame (frame, Qnil);
5201 deleted = Qt;
5203 #ifdef USE_LUCID
5204 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5205 redisplay procedure is not called when a tip frame over menu
5206 items is unmapped. Redisplay the menu manually... */
5208 struct frame *f = SELECTED_FRAME ();
5209 Widget w = f->output_data.x->menubar_widget;
5210 extern void xlwmenu_redisplay P_ ((Widget));
5212 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5213 && w != NULL)
5215 BLOCK_INPUT;
5216 xlwmenu_redisplay (w);
5217 UNBLOCK_INPUT;
5220 #endif /* USE_LUCID */
5223 UNGCPRO;
5224 return unbind_to (count, deleted);
5229 /***********************************************************************
5230 File selection dialog
5231 ***********************************************************************/
5233 #ifdef USE_MOTIF
5235 /* Callback for "OK" and "Cancel" on file selection dialog. */
5237 static void
5238 file_dialog_cb (widget, client_data, call_data)
5239 Widget widget;
5240 XtPointer call_data, client_data;
5242 int *result = (int *) client_data;
5243 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5244 *result = cb->reason;
5248 /* Callback for unmapping a file selection dialog. This is used to
5249 capture the case where a dialog is closed via a window manager's
5250 closer button, for example. Using a XmNdestroyCallback didn't work
5251 in this case. */
5253 static void
5254 file_dialog_unmap_cb (widget, client_data, call_data)
5255 Widget widget;
5256 XtPointer call_data, client_data;
5258 int *result = (int *) client_data;
5259 *result = XmCR_CANCEL;
5262 static Lisp_Object
5263 clean_up_file_dialog (arg)
5264 Lisp_Object arg;
5266 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
5267 Widget dialog = (Widget) p->pointer;
5269 /* Clean up. */
5270 BLOCK_INPUT;
5271 XtUnmanageChild (dialog);
5272 XtDestroyWidget (dialog);
5273 x_menu_set_in_use (0);
5274 UNBLOCK_INPUT;
5276 return Qnil;
5280 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5281 doc: /* Read file name, prompting with PROMPT in directory DIR.
5282 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5283 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5284 or directory must exist. ONLY-DIR-P is ignored." */)
5285 (prompt, dir, default_filename, mustmatch, only_dir_p)
5286 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
5288 int result;
5289 struct frame *f = SELECTED_FRAME ();
5290 Lisp_Object file = Qnil;
5291 Widget dialog, text, help;
5292 Arg al[10];
5293 int ac = 0;
5294 extern XtAppContext Xt_app_con;
5295 XmString dir_xmstring, pattern_xmstring;
5296 int count = SPECPDL_INDEX ();
5297 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5299 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5301 if (popup_activated ())
5302 error ("Trying to use a menu from within a menu-entry");
5304 CHECK_STRING (prompt);
5305 CHECK_STRING (dir);
5307 /* Prevent redisplay. */
5308 specbind (Qinhibit_redisplay, Qt);
5310 BLOCK_INPUT;
5312 /* Create the dialog with PROMPT as title, using DIR as initial
5313 directory and using "*" as pattern. */
5314 dir = Fexpand_file_name (dir, Qnil);
5315 dir_xmstring = XmStringCreateLocalized (SDATA (dir));
5316 pattern_xmstring = XmStringCreateLocalized ("*");
5318 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5319 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5320 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5321 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5322 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5323 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5324 "fsb", al, ac);
5325 XmStringFree (dir_xmstring);
5326 XmStringFree (pattern_xmstring);
5328 /* Add callbacks for OK and Cancel. */
5329 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5330 (XtPointer) &result);
5331 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5332 (XtPointer) &result);
5333 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5334 (XtPointer) &result);
5336 /* Remove the help button since we can't display help. */
5337 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5338 XtUnmanageChild (help);
5340 /* Mark OK button as default. */
5341 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5342 XmNshowAsDefault, True, NULL);
5344 /* If MUSTMATCH is non-nil, disable the file entry field of the
5345 dialog, so that the user must select a file from the files list
5346 box. We can't remove it because we wouldn't have a way to get at
5347 the result file name, then. */
5348 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5349 if (!NILP (mustmatch))
5351 Widget label;
5352 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5353 XtSetSensitive (text, False);
5354 XtSetSensitive (label, False);
5357 /* Manage the dialog, so that list boxes get filled. */
5358 XtManageChild (dialog);
5360 if (STRINGP (default_filename))
5362 XmString default_xmstring;
5363 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5364 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5366 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5367 XmTextFieldReplace (wtext, 0, last_pos,
5368 (SDATA (Ffile_name_nondirectory (default_filename))));
5370 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5371 must include the path for this to work. */
5373 default_xmstring = XmStringCreateLocalized (SDATA (default_filename));
5375 if (XmListItemExists (list, default_xmstring))
5377 int item_pos = XmListItemPos (list, default_xmstring);
5378 /* Select the item and scroll it into view. */
5379 XmListSelectPos (list, item_pos, True);
5380 XmListSetPos (list, item_pos);
5383 XmStringFree (default_xmstring);
5386 record_unwind_protect (clean_up_file_dialog, make_save_value (dialog, 0));
5388 /* Process events until the user presses Cancel or OK. */
5389 x_menu_set_in_use (1);
5390 result = 0;
5391 while (result == 0)
5393 XEvent event;
5394 x_menu_wait_for_event (0);
5395 XtAppNextEvent (Xt_app_con, &event);
5396 if (event.type == KeyPress
5397 && FRAME_X_DISPLAY (f) == event.xkey.display)
5399 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5401 /* Pop down on C-g. */
5402 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5403 XtUnmanageChild (dialog);
5406 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5409 /* Get the result. */
5410 if (result == XmCR_OK)
5412 XmString text;
5413 String data;
5415 XtVaGetValues (dialog, XmNtextString, &text, NULL);
5416 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
5417 XmStringFree (text);
5418 file = build_string (data);
5419 XtFree (data);
5421 else
5422 file = Qnil;
5424 UNBLOCK_INPUT;
5425 UNGCPRO;
5427 /* Make "Cancel" equivalent to C-g. */
5428 if (NILP (file))
5429 Fsignal (Qquit, Qnil);
5431 return unbind_to (count, file);
5434 #endif /* USE_MOTIF */
5436 #ifdef USE_GTK
5438 static Lisp_Object
5439 clean_up_dialog (arg)
5440 Lisp_Object arg;
5442 x_menu_set_in_use (0);
5444 return Qnil;
5447 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5448 doc: /* Read file name, prompting with PROMPT in directory DIR.
5449 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5450 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5451 or directory must exist. If ONLY-DIR-P is non-nil, the user can only select
5452 directories. */)
5453 (prompt, dir, default_filename, mustmatch, only_dir_p)
5454 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
5456 FRAME_PTR f = SELECTED_FRAME ();
5457 char *fn;
5458 Lisp_Object file = Qnil;
5459 int count = SPECPDL_INDEX ();
5460 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5461 char *cdef_file;
5463 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5465 if (popup_activated ())
5466 error ("Trying to use a menu from within a menu-entry");
5468 CHECK_STRING (prompt);
5469 CHECK_STRING (dir);
5471 /* Prevent redisplay. */
5472 specbind (Qinhibit_redisplay, Qt);
5473 record_unwind_protect (clean_up_dialog, Qnil);
5475 BLOCK_INPUT;
5477 if (STRINGP (default_filename))
5478 cdef_file = SDATA (default_filename);
5479 else
5480 cdef_file = SDATA (dir);
5482 fn = xg_get_file_name (f, SDATA (prompt), cdef_file,
5483 ! NILP (mustmatch),
5484 ! NILP (only_dir_p));
5486 if (fn)
5488 file = build_string (fn);
5489 xfree (fn);
5492 UNBLOCK_INPUT;
5493 UNGCPRO;
5495 /* Make "Cancel" equivalent to C-g. */
5496 if (NILP (file))
5497 Fsignal (Qquit, Qnil);
5499 return unbind_to (count, file);
5502 #endif /* USE_GTK */
5505 /***********************************************************************
5506 Keyboard
5507 ***********************************************************************/
5509 #ifdef HAVE_XKBGETKEYBOARD
5510 #include <X11/XKBlib.h>
5511 #include <X11/keysym.h>
5512 #endif
5514 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5515 Sx_backspace_delete_keys_p, 0, 1, 0,
5516 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5517 FRAME nil means use the selected frame.
5518 Value is t if we know that both keys are present, and are mapped to the
5519 usual X keysyms. */)
5520 (frame)
5521 Lisp_Object frame;
5523 #ifdef HAVE_XKBGETKEYBOARD
5524 XkbDescPtr kb;
5525 struct frame *f = check_x_frame (frame);
5526 Display *dpy = FRAME_X_DISPLAY (f);
5527 Lisp_Object have_keys;
5528 int major, minor, op, event, error;
5530 BLOCK_INPUT;
5532 /* Check library version in case we're dynamically linked. */
5533 major = XkbMajorVersion;
5534 minor = XkbMinorVersion;
5535 if (!XkbLibraryVersion (&major, &minor))
5537 UNBLOCK_INPUT;
5538 return Qnil;
5541 /* Check that the server supports XKB. */
5542 major = XkbMajorVersion;
5543 minor = XkbMinorVersion;
5544 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
5546 UNBLOCK_INPUT;
5547 return Qnil;
5550 /* In this code we check that the keyboard has physical keys with names
5551 that start with BKSP (Backspace) and DELE (Delete), and that they
5552 generate keysym XK_BackSpace and XK_Delete respectively.
5553 This function is used to test if normal-erase-is-backspace should be
5554 turned on.
5555 An alternative approach would be to just check if XK_BackSpace and
5556 XK_Delete are mapped to any key. But if any of those are mapped to
5557 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5558 user doesn't know about it, it is better to return false here.
5559 It is more obvious to the user what to do if she/he has two keys
5560 clearly marked with names/symbols and one key does something not
5561 expected (i.e. she/he then tries the other).
5562 The cases where Backspace/Delete is mapped to some other key combination
5563 are rare, and in those cases, normal-erase-is-backspace can be turned on
5564 manually. */
5566 have_keys = Qnil;
5567 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5568 if (kb)
5570 int delete_keycode = 0, backspace_keycode = 0, i;
5572 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5574 for (i = kb->min_key_code;
5575 (i < kb->max_key_code
5576 && (delete_keycode == 0 || backspace_keycode == 0));
5577 ++i)
5579 /* The XKB symbolic key names can be seen most easily in
5580 the PS file generated by `xkbprint -label name
5581 $DISPLAY'. */
5582 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5583 delete_keycode = i;
5584 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5585 backspace_keycode = i;
5588 XkbFreeNames (kb, 0, True);
5591 XkbFreeClientMap (kb, 0, True);
5593 if (delete_keycode
5594 && backspace_keycode
5595 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5596 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5597 have_keys = Qt;
5599 UNBLOCK_INPUT;
5600 return have_keys;
5601 #else /* not HAVE_XKBGETKEYBOARD */
5602 return Qnil;
5603 #endif /* not HAVE_XKBGETKEYBOARD */
5608 /***********************************************************************
5609 Initialization
5610 ***********************************************************************/
5612 /* Keep this list in the same order as frame_parms in frame.c.
5613 Use 0 for unsupported frame parameters. */
5615 frame_parm_handler x_frame_parm_handlers[] =
5617 x_set_autoraise,
5618 x_set_autolower,
5619 x_set_background_color,
5620 x_set_border_color,
5621 x_set_border_width,
5622 x_set_cursor_color,
5623 x_set_cursor_type,
5624 x_set_font,
5625 x_set_foreground_color,
5626 x_set_icon_name,
5627 x_set_icon_type,
5628 x_set_internal_border_width,
5629 x_set_menu_bar_lines,
5630 x_set_mouse_color,
5631 x_explicitly_set_name,
5632 x_set_scroll_bar_width,
5633 x_set_title,
5634 x_set_unsplittable,
5635 x_set_vertical_scroll_bars,
5636 x_set_visibility,
5637 x_set_tool_bar_lines,
5638 x_set_scroll_bar_foreground,
5639 x_set_scroll_bar_background,
5640 x_set_screen_gamma,
5641 x_set_line_spacing,
5642 x_set_fringe_width,
5643 x_set_fringe_width,
5644 x_set_wait_for_wm,
5645 x_set_fullscreen,
5648 void
5649 syms_of_xfns ()
5651 /* This is zero if not using X windows. */
5652 x_in_use = 0;
5654 /* The section below is built by the lisp expression at the top of the file,
5655 just above where these variables are declared. */
5656 /*&&& init symbols here &&&*/
5657 Qnone = intern ("none");
5658 staticpro (&Qnone);
5659 Qsuppress_icon = intern ("suppress-icon");
5660 staticpro (&Qsuppress_icon);
5661 Qundefined_color = intern ("undefined-color");
5662 staticpro (&Qundefined_color);
5663 Qcompound_text = intern ("compound-text");
5664 staticpro (&Qcompound_text);
5665 Qcancel_timer = intern ("cancel-timer");
5666 staticpro (&Qcancel_timer);
5667 /* This is the end of symbol initialization. */
5669 /* Text property `display' should be nonsticky by default. */
5670 Vtext_property_default_nonsticky
5671 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
5674 Fput (Qundefined_color, Qerror_conditions,
5675 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
5676 Fput (Qundefined_color, Qerror_message,
5677 build_string ("Undefined color"));
5679 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
5680 doc: /* The shape of the pointer when over text.
5681 Changing the value does not affect existing frames
5682 unless you set the mouse color. */);
5683 Vx_pointer_shape = Qnil;
5685 #if 0 /* This doesn't really do anything. */
5686 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
5687 doc: /* The shape of the pointer when not over text.
5688 This variable takes effect when you create a new frame
5689 or when you set the mouse color. */);
5690 #endif
5691 Vx_nontext_pointer_shape = Qnil;
5693 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
5694 doc: /* The shape of the pointer when Emacs is busy.
5695 This variable takes effect when you create a new frame
5696 or when you set the mouse color. */);
5697 Vx_hourglass_pointer_shape = Qnil;
5699 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
5700 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
5701 display_hourglass_p = 1;
5703 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
5704 doc: /* *Seconds to wait before displaying an hourglass pointer.
5705 Value must be an integer or float. */);
5706 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
5708 #if 0 /* This doesn't really do anything. */
5709 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
5710 doc: /* The shape of the pointer when over the mode line.
5711 This variable takes effect when you create a new frame
5712 or when you set the mouse color. */);
5713 #endif
5714 Vx_mode_pointer_shape = Qnil;
5716 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
5717 &Vx_sensitive_text_pointer_shape,
5718 doc: /* The shape of the pointer when over mouse-sensitive text.
5719 This variable takes effect when you create a new frame
5720 or when you set the mouse color. */);
5721 Vx_sensitive_text_pointer_shape = Qnil;
5723 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
5724 &Vx_window_horizontal_drag_shape,
5725 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
5726 This variable takes effect when you create a new frame
5727 or when you set the mouse color. */);
5728 Vx_window_horizontal_drag_shape = Qnil;
5730 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
5731 doc: /* A string indicating the foreground color of the cursor box. */);
5732 Vx_cursor_fore_pixel = Qnil;
5734 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
5735 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
5736 Text larger than this is clipped. */);
5737 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
5739 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
5740 doc: /* Non-nil if no X window manager is in use.
5741 Emacs doesn't try to figure this out; this is always nil
5742 unless you set it to something else. */);
5743 /* We don't have any way to find this out, so set it to nil
5744 and maybe the user would like to set it to t. */
5745 Vx_no_window_manager = Qnil;
5747 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
5748 &Vx_pixel_size_width_font_regexp,
5749 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
5751 Since Emacs gets width of a font matching with this regexp from
5752 PIXEL_SIZE field of the name, font finding mechanism gets faster for
5753 such a font. This is especially effective for such large fonts as
5754 Chinese, Japanese, and Korean. */);
5755 Vx_pixel_size_width_font_regexp = Qnil;
5757 /* This is not ifdef:ed, so other builds than GTK can customize it. */
5758 DEFVAR_BOOL ("x-use-old-gtk-file-dialog", &x_use_old_gtk_file_dialog,
5759 doc: /* *Non-nil means prompt with the old GTK file selection dialog.
5760 If nil or if the file selection dialog is not available, the new GTK file
5761 chooser is used instead. To turn off all file dialogs set the
5762 variable `use-file-dialog'. */);
5763 x_use_old_gtk_file_dialog = 0;
5765 Fprovide (intern ("x"), Qnil);
5767 #ifdef USE_X_TOOLKIT
5768 Fprovide (intern ("x-toolkit"), Qnil);
5769 #ifdef USE_MOTIF
5770 Fprovide (intern ("motif"), Qnil);
5772 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
5773 doc: /* Version info for LessTif/Motif. */);
5774 Vmotif_version_string = build_string (XmVERSION_STRING);
5775 #endif /* USE_MOTIF */
5776 #endif /* USE_X_TOOLKIT */
5778 #ifdef USE_GTK
5779 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
5780 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
5781 But for a user it is a toolkit for X, and indeed, configure
5782 accepts --with-x-toolkit=gtk. */
5783 Fprovide (intern ("x-toolkit"), Qnil);
5784 Fprovide (intern ("gtk"), Qnil);
5786 DEFVAR_LISP ("gtk-version-string", &Vgtk_version_string,
5787 doc: /* Version info for GTK+. */);
5789 char gtk_version[40];
5790 g_snprintf (gtk_version, sizeof (gtk_version), "%u.%u.%u",
5791 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
5792 Vgtk_version_string = build_string (gtk_version);
5794 #endif /* USE_GTK */
5796 /* X window properties. */
5797 defsubr (&Sx_change_window_property);
5798 defsubr (&Sx_delete_window_property);
5799 defsubr (&Sx_window_property);
5801 defsubr (&Sxw_display_color_p);
5802 defsubr (&Sx_display_grayscale_p);
5803 defsubr (&Sxw_color_defined_p);
5804 defsubr (&Sxw_color_values);
5805 defsubr (&Sx_server_max_request_size);
5806 defsubr (&Sx_server_vendor);
5807 defsubr (&Sx_server_version);
5808 defsubr (&Sx_display_pixel_width);
5809 defsubr (&Sx_display_pixel_height);
5810 defsubr (&Sx_display_mm_width);
5811 defsubr (&Sx_display_mm_height);
5812 defsubr (&Sx_display_screens);
5813 defsubr (&Sx_display_planes);
5814 defsubr (&Sx_display_color_cells);
5815 defsubr (&Sx_display_visual_class);
5816 defsubr (&Sx_display_backing_store);
5817 defsubr (&Sx_display_save_under);
5818 defsubr (&Sx_create_frame);
5819 defsubr (&Sx_open_connection);
5820 defsubr (&Sx_close_connection);
5821 defsubr (&Sx_display_list);
5822 defsubr (&Sx_synchronize);
5823 defsubr (&Sx_focus_frame);
5824 defsubr (&Sx_backspace_delete_keys_p);
5826 /* Setting callback functions for fontset handler. */
5827 get_font_info_func = x_get_font_info;
5829 #if 0 /* This function pointer doesn't seem to be used anywhere.
5830 And the pointer assigned has the wrong type, anyway. */
5831 list_fonts_func = x_list_fonts;
5832 #endif
5834 load_font_func = x_load_font;
5835 find_ccl_program_func = x_find_ccl_program;
5836 query_font_func = x_query_font;
5837 set_frame_fontset_func = x_set_font;
5838 check_window_system_func = check_x;
5840 hourglass_atimer = NULL;
5841 hourglass_shown_p = 0;
5843 defsubr (&Sx_show_tip);
5844 defsubr (&Sx_hide_tip);
5845 tip_timer = Qnil;
5846 staticpro (&tip_timer);
5847 tip_frame = Qnil;
5848 staticpro (&tip_frame);
5850 last_show_tip_args = Qnil;
5851 staticpro (&last_show_tip_args);
5853 #if defined (USE_MOTIF) || defined (USE_GTK)
5854 defsubr (&Sx_file_dialog);
5855 #endif
5858 #endif /* HAVE_X_WINDOWS */
5860 /* arch-tag: 55040d02-5485-4d58-8b22-95a7a05f3288
5861 (do not change this comment) */