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