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