(x_term_init) [USE_GTK]: Fix typo.
[emacs.git] / src / xterm.c
blob5feea82f3c152f194f0abb4eaedecb557e6ef3af
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001, 2002
3 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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #include "gnu.h"
80 #include "disptab.h"
81 #include "buffer.h"
82 #include "window.h"
83 #include "keyboard.h"
84 #include "intervals.h"
85 #include "process.h"
86 #include "atimer.h"
87 #include "keymap.h"
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
100 #ifdef USE_GTK
101 #include "gtkutil.h"
102 #endif
104 #ifdef USE_LUCID
105 extern int xlwmenu_window_p P_ ((Widget w, Window window));
106 extern void xlwmenu_redisplay P_ ((Widget));
107 #endif
109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
111 extern void free_frame_menubar P_ ((struct frame *));
112 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
113 int));
114 #endif
116 #ifdef USE_X_TOOLKIT
117 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
118 #define HACK_EDITRES
119 extern void _XEditResCheckMessages ();
120 #endif /* not NO_EDITRES */
122 /* Include toolkit specific headers for the scroll bar widget. */
124 #ifdef USE_TOOLKIT_SCROLL_BARS
125 #if defined USE_MOTIF
126 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
127 #include <Xm/ScrollBar.h>
128 #else /* !USE_MOTIF i.e. use Xaw */
130 #ifdef HAVE_XAW3D
131 #include <X11/Xaw3d/Simple.h>
132 #include <X11/Xaw3d/Scrollbar.h>
133 #define ARROW_SCROLLBAR
134 #include <X11/Xaw3d/ScrollbarP.h>
135 #else /* !HAVE_XAW3D */
136 #include <X11/Xaw/Simple.h>
137 #include <X11/Xaw/Scrollbar.h>
138 #endif /* !HAVE_XAW3D */
139 #ifndef XtNpickTop
140 #define XtNpickTop "pickTop"
141 #endif /* !XtNpickTop */
142 #endif /* !USE_MOTIF */
143 #endif /* USE_TOOLKIT_SCROLL_BARS */
145 #endif /* USE_X_TOOLKIT */
147 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
148 #define x_any_window_to_frame x_window_to_frame
149 #define x_top_window_to_frame x_window_to_frame
150 #endif
152 #ifdef USE_X_TOOLKIT
153 #include "widget.h"
154 #ifndef XtNinitialState
155 #define XtNinitialState "initialState"
156 #endif
157 #endif
159 #define abs(x) ((x) < 0 ? -(x) : (x))
161 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
165 extern Lisp_Object Qhelp_echo;
167 /* Non-nil means Emacs uses toolkit scroll bars. */
169 Lisp_Object Vx_toolkit_scroll_bars;
171 /* If a string, XTread_socket generates an event to display that string.
172 (The display is done in read_char.) */
174 static Lisp_Object help_echo;
175 static Lisp_Object help_echo_window;
176 static Lisp_Object help_echo_object;
177 static int help_echo_pos;
179 /* Temporary variable for XTread_socket. */
181 static Lisp_Object previous_help_echo;
183 /* Non-zero means that a HELP_EVENT has been generated since Emacs
184 start. */
186 static int any_help_event_p;
188 /* Non-zero means autoselect window with the mouse cursor. */
190 int mouse_autoselect_window;
192 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
193 static Lisp_Object last_window;
195 /* Non-zero means draw block and hollow cursor as wide as the glyph
196 under it. For example, if a block cursor is over a tab, it will be
197 drawn as wide as that tab on the display. */
199 int x_stretch_cursor_p;
201 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
203 int x_use_underline_position_properties;
205 /* This is a chain of structures for all the X displays currently in
206 use. */
208 struct x_display_info *x_display_list;
210 /* This is a list of cons cells, each of the form (NAME
211 . FONT-LIST-CACHE), one for each element of x_display_list and in
212 the same order. NAME is the name of the frame. FONT-LIST-CACHE
213 records previous values returned by x-list-fonts. */
215 Lisp_Object x_display_name_list;
217 /* Frame being updated by update_frame. This is declared in term.c.
218 This is set by update_begin and looked at by all the XT functions.
219 It is zero while not inside an update. In that case, the XT
220 functions assume that `selected_frame' is the frame to apply to. */
222 extern struct frame *updating_frame;
224 /* This is a frame waiting to be auto-raised, within XTread_socket. */
226 struct frame *pending_autoraise_frame;
228 #ifdef USE_X_TOOLKIT
229 /* The application context for Xt use. */
230 XtAppContext Xt_app_con;
231 static String Xt_default_resources[] = {0};
232 #endif /* USE_X_TOOLKIT */
234 /* Nominal cursor position -- where to draw output.
235 HPOS and VPOS are window relative glyph matrix coordinates.
236 X and Y are window relative pixel coordinates. */
238 struct cursor_pos output_cursor;
240 /* Non-zero means user is interacting with a toolkit scroll bar. */
242 static int toolkit_scroll_bar_interaction;
244 /* Mouse movement.
246 Formerly, we used PointerMotionHintMask (in standard_event_mask)
247 so that we would have to call XQueryPointer after each MotionNotify
248 event to ask for another such event. However, this made mouse tracking
249 slow, and there was a bug that made it eventually stop.
251 Simply asking for MotionNotify all the time seems to work better.
253 In order to avoid asking for motion events and then throwing most
254 of them away or busy-polling the server for mouse positions, we ask
255 the server for pointer motion hints. This means that we get only
256 one event per group of mouse movements. "Groups" are delimited by
257 other kinds of events (focus changes and button clicks, for
258 example), or by XQueryPointer calls; when one of these happens, we
259 get another MotionNotify event the next time the mouse moves. This
260 is at least as efficient as getting motion events when mouse
261 tracking is on, and I suspect only negligibly worse when tracking
262 is off. */
264 /* Where the mouse was last time we reported a mouse event. */
266 FRAME_PTR last_mouse_frame;
267 static XRectangle last_mouse_glyph;
268 static Lisp_Object last_mouse_press_frame;
270 /* The scroll bar in which the last X motion event occurred.
272 If the last X motion event occurred in a scroll bar, we set this so
273 XTmouse_position can know whether to report a scroll bar motion or
274 an ordinary motion.
276 If the last X motion event didn't occur in a scroll bar, we set
277 this to Qnil, to tell XTmouse_position to return an ordinary motion
278 event. */
280 static Lisp_Object last_mouse_scroll_bar;
282 /* This is a hack. We would really prefer that XTmouse_position would
283 return the time associated with the position it returns, but there
284 doesn't seem to be any way to wrest the time-stamp from the server
285 along with the position query. So, we just keep track of the time
286 of the last movement we received, and return that in hopes that
287 it's somewhat accurate. */
289 static Time last_mouse_movement_time;
291 /* Incremented by XTread_socket whenever it really tries to read
292 events. */
294 #ifdef __STDC__
295 static int volatile input_signal_count;
296 #else
297 static int input_signal_count;
298 #endif
300 /* Used locally within XTread_socket. */
302 static int x_noop_count;
304 /* Initial values of argv and argc. */
306 extern char **initial_argv;
307 extern int initial_argc;
309 extern Lisp_Object Vcommand_line_args, Vsystem_name;
311 /* Tells if a window manager is present or not. */
313 extern Lisp_Object Vx_no_window_manager;
315 extern Lisp_Object Qface, Qmouse_face, Qeql;
317 extern int errno;
319 /* A mask of extra modifier bits to put into every keyboard char. */
321 extern EMACS_INT extra_keyboard_modifiers;
323 /* The keysyms to use for the various modifiers. */
325 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
326 Lisp_Object Vx_keysym_table;
327 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
329 static Lisp_Object Qvendor_specific_keysyms;
330 static Lisp_Object Qlatin_1, Qutf_8;
332 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
333 extern Lisp_Object x_icon_type P_ ((struct frame *));
336 /* Enumeration for overriding/changing the face to use for drawing
337 glyphs in x_draw_glyphs. */
339 enum draw_glyphs_face
341 DRAW_NORMAL_TEXT,
342 DRAW_INVERSE_VIDEO,
343 DRAW_CURSOR,
344 DRAW_MOUSE_FACE,
345 DRAW_IMAGE_RAISED,
346 DRAW_IMAGE_SUNKEN
349 static int cursor_in_mouse_face_p P_ ((struct window *));
350 static int clear_mouse_face P_ ((struct x_display_info *));
351 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
352 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
353 static const XColor *x_color_cells P_ ((Display *, int *));
354 static void x_update_window_end P_ ((struct window *, int, int));
355 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
356 void x_delete_display P_ ((struct x_display_info *));
357 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
358 unsigned));
359 static int fast_find_position P_ ((struct window *, int, int *, int *,
360 int *, int *, Lisp_Object));
361 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
362 int *, int *, int *, int *, int));
363 static void set_output_cursor P_ ((struct cursor_pos *));
364 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
365 int *, int *, int *, int));
366 static void note_mode_line_or_margin_highlight P_ ((struct window *, int,
367 int, int));
368 static void note_mouse_highlight P_ ((struct frame *, int, int));
369 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
370 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
371 static void show_mouse_face P_ ((struct x_display_info *,
372 enum draw_glyphs_face));
373 static int x_io_error_quitter P_ ((Display *));
374 int x_catch_errors P_ ((Display *));
375 void x_uncatch_errors P_ ((Display *, int));
376 void x_lower_frame P_ ((struct frame *));
377 void x_scroll_bar_clear P_ ((struct frame *));
378 int x_had_errors_p P_ ((Display *));
379 void x_wm_set_size_hint P_ ((struct frame *, long, int));
380 void x_raise_frame P_ ((struct frame *));
381 void x_set_window_size P_ ((struct frame *, int, int, int));
382 void x_wm_set_window_state P_ ((struct frame *, int));
383 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
384 void x_initialize P_ ((void));
385 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
386 static int x_compute_min_glyph_bounds P_ ((struct frame *));
387 static void x_draw_phys_cursor_glyph P_ ((struct window *,
388 struct glyph_row *,
389 enum draw_glyphs_face));
390 static void x_update_end P_ ((struct frame *));
391 static void XTframe_up_to_date P_ ((struct frame *));
392 static void XTset_terminal_modes P_ ((void));
393 static void XTreset_terminal_modes P_ ((void));
394 static void XTcursor_to P_ ((int, int, int, int));
395 static void x_write_glyphs P_ ((struct glyph *, int));
396 static void x_clear_end_of_line P_ ((int));
397 static void x_clear_frame P_ ((void));
398 static void x_clear_cursor P_ ((struct window *));
399 static void frame_highlight P_ ((struct frame *));
400 static void frame_unhighlight P_ ((struct frame *));
401 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
402 static int x_focus_changed P_ ((int,
403 int,
404 struct x_display_info *,
405 struct frame *,
406 struct input_event *,
407 int));
408 static int x_detect_focus_change P_ ((struct x_display_info *,
409 XEvent *,
410 struct input_event *,
411 int));
412 static void XTframe_rehighlight P_ ((struct frame *));
413 static void x_frame_rehighlight P_ ((struct x_display_info *));
414 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
415 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int,
416 enum text_cursor_kinds));
417 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
418 XRectangle *));
419 static void expose_frame P_ ((struct frame *, int, int, int, int));
420 static int expose_window_tree P_ ((struct window *, XRectangle *));
421 static void expose_overlaps P_ ((struct window *, struct glyph_row *,
422 struct glyph_row *));
423 static int expose_window P_ ((struct window *, XRectangle *));
424 static void expose_area P_ ((struct window *, struct glyph_row *,
425 XRectangle *, enum glyph_row_area));
426 static int expose_line P_ ((struct window *, struct glyph_row *,
427 XRectangle *));
428 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
429 static void x_update_window_cursor P_ ((struct window *, int));
430 static void x_erase_phys_cursor P_ ((struct window *));
431 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
433 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
434 GC, int));
435 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
436 static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
437 int, int, int, int));
438 static void x_flush P_ ((struct frame *f));
439 static void x_update_begin P_ ((struct frame *));
440 static void x_update_window_begin P_ ((struct window *));
441 static void x_draw_vertical_border P_ ((struct window *));
442 static void x_after_update_window_line P_ ((struct glyph_row *));
443 static INLINE void take_vertical_position_into_account P_ ((struct it *));
444 static void x_produce_stretch_glyph P_ ((struct it *));
445 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
446 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
447 enum scroll_bar_part *,
448 Lisp_Object *, Lisp_Object *,
449 unsigned long *));
450 static void x_check_fullscreen P_ ((struct frame *));
451 static void x_check_fullscreen_move P_ ((struct frame *));
452 static int handle_one_xevent P_ ((struct x_display_info *,
453 XEvent *,
454 struct input_event **,
455 int *,
456 int *));
459 /* Flush display of frame F, or of all frames if F is null. */
461 static void
462 x_flush (f)
463 struct frame *f;
465 BLOCK_INPUT;
466 if (f == NULL)
468 Lisp_Object rest, frame;
469 FOR_EACH_FRAME (rest, frame)
470 x_flush (XFRAME (frame));
472 else if (FRAME_X_P (f))
473 XFlush (FRAME_X_DISPLAY (f));
474 UNBLOCK_INPUT;
478 /* Remove calls to XFlush by defining XFlush to an empty replacement.
479 Calls to XFlush should be unnecessary because the X output buffer
480 is flushed automatically as needed by calls to XPending,
481 XNextEvent, or XWindowEvent according to the XFlush man page.
482 XTread_socket calls XPending. Removing XFlush improves
483 performance. */
485 #define XFlush(DISPLAY) (void) 0
488 /***********************************************************************
489 Debugging
490 ***********************************************************************/
492 #if 0
494 /* This is a function useful for recording debugging information about
495 the sequence of occurrences in this file. */
497 struct record
499 char *locus;
500 int type;
503 struct record event_record[100];
505 int event_record_index;
507 record_event (locus, type)
508 char *locus;
509 int type;
511 if (event_record_index == sizeof (event_record) / sizeof (struct record))
512 event_record_index = 0;
514 event_record[event_record_index].locus = locus;
515 event_record[event_record_index].type = type;
516 event_record_index++;
519 #endif /* 0 */
523 /* Return the struct x_display_info corresponding to DPY. */
525 struct x_display_info *
526 x_display_info_for_display (dpy)
527 Display *dpy;
529 struct x_display_info *dpyinfo;
531 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
532 if (dpyinfo->display == dpy)
533 return dpyinfo;
535 return 0;
540 /***********************************************************************
541 Starting and ending an update
542 ***********************************************************************/
544 /* Start an update of frame F. This function is installed as a hook
545 for update_begin, i.e. it is called when update_begin is called.
546 This function is called prior to calls to x_update_window_begin for
547 each window being updated. Currently, there is nothing to do here
548 because all interesting stuff is done on a window basis. */
550 static void
551 x_update_begin (f)
552 struct frame *f;
554 /* Nothing to do. */
558 /* Start update of window W. Set the global variable updated_window
559 to the window being updated and set output_cursor to the cursor
560 position of W. */
562 static void
563 x_update_window_begin (w)
564 struct window *w;
566 struct frame *f = XFRAME (WINDOW_FRAME (w));
567 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
569 updated_window = w;
570 set_output_cursor (&w->cursor);
572 BLOCK_INPUT;
574 if (f == display_info->mouse_face_mouse_frame)
576 /* Don't do highlighting for mouse motion during the update. */
577 display_info->mouse_face_defer = 1;
579 /* If F needs to be redrawn, simply forget about any prior mouse
580 highlighting. */
581 if (FRAME_GARBAGED_P (f))
582 display_info->mouse_face_window = Qnil;
584 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
585 their mouse_face_p flag set, which means that they are always
586 unequal to rows in a desired matrix which never have that
587 flag set. So, rows containing mouse-face glyphs are never
588 scrolled, and we don't have to switch the mouse highlight off
589 here to prevent it from being scrolled. */
591 /* Can we tell that this update does not affect the window
592 where the mouse highlight is? If so, no need to turn off.
593 Likewise, don't do anything if the frame is garbaged;
594 in that case, the frame's current matrix that we would use
595 is all wrong, and we will redisplay that line anyway. */
596 if (!NILP (display_info->mouse_face_window)
597 && w == XWINDOW (display_info->mouse_face_window))
599 int i;
601 for (i = 0; i < w->desired_matrix->nrows; ++i)
602 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
603 break;
605 if (i < w->desired_matrix->nrows)
606 clear_mouse_face (display_info);
608 #endif /* 0 */
611 UNBLOCK_INPUT;
615 /* Draw a vertical window border to the right of window W if W doesn't
616 have vertical scroll bars. */
618 static void
619 x_draw_vertical_border (w)
620 struct window *w;
622 struct frame *f = XFRAME (WINDOW_FRAME (w));
624 /* Redraw borders between horizontally adjacent windows. Don't
625 do it for frames with vertical scroll bars because either the
626 right scroll bar of a window, or the left scroll bar of its
627 neighbor will suffice as a border. */
628 if (!WINDOW_RIGHTMOST_P (w)
629 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
631 int x0, x1, y0, y1;
633 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
634 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
635 y1 -= 1;
637 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
638 f->output_data.x->normal_gc, x1, y0, x1, y1);
643 /* End update of window W (which is equal to updated_window).
645 Draw vertical borders between horizontally adjacent windows, and
646 display W's cursor if CURSOR_ON_P is non-zero.
648 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
649 glyphs in mouse-face were overwritten. In that case we have to
650 make sure that the mouse-highlight is properly redrawn.
652 W may be a menu bar pseudo-window in case we don't have X toolkit
653 support. Such windows don't have a cursor, so don't display it
654 here. */
656 static void
657 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
658 struct window *w;
659 int cursor_on_p, mouse_face_overwritten_p;
661 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
663 if (!w->pseudo_window_p)
665 BLOCK_INPUT;
667 if (cursor_on_p)
668 x_display_and_set_cursor (w, 1, output_cursor.hpos,
669 output_cursor.vpos,
670 output_cursor.x, output_cursor.y);
672 x_draw_vertical_border (w);
673 UNBLOCK_INPUT;
676 /* If a row with mouse-face was overwritten, arrange for
677 XTframe_up_to_date to redisplay the mouse highlight. */
678 if (mouse_face_overwritten_p)
680 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
681 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
682 dpyinfo->mouse_face_window = Qnil;
685 updated_window = NULL;
689 /* End update of frame F. This function is installed as a hook in
690 update_end. */
692 static void
693 x_update_end (f)
694 struct frame *f;
696 /* Mouse highlight may be displayed again. */
697 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
699 BLOCK_INPUT;
700 XFlush (FRAME_X_DISPLAY (f));
701 UNBLOCK_INPUT;
705 /* This function is called from various places in xdisp.c whenever a
706 complete update has been performed. The global variable
707 updated_window is not available here. */
709 static void
710 XTframe_up_to_date (f)
711 struct frame *f;
713 if (FRAME_X_P (f))
715 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
717 if (dpyinfo->mouse_face_deferred_gc
718 || f == dpyinfo->mouse_face_mouse_frame)
720 BLOCK_INPUT;
721 if (dpyinfo->mouse_face_mouse_frame)
722 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
723 dpyinfo->mouse_face_mouse_x,
724 dpyinfo->mouse_face_mouse_y);
725 dpyinfo->mouse_face_deferred_gc = 0;
726 UNBLOCK_INPUT;
732 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
733 arrow bitmaps, or clear the fringes if no bitmaps are required
734 before DESIRED_ROW is made current. The window being updated is
735 found in updated_window. This function It is called from
736 update_window_line only if it is known that there are differences
737 between bitmaps to be drawn between current row and DESIRED_ROW. */
739 static void
740 x_after_update_window_line (desired_row)
741 struct glyph_row *desired_row;
743 struct window *w = updated_window;
744 struct frame *f;
745 int width, height;
747 xassert (w);
749 if (!desired_row->mode_line_p && !w->pseudo_window_p)
751 BLOCK_INPUT;
752 draw_row_fringe_bitmaps (w, desired_row);
753 UNBLOCK_INPUT;
756 /* When a window has disappeared, make sure that no rest of
757 full-width rows stays visible in the internal border. Could
758 check here if updated_window is the leftmost/rightmost window,
759 but I guess it's not worth doing since vertically split windows
760 are almost never used, internal border is rarely set, and the
761 overhead is very small. */
762 if (windows_or_buffers_changed
763 && desired_row->full_width_p
764 && (f = XFRAME (w->frame),
765 width = FRAME_INTERNAL_BORDER_WIDTH (f),
766 width != 0)
767 && (height = desired_row->visible_height,
768 height > 0))
770 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
772 /* Internal border is drawn below the tool bar. */
773 if (WINDOWP (f->tool_bar_window)
774 && w == XWINDOW (f->tool_bar_window))
775 y -= width;
777 BLOCK_INPUT;
778 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
779 0, y, width, height, False);
780 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
781 f->output_data.x->pixel_width - width,
782 y, width, height, False);
783 UNBLOCK_INPUT;
787 static void
788 x_draw_fringe_bitmap (w, row, p)
789 struct window *w;
790 struct glyph_row *row;
791 struct draw_fringe_bitmap_params *p;
793 struct frame *f = XFRAME (WINDOW_FRAME (w));
794 Display *display = FRAME_X_DISPLAY (f);
795 Window window = FRAME_X_WINDOW (f);
796 GC gc = f->output_data.x->normal_gc;
797 struct face *face = p->face;
799 /* Must clip because of partially visible lines. */
800 x_clip_to_row (w, row, gc, 1);
802 if (p->bx >= 0)
804 /* In case the same realized face is used for fringes and
805 for something displayed in the text (e.g. face `region' on
806 mono-displays, the fill style may have been changed to
807 FillSolid in x_draw_glyph_string_background. */
808 if (face->stipple)
809 XSetFillStyle (display, face->gc, FillOpaqueStippled);
810 else
811 XSetForeground (display, face->gc, face->background);
813 XFillRectangle (display, window, face->gc,
814 p->bx, p->by, p->nx, p->ny);
816 if (!face->stipple)
817 XSetForeground (display, face->gc, face->foreground);
820 if (p->which != NO_FRINGE_BITMAP)
822 unsigned char *bits = fringe_bitmaps[p->which].bits + p->dh;
823 Pixmap pixmap;
824 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
826 /* Draw the bitmap. I believe these small pixmaps can be cached
827 by the server. */
828 pixmap = XCreatePixmapFromBitmapData (display, window, bits, p->wd, p->h,
829 face->foreground,
830 face->background, depth);
831 XCopyArea (display, pixmap, window, gc, 0, 0,
832 p->wd, p->h, p->x, p->y);
833 XFreePixmap (display, pixmap);
836 XSetClipMask (display, gc, None);
841 /* This is called when starting Emacs and when restarting after
842 suspend. When starting Emacs, no X window is mapped. And nothing
843 must be done to Emacs's own window if it is suspended (though that
844 rarely happens). */
846 static void
847 XTset_terminal_modes ()
851 /* This is called when exiting or suspending Emacs. Exiting will make
852 the X-windows go away, and suspending requires no action. */
854 static void
855 XTreset_terminal_modes ()
861 /***********************************************************************
862 Output Cursor
863 ***********************************************************************/
865 /* Set the global variable output_cursor to CURSOR. All cursor
866 positions are relative to updated_window. */
868 static void
869 set_output_cursor (cursor)
870 struct cursor_pos *cursor;
872 output_cursor.hpos = cursor->hpos;
873 output_cursor.vpos = cursor->vpos;
874 output_cursor.x = cursor->x;
875 output_cursor.y = cursor->y;
879 /* Set a nominal cursor position.
881 HPOS and VPOS are column/row positions in a window glyph matrix. X
882 and Y are window text area relative pixel positions.
884 If this is done during an update, updated_window will contain the
885 window that is being updated and the position is the future output
886 cursor position for that window. If updated_window is null, use
887 selected_window and display the cursor at the given position. */
889 static void
890 XTcursor_to (vpos, hpos, y, x)
891 int vpos, hpos, y, x;
893 struct window *w;
895 /* If updated_window is not set, work on selected_window. */
896 if (updated_window)
897 w = updated_window;
898 else
899 w = XWINDOW (selected_window);
901 /* Set the output cursor. */
902 output_cursor.hpos = hpos;
903 output_cursor.vpos = vpos;
904 output_cursor.x = x;
905 output_cursor.y = y;
907 /* If not called as part of an update, really display the cursor.
908 This will also set the cursor position of W. */
909 if (updated_window == NULL)
911 BLOCK_INPUT;
912 x_display_cursor (w, 1, hpos, vpos, x, y);
913 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
914 UNBLOCK_INPUT;
920 /***********************************************************************
921 Display Iterator
922 ***********************************************************************/
924 /* Function prototypes of this page. */
926 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
927 struct glyph *,
928 XChar2b *,
929 int *));
930 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
931 int, XChar2b *, int,
932 int));
933 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
934 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
935 static void x_append_glyph P_ ((struct it *));
936 static void x_append_composite_glyph P_ ((struct it *));
937 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
938 int, int, double));
939 static void x_produce_glyphs P_ ((struct it *));
940 static void x_produce_image_glyph P_ ((struct it *it));
943 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
944 is not contained in the font. */
946 static INLINE XCharStruct *
947 x_per_char_metric (font, char2b)
948 XFontStruct *font;
949 XChar2b *char2b;
951 /* The result metric information. */
952 XCharStruct *pcm = NULL;
954 xassert (font && char2b);
956 if (font->per_char != NULL)
958 if (font->min_byte1 == 0 && font->max_byte1 == 0)
960 /* min_char_or_byte2 specifies the linear character index
961 corresponding to the first element of the per_char array,
962 max_char_or_byte2 is the index of the last character. A
963 character with non-zero CHAR2B->byte1 is not in the font.
964 A character with byte2 less than min_char_or_byte2 or
965 greater max_char_or_byte2 is not in the font. */
966 if (char2b->byte1 == 0
967 && char2b->byte2 >= font->min_char_or_byte2
968 && char2b->byte2 <= font->max_char_or_byte2)
969 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
971 else
973 /* If either min_byte1 or max_byte1 are nonzero, both
974 min_char_or_byte2 and max_char_or_byte2 are less than
975 256, and the 2-byte character index values corresponding
976 to the per_char array element N (counting from 0) are:
978 byte1 = N/D + min_byte1
979 byte2 = N\D + min_char_or_byte2
981 where:
983 D = max_char_or_byte2 - min_char_or_byte2 + 1
984 / = integer division
985 \ = integer modulus */
986 if (char2b->byte1 >= font->min_byte1
987 && char2b->byte1 <= font->max_byte1
988 && char2b->byte2 >= font->min_char_or_byte2
989 && char2b->byte2 <= font->max_char_or_byte2)
991 pcm = (font->per_char
992 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
993 * (char2b->byte1 - font->min_byte1))
994 + (char2b->byte2 - font->min_char_or_byte2));
998 else
1000 /* If the per_char pointer is null, all glyphs between the first
1001 and last character indexes inclusive have the same
1002 information, as given by both min_bounds and max_bounds. */
1003 if (char2b->byte2 >= font->min_char_or_byte2
1004 && char2b->byte2 <= font->max_char_or_byte2)
1005 pcm = &font->max_bounds;
1008 return ((pcm == NULL
1009 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1010 ? NULL : pcm);
1014 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1015 the two-byte form of C. Encoding is returned in *CHAR2B. */
1017 static INLINE void
1018 x_encode_char (c, char2b, font_info)
1019 int c;
1020 XChar2b *char2b;
1021 struct font_info *font_info;
1023 int charset = CHAR_CHARSET (c);
1024 XFontStruct *font = font_info->font;
1026 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1027 This may be either a program in a special encoder language or a
1028 fixed encoding. */
1029 if (font_info->font_encoder)
1031 /* It's a program. */
1032 struct ccl_program *ccl = font_info->font_encoder;
1034 if (CHARSET_DIMENSION (charset) == 1)
1036 ccl->reg[0] = charset;
1037 ccl->reg[1] = char2b->byte2;
1038 ccl->reg[2] = -1;
1040 else
1042 ccl->reg[0] = charset;
1043 ccl->reg[1] = char2b->byte1;
1044 ccl->reg[2] = char2b->byte2;
1047 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1049 /* We assume that MSBs are appropriately set/reset by CCL
1050 program. */
1051 if (font->max_byte1 == 0) /* 1-byte font */
1052 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1053 else
1054 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1056 else if (font_info->encoding[charset])
1058 /* Fixed encoding scheme. See fontset.h for the meaning of the
1059 encoding numbers. */
1060 int enc = font_info->encoding[charset];
1062 if ((enc == 1 || enc == 2)
1063 && CHARSET_DIMENSION (charset) == 2)
1064 char2b->byte1 |= 0x80;
1066 if (enc == 1 || enc == 3)
1067 char2b->byte2 |= 0x80;
1072 /* Get face and two-byte form of character C in face FACE_ID on frame
1073 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1074 means we want to display multibyte text. DISPLAY_P non-zero means
1075 make sure that X resources for the face returned are allocated.
1076 Value is a pointer to a realized face that is ready for display if
1077 DISPLAY_P is non-zero. */
1079 static INLINE struct face *
1080 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
1081 struct frame *f;
1082 int c, face_id;
1083 XChar2b *char2b;
1084 int multibyte_p, display_p;
1086 struct face *face = FACE_FROM_ID (f, face_id);
1088 if (!multibyte_p)
1090 /* Unibyte case. We don't have to encode, but we have to make
1091 sure to use a face suitable for unibyte. */
1092 char2b->byte1 = 0;
1093 char2b->byte2 = c;
1094 face_id = FACE_FOR_CHAR (f, face, c);
1095 face = FACE_FROM_ID (f, face_id);
1097 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1099 /* Case of ASCII in a face known to fit ASCII. */
1100 char2b->byte1 = 0;
1101 char2b->byte2 = c;
1103 else
1105 int c1, c2, charset;
1107 /* Split characters into bytes. If c2 is -1 afterwards, C is
1108 really a one-byte character so that byte1 is zero. */
1109 SPLIT_CHAR (c, charset, c1, c2);
1110 if (c2 > 0)
1111 char2b->byte1 = c1, char2b->byte2 = c2;
1112 else
1113 char2b->byte1 = 0, char2b->byte2 = c1;
1115 /* Maybe encode the character in *CHAR2B. */
1116 if (face->font != NULL)
1118 struct font_info *font_info
1119 = FONT_INFO_FROM_ID (f, face->font_info_id);
1120 if (font_info)
1121 x_encode_char (c, char2b, font_info);
1125 /* Make sure X resources of the face are allocated. */
1126 if (display_p)
1128 xassert (face != NULL);
1129 PREPARE_FACE_FOR_DISPLAY (f, face);
1132 return face;
1136 /* Get face and two-byte form of character glyph GLYPH on frame F.
1137 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1138 a pointer to a realized face that is ready for display. */
1140 static INLINE struct face *
1141 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1142 struct frame *f;
1143 struct glyph *glyph;
1144 XChar2b *char2b;
1145 int *two_byte_p;
1147 struct face *face;
1149 xassert (glyph->type == CHAR_GLYPH);
1150 face = FACE_FROM_ID (f, glyph->face_id);
1152 if (two_byte_p)
1153 *two_byte_p = 0;
1155 if (!glyph->multibyte_p)
1157 /* Unibyte case. We don't have to encode, but we have to make
1158 sure to use a face suitable for unibyte. */
1159 char2b->byte1 = 0;
1160 char2b->byte2 = glyph->u.ch;
1162 else if (glyph->u.ch < 128
1163 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1165 /* Case of ASCII in a face known to fit ASCII. */
1166 char2b->byte1 = 0;
1167 char2b->byte2 = glyph->u.ch;
1169 else
1171 int c1, c2, charset;
1173 /* Split characters into bytes. If c2 is -1 afterwards, C is
1174 really a one-byte character so that byte1 is zero. */
1175 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1176 if (c2 > 0)
1177 char2b->byte1 = c1, char2b->byte2 = c2;
1178 else
1179 char2b->byte1 = 0, char2b->byte2 = c1;
1181 /* Maybe encode the character in *CHAR2B. */
1182 if (charset != CHARSET_ASCII)
1184 struct font_info *font_info
1185 = FONT_INFO_FROM_ID (f, face->font_info_id);
1186 if (font_info)
1188 x_encode_char (glyph->u.ch, char2b, font_info);
1189 if (two_byte_p)
1190 *two_byte_p
1191 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1196 /* Make sure X resources of the face are allocated. */
1197 xassert (face != NULL);
1198 PREPARE_FACE_FOR_DISPLAY (f, face);
1199 return face;
1203 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1204 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1206 static INLINE void
1207 x_append_glyph (it)
1208 struct it *it;
1210 struct glyph *glyph;
1211 enum glyph_row_area area = it->area;
1213 xassert (it->glyph_row);
1214 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1216 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1217 if (glyph < it->glyph_row->glyphs[area + 1])
1219 glyph->charpos = CHARPOS (it->position);
1220 glyph->object = it->object;
1221 glyph->pixel_width = it->pixel_width;
1222 glyph->voffset = it->voffset;
1223 glyph->type = CHAR_GLYPH;
1224 glyph->multibyte_p = it->multibyte_p;
1225 glyph->left_box_line_p = it->start_of_box_run_p;
1226 glyph->right_box_line_p = it->end_of_box_run_p;
1227 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1228 || it->phys_descent > it->descent);
1229 glyph->padding_p = 0;
1230 glyph->glyph_not_available_p = it->glyph_not_available_p;
1231 glyph->face_id = it->face_id;
1232 glyph->u.ch = it->char_to_display;
1233 ++it->glyph_row->used[area];
1237 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1238 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1240 static INLINE void
1241 x_append_composite_glyph (it)
1242 struct it *it;
1244 struct glyph *glyph;
1245 enum glyph_row_area area = it->area;
1247 xassert (it->glyph_row);
1249 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1250 if (glyph < it->glyph_row->glyphs[area + 1])
1252 glyph->charpos = CHARPOS (it->position);
1253 glyph->object = it->object;
1254 glyph->pixel_width = it->pixel_width;
1255 glyph->voffset = it->voffset;
1256 glyph->type = COMPOSITE_GLYPH;
1257 glyph->multibyte_p = it->multibyte_p;
1258 glyph->left_box_line_p = it->start_of_box_run_p;
1259 glyph->right_box_line_p = it->end_of_box_run_p;
1260 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1261 || it->phys_descent > it->descent);
1262 glyph->padding_p = 0;
1263 glyph->glyph_not_available_p = 0;
1264 glyph->face_id = it->face_id;
1265 glyph->u.cmp_id = it->cmp_id;
1266 ++it->glyph_row->used[area];
1271 /* Change IT->ascent and IT->height according to the setting of
1272 IT->voffset. */
1274 static INLINE void
1275 take_vertical_position_into_account (it)
1276 struct it *it;
1278 if (it->voffset)
1280 if (it->voffset < 0)
1281 /* Increase the ascent so that we can display the text higher
1282 in the line. */
1283 it->ascent += abs (it->voffset);
1284 else
1285 /* Increase the descent so that we can display the text lower
1286 in the line. */
1287 it->descent += it->voffset;
1292 /* Produce glyphs/get display metrics for the image IT is loaded with.
1293 See the description of struct display_iterator in dispextern.h for
1294 an overview of struct display_iterator. */
1296 static void
1297 x_produce_image_glyph (it)
1298 struct it *it;
1300 struct image *img;
1301 struct face *face;
1303 xassert (it->what == IT_IMAGE);
1305 face = FACE_FROM_ID (it->f, it->face_id);
1306 img = IMAGE_FROM_ID (it->f, it->image_id);
1307 xassert (img);
1309 /* Make sure X resources of the face and image are loaded. */
1310 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1311 prepare_image_for_display (it->f, img);
1313 it->ascent = it->phys_ascent = image_ascent (img, face);
1314 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1315 it->pixel_width = img->width + 2 * img->hmargin;
1317 it->nglyphs = 1;
1319 if (face->box != FACE_NO_BOX)
1321 if (face->box_line_width > 0)
1323 it->ascent += face->box_line_width;
1324 it->descent += face->box_line_width;
1327 if (it->start_of_box_run_p)
1328 it->pixel_width += abs (face->box_line_width);
1329 if (it->end_of_box_run_p)
1330 it->pixel_width += abs (face->box_line_width);
1333 take_vertical_position_into_account (it);
1335 if (it->glyph_row)
1337 struct glyph *glyph;
1338 enum glyph_row_area area = it->area;
1340 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1341 if (glyph < it->glyph_row->glyphs[area + 1])
1343 glyph->charpos = CHARPOS (it->position);
1344 glyph->object = it->object;
1345 glyph->pixel_width = it->pixel_width;
1346 glyph->voffset = it->voffset;
1347 glyph->type = IMAGE_GLYPH;
1348 glyph->multibyte_p = it->multibyte_p;
1349 glyph->left_box_line_p = it->start_of_box_run_p;
1350 glyph->right_box_line_p = it->end_of_box_run_p;
1351 glyph->overlaps_vertically_p = 0;
1352 glyph->padding_p = 0;
1353 glyph->glyph_not_available_p = 0;
1354 glyph->face_id = it->face_id;
1355 glyph->u.img_id = img->id;
1356 ++it->glyph_row->used[area];
1362 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1363 of the glyph, WIDTH and HEIGHT are the width and height of the
1364 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1365 ascent of the glyph (0 <= ASCENT <= 1). */
1367 static void
1368 x_append_stretch_glyph (it, object, width, height, ascent)
1369 struct it *it;
1370 Lisp_Object object;
1371 int width, height;
1372 double ascent;
1374 struct glyph *glyph;
1375 enum glyph_row_area area = it->area;
1377 xassert (ascent >= 0 && ascent <= 1);
1379 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1380 if (glyph < it->glyph_row->glyphs[area + 1])
1382 glyph->charpos = CHARPOS (it->position);
1383 glyph->object = object;
1384 glyph->pixel_width = width;
1385 glyph->voffset = it->voffset;
1386 glyph->type = STRETCH_GLYPH;
1387 glyph->multibyte_p = it->multibyte_p;
1388 glyph->left_box_line_p = it->start_of_box_run_p;
1389 glyph->right_box_line_p = it->end_of_box_run_p;
1390 glyph->overlaps_vertically_p = 0;
1391 glyph->padding_p = 0;
1392 glyph->glyph_not_available_p = 0;
1393 glyph->face_id = it->face_id;
1394 glyph->u.stretch.ascent = height * ascent;
1395 glyph->u.stretch.height = height;
1396 ++it->glyph_row->used[area];
1401 /* Produce a stretch glyph for iterator IT. IT->object is the value
1402 of the glyph property displayed. The value must be a list
1403 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1404 being recognized:
1406 1. `:width WIDTH' specifies that the space should be WIDTH *
1407 canonical char width wide. WIDTH may be an integer or floating
1408 point number.
1410 2. `:relative-width FACTOR' specifies that the width of the stretch
1411 should be computed from the width of the first character having the
1412 `glyph' property, and should be FACTOR times that width.
1414 3. `:align-to HPOS' specifies that the space should be wide enough
1415 to reach HPOS, a value in canonical character units.
1417 Exactly one of the above pairs must be present.
1419 4. `:height HEIGHT' specifies that the height of the stretch produced
1420 should be HEIGHT, measured in canonical character units.
1422 5. `:relative-height FACTOR' specifies that the height of the
1423 stretch should be FACTOR times the height of the characters having
1424 the glyph property.
1426 Either none or exactly one of 4 or 5 must be present.
1428 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1429 of the stretch should be used for the ascent of the stretch.
1430 ASCENT must be in the range 0 <= ASCENT <= 100. */
1432 #define NUMVAL(X) \
1433 ((INTEGERP (X) || FLOATP (X)) \
1434 ? XFLOATINT (X) \
1435 : - 1)
1438 static void
1439 x_produce_stretch_glyph (it)
1440 struct it *it;
1442 /* (space :width WIDTH :height HEIGHT. */
1443 #if GLYPH_DEBUG
1444 extern Lisp_Object Qspace;
1445 #endif
1446 extern Lisp_Object QCwidth, QCheight, QCascent;
1447 extern Lisp_Object QCrelative_width, QCrelative_height;
1448 extern Lisp_Object QCalign_to;
1449 Lisp_Object prop, plist;
1450 double width = 0, height = 0, ascent = 0;
1451 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1452 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1454 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1456 /* List should start with `space'. */
1457 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1458 plist = XCDR (it->object);
1460 /* Compute the width of the stretch. */
1461 if (prop = Fplist_get (plist, QCwidth),
1462 NUMVAL (prop) > 0)
1463 /* Absolute width `:width WIDTH' specified and valid. */
1464 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1465 else if (prop = Fplist_get (plist, QCrelative_width),
1466 NUMVAL (prop) > 0)
1468 /* Relative width `:relative-width FACTOR' specified and valid.
1469 Compute the width of the characters having the `glyph'
1470 property. */
1471 struct it it2;
1472 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1474 it2 = *it;
1475 if (it->multibyte_p)
1477 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1478 - IT_BYTEPOS (*it));
1479 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1481 else
1482 it2.c = *p, it2.len = 1;
1484 it2.glyph_row = NULL;
1485 it2.what = IT_CHARACTER;
1486 x_produce_glyphs (&it2);
1487 width = NUMVAL (prop) * it2.pixel_width;
1489 else if (prop = Fplist_get (plist, QCalign_to),
1490 NUMVAL (prop) > 0)
1491 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1492 else
1493 /* Nothing specified -> width defaults to canonical char width. */
1494 width = CANON_X_UNIT (it->f);
1496 /* Compute height. */
1497 if (prop = Fplist_get (plist, QCheight),
1498 NUMVAL (prop) > 0)
1499 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1500 else if (prop = Fplist_get (plist, QCrelative_height),
1501 NUMVAL (prop) > 0)
1502 height = FONT_HEIGHT (font) * NUMVAL (prop);
1503 else
1504 height = FONT_HEIGHT (font);
1506 /* Compute percentage of height used for ascent. If
1507 `:ascent ASCENT' is present and valid, use that. Otherwise,
1508 derive the ascent from the font in use. */
1509 if (prop = Fplist_get (plist, QCascent),
1510 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1511 ascent = NUMVAL (prop) / 100.0;
1512 else
1513 ascent = (double) font->ascent / FONT_HEIGHT (font);
1515 if (width <= 0)
1516 width = 1;
1517 if (height <= 0)
1518 height = 1;
1520 if (it->glyph_row)
1522 Lisp_Object object = it->stack[it->sp - 1].string;
1523 if (!STRINGP (object))
1524 object = it->w->buffer;
1525 x_append_stretch_glyph (it, object, width, height, ascent);
1528 it->pixel_width = width;
1529 it->ascent = it->phys_ascent = height * ascent;
1530 it->descent = it->phys_descent = height - it->ascent;
1531 it->nglyphs = 1;
1533 if (face->box != FACE_NO_BOX)
1535 if (face->box_line_width > 0)
1537 it->ascent += face->box_line_width;
1538 it->descent += face->box_line_width;
1541 if (it->start_of_box_run_p)
1542 it->pixel_width += abs (face->box_line_width);
1543 if (it->end_of_box_run_p)
1544 it->pixel_width += abs (face->box_line_width);
1547 take_vertical_position_into_account (it);
1550 /* Return proper value to be used as baseline offset of font that has
1551 ASCENT and DESCENT to draw characters by the font at the vertical
1552 center of the line of frame F.
1554 Here, out task is to find the value of BOFF in the following figure;
1556 -------------------------+-----------+-
1557 -+-+---------+-+ | |
1558 | | | | | |
1559 | | | | F_ASCENT F_HEIGHT
1560 | | | ASCENT | |
1561 HEIGHT | | | | |
1562 | | |-|-+------+-----------|------- baseline
1563 | | | | BOFF | |
1564 | |---------|-+-+ | |
1565 | | | DESCENT | |
1566 -+-+---------+-+ F_DESCENT |
1567 -------------------------+-----------+-
1569 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1570 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1571 DESCENT = FONT->descent
1572 HEIGHT = FONT_HEIGHT (FONT)
1573 F_DESCENT = (F->output_data.x->font->descent
1574 - F->output_data.x->baseline_offset)
1575 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1578 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1579 ((FONT)->descent \
1580 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1581 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1582 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1584 /* Produce glyphs/get display metrics for the display element IT is
1585 loaded with. See the description of struct display_iterator in
1586 dispextern.h for an overview of struct display_iterator. */
1588 static void
1589 x_produce_glyphs (it)
1590 struct it *it;
1592 it->glyph_not_available_p = 0;
1594 if (it->what == IT_CHARACTER)
1596 XChar2b char2b;
1597 XFontStruct *font;
1598 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1599 XCharStruct *pcm;
1600 int font_not_found_p;
1601 struct font_info *font_info;
1602 int boff; /* baseline offset */
1603 /* We may change it->multibyte_p upon unibyte<->multibyte
1604 conversion. So, save the current value now and restore it
1605 later.
1607 Note: It seems that we don't have to record multibyte_p in
1608 struct glyph because the character code itself tells if or
1609 not the character is multibyte. Thus, in the future, we must
1610 consider eliminating the field `multibyte_p' in the struct
1611 glyph. */
1612 int saved_multibyte_p = it->multibyte_p;
1614 /* Maybe translate single-byte characters to multibyte, or the
1615 other way. */
1616 it->char_to_display = it->c;
1617 if (!ASCII_BYTE_P (it->c))
1619 if (unibyte_display_via_language_environment
1620 && SINGLE_BYTE_CHAR_P (it->c)
1621 && (it->c >= 0240
1622 || !NILP (Vnonascii_translation_table)))
1624 it->char_to_display = unibyte_char_to_multibyte (it->c);
1625 it->multibyte_p = 1;
1626 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1627 face = FACE_FROM_ID (it->f, it->face_id);
1629 else if (!SINGLE_BYTE_CHAR_P (it->c)
1630 && !it->multibyte_p)
1632 it->multibyte_p = 1;
1633 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1634 face = FACE_FROM_ID (it->f, it->face_id);
1638 /* Get font to use. Encode IT->char_to_display. */
1639 x_get_char_face_and_encoding (it->f, it->char_to_display,
1640 it->face_id, &char2b,
1641 it->multibyte_p, 0);
1642 font = face->font;
1644 /* When no suitable font found, use the default font. */
1645 font_not_found_p = font == NULL;
1646 if (font_not_found_p)
1648 font = FRAME_FONT (it->f);
1649 boff = it->f->output_data.x->baseline_offset;
1650 font_info = NULL;
1652 else
1654 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1655 boff = font_info->baseline_offset;
1656 if (font_info->vertical_centering)
1657 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1660 if (it->char_to_display >= ' '
1661 && (!it->multibyte_p || it->char_to_display < 128))
1663 /* Either unibyte or ASCII. */
1664 int stretched_p;
1666 it->nglyphs = 1;
1668 pcm = x_per_char_metric (font, &char2b);
1669 it->ascent = font->ascent + boff;
1670 it->descent = font->descent - boff;
1672 if (pcm)
1674 it->phys_ascent = pcm->ascent + boff;
1675 it->phys_descent = pcm->descent - boff;
1676 it->pixel_width = pcm->width;
1678 else
1680 it->glyph_not_available_p = 1;
1681 it->phys_ascent = font->ascent + boff;
1682 it->phys_descent = font->descent - boff;
1683 it->pixel_width = FONT_WIDTH (font);
1686 /* If this is a space inside a region of text with
1687 `space-width' property, change its width. */
1688 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1689 if (stretched_p)
1690 it->pixel_width *= XFLOATINT (it->space_width);
1692 /* If face has a box, add the box thickness to the character
1693 height. If character has a box line to the left and/or
1694 right, add the box line width to the character's width. */
1695 if (face->box != FACE_NO_BOX)
1697 int thick = face->box_line_width;
1699 if (thick > 0)
1701 it->ascent += thick;
1702 it->descent += thick;
1704 else
1705 thick = -thick;
1707 if (it->start_of_box_run_p)
1708 it->pixel_width += thick;
1709 if (it->end_of_box_run_p)
1710 it->pixel_width += thick;
1713 /* If face has an overline, add the height of the overline
1714 (1 pixel) and a 1 pixel margin to the character height. */
1715 if (face->overline_p)
1716 it->ascent += 2;
1718 take_vertical_position_into_account (it);
1720 /* If we have to actually produce glyphs, do it. */
1721 if (it->glyph_row)
1723 if (stretched_p)
1725 /* Translate a space with a `space-width' property
1726 into a stretch glyph. */
1727 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1728 x_append_stretch_glyph (it, it->object, it->pixel_width,
1729 it->ascent + it->descent, ascent);
1731 else
1732 x_append_glyph (it);
1734 /* If characters with lbearing or rbearing are displayed
1735 in this line, record that fact in a flag of the
1736 glyph row. This is used to optimize X output code. */
1737 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1738 it->glyph_row->contains_overlapping_glyphs_p = 1;
1741 else if (it->char_to_display == '\n')
1743 /* A newline has no width but we need the height of the line. */
1744 it->pixel_width = 0;
1745 it->nglyphs = 0;
1746 it->ascent = it->phys_ascent = font->ascent + boff;
1747 it->descent = it->phys_descent = font->descent - boff;
1749 if (face->box != FACE_NO_BOX
1750 && face->box_line_width > 0)
1752 it->ascent += face->box_line_width;
1753 it->descent += face->box_line_width;
1756 else if (it->char_to_display == '\t')
1758 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1759 int x = it->current_x + it->continuation_lines_width;
1760 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1762 /* If the distance from the current position to the next tab
1763 stop is less than a canonical character width, use the
1764 tab stop after that. */
1765 if (next_tab_x - x < CANON_X_UNIT (it->f))
1766 next_tab_x += tab_width;
1768 it->pixel_width = next_tab_x - x;
1769 it->nglyphs = 1;
1770 it->ascent = it->phys_ascent = font->ascent + boff;
1771 it->descent = it->phys_descent = font->descent - boff;
1773 if (it->glyph_row)
1775 double ascent = (double) it->ascent / (it->ascent + it->descent);
1776 x_append_stretch_glyph (it, it->object, it->pixel_width,
1777 it->ascent + it->descent, ascent);
1780 else
1782 /* A multi-byte character. Assume that the display width of the
1783 character is the width of the character multiplied by the
1784 width of the font. */
1786 /* If we found a font, this font should give us the right
1787 metrics. If we didn't find a font, use the frame's
1788 default font and calculate the width of the character
1789 from the charset width; this is what old redisplay code
1790 did. */
1791 pcm = x_per_char_metric (font, &char2b);
1792 if (font_not_found_p || !pcm)
1794 int charset = CHAR_CHARSET (it->char_to_display);
1796 it->glyph_not_available_p = 1;
1797 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1798 * CHARSET_WIDTH (charset));
1799 it->phys_ascent = font->ascent + boff;
1800 it->phys_descent = font->descent - boff;
1802 else
1804 it->pixel_width = pcm->width;
1805 it->phys_ascent = pcm->ascent + boff;
1806 it->phys_descent = pcm->descent - boff;
1807 if (it->glyph_row
1808 && (pcm->lbearing < 0
1809 || pcm->rbearing > pcm->width))
1810 it->glyph_row->contains_overlapping_glyphs_p = 1;
1812 it->nglyphs = 1;
1813 it->ascent = font->ascent + boff;
1814 it->descent = font->descent - boff;
1815 if (face->box != FACE_NO_BOX)
1817 int thick = face->box_line_width;
1819 if (thick > 0)
1821 it->ascent += thick;
1822 it->descent += thick;
1824 else
1825 thick = - thick;
1827 if (it->start_of_box_run_p)
1828 it->pixel_width += thick;
1829 if (it->end_of_box_run_p)
1830 it->pixel_width += thick;
1833 /* If face has an overline, add the height of the overline
1834 (1 pixel) and a 1 pixel margin to the character height. */
1835 if (face->overline_p)
1836 it->ascent += 2;
1838 take_vertical_position_into_account (it);
1840 if (it->glyph_row)
1841 x_append_glyph (it);
1843 it->multibyte_p = saved_multibyte_p;
1845 else if (it->what == IT_COMPOSITION)
1847 /* Note: A composition is represented as one glyph in the
1848 glyph matrix. There are no padding glyphs. */
1849 XChar2b char2b;
1850 XFontStruct *font;
1851 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1852 XCharStruct *pcm;
1853 int font_not_found_p;
1854 struct font_info *font_info;
1855 int boff; /* baseline offset */
1856 struct composition *cmp = composition_table[it->cmp_id];
1858 /* Maybe translate single-byte characters to multibyte. */
1859 it->char_to_display = it->c;
1860 if (unibyte_display_via_language_environment
1861 && SINGLE_BYTE_CHAR_P (it->c)
1862 && (it->c >= 0240
1863 || (it->c >= 0200
1864 && !NILP (Vnonascii_translation_table))))
1866 it->char_to_display = unibyte_char_to_multibyte (it->c);
1869 /* Get face and font to use. Encode IT->char_to_display. */
1870 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1871 face = FACE_FROM_ID (it->f, it->face_id);
1872 x_get_char_face_and_encoding (it->f, it->char_to_display,
1873 it->face_id, &char2b, it->multibyte_p, 0);
1874 font = face->font;
1876 /* When no suitable font found, use the default font. */
1877 font_not_found_p = font == NULL;
1878 if (font_not_found_p)
1880 font = FRAME_FONT (it->f);
1881 boff = it->f->output_data.x->baseline_offset;
1882 font_info = NULL;
1884 else
1886 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1887 boff = font_info->baseline_offset;
1888 if (font_info->vertical_centering)
1889 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1892 /* There are no padding glyphs, so there is only one glyph to
1893 produce for the composition. Important is that pixel_width,
1894 ascent and descent are the values of what is drawn by
1895 draw_glyphs (i.e. the values of the overall glyphs composed). */
1896 it->nglyphs = 1;
1898 /* If we have not yet calculated pixel size data of glyphs of
1899 the composition for the current face font, calculate them
1900 now. Theoretically, we have to check all fonts for the
1901 glyphs, but that requires much time and memory space. So,
1902 here we check only the font of the first glyph. This leads
1903 to incorrect display very rarely, and C-l (recenter) can
1904 correct the display anyway. */
1905 if (cmp->font != (void *) font)
1907 /* Ascent and descent of the font of the first character of
1908 this composition (adjusted by baseline offset). Ascent
1909 and descent of overall glyphs should not be less than
1910 them respectively. */
1911 int font_ascent = font->ascent + boff;
1912 int font_descent = font->descent - boff;
1913 /* Bounding box of the overall glyphs. */
1914 int leftmost, rightmost, lowest, highest;
1915 int i, width, ascent, descent;
1917 cmp->font = (void *) font;
1919 /* Initialize the bounding box. */
1920 if (font_info
1921 && (pcm = x_per_char_metric (font, &char2b)))
1923 width = pcm->width;
1924 ascent = pcm->ascent;
1925 descent = pcm->descent;
1927 else
1929 width = FONT_WIDTH (font);
1930 ascent = font->ascent;
1931 descent = font->descent;
1934 rightmost = width;
1935 lowest = - descent + boff;
1936 highest = ascent + boff;
1937 leftmost = 0;
1939 if (font_info
1940 && font_info->default_ascent
1941 && CHAR_TABLE_P (Vuse_default_ascent)
1942 && !NILP (Faref (Vuse_default_ascent,
1943 make_number (it->char_to_display))))
1944 highest = font_info->default_ascent + boff;
1946 /* Draw the first glyph at the normal position. It may be
1947 shifted to right later if some other glyphs are drawn at
1948 the left. */
1949 cmp->offsets[0] = 0;
1950 cmp->offsets[1] = boff;
1952 /* Set cmp->offsets for the remaining glyphs. */
1953 for (i = 1; i < cmp->glyph_len; i++)
1955 int left, right, btm, top;
1956 int ch = COMPOSITION_GLYPH (cmp, i);
1957 int face_id = FACE_FOR_CHAR (it->f, face, ch);
1959 face = FACE_FROM_ID (it->f, face_id);
1960 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
1961 it->multibyte_p, 0);
1962 font = face->font;
1963 if (font == NULL)
1965 font = FRAME_FONT (it->f);
1966 boff = it->f->output_data.x->baseline_offset;
1967 font_info = NULL;
1969 else
1971 font_info
1972 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1973 boff = font_info->baseline_offset;
1974 if (font_info->vertical_centering)
1975 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1978 if (font_info
1979 && (pcm = x_per_char_metric (font, &char2b)))
1981 width = pcm->width;
1982 ascent = pcm->ascent;
1983 descent = pcm->descent;
1985 else
1987 width = FONT_WIDTH (font);
1988 ascent = 1;
1989 descent = 0;
1992 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
1994 /* Relative composition with or without
1995 alternate chars. */
1996 left = (leftmost + rightmost - width) / 2;
1997 btm = - descent + boff;
1998 if (font_info && font_info->relative_compose
1999 && (! CHAR_TABLE_P (Vignore_relative_composition)
2000 || NILP (Faref (Vignore_relative_composition,
2001 make_number (ch)))))
2004 if (- descent >= font_info->relative_compose)
2005 /* One extra pixel between two glyphs. */
2006 btm = highest + 1;
2007 else if (ascent <= 0)
2008 /* One extra pixel between two glyphs. */
2009 btm = lowest - 1 - ascent - descent;
2012 else
2014 /* A composition rule is specified by an integer
2015 value that encodes global and new reference
2016 points (GREF and NREF). GREF and NREF are
2017 specified by numbers as below:
2019 0---1---2 -- ascent
2023 9--10--11 -- center
2025 ---3---4---5--- baseline
2027 6---7---8 -- descent
2029 int rule = COMPOSITION_RULE (cmp, i);
2030 int gref, nref, grefx, grefy, nrefx, nrefy;
2032 COMPOSITION_DECODE_RULE (rule, gref, nref);
2033 grefx = gref % 3, nrefx = nref % 3;
2034 grefy = gref / 3, nrefy = nref / 3;
2036 left = (leftmost
2037 + grefx * (rightmost - leftmost) / 2
2038 - nrefx * width / 2);
2039 btm = ((grefy == 0 ? highest
2040 : grefy == 1 ? 0
2041 : grefy == 2 ? lowest
2042 : (highest + lowest) / 2)
2043 - (nrefy == 0 ? ascent + descent
2044 : nrefy == 1 ? descent - boff
2045 : nrefy == 2 ? 0
2046 : (ascent + descent) / 2));
2049 cmp->offsets[i * 2] = left;
2050 cmp->offsets[i * 2 + 1] = btm + descent;
2052 /* Update the bounding box of the overall glyphs. */
2053 right = left + width;
2054 top = btm + descent + ascent;
2055 if (left < leftmost)
2056 leftmost = left;
2057 if (right > rightmost)
2058 rightmost = right;
2059 if (top > highest)
2060 highest = top;
2061 if (btm < lowest)
2062 lowest = btm;
2065 /* If there are glyphs whose x-offsets are negative,
2066 shift all glyphs to the right and make all x-offsets
2067 non-negative. */
2068 if (leftmost < 0)
2070 for (i = 0; i < cmp->glyph_len; i++)
2071 cmp->offsets[i * 2] -= leftmost;
2072 rightmost -= leftmost;
2075 cmp->pixel_width = rightmost;
2076 cmp->ascent = highest;
2077 cmp->descent = - lowest;
2078 if (cmp->ascent < font_ascent)
2079 cmp->ascent = font_ascent;
2080 if (cmp->descent < font_descent)
2081 cmp->descent = font_descent;
2084 it->pixel_width = cmp->pixel_width;
2085 it->ascent = it->phys_ascent = cmp->ascent;
2086 it->descent = it->phys_descent = cmp->descent;
2088 if (face->box != FACE_NO_BOX)
2090 int thick = face->box_line_width;
2092 if (thick > 0)
2094 it->ascent += thick;
2095 it->descent += thick;
2097 else
2098 thick = - thick;
2100 if (it->start_of_box_run_p)
2101 it->pixel_width += thick;
2102 if (it->end_of_box_run_p)
2103 it->pixel_width += thick;
2106 /* If face has an overline, add the height of the overline
2107 (1 pixel) and a 1 pixel margin to the character height. */
2108 if (face->overline_p)
2109 it->ascent += 2;
2111 take_vertical_position_into_account (it);
2113 if (it->glyph_row)
2114 x_append_composite_glyph (it);
2116 else if (it->what == IT_IMAGE)
2117 x_produce_image_glyph (it);
2118 else if (it->what == IT_STRETCH)
2119 x_produce_stretch_glyph (it);
2121 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2122 because this isn't true for images with `:ascent 100'. */
2123 xassert (it->ascent >= 0 && it->descent >= 0);
2124 if (it->area == TEXT_AREA)
2125 it->current_x += it->pixel_width;
2127 it->descent += it->extra_line_spacing;
2129 it->max_ascent = max (it->max_ascent, it->ascent);
2130 it->max_descent = max (it->max_descent, it->descent);
2131 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2132 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2136 /* Estimate the pixel height of the mode or top line on frame F.
2137 FACE_ID specifies what line's height to estimate. */
2140 x_estimate_mode_line_height (f, face_id)
2141 struct frame *f;
2142 enum face_id face_id;
2144 int height = FONT_HEIGHT (FRAME_FONT (f));
2146 /* This function is called so early when Emacs starts that the face
2147 cache and mode line face are not yet initialized. */
2148 if (FRAME_FACE_CACHE (f))
2150 struct face *face = FACE_FROM_ID (f, face_id);
2151 if (face)
2153 if (face->font)
2154 height = FONT_HEIGHT (face->font);
2155 if (face->box_line_width > 0)
2156 height += 2 * face->box_line_width;
2160 return height;
2164 /***********************************************************************
2165 Glyph display
2166 ***********************************************************************/
2168 /* A sequence of glyphs to be drawn in the same face.
2170 This data structure is not really completely X specific, so it
2171 could possibly, at least partially, be useful for other systems. It
2172 is currently not part of the external redisplay interface because
2173 it's not clear what other systems will need. */
2175 struct glyph_string
2177 /* X-origin of the string. */
2178 int x;
2180 /* Y-origin and y-position of the base line of this string. */
2181 int y, ybase;
2183 /* The width of the string, not including a face extension. */
2184 int width;
2186 /* The width of the string, including a face extension. */
2187 int background_width;
2189 /* The height of this string. This is the height of the line this
2190 string is drawn in, and can be different from the height of the
2191 font the string is drawn in. */
2192 int height;
2194 /* Number of pixels this string overwrites in front of its x-origin.
2195 This number is zero if the string has an lbearing >= 0; it is
2196 -lbearing, if the string has an lbearing < 0. */
2197 int left_overhang;
2199 /* Number of pixels this string overwrites past its right-most
2200 nominal x-position, i.e. x + width. Zero if the string's
2201 rbearing is <= its nominal width, rbearing - width otherwise. */
2202 int right_overhang;
2204 /* The frame on which the glyph string is drawn. */
2205 struct frame *f;
2207 /* The window on which the glyph string is drawn. */
2208 struct window *w;
2210 /* X display and window for convenience. */
2211 Display *display;
2212 Window window;
2214 /* The glyph row for which this string was built. It determines the
2215 y-origin and height of the string. */
2216 struct glyph_row *row;
2218 /* The area within row. */
2219 enum glyph_row_area area;
2221 /* Characters to be drawn, and number of characters. */
2222 XChar2b *char2b;
2223 int nchars;
2225 /* A face-override for drawing cursors, mouse face and similar. */
2226 enum draw_glyphs_face hl;
2228 /* Face in which this string is to be drawn. */
2229 struct face *face;
2231 /* Font in which this string is to be drawn. */
2232 XFontStruct *font;
2234 /* Font info for this string. */
2235 struct font_info *font_info;
2237 /* Non-null means this string describes (part of) a composition.
2238 All characters from char2b are drawn composed. */
2239 struct composition *cmp;
2241 /* Index of this glyph string's first character in the glyph
2242 definition of CMP. If this is zero, this glyph string describes
2243 the first character of a composition. */
2244 int gidx;
2246 /* 1 means this glyph strings face has to be drawn to the right end
2247 of the window's drawing area. */
2248 unsigned extends_to_end_of_line_p : 1;
2250 /* 1 means the background of this string has been drawn. */
2251 unsigned background_filled_p : 1;
2253 /* 1 means glyph string must be drawn with 16-bit functions. */
2254 unsigned two_byte_p : 1;
2256 /* 1 means that the original font determined for drawing this glyph
2257 string could not be loaded. The member `font' has been set to
2258 the frame's default font in this case. */
2259 unsigned font_not_found_p : 1;
2261 /* 1 means that the face in which this glyph string is drawn has a
2262 stipple pattern. */
2263 unsigned stippled_p : 1;
2265 /* 1 means only the foreground of this glyph string must be drawn,
2266 and we should use the physical height of the line this glyph
2267 string appears in as clip rect. */
2268 unsigned for_overlaps_p : 1;
2270 /* The GC to use for drawing this glyph string. */
2271 GC gc;
2273 /* A pointer to the first glyph in the string. This glyph
2274 corresponds to char2b[0]. Needed to draw rectangles if
2275 font_not_found_p is 1. */
2276 struct glyph *first_glyph;
2278 /* Image, if any. */
2279 struct image *img;
2281 struct glyph_string *next, *prev;
2285 #if GLYPH_DEBUG
2287 static void
2288 x_dump_glyph_string (s)
2289 struct glyph_string *s;
2291 fprintf (stderr, "glyph string\n");
2292 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2293 s->x, s->y, s->width, s->height);
2294 fprintf (stderr, " ybase = %d\n", s->ybase);
2295 fprintf (stderr, " hl = %d\n", s->hl);
2296 fprintf (stderr, " left overhang = %d, right = %d\n",
2297 s->left_overhang, s->right_overhang);
2298 fprintf (stderr, " nchars = %d\n", s->nchars);
2299 fprintf (stderr, " extends to end of line = %d\n",
2300 s->extends_to_end_of_line_p);
2301 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2302 fprintf (stderr, " bg width = %d\n", s->background_width);
2305 #endif /* GLYPH_DEBUG */
2309 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2310 struct glyph_string **,
2311 struct glyph_string *,
2312 struct glyph_string *));
2313 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2314 struct glyph_string **,
2315 struct glyph_string *,
2316 struct glyph_string *));
2317 static void x_append_glyph_string P_ ((struct glyph_string **,
2318 struct glyph_string **,
2319 struct glyph_string *));
2320 static int x_left_overwritten P_ ((struct glyph_string *));
2321 static int x_left_overwriting P_ ((struct glyph_string *));
2322 static int x_right_overwritten P_ ((struct glyph_string *));
2323 static int x_right_overwriting P_ ((struct glyph_string *));
2324 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2325 int));
2326 static void x_init_glyph_string P_ ((struct glyph_string *,
2327 XChar2b *, struct window *,
2328 struct glyph_row *,
2329 enum glyph_row_area, int,
2330 enum draw_glyphs_face));
2331 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2332 enum glyph_row_area, int, int,
2333 enum draw_glyphs_face, int));
2334 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2335 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2336 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2337 int));
2338 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2339 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2340 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2341 static void x_draw_glyph_string P_ ((struct glyph_string *));
2342 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2343 static void x_set_cursor_gc P_ ((struct glyph_string *));
2344 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2345 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2346 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2347 int *, int *));
2348 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2349 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2350 unsigned long *, double, int));
2351 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2352 double, int, unsigned long));
2353 static void x_setup_relief_colors P_ ((struct glyph_string *));
2354 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2355 static void x_draw_image_relief P_ ((struct glyph_string *));
2356 static void x_draw_image_foreground P_ ((struct glyph_string *));
2357 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2358 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2359 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2360 int, int, int));
2361 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2362 int, int, int, int, XRectangle *));
2363 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2364 int, int, int, XRectangle *));
2365 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2366 enum glyph_row_area));
2367 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2368 struct glyph_row *,
2369 enum glyph_row_area, int, int));
2371 #if GLYPH_DEBUG
2372 static void x_check_font P_ ((struct frame *, XFontStruct *));
2373 #endif
2376 /* Append the list of glyph strings with head H and tail T to the list
2377 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2379 static INLINE void
2380 x_append_glyph_string_lists (head, tail, h, t)
2381 struct glyph_string **head, **tail;
2382 struct glyph_string *h, *t;
2384 if (h)
2386 if (*head)
2387 (*tail)->next = h;
2388 else
2389 *head = h;
2390 h->prev = *tail;
2391 *tail = t;
2396 /* Prepend the list of glyph strings with head H and tail T to the
2397 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2398 result. */
2400 static INLINE void
2401 x_prepend_glyph_string_lists (head, tail, h, t)
2402 struct glyph_string **head, **tail;
2403 struct glyph_string *h, *t;
2405 if (h)
2407 if (*head)
2408 (*head)->prev = t;
2409 else
2410 *tail = t;
2411 t->next = *head;
2412 *head = h;
2417 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2418 Set *HEAD and *TAIL to the resulting list. */
2420 static INLINE void
2421 x_append_glyph_string (head, tail, s)
2422 struct glyph_string **head, **tail;
2423 struct glyph_string *s;
2425 s->next = s->prev = NULL;
2426 x_append_glyph_string_lists (head, tail, s, s);
2430 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2431 face. */
2433 static void
2434 x_set_cursor_gc (s)
2435 struct glyph_string *s;
2437 if (s->font == FRAME_FONT (s->f)
2438 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2439 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2440 && !s->cmp)
2441 s->gc = s->f->output_data.x->cursor_gc;
2442 else
2444 /* Cursor on non-default face: must merge. */
2445 XGCValues xgcv;
2446 unsigned long mask;
2448 xgcv.background = s->f->output_data.x->cursor_pixel;
2449 xgcv.foreground = s->face->background;
2451 /* If the glyph would be invisible, try a different foreground. */
2452 if (xgcv.foreground == xgcv.background)
2453 xgcv.foreground = s->face->foreground;
2454 if (xgcv.foreground == xgcv.background)
2455 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2456 if (xgcv.foreground == xgcv.background)
2457 xgcv.foreground = s->face->foreground;
2459 /* Make sure the cursor is distinct from text in this face. */
2460 if (xgcv.background == s->face->background
2461 && xgcv.foreground == s->face->foreground)
2463 xgcv.background = s->face->foreground;
2464 xgcv.foreground = s->face->background;
2467 IF_DEBUG (x_check_font (s->f, s->font));
2468 xgcv.font = s->font->fid;
2469 xgcv.graphics_exposures = False;
2470 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2472 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2473 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2474 mask, &xgcv);
2475 else
2476 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2477 = XCreateGC (s->display, s->window, mask, &xgcv);
2479 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2484 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2486 static void
2487 x_set_mouse_face_gc (s)
2488 struct glyph_string *s;
2490 int face_id;
2491 struct face *face;
2493 /* What face has to be used last for the mouse face? */
2494 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2495 face = FACE_FROM_ID (s->f, face_id);
2496 if (face == NULL)
2497 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2499 if (s->first_glyph->type == CHAR_GLYPH)
2500 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2501 else
2502 face_id = FACE_FOR_CHAR (s->f, face, 0);
2503 s->face = FACE_FROM_ID (s->f, face_id);
2504 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2506 /* If font in this face is same as S->font, use it. */
2507 if (s->font == s->face->font)
2508 s->gc = s->face->gc;
2509 else
2511 /* Otherwise construct scratch_cursor_gc with values from FACE
2512 but font FONT. */
2513 XGCValues xgcv;
2514 unsigned long mask;
2516 xgcv.background = s->face->background;
2517 xgcv.foreground = s->face->foreground;
2518 IF_DEBUG (x_check_font (s->f, s->font));
2519 xgcv.font = s->font->fid;
2520 xgcv.graphics_exposures = False;
2521 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2523 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2524 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2525 mask, &xgcv);
2526 else
2527 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2528 = XCreateGC (s->display, s->window, mask, &xgcv);
2530 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2533 xassert (s->gc != 0);
2537 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2538 Faces to use in the mode line have already been computed when the
2539 matrix was built, so there isn't much to do, here. */
2541 static INLINE void
2542 x_set_mode_line_face_gc (s)
2543 struct glyph_string *s;
2545 s->gc = s->face->gc;
2549 /* Set S->gc of glyph string S for drawing that glyph string. Set
2550 S->stippled_p to a non-zero value if the face of S has a stipple
2551 pattern. */
2553 static INLINE void
2554 x_set_glyph_string_gc (s)
2555 struct glyph_string *s;
2557 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2559 if (s->hl == DRAW_NORMAL_TEXT)
2561 s->gc = s->face->gc;
2562 s->stippled_p = s->face->stipple != 0;
2564 else if (s->hl == DRAW_INVERSE_VIDEO)
2566 x_set_mode_line_face_gc (s);
2567 s->stippled_p = s->face->stipple != 0;
2569 else if (s->hl == DRAW_CURSOR)
2571 x_set_cursor_gc (s);
2572 s->stippled_p = 0;
2574 else if (s->hl == DRAW_MOUSE_FACE)
2576 x_set_mouse_face_gc (s);
2577 s->stippled_p = s->face->stipple != 0;
2579 else if (s->hl == DRAW_IMAGE_RAISED
2580 || s->hl == DRAW_IMAGE_SUNKEN)
2582 s->gc = s->face->gc;
2583 s->stippled_p = s->face->stipple != 0;
2585 else
2587 s->gc = s->face->gc;
2588 s->stippled_p = s->face->stipple != 0;
2591 /* GC must have been set. */
2592 xassert (s->gc != 0);
2596 /* Return in *R the clipping rectangle for glyph string S. */
2598 static void
2599 x_get_glyph_string_clip_rect (s, r)
2600 struct glyph_string *s;
2601 XRectangle *r;
2603 if (s->row->full_width_p)
2605 /* Draw full-width. X coordinates are relative to S->w->left. */
2606 int canon_x = CANON_X_UNIT (s->f);
2608 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2609 r->width = XFASTINT (s->w->width) * canon_x;
2611 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2613 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2614 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2615 r->x -= width;
2618 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2620 /* Unless displaying a mode or menu bar line, which are always
2621 fully visible, clip to the visible part of the row. */
2622 if (s->w->pseudo_window_p)
2623 r->height = s->row->visible_height;
2624 else
2625 r->height = s->height;
2627 else
2629 /* This is a text line that may be partially visible. */
2630 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2631 r->width = window_box_width (s->w, s->area);
2632 r->height = s->row->visible_height;
2635 /* If S draws overlapping rows, it's sufficient to use the top and
2636 bottom of the window for clipping because this glyph string
2637 intentionally draws over other lines. */
2638 if (s->for_overlaps_p)
2640 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2641 r->height = window_text_bottom_y (s->w) - r->y;
2643 else
2645 /* Don't use S->y for clipping because it doesn't take partially
2646 visible lines into account. For example, it can be negative for
2647 partially visible lines at the top of a window. */
2648 if (!s->row->full_width_p
2649 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2650 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2651 else
2652 r->y = max (0, s->row->y);
2654 /* If drawing a tool-bar window, draw it over the internal border
2655 at the top of the window. */
2656 if (s->w == XWINDOW (s->f->tool_bar_window))
2657 r->y -= s->f->output_data.x->internal_border_width;
2660 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2664 /* Set clipping for output of glyph string S. S may be part of a mode
2665 line or menu if we don't have X toolkit support. */
2667 static INLINE void
2668 x_set_glyph_string_clipping (s)
2669 struct glyph_string *s;
2671 XRectangle r;
2672 x_get_glyph_string_clip_rect (s, &r);
2673 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2677 /* Compute left and right overhang of glyph string S. If S is a glyph
2678 string for a composition, assume overhangs don't exist. */
2680 static INLINE void
2681 x_compute_glyph_string_overhangs (s)
2682 struct glyph_string *s;
2684 if (s->cmp == NULL
2685 && s->first_glyph->type == CHAR_GLYPH)
2687 XCharStruct cs;
2688 int direction, font_ascent, font_descent;
2689 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2690 &font_ascent, &font_descent, &cs);
2691 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2692 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2697 /* Compute overhangs and x-positions for glyph string S and its
2698 predecessors, or successors. X is the starting x-position for S.
2699 BACKWARD_P non-zero means process predecessors. */
2701 static void
2702 x_compute_overhangs_and_x (s, x, backward_p)
2703 struct glyph_string *s;
2704 int x;
2705 int backward_p;
2707 if (backward_p)
2709 while (s)
2711 x_compute_glyph_string_overhangs (s);
2712 x -= s->width;
2713 s->x = x;
2714 s = s->prev;
2717 else
2719 while (s)
2721 x_compute_glyph_string_overhangs (s);
2722 s->x = x;
2723 x += s->width;
2724 s = s->next;
2730 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2731 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2732 assumed to be zero. */
2734 static void
2735 x_get_glyph_overhangs (glyph, f, left, right)
2736 struct glyph *glyph;
2737 struct frame *f;
2738 int *left, *right;
2740 *left = *right = 0;
2742 if (glyph->type == CHAR_GLYPH)
2744 XFontStruct *font;
2745 struct face *face;
2746 struct font_info *font_info;
2747 XChar2b char2b;
2748 XCharStruct *pcm;
2750 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2751 font = face->font;
2752 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2753 if (font
2754 && (pcm = x_per_char_metric (font, &char2b)))
2756 if (pcm->rbearing > pcm->width)
2757 *right = pcm->rbearing - pcm->width;
2758 if (pcm->lbearing < 0)
2759 *left = -pcm->lbearing;
2765 /* Return the index of the first glyph preceding glyph string S that
2766 is overwritten by S because of S's left overhang. Value is -1
2767 if no glyphs are overwritten. */
2769 static int
2770 x_left_overwritten (s)
2771 struct glyph_string *s;
2773 int k;
2775 if (s->left_overhang)
2777 int x = 0, i;
2778 struct glyph *glyphs = s->row->glyphs[s->area];
2779 int first = s->first_glyph - glyphs;
2781 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2782 x -= glyphs[i].pixel_width;
2784 k = i + 1;
2786 else
2787 k = -1;
2789 return k;
2793 /* Return the index of the first glyph preceding glyph string S that
2794 is overwriting S because of its right overhang. Value is -1 if no
2795 glyph in front of S overwrites S. */
2797 static int
2798 x_left_overwriting (s)
2799 struct glyph_string *s;
2801 int i, k, x;
2802 struct glyph *glyphs = s->row->glyphs[s->area];
2803 int first = s->first_glyph - glyphs;
2805 k = -1;
2806 x = 0;
2807 for (i = first - 1; i >= 0; --i)
2809 int left, right;
2810 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2811 if (x + right > 0)
2812 k = i;
2813 x -= glyphs[i].pixel_width;
2816 return k;
2820 /* Return the index of the last glyph following glyph string S that is
2821 not overwritten by S because of S's right overhang. Value is -1 if
2822 no such glyph is found. */
2824 static int
2825 x_right_overwritten (s)
2826 struct glyph_string *s;
2828 int k = -1;
2830 if (s->right_overhang)
2832 int x = 0, i;
2833 struct glyph *glyphs = s->row->glyphs[s->area];
2834 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2835 int end = s->row->used[s->area];
2837 for (i = first; i < end && s->right_overhang > x; ++i)
2838 x += glyphs[i].pixel_width;
2840 k = i;
2843 return k;
2847 /* Return the index of the last glyph following glyph string S that
2848 overwrites S because of its left overhang. Value is negative
2849 if no such glyph is found. */
2851 static int
2852 x_right_overwriting (s)
2853 struct glyph_string *s;
2855 int i, k, x;
2856 int end = s->row->used[s->area];
2857 struct glyph *glyphs = s->row->glyphs[s->area];
2858 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2860 k = -1;
2861 x = 0;
2862 for (i = first; i < end; ++i)
2864 int left, right;
2865 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2866 if (x - left < 0)
2867 k = i;
2868 x += glyphs[i].pixel_width;
2871 return k;
2875 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
2877 static INLINE void
2878 x_clear_glyph_string_rect (s, x, y, w, h)
2879 struct glyph_string *s;
2880 int x, y, w, h;
2882 XGCValues xgcv;
2883 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2884 XSetForeground (s->display, s->gc, xgcv.background);
2885 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2886 XSetForeground (s->display, s->gc, xgcv.foreground);
2890 /* Draw the background of glyph_string S. If S->background_filled_p
2891 is non-zero don't draw it. FORCE_P non-zero means draw the
2892 background even if it wouldn't be drawn normally. This is used
2893 when a string preceding S draws into the background of S, or S
2894 contains the first component of a composition. */
2896 static void
2897 x_draw_glyph_string_background (s, force_p)
2898 struct glyph_string *s;
2899 int force_p;
2901 /* Nothing to do if background has already been drawn or if it
2902 shouldn't be drawn in the first place. */
2903 if (!s->background_filled_p)
2905 int box_line_width = max (s->face->box_line_width, 0);
2907 if (s->stippled_p)
2909 /* Fill background with a stipple pattern. */
2910 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2911 XFillRectangle (s->display, s->window, s->gc, s->x,
2912 s->y + box_line_width,
2913 s->background_width,
2914 s->height - 2 * box_line_width);
2915 XSetFillStyle (s->display, s->gc, FillSolid);
2916 s->background_filled_p = 1;
2918 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
2919 || s->font_not_found_p
2920 || s->extends_to_end_of_line_p
2921 || force_p)
2923 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
2924 s->background_width,
2925 s->height - 2 * box_line_width);
2926 s->background_filled_p = 1;
2932 /* Draw the foreground of glyph string S. */
2934 static void
2935 x_draw_glyph_string_foreground (s)
2936 struct glyph_string *s;
2938 int i, x;
2940 /* If first glyph of S has a left box line, start drawing the text
2941 of S to the right of that box line. */
2942 if (s->face->box != FACE_NO_BOX
2943 && s->first_glyph->left_box_line_p)
2944 x = s->x + abs (s->face->box_line_width);
2945 else
2946 x = s->x;
2948 /* Draw characters of S as rectangles if S's font could not be
2949 loaded. */
2950 if (s->font_not_found_p)
2952 for (i = 0; i < s->nchars; ++i)
2954 struct glyph *g = s->first_glyph + i;
2955 XDrawRectangle (s->display, s->window,
2956 s->gc, x, s->y, g->pixel_width - 1,
2957 s->height - 1);
2958 x += g->pixel_width;
2961 else
2963 char *char1b = (char *) s->char2b;
2964 int boff = s->font_info->baseline_offset;
2966 if (s->font_info->vertical_centering)
2967 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
2969 /* If we can use 8-bit functions, condense S->char2b. */
2970 if (!s->two_byte_p)
2971 for (i = 0; i < s->nchars; ++i)
2972 char1b[i] = s->char2b[i].byte2;
2974 /* Draw text with XDrawString if background has already been
2975 filled. Otherwise, use XDrawImageString. (Note that
2976 XDrawImageString is usually faster than XDrawString.) Always
2977 use XDrawImageString when drawing the cursor so that there is
2978 no chance that characters under a box cursor are invisible. */
2979 if (s->for_overlaps_p
2980 || (s->background_filled_p && s->hl != DRAW_CURSOR))
2982 /* Draw characters with 16-bit or 8-bit functions. */
2983 if (s->two_byte_p)
2984 XDrawString16 (s->display, s->window, s->gc, x,
2985 s->ybase - boff, s->char2b, s->nchars);
2986 else
2987 XDrawString (s->display, s->window, s->gc, x,
2988 s->ybase - boff, char1b, s->nchars);
2990 else
2992 if (s->two_byte_p)
2993 XDrawImageString16 (s->display, s->window, s->gc, x,
2994 s->ybase - boff, s->char2b, s->nchars);
2995 else
2996 XDrawImageString (s->display, s->window, s->gc, x,
2997 s->ybase - boff, char1b, s->nchars);
3000 if (s->face->overstrike)
3002 /* For overstriking (to simulate bold-face), draw the
3003 characters again shifted to the right by one pixel. */
3004 if (s->two_byte_p)
3005 XDrawString16 (s->display, s->window, s->gc, x + 1,
3006 s->ybase - boff, s->char2b, s->nchars);
3007 else
3008 XDrawString (s->display, s->window, s->gc, x + 1,
3009 s->ybase - boff, char1b, s->nchars);
3014 /* Draw the foreground of composite glyph string S. */
3016 static void
3017 x_draw_composite_glyph_string_foreground (s)
3018 struct glyph_string *s;
3020 int i, x;
3022 /* If first glyph of S has a left box line, start drawing the text
3023 of S to the right of that box line. */
3024 if (s->face->box != FACE_NO_BOX
3025 && s->first_glyph->left_box_line_p)
3026 x = s->x + abs (s->face->box_line_width);
3027 else
3028 x = s->x;
3030 /* S is a glyph string for a composition. S->gidx is the index of
3031 the first character drawn for glyphs of this composition.
3032 S->gidx == 0 means we are drawing the very first character of
3033 this composition. */
3035 /* Draw a rectangle for the composition if the font for the very
3036 first character of the composition could not be loaded. */
3037 if (s->font_not_found_p)
3039 if (s->gidx == 0)
3040 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3041 s->width - 1, s->height - 1);
3043 else
3045 for (i = 0; i < s->nchars; i++, ++s->gidx)
3047 XDrawString16 (s->display, s->window, s->gc,
3048 x + s->cmp->offsets[s->gidx * 2],
3049 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3050 s->char2b + i, 1);
3051 if (s->face->overstrike)
3052 XDrawString16 (s->display, s->window, s->gc,
3053 x + s->cmp->offsets[s->gidx * 2] + 1,
3054 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3055 s->char2b + i, 1);
3061 #ifdef USE_X_TOOLKIT
3063 static struct frame *x_frame_of_widget P_ ((Widget));
3064 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3065 XrmValue *, XrmValue *, XtPointer *));
3066 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3067 XrmValue *, Cardinal *));
3070 /* Return the frame on which widget WIDGET is used.. Abort if frame
3071 cannot be determined. */
3073 static struct frame *
3074 x_frame_of_widget (widget)
3075 Widget widget;
3077 struct x_display_info *dpyinfo;
3078 Lisp_Object tail;
3079 struct frame *f;
3081 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3083 /* Find the top-level shell of the widget. Note that this function
3084 can be called when the widget is not yet realized, so XtWindow
3085 (widget) == 0. That's the reason we can't simply use
3086 x_any_window_to_frame. */
3087 while (!XtIsTopLevelShell (widget))
3088 widget = XtParent (widget);
3090 /* Look for a frame with that top-level widget. Allocate the color
3091 on that frame to get the right gamma correction value. */
3092 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3093 if (GC_FRAMEP (XCAR (tail))
3094 && (f = XFRAME (XCAR (tail)),
3095 (f->output_data.nothing != 1
3096 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3097 && f->output_data.x->widget == widget)
3098 return f;
3100 abort ();
3104 /* Allocate the color COLOR->pixel on the screen and display of
3105 widget WIDGET in colormap CMAP. If an exact match cannot be
3106 allocated, try the nearest color available. Value is non-zero
3107 if successful. This is called from lwlib. */
3110 x_alloc_nearest_color_for_widget (widget, cmap, color)
3111 Widget widget;
3112 Colormap cmap;
3113 XColor *color;
3115 struct frame *f = x_frame_of_widget (widget);
3116 return x_alloc_nearest_color (f, cmap, color);
3120 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3121 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3122 If this produces the same color as PIXEL, try a color where all RGB
3123 values have DELTA added. Return the allocated color in *PIXEL.
3124 DISPLAY is the X display, CMAP is the colormap to operate on.
3125 Value is non-zero if successful. */
3128 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3129 Widget widget;
3130 Display *display;
3131 Colormap cmap;
3132 unsigned long *pixel;
3133 double factor;
3134 int delta;
3136 struct frame *f = x_frame_of_widget (widget);
3137 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3141 /* Structure specifying which arguments should be passed by Xt to
3142 cvt_string_to_pixel. We want the widget's screen and colormap. */
3144 static XtConvertArgRec cvt_string_to_pixel_args[] =
3146 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3147 sizeof (Screen *)},
3148 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3149 sizeof (Colormap)}
3153 /* The address of this variable is returned by
3154 cvt_string_to_pixel. */
3156 static Pixel cvt_string_to_pixel_value;
3159 /* Convert a color name to a pixel color.
3161 DPY is the display we are working on.
3163 ARGS is an array of *NARGS XrmValue structures holding additional
3164 information about the widget for which the conversion takes place.
3165 The contents of this array are determined by the specification
3166 in cvt_string_to_pixel_args.
3168 FROM is a pointer to an XrmValue which points to the color name to
3169 convert. TO is an XrmValue in which to return the pixel color.
3171 CLOSURE_RET is a pointer to user-data, in which we record if
3172 we allocated the color or not.
3174 Value is True if successful, False otherwise. */
3176 static Boolean
3177 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3178 Display *dpy;
3179 XrmValue *args;
3180 Cardinal *nargs;
3181 XrmValue *from, *to;
3182 XtPointer *closure_ret;
3184 Screen *screen;
3185 Colormap cmap;
3186 Pixel pixel;
3187 String color_name;
3188 XColor color;
3190 if (*nargs != 2)
3192 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3193 "wrongParameters", "cvt_string_to_pixel",
3194 "XtToolkitError",
3195 "Screen and colormap args required", NULL, NULL);
3196 return False;
3199 screen = *(Screen **) args[0].addr;
3200 cmap = *(Colormap *) args[1].addr;
3201 color_name = (String) from->addr;
3203 if (strcmp (color_name, XtDefaultBackground) == 0)
3205 *closure_ret = (XtPointer) False;
3206 pixel = WhitePixelOfScreen (screen);
3208 else if (strcmp (color_name, XtDefaultForeground) == 0)
3210 *closure_ret = (XtPointer) False;
3211 pixel = BlackPixelOfScreen (screen);
3213 else if (XParseColor (dpy, cmap, color_name, &color)
3214 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3216 pixel = color.pixel;
3217 *closure_ret = (XtPointer) True;
3219 else
3221 String params[1];
3222 Cardinal nparams = 1;
3224 params[0] = color_name;
3225 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3226 "badValue", "cvt_string_to_pixel",
3227 "XtToolkitError", "Invalid color `%s'",
3228 params, &nparams);
3229 return False;
3232 if (to->addr != NULL)
3234 if (to->size < sizeof (Pixel))
3236 to->size = sizeof (Pixel);
3237 return False;
3240 *(Pixel *) to->addr = pixel;
3242 else
3244 cvt_string_to_pixel_value = pixel;
3245 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3248 to->size = sizeof (Pixel);
3249 return True;
3253 /* Free a pixel color which was previously allocated via
3254 cvt_string_to_pixel. This is registered as the destructor
3255 for this type of resource via XtSetTypeConverter.
3257 APP is the application context in which we work.
3259 TO is a pointer to an XrmValue holding the color to free.
3260 CLOSURE is the value we stored in CLOSURE_RET for this color
3261 in cvt_string_to_pixel.
3263 ARGS and NARGS are like for cvt_string_to_pixel. */
3265 static void
3266 cvt_pixel_dtor (app, to, closure, args, nargs)
3267 XtAppContext app;
3268 XrmValuePtr to;
3269 XtPointer closure;
3270 XrmValuePtr args;
3271 Cardinal *nargs;
3273 if (*nargs != 2)
3275 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3276 "XtToolkitError",
3277 "Screen and colormap arguments required",
3278 NULL, NULL);
3280 else if (closure != NULL)
3282 /* We did allocate the pixel, so free it. */
3283 Screen *screen = *(Screen **) args[0].addr;
3284 Colormap cmap = *(Colormap *) args[1].addr;
3285 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3286 (Pixel *) to->addr, 1);
3291 #endif /* USE_X_TOOLKIT */
3294 /* Value is an array of XColor structures for the contents of the
3295 color map of display DPY. Set *NCELLS to the size of the array.
3296 Note that this probably shouldn't be called for large color maps,
3297 say a 24-bit TrueColor map. */
3299 static const XColor *
3300 x_color_cells (dpy, ncells)
3301 Display *dpy;
3302 int *ncells;
3304 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3306 if (dpyinfo->color_cells == NULL)
3308 Screen *screen = dpyinfo->screen;
3309 int i;
3311 dpyinfo->ncolor_cells
3312 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3313 dpyinfo->color_cells
3314 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3315 * sizeof *dpyinfo->color_cells);
3317 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3318 dpyinfo->color_cells[i].pixel = i;
3320 XQueryColors (dpy, dpyinfo->cmap,
3321 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3324 *ncells = dpyinfo->ncolor_cells;
3325 return dpyinfo->color_cells;
3329 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3330 colors in COLORS. Use cached information, if available. */
3332 void
3333 x_query_colors (f, colors, ncolors)
3334 struct frame *f;
3335 XColor *colors;
3336 int ncolors;
3338 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3340 if (dpyinfo->color_cells)
3342 int i;
3343 for (i = 0; i < ncolors; ++i)
3345 unsigned long pixel = colors[i].pixel;
3346 xassert (pixel < dpyinfo->ncolor_cells);
3347 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3348 colors[i] = dpyinfo->color_cells[pixel];
3351 else
3352 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3356 /* On frame F, translate pixel color to RGB values for the color in
3357 COLOR. Use cached information, if available. */
3359 void
3360 x_query_color (f, color)
3361 struct frame *f;
3362 XColor *color;
3364 x_query_colors (f, color, 1);
3368 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3369 exact match can't be allocated, try the nearest color available.
3370 Value is non-zero if successful. Set *COLOR to the color
3371 allocated. */
3373 static int
3374 x_alloc_nearest_color_1 (dpy, cmap, color)
3375 Display *dpy;
3376 Colormap cmap;
3377 XColor *color;
3379 int rc;
3381 rc = XAllocColor (dpy, cmap, color);
3382 if (rc == 0)
3384 /* If we got to this point, the colormap is full, so we're going
3385 to try to get the next closest color. The algorithm used is
3386 a least-squares matching, which is what X uses for closest
3387 color matching with StaticColor visuals. */
3388 int nearest, i;
3389 unsigned long nearest_delta = ~0;
3390 int ncells;
3391 const XColor *cells = x_color_cells (dpy, &ncells);
3393 for (nearest = i = 0; i < ncells; ++i)
3395 long dred = (color->red >> 8) - (cells[i].red >> 8);
3396 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3397 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3398 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3400 if (delta < nearest_delta)
3402 nearest = i;
3403 nearest_delta = delta;
3407 color->red = cells[nearest].red;
3408 color->green = cells[nearest].green;
3409 color->blue = cells[nearest].blue;
3410 rc = XAllocColor (dpy, cmap, color);
3412 else
3414 /* If allocation succeeded, and the allocated pixel color is not
3415 equal to a cached pixel color recorded earlier, there was a
3416 change in the colormap, so clear the color cache. */
3417 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3418 XColor *cached_color;
3420 if (dpyinfo->color_cells
3421 && (cached_color = &dpyinfo->color_cells[color->pixel],
3422 (cached_color->red != color->red
3423 || cached_color->blue != color->blue
3424 || cached_color->green != color->green)))
3426 xfree (dpyinfo->color_cells);
3427 dpyinfo->color_cells = NULL;
3428 dpyinfo->ncolor_cells = 0;
3432 #ifdef DEBUG_X_COLORS
3433 if (rc)
3434 register_color (color->pixel);
3435 #endif /* DEBUG_X_COLORS */
3437 return rc;
3441 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3442 exact match can't be allocated, try the nearest color available.
3443 Value is non-zero if successful. Set *COLOR to the color
3444 allocated. */
3447 x_alloc_nearest_color (f, cmap, color)
3448 struct frame *f;
3449 Colormap cmap;
3450 XColor *color;
3452 gamma_correct (f, color);
3453 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3457 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3458 It's necessary to do this instead of just using PIXEL directly to
3459 get color reference counts right. */
3461 unsigned long
3462 x_copy_color (f, pixel)
3463 struct frame *f;
3464 unsigned long pixel;
3466 XColor color;
3468 color.pixel = pixel;
3469 BLOCK_INPUT;
3470 x_query_color (f, &color);
3471 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3472 UNBLOCK_INPUT;
3473 #ifdef DEBUG_X_COLORS
3474 register_color (pixel);
3475 #endif
3476 return color.pixel;
3480 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3481 It's necessary to do this instead of just using PIXEL directly to
3482 get color reference counts right. */
3484 unsigned long
3485 x_copy_dpy_color (dpy, cmap, pixel)
3486 Display *dpy;
3487 Colormap cmap;
3488 unsigned long pixel;
3490 XColor color;
3492 color.pixel = pixel;
3493 BLOCK_INPUT;
3494 XQueryColor (dpy, cmap, &color);
3495 XAllocColor (dpy, cmap, &color);
3496 UNBLOCK_INPUT;
3497 #ifdef DEBUG_X_COLORS
3498 register_color (pixel);
3499 #endif
3500 return color.pixel;
3504 /* Brightness beyond which a color won't have its highlight brightness
3505 boosted.
3507 Nominally, highlight colors for `3d' faces are calculated by
3508 brightening an object's color by a constant scale factor, but this
3509 doesn't yield good results for dark colors, so for colors who's
3510 brightness is less than this value (on a scale of 0-65535) have an
3511 use an additional additive factor.
3513 The value here is set so that the default menu-bar/mode-line color
3514 (grey75) will not have its highlights changed at all. */
3515 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3518 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3519 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3520 If this produces the same color as PIXEL, try a color where all RGB
3521 values have DELTA added. Return the allocated color in *PIXEL.
3522 DISPLAY is the X display, CMAP is the colormap to operate on.
3523 Value is non-zero if successful. */
3525 static int
3526 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3527 struct frame *f;
3528 Display *display;
3529 Colormap cmap;
3530 unsigned long *pixel;
3531 double factor;
3532 int delta;
3534 XColor color, new;
3535 long bright;
3536 int success_p;
3538 /* Get RGB color values. */
3539 color.pixel = *pixel;
3540 x_query_color (f, &color);
3542 /* Change RGB values by specified FACTOR. Avoid overflow! */
3543 xassert (factor >= 0);
3544 new.red = min (0xffff, factor * color.red);
3545 new.green = min (0xffff, factor * color.green);
3546 new.blue = min (0xffff, factor * color.blue);
3548 /* Calculate brightness of COLOR. */
3549 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3551 /* We only boost colors that are darker than
3552 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3553 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3554 /* Make an additive adjustment to NEW, because it's dark enough so
3555 that scaling by FACTOR alone isn't enough. */
3557 /* How far below the limit this color is (0 - 1, 1 being darker). */
3558 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3559 /* The additive adjustment. */
3560 int min_delta = delta * dimness * factor / 2;
3562 if (factor < 1)
3564 new.red = max (0, new.red - min_delta);
3565 new.green = max (0, new.green - min_delta);
3566 new.blue = max (0, new.blue - min_delta);
3568 else
3570 new.red = min (0xffff, min_delta + new.red);
3571 new.green = min (0xffff, min_delta + new.green);
3572 new.blue = min (0xffff, min_delta + new.blue);
3576 /* Try to allocate the color. */
3577 success_p = x_alloc_nearest_color (f, cmap, &new);
3578 if (success_p)
3580 if (new.pixel == *pixel)
3582 /* If we end up with the same color as before, try adding
3583 delta to the RGB values. */
3584 x_free_colors (f, &new.pixel, 1);
3586 new.red = min (0xffff, delta + color.red);
3587 new.green = min (0xffff, delta + color.green);
3588 new.blue = min (0xffff, delta + color.blue);
3589 success_p = x_alloc_nearest_color (f, cmap, &new);
3591 else
3592 success_p = 1;
3593 *pixel = new.pixel;
3596 return success_p;
3600 /* Set up the foreground color for drawing relief lines of glyph
3601 string S. RELIEF is a pointer to a struct relief containing the GC
3602 with which lines will be drawn. Use a color that is FACTOR or
3603 DELTA lighter or darker than the relief's background which is found
3604 in S->f->output_data.x->relief_background. If such a color cannot
3605 be allocated, use DEFAULT_PIXEL, instead. */
3607 static void
3608 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3609 struct frame *f;
3610 struct relief *relief;
3611 double factor;
3612 int delta;
3613 unsigned long default_pixel;
3615 XGCValues xgcv;
3616 struct x_output *di = f->output_data.x;
3617 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3618 unsigned long pixel;
3619 unsigned long background = di->relief_background;
3620 Colormap cmap = FRAME_X_COLORMAP (f);
3621 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3622 Display *dpy = FRAME_X_DISPLAY (f);
3624 xgcv.graphics_exposures = False;
3625 xgcv.line_width = 1;
3627 /* Free previously allocated color. The color cell will be reused
3628 when it has been freed as many times as it was allocated, so this
3629 doesn't affect faces using the same colors. */
3630 if (relief->gc
3631 && relief->allocated_p)
3633 x_free_colors (f, &relief->pixel, 1);
3634 relief->allocated_p = 0;
3637 /* Allocate new color. */
3638 xgcv.foreground = default_pixel;
3639 pixel = background;
3640 if (dpyinfo->n_planes != 1
3641 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3643 relief->allocated_p = 1;
3644 xgcv.foreground = relief->pixel = pixel;
3647 if (relief->gc == 0)
3649 xgcv.stipple = dpyinfo->gray;
3650 mask |= GCStipple;
3651 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3653 else
3654 XChangeGC (dpy, relief->gc, mask, &xgcv);
3658 /* Set up colors for the relief lines around glyph string S. */
3660 static void
3661 x_setup_relief_colors (s)
3662 struct glyph_string *s;
3664 struct x_output *di = s->f->output_data.x;
3665 unsigned long color;
3667 if (s->face->use_box_color_for_shadows_p)
3668 color = s->face->box_color;
3669 else if (s->first_glyph->type == IMAGE_GLYPH
3670 && s->img->pixmap
3671 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3672 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3673 else
3675 XGCValues xgcv;
3677 /* Get the background color of the face. */
3678 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3679 color = xgcv.background;
3682 if (di->white_relief.gc == 0
3683 || color != di->relief_background)
3685 di->relief_background = color;
3686 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3687 WHITE_PIX_DEFAULT (s->f));
3688 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3689 BLACK_PIX_DEFAULT (s->f));
3694 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3695 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3696 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3697 relief. LEFT_P non-zero means draw a relief on the left side of
3698 the rectangle. RIGHT_P non-zero means draw a relief on the right
3699 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3700 when drawing. */
3702 static void
3703 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3704 raised_p, left_p, right_p, clip_rect)
3705 struct frame *f;
3706 int left_x, top_y, right_x, bottom_y, width, left_p, right_p, raised_p;
3707 XRectangle *clip_rect;
3709 Display *dpy = FRAME_X_DISPLAY (f);
3710 Window window = FRAME_X_WINDOW (f);
3711 int i;
3712 GC gc;
3714 if (raised_p)
3715 gc = f->output_data.x->white_relief.gc;
3716 else
3717 gc = f->output_data.x->black_relief.gc;
3718 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3720 /* Top. */
3721 for (i = 0; i < width; ++i)
3722 XDrawLine (dpy, window, gc,
3723 left_x + i * left_p, top_y + i,
3724 right_x + 1 - i * right_p, top_y + i);
3726 /* Left. */
3727 if (left_p)
3728 for (i = 0; i < width; ++i)
3729 XDrawLine (dpy, window, gc,
3730 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3732 XSetClipMask (dpy, gc, None);
3733 if (raised_p)
3734 gc = f->output_data.x->black_relief.gc;
3735 else
3736 gc = f->output_data.x->white_relief.gc;
3737 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3739 /* Bottom. */
3740 for (i = 0; i < width; ++i)
3741 XDrawLine (dpy, window, gc,
3742 left_x + i * left_p, bottom_y - i,
3743 right_x + 1 - i * right_p, bottom_y - i);
3745 /* Right. */
3746 if (right_p)
3747 for (i = 0; i < width; ++i)
3748 XDrawLine (dpy, window, gc,
3749 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3751 XSetClipMask (dpy, gc, None);
3755 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3756 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3757 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3758 left side of the rectangle. RIGHT_P non-zero means draw a line
3759 on the right side of the rectangle. CLIP_RECT is the clipping
3760 rectangle to use when drawing. */
3762 static void
3763 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3764 left_p, right_p, clip_rect)
3765 struct glyph_string *s;
3766 int left_x, top_y, right_x, bottom_y, width, left_p, right_p;
3767 XRectangle *clip_rect;
3769 XGCValues xgcv;
3771 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3772 XSetForeground (s->display, s->gc, s->face->box_color);
3773 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3775 /* Top. */
3776 XFillRectangle (s->display, s->window, s->gc,
3777 left_x, top_y, right_x - left_x + 1, width);
3779 /* Left. */
3780 if (left_p)
3781 XFillRectangle (s->display, s->window, s->gc,
3782 left_x, top_y, width, bottom_y - top_y + 1);
3784 /* Bottom. */
3785 XFillRectangle (s->display, s->window, s->gc,
3786 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3788 /* Right. */
3789 if (right_p)
3790 XFillRectangle (s->display, s->window, s->gc,
3791 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3793 XSetForeground (s->display, s->gc, xgcv.foreground);
3794 XSetClipMask (s->display, s->gc, None);
3798 /* Draw a box around glyph string S. */
3800 static void
3801 x_draw_glyph_string_box (s)
3802 struct glyph_string *s;
3804 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3805 int left_p, right_p;
3806 struct glyph *last_glyph;
3807 XRectangle clip_rect;
3809 last_x = window_box_right (s->w, s->area);
3810 if (s->row->full_width_p
3811 && !s->w->pseudo_window_p)
3813 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
3814 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3815 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3818 /* The glyph that may have a right box line. */
3819 last_glyph = (s->cmp || s->img
3820 ? s->first_glyph
3821 : s->first_glyph + s->nchars - 1);
3823 width = abs (s->face->box_line_width);
3824 raised_p = s->face->box == FACE_RAISED_BOX;
3825 left_x = s->x;
3826 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
3827 ? last_x - 1
3828 : min (last_x, s->x + s->background_width) - 1);
3829 top_y = s->y;
3830 bottom_y = top_y + s->height - 1;
3832 left_p = (s->first_glyph->left_box_line_p
3833 || (s->hl == DRAW_MOUSE_FACE
3834 && (s->prev == NULL
3835 || s->prev->hl != s->hl)));
3836 right_p = (last_glyph->right_box_line_p
3837 || (s->hl == DRAW_MOUSE_FACE
3838 && (s->next == NULL
3839 || s->next->hl != s->hl)));
3841 x_get_glyph_string_clip_rect (s, &clip_rect);
3843 if (s->face->box == FACE_SIMPLE_BOX)
3844 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3845 left_p, right_p, &clip_rect);
3846 else
3848 x_setup_relief_colors (s);
3849 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3850 width, raised_p, left_p, right_p, &clip_rect);
3855 /* Draw foreground of image glyph string S. */
3857 static void
3858 x_draw_image_foreground (s)
3859 struct glyph_string *s;
3861 int x;
3862 int y = s->ybase - image_ascent (s->img, s->face);
3864 /* If first glyph of S has a left box line, start drawing it to the
3865 right of that line. */
3866 if (s->face->box != FACE_NO_BOX
3867 && s->first_glyph->left_box_line_p)
3868 x = s->x + abs (s->face->box_line_width);
3869 else
3870 x = s->x;
3872 /* If there is a margin around the image, adjust x- and y-position
3873 by that margin. */
3874 x += s->img->hmargin;
3875 y += s->img->vmargin;
3877 if (s->img->pixmap)
3879 if (s->img->mask)
3881 /* We can't set both a clip mask and use XSetClipRectangles
3882 because the latter also sets a clip mask. We also can't
3883 trust on the shape extension to be available
3884 (XShapeCombineRegion). So, compute the rectangle to draw
3885 manually. */
3886 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3887 | GCFunction);
3888 XGCValues xgcv;
3889 XRectangle clip_rect, image_rect, r;
3891 xgcv.clip_mask = s->img->mask;
3892 xgcv.clip_x_origin = x;
3893 xgcv.clip_y_origin = y;
3894 xgcv.function = GXcopy;
3895 XChangeGC (s->display, s->gc, mask, &xgcv);
3897 x_get_glyph_string_clip_rect (s, &clip_rect);
3898 image_rect.x = x;
3899 image_rect.y = y;
3900 image_rect.width = s->img->width;
3901 image_rect.height = s->img->height;
3902 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3903 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3904 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3906 else
3908 XRectangle clip_rect, image_rect, r;
3910 x_get_glyph_string_clip_rect (s, &clip_rect);
3911 image_rect.x = x;
3912 image_rect.y = y;
3913 image_rect.width = s->img->width;
3914 image_rect.height = s->img->height;
3915 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3916 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3917 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3919 /* When the image has a mask, we can expect that at
3920 least part of a mouse highlight or a block cursor will
3921 be visible. If the image doesn't have a mask, make
3922 a block cursor visible by drawing a rectangle around
3923 the image. I believe it's looking better if we do
3924 nothing here for mouse-face. */
3925 if (s->hl == DRAW_CURSOR)
3927 int r = s->img->relief;
3928 if (r < 0) r = -r;
3929 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
3930 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
3934 else
3935 /* Draw a rectangle if image could not be loaded. */
3936 XDrawRectangle (s->display, s->window, s->gc, x, y,
3937 s->img->width - 1, s->img->height - 1);
3941 /* Draw a relief around the image glyph string S. */
3943 static void
3944 x_draw_image_relief (s)
3945 struct glyph_string *s;
3947 int x0, y0, x1, y1, thick, raised_p;
3948 XRectangle r;
3949 int x;
3950 int y = s->ybase - image_ascent (s->img, s->face);
3952 /* If first glyph of S has a left box line, start drawing it to the
3953 right of that line. */
3954 if (s->face->box != FACE_NO_BOX
3955 && s->first_glyph->left_box_line_p)
3956 x = s->x + abs (s->face->box_line_width);
3957 else
3958 x = s->x;
3960 /* If there is a margin around the image, adjust x- and y-position
3961 by that margin. */
3962 x += s->img->hmargin;
3963 y += s->img->vmargin;
3965 if (s->hl == DRAW_IMAGE_SUNKEN
3966 || s->hl == DRAW_IMAGE_RAISED)
3968 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
3969 raised_p = s->hl == DRAW_IMAGE_RAISED;
3971 else
3973 thick = abs (s->img->relief);
3974 raised_p = s->img->relief > 0;
3977 x0 = x - thick;
3978 y0 = y - thick;
3979 x1 = x + s->img->width + thick - 1;
3980 y1 = y + s->img->height + thick - 1;
3982 x_setup_relief_colors (s);
3983 x_get_glyph_string_clip_rect (s, &r);
3984 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3988 /* Draw the foreground of image glyph string S to PIXMAP. */
3990 static void
3991 x_draw_image_foreground_1 (s, pixmap)
3992 struct glyph_string *s;
3993 Pixmap pixmap;
3995 int x;
3996 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3998 /* If first glyph of S has a left box line, start drawing it to the
3999 right of that line. */
4000 if (s->face->box != FACE_NO_BOX
4001 && s->first_glyph->left_box_line_p)
4002 x = abs (s->face->box_line_width);
4003 else
4004 x = 0;
4006 /* If there is a margin around the image, adjust x- and y-position
4007 by that margin. */
4008 x += s->img->hmargin;
4009 y += s->img->vmargin;
4011 if (s->img->pixmap)
4013 if (s->img->mask)
4015 /* We can't set both a clip mask and use XSetClipRectangles
4016 because the latter also sets a clip mask. We also can't
4017 trust on the shape extension to be available
4018 (XShapeCombineRegion). So, compute the rectangle to draw
4019 manually. */
4020 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4021 | GCFunction);
4022 XGCValues xgcv;
4024 xgcv.clip_mask = s->img->mask;
4025 xgcv.clip_x_origin = x;
4026 xgcv.clip_y_origin = y;
4027 xgcv.function = GXcopy;
4028 XChangeGC (s->display, s->gc, mask, &xgcv);
4030 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4031 0, 0, s->img->width, s->img->height, x, y);
4032 XSetClipMask (s->display, s->gc, None);
4034 else
4036 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4037 0, 0, s->img->width, s->img->height, x, y);
4039 /* When the image has a mask, we can expect that at
4040 least part of a mouse highlight or a block cursor will
4041 be visible. If the image doesn't have a mask, make
4042 a block cursor visible by drawing a rectangle around
4043 the image. I believe it's looking better if we do
4044 nothing here for mouse-face. */
4045 if (s->hl == DRAW_CURSOR)
4047 int r = s->img->relief;
4048 if (r < 0) r = -r;
4049 XDrawRectangle (s->display, s->window, s->gc, x - r, y - r,
4050 s->img->width + r*2 - 1, s->img->height + r*2 - 1);
4054 else
4055 /* Draw a rectangle if image could not be loaded. */
4056 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4057 s->img->width - 1, s->img->height - 1);
4061 /* Draw part of the background of glyph string S. X, Y, W, and H
4062 give the rectangle to draw. */
4064 static void
4065 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4066 struct glyph_string *s;
4067 int x, y, w, h;
4069 if (s->stippled_p)
4071 /* Fill background with a stipple pattern. */
4072 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4073 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4074 XSetFillStyle (s->display, s->gc, FillSolid);
4076 else
4077 x_clear_glyph_string_rect (s, x, y, w, h);
4081 /* Draw image glyph string S.
4083 s->y
4084 s->x +-------------------------
4085 | s->face->box
4087 | +-------------------------
4088 | | s->img->margin
4090 | | +-------------------
4091 | | | the image
4095 static void
4096 x_draw_image_glyph_string (s)
4097 struct glyph_string *s;
4099 int x, y;
4100 int box_line_hwidth = abs (s->face->box_line_width);
4101 int box_line_vwidth = max (s->face->box_line_width, 0);
4102 int height;
4103 Pixmap pixmap = None;
4105 height = s->height - 2 * box_line_vwidth;
4108 /* Fill background with face under the image. Do it only if row is
4109 taller than image or if image has a clip mask to reduce
4110 flickering. */
4111 s->stippled_p = s->face->stipple != 0;
4112 if (height > s->img->height
4113 || s->img->hmargin
4114 || s->img->vmargin
4115 || s->img->mask
4116 || s->img->pixmap == 0
4117 || s->width != s->background_width)
4119 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4120 x = s->x + box_line_hwidth;
4121 else
4122 x = s->x;
4124 y = s->y + box_line_vwidth;
4126 if (s->img->mask)
4128 /* Create a pixmap as large as the glyph string. Fill it
4129 with the background color. Copy the image to it, using
4130 its mask. Copy the temporary pixmap to the display. */
4131 Screen *screen = FRAME_X_SCREEN (s->f);
4132 int depth = DefaultDepthOfScreen (screen);
4134 /* Create a pixmap as large as the glyph string. */
4135 pixmap = XCreatePixmap (s->display, s->window,
4136 s->background_width,
4137 s->height, depth);
4139 /* Don't clip in the following because we're working on the
4140 pixmap. */
4141 XSetClipMask (s->display, s->gc, None);
4143 /* Fill the pixmap with the background color/stipple. */
4144 if (s->stippled_p)
4146 /* Fill background with a stipple pattern. */
4147 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4148 XFillRectangle (s->display, pixmap, s->gc,
4149 0, 0, s->background_width, s->height);
4150 XSetFillStyle (s->display, s->gc, FillSolid);
4152 else
4154 XGCValues xgcv;
4155 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4156 &xgcv);
4157 XSetForeground (s->display, s->gc, xgcv.background);
4158 XFillRectangle (s->display, pixmap, s->gc,
4159 0, 0, s->background_width, s->height);
4160 XSetForeground (s->display, s->gc, xgcv.foreground);
4163 else
4164 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4166 s->background_filled_p = 1;
4169 /* Draw the foreground. */
4170 if (pixmap != None)
4172 x_draw_image_foreground_1 (s, pixmap);
4173 x_set_glyph_string_clipping (s);
4174 XCopyArea (s->display, pixmap, s->window, s->gc,
4175 0, 0, s->background_width, s->height, s->x, s->y);
4176 XFreePixmap (s->display, pixmap);
4178 else
4179 x_draw_image_foreground (s);
4181 /* If we must draw a relief around the image, do it. */
4182 if (s->img->relief
4183 || s->hl == DRAW_IMAGE_RAISED
4184 || s->hl == DRAW_IMAGE_SUNKEN)
4185 x_draw_image_relief (s);
4189 /* Draw stretch glyph string S. */
4191 static void
4192 x_draw_stretch_glyph_string (s)
4193 struct glyph_string *s;
4195 xassert (s->first_glyph->type == STRETCH_GLYPH);
4196 s->stippled_p = s->face->stipple != 0;
4198 if (s->hl == DRAW_CURSOR
4199 && !x_stretch_cursor_p)
4201 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4202 as wide as the stretch glyph. */
4203 int width = min (CANON_X_UNIT (s->f), s->background_width);
4205 /* Draw cursor. */
4206 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4208 /* Clear rest using the GC of the original non-cursor face. */
4209 if (width < s->background_width)
4211 int x = s->x + width, y = s->y;
4212 int w = s->background_width - width, h = s->height;
4213 XRectangle r;
4214 GC gc;
4216 if (s->row->mouse_face_p
4217 && cursor_in_mouse_face_p (s->w))
4219 x_set_mouse_face_gc (s);
4220 gc = s->gc;
4222 else
4223 gc = s->face->gc;
4225 x_get_glyph_string_clip_rect (s, &r);
4226 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4228 if (s->face->stipple)
4230 /* Fill background with a stipple pattern. */
4231 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4232 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4233 XSetFillStyle (s->display, gc, FillSolid);
4235 else
4237 XGCValues xgcv;
4238 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4239 XSetForeground (s->display, gc, xgcv.background);
4240 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4241 XSetForeground (s->display, gc, xgcv.foreground);
4245 else if (!s->background_filled_p)
4246 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4247 s->height);
4249 s->background_filled_p = 1;
4253 /* Draw glyph string S. */
4255 static void
4256 x_draw_glyph_string (s)
4257 struct glyph_string *s;
4259 int relief_drawn_p = 0;
4261 /* If S draws into the background of its successor, draw the
4262 background of the successor first so that S can draw into it.
4263 This makes S->next use XDrawString instead of XDrawImageString. */
4264 if (s->next && s->right_overhang && !s->for_overlaps_p)
4266 xassert (s->next->img == NULL);
4267 x_set_glyph_string_gc (s->next);
4268 x_set_glyph_string_clipping (s->next);
4269 x_draw_glyph_string_background (s->next, 1);
4272 /* Set up S->gc, set clipping and draw S. */
4273 x_set_glyph_string_gc (s);
4275 /* Draw relief (if any) in advance for char/composition so that the
4276 glyph string can be drawn over it. */
4277 if (!s->for_overlaps_p
4278 && s->face->box != FACE_NO_BOX
4279 && (s->first_glyph->type == CHAR_GLYPH
4280 || s->first_glyph->type == COMPOSITE_GLYPH))
4283 x_set_glyph_string_clipping (s);
4284 x_draw_glyph_string_background (s, 1);
4285 x_draw_glyph_string_box (s);
4286 x_set_glyph_string_clipping (s);
4287 relief_drawn_p = 1;
4289 else
4290 x_set_glyph_string_clipping (s);
4292 switch (s->first_glyph->type)
4294 case IMAGE_GLYPH:
4295 x_draw_image_glyph_string (s);
4296 break;
4298 case STRETCH_GLYPH:
4299 x_draw_stretch_glyph_string (s);
4300 break;
4302 case CHAR_GLYPH:
4303 if (s->for_overlaps_p)
4304 s->background_filled_p = 1;
4305 else
4306 x_draw_glyph_string_background (s, 0);
4307 x_draw_glyph_string_foreground (s);
4308 break;
4310 case COMPOSITE_GLYPH:
4311 if (s->for_overlaps_p || s->gidx > 0)
4312 s->background_filled_p = 1;
4313 else
4314 x_draw_glyph_string_background (s, 1);
4315 x_draw_composite_glyph_string_foreground (s);
4316 break;
4318 default:
4319 abort ();
4322 if (!s->for_overlaps_p)
4324 /* Draw underline. */
4325 if (s->face->underline_p)
4327 unsigned long tem, h;
4328 int y;
4330 /* Get the underline thickness. Default is 1 pixel. */
4331 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4332 h = 1;
4334 /* Get the underline position. This is the recommended
4335 vertical offset in pixels from the baseline to the top of
4336 the underline. This is a signed value according to the
4337 specs, and its default is
4339 ROUND ((maximum descent) / 2), with
4340 ROUND(x) = floor (x + 0.5) */
4342 if (x_use_underline_position_properties
4343 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4344 y = s->ybase + (long) tem;
4345 else if (s->face->font)
4346 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4347 else
4348 y = s->y + s->height - h;
4350 if (s->face->underline_defaulted_p)
4351 XFillRectangle (s->display, s->window, s->gc,
4352 s->x, y, s->width, h);
4353 else
4355 XGCValues xgcv;
4356 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4357 XSetForeground (s->display, s->gc, s->face->underline_color);
4358 XFillRectangle (s->display, s->window, s->gc,
4359 s->x, y, s->width, h);
4360 XSetForeground (s->display, s->gc, xgcv.foreground);
4364 /* Draw overline. */
4365 if (s->face->overline_p)
4367 unsigned long dy = 0, h = 1;
4369 if (s->face->overline_color_defaulted_p)
4370 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4371 s->width, h);
4372 else
4374 XGCValues xgcv;
4375 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4376 XSetForeground (s->display, s->gc, s->face->overline_color);
4377 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4378 s->width, h);
4379 XSetForeground (s->display, s->gc, xgcv.foreground);
4383 /* Draw strike-through. */
4384 if (s->face->strike_through_p)
4386 unsigned long h = 1;
4387 unsigned long dy = (s->height - h) / 2;
4389 if (s->face->strike_through_color_defaulted_p)
4390 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4391 s->width, h);
4392 else
4394 XGCValues xgcv;
4395 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4396 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4397 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4398 s->width, h);
4399 XSetForeground (s->display, s->gc, xgcv.foreground);
4403 /* Draw relief if not yet drawn. */
4404 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4405 x_draw_glyph_string_box (s);
4408 /* Reset clipping. */
4409 XSetClipMask (s->display, s->gc, None);
4413 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4414 struct face **, int));
4417 /* Fill glyph string S with composition components specified by S->cmp.
4419 FACES is an array of faces for all components of this composition.
4420 S->gidx is the index of the first component for S.
4421 OVERLAPS_P non-zero means S should draw the foreground only, and
4422 use its physical height for clipping.
4424 Value is the index of a component not in S. */
4426 static int
4427 x_fill_composite_glyph_string (s, faces, overlaps_p)
4428 struct glyph_string *s;
4429 struct face **faces;
4430 int overlaps_p;
4432 int i;
4434 xassert (s);
4436 s->for_overlaps_p = overlaps_p;
4438 s->face = faces[s->gidx];
4439 s->font = s->face->font;
4440 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4442 /* For all glyphs of this composition, starting at the offset
4443 S->gidx, until we reach the end of the definition or encounter a
4444 glyph that requires the different face, add it to S. */
4445 ++s->nchars;
4446 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4447 ++s->nchars;
4449 /* All glyph strings for the same composition has the same width,
4450 i.e. the width set for the first component of the composition. */
4452 s->width = s->first_glyph->pixel_width;
4454 /* If the specified font could not be loaded, use the frame's
4455 default font, but record the fact that we couldn't load it in
4456 the glyph string so that we can draw rectangles for the
4457 characters of the glyph string. */
4458 if (s->font == NULL)
4460 s->font_not_found_p = 1;
4461 s->font = FRAME_FONT (s->f);
4464 /* Adjust base line for subscript/superscript text. */
4465 s->ybase += s->first_glyph->voffset;
4467 xassert (s->face && s->face->gc);
4469 /* This glyph string must always be drawn with 16-bit functions. */
4470 s->two_byte_p = 1;
4472 return s->gidx + s->nchars;
4476 /* Fill glyph string S from a sequence of character glyphs.
4478 FACE_ID is the face id of the string. START is the index of the
4479 first glyph to consider, END is the index of the last + 1.
4480 OVERLAPS_P non-zero means S should draw the foreground only, and
4481 use its physical height for clipping.
4483 Value is the index of the first glyph not in S. */
4485 static int
4486 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4487 struct glyph_string *s;
4488 int face_id;
4489 int start, end, overlaps_p;
4491 struct glyph *glyph, *last;
4492 int voffset;
4493 int glyph_not_available_p;
4495 xassert (s->f == XFRAME (s->w->frame));
4496 xassert (s->nchars == 0);
4497 xassert (start >= 0 && end > start);
4499 s->for_overlaps_p = overlaps_p,
4500 glyph = s->row->glyphs[s->area] + start;
4501 last = s->row->glyphs[s->area] + end;
4502 voffset = glyph->voffset;
4504 glyph_not_available_p = glyph->glyph_not_available_p;
4506 while (glyph < last
4507 && glyph->type == CHAR_GLYPH
4508 && glyph->voffset == voffset
4509 /* Same face id implies same font, nowadays. */
4510 && glyph->face_id == face_id
4511 && glyph->glyph_not_available_p == glyph_not_available_p)
4513 int two_byte_p;
4515 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4516 s->char2b + s->nchars,
4517 &two_byte_p);
4518 s->two_byte_p = two_byte_p;
4519 ++s->nchars;
4520 xassert (s->nchars <= end - start);
4521 s->width += glyph->pixel_width;
4522 ++glyph;
4525 s->font = s->face->font;
4526 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4528 /* If the specified font could not be loaded, use the frame's font,
4529 but record the fact that we couldn't load it in
4530 S->font_not_found_p so that we can draw rectangles for the
4531 characters of the glyph string. */
4532 if (s->font == NULL || glyph_not_available_p)
4534 s->font_not_found_p = 1;
4535 s->font = FRAME_FONT (s->f);
4538 /* Adjust base line for subscript/superscript text. */
4539 s->ybase += voffset;
4541 xassert (s->face && s->face->gc);
4542 return glyph - s->row->glyphs[s->area];
4546 /* Fill glyph string S from image glyph S->first_glyph. */
4548 static void
4549 x_fill_image_glyph_string (s)
4550 struct glyph_string *s;
4552 xassert (s->first_glyph->type == IMAGE_GLYPH);
4553 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4554 xassert (s->img);
4555 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4556 s->font = s->face->font;
4557 s->width = s->first_glyph->pixel_width;
4559 /* Adjust base line for subscript/superscript text. */
4560 s->ybase += s->first_glyph->voffset;
4564 /* Fill glyph string S from a sequence of stretch glyphs.
4566 ROW is the glyph row in which the glyphs are found, AREA is the
4567 area within the row. START is the index of the first glyph to
4568 consider, END is the index of the last + 1.
4570 Value is the index of the first glyph not in S. */
4572 static int
4573 x_fill_stretch_glyph_string (s, row, area, start, end)
4574 struct glyph_string *s;
4575 struct glyph_row *row;
4576 enum glyph_row_area area;
4577 int start, end;
4579 struct glyph *glyph, *last;
4580 int voffset, face_id;
4582 xassert (s->first_glyph->type == STRETCH_GLYPH);
4584 glyph = s->row->glyphs[s->area] + start;
4585 last = s->row->glyphs[s->area] + end;
4586 face_id = glyph->face_id;
4587 s->face = FACE_FROM_ID (s->f, face_id);
4588 s->font = s->face->font;
4589 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4590 s->width = glyph->pixel_width;
4591 voffset = glyph->voffset;
4593 for (++glyph;
4594 (glyph < last
4595 && glyph->type == STRETCH_GLYPH
4596 && glyph->voffset == voffset
4597 && glyph->face_id == face_id);
4598 ++glyph)
4599 s->width += glyph->pixel_width;
4601 /* Adjust base line for subscript/superscript text. */
4602 s->ybase += voffset;
4604 /* The case that face->gc == 0 is handled when drawing the glyph
4605 string by calling PREPARE_FACE_FOR_DISPLAY. */
4606 xassert (s->face);
4607 return glyph - s->row->glyphs[s->area];
4611 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4612 of XChar2b structures for S; it can't be allocated in
4613 x_init_glyph_string because it must be allocated via `alloca'. W
4614 is the window on which S is drawn. ROW and AREA are the glyph row
4615 and area within the row from which S is constructed. START is the
4616 index of the first glyph structure covered by S. HL is a
4617 face-override for drawing S. */
4619 static void
4620 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4621 struct glyph_string *s;
4622 XChar2b *char2b;
4623 struct window *w;
4624 struct glyph_row *row;
4625 enum glyph_row_area area;
4626 int start;
4627 enum draw_glyphs_face hl;
4629 bzero (s, sizeof *s);
4630 s->w = w;
4631 s->f = XFRAME (w->frame);
4632 s->display = FRAME_X_DISPLAY (s->f);
4633 s->window = FRAME_X_WINDOW (s->f);
4634 s->char2b = char2b;
4635 s->hl = hl;
4636 s->row = row;
4637 s->area = area;
4638 s->first_glyph = row->glyphs[area] + start;
4639 s->height = row->height;
4640 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4642 /* Display the internal border below the tool-bar window. */
4643 if (s->w == XWINDOW (s->f->tool_bar_window))
4644 s->y -= s->f->output_data.x->internal_border_width;
4646 s->ybase = s->y + row->ascent;
4650 /* Set background width of glyph string S. START is the index of the
4651 first glyph following S. LAST_X is the right-most x-position + 1
4652 in the drawing area. */
4654 static INLINE void
4655 x_set_glyph_string_background_width (s, start, last_x)
4656 struct glyph_string *s;
4657 int start;
4658 int last_x;
4660 /* If the face of this glyph string has to be drawn to the end of
4661 the drawing area, set S->extends_to_end_of_line_p. */
4662 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4664 if (start == s->row->used[s->area]
4665 && s->area == TEXT_AREA
4666 && ((s->hl == DRAW_NORMAL_TEXT
4667 && (s->row->fill_line_p
4668 || s->face->background != default_face->background
4669 || s->face->stipple != default_face->stipple
4670 || s->row->mouse_face_p))
4671 || s->hl == DRAW_MOUSE_FACE
4672 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4673 && s->row->fill_line_p)))
4674 s->extends_to_end_of_line_p = 1;
4676 /* If S extends its face to the end of the line, set its
4677 background_width to the distance to the right edge of the drawing
4678 area. */
4679 if (s->extends_to_end_of_line_p)
4680 s->background_width = last_x - s->x + 1;
4681 else
4682 s->background_width = s->width;
4686 /* Add a glyph string for a stretch glyph to the list of strings
4687 between HEAD and TAIL. START is the index of the stretch glyph in
4688 row area AREA of glyph row ROW. END is the index of the last glyph
4689 in that glyph row area. X is the current output position assigned
4690 to the new glyph string constructed. HL overrides that face of the
4691 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4692 is the right-most x-position of the drawing area. */
4694 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4695 and below -- keep them on one line. */
4696 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4697 do \
4699 s = (struct glyph_string *) alloca (sizeof *s); \
4700 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4701 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4702 x_append_glyph_string (&HEAD, &TAIL, s); \
4703 s->x = (X); \
4705 while (0)
4708 /* Add a glyph string for an image glyph to the list of strings
4709 between HEAD and TAIL. START is the index of the image glyph in
4710 row area AREA of glyph row ROW. END is the index of the last glyph
4711 in that glyph row area. X is the current output position assigned
4712 to the new glyph string constructed. HL overrides that face of the
4713 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4714 is the right-most x-position of the drawing area. */
4716 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4717 do \
4719 s = (struct glyph_string *) alloca (sizeof *s); \
4720 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4721 x_fill_image_glyph_string (s); \
4722 x_append_glyph_string (&HEAD, &TAIL, s); \
4723 ++START; \
4724 s->x = (X); \
4726 while (0)
4729 /* Add a glyph string for a sequence of character glyphs to the list
4730 of strings between HEAD and TAIL. START is the index of the first
4731 glyph in row area AREA of glyph row ROW that is part of the new
4732 glyph string. END is the index of the last glyph in that glyph row
4733 area. X is the current output position assigned to the new glyph
4734 string constructed. HL overrides that face of the glyph; e.g. it
4735 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4736 right-most x-position of the drawing area. */
4738 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4739 do \
4741 int c, face_id; \
4742 XChar2b *char2b; \
4744 c = (ROW)->glyphs[AREA][START].u.ch; \
4745 face_id = (ROW)->glyphs[AREA][START].face_id; \
4747 s = (struct glyph_string *) alloca (sizeof *s); \
4748 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4749 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4750 x_append_glyph_string (&HEAD, &TAIL, s); \
4751 s->x = (X); \
4752 START = x_fill_glyph_string (s, face_id, START, END, \
4753 OVERLAPS_P); \
4755 while (0)
4758 /* Add a glyph string for a composite sequence to the list of strings
4759 between HEAD and TAIL. START is the index of the first glyph in
4760 row area AREA of glyph row ROW that is part of the new glyph
4761 string. END is the index of the last glyph in that glyph row area.
4762 X is the current output position assigned to the new glyph string
4763 constructed. HL overrides that face of the glyph; e.g. it is
4764 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4765 x-position of the drawing area. */
4767 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4768 do { \
4769 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4770 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4771 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4772 struct composition *cmp = composition_table[cmp_id]; \
4773 int glyph_len = cmp->glyph_len; \
4774 XChar2b *char2b; \
4775 struct face **faces; \
4776 struct glyph_string *first_s = NULL; \
4777 int n; \
4779 base_face = base_face->ascii_face; \
4780 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4781 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4782 /* At first, fill in `char2b' and `faces'. */ \
4783 for (n = 0; n < glyph_len; n++) \
4785 int c = COMPOSITION_GLYPH (cmp, n); \
4786 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4787 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4788 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4789 this_face_id, char2b + n, 1, 1); \
4792 /* Make glyph_strings for each glyph sequence that is drawable by \
4793 the same face, and append them to HEAD/TAIL. */ \
4794 for (n = 0; n < cmp->glyph_len;) \
4796 s = (struct glyph_string *) alloca (sizeof *s); \
4797 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4798 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4799 s->cmp = cmp; \
4800 s->gidx = n; \
4801 s->x = (X); \
4803 if (n == 0) \
4804 first_s = s; \
4806 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4809 ++START; \
4810 s = first_s; \
4811 } while (0)
4814 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4815 of AREA of glyph row ROW on window W between indices START and END.
4816 HL overrides the face for drawing glyph strings, e.g. it is
4817 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4818 x-positions of the drawing area.
4820 This is an ugly monster macro construct because we must use alloca
4821 to allocate glyph strings (because x_draw_glyphs can be called
4822 asynchronously). */
4824 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4825 do \
4827 HEAD = TAIL = NULL; \
4828 while (START < END) \
4830 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4831 switch (first_glyph->type) \
4833 case CHAR_GLYPH: \
4834 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4835 TAIL, HL, X, LAST_X, \
4836 OVERLAPS_P); \
4837 break; \
4839 case COMPOSITE_GLYPH: \
4840 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4841 HEAD, TAIL, HL, X, LAST_X,\
4842 OVERLAPS_P); \
4843 break; \
4845 case STRETCH_GLYPH: \
4846 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4847 HEAD, TAIL, HL, X, LAST_X); \
4848 break; \
4850 case IMAGE_GLYPH: \
4851 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4852 TAIL, HL, X, LAST_X); \
4853 break; \
4855 default: \
4856 abort (); \
4859 x_set_glyph_string_background_width (s, START, LAST_X); \
4860 (X) += s->width; \
4863 while (0)
4866 /* Draw glyphs between START and END in AREA of ROW on window W,
4867 starting at x-position X. X is relative to AREA in W. HL is a
4868 face-override with the following meaning:
4870 DRAW_NORMAL_TEXT draw normally
4871 DRAW_CURSOR draw in cursor face
4872 DRAW_MOUSE_FACE draw in mouse face.
4873 DRAW_INVERSE_VIDEO draw in mode line face
4874 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4875 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4877 If OVERLAPS_P is non-zero, draw only the foreground of characters
4878 and clip to the physical height of ROW.
4880 Value is the x-position reached, relative to AREA of W. */
4882 static int
4883 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
4884 struct window *w;
4885 int x;
4886 struct glyph_row *row;
4887 enum glyph_row_area area;
4888 int start, end;
4889 enum draw_glyphs_face hl;
4890 int overlaps_p;
4892 struct glyph_string *head, *tail;
4893 struct glyph_string *s;
4894 int last_x, area_width;
4895 int x_reached;
4896 int i, j;
4898 /* Let's rather be paranoid than getting a SEGV. */
4899 end = min (end, row->used[area]);
4900 start = max (0, start);
4901 start = min (end, start);
4903 /* Translate X to frame coordinates. Set last_x to the right
4904 end of the drawing area. */
4905 if (row->full_width_p)
4907 /* X is relative to the left edge of W, without scroll bars
4908 or fringes. */
4909 struct frame *f = XFRAME (w->frame);
4910 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4912 x += window_left_x;
4913 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4914 last_x = window_left_x + area_width;
4916 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4918 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4919 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4920 last_x += width;
4921 else
4922 x -= width;
4925 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4926 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
4928 else
4930 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4931 area_width = window_box_width (w, area);
4932 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4935 /* Build a doubly-linked list of glyph_string structures between
4936 head and tail from what we have to draw. Note that the macro
4937 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4938 the reason we use a separate variable `i'. */
4939 i = start;
4940 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4941 overlaps_p);
4942 if (tail)
4943 x_reached = tail->x + tail->background_width;
4944 else
4945 x_reached = x;
4947 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4948 the row, redraw some glyphs in front or following the glyph
4949 strings built above. */
4950 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4952 int dummy_x = 0;
4953 struct glyph_string *h, *t;
4955 /* Compute overhangs for all glyph strings. */
4956 for (s = head; s; s = s->next)
4957 x_compute_glyph_string_overhangs (s);
4959 /* Prepend glyph strings for glyphs in front of the first glyph
4960 string that are overwritten because of the first glyph
4961 string's left overhang. The background of all strings
4962 prepended must be drawn because the first glyph string
4963 draws over it. */
4964 i = x_left_overwritten (head);
4965 if (i >= 0)
4967 j = i;
4968 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4969 DRAW_NORMAL_TEXT, dummy_x, last_x,
4970 overlaps_p);
4971 start = i;
4972 x_compute_overhangs_and_x (t, head->x, 1);
4973 x_prepend_glyph_string_lists (&head, &tail, h, t);
4976 /* Prepend glyph strings for glyphs in front of the first glyph
4977 string that overwrite that glyph string because of their
4978 right overhang. For these strings, only the foreground must
4979 be drawn, because it draws over the glyph string at `head'.
4980 The background must not be drawn because this would overwrite
4981 right overhangs of preceding glyphs for which no glyph
4982 strings exist. */
4983 i = x_left_overwriting (head);
4984 if (i >= 0)
4986 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4987 DRAW_NORMAL_TEXT, dummy_x, last_x,
4988 overlaps_p);
4989 for (s = h; s; s = s->next)
4990 s->background_filled_p = 1;
4991 x_compute_overhangs_and_x (t, head->x, 1);
4992 x_prepend_glyph_string_lists (&head, &tail, h, t);
4995 /* Append glyphs strings for glyphs following the last glyph
4996 string tail that are overwritten by tail. The background of
4997 these strings has to be drawn because tail's foreground draws
4998 over it. */
4999 i = x_right_overwritten (tail);
5000 if (i >= 0)
5002 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5003 DRAW_NORMAL_TEXT, x, last_x,
5004 overlaps_p);
5005 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5006 x_append_glyph_string_lists (&head, &tail, h, t);
5009 /* Append glyph strings for glyphs following the last glyph
5010 string tail that overwrite tail. The foreground of such
5011 glyphs has to be drawn because it writes into the background
5012 of tail. The background must not be drawn because it could
5013 paint over the foreground of following glyphs. */
5014 i = x_right_overwriting (tail);
5015 if (i >= 0)
5017 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5018 DRAW_NORMAL_TEXT, x, last_x,
5019 overlaps_p);
5020 for (s = h; s; s = s->next)
5021 s->background_filled_p = 1;
5022 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5023 x_append_glyph_string_lists (&head, &tail, h, t);
5027 /* Draw all strings. */
5028 for (s = head; s; s = s->next)
5029 x_draw_glyph_string (s);
5031 if (area == TEXT_AREA
5032 && !row->full_width_p
5033 /* When drawing overlapping rows, only the glyph strings'
5034 foreground is drawn, which doesn't erase a cursor
5035 completely. */
5036 && !overlaps_p)
5038 int x0 = head ? head->x : x;
5039 int x1 = tail ? tail->x + tail->background_width : x;
5041 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5042 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5044 if (XFASTINT (w->left_margin_width) != 0)
5046 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5047 x0 -= left_area_width;
5048 x1 -= left_area_width;
5051 notice_overwritten_cursor (w, area, x0, x1,
5052 row->y, MATRIX_ROW_BOTTOM_Y (row));
5055 /* Value is the x-position up to which drawn, relative to AREA of W.
5056 This doesn't include parts drawn because of overhangs. */
5057 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5058 if (!row->full_width_p)
5060 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5061 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5062 if (area > TEXT_AREA)
5063 x_reached -= window_box_width (w, TEXT_AREA);
5066 return x_reached;
5070 /* Fix the display of area AREA of overlapping row ROW in window W. */
5072 static void
5073 x_fix_overlapping_area (w, row, area)
5074 struct window *w;
5075 struct glyph_row *row;
5076 enum glyph_row_area area;
5078 int i, x;
5080 BLOCK_INPUT;
5082 if (area == LEFT_MARGIN_AREA)
5083 x = 0;
5084 else if (area == TEXT_AREA)
5085 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5086 else
5087 x = (window_box_width (w, LEFT_MARGIN_AREA)
5088 + window_box_width (w, TEXT_AREA));
5090 for (i = 0; i < row->used[area];)
5092 if (row->glyphs[area][i].overlaps_vertically_p)
5094 int start = i, start_x = x;
5098 x += row->glyphs[area][i].pixel_width;
5099 ++i;
5101 while (i < row->used[area]
5102 && row->glyphs[area][i].overlaps_vertically_p);
5104 x_draw_glyphs (w, start_x, row, area, start, i,
5105 DRAW_NORMAL_TEXT, 1);
5107 else
5109 x += row->glyphs[area][i].pixel_width;
5110 ++i;
5114 UNBLOCK_INPUT;
5118 /* Output LEN glyphs starting at START at the nominal cursor position.
5119 Advance the nominal cursor over the text. The global variable
5120 updated_window contains the window being updated, updated_row is
5121 the glyph row being updated, and updated_area is the area of that
5122 row being updated. */
5124 static void
5125 x_write_glyphs (start, len)
5126 struct glyph *start;
5127 int len;
5129 int x, hpos;
5131 xassert (updated_window && updated_row);
5132 BLOCK_INPUT;
5134 /* Write glyphs. */
5136 hpos = start - updated_row->glyphs[updated_area];
5137 x = x_draw_glyphs (updated_window, output_cursor.x,
5138 updated_row, updated_area,
5139 hpos, hpos + len,
5140 DRAW_NORMAL_TEXT, 0);
5142 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
5143 if (updated_area == TEXT_AREA
5144 && updated_window->phys_cursor_on_p
5145 && updated_window->phys_cursor.vpos == output_cursor.vpos
5146 && updated_window->phys_cursor.hpos >= hpos
5147 && updated_window->phys_cursor.hpos < hpos + len)
5148 updated_window->phys_cursor_on_p = 0;
5150 UNBLOCK_INPUT;
5152 /* Advance the output cursor. */
5153 output_cursor.hpos += len;
5154 output_cursor.x = x;
5158 /* Insert LEN glyphs from START at the nominal cursor position. */
5160 static void
5161 x_insert_glyphs (start, len)
5162 struct glyph *start;
5163 register int len;
5165 struct frame *f;
5166 struct window *w;
5167 int line_height, shift_by_width, shifted_region_width;
5168 struct glyph_row *row;
5169 struct glyph *glyph;
5170 int frame_x, frame_y, hpos;
5172 xassert (updated_window && updated_row);
5173 BLOCK_INPUT;
5174 w = updated_window;
5175 f = XFRAME (WINDOW_FRAME (w));
5177 /* Get the height of the line we are in. */
5178 row = updated_row;
5179 line_height = row->height;
5181 /* Get the width of the glyphs to insert. */
5182 shift_by_width = 0;
5183 for (glyph = start; glyph < start + len; ++glyph)
5184 shift_by_width += glyph->pixel_width;
5186 /* Get the width of the region to shift right. */
5187 shifted_region_width = (window_box_width (w, updated_area)
5188 - output_cursor.x
5189 - shift_by_width);
5191 /* Shift right. */
5192 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5193 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5194 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5195 f->output_data.x->normal_gc,
5196 frame_x, frame_y,
5197 shifted_region_width, line_height,
5198 frame_x + shift_by_width, frame_y);
5200 /* Write the glyphs. */
5201 hpos = start - row->glyphs[updated_area];
5202 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5203 DRAW_NORMAL_TEXT, 0);
5205 /* Advance the output cursor. */
5206 output_cursor.hpos += len;
5207 output_cursor.x += shift_by_width;
5208 UNBLOCK_INPUT;
5212 /* Delete N glyphs at the nominal cursor position. Not implemented
5213 for X frames. */
5215 static void
5216 x_delete_glyphs (n)
5217 register int n;
5219 abort ();
5223 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5224 If they are <= 0, this is probably an error. */
5226 void
5227 x_clear_area (dpy, window, x, y, width, height, exposures)
5228 Display *dpy;
5229 Window window;
5230 int x, y;
5231 int width, height;
5232 int exposures;
5234 xassert (width > 0 && height > 0);
5235 XClearArea (dpy, window, x, y, width, height, exposures);
5239 /* Erase the current text line from the nominal cursor position
5240 (inclusive) to pixel column TO_X (exclusive). The idea is that
5241 everything from TO_X onward is already erased.
5243 TO_X is a pixel position relative to updated_area of
5244 updated_window. TO_X == -1 means clear to the end of this area. */
5246 static void
5247 x_clear_end_of_line (to_x)
5248 int to_x;
5250 struct frame *f;
5251 struct window *w = updated_window;
5252 int max_x, min_y, max_y;
5253 int from_x, from_y, to_y;
5255 xassert (updated_window && updated_row);
5256 f = XFRAME (w->frame);
5258 if (updated_row->full_width_p)
5260 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5261 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5262 && !w->pseudo_window_p)
5263 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5265 else
5266 max_x = window_box_width (w, updated_area);
5267 max_y = window_text_bottom_y (w);
5269 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5270 of window. For TO_X > 0, truncate to end of drawing area. */
5271 if (to_x == 0)
5272 return;
5273 else if (to_x < 0)
5274 to_x = max_x;
5275 else
5276 to_x = min (to_x, max_x);
5278 to_y = min (max_y, output_cursor.y + updated_row->height);
5280 /* Notice if the cursor will be cleared by this operation. */
5281 if (!updated_row->full_width_p)
5282 notice_overwritten_cursor (w, updated_area,
5283 output_cursor.x, -1,
5284 updated_row->y,
5285 MATRIX_ROW_BOTTOM_Y (updated_row));
5287 from_x = output_cursor.x;
5289 /* Translate to frame coordinates. */
5290 if (updated_row->full_width_p)
5292 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5293 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5295 else
5297 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5298 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5301 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5302 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5303 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5305 /* Prevent inadvertently clearing to end of the X window. */
5306 if (to_x > from_x && to_y > from_y)
5308 BLOCK_INPUT;
5309 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5310 from_x, from_y, to_x - from_x, to_y - from_y,
5311 False);
5312 UNBLOCK_INPUT;
5317 /* Clear entire frame. If updating_frame is non-null, clear that
5318 frame. Otherwise clear the selected frame. */
5320 static void
5321 x_clear_frame ()
5323 struct frame *f;
5325 if (updating_frame)
5326 f = updating_frame;
5327 else
5328 f = SELECTED_FRAME ();
5330 /* Clearing the frame will erase any cursor, so mark them all as no
5331 longer visible. */
5332 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5333 output_cursor.hpos = output_cursor.vpos = 0;
5334 output_cursor.x = -1;
5336 /* We don't set the output cursor here because there will always
5337 follow an explicit cursor_to. */
5338 BLOCK_INPUT;
5339 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5341 /* We have to clear the scroll bars, too. If we have changed
5342 colors or something like that, then they should be notified. */
5343 x_scroll_bar_clear (f);
5345 XFlush (FRAME_X_DISPLAY (f));
5346 UNBLOCK_INPUT;
5351 /* Invert the middle quarter of the frame for .15 sec. */
5353 /* We use the select system call to do the waiting, so we have to make
5354 sure it's available. If it isn't, we just won't do visual bells. */
5356 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5359 /* Subtract the `struct timeval' values X and Y, storing the result in
5360 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5362 static int
5363 timeval_subtract (result, x, y)
5364 struct timeval *result, x, y;
5366 /* Perform the carry for the later subtraction by updating y. This
5367 is safer because on some systems the tv_sec member is unsigned. */
5368 if (x.tv_usec < y.tv_usec)
5370 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5371 y.tv_usec -= 1000000 * nsec;
5372 y.tv_sec += nsec;
5375 if (x.tv_usec - y.tv_usec > 1000000)
5377 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5378 y.tv_usec += 1000000 * nsec;
5379 y.tv_sec -= nsec;
5382 /* Compute the time remaining to wait. tv_usec is certainly
5383 positive. */
5384 result->tv_sec = x.tv_sec - y.tv_sec;
5385 result->tv_usec = x.tv_usec - y.tv_usec;
5387 /* Return indication of whether the result should be considered
5388 negative. */
5389 return x.tv_sec < y.tv_sec;
5392 void
5393 XTflash (f)
5394 struct frame *f;
5396 BLOCK_INPUT;
5399 GC gc;
5401 /* Create a GC that will use the GXxor function to flip foreground
5402 pixels into background pixels. */
5404 XGCValues values;
5406 values.function = GXxor;
5407 values.foreground = (f->output_data.x->foreground_pixel
5408 ^ f->output_data.x->background_pixel);
5410 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5411 GCFunction | GCForeground, &values);
5415 /* Get the height not including a menu bar widget. */
5416 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5417 /* Height of each line to flash. */
5418 int flash_height = FRAME_LINE_HEIGHT (f);
5419 /* These will be the left and right margins of the rectangles. */
5420 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5421 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5423 int width;
5425 /* Don't flash the area between a scroll bar and the frame
5426 edge it is next to. */
5427 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5429 case vertical_scroll_bar_left:
5430 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5431 break;
5433 case vertical_scroll_bar_right:
5434 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5435 break;
5437 default:
5438 break;
5441 width = flash_right - flash_left;
5443 /* If window is tall, flash top and bottom line. */
5444 if (height > 3 * FRAME_LINE_HEIGHT (f))
5446 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5447 flash_left,
5448 (FRAME_INTERNAL_BORDER_WIDTH (f)
5449 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5450 width, flash_height);
5451 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5452 flash_left,
5453 (height - flash_height
5454 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5455 width, flash_height);
5457 else
5458 /* If it is short, flash it all. */
5459 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5460 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5461 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5463 x_flush (f);
5466 struct timeval wakeup;
5468 EMACS_GET_TIME (wakeup);
5470 /* Compute time to wait until, propagating carry from usecs. */
5471 wakeup.tv_usec += 150000;
5472 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5473 wakeup.tv_usec %= 1000000;
5475 /* Keep waiting until past the time wakeup or any input gets
5476 available. */
5477 while (! detect_input_pending ())
5479 struct timeval current;
5480 struct timeval timeout;
5482 EMACS_GET_TIME (current);
5484 /* Break if result would be negative. */
5485 if (timeval_subtract (&current, wakeup, current))
5486 break;
5488 /* How long `select' should wait. */
5489 timeout.tv_sec = 0;
5490 timeout.tv_usec = 10000;
5492 /* Try to wait that long--but we might wake up sooner. */
5493 select (0, NULL, NULL, NULL, &timeout);
5497 /* If window is tall, flash top and bottom line. */
5498 if (height > 3 * FRAME_LINE_HEIGHT (f))
5500 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5501 flash_left,
5502 (FRAME_INTERNAL_BORDER_WIDTH (f)
5503 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5504 width, flash_height);
5505 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5506 flash_left,
5507 (height - flash_height
5508 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5509 width, flash_height);
5511 else
5512 /* If it is short, flash it all. */
5513 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5514 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5515 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5517 XFreeGC (FRAME_X_DISPLAY (f), gc);
5518 x_flush (f);
5522 UNBLOCK_INPUT;
5525 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5528 /* Make audible bell. */
5530 void
5531 XTring_bell ()
5533 struct frame *f = SELECTED_FRAME ();
5535 if (FRAME_X_DISPLAY (f))
5537 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5538 if (visible_bell)
5539 XTflash (f);
5540 else
5541 #endif
5543 BLOCK_INPUT;
5544 XBell (FRAME_X_DISPLAY (f), 0);
5545 XFlush (FRAME_X_DISPLAY (f));
5546 UNBLOCK_INPUT;
5552 /* Specify how many text lines, from the top of the window,
5553 should be affected by insert-lines and delete-lines operations.
5554 This, and those operations, are used only within an update
5555 that is bounded by calls to x_update_begin and x_update_end. */
5557 static void
5558 XTset_terminal_window (n)
5559 register int n;
5561 /* This function intentionally left blank. */
5566 /***********************************************************************
5567 Line Dance
5568 ***********************************************************************/
5570 /* Perform an insert-lines or delete-lines operation, inserting N
5571 lines or deleting -N lines at vertical position VPOS. */
5573 static void
5574 x_ins_del_lines (vpos, n)
5575 int vpos, n;
5577 abort ();
5581 /* Scroll part of the display as described by RUN. */
5583 static void
5584 x_scroll_run (w, run)
5585 struct window *w;
5586 struct run *run;
5588 struct frame *f = XFRAME (w->frame);
5589 int x, y, width, height, from_y, to_y, bottom_y;
5591 /* Get frame-relative bounding box of the text display area of W,
5592 without mode lines. Include in this box the left and right
5593 fringe of W. */
5594 window_box (w, -1, &x, &y, &width, &height);
5595 width += FRAME_X_FRINGE_WIDTH (f);
5596 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5598 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5599 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5600 bottom_y = y + height;
5602 if (to_y < from_y)
5604 /* Scrolling up. Make sure we don't copy part of the mode
5605 line at the bottom. */
5606 if (from_y + run->height > bottom_y)
5607 height = bottom_y - from_y;
5608 else
5609 height = run->height;
5611 else
5613 /* Scolling down. Make sure we don't copy over the mode line.
5614 at the bottom. */
5615 if (to_y + run->height > bottom_y)
5616 height = bottom_y - to_y;
5617 else
5618 height = run->height;
5621 BLOCK_INPUT;
5623 /* Cursor off. Will be switched on again in x_update_window_end. */
5624 updated_window = w;
5625 x_clear_cursor (w);
5627 XCopyArea (FRAME_X_DISPLAY (f),
5628 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5629 f->output_data.x->normal_gc,
5630 x, from_y,
5631 width, height,
5632 x, to_y);
5634 UNBLOCK_INPUT;
5639 /***********************************************************************
5640 Exposure Events
5641 ***********************************************************************/
5643 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5644 corner of the exposed rectangle. W and H are width and height of
5645 the exposed area. All are pixel values. W or H zero means redraw
5646 the entire frame. */
5648 static void
5649 expose_frame (f, x, y, w, h)
5650 struct frame *f;
5651 int x, y, w, h;
5653 XRectangle r;
5654 int mouse_face_overwritten_p = 0;
5656 TRACE ((stderr, "expose_frame "));
5658 /* No need to redraw if frame will be redrawn soon. */
5659 if (FRAME_GARBAGED_P (f))
5661 TRACE ((stderr, " garbaged\n"));
5662 return;
5665 /* If basic faces haven't been realized yet, there is no point in
5666 trying to redraw anything. This can happen when we get an expose
5667 event while Emacs is starting, e.g. by moving another window. */
5668 if (FRAME_FACE_CACHE (f) == NULL
5669 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5671 TRACE ((stderr, " no faces\n"));
5672 return;
5675 if (w == 0 || h == 0)
5677 r.x = r.y = 0;
5678 r.width = CANON_X_UNIT (f) * f->width;
5679 r.height = CANON_Y_UNIT (f) * f->height;
5681 else
5683 r.x = x;
5684 r.y = y;
5685 r.width = w;
5686 r.height = h;
5689 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5690 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5692 if (WINDOWP (f->tool_bar_window))
5693 mouse_face_overwritten_p
5694 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5696 #ifndef USE_X_TOOLKIT
5697 if (WINDOWP (f->menu_bar_window))
5698 mouse_face_overwritten_p
5699 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5700 #endif /* not USE_X_TOOLKIT */
5702 /* Some window managers support a focus-follows-mouse style with
5703 delayed raising of frames. Imagine a partially obscured frame,
5704 and moving the mouse into partially obscured mouse-face on that
5705 frame. The visible part of the mouse-face will be highlighted,
5706 then the WM raises the obscured frame. With at least one WM, KDE
5707 2.1, Emacs is not getting any event for the raising of the frame
5708 (even tried with SubstructureRedirectMask), only Expose events.
5709 These expose events will draw text normally, i.e. not
5710 highlighted. Which means we must redo the highlight here.
5711 Subsume it under ``we love X''. --gerd 2001-08-15 */
5712 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5714 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5715 if (f == dpyinfo->mouse_face_mouse_frame)
5717 int x = dpyinfo->mouse_face_mouse_x;
5718 int y = dpyinfo->mouse_face_mouse_y;
5719 clear_mouse_face (dpyinfo);
5720 note_mouse_highlight (f, x, y);
5726 /* Redraw (parts) of all windows in the window tree rooted at W that
5727 intersect R. R contains frame pixel coordinates. Value is
5728 non-zero if the exposure overwrites mouse-face. */
5730 static int
5731 expose_window_tree (w, r)
5732 struct window *w;
5733 XRectangle *r;
5735 struct frame *f = XFRAME (w->frame);
5736 int mouse_face_overwritten_p = 0;
5738 while (w && !FRAME_GARBAGED_P (f))
5740 if (!NILP (w->hchild))
5741 mouse_face_overwritten_p
5742 |= expose_window_tree (XWINDOW (w->hchild), r);
5743 else if (!NILP (w->vchild))
5744 mouse_face_overwritten_p
5745 |= expose_window_tree (XWINDOW (w->vchild), r);
5746 else
5747 mouse_face_overwritten_p |= expose_window (w, r);
5749 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5752 return mouse_face_overwritten_p;
5756 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5757 which intersects rectangle R. R is in window-relative coordinates. */
5759 static void
5760 expose_area (w, row, r, area)
5761 struct window *w;
5762 struct glyph_row *row;
5763 XRectangle *r;
5764 enum glyph_row_area area;
5766 struct glyph *first = row->glyphs[area];
5767 struct glyph *end = row->glyphs[area] + row->used[area];
5768 struct glyph *last;
5769 int first_x, start_x, x;
5771 if (area == TEXT_AREA && row->fill_line_p)
5772 /* If row extends face to end of line write the whole line. */
5773 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5774 DRAW_NORMAL_TEXT, 0);
5775 else
5777 /* Set START_X to the window-relative start position for drawing glyphs of
5778 AREA. The first glyph of the text area can be partially visible.
5779 The first glyphs of other areas cannot. */
5780 if (area == LEFT_MARGIN_AREA)
5781 start_x = 0;
5782 else if (area == TEXT_AREA)
5783 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5784 else
5785 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5786 + window_box_width (w, TEXT_AREA));
5787 x = start_x;
5789 /* Find the first glyph that must be redrawn. */
5790 while (first < end
5791 && x + first->pixel_width < r->x)
5793 x += first->pixel_width;
5794 ++first;
5797 /* Find the last one. */
5798 last = first;
5799 first_x = x;
5800 while (last < end
5801 && x < r->x + r->width)
5803 x += last->pixel_width;
5804 ++last;
5807 /* Repaint. */
5808 if (last > first)
5809 x_draw_glyphs (w, first_x - start_x, row, area,
5810 first - row->glyphs[area],
5811 last - row->glyphs[area],
5812 DRAW_NORMAL_TEXT, 0);
5817 /* Redraw the parts of the glyph row ROW on window W intersecting
5818 rectangle R. R is in window-relative coordinates. Value is
5819 non-zero if mouse-face was overwritten. */
5821 static int
5822 expose_line (w, row, r)
5823 struct window *w;
5824 struct glyph_row *row;
5825 XRectangle *r;
5827 xassert (row->enabled_p);
5829 if (row->mode_line_p || w->pseudo_window_p)
5830 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5831 DRAW_NORMAL_TEXT, 0);
5832 else
5834 if (row->used[LEFT_MARGIN_AREA])
5835 expose_area (w, row, r, LEFT_MARGIN_AREA);
5836 if (row->used[TEXT_AREA])
5837 expose_area (w, row, r, TEXT_AREA);
5838 if (row->used[RIGHT_MARGIN_AREA])
5839 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5840 draw_row_fringe_bitmaps (w, row);
5843 return row->mouse_face_p;
5847 /* Return non-zero if W's cursor intersects rectangle R. */
5849 static int
5850 x_phys_cursor_in_rect_p (w, r)
5851 struct window *w;
5852 XRectangle *r;
5854 XRectangle cr, result;
5855 struct glyph *cursor_glyph;
5857 cursor_glyph = get_phys_cursor_glyph (w);
5858 if (cursor_glyph)
5860 cr.x = w->phys_cursor.x;
5861 cr.y = w->phys_cursor.y;
5862 cr.width = cursor_glyph->pixel_width;
5863 cr.height = w->phys_cursor_height;
5864 return x_intersect_rectangles (&cr, r, &result);
5866 else
5867 return 0;
5871 /* Redraw those parts of glyphs rows during expose event handling that
5872 overlap other rows. Redrawing of an exposed line writes over parts
5873 of lines overlapping that exposed line; this function fixes that.
5875 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
5876 row in W's current matrix that is exposed and overlaps other rows.
5877 LAST_OVERLAPPING_ROW is the last such row. */
5879 static void
5880 expose_overlaps (w, first_overlapping_row, last_overlapping_row)
5881 struct window *w;
5882 struct glyph_row *first_overlapping_row;
5883 struct glyph_row *last_overlapping_row;
5885 struct glyph_row *row;
5887 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
5888 if (row->overlapping_p)
5890 xassert (row->enabled_p && !row->mode_line_p);
5892 if (row->used[LEFT_MARGIN_AREA])
5893 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA);
5895 if (row->used[TEXT_AREA])
5896 x_fix_overlapping_area (w, row, TEXT_AREA);
5898 if (row->used[RIGHT_MARGIN_AREA])
5899 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA);
5904 /* Redraw the part of window W intersection rectangle FR. Pixel
5905 coordinates in FR are frame-relative. Call this function with
5906 input blocked. Value is non-zero if the exposure overwrites
5907 mouse-face. */
5909 static int
5910 expose_window (w, fr)
5911 struct window *w;
5912 XRectangle *fr;
5914 struct frame *f = XFRAME (w->frame);
5915 XRectangle wr, r;
5916 int mouse_face_overwritten_p = 0;
5918 /* If window is not yet fully initialized, do nothing. This can
5919 happen when toolkit scroll bars are used and a window is split.
5920 Reconfiguring the scroll bar will generate an expose for a newly
5921 created window. */
5922 if (w->current_matrix == NULL)
5923 return 0;
5925 /* When we're currently updating the window, display and current
5926 matrix usually don't agree. Arrange for a thorough display
5927 later. */
5928 if (w == updated_window)
5930 SET_FRAME_GARBAGED (f);
5931 return 0;
5934 /* Frame-relative pixel rectangle of W. */
5935 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
5936 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
5937 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
5938 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
5940 if (x_intersect_rectangles (fr, &wr, &r))
5942 int yb = window_text_bottom_y (w);
5943 struct glyph_row *row;
5944 int cursor_cleared_p;
5945 struct glyph_row *first_overlapping_row, *last_overlapping_row;
5947 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5948 r.x, r.y, r.width, r.height));
5950 /* Convert to window coordinates. */
5951 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
5952 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
5954 /* Turn off the cursor. */
5955 if (!w->pseudo_window_p
5956 && x_phys_cursor_in_rect_p (w, &r))
5958 x_clear_cursor (w);
5959 cursor_cleared_p = 1;
5961 else
5962 cursor_cleared_p = 0;
5964 /* Update lines intersecting rectangle R. */
5965 first_overlapping_row = last_overlapping_row = NULL;
5966 for (row = w->current_matrix->rows;
5967 row->enabled_p;
5968 ++row)
5970 int y0 = row->y;
5971 int y1 = MATRIX_ROW_BOTTOM_Y (row);
5973 if ((y0 >= r.y && y0 < r.y + r.height)
5974 || (y1 > r.y && y1 < r.y + r.height)
5975 || (r.y >= y0 && r.y < y1)
5976 || (r.y + r.height > y0 && r.y + r.height < y1))
5978 if (row->overlapping_p)
5980 if (first_overlapping_row == NULL)
5981 first_overlapping_row = row;
5982 last_overlapping_row = row;
5985 if (expose_line (w, row, &r))
5986 mouse_face_overwritten_p = 1;
5989 if (y1 >= yb)
5990 break;
5993 /* Display the mode line if there is one. */
5994 if (WINDOW_WANTS_MODELINE_P (w)
5995 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5996 row->enabled_p)
5997 && row->y < r.y + r.height)
5999 if (expose_line (w, row, &r))
6000 mouse_face_overwritten_p = 1;
6003 if (!w->pseudo_window_p)
6005 /* Fix the display of overlapping rows. */
6006 if (first_overlapping_row)
6007 expose_overlaps (w, first_overlapping_row, last_overlapping_row);
6009 /* Draw border between windows. */
6010 x_draw_vertical_border (w);
6012 /* Turn the cursor on again. */
6013 if (cursor_cleared_p)
6014 x_update_window_cursor (w, 1);
6018 return mouse_face_overwritten_p;
6022 /* Determine the intersection of two rectangles R1 and R2. Return
6023 the intersection in *RESULT. Value is non-zero if RESULT is not
6024 empty. */
6026 static int
6027 x_intersect_rectangles (r1, r2, result)
6028 XRectangle *r1, *r2, *result;
6030 XRectangle *left, *right;
6031 XRectangle *upper, *lower;
6032 int intersection_p = 0;
6034 /* Rearrange so that R1 is the left-most rectangle. */
6035 if (r1->x < r2->x)
6036 left = r1, right = r2;
6037 else
6038 left = r2, right = r1;
6040 /* X0 of the intersection is right.x0, if this is inside R1,
6041 otherwise there is no intersection. */
6042 if (right->x <= left->x + left->width)
6044 result->x = right->x;
6046 /* The right end of the intersection is the minimum of the
6047 the right ends of left and right. */
6048 result->width = (min (left->x + left->width, right->x + right->width)
6049 - result->x);
6051 /* Same game for Y. */
6052 if (r1->y < r2->y)
6053 upper = r1, lower = r2;
6054 else
6055 upper = r2, lower = r1;
6057 /* The upper end of the intersection is lower.y0, if this is inside
6058 of upper. Otherwise, there is no intersection. */
6059 if (lower->y <= upper->y + upper->height)
6061 result->y = lower->y;
6063 /* The lower end of the intersection is the minimum of the lower
6064 ends of upper and lower. */
6065 result->height = (min (lower->y + lower->height,
6066 upper->y + upper->height)
6067 - result->y);
6068 intersection_p = 1;
6072 return intersection_p;
6079 static void
6080 frame_highlight (f)
6081 struct frame *f;
6083 /* We used to only do this if Vx_no_window_manager was non-nil, but
6084 the ICCCM (section 4.1.6) says that the window's border pixmap
6085 and border pixel are window attributes which are "private to the
6086 client", so we can always change it to whatever we want. */
6087 BLOCK_INPUT;
6088 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6089 f->output_data.x->border_pixel);
6090 UNBLOCK_INPUT;
6091 x_update_cursor (f, 1);
6094 static void
6095 frame_unhighlight (f)
6096 struct frame *f;
6098 /* We used to only do this if Vx_no_window_manager was non-nil, but
6099 the ICCCM (section 4.1.6) says that the window's border pixmap
6100 and border pixel are window attributes which are "private to the
6101 client", so we can always change it to whatever we want. */
6102 BLOCK_INPUT;
6103 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6104 f->output_data.x->border_tile);
6105 UNBLOCK_INPUT;
6106 x_update_cursor (f, 1);
6109 /* The focus has changed. Update the frames as necessary to reflect
6110 the new situation. Note that we can't change the selected frame
6111 here, because the Lisp code we are interrupting might become confused.
6112 Each event gets marked with the frame in which it occurred, so the
6113 Lisp code can tell when the switch took place by examining the events. */
6115 static void
6116 x_new_focus_frame (dpyinfo, frame)
6117 struct x_display_info *dpyinfo;
6118 struct frame *frame;
6120 struct frame *old_focus = dpyinfo->x_focus_frame;
6122 if (frame != dpyinfo->x_focus_frame)
6124 /* Set this before calling other routines, so that they see
6125 the correct value of x_focus_frame. */
6126 dpyinfo->x_focus_frame = frame;
6128 if (old_focus && old_focus->auto_lower)
6129 x_lower_frame (old_focus);
6131 #if 0
6132 selected_frame = frame;
6133 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6134 selected_frame);
6135 Fselect_window (selected_frame->selected_window);
6136 choose_minibuf_frame ();
6137 #endif /* ! 0 */
6139 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6140 pending_autoraise_frame = dpyinfo->x_focus_frame;
6141 else
6142 pending_autoraise_frame = 0;
6145 x_frame_rehighlight (dpyinfo);
6148 /* Handle FocusIn and FocusOut state changes for FRAME.
6149 If FRAME has focus and there exists more than one frame, puts
6150 a FOCUS_IN_EVENT into BUFP.
6151 Returns number of events inserted into BUFP. */
6153 static int
6154 x_focus_changed (type, state, dpyinfo, frame, bufp, numchars)
6155 int type;
6156 int state;
6157 struct x_display_info *dpyinfo;
6158 struct frame *frame;
6159 struct input_event *bufp;
6160 int numchars;
6162 int nr_events = 0;
6164 if (type == FocusIn)
6166 if (dpyinfo->x_focus_event_frame != frame)
6168 x_new_focus_frame (dpyinfo, frame);
6169 dpyinfo->x_focus_event_frame = frame;
6171 /* Don't stop displaying the initial startup message
6172 for a switch-frame event we don't need. */
6173 if (numchars > 0
6174 && GC_NILP (Vterminal_frame)
6175 && GC_CONSP (Vframe_list)
6176 && !GC_NILP (XCDR (Vframe_list)))
6178 bufp->kind = FOCUS_IN_EVENT;
6179 XSETFRAME (bufp->frame_or_window, frame);
6180 bufp->arg = Qnil;
6181 ++bufp;
6182 numchars--;
6183 ++nr_events;
6187 frame->output_data.x->focus_state |= state;
6189 #ifdef HAVE_X_I18N
6190 if (FRAME_XIC (frame))
6191 XSetICFocus (FRAME_XIC (frame));
6192 #endif
6194 else if (type == FocusOut)
6196 frame->output_data.x->focus_state &= ~state;
6198 if (dpyinfo->x_focus_event_frame == frame)
6200 dpyinfo->x_focus_event_frame = 0;
6201 x_new_focus_frame (dpyinfo, 0);
6204 #ifdef HAVE_X_I18N
6205 if (FRAME_XIC (frame))
6206 XUnsetICFocus (FRAME_XIC (frame));
6207 #endif
6210 return nr_events;
6213 /* The focus may have changed. Figure out if it is a real focus change,
6214 by checking both FocusIn/Out and Enter/LeaveNotify events.
6216 Returns number of events inserted into BUFP. */
6218 static int
6219 x_detect_focus_change (dpyinfo, event, bufp, numchars)
6220 struct x_display_info *dpyinfo;
6221 XEvent *event;
6222 struct input_event *bufp;
6223 int numchars;
6225 struct frame *frame;
6226 int nr_events = 0;
6228 frame = x_any_window_to_frame (dpyinfo, event->xany.window);
6229 if (! frame) return nr_events;
6231 switch (event->type)
6233 case EnterNotify:
6234 case LeaveNotify:
6235 if (event->xcrossing.detail != NotifyInferior
6236 && event->xcrossing.focus
6237 && ! (frame->output_data.x->focus_state & FOCUS_EXPLICIT))
6238 nr_events = x_focus_changed ((event->type == EnterNotify
6239 ? FocusIn : FocusOut),
6240 FOCUS_IMPLICIT,
6241 dpyinfo,
6242 frame,
6243 bufp,
6244 numchars);
6245 break;
6247 case FocusIn:
6248 case FocusOut:
6249 nr_events = x_focus_changed (event->type,
6250 (event->xfocus.detail == NotifyPointer
6251 ? FOCUS_IMPLICIT : FOCUS_EXPLICIT),
6252 dpyinfo,
6253 frame,
6254 bufp,
6255 numchars);
6256 break;
6259 return nr_events;
6263 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6265 void
6266 x_mouse_leave (dpyinfo)
6267 struct x_display_info *dpyinfo;
6269 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6272 /* The focus has changed, or we have redirected a frame's focus to
6273 another frame (this happens when a frame uses a surrogate
6274 mini-buffer frame). Shift the highlight as appropriate.
6276 The FRAME argument doesn't necessarily have anything to do with which
6277 frame is being highlighted or un-highlighted; we only use it to find
6278 the appropriate X display info. */
6280 static void
6281 XTframe_rehighlight (frame)
6282 struct frame *frame;
6284 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6287 static void
6288 x_frame_rehighlight (dpyinfo)
6289 struct x_display_info *dpyinfo;
6291 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6293 if (dpyinfo->x_focus_frame)
6295 dpyinfo->x_highlight_frame
6296 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6297 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6298 : dpyinfo->x_focus_frame);
6299 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6301 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6302 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6305 else
6306 dpyinfo->x_highlight_frame = 0;
6308 if (dpyinfo->x_highlight_frame != old_highlight)
6310 if (old_highlight)
6311 frame_unhighlight (old_highlight);
6312 if (dpyinfo->x_highlight_frame)
6313 frame_highlight (dpyinfo->x_highlight_frame);
6319 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6321 /* Initialize mode_switch_bit and modifier_meaning. */
6322 static void
6323 x_find_modifier_meanings (dpyinfo)
6324 struct x_display_info *dpyinfo;
6326 int min_code, max_code;
6327 KeySym *syms;
6328 int syms_per_code;
6329 XModifierKeymap *mods;
6331 dpyinfo->meta_mod_mask = 0;
6332 dpyinfo->shift_lock_mask = 0;
6333 dpyinfo->alt_mod_mask = 0;
6334 dpyinfo->super_mod_mask = 0;
6335 dpyinfo->hyper_mod_mask = 0;
6337 #ifdef HAVE_X11R4
6338 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6339 #else
6340 min_code = dpyinfo->display->min_keycode;
6341 max_code = dpyinfo->display->max_keycode;
6342 #endif
6344 syms = XGetKeyboardMapping (dpyinfo->display,
6345 min_code, max_code - min_code + 1,
6346 &syms_per_code);
6347 mods = XGetModifierMapping (dpyinfo->display);
6349 /* Scan the modifier table to see which modifier bits the Meta and
6350 Alt keysyms are on. */
6352 int row, col; /* The row and column in the modifier table. */
6354 for (row = 3; row < 8; row++)
6355 for (col = 0; col < mods->max_keypermod; col++)
6357 KeyCode code
6358 = mods->modifiermap[(row * mods->max_keypermod) + col];
6360 /* Zeroes are used for filler. Skip them. */
6361 if (code == 0)
6362 continue;
6364 /* Are any of this keycode's keysyms a meta key? */
6366 int code_col;
6368 for (code_col = 0; code_col < syms_per_code; code_col++)
6370 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6372 switch (sym)
6374 case XK_Meta_L:
6375 case XK_Meta_R:
6376 dpyinfo->meta_mod_mask |= (1 << row);
6377 break;
6379 case XK_Alt_L:
6380 case XK_Alt_R:
6381 dpyinfo->alt_mod_mask |= (1 << row);
6382 break;
6384 case XK_Hyper_L:
6385 case XK_Hyper_R:
6386 dpyinfo->hyper_mod_mask |= (1 << row);
6387 break;
6389 case XK_Super_L:
6390 case XK_Super_R:
6391 dpyinfo->super_mod_mask |= (1 << row);
6392 break;
6394 case XK_Shift_Lock:
6395 /* Ignore this if it's not on the lock modifier. */
6396 if ((1 << row) == LockMask)
6397 dpyinfo->shift_lock_mask = LockMask;
6398 break;
6405 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6406 if (! dpyinfo->meta_mod_mask)
6408 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6409 dpyinfo->alt_mod_mask = 0;
6412 /* If some keys are both alt and meta,
6413 make them just meta, not alt. */
6414 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6416 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6419 XFree ((char *) syms);
6420 XFreeModifiermap (mods);
6423 /* Convert between the modifier bits X uses and the modifier bits
6424 Emacs uses. */
6426 static unsigned int
6427 x_x_to_emacs_modifiers (dpyinfo, state)
6428 struct x_display_info *dpyinfo;
6429 unsigned int state;
6431 EMACS_UINT mod_meta = meta_modifier;
6432 EMACS_UINT mod_alt = alt_modifier;
6433 EMACS_UINT mod_hyper = hyper_modifier;
6434 EMACS_UINT mod_super = super_modifier;
6435 Lisp_Object tem;
6437 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6438 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6439 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6440 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6441 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6442 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6443 tem = Fget (Vx_super_keysym, Qmodifier_value);
6444 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6447 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6448 | ((state & ControlMask) ? ctrl_modifier : 0)
6449 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6450 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6451 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6452 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
6455 static unsigned int
6456 x_emacs_to_x_modifiers (dpyinfo, state)
6457 struct x_display_info *dpyinfo;
6458 unsigned int state;
6460 EMACS_UINT mod_meta = meta_modifier;
6461 EMACS_UINT mod_alt = alt_modifier;
6462 EMACS_UINT mod_hyper = hyper_modifier;
6463 EMACS_UINT mod_super = super_modifier;
6465 Lisp_Object tem;
6467 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6468 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6469 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6470 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6471 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6472 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6473 tem = Fget (Vx_super_keysym, Qmodifier_value);
6474 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6477 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6478 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6479 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6480 | ((state & shift_modifier) ? ShiftMask : 0)
6481 | ((state & ctrl_modifier) ? ControlMask : 0)
6482 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
6485 /* Convert a keysym to its name. */
6487 char *
6488 x_get_keysym_name (keysym)
6489 KeySym keysym;
6491 char *value;
6493 BLOCK_INPUT;
6494 value = XKeysymToString (keysym);
6495 UNBLOCK_INPUT;
6497 return value;
6502 /* Mouse clicks and mouse movement. Rah. */
6504 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6505 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6506 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6507 not force the value into range. */
6509 void
6510 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6511 FRAME_PTR f;
6512 register int pix_x, pix_y;
6513 register int *x, *y;
6514 XRectangle *bounds;
6515 int noclip;
6517 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6518 even for negative values. */
6519 if (pix_x < 0)
6520 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6521 if (pix_y < 0)
6522 pix_y -= (f)->output_data.x->line_height - 1;
6524 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6525 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6527 if (bounds)
6529 bounds->width = FONT_WIDTH (f->output_data.x->font);
6530 bounds->height = f->output_data.x->line_height;
6531 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6532 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6535 if (!noclip)
6537 if (pix_x < 0)
6538 pix_x = 0;
6539 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6540 pix_x = FRAME_WINDOW_WIDTH (f);
6542 if (pix_y < 0)
6543 pix_y = 0;
6544 else if (pix_y > f->height)
6545 pix_y = f->height;
6548 *x = pix_x;
6549 *y = pix_y;
6553 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6554 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6555 can't tell the positions because W's display is not up to date,
6556 return 0. */
6559 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6560 struct window *w;
6561 int hpos, vpos;
6562 int *frame_x, *frame_y;
6564 int success_p;
6566 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6567 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6569 if (display_completed)
6571 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6572 struct glyph *glyph = row->glyphs[TEXT_AREA];
6573 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6575 *frame_y = row->y;
6576 *frame_x = row->x;
6577 while (glyph < end)
6579 *frame_x += glyph->pixel_width;
6580 ++glyph;
6583 success_p = 1;
6585 else
6587 *frame_y = *frame_x = 0;
6588 success_p = 0;
6591 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6592 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6593 return success_p;
6597 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6599 If the event is a button press, then note that we have grabbed
6600 the mouse. */
6602 static Lisp_Object
6603 construct_mouse_click (result, event, f)
6604 struct input_event *result;
6605 XButtonEvent *event;
6606 struct frame *f;
6608 /* Make the event type NO_EVENT; we'll change that when we decide
6609 otherwise. */
6610 result->kind = MOUSE_CLICK_EVENT;
6611 result->code = event->button - Button1;
6612 result->timestamp = event->time;
6613 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6614 event->state)
6615 | (event->type == ButtonRelease
6616 ? up_modifier
6617 : down_modifier));
6619 XSETINT (result->x, event->x);
6620 XSETINT (result->y, event->y);
6621 XSETFRAME (result->frame_or_window, f);
6622 result->arg = Qnil;
6623 return Qnil;
6627 /* Function to report a mouse movement to the mainstream Emacs code.
6628 The input handler calls this.
6630 We have received a mouse movement event, which is given in *event.
6631 If the mouse is over a different glyph than it was last time, tell
6632 the mainstream emacs code by setting mouse_moved. If not, ask for
6633 another motion event, so we can check again the next time it moves. */
6635 static XMotionEvent last_mouse_motion_event;
6636 static Lisp_Object last_mouse_motion_frame;
6638 static void
6639 note_mouse_movement (frame, event)
6640 FRAME_PTR frame;
6641 XMotionEvent *event;
6643 last_mouse_movement_time = event->time;
6644 last_mouse_motion_event = *event;
6645 XSETFRAME (last_mouse_motion_frame, frame);
6647 if (event->window != FRAME_X_WINDOW (frame))
6649 frame->mouse_moved = 1;
6650 last_mouse_scroll_bar = Qnil;
6651 note_mouse_highlight (frame, -1, -1);
6654 /* Has the mouse moved off the glyph it was on at the last sighting? */
6655 else if (event->x < last_mouse_glyph.x
6656 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6657 || event->y < last_mouse_glyph.y
6658 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6660 frame->mouse_moved = 1;
6661 last_mouse_scroll_bar = Qnil;
6662 note_mouse_highlight (frame, event->x, event->y);
6667 /************************************************************************
6668 Mouse Face
6669 ************************************************************************/
6671 /* Find the glyph under window-relative coordinates X/Y in window W.
6672 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6673 strings. Return in *HPOS and *VPOS the row and column number of
6674 the glyph found. Return in *AREA the glyph area containing X.
6675 Value is a pointer to the glyph found or null if X/Y is not on
6676 text, or we can't tell because W's current matrix is not up to
6677 date. */
6679 static struct glyph *
6680 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6681 struct window *w;
6682 int x, y;
6683 int *hpos, *vpos, *area;
6684 int buffer_only_p;
6686 struct glyph *glyph, *end;
6687 struct glyph_row *row = NULL;
6688 int x0, i, left_area_width;
6690 /* Find row containing Y. Give up if some row is not enabled. */
6691 for (i = 0; i < w->current_matrix->nrows; ++i)
6693 row = MATRIX_ROW (w->current_matrix, i);
6694 if (!row->enabled_p)
6695 return NULL;
6696 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6697 break;
6700 *vpos = i;
6701 *hpos = 0;
6703 /* Give up if Y is not in the window. */
6704 if (i == w->current_matrix->nrows)
6705 return NULL;
6707 /* Get the glyph area containing X. */
6708 if (w->pseudo_window_p)
6710 *area = TEXT_AREA;
6711 x0 = 0;
6713 else
6715 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6716 if (x < left_area_width)
6718 *area = LEFT_MARGIN_AREA;
6719 x0 = 0;
6721 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6723 *area = TEXT_AREA;
6724 x0 = row->x + left_area_width;
6726 else
6728 *area = RIGHT_MARGIN_AREA;
6729 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6733 /* Find glyph containing X. */
6734 glyph = row->glyphs[*area];
6735 end = glyph + row->used[*area];
6736 while (glyph < end)
6738 if (x < x0 + glyph->pixel_width)
6740 if (w->pseudo_window_p)
6741 break;
6742 else if (!buffer_only_p || BUFFERP (glyph->object))
6743 break;
6746 x0 += glyph->pixel_width;
6747 ++glyph;
6750 if (glyph == end)
6751 return NULL;
6753 *hpos = glyph - row->glyphs[*area];
6754 return glyph;
6758 /* Convert frame-relative x/y to coordinates relative to window W.
6759 Takes pseudo-windows into account. */
6761 static void
6762 frame_to_window_pixel_xy (w, x, y)
6763 struct window *w;
6764 int *x, *y;
6766 if (w->pseudo_window_p)
6768 /* A pseudo-window is always full-width, and starts at the
6769 left edge of the frame, plus a frame border. */
6770 struct frame *f = XFRAME (w->frame);
6771 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6772 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6774 else
6776 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6777 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6782 /* Take proper action when mouse has moved to the mode or header line
6783 or marginal area of window W, x-position X and y-position Y. Area
6784 is 1, 3, 6 or 7 for the mode line, header line, left and right
6785 marginal area respectively. X is relative to the start of the text
6786 display area of W, so the width of bitmap areas and scroll bars
6787 must be subtracted to get a position relative to the start of the
6788 mode line. */
6790 static void
6791 note_mode_line_or_margin_highlight (w, x, y, portion)
6792 struct window *w;
6793 int x, y, portion;
6795 struct frame *f = XFRAME (w->frame);
6796 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6797 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6798 int charpos;
6799 Lisp_Object string, help, map, pos;
6801 if (portion == 1 || portion == 3)
6802 string = mode_line_string (w, x, y, portion == 1, &charpos);
6803 else
6804 string = marginal_area_string (w, x, y, portion, &charpos);
6806 if (STRINGP (string))
6808 pos = make_number (charpos);
6810 /* If we're on a string with `help-echo' text property, arrange
6811 for the help to be displayed. This is done by setting the
6812 global variable help_echo to the help string. */
6813 help = Fget_text_property (pos, Qhelp_echo, string);
6814 if (!NILP (help))
6816 help_echo = help;
6817 XSETWINDOW (help_echo_window, w);
6818 help_echo_object = string;
6819 help_echo_pos = charpos;
6822 /* Change the mouse pointer according to what is under X/Y. */
6823 map = Fget_text_property (pos, Qlocal_map, string);
6824 if (!KEYMAPP (map))
6825 map = Fget_text_property (pos, Qkeymap, string);
6826 if (KEYMAPP (map))
6827 cursor = f->output_data.x->nontext_cursor;
6830 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6834 /* Take proper action when the mouse has moved to position X, Y on
6835 frame F as regards highlighting characters that have mouse-face
6836 properties. Also de-highlighting chars where the mouse was before.
6837 X and Y can be negative or out of range. */
6839 static void
6840 note_mouse_highlight (f, x, y)
6841 struct frame *f;
6842 int x, y;
6844 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6845 int portion;
6846 Lisp_Object window;
6847 struct window *w;
6848 Cursor cursor = None;
6849 struct buffer *b;
6851 /* When a menu is active, don't highlight because this looks odd. */
6852 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
6853 if (popup_activated ())
6854 return;
6855 #endif
6857 if (NILP (Vmouse_highlight)
6858 || !f->glyphs_initialized_p)
6859 return;
6861 dpyinfo->mouse_face_mouse_x = x;
6862 dpyinfo->mouse_face_mouse_y = y;
6863 dpyinfo->mouse_face_mouse_frame = f;
6865 if (dpyinfo->mouse_face_defer)
6866 return;
6868 if (gc_in_progress)
6870 dpyinfo->mouse_face_deferred_gc = 1;
6871 return;
6874 /* Which window is that in? */
6875 window = window_from_coordinates (f, x, y, &portion, 1);
6877 /* If we were displaying active text in another window, clear that. */
6878 if (! EQ (window, dpyinfo->mouse_face_window))
6879 clear_mouse_face (dpyinfo);
6881 /* Not on a window -> return. */
6882 if (!WINDOWP (window))
6883 return;
6885 /* Convert to window-relative pixel coordinates. */
6886 w = XWINDOW (window);
6887 frame_to_window_pixel_xy (w, &x, &y);
6889 /* Handle tool-bar window differently since it doesn't display a
6890 buffer. */
6891 if (EQ (window, f->tool_bar_window))
6893 note_tool_bar_highlight (f, x, y);
6894 return;
6897 /* Mouse is on the mode, header line or margin? */
6898 if (portion == 1 || portion == 3 || portion == 6 || portion == 7)
6900 note_mode_line_or_margin_highlight (w, x, y, portion);
6901 return;
6904 if (portion == 2)
6905 cursor = f->output_data.x->horizontal_drag_cursor;
6906 else
6907 cursor = f->output_data.x->text_cursor;
6909 /* Are we in a window whose display is up to date?
6910 And verify the buffer's text has not changed. */
6911 b = XBUFFER (w->buffer);
6912 if (/* Within text portion of the window. */
6913 portion == 0
6914 && EQ (w->window_end_valid, w->buffer)
6915 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6916 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6918 int hpos, vpos, pos, i, area;
6919 struct glyph *glyph;
6920 Lisp_Object object;
6921 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6922 Lisp_Object *overlay_vec = NULL;
6923 int len, noverlays;
6924 struct buffer *obuf;
6925 int obegv, ozv, same_region;
6927 /* Find the glyph under X/Y. */
6928 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6930 /* Clear mouse face if X/Y not over text. */
6931 if (glyph == NULL
6932 || area != TEXT_AREA
6933 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6935 if (clear_mouse_face (dpyinfo))
6936 cursor = None;
6937 goto set_cursor;
6940 pos = glyph->charpos;
6941 object = glyph->object;
6942 if (!STRINGP (object) && !BUFFERP (object))
6943 goto set_cursor;
6945 /* If we get an out-of-range value, return now; avoid an error. */
6946 if (BUFFERP (object) && pos > BUF_Z (b))
6947 goto set_cursor;
6949 /* Make the window's buffer temporarily current for
6950 overlays_at and compute_char_face. */
6951 obuf = current_buffer;
6952 current_buffer = b;
6953 obegv = BEGV;
6954 ozv = ZV;
6955 BEGV = BEG;
6956 ZV = Z;
6958 /* Is this char mouse-active or does it have help-echo? */
6959 position = make_number (pos);
6961 if (BUFFERP (object))
6963 /* Put all the overlays we want in a vector in overlay_vec.
6964 Store the length in len. If there are more than 10, make
6965 enough space for all, and try again. */
6966 len = 10;
6967 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6968 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6969 if (noverlays > len)
6971 len = noverlays;
6972 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6973 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6976 /* Sort overlays into increasing priority order. */
6977 noverlays = sort_overlays (overlay_vec, noverlays, w);
6979 else
6980 noverlays = 0;
6982 same_region = (EQ (window, dpyinfo->mouse_face_window)
6983 && vpos >= dpyinfo->mouse_face_beg_row
6984 && vpos <= dpyinfo->mouse_face_end_row
6985 && (vpos > dpyinfo->mouse_face_beg_row
6986 || hpos >= dpyinfo->mouse_face_beg_col)
6987 && (vpos < dpyinfo->mouse_face_end_row
6988 || hpos < dpyinfo->mouse_face_end_col
6989 || dpyinfo->mouse_face_past_end));
6991 if (same_region)
6992 cursor = None;
6994 /* Check mouse-face highlighting. */
6995 if (! same_region
6996 /* If there exists an overlay with mouse-face overlapping
6997 the one we are currently highlighting, we have to
6998 check if we enter the overlapping overlay, and then
6999 highlight only that. */
7000 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7001 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7003 /* Find the highest priority overlay that has a mouse-face
7004 property. */
7005 overlay = Qnil;
7006 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7008 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7009 if (!NILP (mouse_face))
7010 overlay = overlay_vec[i];
7013 /* If we're actually highlighting the same overlay as
7014 before, there's no need to do that again. */
7015 if (!NILP (overlay)
7016 && EQ (overlay, dpyinfo->mouse_face_overlay))
7017 goto check_help_echo;
7019 dpyinfo->mouse_face_overlay = overlay;
7021 /* Clear the display of the old active region, if any. */
7022 if (clear_mouse_face (dpyinfo))
7023 cursor = None;
7025 /* If no overlay applies, get a text property. */
7026 if (NILP (overlay))
7027 mouse_face = Fget_text_property (position, Qmouse_face, object);
7029 /* Handle the overlay case. */
7030 if (!NILP (overlay))
7032 /* Find the range of text around this char that
7033 should be active. */
7034 Lisp_Object before, after;
7035 int ignore;
7037 before = Foverlay_start (overlay);
7038 after = Foverlay_end (overlay);
7039 /* Record this as the current active region. */
7040 fast_find_position (w, XFASTINT (before),
7041 &dpyinfo->mouse_face_beg_col,
7042 &dpyinfo->mouse_face_beg_row,
7043 &dpyinfo->mouse_face_beg_x,
7044 &dpyinfo->mouse_face_beg_y, Qnil);
7046 dpyinfo->mouse_face_past_end
7047 = !fast_find_position (w, XFASTINT (after),
7048 &dpyinfo->mouse_face_end_col,
7049 &dpyinfo->mouse_face_end_row,
7050 &dpyinfo->mouse_face_end_x,
7051 &dpyinfo->mouse_face_end_y, Qnil);
7052 dpyinfo->mouse_face_window = window;
7053 dpyinfo->mouse_face_face_id
7054 = face_at_buffer_position (w, pos, 0, 0,
7055 &ignore, pos + 1,
7056 !dpyinfo->mouse_face_hidden);
7058 /* Display it as active. */
7059 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7060 cursor = None;
7062 /* Handle the text property case. */
7063 else if (!NILP (mouse_face) && BUFFERP (object))
7065 /* Find the range of text around this char that
7066 should be active. */
7067 Lisp_Object before, after, beginning, end;
7068 int ignore;
7070 beginning = Fmarker_position (w->start);
7071 end = make_number (BUF_Z (XBUFFER (object))
7072 - XFASTINT (w->window_end_pos));
7073 before
7074 = Fprevious_single_property_change (make_number (pos + 1),
7075 Qmouse_face,
7076 object, beginning);
7077 after
7078 = Fnext_single_property_change (position, Qmouse_face,
7079 object, end);
7081 /* Record this as the current active region. */
7082 fast_find_position (w, XFASTINT (before),
7083 &dpyinfo->mouse_face_beg_col,
7084 &dpyinfo->mouse_face_beg_row,
7085 &dpyinfo->mouse_face_beg_x,
7086 &dpyinfo->mouse_face_beg_y, Qnil);
7087 dpyinfo->mouse_face_past_end
7088 = !fast_find_position (w, XFASTINT (after),
7089 &dpyinfo->mouse_face_end_col,
7090 &dpyinfo->mouse_face_end_row,
7091 &dpyinfo->mouse_face_end_x,
7092 &dpyinfo->mouse_face_end_y, Qnil);
7093 dpyinfo->mouse_face_window = window;
7095 if (BUFFERP (object))
7096 dpyinfo->mouse_face_face_id
7097 = face_at_buffer_position (w, pos, 0, 0,
7098 &ignore, pos + 1,
7099 !dpyinfo->mouse_face_hidden);
7101 /* Display it as active. */
7102 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7103 cursor = None;
7105 else if (!NILP (mouse_face) && STRINGP (object))
7107 Lisp_Object b, e;
7108 int ignore;
7110 b = Fprevious_single_property_change (make_number (pos + 1),
7111 Qmouse_face,
7112 object, Qnil);
7113 e = Fnext_single_property_change (position, Qmouse_face,
7114 object, Qnil);
7115 if (NILP (b))
7116 b = make_number (0);
7117 if (NILP (e))
7118 e = make_number (SCHARS (object) - 1);
7119 fast_find_string_pos (w, XINT (b), object,
7120 &dpyinfo->mouse_face_beg_col,
7121 &dpyinfo->mouse_face_beg_row,
7122 &dpyinfo->mouse_face_beg_x,
7123 &dpyinfo->mouse_face_beg_y, 0);
7124 fast_find_string_pos (w, XINT (e), object,
7125 &dpyinfo->mouse_face_end_col,
7126 &dpyinfo->mouse_face_end_row,
7127 &dpyinfo->mouse_face_end_x,
7128 &dpyinfo->mouse_face_end_y, 1);
7129 dpyinfo->mouse_face_past_end = 0;
7130 dpyinfo->mouse_face_window = window;
7131 dpyinfo->mouse_face_face_id
7132 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7133 glyph->face_id, 1);
7134 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7135 cursor = None;
7137 else if (STRINGP (object) && NILP (mouse_face))
7139 /* A string which doesn't have mouse-face, but
7140 the text ``under'' it might have. */
7141 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7142 int start = MATRIX_ROW_START_CHARPOS (r);
7144 pos = string_buffer_position (w, object, start);
7145 if (pos > 0)
7146 mouse_face = get_char_property_and_overlay (make_number (pos),
7147 Qmouse_face,
7148 w->buffer,
7149 &overlay);
7150 if (!NILP (mouse_face) && !NILP (overlay))
7152 Lisp_Object before = Foverlay_start (overlay);
7153 Lisp_Object after = Foverlay_end (overlay);
7154 int ignore;
7156 /* Note that we might not be able to find position
7157 BEFORE in the glyph matrix if the overlay is
7158 entirely covered by a `display' property. In
7159 this case, we overshoot. So let's stop in
7160 the glyph matrix before glyphs for OBJECT. */
7161 fast_find_position (w, XFASTINT (before),
7162 &dpyinfo->mouse_face_beg_col,
7163 &dpyinfo->mouse_face_beg_row,
7164 &dpyinfo->mouse_face_beg_x,
7165 &dpyinfo->mouse_face_beg_y,
7166 object);
7168 dpyinfo->mouse_face_past_end
7169 = !fast_find_position (w, XFASTINT (after),
7170 &dpyinfo->mouse_face_end_col,
7171 &dpyinfo->mouse_face_end_row,
7172 &dpyinfo->mouse_face_end_x,
7173 &dpyinfo->mouse_face_end_y,
7174 Qnil);
7175 dpyinfo->mouse_face_window = window;
7176 dpyinfo->mouse_face_face_id
7177 = face_at_buffer_position (w, pos, 0, 0,
7178 &ignore, pos + 1,
7179 !dpyinfo->mouse_face_hidden);
7181 /* Display it as active. */
7182 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7183 cursor = None;
7188 check_help_echo:
7190 /* Look for a `help-echo' property. */
7192 Lisp_Object help, overlay;
7194 /* Check overlays first. */
7195 help = overlay = Qnil;
7196 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7198 overlay = overlay_vec[i];
7199 help = Foverlay_get (overlay, Qhelp_echo);
7202 if (!NILP (help))
7204 help_echo = help;
7205 help_echo_window = window;
7206 help_echo_object = overlay;
7207 help_echo_pos = pos;
7209 else
7211 Lisp_Object object = glyph->object;
7212 int charpos = glyph->charpos;
7214 /* Try text properties. */
7215 if (STRINGP (object)
7216 && charpos >= 0
7217 && charpos < SCHARS (object))
7219 help = Fget_text_property (make_number (charpos),
7220 Qhelp_echo, object);
7221 if (NILP (help))
7223 /* If the string itself doesn't specify a help-echo,
7224 see if the buffer text ``under'' it does. */
7225 struct glyph_row *r
7226 = MATRIX_ROW (w->current_matrix, vpos);
7227 int start = MATRIX_ROW_START_CHARPOS (r);
7228 int pos = string_buffer_position (w, object, start);
7229 if (pos > 0)
7231 help = Fget_char_property (make_number (pos),
7232 Qhelp_echo, w->buffer);
7233 if (!NILP (help))
7235 charpos = pos;
7236 object = w->buffer;
7241 else if (BUFFERP (object)
7242 && charpos >= BEGV
7243 && charpos < ZV)
7244 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7245 object);
7247 if (!NILP (help))
7249 help_echo = help;
7250 help_echo_window = window;
7251 help_echo_object = object;
7252 help_echo_pos = charpos;
7257 BEGV = obegv;
7258 ZV = ozv;
7259 current_buffer = obuf;
7262 set_cursor:
7264 if (cursor != None)
7265 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7268 static void
7269 redo_mouse_highlight ()
7271 if (!NILP (last_mouse_motion_frame)
7272 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7273 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7274 last_mouse_motion_event.x,
7275 last_mouse_motion_event.y);
7280 /***********************************************************************
7281 Tool-bars
7282 ***********************************************************************/
7284 static int x_tool_bar_item P_ ((struct frame *, int, int,
7285 struct glyph **, int *, int *, int *));
7287 /* Tool-bar item index of the item on which a mouse button was pressed
7288 or -1. */
7290 static int last_tool_bar_item;
7293 /* Get information about the tool-bar item at position X/Y on frame F.
7294 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7295 the current matrix of the tool-bar window of F, or NULL if not
7296 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7297 item in F->tool_bar_items. Value is
7299 -1 if X/Y is not on a tool-bar item
7300 0 if X/Y is on the same item that was highlighted before.
7301 1 otherwise. */
7303 static int
7304 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7305 struct frame *f;
7306 int x, y;
7307 struct glyph **glyph;
7308 int *hpos, *vpos, *prop_idx;
7310 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7311 struct window *w = XWINDOW (f->tool_bar_window);
7312 int area;
7314 /* Find the glyph under X/Y. */
7315 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7316 if (*glyph == NULL)
7317 return -1;
7319 /* Get the start of this tool-bar item's properties in
7320 f->tool_bar_items. */
7321 if (!tool_bar_item_info (f, *glyph, prop_idx))
7322 return -1;
7324 /* Is mouse on the highlighted item? */
7325 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7326 && *vpos >= dpyinfo->mouse_face_beg_row
7327 && *vpos <= dpyinfo->mouse_face_end_row
7328 && (*vpos > dpyinfo->mouse_face_beg_row
7329 || *hpos >= dpyinfo->mouse_face_beg_col)
7330 && (*vpos < dpyinfo->mouse_face_end_row
7331 || *hpos < dpyinfo->mouse_face_end_col
7332 || dpyinfo->mouse_face_past_end))
7333 return 0;
7335 return 1;
7339 /* Handle mouse button event on the tool-bar of frame F, at
7340 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7341 or ButtonRelase. */
7343 static void
7344 x_handle_tool_bar_click (f, button_event)
7345 struct frame *f;
7346 XButtonEvent *button_event;
7348 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7349 struct window *w = XWINDOW (f->tool_bar_window);
7350 int hpos, vpos, prop_idx;
7351 struct glyph *glyph;
7352 Lisp_Object enabled_p;
7353 int x = button_event->x;
7354 int y = button_event->y;
7356 /* If not on the highlighted tool-bar item, return. */
7357 frame_to_window_pixel_xy (w, &x, &y);
7358 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7359 return;
7361 /* If item is disabled, do nothing. */
7362 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7363 if (NILP (enabled_p))
7364 return;
7366 if (button_event->type == ButtonPress)
7368 /* Show item in pressed state. */
7369 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7370 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7371 last_tool_bar_item = prop_idx;
7373 else
7375 Lisp_Object key, frame;
7376 struct input_event event;
7378 /* Show item in released state. */
7379 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7380 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7382 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7384 XSETFRAME (frame, f);
7385 event.kind = TOOL_BAR_EVENT;
7386 event.frame_or_window = frame;
7387 event.arg = frame;
7388 kbd_buffer_store_event (&event);
7390 event.kind = TOOL_BAR_EVENT;
7391 event.frame_or_window = frame;
7392 event.arg = key;
7393 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7394 button_event->state);
7395 kbd_buffer_store_event (&event);
7396 last_tool_bar_item = -1;
7401 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7402 tool-bar window-relative coordinates X/Y. Called from
7403 note_mouse_highlight. */
7405 static void
7406 note_tool_bar_highlight (f, x, y)
7407 struct frame *f;
7408 int x, y;
7410 Lisp_Object window = f->tool_bar_window;
7411 struct window *w = XWINDOW (window);
7412 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7413 int hpos, vpos;
7414 struct glyph *glyph;
7415 struct glyph_row *row;
7416 int i;
7417 Lisp_Object enabled_p;
7418 int prop_idx;
7419 enum draw_glyphs_face draw;
7420 int mouse_down_p, rc;
7422 /* Function note_mouse_highlight is called with negative x(y
7423 values when mouse moves outside of the frame. */
7424 if (x <= 0 || y <= 0)
7426 clear_mouse_face (dpyinfo);
7427 return;
7430 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7431 if (rc < 0)
7433 /* Not on tool-bar item. */
7434 clear_mouse_face (dpyinfo);
7435 return;
7437 else if (rc == 0)
7438 goto set_help_echo;
7440 clear_mouse_face (dpyinfo);
7442 /* Mouse is down, but on different tool-bar item? */
7443 mouse_down_p = (dpyinfo->grabbed
7444 && f == last_mouse_frame
7445 && FRAME_LIVE_P (f));
7446 if (mouse_down_p
7447 && last_tool_bar_item != prop_idx)
7448 return;
7450 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7451 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7453 /* If tool-bar item is not enabled, don't highlight it. */
7454 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7455 if (!NILP (enabled_p))
7457 /* Compute the x-position of the glyph. In front and past the
7458 image is a space. We include this is the highlighted area. */
7459 row = MATRIX_ROW (w->current_matrix, vpos);
7460 for (i = x = 0; i < hpos; ++i)
7461 x += row->glyphs[TEXT_AREA][i].pixel_width;
7463 /* Record this as the current active region. */
7464 dpyinfo->mouse_face_beg_col = hpos;
7465 dpyinfo->mouse_face_beg_row = vpos;
7466 dpyinfo->mouse_face_beg_x = x;
7467 dpyinfo->mouse_face_beg_y = row->y;
7468 dpyinfo->mouse_face_past_end = 0;
7470 dpyinfo->mouse_face_end_col = hpos + 1;
7471 dpyinfo->mouse_face_end_row = vpos;
7472 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7473 dpyinfo->mouse_face_end_y = row->y;
7474 dpyinfo->mouse_face_window = window;
7475 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7477 /* Display it as active. */
7478 show_mouse_face (dpyinfo, draw);
7479 dpyinfo->mouse_face_image_state = draw;
7482 set_help_echo:
7484 /* Set help_echo to a help string to display for this tool-bar item.
7485 XTread_socket does the rest. */
7486 help_echo_object = help_echo_window = Qnil;
7487 help_echo_pos = -1;
7488 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7489 if (NILP (help_echo))
7490 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7495 /* Find the glyph matrix position of buffer position CHARPOS in window
7496 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7497 current glyphs must be up to date. If CHARPOS is above window
7498 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7499 of last line in W. In the row containing CHARPOS, stop before glyphs
7500 having STOP as object. */
7502 #if 0 /* This is a version of fast_find_position that's more correct
7503 in the presence of hscrolling, for example. I didn't install
7504 it right away because the problem fixed is minor, it failed
7505 in 20.x as well, and I think it's too risky to install
7506 so near the release of 21.1. 2001-09-25 gerd. */
7508 static int
7509 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7510 struct window *w;
7511 int charpos;
7512 int *hpos, *vpos, *x, *y;
7513 Lisp_Object stop;
7515 struct glyph_row *row, *first;
7516 struct glyph *glyph, *end;
7517 int i, past_end = 0;
7519 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7520 row = row_containing_pos (w, charpos, first, NULL, 0);
7521 if (row == NULL)
7523 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7525 *x = *y = *hpos = *vpos = 0;
7526 return 0;
7528 else
7530 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7531 past_end = 1;
7535 *x = row->x;
7536 *y = row->y;
7537 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7539 glyph = row->glyphs[TEXT_AREA];
7540 end = glyph + row->used[TEXT_AREA];
7542 /* Skip over glyphs not having an object at the start of the row.
7543 These are special glyphs like truncation marks on terminal
7544 frames. */
7545 if (row->displays_text_p)
7546 while (glyph < end
7547 && INTEGERP (glyph->object)
7548 && !EQ (stop, glyph->object)
7549 && glyph->charpos < 0)
7551 *x += glyph->pixel_width;
7552 ++glyph;
7555 while (glyph < end
7556 && !INTEGERP (glyph->object)
7557 && !EQ (stop, glyph->object)
7558 && (!BUFFERP (glyph->object)
7559 || glyph->charpos < charpos))
7561 *x += glyph->pixel_width;
7562 ++glyph;
7565 *hpos = glyph - row->glyphs[TEXT_AREA];
7566 return past_end;
7569 #else /* not 0 */
7571 static int
7572 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7573 struct window *w;
7574 int pos;
7575 int *hpos, *vpos, *x, *y;
7576 Lisp_Object stop;
7578 int i;
7579 int lastcol;
7580 int maybe_next_line_p = 0;
7581 int line_start_position;
7582 int yb = window_text_bottom_y (w);
7583 struct glyph_row *row, *best_row;
7584 int row_vpos, best_row_vpos;
7585 int current_x;
7587 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7588 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7590 while (row->y < yb)
7592 if (row->used[TEXT_AREA])
7593 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7594 else
7595 line_start_position = 0;
7597 if (line_start_position > pos)
7598 break;
7599 /* If the position sought is the end of the buffer,
7600 don't include the blank lines at the bottom of the window. */
7601 else if (line_start_position == pos
7602 && pos == BUF_ZV (XBUFFER (w->buffer)))
7604 maybe_next_line_p = 1;
7605 break;
7607 else if (line_start_position > 0)
7609 best_row = row;
7610 best_row_vpos = row_vpos;
7613 if (row->y + row->height >= yb)
7614 break;
7616 ++row;
7617 ++row_vpos;
7620 /* Find the right column within BEST_ROW. */
7621 lastcol = 0;
7622 current_x = best_row->x;
7623 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7625 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7626 int charpos = glyph->charpos;
7628 if (BUFFERP (glyph->object))
7630 if (charpos == pos)
7632 *hpos = i;
7633 *vpos = best_row_vpos;
7634 *x = current_x;
7635 *y = best_row->y;
7636 return 1;
7638 else if (charpos > pos)
7639 break;
7641 else if (EQ (glyph->object, stop))
7642 break;
7644 if (charpos > 0)
7645 lastcol = i;
7646 current_x += glyph->pixel_width;
7649 /* If we're looking for the end of the buffer,
7650 and we didn't find it in the line we scanned,
7651 use the start of the following line. */
7652 if (maybe_next_line_p)
7654 ++best_row;
7655 ++best_row_vpos;
7656 lastcol = 0;
7657 current_x = best_row->x;
7660 *vpos = best_row_vpos;
7661 *hpos = lastcol + 1;
7662 *x = current_x;
7663 *y = best_row->y;
7664 return 0;
7667 #endif /* not 0 */
7670 /* Find the position of the glyph for position POS in OBJECT in
7671 window W's current matrix, and return in *X, *Y the pixel
7672 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7674 RIGHT_P non-zero means return the position of the right edge of the
7675 glyph, RIGHT_P zero means return the left edge position.
7677 If no glyph for POS exists in the matrix, return the position of
7678 the glyph with the next smaller position that is in the matrix, if
7679 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7680 exists in the matrix, return the position of the glyph with the
7681 next larger position in OBJECT.
7683 Value is non-zero if a glyph was found. */
7685 static int
7686 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7687 struct window *w;
7688 int pos;
7689 Lisp_Object object;
7690 int *hpos, *vpos, *x, *y;
7691 int right_p;
7693 int yb = window_text_bottom_y (w);
7694 struct glyph_row *r;
7695 struct glyph *best_glyph = NULL;
7696 struct glyph_row *best_row = NULL;
7697 int best_x = 0;
7699 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7700 r->enabled_p && r->y < yb;
7701 ++r)
7703 struct glyph *g = r->glyphs[TEXT_AREA];
7704 struct glyph *e = g + r->used[TEXT_AREA];
7705 int gx;
7707 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7708 if (EQ (g->object, object))
7710 if (g->charpos == pos)
7712 best_glyph = g;
7713 best_x = gx;
7714 best_row = r;
7715 goto found;
7717 else if (best_glyph == NULL
7718 || ((abs (g->charpos - pos)
7719 < abs (best_glyph->charpos - pos))
7720 && (right_p
7721 ? g->charpos < pos
7722 : g->charpos > pos)))
7724 best_glyph = g;
7725 best_x = gx;
7726 best_row = r;
7731 found:
7733 if (best_glyph)
7735 *x = best_x;
7736 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7738 if (right_p)
7740 *x += best_glyph->pixel_width;
7741 ++*hpos;
7744 *y = best_row->y;
7745 *vpos = best_row - w->current_matrix->rows;
7748 return best_glyph != NULL;
7752 /* Display the active region described by mouse_face_*
7753 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7755 static void
7756 show_mouse_face (dpyinfo, draw)
7757 struct x_display_info *dpyinfo;
7758 enum draw_glyphs_face draw;
7760 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7761 struct frame *f = XFRAME (WINDOW_FRAME (w));
7763 if (/* If window is in the process of being destroyed, don't bother
7764 to do anything. */
7765 w->current_matrix != NULL
7766 /* Don't update mouse highlight if hidden */
7767 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7768 /* Recognize when we are called to operate on rows that don't exist
7769 anymore. This can happen when a window is split. */
7770 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7772 int phys_cursor_on_p = w->phys_cursor_on_p;
7773 struct glyph_row *row, *first, *last;
7775 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7776 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7778 for (row = first; row <= last && row->enabled_p; ++row)
7780 int start_hpos, end_hpos, start_x;
7782 /* For all but the first row, the highlight starts at column 0. */
7783 if (row == first)
7785 start_hpos = dpyinfo->mouse_face_beg_col;
7786 start_x = dpyinfo->mouse_face_beg_x;
7788 else
7790 start_hpos = 0;
7791 start_x = 0;
7794 if (row == last)
7795 end_hpos = dpyinfo->mouse_face_end_col;
7796 else
7797 end_hpos = row->used[TEXT_AREA];
7799 if (end_hpos > start_hpos)
7801 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7802 start_hpos, end_hpos, draw, 0);
7804 row->mouse_face_p
7805 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7809 /* When we've written over the cursor, arrange for it to
7810 be displayed again. */
7811 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7812 x_display_cursor (w, 1,
7813 w->phys_cursor.hpos, w->phys_cursor.vpos,
7814 w->phys_cursor.x, w->phys_cursor.y);
7817 /* Change the mouse cursor. */
7818 if (draw == DRAW_NORMAL_TEXT)
7819 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7820 f->output_data.x->text_cursor);
7821 else if (draw == DRAW_MOUSE_FACE)
7822 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7823 f->output_data.x->cross_cursor);
7824 else
7825 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7826 f->output_data.x->nontext_cursor);
7829 /* Clear out the mouse-highlighted active region.
7830 Redraw it un-highlighted first. Value is non-zero if mouse
7831 face was actually drawn unhighlighted. */
7833 static int
7834 clear_mouse_face (dpyinfo)
7835 struct x_display_info *dpyinfo;
7837 int cleared = 0;
7839 if (!NILP (dpyinfo->mouse_face_window))
7841 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7842 cleared = 1;
7845 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7846 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7847 dpyinfo->mouse_face_window = Qnil;
7848 dpyinfo->mouse_face_overlay = Qnil;
7849 return cleared;
7853 /* Clear any mouse-face on window W. This function is part of the
7854 redisplay interface, and is called from try_window_id and similar
7855 functions to ensure the mouse-highlight is off. */
7857 static void
7858 x_clear_mouse_face (w)
7859 struct window *w;
7861 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7862 Lisp_Object window;
7864 BLOCK_INPUT;
7865 XSETWINDOW (window, w);
7866 if (EQ (window, dpyinfo->mouse_face_window))
7867 clear_mouse_face (dpyinfo);
7868 UNBLOCK_INPUT;
7872 /* Just discard the mouse face information for frame F, if any.
7873 This is used when the size of F is changed. */
7875 void
7876 cancel_mouse_face (f)
7877 FRAME_PTR f;
7879 Lisp_Object window;
7880 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7882 window = dpyinfo->mouse_face_window;
7883 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7885 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7886 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7887 dpyinfo->mouse_face_window = Qnil;
7892 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7895 /* Try to determine frame pixel position and size of the glyph under
7896 frame pixel coordinates X/Y on frame F . Return the position and
7897 size in *RECT. Value is non-zero if we could compute these
7898 values. */
7900 static int
7901 glyph_rect (f, x, y, rect)
7902 struct frame *f;
7903 int x, y;
7904 XRectangle *rect;
7906 Lisp_Object window;
7907 int part, found = 0;
7909 window = window_from_coordinates (f, x, y, &part, 0);
7910 if (!NILP (window))
7912 struct window *w = XWINDOW (window);
7913 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7914 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7916 frame_to_window_pixel_xy (w, &x, &y);
7918 for (; !found && r < end && r->enabled_p; ++r)
7919 if (r->y >= y)
7921 struct glyph *g = r->glyphs[TEXT_AREA];
7922 struct glyph *end = g + r->used[TEXT_AREA];
7923 int gx;
7925 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7926 if (gx >= x)
7928 rect->width = g->pixel_width;
7929 rect->height = r->height;
7930 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7931 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7932 found = 1;
7937 return found;
7941 /* Return the current position of the mouse.
7942 *FP should be a frame which indicates which display to ask about.
7944 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7945 and *PART to the frame, window, and scroll bar part that the mouse
7946 is over. Set *X and *Y to the portion and whole of the mouse's
7947 position on the scroll bar.
7949 If the mouse movement started elsewhere, set *FP to the frame the
7950 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7951 the mouse is over.
7953 Set *TIME to the server time-stamp for the time at which the mouse
7954 was at this position.
7956 Don't store anything if we don't have a valid set of values to report.
7958 This clears the mouse_moved flag, so we can wait for the next mouse
7959 movement. */
7961 static void
7962 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7963 FRAME_PTR *fp;
7964 int insist;
7965 Lisp_Object *bar_window;
7966 enum scroll_bar_part *part;
7967 Lisp_Object *x, *y;
7968 unsigned long *time;
7970 FRAME_PTR f1;
7972 BLOCK_INPUT;
7974 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7975 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7976 else
7978 Window root;
7979 int root_x, root_y;
7981 Window dummy_window;
7982 int dummy;
7984 Lisp_Object frame, tail;
7986 /* Clear the mouse-moved flag for every frame on this display. */
7987 FOR_EACH_FRAME (tail, frame)
7988 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7989 XFRAME (frame)->mouse_moved = 0;
7991 last_mouse_scroll_bar = Qnil;
7993 /* Figure out which root window we're on. */
7994 XQueryPointer (FRAME_X_DISPLAY (*fp),
7995 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7997 /* The root window which contains the pointer. */
7998 &root,
8000 /* Trash which we can't trust if the pointer is on
8001 a different screen. */
8002 &dummy_window,
8004 /* The position on that root window. */
8005 &root_x, &root_y,
8007 /* More trash we can't trust. */
8008 &dummy, &dummy,
8010 /* Modifier keys and pointer buttons, about which
8011 we don't care. */
8012 (unsigned int *) &dummy);
8014 /* Now we have a position on the root; find the innermost window
8015 containing the pointer. */
8017 Window win, child;
8018 int win_x, win_y;
8019 int parent_x = 0, parent_y = 0;
8020 int count;
8022 win = root;
8024 /* XTranslateCoordinates can get errors if the window
8025 structure is changing at the same time this function
8026 is running. So at least we must not crash from them. */
8028 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8030 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8031 && FRAME_LIVE_P (last_mouse_frame))
8033 /* If mouse was grabbed on a frame, give coords for that frame
8034 even if the mouse is now outside it. */
8035 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8037 /* From-window, to-window. */
8038 root, FRAME_X_WINDOW (last_mouse_frame),
8040 /* From-position, to-position. */
8041 root_x, root_y, &win_x, &win_y,
8043 /* Child of win. */
8044 &child);
8045 f1 = last_mouse_frame;
8047 else
8049 while (1)
8051 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8053 /* From-window, to-window. */
8054 root, win,
8056 /* From-position, to-position. */
8057 root_x, root_y, &win_x, &win_y,
8059 /* Child of win. */
8060 &child);
8062 if (child == None || child == win)
8063 break;
8065 win = child;
8066 parent_x = win_x;
8067 parent_y = win_y;
8070 /* Now we know that:
8071 win is the innermost window containing the pointer
8072 (XTC says it has no child containing the pointer),
8073 win_x and win_y are the pointer's position in it
8074 (XTC did this the last time through), and
8075 parent_x and parent_y are the pointer's position in win's parent.
8076 (They are what win_x and win_y were when win was child.
8077 If win is the root window, it has no parent, and
8078 parent_{x,y} are invalid, but that's okay, because we'll
8079 never use them in that case.) */
8081 /* Is win one of our frames? */
8082 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8084 #ifdef USE_X_TOOLKIT
8085 /* If we end up with the menu bar window, say it's not
8086 on the frame. */
8087 if (f1 != NULL
8088 && f1->output_data.x->menubar_widget
8089 && win == XtWindow (f1->output_data.x->menubar_widget))
8090 f1 = NULL;
8091 #endif /* USE_X_TOOLKIT */
8094 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8095 f1 = 0;
8097 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8099 /* If not, is it one of our scroll bars? */
8100 if (! f1)
8102 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8104 if (bar)
8106 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8107 win_x = parent_x;
8108 win_y = parent_y;
8112 if (f1 == 0 && insist > 0)
8113 f1 = SELECTED_FRAME ();
8115 if (f1)
8117 /* Ok, we found a frame. Store all the values.
8118 last_mouse_glyph is a rectangle used to reduce the
8119 generation of mouse events. To not miss any motion
8120 events, we must divide the frame into rectangles of the
8121 size of the smallest character that could be displayed
8122 on it, i.e. into the same rectangles that matrices on
8123 the frame are divided into. */
8125 int width, height, gx, gy;
8126 XRectangle rect;
8128 if (glyph_rect (f1, win_x, win_y, &rect))
8129 last_mouse_glyph = rect;
8130 else
8132 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8133 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8134 gx = win_x;
8135 gy = win_y;
8137 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8138 round down even for negative values. */
8139 if (gx < 0)
8140 gx -= width - 1;
8141 if (gy < 0)
8142 gy -= height - 1;
8143 gx = (gx + width - 1) / width * width;
8144 gy = (gy + height - 1) / height * height;
8146 last_mouse_glyph.width = width;
8147 last_mouse_glyph.height = height;
8148 last_mouse_glyph.x = gx;
8149 last_mouse_glyph.y = gy;
8152 *bar_window = Qnil;
8153 *part = 0;
8154 *fp = f1;
8155 XSETINT (*x, win_x);
8156 XSETINT (*y, win_y);
8157 *time = last_mouse_movement_time;
8162 UNBLOCK_INPUT;
8166 #ifdef USE_X_TOOLKIT
8168 /* Atimer callback function for TIMER. Called every 0.1s to process
8169 Xt timeouts, if needed. We must avoid calling XtAppPending as
8170 much as possible because that function does an implicit XFlush
8171 that slows us down. */
8173 static void
8174 x_process_timeouts (timer)
8175 struct atimer *timer;
8177 if (toolkit_scroll_bar_interaction || popup_activated ())
8179 BLOCK_INPUT;
8180 while (XtAppPending (Xt_app_con) & XtIMTimer)
8181 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8182 UNBLOCK_INPUT;
8186 #endif /* USE_X_TOOLKIT */
8189 /* Scroll bar support. */
8191 /* Given an X window ID, find the struct scroll_bar which manages it.
8192 This can be called in GC, so we have to make sure to strip off mark
8193 bits. */
8195 static struct scroll_bar *
8196 x_window_to_scroll_bar (window_id)
8197 Window window_id;
8199 Lisp_Object tail;
8201 #ifdef USE_GTK
8202 window_id = (Window) xg_get_scroll_id_for_window (window_id);
8203 #endif /* USE_GTK */
8205 for (tail = Vframe_list;
8206 XGCTYPE (tail) == Lisp_Cons;
8207 tail = XCDR (tail))
8209 Lisp_Object frame, bar, condemned;
8211 frame = XCAR (tail);
8212 /* All elements of Vframe_list should be frames. */
8213 if (! GC_FRAMEP (frame))
8214 abort ();
8216 /* Scan this frame's scroll bar list for a scroll bar with the
8217 right window ID. */
8218 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8219 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8220 /* This trick allows us to search both the ordinary and
8221 condemned scroll bar lists with one loop. */
8222 ! GC_NILP (bar) || (bar = condemned,
8223 condemned = Qnil,
8224 ! GC_NILP (bar));
8225 bar = XSCROLL_BAR (bar)->next)
8226 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8227 return XSCROLL_BAR (bar);
8230 return 0;
8234 #if defined USE_LUCID
8236 /* Return the Lucid menu bar WINDOW is part of. Return null
8237 if WINDOW is not part of a menu bar. */
8239 static Widget
8240 x_window_to_menu_bar (window)
8241 Window window;
8243 Lisp_Object tail;
8245 for (tail = Vframe_list;
8246 XGCTYPE (tail) == Lisp_Cons;
8247 tail = XCDR (tail))
8249 Lisp_Object frame = XCAR (tail);
8250 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8252 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8253 return menu_bar;
8256 return NULL;
8259 #endif /* USE_LUCID */
8262 /************************************************************************
8263 Toolkit scroll bars
8264 ************************************************************************/
8266 #ifdef USE_TOOLKIT_SCROLL_BARS
8268 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8269 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8270 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8271 struct scroll_bar *));
8272 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8273 int, int, int));
8276 /* Lisp window being scrolled. Set when starting to interact with
8277 a toolkit scroll bar, reset to nil when ending the interaction. */
8279 static Lisp_Object window_being_scrolled;
8281 /* Last scroll bar part sent in xm_scroll_callback. */
8283 static int last_scroll_bar_part;
8285 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8286 that movements of 1/20 of the screen size are mapped to up/down. */
8288 #ifndef USE_GTK
8289 /* Id of action hook installed for scroll bars. */
8291 static XtActionHookId action_hook_id;
8293 static Boolean xaw3d_arrow_scroll;
8295 /* Whether the drag scrolling maintains the mouse at the top of the
8296 thumb. If not, resizing the thumb needs to be done more carefully
8297 to avoid jerkyness. */
8299 static Boolean xaw3d_pick_top;
8302 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8303 bars are used.. The hook is responsible for detecting when
8304 the user ends an interaction with the scroll bar, and generates
8305 a `end-scroll' SCROLL_BAR_CLICK_EVENT' event if so. */
8307 static void
8308 xt_action_hook (widget, client_data, action_name, event, params,
8309 num_params)
8310 Widget widget;
8311 XtPointer client_data;
8312 String action_name;
8313 XEvent *event;
8314 String *params;
8315 Cardinal *num_params;
8317 int scroll_bar_p;
8318 char *end_action;
8320 #ifdef USE_MOTIF
8321 scroll_bar_p = XmIsScrollBar (widget);
8322 end_action = "Release";
8323 #else /* !USE_MOTIF i.e. use Xaw */
8324 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8325 end_action = "EndScroll";
8326 #endif /* USE_MOTIF */
8328 if (scroll_bar_p
8329 && strcmp (action_name, end_action) == 0
8330 && WINDOWP (window_being_scrolled))
8332 struct window *w;
8334 x_send_scroll_bar_event (window_being_scrolled,
8335 scroll_bar_end_scroll, 0, 0);
8336 w = XWINDOW (window_being_scrolled);
8337 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8338 window_being_scrolled = Qnil;
8339 last_scroll_bar_part = -1;
8341 /* Xt timeouts no longer needed. */
8342 toolkit_scroll_bar_interaction = 0;
8345 #endif /* not USE_GTK */
8347 /* A vector of windows used for communication between
8348 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8350 static struct window **scroll_bar_windows;
8351 static int scroll_bar_windows_size;
8354 /* Send a client message with message type Xatom_Scrollbar for a
8355 scroll action to the frame of WINDOW. PART is a value identifying
8356 the part of the scroll bar that was clicked on. PORTION is the
8357 amount to scroll of a whole of WHOLE. */
8359 static void
8360 x_send_scroll_bar_event (window, part, portion, whole)
8361 Lisp_Object window;
8362 int part, portion, whole;
8364 XEvent event;
8365 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8366 struct window *w = XWINDOW (window);
8367 struct frame *f = XFRAME (w->frame);
8368 int i;
8370 BLOCK_INPUT;
8372 /* Construct a ClientMessage event to send to the frame. */
8373 ev->type = ClientMessage;
8374 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8375 ev->display = FRAME_X_DISPLAY (f);
8376 ev->window = FRAME_X_WINDOW (f);
8377 ev->format = 32;
8379 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8380 not enough to store a pointer or Lisp_Object on a 64 bit system.
8381 So, store the window in scroll_bar_windows and pass the index
8382 into that array in the event. */
8383 for (i = 0; i < scroll_bar_windows_size; ++i)
8384 if (scroll_bar_windows[i] == NULL)
8385 break;
8387 if (i == scroll_bar_windows_size)
8389 int new_size = max (10, 2 * scroll_bar_windows_size);
8390 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8391 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8393 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8394 nbytes);
8395 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8396 scroll_bar_windows_size = new_size;
8399 scroll_bar_windows[i] = w;
8400 ev->data.l[0] = (long) i;
8401 ev->data.l[1] = (long) part;
8402 ev->data.l[2] = (long) 0;
8403 ev->data.l[3] = (long) portion;
8404 ev->data.l[4] = (long) whole;
8406 /* Make Xt timeouts work while the scroll bar is active. */
8407 toolkit_scroll_bar_interaction = 1;
8409 /* Setting the event mask to zero means that the message will
8410 be sent to the client that created the window, and if that
8411 window no longer exists, no event will be sent. */
8412 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8413 UNBLOCK_INPUT;
8417 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8418 in *IEVENT. */
8420 static void
8421 x_scroll_bar_to_input_event (event, ievent)
8422 XEvent *event;
8423 struct input_event *ievent;
8425 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8426 Lisp_Object window;
8427 struct frame *f;
8428 struct window *w;
8430 w = scroll_bar_windows[ev->data.l[0]];
8431 scroll_bar_windows[ev->data.l[0]] = NULL;
8433 XSETWINDOW (window, w);
8434 f = XFRAME (w->frame);
8436 ievent->kind = SCROLL_BAR_CLICK_EVENT;
8437 ievent->frame_or_window = window;
8438 ievent->arg = Qnil;
8439 #ifdef USE_GTK
8440 ievent->timestamp = CurrentTime;
8441 #else
8442 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8443 #endif
8444 ievent->part = ev->data.l[1];
8445 ievent->code = ev->data.l[2];
8446 ievent->x = make_number ((int) ev->data.l[3]);
8447 ievent->y = make_number ((int) ev->data.l[4]);
8448 ievent->modifiers = 0;
8452 #ifdef USE_MOTIF
8454 /* Minimum and maximum values used for Motif scroll bars. */
8456 #define XM_SB_MIN 1
8457 #define XM_SB_MAX 10000000
8458 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8461 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8462 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8463 CALL_DATA is a pointer to a XmScrollBarCallbackStruct. */
8465 static void
8466 xm_scroll_callback (widget, client_data, call_data)
8467 Widget widget;
8468 XtPointer client_data, call_data;
8470 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8471 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8472 int part = -1, whole = 0, portion = 0;
8474 switch (cs->reason)
8476 case XmCR_DECREMENT:
8477 bar->dragging = Qnil;
8478 part = scroll_bar_up_arrow;
8479 break;
8481 case XmCR_INCREMENT:
8482 bar->dragging = Qnil;
8483 part = scroll_bar_down_arrow;
8484 break;
8486 case XmCR_PAGE_DECREMENT:
8487 bar->dragging = Qnil;
8488 part = scroll_bar_above_handle;
8489 break;
8491 case XmCR_PAGE_INCREMENT:
8492 bar->dragging = Qnil;
8493 part = scroll_bar_below_handle;
8494 break;
8496 case XmCR_TO_TOP:
8497 bar->dragging = Qnil;
8498 part = scroll_bar_to_top;
8499 break;
8501 case XmCR_TO_BOTTOM:
8502 bar->dragging = Qnil;
8503 part = scroll_bar_to_bottom;
8504 break;
8506 case XmCR_DRAG:
8508 int slider_size;
8509 int dragging_down_p = (INTEGERP (bar->dragging)
8510 && XINT (bar->dragging) <= cs->value);
8512 /* Get the slider size. */
8513 BLOCK_INPUT;
8514 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8515 UNBLOCK_INPUT;
8517 whole = XM_SB_RANGE - slider_size;
8518 portion = min (cs->value - XM_SB_MIN, whole);
8519 part = scroll_bar_handle;
8520 bar->dragging = make_number (cs->value);
8522 break;
8524 case XmCR_VALUE_CHANGED:
8525 break;
8528 if (part >= 0)
8530 window_being_scrolled = bar->window;
8531 last_scroll_bar_part = part;
8532 x_send_scroll_bar_event (bar->window, part, portion, whole);
8537 #else /* !USE_MOTIF, i.e. Xaw or GTK */
8538 #ifdef USE_GTK
8539 /* Scroll bar callback for Gtk scroll bars. WIDGET is the scroll
8540 bar adjustment widget. DATA is a pointer to the scroll_bar structure. */
8542 static void
8543 xg_scroll_callback (widget, data)
8544 GtkWidget *widget;
8545 gpointer data;
8547 struct scroll_bar *bar = (struct scroll_bar *) data;
8548 gdouble previous;
8549 gdouble position;
8550 gdouble *p;
8551 int diff;
8553 int part = -1, whole = 0, portion = 0;
8554 GtkAdjustment *adj = GTK_ADJUSTMENT (widget);
8556 if (xg_ignore_gtk_scrollbar) return;
8558 position = gtk_adjustment_get_value (adj);
8560 p = g_object_get_data (G_OBJECT (widget), XG_LAST_SB_DATA);
8561 if (! p)
8563 p = (gdouble*) xmalloc (sizeof (gdouble));
8564 *p = XG_SB_MIN;
8565 g_object_set_data (G_OBJECT (widget), XG_LAST_SB_DATA, p);
8568 previous = *p;
8569 *p = position;
8571 diff = (int) (position - previous);
8573 if (diff == (int) adj->step_increment)
8575 part = scroll_bar_down_arrow;
8576 bar->dragging = Qnil;
8578 else if (-diff == (int) adj->step_increment)
8580 part = scroll_bar_up_arrow;
8581 bar->dragging = Qnil;
8583 else if (diff == (int) adj->page_increment)
8585 part = scroll_bar_below_handle;
8586 bar->dragging = Qnil;
8588 else if (-diff == (int) adj->page_increment)
8590 part = scroll_bar_above_handle;
8591 bar->dragging = Qnil;
8593 else
8595 part = scroll_bar_handle;
8596 whole = adj->upper - adj->page_size;
8597 portion = min (position, whole);
8598 bar->dragging = make_number (portion);
8601 if (part >= 0)
8603 xg_ignore_next_thumb = 1;
8604 window_being_scrolled = bar->window;
8605 last_scroll_bar_part = part;
8606 x_send_scroll_bar_event (bar->window, part, portion, whole);
8610 #else /* not USE_GTK */
8612 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8613 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8614 scroll bar struct. CALL_DATA is a pointer to a float saying where
8615 the thumb is. */
8617 static void
8618 xaw_jump_callback (widget, client_data, call_data)
8619 Widget widget;
8620 XtPointer client_data, call_data;
8622 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8623 float top = *(float *) call_data;
8624 float shown;
8625 int whole, portion, height;
8626 int part;
8628 /* Get the size of the thumb, a value between 0 and 1. */
8629 BLOCK_INPUT;
8630 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8631 UNBLOCK_INPUT;
8633 whole = 10000000;
8634 portion = shown < 1 ? top * whole : 0;
8636 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8637 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8638 the bottom, so we force the scrolling whenever we see that we're
8639 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8640 we try to ensure that we always stay two pixels away from the
8641 bottom). */
8642 part = scroll_bar_down_arrow;
8643 else
8644 part = scroll_bar_handle;
8646 window_being_scrolled = bar->window;
8647 bar->dragging = make_number (portion);
8648 last_scroll_bar_part = part;
8649 x_send_scroll_bar_event (bar->window, part, portion, whole);
8653 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8654 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8655 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8656 the scroll bar. CALL_DATA is an integer specifying the action that
8657 has taken place. Its magnitude is in the range 0..height of the
8658 scroll bar. Negative values mean scroll towards buffer start.
8659 Values < height of scroll bar mean line-wise movement. */
8661 static void
8662 xaw_scroll_callback (widget, client_data, call_data)
8663 Widget widget;
8664 XtPointer client_data, call_data;
8666 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8667 /* The position really is stored cast to a pointer. */
8668 int position = (long) call_data;
8669 Dimension height;
8670 int part;
8672 /* Get the height of the scroll bar. */
8673 BLOCK_INPUT;
8674 XtVaGetValues (widget, XtNheight, &height, NULL);
8675 UNBLOCK_INPUT;
8677 if (abs (position) >= height)
8678 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8680 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8681 it maps line-movement to call_data = max(5, height/20). */
8682 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8683 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8684 else
8685 part = scroll_bar_move_ratio;
8687 window_being_scrolled = bar->window;
8688 bar->dragging = Qnil;
8689 last_scroll_bar_part = part;
8690 x_send_scroll_bar_event (bar->window, part, position, height);
8693 #endif /* not USE_GTK */
8694 #endif /* not USE_MOTIF */
8696 #define SCROLL_BAR_NAME "verticalScrollBar"
8698 /* Create the widget for scroll bar BAR on frame F. Record the widget
8699 and X window of the scroll bar in BAR. */
8701 #ifdef USE_GTK
8702 static void
8703 x_create_toolkit_scroll_bar (f, bar)
8704 struct frame *f;
8705 struct scroll_bar *bar;
8707 char *scroll_bar_name = SCROLL_BAR_NAME;
8709 BLOCK_INPUT;
8710 xg_create_scroll_bar (f, bar, G_CALLBACK (xg_scroll_callback),
8711 scroll_bar_name);
8712 UNBLOCK_INPUT;
8715 #else /* not USE_GTK */
8717 static void
8718 x_create_toolkit_scroll_bar (f, bar)
8719 struct frame *f;
8720 struct scroll_bar *bar;
8722 Window xwindow;
8723 Widget widget;
8724 Arg av[20];
8725 int ac = 0;
8726 char *scroll_bar_name = SCROLL_BAR_NAME;
8727 unsigned long pixel;
8729 BLOCK_INPUT;
8731 #ifdef USE_MOTIF
8732 /* Set resources. Create the widget. */
8733 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8734 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8735 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8736 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8737 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8738 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8739 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8741 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8742 if (pixel != -1)
8744 XtSetArg (av[ac], XmNforeground, pixel);
8745 ++ac;
8748 pixel = f->output_data.x->scroll_bar_background_pixel;
8749 if (pixel != -1)
8751 XtSetArg (av[ac], XmNbackground, pixel);
8752 ++ac;
8755 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8756 scroll_bar_name, av, ac);
8758 /* Add one callback for everything that can happen. */
8759 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8760 (XtPointer) bar);
8761 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8762 (XtPointer) bar);
8763 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8764 (XtPointer) bar);
8765 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8766 (XtPointer) bar);
8767 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8768 (XtPointer) bar);
8769 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8770 (XtPointer) bar);
8771 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8772 (XtPointer) bar);
8774 /* Realize the widget. Only after that is the X window created. */
8775 XtRealizeWidget (widget);
8777 /* Set the cursor to an arrow. I didn't find a resource to do that.
8778 And I'm wondering why it hasn't an arrow cursor by default. */
8779 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8780 f->output_data.x->nontext_cursor);
8782 #else /* !USE_MOTIF i.e. use Xaw */
8784 /* Set resources. Create the widget. The background of the
8785 Xaw3d scroll bar widget is a little bit light for my taste.
8786 We don't alter it here to let users change it according
8787 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8788 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8789 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8790 /* For smoother scrolling with Xaw3d -sm */
8791 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8793 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8794 if (pixel != -1)
8796 XtSetArg (av[ac], XtNforeground, pixel);
8797 ++ac;
8800 pixel = f->output_data.x->scroll_bar_background_pixel;
8801 if (pixel != -1)
8803 XtSetArg (av[ac], XtNbackground, pixel);
8804 ++ac;
8807 /* Top/bottom shadow colors. */
8809 /* Allocate them, if necessary. */
8810 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8812 pixel = f->output_data.x->scroll_bar_background_pixel;
8813 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8814 &pixel, 1.2, 0x8000))
8815 pixel = -1;
8816 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8818 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8820 pixel = f->output_data.x->scroll_bar_background_pixel;
8821 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8822 &pixel, 0.6, 0x4000))
8823 pixel = -1;
8824 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8827 /* Tell the toolkit about them. */
8828 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8829 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8830 /* We tried to allocate a color for the top/bottom shadow, and
8831 failed, so tell Xaw3d to use dithering instead. */
8833 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8834 ++ac;
8836 else
8837 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8838 be more consistent with other emacs 3d colors, and since Xaw3d is
8839 not good at dealing with allocation failure. */
8841 /* This tells Xaw3d to use real colors instead of dithering for
8842 the shadows. */
8843 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8844 ++ac;
8846 /* Specify the colors. */
8847 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8848 if (pixel != -1)
8850 XtSetArg (av[ac], "topShadowPixel", pixel);
8851 ++ac;
8853 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8854 if (pixel != -1)
8856 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8857 ++ac;
8861 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8862 f->output_data.x->edit_widget, av, ac);
8865 char *initial = "";
8866 char *val = initial;
8867 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8868 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8869 if (val == initial)
8870 { /* ARROW_SCROLL */
8871 xaw3d_arrow_scroll = True;
8872 /* Isn't that just a personal preference ? -sm */
8873 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8877 /* Define callbacks. */
8878 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8879 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8880 (XtPointer) bar);
8882 /* Realize the widget. Only after that is the X window created. */
8883 XtRealizeWidget (widget);
8885 #endif /* !USE_MOTIF */
8887 /* Install an action hook that lets us detect when the user
8888 finishes interacting with a scroll bar. */
8889 if (action_hook_id == 0)
8890 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8892 /* Remember X window and widget in the scroll bar vector. */
8893 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8894 xwindow = XtWindow (widget);
8895 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8897 UNBLOCK_INPUT;
8899 #endif /* not USE_GTK */
8902 /* Set the thumb size and position of scroll bar BAR. We are currently
8903 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8905 #ifdef USE_GTK
8906 static void
8907 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8908 struct scroll_bar *bar;
8909 int portion, position, whole;
8911 xg_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8914 #else /* not USE_GTK */
8915 static void
8916 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8917 struct scroll_bar *bar;
8918 int portion, position, whole;
8920 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8921 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8922 float top, shown;
8924 BLOCK_INPUT;
8926 #ifdef USE_MOTIF
8928 /* We use an estimate of 30 chars per line rather than the real
8929 `portion' value. This has the disadvantage that the thumb size
8930 is not very representative, but it makes our life a lot easier.
8931 Otherwise, we have to constantly adjust the thumb size, which
8932 we can't always do quickly enough: while dragging, the size of
8933 the thumb might prevent the user from dragging the thumb all the
8934 way to the end. but Motif and some versions of Xaw3d don't allow
8935 updating the thumb size while dragging. Also, even if we can update
8936 its size, the update will often happen too late.
8937 If you don't believe it, check out revision 1.650 of xterm.c to see
8938 what hoops we were going through and the still poor behavior we got. */
8939 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
8940 /* When the thumb is at the bottom, position == whole.
8941 So we need to increase `whole' to make space for the thumb. */
8942 whole += portion;
8944 if (whole <= 0)
8945 top = 0, shown = 1;
8946 else
8948 top = (float) position / whole;
8949 shown = (float) portion / whole;
8952 if (NILP (bar->dragging))
8954 int size, value;
8956 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8957 is the scroll bar's maximum and MIN is the scroll bar's minimum
8958 value. */
8959 size = shown * XM_SB_RANGE;
8960 size = min (size, XM_SB_RANGE);
8961 size = max (size, 1);
8963 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8964 value = top * XM_SB_RANGE;
8965 value = min (value, XM_SB_MAX - size);
8966 value = max (value, XM_SB_MIN);
8968 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8970 #else /* !USE_MOTIF i.e. use Xaw */
8972 if (whole == 0)
8973 top = 0, shown = 1;
8974 else
8976 top = (float) position / whole;
8977 shown = (float) portion / whole;
8981 float old_top, old_shown;
8982 Dimension height;
8983 XtVaGetValues (widget,
8984 XtNtopOfThumb, &old_top,
8985 XtNshown, &old_shown,
8986 XtNheight, &height,
8987 NULL);
8989 /* Massage the top+shown values. */
8990 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8991 top = max (0, min (1, top));
8992 else
8993 top = old_top;
8994 /* Keep two pixels available for moving the thumb down. */
8995 shown = max (0, min (1 - top - (2.0 / height), shown));
8997 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8998 check that your system's configuration file contains a define
8999 for `NARROWPROTO'. See s/freebsd.h for an example. */
9000 if (top != old_top || shown != old_shown)
9002 if (NILP (bar->dragging))
9003 XawScrollbarSetThumb (widget, top, shown);
9004 else
9006 #ifdef HAVE_XAW3D
9007 ScrollbarWidget sb = (ScrollbarWidget) widget;
9008 int scroll_mode = 0;
9010 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
9011 if (xaw3d_arrow_scroll)
9013 /* Xaw3d stupidly ignores resize requests while dragging
9014 so we have to make it believe it's not in dragging mode. */
9015 scroll_mode = sb->scrollbar.scroll_mode;
9016 if (scroll_mode == 2)
9017 sb->scrollbar.scroll_mode = 0;
9019 #endif
9020 /* Try to make the scrolling a tad smoother. */
9021 if (!xaw3d_pick_top)
9022 shown = min (shown, old_shown);
9024 XawScrollbarSetThumb (widget, top, shown);
9026 #ifdef HAVE_XAW3D
9027 if (xaw3d_arrow_scroll && scroll_mode == 2)
9028 sb->scrollbar.scroll_mode = scroll_mode;
9029 #endif
9033 #endif /* !USE_MOTIF */
9035 UNBLOCK_INPUT;
9037 #endif /* not USE_GTK */
9039 #endif /* USE_TOOLKIT_SCROLL_BARS */
9043 /************************************************************************
9044 Scroll bars, general
9045 ************************************************************************/
9047 /* Create a scroll bar and return the scroll bar vector for it. W is
9048 the Emacs window on which to create the scroll bar. TOP, LEFT,
9049 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
9050 scroll bar. */
9052 static struct scroll_bar *
9053 x_scroll_bar_create (w, top, left, width, height)
9054 struct window *w;
9055 int top, left, width, height;
9057 struct frame *f = XFRAME (w->frame);
9058 struct scroll_bar *bar
9059 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
9061 BLOCK_INPUT;
9063 #ifdef USE_TOOLKIT_SCROLL_BARS
9064 x_create_toolkit_scroll_bar (f, bar);
9065 #else /* not USE_TOOLKIT_SCROLL_BARS */
9067 XSetWindowAttributes a;
9068 unsigned long mask;
9069 Window window;
9071 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9072 if (a.background_pixel == -1)
9073 a.background_pixel = f->output_data.x->background_pixel;
9075 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9076 | ButtonMotionMask | PointerMotionHintMask
9077 | ExposureMask);
9078 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
9080 mask = (CWBackPixel | CWEventMask | CWCursor);
9082 /* Clear the area of W that will serve as a scroll bar. This is
9083 for the case that a window has been split horizontally. In
9084 this case, no clear_frame is generated to reduce flickering. */
9085 if (width > 0 && height > 0)
9086 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9087 left, top, width,
9088 window_box_height (w), False);
9090 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9091 /* Position and size of scroll bar. */
9092 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9093 top,
9094 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9095 height,
9096 /* Border width, depth, class, and visual. */
9098 CopyFromParent,
9099 CopyFromParent,
9100 CopyFromParent,
9101 /* Attributes. */
9102 mask, &a);
9103 SET_SCROLL_BAR_X_WINDOW (bar, window);
9105 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9107 XSETWINDOW (bar->window, w);
9108 XSETINT (bar->top, top);
9109 XSETINT (bar->left, left);
9110 XSETINT (bar->width, width);
9111 XSETINT (bar->height, height);
9112 XSETINT (bar->start, 0);
9113 XSETINT (bar->end, 0);
9114 bar->dragging = Qnil;
9116 /* Add bar to its frame's list of scroll bars. */
9117 bar->next = FRAME_SCROLL_BARS (f);
9118 bar->prev = Qnil;
9119 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9120 if (!NILP (bar->next))
9121 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9123 /* Map the window/widget. */
9124 #ifdef USE_TOOLKIT_SCROLL_BARS
9126 #ifdef USE_GTK
9127 xg_update_scrollbar_pos (f,
9128 SCROLL_BAR_X_WINDOW (bar),
9129 top,
9130 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9131 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9132 max (height, 1));
9133 xg_show_scroll_bar (SCROLL_BAR_X_WINDOW (bar));
9134 #else /* not USE_GTK */
9135 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9136 XtConfigureWidget (scroll_bar,
9137 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9138 top,
9139 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9140 max (height, 1), 0);
9141 XtMapWidget (scroll_bar);
9142 #endif /* not USE_GTK */
9144 #else /* not USE_TOOLKIT_SCROLL_BARS */
9145 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9146 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9148 UNBLOCK_INPUT;
9149 return bar;
9153 /* Draw BAR's handle in the proper position.
9155 If the handle is already drawn from START to END, don't bother
9156 redrawing it, unless REBUILD is non-zero; in that case, always
9157 redraw it. (REBUILD is handy for drawing the handle after expose
9158 events.)
9160 Normally, we want to constrain the start and end of the handle to
9161 fit inside its rectangle, but if the user is dragging the scroll
9162 bar handle, we want to let them drag it down all the way, so that
9163 the bar's top is as far down as it goes; otherwise, there's no way
9164 to move to the very end of the buffer. */
9166 #ifndef USE_TOOLKIT_SCROLL_BARS
9168 static void
9169 x_scroll_bar_set_handle (bar, start, end, rebuild)
9170 struct scroll_bar *bar;
9171 int start, end;
9172 int rebuild;
9174 int dragging = ! NILP (bar->dragging);
9175 Window w = SCROLL_BAR_X_WINDOW (bar);
9176 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9177 GC gc = f->output_data.x->normal_gc;
9179 /* If the display is already accurate, do nothing. */
9180 if (! rebuild
9181 && start == XINT (bar->start)
9182 && end == XINT (bar->end))
9183 return;
9185 BLOCK_INPUT;
9188 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9189 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9190 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9192 /* Make sure the values are reasonable, and try to preserve
9193 the distance between start and end. */
9195 int length = end - start;
9197 if (start < 0)
9198 start = 0;
9199 else if (start > top_range)
9200 start = top_range;
9201 end = start + length;
9203 if (end < start)
9204 end = start;
9205 else if (end > top_range && ! dragging)
9206 end = top_range;
9209 /* Store the adjusted setting in the scroll bar. */
9210 XSETINT (bar->start, start);
9211 XSETINT (bar->end, end);
9213 /* Clip the end position, just for display. */
9214 if (end > top_range)
9215 end = top_range;
9217 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9218 below top positions, to make sure the handle is always at least
9219 that many pixels tall. */
9220 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9222 /* Draw the empty space above the handle. Note that we can't clear
9223 zero-height areas; that means "clear to end of window." */
9224 if (0 < start)
9225 x_clear_area (FRAME_X_DISPLAY (f), w,
9226 /* x, y, width, height, and exposures. */
9227 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9228 VERTICAL_SCROLL_BAR_TOP_BORDER,
9229 inside_width, start,
9230 False);
9232 /* Change to proper foreground color if one is specified. */
9233 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9234 XSetForeground (FRAME_X_DISPLAY (f), gc,
9235 f->output_data.x->scroll_bar_foreground_pixel);
9237 /* Draw the handle itself. */
9238 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9239 /* x, y, width, height */
9240 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9241 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9242 inside_width, end - start);
9244 /* Restore the foreground color of the GC if we changed it above. */
9245 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9246 XSetForeground (FRAME_X_DISPLAY (f), gc,
9247 f->output_data.x->foreground_pixel);
9249 /* Draw the empty space below the handle. Note that we can't
9250 clear zero-height areas; that means "clear to end of window." */
9251 if (end < inside_height)
9252 x_clear_area (FRAME_X_DISPLAY (f), w,
9253 /* x, y, width, height, and exposures. */
9254 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9255 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9256 inside_width, inside_height - end,
9257 False);
9261 UNBLOCK_INPUT;
9264 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9266 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9267 nil. */
9269 static void
9270 x_scroll_bar_remove (bar)
9271 struct scroll_bar *bar;
9273 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9274 BLOCK_INPUT;
9276 #ifdef USE_TOOLKIT_SCROLL_BARS
9277 #ifdef USE_GTK
9278 xg_remove_scroll_bar (f, SCROLL_BAR_X_WINDOW (bar));
9279 #else /* not USE_GTK */
9280 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9281 #endif /* not USE_GTK */
9282 #else
9283 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9284 #endif
9286 /* Disassociate this scroll bar from its window. */
9287 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9289 UNBLOCK_INPUT;
9293 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9294 that we are displaying PORTION characters out of a total of WHOLE
9295 characters, starting at POSITION. If WINDOW has no scroll bar,
9296 create one. */
9298 static void
9299 XTset_vertical_scroll_bar (w, portion, whole, position)
9300 struct window *w;
9301 int portion, whole, position;
9303 struct frame *f = XFRAME (w->frame);
9304 struct scroll_bar *bar;
9305 int top, height, left, sb_left, width, sb_width;
9306 int window_x, window_y, window_width, window_height;
9308 /* Get window dimensions. */
9309 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9310 top = window_y;
9311 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9312 height = window_height;
9314 /* Compute the left edge of the scroll bar area. */
9315 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9316 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9317 else
9318 left = XFASTINT (w->left);
9319 left *= CANON_X_UNIT (f);
9320 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9322 /* Compute the width of the scroll bar which might be less than
9323 the width of the area reserved for the scroll bar. */
9324 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9325 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9326 else
9327 sb_width = width;
9329 /* Compute the left edge of the scroll bar. */
9330 #ifdef USE_TOOLKIT_SCROLL_BARS
9331 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9332 sb_left = left + width - sb_width - (width - sb_width) / 2;
9333 else
9334 sb_left = left + (width - sb_width) / 2;
9335 #else
9336 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9337 sb_left = left + width - sb_width;
9338 else
9339 sb_left = left;
9340 #endif
9342 /* Does the scroll bar exist yet? */
9343 if (NILP (w->vertical_scroll_bar))
9345 if (width > 0 && height > 0)
9347 BLOCK_INPUT;
9348 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9349 left, top, width, height, False);
9350 UNBLOCK_INPUT;
9353 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9355 else
9357 /* It may just need to be moved and resized. */
9358 unsigned int mask = 0;
9360 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9362 BLOCK_INPUT;
9364 if (sb_left != XINT (bar->left))
9365 mask |= CWX;
9366 if (top != XINT (bar->top))
9367 mask |= CWY;
9368 if (sb_width != XINT (bar->width))
9369 mask |= CWWidth;
9370 if (height != XINT (bar->height))
9371 mask |= CWHeight;
9373 #ifdef USE_TOOLKIT_SCROLL_BARS
9375 #ifdef USE_GTK
9376 if (mask)
9377 xg_update_scrollbar_pos (f,
9378 SCROLL_BAR_X_WINDOW (bar),
9379 top,
9380 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9381 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9382 max (height, 1));
9383 #else /* not USE_GTK */
9385 /* Since toolkit scroll bars are smaller than the space reserved
9386 for them on the frame, we have to clear "under" them. */
9387 if (width > 0 && height > 0)
9388 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9389 left, top, width, height, False);
9390 /* Move/size the scroll bar widget. */
9391 if (mask)
9392 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9393 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9394 top,
9395 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9396 max (height, 1), 0);
9398 #endif /* not USE_GTK */
9399 #else /* not USE_TOOLKIT_SCROLL_BARS */
9401 /* Clear areas not covered by the scroll bar because of
9402 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9403 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9405 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9406 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9407 height, False);
9408 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9409 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9410 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9411 height, False);
9414 /* Clear areas not covered by the scroll bar because it's not as
9415 wide as the area reserved for it. This makes sure a
9416 previous mode line display is cleared after C-x 2 C-x 1, for
9417 example. */
9419 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9420 int rest = area_width - sb_width;
9421 if (rest > 0 && height > 0)
9423 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9424 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9425 left + area_width - rest, top,
9426 rest, height, False);
9427 else
9428 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9429 left, top, rest, height, False);
9433 /* Move/size the scroll bar window. */
9434 if (mask)
9436 XWindowChanges wc;
9438 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9439 wc.y = top;
9440 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9441 wc.height = height;
9442 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9443 mask, &wc);
9446 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9448 /* Remember new settings. */
9449 XSETINT (bar->left, sb_left);
9450 XSETINT (bar->top, top);
9451 XSETINT (bar->width, sb_width);
9452 XSETINT (bar->height, height);
9454 UNBLOCK_INPUT;
9457 #ifdef USE_TOOLKIT_SCROLL_BARS
9458 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9459 #else /* not USE_TOOLKIT_SCROLL_BARS */
9460 /* Set the scroll bar's current state, unless we're currently being
9461 dragged. */
9462 if (NILP (bar->dragging))
9464 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9466 if (whole == 0)
9467 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9468 else
9470 int start = ((double) position * top_range) / whole;
9471 int end = ((double) (position + portion) * top_range) / whole;
9472 x_scroll_bar_set_handle (bar, start, end, 0);
9475 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9477 XSETVECTOR (w->vertical_scroll_bar, bar);
9481 /* The following three hooks are used when we're doing a thorough
9482 redisplay of the frame. We don't explicitly know which scroll bars
9483 are going to be deleted, because keeping track of when windows go
9484 away is a real pain - "Can you say set-window-configuration, boys
9485 and girls?" Instead, we just assert at the beginning of redisplay
9486 that *all* scroll bars are to be removed, and then save a scroll bar
9487 from the fiery pit when we actually redisplay its window. */
9489 /* Arrange for all scroll bars on FRAME to be removed at the next call
9490 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9491 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9493 static void
9494 XTcondemn_scroll_bars (frame)
9495 FRAME_PTR frame;
9497 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9498 while (! NILP (FRAME_SCROLL_BARS (frame)))
9500 Lisp_Object bar;
9501 bar = FRAME_SCROLL_BARS (frame);
9502 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9503 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9504 XSCROLL_BAR (bar)->prev = Qnil;
9505 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9506 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9507 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9512 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9513 Note that WINDOW isn't necessarily condemned at all. */
9515 static void
9516 XTredeem_scroll_bar (window)
9517 struct window *window;
9519 struct scroll_bar *bar;
9520 struct frame *f;
9522 /* We can't redeem this window's scroll bar if it doesn't have one. */
9523 if (NILP (window->vertical_scroll_bar))
9524 abort ();
9526 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9528 /* Unlink it from the condemned list. */
9529 f = XFRAME (WINDOW_FRAME (window));
9530 if (NILP (bar->prev))
9532 /* If the prev pointer is nil, it must be the first in one of
9533 the lists. */
9534 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9535 /* It's not condemned. Everything's fine. */
9536 return;
9537 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9538 window->vertical_scroll_bar))
9539 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9540 else
9541 /* If its prev pointer is nil, it must be at the front of
9542 one or the other! */
9543 abort ();
9545 else
9546 XSCROLL_BAR (bar->prev)->next = bar->next;
9548 if (! NILP (bar->next))
9549 XSCROLL_BAR (bar->next)->prev = bar->prev;
9551 bar->next = FRAME_SCROLL_BARS (f);
9552 bar->prev = Qnil;
9553 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9554 if (! NILP (bar->next))
9555 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9558 /* Remove all scroll bars on FRAME that haven't been saved since the
9559 last call to `*condemn_scroll_bars_hook'. */
9561 static void
9562 XTjudge_scroll_bars (f)
9563 FRAME_PTR f;
9565 Lisp_Object bar, next;
9567 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9569 /* Clear out the condemned list now so we won't try to process any
9570 more events on the hapless scroll bars. */
9571 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9573 for (; ! NILP (bar); bar = next)
9575 struct scroll_bar *b = XSCROLL_BAR (bar);
9577 x_scroll_bar_remove (b);
9579 next = b->next;
9580 b->next = b->prev = Qnil;
9583 /* Now there should be no references to the condemned scroll bars,
9584 and they should get garbage-collected. */
9588 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9589 is a no-op when using toolkit scroll bars.
9591 This may be called from a signal handler, so we have to ignore GC
9592 mark bits. */
9594 static void
9595 x_scroll_bar_expose (bar, event)
9596 struct scroll_bar *bar;
9597 XEvent *event;
9599 #ifndef USE_TOOLKIT_SCROLL_BARS
9601 Window w = SCROLL_BAR_X_WINDOW (bar);
9602 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9603 GC gc = f->output_data.x->normal_gc;
9604 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9606 BLOCK_INPUT;
9608 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9610 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9611 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9613 /* x, y, width, height */
9614 0, 0,
9615 XINT (bar->width) - 1 - width_trim - width_trim,
9616 XINT (bar->height) - 1);
9618 UNBLOCK_INPUT;
9620 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9623 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9624 is set to something other than NO_EVENT, it is enqueued.
9626 This may be called from a signal handler, so we have to ignore GC
9627 mark bits. */
9630 static void
9631 x_scroll_bar_handle_click (bar, event, emacs_event)
9632 struct scroll_bar *bar;
9633 XEvent *event;
9634 struct input_event *emacs_event;
9636 if (! GC_WINDOWP (bar->window))
9637 abort ();
9639 emacs_event->kind = SCROLL_BAR_CLICK_EVENT;
9640 emacs_event->code = event->xbutton.button - Button1;
9641 emacs_event->modifiers
9642 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9643 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9644 event->xbutton.state)
9645 | (event->type == ButtonRelease
9646 ? up_modifier
9647 : down_modifier));
9648 emacs_event->frame_or_window = bar->window;
9649 emacs_event->arg = Qnil;
9650 emacs_event->timestamp = event->xbutton.time;
9652 #if 0
9653 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9654 int internal_height
9655 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9656 #endif
9657 int top_range
9658 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9659 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9661 if (y < 0) y = 0;
9662 if (y > top_range) y = top_range;
9664 if (y < XINT (bar->start))
9665 emacs_event->part = scroll_bar_above_handle;
9666 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9667 emacs_event->part = scroll_bar_handle;
9668 else
9669 emacs_event->part = scroll_bar_below_handle;
9671 /* Just because the user has clicked on the handle doesn't mean
9672 they want to drag it. Lisp code needs to be able to decide
9673 whether or not we're dragging. */
9674 #if 0
9675 /* If the user has just clicked on the handle, record where they're
9676 holding it. */
9677 if (event->type == ButtonPress
9678 && emacs_event->part == scroll_bar_handle)
9679 XSETINT (bar->dragging, y - XINT (bar->start));
9680 #endif
9682 #ifndef USE_TOOLKIT_SCROLL_BARS
9683 /* If the user has released the handle, set it to its final position. */
9684 if (event->type == ButtonRelease
9685 && ! NILP (bar->dragging))
9687 int new_start = y - XINT (bar->dragging);
9688 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9690 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9691 bar->dragging = Qnil;
9693 #endif
9695 /* Same deal here as the other #if 0. */
9696 #if 0
9697 /* Clicks on the handle are always reported as occurring at the top of
9698 the handle. */
9699 if (emacs_event->part == scroll_bar_handle)
9700 emacs_event->x = bar->start;
9701 else
9702 XSETINT (emacs_event->x, y);
9703 #else
9704 XSETINT (emacs_event->x, y);
9705 #endif
9707 XSETINT (emacs_event->y, top_range);
9711 #ifndef USE_TOOLKIT_SCROLL_BARS
9713 /* Handle some mouse motion while someone is dragging the scroll bar.
9715 This may be called from a signal handler, so we have to ignore GC
9716 mark bits. */
9718 static void
9719 x_scroll_bar_note_movement (bar, event)
9720 struct scroll_bar *bar;
9721 XEvent *event;
9723 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9725 last_mouse_movement_time = event->xmotion.time;
9727 f->mouse_moved = 1;
9728 XSETVECTOR (last_mouse_scroll_bar, bar);
9730 /* If we're dragging the bar, display it. */
9731 if (! GC_NILP (bar->dragging))
9733 /* Where should the handle be now? */
9734 int new_start = event->xmotion.y - XINT (bar->dragging);
9736 if (new_start != XINT (bar->start))
9738 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9740 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9745 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9747 /* Return information to the user about the current position of the mouse
9748 on the scroll bar. */
9750 static void
9751 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9752 FRAME_PTR *fp;
9753 Lisp_Object *bar_window;
9754 enum scroll_bar_part *part;
9755 Lisp_Object *x, *y;
9756 unsigned long *time;
9758 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9759 Window w = SCROLL_BAR_X_WINDOW (bar);
9760 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9761 int win_x, win_y;
9762 Window dummy_window;
9763 int dummy_coord;
9764 unsigned int dummy_mask;
9766 BLOCK_INPUT;
9768 /* Get the mouse's position relative to the scroll bar window, and
9769 report that. */
9770 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9772 /* Root, child, root x and root y. */
9773 &dummy_window, &dummy_window,
9774 &dummy_coord, &dummy_coord,
9776 /* Position relative to scroll bar. */
9777 &win_x, &win_y,
9779 /* Mouse buttons and modifier keys. */
9780 &dummy_mask))
9782 else
9784 #if 0
9785 int inside_height
9786 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9787 #endif
9788 int top_range
9789 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9791 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9793 if (! NILP (bar->dragging))
9794 win_y -= XINT (bar->dragging);
9796 if (win_y < 0)
9797 win_y = 0;
9798 if (win_y > top_range)
9799 win_y = top_range;
9801 *fp = f;
9802 *bar_window = bar->window;
9804 if (! NILP (bar->dragging))
9805 *part = scroll_bar_handle;
9806 else if (win_y < XINT (bar->start))
9807 *part = scroll_bar_above_handle;
9808 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9809 *part = scroll_bar_handle;
9810 else
9811 *part = scroll_bar_below_handle;
9813 XSETINT (*x, win_y);
9814 XSETINT (*y, top_range);
9816 f->mouse_moved = 0;
9817 last_mouse_scroll_bar = Qnil;
9820 *time = last_mouse_movement_time;
9822 UNBLOCK_INPUT;
9826 /* The screen has been cleared so we may have changed foreground or
9827 background colors, and the scroll bars may need to be redrawn.
9828 Clear out the scroll bars, and ask for expose events, so we can
9829 redraw them. */
9831 void
9832 x_scroll_bar_clear (f)
9833 FRAME_PTR f;
9835 #ifndef USE_TOOLKIT_SCROLL_BARS
9836 Lisp_Object bar;
9838 /* We can have scroll bars even if this is 0,
9839 if we just turned off scroll bar mode.
9840 But in that case we should not clear them. */
9841 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9842 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9843 bar = XSCROLL_BAR (bar)->next)
9844 XClearArea (FRAME_X_DISPLAY (f),
9845 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9846 0, 0, 0, 0, True);
9847 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9851 /* Define a queue to save up SelectionRequest events for later handling. */
9853 struct selection_event_queue
9855 XEvent event;
9856 struct selection_event_queue *next;
9859 static struct selection_event_queue *queue;
9861 /* Nonzero means queue up certain events--don't process them yet. */
9863 static int x_queue_selection_requests;
9865 /* Queue up an X event *EVENT, to be processed later. */
9867 static void
9868 x_queue_event (f, event)
9869 FRAME_PTR f;
9870 XEvent *event;
9872 struct selection_event_queue *queue_tmp
9873 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9875 if (queue_tmp != NULL)
9877 queue_tmp->event = *event;
9878 queue_tmp->next = queue;
9879 queue = queue_tmp;
9883 /* Take all the queued events and put them back
9884 so that they get processed afresh. */
9886 static void
9887 x_unqueue_events (display)
9888 Display *display;
9890 while (queue != NULL)
9892 struct selection_event_queue *queue_tmp = queue;
9893 XPutBackEvent (display, &queue_tmp->event);
9894 queue = queue_tmp->next;
9895 xfree ((char *)queue_tmp);
9899 /* Start queuing SelectionRequest events. */
9901 void
9902 x_start_queuing_selection_requests (display)
9903 Display *display;
9905 x_queue_selection_requests++;
9908 /* Stop queuing SelectionRequest events. */
9910 void
9911 x_stop_queuing_selection_requests (display)
9912 Display *display;
9914 x_queue_selection_requests--;
9915 x_unqueue_events (display);
9918 /* The main X event-reading loop - XTread_socket. */
9920 #if 0
9921 /* Time stamp of enter window event. This is only used by XTread_socket,
9922 but we have to put it out here, since static variables within functions
9923 sometimes don't work. */
9925 static Time enter_timestamp;
9926 #endif
9928 /* This holds the state XLookupString needs to implement dead keys
9929 and other tricks known as "compose processing". _X Window System_
9930 says that a portable program can't use this, but Stephen Gildea assures
9931 me that letting the compiler initialize it to zeros will work okay.
9933 This must be defined outside of XTread_socket, for the same reasons
9934 given for enter_timestamp, above. */
9936 static XComposeStatus compose_status;
9938 /* Record the last 100 characters stored
9939 to help debug the loss-of-chars-during-GC problem. */
9941 static int temp_index;
9942 static short temp_buffer[100];
9944 /* Set this to nonzero to fake an "X I/O error"
9945 on a particular display. */
9947 struct x_display_info *XTread_socket_fake_io_error;
9949 /* When we find no input here, we occasionally do a no-op command
9950 to verify that the X server is still running and we can still talk with it.
9951 We try all the open displays, one by one.
9952 This variable is used for cycling thru the displays. */
9954 static struct x_display_info *next_noop_dpyinfo;
9956 #define SET_SAVED_MENU_EVENT(size) \
9957 do \
9959 if (f->output_data.x->saved_menu_event == 0) \
9960 f->output_data.x->saved_menu_event \
9961 = (XEvent *) xmalloc (sizeof (XEvent)); \
9962 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9963 if (numchars >= 1) \
9965 bufp->kind = MENU_BAR_ACTIVATE_EVENT; \
9966 XSETFRAME (bufp->frame_or_window, f); \
9967 bufp->arg = Qnil; \
9968 bufp++; \
9969 count++; \
9970 numchars--; \
9973 while (0)
9975 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9976 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9979 enum
9981 X_EVENT_NORMAL,
9982 X_EVENT_GOTO_OUT,
9983 X_EVENT_DROP
9986 /* Filter events for the current X input method.
9987 DPYINFO is the display this event is for.
9988 EVENT is the X event to filter.
9990 Returns non-zero if the event was filtered, caller shall not process
9991 this event further.
9992 Returns zero if event is wasn't filtered. */
9994 #ifdef HAVE_X_I18N
9995 static int
9996 x_filter_event (dpyinfo, event)
9997 struct x_display_info *dpyinfo;
9998 XEvent *event;
10000 /* XFilterEvent returns non-zero if the input method has
10001 consumed the event. We pass the frame's X window to
10002 XFilterEvent because that's the one for which the IC
10003 was created. */
10005 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10006 event->xclient.window);
10008 return XFilterEvent (event, f1 ? FRAME_X_WINDOW (f1) : None);
10010 #endif
10012 #ifdef USE_GTK
10013 static struct x_display_info *current_dpyinfo;
10014 static struct input_event **current_bufp;
10015 static int *current_numcharsp;
10016 static int current_count;
10017 static int current_finish;
10019 /* This is the filter function invoked by the GTK event loop.
10020 It is invoked before the XEvent is translated to a GdkEvent,
10021 so we have a chanse to act on the event before GTK. */
10022 static GdkFilterReturn
10023 event_handler_gdk (gxev, ev, data)
10024 GdkXEvent *gxev;
10025 GdkEvent *ev;
10026 gpointer data;
10028 XEvent *xev = (XEvent*)gxev;
10030 if (current_numcharsp)
10032 #ifdef HAVE_X_I18N
10033 /* Filter events for the current X input method.
10034 GTK calls XFilterEvent but not for key press and release,
10035 so we do it here. */
10036 if (xev->type == KeyPress || xev->type == KeyRelease)
10037 if (x_filter_event (current_dpyinfo, xev))
10038 return GDK_FILTER_REMOVE;
10039 #endif
10040 current_count += handle_one_xevent (current_dpyinfo,
10041 xev,
10042 current_bufp,
10043 current_numcharsp,
10044 &current_finish);
10046 else
10047 current_finish = x_dispatch_event (xev, GDK_DISPLAY ());
10049 if (current_finish == X_EVENT_GOTO_OUT || current_finish == X_EVENT_DROP)
10050 return GDK_FILTER_REMOVE;
10052 return GDK_FILTER_CONTINUE;
10054 #endif /* USE_GTK */
10057 /* Handles the XEvent EVENT on display DPYINFO.
10059 *FINISH is X_EVENT_GOTO_OUT if caller should stop reading events.
10060 *FINISH is zero if caller should continue reading events.
10061 *FINISH is X_EVENT_DROP if event should not be passed to the toolkit.
10063 Events representing keys are stored in buffer *BUFP_R,
10064 which can hold up to *NUMCHARSP characters.
10065 We return the number of characters stored into the buffer. */
10067 static int
10068 handle_one_xevent (dpyinfo, eventp, bufp_r, numcharsp, finish)
10069 struct x_display_info *dpyinfo;
10070 XEvent *eventp;
10071 /* register */ struct input_event **bufp_r;
10072 /* register */ int *numcharsp;
10073 int *finish;
10075 int count = 0;
10076 int nbytes = 0;
10077 struct frame *f;
10078 struct coding_system coding;
10079 struct input_event *bufp = *bufp_r;
10080 int numchars = *numcharsp;
10081 XEvent event = *eventp;
10083 *finish = X_EVENT_NORMAL;
10085 switch (event.type)
10087 case ClientMessage:
10089 if (event.xclient.message_type
10090 == dpyinfo->Xatom_wm_protocols
10091 && event.xclient.format == 32)
10093 if (event.xclient.data.l[0]
10094 == dpyinfo->Xatom_wm_take_focus)
10096 /* Use x_any_window_to_frame because this
10097 could be the shell widget window
10098 if the frame has no title bar. */
10099 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10100 #ifdef HAVE_X_I18N
10101 /* Not quite sure this is needed -pd */
10102 if (f && FRAME_XIC (f))
10103 XSetICFocus (FRAME_XIC (f));
10104 #endif
10105 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10106 instructs the WM to set the input focus automatically for
10107 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10108 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10109 it has set the focus. So, XSetInputFocus below is not
10110 needed.
10112 The call to XSetInputFocus below has also caused trouble. In
10113 cases where the XSetInputFocus done by the WM and the one
10114 below are temporally close (on a fast machine), the call
10115 below can generate additional FocusIn events which confuse
10116 Emacs. */
10118 /* Since we set WM_TAKE_FOCUS, we must call
10119 XSetInputFocus explicitly. But not if f is null,
10120 since that might be an event for a deleted frame. */
10121 if (f)
10123 Display *d = event.xclient.display;
10124 /* Catch and ignore errors, in case window has been
10125 iconified by a window manager such as GWM. */
10126 int count = x_catch_errors (d);
10127 XSetInputFocus (d, event.xclient.window,
10128 /* The ICCCM says this is
10129 the only valid choice. */
10130 RevertToParent,
10131 event.xclient.data.l[1]);
10132 /* This is needed to detect the error
10133 if there is an error. */
10134 XSync (d, False);
10135 x_uncatch_errors (d, count);
10137 /* Not certain about handling scroll bars here */
10138 #endif /* 0 */
10140 else if (event.xclient.data.l[0]
10141 == dpyinfo->Xatom_wm_save_yourself)
10143 /* Save state modify the WM_COMMAND property to
10144 something which can reinstate us. This notifies
10145 the session manager, who's looking for such a
10146 PropertyNotify. Can restart processing when
10147 a keyboard or mouse event arrives. */
10148 /* If we have a session manager, don't set this.
10149 KDE will then start two Emacsen, one for the
10150 session manager and one for this. */
10151 if (numchars > 0
10152 #ifdef HAVE_X_SM
10153 && ! x_session_have_connection ()
10154 #endif
10157 f = x_top_window_to_frame (dpyinfo,
10158 event.xclient.window);
10159 /* This is just so we only give real data once
10160 for a single Emacs process. */
10161 if (f == SELECTED_FRAME ())
10162 XSetCommand (FRAME_X_DISPLAY (f),
10163 event.xclient.window,
10164 initial_argv, initial_argc);
10165 else if (f)
10166 XSetCommand (FRAME_X_DISPLAY (f),
10167 event.xclient.window,
10168 0, 0);
10171 else if (event.xclient.data.l[0]
10172 == dpyinfo->Xatom_wm_delete_window)
10174 struct frame *f
10175 = x_any_window_to_frame (dpyinfo,
10176 event.xclient.window);
10178 if (f)
10180 if (numchars == 0)
10181 abort ();
10183 bufp->kind = DELETE_WINDOW_EVENT;
10184 XSETFRAME (bufp->frame_or_window, f);
10185 bufp->arg = Qnil;
10186 bufp++;
10188 count += 1;
10189 numchars -= 1;
10191 else
10192 goto OTHER; /* May be a dialog that is to be removed */
10195 else if (event.xclient.message_type
10196 == dpyinfo->Xatom_wm_configure_denied)
10199 else if (event.xclient.message_type
10200 == dpyinfo->Xatom_wm_window_moved)
10202 int new_x, new_y;
10203 struct frame *f
10204 = x_window_to_frame (dpyinfo, event.xclient.window);
10206 new_x = event.xclient.data.s[0];
10207 new_y = event.xclient.data.s[1];
10209 if (f)
10211 f->output_data.x->left_pos = new_x;
10212 f->output_data.x->top_pos = new_y;
10215 #ifdef HACK_EDITRES
10216 else if (event.xclient.message_type
10217 == dpyinfo->Xatom_editres)
10219 struct frame *f
10220 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10221 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10222 &event, NULL);
10224 #endif /* HACK_EDITRES */
10225 else if ((event.xclient.message_type
10226 == dpyinfo->Xatom_DONE)
10227 || (event.xclient.message_type
10228 == dpyinfo->Xatom_PAGE))
10230 /* Ghostview job completed. Kill it. We could
10231 reply with "Next" if we received "Page", but we
10232 currently never do because we are interested in
10233 images, only, which should have 1 page. */
10234 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10235 struct frame *f
10236 = x_window_to_frame (dpyinfo, event.xclient.window);
10237 x_kill_gs_process (pixmap, f);
10238 expose_frame (f, 0, 0, 0, 0);
10240 #ifdef USE_TOOLKIT_SCROLL_BARS
10241 /* Scroll bar callbacks send a ClientMessage from which
10242 we construct an input_event. */
10243 else if (event.xclient.message_type
10244 == dpyinfo->Xatom_Scrollbar)
10246 x_scroll_bar_to_input_event (&event, bufp);
10247 ++bufp, ++count, --numchars;
10248 goto out;
10250 #endif /* USE_TOOLKIT_SCROLL_BARS */
10251 else
10252 goto OTHER;
10254 break;
10256 case SelectionNotify:
10257 #ifdef USE_X_TOOLKIT
10258 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10259 goto OTHER;
10260 #endif /* not USE_X_TOOLKIT */
10261 x_handle_selection_notify (&event.xselection);
10262 break;
10264 case SelectionClear: /* Someone has grabbed ownership. */
10265 #ifdef USE_X_TOOLKIT
10266 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10267 goto OTHER;
10268 #endif /* USE_X_TOOLKIT */
10270 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10272 if (numchars == 0)
10273 abort ();
10275 bufp->kind = SELECTION_CLEAR_EVENT;
10276 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10277 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10278 SELECTION_EVENT_TIME (bufp) = eventp->time;
10279 bufp->frame_or_window = Qnil;
10280 bufp->arg = Qnil;
10281 bufp++;
10283 count += 1;
10284 numchars -= 1;
10286 break;
10288 case SelectionRequest: /* Someone wants our selection. */
10289 #ifdef USE_X_TOOLKIT
10290 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10291 goto OTHER;
10292 #endif /* USE_X_TOOLKIT */
10293 if (x_queue_selection_requests)
10294 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10295 &event);
10296 else
10298 XSelectionRequestEvent *eventp
10299 = (XSelectionRequestEvent *) &event;
10301 if (numchars == 0)
10302 abort ();
10304 bufp->kind = SELECTION_REQUEST_EVENT;
10305 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10306 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10307 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10308 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10309 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10310 SELECTION_EVENT_TIME (bufp) = eventp->time;
10311 bufp->frame_or_window = Qnil;
10312 bufp->arg = Qnil;
10313 bufp++;
10315 count += 1;
10316 numchars -= 1;
10318 break;
10320 case PropertyNotify:
10321 #if 0 /* This is plain wrong. In the case that we are waiting for a
10322 PropertyNotify used as an ACK in incremental selection
10323 transfer, the property will be on the receiver's window. */
10324 #if defined USE_X_TOOLKIT
10325 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10326 goto OTHER;
10327 #endif
10328 #endif
10329 x_handle_property_notify (&event.xproperty);
10330 goto OTHER;
10332 case ReparentNotify:
10333 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10334 if (f)
10336 int x, y;
10337 f->output_data.x->parent_desc = event.xreparent.parent;
10338 x_real_positions (f, &x, &y);
10339 f->output_data.x->left_pos = x;
10340 f->output_data.x->top_pos = y;
10342 goto OTHER;
10343 break;
10345 case Expose:
10346 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10347 if (f)
10349 x_check_fullscreen (f);
10351 if (f->async_visible == 0)
10353 f->async_visible = 1;
10354 f->async_iconified = 0;
10355 f->output_data.x->has_been_visible = 1;
10356 SET_FRAME_GARBAGED (f);
10358 else
10359 expose_frame (x_window_to_frame (dpyinfo,
10360 event.xexpose.window),
10361 event.xexpose.x, event.xexpose.y,
10362 event.xexpose.width, event.xexpose.height);
10364 else
10366 #ifndef USE_TOOLKIT_SCROLL_BARS
10367 struct scroll_bar *bar;
10368 #endif
10369 #if defined USE_LUCID
10370 /* Submenus of the Lucid menu bar aren't widgets
10371 themselves, so there's no way to dispatch events
10372 to them. Recognize this case separately. */
10374 Widget widget
10375 = x_window_to_menu_bar (event.xexpose.window);
10376 if (widget)
10377 xlwmenu_redisplay (widget);
10379 #endif /* USE_LUCID */
10381 #ifdef USE_TOOLKIT_SCROLL_BARS
10382 /* Dispatch event to the widget. */
10383 goto OTHER;
10384 #else /* not USE_TOOLKIT_SCROLL_BARS */
10385 bar = x_window_to_scroll_bar (event.xexpose.window);
10387 if (bar)
10388 x_scroll_bar_expose (bar, &event);
10389 #ifdef USE_X_TOOLKIT
10390 else
10391 goto OTHER;
10392 #endif /* USE_X_TOOLKIT */
10393 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10395 break;
10397 case GraphicsExpose: /* This occurs when an XCopyArea's
10398 source area was obscured or not
10399 available. */
10400 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10401 if (f)
10403 expose_frame (f,
10404 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10405 event.xgraphicsexpose.width,
10406 event.xgraphicsexpose.height);
10408 #ifdef USE_X_TOOLKIT
10409 else
10410 goto OTHER;
10411 #endif /* USE_X_TOOLKIT */
10412 break;
10414 case NoExpose: /* This occurs when an XCopyArea's
10415 source area was completely
10416 available. */
10417 break;
10419 case UnmapNotify:
10420 /* Redo the mouse-highlight after the tooltip has gone. */
10421 if (event.xmap.window == tip_window)
10423 tip_window = 0;
10424 redo_mouse_highlight ();
10427 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10428 if (f) /* F may no longer exist if
10429 the frame was deleted. */
10431 /* While a frame is unmapped, display generation is
10432 disabled; you don't want to spend time updating a
10433 display that won't ever be seen. */
10434 f->async_visible = 0;
10435 /* We can't distinguish, from the event, whether the window
10436 has become iconified or invisible. So assume, if it
10437 was previously visible, than now it is iconified.
10438 But x_make_frame_invisible clears both
10439 the visible flag and the iconified flag;
10440 and that way, we know the window is not iconified now. */
10441 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10443 f->async_iconified = 1;
10445 bufp->kind = ICONIFY_EVENT;
10446 XSETFRAME (bufp->frame_or_window, f);
10447 bufp->arg = Qnil;
10448 bufp++;
10449 count++;
10450 numchars--;
10453 goto OTHER;
10455 case MapNotify:
10456 if (event.xmap.window == tip_window)
10457 /* The tooltip has been drawn already. Avoid
10458 the SET_FRAME_GARBAGED below. */
10459 goto OTHER;
10461 /* We use x_top_window_to_frame because map events can
10462 come for sub-windows and they don't mean that the
10463 frame is visible. */
10464 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10465 if (f)
10467 /* wait_reading_process_input will notice this and update
10468 the frame's display structures.
10469 If we where iconified, we should not set garbaged,
10470 because that stops redrawing on Expose events. This looks
10471 bad if we are called from a recursive event loop
10472 (x_dispatch_event), for example when a dialog is up. */
10473 if (! f->async_iconified)
10474 SET_FRAME_GARBAGED (f);
10476 f->async_visible = 1;
10477 f->async_iconified = 0;
10478 f->output_data.x->has_been_visible = 1;
10480 if (f->iconified)
10482 bufp->kind = DEICONIFY_EVENT;
10483 XSETFRAME (bufp->frame_or_window, f);
10484 bufp->arg = Qnil;
10485 bufp++;
10486 count++;
10487 numchars--;
10489 else if (! NILP (Vframe_list)
10490 && ! NILP (XCDR (Vframe_list)))
10491 /* Force a redisplay sooner or later
10492 to update the frame titles
10493 in case this is the second frame. */
10494 record_asynch_buffer_change ();
10496 goto OTHER;
10498 case KeyPress:
10500 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
10501 /* Dispatch KeyPress events when in menu. */
10502 if (popup_activated ())
10503 goto OTHER;
10504 #endif
10506 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10508 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10510 dpyinfo->mouse_face_hidden = 1;
10511 clear_mouse_face (dpyinfo);
10514 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10515 if (f == 0)
10517 /* Scroll bars consume key events, but we want
10518 the keys to go to the scroll bar's frame. */
10519 Widget widget = XtWindowToWidget (dpyinfo->display,
10520 event.xkey.window);
10521 if (widget && XmIsScrollBar (widget))
10523 widget = XtParent (widget);
10524 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10527 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10529 if (f != 0)
10531 KeySym keysym, orig_keysym;
10532 /* al%imercury@uunet.uu.net says that making this 81
10533 instead of 80 fixed a bug whereby meta chars made
10534 his Emacs hang.
10536 It seems that some version of XmbLookupString has
10537 a bug of not returning XBufferOverflow in
10538 status_return even if the input is too long to
10539 fit in 81 bytes. So, we must prepare sufficient
10540 bytes for copy_buffer. 513 bytes (256 chars for
10541 two-byte character set) seems to be a fairly good
10542 approximation. -- 2000.8.10 handa@etl.go.jp */
10543 unsigned char copy_buffer[513];
10544 unsigned char *copy_bufptr = copy_buffer;
10545 int copy_bufsiz = sizeof (copy_buffer);
10546 int modifiers;
10547 Lisp_Object coding_system = Qlatin_1;
10549 event.xkey.state
10550 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10551 extra_keyboard_modifiers);
10552 modifiers = event.xkey.state;
10554 /* This will have to go some day... */
10556 /* make_lispy_event turns chars into control chars.
10557 Don't do it here because XLookupString is too eager. */
10558 event.xkey.state &= ~ControlMask;
10559 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10560 | dpyinfo->super_mod_mask
10561 | dpyinfo->hyper_mod_mask
10562 | dpyinfo->alt_mod_mask);
10564 /* In case Meta is ComposeCharacter,
10565 clear its status. According to Markus Ehrnsperger
10566 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10567 this enables ComposeCharacter to work whether or
10568 not it is combined with Meta. */
10569 if (modifiers & dpyinfo->meta_mod_mask)
10570 bzero (&compose_status, sizeof (compose_status));
10572 #ifdef HAVE_X_I18N
10573 if (FRAME_XIC (f))
10575 Status status_return;
10577 coding_system = Vlocale_coding_system;
10578 nbytes = XmbLookupString (FRAME_XIC (f),
10579 &event.xkey, copy_bufptr,
10580 copy_bufsiz, &keysym,
10581 &status_return);
10582 if (status_return == XBufferOverflow)
10584 copy_bufsiz = nbytes + 1;
10585 copy_bufptr = (char *) alloca (copy_bufsiz);
10586 nbytes = XmbLookupString (FRAME_XIC (f),
10587 &event.xkey, copy_bufptr,
10588 copy_bufsiz, &keysym,
10589 &status_return);
10591 /* Xutf8LookupString is a new but already deprecated interface. -stef */
10592 #if 0 && defined X_HAVE_UTF8_STRING
10593 else if (status_return == XLookupKeySym)
10594 { /* Try again but with utf-8. */
10595 coding_system = Qutf_8;
10596 nbytes = Xutf8LookupString (FRAME_XIC (f),
10597 &event.xkey, copy_bufptr,
10598 copy_bufsiz, &keysym,
10599 &status_return);
10600 if (status_return == XBufferOverflow)
10602 copy_bufsiz = nbytes + 1;
10603 copy_bufptr = (char *) alloca (copy_bufsiz);
10604 nbytes = Xutf8LookupString (FRAME_XIC (f),
10605 &event.xkey,
10606 copy_bufptr,
10607 copy_bufsiz, &keysym,
10608 &status_return);
10611 #endif
10613 if (status_return == XLookupNone)
10614 break;
10615 else if (status_return == XLookupChars)
10617 keysym = NoSymbol;
10618 modifiers = 0;
10620 else if (status_return != XLookupKeySym
10621 && status_return != XLookupBoth)
10622 abort ();
10624 else
10625 nbytes = XLookupString (&event.xkey, copy_bufptr,
10626 copy_bufsiz, &keysym,
10627 &compose_status);
10628 #else
10629 nbytes = XLookupString (&event.xkey, copy_bufptr,
10630 copy_bufsiz, &keysym,
10631 &compose_status);
10632 #endif
10634 orig_keysym = keysym;
10636 if (numchars > 1)
10638 Lisp_Object c;
10640 /* First deal with keysyms which have defined
10641 translations to characters. */
10642 if (keysym >= 32 && keysym < 128)
10643 /* Avoid explicitly decoding each ASCII character. */
10645 bufp->kind = ASCII_KEYSTROKE_EVENT;
10646 bufp->code = keysym;
10647 XSETFRAME (bufp->frame_or_window, f);
10648 bufp->arg = Qnil;
10649 bufp->modifiers
10650 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10651 modifiers);
10652 bufp->timestamp = event.xkey.time;
10653 bufp++;
10654 count++;
10655 numchars--;
10657 /* Now non-ASCII. */
10658 else if (HASH_TABLE_P (Vx_keysym_table)
10659 && (NATNUMP (c = Fgethash (make_number (keysym),
10660 Vx_keysym_table,
10661 Qnil))))
10663 bufp->kind = (SINGLE_BYTE_CHAR_P (XFASTINT (c))
10664 ? ASCII_KEYSTROKE_EVENT
10665 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
10666 bufp->code = XFASTINT (c);
10667 XSETFRAME (bufp->frame_or_window, f);
10668 bufp->arg = Qnil;
10669 bufp->modifiers
10670 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10671 modifiers);
10672 bufp->timestamp = event.xkey.time;
10673 bufp++;
10674 count++;
10675 numchars--;
10677 /* Random non-modifier sorts of keysyms. */
10678 else if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10679 || keysym == XK_Delete
10680 #ifdef XK_ISO_Left_Tab
10681 || (keysym >= XK_ISO_Left_Tab
10682 && keysym <= XK_ISO_Enter)
10683 #endif
10684 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10685 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10686 #ifdef HPUX
10687 /* This recognizes the "extended function
10688 keys". It seems there's no cleaner way.
10689 Test IsModifierKey to avoid handling
10690 mode_switch incorrectly. */
10691 || ((unsigned) (keysym) >= XK_Select
10692 && (unsigned)(keysym) < XK_KP_Space)
10693 #endif
10694 #ifdef XK_dead_circumflex
10695 || orig_keysym == XK_dead_circumflex
10696 #endif
10697 #ifdef XK_dead_grave
10698 || orig_keysym == XK_dead_grave
10699 #endif
10700 #ifdef XK_dead_tilde
10701 || orig_keysym == XK_dead_tilde
10702 #endif
10703 #ifdef XK_dead_diaeresis
10704 || orig_keysym == XK_dead_diaeresis
10705 #endif
10706 #ifdef XK_dead_macron
10707 || orig_keysym == XK_dead_macron
10708 #endif
10709 #ifdef XK_dead_degree
10710 || orig_keysym == XK_dead_degree
10711 #endif
10712 #ifdef XK_dead_acute
10713 || orig_keysym == XK_dead_acute
10714 #endif
10715 #ifdef XK_dead_cedilla
10716 || orig_keysym == XK_dead_cedilla
10717 #endif
10718 #ifdef XK_dead_breve
10719 || orig_keysym == XK_dead_breve
10720 #endif
10721 #ifdef XK_dead_ogonek
10722 || orig_keysym == XK_dead_ogonek
10723 #endif
10724 #ifdef XK_dead_caron
10725 || orig_keysym == XK_dead_caron
10726 #endif
10727 #ifdef XK_dead_doubleacute
10728 || orig_keysym == XK_dead_doubleacute
10729 #endif
10730 #ifdef XK_dead_abovedot
10731 || orig_keysym == XK_dead_abovedot
10732 #endif
10733 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10734 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10735 /* Any "vendor-specific" key is ok. */
10736 || (orig_keysym & (1 << 28))
10737 || (keysym != NoSymbol && nbytes == 0))
10738 && ! (IsModifierKey (orig_keysym)
10739 #ifndef HAVE_X11R5
10740 #ifdef XK_Mode_switch
10741 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10742 #endif
10743 #ifdef XK_Num_Lock
10744 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10745 #endif
10746 #endif /* not HAVE_X11R5 */
10747 /* The symbols from XK_ISO_Lock
10748 to XK_ISO_Last_Group_Lock
10749 don't have real modifiers but
10750 should be treated similarly to
10751 Mode_switch by Emacs. */
10752 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10753 || ((unsigned)(orig_keysym)
10754 >= XK_ISO_Lock
10755 && (unsigned)(orig_keysym)
10756 <= XK_ISO_Last_Group_Lock)
10757 #endif
10760 if (temp_index == sizeof temp_buffer / sizeof (short))
10761 temp_index = 0;
10762 temp_buffer[temp_index++] = keysym;
10763 /* make_lispy_event will convert this to a symbolic
10764 key. */
10765 bufp->kind = NON_ASCII_KEYSTROKE_EVENT;
10766 bufp->code = keysym;
10767 XSETFRAME (bufp->frame_or_window, f);
10768 bufp->arg = Qnil;
10769 bufp->modifiers
10770 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10771 modifiers);
10772 bufp->timestamp = event.xkey.time;
10773 bufp++;
10774 count++;
10775 numchars--;
10777 else if (numchars > nbytes)
10778 { /* Raw bytes, not keysym. */
10779 register int i;
10780 register int c;
10781 int nchars, len;
10783 /* The input should be decoded with `coding_system'
10784 which depends on which X*LookupString function
10785 we used just above and the locale. */
10786 setup_coding_system (coding_system, &coding);
10787 coding.src_multibyte = 0;
10788 coding.dst_multibyte = 1;
10789 /* The input is converted to events, thus we can't
10790 handle composition. Anyway, there's no XIM that
10791 gives us composition information. */
10792 coding.composing = COMPOSITION_DISABLED;
10794 for (i = 0; i < nbytes; i++)
10796 if (temp_index == (sizeof temp_buffer
10797 / sizeof (short)))
10798 temp_index = 0;
10799 temp_buffer[temp_index++] = copy_bufptr[i];
10803 /* Decode the input data. */
10804 int require;
10805 unsigned char *p;
10807 require = decoding_buffer_size (&coding, nbytes);
10808 p = (unsigned char *) alloca (require);
10809 coding.mode |= CODING_MODE_LAST_BLOCK;
10810 /* We explicitely disable composition
10811 handling because key data should
10812 not contain any composition
10813 sequence. */
10814 coding.composing = COMPOSITION_DISABLED;
10815 decode_coding (&coding, copy_bufptr, p,
10816 nbytes, require);
10817 nbytes = coding.produced;
10818 nchars = coding.produced_char;
10819 copy_bufptr = p;
10822 /* Convert the input data to a sequence of
10823 character events. */
10824 for (i = 0; i < nbytes; i += len)
10826 if (nchars == nbytes)
10827 c = copy_bufptr[i], len = 1;
10828 else
10829 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10830 nbytes - i, len);
10832 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10833 ? ASCII_KEYSTROKE_EVENT
10834 : MULTIBYTE_CHAR_KEYSTROKE_EVENT);
10835 bufp->code = c;
10836 XSETFRAME (bufp->frame_or_window, f);
10837 bufp->arg = Qnil;
10838 bufp->modifiers
10839 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10840 modifiers);
10841 bufp->timestamp = event.xkey.time;
10842 bufp++;
10845 count += nchars;
10846 numchars -= nchars;
10848 if (keysym == NoSymbol)
10849 break;
10851 else
10852 abort ();
10854 else
10855 abort ();
10857 #ifdef HAVE_X_I18N
10858 /* Don't dispatch this event since XtDispatchEvent calls
10859 XFilterEvent, and two calls in a row may freeze the
10860 client. */
10861 break;
10862 #else
10863 goto OTHER;
10864 #endif
10866 case KeyRelease:
10867 #ifdef HAVE_X_I18N
10868 /* Don't dispatch this event since XtDispatchEvent calls
10869 XFilterEvent, and two calls in a row may freeze the
10870 client. */
10871 break;
10872 #else
10873 goto OTHER;
10874 #endif
10876 case EnterNotify:
10878 int n;
10880 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10881 if (n > 0)
10883 bufp += n, count += n, numchars -= n;
10886 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10888 #if 0
10889 if (event.xcrossing.focus)
10891 /* Avoid nasty pop/raise loops. */
10892 if (f && (!(f->auto_raise)
10893 || !(f->auto_lower)
10894 || (event.xcrossing.time - enter_timestamp) > 500))
10896 x_new_focus_frame (dpyinfo, f);
10897 enter_timestamp = event.xcrossing.time;
10900 else if (f == dpyinfo->x_focus_frame)
10901 x_new_focus_frame (dpyinfo, 0);
10902 #endif
10904 /* EnterNotify counts as mouse movement,
10905 so update things that depend on mouse position. */
10906 if (f && !f->output_data.x->hourglass_p)
10907 note_mouse_movement (f, &event.xmotion);
10908 goto OTHER;
10911 case FocusIn:
10913 int n;
10915 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10916 if (n > 0)
10918 bufp += n, count += n, numchars -= n;
10922 goto OTHER;
10924 case LeaveNotify:
10926 int n;
10928 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10929 if (n > 0)
10931 bufp += n, count += n, numchars -= n;
10935 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10936 if (f)
10938 if (f == dpyinfo->mouse_face_mouse_frame)
10940 /* If we move outside the frame, then we're
10941 certainly no longer on any text in the frame. */
10942 clear_mouse_face (dpyinfo);
10943 dpyinfo->mouse_face_mouse_frame = 0;
10946 /* Generate a nil HELP_EVENT to cancel a help-echo.
10947 Do it only if there's something to cancel.
10948 Otherwise, the startup message is cleared when
10949 the mouse leaves the frame. */
10950 if (any_help_event_p)
10952 Lisp_Object frame;
10953 int n;
10955 XSETFRAME (frame, f);
10956 help_echo = Qnil;
10957 n = gen_help_event (bufp, numchars,
10958 Qnil, frame, Qnil, Qnil, 0);
10959 bufp += n, count += n, numchars -= n;
10963 goto OTHER;
10965 case FocusOut:
10967 int n;
10969 n = x_detect_focus_change (dpyinfo, &event, bufp, numchars);
10970 if (n > 0)
10972 bufp += n, count += n, numchars -= n;
10976 goto OTHER;
10978 case MotionNotify:
10980 previous_help_echo = help_echo;
10981 help_echo = help_echo_object = help_echo_window = Qnil;
10982 help_echo_pos = -1;
10984 if (dpyinfo->grabbed && last_mouse_frame
10985 && FRAME_LIVE_P (last_mouse_frame))
10986 f = last_mouse_frame;
10987 else
10988 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10990 if (dpyinfo->mouse_face_hidden)
10992 dpyinfo->mouse_face_hidden = 0;
10993 clear_mouse_face (dpyinfo);
10996 if (f)
10999 /* Generate SELECT_WINDOW_EVENTs when needed. */
11000 if (mouse_autoselect_window)
11002 Lisp_Object window;
11003 int area;
11005 window = window_from_coordinates (f,
11006 event.xmotion.x, event.xmotion.y,
11007 &area, 0);
11009 /* Window will be selected only when it is not selected now and
11010 last mouse movement event was not in it. Minibuffer window
11011 will be selected iff it is active. */
11012 if (WINDOWP(window)
11013 && !EQ (window, last_window)
11014 && !EQ (window, selected_window)
11015 && numchars > 0)
11017 bufp->kind = SELECT_WINDOW_EVENT;
11018 bufp->frame_or_window = window;
11019 bufp->arg = Qnil;
11020 ++bufp, ++count, --numchars;
11023 last_window=window;
11025 note_mouse_movement (f, &event.xmotion);
11027 else
11029 #ifndef USE_TOOLKIT_SCROLL_BARS
11030 struct scroll_bar *bar
11031 = x_window_to_scroll_bar (event.xmotion.window);
11033 if (bar)
11034 x_scroll_bar_note_movement (bar, &event);
11035 #endif /* USE_TOOLKIT_SCROLL_BARS */
11037 /* If we move outside the frame, then we're
11038 certainly no longer on any text in the frame. */
11039 clear_mouse_face (dpyinfo);
11042 /* If the contents of the global variable help_echo
11043 has changed, generate a HELP_EVENT. */
11044 if (!NILP (help_echo)
11045 || !NILP (previous_help_echo))
11047 Lisp_Object frame;
11048 int n;
11050 if (f)
11051 XSETFRAME (frame, f);
11052 else
11053 frame = Qnil;
11055 any_help_event_p = 1;
11056 n = gen_help_event (bufp, numchars, help_echo, frame,
11057 help_echo_window, help_echo_object,
11058 help_echo_pos);
11059 bufp += n, count += n, numchars -= n;
11062 goto OTHER;
11065 case ConfigureNotify:
11066 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
11067 if (f)
11069 #ifndef USE_X_TOOLKIT
11070 #ifdef USE_GTK
11071 xg_resize_widgets (f, event.xconfigure.width,
11072 event.xconfigure.height);
11073 #else /* not USE_GTK */
11074 /* If there is a pending resize for fullscreen, don't
11075 do this one, the right one will come later.
11076 The toolkit version doesn't seem to need this, but we
11077 need to reset it below. */
11078 int dont_resize =
11079 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11080 && FRAME_NEW_WIDTH (f) != 0);
11081 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
11082 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
11083 if (dont_resize)
11084 goto OTHER;
11086 /* In the toolkit version, change_frame_size
11087 is called by the code that handles resizing
11088 of the EmacsFrame widget. */
11090 /* Even if the number of character rows and columns has
11091 not changed, the font size may have changed, so we need
11092 to check the pixel dimensions as well. */
11093 if (columns != f->width
11094 || rows != f->height
11095 || event.xconfigure.width != f->output_data.x->pixel_width
11096 || event.xconfigure.height != f->output_data.x->pixel_height)
11098 change_frame_size (f, rows, columns, 0, 1, 0);
11099 SET_FRAME_GARBAGED (f);
11100 cancel_mouse_face (f);
11102 #endif /* not USE_GTK */
11103 #endif
11105 f->output_data.x->pixel_width = event.xconfigure.width;
11106 f->output_data.x->pixel_height = event.xconfigure.height;
11108 #ifdef USE_GTK
11109 /* GTK creates windows but doesn't map them.
11110 Only get real positions and check fullscreen when mapped. */
11111 if (FRAME_GTK_OUTER_WIDGET (f)
11112 && GTK_WIDGET_MAPPED (FRAME_GTK_OUTER_WIDGET (f)))
11114 #endif
11115 /* What we have now is the position of Emacs's own window.
11116 Convert that to the position of the window manager window. */
11117 x_real_positions (f, &f->output_data.x->left_pos,
11118 &f->output_data.x->top_pos);
11120 x_check_fullscreen_move (f);
11121 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
11122 f->output_data.x->want_fullscreen &=
11123 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
11124 #ifdef USE_GTK
11126 #endif
11127 #ifdef HAVE_X_I18N
11128 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
11129 xic_set_statusarea (f);
11130 #endif
11132 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11134 /* Since the WM decorations come below top_pos now,
11135 we must put them below top_pos in the future. */
11136 f->output_data.x->win_gravity = NorthWestGravity;
11137 x_wm_set_size_hint (f, (long) 0, 0);
11140 goto OTHER;
11142 case ButtonRelease:
11143 case ButtonPress:
11145 /* If we decide we want to generate an event to be seen
11146 by the rest of Emacs, we put it here. */
11147 struct input_event emacs_event;
11148 int tool_bar_p = 0;
11150 emacs_event.kind = NO_EVENT;
11151 bzero (&compose_status, sizeof (compose_status));
11153 if (dpyinfo->grabbed
11154 && last_mouse_frame
11155 && FRAME_LIVE_P (last_mouse_frame))
11156 f = last_mouse_frame;
11157 else
11158 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11160 if (f)
11162 /* Is this in the tool-bar? */
11163 if (WINDOWP (f->tool_bar_window)
11164 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11166 Lisp_Object window;
11167 int p, x, y;
11169 x = event.xbutton.x;
11170 y = event.xbutton.y;
11172 /* Set x and y. */
11173 window = window_from_coordinates (f, x, y, &p, 1);
11174 if (EQ (window, f->tool_bar_window))
11176 x_handle_tool_bar_click (f, &event.xbutton);
11177 tool_bar_p = 1;
11181 if (!tool_bar_p)
11182 if (!dpyinfo->x_focus_frame
11183 || f == dpyinfo->x_focus_frame)
11185 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
11186 if (! popup_activated ())
11187 #endif
11188 construct_mouse_click (&emacs_event, &event, f);
11191 else
11193 struct scroll_bar *bar
11194 = x_window_to_scroll_bar (event.xbutton.window);
11196 #ifdef USE_TOOLKIT_SCROLL_BARS
11197 /* Make the "Ctrl-Mouse-2 splits window" work for toolkit
11198 scroll bars. */
11199 if (bar && event.xbutton.state & ControlMask)
11201 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11202 *finish = X_EVENT_DROP;
11204 #else /* not USE_TOOLKIT_SCROLL_BARS */
11205 if (bar)
11206 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11207 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11210 if (event.type == ButtonPress)
11212 dpyinfo->grabbed |= (1 << event.xbutton.button);
11213 last_mouse_frame = f;
11214 /* Ignore any mouse motion that happened
11215 before this event; any subsequent mouse-movement
11216 Emacs events should reflect only motion after
11217 the ButtonPress. */
11218 if (f != 0)
11219 f->mouse_moved = 0;
11221 if (!tool_bar_p)
11222 last_tool_bar_item = -1;
11224 else
11225 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11227 if (numchars >= 1 && emacs_event.kind != NO_EVENT)
11229 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11230 bufp++;
11231 count++;
11232 numchars--;
11235 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
11236 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11237 /* For a down-event in the menu bar,
11238 don't pass it to Xt right now.
11239 Instead, save it away
11240 and we will pass it to Xt from kbd_buffer_get_event.
11241 That way, we can run some Lisp code first. */
11242 if (
11243 #ifdef USE_GTK
11244 ! popup_activated ()
11246 #endif
11247 f && event.type == ButtonPress
11248 /* Verify the event is really within the menu bar
11249 and not just sent to it due to grabbing. */
11250 && event.xbutton.x >= 0
11251 && event.xbutton.x < f->output_data.x->pixel_width
11252 && event.xbutton.y >= 0
11253 && event.xbutton.y < f->output_data.x->menubar_height
11254 && event.xbutton.same_screen)
11256 SET_SAVED_BUTTON_EVENT;
11257 XSETFRAME (last_mouse_press_frame, f);
11258 #ifdef USE_GTK
11259 *finish = X_EVENT_DROP;
11260 #endif
11262 else if (event.type == ButtonPress)
11264 last_mouse_press_frame = Qnil;
11265 goto OTHER;
11268 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11269 but I am trying to be cautious. */
11270 else if (event.type == ButtonRelease)
11272 if (!NILP (last_mouse_press_frame))
11274 f = XFRAME (last_mouse_press_frame);
11275 if (f->output_data.x)
11276 SET_SAVED_BUTTON_EVENT;
11278 else
11279 goto OTHER;
11281 #endif /* USE_MOTIF */
11282 else
11283 goto OTHER;
11284 #endif /* USE_X_TOOLKIT || USE_GTK */
11286 break;
11288 case CirculateNotify:
11289 goto OTHER;
11291 case CirculateRequest:
11292 goto OTHER;
11294 case VisibilityNotify:
11295 goto OTHER;
11297 case MappingNotify:
11298 /* Someone has changed the keyboard mapping - update the
11299 local cache. */
11300 switch (event.xmapping.request)
11302 case MappingModifier:
11303 x_find_modifier_meanings (dpyinfo);
11304 /* This is meant to fall through. */
11305 case MappingKeyboard:
11306 XRefreshKeyboardMapping (&event.xmapping);
11308 goto OTHER;
11310 default:
11311 OTHER:
11312 #ifdef USE_X_TOOLKIT
11313 BLOCK_INPUT;
11314 if (*finish != X_EVENT_DROP)
11315 XtDispatchEvent (&event);
11316 UNBLOCK_INPUT;
11317 #endif /* USE_X_TOOLKIT */
11318 break;
11321 goto ret;
11323 out:
11324 *finish = X_EVENT_GOTO_OUT;
11326 ret:
11327 *bufp_r = bufp;
11328 *numcharsp = numchars;
11329 *eventp = event;
11331 return count;
11335 /* Handles the XEvent EVENT on display DISPLAY.
11336 This is used for event loops outside the normal event handling,
11337 i.e. looping while a popup menu or a dialog is posted.
11339 Returns the value handle_one_xevent sets in the finish argument. */
11341 x_dispatch_event (event, display)
11342 XEvent *event;
11343 Display *display;
11345 struct x_display_info *dpyinfo;
11346 struct input_event bufp[10];
11347 struct input_event *bufpp = bufp;
11348 int numchars = 10;
11349 int finish = X_EVENT_NORMAL;
11351 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
11352 if (dpyinfo->display == display)
11353 break;
11355 if (dpyinfo)
11357 int i, events;
11358 events = handle_one_xevent (dpyinfo,
11359 event,
11360 &bufpp,
11361 &numchars,
11362 &finish);
11363 for (i = 0; i < events; ++i)
11364 kbd_buffer_store_event (&bufp[i]);
11367 return finish;
11371 /* Read events coming from the X server.
11372 This routine is called by the SIGIO handler.
11373 We return as soon as there are no more events to be read.
11375 Events representing keys are stored in buffer BUFP,
11376 which can hold up to NUMCHARS characters.
11377 We return the number of characters stored into the buffer,
11378 thus pretending to be `read'.
11380 EXPECTED is nonzero if the caller knows input is available. */
11382 static int
11383 XTread_socket (sd, bufp, numchars, expected)
11384 register int sd;
11385 /* register */ struct input_event *bufp;
11386 /* register */ int numchars;
11387 int expected;
11389 int count = 0;
11390 int nbytes = 0;
11391 XEvent event;
11392 int event_found = 0;
11393 struct x_display_info *dpyinfo;
11395 if (interrupt_input_blocked)
11397 interrupt_input_pending = 1;
11398 return -1;
11401 interrupt_input_pending = 0;
11402 BLOCK_INPUT;
11404 /* So people can tell when we have read the available input. */
11405 input_signal_count++;
11407 if (numchars <= 0)
11408 abort (); /* Don't think this happens. */
11410 ++handling_signal;
11412 /* Find the display we are supposed to read input for.
11413 It's the one communicating on descriptor SD. */
11414 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
11416 #if 0 /* This ought to be unnecessary; let's verify it. */
11417 #ifdef FIOSNBIO
11418 /* If available, Xlib uses FIOSNBIO to make the socket
11419 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
11420 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
11421 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
11422 fcntl (dpyinfo->connection, F_SETFL, 0);
11423 #endif /* ! defined (FIOSNBIO) */
11424 #endif
11426 #if 0 /* This code can't be made to work, with multiple displays,
11427 and appears not to be used on any system any more.
11428 Also keyboard.c doesn't turn O_NDELAY on and off
11429 for X connections. */
11430 #ifndef SIGIO
11431 #ifndef HAVE_SELECT
11432 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
11434 extern int read_alarm_should_throw;
11435 read_alarm_should_throw = 1;
11436 XPeekEvent (dpyinfo->display, &event);
11437 read_alarm_should_throw = 0;
11439 #endif /* HAVE_SELECT */
11440 #endif /* SIGIO */
11441 #endif
11443 /* For debugging, this gives a way to fake an I/O error. */
11444 if (dpyinfo == XTread_socket_fake_io_error)
11446 XTread_socket_fake_io_error = 0;
11447 x_io_error_quitter (dpyinfo->display);
11450 #ifdef HAVE_X_SM
11451 BLOCK_INPUT;
11452 count += x_session_check_input (bufp, &numchars);
11453 UNBLOCK_INPUT;
11454 #endif
11456 #ifdef USE_GTK
11457 /* For GTK we must use the GTK event loop. But XEvents gets passed
11458 to our filter function above, and then to the big event switch.
11459 We use a bunch of globals to communicate with our filter function,
11460 that is kind of ugly, but it works. */
11461 current_dpyinfo = dpyinfo;
11463 while (gtk_events_pending ())
11465 static int nr = 0;
11466 current_count = count;
11467 current_numcharsp = &numchars;
11468 current_bufp = &bufp;
11470 gtk_main_iteration ();
11472 count = current_count;
11473 current_bufp = 0;
11474 current_numcharsp = 0;
11476 if (current_finish == X_EVENT_GOTO_OUT)
11477 goto out;
11480 #else /* not USE_GTK */
11481 while (XPending (dpyinfo->display))
11483 int finish;
11485 XNextEvent (dpyinfo->display, &event);
11487 #ifdef HAVE_X_I18N
11488 /* Filter events for the current X input method. */
11489 if (x_filter_event (dpyinfo, &event))
11490 break;
11491 #endif
11492 event_found = 1;
11494 count += handle_one_xevent (dpyinfo,
11495 &event,
11496 &bufp,
11497 &numchars,
11498 &finish);
11500 if (finish == X_EVENT_GOTO_OUT)
11501 goto out;
11503 #endif /* USE_GTK */
11506 out:;
11508 /* On some systems, an X bug causes Emacs to get no more events
11509 when the window is destroyed. Detect that. (1994.) */
11510 if (! event_found)
11512 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11513 One XNOOP in 100 loops will make Emacs terminate.
11514 B. Bretthauer, 1994 */
11515 x_noop_count++;
11516 if (x_noop_count >= 100)
11518 x_noop_count=0;
11520 if (next_noop_dpyinfo == 0)
11521 next_noop_dpyinfo = x_display_list;
11523 XNoOp (next_noop_dpyinfo->display);
11525 /* Each time we get here, cycle through the displays now open. */
11526 next_noop_dpyinfo = next_noop_dpyinfo->next;
11530 /* If the focus was just given to an auto-raising frame,
11531 raise it now. */
11532 /* ??? This ought to be able to handle more than one such frame. */
11533 if (pending_autoraise_frame)
11535 x_raise_frame (pending_autoraise_frame);
11536 pending_autoraise_frame = 0;
11539 UNBLOCK_INPUT;
11540 --handling_signal;
11541 return count;
11547 /***********************************************************************
11548 Text Cursor
11549 ***********************************************************************/
11551 /* Notice when the text cursor of window W has been completely
11552 overwritten by a drawing operation that outputs glyphs in AREA
11553 starting at X0 and ending at X1 in the line starting at Y0 and
11554 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11555 the rest of the line after X0 has been written. Y coordinates
11556 are window-relative. */
11558 static void
11559 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11560 struct window *w;
11561 enum glyph_row_area area;
11562 int x0, y0, x1, y1;
11564 if (area == TEXT_AREA && w->phys_cursor_on_p)
11566 int cx0 = w->phys_cursor.x;
11567 int cx1 = cx0 + w->phys_cursor_width;
11568 int cy0 = w->phys_cursor.y;
11569 int cy1 = cy0 + w->phys_cursor_height;
11571 if (x0 <= cx0 && (x1 < 0 || x1 >= cx1))
11573 /* The cursor image will be completely removed from the
11574 screen if the output area intersects the cursor area in
11575 y-direction. When we draw in [y0 y1[, and some part of
11576 the cursor is at y < y0, that part must have been drawn
11577 before. When scrolling, the cursor is erased before
11578 actually scrolling, so we don't come here. When not
11579 scrolling, the rows above the old cursor row must have
11580 changed, and in this case these rows must have written
11581 over the cursor image.
11583 Likewise if part of the cursor is below y1, with the
11584 exception of the cursor being in the first blank row at
11585 the buffer and window end because update_text_area
11586 doesn't draw that row. (Except when it does, but
11587 that's handled in update_text_area.) */
11589 if (((y0 >= cy0 && y0 < cy1) || (y1 > cy0 && y1 < cy1))
11590 && w->current_matrix->rows[w->phys_cursor.vpos].displays_text_p)
11591 w->phys_cursor_on_p = 0;
11597 /* Set clipping for output in glyph row ROW. W is the window in which
11598 we operate. GC is the graphics context to set clipping in.
11599 WHOLE_LINE_P non-zero means include the areas used for truncation
11600 mark display and alike in the clipping rectangle.
11602 ROW may be a text row or, e.g., a mode line. Text rows must be
11603 clipped to the interior of the window dedicated to text display,
11604 mode lines must be clipped to the whole window. */
11606 static void
11607 x_clip_to_row (w, row, gc, whole_line_p)
11608 struct window *w;
11609 struct glyph_row *row;
11610 GC gc;
11611 int whole_line_p;
11613 struct frame *f = XFRAME (WINDOW_FRAME (w));
11614 XRectangle clip_rect;
11615 int window_x, window_y, window_width, window_height;
11617 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11619 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11620 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11621 clip_rect.y = max (clip_rect.y, window_y);
11622 clip_rect.width = window_width;
11623 clip_rect.height = row->visible_height;
11625 /* If clipping to the whole line, including trunc marks, extend
11626 the rectangle to the left and increase its width. */
11627 if (whole_line_p)
11629 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11630 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11633 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11637 /* Draw a hollow box cursor on window W in glyph row ROW. */
11639 static void
11640 x_draw_hollow_cursor (w, row)
11641 struct window *w;
11642 struct glyph_row *row;
11644 struct frame *f = XFRAME (WINDOW_FRAME (w));
11645 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11646 Display *dpy = FRAME_X_DISPLAY (f);
11647 int x, y, wd, h;
11648 XGCValues xgcv;
11649 struct glyph *cursor_glyph;
11650 GC gc;
11652 /* Compute frame-relative coordinates from window-relative
11653 coordinates. */
11654 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11655 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11656 + row->ascent - w->phys_cursor_ascent);
11657 h = row->height - 1;
11659 /* Get the glyph the cursor is on. If we can't tell because
11660 the current matrix is invalid or such, give up. */
11661 cursor_glyph = get_phys_cursor_glyph (w);
11662 if (cursor_glyph == NULL)
11663 return;
11665 /* Compute the width of the rectangle to draw. If on a stretch
11666 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11667 rectangle as wide as the glyph, but use a canonical character
11668 width instead. */
11669 wd = cursor_glyph->pixel_width - 1;
11670 if (cursor_glyph->type == STRETCH_GLYPH
11671 && !x_stretch_cursor_p)
11672 wd = min (CANON_X_UNIT (f), wd);
11673 w->phys_cursor_width = wd;
11675 /* The foreground of cursor_gc is typically the same as the normal
11676 background color, which can cause the cursor box to be invisible. */
11677 xgcv.foreground = f->output_data.x->cursor_pixel;
11678 if (dpyinfo->scratch_cursor_gc)
11679 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11680 else
11681 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11682 GCForeground, &xgcv);
11683 gc = dpyinfo->scratch_cursor_gc;
11685 /* Set clipping, draw the rectangle, and reset clipping again. */
11686 x_clip_to_row (w, row, gc, 0);
11687 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11688 XSetClipMask (dpy, gc, None);
11692 /* Draw a bar cursor on window W in glyph row ROW.
11694 Implementation note: One would like to draw a bar cursor with an
11695 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11696 Unfortunately, I didn't find a font yet that has this property set.
11697 --gerd. */
11699 static void
11700 x_draw_bar_cursor (w, row, width, kind)
11701 struct window *w;
11702 struct glyph_row *row;
11703 int width;
11704 enum text_cursor_kinds kind;
11706 struct frame *f = XFRAME (w->frame);
11707 struct glyph *cursor_glyph;
11709 /* If cursor is out of bounds, don't draw garbage. This can happen
11710 in mini-buffer windows when switching between echo area glyphs
11711 and mini-buffer. */
11712 cursor_glyph = get_phys_cursor_glyph (w);
11713 if (cursor_glyph == NULL)
11714 return;
11716 /* If on an image, draw like a normal cursor. That's usually better
11717 visible than drawing a bar, esp. if the image is large so that
11718 the bar might not be in the window. */
11719 if (cursor_glyph->type == IMAGE_GLYPH)
11721 struct glyph_row *row;
11722 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11723 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11725 else
11727 Display *dpy = FRAME_X_DISPLAY (f);
11728 Window window = FRAME_X_WINDOW (f);
11729 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11730 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11731 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11732 XGCValues xgcv;
11734 /* If the glyph's background equals the color we normally draw
11735 the bar cursor in, the bar cursor in its normal color is
11736 invisible. Use the glyph's foreground color instead in this
11737 case, on the assumption that the glyph's colors are chosen so
11738 that the glyph is legible. */
11739 if (face->background == f->output_data.x->cursor_pixel)
11740 xgcv.background = xgcv.foreground = face->foreground;
11741 else
11742 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11743 xgcv.graphics_exposures = 0;
11745 if (gc)
11746 XChangeGC (dpy, gc, mask, &xgcv);
11747 else
11749 gc = XCreateGC (dpy, window, mask, &xgcv);
11750 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11753 if (width < 0)
11754 width = FRAME_CURSOR_WIDTH (f);
11755 width = min (cursor_glyph->pixel_width, width);
11757 w->phys_cursor_width = width;
11758 x_clip_to_row (w, row, gc, 0);
11760 if (kind == BAR_CURSOR)
11761 XFillRectangle (dpy, window, gc,
11762 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11763 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11764 width, row->height);
11765 else
11766 XFillRectangle (dpy, window, gc,
11767 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11768 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y +
11769 row->height - width),
11770 cursor_glyph->pixel_width,
11771 width);
11773 XSetClipMask (dpy, gc, None);
11778 /* Clear the cursor of window W to background color, and mark the
11779 cursor as not shown. This is used when the text where the cursor
11780 is is about to be rewritten. */
11782 static void
11783 x_clear_cursor (w)
11784 struct window *w;
11786 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11787 x_update_window_cursor (w, 0);
11791 /* Draw the cursor glyph of window W in glyph row ROW. See the
11792 comment of x_draw_glyphs for the meaning of HL. */
11794 static void
11795 x_draw_phys_cursor_glyph (w, row, hl)
11796 struct window *w;
11797 struct glyph_row *row;
11798 enum draw_glyphs_face hl;
11800 /* If cursor hpos is out of bounds, don't draw garbage. This can
11801 happen in mini-buffer windows when switching between echo area
11802 glyphs and mini-buffer. */
11803 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11805 int on_p = w->phys_cursor_on_p;
11806 int x1;
11808 x1 = x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11809 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11810 hl, 0);
11811 w->phys_cursor_on_p = on_p;
11813 if (hl == DRAW_CURSOR)
11814 w->phys_cursor_width = x1 - w->phys_cursor.x;
11816 /* When we erase the cursor, and ROW is overlapped by other
11817 rows, make sure that these overlapping parts of other rows
11818 are redrawn. */
11819 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11821 if (row > w->current_matrix->rows
11822 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11823 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11825 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11826 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11827 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11833 /* Erase the image of a cursor of window W from the screen. */
11835 static void
11836 x_erase_phys_cursor (w)
11837 struct window *w;
11839 struct frame *f = XFRAME (w->frame);
11840 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11841 int hpos = w->phys_cursor.hpos;
11842 int vpos = w->phys_cursor.vpos;
11843 int mouse_face_here_p = 0;
11844 struct glyph_matrix *active_glyphs = w->current_matrix;
11845 struct glyph_row *cursor_row;
11846 struct glyph *cursor_glyph;
11847 enum draw_glyphs_face hl;
11849 /* No cursor displayed or row invalidated => nothing to do on the
11850 screen. */
11851 if (w->phys_cursor_type == NO_CURSOR)
11852 goto mark_cursor_off;
11854 /* VPOS >= active_glyphs->nrows means that window has been resized.
11855 Don't bother to erase the cursor. */
11856 if (vpos >= active_glyphs->nrows)
11857 goto mark_cursor_off;
11859 /* If row containing cursor is marked invalid, there is nothing we
11860 can do. */
11861 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11862 if (!cursor_row->enabled_p)
11863 goto mark_cursor_off;
11865 /* If row is completely invisible, don't attempt to delete a cursor which
11866 isn't there. This can happen if cursor is at top of a window, and
11867 we switch to a buffer with a header line in that window. */
11868 if (cursor_row->visible_height <= 0)
11869 goto mark_cursor_off;
11871 /* This can happen when the new row is shorter than the old one.
11872 In this case, either x_draw_glyphs or clear_end_of_line
11873 should have cleared the cursor. Note that we wouldn't be
11874 able to erase the cursor in this case because we don't have a
11875 cursor glyph at hand. */
11876 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11877 goto mark_cursor_off;
11879 /* If the cursor is in the mouse face area, redisplay that when
11880 we clear the cursor. */
11881 if (! NILP (dpyinfo->mouse_face_window)
11882 && w == XWINDOW (dpyinfo->mouse_face_window)
11883 && (vpos > dpyinfo->mouse_face_beg_row
11884 || (vpos == dpyinfo->mouse_face_beg_row
11885 && hpos >= dpyinfo->mouse_face_beg_col))
11886 && (vpos < dpyinfo->mouse_face_end_row
11887 || (vpos == dpyinfo->mouse_face_end_row
11888 && hpos < dpyinfo->mouse_face_end_col))
11889 /* Don't redraw the cursor's spot in mouse face if it is at the
11890 end of a line (on a newline). The cursor appears there, but
11891 mouse highlighting does not. */
11892 && cursor_row->used[TEXT_AREA] > hpos)
11893 mouse_face_here_p = 1;
11895 /* Maybe clear the display under the cursor. */
11896 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11898 int x;
11899 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11901 cursor_glyph = get_phys_cursor_glyph (w);
11902 if (cursor_glyph == NULL)
11903 goto mark_cursor_off;
11905 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11907 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11909 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11910 cursor_row->y)),
11911 cursor_glyph->pixel_width,
11912 cursor_row->visible_height,
11913 False);
11916 /* Erase the cursor by redrawing the character underneath it. */
11917 if (mouse_face_here_p)
11918 hl = DRAW_MOUSE_FACE;
11919 else
11920 hl = DRAW_NORMAL_TEXT;
11921 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11923 mark_cursor_off:
11924 w->phys_cursor_on_p = 0;
11925 w->phys_cursor_type = NO_CURSOR;
11929 /* Non-zero if physical cursor of window W is within mouse face. */
11931 static int
11932 cursor_in_mouse_face_p (w)
11933 struct window *w;
11935 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11936 int in_mouse_face = 0;
11938 if (WINDOWP (dpyinfo->mouse_face_window)
11939 && XWINDOW (dpyinfo->mouse_face_window) == w)
11941 int hpos = w->phys_cursor.hpos;
11942 int vpos = w->phys_cursor.vpos;
11944 if (vpos >= dpyinfo->mouse_face_beg_row
11945 && vpos <= dpyinfo->mouse_face_end_row
11946 && (vpos > dpyinfo->mouse_face_beg_row
11947 || hpos >= dpyinfo->mouse_face_beg_col)
11948 && (vpos < dpyinfo->mouse_face_end_row
11949 || hpos < dpyinfo->mouse_face_end_col
11950 || dpyinfo->mouse_face_past_end))
11951 in_mouse_face = 1;
11954 return in_mouse_face;
11958 /* Display or clear cursor of window W. If ON is zero, clear the
11959 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11960 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11962 void
11963 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11964 struct window *w;
11965 int on, hpos, vpos, x, y;
11967 struct frame *f = XFRAME (w->frame);
11968 int new_cursor_type;
11969 int new_cursor_width;
11970 int active_cursor;
11971 struct glyph_matrix *current_glyphs;
11972 struct glyph_row *glyph_row;
11973 struct glyph *glyph;
11975 /* This is pointless on invisible frames, and dangerous on garbaged
11976 windows and frames; in the latter case, the frame or window may
11977 be in the midst of changing its size, and x and y may be off the
11978 window. */
11979 if (! FRAME_VISIBLE_P (f)
11980 || FRAME_GARBAGED_P (f)
11981 || vpos >= w->current_matrix->nrows
11982 || hpos >= w->current_matrix->matrix_w)
11983 return;
11985 /* If cursor is off and we want it off, return quickly. */
11986 if (!on && !w->phys_cursor_on_p)
11987 return;
11989 current_glyphs = w->current_matrix;
11990 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11991 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11993 /* If cursor row is not enabled, we don't really know where to
11994 display the cursor. */
11995 if (!glyph_row->enabled_p)
11997 w->phys_cursor_on_p = 0;
11998 return;
12001 xassert (interrupt_input_blocked);
12003 /* Set new_cursor_type to the cursor we want to be displayed. */
12004 new_cursor_type = get_window_cursor_type (w, &new_cursor_width, &active_cursor);
12006 /* If cursor is currently being shown and we don't want it to be or
12007 it is in the wrong place, or the cursor type is not what we want,
12008 erase it. */
12009 if (w->phys_cursor_on_p
12010 && (!on
12011 || w->phys_cursor.x != x
12012 || w->phys_cursor.y != y
12013 || new_cursor_type != w->phys_cursor_type
12014 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
12015 && new_cursor_width != w->phys_cursor_width)))
12016 x_erase_phys_cursor (w);
12018 /* Don't check phys_cursor_on_p here because that flag is only set
12019 to zero in some cases where we know that the cursor has been
12020 completely erased, to avoid the extra work of erasing the cursor
12021 twice. In other words, phys_cursor_on_p can be 1 and the cursor
12022 still not be visible, or it has only been partly erased. */
12023 if (on)
12025 w->phys_cursor_ascent = glyph_row->ascent;
12026 w->phys_cursor_height = glyph_row->height;
12028 /* Set phys_cursor_.* before x_draw_.* is called because some
12029 of them may need the information. */
12030 w->phys_cursor.x = x;
12031 w->phys_cursor.y = glyph_row->y;
12032 w->phys_cursor.hpos = hpos;
12033 w->phys_cursor.vpos = vpos;
12034 w->phys_cursor_type = new_cursor_type;
12035 w->phys_cursor_on_p = 1;
12037 switch (new_cursor_type)
12039 case HOLLOW_BOX_CURSOR:
12040 x_draw_hollow_cursor (w, glyph_row);
12041 break;
12043 case FILLED_BOX_CURSOR:
12044 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
12045 break;
12047 case BAR_CURSOR:
12048 x_draw_bar_cursor (w, glyph_row, new_cursor_width, BAR_CURSOR);
12049 break;
12051 case HBAR_CURSOR:
12052 x_draw_bar_cursor (w, glyph_row, new_cursor_width, HBAR_CURSOR);
12053 break;
12055 case NO_CURSOR:
12056 w->phys_cursor_width = 0;
12057 break;
12059 default:
12060 abort ();
12063 #ifdef HAVE_X_I18N
12064 if (w == XWINDOW (f->selected_window))
12065 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
12066 xic_set_preeditarea (w, x, y);
12067 #endif
12070 #ifndef XFlush
12071 if (updating_frame != f)
12072 XFlush (FRAME_X_DISPLAY (f));
12073 #endif
12077 /* Display the cursor on window W, or clear it. X and Y are window
12078 relative pixel coordinates. HPOS and VPOS are glyph matrix
12079 positions. If W is not the selected window, display a hollow
12080 cursor. ON non-zero means display the cursor at X, Y which
12081 correspond to HPOS, VPOS, otherwise it is cleared. */
12083 void
12084 x_display_cursor (w, on, hpos, vpos, x, y)
12085 struct window *w;
12086 int on, hpos, vpos, x, y;
12088 BLOCK_INPUT;
12089 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
12090 UNBLOCK_INPUT;
12094 /* Display the cursor on window W, or clear it, according to ON_P.
12095 Don't change the cursor's position. */
12097 void
12098 x_update_cursor (f, on_p)
12099 struct frame *f;
12100 int on_p;
12102 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
12106 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
12107 in the window tree rooted at W. */
12109 static void
12110 x_update_cursor_in_window_tree (w, on_p)
12111 struct window *w;
12112 int on_p;
12114 while (w)
12116 if (!NILP (w->hchild))
12117 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
12118 else if (!NILP (w->vchild))
12119 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
12120 else
12121 x_update_window_cursor (w, on_p);
12123 w = NILP (w->next) ? 0 : XWINDOW (w->next);
12128 /* Switch the display of W's cursor on or off, according to the value
12129 of ON. */
12131 static void
12132 x_update_window_cursor (w, on)
12133 struct window *w;
12134 int on;
12136 /* Don't update cursor in windows whose frame is in the process
12137 of being deleted. */
12138 if (w->current_matrix)
12140 BLOCK_INPUT;
12141 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
12142 w->phys_cursor.x, w->phys_cursor.y);
12143 UNBLOCK_INPUT;
12150 /* Icons. */
12152 /* Make the x-window of frame F use the gnu icon bitmap. */
12155 x_bitmap_icon (f, file)
12156 struct frame *f;
12157 Lisp_Object file;
12159 int bitmap_id;
12161 if (FRAME_X_WINDOW (f) == 0)
12162 return 1;
12164 /* Free up our existing icon bitmap if any. */
12165 if (f->output_data.x->icon_bitmap > 0)
12166 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12167 f->output_data.x->icon_bitmap = 0;
12169 if (STRINGP (file))
12170 bitmap_id = x_create_bitmap_from_file (f, file);
12171 else
12173 /* Create the GNU bitmap if necessary. */
12174 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
12175 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
12176 = x_create_bitmap_from_data (f, gnu_bits,
12177 gnu_width, gnu_height);
12179 /* The first time we create the GNU bitmap,
12180 this increments the ref-count one extra time.
12181 As a result, the GNU bitmap is never freed.
12182 That way, we don't have to worry about allocating it again. */
12183 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
12185 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
12188 x_wm_set_icon_pixmap (f, bitmap_id);
12189 f->output_data.x->icon_bitmap = bitmap_id;
12191 return 0;
12195 /* Make the x-window of frame F use a rectangle with text.
12196 Use ICON_NAME as the text. */
12199 x_text_icon (f, icon_name)
12200 struct frame *f;
12201 char *icon_name;
12203 if (FRAME_X_WINDOW (f) == 0)
12204 return 1;
12206 #ifdef HAVE_X11R4
12208 XTextProperty text;
12209 text.value = (unsigned char *) icon_name;
12210 text.encoding = XA_STRING;
12211 text.format = 8;
12212 text.nitems = strlen (icon_name);
12213 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), &text);
12215 #else /* not HAVE_X11R4 */
12216 XSetIconName (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f), icon_name);
12217 #endif /* not HAVE_X11R4 */
12219 if (f->output_data.x->icon_bitmap > 0)
12220 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
12221 f->output_data.x->icon_bitmap = 0;
12222 x_wm_set_icon_pixmap (f, 0);
12224 return 0;
12227 #define X_ERROR_MESSAGE_SIZE 200
12229 /* If non-nil, this should be a string.
12230 It means catch X errors and store the error message in this string. */
12232 static Lisp_Object x_error_message_string;
12234 /* An X error handler which stores the error message in
12235 x_error_message_string. This is called from x_error_handler if
12236 x_catch_errors is in effect. */
12238 static void
12239 x_error_catcher (display, error)
12240 Display *display;
12241 XErrorEvent *error;
12243 XGetErrorText (display, error->error_code,
12244 SDATA (x_error_message_string),
12245 X_ERROR_MESSAGE_SIZE);
12248 /* Begin trapping X errors for display DPY. Actually we trap X errors
12249 for all displays, but DPY should be the display you are actually
12250 operating on.
12252 After calling this function, X protocol errors no longer cause
12253 Emacs to exit; instead, they are recorded in the string
12254 stored in x_error_message_string.
12256 Calling x_check_errors signals an Emacs error if an X error has
12257 occurred since the last call to x_catch_errors or x_check_errors.
12259 Calling x_uncatch_errors resumes the normal error handling. */
12261 void x_check_errors ();
12262 static Lisp_Object x_catch_errors_unwind ();
12265 x_catch_errors (dpy)
12266 Display *dpy;
12268 int count = SPECPDL_INDEX ();
12270 /* Make sure any errors from previous requests have been dealt with. */
12271 XSync (dpy, False);
12273 record_unwind_protect (x_catch_errors_unwind,
12274 Fcons (make_save_value (dpy, 0),
12275 x_error_message_string));
12277 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
12278 SSET (x_error_message_string, 0, 0);
12280 return count;
12283 /* Unbind the binding that we made to check for X errors. */
12285 static Lisp_Object
12286 x_catch_errors_unwind (old_val)
12287 Lisp_Object old_val;
12289 Lisp_Object first;
12291 first = XCAR (old_val);
12293 XSync (XSAVE_VALUE (first)->pointer, False);
12295 x_error_message_string = XCDR (old_val);
12296 return Qnil;
12299 /* If any X protocol errors have arrived since the last call to
12300 x_catch_errors or x_check_errors, signal an Emacs error using
12301 sprintf (a buffer, FORMAT, the x error message text) as the text. */
12303 void
12304 x_check_errors (dpy, format)
12305 Display *dpy;
12306 char *format;
12308 /* Make sure to catch any errors incurred so far. */
12309 XSync (dpy, False);
12311 if (SREF (x_error_message_string, 0))
12312 error (format, SDATA (x_error_message_string));
12315 /* Nonzero if we had any X protocol errors
12316 since we did x_catch_errors on DPY. */
12319 x_had_errors_p (dpy)
12320 Display *dpy;
12322 /* Make sure to catch any errors incurred so far. */
12323 XSync (dpy, False);
12325 return SREF (x_error_message_string, 0) != 0;
12328 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12330 void
12331 x_clear_errors (dpy)
12332 Display *dpy;
12334 SSET (x_error_message_string, 0, 0);
12337 /* Stop catching X protocol errors and let them make Emacs die.
12338 DPY should be the display that was passed to x_catch_errors.
12339 COUNT should be the value that was returned by
12340 the corresponding call to x_catch_errors. */
12342 void
12343 x_uncatch_errors (dpy, count)
12344 Display *dpy;
12345 int count;
12347 unbind_to (count, Qnil);
12350 #if 0
12351 static unsigned int x_wire_count;
12352 x_trace_wire ()
12354 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12356 #endif /* ! 0 */
12359 /* Handle SIGPIPE, which can happen when the connection to a server
12360 simply goes away. SIGPIPE is handled by x_connection_signal.
12361 Don't need to do anything, because the write which caused the
12362 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12363 which will do the appropriate cleanup for us. */
12365 static SIGTYPE
12366 x_connection_signal (signalnum) /* If we don't have an argument, */
12367 int signalnum; /* some compilers complain in signal calls. */
12369 #ifdef USG
12370 /* USG systems forget handlers when they are used;
12371 must reestablish each time */
12372 signal (signalnum, x_connection_signal);
12373 #endif /* USG */
12377 /************************************************************************
12378 Handling X errors
12379 ************************************************************************/
12381 /* Error message passed to x_connection_closed. */
12383 static char *error_msg;
12385 /* Function installed as fatal_error_signal_hook in
12386 x_connection_closed. Print the X error message, and exit normally,
12387 instead of dumping core when XtCloseDisplay fails. */
12389 static void
12390 x_fatal_error_signal ()
12392 fprintf (stderr, "%s\n", error_msg);
12393 exit (70);
12396 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12397 the text of an error message that lead to the connection loss. */
12399 static SIGTYPE
12400 x_connection_closed (dpy, error_message)
12401 Display *dpy;
12402 char *error_message;
12404 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12405 Lisp_Object frame, tail;
12406 int count;
12408 error_msg = (char *) alloca (strlen (error_message) + 1);
12409 strcpy (error_msg, error_message);
12410 handling_signal = 0;
12412 /* Prevent being called recursively because of an error condition
12413 below. Otherwise, we might end up with printing ``can't find per
12414 display information'' in the recursive call instead of printing
12415 the original message here. */
12416 count = x_catch_errors (dpy);
12418 /* We have to close the display to inform Xt that it doesn't
12419 exist anymore. If we don't, Xt will continue to wait for
12420 events from the display. As a consequence, a sequence of
12422 M-x make-frame-on-display RET :1 RET
12423 ...kill the new frame, so that we get an IO error...
12424 M-x make-frame-on-display RET :1 RET
12426 will indefinitely wait in Xt for events for display `:1', opened
12427 in the first class to make-frame-on-display.
12429 Closing the display is reported to lead to a bus error on
12430 OpenWindows in certain situations. I suspect that is a bug
12431 in OpenWindows. I don't know how to cicumvent it here. */
12433 #ifdef USE_X_TOOLKIT
12434 /* If DPYINFO is null, this means we didn't open the display
12435 in the first place, so don't try to close it. */
12436 if (dpyinfo)
12438 extern void (*fatal_error_signal_hook) P_ ((void));
12439 fatal_error_signal_hook = x_fatal_error_signal;
12440 XtCloseDisplay (dpy);
12441 fatal_error_signal_hook = NULL;
12443 #endif
12445 /* Indicate that this display is dead. */
12446 if (dpyinfo)
12447 dpyinfo->display = 0;
12449 /* First delete frames whose mini-buffers are on frames
12450 that are on the dead display. */
12451 FOR_EACH_FRAME (tail, frame)
12453 Lisp_Object minibuf_frame;
12454 minibuf_frame
12455 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12456 if (FRAME_X_P (XFRAME (frame))
12457 && FRAME_X_P (XFRAME (minibuf_frame))
12458 && ! EQ (frame, minibuf_frame)
12459 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12460 Fdelete_frame (frame, Qt);
12463 /* Now delete all remaining frames on the dead display.
12464 We are now sure none of these is used as the mini-buffer
12465 for another frame that we need to delete. */
12466 FOR_EACH_FRAME (tail, frame)
12467 if (FRAME_X_P (XFRAME (frame))
12468 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12470 /* Set this to t so that Fdelete_frame won't get confused
12471 trying to find a replacement. */
12472 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12473 Fdelete_frame (frame, Qt);
12476 if (dpyinfo)
12477 x_delete_display (dpyinfo);
12479 x_uncatch_errors (dpy, count);
12481 if (x_display_list == 0)
12483 fprintf (stderr, "%s\n", error_msg);
12484 shut_down_emacs (0, 0, Qnil);
12485 exit (70);
12488 /* Ordinary stack unwind doesn't deal with these. */
12489 #ifdef SIGIO
12490 sigunblock (sigmask (SIGIO));
12491 #endif
12492 sigunblock (sigmask (SIGALRM));
12493 TOTALLY_UNBLOCK_INPUT;
12495 clear_waiting_for_input ();
12496 error ("%s", error_msg);
12500 /* This is the usual handler for X protocol errors.
12501 It kills all frames on the display that we got the error for.
12502 If that was the only one, it prints an error message and kills Emacs. */
12504 static void
12505 x_error_quitter (display, error)
12506 Display *display;
12507 XErrorEvent *error;
12509 char buf[256], buf1[356];
12511 /* Note that there is no real way portable across R3/R4 to get the
12512 original error handler. */
12514 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12515 sprintf (buf1, "X protocol error: %s on protocol request %d",
12516 buf, error->request_code);
12517 x_connection_closed (display, buf1);
12521 /* This is the first-level handler for X protocol errors.
12522 It calls x_error_quitter or x_error_catcher. */
12524 static int
12525 x_error_handler (display, error)
12526 Display *display;
12527 XErrorEvent *error;
12529 if (! NILP (x_error_message_string))
12530 x_error_catcher (display, error);
12531 else
12532 x_error_quitter (display, error);
12533 return 0;
12536 /* This is the handler for X IO errors, always.
12537 It kills all frames on the display that we lost touch with.
12538 If that was the only one, it prints an error message and kills Emacs. */
12540 static int
12541 x_io_error_quitter (display)
12542 Display *display;
12544 char buf[256];
12546 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12547 x_connection_closed (display, buf);
12548 return 0;
12551 /* Changing the font of the frame. */
12553 /* Give frame F the font named FONTNAME as its default font, and
12554 return the full name of that font. FONTNAME may be a wildcard
12555 pattern; in that case, we choose some font that fits the pattern.
12556 The return value shows which font we chose. */
12558 Lisp_Object
12559 x_new_font (f, fontname)
12560 struct frame *f;
12561 register char *fontname;
12563 struct font_info *fontp
12564 = FS_LOAD_FONT (f, 0, fontname, -1);
12566 if (!fontp)
12567 return Qnil;
12569 f->output_data.x->font = (XFontStruct *) (fontp->font);
12570 f->output_data.x->baseline_offset = fontp->baseline_offset;
12571 f->output_data.x->fontset = -1;
12573 compute_fringe_widths (f, 1);
12575 /* Compute the scroll bar width in character columns. */
12576 if (f->scroll_bar_pixel_width > 0)
12578 int wid = FONT_WIDTH (f->output_data.x->font);
12579 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12581 else
12583 int wid = FONT_WIDTH (f->output_data.x->font);
12584 f->scroll_bar_cols = (14 + wid - 1) / wid;
12587 /* Now make the frame display the given font. */
12588 if (FRAME_X_WINDOW (f) != 0)
12590 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12591 f->output_data.x->font->fid);
12592 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12593 f->output_data.x->font->fid);
12594 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12595 f->output_data.x->font->fid);
12597 frame_update_line_height (f);
12599 /* Don't change the size of a tip frame; there's no point in
12600 doing it because it's done in Fx_show_tip, and it leads to
12601 problems because the tip frame has no widget. */
12602 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12603 x_set_window_size (f, 0, f->width, f->height);
12605 else
12606 /* If we are setting a new frame's font for the first time,
12607 there are no faces yet, so this font's height is the line height. */
12608 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12610 return build_string (fontp->full_name);
12613 /* Give frame F the fontset named FONTSETNAME as its default font, and
12614 return the full name of that fontset. FONTSETNAME may be a wildcard
12615 pattern; in that case, we choose some fontset that fits the pattern.
12616 The return value shows which fontset we chose. */
12618 Lisp_Object
12619 x_new_fontset (f, fontsetname)
12620 struct frame *f;
12621 char *fontsetname;
12623 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12624 Lisp_Object result;
12626 if (fontset < 0)
12627 return Qnil;
12629 if (f->output_data.x->fontset == fontset)
12630 /* This fontset is already set in frame F. There's nothing more
12631 to do. */
12632 return fontset_name (fontset);
12634 result = x_new_font (f, (SDATA (fontset_ascii (fontset))));
12636 if (!STRINGP (result))
12637 /* Can't load ASCII font. */
12638 return Qnil;
12640 /* Since x_new_font doesn't update any fontset information, do it now. */
12641 f->output_data.x->fontset = fontset;
12643 #ifdef HAVE_X_I18N
12644 if (FRAME_XIC (f)
12645 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12646 xic_set_xfontset (f, SDATA (fontset_ascii (fontset)));
12647 #endif
12649 return build_string (fontsetname);
12653 /***********************************************************************
12654 X Input Methods
12655 ***********************************************************************/
12657 #ifdef HAVE_X_I18N
12659 #ifdef HAVE_X11R6
12661 /* XIM destroy callback function, which is called whenever the
12662 connection to input method XIM dies. CLIENT_DATA contains a
12663 pointer to the x_display_info structure corresponding to XIM. */
12665 static void
12666 xim_destroy_callback (xim, client_data, call_data)
12667 XIM xim;
12668 XPointer client_data;
12669 XPointer call_data;
12671 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12672 Lisp_Object frame, tail;
12674 BLOCK_INPUT;
12676 /* No need to call XDestroyIC.. */
12677 FOR_EACH_FRAME (tail, frame)
12679 struct frame *f = XFRAME (frame);
12680 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12682 FRAME_XIC (f) = NULL;
12683 if (FRAME_XIC_FONTSET (f))
12685 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12686 FRAME_XIC_FONTSET (f) = NULL;
12691 /* No need to call XCloseIM. */
12692 dpyinfo->xim = NULL;
12693 XFree (dpyinfo->xim_styles);
12694 UNBLOCK_INPUT;
12697 #endif /* HAVE_X11R6 */
12699 #ifdef HAVE_X11R6
12700 /* This isn't prototyped in OSF 5.0 or 5.1a. */
12701 extern char *XSetIMValues P_ ((XIM, ...));
12702 #endif
12704 /* Open the connection to the XIM server on display DPYINFO.
12705 RESOURCE_NAME is the resource name Emacs uses. */
12707 static void
12708 xim_open_dpy (dpyinfo, resource_name)
12709 struct x_display_info *dpyinfo;
12710 char *resource_name;
12712 #ifdef USE_XIM
12713 XIM xim;
12715 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12716 dpyinfo->xim = xim;
12718 if (xim)
12720 #ifdef HAVE_X11R6
12721 XIMCallback destroy;
12722 #endif
12724 /* Get supported styles and XIM values. */
12725 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12727 #ifdef HAVE_X11R6
12728 destroy.callback = xim_destroy_callback;
12729 destroy.client_data = (XPointer)dpyinfo;
12730 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12731 #endif
12734 #else /* not USE_XIM */
12735 dpyinfo->xim = NULL;
12736 #endif /* not USE_XIM */
12740 #ifdef HAVE_X11R6_XIM
12742 struct xim_inst_t
12744 struct x_display_info *dpyinfo;
12745 char *resource_name;
12748 /* XIM instantiate callback function, which is called whenever an XIM
12749 server is available. DISPLAY is the display of the XIM.
12750 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12751 when the callback was registered. */
12753 static void
12754 xim_instantiate_callback (display, client_data, call_data)
12755 Display *display;
12756 XPointer client_data;
12757 XPointer call_data;
12759 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12760 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12762 /* We don't support multiple XIM connections. */
12763 if (dpyinfo->xim)
12764 return;
12766 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12768 /* Create XIC for the existing frames on the same display, as long
12769 as they have no XIC. */
12770 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12772 Lisp_Object tail, frame;
12774 BLOCK_INPUT;
12775 FOR_EACH_FRAME (tail, frame)
12777 struct frame *f = XFRAME (frame);
12779 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12780 if (FRAME_XIC (f) == NULL)
12782 create_frame_xic (f);
12783 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12784 xic_set_statusarea (f);
12785 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12787 struct window *w = XWINDOW (f->selected_window);
12788 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12793 UNBLOCK_INPUT;
12797 #endif /* HAVE_X11R6_XIM */
12800 /* Open a connection to the XIM server on display DPYINFO.
12801 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12802 connection only at the first time. On X11R6, open the connection
12803 in the XIM instantiate callback function. */
12805 static void
12806 xim_initialize (dpyinfo, resource_name)
12807 struct x_display_info *dpyinfo;
12808 char *resource_name;
12810 #ifdef USE_XIM
12811 #ifdef HAVE_X11R6_XIM
12812 struct xim_inst_t *xim_inst;
12813 int len;
12815 dpyinfo->xim = NULL;
12816 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12817 xim_inst->dpyinfo = dpyinfo;
12818 len = strlen (resource_name);
12819 xim_inst->resource_name = (char *) xmalloc (len + 1);
12820 bcopy (resource_name, xim_inst->resource_name, len + 1);
12821 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12822 resource_name, EMACS_CLASS,
12823 xim_instantiate_callback,
12824 /* Fixme: This is XPointer in
12825 XFree86 but (XPointer *) on
12826 Tru64, at least. */
12827 (XPointer) xim_inst);
12828 #else /* not HAVE_X11R6_XIM */
12829 dpyinfo->xim = NULL;
12830 xim_open_dpy (dpyinfo, resource_name);
12831 #endif /* not HAVE_X11R6_XIM */
12833 #else /* not USE_XIM */
12834 dpyinfo->xim = NULL;
12835 #endif /* not USE_XIM */
12839 /* Close the connection to the XIM server on display DPYINFO. */
12841 static void
12842 xim_close_dpy (dpyinfo)
12843 struct x_display_info *dpyinfo;
12845 #ifdef USE_XIM
12846 #ifdef HAVE_X11R6_XIM
12847 if (dpyinfo->display)
12848 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12849 NULL, EMACS_CLASS,
12850 xim_instantiate_callback, NULL);
12851 #endif /* not HAVE_X11R6_XIM */
12852 if (dpyinfo->display)
12853 XCloseIM (dpyinfo->xim);
12854 dpyinfo->xim = NULL;
12855 XFree (dpyinfo->xim_styles);
12856 #endif /* USE_XIM */
12859 #endif /* not HAVE_X11R6_XIM */
12863 /* Calculate the absolute position in frame F
12864 from its current recorded position values and gravity. */
12866 void
12867 x_calc_absolute_position (f)
12868 struct frame *f;
12870 Window child;
12871 int win_x = 0, win_y = 0;
12872 int flags = f->output_data.x->size_hint_flags;
12873 int this_window;
12875 /* We have nothing to do if the current position
12876 is already for the top-left corner. */
12877 if (! ((flags & XNegative) || (flags & YNegative)))
12878 return;
12880 this_window = FRAME_OUTER_WINDOW (f);
12882 /* Find the position of the outside upper-left corner of
12883 the inner window, with respect to the outer window.
12884 But do this only if we will need the results. */
12885 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12887 int count;
12889 BLOCK_INPUT;
12890 count = x_catch_errors (FRAME_X_DISPLAY (f));
12891 while (1)
12893 x_clear_errors (FRAME_X_DISPLAY (f));
12894 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12896 /* From-window, to-window. */
12897 this_window,
12898 f->output_data.x->parent_desc,
12900 /* From-position, to-position. */
12901 0, 0, &win_x, &win_y,
12903 /* Child of win. */
12904 &child);
12905 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12907 Window newroot, newparent = 0xdeadbeef;
12908 Window *newchildren;
12909 unsigned int nchildren;
12911 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12912 &newparent, &newchildren, &nchildren))
12913 break;
12915 XFree ((char *) newchildren);
12917 f->output_data.x->parent_desc = newparent;
12919 else
12920 break;
12923 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12924 UNBLOCK_INPUT;
12927 /* Treat negative positions as relative to the leftmost bottommost
12928 position that fits on the screen. */
12929 if (flags & XNegative)
12930 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12931 - 2 * f->output_data.x->border_width - win_x
12932 - PIXEL_WIDTH (f)
12933 + f->output_data.x->left_pos);
12936 int height = PIXEL_HEIGHT (f);
12938 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12939 /* Something is fishy here. When using Motif, starting Emacs with
12940 `-g -0-0', the frame appears too low by a few pixels.
12942 This seems to be so because initially, while Emacs is starting,
12943 the column widget's height and the frame's pixel height are
12944 different. The column widget's height is the right one. In
12945 later invocations, when Emacs is up, the frame's pixel height
12946 is right, though.
12948 It's not obvious where the initial small difference comes from.
12949 2000-12-01, gerd. */
12951 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12952 #endif
12954 if (flags & YNegative)
12955 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12956 - 2 * f->output_data.x->border_width
12957 - win_y
12958 - height
12959 + f->output_data.x->top_pos);
12962 /* The left_pos and top_pos
12963 are now relative to the top and left screen edges,
12964 so the flags should correspond. */
12965 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12968 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12969 to really change the position, and 0 when calling from
12970 x_make_frame_visible (in that case, XOFF and YOFF are the current
12971 position values). It is -1 when calling from x_set_frame_parameters,
12972 which means, do adjust for borders but don't change the gravity. */
12974 void
12975 x_set_offset (f, xoff, yoff, change_gravity)
12976 struct frame *f;
12977 register int xoff, yoff;
12978 int change_gravity;
12980 int modified_top, modified_left;
12982 if (change_gravity > 0)
12984 f->output_data.x->top_pos = yoff;
12985 f->output_data.x->left_pos = xoff;
12986 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12987 if (xoff < 0)
12988 f->output_data.x->size_hint_flags |= XNegative;
12989 if (yoff < 0)
12990 f->output_data.x->size_hint_flags |= YNegative;
12991 f->output_data.x->win_gravity = NorthWestGravity;
12993 x_calc_absolute_position (f);
12995 BLOCK_INPUT;
12996 x_wm_set_size_hint (f, (long) 0, 0);
12998 modified_left = f->output_data.x->left_pos;
12999 modified_top = f->output_data.x->top_pos;
13000 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
13001 this seems to be unnecessary and incorrect. rms, 4/17/97. */
13002 /* It is a mystery why we need to add the border_width here
13003 when the frame is already visible, but experiment says we do. */
13004 if (change_gravity != 0)
13006 modified_left += f->output_data.x->border_width;
13007 modified_top += f->output_data.x->border_width;
13009 #endif
13011 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13012 modified_left, modified_top);
13013 UNBLOCK_INPUT;
13016 /* Check if we need to resize the frame due to a fullscreen request.
13017 If so needed, resize the frame. */
13018 static void
13019 x_check_fullscreen (f)
13020 struct frame *f;
13022 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
13024 int width, height, ign;
13026 x_real_positions (f, &f->output_data.x->left_pos,
13027 &f->output_data.x->top_pos);
13029 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
13031 /* We do not need to move the window, it shall be taken care of
13032 when setting WM manager hints.
13033 If the frame is visible already, the position is checked by
13034 x_check_fullscreen_move. */
13035 if (f->width != width || f->height != height)
13037 change_frame_size (f, height, width, 0, 1, 0);
13038 SET_FRAME_GARBAGED (f);
13039 cancel_mouse_face (f);
13041 /* Wait for the change of frame size to occur */
13042 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
13048 /* If frame parameters are set after the frame is mapped, we need to move
13049 the window. This is done in xfns.c.
13050 Some window managers moves the window to the right position, some
13051 moves the outer window manager window to the specified position.
13052 Here we check that we are in the right spot. If not, make a second
13053 move, assuming we are dealing with the second kind of window manager. */
13054 static void
13055 x_check_fullscreen_move (f)
13056 struct frame *f;
13058 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
13060 int expect_top = f->output_data.x->top_pos;
13061 int expect_left = f->output_data.x->left_pos;
13063 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13064 expect_top = 0;
13065 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13066 expect_left = 0;
13068 if (expect_top != f->output_data.x->top_pos
13069 || expect_left != f->output_data.x->left_pos)
13070 x_set_offset (f, expect_left, expect_top, 1);
13072 /* Just do this once */
13073 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
13078 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
13079 wanted positions of the WM window (not emacs window).
13080 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
13081 window (FRAME_X_WINDOW).
13083 void
13084 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
13085 struct frame *f;
13086 int *width;
13087 int *height;
13088 int *top_pos;
13089 int *left_pos;
13091 int newwidth = f->width, newheight = f->height;
13093 *top_pos = f->output_data.x->top_pos;
13094 *left_pos = f->output_data.x->left_pos;
13096 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
13098 int ph;
13100 ph = FRAME_X_DISPLAY_INFO (f)->height;
13101 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13102 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
13103 - f->output_data.x->y_pixels_diff;
13104 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
13105 *top_pos = 0;
13108 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
13110 int pw;
13112 pw = FRAME_X_DISPLAY_INFO (f)->width;
13113 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13114 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
13115 - f->output_data.x->x_pixels_diff;
13116 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
13117 *left_pos = 0;
13120 *width = newwidth;
13121 *height = newheight;
13125 /* Change the size of frame F's X window to COLS/ROWS in the case F
13126 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
13127 top-left-corner window gravity for this size change and subsequent
13128 size changes. Otherwise we leave the window gravity unchanged. */
13130 static void
13131 x_set_window_size_1 (f, change_gravity, cols, rows)
13132 struct frame *f;
13133 int change_gravity;
13134 int cols, rows;
13136 int pixelwidth, pixelheight;
13138 check_frame_size (f, &rows, &cols);
13139 f->output_data.x->vertical_scroll_bar_extra
13140 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
13142 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
13143 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
13144 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
13146 compute_fringe_widths (f, 0);
13148 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
13149 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
13151 f->output_data.x->win_gravity = NorthWestGravity;
13152 x_wm_set_size_hint (f, (long) 0, 0);
13154 XSync (FRAME_X_DISPLAY (f), False);
13155 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13156 pixelwidth, pixelheight);
13158 /* Now, strictly speaking, we can't be sure that this is accurate,
13159 but the window manager will get around to dealing with the size
13160 change request eventually, and we'll hear how it went when the
13161 ConfigureNotify event gets here.
13163 We could just not bother storing any of this information here,
13164 and let the ConfigureNotify event set everything up, but that
13165 might be kind of confusing to the Lisp code, since size changes
13166 wouldn't be reported in the frame parameters until some random
13167 point in the future when the ConfigureNotify event arrives.
13169 We pass 1 for DELAY since we can't run Lisp code inside of
13170 a BLOCK_INPUT. */
13171 change_frame_size (f, rows, cols, 0, 1, 0);
13172 PIXEL_WIDTH (f) = pixelwidth;
13173 PIXEL_HEIGHT (f) = pixelheight;
13175 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
13176 receive in the ConfigureNotify event; if we get what we asked
13177 for, then the event won't cause the screen to become garbaged, so
13178 we have to make sure to do it here. */
13179 SET_FRAME_GARBAGED (f);
13181 XFlush (FRAME_X_DISPLAY (f));
13185 /* Call this to change the size of frame F's x-window.
13186 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
13187 for this size change and subsequent size changes.
13188 Otherwise we leave the window gravity unchanged. */
13190 void
13191 x_set_window_size (f, change_gravity, cols, rows)
13192 struct frame *f;
13193 int change_gravity;
13194 int cols, rows;
13196 BLOCK_INPUT;
13198 #ifdef USE_GTK
13199 if (FRAME_GTK_WIDGET (f))
13200 xg_frame_set_char_size (f, cols, rows);
13201 else
13202 x_set_window_size_1 (f, change_gravity, cols, rows);
13203 #elif USE_X_TOOLKIT
13205 if (f->output_data.x->widget != NULL)
13207 /* The x and y position of the widget is clobbered by the
13208 call to XtSetValues within EmacsFrameSetCharSize.
13209 This is a real kludge, but I don't understand Xt so I can't
13210 figure out a correct fix. Can anyone else tell me? -- rms. */
13211 int xpos = f->output_data.x->widget->core.x;
13212 int ypos = f->output_data.x->widget->core.y;
13213 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
13214 f->output_data.x->widget->core.x = xpos;
13215 f->output_data.x->widget->core.y = ypos;
13217 else
13218 x_set_window_size_1 (f, change_gravity, cols, rows);
13220 #else /* not USE_X_TOOLKIT */
13222 x_set_window_size_1 (f, change_gravity, cols, rows);
13224 #endif /* not USE_X_TOOLKIT */
13226 /* If cursor was outside the new size, mark it as off. */
13227 mark_window_cursors_off (XWINDOW (f->root_window));
13229 /* Clear out any recollection of where the mouse highlighting was,
13230 since it might be in a place that's outside the new frame size.
13231 Actually checking whether it is outside is a pain in the neck,
13232 so don't try--just let the highlighting be done afresh with new size. */
13233 cancel_mouse_face (f);
13235 UNBLOCK_INPUT;
13238 /* Mouse warping. */
13240 void
13241 x_set_mouse_position (f, x, y)
13242 struct frame *f;
13243 int x, y;
13245 int pix_x, pix_y;
13247 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13248 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
13250 if (pix_x < 0) pix_x = 0;
13251 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13253 if (pix_y < 0) pix_y = 0;
13254 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13256 BLOCK_INPUT;
13258 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13259 0, 0, 0, 0, pix_x, pix_y);
13260 UNBLOCK_INPUT;
13263 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13265 void
13266 x_set_mouse_pixel_position (f, pix_x, pix_y)
13267 struct frame *f;
13268 int pix_x, pix_y;
13270 BLOCK_INPUT;
13272 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13273 0, 0, 0, 0, pix_x, pix_y);
13274 UNBLOCK_INPUT;
13277 /* focus shifting, raising and lowering. */
13279 void
13280 x_focus_on_frame (f)
13281 struct frame *f;
13283 #if 0 /* This proves to be unpleasant. */
13284 x_raise_frame (f);
13285 #endif
13286 #if 0
13287 /* I don't think that the ICCCM allows programs to do things like this
13288 without the interaction of the window manager. Whatever you end up
13289 doing with this code, do it to x_unfocus_frame too. */
13290 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13291 RevertToPointerRoot, CurrentTime);
13292 #endif /* ! 0 */
13295 void
13296 x_unfocus_frame (f)
13297 struct frame *f;
13299 #if 0
13300 /* Look at the remarks in x_focus_on_frame. */
13301 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13302 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13303 RevertToPointerRoot, CurrentTime);
13304 #endif /* ! 0 */
13307 /* Raise frame F. */
13309 void
13310 x_raise_frame (f)
13311 struct frame *f;
13313 if (f->async_visible)
13315 BLOCK_INPUT;
13316 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
13317 XFlush (FRAME_X_DISPLAY (f));
13318 UNBLOCK_INPUT;
13322 /* Lower frame F. */
13324 void
13325 x_lower_frame (f)
13326 struct frame *f;
13328 if (f->async_visible)
13330 BLOCK_INPUT;
13331 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f));
13332 XFlush (FRAME_X_DISPLAY (f));
13333 UNBLOCK_INPUT;
13337 static void
13338 XTframe_raise_lower (f, raise_flag)
13339 FRAME_PTR f;
13340 int raise_flag;
13342 if (raise_flag)
13343 x_raise_frame (f);
13344 else
13345 x_lower_frame (f);
13348 /* Change of visibility. */
13350 /* This tries to wait until the frame is really visible.
13351 However, if the window manager asks the user where to position
13352 the frame, this will return before the user finishes doing that.
13353 The frame will not actually be visible at that time,
13354 but it will become visible later when the window manager
13355 finishes with it. */
13357 void
13358 x_make_frame_visible (f)
13359 struct frame *f;
13361 Lisp_Object type;
13362 int original_top, original_left;
13363 int retry_count = 2;
13365 retry:
13367 BLOCK_INPUT;
13369 type = x_icon_type (f);
13370 if (!NILP (type))
13371 x_bitmap_icon (f, type);
13373 if (! FRAME_VISIBLE_P (f))
13375 /* We test FRAME_GARBAGED_P here to make sure we don't
13376 call x_set_offset a second time
13377 if we get to x_make_frame_visible a second time
13378 before the window gets really visible. */
13379 if (! FRAME_ICONIFIED_P (f)
13380 && ! f->output_data.x->asked_for_visible)
13381 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13383 f->output_data.x->asked_for_visible = 1;
13385 if (! EQ (Vx_no_window_manager, Qt))
13386 x_wm_set_window_state (f, NormalState);
13387 #ifdef USE_X_TOOLKIT
13388 /* This was XtPopup, but that did nothing for an iconified frame. */
13389 XtMapWidget (f->output_data.x->widget);
13390 #else /* not USE_X_TOOLKIT */
13391 #ifdef USE_GTK
13392 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
13393 gtk_window_deiconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
13394 #else
13395 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13396 #endif /* not USE_GTK */
13397 #endif /* not USE_X_TOOLKIT */
13398 #if 0 /* This seems to bring back scroll bars in the wrong places
13399 if the window configuration has changed. They seem
13400 to come back ok without this. */
13401 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13402 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13403 #endif
13406 XFlush (FRAME_X_DISPLAY (f));
13408 /* Synchronize to ensure Emacs knows the frame is visible
13409 before we do anything else. We do this loop with input not blocked
13410 so that incoming events are handled. */
13412 Lisp_Object frame;
13413 int count;
13414 /* This must be before UNBLOCK_INPUT
13415 since events that arrive in response to the actions above
13416 will set it when they are handled. */
13417 int previously_visible = f->output_data.x->has_been_visible;
13419 original_left = f->output_data.x->left_pos;
13420 original_top = f->output_data.x->top_pos;
13422 /* This must come after we set COUNT. */
13423 UNBLOCK_INPUT;
13425 /* We unblock here so that arriving X events are processed. */
13427 /* Now move the window back to where it was "supposed to be".
13428 But don't do it if the gravity is negative.
13429 When the gravity is negative, this uses a position
13430 that is 3 pixels too low. Perhaps that's really the border width.
13432 Don't do this if the window has never been visible before,
13433 because the window manager may choose the position
13434 and we don't want to override it. */
13436 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13437 && f->output_data.x->win_gravity == NorthWestGravity
13438 && previously_visible)
13440 Drawable rootw;
13441 int x, y;
13442 unsigned int width, height, border, depth;
13444 BLOCK_INPUT;
13446 /* On some window managers (such as FVWM) moving an existing
13447 window, even to the same place, causes the window manager
13448 to introduce an offset. This can cause the window to move
13449 to an unexpected location. Check the geometry (a little
13450 slow here) and then verify that the window is in the right
13451 place. If the window is not in the right place, move it
13452 there, and take the potential window manager hit. */
13453 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13454 &rootw, &x, &y, &width, &height, &border, &depth);
13456 if (original_left != x || original_top != y)
13457 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13458 original_left, original_top);
13460 UNBLOCK_INPUT;
13463 XSETFRAME (frame, f);
13465 /* Wait until the frame is visible. Process X events until a
13466 MapNotify event has been seen, or until we think we won't get a
13467 MapNotify at all.. */
13468 for (count = input_signal_count + 10;
13469 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13471 /* Force processing of queued events. */
13472 x_sync (f);
13474 /* Machines that do polling rather than SIGIO have been
13475 observed to go into a busy-wait here. So we'll fake an
13476 alarm signal to let the handler know that there's something
13477 to be read. We used to raise a real alarm, but it seems
13478 that the handler isn't always enabled here. This is
13479 probably a bug. */
13480 if (input_polling_used ())
13482 /* It could be confusing if a real alarm arrives while
13483 processing the fake one. Turn it off and let the
13484 handler reset it. */
13485 extern void poll_for_input_1 P_ ((void));
13486 int old_poll_suppress_count = poll_suppress_count;
13487 poll_suppress_count = 1;
13488 poll_for_input_1 ();
13489 poll_suppress_count = old_poll_suppress_count;
13492 /* See if a MapNotify event has been processed. */
13493 FRAME_SAMPLE_VISIBILITY (f);
13496 /* 2000-09-28: In
13498 (let ((f (selected-frame)))
13499 (iconify-frame f)
13500 (raise-frame f))
13502 the frame is not raised with various window managers on
13503 FreeBSD, Linux and Solaris. It turns out that, for some
13504 unknown reason, the call to XtMapWidget is completely ignored.
13505 Mapping the widget a second time works. */
13507 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13508 goto retry;
13512 /* Change from mapped state to withdrawn state. */
13514 /* Make the frame visible (mapped and not iconified). */
13516 void
13517 x_make_frame_invisible (f)
13518 struct frame *f;
13520 Window window;
13522 /* Use the frame's outermost window, not the one we normally draw on. */
13523 window = FRAME_OUTER_WINDOW (f);
13525 /* Don't keep the highlight on an invisible frame. */
13526 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13527 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13529 #if 0/* This might add unreliability; I don't trust it -- rms. */
13530 if (! f->async_visible && ! f->async_iconified)
13531 return;
13532 #endif
13534 BLOCK_INPUT;
13536 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13537 that the current position of the window is user-specified, rather than
13538 program-specified, so that when the window is mapped again, it will be
13539 placed at the same location, without forcing the user to position it
13540 by hand again (they have already done that once for this window.) */
13541 x_wm_set_size_hint (f, (long) 0, 1);
13543 #ifdef USE_GTK
13544 if (FRAME_GTK_OUTER_WIDGET (f))
13546 gtk_widget_hide (FRAME_GTK_OUTER_WIDGET (f));
13547 goto out;
13549 #endif
13551 #ifdef HAVE_X11R4
13553 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13554 DefaultScreen (FRAME_X_DISPLAY (f))))
13556 UNBLOCK_INPUT_RESIGNAL;
13557 error ("Can't notify window manager of window withdrawal");
13559 #else /* ! defined (HAVE_X11R4) */
13561 /* Tell the window manager what we're going to do. */
13562 if (! EQ (Vx_no_window_manager, Qt))
13564 XEvent unmap;
13566 unmap.xunmap.type = UnmapNotify;
13567 unmap.xunmap.window = window;
13568 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13569 unmap.xunmap.from_configure = False;
13570 if (! XSendEvent (FRAME_X_DISPLAY (f),
13571 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13572 False,
13573 SubstructureRedirectMaskSubstructureNotifyMask,
13574 &unmap))
13576 UNBLOCK_INPUT_RESIGNAL;
13577 error ("Can't notify window manager of withdrawal");
13581 /* Unmap the window ourselves. Cheeky! */
13582 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13583 #endif /* ! defined (HAVE_X11R4) */
13585 out:
13586 /* We can't distinguish this from iconification
13587 just by the event that we get from the server.
13588 So we can't win using the usual strategy of letting
13589 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13590 and synchronize with the server to make sure we agree. */
13591 f->visible = 0;
13592 FRAME_ICONIFIED_P (f) = 0;
13593 f->async_visible = 0;
13594 f->async_iconified = 0;
13596 x_sync (f);
13598 UNBLOCK_INPUT;
13601 /* Change window state from mapped to iconified. */
13603 void
13604 x_iconify_frame (f)
13605 struct frame *f;
13607 int result;
13608 Lisp_Object type;
13610 /* Don't keep the highlight on an invisible frame. */
13611 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13612 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13614 if (f->async_iconified)
13615 return;
13617 BLOCK_INPUT;
13619 FRAME_SAMPLE_VISIBILITY (f);
13621 type = x_icon_type (f);
13622 if (!NILP (type))
13623 x_bitmap_icon (f, type);
13625 #ifdef USE_GTK
13626 if (FRAME_GTK_OUTER_WIDGET (f))
13628 if (! FRAME_VISIBLE_P (f))
13629 gtk_widget_show_all (FRAME_GTK_OUTER_WIDGET (f));
13631 gtk_window_iconify (GTK_WINDOW (FRAME_GTK_OUTER_WIDGET (f)));
13632 f->iconified = 1;
13633 f->visible = 1;
13634 f->async_iconified = 1;
13635 f->async_visible = 0;
13636 UNBLOCK_INPUT;
13637 return;
13639 #endif
13641 #ifdef USE_X_TOOLKIT
13643 if (! FRAME_VISIBLE_P (f))
13645 if (! EQ (Vx_no_window_manager, Qt))
13646 x_wm_set_window_state (f, IconicState);
13647 /* This was XtPopup, but that did nothing for an iconified frame. */
13648 XtMapWidget (f->output_data.x->widget);
13649 /* The server won't give us any event to indicate
13650 that an invisible frame was changed to an icon,
13651 so we have to record it here. */
13652 f->iconified = 1;
13653 f->visible = 1;
13654 f->async_iconified = 1;
13655 f->async_visible = 0;
13656 UNBLOCK_INPUT;
13657 return;
13660 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13661 XtWindow (f->output_data.x->widget),
13662 DefaultScreen (FRAME_X_DISPLAY (f)));
13663 UNBLOCK_INPUT;
13665 if (!result)
13666 error ("Can't notify window manager of iconification");
13668 f->async_iconified = 1;
13669 f->async_visible = 0;
13672 BLOCK_INPUT;
13673 XFlush (FRAME_X_DISPLAY (f));
13674 UNBLOCK_INPUT;
13675 #else /* not USE_X_TOOLKIT */
13677 /* Make sure the X server knows where the window should be positioned,
13678 in case the user deiconifies with the window manager. */
13679 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13680 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13682 /* Since we don't know which revision of X we're running, we'll use both
13683 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13685 /* X11R4: send a ClientMessage to the window manager using the
13686 WM_CHANGE_STATE type. */
13688 XEvent message;
13690 message.xclient.window = FRAME_X_WINDOW (f);
13691 message.xclient.type = ClientMessage;
13692 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13693 message.xclient.format = 32;
13694 message.xclient.data.l[0] = IconicState;
13696 if (! XSendEvent (FRAME_X_DISPLAY (f),
13697 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13698 False,
13699 SubstructureRedirectMask | SubstructureNotifyMask,
13700 &message))
13702 UNBLOCK_INPUT_RESIGNAL;
13703 error ("Can't notify window manager of iconification");
13707 /* X11R3: set the initial_state field of the window manager hints to
13708 IconicState. */
13709 x_wm_set_window_state (f, IconicState);
13711 if (!FRAME_VISIBLE_P (f))
13713 /* If the frame was withdrawn, before, we must map it. */
13714 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13717 f->async_iconified = 1;
13718 f->async_visible = 0;
13720 XFlush (FRAME_X_DISPLAY (f));
13721 UNBLOCK_INPUT;
13722 #endif /* not USE_X_TOOLKIT */
13726 /* Free X resources of frame F. */
13728 void
13729 x_free_frame_resources (f)
13730 struct frame *f;
13732 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13733 Lisp_Object bar;
13734 struct scroll_bar *b;
13736 BLOCK_INPUT;
13738 /* If a display connection is dead, don't try sending more
13739 commands to the X server. */
13740 if (dpyinfo->display)
13742 if (f->output_data.x->icon_desc)
13743 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13745 #ifdef USE_X_TOOLKIT
13746 /* Explicitly destroy the scroll bars of the frame. Without
13747 this, we get "BadDrawable" errors from the toolkit later on,
13748 presumably from expose events generated for the disappearing
13749 toolkit scroll bars. */
13750 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13752 b = XSCROLL_BAR (bar);
13753 x_scroll_bar_remove (b);
13755 #endif
13757 #ifdef HAVE_X_I18N
13758 if (FRAME_XIC (f))
13759 free_frame_xic (f);
13760 #endif
13762 #ifdef USE_X_TOOLKIT
13763 if (f->output_data.x->widget)
13765 XtDestroyWidget (f->output_data.x->widget);
13766 f->output_data.x->widget = NULL;
13768 /* Tooltips don't have widgets, only a simple X window, even if
13769 we are using a toolkit. */
13770 else if (FRAME_X_WINDOW (f))
13771 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13773 free_frame_menubar (f);
13774 #else /* !USE_X_TOOLKIT */
13776 #ifdef USE_GTK
13777 /* In the GTK version, tooltips are normal X
13778 frames. We must check and free both types. */
13779 if (FRAME_GTK_OUTER_WIDGET (f))
13781 gtk_widget_destroy (FRAME_GTK_OUTER_WIDGET (f));
13782 FRAME_X_WINDOW (f) = 0; /* Set to avoid XDestroyWindow below */
13783 FRAME_GTK_OUTER_WIDGET (f) = 0;
13785 #endif /* USE_GTK */
13787 if (FRAME_X_WINDOW (f))
13788 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13789 #endif /* !USE_X_TOOLKIT */
13791 unload_color (f, f->output_data.x->foreground_pixel);
13792 unload_color (f, f->output_data.x->background_pixel);
13793 unload_color (f, f->output_data.x->cursor_pixel);
13794 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13795 unload_color (f, f->output_data.x->border_pixel);
13796 unload_color (f, f->output_data.x->mouse_pixel);
13798 if (f->output_data.x->scroll_bar_background_pixel != -1)
13799 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13800 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13801 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13802 #ifdef USE_TOOLKIT_SCROLL_BARS
13803 /* Scrollbar shadow colors. */
13804 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13805 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13806 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13807 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13808 #endif /* USE_TOOLKIT_SCROLL_BARS */
13809 if (f->output_data.x->white_relief.allocated_p)
13810 unload_color (f, f->output_data.x->white_relief.pixel);
13811 if (f->output_data.x->black_relief.allocated_p)
13812 unload_color (f, f->output_data.x->black_relief.pixel);
13814 if (FRAME_FACE_CACHE (f))
13815 free_frame_faces (f);
13817 x_free_gcs (f);
13818 XFlush (FRAME_X_DISPLAY (f));
13821 if (f->output_data.x->saved_menu_event)
13822 xfree (f->output_data.x->saved_menu_event);
13824 xfree (f->output_data.x);
13825 f->output_data.x = NULL;
13827 if (f == dpyinfo->x_focus_frame)
13828 dpyinfo->x_focus_frame = 0;
13829 if (f == dpyinfo->x_focus_event_frame)
13830 dpyinfo->x_focus_event_frame = 0;
13831 if (f == dpyinfo->x_highlight_frame)
13832 dpyinfo->x_highlight_frame = 0;
13834 if (f == dpyinfo->mouse_face_mouse_frame)
13836 dpyinfo->mouse_face_beg_row
13837 = dpyinfo->mouse_face_beg_col = -1;
13838 dpyinfo->mouse_face_end_row
13839 = dpyinfo->mouse_face_end_col = -1;
13840 dpyinfo->mouse_face_window = Qnil;
13841 dpyinfo->mouse_face_deferred_gc = 0;
13842 dpyinfo->mouse_face_mouse_frame = 0;
13845 UNBLOCK_INPUT;
13849 /* Destroy the X window of frame F. */
13851 void
13852 x_destroy_window (f)
13853 struct frame *f;
13855 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13857 /* If a display connection is dead, don't try sending more
13858 commands to the X server. */
13859 if (dpyinfo->display != 0)
13860 x_free_frame_resources (f);
13862 dpyinfo->reference_count--;
13866 /* Setting window manager hints. */
13868 /* Set the normal size hints for the window manager, for frame F.
13869 FLAGS is the flags word to use--or 0 meaning preserve the flags
13870 that the window now has.
13871 If USER_POSITION is nonzero, we set the USPosition
13872 flag (this is useful when FLAGS is 0).
13873 The GTK version is in gtkutils.c */
13875 #ifndef USE_GTK
13876 void
13877 x_wm_set_size_hint (f, flags, user_position)
13878 struct frame *f;
13879 long flags;
13880 int user_position;
13882 XSizeHints size_hints;
13884 #ifdef USE_X_TOOLKIT
13885 Arg al[2];
13886 int ac = 0;
13887 Dimension widget_width, widget_height;
13888 #endif
13890 Window window = FRAME_OUTER_WINDOW (f);
13892 /* Setting PMaxSize caused various problems. */
13893 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13895 size_hints.x = f->output_data.x->left_pos;
13896 size_hints.y = f->output_data.x->top_pos;
13898 #ifdef USE_X_TOOLKIT
13899 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13900 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13901 XtGetValues (f->output_data.x->widget, al, ac);
13902 size_hints.height = widget_height;
13903 size_hints.width = widget_width;
13904 #else /* not USE_X_TOOLKIT */
13905 size_hints.height = PIXEL_HEIGHT (f);
13906 size_hints.width = PIXEL_WIDTH (f);
13907 #endif /* not USE_X_TOOLKIT */
13909 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13910 size_hints.height_inc = f->output_data.x->line_height;
13911 size_hints.max_width
13912 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13913 size_hints.max_height
13914 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13916 /* Calculate the base and minimum sizes.
13918 (When we use the X toolkit, we don't do it here.
13919 Instead we copy the values that the widgets are using, below.) */
13920 #ifndef USE_X_TOOLKIT
13922 int base_width, base_height;
13923 int min_rows = 0, min_cols = 0;
13925 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13926 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13928 check_frame_size (f, &min_rows, &min_cols);
13930 /* The window manager uses the base width hints to calculate the
13931 current number of rows and columns in the frame while
13932 resizing; min_width and min_height aren't useful for this
13933 purpose, since they might not give the dimensions for a
13934 zero-row, zero-column frame.
13936 We use the base_width and base_height members if we have
13937 them; otherwise, we set the min_width and min_height members
13938 to the size for a zero x zero frame. */
13940 #ifdef HAVE_X11R4
13941 size_hints.flags |= PBaseSize;
13942 size_hints.base_width = base_width;
13943 size_hints.base_height = base_height;
13944 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13945 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13946 #else
13947 size_hints.min_width = base_width;
13948 size_hints.min_height = base_height;
13949 #endif
13952 /* If we don't need the old flags, we don't need the old hint at all. */
13953 if (flags)
13955 size_hints.flags |= flags;
13956 goto no_read;
13958 #endif /* not USE_X_TOOLKIT */
13961 XSizeHints hints; /* Sometimes I hate X Windows... */
13962 long supplied_return;
13963 int value;
13965 #ifdef HAVE_X11R4
13966 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13967 &supplied_return);
13968 #else
13969 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13970 #endif
13972 #ifdef USE_X_TOOLKIT
13973 size_hints.base_height = hints.base_height;
13974 size_hints.base_width = hints.base_width;
13975 size_hints.min_height = hints.min_height;
13976 size_hints.min_width = hints.min_width;
13977 #endif
13979 if (flags)
13980 size_hints.flags |= flags;
13981 else
13983 if (value == 0)
13984 hints.flags = 0;
13985 if (hints.flags & PSize)
13986 size_hints.flags |= PSize;
13987 if (hints.flags & PPosition)
13988 size_hints.flags |= PPosition;
13989 if (hints.flags & USPosition)
13990 size_hints.flags |= USPosition;
13991 if (hints.flags & USSize)
13992 size_hints.flags |= USSize;
13996 #ifndef USE_X_TOOLKIT
13997 no_read:
13998 #endif
14000 #ifdef PWinGravity
14001 size_hints.win_gravity = f->output_data.x->win_gravity;
14002 size_hints.flags |= PWinGravity;
14004 if (user_position)
14006 size_hints.flags &= ~ PPosition;
14007 size_hints.flags |= USPosition;
14009 #endif /* PWinGravity */
14011 #ifdef HAVE_X11R4
14012 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
14013 #else
14014 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
14015 #endif
14017 #endif /* not USE_GTK */
14019 /* Used for IconicState or NormalState */
14021 void
14022 x_wm_set_window_state (f, state)
14023 struct frame *f;
14024 int state;
14026 #ifdef USE_X_TOOLKIT
14027 Arg al[1];
14029 XtSetArg (al[0], XtNinitialState, state);
14030 XtSetValues (f->output_data.x->widget, al, 1);
14031 #else /* not USE_X_TOOLKIT */
14032 Window window = FRAME_X_WINDOW (f);
14034 f->output_data.x->wm_hints.flags |= StateHint;
14035 f->output_data.x->wm_hints.initial_state = state;
14037 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
14038 #endif /* not USE_X_TOOLKIT */
14041 void
14042 x_wm_set_icon_pixmap (f, pixmap_id)
14043 struct frame *f;
14044 int pixmap_id;
14046 Pixmap icon_pixmap;
14048 #ifndef USE_X_TOOLKIT
14049 Window window = FRAME_OUTER_WINDOW (f);
14050 #endif
14052 if (pixmap_id > 0)
14054 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
14055 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
14057 else
14059 /* It seems there is no way to turn off use of an icon pixmap.
14060 The following line does it, only if no icon has yet been created,
14061 for some window managers. But with mwm it crashes.
14062 Some people say it should clear the IconPixmapHint bit in this case,
14063 but that doesn't work, and the X consortium said it isn't the
14064 right thing at all. Since there is no way to win,
14065 best to explicitly give up. */
14066 #if 0
14067 f->output_data.x->wm_hints.icon_pixmap = None;
14068 #else
14069 return;
14070 #endif
14073 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
14076 Arg al[1];
14077 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
14078 XtSetValues (f->output_data.x->widget, al, 1);
14081 #else /* not USE_X_TOOLKIT */
14083 f->output_data.x->wm_hints.flags |= IconPixmapHint;
14084 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
14086 #endif /* not USE_X_TOOLKIT */
14089 void
14090 x_wm_set_icon_position (f, icon_x, icon_y)
14091 struct frame *f;
14092 int icon_x, icon_y;
14094 Window window = FRAME_OUTER_WINDOW (f);
14096 f->output_data.x->wm_hints.flags |= IconPositionHint;
14097 f->output_data.x->wm_hints.icon_x = icon_x;
14098 f->output_data.x->wm_hints.icon_y = icon_y;
14100 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
14104 /***********************************************************************
14105 Fonts
14106 ***********************************************************************/
14108 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
14110 struct font_info *
14111 x_get_font_info (f, font_idx)
14112 FRAME_PTR f;
14113 int font_idx;
14115 return (FRAME_X_FONT_TABLE (f) + font_idx);
14119 /* Return a list of names of available fonts matching PATTERN on frame F.
14121 If SIZE is > 0, it is the size (maximum bounds width) of fonts
14122 to be listed.
14124 SIZE < 0 means include scalable fonts.
14126 Frame F null means we have not yet created any frame on X, and
14127 consult the first display in x_display_list. MAXNAMES sets a limit
14128 on how many fonts to match. */
14130 Lisp_Object
14131 x_list_fonts (f, pattern, size, maxnames)
14132 struct frame *f;
14133 Lisp_Object pattern;
14134 int size;
14135 int maxnames;
14137 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
14138 Lisp_Object tem, second_best;
14139 struct x_display_info *dpyinfo
14140 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
14141 Display *dpy = dpyinfo->display;
14142 int try_XLoadQueryFont = 0;
14143 int count;
14144 int allow_scalable_fonts_p = 0;
14146 if (size < 0)
14148 allow_scalable_fonts_p = 1;
14149 size = 0;
14152 patterns = Fassoc (pattern, Valternate_fontname_alist);
14153 if (NILP (patterns))
14154 patterns = Fcons (pattern, Qnil);
14156 if (maxnames == 1 && !size)
14157 /* We can return any single font matching PATTERN. */
14158 try_XLoadQueryFont = 1;
14160 for (; CONSP (patterns); patterns = XCDR (patterns))
14162 int num_fonts;
14163 char **names = NULL;
14165 pattern = XCAR (patterns);
14166 /* See if we cached the result for this particular query.
14167 The cache is an alist of the form:
14168 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
14169 tem = XCDR (dpyinfo->name_list_element);
14170 key = Fcons (Fcons (pattern, make_number (maxnames)),
14171 allow_scalable_fonts_p ? Qt : Qnil);
14172 list = Fassoc (key, tem);
14173 if (!NILP (list))
14175 list = Fcdr_safe (list);
14176 /* We have a cashed list. Don't have to get the list again. */
14177 goto label_cached;
14180 /* At first, put PATTERN in the cache. */
14182 BLOCK_INPUT;
14183 count = x_catch_errors (dpy);
14185 if (try_XLoadQueryFont)
14187 XFontStruct *font;
14188 unsigned long value;
14190 font = XLoadQueryFont (dpy, SDATA (pattern));
14191 if (x_had_errors_p (dpy))
14193 /* This error is perhaps due to insufficient memory on X
14194 server. Let's just ignore it. */
14195 font = NULL;
14196 x_clear_errors (dpy);
14199 if (font
14200 && XGetFontProperty (font, XA_FONT, &value))
14202 char *name = (char *) XGetAtomName (dpy, (Atom) value);
14203 int len = strlen (name);
14204 char *tmp;
14206 /* If DXPC (a Differential X Protocol Compressor)
14207 Ver.3.7 is running, XGetAtomName will return null
14208 string. We must avoid such a name. */
14209 if (len == 0)
14210 try_XLoadQueryFont = 0;
14211 else
14213 num_fonts = 1;
14214 names = (char **) alloca (sizeof (char *));
14215 /* Some systems only allow alloca assigned to a
14216 simple var. */
14217 tmp = (char *) alloca (len + 1); names[0] = tmp;
14218 bcopy (name, names[0], len + 1);
14219 XFree (name);
14222 else
14223 try_XLoadQueryFont = 0;
14225 if (font)
14226 XFreeFont (dpy, font);
14229 if (!try_XLoadQueryFont)
14231 /* We try at least 10 fonts because XListFonts will return
14232 auto-scaled fonts at the head. */
14233 if (maxnames < 0)
14235 int limit;
14237 for (limit = 500;;)
14239 names = XListFonts (dpy, SDATA (pattern), limit, &num_fonts);
14240 if (num_fonts == limit)
14242 BLOCK_INPUT;
14243 XFreeFontNames (names);
14244 UNBLOCK_INPUT;
14245 limit *= 2;
14247 else
14248 break;
14251 else
14252 names = XListFonts (dpy, SDATA (pattern), max (maxnames, 10),
14253 &num_fonts);
14255 if (x_had_errors_p (dpy))
14257 /* This error is perhaps due to insufficient memory on X
14258 server. Let's just ignore it. */
14259 names = NULL;
14260 x_clear_errors (dpy);
14264 x_uncatch_errors (dpy, count);
14265 UNBLOCK_INPUT;
14267 if (names)
14269 int i;
14271 /* Make a list of all the fonts we got back.
14272 Store that in the font cache for the display. */
14273 for (i = 0; i < num_fonts; i++)
14275 int width = 0;
14276 char *p = names[i];
14277 int average_width = -1, dashes = 0;
14279 /* Count the number of dashes in NAMES[I]. If there are
14280 14 dashes, and the field value following 12th dash
14281 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
14282 is usually too ugly to be used for editing. Let's
14283 ignore it. */
14284 while (*p)
14285 if (*p++ == '-')
14287 dashes++;
14288 if (dashes == 7) /* PIXEL_SIZE field */
14289 width = atoi (p);
14290 else if (dashes == 12) /* AVERAGE_WIDTH field */
14291 average_width = atoi (p);
14294 if (allow_scalable_fonts_p
14295 || dashes < 14 || average_width != 0)
14297 tem = build_string (names[i]);
14298 if (NILP (Fassoc (tem, list)))
14300 if (STRINGP (Vx_pixel_size_width_font_regexp)
14301 && ((fast_c_string_match_ignore_case
14302 (Vx_pixel_size_width_font_regexp, names[i]))
14303 >= 0))
14304 /* We can set the value of PIXEL_SIZE to the
14305 width of this font. */
14306 list = Fcons (Fcons (tem, make_number (width)), list);
14307 else
14308 /* For the moment, width is not known. */
14309 list = Fcons (Fcons (tem, Qnil), list);
14314 if (!try_XLoadQueryFont)
14316 BLOCK_INPUT;
14317 XFreeFontNames (names);
14318 UNBLOCK_INPUT;
14322 /* Now store the result in the cache. */
14323 XSETCDR (dpyinfo->name_list_element,
14324 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14326 label_cached:
14327 if (NILP (list)) continue; /* Try the remaining alternatives. */
14329 newlist = second_best = Qnil;
14330 /* Make a list of the fonts that have the right width. */
14331 for (; CONSP (list); list = XCDR (list))
14333 int found_size;
14335 tem = XCAR (list);
14337 if (!CONSP (tem) || NILP (XCAR (tem)))
14338 continue;
14339 if (!size)
14341 newlist = Fcons (XCAR (tem), newlist);
14342 continue;
14345 if (!INTEGERP (XCDR (tem)))
14347 /* Since we have not yet known the size of this font, we
14348 must try slow function call XLoadQueryFont. */
14349 XFontStruct *thisinfo;
14351 BLOCK_INPUT;
14352 count = x_catch_errors (dpy);
14353 thisinfo = XLoadQueryFont (dpy,
14354 SDATA (XCAR (tem)));
14355 if (x_had_errors_p (dpy))
14357 /* This error is perhaps due to insufficient memory on X
14358 server. Let's just ignore it. */
14359 thisinfo = NULL;
14360 x_clear_errors (dpy);
14362 x_uncatch_errors (dpy, count);
14363 UNBLOCK_INPUT;
14365 if (thisinfo)
14367 XSETCDR (tem,
14368 (thisinfo->min_bounds.width == 0
14369 ? make_number (0)
14370 : make_number (thisinfo->max_bounds.width)));
14371 BLOCK_INPUT;
14372 XFreeFont (dpy, thisinfo);
14373 UNBLOCK_INPUT;
14375 else
14376 /* For unknown reason, the previous call of XListFont had
14377 returned a font which can't be opened. Record the size
14378 as 0 not to try to open it again. */
14379 XSETCDR (tem, make_number (0));
14382 found_size = XINT (XCDR (tem));
14383 if (found_size == size)
14384 newlist = Fcons (XCAR (tem), newlist);
14385 else if (found_size > 0)
14387 if (NILP (second_best))
14388 second_best = tem;
14389 else if (found_size < size)
14391 if (XINT (XCDR (second_best)) > size
14392 || XINT (XCDR (second_best)) < found_size)
14393 second_best = tem;
14395 else
14397 if (XINT (XCDR (second_best)) > size
14398 && XINT (XCDR (second_best)) > found_size)
14399 second_best = tem;
14403 if (!NILP (newlist))
14404 break;
14405 else if (!NILP (second_best))
14407 newlist = Fcons (XCAR (second_best), Qnil);
14408 break;
14412 return newlist;
14416 #if GLYPH_DEBUG
14418 /* Check that FONT is valid on frame F. It is if it can be found in F's
14419 font table. */
14421 static void
14422 x_check_font (f, font)
14423 struct frame *f;
14424 XFontStruct *font;
14426 int i;
14427 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14429 xassert (font != NULL);
14431 for (i = 0; i < dpyinfo->n_fonts; i++)
14432 if (dpyinfo->font_table[i].name
14433 && font == dpyinfo->font_table[i].font)
14434 break;
14436 xassert (i < dpyinfo->n_fonts);
14439 #endif /* GLYPH_DEBUG != 0 */
14441 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14442 Note: There are (broken) X fonts out there with invalid XFontStruct
14443 min_bounds contents. For example, handa@etl.go.jp reports that
14444 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14445 have font->min_bounds.width == 0. */
14447 static INLINE void
14448 x_font_min_bounds (font, w, h)
14449 XFontStruct *font;
14450 int *w, *h;
14452 *h = FONT_HEIGHT (font);
14453 *w = font->min_bounds.width;
14455 /* Try to handle the case where FONT->min_bounds has invalid
14456 contents. Since the only font known to have invalid min_bounds
14457 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14458 if (*w <= 0)
14459 *w = font->max_bounds.width;
14463 /* Compute the smallest character width and smallest font height over
14464 all fonts available on frame F. Set the members smallest_char_width
14465 and smallest_font_height in F's x_display_info structure to
14466 the values computed. Value is non-zero if smallest_font_height or
14467 smallest_char_width become smaller than they were before. */
14469 static int
14470 x_compute_min_glyph_bounds (f)
14471 struct frame *f;
14473 int i;
14474 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14475 XFontStruct *font;
14476 int old_width = dpyinfo->smallest_char_width;
14477 int old_height = dpyinfo->smallest_font_height;
14479 dpyinfo->smallest_font_height = 100000;
14480 dpyinfo->smallest_char_width = 100000;
14482 for (i = 0; i < dpyinfo->n_fonts; ++i)
14483 if (dpyinfo->font_table[i].name)
14485 struct font_info *fontp = dpyinfo->font_table + i;
14486 int w, h;
14488 font = (XFontStruct *) fontp->font;
14489 xassert (font != (XFontStruct *) ~0);
14490 x_font_min_bounds (font, &w, &h);
14492 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14493 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14496 xassert (dpyinfo->smallest_char_width > 0
14497 && dpyinfo->smallest_font_height > 0);
14499 return (dpyinfo->n_fonts == 1
14500 || dpyinfo->smallest_char_width < old_width
14501 || dpyinfo->smallest_font_height < old_height);
14505 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14506 pointer to the structure font_info while allocating it dynamically.
14507 If SIZE is 0, load any size of font.
14508 If loading is failed, return NULL. */
14510 struct font_info *
14511 x_load_font (f, fontname, size)
14512 struct frame *f;
14513 register char *fontname;
14514 int size;
14516 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14517 Lisp_Object font_names;
14518 int count;
14520 /* Get a list of all the fonts that match this name. Once we
14521 have a list of matching fonts, we compare them against the fonts
14522 we already have by comparing names. */
14523 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14525 if (!NILP (font_names))
14527 Lisp_Object tail;
14528 int i;
14530 for (i = 0; i < dpyinfo->n_fonts; i++)
14531 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14532 if (dpyinfo->font_table[i].name
14533 && (!strcmp (dpyinfo->font_table[i].name,
14534 SDATA (XCAR (tail)))
14535 || !strcmp (dpyinfo->font_table[i].full_name,
14536 SDATA (XCAR (tail)))))
14537 return (dpyinfo->font_table + i);
14540 /* Load the font and add it to the table. */
14542 char *full_name;
14543 XFontStruct *font;
14544 struct font_info *fontp;
14545 unsigned long value;
14546 int i;
14548 /* If we have found fonts by x_list_font, load one of them. If
14549 not, we still try to load a font by the name given as FONTNAME
14550 because XListFonts (called in x_list_font) of some X server has
14551 a bug of not finding a font even if the font surely exists and
14552 is loadable by XLoadQueryFont. */
14553 if (size > 0 && !NILP (font_names))
14554 fontname = (char *) SDATA (XCAR (font_names));
14556 BLOCK_INPUT;
14557 count = x_catch_errors (FRAME_X_DISPLAY (f));
14558 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14559 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14561 /* This error is perhaps due to insufficient memory on X
14562 server. Let's just ignore it. */
14563 font = NULL;
14564 x_clear_errors (FRAME_X_DISPLAY (f));
14566 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14567 UNBLOCK_INPUT;
14568 if (!font)
14569 return NULL;
14571 /* Find a free slot in the font table. */
14572 for (i = 0; i < dpyinfo->n_fonts; ++i)
14573 if (dpyinfo->font_table[i].name == NULL)
14574 break;
14576 /* If no free slot found, maybe enlarge the font table. */
14577 if (i == dpyinfo->n_fonts
14578 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14580 int sz;
14581 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14582 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14583 dpyinfo->font_table
14584 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14587 fontp = dpyinfo->font_table + i;
14588 if (i == dpyinfo->n_fonts)
14589 ++dpyinfo->n_fonts;
14591 /* Now fill in the slots of *FONTP. */
14592 BLOCK_INPUT;
14593 fontp->font = font;
14594 fontp->font_idx = i;
14595 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14596 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14598 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14599 full_name = 0;
14600 if (XGetFontProperty (font, XA_FONT, &value))
14602 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14603 char *p = name;
14604 int dashes = 0;
14606 /* Count the number of dashes in the "full name".
14607 If it is too few, this isn't really the font's full name,
14608 so don't use it.
14609 In X11R4, the fonts did not come with their canonical names
14610 stored in them. */
14611 while (*p)
14613 if (*p == '-')
14614 dashes++;
14615 p++;
14618 if (dashes >= 13)
14620 full_name = (char *) xmalloc (p - name + 1);
14621 bcopy (name, full_name, p - name + 1);
14624 XFree (name);
14627 if (full_name != 0)
14628 fontp->full_name = full_name;
14629 else
14630 fontp->full_name = fontp->name;
14632 fontp->size = font->max_bounds.width;
14633 fontp->height = FONT_HEIGHT (font);
14635 if (NILP (font_names))
14637 /* We come here because of a bug of XListFonts mentioned at
14638 the head of this block. Let's store this information in
14639 the cache for x_list_fonts. */
14640 Lisp_Object lispy_name = build_string (fontname);
14641 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14642 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14643 Qnil);
14645 XSETCDR (dpyinfo->name_list_element,
14646 Fcons (Fcons (key,
14647 Fcons (Fcons (lispy_full_name,
14648 make_number (fontp->size)),
14649 Qnil)),
14650 XCDR (dpyinfo->name_list_element)));
14651 if (full_name)
14653 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14654 Qnil);
14655 XSETCDR (dpyinfo->name_list_element,
14656 Fcons (Fcons (key,
14657 Fcons (Fcons (lispy_full_name,
14658 make_number (fontp->size)),
14659 Qnil)),
14660 XCDR (dpyinfo->name_list_element)));
14664 /* The slot `encoding' specifies how to map a character
14665 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14666 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14667 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14668 2:0xA020..0xFF7F). For the moment, we don't know which charset
14669 uses this font. So, we set information in fontp->encoding[1]
14670 which is never used by any charset. If mapping can't be
14671 decided, set FONT_ENCODING_NOT_DECIDED. */
14672 fontp->encoding[1]
14673 = (font->max_byte1 == 0
14674 /* 1-byte font */
14675 ? (font->min_char_or_byte2 < 0x80
14676 ? (font->max_char_or_byte2 < 0x80
14677 ? 0 /* 0x20..0x7F */
14678 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14679 : 1) /* 0xA0..0xFF */
14680 /* 2-byte font */
14681 : (font->min_byte1 < 0x80
14682 ? (font->max_byte1 < 0x80
14683 ? (font->min_char_or_byte2 < 0x80
14684 ? (font->max_char_or_byte2 < 0x80
14685 ? 0 /* 0x2020..0x7F7F */
14686 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14687 : 3) /* 0x20A0..0x7FFF */
14688 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14689 : (font->min_char_or_byte2 < 0x80
14690 ? (font->max_char_or_byte2 < 0x80
14691 ? 2 /* 0xA020..0xFF7F */
14692 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14693 : 1))); /* 0xA0A0..0xFFFF */
14695 fontp->baseline_offset
14696 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14697 ? (long) value : 0);
14698 fontp->relative_compose
14699 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14700 ? (long) value : 0);
14701 fontp->default_ascent
14702 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14703 ? (long) value : 0);
14705 /* Set global flag fonts_changed_p to non-zero if the font loaded
14706 has a character with a smaller width than any other character
14707 before, or if the font loaded has a smaller height than any
14708 other font loaded before. If this happens, it will make a
14709 glyph matrix reallocation necessary. */
14710 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14711 UNBLOCK_INPUT;
14712 return fontp;
14717 /* Return a pointer to struct font_info of a font named FONTNAME for
14718 frame F. If no such font is loaded, return NULL. */
14720 struct font_info *
14721 x_query_font (f, fontname)
14722 struct frame *f;
14723 register char *fontname;
14725 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14726 int i;
14728 for (i = 0; i < dpyinfo->n_fonts; i++)
14729 if (dpyinfo->font_table[i].name
14730 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14731 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14732 return (dpyinfo->font_table + i);
14733 return NULL;
14737 /* Find a CCL program for a font specified by FONTP, and set the member
14738 `encoder' of the structure. */
14740 void
14741 x_find_ccl_program (fontp)
14742 struct font_info *fontp;
14744 Lisp_Object list, elt;
14746 elt = Qnil;
14747 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14749 elt = XCAR (list);
14750 if (CONSP (elt)
14751 && STRINGP (XCAR (elt))
14752 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14753 >= 0)
14754 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14755 >= 0)))
14756 break;
14759 if (! NILP (list))
14761 struct ccl_program *ccl
14762 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14764 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14765 xfree (ccl);
14766 else
14767 fontp->font_encoder = ccl;
14773 /***********************************************************************
14774 Initialization
14775 ***********************************************************************/
14777 #ifdef USE_X_TOOLKIT
14778 static XrmOptionDescRec emacs_options[] = {
14779 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14780 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14782 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14783 XrmoptionSepArg, NULL},
14784 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14786 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14787 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14788 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14789 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14790 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14791 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14792 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14794 #endif /* USE_X_TOOLKIT */
14796 static int x_initialized;
14798 #ifdef MULTI_KBOARD
14799 /* Test whether two display-name strings agree up to the dot that separates
14800 the screen number from the server number. */
14801 static int
14802 same_x_server (name1, name2)
14803 const char *name1, *name2;
14805 int seen_colon = 0;
14806 const unsigned char *system_name = SDATA (Vsystem_name);
14807 int system_name_length = strlen (system_name);
14808 int length_until_period = 0;
14810 while (system_name[length_until_period] != 0
14811 && system_name[length_until_period] != '.')
14812 length_until_period++;
14814 /* Treat `unix' like an empty host name. */
14815 if (! strncmp (name1, "unix:", 5))
14816 name1 += 4;
14817 if (! strncmp (name2, "unix:", 5))
14818 name2 += 4;
14819 /* Treat this host's name like an empty host name. */
14820 if (! strncmp (name1, system_name, system_name_length)
14821 && name1[system_name_length] == ':')
14822 name1 += system_name_length;
14823 if (! strncmp (name2, system_name, system_name_length)
14824 && name2[system_name_length] == ':')
14825 name2 += system_name_length;
14826 /* Treat this host's domainless name like an empty host name. */
14827 if (! strncmp (name1, system_name, length_until_period)
14828 && name1[length_until_period] == ':')
14829 name1 += length_until_period;
14830 if (! strncmp (name2, system_name, length_until_period)
14831 && name2[length_until_period] == ':')
14832 name2 += length_until_period;
14834 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14836 if (*name1 == ':')
14837 seen_colon++;
14838 if (seen_colon && *name1 == '.')
14839 return 1;
14841 return (seen_colon
14842 && (*name1 == '.' || *name1 == '\0')
14843 && (*name2 == '.' || *name2 == '\0'));
14845 #endif
14847 struct x_display_info *
14848 x_term_init (display_name, xrm_option, resource_name)
14849 Lisp_Object display_name;
14850 char *xrm_option;
14851 char *resource_name;
14853 int connection;
14854 Display *dpy;
14855 struct x_display_info *dpyinfo;
14856 XrmDatabase xrdb;
14858 BLOCK_INPUT;
14860 if (!x_initialized)
14862 x_initialize ();
14863 x_initialized = 1;
14866 #ifdef USE_GTK
14868 #define NUM_ARGV 10
14869 int argc;
14870 char *argv[NUM_ARGV];
14871 char **argv2 = argv;
14872 GdkAtom atom;
14874 /* GTK 2.0 can only handle one display, GTK 2.2 can handle more
14875 than one, but this remains to be implemented. */
14876 if (x_initialized > 1)
14877 return 0;
14879 x_initialized++;
14881 for (argc = 0; argc < NUM_ARGV; ++argc)
14882 argv[argc] = 0;
14884 argc = 0;
14885 argv[argc++] = initial_argv[0];
14887 if (! NILP (display_name))
14889 argv[argc++] = "--display";
14890 argv[argc++] = SDATA (display_name);
14893 argv[argc++] = "--name";
14894 argv[argc++] = resource_name;
14896 #ifdef HAVE_X11R5
14897 XSetLocaleModifiers ("");
14898 #endif
14900 gtk_init (&argc, &argv2);
14902 /* gtk_init does set_locale. We must fix locale after calling it. */
14903 fixup_locale ();
14904 xg_initialize ();
14906 dpy = GDK_DISPLAY ();
14908 /* NULL window -> events for all windows go to our function */
14909 gdk_window_add_filter (NULL, event_handler_gdk, NULL);
14911 /* Load our own gtkrc if it exists. */
14913 struct gcpro gcpro1, gcpro2;
14914 char *file = "~/.emacs.d/gtkrc";
14915 Lisp_Object s, abs_file;
14917 GCPRO2 (s, abs_file);
14918 s = make_string (file, strlen (file));
14919 abs_file = Fexpand_file_name(s, Qnil);
14921 if (! NILP (abs_file) && Ffile_readable_p (abs_file))
14922 gtk_rc_parse (SDATA (abs_file));
14924 UNGCPRO;
14927 XSetErrorHandler (x_error_handler);
14928 XSetIOErrorHandler (x_io_error_quitter);
14930 #else /* not USE_GTK */
14931 #ifdef USE_X_TOOLKIT
14932 /* weiner@footloose.sps.mot.com reports that this causes
14933 errors with X11R5:
14934 X protocol error: BadAtom (invalid Atom parameter)
14935 on protocol request 18skiloaf.
14936 So let's not use it until R6. */
14937 #ifdef HAVE_X11XTR6
14938 XtSetLanguageProc (NULL, NULL, NULL);
14939 #endif
14942 int argc = 0;
14943 char *argv[3];
14945 argv[0] = "";
14946 argc = 1;
14947 if (xrm_option)
14949 argv[argc++] = "-xrm";
14950 argv[argc++] = xrm_option;
14952 turn_on_atimers (0);
14953 dpy = XtOpenDisplay (Xt_app_con, SDATA (display_name),
14954 resource_name, EMACS_CLASS,
14955 emacs_options, XtNumber (emacs_options),
14956 &argc, argv);
14957 turn_on_atimers (1);
14959 #ifdef HAVE_X11XTR6
14960 /* I think this is to compensate for XtSetLanguageProc. */
14961 fixup_locale ();
14962 #endif
14965 #else /* not USE_X_TOOLKIT */
14966 #ifdef HAVE_X11R5
14967 XSetLocaleModifiers ("");
14968 #endif
14969 dpy = XOpenDisplay (SDATA (display_name));
14970 #endif /* not USE_X_TOOLKIT */
14971 #endif /* not USE_GTK*/
14973 /* Detect failure. */
14974 if (dpy == 0)
14976 UNBLOCK_INPUT;
14977 return 0;
14980 /* We have definitely succeeded. Record the new connection. */
14982 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14983 bzero (dpyinfo, sizeof *dpyinfo);
14985 #ifdef MULTI_KBOARD
14987 struct x_display_info *share;
14988 Lisp_Object tail;
14990 for (share = x_display_list, tail = x_display_name_list; share;
14991 share = share->next, tail = XCDR (tail))
14992 if (same_x_server (SDATA (XCAR (XCAR (tail))),
14993 SDATA (display_name)))
14994 break;
14995 if (share)
14996 dpyinfo->kboard = share->kboard;
14997 else
14999 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
15000 init_kboard (dpyinfo->kboard);
15001 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
15003 char *vendor = ServerVendor (dpy);
15004 UNBLOCK_INPUT;
15005 dpyinfo->kboard->Vsystem_key_alist
15006 = call1 (Qvendor_specific_keysyms,
15007 build_string (vendor ? vendor : ""));
15008 BLOCK_INPUT;
15011 dpyinfo->kboard->next_kboard = all_kboards;
15012 all_kboards = dpyinfo->kboard;
15013 /* Don't let the initial kboard remain current longer than necessary.
15014 That would cause problems if a file loaded on startup tries to
15015 prompt in the mini-buffer. */
15016 if (current_kboard == initial_kboard)
15017 current_kboard = dpyinfo->kboard;
15019 dpyinfo->kboard->reference_count++;
15021 #endif
15023 /* Put this display on the chain. */
15024 dpyinfo->next = x_display_list;
15025 x_display_list = dpyinfo;
15027 /* Put it on x_display_name_list as well, to keep them parallel. */
15028 x_display_name_list = Fcons (Fcons (display_name, Qnil),
15029 x_display_name_list);
15030 dpyinfo->name_list_element = XCAR (x_display_name_list);
15032 dpyinfo->display = dpy;
15034 #if 0
15035 XSetAfterFunction (x_current_display, x_trace_wire);
15036 #endif /* ! 0 */
15038 dpyinfo->x_id_name
15039 = (char *) xmalloc (SBYTES (Vinvocation_name)
15040 + SBYTES (Vsystem_name)
15041 + 2);
15042 sprintf (dpyinfo->x_id_name, "%s@%s",
15043 SDATA (Vinvocation_name), SDATA (Vsystem_name));
15045 /* Figure out which modifier bits mean what. */
15046 x_find_modifier_meanings (dpyinfo);
15048 /* Get the scroll bar cursor. */
15049 dpyinfo->vertical_scroll_bar_cursor
15050 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
15052 xrdb = x_load_resources (dpyinfo->display, xrm_option,
15053 resource_name, EMACS_CLASS);
15054 #ifdef HAVE_XRMSETDATABASE
15055 XrmSetDatabase (dpyinfo->display, xrdb);
15056 #else
15057 dpyinfo->display->db = xrdb;
15058 #endif
15059 /* Put the rdb where we can find it in a way that works on
15060 all versions. */
15061 dpyinfo->xrdb = xrdb;
15063 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
15064 DefaultScreen (dpyinfo->display));
15065 select_visual (dpyinfo);
15066 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
15067 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
15068 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
15069 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
15070 dpyinfo->grabbed = 0;
15071 dpyinfo->reference_count = 0;
15072 dpyinfo->icon_bitmap_id = -1;
15073 dpyinfo->font_table = NULL;
15074 dpyinfo->n_fonts = 0;
15075 dpyinfo->font_table_size = 0;
15076 dpyinfo->bitmaps = 0;
15077 dpyinfo->bitmaps_size = 0;
15078 dpyinfo->bitmaps_last = 0;
15079 dpyinfo->scratch_cursor_gc = 0;
15080 dpyinfo->mouse_face_mouse_frame = 0;
15081 dpyinfo->mouse_face_deferred_gc = 0;
15082 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
15083 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
15084 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
15085 dpyinfo->mouse_face_window = Qnil;
15086 dpyinfo->mouse_face_overlay = Qnil;
15087 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
15088 dpyinfo->mouse_face_defer = 0;
15089 dpyinfo->mouse_face_hidden = 0;
15090 dpyinfo->x_focus_frame = 0;
15091 dpyinfo->x_focus_event_frame = 0;
15092 dpyinfo->x_highlight_frame = 0;
15093 dpyinfo->image_cache = make_image_cache ();
15095 /* See if a private colormap is requested. */
15096 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
15098 if (dpyinfo->visual->class == PseudoColor)
15100 Lisp_Object value;
15101 value = display_x_get_resource (dpyinfo,
15102 build_string ("privateColormap"),
15103 build_string ("PrivateColormap"),
15104 Qnil, Qnil);
15105 if (STRINGP (value)
15106 && (!strcmp (SDATA (value), "true")
15107 || !strcmp (SDATA (value), "on")))
15108 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
15111 else
15112 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
15113 dpyinfo->visual, AllocNone);
15116 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
15117 double pixels = DisplayHeight (dpyinfo->display, screen_number);
15118 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
15119 dpyinfo->resy = pixels * 25.4 / mm;
15120 pixels = DisplayWidth (dpyinfo->display, screen_number);
15121 mm = DisplayWidthMM (dpyinfo->display, screen_number);
15122 dpyinfo->resx = pixels * 25.4 / mm;
15125 dpyinfo->Xatom_wm_protocols
15126 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
15127 dpyinfo->Xatom_wm_take_focus
15128 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
15129 dpyinfo->Xatom_wm_save_yourself
15130 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
15131 dpyinfo->Xatom_wm_delete_window
15132 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
15133 dpyinfo->Xatom_wm_change_state
15134 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
15135 dpyinfo->Xatom_wm_configure_denied
15136 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
15137 dpyinfo->Xatom_wm_window_moved
15138 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
15139 dpyinfo->Xatom_editres
15140 = XInternAtom (dpyinfo->display, "Editres", False);
15141 dpyinfo->Xatom_CLIPBOARD
15142 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
15143 dpyinfo->Xatom_TIMESTAMP
15144 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
15145 dpyinfo->Xatom_TEXT
15146 = XInternAtom (dpyinfo->display, "TEXT", False);
15147 dpyinfo->Xatom_COMPOUND_TEXT
15148 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
15149 dpyinfo->Xatom_UTF8_STRING
15150 = XInternAtom (dpyinfo->display, "UTF8_STRING", False);
15151 dpyinfo->Xatom_DELETE
15152 = XInternAtom (dpyinfo->display, "DELETE", False);
15153 dpyinfo->Xatom_MULTIPLE
15154 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
15155 dpyinfo->Xatom_INCR
15156 = XInternAtom (dpyinfo->display, "INCR", False);
15157 dpyinfo->Xatom_EMACS_TMP
15158 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
15159 dpyinfo->Xatom_TARGETS
15160 = XInternAtom (dpyinfo->display, "TARGETS", False);
15161 dpyinfo->Xatom_NULL
15162 = XInternAtom (dpyinfo->display, "NULL", False);
15163 dpyinfo->Xatom_ATOM_PAIR
15164 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
15165 /* For properties of font. */
15166 dpyinfo->Xatom_PIXEL_SIZE
15167 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
15168 dpyinfo->Xatom_MULE_BASELINE_OFFSET
15169 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
15170 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
15171 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
15172 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
15173 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
15175 /* Ghostscript support. */
15176 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
15177 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
15179 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
15180 False);
15182 dpyinfo->cut_buffers_initialized = 0;
15184 connection = ConnectionNumber (dpyinfo->display);
15185 dpyinfo->connection = connection;
15188 char null_bits[1];
15190 null_bits[0] = 0x00;
15192 dpyinfo->null_pixel
15193 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15194 null_bits, 1, 1, (long) 0, (long) 0,
15199 extern int gray_bitmap_width, gray_bitmap_height;
15200 extern char *gray_bitmap_bits;
15201 dpyinfo->gray
15202 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
15203 gray_bitmap_bits,
15204 gray_bitmap_width, gray_bitmap_height,
15205 (unsigned long) 1, (unsigned long) 0, 1);
15208 #ifdef HAVE_X_I18N
15209 xim_initialize (dpyinfo, resource_name);
15210 #endif
15212 #ifdef subprocesses
15213 /* This is only needed for distinguishing keyboard and process input. */
15214 if (connection != 0)
15215 add_keyboard_wait_descriptor (connection);
15216 #endif
15218 #ifndef F_SETOWN_BUG
15219 #ifdef F_SETOWN
15220 #ifdef F_SETOWN_SOCK_NEG
15221 /* stdin is a socket here */
15222 fcntl (connection, F_SETOWN, -getpid ());
15223 #else /* ! defined (F_SETOWN_SOCK_NEG) */
15224 fcntl (connection, F_SETOWN, getpid ());
15225 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
15226 #endif /* ! defined (F_SETOWN) */
15227 #endif /* F_SETOWN_BUG */
15229 #ifdef SIGIO
15230 if (interrupt_input)
15231 init_sigio (connection);
15232 #endif /* ! defined (SIGIO) */
15234 #ifdef USE_LUCID
15235 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
15236 /* Make sure that we have a valid font for dialog boxes
15237 so that Xt does not crash. */
15239 Display *dpy = dpyinfo->display;
15240 XrmValue d, fr, to;
15241 Font font;
15242 int count;
15244 d.addr = (XPointer)&dpy;
15245 d.size = sizeof (Display *);
15246 fr.addr = XtDefaultFont;
15247 fr.size = sizeof (XtDefaultFont);
15248 to.size = sizeof (Font *);
15249 to.addr = (XPointer)&font;
15250 count = x_catch_errors (dpy);
15251 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
15252 abort ();
15253 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
15254 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
15255 x_uncatch_errors (dpy, count);
15257 #endif
15258 #endif
15260 /* See if we should run in synchronous mode. This is useful
15261 for debugging X code. */
15263 Lisp_Object value;
15264 value = display_x_get_resource (dpyinfo,
15265 build_string ("synchronous"),
15266 build_string ("Synchronous"),
15267 Qnil, Qnil);
15268 if (STRINGP (value)
15269 && (!strcmp (SDATA (value), "true")
15270 || !strcmp (SDATA (value), "on")))
15271 XSynchronize (dpyinfo->display, True);
15274 UNBLOCK_INPUT;
15276 return dpyinfo;
15279 /* Get rid of display DPYINFO, assuming all frames are already gone,
15280 and without sending any more commands to the X server. */
15282 void
15283 x_delete_display (dpyinfo)
15284 struct x_display_info *dpyinfo;
15286 delete_keyboard_wait_descriptor (dpyinfo->connection);
15288 /* Discard this display from x_display_name_list and x_display_list.
15289 We can't use Fdelq because that can quit. */
15290 if (! NILP (x_display_name_list)
15291 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15292 x_display_name_list = XCDR (x_display_name_list);
15293 else
15295 Lisp_Object tail;
15297 tail = x_display_name_list;
15298 while (CONSP (tail) && CONSP (XCDR (tail)))
15300 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
15302 XSETCDR (tail, XCDR (XCDR (tail)));
15303 break;
15305 tail = XCDR (tail);
15309 if (next_noop_dpyinfo == dpyinfo)
15310 next_noop_dpyinfo = dpyinfo->next;
15312 if (x_display_list == dpyinfo)
15313 x_display_list = dpyinfo->next;
15314 else
15316 struct x_display_info *tail;
15318 for (tail = x_display_list; tail; tail = tail->next)
15319 if (tail->next == dpyinfo)
15320 tail->next = tail->next->next;
15323 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15324 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
15325 XrmDestroyDatabase (dpyinfo->xrdb);
15326 #endif
15327 #endif
15328 #ifdef MULTI_KBOARD
15329 if (--dpyinfo->kboard->reference_count == 0)
15330 delete_kboard (dpyinfo->kboard);
15331 #endif
15332 #ifdef HAVE_X_I18N
15333 if (dpyinfo->xim)
15334 xim_close_dpy (dpyinfo);
15335 #endif
15337 xfree (dpyinfo->font_table);
15338 xfree (dpyinfo->x_id_name);
15339 xfree (dpyinfo->color_cells);
15340 xfree (dpyinfo);
15344 /* Set up use of X before we make the first connection. */
15346 static struct redisplay_interface x_redisplay_interface =
15348 x_produce_glyphs,
15349 x_write_glyphs,
15350 x_insert_glyphs,
15351 x_clear_end_of_line,
15352 x_scroll_run,
15353 x_after_update_window_line,
15354 x_update_window_begin,
15355 x_update_window_end,
15356 XTcursor_to,
15357 x_flush,
15358 x_clear_mouse_face,
15359 x_get_glyph_overhangs,
15360 x_fix_overlapping_area,
15361 x_draw_fringe_bitmap
15364 void
15365 x_initialize ()
15367 rif = &x_redisplay_interface;
15369 clear_frame_hook = x_clear_frame;
15370 ins_del_lines_hook = x_ins_del_lines;
15371 delete_glyphs_hook = x_delete_glyphs;
15372 ring_bell_hook = XTring_bell;
15373 reset_terminal_modes_hook = XTreset_terminal_modes;
15374 set_terminal_modes_hook = XTset_terminal_modes;
15375 update_begin_hook = x_update_begin;
15376 update_end_hook = x_update_end;
15377 set_terminal_window_hook = XTset_terminal_window;
15378 read_socket_hook = XTread_socket;
15379 frame_up_to_date_hook = XTframe_up_to_date;
15380 mouse_position_hook = XTmouse_position;
15381 frame_rehighlight_hook = XTframe_rehighlight;
15382 frame_raise_lower_hook = XTframe_raise_lower;
15383 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15384 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15385 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15386 judge_scroll_bars_hook = XTjudge_scroll_bars;
15387 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15389 scroll_region_ok = 1; /* we'll scroll partial frames */
15390 char_ins_del_ok = 1;
15391 line_ins_del_ok = 1; /* we'll just blt 'em */
15392 fast_clear_end_of_line = 1; /* X does this well */
15393 memory_below_frame = 0; /* we don't remember what scrolls
15394 off the bottom */
15395 baud_rate = 19200;
15397 x_noop_count = 0;
15398 last_tool_bar_item = -1;
15399 any_help_event_p = 0;
15401 /* Try to use interrupt input; if we can't, then start polling. */
15402 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15404 #ifdef USE_X_TOOLKIT
15405 XtToolkitInitialize ();
15407 Xt_app_con = XtCreateApplicationContext ();
15409 /* Register a converter from strings to pixels, which uses
15410 Emacs' color allocation infrastructure. */
15411 XtAppSetTypeConverter (Xt_app_con,
15412 XtRString, XtRPixel, cvt_string_to_pixel,
15413 cvt_string_to_pixel_args,
15414 XtNumber (cvt_string_to_pixel_args),
15415 XtCacheByDisplay, cvt_pixel_dtor);
15417 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15419 /* Install an asynchronous timer that processes Xt timeout events
15420 every 0.1s. This is necessary because some widget sets use
15421 timeouts internally, for example the LessTif menu bar, or the
15422 Xaw3d scroll bar. When Xt timouts aren't processed, these
15423 widgets don't behave normally. */
15425 EMACS_TIME interval;
15426 EMACS_SET_SECS_USECS (interval, 0, 100000);
15427 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15429 #endif
15431 #ifdef USE_TOOLKIT_SCROLL_BARS
15432 #ifndef USE_GTK
15433 xaw3d_arrow_scroll = False;
15434 xaw3d_pick_top = True;
15435 #endif
15436 #endif
15438 /* Note that there is no real way portable across R3/R4 to get the
15439 original error handler. */
15440 XSetErrorHandler (x_error_handler);
15441 XSetIOErrorHandler (x_io_error_quitter);
15443 /* Disable Window Change signals; they are handled by X events. */
15444 #ifdef SIGWINCH
15445 signal (SIGWINCH, SIG_DFL);
15446 #endif /* SIGWINCH */
15448 signal (SIGPIPE, x_connection_signal);
15450 #ifdef HAVE_X_SM
15451 x_session_initialize ();
15452 #endif
15456 void
15457 syms_of_xterm ()
15459 staticpro (&x_error_message_string);
15460 x_error_message_string = Qnil;
15462 staticpro (&x_display_name_list);
15463 x_display_name_list = Qnil;
15465 staticpro (&last_mouse_scroll_bar);
15466 last_mouse_scroll_bar = Qnil;
15468 staticpro (&Qvendor_specific_keysyms);
15469 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15471 staticpro (&Qutf_8);
15472 Qutf_8 = intern ("utf-8");
15473 staticpro (&Qlatin_1);
15474 Qlatin_1 = intern ("latin-1");
15476 staticpro (&last_mouse_press_frame);
15477 last_mouse_press_frame = Qnil;
15479 help_echo = Qnil;
15480 staticpro (&help_echo);
15481 help_echo_object = Qnil;
15482 staticpro (&help_echo_object);
15483 help_echo_window = Qnil;
15484 staticpro (&help_echo_window);
15485 previous_help_echo = Qnil;
15486 staticpro (&previous_help_echo);
15487 help_echo_pos = -1;
15489 DEFVAR_BOOL ("mouse-autoselect-window", &mouse_autoselect_window,
15490 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15491 mouse_autoselect_window = 0;
15493 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15494 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15495 For example, if a block cursor is over a tab, it will be drawn as
15496 wide as that tab on the display. */);
15497 x_stretch_cursor_p = 0;
15499 DEFVAR_BOOL ("x-use-underline-position-properties",
15500 &x_use_underline_position_properties,
15501 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15502 nil means ignore them. If you encounter fonts with bogus
15503 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15504 to 4.1, set this to nil. */);
15505 x_use_underline_position_properties = 1;
15507 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15508 doc: /* What X toolkit scroll bars Emacs uses.
15509 A value of nil means Emacs doesn't use X toolkit scroll bars.
15510 Otherwise, value is a symbol describing the X toolkit. */);
15511 #ifdef USE_TOOLKIT_SCROLL_BARS
15512 #ifdef USE_MOTIF
15513 Vx_toolkit_scroll_bars = intern ("motif");
15514 #elif defined HAVE_XAW3D
15515 Vx_toolkit_scroll_bars = intern ("xaw3d");
15516 #elif USE_GTK
15517 Vx_toolkit_scroll_bars = intern ("gtk");
15518 #else
15519 Vx_toolkit_scroll_bars = intern ("xaw");
15520 #endif
15521 #else
15522 Vx_toolkit_scroll_bars = Qnil;
15523 #endif
15525 staticpro (&last_mouse_motion_frame);
15526 last_mouse_motion_frame = Qnil;
15528 Qmodifier_value = intern ("modifier-value");
15529 Qalt = intern ("alt");
15530 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15531 Qhyper = intern ("hyper");
15532 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15533 Qmeta = intern ("meta");
15534 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15535 Qsuper = intern ("super");
15536 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15538 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15539 doc: /* Which keys Emacs uses for the alt modifier.
15540 This should be one of the symbols `alt', `hyper', `meta', `super'.
15541 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15542 is nil, which is the same as `alt'. */);
15543 Vx_alt_keysym = Qnil;
15545 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15546 doc: /* Which keys Emacs uses for the hyper modifier.
15547 This should be one of the symbols `alt', `hyper', `meta', `super'.
15548 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15549 default is nil, which is the same as `hyper'. */);
15550 Vx_hyper_keysym = Qnil;
15552 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15553 doc: /* Which keys Emacs uses for the meta modifier.
15554 This should be one of the symbols `alt', `hyper', `meta', `super'.
15555 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15556 default is nil, which is the same as `meta'. */);
15557 Vx_meta_keysym = Qnil;
15559 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15560 doc: /* Which keys Emacs uses for the super modifier.
15561 This should be one of the symbols `alt', `hyper', `meta', `super'.
15562 For example, `super' means use the Super_L and Super_R keysyms. The
15563 default is nil, which is the same as `super'. */);
15564 Vx_super_keysym = Qnil;
15566 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
15567 doc: /* Hash table of character codes indexed by X keysym codes. */);
15568 Vx_keysym_table = make_hash_table (Qeql, make_number (900),
15569 make_float (DEFAULT_REHASH_SIZE),
15570 make_float (DEFAULT_REHASH_THRESHOLD),
15571 Qnil, Qnil, Qnil);
15574 #endif /* HAVE_X_WINDOWS */