Merge from emacs--devo--0
[emacs.git] / src / xterm.c
blobd2fb432e82edf7779fa5e208dc45e295cba04176
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "character.h"
72 #include "coding.h"
73 #include "ccl.h"
74 #include "frame.h"
75 #include "dispextern.h"
76 #include "fontset.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #include "gnu.h"
81 #include "disptab.h"
82 #include "buffer.h"
83 #include "window.h"
84 #include "keyboard.h"
85 #include "intervals.h"
86 #include "process.h"
87 #include "atimer.h"
88 #include "keymap.h"
90 #ifdef USE_X_TOOLKIT
91 #include <X11/Shell.h>
92 #endif
94 #ifdef HAVE_SYS_TIME_H
95 #include <sys/time.h>
96 #endif
97 #ifdef HAVE_UNISTD_H
98 #include <unistd.h>
99 #endif
101 #ifdef USE_GTK
102 #include "gtkutil.h"
103 #endif
105 #ifdef USE_FONT_BACKEND
106 #include "font.h"
107 #endif /* USE_FONT_BACKEND */
109 #ifdef USE_LUCID
110 extern int xlwmenu_window_p P_ ((Widget w, Window window));
111 extern void xlwmenu_redisplay P_ ((Widget));
112 #endif
114 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
116 extern void free_frame_menubar P_ ((struct frame *));
117 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
118 int));
119 #endif
121 #ifdef USE_X_TOOLKIT
122 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
123 #define HACK_EDITRES
124 extern void _XEditResCheckMessages ();
125 #endif /* not NO_EDITRES */
127 /* Include toolkit specific headers for the scroll bar widget. */
129 #ifdef USE_TOOLKIT_SCROLL_BARS
130 #if defined USE_MOTIF
131 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
132 #include <Xm/ScrollBar.h>
133 #else /* !USE_MOTIF i.e. use Xaw */
135 #ifdef HAVE_XAW3D
136 #include <X11/Xaw3d/Simple.h>
137 #include <X11/Xaw3d/Scrollbar.h>
138 #include <X11/Xaw3d/ThreeD.h>
139 #else /* !HAVE_XAW3D */
140 #include <X11/Xaw/Simple.h>
141 #include <X11/Xaw/Scrollbar.h>
142 #endif /* !HAVE_XAW3D */
143 #ifndef XtNpickTop
144 #define XtNpickTop "pickTop"
145 #endif /* !XtNpickTop */
146 #endif /* !USE_MOTIF */
147 #endif /* USE_TOOLKIT_SCROLL_BARS */
149 #endif /* USE_X_TOOLKIT */
151 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
152 #define x_any_window_to_frame x_window_to_frame
153 #define x_top_window_to_frame x_window_to_frame
154 #endif
156 #ifdef USE_X_TOOLKIT
157 #include "widget.h"
158 #ifndef XtNinitialState
159 #define XtNinitialState "initialState"
160 #endif
161 #endif
163 #define abs(x) ((x) < 0 ? -(x) : (x))
165 /* Default to using XIM if available. */
166 #ifdef USE_XIM
167 int use_xim = 1;
168 #else
169 int use_xim = 0; /* configure --without-xim */
170 #endif
174 /* Non-nil means Emacs uses toolkit scroll bars. */
176 Lisp_Object Vx_toolkit_scroll_bars;
178 /* Non-zero means that a HELP_EVENT has been generated since Emacs
179 start. */
181 static int any_help_event_p;
183 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
184 static Lisp_Object last_window;
186 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
188 int x_use_underline_position_properties;
190 /* Non-zero means to draw the underline at the same place as the descent line. */
192 int x_underline_at_descent_line;
194 /* This is a chain of structures for all the X displays currently in
195 use. */
197 struct x_display_info *x_display_list;
199 /* This is a list of cons cells, each of the form (NAME
200 . FONT-LIST-CACHE), one for each element of x_display_list and in
201 the same order. NAME is the name of the frame. FONT-LIST-CACHE
202 records previous values returned by x-list-fonts. */
204 Lisp_Object x_display_name_list;
206 /* Frame being updated by update_frame. This is declared in term.c.
207 This is set by update_begin and looked at by all the XT functions.
208 It is zero while not inside an update. In that case, the XT
209 functions assume that `selected_frame' is the frame to apply to. */
211 extern struct frame *updating_frame;
213 /* This is a frame waiting to be auto-raised, within XTread_socket. */
215 struct frame *pending_autoraise_frame;
217 #ifdef USE_X_TOOLKIT
218 /* The application context for Xt use. */
219 XtAppContext Xt_app_con;
220 static String Xt_default_resources[] = {0};
221 #endif /* USE_X_TOOLKIT */
223 /* Non-zero means user is interacting with a toolkit scroll bar. */
225 static int toolkit_scroll_bar_interaction;
227 /* Non-zero means to not move point as a result of clicking on a
228 frame to focus it (when focus-follows-mouse is nil). */
230 int x_mouse_click_focus_ignore_position;
232 /* Non-zero timeout value means ignore next mouse click if it arrives
233 before that timeout elapses (i.e. as part of the same sequence of
234 events resulting from clicking on a frame to select it). */
236 static unsigned long ignore_next_mouse_click_timeout;
238 /* Mouse movement.
240 Formerly, we used PointerMotionHintMask (in standard_event_mask)
241 so that we would have to call XQueryPointer after each MotionNotify
242 event to ask for another such event. However, this made mouse tracking
243 slow, and there was a bug that made it eventually stop.
245 Simply asking for MotionNotify all the time seems to work better.
247 In order to avoid asking for motion events and then throwing most
248 of them away or busy-polling the server for mouse positions, we ask
249 the server for pointer motion hints. This means that we get only
250 one event per group of mouse movements. "Groups" are delimited by
251 other kinds of events (focus changes and button clicks, for
252 example), or by XQueryPointer calls; when one of these happens, we
253 get another MotionNotify event the next time the mouse moves. This
254 is at least as efficient as getting motion events when mouse
255 tracking is on, and I suspect only negligibly worse when tracking
256 is off. */
258 /* Where the mouse was last time we reported a mouse event. */
260 static XRectangle last_mouse_glyph;
261 static FRAME_PTR last_mouse_glyph_frame;
262 static Lisp_Object last_mouse_press_frame;
264 /* The scroll bar in which the last X motion event occurred.
266 If the last X motion event occurred in a scroll bar, we set this so
267 XTmouse_position can know whether to report a scroll bar motion or
268 an ordinary motion.
270 If the last X motion event didn't occur in a scroll bar, we set
271 this to Qnil, to tell XTmouse_position to return an ordinary motion
272 event. */
274 static Lisp_Object last_mouse_scroll_bar;
276 /* This is a hack. We would really prefer that XTmouse_position would
277 return the time associated with the position it returns, but there
278 doesn't seem to be any way to wrest the time-stamp from the server
279 along with the position query. So, we just keep track of the time
280 of the last movement we received, and return that in hopes that
281 it's somewhat accurate. */
283 static Time last_mouse_movement_time;
285 /* Time for last user interaction as returned in X events. */
287 static Time last_user_time;
289 /* Incremented by XTread_socket whenever it really tries to read
290 events. */
292 #ifdef __STDC__
293 static int volatile input_signal_count;
294 #else
295 static int input_signal_count;
296 #endif
298 /* Used locally within XTread_socket. */
300 static int x_noop_count;
302 /* Initial values of argv and argc. */
304 extern char **initial_argv;
305 extern int initial_argc;
307 extern Lisp_Object Vcommand_line_args, Vsystem_name;
309 /* Tells if a window manager is present or not. */
311 extern Lisp_Object Vx_no_window_manager;
313 extern Lisp_Object Qeql;
315 extern int errno;
317 /* A mask of extra modifier bits to put into every keyboard char. */
319 extern EMACS_INT extra_keyboard_modifiers;
321 /* The keysyms to use for the various modifiers. */
323 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
324 Lisp_Object Vx_keysym_table;
325 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
327 static Lisp_Object Qvendor_specific_keysyms;
328 static Lisp_Object Qlatin_1;
330 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
331 extern int x_bitmap_mask P_ ((FRAME_PTR, int));
333 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
334 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
335 static const XColor *x_color_cells P_ ((Display *, int *));
336 static void x_update_window_end P_ ((struct window *, int, int));
338 static int x_io_error_quitter P_ ((Display *));
339 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
340 static int x_compute_min_glyph_bounds P_ ((struct frame *));
341 static void x_update_end P_ ((struct frame *));
342 static void XTframe_up_to_date P_ ((struct frame *));
343 static void XTset_terminal_modes P_ ((void));
344 static void XTreset_terminal_modes P_ ((void));
345 static void x_clear_frame P_ ((void));
346 static void frame_highlight P_ ((struct frame *));
347 static void frame_unhighlight P_ ((struct frame *));
348 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
349 static void x_focus_changed P_ ((int, int, struct x_display_info *,
350 struct frame *, struct input_event *));
351 static void x_detect_focus_change P_ ((struct x_display_info *,
352 XEvent *, struct input_event *));
353 static void XTframe_rehighlight P_ ((struct frame *));
354 static void x_frame_rehighlight P_ ((struct x_display_info *));
355 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
356 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
357 enum text_cursor_kinds));
359 static void x_clip_to_row P_ ((struct window *, struct glyph_row *, int, GC));
360 static void x_flush P_ ((struct frame *f));
361 static void x_update_begin P_ ((struct frame *));
362 static void x_update_window_begin P_ ((struct window *));
363 static void x_after_update_window_line P_ ((struct glyph_row *));
364 static struct scroll_bar *x_window_to_scroll_bar P_ ((Display *, Window));
365 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
366 enum scroll_bar_part *,
367 Lisp_Object *, Lisp_Object *,
368 unsigned long *));
369 static void x_check_fullscreen P_ ((struct frame *));
370 static void x_check_expected_move P_ ((struct frame *, int, int));
371 static void x_sync_with_move P_ ((struct frame *, int, int, int));
372 static int handle_one_xevent P_ ((struct x_display_info *, XEvent *,
373 int *, struct input_event *));
374 /* Don't declare this NO_RETURN because we want no
375 interference with debugging failing X calls. */
376 static SIGTYPE x_connection_closed P_ ((Display *, char *));
379 /* Flush display of frame F, or of all frames if F is null. */
381 static void
382 x_flush (f)
383 struct frame *f;
385 BLOCK_INPUT;
386 if (f == NULL)
388 Lisp_Object rest, frame;
389 FOR_EACH_FRAME (rest, frame)
390 x_flush (XFRAME (frame));
392 else if (FRAME_X_P (f))
393 XFlush (FRAME_X_DISPLAY (f));
394 UNBLOCK_INPUT;
398 /* Remove calls to XFlush by defining XFlush to an empty replacement.
399 Calls to XFlush should be unnecessary because the X output buffer
400 is flushed automatically as needed by calls to XPending,
401 XNextEvent, or XWindowEvent according to the XFlush man page.
402 XTread_socket calls XPending. Removing XFlush improves
403 performance. */
405 #define XFlush(DISPLAY) (void) 0
408 /***********************************************************************
409 Debugging
410 ***********************************************************************/
412 #if 0
414 /* This is a function useful for recording debugging information about
415 the sequence of occurrences in this file. */
417 struct record
419 char *locus;
420 int type;
423 struct record event_record[100];
425 int event_record_index;
427 record_event (locus, type)
428 char *locus;
429 int type;
431 if (event_record_index == sizeof (event_record) / sizeof (struct record))
432 event_record_index = 0;
434 event_record[event_record_index].locus = locus;
435 event_record[event_record_index].type = type;
436 event_record_index++;
439 #endif /* 0 */
443 /* Return the struct x_display_info corresponding to DPY. */
445 struct x_display_info *
446 x_display_info_for_display (dpy)
447 Display *dpy;
449 struct x_display_info *dpyinfo;
451 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
452 if (dpyinfo->display == dpy)
453 return dpyinfo;
455 return 0;
460 /***********************************************************************
461 Starting and ending an update
462 ***********************************************************************/
464 /* Start an update of frame F. This function is installed as a hook
465 for update_begin, i.e. it is called when update_begin is called.
466 This function is called prior to calls to x_update_window_begin for
467 each window being updated. Currently, there is nothing to do here
468 because all interesting stuff is done on a window basis. */
470 static void
471 x_update_begin (f)
472 struct frame *f;
474 /* Nothing to do. */
478 /* Start update of window W. Set the global variable updated_window
479 to the window being updated and set output_cursor to the cursor
480 position of W. */
482 static void
483 x_update_window_begin (w)
484 struct window *w;
486 struct frame *f = XFRAME (WINDOW_FRAME (w));
487 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
489 updated_window = w;
490 set_output_cursor (&w->cursor);
492 BLOCK_INPUT;
494 if (f == display_info->mouse_face_mouse_frame)
496 /* Don't do highlighting for mouse motion during the update. */
497 display_info->mouse_face_defer = 1;
499 /* If F needs to be redrawn, simply forget about any prior mouse
500 highlighting. */
501 if (FRAME_GARBAGED_P (f))
502 display_info->mouse_face_window = Qnil;
504 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
505 their mouse_face_p flag set, which means that they are always
506 unequal to rows in a desired matrix which never have that
507 flag set. So, rows containing mouse-face glyphs are never
508 scrolled, and we don't have to switch the mouse highlight off
509 here to prevent it from being scrolled. */
511 /* Can we tell that this update does not affect the window
512 where the mouse highlight is? If so, no need to turn off.
513 Likewise, don't do anything if the frame is garbaged;
514 in that case, the frame's current matrix that we would use
515 is all wrong, and we will redisplay that line anyway. */
516 if (!NILP (display_info->mouse_face_window)
517 && w == XWINDOW (display_info->mouse_face_window))
519 int i;
521 for (i = 0; i < w->desired_matrix->nrows; ++i)
522 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
523 break;
525 if (i < w->desired_matrix->nrows)
526 clear_mouse_face (display_info);
528 #endif /* 0 */
531 UNBLOCK_INPUT;
535 /* Draw a vertical window border from (x,y0) to (x,y1) */
537 static void
538 x_draw_vertical_window_border (w, x, y0, y1)
539 struct window *w;
540 int x, y0, y1;
542 struct frame *f = XFRAME (WINDOW_FRAME (w));
543 struct face *face;
545 face = FACE_FROM_ID (f, VERTICAL_BORDER_FACE_ID);
546 if (face)
547 XSetForeground (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
548 face->foreground);
550 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
551 f->output_data.x->normal_gc, x, y0, x, y1);
554 /* End update of window W (which is equal to updated_window).
556 Draw vertical borders between horizontally adjacent windows, and
557 display W's cursor if CURSOR_ON_P is non-zero.
559 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
560 glyphs in mouse-face were overwritten. In that case we have to
561 make sure that the mouse-highlight is properly redrawn.
563 W may be a menu bar pseudo-window in case we don't have X toolkit
564 support. Such windows don't have a cursor, so don't display it
565 here. */
567 static void
568 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
569 struct window *w;
570 int cursor_on_p, mouse_face_overwritten_p;
572 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
574 if (!w->pseudo_window_p)
576 BLOCK_INPUT;
578 if (cursor_on_p)
579 display_and_set_cursor (w, 1, output_cursor.hpos,
580 output_cursor.vpos,
581 output_cursor.x, output_cursor.y);
583 if (draw_window_fringes (w, 1))
584 x_draw_vertical_border (w);
586 UNBLOCK_INPUT;
589 /* If a row with mouse-face was overwritten, arrange for
590 XTframe_up_to_date to redisplay the mouse highlight. */
591 if (mouse_face_overwritten_p)
593 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
594 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
595 dpyinfo->mouse_face_window = Qnil;
598 updated_window = NULL;
602 /* End update of frame F. This function is installed as a hook in
603 update_end. */
605 static void
606 x_update_end (f)
607 struct frame *f;
609 /* Mouse highlight may be displayed again. */
610 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
612 #ifndef XFlush
613 BLOCK_INPUT;
614 XFlush (FRAME_X_DISPLAY (f));
615 UNBLOCK_INPUT;
616 #endif
620 /* This function is called from various places in xdisp.c whenever a
621 complete update has been performed. The global variable
622 updated_window is not available here. */
624 static void
625 XTframe_up_to_date (f)
626 struct frame *f;
628 if (FRAME_X_P (f))
630 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
632 if (dpyinfo->mouse_face_deferred_gc
633 || f == dpyinfo->mouse_face_mouse_frame)
635 BLOCK_INPUT;
636 if (dpyinfo->mouse_face_mouse_frame)
637 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
638 dpyinfo->mouse_face_mouse_x,
639 dpyinfo->mouse_face_mouse_y);
640 dpyinfo->mouse_face_deferred_gc = 0;
641 UNBLOCK_INPUT;
647 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
648 arrow bitmaps, or clear the fringes if no bitmaps are required
649 before DESIRED_ROW is made current. The window being updated is
650 found in updated_window. This function It is called from
651 update_window_line only if it is known that there are differences
652 between bitmaps to be drawn between current row and DESIRED_ROW. */
654 static void
655 x_after_update_window_line (desired_row)
656 struct glyph_row *desired_row;
658 struct window *w = updated_window;
659 struct frame *f;
660 int width, height;
662 xassert (w);
664 if (!desired_row->mode_line_p && !w->pseudo_window_p)
665 desired_row->redraw_fringe_bitmaps_p = 1;
667 /* When a window has disappeared, make sure that no rest of
668 full-width rows stays visible in the internal border. Could
669 check here if updated_window is the leftmost/rightmost window,
670 but I guess it's not worth doing since vertically split windows
671 are almost never used, internal border is rarely set, and the
672 overhead is very small. */
673 if (windows_or_buffers_changed
674 && desired_row->full_width_p
675 && (f = XFRAME (w->frame),
676 width = FRAME_INTERNAL_BORDER_WIDTH (f),
677 width != 0)
678 && (height = desired_row->visible_height,
679 height > 0))
681 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
683 /* Internal border is drawn below the tool bar. */
684 if (WINDOWP (f->tool_bar_window)
685 && w == XWINDOW (f->tool_bar_window))
686 y -= width;
688 BLOCK_INPUT;
689 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
690 0, y, width, height, False);
691 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
692 FRAME_PIXEL_WIDTH (f) - width,
693 y, width, height, False);
694 UNBLOCK_INPUT;
698 static void
699 x_draw_fringe_bitmap (w, row, p)
700 struct window *w;
701 struct glyph_row *row;
702 struct draw_fringe_bitmap_params *p;
704 struct frame *f = XFRAME (WINDOW_FRAME (w));
705 Display *display = FRAME_X_DISPLAY (f);
706 Window window = FRAME_X_WINDOW (f);
707 GC gc = f->output_data.x->normal_gc;
708 struct face *face = p->face;
709 int rowY;
711 /* Must clip because of partially visible lines. */
712 rowY = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
713 if (p->y < rowY)
715 /* Adjust position of "bottom aligned" bitmap on partially
716 visible last row. */
717 int oldY = row->y;
718 int oldVH = row->visible_height;
719 row->visible_height = p->h;
720 row->y -= rowY - p->y;
721 x_clip_to_row (w, row, -1, gc);
722 row->y = oldY;
723 row->visible_height = oldVH;
725 else
726 x_clip_to_row (w, row, -1, gc);
728 if (p->bx >= 0 && !p->overlay_p)
730 /* In case the same realized face is used for fringes and
731 for something displayed in the text (e.g. face `region' on
732 mono-displays, the fill style may have been changed to
733 FillSolid in x_draw_glyph_string_background. */
734 if (face->stipple)
735 XSetFillStyle (display, face->gc, FillOpaqueStippled);
736 else
737 XSetForeground (display, face->gc, face->background);
739 XFillRectangle (display, window, face->gc,
740 p->bx, p->by, p->nx, p->ny);
742 if (!face->stipple)
743 XSetForeground (display, face->gc, face->foreground);
746 if (p->which)
748 unsigned char *bits;
749 Pixmap pixmap, clipmask = (Pixmap) 0;
750 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
751 XGCValues gcv;
753 if (p->wd > 8)
754 bits = (unsigned char *)(p->bits + p->dh);
755 else
756 bits = (unsigned char *)p->bits + p->dh;
758 /* Draw the bitmap. I believe these small pixmaps can be cached
759 by the server. */
760 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
761 (p->cursor_p
762 ? (p->overlay_p ? face->background
763 : f->output_data.x->cursor_pixel)
764 : face->foreground),
765 face->background, depth);
767 if (p->overlay_p)
769 clipmask = XCreatePixmapFromBitmapData (display,
770 FRAME_X_DISPLAY_INFO (f)->root_window,
771 bits, p->wd, p->h,
772 1, 0, 1);
773 gcv.clip_mask = clipmask;
774 gcv.clip_x_origin = p->x;
775 gcv.clip_y_origin = p->y;
776 XChangeGC (display, gc, GCClipMask | GCClipXOrigin | GCClipYOrigin, &gcv);
779 XCopyArea (display, pixmap, window, gc, 0, 0,
780 p->wd, p->h, p->x, p->y);
781 XFreePixmap (display, pixmap);
783 if (p->overlay_p)
785 gcv.clip_mask = (Pixmap) 0;
786 XChangeGC (display, gc, GCClipMask, &gcv);
787 XFreePixmap (display, clipmask);
791 XSetClipMask (display, gc, None);
796 /* This is called when starting Emacs and when restarting after
797 suspend. When starting Emacs, no X window is mapped. And nothing
798 must be done to Emacs's own window if it is suspended (though that
799 rarely happens). */
801 static void
802 XTset_terminal_modes ()
806 /* This is called when exiting or suspending Emacs. Exiting will make
807 the X-windows go away, and suspending requires no action. */
809 static void
810 XTreset_terminal_modes ()
816 /***********************************************************************
817 Display Iterator
818 ***********************************************************************/
820 /* Function prototypes of this page. */
822 static int x_encode_char P_ ((int, XChar2b *, struct font_info *,
823 struct charset *, int *));
826 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
827 is not contained in the font. */
829 static XCharStruct *
830 x_per_char_metric (font, char2b, font_type)
831 XFontStruct *font;
832 XChar2b *char2b;
833 int font_type; /* unused on X */
835 /* The result metric information. */
836 XCharStruct *pcm = NULL;
838 xassert (font && char2b);
840 if (font->per_char != NULL)
842 if (font->min_byte1 == 0 && font->max_byte1 == 0)
844 /* min_char_or_byte2 specifies the linear character index
845 corresponding to the first element of the per_char array,
846 max_char_or_byte2 is the index of the last character. A
847 character with non-zero CHAR2B->byte1 is not in the font.
848 A character with byte2 less than min_char_or_byte2 or
849 greater max_char_or_byte2 is not in the font. */
850 if (char2b->byte1 == 0
851 && char2b->byte2 >= font->min_char_or_byte2
852 && char2b->byte2 <= font->max_char_or_byte2)
853 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
855 else
857 /* If either min_byte1 or max_byte1 are nonzero, both
858 min_char_or_byte2 and max_char_or_byte2 are less than
859 256, and the 2-byte character index values corresponding
860 to the per_char array element N (counting from 0) are:
862 byte1 = N/D + min_byte1
863 byte2 = N\D + min_char_or_byte2
865 where:
867 D = max_char_or_byte2 - min_char_or_byte2 + 1
868 / = integer division
869 \ = integer modulus */
870 if (char2b->byte1 >= font->min_byte1
871 && char2b->byte1 <= font->max_byte1
872 && char2b->byte2 >= font->min_char_or_byte2
873 && char2b->byte2 <= font->max_char_or_byte2)
875 pcm = (font->per_char
876 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
877 * (char2b->byte1 - font->min_byte1))
878 + (char2b->byte2 - font->min_char_or_byte2));
882 else
884 /* If the per_char pointer is null, all glyphs between the first
885 and last character indexes inclusive have the same
886 information, as given by both min_bounds and max_bounds. */
887 if (char2b->byte2 >= font->min_char_or_byte2
888 && char2b->byte2 <= font->max_char_or_byte2)
889 pcm = &font->max_bounds;
892 return ((pcm == NULL
893 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
894 ? NULL : pcm);
898 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
899 the two-byte form of C. Encoding is returned in *CHAR2B. */
901 static int
902 x_encode_char (c, char2b, font_info, charset, two_byte_p)
903 int c;
904 XChar2b *char2b;
905 struct font_info *font_info;
906 struct charset *charset;
907 int *two_byte_p;
909 XFontStruct *font = font_info->font;
911 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
912 This may be either a program in a special encoder language or a
913 fixed encoding. */
914 if (font_info->font_encoder)
916 /* It's a program. */
917 struct ccl_program *ccl = font_info->font_encoder;
919 check_ccl_update (ccl);
920 if (CHARSET_DIMENSION (charset) == 1)
922 ccl->reg[0] = CHARSET_ID (charset);
923 ccl->reg[1] = char2b->byte2;
924 ccl->reg[2] = -1;
926 else
928 ccl->reg[0] = CHARSET_ID (charset);
929 ccl->reg[1] = char2b->byte1;
930 ccl->reg[2] = char2b->byte2;
933 ccl_driver (ccl, NULL, NULL, 0, 0, Qnil);
935 /* We assume that MSBs are appropriately set/reset by CCL
936 program. */
937 if (font->max_byte1 == 0) /* 1-byte font */
938 STORE_XCHAR2B (char2b, 0, ccl->reg[1]);
939 else
940 STORE_XCHAR2B (char2b, ccl->reg[1], ccl->reg[2]);
942 else if (font_info->encoding_type)
944 /* Fixed encoding scheme. See fontset.h for the meaning of the
945 encoding numbers. */
946 unsigned char enc = font_info->encoding_type;
948 if ((enc == 1 || enc == 2)
949 && CHARSET_DIMENSION (charset) == 2)
950 char2b->byte1 |= 0x80;
952 if (enc == 1 || enc == 3)
953 char2b->byte2 |= 0x80;
956 if (two_byte_p)
957 *two_byte_p = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
959 return FONT_TYPE_UNKNOWN;
964 /***********************************************************************
965 Glyph display
966 ***********************************************************************/
970 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
971 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
972 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
973 int));
974 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
975 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
976 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
977 static void x_draw_glyph_string P_ ((struct glyph_string *));
978 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
979 static void x_set_cursor_gc P_ ((struct glyph_string *));
980 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
981 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
982 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
983 unsigned long *, double, int));
984 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
985 double, int, unsigned long));
986 static void x_setup_relief_colors P_ ((struct glyph_string *));
987 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
988 static void x_draw_image_relief P_ ((struct glyph_string *));
989 static void x_draw_image_foreground P_ ((struct glyph_string *));
990 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
991 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
992 int, int, int));
993 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
994 int, int, int, int, int, int,
995 XRectangle *));
996 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
997 int, int, int, XRectangle *));
999 #if GLYPH_DEBUG
1000 static void x_check_font P_ ((struct frame *, XFontStruct *));
1001 #endif
1004 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
1005 face. */
1007 static void
1008 x_set_cursor_gc (s)
1009 struct glyph_string *s;
1011 if (s->font == FRAME_FONT (s->f)
1012 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
1013 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
1014 && !s->cmp)
1015 s->gc = s->f->output_data.x->cursor_gc;
1016 else
1018 /* Cursor on non-default face: must merge. */
1019 XGCValues xgcv;
1020 unsigned long mask;
1022 xgcv.background = s->f->output_data.x->cursor_pixel;
1023 xgcv.foreground = s->face->background;
1025 /* If the glyph would be invisible, try a different foreground. */
1026 if (xgcv.foreground == xgcv.background)
1027 xgcv.foreground = s->face->foreground;
1028 if (xgcv.foreground == xgcv.background)
1029 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
1030 if (xgcv.foreground == xgcv.background)
1031 xgcv.foreground = s->face->foreground;
1033 /* Make sure the cursor is distinct from text in this face. */
1034 if (xgcv.background == s->face->background
1035 && xgcv.foreground == s->face->foreground)
1037 xgcv.background = s->face->foreground;
1038 xgcv.foreground = s->face->background;
1041 IF_DEBUG (x_check_font (s->f, s->font));
1042 xgcv.font = s->font->fid;
1043 xgcv.graphics_exposures = False;
1044 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1046 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1047 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1048 mask, &xgcv);
1049 else
1050 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1051 = XCreateGC (s->display, s->window, mask, &xgcv);
1053 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1058 /* Set up S->gc of glyph string S for drawing text in mouse face. */
1060 static void
1061 x_set_mouse_face_gc (s)
1062 struct glyph_string *s;
1064 int face_id;
1065 struct face *face;
1067 /* What face has to be used last for the mouse face? */
1068 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
1069 face = FACE_FROM_ID (s->f, face_id);
1070 if (face == NULL)
1071 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
1073 if (s->first_glyph->type == CHAR_GLYPH)
1074 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch, -1, Qnil);
1075 else
1076 face_id = FACE_FOR_CHAR (s->f, face, 0, -1, Qnil);
1077 s->face = FACE_FROM_ID (s->f, face_id);
1078 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1080 /* If font in this face is same as S->font, use it. */
1081 if (s->font == s->face->font)
1082 s->gc = s->face->gc;
1083 #ifdef USE_FONT_BACKEND
1084 else if (enable_font_backend)
1085 /* No need of setting a font for s->gc. */
1086 s->gc = s->face->gc;
1087 #endif /* USE_FONT_BACKEND */
1088 else
1090 /* Otherwise construct scratch_cursor_gc with values from FACE
1091 but font FONT. */
1092 XGCValues xgcv;
1093 unsigned long mask;
1095 xgcv.background = s->face->background;
1096 xgcv.foreground = s->face->foreground;
1097 IF_DEBUG (x_check_font (s->f, s->font));
1098 xgcv.font = s->font->fid;
1099 xgcv.graphics_exposures = False;
1100 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
1102 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
1103 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
1104 mask, &xgcv);
1105 else
1106 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
1107 = XCreateGC (s->display, s->window, mask, &xgcv);
1109 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
1112 xassert (s->gc != 0);
1116 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
1117 Faces to use in the mode line have already been computed when the
1118 matrix was built, so there isn't much to do, here. */
1120 static INLINE void
1121 x_set_mode_line_face_gc (s)
1122 struct glyph_string *s;
1124 s->gc = s->face->gc;
1128 /* Set S->gc of glyph string S for drawing that glyph string. Set
1129 S->stippled_p to a non-zero value if the face of S has a stipple
1130 pattern. */
1132 static INLINE void
1133 x_set_glyph_string_gc (s)
1134 struct glyph_string *s;
1136 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
1138 if (s->hl == DRAW_NORMAL_TEXT)
1140 s->gc = s->face->gc;
1141 s->stippled_p = s->face->stipple != 0;
1143 else if (s->hl == DRAW_INVERSE_VIDEO)
1145 x_set_mode_line_face_gc (s);
1146 s->stippled_p = s->face->stipple != 0;
1148 else if (s->hl == DRAW_CURSOR)
1150 x_set_cursor_gc (s);
1151 s->stippled_p = 0;
1153 else if (s->hl == DRAW_MOUSE_FACE)
1155 x_set_mouse_face_gc (s);
1156 s->stippled_p = s->face->stipple != 0;
1158 else if (s->hl == DRAW_IMAGE_RAISED
1159 || s->hl == DRAW_IMAGE_SUNKEN)
1161 s->gc = s->face->gc;
1162 s->stippled_p = s->face->stipple != 0;
1164 else
1166 s->gc = s->face->gc;
1167 s->stippled_p = s->face->stipple != 0;
1170 /* GC must have been set. */
1171 xassert (s->gc != 0);
1175 /* Set clipping for output of glyph string S. S may be part of a mode
1176 line or menu if we don't have X toolkit support. */
1178 static INLINE void
1179 x_set_glyph_string_clipping (s)
1180 struct glyph_string *s;
1182 XRectangle r;
1183 get_glyph_string_clip_rect (s, &r);
1184 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
1185 #ifdef USE_FONT_BACKEND
1186 s->clip_x = r.x, s->clip_y = r.y;
1187 s->clip_width = r.width, s->clip_height = r.height;
1188 #endif /* USE_FONT_BACKEND */
1192 /* Set SRC's clipping for output of glyph string DST. This is called
1193 when we are drawing DST's left_overhang or right_overhang only in
1194 the area of SRC. */
1196 static void
1197 x_set_glyph_string_clipping_exactly (src, dst)
1198 struct glyph_string *src, *dst;
1200 XRectangle r;
1202 #ifdef USE_FONT_BACKEND
1203 if (enable_font_backend)
1205 r.x = dst->clip_x = src->x;
1206 r.width = dst->clip_width = src->width;
1207 r.y = dst->clip_y = src->y;
1208 r.height = dst->clip_height = src->height;
1210 else
1212 #endif /* USE_FONT_BACKEND */
1213 struct glyph_string *clip_head = src->clip_head;
1214 struct glyph_string *clip_tail = src->clip_tail;
1216 /* This foces clipping just this glyph string. */
1217 src->clip_head = src->clip_tail = src;
1218 get_glyph_string_clip_rect (src, &r);
1219 src->clip_head = clip_head, src->clip_tail = clip_tail;
1220 #ifdef USE_FONT_BACKEND
1222 #endif /* USE_FONT_BACKEND */
1223 XSetClipRectangles (dst->display, dst->gc, 0, 0, &r, 1, Unsorted);
1227 /* RIF:
1228 Compute left and right overhang of glyph string S. */
1230 static void
1231 x_compute_glyph_string_overhangs (s)
1232 struct glyph_string *s;
1234 if (s->cmp == NULL
1235 && s->first_glyph->type == CHAR_GLYPH)
1237 XCharStruct cs;
1238 int direction, font_ascent, font_descent;
1240 #ifdef USE_FONT_BACKEND
1241 if (enable_font_backend)
1243 unsigned *code = alloca (sizeof (unsigned) * s->nchars);
1244 struct font *font = (struct font *) s->font_info;
1245 struct font_metrics metrics;
1246 int i;
1248 for (i = 0; i < s->nchars; i++)
1249 code[i] = (s->char2b[i].byte1 << 8) | s->char2b[i].byte2;
1250 font->driver->text_extents (font, code, s->nchars, &metrics);
1251 cs.rbearing = metrics.rbearing;
1252 cs.lbearing = metrics.lbearing;
1253 cs.width = metrics.width;
1255 else
1256 #endif /* USE_FONT_BACKEND */
1257 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
1258 &font_ascent, &font_descent, &cs);
1259 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
1260 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
1262 else if (s->cmp)
1264 s->right_overhang = s->cmp->rbearing - s->cmp->pixel_width;
1265 s->left_overhang = - s->cmp->lbearing;
1270 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
1272 static INLINE void
1273 x_clear_glyph_string_rect (s, x, y, w, h)
1274 struct glyph_string *s;
1275 int x, y, w, h;
1277 XGCValues xgcv;
1278 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
1279 XSetForeground (s->display, s->gc, xgcv.background);
1280 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
1281 XSetForeground (s->display, s->gc, xgcv.foreground);
1285 /* Draw the background of glyph_string S. If S->background_filled_p
1286 is non-zero don't draw it. FORCE_P non-zero means draw the
1287 background even if it wouldn't be drawn normally. This is used
1288 when a string preceding S draws into the background of S, or S
1289 contains the first component of a composition. */
1291 static void
1292 x_draw_glyph_string_background (s, force_p)
1293 struct glyph_string *s;
1294 int force_p;
1296 /* Nothing to do if background has already been drawn or if it
1297 shouldn't be drawn in the first place. */
1298 if (!s->background_filled_p)
1300 int box_line_width = max (s->face->box_line_width, 0);
1302 if (s->stippled_p)
1304 /* Fill background with a stipple pattern. */
1305 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
1306 XFillRectangle (s->display, s->window, s->gc, s->x,
1307 s->y + box_line_width,
1308 s->background_width,
1309 s->height - 2 * box_line_width);
1310 XSetFillStyle (s->display, s->gc, FillSolid);
1311 s->background_filled_p = 1;
1313 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
1314 || s->font_not_found_p
1315 || s->extends_to_end_of_line_p
1316 || force_p)
1318 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
1319 s->background_width,
1320 s->height - 2 * box_line_width);
1321 s->background_filled_p = 1;
1327 /* Draw the foreground of glyph string S. */
1329 static void
1330 x_draw_glyph_string_foreground (s)
1331 struct glyph_string *s;
1333 int i, x;
1335 /* If first glyph of S has a left box line, start drawing the text
1336 of S to the right of that box line. */
1337 if (s->face->box != FACE_NO_BOX
1338 && s->first_glyph->left_box_line_p)
1339 x = s->x + abs (s->face->box_line_width);
1340 else
1341 x = s->x;
1343 /* Draw characters of S as rectangles if S's font could not be
1344 loaded. */
1345 if (s->font_not_found_p)
1347 for (i = 0; i < s->nchars; ++i)
1349 struct glyph *g = s->first_glyph + i;
1350 XDrawRectangle (s->display, s->window,
1351 s->gc, x, s->y, g->pixel_width - 1,
1352 s->height - 1);
1353 x += g->pixel_width;
1356 #ifdef USE_FONT_BACKEND
1357 else if (enable_font_backend)
1359 int boff = s->font_info->baseline_offset;
1360 struct font *font = (struct font *) s->font_info;
1361 int y;
1363 if (s->font_info->vertical_centering)
1364 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1366 y = s->ybase - boff;
1367 if (s->for_overlaps
1368 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1369 font->driver->draw (s, 0, s->nchars, x, y, 0);
1370 else
1371 font->driver->draw (s, 0, s->nchars, x, y, 1);
1372 if (s->face->overstrike)
1373 font->driver->draw (s, 0, s->nchars, x + 1, y, 0);
1375 #endif /* USE_FONT_BACKEND */
1376 else
1378 char *char1b = (char *) s->char2b;
1379 int boff = s->font_info->baseline_offset;
1381 if (s->font_info->vertical_centering)
1382 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
1384 /* If we can use 8-bit functions, condense S->char2b. */
1385 if (!s->two_byte_p)
1386 for (i = 0; i < s->nchars; ++i)
1387 char1b[i] = s->char2b[i].byte2;
1389 /* Draw text with XDrawString if background has already been
1390 filled. Otherwise, use XDrawImageString. (Note that
1391 XDrawImageString is usually faster than XDrawString.) Always
1392 use XDrawImageString when drawing the cursor so that there is
1393 no chance that characters under a box cursor are invisible. */
1394 if (s->for_overlaps
1395 || (s->background_filled_p && s->hl != DRAW_CURSOR))
1397 /* Draw characters with 16-bit or 8-bit functions. */
1398 if (s->two_byte_p)
1399 XDrawString16 (s->display, s->window, s->gc, x,
1400 s->ybase - boff, s->char2b, s->nchars);
1401 else
1402 XDrawString (s->display, s->window, s->gc, x,
1403 s->ybase - boff, char1b, s->nchars);
1405 else
1407 if (s->two_byte_p)
1408 XDrawImageString16 (s->display, s->window, s->gc, x,
1409 s->ybase - boff, s->char2b, s->nchars);
1410 else
1411 XDrawImageString (s->display, s->window, s->gc, x,
1412 s->ybase - boff, char1b, s->nchars);
1415 if (s->face->overstrike)
1417 /* For overstriking (to simulate bold-face), draw the
1418 characters again shifted to the right by one pixel. */
1419 if (s->two_byte_p)
1420 XDrawString16 (s->display, s->window, s->gc, x + 1,
1421 s->ybase - boff, s->char2b, s->nchars);
1422 else
1423 XDrawString (s->display, s->window, s->gc, x + 1,
1424 s->ybase - boff, char1b, s->nchars);
1429 /* Draw the foreground of composite glyph string S. */
1431 static void
1432 x_draw_composite_glyph_string_foreground (s)
1433 struct glyph_string *s;
1435 int i, j, x;
1437 /* If first glyph of S has a left box line, start drawing the text
1438 of S to the right of that box line. */
1439 if (s->face && s->face->box != FACE_NO_BOX
1440 && s->first_glyph->left_box_line_p)
1441 x = s->x + abs (s->face->box_line_width);
1442 else
1443 x = s->x;
1445 /* S is a glyph string for a composition. S->gidx is the index of
1446 the first character drawn for glyphs of this composition.
1447 S->gidx == 0 means we are drawing the very first character of
1448 this composition. */
1450 /* Draw a rectangle for the composition if the font for the very
1451 first character of the composition could not be loaded. */
1452 if (s->font_not_found_p)
1454 if (s->gidx == 0)
1455 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
1456 s->width - 1, s->height - 1);
1458 #ifdef USE_FONT_BACKEND
1459 else if (enable_font_backend)
1461 struct font *font = (struct font *) s->font_info;
1462 int y = s->ybase;
1463 int width = 0;
1465 if (s->cmp->method == COMPOSITION_WITH_GLYPH_STRING)
1467 Lisp_Object gstring = AREF (XHASH_TABLE (composition_hash_table)
1468 ->key_and_value,
1469 s->cmp->hash_index * 2);
1470 int from;
1472 for (i = from = 0; i < s->nchars; i++)
1474 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
1475 Lisp_Object adjustment = LGLYPH_ADJUSTMENT (g);
1476 int xoff, yoff, wadjust;
1478 if (! VECTORP (adjustment))
1480 width += XINT (LGLYPH_WIDTH (g));
1481 continue;
1483 if (from < i)
1485 font->driver->draw (s, from, i, x, y, 0);
1486 x += width;
1488 xoff = XINT (AREF (adjustment, 0));
1489 yoff = XINT (AREF (adjustment, 1));
1490 wadjust = XINT (AREF (adjustment, 2));
1492 font->driver->draw (s, i, i + 1, x + xoff, y + yoff, 0);
1493 x += XINT (LGLYPH_WIDTH (g)) + wadjust;
1494 from = i + 1;
1495 width = 0;
1497 if (from < i)
1498 font->driver->draw (s, from, i, x, y, 0);
1500 else
1502 for (i = 0, j = s->gidx; i < s->nchars; i++, j++)
1503 if (COMPOSITION_GLYPH (s->cmp, j) != '\t')
1505 int xx = x + s->cmp->offsets[j * 2];
1506 int yy = y - s->cmp->offsets[j * 2 + 1];
1508 font->driver->draw (s, j, j + 1, xx, yy, 0);
1509 if (s->face->overstrike)
1510 font->driver->draw (s, j, j + 1, xx + 1, yy, 0);
1514 #endif /* USE_FONT_BACKEND */
1515 else
1517 for (i = 0, j = s->gidx; i < s->nchars; i++, j++)
1518 if (s->face)
1520 XDrawString16 (s->display, s->window, s->gc,
1521 x + s->cmp->offsets[j * 2],
1522 s->ybase - s->cmp->offsets[j * 2 + 1],
1523 s->char2b + j, 1);
1524 if (s->face->overstrike)
1525 XDrawString16 (s->display, s->window, s->gc,
1526 x + s->cmp->offsets[j * 2] + 1,
1527 s->ybase - s->cmp->offsets[j * 2 + 1],
1528 s->char2b + j, 1);
1534 #ifdef USE_X_TOOLKIT
1536 static struct frame *x_frame_of_widget P_ ((Widget));
1537 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
1538 XrmValue *, XrmValue *, XtPointer *));
1539 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
1540 XrmValue *, Cardinal *));
1543 /* Return the frame on which widget WIDGET is used.. Abort if frame
1544 cannot be determined. */
1546 static struct frame *
1547 x_frame_of_widget (widget)
1548 Widget widget;
1550 struct x_display_info *dpyinfo;
1551 Lisp_Object tail;
1552 struct frame *f;
1554 dpyinfo = x_display_info_for_display (XtDisplay (widget));
1556 /* Find the top-level shell of the widget. Note that this function
1557 can be called when the widget is not yet realized, so XtWindow
1558 (widget) == 0. That's the reason we can't simply use
1559 x_any_window_to_frame. */
1560 while (!XtIsTopLevelShell (widget))
1561 widget = XtParent (widget);
1563 /* Look for a frame with that top-level widget. Allocate the color
1564 on that frame to get the right gamma correction value. */
1565 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
1566 if (FRAMEP (XCAR (tail))
1567 && (f = XFRAME (XCAR (tail)),
1568 (f->output_data.nothing != 1
1569 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
1570 && f->output_data.x->widget == widget)
1571 return f;
1573 abort ();
1577 /* Allocate the color COLOR->pixel on the screen and display of
1578 widget WIDGET in colormap CMAP. If an exact match cannot be
1579 allocated, try the nearest color available. Value is non-zero
1580 if successful. This is called from lwlib. */
1583 x_alloc_nearest_color_for_widget (widget, cmap, color)
1584 Widget widget;
1585 Colormap cmap;
1586 XColor *color;
1588 struct frame *f = x_frame_of_widget (widget);
1589 return x_alloc_nearest_color (f, cmap, color);
1593 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1594 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1595 If this produces the same color as PIXEL, try a color where all RGB
1596 values have DELTA added. Return the allocated color in *PIXEL.
1597 DISPLAY is the X display, CMAP is the colormap to operate on.
1598 Value is non-zero if successful. */
1601 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
1602 Widget widget;
1603 Display *display;
1604 Colormap cmap;
1605 unsigned long *pixel;
1606 double factor;
1607 int delta;
1609 struct frame *f = x_frame_of_widget (widget);
1610 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
1614 /* Structure specifying which arguments should be passed by Xt to
1615 cvt_string_to_pixel. We want the widget's screen and colormap. */
1617 static XtConvertArgRec cvt_string_to_pixel_args[] =
1619 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
1620 sizeof (Screen *)},
1621 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
1622 sizeof (Colormap)}
1626 /* The address of this variable is returned by
1627 cvt_string_to_pixel. */
1629 static Pixel cvt_string_to_pixel_value;
1632 /* Convert a color name to a pixel color.
1634 DPY is the display we are working on.
1636 ARGS is an array of *NARGS XrmValue structures holding additional
1637 information about the widget for which the conversion takes place.
1638 The contents of this array are determined by the specification
1639 in cvt_string_to_pixel_args.
1641 FROM is a pointer to an XrmValue which points to the color name to
1642 convert. TO is an XrmValue in which to return the pixel color.
1644 CLOSURE_RET is a pointer to user-data, in which we record if
1645 we allocated the color or not.
1647 Value is True if successful, False otherwise. */
1649 static Boolean
1650 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
1651 Display *dpy;
1652 XrmValue *args;
1653 Cardinal *nargs;
1654 XrmValue *from, *to;
1655 XtPointer *closure_ret;
1657 Screen *screen;
1658 Colormap cmap;
1659 Pixel pixel;
1660 String color_name;
1661 XColor color;
1663 if (*nargs != 2)
1665 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1666 "wrongParameters", "cvt_string_to_pixel",
1667 "XtToolkitError",
1668 "Screen and colormap args required", NULL, NULL);
1669 return False;
1672 screen = *(Screen **) args[0].addr;
1673 cmap = *(Colormap *) args[1].addr;
1674 color_name = (String) from->addr;
1676 if (strcmp (color_name, XtDefaultBackground) == 0)
1678 *closure_ret = (XtPointer) False;
1679 pixel = WhitePixelOfScreen (screen);
1681 else if (strcmp (color_name, XtDefaultForeground) == 0)
1683 *closure_ret = (XtPointer) False;
1684 pixel = BlackPixelOfScreen (screen);
1686 else if (XParseColor (dpy, cmap, color_name, &color)
1687 && x_alloc_nearest_color_1 (dpy, cmap, &color))
1689 pixel = color.pixel;
1690 *closure_ret = (XtPointer) True;
1692 else
1694 String params[1];
1695 Cardinal nparams = 1;
1697 params[0] = color_name;
1698 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
1699 "badValue", "cvt_string_to_pixel",
1700 "XtToolkitError", "Invalid color `%s'",
1701 params, &nparams);
1702 return False;
1705 if (to->addr != NULL)
1707 if (to->size < sizeof (Pixel))
1709 to->size = sizeof (Pixel);
1710 return False;
1713 *(Pixel *) to->addr = pixel;
1715 else
1717 cvt_string_to_pixel_value = pixel;
1718 to->addr = (XtPointer) &cvt_string_to_pixel_value;
1721 to->size = sizeof (Pixel);
1722 return True;
1726 /* Free a pixel color which was previously allocated via
1727 cvt_string_to_pixel. This is registered as the destructor
1728 for this type of resource via XtSetTypeConverter.
1730 APP is the application context in which we work.
1732 TO is a pointer to an XrmValue holding the color to free.
1733 CLOSURE is the value we stored in CLOSURE_RET for this color
1734 in cvt_string_to_pixel.
1736 ARGS and NARGS are like for cvt_string_to_pixel. */
1738 static void
1739 cvt_pixel_dtor (app, to, closure, args, nargs)
1740 XtAppContext app;
1741 XrmValuePtr to;
1742 XtPointer closure;
1743 XrmValuePtr args;
1744 Cardinal *nargs;
1746 if (*nargs != 2)
1748 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
1749 "XtToolkitError",
1750 "Screen and colormap arguments required",
1751 NULL, NULL);
1753 else if (closure != NULL)
1755 /* We did allocate the pixel, so free it. */
1756 Screen *screen = *(Screen **) args[0].addr;
1757 Colormap cmap = *(Colormap *) args[1].addr;
1758 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
1759 (Pixel *) to->addr, 1);
1764 #endif /* USE_X_TOOLKIT */
1767 /* Value is an array of XColor structures for the contents of the
1768 color map of display DPY. Set *NCELLS to the size of the array.
1769 Note that this probably shouldn't be called for large color maps,
1770 say a 24-bit TrueColor map. */
1772 static const XColor *
1773 x_color_cells (dpy, ncells)
1774 Display *dpy;
1775 int *ncells;
1777 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1779 if (dpyinfo->color_cells == NULL)
1781 Screen *screen = dpyinfo->screen;
1782 int i;
1784 dpyinfo->ncolor_cells
1785 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
1786 dpyinfo->color_cells
1787 = (XColor *) xmalloc (dpyinfo->ncolor_cells
1788 * sizeof *dpyinfo->color_cells);
1790 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
1791 dpyinfo->color_cells[i].pixel = i;
1793 XQueryColors (dpy, dpyinfo->cmap,
1794 dpyinfo->color_cells, dpyinfo->ncolor_cells);
1797 *ncells = dpyinfo->ncolor_cells;
1798 return dpyinfo->color_cells;
1802 /* On frame F, translate pixel colors to RGB values for the NCOLORS
1803 colors in COLORS. Use cached information, if available. */
1805 void
1806 x_query_colors (f, colors, ncolors)
1807 struct frame *f;
1808 XColor *colors;
1809 int ncolors;
1811 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1813 if (dpyinfo->color_cells)
1815 int i;
1816 for (i = 0; i < ncolors; ++i)
1818 unsigned long pixel = colors[i].pixel;
1819 xassert (pixel < dpyinfo->ncolor_cells);
1820 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
1821 colors[i] = dpyinfo->color_cells[pixel];
1824 else
1825 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
1829 /* On frame F, translate pixel color to RGB values for the color in
1830 COLOR. Use cached information, if available. */
1832 void
1833 x_query_color (f, color)
1834 struct frame *f;
1835 XColor *color;
1837 x_query_colors (f, color, 1);
1841 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
1842 exact match can't be allocated, try the nearest color available.
1843 Value is non-zero if successful. Set *COLOR to the color
1844 allocated. */
1846 static int
1847 x_alloc_nearest_color_1 (dpy, cmap, color)
1848 Display *dpy;
1849 Colormap cmap;
1850 XColor *color;
1852 int rc;
1854 rc = XAllocColor (dpy, cmap, color);
1855 if (rc == 0)
1857 /* If we got to this point, the colormap is full, so we're going
1858 to try to get the next closest color. The algorithm used is
1859 a least-squares matching, which is what X uses for closest
1860 color matching with StaticColor visuals. */
1861 int nearest, i;
1862 unsigned long nearest_delta = ~0;
1863 int ncells;
1864 const XColor *cells = x_color_cells (dpy, &ncells);
1866 for (nearest = i = 0; i < ncells; ++i)
1868 long dred = (color->red >> 8) - (cells[i].red >> 8);
1869 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
1870 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
1871 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
1873 if (delta < nearest_delta)
1875 nearest = i;
1876 nearest_delta = delta;
1880 color->red = cells[nearest].red;
1881 color->green = cells[nearest].green;
1882 color->blue = cells[nearest].blue;
1883 rc = XAllocColor (dpy, cmap, color);
1885 else
1887 /* If allocation succeeded, and the allocated pixel color is not
1888 equal to a cached pixel color recorded earlier, there was a
1889 change in the colormap, so clear the color cache. */
1890 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
1891 XColor *cached_color;
1893 if (dpyinfo->color_cells
1894 && (cached_color = &dpyinfo->color_cells[color->pixel],
1895 (cached_color->red != color->red
1896 || cached_color->blue != color->blue
1897 || cached_color->green != color->green)))
1899 xfree (dpyinfo->color_cells);
1900 dpyinfo->color_cells = NULL;
1901 dpyinfo->ncolor_cells = 0;
1905 #ifdef DEBUG_X_COLORS
1906 if (rc)
1907 register_color (color->pixel);
1908 #endif /* DEBUG_X_COLORS */
1910 return rc;
1914 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
1915 exact match can't be allocated, try the nearest color available.
1916 Value is non-zero if successful. Set *COLOR to the color
1917 allocated. */
1920 x_alloc_nearest_color (f, cmap, color)
1921 struct frame *f;
1922 Colormap cmap;
1923 XColor *color;
1925 gamma_correct (f, color);
1926 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
1930 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
1931 It's necessary to do this instead of just using PIXEL directly to
1932 get color reference counts right. */
1934 unsigned long
1935 x_copy_color (f, pixel)
1936 struct frame *f;
1937 unsigned long pixel;
1939 XColor color;
1941 color.pixel = pixel;
1942 BLOCK_INPUT;
1943 x_query_color (f, &color);
1944 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
1945 UNBLOCK_INPUT;
1946 #ifdef DEBUG_X_COLORS
1947 register_color (pixel);
1948 #endif
1949 return color.pixel;
1953 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
1954 It's necessary to do this instead of just using PIXEL directly to
1955 get color reference counts right. */
1957 unsigned long
1958 x_copy_dpy_color (dpy, cmap, pixel)
1959 Display *dpy;
1960 Colormap cmap;
1961 unsigned long pixel;
1963 XColor color;
1965 color.pixel = pixel;
1966 BLOCK_INPUT;
1967 XQueryColor (dpy, cmap, &color);
1968 XAllocColor (dpy, cmap, &color);
1969 UNBLOCK_INPUT;
1970 #ifdef DEBUG_X_COLORS
1971 register_color (pixel);
1972 #endif
1973 return color.pixel;
1977 /* Brightness beyond which a color won't have its highlight brightness
1978 boosted.
1980 Nominally, highlight colors for `3d' faces are calculated by
1981 brightening an object's color by a constant scale factor, but this
1982 doesn't yield good results for dark colors, so for colors who's
1983 brightness is less than this value (on a scale of 0-65535) have an
1984 use an additional additive factor.
1986 The value here is set so that the default menu-bar/mode-line color
1987 (grey75) will not have its highlights changed at all. */
1988 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
1991 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
1992 or DELTA. Try a color with RGB values multiplied by FACTOR first.
1993 If this produces the same color as PIXEL, try a color where all RGB
1994 values have DELTA added. Return the allocated color in *PIXEL.
1995 DISPLAY is the X display, CMAP is the colormap to operate on.
1996 Value is non-zero if successful. */
1998 static int
1999 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
2000 struct frame *f;
2001 Display *display;
2002 Colormap cmap;
2003 unsigned long *pixel;
2004 double factor;
2005 int delta;
2007 XColor color, new;
2008 long bright;
2009 int success_p;
2011 /* Get RGB color values. */
2012 color.pixel = *pixel;
2013 x_query_color (f, &color);
2015 /* Change RGB values by specified FACTOR. Avoid overflow! */
2016 xassert (factor >= 0);
2017 new.red = min (0xffff, factor * color.red);
2018 new.green = min (0xffff, factor * color.green);
2019 new.blue = min (0xffff, factor * color.blue);
2021 /* Calculate brightness of COLOR. */
2022 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
2024 /* We only boost colors that are darker than
2025 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
2026 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
2027 /* Make an additive adjustment to NEW, because it's dark enough so
2028 that scaling by FACTOR alone isn't enough. */
2030 /* How far below the limit this color is (0 - 1, 1 being darker). */
2031 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
2032 /* The additive adjustment. */
2033 int min_delta = delta * dimness * factor / 2;
2035 if (factor < 1)
2037 new.red = max (0, new.red - min_delta);
2038 new.green = max (0, new.green - min_delta);
2039 new.blue = max (0, new.blue - min_delta);
2041 else
2043 new.red = min (0xffff, min_delta + new.red);
2044 new.green = min (0xffff, min_delta + new.green);
2045 new.blue = min (0xffff, min_delta + new.blue);
2049 /* Try to allocate the color. */
2050 success_p = x_alloc_nearest_color (f, cmap, &new);
2051 if (success_p)
2053 if (new.pixel == *pixel)
2055 /* If we end up with the same color as before, try adding
2056 delta to the RGB values. */
2057 x_free_colors (f, &new.pixel, 1);
2059 new.red = min (0xffff, delta + color.red);
2060 new.green = min (0xffff, delta + color.green);
2061 new.blue = min (0xffff, delta + color.blue);
2062 success_p = x_alloc_nearest_color (f, cmap, &new);
2064 else
2065 success_p = 1;
2066 *pixel = new.pixel;
2069 return success_p;
2073 /* Set up the foreground color for drawing relief lines of glyph
2074 string S. RELIEF is a pointer to a struct relief containing the GC
2075 with which lines will be drawn. Use a color that is FACTOR or
2076 DELTA lighter or darker than the relief's background which is found
2077 in S->f->output_data.x->relief_background. If such a color cannot
2078 be allocated, use DEFAULT_PIXEL, instead. */
2080 static void
2081 x_setup_relief_color (f, relief, factor, delta, default_pixel)
2082 struct frame *f;
2083 struct relief *relief;
2084 double factor;
2085 int delta;
2086 unsigned long default_pixel;
2088 XGCValues xgcv;
2089 struct x_output *di = f->output_data.x;
2090 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
2091 unsigned long pixel;
2092 unsigned long background = di->relief_background;
2093 Colormap cmap = FRAME_X_COLORMAP (f);
2094 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2095 Display *dpy = FRAME_X_DISPLAY (f);
2097 xgcv.graphics_exposures = False;
2098 xgcv.line_width = 1;
2100 /* Free previously allocated color. The color cell will be reused
2101 when it has been freed as many times as it was allocated, so this
2102 doesn't affect faces using the same colors. */
2103 if (relief->gc
2104 && relief->allocated_p)
2106 x_free_colors (f, &relief->pixel, 1);
2107 relief->allocated_p = 0;
2110 /* Allocate new color. */
2111 xgcv.foreground = default_pixel;
2112 pixel = background;
2113 if (dpyinfo->n_planes != 1
2114 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
2116 relief->allocated_p = 1;
2117 xgcv.foreground = relief->pixel = pixel;
2120 if (relief->gc == 0)
2122 xgcv.stipple = dpyinfo->gray;
2123 mask |= GCStipple;
2124 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
2126 else
2127 XChangeGC (dpy, relief->gc, mask, &xgcv);
2131 /* Set up colors for the relief lines around glyph string S. */
2133 static void
2134 x_setup_relief_colors (s)
2135 struct glyph_string *s;
2137 struct x_output *di = s->f->output_data.x;
2138 unsigned long color;
2140 if (s->face->use_box_color_for_shadows_p)
2141 color = s->face->box_color;
2142 else if (s->first_glyph->type == IMAGE_GLYPH
2143 && s->img->pixmap
2144 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
2145 color = IMAGE_BACKGROUND (s->img, s->f, 0);
2146 else
2148 XGCValues xgcv;
2150 /* Get the background color of the face. */
2151 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
2152 color = xgcv.background;
2155 if (di->white_relief.gc == 0
2156 || color != di->relief_background)
2158 di->relief_background = color;
2159 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
2160 WHITE_PIX_DEFAULT (s->f));
2161 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
2162 BLACK_PIX_DEFAULT (s->f));
2167 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
2168 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
2169 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
2170 relief. LEFT_P non-zero means draw a relief on the left side of
2171 the rectangle. RIGHT_P non-zero means draw a relief on the right
2172 side of the rectangle. CLIP_RECT is the clipping rectangle to use
2173 when drawing. */
2175 static void
2176 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
2177 raised_p, top_p, bot_p, left_p, right_p, clip_rect)
2178 struct frame *f;
2179 int left_x, top_y, right_x, bottom_y, width;
2180 int top_p, bot_p, left_p, right_p, raised_p;
2181 XRectangle *clip_rect;
2183 Display *dpy = FRAME_X_DISPLAY (f);
2184 Window window = FRAME_X_WINDOW (f);
2185 int i;
2186 GC gc;
2188 if (raised_p)
2189 gc = f->output_data.x->white_relief.gc;
2190 else
2191 gc = f->output_data.x->black_relief.gc;
2192 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2194 /* Top. */
2195 if (top_p)
2196 for (i = 0; i < width; ++i)
2197 XDrawLine (dpy, window, gc,
2198 left_x + i * left_p, top_y + i,
2199 right_x + 1 - i * right_p, top_y + i);
2201 /* Left. */
2202 if (left_p)
2203 for (i = 0; i < width; ++i)
2204 XDrawLine (dpy, window, gc,
2205 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
2207 XSetClipMask (dpy, gc, None);
2208 if (raised_p)
2209 gc = f->output_data.x->black_relief.gc;
2210 else
2211 gc = f->output_data.x->white_relief.gc;
2212 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
2214 /* Bottom. */
2215 if (bot_p)
2216 for (i = 0; i < width; ++i)
2217 XDrawLine (dpy, window, gc,
2218 left_x + i * left_p, bottom_y - i,
2219 right_x + 1 - i * right_p, bottom_y - i);
2221 /* Right. */
2222 if (right_p)
2223 for (i = 0; i < width; ++i)
2224 XDrawLine (dpy, window, gc,
2225 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
2227 XSetClipMask (dpy, gc, None);
2231 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
2232 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
2233 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
2234 left side of the rectangle. RIGHT_P non-zero means draw a line
2235 on the right side of the rectangle. CLIP_RECT is the clipping
2236 rectangle to use when drawing. */
2238 static void
2239 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2240 left_p, right_p, clip_rect)
2241 struct glyph_string *s;
2242 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
2243 XRectangle *clip_rect;
2245 XGCValues xgcv;
2247 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2248 XSetForeground (s->display, s->gc, s->face->box_color);
2249 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
2251 /* Top. */
2252 XFillRectangle (s->display, s->window, s->gc,
2253 left_x, top_y, right_x - left_x + 1, width);
2255 /* Left. */
2256 if (left_p)
2257 XFillRectangle (s->display, s->window, s->gc,
2258 left_x, top_y, width, bottom_y - top_y + 1);
2260 /* Bottom. */
2261 XFillRectangle (s->display, s->window, s->gc,
2262 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
2264 /* Right. */
2265 if (right_p)
2266 XFillRectangle (s->display, s->window, s->gc,
2267 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
2269 XSetForeground (s->display, s->gc, xgcv.foreground);
2270 XSetClipMask (s->display, s->gc, None);
2274 /* Draw a box around glyph string S. */
2276 static void
2277 x_draw_glyph_string_box (s)
2278 struct glyph_string *s;
2280 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
2281 int left_p, right_p;
2282 struct glyph *last_glyph;
2283 XRectangle clip_rect;
2285 last_x = ((s->row->full_width_p && !s->w->pseudo_window_p)
2286 ? WINDOW_RIGHT_EDGE_X (s->w)
2287 : window_box_right (s->w, s->area));
2289 /* The glyph that may have a right box line. */
2290 last_glyph = (s->cmp || s->img
2291 ? s->first_glyph
2292 : s->first_glyph + s->nchars - 1);
2294 width = abs (s->face->box_line_width);
2295 raised_p = s->face->box == FACE_RAISED_BOX;
2296 left_x = s->x;
2297 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
2298 ? last_x - 1
2299 : min (last_x, s->x + s->background_width) - 1);
2300 top_y = s->y;
2301 bottom_y = top_y + s->height - 1;
2303 left_p = (s->first_glyph->left_box_line_p
2304 || (s->hl == DRAW_MOUSE_FACE
2305 && (s->prev == NULL
2306 || s->prev->hl != s->hl)));
2307 right_p = (last_glyph->right_box_line_p
2308 || (s->hl == DRAW_MOUSE_FACE
2309 && (s->next == NULL
2310 || s->next->hl != s->hl)));
2312 get_glyph_string_clip_rect (s, &clip_rect);
2314 if (s->face->box == FACE_SIMPLE_BOX)
2315 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
2316 left_p, right_p, &clip_rect);
2317 else
2319 x_setup_relief_colors (s);
2320 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
2321 width, raised_p, 1, 1, left_p, right_p, &clip_rect);
2326 /* Draw foreground of image glyph string S. */
2328 static void
2329 x_draw_image_foreground (s)
2330 struct glyph_string *s;
2332 int x = s->x;
2333 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2335 /* If first glyph of S has a left box line, start drawing it to the
2336 right of that line. */
2337 if (s->face->box != FACE_NO_BOX
2338 && s->first_glyph->left_box_line_p
2339 && s->slice.x == 0)
2340 x += abs (s->face->box_line_width);
2342 /* If there is a margin around the image, adjust x- and y-position
2343 by that margin. */
2344 if (s->slice.x == 0)
2345 x += s->img->hmargin;
2346 if (s->slice.y == 0)
2347 y += s->img->vmargin;
2349 if (s->img->pixmap)
2351 if (s->img->mask)
2353 /* We can't set both a clip mask and use XSetClipRectangles
2354 because the latter also sets a clip mask. We also can't
2355 trust on the shape extension to be available
2356 (XShapeCombineRegion). So, compute the rectangle to draw
2357 manually. */
2358 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2359 | GCFunction);
2360 XGCValues xgcv;
2361 XRectangle clip_rect, image_rect, r;
2363 xgcv.clip_mask = s->img->mask;
2364 xgcv.clip_x_origin = x;
2365 xgcv.clip_y_origin = y;
2366 xgcv.function = GXcopy;
2367 XChangeGC (s->display, s->gc, mask, &xgcv);
2369 get_glyph_string_clip_rect (s, &clip_rect);
2370 image_rect.x = x;
2371 image_rect.y = y;
2372 image_rect.width = s->slice.width;
2373 image_rect.height = s->slice.height;
2374 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2375 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2376 s->slice.x + r.x - x, s->slice.y + r.y - y,
2377 r.width, r.height, r.x, r.y);
2379 else
2381 XRectangle clip_rect, image_rect, r;
2383 get_glyph_string_clip_rect (s, &clip_rect);
2384 image_rect.x = x;
2385 image_rect.y = y;
2386 image_rect.width = s->slice.width;
2387 image_rect.height = s->slice.height;
2388 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
2389 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
2390 s->slice.x + r.x - x, s->slice.y + r.y - y,
2391 r.width, r.height, r.x, r.y);
2393 /* When the image has a mask, we can expect that at
2394 least part of a mouse highlight or a block cursor will
2395 be visible. If the image doesn't have a mask, make
2396 a block cursor visible by drawing a rectangle around
2397 the image. I believe it's looking better if we do
2398 nothing here for mouse-face. */
2399 if (s->hl == DRAW_CURSOR)
2401 int r = s->img->relief;
2402 if (r < 0) r = -r;
2403 XDrawRectangle (s->display, s->window, s->gc,
2404 x - r, y - r,
2405 s->slice.width + r*2 - 1,
2406 s->slice.height + r*2 - 1);
2410 else
2411 /* Draw a rectangle if image could not be loaded. */
2412 XDrawRectangle (s->display, s->window, s->gc, x, y,
2413 s->slice.width - 1, s->slice.height - 1);
2417 /* Draw a relief around the image glyph string S. */
2419 static void
2420 x_draw_image_relief (s)
2421 struct glyph_string *s;
2423 int x0, y0, x1, y1, thick, raised_p;
2424 XRectangle r;
2425 int x = s->x;
2426 int y = s->ybase - image_ascent (s->img, s->face, &s->slice);
2428 /* If first glyph of S has a left box line, start drawing it to the
2429 right of that line. */
2430 if (s->face->box != FACE_NO_BOX
2431 && s->first_glyph->left_box_line_p
2432 && s->slice.x == 0)
2433 x += abs (s->face->box_line_width);
2435 /* If there is a margin around the image, adjust x- and y-position
2436 by that margin. */
2437 if (s->slice.x == 0)
2438 x += s->img->hmargin;
2439 if (s->slice.y == 0)
2440 y += s->img->vmargin;
2442 if (s->hl == DRAW_IMAGE_SUNKEN
2443 || s->hl == DRAW_IMAGE_RAISED)
2445 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
2446 raised_p = s->hl == DRAW_IMAGE_RAISED;
2448 else
2450 thick = abs (s->img->relief);
2451 raised_p = s->img->relief > 0;
2454 x0 = x - thick;
2455 y0 = y - thick;
2456 x1 = x + s->slice.width + thick - 1;
2457 y1 = y + s->slice.height + thick - 1;
2459 x_setup_relief_colors (s);
2460 get_glyph_string_clip_rect (s, &r);
2461 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p,
2462 s->slice.y == 0,
2463 s->slice.y + s->slice.height == s->img->height,
2464 s->slice.x == 0,
2465 s->slice.x + s->slice.width == s->img->width,
2466 &r);
2470 /* Draw the foreground of image glyph string S to PIXMAP. */
2472 static void
2473 x_draw_image_foreground_1 (s, pixmap)
2474 struct glyph_string *s;
2475 Pixmap pixmap;
2477 int x = 0;
2478 int y = s->ybase - s->y - image_ascent (s->img, s->face, &s->slice);
2480 /* If first glyph of S has a left box line, start drawing it to the
2481 right of that line. */
2482 if (s->face->box != FACE_NO_BOX
2483 && s->first_glyph->left_box_line_p
2484 && s->slice.x == 0)
2485 x += abs (s->face->box_line_width);
2487 /* If there is a margin around the image, adjust x- and y-position
2488 by that margin. */
2489 if (s->slice.x == 0)
2490 x += s->img->hmargin;
2491 if (s->slice.y == 0)
2492 y += s->img->vmargin;
2494 if (s->img->pixmap)
2496 if (s->img->mask)
2498 /* We can't set both a clip mask and use XSetClipRectangles
2499 because the latter also sets a clip mask. We also can't
2500 trust on the shape extension to be available
2501 (XShapeCombineRegion). So, compute the rectangle to draw
2502 manually. */
2503 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
2504 | GCFunction);
2505 XGCValues xgcv;
2507 xgcv.clip_mask = s->img->mask;
2508 xgcv.clip_x_origin = x - s->slice.x;
2509 xgcv.clip_y_origin = y - s->slice.y;
2510 xgcv.function = GXcopy;
2511 XChangeGC (s->display, s->gc, mask, &xgcv);
2513 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2514 s->slice.x, s->slice.y,
2515 s->slice.width, s->slice.height, x, y);
2516 XSetClipMask (s->display, s->gc, None);
2518 else
2520 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
2521 s->slice.x, s->slice.y,
2522 s->slice.width, s->slice.height, x, y);
2524 /* When the image has a mask, we can expect that at
2525 least part of a mouse highlight or a block cursor will
2526 be visible. If the image doesn't have a mask, make
2527 a block cursor visible by drawing a rectangle around
2528 the image. I believe it's looking better if we do
2529 nothing here for mouse-face. */
2530 if (s->hl == DRAW_CURSOR)
2532 int r = s->img->relief;
2533 if (r < 0) r = -r;
2534 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
2535 s->slice.width + r*2 - 1,
2536 s->slice.height + r*2 - 1);
2540 else
2541 /* Draw a rectangle if image could not be loaded. */
2542 XDrawRectangle (s->display, pixmap, s->gc, x, y,
2543 s->slice.width - 1, s->slice.height - 1);
2547 /* Draw part of the background of glyph string S. X, Y, W, and H
2548 give the rectangle to draw. */
2550 static void
2551 x_draw_glyph_string_bg_rect (s, x, y, w, h)
2552 struct glyph_string *s;
2553 int x, y, w, h;
2555 if (s->stippled_p)
2557 /* Fill background with a stipple pattern. */
2558 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2559 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2560 XSetFillStyle (s->display, s->gc, FillSolid);
2562 else
2563 x_clear_glyph_string_rect (s, x, y, w, h);
2567 /* Draw image glyph string S.
2569 s->y
2570 s->x +-------------------------
2571 | s->face->box
2573 | +-------------------------
2574 | | s->img->margin
2576 | | +-------------------
2577 | | | the image
2581 static void
2582 x_draw_image_glyph_string (s)
2583 struct glyph_string *s;
2585 int box_line_hwidth = abs (s->face->box_line_width);
2586 int box_line_vwidth = max (s->face->box_line_width, 0);
2587 int height;
2588 Pixmap pixmap = None;
2590 height = s->height;
2591 if (s->slice.y == 0)
2592 height -= box_line_vwidth;
2593 if (s->slice.y + s->slice.height >= s->img->height)
2594 height -= box_line_vwidth;
2596 /* Fill background with face under the image. Do it only if row is
2597 taller than image or if image has a clip mask to reduce
2598 flickering. */
2599 s->stippled_p = s->face->stipple != 0;
2600 if (height > s->slice.height
2601 || s->img->hmargin
2602 || s->img->vmargin
2603 || s->img->mask
2604 || s->img->pixmap == 0
2605 || s->width != s->background_width)
2607 if (s->img->mask)
2609 /* Create a pixmap as large as the glyph string. Fill it
2610 with the background color. Copy the image to it, using
2611 its mask. Copy the temporary pixmap to the display. */
2612 Screen *screen = FRAME_X_SCREEN (s->f);
2613 int depth = DefaultDepthOfScreen (screen);
2615 /* Create a pixmap as large as the glyph string. */
2616 pixmap = XCreatePixmap (s->display, s->window,
2617 s->background_width,
2618 s->height, depth);
2620 /* Don't clip in the following because we're working on the
2621 pixmap. */
2622 XSetClipMask (s->display, s->gc, None);
2624 /* Fill the pixmap with the background color/stipple. */
2625 if (s->stippled_p)
2627 /* Fill background with a stipple pattern. */
2628 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2629 XFillRectangle (s->display, pixmap, s->gc,
2630 0, 0, s->background_width, s->height);
2631 XSetFillStyle (s->display, s->gc, FillSolid);
2633 else
2635 XGCValues xgcv;
2636 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
2637 &xgcv);
2638 XSetForeground (s->display, s->gc, xgcv.background);
2639 XFillRectangle (s->display, pixmap, s->gc,
2640 0, 0, s->background_width, s->height);
2641 XSetForeground (s->display, s->gc, xgcv.foreground);
2644 else
2646 int x = s->x;
2647 int y = s->y;
2649 if (s->first_glyph->left_box_line_p
2650 && s->slice.x == 0)
2651 x += box_line_hwidth;
2653 if (s->slice.y == 0)
2654 y += box_line_vwidth;
2656 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
2659 s->background_filled_p = 1;
2662 /* Draw the foreground. */
2663 if (pixmap != None)
2665 x_draw_image_foreground_1 (s, pixmap);
2666 x_set_glyph_string_clipping (s);
2667 XCopyArea (s->display, pixmap, s->window, s->gc,
2668 0, 0, s->background_width, s->height, s->x, s->y);
2669 XFreePixmap (s->display, pixmap);
2671 else
2672 x_draw_image_foreground (s);
2674 /* If we must draw a relief around the image, do it. */
2675 if (s->img->relief
2676 || s->hl == DRAW_IMAGE_RAISED
2677 || s->hl == DRAW_IMAGE_SUNKEN)
2678 x_draw_image_relief (s);
2682 /* Draw stretch glyph string S. */
2684 static void
2685 x_draw_stretch_glyph_string (s)
2686 struct glyph_string *s;
2688 xassert (s->first_glyph->type == STRETCH_GLYPH);
2689 s->stippled_p = s->face->stipple != 0;
2691 if (s->hl == DRAW_CURSOR
2692 && !x_stretch_cursor_p)
2694 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
2695 as wide as the stretch glyph. */
2696 int width, background_width = s->background_width;
2697 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2699 if (x < left_x)
2701 background_width -= left_x - x;
2702 x = left_x;
2704 width = min (FRAME_COLUMN_WIDTH (s->f), background_width);
2706 /* Draw cursor. */
2707 x_draw_glyph_string_bg_rect (s, x, s->y, width, s->height);
2709 /* Clear rest using the GC of the original non-cursor face. */
2710 if (width < background_width)
2712 int y = s->y;
2713 int w = background_width - width, h = s->height;
2714 XRectangle r;
2715 GC gc;
2717 x += width;
2718 if (s->row->mouse_face_p
2719 && cursor_in_mouse_face_p (s->w))
2721 x_set_mouse_face_gc (s);
2722 gc = s->gc;
2724 else
2725 gc = s->face->gc;
2727 get_glyph_string_clip_rect (s, &r);
2728 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
2730 if (s->face->stipple)
2732 /* Fill background with a stipple pattern. */
2733 XSetFillStyle (s->display, gc, FillOpaqueStippled);
2734 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2735 XSetFillStyle (s->display, gc, FillSolid);
2737 else
2739 XGCValues xgcv;
2740 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
2741 XSetForeground (s->display, gc, xgcv.background);
2742 XFillRectangle (s->display, s->window, gc, x, y, w, h);
2743 XSetForeground (s->display, gc, xgcv.foreground);
2747 else if (!s->background_filled_p)
2749 int background_width = s->background_width;
2750 int x = s->x, left_x = window_box_left_offset (s->w, TEXT_AREA);
2752 /* Don't draw into left margin, fringe or scrollbar area
2753 except for header line and mode line. */
2754 if (x < left_x && !s->row->mode_line_p)
2756 background_width -= left_x - x;
2757 x = left_x;
2759 if (background_width > 0)
2760 x_draw_glyph_string_bg_rect (s, x, s->y, background_width, s->height);
2763 s->background_filled_p = 1;
2767 /* Draw glyph string S. */
2769 static void
2770 x_draw_glyph_string (s)
2771 struct glyph_string *s;
2773 int relief_drawn_p = 0;
2775 /* If S draws into the background of its successors, draw the
2776 background of the successors first so that S can draw into it.
2777 This makes S->next use XDrawString instead of XDrawImageString. */
2778 if (s->next && s->right_overhang && !s->for_overlaps)
2780 int width;
2781 struct glyph_string *next;
2783 for (width = 0, next = s->next; next;
2784 width += next->width, next = next->next)
2785 if (next->first_glyph->type != IMAGE_GLYPH)
2787 x_set_glyph_string_gc (next);
2788 x_set_glyph_string_clipping (next);
2789 x_draw_glyph_string_background (next, 1);
2790 #ifdef USE_FONT_BACKEND
2791 next->clip_width = 0;
2792 #endif /* USE_FONT_BACKEND */
2796 /* Set up S->gc, set clipping and draw S. */
2797 x_set_glyph_string_gc (s);
2799 /* Draw relief (if any) in advance for char/composition so that the
2800 glyph string can be drawn over it. */
2801 if (!s->for_overlaps
2802 && s->face->box != FACE_NO_BOX
2803 && (s->first_glyph->type == CHAR_GLYPH
2804 || s->first_glyph->type == COMPOSITE_GLYPH))
2807 x_set_glyph_string_clipping (s);
2808 x_draw_glyph_string_background (s, 1);
2809 x_draw_glyph_string_box (s);
2810 x_set_glyph_string_clipping (s);
2811 relief_drawn_p = 1;
2813 else if ((s->prev && s->prev->hl != s->hl && s->left_overhang)
2814 || (s->next && s->next->hl != s->hl && s->right_overhang))
2815 /* We must clip just this glyph. left_overhang part has already
2816 drawn when s->prev was drawn, and right_overhang part will be
2817 drawn later when s->next is drawn. */
2818 x_set_glyph_string_clipping_exactly (s, s);
2819 else
2820 x_set_glyph_string_clipping (s);
2822 switch (s->first_glyph->type)
2824 case IMAGE_GLYPH:
2825 x_draw_image_glyph_string (s);
2826 break;
2828 case STRETCH_GLYPH:
2829 x_draw_stretch_glyph_string (s);
2830 break;
2832 case CHAR_GLYPH:
2833 if (s->for_overlaps)
2834 s->background_filled_p = 1;
2835 else
2836 x_draw_glyph_string_background (s, 0);
2837 x_draw_glyph_string_foreground (s);
2838 break;
2840 case COMPOSITE_GLYPH:
2841 if (s->for_overlaps || s->gidx > 0)
2842 s->background_filled_p = 1;
2843 else
2844 x_draw_glyph_string_background (s, 1);
2845 x_draw_composite_glyph_string_foreground (s);
2846 break;
2848 default:
2849 abort ();
2852 if (!s->for_overlaps)
2854 /* Draw underline. */
2855 if (s->face->underline_p)
2857 unsigned long tem, h;
2858 int y;
2860 /* Get the underline thickness. Default is 1 pixel. */
2861 #ifdef USE_FONT_BACKEND
2862 if (enable_font_backend)
2863 /* In the future, we must use information of font. */
2864 h = 1;
2865 else
2866 #endif /* USE_FONT_BACKEND */
2867 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
2868 h = 1;
2870 #ifdef USE_FONT_BACKEND
2871 if (enable_font_backend)
2873 if (s->face->font)
2874 /* In the future, we must use information of font. */
2875 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2876 else
2877 y = s->y + s->height - h;
2879 else
2880 #endif
2882 y = s->y + s->height - h;
2883 if (!x_underline_at_descent_line)
2885 /* Get the underline position. This is the recommended
2886 vertical offset in pixels from the baseline to the top of
2887 the underline. This is a signed value according to the
2888 specs, and its default is
2890 ROUND ((maximum descent) / 2), with
2891 ROUND(x) = floor (x + 0.5) */
2893 if (x_use_underline_position_properties
2894 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
2895 y = s->ybase + (long) tem;
2896 else if (s->face->font)
2897 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
2901 if (s->face->underline_defaulted_p)
2902 XFillRectangle (s->display, s->window, s->gc,
2903 s->x, y, s->background_width, h);
2904 else
2906 XGCValues xgcv;
2907 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2908 XSetForeground (s->display, s->gc, s->face->underline_color);
2909 XFillRectangle (s->display, s->window, s->gc,
2910 s->x, y, s->background_width, h);
2911 XSetForeground (s->display, s->gc, xgcv.foreground);
2915 /* Draw overline. */
2916 if (s->face->overline_p)
2918 unsigned long dy = 0, h = 1;
2920 if (s->face->overline_color_defaulted_p)
2921 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2922 s->background_width, h);
2923 else
2925 XGCValues xgcv;
2926 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2927 XSetForeground (s->display, s->gc, s->face->overline_color);
2928 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2929 s->background_width, h);
2930 XSetForeground (s->display, s->gc, xgcv.foreground);
2934 /* Draw strike-through. */
2935 if (s->face->strike_through_p)
2937 unsigned long h = 1;
2938 unsigned long dy = (s->height - h) / 2;
2940 if (s->face->strike_through_color_defaulted_p)
2941 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2942 s->width, h);
2943 else
2945 XGCValues xgcv;
2946 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
2947 XSetForeground (s->display, s->gc, s->face->strike_through_color);
2948 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
2949 s->width, h);
2950 XSetForeground (s->display, s->gc, xgcv.foreground);
2954 /* Draw relief if not yet drawn. */
2955 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
2956 x_draw_glyph_string_box (s);
2958 if (s->prev)
2960 struct glyph_string *prev;
2962 for (prev = s->prev; prev; prev = prev->prev)
2963 if (prev->hl != s->hl
2964 && prev->x + prev->width + prev->right_overhang > s->x)
2966 /* As prev was drawn while clipped to its own area, we
2967 must draw the right_overhang part using s->hl now. */
2968 enum draw_glyphs_face save = prev->hl;
2970 prev->hl = s->hl;
2971 x_set_glyph_string_gc (prev);
2972 x_set_glyph_string_clipping_exactly (s, prev);
2973 if (prev->first_glyph->type == CHAR_GLYPH)
2974 x_draw_glyph_string_foreground (prev);
2975 else
2976 x_draw_composite_glyph_string_foreground (prev);
2977 XSetClipMask (prev->display, prev->gc, None);
2978 prev->hl = save;
2979 #ifdef USE_FONT_BACKEND
2980 prev->clip_width = 0;
2981 #endif /* USE_FONT_BACKEND */
2985 if (s->next)
2987 struct glyph_string *next;
2989 for (next = s->next; next; next = next->next)
2990 if (next->hl != s->hl
2991 && next->x - next->left_overhang < s->x + s->width)
2993 /* As next will be drawn while clipped to its own area,
2994 we must draw the left_overhang part using s->hl now. */
2995 enum draw_glyphs_face save = next->hl;
2997 next->hl = s->hl;
2998 x_set_glyph_string_gc (next);
2999 x_set_glyph_string_clipping_exactly (s, next);
3000 if (next->first_glyph->type == CHAR_GLYPH)
3001 x_draw_glyph_string_foreground (next);
3002 else
3003 x_draw_composite_glyph_string_foreground (next);
3004 XSetClipMask (next->display, next->gc, None);
3005 next->hl = save;
3006 #ifdef USE_FONT_BACKEND
3007 next->clip_width = 0;
3008 #endif /* USE_FONT_BACKEND */
3013 /* Reset clipping. */
3014 XSetClipMask (s->display, s->gc, None);
3015 #ifdef USE_FONT_BACKEND
3016 s->clip_width = 0;
3017 #endif /* USE_FONT_BACKEND */
3020 /* Shift display to make room for inserted glyphs. */
3022 void
3023 x_shift_glyphs_for_insert (f, x, y, width, height, shift_by)
3024 struct frame *f;
3025 int x, y, width, height, shift_by;
3027 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3028 f->output_data.x->normal_gc,
3029 x, y, width, height,
3030 x + shift_by, y);
3033 /* Delete N glyphs at the nominal cursor position. Not implemented
3034 for X frames. */
3036 static void
3037 x_delete_glyphs (n)
3038 register int n;
3040 abort ();
3044 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
3045 If they are <= 0, this is probably an error. */
3047 void
3048 x_clear_area (dpy, window, x, y, width, height, exposures)
3049 Display *dpy;
3050 Window window;
3051 int x, y;
3052 int width, height;
3053 int exposures;
3055 xassert (width > 0 && height > 0);
3056 XClearArea (dpy, window, x, y, width, height, exposures);
3060 /* Clear entire frame. If updating_frame is non-null, clear that
3061 frame. Otherwise clear the selected frame. */
3063 static void
3064 x_clear_frame ()
3066 struct frame *f;
3068 if (updating_frame)
3069 f = updating_frame;
3070 else
3071 f = SELECTED_FRAME ();
3073 /* Clearing the frame will erase any cursor, so mark them all as no
3074 longer visible. */
3075 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
3076 output_cursor.hpos = output_cursor.vpos = 0;
3077 output_cursor.x = -1;
3079 /* We don't set the output cursor here because there will always
3080 follow an explicit cursor_to. */
3081 BLOCK_INPUT;
3082 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
3084 /* We have to clear the scroll bars, too. If we have changed
3085 colors or something like that, then they should be notified. */
3086 x_scroll_bar_clear (f);
3088 XFlush (FRAME_X_DISPLAY (f));
3090 UNBLOCK_INPUT;
3095 /* Invert the middle quarter of the frame for .15 sec. */
3097 /* We use the select system call to do the waiting, so we have to make
3098 sure it's available. If it isn't, we just won't do visual bells. */
3100 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3103 /* Subtract the `struct timeval' values X and Y, storing the result in
3104 *RESULT. Return 1 if the difference is negative, otherwise 0. */
3106 static int
3107 timeval_subtract (result, x, y)
3108 struct timeval *result, x, y;
3110 /* Perform the carry for the later subtraction by updating y. This
3111 is safer because on some systems the tv_sec member is unsigned. */
3112 if (x.tv_usec < y.tv_usec)
3114 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
3115 y.tv_usec -= 1000000 * nsec;
3116 y.tv_sec += nsec;
3119 if (x.tv_usec - y.tv_usec > 1000000)
3121 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
3122 y.tv_usec += 1000000 * nsec;
3123 y.tv_sec -= nsec;
3126 /* Compute the time remaining to wait. tv_usec is certainly
3127 positive. */
3128 result->tv_sec = x.tv_sec - y.tv_sec;
3129 result->tv_usec = x.tv_usec - y.tv_usec;
3131 /* Return indication of whether the result should be considered
3132 negative. */
3133 return x.tv_sec < y.tv_sec;
3136 void
3137 XTflash (f)
3138 struct frame *f;
3140 BLOCK_INPUT;
3143 GC gc;
3145 /* Create a GC that will use the GXxor function to flip foreground
3146 pixels into background pixels. */
3148 XGCValues values;
3150 values.function = GXxor;
3151 values.foreground = (f->output_data.x->foreground_pixel
3152 ^ f->output_data.x->background_pixel);
3154 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3155 GCFunction | GCForeground, &values);
3159 /* Get the height not including a menu bar widget. */
3160 int height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, FRAME_LINES (f));
3161 /* Height of each line to flash. */
3162 int flash_height = FRAME_LINE_HEIGHT (f);
3163 /* These will be the left and right margins of the rectangles. */
3164 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
3165 int flash_right = FRAME_PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
3167 int width;
3169 /* Don't flash the area between a scroll bar and the frame
3170 edge it is next to. */
3171 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
3173 case vertical_scroll_bar_left:
3174 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3175 break;
3177 case vertical_scroll_bar_right:
3178 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
3179 break;
3181 default:
3182 break;
3185 width = flash_right - flash_left;
3187 /* If window is tall, flash top and bottom line. */
3188 if (height > 3 * FRAME_LINE_HEIGHT (f))
3190 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3191 flash_left,
3192 (FRAME_INTERNAL_BORDER_WIDTH (f)
3193 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3194 width, flash_height);
3195 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3196 flash_left,
3197 (height - flash_height
3198 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3199 width, flash_height);
3201 else
3202 /* If it is short, flash it all. */
3203 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3204 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3205 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3207 x_flush (f);
3210 struct timeval wakeup;
3212 EMACS_GET_TIME (wakeup);
3214 /* Compute time to wait until, propagating carry from usecs. */
3215 wakeup.tv_usec += 150000;
3216 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
3217 wakeup.tv_usec %= 1000000;
3219 /* Keep waiting until past the time wakeup or any input gets
3220 available. */
3221 while (! detect_input_pending ())
3223 struct timeval current;
3224 struct timeval timeout;
3226 EMACS_GET_TIME (current);
3228 /* Break if result would be negative. */
3229 if (timeval_subtract (&current, wakeup, current))
3230 break;
3232 /* How long `select' should wait. */
3233 timeout.tv_sec = 0;
3234 timeout.tv_usec = 10000;
3236 /* Try to wait that long--but we might wake up sooner. */
3237 select (0, NULL, NULL, NULL, &timeout);
3241 /* If window is tall, flash top and bottom line. */
3242 if (height > 3 * FRAME_LINE_HEIGHT (f))
3244 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3245 flash_left,
3246 (FRAME_INTERNAL_BORDER_WIDTH (f)
3247 + FRAME_TOOL_BAR_LINES (f) * FRAME_LINE_HEIGHT (f)),
3248 width, flash_height);
3249 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3250 flash_left,
3251 (height - flash_height
3252 - FRAME_INTERNAL_BORDER_WIDTH (f)),
3253 width, flash_height);
3255 else
3256 /* If it is short, flash it all. */
3257 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3258 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
3259 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
3261 XFreeGC (FRAME_X_DISPLAY (f), gc);
3262 x_flush (f);
3266 UNBLOCK_INPUT;
3269 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
3272 /* Make audible bell. */
3274 void
3275 XTring_bell ()
3277 struct frame *f = SELECTED_FRAME ();
3279 if (FRAME_X_DISPLAY (f))
3281 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
3282 if (visible_bell)
3283 XTflash (f);
3284 else
3285 #endif
3287 BLOCK_INPUT;
3288 XBell (FRAME_X_DISPLAY (f), 0);
3289 XFlush (FRAME_X_DISPLAY (f));
3290 UNBLOCK_INPUT;
3296 /* Specify how many text lines, from the top of the window,
3297 should be affected by insert-lines and delete-lines operations.
3298 This, and those operations, are used only within an update
3299 that is bounded by calls to x_update_begin and x_update_end. */
3301 static void
3302 XTset_terminal_window (n)
3303 register int n;
3305 /* This function intentionally left blank. */
3310 /***********************************************************************
3311 Line Dance
3312 ***********************************************************************/
3314 /* Perform an insert-lines or delete-lines operation, inserting N
3315 lines or deleting -N lines at vertical position VPOS. */
3317 static void
3318 x_ins_del_lines (vpos, n)
3319 int vpos, n;
3321 abort ();
3325 /* Scroll part of the display as described by RUN. */
3327 static void
3328 x_scroll_run (w, run)
3329 struct window *w;
3330 struct run *run;
3332 struct frame *f = XFRAME (w->frame);
3333 int x, y, width, height, from_y, to_y, bottom_y;
3335 /* Get frame-relative bounding box of the text display area of W,
3336 without mode lines. Include in this box the left and right
3337 fringe of W. */
3338 window_box (w, -1, &x, &y, &width, &height);
3340 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
3341 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
3342 bottom_y = y + height;
3344 if (to_y < from_y)
3346 /* Scrolling up. Make sure we don't copy part of the mode
3347 line at the bottom. */
3348 if (from_y + run->height > bottom_y)
3349 height = bottom_y - from_y;
3350 else
3351 height = run->height;
3353 else
3355 /* Scolling down. Make sure we don't copy over the mode line.
3356 at the bottom. */
3357 if (to_y + run->height > bottom_y)
3358 height = bottom_y - to_y;
3359 else
3360 height = run->height;
3363 BLOCK_INPUT;
3365 /* Cursor off. Will be switched on again in x_update_window_end. */
3366 updated_window = w;
3367 x_clear_cursor (w);
3369 XCopyArea (FRAME_X_DISPLAY (f),
3370 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
3371 f->output_data.x->normal_gc,
3372 x, from_y,
3373 width, height,
3374 x, to_y);
3376 UNBLOCK_INPUT;
3381 /***********************************************************************
3382 Exposure Events
3383 ***********************************************************************/
3386 static void
3387 frame_highlight (f)
3388 struct frame *f;
3390 /* We used to only do this if Vx_no_window_manager was non-nil, but
3391 the ICCCM (section 4.1.6) says that the window's border pixmap
3392 and border pixel are window attributes which are "private to the
3393 client", so we can always change it to whatever we want. */
3394 BLOCK_INPUT;
3395 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3396 f->output_data.x->border_pixel);
3397 UNBLOCK_INPUT;
3398 x_update_cursor (f, 1);
3401 static void
3402 frame_unhighlight (f)
3403 struct frame *f;
3405 /* We used to only do this if Vx_no_window_manager was non-nil, but
3406 the ICCCM (section 4.1.6) says that the window's border pixmap
3407 and border pixel are window attributes which are "private to the
3408 client", so we can always change it to whatever we want. */
3409 BLOCK_INPUT;
3410 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
3411 f->output_data.x->border_tile);
3412 UNBLOCK_INPUT;
3413 x_update_cursor (f, 1);
3416 /* The focus has changed. Update the frames as necessary to reflect
3417 the new situation. Note that we can't change the selected frame
3418 here, because the Lisp code we are interrupting might become confused.
3419 Each event gets marked with the frame in which it occurred, so the
3420 Lisp code can tell when the switch took place by examining the events. */
3422 static void
3423 x_new_focus_frame (dpyinfo, frame)
3424 struct x_display_info *dpyinfo;
3425 struct frame *frame;
3427 struct frame *old_focus = dpyinfo->x_focus_frame;
3429 if (frame != dpyinfo->x_focus_frame)
3431 /* Set this before calling other routines, so that they see
3432 the correct value of x_focus_frame. */
3433 dpyinfo->x_focus_frame = frame;
3435 if (old_focus && old_focus->auto_lower)
3436 x_lower_frame (old_focus);
3438 #if 0
3439 selected_frame = frame;
3440 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
3441 selected_frame);
3442 Fselect_window (selected_frame->selected_window, Qnil);
3443 choose_minibuf_frame ();
3444 #endif /* ! 0 */
3446 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
3447 pending_autoraise_frame = dpyinfo->x_focus_frame;
3448 else
3449 pending_autoraise_frame = 0;
3452 x_frame_rehighlight (dpyinfo);
3455 /* Handle FocusIn and FocusOut state changes for FRAME.
3456 If FRAME has focus and there exists more than one frame, puts
3457 a FOCUS_IN_EVENT into *BUFP. */
3459 static void
3460 x_focus_changed (type, state, dpyinfo, frame, bufp)
3461 int type;
3462 int state;
3463 struct x_display_info *dpyinfo;
3464 struct frame *frame;
3465 struct input_event *bufp;
3467 if (type == FocusIn)
3469 if (dpyinfo->x_focus_event_frame != frame)
3471 x_new_focus_frame (dpyinfo, frame);
3472 dpyinfo->x_focus_event_frame = frame;
3474 /* Don't stop displaying the initial startup message
3475 for a switch-frame event we don't need. */
3476 if (NILP (Vterminal_frame)
3477 && CONSP (Vframe_list)
3478 && !NILP (XCDR (Vframe_list)))
3480 bufp->kind = FOCUS_IN_EVENT;
3481 XSETFRAME (bufp->frame_or_window, frame);
3485 frame->output_data.x->focus_state |= state;
3487 #ifdef HAVE_X_I18N
3488 if (FRAME_XIC (frame))
3489 XSetICFocus (FRAME_XIC (frame));
3490 #endif
3492 else if (type == FocusOut)
3494 frame->output_data.x->focus_state &= ~state;
3496 if (dpyinfo->x_focus_event_frame == frame)
3498 dpyinfo->x_focus_event_frame = 0;
3499 x_new_focus_frame (dpyinfo, 0);
3502 #ifdef HAVE_X_I18N
3503 if (FRAME_XIC (frame))
3504 XUnsetICFocus (FRAME_XIC (frame));
3505 #endif
3509 /* The focus may have changed. Figure out if it is a real focus change,
3510 by checking both FocusIn/Out and Enter/LeaveNotify events.
3512 Returns FOCUS_IN_EVENT event in *BUFP. */
3514 static void
3515 x_detect_focus_change (dpyinfo, event, bufp)
3516 struct x_display_info *dpyinfo;
3517 XEvent *event;
3518 struct input_event *bufp;
3520 struct frame *frame;
3522 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
3523 if (! frame)
3524 return;
3526 switch (event->type)
3528 case EnterNotify:
3529 case LeaveNotify:
3531 struct frame *focus_frame = dpyinfo->x_focus_event_frame;
3532 int focus_state
3533 = focus_frame ? focus_frame->output_data.x->focus_state : 0;
3535 if (event->xcrossing.detail != NotifyInferior
3536 && event->xcrossing.focus
3537 && ! (focus_state & FOCUS_EXPLICIT))
3538 x_focus_changed ((event->type == EnterNotify ? FocusIn : FocusOut),
3539 FOCUS_IMPLICIT,
3540 dpyinfo, frame, bufp);
3542 break;
3544 case FocusIn:
3545 case FocusOut:
3546 x_focus_changed (event->type,
3547 (event->xfocus.detail == NotifyPointer ?
3548 FOCUS_IMPLICIT : FOCUS_EXPLICIT),
3549 dpyinfo, frame, bufp);
3550 break;
3555 /* Handle an event saying the mouse has moved out of an Emacs frame. */
3557 void
3558 x_mouse_leave (dpyinfo)
3559 struct x_display_info *dpyinfo;
3561 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
3564 /* The focus has changed, or we have redirected a frame's focus to
3565 another frame (this happens when a frame uses a surrogate
3566 mini-buffer frame). Shift the highlight as appropriate.
3568 The FRAME argument doesn't necessarily have anything to do with which
3569 frame is being highlighted or un-highlighted; we only use it to find
3570 the appropriate X display info. */
3572 static void
3573 XTframe_rehighlight (frame)
3574 struct frame *frame;
3576 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
3579 static void
3580 x_frame_rehighlight (dpyinfo)
3581 struct x_display_info *dpyinfo;
3583 struct frame *old_highlight = dpyinfo->x_highlight_frame;
3585 if (dpyinfo->x_focus_frame)
3587 dpyinfo->x_highlight_frame
3588 = ((FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
3589 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
3590 : dpyinfo->x_focus_frame);
3591 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
3593 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
3594 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
3597 else
3598 dpyinfo->x_highlight_frame = 0;
3600 if (dpyinfo->x_highlight_frame != old_highlight)
3602 if (old_highlight)
3603 frame_unhighlight (old_highlight);
3604 if (dpyinfo->x_highlight_frame)
3605 frame_highlight (dpyinfo->x_highlight_frame);
3611 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
3613 /* Initialize mode_switch_bit and modifier_meaning. */
3614 static void
3615 x_find_modifier_meanings (dpyinfo)
3616 struct x_display_info *dpyinfo;
3618 int min_code, max_code;
3619 KeySym *syms;
3620 int syms_per_code;
3621 XModifierKeymap *mods;
3623 dpyinfo->meta_mod_mask = 0;
3624 dpyinfo->shift_lock_mask = 0;
3625 dpyinfo->alt_mod_mask = 0;
3626 dpyinfo->super_mod_mask = 0;
3627 dpyinfo->hyper_mod_mask = 0;
3629 #ifdef HAVE_X11R4
3630 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
3631 #else
3632 min_code = dpyinfo->display->min_keycode;
3633 max_code = dpyinfo->display->max_keycode;
3634 #endif
3636 syms = XGetKeyboardMapping (dpyinfo->display,
3637 min_code, max_code - min_code + 1,
3638 &syms_per_code);
3639 mods = XGetModifierMapping (dpyinfo->display);
3641 /* Scan the modifier table to see which modifier bits the Meta and
3642 Alt keysyms are on. */
3644 int row, col; /* The row and column in the modifier table. */
3645 int found_alt_or_meta;
3647 for (row = 3; row < 8; row++)
3649 found_alt_or_meta = 0;
3650 for (col = 0; col < mods->max_keypermod; col++)
3652 KeyCode code = mods->modifiermap[(row * mods->max_keypermod) + col];
3654 /* Zeroes are used for filler. Skip them. */
3655 if (code == 0)
3656 continue;
3658 /* Are any of this keycode's keysyms a meta key? */
3660 int code_col;
3662 for (code_col = 0; code_col < syms_per_code; code_col++)
3664 int sym = syms[((code - min_code) * syms_per_code) + code_col];
3666 switch (sym)
3668 case XK_Meta_L:
3669 case XK_Meta_R:
3670 found_alt_or_meta = 1;
3671 dpyinfo->meta_mod_mask |= (1 << row);
3672 break;
3674 case XK_Alt_L:
3675 case XK_Alt_R:
3676 found_alt_or_meta = 1;
3677 dpyinfo->alt_mod_mask |= (1 << row);
3678 break;
3680 case XK_Hyper_L:
3681 case XK_Hyper_R:
3682 if (!found_alt_or_meta)
3683 dpyinfo->hyper_mod_mask |= (1 << row);
3684 code_col = syms_per_code;
3685 col = mods->max_keypermod;
3686 break;
3688 case XK_Super_L:
3689 case XK_Super_R:
3690 if (!found_alt_or_meta)
3691 dpyinfo->super_mod_mask |= (1 << row);
3692 code_col = syms_per_code;
3693 col = mods->max_keypermod;
3694 break;
3696 case XK_Shift_Lock:
3697 /* Ignore this if it's not on the lock modifier. */
3698 if (!found_alt_or_meta && ((1 << row) == LockMask))
3699 dpyinfo->shift_lock_mask = LockMask;
3700 code_col = syms_per_code;
3701 col = mods->max_keypermod;
3702 break;
3710 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
3711 if (! dpyinfo->meta_mod_mask)
3713 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
3714 dpyinfo->alt_mod_mask = 0;
3717 /* If some keys are both alt and meta,
3718 make them just meta, not alt. */
3719 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
3721 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
3724 XFree ((char *) syms);
3725 XFreeModifiermap (mods);
3728 /* Convert between the modifier bits X uses and the modifier bits
3729 Emacs uses. */
3731 unsigned int
3732 x_x_to_emacs_modifiers (dpyinfo, state)
3733 struct x_display_info *dpyinfo;
3734 unsigned int state;
3736 EMACS_UINT mod_meta = meta_modifier;
3737 EMACS_UINT mod_alt = alt_modifier;
3738 EMACS_UINT mod_hyper = hyper_modifier;
3739 EMACS_UINT mod_super = super_modifier;
3740 Lisp_Object tem;
3742 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3743 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3744 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3745 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3746 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3747 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3748 tem = Fget (Vx_super_keysym, Qmodifier_value);
3749 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3752 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
3753 | ((state & ControlMask) ? ctrl_modifier : 0)
3754 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
3755 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
3756 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
3757 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
3760 static unsigned int
3761 x_emacs_to_x_modifiers (dpyinfo, state)
3762 struct x_display_info *dpyinfo;
3763 unsigned int state;
3765 EMACS_UINT mod_meta = meta_modifier;
3766 EMACS_UINT mod_alt = alt_modifier;
3767 EMACS_UINT mod_hyper = hyper_modifier;
3768 EMACS_UINT mod_super = super_modifier;
3770 Lisp_Object tem;
3772 tem = Fget (Vx_alt_keysym, Qmodifier_value);
3773 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
3774 tem = Fget (Vx_meta_keysym, Qmodifier_value);
3775 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
3776 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
3777 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
3778 tem = Fget (Vx_super_keysym, Qmodifier_value);
3779 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
3782 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
3783 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
3784 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
3785 | ((state & shift_modifier) ? ShiftMask : 0)
3786 | ((state & ctrl_modifier) ? ControlMask : 0)
3787 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
3790 /* Convert a keysym to its name. */
3792 char *
3793 x_get_keysym_name (keysym)
3794 KeySym keysym;
3796 char *value;
3798 BLOCK_INPUT;
3799 value = XKeysymToString (keysym);
3800 UNBLOCK_INPUT;
3802 return value;
3807 /* Mouse clicks and mouse movement. Rah. */
3809 /* Prepare a mouse-event in *RESULT for placement in the input queue.
3811 If the event is a button press, then note that we have grabbed
3812 the mouse. */
3814 static Lisp_Object
3815 construct_mouse_click (result, event, f)
3816 struct input_event *result;
3817 XButtonEvent *event;
3818 struct frame *f;
3820 /* Make the event type NO_EVENT; we'll change that when we decide
3821 otherwise. */
3822 result->kind = MOUSE_CLICK_EVENT;
3823 result->code = event->button - Button1;
3824 result->timestamp = event->time;
3825 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
3826 event->state)
3827 | (event->type == ButtonRelease
3828 ? up_modifier
3829 : down_modifier));
3831 XSETINT (result->x, event->x);
3832 XSETINT (result->y, event->y);
3833 XSETFRAME (result->frame_or_window, f);
3834 result->arg = Qnil;
3835 return Qnil;
3839 /* Function to report a mouse movement to the mainstream Emacs code.
3840 The input handler calls this.
3842 We have received a mouse movement event, which is given in *event.
3843 If the mouse is over a different glyph than it was last time, tell
3844 the mainstream emacs code by setting mouse_moved. If not, ask for
3845 another motion event, so we can check again the next time it moves. */
3847 static XMotionEvent last_mouse_motion_event;
3848 static Lisp_Object last_mouse_motion_frame;
3850 static int
3851 note_mouse_movement (frame, event)
3852 FRAME_PTR frame;
3853 XMotionEvent *event;
3855 last_mouse_movement_time = event->time;
3856 last_mouse_motion_event = *event;
3857 XSETFRAME (last_mouse_motion_frame, frame);
3859 if (!FRAME_X_OUTPUT (frame))
3860 return 0;
3862 if (event->window != FRAME_X_WINDOW (frame))
3864 frame->mouse_moved = 1;
3865 last_mouse_scroll_bar = Qnil;
3866 note_mouse_highlight (frame, -1, -1);
3867 last_mouse_glyph_frame = 0;
3868 return 1;
3872 /* Has the mouse moved off the glyph it was on at the last sighting? */
3873 if (frame != last_mouse_glyph_frame
3874 || event->x < last_mouse_glyph.x
3875 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
3876 || event->y < last_mouse_glyph.y
3877 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
3879 frame->mouse_moved = 1;
3880 last_mouse_scroll_bar = Qnil;
3881 note_mouse_highlight (frame, event->x, event->y);
3882 /* Remember which glyph we're now on. */
3883 remember_mouse_glyph (frame, event->x, event->y, &last_mouse_glyph);
3884 last_mouse_glyph_frame = frame;
3885 return 1;
3888 return 0;
3892 /************************************************************************
3893 Mouse Face
3894 ************************************************************************/
3896 static void
3897 redo_mouse_highlight ()
3899 if (!NILP (last_mouse_motion_frame)
3900 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
3901 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
3902 last_mouse_motion_event.x,
3903 last_mouse_motion_event.y);
3908 /* Return the current position of the mouse.
3909 *FP should be a frame which indicates which display to ask about.
3911 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
3912 and *PART to the frame, window, and scroll bar part that the mouse
3913 is over. Set *X and *Y to the portion and whole of the mouse's
3914 position on the scroll bar.
3916 If the mouse movement started elsewhere, set *FP to the frame the
3917 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
3918 the mouse is over.
3920 Set *TIME to the server time-stamp for the time at which the mouse
3921 was at this position.
3923 Don't store anything if we don't have a valid set of values to report.
3925 This clears the mouse_moved flag, so we can wait for the next mouse
3926 movement. */
3928 static void
3929 XTmouse_position (fp, insist, bar_window, part, x, y, time)
3930 FRAME_PTR *fp;
3931 int insist;
3932 Lisp_Object *bar_window;
3933 enum scroll_bar_part *part;
3934 Lisp_Object *x, *y;
3935 unsigned long *time;
3937 FRAME_PTR f1;
3939 BLOCK_INPUT;
3941 if (! NILP (last_mouse_scroll_bar) && insist == 0)
3942 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
3943 else
3945 Window root;
3946 int root_x, root_y;
3948 Window dummy_window;
3949 int dummy;
3951 Lisp_Object frame, tail;
3953 /* Clear the mouse-moved flag for every frame on this display. */
3954 FOR_EACH_FRAME (tail, frame)
3955 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
3956 XFRAME (frame)->mouse_moved = 0;
3958 last_mouse_scroll_bar = Qnil;
3960 /* Figure out which root window we're on. */
3961 XQueryPointer (FRAME_X_DISPLAY (*fp),
3962 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
3964 /* The root window which contains the pointer. */
3965 &root,
3967 /* Trash which we can't trust if the pointer is on
3968 a different screen. */
3969 &dummy_window,
3971 /* The position on that root window. */
3972 &root_x, &root_y,
3974 /* More trash we can't trust. */
3975 &dummy, &dummy,
3977 /* Modifier keys and pointer buttons, about which
3978 we don't care. */
3979 (unsigned int *) &dummy);
3981 /* Now we have a position on the root; find the innermost window
3982 containing the pointer. */
3984 Window win, child;
3985 int win_x, win_y;
3986 int parent_x = 0, parent_y = 0;
3988 win = root;
3990 /* XTranslateCoordinates can get errors if the window
3991 structure is changing at the same time this function
3992 is running. So at least we must not crash from them. */
3994 x_catch_errors (FRAME_X_DISPLAY (*fp));
3996 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
3997 && FRAME_LIVE_P (last_mouse_frame))
3999 /* If mouse was grabbed on a frame, give coords for that frame
4000 even if the mouse is now outside it. */
4001 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4003 /* From-window, to-window. */
4004 root, FRAME_X_WINDOW (last_mouse_frame),
4006 /* From-position, to-position. */
4007 root_x, root_y, &win_x, &win_y,
4009 /* Child of win. */
4010 &child);
4011 f1 = last_mouse_frame;
4013 else
4015 while (1)
4017 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
4019 /* From-window, to-window. */
4020 root, win,
4022 /* From-position, to-position. */
4023 root_x, root_y, &win_x, &win_y,
4025 /* Child of win. */
4026 &child);
4028 if (child == None || child == win)
4029 break;
4031 win = child;
4032 parent_x = win_x;
4033 parent_y = win_y;
4036 /* Now we know that:
4037 win is the innermost window containing the pointer
4038 (XTC says it has no child containing the pointer),
4039 win_x and win_y are the pointer's position in it
4040 (XTC did this the last time through), and
4041 parent_x and parent_y are the pointer's position in win's parent.
4042 (They are what win_x and win_y were when win was child.
4043 If win is the root window, it has no parent, and
4044 parent_{x,y} are invalid, but that's okay, because we'll
4045 never use them in that case.) */
4047 /* Is win one of our frames? */
4048 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
4050 #ifdef USE_X_TOOLKIT
4051 /* If we end up with the menu bar window, say it's not
4052 on the frame. */
4053 if (f1 != NULL
4054 && f1->output_data.x->menubar_widget
4055 && win == XtWindow (f1->output_data.x->menubar_widget))
4056 f1 = NULL;
4057 #endif /* USE_X_TOOLKIT */
4060 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
4061 f1 = 0;
4063 x_uncatch_errors ();
4065 /* If not, is it one of our scroll bars? */
4066 if (! f1)
4068 struct scroll_bar *bar;
4070 bar = x_window_to_scroll_bar (FRAME_X_DISPLAY (*fp), win);
4072 if (bar)
4074 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4075 win_x = parent_x;
4076 win_y = parent_y;
4080 if (f1 == 0 && insist > 0)
4081 f1 = SELECTED_FRAME ();
4083 if (f1)
4085 /* Ok, we found a frame. Store all the values.
4086 last_mouse_glyph is a rectangle used to reduce the
4087 generation of mouse events. To not miss any motion
4088 events, we must divide the frame into rectangles of the
4089 size of the smallest character that could be displayed
4090 on it, i.e. into the same rectangles that matrices on
4091 the frame are divided into. */
4093 remember_mouse_glyph (f1, win_x, win_y, &last_mouse_glyph);
4094 last_mouse_glyph_frame = f1;
4096 *bar_window = Qnil;
4097 *part = 0;
4098 *fp = f1;
4099 XSETINT (*x, win_x);
4100 XSETINT (*y, win_y);
4101 *time = last_mouse_movement_time;
4106 UNBLOCK_INPUT;
4111 /***********************************************************************
4112 Scroll bars
4113 ***********************************************************************/
4115 /* Scroll bar support. */
4117 /* Given an X window ID and a DISPLAY, find the struct scroll_bar which
4118 manages it.
4119 This can be called in GC, so we have to make sure to strip off mark
4120 bits. */
4122 static struct scroll_bar *
4123 x_window_to_scroll_bar (display, window_id)
4124 Display *display;
4125 Window window_id;
4127 Lisp_Object tail;
4129 #if defined (USE_GTK) && defined (USE_TOOLKIT_SCROLL_BARS)
4130 window_id = (Window) xg_get_scroll_id_for_window (display, window_id);
4131 #endif /* USE_GTK && USE_TOOLKIT_SCROLL_BARS */
4133 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4135 Lisp_Object frame, bar, condemned;
4137 frame = XCAR (tail);
4138 /* All elements of Vframe_list should be frames. */
4139 if (! FRAMEP (frame))
4140 abort ();
4142 /* Scan this frame's scroll bar list for a scroll bar with the
4143 right window ID. */
4144 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
4145 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
4146 /* This trick allows us to search both the ordinary and
4147 condemned scroll bar lists with one loop. */
4148 ! NILP (bar) || (bar = condemned,
4149 condemned = Qnil,
4150 ! NILP (bar));
4151 bar = XSCROLL_BAR (bar)->next)
4152 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id &&
4153 FRAME_X_DISPLAY (XFRAME (frame)) == display)
4154 return XSCROLL_BAR (bar);
4157 return 0;
4161 #if defined USE_LUCID
4163 /* Return the Lucid menu bar WINDOW is part of. Return null
4164 if WINDOW is not part of a menu bar. */
4166 static Widget
4167 x_window_to_menu_bar (window)
4168 Window window;
4170 Lisp_Object tail;
4172 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
4174 Lisp_Object frame = XCAR (tail);
4175 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
4177 if (menu_bar && xlwmenu_window_p (menu_bar, window))
4178 return menu_bar;
4181 return NULL;
4184 #endif /* USE_LUCID */
4187 /************************************************************************
4188 Toolkit scroll bars
4189 ************************************************************************/
4191 #ifdef USE_TOOLKIT_SCROLL_BARS
4193 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
4194 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
4195 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
4196 struct scroll_bar *));
4197 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
4198 int, int, int));
4201 /* Lisp window being scrolled. Set when starting to interact with
4202 a toolkit scroll bar, reset to nil when ending the interaction. */
4204 static Lisp_Object window_being_scrolled;
4206 /* Last scroll bar part sent in xm_scroll_callback. */
4208 static int last_scroll_bar_part;
4210 /* Whether this is an Xaw with arrow-scrollbars. This should imply
4211 that movements of 1/20 of the screen size are mapped to up/down. */
4213 #ifndef USE_GTK
4214 /* Id of action hook installed for scroll bars. */
4216 static XtActionHookId action_hook_id;
4218 static Boolean xaw3d_arrow_scroll;
4220 /* Whether the drag scrolling maintains the mouse at the top of the
4221 thumb. If not, resizing the thumb needs to be done more carefully
4222 to avoid jerkyness. */
4224 static Boolean xaw3d_pick_top;
4226 /* Action hook installed via XtAppAddActionHook when toolkit scroll
4227 bars are used.. The hook is responsible for detecting when
4228 the user ends an interaction with the scroll bar, and generates
4229 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
4231 static void
4232 xt_action_hook (widget, client_data, action_name, event, params,
4233 num_params)
4234 Widget widget;
4235 XtPointer client_data;
4236 String action_name;
4237 XEvent *event;
4238 String *params;
4239 Cardinal *num_params;
4241 int scroll_bar_p;
4242 char *end_action;
4244 #ifdef USE_MOTIF
4245 scroll_bar_p = XmIsScrollBar (widget);
4246 end_action = "Release";
4247 #else /* !USE_MOTIF i.e. use Xaw */
4248 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
4249 end_action = "EndScroll";
4250 #endif /* USE_MOTIF */
4252 if (scroll_bar_p
4253 && strcmp (action_name, end_action) == 0
4254 && WINDOWP (window_being_scrolled))
4256 struct window *w;
4258 x_send_scroll_bar_event (window_being_scrolled,
4259 scroll_bar_end_scroll, 0, 0);
4260 w = XWINDOW (window_being_scrolled);
4262 if (!NILP (XSCROLL_BAR (w->vertical_scroll_bar)->dragging))
4264 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
4265 /* The thumb size is incorrect while dragging: fix it. */
4266 set_vertical_scroll_bar (w);
4268 window_being_scrolled = Qnil;
4269 last_scroll_bar_part = -1;
4271 /* Xt timeouts no longer needed. */
4272 toolkit_scroll_bar_interaction = 0;
4275 #endif /* not USE_GTK */
4277 /* A vector of windows used for communication between
4278 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
4280 static struct window **scroll_bar_windows;
4281 static int scroll_bar_windows_size;
4284 /* Send a client message with message type Xatom_Scrollbar for a
4285 scroll action to the frame of WINDOW. PART is a value identifying
4286 the part of the scroll bar that was clicked on. PORTION is the
4287 amount to scroll of a whole of WHOLE. */
4289 static void
4290 x_send_scroll_bar_event (window, part, portion, whole)
4291 Lisp_Object window;
4292 int part, portion, whole;
4294 XEvent event;
4295 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
4296 struct window *w = XWINDOW (window);
4297 struct frame *f = XFRAME (w->frame);
4298 int i;
4300 BLOCK_INPUT;
4302 /* Construct a ClientMessage event to send to the frame. */
4303 ev->type = ClientMessage;
4304 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
4305 ev->display = FRAME_X_DISPLAY (f);
4306 ev->window = FRAME_X_WINDOW (f);
4307 ev->format = 32;
4309 /* We can only transfer 32 bits in the XClientMessageEvent, which is
4310 not enough to store a pointer or Lisp_Object on a 64 bit system.
4311 So, store the window in scroll_bar_windows and pass the index
4312 into that array in the event. */
4313 for (i = 0; i < scroll_bar_windows_size; ++i)
4314 if (scroll_bar_windows[i] == NULL)
4315 break;
4317 if (i == scroll_bar_windows_size)
4319 int new_size = max (10, 2 * scroll_bar_windows_size);
4320 size_t nbytes = new_size * sizeof *scroll_bar_windows;
4321 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
4323 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
4324 nbytes);
4325 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
4326 scroll_bar_windows_size = new_size;
4329 scroll_bar_windows[i] = w;
4330 ev->data.l[0] = (long) i;
4331 ev->data.l[1] = (long) part;
4332 ev->data.l[2] = (long) 0;
4333 ev->data.l[3] = (long) portion;
4334 ev->data.l[4] = (long) whole;
4336 /* Make Xt timeouts work while the scroll bar is active. */
4337 toolkit_scroll_bar_interaction = 1;
4338 #ifdef USE_X_TOOLKIT
4339 x_activate_timeout_atimer ();
4340 #endif
4342 /* Setting the event mask to zero means that the message will
4343 be sent to the client that created the window, and if that
4344 window no longer exists, no event will be sent. */
4345 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
4346 UNBLOCK_INPUT;
4350 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
4351 in *IEVENT. */
4353 static void
4354 x_scroll_bar_to_input_event (event, ievent)
4355 XEvent *event;
4356 struct input_event *ievent;
4358 XClientMessageEvent *ev = (XClientMessageEvent *) event;
4359 Lisp_Object window;
4360 struct frame *f;
4361 struct window *w;
4363 w = scroll_bar_windows[ev->data.l[0]];
4364 scroll_bar_windows[ev->data.l[0]] = NULL;
4366 XSETWINDOW (window, w);
4367 f = XFRAME (w->frame);
4369 ievent->kind = SCROLL_BAR_CLICK_EVENT;
4370 ievent->frame_or_window = window;
4371 ievent->arg = Qnil;
4372 #ifdef USE_GTK
4373 ievent->timestamp = CurrentTime;
4374 #else
4375 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
4376 #endif
4377 ievent->part = ev->data.l[1];
4378 ievent->code = ev->data.l[2];
4379 ievent->x = make_number ((int) ev->data.l[3]);
4380 ievent->y = make_number ((int) ev->data.l[4]);
4381 ievent->modifiers = 0;
4385 #ifdef USE_MOTIF
4387 /* Minimum and maximum values used for Motif scroll bars. */
4389 #define XM_SB_MAX 10000000
4392 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
4393 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
4394 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
4396 static void
4397 xm_scroll_callback (widget, client_data, call_data)
4398 Widget widget;
4399 XtPointer client_data, call_data;
4401 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4402 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
4403 int part = -1, whole = 0, portion = 0;
4405 switch (cs->reason)
4407 case XmCR_DECREMENT:
4408 bar->dragging = Qnil;
4409 part = scroll_bar_up_arrow;
4410 break;
4412 case XmCR_INCREMENT:
4413 bar->dragging = Qnil;
4414 part = scroll_bar_down_arrow;
4415 break;
4417 case XmCR_PAGE_DECREMENT:
4418 bar->dragging = Qnil;
4419 part = scroll_bar_above_handle;
4420 break;
4422 case XmCR_PAGE_INCREMENT:
4423 bar->dragging = Qnil;
4424 part = scroll_bar_below_handle;
4425 break;
4427 case XmCR_TO_TOP:
4428 bar->dragging = Qnil;
4429 part = scroll_bar_to_top;
4430 break;
4432 case XmCR_TO_BOTTOM:
4433 bar->dragging = Qnil;
4434 part = scroll_bar_to_bottom;
4435 break;
4437 case XmCR_DRAG:
4439 int slider_size;
4441 /* Get the slider size. */
4442 BLOCK_INPUT;
4443 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
4444 UNBLOCK_INPUT;
4446 whole = XM_SB_MAX - slider_size;
4447 portion = min (cs->value, whole);
4448 part = scroll_bar_handle;
4449 bar->dragging = make_number (cs->value);
4451 break;
4453 case XmCR_VALUE_CHANGED:
4454 break;
4457 if (part >= 0)
4459 window_being_scrolled = bar->window;
4460 last_scroll_bar_part = part;
4461 x_send_scroll_bar_event (bar->window, part, portion, whole);
4466 #else /* !USE_MOTIF, i.e. Xaw or GTK */
4467 #ifdef USE_GTK
4468 /* Scroll bar callback for GTK scroll bars. WIDGET is the scroll
4469 bar widget. DATA is a pointer to the scroll_bar structure. */
4471 static void
4472 xg_scroll_callback (widget, data)
4473 GtkRange *widget;
4474 gpointer data;
4476 struct scroll_bar *bar = (struct scroll_bar *) data;
4477 gdouble previous;
4478 gdouble position;
4479 gdouble *p;
4480 int diff;
4482 int part = -1, whole = 0, portion = 0;
4483 GtkAdjustment *adj = GTK_ADJUSTMENT (gtk_range_get_adjustment (widget));
4485 position = gtk_adjustment_get_value (adj);
4487 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
4488 if (! p)
4490 p = (gdouble*) xmalloc (sizeof (gdouble));
4491 *p = XG_SB_MIN;
4492 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
4495 previous = *p;
4496 *p = position;
4498 if (xg_ignore_gtk_scrollbar) return;
4500 diff = (int) (position - previous);
4502 if (diff == (int) adj->step_increment)
4504 part = scroll_bar_down_arrow;
4505 bar->dragging = Qnil;
4507 else if (-diff == (int) adj->step_increment)
4509 part = scroll_bar_up_arrow;
4510 bar->dragging = Qnil;
4512 else if (diff == (int) adj->page_increment)
4514 part = scroll_bar_below_handle;
4515 bar->dragging = Qnil;
4517 else if (-diff == (int) adj->page_increment)
4519 part = scroll_bar_above_handle;
4520 bar->dragging = Qnil;
4522 else
4524 part = scroll_bar_handle;
4525 whole = adj->upper - adj->page_size;
4526 portion = min ((int)position, whole);
4527 bar->dragging = make_number ((int)portion);
4530 if (part >= 0)
4532 window_being_scrolled = bar->window;
4533 last_scroll_bar_part = part;
4534 x_send_scroll_bar_event (bar->window, part, portion, whole);
4538 #else /* not USE_GTK */
4540 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
4541 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
4542 scroll bar struct. CALL_DATA is a pointer to a float saying where
4543 the thumb is. */
4545 static void
4546 xaw_jump_callback (widget, client_data, call_data)
4547 Widget widget;
4548 XtPointer client_data, call_data;
4550 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4551 float top = *(float *) call_data;
4552 float shown;
4553 int whole, portion, height;
4554 int part;
4556 /* Get the size of the thumb, a value between 0 and 1. */
4557 BLOCK_INPUT;
4558 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
4559 UNBLOCK_INPUT;
4561 whole = 10000000;
4562 portion = shown < 1 ? top * whole : 0;
4564 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
4565 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
4566 the bottom, so we force the scrolling whenever we see that we're
4567 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
4568 we try to ensure that we always stay two pixels away from the
4569 bottom). */
4570 part = scroll_bar_down_arrow;
4571 else
4572 part = scroll_bar_handle;
4574 window_being_scrolled = bar->window;
4575 bar->dragging = make_number (portion);
4576 last_scroll_bar_part = part;
4577 x_send_scroll_bar_event (bar->window, part, portion, whole);
4581 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
4582 i.e. line or page up or down. WIDGET is the Xaw scroll bar
4583 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
4584 the scroll bar. CALL_DATA is an integer specifying the action that
4585 has taken place. Its magnitude is in the range 0..height of the
4586 scroll bar. Negative values mean scroll towards buffer start.
4587 Values < height of scroll bar mean line-wise movement. */
4589 static void
4590 xaw_scroll_callback (widget, client_data, call_data)
4591 Widget widget;
4592 XtPointer client_data, call_data;
4594 struct scroll_bar *bar = (struct scroll_bar *) client_data;
4595 /* The position really is stored cast to a pointer. */
4596 int position = (long) call_data;
4597 Dimension height;
4598 int part;
4600 /* Get the height of the scroll bar. */
4601 BLOCK_INPUT;
4602 XtVaGetValues (widget, XtNheight, &height, NULL);
4603 UNBLOCK_INPUT;
4605 if (abs (position) >= height)
4606 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
4608 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
4609 it maps line-movement to call_data = max(5, height/20). */
4610 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
4611 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
4612 else
4613 part = scroll_bar_move_ratio;
4615 window_being_scrolled = bar->window;
4616 bar->dragging = Qnil;
4617 last_scroll_bar_part = part;
4618 x_send_scroll_bar_event (bar->window, part, position, height);
4621 #endif /* not USE_GTK */
4622 #endif /* not USE_MOTIF */
4624 #define SCROLL_BAR_NAME "verticalScrollBar"
4626 /* Create the widget for scroll bar BAR on frame F. Record the widget
4627 and X window of the scroll bar in BAR. */
4629 #ifdef USE_GTK
4630 static void
4631 x_create_toolkit_scroll_bar (f, bar)
4632 struct frame *f;
4633 struct scroll_bar *bar;
4635 char *scroll_bar_name = SCROLL_BAR_NAME;
4637 BLOCK_INPUT;
4638 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
4639 scroll_bar_name);
4640 UNBLOCK_INPUT;
4643 #else /* not USE_GTK */
4645 static void
4646 x_create_toolkit_scroll_bar (f, bar)
4647 struct frame *f;
4648 struct scroll_bar *bar;
4650 Window xwindow;
4651 Widget widget;
4652 Arg av[20];
4653 int ac = 0;
4654 char *scroll_bar_name = SCROLL_BAR_NAME;
4655 unsigned long pixel;
4657 BLOCK_INPUT;
4659 #ifdef USE_MOTIF
4660 /* Set resources. Create the widget. */
4661 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4662 XtSetArg (av[ac], XmNminimum, 0); ++ac;
4663 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
4664 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
4665 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
4666 XtSetArg (av[ac], XmNincrement, 1); ++ac;
4667 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
4669 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4670 if (pixel != -1)
4672 XtSetArg (av[ac], XmNforeground, pixel);
4673 ++ac;
4676 pixel = f->output_data.x->scroll_bar_background_pixel;
4677 if (pixel != -1)
4679 XtSetArg (av[ac], XmNbackground, pixel);
4680 ++ac;
4683 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
4684 scroll_bar_name, av, ac);
4686 /* Add one callback for everything that can happen. */
4687 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
4688 (XtPointer) bar);
4689 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
4690 (XtPointer) bar);
4691 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
4692 (XtPointer) bar);
4693 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
4694 (XtPointer) bar);
4695 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
4696 (XtPointer) bar);
4697 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
4698 (XtPointer) bar);
4699 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
4700 (XtPointer) bar);
4702 /* Realize the widget. Only after that is the X window created. */
4703 XtRealizeWidget (widget);
4705 /* Set the cursor to an arrow. I didn't find a resource to do that.
4706 And I'm wondering why it hasn't an arrow cursor by default. */
4707 XDefineCursor (XtDisplay (widget), XtWindow (widget),
4708 f->output_data.x->nontext_cursor);
4710 #else /* !USE_MOTIF i.e. use Xaw */
4712 /* Set resources. Create the widget. The background of the
4713 Xaw3d scroll bar widget is a little bit light for my taste.
4714 We don't alter it here to let users change it according
4715 to their taste with `emacs*verticalScrollBar.background: xxx'. */
4716 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
4717 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
4718 /* For smoother scrolling with Xaw3d -sm */
4719 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
4721 pixel = f->output_data.x->scroll_bar_foreground_pixel;
4722 if (pixel != -1)
4724 XtSetArg (av[ac], XtNforeground, pixel);
4725 ++ac;
4728 pixel = f->output_data.x->scroll_bar_background_pixel;
4729 if (pixel != -1)
4731 XtSetArg (av[ac], XtNbackground, pixel);
4732 ++ac;
4735 /* Top/bottom shadow colors. */
4737 /* Allocate them, if necessary. */
4738 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
4740 pixel = f->output_data.x->scroll_bar_background_pixel;
4741 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4742 &pixel, 1.2, 0x8000))
4743 pixel = -1;
4744 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
4746 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4748 pixel = f->output_data.x->scroll_bar_background_pixel;
4749 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
4750 &pixel, 0.6, 0x4000))
4751 pixel = -1;
4752 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
4755 #ifdef XtNbeNiceToColormap
4756 /* Tell the toolkit about them. */
4757 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
4758 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
4759 /* We tried to allocate a color for the top/bottom shadow, and
4760 failed, so tell Xaw3d to use dithering instead. */
4762 XtSetArg (av[ac], XtNbeNiceToColormap, True);
4763 ++ac;
4765 else
4766 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
4767 be more consistent with other emacs 3d colors, and since Xaw3d is
4768 not good at dealing with allocation failure. */
4770 /* This tells Xaw3d to use real colors instead of dithering for
4771 the shadows. */
4772 XtSetArg (av[ac], XtNbeNiceToColormap, False);
4773 ++ac;
4775 /* Specify the colors. */
4776 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
4777 if (pixel != -1)
4779 XtSetArg (av[ac], XtNtopShadowPixel, pixel);
4780 ++ac;
4782 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
4783 if (pixel != -1)
4785 XtSetArg (av[ac], XtNbottomShadowPixel, pixel);
4786 ++ac;
4789 #endif
4791 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
4792 f->output_data.x->edit_widget, av, ac);
4795 char *initial = "";
4796 char *val = initial;
4797 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
4798 #ifdef XtNarrowScrollbars
4799 XtNarrowScrollbars, (XtPointer) &xaw3d_arrow_scroll,
4800 #endif
4801 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
4802 if (xaw3d_arrow_scroll || val == initial)
4803 { /* ARROW_SCROLL */
4804 xaw3d_arrow_scroll = True;
4805 /* Isn't that just a personal preference ? --Stef */
4806 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
4810 /* Define callbacks. */
4811 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
4812 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
4813 (XtPointer) bar);
4815 /* Realize the widget. Only after that is the X window created. */
4816 XtRealizeWidget (widget);
4818 #endif /* !USE_MOTIF */
4820 /* Install an action hook that lets us detect when the user
4821 finishes interacting with a scroll bar. */
4822 if (action_hook_id == 0)
4823 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
4825 /* Remember X window and widget in the scroll bar vector. */
4826 SET_SCROLL_BAR_X_WIDGET (bar, widget);
4827 xwindow = XtWindow (widget);
4828 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
4830 UNBLOCK_INPUT;
4832 #endif /* not USE_GTK */
4835 /* Set the thumb size and position of scroll bar BAR. We are currently
4836 displaying PORTION out of a whole WHOLE, and our position POSITION. */
4838 #ifdef USE_GTK
4839 static void
4840 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4841 struct scroll_bar *bar;
4842 int portion, position, whole;
4844 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
4847 #else /* not USE_GTK */
4848 static void
4849 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
4850 struct scroll_bar *bar;
4851 int portion, position, whole;
4853 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
4854 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
4855 float top, shown;
4857 BLOCK_INPUT;
4859 #ifdef USE_MOTIF
4861 /* We use an estimate of 30 chars per line rather than the real
4862 `portion' value. This has the disadvantage that the thumb size
4863 is not very representative, but it makes our life a lot easier.
4864 Otherwise, we have to constantly adjust the thumb size, which
4865 we can't always do quickly enough: while dragging, the size of
4866 the thumb might prevent the user from dragging the thumb all the
4867 way to the end. but Motif and some versions of Xaw3d don't allow
4868 updating the thumb size while dragging. Also, even if we can update
4869 its size, the update will often happen too late.
4870 If you don't believe it, check out revision 1.650 of xterm.c to see
4871 what hoops we were going through and the still poor behavior we got. */
4872 portion = WINDOW_TOTAL_LINES (XWINDOW (bar->window)) * 30;
4873 /* When the thumb is at the bottom, position == whole.
4874 So we need to increase `whole' to make space for the thumb. */
4875 whole += portion;
4877 if (whole <= 0)
4878 top = 0, shown = 1;
4879 else
4881 top = (float) position / whole;
4882 shown = (float) portion / whole;
4885 if (NILP (bar->dragging))
4887 int size, value;
4889 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
4890 is the scroll bar's maximum and MIN is the scroll bar's minimum
4891 value. */
4892 size = shown * XM_SB_MAX;
4893 size = min (size, XM_SB_MAX);
4894 size = max (size, 1);
4896 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
4897 value = top * XM_SB_MAX;
4898 value = min (value, XM_SB_MAX - size);
4900 XmScrollBarSetValues (widget, value, size, 0, 0, False);
4902 #else /* !USE_MOTIF i.e. use Xaw */
4904 if (whole == 0)
4905 top = 0, shown = 1;
4906 else
4908 top = (float) position / whole;
4909 shown = (float) portion / whole;
4913 float old_top, old_shown;
4914 Dimension height;
4915 XtVaGetValues (widget,
4916 XtNtopOfThumb, &old_top,
4917 XtNshown, &old_shown,
4918 XtNheight, &height,
4919 NULL);
4921 /* Massage the top+shown values. */
4922 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
4923 top = max (0, min (1, top));
4924 else
4925 top = old_top;
4926 /* Keep two pixels available for moving the thumb down. */
4927 shown = max (0, min (1 - top - (2.0 / height), shown));
4929 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
4930 check that your system's configuration file contains a define
4931 for `NARROWPROTO'. See s/freebsd.h for an example. */
4932 if (top != old_top || shown != old_shown)
4934 if (NILP (bar->dragging))
4935 XawScrollbarSetThumb (widget, top, shown);
4936 else
4938 /* Try to make the scrolling a tad smoother. */
4939 if (!xaw3d_pick_top)
4940 shown = min (shown, old_shown);
4942 XawScrollbarSetThumb (widget, top, shown);
4946 #endif /* !USE_MOTIF */
4948 UNBLOCK_INPUT;
4950 #endif /* not USE_GTK */
4952 #endif /* USE_TOOLKIT_SCROLL_BARS */
4956 /************************************************************************
4957 Scroll bars, general
4958 ************************************************************************/
4960 /* Create a scroll bar and return the scroll bar vector for it. W is
4961 the Emacs window on which to create the scroll bar. TOP, LEFT,
4962 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
4963 scroll bar. */
4965 static struct scroll_bar *
4966 x_scroll_bar_create (w, top, left, width, height)
4967 struct window *w;
4968 int top, left, width, height;
4970 struct frame *f = XFRAME (w->frame);
4971 struct scroll_bar *bar
4972 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
4974 BLOCK_INPUT;
4976 #ifdef USE_TOOLKIT_SCROLL_BARS
4977 x_create_toolkit_scroll_bar (f, bar);
4978 #else /* not USE_TOOLKIT_SCROLL_BARS */
4980 XSetWindowAttributes a;
4981 unsigned long mask;
4982 Window window;
4984 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
4985 if (a.background_pixel == -1)
4986 a.background_pixel = f->output_data.x->background_pixel;
4988 a.event_mask = (ButtonPressMask | ButtonReleaseMask
4989 | ButtonMotionMask | PointerMotionHintMask
4990 | ExposureMask);
4991 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
4993 mask = (CWBackPixel | CWEventMask | CWCursor);
4995 /* Clear the area of W that will serve as a scroll bar. This is
4996 for the case that a window has been split horizontally. In
4997 this case, no clear_frame is generated to reduce flickering. */
4998 if (width > 0 && height > 0)
4999 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5000 left, top, width,
5001 window_box_height (w), False);
5003 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5004 /* Position and size of scroll bar. */
5005 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5006 top,
5007 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5008 height,
5009 /* Border width, depth, class, and visual. */
5011 CopyFromParent,
5012 CopyFromParent,
5013 CopyFromParent,
5014 /* Attributes. */
5015 mask, &a);
5016 SET_SCROLL_BAR_X_WINDOW (bar, window);
5018 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5020 XSETWINDOW (bar->window, w);
5021 XSETINT (bar->top, top);
5022 XSETINT (bar->left, left);
5023 XSETINT (bar->width, width);
5024 XSETINT (bar->height, height);
5025 XSETINT (bar->start, 0);
5026 XSETINT (bar->end, 0);
5027 bar->dragging = Qnil;
5029 /* Add bar to its frame's list of scroll bars. */
5030 bar->next = FRAME_SCROLL_BARS (f);
5031 bar->prev = Qnil;
5032 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5033 if (!NILP (bar->next))
5034 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5036 /* Map the window/widget. */
5037 #ifdef USE_TOOLKIT_SCROLL_BARS
5039 #ifdef USE_GTK
5040 xg_update_scrollbar_pos (f,
5041 SCROLL_BAR_X_WINDOW (bar),
5042 top,
5043 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5044 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5045 max (height, 1));
5046 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
5047 #else /* not USE_GTK */
5048 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
5049 XtConfigureWidget (scroll_bar,
5050 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5051 top,
5052 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5053 max (height, 1), 0);
5054 XtMapWidget (scroll_bar);
5055 #endif /* not USE_GTK */
5057 #else /* not USE_TOOLKIT_SCROLL_BARS */
5058 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
5059 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5061 UNBLOCK_INPUT;
5062 return bar;
5066 /* Draw BAR's handle in the proper position.
5068 If the handle is already drawn from START to END, don't bother
5069 redrawing it, unless REBUILD is non-zero; in that case, always
5070 redraw it. (REBUILD is handy for drawing the handle after expose
5071 events.)
5073 Normally, we want to constrain the start and end of the handle to
5074 fit inside its rectangle, but if the user is dragging the scroll
5075 bar handle, we want to let them drag it down all the way, so that
5076 the bar's top is as far down as it goes; otherwise, there's no way
5077 to move to the very end of the buffer. */
5079 #ifndef USE_TOOLKIT_SCROLL_BARS
5081 static void
5082 x_scroll_bar_set_handle (bar, start, end, rebuild)
5083 struct scroll_bar *bar;
5084 int start, end;
5085 int rebuild;
5087 int dragging = ! NILP (bar->dragging);
5088 Window w = SCROLL_BAR_X_WINDOW (bar);
5089 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5090 GC gc = f->output_data.x->normal_gc;
5092 /* If the display is already accurate, do nothing. */
5093 if (! rebuild
5094 && start == XINT (bar->start)
5095 && end == XINT (bar->end))
5096 return;
5098 BLOCK_INPUT;
5101 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
5102 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5103 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5105 /* Make sure the values are reasonable, and try to preserve
5106 the distance between start and end. */
5108 int length = end - start;
5110 if (start < 0)
5111 start = 0;
5112 else if (start > top_range)
5113 start = top_range;
5114 end = start + length;
5116 if (end < start)
5117 end = start;
5118 else if (end > top_range && ! dragging)
5119 end = top_range;
5122 /* Store the adjusted setting in the scroll bar. */
5123 XSETINT (bar->start, start);
5124 XSETINT (bar->end, end);
5126 /* Clip the end position, just for display. */
5127 if (end > top_range)
5128 end = top_range;
5130 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
5131 below top positions, to make sure the handle is always at least
5132 that many pixels tall. */
5133 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
5135 /* Draw the empty space above the handle. Note that we can't clear
5136 zero-height areas; that means "clear to end of window." */
5137 if (0 < start)
5138 x_clear_area (FRAME_X_DISPLAY (f), w,
5139 /* x, y, width, height, and exposures. */
5140 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5141 VERTICAL_SCROLL_BAR_TOP_BORDER,
5142 inside_width, start,
5143 False);
5145 /* Change to proper foreground color if one is specified. */
5146 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5147 XSetForeground (FRAME_X_DISPLAY (f), gc,
5148 f->output_data.x->scroll_bar_foreground_pixel);
5150 /* Draw the handle itself. */
5151 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
5152 /* x, y, width, height */
5153 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5154 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
5155 inside_width, end - start);
5157 /* Restore the foreground color of the GC if we changed it above. */
5158 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5159 XSetForeground (FRAME_X_DISPLAY (f), gc,
5160 f->output_data.x->foreground_pixel);
5162 /* Draw the empty space below the handle. Note that we can't
5163 clear zero-height areas; that means "clear to end of window." */
5164 if (end < inside_height)
5165 x_clear_area (FRAME_X_DISPLAY (f), w,
5166 /* x, y, width, height, and exposures. */
5167 VERTICAL_SCROLL_BAR_LEFT_BORDER,
5168 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
5169 inside_width, inside_height - end,
5170 False);
5174 UNBLOCK_INPUT;
5177 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5179 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
5180 nil. */
5182 static void
5183 x_scroll_bar_remove (bar)
5184 struct scroll_bar *bar;
5186 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5187 BLOCK_INPUT;
5189 #ifdef USE_TOOLKIT_SCROLL_BARS
5190 #ifdef USE_GTK
5191 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
5192 #else /* not USE_GTK */
5193 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
5194 #endif /* not USE_GTK */
5195 #else
5196 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
5197 #endif
5199 /* Disassociate this scroll bar from its window. */
5200 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
5202 UNBLOCK_INPUT;
5206 /* Set the handle of the vertical scroll bar for WINDOW to indicate
5207 that we are displaying PORTION characters out of a total of WHOLE
5208 characters, starting at POSITION. If WINDOW has no scroll bar,
5209 create one. */
5211 static void
5212 XTset_vertical_scroll_bar (w, portion, whole, position)
5213 struct window *w;
5214 int portion, whole, position;
5216 struct frame *f = XFRAME (w->frame);
5217 struct scroll_bar *bar;
5218 int top, height, left, sb_left, width, sb_width;
5219 int window_y, window_height;
5221 /* Get window dimensions. */
5222 window_box (w, -1, 0, &window_y, 0, &window_height);
5223 top = window_y;
5224 width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5225 height = window_height;
5227 /* Compute the left edge of the scroll bar area. */
5228 left = WINDOW_SCROLL_BAR_AREA_X (w);
5230 /* Compute the width of the scroll bar which might be less than
5231 the width of the area reserved for the scroll bar. */
5232 if (WINDOW_CONFIG_SCROLL_BAR_WIDTH (w) > 0)
5233 sb_width = WINDOW_CONFIG_SCROLL_BAR_WIDTH (w);
5234 else
5235 sb_width = width;
5237 /* Compute the left edge of the scroll bar. */
5238 #ifdef USE_TOOLKIT_SCROLL_BARS
5239 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5240 sb_left = (left +
5241 (WINDOW_RIGHTMOST_P (w)
5242 ? width - sb_width - (width - sb_width) / 2
5243 : 0));
5244 else
5245 sb_left = (left +
5246 (WINDOW_LEFTMOST_P (w)
5247 ? (width - sb_width) / 2
5248 : width - sb_width));
5249 #else
5250 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
5251 sb_left = left + width - sb_width;
5252 else
5253 sb_left = left;
5254 #endif
5256 /* Does the scroll bar exist yet? */
5257 if (NILP (w->vertical_scroll_bar))
5259 if (width > 0 && height > 0)
5261 BLOCK_INPUT;
5262 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5263 left, top, width, height, False);
5264 UNBLOCK_INPUT;
5267 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
5269 else
5271 /* It may just need to be moved and resized. */
5272 unsigned int mask = 0;
5274 bar = XSCROLL_BAR (w->vertical_scroll_bar);
5276 BLOCK_INPUT;
5278 if (sb_left != XINT (bar->left))
5279 mask |= CWX;
5280 if (top != XINT (bar->top))
5281 mask |= CWY;
5282 if (sb_width != XINT (bar->width))
5283 mask |= CWWidth;
5284 if (height != XINT (bar->height))
5285 mask |= CWHeight;
5287 #ifdef USE_TOOLKIT_SCROLL_BARS
5289 /* Move/size the scroll bar widget. */
5290 if (mask)
5292 /* Since toolkit scroll bars are smaller than the space reserved
5293 for them on the frame, we have to clear "under" them. */
5294 if (width > 0 && height > 0)
5295 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5296 left, top, width, height, False);
5297 #ifdef USE_GTK
5298 xg_update_scrollbar_pos (f,
5299 SCROLL_BAR_X_WINDOW (bar),
5300 top,
5301 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5302 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM *2,
5303 max (height, 1));
5304 #else /* not USE_GTK */
5305 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
5306 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5307 top,
5308 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
5309 max (height, 1), 0);
5310 #endif /* not USE_GTK */
5312 #else /* not USE_TOOLKIT_SCROLL_BARS */
5314 /* Clear areas not covered by the scroll bar because of
5315 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
5316 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
5318 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5319 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5320 height, False);
5321 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5322 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5323 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
5324 height, False);
5327 /* Clear areas not covered by the scroll bar because it's not as
5328 wide as the area reserved for it. This makes sure a
5329 previous mode line display is cleared after C-x 2 C-x 1, for
5330 example. */
5332 int area_width = WINDOW_CONFIG_SCROLL_BAR_COLS (w) * FRAME_COLUMN_WIDTH (f);
5333 int rest = area_width - sb_width;
5334 if (rest > 0 && height > 0)
5336 if (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
5337 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5338 left + area_width - rest, top,
5339 rest, height, False);
5340 else
5341 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5342 left, top, rest, height, False);
5346 /* Move/size the scroll bar window. */
5347 if (mask)
5349 XWindowChanges wc;
5351 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5352 wc.y = top;
5353 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
5354 wc.height = height;
5355 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
5356 mask, &wc);
5359 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5361 /* Remember new settings. */
5362 XSETINT (bar->left, sb_left);
5363 XSETINT (bar->top, top);
5364 XSETINT (bar->width, sb_width);
5365 XSETINT (bar->height, height);
5367 UNBLOCK_INPUT;
5370 #ifdef USE_TOOLKIT_SCROLL_BARS
5371 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
5372 #else /* not USE_TOOLKIT_SCROLL_BARS */
5373 /* Set the scroll bar's current state, unless we're currently being
5374 dragged. */
5375 if (NILP (bar->dragging))
5377 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
5379 if (whole == 0)
5380 x_scroll_bar_set_handle (bar, 0, top_range, 0);
5381 else
5383 int start = ((double) position * top_range) / whole;
5384 int end = ((double) (position + portion) * top_range) / whole;
5385 x_scroll_bar_set_handle (bar, start, end, 0);
5388 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5390 XSETVECTOR (w->vertical_scroll_bar, bar);
5394 /* The following three hooks are used when we're doing a thorough
5395 redisplay of the frame. We don't explicitly know which scroll bars
5396 are going to be deleted, because keeping track of when windows go
5397 away is a real pain - "Can you say set-window-configuration, boys
5398 and girls?" Instead, we just assert at the beginning of redisplay
5399 that *all* scroll bars are to be removed, and then save a scroll bar
5400 from the fiery pit when we actually redisplay its window. */
5402 /* Arrange for all scroll bars on FRAME to be removed at the next call
5403 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
5404 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
5406 static void
5407 XTcondemn_scroll_bars (frame)
5408 FRAME_PTR frame;
5410 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
5411 while (! NILP (FRAME_SCROLL_BARS (frame)))
5413 Lisp_Object bar;
5414 bar = FRAME_SCROLL_BARS (frame);
5415 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
5416 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
5417 XSCROLL_BAR (bar)->prev = Qnil;
5418 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
5419 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
5420 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
5425 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
5426 Note that WINDOW isn't necessarily condemned at all. */
5428 static void
5429 XTredeem_scroll_bar (window)
5430 struct window *window;
5432 struct scroll_bar *bar;
5433 struct frame *f;
5435 /* We can't redeem this window's scroll bar if it doesn't have one. */
5436 if (NILP (window->vertical_scroll_bar))
5437 abort ();
5439 bar = XSCROLL_BAR (window->vertical_scroll_bar);
5441 /* Unlink it from the condemned list. */
5442 f = XFRAME (WINDOW_FRAME (window));
5443 if (NILP (bar->prev))
5445 /* If the prev pointer is nil, it must be the first in one of
5446 the lists. */
5447 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
5448 /* It's not condemned. Everything's fine. */
5449 return;
5450 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
5451 window->vertical_scroll_bar))
5452 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
5453 else
5454 /* If its prev pointer is nil, it must be at the front of
5455 one or the other! */
5456 abort ();
5458 else
5459 XSCROLL_BAR (bar->prev)->next = bar->next;
5461 if (! NILP (bar->next))
5462 XSCROLL_BAR (bar->next)->prev = bar->prev;
5464 bar->next = FRAME_SCROLL_BARS (f);
5465 bar->prev = Qnil;
5466 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
5467 if (! NILP (bar->next))
5468 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
5471 /* Remove all scroll bars on FRAME that haven't been saved since the
5472 last call to `*condemn_scroll_bars_hook'. */
5474 static void
5475 XTjudge_scroll_bars (f)
5476 FRAME_PTR f;
5478 Lisp_Object bar, next;
5480 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
5482 /* Clear out the condemned list now so we won't try to process any
5483 more events on the hapless scroll bars. */
5484 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
5486 for (; ! NILP (bar); bar = next)
5488 struct scroll_bar *b = XSCROLL_BAR (bar);
5490 x_scroll_bar_remove (b);
5492 next = b->next;
5493 b->next = b->prev = Qnil;
5496 /* Now there should be no references to the condemned scroll bars,
5497 and they should get garbage-collected. */
5501 #ifndef USE_TOOLKIT_SCROLL_BARS
5502 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
5503 is a no-op when using toolkit scroll bars.
5505 This may be called from a signal handler, so we have to ignore GC
5506 mark bits. */
5508 static void
5509 x_scroll_bar_expose (bar, event)
5510 struct scroll_bar *bar;
5511 XEvent *event;
5513 Window w = SCROLL_BAR_X_WINDOW (bar);
5514 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5515 GC gc = f->output_data.x->normal_gc;
5516 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5518 BLOCK_INPUT;
5520 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
5522 /* Switch to scroll bar foreground color. */
5523 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5524 XSetForeground (FRAME_X_DISPLAY (f), gc,
5525 f->output_data.x->scroll_bar_foreground_pixel);
5527 /* Draw a one-pixel border just inside the edges of the scroll bar. */
5528 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
5530 /* x, y, width, height */
5531 0, 0,
5532 XINT (bar->width) - 1 - width_trim - width_trim,
5533 XINT (bar->height) - 1);
5535 /* Restore the foreground color of the GC if we changed it above. */
5536 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
5537 XSetForeground (FRAME_X_DISPLAY (f), gc,
5538 f->output_data.x->foreground_pixel);
5540 UNBLOCK_INPUT;
5543 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5545 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
5546 is set to something other than NO_EVENT, it is enqueued.
5548 This may be called from a signal handler, so we have to ignore GC
5549 mark bits. */
5552 static void
5553 x_scroll_bar_handle_click (bar, event, emacs_event)
5554 struct scroll_bar *bar;
5555 XEvent *event;
5556 struct input_event *emacs_event;
5558 if (! WINDOWP (bar->window))
5559 abort ();
5561 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
5562 emacs_event->code = event->xbutton.button - Button1;
5563 emacs_event->modifiers
5564 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
5565 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
5566 event->xbutton.state)
5567 | (event->type == ButtonRelease
5568 ? up_modifier
5569 : down_modifier));
5570 emacs_event->frame_or_window = bar->window;
5571 emacs_event->arg = Qnil;
5572 emacs_event->timestamp = event->xbutton.time;
5574 #if 0
5575 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5576 int internal_height
5577 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5578 #endif
5579 int top_range
5580 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5581 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
5583 if (y < 0) y = 0;
5584 if (y > top_range) y = top_range;
5586 if (y < XINT (bar->start))
5587 emacs_event->part = scroll_bar_above_handle;
5588 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5589 emacs_event->part = scroll_bar_handle;
5590 else
5591 emacs_event->part = scroll_bar_below_handle;
5593 /* Just because the user has clicked on the handle doesn't mean
5594 they want to drag it. Lisp code needs to be able to decide
5595 whether or not we're dragging. */
5596 #if 0
5597 /* If the user has just clicked on the handle, record where they're
5598 holding it. */
5599 if (event->type == ButtonPress
5600 && emacs_event->part == scroll_bar_handle)
5601 XSETINT (bar->dragging, y - XINT (bar->start));
5602 #endif
5604 #ifndef USE_TOOLKIT_SCROLL_BARS
5605 /* If the user has released the handle, set it to its final position. */
5606 if (event->type == ButtonRelease
5607 && ! NILP (bar->dragging))
5609 int new_start = y - XINT (bar->dragging);
5610 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5612 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5613 bar->dragging = Qnil;
5615 #endif
5617 /* Same deal here as the other #if 0. */
5618 #if 0
5619 /* Clicks on the handle are always reported as occurring at the top of
5620 the handle. */
5621 if (emacs_event->part == scroll_bar_handle)
5622 emacs_event->x = bar->start;
5623 else
5624 XSETINT (emacs_event->x, y);
5625 #else
5626 XSETINT (emacs_event->x, y);
5627 #endif
5629 XSETINT (emacs_event->y, top_range);
5633 #ifndef USE_TOOLKIT_SCROLL_BARS
5635 /* Handle some mouse motion while someone is dragging the scroll bar.
5637 This may be called from a signal handler, so we have to ignore GC
5638 mark bits. */
5640 static void
5641 x_scroll_bar_note_movement (bar, event)
5642 struct scroll_bar *bar;
5643 XEvent *event;
5645 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
5647 last_mouse_movement_time = event->xmotion.time;
5649 f->mouse_moved = 1;
5650 XSETVECTOR (last_mouse_scroll_bar, bar);
5652 /* If we're dragging the bar, display it. */
5653 if (! NILP (bar->dragging))
5655 /* Where should the handle be now? */
5656 int new_start = event->xmotion.y - XINT (bar->dragging);
5658 if (new_start != XINT (bar->start))
5660 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
5662 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
5667 #endif /* !USE_TOOLKIT_SCROLL_BARS */
5669 /* Return information to the user about the current position of the mouse
5670 on the scroll bar. */
5672 static void
5673 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
5674 FRAME_PTR *fp;
5675 Lisp_Object *bar_window;
5676 enum scroll_bar_part *part;
5677 Lisp_Object *x, *y;
5678 unsigned long *time;
5680 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
5681 Window w = SCROLL_BAR_X_WINDOW (bar);
5682 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
5683 int win_x, win_y;
5684 Window dummy_window;
5685 int dummy_coord;
5686 unsigned int dummy_mask;
5688 BLOCK_INPUT;
5690 /* Get the mouse's position relative to the scroll bar window, and
5691 report that. */
5692 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
5694 /* Root, child, root x and root y. */
5695 &dummy_window, &dummy_window,
5696 &dummy_coord, &dummy_coord,
5698 /* Position relative to scroll bar. */
5699 &win_x, &win_y,
5701 /* Mouse buttons and modifier keys. */
5702 &dummy_mask))
5704 else
5706 #if 0
5707 int inside_height
5708 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
5709 #endif
5710 int top_range
5711 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
5713 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
5715 if (! NILP (bar->dragging))
5716 win_y -= XINT (bar->dragging);
5718 if (win_y < 0)
5719 win_y = 0;
5720 if (win_y > top_range)
5721 win_y = top_range;
5723 *fp = f;
5724 *bar_window = bar->window;
5726 if (! NILP (bar->dragging))
5727 *part = scroll_bar_handle;
5728 else if (win_y < XINT (bar->start))
5729 *part = scroll_bar_above_handle;
5730 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
5731 *part = scroll_bar_handle;
5732 else
5733 *part = scroll_bar_below_handle;
5735 XSETINT (*x, win_y);
5736 XSETINT (*y, top_range);
5738 f->mouse_moved = 0;
5739 last_mouse_scroll_bar = Qnil;
5742 *time = last_mouse_movement_time;
5744 UNBLOCK_INPUT;
5748 /* The screen has been cleared so we may have changed foreground or
5749 background colors, and the scroll bars may need to be redrawn.
5750 Clear out the scroll bars, and ask for expose events, so we can
5751 redraw them. */
5753 void
5754 x_scroll_bar_clear (f)
5755 FRAME_PTR f;
5757 #ifndef USE_TOOLKIT_SCROLL_BARS
5758 Lisp_Object bar;
5760 /* We can have scroll bars even if this is 0,
5761 if we just turned off scroll bar mode.
5762 But in that case we should not clear them. */
5763 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5764 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
5765 bar = XSCROLL_BAR (bar)->next)
5766 XClearArea (FRAME_X_DISPLAY (f),
5767 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
5768 0, 0, 0, 0, True);
5769 #endif /* not USE_TOOLKIT_SCROLL_BARS */
5773 /* The main X event-reading loop - XTread_socket. */
5775 #if 0
5776 /* Time stamp of enter window event. This is only used by XTread_socket,
5777 but we have to put it out here, since static variables within functions
5778 sometimes don't work. */
5780 static Time enter_timestamp;
5781 #endif
5783 /* This holds the state XLookupString needs to implement dead keys
5784 and other tricks known as "compose processing". _X Window System_
5785 says that a portable program can't use this, but Stephen Gildea assures
5786 me that letting the compiler initialize it to zeros will work okay.
5788 This must be defined outside of XTread_socket, for the same reasons
5789 given for enter_timestamp, above. */
5791 static XComposeStatus compose_status;
5793 /* Record the last 100 characters stored
5794 to help debug the loss-of-chars-during-GC problem. */
5796 static int temp_index;
5797 static short temp_buffer[100];
5799 #define STORE_KEYSYM_FOR_DEBUG(keysym) \
5800 if (temp_index == sizeof temp_buffer / sizeof (short)) \
5801 temp_index = 0; \
5802 temp_buffer[temp_index++] = (keysym)
5804 /* Set this to nonzero to fake an "X I/O error"
5805 on a particular display. */
5807 struct x_display_info *XTread_socket_fake_io_error;
5809 /* When we find no input here, we occasionally do a no-op command
5810 to verify that the X server is still running and we can still talk with it.
5811 We try all the open displays, one by one.
5812 This variable is used for cycling thru the displays. */
5814 static struct x_display_info *next_noop_dpyinfo;
5816 #define SET_SAVED_MENU_EVENT(size) \
5817 do \
5819 if (f->output_data.x->saved_menu_event == 0) \
5820 f->output_data.x->saved_menu_event \
5821 = (XEvent *) xmalloc (sizeof (XEvent)); \
5822 bcopy (&event, f->output_data.x->saved_menu_event, size); \
5823 inev.ie.kind = MENU_BAR_ACTIVATE_EVENT; \
5824 XSETFRAME (inev.ie.frame_or_window, f); \
5826 while (0)
5828 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
5829 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
5832 enum
5834 X_EVENT_NORMAL,
5835 X_EVENT_GOTO_OUT,
5836 X_EVENT_DROP
5839 /* Filter events for the current X input method.
5840 DPYINFO is the display this event is for.
5841 EVENT is the X event to filter.
5843 Returns non-zero if the event was filtered, caller shall not process
5844 this event further.
5845 Returns zero if event is wasn't filtered. */
5847 #ifdef HAVE_X_I18N
5848 static int
5849 x_filter_event (dpyinfo, event)
5850 struct x_display_info *dpyinfo;
5851 XEvent *event;
5853 /* XFilterEvent returns non-zero if the input method has
5854 consumed the event. We pass the frame's X window to
5855 XFilterEvent because that's the one for which the IC
5856 was created. */
5858 struct frame *f1 = x_any_window_to_frame (dpyinfo,
5859 event->xclient.window);
5861 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
5863 #endif
5865 #ifdef USE_GTK
5866 static int current_count;
5867 static int current_finish;
5868 static struct input_event *current_hold_quit;
5870 /* This is the filter function invoked by the GTK event loop.
5871 It is invoked before the XEvent is translated to a GdkEvent,
5872 so we have a chance to act on the event before GTK. */
5873 static GdkFilterReturn
5874 event_handler_gdk (gxev, ev, data)
5875 GdkXEvent *gxev;
5876 GdkEvent *ev;
5877 gpointer data;
5879 XEvent *xev = (XEvent *) gxev;
5881 if (current_count >= 0)
5883 struct x_display_info *dpyinfo;
5885 dpyinfo = x_display_info_for_display (xev->xany.display);
5887 #ifdef HAVE_X_I18N
5888 /* Filter events for the current X input method.
5889 GTK calls XFilterEvent but not for key press and release,
5890 so we do it here. */
5891 if (xev->type == KeyPress || xev->type == KeyRelease)
5892 if (dpyinfo && x_filter_event (dpyinfo, xev))
5893 return GDK_FILTER_REMOVE;
5894 #endif
5896 if (! dpyinfo)
5897 current_finish = X_EVENT_NORMAL;
5898 else
5900 current_count +=
5901 handle_one_xevent (dpyinfo, xev, &current_finish,
5902 current_hold_quit);
5905 else
5906 current_finish = x_dispatch_event (xev, xev->xany.display);
5908 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
5909 return GDK_FILTER_REMOVE;
5911 return GDK_FILTER_CONTINUE;
5913 #endif /* USE_GTK */
5916 /* Handles the XEvent EVENT on display DPYINFO.
5918 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
5919 *FINISH is zero if caller should continue reading events.
5920 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
5922 We return the number of characters stored into the buffer. */
5924 static int
5925 handle_one_xevent (dpyinfo, eventp, finish, hold_quit)
5926 struct x_display_info *dpyinfo;
5927 XEvent *eventp;
5928 int *finish;
5929 struct input_event *hold_quit;
5931 union {
5932 struct input_event ie;
5933 struct selection_input_event sie;
5934 } inev;
5935 int count = 0;
5936 int do_help = 0;
5937 int nbytes = 0;
5938 struct frame *f;
5939 struct coding_system coding;
5940 XEvent event = *eventp;
5942 *finish = X_EVENT_NORMAL;
5944 EVENT_INIT (inev.ie);
5945 inev.ie.kind = NO_EVENT;
5946 inev.ie.arg = Qnil;
5948 switch (event.type)
5950 case ClientMessage:
5952 if (event.xclient.message_type
5953 == dpyinfo->Xatom_wm_protocols
5954 && event.xclient.format == 32)
5956 if (event.xclient.data.l[0]
5957 == dpyinfo->Xatom_wm_take_focus)
5959 /* Use x_any_window_to_frame because this
5960 could be the shell widget window
5961 if the frame has no title bar. */
5962 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
5963 #ifdef HAVE_X_I18N
5964 /* Not quite sure this is needed -pd */
5965 if (f && FRAME_XIC (f))
5966 XSetICFocus (FRAME_XIC (f));
5967 #endif
5968 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
5969 instructs the WM to set the input focus automatically for
5970 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
5971 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
5972 it has set the focus. So, XSetInputFocus below is not
5973 needed.
5975 The call to XSetInputFocus below has also caused trouble. In
5976 cases where the XSetInputFocus done by the WM and the one
5977 below are temporally close (on a fast machine), the call
5978 below can generate additional FocusIn events which confuse
5979 Emacs. */
5981 /* Since we set WM_TAKE_FOCUS, we must call
5982 XSetInputFocus explicitly. But not if f is null,
5983 since that might be an event for a deleted frame. */
5984 if (f)
5986 Display *d = event.xclient.display;
5987 /* Catch and ignore errors, in case window has been
5988 iconified by a window manager such as GWM. */
5989 x_catch_errors (d);
5990 XSetInputFocus (d, event.xclient.window,
5991 /* The ICCCM says this is
5992 the only valid choice. */
5993 RevertToParent,
5994 event.xclient.data.l[1]);
5995 /* This is needed to detect the error
5996 if there is an error. */
5997 XSync (d, False);
5998 x_uncatch_errors ();
6000 /* Not certain about handling scroll bars here */
6001 #endif /* 0 */
6002 goto done;
6005 if (event.xclient.data.l[0]
6006 == dpyinfo->Xatom_wm_save_yourself)
6008 /* Save state modify the WM_COMMAND property to
6009 something which can reinstate us. This notifies
6010 the session manager, who's looking for such a
6011 PropertyNotify. Can restart processing when
6012 a keyboard or mouse event arrives. */
6013 /* If we have a session manager, don't set this.
6014 KDE will then start two Emacsen, one for the
6015 session manager and one for this. */
6016 #ifdef HAVE_X_SM
6017 if (! x_session_have_connection ())
6018 #endif
6020 f = x_top_window_to_frame (dpyinfo,
6021 event.xclient.window);
6022 /* This is just so we only give real data once
6023 for a single Emacs process. */
6024 if (f == SELECTED_FRAME ())
6025 XSetCommand (FRAME_X_DISPLAY (f),
6026 event.xclient.window,
6027 initial_argv, initial_argc);
6028 else if (f)
6029 XSetCommand (FRAME_X_DISPLAY (f),
6030 event.xclient.window,
6031 0, 0);
6033 goto done;
6036 if (event.xclient.data.l[0]
6037 == dpyinfo->Xatom_wm_delete_window)
6039 f = x_any_window_to_frame (dpyinfo,
6040 event.xclient.window);
6041 if (!f)
6042 goto OTHER; /* May be a dialog that is to be removed */
6044 inev.ie.kind = DELETE_WINDOW_EVENT;
6045 XSETFRAME (inev.ie.frame_or_window, f);
6046 goto done;
6049 goto done;
6052 if (event.xclient.message_type
6053 == dpyinfo->Xatom_wm_configure_denied)
6055 goto done;
6058 if (event.xclient.message_type
6059 == dpyinfo->Xatom_wm_window_moved)
6061 int new_x, new_y;
6062 f = x_window_to_frame (dpyinfo, event.xclient.window);
6064 new_x = event.xclient.data.s[0];
6065 new_y = event.xclient.data.s[1];
6067 if (f)
6069 f->left_pos = new_x;
6070 f->top_pos = new_y;
6072 goto done;
6075 #ifdef HACK_EDITRES
6076 if (event.xclient.message_type
6077 == dpyinfo->Xatom_editres)
6079 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6080 if (f)
6081 _XEditResCheckMessages (f->output_data.x->widget, NULL,
6082 &event, NULL);
6083 goto done;
6085 #endif /* HACK_EDITRES */
6087 if ((event.xclient.message_type
6088 == dpyinfo->Xatom_DONE)
6089 || (event.xclient.message_type
6090 == dpyinfo->Xatom_PAGE))
6092 /* Ghostview job completed. Kill it. We could
6093 reply with "Next" if we received "Page", but we
6094 currently never do because we are interested in
6095 images, only, which should have 1 page. */
6096 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
6097 f = x_window_to_frame (dpyinfo, event.xclient.window);
6098 if (!f)
6099 goto OTHER;
6100 x_kill_gs_process (pixmap, f);
6101 expose_frame (f, 0, 0, 0, 0);
6102 goto done;
6105 #ifdef USE_TOOLKIT_SCROLL_BARS
6106 /* Scroll bar callbacks send a ClientMessage from which
6107 we construct an input_event. */
6108 if (event.xclient.message_type
6109 == dpyinfo->Xatom_Scrollbar)
6111 x_scroll_bar_to_input_event (&event, &inev.ie);
6112 *finish = X_EVENT_GOTO_OUT;
6113 goto done;
6115 #endif /* USE_TOOLKIT_SCROLL_BARS */
6117 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
6118 if (!f)
6119 goto OTHER;
6120 if (x_handle_dnd_message (f, &event.xclient, dpyinfo, &inev.ie))
6121 *finish = X_EVENT_DROP;
6123 break;
6125 case SelectionNotify:
6126 last_user_time = event.xselection.time;
6127 #ifdef USE_X_TOOLKIT
6128 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
6129 goto OTHER;
6130 #endif /* not USE_X_TOOLKIT */
6131 x_handle_selection_notify (&event.xselection);
6132 break;
6134 case SelectionClear: /* Someone has grabbed ownership. */
6135 last_user_time = event.xselectionclear.time;
6136 #ifdef USE_X_TOOLKIT
6137 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
6138 goto OTHER;
6139 #endif /* USE_X_TOOLKIT */
6141 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
6143 inev.ie.kind = SELECTION_CLEAR_EVENT;
6144 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6145 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6146 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6147 inev.ie.frame_or_window = Qnil;
6149 break;
6151 case SelectionRequest: /* Someone wants our selection. */
6152 last_user_time = event.xselectionrequest.time;
6153 #ifdef USE_X_TOOLKIT
6154 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
6155 goto OTHER;
6156 #endif /* USE_X_TOOLKIT */
6158 XSelectionRequestEvent *eventp
6159 = (XSelectionRequestEvent *) &event;
6161 inev.ie.kind = SELECTION_REQUEST_EVENT;
6162 SELECTION_EVENT_DISPLAY (&inev.sie) = eventp->display;
6163 SELECTION_EVENT_REQUESTOR (&inev.sie) = eventp->requestor;
6164 SELECTION_EVENT_SELECTION (&inev.sie) = eventp->selection;
6165 SELECTION_EVENT_TARGET (&inev.sie) = eventp->target;
6166 SELECTION_EVENT_PROPERTY (&inev.sie) = eventp->property;
6167 SELECTION_EVENT_TIME (&inev.sie) = eventp->time;
6168 inev.ie.frame_or_window = Qnil;
6170 break;
6172 case PropertyNotify:
6173 last_user_time = event.xproperty.time;
6174 #if 0 /* This is plain wrong. In the case that we are waiting for a
6175 PropertyNotify used as an ACK in incremental selection
6176 transfer, the property will be on the receiver's window. */
6177 #if defined USE_X_TOOLKIT
6178 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
6179 goto OTHER;
6180 #endif
6181 #endif
6182 x_handle_property_notify (&event.xproperty);
6183 goto OTHER;
6185 case ReparentNotify:
6186 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
6187 if (f)
6189 int x, y;
6190 f->output_data.x->parent_desc = event.xreparent.parent;
6191 x_real_positions (f, &x, &y);
6192 f->left_pos = x;
6193 f->top_pos = y;
6195 /* Perhaps reparented due to a WM restart. Reset this. */
6196 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_UNKNOWN;
6197 FRAME_X_DISPLAY_INFO (f)->net_supported_window = 0;
6199 goto OTHER;
6201 case Expose:
6202 f = x_window_to_frame (dpyinfo, event.xexpose.window);
6203 if (f)
6205 x_check_fullscreen (f);
6207 #ifdef USE_GTK
6208 /* This seems to be needed for GTK 2.6. */
6209 x_clear_area (event.xexpose.display,
6210 event.xexpose.window,
6211 event.xexpose.x, event.xexpose.y,
6212 event.xexpose.width, event.xexpose.height,
6213 FALSE);
6214 #endif
6215 if (f->async_visible == 0)
6217 f->async_visible = 1;
6218 f->async_iconified = 0;
6219 f->output_data.x->has_been_visible = 1;
6220 SET_FRAME_GARBAGED (f);
6222 else
6223 expose_frame (f,
6224 event.xexpose.x, event.xexpose.y,
6225 event.xexpose.width, event.xexpose.height);
6227 else
6229 #ifndef USE_TOOLKIT_SCROLL_BARS
6230 struct scroll_bar *bar;
6231 #endif
6232 #if defined USE_LUCID
6233 /* Submenus of the Lucid menu bar aren't widgets
6234 themselves, so there's no way to dispatch events
6235 to them. Recognize this case separately. */
6237 Widget widget
6238 = x_window_to_menu_bar (event.xexpose.window);
6239 if (widget)
6240 xlwmenu_redisplay (widget);
6242 #endif /* USE_LUCID */
6244 #ifdef USE_TOOLKIT_SCROLL_BARS
6245 /* Dispatch event to the widget. */
6246 goto OTHER;
6247 #else /* not USE_TOOLKIT_SCROLL_BARS */
6248 bar = x_window_to_scroll_bar (event.xexpose.display,
6249 event.xexpose.window);
6251 if (bar)
6252 x_scroll_bar_expose (bar, &event);
6253 #ifdef USE_X_TOOLKIT
6254 else
6255 goto OTHER;
6256 #endif /* USE_X_TOOLKIT */
6257 #endif /* not USE_TOOLKIT_SCROLL_BARS */
6259 break;
6261 case GraphicsExpose: /* This occurs when an XCopyArea's
6262 source area was obscured or not
6263 available. */
6264 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
6265 if (f)
6267 expose_frame (f,
6268 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
6269 event.xgraphicsexpose.width,
6270 event.xgraphicsexpose.height);
6272 #ifdef USE_X_TOOLKIT
6273 else
6274 goto OTHER;
6275 #endif /* USE_X_TOOLKIT */
6276 break;
6278 case NoExpose: /* This occurs when an XCopyArea's
6279 source area was completely
6280 available. */
6281 break;
6283 case UnmapNotify:
6284 /* Redo the mouse-highlight after the tooltip has gone. */
6285 if (event.xmap.window == tip_window)
6287 tip_window = 0;
6288 redo_mouse_highlight ();
6291 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
6292 if (f) /* F may no longer exist if
6293 the frame was deleted. */
6295 /* While a frame is unmapped, display generation is
6296 disabled; you don't want to spend time updating a
6297 display that won't ever be seen. */
6298 f->async_visible = 0;
6299 /* We can't distinguish, from the event, whether the window
6300 has become iconified or invisible. So assume, if it
6301 was previously visible, than now it is iconified.
6302 But x_make_frame_invisible clears both
6303 the visible flag and the iconified flag;
6304 and that way, we know the window is not iconified now. */
6305 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
6307 f->async_iconified = 1;
6309 inev.ie.kind = ICONIFY_EVENT;
6310 XSETFRAME (inev.ie.frame_or_window, f);
6313 goto OTHER;
6315 case MapNotify:
6316 if (event.xmap.window == tip_window)
6317 /* The tooltip has been drawn already. Avoid
6318 the SET_FRAME_GARBAGED below. */
6319 goto OTHER;
6321 /* We use x_top_window_to_frame because map events can
6322 come for sub-windows and they don't mean that the
6323 frame is visible. */
6324 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
6325 if (f)
6327 /* wait_reading_process_output will notice this and update
6328 the frame's display structures.
6329 If we where iconified, we should not set garbaged,
6330 because that stops redrawing on Expose events. This looks
6331 bad if we are called from a recursive event loop
6332 (x_dispatch_event), for example when a dialog is up. */
6333 if (! f->async_iconified)
6334 SET_FRAME_GARBAGED (f);
6336 f->async_visible = 1;
6337 f->async_iconified = 0;
6338 f->output_data.x->has_been_visible = 1;
6340 if (f->iconified)
6342 inev.ie.kind = DEICONIFY_EVENT;
6343 XSETFRAME (inev.ie.frame_or_window, f);
6345 else if (! NILP (Vframe_list)
6346 && ! NILP (XCDR (Vframe_list)))
6347 /* Force a redisplay sooner or later
6348 to update the frame titles
6349 in case this is the second frame. */
6350 record_asynch_buffer_change ();
6352 goto OTHER;
6354 case KeyPress:
6356 last_user_time = event.xkey.time;
6357 ignore_next_mouse_click_timeout = 0;
6359 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6360 /* Dispatch KeyPress events when in menu. */
6361 if (popup_activated ())
6362 goto OTHER;
6363 #endif
6365 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
6367 /* If mouse-highlight is an integer, input clears out
6368 mouse highlighting. */
6369 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight)
6370 && (f == 0
6371 || !EQ (f->tool_bar_window, dpyinfo->mouse_face_window)))
6373 clear_mouse_face (dpyinfo);
6374 dpyinfo->mouse_face_hidden = 1;
6377 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
6378 if (f == 0)
6380 /* Scroll bars consume key events, but we want
6381 the keys to go to the scroll bar's frame. */
6382 Widget widget = XtWindowToWidget (dpyinfo->display,
6383 event.xkey.window);
6384 if (widget && XmIsScrollBar (widget))
6386 widget = XtParent (widget);
6387 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
6390 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
6392 if (f != 0)
6394 KeySym keysym, orig_keysym;
6395 /* al%imercury@uunet.uu.net says that making this 81
6396 instead of 80 fixed a bug whereby meta chars made
6397 his Emacs hang.
6399 It seems that some version of XmbLookupString has
6400 a bug of not returning XBufferOverflow in
6401 status_return even if the input is too long to
6402 fit in 81 bytes. So, we must prepare sufficient
6403 bytes for copy_buffer. 513 bytes (256 chars for
6404 two-byte character set) seems to be a fairly good
6405 approximation. -- 2000.8.10 handa@etl.go.jp */
6406 unsigned char copy_buffer[513];
6407 unsigned char *copy_bufptr = copy_buffer;
6408 int copy_bufsiz = sizeof (copy_buffer);
6409 int modifiers;
6410 Lisp_Object coding_system = Qlatin_1;
6411 Lisp_Object c;
6413 #ifdef USE_GTK
6414 /* Don't pass keys to GTK. A Tab will shift focus to the
6415 tool bar in GTK 2.4. Keys will still go to menus and
6416 dialogs because in that case popup_activated is TRUE
6417 (see above). */
6418 *finish = X_EVENT_DROP;
6419 #endif
6421 event.xkey.state
6422 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
6423 extra_keyboard_modifiers);
6424 modifiers = event.xkey.state;
6426 /* This will have to go some day... */
6428 /* make_lispy_event turns chars into control chars.
6429 Don't do it here because XLookupString is too eager. */
6430 event.xkey.state &= ~ControlMask;
6431 event.xkey.state &= ~(dpyinfo->meta_mod_mask
6432 | dpyinfo->super_mod_mask
6433 | dpyinfo->hyper_mod_mask
6434 | dpyinfo->alt_mod_mask);
6436 /* In case Meta is ComposeCharacter,
6437 clear its status. According to Markus Ehrnsperger
6438 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
6439 this enables ComposeCharacter to work whether or
6440 not it is combined with Meta. */
6441 if (modifiers & dpyinfo->meta_mod_mask)
6442 bzero (&compose_status, sizeof (compose_status));
6444 #ifdef HAVE_X_I18N
6445 if (FRAME_XIC (f))
6447 Status status_return;
6449 coding_system = Vlocale_coding_system;
6450 nbytes = XmbLookupString (FRAME_XIC (f),
6451 &event.xkey, copy_bufptr,
6452 copy_bufsiz, &keysym,
6453 &status_return);
6454 if (status_return == XBufferOverflow)
6456 copy_bufsiz = nbytes + 1;
6457 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6458 nbytes = XmbLookupString (FRAME_XIC (f),
6459 &event.xkey, copy_bufptr,
6460 copy_bufsiz, &keysym,
6461 &status_return);
6463 /* Xutf8LookupString is a new but already deprecated interface. -stef */
6464 #if 0 && defined X_HAVE_UTF8_STRING
6465 else if (status_return == XLookupKeySym)
6466 { /* Try again but with utf-8. */
6467 coding_system = Qutf_8;
6468 nbytes = Xutf8LookupString (FRAME_XIC (f),
6469 &event.xkey, copy_bufptr,
6470 copy_bufsiz, &keysym,
6471 &status_return);
6472 if (status_return == XBufferOverflow)
6474 copy_bufsiz = nbytes + 1;
6475 copy_bufptr = (unsigned char *) alloca (copy_bufsiz);
6476 nbytes = Xutf8LookupString (FRAME_XIC (f),
6477 &event.xkey,
6478 copy_bufptr,
6479 copy_bufsiz, &keysym,
6480 &status_return);
6483 #endif
6485 if (status_return == XLookupNone)
6486 break;
6487 else if (status_return == XLookupChars)
6489 keysym = NoSymbol;
6490 modifiers = 0;
6492 else if (status_return != XLookupKeySym
6493 && status_return != XLookupBoth)
6494 abort ();
6496 else
6497 nbytes = XLookupString (&event.xkey, copy_bufptr,
6498 copy_bufsiz, &keysym,
6499 &compose_status);
6500 #else
6501 nbytes = XLookupString (&event.xkey, copy_bufptr,
6502 copy_bufsiz, &keysym,
6503 &compose_status);
6504 #endif
6506 /* If not using XIM/XIC, and a compose sequence is in progress,
6507 we break here. Otherwise, chars_matched is always 0. */
6508 if (compose_status.chars_matched > 0 && nbytes == 0)
6509 break;
6511 bzero (&compose_status, sizeof (compose_status));
6512 orig_keysym = keysym;
6514 /* Common for all keysym input events. */
6515 XSETFRAME (inev.ie.frame_or_window, f);
6516 inev.ie.modifiers
6517 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f), modifiers);
6518 inev.ie.timestamp = event.xkey.time;
6520 /* First deal with keysyms which have defined
6521 translations to characters. */
6522 if (keysym >= 32 && keysym < 128)
6523 /* Avoid explicitly decoding each ASCII character. */
6525 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6526 inev.ie.code = keysym;
6527 goto done_keysym;
6530 /* Keysyms directly mapped to Unicode characters. */
6531 if (keysym >= 0x01000000 && keysym <= 0x0110FFFF)
6533 if (keysym < 0x01000080)
6534 inev.ie.kind = ASCII_KEYSTROKE_EVENT;
6535 else
6536 inev.ie.kind = MULTIBYTE_CHAR_KEYSTROKE_EVENT;
6537 inev.ie.code = keysym & 0xFFFFFF;
6538 goto done_keysym;
6541 /* Now non-ASCII. */
6542 if (HASH_TABLE_P (Vx_keysym_table)
6543 && (NATNUMP (c = Fgethash (make_number (keysym),
6544 Vx_keysym_table,
6545 Qnil))))
6547 inev.ie.kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
6548 ? ASCII_KEYSTROKE_EVENT
6549 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6550 inev.ie.code = XFASTINT (c);
6551 goto done_keysym;
6554 /* Random non-modifier sorts of keysyms. */
6555 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
6556 || keysym == XK_Delete
6557 #ifdef XK_ISO_Left_Tab
6558 || (keysym >= XK_ISO_Left_Tab
6559 && keysym <= XK_ISO_Enter)
6560 #endif
6561 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
6562 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
6563 #ifdef HPUX
6564 /* This recognizes the "extended function
6565 keys". It seems there's no cleaner way.
6566 Test IsModifierKey to avoid handling
6567 mode_switch incorrectly. */
6568 || ((unsigned) (keysym) >= XK_Select
6569 && (unsigned)(keysym) < XK_KP_Space)
6570 #endif
6571 #ifdef XK_dead_circumflex
6572 || orig_keysym == XK_dead_circumflex
6573 #endif
6574 #ifdef XK_dead_grave
6575 || orig_keysym == XK_dead_grave
6576 #endif
6577 #ifdef XK_dead_tilde
6578 || orig_keysym == XK_dead_tilde
6579 #endif
6580 #ifdef XK_dead_diaeresis
6581 || orig_keysym == XK_dead_diaeresis
6582 #endif
6583 #ifdef XK_dead_macron
6584 || orig_keysym == XK_dead_macron
6585 #endif
6586 #ifdef XK_dead_degree
6587 || orig_keysym == XK_dead_degree
6588 #endif
6589 #ifdef XK_dead_acute
6590 || orig_keysym == XK_dead_acute
6591 #endif
6592 #ifdef XK_dead_cedilla
6593 || orig_keysym == XK_dead_cedilla
6594 #endif
6595 #ifdef XK_dead_breve
6596 || orig_keysym == XK_dead_breve
6597 #endif
6598 #ifdef XK_dead_ogonek
6599 || orig_keysym == XK_dead_ogonek
6600 #endif
6601 #ifdef XK_dead_caron
6602 || orig_keysym == XK_dead_caron
6603 #endif
6604 #ifdef XK_dead_doubleacute
6605 || orig_keysym == XK_dead_doubleacute
6606 #endif
6607 #ifdef XK_dead_abovedot
6608 || orig_keysym == XK_dead_abovedot
6609 #endif
6610 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
6611 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
6612 /* Any "vendor-specific" key is ok. */
6613 || (orig_keysym & (1 << 28))
6614 || (keysym != NoSymbol && nbytes == 0))
6615 && ! (IsModifierKey (orig_keysym)
6616 #ifndef HAVE_X11R5
6617 #ifdef XK_Mode_switch
6618 || ((unsigned)(orig_keysym) == XK_Mode_switch)
6619 #endif
6620 #ifdef XK_Num_Lock
6621 || ((unsigned)(orig_keysym) == XK_Num_Lock)
6622 #endif
6623 #endif /* not HAVE_X11R5 */
6624 /* The symbols from XK_ISO_Lock
6625 to XK_ISO_Last_Group_Lock
6626 don't have real modifiers but
6627 should be treated similarly to
6628 Mode_switch by Emacs. */
6629 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
6630 || ((unsigned)(orig_keysym)
6631 >= XK_ISO_Lock
6632 && (unsigned)(orig_keysym)
6633 <= XK_ISO_Last_Group_Lock)
6634 #endif
6637 STORE_KEYSYM_FOR_DEBUG (keysym);
6638 /* make_lispy_event will convert this to a symbolic
6639 key. */
6640 inev.ie.kind = NON_ASCII_KEYSTROKE_EVENT;
6641 inev.ie.code = keysym;
6642 goto done_keysym;
6645 { /* Raw bytes, not keysym. */
6646 register int i;
6647 register int c;
6648 int nchars, len;
6650 for (i = 0, nchars = 0; i < nbytes; i++)
6652 if (ASCII_BYTE_P (copy_bufptr[i]))
6653 nchars++;
6654 STORE_KEYSYM_FOR_DEBUG (copy_bufptr[i]);
6657 if (nchars < nbytes)
6659 /* Decode the input data. */
6660 int require;
6661 unsigned char *p;
6663 /* The input should be decoded with `coding_system'
6664 which depends on which X*LookupString function
6665 we used just above and the locale. */
6666 setup_coding_system (coding_system, &coding);
6667 coding.src_multibyte = 0;
6668 coding.dst_multibyte = 1;
6669 /* The input is converted to events, thus we can't
6670 handle composition. Anyway, there's no XIM that
6671 gives us composition information. */
6672 coding.common_flags &= ~CODING_ANNOTATION_MASK;
6674 require = MAX_MULTIBYTE_LENGTH * nbytes;
6675 coding.destination = alloca (require);
6676 coding.dst_bytes = require;
6677 coding.mode |= CODING_MODE_LAST_BLOCK;
6678 decode_coding_c_string (&coding, copy_bufptr, nbytes, Qnil);
6679 nbytes = coding.produced;
6680 nchars = coding.produced_char;
6681 copy_bufptr = coding.destination;
6684 /* Convert the input data to a sequence of
6685 character events. */
6686 for (i = 0; i < nbytes; i += len)
6688 if (nchars == nbytes)
6689 c = copy_bufptr[i], len = 1;
6690 else
6691 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
6692 nbytes - i, len);
6693 inev.ie.kind = (SINGLE_BYTE_CHAR_P (c)
6694 ? ASCII_KEYSTROKE_EVENT
6695 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
6696 inev.ie.code = c;
6697 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
6700 /* Previous code updated count by nchars rather than nbytes,
6701 but that seems bogus to me. ++kfs */
6702 count += nbytes;
6704 inev.ie.kind = NO_EVENT; /* Already stored above. */
6706 if (keysym == NoSymbol)
6707 break;
6710 done_keysym:
6711 #ifdef HAVE_X_I18N
6712 /* Don't dispatch this event since XtDispatchEvent calls
6713 XFilterEvent, and two calls in a row may freeze the
6714 client. */
6715 break;
6716 #else
6717 goto OTHER;
6718 #endif
6720 case KeyRelease:
6721 last_user_time = event.xkey.time;
6722 #ifdef HAVE_X_I18N
6723 /* Don't dispatch this event since XtDispatchEvent calls
6724 XFilterEvent, and two calls in a row may freeze the
6725 client. */
6726 break;
6727 #else
6728 goto OTHER;
6729 #endif
6731 case EnterNotify:
6732 last_user_time = event.xcrossing.time;
6733 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6735 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
6737 if (f && x_mouse_click_focus_ignore_position)
6738 ignore_next_mouse_click_timeout = event.xmotion.time + 200;
6740 #if 0
6741 if (event.xcrossing.focus)
6743 /* Avoid nasty pop/raise loops. */
6744 if (f && (!(f->auto_raise)
6745 || !(f->auto_lower)
6746 || (event.xcrossing.time - enter_timestamp) > 500))
6748 x_new_focus_frame (dpyinfo, f);
6749 enter_timestamp = event.xcrossing.time;
6752 else if (f == dpyinfo->x_focus_frame)
6753 x_new_focus_frame (dpyinfo, 0);
6754 #endif
6756 /* EnterNotify counts as mouse movement,
6757 so update things that depend on mouse position. */
6758 if (f && !f->output_data.x->hourglass_p)
6759 note_mouse_movement (f, &event.xmotion);
6760 #ifdef USE_GTK
6761 /* We may get an EnterNotify on the buttons in the toolbar. In that
6762 case we moved out of any highlighted area and need to note this. */
6763 if (!f && last_mouse_glyph_frame)
6764 note_mouse_movement (last_mouse_glyph_frame, &event);
6765 #endif
6766 goto OTHER;
6768 case FocusIn:
6769 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6770 goto OTHER;
6772 case LeaveNotify:
6773 last_user_time = event.xcrossing.time;
6774 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6776 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
6777 if (f)
6779 if (f == dpyinfo->mouse_face_mouse_frame)
6781 /* If we move outside the frame, then we're
6782 certainly no longer on any text in the frame. */
6783 clear_mouse_face (dpyinfo);
6784 dpyinfo->mouse_face_mouse_frame = 0;
6787 /* Generate a nil HELP_EVENT to cancel a help-echo.
6788 Do it only if there's something to cancel.
6789 Otherwise, the startup message is cleared when
6790 the mouse leaves the frame. */
6791 if (any_help_event_p)
6792 do_help = -1;
6794 #ifdef USE_GTK
6795 /* See comment in EnterNotify above */
6796 else if (last_mouse_glyph_frame)
6797 note_mouse_movement (last_mouse_glyph_frame, &event);
6798 #endif
6799 goto OTHER;
6801 case FocusOut:
6802 x_detect_focus_change (dpyinfo, &event, &inev.ie);
6803 goto OTHER;
6805 case MotionNotify:
6807 last_user_time = event.xmotion.time;
6808 previous_help_echo_string = help_echo_string;
6809 help_echo_string = Qnil;
6811 if (dpyinfo->grabbed && last_mouse_frame
6812 && FRAME_LIVE_P (last_mouse_frame))
6813 f = last_mouse_frame;
6814 else
6815 f = x_window_to_frame (dpyinfo, event.xmotion.window);
6817 if (dpyinfo->mouse_face_hidden)
6819 dpyinfo->mouse_face_hidden = 0;
6820 clear_mouse_face (dpyinfo);
6823 if (f)
6826 /* Generate SELECT_WINDOW_EVENTs when needed. */
6827 if (!NILP (Vmouse_autoselect_window))
6829 Lisp_Object window;
6831 window = window_from_coordinates (f,
6832 event.xmotion.x, event.xmotion.y,
6833 0, 0, 0, 0);
6835 /* Window will be selected only when it is not selected now and
6836 last mouse movement event was not in it. Minibuffer window
6837 will be selected iff it is active. */
6838 if (WINDOWP (window)
6839 && !EQ (window, last_window)
6840 && !EQ (window, selected_window))
6842 inev.ie.kind = SELECT_WINDOW_EVENT;
6843 inev.ie.frame_or_window = window;
6846 last_window=window;
6848 if (!note_mouse_movement (f, &event.xmotion))
6849 help_echo_string = previous_help_echo_string;
6851 else
6853 #ifndef USE_TOOLKIT_SCROLL_BARS
6854 struct scroll_bar *bar
6855 = x_window_to_scroll_bar (event.xmotion.display,
6856 event.xmotion.window);
6858 if (bar)
6859 x_scroll_bar_note_movement (bar, &event);
6860 #endif /* USE_TOOLKIT_SCROLL_BARS */
6862 /* If we move outside the frame, then we're
6863 certainly no longer on any text in the frame. */
6864 clear_mouse_face (dpyinfo);
6867 /* If the contents of the global variable help_echo_string
6868 has changed, generate a HELP_EVENT. */
6869 if (!NILP (help_echo_string)
6870 || !NILP (previous_help_echo_string))
6871 do_help = 1;
6872 goto OTHER;
6875 case ConfigureNotify:
6876 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
6877 if (f)
6879 #ifndef USE_X_TOOLKIT
6880 #ifdef USE_GTK
6881 xg_resize_widgets (f, event.xconfigure.width,
6882 event.xconfigure.height);
6883 #else /* not USE_GTK */
6884 /* If there is a pending resize for fullscreen, don't
6885 do this one, the right one will come later.
6886 The toolkit version doesn't seem to need this, but we
6887 need to reset it below. */
6888 int dont_resize
6889 = ((f->want_fullscreen & FULLSCREEN_WAIT)
6890 && f->new_text_cols != 0);
6891 int rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES (f, event.xconfigure.height);
6892 int columns = FRAME_PIXEL_WIDTH_TO_TEXT_COLS (f, event.xconfigure.width);
6894 if (dont_resize)
6895 goto OTHER;
6897 /* In the toolkit version, change_frame_size
6898 is called by the code that handles resizing
6899 of the EmacsFrame widget. */
6901 /* Even if the number of character rows and columns has
6902 not changed, the font size may have changed, so we need
6903 to check the pixel dimensions as well. */
6904 if (columns != FRAME_COLS (f)
6905 || rows != FRAME_LINES (f)
6906 || event.xconfigure.width != FRAME_PIXEL_WIDTH (f)
6907 || event.xconfigure.height != FRAME_PIXEL_HEIGHT (f))
6909 change_frame_size (f, rows, columns, 0, 1, 0);
6910 SET_FRAME_GARBAGED (f);
6911 cancel_mouse_face (f);
6913 #endif /* not USE_GTK */
6914 #endif
6916 FRAME_PIXEL_WIDTH (f) = event.xconfigure.width;
6917 FRAME_PIXEL_HEIGHT (f) = event.xconfigure.height;
6919 #ifdef USE_GTK
6920 /* GTK creates windows but doesn't map them.
6921 Only get real positions and check fullscreen when mapped. */
6922 if (FRAME_GTK_OUTER_WIDGET (f)
6923 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
6924 #endif
6926 x_real_positions (f, &f->left_pos, &f->top_pos);
6928 if (f->want_fullscreen & FULLSCREEN_WAIT)
6929 f->want_fullscreen &= ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
6932 #ifdef HAVE_X_I18N
6933 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
6934 xic_set_statusarea (f);
6935 #endif
6937 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
6939 /* Since the WM decorations come below top_pos now,
6940 we must put them below top_pos in the future. */
6941 f->win_gravity = NorthWestGravity;
6942 x_wm_set_size_hint (f, (long) 0, 0);
6945 goto OTHER;
6947 case ButtonRelease:
6948 case ButtonPress:
6950 /* If we decide we want to generate an event to be seen
6951 by the rest of Emacs, we put it here. */
6952 int tool_bar_p = 0;
6954 bzero (&compose_status, sizeof (compose_status));
6955 last_mouse_glyph_frame = 0;
6956 last_user_time = event.xbutton.time;
6958 if (dpyinfo->grabbed
6959 && last_mouse_frame
6960 && FRAME_LIVE_P (last_mouse_frame))
6961 f = last_mouse_frame;
6962 else
6963 f = x_window_to_frame (dpyinfo, event.xbutton.window);
6965 if (f)
6967 /* Is this in the tool-bar? */
6968 if (WINDOWP (f->tool_bar_window)
6969 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)))
6971 Lisp_Object window;
6972 int x = event.xbutton.x;
6973 int y = event.xbutton.y;
6975 window = window_from_coordinates (f, x, y, 0, 0, 0, 1);
6976 tool_bar_p = EQ (window, f->tool_bar_window);
6978 if (tool_bar_p && event.xbutton.button < 4)
6980 if (event.xbutton.type == ButtonPress)
6981 handle_tool_bar_click (f, x, y, 1, 0);
6982 else
6983 handle_tool_bar_click (f, x, y, 0,
6984 x_x_to_emacs_modifiers (dpyinfo,
6985 event.xbutton.state));
6989 if (!tool_bar_p)
6990 if (!dpyinfo->x_focus_frame
6991 || f == dpyinfo->x_focus_frame)
6993 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6994 if (! popup_activated ())
6995 #endif
6997 if (ignore_next_mouse_click_timeout)
6999 if (event.type == ButtonPress
7000 && (int)(event.xbutton.time - ignore_next_mouse_click_timeout) > 0)
7002 ignore_next_mouse_click_timeout = 0;
7003 construct_mouse_click (&inev.ie, &event.xbutton, f);
7005 if (event.type == ButtonRelease)
7006 ignore_next_mouse_click_timeout = 0;
7008 else
7009 construct_mouse_click (&inev.ie, &event.xbutton, f);
7013 else
7015 struct scroll_bar *bar
7016 = x_window_to_scroll_bar (event.xbutton.display,
7017 event.xbutton.window);
7019 #ifdef USE_TOOLKIT_SCROLL_BARS
7020 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
7021 scroll bars. */
7022 if (bar && event.xbutton.state & ControlMask)
7024 x_scroll_bar_handle_click (bar, &event, &inev.ie);
7025 *finish = X_EVENT_DROP;
7027 #else /* not USE_TOOLKIT_SCROLL_BARS */
7028 if (bar)
7029 x_scroll_bar_handle_click (bar, &event, &inev.ie);
7030 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7033 if (event.type == ButtonPress)
7035 dpyinfo->grabbed |= (1 << event.xbutton.button);
7036 last_mouse_frame = f;
7038 if (!tool_bar_p)
7039 last_tool_bar_item = -1;
7041 else
7042 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
7044 /* Ignore any mouse motion that happened before this event;
7045 any subsequent mouse-movement Emacs events should reflect
7046 only motion after the ButtonPress/Release. */
7047 if (f != 0)
7048 f->mouse_moved = 0;
7050 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
7051 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
7052 /* For a down-event in the menu bar,
7053 don't pass it to Xt right now.
7054 Instead, save it away
7055 and we will pass it to Xt from kbd_buffer_get_event.
7056 That way, we can run some Lisp code first. */
7057 if (
7058 #ifdef USE_GTK
7059 ! popup_activated ()
7061 #endif
7062 f && event.type == ButtonPress
7063 /* Verify the event is really within the menu bar
7064 and not just sent to it due to grabbing. */
7065 && event.xbutton.x >= 0
7066 && event.xbutton.x < FRAME_PIXEL_WIDTH (f)
7067 && event.xbutton.y >= 0
7068 && event.xbutton.y < f->output_data.x->menubar_height
7069 && event.xbutton.same_screen)
7071 SET_SAVED_BUTTON_EVENT;
7072 XSETFRAME (last_mouse_press_frame, f);
7073 #ifdef USE_GTK
7074 *finish = X_EVENT_DROP;
7075 #endif
7077 else if (event.type == ButtonPress)
7079 last_mouse_press_frame = Qnil;
7080 goto OTHER;
7083 #ifdef USE_MOTIF /* This should do not harm for Lucid,
7084 but I am trying to be cautious. */
7085 else if (event.type == ButtonRelease)
7087 if (!NILP (last_mouse_press_frame))
7089 f = XFRAME (last_mouse_press_frame);
7090 if (f->output_data.x)
7091 SET_SAVED_BUTTON_EVENT;
7093 else
7094 goto OTHER;
7096 #endif /* USE_MOTIF */
7097 else
7098 goto OTHER;
7099 #endif /* USE_X_TOOLKIT || USE_GTK */
7101 break;
7103 case CirculateNotify:
7104 goto OTHER;
7106 case CirculateRequest:
7107 goto OTHER;
7109 case VisibilityNotify:
7110 goto OTHER;
7112 case MappingNotify:
7113 /* Someone has changed the keyboard mapping - update the
7114 local cache. */
7115 switch (event.xmapping.request)
7117 case MappingModifier:
7118 x_find_modifier_meanings (dpyinfo);
7119 /* This is meant to fall through. */
7120 case MappingKeyboard:
7121 XRefreshKeyboardMapping (&event.xmapping);
7123 goto OTHER;
7125 default:
7126 OTHER:
7127 #ifdef USE_X_TOOLKIT
7128 BLOCK_INPUT;
7129 if (*finish != X_EVENT_DROP)
7130 XtDispatchEvent (&event);
7131 UNBLOCK_INPUT;
7132 #endif /* USE_X_TOOLKIT */
7133 break;
7136 done:
7137 if (inev.ie.kind != NO_EVENT)
7139 kbd_buffer_store_event_hold (&inev.ie, hold_quit);
7140 count++;
7143 if (do_help
7144 && !(hold_quit && hold_quit->kind != NO_EVENT))
7146 Lisp_Object frame;
7148 if (f)
7149 XSETFRAME (frame, f);
7150 else
7151 frame = Qnil;
7153 if (do_help > 0)
7155 any_help_event_p = 1;
7156 gen_help_event (help_echo_string, frame, help_echo_window,
7157 help_echo_object, help_echo_pos);
7159 else
7161 help_echo_string = Qnil;
7162 gen_help_event (Qnil, frame, Qnil, Qnil, 0);
7164 count++;
7167 *eventp = event;
7168 return count;
7172 /* Handles the XEvent EVENT on display DISPLAY.
7173 This is used for event loops outside the normal event handling,
7174 i.e. looping while a popup menu or a dialog is posted.
7176 Returns the value handle_one_xevent sets in the finish argument. */
7178 x_dispatch_event (event, display)
7179 XEvent *event;
7180 Display *display;
7182 struct x_display_info *dpyinfo;
7183 int finish = X_EVENT_NORMAL;
7185 dpyinfo = x_display_info_for_display (display);
7187 if (dpyinfo)
7188 handle_one_xevent (dpyinfo, event, &finish, 0);
7190 return finish;
7194 /* Read events coming from the X server.
7195 This routine is called by the SIGIO handler.
7196 We return as soon as there are no more events to be read.
7198 We return the number of characters stored into the buffer,
7199 thus pretending to be `read'.
7201 EXPECTED is nonzero if the caller knows input is available. */
7203 static int
7204 XTread_socket (sd, expected, hold_quit)
7205 register int sd;
7206 int expected;
7207 struct input_event *hold_quit;
7209 int count = 0;
7210 XEvent event;
7211 int event_found = 0;
7212 struct x_display_info *dpyinfo;
7214 if (interrupt_input_blocked)
7216 interrupt_input_pending = 1;
7217 return -1;
7220 interrupt_input_pending = 0;
7221 BLOCK_INPUT;
7223 /* So people can tell when we have read the available input. */
7224 input_signal_count++;
7226 ++handling_signal;
7228 /* Find the display we are supposed to read input for.
7229 It's the one communicating on descriptor SD. */
7230 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
7232 #if 0 /* This ought to be unnecessary; let's verify it. */
7233 #ifdef FIOSNBIO
7234 /* If available, Xlib uses FIOSNBIO to make the socket
7235 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
7236 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
7237 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
7238 fcntl (dpyinfo->connection, F_SETFL, 0);
7239 #endif /* ! defined (FIOSNBIO) */
7240 #endif
7242 #if 0 /* This code can't be made to work, with multiple displays,
7243 and appears not to be used on any system any more.
7244 Also keyboard.c doesn't turn O_NDELAY on and off
7245 for X connections. */
7246 #ifndef SIGIO
7247 #ifndef HAVE_SELECT
7248 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
7250 extern int read_alarm_should_throw;
7251 read_alarm_should_throw = 1;
7252 XPeekEvent (dpyinfo->display, &event);
7253 read_alarm_should_throw = 0;
7255 #endif /* HAVE_SELECT */
7256 #endif /* SIGIO */
7257 #endif
7259 /* For debugging, this gives a way to fake an I/O error. */
7260 if (dpyinfo == XTread_socket_fake_io_error)
7262 XTread_socket_fake_io_error = 0;
7263 x_io_error_quitter (dpyinfo->display);
7266 #ifdef HAVE_X_SM
7268 struct input_event inev;
7269 BLOCK_INPUT;
7270 /* We don't need to EVENT_INIT (inev) here, as
7271 x_session_check_input copies an entire input_event. */
7272 if (x_session_check_input (&inev))
7274 kbd_buffer_store_event_hold (&inev, hold_quit);
7275 count++;
7277 UNBLOCK_INPUT;
7279 #endif
7281 #ifndef USE_GTK
7282 while (XPending (dpyinfo->display))
7284 int finish;
7286 XNextEvent (dpyinfo->display, &event);
7288 #ifdef HAVE_X_I18N
7289 /* Filter events for the current X input method. */
7290 if (x_filter_event (dpyinfo, &event))
7291 break;
7292 #endif
7293 event_found = 1;
7295 count += handle_one_xevent (dpyinfo, &event, &finish, hold_quit);
7297 if (finish == X_EVENT_GOTO_OUT)
7298 goto out;
7300 #endif /* not USE_GTK */
7303 #ifdef USE_GTK
7305 /* For GTK we must use the GTK event loop. But XEvents gets passed
7306 to our filter function above, and then to the big event switch.
7307 We use a bunch of globals to communicate with our filter function,
7308 that is kind of ugly, but it works.
7310 There is no way to do one display at the time, GTK just does events
7311 from all displays. */
7313 while (gtk_events_pending ())
7315 current_count = count;
7316 current_hold_quit = hold_quit;
7318 gtk_main_iteration ();
7320 count = current_count;
7321 current_count = -1;
7322 current_hold_quit = 0;
7324 if (current_finish == X_EVENT_GOTO_OUT)
7325 break;
7327 #endif /* USE_GTK */
7329 out:;
7331 /* On some systems, an X bug causes Emacs to get no more events
7332 when the window is destroyed. Detect that. (1994.) */
7333 if (! event_found)
7335 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
7336 One XNOOP in 100 loops will make Emacs terminate.
7337 B. Bretthauer, 1994 */
7338 x_noop_count++;
7339 if (x_noop_count >= 100)
7341 x_noop_count=0;
7343 if (next_noop_dpyinfo == 0)
7344 next_noop_dpyinfo = x_display_list;
7346 XNoOp (next_noop_dpyinfo->display);
7348 /* Each time we get here, cycle through the displays now open. */
7349 next_noop_dpyinfo = next_noop_dpyinfo->next;
7353 /* If the focus was just given to an auto-raising frame,
7354 raise it now. */
7355 /* ??? This ought to be able to handle more than one such frame. */
7356 if (pending_autoraise_frame)
7358 x_raise_frame (pending_autoraise_frame);
7359 pending_autoraise_frame = 0;
7362 --handling_signal;
7363 UNBLOCK_INPUT;
7365 return count;
7371 /***********************************************************************
7372 Text Cursor
7373 ***********************************************************************/
7375 /* Set clipping for output in glyph row ROW. W is the window in which
7376 we operate. GC is the graphics context to set clipping in.
7378 ROW may be a text row or, e.g., a mode line. Text rows must be
7379 clipped to the interior of the window dedicated to text display,
7380 mode lines must be clipped to the whole window. */
7382 static void
7383 x_clip_to_row (w, row, area, gc)
7384 struct window *w;
7385 struct glyph_row *row;
7386 int area;
7387 GC gc;
7389 struct frame *f = XFRAME (WINDOW_FRAME (w));
7390 XRectangle clip_rect;
7391 int window_x, window_y, window_width;
7393 window_box (w, area, &window_x, &window_y, &window_width, 0);
7395 clip_rect.x = window_x;
7396 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, row->y));
7397 clip_rect.y = max (clip_rect.y, window_y);
7398 clip_rect.width = window_width;
7399 clip_rect.height = row->visible_height;
7401 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
7405 /* Draw a hollow box cursor on window W in glyph row ROW. */
7407 static void
7408 x_draw_hollow_cursor (w, row)
7409 struct window *w;
7410 struct glyph_row *row;
7412 struct frame *f = XFRAME (WINDOW_FRAME (w));
7413 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7414 Display *dpy = FRAME_X_DISPLAY (f);
7415 int x, y, wd, h;
7416 XGCValues xgcv;
7417 struct glyph *cursor_glyph;
7418 GC gc;
7420 /* Get the glyph the cursor is on. If we can't tell because
7421 the current matrix is invalid or such, give up. */
7422 cursor_glyph = get_phys_cursor_glyph (w);
7423 if (cursor_glyph == NULL)
7424 return;
7426 /* Compute frame-relative coordinates for phys cursor. */
7427 get_phys_cursor_geometry (w, row, cursor_glyph, &x, &y, &h);
7428 wd = w->phys_cursor_width;
7430 /* The foreground of cursor_gc is typically the same as the normal
7431 background color, which can cause the cursor box to be invisible. */
7432 xgcv.foreground = f->output_data.x->cursor_pixel;
7433 if (dpyinfo->scratch_cursor_gc)
7434 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
7435 else
7436 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
7437 GCForeground, &xgcv);
7438 gc = dpyinfo->scratch_cursor_gc;
7440 /* Set clipping, draw the rectangle, and reset clipping again. */
7441 x_clip_to_row (w, row, TEXT_AREA, gc);
7442 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h - 1);
7443 XSetClipMask (dpy, gc, None);
7447 /* Draw a bar cursor on window W in glyph row ROW.
7449 Implementation note: One would like to draw a bar cursor with an
7450 angle equal to the one given by the font property XA_ITALIC_ANGLE.
7451 Unfortunately, I didn't find a font yet that has this property set.
7452 --gerd. */
7454 static void
7455 x_draw_bar_cursor (w, row, width, kind)
7456 struct window *w;
7457 struct glyph_row *row;
7458 int width;
7459 enum text_cursor_kinds kind;
7461 struct frame *f = XFRAME (w->frame);
7462 struct glyph *cursor_glyph;
7464 /* If cursor is out of bounds, don't draw garbage. This can happen
7465 in mini-buffer windows when switching between echo area glyphs
7466 and mini-buffer. */
7467 cursor_glyph = get_phys_cursor_glyph (w);
7468 if (cursor_glyph == NULL)
7469 return;
7471 /* If on an image, draw like a normal cursor. That's usually better
7472 visible than drawing a bar, esp. if the image is large so that
7473 the bar might not be in the window. */
7474 if (cursor_glyph->type == IMAGE_GLYPH)
7476 struct glyph_row *row;
7477 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
7478 draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
7480 else
7482 Display *dpy = FRAME_X_DISPLAY (f);
7483 Window window = FRAME_X_WINDOW (f);
7484 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
7485 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
7486 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
7487 XGCValues xgcv;
7489 /* If the glyph's background equals the color we normally draw
7490 the bar cursor in, the bar cursor in its normal color is
7491 invisible. Use the glyph's foreground color instead in this
7492 case, on the assumption that the glyph's colors are chosen so
7493 that the glyph is legible. */
7494 if (face->background == f->output_data.x->cursor_pixel)
7495 xgcv.background = xgcv.foreground = face->foreground;
7496 else
7497 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
7498 xgcv.graphics_exposures = 0;
7500 if (gc)
7501 XChangeGC (dpy, gc, mask, &xgcv);
7502 else
7504 gc = XCreateGC (dpy, window, mask, &xgcv);
7505 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
7508 if (width < 0)
7509 width = FRAME_CURSOR_WIDTH (f);
7510 width = min (cursor_glyph->pixel_width, width);
7512 w->phys_cursor_width = width;
7513 x_clip_to_row (w, row, TEXT_AREA, gc);
7515 if (kind == BAR_CURSOR)
7516 XFillRectangle (dpy, window, gc,
7517 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7518 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
7519 width, row->height);
7520 else
7521 XFillRectangle (dpy, window, gc,
7522 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
7523 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
7524 row->height - width),
7525 cursor_glyph->pixel_width,
7526 width);
7528 XSetClipMask (dpy, gc, None);
7533 /* RIF: Define cursor CURSOR on frame F. */
7535 static void
7536 x_define_frame_cursor (f, cursor)
7537 struct frame *f;
7538 Cursor cursor;
7540 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7544 /* RIF: Clear area on frame F. */
7546 static void
7547 x_clear_frame_area (f, x, y, width, height)
7548 struct frame *f;
7549 int x, y, width, height;
7551 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7552 x, y, width, height, False);
7556 /* RIF: Draw cursor on window W. */
7558 static void
7559 x_draw_window_cursor (w, glyph_row, x, y, cursor_type, cursor_width, on_p, active_p)
7560 struct window *w;
7561 struct glyph_row *glyph_row;
7562 int x, y;
7563 int cursor_type, cursor_width;
7564 int on_p, active_p;
7566 struct frame *f = XFRAME (WINDOW_FRAME (w));
7568 if (on_p)
7570 w->phys_cursor_type = cursor_type;
7571 w->phys_cursor_on_p = 1;
7573 if (glyph_row->exact_window_width_line_p
7574 && w->phys_cursor.hpos >= glyph_row->used[TEXT_AREA])
7576 glyph_row->cursor_in_fringe_p = 1;
7577 draw_fringe_bitmap (w, glyph_row, 0);
7579 else
7580 switch (cursor_type)
7582 case HOLLOW_BOX_CURSOR:
7583 x_draw_hollow_cursor (w, glyph_row);
7584 break;
7586 case FILLED_BOX_CURSOR:
7587 draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
7588 break;
7590 case BAR_CURSOR:
7591 x_draw_bar_cursor (w, glyph_row, cursor_width, BAR_CURSOR);
7592 break;
7594 case HBAR_CURSOR:
7595 x_draw_bar_cursor (w, glyph_row, cursor_width, HBAR_CURSOR);
7596 break;
7598 case NO_CURSOR:
7599 w->phys_cursor_width = 0;
7600 break;
7602 default:
7603 abort ();
7606 #ifdef HAVE_X_I18N
7607 if (w == XWINDOW (f->selected_window))
7608 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
7609 xic_set_preeditarea (w, x, y);
7610 #endif
7613 #ifndef XFlush
7614 if (updating_frame != f)
7615 XFlush (FRAME_X_DISPLAY (f));
7616 #endif
7620 /* Icons. */
7622 /* Make the x-window of frame F use the gnu icon bitmap. */
7625 x_bitmap_icon (f, file)
7626 struct frame *f;
7627 Lisp_Object file;
7629 int bitmap_id;
7631 if (FRAME_X_WINDOW (f) == 0)
7632 return 1;
7634 /* Free up our existing icon bitmap and mask if any. */
7635 if (f->output_data.x->icon_bitmap > 0)
7636 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7637 f->output_data.x->icon_bitmap = 0;
7639 if (STRINGP (file))
7641 #ifdef USE_GTK
7642 /* Use gtk_window_set_icon_from_file () if available,
7643 It's not restricted to bitmaps */
7644 if (xg_set_icon (f, file))
7645 return 0;
7646 #endif /* USE_GTK */
7647 bitmap_id = x_create_bitmap_from_file (f, file);
7648 x_create_bitmap_mask (f, bitmap_id);
7650 else
7652 /* Create the GNU bitmap and mask if necessary. */
7653 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
7655 int rc = -1;
7657 #if defined (HAVE_XPM) && defined (HAVE_X_WINDOWS)
7658 #ifdef USE_GTK
7659 if (xg_set_icon_from_xpm_data (f, gnu_xpm_bits))
7660 return 0;
7661 #else
7662 rc = x_create_bitmap_from_xpm_data (f, gnu_xpm_bits);
7663 if (rc != -1)
7664 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7665 #endif /* USE_GTK */
7666 #endif /* defined (HAVE_XPM) && defined (HAVE_X_WINDOWS) */
7668 /* If all else fails, use the (black and white) xbm image. */
7669 if (rc == -1)
7671 rc = x_create_bitmap_from_data (f, gnu_xbm_bits,
7672 gnu_xbm_width, gnu_xbm_height);
7673 if (rc == -1)
7674 return 1;
7676 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id = rc;
7677 x_create_bitmap_mask (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7681 /* The first time we create the GNU bitmap and mask,
7682 this increments the ref-count one extra time.
7683 As a result, the GNU bitmap and mask are never freed.
7684 That way, we don't have to worry about allocating it again. */
7685 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
7687 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
7690 x_wm_set_icon_pixmap (f, bitmap_id);
7691 f->output_data.x->icon_bitmap = bitmap_id;
7693 return 0;
7697 /* Make the x-window of frame F use a rectangle with text.
7698 Use ICON_NAME as the text. */
7701 x_text_icon (f, icon_name)
7702 struct frame *f;
7703 char *icon_name;
7705 if (FRAME_X_WINDOW (f) == 0)
7706 return 1;
7708 #ifdef HAVE_X11R4
7710 XTextProperty text;
7711 text.value = (unsigned char *) icon_name;
7712 text.encoding = XA_STRING;
7713 text.format = 8;
7714 text.nitems = strlen (icon_name);
7715 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
7717 #else /* not HAVE_X11R4 */
7718 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
7719 #endif /* not HAVE_X11R4 */
7721 if (f->output_data.x->icon_bitmap > 0)
7722 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
7723 f->output_data.x->icon_bitmap = 0;
7724 x_wm_set_icon_pixmap (f, 0);
7726 return 0;
7729 #define X_ERROR_MESSAGE_SIZE 200
7731 /* If non-nil, this should be a string.
7732 It means catch X errors and store the error message in this string.
7734 The reason we use a stack is that x_catch_error/x_uncatch_error can
7735 be called from a signal handler.
7738 struct x_error_message_stack {
7739 char string[X_ERROR_MESSAGE_SIZE];
7740 Display *dpy;
7741 struct x_error_message_stack *prev;
7743 static struct x_error_message_stack *x_error_message;
7745 /* An X error handler which stores the error message in
7746 *x_error_message. This is called from x_error_handler if
7747 x_catch_errors is in effect. */
7749 static void
7750 x_error_catcher (display, error)
7751 Display *display;
7752 XErrorEvent *error;
7754 XGetErrorText (display, error->error_code,
7755 x_error_message->string,
7756 X_ERROR_MESSAGE_SIZE);
7759 /* Begin trapping X errors for display DPY. Actually we trap X errors
7760 for all displays, but DPY should be the display you are actually
7761 operating on.
7763 After calling this function, X protocol errors no longer cause
7764 Emacs to exit; instead, they are recorded in the string
7765 stored in *x_error_message.
7767 Calling x_check_errors signals an Emacs error if an X error has
7768 occurred since the last call to x_catch_errors or x_check_errors.
7770 Calling x_uncatch_errors resumes the normal error handling. */
7772 void x_check_errors ();
7774 void
7775 x_catch_errors (dpy)
7776 Display *dpy;
7778 struct x_error_message_stack *data = xmalloc (sizeof (*data));
7780 /* Make sure any errors from previous requests have been dealt with. */
7781 XSync (dpy, False);
7783 data->dpy = dpy;
7784 data->string[0] = 0;
7785 data->prev = x_error_message;
7786 x_error_message = data;
7789 /* Undo the last x_catch_errors call.
7790 DPY should be the display that was passed to x_catch_errors. */
7792 void
7793 x_uncatch_errors ()
7795 struct x_error_message_stack *tmp;
7797 BLOCK_INPUT;
7799 /* The display may have been closed before this function is called.
7800 Check if it is still open before calling XSync. */
7801 if (x_display_info_for_display (x_error_message->dpy) != 0)
7802 XSync (x_error_message->dpy, False);
7804 tmp = x_error_message;
7805 x_error_message = x_error_message->prev;
7806 xfree (tmp);
7807 UNBLOCK_INPUT;
7810 /* If any X protocol errors have arrived since the last call to
7811 x_catch_errors or x_check_errors, signal an Emacs error using
7812 sprintf (a buffer, FORMAT, the x error message text) as the text. */
7814 void
7815 x_check_errors (dpy, format)
7816 Display *dpy;
7817 char *format;
7819 /* Make sure to catch any errors incurred so far. */
7820 XSync (dpy, False);
7822 if (x_error_message->string[0])
7824 char string[X_ERROR_MESSAGE_SIZE];
7825 bcopy (x_error_message->string, string, X_ERROR_MESSAGE_SIZE);
7826 x_uncatch_errors ();
7827 error (format, string);
7831 /* Nonzero if we had any X protocol errors
7832 since we did x_catch_errors on DPY. */
7835 x_had_errors_p (dpy)
7836 Display *dpy;
7838 /* Make sure to catch any errors incurred so far. */
7839 XSync (dpy, False);
7841 return x_error_message->string[0] != 0;
7844 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
7846 void
7847 x_clear_errors (dpy)
7848 Display *dpy;
7850 x_error_message->string[0] = 0;
7853 /* Close off all unclosed x_catch_errors calls. */
7855 void
7856 x_fully_uncatch_errors ()
7858 while (x_error_message)
7859 x_uncatch_errors ();
7862 /* Nonzero if x_catch_errors has been done and not yet canceled. */
7865 x_catching_errors ()
7867 return x_error_message != 0;
7870 #if 0
7871 static unsigned int x_wire_count;
7872 x_trace_wire ()
7874 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
7876 #endif /* ! 0 */
7879 /* Handle SIGPIPE, which can happen when the connection to a server
7880 simply goes away. SIGPIPE is handled by x_connection_signal.
7881 Don't need to do anything, because the write which caused the
7882 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
7883 which will do the appropriate cleanup for us. */
7885 static SIGTYPE
7886 x_connection_signal (signalnum) /* If we don't have an argument, */
7887 int signalnum; /* some compilers complain in signal calls. */
7889 #ifdef USG
7890 /* USG systems forget handlers when they are used;
7891 must reestablish each time */
7892 signal (signalnum, x_connection_signal);
7893 #endif /* USG */
7897 /************************************************************************
7898 Handling X errors
7899 ************************************************************************/
7901 /* Error message passed to x_connection_closed. */
7903 static char *error_msg;
7905 /* Function installed as fatal_error_signal_hook in
7906 x_connection_closed. Print the X error message, and exit normally,
7907 instead of dumping core when XtCloseDisplay fails. */
7909 static void
7910 x_fatal_error_signal ()
7912 fprintf (stderr, "%s\n", error_msg);
7913 exit (70);
7916 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
7917 the text of an error message that lead to the connection loss. */
7919 static SIGTYPE
7920 x_connection_closed (dpy, error_message)
7921 Display *dpy;
7922 char *error_message;
7924 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
7925 Lisp_Object frame, tail;
7927 error_msg = (char *) alloca (strlen (error_message) + 1);
7928 strcpy (error_msg, error_message);
7929 handling_signal = 0;
7931 /* Prevent being called recursively because of an error condition
7932 below. Otherwise, we might end up with printing ``can't find per
7933 display information'' in the recursive call instead of printing
7934 the original message here. */
7935 x_catch_errors (dpy);
7937 /* We have to close the display to inform Xt that it doesn't
7938 exist anymore. If we don't, Xt will continue to wait for
7939 events from the display. As a consequence, a sequence of
7941 M-x make-frame-on-display RET :1 RET
7942 ...kill the new frame, so that we get an IO error...
7943 M-x make-frame-on-display RET :1 RET
7945 will indefinitely wait in Xt for events for display `:1', opened
7946 in the first class to make-frame-on-display.
7948 Closing the display is reported to lead to a bus error on
7949 OpenWindows in certain situations. I suspect that is a bug
7950 in OpenWindows. I don't know how to cicumvent it here. */
7952 #ifdef USE_X_TOOLKIT
7953 /* If DPYINFO is null, this means we didn't open the display
7954 in the first place, so don't try to close it. */
7955 if (dpyinfo)
7957 extern void (*fatal_error_signal_hook) P_ ((void));
7958 fatal_error_signal_hook = x_fatal_error_signal;
7959 XtCloseDisplay (dpy);
7960 fatal_error_signal_hook = NULL;
7962 #endif
7964 #ifdef USE_GTK
7965 if (dpyinfo)
7966 xg_display_close (dpyinfo->display);
7967 #endif
7969 /* Indicate that this display is dead. */
7970 if (dpyinfo)
7971 dpyinfo->display = 0;
7973 /* First delete frames whose mini-buffers are on frames
7974 that are on the dead display. */
7975 FOR_EACH_FRAME (tail, frame)
7977 Lisp_Object minibuf_frame;
7978 minibuf_frame
7979 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
7980 if (FRAME_X_P (XFRAME (frame))
7981 && FRAME_X_P (XFRAME (minibuf_frame))
7982 && ! EQ (frame, minibuf_frame)
7983 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
7984 Fdelete_frame (frame, Qt);
7987 /* Now delete all remaining frames on the dead display.
7988 We are now sure none of these is used as the mini-buffer
7989 for another frame that we need to delete. */
7990 FOR_EACH_FRAME (tail, frame)
7991 if (FRAME_X_P (XFRAME (frame))
7992 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
7994 /* Set this to t so that Fdelete_frame won't get confused
7995 trying to find a replacement. */
7996 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
7997 Fdelete_frame (frame, Qt);
8000 if (dpyinfo)
8001 x_delete_display (dpyinfo);
8003 x_uncatch_errors ();
8005 if (x_display_list == 0)
8007 fprintf (stderr, "%s\n", error_msg);
8008 shut_down_emacs (0, 0, Qnil);
8009 exit (70);
8012 /* Ordinary stack unwind doesn't deal with these. */
8013 #ifdef SIGIO
8014 sigunblock (sigmask (SIGIO));
8015 #endif
8016 sigunblock (sigmask (SIGALRM));
8017 TOTALLY_UNBLOCK_INPUT;
8019 clear_waiting_for_input ();
8020 error ("%s", error_msg);
8023 /* We specifically use it before defining it, so that gcc doesn't inline it,
8024 otherwise gdb doesn't know how to properly put a breakpoint on it. */
8025 static void x_error_quitter P_ ((Display *, XErrorEvent *));
8027 /* This is the first-level handler for X protocol errors.
8028 It calls x_error_quitter or x_error_catcher. */
8030 static int
8031 x_error_handler (display, error)
8032 Display *display;
8033 XErrorEvent *error;
8035 if (x_error_message)
8036 x_error_catcher (display, error);
8037 else
8038 x_error_quitter (display, error);
8039 return 0;
8042 /* This is the usual handler for X protocol errors.
8043 It kills all frames on the display that we got the error for.
8044 If that was the only one, it prints an error message and kills Emacs. */
8046 /* .gdbinit puts a breakpoint here, so make sure it is not inlined. */
8048 #if __GNUC__ >= 3 /* On GCC 3.0 we might get a warning. */
8049 #define NO_INLINE __attribute__((noinline))
8050 #else
8051 #define NO_INLINE
8052 #endif
8054 /* Some versions of GNU/Linux define noinline in their headers. */
8056 #ifdef noinline
8057 #undef noinline
8058 #endif
8060 /* On older GCC versions, just putting x_error_quitter
8061 after x_error_handler prevents inlining into the former. */
8063 static void NO_INLINE
8064 x_error_quitter (display, error)
8065 Display *display;
8066 XErrorEvent *error;
8068 char buf[256], buf1[356];
8070 /* Ignore BadName errors. They can happen because of fonts
8071 or colors that are not defined. */
8073 if (error->error_code == BadName)
8074 return;
8076 /* Note that there is no real way portable across R3/R4 to get the
8077 original error handler. */
8079 XGetErrorText (display, error->error_code, buf, sizeof (buf));
8080 sprintf (buf1, "X protocol error: %s on protocol request %d",
8081 buf, error->request_code);
8082 x_connection_closed (display, buf1);
8086 /* This is the handler for X IO errors, always.
8087 It kills all frames on the display that we lost touch with.
8088 If that was the only one, it prints an error message and kills Emacs. */
8090 static int
8091 x_io_error_quitter (display)
8092 Display *display;
8094 char buf[256];
8096 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
8097 x_connection_closed (display, buf);
8098 return 0;
8101 /* Changing the font of the frame. */
8103 /* Give frame F the font named FONTNAME as its default font, and
8104 return the full name of that font. FONTNAME may be a wildcard
8105 pattern; in that case, we choose some font that fits the pattern.
8106 The return value shows which font we chose. */
8108 Lisp_Object
8109 x_new_font (f, fontname)
8110 struct frame *f;
8111 register char *fontname;
8113 struct font_info *fontp
8114 = FS_LOAD_FONT (f, fontname);
8116 if (!fontp)
8117 return Qnil;
8119 if (FRAME_FONT (f) == (XFontStruct *) (fontp->font))
8120 /* This font is already set in frame F. There's nothing more to
8121 do. */
8122 return build_string (fontp->full_name);
8124 FRAME_FONT (f) = (XFontStruct *) (fontp->font);
8125 FRAME_BASELINE_OFFSET (f) = fontp->baseline_offset;
8126 FRAME_FONTSET (f) = -1;
8128 FRAME_COLUMN_WIDTH (f) = fontp->average_width;
8129 FRAME_SPACE_WIDTH (f) = fontp->space_width;
8130 FRAME_LINE_HEIGHT (f) = FONT_HEIGHT (FRAME_FONT (f));
8132 compute_fringe_widths (f, 1);
8134 /* Compute the scroll bar width in character columns. */
8135 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8137 int wid = FRAME_COLUMN_WIDTH (f);
8138 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8139 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid-1) / wid;
8141 else
8143 int wid = FRAME_COLUMN_WIDTH (f);
8144 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8147 /* Now make the frame display the given font. */
8148 if (FRAME_X_WINDOW (f) != 0)
8150 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
8151 FRAME_FONT (f)->fid);
8152 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
8153 FRAME_FONT (f)->fid);
8154 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
8155 FRAME_FONT (f)->fid);
8157 /* Don't change the size of a tip frame; there's no point in
8158 doing it because it's done in Fx_show_tip, and it leads to
8159 problems because the tip frame has no widget. */
8160 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8161 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8164 return build_string (fontp->full_name);
8167 /* Give frame F the fontset named FONTSETNAME as its default fontset,
8168 and return the full name of that fontset. FONTSETNAME may be a
8169 wildcard pattern; in that case, we choose some fontset that fits
8170 the pattern. FONTSETNAME may be a font name for ASCII characters;
8171 in that case, we create a fontset from that font name.
8173 The return value shows which fontset we chose.
8174 If FONTSETNAME specifies the default fontset, return Qt.
8175 If an ASCII font in the specified fontset can't be loaded, return
8176 Qnil. */
8178 Lisp_Object
8179 x_new_fontset (f, fontsetname)
8180 struct frame *f;
8181 Lisp_Object fontsetname;
8183 int fontset = fs_query_fontset (fontsetname, 0);
8184 Lisp_Object result;
8186 if (fontset > 0 && f->output_data.x->fontset == fontset)
8187 /* This fontset is already set in frame F. There's nothing more
8188 to do. */
8189 return fontset_name (fontset);
8190 else if (fontset == 0)
8191 /* The default fontset can't be the default font. */
8192 return Qt;
8194 if (fontset > 0)
8195 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
8196 else
8197 result = x_new_font (f, SDATA (fontsetname));
8199 if (!STRINGP (result))
8200 /* Can't load ASCII font. */
8201 return Qnil;
8203 if (fontset < 0)
8204 fontset = new_fontset_from_font_name (result);
8206 /* Since x_new_font doesn't update any fontset information, do it now. */
8207 FRAME_FONTSET (f) = fontset;
8209 #ifdef HAVE_X_I18N
8210 if (FRAME_XIC (f)
8211 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8212 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8213 #endif
8215 return fontset_name (fontset);
8218 #ifdef USE_FONT_BACKEND
8219 Lisp_Object
8220 x_new_fontset2 (f, fontset, font_object)
8221 struct frame *f;
8222 int fontset;
8223 Lisp_Object font_object;
8225 struct font *font = XSAVE_VALUE (font_object)->pointer;
8227 if (FRAME_FONT_OBJECT (f) == font)
8228 /* This font is already set in frame F. There's nothing more to
8229 do. */
8230 return fontset_name (fontset);
8232 BLOCK_INPUT;
8234 FRAME_FONT_OBJECT (f) = font;
8235 FRAME_FONT (f) = font->font.font;
8236 FRAME_BASELINE_OFFSET (f) = font->font.baseline_offset;
8237 FRAME_FONTSET (f) = fontset;
8239 FRAME_COLUMN_WIDTH (f) = font->font.average_width;
8240 FRAME_SPACE_WIDTH (f) = font->font.space_width;
8241 FRAME_LINE_HEIGHT (f) = font->font.height;
8243 compute_fringe_widths (f, 1);
8245 /* Compute the scroll bar width in character columns. */
8246 if (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0)
8248 int wid = FRAME_COLUMN_WIDTH (f);
8249 FRAME_CONFIG_SCROLL_BAR_COLS (f)
8250 = (FRAME_CONFIG_SCROLL_BAR_WIDTH (f) + wid - 1) / wid;
8252 else
8254 int wid = FRAME_COLUMN_WIDTH (f);
8255 FRAME_CONFIG_SCROLL_BAR_COLS (f) = (14 + wid - 1) / wid;
8258 /* Now make the frame display the given font. */
8259 if (FRAME_X_WINDOW (f) != 0)
8261 /* Don't change the size of a tip frame; there's no point in
8262 doing it because it's done in Fx_show_tip, and it leads to
8263 problems because the tip frame has no widget. */
8264 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
8265 x_set_window_size (f, 0, FRAME_COLS (f), FRAME_LINES (f));
8268 #ifdef HAVE_X_I18N
8269 if (FRAME_XIC (f)
8270 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
8271 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
8272 #endif
8274 UNBLOCK_INPUT;
8276 return fontset_name (fontset);
8278 #endif /* USE_FONT_BACKEND */
8281 /***********************************************************************
8282 X Input Methods
8283 ***********************************************************************/
8285 #ifdef HAVE_X_I18N
8287 #ifdef HAVE_X11R6
8289 /* XIM destroy callback function, which is called whenever the
8290 connection to input method XIM dies. CLIENT_DATA contains a
8291 pointer to the x_display_info structure corresponding to XIM. */
8293 static void
8294 xim_destroy_callback (xim, client_data, call_data)
8295 XIM xim;
8296 XPointer client_data;
8297 XPointer call_data;
8299 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
8300 Lisp_Object frame, tail;
8302 BLOCK_INPUT;
8304 /* No need to call XDestroyIC.. */
8305 FOR_EACH_FRAME (tail, frame)
8307 struct frame *f = XFRAME (frame);
8308 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
8310 FRAME_XIC (f) = NULL;
8311 xic_free_xfontset (f);
8315 /* No need to call XCloseIM. */
8316 dpyinfo->xim = NULL;
8317 XFree (dpyinfo->xim_styles);
8318 UNBLOCK_INPUT;
8321 #endif /* HAVE_X11R6 */
8323 #ifdef HAVE_X11R6
8324 /* This isn't prototyped in OSF 5.0 or 5.1a. */
8325 extern char *XSetIMValues P_ ((XIM, ...));
8326 #endif
8328 /* Open the connection to the XIM server on display DPYINFO.
8329 RESOURCE_NAME is the resource name Emacs uses. */
8331 static void
8332 xim_open_dpy (dpyinfo, resource_name)
8333 struct x_display_info *dpyinfo;
8334 char *resource_name;
8336 XIM xim;
8338 #ifdef HAVE_XIM
8339 if (use_xim)
8341 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
8342 EMACS_CLASS);
8343 dpyinfo->xim = xim;
8345 if (xim)
8347 #ifdef HAVE_X11R6
8348 XIMCallback destroy;
8349 #endif
8351 /* Get supported styles and XIM values. */
8352 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
8354 #ifdef HAVE_X11R6
8355 destroy.callback = xim_destroy_callback;
8356 destroy.client_data = (XPointer)dpyinfo;
8357 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
8358 #endif
8362 else
8363 #endif /* HAVE_XIM */
8364 dpyinfo->xim = NULL;
8368 #ifdef HAVE_X11R6_XIM
8370 struct xim_inst_t
8372 struct x_display_info *dpyinfo;
8373 char *resource_name;
8376 /* XIM instantiate callback function, which is called whenever an XIM
8377 server is available. DISPLAY is the display of the XIM.
8378 CLIENT_DATA contains a pointer to an xim_inst_t structure created
8379 when the callback was registered. */
8381 static void
8382 xim_instantiate_callback (display, client_data, call_data)
8383 Display *display;
8384 XPointer client_data;
8385 XPointer call_data;
8387 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
8388 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
8390 /* We don't support multiple XIM connections. */
8391 if (dpyinfo->xim)
8392 return;
8394 xim_open_dpy (dpyinfo, xim_inst->resource_name);
8396 /* Create XIC for the existing frames on the same display, as long
8397 as they have no XIC. */
8398 if (dpyinfo->xim && dpyinfo->reference_count > 0)
8400 Lisp_Object tail, frame;
8402 BLOCK_INPUT;
8403 FOR_EACH_FRAME (tail, frame)
8405 struct frame *f = XFRAME (frame);
8407 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
8408 if (FRAME_XIC (f) == NULL)
8410 create_frame_xic (f);
8411 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
8412 xic_set_statusarea (f);
8413 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
8415 struct window *w = XWINDOW (f->selected_window);
8416 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
8421 UNBLOCK_INPUT;
8425 #endif /* HAVE_X11R6_XIM */
8428 /* Open a connection to the XIM server on display DPYINFO.
8429 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
8430 connection only at the first time. On X11R6, open the connection
8431 in the XIM instantiate callback function. */
8433 static void
8434 xim_initialize (dpyinfo, resource_name)
8435 struct x_display_info *dpyinfo;
8436 char *resource_name;
8438 #ifdef HAVE_XIM
8439 if (use_xim)
8441 #ifdef HAVE_X11R6_XIM
8442 struct xim_inst_t *xim_inst;
8443 int len;
8445 dpyinfo->xim = NULL;
8446 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
8447 xim_inst->dpyinfo = dpyinfo;
8448 len = strlen (resource_name);
8449 xim_inst->resource_name = (char *) xmalloc (len + 1);
8450 bcopy (resource_name, xim_inst->resource_name, len + 1);
8451 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8452 resource_name, EMACS_CLASS,
8453 xim_instantiate_callback,
8454 /* This is XPointer in XFree86
8455 but (XPointer *) on Tru64, at
8456 least, hence the configure test. */
8457 (XRegisterIMInstantiateCallback_arg6) xim_inst);
8458 #else /* not HAVE_X11R6_XIM */
8459 dpyinfo->xim = NULL;
8460 xim_open_dpy (dpyinfo, resource_name);
8461 #endif /* not HAVE_X11R6_XIM */
8464 else
8465 #endif /* HAVE_XIM */
8466 dpyinfo->xim = NULL;
8470 /* Close the connection to the XIM server on display DPYINFO. */
8472 static void
8473 xim_close_dpy (dpyinfo)
8474 struct x_display_info *dpyinfo;
8476 #ifdef HAVE_XIM
8477 if (use_xim)
8479 #ifdef HAVE_X11R6_XIM
8480 if (dpyinfo->display)
8481 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
8482 NULL, EMACS_CLASS,
8483 xim_instantiate_callback, NULL);
8484 #endif /* not HAVE_X11R6_XIM */
8485 if (dpyinfo->display)
8486 XCloseIM (dpyinfo->xim);
8487 dpyinfo->xim = NULL;
8488 XFree (dpyinfo->xim_styles);
8490 #endif /* HAVE_XIM */
8493 #endif /* not HAVE_X11R6_XIM */
8497 /* Calculate the absolute position in frame F
8498 from its current recorded position values and gravity. */
8500 void
8501 x_calc_absolute_position (f)
8502 struct frame *f;
8504 int flags = f->size_hint_flags;
8506 /* We have nothing to do if the current position
8507 is already for the top-left corner. */
8508 if (! ((flags & XNegative) || (flags & YNegative)))
8509 return;
8511 /* Treat negative positions as relative to the leftmost bottommost
8512 position that fits on the screen. */
8513 if (flags & XNegative)
8514 f->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
8515 - FRAME_PIXEL_WIDTH (f) + f->left_pos);
8518 int height = FRAME_PIXEL_HEIGHT (f);
8520 #if defined USE_X_TOOLKIT && defined USE_MOTIF
8521 /* Something is fishy here. When using Motif, starting Emacs with
8522 `-g -0-0', the frame appears too low by a few pixels.
8524 This seems to be so because initially, while Emacs is starting,
8525 the column widget's height and the frame's pixel height are
8526 different. The column widget's height is the right one. In
8527 later invocations, when Emacs is up, the frame's pixel height
8528 is right, though.
8530 It's not obvious where the initial small difference comes from.
8531 2000-12-01, gerd. */
8533 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
8534 #endif
8536 if (flags & YNegative)
8537 f->top_pos = (FRAME_X_DISPLAY_INFO (f)->height - height + f->top_pos);
8540 /* The left_pos and top_pos
8541 are now relative to the top and left screen edges,
8542 so the flags should correspond. */
8543 f->size_hint_flags &= ~ (XNegative | YNegative);
8546 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
8547 to really change the position, and 0 when calling from
8548 x_make_frame_visible (in that case, XOFF and YOFF are the current
8549 position values). It is -1 when calling from x_set_frame_parameters,
8550 which means, do adjust for borders but don't change the gravity. */
8552 void
8553 x_set_offset (f, xoff, yoff, change_gravity)
8554 struct frame *f;
8555 register int xoff, yoff;
8556 int change_gravity;
8558 int modified_top, modified_left;
8560 if (change_gravity != 0)
8562 FRAME_X_OUTPUT (f)->left_before_move = f->left_pos;
8563 FRAME_X_OUTPUT (f)->top_before_move = f->top_pos;
8565 f->top_pos = yoff;
8566 f->left_pos = xoff;
8567 f->size_hint_flags &= ~ (XNegative | YNegative);
8568 if (xoff < 0)
8569 f->size_hint_flags |= XNegative;
8570 if (yoff < 0)
8571 f->size_hint_flags |= YNegative;
8572 f->win_gravity = NorthWestGravity;
8574 x_calc_absolute_position (f);
8576 BLOCK_INPUT;
8577 x_wm_set_size_hint (f, (long) 0, 0);
8579 modified_left = f->left_pos;
8580 modified_top = f->top_pos;
8582 if (change_gravity != 0 && FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A)
8584 /* Some WMs (twm, wmaker at least) has an offset that is smaller
8585 than the WM decorations. So we use the calculated offset instead
8586 of the WM decoration sizes here (x/y_pixels_outer_diff). */
8587 modified_left += FRAME_X_OUTPUT (f)->move_offset_left;
8588 modified_top += FRAME_X_OUTPUT (f)->move_offset_top;
8591 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8592 modified_left, modified_top);
8594 x_sync_with_move (f, f->left_pos, f->top_pos,
8595 FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8596 ? 1 : 0);
8598 /* change_gravity is non-zero when this function is called from Lisp to
8599 programmatically move a frame. In that case, we call
8600 x_check_expected_move to discover if we have a "Type A" or "Type B"
8601 window manager, and, for a "Type A" window manager, adjust the position
8602 of the frame.
8604 We call x_check_expected_move if a programmatic move occurred, and
8605 either the window manager type (A/B) is unknown or it is Type A but we
8606 need to compute the top/left offset adjustment for this frame. */
8608 if (change_gravity != 0 &&
8609 (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_UNKNOWN
8610 || (FRAME_X_DISPLAY_INFO (f)->wm_type == X_WMTYPE_A
8611 && (FRAME_X_OUTPUT (f)->move_offset_left == 0
8612 && FRAME_X_OUTPUT (f)->move_offset_top == 0))))
8613 x_check_expected_move (f, modified_left, modified_top);
8615 UNBLOCK_INPUT;
8618 /* Return non-zero if _NET_SUPPORTING_WM_CHECK window exists and _NET_SUPPORTED
8619 on the root window for frame F contains ATOMNAME.
8620 This is how a WM check shall be done according to the Window Manager
8621 Specification/Extended Window Manager Hints at
8622 http://freedesktop.org/wiki/Standards_2fwm_2dspec. */
8624 static int
8625 wm_supports (f, atomname)
8626 struct frame *f;
8627 const char *atomname;
8629 Atom actual_type;
8630 unsigned long actual_size, bytes_remaining;
8631 int i, rc, actual_format;
8632 Atom prop_atom;
8633 Window wmcheck_window;
8634 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
8635 Window target_window = dpyinfo->root_window;
8636 long max_len = 65536;
8637 Display *dpy = FRAME_X_DISPLAY (f);
8638 unsigned char *tmp_data = NULL;
8639 Atom target_type = XA_WINDOW;
8640 Atom want_atom;
8642 BLOCK_INPUT;
8644 prop_atom = XInternAtom (dpy, "_NET_SUPPORTING_WM_CHECK", False);
8646 x_catch_errors (dpy);
8647 rc = XGetWindowProperty (dpy, target_window,
8648 prop_atom, 0, max_len, False, target_type,
8649 &actual_type, &actual_format, &actual_size,
8650 &bytes_remaining, &tmp_data);
8652 if (rc != Success || actual_type != XA_WINDOW || x_had_errors_p (dpy))
8654 if (tmp_data) XFree (tmp_data);
8655 x_uncatch_errors ();
8656 UNBLOCK_INPUT;
8657 return 0;
8660 wmcheck_window = *(Window *) tmp_data;
8661 XFree (tmp_data);
8663 /* Check if window exists. */
8664 XSelectInput (dpy, wmcheck_window, StructureNotifyMask);
8665 x_sync (f);
8666 if (x_had_errors_p (dpy))
8668 x_uncatch_errors ();
8669 UNBLOCK_INPUT;
8670 return 0;
8673 if (dpyinfo->net_supported_window != wmcheck_window)
8675 /* Window changed, reload atoms */
8676 if (dpyinfo->net_supported_atoms != NULL)
8677 XFree (dpyinfo->net_supported_atoms);
8678 dpyinfo->net_supported_atoms = NULL;
8679 dpyinfo->nr_net_supported_atoms = 0;
8680 dpyinfo->net_supported_window = 0;
8682 target_type = XA_ATOM;
8683 prop_atom = XInternAtom (dpy, "_NET_SUPPORTED", False);
8684 tmp_data = NULL;
8685 rc = XGetWindowProperty (dpy, target_window,
8686 prop_atom, 0, max_len, False, target_type,
8687 &actual_type, &actual_format, &actual_size,
8688 &bytes_remaining, &tmp_data);
8690 if (rc != Success || actual_type != XA_ATOM || x_had_errors_p (dpy))
8692 if (tmp_data) XFree (tmp_data);
8693 x_uncatch_errors ();
8694 UNBLOCK_INPUT;
8695 return 0;
8698 dpyinfo->net_supported_atoms = (Atom *)tmp_data;
8699 dpyinfo->nr_net_supported_atoms = actual_size;
8700 dpyinfo->net_supported_window = wmcheck_window;
8703 rc = 0;
8704 want_atom = XInternAtom (dpy, atomname, False);
8706 for (i = 0; rc == 0 && i < dpyinfo->nr_net_supported_atoms; ++i)
8707 rc = dpyinfo->net_supported_atoms[i] == want_atom;
8709 x_uncatch_errors ();
8710 UNBLOCK_INPUT;
8712 return rc;
8715 /* Do fullscreen as specified in extended window manager hints */
8717 static int
8718 do_ewmh_fullscreen (f)
8719 struct frame *f;
8721 int have_net_atom = wm_supports (f, "_NET_WM_STATE");
8723 /* Some window managers don't say they support _NET_WM_STATE, but they do say
8724 they support _NET_WM_STATE_FULLSCREEN. Try that also. */
8725 if (!have_net_atom)
8726 have_net_atom = wm_supports (f, "_NET_WM_STATE_FULLSCREEN");
8728 if (have_net_atom)
8730 Lisp_Object frame;
8731 const char *atom = "_NET_WM_STATE";
8732 const char *fs = "_NET_WM_STATE_FULLSCREEN";
8733 const char *fw = "_NET_WM_STATE_MAXIMIZED_HORZ";
8734 const char *fh = "_NET_WM_STATE_MAXIMIZED_VERT";
8735 const char *what = NULL;
8737 XSETFRAME (frame, f);
8739 /* If there are _NET_ atoms we assume we have extended window manager
8740 hints. */
8741 switch (f->want_fullscreen)
8743 case FULLSCREEN_BOTH:
8744 what = fs;
8745 break;
8746 case FULLSCREEN_WIDTH:
8747 what = fw;
8748 break;
8749 case FULLSCREEN_HEIGHT:
8750 what = fh;
8751 break;
8754 if (what != NULL && !wm_supports (f, what)) return 0;
8757 Fx_send_client_event (frame, make_number (0), frame,
8758 make_unibyte_string (atom, strlen (atom)),
8759 make_number (32),
8760 Fcons (make_number (0), /* Remove */
8761 Fcons
8762 (make_unibyte_string (fs,
8763 strlen (fs)),
8764 Qnil)));
8765 Fx_send_client_event (frame, make_number (0), frame,
8766 make_unibyte_string (atom, strlen (atom)),
8767 make_number (32),
8768 Fcons (make_number (0), /* Remove */
8769 Fcons
8770 (make_unibyte_string (fh,
8771 strlen (fh)),
8772 Qnil)));
8773 Fx_send_client_event (frame, make_number (0), frame,
8774 make_unibyte_string (atom, strlen (atom)),
8775 make_number (32),
8776 Fcons (make_number (0), /* Remove */
8777 Fcons
8778 (make_unibyte_string (fw,
8779 strlen (fw)),
8780 Qnil)));
8781 f->want_fullscreen = FULLSCREEN_NONE;
8782 if (what != NULL)
8783 Fx_send_client_event (frame, make_number (0), frame,
8784 make_unibyte_string (atom, strlen (atom)),
8785 make_number (32),
8786 Fcons (make_number (1), /* Add */
8787 Fcons
8788 (make_unibyte_string (what,
8789 strlen (what)),
8790 Qnil)));
8793 return have_net_atom;
8796 static void
8797 XTfullscreen_hook (f)
8798 FRAME_PTR f;
8800 if (f->async_visible)
8802 BLOCK_INPUT;
8803 do_ewmh_fullscreen (f);
8804 x_sync (f);
8805 UNBLOCK_INPUT;
8810 /* Check if we need to resize the frame due to a fullscreen request.
8811 If so needed, resize the frame. */
8812 static void
8813 x_check_fullscreen (f)
8814 struct frame *f;
8816 if (f->want_fullscreen & FULLSCREEN_BOTH)
8818 int width, height, ign;
8820 if (do_ewmh_fullscreen (f))
8821 return;
8823 x_real_positions (f, &f->left_pos, &f->top_pos);
8825 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
8827 /* We do not need to move the window, it shall be taken care of
8828 when setting WM manager hints.
8829 If the frame is visible already, the position is checked by
8830 x_check_expected_move. */
8831 if (FRAME_COLS (f) != width || FRAME_LINES (f) != height)
8833 change_frame_size (f, height, width, 0, 1, 0);
8834 SET_FRAME_GARBAGED (f);
8835 cancel_mouse_face (f);
8837 /* Wait for the change of frame size to occur */
8838 f->want_fullscreen |= FULLSCREEN_WAIT;
8843 /* This function is called by x_set_offset to determine whether the window
8844 manager interfered with the positioning of the frame. Type A window
8845 managers position the surrounding window manager decorations a small
8846 amount above and left of the user-supplied position. Type B window
8847 managers position the surrounding window manager decorations at the
8848 user-specified position. If we detect a Type A window manager, we
8849 compensate by moving the window right and down by the proper amount. */
8851 static void
8852 x_check_expected_move (f, expected_left, expected_top)
8853 struct frame *f;
8854 int expected_left;
8855 int expected_top;
8857 int current_left = 0, current_top = 0;
8859 /* x_real_positions returns the left and top offsets of the outermost
8860 window manager window around the frame. */
8862 x_real_positions (f, &current_left, &current_top);
8864 if (current_left != expected_left || current_top != expected_top)
8866 /* It's a "Type A" window manager. */
8868 int adjusted_left;
8869 int adjusted_top;
8871 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_A;
8872 FRAME_X_OUTPUT (f)->move_offset_left = expected_left - current_left;
8873 FRAME_X_OUTPUT (f)->move_offset_top = expected_top - current_top;
8875 /* Now fix the mispositioned frame's location. */
8877 adjusted_left = expected_left + FRAME_X_OUTPUT (f)->move_offset_left;
8878 adjusted_top = expected_top + FRAME_X_OUTPUT (f)->move_offset_top;
8880 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
8881 adjusted_left, adjusted_top);
8883 x_sync_with_move (f, expected_left, expected_top, 0);
8885 else
8886 /* It's a "Type B" window manager. We don't have to adjust the
8887 frame's position. */
8889 FRAME_X_DISPLAY_INFO (f)->wm_type = X_WMTYPE_B;
8893 /* Wait for XGetGeometry to return up-to-date position information for a
8894 recently-moved frame. Call this immediately after calling XMoveWindow.
8895 If FUZZY is non-zero, then LEFT and TOP are just estimates of where the
8896 frame has been moved to, so we use a fuzzy position comparison instead
8897 of an exact comparison. */
8899 static void
8900 x_sync_with_move (f, left, top, fuzzy)
8901 struct frame *f;
8902 int left, top, fuzzy;
8904 int count = 0;
8906 while (count++ < 50)
8908 int current_left = 0, current_top = 0;
8910 /* In theory, this call to XSync only needs to happen once, but in
8911 practice, it doesn't seem to work, hence the need for the surrounding
8912 loop. */
8914 XSync (FRAME_X_DISPLAY (f), False);
8915 x_real_positions (f, &current_left, &current_top);
8917 if (fuzzy)
8919 /* The left fuzz-factor is 10 pixels. The top fuzz-factor is 40
8920 pixels. */
8922 if (abs (current_left - left) <= 10 && abs (current_top - top) <= 40)
8923 return;
8925 else if (current_left == left && current_top == top)
8926 return;
8929 /* As a last resort, just wait 0.5 seconds and hope that XGetGeometry
8930 will then return up-to-date position info. */
8932 wait_reading_process_output (0, 500000, 0, 0, Qnil, NULL, 0);
8936 /* Change the size of frame F's X window to COLS/ROWS in the case F
8937 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
8938 top-left-corner window gravity for this size change and subsequent
8939 size changes. Otherwise we leave the window gravity unchanged. */
8941 static void
8942 x_set_window_size_1 (f, change_gravity, cols, rows)
8943 struct frame *f;
8944 int change_gravity;
8945 int cols, rows;
8947 int pixelwidth, pixelheight;
8949 check_frame_size (f, &rows, &cols);
8950 f->scroll_bar_actual_width
8951 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
8953 : FRAME_CONFIG_SCROLL_BAR_WIDTH (f) > 0
8954 ? FRAME_CONFIG_SCROLL_BAR_WIDTH (f)
8955 : (FRAME_CONFIG_SCROLL_BAR_COLS (f) * FRAME_COLUMN_WIDTH (f)));
8957 compute_fringe_widths (f, 0);
8959 pixelwidth = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, cols);
8960 pixelheight = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, rows);
8962 f->win_gravity = NorthWestGravity;
8963 x_wm_set_size_hint (f, (long) 0, 0);
8965 XSync (FRAME_X_DISPLAY (f), False);
8966 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8967 pixelwidth, pixelheight);
8969 /* Now, strictly speaking, we can't be sure that this is accurate,
8970 but the window manager will get around to dealing with the size
8971 change request eventually, and we'll hear how it went when the
8972 ConfigureNotify event gets here.
8974 We could just not bother storing any of this information here,
8975 and let the ConfigureNotify event set everything up, but that
8976 might be kind of confusing to the Lisp code, since size changes
8977 wouldn't be reported in the frame parameters until some random
8978 point in the future when the ConfigureNotify event arrives.
8980 We pass 1 for DELAY since we can't run Lisp code inside of
8981 a BLOCK_INPUT. */
8982 change_frame_size (f, rows, cols, 0, 1, 0);
8983 FRAME_PIXEL_WIDTH (f) = pixelwidth;
8984 FRAME_PIXEL_HEIGHT (f) = pixelheight;
8986 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
8987 receive in the ConfigureNotify event; if we get what we asked
8988 for, then the event won't cause the screen to become garbaged, so
8989 we have to make sure to do it here. */
8990 SET_FRAME_GARBAGED (f);
8992 XFlush (FRAME_X_DISPLAY (f));
8996 /* Call this to change the size of frame F's x-window.
8997 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
8998 for this size change and subsequent size changes.
8999 Otherwise we leave the window gravity unchanged. */
9001 void
9002 x_set_window_size (f, change_gravity, cols, rows)
9003 struct frame *f;
9004 int change_gravity;
9005 int cols, rows;
9007 BLOCK_INPUT;
9009 #ifdef USE_GTK
9010 if (FRAME_GTK_WIDGET (f))
9011 xg_frame_set_char_size (f, cols, rows);
9012 else
9013 x_set_window_size_1 (f, change_gravity, cols, rows);
9014 #elif USE_X_TOOLKIT
9016 if (f->output_data.x->widget != NULL)
9018 /* The x and y position of the widget is clobbered by the
9019 call to XtSetValues within EmacsFrameSetCharSize.
9020 This is a real kludge, but I don't understand Xt so I can't
9021 figure out a correct fix. Can anyone else tell me? -- rms. */
9022 int xpos = f->output_data.x->widget->core.x;
9023 int ypos = f->output_data.x->widget->core.y;
9024 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
9025 f->output_data.x->widget->core.x = xpos;
9026 f->output_data.x->widget->core.y = ypos;
9028 else
9029 x_set_window_size_1 (f, change_gravity, cols, rows);
9031 #else /* not USE_X_TOOLKIT */
9033 x_set_window_size_1 (f, change_gravity, cols, rows);
9035 #endif /* not USE_X_TOOLKIT */
9037 /* If cursor was outside the new size, mark it as off. */
9038 mark_window_cursors_off (XWINDOW (f->root_window));
9040 /* Clear out any recollection of where the mouse highlighting was,
9041 since it might be in a place that's outside the new frame size.
9042 Actually checking whether it is outside is a pain in the neck,
9043 so don't try--just let the highlighting be done afresh with new size. */
9044 cancel_mouse_face (f);
9046 UNBLOCK_INPUT;
9049 /* Mouse warping. */
9051 void
9052 x_set_mouse_position (f, x, y)
9053 struct frame *f;
9054 int x, y;
9056 int pix_x, pix_y;
9058 pix_x = FRAME_COL_TO_PIXEL_X (f, x) + FRAME_COLUMN_WIDTH (f) / 2;
9059 pix_y = FRAME_LINE_TO_PIXEL_Y (f, y) + FRAME_LINE_HEIGHT (f) / 2;
9061 if (pix_x < 0) pix_x = 0;
9062 if (pix_x > FRAME_PIXEL_WIDTH (f)) pix_x = FRAME_PIXEL_WIDTH (f);
9064 if (pix_y < 0) pix_y = 0;
9065 if (pix_y > FRAME_PIXEL_HEIGHT (f)) pix_y = FRAME_PIXEL_HEIGHT (f);
9067 BLOCK_INPUT;
9069 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9070 0, 0, 0, 0, pix_x, pix_y);
9071 UNBLOCK_INPUT;
9074 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
9076 void
9077 x_set_mouse_pixel_position (f, pix_x, pix_y)
9078 struct frame *f;
9079 int pix_x, pix_y;
9081 BLOCK_INPUT;
9083 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
9084 0, 0, 0, 0, pix_x, pix_y);
9085 UNBLOCK_INPUT;
9088 /* focus shifting, raising and lowering. */
9090 void
9091 x_focus_on_frame (f)
9092 struct frame *f;
9094 #if 0 /* This proves to be unpleasant. */
9095 x_raise_frame (f);
9096 #endif
9097 #if 0
9098 /* I don't think that the ICCCM allows programs to do things like this
9099 without the interaction of the window manager. Whatever you end up
9100 doing with this code, do it to x_unfocus_frame too. */
9101 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9102 RevertToPointerRoot, CurrentTime);
9103 #endif /* ! 0 */
9106 void
9107 x_unfocus_frame (f)
9108 struct frame *f;
9110 #if 0
9111 /* Look at the remarks in x_focus_on_frame. */
9112 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
9113 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
9114 RevertToPointerRoot, CurrentTime);
9115 #endif /* ! 0 */
9118 /* Raise frame F. */
9120 void
9121 x_raise_frame (f)
9122 struct frame *f;
9124 BLOCK_INPUT;
9125 if (f->async_visible)
9126 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9128 XFlush (FRAME_X_DISPLAY (f));
9129 UNBLOCK_INPUT;
9132 /* Lower frame F. */
9134 void
9135 x_lower_frame (f)
9136 struct frame *f;
9138 if (f->async_visible)
9140 BLOCK_INPUT;
9141 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
9142 XFlush (FRAME_X_DISPLAY (f));
9143 UNBLOCK_INPUT;
9147 /* Activate frame with Extended Window Manager Hints */
9149 void
9150 x_ewmh_activate_frame (f)
9151 FRAME_PTR f;
9153 /* See Window Manager Specification/Extended Window Manager Hints at
9154 http://freedesktop.org/wiki/Standards_2fwm_2dspec */
9156 const char *atom = "_NET_ACTIVE_WINDOW";
9157 if (f->async_visible && wm_supports (f, atom))
9159 Lisp_Object frame;
9160 XSETFRAME (frame, f);
9161 Fx_send_client_event (frame, make_number (0), frame,
9162 make_unibyte_string (atom, strlen (atom)),
9163 make_number (32),
9164 Fcons (make_number (1),
9165 Fcons (make_number (last_user_time),
9166 Qnil)));
9170 static void
9171 XTframe_raise_lower (f, raise_flag)
9172 FRAME_PTR f;
9173 int raise_flag;
9175 if (raise_flag)
9176 x_raise_frame (f);
9177 else
9178 x_lower_frame (f);
9181 /* Change of visibility. */
9183 /* This tries to wait until the frame is really visible.
9184 However, if the window manager asks the user where to position
9185 the frame, this will return before the user finishes doing that.
9186 The frame will not actually be visible at that time,
9187 but it will become visible later when the window manager
9188 finishes with it. */
9190 void
9191 x_make_frame_visible (f)
9192 struct frame *f;
9194 Lisp_Object type;
9195 int original_top, original_left;
9196 int retry_count = 2;
9198 retry:
9200 BLOCK_INPUT;
9202 type = x_icon_type (f);
9203 if (!NILP (type))
9204 x_bitmap_icon (f, type);
9206 if (! FRAME_VISIBLE_P (f))
9208 /* We test FRAME_GARBAGED_P here to make sure we don't
9209 call x_set_offset a second time
9210 if we get to x_make_frame_visible a second time
9211 before the window gets really visible. */
9212 if (! FRAME_ICONIFIED_P (f)
9213 && ! f->output_data.x->asked_for_visible)
9214 x_set_offset (f, f->left_pos, f->top_pos, 0);
9216 f->output_data.x->asked_for_visible = 1;
9218 if (! EQ (Vx_no_window_manager, Qt))
9219 x_wm_set_window_state (f, NormalState);
9220 #ifdef USE_X_TOOLKIT
9221 /* This was XtPopup, but that did nothing for an iconified frame. */
9222 XtMapWidget (f->output_data.x->widget);
9223 #else /* not USE_X_TOOLKIT */
9224 #ifdef USE_GTK
9225 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9226 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9227 #else
9228 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9229 #endif /* not USE_GTK */
9230 #endif /* not USE_X_TOOLKIT */
9231 #if 0 /* This seems to bring back scroll bars in the wrong places
9232 if the window configuration has changed. They seem
9233 to come back ok without this. */
9234 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9235 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9236 #endif
9239 XFlush (FRAME_X_DISPLAY (f));
9241 /* Synchronize to ensure Emacs knows the frame is visible
9242 before we do anything else. We do this loop with input not blocked
9243 so that incoming events are handled. */
9245 Lisp_Object frame;
9246 int count;
9247 /* This must be before UNBLOCK_INPUT
9248 since events that arrive in response to the actions above
9249 will set it when they are handled. */
9250 int previously_visible = f->output_data.x->has_been_visible;
9252 original_left = f->left_pos;
9253 original_top = f->top_pos;
9255 /* This must come after we set COUNT. */
9256 UNBLOCK_INPUT;
9258 /* We unblock here so that arriving X events are processed. */
9260 /* Now move the window back to where it was "supposed to be".
9261 But don't do it if the gravity is negative.
9262 When the gravity is negative, this uses a position
9263 that is 3 pixels too low. Perhaps that's really the border width.
9265 Don't do this if the window has never been visible before,
9266 because the window manager may choose the position
9267 and we don't want to override it. */
9269 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
9270 && f->win_gravity == NorthWestGravity
9271 && previously_visible)
9273 Drawable rootw;
9274 int x, y;
9275 unsigned int width, height, border, depth;
9277 BLOCK_INPUT;
9279 /* On some window managers (such as FVWM) moving an existing
9280 window, even to the same place, causes the window manager
9281 to introduce an offset. This can cause the window to move
9282 to an unexpected location. Check the geometry (a little
9283 slow here) and then verify that the window is in the right
9284 place. If the window is not in the right place, move it
9285 there, and take the potential window manager hit. */
9286 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9287 &rootw, &x, &y, &width, &height, &border, &depth);
9289 if (original_left != x || original_top != y)
9290 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
9291 original_left, original_top);
9293 UNBLOCK_INPUT;
9296 XSETFRAME (frame, f);
9298 /* Wait until the frame is visible. Process X events until a
9299 MapNotify event has been seen, or until we think we won't get a
9300 MapNotify at all.. */
9301 for (count = input_signal_count + 10;
9302 input_signal_count < count && !FRAME_VISIBLE_P (f);)
9304 /* Force processing of queued events. */
9305 x_sync (f);
9307 /* Machines that do polling rather than SIGIO have been
9308 observed to go into a busy-wait here. So we'll fake an
9309 alarm signal to let the handler know that there's something
9310 to be read. We used to raise a real alarm, but it seems
9311 that the handler isn't always enabled here. This is
9312 probably a bug. */
9313 if (input_polling_used ())
9315 /* It could be confusing if a real alarm arrives while
9316 processing the fake one. Turn it off and let the
9317 handler reset it. */
9318 extern void poll_for_input_1 P_ ((void));
9319 int old_poll_suppress_count = poll_suppress_count;
9320 poll_suppress_count = 1;
9321 poll_for_input_1 ();
9322 poll_suppress_count = old_poll_suppress_count;
9325 /* See if a MapNotify event has been processed. */
9326 FRAME_SAMPLE_VISIBILITY (f);
9329 /* 2000-09-28: In
9331 (let ((f (selected-frame)))
9332 (iconify-frame f)
9333 (raise-frame f))
9335 the frame is not raised with various window managers on
9336 FreeBSD, GNU/Linux and Solaris. It turns out that, for some
9337 unknown reason, the call to XtMapWidget is completely ignored.
9338 Mapping the widget a second time works. */
9340 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
9341 goto retry;
9345 /* Change from mapped state to withdrawn state. */
9347 /* Make the frame visible (mapped and not iconified). */
9349 void
9350 x_make_frame_invisible (f)
9351 struct frame *f;
9353 Window window;
9355 /* Use the frame's outermost window, not the one we normally draw on. */
9356 window = FRAME_OUTER_WINDOW (f);
9358 /* Don't keep the highlight on an invisible frame. */
9359 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9360 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9362 #if 0/* This might add unreliability; I don't trust it -- rms. */
9363 if (! f->async_visible && ! f->async_iconified)
9364 return;
9365 #endif
9367 BLOCK_INPUT;
9369 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
9370 that the current position of the window is user-specified, rather than
9371 program-specified, so that when the window is mapped again, it will be
9372 placed at the same location, without forcing the user to position it
9373 by hand again (they have already done that once for this window.) */
9374 x_wm_set_size_hint (f, (long) 0, 1);
9376 #ifdef USE_GTK
9377 if (FRAME_GTK_OUTER_WIDGET (f))
9378 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
9379 else
9380 #endif
9382 #ifdef HAVE_X11R4
9384 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
9385 DefaultScreen (FRAME_X_DISPLAY (f))))
9387 UNBLOCK_INPUT_RESIGNAL;
9388 error ("Can't notify window manager of window withdrawal");
9390 #else /* ! defined (HAVE_X11R4) */
9392 /* Tell the window manager what we're going to do. */
9393 if (! EQ (Vx_no_window_manager, Qt))
9395 XEvent unmap;
9397 unmap.xunmap.type = UnmapNotify;
9398 unmap.xunmap.window = window;
9399 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
9400 unmap.xunmap.from_configure = False;
9401 if (! XSendEvent (FRAME_X_DISPLAY (f),
9402 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9403 False,
9404 SubstructureRedirectMaskSubstructureNotifyMask,
9405 &unmap))
9407 UNBLOCK_INPUT_RESIGNAL;
9408 error ("Can't notify window manager of withdrawal");
9412 /* Unmap the window ourselves. Cheeky! */
9413 XUnmapWindow (FRAME_X_DISPLAY (f), window);
9414 #endif /* ! defined (HAVE_X11R4) */
9417 /* We can't distinguish this from iconification
9418 just by the event that we get from the server.
9419 So we can't win using the usual strategy of letting
9420 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
9421 and synchronize with the server to make sure we agree. */
9422 f->visible = 0;
9423 FRAME_ICONIFIED_P (f) = 0;
9424 f->async_visible = 0;
9425 f->async_iconified = 0;
9427 x_sync (f);
9429 UNBLOCK_INPUT;
9432 /* Change window state from mapped to iconified. */
9434 void
9435 x_iconify_frame (f)
9436 struct frame *f;
9438 int result;
9439 Lisp_Object type;
9441 /* Don't keep the highlight on an invisible frame. */
9442 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
9443 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
9445 if (f->async_iconified)
9446 return;
9448 BLOCK_INPUT;
9450 FRAME_SAMPLE_VISIBILITY (f);
9452 type = x_icon_type (f);
9453 if (!NILP (type))
9454 x_bitmap_icon (f, type);
9456 #ifdef USE_GTK
9457 if (FRAME_GTK_OUTER_WIDGET (f))
9459 if (! FRAME_VISIBLE_P (f))
9460 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
9462 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
9463 f->iconified = 1;
9464 f->visible = 1;
9465 f->async_iconified = 1;
9466 f->async_visible = 0;
9467 UNBLOCK_INPUT;
9468 return;
9470 #endif
9472 #ifdef USE_X_TOOLKIT
9474 if (! FRAME_VISIBLE_P (f))
9476 if (! EQ (Vx_no_window_manager, Qt))
9477 x_wm_set_window_state (f, IconicState);
9478 /* This was XtPopup, but that did nothing for an iconified frame. */
9479 XtMapWidget (f->output_data.x->widget);
9480 /* The server won't give us any event to indicate
9481 that an invisible frame was changed to an icon,
9482 so we have to record it here. */
9483 f->iconified = 1;
9484 f->visible = 1;
9485 f->async_iconified = 1;
9486 f->async_visible = 0;
9487 UNBLOCK_INPUT;
9488 return;
9491 result = XIconifyWindow (FRAME_X_DISPLAY (f),
9492 XtWindow (f->output_data.x->widget),
9493 DefaultScreen (FRAME_X_DISPLAY (f)));
9494 UNBLOCK_INPUT;
9496 if (!result)
9497 error ("Can't notify window manager of iconification");
9499 f->async_iconified = 1;
9500 f->async_visible = 0;
9503 BLOCK_INPUT;
9504 XFlush (FRAME_X_DISPLAY (f));
9505 UNBLOCK_INPUT;
9506 #else /* not USE_X_TOOLKIT */
9508 /* Make sure the X server knows where the window should be positioned,
9509 in case the user deiconifies with the window manager. */
9510 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
9511 x_set_offset (f, f->left_pos, f->top_pos, 0);
9513 /* Since we don't know which revision of X we're running, we'll use both
9514 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
9516 /* X11R4: send a ClientMessage to the window manager using the
9517 WM_CHANGE_STATE type. */
9519 XEvent message;
9521 message.xclient.window = FRAME_X_WINDOW (f);
9522 message.xclient.type = ClientMessage;
9523 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
9524 message.xclient.format = 32;
9525 message.xclient.data.l[0] = IconicState;
9527 if (! XSendEvent (FRAME_X_DISPLAY (f),
9528 DefaultRootWindow (FRAME_X_DISPLAY (f)),
9529 False,
9530 SubstructureRedirectMask | SubstructureNotifyMask,
9531 &message))
9533 UNBLOCK_INPUT_RESIGNAL;
9534 error ("Can't notify window manager of iconification");
9538 /* X11R3: set the initial_state field of the window manager hints to
9539 IconicState. */
9540 x_wm_set_window_state (f, IconicState);
9542 if (!FRAME_VISIBLE_P (f))
9544 /* If the frame was withdrawn, before, we must map it. */
9545 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9548 f->async_iconified = 1;
9549 f->async_visible = 0;
9551 XFlush (FRAME_X_DISPLAY (f));
9552 UNBLOCK_INPUT;
9553 #endif /* not USE_X_TOOLKIT */
9557 /* Free X resources of frame F. */
9559 void
9560 x_free_frame_resources (f)
9561 struct frame *f;
9563 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9564 Lisp_Object bar;
9565 struct scroll_bar *b;
9567 BLOCK_INPUT;
9569 /* If a display connection is dead, don't try sending more
9570 commands to the X server. */
9571 if (dpyinfo->display)
9573 #ifdef USE_FONT_BACKEND
9574 /* We must free faces before destroying windows because some
9575 font-driver (e.g. xft) access a window while finishing a
9576 face. */
9577 if (enable_font_backend
9578 && FRAME_FACE_CACHE (f))
9579 free_frame_faces (f);
9580 #endif /* USE_FONT_BACKEND */
9582 if (f->output_data.x->icon_desc)
9583 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
9585 #ifdef USE_X_TOOLKIT
9586 /* Explicitly destroy the scroll bars of the frame. Without
9587 this, we get "BadDrawable" errors from the toolkit later on,
9588 presumably from expose events generated for the disappearing
9589 toolkit scroll bars. */
9590 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
9592 b = XSCROLL_BAR (bar);
9593 x_scroll_bar_remove (b);
9595 #endif
9597 #ifdef HAVE_X_I18N
9598 if (FRAME_XIC (f))
9599 free_frame_xic (f);
9600 #endif
9602 #ifdef USE_X_TOOLKIT
9603 if (f->output_data.x->widget)
9605 XtDestroyWidget (f->output_data.x->widget);
9606 f->output_data.x->widget = NULL;
9608 /* Tooltips don't have widgets, only a simple X window, even if
9609 we are using a toolkit. */
9610 else if (FRAME_X_WINDOW (f))
9611 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9613 free_frame_menubar (f);
9614 #else /* !USE_X_TOOLKIT */
9616 #ifdef USE_GTK
9617 /* In the GTK version, tooltips are normal X
9618 frames. We must check and free both types. */
9619 if (FRAME_GTK_OUTER_WIDGET (f))
9621 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
9622 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
9623 FRAME_GTK_OUTER_WIDGET (f) = 0;
9625 #endif /* USE_GTK */
9627 if (FRAME_X_WINDOW (f))
9628 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
9629 #endif /* !USE_X_TOOLKIT */
9631 unload_color (f, f->output_data.x->foreground_pixel);
9632 unload_color (f, f->output_data.x->background_pixel);
9633 unload_color (f, f->output_data.x->cursor_pixel);
9634 unload_color (f, f->output_data.x->cursor_foreground_pixel);
9635 unload_color (f, f->output_data.x->border_pixel);
9636 unload_color (f, f->output_data.x->mouse_pixel);
9638 if (f->output_data.x->scroll_bar_background_pixel != -1)
9639 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
9640 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9641 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
9642 #ifdef USE_TOOLKIT_SCROLL_BARS
9643 /* Scrollbar shadow colors. */
9644 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
9645 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
9646 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
9647 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
9648 #endif /* USE_TOOLKIT_SCROLL_BARS */
9649 if (f->output_data.x->white_relief.allocated_p)
9650 unload_color (f, f->output_data.x->white_relief.pixel);
9651 if (f->output_data.x->black_relief.allocated_p)
9652 unload_color (f, f->output_data.x->black_relief.pixel);
9654 if (FRAME_FACE_CACHE (f))
9655 free_frame_faces (f);
9657 x_free_gcs (f);
9658 XFlush (FRAME_X_DISPLAY (f));
9661 if (f->output_data.x->saved_menu_event)
9662 xfree (f->output_data.x->saved_menu_event);
9664 xfree (f->output_data.x);
9665 f->output_data.x = NULL;
9667 if (f == dpyinfo->x_focus_frame)
9668 dpyinfo->x_focus_frame = 0;
9669 if (f == dpyinfo->x_focus_event_frame)
9670 dpyinfo->x_focus_event_frame = 0;
9671 if (f == dpyinfo->x_highlight_frame)
9672 dpyinfo->x_highlight_frame = 0;
9674 if (f == dpyinfo->mouse_face_mouse_frame)
9676 dpyinfo->mouse_face_beg_row
9677 = dpyinfo->mouse_face_beg_col = -1;
9678 dpyinfo->mouse_face_end_row
9679 = dpyinfo->mouse_face_end_col = -1;
9680 dpyinfo->mouse_face_window = Qnil;
9681 dpyinfo->mouse_face_deferred_gc = 0;
9682 dpyinfo->mouse_face_mouse_frame = 0;
9685 UNBLOCK_INPUT;
9689 /* Destroy the X window of frame F. */
9691 void
9692 x_destroy_window (f)
9693 struct frame *f;
9695 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
9697 /* If a display connection is dead, don't try sending more
9698 commands to the X server. */
9699 if (dpyinfo->display != 0)
9700 x_free_frame_resources (f);
9702 dpyinfo->reference_count--;
9706 /* Setting window manager hints. */
9708 /* Set the normal size hints for the window manager, for frame F.
9709 FLAGS is the flags word to use--or 0 meaning preserve the flags
9710 that the window now has.
9711 If USER_POSITION is nonzero, we set the USPosition
9712 flag (this is useful when FLAGS is 0).
9713 The GTK version is in gtkutils.c */
9715 #ifndef USE_GTK
9716 void
9717 x_wm_set_size_hint (f, flags, user_position)
9718 struct frame *f;
9719 long flags;
9720 int user_position;
9722 XSizeHints size_hints;
9724 #ifdef USE_X_TOOLKIT
9725 Arg al[2];
9726 int ac = 0;
9727 Dimension widget_width, widget_height;
9728 #endif
9730 Window window = FRAME_OUTER_WINDOW (f);
9732 /* Setting PMaxSize caused various problems. */
9733 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
9735 size_hints.x = f->left_pos;
9736 size_hints.y = f->top_pos;
9738 #ifdef USE_X_TOOLKIT
9739 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
9740 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
9741 XtGetValues (f->output_data.x->widget, al, ac);
9742 size_hints.height = widget_height;
9743 size_hints.width = widget_width;
9744 #else /* not USE_X_TOOLKIT */
9745 size_hints.height = FRAME_PIXEL_HEIGHT (f);
9746 size_hints.width = FRAME_PIXEL_WIDTH (f);
9747 #endif /* not USE_X_TOOLKIT */
9749 size_hints.width_inc = FRAME_COLUMN_WIDTH (f);
9750 size_hints.height_inc = FRAME_LINE_HEIGHT (f);
9751 size_hints.max_width
9752 = FRAME_X_DISPLAY_INFO (f)->width - FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9753 size_hints.max_height
9754 = FRAME_X_DISPLAY_INFO (f)->height - FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9756 /* Calculate the base and minimum sizes.
9758 (When we use the X toolkit, we don't do it here.
9759 Instead we copy the values that the widgets are using, below.) */
9760 #ifndef USE_X_TOOLKIT
9762 int base_width, base_height;
9763 int min_rows = 0, min_cols = 0;
9765 base_width = FRAME_TEXT_COLS_TO_PIXEL_WIDTH (f, 0);
9766 base_height = FRAME_TEXT_LINES_TO_PIXEL_HEIGHT (f, 0);
9768 check_frame_size (f, &min_rows, &min_cols);
9770 /* The window manager uses the base width hints to calculate the
9771 current number of rows and columns in the frame while
9772 resizing; min_width and min_height aren't useful for this
9773 purpose, since they might not give the dimensions for a
9774 zero-row, zero-column frame.
9776 We use the base_width and base_height members if we have
9777 them; otherwise, we set the min_width and min_height members
9778 to the size for a zero x zero frame. */
9780 #ifdef HAVE_X11R4
9781 size_hints.flags |= PBaseSize;
9782 size_hints.base_width = base_width;
9783 size_hints.base_height = base_height;
9784 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
9785 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
9786 #else
9787 size_hints.min_width = base_width;
9788 size_hints.min_height = base_height;
9789 #endif
9792 /* If we don't need the old flags, we don't need the old hint at all. */
9793 if (flags)
9795 size_hints.flags |= flags;
9796 goto no_read;
9798 #endif /* not USE_X_TOOLKIT */
9801 XSizeHints hints; /* Sometimes I hate X Windows... */
9802 long supplied_return;
9803 int value;
9805 #ifdef HAVE_X11R4
9806 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
9807 &supplied_return);
9808 #else
9809 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
9810 #endif
9812 #ifdef USE_X_TOOLKIT
9813 size_hints.base_height = hints.base_height;
9814 size_hints.base_width = hints.base_width;
9815 size_hints.min_height = hints.min_height;
9816 size_hints.min_width = hints.min_width;
9817 #endif
9819 if (flags)
9820 size_hints.flags |= flags;
9821 else
9823 if (value == 0)
9824 hints.flags = 0;
9825 if (hints.flags & PSize)
9826 size_hints.flags |= PSize;
9827 if (hints.flags & PPosition)
9828 size_hints.flags |= PPosition;
9829 if (hints.flags & USPosition)
9830 size_hints.flags |= USPosition;
9831 if (hints.flags & USSize)
9832 size_hints.flags |= USSize;
9836 #ifndef USE_X_TOOLKIT
9837 no_read:
9838 #endif
9840 #ifdef PWinGravity
9841 size_hints.win_gravity = f->win_gravity;
9842 size_hints.flags |= PWinGravity;
9844 if (user_position)
9846 size_hints.flags &= ~ PPosition;
9847 size_hints.flags |= USPosition;
9849 #endif /* PWinGravity */
9851 #ifdef HAVE_X11R4
9852 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9853 #else
9854 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
9855 #endif
9857 #endif /* not USE_GTK */
9859 /* Used for IconicState or NormalState */
9861 void
9862 x_wm_set_window_state (f, state)
9863 struct frame *f;
9864 int state;
9866 #ifdef USE_X_TOOLKIT
9867 Arg al[1];
9869 XtSetArg (al[0], XtNinitialState, state);
9870 XtSetValues (f->output_data.x->widget, al, 1);
9871 #else /* not USE_X_TOOLKIT */
9872 Window window = FRAME_X_WINDOW (f);
9874 f->output_data.x->wm_hints.flags |= StateHint;
9875 f->output_data.x->wm_hints.initial_state = state;
9877 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9878 #endif /* not USE_X_TOOLKIT */
9881 void
9882 x_wm_set_icon_pixmap (f, pixmap_id)
9883 struct frame *f;
9884 int pixmap_id;
9886 Pixmap icon_pixmap, icon_mask;
9888 #ifndef USE_X_TOOLKIT
9889 Window window = FRAME_OUTER_WINDOW (f);
9890 #endif
9892 if (pixmap_id > 0)
9894 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
9895 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
9896 icon_mask = x_bitmap_mask (f, pixmap_id);
9897 f->output_data.x->wm_hints.icon_mask = icon_mask;
9899 else
9901 /* It seems there is no way to turn off use of an icon pixmap.
9902 The following line does it, only if no icon has yet been created,
9903 for some window managers. But with mwm it crashes.
9904 Some people say it should clear the IconPixmapHint bit in this case,
9905 but that doesn't work, and the X consortium said it isn't the
9906 right thing at all. Since there is no way to win,
9907 best to explicitly give up. */
9908 #if 0
9909 f->output_data.x->wm_hints.icon_pixmap = None;
9910 f->output_data.x->wm_hints.icon_mask = None;
9911 #else
9912 return;
9913 #endif
9917 #ifdef USE_GTK
9919 xg_set_frame_icon (f, icon_pixmap, icon_mask);
9920 return;
9923 #elif defined (USE_X_TOOLKIT) /* same as in x_wm_set_window_state. */
9926 Arg al[1];
9927 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
9928 XtSetValues (f->output_data.x->widget, al, 1);
9929 XtSetArg (al[0], XtNiconMask, icon_mask);
9930 XtSetValues (f->output_data.x->widget, al, 1);
9933 #else /* not USE_X_TOOLKIT && not USE_GTK */
9935 f->output_data.x->wm_hints.flags |= (IconPixmapHint | IconMaskHint);
9936 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9938 #endif /* not USE_X_TOOLKIT && not USE_GTK */
9941 void
9942 x_wm_set_icon_position (f, icon_x, icon_y)
9943 struct frame *f;
9944 int icon_x, icon_y;
9946 Window window = FRAME_OUTER_WINDOW (f);
9948 f->output_data.x->wm_hints.flags |= IconPositionHint;
9949 f->output_data.x->wm_hints.icon_x = icon_x;
9950 f->output_data.x->wm_hints.icon_y = icon_y;
9952 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
9956 /***********************************************************************
9957 Fonts
9958 ***********************************************************************/
9960 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
9962 struct font_info *
9963 x_get_font_info (f, font_idx)
9964 FRAME_PTR f;
9965 int font_idx;
9967 return (FRAME_X_FONT_TABLE (f) + font_idx);
9971 /* Return a list of names of available fonts matching PATTERN on frame F.
9973 If SIZE is > 0, it is the size (maximum bounds width) of fonts
9974 to be listed.
9976 SIZE < 0 means include auto scaled fonts.
9978 Frame F null means we have not yet created any frame on X, and
9979 consult the first display in x_display_list. MAXNAMES sets a limit
9980 on how many fonts to match. */
9982 Lisp_Object
9983 x_list_fonts (f, pattern, size, maxnames)
9984 struct frame *f;
9985 Lisp_Object pattern;
9986 int size;
9987 int maxnames;
9989 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
9990 Lisp_Object tem, second_best;
9991 struct x_display_info *dpyinfo
9992 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
9993 Display *dpy = dpyinfo->display;
9994 int try_XLoadQueryFont = 0;
9995 int allow_auto_scaled_font = 0;
9997 if (size < 0)
9999 allow_auto_scaled_font = 1;
10000 size = 0;
10003 patterns = Fassoc (pattern, Valternate_fontname_alist);
10004 if (NILP (patterns))
10005 patterns = Fcons (pattern, Qnil);
10007 if (maxnames == 1 && !size)
10008 /* We can return any single font matching PATTERN. */
10009 try_XLoadQueryFont = 1;
10011 for (; CONSP (patterns); patterns = XCDR (patterns))
10013 int num_fonts;
10014 char **names = NULL;
10016 pattern = XCAR (patterns);
10017 /* See if we cached the result for this particular query.
10018 The cache is an alist of the form:
10019 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
10020 tem = XCDR (dpyinfo->name_list_element);
10021 key = Fcons (Fcons (pattern, make_number (maxnames)),
10022 allow_auto_scaled_font ? Qt : Qnil);
10023 list = Fassoc (key, tem);
10024 if (!NILP (list))
10026 list = Fcdr_safe (list);
10027 /* We have a cashed list. Don't have to get the list again. */
10028 goto label_cached;
10031 /* At first, put PATTERN in the cache. */
10033 BLOCK_INPUT;
10034 x_catch_errors (dpy);
10036 if (try_XLoadQueryFont)
10038 XFontStruct *font;
10039 unsigned long value;
10041 font = XLoadQueryFont (dpy, SDATA (pattern));
10042 if (x_had_errors_p (dpy))
10044 /* This error is perhaps due to insufficient memory on X
10045 server. Let's just ignore it. */
10046 font = NULL;
10047 x_clear_errors (dpy);
10050 if (font
10051 && XGetFontProperty (font, XA_FONT, &value))
10053 char *name = (char *) XGetAtomName (dpy, (Atom) value);
10054 int len = strlen (name);
10055 char *tmp;
10057 /* If DXPC (a Differential X Protocol Compressor)
10058 Ver.3.7 is running, XGetAtomName will return null
10059 string. We must avoid such a name. */
10060 if (len == 0)
10061 try_XLoadQueryFont = 0;
10062 else
10064 num_fonts = 1;
10065 names = (char **) alloca (sizeof (char *));
10066 /* Some systems only allow alloca assigned to a
10067 simple var. */
10068 tmp = (char *) alloca (len + 1); names[0] = tmp;
10069 bcopy (name, names[0], len + 1);
10070 XFree (name);
10073 else
10074 try_XLoadQueryFont = 0;
10076 if (font)
10077 XFreeFont (dpy, font);
10080 if (!try_XLoadQueryFont)
10082 /* We try at least 10 fonts because XListFonts will return
10083 auto-scaled fonts at the head. */
10084 if (maxnames < 0)
10086 int limit;
10088 for (limit = 500;;)
10090 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
10091 if (num_fonts == limit)
10093 BLOCK_INPUT;
10094 XFreeFontNames (names);
10095 UNBLOCK_INPUT;
10096 limit *= 2;
10098 else
10099 break;
10102 else
10103 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
10104 &num_fonts);
10106 if (x_had_errors_p (dpy))
10108 /* This error is perhaps due to insufficient memory on X
10109 server. Let's just ignore it. */
10110 names = NULL;
10111 x_clear_errors (dpy);
10115 x_uncatch_errors ();
10116 UNBLOCK_INPUT;
10118 if (names)
10120 int i;
10122 /* Make a list of all the fonts we got back.
10123 Store that in the font cache for the display. */
10124 for (i = 0; i < num_fonts; i++)
10126 int width = 0;
10127 char *p = names[i];
10128 int average_width = -1, resx = 0, dashes = 0;
10130 /* Count the number of dashes in NAMES[I]. If there are
10131 14 dashes, the field value following 9th dash
10132 (RESOLUTION_X) is nonzero, and the field value
10133 following 12th dash (AVERAGE_WIDTH) is 0, this is a
10134 auto-scaled font which is usually too ugly to be used
10135 for editing. Let's ignore it. */
10136 while (*p)
10137 if (*p++ == '-')
10139 dashes++;
10140 if (dashes == 7) /* PIXEL_SIZE field */
10141 width = atoi (p);
10142 else if (dashes == 9)
10143 resx = atoi (p);
10144 else if (dashes == 12) /* AVERAGE_WIDTH field */
10145 average_width = atoi (p);
10148 if (allow_auto_scaled_font
10149 || dashes < 14 || average_width != 0 || resx == 0)
10151 tem = build_string (names[i]);
10152 if (NILP (Fassoc (tem, list)))
10154 if (STRINGP (Vx_pixel_size_width_font_regexp)
10155 && ((fast_c_string_match_ignore_case
10156 (Vx_pixel_size_width_font_regexp, names[i]))
10157 >= 0))
10158 /* We can set the value of PIXEL_SIZE to the
10159 width of this font. */
10160 list = Fcons (Fcons (tem, make_number (width)), list);
10161 else
10162 /* For the moment, width is not known. */
10163 list = Fcons (Fcons (tem, Qnil), list);
10168 if (!try_XLoadQueryFont)
10170 BLOCK_INPUT;
10171 XFreeFontNames (names);
10172 UNBLOCK_INPUT;
10176 /* Now store the result in the cache. */
10177 XSETCDR (dpyinfo->name_list_element,
10178 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
10180 label_cached:
10181 if (NILP (list)) continue; /* Try the remaining alternatives. */
10183 newlist = second_best = Qnil;
10184 /* Make a list of the fonts that have the right width. */
10185 for (; CONSP (list); list = XCDR (list))
10187 int found_size;
10189 tem = XCAR (list);
10191 if (!CONSP (tem) || NILP (XCAR (tem)))
10192 continue;
10193 if (!size)
10195 newlist = Fcons (XCAR (tem), newlist);
10196 continue;
10199 if (!INTEGERP (XCDR (tem)))
10201 /* Since we have not yet known the size of this font, we
10202 must try slow function call XLoadQueryFont. */
10203 XFontStruct *thisinfo;
10205 BLOCK_INPUT;
10206 x_catch_errors (dpy);
10207 thisinfo = XLoadQueryFont (dpy,
10208 SDATA (XCAR (tem)));
10209 if (x_had_errors_p (dpy))
10211 /* This error is perhaps due to insufficient memory on X
10212 server. Let's just ignore it. */
10213 thisinfo = NULL;
10214 x_clear_errors (dpy);
10216 x_uncatch_errors ();
10217 UNBLOCK_INPUT;
10219 if (thisinfo)
10221 XSETCDR (tem,
10222 (thisinfo->min_bounds.width == 0
10223 ? make_number (0)
10224 : make_number (thisinfo->max_bounds.width)));
10225 BLOCK_INPUT;
10226 XFreeFont (dpy, thisinfo);
10227 UNBLOCK_INPUT;
10229 else
10230 /* For unknown reason, the previous call of XListFont had
10231 returned a font which can't be opened. Record the size
10232 as 0 not to try to open it again. */
10233 XSETCDR (tem, make_number (0));
10236 found_size = XINT (XCDR (tem));
10237 if (found_size == size)
10238 newlist = Fcons (XCAR (tem), newlist);
10239 else if (found_size > 0)
10241 if (NILP (second_best))
10242 second_best = tem;
10243 else if (found_size < size)
10245 if (XINT (XCDR (second_best)) > size
10246 || XINT (XCDR (second_best)) < found_size)
10247 second_best = tem;
10249 else
10251 if (XINT (XCDR (second_best)) > size
10252 && XINT (XCDR (second_best)) > found_size)
10253 second_best = tem;
10257 if (!NILP (newlist))
10258 break;
10259 else if (!NILP (second_best))
10261 newlist = Fcons (XCAR (second_best), Qnil);
10262 break;
10266 return newlist;
10270 #if GLYPH_DEBUG
10272 /* Check that FONT is valid on frame F. It is if it can be found in F's
10273 font table. */
10275 static void
10276 x_check_font (f, font)
10277 struct frame *f;
10278 XFontStruct *font;
10280 int i;
10281 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10283 xassert (font != NULL);
10285 for (i = 0; i < dpyinfo->n_fonts; i++)
10286 if (dpyinfo->font_table[i].name
10287 && font == dpyinfo->font_table[i].font)
10288 break;
10290 xassert (i < dpyinfo->n_fonts);
10293 #endif /* GLYPH_DEBUG != 0 */
10295 /* Set *W to the minimum width, *H to the minimum font height of FONT.
10296 Note: There are (broken) X fonts out there with invalid XFontStruct
10297 min_bounds contents. For example, handa@etl.go.jp reports that
10298 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
10299 have font->min_bounds.width == 0. */
10301 static INLINE void
10302 x_font_min_bounds (font, w, h)
10303 XFontStruct *font;
10304 int *w, *h;
10306 *h = FONT_HEIGHT (font);
10307 *w = font->min_bounds.width;
10309 /* Try to handle the case where FONT->min_bounds has invalid
10310 contents. Since the only font known to have invalid min_bounds
10311 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
10312 if (*w <= 0)
10313 *w = font->max_bounds.width;
10317 /* Compute the smallest character width and smallest font height over
10318 all fonts available on frame F. Set the members smallest_char_width
10319 and smallest_font_height in F's x_display_info structure to
10320 the values computed. Value is non-zero if smallest_font_height or
10321 smallest_char_width become smaller than they were before. */
10323 static int
10324 x_compute_min_glyph_bounds (f)
10325 struct frame *f;
10327 int i;
10328 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10329 XFontStruct *font;
10330 int old_width = dpyinfo->smallest_char_width;
10331 int old_height = dpyinfo->smallest_font_height;
10333 dpyinfo->smallest_font_height = 100000;
10334 dpyinfo->smallest_char_width = 100000;
10336 for (i = 0; i < dpyinfo->n_fonts; ++i)
10337 if (dpyinfo->font_table[i].name)
10339 struct font_info *fontp = dpyinfo->font_table + i;
10340 int w, h;
10342 font = (XFontStruct *) fontp->font;
10343 xassert (font != (XFontStruct *) ~0);
10344 x_font_min_bounds (font, &w, &h);
10346 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
10347 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
10350 xassert (dpyinfo->smallest_char_width > 0
10351 && dpyinfo->smallest_font_height > 0);
10353 return (dpyinfo->n_fonts == 1
10354 || dpyinfo->smallest_char_width < old_width
10355 || dpyinfo->smallest_font_height < old_height);
10359 /* Load font named FONTNAME of the size SIZE for frame F, and return a
10360 pointer to the structure font_info while allocating it dynamically.
10361 If SIZE is 0, load any size of font.
10362 If loading is failed, return NULL. */
10364 struct font_info *
10365 x_load_font (f, fontname, size)
10366 struct frame *f;
10367 register char *fontname;
10368 int size;
10370 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10371 Lisp_Object font_names;
10373 /* Get a list of all the fonts that match this name. Once we
10374 have a list of matching fonts, we compare them against the fonts
10375 we already have by comparing names. */
10376 font_names = x_list_fonts (f, build_string (fontname), size, 1);
10378 if (!NILP (font_names))
10380 Lisp_Object tail;
10381 int i;
10383 for (i = 0; i < dpyinfo->n_fonts; i++)
10384 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
10385 if (dpyinfo->font_table[i].name
10386 && (!strcmp (dpyinfo->font_table[i].name,
10387 SDATA (XCAR (tail)))
10388 || !strcmp (dpyinfo->font_table[i].full_name,
10389 SDATA (XCAR (tail)))))
10390 return (dpyinfo->font_table + i);
10393 /* Load the font and add it to the table. */
10395 char *full_name;
10396 XFontStruct *font;
10397 struct font_info *fontp;
10398 unsigned long value;
10399 int i;
10401 /* If we have found fonts by x_list_font, load one of them. If
10402 not, we still try to load a font by the name given as FONTNAME
10403 because XListFonts (called in x_list_font) of some X server has
10404 a bug of not finding a font even if the font surely exists and
10405 is loadable by XLoadQueryFont. */
10406 if (size > 0 && !NILP (font_names))
10407 fontname = (char *) SDATA (XCAR (font_names));
10409 BLOCK_INPUT;
10410 x_catch_errors (FRAME_X_DISPLAY (f));
10411 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
10412 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
10414 /* This error is perhaps due to insufficient memory on X
10415 server. Let's just ignore it. */
10416 font = NULL;
10417 x_clear_errors (FRAME_X_DISPLAY (f));
10419 x_uncatch_errors ();
10420 UNBLOCK_INPUT;
10421 if (!font)
10422 return NULL;
10424 /* Find a free slot in the font table. */
10425 for (i = 0; i < dpyinfo->n_fonts; ++i)
10426 if (dpyinfo->font_table[i].name == NULL)
10427 break;
10429 /* If no free slot found, maybe enlarge the font table. */
10430 if (i == dpyinfo->n_fonts
10431 && dpyinfo->n_fonts == dpyinfo->font_table_size)
10433 int sz;
10434 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
10435 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
10436 dpyinfo->font_table
10437 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
10440 fontp = dpyinfo->font_table + i;
10441 if (i == dpyinfo->n_fonts)
10442 ++dpyinfo->n_fonts;
10444 /* Now fill in the slots of *FONTP. */
10445 BLOCK_INPUT;
10446 bzero (fontp, sizeof (*fontp));
10447 fontp->font = font;
10448 fontp->font_idx = i;
10449 fontp->charset = -1; /* fs_load_font sets it. */
10450 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
10451 bcopy (fontname, fontp->name, strlen (fontname) + 1);
10453 if (font->min_bounds.width == font->max_bounds.width)
10455 /* Fixed width font. */
10456 fontp->average_width = fontp->space_width = font->min_bounds.width;
10458 else
10460 XChar2b char2b;
10461 XCharStruct *pcm;
10463 char2b.byte1 = 0x00, char2b.byte2 = 0x20;
10464 pcm = x_per_char_metric (font, &char2b, 0);
10465 if (pcm)
10466 fontp->space_width = pcm->width;
10467 else
10468 fontp->space_width = FONT_WIDTH (font);
10470 fontp->average_width
10471 = (XGetFontProperty (font, dpyinfo->Xatom_AVERAGE_WIDTH, &value)
10472 ? (long) value / 10 : 0);
10473 if (fontp->average_width < 0)
10474 fontp->average_width = - fontp->average_width;
10475 if (fontp->average_width == 0)
10477 if (pcm)
10479 int width = pcm->width;
10480 for (char2b.byte2 = 33; char2b.byte2 <= 126; char2b.byte2++)
10481 if ((pcm = x_per_char_metric (font, &char2b, 0)) != NULL)
10482 width += pcm->width;
10483 fontp->average_width = width / 95;
10485 else
10486 fontp->average_width = FONT_WIDTH (font);
10490 /* Try to get the full name of FONT. Put it in FULL_NAME. */
10491 full_name = 0;
10492 if (XGetFontProperty (font, XA_FONT, &value))
10494 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
10495 char *p = name;
10496 int dashes = 0;
10498 /* Count the number of dashes in the "full name".
10499 If it is too few, this isn't really the font's full name,
10500 so don't use it.
10501 In X11R4, the fonts did not come with their canonical names
10502 stored in them. */
10503 while (*p)
10505 if (*p == '-')
10506 dashes++;
10507 p++;
10510 if (dashes >= 13)
10512 full_name = (char *) xmalloc (p - name + 1);
10513 bcopy (name, full_name, p - name + 1);
10516 XFree (name);
10519 if (full_name != 0)
10520 fontp->full_name = full_name;
10521 else
10522 fontp->full_name = fontp->name;
10524 fontp->size = font->max_bounds.width;
10525 fontp->height = FONT_HEIGHT (font);
10527 if (NILP (font_names))
10529 /* We come here because of a bug of XListFonts mentioned at
10530 the head of this block. Let's store this information in
10531 the cache for x_list_fonts. */
10532 Lisp_Object lispy_name = build_string (fontname);
10533 Lisp_Object lispy_full_name = build_string (fontp->full_name);
10534 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
10535 Qnil);
10537 XSETCDR (dpyinfo->name_list_element,
10538 Fcons (Fcons (key,
10539 Fcons (Fcons (lispy_full_name,
10540 make_number (fontp->size)),
10541 Qnil)),
10542 XCDR (dpyinfo->name_list_element)));
10543 if (full_name)
10545 key = Fcons (Fcons (lispy_full_name, make_number (256)),
10546 Qnil);
10547 XSETCDR (dpyinfo->name_list_element,
10548 Fcons (Fcons (key,
10549 Fcons (Fcons (lispy_full_name,
10550 make_number (fontp->size)),
10551 Qnil)),
10552 XCDR (dpyinfo->name_list_element)));
10556 /* The slot `encoding' specifies how to map a character
10557 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
10558 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
10559 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
10560 2:0xA020..0xFF7F). For the moment, we don't know which charset
10561 uses this font. So, we set information in fontp->encoding_type
10562 which is never used by any charset. If mapping can't be
10563 decided, set FONT_ENCODING_NOT_DECIDED. */
10564 fontp->encoding_type
10565 = (font->max_byte1 == 0
10566 /* 1-byte font */
10567 ? (font->min_char_or_byte2 < 0x80
10568 ? (font->max_char_or_byte2 < 0x80
10569 ? 0 /* 0x20..0x7F */
10570 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
10571 : 1) /* 0xA0..0xFF */
10572 /* 2-byte font */
10573 : (font->min_byte1 < 0x80
10574 ? (font->max_byte1 < 0x80
10575 ? (font->min_char_or_byte2 < 0x80
10576 ? (font->max_char_or_byte2 < 0x80
10577 ? 0 /* 0x2020..0x7F7F */
10578 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
10579 : 3) /* 0x20A0..0x7FFF */
10580 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
10581 : (font->min_char_or_byte2 < 0x80
10582 ? (font->max_char_or_byte2 < 0x80
10583 ? 2 /* 0xA020..0xFF7F */
10584 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
10585 : 1))); /* 0xA0A0..0xFFFF */
10587 fontp->baseline_offset
10588 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
10589 ? (long) value : 0);
10590 fontp->relative_compose
10591 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
10592 ? (long) value : 0);
10593 fontp->default_ascent
10594 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
10595 ? (long) value : 0);
10597 /* Set global flag fonts_changed_p to non-zero if the font loaded
10598 has a character with a smaller width than any other character
10599 before, or if the font loaded has a smaller height than any
10600 other font loaded before. If this happens, it will make a
10601 glyph matrix reallocation necessary. */
10602 fonts_changed_p |= x_compute_min_glyph_bounds (f);
10603 UNBLOCK_INPUT;
10604 return fontp;
10609 /* Return a pointer to struct font_info of a font named FONTNAME for
10610 frame F. If no such font is loaded, return NULL. */
10612 struct font_info *
10613 x_query_font (f, fontname)
10614 struct frame *f;
10615 register char *fontname;
10617 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10618 int i;
10620 for (i = 0; i < dpyinfo->n_fonts; i++)
10621 if (dpyinfo->font_table[i].name
10622 && (!xstricmp (dpyinfo->font_table[i].name, fontname)
10623 || !xstricmp (dpyinfo->font_table[i].full_name, fontname)))
10624 return (dpyinfo->font_table + i);
10625 return NULL;
10629 /* Find a CCL program for a font specified by FONTP, and set the member
10630 `encoder' of the structure. */
10632 void
10633 x_find_ccl_program (fontp)
10634 struct font_info *fontp;
10636 Lisp_Object list, elt;
10638 elt = Qnil;
10639 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
10641 elt = XCAR (list);
10642 if (CONSP (elt)
10643 && STRINGP (XCAR (elt))
10644 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
10645 >= 0)
10646 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
10647 >= 0)))
10648 break;
10651 if (! NILP (list))
10653 struct ccl_program *ccl
10654 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
10656 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
10657 xfree (ccl);
10658 else
10659 fontp->font_encoder = ccl;
10664 /* Return a char-table whose elements are t if the font FONT_INFO
10665 contains a glyph for the corresponding character, and nil if
10666 not. */
10668 Lisp_Object
10669 x_get_font_repertory (f, font_info)
10670 FRAME_PTR f;
10671 struct font_info *font_info;
10673 XFontStruct *font = (XFontStruct *) font_info->font;
10674 Lisp_Object table;
10675 int min_byte1, max_byte1, min_byte2, max_byte2;
10676 int c;
10677 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
10678 int offset = CHARSET_OFFSET (charset);
10680 table = Fmake_char_table (Qnil, Qnil);
10682 min_byte1 = font->min_byte1;
10683 max_byte1 = font->max_byte1;
10684 min_byte2 = font->min_char_or_byte2;
10685 max_byte2 = font->max_char_or_byte2;
10686 if (min_byte1 == 0 && max_byte1 == 0)
10688 if (! font->per_char || font->all_chars_exist == True)
10690 if (offset >= 0)
10691 char_table_set_range (table, offset + min_byte2,
10692 offset + max_byte2, Qt);
10693 else
10694 for (; min_byte2 <= max_byte2; min_byte2++)
10696 c = DECODE_CHAR (charset, min_byte2);
10697 CHAR_TABLE_SET (table, c, Qt);
10700 else
10702 XCharStruct *pcm = font->per_char;
10703 int from = -1;
10704 int i;
10706 for (i = min_byte2; i <= max_byte2; i++, pcm++)
10708 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
10710 if (from >= 0)
10712 if (offset >= 0)
10713 char_table_set_range (table, offset + from,
10714 offset + i - 1, Qt);
10715 else
10716 for (; from < i; from++)
10718 c = DECODE_CHAR (charset, from);
10719 CHAR_TABLE_SET (table, c, Qt);
10721 from = -1;
10724 else if (from < 0)
10725 from = i;
10727 if (from >= 0)
10729 if (offset >= 0)
10730 char_table_set_range (table, offset + from, offset + i - 1,
10731 Qt);
10732 else
10733 for (; from < i; from++)
10735 c = DECODE_CHAR (charset, from);
10736 CHAR_TABLE_SET (table, c, Qt);
10741 else
10743 if (! font->per_char || font->all_chars_exist == True)
10745 int i, j;
10747 if (offset >= 0)
10748 for (i = min_byte1; i <= max_byte1; i++)
10749 char_table_set_range
10750 (table, offset + ((i << 8) | min_byte2),
10751 offset + ((i << 8) | max_byte2), Qt);
10752 else
10753 for (i = min_byte1; i <= max_byte1; i++)
10754 for (j = min_byte2; j <= max_byte2; j++)
10756 unsigned code = (i << 8) | j;
10757 c = DECODE_CHAR (charset, code);
10758 CHAR_TABLE_SET (table, c, Qt);
10761 else
10763 XCharStruct *pcm = font->per_char;
10764 int i;
10766 for (i = min_byte1; i <= max_byte1; i++)
10768 int from = -1;
10769 int j;
10771 for (j = min_byte2; j <= max_byte2; j++, pcm++)
10773 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
10775 if (from >= 0)
10777 if (offset >= 0)
10778 char_table_set_range
10779 (table, offset + ((i << 8) | from),
10780 offset + ((i << 8) | (j - 1)), Qt);
10781 else
10783 for (; from < j; from++)
10785 unsigned code = (i << 8) | from;
10786 c = ENCODE_CHAR (charset, code);
10787 CHAR_TABLE_SET (table, c, Qt);
10790 from = -1;
10793 else if (from < 0)
10794 from = j;
10796 if (from >= 0)
10798 if (offset >= 0)
10799 char_table_set_range
10800 (table, offset + ((i << 8) | from),
10801 offset + ((i << 8) | (j - 1)), Qt);
10802 else
10804 for (; from < j; from++)
10806 unsigned code = (i << 8) | from;
10807 c = DECODE_CHAR (charset, code);
10808 CHAR_TABLE_SET (table, c, Qt);
10816 return table;
10819 /***********************************************************************
10820 Initialization
10821 ***********************************************************************/
10823 #ifdef USE_X_TOOLKIT
10824 static XrmOptionDescRec emacs_options[] = {
10825 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
10826 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
10828 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
10829 XrmoptionSepArg, NULL},
10830 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
10832 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10833 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10834 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
10835 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10836 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
10837 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
10838 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
10841 /* Whether atimer for Xt timeouts is activated or not. */
10843 static int x_timeout_atimer_activated_flag;
10845 #endif /* USE_X_TOOLKIT */
10847 static int x_initialized;
10849 #ifdef HAVE_X_SM
10850 static int x_session_initialized;
10851 #endif
10853 #ifdef MULTI_KBOARD
10854 /* Test whether two display-name strings agree up to the dot that separates
10855 the screen number from the server number. */
10856 static int
10857 same_x_server (name1, name2)
10858 const char *name1, *name2;
10860 int seen_colon = 0;
10861 const unsigned char *system_name = SDATA (Vsystem_name);
10862 int system_name_length = strlen (system_name);
10863 int length_until_period = 0;
10865 while (system_name[length_until_period] != 0
10866 && system_name[length_until_period] != '.')
10867 length_until_period++;
10869 /* Treat `unix' like an empty host name. */
10870 if (! strncmp (name1, "unix:", 5))
10871 name1 += 4;
10872 if (! strncmp (name2, "unix:", 5))
10873 name2 += 4;
10874 /* Treat this host's name like an empty host name. */
10875 if (! strncmp (name1, system_name, system_name_length)
10876 && name1[system_name_length] == ':')
10877 name1 += system_name_length;
10878 if (! strncmp (name2, system_name, system_name_length)
10879 && name2[system_name_length] == ':')
10880 name2 += system_name_length;
10881 /* Treat this host's domainless name like an empty host name. */
10882 if (! strncmp (name1, system_name, length_until_period)
10883 && name1[length_until_period] == ':')
10884 name1 += length_until_period;
10885 if (! strncmp (name2, system_name, length_until_period)
10886 && name2[length_until_period] == ':')
10887 name2 += length_until_period;
10889 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
10891 if (*name1 == ':')
10892 seen_colon++;
10893 if (seen_colon && *name1 == '.')
10894 return 1;
10896 return (seen_colon
10897 && (*name1 == '.' || *name1 == '\0')
10898 && (*name2 == '.' || *name2 == '\0'));
10900 #endif
10902 /* Count number of set bits in mask and number of bits to shift to
10903 get to the first bit. With MASK 0x7e0, *BITS is set to 6, and *OFFSET
10904 to 5. */
10905 static void
10906 get_bits_and_offset (mask, bits, offset)
10907 unsigned long mask;
10908 int *bits;
10909 int *offset;
10911 int nr = 0;
10912 int off = 0;
10914 while (!(mask & 1))
10916 off++;
10917 mask >>= 1;
10920 while (mask & 1)
10922 nr++;
10923 mask >>= 1;
10926 *offset = off;
10927 *bits = nr;
10931 x_display_ok (display)
10932 const char * display;
10934 int dpy_ok = 1;
10935 Display *dpy;
10937 dpy = XOpenDisplay (display);
10938 if (dpy)
10939 XCloseDisplay (dpy);
10940 else
10941 dpy_ok = 0;
10942 return dpy_ok;
10945 struct x_display_info *
10946 x_term_init (display_name, xrm_option, resource_name)
10947 Lisp_Object display_name;
10948 char *xrm_option;
10949 char *resource_name;
10951 int connection;
10952 Display *dpy;
10953 struct x_display_info *dpyinfo;
10954 XrmDatabase xrdb;
10956 BLOCK_INPUT;
10958 if (!x_initialized)
10960 x_initialize ();
10961 ++x_initialized;
10964 #ifdef USE_GTK
10966 #define NUM_ARGV 10
10967 int argc;
10968 char *argv[NUM_ARGV];
10969 char **argv2 = argv;
10970 GdkAtom atom;
10972 if (x_initialized++ > 1)
10974 /* Opening another display. If xg_display_open returns less
10975 than zero, we are probably on GTK 2.0, which can only handle
10976 one display. GTK 2.2 or later can handle more than one. */
10977 if (xg_display_open (SDATA (display_name), &dpy) < 0)
10978 error ("Sorry, this version of GTK can only handle one display");
10980 else
10982 for (argc = 0; argc < NUM_ARGV; ++argc)
10983 argv[argc] = 0;
10985 argc = 0;
10986 argv[argc++] = initial_argv[0];
10988 if (! NILP (display_name))
10990 argv[argc++] = "--display";
10991 argv[argc++] = SDATA (display_name);
10994 argv[argc++] = "--name";
10995 argv[argc++] = resource_name;
10997 #ifdef HAVE_X11R5
10998 XSetLocaleModifiers ("");
10999 #endif
11001 gtk_init (&argc, &argv2);
11003 /* gtk_init does set_locale. We must fix locale after calling it. */
11004 fixup_locale ();
11005 xg_initialize ();
11007 dpy = GDK_DISPLAY ();
11009 /* NULL window -> events for all windows go to our function */
11010 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
11012 /* Load our own gtkrc if it exists. */
11014 char *file = "~/.emacs.d/gtkrc";
11015 Lisp_Object s, abs_file;
11017 s = make_string (file, strlen (file));
11018 abs_file = Fexpand_file_name (s, Qnil);
11020 if (! NILP (abs_file) && !NILP (Ffile_readable_p (abs_file)))
11021 gtk_rc_parse (SDATA (abs_file));
11024 XSetErrorHandler (x_error_handler);
11025 XSetIOErrorHandler (x_io_error_quitter);
11028 #else /* not USE_GTK */
11029 #ifdef USE_X_TOOLKIT
11030 /* weiner@footloose.sps.mot.com reports that this causes
11031 errors with X11R5:
11032 X protocol error: BadAtom (invalid Atom parameter)
11033 on protocol request 18skiloaf.
11034 So let's not use it until R6. */
11035 #ifdef HAVE_X11XTR6
11036 XtSetLanguageProc (NULL, NULL, NULL);
11037 #endif
11040 int argc = 0;
11041 char *argv[3];
11043 argv[0] = "";
11044 argc = 1;
11045 if (xrm_option)
11047 argv[argc++] = "-xrm";
11048 argv[argc++] = xrm_option;
11050 turn_on_atimers (0);
11051 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
11052 resource_name, EMACS_CLASS,
11053 emacs_options, XtNumber (emacs_options),
11054 &argc, argv);
11055 turn_on_atimers (1);
11057 #ifdef HAVE_X11XTR6
11058 /* I think this is to compensate for XtSetLanguageProc. */
11059 fixup_locale ();
11060 #endif
11063 #else /* not USE_X_TOOLKIT */
11064 #ifdef HAVE_X11R5
11065 XSetLocaleModifiers ("");
11066 #endif
11067 dpy = XOpenDisplay (SDATA (display_name));
11068 #endif /* not USE_X_TOOLKIT */
11069 #endif /* not USE_GTK*/
11071 /* Detect failure. */
11072 if (dpy == 0)
11074 UNBLOCK_INPUT;
11075 return 0;
11078 /* We have definitely succeeded. Record the new connection. */
11080 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
11081 bzero (dpyinfo, sizeof *dpyinfo);
11083 #ifdef MULTI_KBOARD
11085 struct x_display_info *share;
11086 Lisp_Object tail;
11088 for (share = x_display_list, tail = x_display_name_list; share;
11089 share = share->next, tail = XCDR (tail))
11090 if (same_x_server (SDATA (XCAR (XCAR (tail))),
11091 SDATA (display_name)))
11092 break;
11093 if (share)
11094 dpyinfo->kboard = share->kboard;
11095 else
11097 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
11098 init_kboard (dpyinfo->kboard);
11099 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
11101 char *vendor = ServerVendor (dpy);
11102 UNBLOCK_INPUT;
11103 dpyinfo->kboard->Vsystem_key_alist
11104 = call1 (Qvendor_specific_keysyms,
11105 vendor ? build_string (vendor) : empty_unibyte_string);
11106 BLOCK_INPUT;
11109 dpyinfo->kboard->next_kboard = all_kboards;
11110 all_kboards = dpyinfo->kboard;
11111 /* Don't let the initial kboard remain current longer than necessary.
11112 That would cause problems if a file loaded on startup tries to
11113 prompt in the mini-buffer. */
11114 if (current_kboard == initial_kboard)
11115 current_kboard = dpyinfo->kboard;
11117 dpyinfo->kboard->reference_count++;
11119 #endif
11121 /* Put this display on the chain. */
11122 dpyinfo->next = x_display_list;
11123 x_display_list = dpyinfo;
11125 /* Put it on x_display_name_list as well, to keep them parallel. */
11126 x_display_name_list = Fcons (Fcons (display_name, Qnil),
11127 x_display_name_list);
11128 dpyinfo->name_list_element = XCAR (x_display_name_list);
11130 dpyinfo->display = dpy;
11132 #if 0
11133 XSetAfterFunction (x_current_display, x_trace_wire);
11134 #endif /* ! 0 */
11136 dpyinfo->x_id_name
11137 = (char *) xmalloc (SBYTES (Vinvocation_name)
11138 + SBYTES (Vsystem_name)
11139 + 2);
11140 sprintf (dpyinfo->x_id_name, "%s@%s",
11141 SDATA (Vinvocation_name), SDATA (Vsystem_name));
11143 /* Figure out which modifier bits mean what. */
11144 x_find_modifier_meanings (dpyinfo);
11146 /* Get the scroll bar cursor. */
11147 #ifdef USE_GTK
11148 /* We must create a GTK cursor, it is required for GTK widgets. */
11149 dpyinfo->xg_cursor = xg_create_default_cursor (dpyinfo->display);
11150 #endif /* USE_GTK */
11152 dpyinfo->vertical_scroll_bar_cursor
11153 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
11155 xrdb = x_load_resources (dpyinfo->display, xrm_option,
11156 resource_name, EMACS_CLASS);
11157 #ifdef HAVE_XRMSETDATABASE
11158 XrmSetDatabase (dpyinfo->display, xrdb);
11159 #else
11160 dpyinfo->display->db = xrdb;
11161 #endif
11162 /* Put the rdb where we can find it in a way that works on
11163 all versions. */
11164 dpyinfo->xrdb = xrdb;
11166 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
11167 DefaultScreen (dpyinfo->display));
11168 select_visual (dpyinfo);
11169 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
11170 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
11171 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
11172 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
11173 dpyinfo->client_leader_window = 0;
11174 dpyinfo->grabbed = 0;
11175 dpyinfo->reference_count = 0;
11176 dpyinfo->icon_bitmap_id = -1;
11177 dpyinfo->font_table = NULL;
11178 dpyinfo->n_fonts = 0;
11179 dpyinfo->font_table_size = 0;
11180 dpyinfo->bitmaps = 0;
11181 dpyinfo->bitmaps_size = 0;
11182 dpyinfo->bitmaps_last = 0;
11183 dpyinfo->scratch_cursor_gc = 0;
11184 dpyinfo->mouse_face_mouse_frame = 0;
11185 dpyinfo->mouse_face_deferred_gc = 0;
11186 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
11187 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
11188 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
11189 dpyinfo->mouse_face_window = Qnil;
11190 dpyinfo->mouse_face_overlay = Qnil;
11191 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
11192 dpyinfo->mouse_face_defer = 0;
11193 dpyinfo->mouse_face_hidden = 0;
11194 dpyinfo->x_focus_frame = 0;
11195 dpyinfo->x_focus_event_frame = 0;
11196 dpyinfo->x_highlight_frame = 0;
11197 dpyinfo->image_cache = make_image_cache ();
11198 dpyinfo->wm_type = X_WMTYPE_UNKNOWN;
11200 /* See if we can construct pixel values from RGB values. */
11201 dpyinfo->red_bits = dpyinfo->blue_bits = dpyinfo->green_bits = 0;
11202 dpyinfo->red_offset = dpyinfo->blue_offset = dpyinfo->green_offset = 0;
11204 if (dpyinfo->visual->class == TrueColor)
11206 get_bits_and_offset (dpyinfo->visual->red_mask,
11207 &dpyinfo->red_bits, &dpyinfo->red_offset);
11208 get_bits_and_offset (dpyinfo->visual->blue_mask,
11209 &dpyinfo->blue_bits, &dpyinfo->blue_offset);
11210 get_bits_and_offset (dpyinfo->visual->green_mask,
11211 &dpyinfo->green_bits, &dpyinfo->green_offset);
11214 /* See if a private colormap is requested. */
11215 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
11217 if (dpyinfo->visual->class == PseudoColor)
11219 Lisp_Object value;
11220 value = display_x_get_resource (dpyinfo,
11221 build_string ("privateColormap"),
11222 build_string ("PrivateColormap"),
11223 Qnil, Qnil);
11224 if (STRINGP (value)
11225 && (!strcmp (SDATA (value), "true")
11226 || !strcmp (SDATA (value), "on")))
11227 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
11230 else
11231 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
11232 dpyinfo->visual, AllocNone);
11235 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
11236 double pixels = DisplayHeight (dpyinfo->display, screen_number);
11237 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
11238 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11239 dpyinfo->resy = (mm < 1) ? 100 : pixels * 25.4 / mm;
11240 pixels = DisplayWidth (dpyinfo->display, screen_number);
11241 /* Mac OS X 10.3's Xserver sometimes reports 0.0mm. */
11242 mm = DisplayWidthMM (dpyinfo->display, screen_number);
11243 dpyinfo->resx = (mm < 1) ? 100 : pixels * 25.4 / mm;
11246 dpyinfo->Xatom_wm_protocols
11247 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
11248 dpyinfo->Xatom_wm_take_focus
11249 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
11250 dpyinfo->Xatom_wm_save_yourself
11251 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
11252 dpyinfo->Xatom_wm_delete_window
11253 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
11254 dpyinfo->Xatom_wm_change_state
11255 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
11256 dpyinfo->Xatom_wm_configure_denied
11257 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
11258 dpyinfo->Xatom_wm_window_moved
11259 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
11260 dpyinfo->Xatom_wm_client_leader
11261 = XInternAtom (dpyinfo->display, "WM_CLIENT_LEADER", False);
11262 dpyinfo->Xatom_editres
11263 = XInternAtom (dpyinfo->display, "Editres", False);
11264 dpyinfo->Xatom_CLIPBOARD
11265 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
11266 dpyinfo->Xatom_TIMESTAMP
11267 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
11268 dpyinfo->Xatom_TEXT
11269 = XInternAtom (dpyinfo->display, "TEXT", False);
11270 dpyinfo->Xatom_COMPOUND_TEXT
11271 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
11272 dpyinfo->Xatom_UTF8_STRING
11273 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
11274 dpyinfo->Xatom_DELETE
11275 = XInternAtom (dpyinfo->display, "DELETE", False);
11276 dpyinfo->Xatom_MULTIPLE
11277 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
11278 dpyinfo->Xatom_INCR
11279 = XInternAtom (dpyinfo->display, "INCR", False);
11280 dpyinfo->Xatom_EMACS_TMP
11281 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
11282 dpyinfo->Xatom_TARGETS
11283 = XInternAtom (dpyinfo->display, "TARGETS", False);
11284 dpyinfo->Xatom_NULL
11285 = XInternAtom (dpyinfo->display, "NULL", False);
11286 dpyinfo->Xatom_ATOM_PAIR
11287 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
11288 /* For properties of font. */
11289 dpyinfo->Xatom_PIXEL_SIZE
11290 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
11291 dpyinfo->Xatom_AVERAGE_WIDTH
11292 = XInternAtom (dpyinfo->display, "AVERAGE_WIDTH", False);
11293 dpyinfo->Xatom_MULE_BASELINE_OFFSET
11294 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
11295 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
11296 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
11297 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
11298 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
11300 /* Ghostscript support. */
11301 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
11302 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
11304 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
11305 False);
11307 dpyinfo->cut_buffers_initialized = 0;
11309 dpyinfo->x_dnd_atoms_size = 8;
11310 dpyinfo->x_dnd_atoms_length = 0;
11311 dpyinfo->x_dnd_atoms = xmalloc (sizeof (*dpyinfo->x_dnd_atoms)
11312 * dpyinfo->x_dnd_atoms_size);
11314 dpyinfo->net_supported_atoms = NULL;
11315 dpyinfo->nr_net_supported_atoms = 0;
11316 dpyinfo->net_supported_window = 0;
11318 connection = ConnectionNumber (dpyinfo->display);
11319 dpyinfo->connection = connection;
11322 char null_bits[1];
11324 null_bits[0] = 0x00;
11326 dpyinfo->null_pixel
11327 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11328 null_bits, 1, 1, (long) 0, (long) 0,
11333 extern int gray_bitmap_width, gray_bitmap_height;
11334 extern char *gray_bitmap_bits;
11335 dpyinfo->gray
11336 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
11337 gray_bitmap_bits,
11338 gray_bitmap_width, gray_bitmap_height,
11339 (unsigned long) 1, (unsigned long) 0, 1);
11342 #ifdef HAVE_X_I18N
11343 xim_initialize (dpyinfo, resource_name);
11344 #endif
11346 #ifdef subprocesses
11347 /* This is only needed for distinguishing keyboard and process input. */
11348 if (connection != 0)
11349 add_keyboard_wait_descriptor (connection);
11350 #endif
11352 #ifndef F_SETOWN_BUG
11353 #ifdef F_SETOWN
11354 #ifdef F_SETOWN_SOCK_NEG
11355 /* stdin is a socket here */
11356 fcntl (connection, F_SETOWN, -getpid ());
11357 #else /* ! defined (F_SETOWN_SOCK_NEG) */
11358 fcntl (connection, F_SETOWN, getpid ());
11359 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
11360 #endif /* ! defined (F_SETOWN) */
11361 #endif /* F_SETOWN_BUG */
11363 #ifdef SIGIO
11364 if (interrupt_input)
11365 init_sigio (connection);
11366 #endif /* ! defined (SIGIO) */
11368 #ifdef USE_LUCID
11369 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
11370 /* Make sure that we have a valid font for dialog boxes
11371 so that Xt does not crash. */
11373 Display *dpy = dpyinfo->display;
11374 XrmValue d, fr, to;
11375 Font font;
11377 d.addr = (XPointer)&dpy;
11378 d.size = sizeof (Display *);
11379 fr.addr = XtDefaultFont;
11380 fr.size = sizeof (XtDefaultFont);
11381 to.size = sizeof (Font *);
11382 to.addr = (XPointer)&font;
11383 x_catch_errors (dpy);
11384 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
11385 abort ();
11386 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
11387 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
11388 x_uncatch_errors ();
11390 #endif
11391 #endif
11393 /* See if we should run in synchronous mode. This is useful
11394 for debugging X code. */
11396 Lisp_Object value;
11397 value = display_x_get_resource (dpyinfo,
11398 build_string ("synchronous"),
11399 build_string ("Synchronous"),
11400 Qnil, Qnil);
11401 if (STRINGP (value)
11402 && (!strcmp (SDATA (value), "true")
11403 || !strcmp (SDATA (value), "on")))
11404 XSynchronize (dpyinfo->display, True);
11408 Lisp_Object value;
11409 value = display_x_get_resource (dpyinfo,
11410 build_string ("useXIM"),
11411 build_string ("UseXIM"),
11412 Qnil, Qnil);
11413 #ifdef USE_XIM
11414 if (STRINGP (value)
11415 && (!strcmp (XSTRING (value)->data, "false")
11416 || !strcmp (XSTRING (value)->data, "off")))
11417 use_xim = 0;
11418 #else
11419 if (STRINGP (value)
11420 && (!strcmp (XSTRING (value)->data, "true")
11421 || !strcmp (XSTRING (value)->data, "on")))
11422 use_xim = 1;
11423 #endif
11426 #ifdef HAVE_X_SM
11427 /* Only do this for the first display. */
11428 if (!x_session_initialized++)
11429 x_session_initialize (dpyinfo);
11430 #endif
11432 UNBLOCK_INPUT;
11434 return dpyinfo;
11437 /* Get rid of display DPYINFO, assuming all frames are already gone,
11438 and without sending any more commands to the X server. */
11440 void
11441 x_delete_display (dpyinfo)
11442 struct x_display_info *dpyinfo;
11444 int i;
11446 delete_keyboard_wait_descriptor (dpyinfo->connection);
11448 /* Discard this display from x_display_name_list and x_display_list.
11449 We can't use Fdelq because that can quit. */
11450 if (! NILP (x_display_name_list)
11451 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
11452 x_display_name_list = XCDR (x_display_name_list);
11453 else
11455 Lisp_Object tail;
11457 tail = x_display_name_list;
11458 while (CONSP (tail) && CONSP (XCDR (tail)))
11460 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
11462 XSETCDR (tail, XCDR (XCDR (tail)));
11463 break;
11465 tail = XCDR (tail);
11469 if (next_noop_dpyinfo == dpyinfo)
11470 next_noop_dpyinfo = dpyinfo->next;
11472 if (x_display_list == dpyinfo)
11473 x_display_list = dpyinfo->next;
11474 else
11476 struct x_display_info *tail;
11478 for (tail = x_display_list; tail; tail = tail->next)
11479 if (tail->next == dpyinfo)
11480 tail->next = tail->next->next;
11483 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
11484 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
11485 XrmDestroyDatabase (dpyinfo->xrdb);
11486 #endif
11487 #endif
11488 #ifdef MULTI_KBOARD
11489 if (--dpyinfo->kboard->reference_count == 0)
11490 delete_kboard (dpyinfo->kboard);
11491 #endif
11492 #ifdef HAVE_X_I18N
11493 if (dpyinfo->xim)
11494 xim_close_dpy (dpyinfo);
11495 #endif
11497 /* Free the font names in the font table. */
11498 for (i = 0; i < dpyinfo->n_fonts; i++)
11499 if (dpyinfo->font_table[i].name)
11501 if (dpyinfo->font_table[i].name != dpyinfo->font_table[i].full_name)
11502 xfree (dpyinfo->font_table[i].full_name);
11503 xfree (dpyinfo->font_table[i].name);
11506 if (dpyinfo->font_table)
11508 if (dpyinfo->font_table->font_encoder)
11509 xfree (dpyinfo->font_table->font_encoder);
11510 xfree (dpyinfo->font_table);
11512 if (dpyinfo->x_id_name)
11513 xfree (dpyinfo->x_id_name);
11514 if (dpyinfo->color_cells)
11515 xfree (dpyinfo->color_cells);
11516 xfree (dpyinfo);
11519 #ifdef USE_X_TOOLKIT
11521 /* Atimer callback function for TIMER. Called every 0.1s to process
11522 Xt timeouts, if needed. We must avoid calling XtAppPending as
11523 much as possible because that function does an implicit XFlush
11524 that slows us down. */
11526 static void
11527 x_process_timeouts (timer)
11528 struct atimer *timer;
11530 BLOCK_INPUT;
11531 x_timeout_atimer_activated_flag = 0;
11532 if (toolkit_scroll_bar_interaction || popup_activated ())
11534 while (XtAppPending (Xt_app_con) & XtIMTimer)
11535 XtAppProcessEvent (Xt_app_con, XtIMTimer);
11536 /* Reactivate the atimer for next time. */
11537 x_activate_timeout_atimer ();
11539 UNBLOCK_INPUT;
11542 /* Install an asynchronous timer that processes Xt timeout events
11543 every 0.1s as long as either `toolkit_scroll_bar_interaction' or
11544 `popup_activated_flag' (in xmenu.c) is set. Make sure to call this
11545 function whenever these variables are set. This is necessary
11546 because some widget sets use timeouts internally, for example the
11547 LessTif menu bar, or the Xaw3d scroll bar. When Xt timeouts aren't
11548 processed, these widgets don't behave normally. */
11550 void
11551 x_activate_timeout_atimer ()
11553 BLOCK_INPUT;
11554 if (!x_timeout_atimer_activated_flag)
11556 EMACS_TIME interval;
11558 EMACS_SET_SECS_USECS (interval, 0, 100000);
11559 start_atimer (ATIMER_RELATIVE, interval, x_process_timeouts, 0);
11560 x_timeout_atimer_activated_flag = 1;
11562 UNBLOCK_INPUT;
11565 #endif /* USE_X_TOOLKIT */
11568 /* Set up use of X before we make the first connection. */
11570 extern frame_parm_handler x_frame_parm_handlers[];
11572 static struct redisplay_interface x_redisplay_interface =
11574 x_frame_parm_handlers,
11575 x_produce_glyphs,
11576 x_write_glyphs,
11577 x_insert_glyphs,
11578 x_clear_end_of_line,
11579 x_scroll_run,
11580 x_after_update_window_line,
11581 x_update_window_begin,
11582 x_update_window_end,
11583 x_cursor_to,
11584 x_flush,
11585 #ifdef XFlush
11586 x_flush,
11587 #else
11588 0, /* flush_display_optional */
11589 #endif
11590 x_clear_window_mouse_face,
11591 x_get_glyph_overhangs,
11592 x_fix_overlapping_area,
11593 x_draw_fringe_bitmap,
11594 0, /* define_fringe_bitmap */
11595 0, /* destroy_fringe_bitmap */
11596 x_per_char_metric,
11597 x_encode_char,
11598 x_compute_glyph_string_overhangs,
11599 x_draw_glyph_string,
11600 x_define_frame_cursor,
11601 x_clear_frame_area,
11602 x_draw_window_cursor,
11603 x_draw_vertical_window_border,
11604 x_shift_glyphs_for_insert
11607 void
11608 x_initialize ()
11610 rif = &x_redisplay_interface;
11612 clear_frame_hook = x_clear_frame;
11613 ins_del_lines_hook = x_ins_del_lines;
11614 delete_glyphs_hook = x_delete_glyphs;
11615 ring_bell_hook = XTring_bell;
11616 reset_terminal_modes_hook = XTreset_terminal_modes;
11617 set_terminal_modes_hook = XTset_terminal_modes;
11618 update_begin_hook = x_update_begin;
11619 update_end_hook = x_update_end;
11620 set_terminal_window_hook = XTset_terminal_window;
11621 read_socket_hook = XTread_socket;
11622 frame_up_to_date_hook = XTframe_up_to_date;
11623 mouse_position_hook = XTmouse_position;
11624 frame_rehighlight_hook = XTframe_rehighlight;
11625 frame_raise_lower_hook = XTframe_raise_lower;
11626 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
11627 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
11628 redeem_scroll_bar_hook = XTredeem_scroll_bar;
11629 judge_scroll_bars_hook = XTjudge_scroll_bars;
11630 fullscreen_hook = XTfullscreen_hook;
11632 scroll_region_ok = 1; /* we'll scroll partial frames */
11633 char_ins_del_ok = 1;
11634 line_ins_del_ok = 1; /* we'll just blt 'em */
11635 fast_clear_end_of_line = 1; /* X does this well */
11636 memory_below_frame = 0; /* we don't remember what scrolls
11637 off the bottom */
11638 baud_rate = 19200;
11640 x_noop_count = 0;
11641 last_tool_bar_item = -1;
11642 any_help_event_p = 0;
11643 ignore_next_mouse_click_timeout = 0;
11644 #ifdef HAVE_X_SM
11645 x_session_initialized = 0;
11646 #endif
11648 #ifdef USE_GTK
11649 current_count = -1;
11650 #endif
11652 /* Try to use interrupt input; if we can't, then start polling. */
11653 Fset_input_mode (Qt, Qnil, Qt, Qnil);
11655 #ifdef USE_X_TOOLKIT
11656 XtToolkitInitialize ();
11658 Xt_app_con = XtCreateApplicationContext ();
11660 /* Register a converter from strings to pixels, which uses
11661 Emacs' color allocation infrastructure. */
11662 XtAppSetTypeConverter (Xt_app_con,
11663 XtRString, XtRPixel, cvt_string_to_pixel,
11664 cvt_string_to_pixel_args,
11665 XtNumber (cvt_string_to_pixel_args),
11666 XtCacheByDisplay, cvt_pixel_dtor);
11668 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
11669 #endif
11671 #ifdef USE_TOOLKIT_SCROLL_BARS
11672 #ifndef USE_GTK
11673 xaw3d_arrow_scroll = False;
11674 xaw3d_pick_top = True;
11675 #endif
11676 #endif
11678 /* Note that there is no real way portable across R3/R4 to get the
11679 original error handler. */
11680 XSetErrorHandler (x_error_handler);
11681 XSetIOErrorHandler (x_io_error_quitter);
11683 /* Disable Window Change signals; they are handled by X events. */
11684 #ifdef SIGWINCH
11685 signal (SIGWINCH, SIG_DFL);
11686 #endif /* SIGWINCH */
11688 signal (SIGPIPE, x_connection_signal);
11692 void
11693 syms_of_xterm ()
11695 x_error_message = NULL;
11697 staticpro (&x_display_name_list);
11698 x_display_name_list = Qnil;
11700 staticpro (&last_mouse_scroll_bar);
11701 last_mouse_scroll_bar = Qnil;
11703 staticpro (&Qvendor_specific_keysyms);
11704 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
11706 staticpro (&Qlatin_1);
11707 Qlatin_1 = intern ("latin-1");
11709 staticpro (&last_mouse_press_frame);
11710 last_mouse_press_frame = Qnil;
11712 DEFVAR_BOOL ("x-use-underline-position-properties",
11713 &x_use_underline_position_properties,
11714 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
11715 A value of nil means ignore them. If you encounter fonts with bogus
11716 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
11717 to 4.1, set this to nil. */);
11718 x_use_underline_position_properties = 1;
11720 DEFVAR_BOOL ("x-underline-at-descent-line",
11721 &x_underline_at_descent_line,
11722 doc: /* *Non-nil means to draw the underline at the same place as the descent line.
11723 A value of nil means to draw the underline according to the value of the
11724 variable `x-use-underline-position-properties', which is usually at the
11725 baseline level. The default value is nil. */);
11726 x_underline_at_descent_line = 0;
11728 DEFVAR_BOOL ("x-mouse-click-focus-ignore-position",
11729 &x_mouse_click_focus_ignore_position,
11730 doc: /* Non-nil means that a mouse click to focus a frame does not move point.
11731 This variable is only used when the window manager requires that you
11732 click on a frame to select it (give it focus). In that case, a value
11733 of nil, means that the selected window and cursor position changes to
11734 reflect the mouse click position, while a non-nil value means that the
11735 selected window or cursor position is preserved. */);
11736 x_mouse_click_focus_ignore_position = 0;
11738 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
11739 doc: /* What X toolkit scroll bars Emacs uses.
11740 A value of nil means Emacs doesn't use X toolkit scroll bars.
11741 Otherwise, value is a symbol describing the X toolkit. */);
11742 #ifdef USE_TOOLKIT_SCROLL_BARS
11743 #ifdef USE_MOTIF
11744 Vx_toolkit_scroll_bars = intern ("motif");
11745 #elif defined HAVE_XAW3D
11746 Vx_toolkit_scroll_bars = intern ("xaw3d");
11747 #elif USE_GTK
11748 Vx_toolkit_scroll_bars = intern ("gtk");
11749 #else
11750 Vx_toolkit_scroll_bars = intern ("xaw");
11751 #endif
11752 #else
11753 Vx_toolkit_scroll_bars = Qnil;
11754 #endif
11756 staticpro (&last_mouse_motion_frame);
11757 last_mouse_motion_frame = Qnil;
11759 Qmodifier_value = intern ("modifier-value");
11760 Qalt = intern ("alt");
11761 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
11762 Qhyper = intern ("hyper");
11763 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
11764 Qmeta = intern ("meta");
11765 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
11766 Qsuper = intern ("super");
11767 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
11769 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
11770 doc: /* Which keys Emacs uses for the alt modifier.
11771 This should be one of the symbols `alt', `hyper', `meta', `super'.
11772 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
11773 is nil, which is the same as `alt'. */);
11774 Vx_alt_keysym = Qnil;
11776 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
11777 doc: /* Which keys Emacs uses for the hyper modifier.
11778 This should be one of the symbols `alt', `hyper', `meta', `super'.
11779 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
11780 default is nil, which is the same as `hyper'. */);
11781 Vx_hyper_keysym = Qnil;
11783 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
11784 doc: /* Which keys Emacs uses for the meta modifier.
11785 This should be one of the symbols `alt', `hyper', `meta', `super'.
11786 For example, `meta' means use the Meta_L and Meta_R keysyms. The
11787 default is nil, which is the same as `meta'. */);
11788 Vx_meta_keysym = Qnil;
11790 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
11791 doc: /* Which keys Emacs uses for the super modifier.
11792 This should be one of the symbols `alt', `hyper', `meta', `super'.
11793 For example, `super' means use the Super_L and Super_R keysyms. The
11794 default is nil, which is the same as `super'. */);
11795 Vx_super_keysym = Qnil;
11797 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
11798 doc: /* Hash table of character codes indexed by X keysym codes. */);
11799 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
11800 make_float (DEFAULT_REHASH_SIZE),
11801 make_float (DEFAULT_REHASH_THRESHOLD),
11802 Qnil, Qnil, Qnil);
11805 #endif /* HAVE_X_WINDOWS */
11807 /* arch-tag: 6d4e4cb7-abc1-4302-9585-d84dcfb09d0f
11808 (do not change this comment) */