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