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