(xmalloc, xrealloc, xfree): Define using POINTER_TYPE.
[emacs.git] / src / xterm.c
blob7c992eadfde9146a49bc58430fa277affe982ac0
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
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 note_mouse_highlight P_ ((struct frame *, int, int));
416 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
417 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
418 static void show_mouse_face P_ ((struct x_display_info *,
419 enum draw_glyphs_face));
420 static int x_io_error_quitter P_ ((Display *));
421 int x_catch_errors P_ ((Display *));
422 void x_uncatch_errors P_ ((Display *, int));
423 void x_lower_frame P_ ((struct frame *));
424 void x_scroll_bar_clear P_ ((struct frame *));
425 int x_had_errors_p P_ ((Display *));
426 void x_wm_set_size_hint P_ ((struct frame *, long, int));
427 void x_raise_frame P_ ((struct frame *));
428 void x_set_window_size P_ ((struct frame *, int, int, int));
429 void x_wm_set_window_state P_ ((struct frame *, int));
430 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
431 void x_initialize P_ ((void));
432 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
433 static int x_compute_min_glyph_bounds P_ ((struct frame *));
434 static void x_draw_phys_cursor_glyph P_ ((struct window *,
435 struct glyph_row *,
436 enum draw_glyphs_face));
437 static void x_update_end P_ ((struct frame *));
438 static void XTframe_up_to_date P_ ((struct frame *));
439 static void XTreassert_line_highlight P_ ((int, int));
440 static void x_change_line_highlight P_ ((int, int, int, int));
441 static void XTset_terminal_modes P_ ((void));
442 static void XTreset_terminal_modes P_ ((void));
443 static void XTcursor_to P_ ((int, int, int, int));
444 static void x_write_glyphs P_ ((struct glyph *, int));
445 static void x_clear_end_of_line P_ ((int));
446 static void x_clear_frame P_ ((void));
447 static void x_clear_cursor P_ ((struct window *));
448 static void frame_highlight P_ ((struct frame *));
449 static void frame_unhighlight P_ ((struct frame *));
450 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
451 static void XTframe_rehighlight P_ ((struct frame *));
452 static void x_frame_rehighlight P_ ((struct x_display_info *));
453 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
454 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
455 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
456 XRectangle *));
457 static void expose_frame P_ ((struct frame *, int, int, int, int));
458 static void expose_window_tree P_ ((struct window *, XRectangle *));
459 static void expose_window P_ ((struct window *, XRectangle *));
460 static void expose_area P_ ((struct window *, struct glyph_row *,
461 XRectangle *, enum glyph_row_area));
462 static void expose_line P_ ((struct window *, struct glyph_row *,
463 XRectangle *));
464 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
465 static void x_update_window_cursor P_ ((struct window *, int));
466 static void x_erase_phys_cursor P_ ((struct window *));
467 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
468 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
469 enum bitmap_type));
471 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
472 GC, int));
473 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
474 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
475 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
476 static void x_flush P_ ((struct frame *f));
479 /* Flush display of frame F, or of all frames if F is null. */
481 static void
482 x_flush (f)
483 struct frame *f;
485 BLOCK_INPUT;
486 if (f == NULL)
488 Lisp_Object rest, frame;
489 FOR_EACH_FRAME (rest, frame)
490 x_flush (XFRAME (frame));
492 else if (FRAME_X_P (f))
493 XFlush (FRAME_X_DISPLAY (f));
494 UNBLOCK_INPUT;
498 /* Remove calls to XFlush by defining XFlush to an empty replacement.
499 Calls to XFlush should be unnecessary because the X output buffer
500 is flushed automatically as needed by calls to XPending,
501 XNextEvent, or XWindowEvent according to the XFlush man page.
502 XTread_socket calls XPending. Removing XFlush improves
503 performance. */
505 #define XFlush(DISPLAY) (void) 0
508 /***********************************************************************
509 Debugging
510 ***********************************************************************/
512 #if 0
514 /* This is a function useful for recording debugging information about
515 the sequence of occurrences in this file. */
517 struct record
519 char *locus;
520 int type;
523 struct record event_record[100];
525 int event_record_index;
527 record_event (locus, type)
528 char *locus;
529 int type;
531 if (event_record_index == sizeof (event_record) / sizeof (struct record))
532 event_record_index = 0;
534 event_record[event_record_index].locus = locus;
535 event_record[event_record_index].type = type;
536 event_record_index++;
539 #endif /* 0 */
543 /* Return the struct x_display_info corresponding to DPY. */
545 struct x_display_info *
546 x_display_info_for_display (dpy)
547 Display *dpy;
549 struct x_display_info *dpyinfo;
551 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
552 if (dpyinfo->display == dpy)
553 return dpyinfo;
555 return 0;
560 /***********************************************************************
561 Starting and ending an update
562 ***********************************************************************/
564 /* Start an update of frame F. This function is installed as a hook
565 for update_begin, i.e. it is called when update_begin is called.
566 This function is called prior to calls to x_update_window_begin for
567 each window being updated. Currently, there is nothing to do here
568 because all interesting stuff is done on a window basis. */
570 static void
571 x_update_begin (f)
572 struct frame *f;
574 /* Nothing to do. */
578 /* Start update of window W. Set the global variable updated_window
579 to the window being updated and set output_cursor to the cursor
580 position of W. */
582 static void
583 x_update_window_begin (w)
584 struct window *w;
586 struct frame *f = XFRAME (WINDOW_FRAME (w));
587 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
589 updated_window = w;
590 set_output_cursor (&w->cursor);
592 BLOCK_INPUT;
594 if (f == display_info->mouse_face_mouse_frame)
596 /* Don't do highlighting for mouse motion during the update. */
597 display_info->mouse_face_defer = 1;
599 /* If F needs to be redrawn, simply forget about any prior mouse
600 highlighting. */
601 if (FRAME_GARBAGED_P (f))
602 display_info->mouse_face_window = Qnil;
604 /* Can we tell that this update does not affect the window
605 where the mouse highlight is? If so, no need to turn off.
606 Likewise, don't do anything if the frame is garbaged;
607 in that case, the frame's current matrix that we would use
608 is all wrong, and we will redisplay that line anyway. */
609 if (!NILP (display_info->mouse_face_window)
610 && w == XWINDOW (display_info->mouse_face_window))
612 int i;
614 for (i = 0; i < w->desired_matrix->nrows; ++i)
615 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
616 break;
618 if (i < w->desired_matrix->nrows)
619 clear_mouse_face (display_info);
623 UNBLOCK_INPUT;
627 /* Draw a vertical window border to the right of window W if W doesn't
628 have vertical scroll bars. */
630 static void
631 x_draw_vertical_border (w)
632 struct window *w;
634 struct frame *f = XFRAME (WINDOW_FRAME (w));
636 /* Redraw borders between horizontally adjacent windows. Don't
637 do it for frames with vertical scroll bars because either the
638 right scroll bar of a window, or the left scroll bar of its
639 neighbor will suffice as a border. */
640 if (!WINDOW_RIGHTMOST_P (w)
641 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
643 int x0, x1, y0, y1;
645 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
646 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
647 y1 -= 1;
649 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
650 f->output_data.x->normal_gc, x1, y0, x1, y1);
655 /* End update of window W (which is equal to updated_window). Draw
656 vertical borders between horizontally adjacent windows, and display
657 W's cursor if CURSOR_ON_P is non-zero. W may be a menu bar
658 pseudo-window in case we don't have X toolkit support. Such
659 windows don't have a cursor, so don't display it here. */
661 static void
662 x_update_window_end (w, cursor_on_p)
663 struct window *w;
664 int cursor_on_p;
666 if (!w->pseudo_window_p)
668 BLOCK_INPUT;
669 if (cursor_on_p)
670 x_display_and_set_cursor (w, 1, output_cursor.hpos,
671 output_cursor.vpos,
672 output_cursor.x, output_cursor.y);
673 x_draw_vertical_border (w);
674 UNBLOCK_INPUT;
677 updated_window = NULL;
681 /* End update of frame F. This function is installed as a hook in
682 update_end. */
684 static void
685 x_update_end (f)
686 struct frame *f;
688 /* Mouse highlight may be displayed again. */
689 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
691 BLOCK_INPUT;
692 XFlush (FRAME_X_DISPLAY (f));
693 UNBLOCK_INPUT;
697 /* This function is called from various places in xdisp.c whenever a
698 complete update has been performed. The global variable
699 updated_window is not available here. */
701 static void
702 XTframe_up_to_date (f)
703 struct frame *f;
705 if (FRAME_X_P (f))
707 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
708 if (dpyinfo->mouse_face_deferred_gc
709 || f == dpyinfo->mouse_face_mouse_frame)
711 BLOCK_INPUT;
712 if (dpyinfo->mouse_face_mouse_frame)
713 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
714 dpyinfo->mouse_face_mouse_x,
715 dpyinfo->mouse_face_mouse_y);
716 dpyinfo->mouse_face_deferred_gc = 0;
717 UNBLOCK_INPUT;
723 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
724 arrow bitmaps, or clear the areas where they would be displayed
725 before DESIRED_ROW is made current. The window being updated is
726 found in updated_window. This function It is called from
727 update_window_line only if it is known that there are differences
728 between bitmaps to be drawn between current row and DESIRED_ROW. */
730 static void
731 x_after_update_window_line (desired_row)
732 struct glyph_row *desired_row;
734 struct window *w = updated_window;
736 xassert (w);
738 if (!desired_row->mode_line_p && !w->pseudo_window_p)
740 BLOCK_INPUT;
741 x_draw_row_bitmaps (w, desired_row);
743 /* When a window has disappeared, make sure that no rest of
744 full-width rows stays visible in the internal border. */
745 if (windows_or_buffers_changed)
747 struct frame *f = XFRAME (w->frame);
748 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
749 int height = desired_row->visible_height;
750 int x = (window_box_right (w, -1)
751 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
752 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
754 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
755 x, y, width, height, False);
758 UNBLOCK_INPUT;
763 /* Draw the bitmap WHICH in one of the areas to the left or right of
764 window W. ROW is the glyph row for which to display the bitmap; it
765 determines the vertical position at which the bitmap has to be
766 drawn. */
768 static void
769 x_draw_bitmap (w, row, which)
770 struct window *w;
771 struct glyph_row *row;
772 enum bitmap_type which;
774 struct frame *f = XFRAME (WINDOW_FRAME (w));
775 Display *display = FRAME_X_DISPLAY (f);
776 Window window = FRAME_X_WINDOW (f);
777 int x, y, wd, h, dy;
778 unsigned char *bits;
779 Pixmap pixmap;
780 GC gc = f->output_data.x->normal_gc;
781 struct face *face;
782 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
784 /* Must clip because of partially visible lines. */
785 x_clip_to_row (w, row, gc, 1);
787 switch (which)
789 case LEFT_TRUNCATION_BITMAP:
790 wd = left_width;
791 h = left_height;
792 bits = left_bits;
793 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
794 - wd
795 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
796 break;
798 case OVERLAY_ARROW_BITMAP:
799 wd = left_width;
800 h = left_height;
801 bits = ov_bits;
802 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
803 - wd
804 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
805 break;
807 case RIGHT_TRUNCATION_BITMAP:
808 wd = right_width;
809 h = right_height;
810 bits = right_bits;
811 x = window_box_right (w, -1);
812 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
813 break;
815 case CONTINUED_LINE_BITMAP:
816 wd = right_width;
817 h = right_height;
818 bits = continued_bits;
819 x = window_box_right (w, -1);
820 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
821 break;
823 case CONTINUATION_LINE_BITMAP:
824 wd = continuation_width;
825 h = continuation_height;
826 bits = continuation_bits;
827 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
828 - wd
829 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
830 break;
832 case ZV_LINE_BITMAP:
833 wd = zv_width;
834 h = zv_height;
835 bits = zv_bits;
836 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
837 - wd
838 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
839 break;
841 default:
842 abort ();
845 /* Convert to frame coordinates. Set dy to the offset in the row to
846 start drawing the bitmap. */
847 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
848 dy = (row->height - h) / 2;
850 /* Draw the bitmap. I believe these small pixmaps can be cached
851 by the server. */
852 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
853 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
854 face->foreground,
855 face->background, depth);
856 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
857 XFreePixmap (display, pixmap);
858 XSetClipMask (display, gc, None);
862 /* Draw flags bitmaps for glyph row ROW on window W. Call this
863 function with input blocked. */
865 static void
866 x_draw_row_bitmaps (w, row)
867 struct window *w;
868 struct glyph_row *row;
870 struct frame *f = XFRAME (w->frame);
871 enum bitmap_type bitmap;
872 struct face *face;
873 int header_line_height = -1;
875 xassert (interrupt_input_blocked);
877 /* If row is completely invisible, because of vscrolling, we
878 don't have to draw anything. */
879 if (row->visible_height <= 0)
880 return;
882 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
883 PREPARE_FACE_FOR_DISPLAY (f, face);
885 /* Decide which bitmap to draw at the left side. */
886 if (row->overlay_arrow_p)
887 bitmap = OVERLAY_ARROW_BITMAP;
888 else if (row->truncated_on_left_p)
889 bitmap = LEFT_TRUNCATION_BITMAP;
890 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
891 bitmap = CONTINUATION_LINE_BITMAP;
892 else if (row->indicate_empty_line_p)
893 bitmap = ZV_LINE_BITMAP;
894 else
895 bitmap = NO_BITMAP;
897 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
898 the flags area. */
899 if (bitmap == NO_BITMAP
900 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
901 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
903 /* If W has a vertical border to its left, don't draw over it. */
904 int border = ((XFASTINT (w->left) > 0
905 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
906 ? 1 : 0);
907 int left = window_box_left (w, -1);
909 if (header_line_height < 0)
910 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
912 /* In case the same realized face is used for bitmap areas and
913 for something displayed in the text (e.g. face `region' on
914 mono-displays, the fill style may have been changed to
915 FillSolid in x_draw_glyph_string_background. */
916 if (face->stipple)
917 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
918 else
919 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
921 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
922 face->gc,
923 (left
924 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
925 + border),
926 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
927 row->y)),
928 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
929 row->visible_height);
930 if (!face->stipple)
931 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
934 /* Draw the left bitmap. */
935 if (bitmap != NO_BITMAP)
936 x_draw_bitmap (w, row, bitmap);
938 /* Decide which bitmap to draw at the right side. */
939 if (row->truncated_on_right_p)
940 bitmap = RIGHT_TRUNCATION_BITMAP;
941 else if (row->continued_p)
942 bitmap = CONTINUED_LINE_BITMAP;
943 else
944 bitmap = NO_BITMAP;
946 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
947 the flags area. */
948 if (bitmap == NO_BITMAP
949 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
950 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
952 int right = window_box_right (w, -1);
954 if (header_line_height < 0)
955 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
957 /* In case the same realized face is used for bitmap areas and
958 for something displayed in the text (e.g. face `region' on
959 mono-displays, the fill style may have been changed to
960 FillSolid in x_draw_glyph_string_background. */
961 if (face->stipple)
962 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
963 else
964 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
965 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
966 face->gc,
967 right,
968 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
969 row->y)),
970 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
971 row->visible_height);
972 if (!face->stipple)
973 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
976 /* Draw the right bitmap. */
977 if (bitmap != NO_BITMAP)
978 x_draw_bitmap (w, row, bitmap);
982 /***********************************************************************
983 Line Highlighting
984 ***********************************************************************/
986 /* External interface to control of standout mode. Not used for X
987 frames. Aborts when called. */
989 static void
990 XTreassert_line_highlight (new, vpos)
991 int new, vpos;
993 abort ();
997 /* Call this when about to modify line at position VPOS and change
998 whether it is highlighted. Not used for X frames. Aborts when
999 called. */
1001 static void
1002 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1003 int new_highlight, vpos, y, first_unused_hpos;
1005 abort ();
1009 /* This is called when starting Emacs and when restarting after
1010 suspend. When starting Emacs, no X window is mapped. And nothing
1011 must be done to Emacs's own window if it is suspended (though that
1012 rarely happens). */
1014 static void
1015 XTset_terminal_modes ()
1019 /* This is called when exiting or suspending Emacs. Exiting will make
1020 the X-windows go away, and suspending requires no action. */
1022 static void
1023 XTreset_terminal_modes ()
1029 /***********************************************************************
1030 Output Cursor
1031 ***********************************************************************/
1033 /* Set the global variable output_cursor to CURSOR. All cursor
1034 positions are relative to updated_window. */
1036 static void
1037 set_output_cursor (cursor)
1038 struct cursor_pos *cursor;
1040 output_cursor.hpos = cursor->hpos;
1041 output_cursor.vpos = cursor->vpos;
1042 output_cursor.x = cursor->x;
1043 output_cursor.y = cursor->y;
1047 /* Set a nominal cursor position.
1049 HPOS and VPOS are column/row positions in a window glyph matrix. X
1050 and Y are window text area relative pixel positions.
1052 If this is done during an update, updated_window will contain the
1053 window that is being updated and the position is the future output
1054 cursor position for that window. If updated_window is null, use
1055 selected_window and display the cursor at the given position. */
1057 static void
1058 XTcursor_to (vpos, hpos, y, x)
1059 int vpos, hpos, y, x;
1061 struct window *w;
1063 /* If updated_window is not set, work on selected_window. */
1064 if (updated_window)
1065 w = updated_window;
1066 else
1067 w = XWINDOW (selected_window);
1069 /* Set the output cursor. */
1070 output_cursor.hpos = hpos;
1071 output_cursor.vpos = vpos;
1072 output_cursor.x = x;
1073 output_cursor.y = y;
1075 /* If not called as part of an update, really display the cursor.
1076 This will also set the cursor position of W. */
1077 if (updated_window == NULL)
1079 BLOCK_INPUT;
1080 x_display_cursor (w, 1, hpos, vpos, x, y);
1081 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1082 UNBLOCK_INPUT;
1088 /***********************************************************************
1089 Display Iterator
1090 ***********************************************************************/
1092 /* Function prototypes of this page. */
1094 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1095 struct glyph *,
1096 XChar2b *,
1097 int *));
1098 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1099 int, XChar2b *, int));
1100 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1101 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1102 static void x_append_glyph P_ ((struct it *));
1103 static void x_append_composite_glyph P_ ((struct it *));
1104 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1105 int, int, double));
1106 static void x_produce_glyphs P_ ((struct it *));
1107 static void x_produce_image_glyph P_ ((struct it *it));
1110 /* Return a pointer to per-char metric information in FONT of a
1111 character pointed by B which is a pointer to an XChar2b. */
1113 #define PER_CHAR_METRIC(font, b) \
1114 ((font)->per_char \
1115 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1116 + (((font)->min_byte1 || (font)->max_byte1) \
1117 ? (((b)->byte1 - (font)->min_byte1) \
1118 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1119 : 0)) \
1120 : &((font)->max_bounds))
1123 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1124 is not contained in the font. */
1126 static INLINE XCharStruct *
1127 x_per_char_metric (font, char2b)
1128 XFontStruct *font;
1129 XChar2b *char2b;
1131 /* The result metric information. */
1132 XCharStruct *pcm = NULL;
1134 xassert (font && char2b);
1136 if (font->per_char != NULL)
1138 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1140 /* min_char_or_byte2 specifies the linear character index
1141 corresponding to the first element of the per_char array,
1142 max_char_or_byte2 is the index of the last character. A
1143 character with non-zero CHAR2B->byte1 is not in the font.
1144 A character with byte2 less than min_char_or_byte2 or
1145 greater max_char_or_byte2 is not in the font. */
1146 if (char2b->byte1 == 0
1147 && char2b->byte2 >= font->min_char_or_byte2
1148 && char2b->byte2 <= font->max_char_or_byte2)
1149 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1151 else
1153 /* If either min_byte1 or max_byte1 are nonzero, both
1154 min_char_or_byte2 and max_char_or_byte2 are less than
1155 256, and the 2-byte character index values corresponding
1156 to the per_char array element N (counting from 0) are:
1158 byte1 = N/D + min_byte1
1159 byte2 = N\D + min_char_or_byte2
1161 where:
1163 D = max_char_or_byte2 - min_char_or_byte2 + 1
1164 / = integer division
1165 \ = integer modulus */
1166 if (char2b->byte1 >= font->min_byte1
1167 && char2b->byte1 <= font->max_byte1
1168 && char2b->byte2 >= font->min_char_or_byte2
1169 && char2b->byte2 <= font->max_char_or_byte2)
1171 pcm = (font->per_char
1172 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1173 * (char2b->byte1 - font->min_byte1))
1174 + (char2b->byte2 - font->min_char_or_byte2));
1178 else
1180 /* If the per_char pointer is null, all glyphs between the first
1181 and last character indexes inclusive have the same
1182 information, as given by both min_bounds and max_bounds. */
1183 if (char2b->byte2 >= font->min_char_or_byte2
1184 && char2b->byte2 <= font->max_char_or_byte2)
1185 pcm = &font->max_bounds;
1188 return ((pcm == NULL
1189 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1190 ? NULL : pcm);
1194 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1195 the two-byte form of C. Encoding is returned in *CHAR2B. */
1197 static INLINE void
1198 x_encode_char (c, char2b, font_info)
1199 int c;
1200 XChar2b *char2b;
1201 struct font_info *font_info;
1203 int charset = CHAR_CHARSET (c);
1204 XFontStruct *font = font_info->font;
1206 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1207 This may be either a program in a special encoder language or a
1208 fixed encoding. */
1209 if (font_info->font_encoder)
1211 /* It's a program. */
1212 struct ccl_program *ccl = font_info->font_encoder;
1214 if (CHARSET_DIMENSION (charset) == 1)
1216 ccl->reg[0] = charset;
1217 ccl->reg[1] = char2b->byte2;
1219 else
1221 ccl->reg[0] = charset;
1222 ccl->reg[1] = char2b->byte1;
1223 ccl->reg[2] = char2b->byte2;
1226 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1228 /* We assume that MSBs are appropriately set/reset by CCL
1229 program. */
1230 if (font->max_byte1 == 0) /* 1-byte font */
1231 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1232 else
1233 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1235 else if (font_info->encoding[charset])
1237 /* Fixed encoding scheme. See fontset.h for the meaning of the
1238 encoding numbers. */
1239 int enc = font_info->encoding[charset];
1241 if ((enc == 1 || enc == 2)
1242 && CHARSET_DIMENSION (charset) == 2)
1243 char2b->byte1 |= 0x80;
1245 if (enc == 1 || enc == 3)
1246 char2b->byte2 |= 0x80;
1251 /* Get face and two-byte form of character C in face FACE_ID on frame
1252 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1253 means we want to display multibyte text. Value is a pointer to a
1254 realized face that is ready for display. */
1256 static INLINE struct face *
1257 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1258 struct frame *f;
1259 int c, face_id;
1260 XChar2b *char2b;
1261 int multibyte_p;
1263 struct face *face = FACE_FROM_ID (f, face_id);
1265 if (!multibyte_p)
1267 /* Unibyte case. We don't have to encode, but we have to make
1268 sure to use a face suitable for unibyte. */
1269 char2b->byte1 = 0;
1270 char2b->byte2 = c;
1271 face_id = FACE_FOR_CHAR (f, face, c);
1272 face = FACE_FROM_ID (f, face_id);
1274 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1276 /* Case of ASCII in a face known to fit ASCII. */
1277 char2b->byte1 = 0;
1278 char2b->byte2 = c;
1280 else
1282 int c1, c2, charset;
1284 /* Split characters into bytes. If c2 is -1 afterwards, C is
1285 really a one-byte character so that byte1 is zero. */
1286 SPLIT_CHAR (c, charset, c1, c2);
1287 if (c2 > 0)
1288 char2b->byte1 = c1, char2b->byte2 = c2;
1289 else
1290 char2b->byte1 = 0, char2b->byte2 = c1;
1292 /* Maybe encode the character in *CHAR2B. */
1293 if (face->font != NULL)
1295 struct font_info *font_info
1296 = FONT_INFO_FROM_ID (f, face->font_info_id);
1297 if (font_info)
1298 x_encode_char (c, char2b, font_info);
1302 /* Make sure X resources of the face are allocated. */
1303 xassert (face != NULL);
1304 PREPARE_FACE_FOR_DISPLAY (f, face);
1306 return face;
1310 /* Get face and two-byte form of character glyph GLYPH on frame F.
1311 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1312 a pointer to a realized face that is ready for display. */
1314 static INLINE struct face *
1315 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1316 struct frame *f;
1317 struct glyph *glyph;
1318 XChar2b *char2b;
1319 int *two_byte_p;
1321 struct face *face;
1323 xassert (glyph->type == CHAR_GLYPH);
1324 face = FACE_FROM_ID (f, glyph->face_id);
1326 if (two_byte_p)
1327 *two_byte_p = 0;
1329 if (!glyph->multibyte_p)
1331 /* Unibyte case. We don't have to encode, but we have to make
1332 sure to use a face suitable for unibyte. */
1333 char2b->byte1 = 0;
1334 char2b->byte2 = glyph->u.ch;
1336 else if (glyph->u.ch < 128
1337 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1339 /* Case of ASCII in a face known to fit ASCII. */
1340 char2b->byte1 = 0;
1341 char2b->byte2 = glyph->u.ch;
1343 else
1345 int c1, c2, charset;
1347 /* Split characters into bytes. If c2 is -1 afterwards, C is
1348 really a one-byte character so that byte1 is zero. */
1349 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1350 if (c2 > 0)
1351 char2b->byte1 = c1, char2b->byte2 = c2;
1352 else
1353 char2b->byte1 = 0, char2b->byte2 = c1;
1355 /* Maybe encode the character in *CHAR2B. */
1356 if (charset != CHARSET_ASCII)
1358 struct font_info *font_info
1359 = FONT_INFO_FROM_ID (f, face->font_info_id);
1360 if (font_info)
1362 x_encode_char (glyph->u.ch, char2b, font_info);
1363 if (two_byte_p)
1364 *two_byte_p
1365 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1370 /* Make sure X resources of the face are allocated. */
1371 xassert (face != NULL);
1372 PREPARE_FACE_FOR_DISPLAY (f, face);
1373 return face;
1377 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1378 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1380 static INLINE void
1381 x_append_glyph (it)
1382 struct it *it;
1384 struct glyph *glyph;
1385 enum glyph_row_area area = it->area;
1387 xassert (it->glyph_row);
1388 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1390 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1391 if (glyph < it->glyph_row->glyphs[area + 1])
1393 /* Play it safe. If sub-structures of the glyph are not all the
1394 same size, it otherwise be that some bits stay set. This
1395 would prevent a comparison with GLYPH_EQUAL_P. */
1396 glyph->u.val = 0;
1398 glyph->type = CHAR_GLYPH;
1399 glyph->pixel_width = it->pixel_width;
1400 glyph->u.ch = it->char_to_display;
1401 glyph->face_id = it->face_id;
1402 glyph->charpos = CHARPOS (it->position);
1403 glyph->object = it->object;
1404 glyph->left_box_line_p = it->start_of_box_run_p;
1405 glyph->right_box_line_p = it->end_of_box_run_p;
1406 glyph->voffset = it->voffset;
1407 glyph->multibyte_p = it->multibyte_p;
1408 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1409 || it->phys_descent > it->descent);
1410 glyph->glyph_not_available_p = it->glyph_not_available_p;
1411 ++it->glyph_row->used[area];
1415 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1416 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1418 static INLINE void
1419 x_append_composite_glyph (it)
1420 struct it *it;
1422 struct glyph *glyph;
1423 enum glyph_row_area area = it->area;
1425 xassert (it->glyph_row);
1427 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1428 if (glyph < it->glyph_row->glyphs[area + 1])
1430 /* Play it safe. If sub-structures of the glyph are not all the
1431 same size, it otherwise be that some bits stay set. This
1432 would prevent a comparison with GLYPH_EQUAL_P. */
1433 glyph->u.val = 0;
1435 glyph->type = COMPOSITE_GLYPH;
1436 glyph->pixel_width = it->pixel_width;
1437 glyph->u.cmp_id = it->cmp_id;
1438 glyph->face_id = it->face_id;
1439 glyph->charpos = CHARPOS (it->position);
1440 glyph->object = it->object;
1441 glyph->left_box_line_p = it->start_of_box_run_p;
1442 glyph->right_box_line_p = it->end_of_box_run_p;
1443 glyph->voffset = it->voffset;
1444 glyph->multibyte_p = it->multibyte_p;
1445 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1446 || it->phys_descent > it->descent);
1447 ++it->glyph_row->used[area];
1452 /* Change IT->ascent and IT->height according to the setting of
1453 IT->voffset. */
1455 static INLINE void
1456 take_vertical_position_into_account (it)
1457 struct it *it;
1459 if (it->voffset)
1461 if (it->voffset < 0)
1462 /* Increase the ascent so that we can display the text higher
1463 in the line. */
1464 it->ascent += abs (it->voffset);
1465 else
1466 /* Increase the descent so that we can display the text lower
1467 in the line. */
1468 it->descent += it->voffset;
1473 /* Produce glyphs/get display metrics for the image IT is loaded with.
1474 See the description of struct display_iterator in dispextern.h for
1475 an overview of struct display_iterator. */
1477 static void
1478 x_produce_image_glyph (it)
1479 struct it *it;
1481 struct image *img;
1482 struct face *face;
1484 xassert (it->what == IT_IMAGE);
1486 face = FACE_FROM_ID (it->f, it->face_id);
1487 img = IMAGE_FROM_ID (it->f, it->image_id);
1488 xassert (img);
1490 /* Make sure X resources of the face and image are loaded. */
1491 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1492 prepare_image_for_display (it->f, img);
1494 it->ascent = it->phys_ascent = image_ascent (img, face);
1495 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1496 it->pixel_width = img->width + 2 * img->margin;
1498 it->nglyphs = 1;
1500 if (face->box != FACE_NO_BOX)
1502 it->ascent += face->box_line_width;
1503 it->descent += face->box_line_width;
1505 if (it->start_of_box_run_p)
1506 it->pixel_width += face->box_line_width;
1507 if (it->end_of_box_run_p)
1508 it->pixel_width += face->box_line_width;
1511 take_vertical_position_into_account (it);
1513 if (it->glyph_row)
1515 struct glyph *glyph;
1516 enum glyph_row_area area = it->area;
1518 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1519 if (glyph < it->glyph_row->glyphs[area + 1])
1521 glyph->type = IMAGE_GLYPH;
1522 glyph->u.img_id = img->id;
1523 glyph->face_id = it->face_id;
1524 glyph->pixel_width = it->pixel_width;
1525 glyph->charpos = CHARPOS (it->position);
1526 glyph->object = it->object;
1527 glyph->left_box_line_p = it->start_of_box_run_p;
1528 glyph->right_box_line_p = it->end_of_box_run_p;
1529 glyph->voffset = it->voffset;
1530 glyph->multibyte_p = it->multibyte_p;
1531 ++it->glyph_row->used[area];
1537 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1538 of the glyph, WIDTH and HEIGHT are the width and height of the
1539 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1540 ascent of the glyph (0 <= ASCENT <= 1). */
1542 static void
1543 x_append_stretch_glyph (it, object, width, height, ascent)
1544 struct it *it;
1545 Lisp_Object object;
1546 int width, height;
1547 double ascent;
1549 struct glyph *glyph;
1550 enum glyph_row_area area = it->area;
1552 xassert (ascent >= 0 && ascent <= 1);
1554 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1555 if (glyph < it->glyph_row->glyphs[area + 1])
1557 glyph->type = STRETCH_GLYPH;
1558 glyph->u.stretch.ascent = height * ascent;
1559 glyph->u.stretch.height = height;
1560 glyph->face_id = it->face_id;
1561 glyph->pixel_width = width;
1562 glyph->charpos = CHARPOS (it->position);
1563 glyph->object = object;
1564 glyph->left_box_line_p = it->start_of_box_run_p;
1565 glyph->right_box_line_p = it->end_of_box_run_p;
1566 glyph->voffset = it->voffset;
1567 glyph->multibyte_p = it->multibyte_p;
1568 ++it->glyph_row->used[area];
1573 /* Produce a stretch glyph for iterator IT. IT->object is the value
1574 of the glyph property displayed. The value must be a list
1575 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1576 being recognized:
1578 1. `:width WIDTH' specifies that the space should be WIDTH *
1579 canonical char width wide. WIDTH may be an integer or floating
1580 point number.
1582 2. `:relative-width FACTOR' specifies that the width of the stretch
1583 should be computed from the width of the first character having the
1584 `glyph' property, and should be FACTOR times that width.
1586 3. `:align-to HPOS' specifies that the space should be wide enough
1587 to reach HPOS, a value in canonical character units.
1589 Exactly one of the above pairs must be present.
1591 4. `:height HEIGHT' specifies that the height of the stretch produced
1592 should be HEIGHT, measured in canonical character units.
1594 5. `:relative-height FACTOR' specifies that the height of the the
1595 stretch should be FACTOR times the height of the characters having
1596 the glyph property.
1598 Either none or exactly one of 4 or 5 must be present.
1600 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1601 of the stretch should be used for the ascent of the stretch.
1602 ASCENT must be in the range 0 <= ASCENT <= 100. */
1604 #define NUMVAL(X) \
1605 ((INTEGERP (X) || FLOATP (X)) \
1606 ? XFLOATINT (X) \
1607 : - 1)
1610 static void
1611 x_produce_stretch_glyph (it)
1612 struct it *it;
1614 /* (space :width WIDTH :height HEIGHT. */
1615 #if GLYPH_DEBUG
1616 extern Lisp_Object Qspace;
1617 #endif
1618 extern Lisp_Object QCwidth, QCheight, QCascent;
1619 extern Lisp_Object QCrelative_width, QCrelative_height;
1620 extern Lisp_Object QCalign_to;
1621 Lisp_Object prop, plist;
1622 double width = 0, height = 0, ascent = 0;
1623 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1624 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1626 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1628 /* List should start with `space'. */
1629 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1630 plist = XCDR (it->object);
1632 /* Compute the width of the stretch. */
1633 if (prop = Fplist_get (plist, QCwidth),
1634 NUMVAL (prop) > 0)
1635 /* Absolute width `:width WIDTH' specified and valid. */
1636 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1637 else if (prop = Fplist_get (plist, QCrelative_width),
1638 NUMVAL (prop) > 0)
1640 /* Relative width `:relative-width FACTOR' specified and valid.
1641 Compute the width of the characters having the `glyph'
1642 property. */
1643 struct it it2;
1644 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1646 it2 = *it;
1647 if (it->multibyte_p)
1649 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1650 - IT_BYTEPOS (*it));
1651 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1653 else
1654 it2.c = *p, it2.len = 1;
1656 it2.glyph_row = NULL;
1657 it2.what = IT_CHARACTER;
1658 x_produce_glyphs (&it2);
1659 width = NUMVAL (prop) * it2.pixel_width;
1661 else if (prop = Fplist_get (plist, QCalign_to),
1662 NUMVAL (prop) > 0)
1663 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1664 else
1665 /* Nothing specified -> width defaults to canonical char width. */
1666 width = CANON_X_UNIT (it->f);
1668 /* Compute height. */
1669 if (prop = Fplist_get (plist, QCheight),
1670 NUMVAL (prop) > 0)
1671 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1672 else if (prop = Fplist_get (plist, QCrelative_height),
1673 NUMVAL (prop) > 0)
1674 height = FONT_HEIGHT (font) * NUMVAL (prop);
1675 else
1676 height = FONT_HEIGHT (font);
1678 /* Compute percentage of height used for ascent. If
1679 `:ascent ASCENT' is present and valid, use that. Otherwise,
1680 derive the ascent from the font in use. */
1681 if (prop = Fplist_get (plist, QCascent),
1682 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1683 ascent = NUMVAL (prop) / 100.0;
1684 else
1685 ascent = (double) font->ascent / FONT_HEIGHT (font);
1687 if (width <= 0)
1688 width = 1;
1689 if (height <= 0)
1690 height = 1;
1692 if (it->glyph_row)
1694 Lisp_Object object = it->stack[it->sp - 1].string;
1695 if (!STRINGP (object))
1696 object = it->w->buffer;
1697 x_append_stretch_glyph (it, object, width, height, ascent);
1700 it->pixel_width = width;
1701 it->ascent = it->phys_ascent = height * ascent;
1702 it->descent = it->phys_descent = height - it->ascent;
1703 it->nglyphs = 1;
1705 if (face->box != FACE_NO_BOX)
1707 it->ascent += face->box_line_width;
1708 it->descent += face->box_line_width;
1710 if (it->start_of_box_run_p)
1711 it->pixel_width += face->box_line_width;
1712 if (it->end_of_box_run_p)
1713 it->pixel_width += face->box_line_width;
1716 take_vertical_position_into_account (it);
1719 /* Return proper value to be used as baseline offset of font that has
1720 ASCENT and DESCENT to draw characters by the font at the vertical
1721 center of the line of frame F.
1723 Here, out task is to find the value of BOFF in the following figure;
1725 -------------------------+-----------+-
1726 -+-+---------+-+ | |
1727 | | | | | |
1728 | | | | F_ASCENT F_HEIGHT
1729 | | | ASCENT | |
1730 HEIGHT | | | | |
1731 | | |-|-+------+-----------|------- baseline
1732 | | | | BOFF | |
1733 | |---------|-+-+ | |
1734 | | | DESCENT | |
1735 -+-+---------+-+ F_DESCENT |
1736 -------------------------+-----------+-
1738 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1739 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1740 DESCENT = FONT->descent
1741 HEIGHT = FONT_HEIGHT (FONT)
1742 F_DESCENT = (F->output_data.x->font->descent
1743 - F->output_data.x->baseline_offset)
1744 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1747 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1748 ((FONT)->descent \
1749 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1750 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1752 /* Produce glyphs/get display metrics for the display element IT is
1753 loaded with. See the description of struct display_iterator in
1754 dispextern.h for an overview of struct display_iterator. */
1756 static void
1757 x_produce_glyphs (it)
1758 struct it *it;
1760 it->glyph_not_available_p = 0;
1762 if (it->what == IT_CHARACTER)
1764 XChar2b char2b;
1765 XFontStruct *font;
1766 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1767 XCharStruct *pcm;
1768 int font_not_found_p;
1769 struct font_info *font_info;
1770 int boff; /* baseline offset */
1772 /* Maybe translate single-byte characters to multibyte, or the
1773 other way. */
1774 it->char_to_display = it->c;
1775 if (!ASCII_BYTE_P (it->c))
1777 if (unibyte_display_via_language_environment
1778 && SINGLE_BYTE_CHAR_P (it->c)
1779 && (it->c >= 0240
1780 || !NILP (Vnonascii_translation_table)))
1782 it->char_to_display = unibyte_char_to_multibyte (it->c);
1783 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1784 face = FACE_FROM_ID (it->f, it->face_id);
1786 else if (!SINGLE_BYTE_CHAR_P (it->c)
1787 && !it->multibyte_p)
1789 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1790 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1791 face = FACE_FROM_ID (it->f, it->face_id);
1795 /* Get font to use. Encode IT->char_to_display. */
1796 x_get_char_face_and_encoding (it->f, it->char_to_display,
1797 it->face_id, &char2b,
1798 it->multibyte_p);
1799 font = face->font;
1801 /* When no suitable font found, use the default font. */
1802 font_not_found_p = font == NULL;
1803 if (font_not_found_p)
1805 font = FRAME_FONT (it->f);
1806 boff = it->f->output_data.x->baseline_offset;
1807 font_info = NULL;
1809 else
1811 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1812 boff = font_info->baseline_offset;
1813 if (font_info->vertical_centering)
1814 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1817 if (it->char_to_display >= ' '
1818 && (!it->multibyte_p || it->char_to_display < 128))
1820 /* Either unibyte or ASCII. */
1821 int stretched_p;
1823 it->nglyphs = 1;
1825 pcm = x_per_char_metric (font, &char2b);
1826 it->ascent = font->ascent + boff;
1827 it->descent = font->descent - boff;
1829 if (pcm)
1831 it->phys_ascent = pcm->ascent + boff;
1832 it->phys_descent = pcm->descent - boff;
1833 it->pixel_width = pcm->width;
1835 else
1837 it->glyph_not_available_p = 1;
1838 it->phys_ascent = font->ascent + boff;
1839 it->phys_descent = font->descent - boff;
1840 it->pixel_width = FONT_WIDTH (font);
1843 /* If this is a space inside a region of text with
1844 `space-width' property, change its width. */
1845 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1846 if (stretched_p)
1847 it->pixel_width *= XFLOATINT (it->space_width);
1849 /* If face has a box, add the box thickness to the character
1850 height. If character has a box line to the left and/or
1851 right, add the box line width to the character's width. */
1852 if (face->box != FACE_NO_BOX)
1854 int thick = face->box_line_width;
1856 it->ascent += thick;
1857 it->descent += thick;
1859 if (it->start_of_box_run_p)
1860 it->pixel_width += thick;
1861 if (it->end_of_box_run_p)
1862 it->pixel_width += thick;
1865 /* If face has an overline, add the height of the overline
1866 (1 pixel) and a 1 pixel margin to the character height. */
1867 if (face->overline_p)
1868 it->ascent += 2;
1870 take_vertical_position_into_account (it);
1872 /* If we have to actually produce glyphs, do it. */
1873 if (it->glyph_row)
1875 if (stretched_p)
1877 /* Translate a space with a `space-width' property
1878 into a stretch glyph. */
1879 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1880 x_append_stretch_glyph (it, it->object, it->pixel_width,
1881 it->ascent + it->descent, ascent);
1883 else
1884 x_append_glyph (it);
1886 /* If characters with lbearing or rbearing are displayed
1887 in this line, record that fact in a flag of the
1888 glyph row. This is used to optimize X output code. */
1889 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1890 it->glyph_row->contains_overlapping_glyphs_p = 1;
1893 else if (it->char_to_display == '\n')
1895 /* A newline has no width but we need the height of the line. */
1896 it->pixel_width = 0;
1897 it->nglyphs = 0;
1898 it->ascent = it->phys_ascent = font->ascent + boff;
1899 it->descent = it->phys_descent = font->descent - boff;
1901 if (face->box != FACE_NO_BOX)
1903 int thick = face->box_line_width;
1904 it->ascent += thick;
1905 it->descent += thick;
1908 else if (it->char_to_display == '\t')
1910 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1911 int x = it->current_x + it->continuation_lines_width;
1912 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1914 it->pixel_width = next_tab_x - x;
1915 it->nglyphs = 1;
1916 it->ascent = it->phys_ascent = font->ascent + boff;
1917 it->descent = it->phys_descent = font->descent - boff;
1919 if (it->glyph_row)
1921 double ascent = (double) it->ascent / (it->ascent + it->descent);
1922 x_append_stretch_glyph (it, it->object, it->pixel_width,
1923 it->ascent + it->descent, ascent);
1926 else
1928 /* A multi-byte character. Assume that the display width of the
1929 character is the width of the character multiplied by the
1930 width of the font. */
1932 /* If we found a font, this font should give us the right
1933 metrics. If we didn't find a font, use the frame's
1934 default font and calculate the width of the character
1935 from the charset width; this is what old redisplay code
1936 did. */
1937 pcm = x_per_char_metric (font, &char2b);
1938 if (font_not_found_p || !pcm)
1940 int charset = CHAR_CHARSET (it->char_to_display);
1942 it->glyph_not_available_p = 1;
1943 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1944 * CHARSET_WIDTH (charset));
1945 it->phys_ascent = font->ascent + boff;
1946 it->phys_descent = font->descent - boff;
1948 else
1950 it->pixel_width = pcm->width;
1951 it->phys_ascent = pcm->ascent + boff;
1952 it->phys_descent = pcm->descent - boff;
1953 if (it->glyph_row
1954 && (pcm->lbearing < 0
1955 || pcm->rbearing > pcm->width))
1956 it->glyph_row->contains_overlapping_glyphs_p = 1;
1958 it->nglyphs = 1;
1959 it->ascent = font->ascent + boff;
1960 it->descent = font->descent - boff;
1961 if (face->box != FACE_NO_BOX)
1963 int thick = face->box_line_width;
1964 it->ascent += thick;
1965 it->descent += thick;
1967 if (it->start_of_box_run_p)
1968 it->pixel_width += thick;
1969 if (it->end_of_box_run_p)
1970 it->pixel_width += thick;
1973 /* If face has an overline, add the height of the overline
1974 (1 pixel) and a 1 pixel margin to the character height. */
1975 if (face->overline_p)
1976 it->ascent += 2;
1978 take_vertical_position_into_account (it);
1980 if (it->glyph_row)
1981 x_append_glyph (it);
1984 else if (it->what == IT_COMPOSITION)
1986 /* Note: A composition is represented as one glyph in the
1987 glyph matrix. There are no padding glyphs. */
1988 XChar2b char2b;
1989 XFontStruct *font;
1990 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1991 XCharStruct *pcm;
1992 int font_not_found_p;
1993 struct font_info *font_info;
1994 int boff; /* baseline offset */
1995 struct composition *cmp = composition_table[it->cmp_id];
1997 /* Maybe translate single-byte characters to multibyte. */
1998 it->char_to_display = it->c;
1999 if (unibyte_display_via_language_environment
2000 && SINGLE_BYTE_CHAR_P (it->c)
2001 && (it->c >= 0240
2002 || (it->c >= 0200
2003 && !NILP (Vnonascii_translation_table))))
2005 it->char_to_display = unibyte_char_to_multibyte (it->c);
2008 /* Get face and font to use. Encode IT->char_to_display. */
2009 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2010 face = FACE_FROM_ID (it->f, it->face_id);
2011 x_get_char_face_and_encoding (it->f, it->char_to_display,
2012 it->face_id, &char2b, it->multibyte_p);
2013 font = face->font;
2015 /* When no suitable font found, use the default font. */
2016 font_not_found_p = font == NULL;
2017 if (font_not_found_p)
2019 font = FRAME_FONT (it->f);
2020 boff = it->f->output_data.x->baseline_offset;
2021 font_info = NULL;
2023 else
2025 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2026 boff = font_info->baseline_offset;
2027 if (font_info->vertical_centering)
2028 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2031 /* There are no padding glyphs, so there is only one glyph to
2032 produce for the composition. Important is that pixel_width,
2033 ascent and descent are the values of what is drawn by
2034 draw_glyphs (i.e. the values of the overall glyphs composed). */
2035 it->nglyphs = 1;
2037 /* If we have not yet calculated pixel size data of glyphs of
2038 the composition for the current face font, calculate them
2039 now. Theoretically, we have to check all fonts for the
2040 glyphs, but that requires much time and memory space. So,
2041 here we check only the font of the first glyph. This leads
2042 to incorrect display very rarely, and C-l (recenter) can
2043 correct the display anyway. */
2044 if (cmp->font != (void *) font)
2046 /* Ascent and descent of the font of the first character of
2047 this composition (adjusted by baseline offset). Ascent
2048 and descent of overall glyphs should not be less than
2049 them respectively. */
2050 int font_ascent = font->ascent + boff;
2051 int font_descent = font->descent - boff;
2052 /* Bounding box of the overall glyphs. */
2053 int leftmost, rightmost, lowest, highest;
2054 int i, width, ascent, descent;
2056 cmp->font = (void *) font;
2058 /* Initialize the bounding box. */
2059 pcm = x_per_char_metric (font, &char2b);
2060 if (pcm)
2062 width = pcm->width;
2063 ascent = pcm->ascent;
2064 descent = pcm->descent;
2066 else
2068 width = FONT_WIDTH (font);
2069 ascent = font->ascent;
2070 descent = font->descent;
2073 rightmost = width;
2074 lowest = - descent + boff;
2075 highest = ascent + boff;
2076 leftmost = 0;
2078 if (font_info
2079 && font_info->default_ascent
2080 && CHAR_TABLE_P (Vuse_default_ascent)
2081 && !NILP (Faref (Vuse_default_ascent,
2082 make_number (it->char_to_display))))
2083 highest = font_info->default_ascent + boff;
2085 /* Draw the first glyph at the normal position. It may be
2086 shifted to right later if some other glyphs are drawn at
2087 the left. */
2088 cmp->offsets[0] = 0;
2089 cmp->offsets[1] = boff;
2091 /* Set cmp->offsets for the remaining glyphs. */
2092 for (i = 1; i < cmp->glyph_len; i++)
2094 int left, right, btm, top;
2095 int ch = COMPOSITION_GLYPH (cmp, i);
2096 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2098 face = FACE_FROM_ID (it->f, face_id);
2099 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2100 it->multibyte_p);
2101 font = face->font;
2102 if (font == NULL)
2104 font = FRAME_FONT (it->f);
2105 boff = it->f->output_data.x->baseline_offset;
2106 font_info = NULL;
2108 else
2110 font_info
2111 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2112 boff = font_info->baseline_offset;
2113 if (font_info->vertical_centering)
2114 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2117 pcm = x_per_char_metric (font, &char2b);
2118 if (pcm)
2120 width = pcm->width;
2121 ascent = pcm->ascent;
2122 descent = pcm->descent;
2124 else
2126 width = FONT_WIDTH (font);
2127 ascent = font->ascent;
2128 descent = font->descent;
2131 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2133 /* Relative composition with or without
2134 alternate chars. */
2135 left = (leftmost + rightmost - width) / 2;
2136 btm = - descent + boff;
2137 if (font_info && font_info->relative_compose
2138 && (! CHAR_TABLE_P (Vignore_relative_composition)
2139 || NILP (Faref (Vignore_relative_composition,
2140 make_number (ch)))))
2143 if (- descent >= font_info->relative_compose)
2144 /* One extra pixel between two glyphs. */
2145 btm = highest + 1;
2146 else if (ascent <= 0)
2147 /* One extra pixel between two glyphs. */
2148 btm = lowest - 1 - ascent - descent;
2151 else
2153 /* A composition rule is specified by an integer
2154 value that encodes global and new reference
2155 points (GREF and NREF). GREF and NREF are
2156 specified by numbers as below:
2158 0---1---2 -- ascent
2162 9--10--11 -- center
2164 ---3---4---5--- baseline
2166 6---7---8 -- descent
2168 int rule = COMPOSITION_RULE (cmp, i);
2169 int gref, nref, grefx, grefy, nrefx, nrefy;
2171 COMPOSITION_DECODE_RULE (rule, gref, nref);
2172 grefx = gref % 3, nrefx = nref % 3;
2173 grefy = gref / 3, nrefy = nref / 3;
2175 left = (leftmost
2176 + grefx * (rightmost - leftmost) / 2
2177 - nrefx * width / 2);
2178 btm = ((grefy == 0 ? highest
2179 : grefy == 1 ? 0
2180 : grefy == 2 ? lowest
2181 : (highest + lowest) / 2)
2182 - (nrefy == 0 ? ascent + descent
2183 : nrefy == 1 ? descent - boff
2184 : nrefy == 2 ? 0
2185 : (ascent + descent) / 2));
2188 cmp->offsets[i * 2] = left;
2189 cmp->offsets[i * 2 + 1] = btm + descent;
2191 /* Update the bounding box of the overall glyphs. */
2192 right = left + width;
2193 top = btm + descent + ascent;
2194 if (left < leftmost)
2195 leftmost = left;
2196 if (right > rightmost)
2197 rightmost = right;
2198 if (top > highest)
2199 highest = top;
2200 if (btm < lowest)
2201 lowest = btm;
2204 /* If there are glyphs whose x-offsets are negative,
2205 shift all glyphs to the right and make all x-offsets
2206 non-negative. */
2207 if (leftmost < 0)
2209 for (i = 0; i < cmp->glyph_len; i++)
2210 cmp->offsets[i * 2] -= leftmost;
2211 rightmost -= leftmost;
2214 cmp->pixel_width = rightmost;
2215 cmp->ascent = highest;
2216 cmp->descent = - lowest;
2217 if (cmp->ascent < font_ascent)
2218 cmp->ascent = font_ascent;
2219 if (cmp->descent < font_descent)
2220 cmp->descent = font_descent;
2223 it->pixel_width = cmp->pixel_width;
2224 it->ascent = it->phys_ascent = cmp->ascent;
2225 it->descent = it->phys_descent = cmp->descent;
2227 if (face->box != FACE_NO_BOX)
2229 int thick = face->box_line_width;
2230 it->ascent += thick;
2231 it->descent += thick;
2233 if (it->start_of_box_run_p)
2234 it->pixel_width += thick;
2235 if (it->end_of_box_run_p)
2236 it->pixel_width += thick;
2239 /* If face has an overline, add the height of the overline
2240 (1 pixel) and a 1 pixel margin to the character height. */
2241 if (face->overline_p)
2242 it->ascent += 2;
2244 take_vertical_position_into_account (it);
2246 if (it->glyph_row)
2247 x_append_composite_glyph (it);
2249 else if (it->what == IT_IMAGE)
2250 x_produce_image_glyph (it);
2251 else if (it->what == IT_STRETCH)
2252 x_produce_stretch_glyph (it);
2254 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2255 because this isn't true for images with `:ascent 100'. */
2256 xassert (it->ascent >= 0 && it->descent >= 0);
2257 if (it->area == TEXT_AREA)
2258 it->current_x += it->pixel_width;
2260 it->descent += it->extra_line_spacing;
2262 it->max_ascent = max (it->max_ascent, it->ascent);
2263 it->max_descent = max (it->max_descent, it->descent);
2264 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2265 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2269 /* Estimate the pixel height of the mode or top line on frame F.
2270 FACE_ID specifies what line's height to estimate. */
2273 x_estimate_mode_line_height (f, face_id)
2274 struct frame *f;
2275 enum face_id face_id;
2277 int height = 1;
2279 /* This function is called so early when Emacs starts that the face
2280 cache and mode line face are not yet initialized. */
2281 if (FRAME_FACE_CACHE (f))
2283 struct face *face = FACE_FROM_ID (f, face_id);
2284 if (face)
2285 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2288 return height;
2292 /***********************************************************************
2293 Glyph display
2294 ***********************************************************************/
2296 /* A sequence of glyphs to be drawn in the same face.
2298 This data structure is not really completely X specific, so it
2299 could possibly, at least partially, be useful for other systems. It
2300 is currently not part of the external redisplay interface because
2301 it's not clear what other systems will need. */
2303 struct glyph_string
2305 /* X-origin of the string. */
2306 int x;
2308 /* Y-origin and y-position of the base line of this string. */
2309 int y, ybase;
2311 /* The width of the string, not including a face extension. */
2312 int width;
2314 /* The width of the string, including a face extension. */
2315 int background_width;
2317 /* The height of this string. This is the height of the line this
2318 string is drawn in, and can be different from the height of the
2319 font the string is drawn in. */
2320 int height;
2322 /* Number of pixels this string overwrites in front of its x-origin.
2323 This number is zero if the string has an lbearing >= 0; it is
2324 -lbearing, if the string has an lbearing < 0. */
2325 int left_overhang;
2327 /* Number of pixels this string overwrites past its right-most
2328 nominal x-position, i.e. x + width. Zero if the string's
2329 rbearing is <= its nominal width, rbearing - width otherwise. */
2330 int right_overhang;
2332 /* The frame on which the glyph string is drawn. */
2333 struct frame *f;
2335 /* The window on which the glyph string is drawn. */
2336 struct window *w;
2338 /* X display and window for convenience. */
2339 Display *display;
2340 Window window;
2342 /* The glyph row for which this string was built. It determines the
2343 y-origin and height of the string. */
2344 struct glyph_row *row;
2346 /* The area within row. */
2347 enum glyph_row_area area;
2349 /* Characters to be drawn, and number of characters. */
2350 XChar2b *char2b;
2351 int nchars;
2353 /* A face-override for drawing cursors, mouse face and similar. */
2354 enum draw_glyphs_face hl;
2356 /* Face in which this string is to be drawn. */
2357 struct face *face;
2359 /* Font in which this string is to be drawn. */
2360 XFontStruct *font;
2362 /* Font info for this string. */
2363 struct font_info *font_info;
2365 /* Non-null means this string describes (part of) a composition.
2366 All characters from char2b are drawn composed. */
2367 struct composition *cmp;
2369 /* Index of this glyph string's first character in the glyph
2370 definition of CMP. If this is zero, this glyph string describes
2371 the first character of a composition. */
2372 int gidx;
2374 /* 1 means this glyph strings face has to be drawn to the right end
2375 of the window's drawing area. */
2376 unsigned extends_to_end_of_line_p : 1;
2378 /* 1 means the background of this string has been drawn. */
2379 unsigned background_filled_p : 1;
2381 /* 1 means glyph string must be drawn with 16-bit functions. */
2382 unsigned two_byte_p : 1;
2384 /* 1 means that the original font determined for drawing this glyph
2385 string could not be loaded. The member `font' has been set to
2386 the frame's default font in this case. */
2387 unsigned font_not_found_p : 1;
2389 /* 1 means that the face in which this glyph string is drawn has a
2390 stipple pattern. */
2391 unsigned stippled_p : 1;
2393 /* 1 means only the foreground of this glyph string must be drawn,
2394 and we should use the physical height of the line this glyph
2395 string appears in as clip rect. */
2396 unsigned for_overlaps_p : 1;
2398 /* The GC to use for drawing this glyph string. */
2399 GC gc;
2401 /* A pointer to the first glyph in the string. This glyph
2402 corresponds to char2b[0]. Needed to draw rectangles if
2403 font_not_found_p is 1. */
2404 struct glyph *first_glyph;
2406 /* Image, if any. */
2407 struct image *img;
2409 struct glyph_string *next, *prev;
2413 #if 0
2415 static void
2416 x_dump_glyph_string (s)
2417 struct glyph_string *s;
2419 fprintf (stderr, "glyph string\n");
2420 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2421 s->x, s->y, s->width, s->height);
2422 fprintf (stderr, " ybase = %d\n", s->ybase);
2423 fprintf (stderr, " hl = %d\n", s->hl);
2424 fprintf (stderr, " left overhang = %d, right = %d\n",
2425 s->left_overhang, s->right_overhang);
2426 fprintf (stderr, " nchars = %d\n", s->nchars);
2427 fprintf (stderr, " extends to end of line = %d\n",
2428 s->extends_to_end_of_line_p);
2429 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2430 fprintf (stderr, " bg width = %d\n", s->background_width);
2433 #endif /* GLYPH_DEBUG */
2437 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2438 struct glyph_string **,
2439 struct glyph_string *,
2440 struct glyph_string *));
2441 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2442 struct glyph_string **,
2443 struct glyph_string *,
2444 struct glyph_string *));
2445 static void x_append_glyph_string P_ ((struct glyph_string **,
2446 struct glyph_string **,
2447 struct glyph_string *));
2448 static int x_left_overwritten P_ ((struct glyph_string *));
2449 static int x_left_overwriting P_ ((struct glyph_string *));
2450 static int x_right_overwritten P_ ((struct glyph_string *));
2451 static int x_right_overwriting P_ ((struct glyph_string *));
2452 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2453 int));
2454 static void x_init_glyph_string P_ ((struct glyph_string *,
2455 XChar2b *, struct window *,
2456 struct glyph_row *,
2457 enum glyph_row_area, int,
2458 enum draw_glyphs_face));
2459 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2460 enum glyph_row_area, int, int,
2461 enum draw_glyphs_face, int *, int *, int));
2462 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2463 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2464 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2465 int));
2466 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2467 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2468 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2469 static void x_draw_glyph_string P_ ((struct glyph_string *));
2470 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2471 static void x_set_cursor_gc P_ ((struct glyph_string *));
2472 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2473 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2474 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2475 int *, int *));
2476 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2477 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2478 unsigned long *, double, int));
2479 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2480 double, int, unsigned long));
2481 static void x_setup_relief_colors P_ ((struct glyph_string *));
2482 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2483 static void x_draw_image_relief P_ ((struct glyph_string *));
2484 static void x_draw_image_foreground P_ ((struct glyph_string *));
2485 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2486 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2487 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2488 int, int, int));
2489 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2490 int, int, int, int, XRectangle *));
2491 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2492 int, int, int, XRectangle *));
2493 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2494 enum glyph_row_area));
2496 #if GLYPH_DEBUG
2497 static void x_check_font P_ ((struct frame *, XFontStruct *));
2498 #endif
2501 /* Append the list of glyph strings with head H and tail T to the list
2502 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2504 static INLINE void
2505 x_append_glyph_string_lists (head, tail, h, t)
2506 struct glyph_string **head, **tail;
2507 struct glyph_string *h, *t;
2509 if (h)
2511 if (*head)
2512 (*tail)->next = h;
2513 else
2514 *head = h;
2515 h->prev = *tail;
2516 *tail = t;
2521 /* Prepend the list of glyph strings with head H and tail T to the
2522 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2523 result. */
2525 static INLINE void
2526 x_prepend_glyph_string_lists (head, tail, h, t)
2527 struct glyph_string **head, **tail;
2528 struct glyph_string *h, *t;
2530 if (h)
2532 if (*head)
2533 (*head)->prev = t;
2534 else
2535 *tail = t;
2536 t->next = *head;
2537 *head = h;
2542 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2543 Set *HEAD and *TAIL to the resulting list. */
2545 static INLINE void
2546 x_append_glyph_string (head, tail, s)
2547 struct glyph_string **head, **tail;
2548 struct glyph_string *s;
2550 s->next = s->prev = NULL;
2551 x_append_glyph_string_lists (head, tail, s, s);
2555 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2556 face. */
2558 static void
2559 x_set_cursor_gc (s)
2560 struct glyph_string *s;
2562 if (s->font == FRAME_FONT (s->f)
2563 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2564 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2565 && !s->cmp)
2566 s->gc = s->f->output_data.x->cursor_gc;
2567 else
2569 /* Cursor on non-default face: must merge. */
2570 XGCValues xgcv;
2571 unsigned long mask;
2573 xgcv.background = s->f->output_data.x->cursor_pixel;
2574 xgcv.foreground = s->face->background;
2576 /* If the glyph would be invisible, try a different foreground. */
2577 if (xgcv.foreground == xgcv.background)
2578 xgcv.foreground = s->face->foreground;
2579 if (xgcv.foreground == xgcv.background)
2580 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2581 if (xgcv.foreground == xgcv.background)
2582 xgcv.foreground = s->face->foreground;
2584 /* Make sure the cursor is distinct from text in this face. */
2585 if (xgcv.background == s->face->background
2586 && xgcv.foreground == s->face->foreground)
2588 xgcv.background = s->face->foreground;
2589 xgcv.foreground = s->face->background;
2592 IF_DEBUG (x_check_font (s->f, s->font));
2593 xgcv.font = s->font->fid;
2594 xgcv.graphics_exposures = False;
2595 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2597 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2598 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2599 mask, &xgcv);
2600 else
2601 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2602 = XCreateGC (s->display, s->window, mask, &xgcv);
2604 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2609 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2611 static void
2612 x_set_mouse_face_gc (s)
2613 struct glyph_string *s;
2615 int face_id;
2616 struct face *face;
2618 /* What face has to be used for the mouse face? */
2619 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2620 face = FACE_FROM_ID (s->f, face_id);
2621 if (s->first_glyph->type == CHAR_GLYPH)
2622 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2623 else
2624 face_id = FACE_FOR_CHAR (s->f, face, 0);
2625 s->face = FACE_FROM_ID (s->f, face_id);
2626 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2628 /* If font in this face is same as S->font, use it. */
2629 if (s->font == s->face->font)
2630 s->gc = s->face->gc;
2631 else
2633 /* Otherwise construct scratch_cursor_gc with values from FACE
2634 but font FONT. */
2635 XGCValues xgcv;
2636 unsigned long mask;
2638 xgcv.background = s->face->background;
2639 xgcv.foreground = s->face->foreground;
2640 IF_DEBUG (x_check_font (s->f, s->font));
2641 xgcv.font = s->font->fid;
2642 xgcv.graphics_exposures = False;
2643 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2645 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2646 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2647 mask, &xgcv);
2648 else
2649 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2650 = XCreateGC (s->display, s->window, mask, &xgcv);
2652 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2655 xassert (s->gc != 0);
2659 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2660 Faces to use in the mode line have already been computed when the
2661 matrix was built, so there isn't much to do, here. */
2663 static INLINE void
2664 x_set_mode_line_face_gc (s)
2665 struct glyph_string *s;
2667 s->gc = s->face->gc;
2668 xassert (s->gc != 0);
2672 /* Set S->gc of glyph string S for drawing that glyph string. Set
2673 S->stippled_p to a non-zero value if the face of S has a stipple
2674 pattern. */
2676 static INLINE void
2677 x_set_glyph_string_gc (s)
2678 struct glyph_string *s;
2680 if (s->hl == DRAW_NORMAL_TEXT)
2682 s->gc = s->face->gc;
2683 s->stippled_p = s->face->stipple != 0;
2685 else if (s->hl == DRAW_INVERSE_VIDEO)
2687 x_set_mode_line_face_gc (s);
2688 s->stippled_p = s->face->stipple != 0;
2690 else if (s->hl == DRAW_CURSOR)
2692 x_set_cursor_gc (s);
2693 s->stippled_p = 0;
2695 else if (s->hl == DRAW_MOUSE_FACE)
2697 x_set_mouse_face_gc (s);
2698 s->stippled_p = s->face->stipple != 0;
2700 else if (s->hl == DRAW_IMAGE_RAISED
2701 || s->hl == DRAW_IMAGE_SUNKEN)
2703 s->gc = s->face->gc;
2704 s->stippled_p = s->face->stipple != 0;
2706 else
2708 s->gc = s->face->gc;
2709 s->stippled_p = s->face->stipple != 0;
2712 /* GC must have been set. */
2713 xassert (s->gc != 0);
2717 /* Return in *R the clipping rectangle for glyph string S. */
2719 static void
2720 x_get_glyph_string_clip_rect (s, r)
2721 struct glyph_string *s;
2722 XRectangle *r;
2724 if (s->row->full_width_p)
2726 /* Draw full-width. X coordinates are relative to S->w->left. */
2727 int canon_x = CANON_X_UNIT (s->f);
2729 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2730 r->width = XFASTINT (s->w->width) * canon_x;
2732 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2734 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2735 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2736 r->x -= width;
2739 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2741 /* Unless displaying a mode or menu bar line, which are always
2742 fully visible, clip to the visible part of the row. */
2743 if (s->w->pseudo_window_p)
2744 r->height = s->row->visible_height;
2745 else
2746 r->height = s->height;
2748 else
2750 /* This is a text line that may be partially visible. */
2751 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2752 r->width = window_box_width (s->w, s->area);
2753 r->height = s->row->visible_height;
2756 /* Don't use S->y for clipping because it doesn't take partially
2757 visible lines into account. For example, it can be negative for
2758 partially visible lines at the top of a window. */
2759 if (!s->row->full_width_p
2760 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2761 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2762 else
2763 r->y = max (0, s->row->y);
2765 /* If drawing a tool-bar window, draw it over the internal border
2766 at the top of the window. */
2767 if (s->w == XWINDOW (s->f->tool_bar_window))
2768 r->y -= s->f->output_data.x->internal_border_width;
2770 /* If S draws overlapping rows, it's sufficient to use the top and
2771 bottom of the window for clipping because this glyph string
2772 intentionally draws over other lines. */
2773 if (s->for_overlaps_p)
2775 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2776 r->height = window_text_bottom_y (s->w) - r->y;
2779 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2783 /* Set clipping for output of glyph string S. S may be part of a mode
2784 line or menu if we don't have X toolkit support. */
2786 static INLINE void
2787 x_set_glyph_string_clipping (s)
2788 struct glyph_string *s;
2790 XRectangle r;
2791 x_get_glyph_string_clip_rect (s, &r);
2792 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2796 /* Compute left and right overhang of glyph string S. If S is a glyph
2797 string for a composition, assume overhangs don't exist. */
2799 static INLINE void
2800 x_compute_glyph_string_overhangs (s)
2801 struct glyph_string *s;
2803 if (s->cmp == NULL
2804 && s->first_glyph->type == CHAR_GLYPH)
2806 XCharStruct cs;
2807 int direction, font_ascent, font_descent;
2808 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2809 &font_ascent, &font_descent, &cs);
2810 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2811 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2816 /* Compute overhangs and x-positions for glyph string S and its
2817 predecessors, or successors. X is the starting x-position for S.
2818 BACKWARD_P non-zero means process predecessors. */
2820 static void
2821 x_compute_overhangs_and_x (s, x, backward_p)
2822 struct glyph_string *s;
2823 int x;
2824 int backward_p;
2826 if (backward_p)
2828 while (s)
2830 x_compute_glyph_string_overhangs (s);
2831 x -= s->width;
2832 s->x = x;
2833 s = s->prev;
2836 else
2838 while (s)
2840 x_compute_glyph_string_overhangs (s);
2841 s->x = x;
2842 x += s->width;
2843 s = s->next;
2849 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2850 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2851 assumed to be zero. */
2853 static void
2854 x_get_glyph_overhangs (glyph, f, left, right)
2855 struct glyph *glyph;
2856 struct frame *f;
2857 int *left, *right;
2859 *left = *right = 0;
2861 if (glyph->type == CHAR_GLYPH)
2863 XFontStruct *font;
2864 struct face *face;
2865 struct font_info *font_info;
2866 XChar2b char2b;
2867 XCharStruct *pcm;
2869 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2870 font = face->font;
2871 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2872 if (font
2873 && (pcm = x_per_char_metric (font, &char2b)))
2875 if (pcm->rbearing > pcm->width)
2876 *right = pcm->rbearing - pcm->width;
2877 if (pcm->lbearing < 0)
2878 *left = -pcm->lbearing;
2884 /* Return the index of the first glyph preceding glyph string S that
2885 is overwritten by S because of S's left overhang. Value is -1
2886 if no glyphs are overwritten. */
2888 static int
2889 x_left_overwritten (s)
2890 struct glyph_string *s;
2892 int k;
2894 if (s->left_overhang)
2896 int x = 0, i;
2897 struct glyph *glyphs = s->row->glyphs[s->area];
2898 int first = s->first_glyph - glyphs;
2900 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2901 x -= glyphs[i].pixel_width;
2903 k = i + 1;
2905 else
2906 k = -1;
2908 return k;
2912 /* Return the index of the first glyph preceding glyph string S that
2913 is overwriting S because of its right overhang. Value is -1 if no
2914 glyph in front of S overwrites S. */
2916 static int
2917 x_left_overwriting (s)
2918 struct glyph_string *s;
2920 int i, k, x;
2921 struct glyph *glyphs = s->row->glyphs[s->area];
2922 int first = s->first_glyph - glyphs;
2924 k = -1;
2925 x = 0;
2926 for (i = first - 1; i >= 0; --i)
2928 int left, right;
2929 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2930 if (x + right > 0)
2931 k = i;
2932 x -= glyphs[i].pixel_width;
2935 return k;
2939 /* Return the index of the last glyph following glyph string S that is
2940 not overwritten by S because of S's right overhang. Value is -1 if
2941 no such glyph is found. */
2943 static int
2944 x_right_overwritten (s)
2945 struct glyph_string *s;
2947 int k = -1;
2949 if (s->right_overhang)
2951 int x = 0, i;
2952 struct glyph *glyphs = s->row->glyphs[s->area];
2953 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2954 int end = s->row->used[s->area];
2956 for (i = first; i < end && s->right_overhang > x; ++i)
2957 x += glyphs[i].pixel_width;
2959 k = i;
2962 return k;
2966 /* Return the index of the last glyph following glyph string S that
2967 overwrites S because of its left overhang. Value is negative
2968 if no such glyph is found. */
2970 static int
2971 x_right_overwriting (s)
2972 struct glyph_string *s;
2974 int i, k, x;
2975 int end = s->row->used[s->area];
2976 struct glyph *glyphs = s->row->glyphs[s->area];
2977 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2979 k = -1;
2980 x = 0;
2981 for (i = first; i < end; ++i)
2983 int left, right;
2984 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2985 if (x - left < 0)
2986 k = i;
2987 x += glyphs[i].pixel_width;
2990 return k;
2994 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
2996 static INLINE void
2997 x_clear_glyph_string_rect (s, x, y, w, h)
2998 struct glyph_string *s;
2999 int x, y, w, h;
3001 XGCValues xgcv;
3002 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3003 XSetForeground (s->display, s->gc, xgcv.background);
3004 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3005 XSetForeground (s->display, s->gc, xgcv.foreground);
3009 /* Draw the background of glyph_string S. If S->background_filled_p
3010 is non-zero don't draw it. FORCE_P non-zero means draw the
3011 background even if it wouldn't be drawn normally. This is used
3012 when a string preceding S draws into the background of S, or S
3013 contains the first component of a composition. */
3015 static void
3016 x_draw_glyph_string_background (s, force_p)
3017 struct glyph_string *s;
3018 int force_p;
3020 /* Nothing to do if background has already been drawn or if it
3021 shouldn't be drawn in the first place. */
3022 if (!s->background_filled_p)
3024 if (s->stippled_p)
3026 /* Fill background with a stipple pattern. */
3027 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3028 XFillRectangle (s->display, s->window, s->gc, s->x,
3029 s->y + s->face->box_line_width,
3030 s->background_width,
3031 s->height - 2 * s->face->box_line_width);
3032 XSetFillStyle (s->display, s->gc, FillSolid);
3033 s->background_filled_p = 1;
3035 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3036 || s->font_not_found_p
3037 || s->extends_to_end_of_line_p
3038 || force_p)
3040 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3041 s->background_width,
3042 s->height - 2 * s->face->box_line_width);
3043 s->background_filled_p = 1;
3049 /* Draw the foreground of glyph string S. */
3051 static void
3052 x_draw_glyph_string_foreground (s)
3053 struct glyph_string *s;
3055 int i, x;
3057 /* If first glyph of S has a left box line, start drawing the text
3058 of S to the right of that box line. */
3059 if (s->face->box != FACE_NO_BOX
3060 && s->first_glyph->left_box_line_p)
3061 x = s->x + s->face->box_line_width;
3062 else
3063 x = s->x;
3065 /* Draw characters of S as rectangles if S's font could not be
3066 loaded. */
3067 if (s->font_not_found_p)
3069 for (i = 0; i < s->nchars; ++i)
3071 struct glyph *g = s->first_glyph + i;
3072 XDrawRectangle (s->display, s->window,
3073 s->gc, x, s->y, g->pixel_width - 1,
3074 s->height - 1);
3075 x += g->pixel_width;
3078 else
3080 char *char1b = (char *) s->char2b;
3081 int boff = s->font_info->baseline_offset;
3083 if (s->font_info->vertical_centering)
3084 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3086 /* If we can use 8-bit functions, condense S->char2b. */
3087 if (!s->two_byte_p)
3088 for (i = 0; i < s->nchars; ++i)
3089 char1b[i] = s->char2b[i].byte2;
3091 /* Draw text with XDrawString if background has already been
3092 filled. Otherwise, use XDrawImageString. (Note that
3093 XDrawImageString is usually faster than XDrawString.) Always
3094 use XDrawImageString when drawing the cursor so that there is
3095 no chance that characters under a box cursor are invisible. */
3096 if (s->for_overlaps_p
3097 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3099 /* Draw characters with 16-bit or 8-bit functions. */
3100 if (s->two_byte_p)
3101 XDrawString16 (s->display, s->window, s->gc, x,
3102 s->ybase - boff, s->char2b, s->nchars);
3103 else
3104 XDrawString (s->display, s->window, s->gc, x,
3105 s->ybase - boff, char1b, s->nchars);
3107 else
3109 if (s->two_byte_p)
3110 XDrawImageString16 (s->display, s->window, s->gc, x,
3111 s->ybase - boff, s->char2b, s->nchars);
3112 else
3113 XDrawImageString (s->display, s->window, s->gc, x,
3114 s->ybase - boff, char1b, s->nchars);
3119 /* Draw the foreground of composite glyph string S. */
3121 static void
3122 x_draw_composite_glyph_string_foreground (s)
3123 struct glyph_string *s;
3125 int i, x;
3127 /* If first glyph of S has a left box line, start drawing the text
3128 of S to the right of that box line. */
3129 if (s->face->box != FACE_NO_BOX
3130 && s->first_glyph->left_box_line_p)
3131 x = s->x + s->face->box_line_width;
3132 else
3133 x = s->x;
3135 /* S is a glyph string for a composition. S->gidx is the index of
3136 the first character drawn for glyphs of this composition.
3137 S->gidx == 0 means we are drawing the very first character of
3138 this composition. */
3140 /* Draw a rectangle for the composition if the font for the very
3141 first character of the composition could not be loaded. */
3142 if (s->font_not_found_p)
3144 if (s->gidx == 0)
3145 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3146 s->width - 1, s->height - 1);
3148 else
3150 for (i = 0; i < s->nchars; i++, ++s->gidx)
3151 XDrawString16 (s->display, s->window, s->gc,
3152 x + s->cmp->offsets[s->gidx * 2],
3153 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3154 s->char2b + i, 1);
3159 #ifdef USE_X_TOOLKIT
3161 static struct frame *x_frame_of_widget P_ ((Widget));
3164 /* Return the frame on which widget WIDGET is used.. Abort if frame
3165 cannot be determined. */
3167 static struct frame *
3168 x_frame_of_widget (widget)
3169 Widget widget;
3171 struct x_display_info *dpyinfo;
3172 Lisp_Object tail;
3173 struct frame *f;
3175 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3177 /* Find the top-level shell of the widget. Note that this function
3178 can be called when the widget is not yet realized, so XtWindow
3179 (widget) == 0. That's the reason we can't simply use
3180 x_any_window_to_frame. */
3181 while (!XtIsTopLevelShell (widget))
3182 widget = XtParent (widget);
3184 /* Look for a frame with that top-level widget. Allocate the color
3185 on that frame to get the right gamma correction value. */
3186 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3187 if (GC_FRAMEP (XCAR (tail))
3188 && (f = XFRAME (XCAR (tail)),
3189 (f->output_data.nothing != 1
3190 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3191 && f->output_data.x->widget == widget)
3192 return f;
3194 abort ();
3198 /* Allocate the color COLOR->pixel on the screen and display of
3199 widget WIDGET in colormap CMAP. If an exact match cannot be
3200 allocated, try the nearest color available. Value is non-zero
3201 if successful. This is called from lwlib. */
3204 x_alloc_nearest_color_for_widget (widget, cmap, color)
3205 Widget widget;
3206 Colormap cmap;
3207 XColor *color;
3209 struct frame *f = x_frame_of_widget (widget);
3210 return x_alloc_nearest_color (f, cmap, color);
3214 #endif /* USE_X_TOOLKIT */
3217 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3218 CMAP. If an exact match can't be allocated, try the nearest color
3219 available. Value is non-zero if successful. Set *COLOR to the
3220 color allocated. */
3223 x_alloc_nearest_color (f, cmap, color)
3224 struct frame *f;
3225 Colormap cmap;
3226 XColor *color;
3228 Display *display = FRAME_X_DISPLAY (f);
3229 Screen *screen = FRAME_X_SCREEN (f);
3230 int rc;
3232 gamma_correct (f, color);
3233 rc = XAllocColor (display, cmap, color);
3234 if (rc == 0)
3236 /* If we got to this point, the colormap is full, so we're going
3237 to try to get the next closest color. The algorithm used is
3238 a least-squares matching, which is what X uses for closest
3239 color matching with StaticColor visuals. */
3240 int nearest, i;
3241 unsigned long nearest_delta = ~0;
3242 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3243 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3245 for (i = 0; i < ncells; ++i)
3246 cells[i].pixel = i;
3247 XQueryColors (display, cmap, cells, ncells);
3249 for (nearest = i = 0; i < ncells; ++i)
3251 long dred = (color->red >> 8) - (cells[i].red >> 8);
3252 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3253 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3254 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3256 if (delta < nearest_delta)
3258 nearest = i;
3259 nearest_delta = delta;
3263 color->red = cells[nearest].red;
3264 color->green = cells[nearest].green;
3265 color->blue = cells[nearest].blue;
3266 rc = XAllocColor (display, cmap, color);
3269 #ifdef DEBUG_X_COLORS
3270 if (rc)
3271 register_color (color->pixel);
3272 #endif /* DEBUG_X_COLORS */
3274 return rc;
3278 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3279 It's necessary to do this instead of just using PIXEL directly to
3280 get color reference counts right. */
3282 unsigned long
3283 x_copy_color (f, pixel)
3284 struct frame *f;
3285 unsigned long pixel;
3287 XColor color;
3289 color.pixel = pixel;
3290 BLOCK_INPUT;
3291 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3292 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3293 UNBLOCK_INPUT;
3294 #ifdef DEBUG_X_COLORS
3295 register_color (pixel);
3296 #endif
3297 return color.pixel;
3301 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3302 It's necessary to do this instead of just using PIXEL directly to
3303 get color reference counts right. */
3305 unsigned long
3306 x_copy_dpy_color (dpy, cmap, pixel)
3307 Display *dpy;
3308 Colormap cmap;
3309 unsigned long pixel;
3311 XColor color;
3313 color.pixel = pixel;
3314 BLOCK_INPUT;
3315 XQueryColor (dpy, cmap, &color);
3316 XAllocColor (dpy, cmap, &color);
3317 UNBLOCK_INPUT;
3318 #ifdef DEBUG_X_COLORS
3319 register_color (pixel);
3320 #endif
3321 return color.pixel;
3325 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3326 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3327 If this produces the same color as PIXEL, try a color where all RGB
3328 values have DELTA added. Return the allocated color in *PIXEL.
3329 DISPLAY is the X display, CMAP is the colormap to operate on.
3330 Value is non-zero if successful. */
3332 static int
3333 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3334 struct frame *f;
3335 Display *display;
3336 Colormap cmap;
3337 unsigned long *pixel;
3338 double factor;
3339 int delta;
3341 XColor color, new;
3342 int success_p;
3344 /* Get RGB color values. */
3345 color.pixel = *pixel;
3346 XQueryColor (display, cmap, &color);
3348 /* Change RGB values by specified FACTOR. Avoid overflow! */
3349 xassert (factor >= 0);
3350 new.red = min (0xffff, factor * color.red);
3351 new.green = min (0xffff, factor * color.green);
3352 new.blue = min (0xffff, factor * color.blue);
3354 /* Try to allocate the color. */
3355 success_p = x_alloc_nearest_color (f, cmap, &new);
3356 if (success_p)
3358 if (new.pixel == *pixel)
3360 /* If we end up with the same color as before, try adding
3361 delta to the RGB values. */
3362 x_free_colors (f, &new.pixel, 1);
3364 new.red = min (0xffff, delta + color.red);
3365 new.green = min (0xffff, delta + color.green);
3366 new.blue = min (0xffff, delta + color.blue);
3367 success_p = x_alloc_nearest_color (f, cmap, &new);
3369 else
3370 success_p = 1;
3371 *pixel = new.pixel;
3374 return success_p;
3378 /* Set up the foreground color for drawing relief lines of glyph
3379 string S. RELIEF is a pointer to a struct relief containing the GC
3380 with which lines will be drawn. Use a color that is FACTOR or
3381 DELTA lighter or darker than the relief's background which is found
3382 in S->f->output_data.x->relief_background. If such a color cannot
3383 be allocated, use DEFAULT_PIXEL, instead. */
3385 static void
3386 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3387 struct frame *f;
3388 struct relief *relief;
3389 double factor;
3390 int delta;
3391 unsigned long default_pixel;
3393 XGCValues xgcv;
3394 struct x_output *di = f->output_data.x;
3395 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3396 unsigned long pixel;
3397 unsigned long background = di->relief_background;
3398 Colormap cmap = FRAME_X_COLORMAP (f);
3399 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3400 Display *dpy = FRAME_X_DISPLAY (f);
3402 xgcv.graphics_exposures = False;
3403 xgcv.line_width = 1;
3405 /* Free previously allocated color. The color cell will be reused
3406 when it has been freed as many times as it was allocated, so this
3407 doesn't affect faces using the same colors. */
3408 if (relief->gc
3409 && relief->allocated_p)
3411 x_free_colors (f, &relief->pixel, 1);
3412 relief->allocated_p = 0;
3415 /* Allocate new color. */
3416 xgcv.foreground = default_pixel;
3417 pixel = background;
3418 if (dpyinfo->n_planes != 1
3419 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3421 relief->allocated_p = 1;
3422 xgcv.foreground = relief->pixel = pixel;
3425 if (relief->gc == 0)
3427 xgcv.stipple = dpyinfo->gray;
3428 mask |= GCStipple;
3429 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3431 else
3432 XChangeGC (dpy, relief->gc, mask, &xgcv);
3436 /* Set up colors for the relief lines around glyph string S. */
3438 static void
3439 x_setup_relief_colors (s)
3440 struct glyph_string *s;
3442 struct x_output *di = s->f->output_data.x;
3443 unsigned long color;
3445 if (s->face->use_box_color_for_shadows_p)
3446 color = s->face->box_color;
3447 else
3449 XGCValues xgcv;
3451 /* Get the background color of the face. */
3452 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3453 color = xgcv.background;
3456 if (di->white_relief.gc == 0
3457 || color != di->relief_background)
3459 di->relief_background = color;
3460 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3461 WHITE_PIX_DEFAULT (s->f));
3462 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3463 BLACK_PIX_DEFAULT (s->f));
3468 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3469 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3470 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3471 relief. LEFT_P non-zero means draw a relief on the left side of
3472 the rectangle. RIGHT_P non-zero means draw a relief on the right
3473 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3474 when drawing. */
3476 static void
3477 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3478 raised_p, left_p, right_p, clip_rect)
3479 struct frame *f;
3480 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3481 XRectangle *clip_rect;
3483 int i;
3484 GC gc;
3486 if (raised_p)
3487 gc = f->output_data.x->white_relief.gc;
3488 else
3489 gc = f->output_data.x->black_relief.gc;
3490 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3492 /* Top. */
3493 for (i = 0; i < width; ++i)
3494 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3495 left_x + i * left_p, top_y + i,
3496 right_x + 1 - i * right_p, top_y + i);
3498 /* Left. */
3499 if (left_p)
3500 for (i = 0; i < width; ++i)
3501 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3502 left_x + i, top_y + i, left_x + i, bottom_y - i);
3504 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3505 if (raised_p)
3506 gc = f->output_data.x->black_relief.gc;
3507 else
3508 gc = f->output_data.x->white_relief.gc;
3509 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3511 /* Bottom. */
3512 for (i = 0; i < width; ++i)
3513 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3514 left_x + i * left_p, bottom_y - i,
3515 right_x + 1 - i * right_p, bottom_y - i);
3517 /* Right. */
3518 if (right_p)
3519 for (i = 0; i < width; ++i)
3520 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3521 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3523 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3527 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3528 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3529 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3530 left side of the rectangle. RIGHT_P non-zero means draw a line
3531 on the right side of the rectangle. CLIP_RECT is the clipping
3532 rectangle to use when drawing. */
3534 static void
3535 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3536 left_p, right_p, clip_rect)
3537 struct glyph_string *s;
3538 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3539 XRectangle *clip_rect;
3541 XGCValues xgcv;
3543 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3544 XSetForeground (s->display, s->gc, s->face->box_color);
3545 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3547 /* Top. */
3548 XFillRectangle (s->display, s->window, s->gc,
3549 left_x, top_y, right_x - left_x, width);
3551 /* Left. */
3552 if (left_p)
3553 XFillRectangle (s->display, s->window, s->gc,
3554 left_x, top_y, width, bottom_y - top_y);
3556 /* Bottom. */
3557 XFillRectangle (s->display, s->window, s->gc,
3558 left_x, bottom_y - width, right_x - left_x, width);
3560 /* Right. */
3561 if (right_p)
3562 XFillRectangle (s->display, s->window, s->gc,
3563 right_x - width, top_y, width, bottom_y - top_y);
3565 XSetForeground (s->display, s->gc, xgcv.foreground);
3566 XSetClipMask (s->display, s->gc, None);
3570 /* Draw a box around glyph string S. */
3572 static void
3573 x_draw_glyph_string_box (s)
3574 struct glyph_string *s;
3576 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3577 int left_p, right_p;
3578 struct glyph *last_glyph;
3579 XRectangle clip_rect;
3581 last_x = window_box_right (s->w, s->area);
3582 if (s->row->full_width_p
3583 && !s->w->pseudo_window_p)
3585 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3586 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3587 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3590 /* The glyph that may have a right box line. */
3591 last_glyph = (s->cmp || s->img
3592 ? s->first_glyph
3593 : s->first_glyph + s->nchars - 1);
3595 width = s->face->box_line_width;
3596 raised_p = s->face->box == FACE_RAISED_BOX;
3597 left_x = s->x;
3598 right_x = ((s->row->full_width_p
3599 ? last_x - 1
3600 : min (last_x, s->x + s->background_width) - 1));
3601 top_y = s->y;
3602 bottom_y = top_y + s->height - 1;
3604 left_p = (s->first_glyph->left_box_line_p
3605 || (s->hl == DRAW_MOUSE_FACE
3606 && (s->prev == NULL
3607 || s->prev->hl != s->hl)));
3608 right_p = (last_glyph->right_box_line_p
3609 || (s->hl == DRAW_MOUSE_FACE
3610 && (s->next == NULL
3611 || s->next->hl != s->hl)));
3613 x_get_glyph_string_clip_rect (s, &clip_rect);
3615 if (s->face->box == FACE_SIMPLE_BOX)
3616 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3617 left_p, right_p, &clip_rect);
3618 else
3620 x_setup_relief_colors (s);
3621 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3622 width, raised_p, left_p, right_p, &clip_rect);
3627 /* Draw foreground of image glyph string S. */
3629 static void
3630 x_draw_image_foreground (s)
3631 struct glyph_string *s;
3633 int x;
3634 int y = s->ybase - image_ascent (s->img, s->face);
3636 /* If first glyph of S has a left box line, start drawing it to the
3637 right of that line. */
3638 if (s->face->box != FACE_NO_BOX
3639 && s->first_glyph->left_box_line_p)
3640 x = s->x + s->face->box_line_width;
3641 else
3642 x = s->x;
3644 /* If there is a margin around the image, adjust x- and y-position
3645 by that margin. */
3646 if (s->img->margin)
3648 x += s->img->margin;
3649 y += s->img->margin;
3652 if (s->img->pixmap)
3654 if (s->img->mask)
3656 /* We can't set both a clip mask and use XSetClipRectangles
3657 because the latter also sets a clip mask. We also can't
3658 trust on the shape extension to be available
3659 (XShapeCombineRegion). So, compute the rectangle to draw
3660 manually. */
3661 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3662 | GCFunction);
3663 XGCValues xgcv;
3664 XRectangle clip_rect, image_rect, r;
3666 xgcv.clip_mask = s->img->mask;
3667 xgcv.clip_x_origin = x;
3668 xgcv.clip_y_origin = y;
3669 xgcv.function = GXcopy;
3670 XChangeGC (s->display, s->gc, mask, &xgcv);
3672 x_get_glyph_string_clip_rect (s, &clip_rect);
3673 image_rect.x = x;
3674 image_rect.y = y;
3675 image_rect.width = s->img->width;
3676 image_rect.height = s->img->height;
3677 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3678 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3679 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3681 else
3683 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3684 0, 0, s->img->width, s->img->height, x, y);
3686 /* When the image has a mask, we can expect that at
3687 least part of a mouse highlight or a block cursor will
3688 be visible. If the image doesn't have a mask, make
3689 a block cursor visible by drawing a rectangle around
3690 the image. I believe it's looking better if we do
3691 nothing here for mouse-face. */
3692 if (s->hl == DRAW_CURSOR)
3693 XDrawRectangle (s->display, s->window, s->gc, x, y,
3694 s->img->width - 1, s->img->height - 1);
3697 else
3698 /* Draw a rectangle if image could not be loaded. */
3699 XDrawRectangle (s->display, s->window, s->gc, x, y,
3700 s->img->width - 1, s->img->height - 1);
3704 /* Draw a relief around the image glyph string S. */
3706 static void
3707 x_draw_image_relief (s)
3708 struct glyph_string *s;
3710 int x0, y0, x1, y1, thick, raised_p;
3711 XRectangle r;
3712 int x;
3713 int y = s->ybase - image_ascent (s->img, s->face);
3715 /* If first glyph of S has a left box line, start drawing it to the
3716 right of that line. */
3717 if (s->face->box != FACE_NO_BOX
3718 && s->first_glyph->left_box_line_p)
3719 x = s->x + s->face->box_line_width;
3720 else
3721 x = s->x;
3723 /* If there is a margin around the image, adjust x- and y-position
3724 by that margin. */
3725 if (s->img->margin)
3727 x += s->img->margin;
3728 y += s->img->margin;
3731 if (s->hl == DRAW_IMAGE_SUNKEN
3732 || s->hl == DRAW_IMAGE_RAISED)
3734 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3735 raised_p = s->hl == DRAW_IMAGE_RAISED;
3737 else
3739 thick = abs (s->img->relief);
3740 raised_p = s->img->relief > 0;
3743 x0 = x - thick;
3744 y0 = y - thick;
3745 x1 = x + s->img->width + thick - 1;
3746 y1 = y + s->img->height + thick - 1;
3748 x_setup_relief_colors (s);
3749 x_get_glyph_string_clip_rect (s, &r);
3750 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3754 /* Draw the foreground of image glyph string S to PIXMAP. */
3756 static void
3757 x_draw_image_foreground_1 (s, pixmap)
3758 struct glyph_string *s;
3759 Pixmap pixmap;
3761 int x;
3762 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3764 /* If first glyph of S has a left box line, start drawing it to the
3765 right of that line. */
3766 if (s->face->box != FACE_NO_BOX
3767 && s->first_glyph->left_box_line_p)
3768 x = s->face->box_line_width;
3769 else
3770 x = 0;
3772 /* If there is a margin around the image, adjust x- and y-position
3773 by that margin. */
3774 if (s->img->margin)
3776 x += s->img->margin;
3777 y += s->img->margin;
3780 if (s->img->pixmap)
3782 if (s->img->mask)
3784 /* We can't set both a clip mask and use XSetClipRectangles
3785 because the latter also sets a clip mask. We also can't
3786 trust on the shape extension to be available
3787 (XShapeCombineRegion). So, compute the rectangle to draw
3788 manually. */
3789 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3790 | GCFunction);
3791 XGCValues xgcv;
3793 xgcv.clip_mask = s->img->mask;
3794 xgcv.clip_x_origin = x;
3795 xgcv.clip_y_origin = y;
3796 xgcv.function = GXcopy;
3797 XChangeGC (s->display, s->gc, mask, &xgcv);
3799 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3800 0, 0, s->img->width, s->img->height, x, y);
3801 XSetClipMask (s->display, s->gc, None);
3803 else
3805 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3806 0, 0, s->img->width, s->img->height, x, y);
3808 /* When the image has a mask, we can expect that at
3809 least part of a mouse highlight or a block cursor will
3810 be visible. If the image doesn't have a mask, make
3811 a block cursor visible by drawing a rectangle around
3812 the image. I believe it's looking better if we do
3813 nothing here for mouse-face. */
3814 if (s->hl == DRAW_CURSOR)
3815 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3816 s->img->width - 1, s->img->height - 1);
3819 else
3820 /* Draw a rectangle if image could not be loaded. */
3821 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3822 s->img->width - 1, s->img->height - 1);
3826 /* Draw part of the background of glyph string S. X, Y, W, and H
3827 give the rectangle to draw. */
3829 static void
3830 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3831 struct glyph_string *s;
3832 int x, y, w, h;
3834 if (s->stippled_p)
3836 /* Fill background with a stipple pattern. */
3837 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3838 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3839 XSetFillStyle (s->display, s->gc, FillSolid);
3841 else
3842 x_clear_glyph_string_rect (s, x, y, w, h);
3846 /* Draw image glyph string S.
3848 s->y
3849 s->x +-------------------------
3850 | s->face->box
3852 | +-------------------------
3853 | | s->img->margin
3855 | | +-------------------
3856 | | | the image
3860 static void
3861 x_draw_image_glyph_string (s)
3862 struct glyph_string *s;
3864 int x, y;
3865 int box_line_width = s->face->box_line_width;
3866 int margin = s->img->margin;
3867 int height;
3868 Pixmap pixmap = None;
3870 height = s->height - 2 * box_line_width;
3872 /* Fill background with face under the image. Do it only if row is
3873 taller than image or if image has a clip mask to reduce
3874 flickering. */
3875 s->stippled_p = s->face->stipple != 0;
3876 if (height > s->img->height
3877 || margin
3878 || s->img->mask
3879 || s->img->pixmap == 0
3880 || s->width != s->background_width)
3882 if (box_line_width && s->first_glyph->left_box_line_p)
3883 x = s->x + box_line_width;
3884 else
3885 x = s->x;
3887 y = s->y + box_line_width;
3889 if (s->img->mask)
3891 /* Create a pixmap as large as the glyph string Fill it with
3892 the background color. Copy the image to it, using its
3893 mask. Copy the temporary pixmap to the display. */
3894 Screen *screen = FRAME_X_SCREEN (s->f);
3895 int depth = DefaultDepthOfScreen (screen);
3897 /* Create a pixmap as large as the glyph string. */
3898 pixmap = XCreatePixmap (s->display, s->window,
3899 s->background_width,
3900 s->height, depth);
3902 /* Don't clip in the following because we're working on the
3903 pixmap. */
3904 XSetClipMask (s->display, s->gc, None);
3906 /* Fill the pixmap with the background color/stipple. */
3907 if (s->stippled_p)
3909 /* Fill background with a stipple pattern. */
3910 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3911 XFillRectangle (s->display, pixmap, s->gc,
3912 0, 0, s->background_width, s->height);
3913 XSetFillStyle (s->display, s->gc, FillSolid);
3915 else
3917 XGCValues xgcv;
3918 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3919 &xgcv);
3920 XSetForeground (s->display, s->gc, xgcv.background);
3921 XFillRectangle (s->display, pixmap, s->gc,
3922 0, 0, s->background_width, s->height);
3923 XSetForeground (s->display, s->gc, xgcv.foreground);
3926 else
3927 /* Implementation idea: Is it possible to construct a mask?
3928 We could look at the color at the margins of the image, and
3929 say that this color is probably the background color of the
3930 image. */
3931 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3933 s->background_filled_p = 1;
3936 /* Draw the foreground. */
3937 if (pixmap != None)
3939 x_draw_image_foreground_1 (s, pixmap);
3940 x_set_glyph_string_clipping (s);
3941 XCopyArea (s->display, pixmap, s->window, s->gc,
3942 0, 0, s->background_width, s->height, s->x, s->y);
3943 XFreePixmap (s->display, pixmap);
3945 else
3946 x_draw_image_foreground (s);
3948 /* If we must draw a relief around the image, do it. */
3949 if (s->img->relief
3950 || s->hl == DRAW_IMAGE_RAISED
3951 || s->hl == DRAW_IMAGE_SUNKEN)
3952 x_draw_image_relief (s);
3956 /* Draw stretch glyph string S. */
3958 static void
3959 x_draw_stretch_glyph_string (s)
3960 struct glyph_string *s;
3962 xassert (s->first_glyph->type == STRETCH_GLYPH);
3963 s->stippled_p = s->face->stipple != 0;
3965 if (s->hl == DRAW_CURSOR
3966 && !x_stretch_cursor_p)
3968 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
3969 as wide as the stretch glyph. */
3970 int width = min (CANON_X_UNIT (s->f), s->background_width);
3972 /* Draw cursor. */
3973 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
3975 /* Clear rest using the GC of the original non-cursor face. */
3976 if (width < s->background_width)
3978 GC gc = s->face->gc;
3979 int x = s->x + width, y = s->y;
3980 int w = s->background_width - width, h = s->height;
3981 XRectangle r;
3983 x_get_glyph_string_clip_rect (s, &r);
3984 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
3986 if (s->face->stipple)
3988 /* Fill background with a stipple pattern. */
3989 XSetFillStyle (s->display, gc, FillOpaqueStippled);
3990 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3991 XSetFillStyle (s->display, gc, FillSolid);
3993 else
3995 XGCValues xgcv;
3996 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
3997 XSetForeground (s->display, gc, xgcv.background);
3998 XFillRectangle (s->display, s->window, gc, x, y, w, h);
3999 XSetForeground (s->display, gc, xgcv.foreground);
4003 else
4004 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4005 s->height);
4007 s->background_filled_p = 1;
4011 /* Draw glyph string S. */
4013 static void
4014 x_draw_glyph_string (s)
4015 struct glyph_string *s;
4017 /* If S draws into the background of its successor, draw the
4018 background of the successor first so that S can draw into it.
4019 This makes S->next use XDrawString instead of XDrawImageString. */
4020 if (s->next && s->right_overhang && !s->for_overlaps_p)
4022 xassert (s->next->img == NULL);
4023 x_set_glyph_string_gc (s->next);
4024 x_set_glyph_string_clipping (s->next);
4025 x_draw_glyph_string_background (s->next, 1);
4028 /* Set up S->gc, set clipping and draw S. */
4029 x_set_glyph_string_gc (s);
4030 x_set_glyph_string_clipping (s);
4032 switch (s->first_glyph->type)
4034 case IMAGE_GLYPH:
4035 x_draw_image_glyph_string (s);
4036 break;
4038 case STRETCH_GLYPH:
4039 x_draw_stretch_glyph_string (s);
4040 break;
4042 case CHAR_GLYPH:
4043 if (s->for_overlaps_p)
4044 s->background_filled_p = 1;
4045 else
4046 x_draw_glyph_string_background (s, 0);
4047 x_draw_glyph_string_foreground (s);
4048 break;
4050 case COMPOSITE_GLYPH:
4051 if (s->for_overlaps_p || s->gidx > 0)
4052 s->background_filled_p = 1;
4053 else
4054 x_draw_glyph_string_background (s, 1);
4055 x_draw_composite_glyph_string_foreground (s);
4056 break;
4058 default:
4059 abort ();
4062 if (!s->for_overlaps_p)
4064 /* Draw underline. */
4065 if (s->face->underline_p)
4067 unsigned long dy, h;
4069 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4070 h = 1;
4071 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4072 dy = s->height - h;
4074 if (s->face->underline_defaulted_p)
4075 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4076 s->width, h);
4077 else
4079 XGCValues xgcv;
4080 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4081 XSetForeground (s->display, s->gc, s->face->underline_color);
4082 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4083 s->width, h);
4084 XSetForeground (s->display, s->gc, xgcv.foreground);
4088 /* Draw overline. */
4089 if (s->face->overline_p)
4091 unsigned long dy = 0, h = 1;
4093 if (s->face->overline_color_defaulted_p)
4094 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4095 s->width, h);
4096 else
4098 XGCValues xgcv;
4099 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4100 XSetForeground (s->display, s->gc, s->face->overline_color);
4101 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4102 s->width, h);
4103 XSetForeground (s->display, s->gc, xgcv.foreground);
4107 /* Draw strike-through. */
4108 if (s->face->strike_through_p)
4110 unsigned long h = 1;
4111 unsigned long dy = (s->height - h) / 2;
4113 if (s->face->strike_through_color_defaulted_p)
4114 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4115 s->width, h);
4116 else
4118 XGCValues xgcv;
4119 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4120 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4121 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4122 s->width, h);
4123 XSetForeground (s->display, s->gc, xgcv.foreground);
4127 /* Draw relief. */
4128 if (s->face->box != FACE_NO_BOX)
4129 x_draw_glyph_string_box (s);
4132 /* Reset clipping. */
4133 XSetClipMask (s->display, s->gc, None);
4137 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4138 struct face **, int));
4141 /* Load glyph string S with a composition components specified by S->cmp.
4142 FACES is an array of faces for all components of this composition.
4143 S->gidx is the index of the first component for S.
4144 OVERLAPS_P non-zero means S should draw the foreground only, and
4145 use its lines physical height for clipping.
4147 Value is the index of a component not in S. */
4149 static int
4150 x_fill_composite_glyph_string (s, faces, overlaps_p)
4151 struct glyph_string *s;
4152 struct face **faces;
4153 int overlaps_p;
4155 int i;
4157 xassert (s);
4159 s->for_overlaps_p = overlaps_p;
4161 s->face = faces[s->gidx];
4162 s->font = s->face->font;
4163 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4165 /* For all glyphs of this composition, starting at the offset
4166 S->gidx, until we reach the end of the definition or encounter a
4167 glyph that requires the different face, add it to S. */
4168 ++s->nchars;
4169 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4170 ++s->nchars;
4172 /* All glyph strings for the same composition has the same width,
4173 i.e. the width set for the first component of the composition. */
4175 s->width = s->first_glyph->pixel_width;
4177 /* If the specified font could not be loaded, use the frame's
4178 default font, but record the fact that we couldn't load it in
4179 the glyph string so that we can draw rectangles for the
4180 characters of the glyph string. */
4181 if (s->font == NULL)
4183 s->font_not_found_p = 1;
4184 s->font = FRAME_FONT (s->f);
4187 /* Adjust base line for subscript/superscript text. */
4188 s->ybase += s->first_glyph->voffset;
4190 xassert (s->face && s->face->gc);
4192 /* This glyph string must always be drawn with 16-bit functions. */
4193 s->two_byte_p = 1;
4195 return s->gidx + s->nchars;
4199 /* Load glyph string S with a sequence characters.
4200 FACE_ID is the face id of the string. START is the index of the
4201 first glyph to consider, END is the index of the last + 1.
4202 OVERLAPS_P non-zero means S should draw the foreground only, and
4203 use its lines physical height for clipping.
4205 Value is the index of the first glyph not in S. */
4207 static int
4208 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4209 struct glyph_string *s;
4210 int face_id;
4211 int start, end, overlaps_p;
4213 struct glyph *glyph, *last;
4214 int voffset;
4215 int glyph_not_available_p;
4217 xassert (s->f == XFRAME (s->w->frame));
4218 xassert (s->nchars == 0);
4219 xassert (start >= 0 && end > start);
4221 s->for_overlaps_p = overlaps_p,
4222 glyph = s->row->glyphs[s->area] + start;
4223 last = s->row->glyphs[s->area] + end;
4224 voffset = glyph->voffset;
4226 glyph_not_available_p = glyph->glyph_not_available_p;
4228 while (glyph < last
4229 && glyph->type == CHAR_GLYPH
4230 && glyph->voffset == voffset
4231 /* Same face id implies same font, nowadays. */
4232 && glyph->face_id == face_id
4233 && glyph->glyph_not_available_p == glyph_not_available_p)
4235 int two_byte_p;
4237 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4238 s->char2b + s->nchars,
4239 &two_byte_p);
4240 s->two_byte_p = two_byte_p;
4241 ++s->nchars;
4242 xassert (s->nchars <= end - start);
4243 s->width += glyph->pixel_width;
4244 ++glyph;
4247 s->font = s->face->font;
4248 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4250 /* If the specified font could not be loaded, use the frame's font,
4251 but record the fact that we couldn't load it in
4252 S->font_not_found_p so that we can draw rectangles for the
4253 characters of the glyph string. */
4254 if (s->font == NULL || glyph_not_available_p)
4256 s->font_not_found_p = 1;
4257 s->font = FRAME_FONT (s->f);
4260 /* Adjust base line for subscript/superscript text. */
4261 s->ybase += voffset;
4263 xassert (s->face && s->face->gc);
4264 return glyph - s->row->glyphs[s->area];
4268 /* Fill glyph string S from image glyph S->first_glyph. */
4270 static void
4271 x_fill_image_glyph_string (s)
4272 struct glyph_string *s;
4274 xassert (s->first_glyph->type == IMAGE_GLYPH);
4275 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4276 xassert (s->img);
4277 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4278 s->font = s->face->font;
4279 s->width = s->first_glyph->pixel_width;
4281 /* Adjust base line for subscript/superscript text. */
4282 s->ybase += s->first_glyph->voffset;
4286 /* Fill glyph string S from stretch glyph S->first_glyph. */
4288 static void
4289 x_fill_stretch_glyph_string (s)
4290 struct glyph_string *s;
4292 xassert (s->first_glyph->type == STRETCH_GLYPH);
4293 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4294 s->font = s->face->font;
4295 s->width = s->first_glyph->pixel_width;
4297 /* Adjust base line for subscript/superscript text. */
4298 s->ybase += s->first_glyph->voffset;
4302 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4303 of XChar2b structures for S; it can't be allocated in
4304 x_init_glyph_string because it must be allocated via `alloca'. W
4305 is the window on which S is drawn. ROW and AREA are the glyph row
4306 and area within the row from which S is constructed. START is the
4307 index of the first glyph structure covered by S. HL is a
4308 face-override for drawing S. */
4310 static void
4311 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4312 struct glyph_string *s;
4313 XChar2b *char2b;
4314 struct window *w;
4315 struct glyph_row *row;
4316 enum glyph_row_area area;
4317 int start;
4318 enum draw_glyphs_face hl;
4320 bzero (s, sizeof *s);
4321 s->w = w;
4322 s->f = XFRAME (w->frame);
4323 s->display = FRAME_X_DISPLAY (s->f);
4324 s->window = FRAME_X_WINDOW (s->f);
4325 s->char2b = char2b;
4326 s->hl = hl;
4327 s->row = row;
4328 s->area = area;
4329 s->first_glyph = row->glyphs[area] + start;
4330 s->height = row->height;
4331 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4333 /* Display the internal border below the tool-bar window. */
4334 if (s->w == XWINDOW (s->f->tool_bar_window))
4335 s->y -= s->f->output_data.x->internal_border_width;
4337 s->ybase = s->y + row->ascent;
4341 /* Set background width of glyph string S. START is the index of the
4342 first glyph following S. LAST_X is the right-most x-position + 1
4343 in the drawing area. */
4345 static INLINE void
4346 x_set_glyph_string_background_width (s, start, last_x)
4347 struct glyph_string *s;
4348 int start;
4349 int last_x;
4351 /* If the face of this glyph string has to be drawn to the end of
4352 the drawing area, set S->extends_to_end_of_line_p. */
4353 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4355 if (start == s->row->used[s->area]
4356 && s->hl == DRAW_NORMAL_TEXT
4357 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4358 || s->face->background != default_face->background
4359 || s->face->stipple != default_face->stipple))
4360 s->extends_to_end_of_line_p = 1;
4362 /* If S extends its face to the end of the line, set its
4363 background_width to the distance to the right edge of the drawing
4364 area. */
4365 if (s->extends_to_end_of_line_p)
4366 s->background_width = last_x - s->x + 1;
4367 else
4368 s->background_width = s->width;
4372 /* Add a glyph string for a stretch glyph to the list of strings
4373 between HEAD and TAIL. START is the index of the stretch glyph in
4374 row area AREA of glyph row ROW. END is the index of the last glyph
4375 in that glyph row area. X is the current output position assigned
4376 to the new glyph string constructed. HL overrides that face of the
4377 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4378 is the right-most x-position of the drawing area. */
4380 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4381 and below -- keep them on one line. */
4382 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4383 do \
4385 s = (struct glyph_string *) alloca (sizeof *s); \
4386 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4387 x_fill_stretch_glyph_string (s); \
4388 x_append_glyph_string (&HEAD, &TAIL, s); \
4389 ++START; \
4390 s->x = (X); \
4392 while (0)
4395 /* Add a glyph string for an image glyph to the list of strings
4396 between HEAD and TAIL. START is the index of the image glyph in
4397 row area AREA of glyph row ROW. END is the index of the last glyph
4398 in that glyph row area. X is the current output position assigned
4399 to the new glyph string constructed. HL overrides that face of the
4400 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4401 is the right-most x-position of the drawing area. */
4403 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4404 do \
4406 s = (struct glyph_string *) alloca (sizeof *s); \
4407 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4408 x_fill_image_glyph_string (s); \
4409 x_append_glyph_string (&HEAD, &TAIL, s); \
4410 ++START; \
4411 s->x = (X); \
4413 while (0)
4416 /* Add a glyph string for a sequence of character glyphs to the list
4417 of strings between HEAD and TAIL. START is the index of the first
4418 glyph in row area AREA of glyph row ROW that is part of the new
4419 glyph string. END is the index of the last glyph in that glyph row
4420 area. X is the current output position assigned to the new glyph
4421 string constructed. HL overrides that face of the glyph; e.g. it
4422 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4423 right-most x-position of the drawing area. */
4425 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4426 do \
4428 int c, face_id; \
4429 XChar2b *char2b; \
4431 c = (ROW)->glyphs[AREA][START].u.ch; \
4432 face_id = (ROW)->glyphs[AREA][START].face_id; \
4434 s = (struct glyph_string *) alloca (sizeof *s); \
4435 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4436 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4437 x_append_glyph_string (&HEAD, &TAIL, s); \
4438 s->x = (X); \
4439 START = x_fill_glyph_string (s, face_id, START, END, \
4440 OVERLAPS_P); \
4442 while (0)
4445 /* Add a glyph string for a composite sequence to the list of strings
4446 between HEAD and TAIL. START is the index of the first glyph in
4447 row area AREA of glyph row ROW that is part of the new glyph
4448 string. END is the index of the last glyph in that glyph row area.
4449 X is the current output position assigned to the new glyph string
4450 constructed. HL overrides that face of the glyph; e.g. it is
4451 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4452 x-position of the drawing area. */
4454 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4455 do { \
4456 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4457 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4458 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4459 struct composition *cmp = composition_table[cmp_id]; \
4460 int glyph_len = cmp->glyph_len; \
4461 XChar2b *char2b; \
4462 struct face **faces; \
4463 struct glyph_string *first_s = NULL; \
4464 int n; \
4466 base_face = base_face->ascii_face; \
4467 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4468 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4469 /* At first, fill in `char2b' and `faces'. */ \
4470 for (n = 0; n < glyph_len; n++) \
4472 int c = COMPOSITION_GLYPH (cmp, n); \
4473 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4474 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4475 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4476 this_face_id, char2b + n, 1); \
4479 /* Make glyph_strings for each glyph sequence that is drawable by \
4480 the same face, and append them to HEAD/TAIL. */ \
4481 for (n = 0; n < cmp->glyph_len;) \
4483 s = (struct glyph_string *) alloca (sizeof *s); \
4484 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4485 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4486 s->cmp = cmp; \
4487 s->gidx = n; \
4488 s->x = (X); \
4490 if (n == 0) \
4491 first_s = s; \
4493 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4496 ++START; \
4497 s = first_s; \
4498 } while (0)
4501 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4502 of AREA of glyph row ROW on window W between indices START and END.
4503 HL overrides the face for drawing glyph strings, e.g. it is
4504 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4505 x-positions of the drawing area.
4507 This is an ugly monster macro construct because we must use alloca
4508 to allocate glyph strings (because x_draw_glyphs can be called
4509 asynchronously). */
4511 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4512 do \
4514 HEAD = TAIL = NULL; \
4515 while (START < END) \
4517 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4518 switch (first_glyph->type) \
4520 case CHAR_GLYPH: \
4521 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4522 TAIL, HL, X, LAST_X, \
4523 OVERLAPS_P); \
4524 break; \
4526 case COMPOSITE_GLYPH: \
4527 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4528 HEAD, TAIL, HL, X, LAST_X,\
4529 OVERLAPS_P); \
4530 break; \
4532 case STRETCH_GLYPH: \
4533 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4534 HEAD, TAIL, HL, X, LAST_X); \
4535 break; \
4537 case IMAGE_GLYPH: \
4538 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4539 TAIL, HL, X, LAST_X); \
4540 break; \
4542 default: \
4543 abort (); \
4546 x_set_glyph_string_background_width (s, START, LAST_X); \
4547 (X) += s->width; \
4550 while (0)
4553 /* Draw glyphs between START and END in AREA of ROW on window W,
4554 starting at x-position X. X is relative to AREA in W. HL is a
4555 face-override with the following meaning:
4557 DRAW_NORMAL_TEXT draw normally
4558 DRAW_CURSOR draw in cursor face
4559 DRAW_MOUSE_FACE draw in mouse face.
4560 DRAW_INVERSE_VIDEO draw in mode line face
4561 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4562 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4564 If REAL_START is non-null, return in *REAL_START the real starting
4565 position for display. This can be different from START in case
4566 overlapping glyphs must be displayed. If REAL_END is non-null,
4567 return in *REAL_END the real end position for display. This can be
4568 different from END in case overlapping glyphs must be displayed.
4570 If OVERLAPS_P is non-zero, draw only the foreground of characters
4571 and clip to the physical height of ROW.
4573 Value is the x-position reached, relative to AREA of W. */
4575 static int
4576 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4577 overlaps_p)
4578 struct window *w;
4579 int x;
4580 struct glyph_row *row;
4581 enum glyph_row_area area;
4582 int start, end;
4583 enum draw_glyphs_face hl;
4584 int *real_start, *real_end;
4585 int overlaps_p;
4587 struct glyph_string *head, *tail;
4588 struct glyph_string *s;
4589 int last_x, area_width;
4590 int x_reached;
4591 int i, j;
4593 /* Let's rather be paranoid than getting a SEGV. */
4594 start = max (0, start);
4595 end = min (end, row->used[area]);
4596 if (real_start)
4597 *real_start = start;
4598 if (real_end)
4599 *real_end = end;
4601 /* Translate X to frame coordinates. Set last_x to the right
4602 end of the drawing area. */
4603 if (row->full_width_p)
4605 /* X is relative to the left edge of W, without scroll bars
4606 or flag areas. */
4607 struct frame *f = XFRAME (w->frame);
4608 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4609 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4611 x += window_left_x;
4612 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4613 last_x = window_left_x + area_width;
4615 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4617 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4618 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4619 last_x += width;
4620 else
4621 x -= width;
4624 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4625 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4627 else
4629 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4630 area_width = window_box_width (w, area);
4631 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4634 /* Build a doubly-linked list of glyph_string structures between
4635 head and tail from what we have to draw. Note that the macro
4636 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4637 the reason we use a separate variable `i'. */
4638 i = start;
4639 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4640 overlaps_p);
4641 if (tail)
4642 x_reached = tail->x + tail->background_width;
4643 else
4644 x_reached = x;
4646 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4647 the row, redraw some glyphs in front or following the glyph
4648 strings built above. */
4649 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4651 int dummy_x = 0;
4652 struct glyph_string *h, *t;
4654 /* Compute overhangs for all glyph strings. */
4655 for (s = head; s; s = s->next)
4656 x_compute_glyph_string_overhangs (s);
4658 /* Prepend glyph strings for glyphs in front of the first glyph
4659 string that are overwritten because of the first glyph
4660 string's left overhang. The background of all strings
4661 prepended must be drawn because the first glyph string
4662 draws over it. */
4663 i = x_left_overwritten (head);
4664 if (i >= 0)
4666 j = i;
4667 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4668 DRAW_NORMAL_TEXT, dummy_x, last_x,
4669 overlaps_p);
4670 start = i;
4671 if (real_start)
4672 *real_start = start;
4673 x_compute_overhangs_and_x (t, head->x, 1);
4674 x_prepend_glyph_string_lists (&head, &tail, h, t);
4677 /* Prepend glyph strings for glyphs in front of the first glyph
4678 string that overwrite that glyph string because of their
4679 right overhang. For these strings, only the foreground must
4680 be drawn, because it draws over the glyph string at `head'.
4681 The background must not be drawn because this would overwrite
4682 right overhangs of preceding glyphs for which no glyph
4683 strings exist. */
4684 i = x_left_overwriting (head);
4685 if (i >= 0)
4687 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4688 DRAW_NORMAL_TEXT, dummy_x, last_x,
4689 overlaps_p);
4690 for (s = h; s; s = s->next)
4691 s->background_filled_p = 1;
4692 if (real_start)
4693 *real_start = i;
4694 x_compute_overhangs_and_x (t, head->x, 1);
4695 x_prepend_glyph_string_lists (&head, &tail, h, t);
4698 /* Append glyphs strings for glyphs following the last glyph
4699 string tail that are overwritten by tail. The background of
4700 these strings has to be drawn because tail's foreground draws
4701 over it. */
4702 i = x_right_overwritten (tail);
4703 if (i >= 0)
4705 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4706 DRAW_NORMAL_TEXT, x, last_x,
4707 overlaps_p);
4708 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4709 x_append_glyph_string_lists (&head, &tail, h, t);
4710 if (real_end)
4711 *real_end = i;
4714 /* Append glyph strings for glyphs following the last glyph
4715 string tail that overwrite tail. The foreground of such
4716 glyphs has to be drawn because it writes into the background
4717 of tail. The background must not be drawn because it could
4718 paint over the foreground of following glyphs. */
4719 i = x_right_overwriting (tail);
4720 if (i >= 0)
4722 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4723 DRAW_NORMAL_TEXT, x, last_x,
4724 overlaps_p);
4725 for (s = h; s; s = s->next)
4726 s->background_filled_p = 1;
4727 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4728 x_append_glyph_string_lists (&head, &tail, h, t);
4729 if (real_end)
4730 *real_end = i;
4734 /* Draw all strings. */
4735 for (s = head; s; s = s->next)
4736 x_draw_glyph_string (s);
4738 /* Value is the x-position up to which drawn, relative to AREA of W.
4739 This doesn't include parts drawn because of overhangs. */
4740 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4741 if (!row->full_width_p)
4743 if (area > LEFT_MARGIN_AREA)
4744 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4745 if (area > TEXT_AREA)
4746 x_reached -= window_box_width (w, TEXT_AREA);
4748 return x_reached;
4752 /* Fix the display of area AREA of overlapping row ROW in window W. */
4754 static void
4755 x_fix_overlapping_area (w, row, area)
4756 struct window *w;
4757 struct glyph_row *row;
4758 enum glyph_row_area area;
4760 int i, x;
4762 BLOCK_INPUT;
4764 if (area == LEFT_MARGIN_AREA)
4765 x = 0;
4766 else if (area == TEXT_AREA)
4767 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4768 else
4769 x = (window_box_width (w, LEFT_MARGIN_AREA)
4770 + window_box_width (w, TEXT_AREA));
4772 for (i = 0; i < row->used[area];)
4774 if (row->glyphs[area][i].overlaps_vertically_p)
4776 int start = i, start_x = x;
4780 x += row->glyphs[area][i].pixel_width;
4781 ++i;
4783 while (i < row->used[area]
4784 && row->glyphs[area][i].overlaps_vertically_p);
4786 x_draw_glyphs (w, start_x, row, area, start, i,
4787 (row->inverse_p
4788 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4789 NULL, NULL, 1);
4791 else
4793 x += row->glyphs[area][i].pixel_width;
4794 ++i;
4798 UNBLOCK_INPUT;
4802 /* Output LEN glyphs starting at START at the nominal cursor position.
4803 Advance the nominal cursor over the text. The global variable
4804 updated_window contains the window being updated, updated_row is
4805 the glyph row being updated, and updated_area is the area of that
4806 row being updated. */
4808 static void
4809 x_write_glyphs (start, len)
4810 struct glyph *start;
4811 int len;
4813 int x, hpos, real_start, real_end;
4815 xassert (updated_window && updated_row);
4816 BLOCK_INPUT;
4818 /* Write glyphs. */
4820 hpos = start - updated_row->glyphs[updated_area];
4821 x = x_draw_glyphs (updated_window, output_cursor.x,
4822 updated_row, updated_area,
4823 hpos, hpos + len,
4824 (updated_row->inverse_p
4825 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4826 &real_start, &real_end, 0);
4828 /* If we drew over the cursor, note that it is not visible any more. */
4829 note_overwritten_text_cursor (updated_window, real_start,
4830 real_end - real_start);
4832 UNBLOCK_INPUT;
4834 /* Advance the output cursor. */
4835 output_cursor.hpos += len;
4836 output_cursor.x = x;
4840 /* Insert LEN glyphs from START at the nominal cursor position. */
4842 static void
4843 x_insert_glyphs (start, len)
4844 struct glyph *start;
4845 register int len;
4847 struct frame *f;
4848 struct window *w;
4849 int line_height, shift_by_width, shifted_region_width;
4850 struct glyph_row *row;
4851 struct glyph *glyph;
4852 int frame_x, frame_y, hpos, real_start, real_end;
4854 xassert (updated_window && updated_row);
4855 BLOCK_INPUT;
4856 w = updated_window;
4857 f = XFRAME (WINDOW_FRAME (w));
4859 /* Get the height of the line we are in. */
4860 row = updated_row;
4861 line_height = row->height;
4863 /* Get the width of the glyphs to insert. */
4864 shift_by_width = 0;
4865 for (glyph = start; glyph < start + len; ++glyph)
4866 shift_by_width += glyph->pixel_width;
4868 /* Get the width of the region to shift right. */
4869 shifted_region_width = (window_box_width (w, updated_area)
4870 - output_cursor.x
4871 - shift_by_width);
4873 /* Shift right. */
4874 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4875 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4876 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4877 f->output_data.x->normal_gc,
4878 frame_x, frame_y,
4879 shifted_region_width, line_height,
4880 frame_x + shift_by_width, frame_y);
4882 /* Write the glyphs. */
4883 hpos = start - row->glyphs[updated_area];
4884 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4885 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4886 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4888 /* Advance the output cursor. */
4889 output_cursor.hpos += len;
4890 output_cursor.x += shift_by_width;
4891 UNBLOCK_INPUT;
4895 /* Delete N glyphs at the nominal cursor position. Not implemented
4896 for X frames. */
4898 static void
4899 x_delete_glyphs (n)
4900 register int n;
4902 abort ();
4906 /* Erase the current text line from the nominal cursor position
4907 (inclusive) to pixel column TO_X (exclusive). The idea is that
4908 everything from TO_X onward is already erased.
4910 TO_X is a pixel position relative to updated_area of
4911 updated_window. TO_X == -1 means clear to the end of this area. */
4913 static void
4914 x_clear_end_of_line (to_x)
4915 int to_x;
4917 struct frame *f;
4918 struct window *w = updated_window;
4919 int max_x, min_y, max_y;
4920 int from_x, from_y, to_y;
4922 xassert (updated_window && updated_row);
4923 f = XFRAME (w->frame);
4925 if (updated_row->full_width_p)
4927 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
4928 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
4929 && !w->pseudo_window_p)
4930 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4932 else
4933 max_x = window_box_width (w, updated_area);
4934 max_y = window_text_bottom_y (w);
4936 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
4937 of window. For TO_X > 0, truncate to end of drawing area. */
4938 if (to_x == 0)
4939 return;
4940 else if (to_x < 0)
4941 to_x = max_x;
4942 else
4943 to_x = min (to_x, max_x);
4945 to_y = min (max_y, output_cursor.y + updated_row->height);
4947 /* Notice if the cursor will be cleared by this operation. */
4948 if (!updated_row->full_width_p)
4949 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
4951 from_x = output_cursor.x;
4953 /* Translate to frame coordinates. */
4954 if (updated_row->full_width_p)
4956 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
4957 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
4959 else
4961 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
4962 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
4965 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
4966 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
4967 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
4969 /* Prevent inadvertently clearing to end of the X window. */
4970 if (to_x > from_x && to_y > from_y)
4972 BLOCK_INPUT;
4973 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
4974 from_x, from_y, to_x - from_x, to_y - from_y,
4975 False);
4976 UNBLOCK_INPUT;
4981 /* Clear entire frame. If updating_frame is non-null, clear that
4982 frame. Otherwise clear the selected frame. */
4984 static void
4985 x_clear_frame ()
4987 struct frame *f;
4989 if (updating_frame)
4990 f = updating_frame;
4991 else
4992 f = SELECTED_FRAME ();
4994 /* Clearing the frame will erase any cursor, so mark them all as no
4995 longer visible. */
4996 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
4997 output_cursor.hpos = output_cursor.vpos = 0;
4998 output_cursor.x = -1;
5000 /* We don't set the output cursor here because there will always
5001 follow an explicit cursor_to. */
5002 BLOCK_INPUT;
5003 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5005 /* We have to clear the scroll bars, too. If we have changed
5006 colors or something like that, then they should be notified. */
5007 x_scroll_bar_clear (f);
5009 XFlush (FRAME_X_DISPLAY (f));
5010 UNBLOCK_INPUT;
5015 /* Invert the middle quarter of the frame for .15 sec. */
5017 /* We use the select system call to do the waiting, so we have to make
5018 sure it's available. If it isn't, we just won't do visual bells. */
5020 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5023 /* Subtract the `struct timeval' values X and Y, storing the result in
5024 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5026 static int
5027 timeval_subtract (result, x, y)
5028 struct timeval *result, x, y;
5030 /* Perform the carry for the later subtraction by updating y. This
5031 is safer because on some systems the tv_sec member is unsigned. */
5032 if (x.tv_usec < y.tv_usec)
5034 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5035 y.tv_usec -= 1000000 * nsec;
5036 y.tv_sec += nsec;
5039 if (x.tv_usec - y.tv_usec > 1000000)
5041 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5042 y.tv_usec += 1000000 * nsec;
5043 y.tv_sec -= nsec;
5046 /* Compute the time remaining to wait. tv_usec is certainly
5047 positive. */
5048 result->tv_sec = x.tv_sec - y.tv_sec;
5049 result->tv_usec = x.tv_usec - y.tv_usec;
5051 /* Return indication of whether the result should be considered
5052 negative. */
5053 return x.tv_sec < y.tv_sec;
5056 void
5057 XTflash (f)
5058 struct frame *f;
5060 BLOCK_INPUT;
5063 GC gc;
5065 /* Create a GC that will use the GXxor function to flip foreground
5066 pixels into background pixels. */
5068 XGCValues values;
5070 values.function = GXxor;
5071 values.foreground = (f->output_data.x->foreground_pixel
5072 ^ f->output_data.x->background_pixel);
5074 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5075 GCFunction | GCForeground, &values);
5079 /* Get the height not including a menu bar widget. */
5080 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5081 /* Height of each line to flash. */
5082 int flash_height = FRAME_LINE_HEIGHT (f);
5083 /* These will be the left and right margins of the rectangles. */
5084 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5085 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5087 int width;
5089 /* Don't flash the area between a scroll bar and the frame
5090 edge it is next to. */
5091 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5093 case vertical_scroll_bar_left:
5094 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5095 break;
5097 case vertical_scroll_bar_right:
5098 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5099 break;
5101 default:
5102 break;
5105 width = flash_right - flash_left;
5107 /* If window is tall, flash top and bottom line. */
5108 if (height > 3 * FRAME_LINE_HEIGHT (f))
5110 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5111 flash_left,
5112 (FRAME_INTERNAL_BORDER_WIDTH (f)
5113 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5114 width, flash_height);
5115 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5116 flash_left,
5117 (height - flash_height
5118 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5119 width, flash_height);
5121 else
5122 /* If it is short, flash it all. */
5123 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5124 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5125 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5127 x_flush (f);
5130 struct timeval wakeup;
5132 EMACS_GET_TIME (wakeup);
5134 /* Compute time to wait until, propagating carry from usecs. */
5135 wakeup.tv_usec += 150000;
5136 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5137 wakeup.tv_usec %= 1000000;
5139 /* Keep waiting until past the time wakeup. */
5140 while (1)
5142 struct timeval timeout;
5144 EMACS_GET_TIME (timeout);
5146 /* In effect, timeout = wakeup - timeout.
5147 Break if result would be negative. */
5148 if (timeval_subtract (&timeout, wakeup, timeout))
5149 break;
5151 /* Try to wait that long--but we might wake up sooner. */
5152 select (0, NULL, NULL, NULL, &timeout);
5156 /* If window is tall, flash top and bottom line. */
5157 if (height > 3 * FRAME_LINE_HEIGHT (f))
5159 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5160 flash_left,
5161 (FRAME_INTERNAL_BORDER_WIDTH (f)
5162 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5163 width, flash_height);
5164 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5165 flash_left,
5166 (height - flash_height
5167 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5168 width, flash_height);
5170 else
5171 /* If it is short, flash it all. */
5172 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5173 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5174 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5176 XFreeGC (FRAME_X_DISPLAY (f), gc);
5177 x_flush (f);
5181 UNBLOCK_INPUT;
5184 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5187 /* Make audible bell. */
5189 void
5190 XTring_bell ()
5192 struct frame *f = SELECTED_FRAME ();
5194 if (FRAME_X_DISPLAY (f))
5196 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5197 if (visible_bell)
5198 XTflash (f);
5199 else
5200 #endif
5202 BLOCK_INPUT;
5203 XBell (FRAME_X_DISPLAY (f), 0);
5204 XFlush (FRAME_X_DISPLAY (f));
5205 UNBLOCK_INPUT;
5211 /* Specify how many text lines, from the top of the window,
5212 should be affected by insert-lines and delete-lines operations.
5213 This, and those operations, are used only within an update
5214 that is bounded by calls to x_update_begin and x_update_end. */
5216 static void
5217 XTset_terminal_window (n)
5218 register int n;
5220 /* This function intentionally left blank. */
5225 /***********************************************************************
5226 Line Dance
5227 ***********************************************************************/
5229 /* Perform an insert-lines or delete-lines operation, inserting N
5230 lines or deleting -N lines at vertical position VPOS. */
5232 static void
5233 x_ins_del_lines (vpos, n)
5234 int vpos, n;
5236 abort ();
5240 /* Scroll part of the display as described by RUN. */
5242 static void
5243 x_scroll_run (w, run)
5244 struct window *w;
5245 struct run *run;
5247 struct frame *f = XFRAME (w->frame);
5248 int x, y, width, height, from_y, to_y, bottom_y;
5250 /* Get frame-relative bounding box of the text display area of W,
5251 without mode lines. Include in this box the flags areas to the
5252 left and right of W. */
5253 window_box (w, -1, &x, &y, &width, &height);
5254 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5255 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5257 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5258 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5259 bottom_y = y + height;
5261 if (to_y < from_y)
5263 /* Scrolling up. Make sure we don't copy part of the mode
5264 line at the bottom. */
5265 if (from_y + run->height > bottom_y)
5266 height = bottom_y - from_y;
5267 else
5268 height = run->height;
5270 else
5272 /* Scolling down. Make sure we don't copy over the mode line.
5273 at the bottom. */
5274 if (to_y + run->height > bottom_y)
5275 height = bottom_y - to_y;
5276 else
5277 height = run->height;
5280 BLOCK_INPUT;
5282 /* Cursor off. Will be switched on again in x_update_window_end. */
5283 updated_window = w;
5284 x_clear_cursor (w);
5286 XCopyArea (FRAME_X_DISPLAY (f),
5287 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5288 f->output_data.x->normal_gc,
5289 x, from_y,
5290 width, height,
5291 x, to_y);
5293 UNBLOCK_INPUT;
5298 /***********************************************************************
5299 Exposure Events
5300 ***********************************************************************/
5302 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5303 corner of the exposed rectangle. W and H are width and height of
5304 the exposed area. All are pixel values. W or H zero means redraw
5305 the entire frame. */
5307 static void
5308 expose_frame (f, x, y, w, h)
5309 struct frame *f;
5310 int x, y, w, h;
5312 XRectangle r;
5314 TRACE ((stderr, "expose_frame "));
5316 /* No need to redraw if frame will be redrawn soon. */
5317 if (FRAME_GARBAGED_P (f))
5319 TRACE ((stderr, " garbaged\n"));
5320 return;
5323 /* If basic faces haven't been realized yet, there is no point in
5324 trying to redraw anything. This can happen when we get an expose
5325 event while Emacs is starting, e.g. by moving another window. */
5326 if (FRAME_FACE_CACHE (f) == NULL
5327 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5329 TRACE ((stderr, " no faces\n"));
5330 return;
5333 if (w == 0 || h == 0)
5335 r.x = r.y = 0;
5336 r.width = CANON_X_UNIT (f) * f->width;
5337 r.height = CANON_Y_UNIT (f) * f->height;
5339 else
5341 r.x = x;
5342 r.y = y;
5343 r.width = w;
5344 r.height = h;
5347 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5348 expose_window_tree (XWINDOW (f->root_window), &r);
5350 if (WINDOWP (f->tool_bar_window))
5352 struct window *w = XWINDOW (f->tool_bar_window);
5353 XRectangle window_rect;
5354 XRectangle intersection_rect;
5355 int window_x, window_y, window_width, window_height;
5358 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5359 window_rect.x = window_x;
5360 window_rect.y = window_y;
5361 window_rect.width = window_width;
5362 window_rect.height = window_height;
5364 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5365 expose_window (w, &intersection_rect);
5368 #ifndef USE_X_TOOLKIT
5369 if (WINDOWP (f->menu_bar_window))
5371 struct window *w = XWINDOW (f->menu_bar_window);
5372 XRectangle window_rect;
5373 XRectangle intersection_rect;
5374 int window_x, window_y, window_width, window_height;
5377 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5378 window_rect.x = window_x;
5379 window_rect.y = window_y;
5380 window_rect.width = window_width;
5381 window_rect.height = window_height;
5383 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5384 expose_window (w, &intersection_rect);
5386 #endif /* not USE_X_TOOLKIT */
5390 /* Redraw (parts) of all windows in the window tree rooted at W that
5391 intersect R. R contains frame pixel coordinates. */
5393 static void
5394 expose_window_tree (w, r)
5395 struct window *w;
5396 XRectangle *r;
5398 while (w)
5400 if (!NILP (w->hchild))
5401 expose_window_tree (XWINDOW (w->hchild), r);
5402 else if (!NILP (w->vchild))
5403 expose_window_tree (XWINDOW (w->vchild), r);
5404 else
5406 XRectangle window_rect;
5407 XRectangle intersection_rect;
5408 struct frame *f = XFRAME (w->frame);
5409 int window_x, window_y, window_width, window_height;
5411 /* Frame-relative pixel rectangle of W. */
5412 window_box (w, -1, &window_x, &window_y, &window_width,
5413 &window_height);
5414 window_rect.x
5415 = (window_x
5416 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5417 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5418 window_rect.y = window_y;
5419 window_rect.width
5420 = (window_width
5421 + FRAME_X_FLAGS_AREA_WIDTH (f)
5422 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5423 window_rect.height
5424 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5426 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5427 expose_window (w, &intersection_rect);
5430 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5435 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5436 which intersects rectangle R. R is in window-relative coordinates. */
5438 static void
5439 expose_area (w, row, r, area)
5440 struct window *w;
5441 struct glyph_row *row;
5442 XRectangle *r;
5443 enum glyph_row_area area;
5445 int x;
5446 struct glyph *first = row->glyphs[area];
5447 struct glyph *end = row->glyphs[area] + row->used[area];
5448 struct glyph *last;
5449 int first_x;
5451 /* Set x to the window-relative start position for drawing glyphs of
5452 AREA. The first glyph of the text area can be partially visible.
5453 The first glyphs of other areas cannot. */
5454 if (area == LEFT_MARGIN_AREA)
5455 x = 0;
5456 else if (area == TEXT_AREA)
5457 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5458 else
5459 x = (window_box_width (w, LEFT_MARGIN_AREA)
5460 + window_box_width (w, TEXT_AREA));
5462 if (area == TEXT_AREA && row->fill_line_p)
5463 /* If row extends face to end of line write the whole line. */
5464 x_draw_glyphs (w, x, row, area,
5465 0, row->used[area],
5466 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5467 NULL, NULL, 0);
5468 else
5470 /* Find the first glyph that must be redrawn. */
5471 while (first < end
5472 && x + first->pixel_width < r->x)
5474 x += first->pixel_width;
5475 ++first;
5478 /* Find the last one. */
5479 last = first;
5480 first_x = x;
5481 while (last < end
5482 && x < r->x + r->width)
5484 x += last->pixel_width;
5485 ++last;
5488 /* Repaint. */
5489 if (last > first)
5490 x_draw_glyphs (w, first_x, row, area,
5491 first - row->glyphs[area],
5492 last - row->glyphs[area],
5493 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5494 NULL, NULL, 0);
5499 /* Redraw the parts of the glyph row ROW on window W intersecting
5500 rectangle R. R is in window-relative coordinates. */
5502 static void
5503 expose_line (w, row, r)
5504 struct window *w;
5505 struct glyph_row *row;
5506 XRectangle *r;
5508 xassert (row->enabled_p);
5510 if (row->mode_line_p || w->pseudo_window_p)
5511 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5512 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5513 NULL, NULL, 0);
5514 else
5516 if (row->used[LEFT_MARGIN_AREA])
5517 expose_area (w, row, r, LEFT_MARGIN_AREA);
5518 if (row->used[TEXT_AREA])
5519 expose_area (w, row, r, TEXT_AREA);
5520 if (row->used[RIGHT_MARGIN_AREA])
5521 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5522 x_draw_row_bitmaps (w, row);
5527 /* Return non-zero if W's cursor intersects rectangle R. */
5529 static int
5530 x_phys_cursor_in_rect_p (w, r)
5531 struct window *w;
5532 XRectangle *r;
5534 XRectangle cr, result;
5535 struct glyph *cursor_glyph;
5537 cursor_glyph = get_phys_cursor_glyph (w);
5538 if (cursor_glyph)
5540 cr.x = w->phys_cursor.x;
5541 cr.y = w->phys_cursor.y;
5542 cr.width = cursor_glyph->pixel_width;
5543 cr.height = w->phys_cursor_height;
5544 return x_intersect_rectangles (&cr, r, &result);
5546 else
5547 return 0;
5551 /* Redraw a rectangle of window W. R is a rectangle in window
5552 relative coordinates. Call this function with input blocked. */
5554 static void
5555 expose_window (w, r)
5556 struct window *w;
5557 XRectangle *r;
5559 struct glyph_row *row;
5560 int y;
5561 int yb = window_text_bottom_y (w);
5562 int cursor_cleared_p;
5564 /* If window is not yet fully initialized, do nothing. This can
5565 happen when toolkit scroll bars are used and a window is split.
5566 Reconfiguring the scroll bar will generate an expose for a newly
5567 created window. */
5568 if (w->current_matrix == NULL)
5569 return;
5571 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5572 r->x, r->y, r->width, r->height));
5574 /* Convert to window coordinates. */
5575 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5576 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5578 /* Turn off the cursor. */
5579 if (!w->pseudo_window_p
5580 && x_phys_cursor_in_rect_p (w, r))
5582 x_clear_cursor (w);
5583 cursor_cleared_p = 1;
5585 else
5586 cursor_cleared_p = 0;
5588 /* Find the first row intersecting the rectangle R. */
5589 row = w->current_matrix->rows;
5590 y = 0;
5591 while (row->enabled_p
5592 && y < yb
5593 && y + row->height < r->y)
5595 y += row->height;
5596 ++row;
5599 /* Display the text in the rectangle, one text line at a time. */
5600 while (row->enabled_p
5601 && y < yb
5602 && y < r->y + r->height)
5604 expose_line (w, row, r);
5605 y += row->height;
5606 ++row;
5609 /* Display the mode line if there is one. */
5610 if (WINDOW_WANTS_MODELINE_P (w)
5611 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5612 row->enabled_p)
5613 && row->y < r->y + r->height)
5614 expose_line (w, row, r);
5616 if (!w->pseudo_window_p)
5618 /* Draw border between windows. */
5619 x_draw_vertical_border (w);
5621 /* Turn the cursor on again. */
5622 if (cursor_cleared_p)
5623 x_update_window_cursor (w, 1);
5628 /* Determine the intersection of two rectangles R1 and R2. Return
5629 the intersection in *RESULT. Value is non-zero if RESULT is not
5630 empty. */
5632 static int
5633 x_intersect_rectangles (r1, r2, result)
5634 XRectangle *r1, *r2, *result;
5636 XRectangle *left, *right;
5637 XRectangle *upper, *lower;
5638 int intersection_p = 0;
5640 /* Rearrange so that R1 is the left-most rectangle. */
5641 if (r1->x < r2->x)
5642 left = r1, right = r2;
5643 else
5644 left = r2, right = r1;
5646 /* X0 of the intersection is right.x0, if this is inside R1,
5647 otherwise there is no intersection. */
5648 if (right->x <= left->x + left->width)
5650 result->x = right->x;
5652 /* The right end of the intersection is the minimum of the
5653 the right ends of left and right. */
5654 result->width = (min (left->x + left->width, right->x + right->width)
5655 - result->x);
5657 /* Same game for Y. */
5658 if (r1->y < r2->y)
5659 upper = r1, lower = r2;
5660 else
5661 upper = r2, lower = r1;
5663 /* The upper end of the intersection is lower.y0, if this is inside
5664 of upper. Otherwise, there is no intersection. */
5665 if (lower->y <= upper->y + upper->height)
5667 result->y = lower->y;
5669 /* The lower end of the intersection is the minimum of the lower
5670 ends of upper and lower. */
5671 result->height = (min (lower->y + lower->height,
5672 upper->y + upper->height)
5673 - result->y);
5674 intersection_p = 1;
5678 return intersection_p;
5685 static void
5686 frame_highlight (f)
5687 struct frame *f;
5689 /* We used to only do this if Vx_no_window_manager was non-nil, but
5690 the ICCCM (section 4.1.6) says that the window's border pixmap
5691 and border pixel are window attributes which are "private to the
5692 client", so we can always change it to whatever we want. */
5693 BLOCK_INPUT;
5694 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5695 f->output_data.x->border_pixel);
5696 UNBLOCK_INPUT;
5697 x_update_cursor (f, 1);
5700 static void
5701 frame_unhighlight (f)
5702 struct frame *f;
5704 /* We used to only do this if Vx_no_window_manager was non-nil, but
5705 the ICCCM (section 4.1.6) says that the window's border pixmap
5706 and border pixel are window attributes which are "private to the
5707 client", so we can always change it to whatever we want. */
5708 BLOCK_INPUT;
5709 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5710 f->output_data.x->border_tile);
5711 UNBLOCK_INPUT;
5712 x_update_cursor (f, 1);
5715 /* The focus has changed. Update the frames as necessary to reflect
5716 the new situation. Note that we can't change the selected frame
5717 here, because the Lisp code we are interrupting might become confused.
5718 Each event gets marked with the frame in which it occurred, so the
5719 Lisp code can tell when the switch took place by examining the events. */
5721 static void
5722 x_new_focus_frame (dpyinfo, frame)
5723 struct x_display_info *dpyinfo;
5724 struct frame *frame;
5726 struct frame *old_focus = dpyinfo->x_focus_frame;
5728 if (frame != dpyinfo->x_focus_frame)
5730 /* Set this before calling other routines, so that they see
5731 the correct value of x_focus_frame. */
5732 dpyinfo->x_focus_frame = frame;
5734 if (old_focus && old_focus->auto_lower)
5735 x_lower_frame (old_focus);
5737 #if 0
5738 selected_frame = frame;
5739 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5740 selected_frame);
5741 Fselect_window (selected_frame->selected_window);
5742 choose_minibuf_frame ();
5743 #endif /* ! 0 */
5745 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5746 pending_autoraise_frame = dpyinfo->x_focus_frame;
5747 else
5748 pending_autoraise_frame = 0;
5751 x_frame_rehighlight (dpyinfo);
5754 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5756 void
5757 x_mouse_leave (dpyinfo)
5758 struct x_display_info *dpyinfo;
5760 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5763 /* The focus has changed, or we have redirected a frame's focus to
5764 another frame (this happens when a frame uses a surrogate
5765 mini-buffer frame). Shift the highlight as appropriate.
5767 The FRAME argument doesn't necessarily have anything to do with which
5768 frame is being highlighted or un-highlighted; we only use it to find
5769 the appropriate X display info. */
5771 static void
5772 XTframe_rehighlight (frame)
5773 struct frame *frame;
5775 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5778 static void
5779 x_frame_rehighlight (dpyinfo)
5780 struct x_display_info *dpyinfo;
5782 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5784 if (dpyinfo->x_focus_frame)
5786 dpyinfo->x_highlight_frame
5787 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5788 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5789 : dpyinfo->x_focus_frame);
5790 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5792 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5793 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5796 else
5797 dpyinfo->x_highlight_frame = 0;
5799 if (dpyinfo->x_highlight_frame != old_highlight)
5801 if (old_highlight)
5802 frame_unhighlight (old_highlight);
5803 if (dpyinfo->x_highlight_frame)
5804 frame_highlight (dpyinfo->x_highlight_frame);
5810 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5812 /* Initialize mode_switch_bit and modifier_meaning. */
5813 static void
5814 x_find_modifier_meanings (dpyinfo)
5815 struct x_display_info *dpyinfo;
5817 int min_code, max_code;
5818 KeySym *syms;
5819 int syms_per_code;
5820 XModifierKeymap *mods;
5822 dpyinfo->meta_mod_mask = 0;
5823 dpyinfo->shift_lock_mask = 0;
5824 dpyinfo->alt_mod_mask = 0;
5825 dpyinfo->super_mod_mask = 0;
5826 dpyinfo->hyper_mod_mask = 0;
5828 #ifdef HAVE_X11R4
5829 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5830 #else
5831 min_code = dpyinfo->display->min_keycode;
5832 max_code = dpyinfo->display->max_keycode;
5833 #endif
5835 syms = XGetKeyboardMapping (dpyinfo->display,
5836 min_code, max_code - min_code + 1,
5837 &syms_per_code);
5838 mods = XGetModifierMapping (dpyinfo->display);
5840 /* Scan the modifier table to see which modifier bits the Meta and
5841 Alt keysyms are on. */
5843 int row, col; /* The row and column in the modifier table. */
5845 for (row = 3; row < 8; row++)
5846 for (col = 0; col < mods->max_keypermod; col++)
5848 KeyCode code
5849 = mods->modifiermap[(row * mods->max_keypermod) + col];
5851 /* Zeroes are used for filler. Skip them. */
5852 if (code == 0)
5853 continue;
5855 /* Are any of this keycode's keysyms a meta key? */
5857 int code_col;
5859 for (code_col = 0; code_col < syms_per_code; code_col++)
5861 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5863 switch (sym)
5865 case XK_Meta_L:
5866 case XK_Meta_R:
5867 dpyinfo->meta_mod_mask |= (1 << row);
5868 break;
5870 case XK_Alt_L:
5871 case XK_Alt_R:
5872 dpyinfo->alt_mod_mask |= (1 << row);
5873 break;
5875 case XK_Hyper_L:
5876 case XK_Hyper_R:
5877 dpyinfo->hyper_mod_mask |= (1 << row);
5878 break;
5880 case XK_Super_L:
5881 case XK_Super_R:
5882 dpyinfo->super_mod_mask |= (1 << row);
5883 break;
5885 case XK_Shift_Lock:
5886 /* Ignore this if it's not on the lock modifier. */
5887 if ((1 << row) == LockMask)
5888 dpyinfo->shift_lock_mask = LockMask;
5889 break;
5896 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5897 if (! dpyinfo->meta_mod_mask)
5899 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5900 dpyinfo->alt_mod_mask = 0;
5903 /* If some keys are both alt and meta,
5904 make them just meta, not alt. */
5905 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
5907 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
5910 XFree ((char *) syms);
5911 XFreeModifiermap (mods);
5914 /* Convert between the modifier bits X uses and the modifier bits
5915 Emacs uses. */
5917 static unsigned int
5918 x_x_to_emacs_modifiers (dpyinfo, state)
5919 struct x_display_info *dpyinfo;
5920 unsigned int state;
5922 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
5923 | ((state & ControlMask) ? ctrl_modifier : 0)
5924 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
5925 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
5926 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
5927 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
5930 static unsigned int
5931 x_emacs_to_x_modifiers (dpyinfo, state)
5932 struct x_display_info *dpyinfo;
5933 unsigned int state;
5935 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
5936 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
5937 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
5938 | ((state & shift_modifier) ? ShiftMask : 0)
5939 | ((state & ctrl_modifier) ? ControlMask : 0)
5940 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
5943 /* Convert a keysym to its name. */
5945 char *
5946 x_get_keysym_name (keysym)
5947 KeySym keysym;
5949 char *value;
5951 BLOCK_INPUT;
5952 value = XKeysymToString (keysym);
5953 UNBLOCK_INPUT;
5955 return value;
5960 /* Mouse clicks and mouse movement. Rah. */
5962 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
5963 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
5964 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
5965 not force the value into range. */
5967 void
5968 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
5969 FRAME_PTR f;
5970 register int pix_x, pix_y;
5971 register int *x, *y;
5972 XRectangle *bounds;
5973 int noclip;
5975 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
5976 even for negative values. */
5977 if (pix_x < 0)
5978 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
5979 if (pix_y < 0)
5980 pix_y -= (f)->output_data.x->line_height - 1;
5982 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
5983 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
5985 if (bounds)
5987 bounds->width = FONT_WIDTH (f->output_data.x->font);
5988 bounds->height = f->output_data.x->line_height;
5989 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
5990 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
5993 if (!noclip)
5995 if (pix_x < 0)
5996 pix_x = 0;
5997 else if (pix_x > FRAME_WINDOW_WIDTH (f))
5998 pix_x = FRAME_WINDOW_WIDTH (f);
6000 if (pix_y < 0)
6001 pix_y = 0;
6002 else if (pix_y > f->height)
6003 pix_y = f->height;
6006 *x = pix_x;
6007 *y = pix_y;
6011 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6012 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6013 can't tell the positions because W's display is not up to date,
6014 return 0. */
6017 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6018 struct window *w;
6019 int hpos, vpos;
6020 int *frame_x, *frame_y;
6022 int success_p;
6024 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6025 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6027 if (display_completed)
6029 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6030 struct glyph *glyph = row->glyphs[TEXT_AREA];
6031 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6033 *frame_y = row->y;
6034 *frame_x = row->x;
6035 while (glyph < end)
6037 *frame_x += glyph->pixel_width;
6038 ++glyph;
6041 success_p = 1;
6043 else
6045 *frame_y = *frame_x = 0;
6046 success_p = 0;
6049 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6050 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6051 return success_p;
6055 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6057 If the event is a button press, then note that we have grabbed
6058 the mouse. */
6060 static Lisp_Object
6061 construct_mouse_click (result, event, f)
6062 struct input_event *result;
6063 XButtonEvent *event;
6064 struct frame *f;
6066 /* Make the event type no_event; we'll change that when we decide
6067 otherwise. */
6068 result->kind = mouse_click;
6069 result->code = event->button - Button1;
6070 result->timestamp = event->time;
6071 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6072 event->state)
6073 | (event->type == ButtonRelease
6074 ? up_modifier
6075 : down_modifier));
6077 XSETINT (result->x, event->x);
6078 XSETINT (result->y, event->y);
6079 XSETFRAME (result->frame_or_window, f);
6080 return Qnil;
6083 #if 0 /* This function isn't called. --gerd */
6085 /* Prepare a menu-event in *RESULT for placement in the input queue. */
6087 static Lisp_Object
6088 construct_menu_click (result, event, f)
6089 struct input_event *result;
6090 XButtonEvent *event;
6091 struct frame *f;
6093 /* Make the event type no_event; we'll change that when we decide
6094 otherwise. */
6095 result->kind = mouse_click;
6096 result->code = event->button - Button1;
6097 result->timestamp = event->time;
6098 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6099 event->state)
6100 | (event->type == ButtonRelease
6101 ? up_modifier
6102 : down_modifier));
6104 XSETINT (result->x, event->x);
6105 XSETINT (result->y, -1);
6106 XSETFRAME (result->frame_or_window, f);
6109 #endif /* 0 */
6112 /* Function to report a mouse movement to the mainstream Emacs code.
6113 The input handler calls this.
6115 We have received a mouse movement event, which is given in *event.
6116 If the mouse is over a different glyph than it was last time, tell
6117 the mainstream emacs code by setting mouse_moved. If not, ask for
6118 another motion event, so we can check again the next time it moves. */
6120 static XMotionEvent last_mouse_motion_event;
6121 static Lisp_Object last_mouse_motion_frame;
6123 static void
6124 note_mouse_movement (frame, event)
6125 FRAME_PTR frame;
6126 XMotionEvent *event;
6128 last_mouse_movement_time = event->time;
6129 last_mouse_motion_event = *event;
6130 XSETFRAME (last_mouse_motion_frame, frame);
6132 if (event->window != FRAME_X_WINDOW (frame))
6134 frame->mouse_moved = 1;
6135 last_mouse_scroll_bar = Qnil;
6136 note_mouse_highlight (frame, -1, -1);
6139 /* Has the mouse moved off the glyph it was on at the last sighting? */
6140 else if (event->x < last_mouse_glyph.x
6141 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6142 || event->y < last_mouse_glyph.y
6143 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6145 frame->mouse_moved = 1;
6146 last_mouse_scroll_bar = Qnil;
6147 note_mouse_highlight (frame, event->x, event->y);
6151 /* This is used for debugging, to turn off note_mouse_highlight. */
6153 int disable_mouse_highlight;
6157 /************************************************************************
6158 Mouse Face
6159 ************************************************************************/
6161 /* Find the glyph under window-relative coordinates X/Y in window W.
6162 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6163 strings. Return in *HPOS and *VPOS the row and column number of
6164 the glyph found. Return in *AREA the glyph area containing X.
6165 Value is a pointer to the glyph found or null if X/Y is not on
6166 text, or we can't tell because W's current matrix is not up to
6167 date. */
6169 static struct glyph *
6170 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6171 struct window *w;
6172 int x, y;
6173 int *hpos, *vpos, *area;
6175 struct glyph *glyph, *end;
6176 struct glyph_row *row = NULL;
6177 int x0, i, left_area_width;
6179 /* Find row containing Y. Give up if some row is not enabled. */
6180 for (i = 0; i < w->current_matrix->nrows; ++i)
6182 row = MATRIX_ROW (w->current_matrix, i);
6183 if (!row->enabled_p)
6184 return NULL;
6185 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6186 break;
6189 *vpos = i;
6190 *hpos = 0;
6192 /* Give up if Y is not in the window. */
6193 if (i == w->current_matrix->nrows)
6194 return NULL;
6196 /* Get the glyph area containing X. */
6197 if (w->pseudo_window_p)
6199 *area = TEXT_AREA;
6200 x0 = 0;
6202 else
6204 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6205 if (x < left_area_width)
6207 *area = LEFT_MARGIN_AREA;
6208 x0 = 0;
6210 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6212 *area = TEXT_AREA;
6213 x0 = row->x + left_area_width;
6215 else
6217 *area = RIGHT_MARGIN_AREA;
6218 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6222 /* Find glyph containing X. */
6223 glyph = row->glyphs[*area];
6224 end = glyph + row->used[*area];
6225 while (glyph < end)
6227 if (x < x0 + glyph->pixel_width)
6229 if (w->pseudo_window_p)
6230 break;
6231 else if (BUFFERP (glyph->object))
6232 break;
6235 x0 += glyph->pixel_width;
6236 ++glyph;
6239 if (glyph == end)
6240 return NULL;
6242 *hpos = glyph - row->glyphs[*area];
6243 return glyph;
6247 /* Convert frame-relative x/y to coordinates relative to window W.
6248 Takes pseudo-windows into account. */
6250 static void
6251 frame_to_window_pixel_xy (w, x, y)
6252 struct window *w;
6253 int *x, *y;
6255 if (w->pseudo_window_p)
6257 /* A pseudo-window is always full-width, and starts at the
6258 left edge of the frame, plus a frame border. */
6259 struct frame *f = XFRAME (w->frame);
6260 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6261 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6263 else
6265 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6266 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6271 /* Take proper action when mouse has moved to the mode or top line of
6272 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6273 mode line. X is relative to the start of the text display area of
6274 W, so the width of bitmap areas and scroll bars must be subtracted
6275 to get a position relative to the start of the mode line. */
6277 static void
6278 note_mode_line_highlight (w, x, mode_line_p)
6279 struct window *w;
6280 int x, mode_line_p;
6282 struct frame *f = XFRAME (w->frame);
6283 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6284 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6285 struct glyph_row *row;
6287 if (mode_line_p)
6288 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6289 else
6290 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6292 if (row->enabled_p)
6294 struct glyph *glyph, *end;
6295 Lisp_Object help, map;
6296 int x0;
6298 /* Find the glyph under X. */
6299 glyph = row->glyphs[TEXT_AREA];
6300 end = glyph + row->used[TEXT_AREA];
6301 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6302 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6303 while (glyph < end
6304 && x >= x0 + glyph->pixel_width)
6306 x0 += glyph->pixel_width;
6307 ++glyph;
6310 if (glyph < end
6311 && STRINGP (glyph->object)
6312 && XSTRING (glyph->object)->intervals
6313 && glyph->charpos >= 0
6314 && glyph->charpos < XSTRING (glyph->object)->size)
6316 /* If we're on a string with `help-echo' text property,
6317 arrange for the help to be displayed. This is done by
6318 setting the global variable help_echo to the help string. */
6319 help = Fget_text_property (make_number (glyph->charpos),
6320 Qhelp_echo, glyph->object);
6321 if (STRINGP (help))
6322 help_echo = help;
6324 /* Change the mouse pointer according to what is under X/Y. */
6325 map = Fget_text_property (make_number (glyph->charpos),
6326 Qlocal_map, glyph->object);
6327 if (!NILP (Fkeymapp (map)))
6328 cursor = f->output_data.x->nontext_cursor;
6332 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6336 /* Take proper action when the mouse has moved to position X, Y on
6337 frame F as regards highlighting characters that have mouse-face
6338 properties. Also de-highlighting chars where the mouse was before.
6339 X and Y can be negative or out of range. */
6341 static void
6342 note_mouse_highlight (f, x, y)
6343 struct frame *f;
6344 int x, y;
6346 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6347 int portion;
6348 Lisp_Object window;
6349 struct window *w;
6351 /* When a menu is active, don't highlight because this looks odd. */
6352 #ifdef USE_X_TOOLKIT
6353 if (popup_activated ())
6354 return;
6355 #endif
6357 if (disable_mouse_highlight
6358 || !f->glyphs_initialized_p)
6359 return;
6361 dpyinfo->mouse_face_mouse_x = x;
6362 dpyinfo->mouse_face_mouse_y = y;
6363 dpyinfo->mouse_face_mouse_frame = f;
6365 if (dpyinfo->mouse_face_defer)
6366 return;
6368 if (gc_in_progress)
6370 dpyinfo->mouse_face_deferred_gc = 1;
6371 return;
6374 /* Which window is that in? */
6375 window = window_from_coordinates (f, x, y, &portion, 1);
6377 /* If we were displaying active text in another window, clear that. */
6378 if (! EQ (window, dpyinfo->mouse_face_window))
6379 clear_mouse_face (dpyinfo);
6381 /* Not on a window -> return. */
6382 if (!WINDOWP (window))
6383 return;
6385 /* Convert to window-relative pixel coordinates. */
6386 w = XWINDOW (window);
6387 frame_to_window_pixel_xy (w, &x, &y);
6389 /* Handle tool-bar window differently since it doesn't display a
6390 buffer. */
6391 if (EQ (window, f->tool_bar_window))
6393 note_tool_bar_highlight (f, x, y);
6394 return;
6397 if (portion == 1 || portion == 3)
6399 /* Mouse is on the mode or top line. */
6400 note_mode_line_highlight (w, x, portion == 1);
6401 return;
6403 else
6404 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6405 f->output_data.x->text_cursor);
6407 /* Are we in a window whose display is up to date?
6408 And verify the buffer's text has not changed. */
6409 if (/* Within text portion of the window. */
6410 portion == 0
6411 && EQ (w->window_end_valid, w->buffer)
6412 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6413 && (XFASTINT (w->last_overlay_modified)
6414 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6416 int hpos, vpos, pos, i, area;
6417 struct glyph *glyph;
6419 /* Find the glyph under X/Y. */
6420 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6422 /* Clear mouse face if X/Y not over text. */
6423 if (glyph == NULL
6424 || area != TEXT_AREA
6425 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6427 clear_mouse_face (dpyinfo);
6428 return;
6431 pos = glyph->charpos;
6432 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6434 /* Check for mouse-face and help-echo. */
6436 Lisp_Object mouse_face, overlay, position;
6437 Lisp_Object *overlay_vec;
6438 int len, noverlays;
6439 struct buffer *obuf;
6440 int obegv, ozv;
6442 /* If we get an out-of-range value, return now; avoid an error. */
6443 if (pos > BUF_Z (XBUFFER (w->buffer)))
6444 return;
6446 /* Make the window's buffer temporarily current for
6447 overlays_at and compute_char_face. */
6448 obuf = current_buffer;
6449 current_buffer = XBUFFER (w->buffer);
6450 obegv = BEGV;
6451 ozv = ZV;
6452 BEGV = BEG;
6453 ZV = Z;
6455 /* Is this char mouse-active or does it have help-echo? */
6456 XSETINT (position, pos);
6458 /* Put all the overlays we want in a vector in overlay_vec.
6459 Store the length in len. If there are more than 10, make
6460 enough space for all, and try again. */
6461 len = 10;
6462 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6463 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6464 if (noverlays > len)
6466 len = noverlays;
6467 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6468 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
6471 noverlays = sort_overlays (overlay_vec, noverlays, w);
6473 /* Check mouse-face highlighting. */
6474 if (! (EQ (window, dpyinfo->mouse_face_window)
6475 && vpos >= dpyinfo->mouse_face_beg_row
6476 && vpos <= dpyinfo->mouse_face_end_row
6477 && (vpos > dpyinfo->mouse_face_beg_row
6478 || hpos >= dpyinfo->mouse_face_beg_col)
6479 && (vpos < dpyinfo->mouse_face_end_row
6480 || hpos < dpyinfo->mouse_face_end_col
6481 || dpyinfo->mouse_face_past_end)))
6483 /* Clear the display of the old active region, if any. */
6484 clear_mouse_face (dpyinfo);
6486 /* Find the highest priority overlay that has a mouse-face prop. */
6487 overlay = Qnil;
6488 for (i = 0; i < noverlays; i++)
6490 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6491 if (!NILP (mouse_face))
6493 overlay = overlay_vec[i];
6494 break;
6498 /* If no overlay applies, get a text property. */
6499 if (NILP (overlay))
6500 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6502 /* Handle the overlay case. */
6503 if (! NILP (overlay))
6505 /* Find the range of text around this char that
6506 should be active. */
6507 Lisp_Object before, after;
6508 int ignore;
6510 before = Foverlay_start (overlay);
6511 after = Foverlay_end (overlay);
6512 /* Record this as the current active region. */
6513 fast_find_position (w, XFASTINT (before),
6514 &dpyinfo->mouse_face_beg_col,
6515 &dpyinfo->mouse_face_beg_row,
6516 &dpyinfo->mouse_face_beg_x,
6517 &dpyinfo->mouse_face_beg_y);
6518 dpyinfo->mouse_face_past_end
6519 = !fast_find_position (w, XFASTINT (after),
6520 &dpyinfo->mouse_face_end_col,
6521 &dpyinfo->mouse_face_end_row,
6522 &dpyinfo->mouse_face_end_x,
6523 &dpyinfo->mouse_face_end_y);
6524 dpyinfo->mouse_face_window = window;
6525 dpyinfo->mouse_face_face_id
6526 = face_at_buffer_position (w, pos, 0, 0,
6527 &ignore, pos + 1, 1);
6529 /* Display it as active. */
6530 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6532 /* Handle the text property case. */
6533 else if (! NILP (mouse_face))
6535 /* Find the range of text around this char that
6536 should be active. */
6537 Lisp_Object before, after, beginning, end;
6538 int ignore;
6540 beginning = Fmarker_position (w->start);
6541 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6542 - XFASTINT (w->window_end_pos)));
6543 before
6544 = Fprevious_single_property_change (make_number (pos + 1),
6545 Qmouse_face,
6546 w->buffer, beginning);
6547 after
6548 = Fnext_single_property_change (position, Qmouse_face,
6549 w->buffer, end);
6550 /* Record this as the current active region. */
6551 fast_find_position (w, XFASTINT (before),
6552 &dpyinfo->mouse_face_beg_col,
6553 &dpyinfo->mouse_face_beg_row,
6554 &dpyinfo->mouse_face_beg_x,
6555 &dpyinfo->mouse_face_beg_y);
6556 dpyinfo->mouse_face_past_end
6557 = !fast_find_position (w, XFASTINT (after),
6558 &dpyinfo->mouse_face_end_col,
6559 &dpyinfo->mouse_face_end_row,
6560 &dpyinfo->mouse_face_end_x,
6561 &dpyinfo->mouse_face_end_y);
6562 dpyinfo->mouse_face_window = window;
6563 dpyinfo->mouse_face_face_id
6564 = face_at_buffer_position (w, pos, 0, 0,
6565 &ignore, pos + 1, 1);
6567 /* Display it as active. */
6568 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6572 /* Look for a `help-echo' property. */
6574 Lisp_Object help;
6576 /* Check overlays first. */
6577 help = Qnil;
6578 for (i = 0; i < noverlays && !STRINGP (help); ++i)
6579 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
6581 /* Try text properties. */
6582 if (!STRINGP (help)
6583 && ((STRINGP (glyph->object)
6584 && glyph->charpos >= 0
6585 && glyph->charpos < XSTRING (glyph->object)->size)
6586 || (BUFFERP (glyph->object)
6587 && glyph->charpos >= BEGV
6588 && glyph->charpos < ZV)))
6589 help = Fget_text_property (make_number (glyph->charpos),
6590 Qhelp_echo, glyph->object);
6592 if (STRINGP (help))
6593 help_echo = help;
6596 BEGV = obegv;
6597 ZV = ozv;
6598 current_buffer = obuf;
6603 static void
6604 redo_mouse_highlight ()
6606 if (!NILP (last_mouse_motion_frame)
6607 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6608 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6609 last_mouse_motion_event.x,
6610 last_mouse_motion_event.y);
6615 /***********************************************************************
6616 Tool-bars
6617 ***********************************************************************/
6619 static int x_tool_bar_item P_ ((struct frame *, int, int,
6620 struct glyph **, int *, int *, int *));
6622 /* Tool-bar item index of the item on which a mouse button was pressed
6623 or -1. */
6625 static int last_tool_bar_item;
6628 /* Get information about the tool-bar item at position X/Y on frame F.
6629 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6630 the current matrix of the tool-bar window of F, or NULL if not
6631 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6632 item in F->current_tool_bar_items. Value is
6634 -1 if X/Y is not on a tool-bar item
6635 0 if X/Y is on the same item that was highlighted before.
6636 1 otherwise. */
6638 static int
6639 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6640 struct frame *f;
6641 int x, y;
6642 struct glyph **glyph;
6643 int *hpos, *vpos, *prop_idx;
6645 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6646 struct window *w = XWINDOW (f->tool_bar_window);
6647 int area;
6649 /* Find the glyph under X/Y. */
6650 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6651 if (*glyph == NULL)
6652 return -1;
6654 /* Get the start of this tool-bar item's properties in
6655 f->current_tool_bar_items. */
6656 if (!tool_bar_item_info (f, *glyph, prop_idx))
6657 return -1;
6659 /* Is mouse on the highlighted item? */
6660 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6661 && *vpos >= dpyinfo->mouse_face_beg_row
6662 && *vpos <= dpyinfo->mouse_face_end_row
6663 && (*vpos > dpyinfo->mouse_face_beg_row
6664 || *hpos >= dpyinfo->mouse_face_beg_col)
6665 && (*vpos < dpyinfo->mouse_face_end_row
6666 || *hpos < dpyinfo->mouse_face_end_col
6667 || dpyinfo->mouse_face_past_end))
6668 return 0;
6670 return 1;
6674 /* Handle mouse button event on the tool-bar of frame F, at
6675 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6676 or ButtonRelase. */
6678 static void
6679 x_handle_tool_bar_click (f, button_event)
6680 struct frame *f;
6681 XButtonEvent *button_event;
6683 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6684 struct window *w = XWINDOW (f->tool_bar_window);
6685 int hpos, vpos, prop_idx;
6686 struct glyph *glyph;
6687 Lisp_Object enabled_p;
6688 int x = button_event->x;
6689 int y = button_event->y;
6691 /* If not on the highlighted tool-bar item, return. */
6692 frame_to_window_pixel_xy (w, &x, &y);
6693 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6694 return;
6696 /* If item is disabled, do nothing. */
6697 enabled_p = (XVECTOR (f->current_tool_bar_items)
6698 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6699 if (NILP (enabled_p))
6700 return;
6702 if (button_event->type == ButtonPress)
6704 /* Show item in pressed state. */
6705 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6706 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6707 last_tool_bar_item = prop_idx;
6709 else
6711 Lisp_Object key, frame;
6712 struct input_event event;
6714 /* Show item in released state. */
6715 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6716 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6718 key = (XVECTOR (f->current_tool_bar_items)
6719 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6721 XSETFRAME (frame, f);
6722 event.kind = TOOL_BAR_EVENT;
6723 event.frame_or_window = Fcons (frame, Fcons (Qtool_bar, Qnil));
6724 kbd_buffer_store_event (&event);
6726 event.kind = TOOL_BAR_EVENT;
6727 event.frame_or_window = Fcons (frame, key);
6728 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6729 button_event->state);
6730 kbd_buffer_store_event (&event);
6731 last_tool_bar_item = -1;
6736 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6737 tool-bar window-relative coordinates X/Y. Called from
6738 note_mouse_highlight. */
6740 static void
6741 note_tool_bar_highlight (f, x, y)
6742 struct frame *f;
6743 int x, y;
6745 Lisp_Object window = f->tool_bar_window;
6746 struct window *w = XWINDOW (window);
6747 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6748 int hpos, vpos;
6749 struct glyph *glyph;
6750 struct glyph_row *row;
6751 int i;
6752 Lisp_Object enabled_p;
6753 int prop_idx;
6754 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6755 int mouse_down_p, rc;
6757 /* Function note_mouse_highlight is called with negative x(y
6758 values when mouse moves outside of the frame. */
6759 if (x <= 0 || y <= 0)
6761 clear_mouse_face (dpyinfo);
6762 return;
6765 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6766 if (rc < 0)
6768 /* Not on tool-bar item. */
6769 clear_mouse_face (dpyinfo);
6770 return;
6772 else if (rc == 0)
6773 /* On same tool-bar item as before. */
6774 goto set_help_echo;
6776 clear_mouse_face (dpyinfo);
6778 /* Mouse is down, but on different tool-bar item? */
6779 mouse_down_p = (dpyinfo->grabbed
6780 && f == last_mouse_frame
6781 && FRAME_LIVE_P (f));
6782 if (mouse_down_p
6783 && last_tool_bar_item != prop_idx)
6784 return;
6786 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6787 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6789 /* If tool-bar item is not enabled, don't highlight it. */
6790 enabled_p = (XVECTOR (f->current_tool_bar_items)
6791 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6792 if (!NILP (enabled_p))
6794 /* Compute the x-position of the glyph. In front and past the
6795 image is a space. We include this is the highlighted area. */
6796 row = MATRIX_ROW (w->current_matrix, vpos);
6797 for (i = x = 0; i < hpos; ++i)
6798 x += row->glyphs[TEXT_AREA][i].pixel_width;
6800 /* Record this as the current active region. */
6801 dpyinfo->mouse_face_beg_col = hpos;
6802 dpyinfo->mouse_face_beg_row = vpos;
6803 dpyinfo->mouse_face_beg_x = x;
6804 dpyinfo->mouse_face_beg_y = row->y;
6805 dpyinfo->mouse_face_past_end = 0;
6807 dpyinfo->mouse_face_end_col = hpos + 1;
6808 dpyinfo->mouse_face_end_row = vpos;
6809 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6810 dpyinfo->mouse_face_end_y = row->y;
6811 dpyinfo->mouse_face_window = window;
6812 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6814 /* Display it as active. */
6815 show_mouse_face (dpyinfo, draw);
6816 dpyinfo->mouse_face_image_state = draw;
6819 set_help_echo:
6821 /* Set help_echo to a help string.to display for this tool-bar item.
6822 XTread_socket does the rest. */
6823 help_echo = (XVECTOR (f->current_tool_bar_items)
6824 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6825 if (!STRINGP (help_echo))
6826 help_echo = (XVECTOR (f->current_tool_bar_items)
6827 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6832 /* Find the glyph matrix position of buffer position POS in window W.
6833 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6834 current glyphs must be up to date. If POS is above window start
6835 return (0, 0, 0, 0). If POS is after end of W, return end of
6836 last line in W. */
6838 static int
6839 fast_find_position (w, pos, hpos, vpos, x, y)
6840 struct window *w;
6841 int pos;
6842 int *hpos, *vpos, *x, *y;
6844 int i;
6845 int lastcol;
6846 int maybe_next_line_p = 0;
6847 int line_start_position;
6848 int yb = window_text_bottom_y (w);
6849 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6850 struct glyph_row *best_row = row;
6851 int row_vpos = 0, best_row_vpos = 0;
6852 int current_x;
6854 while (row->y < yb)
6856 if (row->used[TEXT_AREA])
6857 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6858 else
6859 line_start_position = 0;
6861 if (line_start_position > pos)
6862 break;
6863 /* If the position sought is the end of the buffer,
6864 don't include the blank lines at the bottom of the window. */
6865 else if (line_start_position == pos
6866 && pos == BUF_ZV (XBUFFER (w->buffer)))
6868 maybe_next_line_p = 1;
6869 break;
6871 else if (line_start_position > 0)
6873 best_row = row;
6874 best_row_vpos = row_vpos;
6877 if (row->y + row->height >= yb)
6878 break;
6880 ++row;
6881 ++row_vpos;
6884 /* Find the right column within BEST_ROW. */
6885 lastcol = 0;
6886 current_x = best_row->x;
6887 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6889 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6890 int charpos;
6892 charpos = glyph->charpos;
6893 if (charpos == pos)
6895 *hpos = i;
6896 *vpos = best_row_vpos;
6897 *x = current_x;
6898 *y = best_row->y;
6899 return 1;
6901 else if (charpos > pos)
6902 break;
6903 else if (charpos > 0)
6904 lastcol = i;
6906 current_x += glyph->pixel_width;
6909 /* If we're looking for the end of the buffer,
6910 and we didn't find it in the line we scanned,
6911 use the start of the following line. */
6912 if (maybe_next_line_p)
6914 ++best_row;
6915 ++best_row_vpos;
6916 lastcol = 0;
6917 current_x = best_row->x;
6920 *vpos = best_row_vpos;
6921 *hpos = lastcol + 1;
6922 *x = current_x;
6923 *y = best_row->y;
6924 return 0;
6928 /* Display the active region described by mouse_face_*
6929 in its mouse-face if HL > 0, in its normal face if HL = 0. */
6931 static void
6932 show_mouse_face (dpyinfo, draw)
6933 struct x_display_info *dpyinfo;
6934 enum draw_glyphs_face draw;
6936 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
6937 struct frame *f = XFRAME (WINDOW_FRAME (w));
6938 int i;
6939 int cursor_off_p = 0;
6940 struct cursor_pos saved_cursor;
6942 saved_cursor = output_cursor;
6944 /* If window is in the process of being destroyed, don't bother
6945 to do anything. */
6946 if (w->current_matrix == NULL)
6947 goto set_x_cursor;
6949 /* Recognize when we are called to operate on rows that don't exist
6950 anymore. This can happen when a window is split. */
6951 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
6952 goto set_x_cursor;
6954 set_output_cursor (&w->phys_cursor);
6956 /* Note that mouse_face_beg_row etc. are window relative. */
6957 for (i = dpyinfo->mouse_face_beg_row;
6958 i <= dpyinfo->mouse_face_end_row;
6959 i++)
6961 int start_hpos, end_hpos, start_x;
6962 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
6964 /* Don't do anything if row doesn't have valid contents. */
6965 if (!row->enabled_p)
6966 continue;
6968 /* For all but the first row, the highlight starts at column 0. */
6969 if (i == dpyinfo->mouse_face_beg_row)
6971 start_hpos = dpyinfo->mouse_face_beg_col;
6972 start_x = dpyinfo->mouse_face_beg_x;
6974 else
6976 start_hpos = 0;
6977 start_x = 0;
6980 if (i == dpyinfo->mouse_face_end_row)
6981 end_hpos = dpyinfo->mouse_face_end_col;
6982 else
6983 end_hpos = row->used[TEXT_AREA];
6985 /* If the cursor's in the text we are about to rewrite, turn the
6986 cursor off. */
6987 if (!w->pseudo_window_p
6988 && i == output_cursor.vpos
6989 && output_cursor.hpos >= start_hpos - 1
6990 && output_cursor.hpos <= end_hpos)
6992 x_update_window_cursor (w, 0);
6993 cursor_off_p = 1;
6996 if (end_hpos > start_hpos)
6997 x_draw_glyphs (w, start_x, row, TEXT_AREA,
6998 start_hpos, end_hpos, draw, NULL, NULL, 0);
7001 /* If we turned the cursor off, turn it back on. */
7002 if (cursor_off_p)
7003 x_display_cursor (w, 1,
7004 output_cursor.hpos, output_cursor.vpos,
7005 output_cursor.x, output_cursor.y);
7007 output_cursor = saved_cursor;
7009 set_x_cursor:
7011 /* Change the mouse cursor. */
7012 if (draw == DRAW_NORMAL_TEXT)
7013 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7014 f->output_data.x->text_cursor);
7015 else if (draw == DRAW_MOUSE_FACE)
7016 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7017 f->output_data.x->cross_cursor);
7018 else
7019 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7020 f->output_data.x->nontext_cursor);
7023 /* Clear out the mouse-highlighted active region.
7024 Redraw it un-highlighted first. */
7026 void
7027 clear_mouse_face (dpyinfo)
7028 struct x_display_info *dpyinfo;
7030 if (tip_frame)
7031 return;
7033 if (! NILP (dpyinfo->mouse_face_window))
7034 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7036 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7037 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7038 dpyinfo->mouse_face_window = Qnil;
7041 /* Just discard the mouse face information for frame F, if any.
7042 This is used when the size of F is changed. */
7044 void
7045 cancel_mouse_face (f)
7046 FRAME_PTR f;
7048 Lisp_Object window;
7049 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7051 window = dpyinfo->mouse_face_window;
7052 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7054 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7055 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7056 dpyinfo->mouse_face_window = Qnil;
7060 static struct scroll_bar *x_window_to_scroll_bar ();
7061 static void x_scroll_bar_report_motion ();
7063 /* Return the current position of the mouse.
7064 *fp should be a frame which indicates which display to ask about.
7066 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7067 and *part to the frame, window, and scroll bar part that the mouse
7068 is over. Set *x and *y to the portion and whole of the mouse's
7069 position on the scroll bar.
7071 If the mouse movement started elsewhere, set *fp to the frame the
7072 mouse is on, *bar_window to nil, and *x and *y to the character cell
7073 the mouse is over.
7075 Set *time to the server time-stamp for the time at which the mouse
7076 was at this position.
7078 Don't store anything if we don't have a valid set of values to report.
7080 This clears the mouse_moved flag, so we can wait for the next mouse
7081 movement. */
7083 static void
7084 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7085 FRAME_PTR *fp;
7086 int insist;
7087 Lisp_Object *bar_window;
7088 enum scroll_bar_part *part;
7089 Lisp_Object *x, *y;
7090 unsigned long *time;
7092 FRAME_PTR f1;
7094 BLOCK_INPUT;
7096 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7097 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7098 else
7100 Window root;
7101 int root_x, root_y;
7103 Window dummy_window;
7104 int dummy;
7106 Lisp_Object frame, tail;
7108 /* Clear the mouse-moved flag for every frame on this display. */
7109 FOR_EACH_FRAME (tail, frame)
7110 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7111 XFRAME (frame)->mouse_moved = 0;
7113 last_mouse_scroll_bar = Qnil;
7115 /* Figure out which root window we're on. */
7116 XQueryPointer (FRAME_X_DISPLAY (*fp),
7117 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7119 /* The root window which contains the pointer. */
7120 &root,
7122 /* Trash which we can't trust if the pointer is on
7123 a different screen. */
7124 &dummy_window,
7126 /* The position on that root window. */
7127 &root_x, &root_y,
7129 /* More trash we can't trust. */
7130 &dummy, &dummy,
7132 /* Modifier keys and pointer buttons, about which
7133 we don't care. */
7134 (unsigned int *) &dummy);
7136 /* Now we have a position on the root; find the innermost window
7137 containing the pointer. */
7139 Window win, child;
7140 int win_x, win_y;
7141 int parent_x = 0, parent_y = 0;
7142 int count;
7144 win = root;
7146 /* XTranslateCoordinates can get errors if the window
7147 structure is changing at the same time this function
7148 is running. So at least we must not crash from them. */
7150 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7152 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7153 && FRAME_LIVE_P (last_mouse_frame))
7155 /* If mouse was grabbed on a frame, give coords for that frame
7156 even if the mouse is now outside it. */
7157 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7159 /* From-window, to-window. */
7160 root, FRAME_X_WINDOW (last_mouse_frame),
7162 /* From-position, to-position. */
7163 root_x, root_y, &win_x, &win_y,
7165 /* Child of win. */
7166 &child);
7167 f1 = last_mouse_frame;
7169 else
7171 while (1)
7173 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7175 /* From-window, to-window. */
7176 root, win,
7178 /* From-position, to-position. */
7179 root_x, root_y, &win_x, &win_y,
7181 /* Child of win. */
7182 &child);
7184 if (child == None || child == win)
7185 break;
7187 win = child;
7188 parent_x = win_x;
7189 parent_y = win_y;
7192 /* Now we know that:
7193 win is the innermost window containing the pointer
7194 (XTC says it has no child containing the pointer),
7195 win_x and win_y are the pointer's position in it
7196 (XTC did this the last time through), and
7197 parent_x and parent_y are the pointer's position in win's parent.
7198 (They are what win_x and win_y were when win was child.
7199 If win is the root window, it has no parent, and
7200 parent_{x,y} are invalid, but that's okay, because we'll
7201 never use them in that case.) */
7203 /* Is win one of our frames? */
7204 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7207 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7208 f1 = 0;
7210 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7212 /* If not, is it one of our scroll bars? */
7213 if (! f1)
7215 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7217 if (bar)
7219 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7220 win_x = parent_x;
7221 win_y = parent_y;
7225 if (f1 == 0 && insist > 0)
7226 f1 = SELECTED_FRAME ();
7228 if (f1)
7230 /* Ok, we found a frame. Store all the values.
7231 last_mouse_glyph is a rectangle used to reduce the
7232 generation of mouse events. To not miss any motion
7233 events, we must divide the frame into rectangles of the
7234 size of the smallest character that could be displayed
7235 on it, i.e. into the same rectangles that matrices on
7236 the frame are divided into. */
7238 #if OLD_REDISPLAY_CODE
7239 int ignore1, ignore2;
7240 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7241 &last_mouse_glyph,
7242 FRAME_X_DISPLAY_INFO (f1)->grabbed
7243 || insist);
7244 #else
7246 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7247 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7248 int x = win_x;
7249 int y = win_y;
7251 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7252 round down even for negative values. */
7253 if (x < 0)
7254 x -= width - 1;
7255 if (y < 0)
7256 y -= height - 1;
7258 last_mouse_glyph.width = width;
7259 last_mouse_glyph.height = height;
7260 last_mouse_glyph.x = (x + width - 1) / width * width;
7261 last_mouse_glyph.y = (y + height - 1) / height * height;
7263 #endif
7265 *bar_window = Qnil;
7266 *part = 0;
7267 *fp = f1;
7268 XSETINT (*x, win_x);
7269 XSETINT (*y, win_y);
7270 *time = last_mouse_movement_time;
7275 UNBLOCK_INPUT;
7279 #ifdef USE_X_TOOLKIT
7281 /* Atimer callback function for TIMER. Called every 0.1s to process
7282 Xt timeouts, if needed. We must avoid calling XtAppPending as
7283 much as possible because that function does an implicit XFlush
7284 that slows us down. */
7286 static void
7287 x_process_timeouts (timer)
7288 struct atimer *timer;
7290 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7292 BLOCK_INPUT;
7293 while (XtAppPending (Xt_app_con) & XtIMTimer)
7294 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7295 UNBLOCK_INPUT;
7299 #endif /* USE_X_TOOLKIT */
7302 /* Scroll bar support. */
7304 /* Given an X window ID, find the struct scroll_bar which manages it.
7305 This can be called in GC, so we have to make sure to strip off mark
7306 bits. */
7308 static struct scroll_bar *
7309 x_window_to_scroll_bar (window_id)
7310 Window window_id;
7312 Lisp_Object tail;
7314 for (tail = Vframe_list;
7315 XGCTYPE (tail) == Lisp_Cons;
7316 tail = XCDR (tail))
7318 Lisp_Object frame, bar, condemned;
7320 frame = XCAR (tail);
7321 /* All elements of Vframe_list should be frames. */
7322 if (! GC_FRAMEP (frame))
7323 abort ();
7325 /* Scan this frame's scroll bar list for a scroll bar with the
7326 right window ID. */
7327 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7328 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7329 /* This trick allows us to search both the ordinary and
7330 condemned scroll bar lists with one loop. */
7331 ! GC_NILP (bar) || (bar = condemned,
7332 condemned = Qnil,
7333 ! GC_NILP (bar));
7334 bar = XSCROLL_BAR (bar)->next)
7335 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7336 return XSCROLL_BAR (bar);
7339 return 0;
7344 /************************************************************************
7345 Toolkit scroll bars
7346 ************************************************************************/
7348 #if USE_TOOLKIT_SCROLL_BARS
7350 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7351 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7352 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7353 struct scroll_bar *));
7354 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7355 int, int, int));
7358 /* Id of action hook installed for scroll bars. */
7360 static XtActionHookId action_hook_id;
7362 /* Lisp window being scrolled. Set when starting to interact with
7363 a toolkit scroll bar, reset to nil when ending the interaction. */
7365 static Lisp_Object window_being_scrolled;
7367 /* Last scroll bar part sent in xm_scroll_callback. */
7369 static int last_scroll_bar_part;
7371 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7372 that movements of 1/20 of the screen size are mapped to up/down. */
7374 static Boolean xaw3d_arrow_scroll;
7376 /* Whether the drag scrolling maintains the mouse at the top of the
7377 thumb. If not, resizing the thumb needs to be done more carefully
7378 to avoid jerkyness. */
7380 static Boolean xaw3d_pick_top;
7383 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7384 bars are used.. The hook is responsible for detecting when
7385 the user ends an interaction with the scroll bar, and generates
7386 a `end-scroll' scroll_bar_click' event if so. */
7388 static void
7389 xt_action_hook (widget, client_data, action_name, event, params,
7390 num_params)
7391 Widget widget;
7392 XtPointer client_data;
7393 String action_name;
7394 XEvent *event;
7395 String *params;
7396 Cardinal *num_params;
7398 int scroll_bar_p;
7399 char *end_action;
7401 #ifdef USE_MOTIF
7402 scroll_bar_p = XmIsScrollBar (widget);
7403 end_action = "Release";
7404 #else /* !USE_MOTIF i.e. use Xaw */
7405 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7406 end_action = "EndScroll";
7407 #endif /* USE_MOTIF */
7409 if (scroll_bar_p
7410 && strcmp (action_name, end_action) == 0
7411 && WINDOWP (window_being_scrolled))
7413 struct window *w;
7415 x_send_scroll_bar_event (window_being_scrolled,
7416 scroll_bar_end_scroll, 0, 0);
7417 w = XWINDOW (window_being_scrolled);
7418 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7419 window_being_scrolled = Qnil;
7420 last_scroll_bar_part = -1;
7422 /* Xt timeouts no longer needed. */
7423 toolkit_scroll_bar_interaction = 0;
7428 /* Send a client message with message type Xatom_Scrollbar for a
7429 scroll action to the frame of WINDOW. PART is a value identifying
7430 the part of the scroll bar that was clicked on. PORTION is the
7431 amount to scroll of a whole of WHOLE. */
7433 static void
7434 x_send_scroll_bar_event (window, part, portion, whole)
7435 Lisp_Object window;
7436 int part, portion, whole;
7438 XEvent event;
7439 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7440 struct frame *f = XFRAME (XWINDOW (window)->frame);
7442 /* Construct a ClientMessage event to send to the frame. */
7443 ev->type = ClientMessage;
7444 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7445 ev->display = FRAME_X_DISPLAY (f);
7446 ev->window = FRAME_X_WINDOW (f);
7447 ev->format = 32;
7448 ev->data.l[0] = (long) XFASTINT (window);
7449 ev->data.l[1] = (long) part;
7450 ev->data.l[2] = (long) 0;
7451 ev->data.l[3] = (long) portion;
7452 ev->data.l[4] = (long) whole;
7454 /* Make Xt timeouts work while the scroll bar is active. */
7455 toolkit_scroll_bar_interaction = 1;
7457 /* Setting the event mask to zero means that the message will
7458 be sent to the client that created the window, and if that
7459 window no longer exists, no event will be sent. */
7460 BLOCK_INPUT;
7461 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7462 UNBLOCK_INPUT;
7466 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7467 in *IEVENT. */
7469 static void
7470 x_scroll_bar_to_input_event (event, ievent)
7471 XEvent *event;
7472 struct input_event *ievent;
7474 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7475 Lisp_Object window;
7476 struct frame *f;
7478 XSETFASTINT (window, ev->data.l[0]);
7479 f = XFRAME (XWINDOW (window)->frame);
7481 ievent->kind = scroll_bar_click;
7482 ievent->frame_or_window = window;
7483 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7484 ievent->part = ev->data.l[1];
7485 ievent->code = ev->data.l[2];
7486 ievent->x = make_number ((int) ev->data.l[3]);
7487 ievent->y = make_number ((int) ev->data.l[4]);
7488 ievent->modifiers = 0;
7492 #ifdef USE_MOTIF
7494 /* Minimum and maximum values used for Motif scroll bars. */
7496 #define XM_SB_MIN 1
7497 #define XM_SB_MAX 10000000
7498 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7501 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7502 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7503 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7505 static void
7506 xm_scroll_callback (widget, client_data, call_data)
7507 Widget widget;
7508 XtPointer client_data, call_data;
7510 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7511 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7512 double percent;
7513 int part = -1, whole = 0, portion = 0;
7515 switch (cs->reason)
7517 case XmCR_DECREMENT:
7518 bar->dragging = Qnil;
7519 part = scroll_bar_up_arrow;
7520 break;
7522 case XmCR_INCREMENT:
7523 bar->dragging = Qnil;
7524 part = scroll_bar_down_arrow;
7525 break;
7527 case XmCR_PAGE_DECREMENT:
7528 bar->dragging = Qnil;
7529 part = scroll_bar_above_handle;
7530 break;
7532 case XmCR_PAGE_INCREMENT:
7533 bar->dragging = Qnil;
7534 part = scroll_bar_below_handle;
7535 break;
7537 case XmCR_TO_TOP:
7538 bar->dragging = Qnil;
7539 part = scroll_bar_to_top;
7540 break;
7542 case XmCR_TO_BOTTOM:
7543 bar->dragging = Qnil;
7544 part = scroll_bar_to_bottom;
7545 break;
7547 case XmCR_DRAG:
7549 int slider_size;
7550 int dragging_down_p = (INTEGERP (bar->dragging)
7551 && XINT (bar->dragging) <= cs->value);
7553 /* Get the slider size. */
7554 BLOCK_INPUT;
7555 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7556 UNBLOCK_INPUT;
7558 /* At the max position of the scroll bar, do a line-wise
7559 movement. Without doing anything, the LessTif scroll bar
7560 calls us with the same cs->value again and again. If we
7561 want to make sure that we can reach the end of the buffer,
7562 we have to do something.
7564 Implementation note: setting bar->dragging always to
7565 cs->value gives a smoother movement at the max position.
7566 Setting it to nil when doing line-wise movement gives
7567 a better slider behavior. */
7569 if (cs->value + slider_size == XM_SB_MAX
7570 || (dragging_down_p
7571 && last_scroll_bar_part == scroll_bar_down_arrow))
7573 part = scroll_bar_down_arrow;
7574 bar->dragging = Qnil;
7576 else
7578 whole = XM_SB_RANGE;
7579 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7580 part = scroll_bar_handle;
7581 bar->dragging = make_number (cs->value);
7584 break;
7586 case XmCR_VALUE_CHANGED:
7587 break;
7590 if (part >= 0)
7592 window_being_scrolled = bar->window;
7593 last_scroll_bar_part = part;
7594 x_send_scroll_bar_event (bar->window, part, portion, whole);
7599 #else /* !USE_MOTIF, i.e. Xaw. */
7602 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7603 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7604 scroll bar struct. CALL_DATA is a pointer to a float saying where
7605 the thumb is. */
7607 static void
7608 xaw_jump_callback (widget, client_data, call_data)
7609 Widget widget;
7610 XtPointer client_data, call_data;
7612 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7613 float top = *(float *) call_data;
7614 float shown;
7615 int whole, portion, height;
7616 int part;
7618 /* Get the size of the thumb, a value between 0 and 1. */
7619 BLOCK_INPUT;
7620 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7621 UNBLOCK_INPUT;
7623 whole = 10000000;
7624 portion = shown < 1 ? top * whole : 0;
7626 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7627 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7628 the bottom, so we force the scrolling whenever we see that we're
7629 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7630 we try to ensure that we always stay two pixels away from the
7631 bottom). */
7632 part = scroll_bar_down_arrow;
7633 else
7634 part = scroll_bar_handle;
7636 window_being_scrolled = bar->window;
7637 bar->dragging = make_number (portion);
7638 last_scroll_bar_part = part;
7639 x_send_scroll_bar_event (bar->window, part, portion, whole);
7643 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7644 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7645 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7646 the scroll bar. CALL_DATA is an integer specifying the action that
7647 has taken place. It's magnitude is in the range 0..height of the
7648 scroll bar. Negative values mean scroll towards buffer start.
7649 Values < height of scroll bar mean line-wise movement. */
7651 static void
7652 xaw_scroll_callback (widget, client_data, call_data)
7653 Widget widget;
7654 XtPointer client_data, call_data;
7656 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7657 int position = (int) call_data;
7658 Dimension height;
7659 int part;
7661 /* Get the height of the scroll bar. */
7662 BLOCK_INPUT;
7663 XtVaGetValues (widget, XtNheight, &height, NULL);
7664 UNBLOCK_INPUT;
7666 if (abs (position) >= height)
7667 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7669 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7670 it maps line-movement to call_data = max(5, height/20). */
7671 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7672 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7673 else
7674 part = scroll_bar_move_ratio;
7676 window_being_scrolled = bar->window;
7677 bar->dragging = Qnil;
7678 last_scroll_bar_part = part;
7679 x_send_scroll_bar_event (bar->window, part, position, height);
7683 #endif /* not USE_MOTIF */
7686 /* Create the widget for scroll bar BAR on frame F. Record the widget
7687 and X window of the scroll bar in BAR. */
7689 static void
7690 x_create_toolkit_scroll_bar (f, bar)
7691 struct frame *f;
7692 struct scroll_bar *bar;
7694 Window xwindow;
7695 Widget widget;
7696 Arg av[20];
7697 int ac = 0;
7698 char *scroll_bar_name = "verticalScrollBar";
7699 unsigned long pixel;
7701 BLOCK_INPUT;
7703 #ifdef USE_MOTIF
7704 /* LessTif 0.85, problems:
7706 1. When the mouse if over the scroll bar, the scroll bar will
7707 get keyboard events. I didn't find a way to turn this off.
7709 2. Do we have to explicitly set the cursor to get an arrow
7710 cursor (see below)? */
7712 /* Set resources. Create the widget. */
7713 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7714 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7715 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7716 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7717 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7718 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7719 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7721 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7722 if (pixel != -1)
7724 XtSetArg (av[ac], XmNforeground, pixel);
7725 ++ac;
7728 pixel = f->output_data.x->scroll_bar_background_pixel;
7729 if (pixel != -1)
7731 XtSetArg (av[ac], XmNbackground, pixel);
7732 ++ac;
7735 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7736 scroll_bar_name, av, ac);
7738 /* Add one callback for everything that can happen. */
7739 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7740 (XtPointer) bar);
7741 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7742 (XtPointer) bar);
7743 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7744 (XtPointer) bar);
7745 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7746 (XtPointer) bar);
7747 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7748 (XtPointer) bar);
7749 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7750 (XtPointer) bar);
7751 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7752 (XtPointer) bar);
7754 /* Realize the widget. Only after that is the X window created. */
7755 XtRealizeWidget (widget);
7757 /* Set the cursor to an arrow. I didn't find a resource to do that.
7758 And I'm wondering why it hasn't an arrow cursor by default. */
7759 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7760 f->output_data.x->nontext_cursor);
7762 #else /* !USE_MOTIF i.e. use Xaw */
7764 /* Set resources. Create the widget. The background of the
7765 Xaw3d scroll bar widget is a little bit light for my taste.
7766 We don't alter it here to let users change it according
7767 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7768 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7769 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7770 /* For smoother scrolling with Xaw3d -sm */
7771 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7772 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
7774 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7775 if (pixel != -1)
7777 XtSetArg (av[ac], XtNforeground, pixel);
7778 ++ac;
7781 pixel = f->output_data.x->scroll_bar_background_pixel;
7782 if (pixel != -1)
7784 XtSetArg (av[ac], XtNbackground, pixel);
7785 ++ac;
7788 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7789 f->output_data.x->edit_widget, av, ac);
7792 char *initial = "";
7793 char *val = initial;
7794 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7795 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7796 if (val == initial)
7797 { /* ARROW_SCROLL */
7798 xaw3d_arrow_scroll = True;
7799 /* Isn't that just a personal preference ? -sm */
7800 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7804 /* Define callbacks. */
7805 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7806 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
7807 (XtPointer) bar);
7809 /* Realize the widget. Only after that is the X window created. */
7810 XtRealizeWidget (widget);
7812 #endif /* !USE_MOTIF */
7814 /* Install an action hook that let's us detect when the user
7815 finishes interacting with a scroll bar. */
7816 if (action_hook_id == 0)
7817 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7819 /* Remember X window and widget in the scroll bar vector. */
7820 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7821 xwindow = XtWindow (widget);
7822 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7824 UNBLOCK_INPUT;
7828 /* Set the thumb size and position of scroll bar BAR. We are currently
7829 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7831 static void
7832 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7833 struct scroll_bar *bar;
7834 int portion, position, whole;
7836 float top, shown;
7837 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7839 if (whole == 0)
7840 top = 0, shown = 1;
7841 else
7843 top = (float) position / whole;
7844 shown = (float) portion / whole;
7847 BLOCK_INPUT;
7849 #ifdef USE_MOTIF
7851 int size, value;
7852 Boolean arrow1_selected, arrow2_selected;
7853 unsigned char flags;
7854 XmScrollBarWidget sb;
7856 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
7857 is the scroll bar's maximum and MIN is the scroll bar's minimum
7858 value. */
7859 size = shown * XM_SB_RANGE;
7860 size = min (size, XM_SB_RANGE);
7861 size = max (size, 1);
7863 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7864 value = top * XM_SB_RANGE;
7865 value = min (value, XM_SB_MAX - size);
7866 value = max (value, XM_SB_MIN);
7868 /* LessTif: Calling XmScrollBarSetValues after an increment or
7869 decrement turns off auto-repeat LessTif-internally. This can
7870 be seen in ScrollBar.c which resets Arrow1Selected and
7871 Arrow2Selected. It also sets internal flags so that LessTif
7872 believes the mouse is in the slider. We either have to change
7873 our code, or work around that by accessing private data. */
7875 sb = (XmScrollBarWidget) widget;
7876 arrow1_selected = sb->scrollBar.arrow1_selected;
7877 arrow2_selected = sb->scrollBar.arrow2_selected;
7878 flags = sb->scrollBar.flags;
7880 if (NILP (bar->dragging))
7881 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7882 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7883 /* This has the negative side effect that the slider value is
7884 not what it would be if we scrolled here using line-wise or
7885 page-wise movement. */
7886 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7887 else
7889 /* If currently dragging, only update the slider size.
7890 This reduces flicker effects. */
7891 int old_value, old_size, increment, page_increment;
7893 XmScrollBarGetValues (widget, &old_value, &old_size,
7894 &increment, &page_increment);
7895 XmScrollBarSetValues (widget, old_value,
7896 min (size, XM_SB_RANGE - old_value),
7897 0, 0, False);
7900 sb->scrollBar.arrow1_selected = arrow1_selected;
7901 sb->scrollBar.arrow2_selected = arrow2_selected;
7902 sb->scrollBar.flags = flags;
7904 #else /* !USE_MOTIF i.e. use Xaw */
7906 float old_top, old_shown;
7907 Dimension height;
7908 XtVaGetValues (widget,
7909 XtNtopOfThumb, &old_top,
7910 XtNshown, &old_shown,
7911 XtNheight, &height,
7912 NULL);
7914 /* Massage the top+shown values. */
7915 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
7916 top = max (0, min (1, top));
7917 else
7918 top = old_top;
7919 /* Keep two pixels available for moving the thumb down. */
7920 shown = max (0, min (1 - top - (2.0 / height), shown));
7922 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
7923 check that your system's configuration file contains a define
7924 for `NARROWPROTO'. See s/freebsd.h for an example. */
7925 if (top != old_top || shown != old_shown)
7927 if (NILP (bar->dragging))
7928 XawScrollbarSetThumb (widget, top, shown);
7929 else
7931 #ifdef HAVE_XAW3D
7932 ScrollbarWidget sb = (ScrollbarWidget) widget;
7933 int scroll_mode = 0;
7935 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
7936 if (xaw3d_arrow_scroll)
7938 /* Xaw3d stupidly ignores resize requests while dragging
7939 so we have to make it believe it's not in dragging mode. */
7940 scroll_mode = sb->scrollbar.scroll_mode;
7941 if (scroll_mode == 2)
7942 sb->scrollbar.scroll_mode = 0;
7944 #endif
7945 /* Try to make the scrolling a tad smoother. */
7946 if (!xaw3d_pick_top)
7947 shown = min (shown, old_shown);
7949 XawScrollbarSetThumb (widget, top, shown);
7951 #ifdef HAVE_XAW3D
7952 if (xaw3d_arrow_scroll && scroll_mode == 2)
7953 sb->scrollbar.scroll_mode = scroll_mode;
7954 #endif
7958 #endif /* !USE_MOTIF */
7960 UNBLOCK_INPUT;
7963 #endif /* USE_TOOLKIT_SCROLL_BARS */
7967 /************************************************************************
7968 Scroll bars, general
7969 ************************************************************************/
7971 /* Create a scroll bar and return the scroll bar vector for it. W is
7972 the Emacs window on which to create the scroll bar. TOP, LEFT,
7973 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
7974 scroll bar. */
7976 static struct scroll_bar *
7977 x_scroll_bar_create (w, top, left, width, height)
7978 struct window *w;
7979 int top, left, width, height;
7981 struct frame *f = XFRAME (w->frame);
7982 struct scroll_bar *bar
7983 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
7985 BLOCK_INPUT;
7987 #if USE_TOOLKIT_SCROLL_BARS
7988 x_create_toolkit_scroll_bar (f, bar);
7989 #else /* not USE_TOOLKIT_SCROLL_BARS */
7991 XSetWindowAttributes a;
7992 unsigned long mask;
7993 Window window;
7995 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
7996 if (a.background_pixel == -1)
7997 a.background_pixel = f->output_data.x->background_pixel;
7999 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8000 | ButtonMotionMask | PointerMotionHintMask
8001 | ExposureMask);
8002 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8004 mask = (CWBackPixel | CWEventMask | CWCursor);
8006 /* Clear the area of W that will serve as a scroll bar. This is
8007 for the case that a window has been split horizontally. In
8008 this case, no clear_frame is generated to reduce flickering. */
8009 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8010 left, top, width,
8011 window_box_height (w), False);
8013 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8014 /* Position and size of scroll bar. */
8015 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8016 top,
8017 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8018 height,
8019 /* Border width, depth, class, and visual. */
8021 CopyFromParent,
8022 CopyFromParent,
8023 CopyFromParent,
8024 /* Attributes. */
8025 mask, &a);
8026 SET_SCROLL_BAR_X_WINDOW (bar, window);
8028 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8030 XSETWINDOW (bar->window, w);
8031 XSETINT (bar->top, top);
8032 XSETINT (bar->left, left);
8033 XSETINT (bar->width, width);
8034 XSETINT (bar->height, height);
8035 XSETINT (bar->start, 0);
8036 XSETINT (bar->end, 0);
8037 bar->dragging = Qnil;
8039 /* Add bar to its frame's list of scroll bars. */
8040 bar->next = FRAME_SCROLL_BARS (f);
8041 bar->prev = Qnil;
8042 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8043 if (!NILP (bar->next))
8044 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8046 /* Map the window/widget. */
8047 #if USE_TOOLKIT_SCROLL_BARS
8048 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8049 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8050 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8051 top,
8052 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8053 height, 0);
8054 #else /* not USE_TOOLKIT_SCROLL_BARS */
8055 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8056 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8058 UNBLOCK_INPUT;
8059 return bar;
8063 /* Draw BAR's handle in the proper position.
8065 If the handle is already drawn from START to END, don't bother
8066 redrawing it, unless REBUILD is non-zero; in that case, always
8067 redraw it. (REBUILD is handy for drawing the handle after expose
8068 events.)
8070 Normally, we want to constrain the start and end of the handle to
8071 fit inside its rectangle, but if the user is dragging the scroll
8072 bar handle, we want to let them drag it down all the way, so that
8073 the bar's top is as far down as it goes; otherwise, there's no way
8074 to move to the very end of the buffer. */
8076 #ifndef USE_TOOLKIT_SCROLL_BARS
8078 static void
8079 x_scroll_bar_set_handle (bar, start, end, rebuild)
8080 struct scroll_bar *bar;
8081 int start, end;
8082 int rebuild;
8084 int dragging = ! NILP (bar->dragging);
8085 Window w = SCROLL_BAR_X_WINDOW (bar);
8086 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8087 GC gc = f->output_data.x->normal_gc;
8089 /* If the display is already accurate, do nothing. */
8090 if (! rebuild
8091 && start == XINT (bar->start)
8092 && end == XINT (bar->end))
8093 return;
8095 BLOCK_INPUT;
8098 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8099 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8100 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8102 /* Make sure the values are reasonable, and try to preserve
8103 the distance between start and end. */
8105 int length = end - start;
8107 if (start < 0)
8108 start = 0;
8109 else if (start > top_range)
8110 start = top_range;
8111 end = start + length;
8113 if (end < start)
8114 end = start;
8115 else if (end > top_range && ! dragging)
8116 end = top_range;
8119 /* Store the adjusted setting in the scroll bar. */
8120 XSETINT (bar->start, start);
8121 XSETINT (bar->end, end);
8123 /* Clip the end position, just for display. */
8124 if (end > top_range)
8125 end = top_range;
8127 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8128 below top positions, to make sure the handle is always at least
8129 that many pixels tall. */
8130 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8132 /* Draw the empty space above the handle. Note that we can't clear
8133 zero-height areas; that means "clear to end of window." */
8134 if (0 < start)
8135 XClearArea (FRAME_X_DISPLAY (f), w,
8137 /* x, y, width, height, and exposures. */
8138 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8139 VERTICAL_SCROLL_BAR_TOP_BORDER,
8140 inside_width, start,
8141 False);
8143 /* Change to proper foreground color if one is specified. */
8144 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8145 XSetForeground (FRAME_X_DISPLAY (f), gc,
8146 f->output_data.x->scroll_bar_foreground_pixel);
8148 /* Draw the handle itself. */
8149 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8151 /* x, y, width, height */
8152 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8153 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8154 inside_width, end - start);
8156 /* Restore the foreground color of the GC if we changed it above. */
8157 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8158 XSetForeground (FRAME_X_DISPLAY (f), gc,
8159 f->output_data.x->foreground_pixel);
8161 /* Draw the empty space below the handle. Note that we can't
8162 clear zero-height areas; that means "clear to end of window." */
8163 if (end < inside_height)
8164 XClearArea (FRAME_X_DISPLAY (f), w,
8166 /* x, y, width, height, and exposures. */
8167 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8168 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8169 inside_width, inside_height - end,
8170 False);
8174 UNBLOCK_INPUT;
8177 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8179 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8180 nil. */
8182 static void
8183 x_scroll_bar_remove (bar)
8184 struct scroll_bar *bar;
8186 BLOCK_INPUT;
8188 #if USE_TOOLKIT_SCROLL_BARS
8189 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8190 #else /* not USE_TOOLKIT_SCROLL_BARS */
8192 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8193 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8195 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8197 /* Disassociate this scroll bar from its window. */
8198 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8200 UNBLOCK_INPUT;
8204 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8205 that we are displaying PORTION characters out of a total of WHOLE
8206 characters, starting at POSITION. If WINDOW has no scroll bar,
8207 create one. */
8209 static void
8210 XTset_vertical_scroll_bar (w, portion, whole, position)
8211 struct window *w;
8212 int portion, whole, position;
8214 struct frame *f = XFRAME (w->frame);
8215 struct scroll_bar *bar;
8216 int top, height, left, sb_left, width, sb_width;
8217 int window_x, window_y, window_width, window_height;
8219 /* Get window dimensions. */
8220 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8221 top = window_y;
8222 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8223 height = window_height;
8225 /* Compute the left edge of the scroll bar area. */
8226 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8227 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8228 else
8229 left = XFASTINT (w->left);
8230 left *= CANON_X_UNIT (f);
8231 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8233 /* Compute the width of the scroll bar which might be less than
8234 the width of the area reserved for the scroll bar. */
8235 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8236 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8237 else
8238 sb_width = width;
8240 /* Compute the left edge of the scroll bar. */
8241 #ifdef USE_TOOLKIT_SCROLL_BARS
8242 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8243 sb_left = left + width - sb_width - (width - sb_width) / 2;
8244 else
8245 sb_left = left + (width - sb_width) / 2;
8246 #else
8247 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8248 sb_left = left + width - sb_width;
8249 else
8250 sb_left = left;
8251 #endif
8253 /* Does the scroll bar exist yet? */
8254 if (NILP (w->vertical_scroll_bar))
8256 BLOCK_INPUT;
8257 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8258 left, top, width, height, False);
8259 UNBLOCK_INPUT;
8260 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8262 else
8264 /* It may just need to be moved and resized. */
8265 unsigned int mask = 0;
8267 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8269 BLOCK_INPUT;
8271 if (sb_left != XINT (bar->left))
8272 mask |= CWX;
8273 if (top != XINT (bar->top))
8274 mask |= CWY;
8275 if (sb_width != XINT (bar->width))
8276 mask |= CWWidth;
8277 if (height != XINT (bar->height))
8278 mask |= CWHeight;
8280 #ifdef USE_TOOLKIT_SCROLL_BARS
8282 /* Since toolkit scroll bars are smaller than the space reserved
8283 for them on the frame, we have to clear "under" them. */
8284 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8285 left, top, width, height, False);
8287 /* Move/size the scroll bar widget. */
8288 if (mask)
8289 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8290 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8291 top,
8292 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8293 height, 0);
8295 #else /* not USE_TOOLKIT_SCROLL_BARS */
8297 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8299 /* Clear areas not covered by the scroll bar. This makes sure a
8300 previous mode line display is cleared after C-x 2 C-x 1, for
8301 example. Non-toolkit scroll bars are as wide as the area
8302 reserved for scroll bars - trim at both sides. */
8303 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8304 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8305 height, False);
8306 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8307 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8308 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8309 height, False);
8312 /* Move/size the scroll bar window. */
8313 if (mask)
8315 XWindowChanges wc;
8317 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8318 wc.y = top;
8319 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8320 wc.height = height;
8321 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8322 mask, &wc);
8325 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8327 /* Remember new settings. */
8328 XSETINT (bar->left, sb_left);
8329 XSETINT (bar->top, top);
8330 XSETINT (bar->width, sb_width);
8331 XSETINT (bar->height, height);
8333 UNBLOCK_INPUT;
8336 #if USE_TOOLKIT_SCROLL_BARS
8337 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8338 #else /* not USE_TOOLKIT_SCROLL_BARS */
8339 /* Set the scroll bar's current state, unless we're currently being
8340 dragged. */
8341 if (NILP (bar->dragging))
8343 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8345 if (whole == 0)
8346 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8347 else
8349 int start = ((double) position * top_range) / whole;
8350 int end = ((double) (position + portion) * top_range) / whole;
8351 x_scroll_bar_set_handle (bar, start, end, 0);
8354 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8356 XSETVECTOR (w->vertical_scroll_bar, bar);
8360 /* The following three hooks are used when we're doing a thorough
8361 redisplay of the frame. We don't explicitly know which scroll bars
8362 are going to be deleted, because keeping track of when windows go
8363 away is a real pain - "Can you say set-window-configuration, boys
8364 and girls?" Instead, we just assert at the beginning of redisplay
8365 that *all* scroll bars are to be removed, and then save a scroll bar
8366 from the fiery pit when we actually redisplay its window. */
8368 /* Arrange for all scroll bars on FRAME to be removed at the next call
8369 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8370 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8372 static void
8373 XTcondemn_scroll_bars (frame)
8374 FRAME_PTR frame;
8376 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8377 while (! NILP (FRAME_SCROLL_BARS (frame)))
8379 Lisp_Object bar;
8380 bar = FRAME_SCROLL_BARS (frame);
8381 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8382 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8383 XSCROLL_BAR (bar)->prev = Qnil;
8384 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8385 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8386 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8390 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8391 Note that WINDOW isn't necessarily condemned at all. */
8392 static void
8393 XTredeem_scroll_bar (window)
8394 struct window *window;
8396 struct scroll_bar *bar;
8398 /* We can't redeem this window's scroll bar if it doesn't have one. */
8399 if (NILP (window->vertical_scroll_bar))
8400 abort ();
8402 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8404 /* Unlink it from the condemned list. */
8406 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8408 if (NILP (bar->prev))
8410 /* If the prev pointer is nil, it must be the first in one of
8411 the lists. */
8412 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8413 /* It's not condemned. Everything's fine. */
8414 return;
8415 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8416 window->vertical_scroll_bar))
8417 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8418 else
8419 /* If its prev pointer is nil, it must be at the front of
8420 one or the other! */
8421 abort ();
8423 else
8424 XSCROLL_BAR (bar->prev)->next = bar->next;
8426 if (! NILP (bar->next))
8427 XSCROLL_BAR (bar->next)->prev = bar->prev;
8429 bar->next = FRAME_SCROLL_BARS (f);
8430 bar->prev = Qnil;
8431 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8432 if (! NILP (bar->next))
8433 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8437 /* Remove all scroll bars on FRAME that haven't been saved since the
8438 last call to `*condemn_scroll_bars_hook'. */
8440 static void
8441 XTjudge_scroll_bars (f)
8442 FRAME_PTR f;
8444 Lisp_Object bar, next;
8446 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8448 /* Clear out the condemned list now so we won't try to process any
8449 more events on the hapless scroll bars. */
8450 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8452 for (; ! NILP (bar); bar = next)
8454 struct scroll_bar *b = XSCROLL_BAR (bar);
8456 x_scroll_bar_remove (b);
8458 next = b->next;
8459 b->next = b->prev = Qnil;
8462 /* Now there should be no references to the condemned scroll bars,
8463 and they should get garbage-collected. */
8467 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8468 is a no-op when using toolkit scroll bars.
8470 This may be called from a signal handler, so we have to ignore GC
8471 mark bits. */
8473 static void
8474 x_scroll_bar_expose (bar, event)
8475 struct scroll_bar *bar;
8476 XEvent *event;
8478 #ifndef USE_TOOLKIT_SCROLL_BARS
8480 Window w = SCROLL_BAR_X_WINDOW (bar);
8481 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8482 GC gc = f->output_data.x->normal_gc;
8483 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8485 BLOCK_INPUT;
8487 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8489 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8490 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8492 /* x, y, width, height */
8493 0, 0,
8494 XINT (bar->width) - 1 - width_trim - width_trim,
8495 XINT (bar->height) - 1);
8497 UNBLOCK_INPUT;
8499 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8502 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8503 is set to something other than no_event, it is enqueued.
8505 This may be called from a signal handler, so we have to ignore GC
8506 mark bits. */
8508 #ifndef USE_TOOLKIT_SCROLL_BARS
8510 static void
8511 x_scroll_bar_handle_click (bar, event, emacs_event)
8512 struct scroll_bar *bar;
8513 XEvent *event;
8514 struct input_event *emacs_event;
8516 if (! GC_WINDOWP (bar->window))
8517 abort ();
8519 emacs_event->kind = scroll_bar_click;
8520 emacs_event->code = event->xbutton.button - Button1;
8521 emacs_event->modifiers
8522 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8523 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8524 event->xbutton.state)
8525 | (event->type == ButtonRelease
8526 ? up_modifier
8527 : down_modifier));
8528 emacs_event->frame_or_window = bar->window;
8529 emacs_event->timestamp = event->xbutton.time;
8531 #if 0
8532 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8533 int internal_height
8534 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8535 #endif
8536 int top_range
8537 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8538 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8540 if (y < 0) y = 0;
8541 if (y > top_range) y = top_range;
8543 if (y < XINT (bar->start))
8544 emacs_event->part = scroll_bar_above_handle;
8545 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8546 emacs_event->part = scroll_bar_handle;
8547 else
8548 emacs_event->part = scroll_bar_below_handle;
8550 /* Just because the user has clicked on the handle doesn't mean
8551 they want to drag it. Lisp code needs to be able to decide
8552 whether or not we're dragging. */
8553 #if 0
8554 /* If the user has just clicked on the handle, record where they're
8555 holding it. */
8556 if (event->type == ButtonPress
8557 && emacs_event->part == scroll_bar_handle)
8558 XSETINT (bar->dragging, y - XINT (bar->start));
8559 #endif
8561 /* If the user has released the handle, set it to its final position. */
8562 if (event->type == ButtonRelease
8563 && ! NILP (bar->dragging))
8565 int new_start = y - XINT (bar->dragging);
8566 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8568 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8569 bar->dragging = Qnil;
8572 /* Same deal here as the other #if 0. */
8573 #if 0
8574 /* Clicks on the handle are always reported as occurring at the top of
8575 the handle. */
8576 if (emacs_event->part == scroll_bar_handle)
8577 emacs_event->x = bar->start;
8578 else
8579 XSETINT (emacs_event->x, y);
8580 #else
8581 XSETINT (emacs_event->x, y);
8582 #endif
8584 XSETINT (emacs_event->y, top_range);
8588 /* Handle some mouse motion while someone is dragging the scroll bar.
8590 This may be called from a signal handler, so we have to ignore GC
8591 mark bits. */
8593 static void
8594 x_scroll_bar_note_movement (bar, event)
8595 struct scroll_bar *bar;
8596 XEvent *event;
8598 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8600 last_mouse_movement_time = event->xmotion.time;
8602 f->mouse_moved = 1;
8603 XSETVECTOR (last_mouse_scroll_bar, bar);
8605 /* If we're dragging the bar, display it. */
8606 if (! GC_NILP (bar->dragging))
8608 /* Where should the handle be now? */
8609 int new_start = event->xmotion.y - XINT (bar->dragging);
8611 if (new_start != XINT (bar->start))
8613 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8615 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8620 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8622 /* Return information to the user about the current position of the mouse
8623 on the scroll bar. */
8625 static void
8626 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8627 FRAME_PTR *fp;
8628 Lisp_Object *bar_window;
8629 enum scroll_bar_part *part;
8630 Lisp_Object *x, *y;
8631 unsigned long *time;
8633 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8634 Window w = SCROLL_BAR_X_WINDOW (bar);
8635 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8636 int win_x, win_y;
8637 Window dummy_window;
8638 int dummy_coord;
8639 unsigned int dummy_mask;
8641 BLOCK_INPUT;
8643 /* Get the mouse's position relative to the scroll bar window, and
8644 report that. */
8645 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8647 /* Root, child, root x and root y. */
8648 &dummy_window, &dummy_window,
8649 &dummy_coord, &dummy_coord,
8651 /* Position relative to scroll bar. */
8652 &win_x, &win_y,
8654 /* Mouse buttons and modifier keys. */
8655 &dummy_mask))
8657 else
8659 #if 0
8660 int inside_height
8661 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8662 #endif
8663 int top_range
8664 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8666 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8668 if (! NILP (bar->dragging))
8669 win_y -= XINT (bar->dragging);
8671 if (win_y < 0)
8672 win_y = 0;
8673 if (win_y > top_range)
8674 win_y = top_range;
8676 *fp = f;
8677 *bar_window = bar->window;
8679 if (! NILP (bar->dragging))
8680 *part = scroll_bar_handle;
8681 else if (win_y < XINT (bar->start))
8682 *part = scroll_bar_above_handle;
8683 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8684 *part = scroll_bar_handle;
8685 else
8686 *part = scroll_bar_below_handle;
8688 XSETINT (*x, win_y);
8689 XSETINT (*y, top_range);
8691 f->mouse_moved = 0;
8692 last_mouse_scroll_bar = Qnil;
8695 *time = last_mouse_movement_time;
8697 UNBLOCK_INPUT;
8701 /* The screen has been cleared so we may have changed foreground or
8702 background colors, and the scroll bars may need to be redrawn.
8703 Clear out the scroll bars, and ask for expose events, so we can
8704 redraw them. */
8706 void
8707 x_scroll_bar_clear (f)
8708 FRAME_PTR f;
8710 #ifndef USE_TOOLKIT_SCROLL_BARS
8711 Lisp_Object bar;
8713 /* We can have scroll bars even if this is 0,
8714 if we just turned off scroll bar mode.
8715 But in that case we should not clear them. */
8716 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8717 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8718 bar = XSCROLL_BAR (bar)->next)
8719 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8720 0, 0, 0, 0, True);
8721 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8724 /* This processes Expose events from the menu-bar specific X event
8725 loop in xmenu.c. This allows to redisplay the frame if necessary
8726 when handling menu-bar or pop-up items. */
8729 process_expose_from_menu (event)
8730 XEvent event;
8732 FRAME_PTR f;
8733 struct x_display_info *dpyinfo;
8734 int frame_exposed_p = 0;
8736 BLOCK_INPUT;
8738 dpyinfo = x_display_info_for_display (event.xexpose.display);
8739 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8740 if (f)
8742 if (f->async_visible == 0)
8744 f->async_visible = 1;
8745 f->async_iconified = 0;
8746 f->output_data.x->has_been_visible = 1;
8747 SET_FRAME_GARBAGED (f);
8749 else
8751 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8752 event.xexpose.x, event.xexpose.y,
8753 event.xexpose.width, event.xexpose.height);
8754 frame_exposed_p = 1;
8757 else
8759 struct scroll_bar *bar
8760 = x_window_to_scroll_bar (event.xexpose.window);
8762 if (bar)
8763 x_scroll_bar_expose (bar, &event);
8766 UNBLOCK_INPUT;
8767 return frame_exposed_p;
8770 /* Define a queue to save up SelectionRequest events for later handling. */
8772 struct selection_event_queue
8774 XEvent event;
8775 struct selection_event_queue *next;
8778 static struct selection_event_queue *queue;
8780 /* Nonzero means queue up certain events--don't process them yet. */
8782 static int x_queue_selection_requests;
8784 /* Queue up an X event *EVENT, to be processed later. */
8786 static void
8787 x_queue_event (f, event)
8788 FRAME_PTR f;
8789 XEvent *event;
8791 struct selection_event_queue *queue_tmp
8792 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8794 if (queue_tmp != NULL)
8796 queue_tmp->event = *event;
8797 queue_tmp->next = queue;
8798 queue = queue_tmp;
8802 /* Take all the queued events and put them back
8803 so that they get processed afresh. */
8805 static void
8806 x_unqueue_events (display)
8807 Display *display;
8809 while (queue != NULL)
8811 struct selection_event_queue *queue_tmp = queue;
8812 XPutBackEvent (display, &queue_tmp->event);
8813 queue = queue_tmp->next;
8814 xfree ((char *)queue_tmp);
8818 /* Start queuing SelectionRequest events. */
8820 void
8821 x_start_queuing_selection_requests (display)
8822 Display *display;
8824 x_queue_selection_requests++;
8827 /* Stop queuing SelectionRequest events. */
8829 void
8830 x_stop_queuing_selection_requests (display)
8831 Display *display;
8833 x_queue_selection_requests--;
8834 x_unqueue_events (display);
8837 /* The main X event-reading loop - XTread_socket. */
8839 /* Time stamp of enter window event. This is only used by XTread_socket,
8840 but we have to put it out here, since static variables within functions
8841 sometimes don't work. */
8843 static Time enter_timestamp;
8845 /* This holds the state XLookupString needs to implement dead keys
8846 and other tricks known as "compose processing". _X Window System_
8847 says that a portable program can't use this, but Stephen Gildea assures
8848 me that letting the compiler initialize it to zeros will work okay.
8850 This must be defined outside of XTread_socket, for the same reasons
8851 given for enter_time stamp, above. */
8853 static XComposeStatus compose_status;
8855 /* Record the last 100 characters stored
8856 to help debug the loss-of-chars-during-GC problem. */
8858 static int temp_index;
8859 static short temp_buffer[100];
8861 /* Set this to nonzero to fake an "X I/O error"
8862 on a particular display. */
8864 struct x_display_info *XTread_socket_fake_io_error;
8866 /* When we find no input here, we occasionally do a no-op command
8867 to verify that the X server is still running and we can still talk with it.
8868 We try all the open displays, one by one.
8869 This variable is used for cycling thru the displays. */
8871 static struct x_display_info *next_noop_dpyinfo;
8873 #define SET_SAVED_MENU_EVENT(size) \
8874 do \
8876 if (f->output_data.x->saved_menu_event == 0) \
8877 f->output_data.x->saved_menu_event \
8878 = (XEvent *) xmalloc (sizeof (XEvent)); \
8879 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8880 if (numchars >= 1) \
8882 bufp->kind = menu_bar_activate_event; \
8883 XSETFRAME (bufp->frame_or_window, f); \
8884 bufp++; \
8885 count++; \
8886 numchars--; \
8889 while (0)
8891 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
8892 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
8894 /* Read events coming from the X server.
8895 This routine is called by the SIGIO handler.
8896 We return as soon as there are no more events to be read.
8898 Events representing keys are stored in buffer BUFP,
8899 which can hold up to NUMCHARS characters.
8900 We return the number of characters stored into the buffer,
8901 thus pretending to be `read'.
8903 EXPECTED is nonzero if the caller knows input is available. */
8906 XTread_socket (sd, bufp, numchars, expected)
8907 register int sd;
8908 /* register */ struct input_event *bufp;
8909 /* register */ int numchars;
8910 int expected;
8912 int count = 0;
8913 int nbytes = 0;
8914 XEvent event;
8915 struct frame *f;
8916 int event_found = 0;
8917 struct x_display_info *dpyinfo;
8919 if (interrupt_input_blocked)
8921 interrupt_input_pending = 1;
8922 return -1;
8925 interrupt_input_pending = 0;
8926 BLOCK_INPUT;
8928 /* So people can tell when we have read the available input. */
8929 input_signal_count++;
8931 if (numchars <= 0)
8932 abort (); /* Don't think this happens. */
8934 /* Find the display we are supposed to read input for.
8935 It's the one communicating on descriptor SD. */
8936 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
8938 #if 0 /* This ought to be unnecessary; let's verify it. */
8939 #ifdef FIOSNBIO
8940 /* If available, Xlib uses FIOSNBIO to make the socket
8941 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
8942 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
8943 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
8944 fcntl (dpyinfo->connection, F_SETFL, 0);
8945 #endif /* ! defined (FIOSNBIO) */
8946 #endif
8948 #if 0 /* This code can't be made to work, with multiple displays,
8949 and appears not to be used on any system any more.
8950 Also keyboard.c doesn't turn O_NDELAY on and off
8951 for X connections. */
8952 #ifndef SIGIO
8953 #ifndef HAVE_SELECT
8954 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
8956 extern int read_alarm_should_throw;
8957 read_alarm_should_throw = 1;
8958 XPeekEvent (dpyinfo->display, &event);
8959 read_alarm_should_throw = 0;
8961 #endif /* HAVE_SELECT */
8962 #endif /* SIGIO */
8963 #endif
8965 /* For debugging, this gives a way to fake an I/O error. */
8966 if (dpyinfo == XTread_socket_fake_io_error)
8968 XTread_socket_fake_io_error = 0;
8969 x_io_error_quitter (dpyinfo->display);
8972 while (XPending (dpyinfo->display))
8974 XNextEvent (dpyinfo->display, &event);
8976 #ifdef HAVE_X_I18N
8978 /* Filter events for the current X input method.
8979 XFilterEvent returns non-zero if the input method has
8980 consumed the event. We pass the frame's X window to
8981 XFilterEvent because that's the one for which the IC
8982 was created. */
8983 struct frame *f1 = x_any_window_to_frame (dpyinfo,
8984 event.xclient.window);
8985 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
8986 break;
8988 #endif
8989 event_found = 1;
8991 switch (event.type)
8993 case ClientMessage:
8995 if (event.xclient.message_type
8996 == dpyinfo->Xatom_wm_protocols
8997 && event.xclient.format == 32)
8999 if (event.xclient.data.l[0]
9000 == dpyinfo->Xatom_wm_take_focus)
9002 /* Use x_any_window_to_frame because this
9003 could be the shell widget window
9004 if the frame has no title bar. */
9005 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9006 #ifdef HAVE_X_I18N
9007 /* Not quite sure this is needed -pd */
9008 if (f && FRAME_XIC (f))
9009 XSetICFocus (FRAME_XIC (f));
9010 #endif
9011 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9012 instructs the WM to set the input focus automatically for
9013 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9014 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9015 it has set the focus. So, XSetInputFocus below is not
9016 needed.
9018 The call to XSetInputFocus below has also caused trouble. In
9019 cases where the XSetInputFocus done by the WM and the one
9020 below are temporally close (on a fast machine), the call
9021 below can generate additional FocusIn events which confuse
9022 Emacs. */
9024 /* Since we set WM_TAKE_FOCUS, we must call
9025 XSetInputFocus explicitly. But not if f is null,
9026 since that might be an event for a deleted frame. */
9027 if (f)
9029 Display *d = event.xclient.display;
9030 /* Catch and ignore errors, in case window has been
9031 iconified by a window manager such as GWM. */
9032 int count = x_catch_errors (d);
9033 XSetInputFocus (d, event.xclient.window,
9034 /* The ICCCM says this is
9035 the only valid choice. */
9036 RevertToParent,
9037 event.xclient.data.l[1]);
9038 /* This is needed to detect the error
9039 if there is an error. */
9040 XSync (d, False);
9041 x_uncatch_errors (d, count);
9043 /* Not certain about handling scroll bars here */
9044 #endif /* 0 */
9046 else if (event.xclient.data.l[0]
9047 == dpyinfo->Xatom_wm_save_yourself)
9049 /* Save state modify the WM_COMMAND property to
9050 something which can reinstate us. This notifies
9051 the session manager, who's looking for such a
9052 PropertyNotify. Can restart processing when
9053 a keyboard or mouse event arrives. */
9054 if (numchars > 0)
9056 f = x_top_window_to_frame (dpyinfo,
9057 event.xclient.window);
9059 /* This is just so we only give real data once
9060 for a single Emacs process. */
9061 if (f == SELECTED_FRAME ())
9062 XSetCommand (FRAME_X_DISPLAY (f),
9063 event.xclient.window,
9064 initial_argv, initial_argc);
9065 else if (f)
9066 XSetCommand (FRAME_X_DISPLAY (f),
9067 event.xclient.window,
9068 0, 0);
9071 else if (event.xclient.data.l[0]
9072 == dpyinfo->Xatom_wm_delete_window)
9074 struct frame *f
9075 = x_any_window_to_frame (dpyinfo,
9076 event.xclient.window);
9078 if (f)
9080 if (numchars == 0)
9081 abort ();
9083 bufp->kind = delete_window_event;
9084 XSETFRAME (bufp->frame_or_window, f);
9085 bufp++;
9087 count += 1;
9088 numchars -= 1;
9092 else if (event.xclient.message_type
9093 == dpyinfo->Xatom_wm_configure_denied)
9096 else if (event.xclient.message_type
9097 == dpyinfo->Xatom_wm_window_moved)
9099 int new_x, new_y;
9100 struct frame *f
9101 = x_window_to_frame (dpyinfo, event.xclient.window);
9103 new_x = event.xclient.data.s[0];
9104 new_y = event.xclient.data.s[1];
9106 if (f)
9108 f->output_data.x->left_pos = new_x;
9109 f->output_data.x->top_pos = new_y;
9112 #ifdef HACK_EDITRES
9113 else if (event.xclient.message_type
9114 == dpyinfo->Xatom_editres)
9116 struct frame *f
9117 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9118 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9119 &event, NULL);
9121 #endif /* HACK_EDITRES */
9122 else if ((event.xclient.message_type
9123 == dpyinfo->Xatom_DONE)
9124 || (event.xclient.message_type
9125 == dpyinfo->Xatom_PAGE))
9127 /* Ghostview job completed. Kill it. We could
9128 reply with "Next" if we received "Page", but we
9129 currently never do because we are interested in
9130 images, only, which should have 1 page. */
9131 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9132 struct frame *f
9133 = x_window_to_frame (dpyinfo, event.xclient.window);
9134 x_kill_gs_process (pixmap, f);
9135 expose_frame (f, 0, 0, 0, 0);
9137 #ifdef USE_TOOLKIT_SCROLL_BARS
9138 /* Scroll bar callbacks send a ClientMessage from which
9139 we construct an input_event. */
9140 else if (event.xclient.message_type
9141 == dpyinfo->Xatom_Scrollbar)
9143 x_scroll_bar_to_input_event (&event, bufp);
9144 ++bufp, ++count, --numchars;
9145 goto out;
9147 #endif /* USE_TOOLKIT_SCROLL_BARS */
9148 else
9149 goto OTHER;
9151 break;
9153 case SelectionNotify:
9154 #ifdef USE_X_TOOLKIT
9155 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9156 goto OTHER;
9157 #endif /* not USE_X_TOOLKIT */
9158 x_handle_selection_notify (&event.xselection);
9159 break;
9161 case SelectionClear: /* Someone has grabbed ownership. */
9162 #ifdef USE_X_TOOLKIT
9163 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9164 goto OTHER;
9165 #endif /* USE_X_TOOLKIT */
9167 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9169 if (numchars == 0)
9170 abort ();
9172 bufp->kind = selection_clear_event;
9173 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9174 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9175 SELECTION_EVENT_TIME (bufp) = eventp->time;
9176 bufp->frame_or_window = Qnil;
9177 bufp++;
9179 count += 1;
9180 numchars -= 1;
9182 break;
9184 case SelectionRequest: /* Someone wants our selection. */
9185 #ifdef USE_X_TOOLKIT
9186 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9187 goto OTHER;
9188 #endif /* USE_X_TOOLKIT */
9189 if (x_queue_selection_requests)
9190 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9191 &event);
9192 else
9194 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9196 if (numchars == 0)
9197 abort ();
9199 bufp->kind = selection_request_event;
9200 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9201 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9202 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9203 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9204 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9205 SELECTION_EVENT_TIME (bufp) = eventp->time;
9206 bufp->frame_or_window = Qnil;
9207 bufp++;
9209 count += 1;
9210 numchars -= 1;
9212 break;
9214 case PropertyNotify:
9215 #ifdef USE_X_TOOLKIT
9216 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9217 goto OTHER;
9218 #endif /* not USE_X_TOOLKIT */
9219 x_handle_property_notify (&event.xproperty);
9220 break;
9222 case ReparentNotify:
9223 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9224 if (f)
9226 int x, y;
9227 f->output_data.x->parent_desc = event.xreparent.parent;
9228 x_real_positions (f, &x, &y);
9229 f->output_data.x->left_pos = x;
9230 f->output_data.x->top_pos = y;
9232 break;
9234 case Expose:
9235 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9236 if (f)
9238 if (f->async_visible == 0)
9240 f->async_visible = 1;
9241 f->async_iconified = 0;
9242 f->output_data.x->has_been_visible = 1;
9243 SET_FRAME_GARBAGED (f);
9245 else
9246 expose_frame (x_window_to_frame (dpyinfo,
9247 event.xexpose.window),
9248 event.xexpose.x, event.xexpose.y,
9249 event.xexpose.width, event.xexpose.height);
9251 else
9253 #ifdef USE_TOOLKIT_SCROLL_BARS
9254 /* Dispatch event to the widget. */
9255 goto OTHER;
9256 #else /* not USE_TOOLKIT_SCROLL_BARS */
9257 struct scroll_bar *bar
9258 = x_window_to_scroll_bar (event.xexpose.window);
9260 if (bar)
9261 x_scroll_bar_expose (bar, &event);
9262 #ifdef USE_X_TOOLKIT
9263 else
9264 goto OTHER;
9265 #endif /* USE_X_TOOLKIT */
9266 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9268 break;
9270 case GraphicsExpose: /* This occurs when an XCopyArea's
9271 source area was obscured or not
9272 available.*/
9273 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9274 if (f)
9276 expose_frame (f,
9277 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9278 event.xgraphicsexpose.width,
9279 event.xgraphicsexpose.height);
9281 #ifdef USE_X_TOOLKIT
9282 else
9283 goto OTHER;
9284 #endif /* USE_X_TOOLKIT */
9285 break;
9287 case NoExpose: /* This occurs when an XCopyArea's
9288 source area was completely
9289 available */
9290 break;
9292 case UnmapNotify:
9293 /* Redo the mouse-highlight after the tooltip has gone. */
9294 if (event.xmap.window == tip_window)
9296 tip_window = 0;
9297 redo_mouse_highlight ();
9300 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9301 if (f) /* F may no longer exist if
9302 the frame was deleted. */
9304 /* While a frame is unmapped, display generation is
9305 disabled; you don't want to spend time updating a
9306 display that won't ever be seen. */
9307 f->async_visible = 0;
9308 /* We can't distinguish, from the event, whether the window
9309 has become iconified or invisible. So assume, if it
9310 was previously visible, than now it is iconified.
9311 But x_make_frame_invisible clears both
9312 the visible flag and the iconified flag;
9313 and that way, we know the window is not iconified now. */
9314 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9316 f->async_iconified = 1;
9318 bufp->kind = iconify_event;
9319 XSETFRAME (bufp->frame_or_window, f);
9320 bufp++;
9321 count++;
9322 numchars--;
9325 goto OTHER;
9327 case MapNotify:
9328 if (event.xmap.window == tip_window)
9329 /* The tooltip has been drawn already. Avoid
9330 the SET_FRAME_GARBAGED below. */
9331 goto OTHER;
9333 /* We use x_top_window_to_frame because map events can
9334 come for sub-windows and they don't mean that the
9335 frame is visible. */
9336 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9337 if (f)
9339 f->async_visible = 1;
9340 f->async_iconified = 0;
9341 f->output_data.x->has_been_visible = 1;
9343 /* wait_reading_process_input will notice this and update
9344 the frame's display structures. */
9345 SET_FRAME_GARBAGED (f);
9347 if (f->iconified)
9349 bufp->kind = deiconify_event;
9350 XSETFRAME (bufp->frame_or_window, f);
9351 bufp++;
9352 count++;
9353 numchars--;
9355 else if (! NILP (Vframe_list)
9356 && ! NILP (XCDR (Vframe_list)))
9357 /* Force a redisplay sooner or later
9358 to update the frame titles
9359 in case this is the second frame. */
9360 record_asynch_buffer_change ();
9362 goto OTHER;
9364 case KeyPress:
9365 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9367 #ifdef USE_MOTIF
9368 /* I couldn't find a way to prevent LessTif scroll bars
9369 from consuming key events. */
9370 if (f == 0)
9372 Widget widget = XtWindowToWidget (dpyinfo->display,
9373 event.xkey.window);
9374 if (widget && XmIsScrollBar (widget))
9376 widget = XtParent (widget);
9377 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9380 #endif /* USE_MOTIF */
9382 if (f != 0)
9384 KeySym keysym, orig_keysym;
9385 /* al%imercury@uunet.uu.net says that making this 81 instead of
9386 80 fixed a bug whereby meta chars made his Emacs hang. */
9387 unsigned char copy_buffer[81];
9388 int modifiers;
9390 event.xkey.state
9391 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9392 extra_keyboard_modifiers);
9393 modifiers = event.xkey.state;
9395 /* This will have to go some day... */
9397 /* make_lispy_event turns chars into control chars.
9398 Don't do it here because XLookupString is too eager. */
9399 event.xkey.state &= ~ControlMask;
9400 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9401 | dpyinfo->super_mod_mask
9402 | dpyinfo->hyper_mod_mask
9403 | dpyinfo->alt_mod_mask);
9405 /* In case Meta is ComposeCharacter,
9406 clear its status. According to Markus Ehrnsperger
9407 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9408 this enables ComposeCharacter to work whether or
9409 not it is combined with Meta. */
9410 if (modifiers & dpyinfo->meta_mod_mask)
9411 bzero (&compose_status, sizeof (compose_status));
9413 #ifdef HAVE_X_I18N
9414 if (FRAME_XIC (f))
9416 unsigned char *copy_bufptr = copy_buffer;
9417 int copy_bufsiz = sizeof (copy_buffer);
9418 Status status_return;
9420 nbytes = XmbLookupString (FRAME_XIC (f),
9421 &event.xkey, copy_bufptr,
9422 copy_bufsiz, &keysym,
9423 &status_return);
9424 if (status_return == XBufferOverflow)
9426 copy_bufsiz = nbytes + 1;
9427 copy_bufptr = (char *) alloca (copy_bufsiz);
9428 nbytes = XmbLookupString (FRAME_XIC (f),
9429 &event.xkey, copy_bufptr,
9430 copy_bufsiz, &keysym,
9431 &status_return);
9434 if (status_return == XLookupNone)
9435 break;
9436 else if (status_return == XLookupChars)
9438 keysym = NoSymbol;
9439 modifiers = 0;
9441 else if (status_return != XLookupKeySym
9442 && status_return != XLookupBoth)
9443 abort ();
9445 else
9446 nbytes = XLookupString (&event.xkey, copy_buffer,
9447 80, &keysym, &compose_status);
9448 #else
9449 nbytes = XLookupString (&event.xkey, copy_buffer,
9450 80, &keysym, &compose_status);
9451 #endif
9453 orig_keysym = keysym;
9455 if (numchars > 1)
9457 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9458 || keysym == XK_Delete
9459 #ifdef XK_ISO_Left_Tab
9460 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9461 #endif
9462 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9463 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9464 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9465 #ifdef HPUX
9466 /* This recognizes the "extended function keys".
9467 It seems there's no cleaner way.
9468 Test IsModifierKey to avoid handling mode_switch
9469 incorrectly. */
9470 || ((unsigned) (keysym) >= XK_Select
9471 && (unsigned)(keysym) < XK_KP_Space)
9472 #endif
9473 #ifdef XK_dead_circumflex
9474 || orig_keysym == XK_dead_circumflex
9475 #endif
9476 #ifdef XK_dead_grave
9477 || orig_keysym == XK_dead_grave
9478 #endif
9479 #ifdef XK_dead_tilde
9480 || orig_keysym == XK_dead_tilde
9481 #endif
9482 #ifdef XK_dead_diaeresis
9483 || orig_keysym == XK_dead_diaeresis
9484 #endif
9485 #ifdef XK_dead_macron
9486 || orig_keysym == XK_dead_macron
9487 #endif
9488 #ifdef XK_dead_degree
9489 || orig_keysym == XK_dead_degree
9490 #endif
9491 #ifdef XK_dead_acute
9492 || orig_keysym == XK_dead_acute
9493 #endif
9494 #ifdef XK_dead_cedilla
9495 || orig_keysym == XK_dead_cedilla
9496 #endif
9497 #ifdef XK_dead_breve
9498 || orig_keysym == XK_dead_breve
9499 #endif
9500 #ifdef XK_dead_ogonek
9501 || orig_keysym == XK_dead_ogonek
9502 #endif
9503 #ifdef XK_dead_caron
9504 || orig_keysym == XK_dead_caron
9505 #endif
9506 #ifdef XK_dead_doubleacute
9507 || orig_keysym == XK_dead_doubleacute
9508 #endif
9509 #ifdef XK_dead_abovedot
9510 || orig_keysym == XK_dead_abovedot
9511 #endif
9512 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9513 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9514 /* Any "vendor-specific" key is ok. */
9515 || (orig_keysym & (1 << 28)))
9516 && ! (IsModifierKey (orig_keysym)
9517 #ifndef HAVE_X11R5
9518 #ifdef XK_Mode_switch
9519 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9520 #endif
9521 #ifdef XK_Num_Lock
9522 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9523 #endif
9524 #endif /* not HAVE_X11R5 */
9527 if (temp_index == sizeof temp_buffer / sizeof (short))
9528 temp_index = 0;
9529 temp_buffer[temp_index++] = keysym;
9530 bufp->kind = non_ascii_keystroke;
9531 bufp->code = keysym;
9532 XSETFRAME (bufp->frame_or_window, f);
9533 bufp->modifiers
9534 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9535 modifiers);
9536 bufp->timestamp = event.xkey.time;
9537 bufp++;
9538 count++;
9539 numchars--;
9541 else if (numchars > nbytes)
9543 register int i;
9545 for (i = 0; i < nbytes; i++)
9547 if (temp_index == sizeof temp_buffer / sizeof (short))
9548 temp_index = 0;
9549 temp_buffer[temp_index++] = copy_buffer[i];
9550 bufp->kind = ascii_keystroke;
9551 bufp->code = copy_buffer[i];
9552 XSETFRAME (bufp->frame_or_window, f);
9553 bufp->modifiers
9554 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9555 modifiers);
9556 bufp->timestamp = event.xkey.time;
9557 bufp++;
9560 count += nbytes;
9561 numchars -= nbytes;
9563 if (keysym == NoSymbol)
9564 break;
9566 else
9567 abort ();
9569 else
9570 abort ();
9572 #ifdef HAVE_X_I18N
9573 /* Don't dispatch this event since XtDispatchEvent calls
9574 XFilterEvent, and two calls in a row may freeze the
9575 client. */
9576 break;
9577 #else
9578 goto OTHER;
9579 #endif
9581 case KeyRelease:
9582 #ifdef HAVE_X_I18N
9583 /* Don't dispatch this event since XtDispatchEvent calls
9584 XFilterEvent, and two calls in a row may freeze the
9585 client. */
9586 break;
9587 #else
9588 goto OTHER;
9589 #endif
9591 /* Here's a possible interpretation of the whole
9592 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9593 you get a FocusIn event, you have to get a FocusOut
9594 event before you relinquish the focus. If you
9595 haven't received a FocusIn event, then a mere
9596 LeaveNotify is enough to free you. */
9598 case EnterNotify:
9600 int from_menu_bar_p = 0;
9602 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9604 #ifdef LESSTIF_VERSION
9605 /* When clicking outside of a menu bar popup to close
9606 it, we get a FocusIn/ EnterNotify sequence of
9607 events. The flag event.xcrossing.focus is not set
9608 in the EnterNotify event of that sequence because
9609 the focus is in the menu bar,
9610 event.xcrossing.window is the frame's X window.
9611 Unconditionally setting the focus frame to null in
9612 this case is not the right thing, because no event
9613 follows that could set the focus frame to the right
9614 value.
9616 This could be a LessTif bug, but I wasn't able to
9617 reproduce the behavior in a simple test program.
9619 (gerd, LessTif 0.88.1). */
9621 if (!event.xcrossing.focus
9622 && f
9623 && f->output_data.x->menubar_widget)
9625 Window focus;
9626 int revert;
9628 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9629 if (focus == XtWindow (f->output_data.x->menubar_widget))
9630 from_menu_bar_p = 1;
9632 #endif /* LESSTIF_VERSION */
9634 if (event.xcrossing.focus || from_menu_bar_p)
9636 /* Avoid nasty pop/raise loops. */
9637 if (f && (!(f->auto_raise)
9638 || !(f->auto_lower)
9639 || (event.xcrossing.time - enter_timestamp) > 500))
9641 x_new_focus_frame (dpyinfo, f);
9642 enter_timestamp = event.xcrossing.time;
9645 else if (f == dpyinfo->x_focus_frame)
9646 x_new_focus_frame (dpyinfo, 0);
9648 /* EnterNotify counts as mouse movement,
9649 so update things that depend on mouse position. */
9650 if (f && !f->output_data.x->busy_p)
9651 note_mouse_movement (f, &event.xmotion);
9652 goto OTHER;
9655 case FocusIn:
9656 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9657 if (event.xfocus.detail != NotifyPointer)
9658 dpyinfo->x_focus_event_frame = f;
9659 if (f)
9661 x_new_focus_frame (dpyinfo, f);
9663 /* Don't stop displaying the initial startup message
9664 for a switch-frame event we don't need. */
9665 if (GC_NILP (Vterminal_frame)
9666 && GC_CONSP (Vframe_list)
9667 && !GC_NILP (XCDR (Vframe_list)))
9669 bufp->kind = FOCUS_IN_EVENT;
9670 XSETFRAME (bufp->frame_or_window, f);
9671 ++bufp, ++count, --numchars;
9675 #ifdef HAVE_X_I18N
9676 if (f && FRAME_XIC (f))
9677 XSetICFocus (FRAME_XIC (f));
9678 #endif
9680 goto OTHER;
9682 case LeaveNotify:
9683 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9684 if (f)
9686 Lisp_Object frame;
9687 int from_menu_bar_p = 0;
9689 if (f == dpyinfo->mouse_face_mouse_frame)
9691 /* If we move outside the frame, then we're
9692 certainly no longer on any text in the frame. */
9693 clear_mouse_face (dpyinfo);
9694 dpyinfo->mouse_face_mouse_frame = 0;
9697 /* Generate a nil HELP_EVENT to cancel a help-echo.
9698 Do it only if there's something to cancel.
9699 Otherwise, the startup message is cleared when
9700 the mouse leaves the frame. */
9701 if (any_help_event_p)
9703 XSETFRAME (frame, f);
9704 bufp->kind = HELP_EVENT;
9705 bufp->frame_or_window = Fcons (frame, Qnil);
9706 ++bufp, ++count, --numchars;
9709 #ifdef LESSTIF_VERSION
9710 /* Please see the comment at the start of the
9711 EnterNotify case. */
9712 if (!event.xcrossing.focus
9713 && f->output_data.x->menubar_widget)
9715 Window focus;
9716 int revert;
9717 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9718 if (focus == XtWindow (f->output_data.x->menubar_widget))
9719 from_menu_bar_p = 1;
9721 #endif /* LESSTIF_VERSION */
9723 if (event.xcrossing.focus || from_menu_bar_p)
9724 x_mouse_leave (dpyinfo);
9725 else
9727 if (f == dpyinfo->x_focus_event_frame)
9728 dpyinfo->x_focus_event_frame = 0;
9729 if (f == dpyinfo->x_focus_frame)
9730 x_new_focus_frame (dpyinfo, 0);
9733 goto OTHER;
9735 case FocusOut:
9736 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9737 if (event.xfocus.detail != NotifyPointer
9738 && f == dpyinfo->x_focus_event_frame)
9739 dpyinfo->x_focus_event_frame = 0;
9740 if (f && f == dpyinfo->x_focus_frame)
9741 x_new_focus_frame (dpyinfo, 0);
9743 #ifdef HAVE_X_I18N
9744 if (f && FRAME_XIC (f))
9745 XUnsetICFocus (FRAME_XIC (f));
9746 #endif
9748 goto OTHER;
9750 case MotionNotify:
9752 previous_help_echo = help_echo;
9753 help_echo = Qnil;
9755 if (dpyinfo->grabbed && last_mouse_frame
9756 && FRAME_LIVE_P (last_mouse_frame))
9757 f = last_mouse_frame;
9758 else
9759 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9761 if (f)
9762 note_mouse_movement (f, &event.xmotion);
9763 else
9765 #ifndef USE_TOOLKIT_SCROLL_BARS
9766 struct scroll_bar *bar
9767 = x_window_to_scroll_bar (event.xmotion.window);
9769 if (bar)
9770 x_scroll_bar_note_movement (bar, &event);
9771 #endif /* USE_TOOLKIT_SCROLL_BARS */
9773 /* If we move outside the frame, then we're
9774 certainly no longer on any text in the frame. */
9775 clear_mouse_face (dpyinfo);
9778 /* If the contents of the global variable help_echo
9779 has changed, generate a HELP_EVENT. */
9780 if (STRINGP (help_echo)
9781 || STRINGP (previous_help_echo))
9783 Lisp_Object frame;
9785 if (f)
9786 XSETFRAME (frame, f);
9787 else
9788 frame = Qnil;
9790 any_help_event_p = 1;
9791 bufp->kind = HELP_EVENT;
9792 bufp->frame_or_window = Fcons (frame, help_echo);
9793 ++bufp, ++count, --numchars;
9796 goto OTHER;
9799 case ConfigureNotify:
9800 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9801 if (f)
9803 #ifndef USE_X_TOOLKIT
9804 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9805 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
9807 /* In the toolkit version, change_frame_size
9808 is called by the code that handles resizing
9809 of the EmacsFrame widget. */
9811 /* Even if the number of character rows and columns has
9812 not changed, the font size may have changed, so we need
9813 to check the pixel dimensions as well. */
9814 if (columns != f->width
9815 || rows != f->height
9816 || event.xconfigure.width != f->output_data.x->pixel_width
9817 || event.xconfigure.height != f->output_data.x->pixel_height)
9819 change_frame_size (f, rows, columns, 0, 1, 0);
9820 SET_FRAME_GARBAGED (f);
9821 cancel_mouse_face (f);
9823 #endif
9825 f->output_data.x->pixel_width = event.xconfigure.width;
9826 f->output_data.x->pixel_height = event.xconfigure.height;
9828 /* What we have now is the position of Emacs's own window.
9829 Convert that to the position of the window manager window. */
9830 x_real_positions (f, &f->output_data.x->left_pos,
9831 &f->output_data.x->top_pos);
9833 #ifdef HAVE_X_I18N
9834 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9835 xic_set_statusarea (f);
9836 #endif
9838 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9840 /* Since the WM decorations come below top_pos now,
9841 we must put them below top_pos in the future. */
9842 f->output_data.x->win_gravity = NorthWestGravity;
9843 x_wm_set_size_hint (f, (long) 0, 0);
9845 #ifdef USE_MOTIF
9846 /* Some window managers pass (0,0) as the location of
9847 the window, and the Motif event handler stores it
9848 in the emacs widget, which messes up Motif menus. */
9849 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9851 event.xconfigure.x = f->output_data.x->widget->core.x;
9852 event.xconfigure.y = f->output_data.x->widget->core.y;
9854 #endif /* USE_MOTIF */
9856 goto OTHER;
9858 case ButtonPress:
9859 case ButtonRelease:
9861 /* If we decide we want to generate an event to be seen
9862 by the rest of Emacs, we put it here. */
9863 struct input_event emacs_event;
9864 int tool_bar_p = 0;
9866 emacs_event.kind = no_event;
9867 bzero (&compose_status, sizeof (compose_status));
9869 if (dpyinfo->grabbed
9870 && last_mouse_frame
9871 && FRAME_LIVE_P (last_mouse_frame))
9872 f = last_mouse_frame;
9873 else
9874 f = x_window_to_frame (dpyinfo, event.xbutton.window);
9876 if (f)
9878 /* Is this in the tool-bar? */
9879 if (WINDOWP (f->tool_bar_window)
9880 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
9882 Lisp_Object window;
9883 int p, x, y;
9885 x = event.xbutton.x;
9886 y = event.xbutton.y;
9888 /* Set x and y. */
9889 window = window_from_coordinates (f, x, y, &p, 1);
9890 if (EQ (window, f->tool_bar_window))
9892 x_handle_tool_bar_click (f, &event.xbutton);
9893 tool_bar_p = 1;
9897 if (!tool_bar_p)
9898 if (!dpyinfo->x_focus_frame
9899 || f == dpyinfo->x_focus_frame)
9900 construct_mouse_click (&emacs_event, &event, f);
9902 else
9904 #ifndef USE_TOOLKIT_SCROLL_BARS
9905 struct scroll_bar *bar
9906 = x_window_to_scroll_bar (event.xbutton.window);
9908 if (bar)
9909 x_scroll_bar_handle_click (bar, &event, &emacs_event);
9910 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9913 if (event.type == ButtonPress)
9915 dpyinfo->grabbed |= (1 << event.xbutton.button);
9916 last_mouse_frame = f;
9917 /* Ignore any mouse motion that happened
9918 before this event; any subsequent mouse-movement
9919 Emacs events should reflect only motion after
9920 the ButtonPress. */
9921 if (f != 0)
9922 f->mouse_moved = 0;
9924 if (!tool_bar_p)
9925 last_tool_bar_item = -1;
9927 else
9929 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
9932 if (numchars >= 1 && emacs_event.kind != no_event)
9934 bcopy (&emacs_event, bufp, sizeof (struct input_event));
9935 bufp++;
9936 count++;
9937 numchars--;
9940 #ifdef USE_X_TOOLKIT
9941 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
9942 /* For a down-event in the menu bar,
9943 don't pass it to Xt right now.
9944 Instead, save it away
9945 and we will pass it to Xt from kbd_buffer_get_event.
9946 That way, we can run some Lisp code first. */
9947 if (f && event.type == ButtonPress
9948 /* Verify the event is really within the menu bar
9949 and not just sent to it due to grabbing. */
9950 && event.xbutton.x >= 0
9951 && event.xbutton.x < f->output_data.x->pixel_width
9952 && event.xbutton.y >= 0
9953 && event.xbutton.y < f->output_data.x->menubar_height
9954 && event.xbutton.same_screen)
9956 SET_SAVED_BUTTON_EVENT;
9957 XSETFRAME (last_mouse_press_frame, f);
9959 else if (event.type == ButtonPress)
9961 last_mouse_press_frame = Qnil;
9962 goto OTHER;
9965 #ifdef USE_MOTIF /* This should do not harm for Lucid,
9966 but I am trying to be cautious. */
9967 else if (event.type == ButtonRelease)
9969 if (!NILP (last_mouse_press_frame))
9971 f = XFRAME (last_mouse_press_frame);
9972 if (f->output_data.x)
9973 SET_SAVED_BUTTON_EVENT;
9975 else
9976 goto OTHER;
9978 #endif /* USE_MOTIF */
9979 else
9980 goto OTHER;
9981 #endif /* USE_X_TOOLKIT */
9983 break;
9985 case CirculateNotify:
9986 goto OTHER;
9988 case CirculateRequest:
9989 goto OTHER;
9991 case VisibilityNotify:
9992 goto OTHER;
9994 case MappingNotify:
9995 /* Someone has changed the keyboard mapping - update the
9996 local cache. */
9997 switch (event.xmapping.request)
9999 case MappingModifier:
10000 x_find_modifier_meanings (dpyinfo);
10001 /* This is meant to fall through. */
10002 case MappingKeyboard:
10003 XRefreshKeyboardMapping (&event.xmapping);
10005 goto OTHER;
10007 default:
10008 OTHER:
10009 #ifdef USE_X_TOOLKIT
10010 BLOCK_INPUT;
10011 XtDispatchEvent (&event);
10012 UNBLOCK_INPUT;
10013 #endif /* USE_X_TOOLKIT */
10014 break;
10019 out:;
10021 /* On some systems, an X bug causes Emacs to get no more events
10022 when the window is destroyed. Detect that. (1994.) */
10023 if (! event_found)
10025 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10026 One XNOOP in 100 loops will make Emacs terminate.
10027 B. Bretthauer, 1994 */
10028 x_noop_count++;
10029 if (x_noop_count >= 100)
10031 x_noop_count=0;
10033 if (next_noop_dpyinfo == 0)
10034 next_noop_dpyinfo = x_display_list;
10036 XNoOp (next_noop_dpyinfo->display);
10038 /* Each time we get here, cycle through the displays now open. */
10039 next_noop_dpyinfo = next_noop_dpyinfo->next;
10043 /* If the focus was just given to an auto-raising frame,
10044 raise it now. */
10045 /* ??? This ought to be able to handle more than one such frame. */
10046 if (pending_autoraise_frame)
10048 x_raise_frame (pending_autoraise_frame);
10049 pending_autoraise_frame = 0;
10052 UNBLOCK_INPUT;
10053 return count;
10059 /***********************************************************************
10060 Text Cursor
10061 ***********************************************************************/
10063 /* Note if the text cursor of window W has been overwritten by a
10064 drawing operation that outputs N glyphs starting at HPOS in the
10065 line given by output_cursor.vpos. N < 0 means all the rest of the
10066 line after HPOS has been written. */
10068 static void
10069 note_overwritten_text_cursor (w, hpos, n)
10070 struct window *w;
10071 int hpos, n;
10073 if (updated_area == TEXT_AREA
10074 && output_cursor.vpos == w->phys_cursor.vpos
10075 && hpos <= w->phys_cursor.hpos
10076 && (n < 0
10077 || hpos + n > w->phys_cursor.hpos))
10078 w->phys_cursor_on_p = 0;
10082 /* Set clipping for output in glyph row ROW. W is the window in which
10083 we operate. GC is the graphics context to set clipping in.
10084 WHOLE_LINE_P non-zero means include the areas used for truncation
10085 mark display and alike in the clipping rectangle.
10087 ROW may be a text row or, e.g., a mode line. Text rows must be
10088 clipped to the interior of the window dedicated to text display,
10089 mode lines must be clipped to the whole window. */
10091 static void
10092 x_clip_to_row (w, row, gc, whole_line_p)
10093 struct window *w;
10094 struct glyph_row *row;
10095 GC gc;
10096 int whole_line_p;
10098 struct frame *f = XFRAME (WINDOW_FRAME (w));
10099 XRectangle clip_rect;
10100 int window_x, window_y, window_width, window_height;
10102 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10104 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10105 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10106 clip_rect.y = max (clip_rect.y, window_y);
10107 clip_rect.width = window_width;
10108 clip_rect.height = row->visible_height;
10110 /* If clipping to the whole line, including trunc marks, extend
10111 the rectangle to the left and increase its width. */
10112 if (whole_line_p)
10114 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10115 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10118 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10122 /* Draw a hollow box cursor on window W in glyph row ROW. */
10124 static void
10125 x_draw_hollow_cursor (w, row)
10126 struct window *w;
10127 struct glyph_row *row;
10129 struct frame *f = XFRAME (WINDOW_FRAME (w));
10130 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10131 Display *dpy = FRAME_X_DISPLAY (f);
10132 int x, y, wd, h;
10133 XGCValues xgcv;
10134 struct glyph *cursor_glyph;
10135 GC gc;
10137 /* Compute frame-relative coordinates from window-relative
10138 coordinates. */
10139 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10140 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10141 + row->ascent - w->phys_cursor_ascent);
10142 h = row->height - 1;
10144 /* Get the glyph the cursor is on. If we can't tell because
10145 the current matrix is invalid or such, give up. */
10146 cursor_glyph = get_phys_cursor_glyph (w);
10147 if (cursor_glyph == NULL)
10148 return;
10150 /* Compute the width of the rectangle to draw. If on a stretch
10151 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10152 rectangle as wide as the glyph, but use a canonical character
10153 width instead. */
10154 wd = cursor_glyph->pixel_width - 1;
10155 if (cursor_glyph->type == STRETCH_GLYPH
10156 && !x_stretch_cursor_p)
10157 wd = min (CANON_X_UNIT (f), wd);
10159 /* The foreground of cursor_gc is typically the same as the normal
10160 background color, which can cause the cursor box to be invisible. */
10161 xgcv.foreground = f->output_data.x->cursor_pixel;
10162 if (dpyinfo->scratch_cursor_gc)
10163 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10164 else
10165 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10166 GCForeground, &xgcv);
10167 gc = dpyinfo->scratch_cursor_gc;
10169 /* Set clipping, draw the rectangle, and reset clipping again. */
10170 x_clip_to_row (w, row, gc, 0);
10171 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10172 XSetClipMask (dpy, gc, None);
10176 /* Draw a bar cursor on window W in glyph row ROW.
10178 Implementation note: One would like to draw a bar cursor with an
10179 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10180 Unfortunately, I didn't find a font yet that has this property set.
10181 --gerd. */
10183 static void
10184 x_draw_bar_cursor (w, row, width)
10185 struct window *w;
10186 struct glyph_row *row;
10187 int width;
10189 /* If cursor hpos is out of bounds, don't draw garbage. This can
10190 happen in mini-buffer windows when switching between echo area
10191 glyphs and mini-buffer. */
10192 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10194 struct frame *f = XFRAME (w->frame);
10195 struct glyph *cursor_glyph;
10196 GC gc;
10197 int x;
10198 unsigned long mask;
10199 XGCValues xgcv;
10200 Display *dpy;
10201 Window window;
10203 cursor_glyph = get_phys_cursor_glyph (w);
10204 if (cursor_glyph == NULL)
10205 return;
10207 xgcv.background = f->output_data.x->cursor_pixel;
10208 xgcv.foreground = f->output_data.x->cursor_pixel;
10209 xgcv.graphics_exposures = 0;
10210 mask = GCForeground | GCBackground | GCGraphicsExposures;
10211 dpy = FRAME_X_DISPLAY (f);
10212 window = FRAME_X_WINDOW (f);
10213 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10215 if (gc)
10216 XChangeGC (dpy, gc, mask, &xgcv);
10217 else
10219 gc = XCreateGC (dpy, window, mask, &xgcv);
10220 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10223 if (width < 0)
10224 width = f->output_data.x->cursor_width;
10226 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10227 x_clip_to_row (w, row, gc, 0);
10228 XFillRectangle (dpy, window, gc,
10230 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10231 min (cursor_glyph->pixel_width, width),
10232 row->height);
10233 XSetClipMask (dpy, gc, None);
10238 /* Clear the cursor of window W to background color, and mark the
10239 cursor as not shown. This is used when the text where the cursor
10240 is is about to be rewritten. */
10242 static void
10243 x_clear_cursor (w)
10244 struct window *w;
10246 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10247 x_update_window_cursor (w, 0);
10251 /* Draw the cursor glyph of window W in glyph row ROW. See the
10252 comment of x_draw_glyphs for the meaning of HL. */
10254 static void
10255 x_draw_phys_cursor_glyph (w, row, hl)
10256 struct window *w;
10257 struct glyph_row *row;
10258 enum draw_glyphs_face hl;
10260 /* If cursor hpos is out of bounds, don't draw garbage. This can
10261 happen in mini-buffer windows when switching between echo area
10262 glyphs and mini-buffer. */
10263 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10265 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10266 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10267 hl, 0, 0, 0);
10269 /* When we erase the cursor, and ROW is overlapped by other
10270 rows, make sure that these overlapping parts of other rows
10271 are redrawn. */
10272 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10274 if (row > w->current_matrix->rows
10275 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10276 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10278 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10279 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10280 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10286 /* Erase the image of a cursor of window W from the screen. */
10288 static void
10289 x_erase_phys_cursor (w)
10290 struct window *w;
10292 struct frame *f = XFRAME (w->frame);
10293 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10294 int hpos = w->phys_cursor.hpos;
10295 int vpos = w->phys_cursor.vpos;
10296 int mouse_face_here_p = 0;
10297 struct glyph_matrix *active_glyphs = w->current_matrix;
10298 struct glyph_row *cursor_row;
10299 struct glyph *cursor_glyph;
10300 enum draw_glyphs_face hl;
10302 /* No cursor displayed or row invalidated => nothing to do on the
10303 screen. */
10304 if (w->phys_cursor_type == NO_CURSOR)
10305 goto mark_cursor_off;
10307 /* VPOS >= active_glyphs->nrows means that window has been resized.
10308 Don't bother to erase the cursor. */
10309 if (vpos >= active_glyphs->nrows)
10310 goto mark_cursor_off;
10312 /* If row containing cursor is marked invalid, there is nothing we
10313 can do. */
10314 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10315 if (!cursor_row->enabled_p)
10316 goto mark_cursor_off;
10318 /* This can happen when the new row is shorter than the old one.
10319 In this case, either x_draw_glyphs or clear_end_of_line
10320 should have cleared the cursor. Note that we wouldn't be
10321 able to erase the cursor in this case because we don't have a
10322 cursor glyph at hand. */
10323 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10324 goto mark_cursor_off;
10326 /* If the cursor is in the mouse face area, redisplay that when
10327 we clear the cursor. */
10328 if (! NILP (dpyinfo->mouse_face_window)
10329 && w == XWINDOW (dpyinfo->mouse_face_window)
10330 && (vpos > dpyinfo->mouse_face_beg_row
10331 || (vpos == dpyinfo->mouse_face_beg_row
10332 && hpos >= dpyinfo->mouse_face_beg_col))
10333 && (vpos < dpyinfo->mouse_face_end_row
10334 || (vpos == dpyinfo->mouse_face_end_row
10335 && hpos < dpyinfo->mouse_face_end_col))
10336 /* Don't redraw the cursor's spot in mouse face if it is at the
10337 end of a line (on a newline). The cursor appears there, but
10338 mouse highlighting does not. */
10339 && cursor_row->used[TEXT_AREA] > hpos)
10340 mouse_face_here_p = 1;
10342 /* Maybe clear the display under the cursor. */
10343 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10345 int x;
10346 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10348 cursor_glyph = get_phys_cursor_glyph (w);
10349 if (cursor_glyph == NULL)
10350 goto mark_cursor_off;
10352 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10354 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10356 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10357 cursor_row->y)),
10358 cursor_glyph->pixel_width,
10359 cursor_row->visible_height,
10360 False);
10363 /* Erase the cursor by redrawing the character underneath it. */
10364 if (mouse_face_here_p)
10365 hl = DRAW_MOUSE_FACE;
10366 else if (cursor_row->inverse_p)
10367 hl = DRAW_INVERSE_VIDEO;
10368 else
10369 hl = DRAW_NORMAL_TEXT;
10370 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10372 mark_cursor_off:
10373 w->phys_cursor_on_p = 0;
10374 w->phys_cursor_type = NO_CURSOR;
10378 /* Display or clear cursor of window W. If ON is zero, clear the
10379 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10380 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10382 void
10383 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10384 struct window *w;
10385 int on, hpos, vpos, x, y;
10387 struct frame *f = XFRAME (w->frame);
10388 int new_cursor_type;
10389 int new_cursor_width;
10390 struct glyph_matrix *current_glyphs;
10391 struct glyph_row *glyph_row;
10392 struct glyph *glyph;
10394 /* This is pointless on invisible frames, and dangerous on garbaged
10395 windows and frames; in the latter case, the frame or window may
10396 be in the midst of changing its size, and x and y may be off the
10397 window. */
10398 if (! FRAME_VISIBLE_P (f)
10399 || FRAME_GARBAGED_P (f)
10400 || vpos >= w->current_matrix->nrows
10401 || hpos >= w->current_matrix->matrix_w)
10402 return;
10404 /* If cursor is off and we want it off, return quickly. */
10405 if (!on && !w->phys_cursor_on_p)
10406 return;
10408 current_glyphs = w->current_matrix;
10409 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10410 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10412 /* If cursor row is not enabled, we don't really know where to
10413 display the cursor. */
10414 if (!glyph_row->enabled_p)
10416 w->phys_cursor_on_p = 0;
10417 return;
10420 xassert (interrupt_input_blocked);
10422 /* Set new_cursor_type to the cursor we want to be displayed. In a
10423 mini-buffer window, we want the cursor only to appear if we are
10424 reading input from this window. For the selected window, we want
10425 the cursor type given by the frame parameter. If explicitly
10426 marked off, draw no cursor. In all other cases, we want a hollow
10427 box cursor. */
10428 new_cursor_width = -1;
10429 if (cursor_in_echo_area
10430 && FRAME_HAS_MINIBUF_P (f)
10431 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10433 if (w == XWINDOW (echo_area_window))
10434 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10435 else
10436 new_cursor_type = HOLLOW_BOX_CURSOR;
10438 else
10440 if (w != XWINDOW (selected_window)
10441 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10443 extern int cursor_in_non_selected_windows;
10445 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
10446 new_cursor_type = NO_CURSOR;
10447 else
10448 new_cursor_type = HOLLOW_BOX_CURSOR;
10450 else if (w->cursor_off_p)
10451 new_cursor_type = NO_CURSOR;
10452 else
10454 struct buffer *b = XBUFFER (w->buffer);
10456 if (EQ (b->cursor_type, Qt))
10457 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10458 else
10459 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10460 &new_cursor_width);
10464 /* If cursor is currently being shown and we don't want it to be or
10465 it is in the wrong place, or the cursor type is not what we want,
10466 erase it. */
10467 if (w->phys_cursor_on_p
10468 && (!on
10469 || w->phys_cursor.x != x
10470 || w->phys_cursor.y != y
10471 || new_cursor_type != w->phys_cursor_type))
10472 x_erase_phys_cursor (w);
10474 /* If the cursor is now invisible and we want it to be visible,
10475 display it. */
10476 if (on && !w->phys_cursor_on_p)
10478 w->phys_cursor_ascent = glyph_row->ascent;
10479 w->phys_cursor_height = glyph_row->height;
10481 /* Set phys_cursor_.* before x_draw_.* is called because some
10482 of them may need the information. */
10483 w->phys_cursor.x = x;
10484 w->phys_cursor.y = glyph_row->y;
10485 w->phys_cursor.hpos = hpos;
10486 w->phys_cursor.vpos = vpos;
10487 w->phys_cursor_type = new_cursor_type;
10488 w->phys_cursor_on_p = 1;
10490 switch (new_cursor_type)
10492 case HOLLOW_BOX_CURSOR:
10493 x_draw_hollow_cursor (w, glyph_row);
10494 break;
10496 case FILLED_BOX_CURSOR:
10497 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10498 break;
10500 case BAR_CURSOR:
10501 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10502 break;
10504 case NO_CURSOR:
10505 break;
10507 default:
10508 abort ();
10511 #ifdef HAVE_X_I18N
10512 if (w == XWINDOW (f->selected_window))
10513 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10514 xic_set_preeditarea (w, x, y);
10515 #endif
10518 #ifndef XFlush
10519 if (updating_frame != f)
10520 XFlush (FRAME_X_DISPLAY (f));
10521 #endif
10525 /* Display the cursor on window W, or clear it. X and Y are window
10526 relative pixel coordinates. HPOS and VPOS are glyph matrix
10527 positions. If W is not the selected window, display a hollow
10528 cursor. ON non-zero means display the cursor at X, Y which
10529 correspond to HPOS, VPOS, otherwise it is cleared. */
10531 void
10532 x_display_cursor (w, on, hpos, vpos, x, y)
10533 struct window *w;
10534 int on, hpos, vpos, x, y;
10536 BLOCK_INPUT;
10537 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10538 UNBLOCK_INPUT;
10542 /* Display the cursor on window W, or clear it, according to ON_P.
10543 Don't change the cursor's position. */
10545 void
10546 x_update_cursor (f, on_p)
10547 struct frame *f;
10549 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10553 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10554 in the window tree rooted at W. */
10556 static void
10557 x_update_cursor_in_window_tree (w, on_p)
10558 struct window *w;
10559 int on_p;
10561 while (w)
10563 if (!NILP (w->hchild))
10564 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10565 else if (!NILP (w->vchild))
10566 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10567 else
10568 x_update_window_cursor (w, on_p);
10570 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10575 /* Switch the display of W's cursor on or off, according to the value
10576 of ON. */
10578 static void
10579 x_update_window_cursor (w, on)
10580 struct window *w;
10581 int on;
10583 /* Don't update cursor in windows whose frame is in the process
10584 of being deleted. */
10585 if (w->current_matrix)
10587 BLOCK_INPUT;
10588 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10589 w->phys_cursor.x, w->phys_cursor.y);
10590 UNBLOCK_INPUT;
10597 /* Icons. */
10599 /* Refresh bitmap kitchen sink icon for frame F
10600 when we get an expose event for it. */
10602 void
10603 refreshicon (f)
10604 struct frame *f;
10606 /* Normally, the window manager handles this function. */
10609 /* Make the x-window of frame F use the gnu icon bitmap. */
10612 x_bitmap_icon (f, file)
10613 struct frame *f;
10614 Lisp_Object file;
10616 int bitmap_id;
10618 if (FRAME_X_WINDOW (f) == 0)
10619 return 1;
10621 /* Free up our existing icon bitmap if any. */
10622 if (f->output_data.x->icon_bitmap > 0)
10623 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10624 f->output_data.x->icon_bitmap = 0;
10626 if (STRINGP (file))
10627 bitmap_id = x_create_bitmap_from_file (f, file);
10628 else
10630 /* Create the GNU bitmap if necessary. */
10631 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10632 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10633 = x_create_bitmap_from_data (f, gnu_bits,
10634 gnu_width, gnu_height);
10636 /* The first time we create the GNU bitmap,
10637 this increments the ref-count one extra time.
10638 As a result, the GNU bitmap is never freed.
10639 That way, we don't have to worry about allocating it again. */
10640 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10642 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10645 x_wm_set_icon_pixmap (f, bitmap_id);
10646 f->output_data.x->icon_bitmap = bitmap_id;
10648 return 0;
10652 /* Make the x-window of frame F use a rectangle with text.
10653 Use ICON_NAME as the text. */
10656 x_text_icon (f, icon_name)
10657 struct frame *f;
10658 char *icon_name;
10660 if (FRAME_X_WINDOW (f) == 0)
10661 return 1;
10663 #ifdef HAVE_X11R4
10665 XTextProperty text;
10666 text.value = (unsigned char *) icon_name;
10667 text.encoding = XA_STRING;
10668 text.format = 8;
10669 text.nitems = strlen (icon_name);
10670 #ifdef USE_X_TOOLKIT
10671 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10672 &text);
10673 #else /* not USE_X_TOOLKIT */
10674 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10675 #endif /* not USE_X_TOOLKIT */
10677 #else /* not HAVE_X11R4 */
10678 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10679 #endif /* not HAVE_X11R4 */
10681 if (f->output_data.x->icon_bitmap > 0)
10682 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10683 f->output_data.x->icon_bitmap = 0;
10684 x_wm_set_icon_pixmap (f, 0);
10686 return 0;
10689 #define X_ERROR_MESSAGE_SIZE 200
10691 /* If non-nil, this should be a string.
10692 It means catch X errors and store the error message in this string. */
10694 static Lisp_Object x_error_message_string;
10696 /* An X error handler which stores the error message in
10697 x_error_message_string. This is called from x_error_handler if
10698 x_catch_errors is in effect. */
10700 static void
10701 x_error_catcher (display, error)
10702 Display *display;
10703 XErrorEvent *error;
10705 XGetErrorText (display, error->error_code,
10706 XSTRING (x_error_message_string)->data,
10707 X_ERROR_MESSAGE_SIZE);
10710 /* Begin trapping X errors for display DPY. Actually we trap X errors
10711 for all displays, but DPY should be the display you are actually
10712 operating on.
10714 After calling this function, X protocol errors no longer cause
10715 Emacs to exit; instead, they are recorded in the string
10716 stored in x_error_message_string.
10718 Calling x_check_errors signals an Emacs error if an X error has
10719 occurred since the last call to x_catch_errors or x_check_errors.
10721 Calling x_uncatch_errors resumes the normal error handling. */
10723 void x_check_errors ();
10724 static Lisp_Object x_catch_errors_unwind ();
10727 x_catch_errors (dpy)
10728 Display *dpy;
10730 int count = specpdl_ptr - specpdl;
10732 /* Make sure any errors from previous requests have been dealt with. */
10733 XSync (dpy, False);
10735 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10737 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10738 XSTRING (x_error_message_string)->data[0] = 0;
10740 return count;
10743 /* Unbind the binding that we made to check for X errors. */
10745 static Lisp_Object
10746 x_catch_errors_unwind (old_val)
10747 Lisp_Object old_val;
10749 x_error_message_string = old_val;
10750 return Qnil;
10753 /* If any X protocol errors have arrived since the last call to
10754 x_catch_errors or x_check_errors, signal an Emacs error using
10755 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10757 void
10758 x_check_errors (dpy, format)
10759 Display *dpy;
10760 char *format;
10762 /* Make sure to catch any errors incurred so far. */
10763 XSync (dpy, False);
10765 if (XSTRING (x_error_message_string)->data[0])
10766 error (format, XSTRING (x_error_message_string)->data);
10769 /* Nonzero if we had any X protocol errors
10770 since we did x_catch_errors on DPY. */
10773 x_had_errors_p (dpy)
10774 Display *dpy;
10776 /* Make sure to catch any errors incurred so far. */
10777 XSync (dpy, False);
10779 return XSTRING (x_error_message_string)->data[0] != 0;
10782 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10784 void
10785 x_clear_errors (dpy)
10786 Display *dpy;
10788 XSTRING (x_error_message_string)->data[0] = 0;
10791 /* Stop catching X protocol errors and let them make Emacs die.
10792 DPY should be the display that was passed to x_catch_errors.
10793 COUNT should be the value that was returned by
10794 the corresponding call to x_catch_errors. */
10796 void
10797 x_uncatch_errors (dpy, count)
10798 Display *dpy;
10799 int count;
10801 unbind_to (count, Qnil);
10804 #if 0
10805 static unsigned int x_wire_count;
10806 x_trace_wire ()
10808 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10810 #endif /* ! 0 */
10813 /* Handle SIGPIPE, which can happen when the connection to a server
10814 simply goes away. SIGPIPE is handled by x_connection_signal.
10815 Don't need to do anything, because the write which caused the
10816 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
10817 which will do the appropriate cleanup for us. */
10819 static SIGTYPE
10820 x_connection_signal (signalnum) /* If we don't have an argument, */
10821 int signalnum; /* some compilers complain in signal calls. */
10823 #ifdef USG
10824 /* USG systems forget handlers when they are used;
10825 must reestablish each time */
10826 signal (signalnum, x_connection_signal);
10827 #endif /* USG */
10830 /* Handling X errors. */
10832 /* Handle the loss of connection to display DISPLAY. */
10834 static SIGTYPE
10835 x_connection_closed (display, error_message)
10836 Display *display;
10837 char *error_message;
10839 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10840 Lisp_Object frame, tail;
10842 /* Indicate that this display is dead. */
10844 #if 0 /* Closing the display caused a bus error on OpenWindows. */
10845 #ifdef USE_X_TOOLKIT
10846 XtCloseDisplay (display);
10847 #endif
10848 #endif
10850 if (dpyinfo)
10851 dpyinfo->display = 0;
10853 /* First delete frames whose mini-buffers are on frames
10854 that are on the dead display. */
10855 FOR_EACH_FRAME (tail, frame)
10857 Lisp_Object minibuf_frame;
10858 minibuf_frame
10859 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
10860 if (FRAME_X_P (XFRAME (frame))
10861 && FRAME_X_P (XFRAME (minibuf_frame))
10862 && ! EQ (frame, minibuf_frame)
10863 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
10864 Fdelete_frame (frame, Qt);
10867 /* Now delete all remaining frames on the dead display.
10868 We are now sure none of these is used as the mini-buffer
10869 for another frame that we need to delete. */
10870 FOR_EACH_FRAME (tail, frame)
10871 if (FRAME_X_P (XFRAME (frame))
10872 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
10874 /* Set this to t so that Fdelete_frame won't get confused
10875 trying to find a replacement. */
10876 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
10877 Fdelete_frame (frame, Qt);
10880 if (dpyinfo)
10881 x_delete_display (dpyinfo);
10883 if (x_display_list == 0)
10885 fprintf (stderr, "%s\n", error_message);
10886 shut_down_emacs (0, 0, Qnil);
10887 exit (70);
10890 /* Ordinary stack unwind doesn't deal with these. */
10891 #ifdef SIGIO
10892 sigunblock (sigmask (SIGIO));
10893 #endif
10894 sigunblock (sigmask (SIGALRM));
10895 TOTALLY_UNBLOCK_INPUT;
10897 clear_waiting_for_input ();
10898 error ("%s", error_message);
10901 /* This is the usual handler for X protocol errors.
10902 It kills all frames on the display that we got the error for.
10903 If that was the only one, it prints an error message and kills Emacs. */
10905 static void
10906 x_error_quitter (display, error)
10907 Display *display;
10908 XErrorEvent *error;
10910 char buf[256], buf1[356];
10912 /* Note that there is no real way portable across R3/R4 to get the
10913 original error handler. */
10915 XGetErrorText (display, error->error_code, buf, sizeof (buf));
10916 sprintf (buf1, "X protocol error: %s on protocol request %d",
10917 buf, error->request_code);
10918 x_connection_closed (display, buf1);
10921 /* This is the first-level handler for X protocol errors.
10922 It calls x_error_quitter or x_error_catcher. */
10924 static int
10925 x_error_handler (display, error)
10926 Display *display;
10927 XErrorEvent *error;
10929 if (! NILP (x_error_message_string))
10930 x_error_catcher (display, error);
10931 else
10932 x_error_quitter (display, error);
10933 return 0;
10936 /* This is the handler for X IO errors, always.
10937 It kills all frames on the display that we lost touch with.
10938 If that was the only one, it prints an error message and kills Emacs. */
10940 static int
10941 x_io_error_quitter (display)
10942 Display *display;
10944 char buf[256];
10946 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
10947 x_connection_closed (display, buf);
10948 return 0;
10951 /* Changing the font of the frame. */
10953 /* Give frame F the font named FONTNAME as its default font, and
10954 return the full name of that font. FONTNAME may be a wildcard
10955 pattern; in that case, we choose some font that fits the pattern.
10956 The return value shows which font we chose. */
10958 Lisp_Object
10959 x_new_font (f, fontname)
10960 struct frame *f;
10961 register char *fontname;
10963 struct font_info *fontp
10964 = FS_LOAD_FONT (f, 0, fontname, -1);
10966 if (!fontp)
10967 return Qnil;
10969 f->output_data.x->font = (XFontStruct *) (fontp->font);
10970 f->output_data.x->baseline_offset = fontp->baseline_offset;
10971 f->output_data.x->fontset = -1;
10973 /* Compute the scroll bar width in character columns. */
10974 if (f->scroll_bar_pixel_width > 0)
10976 int wid = FONT_WIDTH (f->output_data.x->font);
10977 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
10979 else
10981 int wid = FONT_WIDTH (f->output_data.x->font);
10982 f->scroll_bar_cols = (14 + wid - 1) / wid;
10985 /* Now make the frame display the given font. */
10986 if (FRAME_X_WINDOW (f) != 0)
10988 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
10989 f->output_data.x->font->fid);
10990 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
10991 f->output_data.x->font->fid);
10992 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
10993 f->output_data.x->font->fid);
10995 frame_update_line_height (f);
10996 x_set_window_size (f, 0, f->width, f->height);
10998 else
10999 /* If we are setting a new frame's font for the first time,
11000 there are no faces yet, so this font's height is the line height. */
11001 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11003 return build_string (fontp->full_name);
11006 /* Give frame F the fontset named FONTSETNAME as its default font, and
11007 return the full name of that fontset. FONTSETNAME may be a wildcard
11008 pattern; in that case, we choose some fontset that fits the pattern.
11009 The return value shows which fontset we chose. */
11011 Lisp_Object
11012 x_new_fontset (f, fontsetname)
11013 struct frame *f;
11014 char *fontsetname;
11016 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11017 Lisp_Object result;
11019 if (fontset < 0)
11020 return Qnil;
11022 if (f->output_data.x->fontset == fontset)
11023 /* This fontset is already set in frame F. There's nothing more
11024 to do. */
11025 return fontset_name (fontset);
11027 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11029 if (!STRINGP (result))
11030 /* Can't load ASCII font. */
11031 return Qnil;
11033 /* Since x_new_font doesn't update any fontset information, do it now. */
11034 f->output_data.x->fontset = fontset;
11036 #ifdef HAVE_X_I18N
11037 if (FRAME_XIC (f)
11038 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11039 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11040 #endif
11042 return build_string (fontsetname);
11046 /***********************************************************************
11047 X Input Methods
11048 ***********************************************************************/
11050 #ifdef HAVE_X_I18N
11052 #ifdef HAVE_X11R6
11054 /* XIM destroy callback function, which is called whenever the
11055 connection to input method XIM dies. CLIENT_DATA contains a
11056 pointer to the x_display_info structure corresponding to XIM. */
11058 static void
11059 xim_destroy_callback (xim, client_data, call_data)
11060 XIM xim;
11061 XPointer client_data;
11062 XPointer call_data;
11064 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11065 Lisp_Object frame, tail;
11067 BLOCK_INPUT;
11069 /* No need to call XDestroyIC.. */
11070 FOR_EACH_FRAME (tail, frame)
11072 struct frame *f = XFRAME (frame);
11073 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11075 FRAME_XIC (f) = NULL;
11076 if (FRAME_XIC_FONTSET (f))
11078 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11079 FRAME_XIC_FONTSET (f) = NULL;
11084 /* No need to call XCloseIM. */
11085 dpyinfo->xim = NULL;
11086 XFree (dpyinfo->xim_styles);
11087 UNBLOCK_INPUT;
11090 #endif /* HAVE_X11R6 */
11092 /* Open the connection to the XIM server on display DPYINFO.
11093 RESOURCE_NAME is the resource name Emacs uses. */
11095 static void
11096 xim_open_dpy (dpyinfo, resource_name)
11097 struct x_display_info *dpyinfo;
11098 char *resource_name;
11100 XIM xim;
11102 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11103 dpyinfo->xim = xim;
11105 if (xim)
11107 #ifdef HAVE_X11R6
11108 XIMCallback destroy;
11109 #endif
11111 /* Get supported styles and XIM values. */
11112 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11114 #ifdef HAVE_X11R6
11115 destroy.callback = xim_destroy_callback;
11116 destroy.client_data = (XPointer)dpyinfo;
11117 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11118 #endif
11123 #ifdef HAVE_X11R6_XIM
11125 struct xim_inst_t
11127 struct x_display_info *dpyinfo;
11128 char *resource_name;
11131 /* XIM instantiate callback function, which is called whenever an XIM
11132 server is available. DISPLAY is teh display of the XIM.
11133 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11134 when the callback was registered. */
11136 static void
11137 xim_instantiate_callback (display, client_data, call_data)
11138 Display *display;
11139 XPointer client_data;
11140 XPointer call_data;
11142 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11143 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11145 /* We don't support multiple XIM connections. */
11146 if (dpyinfo->xim)
11147 return;
11149 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11151 /* Create XIC for the existing frames on the same display, as long
11152 as they have no XIC. */
11153 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11155 Lisp_Object tail, frame;
11157 BLOCK_INPUT;
11158 FOR_EACH_FRAME (tail, frame)
11160 struct frame *f = XFRAME (frame);
11162 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11163 if (FRAME_XIC (f) == NULL)
11165 create_frame_xic (f);
11166 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11167 xic_set_statusarea (f);
11168 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11170 struct window *w = XWINDOW (f->selected_window);
11171 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11176 UNBLOCK_INPUT;
11180 #endif /* HAVE_X11R6_XIM */
11183 /* Open a connection to the XIM server on display DPYINFO.
11184 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11185 connection only at the first time. On X11R6, open the connection
11186 in the XIM instantiate callback function. */
11188 static void
11189 xim_initialize (dpyinfo, resource_name)
11190 struct x_display_info *dpyinfo;
11191 char *resource_name;
11193 #ifdef HAVE_X11R6_XIM
11194 struct xim_inst_t *xim_inst;
11195 int len;
11197 dpyinfo->xim = NULL;
11198 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11199 xim_inst->dpyinfo = dpyinfo;
11200 len = strlen (resource_name);
11201 xim_inst->resource_name = (char *) xmalloc (len + 1);
11202 bcopy (resource_name, xim_inst->resource_name, len + 1);
11203 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11204 resource_name, EMACS_CLASS,
11205 xim_instantiate_callback,
11206 /* Fixme: This is XPointer in
11207 XFree86 but (XPointer *) on
11208 Tru64, at least. */
11209 (XPointer) xim_inst);
11210 #else /* not HAVE_X11R6_XIM */
11211 dpyinfo->xim = NULL;
11212 xim_open_dpy (dpyinfo, resource_name);
11213 #endif /* not HAVE_X11R6_XIM */
11217 /* Close the connection to the XIM server on display DPYINFO. */
11219 static void
11220 xim_close_dpy (dpyinfo)
11221 struct x_display_info *dpyinfo;
11223 #ifdef HAVE_X11R6_XIM
11224 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11225 NULL, EMACS_CLASS,
11226 xim_instantiate_callback, NULL);
11227 #endif /* not HAVE_X11R6_XIM */
11228 XCloseIM (dpyinfo->xim);
11229 dpyinfo->xim = NULL;
11230 XFree (dpyinfo->xim_styles);
11233 #endif /* not HAVE_X11R6_XIM */
11237 /* Calculate the absolute position in frame F
11238 from its current recorded position values and gravity. */
11240 void
11241 x_calc_absolute_position (f)
11242 struct frame *f;
11244 Window child;
11245 int win_x = 0, win_y = 0;
11246 int flags = f->output_data.x->size_hint_flags;
11247 int this_window;
11249 /* We have nothing to do if the current position
11250 is already for the top-left corner. */
11251 if (! ((flags & XNegative) || (flags & YNegative)))
11252 return;
11254 #ifdef USE_X_TOOLKIT
11255 this_window = XtWindow (f->output_data.x->widget);
11256 #else
11257 this_window = FRAME_X_WINDOW (f);
11258 #endif
11260 /* Find the position of the outside upper-left corner of
11261 the inner window, with respect to the outer window.
11262 But do this only if we will need the results. */
11263 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11265 int count;
11267 BLOCK_INPUT;
11268 count = x_catch_errors (FRAME_X_DISPLAY (f));
11269 while (1)
11271 x_clear_errors (FRAME_X_DISPLAY (f));
11272 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11274 /* From-window, to-window. */
11275 this_window,
11276 f->output_data.x->parent_desc,
11278 /* From-position, to-position. */
11279 0, 0, &win_x, &win_y,
11281 /* Child of win. */
11282 &child);
11283 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11285 Window newroot, newparent = 0xdeadbeef;
11286 Window *newchildren;
11287 unsigned int nchildren;
11289 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11290 &newparent, &newchildren, &nchildren))
11291 break;
11293 XFree ((char *) newchildren);
11295 f->output_data.x->parent_desc = newparent;
11297 else
11298 break;
11301 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11302 UNBLOCK_INPUT;
11305 /* Treat negative positions as relative to the leftmost bottommost
11306 position that fits on the screen. */
11307 if (flags & XNegative)
11308 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11309 - 2 * f->output_data.x->border_width - win_x
11310 - PIXEL_WIDTH (f)
11311 + f->output_data.x->left_pos);
11313 if (flags & YNegative)
11315 int menubar_height = 0;
11317 #ifdef USE_X_TOOLKIT
11318 if (f->output_data.x->menubar_widget)
11319 menubar_height
11320 = (f->output_data.x->menubar_widget->core.height
11321 + f->output_data.x->menubar_widget->core.border_width);
11322 #endif
11324 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11325 - 2 * f->output_data.x->border_width
11326 - win_y
11327 - PIXEL_HEIGHT (f)
11328 - menubar_height
11329 + f->output_data.x->top_pos);
11332 /* The left_pos and top_pos
11333 are now relative to the top and left screen edges,
11334 so the flags should correspond. */
11335 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11338 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11339 to really change the position, and 0 when calling from
11340 x_make_frame_visible (in that case, XOFF and YOFF are the current
11341 position values). It is -1 when calling from x_set_frame_parameters,
11342 which means, do adjust for borders but don't change the gravity. */
11344 void
11345 x_set_offset (f, xoff, yoff, change_gravity)
11346 struct frame *f;
11347 register int xoff, yoff;
11348 int change_gravity;
11350 int modified_top, modified_left;
11352 if (change_gravity > 0)
11354 f->output_data.x->top_pos = yoff;
11355 f->output_data.x->left_pos = xoff;
11356 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11357 if (xoff < 0)
11358 f->output_data.x->size_hint_flags |= XNegative;
11359 if (yoff < 0)
11360 f->output_data.x->size_hint_flags |= YNegative;
11361 f->output_data.x->win_gravity = NorthWestGravity;
11363 x_calc_absolute_position (f);
11365 BLOCK_INPUT;
11366 x_wm_set_size_hint (f, (long) 0, 0);
11368 modified_left = f->output_data.x->left_pos;
11369 modified_top = f->output_data.x->top_pos;
11370 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11371 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11372 /* It is a mystery why we need to add the border_width here
11373 when the frame is already visible, but experiment says we do. */
11374 if (change_gravity != 0)
11376 modified_left += f->output_data.x->border_width;
11377 modified_top += f->output_data.x->border_width;
11379 #endif
11381 #ifdef USE_X_TOOLKIT
11382 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11383 modified_left, modified_top);
11384 #else /* not USE_X_TOOLKIT */
11385 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11386 modified_left, modified_top);
11387 #endif /* not USE_X_TOOLKIT */
11388 UNBLOCK_INPUT;
11391 /* Call this to change the size of frame F's x-window.
11392 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11393 for this size change and subsequent size changes.
11394 Otherwise we leave the window gravity unchanged. */
11396 void
11397 x_set_window_size (f, change_gravity, cols, rows)
11398 struct frame *f;
11399 int change_gravity;
11400 int cols, rows;
11402 #ifndef USE_X_TOOLKIT
11403 int pixelwidth, pixelheight;
11404 #endif
11406 BLOCK_INPUT;
11408 #ifdef USE_X_TOOLKIT
11410 /* The x and y position of the widget is clobbered by the
11411 call to XtSetValues within EmacsFrameSetCharSize.
11412 This is a real kludge, but I don't understand Xt so I can't
11413 figure out a correct fix. Can anyone else tell me? -- rms. */
11414 int xpos = f->output_data.x->widget->core.x;
11415 int ypos = f->output_data.x->widget->core.y;
11416 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11417 f->output_data.x->widget->core.x = xpos;
11418 f->output_data.x->widget->core.y = ypos;
11421 #else /* not USE_X_TOOLKIT */
11423 check_frame_size (f, &rows, &cols);
11424 f->output_data.x->vertical_scroll_bar_extra
11425 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11427 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11428 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11429 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11430 f->output_data.x->flags_areas_extra
11431 = FRAME_FLAGS_AREA_WIDTH (f);
11432 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11433 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11435 f->output_data.x->win_gravity = NorthWestGravity;
11436 x_wm_set_size_hint (f, (long) 0, 0);
11438 XSync (FRAME_X_DISPLAY (f), False);
11439 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11440 pixelwidth, pixelheight);
11442 /* Now, strictly speaking, we can't be sure that this is accurate,
11443 but the window manager will get around to dealing with the size
11444 change request eventually, and we'll hear how it went when the
11445 ConfigureNotify event gets here.
11447 We could just not bother storing any of this information here,
11448 and let the ConfigureNotify event set everything up, but that
11449 might be kind of confusing to the Lisp code, since size changes
11450 wouldn't be reported in the frame parameters until some random
11451 point in the future when the ConfigureNotify event arrives.
11453 We pass 1 for DELAY since we can't run Lisp code inside of
11454 a BLOCK_INPUT. */
11455 change_frame_size (f, rows, cols, 0, 1, 0);
11456 PIXEL_WIDTH (f) = pixelwidth;
11457 PIXEL_HEIGHT (f) = pixelheight;
11459 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11460 receive in the ConfigureNotify event; if we get what we asked
11461 for, then the event won't cause the screen to become garbaged, so
11462 we have to make sure to do it here. */
11463 SET_FRAME_GARBAGED (f);
11465 XFlush (FRAME_X_DISPLAY (f));
11467 #endif /* not USE_X_TOOLKIT */
11469 /* If cursor was outside the new size, mark it as off. */
11470 mark_window_cursors_off (XWINDOW (f->root_window));
11472 /* Clear out any recollection of where the mouse highlighting was,
11473 since it might be in a place that's outside the new frame size.
11474 Actually checking whether it is outside is a pain in the neck,
11475 so don't try--just let the highlighting be done afresh with new size. */
11476 cancel_mouse_face (f);
11478 UNBLOCK_INPUT;
11481 /* Mouse warping. */
11483 void
11484 x_set_mouse_position (f, x, y)
11485 struct frame *f;
11486 int x, y;
11488 int pix_x, pix_y;
11490 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11491 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11493 if (pix_x < 0) pix_x = 0;
11494 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11496 if (pix_y < 0) pix_y = 0;
11497 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11499 BLOCK_INPUT;
11501 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11502 0, 0, 0, 0, pix_x, pix_y);
11503 UNBLOCK_INPUT;
11506 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11508 void
11509 x_set_mouse_pixel_position (f, pix_x, pix_y)
11510 struct frame *f;
11511 int pix_x, pix_y;
11513 BLOCK_INPUT;
11515 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11516 0, 0, 0, 0, pix_x, pix_y);
11517 UNBLOCK_INPUT;
11520 /* focus shifting, raising and lowering. */
11522 void
11523 x_focus_on_frame (f)
11524 struct frame *f;
11526 #if 0 /* This proves to be unpleasant. */
11527 x_raise_frame (f);
11528 #endif
11529 #if 0
11530 /* I don't think that the ICCCM allows programs to do things like this
11531 without the interaction of the window manager. Whatever you end up
11532 doing with this code, do it to x_unfocus_frame too. */
11533 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11534 RevertToPointerRoot, CurrentTime);
11535 #endif /* ! 0 */
11538 void
11539 x_unfocus_frame (f)
11540 struct frame *f;
11542 #if 0
11543 /* Look at the remarks in x_focus_on_frame. */
11544 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
11545 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
11546 RevertToPointerRoot, CurrentTime);
11547 #endif /* ! 0 */
11550 /* Raise frame F. */
11552 void
11553 x_raise_frame (f)
11554 struct frame *f;
11556 if (f->async_visible)
11558 BLOCK_INPUT;
11559 #ifdef USE_X_TOOLKIT
11560 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11561 #else /* not USE_X_TOOLKIT */
11562 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11563 #endif /* not USE_X_TOOLKIT */
11564 XFlush (FRAME_X_DISPLAY (f));
11565 UNBLOCK_INPUT;
11569 /* Lower frame F. */
11571 void
11572 x_lower_frame (f)
11573 struct frame *f;
11575 if (f->async_visible)
11577 BLOCK_INPUT;
11578 #ifdef USE_X_TOOLKIT
11579 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11580 #else /* not USE_X_TOOLKIT */
11581 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11582 #endif /* not USE_X_TOOLKIT */
11583 XFlush (FRAME_X_DISPLAY (f));
11584 UNBLOCK_INPUT;
11588 static void
11589 XTframe_raise_lower (f, raise_flag)
11590 FRAME_PTR f;
11591 int raise_flag;
11593 if (raise_flag)
11594 x_raise_frame (f);
11595 else
11596 x_lower_frame (f);
11599 /* Change of visibility. */
11601 /* This tries to wait until the frame is really visible.
11602 However, if the window manager asks the user where to position
11603 the frame, this will return before the user finishes doing that.
11604 The frame will not actually be visible at that time,
11605 but it will become visible later when the window manager
11606 finishes with it. */
11608 void
11609 x_make_frame_visible (f)
11610 struct frame *f;
11612 Lisp_Object type;
11613 int original_top, original_left;
11615 BLOCK_INPUT;
11617 type = x_icon_type (f);
11618 if (!NILP (type))
11619 x_bitmap_icon (f, type);
11621 if (! FRAME_VISIBLE_P (f))
11623 /* We test FRAME_GARBAGED_P here to make sure we don't
11624 call x_set_offset a second time
11625 if we get to x_make_frame_visible a second time
11626 before the window gets really visible. */
11627 if (! FRAME_ICONIFIED_P (f)
11628 && ! f->output_data.x->asked_for_visible)
11629 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11631 f->output_data.x->asked_for_visible = 1;
11633 if (! EQ (Vx_no_window_manager, Qt))
11634 x_wm_set_window_state (f, NormalState);
11635 #ifdef USE_X_TOOLKIT
11636 /* This was XtPopup, but that did nothing for an iconified frame. */
11637 XtMapWidget (f->output_data.x->widget);
11638 #else /* not USE_X_TOOLKIT */
11639 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11640 #endif /* not USE_X_TOOLKIT */
11641 #if 0 /* This seems to bring back scroll bars in the wrong places
11642 if the window configuration has changed. They seem
11643 to come back ok without this. */
11644 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11645 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11646 #endif
11649 XFlush (FRAME_X_DISPLAY (f));
11651 /* Synchronize to ensure Emacs knows the frame is visible
11652 before we do anything else. We do this loop with input not blocked
11653 so that incoming events are handled. */
11655 Lisp_Object frame;
11656 int count;
11657 /* This must be before UNBLOCK_INPUT
11658 since events that arrive in response to the actions above
11659 will set it when they are handled. */
11660 int previously_visible = f->output_data.x->has_been_visible;
11662 original_left = f->output_data.x->left_pos;
11663 original_top = f->output_data.x->top_pos;
11665 /* This must come after we set COUNT. */
11666 UNBLOCK_INPUT;
11668 /* We unblock here so that arriving X events are processed. */
11670 /* Now move the window back to where it was "supposed to be".
11671 But don't do it if the gravity is negative.
11672 When the gravity is negative, this uses a position
11673 that is 3 pixels too low. Perhaps that's really the border width.
11675 Don't do this if the window has never been visible before,
11676 because the window manager may choose the position
11677 and we don't want to override it. */
11679 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11680 && f->output_data.x->win_gravity == NorthWestGravity
11681 && previously_visible)
11683 Drawable rootw;
11684 int x, y;
11685 unsigned int width, height, border, depth;
11687 BLOCK_INPUT;
11689 /* On some window managers (such as FVWM) moving an existing
11690 window, even to the same place, causes the window manager
11691 to introduce an offset. This can cause the window to move
11692 to an unexpected location. Check the geometry (a little
11693 slow here) and then verify that the window is in the right
11694 place. If the window is not in the right place, move it
11695 there, and take the potential window manager hit. */
11696 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11697 &rootw, &x, &y, &width, &height, &border, &depth);
11699 if (original_left != x || original_top != y)
11700 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11701 original_left, original_top);
11703 UNBLOCK_INPUT;
11706 XSETFRAME (frame, f);
11708 /* Wait until the frame is visible. Process X events until a
11709 MapNotify event has been seen, or until we think we won't get a
11710 MapNotify at all.. */
11711 for (count = input_signal_count + 10;
11712 input_signal_count < count && !FRAME_VISIBLE_P (f);)
11714 /* Force processing of queued events. */
11715 x_sync (f);
11717 /* Machines that do polling rather than SIGIO have been
11718 observed to go into a busy-wait here. So we'll fake an
11719 alarm signal to let the handler know that there's something
11720 to be read. We used to raise a real alarm, but it seems
11721 that the handler isn't always enabled here. This is
11722 probably a bug. */
11723 if (input_polling_used ())
11725 /* It could be confusing if a real alarm arrives while
11726 processing the fake one. Turn it off and let the
11727 handler reset it. */
11728 extern void poll_for_input_1 P_ ((void));
11729 int old_poll_suppress_count = poll_suppress_count;
11730 poll_suppress_count = 1;
11731 poll_for_input_1 ();
11732 poll_suppress_count = old_poll_suppress_count;
11735 /* See if a MapNotify event has been processed. */
11736 FRAME_SAMPLE_VISIBILITY (f);
11741 /* Change from mapped state to withdrawn state. */
11743 /* Make the frame visible (mapped and not iconified). */
11745 void
11746 x_make_frame_invisible (f)
11747 struct frame *f;
11749 Window window;
11751 #ifdef USE_X_TOOLKIT
11752 /* Use the frame's outermost window, not the one we normally draw on. */
11753 window = XtWindow (f->output_data.x->widget);
11754 #else /* not USE_X_TOOLKIT */
11755 window = FRAME_X_WINDOW (f);
11756 #endif /* not USE_X_TOOLKIT */
11758 /* Don't keep the highlight on an invisible frame. */
11759 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11760 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11762 #if 0/* This might add unreliability; I don't trust it -- rms. */
11763 if (! f->async_visible && ! f->async_iconified)
11764 return;
11765 #endif
11767 BLOCK_INPUT;
11769 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11770 that the current position of the window is user-specified, rather than
11771 program-specified, so that when the window is mapped again, it will be
11772 placed at the same location, without forcing the user to position it
11773 by hand again (they have already done that once for this window.) */
11774 x_wm_set_size_hint (f, (long) 0, 1);
11776 #ifdef HAVE_X11R4
11778 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11779 DefaultScreen (FRAME_X_DISPLAY (f))))
11781 UNBLOCK_INPUT_RESIGNAL;
11782 error ("Can't notify window manager of window withdrawal");
11784 #else /* ! defined (HAVE_X11R4) */
11786 /* Tell the window manager what we're going to do. */
11787 if (! EQ (Vx_no_window_manager, Qt))
11789 XEvent unmap;
11791 unmap.xunmap.type = UnmapNotify;
11792 unmap.xunmap.window = window;
11793 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
11794 unmap.xunmap.from_configure = False;
11795 if (! XSendEvent (FRAME_X_DISPLAY (f),
11796 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11797 False,
11798 SubstructureRedirectMaskSubstructureNotifyMask,
11799 &unmap))
11801 UNBLOCK_INPUT_RESIGNAL;
11802 error ("Can't notify window manager of withdrawal");
11806 /* Unmap the window ourselves. Cheeky! */
11807 XUnmapWindow (FRAME_X_DISPLAY (f), window);
11808 #endif /* ! defined (HAVE_X11R4) */
11810 /* We can't distinguish this from iconification
11811 just by the event that we get from the server.
11812 So we can't win using the usual strategy of letting
11813 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11814 and synchronize with the server to make sure we agree. */
11815 f->visible = 0;
11816 FRAME_ICONIFIED_P (f) = 0;
11817 f->async_visible = 0;
11818 f->async_iconified = 0;
11820 x_sync (f);
11822 UNBLOCK_INPUT;
11825 /* Change window state from mapped to iconified. */
11827 void
11828 x_iconify_frame (f)
11829 struct frame *f;
11831 int result;
11832 Lisp_Object type;
11834 /* Don't keep the highlight on an invisible frame. */
11835 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11836 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11838 if (f->async_iconified)
11839 return;
11841 BLOCK_INPUT;
11843 FRAME_SAMPLE_VISIBILITY (f);
11845 type = x_icon_type (f);
11846 if (!NILP (type))
11847 x_bitmap_icon (f, type);
11849 #ifdef USE_X_TOOLKIT
11851 if (! FRAME_VISIBLE_P (f))
11853 if (! EQ (Vx_no_window_manager, Qt))
11854 x_wm_set_window_state (f, IconicState);
11855 /* This was XtPopup, but that did nothing for an iconified frame. */
11856 XtMapWidget (f->output_data.x->widget);
11857 /* The server won't give us any event to indicate
11858 that an invisible frame was changed to an icon,
11859 so we have to record it here. */
11860 f->iconified = 1;
11861 f->visible = 1;
11862 f->async_iconified = 1;
11863 f->async_visible = 0;
11864 UNBLOCK_INPUT;
11865 return;
11868 result = XIconifyWindow (FRAME_X_DISPLAY (f),
11869 XtWindow (f->output_data.x->widget),
11870 DefaultScreen (FRAME_X_DISPLAY (f)));
11871 UNBLOCK_INPUT;
11873 if (!result)
11874 error ("Can't notify window manager of iconification");
11876 f->async_iconified = 1;
11877 f->async_visible = 0;
11880 BLOCK_INPUT;
11881 XFlush (FRAME_X_DISPLAY (f));
11882 UNBLOCK_INPUT;
11883 #else /* not USE_X_TOOLKIT */
11885 /* Make sure the X server knows where the window should be positioned,
11886 in case the user deiconifies with the window manager. */
11887 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
11888 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11890 /* Since we don't know which revision of X we're running, we'll use both
11891 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
11893 /* X11R4: send a ClientMessage to the window manager using the
11894 WM_CHANGE_STATE type. */
11896 XEvent message;
11898 message.xclient.window = FRAME_X_WINDOW (f);
11899 message.xclient.type = ClientMessage;
11900 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
11901 message.xclient.format = 32;
11902 message.xclient.data.l[0] = IconicState;
11904 if (! XSendEvent (FRAME_X_DISPLAY (f),
11905 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11906 False,
11907 SubstructureRedirectMask | SubstructureNotifyMask,
11908 &message))
11910 UNBLOCK_INPUT_RESIGNAL;
11911 error ("Can't notify window manager of iconification");
11915 /* X11R3: set the initial_state field of the window manager hints to
11916 IconicState. */
11917 x_wm_set_window_state (f, IconicState);
11919 if (!FRAME_VISIBLE_P (f))
11921 /* If the frame was withdrawn, before, we must map it. */
11922 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11925 f->async_iconified = 1;
11926 f->async_visible = 0;
11928 XFlush (FRAME_X_DISPLAY (f));
11929 UNBLOCK_INPUT;
11930 #endif /* not USE_X_TOOLKIT */
11933 /* Destroy the X window of frame F. */
11935 void
11936 x_destroy_window (f)
11937 struct frame *f;
11939 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11941 BLOCK_INPUT;
11943 /* If a display connection is dead, don't try sending more
11944 commands to the X server. */
11945 if (dpyinfo->display != 0)
11947 if (f->output_data.x->icon_desc != 0)
11948 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
11949 #ifdef HAVE_X_I18N
11950 if (FRAME_XIC (f))
11951 free_frame_xic (f);
11952 #endif
11953 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
11954 #ifdef USE_X_TOOLKIT
11955 if (f->output_data.x->widget)
11956 XtDestroyWidget (f->output_data.x->widget);
11957 free_frame_menubar (f);
11958 #endif /* USE_X_TOOLKIT */
11960 unload_color (f, f->output_data.x->foreground_pixel);
11961 unload_color (f, f->output_data.x->background_pixel);
11962 unload_color (f, f->output_data.x->cursor_pixel);
11963 unload_color (f, f->output_data.x->cursor_foreground_pixel);
11964 unload_color (f, f->output_data.x->border_pixel);
11965 unload_color (f, f->output_data.x->mouse_pixel);
11966 if (f->output_data.x->scroll_bar_background_pixel != -1)
11967 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
11968 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
11969 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
11970 if (f->output_data.x->white_relief.allocated_p)
11971 unload_color (f, f->output_data.x->white_relief.pixel);
11972 if (f->output_data.x->black_relief.allocated_p)
11973 unload_color (f, f->output_data.x->black_relief.pixel);
11975 free_frame_faces (f);
11976 XFlush (FRAME_X_DISPLAY (f));
11979 if (f->output_data.x->saved_menu_event)
11980 xfree (f->output_data.x->saved_menu_event);
11982 xfree (f->output_data.x);
11983 f->output_data.x = 0;
11984 if (f == dpyinfo->x_focus_frame)
11985 dpyinfo->x_focus_frame = 0;
11986 if (f == dpyinfo->x_focus_event_frame)
11987 dpyinfo->x_focus_event_frame = 0;
11988 if (f == dpyinfo->x_highlight_frame)
11989 dpyinfo->x_highlight_frame = 0;
11991 dpyinfo->reference_count--;
11993 if (f == dpyinfo->mouse_face_mouse_frame)
11995 dpyinfo->mouse_face_beg_row
11996 = dpyinfo->mouse_face_beg_col = -1;
11997 dpyinfo->mouse_face_end_row
11998 = dpyinfo->mouse_face_end_col = -1;
11999 dpyinfo->mouse_face_window = Qnil;
12000 dpyinfo->mouse_face_deferred_gc = 0;
12001 dpyinfo->mouse_face_mouse_frame = 0;
12004 UNBLOCK_INPUT;
12007 /* Setting window manager hints. */
12009 /* Set the normal size hints for the window manager, for frame F.
12010 FLAGS is the flags word to use--or 0 meaning preserve the flags
12011 that the window now has.
12012 If USER_POSITION is nonzero, we set the USPosition
12013 flag (this is useful when FLAGS is 0). */
12015 void
12016 x_wm_set_size_hint (f, flags, user_position)
12017 struct frame *f;
12018 long flags;
12019 int user_position;
12021 XSizeHints size_hints;
12023 #ifdef USE_X_TOOLKIT
12024 Arg al[2];
12025 int ac = 0;
12026 Dimension widget_width, widget_height;
12027 Window window = XtWindow (f->output_data.x->widget);
12028 #else /* not USE_X_TOOLKIT */
12029 Window window = FRAME_X_WINDOW (f);
12030 #endif /* not USE_X_TOOLKIT */
12032 /* Setting PMaxSize caused various problems. */
12033 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12035 size_hints.x = f->output_data.x->left_pos;
12036 size_hints.y = f->output_data.x->top_pos;
12038 #ifdef USE_X_TOOLKIT
12039 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12040 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12041 XtGetValues (f->output_data.x->widget, al, ac);
12042 size_hints.height = widget_height;
12043 size_hints.width = widget_width;
12044 #else /* not USE_X_TOOLKIT */
12045 size_hints.height = PIXEL_HEIGHT (f);
12046 size_hints.width = PIXEL_WIDTH (f);
12047 #endif /* not USE_X_TOOLKIT */
12049 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12050 size_hints.height_inc = f->output_data.x->line_height;
12051 size_hints.max_width
12052 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12053 size_hints.max_height
12054 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12056 /* Calculate the base and minimum sizes.
12058 (When we use the X toolkit, we don't do it here.
12059 Instead we copy the values that the widgets are using, below.) */
12060 #ifndef USE_X_TOOLKIT
12062 int base_width, base_height;
12063 int min_rows = 0, min_cols = 0;
12065 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12066 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12068 check_frame_size (f, &min_rows, &min_cols);
12070 /* The window manager uses the base width hints to calculate the
12071 current number of rows and columns in the frame while
12072 resizing; min_width and min_height aren't useful for this
12073 purpose, since they might not give the dimensions for a
12074 zero-row, zero-column frame.
12076 We use the base_width and base_height members if we have
12077 them; otherwise, we set the min_width and min_height members
12078 to the size for a zero x zero frame. */
12080 #ifdef HAVE_X11R4
12081 size_hints.flags |= PBaseSize;
12082 size_hints.base_width = base_width;
12083 size_hints.base_height = base_height;
12084 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12085 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12086 #else
12087 size_hints.min_width = base_width;
12088 size_hints.min_height = base_height;
12089 #endif
12092 /* If we don't need the old flags, we don't need the old hint at all. */
12093 if (flags)
12095 size_hints.flags |= flags;
12096 goto no_read;
12098 #endif /* not USE_X_TOOLKIT */
12101 XSizeHints hints; /* Sometimes I hate X Windows... */
12102 long supplied_return;
12103 int value;
12105 #ifdef HAVE_X11R4
12106 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12107 &supplied_return);
12108 #else
12109 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12110 #endif
12112 #ifdef USE_X_TOOLKIT
12113 size_hints.base_height = hints.base_height;
12114 size_hints.base_width = hints.base_width;
12115 size_hints.min_height = hints.min_height;
12116 size_hints.min_width = hints.min_width;
12117 #endif
12119 if (flags)
12120 size_hints.flags |= flags;
12121 else
12123 if (value == 0)
12124 hints.flags = 0;
12125 if (hints.flags & PSize)
12126 size_hints.flags |= PSize;
12127 if (hints.flags & PPosition)
12128 size_hints.flags |= PPosition;
12129 if (hints.flags & USPosition)
12130 size_hints.flags |= USPosition;
12131 if (hints.flags & USSize)
12132 size_hints.flags |= USSize;
12136 #ifndef USE_X_TOOLKIT
12137 no_read:
12138 #endif
12140 #ifdef PWinGravity
12141 size_hints.win_gravity = f->output_data.x->win_gravity;
12142 size_hints.flags |= PWinGravity;
12144 if (user_position)
12146 size_hints.flags &= ~ PPosition;
12147 size_hints.flags |= USPosition;
12149 #endif /* PWinGravity */
12151 #ifdef HAVE_X11R4
12152 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12153 #else
12154 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12155 #endif
12158 /* Used for IconicState or NormalState */
12160 void
12161 x_wm_set_window_state (f, state)
12162 struct frame *f;
12163 int state;
12165 #ifdef USE_X_TOOLKIT
12166 Arg al[1];
12168 XtSetArg (al[0], XtNinitialState, state);
12169 XtSetValues (f->output_data.x->widget, al, 1);
12170 #else /* not USE_X_TOOLKIT */
12171 Window window = FRAME_X_WINDOW (f);
12173 f->output_data.x->wm_hints.flags |= StateHint;
12174 f->output_data.x->wm_hints.initial_state = state;
12176 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12177 #endif /* not USE_X_TOOLKIT */
12180 void
12181 x_wm_set_icon_pixmap (f, pixmap_id)
12182 struct frame *f;
12183 int pixmap_id;
12185 Pixmap icon_pixmap;
12187 #ifndef USE_X_TOOLKIT
12188 Window window = FRAME_X_WINDOW (f);
12189 #endif
12191 if (pixmap_id > 0)
12193 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12194 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12196 else
12198 /* It seems there is no way to turn off use of an icon pixmap.
12199 The following line does it, only if no icon has yet been created,
12200 for some window managers. But with mwm it crashes.
12201 Some people say it should clear the IconPixmapHint bit in this case,
12202 but that doesn't work, and the X consortium said it isn't the
12203 right thing at all. Since there is no way to win,
12204 best to explicitly give up. */
12205 #if 0
12206 f->output_data.x->wm_hints.icon_pixmap = None;
12207 #else
12208 return;
12209 #endif
12212 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12215 Arg al[1];
12216 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12217 XtSetValues (f->output_data.x->widget, al, 1);
12220 #else /* not USE_X_TOOLKIT */
12222 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12223 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12225 #endif /* not USE_X_TOOLKIT */
12228 void
12229 x_wm_set_icon_position (f, icon_x, icon_y)
12230 struct frame *f;
12231 int icon_x, icon_y;
12233 #ifdef USE_X_TOOLKIT
12234 Window window = XtWindow (f->output_data.x->widget);
12235 #else
12236 Window window = FRAME_X_WINDOW (f);
12237 #endif
12239 f->output_data.x->wm_hints.flags |= IconPositionHint;
12240 f->output_data.x->wm_hints.icon_x = icon_x;
12241 f->output_data.x->wm_hints.icon_y = icon_y;
12243 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12247 /***********************************************************************
12248 Fonts
12249 ***********************************************************************/
12251 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12253 struct font_info *
12254 x_get_font_info (f, font_idx)
12255 FRAME_PTR f;
12256 int font_idx;
12258 return (FRAME_X_FONT_TABLE (f) + font_idx);
12262 /* Return a list of names of available fonts matching PATTERN on frame
12263 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12264 to be listed. Frame F NULL means we have not yet created any
12265 frame on X, and consult the first display in x_display_list.
12266 MAXNAMES sets a limit on how many fonts to match. */
12268 Lisp_Object
12269 x_list_fonts (f, pattern, size, maxnames)
12270 FRAME_PTR f;
12271 Lisp_Object pattern;
12272 int size;
12273 int maxnames;
12275 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12276 Lisp_Object tem, second_best;
12277 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12278 int try_XLoadQueryFont = 0;
12279 int count;
12281 patterns = Fassoc (pattern, Valternate_fontname_alist);
12282 if (NILP (patterns))
12283 patterns = Fcons (pattern, Qnil);
12285 if (maxnames == 1 && !size)
12286 /* We can return any single font matching PATTERN. */
12287 try_XLoadQueryFont = 1;
12289 for (; CONSP (patterns); patterns = XCDR (patterns))
12291 int num_fonts;
12292 char **names = NULL;
12294 pattern = XCAR (patterns);
12295 /* See if we cached the result for this particular query.
12296 The cache is an alist of the form:
12297 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12299 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12300 key = Fcons (pattern, make_number (maxnames)),
12301 !NILP (list = Fassoc (key, tem))))
12303 list = Fcdr_safe (list);
12304 /* We have a cashed list. Don't have to get the list again. */
12305 goto label_cached;
12308 /* At first, put PATTERN in the cache. */
12310 BLOCK_INPUT;
12311 count = x_catch_errors (dpy);
12313 if (try_XLoadQueryFont)
12315 XFontStruct *font;
12316 unsigned long value;
12318 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12319 if (x_had_errors_p (dpy))
12321 /* This error is perhaps due to insufficient memory on X
12322 server. Let's just ignore it. */
12323 font = NULL;
12324 x_clear_errors (dpy);
12327 if (font
12328 && XGetFontProperty (font, XA_FONT, &value))
12330 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12331 int len = strlen (name);
12332 char *tmp;
12334 /* If DXPC (a Differential X Protocol Compressor)
12335 Ver.3.7 is running, XGetAtomName will return null
12336 string. We must avoid such a name. */
12337 if (len == 0)
12338 try_XLoadQueryFont = 0;
12339 else
12341 num_fonts = 1;
12342 names = (char **) alloca (sizeof (char *));
12343 /* Some systems only allow alloca assigned to a
12344 simple var. */
12345 tmp = (char *) alloca (len + 1); names[0] = tmp;
12346 bcopy (name, names[0], len + 1);
12347 XFree (name);
12350 else
12351 try_XLoadQueryFont = 0;
12353 if (font)
12354 XFreeFont (dpy, font);
12357 if (!try_XLoadQueryFont)
12359 /* We try at least 10 fonts because XListFonts will return
12360 auto-scaled fonts at the head. */
12361 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12362 &num_fonts);
12363 if (x_had_errors_p (dpy))
12365 /* This error is perhaps due to insufficient memory on X
12366 server. Let's just ignore it. */
12367 names = NULL;
12368 x_clear_errors (dpy);
12372 x_uncatch_errors (dpy, count);
12373 UNBLOCK_INPUT;
12375 if (names)
12377 int i;
12379 /* Make a list of all the fonts we got back.
12380 Store that in the font cache for the display. */
12381 for (i = 0; i < num_fonts; i++)
12383 int width = 0;
12384 char *p = names[i];
12385 int average_width = -1, dashes = 0;
12387 /* Count the number of dashes in NAMES[I]. If there are
12388 14 dashes, and the field value following 12th dash
12389 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12390 is usually too ugly to be used for editing. Let's
12391 ignore it. */
12392 while (*p)
12393 if (*p++ == '-')
12395 dashes++;
12396 if (dashes == 7) /* PIXEL_SIZE field */
12397 width = atoi (p);
12398 else if (dashes == 12) /* AVERAGE_WIDTH field */
12399 average_width = atoi (p);
12401 if (dashes < 14 || average_width != 0)
12403 tem = build_string (names[i]);
12404 if (NILP (Fassoc (tem, list)))
12406 if (STRINGP (Vx_pixel_size_width_font_regexp)
12407 && ((fast_c_string_match_ignore_case
12408 (Vx_pixel_size_width_font_regexp, names[i]))
12409 >= 0))
12410 /* We can set the value of PIXEL_SIZE to the
12411 width of this font. */
12412 list = Fcons (Fcons (tem, make_number (width)), list);
12413 else
12414 /* For the moment, width is not known. */
12415 list = Fcons (Fcons (tem, Qnil), list);
12419 if (!try_XLoadQueryFont)
12420 XFreeFontNames (names);
12423 /* Now store the result in the cache. */
12424 if (f != NULL)
12425 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12426 = Fcons (Fcons (key, list),
12427 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12429 label_cached:
12430 if (NILP (list)) continue; /* Try the remaining alternatives. */
12432 newlist = second_best = Qnil;
12433 /* Make a list of the fonts that have the right width. */
12434 for (; CONSP (list); list = XCDR (list))
12436 int found_size;
12438 tem = XCAR (list);
12440 if (!CONSP (tem) || NILP (XCAR (tem)))
12441 continue;
12442 if (!size)
12444 newlist = Fcons (XCAR (tem), newlist);
12445 continue;
12448 if (!INTEGERP (XCDR (tem)))
12450 /* Since we have not yet known the size of this font, we
12451 must try slow function call XLoadQueryFont. */
12452 XFontStruct *thisinfo;
12454 BLOCK_INPUT;
12455 count = x_catch_errors (dpy);
12456 thisinfo = XLoadQueryFont (dpy,
12457 XSTRING (XCAR (tem))->data);
12458 if (x_had_errors_p (dpy))
12460 /* This error is perhaps due to insufficient memory on X
12461 server. Let's just ignore it. */
12462 thisinfo = NULL;
12463 x_clear_errors (dpy);
12465 x_uncatch_errors (dpy, count);
12466 UNBLOCK_INPUT;
12468 if (thisinfo)
12470 XCDR (tem)
12471 = (thisinfo->min_bounds.width == 0
12472 ? make_number (0)
12473 : make_number (thisinfo->max_bounds.width));
12474 XFreeFont (dpy, thisinfo);
12476 else
12477 /* For unknown reason, the previous call of XListFont had
12478 returned a font which can't be opened. Record the size
12479 as 0 not to try to open it again. */
12480 XCDR (tem) = make_number (0);
12483 found_size = XINT (XCDR (tem));
12484 if (found_size == size)
12485 newlist = Fcons (XCAR (tem), newlist);
12486 else if (found_size > 0)
12488 if (NILP (second_best))
12489 second_best = tem;
12490 else if (found_size < size)
12492 if (XINT (XCDR (second_best)) > size
12493 || XINT (XCDR (second_best)) < found_size)
12494 second_best = tem;
12496 else
12498 if (XINT (XCDR (second_best)) > size
12499 && XINT (XCDR (second_best)) > found_size)
12500 second_best = tem;
12504 if (!NILP (newlist))
12505 break;
12506 else if (!NILP (second_best))
12508 newlist = Fcons (XCAR (second_best), Qnil);
12509 break;
12513 return newlist;
12517 #if GLYPH_DEBUG
12519 /* Check that FONT is valid on frame F. It is if it can be found in F's
12520 font table. */
12522 static void
12523 x_check_font (f, font)
12524 struct frame *f;
12525 XFontStruct *font;
12527 int i;
12528 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12530 xassert (font != NULL);
12532 for (i = 0; i < dpyinfo->n_fonts; i++)
12533 if (dpyinfo->font_table[i].name
12534 && font == dpyinfo->font_table[i].font)
12535 break;
12537 xassert (i < dpyinfo->n_fonts);
12540 #endif /* GLYPH_DEBUG != 0 */
12542 /* Set *W to the minimum width, *H to the minimum font height of FONT.
12543 Note: There are (broken) X fonts out there with invalid XFontStruct
12544 min_bounds contents. For example, handa@etl.go.jp reports that
12545 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12546 have font->min_bounds.width == 0. */
12548 static INLINE void
12549 x_font_min_bounds (font, w, h)
12550 XFontStruct *font;
12551 int *w, *h;
12553 *h = FONT_HEIGHT (font);
12554 *w = font->min_bounds.width;
12556 /* Try to handle the case where FONT->min_bounds has invalid
12557 contents. Since the only font known to have invalid min_bounds
12558 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12559 if (*w <= 0)
12560 *w = font->max_bounds.width;
12564 /* Compute the smallest character width and smallest font height over
12565 all fonts available on frame F. Set the members smallest_char_width
12566 and smallest_font_height in F's x_display_info structure to
12567 the values computed. Value is non-zero if smallest_font_height or
12568 smallest_char_width become smaller than they were before. */
12570 static int
12571 x_compute_min_glyph_bounds (f)
12572 struct frame *f;
12574 int i;
12575 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12576 XFontStruct *font;
12577 int old_width = dpyinfo->smallest_char_width;
12578 int old_height = dpyinfo->smallest_font_height;
12580 dpyinfo->smallest_font_height = 100000;
12581 dpyinfo->smallest_char_width = 100000;
12583 for (i = 0; i < dpyinfo->n_fonts; ++i)
12584 if (dpyinfo->font_table[i].name)
12586 struct font_info *fontp = dpyinfo->font_table + i;
12587 int w, h;
12589 font = (XFontStruct *) fontp->font;
12590 xassert (font != (XFontStruct *) ~0);
12591 x_font_min_bounds (font, &w, &h);
12593 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12594 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12597 xassert (dpyinfo->smallest_char_width > 0
12598 && dpyinfo->smallest_font_height > 0);
12600 return (dpyinfo->n_fonts == 1
12601 || dpyinfo->smallest_char_width < old_width
12602 || dpyinfo->smallest_font_height < old_height);
12606 /* Load font named FONTNAME of the size SIZE for frame F, and return a
12607 pointer to the structure font_info while allocating it dynamically.
12608 If SIZE is 0, load any size of font.
12609 If loading is failed, return NULL. */
12611 struct font_info *
12612 x_load_font (f, fontname, size)
12613 struct frame *f;
12614 register char *fontname;
12615 int size;
12617 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12618 Lisp_Object font_names;
12619 int count;
12621 /* Get a list of all the fonts that match this name. Once we
12622 have a list of matching fonts, we compare them against the fonts
12623 we already have by comparing names. */
12624 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12626 if (!NILP (font_names))
12628 Lisp_Object tail;
12629 int i;
12631 for (i = 0; i < dpyinfo->n_fonts; i++)
12632 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
12633 if (dpyinfo->font_table[i].name
12634 && (!strcmp (dpyinfo->font_table[i].name,
12635 XSTRING (XCAR (tail))->data)
12636 || !strcmp (dpyinfo->font_table[i].full_name,
12637 XSTRING (XCAR (tail))->data)))
12638 return (dpyinfo->font_table + i);
12641 /* Load the font and add it to the table. */
12643 char *full_name;
12644 XFontStruct *font;
12645 struct font_info *fontp;
12646 unsigned long value;
12647 int i;
12649 /* If we have found fonts by x_list_font, load one of them. If
12650 not, we still try to load a font by the name given as FONTNAME
12651 because XListFonts (called in x_list_font) of some X server has
12652 a bug of not finding a font even if the font surely exists and
12653 is loadable by XLoadQueryFont. */
12654 if (size > 0 && !NILP (font_names))
12655 fontname = (char *) XSTRING (XCAR (font_names))->data;
12657 BLOCK_INPUT;
12658 count = x_catch_errors (FRAME_X_DISPLAY (f));
12659 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12660 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12662 /* This error is perhaps due to insufficient memory on X
12663 server. Let's just ignore it. */
12664 font = NULL;
12665 x_clear_errors (FRAME_X_DISPLAY (f));
12667 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12668 UNBLOCK_INPUT;
12669 if (!font)
12670 return NULL;
12672 /* Find a free slot in the font table. */
12673 for (i = 0; i < dpyinfo->n_fonts; ++i)
12674 if (dpyinfo->font_table[i].name == NULL)
12675 break;
12677 /* If no free slot found, maybe enlarge the font table. */
12678 if (i == dpyinfo->n_fonts
12679 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12681 int sz;
12682 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12683 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12684 dpyinfo->font_table
12685 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12688 fontp = dpyinfo->font_table + i;
12689 if (i == dpyinfo->n_fonts)
12690 ++dpyinfo->n_fonts;
12692 /* Now fill in the slots of *FONTP. */
12693 BLOCK_INPUT;
12694 fontp->font = font;
12695 fontp->font_idx = i;
12696 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12697 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12699 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12700 full_name = 0;
12701 if (XGetFontProperty (font, XA_FONT, &value))
12703 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12704 char *p = name;
12705 int dashes = 0;
12707 /* Count the number of dashes in the "full name".
12708 If it is too few, this isn't really the font's full name,
12709 so don't use it.
12710 In X11R4, the fonts did not come with their canonical names
12711 stored in them. */
12712 while (*p)
12714 if (*p == '-')
12715 dashes++;
12716 p++;
12719 if (dashes >= 13)
12721 full_name = (char *) xmalloc (p - name + 1);
12722 bcopy (name, full_name, p - name + 1);
12725 XFree (name);
12728 if (full_name != 0)
12729 fontp->full_name = full_name;
12730 else
12731 fontp->full_name = fontp->name;
12733 fontp->size = font->max_bounds.width;
12734 fontp->height = FONT_HEIGHT (font);
12736 /* For some font, ascent and descent in max_bounds field is
12737 larger than the above value. */
12738 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12739 if (max_height > fontp->height)
12740 fontp->height = max_height;
12743 if (NILP (font_names))
12745 /* We come here because of a bug of XListFonts mentioned at
12746 the head of this block. Let's store this information in
12747 the cache for x_list_fonts. */
12748 Lisp_Object lispy_name = build_string (fontname);
12749 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12751 XCDR (dpyinfo->name_list_element)
12752 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12753 Fcons (Fcons (lispy_full_name,
12754 make_number (fontp->size)),
12755 Qnil)),
12756 XCDR (dpyinfo->name_list_element));
12757 if (full_name)
12758 XCDR (dpyinfo->name_list_element)
12759 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12760 Fcons (Fcons (lispy_full_name,
12761 make_number (fontp->size)),
12762 Qnil)),
12763 XCDR (dpyinfo->name_list_element));
12766 /* The slot `encoding' specifies how to map a character
12767 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
12768 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12769 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
12770 2:0xA020..0xFF7F). For the moment, we don't know which charset
12771 uses this font. So, we set information in fontp->encoding[1]
12772 which is never used by any charset. If mapping can't be
12773 decided, set FONT_ENCODING_NOT_DECIDED. */
12774 fontp->encoding[1]
12775 = (font->max_byte1 == 0
12776 /* 1-byte font */
12777 ? (font->min_char_or_byte2 < 0x80
12778 ? (font->max_char_or_byte2 < 0x80
12779 ? 0 /* 0x20..0x7F */
12780 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
12781 : 1) /* 0xA0..0xFF */
12782 /* 2-byte font */
12783 : (font->min_byte1 < 0x80
12784 ? (font->max_byte1 < 0x80
12785 ? (font->min_char_or_byte2 < 0x80
12786 ? (font->max_char_or_byte2 < 0x80
12787 ? 0 /* 0x2020..0x7F7F */
12788 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
12789 : 3) /* 0x20A0..0x7FFF */
12790 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
12791 : (font->min_char_or_byte2 < 0x80
12792 ? (font->max_char_or_byte2 < 0x80
12793 ? 2 /* 0xA020..0xFF7F */
12794 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
12795 : 1))); /* 0xA0A0..0xFFFF */
12797 fontp->baseline_offset
12798 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12799 ? (long) value : 0);
12800 fontp->relative_compose
12801 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12802 ? (long) value : 0);
12803 fontp->default_ascent
12804 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12805 ? (long) value : 0);
12807 /* Set global flag fonts_changed_p to non-zero if the font loaded
12808 has a character with a smaller width than any other character
12809 before, or if the font loaded has a smalle>r height than any
12810 other font loaded before. If this happens, it will make a
12811 glyph matrix reallocation necessary. */
12812 fonts_changed_p = x_compute_min_glyph_bounds (f);
12813 UNBLOCK_INPUT;
12814 return fontp;
12819 /* Return a pointer to struct font_info of a font named FONTNAME for
12820 frame F. If no such font is loaded, return NULL. */
12822 struct font_info *
12823 x_query_font (f, fontname)
12824 struct frame *f;
12825 register char *fontname;
12827 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12828 int i;
12830 for (i = 0; i < dpyinfo->n_fonts; i++)
12831 if (dpyinfo->font_table[i].name
12832 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12833 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
12834 return (dpyinfo->font_table + i);
12835 return NULL;
12839 /* Find a CCL program for a font specified by FONTP, and set the member
12840 `encoder' of the structure. */
12842 void
12843 x_find_ccl_program (fontp)
12844 struct font_info *fontp;
12846 Lisp_Object list, elt;
12848 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
12850 elt = XCAR (list);
12851 if (CONSP (elt)
12852 && STRINGP (XCAR (elt))
12853 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
12854 >= 0))
12855 break;
12857 if (! NILP (list))
12859 struct ccl_program *ccl
12860 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
12862 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
12863 xfree (ccl);
12864 else
12865 fontp->font_encoder = ccl;
12871 /***********************************************************************
12872 Initialization
12873 ***********************************************************************/
12875 #ifdef USE_X_TOOLKIT
12876 static XrmOptionDescRec emacs_options[] = {
12877 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
12878 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
12880 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
12881 XrmoptionSepArg, NULL},
12882 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
12884 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12885 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12886 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
12887 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12888 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
12889 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
12890 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
12892 #endif /* USE_X_TOOLKIT */
12894 static int x_initialized;
12896 #ifdef MULTI_KBOARD
12897 /* Test whether two display-name strings agree up to the dot that separates
12898 the screen number from the server number. */
12899 static int
12900 same_x_server (name1, name2)
12901 char *name1, *name2;
12903 int seen_colon = 0;
12904 unsigned char *system_name = XSTRING (Vsystem_name)->data;
12905 int system_name_length = strlen (system_name);
12906 int length_until_period = 0;
12908 while (system_name[length_until_period] != 0
12909 && system_name[length_until_period] != '.')
12910 length_until_period++;
12912 /* Treat `unix' like an empty host name. */
12913 if (! strncmp (name1, "unix:", 5))
12914 name1 += 4;
12915 if (! strncmp (name2, "unix:", 5))
12916 name2 += 4;
12917 /* Treat this host's name like an empty host name. */
12918 if (! strncmp (name1, system_name, system_name_length)
12919 && name1[system_name_length] == ':')
12920 name1 += system_name_length;
12921 if (! strncmp (name2, system_name, system_name_length)
12922 && name2[system_name_length] == ':')
12923 name2 += system_name_length;
12924 /* Treat this host's domainless name like an empty host name. */
12925 if (! strncmp (name1, system_name, length_until_period)
12926 && name1[length_until_period] == ':')
12927 name1 += length_until_period;
12928 if (! strncmp (name2, system_name, length_until_period)
12929 && name2[length_until_period] == ':')
12930 name2 += length_until_period;
12932 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
12934 if (*name1 == ':')
12935 seen_colon++;
12936 if (seen_colon && *name1 == '.')
12937 return 1;
12939 return (seen_colon
12940 && (*name1 == '.' || *name1 == '\0')
12941 && (*name2 == '.' || *name2 == '\0'));
12943 #endif
12945 struct x_display_info *
12946 x_term_init (display_name, xrm_option, resource_name)
12947 Lisp_Object display_name;
12948 char *xrm_option;
12949 char *resource_name;
12951 int connection;
12952 Display *dpy;
12953 struct x_display_info *dpyinfo;
12954 XrmDatabase xrdb;
12956 BLOCK_INPUT;
12958 if (!x_initialized)
12960 x_initialize ();
12961 x_initialized = 1;
12964 #ifdef USE_X_TOOLKIT
12965 /* weiner@footloose.sps.mot.com reports that this causes
12966 errors with X11R5:
12967 X protocol error: BadAtom (invalid Atom parameter)
12968 on protocol request 18skiloaf.
12969 So let's not use it until R6. */
12970 #ifdef HAVE_X11XTR6
12971 XtSetLanguageProc (NULL, NULL, NULL);
12972 #endif
12975 int argc = 0;
12976 char *argv[3];
12978 argv[0] = "";
12979 argc = 1;
12980 if (xrm_option)
12982 argv[argc++] = "-xrm";
12983 argv[argc++] = xrm_option;
12985 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
12986 resource_name, EMACS_CLASS,
12987 emacs_options, XtNumber (emacs_options),
12988 &argc, argv);
12990 #ifdef HAVE_X11XTR6
12991 /* I think this is to compensate for XtSetLanguageProc. */
12992 fixup_locale ();
12993 #endif
12996 #else /* not USE_X_TOOLKIT */
12997 #ifdef HAVE_X11R5
12998 XSetLocaleModifiers ("");
12999 #endif
13000 dpy = XOpenDisplay (XSTRING (display_name)->data);
13001 #endif /* not USE_X_TOOLKIT */
13003 /* Detect failure. */
13004 if (dpy == 0)
13006 UNBLOCK_INPUT;
13007 return 0;
13010 /* We have definitely succeeded. Record the new connection. */
13012 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13014 #ifdef MULTI_KBOARD
13016 struct x_display_info *share;
13017 Lisp_Object tail;
13019 for (share = x_display_list, tail = x_display_name_list; share;
13020 share = share->next, tail = XCDR (tail))
13021 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13022 XSTRING (display_name)->data))
13023 break;
13024 if (share)
13025 dpyinfo->kboard = share->kboard;
13026 else
13028 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13029 init_kboard (dpyinfo->kboard);
13030 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13032 char *vendor = ServerVendor (dpy);
13033 UNBLOCK_INPUT;
13034 dpyinfo->kboard->Vsystem_key_alist
13035 = call1 (Qvendor_specific_keysyms,
13036 build_string (vendor ? vendor : ""));
13037 BLOCK_INPUT;
13040 dpyinfo->kboard->next_kboard = all_kboards;
13041 all_kboards = dpyinfo->kboard;
13042 /* Don't let the initial kboard remain current longer than necessary.
13043 That would cause problems if a file loaded on startup tries to
13044 prompt in the mini-buffer. */
13045 if (current_kboard == initial_kboard)
13046 current_kboard = dpyinfo->kboard;
13048 dpyinfo->kboard->reference_count++;
13050 #endif
13052 /* Put this display on the chain. */
13053 dpyinfo->next = x_display_list;
13054 x_display_list = dpyinfo;
13056 /* Put it on x_display_name_list as well, to keep them parallel. */
13057 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13058 x_display_name_list);
13059 dpyinfo->name_list_element = XCAR (x_display_name_list);
13061 dpyinfo->display = dpy;
13063 #if 0
13064 XSetAfterFunction (x_current_display, x_trace_wire);
13065 #endif /* ! 0 */
13067 dpyinfo->x_id_name
13068 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13069 + STRING_BYTES (XSTRING (Vsystem_name))
13070 + 2);
13071 sprintf (dpyinfo->x_id_name, "%s@%s",
13072 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13074 /* Figure out which modifier bits mean what. */
13075 x_find_modifier_meanings (dpyinfo);
13077 /* Get the scroll bar cursor. */
13078 dpyinfo->vertical_scroll_bar_cursor
13079 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13081 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13082 resource_name, EMACS_CLASS);
13083 #ifdef HAVE_XRMSETDATABASE
13084 XrmSetDatabase (dpyinfo->display, xrdb);
13085 #else
13086 dpyinfo->display->db = xrdb;
13087 #endif
13088 /* Put the rdb where we can find it in a way that works on
13089 all versions. */
13090 dpyinfo->xrdb = xrdb;
13092 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13093 DefaultScreen (dpyinfo->display));
13094 select_visual (dpyinfo);
13095 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13096 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13097 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13098 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13099 dpyinfo->grabbed = 0;
13100 dpyinfo->reference_count = 0;
13101 dpyinfo->icon_bitmap_id = -1;
13102 dpyinfo->font_table = NULL;
13103 dpyinfo->n_fonts = 0;
13104 dpyinfo->font_table_size = 0;
13105 dpyinfo->bitmaps = 0;
13106 dpyinfo->bitmaps_size = 0;
13107 dpyinfo->bitmaps_last = 0;
13108 dpyinfo->scratch_cursor_gc = 0;
13109 dpyinfo->mouse_face_mouse_frame = 0;
13110 dpyinfo->mouse_face_deferred_gc = 0;
13111 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13112 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13113 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13114 dpyinfo->mouse_face_window = Qnil;
13115 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13116 dpyinfo->mouse_face_defer = 0;
13117 dpyinfo->x_focus_frame = 0;
13118 dpyinfo->x_focus_event_frame = 0;
13119 dpyinfo->x_highlight_frame = 0;
13120 dpyinfo->image_cache = make_image_cache ();
13122 /* See if a private colormap is requested. */
13123 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13125 if (dpyinfo->visual->class == PseudoColor)
13127 Lisp_Object value;
13128 value = display_x_get_resource (dpyinfo,
13129 build_string ("privateColormap"),
13130 build_string ("PrivateColormap"),
13131 Qnil, Qnil);
13132 if (STRINGP (value)
13133 && (!strcmp (XSTRING (value)->data, "true")
13134 || !strcmp (XSTRING (value)->data, "on")))
13135 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13138 else
13139 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13140 dpyinfo->visual, AllocNone);
13143 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13144 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13145 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13146 dpyinfo->resy = pixels * 25.4 / mm;
13147 pixels = DisplayWidth (dpyinfo->display, screen_number);
13148 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13149 dpyinfo->resx = pixels * 25.4 / mm;
13152 dpyinfo->Xatom_wm_protocols
13153 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13154 dpyinfo->Xatom_wm_take_focus
13155 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13156 dpyinfo->Xatom_wm_save_yourself
13157 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13158 dpyinfo->Xatom_wm_delete_window
13159 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13160 dpyinfo->Xatom_wm_change_state
13161 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13162 dpyinfo->Xatom_wm_configure_denied
13163 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13164 dpyinfo->Xatom_wm_window_moved
13165 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13166 dpyinfo->Xatom_editres
13167 = XInternAtom (dpyinfo->display, "Editres", False);
13168 dpyinfo->Xatom_CLIPBOARD
13169 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13170 dpyinfo->Xatom_TIMESTAMP
13171 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13172 dpyinfo->Xatom_TEXT
13173 = XInternAtom (dpyinfo->display, "TEXT", False);
13174 dpyinfo->Xatom_COMPOUND_TEXT
13175 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13176 dpyinfo->Xatom_DELETE
13177 = XInternAtom (dpyinfo->display, "DELETE", False);
13178 dpyinfo->Xatom_MULTIPLE
13179 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13180 dpyinfo->Xatom_INCR
13181 = XInternAtom (dpyinfo->display, "INCR", False);
13182 dpyinfo->Xatom_EMACS_TMP
13183 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13184 dpyinfo->Xatom_TARGETS
13185 = XInternAtom (dpyinfo->display, "TARGETS", False);
13186 dpyinfo->Xatom_NULL
13187 = XInternAtom (dpyinfo->display, "NULL", False);
13188 dpyinfo->Xatom_ATOM_PAIR
13189 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13190 /* For properties of font. */
13191 dpyinfo->Xatom_PIXEL_SIZE
13192 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13193 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13194 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13195 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13196 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13197 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13198 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13200 /* Ghostscript support. */
13201 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13202 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13204 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13205 False);
13207 dpyinfo->cut_buffers_initialized = 0;
13209 connection = ConnectionNumber (dpyinfo->display);
13210 dpyinfo->connection = connection;
13213 char null_bits[1];
13215 null_bits[0] = 0x00;
13217 dpyinfo->null_pixel
13218 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13219 null_bits, 1, 1, (long) 0, (long) 0,
13224 extern int gray_bitmap_width, gray_bitmap_height;
13225 extern unsigned char *gray_bitmap_bits;
13226 dpyinfo->gray
13227 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13228 gray_bitmap_bits,
13229 gray_bitmap_width, gray_bitmap_height,
13230 (unsigned long) 1, (unsigned long) 0, 1);
13233 #ifdef HAVE_X_I18N
13234 xim_initialize (dpyinfo, resource_name);
13235 #endif
13237 #ifdef subprocesses
13238 /* This is only needed for distinguishing keyboard and process input. */
13239 if (connection != 0)
13240 add_keyboard_wait_descriptor (connection);
13241 #endif
13243 #ifndef F_SETOWN_BUG
13244 #ifdef F_SETOWN
13245 #ifdef F_SETOWN_SOCK_NEG
13246 /* stdin is a socket here */
13247 fcntl (connection, F_SETOWN, -getpid ());
13248 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13249 fcntl (connection, F_SETOWN, getpid ());
13250 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13251 #endif /* ! defined (F_SETOWN) */
13252 #endif /* F_SETOWN_BUG */
13254 #ifdef SIGIO
13255 if (interrupt_input)
13256 init_sigio (connection);
13257 #endif /* ! defined (SIGIO) */
13259 #ifdef USE_LUCID
13260 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13261 /* Make sure that we have a valid font for dialog boxes
13262 so that Xt does not crash. */
13264 Display *dpy = dpyinfo->display;
13265 XrmValue d, fr, to;
13266 Font font;
13267 int count;
13269 d.addr = (XPointer)&dpy;
13270 d.size = sizeof (Display *);
13271 fr.addr = XtDefaultFont;
13272 fr.size = sizeof (XtDefaultFont);
13273 to.size = sizeof (Font *);
13274 to.addr = (XPointer)&font;
13275 count = x_catch_errors (dpy);
13276 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13277 abort ();
13278 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13279 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13280 x_uncatch_errors (dpy, count);
13282 #endif
13283 #endif
13285 /* See if we should run in synchronous mode. This is useful
13286 for debugging X code. */
13288 Lisp_Object value;
13289 value = display_x_get_resource (dpyinfo,
13290 build_string ("synchronous"),
13291 build_string ("Synchronous"),
13292 Qnil, Qnil);
13293 if (STRINGP (value)
13294 && (!strcmp (XSTRING (value)->data, "true")
13295 || !strcmp (XSTRING (value)->data, "on")))
13296 XSynchronize (dpyinfo->display, True);
13299 UNBLOCK_INPUT;
13301 return dpyinfo;
13304 /* Get rid of display DPYINFO, assuming all frames are already gone,
13305 and without sending any more commands to the X server. */
13307 void
13308 x_delete_display (dpyinfo)
13309 struct x_display_info *dpyinfo;
13311 delete_keyboard_wait_descriptor (dpyinfo->connection);
13313 /* Discard this display from x_display_name_list and x_display_list.
13314 We can't use Fdelq because that can quit. */
13315 if (! NILP (x_display_name_list)
13316 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13317 x_display_name_list = XCDR (x_display_name_list);
13318 else
13320 Lisp_Object tail;
13322 tail = x_display_name_list;
13323 while (CONSP (tail) && CONSP (XCDR (tail)))
13325 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13327 XCDR (tail) = XCDR (XCDR (tail));
13328 break;
13330 tail = XCDR (tail);
13334 if (next_noop_dpyinfo == dpyinfo)
13335 next_noop_dpyinfo = dpyinfo->next;
13337 if (x_display_list == dpyinfo)
13338 x_display_list = dpyinfo->next;
13339 else
13341 struct x_display_info *tail;
13343 for (tail = x_display_list; tail; tail = tail->next)
13344 if (tail->next == dpyinfo)
13345 tail->next = tail->next->next;
13348 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13349 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13350 XrmDestroyDatabase (dpyinfo->xrdb);
13351 #endif
13352 #endif
13353 #ifdef MULTI_KBOARD
13354 if (--dpyinfo->kboard->reference_count == 0)
13355 delete_kboard (dpyinfo->kboard);
13356 #endif
13357 #ifdef HAVE_X_I18N
13358 if (dpyinfo->xim)
13359 xim_close_dpy (dpyinfo);
13360 #endif
13362 xfree (dpyinfo->font_table);
13363 xfree (dpyinfo->x_id_name);
13364 xfree (dpyinfo);
13367 /* Set up use of X before we make the first connection. */
13369 static struct redisplay_interface x_redisplay_interface =
13371 x_produce_glyphs,
13372 x_write_glyphs,
13373 x_insert_glyphs,
13374 x_clear_end_of_line,
13375 x_scroll_run,
13376 x_after_update_window_line,
13377 x_update_window_begin,
13378 x_update_window_end,
13379 XTcursor_to,
13380 x_flush,
13381 x_get_glyph_overhangs,
13382 x_fix_overlapping_area
13385 void
13386 x_initialize ()
13388 rif = &x_redisplay_interface;
13390 clear_frame_hook = x_clear_frame;
13391 ins_del_lines_hook = x_ins_del_lines;
13392 change_line_highlight_hook = x_change_line_highlight;
13393 delete_glyphs_hook = x_delete_glyphs;
13394 ring_bell_hook = XTring_bell;
13395 reset_terminal_modes_hook = XTreset_terminal_modes;
13396 set_terminal_modes_hook = XTset_terminal_modes;
13397 update_begin_hook = x_update_begin;
13398 update_end_hook = x_update_end;
13399 set_terminal_window_hook = XTset_terminal_window;
13400 read_socket_hook = XTread_socket;
13401 frame_up_to_date_hook = XTframe_up_to_date;
13402 reassert_line_highlight_hook = XTreassert_line_highlight;
13403 mouse_position_hook = XTmouse_position;
13404 frame_rehighlight_hook = XTframe_rehighlight;
13405 frame_raise_lower_hook = XTframe_raise_lower;
13406 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13407 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13408 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13409 judge_scroll_bars_hook = XTjudge_scroll_bars;
13410 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13412 scroll_region_ok = 1; /* we'll scroll partial frames */
13413 char_ins_del_ok = 0; /* just as fast to write the line */
13414 line_ins_del_ok = 1; /* we'll just blt 'em */
13415 fast_clear_end_of_line = 1; /* X does this well */
13416 memory_below_frame = 0; /* we don't remember what scrolls
13417 off the bottom */
13418 baud_rate = 19200;
13420 x_noop_count = 0;
13421 last_tool_bar_item = -1;
13422 any_help_event_p = 0;
13424 /* Try to use interrupt input; if we can't, then start polling. */
13425 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13427 #ifdef USE_X_TOOLKIT
13428 XtToolkitInitialize ();
13429 Xt_app_con = XtCreateApplicationContext ();
13430 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13432 /* Install an asynchronous timer that processes Xt timeout events
13433 every 0.1s. This is necessary because some widget sets use
13434 timeouts internally, for example the LessTif menu bar, or the
13435 Xaw3d scroll bar. When Xt timouts aren't processed, these
13436 widgets don't behave normally. */
13438 EMACS_TIME interval;
13439 EMACS_SET_SECS_USECS (interval, 0, 100000);
13440 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13442 #endif
13444 #if USE_TOOLKIT_SCROLL_BARS
13445 xaw3d_arrow_scroll = False;
13446 xaw3d_pick_top = True;
13447 #endif
13449 /* Note that there is no real way portable across R3/R4 to get the
13450 original error handler. */
13451 XSetErrorHandler (x_error_handler);
13452 XSetIOErrorHandler (x_io_error_quitter);
13454 /* Disable Window Change signals; they are handled by X events. */
13455 #ifdef SIGWINCH
13456 signal (SIGWINCH, SIG_DFL);
13457 #endif /* ! defined (SIGWINCH) */
13459 signal (SIGPIPE, x_connection_signal);
13463 void
13464 syms_of_xterm ()
13466 staticpro (&x_error_message_string);
13467 x_error_message_string = Qnil;
13469 staticpro (&x_display_name_list);
13470 x_display_name_list = Qnil;
13472 staticpro (&last_mouse_scroll_bar);
13473 last_mouse_scroll_bar = Qnil;
13475 staticpro (&Qvendor_specific_keysyms);
13476 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13478 staticpro (&last_mouse_press_frame);
13479 last_mouse_press_frame = Qnil;
13481 staticpro (&help_echo);
13482 help_echo = Qnil;
13483 staticpro (&previous_help_echo);
13484 previous_help_echo = Qnil;
13486 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13487 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13488 For example, if a block cursor is over a tab, it will be drawn as\n\
13489 wide as that tab on the display.");
13490 x_stretch_cursor_p = 0;
13492 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13493 "If not nil, Emacs uses toolkit scroll bars.");
13494 #if USE_TOOLKIT_SCROLL_BARS
13495 x_toolkit_scroll_bars_p = 1;
13496 #else
13497 x_toolkit_scroll_bars_p = 0;
13498 #endif
13500 staticpro (&last_mouse_motion_frame);
13501 last_mouse_motion_frame = Qnil;
13504 #endif /* not HAVE_X_WINDOWS */