(custom-add-parent-links): Filter out custom-group-link,
[emacs.git] / src / xfns.c
blobee4a7bf3b004d1eb0dbc42d2e43ef26f1c421841
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 unsigned 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 = (char *) SDATA (found);
798 BLOCK_INPUT;
800 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
802 if (pixbuf)
804 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
805 pixbuf);
806 g_object_unref (pixbuf);
808 result = 1;
810 else
811 g_error_free (err);
813 UNBLOCK_INPUT;
816 UNGCPRO;
817 return result;
821 xg_set_icon_from_xpm_data (f, data)
822 FRAME_PTR f;
823 char **data;
825 int result = 0;
826 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data ((const char **) data);
828 if (!pixbuf)
829 return 0;
831 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
832 g_object_unref (pixbuf);
833 return 1;
835 #endif /* USE_GTK */
838 /* Functions called only from `x_set_frame_param'
839 to set individual parameters.
841 If FRAME_X_WINDOW (f) is 0,
842 the frame is being created and its X-window does not exist yet.
843 In that case, just record the parameter's new value
844 in the standard place; do not attempt to change the window. */
846 void
847 x_set_foreground_color (f, arg, oldval)
848 struct frame *f;
849 Lisp_Object arg, oldval;
851 struct x_output *x = f->output_data.x;
852 unsigned long fg, old_fg;
854 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
855 old_fg = x->foreground_pixel;
856 x->foreground_pixel = fg;
858 if (FRAME_X_WINDOW (f) != 0)
860 Display *dpy = FRAME_X_DISPLAY (f);
862 BLOCK_INPUT;
863 XSetForeground (dpy, x->normal_gc, fg);
864 XSetBackground (dpy, x->reverse_gc, fg);
866 if (x->cursor_pixel == old_fg)
868 unload_color (f, x->cursor_pixel);
869 x->cursor_pixel = x_copy_color (f, fg);
870 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
873 UNBLOCK_INPUT;
875 update_face_from_frame_parameter (f, Qforeground_color, arg);
877 if (FRAME_VISIBLE_P (f))
878 redraw_frame (f);
881 unload_color (f, old_fg);
884 void
885 x_set_background_color (f, arg, oldval)
886 struct frame *f;
887 Lisp_Object arg, oldval;
889 struct x_output *x = f->output_data.x;
890 unsigned long bg;
892 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
893 unload_color (f, x->background_pixel);
894 x->background_pixel = bg;
896 if (FRAME_X_WINDOW (f) != 0)
898 Display *dpy = FRAME_X_DISPLAY (f);
900 BLOCK_INPUT;
901 XSetBackground (dpy, x->normal_gc, bg);
902 XSetForeground (dpy, x->reverse_gc, bg);
903 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
904 XSetForeground (dpy, x->cursor_gc, bg);
906 #ifdef USE_GTK
907 xg_set_background_color (f, bg);
908 #endif
910 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
911 toolkit scroll bars. */
913 Lisp_Object bar;
914 for (bar = FRAME_SCROLL_BARS (f);
915 !NILP (bar);
916 bar = XSCROLL_BAR (bar)->next)
918 Window window = SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar));
919 XSetWindowBackground (dpy, window, bg);
922 #endif /* USE_TOOLKIT_SCROLL_BARS */
924 UNBLOCK_INPUT;
925 update_face_from_frame_parameter (f, Qbackground_color, arg);
927 if (FRAME_VISIBLE_P (f))
928 redraw_frame (f);
932 void
933 x_set_mouse_color (f, arg, oldval)
934 struct frame *f;
935 Lisp_Object arg, oldval;
937 struct x_output *x = f->output_data.x;
938 Display *dpy = FRAME_X_DISPLAY (f);
939 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
940 Cursor hourglass_cursor, horizontal_drag_cursor;
941 int count;
942 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
943 unsigned long mask_color = x->background_pixel;
945 /* Don't let pointers be invisible. */
946 if (mask_color == pixel)
948 x_free_colors (f, &pixel, 1);
949 pixel = x_copy_color (f, x->foreground_pixel);
952 unload_color (f, x->mouse_pixel);
953 x->mouse_pixel = pixel;
955 BLOCK_INPUT;
957 /* It's not okay to crash if the user selects a screwy cursor. */
958 count = x_catch_errors (dpy);
960 if (!NILP (Vx_pointer_shape))
962 CHECK_NUMBER (Vx_pointer_shape);
963 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
965 else
966 cursor = XCreateFontCursor (dpy, XC_xterm);
967 x_check_errors (dpy, "bad text pointer cursor: %s");
969 if (!NILP (Vx_nontext_pointer_shape))
971 CHECK_NUMBER (Vx_nontext_pointer_shape);
972 nontext_cursor
973 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
975 else
976 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
977 x_check_errors (dpy, "bad nontext pointer cursor: %s");
979 if (!NILP (Vx_hourglass_pointer_shape))
981 CHECK_NUMBER (Vx_hourglass_pointer_shape);
982 hourglass_cursor
983 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
985 else
986 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
987 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
989 if (!NILP (Vx_mode_pointer_shape))
991 CHECK_NUMBER (Vx_mode_pointer_shape);
992 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
994 else
995 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
996 x_check_errors (dpy, "bad modeline pointer cursor: %s");
998 if (!NILP (Vx_sensitive_text_pointer_shape))
1000 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
1001 hand_cursor
1002 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
1004 else
1005 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
1007 if (!NILP (Vx_window_horizontal_drag_shape))
1009 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
1010 horizontal_drag_cursor
1011 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
1013 else
1014 horizontal_drag_cursor
1015 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
1017 /* Check and report errors with the above calls. */
1018 x_check_errors (dpy, "can't set cursor shape: %s");
1019 x_uncatch_errors (dpy, count);
1022 XColor fore_color, back_color;
1024 fore_color.pixel = x->mouse_pixel;
1025 x_query_color (f, &fore_color);
1026 back_color.pixel = mask_color;
1027 x_query_color (f, &back_color);
1029 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
1030 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
1031 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
1032 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
1033 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
1034 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
1037 if (FRAME_X_WINDOW (f) != 0)
1038 XDefineCursor (dpy, FRAME_X_WINDOW (f), cursor);
1040 if (cursor != x->text_cursor
1041 && x->text_cursor != 0)
1042 XFreeCursor (dpy, x->text_cursor);
1043 x->text_cursor = cursor;
1045 if (nontext_cursor != x->nontext_cursor
1046 && x->nontext_cursor != 0)
1047 XFreeCursor (dpy, x->nontext_cursor);
1048 x->nontext_cursor = nontext_cursor;
1050 if (hourglass_cursor != x->hourglass_cursor
1051 && x->hourglass_cursor != 0)
1052 XFreeCursor (dpy, x->hourglass_cursor);
1053 x->hourglass_cursor = hourglass_cursor;
1055 if (mode_cursor != x->modeline_cursor
1056 && x->modeline_cursor != 0)
1057 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
1058 x->modeline_cursor = mode_cursor;
1060 if (hand_cursor != x->hand_cursor
1061 && x->hand_cursor != 0)
1062 XFreeCursor (dpy, x->hand_cursor);
1063 x->hand_cursor = hand_cursor;
1065 if (horizontal_drag_cursor != x->horizontal_drag_cursor
1066 && x->horizontal_drag_cursor != 0)
1067 XFreeCursor (dpy, x->horizontal_drag_cursor);
1068 x->horizontal_drag_cursor = horizontal_drag_cursor;
1070 XFlush (dpy);
1071 UNBLOCK_INPUT;
1073 update_face_from_frame_parameter (f, Qmouse_color, arg);
1076 void
1077 x_set_cursor_color (f, arg, oldval)
1078 struct frame *f;
1079 Lisp_Object arg, oldval;
1081 unsigned long fore_pixel, pixel;
1082 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
1083 struct x_output *x = f->output_data.x;
1085 if (!NILP (Vx_cursor_fore_pixel))
1087 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
1088 WHITE_PIX_DEFAULT (f));
1089 fore_pixel_allocated_p = 1;
1091 else
1092 fore_pixel = x->background_pixel;
1094 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1095 pixel_allocated_p = 1;
1097 /* Make sure that the cursor color differs from the background color. */
1098 if (pixel == x->background_pixel)
1100 if (pixel_allocated_p)
1102 x_free_colors (f, &pixel, 1);
1103 pixel_allocated_p = 0;
1106 pixel = x->mouse_pixel;
1107 if (pixel == fore_pixel)
1109 if (fore_pixel_allocated_p)
1111 x_free_colors (f, &fore_pixel, 1);
1112 fore_pixel_allocated_p = 0;
1114 fore_pixel = x->background_pixel;
1118 unload_color (f, x->cursor_foreground_pixel);
1119 if (!fore_pixel_allocated_p)
1120 fore_pixel = x_copy_color (f, fore_pixel);
1121 x->cursor_foreground_pixel = fore_pixel;
1123 unload_color (f, x->cursor_pixel);
1124 if (!pixel_allocated_p)
1125 pixel = x_copy_color (f, pixel);
1126 x->cursor_pixel = pixel;
1128 if (FRAME_X_WINDOW (f) != 0)
1130 BLOCK_INPUT;
1131 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
1132 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
1133 UNBLOCK_INPUT;
1135 if (FRAME_VISIBLE_P (f))
1137 x_update_cursor (f, 0);
1138 x_update_cursor (f, 1);
1142 update_face_from_frame_parameter (f, Qcursor_color, arg);
1145 /* Set the border-color of frame F to pixel value PIX.
1146 Note that this does not fully take effect if done before
1147 F has an x-window. */
1149 void
1150 x_set_border_pixel (f, pix)
1151 struct frame *f;
1152 int pix;
1154 unload_color (f, f->output_data.x->border_pixel);
1155 f->output_data.x->border_pixel = pix;
1157 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
1159 BLOCK_INPUT;
1160 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1161 (unsigned long)pix);
1162 UNBLOCK_INPUT;
1164 if (FRAME_VISIBLE_P (f))
1165 redraw_frame (f);
1169 /* Set the border-color of frame F to value described by ARG.
1170 ARG can be a string naming a color.
1171 The border-color is used for the border that is drawn by the X server.
1172 Note that this does not fully take effect if done before
1173 F has an x-window; it must be redone when the window is created.
1175 Note: this is done in two routines because of the way X10 works.
1177 Note: under X11, this is normally the province of the window manager,
1178 and so emacs' border colors may be overridden. */
1180 void
1181 x_set_border_color (f, arg, oldval)
1182 struct frame *f;
1183 Lisp_Object arg, oldval;
1185 int pix;
1187 CHECK_STRING (arg);
1188 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1189 x_set_border_pixel (f, pix);
1190 update_face_from_frame_parameter (f, Qborder_color, arg);
1194 void
1195 x_set_cursor_type (f, arg, oldval)
1196 FRAME_PTR f;
1197 Lisp_Object arg, oldval;
1199 set_frame_cursor_types (f, arg);
1201 /* Make sure the cursor gets redrawn. */
1202 cursor_type_changed = 1;
1205 void
1206 x_set_icon_type (f, arg, oldval)
1207 struct frame *f;
1208 Lisp_Object arg, oldval;
1210 int result;
1212 if (STRINGP (arg))
1214 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1215 return;
1217 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1218 return;
1220 BLOCK_INPUT;
1221 if (NILP (arg))
1222 result = x_text_icon (f,
1223 (char *) SDATA ((!NILP (f->icon_name)
1224 ? f->icon_name
1225 : f->name)));
1226 else
1227 result = x_bitmap_icon (f, arg);
1229 if (result)
1231 UNBLOCK_INPUT;
1232 error ("No icon window available");
1235 XFlush (FRAME_X_DISPLAY (f));
1236 UNBLOCK_INPUT;
1239 void
1240 x_set_icon_name (f, arg, oldval)
1241 struct frame *f;
1242 Lisp_Object arg, oldval;
1244 int result;
1246 if (STRINGP (arg))
1248 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1249 return;
1251 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1252 return;
1254 f->icon_name = arg;
1256 if (f->output_data.x->icon_bitmap != 0)
1257 return;
1259 BLOCK_INPUT;
1261 result = x_text_icon (f,
1262 (char *) SDATA ((!NILP (f->icon_name)
1263 ? f->icon_name
1264 : !NILP (f->title)
1265 ? f->title
1266 : f->name)));
1268 if (result)
1270 UNBLOCK_INPUT;
1271 error ("No icon window available");
1274 XFlush (FRAME_X_DISPLAY (f));
1275 UNBLOCK_INPUT;
1279 void
1280 x_set_menu_bar_lines (f, value, oldval)
1281 struct frame *f;
1282 Lisp_Object value, oldval;
1284 int nlines;
1285 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1286 int olines = FRAME_MENU_BAR_LINES (f);
1287 #endif
1289 /* Right now, menu bars don't work properly in minibuf-only frames;
1290 most of the commands try to apply themselves to the minibuffer
1291 frame itself, and get an error because you can't switch buffers
1292 in or split the minibuffer window. */
1293 if (FRAME_MINIBUF_ONLY_P (f))
1294 return;
1296 if (INTEGERP (value))
1297 nlines = XINT (value);
1298 else
1299 nlines = 0;
1301 /* Make sure we redisplay all windows in this frame. */
1302 windows_or_buffers_changed++;
1304 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1305 FRAME_MENU_BAR_LINES (f) = 0;
1306 if (nlines)
1308 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1309 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1310 /* Make sure next redisplay shows the menu bar. */
1311 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
1313 else
1315 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1316 free_frame_menubar (f);
1317 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1318 if (FRAME_X_P (f))
1319 f->output_data.x->menubar_widget = 0;
1321 #else /* not USE_X_TOOLKIT && not USE_GTK */
1322 FRAME_MENU_BAR_LINES (f) = nlines;
1323 change_window_heights (f->root_window, nlines - olines);
1324 #endif /* not USE_X_TOOLKIT */
1325 adjust_glyphs (f);
1329 /* Set the number of lines used for the tool bar of frame F to VALUE.
1330 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1331 is the old number of tool bar lines. This function changes the
1332 height of all windows on frame F to match the new tool bar height.
1333 The frame's height doesn't change. */
1335 void
1336 x_set_tool_bar_lines (f, value, oldval)
1337 struct frame *f;
1338 Lisp_Object value, oldval;
1340 int delta, nlines, root_height;
1341 Lisp_Object root_window;
1343 /* Treat tool bars like menu bars. */
1344 if (FRAME_MINIBUF_ONLY_P (f))
1345 return;
1347 /* Use VALUE only if an integer >= 0. */
1348 if (INTEGERP (value) && XINT (value) >= 0)
1349 nlines = XFASTINT (value);
1350 else
1351 nlines = 0;
1353 #ifdef USE_GTK
1354 FRAME_TOOL_BAR_LINES (f) = 0;
1355 if (nlines)
1357 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1358 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1359 /* Make sure next redisplay shows the tool bar. */
1360 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = Qt;
1361 update_frame_tool_bar (f);
1363 else
1365 if (FRAME_EXTERNAL_TOOL_BAR (f))
1366 free_frame_tool_bar (f);
1367 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1370 return;
1371 #endif
1373 /* Make sure we redisplay all windows in this frame. */
1374 ++windows_or_buffers_changed;
1376 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1378 /* Don't resize the tool-bar to more than we have room for. */
1379 root_window = FRAME_ROOT_WINDOW (f);
1380 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1381 if (root_height - delta < 1)
1383 delta = root_height - 1;
1384 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1387 FRAME_TOOL_BAR_LINES (f) = nlines;
1388 change_window_heights (root_window, delta);
1389 adjust_glyphs (f);
1391 /* We also have to make sure that the internal border at the top of
1392 the frame, below the menu bar or tool bar, is redrawn when the
1393 tool bar disappears. This is so because the internal border is
1394 below the tool bar if one is displayed, but is below the menu bar
1395 if there isn't a tool bar. The tool bar draws into the area
1396 below the menu bar. */
1397 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1399 updating_frame = f;
1400 clear_frame ();
1401 clear_current_matrices (f);
1402 updating_frame = NULL;
1405 /* If the tool bar gets smaller, the internal border below it
1406 has to be cleared. It was formerly part of the display
1407 of the larger tool bar, and updating windows won't clear it. */
1408 if (delta < 0)
1410 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1411 int width = FRAME_PIXEL_WIDTH (f);
1412 int y = nlines * FRAME_LINE_HEIGHT (f);
1414 /* height can be zero here. */
1415 if (height > 0 && width > 0)
1417 BLOCK_INPUT;
1418 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1419 0, y, width, height, False);
1420 UNBLOCK_INPUT;
1423 if (WINDOWP (f->tool_bar_window))
1424 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1429 /* Set the foreground color for scroll bars on frame F to VALUE.
1430 VALUE should be a string, a color name. If it isn't a string or
1431 isn't a valid color name, do nothing. OLDVAL is the old value of
1432 the frame parameter. */
1434 void
1435 x_set_scroll_bar_foreground (f, value, oldval)
1436 struct frame *f;
1437 Lisp_Object value, oldval;
1439 unsigned long pixel;
1441 if (STRINGP (value))
1442 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1443 else
1444 pixel = -1;
1446 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1447 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1449 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1450 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1452 /* Remove all scroll bars because they have wrong colors. */
1453 if (condemn_scroll_bars_hook)
1454 (*condemn_scroll_bars_hook) (f);
1455 if (judge_scroll_bars_hook)
1456 (*judge_scroll_bars_hook) (f);
1458 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1459 redraw_frame (f);
1464 /* Set the background color for scroll bars on frame F to VALUE VALUE
1465 should be a string, a color name. If it isn't a string or isn't a
1466 valid color name, do nothing. OLDVAL is the old value of the frame
1467 parameter. */
1469 void
1470 x_set_scroll_bar_background (f, value, oldval)
1471 struct frame *f;
1472 Lisp_Object value, oldval;
1474 unsigned long pixel;
1476 if (STRINGP (value))
1477 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1478 else
1479 pixel = -1;
1481 if (f->output_data.x->scroll_bar_background_pixel != -1)
1482 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1484 #ifdef USE_TOOLKIT_SCROLL_BARS
1485 /* Scrollbar shadow colors. */
1486 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1488 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1489 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1491 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1493 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1494 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1496 #endif /* USE_TOOLKIT_SCROLL_BARS */
1498 f->output_data.x->scroll_bar_background_pixel = pixel;
1499 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1501 /* Remove all scroll bars because they have wrong colors. */
1502 if (condemn_scroll_bars_hook)
1503 (*condemn_scroll_bars_hook) (f);
1504 if (judge_scroll_bars_hook)
1505 (*judge_scroll_bars_hook) (f);
1507 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1508 redraw_frame (f);
1513 /* Encode Lisp string STRING as a text in a format appropriate for
1514 XICCC (X Inter Client Communication Conventions).
1516 If STRING contains only ASCII characters, do no conversion and
1517 return the string data of STRING. Otherwise, encode the text by
1518 CODING_SYSTEM, and return a newly allocated memory area which
1519 should be freed by `xfree' by a caller.
1521 SELECTIONP non-zero means the string is being encoded for an X
1522 selection, so it is safe to run pre-write conversions (which
1523 may run Lisp code).
1525 Store the byte length of resulting text in *TEXT_BYTES.
1527 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1528 which means that the `encoding' of the result can be `STRING'.
1529 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1530 the result should be `COMPOUND_TEXT'. */
1532 static unsigned char *
1533 x_encode_text (string, coding_system, selectionp, text_bytes, stringp, freep)
1534 Lisp_Object string, coding_system;
1535 int *text_bytes, *stringp;
1536 int selectionp;
1537 int *freep;
1539 unsigned char *str = SDATA (string);
1540 int chars = SCHARS (string);
1541 int bytes = SBYTES (string);
1542 int charset_info;
1543 int bufsize;
1544 unsigned char *buf;
1545 struct coding_system coding;
1546 extern Lisp_Object Qcompound_text_with_extensions;
1548 charset_info = find_charset_in_text (str, chars, bytes, NULL, Qnil);
1549 if (charset_info == 0)
1551 /* No multibyte character in OBJ. We need not encode it. */
1552 *text_bytes = bytes;
1553 *stringp = 1;
1554 *freep = 0;
1555 return str;
1558 setup_coding_system (coding_system, &coding);
1559 if (selectionp
1560 && SYMBOLP (coding.pre_write_conversion)
1561 && !NILP (Ffboundp (coding.pre_write_conversion)))
1563 string = run_pre_post_conversion_on_str (string, &coding, 1);
1564 str = SDATA (string);
1565 chars = SCHARS (string);
1566 bytes = SBYTES (string);
1568 coding.src_multibyte = 1;
1569 coding.dst_multibyte = 0;
1570 coding.mode |= CODING_MODE_LAST_BLOCK;
1571 if (coding.type == coding_type_iso2022)
1572 coding.flags |= CODING_FLAG_ISO_SAFE;
1573 /* We suppress producing escape sequences for composition. */
1574 coding.composing = COMPOSITION_DISABLED;
1575 bufsize = encoding_buffer_size (&coding, bytes);
1576 buf = (unsigned char *) xmalloc (bufsize);
1577 encode_coding (&coding, str, buf, bytes, bufsize);
1578 *text_bytes = coding.produced;
1579 *stringp = (charset_info == 1
1580 || (!EQ (coding_system, Qcompound_text)
1581 && !EQ (coding_system, Qcompound_text_with_extensions)));
1582 *freep = 1;
1583 return buf;
1587 /* Set the WM name to NAME for frame F. Also set the icon name.
1588 If the frame already has an icon name, use that, otherwise set the
1589 icon name to NAME. */
1591 static void
1592 x_set_name_internal (f, name)
1593 FRAME_PTR f;
1594 Lisp_Object name;
1596 if (FRAME_X_WINDOW (f))
1598 BLOCK_INPUT;
1599 #ifdef HAVE_X11R4
1601 XTextProperty text, icon;
1602 int bytes, stringp;
1603 int do_free_icon_value = 0, do_free_text_value = 0;
1604 Lisp_Object coding_system;
1606 coding_system = Qcompound_text;
1607 /* Note: Encoding strategy
1609 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1610 text.encoding. But, there are non-internationalized window
1611 managers which don't support that encoding. So, if NAME
1612 contains only ASCII and 8859-1 characters, encode it by
1613 iso-latin-1, and use "STRING" in text.encoding hoping that
1614 such window managers at least analyze this format correctly,
1615 i.e. treat 8-bit bytes as 8859-1 characters.
1617 We may also be able to use "UTF8_STRING" in text.encoding
1618 in the future which can encode all Unicode characters.
1619 But, for the moment, there's no way to know that the
1620 current window manager supports it or not. */
1621 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1622 &do_free_text_value);
1623 text.encoding = (stringp ? XA_STRING
1624 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1625 text.format = 8;
1626 text.nitems = bytes;
1628 if (NILP (f->icon_name))
1630 icon = text;
1632 else
1634 /* See the above comment "Note: Encoding strategy". */
1635 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1636 &bytes, &stringp, &do_free_icon_value);
1637 icon.encoding = (stringp ? XA_STRING
1638 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1639 icon.format = 8;
1640 icon.nitems = bytes;
1643 #ifdef USE_GTK
1644 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1645 (char *) SDATA (ENCODE_UTF_8 (name)));
1646 #else /* not USE_GTK */
1647 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1648 #endif /* not USE_GTK */
1650 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1652 if (do_free_icon_value)
1653 xfree (icon.value);
1654 if (do_free_text_value)
1655 xfree (text.value);
1657 #else /* not HAVE_X11R4 */
1658 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1659 SDATA (name));
1660 XStoreName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1661 SDATA (name));
1662 #endif /* not HAVE_X11R4 */
1663 UNBLOCK_INPUT;
1667 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1668 x_id_name.
1670 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1671 name; if NAME is a string, set F's name to NAME and set
1672 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1674 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1675 suggesting a new name, which lisp code should override; if
1676 F->explicit_name is set, ignore the new name; otherwise, set it. */
1678 void
1679 x_set_name (f, name, explicit)
1680 struct frame *f;
1681 Lisp_Object name;
1682 int explicit;
1684 /* Make sure that requests from lisp code override requests from
1685 Emacs redisplay code. */
1686 if (explicit)
1688 /* If we're switching from explicit to implicit, we had better
1689 update the mode lines and thereby update the title. */
1690 if (f->explicit_name && NILP (name))
1691 update_mode_lines = 1;
1693 f->explicit_name = ! NILP (name);
1695 else if (f->explicit_name)
1696 return;
1698 /* If NAME is nil, set the name to the x_id_name. */
1699 if (NILP (name))
1701 /* Check for no change needed in this very common case
1702 before we do any consing. */
1703 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1704 SDATA (f->name)))
1705 return;
1706 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1708 else
1709 CHECK_STRING (name);
1711 /* Don't change the name if it's already NAME. */
1712 if (! NILP (Fstring_equal (name, f->name)))
1713 return;
1715 f->name = name;
1717 /* For setting the frame title, the title parameter should override
1718 the name parameter. */
1719 if (! NILP (f->title))
1720 name = f->title;
1722 x_set_name_internal (f, name);
1725 /* This function should be called when the user's lisp code has
1726 specified a name for the frame; the name will override any set by the
1727 redisplay code. */
1728 void
1729 x_explicitly_set_name (f, arg, oldval)
1730 FRAME_PTR f;
1731 Lisp_Object arg, oldval;
1733 x_set_name (f, arg, 1);
1736 /* This function should be called by Emacs redisplay code to set the
1737 name; names set this way will never override names set by the user's
1738 lisp code. */
1739 void
1740 x_implicitly_set_name (f, arg, oldval)
1741 FRAME_PTR f;
1742 Lisp_Object arg, oldval;
1744 x_set_name (f, arg, 0);
1747 /* Change the title of frame F to NAME.
1748 If NAME is nil, use the frame name as the title.
1750 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1751 name; if NAME is a string, set F's name to NAME and set
1752 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1754 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1755 suggesting a new name, which lisp code should override; if
1756 F->explicit_name is set, ignore the new name; otherwise, set it. */
1758 void
1759 x_set_title (f, name, old_name)
1760 struct frame *f;
1761 Lisp_Object name, old_name;
1763 /* Don't change the title if it's already NAME. */
1764 if (EQ (name, f->title))
1765 return;
1767 update_mode_lines = 1;
1769 f->title = name;
1771 if (NILP (name))
1772 name = f->name;
1773 else
1774 CHECK_STRING (name);
1776 x_set_name_internal (f, name);
1779 void
1780 x_set_scroll_bar_default_width (f)
1781 struct frame *f;
1783 int wid = FRAME_COLUMN_WIDTH (f);
1785 #ifdef USE_TOOLKIT_SCROLL_BARS
1786 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1787 int width = 16 + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1788 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1789 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1790 #else
1791 /* Make the actual width at least 14 pixels and a multiple of a
1792 character width. */
1793 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1795 /* Use all of that space (aside from required margins) for the
1796 scroll bar. */
1797 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1798 #endif
1802 /* Record in frame F the specified or default value according to ALIST
1803 of the parameter named PROP (a Lisp symbol). If no value is
1804 specified for PROP, look for an X default for XPROP on the frame
1805 named NAME. If that is not found either, use the value DEFLT. */
1807 static Lisp_Object
1808 x_default_scroll_bar_color_parameter (f, alist, prop, xprop, xclass,
1809 foreground_p)
1810 struct frame *f;
1811 Lisp_Object alist;
1812 Lisp_Object prop;
1813 char *xprop;
1814 char *xclass;
1815 int foreground_p;
1817 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1818 Lisp_Object tem;
1820 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1821 if (EQ (tem, Qunbound))
1823 #ifdef USE_TOOLKIT_SCROLL_BARS
1825 /* See if an X resource for the scroll bar color has been
1826 specified. */
1827 tem = display_x_get_resource (dpyinfo,
1828 build_string (foreground_p
1829 ? "foreground"
1830 : "background"),
1831 empty_string,
1832 build_string ("verticalScrollBar"),
1833 empty_string);
1834 if (!STRINGP (tem))
1836 /* If nothing has been specified, scroll bars will use a
1837 toolkit-dependent default. Because these defaults are
1838 difficult to get at without actually creating a scroll
1839 bar, use nil to indicate that no color has been
1840 specified. */
1841 tem = Qnil;
1844 #else /* not USE_TOOLKIT_SCROLL_BARS */
1846 tem = Qnil;
1848 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1851 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
1852 return tem;
1857 #if !defined (HAVE_X11R4) && !defined (HAVE_XSETWMPROTOCOLS)
1859 Status
1860 XSetWMProtocols (dpy, w, protocols, count)
1861 Display *dpy;
1862 Window w;
1863 Atom *protocols;
1864 int count;
1866 Atom prop;
1867 prop = XInternAtom (dpy, "WM_PROTOCOLS", False);
1868 if (prop == None) return False;
1869 XChangeProperty (dpy, w, prop, XA_ATOM, 32, PropModeReplace,
1870 (unsigned char *) protocols, count);
1871 return True;
1873 #endif /* not HAVE_X11R4 && not HAVE_XSETWMPROTOCOLS */
1875 #ifdef USE_X_TOOLKIT
1877 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1878 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1879 already be present because of the toolkit (Motif adds some of them,
1880 for example, but Xt doesn't). */
1882 static void
1883 hack_wm_protocols (f, widget)
1884 FRAME_PTR f;
1885 Widget widget;
1887 Display *dpy = XtDisplay (widget);
1888 Window w = XtWindow (widget);
1889 int need_delete = 1;
1890 int need_focus = 1;
1891 int need_save = 1;
1893 BLOCK_INPUT;
1895 Atom type;
1896 unsigned char *catoms;
1897 int format = 0;
1898 unsigned long nitems = 0;
1899 unsigned long bytes_after;
1901 if ((XGetWindowProperty (dpy, w,
1902 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1903 (long)0, (long)100, False, XA_ATOM,
1904 &type, &format, &nitems, &bytes_after,
1905 &catoms)
1906 == Success)
1907 && format == 32 && type == XA_ATOM)
1909 Atom *atoms = (Atom *) catoms;
1910 while (nitems > 0)
1912 nitems--;
1913 if (atoms[nitems]
1914 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1915 need_delete = 0;
1916 else if (atoms[nitems]
1917 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1918 need_focus = 0;
1919 else if (atoms[nitems]
1920 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1921 need_save = 0;
1924 if (catoms)
1925 XFree (catoms);
1928 Atom props [10];
1929 int count = 0;
1930 if (need_delete)
1931 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1932 if (need_focus)
1933 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1934 if (need_save)
1935 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1936 if (count)
1937 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1938 XA_ATOM, 32, PropModeAppend,
1939 (unsigned char *) props, count);
1941 UNBLOCK_INPUT;
1943 #endif
1947 /* Support routines for XIC (X Input Context). */
1949 #ifdef HAVE_X_I18N
1951 static XFontSet xic_create_xfontset P_ ((struct frame *, char *));
1952 static XIMStyle best_xim_style P_ ((XIMStyles *, XIMStyles *));
1955 /* Supported XIM styles, ordered by preference. */
1957 static XIMStyle supported_xim_styles[] =
1959 XIMPreeditPosition | XIMStatusArea,
1960 XIMPreeditPosition | XIMStatusNothing,
1961 XIMPreeditPosition | XIMStatusNone,
1962 XIMPreeditNothing | XIMStatusArea,
1963 XIMPreeditNothing | XIMStatusNothing,
1964 XIMPreeditNothing | XIMStatusNone,
1965 XIMPreeditNone | XIMStatusArea,
1966 XIMPreeditNone | XIMStatusNothing,
1967 XIMPreeditNone | XIMStatusNone,
1972 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1974 char xic_defaut_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1976 /* Create an Xt fontset spec from the name of a base font.
1977 If `motif' is True use the Motif syntax. */
1978 char *
1979 xic_create_fontsetname (base_fontname, motif)
1980 char *base_fontname;
1981 Bool motif;
1983 const char *sep = motif ? ";" : ",";
1984 char *fontsetname;
1986 /* Make a fontset name from the base font name. */
1987 if (xic_defaut_fontset == base_fontname)
1988 { /* There is no base font name, use the default. */
1989 int len = strlen (base_fontname) + 2;
1990 fontsetname = xmalloc (len);
1991 bzero (fontsetname, len);
1992 strcpy (fontsetname, base_fontname);
1994 else
1996 /* Make a fontset name from the base font name.
1997 The font set will be made of the following elements:
1998 - the base font.
1999 - the base font where the charset spec is replaced by -*-*.
2000 - the same but with the family also replaced with -*-*-. */
2001 char *p = base_fontname;
2002 int i;
2004 for (i = 0; *p; p++)
2005 if (*p == '-') i++;
2006 if (i != 14)
2007 { /* As the font name doesn't conform to XLFD, we can't
2008 modify it to generalize it to allcs and allfamilies.
2009 Use the specified font plus the default. */
2010 int len = strlen (base_fontname) + strlen (xic_defaut_fontset) + 3;
2011 fontsetname = xmalloc (len);
2012 bzero (fontsetname, len);
2013 strcpy (fontsetname, base_fontname);
2014 strcat (fontsetname, sep);
2015 strcat (fontsetname, xic_defaut_fontset);
2017 else
2019 int len;
2020 char *p1 = NULL, *p2 = NULL;
2021 char *font_allcs = NULL;
2022 char *font_allfamilies = NULL;
2023 char *font_all = NULL;
2024 char *allcs = "*-*-*-*-*-*-*";
2025 char *allfamilies = "-*-*-";
2026 char *all = "*-*-*-*-";
2028 for (i = 0, p = base_fontname; i < 8; p++)
2030 if (*p == '-')
2032 i++;
2033 if (i == 3)
2034 p1 = p + 1;
2035 else if (i == 7)
2036 p2 = p + 1;
2039 /* Build the font spec that matches all charsets. */
2040 len = p - base_fontname + strlen (allcs) + 1;
2041 font_allcs = (char *) alloca (len);
2042 bzero (font_allcs, len);
2043 bcopy (base_fontname, font_allcs, p - base_fontname);
2044 strcat (font_allcs, allcs);
2046 /* Build the font spec that matches all families. */
2047 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
2048 font_allfamilies = (char *) alloca (len);
2049 bzero (font_allfamilies, len);
2050 strcpy (font_allfamilies, allfamilies);
2051 bcopy (p1, font_allfamilies + strlen (allfamilies), p - p1);
2052 strcat (font_allfamilies, allcs);
2054 /* Build the font spec that matches all. */
2055 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
2056 font_all = (char *) alloca (len);
2057 bzero (font_all, len);
2058 strcpy (font_all, allfamilies);
2059 strcat (font_all, all);
2060 bcopy (p2, font_all + strlen (all) + strlen (allfamilies), p - p2);
2061 strcat (font_all, allcs);
2063 /* Build the actual font set name. */
2064 len = strlen (base_fontname) + strlen (font_allcs)
2065 + strlen (font_allfamilies) + strlen (font_all) + 5;
2066 fontsetname = xmalloc (len);
2067 bzero (fontsetname, len);
2068 strcpy (fontsetname, base_fontname);
2069 strcat (fontsetname, sep);
2070 strcat (fontsetname, font_allcs);
2071 strcat (fontsetname, sep);
2072 strcat (fontsetname, font_allfamilies);
2073 strcat (fontsetname, sep);
2074 strcat (fontsetname, font_all);
2077 if (motif)
2078 strcat (fontsetname, ":");
2079 return fontsetname;
2082 static XFontSet
2083 xic_create_xfontset (f, base_fontname)
2084 struct frame *f;
2085 char *base_fontname;
2087 XFontSet xfs = NULL;
2088 char **missing_list = NULL;
2089 int missing_count;
2090 char *def_string;
2091 Lisp_Object rest, frame;
2093 if (!base_fontname)
2094 base_fontname = xic_defaut_fontset;
2096 /* See if there is another frame already using same fontset. */
2097 FOR_EACH_FRAME (rest, frame)
2099 struct frame *cf = XFRAME (frame);
2100 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2101 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2102 && FRAME_XIC_BASE_FONTNAME (cf)
2103 && !strcmp (FRAME_XIC_BASE_FONTNAME (cf), base_fontname))
2105 xfs = FRAME_XIC_FONTSET (cf);
2106 break;
2110 if (!xfs)
2112 char *fontsetname = xic_create_fontsetname (base_fontname, False);
2114 /* New fontset. */
2115 xfs = XCreateFontSet (FRAME_X_DISPLAY (f),
2116 fontsetname, &missing_list,
2117 &missing_count, &def_string);
2118 if (missing_list)
2119 XFreeStringList (missing_list);
2120 xfree (fontsetname);
2123 if (FRAME_XIC_BASE_FONTNAME (f))
2124 xfree (FRAME_XIC_BASE_FONTNAME (f));
2125 FRAME_XIC_BASE_FONTNAME (f) = xstrdup (base_fontname);
2127 /* No need to free def_string. */
2128 return xfs;
2131 /* Free the X fontset of frame F if it is the last frame using it. */
2133 void
2134 xic_free_xfontset (f)
2135 struct frame *f;
2137 Lisp_Object rest, frame;
2138 int shared_p = 0;
2140 if (!FRAME_XIC_FONTSET (f))
2141 return;
2143 /* See if there is another frame sharing the same fontset. */
2144 FOR_EACH_FRAME (rest, frame)
2146 struct frame *cf = XFRAME (frame);
2147 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2148 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2149 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2151 shared_p = 1;
2152 break;
2156 if (!shared_p)
2157 /* The fontset is not used anymore. It is safe to free it. */
2158 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2160 if (FRAME_XIC_BASE_FONTNAME (f))
2161 xfree (FRAME_XIC_BASE_FONTNAME (f));
2162 FRAME_XIC_BASE_FONTNAME (f) = NULL;
2163 FRAME_XIC_FONTSET (f) = NULL;
2167 /* Value is the best input style, given user preferences USER (already
2168 checked to be supported by Emacs), and styles supported by the
2169 input method XIM. */
2171 static XIMStyle
2172 best_xim_style (user, xim)
2173 XIMStyles *user;
2174 XIMStyles *xim;
2176 int i, j;
2178 for (i = 0; i < user->count_styles; ++i)
2179 for (j = 0; j < xim->count_styles; ++j)
2180 if (user->supported_styles[i] == xim->supported_styles[j])
2181 return user->supported_styles[i];
2183 /* Return the default style. */
2184 return XIMPreeditNothing | XIMStatusNothing;
2187 /* Create XIC for frame F. */
2189 static XIMStyle xic_style;
2191 void
2192 create_frame_xic (f)
2193 struct frame *f;
2195 XIM xim;
2196 XIC xic = NULL;
2197 XFontSet xfs = NULL;
2199 if (FRAME_XIC (f))
2200 return;
2202 /* Create X fontset. */
2203 xfs = xic_create_xfontset
2204 (f, (FRAME_FONTSET (f) < 0) ? NULL
2205 : (char *) SDATA (fontset_ascii (FRAME_FONTSET (f))));
2207 xim = FRAME_X_XIM (f);
2208 if (xim)
2210 XRectangle s_area;
2211 XPoint spot;
2212 XVaNestedList preedit_attr;
2213 XVaNestedList status_attr;
2215 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2216 spot.x = 0; spot.y = 1;
2218 /* Determine XIC style. */
2219 if (xic_style == 0)
2221 XIMStyles supported_list;
2222 supported_list.count_styles = (sizeof supported_xim_styles
2223 / sizeof supported_xim_styles[0]);
2224 supported_list.supported_styles = supported_xim_styles;
2225 xic_style = best_xim_style (&supported_list,
2226 FRAME_X_XIM_STYLES (f));
2229 preedit_attr = XVaCreateNestedList (0,
2230 XNFontSet, xfs,
2231 XNForeground,
2232 FRAME_FOREGROUND_PIXEL (f),
2233 XNBackground,
2234 FRAME_BACKGROUND_PIXEL (f),
2235 (xic_style & XIMPreeditPosition
2236 ? XNSpotLocation
2237 : NULL),
2238 &spot,
2239 NULL);
2240 status_attr = XVaCreateNestedList (0,
2241 XNArea,
2242 &s_area,
2243 XNFontSet,
2244 xfs,
2245 XNForeground,
2246 FRAME_FOREGROUND_PIXEL (f),
2247 XNBackground,
2248 FRAME_BACKGROUND_PIXEL (f),
2249 NULL);
2251 xic = XCreateIC (xim,
2252 XNInputStyle, xic_style,
2253 XNClientWindow, FRAME_X_WINDOW (f),
2254 XNFocusWindow, FRAME_X_WINDOW (f),
2255 XNStatusAttributes, status_attr,
2256 XNPreeditAttributes, preedit_attr,
2257 NULL);
2258 XFree (preedit_attr);
2259 XFree (status_attr);
2262 FRAME_XIC (f) = xic;
2263 FRAME_XIC_STYLE (f) = xic_style;
2264 FRAME_XIC_FONTSET (f) = xfs;
2268 /* Destroy XIC and free XIC fontset of frame F, if any. */
2270 void
2271 free_frame_xic (f)
2272 struct frame *f;
2274 if (FRAME_XIC (f) == NULL)
2275 return;
2277 XDestroyIC (FRAME_XIC (f));
2278 xic_free_xfontset (f);
2280 FRAME_XIC (f) = NULL;
2284 /* Place preedit area for XIC of window W's frame to specified
2285 pixel position X/Y. X and Y are relative to window W. */
2287 void
2288 xic_set_preeditarea (w, x, y)
2289 struct window *w;
2290 int x, y;
2292 struct frame *f = XFRAME (w->frame);
2293 XVaNestedList attr;
2294 XPoint spot;
2296 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2297 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2298 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2299 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2300 XFree (attr);
2304 /* Place status area for XIC in bottom right corner of frame F.. */
2306 void
2307 xic_set_statusarea (f)
2308 struct frame *f;
2310 XIC xic = FRAME_XIC (f);
2311 XVaNestedList attr;
2312 XRectangle area;
2313 XRectangle *needed;
2315 /* Negotiate geometry of status area. If input method has existing
2316 status area, use its current size. */
2317 area.x = area.y = area.width = area.height = 0;
2318 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2319 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2320 XFree (attr);
2322 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2323 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2324 XFree (attr);
2326 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2328 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2329 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2330 XFree (attr);
2333 area.width = needed->width;
2334 area.height = needed->height;
2335 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2336 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2337 - FRAME_MENUBAR_HEIGHT (f)
2338 - FRAME_TOOLBAR_HEIGHT (f)
2339 - FRAME_INTERNAL_BORDER_WIDTH (f));
2340 XFree (needed);
2342 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2343 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2344 XFree (attr);
2348 /* Set X fontset for XIC of frame F, using base font name
2349 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2351 void
2352 xic_set_xfontset (f, base_fontname)
2353 struct frame *f;
2354 char *base_fontname;
2356 XVaNestedList attr;
2357 XFontSet xfs;
2359 xic_free_xfontset (f);
2361 xfs = xic_create_xfontset (f, base_fontname);
2363 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2364 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2365 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2366 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2367 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2368 XFree (attr);
2370 FRAME_XIC_FONTSET (f) = xfs;
2373 #endif /* HAVE_X_I18N */
2377 #ifdef USE_X_TOOLKIT
2379 /* Create and set up the X widget for frame F. */
2381 static void
2382 x_window (f, window_prompting, minibuffer_only)
2383 struct frame *f;
2384 long window_prompting;
2385 int minibuffer_only;
2387 XClassHint class_hints;
2388 XSetWindowAttributes attributes;
2389 unsigned long attribute_mask;
2390 Widget shell_widget;
2391 Widget pane_widget;
2392 Widget frame_widget;
2393 Arg al [25];
2394 int ac;
2396 BLOCK_INPUT;
2398 /* Use the resource name as the top-level widget name
2399 for looking up resources. Make a non-Lisp copy
2400 for the window manager, so GC relocation won't bother it.
2402 Elsewhere we specify the window name for the window manager. */
2405 char *str = (char *) SDATA (Vx_resource_name);
2406 f->namebuf = (char *) xmalloc (strlen (str) + 1);
2407 strcpy (f->namebuf, str);
2410 ac = 0;
2411 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2412 XtSetArg (al[ac], XtNinput, 1); ac++;
2413 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2414 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2415 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2416 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2417 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2418 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2419 applicationShellWidgetClass,
2420 FRAME_X_DISPLAY (f), al, ac);
2422 f->output_data.x->widget = shell_widget;
2423 /* maybe_set_screen_title_format (shell_widget); */
2425 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2426 (widget_value *) NULL,
2427 shell_widget, False,
2428 (lw_callback) NULL,
2429 (lw_callback) NULL,
2430 (lw_callback) NULL,
2431 (lw_callback) NULL);
2433 ac = 0;
2434 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2435 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2436 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2437 XtSetValues (pane_widget, al, ac);
2438 f->output_data.x->column_widget = pane_widget;
2440 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2441 the emacs screen when changing menubar. This reduces flickering. */
2443 ac = 0;
2444 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2445 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2446 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2447 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2448 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2449 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2450 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2451 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2452 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2453 al, ac);
2455 f->output_data.x->edit_widget = frame_widget;
2457 XtManageChild (frame_widget);
2459 /* Do some needed geometry management. */
2461 int len;
2462 char *tem, shell_position[32];
2463 Arg al[10];
2464 int ac = 0;
2465 int extra_borders = 0;
2466 int menubar_size
2467 = (f->output_data.x->menubar_widget
2468 ? (f->output_data.x->menubar_widget->core.height
2469 + f->output_data.x->menubar_widget->core.border_width)
2470 : 0);
2472 #if 0 /* Experimentally, we now get the right results
2473 for -geometry -0-0 without this. 24 Aug 96, rms. */
2474 if (FRAME_EXTERNAL_MENU_BAR (f))
2476 Dimension ibw = 0;
2477 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2478 menubar_size += ibw;
2480 #endif
2482 f->output_data.x->menubar_height = menubar_size;
2484 #ifndef USE_LUCID
2485 /* Motif seems to need this amount added to the sizes
2486 specified for the shell widget. The Athena/Lucid widgets don't.
2487 Both conclusions reached experimentally. -- rms. */
2488 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2489 &extra_borders, NULL);
2490 extra_borders *= 2;
2491 #endif
2493 /* Convert our geometry parameters into a geometry string
2494 and specify it.
2495 Note that we do not specify here whether the position
2496 is a user-specified or program-specified one.
2497 We pass that information later, in x_wm_set_size_hints. */
2499 int left = f->left_pos;
2500 int xneg = window_prompting & XNegative;
2501 int top = f->top_pos;
2502 int yneg = window_prompting & YNegative;
2503 if (xneg)
2504 left = -left;
2505 if (yneg)
2506 top = -top;
2508 if (window_prompting & USPosition)
2509 sprintf (shell_position, "=%dx%d%c%d%c%d",
2510 FRAME_PIXEL_WIDTH (f) + extra_borders,
2511 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2512 (xneg ? '-' : '+'), left,
2513 (yneg ? '-' : '+'), top);
2514 else
2516 sprintf (shell_position, "=%dx%d",
2517 FRAME_PIXEL_WIDTH (f) + extra_borders,
2518 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2520 /* Setting x and y when the position is not specified in
2521 the geometry string will set program position in the WM hints.
2522 If Emacs had just one program position, we could set it in
2523 fallback resources, but since each make-frame call can specify
2524 different program positions, this is easier. */
2525 XtSetArg (al[ac], XtNx, left); ac++;
2526 XtSetArg (al[ac], XtNy, top); ac++;
2530 len = strlen (shell_position) + 1;
2531 /* We don't free this because we don't know whether
2532 it is safe to free it while the frame exists.
2533 It isn't worth the trouble of arranging to free it
2534 when the frame is deleted. */
2535 tem = (char *) xmalloc (len);
2536 strncpy (tem, shell_position, len);
2537 XtSetArg (al[ac], XtNgeometry, tem); ac++;
2538 XtSetValues (shell_widget, al, ac);
2541 XtManageChild (pane_widget);
2542 XtRealizeWidget (shell_widget);
2544 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2546 validate_x_resource_name ();
2548 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2549 class_hints.res_class = (char *) SDATA (Vx_resource_class);
2550 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2552 #ifdef HAVE_X_I18N
2553 FRAME_XIC (f) = NULL;
2554 if (use_xim)
2555 create_frame_xic (f);
2556 #endif
2558 f->output_data.x->wm_hints.input = True;
2559 f->output_data.x->wm_hints.flags |= InputHint;
2560 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2561 &f->output_data.x->wm_hints);
2563 hack_wm_protocols (f, shell_widget);
2565 #ifdef HACK_EDITRES
2566 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2567 #endif
2569 /* Do a stupid property change to force the server to generate a
2570 PropertyNotify event so that the event_stream server timestamp will
2571 be initialized to something relevant to the time we created the window.
2573 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2574 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2575 XA_ATOM, 32, PropModeAppend,
2576 (unsigned char*) NULL, 0);
2578 /* Make all the standard events reach the Emacs frame. */
2579 attributes.event_mask = STANDARD_EVENT_SET;
2581 #ifdef HAVE_X_I18N
2582 if (FRAME_XIC (f))
2584 /* XIM server might require some X events. */
2585 unsigned long fevent = NoEventMask;
2586 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2587 attributes.event_mask |= fevent;
2589 #endif /* HAVE_X_I18N */
2591 attribute_mask = CWEventMask;
2592 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2593 attribute_mask, &attributes);
2595 XtMapWidget (frame_widget);
2597 /* x_set_name normally ignores requests to set the name if the
2598 requested name is the same as the current name. This is the one
2599 place where that assumption isn't correct; f->name is set, but
2600 the X server hasn't been told. */
2602 Lisp_Object name;
2603 int explicit = f->explicit_name;
2605 f->explicit_name = 0;
2606 name = f->name;
2607 f->name = Qnil;
2608 x_set_name (f, name, explicit);
2611 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2612 f->output_data.x->text_cursor);
2614 UNBLOCK_INPUT;
2616 /* This is a no-op, except under Motif. Make sure main areas are
2617 set to something reasonable, in case we get an error later. */
2618 lw_set_main_areas (pane_widget, 0, frame_widget);
2621 #else /* not USE_X_TOOLKIT */
2622 #ifdef USE_GTK
2623 void
2624 x_window (f)
2625 FRAME_PTR f;
2627 if (! xg_create_frame_widgets (f))
2628 error ("Unable to create window");
2630 #ifdef HAVE_X_I18N
2631 FRAME_XIC (f) = NULL;
2632 if (use_xim)
2634 BLOCK_INPUT;
2635 create_frame_xic (f);
2636 if (FRAME_XIC (f))
2638 /* XIM server might require some X events. */
2639 unsigned long fevent = NoEventMask;
2640 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2642 if (fevent != NoEventMask)
2644 XSetWindowAttributes attributes;
2645 XWindowAttributes wattr;
2646 unsigned long attribute_mask;
2648 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2649 &wattr);
2650 attributes.event_mask = wattr.your_event_mask | fevent;
2651 attribute_mask = CWEventMask;
2652 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2653 attribute_mask, &attributes);
2656 UNBLOCK_INPUT;
2658 #endif
2661 #else /*! USE_GTK */
2662 /* Create and set up the X window for frame F. */
2664 void
2665 x_window (f)
2666 struct frame *f;
2669 XClassHint class_hints;
2670 XSetWindowAttributes attributes;
2671 unsigned long attribute_mask;
2673 attributes.background_pixel = f->output_data.x->background_pixel;
2674 attributes.border_pixel = f->output_data.x->border_pixel;
2675 attributes.bit_gravity = StaticGravity;
2676 attributes.backing_store = NotUseful;
2677 attributes.save_under = True;
2678 attributes.event_mask = STANDARD_EVENT_SET;
2679 attributes.colormap = FRAME_X_COLORMAP (f);
2680 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2681 | CWColormap);
2683 BLOCK_INPUT;
2684 FRAME_X_WINDOW (f)
2685 = XCreateWindow (FRAME_X_DISPLAY (f),
2686 f->output_data.x->parent_desc,
2687 f->left_pos,
2688 f->top_pos,
2689 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2690 f->border_width,
2691 CopyFromParent, /* depth */
2692 InputOutput, /* class */
2693 FRAME_X_VISUAL (f),
2694 attribute_mask, &attributes);
2696 #ifdef HAVE_X_I18N
2697 if (use_xim)
2699 create_frame_xic (f);
2700 if (FRAME_XIC (f))
2702 /* XIM server might require some X events. */
2703 unsigned long fevent = NoEventMask;
2704 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2705 attributes.event_mask |= fevent;
2706 attribute_mask = CWEventMask;
2707 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2708 attribute_mask, &attributes);
2711 #endif /* HAVE_X_I18N */
2713 validate_x_resource_name ();
2715 class_hints.res_name = (char *) SDATA (Vx_resource_name);
2716 class_hints.res_class = (char *) SDATA (Vx_resource_class);
2717 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2719 /* The menubar is part of the ordinary display;
2720 it does not count in addition to the height of the window. */
2721 f->output_data.x->menubar_height = 0;
2723 /* This indicates that we use the "Passive Input" input model.
2724 Unless we do this, we don't get the Focus{In,Out} events that we
2725 need to draw the cursor correctly. Accursed bureaucrats.
2726 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2728 f->output_data.x->wm_hints.input = True;
2729 f->output_data.x->wm_hints.flags |= InputHint;
2730 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2731 &f->output_data.x->wm_hints);
2732 f->output_data.x->wm_hints.icon_pixmap = None;
2734 /* Request "save yourself" and "delete window" commands from wm. */
2736 Atom protocols[2];
2737 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2738 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2739 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2742 /* x_set_name normally ignores requests to set the name if the
2743 requested name is the same as the current name. This is the one
2744 place where that assumption isn't correct; f->name is set, but
2745 the X server hasn't been told. */
2747 Lisp_Object name;
2748 int explicit = f->explicit_name;
2750 f->explicit_name = 0;
2751 name = f->name;
2752 f->name = Qnil;
2753 x_set_name (f, name, explicit);
2756 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2757 f->output_data.x->text_cursor);
2759 UNBLOCK_INPUT;
2761 if (FRAME_X_WINDOW (f) == 0)
2762 error ("Unable to create window");
2765 #endif /* not USE_GTK */
2766 #endif /* not USE_X_TOOLKIT */
2768 /* Verify that the icon position args for this window are valid. */
2770 static void
2771 x_icon_verify (f, parms)
2772 struct frame *f;
2773 Lisp_Object parms;
2775 Lisp_Object icon_x, icon_y;
2777 /* Set the position of the icon. Note that twm groups all
2778 icons in an icon window. */
2779 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2780 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2781 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2783 CHECK_NUMBER (icon_x);
2784 CHECK_NUMBER (icon_y);
2786 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2787 error ("Both left and top icon corners of icon must be specified");
2790 /* Handle the icon stuff for this window. Perhaps later we might
2791 want an x_set_icon_position which can be called interactively as
2792 well. */
2794 static void
2795 x_icon (f, parms)
2796 struct frame *f;
2797 Lisp_Object parms;
2799 Lisp_Object icon_x, icon_y;
2800 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2802 /* Set the position of the icon. Note that twm groups all
2803 icons in an icon window. */
2804 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2805 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2806 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2808 CHECK_NUMBER (icon_x);
2809 CHECK_NUMBER (icon_y);
2811 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2812 error ("Both left and top icon corners of icon must be specified");
2814 BLOCK_INPUT;
2816 if (! EQ (icon_x, Qunbound))
2817 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2819 /* Start up iconic or window? */
2820 x_wm_set_window_state
2821 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2822 Qicon)
2823 ? IconicState
2824 : NormalState));
2826 x_text_icon (f, (char *) SDATA ((!NILP (f->icon_name)
2827 ? f->icon_name
2828 : f->name)));
2830 UNBLOCK_INPUT;
2833 /* Make the GCs needed for this window, setting the
2834 background, border and mouse colors; also create the
2835 mouse cursor and the gray border tile. */
2837 static char cursor_bits[] =
2839 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2840 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2841 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2842 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
2845 static void
2846 x_make_gc (f)
2847 struct frame *f;
2849 XGCValues gc_values;
2851 BLOCK_INPUT;
2853 /* Create the GCs of this frame.
2854 Note that many default values are used. */
2856 /* Normal video */
2857 gc_values.font = FRAME_FONT (f)->fid;
2858 gc_values.foreground = f->output_data.x->foreground_pixel;
2859 gc_values.background = f->output_data.x->background_pixel;
2860 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2861 f->output_data.x->normal_gc
2862 = XCreateGC (FRAME_X_DISPLAY (f),
2863 FRAME_X_WINDOW (f),
2864 GCLineWidth | GCFont | GCForeground | GCBackground,
2865 &gc_values);
2867 /* Reverse video style. */
2868 gc_values.foreground = f->output_data.x->background_pixel;
2869 gc_values.background = f->output_data.x->foreground_pixel;
2870 f->output_data.x->reverse_gc
2871 = XCreateGC (FRAME_X_DISPLAY (f),
2872 FRAME_X_WINDOW (f),
2873 GCFont | GCForeground | GCBackground | GCLineWidth,
2874 &gc_values);
2876 /* Cursor has cursor-color background, background-color foreground. */
2877 gc_values.foreground = f->output_data.x->background_pixel;
2878 gc_values.background = f->output_data.x->cursor_pixel;
2879 gc_values.fill_style = FillOpaqueStippled;
2880 gc_values.stipple
2881 = XCreateBitmapFromData (FRAME_X_DISPLAY (f),
2882 FRAME_X_DISPLAY_INFO (f)->root_window,
2883 cursor_bits, 16, 16);
2884 f->output_data.x->cursor_gc
2885 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2886 (GCFont | GCForeground | GCBackground
2887 | GCFillStyle /* | GCStipple */ | GCLineWidth),
2888 &gc_values);
2890 /* Reliefs. */
2891 f->output_data.x->white_relief.gc = 0;
2892 f->output_data.x->black_relief.gc = 0;
2894 /* Create the gray border tile used when the pointer is not in
2895 the frame. Since this depends on the frame's pixel values,
2896 this must be done on a per-frame basis. */
2897 f->output_data.x->border_tile
2898 = (XCreatePixmapFromBitmapData
2899 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2900 gray_bits, gray_width, gray_height,
2901 f->output_data.x->foreground_pixel,
2902 f->output_data.x->background_pixel,
2903 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2905 UNBLOCK_INPUT;
2909 /* Free what was was allocated in x_make_gc. */
2911 void
2912 x_free_gcs (f)
2913 struct frame *f;
2915 Display *dpy = FRAME_X_DISPLAY (f);
2917 BLOCK_INPUT;
2919 if (f->output_data.x->normal_gc)
2921 XFreeGC (dpy, f->output_data.x->normal_gc);
2922 f->output_data.x->normal_gc = 0;
2925 if (f->output_data.x->reverse_gc)
2927 XFreeGC (dpy, f->output_data.x->reverse_gc);
2928 f->output_data.x->reverse_gc = 0;
2931 if (f->output_data.x->cursor_gc)
2933 XFreeGC (dpy, f->output_data.x->cursor_gc);
2934 f->output_data.x->cursor_gc = 0;
2937 if (f->output_data.x->border_tile)
2939 XFreePixmap (dpy, f->output_data.x->border_tile);
2940 f->output_data.x->border_tile = 0;
2943 UNBLOCK_INPUT;
2947 /* Handler for signals raised during x_create_frame and
2948 x_create_top_frame. FRAME is the frame which is partially
2949 constructed. */
2951 static Lisp_Object
2952 unwind_create_frame (frame)
2953 Lisp_Object frame;
2955 struct frame *f = XFRAME (frame);
2957 /* If frame is ``official'', nothing to do. */
2958 if (!CONSP (Vframe_list) || !EQ (XCAR (Vframe_list), frame))
2960 #if GLYPH_DEBUG
2961 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2962 #endif
2964 x_free_frame_resources (f);
2966 #if GLYPH_DEBUG
2967 /* Check that reference counts are indeed correct. */
2968 xassert (dpyinfo->reference_count == dpyinfo_refcount);
2969 xassert (dpyinfo->image_cache->refcount == image_cache_refcount);
2970 #endif
2971 return Qt;
2974 return Qnil;
2978 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2979 1, 1, 0,
2980 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2981 Returns an Emacs frame object.
2982 ALIST is an alist of frame parameters.
2983 If the parameters specify that the frame should not have a minibuffer,
2984 and do not specify a specific minibuffer window to use,
2985 then `default-minibuffer-frame' must be a frame whose minibuffer can
2986 be shared by the new frame.
2988 This function is an internal primitive--use `make-frame' instead. */)
2989 (parms)
2990 Lisp_Object parms;
2992 struct frame *f;
2993 Lisp_Object frame, tem;
2994 Lisp_Object name;
2995 int minibuffer_only = 0;
2996 long window_prompting = 0;
2997 int width, height;
2998 int count = SPECPDL_INDEX ();
2999 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3000 Lisp_Object display;
3001 struct x_display_info *dpyinfo = NULL;
3002 Lisp_Object parent;
3003 struct kboard *kb;
3005 check_x ();
3007 /* Use this general default value to start with
3008 until we know if this frame has a specified name. */
3009 Vx_resource_name = Vinvocation_name;
3011 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3012 if (EQ (display, Qunbound))
3013 display = Qnil;
3014 dpyinfo = check_x_display_info (display);
3015 #ifdef MULTI_KBOARD
3016 kb = dpyinfo->kboard;
3017 #else
3018 kb = &the_only_kboard;
3019 #endif
3021 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3022 if (!STRINGP (name)
3023 && ! EQ (name, Qunbound)
3024 && ! NILP (name))
3025 error ("Invalid frame name--not a string or nil");
3027 if (STRINGP (name))
3028 Vx_resource_name = name;
3030 /* See if parent window is specified. */
3031 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3032 if (EQ (parent, Qunbound))
3033 parent = Qnil;
3034 if (! NILP (parent))
3035 CHECK_NUMBER (parent);
3037 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3038 /* No need to protect DISPLAY because that's not used after passing
3039 it to make_frame_without_minibuffer. */
3040 frame = Qnil;
3041 GCPRO4 (parms, parent, name, frame);
3042 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3043 RES_TYPE_SYMBOL);
3044 if (EQ (tem, Qnone) || NILP (tem))
3045 f = make_frame_without_minibuffer (Qnil, kb, display);
3046 else if (EQ (tem, Qonly))
3048 f = make_minibuffer_frame ();
3049 minibuffer_only = 1;
3051 else if (WINDOWP (tem))
3052 f = make_frame_without_minibuffer (tem, kb, display);
3053 else
3054 f = make_frame (1);
3056 XSETFRAME (frame, f);
3058 /* Note that X Windows does support scroll bars. */
3059 FRAME_CAN_HAVE_SCROLL_BARS (f) = 1;
3061 f->output_method = output_x_window;
3062 f->output_data.x = (struct x_output *) xmalloc (sizeof (struct x_output));
3063 bzero (f->output_data.x, sizeof (struct x_output));
3064 f->output_data.x->icon_bitmap = -1;
3065 FRAME_FONTSET (f) = -1;
3066 f->output_data.x->scroll_bar_foreground_pixel = -1;
3067 f->output_data.x->scroll_bar_background_pixel = -1;
3068 #ifdef USE_TOOLKIT_SCROLL_BARS
3069 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3070 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3071 #endif /* USE_TOOLKIT_SCROLL_BARS */
3072 record_unwind_protect (unwind_create_frame, frame);
3074 f->icon_name
3075 = x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3076 RES_TYPE_STRING);
3077 if (! STRINGP (f->icon_name))
3078 f->icon_name = Qnil;
3080 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3081 #if GLYPH_DEBUG
3082 image_cache_refcount = FRAME_X_IMAGE_CACHE (f)->refcount;
3083 dpyinfo_refcount = dpyinfo->reference_count;
3084 #endif /* GLYPH_DEBUG */
3085 #ifdef MULTI_KBOARD
3086 FRAME_KBOARD (f) = kb;
3087 #endif
3089 /* These colors will be set anyway later, but it's important
3090 to get the color reference counts right, so initialize them! */
3092 Lisp_Object black;
3093 struct gcpro gcpro1;
3095 /* Function x_decode_color can signal an error. Make
3096 sure to initialize color slots so that we won't try
3097 to free colors we haven't allocated. */
3098 f->output_data.x->foreground_pixel = -1;
3099 f->output_data.x->background_pixel = -1;
3100 f->output_data.x->cursor_pixel = -1;
3101 f->output_data.x->cursor_foreground_pixel = -1;
3102 f->output_data.x->border_pixel = -1;
3103 f->output_data.x->mouse_pixel = -1;
3105 black = build_string ("black");
3106 GCPRO1 (black);
3107 f->output_data.x->foreground_pixel
3108 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3109 f->output_data.x->background_pixel
3110 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3111 f->output_data.x->cursor_pixel
3112 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3113 f->output_data.x->cursor_foreground_pixel
3114 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3115 f->output_data.x->border_pixel
3116 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3117 f->output_data.x->mouse_pixel
3118 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3119 UNGCPRO;
3122 /* Specify the parent under which to make this X window. */
3124 if (!NILP (parent))
3126 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3127 f->output_data.x->explicit_parent = 1;
3129 else
3131 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3132 f->output_data.x->explicit_parent = 0;
3135 /* Set the name; the functions to which we pass f expect the name to
3136 be set. */
3137 if (EQ (name, Qunbound) || NILP (name))
3139 f->name = build_string (dpyinfo->x_id_name);
3140 f->explicit_name = 0;
3142 else
3144 f->name = name;
3145 f->explicit_name = 1;
3146 /* use the frame's title when getting resources for this frame. */
3147 specbind (Qx_resource_name, name);
3150 /* Extract the window parameters from the supplied values
3151 that are needed to determine window geometry. */
3153 Lisp_Object font;
3155 font = x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
3157 BLOCK_INPUT;
3158 /* First, try whatever font the caller has specified. */
3159 if (STRINGP (font))
3161 tem = Fquery_fontset (font, Qnil);
3162 if (STRINGP (tem))
3163 font = x_new_fontset (f, SDATA (tem));
3164 else
3165 font = x_new_font (f, SDATA (font));
3168 /* Try out a font which we hope has bold and italic variations. */
3169 if (!STRINGP (font))
3170 font = x_new_font (f, "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1");
3171 if (!STRINGP (font))
3172 font = x_new_font (f, "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
3173 if (! STRINGP (font))
3174 font = x_new_font (f, "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1");
3175 if (! STRINGP (font))
3176 /* This was formerly the first thing tried, but it finds too many fonts
3177 and takes too long. */
3178 font = x_new_font (f, "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1");
3179 /* If those didn't work, look for something which will at least work. */
3180 if (! STRINGP (font))
3181 font = x_new_font (f, "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1");
3182 UNBLOCK_INPUT;
3183 if (! STRINGP (font))
3184 font = build_string ("fixed");
3186 x_default_parameter (f, parms, Qfont, font,
3187 "font", "Font", RES_TYPE_STRING);
3190 #ifdef USE_LUCID
3191 /* Prevent lwlib/xlwmenu.c from crashing because of a bug
3192 whereby it fails to get any font. */
3193 xlwmenu_default_font = FRAME_FONT (f);
3194 #endif
3196 x_default_parameter (f, parms, Qborder_width, make_number (2),
3197 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3199 /* This defaults to 1 in order to match xterm. We recognize either
3200 internalBorderWidth or internalBorder (which is what xterm calls
3201 it). */
3202 if (NILP (Fassq (Qinternal_border_width, parms)))
3204 Lisp_Object value;
3206 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3207 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3208 if (! EQ (value, Qunbound))
3209 parms = Fcons (Fcons (Qinternal_border_width, value),
3210 parms);
3212 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
3213 "internalBorderWidth", "internalBorderWidth",
3214 RES_TYPE_NUMBER);
3215 x_default_parameter (f, parms, Qvertical_scroll_bars, Qleft,
3216 "verticalScrollBars", "ScrollBars",
3217 RES_TYPE_SYMBOL);
3219 /* Also do the stuff which must be set before the window exists. */
3220 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3221 "foreground", "Foreground", RES_TYPE_STRING);
3222 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3223 "background", "Background", RES_TYPE_STRING);
3224 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3225 "pointerColor", "Foreground", RES_TYPE_STRING);
3226 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
3227 "cursorColor", "Foreground", RES_TYPE_STRING);
3228 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3229 "borderColor", "BorderColor", RES_TYPE_STRING);
3230 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3231 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3232 x_default_parameter (f, parms, Qline_spacing, Qnil,
3233 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3234 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3235 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3236 x_default_parameter (f, parms, Qright_fringe, Qnil,
3237 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3239 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3240 "scrollBarForeground",
3241 "ScrollBarForeground", 1);
3242 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3243 "scrollBarBackground",
3244 "ScrollBarBackground", 0);
3246 /* Init faces before x_default_parameter is called for scroll-bar
3247 parameters because that function calls x_set_scroll_bar_width,
3248 which calls change_frame_size, which calls Fset_window_buffer,
3249 which runs hooks, which call Fvertical_motion. At the end, we
3250 end up in init_iterator with a null face cache, which should not
3251 happen. */
3252 init_frame_faces (f);
3254 x_default_parameter (f, parms, Qmenu_bar_lines, make_number (1),
3255 "menuBar", "MenuBar", RES_TYPE_NUMBER);
3256 x_default_parameter (f, parms, Qtool_bar_lines, make_number (1),
3257 "toolBar", "ToolBar", RES_TYPE_NUMBER);
3258 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3259 "bufferPredicate", "BufferPredicate",
3260 RES_TYPE_SYMBOL);
3261 x_default_parameter (f, parms, Qtitle, Qnil,
3262 "title", "Title", RES_TYPE_STRING);
3263 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3264 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3265 x_default_parameter (f, parms, Qfullscreen, Qnil,
3266 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3268 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3270 /* Compute the size of the X window. */
3271 window_prompting = x_figure_window_size (f, parms, 1);
3273 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3274 f->no_split = minibuffer_only || EQ (tem, Qt);
3276 x_icon_verify (f, parms);
3278 /* Create the X widget or window. */
3279 #ifdef USE_X_TOOLKIT
3280 x_window (f, window_prompting, minibuffer_only);
3281 #else
3282 x_window (f);
3283 #endif
3285 x_icon (f, parms);
3286 x_make_gc (f);
3288 /* Now consider the frame official. */
3289 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3290 Vframe_list = Fcons (frame, Vframe_list);
3292 /* We need to do this after creating the X window, so that the
3293 icon-creation functions can say whose icon they're describing. */
3294 x_default_parameter (f, parms, Qicon_type, Qt,
3295 "bitmapIcon", "BitmapIcon", RES_TYPE_SYMBOL);
3297 x_default_parameter (f, parms, Qauto_raise, Qnil,
3298 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3299 x_default_parameter (f, parms, Qauto_lower, Qnil,
3300 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3301 x_default_parameter (f, parms, Qcursor_type, Qbox,
3302 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3303 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3304 "scrollBarWidth", "ScrollBarWidth",
3305 RES_TYPE_NUMBER);
3307 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3308 Change will not be effected unless different from the current
3309 FRAME_LINES (f). */
3310 width = FRAME_COLS (f);
3311 height = FRAME_LINES (f);
3313 SET_FRAME_COLS (f, 0);
3314 FRAME_LINES (f) = 0;
3315 change_frame_size (f, height, width, 1, 0, 0);
3317 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3318 /* Create the menu bar. */
3319 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3321 /* If this signals an error, we haven't set size hints for the
3322 frame and we didn't make it visible. */
3323 initialize_frame_menubar (f);
3325 #ifndef USE_GTK
3326 /* This is a no-op, except under Motif where it arranges the
3327 main window for the widgets on it. */
3328 lw_set_main_areas (f->output_data.x->column_widget,
3329 f->output_data.x->menubar_widget,
3330 f->output_data.x->edit_widget);
3331 #endif /* not USE_GTK */
3333 #endif /* USE_X_TOOLKIT || USE_GTK */
3335 /* Tell the server what size and position, etc, we want, and how
3336 badly we want them. This should be done after we have the menu
3337 bar so that its size can be taken into account. */
3338 BLOCK_INPUT;
3339 x_wm_set_size_hint (f, window_prompting, 0);
3340 UNBLOCK_INPUT;
3342 /* Make the window appear on the frame and enable display, unless
3343 the caller says not to. However, with explicit parent, Emacs
3344 cannot control visibility, so don't try. */
3345 if (! f->output_data.x->explicit_parent)
3347 Lisp_Object visibility;
3349 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3350 RES_TYPE_SYMBOL);
3351 if (EQ (visibility, Qunbound))
3352 visibility = Qt;
3354 if (EQ (visibility, Qicon))
3355 x_iconify_frame (f);
3356 else if (! NILP (visibility))
3357 x_make_frame_visible (f);
3358 else
3359 /* Must have been Qnil. */
3363 /* Set the WM leader property. GTK does this itself, so this is not
3364 needed when using GTK. */
3365 if (dpyinfo->client_leader_window != 0)
3367 BLOCK_INPUT;
3368 XChangeProperty (FRAME_X_DISPLAY (f),
3369 FRAME_OUTER_WINDOW (f),
3370 dpyinfo->Xatom_wm_client_leader,
3371 XA_WINDOW, 32, PropModeReplace,
3372 (unsigned char *) &dpyinfo->client_leader_window, 1);
3373 UNBLOCK_INPUT;
3376 /* Initialize `default-minibuffer-frame' in case this is the first
3377 frame on this display device. */
3378 if (FRAME_HAS_MINIBUF_P (f)
3379 && (!FRAMEP (kb->Vdefault_minibuffer_frame)
3380 || !FRAME_LIVE_P (XFRAME (kb->Vdefault_minibuffer_frame))))
3381 kb->Vdefault_minibuffer_frame = frame;
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 if (*root_y + XINT (dy) >= FRAME_X_DISPLAY_INFO (f)->height)
4946 /* Put tip above the pointer. */
4947 *root_y -= XINT (dy);
4948 else
4950 *root_y -= height;
4951 *root_y += XINT (dy);
4954 if (INTEGERP (left))
4955 *root_x = XINT (left);
4956 else if (*root_x + XINT (dx) + width <= FRAME_X_DISPLAY_INFO (f)->width)
4957 /* It fits to the right of the pointer. */
4958 *root_x += XINT (dx);
4959 else if (width + XINT (dx) <= *root_x)
4960 /* It fits to the left of the pointer. */
4961 *root_x -= width + XINT (dx);
4962 else
4963 /* Put it left-justified on the screen--it ought to fit that way. */
4964 *root_x = 0;
4968 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
4969 doc: /* Show STRING in a "tooltip" window on frame FRAME.
4970 A tooltip window is a small X window displaying a string.
4972 FRAME nil or omitted means use the selected frame.
4974 PARMS is an optional list of frame parameters which can be used to
4975 change the tooltip's appearance.
4977 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
4978 means use the default timeout of 5 seconds.
4980 If the list of frame parameters PARAMS contains a `left' parameters,
4981 the tooltip is displayed at that x-position. Otherwise it is
4982 displayed at the mouse position, with offset DX added (default is 5 if
4983 DX isn't specified). Likewise for the y-position; if a `top' frame
4984 parameter is specified, it determines the y-position of the tooltip
4985 window, otherwise it is displayed at the mouse position, with offset
4986 DY added (default is -10).
4988 A tooltip's maximum size is specified by `x-max-tooltip-size'.
4989 Text larger than the specified size is clipped. */)
4990 (string, frame, parms, timeout, dx, dy)
4991 Lisp_Object string, frame, parms, timeout, dx, dy;
4993 struct frame *f;
4994 struct window *w;
4995 int root_x, root_y;
4996 struct buffer *old_buffer;
4997 struct text_pos pos;
4998 int i, width, height;
4999 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
5000 int old_windows_or_buffers_changed = windows_or_buffers_changed;
5001 int count = SPECPDL_INDEX ();
5003 specbind (Qinhibit_redisplay, Qt);
5005 GCPRO4 (string, parms, frame, timeout);
5007 CHECK_STRING (string);
5008 f = check_x_frame (frame);
5009 if (NILP (timeout))
5010 timeout = make_number (5);
5011 else
5012 CHECK_NATNUM (timeout);
5014 if (NILP (dx))
5015 dx = make_number (5);
5016 else
5017 CHECK_NUMBER (dx);
5019 if (NILP (dy))
5020 dy = make_number (-10);
5021 else
5022 CHECK_NUMBER (dy);
5024 if (NILP (last_show_tip_args))
5025 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
5027 if (!NILP (tip_frame))
5029 Lisp_Object last_string = AREF (last_show_tip_args, 0);
5030 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
5031 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
5033 if (EQ (frame, last_frame)
5034 && !NILP (Fequal (last_string, string))
5035 && !NILP (Fequal (last_parms, parms)))
5037 struct frame *f = XFRAME (tip_frame);
5039 /* Only DX and DY have changed. */
5040 if (!NILP (tip_timer))
5042 Lisp_Object timer = tip_timer;
5043 tip_timer = Qnil;
5044 call1 (Qcancel_timer, timer);
5047 BLOCK_INPUT;
5048 compute_tip_xy (f, parms, dx, dy, FRAME_PIXEL_WIDTH (f),
5049 FRAME_PIXEL_HEIGHT (f), &root_x, &root_y);
5050 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5051 root_x, root_y);
5052 UNBLOCK_INPUT;
5053 goto start_timer;
5057 /* Hide a previous tip, if any. */
5058 Fx_hide_tip ();
5060 ASET (last_show_tip_args, 0, string);
5061 ASET (last_show_tip_args, 1, frame);
5062 ASET (last_show_tip_args, 2, parms);
5064 /* Add default values to frame parameters. */
5065 if (NILP (Fassq (Qname, parms)))
5066 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
5067 if (NILP (Fassq (Qinternal_border_width, parms)))
5068 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
5069 if (NILP (Fassq (Qborder_width, parms)))
5070 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
5071 if (NILP (Fassq (Qborder_color, parms)))
5072 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
5073 if (NILP (Fassq (Qbackground_color, parms)))
5074 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
5075 parms);
5077 /* Create a frame for the tooltip, and record it in the global
5078 variable tip_frame. */
5079 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
5080 f = XFRAME (frame);
5082 /* Set up the frame's root window. */
5083 w = XWINDOW (FRAME_ROOT_WINDOW (f));
5084 w->left_col = w->top_line = make_number (0);
5086 if (CONSP (Vx_max_tooltip_size)
5087 && INTEGERP (XCAR (Vx_max_tooltip_size))
5088 && XINT (XCAR (Vx_max_tooltip_size)) > 0
5089 && INTEGERP (XCDR (Vx_max_tooltip_size))
5090 && XINT (XCDR (Vx_max_tooltip_size)) > 0)
5092 w->total_cols = XCAR (Vx_max_tooltip_size);
5093 w->total_lines = XCDR (Vx_max_tooltip_size);
5095 else
5097 w->total_cols = make_number (80);
5098 w->total_lines = make_number (40);
5101 FRAME_TOTAL_COLS (f) = XINT (w->total_cols);
5102 adjust_glyphs (f);
5103 w->pseudo_window_p = 1;
5105 /* Display the tooltip text in a temporary buffer. */
5106 old_buffer = current_buffer;
5107 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->buffer));
5108 current_buffer->truncate_lines = Qnil;
5109 clear_glyph_matrix (w->desired_matrix);
5110 clear_glyph_matrix (w->current_matrix);
5111 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5112 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5114 /* Compute width and height of the tooltip. */
5115 width = height = 0;
5116 for (i = 0; i < w->desired_matrix->nrows; ++i)
5118 struct glyph_row *row = &w->desired_matrix->rows[i];
5119 struct glyph *last;
5120 int row_width;
5122 /* Stop at the first empty row at the end. */
5123 if (!row->enabled_p || !row->displays_text_p)
5124 break;
5126 /* Let the row go over the full width of the frame. */
5127 row->full_width_p = 1;
5129 /* There's a glyph at the end of rows that is used to place
5130 the cursor there. Don't include the width of this glyph. */
5131 if (row->used[TEXT_AREA])
5133 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5134 row_width = row->pixel_width - last->pixel_width;
5136 else
5137 row_width = row->pixel_width;
5139 height += row->height;
5140 width = max (width, row_width);
5143 /* Add the frame's internal border to the width and height the X
5144 window should have. */
5145 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5146 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5148 /* Move the tooltip window where the mouse pointer is. Resize and
5149 show it. */
5150 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5152 BLOCK_INPUT;
5153 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5154 root_x, root_y, width, height);
5155 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5156 UNBLOCK_INPUT;
5158 /* Draw into the window. */
5159 w->must_be_updated_p = 1;
5160 update_single_window (w, 1);
5162 /* Restore original current buffer. */
5163 set_buffer_internal_1 (old_buffer);
5164 windows_or_buffers_changed = old_windows_or_buffers_changed;
5166 start_timer:
5167 /* Let the tip disappear after timeout seconds. */
5168 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5169 intern ("x-hide-tip"));
5171 UNGCPRO;
5172 return unbind_to (count, Qnil);
5176 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5177 doc: /* Hide the current tooltip window, if there is any.
5178 Value is t if tooltip was open, nil otherwise. */)
5181 int count;
5182 Lisp_Object deleted, frame, timer;
5183 struct gcpro gcpro1, gcpro2;
5185 /* Return quickly if nothing to do. */
5186 if (NILP (tip_timer) && NILP (tip_frame))
5187 return Qnil;
5189 frame = tip_frame;
5190 timer = tip_timer;
5191 GCPRO2 (frame, timer);
5192 tip_frame = tip_timer = deleted = Qnil;
5194 count = SPECPDL_INDEX ();
5195 specbind (Qinhibit_redisplay, Qt);
5196 specbind (Qinhibit_quit, Qt);
5198 if (!NILP (timer))
5199 call1 (Qcancel_timer, timer);
5201 if (FRAMEP (frame))
5203 Fdelete_frame (frame, Qnil);
5204 deleted = Qt;
5206 #ifdef USE_LUCID
5207 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5208 redisplay procedure is not called when a tip frame over menu
5209 items is unmapped. Redisplay the menu manually... */
5211 struct frame *f = SELECTED_FRAME ();
5212 Widget w = f->output_data.x->menubar_widget;
5213 extern void xlwmenu_redisplay P_ ((Widget));
5215 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5216 && w != NULL)
5218 BLOCK_INPUT;
5219 xlwmenu_redisplay (w);
5220 UNBLOCK_INPUT;
5223 #endif /* USE_LUCID */
5226 UNGCPRO;
5227 return unbind_to (count, deleted);
5232 /***********************************************************************
5233 File selection dialog
5234 ***********************************************************************/
5236 #ifdef USE_MOTIF
5238 /* Callback for "OK" and "Cancel" on file selection dialog. */
5240 static void
5241 file_dialog_cb (widget, client_data, call_data)
5242 Widget widget;
5243 XtPointer call_data, client_data;
5245 int *result = (int *) client_data;
5246 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5247 *result = cb->reason;
5251 /* Callback for unmapping a file selection dialog. This is used to
5252 capture the case where a dialog is closed via a window manager's
5253 closer button, for example. Using a XmNdestroyCallback didn't work
5254 in this case. */
5256 static void
5257 file_dialog_unmap_cb (widget, client_data, call_data)
5258 Widget widget;
5259 XtPointer call_data, client_data;
5261 int *result = (int *) client_data;
5262 *result = XmCR_CANCEL;
5265 static Lisp_Object
5266 clean_up_file_dialog (arg)
5267 Lisp_Object arg;
5269 struct Lisp_Save_Value *p = XSAVE_VALUE (arg);
5270 Widget dialog = (Widget) p->pointer;
5272 /* Clean up. */
5273 BLOCK_INPUT;
5274 XtUnmanageChild (dialog);
5275 XtDestroyWidget (dialog);
5276 x_menu_set_in_use (0);
5277 UNBLOCK_INPUT;
5279 return Qnil;
5283 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5284 doc: /* Read file name, prompting with PROMPT in directory DIR.
5285 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5286 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5287 or directory must exist. ONLY-DIR-P is ignored." */)
5288 (prompt, dir, default_filename, mustmatch, only_dir_p)
5289 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
5291 int result;
5292 struct frame *f = SELECTED_FRAME ();
5293 Lisp_Object file = Qnil;
5294 Widget dialog, text, help;
5295 Arg al[10];
5296 int ac = 0;
5297 extern XtAppContext Xt_app_con;
5298 XmString dir_xmstring, pattern_xmstring;
5299 int count = SPECPDL_INDEX ();
5300 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5302 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5304 if (popup_activated ())
5305 error ("Trying to use a menu from within a menu-entry");
5307 CHECK_STRING (prompt);
5308 CHECK_STRING (dir);
5310 /* Prevent redisplay. */
5311 specbind (Qinhibit_redisplay, Qt);
5313 BLOCK_INPUT;
5315 /* Create the dialog with PROMPT as title, using DIR as initial
5316 directory and using "*" as pattern. */
5317 dir = Fexpand_file_name (dir, Qnil);
5318 dir_xmstring = XmStringCreateLocalized (SDATA (dir));
5319 pattern_xmstring = XmStringCreateLocalized ("*");
5321 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5322 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5323 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5324 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5325 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5326 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5327 "fsb", al, ac);
5328 XmStringFree (dir_xmstring);
5329 XmStringFree (pattern_xmstring);
5331 /* Add callbacks for OK and Cancel. */
5332 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5333 (XtPointer) &result);
5334 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5335 (XtPointer) &result);
5336 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5337 (XtPointer) &result);
5339 /* Remove the help button since we can't display help. */
5340 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5341 XtUnmanageChild (help);
5343 /* Mark OK button as default. */
5344 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5345 XmNshowAsDefault, True, NULL);
5347 /* If MUSTMATCH is non-nil, disable the file entry field of the
5348 dialog, so that the user must select a file from the files list
5349 box. We can't remove it because we wouldn't have a way to get at
5350 the result file name, then. */
5351 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5352 if (!NILP (mustmatch))
5354 Widget label;
5355 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5356 XtSetSensitive (text, False);
5357 XtSetSensitive (label, False);
5360 /* Manage the dialog, so that list boxes get filled. */
5361 XtManageChild (dialog);
5363 if (STRINGP (default_filename))
5365 XmString default_xmstring;
5366 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5367 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5369 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5370 XmTextFieldReplace (wtext, 0, last_pos,
5371 (SDATA (Ffile_name_nondirectory (default_filename))));
5373 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5374 must include the path for this to work. */
5376 default_xmstring = XmStringCreateLocalized (SDATA (default_filename));
5378 if (XmListItemExists (list, default_xmstring))
5380 int item_pos = XmListItemPos (list, default_xmstring);
5381 /* Select the item and scroll it into view. */
5382 XmListSelectPos (list, item_pos, True);
5383 XmListSetPos (list, item_pos);
5386 XmStringFree (default_xmstring);
5389 record_unwind_protect (clean_up_file_dialog, make_save_value (dialog, 0));
5391 /* Process events until the user presses Cancel or OK. */
5392 x_menu_set_in_use (1);
5393 result = 0;
5394 while (result == 0)
5396 XEvent event;
5397 x_menu_wait_for_event (0);
5398 XtAppNextEvent (Xt_app_con, &event);
5399 if (event.type == KeyPress
5400 && FRAME_X_DISPLAY (f) == event.xkey.display)
5402 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5404 /* Pop down on C-g. */
5405 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5406 XtUnmanageChild (dialog);
5409 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5412 /* Get the result. */
5413 if (result == XmCR_OK)
5415 XmString text;
5416 String data;
5418 XtVaGetValues (dialog, XmNtextString, &text, NULL);
5419 XmStringGetLtoR (text, XmFONTLIST_DEFAULT_TAG, &data);
5420 XmStringFree (text);
5421 file = build_string (data);
5422 XtFree (data);
5424 else
5425 file = Qnil;
5427 UNBLOCK_INPUT;
5428 UNGCPRO;
5430 /* Make "Cancel" equivalent to C-g. */
5431 if (NILP (file))
5432 Fsignal (Qquit, Qnil);
5434 return unbind_to (count, file);
5437 #endif /* USE_MOTIF */
5439 #ifdef USE_GTK
5441 static Lisp_Object
5442 clean_up_dialog (arg)
5443 Lisp_Object arg;
5445 x_menu_set_in_use (0);
5447 return Qnil;
5450 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5451 doc: /* Read file name, prompting with PROMPT in directory DIR.
5452 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5453 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5454 or directory must exist. If ONLY-DIR-P is non-nil, the user can only select
5455 directories. */)
5456 (prompt, dir, default_filename, mustmatch, only_dir_p)
5457 Lisp_Object prompt, dir, default_filename, mustmatch, only_dir_p;
5459 FRAME_PTR f = SELECTED_FRAME ();
5460 char *fn;
5461 Lisp_Object file = Qnil;
5462 int count = SPECPDL_INDEX ();
5463 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5464 char *cdef_file;
5466 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5468 if (popup_activated ())
5469 error ("Trying to use a menu from within a menu-entry");
5471 CHECK_STRING (prompt);
5472 CHECK_STRING (dir);
5474 /* Prevent redisplay. */
5475 specbind (Qinhibit_redisplay, Qt);
5476 record_unwind_protect (clean_up_dialog, Qnil);
5478 BLOCK_INPUT;
5480 if (STRINGP (default_filename))
5481 cdef_file = SDATA (default_filename);
5482 else
5483 cdef_file = SDATA (dir);
5485 fn = xg_get_file_name (f, SDATA (prompt), cdef_file,
5486 ! NILP (mustmatch),
5487 ! NILP (only_dir_p));
5489 if (fn)
5491 file = build_string (fn);
5492 xfree (fn);
5495 UNBLOCK_INPUT;
5496 UNGCPRO;
5498 /* Make "Cancel" equivalent to C-g. */
5499 if (NILP (file))
5500 Fsignal (Qquit, Qnil);
5502 return unbind_to (count, file);
5505 #endif /* USE_GTK */
5508 /***********************************************************************
5509 Keyboard
5510 ***********************************************************************/
5512 #ifdef HAVE_XKBGETKEYBOARD
5513 #include <X11/XKBlib.h>
5514 #include <X11/keysym.h>
5515 #endif
5517 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5518 Sx_backspace_delete_keys_p, 0, 1, 0,
5519 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5520 FRAME nil means use the selected frame.
5521 Value is t if we know that both keys are present, and are mapped to the
5522 usual X keysyms. */)
5523 (frame)
5524 Lisp_Object frame;
5526 #ifdef HAVE_XKBGETKEYBOARD
5527 XkbDescPtr kb;
5528 struct frame *f = check_x_frame (frame);
5529 Display *dpy = FRAME_X_DISPLAY (f);
5530 Lisp_Object have_keys;
5531 int major, minor, op, event, error;
5533 BLOCK_INPUT;
5535 /* Check library version in case we're dynamically linked. */
5536 major = XkbMajorVersion;
5537 minor = XkbMinorVersion;
5538 if (!XkbLibraryVersion (&major, &minor))
5540 UNBLOCK_INPUT;
5541 return Qnil;
5544 /* Check that the server supports XKB. */
5545 major = XkbMajorVersion;
5546 minor = XkbMinorVersion;
5547 if (!XkbQueryExtension (dpy, &op, &event, &error, &major, &minor))
5549 UNBLOCK_INPUT;
5550 return Qnil;
5553 /* In this code we check that the keyboard has physical keys with names
5554 that start with BKSP (Backspace) and DELE (Delete), and that they
5555 generate keysym XK_BackSpace and XK_Delete respectively.
5556 This function is used to test if normal-erase-is-backspace should be
5557 turned on.
5558 An alternative approach would be to just check if XK_BackSpace and
5559 XK_Delete are mapped to any key. But if any of those are mapped to
5560 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5561 user doesn't know about it, it is better to return false here.
5562 It is more obvious to the user what to do if she/he has two keys
5563 clearly marked with names/symbols and one key does something not
5564 expected (i.e. she/he then tries the other).
5565 The cases where Backspace/Delete is mapped to some other key combination
5566 are rare, and in those cases, normal-erase-is-backspace can be turned on
5567 manually. */
5569 have_keys = Qnil;
5570 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5571 if (kb)
5573 int delete_keycode = 0, backspace_keycode = 0, i;
5575 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5577 for (i = kb->min_key_code;
5578 (i < kb->max_key_code
5579 && (delete_keycode == 0 || backspace_keycode == 0));
5580 ++i)
5582 /* The XKB symbolic key names can be seen most easily in
5583 the PS file generated by `xkbprint -label name
5584 $DISPLAY'. */
5585 if (bcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5586 delete_keycode = i;
5587 else if (bcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5588 backspace_keycode = i;
5591 XkbFreeNames (kb, 0, True);
5594 XkbFreeClientMap (kb, 0, True);
5596 if (delete_keycode
5597 && backspace_keycode
5598 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5599 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5600 have_keys = Qt;
5602 UNBLOCK_INPUT;
5603 return have_keys;
5604 #else /* not HAVE_XKBGETKEYBOARD */
5605 return Qnil;
5606 #endif /* not HAVE_XKBGETKEYBOARD */
5611 /***********************************************************************
5612 Initialization
5613 ***********************************************************************/
5615 /* Keep this list in the same order as frame_parms in frame.c.
5616 Use 0 for unsupported frame parameters. */
5618 frame_parm_handler x_frame_parm_handlers[] =
5620 x_set_autoraise,
5621 x_set_autolower,
5622 x_set_background_color,
5623 x_set_border_color,
5624 x_set_border_width,
5625 x_set_cursor_color,
5626 x_set_cursor_type,
5627 x_set_font,
5628 x_set_foreground_color,
5629 x_set_icon_name,
5630 x_set_icon_type,
5631 x_set_internal_border_width,
5632 x_set_menu_bar_lines,
5633 x_set_mouse_color,
5634 x_explicitly_set_name,
5635 x_set_scroll_bar_width,
5636 x_set_title,
5637 x_set_unsplittable,
5638 x_set_vertical_scroll_bars,
5639 x_set_visibility,
5640 x_set_tool_bar_lines,
5641 x_set_scroll_bar_foreground,
5642 x_set_scroll_bar_background,
5643 x_set_screen_gamma,
5644 x_set_line_spacing,
5645 x_set_fringe_width,
5646 x_set_fringe_width,
5647 x_set_wait_for_wm,
5648 x_set_fullscreen,
5651 void
5652 syms_of_xfns ()
5654 /* This is zero if not using X windows. */
5655 x_in_use = 0;
5657 /* The section below is built by the lisp expression at the top of the file,
5658 just above where these variables are declared. */
5659 /*&&& init symbols here &&&*/
5660 Qnone = intern ("none");
5661 staticpro (&Qnone);
5662 Qsuppress_icon = intern ("suppress-icon");
5663 staticpro (&Qsuppress_icon);
5664 Qundefined_color = intern ("undefined-color");
5665 staticpro (&Qundefined_color);
5666 Qcompound_text = intern ("compound-text");
5667 staticpro (&Qcompound_text);
5668 Qcancel_timer = intern ("cancel-timer");
5669 staticpro (&Qcancel_timer);
5670 /* This is the end of symbol initialization. */
5672 /* Text property `display' should be nonsticky by default. */
5673 Vtext_property_default_nonsticky
5674 = Fcons (Fcons (Qdisplay, Qt), Vtext_property_default_nonsticky);
5677 Fput (Qundefined_color, Qerror_conditions,
5678 Fcons (Qundefined_color, Fcons (Qerror, Qnil)));
5679 Fput (Qundefined_color, Qerror_message,
5680 build_string ("Undefined color"));
5682 DEFVAR_LISP ("x-pointer-shape", &Vx_pointer_shape,
5683 doc: /* The shape of the pointer when over text.
5684 Changing the value does not affect existing frames
5685 unless you set the mouse color. */);
5686 Vx_pointer_shape = Qnil;
5688 #if 0 /* This doesn't really do anything. */
5689 DEFVAR_LISP ("x-nontext-pointer-shape", &Vx_nontext_pointer_shape,
5690 doc: /* The shape of the pointer when not over text.
5691 This variable takes effect when you create a new frame
5692 or when you set the mouse color. */);
5693 #endif
5694 Vx_nontext_pointer_shape = Qnil;
5696 DEFVAR_LISP ("x-hourglass-pointer-shape", &Vx_hourglass_pointer_shape,
5697 doc: /* The shape of the pointer when Emacs is busy.
5698 This variable takes effect when you create a new frame
5699 or when you set the mouse color. */);
5700 Vx_hourglass_pointer_shape = Qnil;
5702 DEFVAR_BOOL ("display-hourglass", &display_hourglass_p,
5703 doc: /* Non-zero means Emacs displays an hourglass pointer on window systems. */);
5704 display_hourglass_p = 1;
5706 DEFVAR_LISP ("hourglass-delay", &Vhourglass_delay,
5707 doc: /* *Seconds to wait before displaying an hourglass pointer.
5708 Value must be an integer or float. */);
5709 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
5711 #if 0 /* This doesn't really do anything. */
5712 DEFVAR_LISP ("x-mode-pointer-shape", &Vx_mode_pointer_shape,
5713 doc: /* The shape of the pointer when over the mode line.
5714 This variable takes effect when you create a new frame
5715 or when you set the mouse color. */);
5716 #endif
5717 Vx_mode_pointer_shape = Qnil;
5719 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
5720 &Vx_sensitive_text_pointer_shape,
5721 doc: /* The shape of the pointer when over mouse-sensitive text.
5722 This variable takes effect when you create a new frame
5723 or when you set the mouse color. */);
5724 Vx_sensitive_text_pointer_shape = Qnil;
5726 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
5727 &Vx_window_horizontal_drag_shape,
5728 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
5729 This variable takes effect when you create a new frame
5730 or when you set the mouse color. */);
5731 Vx_window_horizontal_drag_shape = Qnil;
5733 DEFVAR_LISP ("x-cursor-fore-pixel", &Vx_cursor_fore_pixel,
5734 doc: /* A string indicating the foreground color of the cursor box. */);
5735 Vx_cursor_fore_pixel = Qnil;
5737 DEFVAR_LISP ("x-max-tooltip-size", &Vx_max_tooltip_size,
5738 doc: /* Maximum size for tooltips. Value is a pair (COLUMNS . ROWS).
5739 Text larger than this is clipped. */);
5740 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
5742 DEFVAR_LISP ("x-no-window-manager", &Vx_no_window_manager,
5743 doc: /* Non-nil if no X window manager is in use.
5744 Emacs doesn't try to figure this out; this is always nil
5745 unless you set it to something else. */);
5746 /* We don't have any way to find this out, so set it to nil
5747 and maybe the user would like to set it to t. */
5748 Vx_no_window_manager = Qnil;
5750 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
5751 &Vx_pixel_size_width_font_regexp,
5752 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
5754 Since Emacs gets width of a font matching with this regexp from
5755 PIXEL_SIZE field of the name, font finding mechanism gets faster for
5756 such a font. This is especially effective for such large fonts as
5757 Chinese, Japanese, and Korean. */);
5758 Vx_pixel_size_width_font_regexp = Qnil;
5760 /* This is not ifdef:ed, so other builds than GTK can customize it. */
5761 DEFVAR_BOOL ("x-use-old-gtk-file-dialog", &x_use_old_gtk_file_dialog,
5762 doc: /* *Non-nil means prompt with the old GTK file selection dialog.
5763 If nil or if the file selection dialog is not available, the new GTK file
5764 chooser is used instead. To turn off all file dialogs set the
5765 variable `use-file-dialog'. */);
5766 x_use_old_gtk_file_dialog = 0;
5768 Fprovide (intern ("x"), Qnil);
5770 #ifdef USE_X_TOOLKIT
5771 Fprovide (intern ("x-toolkit"), Qnil);
5772 #ifdef USE_MOTIF
5773 Fprovide (intern ("motif"), Qnil);
5775 DEFVAR_LISP ("motif-version-string", &Vmotif_version_string,
5776 doc: /* Version info for LessTif/Motif. */);
5777 Vmotif_version_string = build_string (XmVERSION_STRING);
5778 #endif /* USE_MOTIF */
5779 #endif /* USE_X_TOOLKIT */
5781 #ifdef USE_GTK
5782 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
5783 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
5784 But for a user it is a toolkit for X, and indeed, configure
5785 accepts --with-x-toolkit=gtk. */
5786 Fprovide (intern ("x-toolkit"), Qnil);
5787 Fprovide (intern ("gtk"), Qnil);
5789 DEFVAR_LISP ("gtk-version-string", &Vgtk_version_string,
5790 doc: /* Version info for GTK+. */);
5792 char gtk_version[40];
5793 g_snprintf (gtk_version, sizeof (gtk_version), "%u.%u.%u",
5794 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
5795 Vgtk_version_string = build_string (gtk_version);
5797 #endif /* USE_GTK */
5799 /* X window properties. */
5800 defsubr (&Sx_change_window_property);
5801 defsubr (&Sx_delete_window_property);
5802 defsubr (&Sx_window_property);
5804 defsubr (&Sxw_display_color_p);
5805 defsubr (&Sx_display_grayscale_p);
5806 defsubr (&Sxw_color_defined_p);
5807 defsubr (&Sxw_color_values);
5808 defsubr (&Sx_server_max_request_size);
5809 defsubr (&Sx_server_vendor);
5810 defsubr (&Sx_server_version);
5811 defsubr (&Sx_display_pixel_width);
5812 defsubr (&Sx_display_pixel_height);
5813 defsubr (&Sx_display_mm_width);
5814 defsubr (&Sx_display_mm_height);
5815 defsubr (&Sx_display_screens);
5816 defsubr (&Sx_display_planes);
5817 defsubr (&Sx_display_color_cells);
5818 defsubr (&Sx_display_visual_class);
5819 defsubr (&Sx_display_backing_store);
5820 defsubr (&Sx_display_save_under);
5821 defsubr (&Sx_create_frame);
5822 defsubr (&Sx_open_connection);
5823 defsubr (&Sx_close_connection);
5824 defsubr (&Sx_display_list);
5825 defsubr (&Sx_synchronize);
5826 defsubr (&Sx_focus_frame);
5827 defsubr (&Sx_backspace_delete_keys_p);
5829 /* Setting callback functions for fontset handler. */
5830 get_font_info_func = x_get_font_info;
5832 #if 0 /* This function pointer doesn't seem to be used anywhere.
5833 And the pointer assigned has the wrong type, anyway. */
5834 list_fonts_func = x_list_fonts;
5835 #endif
5837 load_font_func = x_load_font;
5838 find_ccl_program_func = x_find_ccl_program;
5839 query_font_func = x_query_font;
5840 set_frame_fontset_func = x_set_font;
5841 check_window_system_func = check_x;
5843 hourglass_atimer = NULL;
5844 hourglass_shown_p = 0;
5846 defsubr (&Sx_show_tip);
5847 defsubr (&Sx_hide_tip);
5848 tip_timer = Qnil;
5849 staticpro (&tip_timer);
5850 tip_frame = Qnil;
5851 staticpro (&tip_frame);
5853 last_show_tip_args = Qnil;
5854 staticpro (&last_show_tip_args);
5856 #if defined (USE_MOTIF) || defined (USE_GTK)
5857 defsubr (&Sx_file_dialog);
5858 #endif
5861 #endif /* HAVE_X_WINDOWS */
5863 /* arch-tag: 55040d02-5485-4d58-8b22-95a7a05f3288
5864 (do not change this comment) */