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