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