(combine-run-hooks): New function.
[emacs.git] / src / xterm.c
blob9516de7e03300eccb331d4ecb212bfb7eb78f34f
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999
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 #ifndef USG
48 /* Load sys/types.h if not already loaded.
49 In some systems loading it twice is suicidal. */
50 #ifndef makedev
51 #include <sys/types.h>
52 #endif /* makedev */
53 #endif /* USG */
55 #ifdef BSD_SYSTEM
56 #include <sys/ioctl.h>
57 #endif /* ! defined (BSD_SYSTEM) */
59 #include "systty.h"
60 #include "systime.h"
62 #ifndef INCLUDED_FCNTL
63 #include <fcntl.h>
64 #endif
65 #include <ctype.h>
66 #include <errno.h>
67 #include <setjmp.h>
68 #include <sys/stat.h>
69 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
70 /* #include <sys/param.h> */
72 #include "charset.h"
73 #include "ccl.h"
74 #include "frame.h"
75 #include "dispextern.h"
76 #include "fontset.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #if 0
81 #include "sink.h"
82 #include "sinkmask.h"
83 #endif /* ! 0 */
84 #include "gnu.h"
85 #include "disptab.h"
86 #include "buffer.h"
87 #include "window.h"
88 #include "keyboard.h"
89 #include "intervals.h"
90 #include "process.h"
91 #include "atimer.h"
93 #ifdef USE_X_TOOLKIT
94 #include <X11/Shell.h>
95 #endif
97 #include <sys/types.h>
98 #ifdef HAVE_SYS_TIME_H
99 #include <sys/time.h>
100 #endif
101 #ifdef HAVE_UNISTD_H
102 #include <unistd.h>
103 #endif
105 #ifdef USE_X_TOOLKIT
107 extern void free_frame_menubar ();
108 extern FRAME_PTR x_menubar_window_to_frame ();
110 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
111 #define HACK_EDITRES
112 extern void _XEditResCheckMessages ();
113 #endif /* not NO_EDITRES */
115 /* Include toolkit specific headers for the scroll bar widget. */
117 #ifdef USE_TOOLKIT_SCROLL_BARS
118 #if defined USE_MOTIF
119 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
120 #include <Xm/ScrollBar.h>
121 #include <Xm/ScrollBarP.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 #ifdef SOLARIS2
154 /* memmove will be defined as a macro in Xfuncs.h unless
155 <string.h> is included beforehand. The declaration for memmove in
156 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
157 #include <string.h>
158 #endif
160 #ifndef min
161 #define min(a,b) ((a) < (b) ? (a) : (b))
162 #endif
163 #ifndef max
164 #define max(a,b) ((a) > (b) ? (a) : (b))
165 #endif
167 #define abs(x) ((x) < 0 ? -(x) : (x))
169 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
172 /* Bitmaps for truncated lines. */
174 enum bitmap_type
176 NO_BITMAP,
177 LEFT_TRUNCATION_BITMAP,
178 RIGHT_TRUNCATION_BITMAP,
179 OVERLAY_ARROW_BITMAP,
180 CONTINUED_LINE_BITMAP,
181 CONTINUATION_LINE_BITMAP,
182 ZV_LINE_BITMAP
185 /* Bitmap drawn to indicate lines not displaying text if
186 `indicate-empty-lines' is non-nil. */
188 #define zv_width 8
189 #define zv_height 8
190 static unsigned char zv_bits[] = {
191 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
193 /* An arrow like this: `<-'. */
195 #define left_width 8
196 #define left_height 8
197 static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
200 /* Right truncation arrow bitmap `->'. */
202 #define right_width 8
203 #define right_height 8
204 static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
207 /* Marker for continued lines. */
209 #define continued_width 8
210 #define continued_height 8
211 static unsigned char continued_bits[] = {
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
214 /* Marker for continuation lines. */
216 #define continuation_width 8
217 #define continuation_height 8
218 static unsigned char continuation_bits[] = {
219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
221 /* Overlay arrow bitmap. */
223 #if 0
224 /* A bomb. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
229 #else
230 /* A triangular arrow. */
231 #define ov_width 8
232 #define ov_height 8
233 static unsigned char ov_bits[] = {
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
236 #endif
238 extern Lisp_Object Qhelp_echo;
241 /* Non-zero means Emacs uses toolkit scroll bars. */
243 int x_toolkit_scroll_bars_p;
245 /* If a string, XTread_socket generates an event to display that string.
246 (The display is done in read_char.) */
248 static Lisp_Object help_echo;
250 /* Temporary variable for XTread_socket. */
252 static Lisp_Object previous_help_echo;
254 /* Non-zero means that a HELP_EVENT has been generated since Emacs
255 start. */
257 static int any_help_event_p;
259 /* Non-zero means draw block and hollow cursor as wide as the glyph
260 under it. For example, if a block cursor is over a tab, it will be
261 drawn as wide as that tab on the display. */
263 int x_stretch_cursor_p;
265 /* This is a chain of structures for all the X displays currently in
266 use. */
268 struct x_display_info *x_display_list;
270 /* This is a list of cons cells, each of the form (NAME
271 . FONT-LIST-CACHE), one for each element of x_display_list and in
272 the same order. NAME is the name of the frame. FONT-LIST-CACHE
273 records previous values returned by x-list-fonts. */
275 Lisp_Object x_display_name_list;
277 /* Frame being updated by update_frame. This is declared in term.c.
278 This is set by update_begin and looked at by all the XT functions.
279 It is zero while not inside an update. In that case, the XT
280 functions assume that `selected_frame' is the frame to apply to. */
282 extern struct frame *updating_frame;
284 extern int waiting_for_input;
286 /* This is a frame waiting to be auto-raised, within XTread_socket. */
288 struct frame *pending_autoraise_frame;
290 #ifdef USE_X_TOOLKIT
291 /* The application context for Xt use. */
292 XtAppContext Xt_app_con;
293 static String Xt_default_resources[] = {0};
294 #endif /* USE_X_TOOLKIT */
296 /* Nominal cursor position -- where to draw output.
297 HPOS and VPOS are window relative glyph matrix coordinates.
298 X and Y are window relative pixel coordinates. */
300 struct cursor_pos output_cursor;
302 /* Non-zero means user is interacting with a toolkit scroll bar. */
304 static int toolkit_scroll_bar_interaction;
306 /* Mouse movement.
308 Formerly, we used PointerMotionHintMask (in standard_event_mask)
309 so that we would have to call XQueryPointer after each MotionNotify
310 event to ask for another such event. However, this made mouse tracking
311 slow, and there was a bug that made it eventually stop.
313 Simply asking for MotionNotify all the time seems to work better.
315 In order to avoid asking for motion events and then throwing most
316 of them away or busy-polling the server for mouse positions, we ask
317 the server for pointer motion hints. This means that we get only
318 one event per group of mouse movements. "Groups" are delimited by
319 other kinds of events (focus changes and button clicks, for
320 example), or by XQueryPointer calls; when one of these happens, we
321 get another MotionNotify event the next time the mouse moves. This
322 is at least as efficient as getting motion events when mouse
323 tracking is on, and I suspect only negligibly worse when tracking
324 is off. */
326 /* Where the mouse was last time we reported a mouse event. */
328 FRAME_PTR last_mouse_frame;
329 static XRectangle last_mouse_glyph;
330 static Lisp_Object last_mouse_press_frame;
332 /* The scroll bar in which the last X motion event occurred.
334 If the last X motion event occurred in a scroll bar, we set this so
335 XTmouse_position can know whether to report a scroll bar motion or
336 an ordinary motion.
338 If the last X motion event didn't occur in a scroll bar, we set
339 this to Qnil, to tell XTmouse_position to return an ordinary motion
340 event. */
342 static Lisp_Object last_mouse_scroll_bar;
344 /* This is a hack. We would really prefer that XTmouse_position would
345 return the time associated with the position it returns, but there
346 doesn't seem to be any way to wrest the time-stamp from the server
347 along with the position query. So, we just keep track of the time
348 of the last movement we received, and return that in hopes that
349 it's somewhat accurate. */
351 static Time last_mouse_movement_time;
353 /* Incremented by XTread_socket whenever it really tries to read
354 events. */
356 #ifdef __STDC__
357 static int volatile input_signal_count;
358 #else
359 static int input_signal_count;
360 #endif
362 /* Used locally within XTread_socket. */
364 static int x_noop_count;
366 /* Initial values of argv and argc. */
368 extern char **initial_argv;
369 extern int initial_argc;
371 extern Lisp_Object Vcommand_line_args, Vsystem_name;
373 /* Tells if a window manager is present or not. */
375 extern Lisp_Object Vx_no_window_manager;
377 extern Lisp_Object Qface, Qmouse_face;
379 extern int errno;
381 /* A mask of extra modifier bits to put into every keyboard char. */
383 extern int extra_keyboard_modifiers;
385 static Lisp_Object Qvendor_specific_keysyms;
387 extern XrmDatabase x_load_resources ();
388 extern Lisp_Object x_icon_type ();
391 /* Enumeration for overriding/changing the face to use for drawing
392 glyphs in x_draw_glyphs. */
394 enum draw_glyphs_face
396 DRAW_NORMAL_TEXT,
397 DRAW_INVERSE_VIDEO,
398 DRAW_CURSOR,
399 DRAW_MOUSE_FACE,
400 DRAW_IMAGE_RAISED,
401 DRAW_IMAGE_SUNKEN
404 static void x_update_window_end P_ ((struct window *, int));
405 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
406 void x_delete_display P_ ((struct x_display_info *));
407 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
408 unsigned));
409 static int fast_find_position P_ ((struct window *, int, int *, int *,
410 int *, int *));
411 static void set_output_cursor P_ ((struct cursor_pos *));
412 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
413 int *, int *, int *));
414 static void note_mode_line_highlight P_ ((struct window *, int, int));
415 static void x_check_font P_ ((struct frame *, XFontStruct *));
416 static void note_mouse_highlight P_ ((struct frame *, int, int));
417 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
418 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
419 static void show_mouse_face P_ ((struct x_display_info *,
420 enum draw_glyphs_face));
421 static int x_io_error_quitter P_ ((Display *));
422 int x_catch_errors P_ ((Display *));
423 void x_uncatch_errors P_ ((Display *, int));
424 void x_lower_frame P_ ((struct frame *));
425 void x_scroll_bar_clear P_ ((struct frame *));
426 int x_had_errors_p P_ ((Display *));
427 void x_wm_set_size_hint P_ ((struct frame *, long, int));
428 void x_raise_frame P_ ((struct frame *));
429 void x_set_window_size P_ ((struct frame *, int, int, int));
430 void x_wm_set_window_state P_ ((struct frame *, int));
431 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
432 void x_initialize P_ ((void));
433 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
434 static int x_compute_min_glyph_bounds P_ ((struct frame *));
435 static void x_draw_phys_cursor_glyph P_ ((struct window *,
436 struct glyph_row *,
437 enum draw_glyphs_face));
438 static void x_update_end P_ ((struct frame *));
439 static void XTframe_up_to_date P_ ((struct frame *));
440 static void XTreassert_line_highlight P_ ((int, int));
441 static void x_change_line_highlight P_ ((int, int, int, int));
442 static void XTset_terminal_modes P_ ((void));
443 static void XTreset_terminal_modes P_ ((void));
444 static void XTcursor_to P_ ((int, int, int, int));
445 static void x_write_glyphs P_ ((struct glyph *, int));
446 static void x_clear_end_of_line P_ ((int));
447 static void x_clear_frame P_ ((void));
448 static void x_clear_cursor P_ ((struct window *));
449 static void frame_highlight P_ ((struct frame *));
450 static void frame_unhighlight P_ ((struct frame *));
451 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
452 static void XTframe_rehighlight P_ ((struct frame *));
453 static void x_frame_rehighlight P_ ((struct x_display_info *));
454 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
455 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *));
456 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
457 XRectangle *));
458 static void expose_frame P_ ((struct frame *, int, int, int, int));
459 static void expose_window_tree P_ ((struct window *, XRectangle *));
460 static void expose_window P_ ((struct window *, XRectangle *));
461 static void expose_area P_ ((struct window *, struct glyph_row *,
462 XRectangle *, enum glyph_row_area));
463 static void expose_line P_ ((struct window *, struct glyph_row *,
464 XRectangle *));
465 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
466 static void x_update_window_cursor P_ ((struct window *, int));
467 static void x_erase_phys_cursor P_ ((struct window *));
468 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
469 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
470 enum bitmap_type));
472 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
473 GC, int));
474 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
475 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
476 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
477 static void x_flush P_ ((struct frame *f));
480 /* Flush display of frame F, or of all frames if F is null. */
482 static void
483 x_flush (f)
484 struct frame *f;
486 BLOCK_INPUT;
487 if (f == NULL)
489 Lisp_Object rest, frame;
490 FOR_EACH_FRAME (rest, frame)
491 x_flush (XFRAME (frame));
493 else if (FRAME_X_P (f))
494 XFlush (FRAME_X_DISPLAY (f));
495 UNBLOCK_INPUT;
499 /* Remove calls to XFlush by defining XFlush to an empty replacement.
500 Calls to XFlush should be unnecessary because the X output buffer
501 is flushed automatically as needed by calls to XPending,
502 XNextEvent, or XWindowEvent according to the XFlush man page.
503 XTread_socket calls XPending. Removing XFlush improves
504 performance. */
506 #define XFlush(DISPLAY) (void) 0
509 /***********************************************************************
510 Debugging
511 ***********************************************************************/
513 #if 0
515 /* This is a function useful for recording debugging information about
516 the sequence of occurrences in this file. */
518 struct record
520 char *locus;
521 int type;
524 struct record event_record[100];
526 int event_record_index;
528 record_event (locus, type)
529 char *locus;
530 int type;
532 if (event_record_index == sizeof (event_record) / sizeof (struct record))
533 event_record_index = 0;
535 event_record[event_record_index].locus = locus;
536 event_record[event_record_index].type = type;
537 event_record_index++;
540 #endif /* 0 */
544 /* Return the struct x_display_info corresponding to DPY. */
546 struct x_display_info *
547 x_display_info_for_display (dpy)
548 Display *dpy;
550 struct x_display_info *dpyinfo;
552 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
553 if (dpyinfo->display == dpy)
554 return dpyinfo;
556 return 0;
561 /***********************************************************************
562 Starting and ending an update
563 ***********************************************************************/
565 /* Start an update of frame F. This function is installed as a hook
566 for update_begin, i.e. it is called when update_begin is called.
567 This function is called prior to calls to x_update_window_begin for
568 each window being updated. Currently, there is nothing to do here
569 because all interesting stuff is done on a window basis. */
571 static void
572 x_update_begin (f)
573 struct frame *f;
575 /* Nothing to do. */
579 /* Start update of window W. Set the global variable updated_window
580 to the window being updated and set output_cursor to the cursor
581 position of W. */
583 static void
584 x_update_window_begin (w)
585 struct window *w;
587 struct frame *f = XFRAME (WINDOW_FRAME (w));
588 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
590 updated_window = w;
591 set_output_cursor (&w->cursor);
593 BLOCK_INPUT;
595 if (f == display_info->mouse_face_mouse_frame)
597 /* Don't do highlighting for mouse motion during the update. */
598 display_info->mouse_face_defer = 1;
600 /* If F needs to be redrawn, simply forget about any prior mouse
601 highlighting. */
602 if (FRAME_GARBAGED_P (f))
603 display_info->mouse_face_window = Qnil;
605 /* Can we tell that this update does not affect the window
606 where the mouse highlight is? If so, no need to turn off.
607 Likewise, don't do anything if the frame is garbaged;
608 in that case, the frame's current matrix that we would use
609 is all wrong, and we will redisplay that line anyway. */
610 if (!NILP (display_info->mouse_face_window)
611 && w == XWINDOW (display_info->mouse_face_window))
613 int i;
615 for (i = 0; i < w->desired_matrix->nrows; ++i)
616 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
617 break;
619 if (i < w->desired_matrix->nrows)
620 clear_mouse_face (display_info);
624 UNBLOCK_INPUT;
628 /* Draw a vertical window border to the right of window W if W doesn't
629 have vertical scroll bars. */
631 static void
632 x_draw_vertical_border (w)
633 struct window *w;
635 struct frame *f = XFRAME (WINDOW_FRAME (w));
637 /* Redraw borders between horizontally adjacent windows. Don't
638 do it for frames with vertical scroll bars because either the
639 right scroll bar of a window, or the left scroll bar of its
640 neighbor will suffice as a border. */
641 if (!WINDOW_RIGHTMOST_P (w)
642 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
644 int x0, x1, y0, y1;
646 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
647 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
648 y1 -= 1;
650 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
651 f->output_data.x->normal_gc, x1, y0, x1, y1);
656 /* End update of window W (which is equal to updated_window). Draw
657 vertical borders between horizontally adjacent windows, and display
658 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
659 pseudo-window in case we don't have X toolkit support. Such
660 windows don't have a cursor, so don't display it here. */
662 static void
663 x_update_window_end (w, cursor_on_p)
664 struct window *w;
665 int cursor_on_p;
667 if (!w->pseudo_window_p)
669 BLOCK_INPUT;
670 if (cursor_on_p)
671 x_display_and_set_cursor (w, 1, output_cursor.hpos,
672 output_cursor.vpos,
673 output_cursor.x, output_cursor.y);
674 x_draw_vertical_border (w);
675 UNBLOCK_INPUT;
678 updated_window = NULL;
682 /* End update of frame F. This function is installed as a hook in
683 update_end. */
685 static void
686 x_update_end (f)
687 struct frame *f;
689 /* Mouse highlight may be displayed again. */
690 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
692 BLOCK_INPUT;
693 XFlush (FRAME_X_DISPLAY (f));
694 UNBLOCK_INPUT;
698 /* This function is called from various places in xdisp.c whenever a
699 complete update has been performed. The global variable
700 updated_window is not available here. */
702 static void
703 XTframe_up_to_date (f)
704 struct frame *f;
706 if (FRAME_X_P (f))
708 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
709 if (dpyinfo->mouse_face_deferred_gc
710 || f == dpyinfo->mouse_face_mouse_frame)
712 BLOCK_INPUT;
713 if (dpyinfo->mouse_face_mouse_frame)
714 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
715 dpyinfo->mouse_face_mouse_x,
716 dpyinfo->mouse_face_mouse_y);
717 dpyinfo->mouse_face_deferred_gc = 0;
718 UNBLOCK_INPUT;
724 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
725 arrow bitmaps, or clear the areas where they would be displayed
726 before DESIRED_ROW is made current. The window being updated is
727 found in updated_window. This function It is called from
728 update_window_line only if it is known that there are differences
729 between bitmaps to be drawn between current row and DESIRED_ROW. */
731 static void
732 x_after_update_window_line (desired_row)
733 struct glyph_row *desired_row;
735 struct window *w = updated_window;
737 xassert (w);
739 if (!desired_row->mode_line_p && !w->pseudo_window_p)
741 BLOCK_INPUT;
742 x_draw_row_bitmaps (w, desired_row);
744 /* When a window has disappeared, make sure that no rest of
745 full-width rows stays visible in the internal border. */
746 if (windows_or_buffers_changed)
748 struct frame *f = XFRAME (w->frame);
749 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
750 int height = desired_row->visible_height;
751 int x = (window_box_right (w, -1)
752 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
753 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
755 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
756 x, y, width, height, False);
759 UNBLOCK_INPUT;
764 /* Draw the bitmap WHICH in one of the areas to the left or right of
765 window W. ROW is the glyph row for which to display the bitmap; it
766 determines the vertical position at which the bitmap has to be
767 drawn. */
769 static void
770 x_draw_bitmap (w, row, which)
771 struct window *w;
772 struct glyph_row *row;
773 enum bitmap_type which;
775 struct frame *f = XFRAME (WINDOW_FRAME (w));
776 Display *display = FRAME_X_DISPLAY (f);
777 Window window = FRAME_X_WINDOW (f);
778 int x, y, wd, h, dy;
779 unsigned char *bits;
780 Pixmap pixmap;
781 GC gc = f->output_data.x->normal_gc;
782 struct face *face;
783 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
785 /* Must clip because of partially visible lines. */
786 x_clip_to_row (w, row, gc, 1);
788 switch (which)
790 case LEFT_TRUNCATION_BITMAP:
791 wd = left_width;
792 h = left_height;
793 bits = left_bits;
794 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
795 - wd
796 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
797 break;
799 case OVERLAY_ARROW_BITMAP:
800 wd = left_width;
801 h = left_height;
802 bits = ov_bits;
803 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
804 - wd
805 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
806 break;
808 case RIGHT_TRUNCATION_BITMAP:
809 wd = right_width;
810 h = right_height;
811 bits = right_bits;
812 x = window_box_right (w, -1);
813 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
814 break;
816 case CONTINUED_LINE_BITMAP:
817 wd = right_width;
818 h = right_height;
819 bits = continued_bits;
820 x = window_box_right (w, -1);
821 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
822 break;
824 case CONTINUATION_LINE_BITMAP:
825 wd = continuation_width;
826 h = continuation_height;
827 bits = continuation_bits;
828 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
829 - wd
830 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
831 break;
833 case ZV_LINE_BITMAP:
834 wd = zv_width;
835 h = zv_height;
836 bits = zv_bits;
837 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
838 - wd
839 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
840 break;
842 default:
843 abort ();
846 /* Convert to frame coordinates. Set dy to the offset in the row to
847 start drawing the bitmap. */
848 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
849 dy = (row->height - h) / 2;
851 /* Draw the bitmap. I believe these small pixmaps can be cached
852 by the server. */
853 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
854 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
855 face->foreground,
856 face->background, depth);
857 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
858 XFreePixmap (display, pixmap);
859 XSetClipMask (display, gc, None);
863 /* Draw flags bitmaps for glyph row ROW on window W. Call this
864 function with input blocked. */
866 static void
867 x_draw_row_bitmaps (w, row)
868 struct window *w;
869 struct glyph_row *row;
871 struct frame *f = XFRAME (w->frame);
872 enum bitmap_type bitmap;
873 struct face *face;
874 int header_line_height = -1;
876 xassert (interrupt_input_blocked);
878 /* If row is completely invisible, because of vscrolling, we
879 don't have to draw anything. */
880 if (row->visible_height <= 0)
881 return;
883 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
884 PREPARE_FACE_FOR_DISPLAY (f, face);
886 /* Decide which bitmap to draw at the left side. */
887 if (row->overlay_arrow_p)
888 bitmap = OVERLAY_ARROW_BITMAP;
889 else if (row->truncated_on_left_p)
890 bitmap = LEFT_TRUNCATION_BITMAP;
891 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
892 bitmap = CONTINUATION_LINE_BITMAP;
893 else if (row->indicate_empty_line_p)
894 bitmap = ZV_LINE_BITMAP;
895 else
896 bitmap = NO_BITMAP;
898 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
899 the flags area. */
900 if (bitmap == NO_BITMAP
901 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
902 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
904 /* If W has a vertical border to its left, don't draw over it. */
905 int border = ((XFASTINT (w->left) > 0
906 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
907 ? 1 : 0);
908 int left = window_box_left (w, -1);
910 if (header_line_height < 0)
911 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
913 /* In case the same realized face is used for bitmap areas and
914 for something displayed in the text (e.g. face `region' on
915 mono-displays, the fill style may have been changed to
916 FillSolid in x_draw_glyph_string_background. */
917 if (face->stipple)
918 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
919 else
920 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
922 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
923 face->gc,
924 (left
925 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
926 + border),
927 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
928 row->y)),
929 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
930 row->visible_height);
931 if (!face->stipple)
932 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
935 /* Draw the left bitmap. */
936 if (bitmap != NO_BITMAP)
937 x_draw_bitmap (w, row, bitmap);
939 /* Decide which bitmap to draw at the right side. */
940 if (row->truncated_on_right_p)
941 bitmap = RIGHT_TRUNCATION_BITMAP;
942 else if (row->continued_p)
943 bitmap = CONTINUED_LINE_BITMAP;
944 else
945 bitmap = NO_BITMAP;
947 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
948 the flags area. */
949 if (bitmap == NO_BITMAP
950 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
951 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
953 int right = window_box_right (w, -1);
955 if (header_line_height < 0)
956 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
958 /* In case the same realized face is used for bitmap areas and
959 for something displayed in the text (e.g. face `region' on
960 mono-displays, the fill style may have been changed to
961 FillSolid in x_draw_glyph_string_background. */
962 if (face->stipple)
963 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
964 else
965 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
966 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
967 face->gc,
968 right,
969 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
970 row->y)),
971 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
972 row->visible_height);
973 if (!face->stipple)
974 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
977 /* Draw the right bitmap. */
978 if (bitmap != NO_BITMAP)
979 x_draw_bitmap (w, row, bitmap);
983 /***********************************************************************
984 Line Highlighting
985 ***********************************************************************/
987 /* External interface to control of standout mode. Not used for X
988 frames. Aborts when called. */
990 static void
991 XTreassert_line_highlight (new, vpos)
992 int new, vpos;
994 abort ();
998 /* Call this when about to modify line at position VPOS and change
999 whether it is highlighted. Not used for X frames. Aborts when
1000 called. */
1002 static void
1003 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1004 int new_highlight, vpos, y, first_unused_hpos;
1006 abort ();
1010 /* This is called when starting Emacs and when restarting after
1011 suspend. When starting Emacs, no X window is mapped. And nothing
1012 must be done to Emacs's own window if it is suspended (though that
1013 rarely happens). */
1015 static void
1016 XTset_terminal_modes ()
1020 /* This is called when exiting or suspending Emacs. Exiting will make
1021 the X-windows go away, and suspending requires no action. */
1023 static void
1024 XTreset_terminal_modes ()
1030 /***********************************************************************
1031 Output Cursor
1032 ***********************************************************************/
1034 /* Set the global variable output_cursor to CURSOR. All cursor
1035 positions are relative to updated_window. */
1037 static void
1038 set_output_cursor (cursor)
1039 struct cursor_pos *cursor;
1041 output_cursor.hpos = cursor->hpos;
1042 output_cursor.vpos = cursor->vpos;
1043 output_cursor.x = cursor->x;
1044 output_cursor.y = cursor->y;
1048 /* Set a nominal cursor position.
1050 HPOS and VPOS are column/row positions in a window glyph matrix. X
1051 and Y are window text area relative pixel positions.
1053 If this is done during an update, updated_window will contain the
1054 window that is being updated and the position is the future output
1055 cursor position for that window. If updated_window is null, use
1056 selected_window and display the cursor at the given position. */
1058 static void
1059 XTcursor_to (vpos, hpos, y, x)
1060 int vpos, hpos, y, x;
1062 struct window *w;
1064 /* If updated_window is not set, work on selected_window. */
1065 if (updated_window)
1066 w = updated_window;
1067 else
1068 w = XWINDOW (selected_window);
1070 /* Set the output cursor. */
1071 output_cursor.hpos = hpos;
1072 output_cursor.vpos = vpos;
1073 output_cursor.x = x;
1074 output_cursor.y = y;
1076 /* If not called as part of an update, really display the cursor.
1077 This will also set the cursor position of W. */
1078 if (updated_window == NULL)
1080 BLOCK_INPUT;
1081 x_display_cursor (w, 1, hpos, vpos, x, y);
1082 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1083 UNBLOCK_INPUT;
1089 /***********************************************************************
1090 Display Iterator
1091 ***********************************************************************/
1093 /* Function prototypes of this page. */
1095 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1096 struct glyph *,
1097 XChar2b *,
1098 int *));
1099 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1100 int, XChar2b *, int));
1101 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1102 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1103 static void x_append_glyph P_ ((struct it *));
1104 static void x_append_composite_glyph P_ ((struct it *));
1105 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1106 int, int, double));
1107 static void x_produce_glyphs P_ ((struct it *));
1108 static void x_produce_image_glyph P_ ((struct it *it));
1111 /* Return a pointer to per-char metric information in FONT of a
1112 character pointed by B which is a pointer to an XChar2b. */
1114 #define PER_CHAR_METRIC(font, b) \
1115 ((font)->per_char \
1116 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1117 + (((font)->min_byte1 || (font)->max_byte1) \
1118 ? (((b)->byte1 - (font)->min_byte1) \
1119 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1120 : 0)) \
1121 : &((font)->max_bounds))
1124 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1125 is not contained in the font. */
1127 static INLINE XCharStruct *
1128 x_per_char_metric (font, char2b)
1129 XFontStruct *font;
1130 XChar2b *char2b;
1132 /* The result metric information. */
1133 XCharStruct *pcm = NULL;
1135 xassert (font && char2b);
1137 if (font->per_char != NULL)
1139 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1141 /* min_char_or_byte2 specifies the linear character index
1142 corresponding to the first element of the per_char array,
1143 max_char_or_byte2 is the index of the last character. A
1144 character with non-zero CHAR2B->byte1 is not in the font.
1145 A character with byte2 less than min_char_or_byte2 or
1146 greater max_char_or_byte2 is not in the font. */
1147 if (char2b->byte1 == 0
1148 && char2b->byte2 >= font->min_char_or_byte2
1149 && char2b->byte2 <= font->max_char_or_byte2)
1150 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1152 else
1154 /* If either min_byte1 or max_byte1 are nonzero, both
1155 min_char_or_byte2 and max_char_or_byte2 are less than
1156 256, and the 2-byte character index values corresponding
1157 to the per_char array element N (counting from 0) are:
1159 byte1 = N/D + min_byte1
1160 byte2 = N\D + min_char_or_byte2
1162 where:
1164 D = max_char_or_byte2 - min_char_or_byte2 + 1
1165 / = integer division
1166 \ = integer modulus */
1167 if (char2b->byte1 >= font->min_byte1
1168 && char2b->byte1 <= font->max_byte1
1169 && char2b->byte2 >= font->min_char_or_byte2
1170 && char2b->byte2 <= font->max_char_or_byte2)
1172 pcm = (font->per_char
1173 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1174 * (char2b->byte1 - font->min_byte1))
1175 + (char2b->byte2 - font->min_char_or_byte2));
1179 else
1181 /* If the per_char pointer is null, all glyphs between the first
1182 and last character indexes inclusive have the same
1183 information, as given by both min_bounds and max_bounds. */
1184 if (char2b->byte2 >= font->min_char_or_byte2
1185 && char2b->byte2 <= font->max_char_or_byte2)
1186 pcm = &font->max_bounds;
1189 return ((pcm == NULL
1190 || pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0)
1191 ? NULL : pcm);
1195 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1196 the two-byte form of C. Encoding is returned in *CHAR2B. */
1198 static INLINE void
1199 x_encode_char (c, char2b, font_info)
1200 int c;
1201 XChar2b *char2b;
1202 struct font_info *font_info;
1204 int charset = CHAR_CHARSET (c);
1205 XFontStruct *font = font_info->font;
1207 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1208 This may be either a program in a special encoder language or a
1209 fixed encoding. */
1210 if (font_info->font_encoder)
1212 /* It's a program. */
1213 struct ccl_program *ccl = font_info->font_encoder;
1215 if (CHARSET_DIMENSION (charset) == 1)
1217 ccl->reg[0] = charset;
1218 ccl->reg[1] = char2b->byte2;
1220 else
1222 ccl->reg[0] = charset;
1223 ccl->reg[1] = char2b->byte1;
1224 ccl->reg[2] = char2b->byte2;
1227 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1229 /* We assume that MSBs are appropriately set/reset by CCL
1230 program. */
1231 if (font->max_byte1 == 0) /* 1-byte font */
1232 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1233 else
1234 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1236 else if (font_info->encoding[charset])
1238 /* Fixed encoding scheme. See fontset.h for the meaning of the
1239 encoding numbers. */
1240 int enc = font_info->encoding[charset];
1242 if ((enc == 1 || enc == 2)
1243 && CHARSET_DIMENSION (charset) == 2)
1244 char2b->byte1 |= 0x80;
1246 if (enc == 1 || enc == 3)
1247 char2b->byte2 |= 0x80;
1252 /* Get face and two-byte form of character C in face FACE_ID on frame
1253 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1254 means we want to display multibyte text. Value is a pointer to a
1255 realized face that is ready for display. */
1257 static INLINE struct face *
1258 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1259 struct frame *f;
1260 int c, face_id;
1261 XChar2b *char2b;
1262 int multibyte_p;
1264 struct face *face = FACE_FROM_ID (f, face_id);
1266 if (!multibyte_p)
1268 /* Unibyte case. We don't have to encode, but we have to make
1269 sure to use a face suitable for unibyte. */
1270 char2b->byte1 = 0;
1271 char2b->byte2 = c;
1272 face_id = FACE_FOR_CHAR (f, face, c);
1273 face = FACE_FROM_ID (f, face_id);
1275 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1277 /* Case of ASCII in a face known to fit ASCII. */
1278 char2b->byte1 = 0;
1279 char2b->byte2 = c;
1281 else
1283 int c1, c2, charset;
1285 /* Split characters into bytes. If c2 is -1 afterwards, C is
1286 really a one-byte character so that byte1 is zero. */
1287 SPLIT_CHAR (c, charset, c1, c2);
1288 if (c2 > 0)
1289 char2b->byte1 = c1, char2b->byte2 = c2;
1290 else
1291 char2b->byte1 = 0, char2b->byte2 = c1;
1293 /* Maybe encode the character in *CHAR2B. */
1294 if (face->font != NULL)
1296 struct font_info *font_info
1297 = FONT_INFO_FROM_ID (f, face->font_info_id);
1298 if (font_info)
1299 x_encode_char (c, char2b, font_info);
1303 /* Make sure X resources of the face are allocated. */
1304 xassert (face != NULL);
1305 PREPARE_FACE_FOR_DISPLAY (f, face);
1307 return face;
1311 /* Get face and two-byte form of character glyph GLYPH on frame F.
1312 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1313 a pointer to a realized face that is ready for display. */
1315 static INLINE struct face *
1316 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1317 struct frame *f;
1318 struct glyph *glyph;
1319 XChar2b *char2b;
1320 int *two_byte_p;
1322 struct face *face;
1324 xassert (glyph->type == CHAR_GLYPH);
1325 face = FACE_FROM_ID (f, glyph->face_id);
1327 if (two_byte_p)
1328 *two_byte_p = 0;
1330 if (!glyph->multibyte_p)
1332 /* Unibyte case. We don't have to encode, but we have to make
1333 sure to use a face suitable for unibyte. */
1334 char2b->byte1 = 0;
1335 char2b->byte2 = glyph->u.ch;
1337 else if (glyph->u.ch < 128
1338 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1340 /* Case of ASCII in a face known to fit ASCII. */
1341 char2b->byte1 = 0;
1342 char2b->byte2 = glyph->u.ch;
1344 else
1346 int c1, c2, charset;
1348 /* Split characters into bytes. If c2 is -1 afterwards, C is
1349 really a one-byte character so that byte1 is zero. */
1350 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1351 if (c2 > 0)
1352 char2b->byte1 = c1, char2b->byte2 = c2;
1353 else
1354 char2b->byte1 = 0, char2b->byte2 = c1;
1356 /* Maybe encode the character in *CHAR2B. */
1357 if (charset != CHARSET_ASCII)
1359 struct font_info *font_info
1360 = FONT_INFO_FROM_ID (f, face->font_info_id);
1361 if (font_info)
1363 x_encode_char (glyph->u.ch, char2b, font_info);
1364 if (two_byte_p)
1365 *two_byte_p
1366 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1371 /* Make sure X resources of the face are allocated. */
1372 xassert (face != NULL);
1373 PREPARE_FACE_FOR_DISPLAY (f, face);
1374 return face;
1378 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1379 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1381 static INLINE void
1382 x_append_glyph (it)
1383 struct it *it;
1385 struct glyph *glyph;
1386 enum glyph_row_area area = it->area;
1388 xassert (it->glyph_row);
1389 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1391 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1392 if (glyph < it->glyph_row->glyphs[area + 1])
1394 /* Play it safe. If sub-structures of the glyph are not all the
1395 same size, it otherwise be that some bits stay set. This
1396 would prevent a comparison with GLYPH_EQUAL_P. */
1397 glyph->u.val = 0;
1399 glyph->type = CHAR_GLYPH;
1400 glyph->pixel_width = it->pixel_width;
1401 glyph->u.ch = it->char_to_display;
1402 glyph->face_id = it->face_id;
1403 glyph->charpos = CHARPOS (it->position);
1404 glyph->object = it->object;
1405 glyph->left_box_line_p = it->start_of_box_run_p;
1406 glyph->right_box_line_p = it->end_of_box_run_p;
1407 glyph->voffset = it->voffset;
1408 glyph->multibyte_p = it->multibyte_p;
1409 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1410 || it->phys_descent > it->descent);
1411 glyph->glyph_not_available_p = it->glyph_not_available_p;
1412 ++it->glyph_row->used[area];
1416 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1417 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1419 static INLINE void
1420 x_append_composite_glyph (it)
1421 struct it *it;
1423 struct glyph *glyph;
1424 enum glyph_row_area area = it->area;
1426 xassert (it->glyph_row);
1428 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1429 if (glyph < it->glyph_row->glyphs[area + 1])
1431 /* Play it safe. If sub-structures of the glyph are not all the
1432 same size, it otherwise be that some bits stay set. This
1433 would prevent a comparison with GLYPH_EQUAL_P. */
1434 glyph->u.val = 0;
1436 glyph->type = COMPOSITE_GLYPH;
1437 glyph->pixel_width = it->pixel_width;
1438 glyph->u.cmp_id = it->cmp_id;
1439 glyph->face_id = it->face_id;
1440 glyph->charpos = CHARPOS (it->position);
1441 glyph->object = it->object;
1442 glyph->left_box_line_p = it->start_of_box_run_p;
1443 glyph->right_box_line_p = it->end_of_box_run_p;
1444 glyph->voffset = it->voffset;
1445 glyph->multibyte_p = it->multibyte_p;
1446 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1447 || it->phys_descent > it->descent);
1448 ++it->glyph_row->used[area];
1453 /* Change IT->ascent and IT->height according to the setting of
1454 IT->voffset. */
1456 static INLINE void
1457 take_vertical_position_into_account (it)
1458 struct it *it;
1460 if (it->voffset)
1462 if (it->voffset < 0)
1463 /* Increase the ascent so that we can display the text higher
1464 in the line. */
1465 it->ascent += abs (it->voffset);
1466 else
1467 /* Increase the descent so that we can display the text lower
1468 in the line. */
1469 it->descent += it->voffset;
1474 /* Produce glyphs/get display metrics for the image IT is loaded with.
1475 See the description of struct display_iterator in dispextern.h for
1476 an overview of struct display_iterator. */
1478 static void
1479 x_produce_image_glyph (it)
1480 struct it *it;
1482 struct image *img;
1483 struct face *face;
1485 xassert (it->what == IT_IMAGE);
1487 face = FACE_FROM_ID (it->f, it->face_id);
1488 img = IMAGE_FROM_ID (it->f, it->image_id);
1489 xassert (img);
1491 /* Make sure X resources of the face and image are loaded. */
1492 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1493 prepare_image_for_display (it->f, img);
1495 it->ascent = it->phys_ascent = IMAGE_ASCENT (img);
1496 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1497 it->pixel_width = img->width + 2 * img->margin;
1499 it->nglyphs = 1;
1501 if (face->box != FACE_NO_BOX)
1503 it->ascent += face->box_line_width;
1504 it->descent += face->box_line_width;
1506 if (it->start_of_box_run_p)
1507 it->pixel_width += face->box_line_width;
1508 if (it->end_of_box_run_p)
1509 it->pixel_width += face->box_line_width;
1512 take_vertical_position_into_account (it);
1514 if (it->glyph_row)
1516 struct glyph *glyph;
1517 enum glyph_row_area area = it->area;
1519 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1520 if (glyph < it->glyph_row->glyphs[area + 1])
1522 glyph->type = IMAGE_GLYPH;
1523 glyph->u.img_id = img->id;
1524 glyph->face_id = it->face_id;
1525 glyph->pixel_width = it->pixel_width;
1526 glyph->charpos = CHARPOS (it->position);
1527 glyph->object = it->object;
1528 glyph->left_box_line_p = it->start_of_box_run_p;
1529 glyph->right_box_line_p = it->end_of_box_run_p;
1530 glyph->voffset = it->voffset;
1531 glyph->multibyte_p = it->multibyte_p;
1532 ++it->glyph_row->used[area];
1538 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1539 of the glyph, WIDTH and HEIGHT are the width and height of the
1540 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1541 ascent of the glyph (0 <= ASCENT <= 1). */
1543 static void
1544 x_append_stretch_glyph (it, object, width, height, ascent)
1545 struct it *it;
1546 Lisp_Object object;
1547 int width, height;
1548 double ascent;
1550 struct glyph *glyph;
1551 enum glyph_row_area area = it->area;
1553 xassert (ascent >= 0 && ascent <= 1);
1555 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1556 if (glyph < it->glyph_row->glyphs[area + 1])
1558 glyph->type = STRETCH_GLYPH;
1559 glyph->u.stretch.ascent = height * ascent;
1560 glyph->u.stretch.height = height;
1561 glyph->face_id = it->face_id;
1562 glyph->pixel_width = width;
1563 glyph->charpos = CHARPOS (it->position);
1564 glyph->object = object;
1565 glyph->left_box_line_p = it->start_of_box_run_p;
1566 glyph->right_box_line_p = it->end_of_box_run_p;
1567 glyph->voffset = it->voffset;
1568 glyph->multibyte_p = it->multibyte_p;
1569 ++it->glyph_row->used[area];
1574 /* Produce a stretch glyph for iterator IT. IT->object is the value
1575 of the glyph property displayed. The value must be a list
1576 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1577 being recognized:
1579 1. `:width WIDTH' specifies that the space should be WIDTH *
1580 canonical char width wide. WIDTH may be an integer or floating
1581 point number.
1583 2. `:relative-width FACTOR' specifies that the width of the stretch
1584 should be computed from the width of the first character having the
1585 `glyph' property, and should be FACTOR times that width.
1587 3. `:align-to HPOS' specifies that the space should be wide enough
1588 to reach HPOS, a value in canonical character units.
1590 Exactly one of the above pairs must be present.
1592 4. `:height HEIGHT' specifies that the height of the stretch produced
1593 should be HEIGHT, measured in canonical character units.
1595 5. `:relative-height FACTOR' specifies that the height of the the
1596 stretch should be FACTOR times the height of the characters having
1597 the glyph property.
1599 Either none or exactly one of 4 or 5 must be present.
1601 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1602 of the stretch should be used for the ascent of the stretch.
1603 ASCENT must be in the range 0 <= ASCENT <= 100. */
1605 #define NUMVAL(X) \
1606 ((INTEGERP (X) || FLOATP (X)) \
1607 ? XFLOATINT (X) \
1608 : - 1)
1611 static void
1612 x_produce_stretch_glyph (it)
1613 struct it *it;
1615 /* (space :width WIDTH :height HEIGHT. */
1616 extern Lisp_Object QCwidth, QCheight, QCascent, Qspace;
1617 extern Lisp_Object QCrelative_width, QCrelative_height;
1618 extern Lisp_Object QCalign_to;
1619 Lisp_Object prop, plist;
1620 double width = 0, height = 0, ascent = 0;
1621 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1622 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1624 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1626 /* List should start with `space'. */
1627 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1628 plist = XCDR (it->object);
1630 /* Compute the width of the stretch. */
1631 if (prop = Fplist_get (plist, QCwidth),
1632 NUMVAL (prop) > 0)
1633 /* Absolute width `:width WIDTH' specified and valid. */
1634 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1635 else if (prop = Fplist_get (plist, QCrelative_width),
1636 NUMVAL (prop) > 0)
1638 /* Relative width `:relative-width FACTOR' specified and valid.
1639 Compute the width of the characters having the `glyph'
1640 property. */
1641 struct it it2;
1642 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1644 it2 = *it;
1645 if (it->multibyte_p)
1647 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1648 - IT_BYTEPOS (*it));
1649 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1651 else
1652 it2.c = *p, it2.len = 1;
1654 it2.glyph_row = NULL;
1655 it2.what = IT_CHARACTER;
1656 x_produce_glyphs (&it2);
1657 width = NUMVAL (prop) * it2.pixel_width;
1659 else if (prop = Fplist_get (plist, QCalign_to),
1660 NUMVAL (prop) > 0)
1661 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1662 else
1663 /* Nothing specified -> width defaults to canonical char width. */
1664 width = CANON_X_UNIT (it->f);
1666 /* Compute height. */
1667 if (prop = Fplist_get (plist, QCheight),
1668 NUMVAL (prop) > 0)
1669 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1670 else if (prop = Fplist_get (plist, QCrelative_height),
1671 NUMVAL (prop) > 0)
1672 height = FONT_HEIGHT (font) * NUMVAL (prop);
1673 else
1674 height = FONT_HEIGHT (font);
1676 /* Compute percentage of height used for ascent. If
1677 `:ascent ASCENT' is present and valid, use that. Otherwise,
1678 derive the ascent from the font in use. */
1679 if (prop = Fplist_get (plist, QCascent),
1680 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1681 ascent = NUMVAL (prop) / 100.0;
1682 else
1683 ascent = (double) font->ascent / FONT_HEIGHT (font);
1685 if (width <= 0)
1686 width = 1;
1687 if (height <= 0)
1688 height = 1;
1690 if (it->glyph_row)
1692 Lisp_Object object = it->stack[it->sp - 1].string;
1693 if (!STRINGP (object))
1694 object = it->w->buffer;
1695 x_append_stretch_glyph (it, object, width, height, ascent);
1698 it->pixel_width = width;
1699 it->ascent = it->phys_ascent = height * ascent;
1700 it->descent = it->phys_descent = height - it->ascent;
1701 it->nglyphs = 1;
1703 if (face->box != FACE_NO_BOX)
1705 it->ascent += face->box_line_width;
1706 it->descent += face->box_line_width;
1708 if (it->start_of_box_run_p)
1709 it->pixel_width += face->box_line_width;
1710 if (it->end_of_box_run_p)
1711 it->pixel_width += face->box_line_width;
1714 take_vertical_position_into_account (it);
1717 /* Return proper value to be used as baseline offset of font that has
1718 ASCENT and DESCENT to draw characters by the font at the vertical
1719 center of the line of frame F.
1721 Here, out task is to find the value of BOFF in the following figure;
1723 -------------------------+-----------+-
1724 -+-+---------+-+ | |
1725 | | | | | |
1726 | | | | F_ASCENT F_HEIGHT
1727 | | | ASCENT | |
1728 HEIGHT | | | | |
1729 | | |-|-+------+-----------|------- baseline
1730 | | | | BOFF | |
1731 | |---------|-+-+ | |
1732 | | | DESCENT | |
1733 -+-+---------+-+ F_DESCENT |
1734 -------------------------+-----------+-
1736 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1737 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1738 DESCENT = FONT->descent
1739 HEIGHT = FONT_HEIGHT (FONT)
1740 F_DESCENT = (F->output_data.x->font->descent
1741 - F->output_data.x->baseline_offset)
1742 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1745 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1746 ((FONT)->descent \
1747 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1748 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1750 /* Produce glyphs/get display metrics for the display element IT is
1751 loaded with. See the description of struct display_iterator in
1752 dispextern.h for an overview of struct display_iterator. */
1754 static void
1755 x_produce_glyphs (it)
1756 struct it *it;
1758 it->glyph_not_available_p = 0;
1760 if (it->what == IT_CHARACTER)
1762 XChar2b char2b;
1763 XFontStruct *font;
1764 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1765 XCharStruct *pcm;
1766 int font_not_found_p;
1767 struct font_info *font_info;
1768 int boff; /* baseline offset */
1770 /* Maybe translate single-byte characters to multibyte, or the
1771 other way. */
1772 it->char_to_display = it->c;
1773 if (!ASCII_BYTE_P (it->c))
1775 if (unibyte_display_via_language_environment
1776 && SINGLE_BYTE_CHAR_P (it->c)
1777 && (it->c >= 0240
1778 || !NILP (Vnonascii_translation_table)))
1780 it->char_to_display = unibyte_char_to_multibyte (it->c);
1781 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1782 face = FACE_FROM_ID (it->f, it->face_id);
1784 else if (!SINGLE_BYTE_CHAR_P (it->c)
1785 && !it->multibyte_p)
1787 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1788 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1789 face = FACE_FROM_ID (it->f, it->face_id);
1793 /* Get font to use. Encode IT->char_to_display. */
1794 x_get_char_face_and_encoding (it->f, it->char_to_display,
1795 it->face_id, &char2b,
1796 it->multibyte_p);
1797 font = face->font;
1799 /* When no suitable font found, use the default font. */
1800 font_not_found_p = font == NULL;
1801 if (font_not_found_p)
1803 font = FRAME_FONT (it->f);
1804 boff = it->f->output_data.x->baseline_offset;
1805 font_info = NULL;
1807 else
1809 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1810 boff = font_info->baseline_offset;
1811 if (font_info->vertical_centering)
1812 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1815 if (it->char_to_display >= ' '
1816 && (!it->multibyte_p || it->char_to_display < 128))
1818 /* Either unibyte or ASCII. */
1819 int stretched_p;
1821 it->nglyphs = 1;
1823 pcm = x_per_char_metric (font, &char2b);
1824 it->ascent = font->ascent + boff;
1825 it->descent = font->descent - boff;
1826 it->phys_ascent = pcm->ascent + boff;
1827 it->phys_descent = pcm->descent - boff;
1828 it->pixel_width = pcm->width;
1830 /* If this is a space inside a region of text with
1831 `space-width' property, change its width. */
1832 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1833 if (stretched_p)
1834 it->pixel_width *= XFLOATINT (it->space_width);
1836 /* If face has a box, add the box thickness to the character
1837 height. If character has a box line to the left and/or
1838 right, add the box line width to the character's width. */
1839 if (face->box != FACE_NO_BOX)
1841 int thick = face->box_line_width;
1843 it->ascent += thick;
1844 it->descent += thick;
1846 if (it->start_of_box_run_p)
1847 it->pixel_width += thick;
1848 if (it->end_of_box_run_p)
1849 it->pixel_width += thick;
1852 /* If face has an overline, add the height of the overline
1853 (1 pixel) and a 1 pixel margin to the character height. */
1854 if (face->overline_p)
1855 it->ascent += 2;
1857 take_vertical_position_into_account (it);
1859 /* If we have to actually produce glyphs, do it. */
1860 if (it->glyph_row)
1862 if (stretched_p)
1864 /* Translate a space with a `space-width' property
1865 into a stretch glyph. */
1866 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1867 x_append_stretch_glyph (it, it->object, it->pixel_width,
1868 it->ascent + it->descent, ascent);
1870 else
1871 x_append_glyph (it);
1873 /* If characters with lbearing or rbearing are displayed
1874 in this line, record that fact in a flag of the
1875 glyph row. This is used to optimize X output code. */
1876 if (pcm->lbearing < 0
1877 || pcm->rbearing > pcm->width)
1878 it->glyph_row->contains_overlapping_glyphs_p = 1;
1881 else if (it->char_to_display == '\n')
1883 /* A newline has no width but we need the height of the line. */
1884 it->pixel_width = 0;
1885 it->nglyphs = 0;
1886 it->ascent = it->phys_ascent = font->ascent + boff;
1887 it->descent = it->phys_descent = font->descent - boff;
1889 if (face->box != FACE_NO_BOX)
1891 int thick = face->box_line_width;
1892 it->ascent += thick;
1893 it->descent += thick;
1896 else if (it->char_to_display == '\t')
1898 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1899 int x = (it->current_x
1900 - it->prompt_width
1901 + it->continuation_lines_width);
1902 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1904 it->pixel_width = next_tab_x - x;
1905 it->nglyphs = 1;
1906 it->ascent = it->phys_ascent = font->ascent + boff;
1907 it->descent = it->phys_descent = font->descent - boff;
1909 if (it->glyph_row)
1911 double ascent = (double) it->ascent / (it->ascent + it->descent);
1912 x_append_stretch_glyph (it, it->object, it->pixel_width,
1913 it->ascent + it->descent, ascent);
1916 else
1918 /* A multi-byte character. Assume that the display width of the
1919 character is the width of the character multiplied by the
1920 width of the font. */
1922 /* If we found a font, this font should give us the right
1923 metrics. If we didn't find a font, use the frame's
1924 default font and calculate the width of the character
1925 from the charset width; this is what old redisplay code
1926 did. */
1927 pcm = x_per_char_metric (font, &char2b);
1928 if (font_not_found_p || !pcm)
1930 int charset = CHAR_CHARSET (it->char_to_display);
1932 it->glyph_not_available_p = 1;
1933 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1934 * CHARSET_WIDTH (charset));
1935 it->phys_ascent = font->ascent + boff;
1936 it->phys_descent = font->descent - boff;
1938 else
1940 it->pixel_width = pcm->width;
1941 it->phys_ascent = pcm->ascent + boff;
1942 it->phys_descent = pcm->descent - boff;
1943 if (it->glyph_row
1944 && (pcm->lbearing < 0
1945 || pcm->rbearing > pcm->width))
1946 it->glyph_row->contains_overlapping_glyphs_p = 1;
1948 it->nglyphs = 1;
1949 it->ascent = font->ascent + boff;
1950 it->descent = font->descent - boff;
1951 if (face->box != FACE_NO_BOX)
1953 int thick = face->box_line_width;
1954 it->ascent += thick;
1955 it->descent += thick;
1957 if (it->start_of_box_run_p)
1958 it->pixel_width += thick;
1959 if (it->end_of_box_run_p)
1960 it->pixel_width += thick;
1963 /* If face has an overline, add the height of the overline
1964 (1 pixel) and a 1 pixel margin to the character height. */
1965 if (face->overline_p)
1966 it->ascent += 2;
1968 take_vertical_position_into_account (it);
1970 if (it->glyph_row)
1971 x_append_glyph (it);
1974 else if (it->what == IT_COMPOSITION)
1976 /* Note: A composition is represented as one glyph in the
1977 glyph matrix. There are no padding glyphs. */
1978 XChar2b char2b;
1979 XFontStruct *font;
1980 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1981 XCharStruct *pcm;
1982 int font_not_found_p;
1983 struct font_info *font_info;
1984 int boff; /* baseline offset */
1985 struct composition *cmp = composition_table[it->cmp_id];
1987 /* Maybe translate single-byte characters to multibyte. */
1988 it->char_to_display = it->c;
1989 if (unibyte_display_via_language_environment
1990 && SINGLE_BYTE_CHAR_P (it->c)
1991 && (it->c >= 0240
1992 || (it->c >= 0200
1993 && !NILP (Vnonascii_translation_table))))
1995 it->char_to_display = unibyte_char_to_multibyte (it->c);
1998 /* Get face and font to use. Encode IT->char_to_display. */
1999 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2000 face = FACE_FROM_ID (it->f, it->face_id);
2001 x_get_char_face_and_encoding (it->f, it->char_to_display,
2002 it->face_id, &char2b, it->multibyte_p);
2003 font = face->font;
2005 /* When no suitable font found, use the default font. */
2006 font_not_found_p = font == NULL;
2007 if (font_not_found_p)
2009 font = FRAME_FONT (it->f);
2010 boff = it->f->output_data.x->baseline_offset;
2011 font_info = NULL;
2013 else
2015 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2016 boff = font_info->baseline_offset;
2017 if (font_info->vertical_centering)
2018 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2021 /* There are no padding glyphs, so there is only one glyph to
2022 produce for the composition. Important is that pixel_width,
2023 ascent and descent are the values of what is drawn by
2024 draw_glyphs (i.e. the values of the overall glyphs composed). */
2025 it->nglyphs = 1;
2027 /* If we have not yet calculated pixel size data of glyphs of
2028 the composition for the current face font, calculate them
2029 now. Theoretically, we have to check all fonts for the
2030 glyphs, but that requires much time and memory space. So,
2031 here we check only the font of the first glyph. This leads
2032 to incorrect display very rarely, and C-l (recenter) can
2033 correct the display anyway. */
2034 if (cmp->font != (void *) font)
2036 /* Ascent and descent of the font of the first character of
2037 this composition (adjusted by baseline offset). Ascent
2038 and descent of overall glyphs should not be less than
2039 them respectively. */
2040 int font_ascent = font->ascent + boff;
2041 int font_descent = font->descent - boff;
2042 /* Bounding box of the overall glyphs. */
2043 int leftmost, rightmost, lowest, highest;
2044 int i;
2046 cmp->font = (void *) font;
2048 /* Initialize the bounding box. */
2049 pcm = x_per_char_metric (font, &char2b);
2050 leftmost = 0;
2051 rightmost = pcm->width;
2052 lowest = - pcm->descent + boff;
2053 highest = pcm->ascent + boff;
2054 if (font_info
2055 && font_info->default_ascent
2056 && CHAR_TABLE_P (Vuse_default_ascent)
2057 && !NILP (Faref (Vuse_default_ascent,
2058 make_number (it->char_to_display))))
2059 highest = font_info->default_ascent + boff;
2061 /* Draw the first glyph at the normal position. It may be
2062 shifted to right later if some other glyphs are drawn at
2063 the left. */
2064 cmp->offsets[0] = 0;
2065 cmp->offsets[1] = boff;
2067 /* Set cmp->offsets for the remaining glyphs. */
2068 for (i = 1; i < cmp->glyph_len; i++)
2070 int left, right, btm, top;
2071 int ch = COMPOSITION_GLYPH (cmp, i);
2072 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2074 face = FACE_FROM_ID (it->f, face_id);
2075 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2076 it->multibyte_p);
2077 font = face->font;
2078 if (font == NULL)
2080 font = FRAME_FONT (it->f);
2081 boff = it->f->output_data.x->baseline_offset;
2082 font_info = NULL;
2084 else
2086 font_info
2087 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2088 boff = font_info->baseline_offset;
2089 if (font_info->vertical_centering)
2090 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2093 pcm = x_per_char_metric (font, &char2b);
2095 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2097 /* Relative composition with or without
2098 alternate chars. */
2099 left = (leftmost + rightmost - pcm->width) / 2;
2100 btm = - pcm->descent + boff;
2101 if (font_info && font_info->relative_compose
2102 && (! CHAR_TABLE_P (Vignore_relative_composition)
2103 || NILP (Faref (Vignore_relative_composition,
2104 make_number (ch)))))
2107 if (- pcm->descent
2108 >= font_info->relative_compose)
2109 /* One extra pixel between two glyphs. */
2110 btm = highest + 1;
2111 else if (pcm->ascent <= 0)
2112 /* One extra pixel between two glyphs. */
2113 btm = lowest - 1 - pcm->ascent - pcm->descent;
2116 else
2118 /* A composition rule is specified by an integer
2119 value that encodes global and new reference
2120 points (GREF and NREF). GREF and NREF are
2121 specified by numbers as below:
2123 0---1---2 -- ascent
2127 9--10--11 -- center
2129 ---3---4---5--- baseline
2131 6---7---8 -- descent
2133 int rule = COMPOSITION_RULE (cmp, i);
2134 int gref, nref, grefx, grefy, nrefx, nrefy;
2136 COMPOSITION_DECODE_RULE (rule, gref, nref);
2137 grefx = gref % 3, nrefx = nref % 3;
2138 grefy = gref / 3, nrefy = nref / 3;
2140 left = (leftmost
2141 + grefx * (rightmost - leftmost) / 2
2142 - nrefx * pcm->width / 2);
2143 btm = ((grefy == 0 ? highest
2144 : grefy == 1 ? 0
2145 : grefy == 2 ? lowest
2146 : (highest + lowest) / 2)
2147 - (nrefy == 0 ? pcm->ascent + pcm->descent
2148 : nrefy == 1 ? pcm->descent - boff
2149 : nrefy == 2 ? 0
2150 : (pcm->ascent + pcm->descent) / 2));
2153 cmp->offsets[i * 2] = left;
2154 cmp->offsets[i * 2 + 1] = btm + pcm->descent;
2156 /* Update the bounding box of the overall glyphs. */
2157 right = left + pcm->width;
2158 top = btm + pcm->descent + pcm->ascent;
2159 if (left < leftmost)
2160 leftmost = left;
2161 if (right > rightmost)
2162 rightmost = right;
2163 if (top > highest)
2164 highest = top;
2165 if (btm < lowest)
2166 lowest = btm;
2169 /* If there are glyphs whose x-offsets are negative,
2170 shift all glyphs to the right and make all x-offsets
2171 non-negative. */
2172 if (leftmost < 0)
2174 for (i = 0; i < cmp->glyph_len; i++)
2175 cmp->offsets[i * 2] -= leftmost;
2176 rightmost -= leftmost;
2179 cmp->pixel_width = rightmost;
2180 cmp->ascent = highest;
2181 cmp->descent = - lowest;
2182 if (cmp->ascent < font_ascent)
2183 cmp->ascent = font_ascent;
2184 if (cmp->descent < font_descent)
2185 cmp->descent = font_descent;
2188 it->pixel_width = cmp->pixel_width;
2189 it->ascent = it->phys_ascent = cmp->ascent;
2190 it->descent = it->phys_descent = cmp->descent;
2192 if (face->box != FACE_NO_BOX)
2194 int thick = face->box_line_width;
2195 it->ascent += thick;
2196 it->descent += thick;
2198 if (it->start_of_box_run_p)
2199 it->pixel_width += thick;
2200 if (it->end_of_box_run_p)
2201 it->pixel_width += thick;
2204 /* If face has an overline, add the height of the overline
2205 (1 pixel) and a 1 pixel margin to the character height. */
2206 if (face->overline_p)
2207 it->ascent += 2;
2209 take_vertical_position_into_account (it);
2211 if (it->glyph_row)
2212 x_append_composite_glyph (it);
2214 else if (it->what == IT_IMAGE)
2215 x_produce_image_glyph (it);
2216 else if (it->what == IT_STRETCH)
2217 x_produce_stretch_glyph (it);
2219 /* Accumulate dimensions. */
2220 xassert (it->ascent >= 0 && it->descent > 0);
2221 if (it->area == TEXT_AREA)
2222 it->current_x += it->pixel_width;
2224 it->max_ascent = max (it->max_ascent, it->ascent);
2225 it->max_descent = max (it->max_descent, it->descent);
2226 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2227 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2231 /* Estimate the pixel height of the mode or top line on frame F.
2232 FACE_ID specifies what line's height to estimate. */
2235 x_estimate_mode_line_height (f, face_id)
2236 struct frame *f;
2237 enum face_id face_id;
2239 int height = 1;
2241 /* This function is called so early when Emacs starts that the face
2242 cache and mode line face are not yet initialized. */
2243 if (FRAME_FACE_CACHE (f))
2245 struct face *face = FACE_FROM_ID (f, face_id);
2246 if (face)
2247 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2250 return height;
2254 /***********************************************************************
2255 Glyph display
2256 ***********************************************************************/
2258 /* A sequence of glyphs to be drawn in the same face.
2260 This data structure is not really completely X specific, so it
2261 could possibly, at least partially, be useful for other systems. It
2262 is currently not part of the external redisplay interface because
2263 it's not clear what other systems will need. */
2265 struct glyph_string
2267 /* X-origin of the string. */
2268 int x;
2270 /* Y-origin and y-position of the base line of this string. */
2271 int y, ybase;
2273 /* The width of the string, not including a face extension. */
2274 int width;
2276 /* The width of the string, including a face extension. */
2277 int background_width;
2279 /* The height of this string. This is the height of the line this
2280 string is drawn in, and can be different from the height of the
2281 font the string is drawn in. */
2282 int height;
2284 /* Number of pixels this string overwrites in front of its x-origin.
2285 This number is zero if the string has an lbearing >= 0; it is
2286 -lbearing, if the string has an lbearing < 0. */
2287 int left_overhang;
2289 /* Number of pixels this string overwrites past its right-most
2290 nominal x-position, i.e. x + width. Zero if the string's
2291 rbearing is <= its nominal width, rbearing - width otherwise. */
2292 int right_overhang;
2294 /* The frame on which the glyph string is drawn. */
2295 struct frame *f;
2297 /* The window on which the glyph string is drawn. */
2298 struct window *w;
2300 /* X display and window for convenience. */
2301 Display *display;
2302 Window window;
2304 /* The glyph row for which this string was built. It determines the
2305 y-origin and height of the string. */
2306 struct glyph_row *row;
2308 /* The area within row. */
2309 enum glyph_row_area area;
2311 /* Characters to be drawn, and number of characters. */
2312 XChar2b *char2b;
2313 int nchars;
2315 /* A face-override for drawing cursors, mouse face and similar. */
2316 enum draw_glyphs_face hl;
2318 /* Face in which this string is to be drawn. */
2319 struct face *face;
2321 /* Font in which this string is to be drawn. */
2322 XFontStruct *font;
2324 /* Font info for this string. */
2325 struct font_info *font_info;
2327 /* Non-null means this string describes (part of) a composition.
2328 All characters from char2b are drawn composed. */
2329 struct composition *cmp;
2331 /* Index of this glyph string's first character in the glyph
2332 definition of CMP. If this is zero, this glyph string describes
2333 the first character of a composition. */
2334 int gidx;
2336 /* 1 means this glyph strings face has to be drawn to the right end
2337 of the window's drawing area. */
2338 unsigned extends_to_end_of_line_p : 1;
2340 /* 1 means the background of this string has been drawn. */
2341 unsigned background_filled_p : 1;
2343 /* 1 means glyph string must be drawn with 16-bit functions. */
2344 unsigned two_byte_p : 1;
2346 /* 1 means that the original font determined for drawing this glyph
2347 string could not be loaded. The member `font' has been set to
2348 the frame's default font in this case. */
2349 unsigned font_not_found_p : 1;
2351 /* 1 means that the face in which this glyph string is drawn has a
2352 stipple pattern. */
2353 unsigned stippled_p : 1;
2355 /* 1 means only the foreground of this glyph string must be drawn,
2356 and we should use the physical height of the line this glyph
2357 string appears in as clip rect. */
2358 unsigned for_overlaps_p : 1;
2360 /* The GC to use for drawing this glyph string. */
2361 GC gc;
2363 /* A pointer to the first glyph in the string. This glyph
2364 corresponds to char2b[0]. Needed to draw rectangles if
2365 font_not_found_p is 1. */
2366 struct glyph *first_glyph;
2368 /* Image, if any. */
2369 struct image *img;
2371 struct glyph_string *next, *prev;
2375 #if 0
2377 static void
2378 x_dump_glyph_string (s)
2379 struct glyph_string *s;
2381 fprintf (stderr, "glyph string\n");
2382 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2383 s->x, s->y, s->width, s->height);
2384 fprintf (stderr, " ybase = %d\n", s->ybase);
2385 fprintf (stderr, " hl = %d\n", s->hl);
2386 fprintf (stderr, " left overhang = %d, right = %d\n",
2387 s->left_overhang, s->right_overhang);
2388 fprintf (stderr, " nchars = %d\n", s->nchars);
2389 fprintf (stderr, " extends to end of line = %d\n",
2390 s->extends_to_end_of_line_p);
2391 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2392 fprintf (stderr, " bg width = %d\n", s->background_width);
2395 #endif /* GLYPH_DEBUG */
2399 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2400 struct glyph_string **,
2401 struct glyph_string *,
2402 struct glyph_string *));
2403 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2404 struct glyph_string **,
2405 struct glyph_string *,
2406 struct glyph_string *));
2407 static void x_append_glyph_string P_ ((struct glyph_string **,
2408 struct glyph_string **,
2409 struct glyph_string *));
2410 static int x_left_overwritten P_ ((struct glyph_string *));
2411 static int x_left_overwriting P_ ((struct glyph_string *));
2412 static int x_right_overwritten P_ ((struct glyph_string *));
2413 static int x_right_overwriting P_ ((struct glyph_string *));
2414 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2415 int));
2416 static void x_init_glyph_string P_ ((struct glyph_string *,
2417 XChar2b *, struct window *,
2418 struct glyph_row *,
2419 enum glyph_row_area, int,
2420 enum draw_glyphs_face));
2421 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2422 enum glyph_row_area, int, int,
2423 enum draw_glyphs_face, int *, int *, int));
2424 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2425 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2426 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2427 int));
2428 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2429 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2430 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2431 static void x_draw_glyph_string P_ ((struct glyph_string *));
2432 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2433 static void x_set_cursor_gc P_ ((struct glyph_string *));
2434 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2435 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2436 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2437 int *, int *));
2438 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2439 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2440 unsigned long *, double, int));
2441 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2442 double, int, unsigned long));
2443 static void x_setup_relief_colors P_ ((struct glyph_string *));
2444 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2445 static void x_draw_image_relief P_ ((struct glyph_string *));
2446 static void x_draw_image_foreground P_ ((struct glyph_string *));
2447 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2448 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2449 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2450 int, int, int));
2451 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2452 int, int, int, int, XRectangle *));
2453 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2454 int, int, int, XRectangle *));
2455 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2456 enum glyph_row_area));
2459 /* Append the list of glyph strings with head H and tail T to the list
2460 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2462 static INLINE void
2463 x_append_glyph_string_lists (head, tail, h, t)
2464 struct glyph_string **head, **tail;
2465 struct glyph_string *h, *t;
2467 if (h)
2469 if (*head)
2470 (*tail)->next = h;
2471 else
2472 *head = h;
2473 h->prev = *tail;
2474 *tail = t;
2479 /* Prepend the list of glyph strings with head H and tail T to the
2480 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2481 result. */
2483 static INLINE void
2484 x_prepend_glyph_string_lists (head, tail, h, t)
2485 struct glyph_string **head, **tail;
2486 struct glyph_string *h, *t;
2488 if (h)
2490 if (*head)
2491 (*head)->prev = t;
2492 else
2493 *tail = t;
2494 t->next = *head;
2495 *head = h;
2500 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2501 Set *HEAD and *TAIL to the resulting list. */
2503 static INLINE void
2504 x_append_glyph_string (head, tail, s)
2505 struct glyph_string **head, **tail;
2506 struct glyph_string *s;
2508 s->next = s->prev = NULL;
2509 x_append_glyph_string_lists (head, tail, s, s);
2513 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2514 face. */
2516 static void
2517 x_set_cursor_gc (s)
2518 struct glyph_string *s;
2520 if (s->font == FRAME_FONT (s->f)
2521 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2522 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2523 && !s->cmp)
2524 s->gc = s->f->output_data.x->cursor_gc;
2525 else
2527 /* Cursor on non-default face: must merge. */
2528 XGCValues xgcv;
2529 unsigned long mask;
2531 xgcv.background = s->f->output_data.x->cursor_pixel;
2532 xgcv.foreground = s->face->background;
2534 /* If the glyph would be invisible, try a different foreground. */
2535 if (xgcv.foreground == xgcv.background)
2536 xgcv.foreground = s->face->foreground;
2537 if (xgcv.foreground == xgcv.background)
2538 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2539 if (xgcv.foreground == xgcv.background)
2540 xgcv.foreground = s->face->foreground;
2542 /* Make sure the cursor is distinct from text in this face. */
2543 if (xgcv.background == s->face->background
2544 && xgcv.foreground == s->face->foreground)
2546 xgcv.background = s->face->foreground;
2547 xgcv.foreground = s->face->background;
2550 IF_DEBUG (x_check_font (s->f, s->font));
2551 xgcv.font = s->font->fid;
2552 xgcv.graphics_exposures = False;
2553 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2555 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2556 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2557 mask, &xgcv);
2558 else
2559 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2560 = XCreateGC (s->display, s->window, mask, &xgcv);
2562 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2567 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2569 static void
2570 x_set_mouse_face_gc (s)
2571 struct glyph_string *s;
2573 int face_id;
2574 struct face *face;
2576 /* What face has to be used for the mouse face? */
2577 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2578 face = FACE_FROM_ID (s->f, face_id);
2579 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2580 s->face = FACE_FROM_ID (s->f, face_id);
2581 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2583 /* If font in this face is same as S->font, use it. */
2584 if (s->font == s->face->font)
2585 s->gc = s->face->gc;
2586 else
2588 /* Otherwise construct scratch_cursor_gc with values from FACE
2589 but font FONT. */
2590 XGCValues xgcv;
2591 unsigned long mask;
2593 xgcv.background = s->face->background;
2594 xgcv.foreground = s->face->foreground;
2595 IF_DEBUG (x_check_font (s->f, s->font));
2596 xgcv.font = s->font->fid;
2597 xgcv.graphics_exposures = False;
2598 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2600 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2601 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2602 mask, &xgcv);
2603 else
2604 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2605 = XCreateGC (s->display, s->window, mask, &xgcv);
2607 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2610 xassert (s->gc != 0);
2614 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2615 Faces to use in the mode line have already been computed when the
2616 matrix was built, so there isn't much to do, here. */
2618 static INLINE void
2619 x_set_mode_line_face_gc (s)
2620 struct glyph_string *s;
2622 s->gc = s->face->gc;
2623 xassert (s->gc != 0);
2627 /* Set S->gc of glyph string S for drawing that glyph string. Set
2628 S->stippled_p to a non-zero value if the face of S has a stipple
2629 pattern. */
2631 static INLINE void
2632 x_set_glyph_string_gc (s)
2633 struct glyph_string *s;
2635 if (s->hl == DRAW_NORMAL_TEXT)
2637 s->gc = s->face->gc;
2638 s->stippled_p = s->face->stipple != 0;
2640 else if (s->hl == DRAW_INVERSE_VIDEO)
2642 x_set_mode_line_face_gc (s);
2643 s->stippled_p = s->face->stipple != 0;
2645 else if (s->hl == DRAW_CURSOR)
2647 x_set_cursor_gc (s);
2648 s->stippled_p = 0;
2650 else if (s->hl == DRAW_MOUSE_FACE)
2652 x_set_mouse_face_gc (s);
2653 s->stippled_p = s->face->stipple != 0;
2655 else if (s->hl == DRAW_IMAGE_RAISED
2656 || s->hl == DRAW_IMAGE_SUNKEN)
2658 s->gc = s->face->gc;
2659 s->stippled_p = s->face->stipple != 0;
2661 else
2663 s->gc = s->face->gc;
2664 s->stippled_p = s->face->stipple != 0;
2667 /* GC must have been set. */
2668 xassert (s->gc != 0);
2672 /* Return in *R the clipping rectangle for glyph string S. */
2674 static void
2675 x_get_glyph_string_clip_rect (s, r)
2676 struct glyph_string *s;
2677 XRectangle *r;
2679 if (s->row->full_width_p)
2681 /* Draw full-width. X coordinates are relative to S->w->left. */
2682 int canon_x = CANON_X_UNIT (s->f);
2684 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2685 r->width = XFASTINT (s->w->width) * canon_x;
2687 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2689 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2690 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2691 r->x -= width;
2694 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2696 /* Unless displaying a mode or menu bar line, which are always
2697 fully visible, clip to the visible part of the row. */
2698 if (s->w->pseudo_window_p)
2699 r->height = s->row->visible_height;
2700 else
2701 r->height = s->height;
2703 else
2705 /* This is a text line that may be partially visible. */
2706 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2707 r->width = window_box_width (s->w, s->area);
2708 r->height = s->row->visible_height;
2711 /* Don't use S->y for clipping because it doesn't take partially
2712 visible lines into account. For example, it can be negative for
2713 partially visible lines at the top of a window. */
2714 if (!s->row->full_width_p
2715 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2716 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2717 else
2718 r->y = max (0, s->row->y);
2720 /* If drawing a tool-bar window, draw it over the internal border
2721 at the top of the window. */
2722 if (s->w == XWINDOW (s->f->tool_bar_window))
2723 r->y -= s->f->output_data.x->internal_border_width;
2725 /* If S draws overlapping rows, it's sufficient to use the top and
2726 bottom of the window for clipping because this glyph string
2727 intentionally draws over other lines. */
2728 if (s->for_overlaps_p)
2730 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2731 r->height = window_text_bottom_y (s->w) - r->y;
2734 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2738 /* Set clipping for output of glyph string S. S may be part of a mode
2739 line or menu if we don't have X toolkit support. */
2741 static INLINE void
2742 x_set_glyph_string_clipping (s)
2743 struct glyph_string *s;
2745 XRectangle r;
2746 x_get_glyph_string_clip_rect (s, &r);
2747 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2751 /* Compute left and right overhang of glyph string S. If S is a glyph
2752 string for a composition, assume overhangs don't exist. */
2754 static INLINE void
2755 x_compute_glyph_string_overhangs (s)
2756 struct glyph_string *s;
2758 if (s->cmp == NULL
2759 && s->first_glyph->type == CHAR_GLYPH)
2761 XCharStruct cs;
2762 int direction, font_ascent, font_descent;
2763 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2764 &font_ascent, &font_descent, &cs);
2765 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2766 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2771 /* Compute overhangs and x-positions for glyph string S and its
2772 predecessors, or successors. X is the starting x-position for S.
2773 BACKWARD_P non-zero means process predecessors. */
2775 static void
2776 x_compute_overhangs_and_x (s, x, backward_p)
2777 struct glyph_string *s;
2778 int x;
2779 int backward_p;
2781 if (backward_p)
2783 while (s)
2785 x_compute_glyph_string_overhangs (s);
2786 x -= s->width;
2787 s->x = x;
2788 s = s->prev;
2791 else
2793 while (s)
2795 x_compute_glyph_string_overhangs (s);
2796 s->x = x;
2797 x += s->width;
2798 s = s->next;
2804 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2805 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2806 assumed to be zero. */
2808 static void
2809 x_get_glyph_overhangs (glyph, f, left, right)
2810 struct glyph *glyph;
2811 struct frame *f;
2812 int *left, *right;
2814 int c;
2816 *left = *right = 0;
2818 if (glyph->type == CHAR_GLYPH)
2820 XFontStruct *font;
2821 struct face *face;
2822 struct font_info *font_info;
2823 XChar2b char2b;
2824 XCharStruct *pcm;
2826 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2827 font = face->font;
2828 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2829 if (font
2830 && (pcm = x_per_char_metric (font, &char2b)))
2832 if (pcm->rbearing > pcm->width)
2833 *right = pcm->rbearing - pcm->width;
2834 if (pcm->lbearing < 0)
2835 *left = -pcm->lbearing;
2841 /* Return the index of the first glyph preceding glyph string S that
2842 is overwritten by S because of S's left overhang. Value is -1
2843 if no glyphs are overwritten. */
2845 static int
2846 x_left_overwritten (s)
2847 struct glyph_string *s;
2849 int k;
2851 if (s->left_overhang)
2853 int x = 0, i;
2854 struct glyph *glyphs = s->row->glyphs[s->area];
2855 int first = s->first_glyph - glyphs;
2857 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2858 x -= glyphs[i].pixel_width;
2860 k = i + 1;
2862 else
2863 k = -1;
2865 return k;
2869 /* Return the index of the first glyph preceding glyph string S that
2870 is overwriting S because of its right overhang. Value is -1 if no
2871 glyph in front of S overwrites S. */
2873 static int
2874 x_left_overwriting (s)
2875 struct glyph_string *s;
2877 int i, k, x;
2878 struct glyph *glyphs = s->row->glyphs[s->area];
2879 int first = s->first_glyph - glyphs;
2881 k = -1;
2882 x = 0;
2883 for (i = first - 1; i >= 0; --i)
2885 int left, right;
2886 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2887 if (x + right > 0)
2888 k = i;
2889 x -= glyphs[i].pixel_width;
2892 return k;
2896 /* Return the index of the last glyph following glyph string S that is
2897 not overwritten by S because of S's right overhang. Value is -1 if
2898 no such glyph is found. */
2900 static int
2901 x_right_overwritten (s)
2902 struct glyph_string *s;
2904 int k = -1;
2906 if (s->right_overhang)
2908 int x = 0, i;
2909 struct glyph *glyphs = s->row->glyphs[s->area];
2910 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2911 int end = s->row->used[s->area];
2913 for (i = first; i < end && s->right_overhang > x; ++i)
2914 x += glyphs[i].pixel_width;
2916 k = i;
2919 return k;
2923 /* Return the index of the last glyph following glyph string S that
2924 overwrites S because of its left overhang. Value is negative
2925 if no such glyph is found. */
2927 static int
2928 x_right_overwriting (s)
2929 struct glyph_string *s;
2931 int i, k, x;
2932 int end = s->row->used[s->area];
2933 struct glyph *glyphs = s->row->glyphs[s->area];
2934 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2936 k = -1;
2937 x = 0;
2938 for (i = first; i < end; ++i)
2940 int left, right;
2941 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2942 if (x - left < 0)
2943 k = i;
2944 x += glyphs[i].pixel_width;
2947 return k;
2951 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
2953 static INLINE void
2954 x_clear_glyph_string_rect (s, x, y, w, h)
2955 struct glyph_string *s;
2956 int x, y, w, h;
2958 XGCValues xgcv;
2959 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
2960 XSetForeground (s->display, s->gc, xgcv.background);
2961 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
2962 XSetForeground (s->display, s->gc, xgcv.foreground);
2966 /* Draw the background of glyph_string S. If S->background_filled_p
2967 is non-zero don't draw it. FORCE_P non-zero means draw the
2968 background even if it wouldn't be drawn normally. This is used
2969 when a string preceding S draws into the background of S, or S
2970 contains the first component of a composition. */
2972 static void
2973 x_draw_glyph_string_background (s, force_p)
2974 struct glyph_string *s;
2975 int force_p;
2977 /* Nothing to do if background has already been drawn or if it
2978 shouldn't be drawn in the first place. */
2979 if (!s->background_filled_p)
2981 if (s->stippled_p)
2983 /* Fill background with a stipple pattern. */
2984 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
2985 XFillRectangle (s->display, s->window, s->gc, s->x,
2986 s->y + s->face->box_line_width,
2987 s->background_width,
2988 s->height - 2 * s->face->box_line_width);
2989 XSetFillStyle (s->display, s->gc, FillSolid);
2990 s->background_filled_p = 1;
2992 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
2993 || s->font_not_found_p
2994 || s->extends_to_end_of_line_p
2995 || force_p)
2997 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
2998 s->background_width,
2999 s->height - 2 * s->face->box_line_width);
3000 s->background_filled_p = 1;
3006 /* Draw the foreground of glyph string S. */
3008 static void
3009 x_draw_glyph_string_foreground (s)
3010 struct glyph_string *s;
3012 int i, x;
3014 /* If first glyph of S has a left box line, start drawing the text
3015 of S to the right of that box line. */
3016 if (s->face->box != FACE_NO_BOX
3017 && s->first_glyph->left_box_line_p)
3018 x = s->x + s->face->box_line_width;
3019 else
3020 x = s->x;
3022 /* Draw characters of S as rectangles if S's font could not be
3023 loaded. */
3024 if (s->font_not_found_p)
3026 for (i = 0; i < s->nchars; ++i)
3028 struct glyph *g = s->first_glyph + i;
3029 XDrawRectangle (s->display, s->window,
3030 s->gc, x, s->y, g->pixel_width - 1,
3031 s->height - 1);
3032 x += g->pixel_width;
3035 else
3037 char *char1b = (char *) s->char2b;
3038 int boff = s->font_info->baseline_offset;
3040 if (s->font_info->vertical_centering)
3041 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3043 /* If we can use 8-bit functions, condense S->char2b. */
3044 if (!s->two_byte_p)
3045 for (i = 0; i < s->nchars; ++i)
3046 char1b[i] = s->char2b[i].byte2;
3048 /* Draw text with XDrawString if background has already been
3049 filled. Otherwise, use XDrawImageString. (Note that
3050 XDrawImageString is usually faster than XDrawString.) Always
3051 use XDrawImageString when drawing the cursor so that there is
3052 no chance that characters under a box cursor are invisible. */
3053 if (s->for_overlaps_p
3054 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3056 /* Draw characters with 16-bit or 8-bit functions. */
3057 if (s->two_byte_p)
3058 XDrawString16 (s->display, s->window, s->gc, x,
3059 s->ybase - boff, s->char2b, s->nchars);
3060 else
3061 XDrawString (s->display, s->window, s->gc, x,
3062 s->ybase - boff, char1b, s->nchars);
3064 else
3066 if (s->two_byte_p)
3067 XDrawImageString16 (s->display, s->window, s->gc, x,
3068 s->ybase - boff, s->char2b, s->nchars);
3069 else
3070 XDrawImageString (s->display, s->window, s->gc, x,
3071 s->ybase - boff, char1b, s->nchars);
3076 /* Draw the foreground of composite glyph string S. */
3078 static void
3079 x_draw_composite_glyph_string_foreground (s)
3080 struct glyph_string *s;
3082 int i, x;
3084 /* If first glyph of S has a left box line, start drawing the text
3085 of S to the right of that box line. */
3086 if (s->face->box != FACE_NO_BOX
3087 && s->first_glyph->left_box_line_p)
3088 x = s->x + s->face->box_line_width;
3089 else
3090 x = s->x;
3092 /* S is a glyph string for a composition. S->gidx is the index of
3093 the first character drawn for glyphs of this composition.
3094 S->gidx == 0 means we are drawing the very first character of
3095 this composition. */
3097 /* Draw a rectangle for the composition if the font for the very
3098 first character of the composition could not be loaded. */
3099 if (s->font_not_found_p)
3101 if (s->gidx == 0)
3102 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3103 s->width - 1, s->height - 1);
3105 else
3107 for (i = 0; i < s->nchars; i++, ++s->gidx)
3108 XDrawString16 (s->display, s->window, s->gc,
3109 x + s->cmp->offsets[s->gidx * 2],
3110 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3111 s->char2b + i, 1);
3116 #ifdef USE_X_TOOLKIT
3118 /* Allocate the color COLOR->pixel on the screen and display of
3119 widget WIDGET in colormap CMAP. If an exact match cannot be
3120 allocated, try the nearest color available. Value is non-zero
3121 if successful. This is called from lwlib. */
3124 x_alloc_nearest_color_for_widget (widget, cmap, color)
3125 Widget widget;
3126 Colormap cmap;
3127 XColor *color;
3129 struct frame *f;
3130 struct x_display_info *dpyinfo;
3131 Lisp_Object tail;
3133 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3135 /* Find the top-level shell of the widget. Note that this function
3136 can be called when the widget is not yet realized, so XtWindow
3137 (widget) == 0. That's the reason we can't simply use
3138 x_any_window_to_frame. */
3139 while (!XtIsTopLevelShell (widget))
3140 widget = XtParent (widget);
3142 /* Look for a frame with that top-level widget. Allocate the color
3143 on that frame to get the right gamma correction value. */
3144 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3145 if (GC_FRAMEP (XCAR (tail))
3146 && (f = XFRAME (XCAR (tail)),
3147 (f->output_data.nothing != 1
3148 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3149 && f->output_data.x->widget == widget)
3150 return x_alloc_nearest_color (f, cmap, color);
3152 abort ();
3155 #endif /* USE_X_TOOLKIT */
3158 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3159 CMAP. If an exact match can't be allocated, try the nearest color
3160 available. Value is non-zero if successful. Set *COLOR to the
3161 color allocated. */
3164 x_alloc_nearest_color (f, cmap, color)
3165 struct frame *f;
3166 Colormap cmap;
3167 XColor *color;
3169 Display *display = FRAME_X_DISPLAY (f);
3170 Screen *screen = FRAME_X_SCREEN (f);
3171 int rc;
3173 gamma_correct (f, color);
3174 rc = XAllocColor (display, cmap, color);
3175 if (rc == 0)
3177 /* If we got to this point, the colormap is full, so we're going
3178 to try to get the next closest color. The algorithm used is
3179 a least-squares matching, which is what X uses for closest
3180 color matching with StaticColor visuals. */
3181 int nearest, i;
3182 unsigned long nearest_delta = ~0;
3183 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3184 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3186 for (i = 0; i < ncells; ++i)
3187 cells[i].pixel = i;
3188 XQueryColors (display, cmap, cells, ncells);
3190 for (nearest = i = 0; i < ncells; ++i)
3192 long dred = (color->red >> 8) - (cells[i].red >> 8);
3193 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3194 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3195 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3197 if (delta < nearest_delta)
3199 nearest = i;
3200 nearest_delta = delta;
3204 color->red = cells[nearest].red;
3205 color->green = cells[nearest].green;
3206 color->blue = cells[nearest].blue;
3207 rc = XAllocColor (display, cmap, color);
3210 return rc;
3214 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3215 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3216 If this produces the same color as PIXEL, try a color where all RGB
3217 values have DELTA added. Return the allocated color in *PIXEL.
3218 DISPLAY is the X display, CMAP is the colormap to operate on.
3219 Value is non-zero if successful. */
3221 static int
3222 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3223 struct frame *f;
3224 Display *display;
3225 Colormap cmap;
3226 unsigned long *pixel;
3227 double factor;
3228 int delta;
3230 XColor color, new;
3231 int success_p;
3233 /* Get RGB color values. */
3234 color.pixel = *pixel;
3235 XQueryColor (display, cmap, &color);
3237 /* Change RGB values by specified FACTOR. Avoid overflow! */
3238 xassert (factor >= 0);
3239 new.red = min (0xffff, factor * color.red);
3240 new.green = min (0xffff, factor * color.green);
3241 new.blue = min (0xffff, factor * color.blue);
3243 /* Try to allocate the color. */
3244 success_p = x_alloc_nearest_color (f, cmap, &new);
3245 if (success_p)
3247 if (new.pixel == *pixel)
3249 /* If we end up with the same color as before, try adding
3250 delta to the RGB values. */
3251 x_free_colors (f, &new.pixel, 1);
3253 new.red = min (0xffff, delta + color.red);
3254 new.green = min (0xffff, delta + color.green);
3255 new.blue = min (0xffff, delta + color.blue);
3256 success_p = x_alloc_nearest_color (f, cmap, &new);
3258 else
3259 success_p = 1;
3260 *pixel = new.pixel;
3263 return success_p;
3267 /* Set up the foreground color for drawing relief lines of glyph
3268 string S. RELIEF is a pointer to a struct relief containing the GC
3269 with which lines will be drawn. Use a color that is FACTOR or
3270 DELTA lighter or darker than the relief's background which is found
3271 in S->f->output_data.x->relief_background. If such a color cannot
3272 be allocated, use DEFAULT_PIXEL, instead. */
3274 static void
3275 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3276 struct frame *f;
3277 struct relief *relief;
3278 double factor;
3279 int delta;
3280 unsigned long default_pixel;
3282 XGCValues xgcv;
3283 struct x_output *di = f->output_data.x;
3284 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3285 unsigned long pixel;
3286 unsigned long background = di->relief_background;
3287 Colormap cmap = FRAME_X_COLORMAP (f);
3288 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3289 Display *dpy = FRAME_X_DISPLAY (f);
3291 xgcv.graphics_exposures = False;
3292 xgcv.line_width = 1;
3294 /* Free previously allocated color. The color cell will be reused
3295 when it has been freed as many times as it was allocated, so this
3296 doesn't affect faces using the same colors. */
3297 if (relief->gc
3298 && relief->allocated_p)
3300 /* If display has an immutable color map, freeing colors is not
3301 necessary and some servers don't allow it. So don't do it. */
3302 x_free_colors (f, &relief->pixel, 1);
3303 relief->allocated_p = 0;
3306 /* Allocate new color. */
3307 xgcv.foreground = default_pixel;
3308 pixel = background;
3309 if (dpyinfo->n_planes != 1
3310 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3312 relief->allocated_p = 1;
3313 xgcv.foreground = relief->pixel = pixel;
3316 if (relief->gc == 0)
3318 xgcv.stipple = dpyinfo->gray;
3319 mask |= GCStipple;
3320 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3322 else
3323 XChangeGC (dpy, relief->gc, mask, &xgcv);
3327 /* Set up colors for the relief lines around glyph string S. */
3329 static void
3330 x_setup_relief_colors (s)
3331 struct glyph_string *s;
3333 struct x_output *di = s->f->output_data.x;
3334 unsigned long color;
3336 if (s->face->use_box_color_for_shadows_p)
3337 color = s->face->box_color;
3338 else
3340 XGCValues xgcv;
3342 /* Get the background color of the face. */
3343 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3344 color = xgcv.background;
3347 if (di->white_relief.gc == 0
3348 || color != di->relief_background)
3350 di->relief_background = color;
3351 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3352 WHITE_PIX_DEFAULT (s->f));
3353 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3354 BLACK_PIX_DEFAULT (s->f));
3359 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3360 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3361 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3362 relief. LEFT_P non-zero means draw a relief on the left side of
3363 the rectangle. RIGHT_P non-zero means draw a relief on the right
3364 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3365 when drawing. */
3367 static void
3368 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3369 raised_p, left_p, right_p, clip_rect)
3370 struct frame *f;
3371 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3372 XRectangle *clip_rect;
3374 int i;
3375 GC gc;
3377 if (raised_p)
3378 gc = f->output_data.x->white_relief.gc;
3379 else
3380 gc = f->output_data.x->black_relief.gc;
3381 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3383 /* Top. */
3384 for (i = 0; i < width; ++i)
3385 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3386 left_x + i * left_p, top_y + i,
3387 right_x + 1 - i * right_p, top_y + i);
3389 /* Left. */
3390 if (left_p)
3391 for (i = 0; i < width; ++i)
3392 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3393 left_x + i, top_y + i, left_x + i, bottom_y - i);
3395 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3396 if (raised_p)
3397 gc = f->output_data.x->black_relief.gc;
3398 else
3399 gc = f->output_data.x->white_relief.gc;
3400 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3402 /* Bottom. */
3403 for (i = 0; i < width; ++i)
3404 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3405 left_x + i * left_p, bottom_y - i,
3406 right_x + 1 - i * right_p, bottom_y - i);
3408 /* Right. */
3409 if (right_p)
3410 for (i = 0; i < width; ++i)
3411 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3412 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3414 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3418 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3419 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3420 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3421 left side of the rectangle. RIGHT_P non-zero means draw a line
3422 on the right side of the rectangle. CLIP_RECT is the clipping
3423 rectangle to use when drawing. */
3425 static void
3426 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3427 left_p, right_p, clip_rect)
3428 struct glyph_string *s;
3429 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3430 XRectangle *clip_rect;
3432 XGCValues xgcv;
3434 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3435 XSetForeground (s->display, s->gc, s->face->box_color);
3436 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3438 /* Top. */
3439 XFillRectangle (s->display, s->window, s->gc,
3440 left_x, top_y, right_x - left_x, width);
3442 /* Left. */
3443 if (left_p)
3444 XFillRectangle (s->display, s->window, s->gc,
3445 left_x, top_y, width, bottom_y - top_y);
3447 /* Bottom. */
3448 XFillRectangle (s->display, s->window, s->gc,
3449 left_x, bottom_y - width, right_x - left_x, width);
3451 /* Right. */
3452 if (right_p)
3453 XFillRectangle (s->display, s->window, s->gc,
3454 right_x - width, top_y, width, bottom_y - top_y);
3456 XSetForeground (s->display, s->gc, xgcv.foreground);
3457 XSetClipMask (s->display, s->gc, None);
3461 /* Draw a box around glyph string S. */
3463 static void
3464 x_draw_glyph_string_box (s)
3465 struct glyph_string *s;
3467 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3468 int left_p, right_p;
3469 struct glyph *last_glyph;
3470 XRectangle clip_rect;
3472 last_x = window_box_right (s->w, s->area);
3473 if (s->row->full_width_p
3474 && !s->w->pseudo_window_p)
3476 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3477 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3478 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3481 /* The glyph that may have a right box line. */
3482 last_glyph = (s->cmp || s->img
3483 ? s->first_glyph
3484 : s->first_glyph + s->nchars - 1);
3486 width = s->face->box_line_width;
3487 raised_p = s->face->box == FACE_RAISED_BOX;
3488 left_x = s->x;
3489 right_x = ((s->row->full_width_p
3490 ? last_x - 1
3491 : min (last_x, s->x + s->background_width) - 1));
3492 top_y = s->y;
3493 bottom_y = top_y + s->height - 1;
3495 left_p = (s->first_glyph->left_box_line_p
3496 || (s->hl == DRAW_MOUSE_FACE
3497 && (s->prev == NULL
3498 || s->prev->hl != s->hl)));
3499 right_p = (last_glyph->right_box_line_p
3500 || (s->hl == DRAW_MOUSE_FACE
3501 && (s->next == NULL
3502 || s->next->hl != s->hl)));
3504 x_get_glyph_string_clip_rect (s, &clip_rect);
3506 if (s->face->box == FACE_SIMPLE_BOX)
3507 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3508 left_p, right_p, &clip_rect);
3509 else
3511 x_setup_relief_colors (s);
3512 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3513 width, raised_p, left_p, right_p, &clip_rect);
3518 /* Draw foreground of image glyph string S. */
3520 static void
3521 x_draw_image_foreground (s)
3522 struct glyph_string *s;
3524 int x;
3525 int y = s->ybase - IMAGE_ASCENT (s->img);
3527 /* If first glyph of S has a left box line, start drawing it to the
3528 right of that line. */
3529 if (s->face->box != FACE_NO_BOX
3530 && s->first_glyph->left_box_line_p)
3531 x = s->x + s->face->box_line_width;
3532 else
3533 x = s->x;
3535 /* If there is a margin around the image, adjust x- and y-position
3536 by that margin. */
3537 if (s->img->margin)
3539 x += s->img->margin;
3540 y += s->img->margin;
3543 if (s->img->pixmap)
3545 if (s->img->mask)
3547 /* We can't set both a clip mask and use XSetClipRectangles
3548 because the latter also sets a clip mask. We also can't
3549 trust on the shape extension to be available
3550 (XShapeCombineRegion). So, compute the rectangle to draw
3551 manually. */
3552 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3553 | GCFunction);
3554 XGCValues xgcv;
3555 XRectangle clip_rect, image_rect, r;
3557 xgcv.clip_mask = s->img->mask;
3558 xgcv.clip_x_origin = x;
3559 xgcv.clip_y_origin = y;
3560 xgcv.function = GXcopy;
3561 XChangeGC (s->display, s->gc, mask, &xgcv);
3563 x_get_glyph_string_clip_rect (s, &clip_rect);
3564 image_rect.x = x;
3565 image_rect.y = y;
3566 image_rect.width = s->img->width;
3567 image_rect.height = s->img->height;
3568 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3569 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3570 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3572 else
3574 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3575 0, 0, s->img->width, s->img->height, x, y);
3577 /* When the image has a mask, we can expect that at
3578 least part of a mouse highlight or a block cursor will
3579 be visible. If the image doesn't have a mask, make
3580 a block cursor visible by drawing a rectangle around
3581 the image. I believe it's looking better if we do
3582 nothing here for mouse-face. */
3583 if (s->hl == DRAW_CURSOR)
3584 XDrawRectangle (s->display, s->window, s->gc, x, y,
3585 s->img->width - 1, s->img->height - 1);
3588 else
3589 /* Draw a rectangle if image could not be loaded. */
3590 XDrawRectangle (s->display, s->window, s->gc, x, y,
3591 s->img->width - 1, s->img->height - 1);
3595 /* Draw a relief around the image glyph string S. */
3597 static void
3598 x_draw_image_relief (s)
3599 struct glyph_string *s;
3601 int x0, y0, x1, y1, thick, raised_p;
3602 XRectangle r;
3603 int x;
3604 int y = s->ybase - IMAGE_ASCENT (s->img);
3606 /* If first glyph of S has a left box line, start drawing it to the
3607 right of that line. */
3608 if (s->face->box != FACE_NO_BOX
3609 && s->first_glyph->left_box_line_p)
3610 x = s->x + s->face->box_line_width;
3611 else
3612 x = s->x;
3614 /* If there is a margin around the image, adjust x- and y-position
3615 by that margin. */
3616 if (s->img->margin)
3618 x += s->img->margin;
3619 y += s->img->margin;
3622 if (s->hl == DRAW_IMAGE_SUNKEN
3623 || s->hl == DRAW_IMAGE_RAISED)
3625 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3626 raised_p = s->hl == DRAW_IMAGE_RAISED;
3628 else
3630 thick = abs (s->img->relief);
3631 raised_p = s->img->relief > 0;
3634 x0 = x - thick;
3635 y0 = y - thick;
3636 x1 = x + s->img->width + thick - 1;
3637 y1 = y + s->img->height + thick - 1;
3639 x_setup_relief_colors (s);
3640 x_get_glyph_string_clip_rect (s, &r);
3641 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3645 /* Draw the foreground of image glyph string S to PIXMAP. */
3647 static void
3648 x_draw_image_foreground_1 (s, pixmap)
3649 struct glyph_string *s;
3650 Pixmap pixmap;
3652 int x;
3653 int y = s->ybase - s->y - IMAGE_ASCENT (s->img);
3655 /* If first glyph of S has a left box line, start drawing it to the
3656 right of that line. */
3657 if (s->face->box != FACE_NO_BOX
3658 && s->first_glyph->left_box_line_p)
3659 x = s->face->box_line_width;
3660 else
3661 x = 0;
3663 /* If there is a margin around the image, adjust x- and y-position
3664 by that margin. */
3665 if (s->img->margin)
3667 x += s->img->margin;
3668 y += s->img->margin;
3671 if (s->img->pixmap)
3673 if (s->img->mask)
3675 /* We can't set both a clip mask and use XSetClipRectangles
3676 because the latter also sets a clip mask. We also can't
3677 trust on the shape extension to be available
3678 (XShapeCombineRegion). So, compute the rectangle to draw
3679 manually. */
3680 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3681 | GCFunction);
3682 XGCValues xgcv;
3684 xgcv.clip_mask = s->img->mask;
3685 xgcv.clip_x_origin = x;
3686 xgcv.clip_y_origin = y;
3687 xgcv.function = GXcopy;
3688 XChangeGC (s->display, s->gc, mask, &xgcv);
3690 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3691 0, 0, s->img->width, s->img->height, x, y);
3692 XSetClipMask (s->display, s->gc, None);
3694 else
3696 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3697 0, 0, s->img->width, s->img->height, x, y);
3699 /* When the image has a mask, we can expect that at
3700 least part of a mouse highlight or a block cursor will
3701 be visible. If the image doesn't have a mask, make
3702 a block cursor visible by drawing a rectangle around
3703 the image. I believe it's looking better if we do
3704 nothing here for mouse-face. */
3705 if (s->hl == DRAW_CURSOR)
3706 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3707 s->img->width - 1, s->img->height - 1);
3710 else
3711 /* Draw a rectangle if image could not be loaded. */
3712 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3713 s->img->width - 1, s->img->height - 1);
3717 /* Draw part of the background of glyph string S. X, Y, W, and H
3718 give the rectangle to draw. */
3720 static void
3721 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3722 struct glyph_string *s;
3723 int x, y, w, h;
3725 if (s->stippled_p)
3727 /* Fill background with a stipple pattern. */
3728 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3729 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3730 XSetFillStyle (s->display, s->gc, FillSolid);
3732 else
3733 x_clear_glyph_string_rect (s, x, y, w, h);
3737 /* Draw image glyph string S.
3739 s->y
3740 s->x +-------------------------
3741 | s->face->box
3743 | +-------------------------
3744 | | s->img->margin
3746 | | +-------------------
3747 | | | the image
3751 static void
3752 x_draw_image_glyph_string (s)
3753 struct glyph_string *s;
3755 int x, y;
3756 int box_line_width = s->face->box_line_width;
3757 int margin = s->img->margin;
3758 int height;
3759 Pixmap pixmap = None;
3761 height = s->height - 2 * box_line_width;
3763 /* Fill background with face under the image. Do it only if row is
3764 taller than image or if image has a clip mask to reduce
3765 flickering. */
3766 s->stippled_p = s->face->stipple != 0;
3767 if (height > s->img->height
3768 || margin
3769 || s->img->mask
3770 || s->img->pixmap == 0
3771 || s->width != s->background_width)
3773 if (box_line_width && s->first_glyph->left_box_line_p)
3774 x = s->x + box_line_width;
3775 else
3776 x = s->x;
3778 y = s->y + box_line_width;
3780 if (s->img->mask)
3782 /* Create a pixmap as large as the glyph string Fill it with
3783 the background color. Copy the image to it, using its
3784 mask. Copy the temporary pixmap to the display. */
3785 Screen *screen = FRAME_X_SCREEN (s->f);
3786 int depth = DefaultDepthOfScreen (screen);
3788 /* Create a pixmap as large as the glyph string. */
3789 pixmap = XCreatePixmap (s->display, s->window,
3790 s->background_width,
3791 s->height, depth);
3793 /* Don't clip in the following because we're working on the
3794 pixmap. */
3795 XSetClipMask (s->display, s->gc, None);
3797 /* Fill the pixmap with the background color/stipple. */
3798 if (s->stippled_p)
3800 /* Fill background with a stipple pattern. */
3801 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3802 XFillRectangle (s->display, pixmap, s->gc,
3803 0, 0, s->background_width, s->height);
3804 XSetFillStyle (s->display, s->gc, FillSolid);
3806 else
3808 XGCValues xgcv;
3809 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3810 &xgcv);
3811 XSetForeground (s->display, s->gc, xgcv.background);
3812 XFillRectangle (s->display, pixmap, s->gc,
3813 0, 0, s->background_width, s->height);
3814 XSetForeground (s->display, s->gc, xgcv.foreground);
3817 else
3818 /* Implementation idea: Is it possible to construct a mask?
3819 We could look at the color at the margins of the image, and
3820 say that this color is probably the background color of the
3821 image. */
3822 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3824 s->background_filled_p = 1;
3827 /* Draw the foreground. */
3828 if (pixmap != None)
3830 x_draw_image_foreground_1 (s, pixmap);
3831 x_set_glyph_string_clipping (s);
3832 XCopyArea (s->display, pixmap, s->window, s->gc,
3833 0, 0, s->background_width, s->height, s->x, s->y);
3834 XFreePixmap (s->display, pixmap);
3836 else
3837 x_draw_image_foreground (s);
3839 /* If we must draw a relief around the image, do it. */
3840 if (s->img->relief
3841 || s->hl == DRAW_IMAGE_RAISED
3842 || s->hl == DRAW_IMAGE_SUNKEN)
3843 x_draw_image_relief (s);
3847 /* Draw stretch glyph string S. */
3849 static void
3850 x_draw_stretch_glyph_string (s)
3851 struct glyph_string *s;
3853 xassert (s->first_glyph->type == STRETCH_GLYPH);
3854 s->stippled_p = s->face->stipple != 0;
3856 if (s->hl == DRAW_CURSOR
3857 && !x_stretch_cursor_p)
3859 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3860 as wide as the stretch glyph. */
3861 int width = min (CANON_X_UNIT (s->f), s->background_width);
3863 /* Draw cursor. */
3864 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
3866 /* Clear rest using the GC of the original non-cursor face. */
3867 if (width < s->background_width)
3869 GC gc = s->face->gc;
3870 int x = s->x + width, y = s->y;
3871 int w = s->background_width - width, h = s->height;
3872 XRectangle r;
3874 x_get_glyph_string_clip_rect (s, &r);
3875 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
3877 if (s->face->stipple)
3879 /* Fill background with a stipple pattern. */
3880 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3881 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3882 XSetFillStyle (s->display, gc, FillSolid);
3884 else
3886 XGCValues xgcv;
3887 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3888 XSetForeground (s->display, gc, xgcv.background);
3889 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3890 XSetForeground (s->display, gc, xgcv.foreground);
3894 else
3895 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
3896 s->height);
3898 s->background_filled_p = 1;
3902 /* Draw glyph string S. */
3904 static void
3905 x_draw_glyph_string (s)
3906 struct glyph_string *s;
3908 /* If S draws into the background of its successor, draw the
3909 background of the successor first so that S can draw into it.
3910 This makes S->next use XDrawString instead of XDrawImageString. */
3911 if (s->next && s->right_overhang && !s->for_overlaps_p)
3913 xassert (s->next->img == NULL);
3914 x_set_glyph_string_gc (s->next);
3915 x_set_glyph_string_clipping (s->next);
3916 x_draw_glyph_string_background (s->next, 1);
3919 /* Set up S->gc, set clipping and draw S. */
3920 x_set_glyph_string_gc (s);
3921 x_set_glyph_string_clipping (s);
3923 switch (s->first_glyph->type)
3925 case IMAGE_GLYPH:
3926 x_draw_image_glyph_string (s);
3927 break;
3929 case STRETCH_GLYPH:
3930 x_draw_stretch_glyph_string (s);
3931 break;
3933 case CHAR_GLYPH:
3934 if (s->for_overlaps_p)
3935 s->background_filled_p = 1;
3936 else
3937 x_draw_glyph_string_background (s, 0);
3938 x_draw_glyph_string_foreground (s);
3939 break;
3941 case COMPOSITE_GLYPH:
3942 if (s->for_overlaps_p || s->gidx > 0)
3943 s->background_filled_p = 1;
3944 else
3945 x_draw_glyph_string_background (s, 1);
3946 x_draw_composite_glyph_string_foreground (s);
3947 break;
3949 default:
3950 abort ();
3953 if (!s->for_overlaps_p)
3955 /* Draw underline. */
3956 if (s->face->underline_p)
3958 unsigned long dy, h;
3960 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
3961 h = 1;
3962 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
3963 dy = s->height - h;
3965 if (s->face->underline_defaulted_p)
3966 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3967 s->width, h);
3968 else
3970 XGCValues xgcv;
3971 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3972 XSetForeground (s->display, s->gc, s->face->underline_color);
3973 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3974 s->width, h);
3975 XSetForeground (s->display, s->gc, xgcv.foreground);
3979 /* Draw overline. */
3980 if (s->face->overline_p)
3982 unsigned long dy = 0, h = 1;
3984 if (s->face->overline_color_defaulted_p)
3985 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3986 s->width, h);
3987 else
3989 XGCValues xgcv;
3990 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3991 XSetForeground (s->display, s->gc, s->face->overline_color);
3992 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
3993 s->width, h);
3994 XSetForeground (s->display, s->gc, xgcv.foreground);
3998 /* Draw strike-through. */
3999 if (s->face->strike_through_p)
4001 unsigned long h = 1;
4002 unsigned long dy = (s->height - h) / 2;
4004 if (s->face->strike_through_color_defaulted_p)
4005 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4006 s->width, h);
4007 else
4009 XGCValues xgcv;
4010 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4011 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4012 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4013 s->width, h);
4014 XSetForeground (s->display, s->gc, xgcv.foreground);
4018 /* Draw relief. */
4019 if (s->face->box != FACE_NO_BOX)
4020 x_draw_glyph_string_box (s);
4023 /* Reset clipping. */
4024 XSetClipMask (s->display, s->gc, None);
4028 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4029 struct face **, int));
4032 /* Load glyph string S with a composition components specified by S->cmp.
4033 FACES is an array of faces for all components of this composition.
4034 S->gidx is the index of the first component for S.
4035 OVERLAPS_P non-zero means S should draw the foreground only, and
4036 use its lines physical height for clipping.
4038 Value is the index of a component not in S. */
4040 static int
4041 x_fill_composite_glyph_string (s, faces, overlaps_p)
4042 struct glyph_string *s;
4043 struct face **faces;
4044 int overlaps_p;
4046 int i;
4048 xassert (s);
4050 s->for_overlaps_p = overlaps_p;
4052 s->face = faces[s->gidx];
4053 s->font = s->face->font;
4054 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4056 /* For all glyphs of this composition, starting at the offset
4057 S->gidx, until we reach the end of the definition or encounter a
4058 glyph that requires the different face, add it to S. */
4059 ++s->nchars;
4060 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4061 ++s->nchars;
4063 /* All glyph strings for the same composition has the same width,
4064 i.e. the width set for the first component of the composition. */
4066 s->width = s->first_glyph->pixel_width;
4068 /* If the specified font could not be loaded, use the frame's
4069 default font, but record the fact that we couldn't load it in
4070 the glyph string so that we can draw rectangles for the
4071 characters of the glyph string. */
4072 if (s->font == NULL)
4074 s->font_not_found_p = 1;
4075 s->font = FRAME_FONT (s->f);
4078 /* Adjust base line for subscript/superscript text. */
4079 s->ybase += s->first_glyph->voffset;
4081 xassert (s->face && s->face->gc);
4083 /* This glyph string must always be drawn with 16-bit functions. */
4084 s->two_byte_p = 1;
4086 return s->gidx + s->nchars;
4090 /* Load glyph string S with a sequence characters.
4091 FACE_ID is the face id of the string. START is the index of the
4092 first glyph to consider, END is the index of the last + 1.
4093 OVERLAPS_P non-zero means S should draw the foreground only, and
4094 use its lines physical height for clipping.
4096 Value is the index of the first glyph not in S. */
4098 static int
4099 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4100 struct glyph_string *s;
4101 int face_id;
4102 int start, end, overlaps_p;
4104 struct glyph *glyph, *last;
4105 int voffset;
4106 int glyph_not_available_p;
4108 xassert (s->f == XFRAME (s->w->frame));
4109 xassert (s->nchars == 0);
4110 xassert (start >= 0 && end > start);
4112 s->for_overlaps_p = overlaps_p,
4113 glyph = s->row->glyphs[s->area] + start;
4114 last = s->row->glyphs[s->area] + end;
4115 voffset = glyph->voffset;
4117 glyph_not_available_p = glyph->glyph_not_available_p;
4119 while (glyph < last
4120 && glyph->type == CHAR_GLYPH
4121 && glyph->voffset == voffset
4122 /* Same face id implies same font, nowadays. */
4123 && glyph->face_id == face_id
4124 && glyph->glyph_not_available_p == glyph_not_available_p)
4126 int two_byte_p;
4128 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4129 s->char2b + s->nchars,
4130 &two_byte_p);
4131 s->two_byte_p = two_byte_p;
4132 ++s->nchars;
4133 xassert (s->nchars <= end - start);
4134 s->width += glyph->pixel_width;
4135 ++glyph;
4138 s->font = s->face->font;
4139 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4141 /* If the specified font could not be loaded, use the frame's font,
4142 but record the fact that we couldn't load it in
4143 S->font_not_found_p so that we can draw rectangles for the
4144 characters of the glyph string. */
4145 if (s->font == NULL || glyph_not_available_p)
4147 s->font_not_found_p = 1;
4148 s->font = FRAME_FONT (s->f);
4151 /* Adjust base line for subscript/superscript text. */
4152 s->ybase += voffset;
4154 xassert (s->face && s->face->gc);
4155 return glyph - s->row->glyphs[s->area];
4159 /* Fill glyph string S from image glyph S->first_glyph. */
4161 static void
4162 x_fill_image_glyph_string (s)
4163 struct glyph_string *s;
4165 xassert (s->first_glyph->type == IMAGE_GLYPH);
4166 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4167 xassert (s->img);
4168 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4169 s->font = s->face->font;
4170 s->width = s->first_glyph->pixel_width;
4172 /* Adjust base line for subscript/superscript text. */
4173 s->ybase += s->first_glyph->voffset;
4177 /* Fill glyph string S from stretch glyph S->first_glyph. */
4179 static void
4180 x_fill_stretch_glyph_string (s)
4181 struct glyph_string *s;
4183 xassert (s->first_glyph->type == STRETCH_GLYPH);
4184 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4185 s->font = s->face->font;
4186 s->width = s->first_glyph->pixel_width;
4188 /* Adjust base line for subscript/superscript text. */
4189 s->ybase += s->first_glyph->voffset;
4193 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4194 of XChar2b structures for S; it can't be allocated in
4195 x_init_glyph_string because it must be allocated via `alloca'. W
4196 is the window on which S is drawn. ROW and AREA are the glyph row
4197 and area within the row from which S is constructed. START is the
4198 index of the first glyph structure covered by S. HL is a
4199 face-override for drawing S. */
4201 static void
4202 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4203 struct glyph_string *s;
4204 XChar2b *char2b;
4205 struct window *w;
4206 struct glyph_row *row;
4207 enum glyph_row_area area;
4208 int start;
4209 enum draw_glyphs_face hl;
4211 bzero (s, sizeof *s);
4212 s->w = w;
4213 s->f = XFRAME (w->frame);
4214 s->display = FRAME_X_DISPLAY (s->f);
4215 s->window = FRAME_X_WINDOW (s->f);
4216 s->char2b = char2b;
4217 s->hl = hl;
4218 s->row = row;
4219 s->area = area;
4220 s->first_glyph = row->glyphs[area] + start;
4221 s->height = row->height;
4222 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4224 /* Display the internal border below the tool-bar window. */
4225 if (s->w == XWINDOW (s->f->tool_bar_window))
4226 s->y -= s->f->output_data.x->internal_border_width;
4228 s->ybase = s->y + row->ascent;
4232 /* Set background width of glyph string S. START is the index of the
4233 first glyph following S. LAST_X is the right-most x-position + 1
4234 in the drawing area. */
4236 static INLINE void
4237 x_set_glyph_string_background_width (s, start, last_x)
4238 struct glyph_string *s;
4239 int start;
4240 int last_x;
4242 /* If the face of this glyph string has to be drawn to the end of
4243 the drawing area, set S->extends_to_end_of_line_p. */
4244 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4246 if (start == s->row->used[s->area]
4247 && s->hl == DRAW_NORMAL_TEXT
4248 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4249 || s->face->background != default_face->background
4250 || s->face->stipple != default_face->stipple))
4251 s->extends_to_end_of_line_p = 1;
4253 /* If S extends its face to the end of the line, set its
4254 background_width to the distance to the right edge of the drawing
4255 area. */
4256 if (s->extends_to_end_of_line_p)
4257 s->background_width = last_x - s->x + 1;
4258 else
4259 s->background_width = s->width;
4263 /* Add a glyph string for a stretch glyph to the list of strings
4264 between HEAD and TAIL. START is the index of the stretch glyph in
4265 row area AREA of glyph row ROW. END is the index of the last glyph
4266 in that glyph row area. X is the current output position assigned
4267 to the new glyph string constructed. HL overrides that face of the
4268 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4269 is the right-most x-position of the drawing area. */
4271 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4272 and below -- keep them on one line. */
4273 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4274 do \
4276 s = (struct glyph_string *) alloca (sizeof *s); \
4277 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4278 x_fill_stretch_glyph_string (s); \
4279 x_append_glyph_string (&HEAD, &TAIL, s); \
4280 ++START; \
4281 s->x = (X); \
4283 while (0)
4286 /* Add a glyph string for an image glyph to the list of strings
4287 between HEAD and TAIL. START is the index of the image glyph in
4288 row area AREA of glyph row ROW. END is the index of the last glyph
4289 in that glyph row area. X is the current output position assigned
4290 to the new glyph string constructed. HL overrides that face of the
4291 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4292 is the right-most x-position of the drawing area. */
4294 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4295 do \
4297 s = (struct glyph_string *) alloca (sizeof *s); \
4298 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4299 x_fill_image_glyph_string (s); \
4300 x_append_glyph_string (&HEAD, &TAIL, s); \
4301 ++START; \
4302 s->x = (X); \
4304 while (0)
4307 /* Add a glyph string for a sequence of character glyphs to the list
4308 of strings between HEAD and TAIL. START is the index of the first
4309 glyph in row area AREA of glyph row ROW that is part of the new
4310 glyph string. END is the index of the last glyph in that glyph row
4311 area. X is the current output position assigned to the new glyph
4312 string constructed. HL overrides that face of the glyph; e.g. it
4313 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4314 right-most x-position of the drawing area. */
4316 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4317 do \
4319 int c, charset, face_id; \
4320 XChar2b *char2b; \
4322 c = (ROW)->glyphs[AREA][START].u.ch; \
4323 face_id = (ROW)->glyphs[AREA][START].face_id; \
4325 s = (struct glyph_string *) alloca (sizeof *s); \
4326 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4327 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4328 x_append_glyph_string (&HEAD, &TAIL, s); \
4329 s->x = (X); \
4330 START = x_fill_glyph_string (s, face_id, START, END, \
4331 OVERLAPS_P); \
4333 while (0)
4336 /* Add a glyph string for a composite sequence to the list of strings
4337 between HEAD and TAIL. START is the index of the first glyph in
4338 row area AREA of glyph row ROW that is part of the new glyph
4339 string. END is the index of the last glyph in that glyph row area.
4340 X is the current output position assigned to the new glyph string
4341 constructed. HL overrides that face of the glyph; e.g. it is
4342 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4343 x-position of the drawing area. */
4345 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4346 do { \
4347 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4348 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4349 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4350 struct composition *cmp = composition_table[cmp_id]; \
4351 int glyph_len = cmp->glyph_len; \
4352 XChar2b *char2b; \
4353 struct face **faces; \
4354 struct glyph_string *first_s = NULL; \
4355 int n; \
4357 base_face = base_face->ascii_face; \
4358 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4359 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4360 /* At first, fill in `char2b' and `faces'. */ \
4361 for (n = 0; n < glyph_len; n++) \
4363 int c = COMPOSITION_GLYPH (cmp, n); \
4364 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4365 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4366 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4367 this_face_id, char2b + n, 1); \
4370 /* Make glyph_strings for each glyph sequence that is drawable by \
4371 the same face, and append them to HEAD/TAIL. */ \
4372 for (n = 0; n < cmp->glyph_len;) \
4374 s = (struct glyph_string *) alloca (sizeof *s); \
4375 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4376 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4377 s->cmp = cmp; \
4378 s->gidx = n; \
4379 s->x = (X); \
4381 if (n == 0) \
4382 first_s = s; \
4384 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4387 ++START; \
4388 s = first_s; \
4389 } while (0)
4392 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4393 of AREA of glyph row ROW on window W between indices START and END.
4394 HL overrides the face for drawing glyph strings, e.g. it is
4395 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4396 x-positions of the drawing area.
4398 This is an ugly monster macro construct because we must use alloca
4399 to allocate glyph strings (because x_draw_glyphs can be called
4400 asynchronously). */
4402 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4403 do \
4405 HEAD = TAIL = NULL; \
4406 while (START < END) \
4408 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4409 switch (first_glyph->type) \
4411 case CHAR_GLYPH: \
4412 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4413 TAIL, HL, X, LAST_X, \
4414 OVERLAPS_P); \
4415 break; \
4417 case COMPOSITE_GLYPH: \
4418 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4419 HEAD, TAIL, HL, X, LAST_X,\
4420 OVERLAPS_P); \
4421 break; \
4423 case STRETCH_GLYPH: \
4424 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4425 HEAD, TAIL, HL, X, LAST_X); \
4426 break; \
4428 case IMAGE_GLYPH: \
4429 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4430 TAIL, HL, X, LAST_X); \
4431 break; \
4433 default: \
4434 abort (); \
4437 x_set_glyph_string_background_width (s, START, LAST_X); \
4438 (X) += s->width; \
4441 while (0)
4444 /* Draw glyphs between START and END in AREA of ROW on window W,
4445 starting at x-position X. X is relative to AREA in W. HL is a
4446 face-override with the following meaning:
4448 DRAW_NORMAL_TEXT draw normally
4449 DRAW_CURSOR draw in cursor face
4450 DRAW_MOUSE_FACE draw in mouse face.
4451 DRAW_INVERSE_VIDEO draw in mode line face
4452 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4453 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4455 If REAL_START is non-null, return in *REAL_START the real starting
4456 position for display. This can be different from START in case
4457 overlapping glyphs must be displayed. If REAL_END is non-null,
4458 return in *REAL_END the real end position for display. This can be
4459 different from END in case overlapping glyphs must be displayed.
4461 If OVERLAPS_P is non-zero, draw only the foreground of characters
4462 and clip to the physical height of ROW.
4464 Value is the x-position reached, relative to AREA of W. */
4466 static int
4467 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4468 overlaps_p)
4469 struct window *w;
4470 int x;
4471 struct glyph_row *row;
4472 enum glyph_row_area area;
4473 int start, end;
4474 enum draw_glyphs_face hl;
4475 int *real_start, *real_end;
4476 int overlaps_p;
4478 struct glyph_string *head, *tail;
4479 struct glyph_string *s;
4480 int last_x, area_width;
4481 int x_reached;
4482 int i, j;
4484 /* Let's rather be paranoid than getting a SEGV. */
4485 start = max (0, start);
4486 end = min (end, row->used[area]);
4487 if (real_start)
4488 *real_start = start;
4489 if (real_end)
4490 *real_end = end;
4492 /* Translate X to frame coordinates. Set last_x to the right
4493 end of the drawing area. */
4494 if (row->full_width_p)
4496 /* X is relative to the left edge of W, without scroll bars
4497 or flag areas. */
4498 struct frame *f = XFRAME (w->frame);
4499 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4500 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4502 x += window_left_x;
4503 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4504 last_x = window_left_x + area_width;
4506 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4508 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4509 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4510 last_x += width;
4511 else
4512 x -= width;
4515 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4516 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4518 else
4520 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4521 area_width = window_box_width (w, area);
4522 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4525 /* Build a doubly-linked list of glyph_string structures between
4526 head and tail from what we have to draw. Note that the macro
4527 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4528 the reason we use a separate variable `i'. */
4529 i = start;
4530 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4531 overlaps_p);
4532 if (tail)
4533 x_reached = tail->x + tail->background_width;
4534 else
4535 x_reached = x;
4537 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4538 the row, redraw some glyphs in front or following the glyph
4539 strings built above. */
4540 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4542 int dummy_x = 0;
4543 struct glyph_string *h, *t;
4545 /* Compute overhangs for all glyph strings. */
4546 for (s = head; s; s = s->next)
4547 x_compute_glyph_string_overhangs (s);
4549 /* Prepend glyph strings for glyphs in front of the first glyph
4550 string that are overwritten because of the first glyph
4551 string's left overhang. The background of all strings
4552 prepended must be drawn because the first glyph string
4553 draws over it. */
4554 i = x_left_overwritten (head);
4555 if (i >= 0)
4557 j = i;
4558 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4559 DRAW_NORMAL_TEXT, dummy_x, last_x,
4560 overlaps_p);
4561 start = i;
4562 if (real_start)
4563 *real_start = start;
4564 x_compute_overhangs_and_x (t, head->x, 1);
4565 x_prepend_glyph_string_lists (&head, &tail, h, t);
4568 /* Prepend glyph strings for glyphs in front of the first glyph
4569 string that overwrite that glyph string because of their
4570 right overhang. For these strings, only the foreground must
4571 be drawn, because it draws over the glyph string at `head'.
4572 The background must not be drawn because this would overwrite
4573 right overhangs of preceding glyphs for which no glyph
4574 strings exist. */
4575 i = x_left_overwriting (head);
4576 if (i >= 0)
4578 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4579 DRAW_NORMAL_TEXT, dummy_x, last_x,
4580 overlaps_p);
4581 for (s = h; s; s = s->next)
4582 s->background_filled_p = 1;
4583 if (real_start)
4584 *real_start = i;
4585 x_compute_overhangs_and_x (t, head->x, 1);
4586 x_prepend_glyph_string_lists (&head, &tail, h, t);
4589 /* Append glyphs strings for glyphs following the last glyph
4590 string tail that are overwritten by tail. The background of
4591 these strings has to be drawn because tail's foreground draws
4592 over it. */
4593 i = x_right_overwritten (tail);
4594 if (i >= 0)
4596 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4597 DRAW_NORMAL_TEXT, x, last_x,
4598 overlaps_p);
4599 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4600 x_append_glyph_string_lists (&head, &tail, h, t);
4601 if (real_end)
4602 *real_end = i;
4605 /* Append glyph strings for glyphs following the last glyph
4606 string tail that overwrite tail. The foreground of such
4607 glyphs has to be drawn because it writes into the background
4608 of tail. The background must not be drawn because it could
4609 paint over the foreground of following glyphs. */
4610 i = x_right_overwriting (tail);
4611 if (i >= 0)
4613 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4614 DRAW_NORMAL_TEXT, x, last_x,
4615 overlaps_p);
4616 for (s = h; s; s = s->next)
4617 s->background_filled_p = 1;
4618 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4619 x_append_glyph_string_lists (&head, &tail, h, t);
4620 if (real_end)
4621 *real_end = i;
4625 /* Draw all strings. */
4626 for (s = head; s; s = s->next)
4627 x_draw_glyph_string (s);
4629 /* Value is the x-position up to which drawn, relative to AREA of W.
4630 This doesn't include parts drawn because of overhangs. */
4631 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4632 if (!row->full_width_p)
4634 if (area > LEFT_MARGIN_AREA)
4635 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4636 if (area > TEXT_AREA)
4637 x_reached -= window_box_width (w, TEXT_AREA);
4639 return x_reached;
4643 /* Fix the display of area AREA of overlapping row ROW in window W. */
4645 static void
4646 x_fix_overlapping_area (w, row, area)
4647 struct window *w;
4648 struct glyph_row *row;
4649 enum glyph_row_area area;
4651 int i, x;
4653 BLOCK_INPUT;
4655 if (area == LEFT_MARGIN_AREA)
4656 x = 0;
4657 else if (area == TEXT_AREA)
4658 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4659 else
4660 x = (window_box_width (w, LEFT_MARGIN_AREA)
4661 + window_box_width (w, TEXT_AREA));
4663 for (i = 0; i < row->used[area];)
4665 if (row->glyphs[area][i].overlaps_vertically_p)
4667 int start = i, start_x = x;
4671 x += row->glyphs[area][i].pixel_width;
4672 ++i;
4674 while (i < row->used[area]
4675 && row->glyphs[area][i].overlaps_vertically_p);
4677 x_draw_glyphs (w, start_x, row, area, start, i,
4678 (row->inverse_p
4679 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4680 NULL, NULL, 1);
4682 else
4684 x += row->glyphs[area][i].pixel_width;
4685 ++i;
4689 UNBLOCK_INPUT;
4693 /* Output LEN glyphs starting at START at the nominal cursor position.
4694 Advance the nominal cursor over the text. The global variable
4695 updated_window contains the window being updated, updated_row is
4696 the glyph row being updated, and updated_area is the area of that
4697 row being updated. */
4699 static void
4700 x_write_glyphs (start, len)
4701 struct glyph *start;
4702 int len;
4704 int x, hpos, real_start, real_end;
4706 xassert (updated_window && updated_row);
4707 BLOCK_INPUT;
4709 /* Write glyphs. */
4711 hpos = start - updated_row->glyphs[updated_area];
4712 x = x_draw_glyphs (updated_window, output_cursor.x,
4713 updated_row, updated_area,
4714 hpos, hpos + len,
4715 (updated_row->inverse_p
4716 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4717 &real_start, &real_end, 0);
4719 /* If we drew over the cursor, note that it is not visible any more. */
4720 note_overwritten_text_cursor (updated_window, real_start,
4721 real_end - real_start);
4723 UNBLOCK_INPUT;
4725 /* Advance the output cursor. */
4726 output_cursor.hpos += len;
4727 output_cursor.x = x;
4731 /* Insert LEN glyphs from START at the nominal cursor position. */
4733 static void
4734 x_insert_glyphs (start, len)
4735 struct glyph *start;
4736 register int len;
4738 struct frame *f;
4739 struct window *w;
4740 int line_height, shift_by_width, shifted_region_width;
4741 struct glyph_row *row;
4742 struct glyph *glyph;
4743 int frame_x, frame_y, hpos, real_start, real_end;
4745 xassert (updated_window && updated_row);
4746 BLOCK_INPUT;
4747 w = updated_window;
4748 f = XFRAME (WINDOW_FRAME (w));
4750 /* Get the height of the line we are in. */
4751 row = updated_row;
4752 line_height = row->height;
4754 /* Get the width of the glyphs to insert. */
4755 shift_by_width = 0;
4756 for (glyph = start; glyph < start + len; ++glyph)
4757 shift_by_width += glyph->pixel_width;
4759 /* Get the width of the region to shift right. */
4760 shifted_region_width = (window_box_width (w, updated_area)
4761 - output_cursor.x
4762 - shift_by_width);
4764 /* Shift right. */
4765 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4766 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4767 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4768 f->output_data.x->normal_gc,
4769 frame_x, frame_y,
4770 shifted_region_width, line_height,
4771 frame_x + shift_by_width, frame_y);
4773 /* Write the glyphs. */
4774 hpos = start - row->glyphs[updated_area];
4775 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4776 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4777 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4779 /* Advance the output cursor. */
4780 output_cursor.hpos += len;
4781 output_cursor.x += shift_by_width;
4782 UNBLOCK_INPUT;
4786 /* Delete N glyphs at the nominal cursor position. Not implemented
4787 for X frames. */
4789 static void
4790 x_delete_glyphs (n)
4791 register int n;
4793 abort ();
4797 /* Erase the current text line from the nominal cursor position
4798 (inclusive) to pixel column TO_X (exclusive). The idea is that
4799 everything from TO_X onward is already erased.
4801 TO_X is a pixel position relative to updated_area of
4802 updated_window. TO_X == -1 means clear to the end of this area. */
4804 static void
4805 x_clear_end_of_line (to_x)
4806 int to_x;
4808 struct frame *f;
4809 struct window *w = updated_window;
4810 int max_x, min_y, max_y;
4811 int from_x, from_y, to_y;
4813 xassert (updated_window && updated_row);
4814 f = XFRAME (w->frame);
4816 if (updated_row->full_width_p)
4818 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4819 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4820 && !w->pseudo_window_p)
4821 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4823 else
4824 max_x = window_box_width (w, updated_area);
4825 max_y = window_text_bottom_y (w);
4827 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4828 of window. For TO_X > 0, truncate to end of drawing area. */
4829 if (to_x == 0)
4830 return;
4831 else if (to_x < 0)
4832 to_x = max_x;
4833 else
4834 to_x = min (to_x, max_x);
4836 to_y = min (max_y, output_cursor.y + updated_row->height);
4838 /* Notice if the cursor will be cleared by this operation. */
4839 if (!updated_row->full_width_p)
4840 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
4842 from_x = output_cursor.x;
4844 /* Translate to frame coordinates. */
4845 if (updated_row->full_width_p)
4847 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4848 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4850 else
4852 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4853 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4856 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
4857 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4858 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4860 /* Prevent inadvertently clearing to end of the X window. */
4861 if (to_x > from_x && to_y > from_y)
4863 BLOCK_INPUT;
4864 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4865 from_x, from_y, to_x - from_x, to_y - from_y,
4866 False);
4867 UNBLOCK_INPUT;
4872 /* Clear entire frame. If updating_frame is non-null, clear that
4873 frame. Otherwise clear the selected frame. */
4875 static void
4876 x_clear_frame ()
4878 struct frame *f;
4880 if (updating_frame)
4881 f = updating_frame;
4882 else
4883 f = SELECTED_FRAME ();
4885 /* Clearing the frame will erase any cursor, so mark them all as no
4886 longer visible. */
4887 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4888 output_cursor.hpos = output_cursor.vpos = 0;
4889 output_cursor.x = -1;
4891 /* We don't set the output cursor here because there will always
4892 follow an explicit cursor_to. */
4893 BLOCK_INPUT;
4894 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
4896 /* We have to clear the scroll bars, too. If we have changed
4897 colors or something like that, then they should be notified. */
4898 x_scroll_bar_clear (f);
4900 XFlush (FRAME_X_DISPLAY (f));
4901 UNBLOCK_INPUT;
4906 /* Invert the middle quarter of the frame for .15 sec. */
4908 /* We use the select system call to do the waiting, so we have to make
4909 sure it's available. If it isn't, we just won't do visual bells. */
4911 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
4914 /* Subtract the `struct timeval' values X and Y, storing the result in
4915 *RESULT. Return 1 if the difference is negative, otherwise 0. */
4917 static int
4918 timeval_subtract (result, x, y)
4919 struct timeval *result, x, y;
4921 /* Perform the carry for the later subtraction by updating y. This
4922 is safer because on some systems the tv_sec member is unsigned. */
4923 if (x.tv_usec < y.tv_usec)
4925 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
4926 y.tv_usec -= 1000000 * nsec;
4927 y.tv_sec += nsec;
4930 if (x.tv_usec - y.tv_usec > 1000000)
4932 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
4933 y.tv_usec += 1000000 * nsec;
4934 y.tv_sec -= nsec;
4937 /* Compute the time remaining to wait. tv_usec is certainly
4938 positive. */
4939 result->tv_sec = x.tv_sec - y.tv_sec;
4940 result->tv_usec = x.tv_usec - y.tv_usec;
4942 /* Return indication of whether the result should be considered
4943 negative. */
4944 return x.tv_sec < y.tv_sec;
4947 void
4948 XTflash (f)
4949 struct frame *f;
4951 BLOCK_INPUT;
4954 GC gc;
4956 /* Create a GC that will use the GXxor function to flip foreground
4957 pixels into background pixels. */
4959 XGCValues values;
4961 values.function = GXxor;
4962 values.foreground = (f->output_data.x->foreground_pixel
4963 ^ f->output_data.x->background_pixel);
4965 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4966 GCFunction | GCForeground, &values);
4970 /* Get the height not including a menu bar widget. */
4971 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
4972 /* Height of each line to flash. */
4973 int flash_height = FRAME_LINE_HEIGHT (f);
4974 /* These will be the left and right margins of the rectangles. */
4975 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
4976 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
4978 int width;
4980 /* Don't flash the area between a scroll bar and the frame
4981 edge it is next to. */
4982 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
4984 case vertical_scroll_bar_left:
4985 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4986 break;
4988 case vertical_scroll_bar_right:
4989 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
4990 break;
4992 default:
4993 break;
4996 width = flash_right - flash_left;
4998 /* If window is tall, flash top and bottom line. */
4999 if (height > 3 * FRAME_LINE_HEIGHT (f))
5001 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5002 flash_left,
5003 (FRAME_INTERNAL_BORDER_WIDTH (f)
5004 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5005 width, flash_height);
5006 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5007 flash_left,
5008 (height - flash_height
5009 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5010 width, flash_height);
5012 else
5013 /* If it is short, flash it all. */
5014 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5015 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5016 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5018 x_flush (f);
5021 struct timeval wakeup;
5023 EMACS_GET_TIME (wakeup);
5025 /* Compute time to wait until, propagating carry from usecs. */
5026 wakeup.tv_usec += 150000;
5027 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5028 wakeup.tv_usec %= 1000000;
5030 /* Keep waiting until past the time wakeup. */
5031 while (1)
5033 struct timeval timeout;
5035 EMACS_GET_TIME (timeout);
5037 /* In effect, timeout = wakeup - timeout.
5038 Break if result would be negative. */
5039 if (timeval_subtract (&timeout, wakeup, timeout))
5040 break;
5042 /* Try to wait that long--but we might wake up sooner. */
5043 select (0, NULL, NULL, NULL, &timeout);
5047 /* If window is tall, flash top and bottom line. */
5048 if (height > 3 * FRAME_LINE_HEIGHT (f))
5050 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5051 flash_left,
5052 (FRAME_INTERNAL_BORDER_WIDTH (f)
5053 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5054 width, flash_height);
5055 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5056 flash_left,
5057 (height - flash_height
5058 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5059 width, flash_height);
5061 else
5062 /* If it is short, flash it all. */
5063 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5064 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5065 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5067 XFreeGC (FRAME_X_DISPLAY (f), gc);
5068 x_flush (f);
5072 UNBLOCK_INPUT;
5075 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5078 /* Make audible bell. */
5080 void
5081 XTring_bell ()
5083 struct frame *f = SELECTED_FRAME ();
5085 if (FRAME_X_DISPLAY (f))
5087 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5088 if (visible_bell)
5089 XTflash (f);
5090 else
5091 #endif
5093 BLOCK_INPUT;
5094 XBell (FRAME_X_DISPLAY (f), 0);
5095 XFlush (FRAME_X_DISPLAY (f));
5096 UNBLOCK_INPUT;
5102 /* Specify how many text lines, from the top of the window,
5103 should be affected by insert-lines and delete-lines operations.
5104 This, and those operations, are used only within an update
5105 that is bounded by calls to x_update_begin and x_update_end. */
5107 static void
5108 XTset_terminal_window (n)
5109 register int n;
5111 /* This function intentionally left blank. */
5116 /***********************************************************************
5117 Line Dance
5118 ***********************************************************************/
5120 /* Perform an insert-lines or delete-lines operation, inserting N
5121 lines or deleting -N lines at vertical position VPOS. */
5123 static void
5124 x_ins_del_lines (vpos, n)
5125 int vpos, n;
5127 abort ();
5131 /* Scroll part of the display as described by RUN. */
5133 static void
5134 x_scroll_run (w, run)
5135 struct window *w;
5136 struct run *run;
5138 struct frame *f = XFRAME (w->frame);
5139 int x, y, width, height, from_y, to_y, bottom_y;
5141 /* Get frame-relative bounding box of the text display area of W,
5142 without mode lines. Include in this box the flags areas to the
5143 left and right of W. */
5144 window_box (w, -1, &x, &y, &width, &height);
5145 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5146 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5148 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5149 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5150 bottom_y = y + height;
5152 if (to_y < from_y)
5154 /* Scrolling up. Make sure we don't copy part of the mode
5155 line at the bottom. */
5156 if (from_y + run->height > bottom_y)
5157 height = bottom_y - from_y;
5158 else
5159 height = run->height;
5161 else
5163 /* Scolling down. Make sure we don't copy over the mode line.
5164 at the bottom. */
5165 if (to_y + run->height > bottom_y)
5166 height = bottom_y - to_y;
5167 else
5168 height = run->height;
5171 BLOCK_INPUT;
5173 /* Cursor off. Will be switched on again in x_update_window_end. */
5174 updated_window = w;
5175 x_clear_cursor (w);
5177 XCopyArea (FRAME_X_DISPLAY (f),
5178 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5179 f->output_data.x->normal_gc,
5180 x, from_y,
5181 width, height,
5182 x, to_y);
5184 UNBLOCK_INPUT;
5189 /***********************************************************************
5190 Exposure Events
5191 ***********************************************************************/
5193 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5194 corner of the exposed rectangle. W and H are width and height of
5195 the exposed area. All are pixel values. W or H zero means redraw
5196 the entire frame. */
5198 static void
5199 expose_frame (f, x, y, w, h)
5200 struct frame *f;
5201 int x, y, w, h;
5203 XRectangle r;
5205 TRACE ((stderr, "expose_frame "));
5207 /* No need to redraw if frame will be redrawn soon. */
5208 if (FRAME_GARBAGED_P (f))
5210 TRACE ((stderr, " garbaged\n"));
5211 return;
5214 /* If basic faces haven't been realized yet, there is no point in
5215 trying to redraw anything. This can happen when we get an expose
5216 event while Emacs is starting, e.g. by moving another window. */
5217 if (FRAME_FACE_CACHE (f) == NULL
5218 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5220 TRACE ((stderr, " no faces\n"));
5221 return;
5224 if (w == 0 || h == 0)
5226 r.x = r.y = 0;
5227 r.width = CANON_X_UNIT (f) * f->width;
5228 r.height = CANON_Y_UNIT (f) * f->height;
5230 else
5232 r.x = x;
5233 r.y = y;
5234 r.width = w;
5235 r.height = h;
5238 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5239 expose_window_tree (XWINDOW (f->root_window), &r);
5241 if (WINDOWP (f->tool_bar_window))
5243 struct window *w = XWINDOW (f->tool_bar_window);
5244 XRectangle window_rect;
5245 XRectangle intersection_rect;
5246 int window_x, window_y, window_width, window_height;
5249 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5250 window_rect.x = window_x;
5251 window_rect.y = window_y;
5252 window_rect.width = window_width;
5253 window_rect.height = window_height;
5255 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5256 expose_window (w, &intersection_rect);
5259 #ifndef USE_X_TOOLKIT
5260 if (WINDOWP (f->menu_bar_window))
5262 struct window *w = XWINDOW (f->menu_bar_window);
5263 XRectangle window_rect;
5264 XRectangle intersection_rect;
5265 int window_x, window_y, window_width, window_height;
5268 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5269 window_rect.x = window_x;
5270 window_rect.y = window_y;
5271 window_rect.width = window_width;
5272 window_rect.height = window_height;
5274 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5275 expose_window (w, &intersection_rect);
5277 #endif /* not USE_X_TOOLKIT */
5281 /* Redraw (parts) of all windows in the window tree rooted at W that
5282 intersect R. R contains frame pixel coordinates. */
5284 static void
5285 expose_window_tree (w, r)
5286 struct window *w;
5287 XRectangle *r;
5289 while (w)
5291 if (!NILP (w->hchild))
5292 expose_window_tree (XWINDOW (w->hchild), r);
5293 else if (!NILP (w->vchild))
5294 expose_window_tree (XWINDOW (w->vchild), r);
5295 else
5297 XRectangle window_rect;
5298 XRectangle intersection_rect;
5299 struct frame *f = XFRAME (w->frame);
5300 int window_x, window_y, window_width, window_height;
5302 /* Frame-relative pixel rectangle of W. */
5303 window_box (w, -1, &window_x, &window_y, &window_width,
5304 &window_height);
5305 window_rect.x
5306 = (window_x
5307 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5308 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5309 window_rect.y = window_y;
5310 window_rect.width
5311 = (window_width
5312 + FRAME_X_FLAGS_AREA_WIDTH (f)
5313 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5314 window_rect.height
5315 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5317 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5318 expose_window (w, &intersection_rect);
5321 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5326 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5327 which intersects rectangle R. R is in window-relative coordinates. */
5329 static void
5330 expose_area (w, row, r, area)
5331 struct window *w;
5332 struct glyph_row *row;
5333 XRectangle *r;
5334 enum glyph_row_area area;
5336 int x;
5337 struct glyph *first = row->glyphs[area];
5338 struct glyph *end = row->glyphs[area] + row->used[area];
5339 struct glyph *last;
5340 int first_x;
5342 /* Set x to the window-relative start position for drawing glyphs of
5343 AREA. The first glyph of the text area can be partially visible.
5344 The first glyphs of other areas cannot. */
5345 if (area == LEFT_MARGIN_AREA)
5346 x = 0;
5347 else if (area == TEXT_AREA)
5348 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5349 else
5350 x = (window_box_width (w, LEFT_MARGIN_AREA)
5351 + window_box_width (w, TEXT_AREA));
5353 if (area == TEXT_AREA && row->fill_line_p)
5354 /* If row extends face to end of line write the whole line. */
5355 x_draw_glyphs (w, x, row, area,
5356 0, row->used[area],
5357 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5358 NULL, NULL, 0);
5359 else
5361 /* Find the first glyph that must be redrawn. */
5362 while (first < end
5363 && x + first->pixel_width < r->x)
5365 x += first->pixel_width;
5366 ++first;
5369 /* Find the last one. */
5370 last = first;
5371 first_x = x;
5372 while (last < end
5373 && x < r->x + r->width)
5375 x += last->pixel_width;
5376 ++last;
5379 /* Repaint. */
5380 if (last > first)
5381 x_draw_glyphs (w, first_x, row, area,
5382 first - row->glyphs[area],
5383 last - row->glyphs[area],
5384 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5385 NULL, NULL, 0);
5390 /* Redraw the parts of the glyph row ROW on window W intersecting
5391 rectangle R. R is in window-relative coordinates. */
5393 static void
5394 expose_line (w, row, r)
5395 struct window *w;
5396 struct glyph_row *row;
5397 XRectangle *r;
5399 xassert (row->enabled_p);
5401 if (row->mode_line_p || w->pseudo_window_p)
5402 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5403 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5404 NULL, NULL, 0);
5405 else
5407 if (row->used[LEFT_MARGIN_AREA])
5408 expose_area (w, row, r, LEFT_MARGIN_AREA);
5409 if (row->used[TEXT_AREA])
5410 expose_area (w, row, r, TEXT_AREA);
5411 if (row->used[RIGHT_MARGIN_AREA])
5412 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5413 x_draw_row_bitmaps (w, row);
5418 /* Return non-zero if W's cursor intersects rectangle R. */
5420 static int
5421 x_phys_cursor_in_rect_p (w, r)
5422 struct window *w;
5423 XRectangle *r;
5425 XRectangle cr, result;
5426 struct glyph *cursor_glyph;
5428 cursor_glyph = get_phys_cursor_glyph (w);
5429 if (cursor_glyph)
5431 cr.x = w->phys_cursor.x;
5432 cr.y = w->phys_cursor.y;
5433 cr.width = cursor_glyph->pixel_width;
5434 cr.height = w->phys_cursor_height;
5435 return x_intersect_rectangles (&cr, r, &result);
5437 else
5438 return 0;
5442 /* Redraw a rectangle of window W. R is a rectangle in window
5443 relative coordinates. Call this function with input blocked. */
5445 static void
5446 expose_window (w, r)
5447 struct window *w;
5448 XRectangle *r;
5450 struct glyph_row *row;
5451 int y;
5452 int yb = window_text_bottom_y (w);
5453 int cursor_cleared_p;
5455 /* If window is not yet fully initialized, do nothing. This can
5456 happen when toolkit scroll bars are used and a window is split.
5457 Reconfiguring the scroll bar will generate an expose for a newly
5458 created window. */
5459 if (w->current_matrix == NULL)
5460 return;
5462 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5463 r->x, r->y, r->width, r->height));
5465 /* Convert to window coordinates. */
5466 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5467 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5469 /* Turn off the cursor. */
5470 if (!w->pseudo_window_p
5471 && x_phys_cursor_in_rect_p (w, r))
5473 x_clear_cursor (w);
5474 cursor_cleared_p = 1;
5476 else
5477 cursor_cleared_p = 0;
5479 /* Find the first row intersecting the rectangle R. */
5480 row = w->current_matrix->rows;
5481 y = 0;
5482 while (row->enabled_p
5483 && y < yb
5484 && y + row->height < r->y)
5486 y += row->height;
5487 ++row;
5490 /* Display the text in the rectangle, one text line at a time. */
5491 while (row->enabled_p
5492 && y < yb
5493 && y < r->y + r->height)
5495 expose_line (w, row, r);
5496 y += row->height;
5497 ++row;
5500 /* Display the mode line if there is one. */
5501 if (WINDOW_WANTS_MODELINE_P (w)
5502 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5503 row->enabled_p)
5504 && row->y < r->y + r->height)
5505 expose_line (w, row, r);
5507 if (!w->pseudo_window_p)
5509 /* Draw border between windows. */
5510 x_draw_vertical_border (w);
5512 /* Turn the cursor on again. */
5513 if (cursor_cleared_p)
5514 x_update_window_cursor (w, 1);
5519 /* Determine the intersection of two rectangles R1 and R2. Return
5520 the intersection in *RESULT. Value is non-zero if RESULT is not
5521 empty. */
5523 static int
5524 x_intersect_rectangles (r1, r2, result)
5525 XRectangle *r1, *r2, *result;
5527 XRectangle *left, *right;
5528 XRectangle *upper, *lower;
5529 int intersection_p = 0;
5531 /* Rearrange so that R1 is the left-most rectangle. */
5532 if (r1->x < r2->x)
5533 left = r1, right = r2;
5534 else
5535 left = r2, right = r1;
5537 /* X0 of the intersection is right.x0, if this is inside R1,
5538 otherwise there is no intersection. */
5539 if (right->x <= left->x + left->width)
5541 result->x = right->x;
5543 /* The right end of the intersection is the minimum of the
5544 the right ends of left and right. */
5545 result->width = (min (left->x + left->width, right->x + right->width)
5546 - result->x);
5548 /* Same game for Y. */
5549 if (r1->y < r2->y)
5550 upper = r1, lower = r2;
5551 else
5552 upper = r2, lower = r1;
5554 /* The upper end of the intersection is lower.y0, if this is inside
5555 of upper. Otherwise, there is no intersection. */
5556 if (lower->y <= upper->y + upper->height)
5558 result->y = lower->y;
5560 /* The lower end of the intersection is the minimum of the lower
5561 ends of upper and lower. */
5562 result->height = (min (lower->y + lower->height,
5563 upper->y + upper->height)
5564 - result->y);
5565 intersection_p = 1;
5569 return intersection_p;
5576 static void
5577 frame_highlight (f)
5578 struct frame *f;
5580 /* We used to only do this if Vx_no_window_manager was non-nil, but
5581 the ICCCM (section 4.1.6) says that the window's border pixmap
5582 and border pixel are window attributes which are "private to the
5583 client", so we can always change it to whatever we want. */
5584 BLOCK_INPUT;
5585 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5586 f->output_data.x->border_pixel);
5587 UNBLOCK_INPUT;
5588 x_update_cursor (f, 1);
5591 static void
5592 frame_unhighlight (f)
5593 struct frame *f;
5595 /* We used to only do this if Vx_no_window_manager was non-nil, but
5596 the ICCCM (section 4.1.6) says that the window's border pixmap
5597 and border pixel are window attributes which are "private to the
5598 client", so we can always change it to whatever we want. */
5599 BLOCK_INPUT;
5600 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5601 f->output_data.x->border_tile);
5602 UNBLOCK_INPUT;
5603 x_update_cursor (f, 1);
5606 /* The focus has changed. Update the frames as necessary to reflect
5607 the new situation. Note that we can't change the selected frame
5608 here, because the Lisp code we are interrupting might become confused.
5609 Each event gets marked with the frame in which it occurred, so the
5610 Lisp code can tell when the switch took place by examining the events. */
5612 static void
5613 x_new_focus_frame (dpyinfo, frame)
5614 struct x_display_info *dpyinfo;
5615 struct frame *frame;
5617 struct frame *old_focus = dpyinfo->x_focus_frame;
5619 if (frame != dpyinfo->x_focus_frame)
5621 /* Set this before calling other routines, so that they see
5622 the correct value of x_focus_frame. */
5623 dpyinfo->x_focus_frame = frame;
5625 if (old_focus && old_focus->auto_lower)
5626 x_lower_frame (old_focus);
5628 #if 0
5629 selected_frame = frame;
5630 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5631 selected_frame);
5632 Fselect_window (selected_frame->selected_window);
5633 choose_minibuf_frame ();
5634 #endif /* ! 0 */
5636 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5637 pending_autoraise_frame = dpyinfo->x_focus_frame;
5638 else
5639 pending_autoraise_frame = 0;
5642 x_frame_rehighlight (dpyinfo);
5645 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5647 void
5648 x_mouse_leave (dpyinfo)
5649 struct x_display_info *dpyinfo;
5651 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5654 /* The focus has changed, or we have redirected a frame's focus to
5655 another frame (this happens when a frame uses a surrogate
5656 mini-buffer frame). Shift the highlight as appropriate.
5658 The FRAME argument doesn't necessarily have anything to do with which
5659 frame is being highlighted or un-highlighted; we only use it to find
5660 the appropriate X display info. */
5662 static void
5663 XTframe_rehighlight (frame)
5664 struct frame *frame;
5666 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5669 static void
5670 x_frame_rehighlight (dpyinfo)
5671 struct x_display_info *dpyinfo;
5673 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5675 if (dpyinfo->x_focus_frame)
5677 dpyinfo->x_highlight_frame
5678 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5679 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5680 : dpyinfo->x_focus_frame);
5681 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5683 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5684 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5687 else
5688 dpyinfo->x_highlight_frame = 0;
5690 if (dpyinfo->x_highlight_frame != old_highlight)
5692 if (old_highlight)
5693 frame_unhighlight (old_highlight);
5694 if (dpyinfo->x_highlight_frame)
5695 frame_highlight (dpyinfo->x_highlight_frame);
5701 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5703 /* Initialize mode_switch_bit and modifier_meaning. */
5704 static void
5705 x_find_modifier_meanings (dpyinfo)
5706 struct x_display_info *dpyinfo;
5708 int min_code, max_code;
5709 KeySym *syms;
5710 int syms_per_code;
5711 XModifierKeymap *mods;
5713 dpyinfo->meta_mod_mask = 0;
5714 dpyinfo->shift_lock_mask = 0;
5715 dpyinfo->alt_mod_mask = 0;
5716 dpyinfo->super_mod_mask = 0;
5717 dpyinfo->hyper_mod_mask = 0;
5719 #ifdef HAVE_X11R4
5720 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5721 #else
5722 min_code = dpyinfo->display->min_keycode;
5723 max_code = dpyinfo->display->max_keycode;
5724 #endif
5726 syms = XGetKeyboardMapping (dpyinfo->display,
5727 min_code, max_code - min_code + 1,
5728 &syms_per_code);
5729 mods = XGetModifierMapping (dpyinfo->display);
5731 /* Scan the modifier table to see which modifier bits the Meta and
5732 Alt keysyms are on. */
5734 int row, col; /* The row and column in the modifier table. */
5736 for (row = 3; row < 8; row++)
5737 for (col = 0; col < mods->max_keypermod; col++)
5739 KeyCode code
5740 = mods->modifiermap[(row * mods->max_keypermod) + col];
5742 /* Zeroes are used for filler. Skip them. */
5743 if (code == 0)
5744 continue;
5746 /* Are any of this keycode's keysyms a meta key? */
5748 int code_col;
5750 for (code_col = 0; code_col < syms_per_code; code_col++)
5752 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5754 switch (sym)
5756 case XK_Meta_L:
5757 case XK_Meta_R:
5758 dpyinfo->meta_mod_mask |= (1 << row);
5759 break;
5761 case XK_Alt_L:
5762 case XK_Alt_R:
5763 dpyinfo->alt_mod_mask |= (1 << row);
5764 break;
5766 case XK_Hyper_L:
5767 case XK_Hyper_R:
5768 dpyinfo->hyper_mod_mask |= (1 << row);
5769 break;
5771 case XK_Super_L:
5772 case XK_Super_R:
5773 dpyinfo->super_mod_mask |= (1 << row);
5774 break;
5776 case XK_Shift_Lock:
5777 /* Ignore this if it's not on the lock modifier. */
5778 if ((1 << row) == LockMask)
5779 dpyinfo->shift_lock_mask = LockMask;
5780 break;
5787 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5788 if (! dpyinfo->meta_mod_mask)
5790 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5791 dpyinfo->alt_mod_mask = 0;
5794 /* If some keys are both alt and meta,
5795 make them just meta, not alt. */
5796 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
5798 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
5801 XFree ((char *) syms);
5802 XFreeModifiermap (mods);
5805 /* Convert between the modifier bits X uses and the modifier bits
5806 Emacs uses. */
5808 static unsigned int
5809 x_x_to_emacs_modifiers (dpyinfo, state)
5810 struct x_display_info *dpyinfo;
5811 unsigned int state;
5813 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5814 | ((state & ControlMask) ? ctrl_modifier : 0)
5815 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5816 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5817 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5818 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
5821 static unsigned int
5822 x_emacs_to_x_modifiers (dpyinfo, state)
5823 struct x_display_info *dpyinfo;
5824 unsigned int state;
5826 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5827 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5828 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5829 | ((state & shift_modifier) ? ShiftMask : 0)
5830 | ((state & ctrl_modifier) ? ControlMask : 0)
5831 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
5834 /* Convert a keysym to its name. */
5836 char *
5837 x_get_keysym_name (keysym)
5838 KeySym keysym;
5840 char *value;
5842 BLOCK_INPUT;
5843 value = XKeysymToString (keysym);
5844 UNBLOCK_INPUT;
5846 return value;
5851 /* Mouse clicks and mouse movement. Rah. */
5853 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5854 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5855 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5856 not force the value into range. */
5858 void
5859 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5860 FRAME_PTR f;
5861 register int pix_x, pix_y;
5862 register int *x, *y;
5863 XRectangle *bounds;
5864 int noclip;
5866 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5867 even for negative values. */
5868 if (pix_x < 0)
5869 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
5870 if (pix_y < 0)
5871 pix_y -= (f)->output_data.x->line_height - 1;
5873 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5874 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5876 if (bounds)
5878 bounds->width = FONT_WIDTH (f->output_data.x->font);
5879 bounds->height = f->output_data.x->line_height;
5880 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5881 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5884 if (!noclip)
5886 if (pix_x < 0)
5887 pix_x = 0;
5888 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5889 pix_x = FRAME_WINDOW_WIDTH (f);
5891 if (pix_y < 0)
5892 pix_y = 0;
5893 else if (pix_y > f->height)
5894 pix_y = f->height;
5897 *x = pix_x;
5898 *y = pix_y;
5902 /* Given HPOS/VPOS in the current matrix of W, return corresponding
5903 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
5904 can't tell the positions because W's display is not up to date,
5905 return 0. */
5908 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
5909 struct window *w;
5910 int hpos, vpos;
5911 int *frame_x, *frame_y;
5913 int success_p;
5915 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
5916 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
5918 if (display_completed)
5920 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
5921 struct glyph *glyph = row->glyphs[TEXT_AREA];
5922 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
5924 *frame_y = row->y;
5925 *frame_x = row->x;
5926 while (glyph < end)
5928 *frame_x += glyph->pixel_width;
5929 ++glyph;
5932 success_p = 1;
5934 else
5936 *frame_y = *frame_x = 0;
5937 success_p = 0;
5940 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
5941 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
5942 return success_p;
5946 /* Prepare a mouse-event in *RESULT for placement in the input queue.
5948 If the event is a button press, then note that we have grabbed
5949 the mouse. */
5951 static Lisp_Object
5952 construct_mouse_click (result, event, f)
5953 struct input_event *result;
5954 XButtonEvent *event;
5955 struct frame *f;
5957 /* Make the event type no_event; we'll change that when we decide
5958 otherwise. */
5959 result->kind = mouse_click;
5960 result->code = event->button - Button1;
5961 result->timestamp = event->time;
5962 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5963 event->state)
5964 | (event->type == ButtonRelease
5965 ? up_modifier
5966 : down_modifier));
5968 XSETINT (result->x, event->x);
5969 XSETINT (result->y, event->y);
5970 XSETFRAME (result->frame_or_window, f);
5971 return Qnil;
5974 #if 0 /* This function isn't called. --gerd */
5976 /* Prepare a menu-event in *RESULT for placement in the input queue. */
5978 static Lisp_Object
5979 construct_menu_click (result, event, f)
5980 struct input_event *result;
5981 XButtonEvent *event;
5982 struct frame *f;
5984 /* Make the event type no_event; we'll change that when we decide
5985 otherwise. */
5986 result->kind = mouse_click;
5987 result->code = event->button - Button1;
5988 result->timestamp = event->time;
5989 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
5990 event->state)
5991 | (event->type == ButtonRelease
5992 ? up_modifier
5993 : down_modifier));
5995 XSETINT (result->x, event->x);
5996 XSETINT (result->y, -1);
5997 XSETFRAME (result->frame_or_window, f);
6000 #endif /* 0 */
6003 /* Function to report a mouse movement to the mainstream Emacs code.
6004 The input handler calls this.
6006 We have received a mouse movement event, which is given in *event.
6007 If the mouse is over a different glyph than it was last time, tell
6008 the mainstream emacs code by setting mouse_moved. If not, ask for
6009 another motion event, so we can check again the next time it moves. */
6011 static XMotionEvent last_mouse_motion_event;
6012 static Lisp_Object last_mouse_motion_frame;
6014 static void
6015 note_mouse_movement (frame, event)
6016 FRAME_PTR frame;
6017 XMotionEvent *event;
6019 last_mouse_movement_time = event->time;
6020 last_mouse_motion_event = *event;
6021 XSETFRAME (last_mouse_motion_frame, frame);
6023 if (event->window != FRAME_X_WINDOW (frame))
6025 frame->mouse_moved = 1;
6026 last_mouse_scroll_bar = Qnil;
6027 note_mouse_highlight (frame, -1, -1);
6030 /* Has the mouse moved off the glyph it was on at the last sighting? */
6031 else if (event->x < last_mouse_glyph.x
6032 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6033 || event->y < last_mouse_glyph.y
6034 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6036 frame->mouse_moved = 1;
6037 last_mouse_scroll_bar = Qnil;
6038 note_mouse_highlight (frame, event->x, event->y);
6042 /* This is used for debugging, to turn off note_mouse_highlight. */
6044 int disable_mouse_highlight;
6048 /************************************************************************
6049 Mouse Face
6050 ************************************************************************/
6052 /* Find the glyph under window-relative coordinates X/Y in window W.
6053 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6054 strings. Return in *HPOS and *VPOS the row and column number of
6055 the glyph found. Return in *AREA the glyph area containing X.
6056 Value is a pointer to the glyph found or null if X/Y is not on
6057 text, or we can't tell because W's current matrix is not up to
6058 date. */
6060 static struct glyph *
6061 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6062 struct window *w;
6063 int x, y;
6064 int *hpos, *vpos, *area;
6066 struct glyph *glyph, *end;
6067 struct glyph_row *row;
6068 int x0, i, left_area_width;
6070 /* Find row containing Y. Give up if some row is not enabled. */
6071 for (i = 0; i < w->current_matrix->nrows; ++i)
6073 row = MATRIX_ROW (w->current_matrix, i);
6074 if (!row->enabled_p)
6075 return NULL;
6076 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6077 break;
6080 *vpos = i;
6081 *hpos = 0;
6083 /* Give up if Y is not in the window. */
6084 if (i == w->current_matrix->nrows)
6085 return NULL;
6087 /* Get the glyph area containing X. */
6088 if (w->pseudo_window_p)
6090 *area = TEXT_AREA;
6091 x0 = 0;
6093 else
6095 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6096 if (x < left_area_width)
6098 *area = LEFT_MARGIN_AREA;
6099 x0 = 0;
6101 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6103 *area = TEXT_AREA;
6104 x0 = row->x + left_area_width;
6106 else
6108 *area = RIGHT_MARGIN_AREA;
6109 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6113 /* Find glyph containing X. */
6114 glyph = row->glyphs[*area];
6115 end = glyph + row->used[*area];
6116 while (glyph < end)
6118 if (x < x0 + glyph->pixel_width)
6120 if (w->pseudo_window_p)
6121 break;
6122 else if (BUFFERP (glyph->object))
6123 break;
6126 x0 += glyph->pixel_width;
6127 ++glyph;
6130 if (glyph == end)
6131 return NULL;
6133 *hpos = glyph - row->glyphs[*area];
6134 return glyph;
6138 /* Convert frame-relative x/y to coordinates relative to window W.
6139 Takes pseudo-windows into account. */
6141 static void
6142 frame_to_window_pixel_xy (w, x, y)
6143 struct window *w;
6144 int *x, *y;
6146 if (w->pseudo_window_p)
6148 /* A pseudo-window is always full-width, and starts at the
6149 left edge of the frame, plus a frame border. */
6150 struct frame *f = XFRAME (w->frame);
6151 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6152 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6154 else
6156 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6157 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6162 /* Take proper action when mouse has moved to the mode or top line of
6163 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6164 mode line. X is relative to the start of the text display area of
6165 W, so the width of bitmap areas and scroll bars must be subtracted
6166 to get a position relative to the start of the mode line. */
6168 static void
6169 note_mode_line_highlight (w, x, mode_line_p)
6170 struct window *w;
6171 int x, mode_line_p;
6173 struct frame *f = XFRAME (w->frame);
6174 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6175 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6176 struct glyph_row *row;
6178 if (mode_line_p)
6179 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6180 else
6181 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6183 if (row->enabled_p)
6185 struct glyph *glyph, *end;
6186 Lisp_Object help, map;
6187 int x0;
6189 /* Find the glyph under X. */
6190 glyph = row->glyphs[TEXT_AREA];
6191 end = glyph + row->used[TEXT_AREA];
6192 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6193 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6194 while (glyph < end
6195 && x >= x0 + glyph->pixel_width)
6197 x0 += glyph->pixel_width;
6198 ++glyph;
6201 if (glyph < end
6202 && STRINGP (glyph->object)
6203 && XSTRING (glyph->object)->intervals
6204 && glyph->charpos >= 0
6205 && glyph->charpos < XSTRING (glyph->object)->size)
6207 /* If we're on a string with `help-echo' text property,
6208 arrange for the help to be displayed. This is done by
6209 setting the global variable help_echo to the help string. */
6210 help = Fget_text_property (make_number (glyph->charpos),
6211 Qhelp_echo, glyph->object);
6212 if (STRINGP (help))
6213 help_echo = help;
6215 /* Change the mouse pointer according to what is under X/Y. */
6216 map = Fget_text_property (make_number (glyph->charpos),
6217 Qlocal_map, glyph->object);
6218 if (!NILP (Fkeymapp (map)))
6219 cursor = f->output_data.x->nontext_cursor;
6223 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6227 /* Take proper action when the mouse has moved to position X, Y on
6228 frame F as regards highlighting characters that have mouse-face
6229 properties. Also de-highlighting chars where the mouse was before.
6230 X and Y can be negative or out of range. */
6232 static void
6233 note_mouse_highlight (f, x, y)
6234 struct frame *f;
6235 int x, y;
6237 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6238 int portion;
6239 Lisp_Object window;
6240 struct window *w;
6242 /* When a menu is active, don't highlight because this looks odd. */
6243 #ifdef USE_X_TOOLKIT
6244 if (popup_activated ())
6245 return;
6246 #endif
6248 if (disable_mouse_highlight
6249 || !f->glyphs_initialized_p)
6250 return;
6252 dpyinfo->mouse_face_mouse_x = x;
6253 dpyinfo->mouse_face_mouse_y = y;
6254 dpyinfo->mouse_face_mouse_frame = f;
6256 if (dpyinfo->mouse_face_defer)
6257 return;
6259 if (gc_in_progress)
6261 dpyinfo->mouse_face_deferred_gc = 1;
6262 return;
6265 /* Which window is that in? */
6266 window = window_from_coordinates (f, x, y, &portion, 1);
6268 /* If we were displaying active text in another window, clear that. */
6269 if (! EQ (window, dpyinfo->mouse_face_window))
6270 clear_mouse_face (dpyinfo);
6272 /* Not on a window -> return. */
6273 if (!WINDOWP (window))
6274 return;
6276 /* Convert to window-relative pixel coordinates. */
6277 w = XWINDOW (window);
6278 frame_to_window_pixel_xy (w, &x, &y);
6280 /* Handle tool-bar window differently since it doesn't display a
6281 buffer. */
6282 if (EQ (window, f->tool_bar_window))
6284 note_tool_bar_highlight (f, x, y);
6285 return;
6288 if (portion == 1 || portion == 3)
6290 /* Mouse is on the mode or top line. */
6291 note_mode_line_highlight (w, x, portion == 1);
6292 return;
6294 else
6295 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6296 f->output_data.x->text_cursor);
6298 /* Are we in a window whose display is up to date?
6299 And verify the buffer's text has not changed. */
6300 if (/* Within text portion of the window. */
6301 portion == 0
6302 && EQ (w->window_end_valid, w->buffer)
6303 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6304 && (XFASTINT (w->last_overlay_modified)
6305 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6307 int hpos, vpos, pos, i, area;
6308 struct glyph *glyph;
6310 /* Find the glyph under X/Y. */
6311 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6313 /* Clear mouse face if X/Y not over text. */
6314 if (glyph == NULL
6315 || area != TEXT_AREA
6316 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6318 clear_mouse_face (dpyinfo);
6319 return;
6322 pos = glyph->charpos;
6323 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6325 /* Check for mouse-face and help-echo. */
6327 Lisp_Object mouse_face, overlay, position;
6328 Lisp_Object *overlay_vec;
6329 int len, noverlays;
6330 struct buffer *obuf;
6331 int obegv, ozv;
6333 /* If we get an out-of-range value, return now; avoid an error. */
6334 if (pos > BUF_Z (XBUFFER (w->buffer)))
6335 return;
6337 /* Make the window's buffer temporarily current for
6338 overlays_at and compute_char_face. */
6339 obuf = current_buffer;
6340 current_buffer = XBUFFER (w->buffer);
6341 obegv = BEGV;
6342 ozv = ZV;
6343 BEGV = BEG;
6344 ZV = Z;
6346 /* Is this char mouse-active or does it have help-echo? */
6347 XSETINT (position, pos);
6349 /* Put all the overlays we want in a vector in overlay_vec.
6350 Store the length in len. If there are more than 10, make
6351 enough space for all, and try again. */
6352 len = 10;
6353 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6354 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6355 if (noverlays > len)
6357 len = noverlays;
6358 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6359 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6362 noverlays = sort_overlays (overlay_vec, noverlays, w);
6364 /* Check mouse-face highlighting. */
6365 if (! (EQ (window, dpyinfo->mouse_face_window)
6366 && vpos >= dpyinfo->mouse_face_beg_row
6367 && vpos <= dpyinfo->mouse_face_end_row
6368 && (vpos > dpyinfo->mouse_face_beg_row
6369 || hpos >= dpyinfo->mouse_face_beg_col)
6370 && (vpos < dpyinfo->mouse_face_end_row
6371 || hpos < dpyinfo->mouse_face_end_col
6372 || dpyinfo->mouse_face_past_end)))
6374 /* Clear the display of the old active region, if any. */
6375 clear_mouse_face (dpyinfo);
6377 /* Find the highest priority overlay that has a mouse-face prop. */
6378 overlay = Qnil;
6379 for (i = 0; i < noverlays; i++)
6381 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6382 if (!NILP (mouse_face))
6384 overlay = overlay_vec[i];
6385 break;
6389 /* If no overlay applies, get a text property. */
6390 if (NILP (overlay))
6391 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6393 /* Handle the overlay case. */
6394 if (! NILP (overlay))
6396 /* Find the range of text around this char that
6397 should be active. */
6398 Lisp_Object before, after;
6399 int ignore;
6401 before = Foverlay_start (overlay);
6402 after = Foverlay_end (overlay);
6403 /* Record this as the current active region. */
6404 fast_find_position (w, XFASTINT (before),
6405 &dpyinfo->mouse_face_beg_col,
6406 &dpyinfo->mouse_face_beg_row,
6407 &dpyinfo->mouse_face_beg_x,
6408 &dpyinfo->mouse_face_beg_y);
6409 dpyinfo->mouse_face_past_end
6410 = !fast_find_position (w, XFASTINT (after),
6411 &dpyinfo->mouse_face_end_col,
6412 &dpyinfo->mouse_face_end_row,
6413 &dpyinfo->mouse_face_end_x,
6414 &dpyinfo->mouse_face_end_y);
6415 dpyinfo->mouse_face_window = window;
6416 dpyinfo->mouse_face_face_id
6417 = face_at_buffer_position (w, pos, 0, 0,
6418 &ignore, pos + 1, 1);
6420 /* Display it as active. */
6421 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6423 /* Handle the text property case. */
6424 else if (! NILP (mouse_face))
6426 /* Find the range of text around this char that
6427 should be active. */
6428 Lisp_Object before, after, beginning, end;
6429 int ignore;
6431 beginning = Fmarker_position (w->start);
6432 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6433 - XFASTINT (w->window_end_pos)));
6434 before
6435 = Fprevious_single_property_change (make_number (pos + 1),
6436 Qmouse_face,
6437 w->buffer, beginning);
6438 after
6439 = Fnext_single_property_change (position, Qmouse_face,
6440 w->buffer, end);
6441 /* Record this as the current active region. */
6442 fast_find_position (w, XFASTINT (before),
6443 &dpyinfo->mouse_face_beg_col,
6444 &dpyinfo->mouse_face_beg_row,
6445 &dpyinfo->mouse_face_beg_x,
6446 &dpyinfo->mouse_face_beg_y);
6447 dpyinfo->mouse_face_past_end
6448 = !fast_find_position (w, XFASTINT (after),
6449 &dpyinfo->mouse_face_end_col,
6450 &dpyinfo->mouse_face_end_row,
6451 &dpyinfo->mouse_face_end_x,
6452 &dpyinfo->mouse_face_end_y);
6453 dpyinfo->mouse_face_window = window;
6454 dpyinfo->mouse_face_face_id
6455 = face_at_buffer_position (w, pos, 0, 0,
6456 &ignore, pos + 1, 1);
6458 /* Display it as active. */
6459 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6463 /* Look for a `help-echo' property. */
6465 Lisp_Object help;
6467 /* Check overlays first. */
6468 help = Qnil;
6469 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6470 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6472 /* Try text properties. */
6473 if (!STRINGP (help)
6474 && ((STRINGP (glyph->object)
6475 && glyph->charpos >= 0
6476 && glyph->charpos < XSTRING (glyph->object)->size)
6477 || (BUFFERP (glyph->object)
6478 && glyph->charpos >= BEGV
6479 && glyph->charpos < ZV)))
6480 help = Fget_text_property (make_number (glyph->charpos),
6481 Qhelp_echo, glyph->object);
6483 if (STRINGP (help))
6484 help_echo = help;
6487 BEGV = obegv;
6488 ZV = ozv;
6489 current_buffer = obuf;
6494 static void
6495 redo_mouse_highlight ()
6497 if (!NILP (last_mouse_motion_frame)
6498 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6499 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6500 last_mouse_motion_event.x,
6501 last_mouse_motion_event.y);
6506 /***********************************************************************
6507 Tool-bars
6508 ***********************************************************************/
6510 static int x_tool_bar_item P_ ((struct frame *, int, int,
6511 struct glyph **, int *, int *, int *));
6513 /* Tool-bar item index of the item on which a mouse button was pressed
6514 or -1. */
6516 static int last_tool_bar_item;
6519 /* Get information about the tool-bar item at position X/Y on frame F.
6520 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6521 the current matrix of the tool-bar window of F, or NULL if not
6522 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6523 item in F->current_tool_bar_items. Value is
6525 -1 if X/Y is not on a tool-bar item
6526 0 if X/Y is on the same item that was highlighted before.
6527 1 otherwise. */
6529 static int
6530 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6531 struct frame *f;
6532 int x, y;
6533 struct glyph **glyph;
6534 int *hpos, *vpos, *prop_idx;
6536 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6537 struct window *w = XWINDOW (f->tool_bar_window);
6538 int area;
6540 /* Find the glyph under X/Y. */
6541 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6542 if (*glyph == NULL)
6543 return -1;
6545 /* Get the start of this tool-bar item's properties in
6546 f->current_tool_bar_items. */
6547 if (!tool_bar_item_info (f, *glyph, prop_idx))
6548 return -1;
6550 /* Is mouse on the highlighted item? */
6551 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6552 && *vpos >= dpyinfo->mouse_face_beg_row
6553 && *vpos <= dpyinfo->mouse_face_end_row
6554 && (*vpos > dpyinfo->mouse_face_beg_row
6555 || *hpos >= dpyinfo->mouse_face_beg_col)
6556 && (*vpos < dpyinfo->mouse_face_end_row
6557 || *hpos < dpyinfo->mouse_face_end_col
6558 || dpyinfo->mouse_face_past_end))
6559 return 0;
6561 return 1;
6565 /* Handle mouse button event on the tool-bar of frame F, at
6566 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6567 or ButtonRelase. */
6569 static void
6570 x_handle_tool_bar_click (f, button_event)
6571 struct frame *f;
6572 XButtonEvent *button_event;
6574 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6575 struct window *w = XWINDOW (f->tool_bar_window);
6576 int hpos, vpos, prop_idx;
6577 struct glyph *glyph;
6578 Lisp_Object enabled_p;
6579 int x = button_event->x;
6580 int y = button_event->y;
6582 /* If not on the highlighted tool-bar item, return. */
6583 frame_to_window_pixel_xy (w, &x, &y);
6584 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6585 return;
6587 /* If item is disabled, do nothing. */
6588 enabled_p = (XVECTOR (f->current_tool_bar_items)
6589 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6590 if (NILP (enabled_p))
6591 return;
6593 if (button_event->type == ButtonPress)
6595 /* Show item in pressed state. */
6596 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6597 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6598 last_tool_bar_item = prop_idx;
6600 else
6602 Lisp_Object key, frame;
6603 struct input_event event;
6605 /* Show item in released state. */
6606 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6607 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6609 key = (XVECTOR (f->current_tool_bar_items)
6610 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6612 XSETFRAME (frame, f);
6613 event.kind = TOOL_BAR_EVENT;
6614 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
6615 kbd_buffer_store_event (&event);
6617 event.kind = TOOL_BAR_EVENT;
6618 event.frame_or_window = Fcons (frame, key);
6619 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6620 button_event->state);
6621 kbd_buffer_store_event (&event);
6622 last_tool_bar_item = -1;
6627 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6628 tool-bar window-relative coordinates X/Y. Called from
6629 note_mouse_highlight. */
6631 static void
6632 note_tool_bar_highlight (f, x, y)
6633 struct frame *f;
6634 int x, y;
6636 Lisp_Object window = f->tool_bar_window;
6637 struct window *w = XWINDOW (window);
6638 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6639 int hpos, vpos;
6640 struct glyph *glyph;
6641 struct glyph_row *row;
6642 int i;
6643 Lisp_Object enabled_p;
6644 int prop_idx;
6645 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6646 int mouse_down_p, rc;
6648 /* Function note_mouse_highlight is called with negative x(y
6649 values when mouse moves outside of the frame. */
6650 if (x <= 0 || y <= 0)
6652 clear_mouse_face (dpyinfo);
6653 return;
6656 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6657 if (rc < 0)
6659 /* Not on tool-bar item. */
6660 clear_mouse_face (dpyinfo);
6661 return;
6663 else if (rc == 0)
6664 /* On same tool-bar item as before. */
6665 goto set_help_echo;
6667 clear_mouse_face (dpyinfo);
6669 /* Mouse is down, but on different tool-bar item? */
6670 mouse_down_p = (dpyinfo->grabbed
6671 && f == last_mouse_frame
6672 && FRAME_LIVE_P (f));
6673 if (mouse_down_p
6674 && last_tool_bar_item != prop_idx)
6675 return;
6677 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6678 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6680 /* If tool-bar item is not enabled, don't highlight it. */
6681 enabled_p = (XVECTOR (f->current_tool_bar_items)
6682 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6683 if (!NILP (enabled_p))
6685 /* Compute the x-position of the glyph. In front and past the
6686 image is a space. We include this is the highlighted area. */
6687 row = MATRIX_ROW (w->current_matrix, vpos);
6688 for (i = x = 0; i < hpos; ++i)
6689 x += row->glyphs[TEXT_AREA][i].pixel_width;
6691 /* Record this as the current active region. */
6692 dpyinfo->mouse_face_beg_col = hpos;
6693 dpyinfo->mouse_face_beg_row = vpos;
6694 dpyinfo->mouse_face_beg_x = x;
6695 dpyinfo->mouse_face_beg_y = row->y;
6696 dpyinfo->mouse_face_past_end = 0;
6698 dpyinfo->mouse_face_end_col = hpos + 1;
6699 dpyinfo->mouse_face_end_row = vpos;
6700 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6701 dpyinfo->mouse_face_end_y = row->y;
6702 dpyinfo->mouse_face_window = window;
6703 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6705 /* Display it as active. */
6706 show_mouse_face (dpyinfo, draw);
6707 dpyinfo->mouse_face_image_state = draw;
6710 set_help_echo:
6712 /* Set help_echo to a help string.to display for this tool-bar item.
6713 XTread_socket does the rest. */
6714 help_echo = (XVECTOR (f->current_tool_bar_items)
6715 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6716 if (!STRINGP (help_echo))
6717 help_echo = (XVECTOR (f->current_tool_bar_items)
6718 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6723 /* Find the glyph matrix position of buffer position POS in window W.
6724 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6725 current glyphs must be up to date. If POS is above window start
6726 return (0, 0, 0, 0). If POS is after end of W, return end of
6727 last line in W. */
6729 static int
6730 fast_find_position (w, pos, hpos, vpos, x, y)
6731 struct window *w;
6732 int pos;
6733 int *hpos, *vpos, *x, *y;
6735 int i;
6736 int lastcol;
6737 int maybe_next_line_p = 0;
6738 int line_start_position;
6739 int yb = window_text_bottom_y (w);
6740 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6741 struct glyph_row *best_row = row;
6742 int row_vpos = 0, best_row_vpos = 0;
6743 int current_x;
6745 while (row->y < yb)
6747 if (row->used[TEXT_AREA])
6748 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6749 else
6750 line_start_position = 0;
6752 if (line_start_position > pos)
6753 break;
6754 /* If the position sought is the end of the buffer,
6755 don't include the blank lines at the bottom of the window. */
6756 else if (line_start_position == pos
6757 && pos == BUF_ZV (XBUFFER (w->buffer)))
6759 maybe_next_line_p = 1;
6760 break;
6762 else if (line_start_position > 0)
6764 best_row = row;
6765 best_row_vpos = row_vpos;
6768 ++row;
6769 ++row_vpos;
6772 /* Find the right column within BEST_ROW. */
6773 lastcol = 0;
6774 current_x = best_row->x;
6775 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6777 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6778 int charpos;
6780 charpos = glyph->charpos;
6781 if (charpos == pos)
6783 *hpos = i;
6784 *vpos = best_row_vpos;
6785 *x = current_x;
6786 *y = best_row->y;
6787 return 1;
6789 else if (charpos > pos)
6790 break;
6791 else if (charpos > 0)
6792 lastcol = i;
6794 current_x += glyph->pixel_width;
6797 /* If we're looking for the end of the buffer,
6798 and we didn't find it in the line we scanned,
6799 use the start of the following line. */
6800 if (maybe_next_line_p)
6802 ++best_row;
6803 ++best_row_vpos;
6804 lastcol = 0;
6805 current_x = best_row->x;
6808 *vpos = best_row_vpos;
6809 *hpos = lastcol + 1;
6810 *x = current_x;
6811 *y = best_row->y;
6812 return 0;
6816 /* Display the active region described by mouse_face_*
6817 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6819 static void
6820 show_mouse_face (dpyinfo, draw)
6821 struct x_display_info *dpyinfo;
6822 enum draw_glyphs_face draw;
6824 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
6825 struct frame *f = XFRAME (WINDOW_FRAME (w));
6826 int i;
6827 int cursor_off_p = 0;
6828 struct cursor_pos saved_cursor;
6830 saved_cursor = output_cursor;
6832 /* If window is in the process of being destroyed, don't bother
6833 to do anything. */
6834 if (w->current_matrix == NULL)
6835 goto set_x_cursor;
6837 /* Recognize when we are called to operate on rows that don't exist
6838 anymore. This can happen when a window is split. */
6839 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6840 goto set_x_cursor;
6842 set_output_cursor (&w->phys_cursor);
6844 /* Note that mouse_face_beg_row etc. are window relative. */
6845 for (i = dpyinfo->mouse_face_beg_row;
6846 i <= dpyinfo->mouse_face_end_row;
6847 i++)
6849 int start_hpos, end_hpos, start_x;
6850 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6852 /* Don't do anything if row doesn't have valid contents. */
6853 if (!row->enabled_p)
6854 continue;
6856 /* For all but the first row, the highlight starts at column 0. */
6857 if (i == dpyinfo->mouse_face_beg_row)
6859 start_hpos = dpyinfo->mouse_face_beg_col;
6860 start_x = dpyinfo->mouse_face_beg_x;
6862 else
6864 start_hpos = 0;
6865 start_x = 0;
6868 if (i == dpyinfo->mouse_face_end_row)
6869 end_hpos = dpyinfo->mouse_face_end_col;
6870 else
6871 end_hpos = row->used[TEXT_AREA];
6873 /* If the cursor's in the text we are about to rewrite, turn the
6874 cursor off. */
6875 if (!w->pseudo_window_p
6876 && i == output_cursor.vpos
6877 && output_cursor.hpos >= start_hpos - 1
6878 && output_cursor.hpos <= end_hpos)
6880 x_update_window_cursor (w, 0);
6881 cursor_off_p = 1;
6884 if (end_hpos > start_hpos)
6885 x_draw_glyphs (w, start_x, row, TEXT_AREA,
6886 start_hpos, end_hpos, draw, NULL, NULL, 0);
6889 /* If we turned the cursor off, turn it back on. */
6890 if (cursor_off_p)
6891 x_display_cursor (w, 1,
6892 output_cursor.hpos, output_cursor.vpos,
6893 output_cursor.x, output_cursor.y);
6895 output_cursor = saved_cursor;
6897 set_x_cursor:
6899 /* Change the mouse cursor. */
6900 if (draw == DRAW_NORMAL_TEXT)
6901 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6902 f->output_data.x->text_cursor);
6903 else if (draw == DRAW_MOUSE_FACE)
6904 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6905 f->output_data.x->cross_cursor);
6906 else
6907 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6908 f->output_data.x->nontext_cursor);
6911 /* Clear out the mouse-highlighted active region.
6912 Redraw it un-highlighted first. */
6914 void
6915 clear_mouse_face (dpyinfo)
6916 struct x_display_info *dpyinfo;
6918 if (tip_frame)
6919 return;
6921 if (! NILP (dpyinfo->mouse_face_window))
6922 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
6924 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6925 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6926 dpyinfo->mouse_face_window = Qnil;
6929 /* Just discard the mouse face information for frame F, if any.
6930 This is used when the size of F is changed. */
6932 void
6933 cancel_mouse_face (f)
6934 FRAME_PTR f;
6936 Lisp_Object window;
6937 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6939 window = dpyinfo->mouse_face_window;
6940 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
6942 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
6943 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
6944 dpyinfo->mouse_face_window = Qnil;
6948 static struct scroll_bar *x_window_to_scroll_bar ();
6949 static void x_scroll_bar_report_motion ();
6951 /* Return the current position of the mouse.
6952 *fp should be a frame which indicates which display to ask about.
6954 If the mouse movement started in a scroll bar, set *fp, *bar_window,
6955 and *part to the frame, window, and scroll bar part that the mouse
6956 is over. Set *x and *y to the portion and whole of the mouse's
6957 position on the scroll bar.
6959 If the mouse movement started elsewhere, set *fp to the frame the
6960 mouse is on, *bar_window to nil, and *x and *y to the character cell
6961 the mouse is over.
6963 Set *time to the server time-stamp for the time at which the mouse
6964 was at this position.
6966 Don't store anything if we don't have a valid set of values to report.
6968 This clears the mouse_moved flag, so we can wait for the next mouse
6969 movement. */
6971 static void
6972 XTmouse_position (fp, insist, bar_window, part, x, y, time)
6973 FRAME_PTR *fp;
6974 int insist;
6975 Lisp_Object *bar_window;
6976 enum scroll_bar_part *part;
6977 Lisp_Object *x, *y;
6978 unsigned long *time;
6980 FRAME_PTR f1;
6982 BLOCK_INPUT;
6984 if (! NILP (last_mouse_scroll_bar) && insist == 0)
6985 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
6986 else
6988 Window root;
6989 int root_x, root_y;
6991 Window dummy_window;
6992 int dummy;
6994 Lisp_Object frame, tail;
6996 /* Clear the mouse-moved flag for every frame on this display. */
6997 FOR_EACH_FRAME (tail, frame)
6998 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
6999 XFRAME (frame)->mouse_moved = 0;
7001 last_mouse_scroll_bar = Qnil;
7003 /* Figure out which root window we're on. */
7004 XQueryPointer (FRAME_X_DISPLAY (*fp),
7005 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7007 /* The root window which contains the pointer. */
7008 &root,
7010 /* Trash which we can't trust if the pointer is on
7011 a different screen. */
7012 &dummy_window,
7014 /* The position on that root window. */
7015 &root_x, &root_y,
7017 /* More trash we can't trust. */
7018 &dummy, &dummy,
7020 /* Modifier keys and pointer buttons, about which
7021 we don't care. */
7022 (unsigned int *) &dummy);
7024 /* Now we have a position on the root; find the innermost window
7025 containing the pointer. */
7027 Window win, child;
7028 int win_x, win_y;
7029 int parent_x = 0, parent_y = 0;
7030 int count;
7032 win = root;
7034 /* XTranslateCoordinates can get errors if the window
7035 structure is changing at the same time this function
7036 is running. So at least we must not crash from them. */
7038 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7040 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7041 && FRAME_LIVE_P (last_mouse_frame))
7043 /* If mouse was grabbed on a frame, give coords for that frame
7044 even if the mouse is now outside it. */
7045 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7047 /* From-window, to-window. */
7048 root, FRAME_X_WINDOW (last_mouse_frame),
7050 /* From-position, to-position. */
7051 root_x, root_y, &win_x, &win_y,
7053 /* Child of win. */
7054 &child);
7055 f1 = last_mouse_frame;
7057 else
7059 while (1)
7061 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7063 /* From-window, to-window. */
7064 root, win,
7066 /* From-position, to-position. */
7067 root_x, root_y, &win_x, &win_y,
7069 /* Child of win. */
7070 &child);
7072 if (child == None || child == win)
7073 break;
7075 win = child;
7076 parent_x = win_x;
7077 parent_y = win_y;
7080 /* Now we know that:
7081 win is the innermost window containing the pointer
7082 (XTC says it has no child containing the pointer),
7083 win_x and win_y are the pointer's position in it
7084 (XTC did this the last time through), and
7085 parent_x and parent_y are the pointer's position in win's parent.
7086 (They are what win_x and win_y were when win was child.
7087 If win is the root window, it has no parent, and
7088 parent_{x,y} are invalid, but that's okay, because we'll
7089 never use them in that case.) */
7091 /* Is win one of our frames? */
7092 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7095 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7096 f1 = 0;
7098 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7100 /* If not, is it one of our scroll bars? */
7101 if (! f1)
7103 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7105 if (bar)
7107 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7108 win_x = parent_x;
7109 win_y = parent_y;
7113 if (f1 == 0 && insist > 0)
7114 f1 = SELECTED_FRAME ();
7116 if (f1)
7118 /* Ok, we found a frame. Store all the values.
7119 last_mouse_glyph is a rectangle used to reduce the
7120 generation of mouse events. To not miss any motion
7121 events, we must divide the frame into rectangles of the
7122 size of the smallest character that could be displayed
7123 on it, i.e. into the same rectangles that matrices on
7124 the frame are divided into. */
7126 #if OLD_REDISPLAY_CODE
7127 int ignore1, ignore2;
7128 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7129 &last_mouse_glyph,
7130 FRAME_X_DISPLAY_INFO (f1)->grabbed
7131 || insist);
7132 #else
7134 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7135 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7136 int x = win_x;
7137 int y = win_y;
7139 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7140 round down even for negative values. */
7141 if (x < 0)
7142 x -= width - 1;
7143 if (y < 0)
7144 y -= height - 1;
7146 last_mouse_glyph.width = width;
7147 last_mouse_glyph.height = height;
7148 last_mouse_glyph.x = (x + width - 1) / width * width;
7149 last_mouse_glyph.y = (y + height - 1) / height * height;
7151 #endif
7153 *bar_window = Qnil;
7154 *part = 0;
7155 *fp = f1;
7156 XSETINT (*x, win_x);
7157 XSETINT (*y, win_y);
7158 *time = last_mouse_movement_time;
7163 UNBLOCK_INPUT;
7167 #ifdef USE_X_TOOLKIT
7169 /* Atimer callback function for TIMER. Called every 0.1s to process
7170 Xt timeouts, if needed. We must avoid calling XtAppPending as
7171 much as possible because that function does an implicit XFlush
7172 that slows us down. */
7174 static void
7175 x_process_timeouts (timer)
7176 struct atimer *timer;
7178 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7180 BLOCK_INPUT;
7181 while (XtAppPending (Xt_app_con) & XtIMTimer)
7182 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7183 UNBLOCK_INPUT;
7187 #endif /* USE_X_TOOLKIT */
7190 /* Scroll bar support. */
7192 /* Given an X window ID, find the struct scroll_bar which manages it.
7193 This can be called in GC, so we have to make sure to strip off mark
7194 bits. */
7196 static struct scroll_bar *
7197 x_window_to_scroll_bar (window_id)
7198 Window window_id;
7200 Lisp_Object tail;
7202 for (tail = Vframe_list;
7203 XGCTYPE (tail) == Lisp_Cons;
7204 tail = XCDR (tail))
7206 Lisp_Object frame, bar, condemned;
7208 frame = XCAR (tail);
7209 /* All elements of Vframe_list should be frames. */
7210 if (! GC_FRAMEP (frame))
7211 abort ();
7213 /* Scan this frame's scroll bar list for a scroll bar with the
7214 right window ID. */
7215 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7216 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7217 /* This trick allows us to search both the ordinary and
7218 condemned scroll bar lists with one loop. */
7219 ! GC_NILP (bar) || (bar = condemned,
7220 condemned = Qnil,
7221 ! GC_NILP (bar));
7222 bar = XSCROLL_BAR (bar)->next)
7223 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7224 return XSCROLL_BAR (bar);
7227 return 0;
7232 /************************************************************************
7233 Toolkit scroll bars
7234 ************************************************************************/
7236 #if USE_TOOLKIT_SCROLL_BARS
7238 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7239 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7240 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7241 struct scroll_bar *));
7242 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7243 int, int, int));
7246 /* Id of action hook installed for scroll bars. */
7248 static XtActionHookId action_hook_id;
7250 /* Lisp window being scrolled. Set when starting to interact with
7251 a toolkit scroll bar, reset to nil when ending the interaction. */
7253 static Lisp_Object window_being_scrolled;
7255 /* Last scroll bar part sent in xm_scroll_callback. */
7257 static int last_scroll_bar_part;
7259 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7260 that movements of 1/20 of the screen size are mapped to up/down. */
7262 static Boolean xaw3d_arrow_scroll;
7264 /* Whether the drag scrolling maintains the mouse at the top of the
7265 thumb. If not, resizing the thumb needs to be done more carefully
7266 to avoid jerkyness. */
7268 static Boolean xaw3d_pick_top;
7271 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7272 bars are used.. The hook is responsible for detecting when
7273 the user ends an interaction with the scroll bar, and generates
7274 a `end-scroll' scroll_bar_click' event if so. */
7276 static void
7277 xt_action_hook (widget, client_data, action_name, event, params,
7278 num_params)
7279 Widget widget;
7280 XtPointer client_data;
7281 String action_name;
7282 XEvent *event;
7283 String *params;
7284 Cardinal *num_params;
7286 int scroll_bar_p;
7287 char *end_action;
7289 #ifdef USE_MOTIF
7290 scroll_bar_p = XmIsScrollBar (widget);
7291 end_action = "Release";
7292 #else /* !USE_MOTIF i.e. use Xaw */
7293 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7294 end_action = "EndScroll";
7295 #endif /* USE_MOTIF */
7297 if (scroll_bar_p
7298 && strcmp (action_name, end_action) == 0
7299 && WINDOWP (window_being_scrolled))
7301 struct window *w;
7303 x_send_scroll_bar_event (window_being_scrolled,
7304 scroll_bar_end_scroll, 0, 0);
7305 w = XWINDOW (window_being_scrolled);
7306 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7307 window_being_scrolled = Qnil;
7308 last_scroll_bar_part = -1;
7310 /* Xt timeouts no longer needed. */
7311 toolkit_scroll_bar_interaction = 0;
7316 /* Send a client message with message type Xatom_Scrollbar for a
7317 scroll action to the frame of WINDOW. PART is a value identifying
7318 the part of the scroll bar that was clicked on. PORTION is the
7319 amount to scroll of a whole of WHOLE. */
7321 static void
7322 x_send_scroll_bar_event (window, part, portion, whole)
7323 Lisp_Object window;
7324 int part, portion, whole;
7326 XEvent event;
7327 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7328 struct frame *f = XFRAME (XWINDOW (window)->frame);
7330 /* Construct a ClientMessage event to send to the frame. */
7331 ev->type = ClientMessage;
7332 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7333 ev->display = FRAME_X_DISPLAY (f);
7334 ev->window = FRAME_X_WINDOW (f);
7335 ev->format = 32;
7336 ev->data.l[0] = (long) window;
7337 ev->data.l[1] = (long) part;
7338 ev->data.l[2] = (long) 0;
7339 ev->data.l[3] = (long) portion;
7340 ev->data.l[4] = (long) whole;
7342 /* Make Xt timeouts work while the scroll bar is active. */
7343 toolkit_scroll_bar_interaction = 1;
7345 /* Setting the event mask to zero means that the message will
7346 be sent to the client that created the window, and if that
7347 window no longer exists, no event will be sent. */
7348 BLOCK_INPUT;
7349 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7350 UNBLOCK_INPUT;
7354 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7355 in *IEVENT. */
7357 static void
7358 x_scroll_bar_to_input_event (event, ievent)
7359 XEvent *event;
7360 struct input_event *ievent;
7362 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7363 Lisp_Object window = (Lisp_Object) ev->data.l[0];
7364 struct frame *f = XFRAME (XWINDOW (window)->frame);
7366 ievent->kind = scroll_bar_click;
7367 ievent->frame_or_window = window;
7368 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7369 ievent->part = ev->data.l[1];
7370 ievent->code = ev->data.l[2];
7371 ievent->x = make_number ((int) ev->data.l[3]);
7372 ievent->y = make_number ((int) ev->data.l[4]);
7373 ievent->modifiers = 0;
7377 #ifdef USE_MOTIF
7379 /* Minimum and maximum values used for Motif scroll bars. */
7381 #define XM_SB_MIN 1
7382 #define XM_SB_MAX 10000000
7383 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7386 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7387 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7388 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7390 static void
7391 xm_scroll_callback (widget, client_data, call_data)
7392 Widget widget;
7393 XtPointer client_data, call_data;
7395 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7396 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7397 double percent;
7398 int part = -1, whole = 0, portion = 0;
7400 switch (cs->reason)
7402 case XmCR_DECREMENT:
7403 bar->dragging = Qnil;
7404 part = scroll_bar_up_arrow;
7405 break;
7407 case XmCR_INCREMENT:
7408 bar->dragging = Qnil;
7409 part = scroll_bar_down_arrow;
7410 break;
7412 case XmCR_PAGE_DECREMENT:
7413 bar->dragging = Qnil;
7414 part = scroll_bar_above_handle;
7415 break;
7417 case XmCR_PAGE_INCREMENT:
7418 bar->dragging = Qnil;
7419 part = scroll_bar_below_handle;
7420 break;
7422 case XmCR_TO_TOP:
7423 bar->dragging = Qnil;
7424 part = scroll_bar_to_top;
7425 break;
7427 case XmCR_TO_BOTTOM:
7428 bar->dragging = Qnil;
7429 part = scroll_bar_to_bottom;
7430 break;
7432 case XmCR_DRAG:
7434 int slider_size;
7435 int dragging_down_p = (INTEGERP (bar->dragging)
7436 && XINT (bar->dragging) <= cs->value);
7438 /* Get the slider size. */
7439 BLOCK_INPUT;
7440 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7441 UNBLOCK_INPUT;
7443 /* At the max position of the scroll bar, do a line-wise
7444 movement. Without doing anything, the LessTif scroll bar
7445 calls us with the same cs->value again and again. If we
7446 want to make sure that we can reach the end of the buffer,
7447 we have to do something.
7449 Implementation note: setting bar->dragging always to
7450 cs->value gives a smoother movement at the max position.
7451 Setting it to nil when doing line-wise movement gives
7452 a better slider behavior. */
7454 if (cs->value + slider_size == XM_SB_MAX
7455 || (dragging_down_p
7456 && last_scroll_bar_part == scroll_bar_down_arrow))
7458 part = scroll_bar_down_arrow;
7459 bar->dragging = Qnil;
7461 else
7463 whole = XM_SB_RANGE;
7464 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7465 part = scroll_bar_handle;
7466 bar->dragging = make_number (cs->value);
7469 break;
7471 case XmCR_VALUE_CHANGED:
7472 break;
7475 if (part >= 0)
7477 window_being_scrolled = bar->window;
7478 last_scroll_bar_part = part;
7479 x_send_scroll_bar_event (bar->window, part, portion, whole);
7484 #else /* !USE_MOTIF, i.e. Xaw. */
7487 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7488 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7489 scroll bar struct. CALL_DATA is a pointer to a float saying where
7490 the thumb is. */
7492 static void
7493 xaw_jump_callback (widget, client_data, call_data)
7494 Widget widget;
7495 XtPointer client_data, call_data;
7497 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7498 float top = *(float *) call_data;
7499 float shown;
7500 int whole, portion, height;
7501 int part;
7503 /* Get the size of the thumb, a value between 0 and 1. */
7504 BLOCK_INPUT;
7505 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7506 UNBLOCK_INPUT;
7508 whole = 10000000;
7509 portion = shown < 1 ? top * whole : 0;
7511 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7512 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7513 the bottom, so we force the scrolling whenever we see that we're
7514 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7515 we try to ensure that we always stay two pixels away from the
7516 bottom). */
7517 part = scroll_bar_down_arrow;
7518 else
7519 part = scroll_bar_handle;
7521 window_being_scrolled = bar->window;
7522 bar->dragging = make_number (portion);
7523 last_scroll_bar_part = part;
7524 x_send_scroll_bar_event (bar->window, part, portion, whole);
7528 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7529 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7530 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7531 the scroll bar. CALL_DATA is an integer specifying the action that
7532 has taken place. It's magnitude is in the range 0..height of the
7533 scroll bar. Negative values mean scroll towards buffer start.
7534 Values < height of scroll bar mean line-wise movement. */
7536 static void
7537 xaw_scroll_callback (widget, client_data, call_data)
7538 Widget widget;
7539 XtPointer client_data, call_data;
7541 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7542 int position = (int) call_data;
7543 Dimension height;
7544 int part;
7546 /* Get the height of the scroll bar. */
7547 BLOCK_INPUT;
7548 XtVaGetValues (widget, XtNheight, &height, NULL);
7549 UNBLOCK_INPUT;
7551 if (abs (position) >= height)
7552 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7554 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7555 it maps line-movement to call_data = max(5, height/20). */
7556 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7557 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7558 else
7559 part = scroll_bar_move_ratio;
7561 window_being_scrolled = bar->window;
7562 bar->dragging = Qnil;
7563 last_scroll_bar_part = part;
7564 x_send_scroll_bar_event (bar->window, part, position, height);
7568 #endif /* not USE_MOTIF */
7571 /* Create the widget for scroll bar BAR on frame F. Record the widget
7572 and X window of the scroll bar in BAR. */
7574 static void
7575 x_create_toolkit_scroll_bar (f, bar)
7576 struct frame *f;
7577 struct scroll_bar *bar;
7579 Window xwindow;
7580 Widget widget;
7581 Arg av[20];
7582 int ac = 0;
7583 char *scroll_bar_name = "verticalScrollBar";
7584 unsigned long pixel;
7586 BLOCK_INPUT;
7588 #ifdef USE_MOTIF
7589 /* LessTif 0.85, problems:
7591 1. When the mouse if over the scroll bar, the scroll bar will
7592 get keyboard events. I didn't find a way to turn this off.
7594 2. Do we have to explicitly set the cursor to get an arrow
7595 cursor (see below)? */
7597 /* Set resources. Create the widget. */
7598 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7599 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7600 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7601 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7602 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7603 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7604 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7606 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7607 if (pixel != -1)
7609 XtSetArg (av[ac], XmNforeground, pixel);
7610 ++ac;
7613 pixel = f->output_data.x->scroll_bar_background_pixel;
7614 if (pixel != -1)
7616 XtSetArg (av[ac], XmNbackground, pixel);
7617 ++ac;
7620 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7621 scroll_bar_name, av, ac);
7623 /* Add one callback for everything that can happen. */
7624 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7625 (XtPointer) bar);
7626 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7627 (XtPointer) bar);
7628 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7629 (XtPointer) bar);
7630 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7631 (XtPointer) bar);
7632 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7633 (XtPointer) bar);
7634 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7635 (XtPointer) bar);
7636 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7637 (XtPointer) bar);
7639 /* Realize the widget. Only after that is the X window created. */
7640 XtRealizeWidget (widget);
7642 /* Set the cursor to an arrow. I didn't find a resource to do that.
7643 And I'm wondering why it hasn't an arrow cursor by default. */
7644 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7645 f->output_data.x->nontext_cursor);
7647 #else /* !USE_MOTIF i.e. use Xaw */
7649 /* Set resources. Create the widget. The background of the
7650 Xaw3d scroll bar widget is a little bit light for my taste.
7651 We don't alter it here to let users change it according
7652 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7653 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7654 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7655 /* For smoother scrolling with Xaw3d -sm */
7656 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7657 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
7659 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7660 if (pixel != -1)
7662 XtSetArg (av[ac], XtNforeground, pixel);
7663 ++ac;
7666 pixel = f->output_data.x->scroll_bar_background_pixel;
7667 if (pixel != -1)
7669 XtSetArg (av[ac], XtNbackground, pixel);
7670 ++ac;
7673 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7674 f->output_data.x->edit_widget, av, ac);
7677 char *initial = "";
7678 char *val = initial;
7679 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7680 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7681 if (val == initial)
7682 { /* ARROW_SCROLL */
7683 xaw3d_arrow_scroll = True;
7684 /* Isn't that just a personal preference ? -sm */
7685 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7689 /* Define callbacks. */
7690 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7691 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
7692 (XtPointer) bar);
7694 /* Realize the widget. Only after that is the X window created. */
7695 XtRealizeWidget (widget);
7697 #endif /* !USE_MOTIF */
7699 /* Install an action hook that let's us detect when the user
7700 finishes interacting with a scroll bar. */
7701 if (action_hook_id == 0)
7702 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7704 /* Remember X window and widget in the scroll bar vector. */
7705 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7706 xwindow = XtWindow (widget);
7707 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7709 UNBLOCK_INPUT;
7713 /* Set the thumb size and position of scroll bar BAR. We are currently
7714 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7716 static void
7717 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7718 struct scroll_bar *bar;
7719 int portion, position, whole;
7721 float top, shown;
7722 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7724 if (whole == 0)
7725 top = 0, shown = 1;
7726 else
7728 top = (float) position / whole;
7729 shown = (float) portion / whole;
7732 BLOCK_INPUT;
7734 #ifdef USE_MOTIF
7736 int size, value;
7737 Boolean arrow1_selected, arrow2_selected;
7738 unsigned char flags;
7739 XmScrollBarWidget sb;
7741 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
7742 is the scroll bar's maximum and MIN is the scroll bar's minimum
7743 value. */
7744 size = shown * XM_SB_RANGE;
7745 size = min (size, XM_SB_RANGE);
7746 size = max (size, 1);
7748 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7749 value = top * XM_SB_RANGE;
7750 value = min (value, XM_SB_MAX - size);
7751 value = max (value, XM_SB_MIN);
7753 /* LessTif: Calling XmScrollBarSetValues after an increment or
7754 decrement turns off auto-repeat LessTif-internally. This can
7755 be seen in ScrollBar.c which resets Arrow1Selected and
7756 Arrow2Selected. It also sets internal flags so that LessTif
7757 believes the mouse is in the slider. We either have to change
7758 our code, or work around that by accessing private data. */
7760 sb = (XmScrollBarWidget) widget;
7761 arrow1_selected = sb->scrollBar.arrow1_selected;
7762 arrow2_selected = sb->scrollBar.arrow2_selected;
7763 flags = sb->scrollBar.flags;
7765 if (NILP (bar->dragging))
7766 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7767 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7768 /* This has the negative side effect that the slider value is
7769 not what it would be if we scrolled here using line-wise or
7770 page-wise movement. */
7771 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7772 else
7774 /* If currently dragging, only update the slider size.
7775 This reduces flicker effects. */
7776 int old_value, old_size, increment, page_increment;
7778 XmScrollBarGetValues (widget, &old_value, &old_size,
7779 &increment, &page_increment);
7780 XmScrollBarSetValues (widget, old_value,
7781 min (size, XM_SB_RANGE - old_value),
7782 0, 0, False);
7785 sb->scrollBar.arrow1_selected = arrow1_selected;
7786 sb->scrollBar.arrow2_selected = arrow2_selected;
7787 sb->scrollBar.flags = flags;
7789 #else /* !USE_MOTIF i.e. use Xaw */
7791 float old_top, old_shown;
7792 Dimension height;
7793 XtVaGetValues (widget,
7794 XtNtopOfThumb, &old_top,
7795 XtNshown, &old_shown,
7796 XtNheight, &height,
7797 NULL);
7799 /* Massage the top+shown values. */
7800 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7801 top = max (0, min (1, top));
7802 else
7803 top = old_top;
7804 /* Keep two pixels available for moving the thumb down. */
7805 shown = max (0, min (1 - top - (2.0 / height), shown));
7807 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7808 check that your system's configuration file contains a define
7809 for `NARROWPROTO'. See s/freebsd.h for an example. */
7810 if (top != old_top || shown != old_shown)
7812 if (NILP (bar->dragging))
7813 XawScrollbarSetThumb (widget, top, shown);
7814 else
7816 #ifdef HAVE_XAW3D
7817 ScrollbarWidget sb = (ScrollbarWidget) widget;
7818 int scroll_mode;
7820 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7821 if (xaw3d_arrow_scroll)
7823 /* Xaw3d stupidly ignores resize requests while dragging
7824 so we have to make it believe it's not in dragging mode. */
7825 scroll_mode = sb->scrollbar.scroll_mode;
7826 if (scroll_mode == 2)
7827 sb->scrollbar.scroll_mode = 0;
7829 #endif
7830 /* Try to make the scrolling a tad smoother. */
7831 if (!xaw3d_pick_top)
7832 shown = min (shown, old_shown);
7834 XawScrollbarSetThumb (widget, top, shown);
7836 #ifdef HAVE_XAW3D
7837 if (xaw3d_arrow_scroll && scroll_mode == 2)
7838 sb->scrollbar.scroll_mode = scroll_mode;
7839 #endif
7843 #endif /* !USE_MOTIF */
7845 UNBLOCK_INPUT;
7848 #endif /* USE_TOOLKIT_SCROLL_BARS */
7852 /************************************************************************
7853 Scroll bars, general
7854 ************************************************************************/
7856 /* Create a scroll bar and return the scroll bar vector for it. W is
7857 the Emacs window on which to create the scroll bar. TOP, LEFT,
7858 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7859 scroll bar. */
7861 static struct scroll_bar *
7862 x_scroll_bar_create (w, top, left, width, height)
7863 struct window *w;
7864 int top, left, width, height;
7866 struct frame *f = XFRAME (w->frame);
7867 struct scroll_bar *bar
7868 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
7870 BLOCK_INPUT;
7872 #if USE_TOOLKIT_SCROLL_BARS
7873 x_create_toolkit_scroll_bar (f, bar);
7874 #else /* not USE_TOOLKIT_SCROLL_BARS */
7876 XSetWindowAttributes a;
7877 unsigned long mask;
7878 Window window;
7880 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7881 if (a.background_pixel == -1)
7882 a.background_pixel = f->output_data.x->background_pixel;
7884 a.event_mask = (ButtonPressMask | ButtonReleaseMask
7885 | ButtonMotionMask | PointerMotionHintMask
7886 | ExposureMask);
7887 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
7889 mask = (CWBackPixel | CWEventMask | CWCursor);
7891 /* Clear the area of W that will serve as a scroll bar. This is
7892 for the case that a window has been split horizontally. In
7893 this case, no clear_frame is generated to reduce flickering. */
7894 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7895 left, top, width,
7896 window_box_height (w), False);
7898 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7899 /* Position and size of scroll bar. */
7900 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7901 top,
7902 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7903 height,
7904 /* Border width, depth, class, and visual. */
7906 CopyFromParent,
7907 CopyFromParent,
7908 CopyFromParent,
7909 /* Attributes. */
7910 mask, &a);
7911 SET_SCROLL_BAR_X_WINDOW (bar, window);
7913 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7915 XSETWINDOW (bar->window, w);
7916 XSETINT (bar->top, top);
7917 XSETINT (bar->left, left);
7918 XSETINT (bar->width, width);
7919 XSETINT (bar->height, height);
7920 XSETINT (bar->start, 0);
7921 XSETINT (bar->end, 0);
7922 bar->dragging = Qnil;
7924 /* Add bar to its frame's list of scroll bars. */
7925 bar->next = FRAME_SCROLL_BARS (f);
7926 bar->prev = Qnil;
7927 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
7928 if (!NILP (bar->next))
7929 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
7931 /* Map the window/widget. */
7932 #if USE_TOOLKIT_SCROLL_BARS
7933 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
7934 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
7935 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
7936 top,
7937 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
7938 height, 0);
7939 #else /* not USE_TOOLKIT_SCROLL_BARS */
7940 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
7941 #endif /* not USE_TOOLKIT_SCROLL_BARS */
7943 UNBLOCK_INPUT;
7944 return bar;
7948 /* Draw BAR's handle in the proper position.
7950 If the handle is already drawn from START to END, don't bother
7951 redrawing it, unless REBUILD is non-zero; in that case, always
7952 redraw it. (REBUILD is handy for drawing the handle after expose
7953 events.)
7955 Normally, we want to constrain the start and end of the handle to
7956 fit inside its rectangle, but if the user is dragging the scroll
7957 bar handle, we want to let them drag it down all the way, so that
7958 the bar's top is as far down as it goes; otherwise, there's no way
7959 to move to the very end of the buffer. */
7961 #ifndef USE_TOOLKIT_SCROLL_BARS
7963 static void
7964 x_scroll_bar_set_handle (bar, start, end, rebuild)
7965 struct scroll_bar *bar;
7966 int start, end;
7967 int rebuild;
7969 int dragging = ! NILP (bar->dragging);
7970 Window w = SCROLL_BAR_X_WINDOW (bar);
7971 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7972 GC gc = f->output_data.x->normal_gc;
7974 /* If the display is already accurate, do nothing. */
7975 if (! rebuild
7976 && start == XINT (bar->start)
7977 && end == XINT (bar->end))
7978 return;
7980 BLOCK_INPUT;
7983 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
7984 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
7985 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
7987 /* Make sure the values are reasonable, and try to preserve
7988 the distance between start and end. */
7990 int length = end - start;
7992 if (start < 0)
7993 start = 0;
7994 else if (start > top_range)
7995 start = top_range;
7996 end = start + length;
7998 if (end < start)
7999 end = start;
8000 else if (end > top_range && ! dragging)
8001 end = top_range;
8004 /* Store the adjusted setting in the scroll bar. */
8005 XSETINT (bar->start, start);
8006 XSETINT (bar->end, end);
8008 /* Clip the end position, just for display. */
8009 if (end > top_range)
8010 end = top_range;
8012 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8013 below top positions, to make sure the handle is always at least
8014 that many pixels tall. */
8015 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8017 /* Draw the empty space above the handle. Note that we can't clear
8018 zero-height areas; that means "clear to end of window." */
8019 if (0 < start)
8020 XClearArea (FRAME_X_DISPLAY (f), w,
8022 /* x, y, width, height, and exposures. */
8023 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8024 VERTICAL_SCROLL_BAR_TOP_BORDER,
8025 inside_width, start,
8026 False);
8028 /* Change to proper foreground color if one is specified. */
8029 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8030 XSetForeground (FRAME_X_DISPLAY (f), gc,
8031 f->output_data.x->scroll_bar_foreground_pixel);
8033 /* Draw the handle itself. */
8034 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8036 /* x, y, width, height */
8037 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8038 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8039 inside_width, end - start);
8041 /* Restore the foreground color of the GC if we changed it above. */
8042 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8043 XSetForeground (FRAME_X_DISPLAY (f), gc,
8044 f->output_data.x->foreground_pixel);
8046 /* Draw the empty space below the handle. Note that we can't
8047 clear zero-height areas; that means "clear to end of window." */
8048 if (end < inside_height)
8049 XClearArea (FRAME_X_DISPLAY (f), w,
8051 /* x, y, width, height, and exposures. */
8052 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8053 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8054 inside_width, inside_height - end,
8055 False);
8059 UNBLOCK_INPUT;
8062 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8064 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8065 nil. */
8067 static void
8068 x_scroll_bar_remove (bar)
8069 struct scroll_bar *bar;
8071 BLOCK_INPUT;
8073 #if USE_TOOLKIT_SCROLL_BARS
8074 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8075 #else /* not USE_TOOLKIT_SCROLL_BARS */
8077 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8078 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8080 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8082 /* Disassociate this scroll bar from its window. */
8083 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8085 UNBLOCK_INPUT;
8089 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8090 that we are displaying PORTION characters out of a total of WHOLE
8091 characters, starting at POSITION. If WINDOW has no scroll bar,
8092 create one. */
8094 static void
8095 XTset_vertical_scroll_bar (w, portion, whole, position)
8096 struct window *w;
8097 int portion, whole, position;
8099 struct frame *f = XFRAME (w->frame);
8100 struct scroll_bar *bar;
8101 int top, height, left, sb_left, width, sb_width;
8102 int window_x, window_y, window_width, window_height;
8104 /* Get window dimensions. */
8105 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8106 top = window_y;
8107 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8108 height = window_height;
8110 /* Compute the left edge of the scroll bar area. */
8111 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8112 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8113 else
8114 left = XFASTINT (w->left);
8115 left *= CANON_X_UNIT (f);
8116 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8118 /* Compute the width of the scroll bar which might be less than
8119 the width of the area reserved for the scroll bar. */
8120 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8121 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8122 else
8123 sb_width = width;
8125 /* Compute the left edge of the scroll bar. */
8126 #ifdef USE_TOOLKIT_SCROLL_BARS
8127 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8128 sb_left = left + width - sb_width - (width - sb_width) / 2;
8129 else
8130 sb_left = left + (width - sb_width) / 2;
8131 #else
8132 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8133 sb_left = left + width - sb_width;
8134 else
8135 sb_left = left;
8136 #endif
8138 /* Does the scroll bar exist yet? */
8139 if (NILP (w->vertical_scroll_bar))
8141 BLOCK_INPUT;
8142 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8143 left, top, width, height, False);
8144 UNBLOCK_INPUT;
8145 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8147 else
8149 /* It may just need to be moved and resized. */
8150 unsigned int mask = 0;
8152 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8154 BLOCK_INPUT;
8156 if (sb_left != XINT (bar->left))
8157 mask |= CWX;
8158 if (top != XINT (bar->top))
8159 mask |= CWY;
8160 if (sb_width != XINT (bar->width))
8161 mask |= CWWidth;
8162 if (height != XINT (bar->height))
8163 mask |= CWHeight;
8165 #ifdef USE_TOOLKIT_SCROLL_BARS
8167 /* Since toolkit scroll bars are smaller than the space reserved
8168 for them on the frame, we have to clear "under" them. */
8169 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8170 left, top, width, height, False);
8172 /* Move/size the scroll bar widget. */
8173 if (mask)
8174 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8175 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8176 top,
8177 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8178 height, 0);
8180 #else /* not USE_TOOLKIT_SCROLL_BARS */
8182 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8184 /* Clear areas not covered by the scroll bar. This makes sure a
8185 previous mode line display is cleared after C-x 2 C-x 1, for
8186 example. Non-toolkit scroll bars are as wide as the area
8187 reserved for scroll bars - trim at both sides. */
8188 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8189 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8190 height, False);
8191 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8192 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8193 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8194 height, False);
8197 /* Move/size the scroll bar window. */
8198 if (mask)
8200 XWindowChanges wc;
8202 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8203 wc.y = top;
8204 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8205 wc.height = height;
8206 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8207 mask, &wc);
8210 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8212 /* Remember new settings. */
8213 XSETINT (bar->left, sb_left);
8214 XSETINT (bar->top, top);
8215 XSETINT (bar->width, sb_width);
8216 XSETINT (bar->height, height);
8218 UNBLOCK_INPUT;
8221 #if USE_TOOLKIT_SCROLL_BARS
8222 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8223 #else /* not USE_TOOLKIT_SCROLL_BARS */
8224 /* Set the scroll bar's current state, unless we're currently being
8225 dragged. */
8226 if (NILP (bar->dragging))
8228 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8230 if (whole == 0)
8231 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8232 else
8234 int start = ((double) position * top_range) / whole;
8235 int end = ((double) (position + portion) * top_range) / whole;
8236 x_scroll_bar_set_handle (bar, start, end, 0);
8239 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8241 XSETVECTOR (w->vertical_scroll_bar, bar);
8245 /* The following three hooks are used when we're doing a thorough
8246 redisplay of the frame. We don't explicitly know which scroll bars
8247 are going to be deleted, because keeping track of when windows go
8248 away is a real pain - "Can you say set-window-configuration, boys
8249 and girls?" Instead, we just assert at the beginning of redisplay
8250 that *all* scroll bars are to be removed, and then save a scroll bar
8251 from the fiery pit when we actually redisplay its window. */
8253 /* Arrange for all scroll bars on FRAME to be removed at the next call
8254 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8255 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8257 static void
8258 XTcondemn_scroll_bars (frame)
8259 FRAME_PTR frame;
8261 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8262 while (! NILP (FRAME_SCROLL_BARS (frame)))
8264 Lisp_Object bar;
8265 bar = FRAME_SCROLL_BARS (frame);
8266 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8267 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8268 XSCROLL_BAR (bar)->prev = Qnil;
8269 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8270 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8271 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8275 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8276 Note that WINDOW isn't necessarily condemned at all. */
8277 static void
8278 XTredeem_scroll_bar (window)
8279 struct window *window;
8281 struct scroll_bar *bar;
8283 /* We can't redeem this window's scroll bar if it doesn't have one. */
8284 if (NILP (window->vertical_scroll_bar))
8285 abort ();
8287 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8289 /* Unlink it from the condemned list. */
8291 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8293 if (NILP (bar->prev))
8295 /* If the prev pointer is nil, it must be the first in one of
8296 the lists. */
8297 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8298 /* It's not condemned. Everything's fine. */
8299 return;
8300 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8301 window->vertical_scroll_bar))
8302 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8303 else
8304 /* If its prev pointer is nil, it must be at the front of
8305 one or the other! */
8306 abort ();
8308 else
8309 XSCROLL_BAR (bar->prev)->next = bar->next;
8311 if (! NILP (bar->next))
8312 XSCROLL_BAR (bar->next)->prev = bar->prev;
8314 bar->next = FRAME_SCROLL_BARS (f);
8315 bar->prev = Qnil;
8316 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8317 if (! NILP (bar->next))
8318 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8322 /* Remove all scroll bars on FRAME that haven't been saved since the
8323 last call to `*condemn_scroll_bars_hook'. */
8325 static void
8326 XTjudge_scroll_bars (f)
8327 FRAME_PTR f;
8329 Lisp_Object bar, next;
8331 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8333 /* Clear out the condemned list now so we won't try to process any
8334 more events on the hapless scroll bars. */
8335 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8337 for (; ! NILP (bar); bar = next)
8339 struct scroll_bar *b = XSCROLL_BAR (bar);
8341 x_scroll_bar_remove (b);
8343 next = b->next;
8344 b->next = b->prev = Qnil;
8347 /* Now there should be no references to the condemned scroll bars,
8348 and they should get garbage-collected. */
8352 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8353 is a no-op when using toolkit scroll bars.
8355 This may be called from a signal handler, so we have to ignore GC
8356 mark bits. */
8358 static void
8359 x_scroll_bar_expose (bar, event)
8360 struct scroll_bar *bar;
8361 XEvent *event;
8363 #ifndef USE_TOOLKIT_SCROLL_BARS
8365 Window w = SCROLL_BAR_X_WINDOW (bar);
8366 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8367 GC gc = f->output_data.x->normal_gc;
8368 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8370 BLOCK_INPUT;
8372 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8374 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8375 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8377 /* x, y, width, height */
8378 0, 0,
8379 XINT (bar->width) - 1 - width_trim - width_trim,
8380 XINT (bar->height) - 1);
8382 UNBLOCK_INPUT;
8384 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8387 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8388 is set to something other than no_event, it is enqueued.
8390 This may be called from a signal handler, so we have to ignore GC
8391 mark bits. */
8393 #ifndef USE_TOOLKIT_SCROLL_BARS
8395 static void
8396 x_scroll_bar_handle_click (bar, event, emacs_event)
8397 struct scroll_bar *bar;
8398 XEvent *event;
8399 struct input_event *emacs_event;
8401 if (! GC_WINDOWP (bar->window))
8402 abort ();
8404 emacs_event->kind = scroll_bar_click;
8405 emacs_event->code = event->xbutton.button - Button1;
8406 emacs_event->modifiers
8407 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8408 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8409 event->xbutton.state)
8410 | (event->type == ButtonRelease
8411 ? up_modifier
8412 : down_modifier));
8413 emacs_event->frame_or_window = bar->window;
8414 emacs_event->timestamp = event->xbutton.time;
8416 #if 0
8417 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8418 int internal_height
8419 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8420 #endif
8421 int top_range
8422 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8423 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8425 if (y < 0) y = 0;
8426 if (y > top_range) y = top_range;
8428 if (y < XINT (bar->start))
8429 emacs_event->part = scroll_bar_above_handle;
8430 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8431 emacs_event->part = scroll_bar_handle;
8432 else
8433 emacs_event->part = scroll_bar_below_handle;
8435 /* Just because the user has clicked on the handle doesn't mean
8436 they want to drag it. Lisp code needs to be able to decide
8437 whether or not we're dragging. */
8438 #if 0
8439 /* If the user has just clicked on the handle, record where they're
8440 holding it. */
8441 if (event->type == ButtonPress
8442 && emacs_event->part == scroll_bar_handle)
8443 XSETINT (bar->dragging, y - XINT (bar->start));
8444 #endif
8446 /* If the user has released the handle, set it to its final position. */
8447 if (event->type == ButtonRelease
8448 && ! NILP (bar->dragging))
8450 int new_start = y - XINT (bar->dragging);
8451 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8453 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8454 bar->dragging = Qnil;
8457 /* Same deal here as the other #if 0. */
8458 #if 0
8459 /* Clicks on the handle are always reported as occurring at the top of
8460 the handle. */
8461 if (emacs_event->part == scroll_bar_handle)
8462 emacs_event->x = bar->start;
8463 else
8464 XSETINT (emacs_event->x, y);
8465 #else
8466 XSETINT (emacs_event->x, y);
8467 #endif
8469 XSETINT (emacs_event->y, top_range);
8473 /* Handle some mouse motion while someone is dragging the scroll bar.
8475 This may be called from a signal handler, so we have to ignore GC
8476 mark bits. */
8478 static void
8479 x_scroll_bar_note_movement (bar, event)
8480 struct scroll_bar *bar;
8481 XEvent *event;
8483 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8485 last_mouse_movement_time = event->xmotion.time;
8487 f->mouse_moved = 1;
8488 XSETVECTOR (last_mouse_scroll_bar, bar);
8490 /* If we're dragging the bar, display it. */
8491 if (! GC_NILP (bar->dragging))
8493 /* Where should the handle be now? */
8494 int new_start = event->xmotion.y - XINT (bar->dragging);
8496 if (new_start != XINT (bar->start))
8498 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8500 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8505 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8507 /* Return information to the user about the current position of the mouse
8508 on the scroll bar. */
8510 static void
8511 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8512 FRAME_PTR *fp;
8513 Lisp_Object *bar_window;
8514 enum scroll_bar_part *part;
8515 Lisp_Object *x, *y;
8516 unsigned long *time;
8518 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8519 Window w = SCROLL_BAR_X_WINDOW (bar);
8520 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8521 int win_x, win_y;
8522 Window dummy_window;
8523 int dummy_coord;
8524 unsigned int dummy_mask;
8526 BLOCK_INPUT;
8528 /* Get the mouse's position relative to the scroll bar window, and
8529 report that. */
8530 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8532 /* Root, child, root x and root y. */
8533 &dummy_window, &dummy_window,
8534 &dummy_coord, &dummy_coord,
8536 /* Position relative to scroll bar. */
8537 &win_x, &win_y,
8539 /* Mouse buttons and modifier keys. */
8540 &dummy_mask))
8542 else
8544 #if 0
8545 int inside_height
8546 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8547 #endif
8548 int top_range
8549 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8551 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8553 if (! NILP (bar->dragging))
8554 win_y -= XINT (bar->dragging);
8556 if (win_y < 0)
8557 win_y = 0;
8558 if (win_y > top_range)
8559 win_y = top_range;
8561 *fp = f;
8562 *bar_window = bar->window;
8564 if (! NILP (bar->dragging))
8565 *part = scroll_bar_handle;
8566 else if (win_y < XINT (bar->start))
8567 *part = scroll_bar_above_handle;
8568 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8569 *part = scroll_bar_handle;
8570 else
8571 *part = scroll_bar_below_handle;
8573 XSETINT (*x, win_y);
8574 XSETINT (*y, top_range);
8576 f->mouse_moved = 0;
8577 last_mouse_scroll_bar = Qnil;
8580 *time = last_mouse_movement_time;
8582 UNBLOCK_INPUT;
8586 /* The screen has been cleared so we may have changed foreground or
8587 background colors, and the scroll bars may need to be redrawn.
8588 Clear out the scroll bars, and ask for expose events, so we can
8589 redraw them. */
8591 void
8592 x_scroll_bar_clear (f)
8593 FRAME_PTR f;
8595 #ifndef USE_TOOLKIT_SCROLL_BARS
8596 Lisp_Object bar;
8598 /* We can have scroll bars even if this is 0,
8599 if we just turned off scroll bar mode.
8600 But in that case we should not clear them. */
8601 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8602 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8603 bar = XSCROLL_BAR (bar)->next)
8604 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8605 0, 0, 0, 0, True);
8606 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8609 /* This processes Expose events from the menu-bar specific X event
8610 loop in xmenu.c. This allows to redisplay the frame if necessary
8611 when handling menu-bar or pop-up items. */
8614 process_expose_from_menu (event)
8615 XEvent event;
8617 FRAME_PTR f;
8618 struct x_display_info *dpyinfo;
8619 int frame_exposed_p = 0;
8621 BLOCK_INPUT;
8623 dpyinfo = x_display_info_for_display (event.xexpose.display);
8624 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8625 if (f)
8627 if (f->async_visible == 0)
8629 f->async_visible = 1;
8630 f->async_iconified = 0;
8631 f->output_data.x->has_been_visible = 1;
8632 SET_FRAME_GARBAGED (f);
8634 else
8636 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8637 event.xexpose.x, event.xexpose.y,
8638 event.xexpose.width, event.xexpose.height);
8639 frame_exposed_p = 1;
8642 else
8644 struct scroll_bar *bar
8645 = x_window_to_scroll_bar (event.xexpose.window);
8647 if (bar)
8648 x_scroll_bar_expose (bar, &event);
8651 UNBLOCK_INPUT;
8652 return frame_exposed_p;
8655 /* Define a queue to save up SelectionRequest events for later handling. */
8657 struct selection_event_queue
8659 XEvent event;
8660 struct selection_event_queue *next;
8663 static struct selection_event_queue *queue;
8665 /* Nonzero means queue up certain events--don't process them yet. */
8667 static int x_queue_selection_requests;
8669 /* Queue up an X event *EVENT, to be processed later. */
8671 static void
8672 x_queue_event (f, event)
8673 FRAME_PTR f;
8674 XEvent *event;
8676 struct selection_event_queue *queue_tmp
8677 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8679 if (queue_tmp != NULL)
8681 queue_tmp->event = *event;
8682 queue_tmp->next = queue;
8683 queue = queue_tmp;
8687 /* Take all the queued events and put them back
8688 so that they get processed afresh. */
8690 static void
8691 x_unqueue_events (display)
8692 Display *display;
8694 while (queue != NULL)
8696 struct selection_event_queue *queue_tmp = queue;
8697 XPutBackEvent (display, &queue_tmp->event);
8698 queue = queue_tmp->next;
8699 xfree ((char *)queue_tmp);
8703 /* Start queuing SelectionRequest events. */
8705 void
8706 x_start_queuing_selection_requests (display)
8707 Display *display;
8709 x_queue_selection_requests++;
8712 /* Stop queuing SelectionRequest events. */
8714 void
8715 x_stop_queuing_selection_requests (display)
8716 Display *display;
8718 x_queue_selection_requests--;
8719 x_unqueue_events (display);
8722 /* The main X event-reading loop - XTread_socket. */
8724 /* Time stamp of enter window event. This is only used by XTread_socket,
8725 but we have to put it out here, since static variables within functions
8726 sometimes don't work. */
8728 static Time enter_timestamp;
8730 /* This holds the state XLookupString needs to implement dead keys
8731 and other tricks known as "compose processing". _X Window System_
8732 says that a portable program can't use this, but Stephen Gildea assures
8733 me that letting the compiler initialize it to zeros will work okay.
8735 This must be defined outside of XTread_socket, for the same reasons
8736 given for enter_time stamp, above. */
8738 static XComposeStatus compose_status;
8740 /* Record the last 100 characters stored
8741 to help debug the loss-of-chars-during-GC problem. */
8743 static int temp_index;
8744 static short temp_buffer[100];
8746 /* Set this to nonzero to fake an "X I/O error"
8747 on a particular display. */
8749 struct x_display_info *XTread_socket_fake_io_error;
8751 /* When we find no input here, we occasionally do a no-op command
8752 to verify that the X server is still running and we can still talk with it.
8753 We try all the open displays, one by one.
8754 This variable is used for cycling thru the displays. */
8756 static struct x_display_info *next_noop_dpyinfo;
8758 #define SET_SAVED_MENU_EVENT(size) \
8759 do \
8761 if (f->output_data.x->saved_menu_event == 0) \
8762 f->output_data.x->saved_menu_event \
8763 = (XEvent *) xmalloc (sizeof (XEvent)); \
8764 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8765 if (numchars >= 1) \
8767 bufp->kind = menu_bar_activate_event; \
8768 XSETFRAME (bufp->frame_or_window, f); \
8769 bufp++; \
8770 count++; \
8771 numchars--; \
8774 while (0)
8776 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
8777 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8779 /* Read events coming from the X server.
8780 This routine is called by the SIGIO handler.
8781 We return as soon as there are no more events to be read.
8783 Events representing keys are stored in buffer BUFP,
8784 which can hold up to NUMCHARS characters.
8785 We return the number of characters stored into the buffer,
8786 thus pretending to be `read'.
8788 EXPECTED is nonzero if the caller knows input is available. */
8791 XTread_socket (sd, bufp, numchars, expected)
8792 register int sd;
8793 /* register */ struct input_event *bufp;
8794 /* register */ int numchars;
8795 int expected;
8797 int count = 0;
8798 int nbytes = 0;
8799 XEvent event;
8800 struct frame *f;
8801 int event_found = 0;
8802 struct x_display_info *dpyinfo;
8804 if (interrupt_input_blocked)
8806 interrupt_input_pending = 1;
8807 return -1;
8810 interrupt_input_pending = 0;
8811 BLOCK_INPUT;
8813 /* So people can tell when we have read the available input. */
8814 input_signal_count++;
8816 if (numchars <= 0)
8817 abort (); /* Don't think this happens. */
8819 /* Find the display we are supposed to read input for.
8820 It's the one communicating on descriptor SD. */
8821 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8823 #if 0 /* This ought to be unnecessary; let's verify it. */
8824 #ifdef FIOSNBIO
8825 /* If available, Xlib uses FIOSNBIO to make the socket
8826 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
8827 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
8828 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
8829 fcntl (dpyinfo->connection, F_SETFL, 0);
8830 #endif /* ! defined (FIOSNBIO) */
8831 #endif
8833 #if 0 /* This code can't be made to work, with multiple displays,
8834 and appears not to be used on any system any more.
8835 Also keyboard.c doesn't turn O_NDELAY on and off
8836 for X connections. */
8837 #ifndef SIGIO
8838 #ifndef HAVE_SELECT
8839 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8841 extern int read_alarm_should_throw;
8842 read_alarm_should_throw = 1;
8843 XPeekEvent (dpyinfo->display, &event);
8844 read_alarm_should_throw = 0;
8846 #endif /* HAVE_SELECT */
8847 #endif /* SIGIO */
8848 #endif
8850 /* For debugging, this gives a way to fake an I/O error. */
8851 if (dpyinfo == XTread_socket_fake_io_error)
8853 XTread_socket_fake_io_error = 0;
8854 x_io_error_quitter (dpyinfo->display);
8857 while (XPending (dpyinfo->display))
8859 XNextEvent (dpyinfo->display, &event);
8861 #ifdef HAVE_X_I18N
8863 /* Filter events for the current X input method.
8864 XFilterEvent returns non-zero if the input method has
8865 consumed the event. We pass the frame's X window to
8866 XFilterEvent because that's the one for which the IC
8867 was created. */
8868 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8869 event.xclient.window);
8870 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
8871 break;
8873 #endif
8874 event_found = 1;
8876 switch (event.type)
8878 case ClientMessage:
8880 if (event.xclient.message_type
8881 == dpyinfo->Xatom_wm_protocols
8882 && event.xclient.format == 32)
8884 if (event.xclient.data.l[0]
8885 == dpyinfo->Xatom_wm_take_focus)
8887 /* Use x_any_window_to_frame because this
8888 could be the shell widget window
8889 if the frame has no title bar. */
8890 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
8891 #ifdef HAVE_X_I18N
8892 /* Not quite sure this is needed -pd */
8893 if (f && FRAME_XIC (f))
8894 XSetICFocus (FRAME_XIC (f));
8895 #endif
8896 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
8897 instructs the WM to set the input focus automatically for
8898 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
8899 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
8900 it has set the focus. So, XSetInputFocus below is not
8901 needed.
8903 The call to XSetInputFocus below has also caused trouble. In
8904 cases where the XSetInputFocus done by the WM and the one
8905 below are temporally close (on a fast machine), the call
8906 below can generate additional FocusIn events which confuse
8907 Emacs. */
8909 /* Since we set WM_TAKE_FOCUS, we must call
8910 XSetInputFocus explicitly. But not if f is null,
8911 since that might be an event for a deleted frame. */
8912 if (f)
8914 Display *d = event.xclient.display;
8915 /* Catch and ignore errors, in case window has been
8916 iconified by a window manager such as GWM. */
8917 int count = x_catch_errors (d);
8918 XSetInputFocus (d, event.xclient.window,
8919 /* The ICCCM says this is
8920 the only valid choice. */
8921 RevertToParent,
8922 event.xclient.data.l[1]);
8923 /* This is needed to detect the error
8924 if there is an error. */
8925 XSync (d, False);
8926 x_uncatch_errors (d, count);
8928 /* Not certain about handling scroll bars here */
8929 #endif /* 0 */
8931 else if (event.xclient.data.l[0]
8932 == dpyinfo->Xatom_wm_save_yourself)
8934 /* Save state modify the WM_COMMAND property to
8935 something which can reinstate us. This notifies
8936 the session manager, who's looking for such a
8937 PropertyNotify. Can restart processing when
8938 a keyboard or mouse event arrives. */
8939 if (numchars > 0)
8941 f = x_top_window_to_frame (dpyinfo,
8942 event.xclient.window);
8944 /* This is just so we only give real data once
8945 for a single Emacs process. */
8946 if (f == SELECTED_FRAME ())
8947 XSetCommand (FRAME_X_DISPLAY (f),
8948 event.xclient.window,
8949 initial_argv, initial_argc);
8950 else if (f)
8951 XSetCommand (FRAME_X_DISPLAY (f),
8952 event.xclient.window,
8953 0, 0);
8956 else if (event.xclient.data.l[0]
8957 == dpyinfo->Xatom_wm_delete_window)
8959 struct frame *f
8960 = x_any_window_to_frame (dpyinfo,
8961 event.xclient.window);
8963 if (f)
8965 if (numchars == 0)
8966 abort ();
8968 bufp->kind = delete_window_event;
8969 XSETFRAME (bufp->frame_or_window, f);
8970 bufp++;
8972 count += 1;
8973 numchars -= 1;
8977 else if (event.xclient.message_type
8978 == dpyinfo->Xatom_wm_configure_denied)
8981 else if (event.xclient.message_type
8982 == dpyinfo->Xatom_wm_window_moved)
8984 int new_x, new_y;
8985 struct frame *f
8986 = x_window_to_frame (dpyinfo, event.xclient.window);
8988 new_x = event.xclient.data.s[0];
8989 new_y = event.xclient.data.s[1];
8991 if (f)
8993 f->output_data.x->left_pos = new_x;
8994 f->output_data.x->top_pos = new_y;
8997 #ifdef HACK_EDITRES
8998 else if (event.xclient.message_type
8999 == dpyinfo->Xatom_editres)
9001 struct frame *f
9002 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9003 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9004 &event, NULL);
9006 #endif /* HACK_EDITRES */
9007 else if ((event.xclient.message_type
9008 == dpyinfo->Xatom_DONE)
9009 || (event.xclient.message_type
9010 == dpyinfo->Xatom_PAGE))
9012 /* Ghostview job completed. Kill it. We could
9013 reply with "Next" if we received "Page", but we
9014 currently never do because we are interested in
9015 images, only, which should have 1 page. */
9016 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9017 struct frame *f
9018 = x_window_to_frame (dpyinfo, event.xclient.window);
9019 x_kill_gs_process (pixmap, f);
9020 expose_frame (f, 0, 0, 0, 0);
9022 #ifdef USE_TOOLKIT_SCROLL_BARS
9023 /* Scroll bar callbacks send a ClientMessage from which
9024 we construct an input_event. */
9025 else if (event.xclient.message_type
9026 == dpyinfo->Xatom_Scrollbar)
9028 x_scroll_bar_to_input_event (&event, bufp);
9029 ++bufp, ++count, --numchars;
9030 goto out;
9032 #endif /* USE_TOOLKIT_SCROLL_BARS */
9033 else
9034 goto OTHER;
9036 break;
9038 case SelectionNotify:
9039 #ifdef USE_X_TOOLKIT
9040 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9041 goto OTHER;
9042 #endif /* not USE_X_TOOLKIT */
9043 x_handle_selection_notify (&event.xselection);
9044 break;
9046 case SelectionClear: /* Someone has grabbed ownership. */
9047 #ifdef USE_X_TOOLKIT
9048 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9049 goto OTHER;
9050 #endif /* USE_X_TOOLKIT */
9052 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9054 if (numchars == 0)
9055 abort ();
9057 bufp->kind = selection_clear_event;
9058 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9059 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9060 SELECTION_EVENT_TIME (bufp) = eventp->time;
9061 bufp->frame_or_window = Qnil;
9062 bufp++;
9064 count += 1;
9065 numchars -= 1;
9067 break;
9069 case SelectionRequest: /* Someone wants our selection. */
9070 #ifdef USE_X_TOOLKIT
9071 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9072 goto OTHER;
9073 #endif /* USE_X_TOOLKIT */
9074 if (x_queue_selection_requests)
9075 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9076 &event);
9077 else
9079 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9081 if (numchars == 0)
9082 abort ();
9084 bufp->kind = selection_request_event;
9085 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9086 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9087 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9088 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9089 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9090 SELECTION_EVENT_TIME (bufp) = eventp->time;
9091 bufp->frame_or_window = Qnil;
9092 bufp++;
9094 count += 1;
9095 numchars -= 1;
9097 break;
9099 case PropertyNotify:
9100 #ifdef USE_X_TOOLKIT
9101 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9102 goto OTHER;
9103 #endif /* not USE_X_TOOLKIT */
9104 x_handle_property_notify (&event.xproperty);
9105 break;
9107 case ReparentNotify:
9108 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9109 if (f)
9111 int x, y;
9112 f->output_data.x->parent_desc = event.xreparent.parent;
9113 x_real_positions (f, &x, &y);
9114 f->output_data.x->left_pos = x;
9115 f->output_data.x->top_pos = y;
9117 break;
9119 case Expose:
9120 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9121 if (f)
9123 if (f->async_visible == 0)
9125 f->async_visible = 1;
9126 f->async_iconified = 0;
9127 f->output_data.x->has_been_visible = 1;
9128 SET_FRAME_GARBAGED (f);
9130 else
9131 expose_frame (x_window_to_frame (dpyinfo,
9132 event.xexpose.window),
9133 event.xexpose.x, event.xexpose.y,
9134 event.xexpose.width, event.xexpose.height);
9136 else
9138 #ifdef USE_TOOLKIT_SCROLL_BARS
9139 /* Dispatch event to the widget. */
9140 goto OTHER;
9141 #else /* not USE_TOOLKIT_SCROLL_BARS */
9142 struct scroll_bar *bar
9143 = x_window_to_scroll_bar (event.xexpose.window);
9145 if (bar)
9146 x_scroll_bar_expose (bar, &event);
9147 #ifdef USE_X_TOOLKIT
9148 else
9149 goto OTHER;
9150 #endif /* USE_X_TOOLKIT */
9151 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9153 break;
9155 case GraphicsExpose: /* This occurs when an XCopyArea's
9156 source area was obscured or not
9157 available.*/
9158 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9159 if (f)
9161 expose_frame (f,
9162 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9163 event.xgraphicsexpose.width,
9164 event.xgraphicsexpose.height);
9166 #ifdef USE_X_TOOLKIT
9167 else
9168 goto OTHER;
9169 #endif /* USE_X_TOOLKIT */
9170 break;
9172 case NoExpose: /* This occurs when an XCopyArea's
9173 source area was completely
9174 available */
9175 break;
9177 case UnmapNotify:
9178 /* Redo the mouse-highlight after the tooltip has gone. */
9179 if (event.xmap.window == tip_window)
9181 tip_window = 0;
9182 redo_mouse_highlight ();
9185 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9186 if (f) /* F may no longer exist if
9187 the frame was deleted. */
9189 /* While a frame is unmapped, display generation is
9190 disabled; you don't want to spend time updating a
9191 display that won't ever be seen. */
9192 f->async_visible = 0;
9193 /* We can't distinguish, from the event, whether the window
9194 has become iconified or invisible. So assume, if it
9195 was previously visible, than now it is iconified.
9196 But x_make_frame_invisible clears both
9197 the visible flag and the iconified flag;
9198 and that way, we know the window is not iconified now. */
9199 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9201 f->async_iconified = 1;
9203 bufp->kind = iconify_event;
9204 XSETFRAME (bufp->frame_or_window, f);
9205 bufp++;
9206 count++;
9207 numchars--;
9210 goto OTHER;
9212 case MapNotify:
9213 if (event.xmap.window == tip_window)
9214 /* The tooltip has been drawn already. Avoid
9215 the SET_FRAME_GARBAGED below. */
9216 goto OTHER;
9218 /* We use x_top_window_to_frame because map events can
9219 come for sub-windows and they don't mean that the
9220 frame is visible. */
9221 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9222 if (f)
9224 f->async_visible = 1;
9225 f->async_iconified = 0;
9226 f->output_data.x->has_been_visible = 1;
9228 /* wait_reading_process_input will notice this and update
9229 the frame's display structures. */
9230 SET_FRAME_GARBAGED (f);
9232 if (f->iconified)
9234 bufp->kind = deiconify_event;
9235 XSETFRAME (bufp->frame_or_window, f);
9236 bufp++;
9237 count++;
9238 numchars--;
9240 else if (! NILP (Vframe_list)
9241 && ! NILP (XCDR (Vframe_list)))
9242 /* Force a redisplay sooner or later
9243 to update the frame titles
9244 in case this is the second frame. */
9245 record_asynch_buffer_change ();
9247 goto OTHER;
9249 case KeyPress:
9250 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9252 #ifdef USE_MOTIF
9253 /* I couldn't find a way to prevent LessTif scroll bars
9254 from consuming key events. */
9255 if (f == 0)
9257 Widget widget = XtWindowToWidget (dpyinfo->display,
9258 event.xkey.window);
9259 if (widget && XmIsScrollBar (widget))
9261 widget = XtParent (widget);
9262 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9265 #endif /* USE_MOTIF */
9267 if (f != 0)
9269 KeySym keysym, orig_keysym;
9270 /* al%imercury@uunet.uu.net says that making this 81 instead of
9271 80 fixed a bug whereby meta chars made his Emacs hang. */
9272 unsigned char copy_buffer[81];
9273 int modifiers;
9275 event.xkey.state
9276 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9277 extra_keyboard_modifiers);
9278 modifiers = event.xkey.state;
9280 /* This will have to go some day... */
9282 /* make_lispy_event turns chars into control chars.
9283 Don't do it here because XLookupString is too eager. */
9284 event.xkey.state &= ~ControlMask;
9285 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9286 | dpyinfo->super_mod_mask
9287 | dpyinfo->hyper_mod_mask
9288 | dpyinfo->alt_mod_mask);
9290 /* In case Meta is ComposeCharacter,
9291 clear its status. According to Markus Ehrnsperger
9292 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9293 this enables ComposeCharacter to work whether or
9294 not it is combined with Meta. */
9295 if (modifiers & dpyinfo->meta_mod_mask)
9296 bzero (&compose_status, sizeof (compose_status));
9298 #ifdef HAVE_X_I18N
9299 if (FRAME_XIC (f))
9301 unsigned char *copy_bufptr = copy_buffer;
9302 int copy_bufsiz = sizeof (copy_buffer);
9303 Status status_return;
9305 nbytes = XmbLookupString (FRAME_XIC (f),
9306 &event.xkey, copy_bufptr,
9307 copy_bufsiz, &keysym,
9308 &status_return);
9309 if (status_return == XBufferOverflow)
9311 copy_bufsiz = nbytes + 1;
9312 copy_bufptr = (char *) alloca (copy_bufsiz);
9313 nbytes = XmbLookupString (FRAME_XIC (f),
9314 &event.xkey, copy_bufptr,
9315 copy_bufsiz, &keysym,
9316 &status_return);
9319 if (status_return == XLookupNone)
9320 break;
9321 else if (status_return == XLookupChars)
9322 keysym = NoSymbol;
9323 else if (status_return != XLookupKeySym
9324 && status_return != XLookupBoth)
9325 abort ();
9327 else
9328 nbytes = XLookupString (&event.xkey, copy_buffer,
9329 80, &keysym, &compose_status);
9330 #else
9331 nbytes = XLookupString (&event.xkey, copy_buffer,
9332 80, &keysym, &compose_status);
9333 #endif
9335 orig_keysym = keysym;
9337 if (numchars > 1)
9339 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9340 || keysym == XK_Delete
9341 #ifdef XK_ISO_Left_Tab
9342 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9343 #endif
9344 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9345 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9346 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9347 #ifdef HPUX
9348 /* This recognizes the "extended function keys".
9349 It seems there's no cleaner way.
9350 Test IsModifierKey to avoid handling mode_switch
9351 incorrectly. */
9352 || ((unsigned) (keysym) >= XK_Select
9353 && (unsigned)(keysym) < XK_KP_Space)
9354 #endif
9355 #ifdef XK_dead_circumflex
9356 || orig_keysym == XK_dead_circumflex
9357 #endif
9358 #ifdef XK_dead_grave
9359 || orig_keysym == XK_dead_grave
9360 #endif
9361 #ifdef XK_dead_tilde
9362 || orig_keysym == XK_dead_tilde
9363 #endif
9364 #ifdef XK_dead_diaeresis
9365 || orig_keysym == XK_dead_diaeresis
9366 #endif
9367 #ifdef XK_dead_macron
9368 || orig_keysym == XK_dead_macron
9369 #endif
9370 #ifdef XK_dead_degree
9371 || orig_keysym == XK_dead_degree
9372 #endif
9373 #ifdef XK_dead_acute
9374 || orig_keysym == XK_dead_acute
9375 #endif
9376 #ifdef XK_dead_cedilla
9377 || orig_keysym == XK_dead_cedilla
9378 #endif
9379 #ifdef XK_dead_breve
9380 || orig_keysym == XK_dead_breve
9381 #endif
9382 #ifdef XK_dead_ogonek
9383 || orig_keysym == XK_dead_ogonek
9384 #endif
9385 #ifdef XK_dead_caron
9386 || orig_keysym == XK_dead_caron
9387 #endif
9388 #ifdef XK_dead_doubleacute
9389 || orig_keysym == XK_dead_doubleacute
9390 #endif
9391 #ifdef XK_dead_abovedot
9392 || orig_keysym == XK_dead_abovedot
9393 #endif
9394 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9395 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9396 /* Any "vendor-specific" key is ok. */
9397 || (orig_keysym & (1 << 28)))
9398 && ! (IsModifierKey (orig_keysym)
9399 #ifndef HAVE_X11R5
9400 #ifdef XK_Mode_switch
9401 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9402 #endif
9403 #ifdef XK_Num_Lock
9404 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9405 #endif
9406 #endif /* not HAVE_X11R5 */
9409 if (temp_index == sizeof temp_buffer / sizeof (short))
9410 temp_index = 0;
9411 temp_buffer[temp_index++] = keysym;
9412 bufp->kind = non_ascii_keystroke;
9413 bufp->code = keysym;
9414 XSETFRAME (bufp->frame_or_window, f);
9415 bufp->modifiers
9416 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9417 modifiers);
9418 bufp->timestamp = event.xkey.time;
9419 bufp++;
9420 count++;
9421 numchars--;
9423 else if (numchars > nbytes)
9425 register int i;
9427 for (i = 0; i < nbytes; i++)
9429 if (temp_index == sizeof temp_buffer / sizeof (short))
9430 temp_index = 0;
9431 temp_buffer[temp_index++] = copy_buffer[i];
9432 bufp->kind = ascii_keystroke;
9433 bufp->code = copy_buffer[i];
9434 XSETFRAME (bufp->frame_or_window, f);
9435 bufp->modifiers
9436 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9437 modifiers);
9438 bufp->timestamp = event.xkey.time;
9439 bufp++;
9442 count += nbytes;
9443 numchars -= nbytes;
9445 if (keysym == NoSymbol)
9446 break;
9448 else
9449 abort ();
9451 else
9452 abort ();
9454 #ifdef HAVE_X_I18N
9455 /* Don't dispatch this event since XtDispatchEvent calls
9456 XFilterEvent, and two calls in a row may freeze the
9457 client. */
9458 break;
9459 #else
9460 goto OTHER;
9461 #endif
9463 case KeyRelease:
9464 #ifdef HAVE_X_I18N
9465 /* Don't dispatch this event since XtDispatchEvent calls
9466 XFilterEvent, and two calls in a row may freeze the
9467 client. */
9468 break;
9469 #else
9470 goto OTHER;
9471 #endif
9473 /* Here's a possible interpretation of the whole
9474 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9475 you get a FocusIn event, you have to get a FocusOut
9476 event before you relinquish the focus. If you
9477 haven't received a FocusIn event, then a mere
9478 LeaveNotify is enough to free you. */
9480 case EnterNotify:
9482 int from_menu_bar_p = 0;
9484 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9486 #ifdef LESSTIF_VERSION
9487 /* When clicking outside of a menu bar popup to close
9488 it, we get a FocusIn/ EnterNotify sequence of
9489 events. The flag event.xcrossing.focus is not set
9490 in the EnterNotify event of that sequence because
9491 the focus is in the menu bar,
9492 event.xcrossing.window is the frame's X window.
9493 Unconditionally setting the focus frame to null in
9494 this case is not the right thing, because no event
9495 follows that could set the focus frame to the right
9496 value.
9498 This could be a LessTif bug, but I wasn't able to
9499 reproduce the behavior in a simple test program.
9501 (gerd, LessTif 0.88.1). */
9503 if (!event.xcrossing.focus
9504 && f
9505 && f->output_data.x->menubar_widget)
9507 Window focus;
9508 int revert;
9510 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9511 if (focus == XtWindow (f->output_data.x->menubar_widget))
9512 from_menu_bar_p = 1;
9514 #endif /* LESSTIF_VERSION */
9516 if (event.xcrossing.focus || from_menu_bar_p)
9518 /* Avoid nasty pop/raise loops. */
9519 if (f && (!(f->auto_raise)
9520 || !(f->auto_lower)
9521 || (event.xcrossing.time - enter_timestamp) > 500))
9523 x_new_focus_frame (dpyinfo, f);
9524 enter_timestamp = event.xcrossing.time;
9527 else if (f == dpyinfo->x_focus_frame)
9528 x_new_focus_frame (dpyinfo, 0);
9530 /* EnterNotify counts as mouse movement,
9531 so update things that depend on mouse position. */
9532 if (f && !f->output_data.x->busy_p)
9533 note_mouse_movement (f, &event.xmotion);
9534 goto OTHER;
9537 case FocusIn:
9538 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9539 if (event.xfocus.detail != NotifyPointer)
9540 dpyinfo->x_focus_event_frame = f;
9541 if (f)
9542 x_new_focus_frame (dpyinfo, f);
9544 #ifdef HAVE_X_I18N
9545 if (f && FRAME_XIC (f))
9546 XSetICFocus (FRAME_XIC (f));
9547 #endif
9549 goto OTHER;
9551 case LeaveNotify:
9552 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9553 if (f)
9555 Lisp_Object frame;
9556 int from_menu_bar_p = 0;
9558 if (f == dpyinfo->mouse_face_mouse_frame)
9560 /* If we move outside the frame, then we're
9561 certainly no longer on any text in the frame. */
9562 clear_mouse_face (dpyinfo);
9563 dpyinfo->mouse_face_mouse_frame = 0;
9566 /* Generate a nil HELP_EVENT to cancel a help-echo.
9567 Do it only if there's something to cancel.
9568 Otherwise, the startup message is cleared when
9569 the mouse leaves the frame. */
9570 if (any_help_event_p)
9572 XSETFRAME (frame, f);
9573 bufp->kind = HELP_EVENT;
9574 bufp->frame_or_window = Fcons (frame, Qnil);
9575 ++bufp, ++count, --numchars;
9578 #ifdef LESSTIF_VERSION
9579 /* Please see the comment at the start of the
9580 EnterNotify case. */
9581 if (!event.xcrossing.focus
9582 && f->output_data.x->menubar_widget)
9584 Window focus;
9585 int revert;
9586 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9587 if (focus == XtWindow (f->output_data.x->menubar_widget))
9588 from_menu_bar_p = 1;
9590 #endif /* LESSTIF_VERSION */
9592 if (event.xcrossing.focus || from_menu_bar_p)
9593 x_mouse_leave (dpyinfo);
9594 else
9596 if (f == dpyinfo->x_focus_event_frame)
9597 dpyinfo->x_focus_event_frame = 0;
9598 if (f == dpyinfo->x_focus_frame)
9599 x_new_focus_frame (dpyinfo, 0);
9602 goto OTHER;
9604 case FocusOut:
9605 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9606 if (event.xfocus.detail != NotifyPointer
9607 && f == dpyinfo->x_focus_event_frame)
9608 dpyinfo->x_focus_event_frame = 0;
9609 if (f && f == dpyinfo->x_focus_frame)
9610 x_new_focus_frame (dpyinfo, 0);
9612 #ifdef HAVE_X_I18N
9613 if (f && FRAME_XIC (f))
9614 XUnsetICFocus (FRAME_XIC (f));
9615 #endif
9617 goto OTHER;
9619 case MotionNotify:
9621 previous_help_echo = help_echo;
9622 help_echo = Qnil;
9624 if (dpyinfo->grabbed && last_mouse_frame
9625 && FRAME_LIVE_P (last_mouse_frame))
9626 f = last_mouse_frame;
9627 else
9628 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9630 if (f)
9631 note_mouse_movement (f, &event.xmotion);
9632 else
9634 #ifndef USE_TOOLKIT_SCROLL_BARS
9635 struct scroll_bar *bar
9636 = x_window_to_scroll_bar (event.xmotion.window);
9638 if (bar)
9639 x_scroll_bar_note_movement (bar, &event);
9640 #endif /* USE_TOOLKIT_SCROLL_BARS */
9642 /* If we move outside the frame, then we're
9643 certainly no longer on any text in the frame. */
9644 clear_mouse_face (dpyinfo);
9647 /* If the contents of the global variable help_echo
9648 has changed, generate a HELP_EVENT. */
9649 if (STRINGP (help_echo)
9650 || STRINGP (previous_help_echo))
9652 Lisp_Object frame;
9654 if (f)
9655 XSETFRAME (frame, f);
9656 else
9657 frame = Qnil;
9659 any_help_event_p = 1;
9660 bufp->kind = HELP_EVENT;
9661 bufp->frame_or_window = Fcons (frame, help_echo);
9662 ++bufp, ++count, --numchars;
9665 goto OTHER;
9668 case ConfigureNotify:
9669 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9670 if (f)
9672 #ifndef USE_X_TOOLKIT
9673 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9674 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
9676 /* In the toolkit version, change_frame_size
9677 is called by the code that handles resizing
9678 of the EmacsFrame widget. */
9680 /* Even if the number of character rows and columns has
9681 not changed, the font size may have changed, so we need
9682 to check the pixel dimensions as well. */
9683 if (columns != f->width
9684 || rows != f->height
9685 || event.xconfigure.width != f->output_data.x->pixel_width
9686 || event.xconfigure.height != f->output_data.x->pixel_height)
9688 change_frame_size (f, rows, columns, 0, 1, 0);
9689 SET_FRAME_GARBAGED (f);
9690 cancel_mouse_face (f);
9692 #endif
9694 f->output_data.x->pixel_width = event.xconfigure.width;
9695 f->output_data.x->pixel_height = event.xconfigure.height;
9697 /* What we have now is the position of Emacs's own window.
9698 Convert that to the position of the window manager window. */
9699 x_real_positions (f, &f->output_data.x->left_pos,
9700 &f->output_data.x->top_pos);
9702 #ifdef HAVE_X_I18N
9703 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9704 xic_set_statusarea (f);
9705 #endif
9707 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9709 /* Since the WM decorations come below top_pos now,
9710 we must put them below top_pos in the future. */
9711 f->output_data.x->win_gravity = NorthWestGravity;
9712 x_wm_set_size_hint (f, (long) 0, 0);
9714 #ifdef USE_MOTIF
9715 /* Some window managers pass (0,0) as the location of
9716 the window, and the Motif event handler stores it
9717 in the emacs widget, which messes up Motif menus. */
9718 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9720 event.xconfigure.x = f->output_data.x->widget->core.x;
9721 event.xconfigure.y = f->output_data.x->widget->core.y;
9723 #endif /* USE_MOTIF */
9725 goto OTHER;
9727 case ButtonPress:
9728 case ButtonRelease:
9730 /* If we decide we want to generate an event to be seen
9731 by the rest of Emacs, we put it here. */
9732 struct input_event emacs_event;
9733 int tool_bar_p = 0;
9735 emacs_event.kind = no_event;
9736 bzero (&compose_status, sizeof (compose_status));
9738 if (dpyinfo->grabbed
9739 && last_mouse_frame
9740 && FRAME_LIVE_P (last_mouse_frame))
9741 f = last_mouse_frame;
9742 else
9743 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9745 if (f)
9747 /* Is this in the tool-bar? */
9748 if (WINDOWP (f->tool_bar_window)
9749 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
9751 Lisp_Object window;
9752 int p, x, y;
9754 x = event.xbutton.x;
9755 y = event.xbutton.y;
9757 /* Set x and y. */
9758 window = window_from_coordinates (f, x, y, &p, 1);
9759 if (EQ (window, f->tool_bar_window))
9761 x_handle_tool_bar_click (f, &event.xbutton);
9762 tool_bar_p = 1;
9766 if (!tool_bar_p)
9767 if (!dpyinfo->x_focus_frame
9768 || f == dpyinfo->x_focus_frame)
9769 construct_mouse_click (&emacs_event, &event, f);
9771 else
9773 #ifndef USE_TOOLKIT_SCROLL_BARS
9774 struct scroll_bar *bar
9775 = x_window_to_scroll_bar (event.xbutton.window);
9777 if (bar)
9778 x_scroll_bar_handle_click (bar, &event, &emacs_event);
9779 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9782 if (event.type == ButtonPress)
9784 dpyinfo->grabbed |= (1 << event.xbutton.button);
9785 last_mouse_frame = f;
9786 /* Ignore any mouse motion that happened
9787 before this event; any subsequent mouse-movement
9788 Emacs events should reflect only motion after
9789 the ButtonPress. */
9790 if (f != 0)
9791 f->mouse_moved = 0;
9793 if (!tool_bar_p)
9794 last_tool_bar_item = -1;
9796 else
9798 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
9801 if (numchars >= 1 && emacs_event.kind != no_event)
9803 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9804 bufp++;
9805 count++;
9806 numchars--;
9809 #ifdef USE_X_TOOLKIT
9810 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9811 /* For a down-event in the menu bar,
9812 don't pass it to Xt right now.
9813 Instead, save it away
9814 and we will pass it to Xt from kbd_buffer_get_event.
9815 That way, we can run some Lisp code first. */
9816 if (f && event.type == ButtonPress
9817 /* Verify the event is really within the menu bar
9818 and not just sent to it due to grabbing. */
9819 && event.xbutton.x >= 0
9820 && event.xbutton.x < f->output_data.x->pixel_width
9821 && event.xbutton.y >= 0
9822 && event.xbutton.y < f->output_data.x->menubar_height
9823 && event.xbutton.same_screen)
9825 SET_SAVED_BUTTON_EVENT;
9826 XSETFRAME (last_mouse_press_frame, f);
9828 else if (event.type == ButtonPress)
9830 last_mouse_press_frame = Qnil;
9831 goto OTHER;
9834 #ifdef USE_MOTIF /* This should do not harm for Lucid,
9835 but I am trying to be cautious. */
9836 else if (event.type == ButtonRelease)
9838 if (!NILP (last_mouse_press_frame))
9840 f = XFRAME (last_mouse_press_frame);
9841 if (f->output_data.x)
9842 SET_SAVED_BUTTON_EVENT;
9844 else
9845 goto OTHER;
9847 #endif /* USE_MOTIF */
9848 else
9849 goto OTHER;
9850 #endif /* USE_X_TOOLKIT */
9852 break;
9854 case CirculateNotify:
9855 goto OTHER;
9857 case CirculateRequest:
9858 goto OTHER;
9860 case VisibilityNotify:
9861 goto OTHER;
9863 case MappingNotify:
9864 /* Someone has changed the keyboard mapping - update the
9865 local cache. */
9866 switch (event.xmapping.request)
9868 case MappingModifier:
9869 x_find_modifier_meanings (dpyinfo);
9870 /* This is meant to fall through. */
9871 case MappingKeyboard:
9872 XRefreshKeyboardMapping (&event.xmapping);
9874 goto OTHER;
9876 default:
9877 OTHER:
9878 #ifdef USE_X_TOOLKIT
9879 BLOCK_INPUT;
9880 XtDispatchEvent (&event);
9881 UNBLOCK_INPUT;
9882 #endif /* USE_X_TOOLKIT */
9883 break;
9888 out:;
9890 /* On some systems, an X bug causes Emacs to get no more events
9891 when the window is destroyed. Detect that. (1994.) */
9892 if (! event_found)
9894 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
9895 One XNOOP in 100 loops will make Emacs terminate.
9896 B. Bretthauer, 1994 */
9897 x_noop_count++;
9898 if (x_noop_count >= 100)
9900 x_noop_count=0;
9902 if (next_noop_dpyinfo == 0)
9903 next_noop_dpyinfo = x_display_list;
9905 XNoOp (next_noop_dpyinfo->display);
9907 /* Each time we get here, cycle through the displays now open. */
9908 next_noop_dpyinfo = next_noop_dpyinfo->next;
9912 /* If the focus was just given to an auto-raising frame,
9913 raise it now. */
9914 /* ??? This ought to be able to handle more than one such frame. */
9915 if (pending_autoraise_frame)
9917 x_raise_frame (pending_autoraise_frame);
9918 pending_autoraise_frame = 0;
9921 UNBLOCK_INPUT;
9922 return count;
9928 /***********************************************************************
9929 Text Cursor
9930 ***********************************************************************/
9932 /* Note if the text cursor of window W has been overwritten by a
9933 drawing operation that outputs N glyphs starting at HPOS in the
9934 line given by output_cursor.vpos. N < 0 means all the rest of the
9935 line after HPOS has been written. */
9937 static void
9938 note_overwritten_text_cursor (w, hpos, n)
9939 struct window *w;
9940 int hpos, n;
9942 if (updated_area == TEXT_AREA
9943 && output_cursor.vpos == w->phys_cursor.vpos
9944 && hpos <= w->phys_cursor.hpos
9945 && (n < 0
9946 || hpos + n > w->phys_cursor.hpos))
9947 w->phys_cursor_on_p = 0;
9951 /* Set clipping for output in glyph row ROW. W is the window in which
9952 we operate. GC is the graphics context to set clipping in.
9953 WHOLE_LINE_P non-zero means include the areas used for truncation
9954 mark display and alike in the clipping rectangle.
9956 ROW may be a text row or, e.g., a mode line. Text rows must be
9957 clipped to the interior of the window dedicated to text display,
9958 mode lines must be clipped to the whole window. */
9960 static void
9961 x_clip_to_row (w, row, gc, whole_line_p)
9962 struct window *w;
9963 struct glyph_row *row;
9964 GC gc;
9965 int whole_line_p;
9967 struct frame *f = XFRAME (WINDOW_FRAME (w));
9968 XRectangle clip_rect;
9969 int window_x, window_y, window_width, window_height;
9971 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9973 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
9974 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
9975 clip_rect.y = max (clip_rect.y, window_y);
9976 clip_rect.width = window_width;
9977 clip_rect.height = row->visible_height;
9979 /* If clipping to the whole line, including trunc marks, extend
9980 the rectangle to the left and increase its width. */
9981 if (whole_line_p)
9983 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
9984 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
9987 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
9991 /* Draw a hollow box cursor on window W in glyph row ROW. */
9993 static void
9994 x_draw_hollow_cursor (w, row)
9995 struct window *w;
9996 struct glyph_row *row;
9998 struct frame *f = XFRAME (WINDOW_FRAME (w));
9999 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10000 Display *dpy = FRAME_X_DISPLAY (f);
10001 int x, y, wd, h;
10002 XGCValues xgcv;
10003 struct glyph *cursor_glyph;
10004 GC gc;
10006 /* Compute frame-relative coordinates from window-relative
10007 coordinates. */
10008 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10009 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10010 + row->ascent - w->phys_cursor_ascent);
10011 h = row->height - 1;
10013 /* Get the glyph the cursor is on. If we can't tell because
10014 the current matrix is invalid or such, give up. */
10015 cursor_glyph = get_phys_cursor_glyph (w);
10016 if (cursor_glyph == NULL)
10017 return;
10019 /* Compute the width of the rectangle to draw. If on a stretch
10020 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10021 rectangle as wide as the glyph, but use a canonical character
10022 width instead. */
10023 wd = cursor_glyph->pixel_width - 1;
10024 if (cursor_glyph->type == STRETCH_GLYPH
10025 && !x_stretch_cursor_p)
10026 wd = min (CANON_X_UNIT (f), wd);
10028 /* The foreground of cursor_gc is typically the same as the normal
10029 background color, which can cause the cursor box to be invisible. */
10030 xgcv.foreground = f->output_data.x->cursor_pixel;
10031 if (dpyinfo->scratch_cursor_gc)
10032 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10033 else
10034 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10035 GCForeground, &xgcv);
10036 gc = dpyinfo->scratch_cursor_gc;
10038 /* Set clipping, draw the rectangle, and reset clipping again. */
10039 x_clip_to_row (w, row, gc, 0);
10040 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10041 XSetClipMask (dpy, gc, None);
10045 /* Draw a bar cursor on window W in glyph row ROW.
10047 Implementation note: One would like to draw a bar cursor with an
10048 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10049 Unfortunately, I didn't find a font yet that has this property set.
10050 --gerd. */
10052 static void
10053 x_draw_bar_cursor (w, row)
10054 struct window *w;
10055 struct glyph_row *row;
10057 /* If cursor hpos is out of bounds, don't draw garbage. This can
10058 happen in mini-buffer windows when switching between echo area
10059 glyphs and mini-buffer. */
10060 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10062 struct frame *f = XFRAME (w->frame);
10063 struct glyph *cursor_glyph;
10064 GC gc;
10065 int x;
10066 unsigned long mask;
10067 XGCValues xgcv;
10068 Display *dpy;
10069 Window window;
10071 cursor_glyph = get_phys_cursor_glyph (w);
10072 if (cursor_glyph == NULL)
10073 return;
10075 xgcv.background = f->output_data.x->cursor_pixel;
10076 xgcv.foreground = f->output_data.x->cursor_pixel;
10077 xgcv.graphics_exposures = 0;
10078 mask = GCForeground | GCBackground | GCGraphicsExposures;
10079 dpy = FRAME_X_DISPLAY (f);
10080 window = FRAME_X_WINDOW (f);
10081 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10083 if (gc)
10084 XChangeGC (dpy, gc, mask, &xgcv);
10085 else
10087 gc = XCreateGC (dpy, window, mask, &xgcv);
10088 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10091 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10092 x_clip_to_row (w, row, gc, 0);
10093 XFillRectangle (dpy, window, gc,
10095 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10096 min (cursor_glyph->pixel_width,
10097 f->output_data.x->cursor_width),
10098 row->height);
10099 XSetClipMask (dpy, gc, None);
10104 /* Clear the cursor of window W to background color, and mark the
10105 cursor as not shown. This is used when the text where the cursor
10106 is is about to be rewritten. */
10108 static void
10109 x_clear_cursor (w)
10110 struct window *w;
10112 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10113 x_update_window_cursor (w, 0);
10117 /* Draw the cursor glyph of window W in glyph row ROW. See the
10118 comment of x_draw_glyphs for the meaning of HL. */
10120 static void
10121 x_draw_phys_cursor_glyph (w, row, hl)
10122 struct window *w;
10123 struct glyph_row *row;
10124 enum draw_glyphs_face hl;
10126 /* If cursor hpos is out of bounds, don't draw garbage. This can
10127 happen in mini-buffer windows when switching between echo area
10128 glyphs and mini-buffer. */
10129 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10131 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10132 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10133 hl, 0, 0, 0);
10135 /* When we erase the cursor, and ROW is overlapped by other
10136 rows, make sure that these overlapping parts of other rows
10137 are redrawn. */
10138 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10140 if (row > w->current_matrix->rows
10141 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10142 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10144 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10145 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10146 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10152 /* Erase the image of a cursor of window W from the screen. */
10154 static void
10155 x_erase_phys_cursor (w)
10156 struct window *w;
10158 struct frame *f = XFRAME (w->frame);
10159 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10160 int hpos = w->phys_cursor.hpos;
10161 int vpos = w->phys_cursor.vpos;
10162 int mouse_face_here_p = 0;
10163 struct glyph_matrix *active_glyphs = w->current_matrix;
10164 struct glyph_row *cursor_row;
10165 struct glyph *cursor_glyph;
10166 enum draw_glyphs_face hl;
10168 /* No cursor displayed or row invalidated => nothing to do on the
10169 screen. */
10170 if (w->phys_cursor_type == NO_CURSOR)
10171 goto mark_cursor_off;
10173 /* VPOS >= active_glyphs->nrows means that window has been resized.
10174 Don't bother to erase the cursor. */
10175 if (vpos >= active_glyphs->nrows)
10176 goto mark_cursor_off;
10178 /* If row containing cursor is marked invalid, there is nothing we
10179 can do. */
10180 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10181 if (!cursor_row->enabled_p)
10182 goto mark_cursor_off;
10184 /* This can happen when the new row is shorter than the old one.
10185 In this case, either x_draw_glyphs or clear_end_of_line
10186 should have cleared the cursor. Note that we wouldn't be
10187 able to erase the cursor in this case because we don't have a
10188 cursor glyph at hand. */
10189 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10190 goto mark_cursor_off;
10192 /* If the cursor is in the mouse face area, redisplay that when
10193 we clear the cursor. */
10194 if (w == XWINDOW (dpyinfo->mouse_face_window)
10195 && (vpos > dpyinfo->mouse_face_beg_row
10196 || (vpos == dpyinfo->mouse_face_beg_row
10197 && hpos >= dpyinfo->mouse_face_beg_col))
10198 && (vpos < dpyinfo->mouse_face_end_row
10199 || (vpos == dpyinfo->mouse_face_end_row
10200 && hpos < dpyinfo->mouse_face_end_col))
10201 /* Don't redraw the cursor's spot in mouse face if it is at the
10202 end of a line (on a newline). The cursor appears there, but
10203 mouse highlighting does not. */
10204 && cursor_row->used[TEXT_AREA] > hpos)
10205 mouse_face_here_p = 1;
10207 /* Maybe clear the display under the cursor. */
10208 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10210 int x;
10211 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10213 cursor_glyph = get_phys_cursor_glyph (w);
10214 if (cursor_glyph == NULL)
10215 goto mark_cursor_off;
10217 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10219 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10221 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10222 cursor_row->y)),
10223 cursor_glyph->pixel_width,
10224 cursor_row->visible_height,
10225 False);
10228 /* Erase the cursor by redrawing the character underneath it. */
10229 if (mouse_face_here_p)
10230 hl = DRAW_MOUSE_FACE;
10231 else if (cursor_row->inverse_p)
10232 hl = DRAW_INVERSE_VIDEO;
10233 else
10234 hl = DRAW_NORMAL_TEXT;
10235 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10237 mark_cursor_off:
10238 w->phys_cursor_on_p = 0;
10239 w->phys_cursor_type = NO_CURSOR;
10243 /* Display or clear cursor of window W. If ON is zero, clear the
10244 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10245 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10247 void
10248 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10249 struct window *w;
10250 int on, hpos, vpos, x, y;
10252 struct frame *f = XFRAME (w->frame);
10253 int new_cursor_type;
10254 struct glyph_matrix *current_glyphs;
10255 struct glyph_row *glyph_row;
10256 struct glyph *glyph;
10258 /* This is pointless on invisible frames, and dangerous on garbaged
10259 windows and frames; in the latter case, the frame or window may
10260 be in the midst of changing its size, and x and y may be off the
10261 window. */
10262 if (! FRAME_VISIBLE_P (f)
10263 || FRAME_GARBAGED_P (f)
10264 || vpos >= w->current_matrix->nrows
10265 || hpos >= w->current_matrix->matrix_w)
10266 return;
10268 /* If cursor is off and we want it off, return quickly. */
10269 if (!on && !w->phys_cursor_on_p)
10270 return;
10272 current_glyphs = w->current_matrix;
10273 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10274 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10276 /* If cursor row is not enabled, we don't really know where to
10277 display the cursor. */
10278 if (!glyph_row->enabled_p)
10280 w->phys_cursor_on_p = 0;
10281 return;
10284 xassert (interrupt_input_blocked);
10286 /* Set new_cursor_type to the cursor we want to be displayed. In a
10287 mini-buffer window, we want the cursor only to appear if we are
10288 reading input from this window. For the selected window, we want
10289 the cursor type given by the frame parameter. If explicitly
10290 marked off, draw no cursor. In all other cases, we want a hollow
10291 box cursor. */
10292 if (cursor_in_echo_area
10293 && FRAME_HAS_MINIBUF_P (f)
10294 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10296 if (w == XWINDOW (echo_area_window))
10297 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10298 else
10299 new_cursor_type = HOLLOW_BOX_CURSOR;
10301 else
10303 if (w != XWINDOW (selected_window)
10304 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10306 extern int cursor_in_non_selected_windows;
10308 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
10309 new_cursor_type = NO_CURSOR;
10310 else
10311 new_cursor_type = HOLLOW_BOX_CURSOR;
10313 else if (w->cursor_off_p)
10314 new_cursor_type = NO_CURSOR;
10315 else
10316 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10319 /* If cursor is currently being shown and we don't want it to be or
10320 it is in the wrong place, or the cursor type is not what we want,
10321 erase it. */
10322 if (w->phys_cursor_on_p
10323 && (!on
10324 || w->phys_cursor.x != x
10325 || w->phys_cursor.y != y
10326 || new_cursor_type != w->phys_cursor_type))
10327 x_erase_phys_cursor (w);
10329 /* If the cursor is now invisible and we want it to be visible,
10330 display it. */
10331 if (on && !w->phys_cursor_on_p)
10333 w->phys_cursor_ascent = glyph_row->ascent;
10334 w->phys_cursor_height = glyph_row->height;
10336 /* Set phys_cursor_.* before x_draw_.* is called because some
10337 of them may need the information. */
10338 w->phys_cursor.x = x;
10339 w->phys_cursor.y = glyph_row->y;
10340 w->phys_cursor.hpos = hpos;
10341 w->phys_cursor.vpos = vpos;
10342 w->phys_cursor_type = new_cursor_type;
10343 w->phys_cursor_on_p = 1;
10345 switch (new_cursor_type)
10347 case HOLLOW_BOX_CURSOR:
10348 x_draw_hollow_cursor (w, glyph_row);
10349 break;
10351 case FILLED_BOX_CURSOR:
10352 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10353 break;
10355 case BAR_CURSOR:
10356 x_draw_bar_cursor (w, glyph_row);
10357 break;
10359 case NO_CURSOR:
10360 break;
10362 default:
10363 abort ();
10366 #ifdef HAVE_X_I18N
10367 if (w == XWINDOW (f->selected_window))
10368 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10369 xic_set_preeditarea (w, x, y);
10370 #endif
10373 #ifndef XFlush
10374 if (updating_frame != f)
10375 XFlush (FRAME_X_DISPLAY (f));
10376 #endif
10380 /* Display the cursor on window W, or clear it. X and Y are window
10381 relative pixel coordinates. HPOS and VPOS are glyph matrix
10382 positions. If W is not the selected window, display a hollow
10383 cursor. ON non-zero means display the cursor at X, Y which
10384 correspond to HPOS, VPOS, otherwise it is cleared. */
10386 void
10387 x_display_cursor (w, on, hpos, vpos, x, y)
10388 struct window *w;
10389 int on, hpos, vpos, x, y;
10391 BLOCK_INPUT;
10392 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10393 UNBLOCK_INPUT;
10397 /* Display the cursor on window W, or clear it, according to ON_P.
10398 Don't change the cursor's position. */
10400 void
10401 x_update_cursor (f, on_p)
10402 struct frame *f;
10404 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10408 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10409 in the window tree rooted at W. */
10411 static void
10412 x_update_cursor_in_window_tree (w, on_p)
10413 struct window *w;
10414 int on_p;
10416 while (w)
10418 if (!NILP (w->hchild))
10419 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10420 else if (!NILP (w->vchild))
10421 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10422 else
10423 x_update_window_cursor (w, on_p);
10425 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10430 /* Switch the display of W's cursor on or off, according to the value
10431 of ON. */
10433 static void
10434 x_update_window_cursor (w, on)
10435 struct window *w;
10436 int on;
10438 /* Don't update cursor in windows whose frame is in the process
10439 of being deleted. */
10440 if (w->current_matrix)
10442 BLOCK_INPUT;
10443 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10444 w->phys_cursor.x, w->phys_cursor.y);
10445 UNBLOCK_INPUT;
10452 /* Icons. */
10454 /* Refresh bitmap kitchen sink icon for frame F
10455 when we get an expose event for it. */
10457 void
10458 refreshicon (f)
10459 struct frame *f;
10461 /* Normally, the window manager handles this function. */
10464 /* Make the x-window of frame F use the gnu icon bitmap. */
10467 x_bitmap_icon (f, file)
10468 struct frame *f;
10469 Lisp_Object file;
10471 int bitmap_id;
10473 if (FRAME_X_WINDOW (f) == 0)
10474 return 1;
10476 /* Free up our existing icon bitmap if any. */
10477 if (f->output_data.x->icon_bitmap > 0)
10478 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10479 f->output_data.x->icon_bitmap = 0;
10481 if (STRINGP (file))
10482 bitmap_id = x_create_bitmap_from_file (f, file);
10483 else
10485 /* Create the GNU bitmap if necessary. */
10486 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10487 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10488 = x_create_bitmap_from_data (f, gnu_bits,
10489 gnu_width, gnu_height);
10491 /* The first time we create the GNU bitmap,
10492 this increments the ref-count one extra time.
10493 As a result, the GNU bitmap is never freed.
10494 That way, we don't have to worry about allocating it again. */
10495 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10497 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10500 x_wm_set_icon_pixmap (f, bitmap_id);
10501 f->output_data.x->icon_bitmap = bitmap_id;
10503 return 0;
10507 /* Make the x-window of frame F use a rectangle with text.
10508 Use ICON_NAME as the text. */
10511 x_text_icon (f, icon_name)
10512 struct frame *f;
10513 char *icon_name;
10515 if (FRAME_X_WINDOW (f) == 0)
10516 return 1;
10518 #ifdef HAVE_X11R4
10520 XTextProperty text;
10521 text.value = (unsigned char *) icon_name;
10522 text.encoding = XA_STRING;
10523 text.format = 8;
10524 text.nitems = strlen (icon_name);
10525 #ifdef USE_X_TOOLKIT
10526 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10527 &text);
10528 #else /* not USE_X_TOOLKIT */
10529 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10530 #endif /* not USE_X_TOOLKIT */
10532 #else /* not HAVE_X11R4 */
10533 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10534 #endif /* not HAVE_X11R4 */
10536 if (f->output_data.x->icon_bitmap > 0)
10537 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10538 f->output_data.x->icon_bitmap = 0;
10539 x_wm_set_icon_pixmap (f, 0);
10541 return 0;
10544 #define X_ERROR_MESSAGE_SIZE 200
10546 /* If non-nil, this should be a string.
10547 It means catch X errors and store the error message in this string. */
10549 static Lisp_Object x_error_message_string;
10551 /* An X error handler which stores the error message in
10552 x_error_message_string. This is called from x_error_handler if
10553 x_catch_errors is in effect. */
10555 static void
10556 x_error_catcher (display, error)
10557 Display *display;
10558 XErrorEvent *error;
10560 XGetErrorText (display, error->error_code,
10561 XSTRING (x_error_message_string)->data,
10562 X_ERROR_MESSAGE_SIZE);
10565 /* Begin trapping X errors for display DPY. Actually we trap X errors
10566 for all displays, but DPY should be the display you are actually
10567 operating on.
10569 After calling this function, X protocol errors no longer cause
10570 Emacs to exit; instead, they are recorded in the string
10571 stored in x_error_message_string.
10573 Calling x_check_errors signals an Emacs error if an X error has
10574 occurred since the last call to x_catch_errors or x_check_errors.
10576 Calling x_uncatch_errors resumes the normal error handling. */
10578 void x_check_errors ();
10579 static Lisp_Object x_catch_errors_unwind ();
10582 x_catch_errors (dpy)
10583 Display *dpy;
10585 int count = specpdl_ptr - specpdl;
10587 /* Make sure any errors from previous requests have been dealt with. */
10588 XSync (dpy, False);
10590 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10592 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10593 XSTRING (x_error_message_string)->data[0] = 0;
10595 return count;
10598 /* Unbind the binding that we made to check for X errors. */
10600 static Lisp_Object
10601 x_catch_errors_unwind (old_val)
10602 Lisp_Object old_val;
10604 x_error_message_string = old_val;
10605 return Qnil;
10608 /* If any X protocol errors have arrived since the last call to
10609 x_catch_errors or x_check_errors, signal an Emacs error using
10610 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10612 void
10613 x_check_errors (dpy, format)
10614 Display *dpy;
10615 char *format;
10617 /* Make sure to catch any errors incurred so far. */
10618 XSync (dpy, False);
10620 if (XSTRING (x_error_message_string)->data[0])
10621 error (format, XSTRING (x_error_message_string)->data);
10624 /* Nonzero if we had any X protocol errors
10625 since we did x_catch_errors on DPY. */
10628 x_had_errors_p (dpy)
10629 Display *dpy;
10631 /* Make sure to catch any errors incurred so far. */
10632 XSync (dpy, False);
10634 return XSTRING (x_error_message_string)->data[0] != 0;
10637 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10639 void
10640 x_clear_errors (dpy)
10641 Display *dpy;
10643 XSTRING (x_error_message_string)->data[0] = 0;
10646 /* Stop catching X protocol errors and let them make Emacs die.
10647 DPY should be the display that was passed to x_catch_errors.
10648 COUNT should be the value that was returned by
10649 the corresponding call to x_catch_errors. */
10651 void
10652 x_uncatch_errors (dpy, count)
10653 Display *dpy;
10654 int count;
10656 unbind_to (count, Qnil);
10659 #if 0
10660 static unsigned int x_wire_count;
10661 x_trace_wire ()
10663 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10665 #endif /* ! 0 */
10668 /* Handle SIGPIPE, which can happen when the connection to a server
10669 simply goes away. SIGPIPE is handled by x_connection_signal.
10670 Don't need to do anything, because the write which caused the
10671 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
10672 which will do the appropriate cleanup for us. */
10674 static SIGTYPE
10675 x_connection_signal (signalnum) /* If we don't have an argument, */
10676 int signalnum; /* some compilers complain in signal calls. */
10678 #ifdef USG
10679 /* USG systems forget handlers when they are used;
10680 must reestablish each time */
10681 signal (signalnum, x_connection_signal);
10682 #endif /* USG */
10685 /* Handling X errors. */
10687 /* Handle the loss of connection to display DISPLAY. */
10689 static SIGTYPE
10690 x_connection_closed (display, error_message)
10691 Display *display;
10692 char *error_message;
10694 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10695 Lisp_Object frame, tail;
10697 /* Indicate that this display is dead. */
10699 #if 0 /* Closing the display caused a bus error on OpenWindows. */
10700 #ifdef USE_X_TOOLKIT
10701 XtCloseDisplay (display);
10702 #endif
10703 #endif
10705 dpyinfo->display = 0;
10707 /* First delete frames whose mini-buffers are on frames
10708 that are on the dead display. */
10709 FOR_EACH_FRAME (tail, frame)
10711 Lisp_Object minibuf_frame;
10712 minibuf_frame
10713 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
10714 if (FRAME_X_P (XFRAME (frame))
10715 && FRAME_X_P (XFRAME (minibuf_frame))
10716 && ! EQ (frame, minibuf_frame)
10717 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
10718 Fdelete_frame (frame, Qt);
10721 /* Now delete all remaining frames on the dead display.
10722 We are now sure none of these is used as the mini-buffer
10723 for another frame that we need to delete. */
10724 FOR_EACH_FRAME (tail, frame)
10725 if (FRAME_X_P (XFRAME (frame))
10726 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
10728 /* Set this to t so that Fdelete_frame won't get confused
10729 trying to find a replacement. */
10730 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10731 Fdelete_frame (frame, Qt);
10734 if (dpyinfo)
10735 x_delete_display (dpyinfo);
10737 if (x_display_list == 0)
10739 fprintf (stderr, "%s\n", error_message);
10740 shut_down_emacs (0, 0, Qnil);
10741 exit (70);
10744 /* Ordinary stack unwind doesn't deal with these. */
10745 #ifdef SIGIO
10746 sigunblock (sigmask (SIGIO));
10747 #endif
10748 sigunblock (sigmask (SIGALRM));
10749 TOTALLY_UNBLOCK_INPUT;
10751 clear_waiting_for_input ();
10752 error ("%s", error_message);
10755 /* This is the usual handler for X protocol errors.
10756 It kills all frames on the display that we got the error for.
10757 If that was the only one, it prints an error message and kills Emacs. */
10759 static void
10760 x_error_quitter (display, error)
10761 Display *display;
10762 XErrorEvent *error;
10764 char buf[256], buf1[356];
10766 /* Note that there is no real way portable across R3/R4 to get the
10767 original error handler. */
10769 XGetErrorText (display, error->error_code, buf, sizeof (buf));
10770 sprintf (buf1, "X protocol error: %s on protocol request %d",
10771 buf, error->request_code);
10772 x_connection_closed (display, buf1);
10775 /* This is the first-level handler for X protocol errors.
10776 It calls x_error_quitter or x_error_catcher. */
10778 static int
10779 x_error_handler (display, error)
10780 Display *display;
10781 XErrorEvent *error;
10783 if (! NILP (x_error_message_string))
10784 x_error_catcher (display, error);
10785 else
10786 x_error_quitter (display, error);
10787 return 0;
10790 /* This is the handler for X IO errors, always.
10791 It kills all frames on the display that we lost touch with.
10792 If that was the only one, it prints an error message and kills Emacs. */
10794 static int
10795 x_io_error_quitter (display)
10796 Display *display;
10798 char buf[256];
10800 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10801 x_connection_closed (display, buf);
10802 return 0;
10805 /* Changing the font of the frame. */
10807 /* Give frame F the font named FONTNAME as its default font, and
10808 return the full name of that font. FONTNAME may be a wildcard
10809 pattern; in that case, we choose some font that fits the pattern.
10810 The return value shows which font we chose. */
10812 Lisp_Object
10813 x_new_font (f, fontname)
10814 struct frame *f;
10815 register char *fontname;
10817 struct font_info *fontp
10818 = FS_LOAD_FONT (f, 0, fontname, -1);
10820 if (!fontp)
10821 return Qnil;
10823 f->output_data.x->font = (XFontStruct *) (fontp->font);
10824 f->output_data.x->baseline_offset = fontp->baseline_offset;
10825 f->output_data.x->fontset = -1;
10827 /* Compute the scroll bar width in character columns. */
10828 if (f->scroll_bar_pixel_width > 0)
10830 int wid = FONT_WIDTH (f->output_data.x->font);
10831 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10833 else
10835 int wid = FONT_WIDTH (f->output_data.x->font);
10836 f->scroll_bar_cols = (14 + wid - 1) / wid;
10839 /* Now make the frame display the given font. */
10840 if (FRAME_X_WINDOW (f) != 0)
10842 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10843 f->output_data.x->font->fid);
10844 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10845 f->output_data.x->font->fid);
10846 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10847 f->output_data.x->font->fid);
10849 frame_update_line_height (f);
10850 x_set_window_size (f, 0, f->width, f->height);
10852 else
10853 /* If we are setting a new frame's font for the first time,
10854 there are no faces yet, so this font's height is the line height. */
10855 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
10857 return build_string (fontp->full_name);
10860 /* Give frame F the fontset named FONTSETNAME as its default font, and
10861 return the full name of that fontset. FONTSETNAME may be a wildcard
10862 pattern; in that case, we choose some fontset that fits the pattern.
10863 The return value shows which fontset we chose. */
10865 Lisp_Object
10866 x_new_fontset (f, fontsetname)
10867 struct frame *f;
10868 char *fontsetname;
10870 int fontset = fs_query_fontset (build_string (fontsetname), 0);
10871 Lisp_Object result;
10872 char *fontname;
10874 if (fontset < 0)
10875 return Qnil;
10877 if (f->output_data.x->fontset == fontset)
10878 /* This fontset is already set in frame F. There's nothing more
10879 to do. */
10880 return fontset_name (fontset);
10882 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
10884 if (!STRINGP (result))
10885 /* Can't load ASCII font. */
10886 return Qnil;
10888 /* Since x_new_font doesn't update any fontset information, do it now. */
10889 f->output_data.x->fontset = fontset;
10891 #ifdef HAVE_X_I18N
10892 if (FRAME_XIC (f)
10893 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
10894 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
10895 #endif
10897 return build_string (fontsetname);
10901 /***********************************************************************
10902 X Input Methods
10903 ***********************************************************************/
10905 #ifdef HAVE_X_I18N
10907 #ifdef HAVE_X11R6
10909 /* XIM destroy callback function, which is called whenever the
10910 connection to input method XIM dies. CLIENT_DATA contains a
10911 pointer to the x_display_info structure corresponding to XIM. */
10913 static void
10914 xim_destroy_callback (xim, client_data, call_data)
10915 XIM xim;
10916 XPointer client_data;
10917 XPointer call_data;
10919 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
10920 Lisp_Object frame, tail;
10922 BLOCK_INPUT;
10924 /* No need to call XDestroyIC.. */
10925 FOR_EACH_FRAME (tail, frame)
10927 struct frame *f = XFRAME (frame);
10928 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
10930 FRAME_XIC (f) = NULL;
10931 if (FRAME_XIC_FONTSET (f))
10933 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
10934 FRAME_XIC_FONTSET (f) = NULL;
10939 /* No need to call XCloseIM. */
10940 dpyinfo->xim = NULL;
10941 XFree (dpyinfo->xim_styles);
10942 UNBLOCK_INPUT;
10945 #endif /* HAVE_X11R6 */
10947 /* Open the connection to the XIM server on display DPYINFO.
10948 RESOURCE_NAME is the resource name Emacs uses. */
10950 static void
10951 xim_open_dpy (dpyinfo, resource_name)
10952 struct x_display_info *dpyinfo;
10953 char *resource_name;
10955 XIM xim;
10957 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
10958 dpyinfo->xim = xim;
10960 if (xim)
10962 #ifdef HAVE_X11R6
10963 XIMCallback destroy;
10964 #endif
10966 /* Get supported styles and XIM values. */
10967 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
10969 #ifdef HAVE_X11R6
10970 destroy.callback = xim_destroy_callback;
10971 destroy.client_data = (XPointer)dpyinfo;
10972 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
10973 #endif
10978 #ifdef HAVE_X11R6_XIM
10980 struct xim_inst_t
10982 struct x_display_info *dpyinfo;
10983 char *resource_name;
10986 /* XIM instantiate callback function, which is called whenever an XIM
10987 server is available. DISPLAY is teh display of the XIM.
10988 CLIENT_DATA contains a pointer to an xim_inst_t structure created
10989 when the callback was registered. */
10991 static void
10992 xim_instantiate_callback (display, client_data, call_data)
10993 Display *display;
10994 XPointer client_data;
10995 XPointer call_data;
10997 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
10998 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11000 /* We don't support multiple XIM connections. */
11001 if (dpyinfo->xim)
11002 return;
11004 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11006 /* Create XIC for the existing frames on the same display, as long
11007 as they have no XIC. */
11008 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11010 Lisp_Object tail, frame;
11012 BLOCK_INPUT;
11013 FOR_EACH_FRAME (tail, frame)
11015 struct frame *f = XFRAME (frame);
11017 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11018 if (FRAME_XIC (f) == NULL)
11020 create_frame_xic (f);
11021 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11022 xic_set_statusarea (f);
11023 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11025 struct window *w = XWINDOW (f->selected_window);
11026 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11031 UNBLOCK_INPUT;
11035 #endif /* HAVE_X11R6_XIM */
11038 /* Open a connection to the XIM server on display DPYINFO.
11039 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11040 connection only at the first time. On X11R6, open the connection
11041 in the XIM instantiate callback function. */
11043 static void
11044 xim_initialize (dpyinfo, resource_name)
11045 struct x_display_info *dpyinfo;
11046 char *resource_name;
11048 #ifdef HAVE_X11R6_XIM
11049 struct xim_inst_t *xim_inst;
11050 int len;
11052 dpyinfo->xim = NULL;
11053 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11054 xim_inst->dpyinfo = dpyinfo;
11055 len = strlen (resource_name);
11056 xim_inst->resource_name = (char *) xmalloc (len + 1);
11057 bcopy (resource_name, xim_inst->resource_name, len + 1);
11058 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11059 resource_name, EMACS_CLASS,
11060 xim_instantiate_callback,
11061 (XPointer)xim_inst);
11062 #else /* not HAVE_X11R6_XIM */
11063 dpyinfo->xim = NULL;
11064 xim_open_dpy (dpyinfo, resource_name);
11065 #endif /* not HAVE_X11R6_XIM */
11069 /* Close the connection to the XIM server on display DPYINFO. */
11071 static void
11072 xim_close_dpy (dpyinfo)
11073 struct x_display_info *dpyinfo;
11075 #ifdef HAVE_X11R6_XIM
11076 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11077 NULL, EMACS_CLASS,
11078 xim_instantiate_callback, NULL);
11079 #endif /* not HAVE_X11R6_XIM */
11080 XCloseIM (dpyinfo->xim);
11081 dpyinfo->xim = NULL;
11082 XFree (dpyinfo->xim_styles);
11085 #endif /* not HAVE_X11R6_XIM */
11089 /* Calculate the absolute position in frame F
11090 from its current recorded position values and gravity. */
11092 void
11093 x_calc_absolute_position (f)
11094 struct frame *f;
11096 Window child;
11097 int win_x = 0, win_y = 0;
11098 int flags = f->output_data.x->size_hint_flags;
11099 int this_window;
11101 /* We have nothing to do if the current position
11102 is already for the top-left corner. */
11103 if (! ((flags & XNegative) || (flags & YNegative)))
11104 return;
11106 #ifdef USE_X_TOOLKIT
11107 this_window = XtWindow (f->output_data.x->widget);
11108 #else
11109 this_window = FRAME_X_WINDOW (f);
11110 #endif
11112 /* Find the position of the outside upper-left corner of
11113 the inner window, with respect to the outer window.
11114 But do this only if we will need the results. */
11115 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11117 int count;
11119 BLOCK_INPUT;
11120 count = x_catch_errors (FRAME_X_DISPLAY (f));
11121 while (1)
11123 x_clear_errors (FRAME_X_DISPLAY (f));
11124 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11126 /* From-window, to-window. */
11127 this_window,
11128 f->output_data.x->parent_desc,
11130 /* From-position, to-position. */
11131 0, 0, &win_x, &win_y,
11133 /* Child of win. */
11134 &child);
11135 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11137 Window newroot, newparent = 0xdeadbeef;
11138 Window *newchildren;
11139 int nchildren;
11141 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11142 &newparent, &newchildren, &nchildren))
11143 break;
11145 XFree ((char *) newchildren);
11147 f->output_data.x->parent_desc = newparent;
11149 else
11150 break;
11153 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11154 UNBLOCK_INPUT;
11157 /* Treat negative positions as relative to the leftmost bottommost
11158 position that fits on the screen. */
11159 if (flags & XNegative)
11160 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11161 - 2 * f->output_data.x->border_width - win_x
11162 - PIXEL_WIDTH (f)
11163 + f->output_data.x->left_pos);
11165 if (flags & YNegative)
11167 int menubar_height = 0;
11169 #ifdef USE_X_TOOLKIT
11170 if (f->output_data.x->menubar_widget)
11171 menubar_height
11172 = (f->output_data.x->menubar_widget->core.height
11173 + f->output_data.x->menubar_widget->core.border_width);
11174 #endif
11176 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11177 - 2 * f->output_data.x->border_width
11178 - win_y
11179 - PIXEL_HEIGHT (f)
11180 - menubar_height
11181 + f->output_data.x->top_pos);
11184 /* The left_pos and top_pos
11185 are now relative to the top and left screen edges,
11186 so the flags should correspond. */
11187 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11190 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11191 to really change the position, and 0 when calling from
11192 x_make_frame_visible (in that case, XOFF and YOFF are the current
11193 position values). It is -1 when calling from x_set_frame_parameters,
11194 which means, do adjust for borders but don't change the gravity. */
11196 void
11197 x_set_offset (f, xoff, yoff, change_gravity)
11198 struct frame *f;
11199 register int xoff, yoff;
11200 int change_gravity;
11202 int modified_top, modified_left;
11204 if (change_gravity > 0)
11206 f->output_data.x->top_pos = yoff;
11207 f->output_data.x->left_pos = xoff;
11208 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11209 if (xoff < 0)
11210 f->output_data.x->size_hint_flags |= XNegative;
11211 if (yoff < 0)
11212 f->output_data.x->size_hint_flags |= YNegative;
11213 f->output_data.x->win_gravity = NorthWestGravity;
11215 x_calc_absolute_position (f);
11217 BLOCK_INPUT;
11218 x_wm_set_size_hint (f, (long) 0, 0);
11220 modified_left = f->output_data.x->left_pos;
11221 modified_top = f->output_data.x->top_pos;
11222 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11223 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11224 /* It is a mystery why we need to add the border_width here
11225 when the frame is already visible, but experiment says we do. */
11226 if (change_gravity != 0)
11228 modified_left += f->output_data.x->border_width;
11229 modified_top += f->output_data.x->border_width;
11231 #endif
11233 #ifdef USE_X_TOOLKIT
11234 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11235 modified_left, modified_top);
11236 #else /* not USE_X_TOOLKIT */
11237 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11238 modified_left, modified_top);
11239 #endif /* not USE_X_TOOLKIT */
11240 UNBLOCK_INPUT;
11243 /* Call this to change the size of frame F's x-window.
11244 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11245 for this size change and subsequent size changes.
11246 Otherwise we leave the window gravity unchanged. */
11248 void
11249 x_set_window_size (f, change_gravity, cols, rows)
11250 struct frame *f;
11251 int change_gravity;
11252 int cols, rows;
11254 #ifndef USE_X_TOOLKIT
11255 int pixelwidth, pixelheight;
11256 #endif
11258 BLOCK_INPUT;
11260 #ifdef USE_X_TOOLKIT
11262 /* The x and y position of the widget is clobbered by the
11263 call to XtSetValues within EmacsFrameSetCharSize.
11264 This is a real kludge, but I don't understand Xt so I can't
11265 figure out a correct fix. Can anyone else tell me? -- rms. */
11266 int xpos = f->output_data.x->widget->core.x;
11267 int ypos = f->output_data.x->widget->core.y;
11268 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11269 f->output_data.x->widget->core.x = xpos;
11270 f->output_data.x->widget->core.y = ypos;
11273 #else /* not USE_X_TOOLKIT */
11275 check_frame_size (f, &rows, &cols);
11276 f->output_data.x->vertical_scroll_bar_extra
11277 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11279 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11280 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11281 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11282 f->output_data.x->flags_areas_extra
11283 = FRAME_FLAGS_AREA_WIDTH (f);
11284 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11285 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11287 f->output_data.x->win_gravity = NorthWestGravity;
11288 x_wm_set_size_hint (f, (long) 0, 0);
11290 XSync (FRAME_X_DISPLAY (f), False);
11291 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11292 pixelwidth, pixelheight);
11294 /* Now, strictly speaking, we can't be sure that this is accurate,
11295 but the window manager will get around to dealing with the size
11296 change request eventually, and we'll hear how it went when the
11297 ConfigureNotify event gets here.
11299 We could just not bother storing any of this information here,
11300 and let the ConfigureNotify event set everything up, but that
11301 might be kind of confusing to the Lisp code, since size changes
11302 wouldn't be reported in the frame parameters until some random
11303 point in the future when the ConfigureNotify event arrives.
11305 We pass 1 for DELAY since we can't run Lisp code inside of
11306 a BLOCK_INPUT. */
11307 change_frame_size (f, rows, cols, 0, 1, 0);
11308 PIXEL_WIDTH (f) = pixelwidth;
11309 PIXEL_HEIGHT (f) = pixelheight;
11311 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11312 receive in the ConfigureNotify event; if we get what we asked
11313 for, then the event won't cause the screen to become garbaged, so
11314 we have to make sure to do it here. */
11315 SET_FRAME_GARBAGED (f);
11317 XFlush (FRAME_X_DISPLAY (f));
11319 #endif /* not USE_X_TOOLKIT */
11321 /* If cursor was outside the new size, mark it as off. */
11322 mark_window_cursors_off (XWINDOW (f->root_window));
11324 /* Clear out any recollection of where the mouse highlighting was,
11325 since it might be in a place that's outside the new frame size.
11326 Actually checking whether it is outside is a pain in the neck,
11327 so don't try--just let the highlighting be done afresh with new size. */
11328 cancel_mouse_face (f);
11330 UNBLOCK_INPUT;
11333 /* Mouse warping. */
11335 void
11336 x_set_mouse_position (f, x, y)
11337 struct frame *f;
11338 int x, y;
11340 int pix_x, pix_y;
11342 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11343 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11345 if (pix_x < 0) pix_x = 0;
11346 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11348 if (pix_y < 0) pix_y = 0;
11349 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11351 BLOCK_INPUT;
11353 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11354 0, 0, 0, 0, pix_x, pix_y);
11355 UNBLOCK_INPUT;
11358 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11360 void
11361 x_set_mouse_pixel_position (f, pix_x, pix_y)
11362 struct frame *f;
11363 int pix_x, pix_y;
11365 BLOCK_INPUT;
11367 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11368 0, 0, 0, 0, pix_x, pix_y);
11369 UNBLOCK_INPUT;
11372 /* focus shifting, raising and lowering. */
11374 void
11375 x_focus_on_frame (f)
11376 struct frame *f;
11378 #if 0 /* This proves to be unpleasant. */
11379 x_raise_frame (f);
11380 #endif
11381 #if 0
11382 /* I don't think that the ICCCM allows programs to do things like this
11383 without the interaction of the window manager. Whatever you end up
11384 doing with this code, do it to x_unfocus_frame too. */
11385 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11386 RevertToPointerRoot, CurrentTime);
11387 #endif /* ! 0 */
11390 void
11391 x_unfocus_frame (f)
11392 struct frame *f;
11394 #if 0
11395 /* Look at the remarks in x_focus_on_frame. */
11396 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
11397 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
11398 RevertToPointerRoot, CurrentTime);
11399 #endif /* ! 0 */
11402 /* Raise frame F. */
11404 void
11405 x_raise_frame (f)
11406 struct frame *f;
11408 if (f->async_visible)
11410 BLOCK_INPUT;
11411 #ifdef USE_X_TOOLKIT
11412 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11413 #else /* not USE_X_TOOLKIT */
11414 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11415 #endif /* not USE_X_TOOLKIT */
11416 XFlush (FRAME_X_DISPLAY (f));
11417 UNBLOCK_INPUT;
11421 /* Lower frame F. */
11423 void
11424 x_lower_frame (f)
11425 struct frame *f;
11427 if (f->async_visible)
11429 BLOCK_INPUT;
11430 #ifdef USE_X_TOOLKIT
11431 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11432 #else /* not USE_X_TOOLKIT */
11433 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11434 #endif /* not USE_X_TOOLKIT */
11435 XFlush (FRAME_X_DISPLAY (f));
11436 UNBLOCK_INPUT;
11440 static void
11441 XTframe_raise_lower (f, raise_flag)
11442 FRAME_PTR f;
11443 int raise_flag;
11445 if (raise_flag)
11446 x_raise_frame (f);
11447 else
11448 x_lower_frame (f);
11451 /* Change of visibility. */
11453 /* This tries to wait until the frame is really visible.
11454 However, if the window manager asks the user where to position
11455 the frame, this will return before the user finishes doing that.
11456 The frame will not actually be visible at that time,
11457 but it will become visible later when the window manager
11458 finishes with it. */
11460 void
11461 x_make_frame_visible (f)
11462 struct frame *f;
11464 Lisp_Object type;
11465 int original_top, original_left;
11467 BLOCK_INPUT;
11469 type = x_icon_type (f);
11470 if (!NILP (type))
11471 x_bitmap_icon (f, type);
11473 if (! FRAME_VISIBLE_P (f))
11475 /* We test FRAME_GARBAGED_P here to make sure we don't
11476 call x_set_offset a second time
11477 if we get to x_make_frame_visible a second time
11478 before the window gets really visible. */
11479 if (! FRAME_ICONIFIED_P (f)
11480 && ! f->output_data.x->asked_for_visible)
11481 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11483 f->output_data.x->asked_for_visible = 1;
11485 if (! EQ (Vx_no_window_manager, Qt))
11486 x_wm_set_window_state (f, NormalState);
11487 #ifdef USE_X_TOOLKIT
11488 /* This was XtPopup, but that did nothing for an iconified frame. */
11489 XtMapWidget (f->output_data.x->widget);
11490 #else /* not USE_X_TOOLKIT */
11491 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11492 #endif /* not USE_X_TOOLKIT */
11493 #if 0 /* This seems to bring back scroll bars in the wrong places
11494 if the window configuration has changed. They seem
11495 to come back ok without this. */
11496 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11497 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11498 #endif
11501 XFlush (FRAME_X_DISPLAY (f));
11503 /* Synchronize to ensure Emacs knows the frame is visible
11504 before we do anything else. We do this loop with input not blocked
11505 so that incoming events are handled. */
11507 Lisp_Object frame;
11508 int count;
11509 /* This must be before UNBLOCK_INPUT
11510 since events that arrive in response to the actions above
11511 will set it when they are handled. */
11512 int previously_visible = f->output_data.x->has_been_visible;
11514 original_left = f->output_data.x->left_pos;
11515 original_top = f->output_data.x->top_pos;
11517 /* This must come after we set COUNT. */
11518 UNBLOCK_INPUT;
11520 /* We unblock here so that arriving X events are processed. */
11522 /* Now move the window back to where it was "supposed to be".
11523 But don't do it if the gravity is negative.
11524 When the gravity is negative, this uses a position
11525 that is 3 pixels too low. Perhaps that's really the border width.
11527 Don't do this if the window has never been visible before,
11528 because the window manager may choose the position
11529 and we don't want to override it. */
11531 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11532 && f->output_data.x->win_gravity == NorthWestGravity
11533 && previously_visible)
11535 Drawable rootw;
11536 int x, y;
11537 unsigned int width, height, border, depth;
11539 BLOCK_INPUT;
11541 /* On some window managers (such as FVWM) moving an existing
11542 window, even to the same place, causes the window manager
11543 to introduce an offset. This can cause the window to move
11544 to an unexpected location. Check the geometry (a little
11545 slow here) and then verify that the window is in the right
11546 place. If the window is not in the right place, move it
11547 there, and take the potential window manager hit. */
11548 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11549 &rootw, &x, &y, &width, &height, &border, &depth);
11551 if (original_left != x || original_top != y)
11552 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11553 original_left, original_top);
11555 UNBLOCK_INPUT;
11558 XSETFRAME (frame, f);
11560 /* Wait until the frame is visible. Process X events until a
11561 MapNotify event has been seen, or until we think we won't get a
11562 MapNotify at all.. */
11563 for (count = input_signal_count + 10;
11564 input_signal_count < count && !FRAME_VISIBLE_P (f);)
11566 /* Force processing of queued events. */
11567 x_sync (f);
11569 /* Machines that do polling rather than SIGIO have been
11570 observed to go into a busy-wait here. So we'll fake an
11571 alarm signal to let the handler know that there's something
11572 to be read. We used to raise a real alarm, but it seems
11573 that the handler isn't always enabled here. This is
11574 probably a bug. */
11575 if (input_polling_used ())
11577 /* It could be confusing if a real alarm arrives while
11578 processing the fake one. Turn it off and let the
11579 handler reset it. */
11580 int old_poll_suppress_count = poll_suppress_count;
11581 poll_suppress_count = 1;
11582 poll_for_input_1 ();
11583 poll_suppress_count = old_poll_suppress_count;
11586 /* See if a MapNotify event has been processed. */
11587 FRAME_SAMPLE_VISIBILITY (f);
11592 /* Change from mapped state to withdrawn state. */
11594 /* Make the frame visible (mapped and not iconified). */
11596 void
11597 x_make_frame_invisible (f)
11598 struct frame *f;
11600 Window window;
11602 #ifdef USE_X_TOOLKIT
11603 /* Use the frame's outermost window, not the one we normally draw on. */
11604 window = XtWindow (f->output_data.x->widget);
11605 #else /* not USE_X_TOOLKIT */
11606 window = FRAME_X_WINDOW (f);
11607 #endif /* not USE_X_TOOLKIT */
11609 /* Don't keep the highlight on an invisible frame. */
11610 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11611 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11613 #if 0/* This might add unreliability; I don't trust it -- rms. */
11614 if (! f->async_visible && ! f->async_iconified)
11615 return;
11616 #endif
11618 BLOCK_INPUT;
11620 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11621 that the current position of the window is user-specified, rather than
11622 program-specified, so that when the window is mapped again, it will be
11623 placed at the same location, without forcing the user to position it
11624 by hand again (they have already done that once for this window.) */
11625 x_wm_set_size_hint (f, (long) 0, 1);
11627 #ifdef HAVE_X11R4
11629 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11630 DefaultScreen (FRAME_X_DISPLAY (f))))
11632 UNBLOCK_INPUT_RESIGNAL;
11633 error ("Can't notify window manager of window withdrawal");
11635 #else /* ! defined (HAVE_X11R4) */
11637 /* Tell the window manager what we're going to do. */
11638 if (! EQ (Vx_no_window_manager, Qt))
11640 XEvent unmap;
11642 unmap.xunmap.type = UnmapNotify;
11643 unmap.xunmap.window = window;
11644 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
11645 unmap.xunmap.from_configure = False;
11646 if (! XSendEvent (FRAME_X_DISPLAY (f),
11647 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11648 False,
11649 SubstructureRedirectMaskSubstructureNotifyMask,
11650 &unmap))
11652 UNBLOCK_INPUT_RESIGNAL;
11653 error ("Can't notify window manager of withdrawal");
11657 /* Unmap the window ourselves. Cheeky! */
11658 XUnmapWindow (FRAME_X_DISPLAY (f), window);
11659 #endif /* ! defined (HAVE_X11R4) */
11661 /* We can't distinguish this from iconification
11662 just by the event that we get from the server.
11663 So we can't win using the usual strategy of letting
11664 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11665 and synchronize with the server to make sure we agree. */
11666 f->visible = 0;
11667 FRAME_ICONIFIED_P (f) = 0;
11668 f->async_visible = 0;
11669 f->async_iconified = 0;
11671 x_sync (f);
11673 UNBLOCK_INPUT;
11676 /* Change window state from mapped to iconified. */
11678 void
11679 x_iconify_frame (f)
11680 struct frame *f;
11682 int result;
11683 Lisp_Object type;
11685 /* Don't keep the highlight on an invisible frame. */
11686 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11687 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11689 if (f->async_iconified)
11690 return;
11692 BLOCK_INPUT;
11694 FRAME_SAMPLE_VISIBILITY (f);
11696 type = x_icon_type (f);
11697 if (!NILP (type))
11698 x_bitmap_icon (f, type);
11700 #ifdef USE_X_TOOLKIT
11702 if (! FRAME_VISIBLE_P (f))
11704 if (! EQ (Vx_no_window_manager, Qt))
11705 x_wm_set_window_state (f, IconicState);
11706 /* This was XtPopup, but that did nothing for an iconified frame. */
11707 XtMapWidget (f->output_data.x->widget);
11708 /* The server won't give us any event to indicate
11709 that an invisible frame was changed to an icon,
11710 so we have to record it here. */
11711 f->iconified = 1;
11712 f->visible = 1;
11713 f->async_iconified = 1;
11714 f->async_visible = 0;
11715 UNBLOCK_INPUT;
11716 return;
11719 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11720 XtWindow (f->output_data.x->widget),
11721 DefaultScreen (FRAME_X_DISPLAY (f)));
11722 UNBLOCK_INPUT;
11724 if (!result)
11725 error ("Can't notify window manager of iconification");
11727 f->async_iconified = 1;
11728 f->async_visible = 0;
11731 BLOCK_INPUT;
11732 XFlush (FRAME_X_DISPLAY (f));
11733 UNBLOCK_INPUT;
11734 #else /* not USE_X_TOOLKIT */
11736 /* Make sure the X server knows where the window should be positioned,
11737 in case the user deiconifies with the window manager. */
11738 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
11739 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11741 /* Since we don't know which revision of X we're running, we'll use both
11742 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11744 /* X11R4: send a ClientMessage to the window manager using the
11745 WM_CHANGE_STATE type. */
11747 XEvent message;
11749 message.xclient.window = FRAME_X_WINDOW (f);
11750 message.xclient.type = ClientMessage;
11751 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
11752 message.xclient.format = 32;
11753 message.xclient.data.l[0] = IconicState;
11755 if (! XSendEvent (FRAME_X_DISPLAY (f),
11756 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11757 False,
11758 SubstructureRedirectMask | SubstructureNotifyMask,
11759 &message))
11761 UNBLOCK_INPUT_RESIGNAL;
11762 error ("Can't notify window manager of iconification");
11766 /* X11R3: set the initial_state field of the window manager hints to
11767 IconicState. */
11768 x_wm_set_window_state (f, IconicState);
11770 if (!FRAME_VISIBLE_P (f))
11772 /* If the frame was withdrawn, before, we must map it. */
11773 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11776 f->async_iconified = 1;
11777 f->async_visible = 0;
11779 XFlush (FRAME_X_DISPLAY (f));
11780 UNBLOCK_INPUT;
11781 #endif /* not USE_X_TOOLKIT */
11784 /* Destroy the X window of frame F. */
11786 void
11787 x_destroy_window (f)
11788 struct frame *f;
11790 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11792 BLOCK_INPUT;
11794 /* If a display connection is dead, don't try sending more
11795 commands to the X server. */
11796 if (dpyinfo->display != 0)
11798 if (f->output_data.x->icon_desc != 0)
11799 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11800 #ifdef HAVE_X_I18N
11801 if (FRAME_XIC (f))
11802 free_frame_xic (f);
11803 #endif
11804 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
11805 #ifdef USE_X_TOOLKIT
11806 if (f->output_data.x->widget)
11807 XtDestroyWidget (f->output_data.x->widget);
11808 free_frame_menubar (f);
11809 #endif /* USE_X_TOOLKIT */
11811 free_frame_faces (f);
11812 XFlush (FRAME_X_DISPLAY (f));
11815 if (f->output_data.x->saved_menu_event)
11816 xfree (f->output_data.x->saved_menu_event);
11818 xfree (f->output_data.x);
11819 f->output_data.x = 0;
11820 if (f == dpyinfo->x_focus_frame)
11821 dpyinfo->x_focus_frame = 0;
11822 if (f == dpyinfo->x_focus_event_frame)
11823 dpyinfo->x_focus_event_frame = 0;
11824 if (f == dpyinfo->x_highlight_frame)
11825 dpyinfo->x_highlight_frame = 0;
11827 dpyinfo->reference_count--;
11829 if (f == dpyinfo->mouse_face_mouse_frame)
11831 dpyinfo->mouse_face_beg_row
11832 = dpyinfo->mouse_face_beg_col = -1;
11833 dpyinfo->mouse_face_end_row
11834 = dpyinfo->mouse_face_end_col = -1;
11835 dpyinfo->mouse_face_window = Qnil;
11836 dpyinfo->mouse_face_deferred_gc = 0;
11837 dpyinfo->mouse_face_mouse_frame = 0;
11840 UNBLOCK_INPUT;
11843 /* Setting window manager hints. */
11845 /* Set the normal size hints for the window manager, for frame F.
11846 FLAGS is the flags word to use--or 0 meaning preserve the flags
11847 that the window now has.
11848 If USER_POSITION is nonzero, we set the USPosition
11849 flag (this is useful when FLAGS is 0). */
11851 void
11852 x_wm_set_size_hint (f, flags, user_position)
11853 struct frame *f;
11854 long flags;
11855 int user_position;
11857 XSizeHints size_hints;
11859 #ifdef USE_X_TOOLKIT
11860 Arg al[2];
11861 int ac = 0;
11862 Dimension widget_width, widget_height;
11863 Window window = XtWindow (f->output_data.x->widget);
11864 #else /* not USE_X_TOOLKIT */
11865 Window window = FRAME_X_WINDOW (f);
11866 #endif /* not USE_X_TOOLKIT */
11868 /* Setting PMaxSize caused various problems. */
11869 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
11871 size_hints.x = f->output_data.x->left_pos;
11872 size_hints.y = f->output_data.x->top_pos;
11874 #ifdef USE_X_TOOLKIT
11875 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
11876 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
11877 XtGetValues (f->output_data.x->widget, al, ac);
11878 size_hints.height = widget_height;
11879 size_hints.width = widget_width;
11880 #else /* not USE_X_TOOLKIT */
11881 size_hints.height = PIXEL_HEIGHT (f);
11882 size_hints.width = PIXEL_WIDTH (f);
11883 #endif /* not USE_X_TOOLKIT */
11885 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
11886 size_hints.height_inc = f->output_data.x->line_height;
11887 size_hints.max_width
11888 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
11889 size_hints.max_height
11890 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
11892 /* Calculate the base and minimum sizes.
11894 (When we use the X toolkit, we don't do it here.
11895 Instead we copy the values that the widgets are using, below.) */
11896 #ifndef USE_X_TOOLKIT
11898 int base_width, base_height;
11899 int min_rows = 0, min_cols = 0;
11901 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
11902 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
11904 check_frame_size (f, &min_rows, &min_cols);
11906 /* The window manager uses the base width hints to calculate the
11907 current number of rows and columns in the frame while
11908 resizing; min_width and min_height aren't useful for this
11909 purpose, since they might not give the dimensions for a
11910 zero-row, zero-column frame.
11912 We use the base_width and base_height members if we have
11913 them; otherwise, we set the min_width and min_height members
11914 to the size for a zero x zero frame. */
11916 #ifdef HAVE_X11R4
11917 size_hints.flags |= PBaseSize;
11918 size_hints.base_width = base_width;
11919 size_hints.base_height = base_height;
11920 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
11921 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
11922 #else
11923 size_hints.min_width = base_width;
11924 size_hints.min_height = base_height;
11925 #endif
11928 /* If we don't need the old flags, we don't need the old hint at all. */
11929 if (flags)
11931 size_hints.flags |= flags;
11932 goto no_read;
11934 #endif /* not USE_X_TOOLKIT */
11937 XSizeHints hints; /* Sometimes I hate X Windows... */
11938 long supplied_return;
11939 int value;
11941 #ifdef HAVE_X11R4
11942 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
11943 &supplied_return);
11944 #else
11945 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
11946 #endif
11948 #ifdef USE_X_TOOLKIT
11949 size_hints.base_height = hints.base_height;
11950 size_hints.base_width = hints.base_width;
11951 size_hints.min_height = hints.min_height;
11952 size_hints.min_width = hints.min_width;
11953 #endif
11955 if (flags)
11956 size_hints.flags |= flags;
11957 else
11959 if (value == 0)
11960 hints.flags = 0;
11961 if (hints.flags & PSize)
11962 size_hints.flags |= PSize;
11963 if (hints.flags & PPosition)
11964 size_hints.flags |= PPosition;
11965 if (hints.flags & USPosition)
11966 size_hints.flags |= USPosition;
11967 if (hints.flags & USSize)
11968 size_hints.flags |= USSize;
11972 #ifndef USE_X_TOOLKIT
11973 no_read:
11974 #endif
11976 #ifdef PWinGravity
11977 size_hints.win_gravity = f->output_data.x->win_gravity;
11978 size_hints.flags |= PWinGravity;
11980 if (user_position)
11982 size_hints.flags &= ~ PPosition;
11983 size_hints.flags |= USPosition;
11985 #endif /* PWinGravity */
11987 #ifdef HAVE_X11R4
11988 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11989 #else
11990 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
11991 #endif
11994 /* Used for IconicState or NormalState */
11996 void
11997 x_wm_set_window_state (f, state)
11998 struct frame *f;
11999 int state;
12001 #ifdef USE_X_TOOLKIT
12002 Arg al[1];
12004 XtSetArg (al[0], XtNinitialState, state);
12005 XtSetValues (f->output_data.x->widget, al, 1);
12006 #else /* not USE_X_TOOLKIT */
12007 Window window = FRAME_X_WINDOW (f);
12009 f->output_data.x->wm_hints.flags |= StateHint;
12010 f->output_data.x->wm_hints.initial_state = state;
12012 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12013 #endif /* not USE_X_TOOLKIT */
12016 void
12017 x_wm_set_icon_pixmap (f, pixmap_id)
12018 struct frame *f;
12019 int pixmap_id;
12021 Pixmap icon_pixmap;
12023 #ifndef USE_X_TOOLKIT
12024 Window window = FRAME_X_WINDOW (f);
12025 #endif
12027 if (pixmap_id > 0)
12029 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12030 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12032 else
12034 /* It seems there is no way to turn off use of an icon pixmap.
12035 The following line does it, only if no icon has yet been created,
12036 for some window managers. But with mwm it crashes.
12037 Some people say it should clear the IconPixmapHint bit in this case,
12038 but that doesn't work, and the X consortium said it isn't the
12039 right thing at all. Since there is no way to win,
12040 best to explicitly give up. */
12041 #if 0
12042 f->output_data.x->wm_hints.icon_pixmap = None;
12043 #else
12044 return;
12045 #endif
12048 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12051 Arg al[1];
12052 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12053 XtSetValues (f->output_data.x->widget, al, 1);
12056 #else /* not USE_X_TOOLKIT */
12058 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12059 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12061 #endif /* not USE_X_TOOLKIT */
12064 void
12065 x_wm_set_icon_position (f, icon_x, icon_y)
12066 struct frame *f;
12067 int icon_x, icon_y;
12069 #ifdef USE_X_TOOLKIT
12070 Window window = XtWindow (f->output_data.x->widget);
12071 #else
12072 Window window = FRAME_X_WINDOW (f);
12073 #endif
12075 f->output_data.x->wm_hints.flags |= IconPositionHint;
12076 f->output_data.x->wm_hints.icon_x = icon_x;
12077 f->output_data.x->wm_hints.icon_y = icon_y;
12079 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12083 /***********************************************************************
12084 Fonts
12085 ***********************************************************************/
12087 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12089 struct font_info *
12090 x_get_font_info (f, font_idx)
12091 FRAME_PTR f;
12092 int font_idx;
12094 return (FRAME_X_FONT_TABLE (f) + font_idx);
12098 /* Return a list of names of available fonts matching PATTERN on frame
12099 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12100 to be listed. Frame F NULL means we have not yet created any
12101 frame on X, and consult the first display in x_display_list.
12102 MAXNAMES sets a limit on how many fonts to match. */
12104 Lisp_Object
12105 x_list_fonts (f, pattern, size, maxnames)
12106 FRAME_PTR f;
12107 Lisp_Object pattern;
12108 int size;
12109 int maxnames;
12111 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12112 Lisp_Object tem, second_best;
12113 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12114 int try_XLoadQueryFont = 0;
12115 int count;
12117 patterns = Fassoc (pattern, Valternate_fontname_alist);
12118 if (NILP (patterns))
12119 patterns = Fcons (pattern, Qnil);
12121 if (maxnames == 1 && !size)
12122 /* We can return any single font matching PATTERN. */
12123 try_XLoadQueryFont = 1;
12125 for (; CONSP (patterns); patterns = XCDR (patterns))
12127 int num_fonts;
12128 char **names;
12130 pattern = XCAR (patterns);
12131 /* See if we cached the result for this particular query.
12132 The cache is an alist of the form:
12133 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12135 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12136 key = Fcons (pattern, make_number (maxnames)),
12137 !NILP (list = Fassoc (key, tem))))
12139 list = Fcdr_safe (list);
12140 /* We have a cashed list. Don't have to get the list again. */
12141 goto label_cached;
12144 /* At first, put PATTERN in the cache. */
12146 BLOCK_INPUT;
12147 count = x_catch_errors (dpy);
12149 if (try_XLoadQueryFont)
12151 XFontStruct *font;
12152 unsigned long value;
12154 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12155 if (x_had_errors_p (dpy))
12157 /* This error is perhaps due to insufficient memory on X
12158 server. Let's just ignore it. */
12159 font = NULL;
12160 x_clear_errors (dpy);
12163 if (font
12164 && XGetFontProperty (font, XA_FONT, &value))
12166 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12167 int len = strlen (name);
12168 char *tmp;
12170 /* If DXPC (a Differential X Protocol Compressor)
12171 Ver.3.7 is running, XGetAtomName will return null
12172 string. We must avoid such a name. */
12173 if (len == 0)
12174 try_XLoadQueryFont = 0;
12175 else
12177 num_fonts = 1;
12178 names = (char **) alloca (sizeof (char *));
12179 /* Some systems only allow alloca assigned to a
12180 simple var. */
12181 tmp = (char *) alloca (len + 1); names[0] = tmp;
12182 bcopy (name, names[0], len + 1);
12183 XFree (name);
12186 else
12187 try_XLoadQueryFont = 0;
12189 if (font)
12190 XFreeFont (dpy, font);
12193 if (!try_XLoadQueryFont)
12195 /* We try at least 10 fonts because XListFonts will return
12196 auto-scaled fonts at the head. */
12197 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12198 &num_fonts);
12199 if (x_had_errors_p (dpy))
12201 /* This error is perhaps due to insufficient memory on X
12202 server. Let's just ignore it. */
12203 names = NULL;
12204 x_clear_errors (dpy);
12208 x_uncatch_errors (dpy, count);
12209 UNBLOCK_INPUT;
12211 if (names)
12213 int i;
12215 /* Make a list of all the fonts we got back.
12216 Store that in the font cache for the display. */
12217 for (i = 0; i < num_fonts; i++)
12219 int width = 0;
12220 char *p = names[i];
12221 int average_width = -1, dashes = 0;
12223 /* Count the number of dashes in NAMES[I]. If there are
12224 14 dashes, and the field value following 12th dash
12225 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12226 is usually too ugly to be used for editing. Let's
12227 ignore it. */
12228 while (*p)
12229 if (*p++ == '-')
12231 dashes++;
12232 if (dashes == 7) /* PIXEL_SIZE field */
12233 width = atoi (p);
12234 else if (dashes == 12) /* AVERAGE_WIDTH field */
12235 average_width = atoi (p);
12237 if (dashes < 14 || average_width != 0)
12239 tem = build_string (names[i]);
12240 if (NILP (Fassoc (tem, list)))
12242 if (STRINGP (Vx_pixel_size_width_font_regexp)
12243 && ((fast_c_string_match_ignore_case
12244 (Vx_pixel_size_width_font_regexp, names[i]))
12245 >= 0))
12246 /* We can set the value of PIXEL_SIZE to the
12247 width of this font. */
12248 list = Fcons (Fcons (tem, make_number (width)), list);
12249 else
12250 /* For the moment, width is not known. */
12251 list = Fcons (Fcons (tem, Qnil), list);
12255 if (!try_XLoadQueryFont)
12256 XFreeFontNames (names);
12259 /* Now store the result in the cache. */
12260 if (f != NULL)
12261 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12262 = Fcons (Fcons (key, list),
12263 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12265 label_cached:
12266 if (NILP (list)) continue; /* Try the remaining alternatives. */
12268 newlist = second_best = Qnil;
12269 /* Make a list of the fonts that have the right width. */
12270 for (; CONSP (list); list = XCDR (list))
12272 int found_size;
12274 tem = XCAR (list);
12276 if (!CONSP (tem) || NILP (XCAR (tem)))
12277 continue;
12278 if (!size)
12280 newlist = Fcons (XCAR (tem), newlist);
12281 continue;
12284 if (!INTEGERP (XCDR (tem)))
12286 /* Since we have not yet known the size of this font, we
12287 must try slow function call XLoadQueryFont. */
12288 XFontStruct *thisinfo;
12290 BLOCK_INPUT;
12291 count = x_catch_errors (dpy);
12292 thisinfo = XLoadQueryFont (dpy,
12293 XSTRING (XCAR (tem))->data);
12294 if (x_had_errors_p (dpy))
12296 /* This error is perhaps due to insufficient memory on X
12297 server. Let's just ignore it. */
12298 thisinfo = NULL;
12299 x_clear_errors (dpy);
12301 x_uncatch_errors (dpy, count);
12302 UNBLOCK_INPUT;
12304 if (thisinfo)
12306 XCDR (tem)
12307 = (thisinfo->min_bounds.width == 0
12308 ? make_number (0)
12309 : make_number (thisinfo->max_bounds.width));
12310 XFreeFont (dpy, thisinfo);
12312 else
12313 /* For unknown reason, the previous call of XListFont had
12314 returned a font which can't be opened. Record the size
12315 as 0 not to try to open it again. */
12316 XCDR (tem) = make_number (0);
12319 found_size = XINT (XCDR (tem));
12320 if (found_size == size)
12321 newlist = Fcons (XCAR (tem), newlist);
12322 else if (found_size > 0)
12324 if (NILP (second_best))
12325 second_best = tem;
12326 else if (found_size < size)
12328 if (XINT (XCDR (second_best)) > size
12329 || XINT (XCDR (second_best)) < found_size)
12330 second_best = tem;
12332 else
12334 if (XINT (XCDR (second_best)) > size
12335 && XINT (XCDR (second_best)) > found_size)
12336 second_best = tem;
12340 if (!NILP (newlist))
12341 break;
12342 else if (!NILP (second_best))
12344 newlist = Fcons (XCAR (second_best), Qnil);
12345 break;
12349 return newlist;
12353 #if GLYPH_DEBUG
12355 /* Check that FONT is valid on frame F. It is if it can be found in F's
12356 font table. */
12358 static void
12359 x_check_font (f, font)
12360 struct frame *f;
12361 XFontStruct *font;
12363 int i;
12364 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12366 xassert (font != NULL);
12368 for (i = 0; i < dpyinfo->n_fonts; i++)
12369 if (dpyinfo->font_table[i].name
12370 && font == dpyinfo->font_table[i].font)
12371 break;
12373 xassert (i < dpyinfo->n_fonts);
12376 #endif /* GLYPH_DEBUG != 0 */
12378 /* Set *W to the minimum width, *H to the minimum font height of FONT.
12379 Note: There are (broken) X fonts out there with invalid XFontStruct
12380 min_bounds contents. For example, handa@etl.go.jp reports that
12381 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12382 have font->min_bounds.width == 0. */
12384 static INLINE void
12385 x_font_min_bounds (font, w, h)
12386 XFontStruct *font;
12387 int *w, *h;
12389 *h = FONT_HEIGHT (font);
12390 *w = font->min_bounds.width;
12392 /* Try to handle the case where FONT->min_bounds has invalid
12393 contents. Since the only font known to have invalid min_bounds
12394 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12395 if (*w <= 0)
12396 *w = font->max_bounds.width;
12400 /* Compute the smallest character width and smallest font height over
12401 all fonts available on frame F. Set the members smallest_char_width
12402 and smallest_font_height in F's x_display_info structure to
12403 the values computed. Value is non-zero if smallest_font_height or
12404 smallest_char_width become smaller than they were before. */
12406 static int
12407 x_compute_min_glyph_bounds (f)
12408 struct frame *f;
12410 int i;
12411 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12412 XFontStruct *font;
12413 int old_width = dpyinfo->smallest_char_width;
12414 int old_height = dpyinfo->smallest_font_height;
12416 dpyinfo->smallest_font_height = 100000;
12417 dpyinfo->smallest_char_width = 100000;
12419 for (i = 0; i < dpyinfo->n_fonts; ++i)
12420 if (dpyinfo->font_table[i].name)
12422 struct font_info *fontp = dpyinfo->font_table + i;
12423 int w, h;
12425 font = (XFontStruct *) fontp->font;
12426 xassert (font != (XFontStruct *) ~0);
12427 x_font_min_bounds (font, &w, &h);
12429 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12430 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12433 xassert (dpyinfo->smallest_char_width > 0
12434 && dpyinfo->smallest_font_height > 0);
12436 return (dpyinfo->n_fonts == 1
12437 || dpyinfo->smallest_char_width < old_width
12438 || dpyinfo->smallest_font_height < old_height);
12442 /* Load font named FONTNAME of the size SIZE for frame F, and return a
12443 pointer to the structure font_info while allocating it dynamically.
12444 If SIZE is 0, load any size of font.
12445 If loading is failed, return NULL. */
12447 struct font_info *
12448 x_load_font (f, fontname, size)
12449 struct frame *f;
12450 register char *fontname;
12451 int size;
12453 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12454 Lisp_Object font_names;
12455 int count;
12457 /* Get a list of all the fonts that match this name. Once we
12458 have a list of matching fonts, we compare them against the fonts
12459 we already have by comparing names. */
12460 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12462 if (!NILP (font_names))
12464 Lisp_Object tail;
12465 int i;
12467 for (i = 0; i < dpyinfo->n_fonts; i++)
12468 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
12469 if (dpyinfo->font_table[i].name
12470 && (!strcmp (dpyinfo->font_table[i].name,
12471 XSTRING (XCAR (tail))->data)
12472 || !strcmp (dpyinfo->font_table[i].full_name,
12473 XSTRING (XCAR (tail))->data)))
12474 return (dpyinfo->font_table + i);
12477 /* Load the font and add it to the table. */
12479 char *full_name;
12480 XFontStruct *font;
12481 struct font_info *fontp;
12482 unsigned long value;
12483 int i;
12485 /* If we have found fonts by x_list_font, load one of them. If
12486 not, we still try to load a font by the name given as FONTNAME
12487 because XListFonts (called in x_list_font) of some X server has
12488 a bug of not finding a font even if the font surely exists and
12489 is loadable by XLoadQueryFont. */
12490 if (size > 0 && !NILP (font_names))
12491 fontname = (char *) XSTRING (XCAR (font_names))->data;
12493 BLOCK_INPUT;
12494 count = x_catch_errors (FRAME_X_DISPLAY (f));
12495 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12496 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12498 /* This error is perhaps due to insufficient memory on X
12499 server. Let's just ignore it. */
12500 font = NULL;
12501 x_clear_errors (FRAME_X_DISPLAY (f));
12503 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12504 UNBLOCK_INPUT;
12505 if (!font)
12506 return NULL;
12508 /* Find a free slot in the font table. */
12509 for (i = 0; i < dpyinfo->n_fonts; ++i)
12510 if (dpyinfo->font_table[i].name == NULL)
12511 break;
12513 /* If no free slot found, maybe enlarge the font table. */
12514 if (i == dpyinfo->n_fonts
12515 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12517 int sz;
12518 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12519 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12520 dpyinfo->font_table
12521 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12524 fontp = dpyinfo->font_table + i;
12525 if (i == dpyinfo->n_fonts)
12526 ++dpyinfo->n_fonts;
12528 /* Now fill in the slots of *FONTP. */
12529 BLOCK_INPUT;
12530 fontp->font = font;
12531 fontp->font_idx = i;
12532 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12533 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12535 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12536 full_name = 0;
12537 if (XGetFontProperty (font, XA_FONT, &value))
12539 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12540 char *p = name;
12541 int dashes = 0;
12543 /* Count the number of dashes in the "full name".
12544 If it is too few, this isn't really the font's full name,
12545 so don't use it.
12546 In X11R4, the fonts did not come with their canonical names
12547 stored in them. */
12548 while (*p)
12550 if (*p == '-')
12551 dashes++;
12552 p++;
12555 if (dashes >= 13)
12557 full_name = (char *) xmalloc (p - name + 1);
12558 bcopy (name, full_name, p - name + 1);
12561 XFree (name);
12564 if (full_name != 0)
12565 fontp->full_name = full_name;
12566 else
12567 fontp->full_name = fontp->name;
12569 fontp->size = font->max_bounds.width;
12570 fontp->height = FONT_HEIGHT (font);
12572 /* For some font, ascent and descent in max_bounds field is
12573 larger than the above value. */
12574 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12575 if (max_height > fontp->height)
12576 fontp->height = max_height;
12579 if (NILP (font_names))
12581 /* We come here because of a bug of XListFonts mentioned at
12582 the head of this block. Let's store this information in
12583 the cache for x_list_fonts. */
12584 Lisp_Object lispy_name = build_string (fontname);
12585 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12587 XCDR (dpyinfo->name_list_element)
12588 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12589 Fcons (Fcons (lispy_full_name,
12590 make_number (fontp->size)),
12591 Qnil)),
12592 XCDR (dpyinfo->name_list_element));
12593 if (full_name)
12594 XCDR (dpyinfo->name_list_element)
12595 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12596 Fcons (Fcons (lispy_full_name,
12597 make_number (fontp->size)),
12598 Qnil)),
12599 XCDR (dpyinfo->name_list_element));
12602 /* The slot `encoding' specifies how to map a character
12603 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
12604 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12605 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
12606 2:0xA020..0xFF7F). For the moment, we don't know which charset
12607 uses this font. So, we set information in fontp->encoding[1]
12608 which is never used by any charset. If mapping can't be
12609 decided, set FONT_ENCODING_NOT_DECIDED. */
12610 fontp->encoding[1]
12611 = (font->max_byte1 == 0
12612 /* 1-byte font */
12613 ? (font->min_char_or_byte2 < 0x80
12614 ? (font->max_char_or_byte2 < 0x80
12615 ? 0 /* 0x20..0x7F */
12616 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
12617 : 1) /* 0xA0..0xFF */
12618 /* 2-byte font */
12619 : (font->min_byte1 < 0x80
12620 ? (font->max_byte1 < 0x80
12621 ? (font->min_char_or_byte2 < 0x80
12622 ? (font->max_char_or_byte2 < 0x80
12623 ? 0 /* 0x2020..0x7F7F */
12624 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
12625 : 3) /* 0x20A0..0x7FFF */
12626 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
12627 : (font->min_char_or_byte2 < 0x80
12628 ? (font->max_char_or_byte2 < 0x80
12629 ? 2 /* 0xA020..0xFF7F */
12630 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
12631 : 1))); /* 0xA0A0..0xFFFF */
12633 fontp->baseline_offset
12634 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12635 ? (long) value : 0);
12636 fontp->relative_compose
12637 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12638 ? (long) value : 0);
12639 fontp->default_ascent
12640 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12641 ? (long) value : 0);
12643 /* Set global flag fonts_changed_p to non-zero if the font loaded
12644 has a character with a smaller width than any other character
12645 before, or if the font loaded has a smalle>r height than any
12646 other font loaded before. If this happens, it will make a
12647 glyph matrix reallocation necessary. */
12648 fonts_changed_p = x_compute_min_glyph_bounds (f);
12649 UNBLOCK_INPUT;
12650 return fontp;
12655 /* Return a pointer to struct font_info of a font named FONTNAME for
12656 frame F. If no such font is loaded, return NULL. */
12658 struct font_info *
12659 x_query_font (f, fontname)
12660 struct frame *f;
12661 register char *fontname;
12663 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12664 int i;
12666 for (i = 0; i < dpyinfo->n_fonts; i++)
12667 if (dpyinfo->font_table[i].name
12668 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12669 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
12670 return (dpyinfo->font_table + i);
12671 return NULL;
12675 /* Find a CCL program for a font specified by FONTP, and set the member
12676 `encoder' of the structure. */
12678 void
12679 x_find_ccl_program (fontp)
12680 struct font_info *fontp;
12682 Lisp_Object list, elt;
12684 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
12686 elt = XCAR (list);
12687 if (CONSP (elt)
12688 && STRINGP (XCAR (elt))
12689 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
12690 >= 0))
12691 break;
12693 if (! NILP (list))
12695 struct ccl_program *ccl
12696 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
12698 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
12699 xfree (ccl);
12700 else
12701 fontp->font_encoder = ccl;
12707 /***********************************************************************
12708 Initialization
12709 ***********************************************************************/
12711 #ifdef USE_X_TOOLKIT
12712 static XrmOptionDescRec emacs_options[] = {
12713 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12714 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12716 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12717 XrmoptionSepArg, NULL},
12718 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12720 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12721 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12722 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12723 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12724 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12725 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12726 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12728 #endif /* USE_X_TOOLKIT */
12730 static int x_initialized;
12732 #ifdef MULTI_KBOARD
12733 /* Test whether two display-name strings agree up to the dot that separates
12734 the screen number from the server number. */
12735 static int
12736 same_x_server (name1, name2)
12737 char *name1, *name2;
12739 int seen_colon = 0;
12740 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12741 int system_name_length = strlen (system_name);
12742 int length_until_period = 0;
12744 while (system_name[length_until_period] != 0
12745 && system_name[length_until_period] != '.')
12746 length_until_period++;
12748 /* Treat `unix' like an empty host name. */
12749 if (! strncmp (name1, "unix:", 5))
12750 name1 += 4;
12751 if (! strncmp (name2, "unix:", 5))
12752 name2 += 4;
12753 /* Treat this host's name like an empty host name. */
12754 if (! strncmp (name1, system_name, system_name_length)
12755 && name1[system_name_length] == ':')
12756 name1 += system_name_length;
12757 if (! strncmp (name2, system_name, system_name_length)
12758 && name2[system_name_length] == ':')
12759 name2 += system_name_length;
12760 /* Treat this host's domainless name like an empty host name. */
12761 if (! strncmp (name1, system_name, length_until_period)
12762 && name1[length_until_period] == ':')
12763 name1 += length_until_period;
12764 if (! strncmp (name2, system_name, length_until_period)
12765 && name2[length_until_period] == ':')
12766 name2 += length_until_period;
12768 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12770 if (*name1 == ':')
12771 seen_colon++;
12772 if (seen_colon && *name1 == '.')
12773 return 1;
12775 return (seen_colon
12776 && (*name1 == '.' || *name1 == '\0')
12777 && (*name2 == '.' || *name2 == '\0'));
12779 #endif
12781 struct x_display_info *
12782 x_term_init (display_name, xrm_option, resource_name)
12783 Lisp_Object display_name;
12784 char *xrm_option;
12785 char *resource_name;
12787 int connection;
12788 Display *dpy;
12789 struct x_display_info *dpyinfo;
12790 XrmDatabase xrdb;
12792 BLOCK_INPUT;
12794 if (!x_initialized)
12796 x_initialize ();
12797 x_initialized = 1;
12800 #ifdef USE_X_TOOLKIT
12801 /* weiner@footloose.sps.mot.com reports that this causes
12802 errors with X11R5:
12803 X protocol error: BadAtom (invalid Atom parameter)
12804 on protocol request 18skiloaf.
12805 So let's not use it until R6. */
12806 #ifdef HAVE_X11XTR6
12807 XtSetLanguageProc (NULL, NULL, NULL);
12808 #endif
12811 int argc = 0;
12812 char *argv[3];
12814 argv[0] = "";
12815 argc = 1;
12816 if (xrm_option)
12818 argv[argc++] = "-xrm";
12819 argv[argc++] = xrm_option;
12821 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12822 resource_name, EMACS_CLASS,
12823 emacs_options, XtNumber (emacs_options),
12824 &argc, argv);
12826 #ifdef HAVE_X11XTR6
12827 /* I think this is to compensate for XtSetLanguageProc. */
12828 fixup_locale ();
12829 #endif
12832 #else /* not USE_X_TOOLKIT */
12833 #ifdef HAVE_X11R5
12834 XSetLocaleModifiers ("");
12835 #endif
12836 dpy = XOpenDisplay (XSTRING (display_name)->data);
12837 #endif /* not USE_X_TOOLKIT */
12839 /* Detect failure. */
12840 if (dpy == 0)
12842 UNBLOCK_INPUT;
12843 return 0;
12846 /* We have definitely succeeded. Record the new connection. */
12848 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
12850 #ifdef MULTI_KBOARD
12852 struct x_display_info *share;
12853 Lisp_Object tail;
12855 for (share = x_display_list, tail = x_display_name_list; share;
12856 share = share->next, tail = XCDR (tail))
12857 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
12858 XSTRING (display_name)->data))
12859 break;
12860 if (share)
12861 dpyinfo->kboard = share->kboard;
12862 else
12864 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
12865 init_kboard (dpyinfo->kboard);
12866 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
12868 char *vendor = ServerVendor (dpy);
12869 dpyinfo->kboard->Vsystem_key_alist
12870 = call1 (Qvendor_specific_keysyms,
12871 build_string (vendor ? vendor : ""));
12874 dpyinfo->kboard->next_kboard = all_kboards;
12875 all_kboards = dpyinfo->kboard;
12876 /* Don't let the initial kboard remain current longer than necessary.
12877 That would cause problems if a file loaded on startup tries to
12878 prompt in the mini-buffer. */
12879 if (current_kboard == initial_kboard)
12880 current_kboard = dpyinfo->kboard;
12882 dpyinfo->kboard->reference_count++;
12884 #endif
12886 /* Put this display on the chain. */
12887 dpyinfo->next = x_display_list;
12888 x_display_list = dpyinfo;
12890 /* Put it on x_display_name_list as well, to keep them parallel. */
12891 x_display_name_list = Fcons (Fcons (display_name, Qnil),
12892 x_display_name_list);
12893 dpyinfo->name_list_element = XCAR (x_display_name_list);
12895 dpyinfo->display = dpy;
12897 #if 0
12898 XSetAfterFunction (x_current_display, x_trace_wire);
12899 #endif /* ! 0 */
12901 dpyinfo->x_id_name
12902 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
12903 + STRING_BYTES (XSTRING (Vsystem_name))
12904 + 2);
12905 sprintf (dpyinfo->x_id_name, "%s@%s",
12906 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
12908 /* Figure out which modifier bits mean what. */
12909 x_find_modifier_meanings (dpyinfo);
12911 /* Get the scroll bar cursor. */
12912 dpyinfo->vertical_scroll_bar_cursor
12913 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
12915 xrdb = x_load_resources (dpyinfo->display, xrm_option,
12916 resource_name, EMACS_CLASS);
12917 #ifdef HAVE_XRMSETDATABASE
12918 XrmSetDatabase (dpyinfo->display, xrdb);
12919 #else
12920 dpyinfo->display->db = xrdb;
12921 #endif
12922 /* Put the rdb where we can find it in a way that works on
12923 all versions. */
12924 dpyinfo->xrdb = xrdb;
12926 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
12927 DefaultScreen (dpyinfo->display));
12928 select_visual (dpyinfo);
12929 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
12930 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
12931 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
12932 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
12933 dpyinfo->grabbed = 0;
12934 dpyinfo->reference_count = 0;
12935 dpyinfo->icon_bitmap_id = -1;
12936 dpyinfo->font_table = NULL;
12937 dpyinfo->n_fonts = 0;
12938 dpyinfo->font_table_size = 0;
12939 dpyinfo->bitmaps = 0;
12940 dpyinfo->bitmaps_size = 0;
12941 dpyinfo->bitmaps_last = 0;
12942 dpyinfo->scratch_cursor_gc = 0;
12943 dpyinfo->mouse_face_mouse_frame = 0;
12944 dpyinfo->mouse_face_deferred_gc = 0;
12945 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
12946 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
12947 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
12948 dpyinfo->mouse_face_window = Qnil;
12949 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
12950 dpyinfo->mouse_face_defer = 0;
12951 dpyinfo->x_focus_frame = 0;
12952 dpyinfo->x_focus_event_frame = 0;
12953 dpyinfo->x_highlight_frame = 0;
12954 dpyinfo->image_cache = make_image_cache ();
12956 /* See if a private colormap is requested. */
12957 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
12959 if (dpyinfo->visual->class == PseudoColor)
12961 Lisp_Object value;
12962 value = display_x_get_resource (dpyinfo,
12963 build_string ("privateColormap"),
12964 build_string ("PrivateColormap"),
12965 Qnil, Qnil);
12966 if (STRINGP (value)
12967 && (!strcmp (XSTRING (value)->data, "true")
12968 || !strcmp (XSTRING (value)->data, "on")))
12969 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
12972 else
12973 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
12974 dpyinfo->visual, AllocNone);
12977 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
12978 double pixels = DisplayHeight (dpyinfo->display, screen_number);
12979 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
12980 dpyinfo->resy = pixels * 25.4 / mm;
12981 pixels = DisplayWidth (dpyinfo->display, screen_number);
12982 mm = DisplayWidthMM (dpyinfo->display, screen_number);
12983 dpyinfo->resx = pixels * 25.4 / mm;
12986 dpyinfo->Xatom_wm_protocols
12987 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
12988 dpyinfo->Xatom_wm_take_focus
12989 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
12990 dpyinfo->Xatom_wm_save_yourself
12991 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
12992 dpyinfo->Xatom_wm_delete_window
12993 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
12994 dpyinfo->Xatom_wm_change_state
12995 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
12996 dpyinfo->Xatom_wm_configure_denied
12997 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
12998 dpyinfo->Xatom_wm_window_moved
12999 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13000 dpyinfo->Xatom_editres
13001 = XInternAtom (dpyinfo->display, "Editres", False);
13002 dpyinfo->Xatom_CLIPBOARD
13003 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13004 dpyinfo->Xatom_TIMESTAMP
13005 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13006 dpyinfo->Xatom_TEXT
13007 = XInternAtom (dpyinfo->display, "TEXT", False);
13008 dpyinfo->Xatom_COMPOUND_TEXT
13009 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13010 dpyinfo->Xatom_DELETE
13011 = XInternAtom (dpyinfo->display, "DELETE", False);
13012 dpyinfo->Xatom_MULTIPLE
13013 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13014 dpyinfo->Xatom_INCR
13015 = XInternAtom (dpyinfo->display, "INCR", False);
13016 dpyinfo->Xatom_EMACS_TMP
13017 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13018 dpyinfo->Xatom_TARGETS
13019 = XInternAtom (dpyinfo->display, "TARGETS", False);
13020 dpyinfo->Xatom_NULL
13021 = XInternAtom (dpyinfo->display, "NULL", False);
13022 dpyinfo->Xatom_ATOM_PAIR
13023 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13024 /* For properties of font. */
13025 dpyinfo->Xatom_PIXEL_SIZE
13026 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13027 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13028 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13029 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13030 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13031 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13032 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13034 /* Ghostscript support. */
13035 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13036 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13038 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13039 False);
13041 dpyinfo->cut_buffers_initialized = 0;
13043 connection = ConnectionNumber (dpyinfo->display);
13044 dpyinfo->connection = connection;
13047 char null_bits[1];
13049 null_bits[0] = 0x00;
13051 dpyinfo->null_pixel
13052 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13053 null_bits, 1, 1, (long) 0, (long) 0,
13058 extern int gray_bitmap_width, gray_bitmap_height;
13059 extern unsigned char *gray_bitmap_bits;
13060 dpyinfo->gray
13061 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13062 gray_bitmap_bits,
13063 gray_bitmap_width, gray_bitmap_height,
13064 (unsigned long) 1, (unsigned long) 0, 1);
13067 #ifdef HAVE_X_I18N
13068 xim_initialize (dpyinfo, resource_name);
13069 #endif
13071 #ifdef subprocesses
13072 /* This is only needed for distinguishing keyboard and process input. */
13073 if (connection != 0)
13074 add_keyboard_wait_descriptor (connection);
13075 #endif
13077 #ifndef F_SETOWN_BUG
13078 #ifdef F_SETOWN
13079 #ifdef F_SETOWN_SOCK_NEG
13080 /* stdin is a socket here */
13081 fcntl (connection, F_SETOWN, -getpid ());
13082 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13083 fcntl (connection, F_SETOWN, getpid ());
13084 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13085 #endif /* ! defined (F_SETOWN) */
13086 #endif /* F_SETOWN_BUG */
13088 #ifdef SIGIO
13089 if (interrupt_input)
13090 init_sigio (connection);
13091 #endif /* ! defined (SIGIO) */
13093 #ifdef USE_LUCID
13094 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13095 /* Make sure that we have a valid font for dialog boxes
13096 so that Xt does not crash. */
13098 Display *dpy = dpyinfo->display;
13099 XrmValue d, fr, to;
13100 Font font;
13101 int count;
13103 d.addr = (XPointer)&dpy;
13104 d.size = sizeof (Display *);
13105 fr.addr = XtDefaultFont;
13106 fr.size = sizeof (XtDefaultFont);
13107 to.size = sizeof (Font *);
13108 to.addr = (XPointer)&font;
13109 count = x_catch_errors (dpy);
13110 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13111 abort ();
13112 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13113 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13114 x_uncatch_errors (dpy, count);
13116 #endif
13117 #endif
13119 /* See if we should run in synchronous mode. This is useful
13120 for debugging X code. */
13122 Lisp_Object value;
13123 value = display_x_get_resource (dpyinfo,
13124 build_string ("synchronous"),
13125 build_string ("Synchronous"),
13126 Qnil, Qnil);
13127 if (STRINGP (value)
13128 && (!strcmp (XSTRING (value)->data, "true")
13129 || !strcmp (XSTRING (value)->data, "on")))
13130 XSynchronize (dpyinfo->display, True);
13133 UNBLOCK_INPUT;
13135 return dpyinfo;
13138 /* Get rid of display DPYINFO, assuming all frames are already gone,
13139 and without sending any more commands to the X server. */
13141 void
13142 x_delete_display (dpyinfo)
13143 struct x_display_info *dpyinfo;
13145 delete_keyboard_wait_descriptor (dpyinfo->connection);
13147 /* Discard this display from x_display_name_list and x_display_list.
13148 We can't use Fdelq because that can quit. */
13149 if (! NILP (x_display_name_list)
13150 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13151 x_display_name_list = XCDR (x_display_name_list);
13152 else
13154 Lisp_Object tail;
13156 tail = x_display_name_list;
13157 while (CONSP (tail) && CONSP (XCDR (tail)))
13159 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13161 XCDR (tail) = XCDR (XCDR (tail));
13162 break;
13164 tail = XCDR (tail);
13168 if (next_noop_dpyinfo == dpyinfo)
13169 next_noop_dpyinfo = dpyinfo->next;
13171 if (x_display_list == dpyinfo)
13172 x_display_list = dpyinfo->next;
13173 else
13175 struct x_display_info *tail;
13177 for (tail = x_display_list; tail; tail = tail->next)
13178 if (tail->next == dpyinfo)
13179 tail->next = tail->next->next;
13182 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13183 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13184 XrmDestroyDatabase (dpyinfo->xrdb);
13185 #endif
13186 #endif
13187 #ifdef MULTI_KBOARD
13188 if (--dpyinfo->kboard->reference_count == 0)
13189 delete_kboard (dpyinfo->kboard);
13190 #endif
13191 #ifdef HAVE_X_I18N
13192 if (dpyinfo->xim)
13193 xim_close_dpy (dpyinfo);
13194 #endif
13196 xfree (dpyinfo->font_table);
13197 xfree (dpyinfo->x_id_name);
13198 xfree (dpyinfo);
13201 /* Set up use of X before we make the first connection. */
13203 static struct redisplay_interface x_redisplay_interface =
13205 x_produce_glyphs,
13206 x_write_glyphs,
13207 x_insert_glyphs,
13208 x_clear_end_of_line,
13209 x_scroll_run,
13210 x_after_update_window_line,
13211 x_update_window_begin,
13212 x_update_window_end,
13213 XTcursor_to,
13214 x_flush,
13215 x_get_glyph_overhangs,
13216 x_fix_overlapping_area
13219 void
13220 x_initialize ()
13222 rif = &x_redisplay_interface;
13224 clear_frame_hook = x_clear_frame;
13225 ins_del_lines_hook = x_ins_del_lines;
13226 change_line_highlight_hook = x_change_line_highlight;
13227 delete_glyphs_hook = x_delete_glyphs;
13228 ring_bell_hook = XTring_bell;
13229 reset_terminal_modes_hook = XTreset_terminal_modes;
13230 set_terminal_modes_hook = XTset_terminal_modes;
13231 update_begin_hook = x_update_begin;
13232 update_end_hook = x_update_end;
13233 set_terminal_window_hook = XTset_terminal_window;
13234 read_socket_hook = XTread_socket;
13235 frame_up_to_date_hook = XTframe_up_to_date;
13236 reassert_line_highlight_hook = XTreassert_line_highlight;
13237 mouse_position_hook = XTmouse_position;
13238 frame_rehighlight_hook = XTframe_rehighlight;
13239 frame_raise_lower_hook = XTframe_raise_lower;
13240 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13241 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13242 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13243 judge_scroll_bars_hook = XTjudge_scroll_bars;
13244 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13246 scroll_region_ok = 1; /* we'll scroll partial frames */
13247 char_ins_del_ok = 0; /* just as fast to write the line */
13248 line_ins_del_ok = 1; /* we'll just blt 'em */
13249 fast_clear_end_of_line = 1; /* X does this well */
13250 memory_below_frame = 0; /* we don't remember what scrolls
13251 off the bottom */
13252 baud_rate = 19200;
13254 x_noop_count = 0;
13255 last_tool_bar_item = -1;
13256 any_help_event_p = 0;
13258 /* Try to use interrupt input; if we can't, then start polling. */
13259 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13261 #ifdef USE_X_TOOLKIT
13262 XtToolkitInitialize ();
13263 Xt_app_con = XtCreateApplicationContext ();
13264 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13266 /* Install an asynchronous timer that processes Xt timeout events
13267 every 0.1s. This is necessary because some widget sets use
13268 timeouts internally, for example the LessTif menu bar, or the
13269 Xaw3d scroll bar. When Xt timouts aren't processed, these
13270 widgets don't behave normally. */
13272 EMACS_TIME interval;
13273 EMACS_SET_SECS_USECS (interval, 0, 100000);
13274 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13276 #endif
13278 #if USE_TOOLKIT_SCROLL_BARS
13279 xaw3d_arrow_scroll = False;
13280 xaw3d_pick_top = True;
13281 #endif
13283 /* Note that there is no real way portable across R3/R4 to get the
13284 original error handler. */
13285 XSetErrorHandler (x_error_handler);
13286 XSetIOErrorHandler (x_io_error_quitter);
13288 /* Disable Window Change signals; they are handled by X events. */
13289 #ifdef SIGWINCH
13290 signal (SIGWINCH, SIG_DFL);
13291 #endif /* ! defined (SIGWINCH) */
13293 signal (SIGPIPE, x_connection_signal);
13297 void
13298 syms_of_xterm ()
13300 staticpro (&x_error_message_string);
13301 x_error_message_string = Qnil;
13303 staticpro (&x_display_name_list);
13304 x_display_name_list = Qnil;
13306 staticpro (&last_mouse_scroll_bar);
13307 last_mouse_scroll_bar = Qnil;
13309 staticpro (&Qvendor_specific_keysyms);
13310 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13312 staticpro (&last_mouse_press_frame);
13313 last_mouse_press_frame = Qnil;
13315 staticpro (&help_echo);
13316 help_echo = Qnil;
13317 staticpro (&previous_help_echo);
13318 previous_help_echo = Qnil;
13320 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13321 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13322 For example, if a block cursor is over a tab, it will be drawn as\n\
13323 wide as that tab on the display.");
13324 x_stretch_cursor_p = 0;
13326 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13327 "If not nil, Emacs uses toolkit scroll bars.");
13328 #if USE_TOOLKIT_SCROLL_BARS
13329 x_toolkit_scroll_bars_p = 1;
13330 #else
13331 x_toolkit_scroll_bars_p = 0;
13332 #endif
13334 staticpro (&last_mouse_motion_frame);
13335 last_mouse_motion_frame = Qnil;
13338 #endif /* not HAVE_X_WINDOWS */