(cvs-menu): Don't move point. Use popup-menu.
[emacs.git] / src / xterm.c
blobdbd4a3d2068d4e580a3993d50417cff4fc90d09e
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 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "ccl.h"
72 #include "frame.h"
73 #include "dispextern.h"
74 #include "fontset.h"
75 #include "termhooks.h"
76 #include "termopts.h"
77 #include "termchar.h"
78 #if 0
79 #include "sink.h"
80 #include "sinkmask.h"
81 #endif /* ! 0 */
82 #include "gnu.h"
83 #include "disptab.h"
84 #include "buffer.h"
85 #include "window.h"
86 #include "keyboard.h"
87 #include "intervals.h"
88 #include "process.h"
89 #include "atimer.h"
91 #ifdef USE_X_TOOLKIT
92 #include <X11/Shell.h>
93 #endif
95 #ifdef HAVE_SYS_TIME_H
96 #include <sys/time.h>
97 #endif
98 #ifdef HAVE_UNISTD_H
99 #include <unistd.h>
100 #endif
102 #ifdef USE_X_TOOLKIT
104 extern void free_frame_menubar P_ ((struct frame *));
105 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
106 int));
108 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
109 #define HACK_EDITRES
110 extern void _XEditResCheckMessages ();
111 #endif /* not NO_EDITRES */
113 /* Include toolkit specific headers for the scroll bar widget. */
115 #ifdef USE_TOOLKIT_SCROLL_BARS
116 #if defined USE_MOTIF
117 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
118 #include <Xm/ScrollBar.h>
119 #include <Xm/ScrollBarP.h>
120 #else /* !USE_MOTIF i.e. use Xaw */
122 #ifdef HAVE_XAW3D
123 #include <X11/Xaw3d/Simple.h>
124 #include <X11/Xaw3d/Scrollbar.h>
125 #define ARROW_SCROLLBAR
126 #include <X11/Xaw3d/ScrollbarP.h>
127 #else /* !HAVE_XAW3D */
128 #include <X11/Xaw/Simple.h>
129 #include <X11/Xaw/Scrollbar.h>
130 #endif /* !HAVE_XAW3D */
131 #ifndef XtNpickTop
132 #define XtNpickTop "pickTop"
133 #endif /* !XtNpickTop */
134 #endif /* !USE_MOTIF */
135 #endif /* USE_TOOLKIT_SCROLL_BARS */
137 #endif /* USE_X_TOOLKIT */
139 #ifndef USE_X_TOOLKIT
140 #define x_any_window_to_frame x_window_to_frame
141 #define x_top_window_to_frame x_window_to_frame
142 #endif
144 #ifdef USE_X_TOOLKIT
145 #include "widget.h"
146 #ifndef XtNinitialState
147 #define XtNinitialState "initialState"
148 #endif
149 #endif
151 #ifdef SOLARIS2
152 /* memmove will be defined as a macro in Xfuncs.h unless
153 <string.h> is included beforehand. The declaration for memmove in
154 <string.h> will cause a syntax error when Xfuncs.h later includes it. */
155 #include <string.h>
156 #endif
158 #ifndef min
159 #define min(a,b) ((a) < (b) ? (a) : (b))
160 #endif
161 #ifndef max
162 #define max(a,b) ((a) > (b) ? (a) : (b))
163 #endif
165 #define abs(x) ((x) < 0 ? -(x) : (x))
167 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
170 /* Bitmaps for truncated lines. */
172 enum bitmap_type
174 NO_BITMAP,
175 LEFT_TRUNCATION_BITMAP,
176 RIGHT_TRUNCATION_BITMAP,
177 OVERLAY_ARROW_BITMAP,
178 CONTINUED_LINE_BITMAP,
179 CONTINUATION_LINE_BITMAP,
180 ZV_LINE_BITMAP
183 /* Bitmap drawn to indicate lines not displaying text if
184 `indicate-empty-lines' is non-nil. */
186 #define zv_width 8
187 #define zv_height 8
188 static unsigned char zv_bits[] = {
189 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
191 /* An arrow like this: `<-'. */
193 #define left_width 8
194 #define left_height 8
195 static unsigned char left_bits[] = {
196 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
198 /* Right truncation arrow bitmap `->'. */
200 #define right_width 8
201 #define right_height 8
202 static unsigned char right_bits[] = {
203 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
205 /* Marker for continued lines. */
207 #define continued_width 8
208 #define continued_height 8
209 static unsigned char continued_bits[] = {
210 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
212 /* Marker for continuation lines. */
214 #define continuation_width 8
215 #define continuation_height 8
216 static unsigned char continuation_bits[] = {
217 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
219 /* Overlay arrow bitmap. */
221 #if 0
222 /* A bomb. */
223 #define ov_width 8
224 #define ov_height 8
225 static unsigned char ov_bits[] = {
226 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
227 #else
228 /* A triangular arrow. */
229 #define ov_width 8
230 #define ov_height 8
231 static unsigned char ov_bits[] = {
232 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
234 #endif
236 extern Lisp_Object Qhelp_echo;
239 /* Non-zero means Emacs uses toolkit scroll bars. */
241 int x_toolkit_scroll_bars_p;
243 /* If a string, XTread_socket generates an event to display that string.
244 (The display is done in read_char.) */
246 static Lisp_Object help_echo;
247 static Lisp_Object help_echo_window;
248 static Lisp_Object help_echo_object;
249 static int help_echo_pos;
251 /* Temporary variable for XTread_socket. */
253 static Lisp_Object previous_help_echo;
255 /* Non-zero means that a HELP_EVENT has been generated since Emacs
256 start. */
258 static int any_help_event_p;
260 /* Non-zero means draw block and hollow cursor as wide as the glyph
261 under it. For example, if a block cursor is over a tab, it will be
262 drawn as wide as that tab on the display. */
264 int x_stretch_cursor_p;
266 /* This is a chain of structures for all the X displays currently in
267 use. */
269 struct x_display_info *x_display_list;
271 /* This is a list of cons cells, each of the form (NAME
272 . FONT-LIST-CACHE), one for each element of x_display_list and in
273 the same order. NAME is the name of the frame. FONT-LIST-CACHE
274 records previous values returned by x-list-fonts. */
276 Lisp_Object x_display_name_list;
278 /* Frame being updated by update_frame. This is declared in term.c.
279 This is set by update_begin and looked at by all the XT functions.
280 It is zero while not inside an update. In that case, the XT
281 functions assume that `selected_frame' is the frame to apply to. */
283 extern struct frame *updating_frame;
285 extern int waiting_for_input;
287 /* This is a frame waiting to be auto-raised, within XTread_socket. */
289 struct frame *pending_autoraise_frame;
291 #ifdef USE_X_TOOLKIT
292 /* The application context for Xt use. */
293 XtAppContext Xt_app_con;
294 static String Xt_default_resources[] = {0};
295 #endif /* USE_X_TOOLKIT */
297 /* Nominal cursor position -- where to draw output.
298 HPOS and VPOS are window relative glyph matrix coordinates.
299 X and Y are window relative pixel coordinates. */
301 struct cursor_pos output_cursor;
303 /* Non-zero means user is interacting with a toolkit scroll bar. */
305 static int toolkit_scroll_bar_interaction;
307 /* Mouse movement.
309 Formerly, we used PointerMotionHintMask (in standard_event_mask)
310 so that we would have to call XQueryPointer after each MotionNotify
311 event to ask for another such event. However, this made mouse tracking
312 slow, and there was a bug that made it eventually stop.
314 Simply asking for MotionNotify all the time seems to work better.
316 In order to avoid asking for motion events and then throwing most
317 of them away or busy-polling the server for mouse positions, we ask
318 the server for pointer motion hints. This means that we get only
319 one event per group of mouse movements. "Groups" are delimited by
320 other kinds of events (focus changes and button clicks, for
321 example), or by XQueryPointer calls; when one of these happens, we
322 get another MotionNotify event the next time the mouse moves. This
323 is at least as efficient as getting motion events when mouse
324 tracking is on, and I suspect only negligibly worse when tracking
325 is off. */
327 /* Where the mouse was last time we reported a mouse event. */
329 FRAME_PTR last_mouse_frame;
330 static XRectangle last_mouse_glyph;
331 static Lisp_Object last_mouse_press_frame;
333 /* The scroll bar in which the last X motion event occurred.
335 If the last X motion event occurred in a scroll bar, we set this so
336 XTmouse_position can know whether to report a scroll bar motion or
337 an ordinary motion.
339 If the last X motion event didn't occur in a scroll bar, we set
340 this to Qnil, to tell XTmouse_position to return an ordinary motion
341 event. */
343 static Lisp_Object last_mouse_scroll_bar;
345 /* This is a hack. We would really prefer that XTmouse_position would
346 return the time associated with the position it returns, but there
347 doesn't seem to be any way to wrest the time-stamp from the server
348 along with the position query. So, we just keep track of the time
349 of the last movement we received, and return that in hopes that
350 it's somewhat accurate. */
352 static Time last_mouse_movement_time;
354 /* Incremented by XTread_socket whenever it really tries to read
355 events. */
357 #ifdef __STDC__
358 static int volatile input_signal_count;
359 #else
360 static int input_signal_count;
361 #endif
363 /* Used locally within XTread_socket. */
365 static int x_noop_count;
367 /* Initial values of argv and argc. */
369 extern char **initial_argv;
370 extern int initial_argc;
372 extern Lisp_Object Vcommand_line_args, Vsystem_name;
374 /* Tells if a window manager is present or not. */
376 extern Lisp_Object Vx_no_window_manager;
378 extern Lisp_Object Qface, Qmouse_face;
380 extern int errno;
382 /* A mask of extra modifier bits to put into every keyboard char. */
384 extern int extra_keyboard_modifiers;
386 static Lisp_Object Qvendor_specific_keysyms;
388 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
389 extern Lisp_Object x_icon_type P_ ((struct frame *));
392 /* Enumeration for overriding/changing the face to use for drawing
393 glyphs in x_draw_glyphs. */
395 enum draw_glyphs_face
397 DRAW_NORMAL_TEXT,
398 DRAW_INVERSE_VIDEO,
399 DRAW_CURSOR,
400 DRAW_MOUSE_FACE,
401 DRAW_IMAGE_RAISED,
402 DRAW_IMAGE_SUNKEN
405 static void x_update_window_end P_ ((struct window *, int, int));
406 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
407 void x_delete_display P_ ((struct x_display_info *));
408 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
409 unsigned));
410 static int fast_find_position P_ ((struct window *, int, int *, int *,
411 int *, int *));
412 static void set_output_cursor P_ ((struct cursor_pos *));
413 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
414 int *, int *, int *));
415 static void note_mode_line_highlight P_ ((struct window *, int, int));
416 static void note_mouse_highlight P_ ((struct frame *, int, int));
417 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
418 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
419 static void show_mouse_face P_ ((struct x_display_info *,
420 enum draw_glyphs_face));
421 static int x_io_error_quitter P_ ((Display *));
422 int x_catch_errors P_ ((Display *));
423 void x_uncatch_errors P_ ((Display *, int));
424 void x_lower_frame P_ ((struct frame *));
425 void x_scroll_bar_clear P_ ((struct frame *));
426 int x_had_errors_p P_ ((Display *));
427 void x_wm_set_size_hint P_ ((struct frame *, long, int));
428 void x_raise_frame P_ ((struct frame *));
429 void x_set_window_size P_ ((struct frame *, int, int, int));
430 void x_wm_set_window_state P_ ((struct frame *, int));
431 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
432 void x_initialize P_ ((void));
433 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
434 static int x_compute_min_glyph_bounds P_ ((struct frame *));
435 static void x_draw_phys_cursor_glyph P_ ((struct window *,
436 struct glyph_row *,
437 enum draw_glyphs_face));
438 static void x_update_end P_ ((struct frame *));
439 static void XTframe_up_to_date P_ ((struct frame *));
440 static void XTreassert_line_highlight P_ ((int, int));
441 static void x_change_line_highlight P_ ((int, int, int, int));
442 static void XTset_terminal_modes P_ ((void));
443 static void XTreset_terminal_modes P_ ((void));
444 static void XTcursor_to P_ ((int, int, int, int));
445 static void x_write_glyphs P_ ((struct glyph *, int));
446 static void x_clear_end_of_line P_ ((int));
447 static void x_clear_frame P_ ((void));
448 static void x_clear_cursor P_ ((struct window *));
449 static void frame_highlight P_ ((struct frame *));
450 static void frame_unhighlight P_ ((struct frame *));
451 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
452 static void XTframe_rehighlight P_ ((struct frame *));
453 static void x_frame_rehighlight P_ ((struct x_display_info *));
454 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
455 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
456 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
457 XRectangle *));
458 static void expose_frame P_ ((struct frame *, int, int, int, int));
459 static void expose_window_tree P_ ((struct window *, XRectangle *));
460 static void expose_window P_ ((struct window *, XRectangle *));
461 static void expose_area P_ ((struct window *, struct glyph_row *,
462 XRectangle *, enum glyph_row_area));
463 static void expose_line P_ ((struct window *, struct glyph_row *,
464 XRectangle *));
465 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
466 static void x_update_window_cursor P_ ((struct window *, int));
467 static void x_erase_phys_cursor P_ ((struct window *));
468 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
469 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
470 enum bitmap_type));
472 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
473 GC, int));
474 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
475 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
476 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
477 static void x_flush P_ ((struct frame *f));
478 static void x_update_begin P_ ((struct frame *));
479 static void x_update_window_begin P_ ((struct window *));
480 static void x_draw_vertical_border P_ ((struct window *));
481 static void x_after_update_window_line P_ ((struct glyph_row *));
482 static INLINE void take_vertical_position_into_account P_ ((struct it *));
483 static void x_produce_stretch_glyph P_ ((struct it *));
486 /* Flush display of frame F, or of all frames if F is null. */
488 static void
489 x_flush (f)
490 struct frame *f;
492 BLOCK_INPUT;
493 if (f == NULL)
495 Lisp_Object rest, frame;
496 FOR_EACH_FRAME (rest, frame)
497 x_flush (XFRAME (frame));
499 else if (FRAME_X_P (f))
500 XFlush (FRAME_X_DISPLAY (f));
501 UNBLOCK_INPUT;
505 /* Remove calls to XFlush by defining XFlush to an empty replacement.
506 Calls to XFlush should be unnecessary because the X output buffer
507 is flushed automatically as needed by calls to XPending,
508 XNextEvent, or XWindowEvent according to the XFlush man page.
509 XTread_socket calls XPending. Removing XFlush improves
510 performance. */
512 #define XFlush(DISPLAY) (void) 0
515 /***********************************************************************
516 Debugging
517 ***********************************************************************/
519 #if 0
521 /* This is a function useful for recording debugging information about
522 the sequence of occurrences in this file. */
524 struct record
526 char *locus;
527 int type;
530 struct record event_record[100];
532 int event_record_index;
534 record_event (locus, type)
535 char *locus;
536 int type;
538 if (event_record_index == sizeof (event_record) / sizeof (struct record))
539 event_record_index = 0;
541 event_record[event_record_index].locus = locus;
542 event_record[event_record_index].type = type;
543 event_record_index++;
546 #endif /* 0 */
550 /* Return the struct x_display_info corresponding to DPY. */
552 struct x_display_info *
553 x_display_info_for_display (dpy)
554 Display *dpy;
556 struct x_display_info *dpyinfo;
558 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
559 if (dpyinfo->display == dpy)
560 return dpyinfo;
562 return 0;
567 /***********************************************************************
568 Starting and ending an update
569 ***********************************************************************/
571 /* Start an update of frame F. This function is installed as a hook
572 for update_begin, i.e. it is called when update_begin is called.
573 This function is called prior to calls to x_update_window_begin for
574 each window being updated. Currently, there is nothing to do here
575 because all interesting stuff is done on a window basis. */
577 static void
578 x_update_begin (f)
579 struct frame *f;
581 /* Nothing to do. */
585 /* Start update of window W. Set the global variable updated_window
586 to the window being updated and set output_cursor to the cursor
587 position of W. */
589 static void
590 x_update_window_begin (w)
591 struct window *w;
593 struct frame *f = XFRAME (WINDOW_FRAME (w));
594 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
596 updated_window = w;
597 set_output_cursor (&w->cursor);
599 BLOCK_INPUT;
601 if (f == display_info->mouse_face_mouse_frame)
603 /* Don't do highlighting for mouse motion during the update. */
604 display_info->mouse_face_defer = 1;
606 /* If F needs to be redrawn, simply forget about any prior mouse
607 highlighting. */
608 if (FRAME_GARBAGED_P (f))
609 display_info->mouse_face_window = Qnil;
611 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
612 their mouse_face_p flag set, which means that they are always
613 unequal to rows in a desired matrix which never have that
614 flag set. So, rows containing mouse-face glyphs are never
615 scrolled, and we don't have to switch the mouse highlight off
616 here to prevent it from being scrolled. */
618 /* Can we tell that this update does not affect the window
619 where the mouse highlight is? If so, no need to turn off.
620 Likewise, don't do anything if the frame is garbaged;
621 in that case, the frame's current matrix that we would use
622 is all wrong, and we will redisplay that line anyway. */
623 if (!NILP (display_info->mouse_face_window)
624 && w == XWINDOW (display_info->mouse_face_window))
626 int i;
628 for (i = 0; i < w->desired_matrix->nrows; ++i)
629 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
630 break;
632 if (i < w->desired_matrix->nrows)
633 clear_mouse_face (display_info);
635 #endif /* 0 */
638 UNBLOCK_INPUT;
642 /* Draw a vertical window border to the right of window W if W doesn't
643 have vertical scroll bars. */
645 static void
646 x_draw_vertical_border (w)
647 struct window *w;
649 struct frame *f = XFRAME (WINDOW_FRAME (w));
651 /* Redraw borders between horizontally adjacent windows. Don't
652 do it for frames with vertical scroll bars because either the
653 right scroll bar of a window, or the left scroll bar of its
654 neighbor will suffice as a border. */
655 if (!WINDOW_RIGHTMOST_P (w)
656 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
658 int x0, x1, y0, y1;
660 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
661 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
662 y1 -= 1;
664 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
665 f->output_data.x->normal_gc, x1, y0, x1, y1);
670 /* End update of window W (which is equal to updated_window).
672 Draw vertical borders between horizontally adjacent windows, and
673 display W's cursor if CURSOR_ON_P is non-zero.
675 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
676 glyphs in mouse-face were overwritten. In that case we have to
677 make sure that the mouse-highlight is properly redrawn.
679 W may be a menu bar pseudo-window in case we don't have X toolkit
680 support. Such windows don't have a cursor, so don't display it
681 here. */
683 static void
684 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
685 struct window *w;
686 int cursor_on_p, mouse_face_overwritten_p;
688 if (!w->pseudo_window_p)
690 struct x_display_info *dpyinfo
691 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
693 BLOCK_INPUT;
695 /* If a row with mouse-face was overwritten, arrange for
696 XTframe_up_to_date to redisplay the mouse highlight. */
697 if (mouse_face_overwritten_p)
699 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
700 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
701 dpyinfo->mouse_face_window = Qnil;
704 if (cursor_on_p)
705 x_display_and_set_cursor (w, 1, output_cursor.hpos,
706 output_cursor.vpos,
707 output_cursor.x, output_cursor.y);
709 x_draw_vertical_border (w);
710 UNBLOCK_INPUT;
713 updated_window = NULL;
717 /* End update of frame F. This function is installed as a hook in
718 update_end. */
720 static void
721 x_update_end (f)
722 struct frame *f;
724 /* Mouse highlight may be displayed again. */
725 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
727 BLOCK_INPUT;
728 XFlush (FRAME_X_DISPLAY (f));
729 UNBLOCK_INPUT;
733 /* This function is called from various places in xdisp.c whenever a
734 complete update has been performed. The global variable
735 updated_window is not available here. */
737 static void
738 XTframe_up_to_date (f)
739 struct frame *f;
741 if (FRAME_X_P (f))
743 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
745 if (dpyinfo->mouse_face_deferred_gc
746 || f == dpyinfo->mouse_face_mouse_frame)
748 BLOCK_INPUT;
749 if (dpyinfo->mouse_face_mouse_frame)
750 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
751 dpyinfo->mouse_face_mouse_x,
752 dpyinfo->mouse_face_mouse_y);
753 dpyinfo->mouse_face_deferred_gc = 0;
754 UNBLOCK_INPUT;
760 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
761 arrow bitmaps, or clear the areas where they would be displayed
762 before DESIRED_ROW is made current. The window being updated is
763 found in updated_window. This function It is called from
764 update_window_line only if it is known that there are differences
765 between bitmaps to be drawn between current row and DESIRED_ROW. */
767 static void
768 x_after_update_window_line (desired_row)
769 struct glyph_row *desired_row;
771 struct window *w = updated_window;
773 xassert (w);
775 if (!desired_row->mode_line_p && !w->pseudo_window_p)
777 BLOCK_INPUT;
778 x_draw_row_bitmaps (w, desired_row);
780 /* When a window has disappeared, make sure that no rest of
781 full-width rows stays visible in the internal border. */
782 if (windows_or_buffers_changed)
784 struct frame *f = XFRAME (w->frame);
785 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
786 int height = desired_row->visible_height;
787 int x = (window_box_right (w, -1)
788 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
789 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
791 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
792 x, y, width, height, False);
795 UNBLOCK_INPUT;
800 /* Draw the bitmap WHICH in one of the areas to the left or right of
801 window W. ROW is the glyph row for which to display the bitmap; it
802 determines the vertical position at which the bitmap has to be
803 drawn. */
805 static void
806 x_draw_bitmap (w, row, which)
807 struct window *w;
808 struct glyph_row *row;
809 enum bitmap_type which;
811 struct frame *f = XFRAME (WINDOW_FRAME (w));
812 Display *display = FRAME_X_DISPLAY (f);
813 Window window = FRAME_X_WINDOW (f);
814 int x, y, wd, h, dy;
815 unsigned char *bits;
816 Pixmap pixmap;
817 GC gc = f->output_data.x->normal_gc;
818 struct face *face;
819 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
821 /* Must clip because of partially visible lines. */
822 x_clip_to_row (w, row, gc, 1);
824 switch (which)
826 case LEFT_TRUNCATION_BITMAP:
827 wd = left_width;
828 h = left_height;
829 bits = left_bits;
830 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
831 - wd
832 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
833 break;
835 case OVERLAY_ARROW_BITMAP:
836 wd = left_width;
837 h = left_height;
838 bits = ov_bits;
839 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
840 - wd
841 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
842 break;
844 case RIGHT_TRUNCATION_BITMAP:
845 wd = right_width;
846 h = right_height;
847 bits = right_bits;
848 x = window_box_right (w, -1);
849 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
850 break;
852 case CONTINUED_LINE_BITMAP:
853 wd = right_width;
854 h = right_height;
855 bits = continued_bits;
856 x = window_box_right (w, -1);
857 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
858 break;
860 case CONTINUATION_LINE_BITMAP:
861 wd = continuation_width;
862 h = continuation_height;
863 bits = continuation_bits;
864 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
865 - wd
866 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
867 break;
869 case ZV_LINE_BITMAP:
870 wd = zv_width;
871 h = zv_height;
872 bits = zv_bits;
873 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
874 - wd
875 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
876 break;
878 default:
879 abort ();
882 /* Convert to frame coordinates. Set dy to the offset in the row to
883 start drawing the bitmap. */
884 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
885 dy = (row->height - h) / 2;
887 /* Draw the bitmap. I believe these small pixmaps can be cached
888 by the server. */
889 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
890 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
891 face->foreground,
892 face->background, depth);
893 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
894 XFreePixmap (display, pixmap);
895 XSetClipMask (display, gc, None);
899 /* Draw flags bitmaps for glyph row ROW on window W. Call this
900 function with input blocked. */
902 static void
903 x_draw_row_bitmaps (w, row)
904 struct window *w;
905 struct glyph_row *row;
907 struct frame *f = XFRAME (w->frame);
908 enum bitmap_type bitmap;
909 struct face *face;
910 int header_line_height = -1;
912 xassert (interrupt_input_blocked);
914 /* If row is completely invisible, because of vscrolling, we
915 don't have to draw anything. */
916 if (row->visible_height <= 0)
917 return;
919 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
920 PREPARE_FACE_FOR_DISPLAY (f, face);
922 /* Decide which bitmap to draw at the left side. */
923 if (row->overlay_arrow_p)
924 bitmap = OVERLAY_ARROW_BITMAP;
925 else if (row->truncated_on_left_p)
926 bitmap = LEFT_TRUNCATION_BITMAP;
927 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
928 bitmap = CONTINUATION_LINE_BITMAP;
929 else if (row->indicate_empty_line_p)
930 bitmap = ZV_LINE_BITMAP;
931 else
932 bitmap = NO_BITMAP;
934 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
935 the flags area. */
936 if (bitmap == NO_BITMAP
937 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
938 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
940 /* If W has a vertical border to its left, don't draw over it. */
941 int border = ((XFASTINT (w->left) > 0
942 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
943 ? 1 : 0);
944 int left = window_box_left (w, -1);
946 if (header_line_height < 0)
947 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
949 /* In case the same realized face is used for bitmap areas and
950 for something displayed in the text (e.g. face `region' on
951 mono-displays, the fill style may have been changed to
952 FillSolid in x_draw_glyph_string_background. */
953 if (face->stipple)
954 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
955 else
956 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
958 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
959 face->gc,
960 (left
961 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
962 + border),
963 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
964 row->y)),
965 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
966 row->visible_height);
967 if (!face->stipple)
968 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
971 /* Draw the left bitmap. */
972 if (bitmap != NO_BITMAP)
973 x_draw_bitmap (w, row, bitmap);
975 /* Decide which bitmap to draw at the right side. */
976 if (row->truncated_on_right_p)
977 bitmap = RIGHT_TRUNCATION_BITMAP;
978 else if (row->continued_p)
979 bitmap = CONTINUED_LINE_BITMAP;
980 else
981 bitmap = NO_BITMAP;
983 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
984 the flags area. */
985 if (bitmap == NO_BITMAP
986 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
987 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
989 int right = window_box_right (w, -1);
991 if (header_line_height < 0)
992 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
994 /* In case the same realized face is used for bitmap areas and
995 for something displayed in the text (e.g. face `region' on
996 mono-displays, the fill style may have been changed to
997 FillSolid in x_draw_glyph_string_background. */
998 if (face->stipple)
999 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
1000 else
1001 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
1002 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
1003 face->gc,
1004 right,
1005 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1006 row->y)),
1007 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1008 row->visible_height);
1009 if (!face->stipple)
1010 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1013 /* Draw the right bitmap. */
1014 if (bitmap != NO_BITMAP)
1015 x_draw_bitmap (w, row, bitmap);
1019 /***********************************************************************
1020 Line Highlighting
1021 ***********************************************************************/
1023 /* External interface to control of standout mode. Not used for X
1024 frames. Aborts when called. */
1026 static void
1027 XTreassert_line_highlight (new, vpos)
1028 int new, vpos;
1030 abort ();
1034 /* Call this when about to modify line at position VPOS and change
1035 whether it is highlighted. Not used for X frames. Aborts when
1036 called. */
1038 static void
1039 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1040 int new_highlight, vpos, y, first_unused_hpos;
1042 abort ();
1046 /* This is called when starting Emacs and when restarting after
1047 suspend. When starting Emacs, no X window is mapped. And nothing
1048 must be done to Emacs's own window if it is suspended (though that
1049 rarely happens). */
1051 static void
1052 XTset_terminal_modes ()
1056 /* This is called when exiting or suspending Emacs. Exiting will make
1057 the X-windows go away, and suspending requires no action. */
1059 static void
1060 XTreset_terminal_modes ()
1066 /***********************************************************************
1067 Output Cursor
1068 ***********************************************************************/
1070 /* Set the global variable output_cursor to CURSOR. All cursor
1071 positions are relative to updated_window. */
1073 static void
1074 set_output_cursor (cursor)
1075 struct cursor_pos *cursor;
1077 output_cursor.hpos = cursor->hpos;
1078 output_cursor.vpos = cursor->vpos;
1079 output_cursor.x = cursor->x;
1080 output_cursor.y = cursor->y;
1084 /* Set a nominal cursor position.
1086 HPOS and VPOS are column/row positions in a window glyph matrix. X
1087 and Y are window text area relative pixel positions.
1089 If this is done during an update, updated_window will contain the
1090 window that is being updated and the position is the future output
1091 cursor position for that window. If updated_window is null, use
1092 selected_window and display the cursor at the given position. */
1094 static void
1095 XTcursor_to (vpos, hpos, y, x)
1096 int vpos, hpos, y, x;
1098 struct window *w;
1100 /* If updated_window is not set, work on selected_window. */
1101 if (updated_window)
1102 w = updated_window;
1103 else
1104 w = XWINDOW (selected_window);
1106 /* Set the output cursor. */
1107 output_cursor.hpos = hpos;
1108 output_cursor.vpos = vpos;
1109 output_cursor.x = x;
1110 output_cursor.y = y;
1112 /* If not called as part of an update, really display the cursor.
1113 This will also set the cursor position of W. */
1114 if (updated_window == NULL)
1116 BLOCK_INPUT;
1117 x_display_cursor (w, 1, hpos, vpos, x, y);
1118 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1119 UNBLOCK_INPUT;
1125 /***********************************************************************
1126 Display Iterator
1127 ***********************************************************************/
1129 /* Function prototypes of this page. */
1131 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1132 struct glyph *,
1133 XChar2b *,
1134 int *));
1135 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1136 int, XChar2b *, int));
1137 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1138 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1139 static void x_append_glyph P_ ((struct it *));
1140 static void x_append_composite_glyph P_ ((struct it *));
1141 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1142 int, int, double));
1143 static void x_produce_glyphs P_ ((struct it *));
1144 static void x_produce_image_glyph P_ ((struct it *it));
1147 /* Return a pointer to per-char metric information in FONT of a
1148 character pointed by B which is a pointer to an XChar2b. */
1150 #define PER_CHAR_METRIC(font, b) \
1151 ((font)->per_char \
1152 ? ((font)->per_char + (b)->byte2 - (font)->min_char_or_byte2 \
1153 + (((font)->min_byte1 || (font)->max_byte1) \
1154 ? (((b)->byte1 - (font)->min_byte1) \
1155 * ((font)->max_char_or_byte2 - (font)->min_char_or_byte2 + 1)) \
1156 : 0)) \
1157 : &((font)->max_bounds))
1160 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1161 is not contained in the font. */
1163 static INLINE XCharStruct *
1164 x_per_char_metric (font, char2b)
1165 XFontStruct *font;
1166 XChar2b *char2b;
1168 /* The result metric information. */
1169 XCharStruct *pcm = NULL;
1171 xassert (font && char2b);
1173 if (font->per_char != NULL)
1175 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1177 /* min_char_or_byte2 specifies the linear character index
1178 corresponding to the first element of the per_char array,
1179 max_char_or_byte2 is the index of the last character. A
1180 character with non-zero CHAR2B->byte1 is not in the font.
1181 A character with byte2 less than min_char_or_byte2 or
1182 greater max_char_or_byte2 is not in the font. */
1183 if (char2b->byte1 == 0
1184 && char2b->byte2 >= font->min_char_or_byte2
1185 && char2b->byte2 <= font->max_char_or_byte2)
1186 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1188 else
1190 /* If either min_byte1 or max_byte1 are nonzero, both
1191 min_char_or_byte2 and max_char_or_byte2 are less than
1192 256, and the 2-byte character index values corresponding
1193 to the per_char array element N (counting from 0) are:
1195 byte1 = N/D + min_byte1
1196 byte2 = N\D + min_char_or_byte2
1198 where:
1200 D = max_char_or_byte2 - min_char_or_byte2 + 1
1201 / = integer division
1202 \ = integer modulus */
1203 if (char2b->byte1 >= font->min_byte1
1204 && char2b->byte1 <= font->max_byte1
1205 && char2b->byte2 >= font->min_char_or_byte2
1206 && char2b->byte2 <= font->max_char_or_byte2)
1208 pcm = (font->per_char
1209 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1210 * (char2b->byte1 - font->min_byte1))
1211 + (char2b->byte2 - font->min_char_or_byte2));
1215 else
1217 /* If the per_char pointer is null, all glyphs between the first
1218 and last character indexes inclusive have the same
1219 information, as given by both min_bounds and max_bounds. */
1220 if (char2b->byte2 >= font->min_char_or_byte2
1221 && char2b->byte2 <= font->max_char_or_byte2)
1222 pcm = &font->max_bounds;
1225 return ((pcm == NULL
1226 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1227 ? NULL : pcm);
1231 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1232 the two-byte form of C. Encoding is returned in *CHAR2B. */
1234 static INLINE void
1235 x_encode_char (c, char2b, font_info)
1236 int c;
1237 XChar2b *char2b;
1238 struct font_info *font_info;
1240 int charset = CHAR_CHARSET (c);
1241 XFontStruct *font = font_info->font;
1243 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1244 This may be either a program in a special encoder language or a
1245 fixed encoding. */
1246 if (font_info->font_encoder)
1248 /* It's a program. */
1249 struct ccl_program *ccl = font_info->font_encoder;
1251 if (CHARSET_DIMENSION (charset) == 1)
1253 ccl->reg[0] = charset;
1254 ccl->reg[1] = char2b->byte2;
1256 else
1258 ccl->reg[0] = charset;
1259 ccl->reg[1] = char2b->byte1;
1260 ccl->reg[2] = char2b->byte2;
1263 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1265 /* We assume that MSBs are appropriately set/reset by CCL
1266 program. */
1267 if (font->max_byte1 == 0) /* 1-byte font */
1268 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1269 else
1270 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1272 else if (font_info->encoding[charset])
1274 /* Fixed encoding scheme. See fontset.h for the meaning of the
1275 encoding numbers. */
1276 int enc = font_info->encoding[charset];
1278 if ((enc == 1 || enc == 2)
1279 && CHARSET_DIMENSION (charset) == 2)
1280 char2b->byte1 |= 0x80;
1282 if (enc == 1 || enc == 3)
1283 char2b->byte2 |= 0x80;
1288 /* Get face and two-byte form of character C in face FACE_ID on frame
1289 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1290 means we want to display multibyte text. Value is a pointer to a
1291 realized face that is ready for display. */
1293 static INLINE struct face *
1294 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1295 struct frame *f;
1296 int c, face_id;
1297 XChar2b *char2b;
1298 int multibyte_p;
1300 struct face *face = FACE_FROM_ID (f, face_id);
1302 if (!multibyte_p)
1304 /* Unibyte case. We don't have to encode, but we have to make
1305 sure to use a face suitable for unibyte. */
1306 char2b->byte1 = 0;
1307 char2b->byte2 = c;
1308 face_id = FACE_FOR_CHAR (f, face, c);
1309 face = FACE_FROM_ID (f, face_id);
1311 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1313 /* Case of ASCII in a face known to fit ASCII. */
1314 char2b->byte1 = 0;
1315 char2b->byte2 = c;
1317 else
1319 int c1, c2, charset;
1321 /* Split characters into bytes. If c2 is -1 afterwards, C is
1322 really a one-byte character so that byte1 is zero. */
1323 SPLIT_CHAR (c, charset, c1, c2);
1324 if (c2 > 0)
1325 char2b->byte1 = c1, char2b->byte2 = c2;
1326 else
1327 char2b->byte1 = 0, char2b->byte2 = c1;
1329 /* Maybe encode the character in *CHAR2B. */
1330 if (face->font != NULL)
1332 struct font_info *font_info
1333 = FONT_INFO_FROM_ID (f, face->font_info_id);
1334 if (font_info)
1335 x_encode_char (c, char2b, font_info);
1339 /* Make sure X resources of the face are allocated. */
1340 xassert (face != NULL);
1341 PREPARE_FACE_FOR_DISPLAY (f, face);
1343 return face;
1347 /* Get face and two-byte form of character glyph GLYPH on frame F.
1348 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1349 a pointer to a realized face that is ready for display. */
1351 static INLINE struct face *
1352 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1353 struct frame *f;
1354 struct glyph *glyph;
1355 XChar2b *char2b;
1356 int *two_byte_p;
1358 struct face *face;
1360 xassert (glyph->type == CHAR_GLYPH);
1361 face = FACE_FROM_ID (f, glyph->face_id);
1363 if (two_byte_p)
1364 *two_byte_p = 0;
1366 if (!glyph->multibyte_p)
1368 /* Unibyte case. We don't have to encode, but we have to make
1369 sure to use a face suitable for unibyte. */
1370 char2b->byte1 = 0;
1371 char2b->byte2 = glyph->u.ch;
1373 else if (glyph->u.ch < 128
1374 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1376 /* Case of ASCII in a face known to fit ASCII. */
1377 char2b->byte1 = 0;
1378 char2b->byte2 = glyph->u.ch;
1380 else
1382 int c1, c2, charset;
1384 /* Split characters into bytes. If c2 is -1 afterwards, C is
1385 really a one-byte character so that byte1 is zero. */
1386 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1387 if (c2 > 0)
1388 char2b->byte1 = c1, char2b->byte2 = c2;
1389 else
1390 char2b->byte1 = 0, char2b->byte2 = c1;
1392 /* Maybe encode the character in *CHAR2B. */
1393 if (charset != CHARSET_ASCII)
1395 struct font_info *font_info
1396 = FONT_INFO_FROM_ID (f, face->font_info_id);
1397 if (font_info)
1399 x_encode_char (glyph->u.ch, char2b, font_info);
1400 if (two_byte_p)
1401 *two_byte_p
1402 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1407 /* Make sure X resources of the face are allocated. */
1408 xassert (face != NULL);
1409 PREPARE_FACE_FOR_DISPLAY (f, face);
1410 return face;
1414 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1415 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1417 static INLINE void
1418 x_append_glyph (it)
1419 struct it *it;
1421 struct glyph *glyph;
1422 enum glyph_row_area area = it->area;
1424 xassert (it->glyph_row);
1425 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1427 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1428 if (glyph < it->glyph_row->glyphs[area + 1])
1430 glyph->charpos = CHARPOS (it->position);
1431 glyph->object = it->object;
1432 glyph->pixel_width = it->pixel_width;
1433 glyph->voffset = it->voffset;
1434 glyph->type = CHAR_GLYPH;
1435 glyph->multibyte_p = it->multibyte_p;
1436 glyph->left_box_line_p = it->start_of_box_run_p;
1437 glyph->right_box_line_p = it->end_of_box_run_p;
1438 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1439 || it->phys_descent > it->descent);
1440 glyph->padding_p = 0;
1441 glyph->glyph_not_available_p = it->glyph_not_available_p;
1442 glyph->face_id = it->face_id;
1443 glyph->u.ch = it->char_to_display;
1444 ++it->glyph_row->used[area];
1448 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1449 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1451 static INLINE void
1452 x_append_composite_glyph (it)
1453 struct it *it;
1455 struct glyph *glyph;
1456 enum glyph_row_area area = it->area;
1458 xassert (it->glyph_row);
1460 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1461 if (glyph < it->glyph_row->glyphs[area + 1])
1463 glyph->charpos = CHARPOS (it->position);
1464 glyph->object = it->object;
1465 glyph->pixel_width = it->pixel_width;
1466 glyph->voffset = it->voffset;
1467 glyph->type = COMPOSITE_GLYPH;
1468 glyph->multibyte_p = it->multibyte_p;
1469 glyph->left_box_line_p = it->start_of_box_run_p;
1470 glyph->right_box_line_p = it->end_of_box_run_p;
1471 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1472 || it->phys_descent > it->descent);
1473 glyph->padding_p = 0;
1474 glyph->glyph_not_available_p = 0;
1475 glyph->face_id = it->face_id;
1476 glyph->u.cmp_id = it->cmp_id;
1477 ++it->glyph_row->used[area];
1482 /* Change IT->ascent and IT->height according to the setting of
1483 IT->voffset. */
1485 static INLINE void
1486 take_vertical_position_into_account (it)
1487 struct it *it;
1489 if (it->voffset)
1491 if (it->voffset < 0)
1492 /* Increase the ascent so that we can display the text higher
1493 in the line. */
1494 it->ascent += abs (it->voffset);
1495 else
1496 /* Increase the descent so that we can display the text lower
1497 in the line. */
1498 it->descent += it->voffset;
1503 /* Produce glyphs/get display metrics for the image IT is loaded with.
1504 See the description of struct display_iterator in dispextern.h for
1505 an overview of struct display_iterator. */
1507 static void
1508 x_produce_image_glyph (it)
1509 struct it *it;
1511 struct image *img;
1512 struct face *face;
1514 xassert (it->what == IT_IMAGE);
1516 face = FACE_FROM_ID (it->f, it->face_id);
1517 img = IMAGE_FROM_ID (it->f, it->image_id);
1518 xassert (img);
1520 /* Make sure X resources of the face and image are loaded. */
1521 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1522 prepare_image_for_display (it->f, img);
1524 it->ascent = it->phys_ascent = image_ascent (img, face);
1525 it->descent = it->phys_descent = img->height + 2 * img->margin - it->ascent;
1526 it->pixel_width = img->width + 2 * img->margin;
1528 it->nglyphs = 1;
1530 if (face->box != FACE_NO_BOX)
1532 it->ascent += face->box_line_width;
1533 it->descent += face->box_line_width;
1535 if (it->start_of_box_run_p)
1536 it->pixel_width += face->box_line_width;
1537 if (it->end_of_box_run_p)
1538 it->pixel_width += face->box_line_width;
1541 take_vertical_position_into_account (it);
1543 if (it->glyph_row)
1545 struct glyph *glyph;
1546 enum glyph_row_area area = it->area;
1548 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1549 if (glyph < it->glyph_row->glyphs[area + 1])
1551 glyph->charpos = CHARPOS (it->position);
1552 glyph->object = it->object;
1553 glyph->pixel_width = it->pixel_width;
1554 glyph->voffset = it->voffset;
1555 glyph->type = IMAGE_GLYPH;
1556 glyph->multibyte_p = it->multibyte_p;
1557 glyph->left_box_line_p = it->start_of_box_run_p;
1558 glyph->right_box_line_p = it->end_of_box_run_p;
1559 glyph->overlaps_vertically_p = 0;
1560 glyph->padding_p = 0;
1561 glyph->glyph_not_available_p = 0;
1562 glyph->face_id = it->face_id;
1563 glyph->u.img_id = img->id;
1564 ++it->glyph_row->used[area];
1570 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1571 of the glyph, WIDTH and HEIGHT are the width and height of the
1572 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1573 ascent of the glyph (0 <= ASCENT <= 1). */
1575 static void
1576 x_append_stretch_glyph (it, object, width, height, ascent)
1577 struct it *it;
1578 Lisp_Object object;
1579 int width, height;
1580 double ascent;
1582 struct glyph *glyph;
1583 enum glyph_row_area area = it->area;
1585 xassert (ascent >= 0 && ascent <= 1);
1587 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1588 if (glyph < it->glyph_row->glyphs[area + 1])
1590 glyph->charpos = CHARPOS (it->position);
1591 glyph->object = object;
1592 glyph->pixel_width = width;
1593 glyph->voffset = it->voffset;
1594 glyph->type = STRETCH_GLYPH;
1595 glyph->multibyte_p = it->multibyte_p;
1596 glyph->left_box_line_p = it->start_of_box_run_p;
1597 glyph->right_box_line_p = it->end_of_box_run_p;
1598 glyph->overlaps_vertically_p = 0;
1599 glyph->padding_p = 0;
1600 glyph->glyph_not_available_p = 0;
1601 glyph->face_id = it->face_id;
1602 glyph->u.stretch.ascent = height * ascent;
1603 glyph->u.stretch.height = height;
1604 ++it->glyph_row->used[area];
1609 /* Produce a stretch glyph for iterator IT. IT->object is the value
1610 of the glyph property displayed. The value must be a list
1611 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1612 being recognized:
1614 1. `:width WIDTH' specifies that the space should be WIDTH *
1615 canonical char width wide. WIDTH may be an integer or floating
1616 point number.
1618 2. `:relative-width FACTOR' specifies that the width of the stretch
1619 should be computed from the width of the first character having the
1620 `glyph' property, and should be FACTOR times that width.
1622 3. `:align-to HPOS' specifies that the space should be wide enough
1623 to reach HPOS, a value in canonical character units.
1625 Exactly one of the above pairs must be present.
1627 4. `:height HEIGHT' specifies that the height of the stretch produced
1628 should be HEIGHT, measured in canonical character units.
1630 5. `:relative-height FACTOR' specifies that the height of the the
1631 stretch should be FACTOR times the height of the characters having
1632 the glyph property.
1634 Either none or exactly one of 4 or 5 must be present.
1636 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1637 of the stretch should be used for the ascent of the stretch.
1638 ASCENT must be in the range 0 <= ASCENT <= 100. */
1640 #define NUMVAL(X) \
1641 ((INTEGERP (X) || FLOATP (X)) \
1642 ? XFLOATINT (X) \
1643 : - 1)
1646 static void
1647 x_produce_stretch_glyph (it)
1648 struct it *it;
1650 /* (space :width WIDTH :height HEIGHT. */
1651 #if GLYPH_DEBUG
1652 extern Lisp_Object Qspace;
1653 #endif
1654 extern Lisp_Object QCwidth, QCheight, QCascent;
1655 extern Lisp_Object QCrelative_width, QCrelative_height;
1656 extern Lisp_Object QCalign_to;
1657 Lisp_Object prop, plist;
1658 double width = 0, height = 0, ascent = 0;
1659 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1660 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1662 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1664 /* List should start with `space'. */
1665 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1666 plist = XCDR (it->object);
1668 /* Compute the width of the stretch. */
1669 if (prop = Fplist_get (plist, QCwidth),
1670 NUMVAL (prop) > 0)
1671 /* Absolute width `:width WIDTH' specified and valid. */
1672 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1673 else if (prop = Fplist_get (plist, QCrelative_width),
1674 NUMVAL (prop) > 0)
1676 /* Relative width `:relative-width FACTOR' specified and valid.
1677 Compute the width of the characters having the `glyph'
1678 property. */
1679 struct it it2;
1680 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1682 it2 = *it;
1683 if (it->multibyte_p)
1685 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1686 - IT_BYTEPOS (*it));
1687 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1689 else
1690 it2.c = *p, it2.len = 1;
1692 it2.glyph_row = NULL;
1693 it2.what = IT_CHARACTER;
1694 x_produce_glyphs (&it2);
1695 width = NUMVAL (prop) * it2.pixel_width;
1697 else if (prop = Fplist_get (plist, QCalign_to),
1698 NUMVAL (prop) > 0)
1699 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1700 else
1701 /* Nothing specified -> width defaults to canonical char width. */
1702 width = CANON_X_UNIT (it->f);
1704 /* Compute height. */
1705 if (prop = Fplist_get (plist, QCheight),
1706 NUMVAL (prop) > 0)
1707 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1708 else if (prop = Fplist_get (plist, QCrelative_height),
1709 NUMVAL (prop) > 0)
1710 height = FONT_HEIGHT (font) * NUMVAL (prop);
1711 else
1712 height = FONT_HEIGHT (font);
1714 /* Compute percentage of height used for ascent. If
1715 `:ascent ASCENT' is present and valid, use that. Otherwise,
1716 derive the ascent from the font in use. */
1717 if (prop = Fplist_get (plist, QCascent),
1718 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1719 ascent = NUMVAL (prop) / 100.0;
1720 else
1721 ascent = (double) font->ascent / FONT_HEIGHT (font);
1723 if (width <= 0)
1724 width = 1;
1725 if (height <= 0)
1726 height = 1;
1728 if (it->glyph_row)
1730 Lisp_Object object = it->stack[it->sp - 1].string;
1731 if (!STRINGP (object))
1732 object = it->w->buffer;
1733 x_append_stretch_glyph (it, object, width, height, ascent);
1736 it->pixel_width = width;
1737 it->ascent = it->phys_ascent = height * ascent;
1738 it->descent = it->phys_descent = height - it->ascent;
1739 it->nglyphs = 1;
1741 if (face->box != FACE_NO_BOX)
1743 it->ascent += face->box_line_width;
1744 it->descent += face->box_line_width;
1746 if (it->start_of_box_run_p)
1747 it->pixel_width += face->box_line_width;
1748 if (it->end_of_box_run_p)
1749 it->pixel_width += face->box_line_width;
1752 take_vertical_position_into_account (it);
1755 /* Return proper value to be used as baseline offset of font that has
1756 ASCENT and DESCENT to draw characters by the font at the vertical
1757 center of the line of frame F.
1759 Here, out task is to find the value of BOFF in the following figure;
1761 -------------------------+-----------+-
1762 -+-+---------+-+ | |
1763 | | | | | |
1764 | | | | F_ASCENT F_HEIGHT
1765 | | | ASCENT | |
1766 HEIGHT | | | | |
1767 | | |-|-+------+-----------|------- baseline
1768 | | | | BOFF | |
1769 | |---------|-+-+ | |
1770 | | | DESCENT | |
1771 -+-+---------+-+ F_DESCENT |
1772 -------------------------+-----------+-
1774 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1775 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1776 DESCENT = FONT->descent
1777 HEIGHT = FONT_HEIGHT (FONT)
1778 F_DESCENT = (F->output_data.x->font->descent
1779 - F->output_data.x->baseline_offset)
1780 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1783 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1784 ((FONT)->descent \
1785 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT))) / 2 \
1786 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1788 /* Produce glyphs/get display metrics for the display element IT is
1789 loaded with. See the description of struct display_iterator in
1790 dispextern.h for an overview of struct display_iterator. */
1792 static void
1793 x_produce_glyphs (it)
1794 struct it *it;
1796 it->glyph_not_available_p = 0;
1798 if (it->what == IT_CHARACTER)
1800 XChar2b char2b;
1801 XFontStruct *font;
1802 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1803 XCharStruct *pcm;
1804 int font_not_found_p;
1805 struct font_info *font_info;
1806 int boff; /* baseline offset */
1808 /* Maybe translate single-byte characters to multibyte, or the
1809 other way. */
1810 it->char_to_display = it->c;
1811 if (!ASCII_BYTE_P (it->c))
1813 if (unibyte_display_via_language_environment
1814 && SINGLE_BYTE_CHAR_P (it->c)
1815 && (it->c >= 0240
1816 || !NILP (Vnonascii_translation_table)))
1818 it->char_to_display = unibyte_char_to_multibyte (it->c);
1819 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1820 face = FACE_FROM_ID (it->f, it->face_id);
1822 else if (!SINGLE_BYTE_CHAR_P (it->c)
1823 && !it->multibyte_p)
1825 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1826 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1827 face = FACE_FROM_ID (it->f, it->face_id);
1831 /* Get font to use. Encode IT->char_to_display. */
1832 x_get_char_face_and_encoding (it->f, it->char_to_display,
1833 it->face_id, &char2b,
1834 it->multibyte_p);
1835 font = face->font;
1837 /* When no suitable font found, use the default font. */
1838 font_not_found_p = font == NULL;
1839 if (font_not_found_p)
1841 font = FRAME_FONT (it->f);
1842 boff = it->f->output_data.x->baseline_offset;
1843 font_info = NULL;
1845 else
1847 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1848 boff = font_info->baseline_offset;
1849 if (font_info->vertical_centering)
1850 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1853 if (it->char_to_display >= ' '
1854 && (!it->multibyte_p || it->char_to_display < 128))
1856 /* Either unibyte or ASCII. */
1857 int stretched_p;
1859 it->nglyphs = 1;
1861 pcm = x_per_char_metric (font, &char2b);
1862 it->ascent = font->ascent + boff;
1863 it->descent = font->descent - boff;
1865 if (pcm)
1867 it->phys_ascent = pcm->ascent + boff;
1868 it->phys_descent = pcm->descent - boff;
1869 it->pixel_width = pcm->width;
1871 else
1873 it->glyph_not_available_p = 1;
1874 it->phys_ascent = font->ascent + boff;
1875 it->phys_descent = font->descent - boff;
1876 it->pixel_width = FONT_WIDTH (font);
1879 /* If this is a space inside a region of text with
1880 `space-width' property, change its width. */
1881 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1882 if (stretched_p)
1883 it->pixel_width *= XFLOATINT (it->space_width);
1885 /* If face has a box, add the box thickness to the character
1886 height. If character has a box line to the left and/or
1887 right, add the box line width to the character's width. */
1888 if (face->box != FACE_NO_BOX)
1890 int thick = face->box_line_width;
1892 it->ascent += thick;
1893 it->descent += thick;
1895 if (it->start_of_box_run_p)
1896 it->pixel_width += thick;
1897 if (it->end_of_box_run_p)
1898 it->pixel_width += thick;
1901 /* If face has an overline, add the height of the overline
1902 (1 pixel) and a 1 pixel margin to the character height. */
1903 if (face->overline_p)
1904 it->ascent += 2;
1906 take_vertical_position_into_account (it);
1908 /* If we have to actually produce glyphs, do it. */
1909 if (it->glyph_row)
1911 if (stretched_p)
1913 /* Translate a space with a `space-width' property
1914 into a stretch glyph. */
1915 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1916 x_append_stretch_glyph (it, it->object, it->pixel_width,
1917 it->ascent + it->descent, ascent);
1919 else
1920 x_append_glyph (it);
1922 /* If characters with lbearing or rbearing are displayed
1923 in this line, record that fact in a flag of the
1924 glyph row. This is used to optimize X output code. */
1925 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1926 it->glyph_row->contains_overlapping_glyphs_p = 1;
1929 else if (it->char_to_display == '\n')
1931 /* A newline has no width but we need the height of the line. */
1932 it->pixel_width = 0;
1933 it->nglyphs = 0;
1934 it->ascent = it->phys_ascent = font->ascent + boff;
1935 it->descent = it->phys_descent = font->descent - boff;
1937 if (face->box != FACE_NO_BOX)
1939 int thick = face->box_line_width;
1940 it->ascent += thick;
1941 it->descent += thick;
1944 else if (it->char_to_display == '\t')
1946 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1947 int x = it->current_x + it->continuation_lines_width;
1948 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1950 it->pixel_width = next_tab_x - x;
1951 it->nglyphs = 1;
1952 it->ascent = it->phys_ascent = font->ascent + boff;
1953 it->descent = it->phys_descent = font->descent - boff;
1955 if (it->glyph_row)
1957 double ascent = (double) it->ascent / (it->ascent + it->descent);
1958 x_append_stretch_glyph (it, it->object, it->pixel_width,
1959 it->ascent + it->descent, ascent);
1962 else
1964 /* A multi-byte character. Assume that the display width of the
1965 character is the width of the character multiplied by the
1966 width of the font. */
1968 /* If we found a font, this font should give us the right
1969 metrics. If we didn't find a font, use the frame's
1970 default font and calculate the width of the character
1971 from the charset width; this is what old redisplay code
1972 did. */
1973 pcm = x_per_char_metric (font, &char2b);
1974 if (font_not_found_p || !pcm)
1976 int charset = CHAR_CHARSET (it->char_to_display);
1978 it->glyph_not_available_p = 1;
1979 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1980 * CHARSET_WIDTH (charset));
1981 it->phys_ascent = font->ascent + boff;
1982 it->phys_descent = font->descent - boff;
1984 else
1986 it->pixel_width = pcm->width;
1987 it->phys_ascent = pcm->ascent + boff;
1988 it->phys_descent = pcm->descent - boff;
1989 if (it->glyph_row
1990 && (pcm->lbearing < 0
1991 || pcm->rbearing > pcm->width))
1992 it->glyph_row->contains_overlapping_glyphs_p = 1;
1994 it->nglyphs = 1;
1995 it->ascent = font->ascent + boff;
1996 it->descent = font->descent - boff;
1997 if (face->box != FACE_NO_BOX)
1999 int thick = face->box_line_width;
2000 it->ascent += thick;
2001 it->descent += thick;
2003 if (it->start_of_box_run_p)
2004 it->pixel_width += thick;
2005 if (it->end_of_box_run_p)
2006 it->pixel_width += thick;
2009 /* If face has an overline, add the height of the overline
2010 (1 pixel) and a 1 pixel margin to the character height. */
2011 if (face->overline_p)
2012 it->ascent += 2;
2014 take_vertical_position_into_account (it);
2016 if (it->glyph_row)
2017 x_append_glyph (it);
2020 else if (it->what == IT_COMPOSITION)
2022 /* Note: A composition is represented as one glyph in the
2023 glyph matrix. There are no padding glyphs. */
2024 XChar2b char2b;
2025 XFontStruct *font;
2026 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2027 XCharStruct *pcm;
2028 int font_not_found_p;
2029 struct font_info *font_info;
2030 int boff; /* baseline offset */
2031 struct composition *cmp = composition_table[it->cmp_id];
2033 /* Maybe translate single-byte characters to multibyte. */
2034 it->char_to_display = it->c;
2035 if (unibyte_display_via_language_environment
2036 && SINGLE_BYTE_CHAR_P (it->c)
2037 && (it->c >= 0240
2038 || (it->c >= 0200
2039 && !NILP (Vnonascii_translation_table))))
2041 it->char_to_display = unibyte_char_to_multibyte (it->c);
2044 /* Get face and font to use. Encode IT->char_to_display. */
2045 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2046 face = FACE_FROM_ID (it->f, it->face_id);
2047 x_get_char_face_and_encoding (it->f, it->char_to_display,
2048 it->face_id, &char2b, it->multibyte_p);
2049 font = face->font;
2051 /* When no suitable font found, use the default font. */
2052 font_not_found_p = font == NULL;
2053 if (font_not_found_p)
2055 font = FRAME_FONT (it->f);
2056 boff = it->f->output_data.x->baseline_offset;
2057 font_info = NULL;
2059 else
2061 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2062 boff = font_info->baseline_offset;
2063 if (font_info->vertical_centering)
2064 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2067 /* There are no padding glyphs, so there is only one glyph to
2068 produce for the composition. Important is that pixel_width,
2069 ascent and descent are the values of what is drawn by
2070 draw_glyphs (i.e. the values of the overall glyphs composed). */
2071 it->nglyphs = 1;
2073 /* If we have not yet calculated pixel size data of glyphs of
2074 the composition for the current face font, calculate them
2075 now. Theoretically, we have to check all fonts for the
2076 glyphs, but that requires much time and memory space. So,
2077 here we check only the font of the first glyph. This leads
2078 to incorrect display very rarely, and C-l (recenter) can
2079 correct the display anyway. */
2080 if (cmp->font != (void *) font)
2082 /* Ascent and descent of the font of the first character of
2083 this composition (adjusted by baseline offset). Ascent
2084 and descent of overall glyphs should not be less than
2085 them respectively. */
2086 int font_ascent = font->ascent + boff;
2087 int font_descent = font->descent - boff;
2088 /* Bounding box of the overall glyphs. */
2089 int leftmost, rightmost, lowest, highest;
2090 int i, width, ascent, descent;
2092 cmp->font = (void *) font;
2094 /* Initialize the bounding box. */
2095 pcm = x_per_char_metric (font, &char2b);
2096 if (pcm)
2098 width = pcm->width;
2099 ascent = pcm->ascent;
2100 descent = pcm->descent;
2102 else
2104 width = FONT_WIDTH (font);
2105 ascent = font->ascent;
2106 descent = font->descent;
2109 rightmost = width;
2110 lowest = - descent + boff;
2111 highest = ascent + boff;
2112 leftmost = 0;
2114 if (font_info
2115 && font_info->default_ascent
2116 && CHAR_TABLE_P (Vuse_default_ascent)
2117 && !NILP (Faref (Vuse_default_ascent,
2118 make_number (it->char_to_display))))
2119 highest = font_info->default_ascent + boff;
2121 /* Draw the first glyph at the normal position. It may be
2122 shifted to right later if some other glyphs are drawn at
2123 the left. */
2124 cmp->offsets[0] = 0;
2125 cmp->offsets[1] = boff;
2127 /* Set cmp->offsets for the remaining glyphs. */
2128 for (i = 1; i < cmp->glyph_len; i++)
2130 int left, right, btm, top;
2131 int ch = COMPOSITION_GLYPH (cmp, i);
2132 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2134 face = FACE_FROM_ID (it->f, face_id);
2135 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2136 it->multibyte_p);
2137 font = face->font;
2138 if (font == NULL)
2140 font = FRAME_FONT (it->f);
2141 boff = it->f->output_data.x->baseline_offset;
2142 font_info = NULL;
2144 else
2146 font_info
2147 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2148 boff = font_info->baseline_offset;
2149 if (font_info->vertical_centering)
2150 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2153 pcm = x_per_char_metric (font, &char2b);
2154 if (pcm)
2156 width = pcm->width;
2157 ascent = pcm->ascent;
2158 descent = pcm->descent;
2160 else
2162 width = FONT_WIDTH (font);
2163 ascent = font->ascent;
2164 descent = font->descent;
2167 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2169 /* Relative composition with or without
2170 alternate chars. */
2171 left = (leftmost + rightmost - width) / 2;
2172 btm = - descent + boff;
2173 if (font_info && font_info->relative_compose
2174 && (! CHAR_TABLE_P (Vignore_relative_composition)
2175 || NILP (Faref (Vignore_relative_composition,
2176 make_number (ch)))))
2179 if (- descent >= font_info->relative_compose)
2180 /* One extra pixel between two glyphs. */
2181 btm = highest + 1;
2182 else if (ascent <= 0)
2183 /* One extra pixel between two glyphs. */
2184 btm = lowest - 1 - ascent - descent;
2187 else
2189 /* A composition rule is specified by an integer
2190 value that encodes global and new reference
2191 points (GREF and NREF). GREF and NREF are
2192 specified by numbers as below:
2194 0---1---2 -- ascent
2198 9--10--11 -- center
2200 ---3---4---5--- baseline
2202 6---7---8 -- descent
2204 int rule = COMPOSITION_RULE (cmp, i);
2205 int gref, nref, grefx, grefy, nrefx, nrefy;
2207 COMPOSITION_DECODE_RULE (rule, gref, nref);
2208 grefx = gref % 3, nrefx = nref % 3;
2209 grefy = gref / 3, nrefy = nref / 3;
2211 left = (leftmost
2212 + grefx * (rightmost - leftmost) / 2
2213 - nrefx * width / 2);
2214 btm = ((grefy == 0 ? highest
2215 : grefy == 1 ? 0
2216 : grefy == 2 ? lowest
2217 : (highest + lowest) / 2)
2218 - (nrefy == 0 ? ascent + descent
2219 : nrefy == 1 ? descent - boff
2220 : nrefy == 2 ? 0
2221 : (ascent + descent) / 2));
2224 cmp->offsets[i * 2] = left;
2225 cmp->offsets[i * 2 + 1] = btm + descent;
2227 /* Update the bounding box of the overall glyphs. */
2228 right = left + width;
2229 top = btm + descent + ascent;
2230 if (left < leftmost)
2231 leftmost = left;
2232 if (right > rightmost)
2233 rightmost = right;
2234 if (top > highest)
2235 highest = top;
2236 if (btm < lowest)
2237 lowest = btm;
2240 /* If there are glyphs whose x-offsets are negative,
2241 shift all glyphs to the right and make all x-offsets
2242 non-negative. */
2243 if (leftmost < 0)
2245 for (i = 0; i < cmp->glyph_len; i++)
2246 cmp->offsets[i * 2] -= leftmost;
2247 rightmost -= leftmost;
2250 cmp->pixel_width = rightmost;
2251 cmp->ascent = highest;
2252 cmp->descent = - lowest;
2253 if (cmp->ascent < font_ascent)
2254 cmp->ascent = font_ascent;
2255 if (cmp->descent < font_descent)
2256 cmp->descent = font_descent;
2259 it->pixel_width = cmp->pixel_width;
2260 it->ascent = it->phys_ascent = cmp->ascent;
2261 it->descent = it->phys_descent = cmp->descent;
2263 if (face->box != FACE_NO_BOX)
2265 int thick = face->box_line_width;
2266 it->ascent += thick;
2267 it->descent += thick;
2269 if (it->start_of_box_run_p)
2270 it->pixel_width += thick;
2271 if (it->end_of_box_run_p)
2272 it->pixel_width += thick;
2275 /* If face has an overline, add the height of the overline
2276 (1 pixel) and a 1 pixel margin to the character height. */
2277 if (face->overline_p)
2278 it->ascent += 2;
2280 take_vertical_position_into_account (it);
2282 if (it->glyph_row)
2283 x_append_composite_glyph (it);
2285 else if (it->what == IT_IMAGE)
2286 x_produce_image_glyph (it);
2287 else if (it->what == IT_STRETCH)
2288 x_produce_stretch_glyph (it);
2290 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2291 because this isn't true for images with `:ascent 100'. */
2292 xassert (it->ascent >= 0 && it->descent >= 0);
2293 if (it->area == TEXT_AREA)
2294 it->current_x += it->pixel_width;
2296 it->descent += it->extra_line_spacing;
2298 it->max_ascent = max (it->max_ascent, it->ascent);
2299 it->max_descent = max (it->max_descent, it->descent);
2300 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2301 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2305 /* Estimate the pixel height of the mode or top line on frame F.
2306 FACE_ID specifies what line's height to estimate. */
2309 x_estimate_mode_line_height (f, face_id)
2310 struct frame *f;
2311 enum face_id face_id;
2313 int height = 1;
2315 /* This function is called so early when Emacs starts that the face
2316 cache and mode line face are not yet initialized. */
2317 if (FRAME_FACE_CACHE (f))
2319 struct face *face = FACE_FROM_ID (f, face_id);
2320 if (face)
2321 height = FONT_HEIGHT (face->font) + 2 * face->box_line_width;
2324 return height;
2328 /***********************************************************************
2329 Glyph display
2330 ***********************************************************************/
2332 /* A sequence of glyphs to be drawn in the same face.
2334 This data structure is not really completely X specific, so it
2335 could possibly, at least partially, be useful for other systems. It
2336 is currently not part of the external redisplay interface because
2337 it's not clear what other systems will need. */
2339 struct glyph_string
2341 /* X-origin of the string. */
2342 int x;
2344 /* Y-origin and y-position of the base line of this string. */
2345 int y, ybase;
2347 /* The width of the string, not including a face extension. */
2348 int width;
2350 /* The width of the string, including a face extension. */
2351 int background_width;
2353 /* The height of this string. This is the height of the line this
2354 string is drawn in, and can be different from the height of the
2355 font the string is drawn in. */
2356 int height;
2358 /* Number of pixels this string overwrites in front of its x-origin.
2359 This number is zero if the string has an lbearing >= 0; it is
2360 -lbearing, if the string has an lbearing < 0. */
2361 int left_overhang;
2363 /* Number of pixels this string overwrites past its right-most
2364 nominal x-position, i.e. x + width. Zero if the string's
2365 rbearing is <= its nominal width, rbearing - width otherwise. */
2366 int right_overhang;
2368 /* The frame on which the glyph string is drawn. */
2369 struct frame *f;
2371 /* The window on which the glyph string is drawn. */
2372 struct window *w;
2374 /* X display and window for convenience. */
2375 Display *display;
2376 Window window;
2378 /* The glyph row for which this string was built. It determines the
2379 y-origin and height of the string. */
2380 struct glyph_row *row;
2382 /* The area within row. */
2383 enum glyph_row_area area;
2385 /* Characters to be drawn, and number of characters. */
2386 XChar2b *char2b;
2387 int nchars;
2389 /* A face-override for drawing cursors, mouse face and similar. */
2390 enum draw_glyphs_face hl;
2392 /* Face in which this string is to be drawn. */
2393 struct face *face;
2395 /* Font in which this string is to be drawn. */
2396 XFontStruct *font;
2398 /* Font info for this string. */
2399 struct font_info *font_info;
2401 /* Non-null means this string describes (part of) a composition.
2402 All characters from char2b are drawn composed. */
2403 struct composition *cmp;
2405 /* Index of this glyph string's first character in the glyph
2406 definition of CMP. If this is zero, this glyph string describes
2407 the first character of a composition. */
2408 int gidx;
2410 /* 1 means this glyph strings face has to be drawn to the right end
2411 of the window's drawing area. */
2412 unsigned extends_to_end_of_line_p : 1;
2414 /* 1 means the background of this string has been drawn. */
2415 unsigned background_filled_p : 1;
2417 /* 1 means glyph string must be drawn with 16-bit functions. */
2418 unsigned two_byte_p : 1;
2420 /* 1 means that the original font determined for drawing this glyph
2421 string could not be loaded. The member `font' has been set to
2422 the frame's default font in this case. */
2423 unsigned font_not_found_p : 1;
2425 /* 1 means that the face in which this glyph string is drawn has a
2426 stipple pattern. */
2427 unsigned stippled_p : 1;
2429 /* 1 means only the foreground of this glyph string must be drawn,
2430 and we should use the physical height of the line this glyph
2431 string appears in as clip rect. */
2432 unsigned for_overlaps_p : 1;
2434 /* The GC to use for drawing this glyph string. */
2435 GC gc;
2437 /* A pointer to the first glyph in the string. This glyph
2438 corresponds to char2b[0]. Needed to draw rectangles if
2439 font_not_found_p is 1. */
2440 struct glyph *first_glyph;
2442 /* Image, if any. */
2443 struct image *img;
2445 struct glyph_string *next, *prev;
2449 #if 0
2451 static void
2452 x_dump_glyph_string (s)
2453 struct glyph_string *s;
2455 fprintf (stderr, "glyph string\n");
2456 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2457 s->x, s->y, s->width, s->height);
2458 fprintf (stderr, " ybase = %d\n", s->ybase);
2459 fprintf (stderr, " hl = %d\n", s->hl);
2460 fprintf (stderr, " left overhang = %d, right = %d\n",
2461 s->left_overhang, s->right_overhang);
2462 fprintf (stderr, " nchars = %d\n", s->nchars);
2463 fprintf (stderr, " extends to end of line = %d\n",
2464 s->extends_to_end_of_line_p);
2465 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2466 fprintf (stderr, " bg width = %d\n", s->background_width);
2469 #endif /* GLYPH_DEBUG */
2473 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2474 struct glyph_string **,
2475 struct glyph_string *,
2476 struct glyph_string *));
2477 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2478 struct glyph_string **,
2479 struct glyph_string *,
2480 struct glyph_string *));
2481 static void x_append_glyph_string P_ ((struct glyph_string **,
2482 struct glyph_string **,
2483 struct glyph_string *));
2484 static int x_left_overwritten P_ ((struct glyph_string *));
2485 static int x_left_overwriting P_ ((struct glyph_string *));
2486 static int x_right_overwritten P_ ((struct glyph_string *));
2487 static int x_right_overwriting P_ ((struct glyph_string *));
2488 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2489 int));
2490 static void x_init_glyph_string P_ ((struct glyph_string *,
2491 XChar2b *, struct window *,
2492 struct glyph_row *,
2493 enum glyph_row_area, int,
2494 enum draw_glyphs_face));
2495 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2496 enum glyph_row_area, int, int,
2497 enum draw_glyphs_face, int *, int *, int));
2498 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2499 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2500 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2501 int));
2502 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2503 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2504 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2505 static void x_draw_glyph_string P_ ((struct glyph_string *));
2506 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2507 static void x_set_cursor_gc P_ ((struct glyph_string *));
2508 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2509 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2510 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2511 int *, int *));
2512 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2513 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2514 unsigned long *, double, int));
2515 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2516 double, int, unsigned long));
2517 static void x_setup_relief_colors P_ ((struct glyph_string *));
2518 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2519 static void x_draw_image_relief P_ ((struct glyph_string *));
2520 static void x_draw_image_foreground P_ ((struct glyph_string *));
2521 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2522 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2523 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2524 int, int, int));
2525 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2526 int, int, int, int, XRectangle *));
2527 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2528 int, int, int, XRectangle *));
2529 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2530 enum glyph_row_area));
2531 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2532 struct glyph_row *,
2533 enum glyph_row_area, int, int));
2535 #if GLYPH_DEBUG
2536 static void x_check_font P_ ((struct frame *, XFontStruct *));
2537 #endif
2540 /* Append the list of glyph strings with head H and tail T to the list
2541 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2543 static INLINE void
2544 x_append_glyph_string_lists (head, tail, h, t)
2545 struct glyph_string **head, **tail;
2546 struct glyph_string *h, *t;
2548 if (h)
2550 if (*head)
2551 (*tail)->next = h;
2552 else
2553 *head = h;
2554 h->prev = *tail;
2555 *tail = t;
2560 /* Prepend the list of glyph strings with head H and tail T to the
2561 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2562 result. */
2564 static INLINE void
2565 x_prepend_glyph_string_lists (head, tail, h, t)
2566 struct glyph_string **head, **tail;
2567 struct glyph_string *h, *t;
2569 if (h)
2571 if (*head)
2572 (*head)->prev = t;
2573 else
2574 *tail = t;
2575 t->next = *head;
2576 *head = h;
2581 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2582 Set *HEAD and *TAIL to the resulting list. */
2584 static INLINE void
2585 x_append_glyph_string (head, tail, s)
2586 struct glyph_string **head, **tail;
2587 struct glyph_string *s;
2589 s->next = s->prev = NULL;
2590 x_append_glyph_string_lists (head, tail, s, s);
2594 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2595 face. */
2597 static void
2598 x_set_cursor_gc (s)
2599 struct glyph_string *s;
2601 if (s->font == FRAME_FONT (s->f)
2602 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2603 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2604 && !s->cmp)
2605 s->gc = s->f->output_data.x->cursor_gc;
2606 else
2608 /* Cursor on non-default face: must merge. */
2609 XGCValues xgcv;
2610 unsigned long mask;
2612 xgcv.background = s->f->output_data.x->cursor_pixel;
2613 xgcv.foreground = s->face->background;
2615 /* If the glyph would be invisible, try a different foreground. */
2616 if (xgcv.foreground == xgcv.background)
2617 xgcv.foreground = s->face->foreground;
2618 if (xgcv.foreground == xgcv.background)
2619 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2620 if (xgcv.foreground == xgcv.background)
2621 xgcv.foreground = s->face->foreground;
2623 /* Make sure the cursor is distinct from text in this face. */
2624 if (xgcv.background == s->face->background
2625 && xgcv.foreground == s->face->foreground)
2627 xgcv.background = s->face->foreground;
2628 xgcv.foreground = s->face->background;
2631 IF_DEBUG (x_check_font (s->f, s->font));
2632 xgcv.font = s->font->fid;
2633 xgcv.graphics_exposures = False;
2634 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2636 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2637 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2638 mask, &xgcv);
2639 else
2640 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2641 = XCreateGC (s->display, s->window, mask, &xgcv);
2643 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2648 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2650 static void
2651 x_set_mouse_face_gc (s)
2652 struct glyph_string *s;
2654 int face_id;
2655 struct face *face;
2657 /* What face has to be used for the mouse face? */
2658 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2659 face = FACE_FROM_ID (s->f, face_id);
2660 if (s->first_glyph->type == CHAR_GLYPH)
2661 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2662 else
2663 face_id = FACE_FOR_CHAR (s->f, face, 0);
2664 s->face = FACE_FROM_ID (s->f, face_id);
2665 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2667 /* If font in this face is same as S->font, use it. */
2668 if (s->font == s->face->font)
2669 s->gc = s->face->gc;
2670 else
2672 /* Otherwise construct scratch_cursor_gc with values from FACE
2673 but font FONT. */
2674 XGCValues xgcv;
2675 unsigned long mask;
2677 xgcv.background = s->face->background;
2678 xgcv.foreground = s->face->foreground;
2679 IF_DEBUG (x_check_font (s->f, s->font));
2680 xgcv.font = s->font->fid;
2681 xgcv.graphics_exposures = False;
2682 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2684 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2685 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2686 mask, &xgcv);
2687 else
2688 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2689 = XCreateGC (s->display, s->window, mask, &xgcv);
2691 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2694 xassert (s->gc != 0);
2698 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2699 Faces to use in the mode line have already been computed when the
2700 matrix was built, so there isn't much to do, here. */
2702 static INLINE void
2703 x_set_mode_line_face_gc (s)
2704 struct glyph_string *s;
2706 s->gc = s->face->gc;
2710 /* Set S->gc of glyph string S for drawing that glyph string. Set
2711 S->stippled_p to a non-zero value if the face of S has a stipple
2712 pattern. */
2714 static INLINE void
2715 x_set_glyph_string_gc (s)
2716 struct glyph_string *s;
2718 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2720 if (s->hl == DRAW_NORMAL_TEXT)
2722 s->gc = s->face->gc;
2723 s->stippled_p = s->face->stipple != 0;
2725 else if (s->hl == DRAW_INVERSE_VIDEO)
2727 x_set_mode_line_face_gc (s);
2728 s->stippled_p = s->face->stipple != 0;
2730 else if (s->hl == DRAW_CURSOR)
2732 x_set_cursor_gc (s);
2733 s->stippled_p = 0;
2735 else if (s->hl == DRAW_MOUSE_FACE)
2737 x_set_mouse_face_gc (s);
2738 s->stippled_p = s->face->stipple != 0;
2740 else if (s->hl == DRAW_IMAGE_RAISED
2741 || s->hl == DRAW_IMAGE_SUNKEN)
2743 s->gc = s->face->gc;
2744 s->stippled_p = s->face->stipple != 0;
2746 else
2748 s->gc = s->face->gc;
2749 s->stippled_p = s->face->stipple != 0;
2752 /* GC must have been set. */
2753 xassert (s->gc != 0);
2757 /* Return in *R the clipping rectangle for glyph string S. */
2759 static void
2760 x_get_glyph_string_clip_rect (s, r)
2761 struct glyph_string *s;
2762 XRectangle *r;
2764 if (s->row->full_width_p)
2766 /* Draw full-width. X coordinates are relative to S->w->left. */
2767 int canon_x = CANON_X_UNIT (s->f);
2769 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2770 r->width = XFASTINT (s->w->width) * canon_x;
2772 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2774 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2775 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2776 r->x -= width;
2779 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2781 /* Unless displaying a mode or menu bar line, which are always
2782 fully visible, clip to the visible part of the row. */
2783 if (s->w->pseudo_window_p)
2784 r->height = s->row->visible_height;
2785 else
2786 r->height = s->height;
2788 else
2790 /* This is a text line that may be partially visible. */
2791 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2792 r->width = window_box_width (s->w, s->area);
2793 r->height = s->row->visible_height;
2796 /* Don't use S->y for clipping because it doesn't take partially
2797 visible lines into account. For example, it can be negative for
2798 partially visible lines at the top of a window. */
2799 if (!s->row->full_width_p
2800 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2801 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2802 else
2803 r->y = max (0, s->row->y);
2805 /* If drawing a tool-bar window, draw it over the internal border
2806 at the top of the window. */
2807 if (s->w == XWINDOW (s->f->tool_bar_window))
2808 r->y -= s->f->output_data.x->internal_border_width;
2810 /* If S draws overlapping rows, it's sufficient to use the top and
2811 bottom of the window for clipping because this glyph string
2812 intentionally draws over other lines. */
2813 if (s->for_overlaps_p)
2815 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2816 r->height = window_text_bottom_y (s->w) - r->y;
2819 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2823 /* Set clipping for output of glyph string S. S may be part of a mode
2824 line or menu if we don't have X toolkit support. */
2826 static INLINE void
2827 x_set_glyph_string_clipping (s)
2828 struct glyph_string *s;
2830 XRectangle r;
2831 x_get_glyph_string_clip_rect (s, &r);
2832 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2836 /* Compute left and right overhang of glyph string S. If S is a glyph
2837 string for a composition, assume overhangs don't exist. */
2839 static INLINE void
2840 x_compute_glyph_string_overhangs (s)
2841 struct glyph_string *s;
2843 if (s->cmp == NULL
2844 && s->first_glyph->type == CHAR_GLYPH)
2846 XCharStruct cs;
2847 int direction, font_ascent, font_descent;
2848 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2849 &font_ascent, &font_descent, &cs);
2850 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2851 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2856 /* Compute overhangs and x-positions for glyph string S and its
2857 predecessors, or successors. X is the starting x-position for S.
2858 BACKWARD_P non-zero means process predecessors. */
2860 static void
2861 x_compute_overhangs_and_x (s, x, backward_p)
2862 struct glyph_string *s;
2863 int x;
2864 int backward_p;
2866 if (backward_p)
2868 while (s)
2870 x_compute_glyph_string_overhangs (s);
2871 x -= s->width;
2872 s->x = x;
2873 s = s->prev;
2876 else
2878 while (s)
2880 x_compute_glyph_string_overhangs (s);
2881 s->x = x;
2882 x += s->width;
2883 s = s->next;
2889 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2890 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2891 assumed to be zero. */
2893 static void
2894 x_get_glyph_overhangs (glyph, f, left, right)
2895 struct glyph *glyph;
2896 struct frame *f;
2897 int *left, *right;
2899 *left = *right = 0;
2901 if (glyph->type == CHAR_GLYPH)
2903 XFontStruct *font;
2904 struct face *face;
2905 struct font_info *font_info;
2906 XChar2b char2b;
2907 XCharStruct *pcm;
2909 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2910 font = face->font;
2911 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2912 if (font
2913 && (pcm = x_per_char_metric (font, &char2b)))
2915 if (pcm->rbearing > pcm->width)
2916 *right = pcm->rbearing - pcm->width;
2917 if (pcm->lbearing < 0)
2918 *left = -pcm->lbearing;
2924 /* Return the index of the first glyph preceding glyph string S that
2925 is overwritten by S because of S's left overhang. Value is -1
2926 if no glyphs are overwritten. */
2928 static int
2929 x_left_overwritten (s)
2930 struct glyph_string *s;
2932 int k;
2934 if (s->left_overhang)
2936 int x = 0, i;
2937 struct glyph *glyphs = s->row->glyphs[s->area];
2938 int first = s->first_glyph - glyphs;
2940 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2941 x -= glyphs[i].pixel_width;
2943 k = i + 1;
2945 else
2946 k = -1;
2948 return k;
2952 /* Return the index of the first glyph preceding glyph string S that
2953 is overwriting S because of its right overhang. Value is -1 if no
2954 glyph in front of S overwrites S. */
2956 static int
2957 x_left_overwriting (s)
2958 struct glyph_string *s;
2960 int i, k, x;
2961 struct glyph *glyphs = s->row->glyphs[s->area];
2962 int first = s->first_glyph - glyphs;
2964 k = -1;
2965 x = 0;
2966 for (i = first - 1; i >= 0; --i)
2968 int left, right;
2969 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2970 if (x + right > 0)
2971 k = i;
2972 x -= glyphs[i].pixel_width;
2975 return k;
2979 /* Return the index of the last glyph following glyph string S that is
2980 not overwritten by S because of S's right overhang. Value is -1 if
2981 no such glyph is found. */
2983 static int
2984 x_right_overwritten (s)
2985 struct glyph_string *s;
2987 int k = -1;
2989 if (s->right_overhang)
2991 int x = 0, i;
2992 struct glyph *glyphs = s->row->glyphs[s->area];
2993 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
2994 int end = s->row->used[s->area];
2996 for (i = first; i < end && s->right_overhang > x; ++i)
2997 x += glyphs[i].pixel_width;
2999 k = i;
3002 return k;
3006 /* Return the index of the last glyph following glyph string S that
3007 overwrites S because of its left overhang. Value is negative
3008 if no such glyph is found. */
3010 static int
3011 x_right_overwriting (s)
3012 struct glyph_string *s;
3014 int i, k, x;
3015 int end = s->row->used[s->area];
3016 struct glyph *glyphs = s->row->glyphs[s->area];
3017 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3019 k = -1;
3020 x = 0;
3021 for (i = first; i < end; ++i)
3023 int left, right;
3024 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3025 if (x - left < 0)
3026 k = i;
3027 x += glyphs[i].pixel_width;
3030 return k;
3034 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3036 static INLINE void
3037 x_clear_glyph_string_rect (s, x, y, w, h)
3038 struct glyph_string *s;
3039 int x, y, w, h;
3041 XGCValues xgcv;
3042 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3043 XSetForeground (s->display, s->gc, xgcv.background);
3044 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3045 XSetForeground (s->display, s->gc, xgcv.foreground);
3049 /* Draw the background of glyph_string S. If S->background_filled_p
3050 is non-zero don't draw it. FORCE_P non-zero means draw the
3051 background even if it wouldn't be drawn normally. This is used
3052 when a string preceding S draws into the background of S, or S
3053 contains the first component of a composition. */
3055 static void
3056 x_draw_glyph_string_background (s, force_p)
3057 struct glyph_string *s;
3058 int force_p;
3060 /* Nothing to do if background has already been drawn or if it
3061 shouldn't be drawn in the first place. */
3062 if (!s->background_filled_p)
3064 if (s->stippled_p)
3066 /* Fill background with a stipple pattern. */
3067 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3068 XFillRectangle (s->display, s->window, s->gc, s->x,
3069 s->y + s->face->box_line_width,
3070 s->background_width,
3071 s->height - 2 * s->face->box_line_width);
3072 XSetFillStyle (s->display, s->gc, FillSolid);
3073 s->background_filled_p = 1;
3075 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3076 || s->font_not_found_p
3077 || s->extends_to_end_of_line_p
3078 || force_p)
3080 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3081 s->background_width,
3082 s->height - 2 * s->face->box_line_width);
3083 s->background_filled_p = 1;
3089 /* Draw the foreground of glyph string S. */
3091 static void
3092 x_draw_glyph_string_foreground (s)
3093 struct glyph_string *s;
3095 int i, x;
3097 /* If first glyph of S has a left box line, start drawing the text
3098 of S to the right of that box line. */
3099 if (s->face->box != FACE_NO_BOX
3100 && s->first_glyph->left_box_line_p)
3101 x = s->x + s->face->box_line_width;
3102 else
3103 x = s->x;
3105 /* Draw characters of S as rectangles if S's font could not be
3106 loaded. */
3107 if (s->font_not_found_p)
3109 for (i = 0; i < s->nchars; ++i)
3111 struct glyph *g = s->first_glyph + i;
3112 XDrawRectangle (s->display, s->window,
3113 s->gc, x, s->y, g->pixel_width - 1,
3114 s->height - 1);
3115 x += g->pixel_width;
3118 else
3120 char *char1b = (char *) s->char2b;
3121 int boff = s->font_info->baseline_offset;
3123 if (s->font_info->vertical_centering)
3124 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3126 /* If we can use 8-bit functions, condense S->char2b. */
3127 if (!s->two_byte_p)
3128 for (i = 0; i < s->nchars; ++i)
3129 char1b[i] = s->char2b[i].byte2;
3131 /* Draw text with XDrawString if background has already been
3132 filled. Otherwise, use XDrawImageString. (Note that
3133 XDrawImageString is usually faster than XDrawString.) Always
3134 use XDrawImageString when drawing the cursor so that there is
3135 no chance that characters under a box cursor are invisible. */
3136 if (s->for_overlaps_p
3137 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3139 /* Draw characters with 16-bit or 8-bit functions. */
3140 if (s->two_byte_p)
3141 XDrawString16 (s->display, s->window, s->gc, x,
3142 s->ybase - boff, s->char2b, s->nchars);
3143 else
3144 XDrawString (s->display, s->window, s->gc, x,
3145 s->ybase - boff, char1b, s->nchars);
3147 else
3149 if (s->two_byte_p)
3150 XDrawImageString16 (s->display, s->window, s->gc, x,
3151 s->ybase - boff, s->char2b, s->nchars);
3152 else
3153 XDrawImageString (s->display, s->window, s->gc, x,
3154 s->ybase - boff, char1b, s->nchars);
3159 /* Draw the foreground of composite glyph string S. */
3161 static void
3162 x_draw_composite_glyph_string_foreground (s)
3163 struct glyph_string *s;
3165 int i, x;
3167 /* If first glyph of S has a left box line, start drawing the text
3168 of S to the right of that box line. */
3169 if (s->face->box != FACE_NO_BOX
3170 && s->first_glyph->left_box_line_p)
3171 x = s->x + s->face->box_line_width;
3172 else
3173 x = s->x;
3175 /* S is a glyph string for a composition. S->gidx is the index of
3176 the first character drawn for glyphs of this composition.
3177 S->gidx == 0 means we are drawing the very first character of
3178 this composition. */
3180 /* Draw a rectangle for the composition if the font for the very
3181 first character of the composition could not be loaded. */
3182 if (s->font_not_found_p)
3184 if (s->gidx == 0)
3185 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3186 s->width - 1, s->height - 1);
3188 else
3190 for (i = 0; i < s->nchars; i++, ++s->gidx)
3191 XDrawString16 (s->display, s->window, s->gc,
3192 x + s->cmp->offsets[s->gidx * 2],
3193 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3194 s->char2b + i, 1);
3199 #ifdef USE_X_TOOLKIT
3201 static struct frame *x_frame_of_widget P_ ((Widget));
3204 /* Return the frame on which widget WIDGET is used.. Abort if frame
3205 cannot be determined. */
3207 static struct frame *
3208 x_frame_of_widget (widget)
3209 Widget widget;
3211 struct x_display_info *dpyinfo;
3212 Lisp_Object tail;
3213 struct frame *f;
3215 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3217 /* Find the top-level shell of the widget. Note that this function
3218 can be called when the widget is not yet realized, so XtWindow
3219 (widget) == 0. That's the reason we can't simply use
3220 x_any_window_to_frame. */
3221 while (!XtIsTopLevelShell (widget))
3222 widget = XtParent (widget);
3224 /* Look for a frame with that top-level widget. Allocate the color
3225 on that frame to get the right gamma correction value. */
3226 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3227 if (GC_FRAMEP (XCAR (tail))
3228 && (f = XFRAME (XCAR (tail)),
3229 (f->output_data.nothing != 1
3230 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3231 && f->output_data.x->widget == widget)
3232 return f;
3234 abort ();
3238 /* Allocate the color COLOR->pixel on the screen and display of
3239 widget WIDGET in colormap CMAP. If an exact match cannot be
3240 allocated, try the nearest color available. Value is non-zero
3241 if successful. This is called from lwlib. */
3244 x_alloc_nearest_color_for_widget (widget, cmap, color)
3245 Widget widget;
3246 Colormap cmap;
3247 XColor *color;
3249 struct frame *f = x_frame_of_widget (widget);
3250 return x_alloc_nearest_color (f, cmap, color);
3254 #endif /* USE_X_TOOLKIT */
3257 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3258 CMAP. If an exact match can't be allocated, try the nearest color
3259 available. Value is non-zero if successful. Set *COLOR to the
3260 color allocated. */
3263 x_alloc_nearest_color (f, cmap, color)
3264 struct frame *f;
3265 Colormap cmap;
3266 XColor *color;
3268 Display *display = FRAME_X_DISPLAY (f);
3269 Screen *screen = FRAME_X_SCREEN (f);
3270 int rc;
3272 gamma_correct (f, color);
3273 rc = XAllocColor (display, cmap, color);
3274 if (rc == 0)
3276 /* If we got to this point, the colormap is full, so we're going
3277 to try to get the next closest color. The algorithm used is
3278 a least-squares matching, which is what X uses for closest
3279 color matching with StaticColor visuals. */
3280 int nearest, i;
3281 unsigned long nearest_delta = ~0;
3282 int ncells = XDisplayCells (display, XScreenNumberOfScreen (screen));
3283 XColor *cells = (XColor *) alloca (ncells * sizeof *cells);
3285 for (i = 0; i < ncells; ++i)
3286 cells[i].pixel = i;
3287 XQueryColors (display, cmap, cells, ncells);
3289 for (nearest = i = 0; i < ncells; ++i)
3291 long dred = (color->red >> 8) - (cells[i].red >> 8);
3292 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3293 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3294 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3296 if (delta < nearest_delta)
3298 nearest = i;
3299 nearest_delta = delta;
3303 color->red = cells[nearest].red;
3304 color->green = cells[nearest].green;
3305 color->blue = cells[nearest].blue;
3306 rc = XAllocColor (display, cmap, color);
3309 #ifdef DEBUG_X_COLORS
3310 if (rc)
3311 register_color (color->pixel);
3312 #endif /* DEBUG_X_COLORS */
3314 return rc;
3318 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3319 It's necessary to do this instead of just using PIXEL directly to
3320 get color reference counts right. */
3322 unsigned long
3323 x_copy_color (f, pixel)
3324 struct frame *f;
3325 unsigned long pixel;
3327 XColor color;
3329 color.pixel = pixel;
3330 BLOCK_INPUT;
3331 XQueryColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3332 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3333 UNBLOCK_INPUT;
3334 #ifdef DEBUG_X_COLORS
3335 register_color (pixel);
3336 #endif
3337 return color.pixel;
3341 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3342 It's necessary to do this instead of just using PIXEL directly to
3343 get color reference counts right. */
3345 unsigned long
3346 x_copy_dpy_color (dpy, cmap, pixel)
3347 Display *dpy;
3348 Colormap cmap;
3349 unsigned long pixel;
3351 XColor color;
3353 color.pixel = pixel;
3354 BLOCK_INPUT;
3355 XQueryColor (dpy, cmap, &color);
3356 XAllocColor (dpy, cmap, &color);
3357 UNBLOCK_INPUT;
3358 #ifdef DEBUG_X_COLORS
3359 register_color (pixel);
3360 #endif
3361 return color.pixel;
3365 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3366 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3367 If this produces the same color as PIXEL, try a color where all RGB
3368 values have DELTA added. Return the allocated color in *PIXEL.
3369 DISPLAY is the X display, CMAP is the colormap to operate on.
3370 Value is non-zero if successful. */
3372 static int
3373 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3374 struct frame *f;
3375 Display *display;
3376 Colormap cmap;
3377 unsigned long *pixel;
3378 double factor;
3379 int delta;
3381 XColor color, new;
3382 int success_p;
3384 /* Get RGB color values. */
3385 color.pixel = *pixel;
3386 XQueryColor (display, cmap, &color);
3388 /* Change RGB values by specified FACTOR. Avoid overflow! */
3389 xassert (factor >= 0);
3390 new.red = min (0xffff, factor * color.red);
3391 new.green = min (0xffff, factor * color.green);
3392 new.blue = min (0xffff, factor * color.blue);
3394 /* Try to allocate the color. */
3395 success_p = x_alloc_nearest_color (f, cmap, &new);
3396 if (success_p)
3398 if (new.pixel == *pixel)
3400 /* If we end up with the same color as before, try adding
3401 delta to the RGB values. */
3402 x_free_colors (f, &new.pixel, 1);
3404 new.red = min (0xffff, delta + color.red);
3405 new.green = min (0xffff, delta + color.green);
3406 new.blue = min (0xffff, delta + color.blue);
3407 success_p = x_alloc_nearest_color (f, cmap, &new);
3409 else
3410 success_p = 1;
3411 *pixel = new.pixel;
3414 return success_p;
3418 /* Set up the foreground color for drawing relief lines of glyph
3419 string S. RELIEF is a pointer to a struct relief containing the GC
3420 with which lines will be drawn. Use a color that is FACTOR or
3421 DELTA lighter or darker than the relief's background which is found
3422 in S->f->output_data.x->relief_background. If such a color cannot
3423 be allocated, use DEFAULT_PIXEL, instead. */
3425 static void
3426 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3427 struct frame *f;
3428 struct relief *relief;
3429 double factor;
3430 int delta;
3431 unsigned long default_pixel;
3433 XGCValues xgcv;
3434 struct x_output *di = f->output_data.x;
3435 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3436 unsigned long pixel;
3437 unsigned long background = di->relief_background;
3438 Colormap cmap = FRAME_X_COLORMAP (f);
3439 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3440 Display *dpy = FRAME_X_DISPLAY (f);
3442 xgcv.graphics_exposures = False;
3443 xgcv.line_width = 1;
3445 /* Free previously allocated color. The color cell will be reused
3446 when it has been freed as many times as it was allocated, so this
3447 doesn't affect faces using the same colors. */
3448 if (relief->gc
3449 && relief->allocated_p)
3451 x_free_colors (f, &relief->pixel, 1);
3452 relief->allocated_p = 0;
3455 /* Allocate new color. */
3456 xgcv.foreground = default_pixel;
3457 pixel = background;
3458 if (dpyinfo->n_planes != 1
3459 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3461 relief->allocated_p = 1;
3462 xgcv.foreground = relief->pixel = pixel;
3465 if (relief->gc == 0)
3467 xgcv.stipple = dpyinfo->gray;
3468 mask |= GCStipple;
3469 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3471 else
3472 XChangeGC (dpy, relief->gc, mask, &xgcv);
3476 /* Set up colors for the relief lines around glyph string S. */
3478 static void
3479 x_setup_relief_colors (s)
3480 struct glyph_string *s;
3482 struct x_output *di = s->f->output_data.x;
3483 unsigned long color;
3485 if (s->face->use_box_color_for_shadows_p)
3486 color = s->face->box_color;
3487 else
3489 XGCValues xgcv;
3491 /* Get the background color of the face. */
3492 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3493 color = xgcv.background;
3496 if (di->white_relief.gc == 0
3497 || color != di->relief_background)
3499 di->relief_background = color;
3500 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3501 WHITE_PIX_DEFAULT (s->f));
3502 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3503 BLACK_PIX_DEFAULT (s->f));
3508 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3509 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3510 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3511 relief. LEFT_P non-zero means draw a relief on the left side of
3512 the rectangle. RIGHT_P non-zero means draw a relief on the right
3513 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3514 when drawing. */
3516 static void
3517 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3518 raised_p, left_p, right_p, clip_rect)
3519 struct frame *f;
3520 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3521 XRectangle *clip_rect;
3523 int i;
3524 GC gc;
3526 if (raised_p)
3527 gc = f->output_data.x->white_relief.gc;
3528 else
3529 gc = f->output_data.x->black_relief.gc;
3530 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3532 /* Top. */
3533 for (i = 0; i < width; ++i)
3534 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3535 left_x + i * left_p, top_y + i,
3536 right_x + 1 - i * right_p, top_y + i);
3538 /* Left. */
3539 if (left_p)
3540 for (i = 0; i < width; ++i)
3541 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3542 left_x + i, top_y + i, left_x + i, bottom_y - i);
3544 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3545 if (raised_p)
3546 gc = f->output_data.x->black_relief.gc;
3547 else
3548 gc = f->output_data.x->white_relief.gc;
3549 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3551 /* Bottom. */
3552 for (i = 0; i < width; ++i)
3553 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3554 left_x + i * left_p, bottom_y - i,
3555 right_x + 1 - i * right_p, bottom_y - i);
3557 /* Right. */
3558 if (right_p)
3559 for (i = 0; i < width; ++i)
3560 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3561 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3563 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3567 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3568 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3569 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3570 left side of the rectangle. RIGHT_P non-zero means draw a line
3571 on the right side of the rectangle. CLIP_RECT is the clipping
3572 rectangle to use when drawing. */
3574 static void
3575 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3576 left_p, right_p, clip_rect)
3577 struct glyph_string *s;
3578 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3579 XRectangle *clip_rect;
3581 XGCValues xgcv;
3583 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3584 XSetForeground (s->display, s->gc, s->face->box_color);
3585 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3587 /* Top. */
3588 XFillRectangle (s->display, s->window, s->gc,
3589 left_x, top_y, right_x - left_x, width);
3591 /* Left. */
3592 if (left_p)
3593 XFillRectangle (s->display, s->window, s->gc,
3594 left_x, top_y, width, bottom_y - top_y);
3596 /* Bottom. */
3597 XFillRectangle (s->display, s->window, s->gc,
3598 left_x, bottom_y - width, right_x - left_x, width);
3600 /* Right. */
3601 if (right_p)
3602 XFillRectangle (s->display, s->window, s->gc,
3603 right_x - width, top_y, width, bottom_y - top_y);
3605 XSetForeground (s->display, s->gc, xgcv.foreground);
3606 XSetClipMask (s->display, s->gc, None);
3610 /* Draw a box around glyph string S. */
3612 static void
3613 x_draw_glyph_string_box (s)
3614 struct glyph_string *s;
3616 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3617 int left_p, right_p;
3618 struct glyph *last_glyph;
3619 XRectangle clip_rect;
3621 last_x = window_box_right (s->w, s->area);
3622 if (s->row->full_width_p
3623 && !s->w->pseudo_window_p)
3625 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3626 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3627 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3630 /* The glyph that may have a right box line. */
3631 last_glyph = (s->cmp || s->img
3632 ? s->first_glyph
3633 : s->first_glyph + s->nchars - 1);
3635 width = s->face->box_line_width;
3636 raised_p = s->face->box == FACE_RAISED_BOX;
3637 left_x = s->x;
3638 right_x = ((s->row->full_width_p
3639 ? last_x - 1
3640 : min (last_x, s->x + s->background_width) - 1));
3641 top_y = s->y;
3642 bottom_y = top_y + s->height - 1;
3644 left_p = (s->first_glyph->left_box_line_p
3645 || (s->hl == DRAW_MOUSE_FACE
3646 && (s->prev == NULL
3647 || s->prev->hl != s->hl)));
3648 right_p = (last_glyph->right_box_line_p
3649 || (s->hl == DRAW_MOUSE_FACE
3650 && (s->next == NULL
3651 || s->next->hl != s->hl)));
3653 x_get_glyph_string_clip_rect (s, &clip_rect);
3655 if (s->face->box == FACE_SIMPLE_BOX)
3656 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3657 left_p, right_p, &clip_rect);
3658 else
3660 x_setup_relief_colors (s);
3661 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3662 width, raised_p, left_p, right_p, &clip_rect);
3667 /* Draw foreground of image glyph string S. */
3669 static void
3670 x_draw_image_foreground (s)
3671 struct glyph_string *s;
3673 int x;
3674 int y = s->ybase - image_ascent (s->img, s->face);
3676 /* If first glyph of S has a left box line, start drawing it to the
3677 right of that line. */
3678 if (s->face->box != FACE_NO_BOX
3679 && s->first_glyph->left_box_line_p)
3680 x = s->x + s->face->box_line_width;
3681 else
3682 x = s->x;
3684 /* If there is a margin around the image, adjust x- and y-position
3685 by that margin. */
3686 if (s->img->margin)
3688 x += s->img->margin;
3689 y += s->img->margin;
3692 if (s->img->pixmap)
3694 if (s->img->mask)
3696 /* We can't set both a clip mask and use XSetClipRectangles
3697 because the latter also sets a clip mask. We also can't
3698 trust on the shape extension to be available
3699 (XShapeCombineRegion). So, compute the rectangle to draw
3700 manually. */
3701 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3702 | GCFunction);
3703 XGCValues xgcv;
3704 XRectangle clip_rect, image_rect, r;
3706 xgcv.clip_mask = s->img->mask;
3707 xgcv.clip_x_origin = x;
3708 xgcv.clip_y_origin = y;
3709 xgcv.function = GXcopy;
3710 XChangeGC (s->display, s->gc, mask, &xgcv);
3712 x_get_glyph_string_clip_rect (s, &clip_rect);
3713 image_rect.x = x;
3714 image_rect.y = y;
3715 image_rect.width = s->img->width;
3716 image_rect.height = s->img->height;
3717 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3718 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3719 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3721 else
3723 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3724 XGCValues xgcv;
3725 XRectangle clip_rect, image_rect, r;
3727 x_get_glyph_string_clip_rect (s, &clip_rect);
3728 image_rect.x = x;
3729 image_rect.y = y;
3730 image_rect.width = s->img->width;
3731 image_rect.height = s->img->height;
3732 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3733 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3734 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3736 /* When the image has a mask, we can expect that at
3737 least part of a mouse highlight or a block cursor will
3738 be visible. If the image doesn't have a mask, make
3739 a block cursor visible by drawing a rectangle around
3740 the image. I believe it's looking better if we do
3741 nothing here for mouse-face. */
3742 if (s->hl == DRAW_CURSOR)
3743 XDrawRectangle (s->display, s->window, s->gc, x, y,
3744 s->img->width - 1, s->img->height - 1);
3747 else
3748 /* Draw a rectangle if image could not be loaded. */
3749 XDrawRectangle (s->display, s->window, s->gc, x, y,
3750 s->img->width - 1, s->img->height - 1);
3754 /* Draw a relief around the image glyph string S. */
3756 static void
3757 x_draw_image_relief (s)
3758 struct glyph_string *s;
3760 int x0, y0, x1, y1, thick, raised_p;
3761 XRectangle r;
3762 int x;
3763 int y = s->ybase - image_ascent (s->img, s->face);
3765 /* If first glyph of S has a left box line, start drawing it to the
3766 right of that line. */
3767 if (s->face->box != FACE_NO_BOX
3768 && s->first_glyph->left_box_line_p)
3769 x = s->x + s->face->box_line_width;
3770 else
3771 x = s->x;
3773 /* If there is a margin around the image, adjust x- and y-position
3774 by that margin. */
3775 if (s->img->margin)
3777 x += s->img->margin;
3778 y += s->img->margin;
3781 if (s->hl == DRAW_IMAGE_SUNKEN
3782 || s->hl == DRAW_IMAGE_RAISED)
3784 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3785 raised_p = s->hl == DRAW_IMAGE_RAISED;
3787 else
3789 thick = abs (s->img->relief);
3790 raised_p = s->img->relief > 0;
3793 x0 = x - thick;
3794 y0 = y - thick;
3795 x1 = x + s->img->width + thick - 1;
3796 y1 = y + s->img->height + thick - 1;
3798 x_setup_relief_colors (s);
3799 x_get_glyph_string_clip_rect (s, &r);
3800 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3804 /* Draw the foreground of image glyph string S to PIXMAP. */
3806 static void
3807 x_draw_image_foreground_1 (s, pixmap)
3808 struct glyph_string *s;
3809 Pixmap pixmap;
3811 int x;
3812 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3814 /* If first glyph of S has a left box line, start drawing it to the
3815 right of that line. */
3816 if (s->face->box != FACE_NO_BOX
3817 && s->first_glyph->left_box_line_p)
3818 x = s->face->box_line_width;
3819 else
3820 x = 0;
3822 /* If there is a margin around the image, adjust x- and y-position
3823 by that margin. */
3824 if (s->img->margin)
3826 x += s->img->margin;
3827 y += s->img->margin;
3830 if (s->img->pixmap)
3832 if (s->img->mask)
3834 /* We can't set both a clip mask and use XSetClipRectangles
3835 because the latter also sets a clip mask. We also can't
3836 trust on the shape extension to be available
3837 (XShapeCombineRegion). So, compute the rectangle to draw
3838 manually. */
3839 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3840 | GCFunction);
3841 XGCValues xgcv;
3843 xgcv.clip_mask = s->img->mask;
3844 xgcv.clip_x_origin = x;
3845 xgcv.clip_y_origin = y;
3846 xgcv.function = GXcopy;
3847 XChangeGC (s->display, s->gc, mask, &xgcv);
3849 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3850 0, 0, s->img->width, s->img->height, x, y);
3851 XSetClipMask (s->display, s->gc, None);
3853 else
3855 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
3856 0, 0, s->img->width, s->img->height, x, y);
3858 /* When the image has a mask, we can expect that at
3859 least part of a mouse highlight or a block cursor will
3860 be visible. If the image doesn't have a mask, make
3861 a block cursor visible by drawing a rectangle around
3862 the image. I believe it's looking better if we do
3863 nothing here for mouse-face. */
3864 if (s->hl == DRAW_CURSOR)
3865 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3866 s->img->width - 1, s->img->height - 1);
3869 else
3870 /* Draw a rectangle if image could not be loaded. */
3871 XDrawRectangle (s->display, pixmap, s->gc, x, y,
3872 s->img->width - 1, s->img->height - 1);
3876 /* Draw part of the background of glyph string S. X, Y, W, and H
3877 give the rectangle to draw. */
3879 static void
3880 x_draw_glyph_string_bg_rect (s, x, y, w, h)
3881 struct glyph_string *s;
3882 int x, y, w, h;
3884 if (s->stippled_p)
3886 /* Fill background with a stipple pattern. */
3887 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3888 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3889 XSetFillStyle (s->display, s->gc, FillSolid);
3891 else
3892 x_clear_glyph_string_rect (s, x, y, w, h);
3896 /* Draw image glyph string S.
3898 s->y
3899 s->x +-------------------------
3900 | s->face->box
3902 | +-------------------------
3903 | | s->img->margin
3905 | | +-------------------
3906 | | | the image
3910 static void
3911 x_draw_image_glyph_string (s)
3912 struct glyph_string *s;
3914 int x, y;
3915 int box_line_width = s->face->box_line_width;
3916 int margin = s->img->margin;
3917 int height;
3918 Pixmap pixmap = None;
3920 height = s->height - 2 * box_line_width;
3922 /* Fill background with face under the image. Do it only if row is
3923 taller than image or if image has a clip mask to reduce
3924 flickering. */
3925 s->stippled_p = s->face->stipple != 0;
3926 if (height > s->img->height
3927 || margin
3928 || s->img->mask
3929 || s->img->pixmap == 0
3930 || s->width != s->background_width)
3932 if (box_line_width && s->first_glyph->left_box_line_p)
3933 x = s->x + box_line_width;
3934 else
3935 x = s->x;
3937 y = s->y + box_line_width;
3939 if (s->img->mask)
3941 /* Create a pixmap as large as the glyph string Fill it with
3942 the background color. Copy the image to it, using its
3943 mask. Copy the temporary pixmap to the display. */
3944 Screen *screen = FRAME_X_SCREEN (s->f);
3945 int depth = DefaultDepthOfScreen (screen);
3947 /* Create a pixmap as large as the glyph string. */
3948 pixmap = XCreatePixmap (s->display, s->window,
3949 s->background_width,
3950 s->height, depth);
3952 /* Don't clip in the following because we're working on the
3953 pixmap. */
3954 XSetClipMask (s->display, s->gc, None);
3956 /* Fill the pixmap with the background color/stipple. */
3957 if (s->stippled_p)
3959 /* Fill background with a stipple pattern. */
3960 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3961 XFillRectangle (s->display, pixmap, s->gc,
3962 0, 0, s->background_width, s->height);
3963 XSetFillStyle (s->display, s->gc, FillSolid);
3965 else
3967 XGCValues xgcv;
3968 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
3969 &xgcv);
3970 XSetForeground (s->display, s->gc, xgcv.background);
3971 XFillRectangle (s->display, pixmap, s->gc,
3972 0, 0, s->background_width, s->height);
3973 XSetForeground (s->display, s->gc, xgcv.foreground);
3976 else
3977 /* Implementation idea: Is it possible to construct a mask?
3978 We could look at the color at the margins of the image, and
3979 say that this color is probably the background color of the
3980 image. */
3981 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
3983 s->background_filled_p = 1;
3986 /* Draw the foreground. */
3987 if (pixmap != None)
3989 x_draw_image_foreground_1 (s, pixmap);
3990 x_set_glyph_string_clipping (s);
3991 XCopyArea (s->display, pixmap, s->window, s->gc,
3992 0, 0, s->background_width, s->height, s->x, s->y);
3993 XFreePixmap (s->display, pixmap);
3995 else
3996 x_draw_image_foreground (s);
3998 /* If we must draw a relief around the image, do it. */
3999 if (s->img->relief
4000 || s->hl == DRAW_IMAGE_RAISED
4001 || s->hl == DRAW_IMAGE_SUNKEN)
4002 x_draw_image_relief (s);
4006 /* Draw stretch glyph string S. */
4008 static void
4009 x_draw_stretch_glyph_string (s)
4010 struct glyph_string *s;
4012 xassert (s->first_glyph->type == STRETCH_GLYPH);
4013 s->stippled_p = s->face->stipple != 0;
4015 if (s->hl == DRAW_CURSOR
4016 && !x_stretch_cursor_p)
4018 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4019 as wide as the stretch glyph. */
4020 int width = min (CANON_X_UNIT (s->f), s->background_width);
4022 /* Draw cursor. */
4023 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4025 /* Clear rest using the GC of the original non-cursor face. */
4026 if (width < s->background_width)
4028 GC gc = s->face->gc;
4029 int x = s->x + width, y = s->y;
4030 int w = s->background_width - width, h = s->height;
4031 XRectangle r;
4033 x_get_glyph_string_clip_rect (s, &r);
4034 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4036 if (s->face->stipple)
4038 /* Fill background with a stipple pattern. */
4039 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4040 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4041 XSetFillStyle (s->display, gc, FillSolid);
4043 else
4045 XGCValues xgcv;
4046 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4047 XSetForeground (s->display, gc, xgcv.background);
4048 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4049 XSetForeground (s->display, gc, xgcv.foreground);
4053 else
4054 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4055 s->height);
4057 s->background_filled_p = 1;
4061 /* Draw glyph string S. */
4063 static void
4064 x_draw_glyph_string (s)
4065 struct glyph_string *s;
4067 /* If S draws into the background of its successor, draw the
4068 background of the successor first so that S can draw into it.
4069 This makes S->next use XDrawString instead of XDrawImageString. */
4070 if (s->next && s->right_overhang && !s->for_overlaps_p)
4072 xassert (s->next->img == NULL);
4073 x_set_glyph_string_gc (s->next);
4074 x_set_glyph_string_clipping (s->next);
4075 x_draw_glyph_string_background (s->next, 1);
4078 /* Set up S->gc, set clipping and draw S. */
4079 x_set_glyph_string_gc (s);
4080 x_set_glyph_string_clipping (s);
4082 switch (s->first_glyph->type)
4084 case IMAGE_GLYPH:
4085 x_draw_image_glyph_string (s);
4086 break;
4088 case STRETCH_GLYPH:
4089 x_draw_stretch_glyph_string (s);
4090 break;
4092 case CHAR_GLYPH:
4093 if (s->for_overlaps_p)
4094 s->background_filled_p = 1;
4095 else
4096 x_draw_glyph_string_background (s, 0);
4097 x_draw_glyph_string_foreground (s);
4098 break;
4100 case COMPOSITE_GLYPH:
4101 if (s->for_overlaps_p || s->gidx > 0)
4102 s->background_filled_p = 1;
4103 else
4104 x_draw_glyph_string_background (s, 1);
4105 x_draw_composite_glyph_string_foreground (s);
4106 break;
4108 default:
4109 abort ();
4112 if (!s->for_overlaps_p)
4114 /* Draw underline. */
4115 if (s->face->underline_p)
4117 unsigned long dy, h;
4119 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4120 h = 1;
4121 if (!XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &dy))
4122 dy = s->height - h;
4124 if (s->face->underline_defaulted_p)
4125 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4126 s->width, h);
4127 else
4129 XGCValues xgcv;
4130 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4131 XSetForeground (s->display, s->gc, s->face->underline_color);
4132 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4133 s->width, h);
4134 XSetForeground (s->display, s->gc, xgcv.foreground);
4138 /* Draw overline. */
4139 if (s->face->overline_p)
4141 unsigned long dy = 0, h = 1;
4143 if (s->face->overline_color_defaulted_p)
4144 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4145 s->width, h);
4146 else
4148 XGCValues xgcv;
4149 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4150 XSetForeground (s->display, s->gc, s->face->overline_color);
4151 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4152 s->width, h);
4153 XSetForeground (s->display, s->gc, xgcv.foreground);
4157 /* Draw strike-through. */
4158 if (s->face->strike_through_p)
4160 unsigned long h = 1;
4161 unsigned long dy = (s->height - h) / 2;
4163 if (s->face->strike_through_color_defaulted_p)
4164 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4165 s->width, h);
4166 else
4168 XGCValues xgcv;
4169 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4170 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4171 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4172 s->width, h);
4173 XSetForeground (s->display, s->gc, xgcv.foreground);
4177 /* Draw relief. */
4178 if (s->face->box != FACE_NO_BOX)
4179 x_draw_glyph_string_box (s);
4182 /* Reset clipping. */
4183 XSetClipMask (s->display, s->gc, None);
4187 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4188 struct face **, int));
4191 /* Fill glyph string S with composition components specified by S->cmp.
4193 FACES is an array of faces for all components of this composition.
4194 S->gidx is the index of the first component for S.
4195 OVERLAPS_P non-zero means S should draw the foreground only, and
4196 use its physical height for clipping.
4198 Value is the index of a component not in S. */
4200 static int
4201 x_fill_composite_glyph_string (s, faces, overlaps_p)
4202 struct glyph_string *s;
4203 struct face **faces;
4204 int overlaps_p;
4206 int i;
4208 xassert (s);
4210 s->for_overlaps_p = overlaps_p;
4212 s->face = faces[s->gidx];
4213 s->font = s->face->font;
4214 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4216 /* For all glyphs of this composition, starting at the offset
4217 S->gidx, until we reach the end of the definition or encounter a
4218 glyph that requires the different face, add it to S. */
4219 ++s->nchars;
4220 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4221 ++s->nchars;
4223 /* All glyph strings for the same composition has the same width,
4224 i.e. the width set for the first component of the composition. */
4226 s->width = s->first_glyph->pixel_width;
4228 /* If the specified font could not be loaded, use the frame's
4229 default font, but record the fact that we couldn't load it in
4230 the glyph string so that we can draw rectangles for the
4231 characters of the glyph string. */
4232 if (s->font == NULL)
4234 s->font_not_found_p = 1;
4235 s->font = FRAME_FONT (s->f);
4238 /* Adjust base line for subscript/superscript text. */
4239 s->ybase += s->first_glyph->voffset;
4241 xassert (s->face && s->face->gc);
4243 /* This glyph string must always be drawn with 16-bit functions. */
4244 s->two_byte_p = 1;
4246 return s->gidx + s->nchars;
4250 /* Fill glyph string S from a sequence of character glyphs.
4252 FACE_ID is the face id of the string. START is the index of the
4253 first glyph to consider, END is the index of the last + 1.
4254 OVERLAPS_P non-zero means S should draw the foreground only, and
4255 use its physical height for clipping.
4257 Value is the index of the first glyph not in S. */
4259 static int
4260 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4261 struct glyph_string *s;
4262 int face_id;
4263 int start, end, overlaps_p;
4265 struct glyph *glyph, *last;
4266 int voffset;
4267 int glyph_not_available_p;
4269 xassert (s->f == XFRAME (s->w->frame));
4270 xassert (s->nchars == 0);
4271 xassert (start >= 0 && end > start);
4273 s->for_overlaps_p = overlaps_p,
4274 glyph = s->row->glyphs[s->area] + start;
4275 last = s->row->glyphs[s->area] + end;
4276 voffset = glyph->voffset;
4278 glyph_not_available_p = glyph->glyph_not_available_p;
4280 while (glyph < last
4281 && glyph->type == CHAR_GLYPH
4282 && glyph->voffset == voffset
4283 /* Same face id implies same font, nowadays. */
4284 && glyph->face_id == face_id
4285 && glyph->glyph_not_available_p == glyph_not_available_p)
4287 int two_byte_p;
4289 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4290 s->char2b + s->nchars,
4291 &two_byte_p);
4292 s->two_byte_p = two_byte_p;
4293 ++s->nchars;
4294 xassert (s->nchars <= end - start);
4295 s->width += glyph->pixel_width;
4296 ++glyph;
4299 s->font = s->face->font;
4300 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4302 /* If the specified font could not be loaded, use the frame's font,
4303 but record the fact that we couldn't load it in
4304 S->font_not_found_p so that we can draw rectangles for the
4305 characters of the glyph string. */
4306 if (s->font == NULL || glyph_not_available_p)
4308 s->font_not_found_p = 1;
4309 s->font = FRAME_FONT (s->f);
4312 /* Adjust base line for subscript/superscript text. */
4313 s->ybase += voffset;
4315 xassert (s->face && s->face->gc);
4316 return glyph - s->row->glyphs[s->area];
4320 /* Fill glyph string S from image glyph S->first_glyph. */
4322 static void
4323 x_fill_image_glyph_string (s)
4324 struct glyph_string *s;
4326 xassert (s->first_glyph->type == IMAGE_GLYPH);
4327 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4328 xassert (s->img);
4329 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4330 s->font = s->face->font;
4331 s->width = s->first_glyph->pixel_width;
4333 /* Adjust base line for subscript/superscript text. */
4334 s->ybase += s->first_glyph->voffset;
4338 /* Fill glyph string S from a sequence of stretch glyphs.
4340 ROW is the glyph row in which the glyphs are found, AREA is the
4341 area within the row. START is the index of the first glyph to
4342 consider, END is the index of the last + 1.
4344 Value is the index of the first glyph not in S. */
4346 static int
4347 x_fill_stretch_glyph_string (s, row, area, start, end)
4348 struct glyph_string *s;
4349 struct glyph_row *row;
4350 enum glyph_row_area area;
4351 int start, end;
4353 struct glyph *glyph, *last;
4354 int voffset, face_id;
4356 xassert (s->first_glyph->type == STRETCH_GLYPH);
4358 glyph = s->row->glyphs[s->area] + start;
4359 last = s->row->glyphs[s->area] + end;
4360 face_id = glyph->face_id;
4361 s->face = FACE_FROM_ID (s->f, face_id);
4362 s->font = s->face->font;
4363 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4364 s->width = glyph->pixel_width;
4365 voffset = glyph->voffset;
4367 for (++glyph;
4368 (glyph < last
4369 && glyph->type == STRETCH_GLYPH
4370 && glyph->voffset == voffset
4371 && glyph->face_id == face_id);
4372 ++glyph)
4373 s->width += glyph->pixel_width;
4375 /* Adjust base line for subscript/superscript text. */
4376 s->ybase += voffset;
4378 xassert (s->face && s->face->gc);
4379 return glyph - s->row->glyphs[s->area];
4383 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4384 of XChar2b structures for S; it can't be allocated in
4385 x_init_glyph_string because it must be allocated via `alloca'. W
4386 is the window on which S is drawn. ROW and AREA are the glyph row
4387 and area within the row from which S is constructed. START is the
4388 index of the first glyph structure covered by S. HL is a
4389 face-override for drawing S. */
4391 static void
4392 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4393 struct glyph_string *s;
4394 XChar2b *char2b;
4395 struct window *w;
4396 struct glyph_row *row;
4397 enum glyph_row_area area;
4398 int start;
4399 enum draw_glyphs_face hl;
4401 bzero (s, sizeof *s);
4402 s->w = w;
4403 s->f = XFRAME (w->frame);
4404 s->display = FRAME_X_DISPLAY (s->f);
4405 s->window = FRAME_X_WINDOW (s->f);
4406 s->char2b = char2b;
4407 s->hl = hl;
4408 s->row = row;
4409 s->area = area;
4410 s->first_glyph = row->glyphs[area] + start;
4411 s->height = row->height;
4412 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4414 /* Display the internal border below the tool-bar window. */
4415 if (s->w == XWINDOW (s->f->tool_bar_window))
4416 s->y -= s->f->output_data.x->internal_border_width;
4418 s->ybase = s->y + row->ascent;
4422 /* Set background width of glyph string S. START is the index of the
4423 first glyph following S. LAST_X is the right-most x-position + 1
4424 in the drawing area. */
4426 static INLINE void
4427 x_set_glyph_string_background_width (s, start, last_x)
4428 struct glyph_string *s;
4429 int start;
4430 int last_x;
4432 /* If the face of this glyph string has to be drawn to the end of
4433 the drawing area, set S->extends_to_end_of_line_p. */
4434 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4436 if (start == s->row->used[s->area]
4437 && s->hl == DRAW_NORMAL_TEXT
4438 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4439 || s->face->background != default_face->background
4440 || s->face->stipple != default_face->stipple))
4441 s->extends_to_end_of_line_p = 1;
4443 /* If S extends its face to the end of the line, set its
4444 background_width to the distance to the right edge of the drawing
4445 area. */
4446 if (s->extends_to_end_of_line_p)
4447 s->background_width = last_x - s->x + 1;
4448 else
4449 s->background_width = s->width;
4453 /* Add a glyph string for a stretch glyph to the list of strings
4454 between HEAD and TAIL. START is the index of the stretch glyph in
4455 row area AREA of glyph row ROW. END is the index of the last glyph
4456 in that glyph row area. X is the current output position assigned
4457 to the new glyph string constructed. HL overrides that face of the
4458 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4459 is the right-most x-position of the drawing area. */
4461 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4462 and below -- keep them on one line. */
4463 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4464 do \
4466 s = (struct glyph_string *) alloca (sizeof *s); \
4467 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4468 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4469 x_append_glyph_string (&HEAD, &TAIL, s); \
4470 s->x = (X); \
4472 while (0)
4475 /* Add a glyph string for an image glyph to the list of strings
4476 between HEAD and TAIL. START is the index of the image glyph in
4477 row area AREA of glyph row ROW. END is the index of the last glyph
4478 in that glyph row area. X is the current output position assigned
4479 to the new glyph string constructed. HL overrides that face of the
4480 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4481 is the right-most x-position of the drawing area. */
4483 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4484 do \
4486 s = (struct glyph_string *) alloca (sizeof *s); \
4487 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4488 x_fill_image_glyph_string (s); \
4489 x_append_glyph_string (&HEAD, &TAIL, s); \
4490 ++START; \
4491 s->x = (X); \
4493 while (0)
4496 /* Add a glyph string for a sequence of character glyphs to the list
4497 of strings between HEAD and TAIL. START is the index of the first
4498 glyph in row area AREA of glyph row ROW that is part of the new
4499 glyph string. END is the index of the last glyph in that glyph row
4500 area. X is the current output position assigned to the new glyph
4501 string constructed. HL overrides that face of the glyph; e.g. it
4502 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4503 right-most x-position of the drawing area. */
4505 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4506 do \
4508 int c, face_id; \
4509 XChar2b *char2b; \
4511 c = (ROW)->glyphs[AREA][START].u.ch; \
4512 face_id = (ROW)->glyphs[AREA][START].face_id; \
4514 s = (struct glyph_string *) alloca (sizeof *s); \
4515 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4516 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4517 x_append_glyph_string (&HEAD, &TAIL, s); \
4518 s->x = (X); \
4519 START = x_fill_glyph_string (s, face_id, START, END, \
4520 OVERLAPS_P); \
4522 while (0)
4525 /* Add a glyph string for a composite sequence to the list of strings
4526 between HEAD and TAIL. START is the index of the first glyph in
4527 row area AREA of glyph row ROW that is part of the new glyph
4528 string. END is the index of the last glyph in that glyph row area.
4529 X is the current output position assigned to the new glyph string
4530 constructed. HL overrides that face of the glyph; e.g. it is
4531 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4532 x-position of the drawing area. */
4534 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4535 do { \
4536 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4537 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4538 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4539 struct composition *cmp = composition_table[cmp_id]; \
4540 int glyph_len = cmp->glyph_len; \
4541 XChar2b *char2b; \
4542 struct face **faces; \
4543 struct glyph_string *first_s = NULL; \
4544 int n; \
4546 base_face = base_face->ascii_face; \
4547 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4548 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4549 /* At first, fill in `char2b' and `faces'. */ \
4550 for (n = 0; n < glyph_len; n++) \
4552 int c = COMPOSITION_GLYPH (cmp, n); \
4553 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4554 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4555 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4556 this_face_id, char2b + n, 1); \
4559 /* Make glyph_strings for each glyph sequence that is drawable by \
4560 the same face, and append them to HEAD/TAIL. */ \
4561 for (n = 0; n < cmp->glyph_len;) \
4563 s = (struct glyph_string *) alloca (sizeof *s); \
4564 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4565 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4566 s->cmp = cmp; \
4567 s->gidx = n; \
4568 s->x = (X); \
4570 if (n == 0) \
4571 first_s = s; \
4573 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4576 ++START; \
4577 s = first_s; \
4578 } while (0)
4581 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4582 of AREA of glyph row ROW on window W between indices START and END.
4583 HL overrides the face for drawing glyph strings, e.g. it is
4584 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4585 x-positions of the drawing area.
4587 This is an ugly monster macro construct because we must use alloca
4588 to allocate glyph strings (because x_draw_glyphs can be called
4589 asynchronously). */
4591 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4592 do \
4594 HEAD = TAIL = NULL; \
4595 while (START < END) \
4597 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4598 switch (first_glyph->type) \
4600 case CHAR_GLYPH: \
4601 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4602 TAIL, HL, X, LAST_X, \
4603 OVERLAPS_P); \
4604 break; \
4606 case COMPOSITE_GLYPH: \
4607 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4608 HEAD, TAIL, HL, X, LAST_X,\
4609 OVERLAPS_P); \
4610 break; \
4612 case STRETCH_GLYPH: \
4613 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4614 HEAD, TAIL, HL, X, LAST_X); \
4615 break; \
4617 case IMAGE_GLYPH: \
4618 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4619 TAIL, HL, X, LAST_X); \
4620 break; \
4622 default: \
4623 abort (); \
4626 x_set_glyph_string_background_width (s, START, LAST_X); \
4627 (X) += s->width; \
4630 while (0)
4633 /* Draw glyphs between START and END in AREA of ROW on window W,
4634 starting at x-position X. X is relative to AREA in W. HL is a
4635 face-override with the following meaning:
4637 DRAW_NORMAL_TEXT draw normally
4638 DRAW_CURSOR draw in cursor face
4639 DRAW_MOUSE_FACE draw in mouse face.
4640 DRAW_INVERSE_VIDEO draw in mode line face
4641 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4642 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4644 If REAL_START is non-null, return in *REAL_START the real starting
4645 position for display. This can be different from START in case
4646 overlapping glyphs must be displayed. If REAL_END is non-null,
4647 return in *REAL_END the real end position for display. This can be
4648 different from END in case overlapping glyphs must be displayed.
4650 If OVERLAPS_P is non-zero, draw only the foreground of characters
4651 and clip to the physical height of ROW.
4653 Value is the x-position reached, relative to AREA of W. */
4655 static int
4656 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4657 overlaps_p)
4658 struct window *w;
4659 int x;
4660 struct glyph_row *row;
4661 enum glyph_row_area area;
4662 int start, end;
4663 enum draw_glyphs_face hl;
4664 int *real_start, *real_end;
4665 int overlaps_p;
4667 struct glyph_string *head, *tail;
4668 struct glyph_string *s;
4669 int last_x, area_width;
4670 int x_reached;
4671 int i, j;
4673 /* Let's rather be paranoid than getting a SEGV. */
4674 start = max (0, start);
4675 end = min (end, row->used[area]);
4676 if (real_start)
4677 *real_start = start;
4678 if (real_end)
4679 *real_end = end;
4681 /* Translate X to frame coordinates. Set last_x to the right
4682 end of the drawing area. */
4683 if (row->full_width_p)
4685 /* X is relative to the left edge of W, without scroll bars
4686 or flag areas. */
4687 struct frame *f = XFRAME (w->frame);
4688 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4689 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4691 x += window_left_x;
4692 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4693 last_x = window_left_x + area_width;
4695 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4697 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4698 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4699 last_x += width;
4700 else
4701 x -= width;
4704 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4705 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4707 else
4709 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4710 area_width = window_box_width (w, area);
4711 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4714 /* Build a doubly-linked list of glyph_string structures between
4715 head and tail from what we have to draw. Note that the macro
4716 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4717 the reason we use a separate variable `i'. */
4718 i = start;
4719 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4720 overlaps_p);
4721 if (tail)
4722 x_reached = tail->x + tail->background_width;
4723 else
4724 x_reached = x;
4726 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4727 the row, redraw some glyphs in front or following the glyph
4728 strings built above. */
4729 if (!overlaps_p && row->contains_overlapping_glyphs_p)
4731 int dummy_x = 0;
4732 struct glyph_string *h, *t;
4734 /* Compute overhangs for all glyph strings. */
4735 for (s = head; s; s = s->next)
4736 x_compute_glyph_string_overhangs (s);
4738 /* Prepend glyph strings for glyphs in front of the first glyph
4739 string that are overwritten because of the first glyph
4740 string's left overhang. The background of all strings
4741 prepended must be drawn because the first glyph string
4742 draws over it. */
4743 i = x_left_overwritten (head);
4744 if (i >= 0)
4746 j = i;
4747 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4748 DRAW_NORMAL_TEXT, dummy_x, last_x,
4749 overlaps_p);
4750 start = i;
4751 if (real_start)
4752 *real_start = start;
4753 x_compute_overhangs_and_x (t, head->x, 1);
4754 x_prepend_glyph_string_lists (&head, &tail, h, t);
4757 /* Prepend glyph strings for glyphs in front of the first glyph
4758 string that overwrite that glyph string because of their
4759 right overhang. For these strings, only the foreground must
4760 be drawn, because it draws over the glyph string at `head'.
4761 The background must not be drawn because this would overwrite
4762 right overhangs of preceding glyphs for which no glyph
4763 strings exist. */
4764 i = x_left_overwriting (head);
4765 if (i >= 0)
4767 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4768 DRAW_NORMAL_TEXT, dummy_x, last_x,
4769 overlaps_p);
4770 for (s = h; s; s = s->next)
4771 s->background_filled_p = 1;
4772 if (real_start)
4773 *real_start = i;
4774 x_compute_overhangs_and_x (t, head->x, 1);
4775 x_prepend_glyph_string_lists (&head, &tail, h, t);
4778 /* Append glyphs strings for glyphs following the last glyph
4779 string tail that are overwritten by tail. The background of
4780 these strings has to be drawn because tail's foreground draws
4781 over it. */
4782 i = x_right_overwritten (tail);
4783 if (i >= 0)
4785 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4786 DRAW_NORMAL_TEXT, x, last_x,
4787 overlaps_p);
4788 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4789 x_append_glyph_string_lists (&head, &tail, h, t);
4790 if (real_end)
4791 *real_end = i;
4794 /* Append glyph strings for glyphs following the last glyph
4795 string tail that overwrite tail. The foreground of such
4796 glyphs has to be drawn because it writes into the background
4797 of tail. The background must not be drawn because it could
4798 paint over the foreground of following glyphs. */
4799 i = x_right_overwriting (tail);
4800 if (i >= 0)
4802 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4803 DRAW_NORMAL_TEXT, x, last_x,
4804 overlaps_p);
4805 for (s = h; s; s = s->next)
4806 s->background_filled_p = 1;
4807 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4808 x_append_glyph_string_lists (&head, &tail, h, t);
4809 if (real_end)
4810 *real_end = i;
4814 /* Draw all strings. */
4815 for (s = head; s; s = s->next)
4816 x_draw_glyph_string (s);
4818 /* Value is the x-position up to which drawn, relative to AREA of W.
4819 This doesn't include parts drawn because of overhangs. */
4820 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4821 if (!row->full_width_p)
4823 if (area > LEFT_MARGIN_AREA)
4824 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4825 if (area > TEXT_AREA)
4826 x_reached -= window_box_width (w, TEXT_AREA);
4828 return x_reached;
4832 /* Fix the display of area AREA of overlapping row ROW in window W. */
4834 static void
4835 x_fix_overlapping_area (w, row, area)
4836 struct window *w;
4837 struct glyph_row *row;
4838 enum glyph_row_area area;
4840 int i, x;
4842 BLOCK_INPUT;
4844 if (area == LEFT_MARGIN_AREA)
4845 x = 0;
4846 else if (area == TEXT_AREA)
4847 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
4848 else
4849 x = (window_box_width (w, LEFT_MARGIN_AREA)
4850 + window_box_width (w, TEXT_AREA));
4852 for (i = 0; i < row->used[area];)
4854 if (row->glyphs[area][i].overlaps_vertically_p)
4856 int start = i, start_x = x;
4860 x += row->glyphs[area][i].pixel_width;
4861 ++i;
4863 while (i < row->used[area]
4864 && row->glyphs[area][i].overlaps_vertically_p);
4866 x_draw_glyphs (w, start_x, row, area, start, i,
4867 (row->inverse_p
4868 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4869 NULL, NULL, 1);
4871 else
4873 x += row->glyphs[area][i].pixel_width;
4874 ++i;
4878 UNBLOCK_INPUT;
4882 /* Output LEN glyphs starting at START at the nominal cursor position.
4883 Advance the nominal cursor over the text. The global variable
4884 updated_window contains the window being updated, updated_row is
4885 the glyph row being updated, and updated_area is the area of that
4886 row being updated. */
4888 static void
4889 x_write_glyphs (start, len)
4890 struct glyph *start;
4891 int len;
4893 int x, hpos, real_start, real_end;
4895 xassert (updated_window && updated_row);
4896 BLOCK_INPUT;
4898 /* Write glyphs. */
4900 hpos = start - updated_row->glyphs[updated_area];
4901 x = x_draw_glyphs (updated_window, output_cursor.x,
4902 updated_row, updated_area,
4903 hpos, hpos + len,
4904 (updated_row->inverse_p
4905 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
4906 &real_start, &real_end, 0);
4908 /* If we drew over the cursor, note that it is not visible any more. */
4909 note_overwritten_text_cursor (updated_window, real_start,
4910 real_end - real_start);
4912 UNBLOCK_INPUT;
4914 /* Advance the output cursor. */
4915 output_cursor.hpos += len;
4916 output_cursor.x = x;
4920 /* Insert LEN glyphs from START at the nominal cursor position. */
4922 static void
4923 x_insert_glyphs (start, len)
4924 struct glyph *start;
4925 register int len;
4927 struct frame *f;
4928 struct window *w;
4929 int line_height, shift_by_width, shifted_region_width;
4930 struct glyph_row *row;
4931 struct glyph *glyph;
4932 int frame_x, frame_y, hpos, real_start, real_end;
4934 xassert (updated_window && updated_row);
4935 BLOCK_INPUT;
4936 w = updated_window;
4937 f = XFRAME (WINDOW_FRAME (w));
4939 /* Get the height of the line we are in. */
4940 row = updated_row;
4941 line_height = row->height;
4943 /* Get the width of the glyphs to insert. */
4944 shift_by_width = 0;
4945 for (glyph = start; glyph < start + len; ++glyph)
4946 shift_by_width += glyph->pixel_width;
4948 /* Get the width of the region to shift right. */
4949 shifted_region_width = (window_box_width (w, updated_area)
4950 - output_cursor.x
4951 - shift_by_width);
4953 /* Shift right. */
4954 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
4955 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
4956 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
4957 f->output_data.x->normal_gc,
4958 frame_x, frame_y,
4959 shifted_region_width, line_height,
4960 frame_x + shift_by_width, frame_y);
4962 /* Write the glyphs. */
4963 hpos = start - row->glyphs[updated_area];
4964 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
4965 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
4966 note_overwritten_text_cursor (w, real_start, real_end - real_start);
4968 /* Advance the output cursor. */
4969 output_cursor.hpos += len;
4970 output_cursor.x += shift_by_width;
4971 UNBLOCK_INPUT;
4975 /* Delete N glyphs at the nominal cursor position. Not implemented
4976 for X frames. */
4978 static void
4979 x_delete_glyphs (n)
4980 register int n;
4982 abort ();
4986 /* Erase the current text line from the nominal cursor position
4987 (inclusive) to pixel column TO_X (exclusive). The idea is that
4988 everything from TO_X onward is already erased.
4990 TO_X is a pixel position relative to updated_area of
4991 updated_window. TO_X == -1 means clear to the end of this area. */
4993 static void
4994 x_clear_end_of_line (to_x)
4995 int to_x;
4997 struct frame *f;
4998 struct window *w = updated_window;
4999 int max_x, min_y, max_y;
5000 int from_x, from_y, to_y;
5002 xassert (updated_window && updated_row);
5003 f = XFRAME (w->frame);
5005 if (updated_row->full_width_p)
5007 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5008 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5009 && !w->pseudo_window_p)
5010 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5012 else
5013 max_x = window_box_width (w, updated_area);
5014 max_y = window_text_bottom_y (w);
5016 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5017 of window. For TO_X > 0, truncate to end of drawing area. */
5018 if (to_x == 0)
5019 return;
5020 else if (to_x < 0)
5021 to_x = max_x;
5022 else
5023 to_x = min (to_x, max_x);
5025 to_y = min (max_y, output_cursor.y + updated_row->height);
5027 /* Notice if the cursor will be cleared by this operation. */
5028 if (!updated_row->full_width_p)
5029 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5031 from_x = output_cursor.x;
5033 /* Translate to frame coordinates. */
5034 if (updated_row->full_width_p)
5036 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5037 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5039 else
5041 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5042 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5045 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5046 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5047 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5049 /* Prevent inadvertently clearing to end of the X window. */
5050 if (to_x > from_x && to_y > from_y)
5052 BLOCK_INPUT;
5053 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5054 from_x, from_y, to_x - from_x, to_y - from_y,
5055 False);
5056 UNBLOCK_INPUT;
5061 /* Clear entire frame. If updating_frame is non-null, clear that
5062 frame. Otherwise clear the selected frame. */
5064 static void
5065 x_clear_frame ()
5067 struct frame *f;
5069 if (updating_frame)
5070 f = updating_frame;
5071 else
5072 f = SELECTED_FRAME ();
5074 /* Clearing the frame will erase any cursor, so mark them all as no
5075 longer visible. */
5076 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5077 output_cursor.hpos = output_cursor.vpos = 0;
5078 output_cursor.x = -1;
5080 /* We don't set the output cursor here because there will always
5081 follow an explicit cursor_to. */
5082 BLOCK_INPUT;
5083 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5085 /* We have to clear the scroll bars, too. If we have changed
5086 colors or something like that, then they should be notified. */
5087 x_scroll_bar_clear (f);
5089 XFlush (FRAME_X_DISPLAY (f));
5090 UNBLOCK_INPUT;
5095 /* Invert the middle quarter of the frame for .15 sec. */
5097 /* We use the select system call to do the waiting, so we have to make
5098 sure it's available. If it isn't, we just won't do visual bells. */
5100 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5103 /* Subtract the `struct timeval' values X and Y, storing the result in
5104 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5106 static int
5107 timeval_subtract (result, x, y)
5108 struct timeval *result, x, y;
5110 /* Perform the carry for the later subtraction by updating y. This
5111 is safer because on some systems the tv_sec member is unsigned. */
5112 if (x.tv_usec < y.tv_usec)
5114 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5115 y.tv_usec -= 1000000 * nsec;
5116 y.tv_sec += nsec;
5119 if (x.tv_usec - y.tv_usec > 1000000)
5121 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5122 y.tv_usec += 1000000 * nsec;
5123 y.tv_sec -= nsec;
5126 /* Compute the time remaining to wait. tv_usec is certainly
5127 positive. */
5128 result->tv_sec = x.tv_sec - y.tv_sec;
5129 result->tv_usec = x.tv_usec - y.tv_usec;
5131 /* Return indication of whether the result should be considered
5132 negative. */
5133 return x.tv_sec < y.tv_sec;
5136 void
5137 XTflash (f)
5138 struct frame *f;
5140 BLOCK_INPUT;
5143 GC gc;
5145 /* Create a GC that will use the GXxor function to flip foreground
5146 pixels into background pixels. */
5148 XGCValues values;
5150 values.function = GXxor;
5151 values.foreground = (f->output_data.x->foreground_pixel
5152 ^ f->output_data.x->background_pixel);
5154 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5155 GCFunction | GCForeground, &values);
5159 /* Get the height not including a menu bar widget. */
5160 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5161 /* Height of each line to flash. */
5162 int flash_height = FRAME_LINE_HEIGHT (f);
5163 /* These will be the left and right margins of the rectangles. */
5164 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5165 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5167 int width;
5169 /* Don't flash the area between a scroll bar and the frame
5170 edge it is next to. */
5171 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5173 case vertical_scroll_bar_left:
5174 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5175 break;
5177 case vertical_scroll_bar_right:
5178 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5179 break;
5181 default:
5182 break;
5185 width = flash_right - flash_left;
5187 /* If window is tall, flash top and bottom line. */
5188 if (height > 3 * FRAME_LINE_HEIGHT (f))
5190 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5191 flash_left,
5192 (FRAME_INTERNAL_BORDER_WIDTH (f)
5193 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5194 width, flash_height);
5195 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5196 flash_left,
5197 (height - flash_height
5198 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5199 width, flash_height);
5201 else
5202 /* If it is short, flash it all. */
5203 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5204 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5205 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5207 x_flush (f);
5210 struct timeval wakeup;
5212 EMACS_GET_TIME (wakeup);
5214 /* Compute time to wait until, propagating carry from usecs. */
5215 wakeup.tv_usec += 150000;
5216 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5217 wakeup.tv_usec %= 1000000;
5219 /* Keep waiting until past the time wakeup. */
5220 while (1)
5222 struct timeval timeout;
5224 EMACS_GET_TIME (timeout);
5226 /* In effect, timeout = wakeup - timeout.
5227 Break if result would be negative. */
5228 if (timeval_subtract (&timeout, wakeup, timeout))
5229 break;
5231 /* Try to wait that long--but we might wake up sooner. */
5232 select (0, NULL, NULL, NULL, &timeout);
5236 /* If window is tall, flash top and bottom line. */
5237 if (height > 3 * FRAME_LINE_HEIGHT (f))
5239 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5240 flash_left,
5241 (FRAME_INTERNAL_BORDER_WIDTH (f)
5242 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5243 width, flash_height);
5244 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5245 flash_left,
5246 (height - flash_height
5247 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5248 width, flash_height);
5250 else
5251 /* If it is short, flash it all. */
5252 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5253 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5254 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5256 XFreeGC (FRAME_X_DISPLAY (f), gc);
5257 x_flush (f);
5261 UNBLOCK_INPUT;
5264 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5267 /* Make audible bell. */
5269 void
5270 XTring_bell ()
5272 struct frame *f = SELECTED_FRAME ();
5274 if (FRAME_X_DISPLAY (f))
5276 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5277 if (visible_bell)
5278 XTflash (f);
5279 else
5280 #endif
5282 BLOCK_INPUT;
5283 XBell (FRAME_X_DISPLAY (f), 0);
5284 XFlush (FRAME_X_DISPLAY (f));
5285 UNBLOCK_INPUT;
5291 /* Specify how many text lines, from the top of the window,
5292 should be affected by insert-lines and delete-lines operations.
5293 This, and those operations, are used only within an update
5294 that is bounded by calls to x_update_begin and x_update_end. */
5296 static void
5297 XTset_terminal_window (n)
5298 register int n;
5300 /* This function intentionally left blank. */
5305 /***********************************************************************
5306 Line Dance
5307 ***********************************************************************/
5309 /* Perform an insert-lines or delete-lines operation, inserting N
5310 lines or deleting -N lines at vertical position VPOS. */
5312 static void
5313 x_ins_del_lines (vpos, n)
5314 int vpos, n;
5316 abort ();
5320 /* Scroll part of the display as described by RUN. */
5322 static void
5323 x_scroll_run (w, run)
5324 struct window *w;
5325 struct run *run;
5327 struct frame *f = XFRAME (w->frame);
5328 int x, y, width, height, from_y, to_y, bottom_y;
5330 /* Get frame-relative bounding box of the text display area of W,
5331 without mode lines. Include in this box the flags areas to the
5332 left and right of W. */
5333 window_box (w, -1, &x, &y, &width, &height);
5334 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5335 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5337 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5338 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5339 bottom_y = y + height;
5341 if (to_y < from_y)
5343 /* Scrolling up. Make sure we don't copy part of the mode
5344 line at the bottom. */
5345 if (from_y + run->height > bottom_y)
5346 height = bottom_y - from_y;
5347 else
5348 height = run->height;
5350 else
5352 /* Scolling down. Make sure we don't copy over the mode line.
5353 at the bottom. */
5354 if (to_y + run->height > bottom_y)
5355 height = bottom_y - to_y;
5356 else
5357 height = run->height;
5360 BLOCK_INPUT;
5362 /* Cursor off. Will be switched on again in x_update_window_end. */
5363 updated_window = w;
5364 x_clear_cursor (w);
5366 XCopyArea (FRAME_X_DISPLAY (f),
5367 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5368 f->output_data.x->normal_gc,
5369 x, from_y,
5370 width, height,
5371 x, to_y);
5373 UNBLOCK_INPUT;
5378 /***********************************************************************
5379 Exposure Events
5380 ***********************************************************************/
5382 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5383 corner of the exposed rectangle. W and H are width and height of
5384 the exposed area. All are pixel values. W or H zero means redraw
5385 the entire frame. */
5387 static void
5388 expose_frame (f, x, y, w, h)
5389 struct frame *f;
5390 int x, y, w, h;
5392 XRectangle r;
5394 TRACE ((stderr, "expose_frame "));
5396 /* No need to redraw if frame will be redrawn soon. */
5397 if (FRAME_GARBAGED_P (f))
5399 TRACE ((stderr, " garbaged\n"));
5400 return;
5403 /* If basic faces haven't been realized yet, there is no point in
5404 trying to redraw anything. This can happen when we get an expose
5405 event while Emacs is starting, e.g. by moving another window. */
5406 if (FRAME_FACE_CACHE (f) == NULL
5407 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5409 TRACE ((stderr, " no faces\n"));
5410 return;
5413 if (w == 0 || h == 0)
5415 r.x = r.y = 0;
5416 r.width = CANON_X_UNIT (f) * f->width;
5417 r.height = CANON_Y_UNIT (f) * f->height;
5419 else
5421 r.x = x;
5422 r.y = y;
5423 r.width = w;
5424 r.height = h;
5427 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5428 expose_window_tree (XWINDOW (f->root_window), &r);
5430 if (WINDOWP (f->tool_bar_window))
5432 struct window *w = XWINDOW (f->tool_bar_window);
5433 XRectangle window_rect;
5434 XRectangle intersection_rect;
5435 int window_x, window_y, window_width, window_height;
5438 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5439 window_rect.x = window_x;
5440 window_rect.y = window_y;
5441 window_rect.width = window_width;
5442 window_rect.height = window_height;
5444 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5445 expose_window (w, &intersection_rect);
5448 #ifndef USE_X_TOOLKIT
5449 if (WINDOWP (f->menu_bar_window))
5451 struct window *w = XWINDOW (f->menu_bar_window);
5452 XRectangle window_rect;
5453 XRectangle intersection_rect;
5454 int window_x, window_y, window_width, window_height;
5457 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5458 window_rect.x = window_x;
5459 window_rect.y = window_y;
5460 window_rect.width = window_width;
5461 window_rect.height = window_height;
5463 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5464 expose_window (w, &intersection_rect);
5466 #endif /* not USE_X_TOOLKIT */
5470 /* Redraw (parts) of all windows in the window tree rooted at W that
5471 intersect R. R contains frame pixel coordinates. */
5473 static void
5474 expose_window_tree (w, r)
5475 struct window *w;
5476 XRectangle *r;
5478 while (w)
5480 if (!NILP (w->hchild))
5481 expose_window_tree (XWINDOW (w->hchild), r);
5482 else if (!NILP (w->vchild))
5483 expose_window_tree (XWINDOW (w->vchild), r);
5484 else
5486 XRectangle window_rect;
5487 XRectangle intersection_rect;
5488 struct frame *f = XFRAME (w->frame);
5489 int window_x, window_y, window_width, window_height;
5491 /* Frame-relative pixel rectangle of W. */
5492 window_box (w, -1, &window_x, &window_y, &window_width,
5493 &window_height);
5494 window_rect.x
5495 = (window_x
5496 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5497 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5498 window_rect.y = window_y;
5499 window_rect.width
5500 = (window_width
5501 + FRAME_X_FLAGS_AREA_WIDTH (f)
5502 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5503 window_rect.height
5504 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5506 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5507 expose_window (w, &intersection_rect);
5510 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5515 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5516 which intersects rectangle R. R is in window-relative coordinates. */
5518 static void
5519 expose_area (w, row, r, area)
5520 struct window *w;
5521 struct glyph_row *row;
5522 XRectangle *r;
5523 enum glyph_row_area area;
5525 int x;
5526 struct glyph *first = row->glyphs[area];
5527 struct glyph *end = row->glyphs[area] + row->used[area];
5528 struct glyph *last;
5529 int first_x;
5531 /* Set x to the window-relative start position for drawing glyphs of
5532 AREA. The first glyph of the text area can be partially visible.
5533 The first glyphs of other areas cannot. */
5534 if (area == LEFT_MARGIN_AREA)
5535 x = 0;
5536 else if (area == TEXT_AREA)
5537 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5538 else
5539 x = (window_box_width (w, LEFT_MARGIN_AREA)
5540 + window_box_width (w, TEXT_AREA));
5542 if (area == TEXT_AREA && row->fill_line_p)
5543 /* If row extends face to end of line write the whole line. */
5544 x_draw_glyphs (w, x, row, area,
5545 0, row->used[area],
5546 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5547 NULL, NULL, 0);
5548 else
5550 /* Find the first glyph that must be redrawn. */
5551 while (first < end
5552 && x + first->pixel_width < r->x)
5554 x += first->pixel_width;
5555 ++first;
5558 /* Find the last one. */
5559 last = first;
5560 first_x = x;
5561 while (last < end
5562 && x < r->x + r->width)
5564 x += last->pixel_width;
5565 ++last;
5568 /* Repaint. */
5569 if (last > first)
5570 x_draw_glyphs (w, first_x, row, area,
5571 first - row->glyphs[area],
5572 last - row->glyphs[area],
5573 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5574 NULL, NULL, 0);
5579 /* Redraw the parts of the glyph row ROW on window W intersecting
5580 rectangle R. R is in window-relative coordinates. */
5582 static void
5583 expose_line (w, row, r)
5584 struct window *w;
5585 struct glyph_row *row;
5586 XRectangle *r;
5588 xassert (row->enabled_p);
5590 if (row->mode_line_p || w->pseudo_window_p)
5591 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5592 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5593 NULL, NULL, 0);
5594 else
5596 if (row->used[LEFT_MARGIN_AREA])
5597 expose_area (w, row, r, LEFT_MARGIN_AREA);
5598 if (row->used[TEXT_AREA])
5599 expose_area (w, row, r, TEXT_AREA);
5600 if (row->used[RIGHT_MARGIN_AREA])
5601 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5602 x_draw_row_bitmaps (w, row);
5607 /* Return non-zero if W's cursor intersects rectangle R. */
5609 static int
5610 x_phys_cursor_in_rect_p (w, r)
5611 struct window *w;
5612 XRectangle *r;
5614 XRectangle cr, result;
5615 struct glyph *cursor_glyph;
5617 cursor_glyph = get_phys_cursor_glyph (w);
5618 if (cursor_glyph)
5620 cr.x = w->phys_cursor.x;
5621 cr.y = w->phys_cursor.y;
5622 cr.width = cursor_glyph->pixel_width;
5623 cr.height = w->phys_cursor_height;
5624 return x_intersect_rectangles (&cr, r, &result);
5626 else
5627 return 0;
5631 /* Redraw a rectangle of window W. R is a rectangle in window
5632 relative coordinates. Call this function with input blocked. */
5634 static void
5635 expose_window (w, r)
5636 struct window *w;
5637 XRectangle *r;
5639 struct glyph_row *row;
5640 int y;
5641 int yb = window_text_bottom_y (w);
5642 int cursor_cleared_p;
5644 /* If window is not yet fully initialized, do nothing. This can
5645 happen when toolkit scroll bars are used and a window is split.
5646 Reconfiguring the scroll bar will generate an expose for a newly
5647 created window. */
5648 if (w->current_matrix == NULL)
5649 return;
5651 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5652 r->x, r->y, r->width, r->height));
5654 /* Convert to window coordinates. */
5655 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5656 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5658 /* Turn off the cursor. */
5659 if (!w->pseudo_window_p
5660 && x_phys_cursor_in_rect_p (w, r))
5662 x_clear_cursor (w);
5663 cursor_cleared_p = 1;
5665 else
5666 cursor_cleared_p = 0;
5668 /* Find the first row intersecting the rectangle R. */
5669 row = w->current_matrix->rows;
5670 y = 0;
5671 while (row->enabled_p
5672 && y < yb
5673 && y + row->height < r->y)
5675 y += row->height;
5676 ++row;
5679 /* Display the text in the rectangle, one text line at a time. */
5680 while (row->enabled_p
5681 && y < yb
5682 && y < r->y + r->height)
5684 expose_line (w, row, r);
5685 y += row->height;
5686 ++row;
5689 /* Display the mode line if there is one. */
5690 if (WINDOW_WANTS_MODELINE_P (w)
5691 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5692 row->enabled_p)
5693 && row->y < r->y + r->height)
5694 expose_line (w, row, r);
5696 if (!w->pseudo_window_p)
5698 /* Draw border between windows. */
5699 x_draw_vertical_border (w);
5701 /* Turn the cursor on again. */
5702 if (cursor_cleared_p)
5703 x_update_window_cursor (w, 1);
5708 /* Determine the intersection of two rectangles R1 and R2. Return
5709 the intersection in *RESULT. Value is non-zero if RESULT is not
5710 empty. */
5712 static int
5713 x_intersect_rectangles (r1, r2, result)
5714 XRectangle *r1, *r2, *result;
5716 XRectangle *left, *right;
5717 XRectangle *upper, *lower;
5718 int intersection_p = 0;
5720 /* Rearrange so that R1 is the left-most rectangle. */
5721 if (r1->x < r2->x)
5722 left = r1, right = r2;
5723 else
5724 left = r2, right = r1;
5726 /* X0 of the intersection is right.x0, if this is inside R1,
5727 otherwise there is no intersection. */
5728 if (right->x <= left->x + left->width)
5730 result->x = right->x;
5732 /* The right end of the intersection is the minimum of the
5733 the right ends of left and right. */
5734 result->width = (min (left->x + left->width, right->x + right->width)
5735 - result->x);
5737 /* Same game for Y. */
5738 if (r1->y < r2->y)
5739 upper = r1, lower = r2;
5740 else
5741 upper = r2, lower = r1;
5743 /* The upper end of the intersection is lower.y0, if this is inside
5744 of upper. Otherwise, there is no intersection. */
5745 if (lower->y <= upper->y + upper->height)
5747 result->y = lower->y;
5749 /* The lower end of the intersection is the minimum of the lower
5750 ends of upper and lower. */
5751 result->height = (min (lower->y + lower->height,
5752 upper->y + upper->height)
5753 - result->y);
5754 intersection_p = 1;
5758 return intersection_p;
5765 static void
5766 frame_highlight (f)
5767 struct frame *f;
5769 /* We used to only do this if Vx_no_window_manager was non-nil, but
5770 the ICCCM (section 4.1.6) says that the window's border pixmap
5771 and border pixel are window attributes which are "private to the
5772 client", so we can always change it to whatever we want. */
5773 BLOCK_INPUT;
5774 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5775 f->output_data.x->border_pixel);
5776 UNBLOCK_INPUT;
5777 x_update_cursor (f, 1);
5780 static void
5781 frame_unhighlight (f)
5782 struct frame *f;
5784 /* We used to only do this if Vx_no_window_manager was non-nil, but
5785 the ICCCM (section 4.1.6) says that the window's border pixmap
5786 and border pixel are window attributes which are "private to the
5787 client", so we can always change it to whatever we want. */
5788 BLOCK_INPUT;
5789 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5790 f->output_data.x->border_tile);
5791 UNBLOCK_INPUT;
5792 x_update_cursor (f, 1);
5795 /* The focus has changed. Update the frames as necessary to reflect
5796 the new situation. Note that we can't change the selected frame
5797 here, because the Lisp code we are interrupting might become confused.
5798 Each event gets marked with the frame in which it occurred, so the
5799 Lisp code can tell when the switch took place by examining the events. */
5801 static void
5802 x_new_focus_frame (dpyinfo, frame)
5803 struct x_display_info *dpyinfo;
5804 struct frame *frame;
5806 struct frame *old_focus = dpyinfo->x_focus_frame;
5808 if (frame != dpyinfo->x_focus_frame)
5810 /* Set this before calling other routines, so that they see
5811 the correct value of x_focus_frame. */
5812 dpyinfo->x_focus_frame = frame;
5814 if (old_focus && old_focus->auto_lower)
5815 x_lower_frame (old_focus);
5817 #if 0
5818 selected_frame = frame;
5819 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5820 selected_frame);
5821 Fselect_window (selected_frame->selected_window);
5822 choose_minibuf_frame ();
5823 #endif /* ! 0 */
5825 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5826 pending_autoraise_frame = dpyinfo->x_focus_frame;
5827 else
5828 pending_autoraise_frame = 0;
5831 x_frame_rehighlight (dpyinfo);
5834 /* Handle an event saying the mouse has moved out of an Emacs frame. */
5836 void
5837 x_mouse_leave (dpyinfo)
5838 struct x_display_info *dpyinfo;
5840 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
5843 /* The focus has changed, or we have redirected a frame's focus to
5844 another frame (this happens when a frame uses a surrogate
5845 mini-buffer frame). Shift the highlight as appropriate.
5847 The FRAME argument doesn't necessarily have anything to do with which
5848 frame is being highlighted or un-highlighted; we only use it to find
5849 the appropriate X display info. */
5851 static void
5852 XTframe_rehighlight (frame)
5853 struct frame *frame;
5855 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
5858 static void
5859 x_frame_rehighlight (dpyinfo)
5860 struct x_display_info *dpyinfo;
5862 struct frame *old_highlight = dpyinfo->x_highlight_frame;
5864 if (dpyinfo->x_focus_frame)
5866 dpyinfo->x_highlight_frame
5867 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
5868 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
5869 : dpyinfo->x_focus_frame);
5870 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
5872 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
5873 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
5876 else
5877 dpyinfo->x_highlight_frame = 0;
5879 if (dpyinfo->x_highlight_frame != old_highlight)
5881 if (old_highlight)
5882 frame_unhighlight (old_highlight);
5883 if (dpyinfo->x_highlight_frame)
5884 frame_highlight (dpyinfo->x_highlight_frame);
5890 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
5892 /* Initialize mode_switch_bit and modifier_meaning. */
5893 static void
5894 x_find_modifier_meanings (dpyinfo)
5895 struct x_display_info *dpyinfo;
5897 int min_code, max_code;
5898 KeySym *syms;
5899 int syms_per_code;
5900 XModifierKeymap *mods;
5902 dpyinfo->meta_mod_mask = 0;
5903 dpyinfo->shift_lock_mask = 0;
5904 dpyinfo->alt_mod_mask = 0;
5905 dpyinfo->super_mod_mask = 0;
5906 dpyinfo->hyper_mod_mask = 0;
5908 #ifdef HAVE_X11R4
5909 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
5910 #else
5911 min_code = dpyinfo->display->min_keycode;
5912 max_code = dpyinfo->display->max_keycode;
5913 #endif
5915 syms = XGetKeyboardMapping (dpyinfo->display,
5916 min_code, max_code - min_code + 1,
5917 &syms_per_code);
5918 mods = XGetModifierMapping (dpyinfo->display);
5920 /* Scan the modifier table to see which modifier bits the Meta and
5921 Alt keysyms are on. */
5923 int row, col; /* The row and column in the modifier table. */
5925 for (row = 3; row < 8; row++)
5926 for (col = 0; col < mods->max_keypermod; col++)
5928 KeyCode code
5929 = mods->modifiermap[(row * mods->max_keypermod) + col];
5931 /* Zeroes are used for filler. Skip them. */
5932 if (code == 0)
5933 continue;
5935 /* Are any of this keycode's keysyms a meta key? */
5937 int code_col;
5939 for (code_col = 0; code_col < syms_per_code; code_col++)
5941 int sym = syms[((code - min_code) * syms_per_code) + code_col];
5943 switch (sym)
5945 case XK_Meta_L:
5946 case XK_Meta_R:
5947 dpyinfo->meta_mod_mask |= (1 << row);
5948 break;
5950 case XK_Alt_L:
5951 case XK_Alt_R:
5952 dpyinfo->alt_mod_mask |= (1 << row);
5953 break;
5955 case XK_Hyper_L:
5956 case XK_Hyper_R:
5957 dpyinfo->hyper_mod_mask |= (1 << row);
5958 break;
5960 case XK_Super_L:
5961 case XK_Super_R:
5962 dpyinfo->super_mod_mask |= (1 << row);
5963 break;
5965 case XK_Shift_Lock:
5966 /* Ignore this if it's not on the lock modifier. */
5967 if ((1 << row) == LockMask)
5968 dpyinfo->shift_lock_mask = LockMask;
5969 break;
5976 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
5977 if (! dpyinfo->meta_mod_mask)
5979 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
5980 dpyinfo->alt_mod_mask = 0;
5983 /* If some keys are both alt and meta,
5984 make them just meta, not alt. */
5985 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
5987 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
5990 XFree ((char *) syms);
5991 XFreeModifiermap (mods);
5994 /* Convert between the modifier bits X uses and the modifier bits
5995 Emacs uses. */
5997 static unsigned int
5998 x_x_to_emacs_modifiers (dpyinfo, state)
5999 struct x_display_info *dpyinfo;
6000 unsigned int state;
6002 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6003 | ((state & ControlMask) ? ctrl_modifier : 0)
6004 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6005 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6006 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6007 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6010 static unsigned int
6011 x_emacs_to_x_modifiers (dpyinfo, state)
6012 struct x_display_info *dpyinfo;
6013 unsigned int state;
6015 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6016 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6017 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6018 | ((state & shift_modifier) ? ShiftMask : 0)
6019 | ((state & ctrl_modifier) ? ControlMask : 0)
6020 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6023 /* Convert a keysym to its name. */
6025 char *
6026 x_get_keysym_name (keysym)
6027 KeySym keysym;
6029 char *value;
6031 BLOCK_INPUT;
6032 value = XKeysymToString (keysym);
6033 UNBLOCK_INPUT;
6035 return value;
6040 /* Mouse clicks and mouse movement. Rah. */
6042 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6043 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6044 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6045 not force the value into range. */
6047 void
6048 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6049 FRAME_PTR f;
6050 register int pix_x, pix_y;
6051 register int *x, *y;
6052 XRectangle *bounds;
6053 int noclip;
6055 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6056 even for negative values. */
6057 if (pix_x < 0)
6058 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6059 if (pix_y < 0)
6060 pix_y -= (f)->output_data.x->line_height - 1;
6062 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6063 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6065 if (bounds)
6067 bounds->width = FONT_WIDTH (f->output_data.x->font);
6068 bounds->height = f->output_data.x->line_height;
6069 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6070 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6073 if (!noclip)
6075 if (pix_x < 0)
6076 pix_x = 0;
6077 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6078 pix_x = FRAME_WINDOW_WIDTH (f);
6080 if (pix_y < 0)
6081 pix_y = 0;
6082 else if (pix_y > f->height)
6083 pix_y = f->height;
6086 *x = pix_x;
6087 *y = pix_y;
6091 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6092 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6093 can't tell the positions because W's display is not up to date,
6094 return 0. */
6097 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6098 struct window *w;
6099 int hpos, vpos;
6100 int *frame_x, *frame_y;
6102 int success_p;
6104 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6105 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6107 if (display_completed)
6109 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6110 struct glyph *glyph = row->glyphs[TEXT_AREA];
6111 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6113 *frame_y = row->y;
6114 *frame_x = row->x;
6115 while (glyph < end)
6117 *frame_x += glyph->pixel_width;
6118 ++glyph;
6121 success_p = 1;
6123 else
6125 *frame_y = *frame_x = 0;
6126 success_p = 0;
6129 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6130 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6131 return success_p;
6135 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6137 If the event is a button press, then note that we have grabbed
6138 the mouse. */
6140 static Lisp_Object
6141 construct_mouse_click (result, event, f)
6142 struct input_event *result;
6143 XButtonEvent *event;
6144 struct frame *f;
6146 /* Make the event type no_event; we'll change that when we decide
6147 otherwise. */
6148 result->kind = mouse_click;
6149 result->code = event->button - Button1;
6150 result->timestamp = event->time;
6151 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6152 event->state)
6153 | (event->type == ButtonRelease
6154 ? up_modifier
6155 : down_modifier));
6157 XSETINT (result->x, event->x);
6158 XSETINT (result->y, event->y);
6159 XSETFRAME (result->frame_or_window, f);
6160 result->arg = Qnil;
6161 return Qnil;
6165 /* Function to report a mouse movement to the mainstream Emacs code.
6166 The input handler calls this.
6168 We have received a mouse movement event, which is given in *event.
6169 If the mouse is over a different glyph than it was last time, tell
6170 the mainstream emacs code by setting mouse_moved. If not, ask for
6171 another motion event, so we can check again the next time it moves. */
6173 static XMotionEvent last_mouse_motion_event;
6174 static Lisp_Object last_mouse_motion_frame;
6176 static void
6177 note_mouse_movement (frame, event)
6178 FRAME_PTR frame;
6179 XMotionEvent *event;
6181 last_mouse_movement_time = event->time;
6182 last_mouse_motion_event = *event;
6183 XSETFRAME (last_mouse_motion_frame, frame);
6185 if (event->window != FRAME_X_WINDOW (frame))
6187 frame->mouse_moved = 1;
6188 last_mouse_scroll_bar = Qnil;
6189 note_mouse_highlight (frame, -1, -1);
6192 /* Has the mouse moved off the glyph it was on at the last sighting? */
6193 else if (event->x < last_mouse_glyph.x
6194 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6195 || event->y < last_mouse_glyph.y
6196 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6198 frame->mouse_moved = 1;
6199 last_mouse_scroll_bar = Qnil;
6200 note_mouse_highlight (frame, event->x, event->y);
6204 /* This is used for debugging, to turn off note_mouse_highlight. */
6206 int disable_mouse_highlight;
6210 /************************************************************************
6211 Mouse Face
6212 ************************************************************************/
6214 /* Find the glyph under window-relative coordinates X/Y in window W.
6215 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6216 strings. Return in *HPOS and *VPOS the row and column number of
6217 the glyph found. Return in *AREA the glyph area containing X.
6218 Value is a pointer to the glyph found or null if X/Y is not on
6219 text, or we can't tell because W's current matrix is not up to
6220 date. */
6222 static struct glyph *
6223 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6224 struct window *w;
6225 int x, y;
6226 int *hpos, *vpos, *area;
6228 struct glyph *glyph, *end;
6229 struct glyph_row *row = NULL;
6230 int x0, i, left_area_width;
6232 /* Find row containing Y. Give up if some row is not enabled. */
6233 for (i = 0; i < w->current_matrix->nrows; ++i)
6235 row = MATRIX_ROW (w->current_matrix, i);
6236 if (!row->enabled_p)
6237 return NULL;
6238 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6239 break;
6242 *vpos = i;
6243 *hpos = 0;
6245 /* Give up if Y is not in the window. */
6246 if (i == w->current_matrix->nrows)
6247 return NULL;
6249 /* Get the glyph area containing X. */
6250 if (w->pseudo_window_p)
6252 *area = TEXT_AREA;
6253 x0 = 0;
6255 else
6257 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6258 if (x < left_area_width)
6260 *area = LEFT_MARGIN_AREA;
6261 x0 = 0;
6263 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6265 *area = TEXT_AREA;
6266 x0 = row->x + left_area_width;
6268 else
6270 *area = RIGHT_MARGIN_AREA;
6271 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6275 /* Find glyph containing X. */
6276 glyph = row->glyphs[*area];
6277 end = glyph + row->used[*area];
6278 while (glyph < end)
6280 if (x < x0 + glyph->pixel_width)
6282 if (w->pseudo_window_p)
6283 break;
6284 else if (BUFFERP (glyph->object))
6285 break;
6288 x0 += glyph->pixel_width;
6289 ++glyph;
6292 if (glyph == end)
6293 return NULL;
6295 *hpos = glyph - row->glyphs[*area];
6296 return glyph;
6300 /* Convert frame-relative x/y to coordinates relative to window W.
6301 Takes pseudo-windows into account. */
6303 static void
6304 frame_to_window_pixel_xy (w, x, y)
6305 struct window *w;
6306 int *x, *y;
6308 if (w->pseudo_window_p)
6310 /* A pseudo-window is always full-width, and starts at the
6311 left edge of the frame, plus a frame border. */
6312 struct frame *f = XFRAME (w->frame);
6313 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6314 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6316 else
6318 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6319 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6324 /* Take proper action when mouse has moved to the mode or top line of
6325 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6326 mode line. X is relative to the start of the text display area of
6327 W, so the width of bitmap areas and scroll bars must be subtracted
6328 to get a position relative to the start of the mode line. */
6330 static void
6331 note_mode_line_highlight (w, x, mode_line_p)
6332 struct window *w;
6333 int x, mode_line_p;
6335 struct frame *f = XFRAME (w->frame);
6336 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6337 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6338 struct glyph_row *row;
6340 if (mode_line_p)
6341 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6342 else
6343 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6345 if (row->enabled_p)
6347 struct glyph *glyph, *end;
6348 Lisp_Object help, map;
6349 int x0;
6351 /* Find the glyph under X. */
6352 glyph = row->glyphs[TEXT_AREA];
6353 end = glyph + row->used[TEXT_AREA];
6354 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6355 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6356 while (glyph < end
6357 && x >= x0 + glyph->pixel_width)
6359 x0 += glyph->pixel_width;
6360 ++glyph;
6363 if (glyph < end
6364 && STRINGP (glyph->object)
6365 && XSTRING (glyph->object)->intervals
6366 && glyph->charpos >= 0
6367 && glyph->charpos < XSTRING (glyph->object)->size)
6369 /* If we're on a string with `help-echo' text property,
6370 arrange for the help to be displayed. This is done by
6371 setting the global variable help_echo to the help string. */
6372 help = Fget_text_property (make_number (glyph->charpos),
6373 Qhelp_echo, glyph->object);
6374 if (!NILP (help))
6376 help_echo = help;
6377 XSETWINDOW (help_echo_window, w);
6378 help_echo_object = glyph->object;
6379 help_echo_pos = glyph->charpos;
6382 /* Change the mouse pointer according to what is under X/Y. */
6383 map = Fget_text_property (make_number (glyph->charpos),
6384 Qlocal_map, glyph->object);
6385 if (!NILP (Fkeymapp (map)))
6386 cursor = f->output_data.x->nontext_cursor;
6387 else
6389 map = Fget_text_property (make_number (glyph->charpos),
6390 Qkeymap, glyph->object);
6391 if (!NILP (Fkeymapp (map)))
6392 cursor = f->output_data.x->nontext_cursor;
6397 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6401 /* Take proper action when the mouse has moved to position X, Y on
6402 frame F as regards highlighting characters that have mouse-face
6403 properties. Also de-highlighting chars where the mouse was before.
6404 X and Y can be negative or out of range. */
6406 static void
6407 note_mouse_highlight (f, x, y)
6408 struct frame *f;
6409 int x, y;
6411 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6412 int portion;
6413 Lisp_Object window;
6414 struct window *w;
6416 /* When a menu is active, don't highlight because this looks odd. */
6417 #ifdef USE_X_TOOLKIT
6418 if (popup_activated ())
6419 return;
6420 #endif
6422 if (disable_mouse_highlight
6423 || !f->glyphs_initialized_p)
6424 return;
6426 dpyinfo->mouse_face_mouse_x = x;
6427 dpyinfo->mouse_face_mouse_y = y;
6428 dpyinfo->mouse_face_mouse_frame = f;
6430 if (dpyinfo->mouse_face_defer)
6431 return;
6433 if (gc_in_progress)
6435 dpyinfo->mouse_face_deferred_gc = 1;
6436 return;
6439 /* Which window is that in? */
6440 window = window_from_coordinates (f, x, y, &portion, 1);
6442 /* If we were displaying active text in another window, clear that. */
6443 if (! EQ (window, dpyinfo->mouse_face_window))
6444 clear_mouse_face (dpyinfo);
6446 /* Not on a window -> return. */
6447 if (!WINDOWP (window))
6448 return;
6450 /* Convert to window-relative pixel coordinates. */
6451 w = XWINDOW (window);
6452 frame_to_window_pixel_xy (w, &x, &y);
6454 /* Handle tool-bar window differently since it doesn't display a
6455 buffer. */
6456 if (EQ (window, f->tool_bar_window))
6458 note_tool_bar_highlight (f, x, y);
6459 return;
6462 if (portion == 1 || portion == 3)
6464 /* Mouse is on the mode or top line. */
6465 note_mode_line_highlight (w, x, portion == 1);
6466 return;
6468 else
6469 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6470 f->output_data.x->text_cursor);
6472 /* Are we in a window whose display is up to date?
6473 And verify the buffer's text has not changed. */
6474 if (/* Within text portion of the window. */
6475 portion == 0
6476 && EQ (w->window_end_valid, w->buffer)
6477 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6478 && (XFASTINT (w->last_overlay_modified)
6479 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6481 int hpos, vpos, pos, i, area;
6482 struct glyph *glyph;
6484 /* Find the glyph under X/Y. */
6485 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6487 /* Clear mouse face if X/Y not over text. */
6488 if (glyph == NULL
6489 || area != TEXT_AREA
6490 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6492 clear_mouse_face (dpyinfo);
6493 return;
6496 pos = glyph->charpos;
6497 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6499 /* Check for mouse-face and help-echo. */
6501 Lisp_Object mouse_face, overlay, position;
6502 Lisp_Object *overlay_vec;
6503 int len, noverlays;
6504 struct buffer *obuf;
6505 int obegv, ozv;
6507 /* If we get an out-of-range value, return now; avoid an error. */
6508 if (pos > BUF_Z (XBUFFER (w->buffer)))
6509 return;
6511 /* Make the window's buffer temporarily current for
6512 overlays_at and compute_char_face. */
6513 obuf = current_buffer;
6514 current_buffer = XBUFFER (w->buffer);
6515 obegv = BEGV;
6516 ozv = ZV;
6517 BEGV = BEG;
6518 ZV = Z;
6520 /* Is this char mouse-active or does it have help-echo? */
6521 XSETINT (position, pos);
6523 /* Put all the overlays we want in a vector in overlay_vec.
6524 Store the length in len. If there are more than 10, make
6525 enough space for all, and try again. */
6526 len = 10;
6527 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6528 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6529 if (noverlays > len)
6531 len = noverlays;
6532 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6533 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6536 /* Sort overlays into increasing priority order. */
6537 noverlays = sort_overlays (overlay_vec, noverlays, w);
6539 /* Check mouse-face highlighting. */
6540 if (! (EQ (window, dpyinfo->mouse_face_window)
6541 && vpos >= dpyinfo->mouse_face_beg_row
6542 && vpos <= dpyinfo->mouse_face_end_row
6543 && (vpos > dpyinfo->mouse_face_beg_row
6544 || hpos >= dpyinfo->mouse_face_beg_col)
6545 && (vpos < dpyinfo->mouse_face_end_row
6546 || hpos < dpyinfo->mouse_face_end_col
6547 || dpyinfo->mouse_face_past_end)))
6549 /* Clear the display of the old active region, if any. */
6550 clear_mouse_face (dpyinfo);
6552 /* Find the highest priority overlay that has a mouse-face prop. */
6553 overlay = Qnil;
6554 for (i = noverlays - 1; i >= 0; --i)
6556 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6557 if (!NILP (mouse_face))
6559 overlay = overlay_vec[i];
6560 break;
6564 /* If no overlay applies, get a text property. */
6565 if (NILP (overlay))
6566 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6568 /* Handle the overlay case. */
6569 if (! NILP (overlay))
6571 /* Find the range of text around this char that
6572 should be active. */
6573 Lisp_Object before, after;
6574 int ignore;
6576 before = Foverlay_start (overlay);
6577 after = Foverlay_end (overlay);
6578 /* Record this as the current active region. */
6579 fast_find_position (w, XFASTINT (before),
6580 &dpyinfo->mouse_face_beg_col,
6581 &dpyinfo->mouse_face_beg_row,
6582 &dpyinfo->mouse_face_beg_x,
6583 &dpyinfo->mouse_face_beg_y);
6584 dpyinfo->mouse_face_past_end
6585 = !fast_find_position (w, XFASTINT (after),
6586 &dpyinfo->mouse_face_end_col,
6587 &dpyinfo->mouse_face_end_row,
6588 &dpyinfo->mouse_face_end_x,
6589 &dpyinfo->mouse_face_end_y);
6590 dpyinfo->mouse_face_window = window;
6591 dpyinfo->mouse_face_face_id
6592 = face_at_buffer_position (w, pos, 0, 0,
6593 &ignore, pos + 1, 1);
6595 /* Display it as active. */
6596 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6598 /* Handle the text property case. */
6599 else if (! NILP (mouse_face))
6601 /* Find the range of text around this char that
6602 should be active. */
6603 Lisp_Object before, after, beginning, end;
6604 int ignore;
6606 beginning = Fmarker_position (w->start);
6607 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6608 - XFASTINT (w->window_end_pos)));
6609 before
6610 = Fprevious_single_property_change (make_number (pos + 1),
6611 Qmouse_face,
6612 w->buffer, beginning);
6613 after
6614 = Fnext_single_property_change (position, Qmouse_face,
6615 w->buffer, end);
6616 /* Record this as the current active region. */
6617 fast_find_position (w, XFASTINT (before),
6618 &dpyinfo->mouse_face_beg_col,
6619 &dpyinfo->mouse_face_beg_row,
6620 &dpyinfo->mouse_face_beg_x,
6621 &dpyinfo->mouse_face_beg_y);
6622 dpyinfo->mouse_face_past_end
6623 = !fast_find_position (w, XFASTINT (after),
6624 &dpyinfo->mouse_face_end_col,
6625 &dpyinfo->mouse_face_end_row,
6626 &dpyinfo->mouse_face_end_x,
6627 &dpyinfo->mouse_face_end_y);
6628 dpyinfo->mouse_face_window = window;
6629 dpyinfo->mouse_face_face_id
6630 = face_at_buffer_position (w, pos, 0, 0,
6631 &ignore, pos + 1, 1);
6633 /* Display it as active. */
6634 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6638 /* Look for a `help-echo' property. */
6640 Lisp_Object help, overlay;
6642 /* Check overlays first. */
6643 help = Qnil;
6644 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6646 overlay = overlay_vec[i];
6647 help = Foverlay_get (overlay, Qhelp_echo);
6650 if (!NILP (help))
6652 help_echo = help;
6653 help_echo_window = window;
6654 help_echo_object = overlay;
6655 help_echo_pos = pos;
6657 else
6659 /* Try text properties. */
6660 if ((STRINGP (glyph->object)
6661 && glyph->charpos >= 0
6662 && glyph->charpos < XSTRING (glyph->object)->size)
6663 || (BUFFERP (glyph->object)
6664 && glyph->charpos >= BEGV
6665 && glyph->charpos < ZV))
6666 help = Fget_text_property (make_number (glyph->charpos),
6667 Qhelp_echo, glyph->object);
6669 if (!NILP (help))
6671 help_echo = help;
6672 help_echo_window = window;
6673 help_echo_object = glyph->object;
6674 help_echo_pos = glyph->charpos;
6679 BEGV = obegv;
6680 ZV = ozv;
6681 current_buffer = obuf;
6686 static void
6687 redo_mouse_highlight ()
6689 if (!NILP (last_mouse_motion_frame)
6690 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6691 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6692 last_mouse_motion_event.x,
6693 last_mouse_motion_event.y);
6698 /***********************************************************************
6699 Tool-bars
6700 ***********************************************************************/
6702 static int x_tool_bar_item P_ ((struct frame *, int, int,
6703 struct glyph **, int *, int *, int *));
6705 /* Tool-bar item index of the item on which a mouse button was pressed
6706 or -1. */
6708 static int last_tool_bar_item;
6711 /* Get information about the tool-bar item at position X/Y on frame F.
6712 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6713 the current matrix of the tool-bar window of F, or NULL if not
6714 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6715 item in F->current_tool_bar_items. Value is
6717 -1 if X/Y is not on a tool-bar item
6718 0 if X/Y is on the same item that was highlighted before.
6719 1 otherwise. */
6721 static int
6722 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6723 struct frame *f;
6724 int x, y;
6725 struct glyph **glyph;
6726 int *hpos, *vpos, *prop_idx;
6728 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6729 struct window *w = XWINDOW (f->tool_bar_window);
6730 int area;
6732 /* Find the glyph under X/Y. */
6733 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6734 if (*glyph == NULL)
6735 return -1;
6737 /* Get the start of this tool-bar item's properties in
6738 f->current_tool_bar_items. */
6739 if (!tool_bar_item_info (f, *glyph, prop_idx))
6740 return -1;
6742 /* Is mouse on the highlighted item? */
6743 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6744 && *vpos >= dpyinfo->mouse_face_beg_row
6745 && *vpos <= dpyinfo->mouse_face_end_row
6746 && (*vpos > dpyinfo->mouse_face_beg_row
6747 || *hpos >= dpyinfo->mouse_face_beg_col)
6748 && (*vpos < dpyinfo->mouse_face_end_row
6749 || *hpos < dpyinfo->mouse_face_end_col
6750 || dpyinfo->mouse_face_past_end))
6751 return 0;
6753 return 1;
6757 /* Handle mouse button event on the tool-bar of frame F, at
6758 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6759 or ButtonRelase. */
6761 static void
6762 x_handle_tool_bar_click (f, button_event)
6763 struct frame *f;
6764 XButtonEvent *button_event;
6766 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6767 struct window *w = XWINDOW (f->tool_bar_window);
6768 int hpos, vpos, prop_idx;
6769 struct glyph *glyph;
6770 Lisp_Object enabled_p;
6771 int x = button_event->x;
6772 int y = button_event->y;
6774 /* If not on the highlighted tool-bar item, return. */
6775 frame_to_window_pixel_xy (w, &x, &y);
6776 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6777 return;
6779 /* If item is disabled, do nothing. */
6780 enabled_p = (XVECTOR (f->current_tool_bar_items)
6781 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6782 if (NILP (enabled_p))
6783 return;
6785 if (button_event->type == ButtonPress)
6787 /* Show item in pressed state. */
6788 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6789 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6790 last_tool_bar_item = prop_idx;
6792 else
6794 Lisp_Object key, frame;
6795 struct input_event event;
6797 /* Show item in released state. */
6798 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6799 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6801 key = (XVECTOR (f->current_tool_bar_items)
6802 ->contents[prop_idx + TOOL_BAR_ITEM_KEY]);
6804 XSETFRAME (frame, f);
6805 event.kind = TOOL_BAR_EVENT;
6806 event.frame_or_window = frame;
6807 event.arg = frame;
6808 kbd_buffer_store_event (&event);
6810 event.kind = TOOL_BAR_EVENT;
6811 event.frame_or_window = frame;
6812 event.arg = key;
6813 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6814 button_event->state);
6815 kbd_buffer_store_event (&event);
6816 last_tool_bar_item = -1;
6821 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6822 tool-bar window-relative coordinates X/Y. Called from
6823 note_mouse_highlight. */
6825 static void
6826 note_tool_bar_highlight (f, x, y)
6827 struct frame *f;
6828 int x, y;
6830 Lisp_Object window = f->tool_bar_window;
6831 struct window *w = XWINDOW (window);
6832 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6833 int hpos, vpos;
6834 struct glyph *glyph;
6835 struct glyph_row *row;
6836 int i;
6837 Lisp_Object enabled_p;
6838 int prop_idx;
6839 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
6840 int mouse_down_p, rc;
6842 /* Function note_mouse_highlight is called with negative x(y
6843 values when mouse moves outside of the frame. */
6844 if (x <= 0 || y <= 0)
6846 clear_mouse_face (dpyinfo);
6847 return;
6850 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
6851 if (rc < 0)
6853 /* Not on tool-bar item. */
6854 clear_mouse_face (dpyinfo);
6855 return;
6857 else if (rc == 0)
6858 /* On same tool-bar item as before. */
6859 goto set_help_echo;
6861 clear_mouse_face (dpyinfo);
6863 /* Mouse is down, but on different tool-bar item? */
6864 mouse_down_p = (dpyinfo->grabbed
6865 && f == last_mouse_frame
6866 && FRAME_LIVE_P (f));
6867 if (mouse_down_p
6868 && last_tool_bar_item != prop_idx)
6869 return;
6871 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
6872 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
6874 /* If tool-bar item is not enabled, don't highlight it. */
6875 enabled_p = (XVECTOR (f->current_tool_bar_items)
6876 ->contents[prop_idx + TOOL_BAR_ITEM_ENABLED_P]);
6877 if (!NILP (enabled_p))
6879 /* Compute the x-position of the glyph. In front and past the
6880 image is a space. We include this is the highlighted area. */
6881 row = MATRIX_ROW (w->current_matrix, vpos);
6882 for (i = x = 0; i < hpos; ++i)
6883 x += row->glyphs[TEXT_AREA][i].pixel_width;
6885 /* Record this as the current active region. */
6886 dpyinfo->mouse_face_beg_col = hpos;
6887 dpyinfo->mouse_face_beg_row = vpos;
6888 dpyinfo->mouse_face_beg_x = x;
6889 dpyinfo->mouse_face_beg_y = row->y;
6890 dpyinfo->mouse_face_past_end = 0;
6892 dpyinfo->mouse_face_end_col = hpos + 1;
6893 dpyinfo->mouse_face_end_row = vpos;
6894 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
6895 dpyinfo->mouse_face_end_y = row->y;
6896 dpyinfo->mouse_face_window = window;
6897 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
6899 /* Display it as active. */
6900 show_mouse_face (dpyinfo, draw);
6901 dpyinfo->mouse_face_image_state = draw;
6904 set_help_echo:
6906 /* Set help_echo to a help string.to display for this tool-bar item.
6907 XTread_socket does the rest. */
6908 help_echo_object = help_echo_window = Qnil;
6909 help_echo_pos = -1;
6910 help_echo = (XVECTOR (f->current_tool_bar_items)
6911 ->contents[prop_idx + TOOL_BAR_ITEM_HELP]);
6912 if (NILP (help_echo))
6913 help_echo = (XVECTOR (f->current_tool_bar_items)
6914 ->contents[prop_idx + TOOL_BAR_ITEM_CAPTION]);
6919 /* Find the glyph matrix position of buffer position POS in window W.
6920 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
6921 current glyphs must be up to date. If POS is above window start
6922 return (0, 0, 0, 0). If POS is after end of W, return end of
6923 last line in W. */
6925 static int
6926 fast_find_position (w, pos, hpos, vpos, x, y)
6927 struct window *w;
6928 int pos;
6929 int *hpos, *vpos, *x, *y;
6931 int i;
6932 int lastcol;
6933 int maybe_next_line_p = 0;
6934 int line_start_position;
6935 int yb = window_text_bottom_y (w);
6936 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
6937 struct glyph_row *best_row = row;
6938 int row_vpos = 0, best_row_vpos = 0;
6939 int current_x;
6941 while (row->y < yb)
6943 if (row->used[TEXT_AREA])
6944 line_start_position = row->glyphs[TEXT_AREA]->charpos;
6945 else
6946 line_start_position = 0;
6948 if (line_start_position > pos)
6949 break;
6950 /* If the position sought is the end of the buffer,
6951 don't include the blank lines at the bottom of the window. */
6952 else if (line_start_position == pos
6953 && pos == BUF_ZV (XBUFFER (w->buffer)))
6955 maybe_next_line_p = 1;
6956 break;
6958 else if (line_start_position > 0)
6960 best_row = row;
6961 best_row_vpos = row_vpos;
6964 if (row->y + row->height >= yb)
6965 break;
6967 ++row;
6968 ++row_vpos;
6971 /* Find the right column within BEST_ROW. */
6972 lastcol = 0;
6973 current_x = best_row->x;
6974 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
6976 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
6977 int charpos;
6979 charpos = glyph->charpos;
6980 if (charpos == pos)
6982 *hpos = i;
6983 *vpos = best_row_vpos;
6984 *x = current_x;
6985 *y = best_row->y;
6986 return 1;
6988 else if (charpos > pos)
6989 break;
6990 else if (charpos > 0)
6991 lastcol = i;
6993 current_x += glyph->pixel_width;
6996 /* If we're looking for the end of the buffer,
6997 and we didn't find it in the line we scanned,
6998 use the start of the following line. */
6999 if (maybe_next_line_p)
7001 ++best_row;
7002 ++best_row_vpos;
7003 lastcol = 0;
7004 current_x = best_row->x;
7007 *vpos = best_row_vpos;
7008 *hpos = lastcol + 1;
7009 *x = current_x;
7010 *y = best_row->y;
7011 return 0;
7015 /* Display the active region described by mouse_face_*
7016 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7018 static void
7019 show_mouse_face (dpyinfo, draw)
7020 struct x_display_info *dpyinfo;
7021 enum draw_glyphs_face draw;
7023 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7024 struct frame *f = XFRAME (WINDOW_FRAME (w));
7025 int i;
7026 int cursor_off_p = 0;
7027 struct cursor_pos saved_cursor;
7029 saved_cursor = output_cursor;
7031 /* If window is in the process of being destroyed, don't bother
7032 to do anything. */
7033 if (w->current_matrix == NULL)
7034 goto set_x_cursor;
7036 /* Recognize when we are called to operate on rows that don't exist
7037 anymore. This can happen when a window is split. */
7038 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7039 goto set_x_cursor;
7041 set_output_cursor (&w->phys_cursor);
7043 /* Note that mouse_face_beg_row etc. are window relative. */
7044 for (i = dpyinfo->mouse_face_beg_row;
7045 i <= dpyinfo->mouse_face_end_row;
7046 i++)
7048 int start_hpos, end_hpos, start_x;
7049 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7051 /* Don't do anything if row doesn't have valid contents. */
7052 if (!row->enabled_p)
7053 continue;
7055 /* For all but the first row, the highlight starts at column 0. */
7056 if (i == dpyinfo->mouse_face_beg_row)
7058 start_hpos = dpyinfo->mouse_face_beg_col;
7059 start_x = dpyinfo->mouse_face_beg_x;
7061 else
7063 start_hpos = 0;
7064 start_x = 0;
7067 if (i == dpyinfo->mouse_face_end_row)
7068 end_hpos = dpyinfo->mouse_face_end_col;
7069 else
7070 end_hpos = row->used[TEXT_AREA];
7072 /* If the cursor's in the text we are about to rewrite, turn the
7073 cursor off. */
7074 if (!w->pseudo_window_p
7075 && i == output_cursor.vpos
7076 && output_cursor.hpos >= start_hpos - 1
7077 && output_cursor.hpos <= end_hpos)
7079 x_update_window_cursor (w, 0);
7080 cursor_off_p = 1;
7083 if (end_hpos > start_hpos)
7085 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7086 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7087 start_hpos, end_hpos, draw, NULL, NULL, 0);
7091 /* If we turned the cursor off, turn it back on. */
7092 if (cursor_off_p)
7093 x_display_cursor (w, 1,
7094 output_cursor.hpos, output_cursor.vpos,
7095 output_cursor.x, output_cursor.y);
7097 output_cursor = saved_cursor;
7099 set_x_cursor:
7101 /* Change the mouse cursor. */
7102 if (draw == DRAW_NORMAL_TEXT)
7103 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7104 f->output_data.x->text_cursor);
7105 else if (draw == DRAW_MOUSE_FACE)
7106 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7107 f->output_data.x->cross_cursor);
7108 else
7109 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7110 f->output_data.x->nontext_cursor);
7113 /* Clear out the mouse-highlighted active region.
7114 Redraw it un-highlighted first. */
7116 void
7117 clear_mouse_face (dpyinfo)
7118 struct x_display_info *dpyinfo;
7120 if (tip_frame)
7121 return;
7123 if (! NILP (dpyinfo->mouse_face_window))
7124 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7126 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7127 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7128 dpyinfo->mouse_face_window = Qnil;
7132 /* Clear any mouse-face on window W. This function is part of the
7133 redisplay interface, and is called from try_window_id and similar
7134 functions to ensure the mouse-highlight is off. */
7136 static void
7137 x_clear_mouse_face (w)
7138 struct window *w;
7140 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7141 Lisp_Object window;
7143 XSETWINDOW (window, w);
7144 if (EQ (window, dpyinfo->mouse_face_window))
7145 clear_mouse_face (dpyinfo);
7149 /* Just discard the mouse face information for frame F, if any.
7150 This is used when the size of F is changed. */
7152 void
7153 cancel_mouse_face (f)
7154 FRAME_PTR f;
7156 Lisp_Object window;
7157 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7159 window = dpyinfo->mouse_face_window;
7160 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7162 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7163 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7164 dpyinfo->mouse_face_window = Qnil;
7168 static struct scroll_bar *x_window_to_scroll_bar ();
7169 static void x_scroll_bar_report_motion ();
7171 /* Return the current position of the mouse.
7172 *fp should be a frame which indicates which display to ask about.
7174 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7175 and *part to the frame, window, and scroll bar part that the mouse
7176 is over. Set *x and *y to the portion and whole of the mouse's
7177 position on the scroll bar.
7179 If the mouse movement started elsewhere, set *fp to the frame the
7180 mouse is on, *bar_window to nil, and *x and *y to the character cell
7181 the mouse is over.
7183 Set *time to the server time-stamp for the time at which the mouse
7184 was at this position.
7186 Don't store anything if we don't have a valid set of values to report.
7188 This clears the mouse_moved flag, so we can wait for the next mouse
7189 movement. */
7191 static void
7192 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7193 FRAME_PTR *fp;
7194 int insist;
7195 Lisp_Object *bar_window;
7196 enum scroll_bar_part *part;
7197 Lisp_Object *x, *y;
7198 unsigned long *time;
7200 FRAME_PTR f1;
7202 BLOCK_INPUT;
7204 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7205 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7206 else
7208 Window root;
7209 int root_x, root_y;
7211 Window dummy_window;
7212 int dummy;
7214 Lisp_Object frame, tail;
7216 /* Clear the mouse-moved flag for every frame on this display. */
7217 FOR_EACH_FRAME (tail, frame)
7218 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7219 XFRAME (frame)->mouse_moved = 0;
7221 last_mouse_scroll_bar = Qnil;
7223 /* Figure out which root window we're on. */
7224 XQueryPointer (FRAME_X_DISPLAY (*fp),
7225 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7227 /* The root window which contains the pointer. */
7228 &root,
7230 /* Trash which we can't trust if the pointer is on
7231 a different screen. */
7232 &dummy_window,
7234 /* The position on that root window. */
7235 &root_x, &root_y,
7237 /* More trash we can't trust. */
7238 &dummy, &dummy,
7240 /* Modifier keys and pointer buttons, about which
7241 we don't care. */
7242 (unsigned int *) &dummy);
7244 /* Now we have a position on the root; find the innermost window
7245 containing the pointer. */
7247 Window win, child;
7248 int win_x, win_y;
7249 int parent_x = 0, parent_y = 0;
7250 int count;
7252 win = root;
7254 /* XTranslateCoordinates can get errors if the window
7255 structure is changing at the same time this function
7256 is running. So at least we must not crash from them. */
7258 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7260 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7261 && FRAME_LIVE_P (last_mouse_frame))
7263 /* If mouse was grabbed on a frame, give coords for that frame
7264 even if the mouse is now outside it. */
7265 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7267 /* From-window, to-window. */
7268 root, FRAME_X_WINDOW (last_mouse_frame),
7270 /* From-position, to-position. */
7271 root_x, root_y, &win_x, &win_y,
7273 /* Child of win. */
7274 &child);
7275 f1 = last_mouse_frame;
7277 else
7279 while (1)
7281 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7283 /* From-window, to-window. */
7284 root, win,
7286 /* From-position, to-position. */
7287 root_x, root_y, &win_x, &win_y,
7289 /* Child of win. */
7290 &child);
7292 if (child == None || child == win)
7293 break;
7295 win = child;
7296 parent_x = win_x;
7297 parent_y = win_y;
7300 /* Now we know that:
7301 win is the innermost window containing the pointer
7302 (XTC says it has no child containing the pointer),
7303 win_x and win_y are the pointer's position in it
7304 (XTC did this the last time through), and
7305 parent_x and parent_y are the pointer's position in win's parent.
7306 (They are what win_x and win_y were when win was child.
7307 If win is the root window, it has no parent, and
7308 parent_{x,y} are invalid, but that's okay, because we'll
7309 never use them in that case.) */
7311 /* Is win one of our frames? */
7312 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7315 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7316 f1 = 0;
7318 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7320 /* If not, is it one of our scroll bars? */
7321 if (! f1)
7323 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7325 if (bar)
7327 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7328 win_x = parent_x;
7329 win_y = parent_y;
7333 if (f1 == 0 && insist > 0)
7334 f1 = SELECTED_FRAME ();
7336 if (f1)
7338 /* Ok, we found a frame. Store all the values.
7339 last_mouse_glyph is a rectangle used to reduce the
7340 generation of mouse events. To not miss any motion
7341 events, we must divide the frame into rectangles of the
7342 size of the smallest character that could be displayed
7343 on it, i.e. into the same rectangles that matrices on
7344 the frame are divided into. */
7346 #if OLD_REDISPLAY_CODE
7347 int ignore1, ignore2;
7348 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7349 &last_mouse_glyph,
7350 FRAME_X_DISPLAY_INFO (f1)->grabbed
7351 || insist);
7352 #else
7354 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7355 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7356 int x = win_x;
7357 int y = win_y;
7359 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7360 round down even for negative values. */
7361 if (x < 0)
7362 x -= width - 1;
7363 if (y < 0)
7364 y -= height - 1;
7366 last_mouse_glyph.width = width;
7367 last_mouse_glyph.height = height;
7368 last_mouse_glyph.x = (x + width - 1) / width * width;
7369 last_mouse_glyph.y = (y + height - 1) / height * height;
7371 #endif
7373 *bar_window = Qnil;
7374 *part = 0;
7375 *fp = f1;
7376 XSETINT (*x, win_x);
7377 XSETINT (*y, win_y);
7378 *time = last_mouse_movement_time;
7383 UNBLOCK_INPUT;
7387 #ifdef USE_X_TOOLKIT
7389 /* Atimer callback function for TIMER. Called every 0.1s to process
7390 Xt timeouts, if needed. We must avoid calling XtAppPending as
7391 much as possible because that function does an implicit XFlush
7392 that slows us down. */
7394 static void
7395 x_process_timeouts (timer)
7396 struct atimer *timer;
7398 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7400 BLOCK_INPUT;
7401 while (XtAppPending (Xt_app_con) & XtIMTimer)
7402 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7403 UNBLOCK_INPUT;
7407 #endif /* USE_X_TOOLKIT */
7410 /* Scroll bar support. */
7412 /* Given an X window ID, find the struct scroll_bar which manages it.
7413 This can be called in GC, so we have to make sure to strip off mark
7414 bits. */
7416 static struct scroll_bar *
7417 x_window_to_scroll_bar (window_id)
7418 Window window_id;
7420 Lisp_Object tail;
7422 for (tail = Vframe_list;
7423 XGCTYPE (tail) == Lisp_Cons;
7424 tail = XCDR (tail))
7426 Lisp_Object frame, bar, condemned;
7428 frame = XCAR (tail);
7429 /* All elements of Vframe_list should be frames. */
7430 if (! GC_FRAMEP (frame))
7431 abort ();
7433 /* Scan this frame's scroll bar list for a scroll bar with the
7434 right window ID. */
7435 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7436 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7437 /* This trick allows us to search both the ordinary and
7438 condemned scroll bar lists with one loop. */
7439 ! GC_NILP (bar) || (bar = condemned,
7440 condemned = Qnil,
7441 ! GC_NILP (bar));
7442 bar = XSCROLL_BAR (bar)->next)
7443 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7444 return XSCROLL_BAR (bar);
7447 return 0;
7452 /************************************************************************
7453 Toolkit scroll bars
7454 ************************************************************************/
7456 #if USE_TOOLKIT_SCROLL_BARS
7458 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7459 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7460 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7461 struct scroll_bar *));
7462 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7463 int, int, int));
7466 /* Id of action hook installed for scroll bars. */
7468 static XtActionHookId action_hook_id;
7470 /* Lisp window being scrolled. Set when starting to interact with
7471 a toolkit scroll bar, reset to nil when ending the interaction. */
7473 static Lisp_Object window_being_scrolled;
7475 /* Last scroll bar part sent in xm_scroll_callback. */
7477 static int last_scroll_bar_part;
7479 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7480 that movements of 1/20 of the screen size are mapped to up/down. */
7482 static Boolean xaw3d_arrow_scroll;
7484 /* Whether the drag scrolling maintains the mouse at the top of the
7485 thumb. If not, resizing the thumb needs to be done more carefully
7486 to avoid jerkyness. */
7488 static Boolean xaw3d_pick_top;
7491 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7492 bars are used.. The hook is responsible for detecting when
7493 the user ends an interaction with the scroll bar, and generates
7494 a `end-scroll' scroll_bar_click' event if so. */
7496 static void
7497 xt_action_hook (widget, client_data, action_name, event, params,
7498 num_params)
7499 Widget widget;
7500 XtPointer client_data;
7501 String action_name;
7502 XEvent *event;
7503 String *params;
7504 Cardinal *num_params;
7506 int scroll_bar_p;
7507 char *end_action;
7509 #ifdef USE_MOTIF
7510 scroll_bar_p = XmIsScrollBar (widget);
7511 end_action = "Release";
7512 #else /* !USE_MOTIF i.e. use Xaw */
7513 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7514 end_action = "EndScroll";
7515 #endif /* USE_MOTIF */
7517 if (scroll_bar_p
7518 && strcmp (action_name, end_action) == 0
7519 && WINDOWP (window_being_scrolled))
7521 struct window *w;
7523 x_send_scroll_bar_event (window_being_scrolled,
7524 scroll_bar_end_scroll, 0, 0);
7525 w = XWINDOW (window_being_scrolled);
7526 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7527 window_being_scrolled = Qnil;
7528 last_scroll_bar_part = -1;
7530 /* Xt timeouts no longer needed. */
7531 toolkit_scroll_bar_interaction = 0;
7536 /* Send a client message with message type Xatom_Scrollbar for a
7537 scroll action to the frame of WINDOW. PART is a value identifying
7538 the part of the scroll bar that was clicked on. PORTION is the
7539 amount to scroll of a whole of WHOLE. */
7541 static void
7542 x_send_scroll_bar_event (window, part, portion, whole)
7543 Lisp_Object window;
7544 int part, portion, whole;
7546 XEvent event;
7547 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7548 struct frame *f = XFRAME (XWINDOW (window)->frame);
7550 /* Construct a ClientMessage event to send to the frame. */
7551 ev->type = ClientMessage;
7552 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7553 ev->display = FRAME_X_DISPLAY (f);
7554 ev->window = FRAME_X_WINDOW (f);
7555 ev->format = 32;
7556 ev->data.l[0] = (long) XFASTINT (window);
7557 ev->data.l[1] = (long) part;
7558 ev->data.l[2] = (long) 0;
7559 ev->data.l[3] = (long) portion;
7560 ev->data.l[4] = (long) whole;
7562 /* Make Xt timeouts work while the scroll bar is active. */
7563 toolkit_scroll_bar_interaction = 1;
7565 /* Setting the event mask to zero means that the message will
7566 be sent to the client that created the window, and if that
7567 window no longer exists, no event will be sent. */
7568 BLOCK_INPUT;
7569 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7570 UNBLOCK_INPUT;
7574 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7575 in *IEVENT. */
7577 static void
7578 x_scroll_bar_to_input_event (event, ievent)
7579 XEvent *event;
7580 struct input_event *ievent;
7582 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7583 Lisp_Object window;
7584 struct frame *f;
7586 XSETFASTINT (window, ev->data.l[0]);
7587 f = XFRAME (XWINDOW (window)->frame);
7589 ievent->kind = scroll_bar_click;
7590 ievent->frame_or_window = window;
7591 ievent->arg = Qnil;
7592 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7593 ievent->part = ev->data.l[1];
7594 ievent->code = ev->data.l[2];
7595 ievent->x = make_number ((int) ev->data.l[3]);
7596 ievent->y = make_number ((int) ev->data.l[4]);
7597 ievent->modifiers = 0;
7601 #ifdef USE_MOTIF
7603 /* Minimum and maximum values used for Motif scroll bars. */
7605 #define XM_SB_MIN 1
7606 #define XM_SB_MAX 10000000
7607 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7610 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7611 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7612 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7614 static void
7615 xm_scroll_callback (widget, client_data, call_data)
7616 Widget widget;
7617 XtPointer client_data, call_data;
7619 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7620 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7621 double percent;
7622 int part = -1, whole = 0, portion = 0;
7624 switch (cs->reason)
7626 case XmCR_DECREMENT:
7627 bar->dragging = Qnil;
7628 part = scroll_bar_up_arrow;
7629 break;
7631 case XmCR_INCREMENT:
7632 bar->dragging = Qnil;
7633 part = scroll_bar_down_arrow;
7634 break;
7636 case XmCR_PAGE_DECREMENT:
7637 bar->dragging = Qnil;
7638 part = scroll_bar_above_handle;
7639 break;
7641 case XmCR_PAGE_INCREMENT:
7642 bar->dragging = Qnil;
7643 part = scroll_bar_below_handle;
7644 break;
7646 case XmCR_TO_TOP:
7647 bar->dragging = Qnil;
7648 part = scroll_bar_to_top;
7649 break;
7651 case XmCR_TO_BOTTOM:
7652 bar->dragging = Qnil;
7653 part = scroll_bar_to_bottom;
7654 break;
7656 case XmCR_DRAG:
7658 int slider_size;
7659 int dragging_down_p = (INTEGERP (bar->dragging)
7660 && XINT (bar->dragging) <= cs->value);
7662 /* Get the slider size. */
7663 BLOCK_INPUT;
7664 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7665 UNBLOCK_INPUT;
7667 /* At the max position of the scroll bar, do a line-wise
7668 movement. Without doing anything, the LessTif scroll bar
7669 calls us with the same cs->value again and again. If we
7670 want to make sure that we can reach the end of the buffer,
7671 we have to do something.
7673 Implementation note: setting bar->dragging always to
7674 cs->value gives a smoother movement at the max position.
7675 Setting it to nil when doing line-wise movement gives
7676 a better slider behavior. */
7678 if (cs->value + slider_size == XM_SB_MAX
7679 || (dragging_down_p
7680 && last_scroll_bar_part == scroll_bar_down_arrow))
7682 part = scroll_bar_down_arrow;
7683 bar->dragging = Qnil;
7685 else
7687 whole = XM_SB_RANGE;
7688 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7689 part = scroll_bar_handle;
7690 bar->dragging = make_number (cs->value);
7693 break;
7695 case XmCR_VALUE_CHANGED:
7696 break;
7699 if (part >= 0)
7701 window_being_scrolled = bar->window;
7702 last_scroll_bar_part = part;
7703 x_send_scroll_bar_event (bar->window, part, portion, whole);
7708 #else /* !USE_MOTIF, i.e. Xaw. */
7711 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7712 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7713 scroll bar struct. CALL_DATA is a pointer to a float saying where
7714 the thumb is. */
7716 static void
7717 xaw_jump_callback (widget, client_data, call_data)
7718 Widget widget;
7719 XtPointer client_data, call_data;
7721 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7722 float top = *(float *) call_data;
7723 float shown;
7724 int whole, portion, height;
7725 int part;
7727 /* Get the size of the thumb, a value between 0 and 1. */
7728 BLOCK_INPUT;
7729 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7730 UNBLOCK_INPUT;
7732 whole = 10000000;
7733 portion = shown < 1 ? top * whole : 0;
7735 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7736 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7737 the bottom, so we force the scrolling whenever we see that we're
7738 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7739 we try to ensure that we always stay two pixels away from the
7740 bottom). */
7741 part = scroll_bar_down_arrow;
7742 else
7743 part = scroll_bar_handle;
7745 window_being_scrolled = bar->window;
7746 bar->dragging = make_number (portion);
7747 last_scroll_bar_part = part;
7748 x_send_scroll_bar_event (bar->window, part, portion, whole);
7752 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7753 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7754 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7755 the scroll bar. CALL_DATA is an integer specifying the action that
7756 has taken place. It's magnitude is in the range 0..height of the
7757 scroll bar. Negative values mean scroll towards buffer start.
7758 Values < height of scroll bar mean line-wise movement. */
7760 static void
7761 xaw_scroll_callback (widget, client_data, call_data)
7762 Widget widget;
7763 XtPointer client_data, call_data;
7765 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7766 int position = (int) call_data;
7767 Dimension height;
7768 int part;
7770 /* Get the height of the scroll bar. */
7771 BLOCK_INPUT;
7772 XtVaGetValues (widget, XtNheight, &height, NULL);
7773 UNBLOCK_INPUT;
7775 if (abs (position) >= height)
7776 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7778 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7779 it maps line-movement to call_data = max(5, height/20). */
7780 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7781 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7782 else
7783 part = scroll_bar_move_ratio;
7785 window_being_scrolled = bar->window;
7786 bar->dragging = Qnil;
7787 last_scroll_bar_part = part;
7788 x_send_scroll_bar_event (bar->window, part, position, height);
7792 #endif /* not USE_MOTIF */
7795 /* Create the widget for scroll bar BAR on frame F. Record the widget
7796 and X window of the scroll bar in BAR. */
7798 static void
7799 x_create_toolkit_scroll_bar (f, bar)
7800 struct frame *f;
7801 struct scroll_bar *bar;
7803 Window xwindow;
7804 Widget widget;
7805 Arg av[20];
7806 int ac = 0;
7807 char *scroll_bar_name = "verticalScrollBar";
7808 unsigned long pixel;
7810 BLOCK_INPUT;
7812 #ifdef USE_MOTIF
7813 /* LessTif 0.85, problems:
7815 1. When the mouse if over the scroll bar, the scroll bar will
7816 get keyboard events. I didn't find a way to turn this off.
7818 2. Do we have to explicitly set the cursor to get an arrow
7819 cursor (see below)? */
7821 /* Set resources. Create the widget. */
7822 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7823 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
7824 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
7825 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
7826 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
7827 XtSetArg (av[ac], XmNincrement, 1); ++ac;
7828 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
7830 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7831 if (pixel != -1)
7833 XtSetArg (av[ac], XmNforeground, pixel);
7834 ++ac;
7837 pixel = f->output_data.x->scroll_bar_background_pixel;
7838 if (pixel != -1)
7840 XtSetArg (av[ac], XmNbackground, pixel);
7841 ++ac;
7844 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
7845 scroll_bar_name, av, ac);
7847 /* Add one callback for everything that can happen. */
7848 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
7849 (XtPointer) bar);
7850 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
7851 (XtPointer) bar);
7852 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
7853 (XtPointer) bar);
7854 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
7855 (XtPointer) bar);
7856 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
7857 (XtPointer) bar);
7858 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
7859 (XtPointer) bar);
7860 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
7861 (XtPointer) bar);
7863 /* Realize the widget. Only after that is the X window created. */
7864 XtRealizeWidget (widget);
7866 /* Set the cursor to an arrow. I didn't find a resource to do that.
7867 And I'm wondering why it hasn't an arrow cursor by default. */
7868 XDefineCursor (XtDisplay (widget), XtWindow (widget),
7869 f->output_data.x->nontext_cursor);
7871 #else /* !USE_MOTIF i.e. use Xaw */
7873 /* Set resources. Create the widget. The background of the
7874 Xaw3d scroll bar widget is a little bit light for my taste.
7875 We don't alter it here to let users change it according
7876 to their taste with `emacs*verticalScrollBar.background: xxx'. */
7877 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
7878 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
7879 /* For smoother scrolling with Xaw3d -sm */
7880 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
7881 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
7883 pixel = f->output_data.x->scroll_bar_foreground_pixel;
7884 if (pixel != -1)
7886 XtSetArg (av[ac], XtNforeground, pixel);
7887 ++ac;
7890 pixel = f->output_data.x->scroll_bar_background_pixel;
7891 if (pixel != -1)
7893 XtSetArg (av[ac], XtNbackground, pixel);
7894 ++ac;
7897 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
7898 f->output_data.x->edit_widget, av, ac);
7901 char *initial = "";
7902 char *val = initial;
7903 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
7904 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
7905 if (val == initial)
7906 { /* ARROW_SCROLL */
7907 xaw3d_arrow_scroll = True;
7908 /* Isn't that just a personal preference ? -sm */
7909 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
7913 /* Define callbacks. */
7914 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
7915 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
7916 (XtPointer) bar);
7918 /* Realize the widget. Only after that is the X window created. */
7919 XtRealizeWidget (widget);
7921 #endif /* !USE_MOTIF */
7923 /* Install an action hook that let's us detect when the user
7924 finishes interacting with a scroll bar. */
7925 if (action_hook_id == 0)
7926 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
7928 /* Remember X window and widget in the scroll bar vector. */
7929 SET_SCROLL_BAR_X_WIDGET (bar, widget);
7930 xwindow = XtWindow (widget);
7931 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
7933 UNBLOCK_INPUT;
7937 /* Set the thumb size and position of scroll bar BAR. We are currently
7938 displaying PORTION out of a whole WHOLE, and our position POSITION. */
7940 static void
7941 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
7942 struct scroll_bar *bar;
7943 int portion, position, whole;
7945 float top, shown;
7946 Widget widget = SCROLL_BAR_X_WIDGET (bar);
7948 if (whole == 0)
7949 top = 0, shown = 1;
7950 else
7952 top = (float) position / whole;
7953 shown = (float) portion / whole;
7956 BLOCK_INPUT;
7958 #ifdef USE_MOTIF
7960 int size, value;
7961 Boolean arrow1_selected, arrow2_selected;
7962 unsigned char flags;
7963 XmScrollBarWidget sb;
7965 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
7966 is the scroll bar's maximum and MIN is the scroll bar's minimum
7967 value. */
7968 size = shown * XM_SB_RANGE;
7969 size = min (size, XM_SB_RANGE);
7970 size = max (size, 1);
7972 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
7973 value = top * XM_SB_RANGE;
7974 value = min (value, XM_SB_MAX - size);
7975 value = max (value, XM_SB_MIN);
7977 /* LessTif: Calling XmScrollBarSetValues after an increment or
7978 decrement turns off auto-repeat LessTif-internally. This can
7979 be seen in ScrollBar.c which resets Arrow1Selected and
7980 Arrow2Selected. It also sets internal flags so that LessTif
7981 believes the mouse is in the slider. We either have to change
7982 our code, or work around that by accessing private data. */
7984 sb = (XmScrollBarWidget) widget;
7985 arrow1_selected = sb->scrollBar.arrow1_selected;
7986 arrow2_selected = sb->scrollBar.arrow2_selected;
7987 flags = sb->scrollBar.flags;
7989 if (NILP (bar->dragging))
7990 XmScrollBarSetValues (widget, value, size, 0, 0, False);
7991 else if (last_scroll_bar_part == scroll_bar_down_arrow)
7992 /* This has the negative side effect that the slider value is
7993 not what it would be if we scrolled here using line-wise or
7994 page-wise movement. */
7995 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
7996 else
7998 /* If currently dragging, only update the slider size.
7999 This reduces flicker effects. */
8000 int old_value, old_size, increment, page_increment;
8002 XmScrollBarGetValues (widget, &old_value, &old_size,
8003 &increment, &page_increment);
8004 XmScrollBarSetValues (widget, old_value,
8005 min (size, XM_SB_RANGE - old_value),
8006 0, 0, False);
8009 sb->scrollBar.arrow1_selected = arrow1_selected;
8010 sb->scrollBar.arrow2_selected = arrow2_selected;
8011 sb->scrollBar.flags = flags;
8013 #else /* !USE_MOTIF i.e. use Xaw */
8015 float old_top, old_shown;
8016 Dimension height;
8017 XtVaGetValues (widget,
8018 XtNtopOfThumb, &old_top,
8019 XtNshown, &old_shown,
8020 XtNheight, &height,
8021 NULL);
8023 /* Massage the top+shown values. */
8024 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8025 top = max (0, min (1, top));
8026 else
8027 top = old_top;
8028 /* Keep two pixels available for moving the thumb down. */
8029 shown = max (0, min (1 - top - (2.0 / height), shown));
8031 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8032 check that your system's configuration file contains a define
8033 for `NARROWPROTO'. See s/freebsd.h for an example. */
8034 if (top != old_top || shown != old_shown)
8036 if (NILP (bar->dragging))
8037 XawScrollbarSetThumb (widget, top, shown);
8038 else
8040 #ifdef HAVE_XAW3D
8041 ScrollbarWidget sb = (ScrollbarWidget) widget;
8042 int scroll_mode = 0;
8044 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8045 if (xaw3d_arrow_scroll)
8047 /* Xaw3d stupidly ignores resize requests while dragging
8048 so we have to make it believe it's not in dragging mode. */
8049 scroll_mode = sb->scrollbar.scroll_mode;
8050 if (scroll_mode == 2)
8051 sb->scrollbar.scroll_mode = 0;
8053 #endif
8054 /* Try to make the scrolling a tad smoother. */
8055 if (!xaw3d_pick_top)
8056 shown = min (shown, old_shown);
8058 XawScrollbarSetThumb (widget, top, shown);
8060 #ifdef HAVE_XAW3D
8061 if (xaw3d_arrow_scroll && scroll_mode == 2)
8062 sb->scrollbar.scroll_mode = scroll_mode;
8063 #endif
8067 #endif /* !USE_MOTIF */
8069 UNBLOCK_INPUT;
8072 #endif /* USE_TOOLKIT_SCROLL_BARS */
8076 /************************************************************************
8077 Scroll bars, general
8078 ************************************************************************/
8080 /* Create a scroll bar and return the scroll bar vector for it. W is
8081 the Emacs window on which to create the scroll bar. TOP, LEFT,
8082 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8083 scroll bar. */
8085 static struct scroll_bar *
8086 x_scroll_bar_create (w, top, left, width, height)
8087 struct window *w;
8088 int top, left, width, height;
8090 struct frame *f = XFRAME (w->frame);
8091 struct scroll_bar *bar
8092 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8094 BLOCK_INPUT;
8096 #if USE_TOOLKIT_SCROLL_BARS
8097 x_create_toolkit_scroll_bar (f, bar);
8098 #else /* not USE_TOOLKIT_SCROLL_BARS */
8100 XSetWindowAttributes a;
8101 unsigned long mask;
8102 Window window;
8104 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8105 if (a.background_pixel == -1)
8106 a.background_pixel = f->output_data.x->background_pixel;
8108 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8109 | ButtonMotionMask | PointerMotionHintMask
8110 | ExposureMask);
8111 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8113 mask = (CWBackPixel | CWEventMask | CWCursor);
8115 /* Clear the area of W that will serve as a scroll bar. This is
8116 for the case that a window has been split horizontally. In
8117 this case, no clear_frame is generated to reduce flickering. */
8118 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8119 left, top, width,
8120 window_box_height (w), False);
8122 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8123 /* Position and size of scroll bar. */
8124 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8125 top,
8126 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8127 height,
8128 /* Border width, depth, class, and visual. */
8130 CopyFromParent,
8131 CopyFromParent,
8132 CopyFromParent,
8133 /* Attributes. */
8134 mask, &a);
8135 SET_SCROLL_BAR_X_WINDOW (bar, window);
8137 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8139 XSETWINDOW (bar->window, w);
8140 XSETINT (bar->top, top);
8141 XSETINT (bar->left, left);
8142 XSETINT (bar->width, width);
8143 XSETINT (bar->height, height);
8144 XSETINT (bar->start, 0);
8145 XSETINT (bar->end, 0);
8146 bar->dragging = Qnil;
8148 /* Add bar to its frame's list of scroll bars. */
8149 bar->next = FRAME_SCROLL_BARS (f);
8150 bar->prev = Qnil;
8151 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8152 if (!NILP (bar->next))
8153 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8155 /* Map the window/widget. */
8156 #if USE_TOOLKIT_SCROLL_BARS
8157 XtMapWidget (SCROLL_BAR_X_WIDGET (bar));
8158 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8159 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8160 top,
8161 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8162 height, 0);
8163 #else /* not USE_TOOLKIT_SCROLL_BARS */
8164 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8165 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8167 UNBLOCK_INPUT;
8168 return bar;
8172 /* Draw BAR's handle in the proper position.
8174 If the handle is already drawn from START to END, don't bother
8175 redrawing it, unless REBUILD is non-zero; in that case, always
8176 redraw it. (REBUILD is handy for drawing the handle after expose
8177 events.)
8179 Normally, we want to constrain the start and end of the handle to
8180 fit inside its rectangle, but if the user is dragging the scroll
8181 bar handle, we want to let them drag it down all the way, so that
8182 the bar's top is as far down as it goes; otherwise, there's no way
8183 to move to the very end of the buffer. */
8185 #ifndef USE_TOOLKIT_SCROLL_BARS
8187 static void
8188 x_scroll_bar_set_handle (bar, start, end, rebuild)
8189 struct scroll_bar *bar;
8190 int start, end;
8191 int rebuild;
8193 int dragging = ! NILP (bar->dragging);
8194 Window w = SCROLL_BAR_X_WINDOW (bar);
8195 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8196 GC gc = f->output_data.x->normal_gc;
8198 /* If the display is already accurate, do nothing. */
8199 if (! rebuild
8200 && start == XINT (bar->start)
8201 && end == XINT (bar->end))
8202 return;
8204 BLOCK_INPUT;
8207 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8208 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8209 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8211 /* Make sure the values are reasonable, and try to preserve
8212 the distance between start and end. */
8214 int length = end - start;
8216 if (start < 0)
8217 start = 0;
8218 else if (start > top_range)
8219 start = top_range;
8220 end = start + length;
8222 if (end < start)
8223 end = start;
8224 else if (end > top_range && ! dragging)
8225 end = top_range;
8228 /* Store the adjusted setting in the scroll bar. */
8229 XSETINT (bar->start, start);
8230 XSETINT (bar->end, end);
8232 /* Clip the end position, just for display. */
8233 if (end > top_range)
8234 end = top_range;
8236 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8237 below top positions, to make sure the handle is always at least
8238 that many pixels tall. */
8239 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8241 /* Draw the empty space above the handle. Note that we can't clear
8242 zero-height areas; that means "clear to end of window." */
8243 if (0 < start)
8244 XClearArea (FRAME_X_DISPLAY (f), w,
8246 /* x, y, width, height, and exposures. */
8247 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8248 VERTICAL_SCROLL_BAR_TOP_BORDER,
8249 inside_width, start,
8250 False);
8252 /* Change to proper foreground color if one is specified. */
8253 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8254 XSetForeground (FRAME_X_DISPLAY (f), gc,
8255 f->output_data.x->scroll_bar_foreground_pixel);
8257 /* Draw the handle itself. */
8258 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8260 /* x, y, width, height */
8261 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8262 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8263 inside_width, end - start);
8265 /* Restore the foreground color of the GC if we changed it above. */
8266 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8267 XSetForeground (FRAME_X_DISPLAY (f), gc,
8268 f->output_data.x->foreground_pixel);
8270 /* Draw the empty space below the handle. Note that we can't
8271 clear zero-height areas; that means "clear to end of window." */
8272 if (end < inside_height)
8273 XClearArea (FRAME_X_DISPLAY (f), w,
8275 /* x, y, width, height, and exposures. */
8276 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8277 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8278 inside_width, inside_height - end,
8279 False);
8283 UNBLOCK_INPUT;
8286 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8288 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8289 nil. */
8291 static void
8292 x_scroll_bar_remove (bar)
8293 struct scroll_bar *bar;
8295 BLOCK_INPUT;
8297 #if USE_TOOLKIT_SCROLL_BARS
8298 XtDestroyWidget (SCROLL_BAR_X_WIDGET (bar));
8299 #else /* not USE_TOOLKIT_SCROLL_BARS */
8301 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8302 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8304 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8306 /* Disassociate this scroll bar from its window. */
8307 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8309 UNBLOCK_INPUT;
8313 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8314 that we are displaying PORTION characters out of a total of WHOLE
8315 characters, starting at POSITION. If WINDOW has no scroll bar,
8316 create one. */
8318 static void
8319 XTset_vertical_scroll_bar (w, portion, whole, position)
8320 struct window *w;
8321 int portion, whole, position;
8323 struct frame *f = XFRAME (w->frame);
8324 struct scroll_bar *bar;
8325 int top, height, left, sb_left, width, sb_width;
8326 int window_x, window_y, window_width, window_height;
8328 /* Get window dimensions. */
8329 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8330 top = window_y;
8331 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8332 height = window_height;
8334 /* Compute the left edge of the scroll bar area. */
8335 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8336 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8337 else
8338 left = XFASTINT (w->left);
8339 left *= CANON_X_UNIT (f);
8340 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8342 /* Compute the width of the scroll bar which might be less than
8343 the width of the area reserved for the scroll bar. */
8344 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8345 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8346 else
8347 sb_width = width;
8349 /* Compute the left edge of the scroll bar. */
8350 #ifdef USE_TOOLKIT_SCROLL_BARS
8351 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8352 sb_left = left + width - sb_width - (width - sb_width) / 2;
8353 else
8354 sb_left = left + (width - sb_width) / 2;
8355 #else
8356 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8357 sb_left = left + width - sb_width;
8358 else
8359 sb_left = left;
8360 #endif
8362 /* Does the scroll bar exist yet? */
8363 if (NILP (w->vertical_scroll_bar))
8365 BLOCK_INPUT;
8366 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8367 left, top, width, height, False);
8368 UNBLOCK_INPUT;
8369 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8371 else
8373 /* It may just need to be moved and resized. */
8374 unsigned int mask = 0;
8376 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8378 BLOCK_INPUT;
8380 if (sb_left != XINT (bar->left))
8381 mask |= CWX;
8382 if (top != XINT (bar->top))
8383 mask |= CWY;
8384 if (sb_width != XINT (bar->width))
8385 mask |= CWWidth;
8386 if (height != XINT (bar->height))
8387 mask |= CWHeight;
8389 #ifdef USE_TOOLKIT_SCROLL_BARS
8391 /* Since toolkit scroll bars are smaller than the space reserved
8392 for them on the frame, we have to clear "under" them. */
8393 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8394 left, top, width, height, False);
8396 /* Move/size the scroll bar widget. */
8397 if (mask)
8398 XtConfigureWidget (SCROLL_BAR_X_WIDGET (bar),
8399 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8400 top,
8401 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8402 height, 0);
8404 #else /* not USE_TOOLKIT_SCROLL_BARS */
8406 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8408 /* Clear areas not covered by the scroll bar. This makes sure a
8409 previous mode line display is cleared after C-x 2 C-x 1, for
8410 example. Non-toolkit scroll bars are as wide as the area
8411 reserved for scroll bars - trim at both sides. */
8412 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8413 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8414 height, False);
8415 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8416 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8417 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8418 height, False);
8421 /* Move/size the scroll bar window. */
8422 if (mask)
8424 XWindowChanges wc;
8426 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8427 wc.y = top;
8428 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8429 wc.height = height;
8430 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8431 mask, &wc);
8434 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8436 /* Remember new settings. */
8437 XSETINT (bar->left, sb_left);
8438 XSETINT (bar->top, top);
8439 XSETINT (bar->width, sb_width);
8440 XSETINT (bar->height, height);
8442 UNBLOCK_INPUT;
8445 #if USE_TOOLKIT_SCROLL_BARS
8446 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8447 #else /* not USE_TOOLKIT_SCROLL_BARS */
8448 /* Set the scroll bar's current state, unless we're currently being
8449 dragged. */
8450 if (NILP (bar->dragging))
8452 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8454 if (whole == 0)
8455 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8456 else
8458 int start = ((double) position * top_range) / whole;
8459 int end = ((double) (position + portion) * top_range) / whole;
8460 x_scroll_bar_set_handle (bar, start, end, 0);
8463 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8465 XSETVECTOR (w->vertical_scroll_bar, bar);
8469 /* The following three hooks are used when we're doing a thorough
8470 redisplay of the frame. We don't explicitly know which scroll bars
8471 are going to be deleted, because keeping track of when windows go
8472 away is a real pain - "Can you say set-window-configuration, boys
8473 and girls?" Instead, we just assert at the beginning of redisplay
8474 that *all* scroll bars are to be removed, and then save a scroll bar
8475 from the fiery pit when we actually redisplay its window. */
8477 /* Arrange for all scroll bars on FRAME to be removed at the next call
8478 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8479 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8481 static void
8482 XTcondemn_scroll_bars (frame)
8483 FRAME_PTR frame;
8485 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8486 while (! NILP (FRAME_SCROLL_BARS (frame)))
8488 Lisp_Object bar;
8489 bar = FRAME_SCROLL_BARS (frame);
8490 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8491 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8492 XSCROLL_BAR (bar)->prev = Qnil;
8493 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8494 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8495 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8499 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8500 Note that WINDOW isn't necessarily condemned at all. */
8501 static void
8502 XTredeem_scroll_bar (window)
8503 struct window *window;
8505 struct scroll_bar *bar;
8507 /* We can't redeem this window's scroll bar if it doesn't have one. */
8508 if (NILP (window->vertical_scroll_bar))
8509 abort ();
8511 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8513 /* Unlink it from the condemned list. */
8515 FRAME_PTR f = XFRAME (WINDOW_FRAME (window));
8517 if (NILP (bar->prev))
8519 /* If the prev pointer is nil, it must be the first in one of
8520 the lists. */
8521 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8522 /* It's not condemned. Everything's fine. */
8523 return;
8524 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8525 window->vertical_scroll_bar))
8526 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8527 else
8528 /* If its prev pointer is nil, it must be at the front of
8529 one or the other! */
8530 abort ();
8532 else
8533 XSCROLL_BAR (bar->prev)->next = bar->next;
8535 if (! NILP (bar->next))
8536 XSCROLL_BAR (bar->next)->prev = bar->prev;
8538 bar->next = FRAME_SCROLL_BARS (f);
8539 bar->prev = Qnil;
8540 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8541 if (! NILP (bar->next))
8542 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8546 /* Remove all scroll bars on FRAME that haven't been saved since the
8547 last call to `*condemn_scroll_bars_hook'. */
8549 static void
8550 XTjudge_scroll_bars (f)
8551 FRAME_PTR f;
8553 Lisp_Object bar, next;
8555 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8557 /* Clear out the condemned list now so we won't try to process any
8558 more events on the hapless scroll bars. */
8559 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8561 for (; ! NILP (bar); bar = next)
8563 struct scroll_bar *b = XSCROLL_BAR (bar);
8565 x_scroll_bar_remove (b);
8567 next = b->next;
8568 b->next = b->prev = Qnil;
8571 /* Now there should be no references to the condemned scroll bars,
8572 and they should get garbage-collected. */
8576 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8577 is a no-op when using toolkit scroll bars.
8579 This may be called from a signal handler, so we have to ignore GC
8580 mark bits. */
8582 static void
8583 x_scroll_bar_expose (bar, event)
8584 struct scroll_bar *bar;
8585 XEvent *event;
8587 #ifndef USE_TOOLKIT_SCROLL_BARS
8589 Window w = SCROLL_BAR_X_WINDOW (bar);
8590 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8591 GC gc = f->output_data.x->normal_gc;
8592 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8594 BLOCK_INPUT;
8596 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8598 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8599 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8601 /* x, y, width, height */
8602 0, 0,
8603 XINT (bar->width) - 1 - width_trim - width_trim,
8604 XINT (bar->height) - 1);
8606 UNBLOCK_INPUT;
8608 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8611 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8612 is set to something other than no_event, it is enqueued.
8614 This may be called from a signal handler, so we have to ignore GC
8615 mark bits. */
8617 #ifndef USE_TOOLKIT_SCROLL_BARS
8619 static void
8620 x_scroll_bar_handle_click (bar, event, emacs_event)
8621 struct scroll_bar *bar;
8622 XEvent *event;
8623 struct input_event *emacs_event;
8625 if (! GC_WINDOWP (bar->window))
8626 abort ();
8628 emacs_event->kind = scroll_bar_click;
8629 emacs_event->code = event->xbutton.button - Button1;
8630 emacs_event->modifiers
8631 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8632 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8633 event->xbutton.state)
8634 | (event->type == ButtonRelease
8635 ? up_modifier
8636 : down_modifier));
8637 emacs_event->frame_or_window = bar->window;
8638 emacs_event->arg = Qnil;
8639 emacs_event->timestamp = event->xbutton.time;
8641 #if 0
8642 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8643 int internal_height
8644 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8645 #endif
8646 int top_range
8647 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8648 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8650 if (y < 0) y = 0;
8651 if (y > top_range) y = top_range;
8653 if (y < XINT (bar->start))
8654 emacs_event->part = scroll_bar_above_handle;
8655 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8656 emacs_event->part = scroll_bar_handle;
8657 else
8658 emacs_event->part = scroll_bar_below_handle;
8660 /* Just because the user has clicked on the handle doesn't mean
8661 they want to drag it. Lisp code needs to be able to decide
8662 whether or not we're dragging. */
8663 #if 0
8664 /* If the user has just clicked on the handle, record where they're
8665 holding it. */
8666 if (event->type == ButtonPress
8667 && emacs_event->part == scroll_bar_handle)
8668 XSETINT (bar->dragging, y - XINT (bar->start));
8669 #endif
8671 /* If the user has released the handle, set it to its final position. */
8672 if (event->type == ButtonRelease
8673 && ! NILP (bar->dragging))
8675 int new_start = y - XINT (bar->dragging);
8676 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8678 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8679 bar->dragging = Qnil;
8682 /* Same deal here as the other #if 0. */
8683 #if 0
8684 /* Clicks on the handle are always reported as occurring at the top of
8685 the handle. */
8686 if (emacs_event->part == scroll_bar_handle)
8687 emacs_event->x = bar->start;
8688 else
8689 XSETINT (emacs_event->x, y);
8690 #else
8691 XSETINT (emacs_event->x, y);
8692 #endif
8694 XSETINT (emacs_event->y, top_range);
8698 /* Handle some mouse motion while someone is dragging the scroll bar.
8700 This may be called from a signal handler, so we have to ignore GC
8701 mark bits. */
8703 static void
8704 x_scroll_bar_note_movement (bar, event)
8705 struct scroll_bar *bar;
8706 XEvent *event;
8708 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8710 last_mouse_movement_time = event->xmotion.time;
8712 f->mouse_moved = 1;
8713 XSETVECTOR (last_mouse_scroll_bar, bar);
8715 /* If we're dragging the bar, display it. */
8716 if (! GC_NILP (bar->dragging))
8718 /* Where should the handle be now? */
8719 int new_start = event->xmotion.y - XINT (bar->dragging);
8721 if (new_start != XINT (bar->start))
8723 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8725 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8730 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8732 /* Return information to the user about the current position of the mouse
8733 on the scroll bar. */
8735 static void
8736 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8737 FRAME_PTR *fp;
8738 Lisp_Object *bar_window;
8739 enum scroll_bar_part *part;
8740 Lisp_Object *x, *y;
8741 unsigned long *time;
8743 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8744 Window w = SCROLL_BAR_X_WINDOW (bar);
8745 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8746 int win_x, win_y;
8747 Window dummy_window;
8748 int dummy_coord;
8749 unsigned int dummy_mask;
8751 BLOCK_INPUT;
8753 /* Get the mouse's position relative to the scroll bar window, and
8754 report that. */
8755 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8757 /* Root, child, root x and root y. */
8758 &dummy_window, &dummy_window,
8759 &dummy_coord, &dummy_coord,
8761 /* Position relative to scroll bar. */
8762 &win_x, &win_y,
8764 /* Mouse buttons and modifier keys. */
8765 &dummy_mask))
8767 else
8769 #if 0
8770 int inside_height
8771 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8772 #endif
8773 int top_range
8774 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8776 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8778 if (! NILP (bar->dragging))
8779 win_y -= XINT (bar->dragging);
8781 if (win_y < 0)
8782 win_y = 0;
8783 if (win_y > top_range)
8784 win_y = top_range;
8786 *fp = f;
8787 *bar_window = bar->window;
8789 if (! NILP (bar->dragging))
8790 *part = scroll_bar_handle;
8791 else if (win_y < XINT (bar->start))
8792 *part = scroll_bar_above_handle;
8793 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8794 *part = scroll_bar_handle;
8795 else
8796 *part = scroll_bar_below_handle;
8798 XSETINT (*x, win_y);
8799 XSETINT (*y, top_range);
8801 f->mouse_moved = 0;
8802 last_mouse_scroll_bar = Qnil;
8805 *time = last_mouse_movement_time;
8807 UNBLOCK_INPUT;
8811 /* The screen has been cleared so we may have changed foreground or
8812 background colors, and the scroll bars may need to be redrawn.
8813 Clear out the scroll bars, and ask for expose events, so we can
8814 redraw them. */
8816 void
8817 x_scroll_bar_clear (f)
8818 FRAME_PTR f;
8820 #ifndef USE_TOOLKIT_SCROLL_BARS
8821 Lisp_Object bar;
8823 /* We can have scroll bars even if this is 0,
8824 if we just turned off scroll bar mode.
8825 But in that case we should not clear them. */
8826 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
8827 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
8828 bar = XSCROLL_BAR (bar)->next)
8829 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
8830 0, 0, 0, 0, True);
8831 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8834 /* This processes Expose events from the menu-bar specific X event
8835 loop in xmenu.c. This allows to redisplay the frame if necessary
8836 when handling menu-bar or pop-up items. */
8839 process_expose_from_menu (event)
8840 XEvent event;
8842 FRAME_PTR f;
8843 struct x_display_info *dpyinfo;
8844 int frame_exposed_p = 0;
8846 BLOCK_INPUT;
8848 dpyinfo = x_display_info_for_display (event.xexpose.display);
8849 f = x_window_to_frame (dpyinfo, event.xexpose.window);
8850 if (f)
8852 if (f->async_visible == 0)
8854 f->async_visible = 1;
8855 f->async_iconified = 0;
8856 f->output_data.x->has_been_visible = 1;
8857 SET_FRAME_GARBAGED (f);
8859 else
8861 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
8862 event.xexpose.x, event.xexpose.y,
8863 event.xexpose.width, event.xexpose.height);
8864 frame_exposed_p = 1;
8867 else
8869 struct scroll_bar *bar
8870 = x_window_to_scroll_bar (event.xexpose.window);
8872 if (bar)
8873 x_scroll_bar_expose (bar, &event);
8876 UNBLOCK_INPUT;
8877 return frame_exposed_p;
8880 /* Define a queue to save up SelectionRequest events for later handling. */
8882 struct selection_event_queue
8884 XEvent event;
8885 struct selection_event_queue *next;
8888 static struct selection_event_queue *queue;
8890 /* Nonzero means queue up certain events--don't process them yet. */
8892 static int x_queue_selection_requests;
8894 /* Queue up an X event *EVENT, to be processed later. */
8896 static void
8897 x_queue_event (f, event)
8898 FRAME_PTR f;
8899 XEvent *event;
8901 struct selection_event_queue *queue_tmp
8902 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
8904 if (queue_tmp != NULL)
8906 queue_tmp->event = *event;
8907 queue_tmp->next = queue;
8908 queue = queue_tmp;
8912 /* Take all the queued events and put them back
8913 so that they get processed afresh. */
8915 static void
8916 x_unqueue_events (display)
8917 Display *display;
8919 while (queue != NULL)
8921 struct selection_event_queue *queue_tmp = queue;
8922 XPutBackEvent (display, &queue_tmp->event);
8923 queue = queue_tmp->next;
8924 xfree ((char *)queue_tmp);
8928 /* Start queuing SelectionRequest events. */
8930 void
8931 x_start_queuing_selection_requests (display)
8932 Display *display;
8934 x_queue_selection_requests++;
8937 /* Stop queuing SelectionRequest events. */
8939 void
8940 x_stop_queuing_selection_requests (display)
8941 Display *display;
8943 x_queue_selection_requests--;
8944 x_unqueue_events (display);
8947 /* The main X event-reading loop - XTread_socket. */
8949 /* Time stamp of enter window event. This is only used by XTread_socket,
8950 but we have to put it out here, since static variables within functions
8951 sometimes don't work. */
8953 static Time enter_timestamp;
8955 /* This holds the state XLookupString needs to implement dead keys
8956 and other tricks known as "compose processing". _X Window System_
8957 says that a portable program can't use this, but Stephen Gildea assures
8958 me that letting the compiler initialize it to zeros will work okay.
8960 This must be defined outside of XTread_socket, for the same reasons
8961 given for enter_time stamp, above. */
8963 static XComposeStatus compose_status;
8965 /* Record the last 100 characters stored
8966 to help debug the loss-of-chars-during-GC problem. */
8968 static int temp_index;
8969 static short temp_buffer[100];
8971 /* Set this to nonzero to fake an "X I/O error"
8972 on a particular display. */
8974 struct x_display_info *XTread_socket_fake_io_error;
8976 /* When we find no input here, we occasionally do a no-op command
8977 to verify that the X server is still running and we can still talk with it.
8978 We try all the open displays, one by one.
8979 This variable is used for cycling thru the displays. */
8981 static struct x_display_info *next_noop_dpyinfo;
8983 #define SET_SAVED_MENU_EVENT(size) \
8984 do \
8986 if (f->output_data.x->saved_menu_event == 0) \
8987 f->output_data.x->saved_menu_event \
8988 = (XEvent *) xmalloc (sizeof (XEvent)); \
8989 bcopy (&event, f->output_data.x->saved_menu_event, size); \
8990 if (numchars >= 1) \
8992 bufp->kind = menu_bar_activate_event; \
8993 XSETFRAME (bufp->frame_or_window, f); \
8994 bufp->arg = Qnil; \
8995 bufp++; \
8996 count++; \
8997 numchars--; \
9000 while (0)
9002 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9003 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9005 /* Read events coming from the X server.
9006 This routine is called by the SIGIO handler.
9007 We return as soon as there are no more events to be read.
9009 Events representing keys are stored in buffer BUFP,
9010 which can hold up to NUMCHARS characters.
9011 We return the number of characters stored into the buffer,
9012 thus pretending to be `read'.
9014 EXPECTED is nonzero if the caller knows input is available. */
9017 XTread_socket (sd, bufp, numchars, expected)
9018 register int sd;
9019 /* register */ struct input_event *bufp;
9020 /* register */ int numchars;
9021 int expected;
9023 int count = 0;
9024 int nbytes = 0;
9025 XEvent event;
9026 struct frame *f;
9027 int event_found = 0;
9028 struct x_display_info *dpyinfo;
9030 if (interrupt_input_blocked)
9032 interrupt_input_pending = 1;
9033 return -1;
9036 interrupt_input_pending = 0;
9037 BLOCK_INPUT;
9039 /* So people can tell when we have read the available input. */
9040 input_signal_count++;
9042 if (numchars <= 0)
9043 abort (); /* Don't think this happens. */
9045 ++handling_signal;
9047 /* Find the display we are supposed to read input for.
9048 It's the one communicating on descriptor SD. */
9049 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9051 #if 0 /* This ought to be unnecessary; let's verify it. */
9052 #ifdef FIOSNBIO
9053 /* If available, Xlib uses FIOSNBIO to make the socket
9054 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9055 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9056 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9057 fcntl (dpyinfo->connection, F_SETFL, 0);
9058 #endif /* ! defined (FIOSNBIO) */
9059 #endif
9061 #if 0 /* This code can't be made to work, with multiple displays,
9062 and appears not to be used on any system any more.
9063 Also keyboard.c doesn't turn O_NDELAY on and off
9064 for X connections. */
9065 #ifndef SIGIO
9066 #ifndef HAVE_SELECT
9067 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9069 extern int read_alarm_should_throw;
9070 read_alarm_should_throw = 1;
9071 XPeekEvent (dpyinfo->display, &event);
9072 read_alarm_should_throw = 0;
9074 #endif /* HAVE_SELECT */
9075 #endif /* SIGIO */
9076 #endif
9078 /* For debugging, this gives a way to fake an I/O error. */
9079 if (dpyinfo == XTread_socket_fake_io_error)
9081 XTread_socket_fake_io_error = 0;
9082 x_io_error_quitter (dpyinfo->display);
9085 while (XPending (dpyinfo->display))
9087 XNextEvent (dpyinfo->display, &event);
9089 #ifdef HAVE_X_I18N
9091 /* Filter events for the current X input method.
9092 XFilterEvent returns non-zero if the input method has
9093 consumed the event. We pass the frame's X window to
9094 XFilterEvent because that's the one for which the IC
9095 was created. */
9096 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9097 event.xclient.window);
9098 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9099 break;
9101 #endif
9102 event_found = 1;
9104 switch (event.type)
9106 case ClientMessage:
9108 if (event.xclient.message_type
9109 == dpyinfo->Xatom_wm_protocols
9110 && event.xclient.format == 32)
9112 if (event.xclient.data.l[0]
9113 == dpyinfo->Xatom_wm_take_focus)
9115 /* Use x_any_window_to_frame because this
9116 could be the shell widget window
9117 if the frame has no title bar. */
9118 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9119 #ifdef HAVE_X_I18N
9120 /* Not quite sure this is needed -pd */
9121 if (f && FRAME_XIC (f))
9122 XSetICFocus (FRAME_XIC (f));
9123 #endif
9124 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9125 instructs the WM to set the input focus automatically for
9126 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9127 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9128 it has set the focus. So, XSetInputFocus below is not
9129 needed.
9131 The call to XSetInputFocus below has also caused trouble. In
9132 cases where the XSetInputFocus done by the WM and the one
9133 below are temporally close (on a fast machine), the call
9134 below can generate additional FocusIn events which confuse
9135 Emacs. */
9137 /* Since we set WM_TAKE_FOCUS, we must call
9138 XSetInputFocus explicitly. But not if f is null,
9139 since that might be an event for a deleted frame. */
9140 if (f)
9142 Display *d = event.xclient.display;
9143 /* Catch and ignore errors, in case window has been
9144 iconified by a window manager such as GWM. */
9145 int count = x_catch_errors (d);
9146 XSetInputFocus (d, event.xclient.window,
9147 /* The ICCCM says this is
9148 the only valid choice. */
9149 RevertToParent,
9150 event.xclient.data.l[1]);
9151 /* This is needed to detect the error
9152 if there is an error. */
9153 XSync (d, False);
9154 x_uncatch_errors (d, count);
9156 /* Not certain about handling scroll bars here */
9157 #endif /* 0 */
9159 else if (event.xclient.data.l[0]
9160 == dpyinfo->Xatom_wm_save_yourself)
9162 /* Save state modify the WM_COMMAND property to
9163 something which can reinstate us. This notifies
9164 the session manager, who's looking for such a
9165 PropertyNotify. Can restart processing when
9166 a keyboard or mouse event arrives. */
9167 if (numchars > 0)
9169 f = x_top_window_to_frame (dpyinfo,
9170 event.xclient.window);
9172 /* This is just so we only give real data once
9173 for a single Emacs process. */
9174 if (f == SELECTED_FRAME ())
9175 XSetCommand (FRAME_X_DISPLAY (f),
9176 event.xclient.window,
9177 initial_argv, initial_argc);
9178 else if (f)
9179 XSetCommand (FRAME_X_DISPLAY (f),
9180 event.xclient.window,
9181 0, 0);
9184 else if (event.xclient.data.l[0]
9185 == dpyinfo->Xatom_wm_delete_window)
9187 struct frame *f
9188 = x_any_window_to_frame (dpyinfo,
9189 event.xclient.window);
9191 if (f)
9193 if (numchars == 0)
9194 abort ();
9196 bufp->kind = delete_window_event;
9197 XSETFRAME (bufp->frame_or_window, f);
9198 bufp->arg = Qnil;
9199 bufp++;
9201 count += 1;
9202 numchars -= 1;
9206 else if (event.xclient.message_type
9207 == dpyinfo->Xatom_wm_configure_denied)
9210 else if (event.xclient.message_type
9211 == dpyinfo->Xatom_wm_window_moved)
9213 int new_x, new_y;
9214 struct frame *f
9215 = x_window_to_frame (dpyinfo, event.xclient.window);
9217 new_x = event.xclient.data.s[0];
9218 new_y = event.xclient.data.s[1];
9220 if (f)
9222 f->output_data.x->left_pos = new_x;
9223 f->output_data.x->top_pos = new_y;
9226 #ifdef HACK_EDITRES
9227 else if (event.xclient.message_type
9228 == dpyinfo->Xatom_editres)
9230 struct frame *f
9231 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9232 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9233 &event, NULL);
9235 #endif /* HACK_EDITRES */
9236 else if ((event.xclient.message_type
9237 == dpyinfo->Xatom_DONE)
9238 || (event.xclient.message_type
9239 == dpyinfo->Xatom_PAGE))
9241 /* Ghostview job completed. Kill it. We could
9242 reply with "Next" if we received "Page", but we
9243 currently never do because we are interested in
9244 images, only, which should have 1 page. */
9245 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9246 struct frame *f
9247 = x_window_to_frame (dpyinfo, event.xclient.window);
9248 x_kill_gs_process (pixmap, f);
9249 expose_frame (f, 0, 0, 0, 0);
9251 #ifdef USE_TOOLKIT_SCROLL_BARS
9252 /* Scroll bar callbacks send a ClientMessage from which
9253 we construct an input_event. */
9254 else if (event.xclient.message_type
9255 == dpyinfo->Xatom_Scrollbar)
9257 x_scroll_bar_to_input_event (&event, bufp);
9258 ++bufp, ++count, --numchars;
9259 goto out;
9261 #endif /* USE_TOOLKIT_SCROLL_BARS */
9262 else
9263 goto OTHER;
9265 break;
9267 case SelectionNotify:
9268 #ifdef USE_X_TOOLKIT
9269 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9270 goto OTHER;
9271 #endif /* not USE_X_TOOLKIT */
9272 x_handle_selection_notify (&event.xselection);
9273 break;
9275 case SelectionClear: /* Someone has grabbed ownership. */
9276 #ifdef USE_X_TOOLKIT
9277 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9278 goto OTHER;
9279 #endif /* USE_X_TOOLKIT */
9281 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9283 if (numchars == 0)
9284 abort ();
9286 bufp->kind = selection_clear_event;
9287 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9288 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9289 SELECTION_EVENT_TIME (bufp) = eventp->time;
9290 bufp->frame_or_window = Qnil;
9291 bufp->arg = Qnil;
9292 bufp++;
9294 count += 1;
9295 numchars -= 1;
9297 break;
9299 case SelectionRequest: /* Someone wants our selection. */
9300 #ifdef USE_X_TOOLKIT
9301 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9302 goto OTHER;
9303 #endif /* USE_X_TOOLKIT */
9304 if (x_queue_selection_requests)
9305 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9306 &event);
9307 else
9309 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9311 if (numchars == 0)
9312 abort ();
9314 bufp->kind = selection_request_event;
9315 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9316 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9317 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9318 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9319 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9320 SELECTION_EVENT_TIME (bufp) = eventp->time;
9321 bufp->frame_or_window = Qnil;
9322 bufp->arg = Qnil;
9323 bufp++;
9325 count += 1;
9326 numchars -= 1;
9328 break;
9330 case PropertyNotify:
9331 #ifdef USE_X_TOOLKIT
9332 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9333 goto OTHER;
9334 #endif /* not USE_X_TOOLKIT */
9335 x_handle_property_notify (&event.xproperty);
9336 break;
9338 case ReparentNotify:
9339 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9340 if (f)
9342 int x, y;
9343 f->output_data.x->parent_desc = event.xreparent.parent;
9344 x_real_positions (f, &x, &y);
9345 f->output_data.x->left_pos = x;
9346 f->output_data.x->top_pos = y;
9348 break;
9350 case Expose:
9351 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9352 if (f)
9354 if (f->async_visible == 0)
9356 f->async_visible = 1;
9357 f->async_iconified = 0;
9358 f->output_data.x->has_been_visible = 1;
9359 SET_FRAME_GARBAGED (f);
9361 else
9362 expose_frame (x_window_to_frame (dpyinfo,
9363 event.xexpose.window),
9364 event.xexpose.x, event.xexpose.y,
9365 event.xexpose.width, event.xexpose.height);
9367 else
9369 #ifdef USE_TOOLKIT_SCROLL_BARS
9370 /* Dispatch event to the widget. */
9371 goto OTHER;
9372 #else /* not USE_TOOLKIT_SCROLL_BARS */
9373 struct scroll_bar *bar
9374 = x_window_to_scroll_bar (event.xexpose.window);
9376 if (bar)
9377 x_scroll_bar_expose (bar, &event);
9378 #ifdef USE_X_TOOLKIT
9379 else
9380 goto OTHER;
9381 #endif /* USE_X_TOOLKIT */
9382 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9384 break;
9386 case GraphicsExpose: /* This occurs when an XCopyArea's
9387 source area was obscured or not
9388 available.*/
9389 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9390 if (f)
9392 expose_frame (f,
9393 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9394 event.xgraphicsexpose.width,
9395 event.xgraphicsexpose.height);
9397 #ifdef USE_X_TOOLKIT
9398 else
9399 goto OTHER;
9400 #endif /* USE_X_TOOLKIT */
9401 break;
9403 case NoExpose: /* This occurs when an XCopyArea's
9404 source area was completely
9405 available */
9406 break;
9408 case UnmapNotify:
9409 /* Redo the mouse-highlight after the tooltip has gone. */
9410 if (event.xmap.window == tip_window)
9412 tip_window = 0;
9413 redo_mouse_highlight ();
9416 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9417 if (f) /* F may no longer exist if
9418 the frame was deleted. */
9420 /* While a frame is unmapped, display generation is
9421 disabled; you don't want to spend time updating a
9422 display that won't ever be seen. */
9423 f->async_visible = 0;
9424 /* We can't distinguish, from the event, whether the window
9425 has become iconified or invisible. So assume, if it
9426 was previously visible, than now it is iconified.
9427 But x_make_frame_invisible clears both
9428 the visible flag and the iconified flag;
9429 and that way, we know the window is not iconified now. */
9430 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9432 f->async_iconified = 1;
9434 bufp->kind = iconify_event;
9435 XSETFRAME (bufp->frame_or_window, f);
9436 bufp->arg = Qnil;
9437 bufp++;
9438 count++;
9439 numchars--;
9442 goto OTHER;
9444 case MapNotify:
9445 if (event.xmap.window == tip_window)
9446 /* The tooltip has been drawn already. Avoid
9447 the SET_FRAME_GARBAGED below. */
9448 goto OTHER;
9450 /* We use x_top_window_to_frame because map events can
9451 come for sub-windows and they don't mean that the
9452 frame is visible. */
9453 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9454 if (f)
9456 f->async_visible = 1;
9457 f->async_iconified = 0;
9458 f->output_data.x->has_been_visible = 1;
9460 /* wait_reading_process_input will notice this and update
9461 the frame's display structures. */
9462 SET_FRAME_GARBAGED (f);
9464 if (f->iconified)
9466 bufp->kind = deiconify_event;
9467 XSETFRAME (bufp->frame_or_window, f);
9468 bufp->arg = Qnil;
9469 bufp++;
9470 count++;
9471 numchars--;
9473 else if (! NILP (Vframe_list)
9474 && ! NILP (XCDR (Vframe_list)))
9475 /* Force a redisplay sooner or later
9476 to update the frame titles
9477 in case this is the second frame. */
9478 record_asynch_buffer_change ();
9480 goto OTHER;
9482 case KeyPress:
9483 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9485 #ifdef USE_MOTIF
9486 /* I couldn't find a way to prevent LessTif scroll bars
9487 from consuming key events. */
9488 if (f == 0)
9490 Widget widget = XtWindowToWidget (dpyinfo->display,
9491 event.xkey.window);
9492 if (widget && XmIsScrollBar (widget))
9494 widget = XtParent (widget);
9495 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9498 #endif /* USE_MOTIF */
9500 if (f != 0)
9502 KeySym keysym, orig_keysym;
9503 /* al%imercury@uunet.uu.net says that making this 81 instead of
9504 80 fixed a bug whereby meta chars made his Emacs hang. */
9505 unsigned char copy_buffer[81];
9506 int modifiers;
9508 event.xkey.state
9509 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9510 extra_keyboard_modifiers);
9511 modifiers = event.xkey.state;
9513 /* This will have to go some day... */
9515 /* make_lispy_event turns chars into control chars.
9516 Don't do it here because XLookupString is too eager. */
9517 event.xkey.state &= ~ControlMask;
9518 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9519 | dpyinfo->super_mod_mask
9520 | dpyinfo->hyper_mod_mask
9521 | dpyinfo->alt_mod_mask);
9523 /* In case Meta is ComposeCharacter,
9524 clear its status. According to Markus Ehrnsperger
9525 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9526 this enables ComposeCharacter to work whether or
9527 not it is combined with Meta. */
9528 if (modifiers & dpyinfo->meta_mod_mask)
9529 bzero (&compose_status, sizeof (compose_status));
9531 #ifdef HAVE_X_I18N
9532 if (FRAME_XIC (f))
9534 unsigned char *copy_bufptr = copy_buffer;
9535 int copy_bufsiz = sizeof (copy_buffer);
9536 Status status_return;
9538 nbytes = XmbLookupString (FRAME_XIC (f),
9539 &event.xkey, copy_bufptr,
9540 copy_bufsiz, &keysym,
9541 &status_return);
9542 if (status_return == XBufferOverflow)
9544 copy_bufsiz = nbytes + 1;
9545 copy_bufptr = (char *) alloca (copy_bufsiz);
9546 nbytes = XmbLookupString (FRAME_XIC (f),
9547 &event.xkey, copy_bufptr,
9548 copy_bufsiz, &keysym,
9549 &status_return);
9552 if (status_return == XLookupNone)
9553 break;
9554 else if (status_return == XLookupChars)
9556 keysym = NoSymbol;
9557 modifiers = 0;
9559 else if (status_return != XLookupKeySym
9560 && status_return != XLookupBoth)
9561 abort ();
9563 else
9564 nbytes = XLookupString (&event.xkey, copy_buffer,
9565 80, &keysym, &compose_status);
9566 #else
9567 nbytes = XLookupString (&event.xkey, copy_buffer,
9568 80, &keysym, &compose_status);
9569 #endif
9571 orig_keysym = keysym;
9573 if (numchars > 1)
9575 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9576 || keysym == XK_Delete
9577 #ifdef XK_ISO_Left_Tab
9578 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9579 #endif
9580 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9581 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9582 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9583 #ifdef HPUX
9584 /* This recognizes the "extended function keys".
9585 It seems there's no cleaner way.
9586 Test IsModifierKey to avoid handling mode_switch
9587 incorrectly. */
9588 || ((unsigned) (keysym) >= XK_Select
9589 && (unsigned)(keysym) < XK_KP_Space)
9590 #endif
9591 #ifdef XK_dead_circumflex
9592 || orig_keysym == XK_dead_circumflex
9593 #endif
9594 #ifdef XK_dead_grave
9595 || orig_keysym == XK_dead_grave
9596 #endif
9597 #ifdef XK_dead_tilde
9598 || orig_keysym == XK_dead_tilde
9599 #endif
9600 #ifdef XK_dead_diaeresis
9601 || orig_keysym == XK_dead_diaeresis
9602 #endif
9603 #ifdef XK_dead_macron
9604 || orig_keysym == XK_dead_macron
9605 #endif
9606 #ifdef XK_dead_degree
9607 || orig_keysym == XK_dead_degree
9608 #endif
9609 #ifdef XK_dead_acute
9610 || orig_keysym == XK_dead_acute
9611 #endif
9612 #ifdef XK_dead_cedilla
9613 || orig_keysym == XK_dead_cedilla
9614 #endif
9615 #ifdef XK_dead_breve
9616 || orig_keysym == XK_dead_breve
9617 #endif
9618 #ifdef XK_dead_ogonek
9619 || orig_keysym == XK_dead_ogonek
9620 #endif
9621 #ifdef XK_dead_caron
9622 || orig_keysym == XK_dead_caron
9623 #endif
9624 #ifdef XK_dead_doubleacute
9625 || orig_keysym == XK_dead_doubleacute
9626 #endif
9627 #ifdef XK_dead_abovedot
9628 || orig_keysym == XK_dead_abovedot
9629 #endif
9630 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9631 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9632 /* Any "vendor-specific" key is ok. */
9633 || (orig_keysym & (1 << 28)))
9634 && ! (IsModifierKey (orig_keysym)
9635 #ifndef HAVE_X11R5
9636 #ifdef XK_Mode_switch
9637 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9638 #endif
9639 #ifdef XK_Num_Lock
9640 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9641 #endif
9642 #endif /* not HAVE_X11R5 */
9645 if (temp_index == sizeof temp_buffer / sizeof (short))
9646 temp_index = 0;
9647 temp_buffer[temp_index++] = keysym;
9648 bufp->kind = non_ascii_keystroke;
9649 bufp->code = keysym;
9650 XSETFRAME (bufp->frame_or_window, f);
9651 bufp->arg = Qnil;
9652 bufp->modifiers
9653 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9654 modifiers);
9655 bufp->timestamp = event.xkey.time;
9656 bufp++;
9657 count++;
9658 numchars--;
9660 else if (numchars > nbytes)
9662 register int i;
9664 for (i = 0; i < nbytes; i++)
9666 if (temp_index == sizeof temp_buffer / sizeof (short))
9667 temp_index = 0;
9668 temp_buffer[temp_index++] = copy_buffer[i];
9669 bufp->kind = ascii_keystroke;
9670 bufp->code = copy_buffer[i];
9671 XSETFRAME (bufp->frame_or_window, f);
9672 bufp->arg = Qnil;
9673 bufp->modifiers
9674 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9675 modifiers);
9676 bufp->timestamp = event.xkey.time;
9677 bufp++;
9680 count += nbytes;
9681 numchars -= nbytes;
9683 if (keysym == NoSymbol)
9684 break;
9686 else
9687 abort ();
9689 else
9690 abort ();
9692 #ifdef HAVE_X_I18N
9693 /* Don't dispatch this event since XtDispatchEvent calls
9694 XFilterEvent, and two calls in a row may freeze the
9695 client. */
9696 break;
9697 #else
9698 goto OTHER;
9699 #endif
9701 case KeyRelease:
9702 #ifdef HAVE_X_I18N
9703 /* Don't dispatch this event since XtDispatchEvent calls
9704 XFilterEvent, and two calls in a row may freeze the
9705 client. */
9706 break;
9707 #else
9708 goto OTHER;
9709 #endif
9711 /* Here's a possible interpretation of the whole
9712 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9713 you get a FocusIn event, you have to get a FocusOut
9714 event before you relinquish the focus. If you
9715 haven't received a FocusIn event, then a mere
9716 LeaveNotify is enough to free you. */
9718 case EnterNotify:
9720 int from_menu_bar_p = 0;
9722 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
9724 #ifdef LESSTIF_VERSION
9725 /* When clicking outside of a menu bar popup to close
9726 it, we get a FocusIn/ EnterNotify sequence of
9727 events. The flag event.xcrossing.focus is not set
9728 in the EnterNotify event of that sequence because
9729 the focus is in the menu bar,
9730 event.xcrossing.window is the frame's X window.
9731 Unconditionally setting the focus frame to null in
9732 this case is not the right thing, because no event
9733 follows that could set the focus frame to the right
9734 value.
9736 This could be a LessTif bug, but I wasn't able to
9737 reproduce the behavior in a simple test program.
9739 (gerd, LessTif 0.88.1). */
9741 if (!event.xcrossing.focus
9742 && f
9743 && f->output_data.x->menubar_widget)
9745 Window focus;
9746 int revert;
9748 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9749 if (focus == XtWindow (f->output_data.x->menubar_widget))
9750 from_menu_bar_p = 1;
9752 #endif /* LESSTIF_VERSION */
9754 if (event.xcrossing.focus || from_menu_bar_p)
9756 /* Avoid nasty pop/raise loops. */
9757 if (f && (!(f->auto_raise)
9758 || !(f->auto_lower)
9759 || (event.xcrossing.time - enter_timestamp) > 500))
9761 x_new_focus_frame (dpyinfo, f);
9762 enter_timestamp = event.xcrossing.time;
9765 else if (f == dpyinfo->x_focus_frame)
9766 x_new_focus_frame (dpyinfo, 0);
9768 /* EnterNotify counts as mouse movement,
9769 so update things that depend on mouse position. */
9770 if (f && !f->output_data.x->busy_p)
9771 note_mouse_movement (f, &event.xmotion);
9772 goto OTHER;
9775 case FocusIn:
9776 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9777 if (event.xfocus.detail != NotifyPointer)
9778 dpyinfo->x_focus_event_frame = f;
9779 if (f)
9781 x_new_focus_frame (dpyinfo, f);
9783 /* Don't stop displaying the initial startup message
9784 for a switch-frame event we don't need. */
9785 if (GC_NILP (Vterminal_frame)
9786 && GC_CONSP (Vframe_list)
9787 && !GC_NILP (XCDR (Vframe_list)))
9789 bufp->kind = FOCUS_IN_EVENT;
9790 XSETFRAME (bufp->frame_or_window, f);
9791 bufp->arg = Qnil;
9792 ++bufp, ++count, --numchars;
9796 #ifdef HAVE_X_I18N
9797 if (f && FRAME_XIC (f))
9798 XSetICFocus (FRAME_XIC (f));
9799 #endif
9801 goto OTHER;
9803 case LeaveNotify:
9804 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
9805 if (f)
9807 Lisp_Object frame;
9808 int from_menu_bar_p = 0;
9810 if (f == dpyinfo->mouse_face_mouse_frame)
9812 /* If we move outside the frame, then we're
9813 certainly no longer on any text in the frame. */
9814 clear_mouse_face (dpyinfo);
9815 dpyinfo->mouse_face_mouse_frame = 0;
9818 /* Generate a nil HELP_EVENT to cancel a help-echo.
9819 Do it only if there's something to cancel.
9820 Otherwise, the startup message is cleared when
9821 the mouse leaves the frame. */
9822 if (any_help_event_p)
9824 Lisp_Object frame;
9825 int n;
9827 XSETFRAME (frame, f);
9828 n = gen_help_event (bufp, Qnil, frame, Qnil, Qnil, 0);
9829 bufp += n, count += n, numchars -= n;
9832 #ifdef LESSTIF_VERSION
9833 /* Please see the comment at the start of the
9834 EnterNotify case. */
9835 if (!event.xcrossing.focus
9836 && f->output_data.x->menubar_widget)
9838 Window focus;
9839 int revert;
9840 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
9841 if (focus == XtWindow (f->output_data.x->menubar_widget))
9842 from_menu_bar_p = 1;
9844 #endif /* LESSTIF_VERSION */
9846 if (event.xcrossing.focus || from_menu_bar_p)
9847 x_mouse_leave (dpyinfo);
9848 else
9850 if (f == dpyinfo->x_focus_event_frame)
9851 dpyinfo->x_focus_event_frame = 0;
9852 if (f == dpyinfo->x_focus_frame)
9853 x_new_focus_frame (dpyinfo, 0);
9856 goto OTHER;
9858 case FocusOut:
9859 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
9860 if (event.xfocus.detail != NotifyPointer
9861 && f == dpyinfo->x_focus_event_frame)
9862 dpyinfo->x_focus_event_frame = 0;
9863 if (f && f == dpyinfo->x_focus_frame)
9864 x_new_focus_frame (dpyinfo, 0);
9866 #ifdef HAVE_X_I18N
9867 if (f && FRAME_XIC (f))
9868 XUnsetICFocus (FRAME_XIC (f));
9869 #endif
9871 goto OTHER;
9873 case MotionNotify:
9875 previous_help_echo = help_echo;
9876 help_echo = help_echo_object = help_echo_window = Qnil;
9877 help_echo_pos = -1;
9879 if (dpyinfo->grabbed && last_mouse_frame
9880 && FRAME_LIVE_P (last_mouse_frame))
9881 f = last_mouse_frame;
9882 else
9883 f = x_window_to_frame (dpyinfo, event.xmotion.window);
9885 if (f)
9886 note_mouse_movement (f, &event.xmotion);
9887 else
9889 #ifndef USE_TOOLKIT_SCROLL_BARS
9890 struct scroll_bar *bar
9891 = x_window_to_scroll_bar (event.xmotion.window);
9893 if (bar)
9894 x_scroll_bar_note_movement (bar, &event);
9895 #endif /* USE_TOOLKIT_SCROLL_BARS */
9897 /* If we move outside the frame, then we're
9898 certainly no longer on any text in the frame. */
9899 clear_mouse_face (dpyinfo);
9902 /* If the contents of the global variable help_echo
9903 has changed, generate a HELP_EVENT. */
9904 if (!NILP (help_echo)
9905 || !NILP (previous_help_echo))
9907 Lisp_Object frame;
9908 int n;
9910 if (f)
9911 XSETFRAME (frame, f);
9912 else
9913 frame = Qnil;
9915 any_help_event_p = 1;
9916 n = gen_help_event (bufp, help_echo, frame,
9917 help_echo_window, help_echo_object,
9918 help_echo_pos);
9919 bufp += n, count += n, numchars -= n;
9922 goto OTHER;
9925 case ConfigureNotify:
9926 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
9927 if (f)
9929 #ifndef USE_X_TOOLKIT
9930 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
9931 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
9933 /* In the toolkit version, change_frame_size
9934 is called by the code that handles resizing
9935 of the EmacsFrame widget. */
9937 /* Even if the number of character rows and columns has
9938 not changed, the font size may have changed, so we need
9939 to check the pixel dimensions as well. */
9940 if (columns != f->width
9941 || rows != f->height
9942 || event.xconfigure.width != f->output_data.x->pixel_width
9943 || event.xconfigure.height != f->output_data.x->pixel_height)
9945 change_frame_size (f, rows, columns, 0, 1, 0);
9946 SET_FRAME_GARBAGED (f);
9947 cancel_mouse_face (f);
9949 #endif
9951 f->output_data.x->pixel_width = event.xconfigure.width;
9952 f->output_data.x->pixel_height = event.xconfigure.height;
9954 /* What we have now is the position of Emacs's own window.
9955 Convert that to the position of the window manager window. */
9956 x_real_positions (f, &f->output_data.x->left_pos,
9957 &f->output_data.x->top_pos);
9959 #ifdef HAVE_X_I18N
9960 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
9961 xic_set_statusarea (f);
9962 #endif
9964 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
9966 /* Since the WM decorations come below top_pos now,
9967 we must put them below top_pos in the future. */
9968 f->output_data.x->win_gravity = NorthWestGravity;
9969 x_wm_set_size_hint (f, (long) 0, 0);
9971 #ifdef USE_MOTIF
9972 /* Some window managers pass (0,0) as the location of
9973 the window, and the Motif event handler stores it
9974 in the emacs widget, which messes up Motif menus. */
9975 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
9977 event.xconfigure.x = f->output_data.x->widget->core.x;
9978 event.xconfigure.y = f->output_data.x->widget->core.y;
9980 #endif /* USE_MOTIF */
9982 goto OTHER;
9984 case ButtonPress:
9985 case ButtonRelease:
9987 /* If we decide we want to generate an event to be seen
9988 by the rest of Emacs, we put it here. */
9989 struct input_event emacs_event;
9990 int tool_bar_p = 0;
9992 emacs_event.kind = no_event;
9993 bzero (&compose_status, sizeof (compose_status));
9995 if (dpyinfo->grabbed
9996 && last_mouse_frame
9997 && FRAME_LIVE_P (last_mouse_frame))
9998 f = last_mouse_frame;
9999 else
10000 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10002 if (f)
10004 /* Is this in the tool-bar? */
10005 if (WINDOWP (f->tool_bar_window)
10006 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10008 Lisp_Object window;
10009 int p, x, y;
10011 x = event.xbutton.x;
10012 y = event.xbutton.y;
10014 /* Set x and y. */
10015 window = window_from_coordinates (f, x, y, &p, 1);
10016 if (EQ (window, f->tool_bar_window))
10018 x_handle_tool_bar_click (f, &event.xbutton);
10019 tool_bar_p = 1;
10023 if (!tool_bar_p)
10024 if (!dpyinfo->x_focus_frame
10025 || f == dpyinfo->x_focus_frame)
10026 construct_mouse_click (&emacs_event, &event, f);
10028 else
10030 #ifndef USE_TOOLKIT_SCROLL_BARS
10031 struct scroll_bar *bar
10032 = x_window_to_scroll_bar (event.xbutton.window);
10034 if (bar)
10035 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10036 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10039 if (event.type == ButtonPress)
10041 dpyinfo->grabbed |= (1 << event.xbutton.button);
10042 last_mouse_frame = f;
10043 /* Ignore any mouse motion that happened
10044 before this event; any subsequent mouse-movement
10045 Emacs events should reflect only motion after
10046 the ButtonPress. */
10047 if (f != 0)
10048 f->mouse_moved = 0;
10050 if (!tool_bar_p)
10051 last_tool_bar_item = -1;
10053 else
10055 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10058 if (numchars >= 1 && emacs_event.kind != no_event)
10060 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10061 bufp++;
10062 count++;
10063 numchars--;
10066 #ifdef USE_X_TOOLKIT
10067 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10068 /* For a down-event in the menu bar,
10069 don't pass it to Xt right now.
10070 Instead, save it away
10071 and we will pass it to Xt from kbd_buffer_get_event.
10072 That way, we can run some Lisp code first. */
10073 if (f && event.type == ButtonPress
10074 /* Verify the event is really within the menu bar
10075 and not just sent to it due to grabbing. */
10076 && event.xbutton.x >= 0
10077 && event.xbutton.x < f->output_data.x->pixel_width
10078 && event.xbutton.y >= 0
10079 && event.xbutton.y < f->output_data.x->menubar_height
10080 && event.xbutton.same_screen)
10082 SET_SAVED_BUTTON_EVENT;
10083 XSETFRAME (last_mouse_press_frame, f);
10085 else if (event.type == ButtonPress)
10087 last_mouse_press_frame = Qnil;
10088 goto OTHER;
10091 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10092 but I am trying to be cautious. */
10093 else if (event.type == ButtonRelease)
10095 if (!NILP (last_mouse_press_frame))
10097 f = XFRAME (last_mouse_press_frame);
10098 if (f->output_data.x)
10099 SET_SAVED_BUTTON_EVENT;
10101 else
10102 goto OTHER;
10104 #endif /* USE_MOTIF */
10105 else
10106 goto OTHER;
10107 #endif /* USE_X_TOOLKIT */
10109 break;
10111 case CirculateNotify:
10112 goto OTHER;
10114 case CirculateRequest:
10115 goto OTHER;
10117 case VisibilityNotify:
10118 goto OTHER;
10120 case MappingNotify:
10121 /* Someone has changed the keyboard mapping - update the
10122 local cache. */
10123 switch (event.xmapping.request)
10125 case MappingModifier:
10126 x_find_modifier_meanings (dpyinfo);
10127 /* This is meant to fall through. */
10128 case MappingKeyboard:
10129 XRefreshKeyboardMapping (&event.xmapping);
10131 goto OTHER;
10133 default:
10134 OTHER:
10135 #ifdef USE_X_TOOLKIT
10136 BLOCK_INPUT;
10137 XtDispatchEvent (&event);
10138 UNBLOCK_INPUT;
10139 #endif /* USE_X_TOOLKIT */
10140 break;
10145 out:;
10147 /* On some systems, an X bug causes Emacs to get no more events
10148 when the window is destroyed. Detect that. (1994.) */
10149 if (! event_found)
10151 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10152 One XNOOP in 100 loops will make Emacs terminate.
10153 B. Bretthauer, 1994 */
10154 x_noop_count++;
10155 if (x_noop_count >= 100)
10157 x_noop_count=0;
10159 if (next_noop_dpyinfo == 0)
10160 next_noop_dpyinfo = x_display_list;
10162 XNoOp (next_noop_dpyinfo->display);
10164 /* Each time we get here, cycle through the displays now open. */
10165 next_noop_dpyinfo = next_noop_dpyinfo->next;
10169 /* If the focus was just given to an auto-raising frame,
10170 raise it now. */
10171 /* ??? This ought to be able to handle more than one such frame. */
10172 if (pending_autoraise_frame)
10174 x_raise_frame (pending_autoraise_frame);
10175 pending_autoraise_frame = 0;
10178 UNBLOCK_INPUT;
10179 --handling_signal;
10180 return count;
10186 /***********************************************************************
10187 Text Cursor
10188 ***********************************************************************/
10190 /* Note if the text cursor of window W has been overwritten by a
10191 drawing operation that outputs N glyphs starting at HPOS in the
10192 line given by output_cursor.vpos. N < 0 means all the rest of the
10193 line after HPOS has been written. */
10195 static void
10196 note_overwritten_text_cursor (w, hpos, n)
10197 struct window *w;
10198 int hpos, n;
10200 if (updated_area == TEXT_AREA
10201 && output_cursor.vpos == w->phys_cursor.vpos
10202 && hpos <= w->phys_cursor.hpos
10203 && (n < 0
10204 || hpos + n > w->phys_cursor.hpos))
10205 w->phys_cursor_on_p = 0;
10209 /* Set clipping for output in glyph row ROW. W is the window in which
10210 we operate. GC is the graphics context to set clipping in.
10211 WHOLE_LINE_P non-zero means include the areas used for truncation
10212 mark display and alike in the clipping rectangle.
10214 ROW may be a text row or, e.g., a mode line. Text rows must be
10215 clipped to the interior of the window dedicated to text display,
10216 mode lines must be clipped to the whole window. */
10218 static void
10219 x_clip_to_row (w, row, gc, whole_line_p)
10220 struct window *w;
10221 struct glyph_row *row;
10222 GC gc;
10223 int whole_line_p;
10225 struct frame *f = XFRAME (WINDOW_FRAME (w));
10226 XRectangle clip_rect;
10227 int window_x, window_y, window_width, window_height;
10229 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10231 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10232 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10233 clip_rect.y = max (clip_rect.y, window_y);
10234 clip_rect.width = window_width;
10235 clip_rect.height = row->visible_height;
10237 /* If clipping to the whole line, including trunc marks, extend
10238 the rectangle to the left and increase its width. */
10239 if (whole_line_p)
10241 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10242 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10245 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10249 /* Draw a hollow box cursor on window W in glyph row ROW. */
10251 static void
10252 x_draw_hollow_cursor (w, row)
10253 struct window *w;
10254 struct glyph_row *row;
10256 struct frame *f = XFRAME (WINDOW_FRAME (w));
10257 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10258 Display *dpy = FRAME_X_DISPLAY (f);
10259 int x, y, wd, h;
10260 XGCValues xgcv;
10261 struct glyph *cursor_glyph;
10262 GC gc;
10264 /* Compute frame-relative coordinates from window-relative
10265 coordinates. */
10266 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10267 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10268 + row->ascent - w->phys_cursor_ascent);
10269 h = row->height - 1;
10271 /* Get the glyph the cursor is on. If we can't tell because
10272 the current matrix is invalid or such, give up. */
10273 cursor_glyph = get_phys_cursor_glyph (w);
10274 if (cursor_glyph == NULL)
10275 return;
10277 /* Compute the width of the rectangle to draw. If on a stretch
10278 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10279 rectangle as wide as the glyph, but use a canonical character
10280 width instead. */
10281 wd = cursor_glyph->pixel_width - 1;
10282 if (cursor_glyph->type == STRETCH_GLYPH
10283 && !x_stretch_cursor_p)
10284 wd = min (CANON_X_UNIT (f), wd);
10286 /* The foreground of cursor_gc is typically the same as the normal
10287 background color, which can cause the cursor box to be invisible. */
10288 xgcv.foreground = f->output_data.x->cursor_pixel;
10289 if (dpyinfo->scratch_cursor_gc)
10290 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10291 else
10292 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10293 GCForeground, &xgcv);
10294 gc = dpyinfo->scratch_cursor_gc;
10296 /* Set clipping, draw the rectangle, and reset clipping again. */
10297 x_clip_to_row (w, row, gc, 0);
10298 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10299 XSetClipMask (dpy, gc, None);
10303 /* Draw a bar cursor on window W in glyph row ROW.
10305 Implementation note: One would like to draw a bar cursor with an
10306 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10307 Unfortunately, I didn't find a font yet that has this property set.
10308 --gerd. */
10310 static void
10311 x_draw_bar_cursor (w, row, width)
10312 struct window *w;
10313 struct glyph_row *row;
10314 int width;
10316 /* If cursor hpos is out of bounds, don't draw garbage. This can
10317 happen in mini-buffer windows when switching between echo area
10318 glyphs and mini-buffer. */
10319 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10321 struct frame *f = XFRAME (w->frame);
10322 struct glyph *cursor_glyph;
10323 GC gc;
10324 int x;
10325 unsigned long mask;
10326 XGCValues xgcv;
10327 Display *dpy;
10328 Window window;
10330 cursor_glyph = get_phys_cursor_glyph (w);
10331 if (cursor_glyph == NULL)
10332 return;
10334 xgcv.background = f->output_data.x->cursor_pixel;
10335 xgcv.foreground = f->output_data.x->cursor_pixel;
10336 xgcv.graphics_exposures = 0;
10337 mask = GCForeground | GCBackground | GCGraphicsExposures;
10338 dpy = FRAME_X_DISPLAY (f);
10339 window = FRAME_X_WINDOW (f);
10340 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10342 if (gc)
10343 XChangeGC (dpy, gc, mask, &xgcv);
10344 else
10346 gc = XCreateGC (dpy, window, mask, &xgcv);
10347 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10350 if (width < 0)
10351 width = f->output_data.x->cursor_width;
10353 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10354 x_clip_to_row (w, row, gc, 0);
10355 XFillRectangle (dpy, window, gc,
10357 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10358 min (cursor_glyph->pixel_width, width),
10359 row->height);
10360 XSetClipMask (dpy, gc, None);
10365 /* Clear the cursor of window W to background color, and mark the
10366 cursor as not shown. This is used when the text where the cursor
10367 is is about to be rewritten. */
10369 static void
10370 x_clear_cursor (w)
10371 struct window *w;
10373 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10374 x_update_window_cursor (w, 0);
10378 /* Draw the cursor glyph of window W in glyph row ROW. See the
10379 comment of x_draw_glyphs for the meaning of HL. */
10381 static void
10382 x_draw_phys_cursor_glyph (w, row, hl)
10383 struct window *w;
10384 struct glyph_row *row;
10385 enum draw_glyphs_face hl;
10387 /* If cursor hpos is out of bounds, don't draw garbage. This can
10388 happen in mini-buffer windows when switching between echo area
10389 glyphs and mini-buffer. */
10390 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10392 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10393 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10394 hl, 0, 0, 0);
10396 /* When we erase the cursor, and ROW is overlapped by other
10397 rows, make sure that these overlapping parts of other rows
10398 are redrawn. */
10399 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10401 if (row > w->current_matrix->rows
10402 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10403 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10405 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10406 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10407 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10413 /* Erase the image of a cursor of window W from the screen. */
10415 static void
10416 x_erase_phys_cursor (w)
10417 struct window *w;
10419 struct frame *f = XFRAME (w->frame);
10420 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10421 int hpos = w->phys_cursor.hpos;
10422 int vpos = w->phys_cursor.vpos;
10423 int mouse_face_here_p = 0;
10424 struct glyph_matrix *active_glyphs = w->current_matrix;
10425 struct glyph_row *cursor_row;
10426 struct glyph *cursor_glyph;
10427 enum draw_glyphs_face hl;
10429 /* No cursor displayed or row invalidated => nothing to do on the
10430 screen. */
10431 if (w->phys_cursor_type == NO_CURSOR)
10432 goto mark_cursor_off;
10434 /* VPOS >= active_glyphs->nrows means that window has been resized.
10435 Don't bother to erase the cursor. */
10436 if (vpos >= active_glyphs->nrows)
10437 goto mark_cursor_off;
10439 /* If row containing cursor is marked invalid, there is nothing we
10440 can do. */
10441 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10442 if (!cursor_row->enabled_p)
10443 goto mark_cursor_off;
10445 /* This can happen when the new row is shorter than the old one.
10446 In this case, either x_draw_glyphs or clear_end_of_line
10447 should have cleared the cursor. Note that we wouldn't be
10448 able to erase the cursor in this case because we don't have a
10449 cursor glyph at hand. */
10450 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10451 goto mark_cursor_off;
10453 /* If the cursor is in the mouse face area, redisplay that when
10454 we clear the cursor. */
10455 if (! NILP (dpyinfo->mouse_face_window)
10456 && w == XWINDOW (dpyinfo->mouse_face_window)
10457 && (vpos > dpyinfo->mouse_face_beg_row
10458 || (vpos == dpyinfo->mouse_face_beg_row
10459 && hpos >= dpyinfo->mouse_face_beg_col))
10460 && (vpos < dpyinfo->mouse_face_end_row
10461 || (vpos == dpyinfo->mouse_face_end_row
10462 && hpos < dpyinfo->mouse_face_end_col))
10463 /* Don't redraw the cursor's spot in mouse face if it is at the
10464 end of a line (on a newline). The cursor appears there, but
10465 mouse highlighting does not. */
10466 && cursor_row->used[TEXT_AREA] > hpos)
10467 mouse_face_here_p = 1;
10469 /* Maybe clear the display under the cursor. */
10470 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10472 int x;
10473 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10475 cursor_glyph = get_phys_cursor_glyph (w);
10476 if (cursor_glyph == NULL)
10477 goto mark_cursor_off;
10479 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10481 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10483 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10484 cursor_row->y)),
10485 cursor_glyph->pixel_width,
10486 cursor_row->visible_height,
10487 False);
10490 /* Erase the cursor by redrawing the character underneath it. */
10491 if (mouse_face_here_p)
10492 hl = DRAW_MOUSE_FACE;
10493 else if (cursor_row->inverse_p)
10494 hl = DRAW_INVERSE_VIDEO;
10495 else
10496 hl = DRAW_NORMAL_TEXT;
10497 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10499 mark_cursor_off:
10500 w->phys_cursor_on_p = 0;
10501 w->phys_cursor_type = NO_CURSOR;
10505 /* Display or clear cursor of window W. If ON is zero, clear the
10506 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10507 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10509 void
10510 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10511 struct window *w;
10512 int on, hpos, vpos, x, y;
10514 struct frame *f = XFRAME (w->frame);
10515 int new_cursor_type;
10516 int new_cursor_width;
10517 struct glyph_matrix *current_glyphs;
10518 struct glyph_row *glyph_row;
10519 struct glyph *glyph;
10521 /* This is pointless on invisible frames, and dangerous on garbaged
10522 windows and frames; in the latter case, the frame or window may
10523 be in the midst of changing its size, and x and y may be off the
10524 window. */
10525 if (! FRAME_VISIBLE_P (f)
10526 || FRAME_GARBAGED_P (f)
10527 || vpos >= w->current_matrix->nrows
10528 || hpos >= w->current_matrix->matrix_w)
10529 return;
10531 /* If cursor is off and we want it off, return quickly. */
10532 if (!on && !w->phys_cursor_on_p)
10533 return;
10535 current_glyphs = w->current_matrix;
10536 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10537 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10539 /* If cursor row is not enabled, we don't really know where to
10540 display the cursor. */
10541 if (!glyph_row->enabled_p)
10543 w->phys_cursor_on_p = 0;
10544 return;
10547 xassert (interrupt_input_blocked);
10549 /* Set new_cursor_type to the cursor we want to be displayed. In a
10550 mini-buffer window, we want the cursor only to appear if we are
10551 reading input from this window. For the selected window, we want
10552 the cursor type given by the frame parameter. If explicitly
10553 marked off, draw no cursor. In all other cases, we want a hollow
10554 box cursor. */
10555 new_cursor_width = -1;
10556 if (cursor_in_echo_area
10557 && FRAME_HAS_MINIBUF_P (f)
10558 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10560 if (w == XWINDOW (echo_area_window))
10561 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10562 else
10563 new_cursor_type = HOLLOW_BOX_CURSOR;
10565 else
10567 if (w != XWINDOW (selected_window)
10568 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10570 extern int cursor_in_non_selected_windows;
10572 if (MINI_WINDOW_P (w) || !cursor_in_non_selected_windows)
10573 new_cursor_type = NO_CURSOR;
10574 else
10575 new_cursor_type = HOLLOW_BOX_CURSOR;
10577 else if (w->cursor_off_p)
10578 new_cursor_type = NO_CURSOR;
10579 else
10581 struct buffer *b = XBUFFER (w->buffer);
10583 if (EQ (b->cursor_type, Qt))
10584 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10585 else
10586 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10587 &new_cursor_width);
10591 /* If cursor is currently being shown and we don't want it to be or
10592 it is in the wrong place, or the cursor type is not what we want,
10593 erase it. */
10594 if (w->phys_cursor_on_p
10595 && (!on
10596 || w->phys_cursor.x != x
10597 || w->phys_cursor.y != y
10598 || new_cursor_type != w->phys_cursor_type))
10599 x_erase_phys_cursor (w);
10601 /* If the cursor is now invisible and we want it to be visible,
10602 display it. */
10603 if (on && !w->phys_cursor_on_p)
10605 w->phys_cursor_ascent = glyph_row->ascent;
10606 w->phys_cursor_height = glyph_row->height;
10608 /* Set phys_cursor_.* before x_draw_.* is called because some
10609 of them may need the information. */
10610 w->phys_cursor.x = x;
10611 w->phys_cursor.y = glyph_row->y;
10612 w->phys_cursor.hpos = hpos;
10613 w->phys_cursor.vpos = vpos;
10614 w->phys_cursor_type = new_cursor_type;
10615 w->phys_cursor_on_p = 1;
10617 switch (new_cursor_type)
10619 case HOLLOW_BOX_CURSOR:
10620 x_draw_hollow_cursor (w, glyph_row);
10621 break;
10623 case FILLED_BOX_CURSOR:
10624 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10625 break;
10627 case BAR_CURSOR:
10628 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10629 break;
10631 case NO_CURSOR:
10632 break;
10634 default:
10635 abort ();
10638 #ifdef HAVE_X_I18N
10639 if (w == XWINDOW (f->selected_window))
10640 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10641 xic_set_preeditarea (w, x, y);
10642 #endif
10645 #ifndef XFlush
10646 if (updating_frame != f)
10647 XFlush (FRAME_X_DISPLAY (f));
10648 #endif
10652 /* Display the cursor on window W, or clear it. X and Y are window
10653 relative pixel coordinates. HPOS and VPOS are glyph matrix
10654 positions. If W is not the selected window, display a hollow
10655 cursor. ON non-zero means display the cursor at X, Y which
10656 correspond to HPOS, VPOS, otherwise it is cleared. */
10658 void
10659 x_display_cursor (w, on, hpos, vpos, x, y)
10660 struct window *w;
10661 int on, hpos, vpos, x, y;
10663 BLOCK_INPUT;
10664 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10665 UNBLOCK_INPUT;
10669 /* Display the cursor on window W, or clear it, according to ON_P.
10670 Don't change the cursor's position. */
10672 void
10673 x_update_cursor (f, on_p)
10674 struct frame *f;
10676 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10680 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10681 in the window tree rooted at W. */
10683 static void
10684 x_update_cursor_in_window_tree (w, on_p)
10685 struct window *w;
10686 int on_p;
10688 while (w)
10690 if (!NILP (w->hchild))
10691 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10692 else if (!NILP (w->vchild))
10693 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10694 else
10695 x_update_window_cursor (w, on_p);
10697 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10702 /* Switch the display of W's cursor on or off, according to the value
10703 of ON. */
10705 static void
10706 x_update_window_cursor (w, on)
10707 struct window *w;
10708 int on;
10710 /* Don't update cursor in windows whose frame is in the process
10711 of being deleted. */
10712 if (w->current_matrix)
10714 BLOCK_INPUT;
10715 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
10716 w->phys_cursor.x, w->phys_cursor.y);
10717 UNBLOCK_INPUT;
10724 /* Icons. */
10726 /* Refresh bitmap kitchen sink icon for frame F
10727 when we get an expose event for it. */
10729 void
10730 refreshicon (f)
10731 struct frame *f;
10733 /* Normally, the window manager handles this function. */
10736 /* Make the x-window of frame F use the gnu icon bitmap. */
10739 x_bitmap_icon (f, file)
10740 struct frame *f;
10741 Lisp_Object file;
10743 int bitmap_id;
10745 if (FRAME_X_WINDOW (f) == 0)
10746 return 1;
10748 /* Free up our existing icon bitmap if any. */
10749 if (f->output_data.x->icon_bitmap > 0)
10750 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10751 f->output_data.x->icon_bitmap = 0;
10753 if (STRINGP (file))
10754 bitmap_id = x_create_bitmap_from_file (f, file);
10755 else
10757 /* Create the GNU bitmap if necessary. */
10758 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
10759 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
10760 = x_create_bitmap_from_data (f, gnu_bits,
10761 gnu_width, gnu_height);
10763 /* The first time we create the GNU bitmap,
10764 this increments the ref-count one extra time.
10765 As a result, the GNU bitmap is never freed.
10766 That way, we don't have to worry about allocating it again. */
10767 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
10769 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
10772 x_wm_set_icon_pixmap (f, bitmap_id);
10773 f->output_data.x->icon_bitmap = bitmap_id;
10775 return 0;
10779 /* Make the x-window of frame F use a rectangle with text.
10780 Use ICON_NAME as the text. */
10783 x_text_icon (f, icon_name)
10784 struct frame *f;
10785 char *icon_name;
10787 if (FRAME_X_WINDOW (f) == 0)
10788 return 1;
10790 #ifdef HAVE_X11R4
10792 XTextProperty text;
10793 text.value = (unsigned char *) icon_name;
10794 text.encoding = XA_STRING;
10795 text.format = 8;
10796 text.nitems = strlen (icon_name);
10797 #ifdef USE_X_TOOLKIT
10798 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
10799 &text);
10800 #else /* not USE_X_TOOLKIT */
10801 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
10802 #endif /* not USE_X_TOOLKIT */
10804 #else /* not HAVE_X11R4 */
10805 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
10806 #endif /* not HAVE_X11R4 */
10808 if (f->output_data.x->icon_bitmap > 0)
10809 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
10810 f->output_data.x->icon_bitmap = 0;
10811 x_wm_set_icon_pixmap (f, 0);
10813 return 0;
10816 #define X_ERROR_MESSAGE_SIZE 200
10818 /* If non-nil, this should be a string.
10819 It means catch X errors and store the error message in this string. */
10821 static Lisp_Object x_error_message_string;
10823 /* An X error handler which stores the error message in
10824 x_error_message_string. This is called from x_error_handler if
10825 x_catch_errors is in effect. */
10827 static void
10828 x_error_catcher (display, error)
10829 Display *display;
10830 XErrorEvent *error;
10832 XGetErrorText (display, error->error_code,
10833 XSTRING (x_error_message_string)->data,
10834 X_ERROR_MESSAGE_SIZE);
10837 /* Begin trapping X errors for display DPY. Actually we trap X errors
10838 for all displays, but DPY should be the display you are actually
10839 operating on.
10841 After calling this function, X protocol errors no longer cause
10842 Emacs to exit; instead, they are recorded in the string
10843 stored in x_error_message_string.
10845 Calling x_check_errors signals an Emacs error if an X error has
10846 occurred since the last call to x_catch_errors or x_check_errors.
10848 Calling x_uncatch_errors resumes the normal error handling. */
10850 void x_check_errors ();
10851 static Lisp_Object x_catch_errors_unwind ();
10854 x_catch_errors (dpy)
10855 Display *dpy;
10857 int count = specpdl_ptr - specpdl;
10859 /* Make sure any errors from previous requests have been dealt with. */
10860 XSync (dpy, False);
10862 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
10864 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
10865 XSTRING (x_error_message_string)->data[0] = 0;
10867 return count;
10870 /* Unbind the binding that we made to check for X errors. */
10872 static Lisp_Object
10873 x_catch_errors_unwind (old_val)
10874 Lisp_Object old_val;
10876 x_error_message_string = old_val;
10877 return Qnil;
10880 /* If any X protocol errors have arrived since the last call to
10881 x_catch_errors or x_check_errors, signal an Emacs error using
10882 sprintf (a buffer, FORMAT, the x error message text) as the text. */
10884 void
10885 x_check_errors (dpy, format)
10886 Display *dpy;
10887 char *format;
10889 /* Make sure to catch any errors incurred so far. */
10890 XSync (dpy, False);
10892 if (XSTRING (x_error_message_string)->data[0])
10893 error (format, XSTRING (x_error_message_string)->data);
10896 /* Nonzero if we had any X protocol errors
10897 since we did x_catch_errors on DPY. */
10900 x_had_errors_p (dpy)
10901 Display *dpy;
10903 /* Make sure to catch any errors incurred so far. */
10904 XSync (dpy, False);
10906 return XSTRING (x_error_message_string)->data[0] != 0;
10909 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
10911 void
10912 x_clear_errors (dpy)
10913 Display *dpy;
10915 XSTRING (x_error_message_string)->data[0] = 0;
10918 /* Stop catching X protocol errors and let them make Emacs die.
10919 DPY should be the display that was passed to x_catch_errors.
10920 COUNT should be the value that was returned by
10921 the corresponding call to x_catch_errors. */
10923 void
10924 x_uncatch_errors (dpy, count)
10925 Display *dpy;
10926 int count;
10928 unbind_to (count, Qnil);
10931 #if 0
10932 static unsigned int x_wire_count;
10933 x_trace_wire ()
10935 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
10937 #endif /* ! 0 */
10940 /* Handle SIGPIPE, which can happen when the connection to a server
10941 simply goes away. SIGPIPE is handled by x_connection_signal.
10942 Don't need to do anything, because the write which caused the
10943 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
10944 which will do the appropriate cleanup for us. */
10946 static SIGTYPE
10947 x_connection_signal (signalnum) /* If we don't have an argument, */
10948 int signalnum; /* some compilers complain in signal calls. */
10950 #ifdef USG
10951 /* USG systems forget handlers when they are used;
10952 must reestablish each time */
10953 signal (signalnum, x_connection_signal);
10954 #endif /* USG */
10957 /* Handling X errors. */
10959 /* Handle the loss of connection to display DISPLAY. */
10961 static SIGTYPE
10962 x_connection_closed (display, error_message)
10963 Display *display;
10964 char *error_message;
10966 struct x_display_info *dpyinfo = x_display_info_for_display (display);
10967 Lisp_Object frame, tail;
10969 /* Indicate that this display is dead. */
10971 #if 0 /* Closing the display caused a bus error on OpenWindows. */
10972 #ifdef USE_X_TOOLKIT
10973 XtCloseDisplay (display);
10974 #endif
10975 #endif
10977 if (dpyinfo)
10978 dpyinfo->display = 0;
10980 /* First delete frames whose mini-buffers are on frames
10981 that are on the dead display. */
10982 FOR_EACH_FRAME (tail, frame)
10984 Lisp_Object minibuf_frame;
10985 minibuf_frame
10986 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
10987 if (FRAME_X_P (XFRAME (frame))
10988 && FRAME_X_P (XFRAME (minibuf_frame))
10989 && ! EQ (frame, minibuf_frame)
10990 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
10991 Fdelete_frame (frame, Qt);
10994 /* Now delete all remaining frames on the dead display.
10995 We are now sure none of these is used as the mini-buffer
10996 for another frame that we need to delete. */
10997 FOR_EACH_FRAME (tail, frame)
10998 if (FRAME_X_P (XFRAME (frame))
10999 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11001 /* Set this to t so that Fdelete_frame won't get confused
11002 trying to find a replacement. */
11003 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11004 Fdelete_frame (frame, Qt);
11007 if (dpyinfo)
11008 x_delete_display (dpyinfo);
11010 if (x_display_list == 0)
11012 fprintf (stderr, "%s\n", error_message);
11013 shut_down_emacs (0, 0, Qnil);
11014 exit (70);
11017 /* Ordinary stack unwind doesn't deal with these. */
11018 #ifdef SIGIO
11019 sigunblock (sigmask (SIGIO));
11020 #endif
11021 sigunblock (sigmask (SIGALRM));
11022 TOTALLY_UNBLOCK_INPUT;
11024 clear_waiting_for_input ();
11025 error ("%s", error_message);
11028 /* This is the usual handler for X protocol errors.
11029 It kills all frames on the display that we got the error for.
11030 If that was the only one, it prints an error message and kills Emacs. */
11032 static void
11033 x_error_quitter (display, error)
11034 Display *display;
11035 XErrorEvent *error;
11037 char buf[256], buf1[356];
11039 /* Note that there is no real way portable across R3/R4 to get the
11040 original error handler. */
11042 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11043 sprintf (buf1, "X protocol error: %s on protocol request %d",
11044 buf, error->request_code);
11045 x_connection_closed (display, buf1);
11048 /* This is the first-level handler for X protocol errors.
11049 It calls x_error_quitter or x_error_catcher. */
11051 static int
11052 x_error_handler (display, error)
11053 Display *display;
11054 XErrorEvent *error;
11056 if (! NILP (x_error_message_string))
11057 x_error_catcher (display, error);
11058 else
11059 x_error_quitter (display, error);
11060 return 0;
11063 /* This is the handler for X IO errors, always.
11064 It kills all frames on the display that we lost touch with.
11065 If that was the only one, it prints an error message and kills Emacs. */
11067 static int
11068 x_io_error_quitter (display)
11069 Display *display;
11071 char buf[256];
11073 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11074 x_connection_closed (display, buf);
11075 return 0;
11078 /* Changing the font of the frame. */
11080 /* Give frame F the font named FONTNAME as its default font, and
11081 return the full name of that font. FONTNAME may be a wildcard
11082 pattern; in that case, we choose some font that fits the pattern.
11083 The return value shows which font we chose. */
11085 Lisp_Object
11086 x_new_font (f, fontname)
11087 struct frame *f;
11088 register char *fontname;
11090 struct font_info *fontp
11091 = FS_LOAD_FONT (f, 0, fontname, -1);
11093 if (!fontp)
11094 return Qnil;
11096 f->output_data.x->font = (XFontStruct *) (fontp->font);
11097 f->output_data.x->baseline_offset = fontp->baseline_offset;
11098 f->output_data.x->fontset = -1;
11100 /* Compute the scroll bar width in character columns. */
11101 if (f->scroll_bar_pixel_width > 0)
11103 int wid = FONT_WIDTH (f->output_data.x->font);
11104 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11106 else
11108 int wid = FONT_WIDTH (f->output_data.x->font);
11109 f->scroll_bar_cols = (14 + wid - 1) / wid;
11112 /* Now make the frame display the given font. */
11113 if (FRAME_X_WINDOW (f) != 0)
11115 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11116 f->output_data.x->font->fid);
11117 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11118 f->output_data.x->font->fid);
11119 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11120 f->output_data.x->font->fid);
11122 frame_update_line_height (f);
11123 x_set_window_size (f, 0, f->width, f->height);
11125 else
11126 /* If we are setting a new frame's font for the first time,
11127 there are no faces yet, so this font's height is the line height. */
11128 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11130 return build_string (fontp->full_name);
11133 /* Give frame F the fontset named FONTSETNAME as its default font, and
11134 return the full name of that fontset. FONTSETNAME may be a wildcard
11135 pattern; in that case, we choose some fontset that fits the pattern.
11136 The return value shows which fontset we chose. */
11138 Lisp_Object
11139 x_new_fontset (f, fontsetname)
11140 struct frame *f;
11141 char *fontsetname;
11143 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11144 Lisp_Object result;
11146 if (fontset < 0)
11147 return Qnil;
11149 if (f->output_data.x->fontset == fontset)
11150 /* This fontset is already set in frame F. There's nothing more
11151 to do. */
11152 return fontset_name (fontset);
11154 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11156 if (!STRINGP (result))
11157 /* Can't load ASCII font. */
11158 return Qnil;
11160 /* Since x_new_font doesn't update any fontset information, do it now. */
11161 f->output_data.x->fontset = fontset;
11163 #ifdef HAVE_X_I18N
11164 if (FRAME_XIC (f)
11165 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11166 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11167 #endif
11169 return build_string (fontsetname);
11173 /***********************************************************************
11174 X Input Methods
11175 ***********************************************************************/
11177 #ifdef HAVE_X_I18N
11179 #ifdef HAVE_X11R6
11181 /* XIM destroy callback function, which is called whenever the
11182 connection to input method XIM dies. CLIENT_DATA contains a
11183 pointer to the x_display_info structure corresponding to XIM. */
11185 static void
11186 xim_destroy_callback (xim, client_data, call_data)
11187 XIM xim;
11188 XPointer client_data;
11189 XPointer call_data;
11191 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11192 Lisp_Object frame, tail;
11194 BLOCK_INPUT;
11196 /* No need to call XDestroyIC.. */
11197 FOR_EACH_FRAME (tail, frame)
11199 struct frame *f = XFRAME (frame);
11200 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11202 FRAME_XIC (f) = NULL;
11203 if (FRAME_XIC_FONTSET (f))
11205 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11206 FRAME_XIC_FONTSET (f) = NULL;
11211 /* No need to call XCloseIM. */
11212 dpyinfo->xim = NULL;
11213 XFree (dpyinfo->xim_styles);
11214 UNBLOCK_INPUT;
11217 #endif /* HAVE_X11R6 */
11219 /* Open the connection to the XIM server on display DPYINFO.
11220 RESOURCE_NAME is the resource name Emacs uses. */
11222 static void
11223 xim_open_dpy (dpyinfo, resource_name)
11224 struct x_display_info *dpyinfo;
11225 char *resource_name;
11227 #ifdef USE_XIM
11228 XIM xim;
11230 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11231 dpyinfo->xim = xim;
11233 if (xim)
11235 #ifdef HAVE_X11R6
11236 XIMCallback destroy;
11237 #endif
11239 /* Get supported styles and XIM values. */
11240 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11242 #ifdef HAVE_X11R6
11243 destroy.callback = xim_destroy_callback;
11244 destroy.client_data = (XPointer)dpyinfo;
11245 /* This isn't prptotyped in OSF 5.0. */
11246 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11247 #endif
11250 #else /* not USE_XIM */
11251 dpyinfo->xim = NULL;
11252 #endif /* not USE_XIM */
11256 #ifdef HAVE_X11R6_XIM
11258 struct xim_inst_t
11260 struct x_display_info *dpyinfo;
11261 char *resource_name;
11264 /* XIM instantiate callback function, which is called whenever an XIM
11265 server is available. DISPLAY is teh display of the XIM.
11266 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11267 when the callback was registered. */
11269 static void
11270 xim_instantiate_callback (display, client_data, call_data)
11271 Display *display;
11272 XPointer client_data;
11273 XPointer call_data;
11275 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11276 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11278 /* We don't support multiple XIM connections. */
11279 if (dpyinfo->xim)
11280 return;
11282 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11284 /* Create XIC for the existing frames on the same display, as long
11285 as they have no XIC. */
11286 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11288 Lisp_Object tail, frame;
11290 BLOCK_INPUT;
11291 FOR_EACH_FRAME (tail, frame)
11293 struct frame *f = XFRAME (frame);
11295 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11296 if (FRAME_XIC (f) == NULL)
11298 create_frame_xic (f);
11299 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11300 xic_set_statusarea (f);
11301 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11303 struct window *w = XWINDOW (f->selected_window);
11304 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11309 UNBLOCK_INPUT;
11313 #endif /* HAVE_X11R6_XIM */
11316 /* Open a connection to the XIM server on display DPYINFO.
11317 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11318 connection only at the first time. On X11R6, open the connection
11319 in the XIM instantiate callback function. */
11321 static void
11322 xim_initialize (dpyinfo, resource_name)
11323 struct x_display_info *dpyinfo;
11324 char *resource_name;
11326 #ifdef USE_XIM
11327 #ifdef HAVE_X11R6_XIM
11328 struct xim_inst_t *xim_inst;
11329 int len;
11331 dpyinfo->xim = NULL;
11332 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11333 xim_inst->dpyinfo = dpyinfo;
11334 len = strlen (resource_name);
11335 xim_inst->resource_name = (char *) xmalloc (len + 1);
11336 bcopy (resource_name, xim_inst->resource_name, len + 1);
11337 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11338 resource_name, EMACS_CLASS,
11339 xim_instantiate_callback,
11340 /* Fixme: This is XPointer in
11341 XFree86 but (XPointer *) on
11342 Tru64, at least. */
11343 (XPointer) xim_inst);
11344 #else /* not HAVE_X11R6_XIM */
11345 dpyinfo->xim = NULL;
11346 xim_open_dpy (dpyinfo, resource_name);
11347 #endif /* not HAVE_X11R6_XIM */
11349 #else /* not USE_XIM */
11350 dpyinfo->xim = NULL;
11351 #endif /* not USE_XIM */
11355 /* Close the connection to the XIM server on display DPYINFO. */
11357 static void
11358 xim_close_dpy (dpyinfo)
11359 struct x_display_info *dpyinfo;
11361 #ifdef USE_XIM
11362 #ifdef HAVE_X11R6_XIM
11363 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11364 NULL, EMACS_CLASS,
11365 xim_instantiate_callback, NULL);
11366 #endif /* not HAVE_X11R6_XIM */
11367 XCloseIM (dpyinfo->xim);
11368 dpyinfo->xim = NULL;
11369 XFree (dpyinfo->xim_styles);
11370 #endif /* USE_XIM */
11373 #endif /* not HAVE_X11R6_XIM */
11377 /* Calculate the absolute position in frame F
11378 from its current recorded position values and gravity. */
11380 void
11381 x_calc_absolute_position (f)
11382 struct frame *f;
11384 Window child;
11385 int win_x = 0, win_y = 0;
11386 int flags = f->output_data.x->size_hint_flags;
11387 int this_window;
11389 /* We have nothing to do if the current position
11390 is already for the top-left corner. */
11391 if (! ((flags & XNegative) || (flags & YNegative)))
11392 return;
11394 #ifdef USE_X_TOOLKIT
11395 this_window = XtWindow (f->output_data.x->widget);
11396 #else
11397 this_window = FRAME_X_WINDOW (f);
11398 #endif
11400 /* Find the position of the outside upper-left corner of
11401 the inner window, with respect to the outer window.
11402 But do this only if we will need the results. */
11403 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11405 int count;
11407 BLOCK_INPUT;
11408 count = x_catch_errors (FRAME_X_DISPLAY (f));
11409 while (1)
11411 x_clear_errors (FRAME_X_DISPLAY (f));
11412 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11414 /* From-window, to-window. */
11415 this_window,
11416 f->output_data.x->parent_desc,
11418 /* From-position, to-position. */
11419 0, 0, &win_x, &win_y,
11421 /* Child of win. */
11422 &child);
11423 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11425 Window newroot, newparent = 0xdeadbeef;
11426 Window *newchildren;
11427 unsigned int nchildren;
11429 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11430 &newparent, &newchildren, &nchildren))
11431 break;
11433 XFree ((char *) newchildren);
11435 f->output_data.x->parent_desc = newparent;
11437 else
11438 break;
11441 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11442 UNBLOCK_INPUT;
11445 /* Treat negative positions as relative to the leftmost bottommost
11446 position that fits on the screen. */
11447 if (flags & XNegative)
11448 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11449 - 2 * f->output_data.x->border_width - win_x
11450 - PIXEL_WIDTH (f)
11451 + f->output_data.x->left_pos);
11453 if (flags & YNegative)
11455 int menubar_height = 0;
11457 #ifdef USE_X_TOOLKIT
11458 if (f->output_data.x->menubar_widget)
11459 menubar_height
11460 = (f->output_data.x->menubar_widget->core.height
11461 + f->output_data.x->menubar_widget->core.border_width);
11462 #endif
11464 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11465 - 2 * f->output_data.x->border_width
11466 - win_y
11467 - PIXEL_HEIGHT (f)
11468 - menubar_height
11469 + f->output_data.x->top_pos);
11472 /* The left_pos and top_pos
11473 are now relative to the top and left screen edges,
11474 so the flags should correspond. */
11475 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11478 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11479 to really change the position, and 0 when calling from
11480 x_make_frame_visible (in that case, XOFF and YOFF are the current
11481 position values). It is -1 when calling from x_set_frame_parameters,
11482 which means, do adjust for borders but don't change the gravity. */
11484 void
11485 x_set_offset (f, xoff, yoff, change_gravity)
11486 struct frame *f;
11487 register int xoff, yoff;
11488 int change_gravity;
11490 int modified_top, modified_left;
11492 if (change_gravity > 0)
11494 f->output_data.x->top_pos = yoff;
11495 f->output_data.x->left_pos = xoff;
11496 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11497 if (xoff < 0)
11498 f->output_data.x->size_hint_flags |= XNegative;
11499 if (yoff < 0)
11500 f->output_data.x->size_hint_flags |= YNegative;
11501 f->output_data.x->win_gravity = NorthWestGravity;
11503 x_calc_absolute_position (f);
11505 BLOCK_INPUT;
11506 x_wm_set_size_hint (f, (long) 0, 0);
11508 modified_left = f->output_data.x->left_pos;
11509 modified_top = f->output_data.x->top_pos;
11510 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11511 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11512 /* It is a mystery why we need to add the border_width here
11513 when the frame is already visible, but experiment says we do. */
11514 if (change_gravity != 0)
11516 modified_left += f->output_data.x->border_width;
11517 modified_top += f->output_data.x->border_width;
11519 #endif
11521 #ifdef USE_X_TOOLKIT
11522 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11523 modified_left, modified_top);
11524 #else /* not USE_X_TOOLKIT */
11525 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11526 modified_left, modified_top);
11527 #endif /* not USE_X_TOOLKIT */
11528 UNBLOCK_INPUT;
11531 /* Call this to change the size of frame F's x-window.
11532 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11533 for this size change and subsequent size changes.
11534 Otherwise we leave the window gravity unchanged. */
11536 void
11537 x_set_window_size (f, change_gravity, cols, rows)
11538 struct frame *f;
11539 int change_gravity;
11540 int cols, rows;
11542 #ifndef USE_X_TOOLKIT
11543 int pixelwidth, pixelheight;
11544 #endif
11546 BLOCK_INPUT;
11548 #ifdef USE_X_TOOLKIT
11550 /* The x and y position of the widget is clobbered by the
11551 call to XtSetValues within EmacsFrameSetCharSize.
11552 This is a real kludge, but I don't understand Xt so I can't
11553 figure out a correct fix. Can anyone else tell me? -- rms. */
11554 int xpos = f->output_data.x->widget->core.x;
11555 int ypos = f->output_data.x->widget->core.y;
11556 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11557 f->output_data.x->widget->core.x = xpos;
11558 f->output_data.x->widget->core.y = ypos;
11561 #else /* not USE_X_TOOLKIT */
11563 check_frame_size (f, &rows, &cols);
11564 f->output_data.x->vertical_scroll_bar_extra
11565 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11567 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11568 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11569 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11570 f->output_data.x->flags_areas_extra
11571 = FRAME_FLAGS_AREA_WIDTH (f);
11572 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11573 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11575 f->output_data.x->win_gravity = NorthWestGravity;
11576 x_wm_set_size_hint (f, (long) 0, 0);
11578 XSync (FRAME_X_DISPLAY (f), False);
11579 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11580 pixelwidth, pixelheight);
11582 /* Now, strictly speaking, we can't be sure that this is accurate,
11583 but the window manager will get around to dealing with the size
11584 change request eventually, and we'll hear how it went when the
11585 ConfigureNotify event gets here.
11587 We could just not bother storing any of this information here,
11588 and let the ConfigureNotify event set everything up, but that
11589 might be kind of confusing to the Lisp code, since size changes
11590 wouldn't be reported in the frame parameters until some random
11591 point in the future when the ConfigureNotify event arrives.
11593 We pass 1 for DELAY since we can't run Lisp code inside of
11594 a BLOCK_INPUT. */
11595 change_frame_size (f, rows, cols, 0, 1, 0);
11596 PIXEL_WIDTH (f) = pixelwidth;
11597 PIXEL_HEIGHT (f) = pixelheight;
11599 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11600 receive in the ConfigureNotify event; if we get what we asked
11601 for, then the event won't cause the screen to become garbaged, so
11602 we have to make sure to do it here. */
11603 SET_FRAME_GARBAGED (f);
11605 XFlush (FRAME_X_DISPLAY (f));
11607 #endif /* not USE_X_TOOLKIT */
11609 /* If cursor was outside the new size, mark it as off. */
11610 mark_window_cursors_off (XWINDOW (f->root_window));
11612 /* Clear out any recollection of where the mouse highlighting was,
11613 since it might be in a place that's outside the new frame size.
11614 Actually checking whether it is outside is a pain in the neck,
11615 so don't try--just let the highlighting be done afresh with new size. */
11616 cancel_mouse_face (f);
11618 UNBLOCK_INPUT;
11621 /* Mouse warping. */
11623 void
11624 x_set_mouse_position (f, x, y)
11625 struct frame *f;
11626 int x, y;
11628 int pix_x, pix_y;
11630 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11631 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11633 if (pix_x < 0) pix_x = 0;
11634 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11636 if (pix_y < 0) pix_y = 0;
11637 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11639 BLOCK_INPUT;
11641 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11642 0, 0, 0, 0, pix_x, pix_y);
11643 UNBLOCK_INPUT;
11646 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11648 void
11649 x_set_mouse_pixel_position (f, pix_x, pix_y)
11650 struct frame *f;
11651 int pix_x, pix_y;
11653 BLOCK_INPUT;
11655 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11656 0, 0, 0, 0, pix_x, pix_y);
11657 UNBLOCK_INPUT;
11660 /* focus shifting, raising and lowering. */
11662 void
11663 x_focus_on_frame (f)
11664 struct frame *f;
11666 #if 0 /* This proves to be unpleasant. */
11667 x_raise_frame (f);
11668 #endif
11669 #if 0
11670 /* I don't think that the ICCCM allows programs to do things like this
11671 without the interaction of the window manager. Whatever you end up
11672 doing with this code, do it to x_unfocus_frame too. */
11673 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11674 RevertToPointerRoot, CurrentTime);
11675 #endif /* ! 0 */
11678 void
11679 x_unfocus_frame (f)
11680 struct frame *f;
11682 #if 0
11683 /* Look at the remarks in x_focus_on_frame. */
11684 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
11685 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
11686 RevertToPointerRoot, CurrentTime);
11687 #endif /* ! 0 */
11690 /* Raise frame F. */
11692 void
11693 x_raise_frame (f)
11694 struct frame *f;
11696 if (f->async_visible)
11698 BLOCK_INPUT;
11699 #ifdef USE_X_TOOLKIT
11700 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11701 #else /* not USE_X_TOOLKIT */
11702 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11703 #endif /* not USE_X_TOOLKIT */
11704 XFlush (FRAME_X_DISPLAY (f));
11705 UNBLOCK_INPUT;
11709 /* Lower frame F. */
11711 void
11712 x_lower_frame (f)
11713 struct frame *f;
11715 if (f->async_visible)
11717 BLOCK_INPUT;
11718 #ifdef USE_X_TOOLKIT
11719 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
11720 #else /* not USE_X_TOOLKIT */
11721 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11722 #endif /* not USE_X_TOOLKIT */
11723 XFlush (FRAME_X_DISPLAY (f));
11724 UNBLOCK_INPUT;
11728 static void
11729 XTframe_raise_lower (f, raise_flag)
11730 FRAME_PTR f;
11731 int raise_flag;
11733 if (raise_flag)
11734 x_raise_frame (f);
11735 else
11736 x_lower_frame (f);
11739 /* Change of visibility. */
11741 /* This tries to wait until the frame is really visible.
11742 However, if the window manager asks the user where to position
11743 the frame, this will return before the user finishes doing that.
11744 The frame will not actually be visible at that time,
11745 but it will become visible later when the window manager
11746 finishes with it. */
11748 void
11749 x_make_frame_visible (f)
11750 struct frame *f;
11752 Lisp_Object type;
11753 int original_top, original_left;
11755 BLOCK_INPUT;
11757 type = x_icon_type (f);
11758 if (!NILP (type))
11759 x_bitmap_icon (f, type);
11761 if (! FRAME_VISIBLE_P (f))
11763 /* We test FRAME_GARBAGED_P here to make sure we don't
11764 call x_set_offset a second time
11765 if we get to x_make_frame_visible a second time
11766 before the window gets really visible. */
11767 if (! FRAME_ICONIFIED_P (f)
11768 && ! f->output_data.x->asked_for_visible)
11769 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
11771 f->output_data.x->asked_for_visible = 1;
11773 if (! EQ (Vx_no_window_manager, Qt))
11774 x_wm_set_window_state (f, NormalState);
11775 #ifdef USE_X_TOOLKIT
11776 /* This was XtPopup, but that did nothing for an iconified frame. */
11777 XtMapWidget (f->output_data.x->widget);
11778 #else /* not USE_X_TOOLKIT */
11779 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11780 #endif /* not USE_X_TOOLKIT */
11781 #if 0 /* This seems to bring back scroll bars in the wrong places
11782 if the window configuration has changed. They seem
11783 to come back ok without this. */
11784 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
11785 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
11786 #endif
11789 XFlush (FRAME_X_DISPLAY (f));
11791 /* Synchronize to ensure Emacs knows the frame is visible
11792 before we do anything else. We do this loop with input not blocked
11793 so that incoming events are handled. */
11795 Lisp_Object frame;
11796 int count;
11797 /* This must be before UNBLOCK_INPUT
11798 since events that arrive in response to the actions above
11799 will set it when they are handled. */
11800 int previously_visible = f->output_data.x->has_been_visible;
11802 original_left = f->output_data.x->left_pos;
11803 original_top = f->output_data.x->top_pos;
11805 /* This must come after we set COUNT. */
11806 UNBLOCK_INPUT;
11808 /* We unblock here so that arriving X events are processed. */
11810 /* Now move the window back to where it was "supposed to be".
11811 But don't do it if the gravity is negative.
11812 When the gravity is negative, this uses a position
11813 that is 3 pixels too low. Perhaps that's really the border width.
11815 Don't do this if the window has never been visible before,
11816 because the window manager may choose the position
11817 and we don't want to override it. */
11819 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
11820 && f->output_data.x->win_gravity == NorthWestGravity
11821 && previously_visible)
11823 Drawable rootw;
11824 int x, y;
11825 unsigned int width, height, border, depth;
11827 BLOCK_INPUT;
11829 /* On some window managers (such as FVWM) moving an existing
11830 window, even to the same place, causes the window manager
11831 to introduce an offset. This can cause the window to move
11832 to an unexpected location. Check the geometry (a little
11833 slow here) and then verify that the window is in the right
11834 place. If the window is not in the right place, move it
11835 there, and take the potential window manager hit. */
11836 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11837 &rootw, &x, &y, &width, &height, &border, &depth);
11839 if (original_left != x || original_top != y)
11840 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
11841 original_left, original_top);
11843 UNBLOCK_INPUT;
11846 XSETFRAME (frame, f);
11848 /* Wait until the frame is visible. Process X events until a
11849 MapNotify event has been seen, or until we think we won't get a
11850 MapNotify at all.. */
11851 for (count = input_signal_count + 10;
11852 input_signal_count < count && !FRAME_VISIBLE_P (f);)
11854 /* Force processing of queued events. */
11855 x_sync (f);
11857 /* Machines that do polling rather than SIGIO have been
11858 observed to go into a busy-wait here. So we'll fake an
11859 alarm signal to let the handler know that there's something
11860 to be read. We used to raise a real alarm, but it seems
11861 that the handler isn't always enabled here. This is
11862 probably a bug. */
11863 if (input_polling_used ())
11865 /* It could be confusing if a real alarm arrives while
11866 processing the fake one. Turn it off and let the
11867 handler reset it. */
11868 extern void poll_for_input_1 P_ ((void));
11869 int old_poll_suppress_count = poll_suppress_count;
11870 poll_suppress_count = 1;
11871 poll_for_input_1 ();
11872 poll_suppress_count = old_poll_suppress_count;
11875 /* See if a MapNotify event has been processed. */
11876 FRAME_SAMPLE_VISIBILITY (f);
11881 /* Change from mapped state to withdrawn state. */
11883 /* Make the frame visible (mapped and not iconified). */
11885 void
11886 x_make_frame_invisible (f)
11887 struct frame *f;
11889 Window window;
11891 #ifdef USE_X_TOOLKIT
11892 /* Use the frame's outermost window, not the one we normally draw on. */
11893 window = XtWindow (f->output_data.x->widget);
11894 #else /* not USE_X_TOOLKIT */
11895 window = FRAME_X_WINDOW (f);
11896 #endif /* not USE_X_TOOLKIT */
11898 /* Don't keep the highlight on an invisible frame. */
11899 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11900 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11902 #if 0/* This might add unreliability; I don't trust it -- rms. */
11903 if (! f->async_visible && ! f->async_iconified)
11904 return;
11905 #endif
11907 BLOCK_INPUT;
11909 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
11910 that the current position of the window is user-specified, rather than
11911 program-specified, so that when the window is mapped again, it will be
11912 placed at the same location, without forcing the user to position it
11913 by hand again (they have already done that once for this window.) */
11914 x_wm_set_size_hint (f, (long) 0, 1);
11916 #ifdef HAVE_X11R4
11918 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
11919 DefaultScreen (FRAME_X_DISPLAY (f))))
11921 UNBLOCK_INPUT_RESIGNAL;
11922 error ("Can't notify window manager of window withdrawal");
11924 #else /* ! defined (HAVE_X11R4) */
11926 /* Tell the window manager what we're going to do. */
11927 if (! EQ (Vx_no_window_manager, Qt))
11929 XEvent unmap;
11931 unmap.xunmap.type = UnmapNotify;
11932 unmap.xunmap.window = window;
11933 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
11934 unmap.xunmap.from_configure = False;
11935 if (! XSendEvent (FRAME_X_DISPLAY (f),
11936 DefaultRootWindow (FRAME_X_DISPLAY (f)),
11937 False,
11938 SubstructureRedirectMaskSubstructureNotifyMask,
11939 &unmap))
11941 UNBLOCK_INPUT_RESIGNAL;
11942 error ("Can't notify window manager of withdrawal");
11946 /* Unmap the window ourselves. Cheeky! */
11947 XUnmapWindow (FRAME_X_DISPLAY (f), window);
11948 #endif /* ! defined (HAVE_X11R4) */
11950 /* We can't distinguish this from iconification
11951 just by the event that we get from the server.
11952 So we can't win using the usual strategy of letting
11953 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
11954 and synchronize with the server to make sure we agree. */
11955 f->visible = 0;
11956 FRAME_ICONIFIED_P (f) = 0;
11957 f->async_visible = 0;
11958 f->async_iconified = 0;
11960 x_sync (f);
11962 UNBLOCK_INPUT;
11965 /* Change window state from mapped to iconified. */
11967 void
11968 x_iconify_frame (f)
11969 struct frame *f;
11971 int result;
11972 Lisp_Object type;
11974 /* Don't keep the highlight on an invisible frame. */
11975 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
11976 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
11978 if (f->async_iconified)
11979 return;
11981 BLOCK_INPUT;
11983 FRAME_SAMPLE_VISIBILITY (f);
11985 type = x_icon_type (f);
11986 if (!NILP (type))
11987 x_bitmap_icon (f, type);
11989 #ifdef USE_X_TOOLKIT
11991 if (! FRAME_VISIBLE_P (f))
11993 if (! EQ (Vx_no_window_manager, Qt))
11994 x_wm_set_window_state (f, IconicState);
11995 /* This was XtPopup, but that did nothing for an iconified frame. */
11996 XtMapWidget (f->output_data.x->widget);
11997 /* The server won't give us any event to indicate
11998 that an invisible frame was changed to an icon,
11999 so we have to record it here. */
12000 f->iconified = 1;
12001 f->visible = 1;
12002 f->async_iconified = 1;
12003 f->async_visible = 0;
12004 UNBLOCK_INPUT;
12005 return;
12008 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12009 XtWindow (f->output_data.x->widget),
12010 DefaultScreen (FRAME_X_DISPLAY (f)));
12011 UNBLOCK_INPUT;
12013 if (!result)
12014 error ("Can't notify window manager of iconification");
12016 f->async_iconified = 1;
12017 f->async_visible = 0;
12020 BLOCK_INPUT;
12021 XFlush (FRAME_X_DISPLAY (f));
12022 UNBLOCK_INPUT;
12023 #else /* not USE_X_TOOLKIT */
12025 /* Make sure the X server knows where the window should be positioned,
12026 in case the user deiconifies with the window manager. */
12027 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12028 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12030 /* Since we don't know which revision of X we're running, we'll use both
12031 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12033 /* X11R4: send a ClientMessage to the window manager using the
12034 WM_CHANGE_STATE type. */
12036 XEvent message;
12038 message.xclient.window = FRAME_X_WINDOW (f);
12039 message.xclient.type = ClientMessage;
12040 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12041 message.xclient.format = 32;
12042 message.xclient.data.l[0] = IconicState;
12044 if (! XSendEvent (FRAME_X_DISPLAY (f),
12045 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12046 False,
12047 SubstructureRedirectMask | SubstructureNotifyMask,
12048 &message))
12050 UNBLOCK_INPUT_RESIGNAL;
12051 error ("Can't notify window manager of iconification");
12055 /* X11R3: set the initial_state field of the window manager hints to
12056 IconicState. */
12057 x_wm_set_window_state (f, IconicState);
12059 if (!FRAME_VISIBLE_P (f))
12061 /* If the frame was withdrawn, before, we must map it. */
12062 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12065 f->async_iconified = 1;
12066 f->async_visible = 0;
12068 XFlush (FRAME_X_DISPLAY (f));
12069 UNBLOCK_INPUT;
12070 #endif /* not USE_X_TOOLKIT */
12073 /* Destroy the X window of frame F. */
12075 void
12076 x_destroy_window (f)
12077 struct frame *f;
12079 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12081 BLOCK_INPUT;
12083 /* If a display connection is dead, don't try sending more
12084 commands to the X server. */
12085 if (dpyinfo->display != 0)
12087 if (f->output_data.x->icon_desc != 0)
12088 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12089 #ifdef HAVE_X_I18N
12090 if (FRAME_XIC (f))
12091 free_frame_xic (f);
12092 #endif
12093 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->window_desc);
12094 #ifdef USE_X_TOOLKIT
12095 if (f->output_data.x->widget)
12096 XtDestroyWidget (f->output_data.x->widget);
12097 free_frame_menubar (f);
12098 #endif /* USE_X_TOOLKIT */
12100 unload_color (f, f->output_data.x->foreground_pixel);
12101 unload_color (f, f->output_data.x->background_pixel);
12102 unload_color (f, f->output_data.x->cursor_pixel);
12103 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12104 unload_color (f, f->output_data.x->border_pixel);
12105 unload_color (f, f->output_data.x->mouse_pixel);
12106 if (f->output_data.x->scroll_bar_background_pixel != -1)
12107 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12108 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12109 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12110 if (f->output_data.x->white_relief.allocated_p)
12111 unload_color (f, f->output_data.x->white_relief.pixel);
12112 if (f->output_data.x->black_relief.allocated_p)
12113 unload_color (f, f->output_data.x->black_relief.pixel);
12115 free_frame_faces (f);
12116 XFlush (FRAME_X_DISPLAY (f));
12119 if (f->output_data.x->saved_menu_event)
12120 xfree (f->output_data.x->saved_menu_event);
12122 xfree (f->output_data.x);
12123 f->output_data.x = 0;
12124 if (f == dpyinfo->x_focus_frame)
12125 dpyinfo->x_focus_frame = 0;
12126 if (f == dpyinfo->x_focus_event_frame)
12127 dpyinfo->x_focus_event_frame = 0;
12128 if (f == dpyinfo->x_highlight_frame)
12129 dpyinfo->x_highlight_frame = 0;
12131 dpyinfo->reference_count--;
12133 if (f == dpyinfo->mouse_face_mouse_frame)
12135 dpyinfo->mouse_face_beg_row
12136 = dpyinfo->mouse_face_beg_col = -1;
12137 dpyinfo->mouse_face_end_row
12138 = dpyinfo->mouse_face_end_col = -1;
12139 dpyinfo->mouse_face_window = Qnil;
12140 dpyinfo->mouse_face_deferred_gc = 0;
12141 dpyinfo->mouse_face_mouse_frame = 0;
12144 UNBLOCK_INPUT;
12147 /* Setting window manager hints. */
12149 /* Set the normal size hints for the window manager, for frame F.
12150 FLAGS is the flags word to use--or 0 meaning preserve the flags
12151 that the window now has.
12152 If USER_POSITION is nonzero, we set the USPosition
12153 flag (this is useful when FLAGS is 0). */
12155 void
12156 x_wm_set_size_hint (f, flags, user_position)
12157 struct frame *f;
12158 long flags;
12159 int user_position;
12161 XSizeHints size_hints;
12163 #ifdef USE_X_TOOLKIT
12164 Arg al[2];
12165 int ac = 0;
12166 Dimension widget_width, widget_height;
12167 Window window = XtWindow (f->output_data.x->widget);
12168 #else /* not USE_X_TOOLKIT */
12169 Window window = FRAME_X_WINDOW (f);
12170 #endif /* not USE_X_TOOLKIT */
12172 /* Setting PMaxSize caused various problems. */
12173 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12175 size_hints.x = f->output_data.x->left_pos;
12176 size_hints.y = f->output_data.x->top_pos;
12178 #ifdef USE_X_TOOLKIT
12179 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12180 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12181 XtGetValues (f->output_data.x->widget, al, ac);
12182 size_hints.height = widget_height;
12183 size_hints.width = widget_width;
12184 #else /* not USE_X_TOOLKIT */
12185 size_hints.height = PIXEL_HEIGHT (f);
12186 size_hints.width = PIXEL_WIDTH (f);
12187 #endif /* not USE_X_TOOLKIT */
12189 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12190 size_hints.height_inc = f->output_data.x->line_height;
12191 size_hints.max_width
12192 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12193 size_hints.max_height
12194 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12196 /* Calculate the base and minimum sizes.
12198 (When we use the X toolkit, we don't do it here.
12199 Instead we copy the values that the widgets are using, below.) */
12200 #ifndef USE_X_TOOLKIT
12202 int base_width, base_height;
12203 int min_rows = 0, min_cols = 0;
12205 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12206 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12208 check_frame_size (f, &min_rows, &min_cols);
12210 /* The window manager uses the base width hints to calculate the
12211 current number of rows and columns in the frame while
12212 resizing; min_width and min_height aren't useful for this
12213 purpose, since they might not give the dimensions for a
12214 zero-row, zero-column frame.
12216 We use the base_width and base_height members if we have
12217 them; otherwise, we set the min_width and min_height members
12218 to the size for a zero x zero frame. */
12220 #ifdef HAVE_X11R4
12221 size_hints.flags |= PBaseSize;
12222 size_hints.base_width = base_width;
12223 size_hints.base_height = base_height;
12224 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12225 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12226 #else
12227 size_hints.min_width = base_width;
12228 size_hints.min_height = base_height;
12229 #endif
12232 /* If we don't need the old flags, we don't need the old hint at all. */
12233 if (flags)
12235 size_hints.flags |= flags;
12236 goto no_read;
12238 #endif /* not USE_X_TOOLKIT */
12241 XSizeHints hints; /* Sometimes I hate X Windows... */
12242 long supplied_return;
12243 int value;
12245 #ifdef HAVE_X11R4
12246 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12247 &supplied_return);
12248 #else
12249 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12250 #endif
12252 #ifdef USE_X_TOOLKIT
12253 size_hints.base_height = hints.base_height;
12254 size_hints.base_width = hints.base_width;
12255 size_hints.min_height = hints.min_height;
12256 size_hints.min_width = hints.min_width;
12257 #endif
12259 if (flags)
12260 size_hints.flags |= flags;
12261 else
12263 if (value == 0)
12264 hints.flags = 0;
12265 if (hints.flags & PSize)
12266 size_hints.flags |= PSize;
12267 if (hints.flags & PPosition)
12268 size_hints.flags |= PPosition;
12269 if (hints.flags & USPosition)
12270 size_hints.flags |= USPosition;
12271 if (hints.flags & USSize)
12272 size_hints.flags |= USSize;
12276 #ifndef USE_X_TOOLKIT
12277 no_read:
12278 #endif
12280 #ifdef PWinGravity
12281 size_hints.win_gravity = f->output_data.x->win_gravity;
12282 size_hints.flags |= PWinGravity;
12284 if (user_position)
12286 size_hints.flags &= ~ PPosition;
12287 size_hints.flags |= USPosition;
12289 #endif /* PWinGravity */
12291 #ifdef HAVE_X11R4
12292 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12293 #else
12294 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12295 #endif
12298 /* Used for IconicState or NormalState */
12300 void
12301 x_wm_set_window_state (f, state)
12302 struct frame *f;
12303 int state;
12305 #ifdef USE_X_TOOLKIT
12306 Arg al[1];
12308 XtSetArg (al[0], XtNinitialState, state);
12309 XtSetValues (f->output_data.x->widget, al, 1);
12310 #else /* not USE_X_TOOLKIT */
12311 Window window = FRAME_X_WINDOW (f);
12313 f->output_data.x->wm_hints.flags |= StateHint;
12314 f->output_data.x->wm_hints.initial_state = state;
12316 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12317 #endif /* not USE_X_TOOLKIT */
12320 void
12321 x_wm_set_icon_pixmap (f, pixmap_id)
12322 struct frame *f;
12323 int pixmap_id;
12325 Pixmap icon_pixmap;
12327 #ifndef USE_X_TOOLKIT
12328 Window window = FRAME_X_WINDOW (f);
12329 #endif
12331 if (pixmap_id > 0)
12333 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12334 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12336 else
12338 /* It seems there is no way to turn off use of an icon pixmap.
12339 The following line does it, only if no icon has yet been created,
12340 for some window managers. But with mwm it crashes.
12341 Some people say it should clear the IconPixmapHint bit in this case,
12342 but that doesn't work, and the X consortium said it isn't the
12343 right thing at all. Since there is no way to win,
12344 best to explicitly give up. */
12345 #if 0
12346 f->output_data.x->wm_hints.icon_pixmap = None;
12347 #else
12348 return;
12349 #endif
12352 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12355 Arg al[1];
12356 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12357 XtSetValues (f->output_data.x->widget, al, 1);
12360 #else /* not USE_X_TOOLKIT */
12362 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12363 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12365 #endif /* not USE_X_TOOLKIT */
12368 void
12369 x_wm_set_icon_position (f, icon_x, icon_y)
12370 struct frame *f;
12371 int icon_x, icon_y;
12373 #ifdef USE_X_TOOLKIT
12374 Window window = XtWindow (f->output_data.x->widget);
12375 #else
12376 Window window = FRAME_X_WINDOW (f);
12377 #endif
12379 f->output_data.x->wm_hints.flags |= IconPositionHint;
12380 f->output_data.x->wm_hints.icon_x = icon_x;
12381 f->output_data.x->wm_hints.icon_y = icon_y;
12383 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12387 /***********************************************************************
12388 Fonts
12389 ***********************************************************************/
12391 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12393 struct font_info *
12394 x_get_font_info (f, font_idx)
12395 FRAME_PTR f;
12396 int font_idx;
12398 return (FRAME_X_FONT_TABLE (f) + font_idx);
12402 /* Return a list of names of available fonts matching PATTERN on frame
12403 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12404 to be listed. Frame F NULL means we have not yet created any
12405 frame on X, and consult the first display in x_display_list.
12406 MAXNAMES sets a limit on how many fonts to match. */
12408 Lisp_Object
12409 x_list_fonts (f, pattern, size, maxnames)
12410 FRAME_PTR f;
12411 Lisp_Object pattern;
12412 int size;
12413 int maxnames;
12415 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12416 Lisp_Object tem, second_best;
12417 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12418 int try_XLoadQueryFont = 0;
12419 int count;
12421 patterns = Fassoc (pattern, Valternate_fontname_alist);
12422 if (NILP (patterns))
12423 patterns = Fcons (pattern, Qnil);
12425 if (maxnames == 1 && !size)
12426 /* We can return any single font matching PATTERN. */
12427 try_XLoadQueryFont = 1;
12429 for (; CONSP (patterns); patterns = XCDR (patterns))
12431 int num_fonts;
12432 char **names = NULL;
12434 pattern = XCAR (patterns);
12435 /* See if we cached the result for this particular query.
12436 The cache is an alist of the form:
12437 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12439 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12440 key = Fcons (pattern, make_number (maxnames)),
12441 !NILP (list = Fassoc (key, tem))))
12443 list = Fcdr_safe (list);
12444 /* We have a cashed list. Don't have to get the list again. */
12445 goto label_cached;
12448 /* At first, put PATTERN in the cache. */
12450 BLOCK_INPUT;
12451 count = x_catch_errors (dpy);
12453 if (try_XLoadQueryFont)
12455 XFontStruct *font;
12456 unsigned long value;
12458 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12459 if (x_had_errors_p (dpy))
12461 /* This error is perhaps due to insufficient memory on X
12462 server. Let's just ignore it. */
12463 font = NULL;
12464 x_clear_errors (dpy);
12467 if (font
12468 && XGetFontProperty (font, XA_FONT, &value))
12470 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12471 int len = strlen (name);
12472 char *tmp;
12474 /* If DXPC (a Differential X Protocol Compressor)
12475 Ver.3.7 is running, XGetAtomName will return null
12476 string. We must avoid such a name. */
12477 if (len == 0)
12478 try_XLoadQueryFont = 0;
12479 else
12481 num_fonts = 1;
12482 names = (char **) alloca (sizeof (char *));
12483 /* Some systems only allow alloca assigned to a
12484 simple var. */
12485 tmp = (char *) alloca (len + 1); names[0] = tmp;
12486 bcopy (name, names[0], len + 1);
12487 XFree (name);
12490 else
12491 try_XLoadQueryFont = 0;
12493 if (font)
12494 XFreeFont (dpy, font);
12497 if (!try_XLoadQueryFont)
12499 /* We try at least 10 fonts because XListFonts will return
12500 auto-scaled fonts at the head. */
12501 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12502 &num_fonts);
12503 if (x_had_errors_p (dpy))
12505 /* This error is perhaps due to insufficient memory on X
12506 server. Let's just ignore it. */
12507 names = NULL;
12508 x_clear_errors (dpy);
12512 x_uncatch_errors (dpy, count);
12513 UNBLOCK_INPUT;
12515 if (names)
12517 int i;
12519 /* Make a list of all the fonts we got back.
12520 Store that in the font cache for the display. */
12521 for (i = 0; i < num_fonts; i++)
12523 int width = 0;
12524 char *p = names[i];
12525 int average_width = -1, dashes = 0;
12527 /* Count the number of dashes in NAMES[I]. If there are
12528 14 dashes, and the field value following 12th dash
12529 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12530 is usually too ugly to be used for editing. Let's
12531 ignore it. */
12532 while (*p)
12533 if (*p++ == '-')
12535 dashes++;
12536 if (dashes == 7) /* PIXEL_SIZE field */
12537 width = atoi (p);
12538 else if (dashes == 12) /* AVERAGE_WIDTH field */
12539 average_width = atoi (p);
12541 if (dashes < 14 || average_width != 0)
12543 tem = build_string (names[i]);
12544 if (NILP (Fassoc (tem, list)))
12546 if (STRINGP (Vx_pixel_size_width_font_regexp)
12547 && ((fast_c_string_match_ignore_case
12548 (Vx_pixel_size_width_font_regexp, names[i]))
12549 >= 0))
12550 /* We can set the value of PIXEL_SIZE to the
12551 width of this font. */
12552 list = Fcons (Fcons (tem, make_number (width)), list);
12553 else
12554 /* For the moment, width is not known. */
12555 list = Fcons (Fcons (tem, Qnil), list);
12559 if (!try_XLoadQueryFont)
12560 XFreeFontNames (names);
12563 /* Now store the result in the cache. */
12564 if (f != NULL)
12565 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12566 = Fcons (Fcons (key, list),
12567 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12569 label_cached:
12570 if (NILP (list)) continue; /* Try the remaining alternatives. */
12572 newlist = second_best = Qnil;
12573 /* Make a list of the fonts that have the right width. */
12574 for (; CONSP (list); list = XCDR (list))
12576 int found_size;
12578 tem = XCAR (list);
12580 if (!CONSP (tem) || NILP (XCAR (tem)))
12581 continue;
12582 if (!size)
12584 newlist = Fcons (XCAR (tem), newlist);
12585 continue;
12588 if (!INTEGERP (XCDR (tem)))
12590 /* Since we have not yet known the size of this font, we
12591 must try slow function call XLoadQueryFont. */
12592 XFontStruct *thisinfo;
12594 BLOCK_INPUT;
12595 count = x_catch_errors (dpy);
12596 thisinfo = XLoadQueryFont (dpy,
12597 XSTRING (XCAR (tem))->data);
12598 if (x_had_errors_p (dpy))
12600 /* This error is perhaps due to insufficient memory on X
12601 server. Let's just ignore it. */
12602 thisinfo = NULL;
12603 x_clear_errors (dpy);
12605 x_uncatch_errors (dpy, count);
12606 UNBLOCK_INPUT;
12608 if (thisinfo)
12610 XCDR (tem)
12611 = (thisinfo->min_bounds.width == 0
12612 ? make_number (0)
12613 : make_number (thisinfo->max_bounds.width));
12614 XFreeFont (dpy, thisinfo);
12616 else
12617 /* For unknown reason, the previous call of XListFont had
12618 returned a font which can't be opened. Record the size
12619 as 0 not to try to open it again. */
12620 XCDR (tem) = make_number (0);
12623 found_size = XINT (XCDR (tem));
12624 if (found_size == size)
12625 newlist = Fcons (XCAR (tem), newlist);
12626 else if (found_size > 0)
12628 if (NILP (second_best))
12629 second_best = tem;
12630 else if (found_size < size)
12632 if (XINT (XCDR (second_best)) > size
12633 || XINT (XCDR (second_best)) < found_size)
12634 second_best = tem;
12636 else
12638 if (XINT (XCDR (second_best)) > size
12639 && XINT (XCDR (second_best)) > found_size)
12640 second_best = tem;
12644 if (!NILP (newlist))
12645 break;
12646 else if (!NILP (second_best))
12648 newlist = Fcons (XCAR (second_best), Qnil);
12649 break;
12653 return newlist;
12657 #if GLYPH_DEBUG
12659 /* Check that FONT is valid on frame F. It is if it can be found in F's
12660 font table. */
12662 static void
12663 x_check_font (f, font)
12664 struct frame *f;
12665 XFontStruct *font;
12667 int i;
12668 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12670 xassert (font != NULL);
12672 for (i = 0; i < dpyinfo->n_fonts; i++)
12673 if (dpyinfo->font_table[i].name
12674 && font == dpyinfo->font_table[i].font)
12675 break;
12677 xassert (i < dpyinfo->n_fonts);
12680 #endif /* GLYPH_DEBUG != 0 */
12682 /* Set *W to the minimum width, *H to the minimum font height of FONT.
12683 Note: There are (broken) X fonts out there with invalid XFontStruct
12684 min_bounds contents. For example, handa@etl.go.jp reports that
12685 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
12686 have font->min_bounds.width == 0. */
12688 static INLINE void
12689 x_font_min_bounds (font, w, h)
12690 XFontStruct *font;
12691 int *w, *h;
12693 *h = FONT_HEIGHT (font);
12694 *w = font->min_bounds.width;
12696 /* Try to handle the case where FONT->min_bounds has invalid
12697 contents. Since the only font known to have invalid min_bounds
12698 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
12699 if (*w <= 0)
12700 *w = font->max_bounds.width;
12704 /* Compute the smallest character width and smallest font height over
12705 all fonts available on frame F. Set the members smallest_char_width
12706 and smallest_font_height in F's x_display_info structure to
12707 the values computed. Value is non-zero if smallest_font_height or
12708 smallest_char_width become smaller than they were before. */
12710 static int
12711 x_compute_min_glyph_bounds (f)
12712 struct frame *f;
12714 int i;
12715 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12716 XFontStruct *font;
12717 int old_width = dpyinfo->smallest_char_width;
12718 int old_height = dpyinfo->smallest_font_height;
12720 dpyinfo->smallest_font_height = 100000;
12721 dpyinfo->smallest_char_width = 100000;
12723 for (i = 0; i < dpyinfo->n_fonts; ++i)
12724 if (dpyinfo->font_table[i].name)
12726 struct font_info *fontp = dpyinfo->font_table + i;
12727 int w, h;
12729 font = (XFontStruct *) fontp->font;
12730 xassert (font != (XFontStruct *) ~0);
12731 x_font_min_bounds (font, &w, &h);
12733 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
12734 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
12737 xassert (dpyinfo->smallest_char_width > 0
12738 && dpyinfo->smallest_font_height > 0);
12740 return (dpyinfo->n_fonts == 1
12741 || dpyinfo->smallest_char_width < old_width
12742 || dpyinfo->smallest_font_height < old_height);
12746 /* Load font named FONTNAME of the size SIZE for frame F, and return a
12747 pointer to the structure font_info while allocating it dynamically.
12748 If SIZE is 0, load any size of font.
12749 If loading is failed, return NULL. */
12751 struct font_info *
12752 x_load_font (f, fontname, size)
12753 struct frame *f;
12754 register char *fontname;
12755 int size;
12757 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12758 Lisp_Object font_names;
12759 int count;
12761 /* Get a list of all the fonts that match this name. Once we
12762 have a list of matching fonts, we compare them against the fonts
12763 we already have by comparing names. */
12764 font_names = x_list_fonts (f, build_string (fontname), size, 1);
12766 if (!NILP (font_names))
12768 Lisp_Object tail;
12769 int i;
12771 for (i = 0; i < dpyinfo->n_fonts; i++)
12772 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
12773 if (dpyinfo->font_table[i].name
12774 && (!strcmp (dpyinfo->font_table[i].name,
12775 XSTRING (XCAR (tail))->data)
12776 || !strcmp (dpyinfo->font_table[i].full_name,
12777 XSTRING (XCAR (tail))->data)))
12778 return (dpyinfo->font_table + i);
12781 /* Load the font and add it to the table. */
12783 char *full_name;
12784 XFontStruct *font;
12785 struct font_info *fontp;
12786 unsigned long value;
12787 int i;
12789 /* If we have found fonts by x_list_font, load one of them. If
12790 not, we still try to load a font by the name given as FONTNAME
12791 because XListFonts (called in x_list_font) of some X server has
12792 a bug of not finding a font even if the font surely exists and
12793 is loadable by XLoadQueryFont. */
12794 if (size > 0 && !NILP (font_names))
12795 fontname = (char *) XSTRING (XCAR (font_names))->data;
12797 BLOCK_INPUT;
12798 count = x_catch_errors (FRAME_X_DISPLAY (f));
12799 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
12800 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12802 /* This error is perhaps due to insufficient memory on X
12803 server. Let's just ignore it. */
12804 font = NULL;
12805 x_clear_errors (FRAME_X_DISPLAY (f));
12807 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12808 UNBLOCK_INPUT;
12809 if (!font)
12810 return NULL;
12812 /* Find a free slot in the font table. */
12813 for (i = 0; i < dpyinfo->n_fonts; ++i)
12814 if (dpyinfo->font_table[i].name == NULL)
12815 break;
12817 /* If no free slot found, maybe enlarge the font table. */
12818 if (i == dpyinfo->n_fonts
12819 && dpyinfo->n_fonts == dpyinfo->font_table_size)
12821 int sz;
12822 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
12823 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
12824 dpyinfo->font_table
12825 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
12828 fontp = dpyinfo->font_table + i;
12829 if (i == dpyinfo->n_fonts)
12830 ++dpyinfo->n_fonts;
12832 /* Now fill in the slots of *FONTP. */
12833 BLOCK_INPUT;
12834 fontp->font = font;
12835 fontp->font_idx = i;
12836 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
12837 bcopy (fontname, fontp->name, strlen (fontname) + 1);
12839 /* Try to get the full name of FONT. Put it in FULL_NAME. */
12840 full_name = 0;
12841 if (XGetFontProperty (font, XA_FONT, &value))
12843 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
12844 char *p = name;
12845 int dashes = 0;
12847 /* Count the number of dashes in the "full name".
12848 If it is too few, this isn't really the font's full name,
12849 so don't use it.
12850 In X11R4, the fonts did not come with their canonical names
12851 stored in them. */
12852 while (*p)
12854 if (*p == '-')
12855 dashes++;
12856 p++;
12859 if (dashes >= 13)
12861 full_name = (char *) xmalloc (p - name + 1);
12862 bcopy (name, full_name, p - name + 1);
12865 XFree (name);
12868 if (full_name != 0)
12869 fontp->full_name = full_name;
12870 else
12871 fontp->full_name = fontp->name;
12873 fontp->size = font->max_bounds.width;
12874 fontp->height = FONT_HEIGHT (font);
12876 /* For some font, ascent and descent in max_bounds field is
12877 larger than the above value. */
12878 int max_height = font->max_bounds.ascent + font->max_bounds.descent;
12879 if (max_height > fontp->height)
12880 fontp->height = max_height;
12883 if (NILP (font_names))
12885 /* We come here because of a bug of XListFonts mentioned at
12886 the head of this block. Let's store this information in
12887 the cache for x_list_fonts. */
12888 Lisp_Object lispy_name = build_string (fontname);
12889 Lisp_Object lispy_full_name = build_string (fontp->full_name);
12891 XCDR (dpyinfo->name_list_element)
12892 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
12893 Fcons (Fcons (lispy_full_name,
12894 make_number (fontp->size)),
12895 Qnil)),
12896 XCDR (dpyinfo->name_list_element));
12897 if (full_name)
12898 XCDR (dpyinfo->name_list_element)
12899 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
12900 Fcons (Fcons (lispy_full_name,
12901 make_number (fontp->size)),
12902 Qnil)),
12903 XCDR (dpyinfo->name_list_element));
12906 /* The slot `encoding' specifies how to map a character
12907 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
12908 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
12909 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
12910 2:0xA020..0xFF7F). For the moment, we don't know which charset
12911 uses this font. So, we set information in fontp->encoding[1]
12912 which is never used by any charset. If mapping can't be
12913 decided, set FONT_ENCODING_NOT_DECIDED. */
12914 fontp->encoding[1]
12915 = (font->max_byte1 == 0
12916 /* 1-byte font */
12917 ? (font->min_char_or_byte2 < 0x80
12918 ? (font->max_char_or_byte2 < 0x80
12919 ? 0 /* 0x20..0x7F */
12920 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
12921 : 1) /* 0xA0..0xFF */
12922 /* 2-byte font */
12923 : (font->min_byte1 < 0x80
12924 ? (font->max_byte1 < 0x80
12925 ? (font->min_char_or_byte2 < 0x80
12926 ? (font->max_char_or_byte2 < 0x80
12927 ? 0 /* 0x2020..0x7F7F */
12928 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
12929 : 3) /* 0x20A0..0x7FFF */
12930 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
12931 : (font->min_char_or_byte2 < 0x80
12932 ? (font->max_char_or_byte2 < 0x80
12933 ? 2 /* 0xA020..0xFF7F */
12934 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
12935 : 1))); /* 0xA0A0..0xFFFF */
12937 fontp->baseline_offset
12938 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
12939 ? (long) value : 0);
12940 fontp->relative_compose
12941 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
12942 ? (long) value : 0);
12943 fontp->default_ascent
12944 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
12945 ? (long) value : 0);
12947 /* Set global flag fonts_changed_p to non-zero if the font loaded
12948 has a character with a smaller width than any other character
12949 before, or if the font loaded has a smalle>r height than any
12950 other font loaded before. If this happens, it will make a
12951 glyph matrix reallocation necessary. */
12952 fonts_changed_p = x_compute_min_glyph_bounds (f);
12953 UNBLOCK_INPUT;
12954 return fontp;
12959 /* Return a pointer to struct font_info of a font named FONTNAME for
12960 frame F. If no such font is loaded, return NULL. */
12962 struct font_info *
12963 x_query_font (f, fontname)
12964 struct frame *f;
12965 register char *fontname;
12967 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12968 int i;
12970 for (i = 0; i < dpyinfo->n_fonts; i++)
12971 if (dpyinfo->font_table[i].name
12972 && (!strcmp (dpyinfo->font_table[i].name, fontname)
12973 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
12974 return (dpyinfo->font_table + i);
12975 return NULL;
12979 /* Find a CCL program for a font specified by FONTP, and set the member
12980 `encoder' of the structure. */
12982 void
12983 x_find_ccl_program (fontp)
12984 struct font_info *fontp;
12986 Lisp_Object list, elt;
12988 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
12990 elt = XCAR (list);
12991 if (CONSP (elt)
12992 && STRINGP (XCAR (elt))
12993 && (fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
12994 >= 0))
12995 break;
12997 if (! NILP (list))
12999 struct ccl_program *ccl
13000 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13002 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13003 xfree (ccl);
13004 else
13005 fontp->font_encoder = ccl;
13011 /***********************************************************************
13012 Initialization
13013 ***********************************************************************/
13015 #ifdef USE_X_TOOLKIT
13016 static XrmOptionDescRec emacs_options[] = {
13017 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13018 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13020 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13021 XrmoptionSepArg, NULL},
13022 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13024 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13025 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13026 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13027 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13028 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13029 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13030 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13032 #endif /* USE_X_TOOLKIT */
13034 static int x_initialized;
13036 #ifdef MULTI_KBOARD
13037 /* Test whether two display-name strings agree up to the dot that separates
13038 the screen number from the server number. */
13039 static int
13040 same_x_server (name1, name2)
13041 char *name1, *name2;
13043 int seen_colon = 0;
13044 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13045 int system_name_length = strlen (system_name);
13046 int length_until_period = 0;
13048 while (system_name[length_until_period] != 0
13049 && system_name[length_until_period] != '.')
13050 length_until_period++;
13052 /* Treat `unix' like an empty host name. */
13053 if (! strncmp (name1, "unix:", 5))
13054 name1 += 4;
13055 if (! strncmp (name2, "unix:", 5))
13056 name2 += 4;
13057 /* Treat this host's name like an empty host name. */
13058 if (! strncmp (name1, system_name, system_name_length)
13059 && name1[system_name_length] == ':')
13060 name1 += system_name_length;
13061 if (! strncmp (name2, system_name, system_name_length)
13062 && name2[system_name_length] == ':')
13063 name2 += system_name_length;
13064 /* Treat this host's domainless name like an empty host name. */
13065 if (! strncmp (name1, system_name, length_until_period)
13066 && name1[length_until_period] == ':')
13067 name1 += length_until_period;
13068 if (! strncmp (name2, system_name, length_until_period)
13069 && name2[length_until_period] == ':')
13070 name2 += length_until_period;
13072 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13074 if (*name1 == ':')
13075 seen_colon++;
13076 if (seen_colon && *name1 == '.')
13077 return 1;
13079 return (seen_colon
13080 && (*name1 == '.' || *name1 == '\0')
13081 && (*name2 == '.' || *name2 == '\0'));
13083 #endif
13085 struct x_display_info *
13086 x_term_init (display_name, xrm_option, resource_name)
13087 Lisp_Object display_name;
13088 char *xrm_option;
13089 char *resource_name;
13091 int connection;
13092 Display *dpy;
13093 struct x_display_info *dpyinfo;
13094 XrmDatabase xrdb;
13096 BLOCK_INPUT;
13098 if (!x_initialized)
13100 x_initialize ();
13101 x_initialized = 1;
13104 #ifdef USE_X_TOOLKIT
13105 /* weiner@footloose.sps.mot.com reports that this causes
13106 errors with X11R5:
13107 X protocol error: BadAtom (invalid Atom parameter)
13108 on protocol request 18skiloaf.
13109 So let's not use it until R6. */
13110 #ifdef HAVE_X11XTR6
13111 XtSetLanguageProc (NULL, NULL, NULL);
13112 #endif
13115 int argc = 0;
13116 char *argv[3];
13118 argv[0] = "";
13119 argc = 1;
13120 if (xrm_option)
13122 argv[argc++] = "-xrm";
13123 argv[argc++] = xrm_option;
13125 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13126 resource_name, EMACS_CLASS,
13127 emacs_options, XtNumber (emacs_options),
13128 &argc, argv);
13130 #ifdef HAVE_X11XTR6
13131 /* I think this is to compensate for XtSetLanguageProc. */
13132 fixup_locale ();
13133 #endif
13136 #else /* not USE_X_TOOLKIT */
13137 #ifdef HAVE_X11R5
13138 XSetLocaleModifiers ("");
13139 #endif
13140 dpy = XOpenDisplay (XSTRING (display_name)->data);
13141 #endif /* not USE_X_TOOLKIT */
13143 /* Detect failure. */
13144 if (dpy == 0)
13146 UNBLOCK_INPUT;
13147 return 0;
13150 /* We have definitely succeeded. Record the new connection. */
13152 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13154 #ifdef MULTI_KBOARD
13156 struct x_display_info *share;
13157 Lisp_Object tail;
13159 for (share = x_display_list, tail = x_display_name_list; share;
13160 share = share->next, tail = XCDR (tail))
13161 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13162 XSTRING (display_name)->data))
13163 break;
13164 if (share)
13165 dpyinfo->kboard = share->kboard;
13166 else
13168 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13169 init_kboard (dpyinfo->kboard);
13170 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13172 char *vendor = ServerVendor (dpy);
13173 UNBLOCK_INPUT;
13174 dpyinfo->kboard->Vsystem_key_alist
13175 = call1 (Qvendor_specific_keysyms,
13176 build_string (vendor ? vendor : ""));
13177 BLOCK_INPUT;
13180 dpyinfo->kboard->next_kboard = all_kboards;
13181 all_kboards = dpyinfo->kboard;
13182 /* Don't let the initial kboard remain current longer than necessary.
13183 That would cause problems if a file loaded on startup tries to
13184 prompt in the mini-buffer. */
13185 if (current_kboard == initial_kboard)
13186 current_kboard = dpyinfo->kboard;
13188 dpyinfo->kboard->reference_count++;
13190 #endif
13192 /* Put this display on the chain. */
13193 dpyinfo->next = x_display_list;
13194 x_display_list = dpyinfo;
13196 /* Put it on x_display_name_list as well, to keep them parallel. */
13197 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13198 x_display_name_list);
13199 dpyinfo->name_list_element = XCAR (x_display_name_list);
13201 dpyinfo->display = dpy;
13203 #if 0
13204 XSetAfterFunction (x_current_display, x_trace_wire);
13205 #endif /* ! 0 */
13207 dpyinfo->x_id_name
13208 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13209 + STRING_BYTES (XSTRING (Vsystem_name))
13210 + 2);
13211 sprintf (dpyinfo->x_id_name, "%s@%s",
13212 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13214 /* Figure out which modifier bits mean what. */
13215 x_find_modifier_meanings (dpyinfo);
13217 /* Get the scroll bar cursor. */
13218 dpyinfo->vertical_scroll_bar_cursor
13219 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13221 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13222 resource_name, EMACS_CLASS);
13223 #ifdef HAVE_XRMSETDATABASE
13224 XrmSetDatabase (dpyinfo->display, xrdb);
13225 #else
13226 dpyinfo->display->db = xrdb;
13227 #endif
13228 /* Put the rdb where we can find it in a way that works on
13229 all versions. */
13230 dpyinfo->xrdb = xrdb;
13232 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13233 DefaultScreen (dpyinfo->display));
13234 select_visual (dpyinfo);
13235 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13236 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13237 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13238 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13239 dpyinfo->grabbed = 0;
13240 dpyinfo->reference_count = 0;
13241 dpyinfo->icon_bitmap_id = -1;
13242 dpyinfo->font_table = NULL;
13243 dpyinfo->n_fonts = 0;
13244 dpyinfo->font_table_size = 0;
13245 dpyinfo->bitmaps = 0;
13246 dpyinfo->bitmaps_size = 0;
13247 dpyinfo->bitmaps_last = 0;
13248 dpyinfo->scratch_cursor_gc = 0;
13249 dpyinfo->mouse_face_mouse_frame = 0;
13250 dpyinfo->mouse_face_deferred_gc = 0;
13251 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13252 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13253 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13254 dpyinfo->mouse_face_window = Qnil;
13255 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13256 dpyinfo->mouse_face_defer = 0;
13257 dpyinfo->x_focus_frame = 0;
13258 dpyinfo->x_focus_event_frame = 0;
13259 dpyinfo->x_highlight_frame = 0;
13260 dpyinfo->image_cache = make_image_cache ();
13262 /* See if a private colormap is requested. */
13263 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13265 if (dpyinfo->visual->class == PseudoColor)
13267 Lisp_Object value;
13268 value = display_x_get_resource (dpyinfo,
13269 build_string ("privateColormap"),
13270 build_string ("PrivateColormap"),
13271 Qnil, Qnil);
13272 if (STRINGP (value)
13273 && (!strcmp (XSTRING (value)->data, "true")
13274 || !strcmp (XSTRING (value)->data, "on")))
13275 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13278 else
13279 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13280 dpyinfo->visual, AllocNone);
13283 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13284 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13285 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13286 dpyinfo->resy = pixels * 25.4 / mm;
13287 pixels = DisplayWidth (dpyinfo->display, screen_number);
13288 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13289 dpyinfo->resx = pixels * 25.4 / mm;
13292 dpyinfo->Xatom_wm_protocols
13293 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13294 dpyinfo->Xatom_wm_take_focus
13295 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13296 dpyinfo->Xatom_wm_save_yourself
13297 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13298 dpyinfo->Xatom_wm_delete_window
13299 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13300 dpyinfo->Xatom_wm_change_state
13301 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13302 dpyinfo->Xatom_wm_configure_denied
13303 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13304 dpyinfo->Xatom_wm_window_moved
13305 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13306 dpyinfo->Xatom_editres
13307 = XInternAtom (dpyinfo->display, "Editres", False);
13308 dpyinfo->Xatom_CLIPBOARD
13309 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13310 dpyinfo->Xatom_TIMESTAMP
13311 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13312 dpyinfo->Xatom_TEXT
13313 = XInternAtom (dpyinfo->display, "TEXT", False);
13314 dpyinfo->Xatom_COMPOUND_TEXT
13315 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13316 dpyinfo->Xatom_DELETE
13317 = XInternAtom (dpyinfo->display, "DELETE", False);
13318 dpyinfo->Xatom_MULTIPLE
13319 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13320 dpyinfo->Xatom_INCR
13321 = XInternAtom (dpyinfo->display, "INCR", False);
13322 dpyinfo->Xatom_EMACS_TMP
13323 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13324 dpyinfo->Xatom_TARGETS
13325 = XInternAtom (dpyinfo->display, "TARGETS", False);
13326 dpyinfo->Xatom_NULL
13327 = XInternAtom (dpyinfo->display, "NULL", False);
13328 dpyinfo->Xatom_ATOM_PAIR
13329 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13330 /* For properties of font. */
13331 dpyinfo->Xatom_PIXEL_SIZE
13332 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13333 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13334 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13335 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13336 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13337 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13338 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13340 /* Ghostscript support. */
13341 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13342 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13344 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13345 False);
13347 dpyinfo->cut_buffers_initialized = 0;
13349 connection = ConnectionNumber (dpyinfo->display);
13350 dpyinfo->connection = connection;
13353 char null_bits[1];
13355 null_bits[0] = 0x00;
13357 dpyinfo->null_pixel
13358 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13359 null_bits, 1, 1, (long) 0, (long) 0,
13364 extern int gray_bitmap_width, gray_bitmap_height;
13365 extern unsigned char *gray_bitmap_bits;
13366 dpyinfo->gray
13367 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13368 gray_bitmap_bits,
13369 gray_bitmap_width, gray_bitmap_height,
13370 (unsigned long) 1, (unsigned long) 0, 1);
13373 #ifdef HAVE_X_I18N
13374 xim_initialize (dpyinfo, resource_name);
13375 #endif
13377 #ifdef subprocesses
13378 /* This is only needed for distinguishing keyboard and process input. */
13379 if (connection != 0)
13380 add_keyboard_wait_descriptor (connection);
13381 #endif
13383 #ifndef F_SETOWN_BUG
13384 #ifdef F_SETOWN
13385 #ifdef F_SETOWN_SOCK_NEG
13386 /* stdin is a socket here */
13387 fcntl (connection, F_SETOWN, -getpid ());
13388 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13389 fcntl (connection, F_SETOWN, getpid ());
13390 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13391 #endif /* ! defined (F_SETOWN) */
13392 #endif /* F_SETOWN_BUG */
13394 #ifdef SIGIO
13395 if (interrupt_input)
13396 init_sigio (connection);
13397 #endif /* ! defined (SIGIO) */
13399 #ifdef USE_LUCID
13400 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13401 /* Make sure that we have a valid font for dialog boxes
13402 so that Xt does not crash. */
13404 Display *dpy = dpyinfo->display;
13405 XrmValue d, fr, to;
13406 Font font;
13407 int count;
13409 d.addr = (XPointer)&dpy;
13410 d.size = sizeof (Display *);
13411 fr.addr = XtDefaultFont;
13412 fr.size = sizeof (XtDefaultFont);
13413 to.size = sizeof (Font *);
13414 to.addr = (XPointer)&font;
13415 count = x_catch_errors (dpy);
13416 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13417 abort ();
13418 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13419 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13420 x_uncatch_errors (dpy, count);
13422 #endif
13423 #endif
13425 /* See if we should run in synchronous mode. This is useful
13426 for debugging X code. */
13428 Lisp_Object value;
13429 value = display_x_get_resource (dpyinfo,
13430 build_string ("synchronous"),
13431 build_string ("Synchronous"),
13432 Qnil, Qnil);
13433 if (STRINGP (value)
13434 && (!strcmp (XSTRING (value)->data, "true")
13435 || !strcmp (XSTRING (value)->data, "on")))
13436 XSynchronize (dpyinfo->display, True);
13439 UNBLOCK_INPUT;
13441 return dpyinfo;
13444 /* Get rid of display DPYINFO, assuming all frames are already gone,
13445 and without sending any more commands to the X server. */
13447 void
13448 x_delete_display (dpyinfo)
13449 struct x_display_info *dpyinfo;
13451 delete_keyboard_wait_descriptor (dpyinfo->connection);
13453 /* Discard this display from x_display_name_list and x_display_list.
13454 We can't use Fdelq because that can quit. */
13455 if (! NILP (x_display_name_list)
13456 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13457 x_display_name_list = XCDR (x_display_name_list);
13458 else
13460 Lisp_Object tail;
13462 tail = x_display_name_list;
13463 while (CONSP (tail) && CONSP (XCDR (tail)))
13465 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13467 XCDR (tail) = XCDR (XCDR (tail));
13468 break;
13470 tail = XCDR (tail);
13474 if (next_noop_dpyinfo == dpyinfo)
13475 next_noop_dpyinfo = dpyinfo->next;
13477 if (x_display_list == dpyinfo)
13478 x_display_list = dpyinfo->next;
13479 else
13481 struct x_display_info *tail;
13483 for (tail = x_display_list; tail; tail = tail->next)
13484 if (tail->next == dpyinfo)
13485 tail->next = tail->next->next;
13488 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13489 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13490 XrmDestroyDatabase (dpyinfo->xrdb);
13491 #endif
13492 #endif
13493 #ifdef MULTI_KBOARD
13494 if (--dpyinfo->kboard->reference_count == 0)
13495 delete_kboard (dpyinfo->kboard);
13496 #endif
13497 #ifdef HAVE_X_I18N
13498 if (dpyinfo->xim)
13499 xim_close_dpy (dpyinfo);
13500 #endif
13502 xfree (dpyinfo->font_table);
13503 xfree (dpyinfo->x_id_name);
13504 xfree (dpyinfo);
13507 /* Set up use of X before we make the first connection. */
13509 static struct redisplay_interface x_redisplay_interface =
13511 x_produce_glyphs,
13512 x_write_glyphs,
13513 x_insert_glyphs,
13514 x_clear_end_of_line,
13515 x_scroll_run,
13516 x_after_update_window_line,
13517 x_update_window_begin,
13518 x_update_window_end,
13519 XTcursor_to,
13520 x_flush,
13521 x_clear_mouse_face,
13522 x_get_glyph_overhangs,
13523 x_fix_overlapping_area
13526 void
13527 x_initialize ()
13529 rif = &x_redisplay_interface;
13531 clear_frame_hook = x_clear_frame;
13532 ins_del_lines_hook = x_ins_del_lines;
13533 change_line_highlight_hook = x_change_line_highlight;
13534 delete_glyphs_hook = x_delete_glyphs;
13535 ring_bell_hook = XTring_bell;
13536 reset_terminal_modes_hook = XTreset_terminal_modes;
13537 set_terminal_modes_hook = XTset_terminal_modes;
13538 update_begin_hook = x_update_begin;
13539 update_end_hook = x_update_end;
13540 set_terminal_window_hook = XTset_terminal_window;
13541 read_socket_hook = XTread_socket;
13542 frame_up_to_date_hook = XTframe_up_to_date;
13543 reassert_line_highlight_hook = XTreassert_line_highlight;
13544 mouse_position_hook = XTmouse_position;
13545 frame_rehighlight_hook = XTframe_rehighlight;
13546 frame_raise_lower_hook = XTframe_raise_lower;
13547 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13548 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13549 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13550 judge_scroll_bars_hook = XTjudge_scroll_bars;
13551 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13553 scroll_region_ok = 1; /* we'll scroll partial frames */
13554 char_ins_del_ok = 0; /* just as fast to write the line */
13555 line_ins_del_ok = 1; /* we'll just blt 'em */
13556 fast_clear_end_of_line = 1; /* X does this well */
13557 memory_below_frame = 0; /* we don't remember what scrolls
13558 off the bottom */
13559 baud_rate = 19200;
13561 x_noop_count = 0;
13562 last_tool_bar_item = -1;
13563 any_help_event_p = 0;
13565 /* Try to use interrupt input; if we can't, then start polling. */
13566 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13568 #ifdef USE_X_TOOLKIT
13569 XtToolkitInitialize ();
13570 Xt_app_con = XtCreateApplicationContext ();
13571 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13573 /* Install an asynchronous timer that processes Xt timeout events
13574 every 0.1s. This is necessary because some widget sets use
13575 timeouts internally, for example the LessTif menu bar, or the
13576 Xaw3d scroll bar. When Xt timouts aren't processed, these
13577 widgets don't behave normally. */
13579 EMACS_TIME interval;
13580 EMACS_SET_SECS_USECS (interval, 0, 100000);
13581 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13583 #endif
13585 #if USE_TOOLKIT_SCROLL_BARS
13586 xaw3d_arrow_scroll = False;
13587 xaw3d_pick_top = True;
13588 #endif
13590 /* Note that there is no real way portable across R3/R4 to get the
13591 original error handler. */
13592 XSetErrorHandler (x_error_handler);
13593 XSetIOErrorHandler (x_io_error_quitter);
13595 /* Disable Window Change signals; they are handled by X events. */
13596 #ifdef SIGWINCH
13597 signal (SIGWINCH, SIG_DFL);
13598 #endif /* ! defined (SIGWINCH) */
13600 signal (SIGPIPE, x_connection_signal);
13604 void
13605 syms_of_xterm ()
13607 staticpro (&x_error_message_string);
13608 x_error_message_string = Qnil;
13610 staticpro (&x_display_name_list);
13611 x_display_name_list = Qnil;
13613 staticpro (&last_mouse_scroll_bar);
13614 last_mouse_scroll_bar = Qnil;
13616 staticpro (&Qvendor_specific_keysyms);
13617 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13619 staticpro (&last_mouse_press_frame);
13620 last_mouse_press_frame = Qnil;
13622 help_echo = Qnil;
13623 staticpro (&help_echo);
13624 help_echo_object = Qnil;
13625 staticpro (&help_echo_object);
13626 help_echo_window = Qnil;
13627 staticpro (&help_echo_window);
13628 previous_help_echo = Qnil;
13629 staticpro (&previous_help_echo);
13630 help_echo_pos = -1;
13632 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13633 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13634 For example, if a block cursor is over a tab, it will be drawn as\n\
13635 wide as that tab on the display.");
13636 x_stretch_cursor_p = 0;
13638 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
13639 "If not nil, Emacs uses toolkit scroll bars.");
13640 #if USE_TOOLKIT_SCROLL_BARS
13641 x_toolkit_scroll_bars_p = 1;
13642 #else
13643 x_toolkit_scroll_bars_p = 0;
13644 #endif
13646 staticpro (&last_mouse_motion_frame);
13647 last_mouse_motion_frame = Qnil;
13650 #endif /* not HAVE_X_WINDOWS */