* lisp/simple.el (mark-word): Mark more if repeated.
[emacs.git] / src / xterm.c
blob6f53a8ed848cb69d22641d8c658c37dfa9370b5f
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
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_LUCID
101 extern int xlwmenu_window_p (Widget w, Window window);
102 extern void xlwmenu_redisplay P_ ((Widget));
103 #endif
105 #ifdef USE_X_TOOLKIT
107 extern void free_frame_menubar P_ ((struct frame *));
108 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
109 int));
111 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
112 #define HACK_EDITRES
113 extern void _XEditResCheckMessages ();
114 #endif /* not NO_EDITRES */
116 /* Include toolkit specific headers for the scroll bar widget. */
118 #ifdef USE_TOOLKIT_SCROLL_BARS
119 #if defined USE_MOTIF
120 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
121 #include <Xm/ScrollBar.h>
122 #else /* !USE_MOTIF i.e. use Xaw */
124 #ifdef HAVE_XAW3D
125 #include <X11/Xaw3d/Simple.h>
126 #include <X11/Xaw3d/Scrollbar.h>
127 #define ARROW_SCROLLBAR
128 #include <X11/Xaw3d/ScrollbarP.h>
129 #else /* !HAVE_XAW3D */
130 #include <X11/Xaw/Simple.h>
131 #include <X11/Xaw/Scrollbar.h>
132 #endif /* !HAVE_XAW3D */
133 #ifndef XtNpickTop
134 #define XtNpickTop "pickTop"
135 #endif /* !XtNpickTop */
136 #endif /* !USE_MOTIF */
137 #endif /* USE_TOOLKIT_SCROLL_BARS */
139 #endif /* USE_X_TOOLKIT */
141 #ifndef USE_X_TOOLKIT
142 #define x_any_window_to_frame x_window_to_frame
143 #define x_top_window_to_frame x_window_to_frame
144 #endif
146 #ifdef USE_X_TOOLKIT
147 #include "widget.h"
148 #ifndef XtNinitialState
149 #define XtNinitialState "initialState"
150 #endif
151 #endif
153 #define abs(x) ((x) < 0 ? -(x) : (x))
155 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
158 /* Fringe bitmaps. */
160 enum fringe_bitmap_type
162 NO_FRINGE_BITMAP,
163 LEFT_TRUNCATION_BITMAP,
164 RIGHT_TRUNCATION_BITMAP,
165 OVERLAY_ARROW_BITMAP,
166 CONTINUED_LINE_BITMAP,
167 CONTINUATION_LINE_BITMAP,
168 ZV_LINE_BITMAP
171 /* Bitmap drawn to indicate lines not displaying text if
172 `indicate-empty-lines' is non-nil. */
174 #define zv_width 8
175 #define zv_height 72
176 #define zv_period 3
177 static unsigned char zv_bits[] = {
178 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
179 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
180 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
181 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
182 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
183 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
187 /* An arrow like this: `<-'. */
189 #define left_width 8
190 #define left_height 8
191 static unsigned char left_bits[] = {
192 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
194 /* Right truncation arrow bitmap `->'. */
196 #define right_width 8
197 #define right_height 8
198 static unsigned char right_bits[] = {
199 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
201 /* Marker for continued lines. */
203 #define continued_width 8
204 #define continued_height 8
205 static unsigned char continued_bits[] = {
206 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
208 /* Marker for continuation lines. */
210 #define continuation_width 8
211 #define continuation_height 8
212 static unsigned char continuation_bits[] = {
213 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
215 /* Overlay arrow bitmap. */
217 #if 0
218 /* A bomb. */
219 #define ov_width 8
220 #define ov_height 8
221 static unsigned char ov_bits[] = {
222 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
223 #else
224 /* A triangular arrow. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
230 #endif
232 extern Lisp_Object Qhelp_echo;
235 /* Non-nil means Emacs uses toolkit scroll bars. */
237 Lisp_Object Vx_toolkit_scroll_bars;
239 /* If a string, XTread_socket generates an event to display that string.
240 (The display is done in read_char.) */
242 static Lisp_Object help_echo;
243 static Lisp_Object help_echo_window;
244 static Lisp_Object help_echo_object;
245 static int help_echo_pos;
247 /* Temporary variable for XTread_socket. */
249 static Lisp_Object previous_help_echo;
251 /* Non-zero means that a HELP_EVENT has been generated since Emacs
252 start. */
254 static int any_help_event_p;
256 /* Non-zero means draw block and hollow cursor as wide as the glyph
257 under it. For example, if a block cursor is over a tab, it will be
258 drawn as wide as that tab on the display. */
260 int x_stretch_cursor_p;
262 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
264 int x_use_underline_position_properties;
266 /* This is a chain of structures for all the X displays currently in
267 use. */
269 struct x_display_info *x_display_list;
271 /* This is a list of cons cells, each of the form (NAME
272 . FONT-LIST-CACHE), one for each element of x_display_list and in
273 the same order. NAME is the name of the frame. FONT-LIST-CACHE
274 records previous values returned by x-list-fonts. */
276 Lisp_Object x_display_name_list;
278 /* Frame being updated by update_frame. This is declared in term.c.
279 This is set by update_begin and looked at by all the XT functions.
280 It is zero while not inside an update. In that case, the XT
281 functions assume that `selected_frame' is the frame to apply to. */
283 extern struct frame *updating_frame;
285 /* This is a frame waiting to be auto-raised, within XTread_socket. */
287 struct frame *pending_autoraise_frame;
289 #ifdef USE_X_TOOLKIT
290 /* The application context for Xt use. */
291 XtAppContext Xt_app_con;
292 static String Xt_default_resources[] = {0};
293 #endif /* USE_X_TOOLKIT */
295 /* Nominal cursor position -- where to draw output.
296 HPOS and VPOS are window relative glyph matrix coordinates.
297 X and Y are window relative pixel coordinates. */
299 struct cursor_pos output_cursor;
301 /* Non-zero means user is interacting with a toolkit scroll bar. */
303 static int toolkit_scroll_bar_interaction;
305 /* Mouse movement.
307 Formerly, we used PointerMotionHintMask (in standard_event_mask)
308 so that we would have to call XQueryPointer after each MotionNotify
309 event to ask for another such event. However, this made mouse tracking
310 slow, and there was a bug that made it eventually stop.
312 Simply asking for MotionNotify all the time seems to work better.
314 In order to avoid asking for motion events and then throwing most
315 of them away or busy-polling the server for mouse positions, we ask
316 the server for pointer motion hints. This means that we get only
317 one event per group of mouse movements. "Groups" are delimited by
318 other kinds of events (focus changes and button clicks, for
319 example), or by XQueryPointer calls; when one of these happens, we
320 get another MotionNotify event the next time the mouse moves. This
321 is at least as efficient as getting motion events when mouse
322 tracking is on, and I suspect only negligibly worse when tracking
323 is off. */
325 /* Where the mouse was last time we reported a mouse event. */
327 FRAME_PTR last_mouse_frame;
328 static XRectangle last_mouse_glyph;
329 static Lisp_Object last_mouse_press_frame;
331 /* The scroll bar in which the last X motion event occurred.
333 If the last X motion event occurred in a scroll bar, we set this so
334 XTmouse_position can know whether to report a scroll bar motion or
335 an ordinary motion.
337 If the last X motion event didn't occur in a scroll bar, we set
338 this to Qnil, to tell XTmouse_position to return an ordinary motion
339 event. */
341 static Lisp_Object last_mouse_scroll_bar;
343 /* This is a hack. We would really prefer that XTmouse_position would
344 return the time associated with the position it returns, but there
345 doesn't seem to be any way to wrest the time-stamp from the server
346 along with the position query. So, we just keep track of the time
347 of the last movement we received, and return that in hopes that
348 it's somewhat accurate. */
350 static Time last_mouse_movement_time;
352 /* Incremented by XTread_socket whenever it really tries to read
353 events. */
355 #ifdef __STDC__
356 static int volatile input_signal_count;
357 #else
358 static int input_signal_count;
359 #endif
361 /* Used locally within XTread_socket. */
363 static int x_noop_count;
365 /* Initial values of argv and argc. */
367 extern char **initial_argv;
368 extern int initial_argc;
370 extern Lisp_Object Vcommand_line_args, Vsystem_name;
372 /* Tells if a window manager is present or not. */
374 extern Lisp_Object Vx_no_window_manager;
376 extern Lisp_Object Qface, Qmouse_face;
378 extern int errno;
380 /* A mask of extra modifier bits to put into every keyboard char. */
382 extern int extra_keyboard_modifiers;
384 static Lisp_Object Qvendor_specific_keysyms;
386 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
387 extern Lisp_Object x_icon_type P_ ((struct frame *));
390 /* Enumeration for overriding/changing the face to use for drawing
391 glyphs in x_draw_glyphs. */
393 enum draw_glyphs_face
395 DRAW_NORMAL_TEXT,
396 DRAW_INVERSE_VIDEO,
397 DRAW_CURSOR,
398 DRAW_MOUSE_FACE,
399 DRAW_IMAGE_RAISED,
400 DRAW_IMAGE_SUNKEN
403 static int cursor_in_mouse_face_p P_ ((struct window *));
404 static int clear_mouse_face P_ ((struct x_display_info *));
405 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
406 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
407 static const XColor *x_color_cells P_ ((Display *, int *));
408 static void x_update_window_end P_ ((struct window *, int, int));
409 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
410 void x_delete_display P_ ((struct x_display_info *));
411 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
412 unsigned));
413 static int fast_find_position P_ ((struct window *, int, int *, int *,
414 int *, int *, Lisp_Object));
415 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
416 int *, int *, int *, int *, int));
417 static void set_output_cursor P_ ((struct cursor_pos *));
418 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
419 int *, int *, int *, int));
420 static void note_mode_line_highlight P_ ((struct window *, int, int));
421 static void note_mouse_highlight P_ ((struct frame *, int, int));
422 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
423 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
424 static void show_mouse_face P_ ((struct x_display_info *,
425 enum draw_glyphs_face));
426 static int x_io_error_quitter P_ ((Display *));
427 int x_catch_errors P_ ((Display *));
428 void x_uncatch_errors P_ ((Display *, int));
429 void x_lower_frame P_ ((struct frame *));
430 void x_scroll_bar_clear P_ ((struct frame *));
431 int x_had_errors_p P_ ((Display *));
432 void x_wm_set_size_hint P_ ((struct frame *, long, int));
433 void x_raise_frame P_ ((struct frame *));
434 void x_set_window_size P_ ((struct frame *, int, int, int));
435 void x_wm_set_window_state P_ ((struct frame *, int));
436 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
437 void x_initialize P_ ((void));
438 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
439 static int x_compute_min_glyph_bounds P_ ((struct frame *));
440 static void x_draw_phys_cursor_glyph P_ ((struct window *,
441 struct glyph_row *,
442 enum draw_glyphs_face));
443 static void x_update_end P_ ((struct frame *));
444 static void XTframe_up_to_date P_ ((struct frame *));
445 static void XTset_terminal_modes P_ ((void));
446 static void XTreset_terminal_modes P_ ((void));
447 static void XTcursor_to P_ ((int, int, int, int));
448 static void x_write_glyphs P_ ((struct glyph *, int));
449 static void x_clear_end_of_line P_ ((int));
450 static void x_clear_frame P_ ((void));
451 static void x_clear_cursor P_ ((struct window *));
452 static void frame_highlight P_ ((struct frame *));
453 static void frame_unhighlight P_ ((struct frame *));
454 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
455 static void XTframe_rehighlight P_ ((struct frame *));
456 static void x_frame_rehighlight P_ ((struct x_display_info *));
457 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
458 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
459 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
460 XRectangle *));
461 static void expose_frame P_ ((struct frame *, int, int, int, int));
462 static int expose_window_tree P_ ((struct window *, XRectangle *));
463 static int expose_window P_ ((struct window *, XRectangle *));
464 static void expose_area P_ ((struct window *, struct glyph_row *,
465 XRectangle *, enum glyph_row_area));
466 static int expose_line P_ ((struct window *, struct glyph_row *,
467 XRectangle *));
468 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
469 static void x_update_window_cursor P_ ((struct window *, int));
470 static void x_erase_phys_cursor P_ ((struct window *));
471 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
472 static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
473 enum fringe_bitmap_type, int left_p));
475 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
476 GC, int));
477 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
478 static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
479 static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
480 int, int, int, int));
481 static void x_flush P_ ((struct frame *f));
482 static void x_update_begin P_ ((struct frame *));
483 static void x_update_window_begin P_ ((struct window *));
484 static void x_draw_vertical_border P_ ((struct window *));
485 static void x_after_update_window_line P_ ((struct glyph_row *));
486 static INLINE void take_vertical_position_into_account P_ ((struct it *));
487 static void x_produce_stretch_glyph P_ ((struct it *));
488 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
489 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
490 enum scroll_bar_part *,
491 Lisp_Object *, Lisp_Object *,
492 unsigned long *));
493 static void x_check_fullscreen P_ ((struct frame *));
494 static void x_check_fullscreen_move P_ ((struct frame *));
496 /* Flush display of frame F, or of all frames if F is null. */
498 static void
499 x_flush (f)
500 struct frame *f;
502 BLOCK_INPUT;
503 if (f == NULL)
505 Lisp_Object rest, frame;
506 FOR_EACH_FRAME (rest, frame)
507 x_flush (XFRAME (frame));
509 else if (FRAME_X_P (f))
510 XFlush (FRAME_X_DISPLAY (f));
511 UNBLOCK_INPUT;
515 /* Remove calls to XFlush by defining XFlush to an empty replacement.
516 Calls to XFlush should be unnecessary because the X output buffer
517 is flushed automatically as needed by calls to XPending,
518 XNextEvent, or XWindowEvent according to the XFlush man page.
519 XTread_socket calls XPending. Removing XFlush improves
520 performance. */
522 #define XFlush(DISPLAY) (void) 0
525 /***********************************************************************
526 Debugging
527 ***********************************************************************/
529 #if 0
531 /* This is a function useful for recording debugging information about
532 the sequence of occurrences in this file. */
534 struct record
536 char *locus;
537 int type;
540 struct record event_record[100];
542 int event_record_index;
544 record_event (locus, type)
545 char *locus;
546 int type;
548 if (event_record_index == sizeof (event_record) / sizeof (struct record))
549 event_record_index = 0;
551 event_record[event_record_index].locus = locus;
552 event_record[event_record_index].type = type;
553 event_record_index++;
556 #endif /* 0 */
560 /* Return the struct x_display_info corresponding to DPY. */
562 struct x_display_info *
563 x_display_info_for_display (dpy)
564 Display *dpy;
566 struct x_display_info *dpyinfo;
568 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
569 if (dpyinfo->display == dpy)
570 return dpyinfo;
572 return 0;
577 /***********************************************************************
578 Starting and ending an update
579 ***********************************************************************/
581 /* Start an update of frame F. This function is installed as a hook
582 for update_begin, i.e. it is called when update_begin is called.
583 This function is called prior to calls to x_update_window_begin for
584 each window being updated. Currently, there is nothing to do here
585 because all interesting stuff is done on a window basis. */
587 static void
588 x_update_begin (f)
589 struct frame *f;
591 /* Nothing to do. */
595 /* Start update of window W. Set the global variable updated_window
596 to the window being updated and set output_cursor to the cursor
597 position of W. */
599 static void
600 x_update_window_begin (w)
601 struct window *w;
603 struct frame *f = XFRAME (WINDOW_FRAME (w));
604 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
606 updated_window = w;
607 set_output_cursor (&w->cursor);
609 BLOCK_INPUT;
611 if (f == display_info->mouse_face_mouse_frame)
613 /* Don't do highlighting for mouse motion during the update. */
614 display_info->mouse_face_defer = 1;
616 /* If F needs to be redrawn, simply forget about any prior mouse
617 highlighting. */
618 if (FRAME_GARBAGED_P (f))
619 display_info->mouse_face_window = Qnil;
621 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
622 their mouse_face_p flag set, which means that they are always
623 unequal to rows in a desired matrix which never have that
624 flag set. So, rows containing mouse-face glyphs are never
625 scrolled, and we don't have to switch the mouse highlight off
626 here to prevent it from being scrolled. */
628 /* Can we tell that this update does not affect the window
629 where the mouse highlight is? If so, no need to turn off.
630 Likewise, don't do anything if the frame is garbaged;
631 in that case, the frame's current matrix that we would use
632 is all wrong, and we will redisplay that line anyway. */
633 if (!NILP (display_info->mouse_face_window)
634 && w == XWINDOW (display_info->mouse_face_window))
636 int i;
638 for (i = 0; i < w->desired_matrix->nrows; ++i)
639 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
640 break;
642 if (i < w->desired_matrix->nrows)
643 clear_mouse_face (display_info);
645 #endif /* 0 */
648 UNBLOCK_INPUT;
652 /* Draw a vertical window border to the right of window W if W doesn't
653 have vertical scroll bars. */
655 static void
656 x_draw_vertical_border (w)
657 struct window *w;
659 struct frame *f = XFRAME (WINDOW_FRAME (w));
661 /* Redraw borders between horizontally adjacent windows. Don't
662 do it for frames with vertical scroll bars because either the
663 right scroll bar of a window, or the left scroll bar of its
664 neighbor will suffice as a border. */
665 if (!WINDOW_RIGHTMOST_P (w)
666 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
668 int x0, x1, y0, y1;
670 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
671 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
672 y1 -= 1;
674 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
675 f->output_data.x->normal_gc, x1, y0, x1, y1);
680 /* End update of window W (which is equal to updated_window).
682 Draw vertical borders between horizontally adjacent windows, and
683 display W's cursor if CURSOR_ON_P is non-zero.
685 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
686 glyphs in mouse-face were overwritten. In that case we have to
687 make sure that the mouse-highlight is properly redrawn.
689 W may be a menu bar pseudo-window in case we don't have X toolkit
690 support. Such windows don't have a cursor, so don't display it
691 here. */
693 static void
694 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
695 struct window *w;
696 int cursor_on_p, mouse_face_overwritten_p;
698 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
700 if (!w->pseudo_window_p)
702 BLOCK_INPUT;
704 if (cursor_on_p)
705 x_display_and_set_cursor (w, 1, output_cursor.hpos,
706 output_cursor.vpos,
707 output_cursor.x, output_cursor.y);
709 x_draw_vertical_border (w);
710 UNBLOCK_INPUT;
713 /* If a row with mouse-face was overwritten, arrange for
714 XTframe_up_to_date to redisplay the mouse highlight. */
715 if (mouse_face_overwritten_p)
717 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
718 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
719 dpyinfo->mouse_face_window = Qnil;
722 updated_window = NULL;
726 /* End update of frame F. This function is installed as a hook in
727 update_end. */
729 static void
730 x_update_end (f)
731 struct frame *f;
733 /* Mouse highlight may be displayed again. */
734 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
736 BLOCK_INPUT;
737 XFlush (FRAME_X_DISPLAY (f));
738 UNBLOCK_INPUT;
742 /* This function is called from various places in xdisp.c whenever a
743 complete update has been performed. The global variable
744 updated_window is not available here. */
746 static void
747 XTframe_up_to_date (f)
748 struct frame *f;
750 if (FRAME_X_P (f))
752 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
754 if (dpyinfo->mouse_face_deferred_gc
755 || f == dpyinfo->mouse_face_mouse_frame)
757 BLOCK_INPUT;
758 if (dpyinfo->mouse_face_mouse_frame)
759 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
760 dpyinfo->mouse_face_mouse_x,
761 dpyinfo->mouse_face_mouse_y);
762 dpyinfo->mouse_face_deferred_gc = 0;
763 UNBLOCK_INPUT;
769 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
770 arrow bitmaps, or clear the fringes if no bitmaps are required
771 before DESIRED_ROW is made current. The window being updated is
772 found in updated_window. This function It is called from
773 update_window_line only if it is known that there are differences
774 between bitmaps to be drawn between current row and DESIRED_ROW. */
776 static void
777 x_after_update_window_line (desired_row)
778 struct glyph_row *desired_row;
780 struct window *w = updated_window;
781 struct frame *f;
782 int width, height;
784 xassert (w);
786 if (!desired_row->mode_line_p && !w->pseudo_window_p)
788 BLOCK_INPUT;
789 x_draw_row_fringe_bitmaps (w, desired_row);
790 UNBLOCK_INPUT;
793 /* When a window has disappeared, make sure that no rest of
794 full-width rows stays visible in the internal border. Could
795 check here if updated_window is the leftmost/rightmost window,
796 but I guess it's not worth doing since vertically split windows
797 are almost never used, internal border is rarely set, and the
798 overhead is very small. */
799 if (windows_or_buffers_changed
800 && desired_row->full_width_p
801 && (f = XFRAME (w->frame),
802 width = FRAME_INTERNAL_BORDER_WIDTH (f),
803 width != 0)
804 && (height = desired_row->visible_height,
805 height > 0))
807 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
809 /* Internal border is drawn below the tool bar. */
810 if (WINDOWP (f->tool_bar_window)
811 && w == XWINDOW (f->tool_bar_window))
812 y -= width;
814 BLOCK_INPUT;
815 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
816 0, y, width, height, False);
817 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
818 f->output_data.x->pixel_width - width,
819 y, width, height, False);
820 UNBLOCK_INPUT;
825 /* Draw the bitmap WHICH in one of the left or right fringes of
826 window W. ROW is the glyph row for which to display the bitmap; it
827 determines the vertical position at which the bitmap has to be
828 drawn. */
830 static void
831 x_draw_fringe_bitmap (w, row, which, left_p)
832 struct window *w;
833 struct glyph_row *row;
834 enum fringe_bitmap_type which;
835 int left_p;
837 struct frame *f = XFRAME (WINDOW_FRAME (w));
838 Display *display = FRAME_X_DISPLAY (f);
839 Window window = FRAME_X_WINDOW (f);
840 int x, y, wd, h, dy;
841 int b1, b2;
842 unsigned char *bits;
843 Pixmap pixmap;
844 GC gc = f->output_data.x->normal_gc;
845 struct face *face;
846 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
848 /* Must clip because of partially visible lines. */
849 x_clip_to_row (w, row, gc, 1);
851 /* Convert row to frame coordinates. */
852 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
854 switch (which)
856 case NO_FRINGE_BITMAP:
857 wd = 0;
858 h = 0;
859 break;
861 case LEFT_TRUNCATION_BITMAP:
862 wd = left_width;
863 h = left_height;
864 bits = left_bits;
865 break;
867 case OVERLAY_ARROW_BITMAP:
868 wd = ov_width;
869 h = ov_height;
870 bits = ov_bits;
871 break;
873 case RIGHT_TRUNCATION_BITMAP:
874 wd = right_width;
875 h = right_height;
876 bits = right_bits;
877 break;
879 case CONTINUED_LINE_BITMAP:
880 wd = continued_width;
881 h = continued_height;
882 bits = continued_bits;
883 break;
885 case CONTINUATION_LINE_BITMAP:
886 wd = continuation_width;
887 h = continuation_height;
888 bits = continuation_bits;
889 break;
891 case ZV_LINE_BITMAP:
892 wd = zv_width;
893 h = zv_height - (y % zv_period);
894 bits = zv_bits + (y % zv_period);
895 break;
897 default:
898 abort ();
901 /* Clip bitmap if too high. */
902 if (h > row->height)
903 h = row->height;
905 /* Set dy to the offset in the row to start drawing the bitmap. */
906 dy = (row->height - h) / 2;
908 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
909 PREPARE_FACE_FOR_DISPLAY (f, face);
911 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
912 the fringe. */
913 b1 = b2 = -1;
914 if (left_p)
916 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
917 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
918 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
919 - wd
920 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
921 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
923 /* If W has a vertical border to its left, don't draw over it. */
924 int border = ((XFASTINT (w->left) > 0
925 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
926 ? 1 : 0);
927 b1 = (window_box_left (w, -1)
928 - FRAME_X_LEFT_FRINGE_WIDTH (f)
929 + border);
930 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
933 else
935 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
936 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
937 x = (window_box_right (w, -1)
938 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
939 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
940 the fringe. */
941 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
943 b1 = window_box_right (w, -1);
944 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
948 if (b1 >= 0)
950 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
952 /* In case the same realized face is used for fringes and
953 for something displayed in the text (e.g. face `region' on
954 mono-displays, the fill style may have been changed to
955 FillSolid in x_draw_glyph_string_background. */
956 if (face->stipple)
957 XSetFillStyle (display, face->gc, FillOpaqueStippled);
958 else
959 XSetForeground (display, face->gc, face->background);
961 XFillRectangle (display, window, face->gc,
963 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
964 row->y)),
966 row->visible_height);
967 if (!face->stipple)
968 XSetForeground (display, face->gc, face->foreground);
971 if (which != NO_FRINGE_BITMAP)
973 /* Draw the bitmap. I believe these small pixmaps can be cached
974 by the server. */
975 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
976 face->foreground,
977 face->background, depth);
978 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
979 XFreePixmap (display, pixmap);
982 XSetClipMask (display, gc, None);
986 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
987 function with input blocked. */
989 static void
990 x_draw_row_fringe_bitmaps (w, row)
991 struct window *w;
992 struct glyph_row *row;
994 struct frame *f = XFRAME (w->frame);
995 enum fringe_bitmap_type bitmap;
997 xassert (interrupt_input_blocked);
999 /* If row is completely invisible, because of vscrolling, we
1000 don't have to draw anything. */
1001 if (row->visible_height <= 0)
1002 return;
1004 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1006 /* Decide which bitmap to draw in the left fringe. */
1007 if (row->overlay_arrow_p)
1008 bitmap = OVERLAY_ARROW_BITMAP;
1009 else if (row->truncated_on_left_p)
1010 bitmap = LEFT_TRUNCATION_BITMAP;
1011 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1012 bitmap = CONTINUATION_LINE_BITMAP;
1013 else if (row->indicate_empty_line_p)
1014 bitmap = ZV_LINE_BITMAP;
1015 else
1016 bitmap = NO_FRINGE_BITMAP;
1018 x_draw_fringe_bitmap (w, row, bitmap, 1);
1021 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1023 /* Decide which bitmap to draw in the right fringe. */
1024 if (row->truncated_on_right_p)
1025 bitmap = RIGHT_TRUNCATION_BITMAP;
1026 else if (row->continued_p)
1027 bitmap = CONTINUED_LINE_BITMAP;
1028 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1029 bitmap = ZV_LINE_BITMAP;
1030 else
1031 bitmap = NO_FRINGE_BITMAP;
1033 x_draw_fringe_bitmap (w, row, bitmap, 0);
1039 /* This is called when starting Emacs and when restarting after
1040 suspend. When starting Emacs, no X window is mapped. And nothing
1041 must be done to Emacs's own window if it is suspended (though that
1042 rarely happens). */
1044 static void
1045 XTset_terminal_modes ()
1049 /* This is called when exiting or suspending Emacs. Exiting will make
1050 the X-windows go away, and suspending requires no action. */
1052 static void
1053 XTreset_terminal_modes ()
1059 /***********************************************************************
1060 Output Cursor
1061 ***********************************************************************/
1063 /* Set the global variable output_cursor to CURSOR. All cursor
1064 positions are relative to updated_window. */
1066 static void
1067 set_output_cursor (cursor)
1068 struct cursor_pos *cursor;
1070 output_cursor.hpos = cursor->hpos;
1071 output_cursor.vpos = cursor->vpos;
1072 output_cursor.x = cursor->x;
1073 output_cursor.y = cursor->y;
1077 /* Set a nominal cursor position.
1079 HPOS and VPOS are column/row positions in a window glyph matrix. X
1080 and Y are window text area relative pixel positions.
1082 If this is done during an update, updated_window will contain the
1083 window that is being updated and the position is the future output
1084 cursor position for that window. If updated_window is null, use
1085 selected_window and display the cursor at the given position. */
1087 static void
1088 XTcursor_to (vpos, hpos, y, x)
1089 int vpos, hpos, y, x;
1091 struct window *w;
1093 /* If updated_window is not set, work on selected_window. */
1094 if (updated_window)
1095 w = updated_window;
1096 else
1097 w = XWINDOW (selected_window);
1099 /* Set the output cursor. */
1100 output_cursor.hpos = hpos;
1101 output_cursor.vpos = vpos;
1102 output_cursor.x = x;
1103 output_cursor.y = y;
1105 /* If not called as part of an update, really display the cursor.
1106 This will also set the cursor position of W. */
1107 if (updated_window == NULL)
1109 BLOCK_INPUT;
1110 x_display_cursor (w, 1, hpos, vpos, x, y);
1111 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1112 UNBLOCK_INPUT;
1118 /***********************************************************************
1119 Display Iterator
1120 ***********************************************************************/
1122 /* Function prototypes of this page. */
1124 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1125 struct glyph *,
1126 XChar2b *,
1127 int *));
1128 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1129 int, XChar2b *, int));
1130 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1131 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1132 static void x_append_glyph P_ ((struct it *));
1133 static void x_append_composite_glyph P_ ((struct it *));
1134 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1135 int, int, double));
1136 static void x_produce_glyphs P_ ((struct it *));
1137 static void x_produce_image_glyph P_ ((struct it *it));
1140 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1141 is not contained in the font. */
1143 static INLINE XCharStruct *
1144 x_per_char_metric (font, char2b)
1145 XFontStruct *font;
1146 XChar2b *char2b;
1148 /* The result metric information. */
1149 XCharStruct *pcm = NULL;
1151 xassert (font && char2b);
1153 if (font->per_char != NULL)
1155 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1157 /* min_char_or_byte2 specifies the linear character index
1158 corresponding to the first element of the per_char array,
1159 max_char_or_byte2 is the index of the last character. A
1160 character with non-zero CHAR2B->byte1 is not in the font.
1161 A character with byte2 less than min_char_or_byte2 or
1162 greater max_char_or_byte2 is not in the font. */
1163 if (char2b->byte1 == 0
1164 && char2b->byte2 >= font->min_char_or_byte2
1165 && char2b->byte2 <= font->max_char_or_byte2)
1166 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1168 else
1170 /* If either min_byte1 or max_byte1 are nonzero, both
1171 min_char_or_byte2 and max_char_or_byte2 are less than
1172 256, and the 2-byte character index values corresponding
1173 to the per_char array element N (counting from 0) are:
1175 byte1 = N/D + min_byte1
1176 byte2 = N\D + min_char_or_byte2
1178 where:
1180 D = max_char_or_byte2 - min_char_or_byte2 + 1
1181 / = integer division
1182 \ = integer modulus */
1183 if (char2b->byte1 >= font->min_byte1
1184 && char2b->byte1 <= font->max_byte1
1185 && char2b->byte2 >= font->min_char_or_byte2
1186 && char2b->byte2 <= font->max_char_or_byte2)
1188 pcm = (font->per_char
1189 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1190 * (char2b->byte1 - font->min_byte1))
1191 + (char2b->byte2 - font->min_char_or_byte2));
1195 else
1197 /* If the per_char pointer is null, all glyphs between the first
1198 and last character indexes inclusive have the same
1199 information, as given by both min_bounds and max_bounds. */
1200 if (char2b->byte2 >= font->min_char_or_byte2
1201 && char2b->byte2 <= font->max_char_or_byte2)
1202 pcm = &font->max_bounds;
1205 return ((pcm == NULL
1206 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1207 ? NULL : pcm);
1211 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1212 the two-byte form of C. Encoding is returned in *CHAR2B. */
1214 static INLINE void
1215 x_encode_char (c, char2b, font_info)
1216 int c;
1217 XChar2b *char2b;
1218 struct font_info *font_info;
1220 int charset = CHAR_CHARSET (c);
1221 XFontStruct *font = font_info->font;
1223 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1224 This may be either a program in a special encoder language or a
1225 fixed encoding. */
1226 if (font_info->font_encoder)
1228 /* It's a program. */
1229 struct ccl_program *ccl = font_info->font_encoder;
1231 if (CHARSET_DIMENSION (charset) == 1)
1233 ccl->reg[0] = charset;
1234 ccl->reg[1] = char2b->byte2;
1236 else
1238 ccl->reg[0] = charset;
1239 ccl->reg[1] = char2b->byte1;
1240 ccl->reg[2] = char2b->byte2;
1243 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1245 /* We assume that MSBs are appropriately set/reset by CCL
1246 program. */
1247 if (font->max_byte1 == 0) /* 1-byte font */
1248 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1249 else
1250 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1252 else if (font_info->encoding[charset])
1254 /* Fixed encoding scheme. See fontset.h for the meaning of the
1255 encoding numbers. */
1256 int enc = font_info->encoding[charset];
1258 if ((enc == 1 || enc == 2)
1259 && CHARSET_DIMENSION (charset) == 2)
1260 char2b->byte1 |= 0x80;
1262 if (enc == 1 || enc == 3)
1263 char2b->byte2 |= 0x80;
1268 /* Get face and two-byte form of character C in face FACE_ID on frame
1269 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1270 means we want to display multibyte text. Value is a pointer to a
1271 realized face that is ready for display. */
1273 static INLINE struct face *
1274 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1275 struct frame *f;
1276 int c, face_id;
1277 XChar2b *char2b;
1278 int multibyte_p;
1280 struct face *face = FACE_FROM_ID (f, face_id);
1282 if (!multibyte_p)
1284 /* Unibyte case. We don't have to encode, but we have to make
1285 sure to use a face suitable for unibyte. */
1286 char2b->byte1 = 0;
1287 char2b->byte2 = c;
1288 face_id = FACE_FOR_CHAR (f, face, c);
1289 face = FACE_FROM_ID (f, face_id);
1291 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1293 /* Case of ASCII in a face known to fit ASCII. */
1294 char2b->byte1 = 0;
1295 char2b->byte2 = c;
1297 else
1299 int c1, c2, charset;
1301 /* Split characters into bytes. If c2 is -1 afterwards, C is
1302 really a one-byte character so that byte1 is zero. */
1303 SPLIT_CHAR (c, charset, c1, c2);
1304 if (c2 > 0)
1305 char2b->byte1 = c1, char2b->byte2 = c2;
1306 else
1307 char2b->byte1 = 0, char2b->byte2 = c1;
1309 /* Maybe encode the character in *CHAR2B. */
1310 if (face->font != NULL)
1312 struct font_info *font_info
1313 = FONT_INFO_FROM_ID (f, face->font_info_id);
1314 if (font_info)
1315 x_encode_char (c, char2b, font_info);
1319 /* Make sure X resources of the face are allocated. */
1320 xassert (face != NULL);
1321 PREPARE_FACE_FOR_DISPLAY (f, face);
1323 return face;
1327 /* Get face and two-byte form of character glyph GLYPH on frame F.
1328 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1329 a pointer to a realized face that is ready for display. */
1331 static INLINE struct face *
1332 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1333 struct frame *f;
1334 struct glyph *glyph;
1335 XChar2b *char2b;
1336 int *two_byte_p;
1338 struct face *face;
1340 xassert (glyph->type == CHAR_GLYPH);
1341 face = FACE_FROM_ID (f, glyph->face_id);
1343 if (two_byte_p)
1344 *two_byte_p = 0;
1346 if (!glyph->multibyte_p)
1348 /* Unibyte case. We don't have to encode, but we have to make
1349 sure to use a face suitable for unibyte. */
1350 char2b->byte1 = 0;
1351 char2b->byte2 = glyph->u.ch;
1353 else if (glyph->u.ch < 128
1354 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1356 /* Case of ASCII in a face known to fit ASCII. */
1357 char2b->byte1 = 0;
1358 char2b->byte2 = glyph->u.ch;
1360 else
1362 int c1, c2, charset;
1364 /* Split characters into bytes. If c2 is -1 afterwards, C is
1365 really a one-byte character so that byte1 is zero. */
1366 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1367 if (c2 > 0)
1368 char2b->byte1 = c1, char2b->byte2 = c2;
1369 else
1370 char2b->byte1 = 0, char2b->byte2 = c1;
1372 /* Maybe encode the character in *CHAR2B. */
1373 if (charset != CHARSET_ASCII)
1375 struct font_info *font_info
1376 = FONT_INFO_FROM_ID (f, face->font_info_id);
1377 if (font_info)
1379 x_encode_char (glyph->u.ch, char2b, font_info);
1380 if (two_byte_p)
1381 *two_byte_p
1382 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1387 /* Make sure X resources of the face are allocated. */
1388 xassert (face != NULL);
1389 PREPARE_FACE_FOR_DISPLAY (f, face);
1390 return face;
1394 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1395 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1397 static INLINE void
1398 x_append_glyph (it)
1399 struct it *it;
1401 struct glyph *glyph;
1402 enum glyph_row_area area = it->area;
1404 xassert (it->glyph_row);
1405 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1407 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1408 if (glyph < it->glyph_row->glyphs[area + 1])
1410 glyph->charpos = CHARPOS (it->position);
1411 glyph->object = it->object;
1412 glyph->pixel_width = it->pixel_width;
1413 glyph->voffset = it->voffset;
1414 glyph->type = CHAR_GLYPH;
1415 glyph->multibyte_p = it->multibyte_p;
1416 glyph->left_box_line_p = it->start_of_box_run_p;
1417 glyph->right_box_line_p = it->end_of_box_run_p;
1418 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1419 || it->phys_descent > it->descent);
1420 glyph->padding_p = 0;
1421 glyph->glyph_not_available_p = it->glyph_not_available_p;
1422 glyph->face_id = it->face_id;
1423 glyph->u.ch = it->char_to_display;
1424 ++it->glyph_row->used[area];
1428 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1429 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1431 static INLINE void
1432 x_append_composite_glyph (it)
1433 struct it *it;
1435 struct glyph *glyph;
1436 enum glyph_row_area area = it->area;
1438 xassert (it->glyph_row);
1440 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1441 if (glyph < it->glyph_row->glyphs[area + 1])
1443 glyph->charpos = CHARPOS (it->position);
1444 glyph->object = it->object;
1445 glyph->pixel_width = it->pixel_width;
1446 glyph->voffset = it->voffset;
1447 glyph->type = COMPOSITE_GLYPH;
1448 glyph->multibyte_p = it->multibyte_p;
1449 glyph->left_box_line_p = it->start_of_box_run_p;
1450 glyph->right_box_line_p = it->end_of_box_run_p;
1451 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1452 || it->phys_descent > it->descent);
1453 glyph->padding_p = 0;
1454 glyph->glyph_not_available_p = 0;
1455 glyph->face_id = it->face_id;
1456 glyph->u.cmp_id = it->cmp_id;
1457 ++it->glyph_row->used[area];
1462 /* Change IT->ascent and IT->height according to the setting of
1463 IT->voffset. */
1465 static INLINE void
1466 take_vertical_position_into_account (it)
1467 struct it *it;
1469 if (it->voffset)
1471 if (it->voffset < 0)
1472 /* Increase the ascent so that we can display the text higher
1473 in the line. */
1474 it->ascent += abs (it->voffset);
1475 else
1476 /* Increase the descent so that we can display the text lower
1477 in the line. */
1478 it->descent += it->voffset;
1483 /* Produce glyphs/get display metrics for the image IT is loaded with.
1484 See the description of struct display_iterator in dispextern.h for
1485 an overview of struct display_iterator. */
1487 static void
1488 x_produce_image_glyph (it)
1489 struct it *it;
1491 struct image *img;
1492 struct face *face;
1494 xassert (it->what == IT_IMAGE);
1496 face = FACE_FROM_ID (it->f, it->face_id);
1497 img = IMAGE_FROM_ID (it->f, it->image_id);
1498 xassert (img);
1500 /* Make sure X resources of the face and image are loaded. */
1501 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1502 prepare_image_for_display (it->f, img);
1504 it->ascent = it->phys_ascent = image_ascent (img, face);
1505 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1506 it->pixel_width = img->width + 2 * img->hmargin;
1508 it->nglyphs = 1;
1510 if (face->box != FACE_NO_BOX)
1512 if (face->box_line_width > 0)
1514 it->ascent += face->box_line_width;
1515 it->descent += face->box_line_width;
1518 if (it->start_of_box_run_p)
1519 it->pixel_width += abs (face->box_line_width);
1520 if (it->end_of_box_run_p)
1521 it->pixel_width += abs (face->box_line_width);
1524 take_vertical_position_into_account (it);
1526 if (it->glyph_row)
1528 struct glyph *glyph;
1529 enum glyph_row_area area = it->area;
1531 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1532 if (glyph < it->glyph_row->glyphs[area + 1])
1534 glyph->charpos = CHARPOS (it->position);
1535 glyph->object = it->object;
1536 glyph->pixel_width = it->pixel_width;
1537 glyph->voffset = it->voffset;
1538 glyph->type = IMAGE_GLYPH;
1539 glyph->multibyte_p = it->multibyte_p;
1540 glyph->left_box_line_p = it->start_of_box_run_p;
1541 glyph->right_box_line_p = it->end_of_box_run_p;
1542 glyph->overlaps_vertically_p = 0;
1543 glyph->padding_p = 0;
1544 glyph->glyph_not_available_p = 0;
1545 glyph->face_id = it->face_id;
1546 glyph->u.img_id = img->id;
1547 ++it->glyph_row->used[area];
1553 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1554 of the glyph, WIDTH and HEIGHT are the width and height of the
1555 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1556 ascent of the glyph (0 <= ASCENT <= 1). */
1558 static void
1559 x_append_stretch_glyph (it, object, width, height, ascent)
1560 struct it *it;
1561 Lisp_Object object;
1562 int width, height;
1563 double ascent;
1565 struct glyph *glyph;
1566 enum glyph_row_area area = it->area;
1568 xassert (ascent >= 0 && ascent <= 1);
1570 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1571 if (glyph < it->glyph_row->glyphs[area + 1])
1573 glyph->charpos = CHARPOS (it->position);
1574 glyph->object = object;
1575 glyph->pixel_width = width;
1576 glyph->voffset = it->voffset;
1577 glyph->type = STRETCH_GLYPH;
1578 glyph->multibyte_p = it->multibyte_p;
1579 glyph->left_box_line_p = it->start_of_box_run_p;
1580 glyph->right_box_line_p = it->end_of_box_run_p;
1581 glyph->overlaps_vertically_p = 0;
1582 glyph->padding_p = 0;
1583 glyph->glyph_not_available_p = 0;
1584 glyph->face_id = it->face_id;
1585 glyph->u.stretch.ascent = height * ascent;
1586 glyph->u.stretch.height = height;
1587 ++it->glyph_row->used[area];
1592 /* Produce a stretch glyph for iterator IT. IT->object is the value
1593 of the glyph property displayed. The value must be a list
1594 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1595 being recognized:
1597 1. `:width WIDTH' specifies that the space should be WIDTH *
1598 canonical char width wide. WIDTH may be an integer or floating
1599 point number.
1601 2. `:relative-width FACTOR' specifies that the width of the stretch
1602 should be computed from the width of the first character having the
1603 `glyph' property, and should be FACTOR times that width.
1605 3. `:align-to HPOS' specifies that the space should be wide enough
1606 to reach HPOS, a value in canonical character units.
1608 Exactly one of the above pairs must be present.
1610 4. `:height HEIGHT' specifies that the height of the stretch produced
1611 should be HEIGHT, measured in canonical character units.
1613 5. `:relative-height FACTOR' specifies that the height of the
1614 stretch should be FACTOR times the height of the characters having
1615 the glyph property.
1617 Either none or exactly one of 4 or 5 must be present.
1619 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1620 of the stretch should be used for the ascent of the stretch.
1621 ASCENT must be in the range 0 <= ASCENT <= 100. */
1623 #define NUMVAL(X) \
1624 ((INTEGERP (X) || FLOATP (X)) \
1625 ? XFLOATINT (X) \
1626 : - 1)
1629 static void
1630 x_produce_stretch_glyph (it)
1631 struct it *it;
1633 /* (space :width WIDTH :height HEIGHT. */
1634 #if GLYPH_DEBUG
1635 extern Lisp_Object Qspace;
1636 #endif
1637 extern Lisp_Object QCwidth, QCheight, QCascent;
1638 extern Lisp_Object QCrelative_width, QCrelative_height;
1639 extern Lisp_Object QCalign_to;
1640 Lisp_Object prop, plist;
1641 double width = 0, height = 0, ascent = 0;
1642 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1643 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1645 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1647 /* List should start with `space'. */
1648 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1649 plist = XCDR (it->object);
1651 /* Compute the width of the stretch. */
1652 if (prop = Fplist_get (plist, QCwidth),
1653 NUMVAL (prop) > 0)
1654 /* Absolute width `:width WIDTH' specified and valid. */
1655 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1656 else if (prop = Fplist_get (plist, QCrelative_width),
1657 NUMVAL (prop) > 0)
1659 /* Relative width `:relative-width FACTOR' specified and valid.
1660 Compute the width of the characters having the `glyph'
1661 property. */
1662 struct it it2;
1663 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1665 it2 = *it;
1666 if (it->multibyte_p)
1668 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1669 - IT_BYTEPOS (*it));
1670 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1672 else
1673 it2.c = *p, it2.len = 1;
1675 it2.glyph_row = NULL;
1676 it2.what = IT_CHARACTER;
1677 x_produce_glyphs (&it2);
1678 width = NUMVAL (prop) * it2.pixel_width;
1680 else if (prop = Fplist_get (plist, QCalign_to),
1681 NUMVAL (prop) > 0)
1682 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1683 else
1684 /* Nothing specified -> width defaults to canonical char width. */
1685 width = CANON_X_UNIT (it->f);
1687 /* Compute height. */
1688 if (prop = Fplist_get (plist, QCheight),
1689 NUMVAL (prop) > 0)
1690 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1691 else if (prop = Fplist_get (plist, QCrelative_height),
1692 NUMVAL (prop) > 0)
1693 height = FONT_HEIGHT (font) * NUMVAL (prop);
1694 else
1695 height = FONT_HEIGHT (font);
1697 /* Compute percentage of height used for ascent. If
1698 `:ascent ASCENT' is present and valid, use that. Otherwise,
1699 derive the ascent from the font in use. */
1700 if (prop = Fplist_get (plist, QCascent),
1701 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1702 ascent = NUMVAL (prop) / 100.0;
1703 else
1704 ascent = (double) font->ascent / FONT_HEIGHT (font);
1706 if (width <= 0)
1707 width = 1;
1708 if (height <= 0)
1709 height = 1;
1711 if (it->glyph_row)
1713 Lisp_Object object = it->stack[it->sp - 1].string;
1714 if (!STRINGP (object))
1715 object = it->w->buffer;
1716 x_append_stretch_glyph (it, object, width, height, ascent);
1719 it->pixel_width = width;
1720 it->ascent = it->phys_ascent = height * ascent;
1721 it->descent = it->phys_descent = height - it->ascent;
1722 it->nglyphs = 1;
1724 if (face->box != FACE_NO_BOX)
1726 if (face->box_line_width > 0)
1728 it->ascent += face->box_line_width;
1729 it->descent += face->box_line_width;
1732 if (it->start_of_box_run_p)
1733 it->pixel_width += abs (face->box_line_width);
1734 if (it->end_of_box_run_p)
1735 it->pixel_width += abs (face->box_line_width);
1738 take_vertical_position_into_account (it);
1741 /* Return proper value to be used as baseline offset of font that has
1742 ASCENT and DESCENT to draw characters by the font at the vertical
1743 center of the line of frame F.
1745 Here, out task is to find the value of BOFF in the following figure;
1747 -------------------------+-----------+-
1748 -+-+---------+-+ | |
1749 | | | | | |
1750 | | | | F_ASCENT F_HEIGHT
1751 | | | ASCENT | |
1752 HEIGHT | | | | |
1753 | | |-|-+------+-----------|------- baseline
1754 | | | | BOFF | |
1755 | |---------|-+-+ | |
1756 | | | DESCENT | |
1757 -+-+---------+-+ F_DESCENT |
1758 -------------------------+-----------+-
1760 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1761 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1762 DESCENT = FONT->descent
1763 HEIGHT = FONT_HEIGHT (FONT)
1764 F_DESCENT = (F->output_data.x->font->descent
1765 - F->output_data.x->baseline_offset)
1766 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1769 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1770 ((FONT)->descent \
1771 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1772 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1773 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1775 /* Produce glyphs/get display metrics for the display element IT is
1776 loaded with. See the description of struct display_iterator in
1777 dispextern.h for an overview of struct display_iterator. */
1779 static void
1780 x_produce_glyphs (it)
1781 struct it *it;
1783 it->glyph_not_available_p = 0;
1785 if (it->what == IT_CHARACTER)
1787 XChar2b char2b;
1788 XFontStruct *font;
1789 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1790 XCharStruct *pcm;
1791 int font_not_found_p;
1792 struct font_info *font_info;
1793 int boff; /* baseline offset */
1794 /* We may change it->multibyte_p upon unibyte<->multibyte
1795 conversion. So, save the current value now and restore it
1796 later.
1798 Note: It seems that we don't have to record multibyte_p in
1799 struct glyph because the character code itself tells if or
1800 not the character is multibyte. Thus, in the future, we must
1801 consider eliminating the field `multibyte_p' in the struct
1802 glyph. */
1803 int saved_multibyte_p = it->multibyte_p;
1805 /* Maybe translate single-byte characters to multibyte, or the
1806 other way. */
1807 it->char_to_display = it->c;
1808 if (!ASCII_BYTE_P (it->c))
1810 if (unibyte_display_via_language_environment
1811 && SINGLE_BYTE_CHAR_P (it->c)
1812 && (it->c >= 0240
1813 || !NILP (Vnonascii_translation_table)))
1815 it->char_to_display = unibyte_char_to_multibyte (it->c);
1816 it->multibyte_p = 1;
1817 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1818 face = FACE_FROM_ID (it->f, it->face_id);
1820 else if (!SINGLE_BYTE_CHAR_P (it->c)
1821 && !it->multibyte_p)
1823 it->multibyte_p = 1;
1824 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1825 face = FACE_FROM_ID (it->f, it->face_id);
1829 /* Get font to use. Encode IT->char_to_display. */
1830 x_get_char_face_and_encoding (it->f, it->char_to_display,
1831 it->face_id, &char2b,
1832 it->multibyte_p);
1833 font = face->font;
1835 /* When no suitable font found, use the default font. */
1836 font_not_found_p = font == NULL;
1837 if (font_not_found_p)
1839 font = FRAME_FONT (it->f);
1840 boff = it->f->output_data.x->baseline_offset;
1841 font_info = NULL;
1843 else
1845 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1846 boff = font_info->baseline_offset;
1847 if (font_info->vertical_centering)
1848 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1851 if (it->char_to_display >= ' '
1852 && (!it->multibyte_p || it->char_to_display < 128))
1854 /* Either unibyte or ASCII. */
1855 int stretched_p;
1857 it->nglyphs = 1;
1859 pcm = x_per_char_metric (font, &char2b);
1860 it->ascent = font->ascent + boff;
1861 it->descent = font->descent - boff;
1863 if (pcm)
1865 it->phys_ascent = pcm->ascent + boff;
1866 it->phys_descent = pcm->descent - boff;
1867 it->pixel_width = pcm->width;
1869 else
1871 it->glyph_not_available_p = 1;
1872 it->phys_ascent = font->ascent + boff;
1873 it->phys_descent = font->descent - boff;
1874 it->pixel_width = FONT_WIDTH (font);
1877 /* If this is a space inside a region of text with
1878 `space-width' property, change its width. */
1879 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1880 if (stretched_p)
1881 it->pixel_width *= XFLOATINT (it->space_width);
1883 /* If face has a box, add the box thickness to the character
1884 height. If character has a box line to the left and/or
1885 right, add the box line width to the character's width. */
1886 if (face->box != FACE_NO_BOX)
1888 int thick = face->box_line_width;
1890 if (thick > 0)
1892 it->ascent += thick;
1893 it->descent += thick;
1895 else
1896 thick = -thick;
1898 if (it->start_of_box_run_p)
1899 it->pixel_width += thick;
1900 if (it->end_of_box_run_p)
1901 it->pixel_width += thick;
1904 /* If face has an overline, add the height of the overline
1905 (1 pixel) and a 1 pixel margin to the character height. */
1906 if (face->overline_p)
1907 it->ascent += 2;
1909 take_vertical_position_into_account (it);
1911 /* If we have to actually produce glyphs, do it. */
1912 if (it->glyph_row)
1914 if (stretched_p)
1916 /* Translate a space with a `space-width' property
1917 into a stretch glyph. */
1918 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1919 x_append_stretch_glyph (it, it->object, it->pixel_width,
1920 it->ascent + it->descent, ascent);
1922 else
1923 x_append_glyph (it);
1925 /* If characters with lbearing or rbearing are displayed
1926 in this line, record that fact in a flag of the
1927 glyph row. This is used to optimize X output code. */
1928 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1929 it->glyph_row->contains_overlapping_glyphs_p = 1;
1932 else if (it->char_to_display == '\n')
1934 /* A newline has no width but we need the height of the line. */
1935 it->pixel_width = 0;
1936 it->nglyphs = 0;
1937 it->ascent = it->phys_ascent = font->ascent + boff;
1938 it->descent = it->phys_descent = font->descent - boff;
1940 if (face->box != FACE_NO_BOX
1941 && face->box_line_width > 0)
1943 it->ascent += face->box_line_width;
1944 it->descent += face->box_line_width;
1947 else if (it->char_to_display == '\t')
1949 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1950 int x = it->current_x + it->continuation_lines_width;
1951 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1953 /* If the distance from the current position to the next tab
1954 stop is less than a canonical character width, use the
1955 tab stop after that. */
1956 if (next_tab_x - x < CANON_X_UNIT (it->f))
1957 next_tab_x += tab_width;
1959 it->pixel_width = next_tab_x - x;
1960 it->nglyphs = 1;
1961 it->ascent = it->phys_ascent = font->ascent + boff;
1962 it->descent = it->phys_descent = font->descent - boff;
1964 if (it->glyph_row)
1966 double ascent = (double) it->ascent / (it->ascent + it->descent);
1967 x_append_stretch_glyph (it, it->object, it->pixel_width,
1968 it->ascent + it->descent, ascent);
1971 else
1973 /* A multi-byte character. Assume that the display width of the
1974 character is the width of the character multiplied by the
1975 width of the font. */
1977 /* If we found a font, this font should give us the right
1978 metrics. If we didn't find a font, use the frame's
1979 default font and calculate the width of the character
1980 from the charset width; this is what old redisplay code
1981 did. */
1982 pcm = x_per_char_metric (font, &char2b);
1983 if (font_not_found_p || !pcm)
1985 int charset = CHAR_CHARSET (it->char_to_display);
1987 it->glyph_not_available_p = 1;
1988 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1989 * CHARSET_WIDTH (charset));
1990 it->phys_ascent = font->ascent + boff;
1991 it->phys_descent = font->descent - boff;
1993 else
1995 it->pixel_width = pcm->width;
1996 it->phys_ascent = pcm->ascent + boff;
1997 it->phys_descent = pcm->descent - boff;
1998 if (it->glyph_row
1999 && (pcm->lbearing < 0
2000 || pcm->rbearing > pcm->width))
2001 it->glyph_row->contains_overlapping_glyphs_p = 1;
2003 it->nglyphs = 1;
2004 it->ascent = font->ascent + boff;
2005 it->descent = font->descent - boff;
2006 if (face->box != FACE_NO_BOX)
2008 int thick = face->box_line_width;
2010 if (thick > 0)
2012 it->ascent += thick;
2013 it->descent += thick;
2015 else
2016 thick = - thick;
2018 if (it->start_of_box_run_p)
2019 it->pixel_width += thick;
2020 if (it->end_of_box_run_p)
2021 it->pixel_width += thick;
2024 /* If face has an overline, add the height of the overline
2025 (1 pixel) and a 1 pixel margin to the character height. */
2026 if (face->overline_p)
2027 it->ascent += 2;
2029 take_vertical_position_into_account (it);
2031 if (it->glyph_row)
2032 x_append_glyph (it);
2034 it->multibyte_p = saved_multibyte_p;
2036 else if (it->what == IT_COMPOSITION)
2038 /* Note: A composition is represented as one glyph in the
2039 glyph matrix. There are no padding glyphs. */
2040 XChar2b char2b;
2041 XFontStruct *font;
2042 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2043 XCharStruct *pcm;
2044 int font_not_found_p;
2045 struct font_info *font_info;
2046 int boff; /* baseline offset */
2047 struct composition *cmp = composition_table[it->cmp_id];
2049 /* Maybe translate single-byte characters to multibyte. */
2050 it->char_to_display = it->c;
2051 if (unibyte_display_via_language_environment
2052 && SINGLE_BYTE_CHAR_P (it->c)
2053 && (it->c >= 0240
2054 || (it->c >= 0200
2055 && !NILP (Vnonascii_translation_table))))
2057 it->char_to_display = unibyte_char_to_multibyte (it->c);
2060 /* Get face and font to use. Encode IT->char_to_display. */
2061 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2062 face = FACE_FROM_ID (it->f, it->face_id);
2063 x_get_char_face_and_encoding (it->f, it->char_to_display,
2064 it->face_id, &char2b, it->multibyte_p);
2065 font = face->font;
2067 /* When no suitable font found, use the default font. */
2068 font_not_found_p = font == NULL;
2069 if (font_not_found_p)
2071 font = FRAME_FONT (it->f);
2072 boff = it->f->output_data.x->baseline_offset;
2073 font_info = NULL;
2075 else
2077 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2078 boff = font_info->baseline_offset;
2079 if (font_info->vertical_centering)
2080 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2083 /* There are no padding glyphs, so there is only one glyph to
2084 produce for the composition. Important is that pixel_width,
2085 ascent and descent are the values of what is drawn by
2086 draw_glyphs (i.e. the values of the overall glyphs composed). */
2087 it->nglyphs = 1;
2089 /* If we have not yet calculated pixel size data of glyphs of
2090 the composition for the current face font, calculate them
2091 now. Theoretically, we have to check all fonts for the
2092 glyphs, but that requires much time and memory space. So,
2093 here we check only the font of the first glyph. This leads
2094 to incorrect display very rarely, and C-l (recenter) can
2095 correct the display anyway. */
2096 if (cmp->font != (void *) font)
2098 /* Ascent and descent of the font of the first character of
2099 this composition (adjusted by baseline offset). Ascent
2100 and descent of overall glyphs should not be less than
2101 them respectively. */
2102 int font_ascent = font->ascent + boff;
2103 int font_descent = font->descent - boff;
2104 /* Bounding box of the overall glyphs. */
2105 int leftmost, rightmost, lowest, highest;
2106 int i, width, ascent, descent;
2108 cmp->font = (void *) font;
2110 /* Initialize the bounding box. */
2111 if (font_info
2112 && (pcm = x_per_char_metric (font, &char2b)))
2114 width = pcm->width;
2115 ascent = pcm->ascent;
2116 descent = pcm->descent;
2118 else
2120 width = FONT_WIDTH (font);
2121 ascent = font->ascent;
2122 descent = font->descent;
2125 rightmost = width;
2126 lowest = - descent + boff;
2127 highest = ascent + boff;
2128 leftmost = 0;
2130 if (font_info
2131 && font_info->default_ascent
2132 && CHAR_TABLE_P (Vuse_default_ascent)
2133 && !NILP (Faref (Vuse_default_ascent,
2134 make_number (it->char_to_display))))
2135 highest = font_info->default_ascent + boff;
2137 /* Draw the first glyph at the normal position. It may be
2138 shifted to right later if some other glyphs are drawn at
2139 the left. */
2140 cmp->offsets[0] = 0;
2141 cmp->offsets[1] = boff;
2143 /* Set cmp->offsets for the remaining glyphs. */
2144 for (i = 1; i < cmp->glyph_len; i++)
2146 int left, right, btm, top;
2147 int ch = COMPOSITION_GLYPH (cmp, i);
2148 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2150 face = FACE_FROM_ID (it->f, face_id);
2151 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2152 it->multibyte_p);
2153 font = face->font;
2154 if (font == NULL)
2156 font = FRAME_FONT (it->f);
2157 boff = it->f->output_data.x->baseline_offset;
2158 font_info = NULL;
2160 else
2162 font_info
2163 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2164 boff = font_info->baseline_offset;
2165 if (font_info->vertical_centering)
2166 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2169 if (font_info
2170 && (pcm = x_per_char_metric (font, &char2b)))
2172 width = pcm->width;
2173 ascent = pcm->ascent;
2174 descent = pcm->descent;
2176 else
2178 width = FONT_WIDTH (font);
2179 ascent = 1;
2180 descent = 0;
2183 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2185 /* Relative composition with or without
2186 alternate chars. */
2187 left = (leftmost + rightmost - width) / 2;
2188 btm = - descent + boff;
2189 if (font_info && font_info->relative_compose
2190 && (! CHAR_TABLE_P (Vignore_relative_composition)
2191 || NILP (Faref (Vignore_relative_composition,
2192 make_number (ch)))))
2195 if (- descent >= font_info->relative_compose)
2196 /* One extra pixel between two glyphs. */
2197 btm = highest + 1;
2198 else if (ascent <= 0)
2199 /* One extra pixel between two glyphs. */
2200 btm = lowest - 1 - ascent - descent;
2203 else
2205 /* A composition rule is specified by an integer
2206 value that encodes global and new reference
2207 points (GREF and NREF). GREF and NREF are
2208 specified by numbers as below:
2210 0---1---2 -- ascent
2214 9--10--11 -- center
2216 ---3---4---5--- baseline
2218 6---7---8 -- descent
2220 int rule = COMPOSITION_RULE (cmp, i);
2221 int gref, nref, grefx, grefy, nrefx, nrefy;
2223 COMPOSITION_DECODE_RULE (rule, gref, nref);
2224 grefx = gref % 3, nrefx = nref % 3;
2225 grefy = gref / 3, nrefy = nref / 3;
2227 left = (leftmost
2228 + grefx * (rightmost - leftmost) / 2
2229 - nrefx * width / 2);
2230 btm = ((grefy == 0 ? highest
2231 : grefy == 1 ? 0
2232 : grefy == 2 ? lowest
2233 : (highest + lowest) / 2)
2234 - (nrefy == 0 ? ascent + descent
2235 : nrefy == 1 ? descent - boff
2236 : nrefy == 2 ? 0
2237 : (ascent + descent) / 2));
2240 cmp->offsets[i * 2] = left;
2241 cmp->offsets[i * 2 + 1] = btm + descent;
2243 /* Update the bounding box of the overall glyphs. */
2244 right = left + width;
2245 top = btm + descent + ascent;
2246 if (left < leftmost)
2247 leftmost = left;
2248 if (right > rightmost)
2249 rightmost = right;
2250 if (top > highest)
2251 highest = top;
2252 if (btm < lowest)
2253 lowest = btm;
2256 /* If there are glyphs whose x-offsets are negative,
2257 shift all glyphs to the right and make all x-offsets
2258 non-negative. */
2259 if (leftmost < 0)
2261 for (i = 0; i < cmp->glyph_len; i++)
2262 cmp->offsets[i * 2] -= leftmost;
2263 rightmost -= leftmost;
2266 cmp->pixel_width = rightmost;
2267 cmp->ascent = highest;
2268 cmp->descent = - lowest;
2269 if (cmp->ascent < font_ascent)
2270 cmp->ascent = font_ascent;
2271 if (cmp->descent < font_descent)
2272 cmp->descent = font_descent;
2275 it->pixel_width = cmp->pixel_width;
2276 it->ascent = it->phys_ascent = cmp->ascent;
2277 it->descent = it->phys_descent = cmp->descent;
2279 if (face->box != FACE_NO_BOX)
2281 int thick = face->box_line_width;
2283 if (thick > 0)
2285 it->ascent += thick;
2286 it->descent += thick;
2288 else
2289 thick = - thick;
2291 if (it->start_of_box_run_p)
2292 it->pixel_width += thick;
2293 if (it->end_of_box_run_p)
2294 it->pixel_width += thick;
2297 /* If face has an overline, add the height of the overline
2298 (1 pixel) and a 1 pixel margin to the character height. */
2299 if (face->overline_p)
2300 it->ascent += 2;
2302 take_vertical_position_into_account (it);
2304 if (it->glyph_row)
2305 x_append_composite_glyph (it);
2307 else if (it->what == IT_IMAGE)
2308 x_produce_image_glyph (it);
2309 else if (it->what == IT_STRETCH)
2310 x_produce_stretch_glyph (it);
2312 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2313 because this isn't true for images with `:ascent 100'. */
2314 xassert (it->ascent >= 0 && it->descent >= 0);
2315 if (it->area == TEXT_AREA)
2316 it->current_x += it->pixel_width;
2318 it->descent += it->extra_line_spacing;
2320 it->max_ascent = max (it->max_ascent, it->ascent);
2321 it->max_descent = max (it->max_descent, it->descent);
2322 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2323 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2327 /* Estimate the pixel height of the mode or top line on frame F.
2328 FACE_ID specifies what line's height to estimate. */
2331 x_estimate_mode_line_height (f, face_id)
2332 struct frame *f;
2333 enum face_id face_id;
2335 int height = FONT_HEIGHT (FRAME_FONT (f));
2337 /* This function is called so early when Emacs starts that the face
2338 cache and mode line face are not yet initialized. */
2339 if (FRAME_FACE_CACHE (f))
2341 struct face *face = FACE_FROM_ID (f, face_id);
2342 if (face)
2344 if (face->font)
2345 height = FONT_HEIGHT (face->font);
2346 if (face->box_line_width > 0)
2347 height += 2 * face->box_line_width;
2351 return height;
2355 /***********************************************************************
2356 Glyph display
2357 ***********************************************************************/
2359 /* A sequence of glyphs to be drawn in the same face.
2361 This data structure is not really completely X specific, so it
2362 could possibly, at least partially, be useful for other systems. It
2363 is currently not part of the external redisplay interface because
2364 it's not clear what other systems will need. */
2366 struct glyph_string
2368 /* X-origin of the string. */
2369 int x;
2371 /* Y-origin and y-position of the base line of this string. */
2372 int y, ybase;
2374 /* The width of the string, not including a face extension. */
2375 int width;
2377 /* The width of the string, including a face extension. */
2378 int background_width;
2380 /* The height of this string. This is the height of the line this
2381 string is drawn in, and can be different from the height of the
2382 font the string is drawn in. */
2383 int height;
2385 /* Number of pixels this string overwrites in front of its x-origin.
2386 This number is zero if the string has an lbearing >= 0; it is
2387 -lbearing, if the string has an lbearing < 0. */
2388 int left_overhang;
2390 /* Number of pixels this string overwrites past its right-most
2391 nominal x-position, i.e. x + width. Zero if the string's
2392 rbearing is <= its nominal width, rbearing - width otherwise. */
2393 int right_overhang;
2395 /* The frame on which the glyph string is drawn. */
2396 struct frame *f;
2398 /* The window on which the glyph string is drawn. */
2399 struct window *w;
2401 /* X display and window for convenience. */
2402 Display *display;
2403 Window window;
2405 /* The glyph row for which this string was built. It determines the
2406 y-origin and height of the string. */
2407 struct glyph_row *row;
2409 /* The area within row. */
2410 enum glyph_row_area area;
2412 /* Characters to be drawn, and number of characters. */
2413 XChar2b *char2b;
2414 int nchars;
2416 /* A face-override for drawing cursors, mouse face and similar. */
2417 enum draw_glyphs_face hl;
2419 /* Face in which this string is to be drawn. */
2420 struct face *face;
2422 /* Font in which this string is to be drawn. */
2423 XFontStruct *font;
2425 /* Font info for this string. */
2426 struct font_info *font_info;
2428 /* Non-null means this string describes (part of) a composition.
2429 All characters from char2b are drawn composed. */
2430 struct composition *cmp;
2432 /* Index of this glyph string's first character in the glyph
2433 definition of CMP. If this is zero, this glyph string describes
2434 the first character of a composition. */
2435 int gidx;
2437 /* 1 means this glyph strings face has to be drawn to the right end
2438 of the window's drawing area. */
2439 unsigned extends_to_end_of_line_p : 1;
2441 /* 1 means the background of this string has been drawn. */
2442 unsigned background_filled_p : 1;
2444 /* 1 means glyph string must be drawn with 16-bit functions. */
2445 unsigned two_byte_p : 1;
2447 /* 1 means that the original font determined for drawing this glyph
2448 string could not be loaded. The member `font' has been set to
2449 the frame's default font in this case. */
2450 unsigned font_not_found_p : 1;
2452 /* 1 means that the face in which this glyph string is drawn has a
2453 stipple pattern. */
2454 unsigned stippled_p : 1;
2456 /* 1 means only the foreground of this glyph string must be drawn,
2457 and we should use the physical height of the line this glyph
2458 string appears in as clip rect. */
2459 unsigned for_overlaps_p : 1;
2461 /* The GC to use for drawing this glyph string. */
2462 GC gc;
2464 /* A pointer to the first glyph in the string. This glyph
2465 corresponds to char2b[0]. Needed to draw rectangles if
2466 font_not_found_p is 1. */
2467 struct glyph *first_glyph;
2469 /* Image, if any. */
2470 struct image *img;
2472 struct glyph_string *next, *prev;
2476 #if GLYPH_DEBUG
2478 static void
2479 x_dump_glyph_string (s)
2480 struct glyph_string *s;
2482 fprintf (stderr, "glyph string\n");
2483 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2484 s->x, s->y, s->width, s->height);
2485 fprintf (stderr, " ybase = %d\n", s->ybase);
2486 fprintf (stderr, " hl = %d\n", s->hl);
2487 fprintf (stderr, " left overhang = %d, right = %d\n",
2488 s->left_overhang, s->right_overhang);
2489 fprintf (stderr, " nchars = %d\n", s->nchars);
2490 fprintf (stderr, " extends to end of line = %d\n",
2491 s->extends_to_end_of_line_p);
2492 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2493 fprintf (stderr, " bg width = %d\n", s->background_width);
2496 #endif /* GLYPH_DEBUG */
2500 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2501 struct glyph_string **,
2502 struct glyph_string *,
2503 struct glyph_string *));
2504 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2505 struct glyph_string **,
2506 struct glyph_string *,
2507 struct glyph_string *));
2508 static void x_append_glyph_string P_ ((struct glyph_string **,
2509 struct glyph_string **,
2510 struct glyph_string *));
2511 static int x_left_overwritten P_ ((struct glyph_string *));
2512 static int x_left_overwriting P_ ((struct glyph_string *));
2513 static int x_right_overwritten P_ ((struct glyph_string *));
2514 static int x_right_overwriting P_ ((struct glyph_string *));
2515 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2516 int));
2517 static void x_init_glyph_string P_ ((struct glyph_string *,
2518 XChar2b *, struct window *,
2519 struct glyph_row *,
2520 enum glyph_row_area, int,
2521 enum draw_glyphs_face));
2522 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2523 enum glyph_row_area, int, int,
2524 enum draw_glyphs_face, int));
2525 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2526 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2527 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2528 int));
2529 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2530 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2531 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2532 static void x_draw_glyph_string P_ ((struct glyph_string *));
2533 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2534 static void x_set_cursor_gc P_ ((struct glyph_string *));
2535 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2536 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2537 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2538 int *, int *));
2539 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2540 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2541 unsigned long *, double, int));
2542 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2543 double, int, unsigned long));
2544 static void x_setup_relief_colors P_ ((struct glyph_string *));
2545 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2546 static void x_draw_image_relief P_ ((struct glyph_string *));
2547 static void x_draw_image_foreground P_ ((struct glyph_string *));
2548 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2549 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2550 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2551 int, int, int));
2552 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2553 int, int, int, int, XRectangle *));
2554 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2555 int, int, int, XRectangle *));
2556 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2557 enum glyph_row_area));
2558 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2559 struct glyph_row *,
2560 enum glyph_row_area, int, int));
2562 #if GLYPH_DEBUG
2563 static void x_check_font P_ ((struct frame *, XFontStruct *));
2564 #endif
2567 /* Append the list of glyph strings with head H and tail T to the list
2568 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2570 static INLINE void
2571 x_append_glyph_string_lists (head, tail, h, t)
2572 struct glyph_string **head, **tail;
2573 struct glyph_string *h, *t;
2575 if (h)
2577 if (*head)
2578 (*tail)->next = h;
2579 else
2580 *head = h;
2581 h->prev = *tail;
2582 *tail = t;
2587 /* Prepend the list of glyph strings with head H and tail T to the
2588 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2589 result. */
2591 static INLINE void
2592 x_prepend_glyph_string_lists (head, tail, h, t)
2593 struct glyph_string **head, **tail;
2594 struct glyph_string *h, *t;
2596 if (h)
2598 if (*head)
2599 (*head)->prev = t;
2600 else
2601 *tail = t;
2602 t->next = *head;
2603 *head = h;
2608 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2609 Set *HEAD and *TAIL to the resulting list. */
2611 static INLINE void
2612 x_append_glyph_string (head, tail, s)
2613 struct glyph_string **head, **tail;
2614 struct glyph_string *s;
2616 s->next = s->prev = NULL;
2617 x_append_glyph_string_lists (head, tail, s, s);
2621 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2622 face. */
2624 static void
2625 x_set_cursor_gc (s)
2626 struct glyph_string *s;
2628 if (s->font == FRAME_FONT (s->f)
2629 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2630 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2631 && !s->cmp)
2632 s->gc = s->f->output_data.x->cursor_gc;
2633 else
2635 /* Cursor on non-default face: must merge. */
2636 XGCValues xgcv;
2637 unsigned long mask;
2639 xgcv.background = s->f->output_data.x->cursor_pixel;
2640 xgcv.foreground = s->face->background;
2642 /* If the glyph would be invisible, try a different foreground. */
2643 if (xgcv.foreground == xgcv.background)
2644 xgcv.foreground = s->face->foreground;
2645 if (xgcv.foreground == xgcv.background)
2646 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2647 if (xgcv.foreground == xgcv.background)
2648 xgcv.foreground = s->face->foreground;
2650 /* Make sure the cursor is distinct from text in this face. */
2651 if (xgcv.background == s->face->background
2652 && xgcv.foreground == s->face->foreground)
2654 xgcv.background = s->face->foreground;
2655 xgcv.foreground = s->face->background;
2658 IF_DEBUG (x_check_font (s->f, s->font));
2659 xgcv.font = s->font->fid;
2660 xgcv.graphics_exposures = False;
2661 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2663 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2664 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2665 mask, &xgcv);
2666 else
2667 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2668 = XCreateGC (s->display, s->window, mask, &xgcv);
2670 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2675 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2677 static void
2678 x_set_mouse_face_gc (s)
2679 struct glyph_string *s;
2681 int face_id;
2682 struct face *face;
2684 /* What face has to be used last for the mouse face? */
2685 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2686 face = FACE_FROM_ID (s->f, face_id);
2687 if (face == NULL)
2688 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2690 if (s->first_glyph->type == CHAR_GLYPH)
2691 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2692 else
2693 face_id = FACE_FOR_CHAR (s->f, face, 0);
2694 s->face = FACE_FROM_ID (s->f, face_id);
2695 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2697 /* If font in this face is same as S->font, use it. */
2698 if (s->font == s->face->font)
2699 s->gc = s->face->gc;
2700 else
2702 /* Otherwise construct scratch_cursor_gc with values from FACE
2703 but font FONT. */
2704 XGCValues xgcv;
2705 unsigned long mask;
2707 xgcv.background = s->face->background;
2708 xgcv.foreground = s->face->foreground;
2709 IF_DEBUG (x_check_font (s->f, s->font));
2710 xgcv.font = s->font->fid;
2711 xgcv.graphics_exposures = False;
2712 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2714 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2715 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2716 mask, &xgcv);
2717 else
2718 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2719 = XCreateGC (s->display, s->window, mask, &xgcv);
2721 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2724 xassert (s->gc != 0);
2728 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2729 Faces to use in the mode line have already been computed when the
2730 matrix was built, so there isn't much to do, here. */
2732 static INLINE void
2733 x_set_mode_line_face_gc (s)
2734 struct glyph_string *s;
2736 s->gc = s->face->gc;
2740 /* Set S->gc of glyph string S for drawing that glyph string. Set
2741 S->stippled_p to a non-zero value if the face of S has a stipple
2742 pattern. */
2744 static INLINE void
2745 x_set_glyph_string_gc (s)
2746 struct glyph_string *s;
2748 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2750 if (s->hl == DRAW_NORMAL_TEXT)
2752 s->gc = s->face->gc;
2753 s->stippled_p = s->face->stipple != 0;
2755 else if (s->hl == DRAW_INVERSE_VIDEO)
2757 x_set_mode_line_face_gc (s);
2758 s->stippled_p = s->face->stipple != 0;
2760 else if (s->hl == DRAW_CURSOR)
2762 x_set_cursor_gc (s);
2763 s->stippled_p = 0;
2765 else if (s->hl == DRAW_MOUSE_FACE)
2767 x_set_mouse_face_gc (s);
2768 s->stippled_p = s->face->stipple != 0;
2770 else if (s->hl == DRAW_IMAGE_RAISED
2771 || s->hl == DRAW_IMAGE_SUNKEN)
2773 s->gc = s->face->gc;
2774 s->stippled_p = s->face->stipple != 0;
2776 else
2778 s->gc = s->face->gc;
2779 s->stippled_p = s->face->stipple != 0;
2782 /* GC must have been set. */
2783 xassert (s->gc != 0);
2787 /* Return in *R the clipping rectangle for glyph string S. */
2789 static void
2790 x_get_glyph_string_clip_rect (s, r)
2791 struct glyph_string *s;
2792 XRectangle *r;
2794 if (s->row->full_width_p)
2796 /* Draw full-width. X coordinates are relative to S->w->left. */
2797 int canon_x = CANON_X_UNIT (s->f);
2799 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2800 r->width = XFASTINT (s->w->width) * canon_x;
2802 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2804 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2805 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2806 r->x -= width;
2809 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2811 /* Unless displaying a mode or menu bar line, which are always
2812 fully visible, clip to the visible part of the row. */
2813 if (s->w->pseudo_window_p)
2814 r->height = s->row->visible_height;
2815 else
2816 r->height = s->height;
2818 else
2820 /* This is a text line that may be partially visible. */
2821 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2822 r->width = window_box_width (s->w, s->area);
2823 r->height = s->row->visible_height;
2826 /* If S draws overlapping rows, it's sufficient to use the top and
2827 bottom of the window for clipping because this glyph string
2828 intentionally draws over other lines. */
2829 if (s->for_overlaps_p)
2831 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2832 r->height = window_text_bottom_y (s->w) - r->y;
2834 else
2836 /* Don't use S->y for clipping because it doesn't take partially
2837 visible lines into account. For example, it can be negative for
2838 partially visible lines at the top of a window. */
2839 if (!s->row->full_width_p
2840 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2841 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2842 else
2843 r->y = max (0, s->row->y);
2845 /* If drawing a tool-bar window, draw it over the internal border
2846 at the top of the window. */
2847 if (s->w == XWINDOW (s->f->tool_bar_window))
2848 r->y -= s->f->output_data.x->internal_border_width;
2851 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2855 /* Set clipping for output of glyph string S. S may be part of a mode
2856 line or menu if we don't have X toolkit support. */
2858 static INLINE void
2859 x_set_glyph_string_clipping (s)
2860 struct glyph_string *s;
2862 XRectangle r;
2863 x_get_glyph_string_clip_rect (s, &r);
2864 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2868 /* Compute left and right overhang of glyph string S. If S is a glyph
2869 string for a composition, assume overhangs don't exist. */
2871 static INLINE void
2872 x_compute_glyph_string_overhangs (s)
2873 struct glyph_string *s;
2875 if (s->cmp == NULL
2876 && s->first_glyph->type == CHAR_GLYPH)
2878 XCharStruct cs;
2879 int direction, font_ascent, font_descent;
2880 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2881 &font_ascent, &font_descent, &cs);
2882 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2883 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2888 /* Compute overhangs and x-positions for glyph string S and its
2889 predecessors, or successors. X is the starting x-position for S.
2890 BACKWARD_P non-zero means process predecessors. */
2892 static void
2893 x_compute_overhangs_and_x (s, x, backward_p)
2894 struct glyph_string *s;
2895 int x;
2896 int backward_p;
2898 if (backward_p)
2900 while (s)
2902 x_compute_glyph_string_overhangs (s);
2903 x -= s->width;
2904 s->x = x;
2905 s = s->prev;
2908 else
2910 while (s)
2912 x_compute_glyph_string_overhangs (s);
2913 s->x = x;
2914 x += s->width;
2915 s = s->next;
2921 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2922 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2923 assumed to be zero. */
2925 static void
2926 x_get_glyph_overhangs (glyph, f, left, right)
2927 struct glyph *glyph;
2928 struct frame *f;
2929 int *left, *right;
2931 *left = *right = 0;
2933 if (glyph->type == CHAR_GLYPH)
2935 XFontStruct *font;
2936 struct face *face;
2937 struct font_info *font_info;
2938 XChar2b char2b;
2939 XCharStruct *pcm;
2941 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2942 font = face->font;
2943 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2944 if (font
2945 && (pcm = x_per_char_metric (font, &char2b)))
2947 if (pcm->rbearing > pcm->width)
2948 *right = pcm->rbearing - pcm->width;
2949 if (pcm->lbearing < 0)
2950 *left = -pcm->lbearing;
2956 /* Return the index of the first glyph preceding glyph string S that
2957 is overwritten by S because of S's left overhang. Value is -1
2958 if no glyphs are overwritten. */
2960 static int
2961 x_left_overwritten (s)
2962 struct glyph_string *s;
2964 int k;
2966 if (s->left_overhang)
2968 int x = 0, i;
2969 struct glyph *glyphs = s->row->glyphs[s->area];
2970 int first = s->first_glyph - glyphs;
2972 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2973 x -= glyphs[i].pixel_width;
2975 k = i + 1;
2977 else
2978 k = -1;
2980 return k;
2984 /* Return the index of the first glyph preceding glyph string S that
2985 is overwriting S because of its right overhang. Value is -1 if no
2986 glyph in front of S overwrites S. */
2988 static int
2989 x_left_overwriting (s)
2990 struct glyph_string *s;
2992 int i, k, x;
2993 struct glyph *glyphs = s->row->glyphs[s->area];
2994 int first = s->first_glyph - glyphs;
2996 k = -1;
2997 x = 0;
2998 for (i = first - 1; i >= 0; --i)
3000 int left, right;
3001 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3002 if (x + right > 0)
3003 k = i;
3004 x -= glyphs[i].pixel_width;
3007 return k;
3011 /* Return the index of the last glyph following glyph string S that is
3012 not overwritten by S because of S's right overhang. Value is -1 if
3013 no such glyph is found. */
3015 static int
3016 x_right_overwritten (s)
3017 struct glyph_string *s;
3019 int k = -1;
3021 if (s->right_overhang)
3023 int x = 0, i;
3024 struct glyph *glyphs = s->row->glyphs[s->area];
3025 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3026 int end = s->row->used[s->area];
3028 for (i = first; i < end && s->right_overhang > x; ++i)
3029 x += glyphs[i].pixel_width;
3031 k = i;
3034 return k;
3038 /* Return the index of the last glyph following glyph string S that
3039 overwrites S because of its left overhang. Value is negative
3040 if no such glyph is found. */
3042 static int
3043 x_right_overwriting (s)
3044 struct glyph_string *s;
3046 int i, k, x;
3047 int end = s->row->used[s->area];
3048 struct glyph *glyphs = s->row->glyphs[s->area];
3049 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3051 k = -1;
3052 x = 0;
3053 for (i = first; i < end; ++i)
3055 int left, right;
3056 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3057 if (x - left < 0)
3058 k = i;
3059 x += glyphs[i].pixel_width;
3062 return k;
3066 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3068 static INLINE void
3069 x_clear_glyph_string_rect (s, x, y, w, h)
3070 struct glyph_string *s;
3071 int x, y, w, h;
3073 XGCValues xgcv;
3074 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3075 XSetForeground (s->display, s->gc, xgcv.background);
3076 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3077 XSetForeground (s->display, s->gc, xgcv.foreground);
3081 /* Draw the background of glyph_string S. If S->background_filled_p
3082 is non-zero don't draw it. FORCE_P non-zero means draw the
3083 background even if it wouldn't be drawn normally. This is used
3084 when a string preceding S draws into the background of S, or S
3085 contains the first component of a composition. */
3087 static void
3088 x_draw_glyph_string_background (s, force_p)
3089 struct glyph_string *s;
3090 int force_p;
3092 /* Nothing to do if background has already been drawn or if it
3093 shouldn't be drawn in the first place. */
3094 if (!s->background_filled_p)
3096 int box_line_width = max (s->face->box_line_width, 0);
3098 if (s->stippled_p)
3100 /* Fill background with a stipple pattern. */
3101 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3102 XFillRectangle (s->display, s->window, s->gc, s->x,
3103 s->y + box_line_width,
3104 s->background_width,
3105 s->height - 2 * box_line_width);
3106 XSetFillStyle (s->display, s->gc, FillSolid);
3107 s->background_filled_p = 1;
3109 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3110 || s->font_not_found_p
3111 || s->extends_to_end_of_line_p
3112 || force_p)
3114 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3115 s->background_width,
3116 s->height - 2 * box_line_width);
3117 s->background_filled_p = 1;
3123 /* Draw the foreground of glyph string S. */
3125 static void
3126 x_draw_glyph_string_foreground (s)
3127 struct glyph_string *s;
3129 int i, x;
3131 /* If first glyph of S has a left box line, start drawing the text
3132 of S to the right of that box line. */
3133 if (s->face->box != FACE_NO_BOX
3134 && s->first_glyph->left_box_line_p)
3135 x = s->x + abs (s->face->box_line_width);
3136 else
3137 x = s->x;
3139 /* Draw characters of S as rectangles if S's font could not be
3140 loaded. */
3141 if (s->font_not_found_p)
3143 for (i = 0; i < s->nchars; ++i)
3145 struct glyph *g = s->first_glyph + i;
3146 XDrawRectangle (s->display, s->window,
3147 s->gc, x, s->y, g->pixel_width - 1,
3148 s->height - 1);
3149 x += g->pixel_width;
3152 else
3154 char *char1b = (char *) s->char2b;
3155 int boff = s->font_info->baseline_offset;
3157 if (s->font_info->vertical_centering)
3158 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3160 /* If we can use 8-bit functions, condense S->char2b. */
3161 if (!s->two_byte_p)
3162 for (i = 0; i < s->nchars; ++i)
3163 char1b[i] = s->char2b[i].byte2;
3165 /* Draw text with XDrawString if background has already been
3166 filled. Otherwise, use XDrawImageString. (Note that
3167 XDrawImageString is usually faster than XDrawString.) Always
3168 use XDrawImageString when drawing the cursor so that there is
3169 no chance that characters under a box cursor are invisible. */
3170 if (s->for_overlaps_p
3171 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3173 /* Draw characters with 16-bit or 8-bit functions. */
3174 if (s->two_byte_p)
3175 XDrawString16 (s->display, s->window, s->gc, x,
3176 s->ybase - boff, s->char2b, s->nchars);
3177 else
3178 XDrawString (s->display, s->window, s->gc, x,
3179 s->ybase - boff, char1b, s->nchars);
3181 else
3183 if (s->two_byte_p)
3184 XDrawImageString16 (s->display, s->window, s->gc, x,
3185 s->ybase - boff, s->char2b, s->nchars);
3186 else
3187 XDrawImageString (s->display, s->window, s->gc, x,
3188 s->ybase - boff, char1b, s->nchars);
3193 /* Draw the foreground of composite glyph string S. */
3195 static void
3196 x_draw_composite_glyph_string_foreground (s)
3197 struct glyph_string *s;
3199 int i, x;
3201 /* If first glyph of S has a left box line, start drawing the text
3202 of S to the right of that box line. */
3203 if (s->face->box != FACE_NO_BOX
3204 && s->first_glyph->left_box_line_p)
3205 x = s->x + abs (s->face->box_line_width);
3206 else
3207 x = s->x;
3209 /* S is a glyph string for a composition. S->gidx is the index of
3210 the first character drawn for glyphs of this composition.
3211 S->gidx == 0 means we are drawing the very first character of
3212 this composition. */
3214 /* Draw a rectangle for the composition if the font for the very
3215 first character of the composition could not be loaded. */
3216 if (s->font_not_found_p)
3218 if (s->gidx == 0)
3219 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3220 s->width - 1, s->height - 1);
3222 else
3224 for (i = 0; i < s->nchars; i++, ++s->gidx)
3225 XDrawString16 (s->display, s->window, s->gc,
3226 x + s->cmp->offsets[s->gidx * 2],
3227 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3228 s->char2b + i, 1);
3233 #ifdef USE_X_TOOLKIT
3235 static struct frame *x_frame_of_widget P_ ((Widget));
3236 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3237 XrmValue *, XrmValue *, XtPointer *));
3238 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3239 XrmValue *, Cardinal *));
3242 /* Return the frame on which widget WIDGET is used.. Abort if frame
3243 cannot be determined. */
3245 static struct frame *
3246 x_frame_of_widget (widget)
3247 Widget widget;
3249 struct x_display_info *dpyinfo;
3250 Lisp_Object tail;
3251 struct frame *f;
3253 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3255 /* Find the top-level shell of the widget. Note that this function
3256 can be called when the widget is not yet realized, so XtWindow
3257 (widget) == 0. That's the reason we can't simply use
3258 x_any_window_to_frame. */
3259 while (!XtIsTopLevelShell (widget))
3260 widget = XtParent (widget);
3262 /* Look for a frame with that top-level widget. Allocate the color
3263 on that frame to get the right gamma correction value. */
3264 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3265 if (GC_FRAMEP (XCAR (tail))
3266 && (f = XFRAME (XCAR (tail)),
3267 (f->output_data.nothing != 1
3268 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3269 && f->output_data.x->widget == widget)
3270 return f;
3272 abort ();
3276 /* Allocate the color COLOR->pixel on the screen and display of
3277 widget WIDGET in colormap CMAP. If an exact match cannot be
3278 allocated, try the nearest color available. Value is non-zero
3279 if successful. This is called from lwlib. */
3282 x_alloc_nearest_color_for_widget (widget, cmap, color)
3283 Widget widget;
3284 Colormap cmap;
3285 XColor *color;
3287 struct frame *f = x_frame_of_widget (widget);
3288 return x_alloc_nearest_color (f, cmap, color);
3292 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3293 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3294 If this produces the same color as PIXEL, try a color where all RGB
3295 values have DELTA added. Return the allocated color in *PIXEL.
3296 DISPLAY is the X display, CMAP is the colormap to operate on.
3297 Value is non-zero if successful. */
3300 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3301 Widget widget;
3302 Display *display;
3303 Colormap cmap;
3304 unsigned long *pixel;
3305 double factor;
3306 int delta;
3308 struct frame *f = x_frame_of_widget (widget);
3309 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3313 /* Structure specifying which arguments should be passed by Xt to
3314 cvt_string_to_pixel. We want the widget's screen and colormap. */
3316 static XtConvertArgRec cvt_string_to_pixel_args[] =
3318 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3319 sizeof (Screen *)},
3320 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3321 sizeof (Colormap)}
3325 /* The address of this variable is returned by
3326 cvt_string_to_pixel. */
3328 static Pixel cvt_string_to_pixel_value;
3331 /* Convert a color name to a pixel color.
3333 DPY is the display we are working on.
3335 ARGS is an array of *NARGS XrmValue structures holding additional
3336 information about the widget for which the conversion takes place.
3337 The contents of this array are determined by the specification
3338 in cvt_string_to_pixel_args.
3340 FROM is a pointer to an XrmValue which points to the color name to
3341 convert. TO is an XrmValue in which to return the pixel color.
3343 CLOSURE_RET is a pointer to user-data, in which we record if
3344 we allocated the color or not.
3346 Value is True if successful, False otherwise. */
3348 static Boolean
3349 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3350 Display *dpy;
3351 XrmValue *args;
3352 Cardinal *nargs;
3353 XrmValue *from, *to;
3354 XtPointer *closure_ret;
3356 Screen *screen;
3357 Colormap cmap;
3358 Pixel pixel;
3359 String color_name;
3360 XColor color;
3362 if (*nargs != 2)
3364 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3365 "wrongParameters", "cvt_string_to_pixel",
3366 "XtToolkitError",
3367 "Screen and colormap args required", NULL, NULL);
3368 return False;
3371 screen = *(Screen **) args[0].addr;
3372 cmap = *(Colormap *) args[1].addr;
3373 color_name = (String) from->addr;
3375 if (strcmp (color_name, XtDefaultBackground) == 0)
3377 *closure_ret = (XtPointer) False;
3378 pixel = WhitePixelOfScreen (screen);
3380 else if (strcmp (color_name, XtDefaultForeground) == 0)
3382 *closure_ret = (XtPointer) False;
3383 pixel = BlackPixelOfScreen (screen);
3385 else if (XParseColor (dpy, cmap, color_name, &color)
3386 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3388 pixel = color.pixel;
3389 *closure_ret = (XtPointer) True;
3391 else
3393 String params[1];
3394 Cardinal nparams = 1;
3396 params[0] = color_name;
3397 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3398 "badValue", "cvt_string_to_pixel",
3399 "XtToolkitError", "Invalid color `%s'",
3400 params, &nparams);
3401 return False;
3404 if (to->addr != NULL)
3406 if (to->size < sizeof (Pixel))
3408 to->size = sizeof (Pixel);
3409 return False;
3412 *(Pixel *) to->addr = pixel;
3414 else
3416 cvt_string_to_pixel_value = pixel;
3417 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3420 to->size = sizeof (Pixel);
3421 return True;
3425 /* Free a pixel color which was previously allocated via
3426 cvt_string_to_pixel. This is registered as the destructor
3427 for this type of resource via XtSetTypeConverter.
3429 APP is the application context in which we work.
3431 TO is a pointer to an XrmValue holding the color to free.
3432 CLOSURE is the value we stored in CLOSURE_RET for this color
3433 in cvt_string_to_pixel.
3435 ARGS and NARGS are like for cvt_string_to_pixel. */
3437 static void
3438 cvt_pixel_dtor (app, to, closure, args, nargs)
3439 XtAppContext app;
3440 XrmValuePtr to;
3441 XtPointer closure;
3442 XrmValuePtr args;
3443 Cardinal *nargs;
3445 if (*nargs != 2)
3447 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3448 "XtToolkitError",
3449 "Screen and colormap arguments required",
3450 NULL, NULL);
3452 else if (closure != NULL)
3454 /* We did allocate the pixel, so free it. */
3455 Screen *screen = *(Screen **) args[0].addr;
3456 Colormap cmap = *(Colormap *) args[1].addr;
3457 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3458 (Pixel *) to->addr, 1);
3463 #endif /* USE_X_TOOLKIT */
3466 /* Value is an array of XColor structures for the contents of the
3467 color map of display DPY. Set *NCELLS to the size of the array.
3468 Note that this probably shouldn't be called for large color maps,
3469 say a 24-bit TrueColor map. */
3471 static const XColor *
3472 x_color_cells (dpy, ncells)
3473 Display *dpy;
3474 int *ncells;
3476 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3478 if (dpyinfo->color_cells == NULL)
3480 Screen *screen = dpyinfo->screen;
3481 int i;
3483 dpyinfo->ncolor_cells
3484 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3485 dpyinfo->color_cells
3486 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3487 * sizeof *dpyinfo->color_cells);
3489 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3490 dpyinfo->color_cells[i].pixel = i;
3492 XQueryColors (dpy, dpyinfo->cmap,
3493 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3496 *ncells = dpyinfo->ncolor_cells;
3497 return dpyinfo->color_cells;
3501 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3502 colors in COLORS. Use cached information, if available. */
3504 void
3505 x_query_colors (f, colors, ncolors)
3506 struct frame *f;
3507 XColor *colors;
3508 int ncolors;
3510 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3512 if (dpyinfo->color_cells)
3514 int i;
3515 for (i = 0; i < ncolors; ++i)
3517 unsigned long pixel = colors[i].pixel;
3518 xassert (pixel < dpyinfo->ncolor_cells);
3519 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3520 colors[i] = dpyinfo->color_cells[pixel];
3523 else
3524 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3528 /* On frame F, translate pixel color to RGB values for the color in
3529 COLOR. Use cached information, if available. */
3531 void
3532 x_query_color (f, color)
3533 struct frame *f;
3534 XColor *color;
3536 x_query_colors (f, color, 1);
3540 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3541 exact match can't be allocated, try the nearest color available.
3542 Value is non-zero if successful. Set *COLOR to the color
3543 allocated. */
3545 static int
3546 x_alloc_nearest_color_1 (dpy, cmap, color)
3547 Display *dpy;
3548 Colormap cmap;
3549 XColor *color;
3551 int rc;
3553 rc = XAllocColor (dpy, cmap, color);
3554 if (rc == 0)
3556 /* If we got to this point, the colormap is full, so we're going
3557 to try to get the next closest color. The algorithm used is
3558 a least-squares matching, which is what X uses for closest
3559 color matching with StaticColor visuals. */
3560 int nearest, i;
3561 unsigned long nearest_delta = ~0;
3562 int ncells;
3563 const XColor *cells = x_color_cells (dpy, &ncells);
3565 for (nearest = i = 0; i < ncells; ++i)
3567 long dred = (color->red >> 8) - (cells[i].red >> 8);
3568 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3569 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3570 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3572 if (delta < nearest_delta)
3574 nearest = i;
3575 nearest_delta = delta;
3579 color->red = cells[nearest].red;
3580 color->green = cells[nearest].green;
3581 color->blue = cells[nearest].blue;
3582 rc = XAllocColor (dpy, cmap, color);
3584 else
3586 /* If allocation succeeded, and the allocated pixel color is not
3587 equal to a cached pixel color recorded earlier, there was a
3588 change in the colormap, so clear the color cache. */
3589 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3590 XColor *cached_color;
3592 if (dpyinfo->color_cells
3593 && (cached_color = &dpyinfo->color_cells[color->pixel],
3594 (cached_color->red != color->red
3595 || cached_color->blue != color->blue
3596 || cached_color->green != color->green)))
3598 xfree (dpyinfo->color_cells);
3599 dpyinfo->color_cells = NULL;
3600 dpyinfo->ncolor_cells = 0;
3604 #ifdef DEBUG_X_COLORS
3605 if (rc)
3606 register_color (color->pixel);
3607 #endif /* DEBUG_X_COLORS */
3609 return rc;
3613 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3614 exact match can't be allocated, try the nearest color available.
3615 Value is non-zero if successful. Set *COLOR to the color
3616 allocated. */
3619 x_alloc_nearest_color (f, cmap, color)
3620 struct frame *f;
3621 Colormap cmap;
3622 XColor *color;
3624 gamma_correct (f, color);
3625 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3629 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3630 It's necessary to do this instead of just using PIXEL directly to
3631 get color reference counts right. */
3633 unsigned long
3634 x_copy_color (f, pixel)
3635 struct frame *f;
3636 unsigned long pixel;
3638 XColor color;
3640 color.pixel = pixel;
3641 BLOCK_INPUT;
3642 x_query_color (f, &color);
3643 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3644 UNBLOCK_INPUT;
3645 #ifdef DEBUG_X_COLORS
3646 register_color (pixel);
3647 #endif
3648 return color.pixel;
3652 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3653 It's necessary to do this instead of just using PIXEL directly to
3654 get color reference counts right. */
3656 unsigned long
3657 x_copy_dpy_color (dpy, cmap, pixel)
3658 Display *dpy;
3659 Colormap cmap;
3660 unsigned long pixel;
3662 XColor color;
3664 color.pixel = pixel;
3665 BLOCK_INPUT;
3666 XQueryColor (dpy, cmap, &color);
3667 XAllocColor (dpy, cmap, &color);
3668 UNBLOCK_INPUT;
3669 #ifdef DEBUG_X_COLORS
3670 register_color (pixel);
3671 #endif
3672 return color.pixel;
3676 /* Brightness beyond which a color won't have its highlight brightness
3677 boosted.
3679 Nominally, highlight colors for `3d' faces are calculated by
3680 brightening an object's color by a constant scale factor, but this
3681 doesn't yield good results for dark colors, so for colors who's
3682 brightness is less than this value (on a scale of 0-65535) have an
3683 use an additional additive factor.
3685 The value here is set so that the default menu-bar/mode-line color
3686 (grey75) will not have its highlights changed at all. */
3687 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3690 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3691 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3692 If this produces the same color as PIXEL, try a color where all RGB
3693 values have DELTA added. Return the allocated color in *PIXEL.
3694 DISPLAY is the X display, CMAP is the colormap to operate on.
3695 Value is non-zero if successful. */
3697 static int
3698 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3699 struct frame *f;
3700 Display *display;
3701 Colormap cmap;
3702 unsigned long *pixel;
3703 double factor;
3704 int delta;
3706 XColor color, new;
3707 long bright;
3708 int success_p;
3710 /* Get RGB color values. */
3711 color.pixel = *pixel;
3712 x_query_color (f, &color);
3714 /* Change RGB values by specified FACTOR. Avoid overflow! */
3715 xassert (factor >= 0);
3716 new.red = min (0xffff, factor * color.red);
3717 new.green = min (0xffff, factor * color.green);
3718 new.blue = min (0xffff, factor * color.blue);
3720 /* Calculate brightness of COLOR. */
3721 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3723 /* We only boost colors that are darker than
3724 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3725 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3726 /* Make an additive adjustment to NEW, because it's dark enough so
3727 that scaling by FACTOR alone isn't enough. */
3729 /* How far below the limit this color is (0 - 1, 1 being darker). */
3730 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3731 /* The additive adjustment. */
3732 int min_delta = delta * dimness * factor / 2;
3734 if (factor < 1)
3736 new.red = max (0, new.red - min_delta);
3737 new.green = max (0, new.green - min_delta);
3738 new.blue = max (0, new.blue - min_delta);
3740 else
3742 new.red = min (0xffff, min_delta + new.red);
3743 new.green = min (0xffff, min_delta + new.green);
3744 new.blue = min (0xffff, min_delta + new.blue);
3748 /* Try to allocate the color. */
3749 success_p = x_alloc_nearest_color (f, cmap, &new);
3750 if (success_p)
3752 if (new.pixel == *pixel)
3754 /* If we end up with the same color as before, try adding
3755 delta to the RGB values. */
3756 x_free_colors (f, &new.pixel, 1);
3758 new.red = min (0xffff, delta + color.red);
3759 new.green = min (0xffff, delta + color.green);
3760 new.blue = min (0xffff, delta + color.blue);
3761 success_p = x_alloc_nearest_color (f, cmap, &new);
3763 else
3764 success_p = 1;
3765 *pixel = new.pixel;
3768 return success_p;
3772 /* Set up the foreground color for drawing relief lines of glyph
3773 string S. RELIEF is a pointer to a struct relief containing the GC
3774 with which lines will be drawn. Use a color that is FACTOR or
3775 DELTA lighter or darker than the relief's background which is found
3776 in S->f->output_data.x->relief_background. If such a color cannot
3777 be allocated, use DEFAULT_PIXEL, instead. */
3779 static void
3780 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3781 struct frame *f;
3782 struct relief *relief;
3783 double factor;
3784 int delta;
3785 unsigned long default_pixel;
3787 XGCValues xgcv;
3788 struct x_output *di = f->output_data.x;
3789 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3790 unsigned long pixel;
3791 unsigned long background = di->relief_background;
3792 Colormap cmap = FRAME_X_COLORMAP (f);
3793 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3794 Display *dpy = FRAME_X_DISPLAY (f);
3796 xgcv.graphics_exposures = False;
3797 xgcv.line_width = 1;
3799 /* Free previously allocated color. The color cell will be reused
3800 when it has been freed as many times as it was allocated, so this
3801 doesn't affect faces using the same colors. */
3802 if (relief->gc
3803 && relief->allocated_p)
3805 x_free_colors (f, &relief->pixel, 1);
3806 relief->allocated_p = 0;
3809 /* Allocate new color. */
3810 xgcv.foreground = default_pixel;
3811 pixel = background;
3812 if (dpyinfo->n_planes != 1
3813 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3815 relief->allocated_p = 1;
3816 xgcv.foreground = relief->pixel = pixel;
3819 if (relief->gc == 0)
3821 xgcv.stipple = dpyinfo->gray;
3822 mask |= GCStipple;
3823 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3825 else
3826 XChangeGC (dpy, relief->gc, mask, &xgcv);
3830 /* Set up colors for the relief lines around glyph string S. */
3832 static void
3833 x_setup_relief_colors (s)
3834 struct glyph_string *s;
3836 struct x_output *di = s->f->output_data.x;
3837 unsigned long color;
3839 if (s->face->use_box_color_for_shadows_p)
3840 color = s->face->box_color;
3841 else if (s->first_glyph->type == IMAGE_GLYPH
3842 && s->img->pixmap
3843 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3844 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3845 else
3847 XGCValues xgcv;
3849 /* Get the background color of the face. */
3850 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3851 color = xgcv.background;
3854 if (di->white_relief.gc == 0
3855 || color != di->relief_background)
3857 di->relief_background = color;
3858 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3859 WHITE_PIX_DEFAULT (s->f));
3860 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3861 BLACK_PIX_DEFAULT (s->f));
3866 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3867 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3868 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3869 relief. LEFT_P non-zero means draw a relief on the left side of
3870 the rectangle. RIGHT_P non-zero means draw a relief on the right
3871 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3872 when drawing. */
3874 static void
3875 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3876 raised_p, left_p, right_p, clip_rect)
3877 struct frame *f;
3878 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3879 XRectangle *clip_rect;
3881 Display *dpy = FRAME_X_DISPLAY (f);
3882 Window window = FRAME_X_WINDOW (f);
3883 int i;
3884 GC gc;
3886 if (raised_p)
3887 gc = f->output_data.x->white_relief.gc;
3888 else
3889 gc = f->output_data.x->black_relief.gc;
3890 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3892 /* Top. */
3893 for (i = 0; i < width; ++i)
3894 XDrawLine (dpy, window, gc,
3895 left_x + i * left_p, top_y + i,
3896 right_x + 1 - i * right_p, top_y + i);
3898 /* Left. */
3899 if (left_p)
3900 for (i = 0; i < width; ++i)
3901 XDrawLine (dpy, window, gc,
3902 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3904 XSetClipMask (dpy, gc, None);
3905 if (raised_p)
3906 gc = f->output_data.x->black_relief.gc;
3907 else
3908 gc = f->output_data.x->white_relief.gc;
3909 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3911 /* Bottom. */
3912 for (i = 0; i < width; ++i)
3913 XDrawLine (dpy, window, gc,
3914 left_x + i * left_p, bottom_y - i,
3915 right_x + 1 - i * right_p, bottom_y - i);
3917 /* Right. */
3918 if (right_p)
3919 for (i = 0; i < width; ++i)
3920 XDrawLine (dpy, window, gc,
3921 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3923 XSetClipMask (dpy, gc, None);
3927 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3928 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3929 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3930 left side of the rectangle. RIGHT_P non-zero means draw a line
3931 on the right side of the rectangle. CLIP_RECT is the clipping
3932 rectangle to use when drawing. */
3934 static void
3935 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3936 left_p, right_p, clip_rect)
3937 struct glyph_string *s;
3938 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3939 XRectangle *clip_rect;
3941 XGCValues xgcv;
3943 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3944 XSetForeground (s->display, s->gc, s->face->box_color);
3945 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3947 /* Top. */
3948 XFillRectangle (s->display, s->window, s->gc,
3949 left_x, top_y, right_x - left_x + 1, width);
3951 /* Left. */
3952 if (left_p)
3953 XFillRectangle (s->display, s->window, s->gc,
3954 left_x, top_y, width, bottom_y - top_y + 1);
3956 /* Bottom. */
3957 XFillRectangle (s->display, s->window, s->gc,
3958 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3960 /* Right. */
3961 if (right_p)
3962 XFillRectangle (s->display, s->window, s->gc,
3963 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3965 XSetForeground (s->display, s->gc, xgcv.foreground);
3966 XSetClipMask (s->display, s->gc, None);
3970 /* Draw a box around glyph string S. */
3972 static void
3973 x_draw_glyph_string_box (s)
3974 struct glyph_string *s;
3976 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3977 int left_p, right_p;
3978 struct glyph *last_glyph;
3979 XRectangle clip_rect;
3981 last_x = window_box_right (s->w, s->area);
3982 if (s->row->full_width_p
3983 && !s->w->pseudo_window_p)
3985 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
3986 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3987 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3990 /* The glyph that may have a right box line. */
3991 last_glyph = (s->cmp || s->img
3992 ? s->first_glyph
3993 : s->first_glyph + s->nchars - 1);
3995 width = abs (s->face->box_line_width);
3996 raised_p = s->face->box == FACE_RAISED_BOX;
3997 left_x = s->x;
3998 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
3999 ? last_x - 1
4000 : min (last_x, s->x + s->background_width) - 1);
4001 top_y = s->y;
4002 bottom_y = top_y + s->height - 1;
4004 left_p = (s->first_glyph->left_box_line_p
4005 || (s->hl == DRAW_MOUSE_FACE
4006 && (s->prev == NULL
4007 || s->prev->hl != s->hl)));
4008 right_p = (last_glyph->right_box_line_p
4009 || (s->hl == DRAW_MOUSE_FACE
4010 && (s->next == NULL
4011 || s->next->hl != s->hl)));
4013 x_get_glyph_string_clip_rect (s, &clip_rect);
4015 if (s->face->box == FACE_SIMPLE_BOX)
4016 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4017 left_p, right_p, &clip_rect);
4018 else
4020 x_setup_relief_colors (s);
4021 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4022 width, raised_p, left_p, right_p, &clip_rect);
4027 /* Draw foreground of image glyph string S. */
4029 static void
4030 x_draw_image_foreground (s)
4031 struct glyph_string *s;
4033 int x;
4034 int y = s->ybase - image_ascent (s->img, s->face);
4036 /* If first glyph of S has a left box line, start drawing it to the
4037 right of that line. */
4038 if (s->face->box != FACE_NO_BOX
4039 && s->first_glyph->left_box_line_p)
4040 x = s->x + abs (s->face->box_line_width);
4041 else
4042 x = s->x;
4044 /* If there is a margin around the image, adjust x- and y-position
4045 by that margin. */
4046 x += s->img->hmargin;
4047 y += s->img->vmargin;
4049 if (s->img->pixmap)
4051 if (s->img->mask)
4053 /* We can't set both a clip mask and use XSetClipRectangles
4054 because the latter also sets a clip mask. We also can't
4055 trust on the shape extension to be available
4056 (XShapeCombineRegion). So, compute the rectangle to draw
4057 manually. */
4058 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4059 | GCFunction);
4060 XGCValues xgcv;
4061 XRectangle clip_rect, image_rect, r;
4063 xgcv.clip_mask = s->img->mask;
4064 xgcv.clip_x_origin = x;
4065 xgcv.clip_y_origin = y;
4066 xgcv.function = GXcopy;
4067 XChangeGC (s->display, s->gc, mask, &xgcv);
4069 x_get_glyph_string_clip_rect (s, &clip_rect);
4070 image_rect.x = x;
4071 image_rect.y = y;
4072 image_rect.width = s->img->width;
4073 image_rect.height = s->img->height;
4074 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4075 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4076 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4078 else
4080 XRectangle clip_rect, image_rect, r;
4082 x_get_glyph_string_clip_rect (s, &clip_rect);
4083 image_rect.x = x;
4084 image_rect.y = y;
4085 image_rect.width = s->img->width;
4086 image_rect.height = s->img->height;
4087 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4088 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4089 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4091 /* When the image has a mask, we can expect that at
4092 least part of a mouse highlight or a block cursor will
4093 be visible. If the image doesn't have a mask, make
4094 a block cursor visible by drawing a rectangle around
4095 the image. I believe it's looking better if we do
4096 nothing here for mouse-face. */
4097 if (s->hl == DRAW_CURSOR)
4098 XDrawRectangle (s->display, s->window, s->gc, x, y,
4099 s->img->width - 1, s->img->height - 1);
4102 else
4103 /* Draw a rectangle if image could not be loaded. */
4104 XDrawRectangle (s->display, s->window, s->gc, x, y,
4105 s->img->width - 1, s->img->height - 1);
4109 /* Draw a relief around the image glyph string S. */
4111 static void
4112 x_draw_image_relief (s)
4113 struct glyph_string *s;
4115 int x0, y0, x1, y1, thick, raised_p;
4116 XRectangle r;
4117 int x;
4118 int y = s->ybase - image_ascent (s->img, s->face);
4120 /* If first glyph of S has a left box line, start drawing it to the
4121 right of that line. */
4122 if (s->face->box != FACE_NO_BOX
4123 && s->first_glyph->left_box_line_p)
4124 x = s->x + abs (s->face->box_line_width);
4125 else
4126 x = s->x;
4128 /* If there is a margin around the image, adjust x- and y-position
4129 by that margin. */
4130 x += s->img->hmargin;
4131 y += s->img->vmargin;
4133 if (s->hl == DRAW_IMAGE_SUNKEN
4134 || s->hl == DRAW_IMAGE_RAISED)
4136 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4137 raised_p = s->hl == DRAW_IMAGE_RAISED;
4139 else
4141 thick = abs (s->img->relief);
4142 raised_p = s->img->relief > 0;
4145 x0 = x - thick;
4146 y0 = y - thick;
4147 x1 = x + s->img->width + thick - 1;
4148 y1 = y + s->img->height + thick - 1;
4150 x_setup_relief_colors (s);
4151 x_get_glyph_string_clip_rect (s, &r);
4152 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4156 /* Draw the foreground of image glyph string S to PIXMAP. */
4158 static void
4159 x_draw_image_foreground_1 (s, pixmap)
4160 struct glyph_string *s;
4161 Pixmap pixmap;
4163 int x;
4164 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4166 /* If first glyph of S has a left box line, start drawing it to the
4167 right of that line. */
4168 if (s->face->box != FACE_NO_BOX
4169 && s->first_glyph->left_box_line_p)
4170 x = abs (s->face->box_line_width);
4171 else
4172 x = 0;
4174 /* If there is a margin around the image, adjust x- and y-position
4175 by that margin. */
4176 x += s->img->hmargin;
4177 y += s->img->vmargin;
4179 if (s->img->pixmap)
4181 if (s->img->mask)
4183 /* We can't set both a clip mask and use XSetClipRectangles
4184 because the latter also sets a clip mask. We also can't
4185 trust on the shape extension to be available
4186 (XShapeCombineRegion). So, compute the rectangle to draw
4187 manually. */
4188 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4189 | GCFunction);
4190 XGCValues xgcv;
4192 xgcv.clip_mask = s->img->mask;
4193 xgcv.clip_x_origin = x;
4194 xgcv.clip_y_origin = y;
4195 xgcv.function = GXcopy;
4196 XChangeGC (s->display, s->gc, mask, &xgcv);
4198 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4199 0, 0, s->img->width, s->img->height, x, y);
4200 XSetClipMask (s->display, s->gc, None);
4202 else
4204 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4205 0, 0, s->img->width, s->img->height, x, y);
4207 /* When the image has a mask, we can expect that at
4208 least part of a mouse highlight or a block cursor will
4209 be visible. If the image doesn't have a mask, make
4210 a block cursor visible by drawing a rectangle around
4211 the image. I believe it's looking better if we do
4212 nothing here for mouse-face. */
4213 if (s->hl == DRAW_CURSOR)
4214 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4215 s->img->width - 1, s->img->height - 1);
4218 else
4219 /* Draw a rectangle if image could not be loaded. */
4220 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4221 s->img->width - 1, s->img->height - 1);
4225 /* Draw part of the background of glyph string S. X, Y, W, and H
4226 give the rectangle to draw. */
4228 static void
4229 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4230 struct glyph_string *s;
4231 int x, y, w, h;
4233 if (s->stippled_p)
4235 /* Fill background with a stipple pattern. */
4236 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4237 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4238 XSetFillStyle (s->display, s->gc, FillSolid);
4240 else
4241 x_clear_glyph_string_rect (s, x, y, w, h);
4245 /* Draw image glyph string S.
4247 s->y
4248 s->x +-------------------------
4249 | s->face->box
4251 | +-------------------------
4252 | | s->img->margin
4254 | | +-------------------
4255 | | | the image
4259 static void
4260 x_draw_image_glyph_string (s)
4261 struct glyph_string *s;
4263 int x, y;
4264 int box_line_hwidth = abs (s->face->box_line_width);
4265 int box_line_vwidth = max (s->face->box_line_width, 0);
4266 int height;
4267 Pixmap pixmap = None;
4269 height = s->height - 2 * box_line_vwidth;
4271 /* Fill background with face under the image. Do it only if row is
4272 taller than image or if image has a clip mask to reduce
4273 flickering. */
4274 s->stippled_p = s->face->stipple != 0;
4275 if (height > s->img->height
4276 || s->img->hmargin
4277 || s->img->vmargin
4278 || s->img->mask
4279 || s->img->pixmap == 0
4280 || s->width != s->background_width)
4282 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4283 x = s->x + box_line_hwidth;
4284 else
4285 x = s->x;
4287 y = s->y + box_line_vwidth;
4289 if (s->img->mask)
4291 /* Create a pixmap as large as the glyph string. Fill it
4292 with the background color. Copy the image to it, using
4293 its mask. Copy the temporary pixmap to the display. */
4294 Screen *screen = FRAME_X_SCREEN (s->f);
4295 int depth = DefaultDepthOfScreen (screen);
4297 /* Create a pixmap as large as the glyph string. */
4298 pixmap = XCreatePixmap (s->display, s->window,
4299 s->background_width,
4300 s->height, depth);
4302 /* Don't clip in the following because we're working on the
4303 pixmap. */
4304 XSetClipMask (s->display, s->gc, None);
4306 /* Fill the pixmap with the background color/stipple. */
4307 if (s->stippled_p)
4309 /* Fill background with a stipple pattern. */
4310 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4311 XFillRectangle (s->display, pixmap, s->gc,
4312 0, 0, s->background_width, s->height);
4313 XSetFillStyle (s->display, s->gc, FillSolid);
4315 else
4317 XGCValues xgcv;
4318 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4319 &xgcv);
4320 XSetForeground (s->display, s->gc, xgcv.background);
4321 XFillRectangle (s->display, pixmap, s->gc,
4322 0, 0, s->background_width, s->height);
4323 XSetForeground (s->display, s->gc, xgcv.foreground);
4326 else
4327 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4329 s->background_filled_p = 1;
4332 /* Draw the foreground. */
4333 if (pixmap != None)
4335 x_draw_image_foreground_1 (s, pixmap);
4336 x_set_glyph_string_clipping (s);
4337 XCopyArea (s->display, pixmap, s->window, s->gc,
4338 0, 0, s->background_width, s->height, s->x, s->y);
4339 XFreePixmap (s->display, pixmap);
4341 else
4342 x_draw_image_foreground (s);
4344 /* If we must draw a relief around the image, do it. */
4345 if (s->img->relief
4346 || s->hl == DRAW_IMAGE_RAISED
4347 || s->hl == DRAW_IMAGE_SUNKEN)
4348 x_draw_image_relief (s);
4352 /* Draw stretch glyph string S. */
4354 static void
4355 x_draw_stretch_glyph_string (s)
4356 struct glyph_string *s;
4358 xassert (s->first_glyph->type == STRETCH_GLYPH);
4359 s->stippled_p = s->face->stipple != 0;
4361 if (s->hl == DRAW_CURSOR
4362 && !x_stretch_cursor_p)
4364 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4365 as wide as the stretch glyph. */
4366 int width = min (CANON_X_UNIT (s->f), s->background_width);
4368 /* Draw cursor. */
4369 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4371 /* Clear rest using the GC of the original non-cursor face. */
4372 if (width < s->background_width)
4374 int x = s->x + width, y = s->y;
4375 int w = s->background_width - width, h = s->height;
4376 XRectangle r;
4377 GC gc;
4379 if (s->row->mouse_face_p
4380 && cursor_in_mouse_face_p (s->w))
4382 x_set_mouse_face_gc (s);
4383 gc = s->gc;
4385 else
4386 gc = s->face->gc;
4388 x_get_glyph_string_clip_rect (s, &r);
4389 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4391 if (s->face->stipple)
4393 /* Fill background with a stipple pattern. */
4394 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4395 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4396 XSetFillStyle (s->display, gc, FillSolid);
4398 else
4400 XGCValues xgcv;
4401 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4402 XSetForeground (s->display, gc, xgcv.background);
4403 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4404 XSetForeground (s->display, gc, xgcv.foreground);
4408 else if (!s->background_filled_p)
4409 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4410 s->height);
4412 s->background_filled_p = 1;
4416 /* Draw glyph string S. */
4418 static void
4419 x_draw_glyph_string (s)
4420 struct glyph_string *s;
4422 int relief_drawn_p = 0;
4424 /* If S draws into the background of its successor, draw the
4425 background of the successor first so that S can draw into it.
4426 This makes S->next use XDrawString instead of XDrawImageString. */
4427 if (s->next && s->right_overhang && !s->for_overlaps_p)
4429 xassert (s->next->img == NULL);
4430 x_set_glyph_string_gc (s->next);
4431 x_set_glyph_string_clipping (s->next);
4432 x_draw_glyph_string_background (s->next, 1);
4435 /* Set up S->gc, set clipping and draw S. */
4436 x_set_glyph_string_gc (s);
4438 /* Draw relief (if any) in advance for char/composition so that the
4439 glyph string can be drawn over it. */
4440 if (!s->for_overlaps_p
4441 && s->face->box != FACE_NO_BOX
4442 && (s->first_glyph->type == CHAR_GLYPH
4443 || s->first_glyph->type == COMPOSITE_GLYPH))
4446 x_set_glyph_string_clipping (s);
4447 x_draw_glyph_string_background (s, 1);
4448 x_draw_glyph_string_box (s);
4449 x_set_glyph_string_clipping (s);
4450 relief_drawn_p = 1;
4452 else
4453 x_set_glyph_string_clipping (s);
4455 switch (s->first_glyph->type)
4457 case IMAGE_GLYPH:
4458 x_draw_image_glyph_string (s);
4459 break;
4461 case STRETCH_GLYPH:
4462 x_draw_stretch_glyph_string (s);
4463 break;
4465 case CHAR_GLYPH:
4466 if (s->for_overlaps_p)
4467 s->background_filled_p = 1;
4468 else
4469 x_draw_glyph_string_background (s, 0);
4470 x_draw_glyph_string_foreground (s);
4471 break;
4473 case COMPOSITE_GLYPH:
4474 if (s->for_overlaps_p || s->gidx > 0)
4475 s->background_filled_p = 1;
4476 else
4477 x_draw_glyph_string_background (s, 1);
4478 x_draw_composite_glyph_string_foreground (s);
4479 break;
4481 default:
4482 abort ();
4485 if (!s->for_overlaps_p)
4487 /* Draw underline. */
4488 if (s->face->underline_p)
4490 unsigned long tem, h;
4491 int y;
4493 /* Get the underline thickness. Default is 1 pixel. */
4494 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4495 h = 1;
4497 /* Get the underline position. This is the recommended
4498 vertical offset in pixels from the baseline to the top of
4499 the underline. This is a signed value according to the
4500 specs, and its default is
4502 ROUND ((maximum descent) / 2), with
4503 ROUND(x) = floor (x + 0.5) */
4505 if (x_use_underline_position_properties
4506 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4507 y = s->ybase + (long) tem;
4508 else if (s->face->font)
4509 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4510 else
4511 y = s->y + s->height - h;
4513 if (s->face->underline_defaulted_p)
4514 XFillRectangle (s->display, s->window, s->gc,
4515 s->x, y, s->width, h);
4516 else
4518 XGCValues xgcv;
4519 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4520 XSetForeground (s->display, s->gc, s->face->underline_color);
4521 XFillRectangle (s->display, s->window, s->gc,
4522 s->x, y, s->width, h);
4523 XSetForeground (s->display, s->gc, xgcv.foreground);
4527 /* Draw overline. */
4528 if (s->face->overline_p)
4530 unsigned long dy = 0, h = 1;
4532 if (s->face->overline_color_defaulted_p)
4533 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4534 s->width, h);
4535 else
4537 XGCValues xgcv;
4538 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4539 XSetForeground (s->display, s->gc, s->face->overline_color);
4540 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4541 s->width, h);
4542 XSetForeground (s->display, s->gc, xgcv.foreground);
4546 /* Draw strike-through. */
4547 if (s->face->strike_through_p)
4549 unsigned long h = 1;
4550 unsigned long dy = (s->height - h) / 2;
4552 if (s->face->strike_through_color_defaulted_p)
4553 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4554 s->width, h);
4555 else
4557 XGCValues xgcv;
4558 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4559 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4560 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4561 s->width, h);
4562 XSetForeground (s->display, s->gc, xgcv.foreground);
4566 /* Draw relief if not yet drawn. */
4567 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4568 x_draw_glyph_string_box (s);
4571 /* Reset clipping. */
4572 XSetClipMask (s->display, s->gc, None);
4576 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4577 struct face **, int));
4580 /* Fill glyph string S with composition components specified by S->cmp.
4582 FACES is an array of faces for all components of this composition.
4583 S->gidx is the index of the first component for S.
4584 OVERLAPS_P non-zero means S should draw the foreground only, and
4585 use its physical height for clipping.
4587 Value is the index of a component not in S. */
4589 static int
4590 x_fill_composite_glyph_string (s, faces, overlaps_p)
4591 struct glyph_string *s;
4592 struct face **faces;
4593 int overlaps_p;
4595 int i;
4597 xassert (s);
4599 s->for_overlaps_p = overlaps_p;
4601 s->face = faces[s->gidx];
4602 s->font = s->face->font;
4603 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4605 /* For all glyphs of this composition, starting at the offset
4606 S->gidx, until we reach the end of the definition or encounter a
4607 glyph that requires the different face, add it to S. */
4608 ++s->nchars;
4609 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4610 ++s->nchars;
4612 /* All glyph strings for the same composition has the same width,
4613 i.e. the width set for the first component of the composition. */
4615 s->width = s->first_glyph->pixel_width;
4617 /* If the specified font could not be loaded, use the frame's
4618 default font, but record the fact that we couldn't load it in
4619 the glyph string so that we can draw rectangles for the
4620 characters of the glyph string. */
4621 if (s->font == NULL)
4623 s->font_not_found_p = 1;
4624 s->font = FRAME_FONT (s->f);
4627 /* Adjust base line for subscript/superscript text. */
4628 s->ybase += s->first_glyph->voffset;
4630 xassert (s->face && s->face->gc);
4632 /* This glyph string must always be drawn with 16-bit functions. */
4633 s->two_byte_p = 1;
4635 return s->gidx + s->nchars;
4639 /* Fill glyph string S from a sequence of character glyphs.
4641 FACE_ID is the face id of the string. START is the index of the
4642 first glyph to consider, END is the index of the last + 1.
4643 OVERLAPS_P non-zero means S should draw the foreground only, and
4644 use its physical height for clipping.
4646 Value is the index of the first glyph not in S. */
4648 static int
4649 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4650 struct glyph_string *s;
4651 int face_id;
4652 int start, end, overlaps_p;
4654 struct glyph *glyph, *last;
4655 int voffset;
4656 int glyph_not_available_p;
4658 xassert (s->f == XFRAME (s->w->frame));
4659 xassert (s->nchars == 0);
4660 xassert (start >= 0 && end > start);
4662 s->for_overlaps_p = overlaps_p,
4663 glyph = s->row->glyphs[s->area] + start;
4664 last = s->row->glyphs[s->area] + end;
4665 voffset = glyph->voffset;
4667 glyph_not_available_p = glyph->glyph_not_available_p;
4669 while (glyph < last
4670 && glyph->type == CHAR_GLYPH
4671 && glyph->voffset == voffset
4672 /* Same face id implies same font, nowadays. */
4673 && glyph->face_id == face_id
4674 && glyph->glyph_not_available_p == glyph_not_available_p)
4676 int two_byte_p;
4678 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4679 s->char2b + s->nchars,
4680 &two_byte_p);
4681 s->two_byte_p = two_byte_p;
4682 ++s->nchars;
4683 xassert (s->nchars <= end - start);
4684 s->width += glyph->pixel_width;
4685 ++glyph;
4688 s->font = s->face->font;
4689 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4691 /* If the specified font could not be loaded, use the frame's font,
4692 but record the fact that we couldn't load it in
4693 S->font_not_found_p so that we can draw rectangles for the
4694 characters of the glyph string. */
4695 if (s->font == NULL || glyph_not_available_p)
4697 s->font_not_found_p = 1;
4698 s->font = FRAME_FONT (s->f);
4701 /* Adjust base line for subscript/superscript text. */
4702 s->ybase += voffset;
4704 xassert (s->face && s->face->gc);
4705 return glyph - s->row->glyphs[s->area];
4709 /* Fill glyph string S from image glyph S->first_glyph. */
4711 static void
4712 x_fill_image_glyph_string (s)
4713 struct glyph_string *s;
4715 xassert (s->first_glyph->type == IMAGE_GLYPH);
4716 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4717 xassert (s->img);
4718 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4719 s->font = s->face->font;
4720 s->width = s->first_glyph->pixel_width;
4722 /* Adjust base line for subscript/superscript text. */
4723 s->ybase += s->first_glyph->voffset;
4727 /* Fill glyph string S from a sequence of stretch glyphs.
4729 ROW is the glyph row in which the glyphs are found, AREA is the
4730 area within the row. START is the index of the first glyph to
4731 consider, END is the index of the last + 1.
4733 Value is the index of the first glyph not in S. */
4735 static int
4736 x_fill_stretch_glyph_string (s, row, area, start, end)
4737 struct glyph_string *s;
4738 struct glyph_row *row;
4739 enum glyph_row_area area;
4740 int start, end;
4742 struct glyph *glyph, *last;
4743 int voffset, face_id;
4745 xassert (s->first_glyph->type == STRETCH_GLYPH);
4747 glyph = s->row->glyphs[s->area] + start;
4748 last = s->row->glyphs[s->area] + end;
4749 face_id = glyph->face_id;
4750 s->face = FACE_FROM_ID (s->f, face_id);
4751 s->font = s->face->font;
4752 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4753 s->width = glyph->pixel_width;
4754 voffset = glyph->voffset;
4756 for (++glyph;
4757 (glyph < last
4758 && glyph->type == STRETCH_GLYPH
4759 && glyph->voffset == voffset
4760 && glyph->face_id == face_id);
4761 ++glyph)
4762 s->width += glyph->pixel_width;
4764 /* Adjust base line for subscript/superscript text. */
4765 s->ybase += voffset;
4767 /* The case that face->gc == 0 is handled when drawing the glyph
4768 string by calling PREPARE_FACE_FOR_DISPLAY. */
4769 xassert (s->face);
4770 return glyph - s->row->glyphs[s->area];
4774 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4775 of XChar2b structures for S; it can't be allocated in
4776 x_init_glyph_string because it must be allocated via `alloca'. W
4777 is the window on which S is drawn. ROW and AREA are the glyph row
4778 and area within the row from which S is constructed. START is the
4779 index of the first glyph structure covered by S. HL is a
4780 face-override for drawing S. */
4782 static void
4783 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4784 struct glyph_string *s;
4785 XChar2b *char2b;
4786 struct window *w;
4787 struct glyph_row *row;
4788 enum glyph_row_area area;
4789 int start;
4790 enum draw_glyphs_face hl;
4792 bzero (s, sizeof *s);
4793 s->w = w;
4794 s->f = XFRAME (w->frame);
4795 s->display = FRAME_X_DISPLAY (s->f);
4796 s->window = FRAME_X_WINDOW (s->f);
4797 s->char2b = char2b;
4798 s->hl = hl;
4799 s->row = row;
4800 s->area = area;
4801 s->first_glyph = row->glyphs[area] + start;
4802 s->height = row->height;
4803 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4805 /* Display the internal border below the tool-bar window. */
4806 if (s->w == XWINDOW (s->f->tool_bar_window))
4807 s->y -= s->f->output_data.x->internal_border_width;
4809 s->ybase = s->y + row->ascent;
4813 /* Set background width of glyph string S. START is the index of the
4814 first glyph following S. LAST_X is the right-most x-position + 1
4815 in the drawing area. */
4817 static INLINE void
4818 x_set_glyph_string_background_width (s, start, last_x)
4819 struct glyph_string *s;
4820 int start;
4821 int last_x;
4823 /* If the face of this glyph string has to be drawn to the end of
4824 the drawing area, set S->extends_to_end_of_line_p. */
4825 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4827 if (start == s->row->used[s->area]
4828 && s->area == TEXT_AREA
4829 && ((s->hl == DRAW_NORMAL_TEXT
4830 && (s->row->fill_line_p
4831 || s->face->background != default_face->background
4832 || s->face->stipple != default_face->stipple
4833 || s->row->mouse_face_p))
4834 || s->hl == DRAW_MOUSE_FACE
4835 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4836 && s->row->fill_line_p)))
4837 s->extends_to_end_of_line_p = 1;
4839 /* If S extends its face to the end of the line, set its
4840 background_width to the distance to the right edge of the drawing
4841 area. */
4842 if (s->extends_to_end_of_line_p)
4843 s->background_width = last_x - s->x + 1;
4844 else
4845 s->background_width = s->width;
4849 /* Add a glyph string for a stretch glyph to the list of strings
4850 between HEAD and TAIL. START is the index of the stretch glyph in
4851 row area AREA of glyph row ROW. END is the index of the last glyph
4852 in that glyph row area. X is the current output position assigned
4853 to the new glyph string constructed. HL overrides that face of the
4854 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4855 is the right-most x-position of the drawing area. */
4857 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4858 and below -- keep them on one line. */
4859 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4860 do \
4862 s = (struct glyph_string *) alloca (sizeof *s); \
4863 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4864 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4865 x_append_glyph_string (&HEAD, &TAIL, s); \
4866 s->x = (X); \
4868 while (0)
4871 /* Add a glyph string for an image glyph to the list of strings
4872 between HEAD and TAIL. START is the index of the image glyph in
4873 row area AREA of glyph row ROW. END is the index of the last glyph
4874 in that glyph row area. X is the current output position assigned
4875 to the new glyph string constructed. HL overrides that face of the
4876 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4877 is the right-most x-position of the drawing area. */
4879 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4880 do \
4882 s = (struct glyph_string *) alloca (sizeof *s); \
4883 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4884 x_fill_image_glyph_string (s); \
4885 x_append_glyph_string (&HEAD, &TAIL, s); \
4886 ++START; \
4887 s->x = (X); \
4889 while (0)
4892 /* Add a glyph string for a sequence of character glyphs to the list
4893 of strings between HEAD and TAIL. START is the index of the first
4894 glyph in row area AREA of glyph row ROW that is part of the new
4895 glyph string. END is the index of the last glyph in that glyph row
4896 area. X is the current output position assigned to the new glyph
4897 string constructed. HL overrides that face of the glyph; e.g. it
4898 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4899 right-most x-position of the drawing area. */
4901 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4902 do \
4904 int c, face_id; \
4905 XChar2b *char2b; \
4907 c = (ROW)->glyphs[AREA][START].u.ch; \
4908 face_id = (ROW)->glyphs[AREA][START].face_id; \
4910 s = (struct glyph_string *) alloca (sizeof *s); \
4911 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4912 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4913 x_append_glyph_string (&HEAD, &TAIL, s); \
4914 s->x = (X); \
4915 START = x_fill_glyph_string (s, face_id, START, END, \
4916 OVERLAPS_P); \
4918 while (0)
4921 /* Add a glyph string for a composite sequence to the list of strings
4922 between HEAD and TAIL. START is the index of the first glyph in
4923 row area AREA of glyph row ROW that is part of the new glyph
4924 string. END is the index of the last glyph in that glyph row area.
4925 X is the current output position assigned to the new glyph string
4926 constructed. HL overrides that face of the glyph; e.g. it is
4927 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4928 x-position of the drawing area. */
4930 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4931 do { \
4932 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4933 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4934 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4935 struct composition *cmp = composition_table[cmp_id]; \
4936 int glyph_len = cmp->glyph_len; \
4937 XChar2b *char2b; \
4938 struct face **faces; \
4939 struct glyph_string *first_s = NULL; \
4940 int n; \
4942 base_face = base_face->ascii_face; \
4943 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4944 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4945 /* At first, fill in `char2b' and `faces'. */ \
4946 for (n = 0; n < glyph_len; n++) \
4948 int c = COMPOSITION_GLYPH (cmp, n); \
4949 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4950 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4951 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4952 this_face_id, char2b + n, 1); \
4955 /* Make glyph_strings for each glyph sequence that is drawable by \
4956 the same face, and append them to HEAD/TAIL. */ \
4957 for (n = 0; n < cmp->glyph_len;) \
4959 s = (struct glyph_string *) alloca (sizeof *s); \
4960 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4961 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4962 s->cmp = cmp; \
4963 s->gidx = n; \
4964 s->x = (X); \
4966 if (n == 0) \
4967 first_s = s; \
4969 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4972 ++START; \
4973 s = first_s; \
4974 } while (0)
4977 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4978 of AREA of glyph row ROW on window W between indices START and END.
4979 HL overrides the face for drawing glyph strings, e.g. it is
4980 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4981 x-positions of the drawing area.
4983 This is an ugly monster macro construct because we must use alloca
4984 to allocate glyph strings (because x_draw_glyphs can be called
4985 asynchronously). */
4987 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4988 do \
4990 HEAD = TAIL = NULL; \
4991 while (START < END) \
4993 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4994 switch (first_glyph->type) \
4996 case CHAR_GLYPH: \
4997 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4998 TAIL, HL, X, LAST_X, \
4999 OVERLAPS_P); \
5000 break; \
5002 case COMPOSITE_GLYPH: \
5003 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5004 HEAD, TAIL, HL, X, LAST_X,\
5005 OVERLAPS_P); \
5006 break; \
5008 case STRETCH_GLYPH: \
5009 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5010 HEAD, TAIL, HL, X, LAST_X); \
5011 break; \
5013 case IMAGE_GLYPH: \
5014 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5015 TAIL, HL, X, LAST_X); \
5016 break; \
5018 default: \
5019 abort (); \
5022 x_set_glyph_string_background_width (s, START, LAST_X); \
5023 (X) += s->width; \
5026 while (0)
5029 /* Draw glyphs between START and END in AREA of ROW on window W,
5030 starting at x-position X. X is relative to AREA in W. HL is a
5031 face-override with the following meaning:
5033 DRAW_NORMAL_TEXT draw normally
5034 DRAW_CURSOR draw in cursor face
5035 DRAW_MOUSE_FACE draw in mouse face.
5036 DRAW_INVERSE_VIDEO draw in mode line face
5037 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5038 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5040 If OVERLAPS_P is non-zero, draw only the foreground of characters
5041 and clip to the physical height of ROW.
5043 Value is the x-position reached, relative to AREA of W. */
5045 static int
5046 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5047 struct window *w;
5048 int x;
5049 struct glyph_row *row;
5050 enum glyph_row_area area;
5051 int start, end;
5052 enum draw_glyphs_face hl;
5053 int overlaps_p;
5055 struct glyph_string *head, *tail;
5056 struct glyph_string *s;
5057 int last_x, area_width;
5058 int x_reached;
5059 int i, j;
5061 /* Let's rather be paranoid than getting a SEGV. */
5062 end = min (end, row->used[area]);
5063 start = max (0, start);
5064 start = min (end, start);
5066 /* Translate X to frame coordinates. Set last_x to the right
5067 end of the drawing area. */
5068 if (row->full_width_p)
5070 /* X is relative to the left edge of W, without scroll bars
5071 or fringes. */
5072 struct frame *f = XFRAME (w->frame);
5073 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5075 x += window_left_x;
5076 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5077 last_x = window_left_x + area_width;
5079 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5081 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5082 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5083 last_x += width;
5084 else
5085 x -= width;
5088 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5089 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5091 else
5093 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5094 area_width = window_box_width (w, area);
5095 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5098 /* Build a doubly-linked list of glyph_string structures between
5099 head and tail from what we have to draw. Note that the macro
5100 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5101 the reason we use a separate variable `i'. */
5102 i = start;
5103 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5104 overlaps_p);
5105 if (tail)
5106 x_reached = tail->x + tail->background_width;
5107 else
5108 x_reached = x;
5110 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5111 the row, redraw some glyphs in front or following the glyph
5112 strings built above. */
5113 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5115 int dummy_x = 0;
5116 struct glyph_string *h, *t;
5118 /* Compute overhangs for all glyph strings. */
5119 for (s = head; s; s = s->next)
5120 x_compute_glyph_string_overhangs (s);
5122 /* Prepend glyph strings for glyphs in front of the first glyph
5123 string that are overwritten because of the first glyph
5124 string's left overhang. The background of all strings
5125 prepended must be drawn because the first glyph string
5126 draws over it. */
5127 i = x_left_overwritten (head);
5128 if (i >= 0)
5130 j = i;
5131 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5132 DRAW_NORMAL_TEXT, dummy_x, last_x,
5133 overlaps_p);
5134 start = i;
5135 x_compute_overhangs_and_x (t, head->x, 1);
5136 x_prepend_glyph_string_lists (&head, &tail, h, t);
5139 /* Prepend glyph strings for glyphs in front of the first glyph
5140 string that overwrite that glyph string because of their
5141 right overhang. For these strings, only the foreground must
5142 be drawn, because it draws over the glyph string at `head'.
5143 The background must not be drawn because this would overwrite
5144 right overhangs of preceding glyphs for which no glyph
5145 strings exist. */
5146 i = x_left_overwriting (head);
5147 if (i >= 0)
5149 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5150 DRAW_NORMAL_TEXT, dummy_x, last_x,
5151 overlaps_p);
5152 for (s = h; s; s = s->next)
5153 s->background_filled_p = 1;
5154 x_compute_overhangs_and_x (t, head->x, 1);
5155 x_prepend_glyph_string_lists (&head, &tail, h, t);
5158 /* Append glyphs strings for glyphs following the last glyph
5159 string tail that are overwritten by tail. The background of
5160 these strings has to be drawn because tail's foreground draws
5161 over it. */
5162 i = x_right_overwritten (tail);
5163 if (i >= 0)
5165 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5166 DRAW_NORMAL_TEXT, x, last_x,
5167 overlaps_p);
5168 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5169 x_append_glyph_string_lists (&head, &tail, h, t);
5172 /* Append glyph strings for glyphs following the last glyph
5173 string tail that overwrite tail. The foreground of such
5174 glyphs has to be drawn because it writes into the background
5175 of tail. The background must not be drawn because it could
5176 paint over the foreground of following glyphs. */
5177 i = x_right_overwriting (tail);
5178 if (i >= 0)
5180 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5181 DRAW_NORMAL_TEXT, x, last_x,
5182 overlaps_p);
5183 for (s = h; s; s = s->next)
5184 s->background_filled_p = 1;
5185 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5186 x_append_glyph_string_lists (&head, &tail, h, t);
5190 /* Draw all strings. */
5191 for (s = head; s; s = s->next)
5192 x_draw_glyph_string (s);
5194 if (area == TEXT_AREA
5195 && !row->full_width_p
5196 /* When drawing overlapping rows, only the glyph strings'
5197 foreground is drawn, which doesn't erase a cursor
5198 completely. */
5199 && !overlaps_p)
5201 int x0 = head ? head->x : x;
5202 int x1 = tail ? tail->x + tail->background_width : x;
5204 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5205 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5207 if (XFASTINT (w->left_margin_width) != 0)
5209 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5210 x0 -= left_area_width;
5211 x1 -= left_area_width;
5214 notice_overwritten_cursor (w, area, x0, x1,
5215 row->y, MATRIX_ROW_BOTTOM_Y (row));
5218 /* Value is the x-position up to which drawn, relative to AREA of W.
5219 This doesn't include parts drawn because of overhangs. */
5220 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5221 if (!row->full_width_p)
5223 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5224 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5225 if (area > TEXT_AREA)
5226 x_reached -= window_box_width (w, TEXT_AREA);
5229 return x_reached;
5233 /* Fix the display of area AREA of overlapping row ROW in window W. */
5235 static void
5236 x_fix_overlapping_area (w, row, area)
5237 struct window *w;
5238 struct glyph_row *row;
5239 enum glyph_row_area area;
5241 int i, x;
5243 BLOCK_INPUT;
5245 if (area == LEFT_MARGIN_AREA)
5246 x = 0;
5247 else if (area == TEXT_AREA)
5248 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5249 else
5250 x = (window_box_width (w, LEFT_MARGIN_AREA)
5251 + window_box_width (w, TEXT_AREA));
5253 for (i = 0; i < row->used[area];)
5255 if (row->glyphs[area][i].overlaps_vertically_p)
5257 int start = i, start_x = x;
5261 x += row->glyphs[area][i].pixel_width;
5262 ++i;
5264 while (i < row->used[area]
5265 && row->glyphs[area][i].overlaps_vertically_p);
5267 x_draw_glyphs (w, start_x, row, area, start, i,
5268 DRAW_NORMAL_TEXT, 1);
5270 else
5272 x += row->glyphs[area][i].pixel_width;
5273 ++i;
5277 UNBLOCK_INPUT;
5281 /* Output LEN glyphs starting at START at the nominal cursor position.
5282 Advance the nominal cursor over the text. The global variable
5283 updated_window contains the window being updated, updated_row is
5284 the glyph row being updated, and updated_area is the area of that
5285 row being updated. */
5287 static void
5288 x_write_glyphs (start, len)
5289 struct glyph *start;
5290 int len;
5292 int x, hpos;
5294 xassert (updated_window && updated_row);
5295 BLOCK_INPUT;
5297 /* Write glyphs. */
5299 hpos = start - updated_row->glyphs[updated_area];
5300 x = x_draw_glyphs (updated_window, output_cursor.x,
5301 updated_row, updated_area,
5302 hpos, hpos + len,
5303 DRAW_NORMAL_TEXT, 0);
5305 UNBLOCK_INPUT;
5307 /* Advance the output cursor. */
5308 output_cursor.hpos += len;
5309 output_cursor.x = x;
5313 /* Insert LEN glyphs from START at the nominal cursor position. */
5315 static void
5316 x_insert_glyphs (start, len)
5317 struct glyph *start;
5318 register int len;
5320 struct frame *f;
5321 struct window *w;
5322 int line_height, shift_by_width, shifted_region_width;
5323 struct glyph_row *row;
5324 struct glyph *glyph;
5325 int frame_x, frame_y, hpos;
5327 xassert (updated_window && updated_row);
5328 BLOCK_INPUT;
5329 w = updated_window;
5330 f = XFRAME (WINDOW_FRAME (w));
5332 /* Get the height of the line we are in. */
5333 row = updated_row;
5334 line_height = row->height;
5336 /* Get the width of the glyphs to insert. */
5337 shift_by_width = 0;
5338 for (glyph = start; glyph < start + len; ++glyph)
5339 shift_by_width += glyph->pixel_width;
5341 /* Get the width of the region to shift right. */
5342 shifted_region_width = (window_box_width (w, updated_area)
5343 - output_cursor.x
5344 - shift_by_width);
5346 /* Shift right. */
5347 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5348 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5349 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5350 f->output_data.x->normal_gc,
5351 frame_x, frame_y,
5352 shifted_region_width, line_height,
5353 frame_x + shift_by_width, frame_y);
5355 /* Write the glyphs. */
5356 hpos = start - row->glyphs[updated_area];
5357 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5358 DRAW_NORMAL_TEXT, 0);
5360 /* Advance the output cursor. */
5361 output_cursor.hpos += len;
5362 output_cursor.x += shift_by_width;
5363 UNBLOCK_INPUT;
5367 /* Delete N glyphs at the nominal cursor position. Not implemented
5368 for X frames. */
5370 static void
5371 x_delete_glyphs (n)
5372 register int n;
5374 abort ();
5378 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5379 If they are <= 0, this is probably an error. */
5381 void
5382 x_clear_area (dpy, window, x, y, width, height, exposures)
5383 Display *dpy;
5384 Window window;
5385 int x, y;
5386 int width, height;
5387 int exposures;
5389 xassert (width > 0 && height > 0);
5390 XClearArea (dpy, window, x, y, width, height, exposures);
5394 /* Erase the current text line from the nominal cursor position
5395 (inclusive) to pixel column TO_X (exclusive). The idea is that
5396 everything from TO_X onward is already erased.
5398 TO_X is a pixel position relative to updated_area of
5399 updated_window. TO_X == -1 means clear to the end of this area. */
5401 static void
5402 x_clear_end_of_line (to_x)
5403 int to_x;
5405 struct frame *f;
5406 struct window *w = updated_window;
5407 int max_x, min_y, max_y;
5408 int from_x, from_y, to_y;
5410 xassert (updated_window && updated_row);
5411 f = XFRAME (w->frame);
5413 if (updated_row->full_width_p)
5415 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5416 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5417 && !w->pseudo_window_p)
5418 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5420 else
5421 max_x = window_box_width (w, updated_area);
5422 max_y = window_text_bottom_y (w);
5424 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5425 of window. For TO_X > 0, truncate to end of drawing area. */
5426 if (to_x == 0)
5427 return;
5428 else if (to_x < 0)
5429 to_x = max_x;
5430 else
5431 to_x = min (to_x, max_x);
5433 to_y = min (max_y, output_cursor.y + updated_row->height);
5435 /* Notice if the cursor will be cleared by this operation. */
5436 if (!updated_row->full_width_p)
5437 notice_overwritten_cursor (w, updated_area,
5438 output_cursor.x, -1,
5439 updated_row->y,
5440 MATRIX_ROW_BOTTOM_Y (updated_row));
5442 from_x = output_cursor.x;
5444 /* Translate to frame coordinates. */
5445 if (updated_row->full_width_p)
5447 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5448 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5450 else
5452 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5453 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5456 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5457 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5458 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5460 /* Prevent inadvertently clearing to end of the X window. */
5461 if (to_x > from_x && to_y > from_y)
5463 BLOCK_INPUT;
5464 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5465 from_x, from_y, to_x - from_x, to_y - from_y,
5466 False);
5467 UNBLOCK_INPUT;
5472 /* Clear entire frame. If updating_frame is non-null, clear that
5473 frame. Otherwise clear the selected frame. */
5475 static void
5476 x_clear_frame ()
5478 struct frame *f;
5480 if (updating_frame)
5481 f = updating_frame;
5482 else
5483 f = SELECTED_FRAME ();
5485 /* Clearing the frame will erase any cursor, so mark them all as no
5486 longer visible. */
5487 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5488 output_cursor.hpos = output_cursor.vpos = 0;
5489 output_cursor.x = -1;
5491 /* We don't set the output cursor here because there will always
5492 follow an explicit cursor_to. */
5493 BLOCK_INPUT;
5494 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5496 /* We have to clear the scroll bars, too. If we have changed
5497 colors or something like that, then they should be notified. */
5498 x_scroll_bar_clear (f);
5500 XFlush (FRAME_X_DISPLAY (f));
5501 UNBLOCK_INPUT;
5506 /* Invert the middle quarter of the frame for .15 sec. */
5508 /* We use the select system call to do the waiting, so we have to make
5509 sure it's available. If it isn't, we just won't do visual bells. */
5511 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5514 /* Subtract the `struct timeval' values X and Y, storing the result in
5515 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5517 static int
5518 timeval_subtract (result, x, y)
5519 struct timeval *result, x, y;
5521 /* Perform the carry for the later subtraction by updating y. This
5522 is safer because on some systems the tv_sec member is unsigned. */
5523 if (x.tv_usec < y.tv_usec)
5525 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5526 y.tv_usec -= 1000000 * nsec;
5527 y.tv_sec += nsec;
5530 if (x.tv_usec - y.tv_usec > 1000000)
5532 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5533 y.tv_usec += 1000000 * nsec;
5534 y.tv_sec -= nsec;
5537 /* Compute the time remaining to wait. tv_usec is certainly
5538 positive. */
5539 result->tv_sec = x.tv_sec - y.tv_sec;
5540 result->tv_usec = x.tv_usec - y.tv_usec;
5542 /* Return indication of whether the result should be considered
5543 negative. */
5544 return x.tv_sec < y.tv_sec;
5547 void
5548 XTflash (f)
5549 struct frame *f;
5551 BLOCK_INPUT;
5554 GC gc;
5556 /* Create a GC that will use the GXxor function to flip foreground
5557 pixels into background pixels. */
5559 XGCValues values;
5561 values.function = GXxor;
5562 values.foreground = (f->output_data.x->foreground_pixel
5563 ^ f->output_data.x->background_pixel);
5565 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5566 GCFunction | GCForeground, &values);
5570 /* Get the height not including a menu bar widget. */
5571 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5572 /* Height of each line to flash. */
5573 int flash_height = FRAME_LINE_HEIGHT (f);
5574 /* These will be the left and right margins of the rectangles. */
5575 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5576 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5578 int width;
5580 /* Don't flash the area between a scroll bar and the frame
5581 edge it is next to. */
5582 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5584 case vertical_scroll_bar_left:
5585 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5586 break;
5588 case vertical_scroll_bar_right:
5589 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5590 break;
5592 default:
5593 break;
5596 width = flash_right - flash_left;
5598 /* If window is tall, flash top and bottom line. */
5599 if (height > 3 * FRAME_LINE_HEIGHT (f))
5601 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5602 flash_left,
5603 (FRAME_INTERNAL_BORDER_WIDTH (f)
5604 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5605 width, flash_height);
5606 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5607 flash_left,
5608 (height - flash_height
5609 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5610 width, flash_height);
5612 else
5613 /* If it is short, flash it all. */
5614 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5615 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5616 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5618 x_flush (f);
5621 struct timeval wakeup;
5623 EMACS_GET_TIME (wakeup);
5625 /* Compute time to wait until, propagating carry from usecs. */
5626 wakeup.tv_usec += 150000;
5627 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5628 wakeup.tv_usec %= 1000000;
5630 /* Keep waiting until past the time wakeup or any input gets
5631 available. */
5632 while (! detect_input_pending ())
5634 struct timeval current;
5635 struct timeval timeout;
5637 EMACS_GET_TIME (current);
5639 /* Break if result would be negative. */
5640 if (timeval_subtract (&current, wakeup, current))
5641 break;
5643 /* How long `select' should wait. */
5644 timeout.tv_sec = 0;
5645 timeout.tv_usec = 10000;
5647 /* Try to wait that long--but we might wake up sooner. */
5648 select (0, NULL, NULL, NULL, &timeout);
5652 /* If window is tall, flash top and bottom line. */
5653 if (height > 3 * FRAME_LINE_HEIGHT (f))
5655 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5656 flash_left,
5657 (FRAME_INTERNAL_BORDER_WIDTH (f)
5658 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5659 width, flash_height);
5660 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5661 flash_left,
5662 (height - flash_height
5663 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5664 width, flash_height);
5666 else
5667 /* If it is short, flash it all. */
5668 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5669 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5670 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5672 XFreeGC (FRAME_X_DISPLAY (f), gc);
5673 x_flush (f);
5677 UNBLOCK_INPUT;
5680 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5683 /* Make audible bell. */
5685 void
5686 XTring_bell ()
5688 struct frame *f = SELECTED_FRAME ();
5690 if (FRAME_X_DISPLAY (f))
5692 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5693 if (visible_bell)
5694 XTflash (f);
5695 else
5696 #endif
5698 BLOCK_INPUT;
5699 XBell (FRAME_X_DISPLAY (f), 0);
5700 XFlush (FRAME_X_DISPLAY (f));
5701 UNBLOCK_INPUT;
5707 /* Specify how many text lines, from the top of the window,
5708 should be affected by insert-lines and delete-lines operations.
5709 This, and those operations, are used only within an update
5710 that is bounded by calls to x_update_begin and x_update_end. */
5712 static void
5713 XTset_terminal_window (n)
5714 register int n;
5716 /* This function intentionally left blank. */
5721 /***********************************************************************
5722 Line Dance
5723 ***********************************************************************/
5725 /* Perform an insert-lines or delete-lines operation, inserting N
5726 lines or deleting -N lines at vertical position VPOS. */
5728 static void
5729 x_ins_del_lines (vpos, n)
5730 int vpos, n;
5732 abort ();
5736 /* Scroll part of the display as described by RUN. */
5738 static void
5739 x_scroll_run (w, run)
5740 struct window *w;
5741 struct run *run;
5743 struct frame *f = XFRAME (w->frame);
5744 int x, y, width, height, from_y, to_y, bottom_y;
5746 /* Get frame-relative bounding box of the text display area of W,
5747 without mode lines. Include in this box the left and right
5748 fringe of W. */
5749 window_box (w, -1, &x, &y, &width, &height);
5750 width += FRAME_X_FRINGE_WIDTH (f);
5751 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5753 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5754 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5755 bottom_y = y + height;
5757 if (to_y < from_y)
5759 /* Scrolling up. Make sure we don't copy part of the mode
5760 line at the bottom. */
5761 if (from_y + run->height > bottom_y)
5762 height = bottom_y - from_y;
5763 else
5764 height = run->height;
5766 else
5768 /* Scolling down. Make sure we don't copy over the mode line.
5769 at the bottom. */
5770 if (to_y + run->height > bottom_y)
5771 height = bottom_y - to_y;
5772 else
5773 height = run->height;
5776 BLOCK_INPUT;
5778 /* Cursor off. Will be switched on again in x_update_window_end. */
5779 updated_window = w;
5780 x_clear_cursor (w);
5782 XCopyArea (FRAME_X_DISPLAY (f),
5783 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5784 f->output_data.x->normal_gc,
5785 x, from_y,
5786 width, height,
5787 x, to_y);
5789 UNBLOCK_INPUT;
5794 /***********************************************************************
5795 Exposure Events
5796 ***********************************************************************/
5798 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5799 corner of the exposed rectangle. W and H are width and height of
5800 the exposed area. All are pixel values. W or H zero means redraw
5801 the entire frame. */
5803 static void
5804 expose_frame (f, x, y, w, h)
5805 struct frame *f;
5806 int x, y, w, h;
5808 XRectangle r;
5809 int mouse_face_overwritten_p = 0;
5811 TRACE ((stderr, "expose_frame "));
5813 /* No need to redraw if frame will be redrawn soon. */
5814 if (FRAME_GARBAGED_P (f))
5816 TRACE ((stderr, " garbaged\n"));
5817 return;
5820 /* If basic faces haven't been realized yet, there is no point in
5821 trying to redraw anything. This can happen when we get an expose
5822 event while Emacs is starting, e.g. by moving another window. */
5823 if (FRAME_FACE_CACHE (f) == NULL
5824 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5826 TRACE ((stderr, " no faces\n"));
5827 return;
5830 if (w == 0 || h == 0)
5832 r.x = r.y = 0;
5833 r.width = CANON_X_UNIT (f) * f->width;
5834 r.height = CANON_Y_UNIT (f) * f->height;
5836 else
5838 r.x = x;
5839 r.y = y;
5840 r.width = w;
5841 r.height = h;
5844 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5845 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5847 if (WINDOWP (f->tool_bar_window))
5848 mouse_face_overwritten_p
5849 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5851 #ifndef USE_X_TOOLKIT
5852 if (WINDOWP (f->menu_bar_window))
5853 mouse_face_overwritten_p
5854 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5855 #endif /* not USE_X_TOOLKIT */
5857 /* Some window managers support a focus-follows-mouse style with
5858 delayed raising of frames. Imagine a partially obscured frame,
5859 and moving the mouse into partially obscured mouse-face on that
5860 frame. The visible part of the mouse-face will be highlighted,
5861 then the WM raises the obscured frame. With at least one WM, KDE
5862 2.1, Emacs is not getting any event for the raising of the frame
5863 (even tried with SubstructureRedirectMask), only Expose events.
5864 These expose events will draw text normally, i.e. not
5865 highlighted. Which means we must redo the highlight here.
5866 Subsume it under ``we love X''. --gerd 2001-08-15 */
5867 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5869 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5870 if (f == dpyinfo->mouse_face_mouse_frame)
5872 int x = dpyinfo->mouse_face_mouse_x;
5873 int y = dpyinfo->mouse_face_mouse_y;
5874 clear_mouse_face (dpyinfo);
5875 note_mouse_highlight (f, x, y);
5881 /* Redraw (parts) of all windows in the window tree rooted at W that
5882 intersect R. R contains frame pixel coordinates. Value is
5883 non-zero if the exposure overwrites mouse-face. */
5885 static int
5886 expose_window_tree (w, r)
5887 struct window *w;
5888 XRectangle *r;
5890 struct frame *f = XFRAME (w->frame);
5891 int mouse_face_overwritten_p = 0;
5893 while (w && !FRAME_GARBAGED_P (f))
5895 if (!NILP (w->hchild))
5896 mouse_face_overwritten_p
5897 |= expose_window_tree (XWINDOW (w->hchild), r);
5898 else if (!NILP (w->vchild))
5899 mouse_face_overwritten_p
5900 |= expose_window_tree (XWINDOW (w->vchild), r);
5901 else
5902 mouse_face_overwritten_p |= expose_window (w, r);
5904 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5907 return mouse_face_overwritten_p;
5911 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5912 which intersects rectangle R. R is in window-relative coordinates. */
5914 static void
5915 expose_area (w, row, r, area)
5916 struct window *w;
5917 struct glyph_row *row;
5918 XRectangle *r;
5919 enum glyph_row_area area;
5921 struct glyph *first = row->glyphs[area];
5922 struct glyph *end = row->glyphs[area] + row->used[area];
5923 struct glyph *last;
5924 int first_x, start_x, x;
5926 if (area == TEXT_AREA && row->fill_line_p)
5927 /* If row extends face to end of line write the whole line. */
5928 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5929 DRAW_NORMAL_TEXT, 0);
5930 else
5932 /* Set START_X to the window-relative start position for drawing glyphs of
5933 AREA. The first glyph of the text area can be partially visible.
5934 The first glyphs of other areas cannot. */
5935 if (area == LEFT_MARGIN_AREA)
5936 start_x = 0;
5937 else if (area == TEXT_AREA)
5938 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5939 else
5940 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5941 + window_box_width (w, TEXT_AREA));
5942 x = start_x;
5944 /* Find the first glyph that must be redrawn. */
5945 while (first < end
5946 && x + first->pixel_width < r->x)
5948 x += first->pixel_width;
5949 ++first;
5952 /* Find the last one. */
5953 last = first;
5954 first_x = x;
5955 while (last < end
5956 && x < r->x + r->width)
5958 x += last->pixel_width;
5959 ++last;
5962 /* Repaint. */
5963 if (last > first)
5964 x_draw_glyphs (w, first_x - start_x, row, area,
5965 first - row->glyphs[area],
5966 last - row->glyphs[area],
5967 DRAW_NORMAL_TEXT, 0);
5972 /* Redraw the parts of the glyph row ROW on window W intersecting
5973 rectangle R. R is in window-relative coordinates. Value is
5974 non-zero if mouse-face was overwritten. */
5976 static int
5977 expose_line (w, row, r)
5978 struct window *w;
5979 struct glyph_row *row;
5980 XRectangle *r;
5982 xassert (row->enabled_p);
5984 if (row->mode_line_p || w->pseudo_window_p)
5985 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5986 DRAW_NORMAL_TEXT, 0);
5987 else
5989 if (row->used[LEFT_MARGIN_AREA])
5990 expose_area (w, row, r, LEFT_MARGIN_AREA);
5991 if (row->used[TEXT_AREA])
5992 expose_area (w, row, r, TEXT_AREA);
5993 if (row->used[RIGHT_MARGIN_AREA])
5994 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5995 x_draw_row_fringe_bitmaps (w, row);
5998 return row->mouse_face_p;
6002 /* Return non-zero if W's cursor intersects rectangle R. */
6004 static int
6005 x_phys_cursor_in_rect_p (w, r)
6006 struct window *w;
6007 XRectangle *r;
6009 XRectangle cr, result;
6010 struct glyph *cursor_glyph;
6012 cursor_glyph = get_phys_cursor_glyph (w);
6013 if (cursor_glyph)
6015 cr.x = w->phys_cursor.x;
6016 cr.y = w->phys_cursor.y;
6017 cr.width = cursor_glyph->pixel_width;
6018 cr.height = w->phys_cursor_height;
6019 return x_intersect_rectangles (&cr, r, &result);
6021 else
6022 return 0;
6026 /* Redraw the part of window W intersection rectangle FR. Pixel
6027 coordinates in FR are frame-relative. Call this function with
6028 input blocked. Value is non-zero if the exposure overwrites
6029 mouse-face. */
6031 static int
6032 expose_window (w, fr)
6033 struct window *w;
6034 XRectangle *fr;
6036 struct frame *f = XFRAME (w->frame);
6037 XRectangle wr, r;
6038 int mouse_face_overwritten_p = 0;
6040 /* If window is not yet fully initialized, do nothing. This can
6041 happen when toolkit scroll bars are used and a window is split.
6042 Reconfiguring the scroll bar will generate an expose for a newly
6043 created window. */
6044 if (w->current_matrix == NULL)
6045 return 0;
6047 /* When we're currently updating the window, display and current
6048 matrix usually don't agree. Arrange for a thorough display
6049 later. */
6050 if (w == updated_window)
6052 SET_FRAME_GARBAGED (f);
6053 return 0;
6056 /* Frame-relative pixel rectangle of W. */
6057 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6058 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6059 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6060 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6062 if (x_intersect_rectangles (fr, &wr, &r))
6064 int yb = window_text_bottom_y (w);
6065 struct glyph_row *row;
6066 int cursor_cleared_p;
6068 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6069 r.x, r.y, r.width, r.height));
6071 /* Convert to window coordinates. */
6072 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6073 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6075 /* Turn off the cursor. */
6076 if (!w->pseudo_window_p
6077 && x_phys_cursor_in_rect_p (w, &r))
6079 x_clear_cursor (w);
6080 cursor_cleared_p = 1;
6082 else
6083 cursor_cleared_p = 0;
6085 /* Find the first row intersecting the rectangle R. */
6086 for (row = w->current_matrix->rows;
6087 row->enabled_p;
6088 ++row)
6090 int y0 = row->y;
6091 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6093 if ((y0 >= r.y && y0 < r.y + r.height)
6094 || (y1 > r.y && y1 < r.y + r.height)
6095 || (r.y >= y0 && r.y < y1)
6096 || (r.y + r.height > y0 && r.y + r.height < y1))
6098 if (expose_line (w, row, &r))
6099 mouse_face_overwritten_p = 1;
6102 if (y1 >= yb)
6103 break;
6106 /* Display the mode line if there is one. */
6107 if (WINDOW_WANTS_MODELINE_P (w)
6108 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6109 row->enabled_p)
6110 && row->y < r.y + r.height)
6112 if (expose_line (w, row, &r))
6113 mouse_face_overwritten_p = 1;
6116 if (!w->pseudo_window_p)
6118 /* Draw border between windows. */
6119 x_draw_vertical_border (w);
6121 /* Turn the cursor on again. */
6122 if (cursor_cleared_p)
6123 x_update_window_cursor (w, 1);
6127 return mouse_face_overwritten_p;
6131 /* Determine the intersection of two rectangles R1 and R2. Return
6132 the intersection in *RESULT. Value is non-zero if RESULT is not
6133 empty. */
6135 static int
6136 x_intersect_rectangles (r1, r2, result)
6137 XRectangle *r1, *r2, *result;
6139 XRectangle *left, *right;
6140 XRectangle *upper, *lower;
6141 int intersection_p = 0;
6143 /* Rearrange so that R1 is the left-most rectangle. */
6144 if (r1->x < r2->x)
6145 left = r1, right = r2;
6146 else
6147 left = r2, right = r1;
6149 /* X0 of the intersection is right.x0, if this is inside R1,
6150 otherwise there is no intersection. */
6151 if (right->x <= left->x + left->width)
6153 result->x = right->x;
6155 /* The right end of the intersection is the minimum of the
6156 the right ends of left and right. */
6157 result->width = (min (left->x + left->width, right->x + right->width)
6158 - result->x);
6160 /* Same game for Y. */
6161 if (r1->y < r2->y)
6162 upper = r1, lower = r2;
6163 else
6164 upper = r2, lower = r1;
6166 /* The upper end of the intersection is lower.y0, if this is inside
6167 of upper. Otherwise, there is no intersection. */
6168 if (lower->y <= upper->y + upper->height)
6170 result->y = lower->y;
6172 /* The lower end of the intersection is the minimum of the lower
6173 ends of upper and lower. */
6174 result->height = (min (lower->y + lower->height,
6175 upper->y + upper->height)
6176 - result->y);
6177 intersection_p = 1;
6181 return intersection_p;
6188 static void
6189 frame_highlight (f)
6190 struct frame *f;
6192 /* We used to only do this if Vx_no_window_manager was non-nil, but
6193 the ICCCM (section 4.1.6) says that the window's border pixmap
6194 and border pixel are window attributes which are "private to the
6195 client", so we can always change it to whatever we want. */
6196 BLOCK_INPUT;
6197 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6198 f->output_data.x->border_pixel);
6199 UNBLOCK_INPUT;
6200 x_update_cursor (f, 1);
6203 static void
6204 frame_unhighlight (f)
6205 struct frame *f;
6207 /* We used to only do this if Vx_no_window_manager was non-nil, but
6208 the ICCCM (section 4.1.6) says that the window's border pixmap
6209 and border pixel are window attributes which are "private to the
6210 client", so we can always change it to whatever we want. */
6211 BLOCK_INPUT;
6212 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6213 f->output_data.x->border_tile);
6214 UNBLOCK_INPUT;
6215 x_update_cursor (f, 1);
6218 /* The focus has changed. Update the frames as necessary to reflect
6219 the new situation. Note that we can't change the selected frame
6220 here, because the Lisp code we are interrupting might become confused.
6221 Each event gets marked with the frame in which it occurred, so the
6222 Lisp code can tell when the switch took place by examining the events. */
6224 static void
6225 x_new_focus_frame (dpyinfo, frame)
6226 struct x_display_info *dpyinfo;
6227 struct frame *frame;
6229 struct frame *old_focus = dpyinfo->x_focus_frame;
6231 if (frame != dpyinfo->x_focus_frame)
6233 /* Set this before calling other routines, so that they see
6234 the correct value of x_focus_frame. */
6235 dpyinfo->x_focus_frame = frame;
6237 if (old_focus && old_focus->auto_lower)
6238 x_lower_frame (old_focus);
6240 #if 0
6241 selected_frame = frame;
6242 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6243 selected_frame);
6244 Fselect_window (selected_frame->selected_window);
6245 choose_minibuf_frame ();
6246 #endif /* ! 0 */
6248 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6249 pending_autoraise_frame = dpyinfo->x_focus_frame;
6250 else
6251 pending_autoraise_frame = 0;
6254 x_frame_rehighlight (dpyinfo);
6257 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6259 void
6260 x_mouse_leave (dpyinfo)
6261 struct x_display_info *dpyinfo;
6263 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6266 /* The focus has changed, or we have redirected a frame's focus to
6267 another frame (this happens when a frame uses a surrogate
6268 mini-buffer frame). Shift the highlight as appropriate.
6270 The FRAME argument doesn't necessarily have anything to do with which
6271 frame is being highlighted or un-highlighted; we only use it to find
6272 the appropriate X display info. */
6274 static void
6275 XTframe_rehighlight (frame)
6276 struct frame *frame;
6278 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6281 static void
6282 x_frame_rehighlight (dpyinfo)
6283 struct x_display_info *dpyinfo;
6285 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6287 if (dpyinfo->x_focus_frame)
6289 dpyinfo->x_highlight_frame
6290 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6291 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6292 : dpyinfo->x_focus_frame);
6293 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6295 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6296 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6299 else
6300 dpyinfo->x_highlight_frame = 0;
6302 if (dpyinfo->x_highlight_frame != old_highlight)
6304 if (old_highlight)
6305 frame_unhighlight (old_highlight);
6306 if (dpyinfo->x_highlight_frame)
6307 frame_highlight (dpyinfo->x_highlight_frame);
6313 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6315 /* Initialize mode_switch_bit and modifier_meaning. */
6316 static void
6317 x_find_modifier_meanings (dpyinfo)
6318 struct x_display_info *dpyinfo;
6320 int min_code, max_code;
6321 KeySym *syms;
6322 int syms_per_code;
6323 XModifierKeymap *mods;
6325 dpyinfo->meta_mod_mask = 0;
6326 dpyinfo->shift_lock_mask = 0;
6327 dpyinfo->alt_mod_mask = 0;
6328 dpyinfo->super_mod_mask = 0;
6329 dpyinfo->hyper_mod_mask = 0;
6331 #ifdef HAVE_X11R4
6332 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6333 #else
6334 min_code = dpyinfo->display->min_keycode;
6335 max_code = dpyinfo->display->max_keycode;
6336 #endif
6338 syms = XGetKeyboardMapping (dpyinfo->display,
6339 min_code, max_code - min_code + 1,
6340 &syms_per_code);
6341 mods = XGetModifierMapping (dpyinfo->display);
6343 /* Scan the modifier table to see which modifier bits the Meta and
6344 Alt keysyms are on. */
6346 int row, col; /* The row and column in the modifier table. */
6348 for (row = 3; row < 8; row++)
6349 for (col = 0; col < mods->max_keypermod; col++)
6351 KeyCode code
6352 = mods->modifiermap[(row * mods->max_keypermod) + col];
6354 /* Zeroes are used for filler. Skip them. */
6355 if (code == 0)
6356 continue;
6358 /* Are any of this keycode's keysyms a meta key? */
6360 int code_col;
6362 for (code_col = 0; code_col < syms_per_code; code_col++)
6364 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6366 switch (sym)
6368 case XK_Meta_L:
6369 case XK_Meta_R:
6370 dpyinfo->meta_mod_mask |= (1 << row);
6371 break;
6373 case XK_Alt_L:
6374 case XK_Alt_R:
6375 dpyinfo->alt_mod_mask |= (1 << row);
6376 break;
6378 case XK_Hyper_L:
6379 case XK_Hyper_R:
6380 dpyinfo->hyper_mod_mask |= (1 << row);
6381 break;
6383 case XK_Super_L:
6384 case XK_Super_R:
6385 dpyinfo->super_mod_mask |= (1 << row);
6386 break;
6388 case XK_Shift_Lock:
6389 /* Ignore this if it's not on the lock modifier. */
6390 if ((1 << row) == LockMask)
6391 dpyinfo->shift_lock_mask = LockMask;
6392 break;
6399 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6400 if (! dpyinfo->meta_mod_mask)
6402 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6403 dpyinfo->alt_mod_mask = 0;
6406 /* If some keys are both alt and meta,
6407 make them just meta, not alt. */
6408 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6410 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6413 XFree ((char *) syms);
6414 XFreeModifiermap (mods);
6417 /* Convert between the modifier bits X uses and the modifier bits
6418 Emacs uses. */
6420 static unsigned int
6421 x_x_to_emacs_modifiers (dpyinfo, state)
6422 struct x_display_info *dpyinfo;
6423 unsigned int state;
6425 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6426 | ((state & ControlMask) ? ctrl_modifier : 0)
6427 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6428 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6429 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6430 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6433 static unsigned int
6434 x_emacs_to_x_modifiers (dpyinfo, state)
6435 struct x_display_info *dpyinfo;
6436 unsigned int state;
6438 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6439 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6440 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6441 | ((state & shift_modifier) ? ShiftMask : 0)
6442 | ((state & ctrl_modifier) ? ControlMask : 0)
6443 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6446 /* Convert a keysym to its name. */
6448 char *
6449 x_get_keysym_name (keysym)
6450 KeySym keysym;
6452 char *value;
6454 BLOCK_INPUT;
6455 value = XKeysymToString (keysym);
6456 UNBLOCK_INPUT;
6458 return value;
6463 /* Mouse clicks and mouse movement. Rah. */
6465 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6466 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6467 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6468 not force the value into range. */
6470 void
6471 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6472 FRAME_PTR f;
6473 register int pix_x, pix_y;
6474 register int *x, *y;
6475 XRectangle *bounds;
6476 int noclip;
6478 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6479 even for negative values. */
6480 if (pix_x < 0)
6481 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6482 if (pix_y < 0)
6483 pix_y -= (f)->output_data.x->line_height - 1;
6485 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6486 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6488 if (bounds)
6490 bounds->width = FONT_WIDTH (f->output_data.x->font);
6491 bounds->height = f->output_data.x->line_height;
6492 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6493 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6496 if (!noclip)
6498 if (pix_x < 0)
6499 pix_x = 0;
6500 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6501 pix_x = FRAME_WINDOW_WIDTH (f);
6503 if (pix_y < 0)
6504 pix_y = 0;
6505 else if (pix_y > f->height)
6506 pix_y = f->height;
6509 *x = pix_x;
6510 *y = pix_y;
6514 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6515 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6516 can't tell the positions because W's display is not up to date,
6517 return 0. */
6520 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6521 struct window *w;
6522 int hpos, vpos;
6523 int *frame_x, *frame_y;
6525 int success_p;
6527 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6528 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6530 if (display_completed)
6532 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6533 struct glyph *glyph = row->glyphs[TEXT_AREA];
6534 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6536 *frame_y = row->y;
6537 *frame_x = row->x;
6538 while (glyph < end)
6540 *frame_x += glyph->pixel_width;
6541 ++glyph;
6544 success_p = 1;
6546 else
6548 *frame_y = *frame_x = 0;
6549 success_p = 0;
6552 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6553 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6554 return success_p;
6558 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6560 If the event is a button press, then note that we have grabbed
6561 the mouse. */
6563 static Lisp_Object
6564 construct_mouse_click (result, event, f)
6565 struct input_event *result;
6566 XButtonEvent *event;
6567 struct frame *f;
6569 /* Make the event type no_event; we'll change that when we decide
6570 otherwise. */
6571 result->kind = mouse_click;
6572 result->code = event->button - Button1;
6573 result->timestamp = event->time;
6574 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6575 event->state)
6576 | (event->type == ButtonRelease
6577 ? up_modifier
6578 : down_modifier));
6580 XSETINT (result->x, event->x);
6581 XSETINT (result->y, event->y);
6582 XSETFRAME (result->frame_or_window, f);
6583 result->arg = Qnil;
6584 return Qnil;
6588 /* Function to report a mouse movement to the mainstream Emacs code.
6589 The input handler calls this.
6591 We have received a mouse movement event, which is given in *event.
6592 If the mouse is over a different glyph than it was last time, tell
6593 the mainstream emacs code by setting mouse_moved. If not, ask for
6594 another motion event, so we can check again the next time it moves. */
6596 static XMotionEvent last_mouse_motion_event;
6597 static Lisp_Object last_mouse_motion_frame;
6599 static void
6600 note_mouse_movement (frame, event)
6601 FRAME_PTR frame;
6602 XMotionEvent *event;
6604 last_mouse_movement_time = event->time;
6605 last_mouse_motion_event = *event;
6606 XSETFRAME (last_mouse_motion_frame, frame);
6608 if (event->window != FRAME_X_WINDOW (frame))
6610 frame->mouse_moved = 1;
6611 last_mouse_scroll_bar = Qnil;
6612 note_mouse_highlight (frame, -1, -1);
6615 /* Has the mouse moved off the glyph it was on at the last sighting? */
6616 else if (event->x < last_mouse_glyph.x
6617 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6618 || event->y < last_mouse_glyph.y
6619 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6621 frame->mouse_moved = 1;
6622 last_mouse_scroll_bar = Qnil;
6623 note_mouse_highlight (frame, event->x, event->y);
6627 /* This is used for debugging, to turn off note_mouse_highlight. */
6629 int disable_mouse_highlight;
6633 /************************************************************************
6634 Mouse Face
6635 ************************************************************************/
6637 /* Find the glyph under window-relative coordinates X/Y in window W.
6638 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6639 strings. Return in *HPOS and *VPOS the row and column number of
6640 the glyph found. Return in *AREA the glyph area containing X.
6641 Value is a pointer to the glyph found or null if X/Y is not on
6642 text, or we can't tell because W's current matrix is not up to
6643 date. */
6645 static struct glyph *
6646 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6647 struct window *w;
6648 int x, y;
6649 int *hpos, *vpos, *area;
6650 int buffer_only_p;
6652 struct glyph *glyph, *end;
6653 struct glyph_row *row = NULL;
6654 int x0, i, left_area_width;
6656 /* Find row containing Y. Give up if some row is not enabled. */
6657 for (i = 0; i < w->current_matrix->nrows; ++i)
6659 row = MATRIX_ROW (w->current_matrix, i);
6660 if (!row->enabled_p)
6661 return NULL;
6662 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6663 break;
6666 *vpos = i;
6667 *hpos = 0;
6669 /* Give up if Y is not in the window. */
6670 if (i == w->current_matrix->nrows)
6671 return NULL;
6673 /* Get the glyph area containing X. */
6674 if (w->pseudo_window_p)
6676 *area = TEXT_AREA;
6677 x0 = 0;
6679 else
6681 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6682 if (x < left_area_width)
6684 *area = LEFT_MARGIN_AREA;
6685 x0 = 0;
6687 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6689 *area = TEXT_AREA;
6690 x0 = row->x + left_area_width;
6692 else
6694 *area = RIGHT_MARGIN_AREA;
6695 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6699 /* Find glyph containing X. */
6700 glyph = row->glyphs[*area];
6701 end = glyph + row->used[*area];
6702 while (glyph < end)
6704 if (x < x0 + glyph->pixel_width)
6706 if (w->pseudo_window_p)
6707 break;
6708 else if (!buffer_only_p || BUFFERP (glyph->object))
6709 break;
6712 x0 += glyph->pixel_width;
6713 ++glyph;
6716 if (glyph == end)
6717 return NULL;
6719 *hpos = glyph - row->glyphs[*area];
6720 return glyph;
6724 /* Convert frame-relative x/y to coordinates relative to window W.
6725 Takes pseudo-windows into account. */
6727 static void
6728 frame_to_window_pixel_xy (w, x, y)
6729 struct window *w;
6730 int *x, *y;
6732 if (w->pseudo_window_p)
6734 /* A pseudo-window is always full-width, and starts at the
6735 left edge of the frame, plus a frame border. */
6736 struct frame *f = XFRAME (w->frame);
6737 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6738 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6740 else
6742 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6743 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6748 /* Take proper action when mouse has moved to the mode or header line of
6749 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6750 mode line. X is relative to the start of the text display area of
6751 W, so the width of fringes and scroll bars must be subtracted
6752 to get a position relative to the start of the mode line. */
6754 static void
6755 note_mode_line_highlight (w, x, mode_line_p)
6756 struct window *w;
6757 int x, mode_line_p;
6759 struct frame *f = XFRAME (w->frame);
6760 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6761 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6762 struct glyph_row *row;
6764 if (mode_line_p)
6765 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6766 else
6767 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6769 if (row->enabled_p)
6771 struct glyph *glyph, *end;
6772 Lisp_Object help, map;
6773 int x0;
6775 /* Find the glyph under X. */
6776 glyph = row->glyphs[TEXT_AREA];
6777 end = glyph + row->used[TEXT_AREA];
6778 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6779 + FRAME_X_LEFT_FRINGE_WIDTH (f));
6781 while (glyph < end
6782 && x >= x0 + glyph->pixel_width)
6784 x0 += glyph->pixel_width;
6785 ++glyph;
6788 if (glyph < end
6789 && STRINGP (glyph->object)
6790 && XSTRING (glyph->object)->intervals
6791 && glyph->charpos >= 0
6792 && glyph->charpos < XSTRING (glyph->object)->size)
6794 /* If we're on a string with `help-echo' text property,
6795 arrange for the help to be displayed. This is done by
6796 setting the global variable help_echo to the help string. */
6797 help = Fget_text_property (make_number (glyph->charpos),
6798 Qhelp_echo, glyph->object);
6799 if (!NILP (help))
6801 help_echo = help;
6802 XSETWINDOW (help_echo_window, w);
6803 help_echo_object = glyph->object;
6804 help_echo_pos = glyph->charpos;
6807 /* Change the mouse pointer according to what is under X/Y. */
6808 map = Fget_text_property (make_number (glyph->charpos),
6809 Qlocal_map, glyph->object);
6810 if (KEYMAPP (map))
6811 cursor = f->output_data.x->nontext_cursor;
6812 else
6814 map = Fget_text_property (make_number (glyph->charpos),
6815 Qkeymap, glyph->object);
6816 if (KEYMAPP (map))
6817 cursor = f->output_data.x->nontext_cursor;
6822 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6826 /* Take proper action when the mouse has moved to position X, Y on
6827 frame F as regards highlighting characters that have mouse-face
6828 properties. Also de-highlighting chars where the mouse was before.
6829 X and Y can be negative or out of range. */
6831 static void
6832 note_mouse_highlight (f, x, y)
6833 struct frame *f;
6834 int x, y;
6836 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6837 int portion;
6838 Lisp_Object window;
6839 struct window *w;
6840 Cursor cursor = None;
6841 struct buffer *b;
6843 /* When a menu is active, don't highlight because this looks odd. */
6844 #ifdef USE_X_TOOLKIT
6845 if (popup_activated ())
6846 return;
6847 #endif
6849 if (disable_mouse_highlight
6850 || !f->glyphs_initialized_p)
6851 return;
6853 dpyinfo->mouse_face_mouse_x = x;
6854 dpyinfo->mouse_face_mouse_y = y;
6855 dpyinfo->mouse_face_mouse_frame = f;
6857 if (dpyinfo->mouse_face_defer)
6858 return;
6860 if (gc_in_progress)
6862 dpyinfo->mouse_face_deferred_gc = 1;
6863 return;
6866 /* Which window is that in? */
6867 window = window_from_coordinates (f, x, y, &portion, 1);
6869 /* If we were displaying active text in another window, clear that. */
6870 if (! EQ (window, dpyinfo->mouse_face_window))
6871 clear_mouse_face (dpyinfo);
6873 /* Not on a window -> return. */
6874 if (!WINDOWP (window))
6875 return;
6877 /* Convert to window-relative pixel coordinates. */
6878 w = XWINDOW (window);
6879 frame_to_window_pixel_xy (w, &x, &y);
6881 /* Handle tool-bar window differently since it doesn't display a
6882 buffer. */
6883 if (EQ (window, f->tool_bar_window))
6885 note_tool_bar_highlight (f, x, y);
6886 return;
6889 /* Mouse is on the mode or header line? */
6890 if (portion == 1 || portion == 3)
6892 note_mode_line_highlight (w, x, portion == 1);
6893 return;
6896 if (portion == 2)
6897 cursor = f->output_data.x->horizontal_drag_cursor;
6898 else
6899 cursor = f->output_data.x->text_cursor;
6901 /* Are we in a window whose display is up to date?
6902 And verify the buffer's text has not changed. */
6903 b = XBUFFER (w->buffer);
6904 if (/* Within text portion of the window. */
6905 portion == 0
6906 && EQ (w->window_end_valid, w->buffer)
6907 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6908 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6910 int hpos, vpos, pos, i, area;
6911 struct glyph *glyph;
6912 Lisp_Object object;
6913 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6914 Lisp_Object *overlay_vec = NULL;
6915 int len, noverlays;
6916 struct buffer *obuf;
6917 int obegv, ozv, same_region;
6919 /* Find the glyph under X/Y. */
6920 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6922 /* Clear mouse face if X/Y not over text. */
6923 if (glyph == NULL
6924 || area != TEXT_AREA
6925 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6927 if (clear_mouse_face (dpyinfo))
6928 cursor = None;
6929 goto set_cursor;
6932 pos = glyph->charpos;
6933 object = glyph->object;
6934 if (!STRINGP (object) && !BUFFERP (object))
6935 goto set_cursor;
6937 /* If we get an out-of-range value, return now; avoid an error. */
6938 if (BUFFERP (object) && pos > BUF_Z (b))
6939 goto set_cursor;
6941 /* Make the window's buffer temporarily current for
6942 overlays_at and compute_char_face. */
6943 obuf = current_buffer;
6944 current_buffer = b;
6945 obegv = BEGV;
6946 ozv = ZV;
6947 BEGV = BEG;
6948 ZV = Z;
6950 /* Is this char mouse-active or does it have help-echo? */
6951 position = make_number (pos);
6953 if (BUFFERP (object))
6955 /* Put all the overlays we want in a vector in overlay_vec.
6956 Store the length in len. If there are more than 10, make
6957 enough space for all, and try again. */
6958 len = 10;
6959 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6960 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6961 if (noverlays > len)
6963 len = noverlays;
6964 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6965 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6968 /* Sort overlays into increasing priority order. */
6969 noverlays = sort_overlays (overlay_vec, noverlays, w);
6971 else
6972 noverlays = 0;
6974 same_region = (EQ (window, dpyinfo->mouse_face_window)
6975 && vpos >= dpyinfo->mouse_face_beg_row
6976 && vpos <= dpyinfo->mouse_face_end_row
6977 && (vpos > dpyinfo->mouse_face_beg_row
6978 || hpos >= dpyinfo->mouse_face_beg_col)
6979 && (vpos < dpyinfo->mouse_face_end_row
6980 || hpos < dpyinfo->mouse_face_end_col
6981 || dpyinfo->mouse_face_past_end));
6983 if (same_region)
6984 cursor = None;
6986 /* Check mouse-face highlighting. */
6987 if (! same_region
6988 /* If there exists an overlay with mouse-face overlapping
6989 the one we are currently highlighting, we have to
6990 check if we enter the overlapping overlay, and then
6991 highlight only that. */
6992 || (OVERLAYP (dpyinfo->mouse_face_overlay)
6993 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
6995 /* Find the highest priority overlay that has a mouse-face
6996 property. */
6997 overlay = Qnil;
6998 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7000 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7001 if (!NILP (mouse_face))
7002 overlay = overlay_vec[i];
7005 /* If we're actually highlighting the same overlay as
7006 before, there's no need to do that again. */
7007 if (!NILP (overlay)
7008 && EQ (overlay, dpyinfo->mouse_face_overlay))
7009 goto check_help_echo;
7011 dpyinfo->mouse_face_overlay = overlay;
7013 /* Clear the display of the old active region, if any. */
7014 if (clear_mouse_face (dpyinfo))
7015 cursor = None;
7017 /* If no overlay applies, get a text property. */
7018 if (NILP (overlay))
7019 mouse_face = Fget_text_property (position, Qmouse_face, object);
7021 /* Handle the overlay case. */
7022 if (!NILP (overlay))
7024 /* Find the range of text around this char that
7025 should be active. */
7026 Lisp_Object before, after;
7027 int ignore;
7029 before = Foverlay_start (overlay);
7030 after = Foverlay_end (overlay);
7031 /* Record this as the current active region. */
7032 fast_find_position (w, XFASTINT (before),
7033 &dpyinfo->mouse_face_beg_col,
7034 &dpyinfo->mouse_face_beg_row,
7035 &dpyinfo->mouse_face_beg_x,
7036 &dpyinfo->mouse_face_beg_y, Qnil);
7038 dpyinfo->mouse_face_past_end
7039 = !fast_find_position (w, XFASTINT (after),
7040 &dpyinfo->mouse_face_end_col,
7041 &dpyinfo->mouse_face_end_row,
7042 &dpyinfo->mouse_face_end_x,
7043 &dpyinfo->mouse_face_end_y, Qnil);
7044 dpyinfo->mouse_face_window = window;
7045 dpyinfo->mouse_face_face_id
7046 = face_at_buffer_position (w, pos, 0, 0,
7047 &ignore, pos + 1, 1);
7049 /* Display it as active. */
7050 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7051 cursor = None;
7053 /* Handle the text property case. */
7054 else if (!NILP (mouse_face) && BUFFERP (object))
7056 /* Find the range of text around this char that
7057 should be active. */
7058 Lisp_Object before, after, beginning, end;
7059 int ignore;
7061 beginning = Fmarker_position (w->start);
7062 end = make_number (BUF_Z (XBUFFER (object))
7063 - XFASTINT (w->window_end_pos));
7064 before
7065 = Fprevious_single_property_change (make_number (pos + 1),
7066 Qmouse_face,
7067 object, beginning);
7068 after
7069 = Fnext_single_property_change (position, Qmouse_face,
7070 object, end);
7072 /* Record this as the current active region. */
7073 fast_find_position (w, XFASTINT (before),
7074 &dpyinfo->mouse_face_beg_col,
7075 &dpyinfo->mouse_face_beg_row,
7076 &dpyinfo->mouse_face_beg_x,
7077 &dpyinfo->mouse_face_beg_y, Qnil);
7078 dpyinfo->mouse_face_past_end
7079 = !fast_find_position (w, XFASTINT (after),
7080 &dpyinfo->mouse_face_end_col,
7081 &dpyinfo->mouse_face_end_row,
7082 &dpyinfo->mouse_face_end_x,
7083 &dpyinfo->mouse_face_end_y, Qnil);
7084 dpyinfo->mouse_face_window = window;
7086 if (BUFFERP (object))
7087 dpyinfo->mouse_face_face_id
7088 = face_at_buffer_position (w, pos, 0, 0,
7089 &ignore, pos + 1, 1);
7091 /* Display it as active. */
7092 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7093 cursor = None;
7095 else if (!NILP (mouse_face) && STRINGP (object))
7097 Lisp_Object b, e;
7098 int ignore;
7100 b = Fprevious_single_property_change (make_number (pos + 1),
7101 Qmouse_face,
7102 object, Qnil);
7103 e = Fnext_single_property_change (position, Qmouse_face,
7104 object, Qnil);
7105 if (NILP (b))
7106 b = make_number (0);
7107 if (NILP (e))
7108 e = make_number (XSTRING (object)->size - 1);
7109 fast_find_string_pos (w, XINT (b), object,
7110 &dpyinfo->mouse_face_beg_col,
7111 &dpyinfo->mouse_face_beg_row,
7112 &dpyinfo->mouse_face_beg_x,
7113 &dpyinfo->mouse_face_beg_y, 0);
7114 fast_find_string_pos (w, XINT (e), object,
7115 &dpyinfo->mouse_face_end_col,
7116 &dpyinfo->mouse_face_end_row,
7117 &dpyinfo->mouse_face_end_x,
7118 &dpyinfo->mouse_face_end_y, 1);
7119 dpyinfo->mouse_face_past_end = 0;
7120 dpyinfo->mouse_face_window = window;
7121 dpyinfo->mouse_face_face_id
7122 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7123 glyph->face_id, 1);
7124 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7125 cursor = None;
7127 else if (STRINGP (object) && NILP (mouse_face))
7129 /* A string which doesn't have mouse-face, but
7130 the text ``under'' it might have. */
7131 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7132 int start = MATRIX_ROW_START_CHARPOS (r);
7134 pos = string_buffer_position (w, object, start);
7135 if (pos > 0)
7136 mouse_face = get_char_property_and_overlay (make_number (pos),
7137 Qmouse_face,
7138 w->buffer,
7139 &overlay);
7140 if (!NILP (mouse_face) && !NILP (overlay))
7142 Lisp_Object before = Foverlay_start (overlay);
7143 Lisp_Object after = Foverlay_end (overlay);
7144 int ignore;
7146 /* Note that we might not be able to find position
7147 BEFORE in the glyph matrix if the overlay is
7148 entirely covered by a `display' property. In
7149 this case, we overshoot. So let's stop in
7150 the glyph matrix before glyphs for OBJECT. */
7151 fast_find_position (w, XFASTINT (before),
7152 &dpyinfo->mouse_face_beg_col,
7153 &dpyinfo->mouse_face_beg_row,
7154 &dpyinfo->mouse_face_beg_x,
7155 &dpyinfo->mouse_face_beg_y,
7156 object);
7158 dpyinfo->mouse_face_past_end
7159 = !fast_find_position (w, XFASTINT (after),
7160 &dpyinfo->mouse_face_end_col,
7161 &dpyinfo->mouse_face_end_row,
7162 &dpyinfo->mouse_face_end_x,
7163 &dpyinfo->mouse_face_end_y,
7164 Qnil);
7165 dpyinfo->mouse_face_window = window;
7166 dpyinfo->mouse_face_face_id
7167 = face_at_buffer_position (w, pos, 0, 0,
7168 &ignore, pos + 1, 1);
7170 /* Display it as active. */
7171 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7172 cursor = None;
7177 check_help_echo:
7179 /* Look for a `help-echo' property. */
7181 Lisp_Object help, overlay;
7183 /* Check overlays first. */
7184 help = overlay = Qnil;
7185 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7187 overlay = overlay_vec[i];
7188 help = Foverlay_get (overlay, Qhelp_echo);
7191 if (!NILP (help))
7193 help_echo = help;
7194 help_echo_window = window;
7195 help_echo_object = overlay;
7196 help_echo_pos = pos;
7198 else
7200 Lisp_Object object = glyph->object;
7201 int charpos = glyph->charpos;
7203 /* Try text properties. */
7204 if (STRINGP (object)
7205 && charpos >= 0
7206 && charpos < XSTRING (object)->size)
7208 help = Fget_text_property (make_number (charpos),
7209 Qhelp_echo, object);
7210 if (NILP (help))
7212 /* If the string itself doesn't specify a help-echo,
7213 see if the buffer text ``under'' it does. */
7214 struct glyph_row *r
7215 = MATRIX_ROW (w->current_matrix, vpos);
7216 int start = MATRIX_ROW_START_CHARPOS (r);
7217 int pos = string_buffer_position (w, object, start);
7218 if (pos > 0)
7220 help = Fget_char_property (make_number (pos),
7221 Qhelp_echo, w->buffer);
7222 if (!NILP (help))
7224 charpos = pos;
7225 object = w->buffer;
7230 else if (BUFFERP (object)
7231 && charpos >= BEGV
7232 && charpos < ZV)
7233 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7234 object);
7236 if (!NILP (help))
7238 help_echo = help;
7239 help_echo_window = window;
7240 help_echo_object = object;
7241 help_echo_pos = charpos;
7246 BEGV = obegv;
7247 ZV = ozv;
7248 current_buffer = obuf;
7251 set_cursor:
7253 if (cursor != None)
7254 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7257 static void
7258 redo_mouse_highlight ()
7260 if (!NILP (last_mouse_motion_frame)
7261 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7262 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7263 last_mouse_motion_event.x,
7264 last_mouse_motion_event.y);
7269 /***********************************************************************
7270 Tool-bars
7271 ***********************************************************************/
7273 static int x_tool_bar_item P_ ((struct frame *, int, int,
7274 struct glyph **, int *, int *, int *));
7276 /* Tool-bar item index of the item on which a mouse button was pressed
7277 or -1. */
7279 static int last_tool_bar_item;
7282 /* Get information about the tool-bar item at position X/Y on frame F.
7283 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7284 the current matrix of the tool-bar window of F, or NULL if not
7285 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7286 item in F->tool_bar_items. Value is
7288 -1 if X/Y is not on a tool-bar item
7289 0 if X/Y is on the same item that was highlighted before.
7290 1 otherwise. */
7292 static int
7293 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7294 struct frame *f;
7295 int x, y;
7296 struct glyph **glyph;
7297 int *hpos, *vpos, *prop_idx;
7299 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7300 struct window *w = XWINDOW (f->tool_bar_window);
7301 int area;
7303 /* Find the glyph under X/Y. */
7304 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7305 if (*glyph == NULL)
7306 return -1;
7308 /* Get the start of this tool-bar item's properties in
7309 f->tool_bar_items. */
7310 if (!tool_bar_item_info (f, *glyph, prop_idx))
7311 return -1;
7313 /* Is mouse on the highlighted item? */
7314 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7315 && *vpos >= dpyinfo->mouse_face_beg_row
7316 && *vpos <= dpyinfo->mouse_face_end_row
7317 && (*vpos > dpyinfo->mouse_face_beg_row
7318 || *hpos >= dpyinfo->mouse_face_beg_col)
7319 && (*vpos < dpyinfo->mouse_face_end_row
7320 || *hpos < dpyinfo->mouse_face_end_col
7321 || dpyinfo->mouse_face_past_end))
7322 return 0;
7324 return 1;
7328 /* Handle mouse button event on the tool-bar of frame F, at
7329 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7330 or ButtonRelase. */
7332 static void
7333 x_handle_tool_bar_click (f, button_event)
7334 struct frame *f;
7335 XButtonEvent *button_event;
7337 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7338 struct window *w = XWINDOW (f->tool_bar_window);
7339 int hpos, vpos, prop_idx;
7340 struct glyph *glyph;
7341 Lisp_Object enabled_p;
7342 int x = button_event->x;
7343 int y = button_event->y;
7345 /* If not on the highlighted tool-bar item, return. */
7346 frame_to_window_pixel_xy (w, &x, &y);
7347 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7348 return;
7350 /* If item is disabled, do nothing. */
7351 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7352 if (NILP (enabled_p))
7353 return;
7355 if (button_event->type == ButtonPress)
7357 /* Show item in pressed state. */
7358 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7359 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7360 last_tool_bar_item = prop_idx;
7362 else
7364 Lisp_Object key, frame;
7365 struct input_event event;
7367 /* Show item in released state. */
7368 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7369 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7371 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7373 XSETFRAME (frame, f);
7374 event.kind = TOOL_BAR_EVENT;
7375 event.frame_or_window = frame;
7376 event.arg = frame;
7377 kbd_buffer_store_event (&event);
7379 event.kind = TOOL_BAR_EVENT;
7380 event.frame_or_window = frame;
7381 event.arg = key;
7382 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7383 button_event->state);
7384 kbd_buffer_store_event (&event);
7385 last_tool_bar_item = -1;
7390 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7391 tool-bar window-relative coordinates X/Y. Called from
7392 note_mouse_highlight. */
7394 static void
7395 note_tool_bar_highlight (f, x, y)
7396 struct frame *f;
7397 int x, y;
7399 Lisp_Object window = f->tool_bar_window;
7400 struct window *w = XWINDOW (window);
7401 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7402 int hpos, vpos;
7403 struct glyph *glyph;
7404 struct glyph_row *row;
7405 int i;
7406 Lisp_Object enabled_p;
7407 int prop_idx;
7408 enum draw_glyphs_face draw;
7409 int mouse_down_p, rc;
7411 /* Function note_mouse_highlight is called with negative x(y
7412 values when mouse moves outside of the frame. */
7413 if (x <= 0 || y <= 0)
7415 clear_mouse_face (dpyinfo);
7416 return;
7419 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7420 if (rc < 0)
7422 /* Not on tool-bar item. */
7423 clear_mouse_face (dpyinfo);
7424 return;
7426 else if (rc == 0)
7427 goto set_help_echo;
7429 clear_mouse_face (dpyinfo);
7431 /* Mouse is down, but on different tool-bar item? */
7432 mouse_down_p = (dpyinfo->grabbed
7433 && f == last_mouse_frame
7434 && FRAME_LIVE_P (f));
7435 if (mouse_down_p
7436 && last_tool_bar_item != prop_idx)
7437 return;
7439 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7440 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7442 /* If tool-bar item is not enabled, don't highlight it. */
7443 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7444 if (!NILP (enabled_p))
7446 /* Compute the x-position of the glyph. In front and past the
7447 image is a space. We include this is the highlighted area. */
7448 row = MATRIX_ROW (w->current_matrix, vpos);
7449 for (i = x = 0; i < hpos; ++i)
7450 x += row->glyphs[TEXT_AREA][i].pixel_width;
7452 /* Record this as the current active region. */
7453 dpyinfo->mouse_face_beg_col = hpos;
7454 dpyinfo->mouse_face_beg_row = vpos;
7455 dpyinfo->mouse_face_beg_x = x;
7456 dpyinfo->mouse_face_beg_y = row->y;
7457 dpyinfo->mouse_face_past_end = 0;
7459 dpyinfo->mouse_face_end_col = hpos + 1;
7460 dpyinfo->mouse_face_end_row = vpos;
7461 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7462 dpyinfo->mouse_face_end_y = row->y;
7463 dpyinfo->mouse_face_window = window;
7464 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7466 /* Display it as active. */
7467 show_mouse_face (dpyinfo, draw);
7468 dpyinfo->mouse_face_image_state = draw;
7471 set_help_echo:
7473 /* Set help_echo to a help string to display for this tool-bar item.
7474 XTread_socket does the rest. */
7475 help_echo_object = help_echo_window = Qnil;
7476 help_echo_pos = -1;
7477 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7478 if (NILP (help_echo))
7479 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7484 /* Find the glyph matrix position of buffer position CHARPOS in window
7485 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7486 current glyphs must be up to date. If CHARPOS is above window
7487 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7488 of last line in W. In the row containing CHARPOS, stop before glyphs
7489 having STOP as object. */
7491 #if 0 /* This is a version of fast_find_position that's more correct
7492 in the presence of hscrolling, for example. I didn't install
7493 it right away because the problem fixed is minor, it failed
7494 in 20.x as well, and I think it's too risky to install
7495 so near the release of 21.1. 2001-09-25 gerd. */
7497 static int
7498 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7499 struct window *w;
7500 int charpos;
7501 int *hpos, *vpos, *x, *y;
7502 Lisp_Object stop;
7504 struct glyph_row *row, *first;
7505 struct glyph *glyph, *end;
7506 int i, past_end = 0;
7508 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7509 row = row_containing_pos (w, charpos, first, NULL, 0);
7510 if (row == NULL)
7512 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7514 *x = *y = *hpos = *vpos = 0;
7515 return 0;
7517 else
7519 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7520 past_end = 1;
7524 *x = row->x;
7525 *y = row->y;
7526 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7528 glyph = row->glyphs[TEXT_AREA];
7529 end = glyph + row->used[TEXT_AREA];
7531 /* Skip over glyphs not having an object at the start of the row.
7532 These are special glyphs like truncation marks on terminal
7533 frames. */
7534 if (row->displays_text_p)
7535 while (glyph < end
7536 && INTEGERP (glyph->object)
7537 && !EQ (stop, glyph->object)
7538 && glyph->charpos < 0)
7540 *x += glyph->pixel_width;
7541 ++glyph;
7544 while (glyph < end
7545 && !INTEGERP (glyph->object)
7546 && !EQ (stop, glyph->object)
7547 && (!BUFFERP (glyph->object)
7548 || glyph->charpos < charpos))
7550 *x += glyph->pixel_width;
7551 ++glyph;
7554 *hpos = glyph - row->glyphs[TEXT_AREA];
7555 return past_end;
7558 #else /* not 0 */
7560 static int
7561 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7562 struct window *w;
7563 int pos;
7564 int *hpos, *vpos, *x, *y;
7565 Lisp_Object stop;
7567 int i;
7568 int lastcol;
7569 int maybe_next_line_p = 0;
7570 int line_start_position;
7571 int yb = window_text_bottom_y (w);
7572 struct glyph_row *row, *best_row;
7573 int row_vpos, best_row_vpos;
7574 int current_x;
7576 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7577 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7579 while (row->y < yb)
7581 if (row->used[TEXT_AREA])
7582 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7583 else
7584 line_start_position = 0;
7586 if (line_start_position > pos)
7587 break;
7588 /* If the position sought is the end of the buffer,
7589 don't include the blank lines at the bottom of the window. */
7590 else if (line_start_position == pos
7591 && pos == BUF_ZV (XBUFFER (w->buffer)))
7593 maybe_next_line_p = 1;
7594 break;
7596 else if (line_start_position > 0)
7598 best_row = row;
7599 best_row_vpos = row_vpos;
7602 if (row->y + row->height >= yb)
7603 break;
7605 ++row;
7606 ++row_vpos;
7609 /* Find the right column within BEST_ROW. */
7610 lastcol = 0;
7611 current_x = best_row->x;
7612 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7614 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7615 int charpos = glyph->charpos;
7617 if (BUFFERP (glyph->object))
7619 if (charpos == pos)
7621 *hpos = i;
7622 *vpos = best_row_vpos;
7623 *x = current_x;
7624 *y = best_row->y;
7625 return 1;
7627 else if (charpos > pos)
7628 break;
7630 else if (EQ (glyph->object, stop))
7631 break;
7633 if (charpos > 0)
7634 lastcol = i;
7635 current_x += glyph->pixel_width;
7638 /* If we're looking for the end of the buffer,
7639 and we didn't find it in the line we scanned,
7640 use the start of the following line. */
7641 if (maybe_next_line_p)
7643 ++best_row;
7644 ++best_row_vpos;
7645 lastcol = 0;
7646 current_x = best_row->x;
7649 *vpos = best_row_vpos;
7650 *hpos = lastcol + 1;
7651 *x = current_x;
7652 *y = best_row->y;
7653 return 0;
7656 #endif /* not 0 */
7659 /* Find the position of the glyph for position POS in OBJECT in
7660 window W's current matrix, and return in *X, *Y the pixel
7661 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7663 RIGHT_P non-zero means return the position of the right edge of the
7664 glyph, RIGHT_P zero means return the left edge position.
7666 If no glyph for POS exists in the matrix, return the position of
7667 the glyph with the next smaller position that is in the matrix, if
7668 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7669 exists in the matrix, return the position of the glyph with the
7670 next larger position in OBJECT.
7672 Value is non-zero if a glyph was found. */
7674 static int
7675 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7676 struct window *w;
7677 int pos;
7678 Lisp_Object object;
7679 int *hpos, *vpos, *x, *y;
7680 int right_p;
7682 int yb = window_text_bottom_y (w);
7683 struct glyph_row *r;
7684 struct glyph *best_glyph = NULL;
7685 struct glyph_row *best_row = NULL;
7686 int best_x = 0;
7688 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7689 r->enabled_p && r->y < yb;
7690 ++r)
7692 struct glyph *g = r->glyphs[TEXT_AREA];
7693 struct glyph *e = g + r->used[TEXT_AREA];
7694 int gx;
7696 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7697 if (EQ (g->object, object))
7699 if (g->charpos == pos)
7701 best_glyph = g;
7702 best_x = gx;
7703 best_row = r;
7704 goto found;
7706 else if (best_glyph == NULL
7707 || ((abs (g->charpos - pos)
7708 < abs (best_glyph->charpos - pos))
7709 && (right_p
7710 ? g->charpos < pos
7711 : g->charpos > pos)))
7713 best_glyph = g;
7714 best_x = gx;
7715 best_row = r;
7720 found:
7722 if (best_glyph)
7724 *x = best_x;
7725 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7727 if (right_p)
7729 *x += best_glyph->pixel_width;
7730 ++*hpos;
7733 *y = best_row->y;
7734 *vpos = best_row - w->current_matrix->rows;
7737 return best_glyph != NULL;
7741 /* Display the active region described by mouse_face_*
7742 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7744 static void
7745 show_mouse_face (dpyinfo, draw)
7746 struct x_display_info *dpyinfo;
7747 enum draw_glyphs_face draw;
7749 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7750 struct frame *f = XFRAME (WINDOW_FRAME (w));
7752 if (/* If window is in the process of being destroyed, don't bother
7753 to do anything. */
7754 w->current_matrix != NULL
7755 /* Recognize when we are called to operate on rows that don't exist
7756 anymore. This can happen when a window is split. */
7757 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7759 int phys_cursor_on_p = w->phys_cursor_on_p;
7760 struct glyph_row *row, *first, *last;
7762 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7763 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7765 for (row = first; row <= last && row->enabled_p; ++row)
7767 int start_hpos, end_hpos, start_x;
7769 /* For all but the first row, the highlight starts at column 0. */
7770 if (row == first)
7772 start_hpos = dpyinfo->mouse_face_beg_col;
7773 start_x = dpyinfo->mouse_face_beg_x;
7775 else
7777 start_hpos = 0;
7778 start_x = 0;
7781 if (row == last)
7782 end_hpos = dpyinfo->mouse_face_end_col;
7783 else
7784 end_hpos = row->used[TEXT_AREA];
7786 if (end_hpos > start_hpos)
7788 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7789 start_hpos, end_hpos, draw, 0);
7791 row->mouse_face_p
7792 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7796 /* When we've written over the cursor, arrange for it to
7797 be displayed again. */
7798 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7799 x_display_cursor (w, 1,
7800 w->phys_cursor.hpos, w->phys_cursor.vpos,
7801 w->phys_cursor.x, w->phys_cursor.y);
7804 /* Change the mouse cursor. */
7805 if (draw == DRAW_NORMAL_TEXT)
7806 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7807 f->output_data.x->text_cursor);
7808 else if (draw == DRAW_MOUSE_FACE)
7809 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7810 f->output_data.x->cross_cursor);
7811 else
7812 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7813 f->output_data.x->nontext_cursor);
7816 /* Clear out the mouse-highlighted active region.
7817 Redraw it un-highlighted first. Value is non-zero if mouse
7818 face was actually drawn unhighlighted. */
7820 static int
7821 clear_mouse_face (dpyinfo)
7822 struct x_display_info *dpyinfo;
7824 int cleared = 0;
7826 if (!NILP (dpyinfo->mouse_face_window))
7828 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7829 cleared = 1;
7832 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7833 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7834 dpyinfo->mouse_face_window = Qnil;
7835 dpyinfo->mouse_face_overlay = Qnil;
7836 return cleared;
7840 /* Clear any mouse-face on window W. This function is part of the
7841 redisplay interface, and is called from try_window_id and similar
7842 functions to ensure the mouse-highlight is off. */
7844 static void
7845 x_clear_mouse_face (w)
7846 struct window *w;
7848 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7849 Lisp_Object window;
7851 BLOCK_INPUT;
7852 XSETWINDOW (window, w);
7853 if (EQ (window, dpyinfo->mouse_face_window))
7854 clear_mouse_face (dpyinfo);
7855 UNBLOCK_INPUT;
7859 /* Just discard the mouse face information for frame F, if any.
7860 This is used when the size of F is changed. */
7862 void
7863 cancel_mouse_face (f)
7864 FRAME_PTR f;
7866 Lisp_Object window;
7867 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7869 window = dpyinfo->mouse_face_window;
7870 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7872 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7873 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7874 dpyinfo->mouse_face_window = Qnil;
7879 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7882 /* Try to determine frame pixel position and size of the glyph under
7883 frame pixel coordinates X/Y on frame F . Return the position and
7884 size in *RECT. Value is non-zero if we could compute these
7885 values. */
7887 static int
7888 glyph_rect (f, x, y, rect)
7889 struct frame *f;
7890 int x, y;
7891 XRectangle *rect;
7893 Lisp_Object window;
7894 int part, found = 0;
7896 window = window_from_coordinates (f, x, y, &part, 0);
7897 if (!NILP (window))
7899 struct window *w = XWINDOW (window);
7900 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7901 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7903 frame_to_window_pixel_xy (w, &x, &y);
7905 for (; !found && r < end && r->enabled_p; ++r)
7906 if (r->y >= y)
7908 struct glyph *g = r->glyphs[TEXT_AREA];
7909 struct glyph *end = g + r->used[TEXT_AREA];
7910 int gx;
7912 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7913 if (gx >= x)
7915 rect->width = g->pixel_width;
7916 rect->height = r->height;
7917 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7918 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7919 found = 1;
7924 return found;
7928 /* Return the current position of the mouse.
7929 *FP should be a frame which indicates which display to ask about.
7931 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7932 and *PART to the frame, window, and scroll bar part that the mouse
7933 is over. Set *X and *Y to the portion and whole of the mouse's
7934 position on the scroll bar.
7936 If the mouse movement started elsewhere, set *FP to the frame the
7937 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7938 the mouse is over.
7940 Set *TIME to the server time-stamp for the time at which the mouse
7941 was at this position.
7943 Don't store anything if we don't have a valid set of values to report.
7945 This clears the mouse_moved flag, so we can wait for the next mouse
7946 movement. */
7948 static void
7949 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7950 FRAME_PTR *fp;
7951 int insist;
7952 Lisp_Object *bar_window;
7953 enum scroll_bar_part *part;
7954 Lisp_Object *x, *y;
7955 unsigned long *time;
7957 FRAME_PTR f1;
7959 BLOCK_INPUT;
7961 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7962 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7963 else
7965 Window root;
7966 int root_x, root_y;
7968 Window dummy_window;
7969 int dummy;
7971 Lisp_Object frame, tail;
7973 /* Clear the mouse-moved flag for every frame on this display. */
7974 FOR_EACH_FRAME (tail, frame)
7975 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7976 XFRAME (frame)->mouse_moved = 0;
7978 last_mouse_scroll_bar = Qnil;
7980 /* Figure out which root window we're on. */
7981 XQueryPointer (FRAME_X_DISPLAY (*fp),
7982 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7984 /* The root window which contains the pointer. */
7985 &root,
7987 /* Trash which we can't trust if the pointer is on
7988 a different screen. */
7989 &dummy_window,
7991 /* The position on that root window. */
7992 &root_x, &root_y,
7994 /* More trash we can't trust. */
7995 &dummy, &dummy,
7997 /* Modifier keys and pointer buttons, about which
7998 we don't care. */
7999 (unsigned int *) &dummy);
8001 /* Now we have a position on the root; find the innermost window
8002 containing the pointer. */
8004 Window win, child;
8005 int win_x, win_y;
8006 int parent_x = 0, parent_y = 0;
8007 int count;
8009 win = root;
8011 /* XTranslateCoordinates can get errors if the window
8012 structure is changing at the same time this function
8013 is running. So at least we must not crash from them. */
8015 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8017 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8018 && FRAME_LIVE_P (last_mouse_frame))
8020 /* If mouse was grabbed on a frame, give coords for that frame
8021 even if the mouse is now outside it. */
8022 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8024 /* From-window, to-window. */
8025 root, FRAME_X_WINDOW (last_mouse_frame),
8027 /* From-position, to-position. */
8028 root_x, root_y, &win_x, &win_y,
8030 /* Child of win. */
8031 &child);
8032 f1 = last_mouse_frame;
8034 else
8036 while (1)
8038 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8040 /* From-window, to-window. */
8041 root, win,
8043 /* From-position, to-position. */
8044 root_x, root_y, &win_x, &win_y,
8046 /* Child of win. */
8047 &child);
8049 if (child == None || child == win)
8050 break;
8052 win = child;
8053 parent_x = win_x;
8054 parent_y = win_y;
8057 /* Now we know that:
8058 win is the innermost window containing the pointer
8059 (XTC says it has no child containing the pointer),
8060 win_x and win_y are the pointer's position in it
8061 (XTC did this the last time through), and
8062 parent_x and parent_y are the pointer's position in win's parent.
8063 (They are what win_x and win_y were when win was child.
8064 If win is the root window, it has no parent, and
8065 parent_{x,y} are invalid, but that's okay, because we'll
8066 never use them in that case.) */
8068 /* Is win one of our frames? */
8069 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8071 #ifdef USE_X_TOOLKIT
8072 /* If we end up with the menu bar window, say it's not
8073 on the frame. */
8074 if (f1 != NULL
8075 && f1->output_data.x->menubar_widget
8076 && win == XtWindow (f1->output_data.x->menubar_widget))
8077 f1 = NULL;
8078 #endif /* USE_X_TOOLKIT */
8081 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8082 f1 = 0;
8084 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8086 /* If not, is it one of our scroll bars? */
8087 if (! f1)
8089 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8091 if (bar)
8093 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8094 win_x = parent_x;
8095 win_y = parent_y;
8099 if (f1 == 0 && insist > 0)
8100 f1 = SELECTED_FRAME ();
8102 if (f1)
8104 /* Ok, we found a frame. Store all the values.
8105 last_mouse_glyph is a rectangle used to reduce the
8106 generation of mouse events. To not miss any motion
8107 events, we must divide the frame into rectangles of the
8108 size of the smallest character that could be displayed
8109 on it, i.e. into the same rectangles that matrices on
8110 the frame are divided into. */
8112 int width, height, gx, gy;
8113 XRectangle rect;
8115 if (glyph_rect (f1, win_x, win_y, &rect))
8116 last_mouse_glyph = rect;
8117 else
8119 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8120 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8121 gx = win_x;
8122 gy = win_y;
8124 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8125 round down even for negative values. */
8126 if (gx < 0)
8127 gx -= width - 1;
8128 if (gy < 0)
8129 gy -= height - 1;
8130 gx = (gx + width - 1) / width * width;
8131 gy = (gy + height - 1) / height * height;
8133 last_mouse_glyph.width = width;
8134 last_mouse_glyph.height = height;
8135 last_mouse_glyph.x = gx;
8136 last_mouse_glyph.y = gy;
8139 *bar_window = Qnil;
8140 *part = 0;
8141 *fp = f1;
8142 XSETINT (*x, win_x);
8143 XSETINT (*y, win_y);
8144 *time = last_mouse_movement_time;
8149 UNBLOCK_INPUT;
8153 #ifdef USE_X_TOOLKIT
8155 /* Atimer callback function for TIMER. Called every 0.1s to process
8156 Xt timeouts, if needed. We must avoid calling XtAppPending as
8157 much as possible because that function does an implicit XFlush
8158 that slows us down. */
8160 static void
8161 x_process_timeouts (timer)
8162 struct atimer *timer;
8164 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8166 BLOCK_INPUT;
8167 while (XtAppPending (Xt_app_con) & XtIMTimer)
8168 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8169 UNBLOCK_INPUT;
8173 #endif /* USE_X_TOOLKIT */
8176 /* Scroll bar support. */
8178 /* Given an X window ID, find the struct scroll_bar which manages it.
8179 This can be called in GC, so we have to make sure to strip off mark
8180 bits. */
8182 static struct scroll_bar *
8183 x_window_to_scroll_bar (window_id)
8184 Window window_id;
8186 Lisp_Object tail;
8188 for (tail = Vframe_list;
8189 XGCTYPE (tail) == Lisp_Cons;
8190 tail = XCDR (tail))
8192 Lisp_Object frame, bar, condemned;
8194 frame = XCAR (tail);
8195 /* All elements of Vframe_list should be frames. */
8196 if (! GC_FRAMEP (frame))
8197 abort ();
8199 /* Scan this frame's scroll bar list for a scroll bar with the
8200 right window ID. */
8201 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8202 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8203 /* This trick allows us to search both the ordinary and
8204 condemned scroll bar lists with one loop. */
8205 ! GC_NILP (bar) || (bar = condemned,
8206 condemned = Qnil,
8207 ! GC_NILP (bar));
8208 bar = XSCROLL_BAR (bar)->next)
8209 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8210 return XSCROLL_BAR (bar);
8213 return 0;
8217 #if defined USE_LUCID
8219 /* Return the Lucid menu bar WINDOW is part of. Return null
8220 if WINDOW is not part of a menu bar. */
8222 static Widget
8223 x_window_to_menu_bar (window)
8224 Window window;
8226 Lisp_Object tail;
8228 for (tail = Vframe_list;
8229 XGCTYPE (tail) == Lisp_Cons;
8230 tail = XCDR (tail))
8232 Lisp_Object frame = XCAR (tail);
8233 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8235 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8236 return menu_bar;
8239 return NULL;
8242 #endif /* USE_LUCID */
8245 /************************************************************************
8246 Toolkit scroll bars
8247 ************************************************************************/
8249 #ifdef USE_TOOLKIT_SCROLL_BARS
8251 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8252 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8253 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8254 struct scroll_bar *));
8255 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8256 int, int, int));
8259 /* Id of action hook installed for scroll bars. */
8261 static XtActionHookId action_hook_id;
8263 /* Lisp window being scrolled. Set when starting to interact with
8264 a toolkit scroll bar, reset to nil when ending the interaction. */
8266 static Lisp_Object window_being_scrolled;
8268 /* Last scroll bar part sent in xm_scroll_callback. */
8270 static int last_scroll_bar_part;
8272 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8273 that movements of 1/20 of the screen size are mapped to up/down. */
8275 static Boolean xaw3d_arrow_scroll;
8277 /* Whether the drag scrolling maintains the mouse at the top of the
8278 thumb. If not, resizing the thumb needs to be done more carefully
8279 to avoid jerkyness. */
8281 static Boolean xaw3d_pick_top;
8284 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8285 bars are used.. The hook is responsible for detecting when
8286 the user ends an interaction with the scroll bar, and generates
8287 a `end-scroll' scroll_bar_click' event if so. */
8289 static void
8290 xt_action_hook (widget, client_data, action_name, event, params,
8291 num_params)
8292 Widget widget;
8293 XtPointer client_data;
8294 String action_name;
8295 XEvent *event;
8296 String *params;
8297 Cardinal *num_params;
8299 int scroll_bar_p;
8300 char *end_action;
8302 #ifdef USE_MOTIF
8303 scroll_bar_p = XmIsScrollBar (widget);
8304 end_action = "Release";
8305 #else /* !USE_MOTIF i.e. use Xaw */
8306 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8307 end_action = "EndScroll";
8308 #endif /* USE_MOTIF */
8310 if (scroll_bar_p
8311 && strcmp (action_name, end_action) == 0
8312 && WINDOWP (window_being_scrolled))
8314 struct window *w;
8316 x_send_scroll_bar_event (window_being_scrolled,
8317 scroll_bar_end_scroll, 0, 0);
8318 w = XWINDOW (window_being_scrolled);
8319 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8320 window_being_scrolled = Qnil;
8321 last_scroll_bar_part = -1;
8323 /* Xt timeouts no longer needed. */
8324 toolkit_scroll_bar_interaction = 0;
8328 /* A vector of windows used for communication between
8329 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8331 static struct window **scroll_bar_windows;
8332 static int scroll_bar_windows_size;
8335 /* Send a client message with message type Xatom_Scrollbar for a
8336 scroll action to the frame of WINDOW. PART is a value identifying
8337 the part of the scroll bar that was clicked on. PORTION is the
8338 amount to scroll of a whole of WHOLE. */
8340 static void
8341 x_send_scroll_bar_event (window, part, portion, whole)
8342 Lisp_Object window;
8343 int part, portion, whole;
8345 XEvent event;
8346 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8347 struct window *w = XWINDOW (window);
8348 struct frame *f = XFRAME (w->frame);
8349 int i;
8351 BLOCK_INPUT;
8353 /* Construct a ClientMessage event to send to the frame. */
8354 ev->type = ClientMessage;
8355 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8356 ev->display = FRAME_X_DISPLAY (f);
8357 ev->window = FRAME_X_WINDOW (f);
8358 ev->format = 32;
8360 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8361 not enough to store a pointer or Lisp_Object on a 64 bit system.
8362 So, store the window in scroll_bar_windows and pass the index
8363 into that array in the event. */
8364 for (i = 0; i < scroll_bar_windows_size; ++i)
8365 if (scroll_bar_windows[i] == NULL)
8366 break;
8368 if (i == scroll_bar_windows_size)
8370 int new_size = max (10, 2 * scroll_bar_windows_size);
8371 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8372 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8374 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8375 nbytes);
8376 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8377 scroll_bar_windows_size = new_size;
8380 scroll_bar_windows[i] = w;
8381 ev->data.l[0] = (long) i;
8382 ev->data.l[1] = (long) part;
8383 ev->data.l[2] = (long) 0;
8384 ev->data.l[3] = (long) portion;
8385 ev->data.l[4] = (long) whole;
8387 /* Make Xt timeouts work while the scroll bar is active. */
8388 toolkit_scroll_bar_interaction = 1;
8390 /* Setting the event mask to zero means that the message will
8391 be sent to the client that created the window, and if that
8392 window no longer exists, no event will be sent. */
8393 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8394 UNBLOCK_INPUT;
8398 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8399 in *IEVENT. */
8401 static void
8402 x_scroll_bar_to_input_event (event, ievent)
8403 XEvent *event;
8404 struct input_event *ievent;
8406 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8407 Lisp_Object window;
8408 struct frame *f;
8409 struct window *w;
8411 w = scroll_bar_windows[ev->data.l[0]];
8412 scroll_bar_windows[ev->data.l[0]] = NULL;
8414 XSETWINDOW (window, w);
8415 f = XFRAME (w->frame);
8417 ievent->kind = scroll_bar_click;
8418 ievent->frame_or_window = window;
8419 ievent->arg = Qnil;
8420 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8421 ievent->part = ev->data.l[1];
8422 ievent->code = ev->data.l[2];
8423 ievent->x = make_number ((int) ev->data.l[3]);
8424 ievent->y = make_number ((int) ev->data.l[4]);
8425 ievent->modifiers = 0;
8429 #ifdef USE_MOTIF
8431 /* Minimum and maximum values used for Motif scroll bars. */
8433 #define XM_SB_MIN 1
8434 #define XM_SB_MAX 10000000
8435 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8438 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8439 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8440 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8442 static void
8443 xm_scroll_callback (widget, client_data, call_data)
8444 Widget widget;
8445 XtPointer client_data, call_data;
8447 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8448 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8449 int part = -1, whole = 0, portion = 0;
8451 switch (cs->reason)
8453 case XmCR_DECREMENT:
8454 bar->dragging = Qnil;
8455 part = scroll_bar_up_arrow;
8456 break;
8458 case XmCR_INCREMENT:
8459 bar->dragging = Qnil;
8460 part = scroll_bar_down_arrow;
8461 break;
8463 case XmCR_PAGE_DECREMENT:
8464 bar->dragging = Qnil;
8465 part = scroll_bar_above_handle;
8466 break;
8468 case XmCR_PAGE_INCREMENT:
8469 bar->dragging = Qnil;
8470 part = scroll_bar_below_handle;
8471 break;
8473 case XmCR_TO_TOP:
8474 bar->dragging = Qnil;
8475 part = scroll_bar_to_top;
8476 break;
8478 case XmCR_TO_BOTTOM:
8479 bar->dragging = Qnil;
8480 part = scroll_bar_to_bottom;
8481 break;
8483 case XmCR_DRAG:
8485 int slider_size;
8486 int dragging_down_p = (INTEGERP (bar->dragging)
8487 && XINT (bar->dragging) <= cs->value);
8489 /* Get the slider size. */
8490 BLOCK_INPUT;
8491 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8492 UNBLOCK_INPUT;
8494 /* At the max position of the scroll bar, do a line-wise
8495 movement. Without doing anything, we would be called with
8496 the same cs->value again and again. If we want to make
8497 sure that we can reach the end of the buffer, we have to do
8498 something.
8500 Implementation note: setting bar->dragging always to
8501 cs->value gives a smoother movement at the max position.
8502 Setting it to nil when doing line-wise movement gives
8503 a better slider behavior. */
8505 if (cs->value + slider_size == XM_SB_MAX
8506 || (dragging_down_p
8507 && last_scroll_bar_part == scroll_bar_down_arrow))
8509 part = scroll_bar_down_arrow;
8510 bar->dragging = Qnil;
8512 else
8514 whole = XM_SB_RANGE;
8515 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8516 part = scroll_bar_handle;
8517 bar->dragging = make_number (cs->value);
8520 break;
8522 case XmCR_VALUE_CHANGED:
8523 break;
8526 if (part >= 0)
8528 window_being_scrolled = bar->window;
8529 last_scroll_bar_part = part;
8530 x_send_scroll_bar_event (bar->window, part, portion, whole);
8535 #else /* !USE_MOTIF, i.e. Xaw. */
8538 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8539 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8540 scroll bar struct. CALL_DATA is a pointer to a float saying where
8541 the thumb is. */
8543 static void
8544 xaw_jump_callback (widget, client_data, call_data)
8545 Widget widget;
8546 XtPointer client_data, call_data;
8548 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8549 float top = *(float *) call_data;
8550 float shown;
8551 int whole, portion, height;
8552 int part;
8554 /* Get the size of the thumb, a value between 0 and 1. */
8555 BLOCK_INPUT;
8556 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8557 UNBLOCK_INPUT;
8559 whole = 10000000;
8560 portion = shown < 1 ? top * whole : 0;
8562 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8563 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8564 the bottom, so we force the scrolling whenever we see that we're
8565 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8566 we try to ensure that we always stay two pixels away from the
8567 bottom). */
8568 part = scroll_bar_down_arrow;
8569 else
8570 part = scroll_bar_handle;
8572 window_being_scrolled = bar->window;
8573 bar->dragging = make_number (portion);
8574 last_scroll_bar_part = part;
8575 x_send_scroll_bar_event (bar->window, part, portion, whole);
8579 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8580 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8581 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8582 the scroll bar. CALL_DATA is an integer specifying the action that
8583 has taken place. It's magnitude is in the range 0..height of the
8584 scroll bar. Negative values mean scroll towards buffer start.
8585 Values < height of scroll bar mean line-wise movement. */
8587 static void
8588 xaw_scroll_callback (widget, client_data, call_data)
8589 Widget widget;
8590 XtPointer client_data, call_data;
8592 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8593 int position = (int) call_data;
8594 Dimension height;
8595 int part;
8597 /* Get the height of the scroll bar. */
8598 BLOCK_INPUT;
8599 XtVaGetValues (widget, XtNheight, &height, NULL);
8600 UNBLOCK_INPUT;
8602 if (abs (position) >= height)
8603 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8605 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8606 it maps line-movement to call_data = max(5, height/20). */
8607 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8608 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8609 else
8610 part = scroll_bar_move_ratio;
8612 window_being_scrolled = bar->window;
8613 bar->dragging = Qnil;
8614 last_scroll_bar_part = part;
8615 x_send_scroll_bar_event (bar->window, part, position, height);
8619 #endif /* not USE_MOTIF */
8622 /* Create the widget for scroll bar BAR on frame F. Record the widget
8623 and X window of the scroll bar in BAR. */
8625 static void
8626 x_create_toolkit_scroll_bar (f, bar)
8627 struct frame *f;
8628 struct scroll_bar *bar;
8630 Window xwindow;
8631 Widget widget;
8632 Arg av[20];
8633 int ac = 0;
8634 char *scroll_bar_name = "verticalScrollBar";
8635 unsigned long pixel;
8637 BLOCK_INPUT;
8639 #ifdef USE_MOTIF
8640 /* Set resources. Create the widget. */
8641 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8642 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8643 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8644 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8645 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8646 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8647 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8649 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8650 if (pixel != -1)
8652 XtSetArg (av[ac], XmNforeground, pixel);
8653 ++ac;
8656 pixel = f->output_data.x->scroll_bar_background_pixel;
8657 if (pixel != -1)
8659 XtSetArg (av[ac], XmNbackground, pixel);
8660 ++ac;
8663 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8664 scroll_bar_name, av, ac);
8666 /* Add one callback for everything that can happen. */
8667 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8668 (XtPointer) bar);
8669 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8670 (XtPointer) bar);
8671 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8672 (XtPointer) bar);
8673 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8674 (XtPointer) bar);
8675 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8676 (XtPointer) bar);
8677 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8678 (XtPointer) bar);
8679 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8680 (XtPointer) bar);
8682 /* Realize the widget. Only after that is the X window created. */
8683 XtRealizeWidget (widget);
8685 /* Set the cursor to an arrow. I didn't find a resource to do that.
8686 And I'm wondering why it hasn't an arrow cursor by default. */
8687 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8688 f->output_data.x->nontext_cursor);
8690 #else /* !USE_MOTIF i.e. use Xaw */
8692 /* Set resources. Create the widget. The background of the
8693 Xaw3d scroll bar widget is a little bit light for my taste.
8694 We don't alter it here to let users change it according
8695 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8696 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8697 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8698 /* For smoother scrolling with Xaw3d -sm */
8699 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8701 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8702 if (pixel != -1)
8704 XtSetArg (av[ac], XtNforeground, pixel);
8705 ++ac;
8708 pixel = f->output_data.x->scroll_bar_background_pixel;
8709 if (pixel != -1)
8711 XtSetArg (av[ac], XtNbackground, pixel);
8712 ++ac;
8715 /* Top/bottom shadow colors. */
8717 /* Allocate them, if necessary. */
8718 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8720 pixel = f->output_data.x->scroll_bar_background_pixel;
8721 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8722 &pixel, 1.2, 0x8000))
8723 pixel = -1;
8724 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8726 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8728 pixel = f->output_data.x->scroll_bar_background_pixel;
8729 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8730 &pixel, 0.6, 0x4000))
8731 pixel = -1;
8732 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8735 /* Tell the toolkit about them. */
8736 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8737 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8738 /* We tried to allocate a color for the top/bottom shadow, and
8739 failed, so tell Xaw3d to use dithering instead. */
8741 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8742 ++ac;
8744 else
8745 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8746 be more consistent with other emacs 3d colors, and since Xaw3d is
8747 not good at dealing with allocation failure. */
8749 /* This tells Xaw3d to use real colors instead of dithering for
8750 the shadows. */
8751 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8752 ++ac;
8754 /* Specify the colors. */
8755 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8756 if (pixel != -1)
8758 XtSetArg (av[ac], "topShadowPixel", pixel);
8759 ++ac;
8761 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8762 if (pixel != -1)
8764 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8765 ++ac;
8769 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8770 f->output_data.x->edit_widget, av, ac);
8773 char *initial = "";
8774 char *val = initial;
8775 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8776 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8777 if (val == initial)
8778 { /* ARROW_SCROLL */
8779 xaw3d_arrow_scroll = True;
8780 /* Isn't that just a personal preference ? -sm */
8781 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8785 /* Define callbacks. */
8786 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8787 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8788 (XtPointer) bar);
8790 /* Realize the widget. Only after that is the X window created. */
8791 XtRealizeWidget (widget);
8793 #endif /* !USE_MOTIF */
8795 /* Install an action hook that let's us detect when the user
8796 finishes interacting with a scroll bar. */
8797 if (action_hook_id == 0)
8798 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8800 /* Remember X window and widget in the scroll bar vector. */
8801 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8802 xwindow = XtWindow (widget);
8803 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8805 UNBLOCK_INPUT;
8809 /* Set the thumb size and position of scroll bar BAR. We are currently
8810 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8812 static void
8813 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8814 struct scroll_bar *bar;
8815 int portion, position, whole;
8817 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8818 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8819 float top, shown;
8821 if (whole == 0)
8822 top = 0, shown = 1;
8823 else
8825 top = (float) position / whole;
8826 shown = (float) portion / whole;
8829 BLOCK_INPUT;
8831 #ifdef USE_MOTIF
8833 int size, value;
8835 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8836 is the scroll bar's maximum and MIN is the scroll bar's minimum
8837 value. */
8838 size = shown * XM_SB_RANGE;
8839 size = min (size, XM_SB_RANGE);
8840 size = max (size, 1);
8842 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8843 value = top * XM_SB_RANGE;
8844 value = min (value, XM_SB_MAX - size);
8845 value = max (value, XM_SB_MIN);
8847 if (NILP (bar->dragging))
8848 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8849 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8850 /* This has the negative side effect that the slider value is
8851 not what it would be if we scrolled here using line-wise or
8852 page-wise movement. */
8853 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8854 else
8856 /* If currently dragging, only update the slider size.
8857 This reduces flicker effects. */
8858 int old_value, old_size, increment, page_increment;
8860 XmScrollBarGetValues (widget, &old_value, &old_size,
8861 &increment, &page_increment);
8862 XmScrollBarSetValues (widget, old_value,
8863 min (size, XM_SB_RANGE - old_value),
8864 0, 0, False);
8867 #else /* !USE_MOTIF i.e. use Xaw */
8869 float old_top, old_shown;
8870 Dimension height;
8871 XtVaGetValues (widget,
8872 XtNtopOfThumb, &old_top,
8873 XtNshown, &old_shown,
8874 XtNheight, &height,
8875 NULL);
8877 /* Massage the top+shown values. */
8878 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8879 top = max (0, min (1, top));
8880 else
8881 top = old_top;
8882 /* Keep two pixels available for moving the thumb down. */
8883 shown = max (0, min (1 - top - (2.0 / height), shown));
8885 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8886 check that your system's configuration file contains a define
8887 for `NARROWPROTO'. See s/freebsd.h for an example. */
8888 if (top != old_top || shown != old_shown)
8890 if (NILP (bar->dragging))
8891 XawScrollbarSetThumb (widget, top, shown);
8892 else
8894 #ifdef HAVE_XAW3D
8895 ScrollbarWidget sb = (ScrollbarWidget) widget;
8896 int scroll_mode = 0;
8898 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8899 if (xaw3d_arrow_scroll)
8901 /* Xaw3d stupidly ignores resize requests while dragging
8902 so we have to make it believe it's not in dragging mode. */
8903 scroll_mode = sb->scrollbar.scroll_mode;
8904 if (scroll_mode == 2)
8905 sb->scrollbar.scroll_mode = 0;
8907 #endif
8908 /* Try to make the scrolling a tad smoother. */
8909 if (!xaw3d_pick_top)
8910 shown = min (shown, old_shown);
8912 XawScrollbarSetThumb (widget, top, shown);
8914 #ifdef HAVE_XAW3D
8915 if (xaw3d_arrow_scroll && scroll_mode == 2)
8916 sb->scrollbar.scroll_mode = scroll_mode;
8917 #endif
8921 #endif /* !USE_MOTIF */
8923 UNBLOCK_INPUT;
8926 #endif /* USE_TOOLKIT_SCROLL_BARS */
8930 /************************************************************************
8931 Scroll bars, general
8932 ************************************************************************/
8934 /* Create a scroll bar and return the scroll bar vector for it. W is
8935 the Emacs window on which to create the scroll bar. TOP, LEFT,
8936 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8937 scroll bar. */
8939 static struct scroll_bar *
8940 x_scroll_bar_create (w, top, left, width, height)
8941 struct window *w;
8942 int top, left, width, height;
8944 struct frame *f = XFRAME (w->frame);
8945 struct scroll_bar *bar
8946 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8948 BLOCK_INPUT;
8950 #ifdef USE_TOOLKIT_SCROLL_BARS
8951 x_create_toolkit_scroll_bar (f, bar);
8952 #else /* not USE_TOOLKIT_SCROLL_BARS */
8954 XSetWindowAttributes a;
8955 unsigned long mask;
8956 Window window;
8958 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8959 if (a.background_pixel == -1)
8960 a.background_pixel = f->output_data.x->background_pixel;
8962 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8963 | ButtonMotionMask | PointerMotionHintMask
8964 | ExposureMask);
8965 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8967 mask = (CWBackPixel | CWEventMask | CWCursor);
8969 /* Clear the area of W that will serve as a scroll bar. This is
8970 for the case that a window has been split horizontally. In
8971 this case, no clear_frame is generated to reduce flickering. */
8972 if (width > 0 && height > 0)
8973 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8974 left, top, width,
8975 window_box_height (w), False);
8977 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8978 /* Position and size of scroll bar. */
8979 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8980 top,
8981 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8982 height,
8983 /* Border width, depth, class, and visual. */
8985 CopyFromParent,
8986 CopyFromParent,
8987 CopyFromParent,
8988 /* Attributes. */
8989 mask, &a);
8990 SET_SCROLL_BAR_X_WINDOW (bar, window);
8992 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8994 XSETWINDOW (bar->window, w);
8995 XSETINT (bar->top, top);
8996 XSETINT (bar->left, left);
8997 XSETINT (bar->width, width);
8998 XSETINT (bar->height, height);
8999 XSETINT (bar->start, 0);
9000 XSETINT (bar->end, 0);
9001 bar->dragging = Qnil;
9003 /* Add bar to its frame's list of scroll bars. */
9004 bar->next = FRAME_SCROLL_BARS (f);
9005 bar->prev = Qnil;
9006 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9007 if (!NILP (bar->next))
9008 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9010 /* Map the window/widget. */
9011 #ifdef USE_TOOLKIT_SCROLL_BARS
9013 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9014 XtConfigureWidget (scroll_bar,
9015 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9016 top,
9017 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9018 max (height, 1), 0);
9019 XtMapWidget (scroll_bar);
9021 #else /* not USE_TOOLKIT_SCROLL_BARS */
9022 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9023 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9025 UNBLOCK_INPUT;
9026 return bar;
9030 /* Draw BAR's handle in the proper position.
9032 If the handle is already drawn from START to END, don't bother
9033 redrawing it, unless REBUILD is non-zero; in that case, always
9034 redraw it. (REBUILD is handy for drawing the handle after expose
9035 events.)
9037 Normally, we want to constrain the start and end of the handle to
9038 fit inside its rectangle, but if the user is dragging the scroll
9039 bar handle, we want to let them drag it down all the way, so that
9040 the bar's top is as far down as it goes; otherwise, there's no way
9041 to move to the very end of the buffer. */
9043 #ifndef USE_TOOLKIT_SCROLL_BARS
9045 static void
9046 x_scroll_bar_set_handle (bar, start, end, rebuild)
9047 struct scroll_bar *bar;
9048 int start, end;
9049 int rebuild;
9051 int dragging = ! NILP (bar->dragging);
9052 Window w = SCROLL_BAR_X_WINDOW (bar);
9053 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9054 GC gc = f->output_data.x->normal_gc;
9056 /* If the display is already accurate, do nothing. */
9057 if (! rebuild
9058 && start == XINT (bar->start)
9059 && end == XINT (bar->end))
9060 return;
9062 BLOCK_INPUT;
9065 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9066 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9067 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9069 /* Make sure the values are reasonable, and try to preserve
9070 the distance between start and end. */
9072 int length = end - start;
9074 if (start < 0)
9075 start = 0;
9076 else if (start > top_range)
9077 start = top_range;
9078 end = start + length;
9080 if (end < start)
9081 end = start;
9082 else if (end > top_range && ! dragging)
9083 end = top_range;
9086 /* Store the adjusted setting in the scroll bar. */
9087 XSETINT (bar->start, start);
9088 XSETINT (bar->end, end);
9090 /* Clip the end position, just for display. */
9091 if (end > top_range)
9092 end = top_range;
9094 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9095 below top positions, to make sure the handle is always at least
9096 that many pixels tall. */
9097 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9099 /* Draw the empty space above the handle. Note that we can't clear
9100 zero-height areas; that means "clear to end of window." */
9101 if (0 < start)
9102 x_clear_area (FRAME_X_DISPLAY (f), w,
9103 /* x, y, width, height, and exposures. */
9104 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9105 VERTICAL_SCROLL_BAR_TOP_BORDER,
9106 inside_width, start,
9107 False);
9109 /* Change to proper foreground color if one is specified. */
9110 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9111 XSetForeground (FRAME_X_DISPLAY (f), gc,
9112 f->output_data.x->scroll_bar_foreground_pixel);
9114 /* Draw the handle itself. */
9115 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9116 /* x, y, width, height */
9117 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9118 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9119 inside_width, end - start);
9121 /* Restore the foreground color of the GC if we changed it above. */
9122 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9123 XSetForeground (FRAME_X_DISPLAY (f), gc,
9124 f->output_data.x->foreground_pixel);
9126 /* Draw the empty space below the handle. Note that we can't
9127 clear zero-height areas; that means "clear to end of window." */
9128 if (end < inside_height)
9129 x_clear_area (FRAME_X_DISPLAY (f), w,
9130 /* x, y, width, height, and exposures. */
9131 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9132 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9133 inside_width, inside_height - end,
9134 False);
9138 UNBLOCK_INPUT;
9141 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9143 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9144 nil. */
9146 static void
9147 x_scroll_bar_remove (bar)
9148 struct scroll_bar *bar;
9150 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9151 BLOCK_INPUT;
9153 #ifdef USE_TOOLKIT_SCROLL_BARS
9154 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9155 #else
9156 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9157 #endif
9159 /* Disassociate this scroll bar from its window. */
9160 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9162 UNBLOCK_INPUT;
9166 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9167 that we are displaying PORTION characters out of a total of WHOLE
9168 characters, starting at POSITION. If WINDOW has no scroll bar,
9169 create one. */
9171 static void
9172 XTset_vertical_scroll_bar (w, portion, whole, position)
9173 struct window *w;
9174 int portion, whole, position;
9176 struct frame *f = XFRAME (w->frame);
9177 struct scroll_bar *bar;
9178 int top, height, left, sb_left, width, sb_width;
9179 int window_x, window_y, window_width, window_height;
9181 /* Get window dimensions. */
9182 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9183 top = window_y;
9184 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9185 height = window_height;
9187 /* Compute the left edge of the scroll bar area. */
9188 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9189 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9190 else
9191 left = XFASTINT (w->left);
9192 left *= CANON_X_UNIT (f);
9193 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9195 /* Compute the width of the scroll bar which might be less than
9196 the width of the area reserved for the scroll bar. */
9197 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9198 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9199 else
9200 sb_width = width;
9202 /* Compute the left edge of the scroll bar. */
9203 #ifdef USE_TOOLKIT_SCROLL_BARS
9204 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9205 sb_left = left + width - sb_width - (width - sb_width) / 2;
9206 else
9207 sb_left = left + (width - sb_width) / 2;
9208 #else
9209 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9210 sb_left = left + width - sb_width;
9211 else
9212 sb_left = left;
9213 #endif
9215 /* Does the scroll bar exist yet? */
9216 if (NILP (w->vertical_scroll_bar))
9218 if (width > 0 && height > 0)
9220 BLOCK_INPUT;
9221 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9222 left, top, width, height, False);
9223 UNBLOCK_INPUT;
9226 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9228 else
9230 /* It may just need to be moved and resized. */
9231 unsigned int mask = 0;
9233 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9235 BLOCK_INPUT;
9237 if (sb_left != XINT (bar->left))
9238 mask |= CWX;
9239 if (top != XINT (bar->top))
9240 mask |= CWY;
9241 if (sb_width != XINT (bar->width))
9242 mask |= CWWidth;
9243 if (height != XINT (bar->height))
9244 mask |= CWHeight;
9246 #ifdef USE_TOOLKIT_SCROLL_BARS
9248 /* Since toolkit scroll bars are smaller than the space reserved
9249 for them on the frame, we have to clear "under" them. */
9250 if (width > 0 && height > 0)
9251 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9252 left, top, width, height, False);
9254 /* Move/size the scroll bar widget. */
9255 if (mask)
9256 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9257 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9258 top,
9259 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9260 max (height, 1), 0);
9262 #else /* not USE_TOOLKIT_SCROLL_BARS */
9264 /* Clear areas not covered by the scroll bar because of
9265 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9266 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9268 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9269 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9270 height, False);
9271 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9272 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9273 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9274 height, False);
9277 /* Clear areas not covered by the scroll bar because it's not as
9278 wide as the area reserved for it . This makes sure a
9279 previous mode line display is cleared after C-x 2 C-x 1, for
9280 example. */
9282 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9283 int rest = area_width - sb_width;
9284 if (rest > 0 && height > 0)
9286 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9287 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9288 left + area_width - rest, top,
9289 rest, height, False);
9290 else
9291 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9292 left, top, rest, height, False);
9296 /* Move/size the scroll bar window. */
9297 if (mask)
9299 XWindowChanges wc;
9301 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9302 wc.y = top;
9303 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9304 wc.height = height;
9305 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9306 mask, &wc);
9309 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9311 /* Remember new settings. */
9312 XSETINT (bar->left, sb_left);
9313 XSETINT (bar->top, top);
9314 XSETINT (bar->width, sb_width);
9315 XSETINT (bar->height, height);
9317 UNBLOCK_INPUT;
9320 #ifdef USE_TOOLKIT_SCROLL_BARS
9321 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9322 #else /* not USE_TOOLKIT_SCROLL_BARS */
9323 /* Set the scroll bar's current state, unless we're currently being
9324 dragged. */
9325 if (NILP (bar->dragging))
9327 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9329 if (whole == 0)
9330 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9331 else
9333 int start = ((double) position * top_range) / whole;
9334 int end = ((double) (position + portion) * top_range) / whole;
9335 x_scroll_bar_set_handle (bar, start, end, 0);
9338 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9340 XSETVECTOR (w->vertical_scroll_bar, bar);
9344 /* The following three hooks are used when we're doing a thorough
9345 redisplay of the frame. We don't explicitly know which scroll bars
9346 are going to be deleted, because keeping track of when windows go
9347 away is a real pain - "Can you say set-window-configuration, boys
9348 and girls?" Instead, we just assert at the beginning of redisplay
9349 that *all* scroll bars are to be removed, and then save a scroll bar
9350 from the fiery pit when we actually redisplay its window. */
9352 /* Arrange for all scroll bars on FRAME to be removed at the next call
9353 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9354 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9356 static void
9357 XTcondemn_scroll_bars (frame)
9358 FRAME_PTR frame;
9360 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9361 while (! NILP (FRAME_SCROLL_BARS (frame)))
9363 Lisp_Object bar;
9364 bar = FRAME_SCROLL_BARS (frame);
9365 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9366 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9367 XSCROLL_BAR (bar)->prev = Qnil;
9368 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9369 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9370 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9375 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9376 Note that WINDOW isn't necessarily condemned at all. */
9378 static void
9379 XTredeem_scroll_bar (window)
9380 struct window *window;
9382 struct scroll_bar *bar;
9383 struct frame *f;
9385 /* We can't redeem this window's scroll bar if it doesn't have one. */
9386 if (NILP (window->vertical_scroll_bar))
9387 abort ();
9389 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9391 /* Unlink it from the condemned list. */
9392 f = XFRAME (WINDOW_FRAME (window));
9393 if (NILP (bar->prev))
9395 /* If the prev pointer is nil, it must be the first in one of
9396 the lists. */
9397 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9398 /* It's not condemned. Everything's fine. */
9399 return;
9400 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9401 window->vertical_scroll_bar))
9402 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9403 else
9404 /* If its prev pointer is nil, it must be at the front of
9405 one or the other! */
9406 abort ();
9408 else
9409 XSCROLL_BAR (bar->prev)->next = bar->next;
9411 if (! NILP (bar->next))
9412 XSCROLL_BAR (bar->next)->prev = bar->prev;
9414 bar->next = FRAME_SCROLL_BARS (f);
9415 bar->prev = Qnil;
9416 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9417 if (! NILP (bar->next))
9418 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9421 /* Remove all scroll bars on FRAME that haven't been saved since the
9422 last call to `*condemn_scroll_bars_hook'. */
9424 static void
9425 XTjudge_scroll_bars (f)
9426 FRAME_PTR f;
9428 Lisp_Object bar, next;
9430 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9432 /* Clear out the condemned list now so we won't try to process any
9433 more events on the hapless scroll bars. */
9434 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9436 for (; ! NILP (bar); bar = next)
9438 struct scroll_bar *b = XSCROLL_BAR (bar);
9440 x_scroll_bar_remove (b);
9442 next = b->next;
9443 b->next = b->prev = Qnil;
9446 /* Now there should be no references to the condemned scroll bars,
9447 and they should get garbage-collected. */
9451 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9452 is a no-op when using toolkit scroll bars.
9454 This may be called from a signal handler, so we have to ignore GC
9455 mark bits. */
9457 static void
9458 x_scroll_bar_expose (bar, event)
9459 struct scroll_bar *bar;
9460 XEvent *event;
9462 #ifndef USE_TOOLKIT_SCROLL_BARS
9464 Window w = SCROLL_BAR_X_WINDOW (bar);
9465 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9466 GC gc = f->output_data.x->normal_gc;
9467 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9469 BLOCK_INPUT;
9471 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9473 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9474 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9476 /* x, y, width, height */
9477 0, 0,
9478 XINT (bar->width) - 1 - width_trim - width_trim,
9479 XINT (bar->height) - 1);
9481 UNBLOCK_INPUT;
9483 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9486 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9487 is set to something other than no_event, it is enqueued.
9489 This may be called from a signal handler, so we have to ignore GC
9490 mark bits. */
9492 #ifndef USE_TOOLKIT_SCROLL_BARS
9494 static void
9495 x_scroll_bar_handle_click (bar, event, emacs_event)
9496 struct scroll_bar *bar;
9497 XEvent *event;
9498 struct input_event *emacs_event;
9500 if (! GC_WINDOWP (bar->window))
9501 abort ();
9503 emacs_event->kind = scroll_bar_click;
9504 emacs_event->code = event->xbutton.button - Button1;
9505 emacs_event->modifiers
9506 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9507 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9508 event->xbutton.state)
9509 | (event->type == ButtonRelease
9510 ? up_modifier
9511 : down_modifier));
9512 emacs_event->frame_or_window = bar->window;
9513 emacs_event->arg = Qnil;
9514 emacs_event->timestamp = event->xbutton.time;
9516 #if 0
9517 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9518 int internal_height
9519 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9520 #endif
9521 int top_range
9522 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9523 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9525 if (y < 0) y = 0;
9526 if (y > top_range) y = top_range;
9528 if (y < XINT (bar->start))
9529 emacs_event->part = scroll_bar_above_handle;
9530 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9531 emacs_event->part = scroll_bar_handle;
9532 else
9533 emacs_event->part = scroll_bar_below_handle;
9535 /* Just because the user has clicked on the handle doesn't mean
9536 they want to drag it. Lisp code needs to be able to decide
9537 whether or not we're dragging. */
9538 #if 0
9539 /* If the user has just clicked on the handle, record where they're
9540 holding it. */
9541 if (event->type == ButtonPress
9542 && emacs_event->part == scroll_bar_handle)
9543 XSETINT (bar->dragging, y - XINT (bar->start));
9544 #endif
9546 /* If the user has released the handle, set it to its final position. */
9547 if (event->type == ButtonRelease
9548 && ! NILP (bar->dragging))
9550 int new_start = y - XINT (bar->dragging);
9551 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9553 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9554 bar->dragging = Qnil;
9557 /* Same deal here as the other #if 0. */
9558 #if 0
9559 /* Clicks on the handle are always reported as occurring at the top of
9560 the handle. */
9561 if (emacs_event->part == scroll_bar_handle)
9562 emacs_event->x = bar->start;
9563 else
9564 XSETINT (emacs_event->x, y);
9565 #else
9566 XSETINT (emacs_event->x, y);
9567 #endif
9569 XSETINT (emacs_event->y, top_range);
9573 /* Handle some mouse motion while someone is dragging the scroll bar.
9575 This may be called from a signal handler, so we have to ignore GC
9576 mark bits. */
9578 static void
9579 x_scroll_bar_note_movement (bar, event)
9580 struct scroll_bar *bar;
9581 XEvent *event;
9583 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9585 last_mouse_movement_time = event->xmotion.time;
9587 f->mouse_moved = 1;
9588 XSETVECTOR (last_mouse_scroll_bar, bar);
9590 /* If we're dragging the bar, display it. */
9591 if (! GC_NILP (bar->dragging))
9593 /* Where should the handle be now? */
9594 int new_start = event->xmotion.y - XINT (bar->dragging);
9596 if (new_start != XINT (bar->start))
9598 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9600 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9605 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9607 /* Return information to the user about the current position of the mouse
9608 on the scroll bar. */
9610 static void
9611 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9612 FRAME_PTR *fp;
9613 Lisp_Object *bar_window;
9614 enum scroll_bar_part *part;
9615 Lisp_Object *x, *y;
9616 unsigned long *time;
9618 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9619 Window w = SCROLL_BAR_X_WINDOW (bar);
9620 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9621 int win_x, win_y;
9622 Window dummy_window;
9623 int dummy_coord;
9624 unsigned int dummy_mask;
9626 BLOCK_INPUT;
9628 /* Get the mouse's position relative to the scroll bar window, and
9629 report that. */
9630 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9632 /* Root, child, root x and root y. */
9633 &dummy_window, &dummy_window,
9634 &dummy_coord, &dummy_coord,
9636 /* Position relative to scroll bar. */
9637 &win_x, &win_y,
9639 /* Mouse buttons and modifier keys. */
9640 &dummy_mask))
9642 else
9644 #if 0
9645 int inside_height
9646 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9647 #endif
9648 int top_range
9649 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9651 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9653 if (! NILP (bar->dragging))
9654 win_y -= XINT (bar->dragging);
9656 if (win_y < 0)
9657 win_y = 0;
9658 if (win_y > top_range)
9659 win_y = top_range;
9661 *fp = f;
9662 *bar_window = bar->window;
9664 if (! NILP (bar->dragging))
9665 *part = scroll_bar_handle;
9666 else if (win_y < XINT (bar->start))
9667 *part = scroll_bar_above_handle;
9668 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9669 *part = scroll_bar_handle;
9670 else
9671 *part = scroll_bar_below_handle;
9673 XSETINT (*x, win_y);
9674 XSETINT (*y, top_range);
9676 f->mouse_moved = 0;
9677 last_mouse_scroll_bar = Qnil;
9680 *time = last_mouse_movement_time;
9682 UNBLOCK_INPUT;
9686 /* The screen has been cleared so we may have changed foreground or
9687 background colors, and the scroll bars may need to be redrawn.
9688 Clear out the scroll bars, and ask for expose events, so we can
9689 redraw them. */
9691 void
9692 x_scroll_bar_clear (f)
9693 FRAME_PTR f;
9695 #ifndef USE_TOOLKIT_SCROLL_BARS
9696 Lisp_Object bar;
9698 /* We can have scroll bars even if this is 0,
9699 if we just turned off scroll bar mode.
9700 But in that case we should not clear them. */
9701 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9702 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9703 bar = XSCROLL_BAR (bar)->next)
9704 XClearArea (FRAME_X_DISPLAY (f),
9705 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9706 0, 0, 0, 0, True);
9707 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9710 /* This processes Expose events from the menu-bar specific X event
9711 loop in xmenu.c. This allows to redisplay the frame if necessary
9712 when handling menu-bar or pop-up items. */
9715 process_expose_from_menu (event)
9716 XEvent event;
9718 FRAME_PTR f;
9719 struct x_display_info *dpyinfo;
9720 int frame_exposed_p = 0;
9722 BLOCK_INPUT;
9724 dpyinfo = x_display_info_for_display (event.xexpose.display);
9725 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9726 if (f)
9728 if (f->async_visible == 0)
9730 f->async_visible = 1;
9731 f->async_iconified = 0;
9732 f->output_data.x->has_been_visible = 1;
9733 SET_FRAME_GARBAGED (f);
9735 else
9737 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9738 event.xexpose.x, event.xexpose.y,
9739 event.xexpose.width, event.xexpose.height);
9740 frame_exposed_p = 1;
9743 else
9745 struct scroll_bar *bar
9746 = x_window_to_scroll_bar (event.xexpose.window);
9748 if (bar)
9749 x_scroll_bar_expose (bar, &event);
9752 UNBLOCK_INPUT;
9753 return frame_exposed_p;
9756 /* Define a queue to save up SelectionRequest events for later handling. */
9758 struct selection_event_queue
9760 XEvent event;
9761 struct selection_event_queue *next;
9764 static struct selection_event_queue *queue;
9766 /* Nonzero means queue up certain events--don't process them yet. */
9768 static int x_queue_selection_requests;
9770 /* Queue up an X event *EVENT, to be processed later. */
9772 static void
9773 x_queue_event (f, event)
9774 FRAME_PTR f;
9775 XEvent *event;
9777 struct selection_event_queue *queue_tmp
9778 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9780 if (queue_tmp != NULL)
9782 queue_tmp->event = *event;
9783 queue_tmp->next = queue;
9784 queue = queue_tmp;
9788 /* Take all the queued events and put them back
9789 so that they get processed afresh. */
9791 static void
9792 x_unqueue_events (display)
9793 Display *display;
9795 while (queue != NULL)
9797 struct selection_event_queue *queue_tmp = queue;
9798 XPutBackEvent (display, &queue_tmp->event);
9799 queue = queue_tmp->next;
9800 xfree ((char *)queue_tmp);
9804 /* Start queuing SelectionRequest events. */
9806 void
9807 x_start_queuing_selection_requests (display)
9808 Display *display;
9810 x_queue_selection_requests++;
9813 /* Stop queuing SelectionRequest events. */
9815 void
9816 x_stop_queuing_selection_requests (display)
9817 Display *display;
9819 x_queue_selection_requests--;
9820 x_unqueue_events (display);
9823 /* The main X event-reading loop - XTread_socket. */
9825 /* Time stamp of enter window event. This is only used by XTread_socket,
9826 but we have to put it out here, since static variables within functions
9827 sometimes don't work. */
9829 static Time enter_timestamp;
9831 /* This holds the state XLookupString needs to implement dead keys
9832 and other tricks known as "compose processing". _X Window System_
9833 says that a portable program can't use this, but Stephen Gildea assures
9834 me that letting the compiler initialize it to zeros will work okay.
9836 This must be defined outside of XTread_socket, for the same reasons
9837 given for enter_timestamp, above. */
9839 static XComposeStatus compose_status;
9841 /* Record the last 100 characters stored
9842 to help debug the loss-of-chars-during-GC problem. */
9844 static int temp_index;
9845 static short temp_buffer[100];
9847 /* Set this to nonzero to fake an "X I/O error"
9848 on a particular display. */
9850 struct x_display_info *XTread_socket_fake_io_error;
9852 /* When we find no input here, we occasionally do a no-op command
9853 to verify that the X server is still running and we can still talk with it.
9854 We try all the open displays, one by one.
9855 This variable is used for cycling thru the displays. */
9857 static struct x_display_info *next_noop_dpyinfo;
9859 #define SET_SAVED_MENU_EVENT(size) \
9860 do \
9862 if (f->output_data.x->saved_menu_event == 0) \
9863 f->output_data.x->saved_menu_event \
9864 = (XEvent *) xmalloc (sizeof (XEvent)); \
9865 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9866 if (numchars >= 1) \
9868 bufp->kind = menu_bar_activate_event; \
9869 XSETFRAME (bufp->frame_or_window, f); \
9870 bufp->arg = Qnil; \
9871 bufp++; \
9872 count++; \
9873 numchars--; \
9876 while (0)
9878 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9879 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9881 /* Read events coming from the X server.
9882 This routine is called by the SIGIO handler.
9883 We return as soon as there are no more events to be read.
9885 Events representing keys are stored in buffer BUFP,
9886 which can hold up to NUMCHARS characters.
9887 We return the number of characters stored into the buffer,
9888 thus pretending to be `read'.
9890 EXPECTED is nonzero if the caller knows input is available. */
9893 XTread_socket (sd, bufp, numchars, expected)
9894 register int sd;
9895 /* register */ struct input_event *bufp;
9896 /* register */ int numchars;
9897 int expected;
9899 int count = 0;
9900 int nbytes = 0;
9901 XEvent event;
9902 struct frame *f;
9903 int event_found = 0;
9904 struct x_display_info *dpyinfo;
9905 struct coding_system coding;
9907 if (interrupt_input_blocked)
9909 interrupt_input_pending = 1;
9910 return -1;
9913 interrupt_input_pending = 0;
9914 BLOCK_INPUT;
9916 /* So people can tell when we have read the available input. */
9917 input_signal_count++;
9919 if (numchars <= 0)
9920 abort (); /* Don't think this happens. */
9922 ++handling_signal;
9924 /* The input should be decoded if it is from XIM. Currently the
9925 locale of XIM is the same as that of the system. So, we can use
9926 Vlocale_coding_system which is initialized properly at Emacs
9927 startup time. */
9928 setup_coding_system (Vlocale_coding_system, &coding);
9929 coding.src_multibyte = 0;
9930 coding.dst_multibyte = 1;
9931 /* The input is converted to events, thus we can't handle
9932 composition. Anyway, there's no XIM that gives us composition
9933 information. */
9934 coding.composing = COMPOSITION_DISABLED;
9936 /* Find the display we are supposed to read input for.
9937 It's the one communicating on descriptor SD. */
9938 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9940 #if 0 /* This ought to be unnecessary; let's verify it. */
9941 #ifdef FIOSNBIO
9942 /* If available, Xlib uses FIOSNBIO to make the socket
9943 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9944 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9945 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9946 fcntl (dpyinfo->connection, F_SETFL, 0);
9947 #endif /* ! defined (FIOSNBIO) */
9948 #endif
9950 #if 0 /* This code can't be made to work, with multiple displays,
9951 and appears not to be used on any system any more.
9952 Also keyboard.c doesn't turn O_NDELAY on and off
9953 for X connections. */
9954 #ifndef SIGIO
9955 #ifndef HAVE_SELECT
9956 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9958 extern int read_alarm_should_throw;
9959 read_alarm_should_throw = 1;
9960 XPeekEvent (dpyinfo->display, &event);
9961 read_alarm_should_throw = 0;
9963 #endif /* HAVE_SELECT */
9964 #endif /* SIGIO */
9965 #endif
9967 /* For debugging, this gives a way to fake an I/O error. */
9968 if (dpyinfo == XTread_socket_fake_io_error)
9970 XTread_socket_fake_io_error = 0;
9971 x_io_error_quitter (dpyinfo->display);
9974 while (XPending (dpyinfo->display))
9976 XNextEvent (dpyinfo->display, &event);
9978 #ifdef HAVE_X_I18N
9980 /* Filter events for the current X input method.
9981 XFilterEvent returns non-zero if the input method has
9982 consumed the event. We pass the frame's X window to
9983 XFilterEvent because that's the one for which the IC
9984 was created. */
9985 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9986 event.xclient.window);
9987 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9988 break;
9990 #endif
9991 event_found = 1;
9993 switch (event.type)
9995 case ClientMessage:
9997 if (event.xclient.message_type
9998 == dpyinfo->Xatom_wm_protocols
9999 && event.xclient.format == 32)
10001 if (event.xclient.data.l[0]
10002 == dpyinfo->Xatom_wm_take_focus)
10004 /* Use x_any_window_to_frame because this
10005 could be the shell widget window
10006 if the frame has no title bar. */
10007 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10008 #ifdef HAVE_X_I18N
10009 /* Not quite sure this is needed -pd */
10010 if (f && FRAME_XIC (f))
10011 XSetICFocus (FRAME_XIC (f));
10012 #endif
10013 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10014 instructs the WM to set the input focus automatically for
10015 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10016 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10017 it has set the focus. So, XSetInputFocus below is not
10018 needed.
10020 The call to XSetInputFocus below has also caused trouble. In
10021 cases where the XSetInputFocus done by the WM and the one
10022 below are temporally close (on a fast machine), the call
10023 below can generate additional FocusIn events which confuse
10024 Emacs. */
10026 /* Since we set WM_TAKE_FOCUS, we must call
10027 XSetInputFocus explicitly. But not if f is null,
10028 since that might be an event for a deleted frame. */
10029 if (f)
10031 Display *d = event.xclient.display;
10032 /* Catch and ignore errors, in case window has been
10033 iconified by a window manager such as GWM. */
10034 int count = x_catch_errors (d);
10035 XSetInputFocus (d, event.xclient.window,
10036 /* The ICCCM says this is
10037 the only valid choice. */
10038 RevertToParent,
10039 event.xclient.data.l[1]);
10040 /* This is needed to detect the error
10041 if there is an error. */
10042 XSync (d, False);
10043 x_uncatch_errors (d, count);
10045 /* Not certain about handling scroll bars here */
10046 #endif /* 0 */
10048 else if (event.xclient.data.l[0]
10049 == dpyinfo->Xatom_wm_save_yourself)
10051 /* Save state modify the WM_COMMAND property to
10052 something which can reinstate us. This notifies
10053 the session manager, who's looking for such a
10054 PropertyNotify. Can restart processing when
10055 a keyboard or mouse event arrives. */
10056 if (numchars > 0)
10058 f = x_top_window_to_frame (dpyinfo,
10059 event.xclient.window);
10061 /* This is just so we only give real data once
10062 for a single Emacs process. */
10063 if (f == SELECTED_FRAME ())
10064 XSetCommand (FRAME_X_DISPLAY (f),
10065 event.xclient.window,
10066 initial_argv, initial_argc);
10067 else if (f)
10068 XSetCommand (FRAME_X_DISPLAY (f),
10069 event.xclient.window,
10070 0, 0);
10073 else if (event.xclient.data.l[0]
10074 == dpyinfo->Xatom_wm_delete_window)
10076 struct frame *f
10077 = x_any_window_to_frame (dpyinfo,
10078 event.xclient.window);
10080 if (f)
10082 if (numchars == 0)
10083 abort ();
10085 bufp->kind = delete_window_event;
10086 XSETFRAME (bufp->frame_or_window, f);
10087 bufp->arg = Qnil;
10088 bufp++;
10090 count += 1;
10091 numchars -= 1;
10095 else if (event.xclient.message_type
10096 == dpyinfo->Xatom_wm_configure_denied)
10099 else if (event.xclient.message_type
10100 == dpyinfo->Xatom_wm_window_moved)
10102 int new_x, new_y;
10103 struct frame *f
10104 = x_window_to_frame (dpyinfo, event.xclient.window);
10106 new_x = event.xclient.data.s[0];
10107 new_y = event.xclient.data.s[1];
10109 if (f)
10111 f->output_data.x->left_pos = new_x;
10112 f->output_data.x->top_pos = new_y;
10115 #ifdef HACK_EDITRES
10116 else if (event.xclient.message_type
10117 == dpyinfo->Xatom_editres)
10119 struct frame *f
10120 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10121 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10122 &event, NULL);
10124 #endif /* HACK_EDITRES */
10125 else if ((event.xclient.message_type
10126 == dpyinfo->Xatom_DONE)
10127 || (event.xclient.message_type
10128 == dpyinfo->Xatom_PAGE))
10130 /* Ghostview job completed. Kill it. We could
10131 reply with "Next" if we received "Page", but we
10132 currently never do because we are interested in
10133 images, only, which should have 1 page. */
10134 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10135 struct frame *f
10136 = x_window_to_frame (dpyinfo, event.xclient.window);
10137 x_kill_gs_process (pixmap, f);
10138 expose_frame (f, 0, 0, 0, 0);
10140 #ifdef USE_TOOLKIT_SCROLL_BARS
10141 /* Scroll bar callbacks send a ClientMessage from which
10142 we construct an input_event. */
10143 else if (event.xclient.message_type
10144 == dpyinfo->Xatom_Scrollbar)
10146 x_scroll_bar_to_input_event (&event, bufp);
10147 ++bufp, ++count, --numchars;
10148 goto out;
10150 #endif /* USE_TOOLKIT_SCROLL_BARS */
10151 else
10152 goto OTHER;
10154 break;
10156 case SelectionNotify:
10157 #ifdef USE_X_TOOLKIT
10158 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10159 goto OTHER;
10160 #endif /* not USE_X_TOOLKIT */
10161 x_handle_selection_notify (&event.xselection);
10162 break;
10164 case SelectionClear: /* Someone has grabbed ownership. */
10165 #ifdef USE_X_TOOLKIT
10166 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10167 goto OTHER;
10168 #endif /* USE_X_TOOLKIT */
10170 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10172 if (numchars == 0)
10173 abort ();
10175 bufp->kind = selection_clear_event;
10176 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10177 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10178 SELECTION_EVENT_TIME (bufp) = eventp->time;
10179 bufp->frame_or_window = Qnil;
10180 bufp->arg = Qnil;
10181 bufp++;
10183 count += 1;
10184 numchars -= 1;
10186 break;
10188 case SelectionRequest: /* Someone wants our selection. */
10189 #ifdef USE_X_TOOLKIT
10190 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10191 goto OTHER;
10192 #endif /* USE_X_TOOLKIT */
10193 if (x_queue_selection_requests)
10194 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10195 &event);
10196 else
10198 XSelectionRequestEvent *eventp
10199 = (XSelectionRequestEvent *) &event;
10201 if (numchars == 0)
10202 abort ();
10204 bufp->kind = selection_request_event;
10205 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10206 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10207 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10208 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10209 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10210 SELECTION_EVENT_TIME (bufp) = eventp->time;
10211 bufp->frame_or_window = Qnil;
10212 bufp->arg = Qnil;
10213 bufp++;
10215 count += 1;
10216 numchars -= 1;
10218 break;
10220 case PropertyNotify:
10221 #if 0 /* This is plain wrong. In the case that we are waiting for a
10222 PropertyNotify used as an ACK in incremental selection
10223 transfer, the property will be on the receiver's window. */
10224 #if defined USE_X_TOOLKIT
10225 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10226 goto OTHER;
10227 #endif
10228 #endif
10229 x_handle_property_notify (&event.xproperty);
10230 goto OTHER;
10232 case ReparentNotify:
10233 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10234 if (f)
10236 int x, y;
10237 f->output_data.x->parent_desc = event.xreparent.parent;
10238 x_real_positions (f, &x, &y);
10239 f->output_data.x->left_pos = x;
10240 f->output_data.x->top_pos = y;
10242 break;
10244 case Expose:
10245 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10246 if (f)
10248 x_check_fullscreen (f);
10250 if (f->async_visible == 0)
10252 f->async_visible = 1;
10253 f->async_iconified = 0;
10254 f->output_data.x->has_been_visible = 1;
10255 SET_FRAME_GARBAGED (f);
10257 else
10258 expose_frame (x_window_to_frame (dpyinfo,
10259 event.xexpose.window),
10260 event.xexpose.x, event.xexpose.y,
10261 event.xexpose.width, event.xexpose.height);
10263 else
10265 #ifndef USE_TOOLKIT_SCROLL_BARS
10266 struct scroll_bar *bar;
10267 #endif
10268 #if defined USE_LUCID
10269 /* Submenus of the Lucid menu bar aren't widgets
10270 themselves, so there's no way to dispatch events
10271 to them. Recognize this case separately. */
10273 Widget widget
10274 = x_window_to_menu_bar (event.xexpose.window);
10275 if (widget)
10276 xlwmenu_redisplay (widget);
10278 #endif /* USE_LUCID */
10280 #ifdef USE_TOOLKIT_SCROLL_BARS
10281 /* Dispatch event to the widget. */
10282 goto OTHER;
10283 #else /* not USE_TOOLKIT_SCROLL_BARS */
10284 bar = x_window_to_scroll_bar (event.xexpose.window);
10286 if (bar)
10287 x_scroll_bar_expose (bar, &event);
10288 #ifdef USE_X_TOOLKIT
10289 else
10290 goto OTHER;
10291 #endif /* USE_X_TOOLKIT */
10292 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10294 break;
10296 case GraphicsExpose: /* This occurs when an XCopyArea's
10297 source area was obscured or not
10298 available. */
10299 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10300 if (f)
10302 expose_frame (f,
10303 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10304 event.xgraphicsexpose.width,
10305 event.xgraphicsexpose.height);
10307 #ifdef USE_X_TOOLKIT
10308 else
10309 goto OTHER;
10310 #endif /* USE_X_TOOLKIT */
10311 break;
10313 case NoExpose: /* This occurs when an XCopyArea's
10314 source area was completely
10315 available. */
10316 break;
10318 case UnmapNotify:
10319 /* Redo the mouse-highlight after the tooltip has gone. */
10320 if (event.xmap.window == tip_window)
10322 tip_window = 0;
10323 redo_mouse_highlight ();
10326 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10327 if (f) /* F may no longer exist if
10328 the frame was deleted. */
10330 /* While a frame is unmapped, display generation is
10331 disabled; you don't want to spend time updating a
10332 display that won't ever be seen. */
10333 f->async_visible = 0;
10334 /* We can't distinguish, from the event, whether the window
10335 has become iconified or invisible. So assume, if it
10336 was previously visible, than now it is iconified.
10337 But x_make_frame_invisible clears both
10338 the visible flag and the iconified flag;
10339 and that way, we know the window is not iconified now. */
10340 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10342 f->async_iconified = 1;
10344 bufp->kind = iconify_event;
10345 XSETFRAME (bufp->frame_or_window, f);
10346 bufp->arg = Qnil;
10347 bufp++;
10348 count++;
10349 numchars--;
10352 goto OTHER;
10354 case MapNotify:
10355 if (event.xmap.window == tip_window)
10356 /* The tooltip has been drawn already. Avoid
10357 the SET_FRAME_GARBAGED below. */
10358 goto OTHER;
10360 /* We use x_top_window_to_frame because map events can
10361 come for sub-windows and they don't mean that the
10362 frame is visible. */
10363 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10364 if (f)
10366 f->async_visible = 1;
10367 f->async_iconified = 0;
10368 f->output_data.x->has_been_visible = 1;
10370 /* wait_reading_process_input will notice this and update
10371 the frame's display structures. */
10372 SET_FRAME_GARBAGED (f);
10374 if (f->iconified)
10376 bufp->kind = deiconify_event;
10377 XSETFRAME (bufp->frame_or_window, f);
10378 bufp->arg = Qnil;
10379 bufp++;
10380 count++;
10381 numchars--;
10383 else if (! NILP (Vframe_list)
10384 && ! NILP (XCDR (Vframe_list)))
10385 /* Force a redisplay sooner or later
10386 to update the frame titles
10387 in case this is the second frame. */
10388 record_asynch_buffer_change ();
10390 goto OTHER;
10392 case KeyPress:
10393 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10395 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10396 if (f == 0)
10398 /* Scroll bars consume key events, but we want
10399 the keys to go to the scroll bar's frame. */
10400 Widget widget = XtWindowToWidget (dpyinfo->display,
10401 event.xkey.window);
10402 if (widget && XmIsScrollBar (widget))
10404 widget = XtParent (widget);
10405 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10408 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10410 if (f != 0)
10412 KeySym keysym, orig_keysym;
10413 /* al%imercury@uunet.uu.net says that making this 81
10414 instead of 80 fixed a bug whereby meta chars made
10415 his Emacs hang.
10417 It seems that some version of XmbLookupString has
10418 a bug of not returning XBufferOverflow in
10419 status_return even if the input is too long to
10420 fit in 81 bytes. So, we must prepare sufficient
10421 bytes for copy_buffer. 513 bytes (256 chars for
10422 two-byte character set) seems to be a fairly good
10423 approximation. -- 2000.8.10 handa@etl.go.jp */
10424 unsigned char copy_buffer[513];
10425 unsigned char *copy_bufptr = copy_buffer;
10426 int copy_bufsiz = sizeof (copy_buffer);
10427 int modifiers;
10429 event.xkey.state
10430 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10431 extra_keyboard_modifiers);
10432 modifiers = event.xkey.state;
10434 /* This will have to go some day... */
10436 /* make_lispy_event turns chars into control chars.
10437 Don't do it here because XLookupString is too eager. */
10438 event.xkey.state &= ~ControlMask;
10439 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10440 | dpyinfo->super_mod_mask
10441 | dpyinfo->hyper_mod_mask
10442 | dpyinfo->alt_mod_mask);
10444 /* In case Meta is ComposeCharacter,
10445 clear its status. According to Markus Ehrnsperger
10446 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10447 this enables ComposeCharacter to work whether or
10448 not it is combined with Meta. */
10449 if (modifiers & dpyinfo->meta_mod_mask)
10450 bzero (&compose_status, sizeof (compose_status));
10452 #ifdef HAVE_X_I18N
10453 if (FRAME_XIC (f))
10455 Status status_return;
10457 nbytes = XmbLookupString (FRAME_XIC (f),
10458 &event.xkey, copy_bufptr,
10459 copy_bufsiz, &keysym,
10460 &status_return);
10461 if (status_return == XBufferOverflow)
10463 copy_bufsiz = nbytes + 1;
10464 copy_bufptr = (char *) alloca (copy_bufsiz);
10465 nbytes = XmbLookupString (FRAME_XIC (f),
10466 &event.xkey, copy_bufptr,
10467 copy_bufsiz, &keysym,
10468 &status_return);
10471 if (status_return == XLookupNone)
10472 break;
10473 else if (status_return == XLookupChars)
10475 keysym = NoSymbol;
10476 modifiers = 0;
10478 else if (status_return != XLookupKeySym
10479 && status_return != XLookupBoth)
10480 abort ();
10482 else
10483 nbytes = XLookupString (&event.xkey, copy_bufptr,
10484 copy_bufsiz, &keysym,
10485 &compose_status);
10486 #else
10487 nbytes = XLookupString (&event.xkey, copy_bufptr,
10488 copy_bufsiz, &keysym,
10489 &compose_status);
10490 #endif
10492 orig_keysym = keysym;
10494 if (numchars > 1)
10496 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10497 || keysym == XK_Delete
10498 #ifdef XK_ISO_Left_Tab
10499 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10500 #endif
10501 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10502 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10503 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10504 #ifdef HPUX
10505 /* This recognizes the "extended function keys".
10506 It seems there's no cleaner way.
10507 Test IsModifierKey to avoid handling mode_switch
10508 incorrectly. */
10509 || ((unsigned) (keysym) >= XK_Select
10510 && (unsigned)(keysym) < XK_KP_Space)
10511 #endif
10512 #ifdef XK_dead_circumflex
10513 || orig_keysym == XK_dead_circumflex
10514 #endif
10515 #ifdef XK_dead_grave
10516 || orig_keysym == XK_dead_grave
10517 #endif
10518 #ifdef XK_dead_tilde
10519 || orig_keysym == XK_dead_tilde
10520 #endif
10521 #ifdef XK_dead_diaeresis
10522 || orig_keysym == XK_dead_diaeresis
10523 #endif
10524 #ifdef XK_dead_macron
10525 || orig_keysym == XK_dead_macron
10526 #endif
10527 #ifdef XK_dead_degree
10528 || orig_keysym == XK_dead_degree
10529 #endif
10530 #ifdef XK_dead_acute
10531 || orig_keysym == XK_dead_acute
10532 #endif
10533 #ifdef XK_dead_cedilla
10534 || orig_keysym == XK_dead_cedilla
10535 #endif
10536 #ifdef XK_dead_breve
10537 || orig_keysym == XK_dead_breve
10538 #endif
10539 #ifdef XK_dead_ogonek
10540 || orig_keysym == XK_dead_ogonek
10541 #endif
10542 #ifdef XK_dead_caron
10543 || orig_keysym == XK_dead_caron
10544 #endif
10545 #ifdef XK_dead_doubleacute
10546 || orig_keysym == XK_dead_doubleacute
10547 #endif
10548 #ifdef XK_dead_abovedot
10549 || orig_keysym == XK_dead_abovedot
10550 #endif
10551 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10552 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10553 /* Any "vendor-specific" key is ok. */
10554 || (orig_keysym & (1 << 28))
10555 || (keysym != NoSymbol && nbytes == 0))
10556 && ! (IsModifierKey (orig_keysym)
10557 #ifndef HAVE_X11R5
10558 #ifdef XK_Mode_switch
10559 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10560 #endif
10561 #ifdef XK_Num_Lock
10562 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10563 #endif
10564 #endif /* not HAVE_X11R5 */
10567 if (temp_index == sizeof temp_buffer / sizeof (short))
10568 temp_index = 0;
10569 temp_buffer[temp_index++] = keysym;
10570 bufp->kind = non_ascii_keystroke;
10571 bufp->code = keysym;
10572 XSETFRAME (bufp->frame_or_window, f);
10573 bufp->arg = Qnil;
10574 bufp->modifiers
10575 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10576 modifiers);
10577 bufp->timestamp = event.xkey.time;
10578 bufp++;
10579 count++;
10580 numchars--;
10582 else if (numchars > nbytes)
10584 register int i;
10585 register int c;
10586 int nchars, len;
10588 for (i = 0; i < nbytes; i++)
10590 if (temp_index == (sizeof temp_buffer
10591 / sizeof (short)))
10592 temp_index = 0;
10593 temp_buffer[temp_index++] = copy_bufptr[i];
10596 if (/* If the event is not from XIM, */
10597 event.xkey.keycode != 0
10598 /* or the current locale doesn't request
10599 decoding of the intup data, ... */
10600 || coding.type == coding_type_raw_text
10601 || coding.type == coding_type_no_conversion)
10603 /* ... we can use the input data as is. */
10604 nchars = nbytes;
10606 else
10608 /* We have to decode the input data. */
10609 int require;
10610 unsigned char *p;
10612 require = decoding_buffer_size (&coding, nbytes);
10613 p = (unsigned char *) alloca (require);
10614 coding.mode |= CODING_MODE_LAST_BLOCK;
10615 decode_coding (&coding, copy_bufptr, p,
10616 nbytes, require);
10617 nbytes = coding.produced;
10618 nchars = coding.produced_char;
10619 copy_bufptr = p;
10622 /* Convert the input data to a sequence of
10623 character events. */
10624 for (i = 0; i < nbytes; i += len)
10626 if (nchars == nbytes)
10627 c = copy_bufptr[i], len = 1;
10628 else
10629 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10630 nbytes - i, len);
10632 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10633 ? ascii_keystroke
10634 : multibyte_char_keystroke);
10635 bufp->code = c;
10636 XSETFRAME (bufp->frame_or_window, f);
10637 bufp->arg = Qnil;
10638 bufp->modifiers
10639 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10640 modifiers);
10641 bufp->timestamp = event.xkey.time;
10642 bufp++;
10645 count += nchars;
10646 numchars -= nchars;
10648 if (keysym == NoSymbol)
10649 break;
10651 else
10652 abort ();
10654 else
10655 abort ();
10657 #ifdef HAVE_X_I18N
10658 /* Don't dispatch this event since XtDispatchEvent calls
10659 XFilterEvent, and two calls in a row may freeze the
10660 client. */
10661 break;
10662 #else
10663 goto OTHER;
10664 #endif
10666 case KeyRelease:
10667 #ifdef HAVE_X_I18N
10668 /* Don't dispatch this event since XtDispatchEvent calls
10669 XFilterEvent, and two calls in a row may freeze the
10670 client. */
10671 break;
10672 #else
10673 goto OTHER;
10674 #endif
10676 /* Here's a possible interpretation of the whole
10677 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10678 you get a FocusIn event, you have to get a FocusOut
10679 event before you relinquish the focus. If you
10680 haven't received a FocusIn event, then a mere
10681 LeaveNotify is enough to free you. */
10683 case EnterNotify:
10685 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10687 #if 0
10688 if (event.xcrossing.focus)
10690 /* Avoid nasty pop/raise loops. */
10691 if (f && (!(f->auto_raise)
10692 || !(f->auto_lower)
10693 || (event.xcrossing.time - enter_timestamp) > 500))
10695 x_new_focus_frame (dpyinfo, f);
10696 enter_timestamp = event.xcrossing.time;
10699 else if (f == dpyinfo->x_focus_frame)
10700 x_new_focus_frame (dpyinfo, 0);
10701 #endif
10703 /* EnterNotify counts as mouse movement,
10704 so update things that depend on mouse position. */
10705 if (f && !f->output_data.x->hourglass_p)
10706 note_mouse_movement (f, &event.xmotion);
10707 goto OTHER;
10710 case FocusIn:
10711 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10712 if (event.xfocus.detail != NotifyPointer)
10713 dpyinfo->x_focus_event_frame = f;
10714 if (f)
10716 x_new_focus_frame (dpyinfo, f);
10718 /* Don't stop displaying the initial startup message
10719 for a switch-frame event we don't need. */
10720 if (GC_NILP (Vterminal_frame)
10721 && GC_CONSP (Vframe_list)
10722 && !GC_NILP (XCDR (Vframe_list)))
10724 bufp->kind = FOCUS_IN_EVENT;
10725 XSETFRAME (bufp->frame_or_window, f);
10726 bufp->arg = Qnil;
10727 ++bufp, ++count, --numchars;
10731 #ifdef HAVE_X_I18N
10732 if (f && FRAME_XIC (f))
10733 XSetICFocus (FRAME_XIC (f));
10734 #endif
10736 goto OTHER;
10738 case LeaveNotify:
10739 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10740 if (f)
10742 if (f == dpyinfo->mouse_face_mouse_frame)
10744 /* If we move outside the frame, then we're
10745 certainly no longer on any text in the frame. */
10746 clear_mouse_face (dpyinfo);
10747 dpyinfo->mouse_face_mouse_frame = 0;
10750 /* Generate a nil HELP_EVENT to cancel a help-echo.
10751 Do it only if there's something to cancel.
10752 Otherwise, the startup message is cleared when
10753 the mouse leaves the frame. */
10754 if (any_help_event_p)
10756 Lisp_Object frame;
10757 int n;
10759 XSETFRAME (frame, f);
10760 help_echo = Qnil;
10761 n = gen_help_event (bufp, numchars,
10762 Qnil, frame, Qnil, Qnil, 0);
10763 bufp += n, count += n, numchars -= n;
10766 #if 0
10767 if (event.xcrossing.focus)
10768 x_mouse_leave (dpyinfo);
10769 else
10771 if (f == dpyinfo->x_focus_event_frame)
10772 dpyinfo->x_focus_event_frame = 0;
10773 if (f == dpyinfo->x_focus_frame)
10774 x_new_focus_frame (dpyinfo, 0);
10776 #endif
10778 goto OTHER;
10780 case FocusOut:
10781 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10782 if (event.xfocus.detail != NotifyPointer
10783 && f == dpyinfo->x_focus_event_frame)
10784 dpyinfo->x_focus_event_frame = 0;
10785 if (f && f == dpyinfo->x_focus_frame)
10786 x_new_focus_frame (dpyinfo, 0);
10788 #ifdef HAVE_X_I18N
10789 if (f && FRAME_XIC (f))
10790 XUnsetICFocus (FRAME_XIC (f));
10791 #endif
10793 goto OTHER;
10795 case MotionNotify:
10797 previous_help_echo = help_echo;
10798 help_echo = help_echo_object = help_echo_window = Qnil;
10799 help_echo_pos = -1;
10801 if (dpyinfo->grabbed && last_mouse_frame
10802 && FRAME_LIVE_P (last_mouse_frame))
10803 f = last_mouse_frame;
10804 else
10805 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10807 if (f)
10808 note_mouse_movement (f, &event.xmotion);
10809 else
10811 #ifndef USE_TOOLKIT_SCROLL_BARS
10812 struct scroll_bar *bar
10813 = x_window_to_scroll_bar (event.xmotion.window);
10815 if (bar)
10816 x_scroll_bar_note_movement (bar, &event);
10817 #endif /* USE_TOOLKIT_SCROLL_BARS */
10819 /* If we move outside the frame, then we're
10820 certainly no longer on any text in the frame. */
10821 clear_mouse_face (dpyinfo);
10824 /* If the contents of the global variable help_echo
10825 has changed, generate a HELP_EVENT. */
10826 if (!NILP (help_echo)
10827 || !NILP (previous_help_echo))
10829 Lisp_Object frame;
10830 int n;
10832 if (f)
10833 XSETFRAME (frame, f);
10834 else
10835 frame = Qnil;
10837 any_help_event_p = 1;
10838 n = gen_help_event (bufp, numchars, help_echo, frame,
10839 help_echo_window, help_echo_object,
10840 help_echo_pos);
10841 bufp += n, count += n, numchars -= n;
10844 goto OTHER;
10847 case ConfigureNotify:
10848 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10849 if (f)
10851 #ifndef USE_X_TOOLKIT
10852 /* If there is a pending resize for fullscreen, don't
10853 do this one, the right one will come later.
10854 The toolkit version doesn't seem to need this, but we
10855 need to reset it below. */
10856 int dont_resize =
10857 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10858 && FRAME_NEW_WIDTH (f) != 0);
10859 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10860 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10861 if (dont_resize)
10862 goto OTHER;
10864 /* In the toolkit version, change_frame_size
10865 is called by the code that handles resizing
10866 of the EmacsFrame widget. */
10868 /* Even if the number of character rows and columns has
10869 not changed, the font size may have changed, so we need
10870 to check the pixel dimensions as well. */
10871 if (columns != f->width
10872 || rows != f->height
10873 || event.xconfigure.width != f->output_data.x->pixel_width
10874 || event.xconfigure.height != f->output_data.x->pixel_height)
10876 change_frame_size (f, rows, columns, 0, 1, 0);
10877 SET_FRAME_GARBAGED (f);
10878 cancel_mouse_face (f);
10880 #endif
10882 f->output_data.x->pixel_width = event.xconfigure.width;
10883 f->output_data.x->pixel_height = event.xconfigure.height;
10885 /* What we have now is the position of Emacs's own window.
10886 Convert that to the position of the window manager window. */
10887 x_real_positions (f, &f->output_data.x->left_pos,
10888 &f->output_data.x->top_pos);
10890 x_check_fullscreen_move(f);
10891 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10892 f->output_data.x->want_fullscreen &=
10893 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
10894 #ifdef HAVE_X_I18N
10895 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10896 xic_set_statusarea (f);
10897 #endif
10899 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10901 /* Since the WM decorations come below top_pos now,
10902 we must put them below top_pos in the future. */
10903 f->output_data.x->win_gravity = NorthWestGravity;
10904 x_wm_set_size_hint (f, (long) 0, 0);
10906 #ifdef USE_MOTIF
10907 /* Some window managers pass (0,0) as the location of
10908 the window, and the Motif event handler stores it
10909 in the emacs widget, which messes up Motif menus. */
10910 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10912 event.xconfigure.x = f->output_data.x->widget->core.x;
10913 event.xconfigure.y = f->output_data.x->widget->core.y;
10915 #endif /* USE_MOTIF */
10917 goto OTHER;
10919 case ButtonPress:
10920 case ButtonRelease:
10922 /* If we decide we want to generate an event to be seen
10923 by the rest of Emacs, we put it here. */
10924 struct input_event emacs_event;
10925 int tool_bar_p = 0;
10927 emacs_event.kind = no_event;
10928 bzero (&compose_status, sizeof (compose_status));
10930 if (dpyinfo->grabbed
10931 && last_mouse_frame
10932 && FRAME_LIVE_P (last_mouse_frame))
10933 f = last_mouse_frame;
10934 else
10935 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10937 if (f)
10939 /* Is this in the tool-bar? */
10940 if (WINDOWP (f->tool_bar_window)
10941 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10943 Lisp_Object window;
10944 int p, x, y;
10946 x = event.xbutton.x;
10947 y = event.xbutton.y;
10949 /* Set x and y. */
10950 window = window_from_coordinates (f, x, y, &p, 1);
10951 if (EQ (window, f->tool_bar_window))
10953 x_handle_tool_bar_click (f, &event.xbutton);
10954 tool_bar_p = 1;
10958 if (!tool_bar_p)
10959 if (!dpyinfo->x_focus_frame
10960 || f == dpyinfo->x_focus_frame)
10961 construct_mouse_click (&emacs_event, &event, f);
10963 else
10965 #ifndef USE_TOOLKIT_SCROLL_BARS
10966 struct scroll_bar *bar
10967 = x_window_to_scroll_bar (event.xbutton.window);
10969 if (bar)
10970 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10971 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10974 if (event.type == ButtonPress)
10976 dpyinfo->grabbed |= (1 << event.xbutton.button);
10977 last_mouse_frame = f;
10978 /* Ignore any mouse motion that happened
10979 before this event; any subsequent mouse-movement
10980 Emacs events should reflect only motion after
10981 the ButtonPress. */
10982 if (f != 0)
10983 f->mouse_moved = 0;
10985 if (!tool_bar_p)
10986 last_tool_bar_item = -1;
10988 else
10990 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10993 if (numchars >= 1 && emacs_event.kind != no_event)
10995 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10996 bufp++;
10997 count++;
10998 numchars--;
11001 #ifdef USE_X_TOOLKIT
11002 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11003 /* For a down-event in the menu bar,
11004 don't pass it to Xt right now.
11005 Instead, save it away
11006 and we will pass it to Xt from kbd_buffer_get_event.
11007 That way, we can run some Lisp code first. */
11008 if (f && event.type == ButtonPress
11009 /* Verify the event is really within the menu bar
11010 and not just sent to it due to grabbing. */
11011 && event.xbutton.x >= 0
11012 && event.xbutton.x < f->output_data.x->pixel_width
11013 && event.xbutton.y >= 0
11014 && event.xbutton.y < f->output_data.x->menubar_height
11015 && event.xbutton.same_screen)
11017 SET_SAVED_BUTTON_EVENT;
11018 XSETFRAME (last_mouse_press_frame, f);
11020 else if (event.type == ButtonPress)
11022 last_mouse_press_frame = Qnil;
11023 goto OTHER;
11026 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11027 but I am trying to be cautious. */
11028 else if (event.type == ButtonRelease)
11030 if (!NILP (last_mouse_press_frame))
11032 f = XFRAME (last_mouse_press_frame);
11033 if (f->output_data.x)
11034 SET_SAVED_BUTTON_EVENT;
11036 else
11037 goto OTHER;
11039 #endif /* USE_MOTIF */
11040 else
11041 goto OTHER;
11042 #endif /* USE_X_TOOLKIT */
11044 break;
11046 case CirculateNotify:
11047 goto OTHER;
11049 case CirculateRequest:
11050 goto OTHER;
11052 case VisibilityNotify:
11053 goto OTHER;
11055 case MappingNotify:
11056 /* Someone has changed the keyboard mapping - update the
11057 local cache. */
11058 switch (event.xmapping.request)
11060 case MappingModifier:
11061 x_find_modifier_meanings (dpyinfo);
11062 /* This is meant to fall through. */
11063 case MappingKeyboard:
11064 XRefreshKeyboardMapping (&event.xmapping);
11066 goto OTHER;
11068 default:
11069 OTHER:
11070 #ifdef USE_X_TOOLKIT
11071 BLOCK_INPUT;
11072 XtDispatchEvent (&event);
11073 UNBLOCK_INPUT;
11074 #endif /* USE_X_TOOLKIT */
11075 break;
11080 out:;
11082 /* On some systems, an X bug causes Emacs to get no more events
11083 when the window is destroyed. Detect that. (1994.) */
11084 if (! event_found)
11086 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11087 One XNOOP in 100 loops will make Emacs terminate.
11088 B. Bretthauer, 1994 */
11089 x_noop_count++;
11090 if (x_noop_count >= 100)
11092 x_noop_count=0;
11094 if (next_noop_dpyinfo == 0)
11095 next_noop_dpyinfo = x_display_list;
11097 XNoOp (next_noop_dpyinfo->display);
11099 /* Each time we get here, cycle through the displays now open. */
11100 next_noop_dpyinfo = next_noop_dpyinfo->next;
11104 /* If the focus was just given to an auto-raising frame,
11105 raise it now. */
11106 /* ??? This ought to be able to handle more than one such frame. */
11107 if (pending_autoraise_frame)
11109 x_raise_frame (pending_autoraise_frame);
11110 pending_autoraise_frame = 0;
11113 UNBLOCK_INPUT;
11114 --handling_signal;
11115 return count;
11121 /***********************************************************************
11122 Text Cursor
11123 ***********************************************************************/
11125 /* Notice when the text cursor of window W has been completely
11126 overwritten by a drawing operation that outputs glyphs in AREA
11127 starting at X0 and ending at X1 in the line starting at Y0 and
11128 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11129 the rest of the line after X0 has been written. Y coordinates
11130 are window-relative. */
11132 static void
11133 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11134 struct window *w;
11135 enum glyph_row_area area;
11136 int x0, y0, x1, y1;
11138 if (area == TEXT_AREA
11139 && w->phys_cursor_on_p
11140 && y0 <= w->phys_cursor.y
11141 && y1 >= w->phys_cursor.y + w->phys_cursor_height
11142 && x0 <= w->phys_cursor.x
11143 && (x1 < 0 || x1 > w->phys_cursor.x))
11144 w->phys_cursor_on_p = 0;
11148 /* Set clipping for output in glyph row ROW. W is the window in which
11149 we operate. GC is the graphics context to set clipping in.
11150 WHOLE_LINE_P non-zero means include the areas used for truncation
11151 mark display and alike in the clipping rectangle.
11153 ROW may be a text row or, e.g., a mode line. Text rows must be
11154 clipped to the interior of the window dedicated to text display,
11155 mode lines must be clipped to the whole window. */
11157 static void
11158 x_clip_to_row (w, row, gc, whole_line_p)
11159 struct window *w;
11160 struct glyph_row *row;
11161 GC gc;
11162 int whole_line_p;
11164 struct frame *f = XFRAME (WINDOW_FRAME (w));
11165 XRectangle clip_rect;
11166 int window_x, window_y, window_width, window_height;
11168 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11170 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11171 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11172 clip_rect.y = max (clip_rect.y, window_y);
11173 clip_rect.width = window_width;
11174 clip_rect.height = row->visible_height;
11176 /* If clipping to the whole line, including trunc marks, extend
11177 the rectangle to the left and increase its width. */
11178 if (whole_line_p)
11180 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11181 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11184 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11188 /* Draw a hollow box cursor on window W in glyph row ROW. */
11190 static void
11191 x_draw_hollow_cursor (w, row)
11192 struct window *w;
11193 struct glyph_row *row;
11195 struct frame *f = XFRAME (WINDOW_FRAME (w));
11196 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11197 Display *dpy = FRAME_X_DISPLAY (f);
11198 int x, y, wd, h;
11199 XGCValues xgcv;
11200 struct glyph *cursor_glyph;
11201 GC gc;
11203 /* Compute frame-relative coordinates from window-relative
11204 coordinates. */
11205 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11206 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11207 + row->ascent - w->phys_cursor_ascent);
11208 h = row->height - 1;
11210 /* Get the glyph the cursor is on. If we can't tell because
11211 the current matrix is invalid or such, give up. */
11212 cursor_glyph = get_phys_cursor_glyph (w);
11213 if (cursor_glyph == NULL)
11214 return;
11216 /* Compute the width of the rectangle to draw. If on a stretch
11217 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11218 rectangle as wide as the glyph, but use a canonical character
11219 width instead. */
11220 wd = cursor_glyph->pixel_width - 1;
11221 if (cursor_glyph->type == STRETCH_GLYPH
11222 && !x_stretch_cursor_p)
11223 wd = min (CANON_X_UNIT (f), wd);
11225 /* The foreground of cursor_gc is typically the same as the normal
11226 background color, which can cause the cursor box to be invisible. */
11227 xgcv.foreground = f->output_data.x->cursor_pixel;
11228 if (dpyinfo->scratch_cursor_gc)
11229 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11230 else
11231 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11232 GCForeground, &xgcv);
11233 gc = dpyinfo->scratch_cursor_gc;
11235 /* Set clipping, draw the rectangle, and reset clipping again. */
11236 x_clip_to_row (w, row, gc, 0);
11237 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11238 XSetClipMask (dpy, gc, None);
11242 /* Draw a bar cursor on window W in glyph row ROW.
11244 Implementation note: One would like to draw a bar cursor with an
11245 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11246 Unfortunately, I didn't find a font yet that has this property set.
11247 --gerd. */
11249 static void
11250 x_draw_bar_cursor (w, row, width)
11251 struct window *w;
11252 struct glyph_row *row;
11253 int width;
11255 struct frame *f = XFRAME (w->frame);
11256 struct glyph *cursor_glyph;
11258 /* If cursor is out of bounds, don't draw garbage. This can happen
11259 in mini-buffer windows when switching between echo area glyphs
11260 and mini-buffer. */
11261 cursor_glyph = get_phys_cursor_glyph (w);
11262 if (cursor_glyph == NULL)
11263 return;
11265 /* If on an image, draw like a normal cursor. That's usually better
11266 visible than drawing a bar, esp. if the image is large so that
11267 the bar might not be in the window. */
11268 if (cursor_glyph->type == IMAGE_GLYPH)
11270 struct glyph_row *row;
11271 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11272 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11274 else
11276 Display *dpy = FRAME_X_DISPLAY (f);
11277 Window window = FRAME_X_WINDOW (f);
11278 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11279 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11280 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11281 XGCValues xgcv;
11283 /* If the glyph's background equals the color we normally draw
11284 the bar cursor in, the bar cursor in its normal color is
11285 invisible. Use the glyph's foreground color instead in this
11286 case, on the assumption that the glyph's colors are chosen so
11287 that the glyph is legible. */
11288 if (face->background == f->output_data.x->cursor_pixel)
11289 xgcv.background = xgcv.foreground = face->foreground;
11290 else
11291 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11292 xgcv.graphics_exposures = 0;
11294 if (gc)
11295 XChangeGC (dpy, gc, mask, &xgcv);
11296 else
11298 gc = XCreateGC (dpy, window, mask, &xgcv);
11299 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11302 if (width < 0)
11303 width = f->output_data.x->cursor_width;
11304 width = min (cursor_glyph->pixel_width, width);
11306 x_clip_to_row (w, row, gc, 0);
11307 XFillRectangle (dpy, window, gc,
11308 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11309 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11310 width, row->height);
11311 XSetClipMask (dpy, gc, None);
11316 /* Clear the cursor of window W to background color, and mark the
11317 cursor as not shown. This is used when the text where the cursor
11318 is is about to be rewritten. */
11320 static void
11321 x_clear_cursor (w)
11322 struct window *w;
11324 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11325 x_update_window_cursor (w, 0);
11329 /* Draw the cursor glyph of window W in glyph row ROW. See the
11330 comment of x_draw_glyphs for the meaning of HL. */
11332 static void
11333 x_draw_phys_cursor_glyph (w, row, hl)
11334 struct window *w;
11335 struct glyph_row *row;
11336 enum draw_glyphs_face hl;
11338 /* If cursor hpos is out of bounds, don't draw garbage. This can
11339 happen in mini-buffer windows when switching between echo area
11340 glyphs and mini-buffer. */
11341 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11343 int on_p = w->phys_cursor_on_p;
11345 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11346 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11347 hl, 0);
11348 w->phys_cursor_on_p = on_p;
11350 /* When we erase the cursor, and ROW is overlapped by other
11351 rows, make sure that these overlapping parts of other rows
11352 are redrawn. */
11353 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11355 if (row > w->current_matrix->rows
11356 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11357 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11359 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11360 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11361 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11367 /* Erase the image of a cursor of window W from the screen. */
11369 static void
11370 x_erase_phys_cursor (w)
11371 struct window *w;
11373 struct frame *f = XFRAME (w->frame);
11374 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11375 int hpos = w->phys_cursor.hpos;
11376 int vpos = w->phys_cursor.vpos;
11377 int mouse_face_here_p = 0;
11378 struct glyph_matrix *active_glyphs = w->current_matrix;
11379 struct glyph_row *cursor_row;
11380 struct glyph *cursor_glyph;
11381 enum draw_glyphs_face hl;
11383 /* No cursor displayed or row invalidated => nothing to do on the
11384 screen. */
11385 if (w->phys_cursor_type == NO_CURSOR)
11386 goto mark_cursor_off;
11388 /* VPOS >= active_glyphs->nrows means that window has been resized.
11389 Don't bother to erase the cursor. */
11390 if (vpos >= active_glyphs->nrows)
11391 goto mark_cursor_off;
11393 /* If row containing cursor is marked invalid, there is nothing we
11394 can do. */
11395 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11396 if (!cursor_row->enabled_p)
11397 goto mark_cursor_off;
11399 /* If row is completely invisible, don't attempt to delete a cursor which
11400 isn't there. This can happen if cursor is at top of a window, and
11401 we switch to a buffer with a header line in that window. */
11402 if (cursor_row->visible_height <= 0)
11403 goto mark_cursor_off;
11405 /* This can happen when the new row is shorter than the old one.
11406 In this case, either x_draw_glyphs or clear_end_of_line
11407 should have cleared the cursor. Note that we wouldn't be
11408 able to erase the cursor in this case because we don't have a
11409 cursor glyph at hand. */
11410 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11411 goto mark_cursor_off;
11413 /* If the cursor is in the mouse face area, redisplay that when
11414 we clear the cursor. */
11415 if (! NILP (dpyinfo->mouse_face_window)
11416 && w == XWINDOW (dpyinfo->mouse_face_window)
11417 && (vpos > dpyinfo->mouse_face_beg_row
11418 || (vpos == dpyinfo->mouse_face_beg_row
11419 && hpos >= dpyinfo->mouse_face_beg_col))
11420 && (vpos < dpyinfo->mouse_face_end_row
11421 || (vpos == dpyinfo->mouse_face_end_row
11422 && hpos < dpyinfo->mouse_face_end_col))
11423 /* Don't redraw the cursor's spot in mouse face if it is at the
11424 end of a line (on a newline). The cursor appears there, but
11425 mouse highlighting does not. */
11426 && cursor_row->used[TEXT_AREA] > hpos)
11427 mouse_face_here_p = 1;
11429 /* Maybe clear the display under the cursor. */
11430 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11432 int x;
11433 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11435 cursor_glyph = get_phys_cursor_glyph (w);
11436 if (cursor_glyph == NULL)
11437 goto mark_cursor_off;
11439 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11441 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11443 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11444 cursor_row->y)),
11445 cursor_glyph->pixel_width,
11446 cursor_row->visible_height,
11447 False);
11450 /* Erase the cursor by redrawing the character underneath it. */
11451 if (mouse_face_here_p)
11452 hl = DRAW_MOUSE_FACE;
11453 else
11454 hl = DRAW_NORMAL_TEXT;
11455 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11457 mark_cursor_off:
11458 w->phys_cursor_on_p = 0;
11459 w->phys_cursor_type = NO_CURSOR;
11463 /* Non-zero if physical cursor of window W is within mouse face. */
11465 static int
11466 cursor_in_mouse_face_p (w)
11467 struct window *w;
11469 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11470 int in_mouse_face = 0;
11472 if (WINDOWP (dpyinfo->mouse_face_window)
11473 && XWINDOW (dpyinfo->mouse_face_window) == w)
11475 int hpos = w->phys_cursor.hpos;
11476 int vpos = w->phys_cursor.vpos;
11478 if (vpos >= dpyinfo->mouse_face_beg_row
11479 && vpos <= dpyinfo->mouse_face_end_row
11480 && (vpos > dpyinfo->mouse_face_beg_row
11481 || hpos >= dpyinfo->mouse_face_beg_col)
11482 && (vpos < dpyinfo->mouse_face_end_row
11483 || hpos < dpyinfo->mouse_face_end_col
11484 || dpyinfo->mouse_face_past_end))
11485 in_mouse_face = 1;
11488 return in_mouse_face;
11492 /* Display or clear cursor of window W. If ON is zero, clear the
11493 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11494 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11496 void
11497 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11498 struct window *w;
11499 int on, hpos, vpos, x, y;
11501 struct frame *f = XFRAME (w->frame);
11502 int new_cursor_type;
11503 int new_cursor_width;
11504 struct glyph_matrix *current_glyphs;
11505 struct glyph_row *glyph_row;
11506 struct glyph *glyph;
11507 int cursor_non_selected;
11509 /* This is pointless on invisible frames, and dangerous on garbaged
11510 windows and frames; in the latter case, the frame or window may
11511 be in the midst of changing its size, and x and y may be off the
11512 window. */
11513 if (! FRAME_VISIBLE_P (f)
11514 || FRAME_GARBAGED_P (f)
11515 || vpos >= w->current_matrix->nrows
11516 || hpos >= w->current_matrix->matrix_w)
11517 return;
11519 /* If cursor is off and we want it off, return quickly. */
11520 if (!on && !w->phys_cursor_on_p)
11521 return;
11523 current_glyphs = w->current_matrix;
11524 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11525 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11527 /* If cursor row is not enabled, we don't really know where to
11528 display the cursor. */
11529 if (!glyph_row->enabled_p)
11531 w->phys_cursor_on_p = 0;
11532 return;
11535 xassert (interrupt_input_blocked);
11537 /* Set new_cursor_type to the cursor we want to be displayed. In a
11538 mini-buffer window, we want the cursor only to appear if we are
11539 reading input from this window. For the selected window, we want
11540 the cursor type given by the frame parameter. If explicitly
11541 marked off, draw no cursor. In all other cases, we want a hollow
11542 box cursor. */
11543 cursor_non_selected
11544 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11545 w->buffer));
11546 new_cursor_width = -1;
11547 if (cursor_in_echo_area
11548 && FRAME_HAS_MINIBUF_P (f)
11549 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11551 if (w == XWINDOW (echo_area_window))
11552 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11553 else if (cursor_non_selected)
11554 new_cursor_type = HOLLOW_BOX_CURSOR;
11555 else
11556 new_cursor_type = NO_CURSOR;
11558 else
11560 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11561 || w != XWINDOW (f->selected_window))
11563 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11564 || !cursor_non_selected
11565 || NILP (XBUFFER (w->buffer)->cursor_type))
11566 new_cursor_type = NO_CURSOR;
11567 else
11568 new_cursor_type = HOLLOW_BOX_CURSOR;
11570 else if (w->cursor_off_p)
11571 new_cursor_type = NO_CURSOR;
11572 else
11574 struct buffer *b = XBUFFER (w->buffer);
11576 if (EQ (b->cursor_type, Qt))
11577 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11578 else
11579 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11580 &new_cursor_width);
11584 /* If cursor is currently being shown and we don't want it to be or
11585 it is in the wrong place, or the cursor type is not what we want,
11586 erase it. */
11587 if (w->phys_cursor_on_p
11588 && (!on
11589 || w->phys_cursor.x != x
11590 || w->phys_cursor.y != y
11591 || new_cursor_type != w->phys_cursor_type))
11592 x_erase_phys_cursor (w);
11594 /* If the cursor is now invisible and we want it to be visible,
11595 display it. */
11596 if (on && !w->phys_cursor_on_p)
11598 w->phys_cursor_ascent = glyph_row->ascent;
11599 w->phys_cursor_height = glyph_row->height;
11601 /* Set phys_cursor_.* before x_draw_.* is called because some
11602 of them may need the information. */
11603 w->phys_cursor.x = x;
11604 w->phys_cursor.y = glyph_row->y;
11605 w->phys_cursor.hpos = hpos;
11606 w->phys_cursor.vpos = vpos;
11607 w->phys_cursor_type = new_cursor_type;
11608 w->phys_cursor_on_p = 1;
11610 switch (new_cursor_type)
11612 case HOLLOW_BOX_CURSOR:
11613 x_draw_hollow_cursor (w, glyph_row);
11614 break;
11616 case FILLED_BOX_CURSOR:
11617 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11618 break;
11620 case BAR_CURSOR:
11621 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11622 break;
11624 case NO_CURSOR:
11625 break;
11627 default:
11628 abort ();
11631 #ifdef HAVE_X_I18N
11632 if (w == XWINDOW (f->selected_window))
11633 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11634 xic_set_preeditarea (w, x, y);
11635 #endif
11638 #ifndef XFlush
11639 if (updating_frame != f)
11640 XFlush (FRAME_X_DISPLAY (f));
11641 #endif
11645 /* Display the cursor on window W, or clear it. X and Y are window
11646 relative pixel coordinates. HPOS and VPOS are glyph matrix
11647 positions. If W is not the selected window, display a hollow
11648 cursor. ON non-zero means display the cursor at X, Y which
11649 correspond to HPOS, VPOS, otherwise it is cleared. */
11651 void
11652 x_display_cursor (w, on, hpos, vpos, x, y)
11653 struct window *w;
11654 int on, hpos, vpos, x, y;
11656 BLOCK_INPUT;
11657 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11658 UNBLOCK_INPUT;
11662 /* Display the cursor on window W, or clear it, according to ON_P.
11663 Don't change the cursor's position. */
11665 void
11666 x_update_cursor (f, on_p)
11667 struct frame *f;
11669 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11673 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11674 in the window tree rooted at W. */
11676 static void
11677 x_update_cursor_in_window_tree (w, on_p)
11678 struct window *w;
11679 int on_p;
11681 while (w)
11683 if (!NILP (w->hchild))
11684 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11685 else if (!NILP (w->vchild))
11686 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11687 else
11688 x_update_window_cursor (w, on_p);
11690 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11695 /* Switch the display of W's cursor on or off, according to the value
11696 of ON. */
11698 static void
11699 x_update_window_cursor (w, on)
11700 struct window *w;
11701 int on;
11703 /* Don't update cursor in windows whose frame is in the process
11704 of being deleted. */
11705 if (w->current_matrix)
11707 BLOCK_INPUT;
11708 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11709 w->phys_cursor.x, w->phys_cursor.y);
11710 UNBLOCK_INPUT;
11717 /* Icons. */
11719 /* Make the x-window of frame F use the gnu icon bitmap. */
11722 x_bitmap_icon (f, file)
11723 struct frame *f;
11724 Lisp_Object file;
11726 int bitmap_id;
11728 if (FRAME_X_WINDOW (f) == 0)
11729 return 1;
11731 /* Free up our existing icon bitmap if any. */
11732 if (f->output_data.x->icon_bitmap > 0)
11733 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11734 f->output_data.x->icon_bitmap = 0;
11736 if (STRINGP (file))
11737 bitmap_id = x_create_bitmap_from_file (f, file);
11738 else
11740 /* Create the GNU bitmap if necessary. */
11741 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11742 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11743 = x_create_bitmap_from_data (f, gnu_bits,
11744 gnu_width, gnu_height);
11746 /* The first time we create the GNU bitmap,
11747 this increments the ref-count one extra time.
11748 As a result, the GNU bitmap is never freed.
11749 That way, we don't have to worry about allocating it again. */
11750 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11752 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11755 x_wm_set_icon_pixmap (f, bitmap_id);
11756 f->output_data.x->icon_bitmap = bitmap_id;
11758 return 0;
11762 /* Make the x-window of frame F use a rectangle with text.
11763 Use ICON_NAME as the text. */
11766 x_text_icon (f, icon_name)
11767 struct frame *f;
11768 char *icon_name;
11770 if (FRAME_X_WINDOW (f) == 0)
11771 return 1;
11773 #ifdef HAVE_X11R4
11775 XTextProperty text;
11776 text.value = (unsigned char *) icon_name;
11777 text.encoding = XA_STRING;
11778 text.format = 8;
11779 text.nitems = strlen (icon_name);
11780 #ifdef USE_X_TOOLKIT
11781 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11782 &text);
11783 #else /* not USE_X_TOOLKIT */
11784 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11785 #endif /* not USE_X_TOOLKIT */
11787 #else /* not HAVE_X11R4 */
11788 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11789 #endif /* not HAVE_X11R4 */
11791 if (f->output_data.x->icon_bitmap > 0)
11792 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11793 f->output_data.x->icon_bitmap = 0;
11794 x_wm_set_icon_pixmap (f, 0);
11796 return 0;
11799 #define X_ERROR_MESSAGE_SIZE 200
11801 /* If non-nil, this should be a string.
11802 It means catch X errors and store the error message in this string. */
11804 static Lisp_Object x_error_message_string;
11806 /* An X error handler which stores the error message in
11807 x_error_message_string. This is called from x_error_handler if
11808 x_catch_errors is in effect. */
11810 static void
11811 x_error_catcher (display, error)
11812 Display *display;
11813 XErrorEvent *error;
11815 XGetErrorText (display, error->error_code,
11816 XSTRING (x_error_message_string)->data,
11817 X_ERROR_MESSAGE_SIZE);
11820 /* Begin trapping X errors for display DPY. Actually we trap X errors
11821 for all displays, but DPY should be the display you are actually
11822 operating on.
11824 After calling this function, X protocol errors no longer cause
11825 Emacs to exit; instead, they are recorded in the string
11826 stored in x_error_message_string.
11828 Calling x_check_errors signals an Emacs error if an X error has
11829 occurred since the last call to x_catch_errors or x_check_errors.
11831 Calling x_uncatch_errors resumes the normal error handling. */
11833 void x_check_errors ();
11834 static Lisp_Object x_catch_errors_unwind ();
11837 x_catch_errors (dpy)
11838 Display *dpy;
11840 int count = specpdl_ptr - specpdl;
11842 /* Make sure any errors from previous requests have been dealt with. */
11843 XSync (dpy, False);
11845 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11847 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11848 XSTRING (x_error_message_string)->data[0] = 0;
11850 return count;
11853 /* Unbind the binding that we made to check for X errors. */
11855 static Lisp_Object
11856 x_catch_errors_unwind (old_val)
11857 Lisp_Object old_val;
11859 x_error_message_string = old_val;
11860 return Qnil;
11863 /* If any X protocol errors have arrived since the last call to
11864 x_catch_errors or x_check_errors, signal an Emacs error using
11865 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11867 void
11868 x_check_errors (dpy, format)
11869 Display *dpy;
11870 char *format;
11872 /* Make sure to catch any errors incurred so far. */
11873 XSync (dpy, False);
11875 if (XSTRING (x_error_message_string)->data[0])
11876 error (format, XSTRING (x_error_message_string)->data);
11879 /* Nonzero if we had any X protocol errors
11880 since we did x_catch_errors on DPY. */
11883 x_had_errors_p (dpy)
11884 Display *dpy;
11886 /* Make sure to catch any errors incurred so far. */
11887 XSync (dpy, False);
11889 return XSTRING (x_error_message_string)->data[0] != 0;
11892 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11894 void
11895 x_clear_errors (dpy)
11896 Display *dpy;
11898 XSTRING (x_error_message_string)->data[0] = 0;
11901 /* Stop catching X protocol errors and let them make Emacs die.
11902 DPY should be the display that was passed to x_catch_errors.
11903 COUNT should be the value that was returned by
11904 the corresponding call to x_catch_errors. */
11906 void
11907 x_uncatch_errors (dpy, count)
11908 Display *dpy;
11909 int count;
11911 unbind_to (count, Qnil);
11914 #if 0
11915 static unsigned int x_wire_count;
11916 x_trace_wire ()
11918 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11920 #endif /* ! 0 */
11923 /* Handle SIGPIPE, which can happen when the connection to a server
11924 simply goes away. SIGPIPE is handled by x_connection_signal.
11925 Don't need to do anything, because the write which caused the
11926 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11927 which will do the appropriate cleanup for us. */
11929 static SIGTYPE
11930 x_connection_signal (signalnum) /* If we don't have an argument, */
11931 int signalnum; /* some compilers complain in signal calls. */
11933 #ifdef USG
11934 /* USG systems forget handlers when they are used;
11935 must reestablish each time */
11936 signal (signalnum, x_connection_signal);
11937 #endif /* USG */
11941 /************************************************************************
11942 Handling X errors
11943 ************************************************************************/
11945 /* Error message passed to x_connection_closed. */
11947 static char *error_msg;
11949 /* Function installed as fatal_error_signal_hook in
11950 x_connection_closed. Print the X error message, and exit normally,
11951 instead of dumping core when XtCloseDisplay fails. */
11953 static void
11954 x_fatal_error_signal ()
11956 fprintf (stderr, "%s\n", error_msg);
11957 exit (70);
11960 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11961 the text of an error message that lead to the connection loss. */
11963 static SIGTYPE
11964 x_connection_closed (dpy, error_message)
11965 Display *dpy;
11966 char *error_message;
11968 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11969 Lisp_Object frame, tail;
11970 int count;
11972 error_msg = (char *) alloca (strlen (error_message) + 1);
11973 strcpy (error_msg, error_message);
11974 handling_signal = 0;
11976 /* Prevent being called recursively because of an error condition
11977 below. Otherwise, we might end up with printing ``can't find per
11978 display information'' in the recursive call instead of printing
11979 the original message here. */
11980 count = x_catch_errors (dpy);
11982 /* We have to close the display to inform Xt that it doesn't
11983 exist anymore. If we don't, Xt will continue to wait for
11984 events from the display. As a consequence, a sequence of
11986 M-x make-frame-on-display RET :1 RET
11987 ...kill the new frame, so that we get an IO error...
11988 M-x make-frame-on-display RET :1 RET
11990 will indefinitely wait in Xt for events for display `:1', opened
11991 in the first class to make-frame-on-display.
11993 Closing the display is reported to lead to a bus error on
11994 OpenWindows in certain situations. I suspect that is a bug
11995 in OpenWindows. I don't know how to cicumvent it here. */
11997 #ifdef USE_X_TOOLKIT
11998 /* If DPYINFO is null, this means we didn't open the display
11999 in the first place, so don't try to close it. */
12000 if (dpyinfo)
12002 extern void (*fatal_error_signal_hook) P_ ((void));
12003 fatal_error_signal_hook = x_fatal_error_signal;
12004 XtCloseDisplay (dpy);
12005 fatal_error_signal_hook = NULL;
12007 #endif
12009 /* Indicate that this display is dead. */
12010 if (dpyinfo)
12011 dpyinfo->display = 0;
12013 /* First delete frames whose mini-buffers are on frames
12014 that are on the dead display. */
12015 FOR_EACH_FRAME (tail, frame)
12017 Lisp_Object minibuf_frame;
12018 minibuf_frame
12019 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12020 if (FRAME_X_P (XFRAME (frame))
12021 && FRAME_X_P (XFRAME (minibuf_frame))
12022 && ! EQ (frame, minibuf_frame)
12023 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12024 Fdelete_frame (frame, Qt);
12027 /* Now delete all remaining frames on the dead display.
12028 We are now sure none of these is used as the mini-buffer
12029 for another frame that we need to delete. */
12030 FOR_EACH_FRAME (tail, frame)
12031 if (FRAME_X_P (XFRAME (frame))
12032 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12034 /* Set this to t so that Fdelete_frame won't get confused
12035 trying to find a replacement. */
12036 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12037 Fdelete_frame (frame, Qt);
12040 if (dpyinfo)
12041 x_delete_display (dpyinfo);
12043 x_uncatch_errors (dpy, count);
12045 if (x_display_list == 0)
12047 fprintf (stderr, "%s\n", error_msg);
12048 shut_down_emacs (0, 0, Qnil);
12049 exit (70);
12052 /* Ordinary stack unwind doesn't deal with these. */
12053 #ifdef SIGIO
12054 sigunblock (sigmask (SIGIO));
12055 #endif
12056 sigunblock (sigmask (SIGALRM));
12057 TOTALLY_UNBLOCK_INPUT;
12059 clear_waiting_for_input ();
12060 error ("%s", error_msg);
12064 /* This is the usual handler for X protocol errors.
12065 It kills all frames on the display that we got the error for.
12066 If that was the only one, it prints an error message and kills Emacs. */
12068 static void
12069 x_error_quitter (display, error)
12070 Display *display;
12071 XErrorEvent *error;
12073 char buf[256], buf1[356];
12075 /* Note that there is no real way portable across R3/R4 to get the
12076 original error handler. */
12078 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12079 sprintf (buf1, "X protocol error: %s on protocol request %d",
12080 buf, error->request_code);
12081 x_connection_closed (display, buf1);
12085 /* This is the first-level handler for X protocol errors.
12086 It calls x_error_quitter or x_error_catcher. */
12088 static int
12089 x_error_handler (display, error)
12090 Display *display;
12091 XErrorEvent *error;
12093 if (! NILP (x_error_message_string))
12094 x_error_catcher (display, error);
12095 else
12096 x_error_quitter (display, error);
12097 return 0;
12100 /* This is the handler for X IO errors, always.
12101 It kills all frames on the display that we lost touch with.
12102 If that was the only one, it prints an error message and kills Emacs. */
12104 static int
12105 x_io_error_quitter (display)
12106 Display *display;
12108 char buf[256];
12110 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12111 x_connection_closed (display, buf);
12112 return 0;
12115 /* Changing the font of the frame. */
12117 /* Give frame F the font named FONTNAME as its default font, and
12118 return the full name of that font. FONTNAME may be a wildcard
12119 pattern; in that case, we choose some font that fits the pattern.
12120 The return value shows which font we chose. */
12122 Lisp_Object
12123 x_new_font (f, fontname)
12124 struct frame *f;
12125 register char *fontname;
12127 struct font_info *fontp
12128 = FS_LOAD_FONT (f, 0, fontname, -1);
12130 if (!fontp)
12131 return Qnil;
12133 f->output_data.x->font = (XFontStruct *) (fontp->font);
12134 f->output_data.x->baseline_offset = fontp->baseline_offset;
12135 f->output_data.x->fontset = -1;
12137 x_compute_fringe_widths (f, 1);
12139 /* Compute the scroll bar width in character columns. */
12140 if (f->scroll_bar_pixel_width > 0)
12142 int wid = FONT_WIDTH (f->output_data.x->font);
12143 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12145 else
12147 int wid = FONT_WIDTH (f->output_data.x->font);
12148 f->scroll_bar_cols = (14 + wid - 1) / wid;
12151 /* Now make the frame display the given font. */
12152 if (FRAME_X_WINDOW (f) != 0)
12154 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12155 f->output_data.x->font->fid);
12156 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12157 f->output_data.x->font->fid);
12158 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12159 f->output_data.x->font->fid);
12161 frame_update_line_height (f);
12163 /* Don't change the size of a tip frame; there's no point in
12164 doing it because it's done in Fx_show_tip, and it leads to
12165 problems because the tip frame has no widget. */
12166 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12167 x_set_window_size (f, 0, f->width, f->height);
12169 else
12170 /* If we are setting a new frame's font for the first time,
12171 there are no faces yet, so this font's height is the line height. */
12172 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12174 return build_string (fontp->full_name);
12177 /* Give frame F the fontset named FONTSETNAME as its default font, and
12178 return the full name of that fontset. FONTSETNAME may be a wildcard
12179 pattern; in that case, we choose some fontset that fits the pattern.
12180 The return value shows which fontset we chose. */
12182 Lisp_Object
12183 x_new_fontset (f, fontsetname)
12184 struct frame *f;
12185 char *fontsetname;
12187 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12188 Lisp_Object result;
12190 if (fontset < 0)
12191 return Qnil;
12193 if (f->output_data.x->fontset == fontset)
12194 /* This fontset is already set in frame F. There's nothing more
12195 to do. */
12196 return fontset_name (fontset);
12198 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12200 if (!STRINGP (result))
12201 /* Can't load ASCII font. */
12202 return Qnil;
12204 /* Since x_new_font doesn't update any fontset information, do it now. */
12205 f->output_data.x->fontset = fontset;
12207 #ifdef HAVE_X_I18N
12208 if (FRAME_XIC (f)
12209 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12210 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12211 #endif
12213 return build_string (fontsetname);
12216 /* Compute actual fringe widths */
12218 void
12219 x_compute_fringe_widths (f, redraw)
12220 struct frame *f;
12221 int redraw;
12223 int o_left = f->output_data.x->left_fringe_width;
12224 int o_right = f->output_data.x->right_fringe_width;
12225 int o_cols = f->output_data.x->fringe_cols;
12227 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12228 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12229 int left_fringe_width, right_fringe_width;
12231 if (!NILP (left_fringe))
12232 left_fringe = Fcdr (left_fringe);
12233 if (!NILP (right_fringe))
12234 right_fringe = Fcdr (right_fringe);
12236 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12237 XINT (left_fringe));
12238 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12239 XINT (right_fringe));
12241 if (left_fringe_width || right_fringe_width)
12243 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12244 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12245 int conf_wid = left_wid + right_wid;
12246 int font_wid = FONT_WIDTH (f->output_data.x->font);
12247 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12248 int real_wid = cols * font_wid;
12249 if (left_wid && right_wid)
12251 if (left_fringe_width < 0)
12253 /* Left fringe width is fixed, adjust right fringe if necessary */
12254 f->output_data.x->left_fringe_width = left_wid;
12255 f->output_data.x->right_fringe_width = real_wid - left_wid;
12257 else if (right_fringe_width < 0)
12259 /* Right fringe width is fixed, adjust left fringe if necessary */
12260 f->output_data.x->left_fringe_width = real_wid - right_wid;
12261 f->output_data.x->right_fringe_width = right_wid;
12263 else
12265 /* Adjust both fringes with an equal amount.
12266 Note that we are doing integer arithmetic here, so don't
12267 lose a pixel if the total width is an odd number. */
12268 int fill = real_wid - conf_wid;
12269 f->output_data.x->left_fringe_width = left_wid + fill/2;
12270 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12273 else if (left_fringe_width)
12275 f->output_data.x->left_fringe_width = real_wid;
12276 f->output_data.x->right_fringe_width = 0;
12278 else
12280 f->output_data.x->left_fringe_width = 0;
12281 f->output_data.x->right_fringe_width = real_wid;
12283 f->output_data.x->fringe_cols = cols;
12284 f->output_data.x->fringes_extra = real_wid;
12286 else
12288 f->output_data.x->left_fringe_width = 0;
12289 f->output_data.x->right_fringe_width = 0;
12290 f->output_data.x->fringe_cols = 0;
12291 f->output_data.x->fringes_extra = 0;
12294 if (redraw && FRAME_VISIBLE_P (f))
12295 if (o_left != f->output_data.x->left_fringe_width ||
12296 o_right != f->output_data.x->right_fringe_width ||
12297 o_cols != f->output_data.x->fringe_cols)
12298 redraw_frame (f);
12301 /***********************************************************************
12302 X Input Methods
12303 ***********************************************************************/
12305 #ifdef HAVE_X_I18N
12307 #ifdef HAVE_X11R6
12309 /* XIM destroy callback function, which is called whenever the
12310 connection to input method XIM dies. CLIENT_DATA contains a
12311 pointer to the x_display_info structure corresponding to XIM. */
12313 static void
12314 xim_destroy_callback (xim, client_data, call_data)
12315 XIM xim;
12316 XPointer client_data;
12317 XPointer call_data;
12319 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12320 Lisp_Object frame, tail;
12322 BLOCK_INPUT;
12324 /* No need to call XDestroyIC.. */
12325 FOR_EACH_FRAME (tail, frame)
12327 struct frame *f = XFRAME (frame);
12328 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12330 FRAME_XIC (f) = NULL;
12331 if (FRAME_XIC_FONTSET (f))
12333 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12334 FRAME_XIC_FONTSET (f) = NULL;
12339 /* No need to call XCloseIM. */
12340 dpyinfo->xim = NULL;
12341 XFree (dpyinfo->xim_styles);
12342 UNBLOCK_INPUT;
12345 #endif /* HAVE_X11R6 */
12347 /* Open the connection to the XIM server on display DPYINFO.
12348 RESOURCE_NAME is the resource name Emacs uses. */
12350 static void
12351 xim_open_dpy (dpyinfo, resource_name)
12352 struct x_display_info *dpyinfo;
12353 char *resource_name;
12355 #ifdef USE_XIM
12356 XIM xim;
12358 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12359 dpyinfo->xim = xim;
12361 if (xim)
12363 #ifdef HAVE_X11R6
12364 XIMCallback destroy;
12365 #endif
12367 /* Get supported styles and XIM values. */
12368 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12370 #ifdef HAVE_X11R6
12371 destroy.callback = xim_destroy_callback;
12372 destroy.client_data = (XPointer)dpyinfo;
12373 /* This isn't prototyped in OSF 5.0. */
12374 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12375 #endif
12378 #else /* not USE_XIM */
12379 dpyinfo->xim = NULL;
12380 #endif /* not USE_XIM */
12384 #ifdef HAVE_X11R6_XIM
12386 struct xim_inst_t
12388 struct x_display_info *dpyinfo;
12389 char *resource_name;
12392 /* XIM instantiate callback function, which is called whenever an XIM
12393 server is available. DISPLAY is teh display of the XIM.
12394 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12395 when the callback was registered. */
12397 static void
12398 xim_instantiate_callback (display, client_data, call_data)
12399 Display *display;
12400 XPointer client_data;
12401 XPointer call_data;
12403 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12404 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12406 /* We don't support multiple XIM connections. */
12407 if (dpyinfo->xim)
12408 return;
12410 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12412 /* Create XIC for the existing frames on the same display, as long
12413 as they have no XIC. */
12414 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12416 Lisp_Object tail, frame;
12418 BLOCK_INPUT;
12419 FOR_EACH_FRAME (tail, frame)
12421 struct frame *f = XFRAME (frame);
12423 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12424 if (FRAME_XIC (f) == NULL)
12426 create_frame_xic (f);
12427 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12428 xic_set_statusarea (f);
12429 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12431 struct window *w = XWINDOW (f->selected_window);
12432 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12437 UNBLOCK_INPUT;
12441 #endif /* HAVE_X11R6_XIM */
12444 /* Open a connection to the XIM server on display DPYINFO.
12445 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12446 connection only at the first time. On X11R6, open the connection
12447 in the XIM instantiate callback function. */
12449 static void
12450 xim_initialize (dpyinfo, resource_name)
12451 struct x_display_info *dpyinfo;
12452 char *resource_name;
12454 #ifdef USE_XIM
12455 #ifdef HAVE_X11R6_XIM
12456 struct xim_inst_t *xim_inst;
12457 int len;
12459 dpyinfo->xim = NULL;
12460 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12461 xim_inst->dpyinfo = dpyinfo;
12462 len = strlen (resource_name);
12463 xim_inst->resource_name = (char *) xmalloc (len + 1);
12464 bcopy (resource_name, xim_inst->resource_name, len + 1);
12465 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12466 resource_name, EMACS_CLASS,
12467 xim_instantiate_callback,
12468 /* Fixme: This is XPointer in
12469 XFree86 but (XPointer *) on
12470 Tru64, at least. */
12471 (XPointer) xim_inst);
12472 #else /* not HAVE_X11R6_XIM */
12473 dpyinfo->xim = NULL;
12474 xim_open_dpy (dpyinfo, resource_name);
12475 #endif /* not HAVE_X11R6_XIM */
12477 #else /* not USE_XIM */
12478 dpyinfo->xim = NULL;
12479 #endif /* not USE_XIM */
12483 /* Close the connection to the XIM server on display DPYINFO. */
12485 static void
12486 xim_close_dpy (dpyinfo)
12487 struct x_display_info *dpyinfo;
12489 #ifdef USE_XIM
12490 #ifdef HAVE_X11R6_XIM
12491 if (dpyinfo->display)
12492 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12493 NULL, EMACS_CLASS,
12494 xim_instantiate_callback, NULL);
12495 #endif /* not HAVE_X11R6_XIM */
12496 if (dpyinfo->display)
12497 XCloseIM (dpyinfo->xim);
12498 dpyinfo->xim = NULL;
12499 XFree (dpyinfo->xim_styles);
12500 #endif /* USE_XIM */
12503 #endif /* not HAVE_X11R6_XIM */
12507 /* Calculate the absolute position in frame F
12508 from its current recorded position values and gravity. */
12510 void
12511 x_calc_absolute_position (f)
12512 struct frame *f;
12514 Window child;
12515 int win_x = 0, win_y = 0;
12516 int flags = f->output_data.x->size_hint_flags;
12517 int this_window;
12519 /* We have nothing to do if the current position
12520 is already for the top-left corner. */
12521 if (! ((flags & XNegative) || (flags & YNegative)))
12522 return;
12524 #ifdef USE_X_TOOLKIT
12525 this_window = XtWindow (f->output_data.x->widget);
12526 #else
12527 this_window = FRAME_X_WINDOW (f);
12528 #endif
12530 /* Find the position of the outside upper-left corner of
12531 the inner window, with respect to the outer window.
12532 But do this only if we will need the results. */
12533 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12535 int count;
12537 BLOCK_INPUT;
12538 count = x_catch_errors (FRAME_X_DISPLAY (f));
12539 while (1)
12541 x_clear_errors (FRAME_X_DISPLAY (f));
12542 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12544 /* From-window, to-window. */
12545 this_window,
12546 f->output_data.x->parent_desc,
12548 /* From-position, to-position. */
12549 0, 0, &win_x, &win_y,
12551 /* Child of win. */
12552 &child);
12553 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12555 Window newroot, newparent = 0xdeadbeef;
12556 Window *newchildren;
12557 unsigned int nchildren;
12559 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12560 &newparent, &newchildren, &nchildren))
12561 break;
12563 XFree ((char *) newchildren);
12565 f->output_data.x->parent_desc = newparent;
12567 else
12568 break;
12571 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12572 UNBLOCK_INPUT;
12575 /* Treat negative positions as relative to the leftmost bottommost
12576 position that fits on the screen. */
12577 if (flags & XNegative)
12578 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12579 - 2 * f->output_data.x->border_width - win_x
12580 - PIXEL_WIDTH (f)
12581 + f->output_data.x->left_pos);
12584 int height = PIXEL_HEIGHT (f);
12586 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12587 /* Something is fishy here. When using Motif, starting Emacs with
12588 `-g -0-0', the frame appears too low by a few pixels.
12590 This seems to be so because initially, while Emacs is starting,
12591 the column widget's height and the frame's pixel height are
12592 different. The column widget's height is the right one. In
12593 later invocations, when Emacs is up, the frame's pixel height
12594 is right, though.
12596 It's not obvious where the initial small difference comes from.
12597 2000-12-01, gerd. */
12599 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12600 #endif
12602 if (flags & YNegative)
12603 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12604 - 2 * f->output_data.x->border_width
12605 - win_y
12606 - height
12607 + f->output_data.x->top_pos);
12610 /* The left_pos and top_pos
12611 are now relative to the top and left screen edges,
12612 so the flags should correspond. */
12613 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12616 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12617 to really change the position, and 0 when calling from
12618 x_make_frame_visible (in that case, XOFF and YOFF are the current
12619 position values). It is -1 when calling from x_set_frame_parameters,
12620 which means, do adjust for borders but don't change the gravity. */
12622 void
12623 x_set_offset (f, xoff, yoff, change_gravity)
12624 struct frame *f;
12625 register int xoff, yoff;
12626 int change_gravity;
12628 int modified_top, modified_left;
12630 if (change_gravity > 0)
12632 f->output_data.x->top_pos = yoff;
12633 f->output_data.x->left_pos = xoff;
12634 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12635 if (xoff < 0)
12636 f->output_data.x->size_hint_flags |= XNegative;
12637 if (yoff < 0)
12638 f->output_data.x->size_hint_flags |= YNegative;
12639 f->output_data.x->win_gravity = NorthWestGravity;
12641 x_calc_absolute_position (f);
12643 BLOCK_INPUT;
12644 x_wm_set_size_hint (f, (long) 0, 0);
12646 modified_left = f->output_data.x->left_pos;
12647 modified_top = f->output_data.x->top_pos;
12648 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12649 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12650 /* It is a mystery why we need to add the border_width here
12651 when the frame is already visible, but experiment says we do. */
12652 if (change_gravity != 0)
12654 modified_left += f->output_data.x->border_width;
12655 modified_top += f->output_data.x->border_width;
12657 #endif
12659 #ifdef USE_X_TOOLKIT
12660 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12661 modified_left, modified_top);
12662 #else /* not USE_X_TOOLKIT */
12663 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12664 modified_left, modified_top);
12665 #endif /* not USE_X_TOOLKIT */
12666 UNBLOCK_INPUT;
12669 /* Check if we need to resize the frame due to a fullscreen request.
12670 If so needed, resize the frame. */
12671 static void
12672 x_check_fullscreen (f)
12673 struct frame *f;
12675 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12677 int width, height, ign;
12679 x_real_positions (f, &f->output_data.x->left_pos,
12680 &f->output_data.x->top_pos);
12682 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12684 /* We do not need to move the window, it shall be taken care of
12685 when setting WM manager hints.
12686 If the frame is visible already, the position is checked by
12687 x_check_fullscreen_move. */
12688 if (f->width != width || f->height != height)
12690 change_frame_size (f, height, width, 0, 1, 0);
12691 SET_FRAME_GARBAGED (f);
12692 cancel_mouse_face (f);
12694 /* Wait for the change of frame size to occur */
12695 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12701 /* If frame parameters are set after the frame is mapped, we need to move
12702 the window. This is done in xfns.c.
12703 Some window managers moves the window to the right position, some
12704 moves the outer window manager window to the specified position.
12705 Here we check that we are in the right spot. If not, make a second
12706 move, assuming we are dealing with the second kind of window manager. */
12707 static void
12708 x_check_fullscreen_move (f)
12709 struct frame *f;
12711 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12713 int expect_top = f->output_data.x->top_pos;
12714 int expect_left = f->output_data.x->left_pos;
12716 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12717 expect_top = 0;
12718 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12719 expect_left = 0;
12721 if (expect_top != f->output_data.x->top_pos
12722 || expect_left != f->output_data.x->left_pos)
12723 x_set_offset (f, expect_left, expect_top, 1);
12725 /* Just do this once */
12726 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12731 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12732 wanted positions of the WM window (not emacs window).
12733 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12734 window (FRAME_X_WINDOW).
12736 void
12737 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12738 struct frame *f;
12739 int *width;
12740 int *height;
12741 int *top_pos;
12742 int *left_pos;
12744 int newwidth = f->width, newheight = f->height;
12746 *top_pos = f->output_data.x->top_pos;
12747 *left_pos = f->output_data.x->left_pos;
12749 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12751 int ph;
12753 ph = FRAME_X_DISPLAY_INFO (f)->height;
12754 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12755 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12756 - f->output_data.x->y_pixels_diff;
12757 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12758 *top_pos = 0;
12761 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12763 int pw;
12765 pw = FRAME_X_DISPLAY_INFO (f)->width;
12766 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12767 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12768 - f->output_data.x->x_pixels_diff;
12769 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12770 *left_pos = 0;
12773 *width = newwidth;
12774 *height = newheight;
12778 /* Change the size of frame F's X window to COLS/ROWS in the case F
12779 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12780 top-left-corner window gravity for this size change and subsequent
12781 size changes. Otherwise we leave the window gravity unchanged. */
12783 static void
12784 x_set_window_size_1 (f, change_gravity, cols, rows)
12785 struct frame *f;
12786 int change_gravity;
12787 int cols, rows;
12789 int pixelwidth, pixelheight;
12791 check_frame_size (f, &rows, &cols);
12792 f->output_data.x->vertical_scroll_bar_extra
12793 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12795 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12796 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12797 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12799 x_compute_fringe_widths (f, 0);
12801 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12802 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12804 f->output_data.x->win_gravity = NorthWestGravity;
12805 x_wm_set_size_hint (f, (long) 0, 0);
12807 XSync (FRAME_X_DISPLAY (f), False);
12808 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12809 pixelwidth, pixelheight);
12811 /* Now, strictly speaking, we can't be sure that this is accurate,
12812 but the window manager will get around to dealing with the size
12813 change request eventually, and we'll hear how it went when the
12814 ConfigureNotify event gets here.
12816 We could just not bother storing any of this information here,
12817 and let the ConfigureNotify event set everything up, but that
12818 might be kind of confusing to the Lisp code, since size changes
12819 wouldn't be reported in the frame parameters until some random
12820 point in the future when the ConfigureNotify event arrives.
12822 We pass 1 for DELAY since we can't run Lisp code inside of
12823 a BLOCK_INPUT. */
12824 change_frame_size (f, rows, cols, 0, 1, 0);
12825 PIXEL_WIDTH (f) = pixelwidth;
12826 PIXEL_HEIGHT (f) = pixelheight;
12828 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12829 receive in the ConfigureNotify event; if we get what we asked
12830 for, then the event won't cause the screen to become garbaged, so
12831 we have to make sure to do it here. */
12832 SET_FRAME_GARBAGED (f);
12834 XFlush (FRAME_X_DISPLAY (f));
12838 /* Call this to change the size of frame F's x-window.
12839 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12840 for this size change and subsequent size changes.
12841 Otherwise we leave the window gravity unchanged. */
12843 void
12844 x_set_window_size (f, change_gravity, cols, rows)
12845 struct frame *f;
12846 int change_gravity;
12847 int cols, rows;
12849 BLOCK_INPUT;
12851 #ifdef USE_X_TOOLKIT
12853 if (f->output_data.x->widget != NULL)
12855 /* The x and y position of the widget is clobbered by the
12856 call to XtSetValues within EmacsFrameSetCharSize.
12857 This is a real kludge, but I don't understand Xt so I can't
12858 figure out a correct fix. Can anyone else tell me? -- rms. */
12859 int xpos = f->output_data.x->widget->core.x;
12860 int ypos = f->output_data.x->widget->core.y;
12861 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12862 f->output_data.x->widget->core.x = xpos;
12863 f->output_data.x->widget->core.y = ypos;
12865 else
12866 x_set_window_size_1 (f, change_gravity, cols, rows);
12868 #else /* not USE_X_TOOLKIT */
12870 x_set_window_size_1 (f, change_gravity, cols, rows);
12872 #endif /* not USE_X_TOOLKIT */
12874 /* If cursor was outside the new size, mark it as off. */
12875 mark_window_cursors_off (XWINDOW (f->root_window));
12877 /* Clear out any recollection of where the mouse highlighting was,
12878 since it might be in a place that's outside the new frame size.
12879 Actually checking whether it is outside is a pain in the neck,
12880 so don't try--just let the highlighting be done afresh with new size. */
12881 cancel_mouse_face (f);
12883 UNBLOCK_INPUT;
12886 /* Mouse warping. */
12888 void
12889 x_set_mouse_position (f, x, y)
12890 struct frame *f;
12891 int x, y;
12893 int pix_x, pix_y;
12895 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
12896 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
12898 if (pix_x < 0) pix_x = 0;
12899 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
12901 if (pix_y < 0) pix_y = 0;
12902 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
12904 BLOCK_INPUT;
12906 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12907 0, 0, 0, 0, pix_x, pix_y);
12908 UNBLOCK_INPUT;
12911 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
12913 void
12914 x_set_mouse_pixel_position (f, pix_x, pix_y)
12915 struct frame *f;
12916 int pix_x, pix_y;
12918 BLOCK_INPUT;
12920 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
12921 0, 0, 0, 0, pix_x, pix_y);
12922 UNBLOCK_INPUT;
12925 /* focus shifting, raising and lowering. */
12927 void
12928 x_focus_on_frame (f)
12929 struct frame *f;
12931 #if 0 /* This proves to be unpleasant. */
12932 x_raise_frame (f);
12933 #endif
12934 #if 0
12935 /* I don't think that the ICCCM allows programs to do things like this
12936 without the interaction of the window manager. Whatever you end up
12937 doing with this code, do it to x_unfocus_frame too. */
12938 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12939 RevertToPointerRoot, CurrentTime);
12940 #endif /* ! 0 */
12943 void
12944 x_unfocus_frame (f)
12945 struct frame *f;
12947 #if 0
12948 /* Look at the remarks in x_focus_on_frame. */
12949 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12950 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12951 RevertToPointerRoot, CurrentTime);
12952 #endif /* ! 0 */
12955 /* Raise frame F. */
12957 void
12958 x_raise_frame (f)
12959 struct frame *f;
12961 if (f->async_visible)
12963 BLOCK_INPUT;
12964 #ifdef USE_X_TOOLKIT
12965 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12966 #else /* not USE_X_TOOLKIT */
12967 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12968 #endif /* not USE_X_TOOLKIT */
12969 XFlush (FRAME_X_DISPLAY (f));
12970 UNBLOCK_INPUT;
12974 /* Lower frame F. */
12976 void
12977 x_lower_frame (f)
12978 struct frame *f;
12980 if (f->async_visible)
12982 BLOCK_INPUT;
12983 #ifdef USE_X_TOOLKIT
12984 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12985 #else /* not USE_X_TOOLKIT */
12986 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12987 #endif /* not USE_X_TOOLKIT */
12988 XFlush (FRAME_X_DISPLAY (f));
12989 UNBLOCK_INPUT;
12993 static void
12994 XTframe_raise_lower (f, raise_flag)
12995 FRAME_PTR f;
12996 int raise_flag;
12998 if (raise_flag)
12999 x_raise_frame (f);
13000 else
13001 x_lower_frame (f);
13004 /* Change of visibility. */
13006 /* This tries to wait until the frame is really visible.
13007 However, if the window manager asks the user where to position
13008 the frame, this will return before the user finishes doing that.
13009 The frame will not actually be visible at that time,
13010 but it will become visible later when the window manager
13011 finishes with it. */
13013 void
13014 x_make_frame_visible (f)
13015 struct frame *f;
13017 Lisp_Object type;
13018 int original_top, original_left;
13019 int retry_count = 2;
13021 retry:
13023 BLOCK_INPUT;
13025 type = x_icon_type (f);
13026 if (!NILP (type))
13027 x_bitmap_icon (f, type);
13029 if (! FRAME_VISIBLE_P (f))
13031 /* We test FRAME_GARBAGED_P here to make sure we don't
13032 call x_set_offset a second time
13033 if we get to x_make_frame_visible a second time
13034 before the window gets really visible. */
13035 if (! FRAME_ICONIFIED_P (f)
13036 && ! f->output_data.x->asked_for_visible)
13037 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13039 f->output_data.x->asked_for_visible = 1;
13041 if (! EQ (Vx_no_window_manager, Qt))
13042 x_wm_set_window_state (f, NormalState);
13043 #ifdef USE_X_TOOLKIT
13044 /* This was XtPopup, but that did nothing for an iconified frame. */
13045 XtMapWidget (f->output_data.x->widget);
13046 #else /* not USE_X_TOOLKIT */
13047 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13048 #endif /* not USE_X_TOOLKIT */
13049 #if 0 /* This seems to bring back scroll bars in the wrong places
13050 if the window configuration has changed. They seem
13051 to come back ok without this. */
13052 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13053 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13054 #endif
13057 XFlush (FRAME_X_DISPLAY (f));
13059 /* Synchronize to ensure Emacs knows the frame is visible
13060 before we do anything else. We do this loop with input not blocked
13061 so that incoming events are handled. */
13063 Lisp_Object frame;
13064 int count;
13065 /* This must be before UNBLOCK_INPUT
13066 since events that arrive in response to the actions above
13067 will set it when they are handled. */
13068 int previously_visible = f->output_data.x->has_been_visible;
13070 original_left = f->output_data.x->left_pos;
13071 original_top = f->output_data.x->top_pos;
13073 /* This must come after we set COUNT. */
13074 UNBLOCK_INPUT;
13076 /* We unblock here so that arriving X events are processed. */
13078 /* Now move the window back to where it was "supposed to be".
13079 But don't do it if the gravity is negative.
13080 When the gravity is negative, this uses a position
13081 that is 3 pixels too low. Perhaps that's really the border width.
13083 Don't do this if the window has never been visible before,
13084 because the window manager may choose the position
13085 and we don't want to override it. */
13087 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13088 && f->output_data.x->win_gravity == NorthWestGravity
13089 && previously_visible)
13091 Drawable rootw;
13092 int x, y;
13093 unsigned int width, height, border, depth;
13095 BLOCK_INPUT;
13097 /* On some window managers (such as FVWM) moving an existing
13098 window, even to the same place, causes the window manager
13099 to introduce an offset. This can cause the window to move
13100 to an unexpected location. Check the geometry (a little
13101 slow here) and then verify that the window is in the right
13102 place. If the window is not in the right place, move it
13103 there, and take the potential window manager hit. */
13104 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13105 &rootw, &x, &y, &width, &height, &border, &depth);
13107 if (original_left != x || original_top != y)
13108 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13109 original_left, original_top);
13111 UNBLOCK_INPUT;
13114 XSETFRAME (frame, f);
13116 /* Wait until the frame is visible. Process X events until a
13117 MapNotify event has been seen, or until we think we won't get a
13118 MapNotify at all.. */
13119 for (count = input_signal_count + 10;
13120 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13122 /* Force processing of queued events. */
13123 x_sync (f);
13125 /* Machines that do polling rather than SIGIO have been
13126 observed to go into a busy-wait here. So we'll fake an
13127 alarm signal to let the handler know that there's something
13128 to be read. We used to raise a real alarm, but it seems
13129 that the handler isn't always enabled here. This is
13130 probably a bug. */
13131 if (input_polling_used ())
13133 /* It could be confusing if a real alarm arrives while
13134 processing the fake one. Turn it off and let the
13135 handler reset it. */
13136 extern void poll_for_input_1 P_ ((void));
13137 int old_poll_suppress_count = poll_suppress_count;
13138 poll_suppress_count = 1;
13139 poll_for_input_1 ();
13140 poll_suppress_count = old_poll_suppress_count;
13143 /* See if a MapNotify event has been processed. */
13144 FRAME_SAMPLE_VISIBILITY (f);
13147 /* 2000-09-28: In
13149 (let ((f (selected-frame)))
13150 (iconify-frame f)
13151 (raise-frame f))
13153 the frame is not raised with various window managers on
13154 FreeBSD, Linux and Solaris. It turns out that, for some
13155 unknown reason, the call to XtMapWidget is completely ignored.
13156 Mapping the widget a second time works. */
13158 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13159 goto retry;
13163 /* Change from mapped state to withdrawn state. */
13165 /* Make the frame visible (mapped and not iconified). */
13167 void
13168 x_make_frame_invisible (f)
13169 struct frame *f;
13171 Window window;
13173 #ifdef USE_X_TOOLKIT
13174 /* Use the frame's outermost window, not the one we normally draw on. */
13175 window = XtWindow (f->output_data.x->widget);
13176 #else /* not USE_X_TOOLKIT */
13177 window = FRAME_X_WINDOW (f);
13178 #endif /* not USE_X_TOOLKIT */
13180 /* Don't keep the highlight on an invisible frame. */
13181 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13182 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13184 #if 0/* This might add unreliability; I don't trust it -- rms. */
13185 if (! f->async_visible && ! f->async_iconified)
13186 return;
13187 #endif
13189 BLOCK_INPUT;
13191 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13192 that the current position of the window is user-specified, rather than
13193 program-specified, so that when the window is mapped again, it will be
13194 placed at the same location, without forcing the user to position it
13195 by hand again (they have already done that once for this window.) */
13196 x_wm_set_size_hint (f, (long) 0, 1);
13198 #ifdef HAVE_X11R4
13200 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13201 DefaultScreen (FRAME_X_DISPLAY (f))))
13203 UNBLOCK_INPUT_RESIGNAL;
13204 error ("Can't notify window manager of window withdrawal");
13206 #else /* ! defined (HAVE_X11R4) */
13208 /* Tell the window manager what we're going to do. */
13209 if (! EQ (Vx_no_window_manager, Qt))
13211 XEvent unmap;
13213 unmap.xunmap.type = UnmapNotify;
13214 unmap.xunmap.window = window;
13215 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13216 unmap.xunmap.from_configure = False;
13217 if (! XSendEvent (FRAME_X_DISPLAY (f),
13218 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13219 False,
13220 SubstructureRedirectMaskSubstructureNotifyMask,
13221 &unmap))
13223 UNBLOCK_INPUT_RESIGNAL;
13224 error ("Can't notify window manager of withdrawal");
13228 /* Unmap the window ourselves. Cheeky! */
13229 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13230 #endif /* ! defined (HAVE_X11R4) */
13232 /* We can't distinguish this from iconification
13233 just by the event that we get from the server.
13234 So we can't win using the usual strategy of letting
13235 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13236 and synchronize with the server to make sure we agree. */
13237 f->visible = 0;
13238 FRAME_ICONIFIED_P (f) = 0;
13239 f->async_visible = 0;
13240 f->async_iconified = 0;
13242 x_sync (f);
13244 UNBLOCK_INPUT;
13247 /* Change window state from mapped to iconified. */
13249 void
13250 x_iconify_frame (f)
13251 struct frame *f;
13253 int result;
13254 Lisp_Object type;
13256 /* Don't keep the highlight on an invisible frame. */
13257 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13258 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13260 if (f->async_iconified)
13261 return;
13263 BLOCK_INPUT;
13265 FRAME_SAMPLE_VISIBILITY (f);
13267 type = x_icon_type (f);
13268 if (!NILP (type))
13269 x_bitmap_icon (f, type);
13271 #ifdef USE_X_TOOLKIT
13273 if (! FRAME_VISIBLE_P (f))
13275 if (! EQ (Vx_no_window_manager, Qt))
13276 x_wm_set_window_state (f, IconicState);
13277 /* This was XtPopup, but that did nothing for an iconified frame. */
13278 XtMapWidget (f->output_data.x->widget);
13279 /* The server won't give us any event to indicate
13280 that an invisible frame was changed to an icon,
13281 so we have to record it here. */
13282 f->iconified = 1;
13283 f->visible = 1;
13284 f->async_iconified = 1;
13285 f->async_visible = 0;
13286 UNBLOCK_INPUT;
13287 return;
13290 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13291 XtWindow (f->output_data.x->widget),
13292 DefaultScreen (FRAME_X_DISPLAY (f)));
13293 UNBLOCK_INPUT;
13295 if (!result)
13296 error ("Can't notify window manager of iconification");
13298 f->async_iconified = 1;
13299 f->async_visible = 0;
13302 BLOCK_INPUT;
13303 XFlush (FRAME_X_DISPLAY (f));
13304 UNBLOCK_INPUT;
13305 #else /* not USE_X_TOOLKIT */
13307 /* Make sure the X server knows where the window should be positioned,
13308 in case the user deiconifies with the window manager. */
13309 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13310 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13312 /* Since we don't know which revision of X we're running, we'll use both
13313 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13315 /* X11R4: send a ClientMessage to the window manager using the
13316 WM_CHANGE_STATE type. */
13318 XEvent message;
13320 message.xclient.window = FRAME_X_WINDOW (f);
13321 message.xclient.type = ClientMessage;
13322 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13323 message.xclient.format = 32;
13324 message.xclient.data.l[0] = IconicState;
13326 if (! XSendEvent (FRAME_X_DISPLAY (f),
13327 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13328 False,
13329 SubstructureRedirectMask | SubstructureNotifyMask,
13330 &message))
13332 UNBLOCK_INPUT_RESIGNAL;
13333 error ("Can't notify window manager of iconification");
13337 /* X11R3: set the initial_state field of the window manager hints to
13338 IconicState. */
13339 x_wm_set_window_state (f, IconicState);
13341 if (!FRAME_VISIBLE_P (f))
13343 /* If the frame was withdrawn, before, we must map it. */
13344 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13347 f->async_iconified = 1;
13348 f->async_visible = 0;
13350 XFlush (FRAME_X_DISPLAY (f));
13351 UNBLOCK_INPUT;
13352 #endif /* not USE_X_TOOLKIT */
13356 /* Free X resources of frame F. */
13358 void
13359 x_free_frame_resources (f)
13360 struct frame *f;
13362 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13363 Lisp_Object bar;
13364 struct scroll_bar *b;
13366 BLOCK_INPUT;
13368 /* If a display connection is dead, don't try sending more
13369 commands to the X server. */
13370 if (dpyinfo->display)
13372 if (f->output_data.x->icon_desc)
13373 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13375 #ifdef USE_X_TOOLKIT
13376 /* Explicitly destroy the scroll bars of the frame. Without
13377 this, we get "BadDrawable" errors from the toolkit later on,
13378 presumably from expose events generated for the disappearing
13379 toolkit scroll bars. */
13380 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13382 b = XSCROLL_BAR (bar);
13383 x_scroll_bar_remove (b);
13385 #endif
13387 #ifdef HAVE_X_I18N
13388 if (FRAME_XIC (f))
13389 free_frame_xic (f);
13390 #endif
13392 #ifdef USE_X_TOOLKIT
13393 if (f->output_data.x->widget)
13395 XtDestroyWidget (f->output_data.x->widget);
13396 f->output_data.x->widget = NULL;
13398 /* Tooltips don't have widgets, only a simple X window, even if
13399 we are using a toolkit. */
13400 else if (FRAME_X_WINDOW (f))
13401 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13403 free_frame_menubar (f);
13404 #else /* !USE_X_TOOLKIT */
13405 if (FRAME_X_WINDOW (f))
13406 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13407 #endif /* !USE_X_TOOLKIT */
13409 unload_color (f, f->output_data.x->foreground_pixel);
13410 unload_color (f, f->output_data.x->background_pixel);
13411 unload_color (f, f->output_data.x->cursor_pixel);
13412 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13413 unload_color (f, f->output_data.x->border_pixel);
13414 unload_color (f, f->output_data.x->mouse_pixel);
13416 if (f->output_data.x->scroll_bar_background_pixel != -1)
13417 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13418 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13419 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13420 #ifdef USE_TOOLKIT_SCROLL_BARS
13421 /* Scrollbar shadow colors. */
13422 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13423 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13424 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13425 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13426 #endif /* USE_TOOLKIT_SCROLL_BARS */
13427 if (f->output_data.x->white_relief.allocated_p)
13428 unload_color (f, f->output_data.x->white_relief.pixel);
13429 if (f->output_data.x->black_relief.allocated_p)
13430 unload_color (f, f->output_data.x->black_relief.pixel);
13432 if (FRAME_FACE_CACHE (f))
13433 free_frame_faces (f);
13435 x_free_gcs (f);
13436 XFlush (FRAME_X_DISPLAY (f));
13439 if (f->output_data.x->saved_menu_event)
13440 xfree (f->output_data.x->saved_menu_event);
13442 xfree (f->output_data.x);
13443 f->output_data.x = NULL;
13445 if (f == dpyinfo->x_focus_frame)
13446 dpyinfo->x_focus_frame = 0;
13447 if (f == dpyinfo->x_focus_event_frame)
13448 dpyinfo->x_focus_event_frame = 0;
13449 if (f == dpyinfo->x_highlight_frame)
13450 dpyinfo->x_highlight_frame = 0;
13452 if (f == dpyinfo->mouse_face_mouse_frame)
13454 dpyinfo->mouse_face_beg_row
13455 = dpyinfo->mouse_face_beg_col = -1;
13456 dpyinfo->mouse_face_end_row
13457 = dpyinfo->mouse_face_end_col = -1;
13458 dpyinfo->mouse_face_window = Qnil;
13459 dpyinfo->mouse_face_deferred_gc = 0;
13460 dpyinfo->mouse_face_mouse_frame = 0;
13463 UNBLOCK_INPUT;
13467 /* Destroy the X window of frame F. */
13469 void
13470 x_destroy_window (f)
13471 struct frame *f;
13473 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13475 /* If a display connection is dead, don't try sending more
13476 commands to the X server. */
13477 if (dpyinfo->display != 0)
13478 x_free_frame_resources (f);
13480 dpyinfo->reference_count--;
13484 /* Setting window manager hints. */
13486 /* Set the normal size hints for the window manager, for frame F.
13487 FLAGS is the flags word to use--or 0 meaning preserve the flags
13488 that the window now has.
13489 If USER_POSITION is nonzero, we set the USPosition
13490 flag (this is useful when FLAGS is 0). */
13492 void
13493 x_wm_set_size_hint (f, flags, user_position)
13494 struct frame *f;
13495 long flags;
13496 int user_position;
13498 XSizeHints size_hints;
13500 #ifdef USE_X_TOOLKIT
13501 Arg al[2];
13502 int ac = 0;
13503 Dimension widget_width, widget_height;
13504 Window window = XtWindow (f->output_data.x->widget);
13505 #else /* not USE_X_TOOLKIT */
13506 Window window = FRAME_X_WINDOW (f);
13507 #endif /* not USE_X_TOOLKIT */
13509 /* Setting PMaxSize caused various problems. */
13510 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13512 size_hints.x = f->output_data.x->left_pos;
13513 size_hints.y = f->output_data.x->top_pos;
13515 #ifdef USE_X_TOOLKIT
13516 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13517 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13518 XtGetValues (f->output_data.x->widget, al, ac);
13519 size_hints.height = widget_height;
13520 size_hints.width = widget_width;
13521 #else /* not USE_X_TOOLKIT */
13522 size_hints.height = PIXEL_HEIGHT (f);
13523 size_hints.width = PIXEL_WIDTH (f);
13524 #endif /* not USE_X_TOOLKIT */
13526 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13527 size_hints.height_inc = f->output_data.x->line_height;
13528 size_hints.max_width
13529 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13530 size_hints.max_height
13531 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13533 /* Calculate the base and minimum sizes.
13535 (When we use the X toolkit, we don't do it here.
13536 Instead we copy the values that the widgets are using, below.) */
13537 #ifndef USE_X_TOOLKIT
13539 int base_width, base_height;
13540 int min_rows = 0, min_cols = 0;
13542 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13543 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13545 check_frame_size (f, &min_rows, &min_cols);
13547 /* The window manager uses the base width hints to calculate the
13548 current number of rows and columns in the frame while
13549 resizing; min_width and min_height aren't useful for this
13550 purpose, since they might not give the dimensions for a
13551 zero-row, zero-column frame.
13553 We use the base_width and base_height members if we have
13554 them; otherwise, we set the min_width and min_height members
13555 to the size for a zero x zero frame. */
13557 #ifdef HAVE_X11R4
13558 size_hints.flags |= PBaseSize;
13559 size_hints.base_width = base_width;
13560 size_hints.base_height = base_height;
13561 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13562 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13563 #else
13564 size_hints.min_width = base_width;
13565 size_hints.min_height = base_height;
13566 #endif
13569 /* If we don't need the old flags, we don't need the old hint at all. */
13570 if (flags)
13572 size_hints.flags |= flags;
13573 goto no_read;
13575 #endif /* not USE_X_TOOLKIT */
13578 XSizeHints hints; /* Sometimes I hate X Windows... */
13579 long supplied_return;
13580 int value;
13582 #ifdef HAVE_X11R4
13583 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13584 &supplied_return);
13585 #else
13586 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13587 #endif
13589 #ifdef USE_X_TOOLKIT
13590 size_hints.base_height = hints.base_height;
13591 size_hints.base_width = hints.base_width;
13592 size_hints.min_height = hints.min_height;
13593 size_hints.min_width = hints.min_width;
13594 #endif
13596 if (flags)
13597 size_hints.flags |= flags;
13598 else
13600 if (value == 0)
13601 hints.flags = 0;
13602 if (hints.flags & PSize)
13603 size_hints.flags |= PSize;
13604 if (hints.flags & PPosition)
13605 size_hints.flags |= PPosition;
13606 if (hints.flags & USPosition)
13607 size_hints.flags |= USPosition;
13608 if (hints.flags & USSize)
13609 size_hints.flags |= USSize;
13613 #ifndef USE_X_TOOLKIT
13614 no_read:
13615 #endif
13617 #ifdef PWinGravity
13618 size_hints.win_gravity = f->output_data.x->win_gravity;
13619 size_hints.flags |= PWinGravity;
13621 if (user_position)
13623 size_hints.flags &= ~ PPosition;
13624 size_hints.flags |= USPosition;
13626 #endif /* PWinGravity */
13628 #ifdef HAVE_X11R4
13629 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13630 #else
13631 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13632 #endif
13635 /* Used for IconicState or NormalState */
13637 void
13638 x_wm_set_window_state (f, state)
13639 struct frame *f;
13640 int state;
13642 #ifdef USE_X_TOOLKIT
13643 Arg al[1];
13645 XtSetArg (al[0], XtNinitialState, state);
13646 XtSetValues (f->output_data.x->widget, al, 1);
13647 #else /* not USE_X_TOOLKIT */
13648 Window window = FRAME_X_WINDOW (f);
13650 f->output_data.x->wm_hints.flags |= StateHint;
13651 f->output_data.x->wm_hints.initial_state = state;
13653 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13654 #endif /* not USE_X_TOOLKIT */
13657 void
13658 x_wm_set_icon_pixmap (f, pixmap_id)
13659 struct frame *f;
13660 int pixmap_id;
13662 Pixmap icon_pixmap;
13664 #ifndef USE_X_TOOLKIT
13665 Window window = FRAME_X_WINDOW (f);
13666 #endif
13668 if (pixmap_id > 0)
13670 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13671 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13673 else
13675 /* It seems there is no way to turn off use of an icon pixmap.
13676 The following line does it, only if no icon has yet been created,
13677 for some window managers. But with mwm it crashes.
13678 Some people say it should clear the IconPixmapHint bit in this case,
13679 but that doesn't work, and the X consortium said it isn't the
13680 right thing at all. Since there is no way to win,
13681 best to explicitly give up. */
13682 #if 0
13683 f->output_data.x->wm_hints.icon_pixmap = None;
13684 #else
13685 return;
13686 #endif
13689 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13692 Arg al[1];
13693 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13694 XtSetValues (f->output_data.x->widget, al, 1);
13697 #else /* not USE_X_TOOLKIT */
13699 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13700 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13702 #endif /* not USE_X_TOOLKIT */
13705 void
13706 x_wm_set_icon_position (f, icon_x, icon_y)
13707 struct frame *f;
13708 int icon_x, icon_y;
13710 #ifdef USE_X_TOOLKIT
13711 Window window = XtWindow (f->output_data.x->widget);
13712 #else
13713 Window window = FRAME_X_WINDOW (f);
13714 #endif
13716 f->output_data.x->wm_hints.flags |= IconPositionHint;
13717 f->output_data.x->wm_hints.icon_x = icon_x;
13718 f->output_data.x->wm_hints.icon_y = icon_y;
13720 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13724 /***********************************************************************
13725 Fonts
13726 ***********************************************************************/
13728 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13730 struct font_info *
13731 x_get_font_info (f, font_idx)
13732 FRAME_PTR f;
13733 int font_idx;
13735 return (FRAME_X_FONT_TABLE (f) + font_idx);
13739 /* Return a list of names of available fonts matching PATTERN on frame F.
13741 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13742 to be listed.
13744 SIZE < 0 means include scalable fonts.
13746 Frame F null means we have not yet created any frame on X, and
13747 consult the first display in x_display_list. MAXNAMES sets a limit
13748 on how many fonts to match. */
13750 Lisp_Object
13751 x_list_fonts (f, pattern, size, maxnames)
13752 struct frame *f;
13753 Lisp_Object pattern;
13754 int size;
13755 int maxnames;
13757 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13758 Lisp_Object tem, second_best;
13759 struct x_display_info *dpyinfo
13760 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13761 Display *dpy = dpyinfo->display;
13762 int try_XLoadQueryFont = 0;
13763 int count;
13764 int allow_scalable_fonts_p = 0;
13766 if (size < 0)
13768 allow_scalable_fonts_p = 1;
13769 size = 0;
13772 patterns = Fassoc (pattern, Valternate_fontname_alist);
13773 if (NILP (patterns))
13774 patterns = Fcons (pattern, Qnil);
13776 if (maxnames == 1 && !size)
13777 /* We can return any single font matching PATTERN. */
13778 try_XLoadQueryFont = 1;
13780 for (; CONSP (patterns); patterns = XCDR (patterns))
13782 int num_fonts;
13783 char **names = NULL;
13785 pattern = XCAR (patterns);
13786 /* See if we cached the result for this particular query.
13787 The cache is an alist of the form:
13788 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13789 tem = XCDR (dpyinfo->name_list_element);
13790 key = Fcons (Fcons (pattern, make_number (maxnames)),
13791 allow_scalable_fonts_p ? Qt : Qnil);
13792 list = Fassoc (key, tem);
13793 if (!NILP (list))
13795 list = Fcdr_safe (list);
13796 /* We have a cashed list. Don't have to get the list again. */
13797 goto label_cached;
13800 /* At first, put PATTERN in the cache. */
13802 BLOCK_INPUT;
13803 count = x_catch_errors (dpy);
13805 if (try_XLoadQueryFont)
13807 XFontStruct *font;
13808 unsigned long value;
13810 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13811 if (x_had_errors_p (dpy))
13813 /* This error is perhaps due to insufficient memory on X
13814 server. Let's just ignore it. */
13815 font = NULL;
13816 x_clear_errors (dpy);
13819 if (font
13820 && XGetFontProperty (font, XA_FONT, &value))
13822 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13823 int len = strlen (name);
13824 char *tmp;
13826 /* If DXPC (a Differential X Protocol Compressor)
13827 Ver.3.7 is running, XGetAtomName will return null
13828 string. We must avoid such a name. */
13829 if (len == 0)
13830 try_XLoadQueryFont = 0;
13831 else
13833 num_fonts = 1;
13834 names = (char **) alloca (sizeof (char *));
13835 /* Some systems only allow alloca assigned to a
13836 simple var. */
13837 tmp = (char *) alloca (len + 1); names[0] = tmp;
13838 bcopy (name, names[0], len + 1);
13839 XFree (name);
13842 else
13843 try_XLoadQueryFont = 0;
13845 if (font)
13846 XFreeFont (dpy, font);
13849 if (!try_XLoadQueryFont)
13851 /* We try at least 10 fonts because XListFonts will return
13852 auto-scaled fonts at the head. */
13853 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13854 &num_fonts);
13855 if (x_had_errors_p (dpy))
13857 /* This error is perhaps due to insufficient memory on X
13858 server. Let's just ignore it. */
13859 names = NULL;
13860 x_clear_errors (dpy);
13864 x_uncatch_errors (dpy, count);
13865 UNBLOCK_INPUT;
13867 if (names)
13869 int i;
13871 /* Make a list of all the fonts we got back.
13872 Store that in the font cache for the display. */
13873 for (i = 0; i < num_fonts; i++)
13875 int width = 0;
13876 char *p = names[i];
13877 int average_width = -1, dashes = 0;
13879 /* Count the number of dashes in NAMES[I]. If there are
13880 14 dashes, and the field value following 12th dash
13881 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13882 is usually too ugly to be used for editing. Let's
13883 ignore it. */
13884 while (*p)
13885 if (*p++ == '-')
13887 dashes++;
13888 if (dashes == 7) /* PIXEL_SIZE field */
13889 width = atoi (p);
13890 else if (dashes == 12) /* AVERAGE_WIDTH field */
13891 average_width = atoi (p);
13894 if (allow_scalable_fonts_p
13895 || dashes < 14 || average_width != 0)
13897 tem = build_string (names[i]);
13898 if (NILP (Fassoc (tem, list)))
13900 if (STRINGP (Vx_pixel_size_width_font_regexp)
13901 && ((fast_c_string_match_ignore_case
13902 (Vx_pixel_size_width_font_regexp, names[i]))
13903 >= 0))
13904 /* We can set the value of PIXEL_SIZE to the
13905 width of this font. */
13906 list = Fcons (Fcons (tem, make_number (width)), list);
13907 else
13908 /* For the moment, width is not known. */
13909 list = Fcons (Fcons (tem, Qnil), list);
13914 if (!try_XLoadQueryFont)
13916 BLOCK_INPUT;
13917 XFreeFontNames (names);
13918 UNBLOCK_INPUT;
13922 /* Now store the result in the cache. */
13923 XSETCDR (dpyinfo->name_list_element,
13924 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
13926 label_cached:
13927 if (NILP (list)) continue; /* Try the remaining alternatives. */
13929 newlist = second_best = Qnil;
13930 /* Make a list of the fonts that have the right width. */
13931 for (; CONSP (list); list = XCDR (list))
13933 int found_size;
13935 tem = XCAR (list);
13937 if (!CONSP (tem) || NILP (XCAR (tem)))
13938 continue;
13939 if (!size)
13941 newlist = Fcons (XCAR (tem), newlist);
13942 continue;
13945 if (!INTEGERP (XCDR (tem)))
13947 /* Since we have not yet known the size of this font, we
13948 must try slow function call XLoadQueryFont. */
13949 XFontStruct *thisinfo;
13951 BLOCK_INPUT;
13952 count = x_catch_errors (dpy);
13953 thisinfo = XLoadQueryFont (dpy,
13954 XSTRING (XCAR (tem))->data);
13955 if (x_had_errors_p (dpy))
13957 /* This error is perhaps due to insufficient memory on X
13958 server. Let's just ignore it. */
13959 thisinfo = NULL;
13960 x_clear_errors (dpy);
13962 x_uncatch_errors (dpy, count);
13963 UNBLOCK_INPUT;
13965 if (thisinfo)
13967 XSETCDR (tem,
13968 (thisinfo->min_bounds.width == 0
13969 ? make_number (0)
13970 : make_number (thisinfo->max_bounds.width)));
13971 BLOCK_INPUT;
13972 XFreeFont (dpy, thisinfo);
13973 UNBLOCK_INPUT;
13975 else
13976 /* For unknown reason, the previous call of XListFont had
13977 returned a font which can't be opened. Record the size
13978 as 0 not to try to open it again. */
13979 XSETCDR (tem, make_number (0));
13982 found_size = XINT (XCDR (tem));
13983 if (found_size == size)
13984 newlist = Fcons (XCAR (tem), newlist);
13985 else if (found_size > 0)
13987 if (NILP (second_best))
13988 second_best = tem;
13989 else if (found_size < size)
13991 if (XINT (XCDR (second_best)) > size
13992 || XINT (XCDR (second_best)) < found_size)
13993 second_best = tem;
13995 else
13997 if (XINT (XCDR (second_best)) > size
13998 && XINT (XCDR (second_best)) > found_size)
13999 second_best = tem;
14003 if (!NILP (newlist))
14004 break;
14005 else if (!NILP (second_best))
14007 newlist = Fcons (XCAR (second_best), Qnil);
14008 break;
14012 return newlist;
14016 #if GLYPH_DEBUG
14018 /* Check that FONT is valid on frame F. It is if it can be found in F's
14019 font table. */
14021 static void
14022 x_check_font (f, font)
14023 struct frame *f;
14024 XFontStruct *font;
14026 int i;
14027 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14029 xassert (font != NULL);
14031 for (i = 0; i < dpyinfo->n_fonts; i++)
14032 if (dpyinfo->font_table[i].name
14033 && font == dpyinfo->font_table[i].font)
14034 break;
14036 xassert (i < dpyinfo->n_fonts);
14039 #endif /* GLYPH_DEBUG != 0 */
14041 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14042 Note: There are (broken) X fonts out there with invalid XFontStruct
14043 min_bounds contents. For example, handa@etl.go.jp reports that
14044 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14045 have font->min_bounds.width == 0. */
14047 static INLINE void
14048 x_font_min_bounds (font, w, h)
14049 XFontStruct *font;
14050 int *w, *h;
14052 *h = FONT_HEIGHT (font);
14053 *w = font->min_bounds.width;
14055 /* Try to handle the case where FONT->min_bounds has invalid
14056 contents. Since the only font known to have invalid min_bounds
14057 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14058 if (*w <= 0)
14059 *w = font->max_bounds.width;
14063 /* Compute the smallest character width and smallest font height over
14064 all fonts available on frame F. Set the members smallest_char_width
14065 and smallest_font_height in F's x_display_info structure to
14066 the values computed. Value is non-zero if smallest_font_height or
14067 smallest_char_width become smaller than they were before. */
14069 static int
14070 x_compute_min_glyph_bounds (f)
14071 struct frame *f;
14073 int i;
14074 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14075 XFontStruct *font;
14076 int old_width = dpyinfo->smallest_char_width;
14077 int old_height = dpyinfo->smallest_font_height;
14079 dpyinfo->smallest_font_height = 100000;
14080 dpyinfo->smallest_char_width = 100000;
14082 for (i = 0; i < dpyinfo->n_fonts; ++i)
14083 if (dpyinfo->font_table[i].name)
14085 struct font_info *fontp = dpyinfo->font_table + i;
14086 int w, h;
14088 font = (XFontStruct *) fontp->font;
14089 xassert (font != (XFontStruct *) ~0);
14090 x_font_min_bounds (font, &w, &h);
14092 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14093 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14096 xassert (dpyinfo->smallest_char_width > 0
14097 && dpyinfo->smallest_font_height > 0);
14099 return (dpyinfo->n_fonts == 1
14100 || dpyinfo->smallest_char_width < old_width
14101 || dpyinfo->smallest_font_height < old_height);
14105 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14106 pointer to the structure font_info while allocating it dynamically.
14107 If SIZE is 0, load any size of font.
14108 If loading is failed, return NULL. */
14110 struct font_info *
14111 x_load_font (f, fontname, size)
14112 struct frame *f;
14113 register char *fontname;
14114 int size;
14116 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14117 Lisp_Object font_names;
14118 int count;
14120 /* Get a list of all the fonts that match this name. Once we
14121 have a list of matching fonts, we compare them against the fonts
14122 we already have by comparing names. */
14123 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14125 if (!NILP (font_names))
14127 Lisp_Object tail;
14128 int i;
14130 for (i = 0; i < dpyinfo->n_fonts; i++)
14131 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14132 if (dpyinfo->font_table[i].name
14133 && (!strcmp (dpyinfo->font_table[i].name,
14134 XSTRING (XCAR (tail))->data)
14135 || !strcmp (dpyinfo->font_table[i].full_name,
14136 XSTRING (XCAR (tail))->data)))
14137 return (dpyinfo->font_table + i);
14140 /* Load the font and add it to the table. */
14142 char *full_name;
14143 XFontStruct *font;
14144 struct font_info *fontp;
14145 unsigned long value;
14146 int i;
14148 /* If we have found fonts by x_list_font, load one of them. If
14149 not, we still try to load a font by the name given as FONTNAME
14150 because XListFonts (called in x_list_font) of some X server has
14151 a bug of not finding a font even if the font surely exists and
14152 is loadable by XLoadQueryFont. */
14153 if (size > 0 && !NILP (font_names))
14154 fontname = (char *) XSTRING (XCAR (font_names))->data;
14156 BLOCK_INPUT;
14157 count = x_catch_errors (FRAME_X_DISPLAY (f));
14158 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14159 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14161 /* This error is perhaps due to insufficient memory on X
14162 server. Let's just ignore it. */
14163 font = NULL;
14164 x_clear_errors (FRAME_X_DISPLAY (f));
14166 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14167 UNBLOCK_INPUT;
14168 if (!font)
14169 return NULL;
14171 /* Find a free slot in the font table. */
14172 for (i = 0; i < dpyinfo->n_fonts; ++i)
14173 if (dpyinfo->font_table[i].name == NULL)
14174 break;
14176 /* If no free slot found, maybe enlarge the font table. */
14177 if (i == dpyinfo->n_fonts
14178 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14180 int sz;
14181 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14182 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14183 dpyinfo->font_table
14184 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14187 fontp = dpyinfo->font_table + i;
14188 if (i == dpyinfo->n_fonts)
14189 ++dpyinfo->n_fonts;
14191 /* Now fill in the slots of *FONTP. */
14192 BLOCK_INPUT;
14193 fontp->font = font;
14194 fontp->font_idx = i;
14195 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14196 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14198 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14199 full_name = 0;
14200 if (XGetFontProperty (font, XA_FONT, &value))
14202 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14203 char *p = name;
14204 int dashes = 0;
14206 /* Count the number of dashes in the "full name".
14207 If it is too few, this isn't really the font's full name,
14208 so don't use it.
14209 In X11R4, the fonts did not come with their canonical names
14210 stored in them. */
14211 while (*p)
14213 if (*p == '-')
14214 dashes++;
14215 p++;
14218 if (dashes >= 13)
14220 full_name = (char *) xmalloc (p - name + 1);
14221 bcopy (name, full_name, p - name + 1);
14224 XFree (name);
14227 if (full_name != 0)
14228 fontp->full_name = full_name;
14229 else
14230 fontp->full_name = fontp->name;
14232 fontp->size = font->max_bounds.width;
14233 fontp->height = FONT_HEIGHT (font);
14235 if (NILP (font_names))
14237 /* We come here because of a bug of XListFonts mentioned at
14238 the head of this block. Let's store this information in
14239 the cache for x_list_fonts. */
14240 Lisp_Object lispy_name = build_string (fontname);
14241 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14242 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14243 Qnil);
14245 XSETCDR (dpyinfo->name_list_element,
14246 Fcons (Fcons (key,
14247 Fcons (Fcons (lispy_full_name,
14248 make_number (fontp->size)),
14249 Qnil)),
14250 XCDR (dpyinfo->name_list_element)));
14251 if (full_name)
14253 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14254 Qnil);
14255 XSETCDR (dpyinfo->name_list_element,
14256 Fcons (Fcons (key,
14257 Fcons (Fcons (lispy_full_name,
14258 make_number (fontp->size)),
14259 Qnil)),
14260 XCDR (dpyinfo->name_list_element)));
14264 /* The slot `encoding' specifies how to map a character
14265 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14266 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14267 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14268 2:0xA020..0xFF7F). For the moment, we don't know which charset
14269 uses this font. So, we set information in fontp->encoding[1]
14270 which is never used by any charset. If mapping can't be
14271 decided, set FONT_ENCODING_NOT_DECIDED. */
14272 fontp->encoding[1]
14273 = (font->max_byte1 == 0
14274 /* 1-byte font */
14275 ? (font->min_char_or_byte2 < 0x80
14276 ? (font->max_char_or_byte2 < 0x80
14277 ? 0 /* 0x20..0x7F */
14278 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14279 : 1) /* 0xA0..0xFF */
14280 /* 2-byte font */
14281 : (font->min_byte1 < 0x80
14282 ? (font->max_byte1 < 0x80
14283 ? (font->min_char_or_byte2 < 0x80
14284 ? (font->max_char_or_byte2 < 0x80
14285 ? 0 /* 0x2020..0x7F7F */
14286 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14287 : 3) /* 0x20A0..0x7FFF */
14288 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14289 : (font->min_char_or_byte2 < 0x80
14290 ? (font->max_char_or_byte2 < 0x80
14291 ? 2 /* 0xA020..0xFF7F */
14292 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14293 : 1))); /* 0xA0A0..0xFFFF */
14295 fontp->baseline_offset
14296 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14297 ? (long) value : 0);
14298 fontp->relative_compose
14299 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14300 ? (long) value : 0);
14301 fontp->default_ascent
14302 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14303 ? (long) value : 0);
14305 /* Set global flag fonts_changed_p to non-zero if the font loaded
14306 has a character with a smaller width than any other character
14307 before, or if the font loaded has a smaller height than any
14308 other font loaded before. If this happens, it will make a
14309 glyph matrix reallocation necessary. */
14310 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14311 UNBLOCK_INPUT;
14312 return fontp;
14317 /* Return a pointer to struct font_info of a font named FONTNAME for
14318 frame F. If no such font is loaded, return NULL. */
14320 struct font_info *
14321 x_query_font (f, fontname)
14322 struct frame *f;
14323 register char *fontname;
14325 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14326 int i;
14328 for (i = 0; i < dpyinfo->n_fonts; i++)
14329 if (dpyinfo->font_table[i].name
14330 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14331 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14332 return (dpyinfo->font_table + i);
14333 return NULL;
14337 /* Find a CCL program for a font specified by FONTP, and set the member
14338 `encoder' of the structure. */
14340 void
14341 x_find_ccl_program (fontp)
14342 struct font_info *fontp;
14344 Lisp_Object list, elt;
14346 elt = Qnil;
14347 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14349 elt = XCAR (list);
14350 if (CONSP (elt)
14351 && STRINGP (XCAR (elt))
14352 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14353 >= 0)
14354 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14355 >= 0)))
14356 break;
14359 if (! NILP (list))
14361 struct ccl_program *ccl
14362 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14364 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14365 xfree (ccl);
14366 else
14367 fontp->font_encoder = ccl;
14373 /***********************************************************************
14374 Initialization
14375 ***********************************************************************/
14377 #ifdef USE_X_TOOLKIT
14378 static XrmOptionDescRec emacs_options[] = {
14379 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14380 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14382 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14383 XrmoptionSepArg, NULL},
14384 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14386 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14387 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14388 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14389 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14390 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14391 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14392 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14394 #endif /* USE_X_TOOLKIT */
14396 static int x_initialized;
14398 #ifdef MULTI_KBOARD
14399 /* Test whether two display-name strings agree up to the dot that separates
14400 the screen number from the server number. */
14401 static int
14402 same_x_server (name1, name2)
14403 char *name1, *name2;
14405 int seen_colon = 0;
14406 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14407 int system_name_length = strlen (system_name);
14408 int length_until_period = 0;
14410 while (system_name[length_until_period] != 0
14411 && system_name[length_until_period] != '.')
14412 length_until_period++;
14414 /* Treat `unix' like an empty host name. */
14415 if (! strncmp (name1, "unix:", 5))
14416 name1 += 4;
14417 if (! strncmp (name2, "unix:", 5))
14418 name2 += 4;
14419 /* Treat this host's name like an empty host name. */
14420 if (! strncmp (name1, system_name, system_name_length)
14421 && name1[system_name_length] == ':')
14422 name1 += system_name_length;
14423 if (! strncmp (name2, system_name, system_name_length)
14424 && name2[system_name_length] == ':')
14425 name2 += system_name_length;
14426 /* Treat this host's domainless name like an empty host name. */
14427 if (! strncmp (name1, system_name, length_until_period)
14428 && name1[length_until_period] == ':')
14429 name1 += length_until_period;
14430 if (! strncmp (name2, system_name, length_until_period)
14431 && name2[length_until_period] == ':')
14432 name2 += length_until_period;
14434 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14436 if (*name1 == ':')
14437 seen_colon++;
14438 if (seen_colon && *name1 == '.')
14439 return 1;
14441 return (seen_colon
14442 && (*name1 == '.' || *name1 == '\0')
14443 && (*name2 == '.' || *name2 == '\0'));
14445 #endif
14447 struct x_display_info *
14448 x_term_init (display_name, xrm_option, resource_name)
14449 Lisp_Object display_name;
14450 char *xrm_option;
14451 char *resource_name;
14453 int connection;
14454 Display *dpy;
14455 struct x_display_info *dpyinfo;
14456 XrmDatabase xrdb;
14458 BLOCK_INPUT;
14460 if (!x_initialized)
14462 x_initialize ();
14463 x_initialized = 1;
14466 #ifdef USE_X_TOOLKIT
14467 /* weiner@footloose.sps.mot.com reports that this causes
14468 errors with X11R5:
14469 X protocol error: BadAtom (invalid Atom parameter)
14470 on protocol request 18skiloaf.
14471 So let's not use it until R6. */
14472 #ifdef HAVE_X11XTR6
14473 XtSetLanguageProc (NULL, NULL, NULL);
14474 #endif
14477 int argc = 0;
14478 char *argv[3];
14480 argv[0] = "";
14481 argc = 1;
14482 if (xrm_option)
14484 argv[argc++] = "-xrm";
14485 argv[argc++] = xrm_option;
14487 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14488 resource_name, EMACS_CLASS,
14489 emacs_options, XtNumber (emacs_options),
14490 &argc, argv);
14492 #ifdef HAVE_X11XTR6
14493 /* I think this is to compensate for XtSetLanguageProc. */
14494 fixup_locale ();
14495 #endif
14498 #else /* not USE_X_TOOLKIT */
14499 #ifdef HAVE_X11R5
14500 XSetLocaleModifiers ("");
14501 #endif
14502 dpy = XOpenDisplay (XSTRING (display_name)->data);
14503 #endif /* not USE_X_TOOLKIT */
14505 /* Detect failure. */
14506 if (dpy == 0)
14508 UNBLOCK_INPUT;
14509 return 0;
14512 /* We have definitely succeeded. Record the new connection. */
14514 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14515 bzero (dpyinfo, sizeof *dpyinfo);
14517 #ifdef MULTI_KBOARD
14519 struct x_display_info *share;
14520 Lisp_Object tail;
14522 for (share = x_display_list, tail = x_display_name_list; share;
14523 share = share->next, tail = XCDR (tail))
14524 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14525 XSTRING (display_name)->data))
14526 break;
14527 if (share)
14528 dpyinfo->kboard = share->kboard;
14529 else
14531 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14532 init_kboard (dpyinfo->kboard);
14533 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14535 char *vendor = ServerVendor (dpy);
14536 UNBLOCK_INPUT;
14537 dpyinfo->kboard->Vsystem_key_alist
14538 = call1 (Qvendor_specific_keysyms,
14539 build_string (vendor ? vendor : ""));
14540 BLOCK_INPUT;
14543 dpyinfo->kboard->next_kboard = all_kboards;
14544 all_kboards = dpyinfo->kboard;
14545 /* Don't let the initial kboard remain current longer than necessary.
14546 That would cause problems if a file loaded on startup tries to
14547 prompt in the mini-buffer. */
14548 if (current_kboard == initial_kboard)
14549 current_kboard = dpyinfo->kboard;
14551 dpyinfo->kboard->reference_count++;
14553 #endif
14555 /* Put this display on the chain. */
14556 dpyinfo->next = x_display_list;
14557 x_display_list = dpyinfo;
14559 /* Put it on x_display_name_list as well, to keep them parallel. */
14560 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14561 x_display_name_list);
14562 dpyinfo->name_list_element = XCAR (x_display_name_list);
14564 dpyinfo->display = dpy;
14566 #if 0
14567 XSetAfterFunction (x_current_display, x_trace_wire);
14568 #endif /* ! 0 */
14570 dpyinfo->x_id_name
14571 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14572 + STRING_BYTES (XSTRING (Vsystem_name))
14573 + 2);
14574 sprintf (dpyinfo->x_id_name, "%s@%s",
14575 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14577 /* Figure out which modifier bits mean what. */
14578 x_find_modifier_meanings (dpyinfo);
14580 /* Get the scroll bar cursor. */
14581 dpyinfo->vertical_scroll_bar_cursor
14582 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14584 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14585 resource_name, EMACS_CLASS);
14586 #ifdef HAVE_XRMSETDATABASE
14587 XrmSetDatabase (dpyinfo->display, xrdb);
14588 #else
14589 dpyinfo->display->db = xrdb;
14590 #endif
14591 /* Put the rdb where we can find it in a way that works on
14592 all versions. */
14593 dpyinfo->xrdb = xrdb;
14595 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14596 DefaultScreen (dpyinfo->display));
14597 select_visual (dpyinfo);
14598 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14599 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14600 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14601 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14602 dpyinfo->grabbed = 0;
14603 dpyinfo->reference_count = 0;
14604 dpyinfo->icon_bitmap_id = -1;
14605 dpyinfo->font_table = NULL;
14606 dpyinfo->n_fonts = 0;
14607 dpyinfo->font_table_size = 0;
14608 dpyinfo->bitmaps = 0;
14609 dpyinfo->bitmaps_size = 0;
14610 dpyinfo->bitmaps_last = 0;
14611 dpyinfo->scratch_cursor_gc = 0;
14612 dpyinfo->mouse_face_mouse_frame = 0;
14613 dpyinfo->mouse_face_deferred_gc = 0;
14614 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14615 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14616 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14617 dpyinfo->mouse_face_window = Qnil;
14618 dpyinfo->mouse_face_overlay = Qnil;
14619 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14620 dpyinfo->mouse_face_defer = 0;
14621 dpyinfo->x_focus_frame = 0;
14622 dpyinfo->x_focus_event_frame = 0;
14623 dpyinfo->x_highlight_frame = 0;
14624 dpyinfo->image_cache = make_image_cache ();
14626 /* See if a private colormap is requested. */
14627 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14629 if (dpyinfo->visual->class == PseudoColor)
14631 Lisp_Object value;
14632 value = display_x_get_resource (dpyinfo,
14633 build_string ("privateColormap"),
14634 build_string ("PrivateColormap"),
14635 Qnil, Qnil);
14636 if (STRINGP (value)
14637 && (!strcmp (XSTRING (value)->data, "true")
14638 || !strcmp (XSTRING (value)->data, "on")))
14639 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14642 else
14643 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14644 dpyinfo->visual, AllocNone);
14647 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14648 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14649 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14650 dpyinfo->resy = pixels * 25.4 / mm;
14651 pixels = DisplayWidth (dpyinfo->display, screen_number);
14652 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14653 dpyinfo->resx = pixels * 25.4 / mm;
14656 dpyinfo->Xatom_wm_protocols
14657 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14658 dpyinfo->Xatom_wm_take_focus
14659 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14660 dpyinfo->Xatom_wm_save_yourself
14661 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14662 dpyinfo->Xatom_wm_delete_window
14663 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14664 dpyinfo->Xatom_wm_change_state
14665 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14666 dpyinfo->Xatom_wm_configure_denied
14667 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14668 dpyinfo->Xatom_wm_window_moved
14669 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14670 dpyinfo->Xatom_editres
14671 = XInternAtom (dpyinfo->display, "Editres", False);
14672 dpyinfo->Xatom_CLIPBOARD
14673 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14674 dpyinfo->Xatom_TIMESTAMP
14675 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14676 dpyinfo->Xatom_TEXT
14677 = XInternAtom (dpyinfo->display, "TEXT", False);
14678 dpyinfo->Xatom_COMPOUND_TEXT
14679 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14680 dpyinfo->Xatom_DELETE
14681 = XInternAtom (dpyinfo->display, "DELETE", False);
14682 dpyinfo->Xatom_MULTIPLE
14683 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14684 dpyinfo->Xatom_INCR
14685 = XInternAtom (dpyinfo->display, "INCR", False);
14686 dpyinfo->Xatom_EMACS_TMP
14687 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14688 dpyinfo->Xatom_TARGETS
14689 = XInternAtom (dpyinfo->display, "TARGETS", False);
14690 dpyinfo->Xatom_NULL
14691 = XInternAtom (dpyinfo->display, "NULL", False);
14692 dpyinfo->Xatom_ATOM_PAIR
14693 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14694 /* For properties of font. */
14695 dpyinfo->Xatom_PIXEL_SIZE
14696 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14697 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14698 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14699 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14700 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14701 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14702 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14704 /* Ghostscript support. */
14705 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14706 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14708 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14709 False);
14711 dpyinfo->cut_buffers_initialized = 0;
14713 connection = ConnectionNumber (dpyinfo->display);
14714 dpyinfo->connection = connection;
14717 char null_bits[1];
14719 null_bits[0] = 0x00;
14721 dpyinfo->null_pixel
14722 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14723 null_bits, 1, 1, (long) 0, (long) 0,
14728 extern int gray_bitmap_width, gray_bitmap_height;
14729 extern char *gray_bitmap_bits;
14730 dpyinfo->gray
14731 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14732 gray_bitmap_bits,
14733 gray_bitmap_width, gray_bitmap_height,
14734 (unsigned long) 1, (unsigned long) 0, 1);
14737 #ifdef HAVE_X_I18N
14738 xim_initialize (dpyinfo, resource_name);
14739 #endif
14741 #ifdef subprocesses
14742 /* This is only needed for distinguishing keyboard and process input. */
14743 if (connection != 0)
14744 add_keyboard_wait_descriptor (connection);
14745 #endif
14747 #ifndef F_SETOWN_BUG
14748 #ifdef F_SETOWN
14749 #ifdef F_SETOWN_SOCK_NEG
14750 /* stdin is a socket here */
14751 fcntl (connection, F_SETOWN, -getpid ());
14752 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14753 fcntl (connection, F_SETOWN, getpid ());
14754 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14755 #endif /* ! defined (F_SETOWN) */
14756 #endif /* F_SETOWN_BUG */
14758 #ifdef SIGIO
14759 if (interrupt_input)
14760 init_sigio (connection);
14761 #endif /* ! defined (SIGIO) */
14763 #ifdef USE_LUCID
14764 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14765 /* Make sure that we have a valid font for dialog boxes
14766 so that Xt does not crash. */
14768 Display *dpy = dpyinfo->display;
14769 XrmValue d, fr, to;
14770 Font font;
14771 int count;
14773 d.addr = (XPointer)&dpy;
14774 d.size = sizeof (Display *);
14775 fr.addr = XtDefaultFont;
14776 fr.size = sizeof (XtDefaultFont);
14777 to.size = sizeof (Font *);
14778 to.addr = (XPointer)&font;
14779 count = x_catch_errors (dpy);
14780 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14781 abort ();
14782 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14783 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14784 x_uncatch_errors (dpy, count);
14786 #endif
14787 #endif
14789 /* See if we should run in synchronous mode. This is useful
14790 for debugging X code. */
14792 Lisp_Object value;
14793 value = display_x_get_resource (dpyinfo,
14794 build_string ("synchronous"),
14795 build_string ("Synchronous"),
14796 Qnil, Qnil);
14797 if (STRINGP (value)
14798 && (!strcmp (XSTRING (value)->data, "true")
14799 || !strcmp (XSTRING (value)->data, "on")))
14800 XSynchronize (dpyinfo->display, True);
14803 UNBLOCK_INPUT;
14805 return dpyinfo;
14808 /* Get rid of display DPYINFO, assuming all frames are already gone,
14809 and without sending any more commands to the X server. */
14811 void
14812 x_delete_display (dpyinfo)
14813 struct x_display_info *dpyinfo;
14815 delete_keyboard_wait_descriptor (dpyinfo->connection);
14817 /* Discard this display from x_display_name_list and x_display_list.
14818 We can't use Fdelq because that can quit. */
14819 if (! NILP (x_display_name_list)
14820 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14821 x_display_name_list = XCDR (x_display_name_list);
14822 else
14824 Lisp_Object tail;
14826 tail = x_display_name_list;
14827 while (CONSP (tail) && CONSP (XCDR (tail)))
14829 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14831 XSETCDR (tail, XCDR (XCDR (tail)));
14832 break;
14834 tail = XCDR (tail);
14838 if (next_noop_dpyinfo == dpyinfo)
14839 next_noop_dpyinfo = dpyinfo->next;
14841 if (x_display_list == dpyinfo)
14842 x_display_list = dpyinfo->next;
14843 else
14845 struct x_display_info *tail;
14847 for (tail = x_display_list; tail; tail = tail->next)
14848 if (tail->next == dpyinfo)
14849 tail->next = tail->next->next;
14852 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14853 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14854 XrmDestroyDatabase (dpyinfo->xrdb);
14855 #endif
14856 #endif
14857 #ifdef MULTI_KBOARD
14858 if (--dpyinfo->kboard->reference_count == 0)
14859 delete_kboard (dpyinfo->kboard);
14860 #endif
14861 #ifdef HAVE_X_I18N
14862 if (dpyinfo->xim)
14863 xim_close_dpy (dpyinfo);
14864 #endif
14866 xfree (dpyinfo->font_table);
14867 xfree (dpyinfo->x_id_name);
14868 xfree (dpyinfo->color_cells);
14869 xfree (dpyinfo);
14873 /* Set up use of X before we make the first connection. */
14875 static struct redisplay_interface x_redisplay_interface =
14877 x_produce_glyphs,
14878 x_write_glyphs,
14879 x_insert_glyphs,
14880 x_clear_end_of_line,
14881 x_scroll_run,
14882 x_after_update_window_line,
14883 x_update_window_begin,
14884 x_update_window_end,
14885 XTcursor_to,
14886 x_flush,
14887 x_clear_mouse_face,
14888 x_get_glyph_overhangs,
14889 x_fix_overlapping_area
14892 void
14893 x_initialize ()
14895 rif = &x_redisplay_interface;
14897 clear_frame_hook = x_clear_frame;
14898 ins_del_lines_hook = x_ins_del_lines;
14899 delete_glyphs_hook = x_delete_glyphs;
14900 ring_bell_hook = XTring_bell;
14901 reset_terminal_modes_hook = XTreset_terminal_modes;
14902 set_terminal_modes_hook = XTset_terminal_modes;
14903 update_begin_hook = x_update_begin;
14904 update_end_hook = x_update_end;
14905 set_terminal_window_hook = XTset_terminal_window;
14906 read_socket_hook = XTread_socket;
14907 frame_up_to_date_hook = XTframe_up_to_date;
14908 mouse_position_hook = XTmouse_position;
14909 frame_rehighlight_hook = XTframe_rehighlight;
14910 frame_raise_lower_hook = XTframe_raise_lower;
14911 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
14912 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
14913 redeem_scroll_bar_hook = XTredeem_scroll_bar;
14914 judge_scroll_bars_hook = XTjudge_scroll_bars;
14915 estimate_mode_line_height_hook = x_estimate_mode_line_height;
14917 scroll_region_ok = 1; /* we'll scroll partial frames */
14918 char_ins_del_ok = 1;
14919 line_ins_del_ok = 1; /* we'll just blt 'em */
14920 fast_clear_end_of_line = 1; /* X does this well */
14921 memory_below_frame = 0; /* we don't remember what scrolls
14922 off the bottom */
14923 baud_rate = 19200;
14925 x_noop_count = 0;
14926 last_tool_bar_item = -1;
14927 any_help_event_p = 0;
14929 /* Try to use interrupt input; if we can't, then start polling. */
14930 Fset_input_mode (Qt, Qnil, Qt, Qnil);
14932 #ifdef USE_X_TOOLKIT
14933 XtToolkitInitialize ();
14935 Xt_app_con = XtCreateApplicationContext ();
14937 /* Register a converter from strings to pixels, which uses
14938 Emacs' color allocation infrastructure. */
14939 XtAppSetTypeConverter (Xt_app_con,
14940 XtRString, XtRPixel, cvt_string_to_pixel,
14941 cvt_string_to_pixel_args,
14942 XtNumber (cvt_string_to_pixel_args),
14943 XtCacheByDisplay, cvt_pixel_dtor);
14945 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
14947 /* Install an asynchronous timer that processes Xt timeout events
14948 every 0.1s. This is necessary because some widget sets use
14949 timeouts internally, for example the LessTif menu bar, or the
14950 Xaw3d scroll bar. When Xt timouts aren't processed, these
14951 widgets don't behave normally. */
14953 EMACS_TIME interval;
14954 EMACS_SET_SECS_USECS (interval, 0, 100000);
14955 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
14957 #endif
14959 #ifdef USE_TOOLKIT_SCROLL_BARS
14960 xaw3d_arrow_scroll = False;
14961 xaw3d_pick_top = True;
14962 #endif
14964 /* Note that there is no real way portable across R3/R4 to get the
14965 original error handler. */
14966 XSetErrorHandler (x_error_handler);
14967 XSetIOErrorHandler (x_io_error_quitter);
14969 /* Disable Window Change signals; they are handled by X events. */
14970 #ifdef SIGWINCH
14971 signal (SIGWINCH, SIG_DFL);
14972 #endif /* ! defined (SIGWINCH) */
14974 signal (SIGPIPE, x_connection_signal);
14978 void
14979 syms_of_xterm ()
14981 staticpro (&x_error_message_string);
14982 x_error_message_string = Qnil;
14984 staticpro (&x_display_name_list);
14985 x_display_name_list = Qnil;
14987 staticpro (&last_mouse_scroll_bar);
14988 last_mouse_scroll_bar = Qnil;
14990 staticpro (&Qvendor_specific_keysyms);
14991 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14993 staticpro (&last_mouse_press_frame);
14994 last_mouse_press_frame = Qnil;
14996 help_echo = Qnil;
14997 staticpro (&help_echo);
14998 help_echo_object = Qnil;
14999 staticpro (&help_echo_object);
15000 help_echo_window = Qnil;
15001 staticpro (&help_echo_window);
15002 previous_help_echo = Qnil;
15003 staticpro (&previous_help_echo);
15004 help_echo_pos = -1;
15006 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15007 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15008 For example, if a block cursor is over a tab, it will be drawn as
15009 wide as that tab on the display. */);
15010 x_stretch_cursor_p = 0;
15012 DEFVAR_BOOL ("x-use-underline-position-properties",
15013 &x_use_underline_position_properties,
15014 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15015 nil means ignore them. If you encounter fonts with bogus
15016 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15017 to 4.1, set this to nil. */);
15018 x_use_underline_position_properties = 1;
15020 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15021 doc: /* What X toolkit scroll bars Emacs uses.
15022 A value of nil means Emacs doesn't use X toolkit scroll bars.
15023 Otherwise, value is a symbol describing the X toolkit. */);
15024 #ifdef USE_TOOLKIT_SCROLL_BARS
15025 #ifdef USE_MOTIF
15026 Vx_toolkit_scroll_bars = intern ("motif");
15027 #elif defined HAVE_XAW3D
15028 Vx_toolkit_scroll_bars = intern ("xaw3d");
15029 #else
15030 Vx_toolkit_scroll_bars = intern ("xaw");
15031 #endif
15032 #else
15033 Vx_toolkit_scroll_bars = Qnil;
15034 #endif
15036 staticpro (&last_mouse_motion_frame);
15037 last_mouse_motion_frame = Qnil;
15040 #endif /* HAVE_X_WINDOWS */