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