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