Merge from emacs-24; up to 2012-12-27T08:21:08Z!rgm@gnu.org
[bpt/emacs.git] / src / xfns.c
1 /* Functions for the X window system.
2
3 Copyright (C) 1989, 1992-2013 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21 #include <stdio.h>
22 #include <math.h>
23 #include <unistd.h>
24
25 /* This makes the fields of a Display accessible, in Xlib header files. */
26
27 #define XLIB_ILLEGAL_ACCESS
28
29 #include "lisp.h"
30 #include "xterm.h"
31 #include "frame.h"
32 #include "window.h"
33 #include "character.h"
34 #include "buffer.h"
35 #include "intervals.h"
36 #include "dispextern.h"
37 #include "keyboard.h"
38 #include "blockinput.h"
39 #include <epaths.h>
40 #include "charset.h"
41 #include "coding.h"
42 #include "fontset.h"
43 #include "systime.h"
44 #include "termhooks.h"
45 #include "atimer.h"
46 #include "termchar.h"
47 #include "font.h"
48
49 #ifdef HAVE_X_WINDOWS
50
51 #include <sys/types.h>
52 #include <sys/stat.h>
53
54 #if 1 /* Used to be #ifdef EMACS_BITMAP_FILES, but this should always work. */
55 #include "bitmaps/gray.xbm"
56 #else
57 #include <X11/bitmaps/gray>
58 #endif
59
60 #include "xsettings.h"
61
62 #ifdef USE_GTK
63 #include "gtkutil.h"
64 #endif
65
66 #ifdef USE_X_TOOLKIT
67 #include <X11/Shell.h>
68
69 #ifndef USE_MOTIF
70 #ifdef HAVE_XAW3D
71 #include <X11/Xaw3d/Paned.h>
72 #include <X11/Xaw3d/Label.h>
73 #else /* !HAVE_XAW3D */
74 #include <X11/Xaw/Paned.h>
75 #include <X11/Xaw/Label.h>
76 #endif /* HAVE_XAW3D */
77 #endif /* USE_MOTIF */
78
79 #ifdef USG
80 #undef USG /* ####KLUDGE for Solaris 2.2 and up */
81 #include <X11/Xos.h>
82 #define USG
83 #ifdef USG /* Pacify gcc -Wunused-macros. */
84 #endif
85 #else
86 #include <X11/Xos.h>
87 #endif
88
89 #include "widget.h"
90
91 #include "../lwlib/lwlib.h"
92
93 #ifdef USE_MOTIF
94 #include <Xm/Xm.h>
95 #include <Xm/DialogS.h>
96 #include <Xm/FileSB.h>
97 #include <Xm/List.h>
98 #include <Xm/TextF.h>
99 #endif
100
101 #ifdef USE_LUCID
102 #include "../lwlib/xlwmenu.h"
103 #endif
104
105 #if !defined (NO_EDITRES)
106 #define HACK_EDITRES
107 extern void _XEditResCheckMessages (Widget, XtPointer, XEvent *, Boolean *);
108 #endif /* not defined NO_EDITRES */
109
110 /* Unique id counter for widgets created by the Lucid Widget Library. */
111
112 extern LWLIB_ID widget_id_tick;
113
114 #ifdef USE_MOTIF
115
116 #endif /* USE_MOTIF */
117
118 #endif /* USE_X_TOOLKIT */
119
120 #ifdef USE_GTK
121
122 #endif /* USE_GTK */
123
124 #define MAXREQUEST(dpy) (XMaxRequestSize (dpy))
125
126 static Lisp_Object Qsuppress_icon;
127 static Lisp_Object Qundefined_color;
128 static Lisp_Object Qcompound_text, Qcancel_timer;
129 Lisp_Object Qfont_param;
130
131 #ifdef GLYPH_DEBUG
132 static ptrdiff_t image_cache_refcount;
133 static int dpyinfo_refcount;
134 #endif
135
136 static struct x_display_info *x_display_info_for_name (Lisp_Object);
137
138 /* Let the user specify an X display with a Lisp object.
139 OBJECT may be nil, a frame or a terminal object.
140 nil stands for the selected frame--or, if that is not an X frame,
141 the first X display on the list. */
142
143 struct x_display_info *
144 check_x_display_info (Lisp_Object object)
145 {
146 struct x_display_info *dpyinfo = NULL;
147
148 if (NILP (object))
149 {
150 struct frame *sf = XFRAME (selected_frame);
151
152 if (FRAME_X_P (sf) && FRAME_LIVE_P (sf))
153 dpyinfo = FRAME_X_DISPLAY_INFO (sf);
154 else if (x_display_list != 0)
155 dpyinfo = x_display_list;
156 else
157 error ("X windows are not in use or not initialized");
158 }
159 else if (TERMINALP (object))
160 {
161 struct terminal *t = get_terminal (object, 1);
162
163 if (t->type != output_x_window)
164 error ("Terminal %"pI"d is not an X display", XINT (object));
165
166 dpyinfo = t->display_info.x;
167 }
168 else if (STRINGP (object))
169 dpyinfo = x_display_info_for_name (object);
170 else
171 {
172 FRAME_PTR f = decode_window_system_frame (object);
173 dpyinfo = FRAME_X_DISPLAY_INFO (f);
174 }
175
176 return dpyinfo;
177 }
178
179 \f
180 /* Return the Emacs frame-object corresponding to an X window.
181 It could be the frame's main window or an icon window. */
182
183 /* This function can be called during GC, so use GC_xxx type test macros. */
184
185 struct frame *
186 x_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
187 {
188 Lisp_Object tail, frame;
189 struct frame *f;
190
191 if (wdesc == None)
192 return NULL;
193
194 FOR_EACH_FRAME (tail, frame)
195 {
196 f = XFRAME (frame);
197 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
198 continue;
199 if (f->output_data.x->hourglass_window == wdesc)
200 return f;
201 #ifdef USE_X_TOOLKIT
202 if ((f->output_data.x->edit_widget
203 && XtWindow (f->output_data.x->edit_widget) == wdesc)
204 /* A tooltip frame? */
205 || (!f->output_data.x->edit_widget
206 && FRAME_X_WINDOW (f) == wdesc)
207 || f->output_data.x->icon_desc == wdesc)
208 return f;
209 #else /* not USE_X_TOOLKIT */
210 #ifdef USE_GTK
211 if (f->output_data.x->edit_widget)
212 {
213 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
214 struct x_output *x = f->output_data.x;
215 if (gwdesc != 0 && gwdesc == x->edit_widget)
216 return f;
217 }
218 #endif /* USE_GTK */
219 if (FRAME_X_WINDOW (f) == wdesc
220 || f->output_data.x->icon_desc == wdesc)
221 return f;
222 #endif /* not USE_X_TOOLKIT */
223 }
224 return 0;
225 }
226
227 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
228 /* Like x_window_to_frame but also compares the window with the widget's
229 windows. */
230
231 struct frame *
232 x_any_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
233 {
234 Lisp_Object tail, frame;
235 struct frame *f, *found = NULL;
236 struct x_output *x;
237
238 if (wdesc == None)
239 return NULL;
240
241 FOR_EACH_FRAME (tail, frame)
242 {
243 if (found)
244 break;
245 f = XFRAME (frame);
246 if (FRAME_X_P (f) && FRAME_X_DISPLAY_INFO (f) == dpyinfo)
247 {
248 /* This frame matches if the window is any of its widgets. */
249 x = f->output_data.x;
250 if (x->hourglass_window == wdesc)
251 found = f;
252 else if (x->widget)
253 {
254 #ifdef USE_GTK
255 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
256 if (gwdesc != 0
257 && gtk_widget_get_toplevel (gwdesc) == x->widget)
258 found = f;
259 #else
260 if (wdesc == XtWindow (x->widget)
261 || wdesc == XtWindow (x->column_widget)
262 || wdesc == XtWindow (x->edit_widget))
263 found = f;
264 /* Match if the window is this frame's menubar. */
265 else if (lw_window_is_in_menubar (wdesc, x->menubar_widget))
266 found = f;
267 #endif
268 }
269 else if (FRAME_X_WINDOW (f) == wdesc)
270 /* A tooltip frame. */
271 found = f;
272 }
273 }
274
275 return found;
276 }
277
278 /* Likewise, but consider only the menu bar widget. */
279
280 struct frame *
281 x_menubar_window_to_frame (struct x_display_info *dpyinfo, XEvent *event)
282 {
283 Window wdesc = event->xany.window;
284 Lisp_Object tail, frame;
285 struct frame *f;
286 struct x_output *x;
287
288 if (wdesc == None)
289 return NULL;
290
291 FOR_EACH_FRAME (tail, frame)
292 {
293 f = XFRAME (frame);
294 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
295 continue;
296 x = f->output_data.x;
297 #ifdef USE_GTK
298 if (x->menubar_widget && xg_event_is_for_menubar (f, event))
299 return f;
300 #else
301 /* Match if the window is this frame's menubar. */
302 if (x->menubar_widget
303 && lw_window_is_in_menubar (wdesc, x->menubar_widget))
304 return f;
305 #endif
306 }
307 return 0;
308 }
309
310 /* Return the frame whose principal (outermost) window is WDESC.
311 If WDESC is some other (smaller) window, we return 0. */
312
313 struct frame *
314 x_top_window_to_frame (struct x_display_info *dpyinfo, int wdesc)
315 {
316 Lisp_Object tail, frame;
317 struct frame *f;
318 struct x_output *x;
319
320 if (wdesc == None)
321 return NULL;
322
323 FOR_EACH_FRAME (tail, frame)
324 {
325 f = XFRAME (frame);
326 if (!FRAME_X_P (f) || FRAME_X_DISPLAY_INFO (f) != dpyinfo)
327 continue;
328 x = f->output_data.x;
329
330 if (x->widget)
331 {
332 /* This frame matches if the window is its topmost widget. */
333 #ifdef USE_GTK
334 GtkWidget *gwdesc = xg_win_to_widget (dpyinfo->display, wdesc);
335 if (gwdesc == x->widget)
336 return f;
337 #else
338 if (wdesc == XtWindow (x->widget))
339 return f;
340 #if 0 /* I don't know why it did this,
341 but it seems logically wrong,
342 and it causes trouble for MapNotify events. */
343 /* Match if the window is this frame's menubar. */
344 if (x->menubar_widget
345 && wdesc == XtWindow (x->menubar_widget))
346 return f;
347 #endif
348 #endif
349 }
350 else if (FRAME_X_WINDOW (f) == wdesc)
351 /* Tooltip frame. */
352 return f;
353 }
354 return 0;
355 }
356 #endif /* USE_X_TOOLKIT || USE_GTK */
357
358 \f
359
360 /* Store the screen positions of frame F into XPTR and YPTR.
361 These are the positions of the containing window manager window,
362 not Emacs's own window. */
363
364 void
365 x_real_positions (FRAME_PTR f, int *xptr, int *yptr)
366 {
367 int win_x, win_y, outer_x IF_LINT (= 0), outer_y IF_LINT (= 0);
368 int real_x = 0, real_y = 0;
369 int had_errors = 0;
370 Window win = f->output_data.x->parent_desc;
371 Atom actual_type;
372 unsigned long actual_size, bytes_remaining;
373 int rc, actual_format;
374 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
375 long max_len = 400;
376 Display *dpy = FRAME_X_DISPLAY (f);
377 unsigned char *tmp_data = NULL;
378 Atom target_type = XA_CARDINAL;
379
380 block_input ();
381
382 x_catch_errors (dpy);
383
384 if (win == dpyinfo->root_window)
385 win = FRAME_OUTER_WINDOW (f);
386
387 /* This loop traverses up the containment tree until we hit the root
388 window. Window managers may intersect many windows between our window
389 and the root window. The window we find just before the root window
390 should be the outer WM window. */
391 for (;;)
392 {
393 Window wm_window, rootw;
394 Window *tmp_children;
395 unsigned int tmp_nchildren;
396 int success;
397
398 success = XQueryTree (FRAME_X_DISPLAY (f), win, &rootw,
399 &wm_window, &tmp_children, &tmp_nchildren);
400
401 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
402
403 /* Don't free tmp_children if XQueryTree failed. */
404 if (! success)
405 break;
406
407 XFree (tmp_children);
408
409 if (wm_window == rootw || had_errors)
410 break;
411
412 win = wm_window;
413 }
414
415 if (! had_errors)
416 {
417 unsigned int ign;
418 Window child, rootw;
419
420 /* Get the real coordinates for the WM window upper left corner */
421 XGetGeometry (FRAME_X_DISPLAY (f), win,
422 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
423
424 /* Translate real coordinates to coordinates relative to our
425 window. For our window, the upper left corner is 0, 0.
426 Since the upper left corner of the WM window is outside
427 our window, win_x and win_y will be negative:
428
429 ------------------ ---> x
430 | title |
431 | ----------------- v y
432 | | our window
433 */
434 XTranslateCoordinates (FRAME_X_DISPLAY (f),
435
436 /* From-window, to-window. */
437 FRAME_X_DISPLAY_INFO (f)->root_window,
438 FRAME_X_WINDOW (f),
439
440 /* From-position, to-position. */
441 real_x, real_y, &win_x, &win_y,
442
443 /* Child of win. */
444 &child);
445
446 if (FRAME_X_WINDOW (f) == FRAME_OUTER_WINDOW (f))
447 {
448 outer_x = win_x;
449 outer_y = win_y;
450 }
451 else
452 {
453 XTranslateCoordinates (FRAME_X_DISPLAY (f),
454
455 /* From-window, to-window. */
456 FRAME_X_DISPLAY_INFO (f)->root_window,
457 FRAME_OUTER_WINDOW (f),
458
459 /* From-position, to-position. */
460 real_x, real_y, &outer_x, &outer_y,
461
462 /* Child of win. */
463 &child);
464 }
465
466 had_errors = x_had_errors_p (FRAME_X_DISPLAY (f));
467 }
468
469
470 if (dpyinfo->root_window == f->output_data.x->parent_desc)
471 {
472 /* Try _NET_FRAME_EXTENTS if our parent is the root window. */
473 rc = XGetWindowProperty (dpy, win, dpyinfo->Xatom_net_frame_extents,
474 0, max_len, False, target_type,
475 &actual_type, &actual_format, &actual_size,
476 &bytes_remaining, &tmp_data);
477
478 if (rc == Success && actual_type == target_type && !x_had_errors_p (dpy)
479 && actual_size == 4 && actual_format == 32)
480 {
481 unsigned int ign;
482 Window rootw;
483 long *fe = (long *)tmp_data;
484
485 XGetGeometry (FRAME_X_DISPLAY (f), win,
486 &rootw, &real_x, &real_y, &ign, &ign, &ign, &ign);
487 outer_x = -fe[0];
488 outer_y = -fe[2];
489 real_x -= fe[0];
490 real_y -= fe[2];
491 }
492 }
493
494 if (tmp_data) XFree (tmp_data);
495
496 x_uncatch_errors ();
497
498 unblock_input ();
499
500 if (had_errors) return;
501
502 f->x_pixels_diff = -win_x;
503 f->y_pixels_diff = -win_y;
504
505 FRAME_X_OUTPUT (f)->x_pixels_outer_diff = -outer_x;
506 FRAME_X_OUTPUT (f)->y_pixels_outer_diff = -outer_y;
507
508 *xptr = real_x;
509 *yptr = real_y;
510 }
511
512 \f
513
514
515 /* Gamma-correct COLOR on frame F. */
516
517 void
518 gamma_correct (struct frame *f, XColor *color)
519 {
520 if (f->gamma)
521 {
522 color->red = pow (color->red / 65535.0, f->gamma) * 65535.0 + 0.5;
523 color->green = pow (color->green / 65535.0, f->gamma) * 65535.0 + 0.5;
524 color->blue = pow (color->blue / 65535.0, f->gamma) * 65535.0 + 0.5;
525 }
526 }
527
528
529 /* Decide if color named COLOR_NAME is valid for use on frame F. If
530 so, return the RGB values in COLOR. If ALLOC_P,
531 allocate the color. Value is false if COLOR_NAME is invalid, or
532 no color could be allocated. */
533
534 bool
535 x_defined_color (struct frame *f, const char *color_name,
536 XColor *color, bool alloc_p)
537 {
538 bool success_p = 0;
539 Display *dpy = FRAME_X_DISPLAY (f);
540 Colormap cmap = FRAME_X_COLORMAP (f);
541
542 block_input ();
543 #ifdef USE_GTK
544 success_p = xg_check_special_colors (f, color_name, color);
545 #endif
546 if (!success_p)
547 success_p = XParseColor (dpy, cmap, color_name, color) != 0;
548 if (success_p && alloc_p)
549 success_p = x_alloc_nearest_color (f, cmap, color);
550 unblock_input ();
551
552 return success_p;
553 }
554
555
556 /* Return the pixel color value for color COLOR_NAME on frame F. If F
557 is a monochrome frame, return MONO_COLOR regardless of what ARG says.
558 Signal an error if color can't be allocated. */
559
560 static int
561 x_decode_color (FRAME_PTR f, Lisp_Object color_name, int mono_color)
562 {
563 XColor cdef;
564
565 CHECK_STRING (color_name);
566
567 #if 0 /* Don't do this. It's wrong when we're not using the default
568 colormap, it makes freeing difficult, and it's probably not
569 an important optimization. */
570 if (strcmp (SDATA (color_name), "black") == 0)
571 return BLACK_PIX_DEFAULT (f);
572 else if (strcmp (SDATA (color_name), "white") == 0)
573 return WHITE_PIX_DEFAULT (f);
574 #endif
575
576 /* Return MONO_COLOR for monochrome frames. */
577 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
578 return mono_color;
579
580 /* x_defined_color is responsible for coping with failures
581 by looking for a near-miss. */
582 if (x_defined_color (f, SSDATA (color_name), &cdef, 1))
583 return cdef.pixel;
584
585 signal_error ("Undefined color", color_name);
586 }
587
588
589 \f
590 /* Change the `wait-for-wm' frame parameter of frame F. OLD_VALUE is
591 the previous value of that parameter, NEW_VALUE is the new value.
592 See also the comment of wait_for_wm in struct x_output. */
593
594 static void
595 x_set_wait_for_wm (struct frame *f, Lisp_Object new_value, Lisp_Object old_value)
596 {
597 f->output_data.x->wait_for_wm = !NILP (new_value);
598 }
599
600 static void
601 x_set_tool_bar_position (struct frame *f,
602 Lisp_Object new_value,
603 Lisp_Object old_value)
604 {
605 if (! EQ (new_value, Qleft) && ! EQ (new_value, Qright)
606 && ! EQ (new_value, Qbottom) && ! EQ (new_value, Qtop))
607 return;
608 if (EQ (new_value, old_value)) return;
609
610 #ifdef USE_GTK
611 xg_change_toolbar_position (f, new_value);
612 fset_tool_bar_position (f, new_value);
613 #endif
614 }
615
616 #ifdef USE_GTK
617
618 /* Set icon from FILE for frame F. By using GTK functions the icon
619 may be any format that GdkPixbuf knows about, i.e. not just bitmaps. */
620
621 int
622 xg_set_icon (FRAME_PTR f, Lisp_Object file)
623 {
624 int result = 0;
625 Lisp_Object found;
626
627 found = x_find_image_file (file);
628
629 if (! NILP (found))
630 {
631 GdkPixbuf *pixbuf;
632 GError *err = NULL;
633 char *filename = SSDATA (found);
634 block_input ();
635
636 pixbuf = gdk_pixbuf_new_from_file (filename, &err);
637
638 if (pixbuf)
639 {
640 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
641 pixbuf);
642 g_object_unref (pixbuf);
643
644 result = 1;
645 }
646 else
647 g_error_free (err);
648
649 unblock_input ();
650 }
651
652 return result;
653 }
654
655 int
656 xg_set_icon_from_xpm_data (FRAME_PTR f, const char **data)
657 {
658 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_xpm_data (data);
659
660 if (!pixbuf)
661 return 0;
662
663 gtk_window_set_icon (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)), pixbuf);
664 g_object_unref (pixbuf);
665 return 1;
666 }
667 #endif /* USE_GTK */
668
669
670 /* Functions called only from `x_set_frame_param'
671 to set individual parameters.
672
673 If FRAME_X_WINDOW (f) is 0,
674 the frame is being created and its X-window does not exist yet.
675 In that case, just record the parameter's new value
676 in the standard place; do not attempt to change the window. */
677
678 static void
679 x_set_foreground_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
680 {
681 struct x_output *x = f->output_data.x;
682 unsigned long fg, old_fg;
683
684 fg = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
685 old_fg = FRAME_FOREGROUND_PIXEL (f);
686 FRAME_FOREGROUND_PIXEL (f) = fg;
687
688 if (FRAME_X_WINDOW (f) != 0)
689 {
690 Display *dpy = FRAME_X_DISPLAY (f);
691
692 block_input ();
693 XSetForeground (dpy, x->normal_gc, fg);
694 XSetBackground (dpy, x->reverse_gc, fg);
695
696 if (x->cursor_pixel == old_fg)
697 {
698 unload_color (f, x->cursor_pixel);
699 x->cursor_pixel = x_copy_color (f, fg);
700 XSetBackground (dpy, x->cursor_gc, x->cursor_pixel);
701 }
702
703 unblock_input ();
704
705 update_face_from_frame_parameter (f, Qforeground_color, arg);
706
707 if (FRAME_VISIBLE_P (f))
708 redraw_frame (f);
709 }
710
711 unload_color (f, old_fg);
712 }
713
714 static void
715 x_set_background_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
716 {
717 struct x_output *x = f->output_data.x;
718 unsigned long bg;
719
720 bg = x_decode_color (f, arg, WHITE_PIX_DEFAULT (f));
721 unload_color (f, FRAME_BACKGROUND_PIXEL (f));
722 FRAME_BACKGROUND_PIXEL (f) = bg;
723
724 if (FRAME_X_WINDOW (f) != 0)
725 {
726 Display *dpy = FRAME_X_DISPLAY (f);
727
728 block_input ();
729 XSetBackground (dpy, x->normal_gc, bg);
730 XSetForeground (dpy, x->reverse_gc, bg);
731 XSetWindowBackground (dpy, FRAME_X_WINDOW (f), bg);
732 XSetForeground (dpy, x->cursor_gc, bg);
733
734 #ifdef USE_GTK
735 xg_set_background_color (f, bg);
736 #endif
737
738 #ifndef USE_TOOLKIT_SCROLL_BARS /* Turns out to be annoying with
739 toolkit scroll bars. */
740 {
741 Lisp_Object bar;
742 for (bar = FRAME_SCROLL_BARS (f);
743 !NILP (bar);
744 bar = XSCROLL_BAR (bar)->next)
745 {
746 Window window = XSCROLL_BAR (bar)->x_window;
747 XSetWindowBackground (dpy, window, bg);
748 }
749 }
750 #endif /* USE_TOOLKIT_SCROLL_BARS */
751
752 unblock_input ();
753 update_face_from_frame_parameter (f, Qbackground_color, arg);
754
755 if (FRAME_VISIBLE_P (f))
756 redraw_frame (f);
757 }
758 }
759
760 static Cursor
761 make_invisible_cursor (struct frame *f)
762 {
763 Display *dpy = FRAME_X_DISPLAY (f);
764 static char const no_data[] = { 0 };
765 Pixmap pix;
766 XColor col;
767 Cursor c = 0;
768
769 x_catch_errors (dpy);
770 pix = XCreateBitmapFromData (dpy, FRAME_X_DISPLAY_INFO (f)->root_window,
771 no_data, 1, 1);
772 if (! x_had_errors_p (dpy) && pix != None)
773 {
774 Cursor pixc;
775 col.pixel = 0;
776 col.red = col.green = col.blue = 0;
777 col.flags = DoRed | DoGreen | DoBlue;
778 pixc = XCreatePixmapCursor (dpy, pix, pix, &col, &col, 0, 0);
779 if (! x_had_errors_p (dpy) && pixc != None)
780 c = pixc;
781 XFreePixmap (dpy, pix);
782 }
783
784 x_uncatch_errors ();
785
786 return c;
787 }
788
789 static void
790 x_set_mouse_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
791 {
792 struct x_output *x = f->output_data.x;
793 Display *dpy = FRAME_X_DISPLAY (f);
794 Cursor cursor, nontext_cursor, mode_cursor, hand_cursor;
795 Cursor hourglass_cursor, horizontal_drag_cursor;
796 unsigned long pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
797 unsigned long mask_color = FRAME_BACKGROUND_PIXEL (f);
798
799 /* Don't let pointers be invisible. */
800 if (mask_color == pixel)
801 {
802 x_free_colors (f, &pixel, 1);
803 pixel = x_copy_color (f, FRAME_FOREGROUND_PIXEL (f));
804 }
805
806 unload_color (f, x->mouse_pixel);
807 x->mouse_pixel = pixel;
808
809 block_input ();
810
811 /* It's not okay to crash if the user selects a screwy cursor. */
812 x_catch_errors (dpy);
813
814 if (!NILP (Vx_pointer_shape))
815 {
816 CHECK_NUMBER (Vx_pointer_shape);
817 cursor = XCreateFontCursor (dpy, XINT (Vx_pointer_shape));
818 }
819 else
820 cursor = XCreateFontCursor (dpy, XC_xterm);
821 x_check_errors (dpy, "bad text pointer cursor: %s");
822
823 if (!NILP (Vx_nontext_pointer_shape))
824 {
825 CHECK_NUMBER (Vx_nontext_pointer_shape);
826 nontext_cursor
827 = XCreateFontCursor (dpy, XINT (Vx_nontext_pointer_shape));
828 }
829 else
830 nontext_cursor = XCreateFontCursor (dpy, XC_left_ptr);
831 x_check_errors (dpy, "bad nontext pointer cursor: %s");
832
833 if (!NILP (Vx_hourglass_pointer_shape))
834 {
835 CHECK_NUMBER (Vx_hourglass_pointer_shape);
836 hourglass_cursor
837 = XCreateFontCursor (dpy, XINT (Vx_hourglass_pointer_shape));
838 }
839 else
840 hourglass_cursor = XCreateFontCursor (dpy, XC_watch);
841 x_check_errors (dpy, "bad hourglass pointer cursor: %s");
842
843 if (!NILP (Vx_mode_pointer_shape))
844 {
845 CHECK_NUMBER (Vx_mode_pointer_shape);
846 mode_cursor = XCreateFontCursor (dpy, XINT (Vx_mode_pointer_shape));
847 }
848 else
849 mode_cursor = XCreateFontCursor (dpy, XC_xterm);
850 x_check_errors (dpy, "bad modeline pointer cursor: %s");
851
852 if (!NILP (Vx_sensitive_text_pointer_shape))
853 {
854 CHECK_NUMBER (Vx_sensitive_text_pointer_shape);
855 hand_cursor
856 = XCreateFontCursor (dpy, XINT (Vx_sensitive_text_pointer_shape));
857 }
858 else
859 hand_cursor = XCreateFontCursor (dpy, XC_hand2);
860
861 if (!NILP (Vx_window_horizontal_drag_shape))
862 {
863 CHECK_NUMBER (Vx_window_horizontal_drag_shape);
864 horizontal_drag_cursor
865 = XCreateFontCursor (dpy, XINT (Vx_window_horizontal_drag_shape));
866 }
867 else
868 horizontal_drag_cursor
869 = XCreateFontCursor (dpy, XC_sb_h_double_arrow);
870
871 /* Check and report errors with the above calls. */
872 x_check_errors (dpy, "can't set cursor shape: %s");
873 x_uncatch_errors ();
874
875 {
876 XColor fore_color, back_color;
877
878 fore_color.pixel = x->mouse_pixel;
879 x_query_color (f, &fore_color);
880 back_color.pixel = mask_color;
881 x_query_color (f, &back_color);
882
883 XRecolorCursor (dpy, cursor, &fore_color, &back_color);
884 XRecolorCursor (dpy, nontext_cursor, &fore_color, &back_color);
885 XRecolorCursor (dpy, mode_cursor, &fore_color, &back_color);
886 XRecolorCursor (dpy, hand_cursor, &fore_color, &back_color);
887 XRecolorCursor (dpy, hourglass_cursor, &fore_color, &back_color);
888 XRecolorCursor (dpy, horizontal_drag_cursor, &fore_color, &back_color);
889 }
890
891 if (FRAME_X_WINDOW (f) != 0)
892 XDefineCursor (dpy, FRAME_X_WINDOW (f),
893 f->output_data.x->current_cursor = cursor);
894
895 if (FRAME_X_DISPLAY_INFO (f)->invisible_cursor == 0)
896 FRAME_X_DISPLAY_INFO (f)->invisible_cursor = make_invisible_cursor (f);
897
898 if (cursor != x->text_cursor
899 && x->text_cursor != 0)
900 XFreeCursor (dpy, x->text_cursor);
901 x->text_cursor = cursor;
902
903 if (nontext_cursor != x->nontext_cursor
904 && x->nontext_cursor != 0)
905 XFreeCursor (dpy, x->nontext_cursor);
906 x->nontext_cursor = nontext_cursor;
907
908 if (hourglass_cursor != x->hourglass_cursor
909 && x->hourglass_cursor != 0)
910 XFreeCursor (dpy, x->hourglass_cursor);
911 x->hourglass_cursor = hourglass_cursor;
912
913 if (mode_cursor != x->modeline_cursor
914 && x->modeline_cursor != 0)
915 XFreeCursor (dpy, f->output_data.x->modeline_cursor);
916 x->modeline_cursor = mode_cursor;
917
918 if (hand_cursor != x->hand_cursor
919 && x->hand_cursor != 0)
920 XFreeCursor (dpy, x->hand_cursor);
921 x->hand_cursor = hand_cursor;
922
923 if (horizontal_drag_cursor != x->horizontal_drag_cursor
924 && x->horizontal_drag_cursor != 0)
925 XFreeCursor (dpy, x->horizontal_drag_cursor);
926 x->horizontal_drag_cursor = horizontal_drag_cursor;
927
928 XFlush (dpy);
929 unblock_input ();
930
931 update_face_from_frame_parameter (f, Qmouse_color, arg);
932 }
933
934 static void
935 x_set_cursor_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
936 {
937 unsigned long fore_pixel, pixel;
938 int fore_pixel_allocated_p = 0, pixel_allocated_p = 0;
939 struct x_output *x = f->output_data.x;
940
941 if (!NILP (Vx_cursor_fore_pixel))
942 {
943 fore_pixel = x_decode_color (f, Vx_cursor_fore_pixel,
944 WHITE_PIX_DEFAULT (f));
945 fore_pixel_allocated_p = 1;
946 }
947 else
948 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
949
950 pixel = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
951 pixel_allocated_p = 1;
952
953 /* Make sure that the cursor color differs from the background color. */
954 if (pixel == FRAME_BACKGROUND_PIXEL (f))
955 {
956 if (pixel_allocated_p)
957 {
958 x_free_colors (f, &pixel, 1);
959 pixel_allocated_p = 0;
960 }
961
962 pixel = x->mouse_pixel;
963 if (pixel == fore_pixel)
964 {
965 if (fore_pixel_allocated_p)
966 {
967 x_free_colors (f, &fore_pixel, 1);
968 fore_pixel_allocated_p = 0;
969 }
970 fore_pixel = FRAME_BACKGROUND_PIXEL (f);
971 }
972 }
973
974 unload_color (f, x->cursor_foreground_pixel);
975 if (!fore_pixel_allocated_p)
976 fore_pixel = x_copy_color (f, fore_pixel);
977 x->cursor_foreground_pixel = fore_pixel;
978
979 unload_color (f, x->cursor_pixel);
980 if (!pixel_allocated_p)
981 pixel = x_copy_color (f, pixel);
982 x->cursor_pixel = pixel;
983
984 if (FRAME_X_WINDOW (f) != 0)
985 {
986 block_input ();
987 XSetBackground (FRAME_X_DISPLAY (f), x->cursor_gc, x->cursor_pixel);
988 XSetForeground (FRAME_X_DISPLAY (f), x->cursor_gc, fore_pixel);
989 unblock_input ();
990
991 if (FRAME_VISIBLE_P (f))
992 {
993 x_update_cursor (f, 0);
994 x_update_cursor (f, 1);
995 }
996 }
997
998 update_face_from_frame_parameter (f, Qcursor_color, arg);
999 }
1000 \f
1001 /* Set the border-color of frame F to pixel value PIX.
1002 Note that this does not fully take effect if done before
1003 F has an x-window. */
1004
1005 static void
1006 x_set_border_pixel (struct frame *f, int pix)
1007 {
1008 unload_color (f, f->output_data.x->border_pixel);
1009 f->output_data.x->border_pixel = pix;
1010
1011 if (FRAME_X_WINDOW (f) != 0 && f->border_width > 0)
1012 {
1013 block_input ();
1014 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), pix);
1015 unblock_input ();
1016
1017 if (FRAME_VISIBLE_P (f))
1018 redraw_frame (f);
1019 }
1020 }
1021
1022 /* Set the border-color of frame F to value described by ARG.
1023 ARG can be a string naming a color.
1024 The border-color is used for the border that is drawn by the X server.
1025 Note that this does not fully take effect if done before
1026 F has an x-window; it must be redone when the window is created.
1027
1028 Note: this is done in two routines because of the way X10 works.
1029
1030 Note: under X11, this is normally the province of the window manager,
1031 and so emacs' border colors may be overridden. */
1032
1033 static void
1034 x_set_border_color (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1035 {
1036 int pix;
1037
1038 CHECK_STRING (arg);
1039 pix = x_decode_color (f, arg, BLACK_PIX_DEFAULT (f));
1040 x_set_border_pixel (f, pix);
1041 update_face_from_frame_parameter (f, Qborder_color, arg);
1042 }
1043
1044
1045 static void
1046 x_set_cursor_type (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1047 {
1048 set_frame_cursor_types (f, arg);
1049
1050 /* Make sure the cursor gets redrawn. */
1051 cursor_type_changed = 1;
1052 }
1053 \f
1054 static void
1055 x_set_icon_type (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1056 {
1057 int result;
1058
1059 if (STRINGP (arg))
1060 {
1061 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1062 return;
1063 }
1064 else if (!STRINGP (oldval) && EQ (oldval, Qnil) == EQ (arg, Qnil))
1065 return;
1066
1067 block_input ();
1068 if (NILP (arg))
1069 result = x_text_icon (f,
1070 SSDATA ((!NILP (f->icon_name)
1071 ? f->icon_name
1072 : f->name)));
1073 else
1074 result = x_bitmap_icon (f, arg);
1075
1076 if (result)
1077 {
1078 unblock_input ();
1079 error ("No icon window available");
1080 }
1081
1082 XFlush (FRAME_X_DISPLAY (f));
1083 unblock_input ();
1084 }
1085
1086 static void
1087 x_set_icon_name (struct frame *f, Lisp_Object arg, Lisp_Object oldval)
1088 {
1089 int result;
1090
1091 if (STRINGP (arg))
1092 {
1093 if (STRINGP (oldval) && EQ (Fstring_equal (oldval, arg), Qt))
1094 return;
1095 }
1096 else if (!NILP (arg) || NILP (oldval))
1097 return;
1098
1099 fset_icon_name (f, arg);
1100
1101 if (f->output_data.x->icon_bitmap != 0)
1102 return;
1103
1104 block_input ();
1105
1106 result = x_text_icon (f,
1107 SSDATA ((!NILP (f->icon_name)
1108 ? f->icon_name
1109 : !NILP (f->title)
1110 ? f->title
1111 : f->name)));
1112
1113 if (result)
1114 {
1115 unblock_input ();
1116 error ("No icon window available");
1117 }
1118
1119 XFlush (FRAME_X_DISPLAY (f));
1120 unblock_input ();
1121 }
1122
1123 \f
1124 void
1125 x_set_menu_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1126 {
1127 int nlines;
1128 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
1129 int olines = FRAME_MENU_BAR_LINES (f);
1130 #endif
1131
1132 /* Right now, menu bars don't work properly in minibuf-only frames;
1133 most of the commands try to apply themselves to the minibuffer
1134 frame itself, and get an error because you can't switch buffers
1135 in or split the minibuffer window. */
1136 if (FRAME_MINIBUF_ONLY_P (f))
1137 return;
1138
1139 if (TYPE_RANGED_INTEGERP (int, value))
1140 nlines = XINT (value);
1141 else
1142 nlines = 0;
1143
1144 /* Make sure we redisplay all windows in this frame. */
1145 windows_or_buffers_changed++;
1146
1147 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
1148 FRAME_MENU_BAR_LINES (f) = 0;
1149 if (nlines)
1150 {
1151 FRAME_EXTERNAL_MENU_BAR (f) = 1;
1152 if (FRAME_X_P (f) && f->output_data.x->menubar_widget == 0)
1153 /* Make sure next redisplay shows the menu bar. */
1154 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1155 }
1156 else
1157 {
1158 if (FRAME_EXTERNAL_MENU_BAR (f) == 1)
1159 free_frame_menubar (f);
1160 FRAME_EXTERNAL_MENU_BAR (f) = 0;
1161 if (FRAME_X_P (f))
1162 f->output_data.x->menubar_widget = 0;
1163 }
1164 #else /* not USE_X_TOOLKIT && not USE_GTK */
1165 FRAME_MENU_BAR_LINES (f) = nlines;
1166 resize_frame_windows (f, FRAME_LINES (f), 0);
1167
1168 /* If the menu bar height gets changed, the internal border below
1169 the top margin has to be cleared. Also, if the menu bar gets
1170 larger, the area for the added lines has to be cleared except for
1171 the first menu bar line that is to be drawn later. */
1172 if (nlines != olines)
1173 {
1174 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1175 int width = FRAME_PIXEL_WIDTH (f);
1176 int y;
1177
1178 /* height can be zero here. */
1179 if (height > 0 && width > 0)
1180 {
1181 y = FRAME_TOP_MARGIN_HEIGHT (f);
1182
1183 block_input ();
1184 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1185 0, y, width, height, False);
1186 unblock_input ();
1187 }
1188
1189 if (nlines > 1 && nlines > olines)
1190 {
1191 y = (olines == 0 ? 1 : olines) * FRAME_LINE_HEIGHT (f);
1192 height = nlines * FRAME_LINE_HEIGHT (f) - y;
1193
1194 block_input ();
1195 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1196 0, y, width, height, False);
1197 unblock_input ();
1198 }
1199
1200 if (nlines == 0 && WINDOWP (f->menu_bar_window))
1201 clear_glyph_matrix (XWINDOW (f->menu_bar_window)->current_matrix);
1202 }
1203 #endif /* not USE_X_TOOLKIT && not USE_GTK */
1204 adjust_glyphs (f);
1205 run_window_configuration_change_hook (f);
1206 }
1207
1208
1209 /* Set the number of lines used for the tool bar of frame F to VALUE.
1210 VALUE not an integer, or < 0 means set the lines to zero. OLDVAL
1211 is the old number of tool bar lines. This function changes the
1212 height of all windows on frame F to match the new tool bar height.
1213 The frame's height doesn't change. */
1214
1215 void
1216 x_set_tool_bar_lines (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1217 {
1218 int delta, nlines, root_height;
1219 Lisp_Object root_window;
1220
1221 /* Treat tool bars like menu bars. */
1222 if (FRAME_MINIBUF_ONLY_P (f))
1223 return;
1224
1225 /* Use VALUE only if an int >= 0. */
1226 if (RANGED_INTEGERP (0, value, INT_MAX))
1227 nlines = XFASTINT (value);
1228 else
1229 nlines = 0;
1230
1231 #ifdef USE_GTK
1232 FRAME_TOOL_BAR_LINES (f) = 0;
1233 if (nlines)
1234 {
1235 FRAME_EXTERNAL_TOOL_BAR (f) = 1;
1236 if (FRAME_X_P (f) && f->output_data.x->toolbar_widget == 0)
1237 /* Make sure next redisplay shows the tool bar. */
1238 XWINDOW (FRAME_SELECTED_WINDOW (f))->update_mode_line = 1;
1239 update_frame_tool_bar (f);
1240 }
1241 else
1242 {
1243 if (FRAME_EXTERNAL_TOOL_BAR (f))
1244 free_frame_tool_bar (f);
1245 FRAME_EXTERNAL_TOOL_BAR (f) = 0;
1246 }
1247
1248 return;
1249 #endif
1250
1251 /* Make sure we redisplay all windows in this frame. */
1252 ++windows_or_buffers_changed;
1253
1254 delta = nlines - FRAME_TOOL_BAR_LINES (f);
1255
1256 /* Don't resize the tool-bar to more than we have room for. */
1257 root_window = FRAME_ROOT_WINDOW (f);
1258 root_height = WINDOW_TOTAL_LINES (XWINDOW (root_window));
1259 if (root_height - delta < 1)
1260 {
1261 delta = root_height - 1;
1262 nlines = FRAME_TOOL_BAR_LINES (f) + delta;
1263 }
1264
1265 FRAME_TOOL_BAR_LINES (f) = nlines;
1266 resize_frame_windows (f, FRAME_LINES (f), 0);
1267 adjust_glyphs (f);
1268
1269 /* We also have to make sure that the internal border at the top of
1270 the frame, below the menu bar or tool bar, is redrawn when the
1271 tool bar disappears. This is so because the internal border is
1272 below the tool bar if one is displayed, but is below the menu bar
1273 if there isn't a tool bar. The tool bar draws into the area
1274 below the menu bar. */
1275 if (FRAME_X_WINDOW (f) && FRAME_TOOL_BAR_LINES (f) == 0)
1276 {
1277 clear_frame (f);
1278 clear_current_matrices (f);
1279 }
1280
1281 /* If the tool bar gets smaller, the internal border below it
1282 has to be cleared. It was formerly part of the display
1283 of the larger tool bar, and updating windows won't clear it. */
1284 if (delta < 0)
1285 {
1286 int height = FRAME_INTERNAL_BORDER_WIDTH (f);
1287 int width = FRAME_PIXEL_WIDTH (f);
1288 int y = (FRAME_MENU_BAR_LINES (f) + nlines) * FRAME_LINE_HEIGHT (f);
1289
1290 /* height can be zero here. */
1291 if (height > 0 && width > 0)
1292 {
1293 block_input ();
1294 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1295 0, y, width, height, False);
1296 unblock_input ();
1297 }
1298
1299 if (WINDOWP (f->tool_bar_window))
1300 clear_glyph_matrix (XWINDOW (f->tool_bar_window)->current_matrix);
1301 }
1302
1303 run_window_configuration_change_hook (f);
1304
1305 }
1306
1307
1308 /* Set the foreground color for scroll bars on frame F to VALUE.
1309 VALUE should be a string, a color name. If it isn't a string or
1310 isn't a valid color name, do nothing. OLDVAL is the old value of
1311 the frame parameter. */
1312
1313 static void
1314 x_set_scroll_bar_foreground (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1315 {
1316 unsigned long pixel;
1317
1318 if (STRINGP (value))
1319 pixel = x_decode_color (f, value, BLACK_PIX_DEFAULT (f));
1320 else
1321 pixel = -1;
1322
1323 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
1324 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
1325
1326 f->output_data.x->scroll_bar_foreground_pixel = pixel;
1327 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1328 {
1329 /* Remove all scroll bars because they have wrong colors. */
1330 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1331 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1332 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1333 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1334
1335 update_face_from_frame_parameter (f, Qscroll_bar_foreground, value);
1336 redraw_frame (f);
1337 }
1338 }
1339
1340
1341 /* Set the background color for scroll bars on frame F to VALUE VALUE
1342 should be a string, a color name. If it isn't a string or isn't a
1343 valid color name, do nothing. OLDVAL is the old value of the frame
1344 parameter. */
1345
1346 static void
1347 x_set_scroll_bar_background (struct frame *f, Lisp_Object value, Lisp_Object oldval)
1348 {
1349 unsigned long pixel;
1350
1351 if (STRINGP (value))
1352 pixel = x_decode_color (f, value, WHITE_PIX_DEFAULT (f));
1353 else
1354 pixel = -1;
1355
1356 if (f->output_data.x->scroll_bar_background_pixel != -1)
1357 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
1358
1359 #ifdef USE_TOOLKIT_SCROLL_BARS
1360 /* Scrollbar shadow colors. */
1361 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
1362 {
1363 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
1364 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
1365 }
1366 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
1367 {
1368 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
1369 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
1370 }
1371 #endif /* USE_TOOLKIT_SCROLL_BARS */
1372
1373 f->output_data.x->scroll_bar_background_pixel = pixel;
1374 if (FRAME_X_WINDOW (f) && FRAME_VISIBLE_P (f))
1375 {
1376 /* Remove all scroll bars because they have wrong colors. */
1377 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
1378 (*FRAME_TERMINAL (f)->condemn_scroll_bars_hook) (f);
1379 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
1380 (*FRAME_TERMINAL (f)->judge_scroll_bars_hook) (f);
1381
1382 update_face_from_frame_parameter (f, Qscroll_bar_background, value);
1383 redraw_frame (f);
1384 }
1385 }
1386
1387 \f
1388 /* Encode Lisp string STRING as a text in a format appropriate for
1389 XICCC (X Inter Client Communication Conventions).
1390
1391 This can call Lisp code, so callers must GCPRO.
1392
1393 If STRING contains only ASCII characters, do no conversion and
1394 return the string data of STRING. Otherwise, encode the text by
1395 CODING_SYSTEM, and return a newly allocated memory area which
1396 should be freed by `xfree' by a caller.
1397
1398 SELECTIONP non-zero means the string is being encoded for an X
1399 selection, so it is safe to run pre-write conversions (which
1400 may run Lisp code).
1401
1402 Store the byte length of resulting text in *TEXT_BYTES.
1403
1404 If the text contains only ASCII and Latin-1, store 1 in *STRING_P,
1405 which means that the `encoding' of the result can be `STRING'.
1406 Otherwise store 0 in *STRINGP, which means that the `encoding' of
1407 the result should be `COMPOUND_TEXT'. */
1408
1409 static unsigned char *
1410 x_encode_text (Lisp_Object string, Lisp_Object coding_system, int selectionp,
1411 ptrdiff_t *text_bytes, int *stringp, int *freep)
1412 {
1413 int result = string_xstring_p (string);
1414 struct coding_system coding;
1415
1416 if (result == 0)
1417 {
1418 /* No multibyte character in OBJ. We need not encode it. */
1419 *text_bytes = SBYTES (string);
1420 *stringp = 1;
1421 *freep = 0;
1422 return SDATA (string);
1423 }
1424
1425 setup_coding_system (coding_system, &coding);
1426 coding.mode |= (CODING_MODE_SAFE_ENCODING | CODING_MODE_LAST_BLOCK);
1427 /* We suppress producing escape sequences for composition. */
1428 coding.common_flags &= ~CODING_ANNOTATION_MASK;
1429 coding.destination = xnmalloc (SCHARS (string), 2);
1430 coding.dst_bytes = SCHARS (string) * 2;
1431 encode_coding_object (&coding, string, 0, 0,
1432 SCHARS (string), SBYTES (string), Qnil);
1433 *text_bytes = coding.produced;
1434 *stringp = (result == 1 || !EQ (coding_system, Qcompound_text));
1435 *freep = 1;
1436 return coding.destination;
1437 }
1438
1439 \f
1440 /* Set the WM name to NAME for frame F. Also set the icon name.
1441 If the frame already has an icon name, use that, otherwise set the
1442 icon name to NAME. */
1443
1444 static void
1445 x_set_name_internal (FRAME_PTR f, Lisp_Object name)
1446 {
1447 if (FRAME_X_WINDOW (f))
1448 {
1449 block_input ();
1450 {
1451 XTextProperty text, icon;
1452 ptrdiff_t bytes;
1453 int stringp;
1454 int do_free_icon_value = 0, do_free_text_value = 0;
1455 Lisp_Object coding_system;
1456 Lisp_Object encoded_name;
1457 Lisp_Object encoded_icon_name;
1458 struct gcpro gcpro1;
1459
1460 /* As ENCODE_UTF_8 may cause GC and relocation of string data,
1461 we use it before x_encode_text that may return string data. */
1462 GCPRO1 (name);
1463 encoded_name = ENCODE_UTF_8 (name);
1464 UNGCPRO;
1465
1466 coding_system = Qcompound_text;
1467 /* Note: Encoding strategy
1468
1469 We encode NAME by compound-text and use "COMPOUND-TEXT" in
1470 text.encoding. But, there are non-internationalized window
1471 managers which don't support that encoding. So, if NAME
1472 contains only ASCII and 8859-1 characters, encode it by
1473 iso-latin-1, and use "STRING" in text.encoding hoping that
1474 such window managers at least analyze this format correctly,
1475 i.e. treat 8-bit bytes as 8859-1 characters.
1476
1477 We may also be able to use "UTF8_STRING" in text.encoding
1478 in the future which can encode all Unicode characters.
1479 But, for the moment, there's no way to know that the
1480 current window manager supports it or not.
1481
1482 Either way, we also set the _NET_WM_NAME and _NET_WM_ICON_NAME
1483 properties. Per the EWMH specification, those two properties
1484 are always UTF8_STRING. This matches what gtk_window_set_title()
1485 does in the USE_GTK case. */
1486 text.value = x_encode_text (name, coding_system, 0, &bytes, &stringp,
1487 &do_free_text_value);
1488 text.encoding = (stringp ? XA_STRING
1489 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1490 text.format = 8;
1491 text.nitems = bytes;
1492 if (text.nitems != bytes)
1493 error ("Window name too large");
1494
1495 if (!STRINGP (f->icon_name))
1496 {
1497 icon = text;
1498 encoded_icon_name = encoded_name;
1499 }
1500 else
1501 {
1502 /* See the above comment "Note: Encoding strategy". */
1503 icon.value = x_encode_text (f->icon_name, coding_system, 0,
1504 &bytes, &stringp, &do_free_icon_value);
1505 icon.encoding = (stringp ? XA_STRING
1506 : FRAME_X_DISPLAY_INFO (f)->Xatom_COMPOUND_TEXT);
1507 icon.format = 8;
1508 icon.nitems = bytes;
1509 if (icon.nitems != bytes)
1510 error ("Icon name too large");
1511
1512 encoded_icon_name = ENCODE_UTF_8 (f->icon_name);
1513 }
1514
1515 #ifdef USE_GTK
1516 gtk_window_set_title (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)),
1517 SSDATA (encoded_name));
1518 #else /* not USE_GTK */
1519 XSetWMName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
1520 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1521 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_name,
1522 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1523 8, PropModeReplace,
1524 SDATA (encoded_name),
1525 SBYTES (encoded_name));
1526 #endif /* not USE_GTK */
1527
1528 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &icon);
1529 XChangeProperty (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
1530 FRAME_X_DISPLAY_INFO (f)->Xatom_net_wm_icon_name,
1531 FRAME_X_DISPLAY_INFO (f)->Xatom_UTF8_STRING,
1532 8, PropModeReplace,
1533 SDATA (encoded_icon_name),
1534 SBYTES (encoded_icon_name));
1535
1536 if (do_free_icon_value)
1537 xfree (icon.value);
1538 if (do_free_text_value)
1539 xfree (text.value);
1540 }
1541 unblock_input ();
1542 }
1543 }
1544
1545 /* Change the name of frame F to NAME. If NAME is nil, set F's name to
1546 x_id_name.
1547
1548 If EXPLICIT is non-zero, that indicates that lisp code is setting the
1549 name; if NAME is a string, set F's name to NAME and set
1550 F->explicit_name; if NAME is Qnil, then clear F->explicit_name.
1551
1552 If EXPLICIT is zero, that indicates that Emacs redisplay code is
1553 suggesting a new name, which lisp code should override; if
1554 F->explicit_name is set, ignore the new name; otherwise, set it. */
1555
1556 static void
1557 x_set_name (struct frame *f, Lisp_Object name, int explicit)
1558 {
1559 /* Make sure that requests from lisp code override requests from
1560 Emacs redisplay code. */
1561 if (explicit)
1562 {
1563 /* If we're switching from explicit to implicit, we had better
1564 update the mode lines and thereby update the title. */
1565 if (f->explicit_name && NILP (name))
1566 update_mode_lines = 1;
1567
1568 f->explicit_name = ! NILP (name);
1569 }
1570 else if (f->explicit_name)
1571 return;
1572
1573 /* If NAME is nil, set the name to the x_id_name. */
1574 if (NILP (name))
1575 {
1576 /* Check for no change needed in this very common case
1577 before we do any consing. */
1578 if (!strcmp (FRAME_X_DISPLAY_INFO (f)->x_id_name,
1579 SSDATA (f->name)))
1580 return;
1581 name = build_string (FRAME_X_DISPLAY_INFO (f)->x_id_name);
1582 }
1583 else
1584 CHECK_STRING (name);
1585
1586 /* Don't change the name if it's already NAME. */
1587 if (! NILP (Fstring_equal (name, f->name)))
1588 return;
1589
1590 fset_name (f, name);
1591
1592 /* For setting the frame title, the title parameter should override
1593 the name parameter. */
1594 if (! NILP (f->title))
1595 name = f->title;
1596
1597 x_set_name_internal (f, name);
1598 }
1599
1600 /* This function should be called when the user's lisp code has
1601 specified a name for the frame; the name will override any set by the
1602 redisplay code. */
1603 static void
1604 x_explicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1605 {
1606 x_set_name (f, arg, 1);
1607 }
1608
1609 /* This function should be called by Emacs redisplay code to set the
1610 name; names set this way will never override names set by the user's
1611 lisp code. */
1612 void
1613 x_implicitly_set_name (FRAME_PTR f, Lisp_Object arg, Lisp_Object oldval)
1614 {
1615 x_set_name (f, arg, 0);
1616 }
1617 \f
1618 /* Change the title of frame F to NAME.
1619 If NAME is nil, use the frame name as the title. */
1620
1621 static void
1622 x_set_title (struct frame *f, Lisp_Object name, Lisp_Object old_name)
1623 {
1624 /* Don't change the title if it's already NAME. */
1625 if (EQ (name, f->title))
1626 return;
1627
1628 update_mode_lines = 1;
1629
1630 fset_title (f, name);
1631
1632 if (NILP (name))
1633 name = f->name;
1634 else
1635 CHECK_STRING (name);
1636
1637 x_set_name_internal (f, name);
1638 }
1639
1640 void
1641 x_set_scroll_bar_default_width (struct frame *f)
1642 {
1643 int wid = FRAME_COLUMN_WIDTH (f);
1644 #ifdef USE_TOOLKIT_SCROLL_BARS
1645 #ifdef USE_GTK
1646 int minw = xg_get_default_scrollbar_width ();
1647 #else
1648 int minw = 16;
1649 #endif
1650 /* A minimum width of 14 doesn't look good for toolkit scroll bars. */
1651 int width = minw + 2 * VERTICAL_SCROLL_BAR_WIDTH_TRIM;
1652 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (width + wid - 1) / wid;
1653 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = width;
1654 #else
1655 /* Make the actual width at least 14 pixels and a multiple of a
1656 character width. */
1657 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
1658
1659 /* Use all of that space (aside from required margins) for the
1660 scroll bar. */
1661 FRAME_CONFIG_SCROLL_BAR_WIDTH (f) = 0;
1662 #endif
1663 }
1664
1665 \f
1666 /* Record in frame F the specified or default value according to ALIST
1667 of the parameter named PROP (a Lisp symbol). If no value is
1668 specified for PROP, look for an X default for XPROP on the frame
1669 named NAME. If that is not found either, use the value DEFLT. */
1670
1671 static Lisp_Object
1672 x_default_scroll_bar_color_parameter (struct frame *f,
1673 Lisp_Object alist, Lisp_Object prop,
1674 const char *xprop, const char *xclass,
1675 int foreground_p)
1676 {
1677 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1678 Lisp_Object tem;
1679
1680 tem = x_get_arg (dpyinfo, alist, prop, xprop, xclass, RES_TYPE_STRING);
1681 if (EQ (tem, Qunbound))
1682 {
1683 #ifdef USE_TOOLKIT_SCROLL_BARS
1684
1685 /* See if an X resource for the scroll bar color has been
1686 specified. */
1687 tem = display_x_get_resource (dpyinfo,
1688 build_string (foreground_p
1689 ? "foreground"
1690 : "background"),
1691 empty_unibyte_string,
1692 build_string ("verticalScrollBar"),
1693 empty_unibyte_string);
1694 if (!STRINGP (tem))
1695 {
1696 /* If nothing has been specified, scroll bars will use a
1697 toolkit-dependent default. Because these defaults are
1698 difficult to get at without actually creating a scroll
1699 bar, use nil to indicate that no color has been
1700 specified. */
1701 tem = Qnil;
1702 }
1703
1704 #else /* not USE_TOOLKIT_SCROLL_BARS */
1705
1706 tem = Qnil;
1707
1708 #endif /* not USE_TOOLKIT_SCROLL_BARS */
1709 }
1710
1711 x_set_frame_parameters (f, Fcons (Fcons (prop, tem), Qnil));
1712 return tem;
1713 }
1714
1715
1716
1717 \f
1718 #ifdef USE_X_TOOLKIT
1719
1720 /* If the WM_PROTOCOLS property does not already contain WM_TAKE_FOCUS,
1721 WM_DELETE_WINDOW, and WM_SAVE_YOURSELF, then add them. (They may
1722 already be present because of the toolkit (Motif adds some of them,
1723 for example, but Xt doesn't). */
1724
1725 static void
1726 hack_wm_protocols (FRAME_PTR f, Widget widget)
1727 {
1728 Display *dpy = XtDisplay (widget);
1729 Window w = XtWindow (widget);
1730 int need_delete = 1;
1731 int need_focus = 1;
1732 int need_save = 1;
1733
1734 block_input ();
1735 {
1736 Atom type;
1737 unsigned char *catoms;
1738 int format = 0;
1739 unsigned long nitems = 0;
1740 unsigned long bytes_after;
1741
1742 if ((XGetWindowProperty (dpy, w,
1743 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1744 (long)0, (long)100, False, XA_ATOM,
1745 &type, &format, &nitems, &bytes_after,
1746 &catoms)
1747 == Success)
1748 && format == 32 && type == XA_ATOM)
1749 {
1750 Atom *atoms = (Atom *) catoms;
1751 while (nitems > 0)
1752 {
1753 nitems--;
1754 if (atoms[nitems]
1755 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window)
1756 need_delete = 0;
1757 else if (atoms[nitems]
1758 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus)
1759 need_focus = 0;
1760 else if (atoms[nitems]
1761 == FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself)
1762 need_save = 0;
1763 }
1764 }
1765 if (catoms)
1766 XFree (catoms);
1767 }
1768 {
1769 Atom props [10];
1770 int count = 0;
1771 if (need_delete)
1772 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
1773 if (need_focus)
1774 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_take_focus;
1775 if (need_save)
1776 props[count++] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
1777 if (count)
1778 XChangeProperty (dpy, w, FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
1779 XA_ATOM, 32, PropModeAppend,
1780 (unsigned char *) props, count);
1781 }
1782 unblock_input ();
1783 }
1784 #endif
1785
1786
1787 \f
1788 /* Support routines for XIC (X Input Context). */
1789
1790 #ifdef HAVE_X_I18N
1791
1792 static XFontSet xic_create_xfontset (struct frame *);
1793 static XIMStyle best_xim_style (XIMStyles *, XIMStyles *);
1794
1795
1796 /* Supported XIM styles, ordered by preference. */
1797
1798 static XIMStyle supported_xim_styles[] =
1799 {
1800 XIMPreeditPosition | XIMStatusArea,
1801 XIMPreeditPosition | XIMStatusNothing,
1802 XIMPreeditPosition | XIMStatusNone,
1803 XIMPreeditNothing | XIMStatusArea,
1804 XIMPreeditNothing | XIMStatusNothing,
1805 XIMPreeditNothing | XIMStatusNone,
1806 XIMPreeditNone | XIMStatusArea,
1807 XIMPreeditNone | XIMStatusNothing,
1808 XIMPreeditNone | XIMStatusNone,
1809 0,
1810 };
1811
1812
1813 #if defined HAVE_X_WINDOWS && defined USE_X_TOOLKIT
1814 /* Create an X fontset on frame F with base font name BASE_FONTNAME. */
1815
1816 static const char xic_default_fontset[] = "-*-*-*-r-normal--14-*-*-*-*-*-*-*";
1817
1818 /* Create an Xt fontset spec from the name of a base font.
1819 If `motif' is True use the Motif syntax. */
1820 char *
1821 xic_create_fontsetname (const char *base_fontname, int motif)
1822 {
1823 const char *sep = motif ? ";" : ",";
1824 char *fontsetname;
1825
1826 /* Make a fontset name from the base font name. */
1827 if (xic_default_fontset == base_fontname)
1828 {
1829 /* There is no base font name, use the default. */
1830 fontsetname = xmalloc (strlen (base_fontname) + 2);
1831 strcpy (fontsetname, base_fontname);
1832 }
1833 else
1834 {
1835 /* Make a fontset name from the base font name.
1836 The font set will be made of the following elements:
1837 - the base font.
1838 - the base font where the charset spec is replaced by -*-*.
1839 - the same but with the family also replaced with -*-*-. */
1840 const char *p = base_fontname;
1841 ptrdiff_t i;
1842
1843 for (i = 0; *p; p++)
1844 if (*p == '-') i++;
1845 if (i != 14)
1846 {
1847 /* As the font name doesn't conform to XLFD, we can't
1848 modify it to generalize it to allcs and allfamilies.
1849 Use the specified font plus the default. */
1850 fontsetname = xmalloc (strlen (base_fontname)
1851 + strlen (xic_default_fontset) + 3);
1852 strcpy (fontsetname, base_fontname);
1853 strcat (fontsetname, sep);
1854 strcat (fontsetname, xic_default_fontset);
1855 }
1856 else
1857 {
1858 ptrdiff_t len;
1859 const char *p1 = NULL, *p2 = NULL, *p3 = NULL;
1860 char *font_allcs = NULL;
1861 char *font_allfamilies = NULL;
1862 char *font_all = NULL;
1863 const char *allcs = "*-*-*-*-*-*-*";
1864 const char *allfamilies = "-*-*-";
1865 const char *all = "*-*-*-*-";
1866 char *base;
1867
1868 for (i = 0, p = base_fontname; i < 8; p++)
1869 {
1870 if (*p == '-')
1871 {
1872 i++;
1873 if (i == 3)
1874 p1 = p + 1;
1875 else if (i == 7)
1876 p2 = p + 1;
1877 else if (i == 6)
1878 p3 = p + 1;
1879 }
1880 }
1881 /* If base_fontname specifies ADSTYLE, make it a
1882 wildcard. */
1883 if (*p3 != '*')
1884 {
1885 ptrdiff_t diff = (p2 - p3) - 2;
1886
1887 base = alloca (strlen (base_fontname) + 1);
1888 memcpy (base, base_fontname, p3 - base_fontname);
1889 base[p3 - base_fontname] = '*';
1890 base[(p3 - base_fontname) + 1] = '-';
1891 strcpy (base + (p3 - base_fontname) + 2, p2);
1892 p = base + (p - base_fontname) - diff;
1893 p1 = base + (p1 - base_fontname);
1894 p2 = base + (p2 - base_fontname) - diff;
1895 base_fontname = base;
1896 }
1897
1898 /* Build the font spec that matches all charsets. */
1899 len = p - base_fontname + strlen (allcs) + 1;
1900 font_allcs = alloca (len);
1901 memcpy (font_allcs, base_fontname, p - base_fontname);
1902 strcat (font_allcs, allcs);
1903
1904 /* Build the font spec that matches all families and
1905 add-styles. */
1906 len = p - p1 + strlen (allcs) + strlen (allfamilies) + 1;
1907 font_allfamilies = alloca (len);
1908 strcpy (font_allfamilies, allfamilies);
1909 memcpy (font_allfamilies + strlen (allfamilies), p1, p - p1);
1910 strcat (font_allfamilies, allcs);
1911
1912 /* Build the font spec that matches all. */
1913 len = p - p2 + strlen (allcs) + strlen (all) + strlen (allfamilies) + 1;
1914 font_all = alloca (len);
1915 strcpy (font_all, allfamilies);
1916 strcat (font_all, all);
1917 memcpy (font_all + strlen (all) + strlen (allfamilies), p2, p - p2);
1918 strcat (font_all, allcs);
1919
1920 /* Build the actual font set name. */
1921 len = strlen (base_fontname) + strlen (font_allcs)
1922 + strlen (font_allfamilies) + strlen (font_all) + 5;
1923 fontsetname = xmalloc (len);
1924 strcpy (fontsetname, base_fontname);
1925 strcat (fontsetname, sep);
1926 strcat (fontsetname, font_allcs);
1927 strcat (fontsetname, sep);
1928 strcat (fontsetname, font_allfamilies);
1929 strcat (fontsetname, sep);
1930 strcat (fontsetname, font_all);
1931 }
1932 }
1933 if (motif)
1934 strcat (fontsetname, ":");
1935 return fontsetname;
1936 }
1937 #endif /* HAVE_X_WINDOWS && USE_X_TOOLKIT */
1938
1939 #ifdef DEBUG_XIC_FONTSET
1940 static void
1941 print_fontset_result (XFontSet xfs, char *name, char **missing_list,
1942 int missing_count)
1943 {
1944 if (xfs)
1945 fprintf (stderr, "XIC Fontset created: %s\n", name);
1946 else
1947 {
1948 fprintf (stderr, "XIC Fontset failed: %s\n", name);
1949 while (missing_count-- > 0)
1950 {
1951 fprintf (stderr, " missing: %s\n", *missing_list);
1952 missing_list++;
1953 }
1954 }
1955
1956 }
1957 #endif
1958
1959 static XFontSet
1960 xic_create_xfontset (struct frame *f)
1961 {
1962 XFontSet xfs = NULL;
1963 struct font *font = FRAME_FONT (f);
1964 int pixel_size = font->pixel_size;
1965 Lisp_Object rest, frame;
1966
1967 /* See if there is another frame already using same fontset. */
1968 FOR_EACH_FRAME (rest, frame)
1969 {
1970 struct frame *cf = XFRAME (frame);
1971
1972 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
1973 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
1974 && FRAME_FONT (f)
1975 && FRAME_FONT (f)->pixel_size == pixel_size)
1976 {
1977 xfs = FRAME_XIC_FONTSET (cf);
1978 break;
1979 }
1980 }
1981
1982 if (! xfs)
1983 {
1984 char buf[256];
1985 char **missing_list;
1986 int missing_count;
1987 char *def_string;
1988 const char *xlfd_format = "-*-*-medium-r-normal--%d-*-*-*-*-*";
1989
1990 sprintf (buf, xlfd_format, pixel_size);
1991 missing_list = NULL;
1992 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
1993 &missing_list, &missing_count, &def_string);
1994 #ifdef DEBUG_XIC_FONTSET
1995 print_fontset_result (xfs, buf, missing_list, missing_count);
1996 #endif
1997 if (missing_list)
1998 XFreeStringList (missing_list);
1999 if (! xfs)
2000 {
2001 /* List of pixel sizes most likely available. Find one that
2002 is closest to pixel_size. */
2003 int sizes[] = {0, 8, 10, 11, 12, 14, 17, 18, 20, 24, 26, 34, 0};
2004 int *smaller, *larger;
2005
2006 for (smaller = sizes; smaller[1]; smaller++)
2007 if (smaller[1] >= pixel_size)
2008 break;
2009 larger = smaller + 1;
2010 if (*larger == pixel_size)
2011 larger++;
2012 while (*smaller || *larger)
2013 {
2014 int this_size;
2015
2016 if (! *larger)
2017 this_size = *smaller--;
2018 else if (! *smaller)
2019 this_size = *larger++;
2020 else if (pixel_size - *smaller < *larger - pixel_size)
2021 this_size = *smaller--;
2022 else
2023 this_size = *larger++;
2024 sprintf (buf, xlfd_format, this_size);
2025 missing_list = NULL;
2026 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), buf,
2027 &missing_list, &missing_count, &def_string);
2028 #ifdef DEBUG_XIC_FONTSET
2029 print_fontset_result (xfs, buf, missing_list, missing_count);
2030 #endif
2031 if (missing_list)
2032 XFreeStringList (missing_list);
2033 if (xfs)
2034 break;
2035 }
2036 }
2037 if (! xfs)
2038 {
2039 const char *last_resort = "-*-*-*-r-normal--*-*-*-*-*-*";
2040
2041 missing_list = NULL;
2042 xfs = XCreateFontSet (FRAME_X_DISPLAY (f), last_resort,
2043 &missing_list, &missing_count, &def_string);
2044 #ifdef DEBUG_XIC_FONTSET
2045 print_fontset_result (xfs, last_resort, missing_list, missing_count);
2046 #endif
2047 if (missing_list)
2048 XFreeStringList (missing_list);
2049 }
2050
2051 }
2052
2053 return xfs;
2054 }
2055
2056 /* Free the X fontset of frame F if it is the last frame using it. */
2057
2058 void
2059 xic_free_xfontset (struct frame *f)
2060 {
2061 Lisp_Object rest, frame;
2062 int shared_p = 0;
2063
2064 if (!FRAME_XIC_FONTSET (f))
2065 return;
2066
2067 /* See if there is another frame sharing the same fontset. */
2068 FOR_EACH_FRAME (rest, frame)
2069 {
2070 struct frame *cf = XFRAME (frame);
2071 if (cf != f && FRAME_LIVE_P (f) && FRAME_X_P (cf)
2072 && FRAME_X_DISPLAY_INFO (cf) == FRAME_X_DISPLAY_INFO (f)
2073 && FRAME_XIC_FONTSET (cf) == FRAME_XIC_FONTSET (f))
2074 {
2075 shared_p = 1;
2076 break;
2077 }
2078 }
2079
2080 if (!shared_p)
2081 /* The fontset is not used anymore. It is safe to free it. */
2082 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
2083
2084 if (FRAME_XIC_BASE_FONTNAME (f))
2085 xfree (FRAME_XIC_BASE_FONTNAME (f));
2086 FRAME_XIC_BASE_FONTNAME (f) = NULL;
2087 FRAME_XIC_FONTSET (f) = NULL;
2088 }
2089
2090
2091 /* Value is the best input style, given user preferences USER (already
2092 checked to be supported by Emacs), and styles supported by the
2093 input method XIM. */
2094
2095 static XIMStyle
2096 best_xim_style (XIMStyles *user, XIMStyles *xim)
2097 {
2098 int i, j;
2099
2100 for (i = 0; i < user->count_styles; ++i)
2101 for (j = 0; j < xim->count_styles; ++j)
2102 if (user->supported_styles[i] == xim->supported_styles[j])
2103 return user->supported_styles[i];
2104
2105 /* Return the default style. */
2106 return XIMPreeditNothing | XIMStatusNothing;
2107 }
2108
2109 /* Create XIC for frame F. */
2110
2111 static XIMStyle xic_style;
2112
2113 void
2114 create_frame_xic (struct frame *f)
2115 {
2116 XIM xim;
2117 XIC xic = NULL;
2118 XFontSet xfs = NULL;
2119
2120 if (FRAME_XIC (f))
2121 return;
2122
2123 /* Create X fontset. */
2124 xfs = xic_create_xfontset (f);
2125 xim = FRAME_X_XIM (f);
2126 if (xim)
2127 {
2128 XRectangle s_area;
2129 XPoint spot;
2130 XVaNestedList preedit_attr;
2131 XVaNestedList status_attr;
2132
2133 s_area.x = 0; s_area.y = 0; s_area.width = 1; s_area.height = 1;
2134 spot.x = 0; spot.y = 1;
2135
2136 /* Determine XIC style. */
2137 if (xic_style == 0)
2138 {
2139 XIMStyles supported_list;
2140 supported_list.count_styles = (sizeof supported_xim_styles
2141 / sizeof supported_xim_styles[0]);
2142 supported_list.supported_styles = supported_xim_styles;
2143 xic_style = best_xim_style (&supported_list,
2144 FRAME_X_XIM_STYLES (f));
2145 }
2146
2147 preedit_attr = XVaCreateNestedList (0,
2148 XNFontSet, xfs,
2149 XNForeground,
2150 FRAME_FOREGROUND_PIXEL (f),
2151 XNBackground,
2152 FRAME_BACKGROUND_PIXEL (f),
2153 (xic_style & XIMPreeditPosition
2154 ? XNSpotLocation
2155 : NULL),
2156 &spot,
2157 NULL);
2158 status_attr = XVaCreateNestedList (0,
2159 XNArea,
2160 &s_area,
2161 XNFontSet,
2162 xfs,
2163 XNForeground,
2164 FRAME_FOREGROUND_PIXEL (f),
2165 XNBackground,
2166 FRAME_BACKGROUND_PIXEL (f),
2167 NULL);
2168
2169 xic = XCreateIC (xim,
2170 XNInputStyle, xic_style,
2171 XNClientWindow, FRAME_X_WINDOW (f),
2172 XNFocusWindow, FRAME_X_WINDOW (f),
2173 XNStatusAttributes, status_attr,
2174 XNPreeditAttributes, preedit_attr,
2175 NULL);
2176 XFree (preedit_attr);
2177 XFree (status_attr);
2178 }
2179
2180 FRAME_XIC (f) = xic;
2181 FRAME_XIC_STYLE (f) = xic_style;
2182 FRAME_XIC_FONTSET (f) = xfs;
2183 }
2184
2185
2186 /* Destroy XIC and free XIC fontset of frame F, if any. */
2187
2188 void
2189 free_frame_xic (struct frame *f)
2190 {
2191 if (FRAME_XIC (f) == NULL)
2192 return;
2193
2194 XDestroyIC (FRAME_XIC (f));
2195 xic_free_xfontset (f);
2196
2197 FRAME_XIC (f) = NULL;
2198 }
2199
2200
2201 /* Place preedit area for XIC of window W's frame to specified
2202 pixel position X/Y. X and Y are relative to window W. */
2203
2204 void
2205 xic_set_preeditarea (struct window *w, int x, int y)
2206 {
2207 struct frame *f = XFRAME (w->frame);
2208 XVaNestedList attr;
2209 XPoint spot;
2210
2211 spot.x = WINDOW_TO_FRAME_PIXEL_X (w, x) + WINDOW_LEFT_FRINGE_WIDTH (w);
2212 spot.y = WINDOW_TO_FRAME_PIXEL_Y (w, y) + FONT_BASE (FRAME_FONT (f));
2213 attr = XVaCreateNestedList (0, XNSpotLocation, &spot, NULL);
2214 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2215 XFree (attr);
2216 }
2217
2218
2219 /* Place status area for XIC in bottom right corner of frame F.. */
2220
2221 void
2222 xic_set_statusarea (struct frame *f)
2223 {
2224 XIC xic = FRAME_XIC (f);
2225 XVaNestedList attr;
2226 XRectangle area;
2227 XRectangle *needed;
2228
2229 /* Negotiate geometry of status area. If input method has existing
2230 status area, use its current size. */
2231 area.x = area.y = area.width = area.height = 0;
2232 attr = XVaCreateNestedList (0, XNAreaNeeded, &area, NULL);
2233 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2234 XFree (attr);
2235
2236 attr = XVaCreateNestedList (0, XNAreaNeeded, &needed, NULL);
2237 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2238 XFree (attr);
2239
2240 if (needed->width == 0) /* Use XNArea instead of XNAreaNeeded */
2241 {
2242 attr = XVaCreateNestedList (0, XNArea, &needed, NULL);
2243 XGetICValues (xic, XNStatusAttributes, attr, NULL);
2244 XFree (attr);
2245 }
2246
2247 area.width = needed->width;
2248 area.height = needed->height;
2249 area.x = FRAME_PIXEL_WIDTH (f) - area.width - FRAME_INTERNAL_BORDER_WIDTH (f);
2250 area.y = (FRAME_PIXEL_HEIGHT (f) - area.height
2251 - FRAME_MENUBAR_HEIGHT (f)
2252 - FRAME_TOOLBAR_TOP_HEIGHT (f)
2253 - FRAME_INTERNAL_BORDER_WIDTH (f));
2254 XFree (needed);
2255
2256 attr = XVaCreateNestedList (0, XNArea, &area, NULL);
2257 XSetICValues (xic, XNStatusAttributes, attr, NULL);
2258 XFree (attr);
2259 }
2260
2261
2262 /* Set X fontset for XIC of frame F, using base font name
2263 BASE_FONTNAME. Called when a new Emacs fontset is chosen. */
2264
2265 void
2266 xic_set_xfontset (struct frame *f, const char *base_fontname)
2267 {
2268 XVaNestedList attr;
2269 XFontSet xfs;
2270
2271 xic_free_xfontset (f);
2272
2273 xfs = xic_create_xfontset (f);
2274
2275 attr = XVaCreateNestedList (0, XNFontSet, xfs, NULL);
2276 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
2277 XSetICValues (FRAME_XIC (f), XNPreeditAttributes, attr, NULL);
2278 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
2279 XSetICValues (FRAME_XIC (f), XNStatusAttributes, attr, NULL);
2280 XFree (attr);
2281
2282 FRAME_XIC_FONTSET (f) = xfs;
2283 }
2284
2285 #endif /* HAVE_X_I18N */
2286
2287
2288 \f
2289 #ifdef USE_X_TOOLKIT
2290
2291 /* Create and set up the X widget for frame F. */
2292
2293 static void
2294 x_window (struct frame *f, long window_prompting, int minibuffer_only)
2295 {
2296 XClassHint class_hints;
2297 XSetWindowAttributes attributes;
2298 unsigned long attribute_mask;
2299 Widget shell_widget;
2300 Widget pane_widget;
2301 Widget frame_widget;
2302 Arg al [25];
2303 int ac;
2304
2305 block_input ();
2306
2307 /* Use the resource name as the top-level widget name
2308 for looking up resources. Make a non-Lisp copy
2309 for the window manager, so GC relocation won't bother it.
2310
2311 Elsewhere we specify the window name for the window manager. */
2312
2313 {
2314 char *str = SSDATA (Vx_resource_name);
2315 f->namebuf = xmalloc (strlen (str) + 1);
2316 strcpy (f->namebuf, str);
2317 }
2318
2319 ac = 0;
2320 XtSetArg (al[ac], XtNallowShellResize, 1); ac++;
2321 XtSetArg (al[ac], XtNinput, 1); ac++;
2322 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2323 XtSetArg (al[ac], XtNborderWidth, f->border_width); ac++;
2324 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2325 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2326 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2327 shell_widget = XtAppCreateShell (f->namebuf, EMACS_CLASS,
2328 applicationShellWidgetClass,
2329 FRAME_X_DISPLAY (f), al, ac);
2330
2331 f->output_data.x->widget = shell_widget;
2332 /* maybe_set_screen_title_format (shell_widget); */
2333
2334 pane_widget = lw_create_widget ("main", "pane", widget_id_tick++,
2335 (widget_value *) NULL,
2336 shell_widget, False,
2337 (lw_callback) NULL,
2338 (lw_callback) NULL,
2339 (lw_callback) NULL,
2340 (lw_callback) NULL);
2341
2342 ac = 0;
2343 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2344 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2345 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2346 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2347 XtSetValues (pane_widget, al, ac);
2348 f->output_data.x->column_widget = pane_widget;
2349
2350 /* mappedWhenManaged to false tells to the paned window to not map/unmap
2351 the emacs screen when changing menubar. This reduces flickering. */
2352
2353 ac = 0;
2354 XtSetArg (al[ac], XtNmappedWhenManaged, 0); ac++;
2355 XtSetArg (al[ac], XtNshowGrip, 0); ac++;
2356 XtSetArg (al[ac], XtNallowResize, 1); ac++;
2357 XtSetArg (al[ac], XtNresizeToPreferred, 1); ac++;
2358 XtSetArg (al[ac], XtNemacsFrame, f); ac++;
2359 XtSetArg (al[ac], XtNvisual, FRAME_X_VISUAL (f)); ac++;
2360 XtSetArg (al[ac], XtNdepth, FRAME_X_DISPLAY_INFO (f)->n_planes); ac++;
2361 XtSetArg (al[ac], XtNcolormap, FRAME_X_COLORMAP (f)); ac++;
2362 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
2363 frame_widget = XtCreateWidget (f->namebuf, emacsFrameClass, pane_widget,
2364 al, ac);
2365
2366 f->output_data.x->edit_widget = frame_widget;
2367
2368 XtManageChild (frame_widget);
2369
2370 /* Do some needed geometry management. */
2371 {
2372 char *tem, shell_position[sizeof "=x++" + 4 * INT_STRLEN_BOUND (int)];
2373 Arg gal[10];
2374 int gac = 0;
2375 int extra_borders = 0;
2376 int menubar_size
2377 = (f->output_data.x->menubar_widget
2378 ? (f->output_data.x->menubar_widget->core.height
2379 + f->output_data.x->menubar_widget->core.border_width)
2380 : 0);
2381
2382 #if 0 /* Experimentally, we now get the right results
2383 for -geometry -0-0 without this. 24 Aug 96, rms. */
2384 if (FRAME_EXTERNAL_MENU_BAR (f))
2385 {
2386 Dimension ibw = 0;
2387 XtVaGetValues (pane_widget, XtNinternalBorderWidth, &ibw, NULL);
2388 menubar_size += ibw;
2389 }
2390 #endif
2391
2392 f->output_data.x->menubar_height = menubar_size;
2393
2394 #ifndef USE_LUCID
2395 /* Motif seems to need this amount added to the sizes
2396 specified for the shell widget. The Athena/Lucid widgets don't.
2397 Both conclusions reached experimentally. -- rms. */
2398 XtVaGetValues (f->output_data.x->edit_widget, XtNinternalBorderWidth,
2399 &extra_borders, NULL);
2400 extra_borders *= 2;
2401 #endif
2402
2403 /* Convert our geometry parameters into a geometry string
2404 and specify it.
2405 Note that we do not specify here whether the position
2406 is a user-specified or program-specified one.
2407 We pass that information later, in x_wm_set_size_hints. */
2408 {
2409 int left = f->left_pos;
2410 int xneg = window_prompting & XNegative;
2411 int top = f->top_pos;
2412 int yneg = window_prompting & YNegative;
2413 if (xneg)
2414 left = -left;
2415 if (yneg)
2416 top = -top;
2417
2418 if (window_prompting & USPosition)
2419 sprintf (shell_position, "=%dx%d%c%d%c%d",
2420 FRAME_PIXEL_WIDTH (f) + extra_borders,
2421 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders,
2422 (xneg ? '-' : '+'), left,
2423 (yneg ? '-' : '+'), top);
2424 else
2425 {
2426 sprintf (shell_position, "=%dx%d",
2427 FRAME_PIXEL_WIDTH (f) + extra_borders,
2428 FRAME_PIXEL_HEIGHT (f) + menubar_size + extra_borders);
2429
2430 /* Setting x and y when the position is not specified in
2431 the geometry string will set program position in the WM hints.
2432 If Emacs had just one program position, we could set it in
2433 fallback resources, but since each make-frame call can specify
2434 different program positions, this is easier. */
2435 XtSetArg (gal[gac], XtNx, left); gac++;
2436 XtSetArg (gal[gac], XtNy, top); gac++;
2437 }
2438 }
2439
2440 /* We don't free this because we don't know whether
2441 it is safe to free it while the frame exists.
2442 It isn't worth the trouble of arranging to free it
2443 when the frame is deleted. */
2444 tem = xstrdup (shell_position);
2445 XtSetArg (gal[gac], XtNgeometry, tem); gac++;
2446 XtSetValues (shell_widget, gal, gac);
2447 }
2448
2449 XtManageChild (pane_widget);
2450 XtRealizeWidget (shell_widget);
2451
2452 if (FRAME_X_EMBEDDED_P (f))
2453 XReparentWindow (FRAME_X_DISPLAY (f), XtWindow (shell_widget),
2454 f->output_data.x->parent_desc, 0, 0);
2455
2456 FRAME_X_WINDOW (f) = XtWindow (frame_widget);
2457
2458 validate_x_resource_name ();
2459
2460 class_hints.res_name = SSDATA (Vx_resource_name);
2461 class_hints.res_class = SSDATA (Vx_resource_class);
2462 XSetClassHint (FRAME_X_DISPLAY (f), XtWindow (shell_widget), &class_hints);
2463
2464 #ifdef HAVE_X_I18N
2465 FRAME_XIC (f) = NULL;
2466 if (use_xim)
2467 create_frame_xic (f);
2468 #endif
2469
2470 f->output_data.x->wm_hints.input = True;
2471 f->output_data.x->wm_hints.flags |= InputHint;
2472 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2473 &f->output_data.x->wm_hints);
2474
2475 hack_wm_protocols (f, shell_widget);
2476
2477 #ifdef HACK_EDITRES
2478 XtAddEventHandler (shell_widget, 0, True, _XEditResCheckMessages, 0);
2479 #endif
2480
2481 /* Do a stupid property change to force the server to generate a
2482 PropertyNotify event so that the event_stream server timestamp will
2483 be initialized to something relevant to the time we created the window.
2484 */
2485 XChangeProperty (XtDisplay (frame_widget), XtWindow (frame_widget),
2486 FRAME_X_DISPLAY_INFO (f)->Xatom_wm_protocols,
2487 XA_ATOM, 32, PropModeAppend,
2488 (unsigned char*) NULL, 0);
2489
2490 /* Make all the standard events reach the Emacs frame. */
2491 attributes.event_mask = STANDARD_EVENT_SET;
2492
2493 #ifdef HAVE_X_I18N
2494 if (FRAME_XIC (f))
2495 {
2496 /* XIM server might require some X events. */
2497 unsigned long fevent = NoEventMask;
2498 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2499 attributes.event_mask |= fevent;
2500 }
2501 #endif /* HAVE_X_I18N */
2502
2503 attribute_mask = CWEventMask;
2504 XChangeWindowAttributes (XtDisplay (shell_widget), XtWindow (shell_widget),
2505 attribute_mask, &attributes);
2506
2507 XtMapWidget (frame_widget);
2508
2509 /* x_set_name normally ignores requests to set the name if the
2510 requested name is the same as the current name. This is the one
2511 place where that assumption isn't correct; f->name is set, but
2512 the X server hasn't been told. */
2513 {
2514 Lisp_Object name;
2515 int explicit = f->explicit_name;
2516
2517 f->explicit_name = 0;
2518 name = f->name;
2519 fset_name (f, Qnil);
2520 x_set_name (f, name, explicit);
2521 }
2522
2523 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2524 f->output_data.x->current_cursor
2525 = f->output_data.x->text_cursor);
2526
2527 unblock_input ();
2528
2529 /* This is a no-op, except under Motif. Make sure main areas are
2530 set to something reasonable, in case we get an error later. */
2531 lw_set_main_areas (pane_widget, 0, frame_widget);
2532 }
2533
2534 #else /* not USE_X_TOOLKIT */
2535 #ifdef USE_GTK
2536 static void
2537 x_window (FRAME_PTR f)
2538 {
2539 if (! xg_create_frame_widgets (f))
2540 error ("Unable to create window");
2541
2542 #ifdef HAVE_X_I18N
2543 FRAME_XIC (f) = NULL;
2544 if (use_xim)
2545 {
2546 block_input ();
2547 create_frame_xic (f);
2548 if (FRAME_XIC (f))
2549 {
2550 /* XIM server might require some X events. */
2551 unsigned long fevent = NoEventMask;
2552 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2553
2554 if (fevent != NoEventMask)
2555 {
2556 XSetWindowAttributes attributes;
2557 XWindowAttributes wattr;
2558 unsigned long attribute_mask;
2559
2560 XGetWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2561 &wattr);
2562 attributes.event_mask = wattr.your_event_mask | fevent;
2563 attribute_mask = CWEventMask;
2564 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2565 attribute_mask, &attributes);
2566 }
2567 }
2568 unblock_input ();
2569 }
2570 #endif
2571 }
2572
2573 #else /*! USE_GTK */
2574 /* Create and set up the X window for frame F. */
2575
2576 static void
2577 x_window (struct frame *f)
2578 {
2579 XClassHint class_hints;
2580 XSetWindowAttributes attributes;
2581 unsigned long attribute_mask;
2582
2583 attributes.background_pixel = FRAME_BACKGROUND_PIXEL (f);
2584 attributes.border_pixel = f->output_data.x->border_pixel;
2585 attributes.bit_gravity = StaticGravity;
2586 attributes.backing_store = NotUseful;
2587 attributes.save_under = True;
2588 attributes.event_mask = STANDARD_EVENT_SET;
2589 attributes.colormap = FRAME_X_COLORMAP (f);
2590 attribute_mask = (CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask
2591 | CWColormap);
2592
2593 block_input ();
2594 FRAME_X_WINDOW (f)
2595 = XCreateWindow (FRAME_X_DISPLAY (f),
2596 f->output_data.x->parent_desc,
2597 f->left_pos,
2598 f->top_pos,
2599 FRAME_PIXEL_WIDTH (f), FRAME_PIXEL_HEIGHT (f),
2600 f->border_width,
2601 CopyFromParent, /* depth */
2602 InputOutput, /* class */
2603 FRAME_X_VISUAL (f),
2604 attribute_mask, &attributes);
2605
2606 #ifdef HAVE_X_I18N
2607 if (use_xim)
2608 {
2609 create_frame_xic (f);
2610 if (FRAME_XIC (f))
2611 {
2612 /* XIM server might require some X events. */
2613 unsigned long fevent = NoEventMask;
2614 XGetICValues (FRAME_XIC (f), XNFilterEvents, &fevent, NULL);
2615 attributes.event_mask |= fevent;
2616 attribute_mask = CWEventMask;
2617 XChangeWindowAttributes (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2618 attribute_mask, &attributes);
2619 }
2620 }
2621 #endif /* HAVE_X_I18N */
2622
2623 validate_x_resource_name ();
2624
2625 class_hints.res_name = SSDATA (Vx_resource_name);
2626 class_hints.res_class = SSDATA (Vx_resource_class);
2627 XSetClassHint (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &class_hints);
2628
2629 /* The menubar is part of the ordinary display;
2630 it does not count in addition to the height of the window. */
2631 f->output_data.x->menubar_height = 0;
2632
2633 /* This indicates that we use the "Passive Input" input model.
2634 Unless we do this, we don't get the Focus{In,Out} events that we
2635 need to draw the cursor correctly. Accursed bureaucrats.
2636 XWhipsAndChains (FRAME_X_DISPLAY (f), IronMaiden, &TheRack); */
2637
2638 f->output_data.x->wm_hints.input = True;
2639 f->output_data.x->wm_hints.flags |= InputHint;
2640 XSetWMHints (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2641 &f->output_data.x->wm_hints);
2642 f->output_data.x->wm_hints.icon_pixmap = None;
2643
2644 /* Request "save yourself" and "delete window" commands from wm. */
2645 {
2646 Atom protocols[2];
2647 protocols[0] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_delete_window;
2648 protocols[1] = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_save_yourself;
2649 XSetWMProtocols (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), protocols, 2);
2650 }
2651
2652 /* x_set_name normally ignores requests to set the name if the
2653 requested name is the same as the current name. This is the one
2654 place where that assumption isn't correct; f->name is set, but
2655 the X server hasn't been told. */
2656 {
2657 Lisp_Object name;
2658 int explicit = f->explicit_name;
2659
2660 f->explicit_name = 0;
2661 name = f->name;
2662 fset_name (f, Qnil);
2663 x_set_name (f, name, explicit);
2664 }
2665
2666 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2667 f->output_data.x->current_cursor
2668 = f->output_data.x->text_cursor);
2669
2670 unblock_input ();
2671
2672 if (FRAME_X_WINDOW (f) == 0)
2673 error ("Unable to create window");
2674 }
2675
2676 #endif /* not USE_GTK */
2677 #endif /* not USE_X_TOOLKIT */
2678
2679 /* Verify that the icon position args for this window are valid. */
2680
2681 static void
2682 x_icon_verify (struct frame *f, Lisp_Object parms)
2683 {
2684 Lisp_Object icon_x, icon_y;
2685
2686 /* Set the position of the icon. Note that twm groups all
2687 icons in an icon window. */
2688 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2689 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2690 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2691 {
2692 CHECK_NUMBER (icon_x);
2693 CHECK_NUMBER (icon_y);
2694 }
2695 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2696 error ("Both left and top icon corners of icon must be specified");
2697 }
2698
2699 /* Handle the icon stuff for this window. Perhaps later we might
2700 want an x_set_icon_position which can be called interactively as
2701 well. */
2702
2703 static void
2704 x_icon (struct frame *f, Lisp_Object parms)
2705 {
2706 Lisp_Object icon_x, icon_y;
2707 #if 0
2708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2709 #endif
2710
2711 /* Set the position of the icon. Note that twm groups all
2712 icons in an icon window. */
2713 icon_x = x_frame_get_and_record_arg (f, parms, Qicon_left, 0, 0, RES_TYPE_NUMBER);
2714 icon_y = x_frame_get_and_record_arg (f, parms, Qicon_top, 0, 0, RES_TYPE_NUMBER);
2715 if (!EQ (icon_x, Qunbound) && !EQ (icon_y, Qunbound))
2716 {
2717 CHECK_TYPE_RANGED_INTEGER (int, icon_x);
2718 CHECK_TYPE_RANGED_INTEGER (int, icon_y);
2719 }
2720 else if (!EQ (icon_x, Qunbound) || !EQ (icon_y, Qunbound))
2721 error ("Both left and top icon corners of icon must be specified");
2722
2723 block_input ();
2724
2725 if (! EQ (icon_x, Qunbound))
2726 x_wm_set_icon_position (f, XINT (icon_x), XINT (icon_y));
2727
2728 #if 0 /* x_get_arg removes the visibility parameter as a side effect,
2729 but x_create_frame still needs it. */
2730 /* Start up iconic or window? */
2731 x_wm_set_window_state
2732 (f, (EQ (x_get_arg (dpyinfo, parms, Qvisibility, 0, 0, RES_TYPE_SYMBOL),
2733 Qicon)
2734 ? IconicState
2735 : NormalState));
2736 #endif
2737
2738 x_text_icon (f, SSDATA ((!NILP (f->icon_name)
2739 ? f->icon_name
2740 : f->name)));
2741
2742 unblock_input ();
2743 }
2744
2745 /* Make the GCs needed for this window, setting the
2746 background, border and mouse colors; also create the
2747 mouse cursor and the gray border tile. */
2748
2749 static void
2750 x_make_gc (struct frame *f)
2751 {
2752 XGCValues gc_values;
2753
2754 block_input ();
2755
2756 /* Create the GCs of this frame.
2757 Note that many default values are used. */
2758
2759 gc_values.foreground = FRAME_FOREGROUND_PIXEL (f);
2760 gc_values.background = FRAME_BACKGROUND_PIXEL (f);
2761 gc_values.line_width = 0; /* Means 1 using fast algorithm. */
2762 f->output_data.x->normal_gc
2763 = XCreateGC (FRAME_X_DISPLAY (f),
2764 FRAME_X_WINDOW (f),
2765 GCLineWidth | GCForeground | GCBackground,
2766 &gc_values);
2767
2768 /* Reverse video style. */
2769 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2770 gc_values.background = FRAME_FOREGROUND_PIXEL (f);
2771 f->output_data.x->reverse_gc
2772 = XCreateGC (FRAME_X_DISPLAY (f),
2773 FRAME_X_WINDOW (f),
2774 GCForeground | GCBackground | GCLineWidth,
2775 &gc_values);
2776
2777 /* Cursor has cursor-color background, background-color foreground. */
2778 gc_values.foreground = FRAME_BACKGROUND_PIXEL (f);
2779 gc_values.background = f->output_data.x->cursor_pixel;
2780 gc_values.fill_style = FillOpaqueStippled;
2781 f->output_data.x->cursor_gc
2782 = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2783 (GCForeground | GCBackground
2784 | GCFillStyle | GCLineWidth),
2785 &gc_values);
2786
2787 /* Reliefs. */
2788 f->output_data.x->white_relief.gc = 0;
2789 f->output_data.x->black_relief.gc = 0;
2790
2791 /* Create the gray border tile used when the pointer is not in
2792 the frame. Since this depends on the frame's pixel values,
2793 this must be done on a per-frame basis. */
2794 f->output_data.x->border_tile
2795 = (XCreatePixmapFromBitmapData
2796 (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
2797 gray_bits, gray_width, gray_height,
2798 FRAME_FOREGROUND_PIXEL (f),
2799 FRAME_BACKGROUND_PIXEL (f),
2800 DefaultDepth (FRAME_X_DISPLAY (f), FRAME_X_SCREEN_NUMBER (f))));
2801
2802 unblock_input ();
2803 }
2804
2805
2806 /* Free what was allocated in x_make_gc. */
2807
2808 void
2809 x_free_gcs (struct frame *f)
2810 {
2811 Display *dpy = FRAME_X_DISPLAY (f);
2812
2813 block_input ();
2814
2815 if (f->output_data.x->normal_gc)
2816 {
2817 XFreeGC (dpy, f->output_data.x->normal_gc);
2818 f->output_data.x->normal_gc = 0;
2819 }
2820
2821 if (f->output_data.x->reverse_gc)
2822 {
2823 XFreeGC (dpy, f->output_data.x->reverse_gc);
2824 f->output_data.x->reverse_gc = 0;
2825 }
2826
2827 if (f->output_data.x->cursor_gc)
2828 {
2829 XFreeGC (dpy, f->output_data.x->cursor_gc);
2830 f->output_data.x->cursor_gc = 0;
2831 }
2832
2833 if (f->output_data.x->border_tile)
2834 {
2835 XFreePixmap (dpy, f->output_data.x->border_tile);
2836 f->output_data.x->border_tile = 0;
2837 }
2838
2839 unblock_input ();
2840 }
2841
2842
2843 /* Handler for signals raised during x_create_frame and
2844 x_create_tip_frame. FRAME is the frame which is partially
2845 constructed. */
2846
2847 static Lisp_Object
2848 unwind_create_frame (Lisp_Object frame)
2849 {
2850 struct frame *f = XFRAME (frame);
2851
2852 /* If frame is already dead, nothing to do. This can happen if the
2853 display is disconnected after the frame has become official, but
2854 before x_create_frame removes the unwind protect. */
2855 if (!FRAME_LIVE_P (f))
2856 return Qnil;
2857
2858 /* If frame is ``official'', nothing to do. */
2859 if (NILP (Fmemq (frame, Vframe_list)))
2860 {
2861 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2862 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2863 #endif
2864
2865 x_free_frame_resources (f);
2866 free_glyphs (f);
2867
2868 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2869 /* Check that reference counts are indeed correct. */
2870 eassert (dpyinfo->reference_count == dpyinfo_refcount);
2871 eassert (dpyinfo->terminal->image_cache->refcount == image_cache_refcount);
2872 #endif
2873 return Qt;
2874 }
2875
2876 return Qnil;
2877 }
2878
2879 static Lisp_Object
2880 unwind_create_frame_1 (Lisp_Object val)
2881 {
2882 inhibit_lisp_code = val;
2883 return Qnil;
2884 }
2885
2886 static void
2887 x_default_font_parameter (struct frame *f, Lisp_Object parms)
2888 {
2889 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2890 Lisp_Object font_param = x_get_arg (dpyinfo, parms, Qfont, NULL, NULL,
2891 RES_TYPE_STRING);
2892 Lisp_Object font = Qnil;
2893 if (EQ (font_param, Qunbound))
2894 font_param = Qnil;
2895
2896 if (NILP (font_param))
2897 {
2898 /* System font should take precedence over X resources. We suggest this
2899 regardless of font-use-system-font because .emacs may not have been
2900 read yet. */
2901 const char *system_font = xsettings_get_system_font ();
2902 if (system_font)
2903 font = font_open_by_name (f, build_unibyte_string (system_font));
2904 }
2905
2906 if (NILP (font))
2907 font = !NILP (font_param) ? font_param
2908 : x_get_arg (dpyinfo, parms, Qfont, "font", "Font", RES_TYPE_STRING);
2909
2910 if (! FONTP (font) && ! STRINGP (font))
2911 {
2912 const char *names[]
2913 = {
2914 #ifdef HAVE_XFT
2915 /* This will find the normal Xft font. */
2916 "monospace-10",
2917 #endif
2918 "-adobe-courier-medium-r-*-*-*-120-*-*-*-*-iso8859-1",
2919 "-misc-fixed-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2920 "-*-*-medium-r-normal-*-*-140-*-*-c-*-iso8859-1",
2921 /* This was formerly the first thing tried, but it finds
2922 too many fonts and takes too long. */
2923 "-*-*-medium-r-*-*-*-*-*-*-c-*-iso8859-1",
2924 /* If those didn't work, look for something which will
2925 at least work. */
2926 "-*-fixed-*-*-*-*-*-140-*-*-c-*-iso8859-1",
2927 "fixed",
2928 NULL };
2929 int i;
2930
2931 for (i = 0; names[i]; i++)
2932 {
2933 font = font_open_by_name (f, build_unibyte_string (names[i]));
2934 if (! NILP (font))
2935 break;
2936 }
2937 if (NILP (font))
2938 error ("No suitable font was found");
2939 }
2940 else if (!NILP (font_param))
2941 {
2942 /* Remember the explicit font parameter, so we can re-apply it after
2943 we've applied the `default' face settings. */
2944 x_set_frame_parameters (f, Fcons (Fcons (Qfont_param, font_param), Qnil));
2945 }
2946
2947 /* This call will make X resources override any system font setting. */
2948 x_default_parameter (f, parms, Qfont, font, "font", "Font", RES_TYPE_STRING);
2949 }
2950
2951
2952 DEFUN ("x-wm-set-size-hint", Fx_wm_set_size_hint, Sx_wm_set_size_hint,
2953 0, 1, 0,
2954 doc: /* Send the size hints for frame FRAME to the window manager.
2955 If FRAME is omitted or nil, use the selected frame.
2956 Signal error if FRAME is not an X frame. */)
2957 (Lisp_Object frame)
2958 {
2959 struct frame *f = decode_window_system_frame (frame);
2960
2961 block_input ();
2962 x_wm_set_size_hint (f, 0, 0);
2963 unblock_input ();
2964 return Qnil;
2965 }
2966
2967 static void
2968 set_machine_and_pid_properties (struct frame *f)
2969 {
2970 long pid = (long) getpid ();
2971
2972 /* This will set WM_CLIENT_MACHINE and WM_LOCALE_NAME. */
2973 XSetWMProperties (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), NULL, NULL,
2974 NULL, 0, NULL, NULL, NULL);
2975 XChangeProperty (FRAME_X_DISPLAY (f),
2976 FRAME_OUTER_WINDOW (f),
2977 XInternAtom (FRAME_X_DISPLAY (f),
2978 "_NET_WM_PID",
2979 False),
2980 XA_CARDINAL, 32, PropModeReplace,
2981 (unsigned char *) &pid, 1);
2982 }
2983
2984 DEFUN ("x-create-frame", Fx_create_frame, Sx_create_frame,
2985 1, 1, 0,
2986 doc: /* Make a new X window, which is called a "frame" in Emacs terms.
2987 Return an Emacs frame object.
2988 PARMS is an alist of frame parameters.
2989 If the parameters specify that the frame should not have a minibuffer,
2990 and do not specify a specific minibuffer window to use,
2991 then `default-minibuffer-frame' must be a frame whose minibuffer can
2992 be shared by the new frame.
2993
2994 This function is an internal primitive--use `make-frame' instead. */)
2995 (Lisp_Object parms)
2996 {
2997 struct frame *f;
2998 Lisp_Object frame, tem;
2999 Lisp_Object name;
3000 int minibuffer_only = 0;
3001 long window_prompting = 0;
3002 int width, height;
3003 ptrdiff_t count = SPECPDL_INDEX ();
3004 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
3005 Lisp_Object display;
3006 struct x_display_info *dpyinfo = NULL;
3007 Lisp_Object parent;
3008 struct kboard *kb;
3009
3010 parms = Fcopy_alist (parms);
3011
3012 /* Use this general default value to start with
3013 until we know if this frame has a specified name. */
3014 Vx_resource_name = Vinvocation_name;
3015
3016 display = x_get_arg (dpyinfo, parms, Qterminal, 0, 0, RES_TYPE_NUMBER);
3017 if (EQ (display, Qunbound))
3018 display = x_get_arg (dpyinfo, parms, Qdisplay, 0, 0, RES_TYPE_STRING);
3019 if (EQ (display, Qunbound))
3020 display = Qnil;
3021 dpyinfo = check_x_display_info (display);
3022 kb = dpyinfo->terminal->kboard;
3023
3024 if (!dpyinfo->terminal->name)
3025 error ("Terminal is not live, can't create new frames on it");
3026
3027 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
3028 if (!STRINGP (name)
3029 && ! EQ (name, Qunbound)
3030 && ! NILP (name))
3031 error ("Invalid frame name--not a string or nil");
3032
3033 if (STRINGP (name))
3034 Vx_resource_name = name;
3035
3036 /* See if parent window is specified. */
3037 parent = x_get_arg (dpyinfo, parms, Qparent_id, NULL, NULL, RES_TYPE_NUMBER);
3038 if (EQ (parent, Qunbound))
3039 parent = Qnil;
3040 if (! NILP (parent))
3041 CHECK_NUMBER (parent);
3042
3043 /* make_frame_without_minibuffer can run Lisp code and garbage collect. */
3044 /* No need to protect DISPLAY because that's not used after passing
3045 it to make_frame_without_minibuffer. */
3046 frame = Qnil;
3047 GCPRO4 (parms, parent, name, frame);
3048 tem = x_get_arg (dpyinfo, parms, Qminibuffer, "minibuffer", "Minibuffer",
3049 RES_TYPE_SYMBOL);
3050 if (EQ (tem, Qnone) || NILP (tem))
3051 f = make_frame_without_minibuffer (Qnil, kb, display);
3052 else if (EQ (tem, Qonly))
3053 {
3054 f = make_minibuffer_frame ();
3055 minibuffer_only = 1;
3056 }
3057 else if (WINDOWP (tem))
3058 f = make_frame_without_minibuffer (tem, kb, display);
3059 else
3060 f = make_frame (1);
3061
3062 XSETFRAME (frame, f);
3063
3064 f->terminal = dpyinfo->terminal;
3065
3066 f->output_method = output_x_window;
3067 f->output_data.x = xzalloc (sizeof *f->output_data.x);
3068 f->output_data.x->icon_bitmap = -1;
3069 FRAME_FONTSET (f) = -1;
3070 f->output_data.x->scroll_bar_foreground_pixel = -1;
3071 f->output_data.x->scroll_bar_background_pixel = -1;
3072 #ifdef USE_TOOLKIT_SCROLL_BARS
3073 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
3074 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
3075 #endif /* USE_TOOLKIT_SCROLL_BARS */
3076
3077 fset_icon_name (f,
3078 x_get_arg (dpyinfo, parms, Qicon_name, "iconName", "Title",
3079 RES_TYPE_STRING));
3080 if (! STRINGP (f->icon_name))
3081 fset_icon_name (f, Qnil);
3082
3083 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
3084
3085 /* With FRAME_X_DISPLAY_INFO set up, this unwind-protect is safe. */
3086 record_unwind_protect (unwind_create_frame, frame);
3087
3088 /* These colors will be set anyway later, but it's important
3089 to get the color reference counts right, so initialize them! */
3090 {
3091 Lisp_Object black;
3092 struct gcpro gcpro1;
3093
3094 /* Function x_decode_color can signal an error. Make
3095 sure to initialize color slots so that we won't try
3096 to free colors we haven't allocated. */
3097 FRAME_FOREGROUND_PIXEL (f) = -1;
3098 FRAME_BACKGROUND_PIXEL (f) = -1;
3099 f->output_data.x->cursor_pixel = -1;
3100 f->output_data.x->cursor_foreground_pixel = -1;
3101 f->output_data.x->border_pixel = -1;
3102 f->output_data.x->mouse_pixel = -1;
3103
3104 black = build_string ("black");
3105 GCPRO1 (black);
3106 FRAME_FOREGROUND_PIXEL (f)
3107 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3108 FRAME_BACKGROUND_PIXEL (f)
3109 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3110 f->output_data.x->cursor_pixel
3111 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3112 f->output_data.x->cursor_foreground_pixel
3113 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3114 f->output_data.x->border_pixel
3115 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3116 f->output_data.x->mouse_pixel
3117 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
3118 UNGCPRO;
3119 }
3120
3121 /* Specify the parent under which to make this X window. */
3122
3123 if (!NILP (parent))
3124 {
3125 f->output_data.x->parent_desc = (Window) XFASTINT (parent);
3126 f->output_data.x->explicit_parent = 1;
3127 }
3128 else
3129 {
3130 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
3131 f->output_data.x->explicit_parent = 0;
3132 }
3133
3134 /* Set the name; the functions to which we pass f expect the name to
3135 be set. */
3136 if (EQ (name, Qunbound) || NILP (name))
3137 {
3138 fset_name (f, build_string (dpyinfo->x_id_name));
3139 f->explicit_name = 0;
3140 }
3141 else
3142 {
3143 fset_name (f, name);
3144 f->explicit_name = 1;
3145 /* use the frame's title when getting resources for this frame. */
3146 specbind (Qx_resource_name, name);
3147 }
3148
3149 #ifdef HAVE_FREETYPE
3150 #ifdef HAVE_XFT
3151 register_font_driver (&xftfont_driver, f);
3152 #else /* not HAVE_XFT */
3153 register_font_driver (&ftxfont_driver, f);
3154 #endif /* not HAVE_XFT */
3155 #endif /* HAVE_FREETYPE */
3156 register_font_driver (&xfont_driver, f);
3157
3158 x_default_parameter (f, parms, Qfont_backend, Qnil,
3159 "fontBackend", "FontBackend", RES_TYPE_STRING);
3160
3161 /* Extract the window parameters from the supplied values
3162 that are needed to determine window geometry. */
3163 x_default_font_parameter (f, parms);
3164 if (!FRAME_FONT (f))
3165 {
3166 delete_frame (frame, Qnoelisp);
3167 error ("Invalid frame font");
3168 }
3169
3170 /* Frame contents get displaced if an embedded X window has a border. */
3171 if (! FRAME_X_EMBEDDED_P (f))
3172 x_default_parameter (f, parms, Qborder_width, make_number (0),
3173 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
3174
3175 /* This defaults to 1 in order to match xterm. We recognize either
3176 internalBorderWidth or internalBorder (which is what xterm calls
3177 it). */
3178 if (NILP (Fassq (Qinternal_border_width, parms)))
3179 {
3180 Lisp_Object value;
3181
3182 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
3183 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
3184 if (! EQ (value, Qunbound))
3185 parms = Fcons (Fcons (Qinternal_border_width, value),
3186 parms);
3187 }
3188 x_default_parameter (f, parms, Qinternal_border_width,
3189 #ifdef USE_GTK /* We used to impose 0 in xg_create_frame_widgets. */
3190 make_number (0),
3191 #else
3192 make_number (1),
3193 #endif
3194 "internalBorderWidth", "internalBorderWidth",
3195 RES_TYPE_NUMBER);
3196 x_default_parameter (f, parms, Qvertical_scroll_bars,
3197 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
3198 Qright,
3199 #else
3200 Qleft,
3201 #endif
3202 "verticalScrollBars", "ScrollBars",
3203 RES_TYPE_SYMBOL);
3204
3205 /* Also do the stuff which must be set before the window exists. */
3206 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
3207 "foreground", "Foreground", RES_TYPE_STRING);
3208 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
3209 "background", "Background", RES_TYPE_STRING);
3210 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
3211 "pointerColor", "Foreground", RES_TYPE_STRING);
3212 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
3213 "borderColor", "BorderColor", RES_TYPE_STRING);
3214 x_default_parameter (f, parms, Qscreen_gamma, Qnil,
3215 "screenGamma", "ScreenGamma", RES_TYPE_FLOAT);
3216 x_default_parameter (f, parms, Qline_spacing, Qnil,
3217 "lineSpacing", "LineSpacing", RES_TYPE_NUMBER);
3218 x_default_parameter (f, parms, Qleft_fringe, Qnil,
3219 "leftFringe", "LeftFringe", RES_TYPE_NUMBER);
3220 x_default_parameter (f, parms, Qright_fringe, Qnil,
3221 "rightFringe", "RightFringe", RES_TYPE_NUMBER);
3222
3223 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_foreground,
3224 "scrollBarForeground",
3225 "ScrollBarForeground", 1);
3226 x_default_scroll_bar_color_parameter (f, parms, Qscroll_bar_background,
3227 "scrollBarBackground",
3228 "ScrollBarBackground", 0);
3229
3230 #ifdef GLYPH_DEBUG
3231 image_cache_refcount =
3232 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
3233 dpyinfo_refcount = dpyinfo->reference_count;
3234 #endif /* GLYPH_DEBUG */
3235
3236 /* Init faces before x_default_parameter is called for scroll-bar
3237 parameters because that function calls x_set_scroll_bar_width,
3238 which calls change_frame_size, which calls Fset_window_buffer,
3239 which runs hooks, which call Fvertical_motion. At the end, we
3240 end up in init_iterator with a null face cache, which should not
3241 happen. */
3242 init_frame_faces (f);
3243
3244 /* Set the menu-bar-lines and tool-bar-lines parameters. We don't
3245 look up the X resources controlling the menu-bar and tool-bar
3246 here; they are processed specially at startup, and reflected in
3247 the values of the mode variables.
3248
3249 Avoid calling window-configuration-change-hook; otherwise we
3250 could get an infloop in next_frame since the frame is not yet in
3251 Vframe_list. */
3252 {
3253 ptrdiff_t count2 = SPECPDL_INDEX ();
3254 record_unwind_protect (unwind_create_frame_1, inhibit_lisp_code);
3255 inhibit_lisp_code = Qt;
3256
3257 x_default_parameter (f, parms, Qmenu_bar_lines,
3258 NILP (Vmenu_bar_mode)
3259 ? make_number (0) : make_number (1),
3260 NULL, NULL, RES_TYPE_NUMBER);
3261 x_default_parameter (f, parms, Qtool_bar_lines,
3262 NILP (Vtool_bar_mode)
3263 ? make_number (0) : make_number (1),
3264 NULL, NULL, RES_TYPE_NUMBER);
3265
3266 unbind_to (count2, Qnil);
3267 }
3268
3269 x_default_parameter (f, parms, Qbuffer_predicate, Qnil,
3270 "bufferPredicate", "BufferPredicate",
3271 RES_TYPE_SYMBOL);
3272 x_default_parameter (f, parms, Qtitle, Qnil,
3273 "title", "Title", RES_TYPE_STRING);
3274 x_default_parameter (f, parms, Qwait_for_wm, Qt,
3275 "waitForWM", "WaitForWM", RES_TYPE_BOOLEAN);
3276 x_default_parameter (f, parms, Qfullscreen, Qnil,
3277 "fullscreen", "Fullscreen", RES_TYPE_SYMBOL);
3278 x_default_parameter (f, parms, Qtool_bar_position,
3279 f->tool_bar_position, 0, 0, RES_TYPE_SYMBOL);
3280
3281 /* Compute the size of the X window. */
3282 window_prompting = x_figure_window_size (f, parms, 1);
3283
3284 tem = x_get_arg (dpyinfo, parms, Qunsplittable, 0, 0, RES_TYPE_BOOLEAN);
3285 f->no_split = minibuffer_only || EQ (tem, Qt);
3286
3287 x_icon_verify (f, parms);
3288
3289 /* Create the X widget or window. */
3290 #ifdef USE_X_TOOLKIT
3291 x_window (f, window_prompting, minibuffer_only);
3292 #else
3293 x_window (f);
3294 #endif
3295
3296 x_icon (f, parms);
3297 x_make_gc (f);
3298
3299 /* Now consider the frame official. */
3300 f->terminal->reference_count++;
3301 FRAME_X_DISPLAY_INFO (f)->reference_count++;
3302 Vframe_list = Fcons (frame, Vframe_list);
3303
3304 /* We need to do this after creating the X window, so that the
3305 icon-creation functions can say whose icon they're describing. */
3306 x_default_parameter (f, parms, Qicon_type, Qt,
3307 "bitmapIcon", "BitmapIcon", RES_TYPE_BOOLEAN);
3308
3309 x_default_parameter (f, parms, Qauto_raise, Qnil,
3310 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3311 x_default_parameter (f, parms, Qauto_lower, Qnil,
3312 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
3313 x_default_parameter (f, parms, Qcursor_type, Qbox,
3314 "cursorType", "CursorType", RES_TYPE_SYMBOL);
3315 x_default_parameter (f, parms, Qscroll_bar_width, Qnil,
3316 "scrollBarWidth", "ScrollBarWidth",
3317 RES_TYPE_NUMBER);
3318 x_default_parameter (f, parms, Qalpha, Qnil,
3319 "alpha", "Alpha", RES_TYPE_NUMBER);
3320
3321 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
3322 Change will not be effected unless different from the current
3323 FRAME_LINES (f). */
3324 width = FRAME_COLS (f);
3325 height = FRAME_LINES (f);
3326
3327 SET_FRAME_COLS (f, 0);
3328 FRAME_LINES (f) = 0;
3329 change_frame_size (f, height, width, 1, 0, 0);
3330
3331 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
3332 /* Create the menu bar. */
3333 if (!minibuffer_only && FRAME_EXTERNAL_MENU_BAR (f))
3334 {
3335 /* If this signals an error, we haven't set size hints for the
3336 frame and we didn't make it visible. */
3337 initialize_frame_menubar (f);
3338
3339 #ifndef USE_GTK
3340 /* This is a no-op, except under Motif where it arranges the
3341 main window for the widgets on it. */
3342 lw_set_main_areas (f->output_data.x->column_widget,
3343 f->output_data.x->menubar_widget,
3344 f->output_data.x->edit_widget);
3345 #endif /* not USE_GTK */
3346 }
3347 #endif /* USE_X_TOOLKIT || USE_GTK */
3348
3349 /* Tell the server what size and position, etc, we want, and how
3350 badly we want them. This should be done after we have the menu
3351 bar so that its size can be taken into account. */
3352 block_input ();
3353 x_wm_set_size_hint (f, window_prompting, 0);
3354 unblock_input ();
3355
3356 /* Make the window appear on the frame and enable display, unless
3357 the caller says not to. However, with explicit parent, Emacs
3358 cannot control visibility, so don't try. */
3359 if (! f->output_data.x->explicit_parent)
3360 {
3361 Lisp_Object visibility;
3362
3363 visibility = x_get_arg (dpyinfo, parms, Qvisibility, 0, 0,
3364 RES_TYPE_SYMBOL);
3365 if (EQ (visibility, Qunbound))
3366 visibility = Qt;
3367
3368 if (EQ (visibility, Qicon))
3369 x_iconify_frame (f);
3370 else if (! NILP (visibility))
3371 x_make_frame_visible (f);
3372 else
3373 {
3374 /* Must have been Qnil. */
3375 }
3376 }
3377
3378 block_input ();
3379
3380 /* Set machine name and pid for the purpose of window managers. */
3381 set_machine_and_pid_properties (f);
3382
3383 /* Set the WM leader property. GTK does this itself, so this is not
3384 needed when using GTK. */
3385 if (dpyinfo->client_leader_window != 0)
3386 {
3387 XChangeProperty (FRAME_X_DISPLAY (f),
3388 FRAME_OUTER_WINDOW (f),
3389 dpyinfo->Xatom_wm_client_leader,
3390 XA_WINDOW, 32, PropModeReplace,
3391 (unsigned char *) &dpyinfo->client_leader_window, 1);
3392 }
3393
3394 unblock_input ();
3395
3396 /* Initialize `default-minibuffer-frame' in case this is the first
3397 frame on this terminal. */
3398 if (FRAME_HAS_MINIBUF_P (f)
3399 && (!FRAMEP (KVAR (kb, Vdefault_minibuffer_frame))
3400 || !FRAME_LIVE_P (XFRAME (KVAR (kb, Vdefault_minibuffer_frame)))))
3401 kset_default_minibuffer_frame (kb, frame);
3402
3403 /* All remaining specified parameters, which have not been "used"
3404 by x_get_arg and friends, now go in the misc. alist of the frame. */
3405 for (tem = parms; CONSP (tem); tem = XCDR (tem))
3406 if (CONSP (XCAR (tem)) && !NILP (XCAR (XCAR (tem))))
3407 fset_param_alist (f, Fcons (XCAR (tem), f->param_alist));
3408
3409 UNGCPRO;
3410
3411 /* Make sure windows on this frame appear in calls to next-window
3412 and similar functions. */
3413 Vwindow_list = Qnil;
3414
3415 return unbind_to (count, frame);
3416 }
3417
3418
3419 /* FRAME is used only to get a handle on the X display. We don't pass the
3420 display info directly because we're called from frame.c, which doesn't
3421 know about that structure. */
3422
3423 Lisp_Object
3424 x_get_focus_frame (struct frame *frame)
3425 {
3426 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (frame);
3427 Lisp_Object xfocus;
3428 if (! dpyinfo->x_focus_frame)
3429 return Qnil;
3430
3431 XSETFRAME (xfocus, dpyinfo->x_focus_frame);
3432 return xfocus;
3433 }
3434
3435
3436 /* In certain situations, when the window manager follows a
3437 click-to-focus policy, there seems to be no way around calling
3438 XSetInputFocus to give another frame the input focus .
3439
3440 In an ideal world, XSetInputFocus should generally be avoided so
3441 that applications don't interfere with the window manager's focus
3442 policy. But I think it's okay to use when it's clearly done
3443 following a user-command. */
3444
3445 DEFUN ("x-focus-frame", Fx_focus_frame, Sx_focus_frame, 1, 1, 0,
3446 doc: /* Set the input focus to FRAME.
3447 FRAME nil means use the selected frame. */)
3448 (Lisp_Object frame)
3449 {
3450 struct frame *f = decode_window_system_frame (frame);
3451 Display *dpy = FRAME_X_DISPLAY (f);
3452
3453 block_input ();
3454 x_catch_errors (dpy);
3455
3456 if (FRAME_X_EMBEDDED_P (f))
3457 {
3458 /* For Xembedded frames, normally the embedder forwards key
3459 events. See XEmbed Protocol Specification at
3460 http://freedesktop.org/wiki/Specifications/xembed-spec */
3461 xembed_request_focus (f);
3462 }
3463 else
3464 {
3465 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3466 RevertToParent, CurrentTime);
3467 x_ewmh_activate_frame (f);
3468 }
3469
3470 x_uncatch_errors ();
3471 unblock_input ();
3472
3473 return Qnil;
3474 }
3475
3476 \f
3477 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
3478 doc: /* Internal function called by `color-defined-p', which see
3479 .\(Note that the Nextstep version of this function ignores FRAME.) */)
3480 (Lisp_Object color, Lisp_Object frame)
3481 {
3482 XColor foo;
3483 FRAME_PTR f = decode_window_system_frame (frame);
3484
3485 CHECK_STRING (color);
3486
3487 if (x_defined_color (f, SSDATA (color), &foo, 0))
3488 return Qt;
3489 else
3490 return Qnil;
3491 }
3492
3493 DEFUN ("xw-color-values", Fxw_color_values, Sxw_color_values, 1, 2, 0,
3494 doc: /* Internal function called by `color-values', which see. */)
3495 (Lisp_Object color, Lisp_Object frame)
3496 {
3497 XColor foo;
3498 FRAME_PTR f = decode_window_system_frame (frame);
3499
3500 CHECK_STRING (color);
3501
3502 if (x_defined_color (f, SSDATA (color), &foo, 0))
3503 return list3i (foo.red, foo.green, foo.blue);
3504 else
3505 return Qnil;
3506 }
3507
3508 DEFUN ("xw-display-color-p", Fxw_display_color_p, Sxw_display_color_p, 0, 1, 0,
3509 doc: /* Internal function called by `display-color-p', which see. */)
3510 (Lisp_Object terminal)
3511 {
3512 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3513
3514 if (dpyinfo->n_planes <= 2)
3515 return Qnil;
3516
3517 switch (dpyinfo->visual->class)
3518 {
3519 case StaticColor:
3520 case PseudoColor:
3521 case TrueColor:
3522 case DirectColor:
3523 return Qt;
3524
3525 default:
3526 return Qnil;
3527 }
3528 }
3529
3530 DEFUN ("x-display-grayscale-p", Fx_display_grayscale_p, Sx_display_grayscale_p,
3531 0, 1, 0,
3532 doc: /* Return t if the X display supports shades of gray.
3533 Note that color displays do support shades of gray.
3534 The optional argument TERMINAL specifies which display to ask about.
3535 TERMINAL should be a terminal object, a frame or a display name (a string).
3536 If omitted or nil, that stands for the selected frame's display. */)
3537 (Lisp_Object terminal)
3538 {
3539 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3540
3541 if (dpyinfo->n_planes <= 1)
3542 return Qnil;
3543
3544 switch (dpyinfo->visual->class)
3545 {
3546 case StaticColor:
3547 case PseudoColor:
3548 case TrueColor:
3549 case DirectColor:
3550 case StaticGray:
3551 case GrayScale:
3552 return Qt;
3553
3554 default:
3555 return Qnil;
3556 }
3557 }
3558
3559 DEFUN ("x-display-pixel-width", Fx_display_pixel_width, Sx_display_pixel_width,
3560 0, 1, 0,
3561 doc: /* Return the width in pixels of the X display TERMINAL.
3562 The optional argument TERMINAL specifies which display to ask about.
3563 TERMINAL should be a terminal object, a frame or a display name (a string).
3564 If omitted or nil, that stands for the selected frame's display. */)
3565 (Lisp_Object terminal)
3566 {
3567 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3568
3569 return make_number (x_display_pixel_width (dpyinfo));
3570 }
3571
3572 DEFUN ("x-display-pixel-height", Fx_display_pixel_height,
3573 Sx_display_pixel_height, 0, 1, 0,
3574 doc: /* Return the height in pixels of the X display TERMINAL.
3575 The optional argument TERMINAL specifies which display to ask about.
3576 TERMINAL should be a terminal object, a frame or a display name (a string).
3577 If omitted or nil, that stands for the selected frame's display. */)
3578 (Lisp_Object terminal)
3579 {
3580 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3581
3582 return make_number (x_display_pixel_height (dpyinfo));
3583 }
3584
3585 DEFUN ("x-display-planes", Fx_display_planes, Sx_display_planes,
3586 0, 1, 0,
3587 doc: /* Return the number of bitplanes of the X display TERMINAL.
3588 The optional argument TERMINAL specifies which display to ask about.
3589 TERMINAL should be a terminal object, a frame or a display name (a string).
3590 If omitted or nil, that stands for the selected frame's display. */)
3591 (Lisp_Object terminal)
3592 {
3593 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3594
3595 return make_number (dpyinfo->n_planes);
3596 }
3597
3598 DEFUN ("x-display-color-cells", Fx_display_color_cells, Sx_display_color_cells,
3599 0, 1, 0,
3600 doc: /* Return the number of color cells of the X display TERMINAL.
3601 The optional argument TERMINAL specifies which display to ask about.
3602 TERMINAL should be a terminal object, a frame or a display name (a string).
3603 If omitted or nil, that stands for the selected frame's display. */)
3604 (Lisp_Object terminal)
3605 {
3606 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3607
3608 int nr_planes = DisplayPlanes (dpyinfo->display,
3609 XScreenNumberOfScreen (dpyinfo->screen));
3610
3611 /* Truncate nr_planes to 24 to avoid integer overflow.
3612 Some displays says 32, but only 24 bits are actually significant.
3613 There are only very few and rare video cards that have more than
3614 24 significant bits. Also 24 bits is more than 16 million colors,
3615 it "should be enough for everyone". */
3616 if (nr_planes > 24) nr_planes = 24;
3617
3618 return make_number (1 << nr_planes);
3619 }
3620
3621 DEFUN ("x-server-max-request-size", Fx_server_max_request_size,
3622 Sx_server_max_request_size,
3623 0, 1, 0,
3624 doc: /* Return the maximum request size of the X server of display TERMINAL.
3625 The optional argument TERMINAL specifies which display to ask about.
3626 TERMINAL should be a terminal object, a frame or a display name (a string).
3627 If omitted or nil, that stands for the selected frame's display. */)
3628 (Lisp_Object terminal)
3629 {
3630 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3631
3632 return make_number (MAXREQUEST (dpyinfo->display));
3633 }
3634
3635 DEFUN ("x-server-vendor", Fx_server_vendor, Sx_server_vendor, 0, 1, 0,
3636 doc: /* Return the "vendor ID" string of the X server of display TERMINAL.
3637 \(Labeling every distributor as a "vendor" embodies the false assumption
3638 that operating systems cannot be developed and distributed noncommercially.)
3639 The optional argument TERMINAL specifies which display to ask about.
3640 TERMINAL should be a terminal object, a frame or a display name (a string).
3641 If omitted or nil, that stands for the selected frame's display. */)
3642 (Lisp_Object terminal)
3643 {
3644 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3645 const char *vendor = ServerVendor (dpyinfo->display);
3646
3647 if (! vendor) vendor = "";
3648 return build_string (vendor);
3649 }
3650
3651 DEFUN ("x-server-version", Fx_server_version, Sx_server_version, 0, 1, 0,
3652 doc: /* Return the version numbers of the X server of display TERMINAL.
3653 The value is a list of three integers: the major and minor
3654 version numbers of the X Protocol in use, and the distributor-specific release
3655 number. See also the function `x-server-vendor'.
3656
3657 The optional argument TERMINAL specifies which display to ask about.
3658 TERMINAL should be a terminal object, a frame or a display name (a string).
3659 If omitted or nil, that stands for the selected frame's display. */)
3660 (Lisp_Object terminal)
3661 {
3662 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3663 Display *dpy = dpyinfo->display;
3664
3665 return list3i (ProtocolVersion (dpy), ProtocolRevision (dpy),
3666 VendorRelease (dpy));
3667 }
3668
3669 DEFUN ("x-display-screens", Fx_display_screens, Sx_display_screens, 0, 1, 0,
3670 doc: /* Return the number of screens on the X server of display TERMINAL.
3671 The optional argument TERMINAL specifies which display to ask about.
3672 TERMINAL should be a terminal object, a frame or a display name (a string).
3673 If omitted or nil, that stands for the selected frame's display. */)
3674 (Lisp_Object terminal)
3675 {
3676 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3677
3678 return make_number (ScreenCount (dpyinfo->display));
3679 }
3680
3681 DEFUN ("x-display-mm-height", Fx_display_mm_height, Sx_display_mm_height, 0, 1, 0,
3682 doc: /* Return the height in millimeters of the X display TERMINAL.
3683 The optional argument TERMINAL specifies which display to ask about.
3684 TERMINAL should be a terminal object, a frame or a display name (a string).
3685 If omitted or nil, that stands for the selected frame's display. */)
3686 (Lisp_Object terminal)
3687 {
3688 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3689
3690 return make_number (HeightMMOfScreen (dpyinfo->screen));
3691 }
3692
3693 DEFUN ("x-display-mm-width", Fx_display_mm_width, Sx_display_mm_width, 0, 1, 0,
3694 doc: /* Return the width in millimeters of the X display TERMINAL.
3695 The optional argument TERMINAL specifies which display to ask about.
3696 TERMINAL should be a terminal object, a frame or a display name (a string).
3697 If omitted or nil, that stands for the selected frame's display. */)
3698 (Lisp_Object terminal)
3699 {
3700 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3701
3702 return make_number (WidthMMOfScreen (dpyinfo->screen));
3703 }
3704
3705 DEFUN ("x-display-backing-store", Fx_display_backing_store,
3706 Sx_display_backing_store, 0, 1, 0,
3707 doc: /* Return an indication of whether X display TERMINAL does backing store.
3708 The value may be `always', `when-mapped', or `not-useful'.
3709 The optional argument TERMINAL specifies which display to ask about.
3710 TERMINAL should be a terminal object, a frame or a display name (a string).
3711 If omitted or nil, that stands for the selected frame's display. */)
3712 (Lisp_Object terminal)
3713 {
3714 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3715 Lisp_Object result;
3716
3717 switch (DoesBackingStore (dpyinfo->screen))
3718 {
3719 case Always:
3720 result = intern ("always");
3721 break;
3722
3723 case WhenMapped:
3724 result = intern ("when-mapped");
3725 break;
3726
3727 case NotUseful:
3728 result = intern ("not-useful");
3729 break;
3730
3731 default:
3732 error ("Strange value for BackingStore parameter of screen");
3733 }
3734
3735 return result;
3736 }
3737
3738 DEFUN ("x-display-visual-class", Fx_display_visual_class,
3739 Sx_display_visual_class, 0, 1, 0,
3740 doc: /* Return the visual class of the X display TERMINAL.
3741 The value is one of the symbols `static-gray', `gray-scale',
3742 `static-color', `pseudo-color', `true-color', or `direct-color'.
3743
3744 The optional argument TERMINAL specifies which display to ask about.
3745 TERMINAL should a terminal object, a frame or a display name (a string).
3746 If omitted or nil, that stands for the selected frame's display. */)
3747 (Lisp_Object terminal)
3748 {
3749 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3750 Lisp_Object result;
3751
3752 switch (dpyinfo->visual->class)
3753 {
3754 case StaticGray:
3755 result = intern ("static-gray");
3756 break;
3757 case GrayScale:
3758 result = intern ("gray-scale");
3759 break;
3760 case StaticColor:
3761 result = intern ("static-color");
3762 break;
3763 case PseudoColor:
3764 result = intern ("pseudo-color");
3765 break;
3766 case TrueColor:
3767 result = intern ("true-color");
3768 break;
3769 case DirectColor:
3770 result = intern ("direct-color");
3771 break;
3772 default:
3773 error ("Display has an unknown visual class");
3774 }
3775
3776 return result;
3777 }
3778
3779 DEFUN ("x-display-save-under", Fx_display_save_under,
3780 Sx_display_save_under, 0, 1, 0,
3781 doc: /* Return t if the X display TERMINAL supports the save-under feature.
3782 The optional argument TERMINAL specifies which display to ask about.
3783 TERMINAL should be a terminal object, a frame or a display name (a string).
3784 If omitted or nil, that stands for the selected frame's display. */)
3785 (Lisp_Object terminal)
3786 {
3787 struct x_display_info *dpyinfo = check_x_display_info (terminal);
3788
3789 if (DoesSaveUnders (dpyinfo->screen) == True)
3790 return Qt;
3791 else
3792 return Qnil;
3793 }
3794 \f
3795 int
3796 x_pixel_width (register struct frame *f)
3797 {
3798 return FRAME_PIXEL_WIDTH (f);
3799 }
3800
3801 int
3802 x_pixel_height (register struct frame *f)
3803 {
3804 return FRAME_PIXEL_HEIGHT (f);
3805 }
3806
3807 /************************************************************************
3808 X Displays
3809 ************************************************************************/
3810
3811 \f
3812 /* Mapping visual names to visuals. */
3813
3814 static struct visual_class
3815 {
3816 const char *name;
3817 int class;
3818 }
3819 visual_classes[] =
3820 {
3821 {"StaticGray", StaticGray},
3822 {"GrayScale", GrayScale},
3823 {"StaticColor", StaticColor},
3824 {"PseudoColor", PseudoColor},
3825 {"TrueColor", TrueColor},
3826 {"DirectColor", DirectColor},
3827 {NULL, 0}
3828 };
3829
3830
3831 #ifndef HAVE_XSCREENNUMBEROFSCREEN
3832
3833 /* Value is the screen number of screen SCR. This is a substitute for
3834 the X function with the same name when that doesn't exist. */
3835
3836 int
3837 XScreenNumberOfScreen (scr)
3838 register Screen *scr;
3839 {
3840 Display *dpy = scr->display;
3841 int i;
3842
3843 for (i = 0; i < dpy->nscreens; ++i)
3844 if (scr == dpy->screens + i)
3845 break;
3846
3847 return i;
3848 }
3849
3850 #endif /* not HAVE_XSCREENNUMBEROFSCREEN */
3851
3852
3853 /* Select the visual that should be used on display DPYINFO. Set
3854 members of DPYINFO appropriately. Called from x_term_init. */
3855
3856 void
3857 select_visual (struct x_display_info *dpyinfo)
3858 {
3859 Display *dpy = dpyinfo->display;
3860 Screen *screen = dpyinfo->screen;
3861 Lisp_Object value;
3862
3863 /* See if a visual is specified. */
3864 value = display_x_get_resource (dpyinfo,
3865 build_string ("visualClass"),
3866 build_string ("VisualClass"),
3867 Qnil, Qnil);
3868 if (STRINGP (value))
3869 {
3870 /* VALUE should be of the form CLASS-DEPTH, where CLASS is one
3871 of `PseudoColor', `TrueColor' etc. and DEPTH is the color
3872 depth, a decimal number. NAME is compared with case ignored. */
3873 char *s = alloca (SBYTES (value) + 1);
3874 char *dash;
3875 int i, class = -1;
3876 XVisualInfo vinfo;
3877
3878 strcpy (s, SSDATA (value));
3879 dash = strchr (s, '-');
3880 if (dash)
3881 {
3882 dpyinfo->n_planes = atoi (dash + 1);
3883 *dash = '\0';
3884 }
3885 else
3886 /* We won't find a matching visual with depth 0, so that
3887 an error will be printed below. */
3888 dpyinfo->n_planes = 0;
3889
3890 /* Determine the visual class. */
3891 for (i = 0; visual_classes[i].name; ++i)
3892 if (xstrcasecmp (s, visual_classes[i].name) == 0)
3893 {
3894 class = visual_classes[i].class;
3895 break;
3896 }
3897
3898 /* Look up a matching visual for the specified class. */
3899 if (class == -1
3900 || !XMatchVisualInfo (dpy, XScreenNumberOfScreen (screen),
3901 dpyinfo->n_planes, class, &vinfo))
3902 fatal ("Invalid visual specification `%s'", SDATA (value));
3903
3904 dpyinfo->visual = vinfo.visual;
3905 }
3906 else
3907 {
3908 int n_visuals;
3909 XVisualInfo *vinfo, vinfo_template;
3910
3911 dpyinfo->visual = DefaultVisualOfScreen (screen);
3912
3913 vinfo_template.visualid = XVisualIDFromVisual (dpyinfo->visual);
3914 vinfo_template.screen = XScreenNumberOfScreen (screen);
3915 vinfo = XGetVisualInfo (dpy, VisualIDMask | VisualScreenMask,
3916 &vinfo_template, &n_visuals);
3917 if (n_visuals <= 0)
3918 fatal ("Can't get proper X visual info");
3919
3920 dpyinfo->n_planes = vinfo->depth;
3921 XFree (vinfo);
3922 }
3923 }
3924
3925
3926 /* Return the X display structure for the display named NAME.
3927 Open a new connection if necessary. */
3928
3929 static struct x_display_info *
3930 x_display_info_for_name (Lisp_Object name)
3931 {
3932 Lisp_Object names;
3933 struct x_display_info *dpyinfo;
3934
3935 CHECK_STRING (name);
3936
3937 #if 0
3938 if (! EQ (Vinitial_window_system, intern ("x")))
3939 error ("Not using X Windows"); /* That doesn't stop us anymore. */
3940 #endif
3941
3942 for (dpyinfo = x_display_list, names = x_display_name_list;
3943 dpyinfo;
3944 dpyinfo = dpyinfo->next, names = XCDR (names))
3945 {
3946 Lisp_Object tem;
3947 tem = Fstring_equal (XCAR (XCAR (names)), name);
3948 if (!NILP (tem))
3949 return dpyinfo;
3950 }
3951
3952 /* Use this general default value to start with. */
3953 Vx_resource_name = Vinvocation_name;
3954
3955 validate_x_resource_name ();
3956
3957 dpyinfo = x_term_init (name, (char *)0,
3958 SSDATA (Vx_resource_name));
3959
3960 if (dpyinfo == 0)
3961 error ("Cannot connect to X server %s", SDATA (name));
3962
3963 XSETFASTINT (Vwindow_system_version, 11);
3964
3965 return dpyinfo;
3966 }
3967
3968
3969 DEFUN ("x-open-connection", Fx_open_connection, Sx_open_connection,
3970 1, 3, 0,
3971 doc: /* Open a connection to a display server.
3972 DISPLAY is the name of the display to connect to.
3973 Optional second arg XRM-STRING is a string of resources in xrdb format.
3974 If the optional third arg MUST-SUCCEED is non-nil,
3975 terminate Emacs if we can't open the connection.
3976 \(In the Nextstep version, the last two arguments are currently ignored.) */)
3977 (Lisp_Object display, Lisp_Object xrm_string, Lisp_Object must_succeed)
3978 {
3979 char *xrm_option;
3980 struct x_display_info *dpyinfo;
3981
3982 CHECK_STRING (display);
3983 if (! NILP (xrm_string))
3984 CHECK_STRING (xrm_string);
3985
3986 #if 0
3987 if (! EQ (Vinitial_window_system, intern ("x")))
3988 error ("Not using X Windows"); /* That doesn't stop us anymore. */
3989 #endif
3990
3991 if (! NILP (xrm_string))
3992 xrm_option = SSDATA (xrm_string);
3993 else
3994 xrm_option = (char *) 0;
3995
3996 validate_x_resource_name ();
3997
3998 /* This is what opens the connection and sets x_current_display.
3999 This also initializes many symbols, such as those used for input. */
4000 dpyinfo = x_term_init (display, xrm_option,
4001 SSDATA (Vx_resource_name));
4002
4003 if (dpyinfo == 0)
4004 {
4005 if (!NILP (must_succeed))
4006 fatal ("Cannot connect to X server %s.\n\
4007 Check the DISPLAY environment variable or use `-d'.\n\
4008 Also use the `xauth' program to verify that you have the proper\n\
4009 authorization information needed to connect the X server.\n\
4010 An insecure way to solve the problem may be to use `xhost'.\n",
4011 SDATA (display));
4012 else
4013 error ("Cannot connect to X server %s", SDATA (display));
4014 }
4015
4016 XSETFASTINT (Vwindow_system_version, 11);
4017 return Qnil;
4018 }
4019
4020 DEFUN ("x-close-connection", Fx_close_connection,
4021 Sx_close_connection, 1, 1, 0,
4022 doc: /* Close the connection to TERMINAL's X server.
4023 For TERMINAL, specify a terminal object, a frame or a display name (a
4024 string). If TERMINAL is nil, that stands for the selected frame's
4025 terminal. */)
4026 (Lisp_Object terminal)
4027 {
4028 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4029
4030 if (dpyinfo->reference_count > 0)
4031 error ("Display still has frames on it");
4032
4033 x_delete_terminal (dpyinfo->terminal);
4034
4035 return Qnil;
4036 }
4037
4038 DEFUN ("x-display-list", Fx_display_list, Sx_display_list, 0, 0, 0,
4039 doc: /* Return the list of display names that Emacs has connections to. */)
4040 (void)
4041 {
4042 Lisp_Object tail, result;
4043
4044 result = Qnil;
4045 for (tail = x_display_name_list; CONSP (tail); tail = XCDR (tail))
4046 result = Fcons (XCAR (XCAR (tail)), result);
4047
4048 return result;
4049 }
4050
4051 DEFUN ("x-synchronize", Fx_synchronize, Sx_synchronize, 1, 2, 0,
4052 doc: /* If ON is non-nil, report X errors as soon as the erring request is made.
4053 This function only has an effect on X Windows. With MS Windows, it is
4054 defined but does nothing.
4055
4056 If ON is nil, allow buffering of requests.
4057 Turning on synchronization prohibits the Xlib routines from buffering
4058 requests and seriously degrades performance, but makes debugging much
4059 easier.
4060 The optional second argument TERMINAL specifies which display to act on.
4061 TERMINAL should be a terminal object, a frame or a display name (a string).
4062 If TERMINAL is omitted or nil, that stands for the selected frame's display. */)
4063 (Lisp_Object on, Lisp_Object terminal)
4064 {
4065 struct x_display_info *dpyinfo = check_x_display_info (terminal);
4066
4067 XSynchronize (dpyinfo->display, !EQ (on, Qnil));
4068
4069 return Qnil;
4070 }
4071
4072 /* Wait for responses to all X commands issued so far for frame F. */
4073
4074 void
4075 x_sync (FRAME_PTR f)
4076 {
4077 block_input ();
4078 XSync (FRAME_X_DISPLAY (f), False);
4079 unblock_input ();
4080 }
4081
4082 \f
4083 /***********************************************************************
4084 Window properties
4085 ***********************************************************************/
4086
4087 DEFUN ("x-change-window-property", Fx_change_window_property,
4088 Sx_change_window_property, 2, 6, 0,
4089 doc: /* Change window property PROP to VALUE on the X window of FRAME.
4090 PROP must be a string. VALUE may be a string or a list of conses,
4091 numbers and/or strings. If an element in the list is a string, it is
4092 converted to an atom and the value of the atom is used. If an element
4093 is a cons, it is converted to a 32 bit number where the car is the 16
4094 top bits and the cdr is the lower 16 bits.
4095
4096 FRAME nil or omitted means use the selected frame.
4097 If TYPE is given and non-nil, it is the name of the type of VALUE.
4098 If TYPE is not given or nil, the type is STRING.
4099 FORMAT gives the size in bits of each element if VALUE is a list.
4100 It must be one of 8, 16 or 32.
4101 If VALUE is a string or FORMAT is nil or not given, FORMAT defaults to 8.
4102 If OUTER-P is non-nil, the property is changed for the outer X window of
4103 FRAME. Default is to change on the edit X window. */)
4104 (Lisp_Object prop, Lisp_Object value, Lisp_Object frame,
4105 Lisp_Object type, Lisp_Object format, Lisp_Object outer_p)
4106 {
4107 struct frame *f = decode_window_system_frame (frame);
4108 Atom prop_atom;
4109 Atom target_type = XA_STRING;
4110 int element_format = 8;
4111 unsigned char *data;
4112 int nelements;
4113 Window w;
4114
4115 CHECK_STRING (prop);
4116
4117 if (! NILP (format))
4118 {
4119 CHECK_NUMBER (format);
4120
4121 if (XINT (format) != 8 && XINT (format) != 16
4122 && XINT (format) != 32)
4123 error ("FORMAT must be one of 8, 16 or 32");
4124 element_format = XINT (format);
4125 }
4126
4127 if (CONSP (value))
4128 {
4129 ptrdiff_t elsize;
4130
4131 nelements = x_check_property_data (value);
4132 if (nelements == -1)
4133 error ("Bad data in VALUE, must be number, string or cons");
4134
4135 /* The man page for XChangeProperty:
4136 "If the specified format is 32, the property data must be a
4137 long array."
4138 This applies even if long is more than 32 bits. The X library
4139 converts to 32 bits before sending to the X server. */
4140 elsize = element_format == 32 ? sizeof (long) : element_format >> 3;
4141 data = xnmalloc (nelements, elsize);
4142
4143 x_fill_property_data (FRAME_X_DISPLAY (f), value, data, element_format);
4144 }
4145 else
4146 {
4147 CHECK_STRING (value);
4148 data = SDATA (value);
4149 if (INT_MAX < SBYTES (value))
4150 error ("VALUE too long");
4151 nelements = SBYTES (value);
4152 }
4153
4154 block_input ();
4155 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4156 if (! NILP (type))
4157 {
4158 CHECK_STRING (type);
4159 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4160 }
4161
4162 if (! NILP (outer_p)) w = FRAME_OUTER_WINDOW (f);
4163 else w = FRAME_X_WINDOW (f);
4164
4165 XChangeProperty (FRAME_X_DISPLAY (f), w,
4166 prop_atom, target_type, element_format, PropModeReplace,
4167 data, nelements);
4168
4169 if (CONSP (value)) xfree (data);
4170
4171 /* Make sure the property is set when we return. */
4172 XFlush (FRAME_X_DISPLAY (f));
4173 unblock_input ();
4174
4175 return value;
4176 }
4177
4178
4179 DEFUN ("x-delete-window-property", Fx_delete_window_property,
4180 Sx_delete_window_property, 1, 2, 0,
4181 doc: /* Remove window property PROP from X window of FRAME.
4182 FRAME nil or omitted means use the selected frame. Value is PROP. */)
4183 (Lisp_Object prop, Lisp_Object frame)
4184 {
4185 struct frame *f = decode_window_system_frame (frame);
4186 Atom prop_atom;
4187
4188 CHECK_STRING (prop);
4189 block_input ();
4190 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4191 XDeleteProperty (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), prop_atom);
4192
4193 /* Make sure the property is removed when we return. */
4194 XFlush (FRAME_X_DISPLAY (f));
4195 unblock_input ();
4196
4197 return prop;
4198 }
4199
4200
4201 DEFUN ("x-window-property", Fx_window_property, Sx_window_property,
4202 1, 6, 0,
4203 doc: /* Value is the value of window property PROP on FRAME.
4204 If FRAME is nil or omitted, use the selected frame.
4205
4206 On X Windows, the following optional arguments are also accepted:
4207 If TYPE is nil or omitted, get the property as a string.
4208 Otherwise TYPE is the name of the atom that denotes the type expected.
4209 If SOURCE is non-nil, get the property on that window instead of from
4210 FRAME. The number 0 denotes the root window.
4211 If DELETE-P is non-nil, delete the property after retrieving it.
4212 If VECTOR-RET-P is non-nil, don't return a string but a vector of values.
4213
4214 On MS Windows, this function accepts but ignores those optional arguments.
4215
4216 Value is nil if FRAME hasn't a property with name PROP or if PROP has
4217 no value of TYPE (always string in the MS Windows case). */)
4218 (Lisp_Object prop, Lisp_Object frame, Lisp_Object type,
4219 Lisp_Object source, Lisp_Object delete_p, Lisp_Object vector_ret_p)
4220 {
4221 struct frame *f = decode_window_system_frame (frame);
4222 Atom prop_atom;
4223 int rc;
4224 Lisp_Object prop_value = Qnil;
4225 unsigned char *tmp_data = NULL;
4226 Atom actual_type;
4227 Atom target_type = XA_STRING;
4228 int actual_format;
4229 unsigned long actual_size, bytes_remaining;
4230 Window target_window = FRAME_X_WINDOW (f);
4231 struct gcpro gcpro1;
4232
4233 GCPRO1 (prop_value);
4234 CHECK_STRING (prop);
4235
4236 if (! NILP (source))
4237 {
4238 CONS_TO_INTEGER (source, Window, target_window);
4239 if (! target_window)
4240 target_window = FRAME_X_DISPLAY_INFO (f)->root_window;
4241 }
4242
4243 block_input ();
4244 if (STRINGP (type))
4245 {
4246 if (strcmp ("AnyPropertyType", SSDATA (type)) == 0)
4247 target_type = AnyPropertyType;
4248 else
4249 target_type = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (type), False);
4250 }
4251
4252 prop_atom = XInternAtom (FRAME_X_DISPLAY (f), SSDATA (prop), False);
4253 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4254 prop_atom, 0, 0, False, target_type,
4255 &actual_type, &actual_format, &actual_size,
4256 &bytes_remaining, &tmp_data);
4257 if (rc == Success)
4258 {
4259 int size = bytes_remaining;
4260
4261 XFree (tmp_data);
4262 tmp_data = NULL;
4263
4264 rc = XGetWindowProperty (FRAME_X_DISPLAY (f), target_window,
4265 prop_atom, 0, bytes_remaining,
4266 ! NILP (delete_p), target_type,
4267 &actual_type, &actual_format,
4268 &actual_size, &bytes_remaining,
4269 &tmp_data);
4270 if (rc == Success && tmp_data)
4271 {
4272 /* The man page for XGetWindowProperty says:
4273 "If the returned format is 32, the returned data is represented
4274 as a long array and should be cast to that type to obtain the
4275 elements."
4276 This applies even if long is more than 32 bits, the X library
4277 converts from 32 bit elements received from the X server to long
4278 and passes the long array to us. Thus, for that case memcpy can not
4279 be used. We convert to a 32 bit type here, because so much code
4280 assume on that.
4281
4282 The bytes and offsets passed to XGetWindowProperty refers to the
4283 property and those are indeed in 32 bit quantities if format is
4284 32. */
4285
4286 if (BITS_PER_LONG > 32 && actual_format == 32)
4287 {
4288 unsigned long i;
4289 int *idata = (int *) tmp_data;
4290 long *ldata = (long *) tmp_data;
4291
4292 for (i = 0; i < actual_size; ++i)
4293 idata[i] = (int) ldata[i];
4294 }
4295
4296 if (NILP (vector_ret_p))
4297 prop_value = make_string ((char *) tmp_data, size);
4298 else
4299 prop_value = x_property_data_to_lisp (f,
4300 tmp_data,
4301 actual_type,
4302 actual_format,
4303 actual_size);
4304 }
4305
4306 if (tmp_data) XFree (tmp_data);
4307 }
4308
4309 unblock_input ();
4310 UNGCPRO;
4311 return prop_value;
4312 }
4313
4314
4315 \f
4316 /***********************************************************************
4317 Busy cursor
4318 ***********************************************************************/
4319
4320 /* Timer function of hourglass_atimer. TIMER is equal to
4321 hourglass_atimer.
4322
4323 Display an hourglass pointer on all frames by mapping the frames'
4324 hourglass_window. Set the hourglass_p flag in the frames'
4325 output_data.x structure to indicate that an hourglass cursor is
4326 shown on the frames. */
4327
4328 void
4329 show_hourglass (struct atimer *timer)
4330 {
4331 /* The timer implementation will cancel this timer automatically
4332 after this function has run. Set hourglass_atimer to null
4333 so that we know the timer doesn't have to be canceled. */
4334 hourglass_atimer = NULL;
4335
4336 if (!hourglass_shown_p)
4337 {
4338 Lisp_Object rest, frame;
4339
4340 block_input ();
4341
4342 FOR_EACH_FRAME (rest, frame)
4343 {
4344 struct frame *f = XFRAME (frame);
4345
4346 if (FRAME_LIVE_P (f) && FRAME_X_P (f) && FRAME_X_DISPLAY (f))
4347 {
4348 Display *dpy = FRAME_X_DISPLAY (f);
4349
4350 #ifdef USE_X_TOOLKIT
4351 if (f->output_data.x->widget)
4352 #else
4353 if (FRAME_OUTER_WINDOW (f))
4354 #endif
4355 {
4356 f->output_data.x->hourglass_p = 1;
4357
4358 if (!f->output_data.x->hourglass_window)
4359 {
4360 unsigned long mask = CWCursor;
4361 XSetWindowAttributes attrs;
4362 #ifdef USE_GTK
4363 Window parent = FRAME_X_WINDOW (f);
4364 #else
4365 Window parent = FRAME_OUTER_WINDOW (f);
4366 #endif
4367 attrs.cursor = f->output_data.x->hourglass_cursor;
4368
4369 f->output_data.x->hourglass_window
4370 = XCreateWindow (dpy, parent,
4371 0, 0, 32000, 32000, 0, 0,
4372 InputOnly,
4373 CopyFromParent,
4374 mask, &attrs);
4375 }
4376
4377 XMapRaised (dpy, f->output_data.x->hourglass_window);
4378 XFlush (dpy);
4379 }
4380 }
4381 }
4382
4383 hourglass_shown_p = 1;
4384 unblock_input ();
4385 }
4386 }
4387
4388
4389 /* Hide the hourglass pointer on all frames, if it is currently
4390 shown. */
4391
4392 void
4393 hide_hourglass (void)
4394 {
4395 if (hourglass_shown_p)
4396 {
4397 Lisp_Object rest, frame;
4398
4399 block_input ();
4400 FOR_EACH_FRAME (rest, frame)
4401 {
4402 struct frame *f = XFRAME (frame);
4403
4404 if (FRAME_X_P (f)
4405 /* Watch out for newly created frames. */
4406 && f->output_data.x->hourglass_window)
4407 {
4408 XUnmapWindow (FRAME_X_DISPLAY (f),
4409 f->output_data.x->hourglass_window);
4410 /* Sync here because XTread_socket looks at the
4411 hourglass_p flag that is reset to zero below. */
4412 XSync (FRAME_X_DISPLAY (f), False);
4413 f->output_data.x->hourglass_p = 0;
4414 }
4415 }
4416
4417 hourglass_shown_p = 0;
4418 unblock_input ();
4419 }
4420 }
4421
4422
4423 \f
4424 /***********************************************************************
4425 Tool tips
4426 ***********************************************************************/
4427
4428 static Lisp_Object x_create_tip_frame (struct x_display_info *,
4429 Lisp_Object, Lisp_Object);
4430 static void compute_tip_xy (struct frame *, Lisp_Object, Lisp_Object,
4431 Lisp_Object, int, int, int *, int *);
4432
4433 /* The frame of a currently visible tooltip. */
4434
4435 Lisp_Object tip_frame;
4436
4437 /* If non-nil, a timer started that hides the last tooltip when it
4438 fires. */
4439
4440 static Lisp_Object tip_timer;
4441 Window tip_window;
4442
4443 /* If non-nil, a vector of 3 elements containing the last args
4444 with which x-show-tip was called. See there. */
4445
4446 static Lisp_Object last_show_tip_args;
4447
4448
4449 static Lisp_Object
4450 unwind_create_tip_frame (Lisp_Object frame)
4451 {
4452 Lisp_Object deleted;
4453
4454 deleted = unwind_create_frame (frame);
4455 if (EQ (deleted, Qt))
4456 {
4457 tip_window = None;
4458 tip_frame = Qnil;
4459 }
4460
4461 return deleted;
4462 }
4463
4464
4465 /* Create a frame for a tooltip on the display described by DPYINFO.
4466 PARMS is a list of frame parameters. TEXT is the string to
4467 display in the tip frame. Value is the frame.
4468
4469 Note that functions called here, esp. x_default_parameter can
4470 signal errors, for instance when a specified color name is
4471 undefined. We have to make sure that we're in a consistent state
4472 when this happens. */
4473
4474 static Lisp_Object
4475 x_create_tip_frame (struct x_display_info *dpyinfo,
4476 Lisp_Object parms,
4477 Lisp_Object text)
4478 {
4479 struct frame *f;
4480 Lisp_Object frame;
4481 Lisp_Object name;
4482 int width, height;
4483 ptrdiff_t count = SPECPDL_INDEX ();
4484 struct gcpro gcpro1, gcpro2, gcpro3;
4485 int face_change_count_before = face_change_count;
4486 Lisp_Object buffer;
4487 struct buffer *old_buffer;
4488
4489 if (!dpyinfo->terminal->name)
4490 error ("Terminal is not live, can't create new frames on it");
4491
4492 parms = Fcopy_alist (parms);
4493
4494 /* Get the name of the frame to use for resource lookup. */
4495 name = x_get_arg (dpyinfo, parms, Qname, "name", "Name", RES_TYPE_STRING);
4496 if (!STRINGP (name)
4497 && !EQ (name, Qunbound)
4498 && !NILP (name))
4499 error ("Invalid frame name--not a string or nil");
4500
4501 frame = Qnil;
4502 GCPRO3 (parms, name, frame);
4503 f = make_frame (1);
4504 XSETFRAME (frame, f);
4505
4506 buffer = Fget_buffer_create (build_string (" *tip*"));
4507 /* Use set_window_buffer instead of Fset_window_buffer (see
4508 discussion of bug#11984, bug#12025, bug#12026). */
4509 set_window_buffer (FRAME_ROOT_WINDOW (f), buffer, 0, 0);
4510 old_buffer = current_buffer;
4511 set_buffer_internal_1 (XBUFFER (buffer));
4512 bset_truncate_lines (current_buffer, Qnil);
4513 specbind (Qinhibit_read_only, Qt);
4514 specbind (Qinhibit_modification_hooks, Qt);
4515 Ferase_buffer ();
4516 Finsert (1, &text);
4517 set_buffer_internal_1 (old_buffer);
4518
4519 record_unwind_protect (unwind_create_tip_frame, frame);
4520
4521 f->terminal = dpyinfo->terminal;
4522
4523 /* By setting the output method, we're essentially saying that
4524 the frame is live, as per FRAME_LIVE_P. If we get a signal
4525 from this point on, x_destroy_window might screw up reference
4526 counts etc. */
4527 f->output_method = output_x_window;
4528 f->output_data.x = xzalloc (sizeof *f->output_data.x);
4529 f->output_data.x->icon_bitmap = -1;
4530 FRAME_FONTSET (f) = -1;
4531 f->output_data.x->scroll_bar_foreground_pixel = -1;
4532 f->output_data.x->scroll_bar_background_pixel = -1;
4533 #ifdef USE_TOOLKIT_SCROLL_BARS
4534 f->output_data.x->scroll_bar_top_shadow_pixel = -1;
4535 f->output_data.x->scroll_bar_bottom_shadow_pixel = -1;
4536 #endif /* USE_TOOLKIT_SCROLL_BARS */
4537 fset_icon_name (f, Qnil);
4538 FRAME_X_DISPLAY_INFO (f) = dpyinfo;
4539 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4540 f->output_data.x->explicit_parent = 0;
4541
4542 /* These colors will be set anyway later, but it's important
4543 to get the color reference counts right, so initialize them! */
4544 {
4545 Lisp_Object black;
4546 struct gcpro gcpro1;
4547
4548 /* Function x_decode_color can signal an error. Make
4549 sure to initialize color slots so that we won't try
4550 to free colors we haven't allocated. */
4551 FRAME_FOREGROUND_PIXEL (f) = -1;
4552 FRAME_BACKGROUND_PIXEL (f) = -1;
4553 f->output_data.x->cursor_pixel = -1;
4554 f->output_data.x->cursor_foreground_pixel = -1;
4555 f->output_data.x->border_pixel = -1;
4556 f->output_data.x->mouse_pixel = -1;
4557
4558 black = build_string ("black");
4559 GCPRO1 (black);
4560 FRAME_FOREGROUND_PIXEL (f)
4561 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4562 FRAME_BACKGROUND_PIXEL (f)
4563 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4564 f->output_data.x->cursor_pixel
4565 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4566 f->output_data.x->cursor_foreground_pixel
4567 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4568 f->output_data.x->border_pixel
4569 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4570 f->output_data.x->mouse_pixel
4571 = x_decode_color (f, black, BLACK_PIX_DEFAULT (f));
4572 UNGCPRO;
4573 }
4574
4575 /* Set the name; the functions to which we pass f expect the name to
4576 be set. */
4577 if (EQ (name, Qunbound) || NILP (name))
4578 {
4579 fset_name (f, build_string (dpyinfo->x_id_name));
4580 f->explicit_name = 0;
4581 }
4582 else
4583 {
4584 fset_name (f, name);
4585 f->explicit_name = 1;
4586 /* use the frame's title when getting resources for this frame. */
4587 specbind (Qx_resource_name, name);
4588 }
4589
4590 register_font_driver (&xfont_driver, f);
4591 #ifdef HAVE_FREETYPE
4592 #ifdef HAVE_XFT
4593 register_font_driver (&xftfont_driver, f);
4594 #else /* not HAVE_XFT */
4595 register_font_driver (&ftxfont_driver, f);
4596 #endif /* not HAVE_XFT */
4597 #endif /* HAVE_FREETYPE */
4598
4599 x_default_parameter (f, parms, Qfont_backend, Qnil,
4600 "fontBackend", "FontBackend", RES_TYPE_STRING);
4601
4602 /* Extract the window parameters from the supplied values that are
4603 needed to determine window geometry. */
4604 x_default_font_parameter (f, parms);
4605
4606 x_default_parameter (f, parms, Qborder_width, make_number (0),
4607 "borderWidth", "BorderWidth", RES_TYPE_NUMBER);
4608
4609 /* This defaults to 2 in order to match xterm. We recognize either
4610 internalBorderWidth or internalBorder (which is what xterm calls
4611 it). */
4612 if (NILP (Fassq (Qinternal_border_width, parms)))
4613 {
4614 Lisp_Object value;
4615
4616 value = x_get_arg (dpyinfo, parms, Qinternal_border_width,
4617 "internalBorder", "internalBorder", RES_TYPE_NUMBER);
4618 if (! EQ (value, Qunbound))
4619 parms = Fcons (Fcons (Qinternal_border_width, value),
4620 parms);
4621 }
4622
4623 x_default_parameter (f, parms, Qinternal_border_width, make_number (1),
4624 "internalBorderWidth", "internalBorderWidth",
4625 RES_TYPE_NUMBER);
4626
4627 /* Also do the stuff which must be set before the window exists. */
4628 x_default_parameter (f, parms, Qforeground_color, build_string ("black"),
4629 "foreground", "Foreground", RES_TYPE_STRING);
4630 x_default_parameter (f, parms, Qbackground_color, build_string ("white"),
4631 "background", "Background", RES_TYPE_STRING);
4632 x_default_parameter (f, parms, Qmouse_color, build_string ("black"),
4633 "pointerColor", "Foreground", RES_TYPE_STRING);
4634 x_default_parameter (f, parms, Qcursor_color, build_string ("black"),
4635 "cursorColor", "Foreground", RES_TYPE_STRING);
4636 x_default_parameter (f, parms, Qborder_color, build_string ("black"),
4637 "borderColor", "BorderColor", RES_TYPE_STRING);
4638
4639 #ifdef GLYPH_DEBUG
4640 image_cache_refcount =
4641 FRAME_IMAGE_CACHE (f) ? FRAME_IMAGE_CACHE (f)->refcount : 0;
4642 dpyinfo_refcount = dpyinfo->reference_count;
4643 #endif /* GLYPH_DEBUG */
4644
4645 /* Init faces before x_default_parameter is called for scroll-bar
4646 parameters because that function calls x_set_scroll_bar_width,
4647 which calls change_frame_size, which calls Fset_window_buffer,
4648 which runs hooks, which call Fvertical_motion. At the end, we
4649 end up in init_iterator with a null face cache, which should not
4650 happen. */
4651 init_frame_faces (f);
4652
4653 f->output_data.x->parent_desc = FRAME_X_DISPLAY_INFO (f)->root_window;
4654
4655 x_figure_window_size (f, parms, 0);
4656
4657 {
4658 XSetWindowAttributes attrs;
4659 unsigned long mask;
4660 Atom type = FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type_tooltip;
4661
4662 block_input ();
4663 mask = CWBackPixel | CWOverrideRedirect | CWEventMask;
4664 if (DoesSaveUnders (dpyinfo->screen))
4665 mask |= CWSaveUnder;
4666
4667 /* Window managers look at the override-redirect flag to determine
4668 whether or net to give windows a decoration (Xlib spec, chapter
4669 3.2.8). */
4670 attrs.override_redirect = True;
4671 attrs.save_under = True;
4672 attrs.background_pixel = FRAME_BACKGROUND_PIXEL (f);
4673 /* Arrange for getting MapNotify and UnmapNotify events. */
4674 attrs.event_mask = StructureNotifyMask;
4675 tip_window
4676 = FRAME_X_WINDOW (f)
4677 = XCreateWindow (FRAME_X_DISPLAY (f),
4678 FRAME_X_DISPLAY_INFO (f)->root_window,
4679 /* x, y, width, height */
4680 0, 0, 1, 1,
4681 /* Border. */
4682 f->border_width,
4683 CopyFromParent, InputOutput, CopyFromParent,
4684 mask, &attrs);
4685 XChangeProperty (FRAME_X_DISPLAY (f), tip_window,
4686 FRAME_X_DISPLAY_INFO (f)->Xatom_net_window_type,
4687 XA_ATOM, 32, PropModeReplace,
4688 (unsigned char *)&type, 1);
4689 unblock_input ();
4690 }
4691
4692 x_make_gc (f);
4693
4694 x_default_parameter (f, parms, Qauto_raise, Qnil,
4695 "autoRaise", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4696 x_default_parameter (f, parms, Qauto_lower, Qnil,
4697 "autoLower", "AutoRaiseLower", RES_TYPE_BOOLEAN);
4698 x_default_parameter (f, parms, Qcursor_type, Qbox,
4699 "cursorType", "CursorType", RES_TYPE_SYMBOL);
4700
4701 /* Dimensions, especially FRAME_LINES (f), must be done via change_frame_size.
4702 Change will not be effected unless different from the current
4703 FRAME_LINES (f). */
4704 width = FRAME_COLS (f);
4705 height = FRAME_LINES (f);
4706 SET_FRAME_COLS (f, 0);
4707 FRAME_LINES (f) = 0;
4708 change_frame_size (f, height, width, 1, 0, 0);
4709
4710 /* Add `tooltip' frame parameter's default value. */
4711 if (NILP (Fframe_parameter (frame, Qtooltip)))
4712 Fmodify_frame_parameters (frame, Fcons (Fcons (Qtooltip, Qt), Qnil));
4713
4714 /* FIXME - can this be done in a similar way to normal frames?
4715 http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00641.html */
4716
4717 /* Set the `display-type' frame parameter before setting up faces. */
4718 {
4719 Lisp_Object disptype;
4720
4721 if (FRAME_X_DISPLAY_INFO (f)->n_planes == 1)
4722 disptype = intern ("mono");
4723 else if (FRAME_X_DISPLAY_INFO (f)->visual->class == GrayScale
4724 || FRAME_X_DISPLAY_INFO (f)->visual->class == StaticGray)
4725 disptype = intern ("grayscale");
4726 else
4727 disptype = intern ("color");
4728
4729 if (NILP (Fframe_parameter (frame, Qdisplay_type)))
4730 Fmodify_frame_parameters (frame, Fcons (Fcons (Qdisplay_type, disptype),
4731 Qnil));
4732 }
4733
4734 /* Set up faces after all frame parameters are known. This call
4735 also merges in face attributes specified for new frames.
4736
4737 Frame parameters may be changed if .Xdefaults contains
4738 specifications for the default font. For example, if there is an
4739 `Emacs.default.attributeBackground: pink', the `background-color'
4740 attribute of the frame get's set, which let's the internal border
4741 of the tooltip frame appear in pink. Prevent this. */
4742 {
4743 Lisp_Object bg = Fframe_parameter (frame, Qbackground_color);
4744
4745 /* Set tip_frame here, so that */
4746 tip_frame = frame;
4747 call2 (Qface_set_after_frame_default, frame, Qnil);
4748
4749 if (!EQ (bg, Fframe_parameter (frame, Qbackground_color)))
4750 Fmodify_frame_parameters (frame, Fcons (Fcons (Qbackground_color, bg),
4751 Qnil));
4752 }
4753
4754 f->no_split = 1;
4755
4756 UNGCPRO;
4757
4758 /* Now that the frame will be official, it counts as a reference to
4759 its display and terminal. */
4760 FRAME_X_DISPLAY_INFO (f)->reference_count++;
4761 f->terminal->reference_count++;
4762
4763 /* It is now ok to make the frame official even if we get an error
4764 below. And the frame needs to be on Vframe_list or making it
4765 visible won't work. */
4766 Vframe_list = Fcons (frame, Vframe_list);
4767
4768
4769 /* Setting attributes of faces of the tooltip frame from resources
4770 and similar will increment face_change_count, which leads to the
4771 clearing of all current matrices. Since this isn't necessary
4772 here, avoid it by resetting face_change_count to the value it
4773 had before we created the tip frame. */
4774 face_change_count = face_change_count_before;
4775
4776 /* Discard the unwind_protect. */
4777 return unbind_to (count, frame);
4778 }
4779
4780
4781 /* Compute where to display tip frame F. PARMS is the list of frame
4782 parameters for F. DX and DY are specified offsets from the current
4783 location of the mouse. WIDTH and HEIGHT are the width and height
4784 of the tooltip. Return coordinates relative to the root window of
4785 the display in *ROOT_X, and *ROOT_Y. */
4786
4787 static void
4788 compute_tip_xy (struct frame *f, Lisp_Object parms, Lisp_Object dx, Lisp_Object dy, int width, int height, int *root_x, int *root_y)
4789 {
4790 Lisp_Object left, top;
4791 int win_x, win_y;
4792 Window root, child;
4793 unsigned pmask;
4794
4795 /* User-specified position? */
4796 left = Fcdr (Fassq (Qleft, parms));
4797 top = Fcdr (Fassq (Qtop, parms));
4798
4799 /* Move the tooltip window where the mouse pointer is. Resize and
4800 show it. */
4801 if (!INTEGERP (left) || !INTEGERP (top))
4802 {
4803 block_input ();
4804 XQueryPointer (FRAME_X_DISPLAY (f), FRAME_X_DISPLAY_INFO (f)->root_window,
4805 &root, &child, root_x, root_y, &win_x, &win_y, &pmask);
4806 unblock_input ();
4807 }
4808
4809 if (INTEGERP (top))
4810 *root_y = XINT (top);
4811 else if (*root_y + XINT (dy) <= 0)
4812 *root_y = 0; /* Can happen for negative dy */
4813 else if (*root_y + XINT (dy) + height
4814 <= x_display_pixel_height (FRAME_X_DISPLAY_INFO (f)))
4815 /* It fits below the pointer */
4816 *root_y += XINT (dy);
4817 else if (height + XINT (dy) <= *root_y)
4818 /* It fits above the pointer. */
4819 *root_y -= height + XINT (dy);
4820 else
4821 /* Put it on the top. */
4822 *root_y = 0;
4823
4824 if (INTEGERP (left))
4825 *root_x = XINT (left);
4826 else if (*root_x + XINT (dx) <= 0)
4827 *root_x = 0; /* Can happen for negative dx */
4828 else if (*root_x + XINT (dx) + width
4829 <= x_display_pixel_width (FRAME_X_DISPLAY_INFO (f)))
4830 /* It fits to the right of the pointer. */
4831 *root_x += XINT (dx);
4832 else if (width + XINT (dx) <= *root_x)
4833 /* It fits to the left of the pointer. */
4834 *root_x -= width + XINT (dx);
4835 else
4836 /* Put it left-justified on the screen--it ought to fit that way. */
4837 *root_x = 0;
4838 }
4839
4840
4841 DEFUN ("x-show-tip", Fx_show_tip, Sx_show_tip, 1, 6, 0,
4842 doc: /* Show STRING in a "tooltip" window on frame FRAME.
4843 A tooltip window is a small X window displaying a string.
4844
4845 This is an internal function; Lisp code should call `tooltip-show'.
4846
4847 FRAME nil or omitted means use the selected frame.
4848
4849 PARMS is an optional list of frame parameters which can be used to
4850 change the tooltip's appearance.
4851
4852 Automatically hide the tooltip after TIMEOUT seconds. TIMEOUT nil
4853 means use the default timeout of 5 seconds.
4854
4855 If the list of frame parameters PARMS contains a `left' parameters,
4856 the tooltip is displayed at that x-position. Otherwise it is
4857 displayed at the mouse position, with offset DX added (default is 5 if
4858 DX isn't specified). Likewise for the y-position; if a `top' frame
4859 parameter is specified, it determines the y-position of the tooltip
4860 window, otherwise it is displayed at the mouse position, with offset
4861 DY added (default is -10).
4862
4863 A tooltip's maximum size is specified by `x-max-tooltip-size'.
4864 Text larger than the specified size is clipped. */)
4865 (Lisp_Object string, Lisp_Object frame, Lisp_Object parms, Lisp_Object timeout, Lisp_Object dx, Lisp_Object dy)
4866 {
4867 struct frame *f;
4868 struct window *w;
4869 int root_x, root_y;
4870 struct buffer *old_buffer;
4871 struct text_pos pos;
4872 int i, width, height, seen_reversed_p;
4873 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
4874 int old_windows_or_buffers_changed = windows_or_buffers_changed;
4875 ptrdiff_t count = SPECPDL_INDEX ();
4876
4877 specbind (Qinhibit_redisplay, Qt);
4878
4879 GCPRO4 (string, parms, frame, timeout);
4880
4881 CHECK_STRING (string);
4882 if (SCHARS (string) == 0)
4883 string = make_unibyte_string (" ", 1);
4884
4885 f = decode_window_system_frame (frame);
4886 if (NILP (timeout))
4887 timeout = make_number (5);
4888 else
4889 CHECK_NATNUM (timeout);
4890
4891 if (NILP (dx))
4892 dx = make_number (5);
4893 else
4894 CHECK_NUMBER (dx);
4895
4896 if (NILP (dy))
4897 dy = make_number (-10);
4898 else
4899 CHECK_NUMBER (dy);
4900
4901 #ifdef USE_GTK
4902 if (x_gtk_use_system_tooltips)
4903 {
4904 bool ok;
4905
4906 /* Hide a previous tip, if any. */
4907 Fx_hide_tip ();
4908
4909 block_input ();
4910 ok = xg_prepare_tooltip (f, string, &width, &height);
4911 if (ok)
4912 {
4913 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
4914 xg_show_tooltip (f, root_x, root_y);
4915 /* This is used in Fx_hide_tip. */
4916 XSETFRAME (tip_frame, f);
4917 }
4918 unblock_input ();
4919 if (ok) goto start_timer;
4920 }
4921 #endif /* USE_GTK */
4922
4923 if (NILP (last_show_tip_args))
4924 last_show_tip_args = Fmake_vector (make_number (3), Qnil);
4925
4926 if (!NILP (tip_frame))
4927 {
4928 Lisp_Object last_string = AREF (last_show_tip_args, 0);
4929 Lisp_Object last_frame = AREF (last_show_tip_args, 1);
4930 Lisp_Object last_parms = AREF (last_show_tip_args, 2);
4931
4932 if (EQ (frame, last_frame)
4933 && !NILP (Fequal (last_string, string))
4934 && !NILP (Fequal (last_parms, parms)))
4935 {
4936 struct frame *tip_f = XFRAME (tip_frame);
4937
4938 /* Only DX and DY have changed. */
4939 if (!NILP (tip_timer))
4940 {
4941 Lisp_Object timer = tip_timer;
4942 tip_timer = Qnil;
4943 call1 (Qcancel_timer, timer);
4944 }
4945
4946 block_input ();
4947 compute_tip_xy (tip_f, parms, dx, dy, FRAME_PIXEL_WIDTH (tip_f),
4948 FRAME_PIXEL_HEIGHT (tip_f), &root_x, &root_y);
4949 XMoveWindow (FRAME_X_DISPLAY (tip_f), FRAME_X_WINDOW (tip_f),
4950 root_x, root_y);
4951 unblock_input ();
4952 goto start_timer;
4953 }
4954 }
4955
4956 /* Hide a previous tip, if any. */
4957 Fx_hide_tip ();
4958
4959 ASET (last_show_tip_args, 0, string);
4960 ASET (last_show_tip_args, 1, frame);
4961 ASET (last_show_tip_args, 2, parms);
4962
4963 /* Add default values to frame parameters. */
4964 if (NILP (Fassq (Qname, parms)))
4965 parms = Fcons (Fcons (Qname, build_string ("tooltip")), parms);
4966 if (NILP (Fassq (Qinternal_border_width, parms)))
4967 parms = Fcons (Fcons (Qinternal_border_width, make_number (3)), parms);
4968 if (NILP (Fassq (Qborder_width, parms)))
4969 parms = Fcons (Fcons (Qborder_width, make_number (1)), parms);
4970 if (NILP (Fassq (Qborder_color, parms)))
4971 parms = Fcons (Fcons (Qborder_color, build_string ("lightyellow")), parms);
4972 if (NILP (Fassq (Qbackground_color, parms)))
4973 parms = Fcons (Fcons (Qbackground_color, build_string ("lightyellow")),
4974 parms);
4975
4976 /* Create a frame for the tooltip, and record it in the global
4977 variable tip_frame. */
4978 frame = x_create_tip_frame (FRAME_X_DISPLAY_INFO (f), parms, string);
4979 f = XFRAME (frame);
4980
4981 /* Set up the frame's root window. */
4982 w = XWINDOW (FRAME_ROOT_WINDOW (f));
4983 w->left_col = 0;
4984 w->top_line = 0;
4985
4986 if (CONSP (Vx_max_tooltip_size)
4987 && RANGED_INTEGERP (1, XCAR (Vx_max_tooltip_size), INT_MAX)
4988 && RANGED_INTEGERP (1, XCDR (Vx_max_tooltip_size), INT_MAX))
4989 {
4990 w->total_cols = XFASTINT (XCAR (Vx_max_tooltip_size));
4991 w->total_lines = XFASTINT (XCDR (Vx_max_tooltip_size));
4992 }
4993 else
4994 {
4995 w->total_cols = 80;
4996 w->total_lines = 40;
4997 }
4998
4999 FRAME_TOTAL_COLS (f) = w->total_cols;
5000 adjust_glyphs (f);
5001 w->pseudo_window_p = 1;
5002
5003 /* Display the tooltip text in a temporary buffer. */
5004 old_buffer = current_buffer;
5005 set_buffer_internal_1 (XBUFFER (XWINDOW (FRAME_ROOT_WINDOW (f))->contents));
5006 bset_truncate_lines (current_buffer, Qnil);
5007 clear_glyph_matrix (w->desired_matrix);
5008 clear_glyph_matrix (w->current_matrix);
5009 SET_TEXT_POS (pos, BEGV, BEGV_BYTE);
5010 try_window (FRAME_ROOT_WINDOW (f), pos, TRY_WINDOW_IGNORE_FONTS_CHANGE);
5011
5012 /* Compute width and height of the tooltip. */
5013 width = height = seen_reversed_p = 0;
5014 for (i = 0; i < w->desired_matrix->nrows; ++i)
5015 {
5016 struct glyph_row *row = &w->desired_matrix->rows[i];
5017 struct glyph *last;
5018 int row_width;
5019
5020 /* Stop at the first empty row at the end. */
5021 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5022 break;
5023
5024 /* Let the row go over the full width of the frame. */
5025 row->full_width_p = 1;
5026
5027 row_width = row->pixel_width;
5028 if (row->used[TEXT_AREA])
5029 {
5030 /* There's a glyph at the end of rows that is used to place
5031 the cursor there. Don't include the width of this glyph. */
5032 if (!row->reversed_p)
5033 {
5034 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5035 if (INTEGERP (last->object))
5036 row_width -= last->pixel_width;
5037 }
5038 else
5039 {
5040 /* There could be a stretch glyph at the beginning of R2L
5041 rows that is produced by extend_face_to_end_of_line.
5042 Don't count that glyph. */
5043 struct glyph *g = row->glyphs[TEXT_AREA];
5044
5045 if (g->type == STRETCH_GLYPH && INTEGERP (g->object))
5046 {
5047 row_width -= g->pixel_width;
5048 seen_reversed_p = 1;
5049 }
5050 }
5051 }
5052
5053 height += row->height;
5054 width = max (width, row_width);
5055 }
5056
5057 /* If we've seen partial-length R2L rows, we need to re-adjust the
5058 tool-tip frame width and redisplay it again, to avoid over-wide
5059 tips due to the stretch glyph that extends R2L lines to full
5060 width of the frame. */
5061 if (seen_reversed_p)
5062 {
5063 /* w->total_cols and FRAME_TOTAL_COLS want the width in columns,
5064 not in pixels. */
5065 width /= WINDOW_FRAME_COLUMN_WIDTH (w);
5066 w->total_cols = width;
5067 FRAME_TOTAL_COLS (f) = width;
5068 adjust_glyphs (f);
5069 clear_glyph_matrix (w->desired_matrix);
5070 clear_glyph_matrix (w->current_matrix);
5071 try_window (FRAME_ROOT_WINDOW (f), pos, 0);
5072 width = height = 0;
5073 /* Recompute width and height of the tooltip. */
5074 for (i = 0; i < w->desired_matrix->nrows; ++i)
5075 {
5076 struct glyph_row *row = &w->desired_matrix->rows[i];
5077 struct glyph *last;
5078 int row_width;
5079
5080 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
5081 break;
5082 row->full_width_p = 1;
5083 row_width = row->pixel_width;
5084 if (row->used[TEXT_AREA] && !row->reversed_p)
5085 {
5086 last = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
5087 if (INTEGERP (last->object))
5088 row_width -= last->pixel_width;
5089 }
5090
5091 height += row->height;
5092 width = max (width, row_width);
5093 }
5094 }
5095
5096 /* Add the frame's internal border to the width and height the X
5097 window should have. */
5098 height += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5099 width += 2 * FRAME_INTERNAL_BORDER_WIDTH (f);
5100
5101 /* Move the tooltip window where the mouse pointer is. Resize and
5102 show it. */
5103 compute_tip_xy (f, parms, dx, dy, width, height, &root_x, &root_y);
5104
5105 block_input ();
5106 XMoveResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5107 root_x, root_y, width, height);
5108 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5109 unblock_input ();
5110
5111 /* Draw into the window. */
5112 w->must_be_updated_p = 1;
5113 update_single_window (w, 1);
5114
5115 /* Restore original current buffer. */
5116 set_buffer_internal_1 (old_buffer);
5117 windows_or_buffers_changed = old_windows_or_buffers_changed;
5118
5119 start_timer:
5120 /* Let the tip disappear after timeout seconds. */
5121 tip_timer = call3 (intern ("run-at-time"), timeout, Qnil,
5122 intern ("x-hide-tip"));
5123
5124 UNGCPRO;
5125 return unbind_to (count, Qnil);
5126 }
5127
5128
5129 DEFUN ("x-hide-tip", Fx_hide_tip, Sx_hide_tip, 0, 0, 0,
5130 doc: /* Hide the current tooltip window, if there is any.
5131 Value is t if tooltip was open, nil otherwise. */)
5132 (void)
5133 {
5134 ptrdiff_t count;
5135 Lisp_Object deleted, frame, timer;
5136 struct gcpro gcpro1, gcpro2;
5137
5138 /* Return quickly if nothing to do. */
5139 if (NILP (tip_timer) && NILP (tip_frame))
5140 return Qnil;
5141
5142 frame = tip_frame;
5143 timer = tip_timer;
5144 GCPRO2 (frame, timer);
5145 tip_frame = tip_timer = deleted = Qnil;
5146
5147 count = SPECPDL_INDEX ();
5148 specbind (Qinhibit_redisplay, Qt);
5149 specbind (Qinhibit_quit, Qt);
5150
5151 if (!NILP (timer))
5152 call1 (Qcancel_timer, timer);
5153
5154 #ifdef USE_GTK
5155 {
5156 /* When using system tooltip, tip_frame is the Emacs frame on which
5157 the tip is shown. */
5158 struct frame *f = XFRAME (frame);
5159 if (FRAME_LIVE_P (f) && xg_hide_tooltip (f))
5160 frame = Qnil;
5161 }
5162 #endif
5163
5164 if (FRAMEP (frame))
5165 {
5166 delete_frame (frame, Qnil);
5167 deleted = Qt;
5168
5169 #ifdef USE_LUCID
5170 /* Bloodcurdling hack alert: The Lucid menu bar widget's
5171 redisplay procedure is not called when a tip frame over menu
5172 items is unmapped. Redisplay the menu manually... */
5173 {
5174 Widget w;
5175 struct frame *f = SELECTED_FRAME ();
5176 w = f->output_data.x->menubar_widget;
5177
5178 if (!DoesSaveUnders (FRAME_X_DISPLAY_INFO (f)->screen)
5179 && w != NULL)
5180 {
5181 block_input ();
5182 xlwmenu_redisplay (w);
5183 unblock_input ();
5184 }
5185 }
5186 #endif /* USE_LUCID */
5187 }
5188
5189 UNGCPRO;
5190 return unbind_to (count, deleted);
5191 }
5192
5193
5194 \f
5195 /***********************************************************************
5196 File selection dialog
5197 ***********************************************************************/
5198
5199 DEFUN ("x-uses-old-gtk-dialog", Fx_uses_old_gtk_dialog,
5200 Sx_uses_old_gtk_dialog,
5201 0, 0, 0,
5202 doc: /* Return t if the old Gtk+ file selection dialog is used. */)
5203 (void)
5204 {
5205 #ifdef USE_GTK
5206 if (use_dialog_box
5207 && use_file_dialog
5208 && window_system_available (SELECTED_FRAME ())
5209 && xg_uses_old_file_dialog ())
5210 return Qt;
5211 #endif
5212 return Qnil;
5213 }
5214
5215
5216 #ifdef USE_MOTIF
5217 /* Callback for "OK" and "Cancel" on file selection dialog. */
5218
5219 static void
5220 file_dialog_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5221 {
5222 int *result = (int *) client_data;
5223 XmAnyCallbackStruct *cb = (XmAnyCallbackStruct *) call_data;
5224 *result = cb->reason;
5225 }
5226
5227
5228 /* Callback for unmapping a file selection dialog. This is used to
5229 capture the case where a dialog is closed via a window manager's
5230 closer button, for example. Using a XmNdestroyCallback didn't work
5231 in this case. */
5232
5233 static void
5234 file_dialog_unmap_cb (Widget widget, XtPointer client_data, XtPointer call_data)
5235 {
5236 int *result = (int *) client_data;
5237 *result = XmCR_CANCEL;
5238 }
5239
5240 static Lisp_Object
5241 clean_up_file_dialog (Lisp_Object arg)
5242 {
5243 Widget dialog = XSAVE_POINTER (arg, 0);
5244
5245 /* Clean up. */
5246 block_input ();
5247 XtUnmanageChild (dialog);
5248 XtDestroyWidget (dialog);
5249 x_menu_set_in_use (0);
5250 unblock_input ();
5251
5252 return Qnil;
5253 }
5254
5255
5256 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5257 doc: /* Read file name, prompting with PROMPT in directory DIR.
5258 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5259 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5260 or directory must exist.
5261
5262 This function is only defined on NS, MS Windows, and X Windows with the
5263 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5264 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5265 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5266 {
5267 int result;
5268 struct frame *f = SELECTED_FRAME ();
5269 Lisp_Object file = Qnil;
5270 Lisp_Object decoded_file;
5271 Widget dialog, text, help;
5272 Arg al[10];
5273 int ac = 0;
5274 XmString dir_xmstring, pattern_xmstring;
5275 ptrdiff_t count = SPECPDL_INDEX ();
5276 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5277
5278 check_window_system (f);
5279
5280 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5281
5282 if (popup_activated ())
5283 error ("Trying to use a menu from within a menu-entry");
5284
5285 CHECK_STRING (prompt);
5286 CHECK_STRING (dir);
5287
5288 /* Prevent redisplay. */
5289 specbind (Qinhibit_redisplay, Qt);
5290
5291 block_input ();
5292
5293 /* Create the dialog with PROMPT as title, using DIR as initial
5294 directory and using "*" as pattern. */
5295 dir = Fexpand_file_name (dir, Qnil);
5296 dir_xmstring = XmStringCreateLocalized (SSDATA (dir));
5297 pattern_xmstring = XmStringCreateLocalized ("*");
5298
5299 XtSetArg (al[ac], XmNtitle, SDATA (prompt)); ++ac;
5300 XtSetArg (al[ac], XmNdirectory, dir_xmstring); ++ac;
5301 XtSetArg (al[ac], XmNpattern, pattern_xmstring); ++ac;
5302 XtSetArg (al[ac], XmNresizePolicy, XmRESIZE_GROW); ++ac;
5303 XtSetArg (al[ac], XmNdialogStyle, XmDIALOG_APPLICATION_MODAL); ++ac;
5304 dialog = XmCreateFileSelectionDialog (f->output_data.x->widget,
5305 "fsb", al, ac);
5306 XmStringFree (dir_xmstring);
5307 XmStringFree (pattern_xmstring);
5308
5309 /* Add callbacks for OK and Cancel. */
5310 XtAddCallback (dialog, XmNokCallback, file_dialog_cb,
5311 (XtPointer) &result);
5312 XtAddCallback (dialog, XmNcancelCallback, file_dialog_cb,
5313 (XtPointer) &result);
5314 XtAddCallback (dialog, XmNunmapCallback, file_dialog_unmap_cb,
5315 (XtPointer) &result);
5316
5317 /* Remove the help button since we can't display help. */
5318 help = XmFileSelectionBoxGetChild (dialog, XmDIALOG_HELP_BUTTON);
5319 XtUnmanageChild (help);
5320
5321 /* Mark OK button as default. */
5322 XtVaSetValues (XmFileSelectionBoxGetChild (dialog, XmDIALOG_OK_BUTTON),
5323 XmNshowAsDefault, True, NULL);
5324
5325 /* If MUSTMATCH is non-nil, disable the file entry field of the
5326 dialog, so that the user must select a file from the files list
5327 box. We can't remove it because we wouldn't have a way to get at
5328 the result file name, then. */
5329 text = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5330 if (!NILP (mustmatch))
5331 {
5332 Widget label;
5333 label = XmFileSelectionBoxGetChild (dialog, XmDIALOG_SELECTION_LABEL);
5334 XtSetSensitive (text, False);
5335 XtSetSensitive (label, False);
5336 }
5337
5338 /* Manage the dialog, so that list boxes get filled. */
5339 XtManageChild (dialog);
5340
5341 if (STRINGP (default_filename))
5342 {
5343 XmString default_xmstring;
5344 Widget wtext = XmFileSelectionBoxGetChild (dialog, XmDIALOG_TEXT);
5345 Widget list = XmFileSelectionBoxGetChild (dialog, XmDIALOG_LIST);
5346
5347 XmTextPosition last_pos = XmTextFieldGetLastPosition (wtext);
5348 XmTextFieldReplace (wtext, 0, last_pos,
5349 (SSDATA (Ffile_name_nondirectory (default_filename))));
5350
5351 /* Select DEFAULT_FILENAME in the files list box. DEFAULT_FILENAME
5352 must include the path for this to work. */
5353
5354 default_xmstring = XmStringCreateLocalized (SSDATA (default_filename));
5355
5356 if (XmListItemExists (list, default_xmstring))
5357 {
5358 int item_pos = XmListItemPos (list, default_xmstring);
5359 /* Select the item and scroll it into view. */
5360 XmListSelectPos (list, item_pos, True);
5361 XmListSetPos (list, item_pos);
5362 }
5363
5364 XmStringFree (default_xmstring);
5365 }
5366
5367 record_unwind_protect (clean_up_file_dialog, make_save_pointer (dialog));
5368
5369 /* Process events until the user presses Cancel or OK. */
5370 x_menu_set_in_use (1);
5371 result = 0;
5372 while (result == 0)
5373 {
5374 XEvent event;
5375 x_menu_wait_for_event (0);
5376 XtAppNextEvent (Xt_app_con, &event);
5377 if (event.type == KeyPress
5378 && FRAME_X_DISPLAY (f) == event.xkey.display)
5379 {
5380 KeySym keysym = XLookupKeysym (&event.xkey, 0);
5381
5382 /* Pop down on C-g. */
5383 if (keysym == XK_g && (event.xkey.state & ControlMask) != 0)
5384 XtUnmanageChild (dialog);
5385 }
5386
5387 (void) x_dispatch_event (&event, FRAME_X_DISPLAY (f));
5388 }
5389
5390 /* Get the result. */
5391 if (result == XmCR_OK)
5392 {
5393 XmString text_string;
5394 String data;
5395
5396 XtVaGetValues (dialog, XmNtextString, &text_string, NULL);
5397 XmStringGetLtoR (text_string, XmFONTLIST_DEFAULT_TAG, &data);
5398 XmStringFree (text_string);
5399 file = build_string (data);
5400 XtFree (data);
5401 }
5402 else
5403 file = Qnil;
5404
5405 unblock_input ();
5406 UNGCPRO;
5407
5408 /* Make "Cancel" equivalent to C-g. */
5409 if (NILP (file))
5410 Fsignal (Qquit, Qnil);
5411
5412 decoded_file = DECODE_FILE (file);
5413
5414 return unbind_to (count, decoded_file);
5415 }
5416
5417 #endif /* USE_MOTIF */
5418
5419 #ifdef USE_GTK
5420
5421 static Lisp_Object
5422 clean_up_dialog (Lisp_Object arg)
5423 {
5424 x_menu_set_in_use (0);
5425
5426 return Qnil;
5427 }
5428
5429 DEFUN ("x-file-dialog", Fx_file_dialog, Sx_file_dialog, 2, 5, 0,
5430 doc: /* Read file name, prompting with PROMPT in directory DIR.
5431 Use a file selection dialog. Select DEFAULT-FILENAME in the dialog's file
5432 selection box, if specified. If MUSTMATCH is non-nil, the returned file
5433 or directory must exist.
5434
5435 This function is only defined on NS, MS Windows, and X Windows with the
5436 Motif or Gtk toolkits. With the Motif toolkit, ONLY-DIR-P is ignored.
5437 Otherwise, if ONLY-DIR-P is non-nil, the user can only select directories. */)
5438 (Lisp_Object prompt, Lisp_Object dir, Lisp_Object default_filename, Lisp_Object mustmatch, Lisp_Object only_dir_p)
5439 {
5440 FRAME_PTR f = SELECTED_FRAME ();
5441 char *fn;
5442 Lisp_Object file = Qnil;
5443 Lisp_Object decoded_file;
5444 ptrdiff_t count = SPECPDL_INDEX ();
5445 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4, gcpro5, gcpro6;
5446 char *cdef_file;
5447
5448 check_window_system (f);
5449
5450 GCPRO6 (prompt, dir, default_filename, mustmatch, only_dir_p, file);
5451
5452 if (popup_activated ())
5453 error ("Trying to use a menu from within a menu-entry");
5454
5455 CHECK_STRING (prompt);
5456 CHECK_STRING (dir);
5457
5458 /* Prevent redisplay. */
5459 specbind (Qinhibit_redisplay, Qt);
5460 record_unwind_protect (clean_up_dialog, Qnil);
5461
5462 block_input ();
5463
5464 if (STRINGP (default_filename))
5465 cdef_file = SSDATA (default_filename);
5466 else
5467 cdef_file = SSDATA (dir);
5468
5469 fn = xg_get_file_name (f, SSDATA (prompt), cdef_file,
5470 ! NILP (mustmatch),
5471 ! NILP (only_dir_p));
5472
5473 if (fn)
5474 {
5475 file = build_string (fn);
5476 xfree (fn);
5477 }
5478
5479 unblock_input ();
5480 UNGCPRO;
5481
5482 /* Make "Cancel" equivalent to C-g. */
5483 if (NILP (file))
5484 Fsignal (Qquit, Qnil);
5485
5486 decoded_file = DECODE_FILE (file);
5487
5488 return unbind_to (count, decoded_file);
5489 }
5490
5491
5492 #ifdef HAVE_FREETYPE
5493
5494 DEFUN ("x-select-font", Fx_select_font, Sx_select_font, 0, 2, 0,
5495 doc: /* Read a font using a GTK dialog.
5496 Return either a font spec (for GTK versions >= 3.2) or a string
5497 containing a GTK-style font name.
5498
5499 FRAME is the frame on which to pop up the font chooser. If omitted or
5500 nil, it defaults to the selected frame. */)
5501 (Lisp_Object frame, Lisp_Object ignored)
5502 {
5503 FRAME_PTR f = decode_window_system_frame (frame);
5504 Lisp_Object font;
5505 Lisp_Object font_param;
5506 char *default_name = NULL;
5507 struct gcpro gcpro1, gcpro2;
5508 ptrdiff_t count = SPECPDL_INDEX ();
5509
5510 if (popup_activated ())
5511 error ("Trying to use a menu from within a menu-entry");
5512
5513 /* Prevent redisplay. */
5514 specbind (Qinhibit_redisplay, Qt);
5515 record_unwind_protect (clean_up_dialog, Qnil);
5516
5517 block_input ();
5518
5519 GCPRO2 (font_param, font);
5520
5521 XSETFONT (font, FRAME_FONT (f));
5522 font_param = Ffont_get (font, intern (":name"));
5523 if (STRINGP (font_param))
5524 default_name = xstrdup (SSDATA (font_param));
5525 else
5526 {
5527 font_param = Fframe_parameter (frame, Qfont_param);
5528 if (STRINGP (font_param))
5529 default_name = xstrdup (SSDATA (font_param));
5530 }
5531
5532 font = xg_get_font (f, default_name);
5533 xfree (default_name);
5534
5535 unblock_input ();
5536
5537 if (NILP (font))
5538 Fsignal (Qquit, Qnil);
5539
5540 return unbind_to (count, font);
5541 }
5542 #endif /* HAVE_FREETYPE */
5543
5544 #endif /* USE_GTK */
5545
5546 \f
5547 /***********************************************************************
5548 Keyboard
5549 ***********************************************************************/
5550
5551 #ifdef HAVE_XKB
5552 #include <X11/XKBlib.h>
5553 #include <X11/keysym.h>
5554 #endif
5555
5556 DEFUN ("x-backspace-delete-keys-p", Fx_backspace_delete_keys_p,
5557 Sx_backspace_delete_keys_p, 0, 1, 0,
5558 doc: /* Check if both Backspace and Delete keys are on the keyboard of FRAME.
5559 FRAME nil means use the selected frame.
5560 Value is t if we know that both keys are present, and are mapped to the
5561 usual X keysyms. Value is `lambda' if we cannot determine if both keys are
5562 present and mapped to the usual X keysyms. */)
5563 (Lisp_Object frame)
5564 {
5565 #ifndef HAVE_XKB
5566 return Qlambda;
5567 #else
5568 XkbDescPtr kb;
5569 struct frame *f = decode_window_system_frame (frame);
5570 Display *dpy = FRAME_X_DISPLAY (f);
5571 Lisp_Object have_keys;
5572 int major, minor, op, event, error_code;
5573
5574 block_input ();
5575
5576 /* Check library version in case we're dynamically linked. */
5577 major = XkbMajorVersion;
5578 minor = XkbMinorVersion;
5579 if (!XkbLibraryVersion (&major, &minor))
5580 {
5581 unblock_input ();
5582 return Qlambda;
5583 }
5584
5585 /* Check that the server supports XKB. */
5586 major = XkbMajorVersion;
5587 minor = XkbMinorVersion;
5588 if (!XkbQueryExtension (dpy, &op, &event, &error_code, &major, &minor))
5589 {
5590 unblock_input ();
5591 return Qlambda;
5592 }
5593
5594 /* In this code we check that the keyboard has physical keys with names
5595 that start with BKSP (Backspace) and DELE (Delete), and that they
5596 generate keysym XK_BackSpace and XK_Delete respectively.
5597 This function is used to test if normal-erase-is-backspace should be
5598 turned on.
5599 An alternative approach would be to just check if XK_BackSpace and
5600 XK_Delete are mapped to any key. But if any of those are mapped to
5601 some non-intuitive key combination (Meta-Shift-Ctrl-whatever) and the
5602 user doesn't know about it, it is better to return false here.
5603 It is more obvious to the user what to do if she/he has two keys
5604 clearly marked with names/symbols and one key does something not
5605 expected (i.e. she/he then tries the other).
5606 The cases where Backspace/Delete is mapped to some other key combination
5607 are rare, and in those cases, normal-erase-is-backspace can be turned on
5608 manually. */
5609
5610 have_keys = Qnil;
5611 kb = XkbGetMap (dpy, XkbAllMapComponentsMask, XkbUseCoreKbd);
5612 if (kb)
5613 {
5614 int delete_keycode = 0, backspace_keycode = 0, i;
5615
5616 if (XkbGetNames (dpy, XkbAllNamesMask, kb) == Success)
5617 {
5618 for (i = kb->min_key_code;
5619 (i < kb->max_key_code
5620 && (delete_keycode == 0 || backspace_keycode == 0));
5621 ++i)
5622 {
5623 /* The XKB symbolic key names can be seen most easily in
5624 the PS file generated by `xkbprint -label name
5625 $DISPLAY'. */
5626 if (memcmp ("DELE", kb->names->keys[i].name, 4) == 0)
5627 delete_keycode = i;
5628 else if (memcmp ("BKSP", kb->names->keys[i].name, 4) == 0)
5629 backspace_keycode = i;
5630 }
5631
5632 XkbFreeNames (kb, 0, True);
5633 }
5634
5635 XkbFreeClientMap (kb, 0, True);
5636
5637 if (delete_keycode
5638 && backspace_keycode
5639 && XKeysymToKeycode (dpy, XK_Delete) == delete_keycode
5640 && XKeysymToKeycode (dpy, XK_BackSpace) == backspace_keycode)
5641 have_keys = Qt;
5642 }
5643 unblock_input ();
5644 return have_keys;
5645 #endif
5646 }
5647
5648
5649 \f
5650 /***********************************************************************
5651 Initialization
5652 ***********************************************************************/
5653
5654 /* Keep this list in the same order as frame_parms in frame.c.
5655 Use 0 for unsupported frame parameters. */
5656
5657 frame_parm_handler x_frame_parm_handlers[] =
5658 {
5659 x_set_autoraise,
5660 x_set_autolower,
5661 x_set_background_color,
5662 x_set_border_color,
5663 x_set_border_width,
5664 x_set_cursor_color,
5665 x_set_cursor_type,
5666 x_set_font,
5667 x_set_foreground_color,
5668 x_set_icon_name,
5669 x_set_icon_type,
5670 x_set_internal_border_width,
5671 x_set_menu_bar_lines,
5672 x_set_mouse_color,
5673 x_explicitly_set_name,
5674 x_set_scroll_bar_width,
5675 x_set_title,
5676 x_set_unsplittable,
5677 x_set_vertical_scroll_bars,
5678 x_set_visibility,
5679 x_set_tool_bar_lines,
5680 x_set_scroll_bar_foreground,
5681 x_set_scroll_bar_background,
5682 x_set_screen_gamma,
5683 x_set_line_spacing,
5684 x_set_fringe_width,
5685 x_set_fringe_width,
5686 x_set_wait_for_wm,
5687 x_set_fullscreen,
5688 x_set_font_backend,
5689 x_set_alpha,
5690 x_set_sticky,
5691 x_set_tool_bar_position,
5692 };
5693
5694 void
5695 syms_of_xfns (void)
5696 {
5697 /* The section below is built by the lisp expression at the top of the file,
5698 just above where these variables are declared. */
5699 /*&&& init symbols here &&&*/
5700 DEFSYM (Qsuppress_icon, "suppress-icon");
5701 DEFSYM (Qundefined_color, "undefined-color");
5702 DEFSYM (Qcompound_text, "compound-text");
5703 DEFSYM (Qcancel_timer, "cancel-timer");
5704 DEFSYM (Qfont_param, "font-parameter");
5705 /* This is the end of symbol initialization. */
5706
5707 Fput (Qundefined_color, Qerror_conditions,
5708 listn (CONSTYPE_PURE, 2, Qundefined_color, Qerror));
5709 Fput (Qundefined_color, Qerror_message,
5710 build_pure_c_string ("Undefined color"));
5711
5712 DEFVAR_LISP ("x-pointer-shape", Vx_pointer_shape,
5713 doc: /* The shape of the pointer when over text.
5714 Changing the value does not affect existing frames
5715 unless you set the mouse color. */);
5716 Vx_pointer_shape = Qnil;
5717
5718 #if 0 /* This doesn't really do anything. */
5719 DEFVAR_LISP ("x-nontext-pointer-shape", Vx_nontext_pointer_shape,
5720 doc: /* The shape of the pointer when not over text.
5721 This variable takes effect when you create a new frame
5722 or when you set the mouse color. */);
5723 #endif
5724 Vx_nontext_pointer_shape = Qnil;
5725
5726 DEFVAR_LISP ("x-hourglass-pointer-shape", Vx_hourglass_pointer_shape,
5727 doc: /* The shape of the pointer when Emacs is busy.
5728 This variable takes effect when you create a new frame
5729 or when you set the mouse color. */);
5730 Vx_hourglass_pointer_shape = Qnil;
5731
5732 #if 0 /* This doesn't really do anything. */
5733 DEFVAR_LISP ("x-mode-pointer-shape", Vx_mode_pointer_shape,
5734 doc: /* The shape of the pointer when over the mode line.
5735 This variable takes effect when you create a new frame
5736 or when you set the mouse color. */);
5737 #endif
5738 Vx_mode_pointer_shape = Qnil;
5739
5740 DEFVAR_LISP ("x-sensitive-text-pointer-shape",
5741 Vx_sensitive_text_pointer_shape,
5742 doc: /* The shape of the pointer when over mouse-sensitive text.
5743 This variable takes effect when you create a new frame
5744 or when you set the mouse color. */);
5745 Vx_sensitive_text_pointer_shape = Qnil;
5746
5747 DEFVAR_LISP ("x-window-horizontal-drag-cursor",
5748 Vx_window_horizontal_drag_shape,
5749 doc: /* Pointer shape to use for indicating a window can be dragged horizontally.
5750 This variable takes effect when you create a new frame
5751 or when you set the mouse color. */);
5752 Vx_window_horizontal_drag_shape = Qnil;
5753
5754 DEFVAR_LISP ("x-cursor-fore-pixel", Vx_cursor_fore_pixel,
5755 doc: /* A string indicating the foreground color of the cursor box. */);
5756 Vx_cursor_fore_pixel = Qnil;
5757
5758 DEFVAR_LISP ("x-max-tooltip-size", Vx_max_tooltip_size,
5759 doc: /* Maximum size for tooltips.
5760 Value is a pair (COLUMNS . ROWS). Text larger than this is clipped. */);
5761 Vx_max_tooltip_size = Fcons (make_number (80), make_number (40));
5762
5763 DEFVAR_LISP ("x-no-window-manager", Vx_no_window_manager,
5764 doc: /* Non-nil if no X window manager is in use.
5765 Emacs doesn't try to figure this out; this is always nil
5766 unless you set it to something else. */);
5767 /* We don't have any way to find this out, so set it to nil
5768 and maybe the user would like to set it to t. */
5769 Vx_no_window_manager = Qnil;
5770
5771 DEFVAR_LISP ("x-pixel-size-width-font-regexp",
5772 Vx_pixel_size_width_font_regexp,
5773 doc: /* Regexp matching a font name whose width is the same as `PIXEL_SIZE'.
5774
5775 Since Emacs gets width of a font matching with this regexp from
5776 PIXEL_SIZE field of the name, font finding mechanism gets faster for
5777 such a font. This is especially effective for such large fonts as
5778 Chinese, Japanese, and Korean. */);
5779 Vx_pixel_size_width_font_regexp = Qnil;
5780
5781 /* This is not ifdef:ed, so other builds than GTK can customize it. */
5782 DEFVAR_BOOL ("x-gtk-use-old-file-dialog", x_gtk_use_old_file_dialog,
5783 doc: /* Non-nil means prompt with the old GTK file selection dialog.
5784 If nil or if the file selection dialog is not available, the new GTK file
5785 chooser is used instead. To turn off all file dialogs set the
5786 variable `use-file-dialog'. */);
5787 x_gtk_use_old_file_dialog = 0;
5788
5789 DEFVAR_BOOL ("x-gtk-show-hidden-files", x_gtk_show_hidden_files,
5790 doc: /* If non-nil, the GTK file chooser will by default show hidden files.
5791 Note that this is just the default, there is a toggle button on the file
5792 chooser to show or not show hidden files on a case by case basis. */);
5793 x_gtk_show_hidden_files = 0;
5794
5795 DEFVAR_BOOL ("x-gtk-file-dialog-help-text", x_gtk_file_dialog_help_text,
5796 doc: /* If non-nil, the GTK file chooser will show additional help text.
5797 If more space for files in the file chooser dialog is wanted, set this to nil
5798 to turn the additional text off. */);
5799 x_gtk_file_dialog_help_text = 1;
5800
5801 DEFVAR_BOOL ("x-gtk-whole-detached-tool-bar", x_gtk_whole_detached_tool_bar,
5802 doc: /* If non-nil, a detached tool bar is shown in full.
5803 The default is to just show an arrow and pressing on that arrow shows
5804 the tool bar buttons. */);
5805 x_gtk_whole_detached_tool_bar = 0;
5806
5807 DEFVAR_BOOL ("x-gtk-use-system-tooltips", x_gtk_use_system_tooltips,
5808 doc: /* If non-nil with a Gtk+ built Emacs, the Gtk+ tooltip is used.
5809 Otherwise use Emacs own tooltip implementation.
5810 When using Gtk+ tooltips, the tooltip face is not used. */);
5811 x_gtk_use_system_tooltips = 1;
5812
5813 Fprovide (intern_c_string ("x"), Qnil);
5814
5815 #ifdef USE_X_TOOLKIT
5816 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5817 #ifdef USE_MOTIF
5818 Fprovide (intern_c_string ("motif"), Qnil);
5819
5820 DEFVAR_LISP ("motif-version-string", Vmotif_version_string,
5821 doc: /* Version info for LessTif/Motif. */);
5822 Vmotif_version_string = build_string (XmVERSION_STRING);
5823 #endif /* USE_MOTIF */
5824 #endif /* USE_X_TOOLKIT */
5825
5826 #ifdef USE_GTK
5827 /* Provide x-toolkit also for GTK. Internally GTK does not use Xt so it
5828 is not an X toolkit in that sense (USE_X_TOOLKIT is not defined).
5829 But for a user it is a toolkit for X, and indeed, configure
5830 accepts --with-x-toolkit=gtk. */
5831 Fprovide (intern_c_string ("x-toolkit"), Qnil);
5832 Fprovide (intern_c_string ("gtk"), Qnil);
5833 Fprovide (intern_c_string ("move-toolbar"), Qnil);
5834
5835 DEFVAR_LISP ("gtk-version-string", Vgtk_version_string,
5836 doc: /* Version info for GTK+. */);
5837 {
5838 char gtk_version[sizeof ".." + 3 * INT_STRLEN_BOUND (int)];
5839 int len = sprintf (gtk_version, "%d.%d.%d",
5840 GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION);
5841 Vgtk_version_string = make_pure_string (gtk_version, len, len, 0);
5842 }
5843 #endif /* USE_GTK */
5844
5845 /* X window properties. */
5846 defsubr (&Sx_change_window_property);
5847 defsubr (&Sx_delete_window_property);
5848 defsubr (&Sx_window_property);
5849
5850 defsubr (&Sxw_display_color_p);
5851 defsubr (&Sx_display_grayscale_p);
5852 defsubr (&Sxw_color_defined_p);
5853 defsubr (&Sxw_color_values);
5854 defsubr (&Sx_server_max_request_size);
5855 defsubr (&Sx_server_vendor);
5856 defsubr (&Sx_server_version);
5857 defsubr (&Sx_display_pixel_width);
5858 defsubr (&Sx_display_pixel_height);
5859 defsubr (&Sx_display_mm_width);
5860 defsubr (&Sx_display_mm_height);
5861 defsubr (&Sx_display_screens);
5862 defsubr (&Sx_display_planes);
5863 defsubr (&Sx_display_color_cells);
5864 defsubr (&Sx_display_visual_class);
5865 defsubr (&Sx_display_backing_store);
5866 defsubr (&Sx_display_save_under);
5867 defsubr (&Sx_wm_set_size_hint);
5868 defsubr (&Sx_create_frame);
5869 defsubr (&Sx_open_connection);
5870 defsubr (&Sx_close_connection);
5871 defsubr (&Sx_display_list);
5872 defsubr (&Sx_synchronize);
5873 defsubr (&Sx_focus_frame);
5874 defsubr (&Sx_backspace_delete_keys_p);
5875
5876 defsubr (&Sx_show_tip);
5877 defsubr (&Sx_hide_tip);
5878 tip_timer = Qnil;
5879 staticpro (&tip_timer);
5880 tip_frame = Qnil;
5881 staticpro (&tip_frame);
5882
5883 last_show_tip_args = Qnil;
5884 staticpro (&last_show_tip_args);
5885
5886 defsubr (&Sx_uses_old_gtk_dialog);
5887 #if defined (USE_MOTIF) || defined (USE_GTK)
5888 defsubr (&Sx_file_dialog);
5889 #endif
5890
5891 #if defined (USE_GTK) && defined (HAVE_FREETYPE)
5892 defsubr (&Sx_select_font);
5893 #endif
5894 }
5895
5896 #endif /* HAVE_X_WINDOWS */