(x_set_mouse_face_gc): If the last use mouse face
[emacs.git] / src / xterm.c
blob1ad1fd4d1f99f31b796141d86fec88db65ecd48c
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "coding.h"
72 #include "ccl.h"
73 #include "frame.h"
74 #include "dispextern.h"
75 #include "fontset.h"
76 #include "termhooks.h"
77 #include "termopts.h"
78 #include "termchar.h"
79 #if 0
80 #include "sink.h"
81 #include "sinkmask.h"
82 #endif /* ! 0 */
83 #include "gnu.h"
84 #include "disptab.h"
85 #include "buffer.h"
86 #include "window.h"
87 #include "keyboard.h"
88 #include "intervals.h"
89 #include "process.h"
90 #include "atimer.h"
92 #ifdef USE_X_TOOLKIT
93 #include <X11/Shell.h>
94 #endif
96 #ifdef HAVE_SYS_TIME_H
97 #include <sys/time.h>
98 #endif
99 #ifdef HAVE_UNISTD_H
100 #include <unistd.h>
101 #endif
103 #ifdef USE_X_TOOLKIT
105 extern void free_frame_menubar P_ ((struct frame *));
106 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
107 int));
109 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
110 #define HACK_EDITRES
111 extern void _XEditResCheckMessages ();
112 #endif /* not NO_EDITRES */
114 /* Include toolkit specific headers for the scroll bar widget. */
116 #ifdef USE_TOOLKIT_SCROLL_BARS
117 #if defined USE_MOTIF
118 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
119 #include <Xm/ScrollBar.h>
120 #include <Xm/ScrollBarP.h>
121 #else /* !USE_MOTIF i.e. use Xaw */
123 #ifdef HAVE_XAW3D
124 #include <X11/Xaw3d/Simple.h>
125 #include <X11/Xaw3d/Scrollbar.h>
126 #define ARROW_SCROLLBAR
127 #include <X11/Xaw3d/ScrollbarP.h>
128 #else /* !HAVE_XAW3D */
129 #include <X11/Xaw/Simple.h>
130 #include <X11/Xaw/Scrollbar.h>
131 #endif /* !HAVE_XAW3D */
132 #ifndef XtNpickTop
133 #define XtNpickTop "pickTop"
134 #endif /* !XtNpickTop */
135 #endif /* !USE_MOTIF */
136 #endif /* USE_TOOLKIT_SCROLL_BARS */
138 #endif /* USE_X_TOOLKIT */
140 #ifndef USE_X_TOOLKIT
141 #define x_any_window_to_frame x_window_to_frame
142 #define x_top_window_to_frame x_window_to_frame
143 #endif
145 #ifdef USE_X_TOOLKIT
146 #include "widget.h"
147 #ifndef XtNinitialState
148 #define XtNinitialState "initialState"
149 #endif
150 #endif
152 #ifndef min
153 #define min(a,b) ((a) < (b) ? (a) : (b))
154 #endif
155 #ifndef max
156 #define max(a,b) ((a) > (b) ? (a) : (b))
157 #endif
159 #define abs(x) ((x) < 0 ? -(x) : (x))
161 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
164 /* Bitmaps for truncated lines. */
166 enum bitmap_type
168 NO_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
177 /* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
180 #define zv_width 8
181 #define zv_height 8
182 static unsigned char zv_bits[] = {
183 0x00, 0x00, 0x1e, 0x1e, 0x1e, 0x1e, 0x00, 0x00};
185 /* An arrow like this: `<-'. */
187 #define left_width 8
188 #define left_height 8
189 static unsigned char left_bits[] = {
190 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
192 /* Right truncation arrow bitmap `->'. */
194 #define right_width 8
195 #define right_height 8
196 static unsigned char right_bits[] = {
197 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
199 /* Marker for continued lines. */
201 #define continued_width 8
202 #define continued_height 8
203 static unsigned char continued_bits[] = {
204 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
206 /* Marker for continuation lines. */
208 #define continuation_width 8
209 #define continuation_height 8
210 static unsigned char continuation_bits[] = {
211 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
213 /* Overlay arrow bitmap. */
215 #if 0
216 /* A bomb. */
217 #define ov_width 8
218 #define ov_height 8
219 static unsigned char ov_bits[] = {
220 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
221 #else
222 /* A triangular arrow. */
223 #define ov_width 8
224 #define ov_height 8
225 static unsigned char ov_bits[] = {
226 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
228 #endif
230 extern Lisp_Object Qhelp_echo;
233 /* Non-nil means Emacs uses toolkit scroll bars. */
235 Lisp_Object Vx_toolkit_scroll_bars;
237 /* If a string, XTread_socket generates an event to display that string.
238 (The display is done in read_char.) */
240 static Lisp_Object help_echo;
241 static Lisp_Object help_echo_window;
242 static Lisp_Object help_echo_object;
243 static int help_echo_pos;
245 /* Temporary variable for XTread_socket. */
247 static Lisp_Object previous_help_echo;
249 /* Non-zero means that a HELP_EVENT has been generated since Emacs
250 start. */
252 static int any_help_event_p;
254 /* Non-zero means draw block and hollow cursor as wide as the glyph
255 under it. For example, if a block cursor is over a tab, it will be
256 drawn as wide as that tab on the display. */
258 int x_stretch_cursor_p;
260 /* This is a chain of structures for all the X displays currently in
261 use. */
263 struct x_display_info *x_display_list;
265 /* This is a list of cons cells, each of the form (NAME
266 . FONT-LIST-CACHE), one for each element of x_display_list and in
267 the same order. NAME is the name of the frame. FONT-LIST-CACHE
268 records previous values returned by x-list-fonts. */
270 Lisp_Object x_display_name_list;
272 /* Frame being updated by update_frame. This is declared in term.c.
273 This is set by update_begin and looked at by all the XT functions.
274 It is zero while not inside an update. In that case, the XT
275 functions assume that `selected_frame' is the frame to apply to. */
277 extern struct frame *updating_frame;
279 extern int waiting_for_input;
281 /* This is a frame waiting to be auto-raised, within XTread_socket. */
283 struct frame *pending_autoraise_frame;
285 #ifdef USE_X_TOOLKIT
286 /* The application context for Xt use. */
287 XtAppContext Xt_app_con;
288 static String Xt_default_resources[] = {0};
289 #endif /* USE_X_TOOLKIT */
291 /* Nominal cursor position -- where to draw output.
292 HPOS and VPOS are window relative glyph matrix coordinates.
293 X and Y are window relative pixel coordinates. */
295 struct cursor_pos output_cursor;
297 /* Non-zero means user is interacting with a toolkit scroll bar. */
299 static int toolkit_scroll_bar_interaction;
301 /* Mouse movement.
303 Formerly, we used PointerMotionHintMask (in standard_event_mask)
304 so that we would have to call XQueryPointer after each MotionNotify
305 event to ask for another such event. However, this made mouse tracking
306 slow, and there was a bug that made it eventually stop.
308 Simply asking for MotionNotify all the time seems to work better.
310 In order to avoid asking for motion events and then throwing most
311 of them away or busy-polling the server for mouse positions, we ask
312 the server for pointer motion hints. This means that we get only
313 one event per group of mouse movements. "Groups" are delimited by
314 other kinds of events (focus changes and button clicks, for
315 example), or by XQueryPointer calls; when one of these happens, we
316 get another MotionNotify event the next time the mouse moves. This
317 is at least as efficient as getting motion events when mouse
318 tracking is on, and I suspect only negligibly worse when tracking
319 is off. */
321 /* Where the mouse was last time we reported a mouse event. */
323 FRAME_PTR last_mouse_frame;
324 static XRectangle last_mouse_glyph;
325 static Lisp_Object last_mouse_press_frame;
327 /* The scroll bar in which the last X motion event occurred.
329 If the last X motion event occurred in a scroll bar, we set this so
330 XTmouse_position can know whether to report a scroll bar motion or
331 an ordinary motion.
333 If the last X motion event didn't occur in a scroll bar, we set
334 this to Qnil, to tell XTmouse_position to return an ordinary motion
335 event. */
337 static Lisp_Object last_mouse_scroll_bar;
339 /* This is a hack. We would really prefer that XTmouse_position would
340 return the time associated with the position it returns, but there
341 doesn't seem to be any way to wrest the time-stamp from the server
342 along with the position query. So, we just keep track of the time
343 of the last movement we received, and return that in hopes that
344 it's somewhat accurate. */
346 static Time last_mouse_movement_time;
348 /* Incremented by XTread_socket whenever it really tries to read
349 events. */
351 #ifdef __STDC__
352 static int volatile input_signal_count;
353 #else
354 static int input_signal_count;
355 #endif
357 /* Used locally within XTread_socket. */
359 static int x_noop_count;
361 /* Initial values of argv and argc. */
363 extern char **initial_argv;
364 extern int initial_argc;
366 extern Lisp_Object Vcommand_line_args, Vsystem_name;
368 /* Tells if a window manager is present or not. */
370 extern Lisp_Object Vx_no_window_manager;
372 extern Lisp_Object Qface, Qmouse_face;
374 extern int errno;
376 /* A mask of extra modifier bits to put into every keyboard char. */
378 extern int extra_keyboard_modifiers;
380 static Lisp_Object Qvendor_specific_keysyms;
382 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
383 extern Lisp_Object x_icon_type P_ ((struct frame *));
386 /* Enumeration for overriding/changing the face to use for drawing
387 glyphs in x_draw_glyphs. */
389 enum draw_glyphs_face
391 DRAW_NORMAL_TEXT,
392 DRAW_INVERSE_VIDEO,
393 DRAW_CURSOR,
394 DRAW_MOUSE_FACE,
395 DRAW_IMAGE_RAISED,
396 DRAW_IMAGE_SUNKEN
399 static const XColor *x_color_cells P_ ((struct frame *, int *));
400 static void x_update_window_end P_ ((struct window *, int, int));
401 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
402 void x_delete_display P_ ((struct x_display_info *));
403 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
404 unsigned));
405 static int fast_find_position P_ ((struct window *, int, int *, int *,
406 int *, int *));
407 static void set_output_cursor P_ ((struct cursor_pos *));
408 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
409 int *, int *, int *));
410 static void note_mode_line_highlight P_ ((struct window *, int, int));
411 static void note_mouse_highlight P_ ((struct frame *, int, int));
412 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
413 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
414 static void show_mouse_face P_ ((struct x_display_info *,
415 enum draw_glyphs_face));
416 static int x_io_error_quitter P_ ((Display *));
417 int x_catch_errors P_ ((Display *));
418 void x_uncatch_errors P_ ((Display *, int));
419 void x_lower_frame P_ ((struct frame *));
420 void x_scroll_bar_clear P_ ((struct frame *));
421 int x_had_errors_p P_ ((Display *));
422 void x_wm_set_size_hint P_ ((struct frame *, long, int));
423 void x_raise_frame P_ ((struct frame *));
424 void x_set_window_size P_ ((struct frame *, int, int, int));
425 void x_wm_set_window_state P_ ((struct frame *, int));
426 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
427 void x_initialize P_ ((void));
428 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
429 static int x_compute_min_glyph_bounds P_ ((struct frame *));
430 static void x_draw_phys_cursor_glyph P_ ((struct window *,
431 struct glyph_row *,
432 enum draw_glyphs_face));
433 static void x_update_end P_ ((struct frame *));
434 static void XTframe_up_to_date P_ ((struct frame *));
435 static void XTreassert_line_highlight P_ ((int, int));
436 static void x_change_line_highlight P_ ((int, int, int, int));
437 static void XTset_terminal_modes P_ ((void));
438 static void XTreset_terminal_modes P_ ((void));
439 static void XTcursor_to P_ ((int, int, int, int));
440 static void x_write_glyphs P_ ((struct glyph *, int));
441 static void x_clear_end_of_line P_ ((int));
442 static void x_clear_frame P_ ((void));
443 static void x_clear_cursor P_ ((struct window *));
444 static void frame_highlight P_ ((struct frame *));
445 static void frame_unhighlight P_ ((struct frame *));
446 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
447 static void XTframe_rehighlight P_ ((struct frame *));
448 static void x_frame_rehighlight P_ ((struct x_display_info *));
449 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
450 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
451 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
452 XRectangle *));
453 static void expose_frame P_ ((struct frame *, int, int, int, int));
454 static void expose_window_tree P_ ((struct window *, XRectangle *));
455 static void expose_window P_ ((struct window *, XRectangle *));
456 static void expose_area P_ ((struct window *, struct glyph_row *,
457 XRectangle *, enum glyph_row_area));
458 static void expose_line P_ ((struct window *, struct glyph_row *,
459 XRectangle *));
460 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
461 static void x_update_window_cursor P_ ((struct window *, int));
462 static void x_erase_phys_cursor P_ ((struct window *));
463 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
464 static void x_draw_bitmap P_ ((struct window *, struct glyph_row *,
465 enum bitmap_type));
467 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
468 GC, int));
469 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
470 static void x_draw_row_bitmaps P_ ((struct window *, struct glyph_row *));
471 static void note_overwritten_text_cursor P_ ((struct window *, int, int));
472 static void x_flush P_ ((struct frame *f));
473 static void x_update_begin P_ ((struct frame *));
474 static void x_update_window_begin P_ ((struct window *));
475 static void x_draw_vertical_border P_ ((struct window *));
476 static void x_after_update_window_line P_ ((struct glyph_row *));
477 static INLINE void take_vertical_position_into_account P_ ((struct it *));
478 static void x_produce_stretch_glyph P_ ((struct it *));
481 /* Flush display of frame F, or of all frames if F is null. */
483 static void
484 x_flush (f)
485 struct frame *f;
487 BLOCK_INPUT;
488 if (f == NULL)
490 Lisp_Object rest, frame;
491 FOR_EACH_FRAME (rest, frame)
492 x_flush (XFRAME (frame));
494 else if (FRAME_X_P (f))
495 XFlush (FRAME_X_DISPLAY (f));
496 UNBLOCK_INPUT;
500 /* Remove calls to XFlush by defining XFlush to an empty replacement.
501 Calls to XFlush should be unnecessary because the X output buffer
502 is flushed automatically as needed by calls to XPending,
503 XNextEvent, or XWindowEvent according to the XFlush man page.
504 XTread_socket calls XPending. Removing XFlush improves
505 performance. */
507 #define XFlush(DISPLAY) (void) 0
510 /***********************************************************************
511 Debugging
512 ***********************************************************************/
514 #if 0
516 /* This is a function useful for recording debugging information about
517 the sequence of occurrences in this file. */
519 struct record
521 char *locus;
522 int type;
525 struct record event_record[100];
527 int event_record_index;
529 record_event (locus, type)
530 char *locus;
531 int type;
533 if (event_record_index == sizeof (event_record) / sizeof (struct record))
534 event_record_index = 0;
536 event_record[event_record_index].locus = locus;
537 event_record[event_record_index].type = type;
538 event_record_index++;
541 #endif /* 0 */
545 /* Return the struct x_display_info corresponding to DPY. */
547 struct x_display_info *
548 x_display_info_for_display (dpy)
549 Display *dpy;
551 struct x_display_info *dpyinfo;
553 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
554 if (dpyinfo->display == dpy)
555 return dpyinfo;
557 return 0;
562 /***********************************************************************
563 Starting and ending an update
564 ***********************************************************************/
566 /* Start an update of frame F. This function is installed as a hook
567 for update_begin, i.e. it is called when update_begin is called.
568 This function is called prior to calls to x_update_window_begin for
569 each window being updated. Currently, there is nothing to do here
570 because all interesting stuff is done on a window basis. */
572 static void
573 x_update_begin (f)
574 struct frame *f;
576 /* Nothing to do. */
580 /* Start update of window W. Set the global variable updated_window
581 to the window being updated and set output_cursor to the cursor
582 position of W. */
584 static void
585 x_update_window_begin (w)
586 struct window *w;
588 struct frame *f = XFRAME (WINDOW_FRAME (w));
589 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
591 updated_window = w;
592 set_output_cursor (&w->cursor);
594 BLOCK_INPUT;
596 if (f == display_info->mouse_face_mouse_frame)
598 /* Don't do highlighting for mouse motion during the update. */
599 display_info->mouse_face_defer = 1;
601 /* If F needs to be redrawn, simply forget about any prior mouse
602 highlighting. */
603 if (FRAME_GARBAGED_P (f))
604 display_info->mouse_face_window = Qnil;
606 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
607 their mouse_face_p flag set, which means that they are always
608 unequal to rows in a desired matrix which never have that
609 flag set. So, rows containing mouse-face glyphs are never
610 scrolled, and we don't have to switch the mouse highlight off
611 here to prevent it from being scrolled. */
613 /* Can we tell that this update does not affect the window
614 where the mouse highlight is? If so, no need to turn off.
615 Likewise, don't do anything if the frame is garbaged;
616 in that case, the frame's current matrix that we would use
617 is all wrong, and we will redisplay that line anyway. */
618 if (!NILP (display_info->mouse_face_window)
619 && w == XWINDOW (display_info->mouse_face_window))
621 int i;
623 for (i = 0; i < w->desired_matrix->nrows; ++i)
624 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
625 break;
627 if (i < w->desired_matrix->nrows)
628 clear_mouse_face (display_info);
630 #endif /* 0 */
633 UNBLOCK_INPUT;
637 /* Draw a vertical window border to the right of window W if W doesn't
638 have vertical scroll bars. */
640 static void
641 x_draw_vertical_border (w)
642 struct window *w;
644 struct frame *f = XFRAME (WINDOW_FRAME (w));
646 /* Redraw borders between horizontally adjacent windows. Don't
647 do it for frames with vertical scroll bars because either the
648 right scroll bar of a window, or the left scroll bar of its
649 neighbor will suffice as a border. */
650 if (!WINDOW_RIGHTMOST_P (w)
651 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
653 int x0, x1, y0, y1;
655 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
656 x1 += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f);
657 y1 -= 1;
659 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
660 f->output_data.x->normal_gc, x1, y0, x1, y1);
665 /* End update of window W (which is equal to updated_window).
667 Draw vertical borders between horizontally adjacent windows, and
668 display W's cursor if CURSOR_ON_P is non-zero.
670 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
671 glyphs in mouse-face were overwritten. In that case we have to
672 make sure that the mouse-highlight is properly redrawn.
674 W may be a menu bar pseudo-window in case we don't have X toolkit
675 support. Such windows don't have a cursor, so don't display it
676 here. */
678 static void
679 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
680 struct window *w;
681 int cursor_on_p, mouse_face_overwritten_p;
683 if (!w->pseudo_window_p)
685 struct x_display_info *dpyinfo
686 = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
688 BLOCK_INPUT;
690 /* If a row with mouse-face was overwritten, arrange for
691 XTframe_up_to_date to redisplay the mouse highlight. */
692 if (mouse_face_overwritten_p)
694 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
695 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
696 dpyinfo->mouse_face_window = Qnil;
699 if (cursor_on_p)
700 x_display_and_set_cursor (w, 1, output_cursor.hpos,
701 output_cursor.vpos,
702 output_cursor.x, output_cursor.y);
704 x_draw_vertical_border (w);
705 UNBLOCK_INPUT;
708 updated_window = NULL;
712 /* End update of frame F. This function is installed as a hook in
713 update_end. */
715 static void
716 x_update_end (f)
717 struct frame *f;
719 /* Mouse highlight may be displayed again. */
720 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
722 BLOCK_INPUT;
723 XFlush (FRAME_X_DISPLAY (f));
724 UNBLOCK_INPUT;
728 /* This function is called from various places in xdisp.c whenever a
729 complete update has been performed. The global variable
730 updated_window is not available here. */
732 static void
733 XTframe_up_to_date (f)
734 struct frame *f;
736 if (FRAME_X_P (f))
738 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
740 if (dpyinfo->mouse_face_deferred_gc
741 || f == dpyinfo->mouse_face_mouse_frame)
743 BLOCK_INPUT;
744 if (dpyinfo->mouse_face_mouse_frame)
745 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
746 dpyinfo->mouse_face_mouse_x,
747 dpyinfo->mouse_face_mouse_y);
748 dpyinfo->mouse_face_deferred_gc = 0;
749 UNBLOCK_INPUT;
755 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
756 arrow bitmaps, or clear the areas where they would be displayed
757 before DESIRED_ROW is made current. The window being updated is
758 found in updated_window. This function It is called from
759 update_window_line only if it is known that there are differences
760 between bitmaps to be drawn between current row and DESIRED_ROW. */
762 static void
763 x_after_update_window_line (desired_row)
764 struct glyph_row *desired_row;
766 struct window *w = updated_window;
768 xassert (w);
770 if (!desired_row->mode_line_p && !w->pseudo_window_p)
772 BLOCK_INPUT;
773 x_draw_row_bitmaps (w, desired_row);
775 /* When a window has disappeared, make sure that no rest of
776 full-width rows stays visible in the internal border. */
777 if (windows_or_buffers_changed)
779 struct frame *f = XFRAME (w->frame);
780 int width = FRAME_INTERNAL_BORDER_WIDTH (f);
781 int height = desired_row->visible_height;
782 int x = (window_box_right (w, -1)
783 + FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f));
784 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
786 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
787 x, y, width, height, False);
790 UNBLOCK_INPUT;
795 /* Draw the bitmap WHICH in one of the areas to the left or right of
796 window W. ROW is the glyph row for which to display the bitmap; it
797 determines the vertical position at which the bitmap has to be
798 drawn. */
800 static void
801 x_draw_bitmap (w, row, which)
802 struct window *w;
803 struct glyph_row *row;
804 enum bitmap_type which;
806 struct frame *f = XFRAME (WINDOW_FRAME (w));
807 Display *display = FRAME_X_DISPLAY (f);
808 Window window = FRAME_X_WINDOW (f);
809 int x, y, wd, h, dy;
810 unsigned char *bits;
811 Pixmap pixmap;
812 GC gc = f->output_data.x->normal_gc;
813 struct face *face;
814 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
816 /* Must clip because of partially visible lines. */
817 x_clip_to_row (w, row, gc, 1);
819 switch (which)
821 case LEFT_TRUNCATION_BITMAP:
822 wd = left_width;
823 h = left_height;
824 bits = left_bits;
825 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
826 - wd
827 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
828 break;
830 case OVERLAY_ARROW_BITMAP:
831 wd = left_width;
832 h = left_height;
833 bits = ov_bits;
834 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
835 - wd
836 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
837 break;
839 case RIGHT_TRUNCATION_BITMAP:
840 wd = right_width;
841 h = right_height;
842 bits = right_bits;
843 x = window_box_right (w, -1);
844 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
845 break;
847 case CONTINUED_LINE_BITMAP:
848 wd = right_width;
849 h = right_height;
850 bits = continued_bits;
851 x = window_box_right (w, -1);
852 x += (FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f) - wd) / 2;
853 break;
855 case CONTINUATION_LINE_BITMAP:
856 wd = continuation_width;
857 h = continuation_height;
858 bits = continuation_bits;
859 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
860 - wd
861 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
862 break;
864 case ZV_LINE_BITMAP:
865 wd = zv_width;
866 h = zv_height;
867 bits = zv_bits;
868 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
869 - wd
870 - (FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - wd) / 2);
871 break;
873 default:
874 abort ();
877 /* Convert to frame coordinates. Set dy to the offset in the row to
878 start drawing the bitmap. */
879 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
880 dy = (row->height - h) / 2;
882 /* Draw the bitmap. I believe these small pixmaps can be cached
883 by the server. */
884 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
885 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
886 face->foreground,
887 face->background, depth);
888 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
889 XFreePixmap (display, pixmap);
890 XSetClipMask (display, gc, None);
894 /* Draw flags bitmaps for glyph row ROW on window W. Call this
895 function with input blocked. */
897 static void
898 x_draw_row_bitmaps (w, row)
899 struct window *w;
900 struct glyph_row *row;
902 struct frame *f = XFRAME (w->frame);
903 enum bitmap_type bitmap;
904 struct face *face;
905 int header_line_height = -1;
907 xassert (interrupt_input_blocked);
909 /* If row is completely invisible, because of vscrolling, we
910 don't have to draw anything. */
911 if (row->visible_height <= 0)
912 return;
914 face = FACE_FROM_ID (f, BITMAP_AREA_FACE_ID);
915 PREPARE_FACE_FOR_DISPLAY (f, face);
917 /* Decide which bitmap to draw at the left side. */
918 if (row->overlay_arrow_p)
919 bitmap = OVERLAY_ARROW_BITMAP;
920 else if (row->truncated_on_left_p)
921 bitmap = LEFT_TRUNCATION_BITMAP;
922 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
923 bitmap = CONTINUATION_LINE_BITMAP;
924 else if (row->indicate_empty_line_p)
925 bitmap = ZV_LINE_BITMAP;
926 else
927 bitmap = NO_BITMAP;
929 /* Clear flags area if no bitmap to draw or if bitmap doesn't fill
930 the flags area. */
931 if (bitmap == NO_BITMAP
932 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
933 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
935 /* If W has a vertical border to its left, don't draw over it. */
936 int border = ((XFASTINT (w->left) > 0
937 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
938 ? 1 : 0);
939 int left = window_box_left (w, -1);
941 if (header_line_height < 0)
942 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
944 /* In case the same realized face is used for bitmap areas and
945 for something displayed in the text (e.g. face `region' on
946 mono-displays, the fill style may have been changed to
947 FillSolid in x_draw_glyph_string_background. */
948 if (face->stipple)
949 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
950 else
951 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
953 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
954 face->gc,
955 (left
956 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
957 + border),
958 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
959 row->y)),
960 FRAME_X_LEFT_FLAGS_AREA_WIDTH (f) - border,
961 row->visible_height);
962 if (!face->stipple)
963 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
966 /* Draw the left bitmap. */
967 if (bitmap != NO_BITMAP)
968 x_draw_bitmap (w, row, bitmap);
970 /* Decide which bitmap to draw at the right side. */
971 if (row->truncated_on_right_p)
972 bitmap = RIGHT_TRUNCATION_BITMAP;
973 else if (row->continued_p)
974 bitmap = CONTINUED_LINE_BITMAP;
975 else
976 bitmap = NO_BITMAP;
978 /* Clear flags area if no bitmap to draw of if bitmap doesn't fill
979 the flags area. */
980 if (bitmap == NO_BITMAP
981 || FRAME_FLAGS_BITMAP_WIDTH (f) < FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f)
982 || row->height > FRAME_FLAGS_BITMAP_HEIGHT (f))
984 int right = window_box_right (w, -1);
986 if (header_line_height < 0)
987 header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
989 /* In case the same realized face is used for bitmap areas and
990 for something displayed in the text (e.g. face `region' on
991 mono-displays, the fill style may have been changed to
992 FillSolid in x_draw_glyph_string_background. */
993 if (face->stipple)
994 XSetFillStyle (FRAME_X_DISPLAY (f), face->gc, FillOpaqueStippled);
995 else
996 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->background);
997 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
998 face->gc,
999 right,
1000 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
1001 row->y)),
1002 FRAME_X_RIGHT_FLAGS_AREA_WIDTH (f),
1003 row->visible_height);
1004 if (!face->stipple)
1005 XSetForeground (FRAME_X_DISPLAY (f), face->gc, face->foreground);
1008 /* Draw the right bitmap. */
1009 if (bitmap != NO_BITMAP)
1010 x_draw_bitmap (w, row, bitmap);
1014 /***********************************************************************
1015 Line Highlighting
1016 ***********************************************************************/
1018 /* External interface to control of standout mode. Not used for X
1019 frames. Aborts when called. */
1021 static void
1022 XTreassert_line_highlight (new, vpos)
1023 int new, vpos;
1025 abort ();
1029 /* Call this when about to modify line at position VPOS and change
1030 whether it is highlighted. Not used for X frames. Aborts when
1031 called. */
1033 static void
1034 x_change_line_highlight (new_highlight, vpos, y, first_unused_hpos)
1035 int new_highlight, vpos, y, first_unused_hpos;
1037 abort ();
1041 /* This is called when starting Emacs and when restarting after
1042 suspend. When starting Emacs, no X window is mapped. And nothing
1043 must be done to Emacs's own window if it is suspended (though that
1044 rarely happens). */
1046 static void
1047 XTset_terminal_modes ()
1051 /* This is called when exiting or suspending Emacs. Exiting will make
1052 the X-windows go away, and suspending requires no action. */
1054 static void
1055 XTreset_terminal_modes ()
1061 /***********************************************************************
1062 Output Cursor
1063 ***********************************************************************/
1065 /* Set the global variable output_cursor to CURSOR. All cursor
1066 positions are relative to updated_window. */
1068 static void
1069 set_output_cursor (cursor)
1070 struct cursor_pos *cursor;
1072 output_cursor.hpos = cursor->hpos;
1073 output_cursor.vpos = cursor->vpos;
1074 output_cursor.x = cursor->x;
1075 output_cursor.y = cursor->y;
1079 /* Set a nominal cursor position.
1081 HPOS and VPOS are column/row positions in a window glyph matrix. X
1082 and Y are window text area relative pixel positions.
1084 If this is done during an update, updated_window will contain the
1085 window that is being updated and the position is the future output
1086 cursor position for that window. If updated_window is null, use
1087 selected_window and display the cursor at the given position. */
1089 static void
1090 XTcursor_to (vpos, hpos, y, x)
1091 int vpos, hpos, y, x;
1093 struct window *w;
1095 /* If updated_window is not set, work on selected_window. */
1096 if (updated_window)
1097 w = updated_window;
1098 else
1099 w = XWINDOW (selected_window);
1101 /* Set the output cursor. */
1102 output_cursor.hpos = hpos;
1103 output_cursor.vpos = vpos;
1104 output_cursor.x = x;
1105 output_cursor.y = y;
1107 /* If not called as part of an update, really display the cursor.
1108 This will also set the cursor position of W. */
1109 if (updated_window == NULL)
1111 BLOCK_INPUT;
1112 x_display_cursor (w, 1, hpos, vpos, x, y);
1113 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1114 UNBLOCK_INPUT;
1120 /***********************************************************************
1121 Display Iterator
1122 ***********************************************************************/
1124 /* Function prototypes of this page. */
1126 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1127 struct glyph *,
1128 XChar2b *,
1129 int *));
1130 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1131 int, XChar2b *, int));
1132 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1133 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1134 static void x_append_glyph P_ ((struct it *));
1135 static void x_append_composite_glyph P_ ((struct it *));
1136 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1137 int, int, double));
1138 static void x_produce_glyphs P_ ((struct it *));
1139 static void x_produce_image_glyph P_ ((struct it *it));
1142 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1143 is not contained in the font. */
1145 static INLINE XCharStruct *
1146 x_per_char_metric (font, char2b)
1147 XFontStruct *font;
1148 XChar2b *char2b;
1150 /* The result metric information. */
1151 XCharStruct *pcm = NULL;
1153 xassert (font && char2b);
1155 if (font->per_char != NULL)
1157 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1159 /* min_char_or_byte2 specifies the linear character index
1160 corresponding to the first element of the per_char array,
1161 max_char_or_byte2 is the index of the last character. A
1162 character with non-zero CHAR2B->byte1 is not in the font.
1163 A character with byte2 less than min_char_or_byte2 or
1164 greater max_char_or_byte2 is not in the font. */
1165 if (char2b->byte1 == 0
1166 && char2b->byte2 >= font->min_char_or_byte2
1167 && char2b->byte2 <= font->max_char_or_byte2)
1168 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1170 else
1172 /* If either min_byte1 or max_byte1 are nonzero, both
1173 min_char_or_byte2 and max_char_or_byte2 are less than
1174 256, and the 2-byte character index values corresponding
1175 to the per_char array element N (counting from 0) are:
1177 byte1 = N/D + min_byte1
1178 byte2 = N\D + min_char_or_byte2
1180 where:
1182 D = max_char_or_byte2 - min_char_or_byte2 + 1
1183 / = integer division
1184 \ = integer modulus */
1185 if (char2b->byte1 >= font->min_byte1
1186 && char2b->byte1 <= font->max_byte1
1187 && char2b->byte2 >= font->min_char_or_byte2
1188 && char2b->byte2 <= font->max_char_or_byte2)
1190 pcm = (font->per_char
1191 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1192 * (char2b->byte1 - font->min_byte1))
1193 + (char2b->byte2 - font->min_char_or_byte2));
1197 else
1199 /* If the per_char pointer is null, all glyphs between the first
1200 and last character indexes inclusive have the same
1201 information, as given by both min_bounds and max_bounds. */
1202 if (char2b->byte2 >= font->min_char_or_byte2
1203 && char2b->byte2 <= font->max_char_or_byte2)
1204 pcm = &font->max_bounds;
1207 return ((pcm == NULL
1208 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1209 ? NULL : pcm);
1213 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1214 the two-byte form of C. Encoding is returned in *CHAR2B. */
1216 static INLINE void
1217 x_encode_char (c, char2b, font_info)
1218 int c;
1219 XChar2b *char2b;
1220 struct font_info *font_info;
1222 int charset = CHAR_CHARSET (c);
1223 XFontStruct *font = font_info->font;
1225 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1226 This may be either a program in a special encoder language or a
1227 fixed encoding. */
1228 if (font_info->font_encoder)
1230 /* It's a program. */
1231 struct ccl_program *ccl = font_info->font_encoder;
1233 if (CHARSET_DIMENSION (charset) == 1)
1235 ccl->reg[0] = charset;
1236 ccl->reg[1] = char2b->byte2;
1238 else
1240 ccl->reg[0] = charset;
1241 ccl->reg[1] = char2b->byte1;
1242 ccl->reg[2] = char2b->byte2;
1245 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1247 /* We assume that MSBs are appropriately set/reset by CCL
1248 program. */
1249 if (font->max_byte1 == 0) /* 1-byte font */
1250 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1251 else
1252 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1254 else if (font_info->encoding[charset])
1256 /* Fixed encoding scheme. See fontset.h for the meaning of the
1257 encoding numbers. */
1258 int enc = font_info->encoding[charset];
1260 if ((enc == 1 || enc == 2)
1261 && CHARSET_DIMENSION (charset) == 2)
1262 char2b->byte1 |= 0x80;
1264 if (enc == 1 || enc == 3)
1265 char2b->byte2 |= 0x80;
1270 /* Get face and two-byte form of character C in face FACE_ID on frame
1271 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1272 means we want to display multibyte text. Value is a pointer to a
1273 realized face that is ready for display. */
1275 static INLINE struct face *
1276 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1277 struct frame *f;
1278 int c, face_id;
1279 XChar2b *char2b;
1280 int multibyte_p;
1282 struct face *face = FACE_FROM_ID (f, face_id);
1284 if (!multibyte_p)
1286 /* Unibyte case. We don't have to encode, but we have to make
1287 sure to use a face suitable for unibyte. */
1288 char2b->byte1 = 0;
1289 char2b->byte2 = c;
1290 face_id = FACE_FOR_CHAR (f, face, c);
1291 face = FACE_FROM_ID (f, face_id);
1293 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1295 /* Case of ASCII in a face known to fit ASCII. */
1296 char2b->byte1 = 0;
1297 char2b->byte2 = c;
1299 else
1301 int c1, c2, charset;
1303 /* Split characters into bytes. If c2 is -1 afterwards, C is
1304 really a one-byte character so that byte1 is zero. */
1305 SPLIT_CHAR (c, charset, c1, c2);
1306 if (c2 > 0)
1307 char2b->byte1 = c1, char2b->byte2 = c2;
1308 else
1309 char2b->byte1 = 0, char2b->byte2 = c1;
1311 /* Maybe encode the character in *CHAR2B. */
1312 if (face->font != NULL)
1314 struct font_info *font_info
1315 = FONT_INFO_FROM_ID (f, face->font_info_id);
1316 if (font_info)
1317 x_encode_char (c, char2b, font_info);
1321 /* Make sure X resources of the face are allocated. */
1322 xassert (face != NULL);
1323 PREPARE_FACE_FOR_DISPLAY (f, face);
1325 return face;
1329 /* Get face and two-byte form of character glyph GLYPH on frame F.
1330 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1331 a pointer to a realized face that is ready for display. */
1333 static INLINE struct face *
1334 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1335 struct frame *f;
1336 struct glyph *glyph;
1337 XChar2b *char2b;
1338 int *two_byte_p;
1340 struct face *face;
1342 xassert (glyph->type == CHAR_GLYPH);
1343 face = FACE_FROM_ID (f, glyph->face_id);
1345 if (two_byte_p)
1346 *two_byte_p = 0;
1348 if (!glyph->multibyte_p)
1350 /* Unibyte case. We don't have to encode, but we have to make
1351 sure to use a face suitable for unibyte. */
1352 char2b->byte1 = 0;
1353 char2b->byte2 = glyph->u.ch;
1355 else if (glyph->u.ch < 128
1356 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1358 /* Case of ASCII in a face known to fit ASCII. */
1359 char2b->byte1 = 0;
1360 char2b->byte2 = glyph->u.ch;
1362 else
1364 int c1, c2, charset;
1366 /* Split characters into bytes. If c2 is -1 afterwards, C is
1367 really a one-byte character so that byte1 is zero. */
1368 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1369 if (c2 > 0)
1370 char2b->byte1 = c1, char2b->byte2 = c2;
1371 else
1372 char2b->byte1 = 0, char2b->byte2 = c1;
1374 /* Maybe encode the character in *CHAR2B. */
1375 if (charset != CHARSET_ASCII)
1377 struct font_info *font_info
1378 = FONT_INFO_FROM_ID (f, face->font_info_id);
1379 if (font_info)
1381 x_encode_char (glyph->u.ch, char2b, font_info);
1382 if (two_byte_p)
1383 *two_byte_p
1384 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1389 /* Make sure X resources of the face are allocated. */
1390 xassert (face != NULL);
1391 PREPARE_FACE_FOR_DISPLAY (f, face);
1392 return face;
1396 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1397 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1399 static INLINE void
1400 x_append_glyph (it)
1401 struct it *it;
1403 struct glyph *glyph;
1404 enum glyph_row_area area = it->area;
1406 xassert (it->glyph_row);
1407 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1409 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1410 if (glyph < it->glyph_row->glyphs[area + 1])
1412 glyph->charpos = CHARPOS (it->position);
1413 glyph->object = it->object;
1414 glyph->pixel_width = it->pixel_width;
1415 glyph->voffset = it->voffset;
1416 glyph->type = CHAR_GLYPH;
1417 glyph->multibyte_p = it->multibyte_p;
1418 glyph->left_box_line_p = it->start_of_box_run_p;
1419 glyph->right_box_line_p = it->end_of_box_run_p;
1420 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1421 || it->phys_descent > it->descent);
1422 glyph->padding_p = 0;
1423 glyph->glyph_not_available_p = it->glyph_not_available_p;
1424 glyph->face_id = it->face_id;
1425 glyph->u.ch = it->char_to_display;
1426 ++it->glyph_row->used[area];
1430 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1431 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1433 static INLINE void
1434 x_append_composite_glyph (it)
1435 struct it *it;
1437 struct glyph *glyph;
1438 enum glyph_row_area area = it->area;
1440 xassert (it->glyph_row);
1442 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1443 if (glyph < it->glyph_row->glyphs[area + 1])
1445 glyph->charpos = CHARPOS (it->position);
1446 glyph->object = it->object;
1447 glyph->pixel_width = it->pixel_width;
1448 glyph->voffset = it->voffset;
1449 glyph->type = COMPOSITE_GLYPH;
1450 glyph->multibyte_p = it->multibyte_p;
1451 glyph->left_box_line_p = it->start_of_box_run_p;
1452 glyph->right_box_line_p = it->end_of_box_run_p;
1453 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1454 || it->phys_descent > it->descent);
1455 glyph->padding_p = 0;
1456 glyph->glyph_not_available_p = 0;
1457 glyph->face_id = it->face_id;
1458 glyph->u.cmp_id = it->cmp_id;
1459 ++it->glyph_row->used[area];
1464 /* Change IT->ascent and IT->height according to the setting of
1465 IT->voffset. */
1467 static INLINE void
1468 take_vertical_position_into_account (it)
1469 struct it *it;
1471 if (it->voffset)
1473 if (it->voffset < 0)
1474 /* Increase the ascent so that we can display the text higher
1475 in the line. */
1476 it->ascent += abs (it->voffset);
1477 else
1478 /* Increase the descent so that we can display the text lower
1479 in the line. */
1480 it->descent += it->voffset;
1485 /* Produce glyphs/get display metrics for the image IT is loaded with.
1486 See the description of struct display_iterator in dispextern.h for
1487 an overview of struct display_iterator. */
1489 static void
1490 x_produce_image_glyph (it)
1491 struct it *it;
1493 struct image *img;
1494 struct face *face;
1496 xassert (it->what == IT_IMAGE);
1498 face = FACE_FROM_ID (it->f, it->face_id);
1499 img = IMAGE_FROM_ID (it->f, it->image_id);
1500 xassert (img);
1502 /* Make sure X resources of the face and image are loaded. */
1503 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1504 prepare_image_for_display (it->f, img);
1506 it->ascent = it->phys_ascent = image_ascent (img, face);
1507 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1508 it->pixel_width = img->width + 2 * img->hmargin;
1510 it->nglyphs = 1;
1512 if (face->box != FACE_NO_BOX)
1514 it->ascent += face->box_line_width;
1515 it->descent += face->box_line_width;
1517 if (it->start_of_box_run_p)
1518 it->pixel_width += face->box_line_width;
1519 if (it->end_of_box_run_p)
1520 it->pixel_width += face->box_line_width;
1523 take_vertical_position_into_account (it);
1525 if (it->glyph_row)
1527 struct glyph *glyph;
1528 enum glyph_row_area area = it->area;
1530 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1531 if (glyph < it->glyph_row->glyphs[area + 1])
1533 glyph->charpos = CHARPOS (it->position);
1534 glyph->object = it->object;
1535 glyph->pixel_width = it->pixel_width;
1536 glyph->voffset = it->voffset;
1537 glyph->type = IMAGE_GLYPH;
1538 glyph->multibyte_p = it->multibyte_p;
1539 glyph->left_box_line_p = it->start_of_box_run_p;
1540 glyph->right_box_line_p = it->end_of_box_run_p;
1541 glyph->overlaps_vertically_p = 0;
1542 glyph->padding_p = 0;
1543 glyph->glyph_not_available_p = 0;
1544 glyph->face_id = it->face_id;
1545 glyph->u.img_id = img->id;
1546 ++it->glyph_row->used[area];
1552 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1553 of the glyph, WIDTH and HEIGHT are the width and height of the
1554 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1555 ascent of the glyph (0 <= ASCENT <= 1). */
1557 static void
1558 x_append_stretch_glyph (it, object, width, height, ascent)
1559 struct it *it;
1560 Lisp_Object object;
1561 int width, height;
1562 double ascent;
1564 struct glyph *glyph;
1565 enum glyph_row_area area = it->area;
1567 xassert (ascent >= 0 && ascent <= 1);
1569 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1570 if (glyph < it->glyph_row->glyphs[area + 1])
1572 glyph->charpos = CHARPOS (it->position);
1573 glyph->object = object;
1574 glyph->pixel_width = width;
1575 glyph->voffset = it->voffset;
1576 glyph->type = STRETCH_GLYPH;
1577 glyph->multibyte_p = it->multibyte_p;
1578 glyph->left_box_line_p = it->start_of_box_run_p;
1579 glyph->right_box_line_p = it->end_of_box_run_p;
1580 glyph->overlaps_vertically_p = 0;
1581 glyph->padding_p = 0;
1582 glyph->glyph_not_available_p = 0;
1583 glyph->face_id = it->face_id;
1584 glyph->u.stretch.ascent = height * ascent;
1585 glyph->u.stretch.height = height;
1586 ++it->glyph_row->used[area];
1591 /* Produce a stretch glyph for iterator IT. IT->object is the value
1592 of the glyph property displayed. The value must be a list
1593 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1594 being recognized:
1596 1. `:width WIDTH' specifies that the space should be WIDTH *
1597 canonical char width wide. WIDTH may be an integer or floating
1598 point number.
1600 2. `:relative-width FACTOR' specifies that the width of the stretch
1601 should be computed from the width of the first character having the
1602 `glyph' property, and should be FACTOR times that width.
1604 3. `:align-to HPOS' specifies that the space should be wide enough
1605 to reach HPOS, a value in canonical character units.
1607 Exactly one of the above pairs must be present.
1609 4. `:height HEIGHT' specifies that the height of the stretch produced
1610 should be HEIGHT, measured in canonical character units.
1612 5. `:relative-height FACTOR' specifies that the height of the the
1613 stretch should be FACTOR times the height of the characters having
1614 the glyph property.
1616 Either none or exactly one of 4 or 5 must be present.
1618 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1619 of the stretch should be used for the ascent of the stretch.
1620 ASCENT must be in the range 0 <= ASCENT <= 100. */
1622 #define NUMVAL(X) \
1623 ((INTEGERP (X) || FLOATP (X)) \
1624 ? XFLOATINT (X) \
1625 : - 1)
1628 static void
1629 x_produce_stretch_glyph (it)
1630 struct it *it;
1632 /* (space :width WIDTH :height HEIGHT. */
1633 #if GLYPH_DEBUG
1634 extern Lisp_Object Qspace;
1635 #endif
1636 extern Lisp_Object QCwidth, QCheight, QCascent;
1637 extern Lisp_Object QCrelative_width, QCrelative_height;
1638 extern Lisp_Object QCalign_to;
1639 Lisp_Object prop, plist;
1640 double width = 0, height = 0, ascent = 0;
1641 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1642 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1644 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1646 /* List should start with `space'. */
1647 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1648 plist = XCDR (it->object);
1650 /* Compute the width of the stretch. */
1651 if (prop = Fplist_get (plist, QCwidth),
1652 NUMVAL (prop) > 0)
1653 /* Absolute width `:width WIDTH' specified and valid. */
1654 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1655 else if (prop = Fplist_get (plist, QCrelative_width),
1656 NUMVAL (prop) > 0)
1658 /* Relative width `:relative-width FACTOR' specified and valid.
1659 Compute the width of the characters having the `glyph'
1660 property. */
1661 struct it it2;
1662 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1664 it2 = *it;
1665 if (it->multibyte_p)
1667 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1668 - IT_BYTEPOS (*it));
1669 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1671 else
1672 it2.c = *p, it2.len = 1;
1674 it2.glyph_row = NULL;
1675 it2.what = IT_CHARACTER;
1676 x_produce_glyphs (&it2);
1677 width = NUMVAL (prop) * it2.pixel_width;
1679 else if (prop = Fplist_get (plist, QCalign_to),
1680 NUMVAL (prop) > 0)
1681 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1682 else
1683 /* Nothing specified -> width defaults to canonical char width. */
1684 width = CANON_X_UNIT (it->f);
1686 /* Compute height. */
1687 if (prop = Fplist_get (plist, QCheight),
1688 NUMVAL (prop) > 0)
1689 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1690 else if (prop = Fplist_get (plist, QCrelative_height),
1691 NUMVAL (prop) > 0)
1692 height = FONT_HEIGHT (font) * NUMVAL (prop);
1693 else
1694 height = FONT_HEIGHT (font);
1696 /* Compute percentage of height used for ascent. If
1697 `:ascent ASCENT' is present and valid, use that. Otherwise,
1698 derive the ascent from the font in use. */
1699 if (prop = Fplist_get (plist, QCascent),
1700 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1701 ascent = NUMVAL (prop) / 100.0;
1702 else
1703 ascent = (double) font->ascent / FONT_HEIGHT (font);
1705 if (width <= 0)
1706 width = 1;
1707 if (height <= 0)
1708 height = 1;
1710 if (it->glyph_row)
1712 Lisp_Object object = it->stack[it->sp - 1].string;
1713 if (!STRINGP (object))
1714 object = it->w->buffer;
1715 x_append_stretch_glyph (it, object, width, height, ascent);
1718 it->pixel_width = width;
1719 it->ascent = it->phys_ascent = height * ascent;
1720 it->descent = it->phys_descent = height - it->ascent;
1721 it->nglyphs = 1;
1723 if (face->box != FACE_NO_BOX)
1725 it->ascent += face->box_line_width;
1726 it->descent += face->box_line_width;
1728 if (it->start_of_box_run_p)
1729 it->pixel_width += face->box_line_width;
1730 if (it->end_of_box_run_p)
1731 it->pixel_width += face->box_line_width;
1734 take_vertical_position_into_account (it);
1737 /* Return proper value to be used as baseline offset of font that has
1738 ASCENT and DESCENT to draw characters by the font at the vertical
1739 center of the line of frame F.
1741 Here, out task is to find the value of BOFF in the following figure;
1743 -------------------------+-----------+-
1744 -+-+---------+-+ | |
1745 | | | | | |
1746 | | | | F_ASCENT F_HEIGHT
1747 | | | ASCENT | |
1748 HEIGHT | | | | |
1749 | | |-|-+------+-----------|------- baseline
1750 | | | | BOFF | |
1751 | |---------|-+-+ | |
1752 | | | DESCENT | |
1753 -+-+---------+-+ F_DESCENT |
1754 -------------------------+-----------+-
1756 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1757 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1758 DESCENT = FONT->descent
1759 HEIGHT = FONT_HEIGHT (FONT)
1760 F_DESCENT = (F->output_data.x->font->descent
1761 - F->output_data.x->baseline_offset)
1762 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1765 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1766 ((FONT)->descent \
1767 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1768 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1769 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1771 /* Produce glyphs/get display metrics for the display element IT is
1772 loaded with. See the description of struct display_iterator in
1773 dispextern.h for an overview of struct display_iterator. */
1775 static void
1776 x_produce_glyphs (it)
1777 struct it *it;
1779 it->glyph_not_available_p = 0;
1781 if (it->what == IT_CHARACTER)
1783 XChar2b char2b;
1784 XFontStruct *font;
1785 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1786 XCharStruct *pcm;
1787 int font_not_found_p;
1788 struct font_info *font_info;
1789 int boff; /* baseline offset */
1790 /* We may change it->multibyte_p upon unibyte<->multibyte
1791 conversion. So, save the current value now and restore it
1792 later.
1794 Note: It seems that we don't have to record multibyte_p in
1795 struct glyph because the character code itself tells if or
1796 not the character is multibyte. Thus, in the future, we must
1797 consider eliminating the field `multibyte_p' in the struct
1798 glyph.
1800 int saved_multibyte_p = it->multibyte_p;
1802 /* Maybe translate single-byte characters to multibyte, or the
1803 other way. */
1804 it->char_to_display = it->c;
1805 if (!ASCII_BYTE_P (it->c))
1807 if (unibyte_display_via_language_environment
1808 && SINGLE_BYTE_CHAR_P (it->c)
1809 && (it->c >= 0240
1810 || !NILP (Vnonascii_translation_table)))
1812 it->char_to_display = unibyte_char_to_multibyte (it->c);
1813 it->multibyte_p = 1;
1814 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1815 face = FACE_FROM_ID (it->f, it->face_id);
1817 else if (!SINGLE_BYTE_CHAR_P (it->c)
1818 && !it->multibyte_p)
1820 it->char_to_display = multibyte_char_to_unibyte (it->c, Qnil);
1821 it->multibyte_p = 0;
1822 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1823 face = FACE_FROM_ID (it->f, it->face_id);
1827 /* Get font to use. Encode IT->char_to_display. */
1828 x_get_char_face_and_encoding (it->f, it->char_to_display,
1829 it->face_id, &char2b,
1830 it->multibyte_p);
1831 font = face->font;
1833 /* When no suitable font found, use the default font. */
1834 font_not_found_p = font == NULL;
1835 if (font_not_found_p)
1837 font = FRAME_FONT (it->f);
1838 boff = it->f->output_data.x->baseline_offset;
1839 font_info = NULL;
1841 else
1843 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1844 boff = font_info->baseline_offset;
1845 if (font_info->vertical_centering)
1846 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1849 if (it->char_to_display >= ' '
1850 && (!it->multibyte_p || it->char_to_display < 128))
1852 /* Either unibyte or ASCII. */
1853 int stretched_p;
1855 it->nglyphs = 1;
1857 pcm = x_per_char_metric (font, &char2b);
1858 it->ascent = font->ascent + boff;
1859 it->descent = font->descent - boff;
1861 if (pcm)
1863 it->phys_ascent = pcm->ascent + boff;
1864 it->phys_descent = pcm->descent - boff;
1865 it->pixel_width = pcm->width;
1867 else
1869 it->glyph_not_available_p = 1;
1870 it->phys_ascent = font->ascent + boff;
1871 it->phys_descent = font->descent - boff;
1872 it->pixel_width = FONT_WIDTH (font);
1875 /* If this is a space inside a region of text with
1876 `space-width' property, change its width. */
1877 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1878 if (stretched_p)
1879 it->pixel_width *= XFLOATINT (it->space_width);
1881 /* If face has a box, add the box thickness to the character
1882 height. If character has a box line to the left and/or
1883 right, add the box line width to the character's width. */
1884 if (face->box != FACE_NO_BOX)
1886 int thick = face->box_line_width;
1888 it->ascent += thick;
1889 it->descent += thick;
1891 if (it->start_of_box_run_p)
1892 it->pixel_width += thick;
1893 if (it->end_of_box_run_p)
1894 it->pixel_width += thick;
1897 /* If face has an overline, add the height of the overline
1898 (1 pixel) and a 1 pixel margin to the character height. */
1899 if (face->overline_p)
1900 it->ascent += 2;
1902 take_vertical_position_into_account (it);
1904 /* If we have to actually produce glyphs, do it. */
1905 if (it->glyph_row)
1907 if (stretched_p)
1909 /* Translate a space with a `space-width' property
1910 into a stretch glyph. */
1911 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1912 x_append_stretch_glyph (it, it->object, it->pixel_width,
1913 it->ascent + it->descent, ascent);
1915 else
1916 x_append_glyph (it);
1918 /* If characters with lbearing or rbearing are displayed
1919 in this line, record that fact in a flag of the
1920 glyph row. This is used to optimize X output code. */
1921 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1922 it->glyph_row->contains_overlapping_glyphs_p = 1;
1925 else if (it->char_to_display == '\n')
1927 /* A newline has no width but we need the height of the line. */
1928 it->pixel_width = 0;
1929 it->nglyphs = 0;
1930 it->ascent = it->phys_ascent = font->ascent + boff;
1931 it->descent = it->phys_descent = font->descent - boff;
1933 if (face->box != FACE_NO_BOX)
1935 int thick = face->box_line_width;
1936 it->ascent += thick;
1937 it->descent += thick;
1940 else if (it->char_to_display == '\t')
1942 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1943 int x = it->current_x + it->continuation_lines_width;
1944 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1946 /* If the distance from the current position to the next tab
1947 stop is less than a canonical character width, use the
1948 tab stop after that. */
1949 if (next_tab_x - x < CANON_X_UNIT (it->f))
1950 next_tab_x += tab_width;
1952 it->pixel_width = next_tab_x - x;
1953 it->nglyphs = 1;
1954 it->ascent = it->phys_ascent = font->ascent + boff;
1955 it->descent = it->phys_descent = font->descent - boff;
1957 if (it->glyph_row)
1959 double ascent = (double) it->ascent / (it->ascent + it->descent);
1960 x_append_stretch_glyph (it, it->object, it->pixel_width,
1961 it->ascent + it->descent, ascent);
1964 else
1966 /* A multi-byte character. Assume that the display width of the
1967 character is the width of the character multiplied by the
1968 width of the font. */
1970 /* If we found a font, this font should give us the right
1971 metrics. If we didn't find a font, use the frame's
1972 default font and calculate the width of the character
1973 from the charset width; this is what old redisplay code
1974 did. */
1975 pcm = x_per_char_metric (font, &char2b);
1976 if (font_not_found_p || !pcm)
1978 int charset = CHAR_CHARSET (it->char_to_display);
1980 it->glyph_not_available_p = 1;
1981 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1982 * CHARSET_WIDTH (charset));
1983 it->phys_ascent = font->ascent + boff;
1984 it->phys_descent = font->descent - boff;
1986 else
1988 it->pixel_width = pcm->width;
1989 it->phys_ascent = pcm->ascent + boff;
1990 it->phys_descent = pcm->descent - boff;
1991 if (it->glyph_row
1992 && (pcm->lbearing < 0
1993 || pcm->rbearing > pcm->width))
1994 it->glyph_row->contains_overlapping_glyphs_p = 1;
1996 it->nglyphs = 1;
1997 it->ascent = font->ascent + boff;
1998 it->descent = font->descent - boff;
1999 if (face->box != FACE_NO_BOX)
2001 int thick = face->box_line_width;
2002 it->ascent += thick;
2003 it->descent += thick;
2005 if (it->start_of_box_run_p)
2006 it->pixel_width += thick;
2007 if (it->end_of_box_run_p)
2008 it->pixel_width += thick;
2011 /* If face has an overline, add the height of the overline
2012 (1 pixel) and a 1 pixel margin to the character height. */
2013 if (face->overline_p)
2014 it->ascent += 2;
2016 take_vertical_position_into_account (it);
2018 if (it->glyph_row)
2019 x_append_glyph (it);
2021 it->multibyte_p = saved_multibyte_p;
2023 else if (it->what == IT_COMPOSITION)
2025 /* Note: A composition is represented as one glyph in the
2026 glyph matrix. There are no padding glyphs. */
2027 XChar2b char2b;
2028 XFontStruct *font;
2029 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2030 XCharStruct *pcm;
2031 int font_not_found_p;
2032 struct font_info *font_info;
2033 int boff; /* baseline offset */
2034 struct composition *cmp = composition_table[it->cmp_id];
2036 /* Maybe translate single-byte characters to multibyte. */
2037 it->char_to_display = it->c;
2038 if (unibyte_display_via_language_environment
2039 && SINGLE_BYTE_CHAR_P (it->c)
2040 && (it->c >= 0240
2041 || (it->c >= 0200
2042 && !NILP (Vnonascii_translation_table))))
2044 it->char_to_display = unibyte_char_to_multibyte (it->c);
2047 /* Get face and font to use. Encode IT->char_to_display. */
2048 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2049 face = FACE_FROM_ID (it->f, it->face_id);
2050 x_get_char_face_and_encoding (it->f, it->char_to_display,
2051 it->face_id, &char2b, it->multibyte_p);
2052 font = face->font;
2054 /* When no suitable font found, use the default font. */
2055 font_not_found_p = font == NULL;
2056 if (font_not_found_p)
2058 font = FRAME_FONT (it->f);
2059 boff = it->f->output_data.x->baseline_offset;
2060 font_info = NULL;
2062 else
2064 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2065 boff = font_info->baseline_offset;
2066 if (font_info->vertical_centering)
2067 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2070 /* There are no padding glyphs, so there is only one glyph to
2071 produce for the composition. Important is that pixel_width,
2072 ascent and descent are the values of what is drawn by
2073 draw_glyphs (i.e. the values of the overall glyphs composed). */
2074 it->nglyphs = 1;
2076 /* If we have not yet calculated pixel size data of glyphs of
2077 the composition for the current face font, calculate them
2078 now. Theoretically, we have to check all fonts for the
2079 glyphs, but that requires much time and memory space. So,
2080 here we check only the font of the first glyph. This leads
2081 to incorrect display very rarely, and C-l (recenter) can
2082 correct the display anyway. */
2083 if (cmp->font != (void *) font)
2085 /* Ascent and descent of the font of the first character of
2086 this composition (adjusted by baseline offset). Ascent
2087 and descent of overall glyphs should not be less than
2088 them respectively. */
2089 int font_ascent = font->ascent + boff;
2090 int font_descent = font->descent - boff;
2091 /* Bounding box of the overall glyphs. */
2092 int leftmost, rightmost, lowest, highest;
2093 int i, width, ascent, descent;
2095 cmp->font = (void *) font;
2097 /* Initialize the bounding box. */
2098 if (font_info
2099 && (pcm = x_per_char_metric (font, &char2b)))
2101 width = pcm->width;
2102 ascent = pcm->ascent;
2103 descent = pcm->descent;
2105 else
2107 width = FONT_WIDTH (font);
2108 ascent = font->ascent;
2109 descent = font->descent;
2112 rightmost = width;
2113 lowest = - descent + boff;
2114 highest = ascent + boff;
2115 leftmost = 0;
2117 if (font_info
2118 && font_info->default_ascent
2119 && CHAR_TABLE_P (Vuse_default_ascent)
2120 && !NILP (Faref (Vuse_default_ascent,
2121 make_number (it->char_to_display))))
2122 highest = font_info->default_ascent + boff;
2124 /* Draw the first glyph at the normal position. It may be
2125 shifted to right later if some other glyphs are drawn at
2126 the left. */
2127 cmp->offsets[0] = 0;
2128 cmp->offsets[1] = boff;
2130 /* Set cmp->offsets for the remaining glyphs. */
2131 for (i = 1; i < cmp->glyph_len; i++)
2133 int left, right, btm, top;
2134 int ch = COMPOSITION_GLYPH (cmp, i);
2135 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2137 face = FACE_FROM_ID (it->f, face_id);
2138 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2139 it->multibyte_p);
2140 font = face->font;
2141 if (font == NULL)
2143 font = FRAME_FONT (it->f);
2144 boff = it->f->output_data.x->baseline_offset;
2145 font_info = NULL;
2147 else
2149 font_info
2150 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2151 boff = font_info->baseline_offset;
2152 if (font_info->vertical_centering)
2153 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2156 if (font_info
2157 && (pcm = x_per_char_metric (font, &char2b)))
2159 width = pcm->width;
2160 ascent = pcm->ascent;
2161 descent = pcm->descent;
2163 else
2165 width = FONT_WIDTH (font);
2166 ascent = 1;
2167 descent = 0;
2170 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2172 /* Relative composition with or without
2173 alternate chars. */
2174 left = (leftmost + rightmost - width) / 2;
2175 btm = - descent + boff;
2176 if (font_info && font_info->relative_compose
2177 && (! CHAR_TABLE_P (Vignore_relative_composition)
2178 || NILP (Faref (Vignore_relative_composition,
2179 make_number (ch)))))
2182 if (- descent >= font_info->relative_compose)
2183 /* One extra pixel between two glyphs. */
2184 btm = highest + 1;
2185 else if (ascent <= 0)
2186 /* One extra pixel between two glyphs. */
2187 btm = lowest - 1 - ascent - descent;
2190 else
2192 /* A composition rule is specified by an integer
2193 value that encodes global and new reference
2194 points (GREF and NREF). GREF and NREF are
2195 specified by numbers as below:
2197 0---1---2 -- ascent
2201 9--10--11 -- center
2203 ---3---4---5--- baseline
2205 6---7---8 -- descent
2207 int rule = COMPOSITION_RULE (cmp, i);
2208 int gref, nref, grefx, grefy, nrefx, nrefy;
2210 COMPOSITION_DECODE_RULE (rule, gref, nref);
2211 grefx = gref % 3, nrefx = nref % 3;
2212 grefy = gref / 3, nrefy = nref / 3;
2214 left = (leftmost
2215 + grefx * (rightmost - leftmost) / 2
2216 - nrefx * width / 2);
2217 btm = ((grefy == 0 ? highest
2218 : grefy == 1 ? 0
2219 : grefy == 2 ? lowest
2220 : (highest + lowest) / 2)
2221 - (nrefy == 0 ? ascent + descent
2222 : nrefy == 1 ? descent - boff
2223 : nrefy == 2 ? 0
2224 : (ascent + descent) / 2));
2227 cmp->offsets[i * 2] = left;
2228 cmp->offsets[i * 2 + 1] = btm + descent;
2230 /* Update the bounding box of the overall glyphs. */
2231 right = left + width;
2232 top = btm + descent + ascent;
2233 if (left < leftmost)
2234 leftmost = left;
2235 if (right > rightmost)
2236 rightmost = right;
2237 if (top > highest)
2238 highest = top;
2239 if (btm < lowest)
2240 lowest = btm;
2243 /* If there are glyphs whose x-offsets are negative,
2244 shift all glyphs to the right and make all x-offsets
2245 non-negative. */
2246 if (leftmost < 0)
2248 for (i = 0; i < cmp->glyph_len; i++)
2249 cmp->offsets[i * 2] -= leftmost;
2250 rightmost -= leftmost;
2253 cmp->pixel_width = rightmost;
2254 cmp->ascent = highest;
2255 cmp->descent = - lowest;
2256 if (cmp->ascent < font_ascent)
2257 cmp->ascent = font_ascent;
2258 if (cmp->descent < font_descent)
2259 cmp->descent = font_descent;
2262 it->pixel_width = cmp->pixel_width;
2263 it->ascent = it->phys_ascent = cmp->ascent;
2264 it->descent = it->phys_descent = cmp->descent;
2266 if (face->box != FACE_NO_BOX)
2268 int thick = face->box_line_width;
2269 it->ascent += thick;
2270 it->descent += thick;
2272 if (it->start_of_box_run_p)
2273 it->pixel_width += thick;
2274 if (it->end_of_box_run_p)
2275 it->pixel_width += thick;
2278 /* If face has an overline, add the height of the overline
2279 (1 pixel) and a 1 pixel margin to the character height. */
2280 if (face->overline_p)
2281 it->ascent += 2;
2283 take_vertical_position_into_account (it);
2285 if (it->glyph_row)
2286 x_append_composite_glyph (it);
2288 else if (it->what == IT_IMAGE)
2289 x_produce_image_glyph (it);
2290 else if (it->what == IT_STRETCH)
2291 x_produce_stretch_glyph (it);
2293 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2294 because this isn't true for images with `:ascent 100'. */
2295 xassert (it->ascent >= 0 && it->descent >= 0);
2296 if (it->area == TEXT_AREA)
2297 it->current_x += it->pixel_width;
2299 it->descent += it->extra_line_spacing;
2301 it->max_ascent = max (it->max_ascent, it->ascent);
2302 it->max_descent = max (it->max_descent, it->descent);
2303 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2304 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2308 /* Estimate the pixel height of the mode or top line on frame F.
2309 FACE_ID specifies what line's height to estimate. */
2312 x_estimate_mode_line_height (f, face_id)
2313 struct frame *f;
2314 enum face_id face_id;
2316 int height = FONT_HEIGHT (FRAME_FONT (f));
2318 /* This function is called so early when Emacs starts that the face
2319 cache and mode line face are not yet initialized. */
2320 if (FRAME_FACE_CACHE (f))
2322 struct face *face = FACE_FROM_ID (f, face_id);
2323 if (face)
2325 if (face->font)
2326 height = FONT_HEIGHT (face->font);
2327 height += 2 * face->box_line_width;
2331 return height;
2335 /***********************************************************************
2336 Glyph display
2337 ***********************************************************************/
2339 /* A sequence of glyphs to be drawn in the same face.
2341 This data structure is not really completely X specific, so it
2342 could possibly, at least partially, be useful for other systems. It
2343 is currently not part of the external redisplay interface because
2344 it's not clear what other systems will need. */
2346 struct glyph_string
2348 /* X-origin of the string. */
2349 int x;
2351 /* Y-origin and y-position of the base line of this string. */
2352 int y, ybase;
2354 /* The width of the string, not including a face extension. */
2355 int width;
2357 /* The width of the string, including a face extension. */
2358 int background_width;
2360 /* The height of this string. This is the height of the line this
2361 string is drawn in, and can be different from the height of the
2362 font the string is drawn in. */
2363 int height;
2365 /* Number of pixels this string overwrites in front of its x-origin.
2366 This number is zero if the string has an lbearing >= 0; it is
2367 -lbearing, if the string has an lbearing < 0. */
2368 int left_overhang;
2370 /* Number of pixels this string overwrites past its right-most
2371 nominal x-position, i.e. x + width. Zero if the string's
2372 rbearing is <= its nominal width, rbearing - width otherwise. */
2373 int right_overhang;
2375 /* The frame on which the glyph string is drawn. */
2376 struct frame *f;
2378 /* The window on which the glyph string is drawn. */
2379 struct window *w;
2381 /* X display and window for convenience. */
2382 Display *display;
2383 Window window;
2385 /* The glyph row for which this string was built. It determines the
2386 y-origin and height of the string. */
2387 struct glyph_row *row;
2389 /* The area within row. */
2390 enum glyph_row_area area;
2392 /* Characters to be drawn, and number of characters. */
2393 XChar2b *char2b;
2394 int nchars;
2396 /* A face-override for drawing cursors, mouse face and similar. */
2397 enum draw_glyphs_face hl;
2399 /* Face in which this string is to be drawn. */
2400 struct face *face;
2402 /* Font in which this string is to be drawn. */
2403 XFontStruct *font;
2405 /* Font info for this string. */
2406 struct font_info *font_info;
2408 /* Non-null means this string describes (part of) a composition.
2409 All characters from char2b are drawn composed. */
2410 struct composition *cmp;
2412 /* Index of this glyph string's first character in the glyph
2413 definition of CMP. If this is zero, this glyph string describes
2414 the first character of a composition. */
2415 int gidx;
2417 /* 1 means this glyph strings face has to be drawn to the right end
2418 of the window's drawing area. */
2419 unsigned extends_to_end_of_line_p : 1;
2421 /* 1 means the background of this string has been drawn. */
2422 unsigned background_filled_p : 1;
2424 /* 1 means glyph string must be drawn with 16-bit functions. */
2425 unsigned two_byte_p : 1;
2427 /* 1 means that the original font determined for drawing this glyph
2428 string could not be loaded. The member `font' has been set to
2429 the frame's default font in this case. */
2430 unsigned font_not_found_p : 1;
2432 /* 1 means that the face in which this glyph string is drawn has a
2433 stipple pattern. */
2434 unsigned stippled_p : 1;
2436 /* 1 means only the foreground of this glyph string must be drawn,
2437 and we should use the physical height of the line this glyph
2438 string appears in as clip rect. */
2439 unsigned for_overlaps_p : 1;
2441 /* The GC to use for drawing this glyph string. */
2442 GC gc;
2444 /* A pointer to the first glyph in the string. This glyph
2445 corresponds to char2b[0]. Needed to draw rectangles if
2446 font_not_found_p is 1. */
2447 struct glyph *first_glyph;
2449 /* Image, if any. */
2450 struct image *img;
2452 struct glyph_string *next, *prev;
2456 #if 0
2458 static void
2459 x_dump_glyph_string (s)
2460 struct glyph_string *s;
2462 fprintf (stderr, "glyph string\n");
2463 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2464 s->x, s->y, s->width, s->height);
2465 fprintf (stderr, " ybase = %d\n", s->ybase);
2466 fprintf (stderr, " hl = %d\n", s->hl);
2467 fprintf (stderr, " left overhang = %d, right = %d\n",
2468 s->left_overhang, s->right_overhang);
2469 fprintf (stderr, " nchars = %d\n", s->nchars);
2470 fprintf (stderr, " extends to end of line = %d\n",
2471 s->extends_to_end_of_line_p);
2472 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2473 fprintf (stderr, " bg width = %d\n", s->background_width);
2476 #endif /* GLYPH_DEBUG */
2480 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2481 struct glyph_string **,
2482 struct glyph_string *,
2483 struct glyph_string *));
2484 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2485 struct glyph_string **,
2486 struct glyph_string *,
2487 struct glyph_string *));
2488 static void x_append_glyph_string P_ ((struct glyph_string **,
2489 struct glyph_string **,
2490 struct glyph_string *));
2491 static int x_left_overwritten P_ ((struct glyph_string *));
2492 static int x_left_overwriting P_ ((struct glyph_string *));
2493 static int x_right_overwritten P_ ((struct glyph_string *));
2494 static int x_right_overwriting P_ ((struct glyph_string *));
2495 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2496 int));
2497 static void x_init_glyph_string P_ ((struct glyph_string *,
2498 XChar2b *, struct window *,
2499 struct glyph_row *,
2500 enum glyph_row_area, int,
2501 enum draw_glyphs_face));
2502 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2503 enum glyph_row_area, int, int,
2504 enum draw_glyphs_face, int *, int *, int));
2505 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2506 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2507 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2508 int));
2509 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2510 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2511 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2512 static void x_draw_glyph_string P_ ((struct glyph_string *));
2513 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2514 static void x_set_cursor_gc P_ ((struct glyph_string *));
2515 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2516 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2517 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2518 int *, int *));
2519 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2520 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2521 unsigned long *, double, int));
2522 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2523 double, int, unsigned long));
2524 static void x_setup_relief_colors P_ ((struct glyph_string *));
2525 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2526 static void x_draw_image_relief P_ ((struct glyph_string *));
2527 static void x_draw_image_foreground P_ ((struct glyph_string *));
2528 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2529 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2530 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2531 int, int, int));
2532 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2533 int, int, int, int, XRectangle *));
2534 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2535 int, int, int, XRectangle *));
2536 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2537 enum glyph_row_area));
2538 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2539 struct glyph_row *,
2540 enum glyph_row_area, int, int));
2542 #if GLYPH_DEBUG
2543 static void x_check_font P_ ((struct frame *, XFontStruct *));
2544 #endif
2547 /* Append the list of glyph strings with head H and tail T to the list
2548 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2550 static INLINE void
2551 x_append_glyph_string_lists (head, tail, h, t)
2552 struct glyph_string **head, **tail;
2553 struct glyph_string *h, *t;
2555 if (h)
2557 if (*head)
2558 (*tail)->next = h;
2559 else
2560 *head = h;
2561 h->prev = *tail;
2562 *tail = t;
2567 /* Prepend the list of glyph strings with head H and tail T to the
2568 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2569 result. */
2571 static INLINE void
2572 x_prepend_glyph_string_lists (head, tail, h, t)
2573 struct glyph_string **head, **tail;
2574 struct glyph_string *h, *t;
2576 if (h)
2578 if (*head)
2579 (*head)->prev = t;
2580 else
2581 *tail = t;
2582 t->next = *head;
2583 *head = h;
2588 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2589 Set *HEAD and *TAIL to the resulting list. */
2591 static INLINE void
2592 x_append_glyph_string (head, tail, s)
2593 struct glyph_string **head, **tail;
2594 struct glyph_string *s;
2596 s->next = s->prev = NULL;
2597 x_append_glyph_string_lists (head, tail, s, s);
2601 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2602 face. */
2604 static void
2605 x_set_cursor_gc (s)
2606 struct glyph_string *s;
2608 if (s->font == FRAME_FONT (s->f)
2609 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2610 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2611 && !s->cmp)
2612 s->gc = s->f->output_data.x->cursor_gc;
2613 else
2615 /* Cursor on non-default face: must merge. */
2616 XGCValues xgcv;
2617 unsigned long mask;
2619 xgcv.background = s->f->output_data.x->cursor_pixel;
2620 xgcv.foreground = s->face->background;
2622 /* If the glyph would be invisible, try a different foreground. */
2623 if (xgcv.foreground == xgcv.background)
2624 xgcv.foreground = s->face->foreground;
2625 if (xgcv.foreground == xgcv.background)
2626 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2627 if (xgcv.foreground == xgcv.background)
2628 xgcv.foreground = s->face->foreground;
2630 /* Make sure the cursor is distinct from text in this face. */
2631 if (xgcv.background == s->face->background
2632 && xgcv.foreground == s->face->foreground)
2634 xgcv.background = s->face->foreground;
2635 xgcv.foreground = s->face->background;
2638 IF_DEBUG (x_check_font (s->f, s->font));
2639 xgcv.font = s->font->fid;
2640 xgcv.graphics_exposures = False;
2641 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2643 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2644 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2645 mask, &xgcv);
2646 else
2647 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2648 = XCreateGC (s->display, s->window, mask, &xgcv);
2650 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2655 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2657 static void
2658 x_set_mouse_face_gc (s)
2659 struct glyph_string *s;
2661 int face_id;
2662 struct face *face;
2664 /* What face has to be used last for the mouse face? */
2665 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2666 face = FACE_FROM_ID (s->f, face_id);
2667 if (face == NULL)
2668 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2670 if (s->first_glyph->type == CHAR_GLYPH)
2671 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2672 else
2673 face_id = FACE_FOR_CHAR (s->f, face, 0);
2674 s->face = FACE_FROM_ID (s->f, face_id);
2675 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2677 /* If font in this face is same as S->font, use it. */
2678 if (s->font == s->face->font)
2679 s->gc = s->face->gc;
2680 else
2682 /* Otherwise construct scratch_cursor_gc with values from FACE
2683 but font FONT. */
2684 XGCValues xgcv;
2685 unsigned long mask;
2687 xgcv.background = s->face->background;
2688 xgcv.foreground = s->face->foreground;
2689 IF_DEBUG (x_check_font (s->f, s->font));
2690 xgcv.font = s->font->fid;
2691 xgcv.graphics_exposures = False;
2692 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2694 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2695 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2696 mask, &xgcv);
2697 else
2698 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2699 = XCreateGC (s->display, s->window, mask, &xgcv);
2701 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2704 xassert (s->gc != 0);
2708 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2709 Faces to use in the mode line have already been computed when the
2710 matrix was built, so there isn't much to do, here. */
2712 static INLINE void
2713 x_set_mode_line_face_gc (s)
2714 struct glyph_string *s;
2716 s->gc = s->face->gc;
2720 /* Set S->gc of glyph string S for drawing that glyph string. Set
2721 S->stippled_p to a non-zero value if the face of S has a stipple
2722 pattern. */
2724 static INLINE void
2725 x_set_glyph_string_gc (s)
2726 struct glyph_string *s;
2728 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2730 if (s->hl == DRAW_NORMAL_TEXT)
2732 s->gc = s->face->gc;
2733 s->stippled_p = s->face->stipple != 0;
2735 else if (s->hl == DRAW_INVERSE_VIDEO)
2737 x_set_mode_line_face_gc (s);
2738 s->stippled_p = s->face->stipple != 0;
2740 else if (s->hl == DRAW_CURSOR)
2742 x_set_cursor_gc (s);
2743 s->stippled_p = 0;
2745 else if (s->hl == DRAW_MOUSE_FACE)
2747 x_set_mouse_face_gc (s);
2748 s->stippled_p = s->face->stipple != 0;
2750 else if (s->hl == DRAW_IMAGE_RAISED
2751 || s->hl == DRAW_IMAGE_SUNKEN)
2753 s->gc = s->face->gc;
2754 s->stippled_p = s->face->stipple != 0;
2756 else
2758 s->gc = s->face->gc;
2759 s->stippled_p = s->face->stipple != 0;
2762 /* GC must have been set. */
2763 xassert (s->gc != 0);
2767 /* Return in *R the clipping rectangle for glyph string S. */
2769 static void
2770 x_get_glyph_string_clip_rect (s, r)
2771 struct glyph_string *s;
2772 XRectangle *r;
2774 if (s->row->full_width_p)
2776 /* Draw full-width. X coordinates are relative to S->w->left. */
2777 int canon_x = CANON_X_UNIT (s->f);
2779 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2780 r->width = XFASTINT (s->w->width) * canon_x;
2782 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2784 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2785 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2786 r->x -= width;
2789 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2791 /* Unless displaying a mode or menu bar line, which are always
2792 fully visible, clip to the visible part of the row. */
2793 if (s->w->pseudo_window_p)
2794 r->height = s->row->visible_height;
2795 else
2796 r->height = s->height;
2798 else
2800 /* This is a text line that may be partially visible. */
2801 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2802 r->width = window_box_width (s->w, s->area);
2803 r->height = s->row->visible_height;
2806 /* Don't use S->y for clipping because it doesn't take partially
2807 visible lines into account. For example, it can be negative for
2808 partially visible lines at the top of a window. */
2809 if (!s->row->full_width_p
2810 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2811 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2812 else
2813 r->y = max (0, s->row->y);
2815 /* If drawing a tool-bar window, draw it over the internal border
2816 at the top of the window. */
2817 if (s->w == XWINDOW (s->f->tool_bar_window))
2818 r->y -= s->f->output_data.x->internal_border_width;
2820 /* If S draws overlapping rows, it's sufficient to use the top and
2821 bottom of the window for clipping because this glyph string
2822 intentionally draws over other lines. */
2823 if (s->for_overlaps_p)
2825 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2826 r->height = window_text_bottom_y (s->w) - r->y;
2829 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2833 /* Set clipping for output of glyph string S. S may be part of a mode
2834 line or menu if we don't have X toolkit support. */
2836 static INLINE void
2837 x_set_glyph_string_clipping (s)
2838 struct glyph_string *s;
2840 XRectangle r;
2841 x_get_glyph_string_clip_rect (s, &r);
2842 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2846 /* Compute left and right overhang of glyph string S. If S is a glyph
2847 string for a composition, assume overhangs don't exist. */
2849 static INLINE void
2850 x_compute_glyph_string_overhangs (s)
2851 struct glyph_string *s;
2853 if (s->cmp == NULL
2854 && s->first_glyph->type == CHAR_GLYPH)
2856 XCharStruct cs;
2857 int direction, font_ascent, font_descent;
2858 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2859 &font_ascent, &font_descent, &cs);
2860 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2861 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2866 /* Compute overhangs and x-positions for glyph string S and its
2867 predecessors, or successors. X is the starting x-position for S.
2868 BACKWARD_P non-zero means process predecessors. */
2870 static void
2871 x_compute_overhangs_and_x (s, x, backward_p)
2872 struct glyph_string *s;
2873 int x;
2874 int backward_p;
2876 if (backward_p)
2878 while (s)
2880 x_compute_glyph_string_overhangs (s);
2881 x -= s->width;
2882 s->x = x;
2883 s = s->prev;
2886 else
2888 while (s)
2890 x_compute_glyph_string_overhangs (s);
2891 s->x = x;
2892 x += s->width;
2893 s = s->next;
2899 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2900 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2901 assumed to be zero. */
2903 static void
2904 x_get_glyph_overhangs (glyph, f, left, right)
2905 struct glyph *glyph;
2906 struct frame *f;
2907 int *left, *right;
2909 *left = *right = 0;
2911 if (glyph->type == CHAR_GLYPH)
2913 XFontStruct *font;
2914 struct face *face;
2915 struct font_info *font_info;
2916 XChar2b char2b;
2917 XCharStruct *pcm;
2919 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2920 font = face->font;
2921 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2922 if (font
2923 && (pcm = x_per_char_metric (font, &char2b)))
2925 if (pcm->rbearing > pcm->width)
2926 *right = pcm->rbearing - pcm->width;
2927 if (pcm->lbearing < 0)
2928 *left = -pcm->lbearing;
2934 /* Return the index of the first glyph preceding glyph string S that
2935 is overwritten by S because of S's left overhang. Value is -1
2936 if no glyphs are overwritten. */
2938 static int
2939 x_left_overwritten (s)
2940 struct glyph_string *s;
2942 int k;
2944 if (s->left_overhang)
2946 int x = 0, i;
2947 struct glyph *glyphs = s->row->glyphs[s->area];
2948 int first = s->first_glyph - glyphs;
2950 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2951 x -= glyphs[i].pixel_width;
2953 k = i + 1;
2955 else
2956 k = -1;
2958 return k;
2962 /* Return the index of the first glyph preceding glyph string S that
2963 is overwriting S because of its right overhang. Value is -1 if no
2964 glyph in front of S overwrites S. */
2966 static int
2967 x_left_overwriting (s)
2968 struct glyph_string *s;
2970 int i, k, x;
2971 struct glyph *glyphs = s->row->glyphs[s->area];
2972 int first = s->first_glyph - glyphs;
2974 k = -1;
2975 x = 0;
2976 for (i = first - 1; i >= 0; --i)
2978 int left, right;
2979 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2980 if (x + right > 0)
2981 k = i;
2982 x -= glyphs[i].pixel_width;
2985 return k;
2989 /* Return the index of the last glyph following glyph string S that is
2990 not overwritten by S because of S's right overhang. Value is -1 if
2991 no such glyph is found. */
2993 static int
2994 x_right_overwritten (s)
2995 struct glyph_string *s;
2997 int k = -1;
2999 if (s->right_overhang)
3001 int x = 0, i;
3002 struct glyph *glyphs = s->row->glyphs[s->area];
3003 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3004 int end = s->row->used[s->area];
3006 for (i = first; i < end && s->right_overhang > x; ++i)
3007 x += glyphs[i].pixel_width;
3009 k = i;
3012 return k;
3016 /* Return the index of the last glyph following glyph string S that
3017 overwrites S because of its left overhang. Value is negative
3018 if no such glyph is found. */
3020 static int
3021 x_right_overwriting (s)
3022 struct glyph_string *s;
3024 int i, k, x;
3025 int end = s->row->used[s->area];
3026 struct glyph *glyphs = s->row->glyphs[s->area];
3027 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3029 k = -1;
3030 x = 0;
3031 for (i = first; i < end; ++i)
3033 int left, right;
3034 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3035 if (x - left < 0)
3036 k = i;
3037 x += glyphs[i].pixel_width;
3040 return k;
3044 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3046 static INLINE void
3047 x_clear_glyph_string_rect (s, x, y, w, h)
3048 struct glyph_string *s;
3049 int x, y, w, h;
3051 XGCValues xgcv;
3052 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3053 XSetForeground (s->display, s->gc, xgcv.background);
3054 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3055 XSetForeground (s->display, s->gc, xgcv.foreground);
3059 /* Draw the background of glyph_string S. If S->background_filled_p
3060 is non-zero don't draw it. FORCE_P non-zero means draw the
3061 background even if it wouldn't be drawn normally. This is used
3062 when a string preceding S draws into the background of S, or S
3063 contains the first component of a composition. */
3065 static void
3066 x_draw_glyph_string_background (s, force_p)
3067 struct glyph_string *s;
3068 int force_p;
3070 /* Nothing to do if background has already been drawn or if it
3071 shouldn't be drawn in the first place. */
3072 if (!s->background_filled_p)
3074 if (s->stippled_p)
3076 /* Fill background with a stipple pattern. */
3077 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3078 XFillRectangle (s->display, s->window, s->gc, s->x,
3079 s->y + s->face->box_line_width,
3080 s->background_width,
3081 s->height - 2 * s->face->box_line_width);
3082 XSetFillStyle (s->display, s->gc, FillSolid);
3083 s->background_filled_p = 1;
3085 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3086 || s->font_not_found_p
3087 || s->extends_to_end_of_line_p
3088 || force_p)
3090 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3091 s->background_width,
3092 s->height - 2 * s->face->box_line_width);
3093 s->background_filled_p = 1;
3099 /* Draw the foreground of glyph string S. */
3101 static void
3102 x_draw_glyph_string_foreground (s)
3103 struct glyph_string *s;
3105 int i, x;
3107 /* If first glyph of S has a left box line, start drawing the text
3108 of S to the right of that box line. */
3109 if (s->face->box != FACE_NO_BOX
3110 && s->first_glyph->left_box_line_p)
3111 x = s->x + s->face->box_line_width;
3112 else
3113 x = s->x;
3115 /* Draw characters of S as rectangles if S's font could not be
3116 loaded. */
3117 if (s->font_not_found_p)
3119 for (i = 0; i < s->nchars; ++i)
3121 struct glyph *g = s->first_glyph + i;
3122 XDrawRectangle (s->display, s->window,
3123 s->gc, x, s->y, g->pixel_width - 1,
3124 s->height - 1);
3125 x += g->pixel_width;
3128 else
3130 char *char1b = (char *) s->char2b;
3131 int boff = s->font_info->baseline_offset;
3133 if (s->font_info->vertical_centering)
3134 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3136 /* If we can use 8-bit functions, condense S->char2b. */
3137 if (!s->two_byte_p)
3138 for (i = 0; i < s->nchars; ++i)
3139 char1b[i] = s->char2b[i].byte2;
3141 /* Draw text with XDrawString if background has already been
3142 filled. Otherwise, use XDrawImageString. (Note that
3143 XDrawImageString is usually faster than XDrawString.) Always
3144 use XDrawImageString when drawing the cursor so that there is
3145 no chance that characters under a box cursor are invisible. */
3146 if (s->for_overlaps_p
3147 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3149 /* Draw characters with 16-bit or 8-bit functions. */
3150 if (s->two_byte_p)
3151 XDrawString16 (s->display, s->window, s->gc, x,
3152 s->ybase - boff, s->char2b, s->nchars);
3153 else
3154 XDrawString (s->display, s->window, s->gc, x,
3155 s->ybase - boff, char1b, s->nchars);
3157 else
3159 if (s->two_byte_p)
3160 XDrawImageString16 (s->display, s->window, s->gc, x,
3161 s->ybase - boff, s->char2b, s->nchars);
3162 else
3163 XDrawImageString (s->display, s->window, s->gc, x,
3164 s->ybase - boff, char1b, s->nchars);
3169 /* Draw the foreground of composite glyph string S. */
3171 static void
3172 x_draw_composite_glyph_string_foreground (s)
3173 struct glyph_string *s;
3175 int i, x;
3177 /* If first glyph of S has a left box line, start drawing the text
3178 of S to the right of that box line. */
3179 if (s->face->box != FACE_NO_BOX
3180 && s->first_glyph->left_box_line_p)
3181 x = s->x + s->face->box_line_width;
3182 else
3183 x = s->x;
3185 /* S is a glyph string for a composition. S->gidx is the index of
3186 the first character drawn for glyphs of this composition.
3187 S->gidx == 0 means we are drawing the very first character of
3188 this composition. */
3190 /* Draw a rectangle for the composition if the font for the very
3191 first character of the composition could not be loaded. */
3192 if (s->font_not_found_p)
3194 if (s->gidx == 0)
3195 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3196 s->width - 1, s->height - 1);
3198 else
3200 for (i = 0; i < s->nchars; i++, ++s->gidx)
3201 XDrawString16 (s->display, s->window, s->gc,
3202 x + s->cmp->offsets[s->gidx * 2],
3203 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3204 s->char2b + i, 1);
3209 #ifdef USE_X_TOOLKIT
3211 static struct frame *x_frame_of_widget P_ ((Widget));
3214 /* Return the frame on which widget WIDGET is used.. Abort if frame
3215 cannot be determined. */
3217 static struct frame *
3218 x_frame_of_widget (widget)
3219 Widget widget;
3221 struct x_display_info *dpyinfo;
3222 Lisp_Object tail;
3223 struct frame *f;
3225 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3227 /* Find the top-level shell of the widget. Note that this function
3228 can be called when the widget is not yet realized, so XtWindow
3229 (widget) == 0. That's the reason we can't simply use
3230 x_any_window_to_frame. */
3231 while (!XtIsTopLevelShell (widget))
3232 widget = XtParent (widget);
3234 /* Look for a frame with that top-level widget. Allocate the color
3235 on that frame to get the right gamma correction value. */
3236 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3237 if (GC_FRAMEP (XCAR (tail))
3238 && (f = XFRAME (XCAR (tail)),
3239 (f->output_data.nothing != 1
3240 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3241 && f->output_data.x->widget == widget)
3242 return f;
3244 abort ();
3248 /* Allocate the color COLOR->pixel on the screen and display of
3249 widget WIDGET in colormap CMAP. If an exact match cannot be
3250 allocated, try the nearest color available. Value is non-zero
3251 if successful. This is called from lwlib. */
3254 x_alloc_nearest_color_for_widget (widget, cmap, color)
3255 Widget widget;
3256 Colormap cmap;
3257 XColor *color;
3259 struct frame *f = x_frame_of_widget (widget);
3260 return x_alloc_nearest_color (f, cmap, color);
3264 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3265 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3266 If this produces the same color as PIXEL, try a color where all RGB
3267 values have DELTA added. Return the allocated color in *PIXEL.
3268 DISPLAY is the X display, CMAP is the colormap to operate on.
3269 Value is non-zero if successful. */
3272 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3273 Widget widget;
3274 Display *display;
3275 Colormap cmap;
3276 unsigned long *pixel;
3277 double factor;
3278 int delta;
3280 struct frame *f = x_frame_of_widget (widget);
3281 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3285 #endif /* USE_X_TOOLKIT */
3288 /* Value is an array of XColor structures for the contents of the
3289 color map of frame F. Set *NCELLS to the size of the array.
3290 Note that this probably shouldn't be called for large color maps,
3291 say a 24-bit TrueColor map. */
3293 static const XColor *
3294 x_color_cells (f, ncells)
3295 struct frame *f;
3296 int *ncells;
3298 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3300 if (dpyinfo->color_cells == NULL)
3302 Display *display = FRAME_X_DISPLAY (f);
3303 Screen *screen = FRAME_X_SCREEN (f);
3304 int i;
3306 dpyinfo->ncolor_cells
3307 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3308 dpyinfo->color_cells
3309 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3310 * sizeof *dpyinfo->color_cells);
3312 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3313 dpyinfo->color_cells[i].pixel = i;
3315 XQueryColors (display, FRAME_X_COLORMAP (f),
3316 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3319 *ncells = dpyinfo->ncolor_cells;
3320 return dpyinfo->color_cells;
3324 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3325 colors in COLORS. Use cached information, if available. */
3327 void
3328 x_query_colors (f, colors, ncolors)
3329 struct frame *f;
3330 XColor *colors;
3331 int ncolors;
3333 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3335 if (dpyinfo->color_cells)
3337 int i;
3338 for (i = 0; i < ncolors; ++i)
3340 unsigned long pixel = colors[i].pixel;
3341 xassert (pixel < dpyinfo->ncolor_cells);
3342 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3343 colors[i] = dpyinfo->color_cells[pixel];
3346 else
3347 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3351 /* On frame F, translate pixel color to RGB values for the color in
3352 COLOR. Use cached information, if available. */
3354 void
3355 x_query_color (f, color)
3356 struct frame *f;
3357 XColor *color;
3359 x_query_colors (f, color, 1);
3363 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3364 CMAP. If an exact match can't be allocated, try the nearest color
3365 available. Value is non-zero if successful. Set *COLOR to the
3366 color allocated. */
3369 x_alloc_nearest_color (f, cmap, color)
3370 struct frame *f;
3371 Colormap cmap;
3372 XColor *color;
3374 Display *display = FRAME_X_DISPLAY (f);
3375 Screen *screen = FRAME_X_SCREEN (f);
3376 int rc;
3378 gamma_correct (f, color);
3379 rc = XAllocColor (display, cmap, color);
3380 if (rc == 0)
3382 /* If we got to this point, the colormap is full, so we're going
3383 to try to get the next closest color. The algorithm used is
3384 a least-squares matching, which is what X uses for closest
3385 color matching with StaticColor visuals. */
3386 int nearest, i;
3387 unsigned long nearest_delta = ~0;
3388 int ncells;
3389 const XColor *cells = x_color_cells (f, &ncells);
3391 for (nearest = i = 0; i < ncells; ++i)
3393 long dred = (color->red >> 8) - (cells[i].red >> 8);
3394 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3395 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3396 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3398 if (delta < nearest_delta)
3400 nearest = i;
3401 nearest_delta = delta;
3405 color->red = cells[nearest].red;
3406 color->green = cells[nearest].green;
3407 color->blue = cells[nearest].blue;
3408 rc = XAllocColor (display, cmap, color);
3410 else
3412 /* If allocation succeeded, and the allocated pixel color is not
3413 equal to a cached pixel color recorded earlier, there was a
3414 change in the colormap, so clear the color cache. */
3415 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3416 XColor *cached_color;
3418 if (dpyinfo->color_cells
3419 && (cached_color = &dpyinfo->color_cells[color->pixel],
3420 (cached_color->red != color->red
3421 || cached_color->blue != color->blue
3422 || cached_color->green != color->green)))
3424 xfree (dpyinfo->color_cells);
3425 dpyinfo->color_cells = NULL;
3426 dpyinfo->ncolor_cells = 0;
3430 #ifdef DEBUG_X_COLORS
3431 if (rc)
3432 register_color (color->pixel);
3433 #endif /* DEBUG_X_COLORS */
3435 return rc;
3439 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3440 It's necessary to do this instead of just using PIXEL directly to
3441 get color reference counts right. */
3443 unsigned long
3444 x_copy_color (f, pixel)
3445 struct frame *f;
3446 unsigned long pixel;
3448 XColor color;
3450 color.pixel = pixel;
3451 BLOCK_INPUT;
3452 x_query_color (f, &color);
3453 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3454 UNBLOCK_INPUT;
3455 #ifdef DEBUG_X_COLORS
3456 register_color (pixel);
3457 #endif
3458 return color.pixel;
3462 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3463 It's necessary to do this instead of just using PIXEL directly to
3464 get color reference counts right. */
3466 unsigned long
3467 x_copy_dpy_color (dpy, cmap, pixel)
3468 Display *dpy;
3469 Colormap cmap;
3470 unsigned long pixel;
3472 XColor color;
3474 color.pixel = pixel;
3475 BLOCK_INPUT;
3476 XQueryColor (dpy, cmap, &color);
3477 XAllocColor (dpy, cmap, &color);
3478 UNBLOCK_INPUT;
3479 #ifdef DEBUG_X_COLORS
3480 register_color (pixel);
3481 #endif
3482 return color.pixel;
3486 /* Brightness beyond which a color won't have its highlight brightness
3487 boosted.
3489 Nominally, highlight colors for `3d' faces are calculated by
3490 brightening an object's color by a constant scale factor, but this
3491 doesn't yield good results for dark colors, so for colors who's
3492 brightness is less than this value (on a scale of 0-65535) have an
3493 use an additional additive factor.
3495 The value here is set so that the default menu-bar/mode-line color
3496 (grey75) will not have its highlights changed at all. */
3497 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3500 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3501 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3502 If this produces the same color as PIXEL, try a color where all RGB
3503 values have DELTA added. Return the allocated color in *PIXEL.
3504 DISPLAY is the X display, CMAP is the colormap to operate on.
3505 Value is non-zero if successful. */
3507 static int
3508 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3509 struct frame *f;
3510 Display *display;
3511 Colormap cmap;
3512 unsigned long *pixel;
3513 double factor;
3514 int delta;
3516 XColor color, new;
3517 long bright;
3518 int success_p;
3520 /* Get RGB color values. */
3521 color.pixel = *pixel;
3522 x_query_color (f, &color);
3524 /* Change RGB values by specified FACTOR. Avoid overflow! */
3525 xassert (factor >= 0);
3526 new.red = min (0xffff, factor * color.red);
3527 new.green = min (0xffff, factor * color.green);
3528 new.blue = min (0xffff, factor * color.blue);
3530 /* Calculate brightness of COLOR. */
3531 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3533 /* We only boost colors that are darker than
3534 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3535 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3536 /* Make an additive adjustment to NEW, because it's dark enough so
3537 that scaling by FACTOR alone isn't enough. */
3539 /* How far below the limit this color is (0 - 1, 1 being darker). */
3540 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3541 /* The additive adjustment. */
3542 int min_delta = delta * dimness * factor / 2;
3544 if (factor < 1)
3546 new.red = max (0, new.red - min_delta);
3547 new.green = max (0, new.green - min_delta);
3548 new.blue = max (0, new.blue - min_delta);
3550 else
3552 new.red = min (0xffff, min_delta + new.red);
3553 new.green = min (0xffff, min_delta + new.green);
3554 new.blue = min (0xffff, min_delta + new.blue);
3558 /* Try to allocate the color. */
3559 success_p = x_alloc_nearest_color (f, cmap, &new);
3560 if (success_p)
3562 if (new.pixel == *pixel)
3564 /* If we end up with the same color as before, try adding
3565 delta to the RGB values. */
3566 x_free_colors (f, &new.pixel, 1);
3568 new.red = min (0xffff, delta + color.red);
3569 new.green = min (0xffff, delta + color.green);
3570 new.blue = min (0xffff, delta + color.blue);
3571 success_p = x_alloc_nearest_color (f, cmap, &new);
3573 else
3574 success_p = 1;
3575 *pixel = new.pixel;
3578 return success_p;
3582 /* Set up the foreground color for drawing relief lines of glyph
3583 string S. RELIEF is a pointer to a struct relief containing the GC
3584 with which lines will be drawn. Use a color that is FACTOR or
3585 DELTA lighter or darker than the relief's background which is found
3586 in S->f->output_data.x->relief_background. If such a color cannot
3587 be allocated, use DEFAULT_PIXEL, instead. */
3589 static void
3590 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3591 struct frame *f;
3592 struct relief *relief;
3593 double factor;
3594 int delta;
3595 unsigned long default_pixel;
3597 XGCValues xgcv;
3598 struct x_output *di = f->output_data.x;
3599 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3600 unsigned long pixel;
3601 unsigned long background = di->relief_background;
3602 Colormap cmap = FRAME_X_COLORMAP (f);
3603 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3604 Display *dpy = FRAME_X_DISPLAY (f);
3606 xgcv.graphics_exposures = False;
3607 xgcv.line_width = 1;
3609 /* Free previously allocated color. The color cell will be reused
3610 when it has been freed as many times as it was allocated, so this
3611 doesn't affect faces using the same colors. */
3612 if (relief->gc
3613 && relief->allocated_p)
3615 x_free_colors (f, &relief->pixel, 1);
3616 relief->allocated_p = 0;
3619 /* Allocate new color. */
3620 xgcv.foreground = default_pixel;
3621 pixel = background;
3622 if (dpyinfo->n_planes != 1
3623 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3625 relief->allocated_p = 1;
3626 xgcv.foreground = relief->pixel = pixel;
3629 if (relief->gc == 0)
3631 xgcv.stipple = dpyinfo->gray;
3632 mask |= GCStipple;
3633 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3635 else
3636 XChangeGC (dpy, relief->gc, mask, &xgcv);
3640 /* Set up colors for the relief lines around glyph string S. */
3642 static void
3643 x_setup_relief_colors (s)
3644 struct glyph_string *s;
3646 struct x_output *di = s->f->output_data.x;
3647 unsigned long color;
3649 if (s->face->use_box_color_for_shadows_p)
3650 color = s->face->box_color;
3651 else
3653 XGCValues xgcv;
3655 /* Get the background color of the face. */
3656 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3657 color = xgcv.background;
3660 if (di->white_relief.gc == 0
3661 || color != di->relief_background)
3663 di->relief_background = color;
3664 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3665 WHITE_PIX_DEFAULT (s->f));
3666 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3667 BLACK_PIX_DEFAULT (s->f));
3672 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3673 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3674 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3675 relief. LEFT_P non-zero means draw a relief on the left side of
3676 the rectangle. RIGHT_P non-zero means draw a relief on the right
3677 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3678 when drawing. */
3680 static void
3681 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3682 raised_p, left_p, right_p, clip_rect)
3683 struct frame *f;
3684 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3685 XRectangle *clip_rect;
3687 int i;
3688 GC gc;
3690 if (raised_p)
3691 gc = f->output_data.x->white_relief.gc;
3692 else
3693 gc = f->output_data.x->black_relief.gc;
3694 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3696 /* Top. */
3697 for (i = 0; i < width; ++i)
3698 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3699 left_x + i * left_p, top_y + i,
3700 right_x + 1 - i * right_p, top_y + i);
3702 /* Left. */
3703 if (left_p)
3704 for (i = 0; i < width; ++i)
3705 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3706 left_x + i, top_y + i, left_x + i, bottom_y - i);
3708 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3709 if (raised_p)
3710 gc = f->output_data.x->black_relief.gc;
3711 else
3712 gc = f->output_data.x->white_relief.gc;
3713 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3715 /* Bottom. */
3716 for (i = 0; i < width; ++i)
3717 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3718 left_x + i * left_p, bottom_y - i,
3719 right_x + 1 - i * right_p, bottom_y - i);
3721 /* Right. */
3722 if (right_p)
3723 for (i = 0; i < width; ++i)
3724 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3725 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3727 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3731 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3732 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3733 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3734 left side of the rectangle. RIGHT_P non-zero means draw a line
3735 on the right side of the rectangle. CLIP_RECT is the clipping
3736 rectangle to use when drawing. */
3738 static void
3739 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3740 left_p, right_p, clip_rect)
3741 struct glyph_string *s;
3742 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3743 XRectangle *clip_rect;
3745 XGCValues xgcv;
3747 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3748 XSetForeground (s->display, s->gc, s->face->box_color);
3749 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3751 /* Top. */
3752 XFillRectangle (s->display, s->window, s->gc,
3753 left_x, top_y, right_x - left_x + 1, width);
3755 /* Left. */
3756 if (left_p)
3757 XFillRectangle (s->display, s->window, s->gc,
3758 left_x, top_y, width, bottom_y - top_y + 1);
3760 /* Bottom. */
3761 XFillRectangle (s->display, s->window, s->gc,
3762 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3764 /* Right. */
3765 if (right_p)
3766 XFillRectangle (s->display, s->window, s->gc,
3767 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3769 XSetForeground (s->display, s->gc, xgcv.foreground);
3770 XSetClipMask (s->display, s->gc, None);
3774 /* Draw a box around glyph string S. */
3776 static void
3777 x_draw_glyph_string_box (s)
3778 struct glyph_string *s;
3780 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3781 int left_p, right_p;
3782 struct glyph *last_glyph;
3783 XRectangle clip_rect;
3785 last_x = window_box_right (s->w, s->area);
3786 if (s->row->full_width_p
3787 && !s->w->pseudo_window_p)
3789 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3790 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3791 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3794 /* The glyph that may have a right box line. */
3795 last_glyph = (s->cmp || s->img
3796 ? s->first_glyph
3797 : s->first_glyph + s->nchars - 1);
3799 width = s->face->box_line_width;
3800 raised_p = s->face->box == FACE_RAISED_BOX;
3801 left_x = s->x;
3802 right_x = ((s->row->full_width_p
3803 ? last_x - 1
3804 : min (last_x, s->x + s->background_width) - 1));
3805 top_y = s->y;
3806 bottom_y = top_y + s->height - 1;
3808 left_p = (s->first_glyph->left_box_line_p
3809 || (s->hl == DRAW_MOUSE_FACE
3810 && (s->prev == NULL
3811 || s->prev->hl != s->hl)));
3812 right_p = (last_glyph->right_box_line_p
3813 || (s->hl == DRAW_MOUSE_FACE
3814 && (s->next == NULL
3815 || s->next->hl != s->hl)));
3817 x_get_glyph_string_clip_rect (s, &clip_rect);
3819 if (s->face->box == FACE_SIMPLE_BOX)
3820 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3821 left_p, right_p, &clip_rect);
3822 else
3824 x_setup_relief_colors (s);
3825 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3826 width, raised_p, left_p, right_p, &clip_rect);
3831 /* Draw foreground of image glyph string S. */
3833 static void
3834 x_draw_image_foreground (s)
3835 struct glyph_string *s;
3837 int x;
3838 int y = s->ybase - image_ascent (s->img, s->face);
3840 /* If first glyph of S has a left box line, start drawing it to the
3841 right of that line. */
3842 if (s->face->box != FACE_NO_BOX
3843 && s->first_glyph->left_box_line_p)
3844 x = s->x + s->face->box_line_width;
3845 else
3846 x = s->x;
3848 /* If there is a margin around the image, adjust x- and y-position
3849 by that margin. */
3850 x += s->img->hmargin;
3851 y += s->img->vmargin;
3853 if (s->img->pixmap)
3855 if (s->img->mask)
3857 /* We can't set both a clip mask and use XSetClipRectangles
3858 because the latter also sets a clip mask. We also can't
3859 trust on the shape extension to be available
3860 (XShapeCombineRegion). So, compute the rectangle to draw
3861 manually. */
3862 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3863 | GCFunction);
3864 XGCValues xgcv;
3865 XRectangle clip_rect, image_rect, r;
3867 xgcv.clip_mask = s->img->mask;
3868 xgcv.clip_x_origin = x;
3869 xgcv.clip_y_origin = y;
3870 xgcv.function = GXcopy;
3871 XChangeGC (s->display, s->gc, mask, &xgcv);
3873 x_get_glyph_string_clip_rect (s, &clip_rect);
3874 image_rect.x = x;
3875 image_rect.y = y;
3876 image_rect.width = s->img->width;
3877 image_rect.height = s->img->height;
3878 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3879 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3880 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3882 else
3884 unsigned long mask = GCClipXOrigin | GCClipYOrigin | GCFunction;
3885 XGCValues xgcv;
3886 XRectangle clip_rect, image_rect, r;
3888 x_get_glyph_string_clip_rect (s, &clip_rect);
3889 image_rect.x = x;
3890 image_rect.y = y;
3891 image_rect.width = s->img->width;
3892 image_rect.height = s->img->height;
3893 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
3894 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
3895 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
3897 /* When the image has a mask, we can expect that at
3898 least part of a mouse highlight or a block cursor will
3899 be visible. If the image doesn't have a mask, make
3900 a block cursor visible by drawing a rectangle around
3901 the image. I believe it's looking better if we do
3902 nothing here for mouse-face. */
3903 if (s->hl == DRAW_CURSOR)
3904 XDrawRectangle (s->display, s->window, s->gc, x, y,
3905 s->img->width - 1, s->img->height - 1);
3908 else
3909 /* Draw a rectangle if image could not be loaded. */
3910 XDrawRectangle (s->display, s->window, s->gc, x, y,
3911 s->img->width - 1, s->img->height - 1);
3915 /* Draw a relief around the image glyph string S. */
3917 static void
3918 x_draw_image_relief (s)
3919 struct glyph_string *s;
3921 int x0, y0, x1, y1, thick, raised_p;
3922 XRectangle r;
3923 int x;
3924 int y = s->ybase - image_ascent (s->img, s->face);
3926 /* If first glyph of S has a left box line, start drawing it to the
3927 right of that line. */
3928 if (s->face->box != FACE_NO_BOX
3929 && s->first_glyph->left_box_line_p)
3930 x = s->x + s->face->box_line_width;
3931 else
3932 x = s->x;
3934 /* If there is a margin around the image, adjust x- and y-position
3935 by that margin. */
3936 x += s->img->hmargin;
3937 y += s->img->vmargin;
3939 if (s->hl == DRAW_IMAGE_SUNKEN
3940 || s->hl == DRAW_IMAGE_RAISED)
3942 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3943 raised_p = s->hl == DRAW_IMAGE_RAISED;
3945 else
3947 thick = abs (s->img->relief);
3948 raised_p = s->img->relief > 0;
3951 x0 = x - thick;
3952 y0 = y - thick;
3953 x1 = x + s->img->width + thick - 1;
3954 y1 = y + s->img->height + thick - 1;
3956 x_setup_relief_colors (s);
3957 x_get_glyph_string_clip_rect (s, &r);
3958 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3962 /* Draw the foreground of image glyph string S to PIXMAP. */
3964 static void
3965 x_draw_image_foreground_1 (s, pixmap)
3966 struct glyph_string *s;
3967 Pixmap pixmap;
3969 int x;
3970 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3972 /* If first glyph of S has a left box line, start drawing it to the
3973 right of that line. */
3974 if (s->face->box != FACE_NO_BOX
3975 && s->first_glyph->left_box_line_p)
3976 x = s->face->box_line_width;
3977 else
3978 x = 0;
3980 /* If there is a margin around the image, adjust x- and y-position
3981 by that margin. */
3982 x += s->img->hmargin;
3983 y += s->img->vmargin;
3985 if (s->img->pixmap)
3987 if (s->img->mask)
3989 /* We can't set both a clip mask and use XSetClipRectangles
3990 because the latter also sets a clip mask. We also can't
3991 trust on the shape extension to be available
3992 (XShapeCombineRegion). So, compute the rectangle to draw
3993 manually. */
3994 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
3995 | GCFunction);
3996 XGCValues xgcv;
3998 xgcv.clip_mask = s->img->mask;
3999 xgcv.clip_x_origin = x;
4000 xgcv.clip_y_origin = y;
4001 xgcv.function = GXcopy;
4002 XChangeGC (s->display, s->gc, mask, &xgcv);
4004 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4005 0, 0, s->img->width, s->img->height, x, y);
4006 XSetClipMask (s->display, s->gc, None);
4008 else
4010 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4011 0, 0, s->img->width, s->img->height, x, y);
4013 /* When the image has a mask, we can expect that at
4014 least part of a mouse highlight or a block cursor will
4015 be visible. If the image doesn't have a mask, make
4016 a block cursor visible by drawing a rectangle around
4017 the image. I believe it's looking better if we do
4018 nothing here for mouse-face. */
4019 if (s->hl == DRAW_CURSOR)
4020 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4021 s->img->width - 1, s->img->height - 1);
4024 else
4025 /* Draw a rectangle if image could not be loaded. */
4026 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4027 s->img->width - 1, s->img->height - 1);
4031 /* Draw part of the background of glyph string S. X, Y, W, and H
4032 give the rectangle to draw. */
4034 static void
4035 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4036 struct glyph_string *s;
4037 int x, y, w, h;
4039 if (s->stippled_p)
4041 /* Fill background with a stipple pattern. */
4042 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4043 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4044 XSetFillStyle (s->display, s->gc, FillSolid);
4046 else
4047 x_clear_glyph_string_rect (s, x, y, w, h);
4051 /* Draw image glyph string S.
4053 s->y
4054 s->x +-------------------------
4055 | s->face->box
4057 | +-------------------------
4058 | | s->img->margin
4060 | | +-------------------
4061 | | | the image
4065 static void
4066 x_draw_image_glyph_string (s)
4067 struct glyph_string *s;
4069 int x, y;
4070 int box_line_width = s->face->box_line_width;
4071 int height;
4072 Pixmap pixmap = None;
4074 height = s->height - 2 * box_line_width;
4076 /* Fill background with face under the image. Do it only if row is
4077 taller than image or if image has a clip mask to reduce
4078 flickering. */
4079 s->stippled_p = s->face->stipple != 0;
4080 if (height > s->img->height
4081 || s->img->hmargin
4082 || s->img->vmargin
4083 || s->img->mask
4084 || s->img->pixmap == 0
4085 || s->width != s->background_width)
4087 if (box_line_width && s->first_glyph->left_box_line_p)
4088 x = s->x + box_line_width;
4089 else
4090 x = s->x;
4092 y = s->y + box_line_width;
4094 if (s->img->mask)
4096 /* Create a pixmap as large as the glyph string. Fill it
4097 with the background color. Copy the image to it, using
4098 its mask. Copy the temporary pixmap to the display. */
4099 Screen *screen = FRAME_X_SCREEN (s->f);
4100 int depth = DefaultDepthOfScreen (screen);
4102 /* Create a pixmap as large as the glyph string. */
4103 pixmap = XCreatePixmap (s->display, s->window,
4104 s->background_width,
4105 s->height, depth);
4107 /* Don't clip in the following because we're working on the
4108 pixmap. */
4109 XSetClipMask (s->display, s->gc, None);
4111 /* Fill the pixmap with the background color/stipple. */
4112 if (s->stippled_p)
4114 /* Fill background with a stipple pattern. */
4115 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4116 XFillRectangle (s->display, pixmap, s->gc,
4117 0, 0, s->background_width, s->height);
4118 XSetFillStyle (s->display, s->gc, FillSolid);
4120 else
4122 XGCValues xgcv;
4123 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4124 &xgcv);
4125 XSetForeground (s->display, s->gc, xgcv.background);
4126 XFillRectangle (s->display, pixmap, s->gc,
4127 0, 0, s->background_width, s->height);
4128 XSetForeground (s->display, s->gc, xgcv.foreground);
4131 else
4132 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4134 s->background_filled_p = 1;
4137 /* Draw the foreground. */
4138 if (pixmap != None)
4140 x_draw_image_foreground_1 (s, pixmap);
4141 x_set_glyph_string_clipping (s);
4142 XCopyArea (s->display, pixmap, s->window, s->gc,
4143 0, 0, s->background_width, s->height, s->x, s->y);
4144 XFreePixmap (s->display, pixmap);
4146 else
4147 x_draw_image_foreground (s);
4149 /* If we must draw a relief around the image, do it. */
4150 if (s->img->relief
4151 || s->hl == DRAW_IMAGE_RAISED
4152 || s->hl == DRAW_IMAGE_SUNKEN)
4153 x_draw_image_relief (s);
4157 /* Draw stretch glyph string S. */
4159 static void
4160 x_draw_stretch_glyph_string (s)
4161 struct glyph_string *s;
4163 xassert (s->first_glyph->type == STRETCH_GLYPH);
4164 s->stippled_p = s->face->stipple != 0;
4166 if (s->hl == DRAW_CURSOR
4167 && !x_stretch_cursor_p)
4169 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4170 as wide as the stretch glyph. */
4171 int width = min (CANON_X_UNIT (s->f), s->background_width);
4173 /* Draw cursor. */
4174 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4176 /* Clear rest using the GC of the original non-cursor face. */
4177 if (width < s->background_width)
4179 GC gc = s->face->gc;
4180 int x = s->x + width, y = s->y;
4181 int w = s->background_width - width, h = s->height;
4182 XRectangle r;
4184 x_get_glyph_string_clip_rect (s, &r);
4185 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4187 if (s->face->stipple)
4189 /* Fill background with a stipple pattern. */
4190 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4191 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4192 XSetFillStyle (s->display, gc, FillSolid);
4194 else
4196 XGCValues xgcv;
4197 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4198 XSetForeground (s->display, gc, xgcv.background);
4199 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4200 XSetForeground (s->display, gc, xgcv.foreground);
4204 else
4205 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4206 s->height);
4208 s->background_filled_p = 1;
4212 /* Draw glyph string S. */
4214 static void
4215 x_draw_glyph_string (s)
4216 struct glyph_string *s;
4218 /* If S draws into the background of its successor, draw the
4219 background of the successor first so that S can draw into it.
4220 This makes S->next use XDrawString instead of XDrawImageString. */
4221 if (s->next && s->right_overhang && !s->for_overlaps_p)
4223 xassert (s->next->img == NULL);
4224 x_set_glyph_string_gc (s->next);
4225 x_set_glyph_string_clipping (s->next);
4226 x_draw_glyph_string_background (s->next, 1);
4229 /* Set up S->gc, set clipping and draw S. */
4230 x_set_glyph_string_gc (s);
4231 x_set_glyph_string_clipping (s);
4233 switch (s->first_glyph->type)
4235 case IMAGE_GLYPH:
4236 x_draw_image_glyph_string (s);
4237 break;
4239 case STRETCH_GLYPH:
4240 x_draw_stretch_glyph_string (s);
4241 break;
4243 case CHAR_GLYPH:
4244 if (s->for_overlaps_p)
4245 s->background_filled_p = 1;
4246 else
4247 x_draw_glyph_string_background (s, 0);
4248 x_draw_glyph_string_foreground (s);
4249 break;
4251 case COMPOSITE_GLYPH:
4252 if (s->for_overlaps_p || s->gidx > 0)
4253 s->background_filled_p = 1;
4254 else
4255 x_draw_glyph_string_background (s, 1);
4256 x_draw_composite_glyph_string_foreground (s);
4257 break;
4259 default:
4260 abort ();
4263 if (!s->for_overlaps_p)
4265 /* Draw underline. */
4266 if (s->face->underline_p)
4268 unsigned long tem, h;
4269 int y;
4271 /* Get the underline thickness. Default is 1 pixel. */
4272 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4273 h = 1;
4275 /* Get the underline position. This is the recommended
4276 vertical offset in pixels from the baseline to the top of
4277 the underline. This is a signed value according to the
4278 specs, and its default is
4280 ROUND ((maximum descent) / 2), with
4281 ROUND(x) = floor (x + 0.5) */
4283 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4284 y = s->ybase + (long) tem;
4285 else if (s->face->font)
4286 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4287 else
4288 y = s->height - h;
4290 if (s->face->underline_defaulted_p)
4291 XFillRectangle (s->display, s->window, s->gc,
4292 s->x, y, s->width, h);
4293 else
4295 XGCValues xgcv;
4296 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4297 XSetForeground (s->display, s->gc, s->face->underline_color);
4298 XFillRectangle (s->display, s->window, s->gc,
4299 s->x, y, s->width, h);
4300 XSetForeground (s->display, s->gc, xgcv.foreground);
4304 /* Draw overline. */
4305 if (s->face->overline_p)
4307 unsigned long dy = 0, h = 1;
4309 if (s->face->overline_color_defaulted_p)
4310 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4311 s->width, h);
4312 else
4314 XGCValues xgcv;
4315 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4316 XSetForeground (s->display, s->gc, s->face->overline_color);
4317 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4318 s->width, h);
4319 XSetForeground (s->display, s->gc, xgcv.foreground);
4323 /* Draw strike-through. */
4324 if (s->face->strike_through_p)
4326 unsigned long h = 1;
4327 unsigned long dy = (s->height - h) / 2;
4329 if (s->face->strike_through_color_defaulted_p)
4330 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4331 s->width, h);
4332 else
4334 XGCValues xgcv;
4335 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4336 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4337 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4338 s->width, h);
4339 XSetForeground (s->display, s->gc, xgcv.foreground);
4343 /* Draw relief. */
4344 if (s->face->box != FACE_NO_BOX)
4345 x_draw_glyph_string_box (s);
4348 /* Reset clipping. */
4349 XSetClipMask (s->display, s->gc, None);
4353 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4354 struct face **, int));
4357 /* Fill glyph string S with composition components specified by S->cmp.
4359 FACES is an array of faces for all components of this composition.
4360 S->gidx is the index of the first component for S.
4361 OVERLAPS_P non-zero means S should draw the foreground only, and
4362 use its physical height for clipping.
4364 Value is the index of a component not in S. */
4366 static int
4367 x_fill_composite_glyph_string (s, faces, overlaps_p)
4368 struct glyph_string *s;
4369 struct face **faces;
4370 int overlaps_p;
4372 int i;
4374 xassert (s);
4376 s->for_overlaps_p = overlaps_p;
4378 s->face = faces[s->gidx];
4379 s->font = s->face->font;
4380 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4382 /* For all glyphs of this composition, starting at the offset
4383 S->gidx, until we reach the end of the definition or encounter a
4384 glyph that requires the different face, add it to S. */
4385 ++s->nchars;
4386 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4387 ++s->nchars;
4389 /* All glyph strings for the same composition has the same width,
4390 i.e. the width set for the first component of the composition. */
4392 s->width = s->first_glyph->pixel_width;
4394 /* If the specified font could not be loaded, use the frame's
4395 default font, but record the fact that we couldn't load it in
4396 the glyph string so that we can draw rectangles for the
4397 characters of the glyph string. */
4398 if (s->font == NULL)
4400 s->font_not_found_p = 1;
4401 s->font = FRAME_FONT (s->f);
4404 /* Adjust base line for subscript/superscript text. */
4405 s->ybase += s->first_glyph->voffset;
4407 xassert (s->face && s->face->gc);
4409 /* This glyph string must always be drawn with 16-bit functions. */
4410 s->two_byte_p = 1;
4412 return s->gidx + s->nchars;
4416 /* Fill glyph string S from a sequence of character glyphs.
4418 FACE_ID is the face id of the string. START is the index of the
4419 first glyph to consider, END is the index of the last + 1.
4420 OVERLAPS_P non-zero means S should draw the foreground only, and
4421 use its physical height for clipping.
4423 Value is the index of the first glyph not in S. */
4425 static int
4426 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4427 struct glyph_string *s;
4428 int face_id;
4429 int start, end, overlaps_p;
4431 struct glyph *glyph, *last;
4432 int voffset;
4433 int glyph_not_available_p;
4435 xassert (s->f == XFRAME (s->w->frame));
4436 xassert (s->nchars == 0);
4437 xassert (start >= 0 && end > start);
4439 s->for_overlaps_p = overlaps_p,
4440 glyph = s->row->glyphs[s->area] + start;
4441 last = s->row->glyphs[s->area] + end;
4442 voffset = glyph->voffset;
4444 glyph_not_available_p = glyph->glyph_not_available_p;
4446 while (glyph < last
4447 && glyph->type == CHAR_GLYPH
4448 && glyph->voffset == voffset
4449 /* Same face id implies same font, nowadays. */
4450 && glyph->face_id == face_id
4451 && glyph->glyph_not_available_p == glyph_not_available_p)
4453 int two_byte_p;
4455 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4456 s->char2b + s->nchars,
4457 &two_byte_p);
4458 s->two_byte_p = two_byte_p;
4459 ++s->nchars;
4460 xassert (s->nchars <= end - start);
4461 s->width += glyph->pixel_width;
4462 ++glyph;
4465 s->font = s->face->font;
4466 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4468 /* If the specified font could not be loaded, use the frame's font,
4469 but record the fact that we couldn't load it in
4470 S->font_not_found_p so that we can draw rectangles for the
4471 characters of the glyph string. */
4472 if (s->font == NULL || glyph_not_available_p)
4474 s->font_not_found_p = 1;
4475 s->font = FRAME_FONT (s->f);
4478 /* Adjust base line for subscript/superscript text. */
4479 s->ybase += voffset;
4481 xassert (s->face && s->face->gc);
4482 return glyph - s->row->glyphs[s->area];
4486 /* Fill glyph string S from image glyph S->first_glyph. */
4488 static void
4489 x_fill_image_glyph_string (s)
4490 struct glyph_string *s;
4492 xassert (s->first_glyph->type == IMAGE_GLYPH);
4493 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4494 xassert (s->img);
4495 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4496 s->font = s->face->font;
4497 s->width = s->first_glyph->pixel_width;
4499 /* Adjust base line for subscript/superscript text. */
4500 s->ybase += s->first_glyph->voffset;
4504 /* Fill glyph string S from a sequence of stretch glyphs.
4506 ROW is the glyph row in which the glyphs are found, AREA is the
4507 area within the row. START is the index of the first glyph to
4508 consider, END is the index of the last + 1.
4510 Value is the index of the first glyph not in S. */
4512 static int
4513 x_fill_stretch_glyph_string (s, row, area, start, end)
4514 struct glyph_string *s;
4515 struct glyph_row *row;
4516 enum glyph_row_area area;
4517 int start, end;
4519 struct glyph *glyph, *last;
4520 int voffset, face_id;
4522 xassert (s->first_glyph->type == STRETCH_GLYPH);
4524 glyph = s->row->glyphs[s->area] + start;
4525 last = s->row->glyphs[s->area] + end;
4526 face_id = glyph->face_id;
4527 s->face = FACE_FROM_ID (s->f, face_id);
4528 s->font = s->face->font;
4529 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4530 s->width = glyph->pixel_width;
4531 voffset = glyph->voffset;
4533 for (++glyph;
4534 (glyph < last
4535 && glyph->type == STRETCH_GLYPH
4536 && glyph->voffset == voffset
4537 && glyph->face_id == face_id);
4538 ++glyph)
4539 s->width += glyph->pixel_width;
4541 /* Adjust base line for subscript/superscript text. */
4542 s->ybase += voffset;
4544 xassert (s->face && s->face->gc);
4545 return glyph - s->row->glyphs[s->area];
4549 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4550 of XChar2b structures for S; it can't be allocated in
4551 x_init_glyph_string because it must be allocated via `alloca'. W
4552 is the window on which S is drawn. ROW and AREA are the glyph row
4553 and area within the row from which S is constructed. START is the
4554 index of the first glyph structure covered by S. HL is a
4555 face-override for drawing S. */
4557 static void
4558 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4559 struct glyph_string *s;
4560 XChar2b *char2b;
4561 struct window *w;
4562 struct glyph_row *row;
4563 enum glyph_row_area area;
4564 int start;
4565 enum draw_glyphs_face hl;
4567 bzero (s, sizeof *s);
4568 s->w = w;
4569 s->f = XFRAME (w->frame);
4570 s->display = FRAME_X_DISPLAY (s->f);
4571 s->window = FRAME_X_WINDOW (s->f);
4572 s->char2b = char2b;
4573 s->hl = hl;
4574 s->row = row;
4575 s->area = area;
4576 s->first_glyph = row->glyphs[area] + start;
4577 s->height = row->height;
4578 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4580 /* Display the internal border below the tool-bar window. */
4581 if (s->w == XWINDOW (s->f->tool_bar_window))
4582 s->y -= s->f->output_data.x->internal_border_width;
4584 s->ybase = s->y + row->ascent;
4588 /* Set background width of glyph string S. START is the index of the
4589 first glyph following S. LAST_X is the right-most x-position + 1
4590 in the drawing area. */
4592 static INLINE void
4593 x_set_glyph_string_background_width (s, start, last_x)
4594 struct glyph_string *s;
4595 int start;
4596 int last_x;
4598 /* If the face of this glyph string has to be drawn to the end of
4599 the drawing area, set S->extends_to_end_of_line_p. */
4600 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4602 if (start == s->row->used[s->area]
4603 && s->hl == DRAW_NORMAL_TEXT
4604 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4605 || s->face->background != default_face->background
4606 || s->face->stipple != default_face->stipple))
4607 s->extends_to_end_of_line_p = 1;
4609 /* If S extends its face to the end of the line, set its
4610 background_width to the distance to the right edge of the drawing
4611 area. */
4612 if (s->extends_to_end_of_line_p)
4613 s->background_width = last_x - s->x + 1;
4614 else
4615 s->background_width = s->width;
4619 /* Add a glyph string for a stretch glyph to the list of strings
4620 between HEAD and TAIL. START is the index of the stretch glyph in
4621 row area AREA of glyph row ROW. END is the index of the last glyph
4622 in that glyph row area. X is the current output position assigned
4623 to the new glyph string constructed. HL overrides that face of the
4624 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4625 is the right-most x-position of the drawing area. */
4627 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4628 and below -- keep them on one line. */
4629 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4630 do \
4632 s = (struct glyph_string *) alloca (sizeof *s); \
4633 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4634 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4635 x_append_glyph_string (&HEAD, &TAIL, s); \
4636 s->x = (X); \
4638 while (0)
4641 /* Add a glyph string for an image glyph to the list of strings
4642 between HEAD and TAIL. START is the index of the image glyph in
4643 row area AREA of glyph row ROW. END is the index of the last glyph
4644 in that glyph row area. X is the current output position assigned
4645 to the new glyph string constructed. HL overrides that face of the
4646 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4647 is the right-most x-position of the drawing area. */
4649 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4650 do \
4652 s = (struct glyph_string *) alloca (sizeof *s); \
4653 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4654 x_fill_image_glyph_string (s); \
4655 x_append_glyph_string (&HEAD, &TAIL, s); \
4656 ++START; \
4657 s->x = (X); \
4659 while (0)
4662 /* Add a glyph string for a sequence of character glyphs to the list
4663 of strings between HEAD and TAIL. START is the index of the first
4664 glyph in row area AREA of glyph row ROW that is part of the new
4665 glyph string. END is the index of the last glyph in that glyph row
4666 area. X is the current output position assigned to the new glyph
4667 string constructed. HL overrides that face of the glyph; e.g. it
4668 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4669 right-most x-position of the drawing area. */
4671 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4672 do \
4674 int c, face_id; \
4675 XChar2b *char2b; \
4677 c = (ROW)->glyphs[AREA][START].u.ch; \
4678 face_id = (ROW)->glyphs[AREA][START].face_id; \
4680 s = (struct glyph_string *) alloca (sizeof *s); \
4681 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4682 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4683 x_append_glyph_string (&HEAD, &TAIL, s); \
4684 s->x = (X); \
4685 START = x_fill_glyph_string (s, face_id, START, END, \
4686 OVERLAPS_P); \
4688 while (0)
4691 /* Add a glyph string for a composite sequence to the list of strings
4692 between HEAD and TAIL. START is the index of the first glyph in
4693 row area AREA of glyph row ROW that is part of the new glyph
4694 string. END is the index of the last glyph in that glyph row area.
4695 X is the current output position assigned to the new glyph string
4696 constructed. HL overrides that face of the glyph; e.g. it is
4697 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4698 x-position of the drawing area. */
4700 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4701 do { \
4702 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4703 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4704 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4705 struct composition *cmp = composition_table[cmp_id]; \
4706 int glyph_len = cmp->glyph_len; \
4707 XChar2b *char2b; \
4708 struct face **faces; \
4709 struct glyph_string *first_s = NULL; \
4710 int n; \
4712 base_face = base_face->ascii_face; \
4713 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4714 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4715 /* At first, fill in `char2b' and `faces'. */ \
4716 for (n = 0; n < glyph_len; n++) \
4718 int c = COMPOSITION_GLYPH (cmp, n); \
4719 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4720 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4721 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4722 this_face_id, char2b + n, 1); \
4725 /* Make glyph_strings for each glyph sequence that is drawable by \
4726 the same face, and append them to HEAD/TAIL. */ \
4727 for (n = 0; n < cmp->glyph_len;) \
4729 s = (struct glyph_string *) alloca (sizeof *s); \
4730 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4731 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4732 s->cmp = cmp; \
4733 s->gidx = n; \
4734 s->x = (X); \
4736 if (n == 0) \
4737 first_s = s; \
4739 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4742 ++START; \
4743 s = first_s; \
4744 } while (0)
4747 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4748 of AREA of glyph row ROW on window W between indices START and END.
4749 HL overrides the face for drawing glyph strings, e.g. it is
4750 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4751 x-positions of the drawing area.
4753 This is an ugly monster macro construct because we must use alloca
4754 to allocate glyph strings (because x_draw_glyphs can be called
4755 asynchronously). */
4757 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4758 do \
4760 HEAD = TAIL = NULL; \
4761 while (START < END) \
4763 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4764 switch (first_glyph->type) \
4766 case CHAR_GLYPH: \
4767 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4768 TAIL, HL, X, LAST_X, \
4769 OVERLAPS_P); \
4770 break; \
4772 case COMPOSITE_GLYPH: \
4773 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4774 HEAD, TAIL, HL, X, LAST_X,\
4775 OVERLAPS_P); \
4776 break; \
4778 case STRETCH_GLYPH: \
4779 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4780 HEAD, TAIL, HL, X, LAST_X); \
4781 break; \
4783 case IMAGE_GLYPH: \
4784 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4785 TAIL, HL, X, LAST_X); \
4786 break; \
4788 default: \
4789 abort (); \
4792 x_set_glyph_string_background_width (s, START, LAST_X); \
4793 (X) += s->width; \
4796 while (0)
4799 /* Draw glyphs between START and END in AREA of ROW on window W,
4800 starting at x-position X. X is relative to AREA in W. HL is a
4801 face-override with the following meaning:
4803 DRAW_NORMAL_TEXT draw normally
4804 DRAW_CURSOR draw in cursor face
4805 DRAW_MOUSE_FACE draw in mouse face.
4806 DRAW_INVERSE_VIDEO draw in mode line face
4807 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4808 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4810 If REAL_START is non-null, return in *REAL_START the real starting
4811 position for display. This can be different from START in case
4812 overlapping glyphs must be displayed. If REAL_END is non-null,
4813 return in *REAL_END the real end position for display. This can be
4814 different from END in case overlapping glyphs must be displayed.
4816 If OVERLAPS_P is non-zero, draw only the foreground of characters
4817 and clip to the physical height of ROW.
4819 Value is the x-position reached, relative to AREA of W. */
4821 static int
4822 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4823 overlaps_p)
4824 struct window *w;
4825 int x;
4826 struct glyph_row *row;
4827 enum glyph_row_area area;
4828 int start, end;
4829 enum draw_glyphs_face hl;
4830 int *real_start, *real_end;
4831 int overlaps_p;
4833 struct glyph_string *head, *tail;
4834 struct glyph_string *s;
4835 int last_x, area_width;
4836 int x_reached;
4837 int i, j;
4839 /* Let's rather be paranoid than getting a SEGV. */
4840 end = min (end, row->used[area]);
4841 start = max (0, start);
4842 start = min (end, start);
4843 if (real_start)
4844 *real_start = start;
4845 if (real_end)
4846 *real_end = end;
4848 /* Translate X to frame coordinates. Set last_x to the right
4849 end of the drawing area. */
4850 if (row->full_width_p)
4852 /* X is relative to the left edge of W, without scroll bars
4853 or flag areas. */
4854 struct frame *f = XFRAME (w->frame);
4855 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4856 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4858 x += window_left_x;
4859 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4860 last_x = window_left_x + area_width;
4862 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4864 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4865 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4866 last_x += width;
4867 else
4868 x -= width;
4871 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4872 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4874 else
4876 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4877 area_width = window_box_width (w, area);
4878 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4881 /* Build a doubly-linked list of glyph_string structures between
4882 head and tail from what we have to draw. Note that the macro
4883 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4884 the reason we use a separate variable `i'. */
4885 i = start;
4886 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4887 overlaps_p);
4888 if (tail)
4889 x_reached = tail->x + tail->background_width;
4890 else
4891 x_reached = x;
4893 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4894 the row, redraw some glyphs in front or following the glyph
4895 strings built above. */
4896 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4898 int dummy_x = 0;
4899 struct glyph_string *h, *t;
4901 /* Compute overhangs for all glyph strings. */
4902 for (s = head; s; s = s->next)
4903 x_compute_glyph_string_overhangs (s);
4905 /* Prepend glyph strings for glyphs in front of the first glyph
4906 string that are overwritten because of the first glyph
4907 string's left overhang. The background of all strings
4908 prepended must be drawn because the first glyph string
4909 draws over it. */
4910 i = x_left_overwritten (head);
4911 if (i >= 0)
4913 j = i;
4914 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4915 DRAW_NORMAL_TEXT, dummy_x, last_x,
4916 overlaps_p);
4917 start = i;
4918 if (real_start)
4919 *real_start = start;
4920 x_compute_overhangs_and_x (t, head->x, 1);
4921 x_prepend_glyph_string_lists (&head, &tail, h, t);
4924 /* Prepend glyph strings for glyphs in front of the first glyph
4925 string that overwrite that glyph string because of their
4926 right overhang. For these strings, only the foreground must
4927 be drawn, because it draws over the glyph string at `head'.
4928 The background must not be drawn because this would overwrite
4929 right overhangs of preceding glyphs for which no glyph
4930 strings exist. */
4931 i = x_left_overwriting (head);
4932 if (i >= 0)
4934 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4935 DRAW_NORMAL_TEXT, dummy_x, last_x,
4936 overlaps_p);
4937 for (s = h; s; s = s->next)
4938 s->background_filled_p = 1;
4939 if (real_start)
4940 *real_start = i;
4941 x_compute_overhangs_and_x (t, head->x, 1);
4942 x_prepend_glyph_string_lists (&head, &tail, h, t);
4945 /* Append glyphs strings for glyphs following the last glyph
4946 string tail that are overwritten by tail. The background of
4947 these strings has to be drawn because tail's foreground draws
4948 over it. */
4949 i = x_right_overwritten (tail);
4950 if (i >= 0)
4952 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4953 DRAW_NORMAL_TEXT, x, last_x,
4954 overlaps_p);
4955 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4956 x_append_glyph_string_lists (&head, &tail, h, t);
4957 if (real_end)
4958 *real_end = i;
4961 /* Append glyph strings for glyphs following the last glyph
4962 string tail that overwrite tail. The foreground of such
4963 glyphs has to be drawn because it writes into the background
4964 of tail. The background must not be drawn because it could
4965 paint over the foreground of following glyphs. */
4966 i = x_right_overwriting (tail);
4967 if (i >= 0)
4969 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4970 DRAW_NORMAL_TEXT, x, last_x,
4971 overlaps_p);
4972 for (s = h; s; s = s->next)
4973 s->background_filled_p = 1;
4974 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4975 x_append_glyph_string_lists (&head, &tail, h, t);
4976 if (real_end)
4977 *real_end = i;
4981 /* Draw all strings. */
4982 for (s = head; s; s = s->next)
4983 x_draw_glyph_string (s);
4985 /* Value is the x-position up to which drawn, relative to AREA of W.
4986 This doesn't include parts drawn because of overhangs. */
4987 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4988 if (!row->full_width_p)
4990 if (area > LEFT_MARGIN_AREA)
4991 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4992 if (area > TEXT_AREA)
4993 x_reached -= window_box_width (w, TEXT_AREA);
4996 return x_reached;
5000 /* Fix the display of area AREA of overlapping row ROW in window W. */
5002 static void
5003 x_fix_overlapping_area (w, row, area)
5004 struct window *w;
5005 struct glyph_row *row;
5006 enum glyph_row_area area;
5008 int i, x;
5010 BLOCK_INPUT;
5012 if (area == LEFT_MARGIN_AREA)
5013 x = 0;
5014 else if (area == TEXT_AREA)
5015 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5016 else
5017 x = (window_box_width (w, LEFT_MARGIN_AREA)
5018 + window_box_width (w, TEXT_AREA));
5020 for (i = 0; i < row->used[area];)
5022 if (row->glyphs[area][i].overlaps_vertically_p)
5024 int start = i, start_x = x;
5028 x += row->glyphs[area][i].pixel_width;
5029 ++i;
5031 while (i < row->used[area]
5032 && row->glyphs[area][i].overlaps_vertically_p);
5034 x_draw_glyphs (w, start_x, row, area, start, i,
5035 (row->inverse_p
5036 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5037 NULL, NULL, 1);
5039 else
5041 x += row->glyphs[area][i].pixel_width;
5042 ++i;
5046 UNBLOCK_INPUT;
5050 /* Output LEN glyphs starting at START at the nominal cursor position.
5051 Advance the nominal cursor over the text. The global variable
5052 updated_window contains the window being updated, updated_row is
5053 the glyph row being updated, and updated_area is the area of that
5054 row being updated. */
5056 static void
5057 x_write_glyphs (start, len)
5058 struct glyph *start;
5059 int len;
5061 int x, hpos, real_start, real_end;
5063 xassert (updated_window && updated_row);
5064 BLOCK_INPUT;
5066 /* Write glyphs. */
5068 hpos = start - updated_row->glyphs[updated_area];
5069 x = x_draw_glyphs (updated_window, output_cursor.x,
5070 updated_row, updated_area,
5071 hpos, hpos + len,
5072 (updated_row->inverse_p
5073 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5074 &real_start, &real_end, 0);
5076 /* If we drew over the cursor, note that it is not visible any more. */
5077 note_overwritten_text_cursor (updated_window, real_start,
5078 real_end - real_start);
5080 UNBLOCK_INPUT;
5082 /* Advance the output cursor. */
5083 output_cursor.hpos += len;
5084 output_cursor.x = x;
5088 /* Insert LEN glyphs from START at the nominal cursor position. */
5090 static void
5091 x_insert_glyphs (start, len)
5092 struct glyph *start;
5093 register int len;
5095 struct frame *f;
5096 struct window *w;
5097 int line_height, shift_by_width, shifted_region_width;
5098 struct glyph_row *row;
5099 struct glyph *glyph;
5100 int frame_x, frame_y, hpos, real_start, real_end;
5102 xassert (updated_window && updated_row);
5103 BLOCK_INPUT;
5104 w = updated_window;
5105 f = XFRAME (WINDOW_FRAME (w));
5107 /* Get the height of the line we are in. */
5108 row = updated_row;
5109 line_height = row->height;
5111 /* Get the width of the glyphs to insert. */
5112 shift_by_width = 0;
5113 for (glyph = start; glyph < start + len; ++glyph)
5114 shift_by_width += glyph->pixel_width;
5116 /* Get the width of the region to shift right. */
5117 shifted_region_width = (window_box_width (w, updated_area)
5118 - output_cursor.x
5119 - shift_by_width);
5121 /* Shift right. */
5122 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
5123 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5124 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5125 f->output_data.x->normal_gc,
5126 frame_x, frame_y,
5127 shifted_region_width, line_height,
5128 frame_x + shift_by_width, frame_y);
5130 /* Write the glyphs. */
5131 hpos = start - row->glyphs[updated_area];
5132 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5133 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5134 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5136 /* Advance the output cursor. */
5137 output_cursor.hpos += len;
5138 output_cursor.x += shift_by_width;
5139 UNBLOCK_INPUT;
5143 /* Delete N glyphs at the nominal cursor position. Not implemented
5144 for X frames. */
5146 static void
5147 x_delete_glyphs (n)
5148 register int n;
5150 abort ();
5154 /* Erase the current text line from the nominal cursor position
5155 (inclusive) to pixel column TO_X (exclusive). The idea is that
5156 everything from TO_X onward is already erased.
5158 TO_X is a pixel position relative to updated_area of
5159 updated_window. TO_X == -1 means clear to the end of this area. */
5161 static void
5162 x_clear_end_of_line (to_x)
5163 int to_x;
5165 struct frame *f;
5166 struct window *w = updated_window;
5167 int max_x, min_y, max_y;
5168 int from_x, from_y, to_y;
5170 xassert (updated_window && updated_row);
5171 f = XFRAME (w->frame);
5173 if (updated_row->full_width_p)
5175 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5176 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5177 && !w->pseudo_window_p)
5178 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5180 else
5181 max_x = window_box_width (w, updated_area);
5182 max_y = window_text_bottom_y (w);
5184 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5185 of window. For TO_X > 0, truncate to end of drawing area. */
5186 if (to_x == 0)
5187 return;
5188 else if (to_x < 0)
5189 to_x = max_x;
5190 else
5191 to_x = min (to_x, max_x);
5193 to_y = min (max_y, output_cursor.y + updated_row->height);
5195 /* Notice if the cursor will be cleared by this operation. */
5196 if (!updated_row->full_width_p)
5197 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5199 from_x = output_cursor.x;
5201 /* Translate to frame coordinates. */
5202 if (updated_row->full_width_p)
5204 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5205 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5207 else
5209 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5210 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5213 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5214 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5215 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5217 /* Prevent inadvertently clearing to end of the X window. */
5218 if (to_x > from_x && to_y > from_y)
5220 BLOCK_INPUT;
5221 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5222 from_x, from_y, to_x - from_x, to_y - from_y,
5223 False);
5224 UNBLOCK_INPUT;
5229 /* Clear entire frame. If updating_frame is non-null, clear that
5230 frame. Otherwise clear the selected frame. */
5232 static void
5233 x_clear_frame ()
5235 struct frame *f;
5237 if (updating_frame)
5238 f = updating_frame;
5239 else
5240 f = SELECTED_FRAME ();
5242 /* Clearing the frame will erase any cursor, so mark them all as no
5243 longer visible. */
5244 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5245 output_cursor.hpos = output_cursor.vpos = 0;
5246 output_cursor.x = -1;
5248 /* We don't set the output cursor here because there will always
5249 follow an explicit cursor_to. */
5250 BLOCK_INPUT;
5251 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5253 /* We have to clear the scroll bars, too. If we have changed
5254 colors or something like that, then they should be notified. */
5255 x_scroll_bar_clear (f);
5257 XFlush (FRAME_X_DISPLAY (f));
5258 UNBLOCK_INPUT;
5263 /* Invert the middle quarter of the frame for .15 sec. */
5265 /* We use the select system call to do the waiting, so we have to make
5266 sure it's available. If it isn't, we just won't do visual bells. */
5268 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5271 /* Subtract the `struct timeval' values X and Y, storing the result in
5272 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5274 static int
5275 timeval_subtract (result, x, y)
5276 struct timeval *result, x, y;
5278 /* Perform the carry for the later subtraction by updating y. This
5279 is safer because on some systems the tv_sec member is unsigned. */
5280 if (x.tv_usec < y.tv_usec)
5282 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5283 y.tv_usec -= 1000000 * nsec;
5284 y.tv_sec += nsec;
5287 if (x.tv_usec - y.tv_usec > 1000000)
5289 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5290 y.tv_usec += 1000000 * nsec;
5291 y.tv_sec -= nsec;
5294 /* Compute the time remaining to wait. tv_usec is certainly
5295 positive. */
5296 result->tv_sec = x.tv_sec - y.tv_sec;
5297 result->tv_usec = x.tv_usec - y.tv_usec;
5299 /* Return indication of whether the result should be considered
5300 negative. */
5301 return x.tv_sec < y.tv_sec;
5304 void
5305 XTflash (f)
5306 struct frame *f;
5308 BLOCK_INPUT;
5311 GC gc;
5313 /* Create a GC that will use the GXxor function to flip foreground
5314 pixels into background pixels. */
5316 XGCValues values;
5318 values.function = GXxor;
5319 values.foreground = (f->output_data.x->foreground_pixel
5320 ^ f->output_data.x->background_pixel);
5322 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5323 GCFunction | GCForeground, &values);
5327 /* Get the height not including a menu bar widget. */
5328 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5329 /* Height of each line to flash. */
5330 int flash_height = FRAME_LINE_HEIGHT (f);
5331 /* These will be the left and right margins of the rectangles. */
5332 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5333 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5335 int width;
5337 /* Don't flash the area between a scroll bar and the frame
5338 edge it is next to. */
5339 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5341 case vertical_scroll_bar_left:
5342 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5343 break;
5345 case vertical_scroll_bar_right:
5346 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5347 break;
5349 default:
5350 break;
5353 width = flash_right - flash_left;
5355 /* If window is tall, flash top and bottom line. */
5356 if (height > 3 * FRAME_LINE_HEIGHT (f))
5358 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5359 flash_left,
5360 (FRAME_INTERNAL_BORDER_WIDTH (f)
5361 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5362 width, flash_height);
5363 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5364 flash_left,
5365 (height - flash_height
5366 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5367 width, flash_height);
5369 else
5370 /* If it is short, flash it all. */
5371 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5372 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5373 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5375 x_flush (f);
5378 struct timeval wakeup;
5380 EMACS_GET_TIME (wakeup);
5382 /* Compute time to wait until, propagating carry from usecs. */
5383 wakeup.tv_usec += 150000;
5384 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5385 wakeup.tv_usec %= 1000000;
5387 /* Keep waiting until past the time wakeup. */
5388 while (1)
5390 struct timeval timeout;
5392 EMACS_GET_TIME (timeout);
5394 /* In effect, timeout = wakeup - timeout.
5395 Break if result would be negative. */
5396 if (timeval_subtract (&timeout, wakeup, timeout))
5397 break;
5399 /* Try to wait that long--but we might wake up sooner. */
5400 select (0, NULL, NULL, NULL, &timeout);
5404 /* If window is tall, flash top and bottom line. */
5405 if (height > 3 * FRAME_LINE_HEIGHT (f))
5407 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5408 flash_left,
5409 (FRAME_INTERNAL_BORDER_WIDTH (f)
5410 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5411 width, flash_height);
5412 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5413 flash_left,
5414 (height - flash_height
5415 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5416 width, flash_height);
5418 else
5419 /* If it is short, flash it all. */
5420 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5421 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5422 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5424 XFreeGC (FRAME_X_DISPLAY (f), gc);
5425 x_flush (f);
5429 UNBLOCK_INPUT;
5432 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5435 /* Make audible bell. */
5437 void
5438 XTring_bell ()
5440 struct frame *f = SELECTED_FRAME ();
5442 if (FRAME_X_DISPLAY (f))
5444 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5445 if (visible_bell)
5446 XTflash (f);
5447 else
5448 #endif
5450 BLOCK_INPUT;
5451 XBell (FRAME_X_DISPLAY (f), 0);
5452 XFlush (FRAME_X_DISPLAY (f));
5453 UNBLOCK_INPUT;
5459 /* Specify how many text lines, from the top of the window,
5460 should be affected by insert-lines and delete-lines operations.
5461 This, and those operations, are used only within an update
5462 that is bounded by calls to x_update_begin and x_update_end. */
5464 static void
5465 XTset_terminal_window (n)
5466 register int n;
5468 /* This function intentionally left blank. */
5473 /***********************************************************************
5474 Line Dance
5475 ***********************************************************************/
5477 /* Perform an insert-lines or delete-lines operation, inserting N
5478 lines or deleting -N lines at vertical position VPOS. */
5480 static void
5481 x_ins_del_lines (vpos, n)
5482 int vpos, n;
5484 abort ();
5488 /* Scroll part of the display as described by RUN. */
5490 static void
5491 x_scroll_run (w, run)
5492 struct window *w;
5493 struct run *run;
5495 struct frame *f = XFRAME (w->frame);
5496 int x, y, width, height, from_y, to_y, bottom_y;
5498 /* Get frame-relative bounding box of the text display area of W,
5499 without mode lines. Include in this box the flags areas to the
5500 left and right of W. */
5501 window_box (w, -1, &x, &y, &width, &height);
5502 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5503 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5505 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5506 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5507 bottom_y = y + height;
5509 if (to_y < from_y)
5511 /* Scrolling up. Make sure we don't copy part of the mode
5512 line at the bottom. */
5513 if (from_y + run->height > bottom_y)
5514 height = bottom_y - from_y;
5515 else
5516 height = run->height;
5518 else
5520 /* Scolling down. Make sure we don't copy over the mode line.
5521 at the bottom. */
5522 if (to_y + run->height > bottom_y)
5523 height = bottom_y - to_y;
5524 else
5525 height = run->height;
5528 BLOCK_INPUT;
5530 /* Cursor off. Will be switched on again in x_update_window_end. */
5531 updated_window = w;
5532 x_clear_cursor (w);
5534 XCopyArea (FRAME_X_DISPLAY (f),
5535 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5536 f->output_data.x->normal_gc,
5537 x, from_y,
5538 width, height,
5539 x, to_y);
5541 UNBLOCK_INPUT;
5546 /***********************************************************************
5547 Exposure Events
5548 ***********************************************************************/
5550 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5551 corner of the exposed rectangle. W and H are width and height of
5552 the exposed area. All are pixel values. W or H zero means redraw
5553 the entire frame. */
5555 static void
5556 expose_frame (f, x, y, w, h)
5557 struct frame *f;
5558 int x, y, w, h;
5560 XRectangle r;
5562 TRACE ((stderr, "expose_frame "));
5564 /* No need to redraw if frame will be redrawn soon. */
5565 if (FRAME_GARBAGED_P (f))
5567 TRACE ((stderr, " garbaged\n"));
5568 return;
5571 /* If basic faces haven't been realized yet, there is no point in
5572 trying to redraw anything. This can happen when we get an expose
5573 event while Emacs is starting, e.g. by moving another window. */
5574 if (FRAME_FACE_CACHE (f) == NULL
5575 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5577 TRACE ((stderr, " no faces\n"));
5578 return;
5581 if (w == 0 || h == 0)
5583 r.x = r.y = 0;
5584 r.width = CANON_X_UNIT (f) * f->width;
5585 r.height = CANON_Y_UNIT (f) * f->height;
5587 else
5589 r.x = x;
5590 r.y = y;
5591 r.width = w;
5592 r.height = h;
5595 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5596 expose_window_tree (XWINDOW (f->root_window), &r);
5598 if (WINDOWP (f->tool_bar_window))
5600 struct window *w = XWINDOW (f->tool_bar_window);
5601 XRectangle window_rect;
5602 XRectangle intersection_rect;
5603 int window_x, window_y, window_width, window_height;
5606 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5607 window_rect.x = window_x;
5608 window_rect.y = window_y;
5609 window_rect.width = window_width;
5610 window_rect.height = window_height;
5612 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5613 expose_window (w, &intersection_rect);
5616 #ifndef USE_X_TOOLKIT
5617 if (WINDOWP (f->menu_bar_window))
5619 struct window *w = XWINDOW (f->menu_bar_window);
5620 XRectangle window_rect;
5621 XRectangle intersection_rect;
5622 int window_x, window_y, window_width, window_height;
5625 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5626 window_rect.x = window_x;
5627 window_rect.y = window_y;
5628 window_rect.width = window_width;
5629 window_rect.height = window_height;
5631 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5632 expose_window (w, &intersection_rect);
5634 #endif /* not USE_X_TOOLKIT */
5638 /* Redraw (parts) of all windows in the window tree rooted at W that
5639 intersect R. R contains frame pixel coordinates. */
5641 static void
5642 expose_window_tree (w, r)
5643 struct window *w;
5644 XRectangle *r;
5646 while (w)
5648 if (!NILP (w->hchild))
5649 expose_window_tree (XWINDOW (w->hchild), r);
5650 else if (!NILP (w->vchild))
5651 expose_window_tree (XWINDOW (w->vchild), r);
5652 else
5654 XRectangle window_rect;
5655 XRectangle intersection_rect;
5656 struct frame *f = XFRAME (w->frame);
5657 int window_x, window_y, window_width, window_height;
5659 /* Frame-relative pixel rectangle of W. */
5660 window_box (w, -1, &window_x, &window_y, &window_width,
5661 &window_height);
5662 window_rect.x
5663 = (window_x
5664 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5665 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5666 window_rect.y = window_y;
5667 window_rect.width
5668 = (window_width
5669 + FRAME_X_FLAGS_AREA_WIDTH (f)
5670 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5671 window_rect.height
5672 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5674 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5675 expose_window (w, &intersection_rect);
5678 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5683 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5684 which intersects rectangle R. R is in window-relative coordinates. */
5686 static void
5687 expose_area (w, row, r, area)
5688 struct window *w;
5689 struct glyph_row *row;
5690 XRectangle *r;
5691 enum glyph_row_area area;
5693 struct glyph *first = row->glyphs[area];
5694 struct glyph *end = row->glyphs[area] + row->used[area];
5695 struct glyph *last;
5696 int first_x, start_x, x;
5698 if (area == TEXT_AREA && row->fill_line_p)
5699 /* If row extends face to end of line write the whole line. */
5700 x_draw_glyphs (w, 0, row, area,
5701 0, row->used[area],
5702 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5703 NULL, NULL, 0);
5704 else
5706 /* Set START_X to the window-relative start position for drawing glyphs of
5707 AREA. The first glyph of the text area can be partially visible.
5708 The first glyphs of other areas cannot. */
5709 if (area == LEFT_MARGIN_AREA)
5710 start_x = 0;
5711 else if (area == TEXT_AREA)
5712 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5713 else
5714 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5715 + window_box_width (w, TEXT_AREA));
5716 x = start_x;
5718 /* Find the first glyph that must be redrawn. */
5719 while (first < end
5720 && x + first->pixel_width < r->x)
5722 x += first->pixel_width;
5723 ++first;
5726 /* Find the last one. */
5727 last = first;
5728 first_x = x;
5729 while (last < end
5730 && x < r->x + r->width)
5732 x += last->pixel_width;
5733 ++last;
5736 /* Repaint. */
5737 if (last > first)
5738 x_draw_glyphs (w, first_x - start_x, row, area,
5739 first - row->glyphs[area],
5740 last - row->glyphs[area],
5741 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5742 NULL, NULL, 0);
5747 /* Redraw the parts of the glyph row ROW on window W intersecting
5748 rectangle R. R is in window-relative coordinates. */
5750 static void
5751 expose_line (w, row, r)
5752 struct window *w;
5753 struct glyph_row *row;
5754 XRectangle *r;
5756 xassert (row->enabled_p);
5758 if (row->mode_line_p || w->pseudo_window_p)
5759 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5760 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5761 NULL, NULL, 0);
5762 else
5764 if (row->used[LEFT_MARGIN_AREA])
5765 expose_area (w, row, r, LEFT_MARGIN_AREA);
5766 if (row->used[TEXT_AREA])
5767 expose_area (w, row, r, TEXT_AREA);
5768 if (row->used[RIGHT_MARGIN_AREA])
5769 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5770 x_draw_row_bitmaps (w, row);
5775 /* Return non-zero if W's cursor intersects rectangle R. */
5777 static int
5778 x_phys_cursor_in_rect_p (w, r)
5779 struct window *w;
5780 XRectangle *r;
5782 XRectangle cr, result;
5783 struct glyph *cursor_glyph;
5785 cursor_glyph = get_phys_cursor_glyph (w);
5786 if (cursor_glyph)
5788 cr.x = w->phys_cursor.x;
5789 cr.y = w->phys_cursor.y;
5790 cr.width = cursor_glyph->pixel_width;
5791 cr.height = w->phys_cursor_height;
5792 return x_intersect_rectangles (&cr, r, &result);
5794 else
5795 return 0;
5799 /* Redraw a rectangle of window W. R is a rectangle in window
5800 relative coordinates. Call this function with input blocked. */
5802 static void
5803 expose_window (w, r)
5804 struct window *w;
5805 XRectangle *r;
5807 struct glyph_row *row;
5808 int y;
5809 int yb = window_text_bottom_y (w);
5810 int cursor_cleared_p;
5812 /* If window is not yet fully initialized, do nothing. This can
5813 happen when toolkit scroll bars are used and a window is split.
5814 Reconfiguring the scroll bar will generate an expose for a newly
5815 created window. */
5816 if (w->current_matrix == NULL || w == updated_window)
5817 return;
5819 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5820 r->x, r->y, r->width, r->height));
5822 /* Convert to window coordinates. */
5823 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5824 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5826 /* Turn off the cursor. */
5827 if (!w->pseudo_window_p
5828 && x_phys_cursor_in_rect_p (w, r))
5830 x_clear_cursor (w);
5831 cursor_cleared_p = 1;
5833 else
5834 cursor_cleared_p = 0;
5836 /* Find the first row intersecting the rectangle R. */
5837 row = w->current_matrix->rows;
5838 y = 0;
5839 while (row->enabled_p
5840 && y < yb
5841 && y + row->height < r->y)
5843 y += row->height;
5844 ++row;
5847 /* Display the text in the rectangle, one text line at a time. */
5848 while (row->enabled_p
5849 && y < yb
5850 && y < r->y + r->height)
5852 expose_line (w, row, r);
5853 y += row->height;
5854 ++row;
5857 /* Display the mode line if there is one. */
5858 if (WINDOW_WANTS_MODELINE_P (w)
5859 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5860 row->enabled_p)
5861 && row->y < r->y + r->height)
5862 expose_line (w, row, r);
5864 if (!w->pseudo_window_p)
5866 /* Draw border between windows. */
5867 x_draw_vertical_border (w);
5869 /* Turn the cursor on again. */
5870 if (cursor_cleared_p)
5871 x_update_window_cursor (w, 1);
5876 /* Determine the intersection of two rectangles R1 and R2. Return
5877 the intersection in *RESULT. Value is non-zero if RESULT is not
5878 empty. */
5880 static int
5881 x_intersect_rectangles (r1, r2, result)
5882 XRectangle *r1, *r2, *result;
5884 XRectangle *left, *right;
5885 XRectangle *upper, *lower;
5886 int intersection_p = 0;
5888 /* Rearrange so that R1 is the left-most rectangle. */
5889 if (r1->x < r2->x)
5890 left = r1, right = r2;
5891 else
5892 left = r2, right = r1;
5894 /* X0 of the intersection is right.x0, if this is inside R1,
5895 otherwise there is no intersection. */
5896 if (right->x <= left->x + left->width)
5898 result->x = right->x;
5900 /* The right end of the intersection is the minimum of the
5901 the right ends of left and right. */
5902 result->width = (min (left->x + left->width, right->x + right->width)
5903 - result->x);
5905 /* Same game for Y. */
5906 if (r1->y < r2->y)
5907 upper = r1, lower = r2;
5908 else
5909 upper = r2, lower = r1;
5911 /* The upper end of the intersection is lower.y0, if this is inside
5912 of upper. Otherwise, there is no intersection. */
5913 if (lower->y <= upper->y + upper->height)
5915 result->y = lower->y;
5917 /* The lower end of the intersection is the minimum of the lower
5918 ends of upper and lower. */
5919 result->height = (min (lower->y + lower->height,
5920 upper->y + upper->height)
5921 - result->y);
5922 intersection_p = 1;
5926 return intersection_p;
5933 static void
5934 frame_highlight (f)
5935 struct frame *f;
5937 /* We used to only do this if Vx_no_window_manager was non-nil, but
5938 the ICCCM (section 4.1.6) says that the window's border pixmap
5939 and border pixel are window attributes which are "private to the
5940 client", so we can always change it to whatever we want. */
5941 BLOCK_INPUT;
5942 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5943 f->output_data.x->border_pixel);
5944 UNBLOCK_INPUT;
5945 x_update_cursor (f, 1);
5948 static void
5949 frame_unhighlight (f)
5950 struct frame *f;
5952 /* We used to only do this if Vx_no_window_manager was non-nil, but
5953 the ICCCM (section 4.1.6) says that the window's border pixmap
5954 and border pixel are window attributes which are "private to the
5955 client", so we can always change it to whatever we want. */
5956 BLOCK_INPUT;
5957 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5958 f->output_data.x->border_tile);
5959 UNBLOCK_INPUT;
5960 x_update_cursor (f, 1);
5963 /* The focus has changed. Update the frames as necessary to reflect
5964 the new situation. Note that we can't change the selected frame
5965 here, because the Lisp code we are interrupting might become confused.
5966 Each event gets marked with the frame in which it occurred, so the
5967 Lisp code can tell when the switch took place by examining the events. */
5969 static void
5970 x_new_focus_frame (dpyinfo, frame)
5971 struct x_display_info *dpyinfo;
5972 struct frame *frame;
5974 struct frame *old_focus = dpyinfo->x_focus_frame;
5976 if (frame != dpyinfo->x_focus_frame)
5978 /* Set this before calling other routines, so that they see
5979 the correct value of x_focus_frame. */
5980 dpyinfo->x_focus_frame = frame;
5982 if (old_focus && old_focus->auto_lower)
5983 x_lower_frame (old_focus);
5985 #if 0
5986 selected_frame = frame;
5987 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5988 selected_frame);
5989 Fselect_window (selected_frame->selected_window);
5990 choose_minibuf_frame ();
5991 #endif /* ! 0 */
5993 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
5994 pending_autoraise_frame = dpyinfo->x_focus_frame;
5995 else
5996 pending_autoraise_frame = 0;
5999 x_frame_rehighlight (dpyinfo);
6002 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6004 void
6005 x_mouse_leave (dpyinfo)
6006 struct x_display_info *dpyinfo;
6008 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6011 /* The focus has changed, or we have redirected a frame's focus to
6012 another frame (this happens when a frame uses a surrogate
6013 mini-buffer frame). Shift the highlight as appropriate.
6015 The FRAME argument doesn't necessarily have anything to do with which
6016 frame is being highlighted or un-highlighted; we only use it to find
6017 the appropriate X display info. */
6019 static void
6020 XTframe_rehighlight (frame)
6021 struct frame *frame;
6023 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6026 static void
6027 x_frame_rehighlight (dpyinfo)
6028 struct x_display_info *dpyinfo;
6030 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6032 if (dpyinfo->x_focus_frame)
6034 dpyinfo->x_highlight_frame
6035 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6036 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6037 : dpyinfo->x_focus_frame);
6038 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6040 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6041 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6044 else
6045 dpyinfo->x_highlight_frame = 0;
6047 if (dpyinfo->x_highlight_frame != old_highlight)
6049 if (old_highlight)
6050 frame_unhighlight (old_highlight);
6051 if (dpyinfo->x_highlight_frame)
6052 frame_highlight (dpyinfo->x_highlight_frame);
6058 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6060 /* Initialize mode_switch_bit and modifier_meaning. */
6061 static void
6062 x_find_modifier_meanings (dpyinfo)
6063 struct x_display_info *dpyinfo;
6065 int min_code, max_code;
6066 KeySym *syms;
6067 int syms_per_code;
6068 XModifierKeymap *mods;
6070 dpyinfo->meta_mod_mask = 0;
6071 dpyinfo->shift_lock_mask = 0;
6072 dpyinfo->alt_mod_mask = 0;
6073 dpyinfo->super_mod_mask = 0;
6074 dpyinfo->hyper_mod_mask = 0;
6076 #ifdef HAVE_X11R4
6077 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6078 #else
6079 min_code = dpyinfo->display->min_keycode;
6080 max_code = dpyinfo->display->max_keycode;
6081 #endif
6083 syms = XGetKeyboardMapping (dpyinfo->display,
6084 min_code, max_code - min_code + 1,
6085 &syms_per_code);
6086 mods = XGetModifierMapping (dpyinfo->display);
6088 /* Scan the modifier table to see which modifier bits the Meta and
6089 Alt keysyms are on. */
6091 int row, col; /* The row and column in the modifier table. */
6093 for (row = 3; row < 8; row++)
6094 for (col = 0; col < mods->max_keypermod; col++)
6096 KeyCode code
6097 = mods->modifiermap[(row * mods->max_keypermod) + col];
6099 /* Zeroes are used for filler. Skip them. */
6100 if (code == 0)
6101 continue;
6103 /* Are any of this keycode's keysyms a meta key? */
6105 int code_col;
6107 for (code_col = 0; code_col < syms_per_code; code_col++)
6109 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6111 switch (sym)
6113 case XK_Meta_L:
6114 case XK_Meta_R:
6115 dpyinfo->meta_mod_mask |= (1 << row);
6116 break;
6118 case XK_Alt_L:
6119 case XK_Alt_R:
6120 dpyinfo->alt_mod_mask |= (1 << row);
6121 break;
6123 case XK_Hyper_L:
6124 case XK_Hyper_R:
6125 dpyinfo->hyper_mod_mask |= (1 << row);
6126 break;
6128 case XK_Super_L:
6129 case XK_Super_R:
6130 dpyinfo->super_mod_mask |= (1 << row);
6131 break;
6133 case XK_Shift_Lock:
6134 /* Ignore this if it's not on the lock modifier. */
6135 if ((1 << row) == LockMask)
6136 dpyinfo->shift_lock_mask = LockMask;
6137 break;
6144 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6145 if (! dpyinfo->meta_mod_mask)
6147 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6148 dpyinfo->alt_mod_mask = 0;
6151 /* If some keys are both alt and meta,
6152 make them just meta, not alt. */
6153 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6155 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6158 XFree ((char *) syms);
6159 XFreeModifiermap (mods);
6162 /* Convert between the modifier bits X uses and the modifier bits
6163 Emacs uses. */
6165 static unsigned int
6166 x_x_to_emacs_modifiers (dpyinfo, state)
6167 struct x_display_info *dpyinfo;
6168 unsigned int state;
6170 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6171 | ((state & ControlMask) ? ctrl_modifier : 0)
6172 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6173 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6174 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6175 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6178 static unsigned int
6179 x_emacs_to_x_modifiers (dpyinfo, state)
6180 struct x_display_info *dpyinfo;
6181 unsigned int state;
6183 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6184 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6185 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6186 | ((state & shift_modifier) ? ShiftMask : 0)
6187 | ((state & ctrl_modifier) ? ControlMask : 0)
6188 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6191 /* Convert a keysym to its name. */
6193 char *
6194 x_get_keysym_name (keysym)
6195 KeySym keysym;
6197 char *value;
6199 BLOCK_INPUT;
6200 value = XKeysymToString (keysym);
6201 UNBLOCK_INPUT;
6203 return value;
6208 /* Mouse clicks and mouse movement. Rah. */
6210 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6211 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6212 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6213 not force the value into range. */
6215 void
6216 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6217 FRAME_PTR f;
6218 register int pix_x, pix_y;
6219 register int *x, *y;
6220 XRectangle *bounds;
6221 int noclip;
6223 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6224 even for negative values. */
6225 if (pix_x < 0)
6226 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6227 if (pix_y < 0)
6228 pix_y -= (f)->output_data.x->line_height - 1;
6230 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6231 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6233 if (bounds)
6235 bounds->width = FONT_WIDTH (f->output_data.x->font);
6236 bounds->height = f->output_data.x->line_height;
6237 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6238 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6241 if (!noclip)
6243 if (pix_x < 0)
6244 pix_x = 0;
6245 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6246 pix_x = FRAME_WINDOW_WIDTH (f);
6248 if (pix_y < 0)
6249 pix_y = 0;
6250 else if (pix_y > f->height)
6251 pix_y = f->height;
6254 *x = pix_x;
6255 *y = pix_y;
6259 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6260 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6261 can't tell the positions because W's display is not up to date,
6262 return 0. */
6265 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6266 struct window *w;
6267 int hpos, vpos;
6268 int *frame_x, *frame_y;
6270 int success_p;
6272 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6273 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6275 if (display_completed)
6277 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6278 struct glyph *glyph = row->glyphs[TEXT_AREA];
6279 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6281 *frame_y = row->y;
6282 *frame_x = row->x;
6283 while (glyph < end)
6285 *frame_x += glyph->pixel_width;
6286 ++glyph;
6289 success_p = 1;
6291 else
6293 *frame_y = *frame_x = 0;
6294 success_p = 0;
6297 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6298 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6299 return success_p;
6303 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6305 If the event is a button press, then note that we have grabbed
6306 the mouse. */
6308 static Lisp_Object
6309 construct_mouse_click (result, event, f)
6310 struct input_event *result;
6311 XButtonEvent *event;
6312 struct frame *f;
6314 /* Make the event type no_event; we'll change that when we decide
6315 otherwise. */
6316 result->kind = mouse_click;
6317 result->code = event->button - Button1;
6318 result->timestamp = event->time;
6319 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6320 event->state)
6321 | (event->type == ButtonRelease
6322 ? up_modifier
6323 : down_modifier));
6325 XSETINT (result->x, event->x);
6326 XSETINT (result->y, event->y);
6327 XSETFRAME (result->frame_or_window, f);
6328 result->arg = Qnil;
6329 return Qnil;
6333 /* Function to report a mouse movement to the mainstream Emacs code.
6334 The input handler calls this.
6336 We have received a mouse movement event, which is given in *event.
6337 If the mouse is over a different glyph than it was last time, tell
6338 the mainstream emacs code by setting mouse_moved. If not, ask for
6339 another motion event, so we can check again the next time it moves. */
6341 static XMotionEvent last_mouse_motion_event;
6342 static Lisp_Object last_mouse_motion_frame;
6344 static void
6345 note_mouse_movement (frame, event)
6346 FRAME_PTR frame;
6347 XMotionEvent *event;
6349 last_mouse_movement_time = event->time;
6350 last_mouse_motion_event = *event;
6351 XSETFRAME (last_mouse_motion_frame, frame);
6353 if (event->window != FRAME_X_WINDOW (frame))
6355 frame->mouse_moved = 1;
6356 last_mouse_scroll_bar = Qnil;
6357 note_mouse_highlight (frame, -1, -1);
6360 /* Has the mouse moved off the glyph it was on at the last sighting? */
6361 else if (event->x < last_mouse_glyph.x
6362 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6363 || event->y < last_mouse_glyph.y
6364 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6366 frame->mouse_moved = 1;
6367 last_mouse_scroll_bar = Qnil;
6368 note_mouse_highlight (frame, event->x, event->y);
6372 /* This is used for debugging, to turn off note_mouse_highlight. */
6374 int disable_mouse_highlight;
6378 /************************************************************************
6379 Mouse Face
6380 ************************************************************************/
6382 /* Find the glyph under window-relative coordinates X/Y in window W.
6383 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6384 strings. Return in *HPOS and *VPOS the row and column number of
6385 the glyph found. Return in *AREA the glyph area containing X.
6386 Value is a pointer to the glyph found or null if X/Y is not on
6387 text, or we can't tell because W's current matrix is not up to
6388 date. */
6390 static struct glyph *
6391 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6392 struct window *w;
6393 int x, y;
6394 int *hpos, *vpos, *area;
6396 struct glyph *glyph, *end;
6397 struct glyph_row *row = NULL;
6398 int x0, i, left_area_width;
6400 /* Find row containing Y. Give up if some row is not enabled. */
6401 for (i = 0; i < w->current_matrix->nrows; ++i)
6403 row = MATRIX_ROW (w->current_matrix, i);
6404 if (!row->enabled_p)
6405 return NULL;
6406 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6407 break;
6410 *vpos = i;
6411 *hpos = 0;
6413 /* Give up if Y is not in the window. */
6414 if (i == w->current_matrix->nrows)
6415 return NULL;
6417 /* Get the glyph area containing X. */
6418 if (w->pseudo_window_p)
6420 *area = TEXT_AREA;
6421 x0 = 0;
6423 else
6425 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6426 if (x < left_area_width)
6428 *area = LEFT_MARGIN_AREA;
6429 x0 = 0;
6431 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6433 *area = TEXT_AREA;
6434 x0 = row->x + left_area_width;
6436 else
6438 *area = RIGHT_MARGIN_AREA;
6439 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6443 /* Find glyph containing X. */
6444 glyph = row->glyphs[*area];
6445 end = glyph + row->used[*area];
6446 while (glyph < end)
6448 if (x < x0 + glyph->pixel_width)
6450 if (w->pseudo_window_p)
6451 break;
6452 else if (BUFFERP (glyph->object))
6453 break;
6456 x0 += glyph->pixel_width;
6457 ++glyph;
6460 if (glyph == end)
6461 return NULL;
6463 *hpos = glyph - row->glyphs[*area];
6464 return glyph;
6468 /* Convert frame-relative x/y to coordinates relative to window W.
6469 Takes pseudo-windows into account. */
6471 static void
6472 frame_to_window_pixel_xy (w, x, y)
6473 struct window *w;
6474 int *x, *y;
6476 if (w->pseudo_window_p)
6478 /* A pseudo-window is always full-width, and starts at the
6479 left edge of the frame, plus a frame border. */
6480 struct frame *f = XFRAME (w->frame);
6481 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6482 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6484 else
6486 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6487 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6492 /* Take proper action when mouse has moved to the mode or header line of
6493 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6494 mode line. X is relative to the start of the text display area of
6495 W, so the width of bitmap areas and scroll bars must be subtracted
6496 to get a position relative to the start of the mode line. */
6498 static void
6499 note_mode_line_highlight (w, x, mode_line_p)
6500 struct window *w;
6501 int x, mode_line_p;
6503 struct frame *f = XFRAME (w->frame);
6504 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6505 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6506 struct glyph_row *row;
6508 if (mode_line_p)
6509 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6510 else
6511 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6513 if (row->enabled_p)
6515 struct glyph *glyph, *end;
6516 Lisp_Object help, map;
6517 int x0;
6519 /* Find the glyph under X. */
6520 glyph = row->glyphs[TEXT_AREA];
6521 end = glyph + row->used[TEXT_AREA];
6522 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6523 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6525 while (glyph < end
6526 && x >= x0 + glyph->pixel_width)
6528 x0 += glyph->pixel_width;
6529 ++glyph;
6532 if (glyph < end
6533 && STRINGP (glyph->object)
6534 && XSTRING (glyph->object)->intervals
6535 && glyph->charpos >= 0
6536 && glyph->charpos < XSTRING (glyph->object)->size)
6538 /* If we're on a string with `help-echo' text property,
6539 arrange for the help to be displayed. This is done by
6540 setting the global variable help_echo to the help string. */
6541 help = Fget_text_property (make_number (glyph->charpos),
6542 Qhelp_echo, glyph->object);
6543 if (!NILP (help))
6545 help_echo = help;
6546 XSETWINDOW (help_echo_window, w);
6547 help_echo_object = glyph->object;
6548 help_echo_pos = glyph->charpos;
6551 /* Change the mouse pointer according to what is under X/Y. */
6552 map = Fget_text_property (make_number (glyph->charpos),
6553 Qlocal_map, glyph->object);
6554 if (KEYMAPP (map))
6555 cursor = f->output_data.x->nontext_cursor;
6556 else
6558 map = Fget_text_property (make_number (glyph->charpos),
6559 Qkeymap, glyph->object);
6560 if (KEYMAPP (map))
6561 cursor = f->output_data.x->nontext_cursor;
6566 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6570 /* Take proper action when the mouse has moved to position X, Y on
6571 frame F as regards highlighting characters that have mouse-face
6572 properties. Also de-highlighting chars where the mouse was before.
6573 X and Y can be negative or out of range. */
6575 static void
6576 note_mouse_highlight (f, x, y)
6577 struct frame *f;
6578 int x, y;
6580 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6581 int portion;
6582 Lisp_Object window;
6583 struct window *w;
6585 /* When a menu is active, don't highlight because this looks odd. */
6586 #ifdef USE_X_TOOLKIT
6587 if (popup_activated ())
6588 return;
6589 #endif
6591 if (disable_mouse_highlight
6592 || !f->glyphs_initialized_p)
6593 return;
6595 dpyinfo->mouse_face_mouse_x = x;
6596 dpyinfo->mouse_face_mouse_y = y;
6597 dpyinfo->mouse_face_mouse_frame = f;
6599 if (dpyinfo->mouse_face_defer)
6600 return;
6602 if (gc_in_progress)
6604 dpyinfo->mouse_face_deferred_gc = 1;
6605 return;
6608 /* Which window is that in? */
6609 window = window_from_coordinates (f, x, y, &portion, 1);
6611 /* If we were displaying active text in another window, clear that. */
6612 if (! EQ (window, dpyinfo->mouse_face_window))
6613 clear_mouse_face (dpyinfo);
6615 /* Not on a window -> return. */
6616 if (!WINDOWP (window))
6617 return;
6619 /* Convert to window-relative pixel coordinates. */
6620 w = XWINDOW (window);
6621 frame_to_window_pixel_xy (w, &x, &y);
6623 /* Handle tool-bar window differently since it doesn't display a
6624 buffer. */
6625 if (EQ (window, f->tool_bar_window))
6627 note_tool_bar_highlight (f, x, y);
6628 return;
6631 if (portion == 1 || portion == 3)
6633 /* Mouse is on the mode or top line. */
6634 note_mode_line_highlight (w, x, portion == 1);
6635 return;
6637 else if (portion == 2)
6638 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6639 f->output_data.x->horizontal_drag_cursor);
6640 else
6641 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6642 f->output_data.x->text_cursor);
6644 /* Are we in a window whose display is up to date?
6645 And verify the buffer's text has not changed. */
6646 if (/* Within text portion of the window. */
6647 portion == 0
6648 && EQ (w->window_end_valid, w->buffer)
6649 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6650 && (XFASTINT (w->last_overlay_modified)
6651 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6653 int hpos, vpos, pos, i, area;
6654 struct glyph *glyph;
6656 /* Find the glyph under X/Y. */
6657 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6659 /* Clear mouse face if X/Y not over text. */
6660 if (glyph == NULL
6661 || area != TEXT_AREA
6662 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6664 clear_mouse_face (dpyinfo);
6665 return;
6668 pos = glyph->charpos;
6669 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6671 /* Check for mouse-face and help-echo. */
6673 Lisp_Object mouse_face, overlay, position;
6674 Lisp_Object *overlay_vec;
6675 int len, noverlays;
6676 struct buffer *obuf;
6677 int obegv, ozv;
6679 /* If we get an out-of-range value, return now; avoid an error. */
6680 if (pos > BUF_Z (XBUFFER (w->buffer)))
6681 return;
6683 /* Make the window's buffer temporarily current for
6684 overlays_at and compute_char_face. */
6685 obuf = current_buffer;
6686 current_buffer = XBUFFER (w->buffer);
6687 obegv = BEGV;
6688 ozv = ZV;
6689 BEGV = BEG;
6690 ZV = Z;
6692 /* Is this char mouse-active or does it have help-echo? */
6693 XSETINT (position, pos);
6695 /* Put all the overlays we want in a vector in overlay_vec.
6696 Store the length in len. If there are more than 10, make
6697 enough space for all, and try again. */
6698 len = 10;
6699 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6700 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6701 if (noverlays > len)
6703 len = noverlays;
6704 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6705 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6708 /* Sort overlays into increasing priority order. */
6709 noverlays = sort_overlays (overlay_vec, noverlays, w);
6711 /* Check mouse-face highlighting. */
6712 if (! (EQ (window, dpyinfo->mouse_face_window)
6713 && vpos >= dpyinfo->mouse_face_beg_row
6714 && vpos <= dpyinfo->mouse_face_end_row
6715 && (vpos > dpyinfo->mouse_face_beg_row
6716 || hpos >= dpyinfo->mouse_face_beg_col)
6717 && (vpos < dpyinfo->mouse_face_end_row
6718 || hpos < dpyinfo->mouse_face_end_col
6719 || dpyinfo->mouse_face_past_end)))
6721 /* Clear the display of the old active region, if any. */
6722 clear_mouse_face (dpyinfo);
6724 /* Find the highest priority overlay that has a mouse-face prop. */
6725 overlay = Qnil;
6726 for (i = noverlays - 1; i >= 0; --i)
6728 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6729 if (!NILP (mouse_face))
6731 overlay = overlay_vec[i];
6732 break;
6736 /* If no overlay applies, get a text property. */
6737 if (NILP (overlay))
6738 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6740 /* Handle the overlay case. */
6741 if (! NILP (overlay))
6743 /* Find the range of text around this char that
6744 should be active. */
6745 Lisp_Object before, after;
6746 int ignore;
6748 before = Foverlay_start (overlay);
6749 after = Foverlay_end (overlay);
6750 /* Record this as the current active region. */
6751 fast_find_position (w, XFASTINT (before),
6752 &dpyinfo->mouse_face_beg_col,
6753 &dpyinfo->mouse_face_beg_row,
6754 &dpyinfo->mouse_face_beg_x,
6755 &dpyinfo->mouse_face_beg_y);
6756 dpyinfo->mouse_face_past_end
6757 = !fast_find_position (w, XFASTINT (after),
6758 &dpyinfo->mouse_face_end_col,
6759 &dpyinfo->mouse_face_end_row,
6760 &dpyinfo->mouse_face_end_x,
6761 &dpyinfo->mouse_face_end_y);
6762 dpyinfo->mouse_face_window = window;
6763 dpyinfo->mouse_face_face_id
6764 = face_at_buffer_position (w, pos, 0, 0,
6765 &ignore, pos + 1, 1);
6767 /* Display it as active. */
6768 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6770 /* Handle the text property case. */
6771 else if (! NILP (mouse_face))
6773 /* Find the range of text around this char that
6774 should be active. */
6775 Lisp_Object before, after, beginning, end;
6776 int ignore;
6778 beginning = Fmarker_position (w->start);
6779 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6780 - XFASTINT (w->window_end_pos)));
6781 before
6782 = Fprevious_single_property_change (make_number (pos + 1),
6783 Qmouse_face,
6784 w->buffer, beginning);
6785 after
6786 = Fnext_single_property_change (position, Qmouse_face,
6787 w->buffer, end);
6788 /* Record this as the current active region. */
6789 fast_find_position (w, XFASTINT (before),
6790 &dpyinfo->mouse_face_beg_col,
6791 &dpyinfo->mouse_face_beg_row,
6792 &dpyinfo->mouse_face_beg_x,
6793 &dpyinfo->mouse_face_beg_y);
6794 dpyinfo->mouse_face_past_end
6795 = !fast_find_position (w, XFASTINT (after),
6796 &dpyinfo->mouse_face_end_col,
6797 &dpyinfo->mouse_face_end_row,
6798 &dpyinfo->mouse_face_end_x,
6799 &dpyinfo->mouse_face_end_y);
6800 dpyinfo->mouse_face_window = window;
6801 dpyinfo->mouse_face_face_id
6802 = face_at_buffer_position (w, pos, 0, 0,
6803 &ignore, pos + 1, 1);
6805 /* Display it as active. */
6806 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6810 /* Look for a `help-echo' property. */
6812 Lisp_Object help, overlay;
6814 /* Check overlays first. */
6815 help = overlay = Qnil;
6816 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6818 overlay = overlay_vec[i];
6819 help = Foverlay_get (overlay, Qhelp_echo);
6822 if (!NILP (help))
6824 help_echo = help;
6825 help_echo_window = window;
6826 help_echo_object = overlay;
6827 help_echo_pos = pos;
6829 else
6831 /* Try text properties. */
6832 if ((STRINGP (glyph->object)
6833 && glyph->charpos >= 0
6834 && glyph->charpos < XSTRING (glyph->object)->size)
6835 || (BUFFERP (glyph->object)
6836 && glyph->charpos >= BEGV
6837 && glyph->charpos < ZV))
6838 help = Fget_text_property (make_number (glyph->charpos),
6839 Qhelp_echo, glyph->object);
6841 if (!NILP (help))
6843 help_echo = help;
6844 help_echo_window = window;
6845 help_echo_object = glyph->object;
6846 help_echo_pos = glyph->charpos;
6851 BEGV = obegv;
6852 ZV = ozv;
6853 current_buffer = obuf;
6858 static void
6859 redo_mouse_highlight ()
6861 if (!NILP (last_mouse_motion_frame)
6862 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6863 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6864 last_mouse_motion_event.x,
6865 last_mouse_motion_event.y);
6870 /***********************************************************************
6871 Tool-bars
6872 ***********************************************************************/
6874 static int x_tool_bar_item P_ ((struct frame *, int, int,
6875 struct glyph **, int *, int *, int *));
6877 /* Tool-bar item index of the item on which a mouse button was pressed
6878 or -1. */
6880 static int last_tool_bar_item;
6883 /* Get information about the tool-bar item at position X/Y on frame F.
6884 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6885 the current matrix of the tool-bar window of F, or NULL if not
6886 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6887 item in F->tool_bar_items. Value is
6889 -1 if X/Y is not on a tool-bar item
6890 0 if X/Y is on the same item that was highlighted before.
6891 1 otherwise. */
6893 static int
6894 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6895 struct frame *f;
6896 int x, y;
6897 struct glyph **glyph;
6898 int *hpos, *vpos, *prop_idx;
6900 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6901 struct window *w = XWINDOW (f->tool_bar_window);
6902 int area;
6904 /* Find the glyph under X/Y. */
6905 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6906 if (*glyph == NULL)
6907 return -1;
6909 /* Get the start of this tool-bar item's properties in
6910 f->tool_bar_items. */
6911 if (!tool_bar_item_info (f, *glyph, prop_idx))
6912 return -1;
6914 /* Is mouse on the highlighted item? */
6915 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6916 && *vpos >= dpyinfo->mouse_face_beg_row
6917 && *vpos <= dpyinfo->mouse_face_end_row
6918 && (*vpos > dpyinfo->mouse_face_beg_row
6919 || *hpos >= dpyinfo->mouse_face_beg_col)
6920 && (*vpos < dpyinfo->mouse_face_end_row
6921 || *hpos < dpyinfo->mouse_face_end_col
6922 || dpyinfo->mouse_face_past_end))
6923 return 0;
6925 return 1;
6929 /* Handle mouse button event on the tool-bar of frame F, at
6930 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6931 or ButtonRelase. */
6933 static void
6934 x_handle_tool_bar_click (f, button_event)
6935 struct frame *f;
6936 XButtonEvent *button_event;
6938 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6939 struct window *w = XWINDOW (f->tool_bar_window);
6940 int hpos, vpos, prop_idx;
6941 struct glyph *glyph;
6942 Lisp_Object enabled_p;
6943 int x = button_event->x;
6944 int y = button_event->y;
6946 /* If not on the highlighted tool-bar item, return. */
6947 frame_to_window_pixel_xy (w, &x, &y);
6948 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6949 return;
6951 /* If item is disabled, do nothing. */
6952 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
6953 if (NILP (enabled_p))
6954 return;
6956 if (button_event->type == ButtonPress)
6958 /* Show item in pressed state. */
6959 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6960 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6961 last_tool_bar_item = prop_idx;
6963 else
6965 Lisp_Object key, frame;
6966 struct input_event event;
6968 /* Show item in released state. */
6969 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6970 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6972 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
6974 XSETFRAME (frame, f);
6975 event.kind = TOOL_BAR_EVENT;
6976 event.frame_or_window = frame;
6977 event.arg = frame;
6978 kbd_buffer_store_event (&event);
6980 event.kind = TOOL_BAR_EVENT;
6981 event.frame_or_window = frame;
6982 event.arg = key;
6983 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6984 button_event->state);
6985 kbd_buffer_store_event (&event);
6986 last_tool_bar_item = -1;
6991 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6992 tool-bar window-relative coordinates X/Y. Called from
6993 note_mouse_highlight. */
6995 static void
6996 note_tool_bar_highlight (f, x, y)
6997 struct frame *f;
6998 int x, y;
7000 Lisp_Object window = f->tool_bar_window;
7001 struct window *w = XWINDOW (window);
7002 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7003 int hpos, vpos;
7004 struct glyph *glyph;
7005 struct glyph_row *row;
7006 int i;
7007 Lisp_Object enabled_p;
7008 int prop_idx;
7009 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7010 int mouse_down_p, rc;
7012 /* Function note_mouse_highlight is called with negative x(y
7013 values when mouse moves outside of the frame. */
7014 if (x <= 0 || y <= 0)
7016 clear_mouse_face (dpyinfo);
7017 return;
7020 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7021 if (rc < 0)
7023 /* Not on tool-bar item. */
7024 clear_mouse_face (dpyinfo);
7025 return;
7027 else if (rc == 0)
7028 /* On same tool-bar item as before. */
7029 goto set_help_echo;
7031 clear_mouse_face (dpyinfo);
7033 /* Mouse is down, but on different tool-bar item? */
7034 mouse_down_p = (dpyinfo->grabbed
7035 && f == last_mouse_frame
7036 && FRAME_LIVE_P (f));
7037 if (mouse_down_p
7038 && last_tool_bar_item != prop_idx)
7039 return;
7041 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7042 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7044 /* If tool-bar item is not enabled, don't highlight it. */
7045 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7046 if (!NILP (enabled_p))
7048 /* Compute the x-position of the glyph. In front and past the
7049 image is a space. We include this is the highlighted area. */
7050 row = MATRIX_ROW (w->current_matrix, vpos);
7051 for (i = x = 0; i < hpos; ++i)
7052 x += row->glyphs[TEXT_AREA][i].pixel_width;
7054 /* Record this as the current active region. */
7055 dpyinfo->mouse_face_beg_col = hpos;
7056 dpyinfo->mouse_face_beg_row = vpos;
7057 dpyinfo->mouse_face_beg_x = x;
7058 dpyinfo->mouse_face_beg_y = row->y;
7059 dpyinfo->mouse_face_past_end = 0;
7061 dpyinfo->mouse_face_end_col = hpos + 1;
7062 dpyinfo->mouse_face_end_row = vpos;
7063 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7064 dpyinfo->mouse_face_end_y = row->y;
7065 dpyinfo->mouse_face_window = window;
7066 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7068 /* Display it as active. */
7069 show_mouse_face (dpyinfo, draw);
7070 dpyinfo->mouse_face_image_state = draw;
7073 set_help_echo:
7075 /* Set help_echo to a help string.to display for this tool-bar item.
7076 XTread_socket does the rest. */
7077 help_echo_object = help_echo_window = Qnil;
7078 help_echo_pos = -1;
7079 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7080 if (NILP (help_echo))
7081 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7086 /* Find the glyph matrix position of buffer position POS in window W.
7087 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7088 current glyphs must be up to date. If POS is above window start
7089 return (0, 0, 0, 0). If POS is after end of W, return end of
7090 last line in W. */
7092 static int
7093 fast_find_position (w, pos, hpos, vpos, x, y)
7094 struct window *w;
7095 int pos;
7096 int *hpos, *vpos, *x, *y;
7098 int i;
7099 int lastcol;
7100 int maybe_next_line_p = 0;
7101 int line_start_position;
7102 int yb = window_text_bottom_y (w);
7103 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7104 struct glyph_row *best_row = row;
7105 int row_vpos = 0, best_row_vpos = 0;
7106 int current_x;
7108 while (row->y < yb)
7110 if (row->used[TEXT_AREA])
7111 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7112 else
7113 line_start_position = 0;
7115 if (line_start_position > pos)
7116 break;
7117 /* If the position sought is the end of the buffer,
7118 don't include the blank lines at the bottom of the window. */
7119 else if (line_start_position == pos
7120 && pos == BUF_ZV (XBUFFER (w->buffer)))
7122 maybe_next_line_p = 1;
7123 break;
7125 else if (line_start_position > 0)
7127 best_row = row;
7128 best_row_vpos = row_vpos;
7131 if (row->y + row->height >= yb)
7132 break;
7134 ++row;
7135 ++row_vpos;
7138 /* Find the right column within BEST_ROW. */
7139 lastcol = 0;
7140 current_x = best_row->x;
7141 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7143 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7144 int charpos;
7146 charpos = glyph->charpos;
7147 if (charpos == pos)
7149 *hpos = i;
7150 *vpos = best_row_vpos;
7151 *x = current_x;
7152 *y = best_row->y;
7153 return 1;
7155 else if (charpos > pos)
7156 break;
7157 else if (charpos > 0)
7158 lastcol = i;
7160 current_x += glyph->pixel_width;
7163 /* If we're looking for the end of the buffer,
7164 and we didn't find it in the line we scanned,
7165 use the start of the following line. */
7166 if (maybe_next_line_p)
7168 ++best_row;
7169 ++best_row_vpos;
7170 lastcol = 0;
7171 current_x = best_row->x;
7174 *vpos = best_row_vpos;
7175 *hpos = lastcol + 1;
7176 *x = current_x;
7177 *y = best_row->y;
7178 return 0;
7182 /* Display the active region described by mouse_face_*
7183 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7185 static void
7186 show_mouse_face (dpyinfo, draw)
7187 struct x_display_info *dpyinfo;
7188 enum draw_glyphs_face draw;
7190 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7191 struct frame *f = XFRAME (WINDOW_FRAME (w));
7192 int i;
7193 int cursor_off_p = 0;
7194 struct cursor_pos saved_cursor;
7196 saved_cursor = output_cursor;
7198 /* If window is in the process of being destroyed, don't bother
7199 to do anything. */
7200 if (w->current_matrix == NULL)
7201 goto set_x_cursor;
7203 /* Recognize when we are called to operate on rows that don't exist
7204 anymore. This can happen when a window is split. */
7205 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7206 goto set_x_cursor;
7208 set_output_cursor (&w->phys_cursor);
7210 /* Note that mouse_face_beg_row etc. are window relative. */
7211 for (i = dpyinfo->mouse_face_beg_row;
7212 i <= dpyinfo->mouse_face_end_row;
7213 i++)
7215 int start_hpos, end_hpos, start_x;
7216 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7218 /* Don't do anything if row doesn't have valid contents. */
7219 if (!row->enabled_p)
7220 continue;
7222 /* For all but the first row, the highlight starts at column 0. */
7223 if (i == dpyinfo->mouse_face_beg_row)
7225 start_hpos = dpyinfo->mouse_face_beg_col;
7226 start_x = dpyinfo->mouse_face_beg_x;
7228 else
7230 start_hpos = 0;
7231 start_x = 0;
7234 if (i == dpyinfo->mouse_face_end_row)
7235 end_hpos = dpyinfo->mouse_face_end_col;
7236 else
7237 end_hpos = row->used[TEXT_AREA];
7239 /* If the cursor's in the text we are about to rewrite, turn the
7240 cursor off. */
7241 if (!w->pseudo_window_p
7242 && i == output_cursor.vpos
7243 && output_cursor.hpos >= start_hpos - 1
7244 && output_cursor.hpos <= end_hpos)
7246 x_update_window_cursor (w, 0);
7247 cursor_off_p = 1;
7250 if (end_hpos > start_hpos)
7252 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7253 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7254 start_hpos, end_hpos, draw, NULL, NULL, 0);
7258 /* If we turned the cursor off, turn it back on. */
7259 if (cursor_off_p)
7260 x_display_cursor (w, 1,
7261 output_cursor.hpos, output_cursor.vpos,
7262 output_cursor.x, output_cursor.y);
7264 output_cursor = saved_cursor;
7266 set_x_cursor:
7268 /* Change the mouse cursor. */
7269 if (draw == DRAW_NORMAL_TEXT)
7270 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7271 f->output_data.x->text_cursor);
7272 else if (draw == DRAW_MOUSE_FACE)
7273 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7274 f->output_data.x->cross_cursor);
7275 else
7276 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7277 f->output_data.x->nontext_cursor);
7280 /* Clear out the mouse-highlighted active region.
7281 Redraw it un-highlighted first. */
7283 void
7284 clear_mouse_face (dpyinfo)
7285 struct x_display_info *dpyinfo;
7287 #if 0 /* This prevents redrawing tool bar items when changing from one
7288 to another while a tooltip is open, so don't do it. */
7289 if (!NILP (tip_frame))
7290 return;
7291 #endif
7293 if (! NILP (dpyinfo->mouse_face_window))
7294 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7296 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7297 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7298 dpyinfo->mouse_face_window = Qnil;
7302 /* Clear any mouse-face on window W. This function is part of the
7303 redisplay interface, and is called from try_window_id and similar
7304 functions to ensure the mouse-highlight is off. */
7306 static void
7307 x_clear_mouse_face (w)
7308 struct window *w;
7310 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7311 Lisp_Object window;
7313 BLOCK_INPUT;
7314 XSETWINDOW (window, w);
7315 if (EQ (window, dpyinfo->mouse_face_window))
7316 clear_mouse_face (dpyinfo);
7317 UNBLOCK_INPUT;
7321 /* Just discard the mouse face information for frame F, if any.
7322 This is used when the size of F is changed. */
7324 void
7325 cancel_mouse_face (f)
7326 FRAME_PTR f;
7328 Lisp_Object window;
7329 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7331 window = dpyinfo->mouse_face_window;
7332 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7334 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7335 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7336 dpyinfo->mouse_face_window = Qnil;
7340 static struct scroll_bar *x_window_to_scroll_bar ();
7341 static void x_scroll_bar_report_motion ();
7343 /* Return the current position of the mouse.
7344 *fp should be a frame which indicates which display to ask about.
7346 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7347 and *part to the frame, window, and scroll bar part that the mouse
7348 is over. Set *x and *y to the portion and whole of the mouse's
7349 position on the scroll bar.
7351 If the mouse movement started elsewhere, set *fp to the frame the
7352 mouse is on, *bar_window to nil, and *x and *y to the character cell
7353 the mouse is over.
7355 Set *time to the server time-stamp for the time at which the mouse
7356 was at this position.
7358 Don't store anything if we don't have a valid set of values to report.
7360 This clears the mouse_moved flag, so we can wait for the next mouse
7361 movement. */
7363 static void
7364 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7365 FRAME_PTR *fp;
7366 int insist;
7367 Lisp_Object *bar_window;
7368 enum scroll_bar_part *part;
7369 Lisp_Object *x, *y;
7370 unsigned long *time;
7372 FRAME_PTR f1;
7374 BLOCK_INPUT;
7376 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7377 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7378 else
7380 Window root;
7381 int root_x, root_y;
7383 Window dummy_window;
7384 int dummy;
7386 Lisp_Object frame, tail;
7388 /* Clear the mouse-moved flag for every frame on this display. */
7389 FOR_EACH_FRAME (tail, frame)
7390 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7391 XFRAME (frame)->mouse_moved = 0;
7393 last_mouse_scroll_bar = Qnil;
7395 /* Figure out which root window we're on. */
7396 XQueryPointer (FRAME_X_DISPLAY (*fp),
7397 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7399 /* The root window which contains the pointer. */
7400 &root,
7402 /* Trash which we can't trust if the pointer is on
7403 a different screen. */
7404 &dummy_window,
7406 /* The position on that root window. */
7407 &root_x, &root_y,
7409 /* More trash we can't trust. */
7410 &dummy, &dummy,
7412 /* Modifier keys and pointer buttons, about which
7413 we don't care. */
7414 (unsigned int *) &dummy);
7416 /* Now we have a position on the root; find the innermost window
7417 containing the pointer. */
7419 Window win, child;
7420 int win_x, win_y;
7421 int parent_x = 0, parent_y = 0;
7422 int count;
7424 win = root;
7426 /* XTranslateCoordinates can get errors if the window
7427 structure is changing at the same time this function
7428 is running. So at least we must not crash from them. */
7430 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7432 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7433 && FRAME_LIVE_P (last_mouse_frame))
7435 /* If mouse was grabbed on a frame, give coords for that frame
7436 even if the mouse is now outside it. */
7437 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7439 /* From-window, to-window. */
7440 root, FRAME_X_WINDOW (last_mouse_frame),
7442 /* From-position, to-position. */
7443 root_x, root_y, &win_x, &win_y,
7445 /* Child of win. */
7446 &child);
7447 f1 = last_mouse_frame;
7449 else
7451 while (1)
7453 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7455 /* From-window, to-window. */
7456 root, win,
7458 /* From-position, to-position. */
7459 root_x, root_y, &win_x, &win_y,
7461 /* Child of win. */
7462 &child);
7464 if (child == None || child == win)
7465 break;
7467 win = child;
7468 parent_x = win_x;
7469 parent_y = win_y;
7472 /* Now we know that:
7473 win is the innermost window containing the pointer
7474 (XTC says it has no child containing the pointer),
7475 win_x and win_y are the pointer's position in it
7476 (XTC did this the last time through), and
7477 parent_x and parent_y are the pointer's position in win's parent.
7478 (They are what win_x and win_y were when win was child.
7479 If win is the root window, it has no parent, and
7480 parent_{x,y} are invalid, but that's okay, because we'll
7481 never use them in that case.) */
7483 /* Is win one of our frames? */
7484 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7486 #ifdef USE_X_TOOLKIT
7487 /* If we end up with the menu bar window, say it's not
7488 on the frame. */
7489 if (f1 != NULL
7490 && f1->output_data.x->menubar_widget
7491 && win == XtWindow (f1->output_data.x->menubar_widget))
7492 f1 = NULL;
7493 #endif /* USE_X_TOOLKIT */
7496 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7497 f1 = 0;
7499 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7501 /* If not, is it one of our scroll bars? */
7502 if (! f1)
7504 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7506 if (bar)
7508 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7509 win_x = parent_x;
7510 win_y = parent_y;
7514 if (f1 == 0 && insist > 0)
7515 f1 = SELECTED_FRAME ();
7517 if (f1)
7519 /* Ok, we found a frame. Store all the values.
7520 last_mouse_glyph is a rectangle used to reduce the
7521 generation of mouse events. To not miss any motion
7522 events, we must divide the frame into rectangles of the
7523 size of the smallest character that could be displayed
7524 on it, i.e. into the same rectangles that matrices on
7525 the frame are divided into. */
7527 #if OLD_REDISPLAY_CODE
7528 int ignore1, ignore2;
7529 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7530 &last_mouse_glyph,
7531 FRAME_X_DISPLAY_INFO (f1)->grabbed
7532 || insist);
7533 #else
7535 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7536 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7537 int x = win_x;
7538 int y = win_y;
7540 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7541 round down even for negative values. */
7542 if (x < 0)
7543 x -= width - 1;
7544 if (y < 0)
7545 y -= height - 1;
7547 last_mouse_glyph.width = width;
7548 last_mouse_glyph.height = height;
7549 last_mouse_glyph.x = (x + width - 1) / width * width;
7550 last_mouse_glyph.y = (y + height - 1) / height * height;
7552 #endif
7554 *bar_window = Qnil;
7555 *part = 0;
7556 *fp = f1;
7557 XSETINT (*x, win_x);
7558 XSETINT (*y, win_y);
7559 *time = last_mouse_movement_time;
7564 UNBLOCK_INPUT;
7568 #ifdef USE_X_TOOLKIT
7570 /* Atimer callback function for TIMER. Called every 0.1s to process
7571 Xt timeouts, if needed. We must avoid calling XtAppPending as
7572 much as possible because that function does an implicit XFlush
7573 that slows us down. */
7575 static void
7576 x_process_timeouts (timer)
7577 struct atimer *timer;
7579 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7581 BLOCK_INPUT;
7582 while (XtAppPending (Xt_app_con) & XtIMTimer)
7583 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7584 UNBLOCK_INPUT;
7588 #endif /* USE_X_TOOLKIT */
7591 /* Scroll bar support. */
7593 /* Given an X window ID, find the struct scroll_bar which manages it.
7594 This can be called in GC, so we have to make sure to strip off mark
7595 bits. */
7597 static struct scroll_bar *
7598 x_window_to_scroll_bar (window_id)
7599 Window window_id;
7601 Lisp_Object tail;
7603 for (tail = Vframe_list;
7604 XGCTYPE (tail) == Lisp_Cons;
7605 tail = XCDR (tail))
7607 Lisp_Object frame, bar, condemned;
7609 frame = XCAR (tail);
7610 /* All elements of Vframe_list should be frames. */
7611 if (! GC_FRAMEP (frame))
7612 abort ();
7614 /* Scan this frame's scroll bar list for a scroll bar with the
7615 right window ID. */
7616 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7617 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7618 /* This trick allows us to search both the ordinary and
7619 condemned scroll bar lists with one loop. */
7620 ! GC_NILP (bar) || (bar = condemned,
7621 condemned = Qnil,
7622 ! GC_NILP (bar));
7623 bar = XSCROLL_BAR (bar)->next)
7624 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7625 return XSCROLL_BAR (bar);
7628 return 0;
7633 /************************************************************************
7634 Toolkit scroll bars
7635 ************************************************************************/
7637 #ifdef USE_TOOLKIT_SCROLL_BARS
7639 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7640 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7641 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7642 struct scroll_bar *));
7643 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7644 int, int, int));
7647 /* Id of action hook installed for scroll bars. */
7649 static XtActionHookId action_hook_id;
7651 /* Lisp window being scrolled. Set when starting to interact with
7652 a toolkit scroll bar, reset to nil when ending the interaction. */
7654 static Lisp_Object window_being_scrolled;
7656 /* Last scroll bar part sent in xm_scroll_callback. */
7658 static int last_scroll_bar_part;
7660 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7661 that movements of 1/20 of the screen size are mapped to up/down. */
7663 static Boolean xaw3d_arrow_scroll;
7665 /* Whether the drag scrolling maintains the mouse at the top of the
7666 thumb. If not, resizing the thumb needs to be done more carefully
7667 to avoid jerkyness. */
7669 static Boolean xaw3d_pick_top;
7672 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7673 bars are used.. The hook is responsible for detecting when
7674 the user ends an interaction with the scroll bar, and generates
7675 a `end-scroll' scroll_bar_click' event if so. */
7677 static void
7678 xt_action_hook (widget, client_data, action_name, event, params,
7679 num_params)
7680 Widget widget;
7681 XtPointer client_data;
7682 String action_name;
7683 XEvent *event;
7684 String *params;
7685 Cardinal *num_params;
7687 int scroll_bar_p;
7688 char *end_action;
7690 #ifdef USE_MOTIF
7691 scroll_bar_p = XmIsScrollBar (widget);
7692 end_action = "Release";
7693 #else /* !USE_MOTIF i.e. use Xaw */
7694 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7695 end_action = "EndScroll";
7696 #endif /* USE_MOTIF */
7698 if (scroll_bar_p
7699 && strcmp (action_name, end_action) == 0
7700 && WINDOWP (window_being_scrolled))
7702 struct window *w;
7704 x_send_scroll_bar_event (window_being_scrolled,
7705 scroll_bar_end_scroll, 0, 0);
7706 w = XWINDOW (window_being_scrolled);
7707 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7708 window_being_scrolled = Qnil;
7709 last_scroll_bar_part = -1;
7711 /* Xt timeouts no longer needed. */
7712 toolkit_scroll_bar_interaction = 0;
7716 /* A vector of windows used for communication between
7717 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7719 static struct window **scroll_bar_windows;
7720 static int scroll_bar_windows_size;
7723 /* Send a client message with message type Xatom_Scrollbar for a
7724 scroll action to the frame of WINDOW. PART is a value identifying
7725 the part of the scroll bar that was clicked on. PORTION is the
7726 amount to scroll of a whole of WHOLE. */
7728 static void
7729 x_send_scroll_bar_event (window, part, portion, whole)
7730 Lisp_Object window;
7731 int part, portion, whole;
7733 XEvent event;
7734 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7735 struct window *w = XWINDOW (window);
7736 struct frame *f = XFRAME (w->frame);
7737 int i;
7739 BLOCK_INPUT;
7741 /* Construct a ClientMessage event to send to the frame. */
7742 ev->type = ClientMessage;
7743 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7744 ev->display = FRAME_X_DISPLAY (f);
7745 ev->window = FRAME_X_WINDOW (f);
7746 ev->format = 32;
7748 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7749 not enough to store a pointer or Lisp_Object on a 64 bit system.
7750 So, store the window in scroll_bar_windows and pass the index
7751 into that array in the event. */
7752 for (i = 0; i < scroll_bar_windows_size; ++i)
7753 if (scroll_bar_windows[i] == NULL)
7754 break;
7756 if (i == scroll_bar_windows_size)
7758 int new_size = max (10, 2 * scroll_bar_windows_size);
7759 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7760 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7762 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7763 nbytes);
7764 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7765 scroll_bar_windows_size = new_size;
7768 scroll_bar_windows[i] = w;
7769 ev->data.l[0] = (long) i;
7770 ev->data.l[1] = (long) part;
7771 ev->data.l[2] = (long) 0;
7772 ev->data.l[3] = (long) portion;
7773 ev->data.l[4] = (long) whole;
7775 /* Make Xt timeouts work while the scroll bar is active. */
7776 toolkit_scroll_bar_interaction = 1;
7778 /* Setting the event mask to zero means that the message will
7779 be sent to the client that created the window, and if that
7780 window no longer exists, no event will be sent. */
7781 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7782 UNBLOCK_INPUT;
7786 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7787 in *IEVENT. */
7789 static void
7790 x_scroll_bar_to_input_event (event, ievent)
7791 XEvent *event;
7792 struct input_event *ievent;
7794 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7795 Lisp_Object window;
7796 struct frame *f;
7797 struct window *w;
7799 w = scroll_bar_windows[ev->data.l[0]];
7800 scroll_bar_windows[ev->data.l[0]] = NULL;
7802 XSETWINDOW (window, w);
7803 f = XFRAME (w->frame);
7805 ievent->kind = scroll_bar_click;
7806 ievent->frame_or_window = window;
7807 ievent->arg = Qnil;
7808 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7809 ievent->part = ev->data.l[1];
7810 ievent->code = ev->data.l[2];
7811 ievent->x = make_number ((int) ev->data.l[3]);
7812 ievent->y = make_number ((int) ev->data.l[4]);
7813 ievent->modifiers = 0;
7817 #ifdef USE_MOTIF
7819 /* Minimum and maximum values used for Motif scroll bars. */
7821 #define XM_SB_MIN 1
7822 #define XM_SB_MAX 10000000
7823 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7826 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7827 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7828 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7830 static void
7831 xm_scroll_callback (widget, client_data, call_data)
7832 Widget widget;
7833 XtPointer client_data, call_data;
7835 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7836 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7837 double percent;
7838 int part = -1, whole = 0, portion = 0;
7840 switch (cs->reason)
7842 case XmCR_DECREMENT:
7843 bar->dragging = Qnil;
7844 part = scroll_bar_up_arrow;
7845 break;
7847 case XmCR_INCREMENT:
7848 bar->dragging = Qnil;
7849 part = scroll_bar_down_arrow;
7850 break;
7852 case XmCR_PAGE_DECREMENT:
7853 bar->dragging = Qnil;
7854 part = scroll_bar_above_handle;
7855 break;
7857 case XmCR_PAGE_INCREMENT:
7858 bar->dragging = Qnil;
7859 part = scroll_bar_below_handle;
7860 break;
7862 case XmCR_TO_TOP:
7863 bar->dragging = Qnil;
7864 part = scroll_bar_to_top;
7865 break;
7867 case XmCR_TO_BOTTOM:
7868 bar->dragging = Qnil;
7869 part = scroll_bar_to_bottom;
7870 break;
7872 case XmCR_DRAG:
7874 int slider_size;
7875 int dragging_down_p = (INTEGERP (bar->dragging)
7876 && XINT (bar->dragging) <= cs->value);
7878 /* Get the slider size. */
7879 BLOCK_INPUT;
7880 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7881 UNBLOCK_INPUT;
7883 /* At the max position of the scroll bar, do a line-wise
7884 movement. Without doing anything, the LessTif scroll bar
7885 calls us with the same cs->value again and again. If we
7886 want to make sure that we can reach the end of the buffer,
7887 we have to do something.
7889 Implementation note: setting bar->dragging always to
7890 cs->value gives a smoother movement at the max position.
7891 Setting it to nil when doing line-wise movement gives
7892 a better slider behavior. */
7894 if (cs->value + slider_size == XM_SB_MAX
7895 || (dragging_down_p
7896 && last_scroll_bar_part == scroll_bar_down_arrow))
7898 part = scroll_bar_down_arrow;
7899 bar->dragging = Qnil;
7901 else
7903 whole = XM_SB_RANGE;
7904 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7905 part = scroll_bar_handle;
7906 bar->dragging = make_number (cs->value);
7909 break;
7911 case XmCR_VALUE_CHANGED:
7912 break;
7915 if (part >= 0)
7917 window_being_scrolled = bar->window;
7918 last_scroll_bar_part = part;
7919 x_send_scroll_bar_event (bar->window, part, portion, whole);
7924 #else /* !USE_MOTIF, i.e. Xaw. */
7927 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7928 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7929 scroll bar struct. CALL_DATA is a pointer to a float saying where
7930 the thumb is. */
7932 static void
7933 xaw_jump_callback (widget, client_data, call_data)
7934 Widget widget;
7935 XtPointer client_data, call_data;
7937 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7938 float top = *(float *) call_data;
7939 float shown;
7940 int whole, portion, height;
7941 int part;
7943 /* Get the size of the thumb, a value between 0 and 1. */
7944 BLOCK_INPUT;
7945 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7946 UNBLOCK_INPUT;
7948 whole = 10000000;
7949 portion = shown < 1 ? top * whole : 0;
7951 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7952 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7953 the bottom, so we force the scrolling whenever we see that we're
7954 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7955 we try to ensure that we always stay two pixels away from the
7956 bottom). */
7957 part = scroll_bar_down_arrow;
7958 else
7959 part = scroll_bar_handle;
7961 window_being_scrolled = bar->window;
7962 bar->dragging = make_number (portion);
7963 last_scroll_bar_part = part;
7964 x_send_scroll_bar_event (bar->window, part, portion, whole);
7968 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7969 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7970 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7971 the scroll bar. CALL_DATA is an integer specifying the action that
7972 has taken place. It's magnitude is in the range 0..height of the
7973 scroll bar. Negative values mean scroll towards buffer start.
7974 Values < height of scroll bar mean line-wise movement. */
7976 static void
7977 xaw_scroll_callback (widget, client_data, call_data)
7978 Widget widget;
7979 XtPointer client_data, call_data;
7981 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7982 int position = (int) call_data;
7983 Dimension height;
7984 int part;
7986 /* Get the height of the scroll bar. */
7987 BLOCK_INPUT;
7988 XtVaGetValues (widget, XtNheight, &height, NULL);
7989 UNBLOCK_INPUT;
7991 if (abs (position) >= height)
7992 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7994 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7995 it maps line-movement to call_data = max(5, height/20). */
7996 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
7997 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
7998 else
7999 part = scroll_bar_move_ratio;
8001 window_being_scrolled = bar->window;
8002 bar->dragging = Qnil;
8003 last_scroll_bar_part = part;
8004 x_send_scroll_bar_event (bar->window, part, position, height);
8008 #endif /* not USE_MOTIF */
8011 /* Create the widget for scroll bar BAR on frame F. Record the widget
8012 and X window of the scroll bar in BAR. */
8014 static void
8015 x_create_toolkit_scroll_bar (f, bar)
8016 struct frame *f;
8017 struct scroll_bar *bar;
8019 Window xwindow;
8020 Widget widget;
8021 Arg av[20];
8022 int ac = 0;
8023 char *scroll_bar_name = "verticalScrollBar";
8024 unsigned long pixel;
8026 BLOCK_INPUT;
8028 #ifdef USE_MOTIF
8029 /* LessTif 0.85, problems:
8031 1. When the mouse if over the scroll bar, the scroll bar will
8032 get keyboard events. I didn't find a way to turn this off.
8034 2. Do we have to explicitly set the cursor to get an arrow
8035 cursor (see below)? */
8037 /* Set resources. Create the widget. */
8038 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8039 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8040 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8041 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8042 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8043 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8044 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8046 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8047 if (pixel != -1)
8049 XtSetArg (av[ac], XmNforeground, pixel);
8050 ++ac;
8053 pixel = f->output_data.x->scroll_bar_background_pixel;
8054 if (pixel != -1)
8056 XtSetArg (av[ac], XmNbackground, pixel);
8057 ++ac;
8060 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8061 scroll_bar_name, av, ac);
8063 /* Add one callback for everything that can happen. */
8064 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8065 (XtPointer) bar);
8066 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8067 (XtPointer) bar);
8068 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8069 (XtPointer) bar);
8070 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8071 (XtPointer) bar);
8072 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8073 (XtPointer) bar);
8074 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8075 (XtPointer) bar);
8076 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8077 (XtPointer) bar);
8079 /* Realize the widget. Only after that is the X window created. */
8080 XtRealizeWidget (widget);
8082 /* Set the cursor to an arrow. I didn't find a resource to do that.
8083 And I'm wondering why it hasn't an arrow cursor by default. */
8084 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8085 f->output_data.x->nontext_cursor);
8087 #else /* !USE_MOTIF i.e. use Xaw */
8089 /* Set resources. Create the widget. The background of the
8090 Xaw3d scroll bar widget is a little bit light for my taste.
8091 We don't alter it here to let users change it according
8092 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8093 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8094 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8095 /* For smoother scrolling with Xaw3d -sm */
8096 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8097 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8099 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8100 if (pixel != -1)
8102 XtSetArg (av[ac], XtNforeground, pixel);
8103 ++ac;
8106 pixel = f->output_data.x->scroll_bar_background_pixel;
8107 if (pixel != -1)
8109 XtSetArg (av[ac], XtNbackground, pixel);
8110 ++ac;
8113 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8114 f->output_data.x->edit_widget, av, ac);
8117 char *initial = "";
8118 char *val = initial;
8119 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8120 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8121 if (val == initial)
8122 { /* ARROW_SCROLL */
8123 xaw3d_arrow_scroll = True;
8124 /* Isn't that just a personal preference ? -sm */
8125 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8129 /* Define callbacks. */
8130 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8131 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8132 (XtPointer) bar);
8134 /* Realize the widget. Only after that is the X window created. */
8135 XtRealizeWidget (widget);
8137 #endif /* !USE_MOTIF */
8139 /* Install an action hook that let's us detect when the user
8140 finishes interacting with a scroll bar. */
8141 if (action_hook_id == 0)
8142 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8144 /* Remember X window and widget in the scroll bar vector. */
8145 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8146 xwindow = XtWindow (widget);
8147 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8149 UNBLOCK_INPUT;
8153 /* Set the thumb size and position of scroll bar BAR. We are currently
8154 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8156 static void
8157 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8158 struct scroll_bar *bar;
8159 int portion, position, whole;
8161 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8162 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8163 float top, shown;
8165 if (whole == 0)
8166 top = 0, shown = 1;
8167 else
8169 top = (float) position / whole;
8170 shown = (float) portion / whole;
8173 BLOCK_INPUT;
8175 #ifdef USE_MOTIF
8177 int size, value;
8178 Boolean arrow1_selected, arrow2_selected;
8179 unsigned char flags;
8180 XmScrollBarWidget sb;
8182 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8183 is the scroll bar's maximum and MIN is the scroll bar's minimum
8184 value. */
8185 size = shown * XM_SB_RANGE;
8186 size = min (size, XM_SB_RANGE);
8187 size = max (size, 1);
8189 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8190 value = top * XM_SB_RANGE;
8191 value = min (value, XM_SB_MAX - size);
8192 value = max (value, XM_SB_MIN);
8194 /* LessTif: Calling XmScrollBarSetValues after an increment or
8195 decrement turns off auto-repeat LessTif-internally. This can
8196 be seen in ScrollBar.c which resets Arrow1Selected and
8197 Arrow2Selected. It also sets internal flags so that LessTif
8198 believes the mouse is in the slider. We either have to change
8199 our code, or work around that by accessing private data. */
8201 sb = (XmScrollBarWidget) widget;
8202 arrow1_selected = sb->scrollBar.arrow1_selected;
8203 arrow2_selected = sb->scrollBar.arrow2_selected;
8204 flags = sb->scrollBar.flags;
8206 if (NILP (bar->dragging))
8207 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8208 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8209 /* This has the negative side effect that the slider value is
8210 not what it would be if we scrolled here using line-wise or
8211 page-wise movement. */
8212 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8213 else
8215 /* If currently dragging, only update the slider size.
8216 This reduces flicker effects. */
8217 int old_value, old_size, increment, page_increment;
8219 XmScrollBarGetValues (widget, &old_value, &old_size,
8220 &increment, &page_increment);
8221 XmScrollBarSetValues (widget, old_value,
8222 min (size, XM_SB_RANGE - old_value),
8223 0, 0, False);
8226 sb->scrollBar.arrow1_selected = arrow1_selected;
8227 sb->scrollBar.arrow2_selected = arrow2_selected;
8228 sb->scrollBar.flags = flags;
8230 #else /* !USE_MOTIF i.e. use Xaw */
8232 float old_top, old_shown;
8233 Dimension height;
8234 XtVaGetValues (widget,
8235 XtNtopOfThumb, &old_top,
8236 XtNshown, &old_shown,
8237 XtNheight, &height,
8238 NULL);
8240 /* Massage the top+shown values. */
8241 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8242 top = max (0, min (1, top));
8243 else
8244 top = old_top;
8245 /* Keep two pixels available for moving the thumb down. */
8246 shown = max (0, min (1 - top - (2.0 / height), shown));
8248 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8249 check that your system's configuration file contains a define
8250 for `NARROWPROTO'. See s/freebsd.h for an example. */
8251 if (top != old_top || shown != old_shown)
8253 if (NILP (bar->dragging))
8254 XawScrollbarSetThumb (widget, top, shown);
8255 else
8257 #ifdef HAVE_XAW3D
8258 ScrollbarWidget sb = (ScrollbarWidget) widget;
8259 int scroll_mode = 0;
8261 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8262 if (xaw3d_arrow_scroll)
8264 /* Xaw3d stupidly ignores resize requests while dragging
8265 so we have to make it believe it's not in dragging mode. */
8266 scroll_mode = sb->scrollbar.scroll_mode;
8267 if (scroll_mode == 2)
8268 sb->scrollbar.scroll_mode = 0;
8270 #endif
8271 /* Try to make the scrolling a tad smoother. */
8272 if (!xaw3d_pick_top)
8273 shown = min (shown, old_shown);
8275 XawScrollbarSetThumb (widget, top, shown);
8277 #ifdef HAVE_XAW3D
8278 if (xaw3d_arrow_scroll && scroll_mode == 2)
8279 sb->scrollbar.scroll_mode = scroll_mode;
8280 #endif
8284 #endif /* !USE_MOTIF */
8286 UNBLOCK_INPUT;
8289 #endif /* USE_TOOLKIT_SCROLL_BARS */
8293 /************************************************************************
8294 Scroll bars, general
8295 ************************************************************************/
8297 /* Create a scroll bar and return the scroll bar vector for it. W is
8298 the Emacs window on which to create the scroll bar. TOP, LEFT,
8299 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8300 scroll bar. */
8302 static struct scroll_bar *
8303 x_scroll_bar_create (w, top, left, width, height)
8304 struct window *w;
8305 int top, left, width, height;
8307 struct frame *f = XFRAME (w->frame);
8308 struct scroll_bar *bar
8309 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8311 BLOCK_INPUT;
8313 #ifdef USE_TOOLKIT_SCROLL_BARS
8314 x_create_toolkit_scroll_bar (f, bar);
8315 #else /* not USE_TOOLKIT_SCROLL_BARS */
8317 XSetWindowAttributes a;
8318 unsigned long mask;
8319 Window window;
8321 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8322 if (a.background_pixel == -1)
8323 a.background_pixel = f->output_data.x->background_pixel;
8325 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8326 | ButtonMotionMask | PointerMotionHintMask
8327 | ExposureMask);
8328 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8330 mask = (CWBackPixel | CWEventMask | CWCursor);
8332 /* Clear the area of W that will serve as a scroll bar. This is
8333 for the case that a window has been split horizontally. In
8334 this case, no clear_frame is generated to reduce flickering. */
8335 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8336 left, top, width,
8337 window_box_height (w), False);
8339 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8340 /* Position and size of scroll bar. */
8341 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8342 top,
8343 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8344 height,
8345 /* Border width, depth, class, and visual. */
8347 CopyFromParent,
8348 CopyFromParent,
8349 CopyFromParent,
8350 /* Attributes. */
8351 mask, &a);
8352 SET_SCROLL_BAR_X_WINDOW (bar, window);
8354 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8356 XSETWINDOW (bar->window, w);
8357 XSETINT (bar->top, top);
8358 XSETINT (bar->left, left);
8359 XSETINT (bar->width, width);
8360 XSETINT (bar->height, height);
8361 XSETINT (bar->start, 0);
8362 XSETINT (bar->end, 0);
8363 bar->dragging = Qnil;
8365 /* Add bar to its frame's list of scroll bars. */
8366 bar->next = FRAME_SCROLL_BARS (f);
8367 bar->prev = Qnil;
8368 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8369 if (!NILP (bar->next))
8370 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8372 /* Map the window/widget. */
8373 #ifdef USE_TOOLKIT_SCROLL_BARS
8375 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8376 XtConfigureWidget (scroll_bar,
8377 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8378 top,
8379 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8380 height, 0);
8381 XtMapWidget (scroll_bar);
8383 #else /* not USE_TOOLKIT_SCROLL_BARS */
8384 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8385 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8387 UNBLOCK_INPUT;
8388 return bar;
8392 /* Draw BAR's handle in the proper position.
8394 If the handle is already drawn from START to END, don't bother
8395 redrawing it, unless REBUILD is non-zero; in that case, always
8396 redraw it. (REBUILD is handy for drawing the handle after expose
8397 events.)
8399 Normally, we want to constrain the start and end of the handle to
8400 fit inside its rectangle, but if the user is dragging the scroll
8401 bar handle, we want to let them drag it down all the way, so that
8402 the bar's top is as far down as it goes; otherwise, there's no way
8403 to move to the very end of the buffer. */
8405 #ifndef USE_TOOLKIT_SCROLL_BARS
8407 static void
8408 x_scroll_bar_set_handle (bar, start, end, rebuild)
8409 struct scroll_bar *bar;
8410 int start, end;
8411 int rebuild;
8413 int dragging = ! NILP (bar->dragging);
8414 Window w = SCROLL_BAR_X_WINDOW (bar);
8415 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8416 GC gc = f->output_data.x->normal_gc;
8418 /* If the display is already accurate, do nothing. */
8419 if (! rebuild
8420 && start == XINT (bar->start)
8421 && end == XINT (bar->end))
8422 return;
8424 BLOCK_INPUT;
8427 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8428 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8429 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8431 /* Make sure the values are reasonable, and try to preserve
8432 the distance between start and end. */
8434 int length = end - start;
8436 if (start < 0)
8437 start = 0;
8438 else if (start > top_range)
8439 start = top_range;
8440 end = start + length;
8442 if (end < start)
8443 end = start;
8444 else if (end > top_range && ! dragging)
8445 end = top_range;
8448 /* Store the adjusted setting in the scroll bar. */
8449 XSETINT (bar->start, start);
8450 XSETINT (bar->end, end);
8452 /* Clip the end position, just for display. */
8453 if (end > top_range)
8454 end = top_range;
8456 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8457 below top positions, to make sure the handle is always at least
8458 that many pixels tall. */
8459 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8461 /* Draw the empty space above the handle. Note that we can't clear
8462 zero-height areas; that means "clear to end of window." */
8463 if (0 < start)
8464 XClearArea (FRAME_X_DISPLAY (f), w,
8466 /* x, y, width, height, and exposures. */
8467 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8468 VERTICAL_SCROLL_BAR_TOP_BORDER,
8469 inside_width, start,
8470 False);
8472 /* Change to proper foreground color if one is specified. */
8473 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8474 XSetForeground (FRAME_X_DISPLAY (f), gc,
8475 f->output_data.x->scroll_bar_foreground_pixel);
8477 /* Draw the handle itself. */
8478 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8480 /* x, y, width, height */
8481 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8482 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8483 inside_width, end - start);
8485 /* Restore the foreground color of the GC if we changed it above. */
8486 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8487 XSetForeground (FRAME_X_DISPLAY (f), gc,
8488 f->output_data.x->foreground_pixel);
8490 /* Draw the empty space below the handle. Note that we can't
8491 clear zero-height areas; that means "clear to end of window." */
8492 if (end < inside_height)
8493 XClearArea (FRAME_X_DISPLAY (f), w,
8495 /* x, y, width, height, and exposures. */
8496 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8497 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8498 inside_width, inside_height - end,
8499 False);
8503 UNBLOCK_INPUT;
8506 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8508 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8509 nil. */
8511 static void
8512 x_scroll_bar_remove (bar)
8513 struct scroll_bar *bar;
8515 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8516 BLOCK_INPUT;
8518 #ifdef USE_TOOLKIT_SCROLL_BARS
8519 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8520 #else
8521 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8522 #endif
8524 /* Disassociate this scroll bar from its window. */
8525 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8527 UNBLOCK_INPUT;
8531 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8532 that we are displaying PORTION characters out of a total of WHOLE
8533 characters, starting at POSITION. If WINDOW has no scroll bar,
8534 create one. */
8536 static void
8537 XTset_vertical_scroll_bar (w, portion, whole, position)
8538 struct window *w;
8539 int portion, whole, position;
8541 struct frame *f = XFRAME (w->frame);
8542 struct scroll_bar *bar;
8543 int top, height, left, sb_left, width, sb_width;
8544 int window_x, window_y, window_width, window_height;
8546 /* Get window dimensions. */
8547 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8548 top = window_y;
8549 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8550 height = window_height;
8552 /* Compute the left edge of the scroll bar area. */
8553 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8554 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8555 else
8556 left = XFASTINT (w->left);
8557 left *= CANON_X_UNIT (f);
8558 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8560 /* Compute the width of the scroll bar which might be less than
8561 the width of the area reserved for the scroll bar. */
8562 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8563 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8564 else
8565 sb_width = width;
8567 /* Compute the left edge of the scroll bar. */
8568 #ifdef USE_TOOLKIT_SCROLL_BARS
8569 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8570 sb_left = left + width - sb_width - (width - sb_width) / 2;
8571 else
8572 sb_left = left + (width - sb_width) / 2;
8573 #else
8574 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8575 sb_left = left + width - sb_width;
8576 else
8577 sb_left = left;
8578 #endif
8580 /* Does the scroll bar exist yet? */
8581 if (NILP (w->vertical_scroll_bar))
8583 BLOCK_INPUT;
8584 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8585 left, top, width, height, False);
8586 UNBLOCK_INPUT;
8587 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8589 else
8591 /* It may just need to be moved and resized. */
8592 unsigned int mask = 0;
8594 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8596 BLOCK_INPUT;
8598 if (sb_left != XINT (bar->left))
8599 mask |= CWX;
8600 if (top != XINT (bar->top))
8601 mask |= CWY;
8602 if (sb_width != XINT (bar->width))
8603 mask |= CWWidth;
8604 if (height != XINT (bar->height))
8605 mask |= CWHeight;
8607 #ifdef USE_TOOLKIT_SCROLL_BARS
8609 /* Since toolkit scroll bars are smaller than the space reserved
8610 for them on the frame, we have to clear "under" them. */
8611 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8612 left, top, width, height, False);
8614 /* Move/size the scroll bar widget. */
8615 if (mask)
8616 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
8617 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8618 top,
8619 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8620 height, 0);
8622 #else /* not USE_TOOLKIT_SCROLL_BARS */
8624 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8626 /* Clear areas not covered by the scroll bar. This makes sure a
8627 previous mode line display is cleared after C-x 2 C-x 1, for
8628 example. Non-toolkit scroll bars are as wide as the area
8629 reserved for scroll bars - trim at both sides. */
8630 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8631 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8632 height, False);
8633 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8634 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8635 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8636 height, False);
8639 /* Move/size the scroll bar window. */
8640 if (mask)
8642 XWindowChanges wc;
8644 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8645 wc.y = top;
8646 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8647 wc.height = height;
8648 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8649 mask, &wc);
8652 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8654 /* Remember new settings. */
8655 XSETINT (bar->left, sb_left);
8656 XSETINT (bar->top, top);
8657 XSETINT (bar->width, sb_width);
8658 XSETINT (bar->height, height);
8660 UNBLOCK_INPUT;
8663 #ifdef USE_TOOLKIT_SCROLL_BARS
8664 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8665 #else /* not USE_TOOLKIT_SCROLL_BARS */
8666 /* Set the scroll bar's current state, unless we're currently being
8667 dragged. */
8668 if (NILP (bar->dragging))
8670 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8672 if (whole == 0)
8673 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8674 else
8676 int start = ((double) position * top_range) / whole;
8677 int end = ((double) (position + portion) * top_range) / whole;
8678 x_scroll_bar_set_handle (bar, start, end, 0);
8681 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8683 XSETVECTOR (w->vertical_scroll_bar, bar);
8687 /* The following three hooks are used when we're doing a thorough
8688 redisplay of the frame. We don't explicitly know which scroll bars
8689 are going to be deleted, because keeping track of when windows go
8690 away is a real pain - "Can you say set-window-configuration, boys
8691 and girls?" Instead, we just assert at the beginning of redisplay
8692 that *all* scroll bars are to be removed, and then save a scroll bar
8693 from the fiery pit when we actually redisplay its window. */
8695 /* Arrange for all scroll bars on FRAME to be removed at the next call
8696 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8697 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8699 static void
8700 XTcondemn_scroll_bars (frame)
8701 FRAME_PTR frame;
8703 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8704 while (! NILP (FRAME_SCROLL_BARS (frame)))
8706 Lisp_Object bar;
8707 bar = FRAME_SCROLL_BARS (frame);
8708 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8709 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8710 XSCROLL_BAR (bar)->prev = Qnil;
8711 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8712 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8713 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8718 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8719 Note that WINDOW isn't necessarily condemned at all. */
8721 static void
8722 XTredeem_scroll_bar (window)
8723 struct window *window;
8725 struct scroll_bar *bar;
8726 struct frame *f;
8728 /* We can't redeem this window's scroll bar if it doesn't have one. */
8729 if (NILP (window->vertical_scroll_bar))
8730 abort ();
8732 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8734 /* Unlink it from the condemned list. */
8735 f = XFRAME (WINDOW_FRAME (window));
8736 if (NILP (bar->prev))
8738 /* If the prev pointer is nil, it must be the first in one of
8739 the lists. */
8740 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8741 /* It's not condemned. Everything's fine. */
8742 return;
8743 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8744 window->vertical_scroll_bar))
8745 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8746 else
8747 /* If its prev pointer is nil, it must be at the front of
8748 one or the other! */
8749 abort ();
8751 else
8752 XSCROLL_BAR (bar->prev)->next = bar->next;
8754 if (! NILP (bar->next))
8755 XSCROLL_BAR (bar->next)->prev = bar->prev;
8757 bar->next = FRAME_SCROLL_BARS (f);
8758 bar->prev = Qnil;
8759 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8760 if (! NILP (bar->next))
8761 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8764 /* Remove all scroll bars on FRAME that haven't been saved since the
8765 last call to `*condemn_scroll_bars_hook'. */
8767 static void
8768 XTjudge_scroll_bars (f)
8769 FRAME_PTR f;
8771 Lisp_Object bar, next;
8773 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8775 /* Clear out the condemned list now so we won't try to process any
8776 more events on the hapless scroll bars. */
8777 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8779 for (; ! NILP (bar); bar = next)
8781 struct scroll_bar *b = XSCROLL_BAR (bar);
8783 x_scroll_bar_remove (b);
8785 next = b->next;
8786 b->next = b->prev = Qnil;
8789 /* Now there should be no references to the condemned scroll bars,
8790 and they should get garbage-collected. */
8794 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8795 is a no-op when using toolkit scroll bars.
8797 This may be called from a signal handler, so we have to ignore GC
8798 mark bits. */
8800 static void
8801 x_scroll_bar_expose (bar, event)
8802 struct scroll_bar *bar;
8803 XEvent *event;
8805 #ifndef USE_TOOLKIT_SCROLL_BARS
8807 Window w = SCROLL_BAR_X_WINDOW (bar);
8808 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8809 GC gc = f->output_data.x->normal_gc;
8810 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8812 BLOCK_INPUT;
8814 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8816 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8817 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8819 /* x, y, width, height */
8820 0, 0,
8821 XINT (bar->width) - 1 - width_trim - width_trim,
8822 XINT (bar->height) - 1);
8824 UNBLOCK_INPUT;
8826 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8829 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8830 is set to something other than no_event, it is enqueued.
8832 This may be called from a signal handler, so we have to ignore GC
8833 mark bits. */
8835 #ifndef USE_TOOLKIT_SCROLL_BARS
8837 static void
8838 x_scroll_bar_handle_click (bar, event, emacs_event)
8839 struct scroll_bar *bar;
8840 XEvent *event;
8841 struct input_event *emacs_event;
8843 if (! GC_WINDOWP (bar->window))
8844 abort ();
8846 emacs_event->kind = scroll_bar_click;
8847 emacs_event->code = event->xbutton.button - Button1;
8848 emacs_event->modifiers
8849 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8850 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8851 event->xbutton.state)
8852 | (event->type == ButtonRelease
8853 ? up_modifier
8854 : down_modifier));
8855 emacs_event->frame_or_window = bar->window;
8856 emacs_event->arg = Qnil;
8857 emacs_event->timestamp = event->xbutton.time;
8859 #if 0
8860 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8861 int internal_height
8862 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8863 #endif
8864 int top_range
8865 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8866 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8868 if (y < 0) y = 0;
8869 if (y > top_range) y = top_range;
8871 if (y < XINT (bar->start))
8872 emacs_event->part = scroll_bar_above_handle;
8873 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8874 emacs_event->part = scroll_bar_handle;
8875 else
8876 emacs_event->part = scroll_bar_below_handle;
8878 /* Just because the user has clicked on the handle doesn't mean
8879 they want to drag it. Lisp code needs to be able to decide
8880 whether or not we're dragging. */
8881 #if 0
8882 /* If the user has just clicked on the handle, record where they're
8883 holding it. */
8884 if (event->type == ButtonPress
8885 && emacs_event->part == scroll_bar_handle)
8886 XSETINT (bar->dragging, y - XINT (bar->start));
8887 #endif
8889 /* If the user has released the handle, set it to its final position. */
8890 if (event->type == ButtonRelease
8891 && ! NILP (bar->dragging))
8893 int new_start = y - XINT (bar->dragging);
8894 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8896 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8897 bar->dragging = Qnil;
8900 /* Same deal here as the other #if 0. */
8901 #if 0
8902 /* Clicks on the handle are always reported as occurring at the top of
8903 the handle. */
8904 if (emacs_event->part == scroll_bar_handle)
8905 emacs_event->x = bar->start;
8906 else
8907 XSETINT (emacs_event->x, y);
8908 #else
8909 XSETINT (emacs_event->x, y);
8910 #endif
8912 XSETINT (emacs_event->y, top_range);
8916 /* Handle some mouse motion while someone is dragging the scroll bar.
8918 This may be called from a signal handler, so we have to ignore GC
8919 mark bits. */
8921 static void
8922 x_scroll_bar_note_movement (bar, event)
8923 struct scroll_bar *bar;
8924 XEvent *event;
8926 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8928 last_mouse_movement_time = event->xmotion.time;
8930 f->mouse_moved = 1;
8931 XSETVECTOR (last_mouse_scroll_bar, bar);
8933 /* If we're dragging the bar, display it. */
8934 if (! GC_NILP (bar->dragging))
8936 /* Where should the handle be now? */
8937 int new_start = event->xmotion.y - XINT (bar->dragging);
8939 if (new_start != XINT (bar->start))
8941 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8943 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8948 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8950 /* Return information to the user about the current position of the mouse
8951 on the scroll bar. */
8953 static void
8954 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8955 FRAME_PTR *fp;
8956 Lisp_Object *bar_window;
8957 enum scroll_bar_part *part;
8958 Lisp_Object *x, *y;
8959 unsigned long *time;
8961 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8962 Window w = SCROLL_BAR_X_WINDOW (bar);
8963 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8964 int win_x, win_y;
8965 Window dummy_window;
8966 int dummy_coord;
8967 unsigned int dummy_mask;
8969 BLOCK_INPUT;
8971 /* Get the mouse's position relative to the scroll bar window, and
8972 report that. */
8973 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8975 /* Root, child, root x and root y. */
8976 &dummy_window, &dummy_window,
8977 &dummy_coord, &dummy_coord,
8979 /* Position relative to scroll bar. */
8980 &win_x, &win_y,
8982 /* Mouse buttons and modifier keys. */
8983 &dummy_mask))
8985 else
8987 #if 0
8988 int inside_height
8989 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8990 #endif
8991 int top_range
8992 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8994 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8996 if (! NILP (bar->dragging))
8997 win_y -= XINT (bar->dragging);
8999 if (win_y < 0)
9000 win_y = 0;
9001 if (win_y > top_range)
9002 win_y = top_range;
9004 *fp = f;
9005 *bar_window = bar->window;
9007 if (! NILP (bar->dragging))
9008 *part = scroll_bar_handle;
9009 else if (win_y < XINT (bar->start))
9010 *part = scroll_bar_above_handle;
9011 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9012 *part = scroll_bar_handle;
9013 else
9014 *part = scroll_bar_below_handle;
9016 XSETINT (*x, win_y);
9017 XSETINT (*y, top_range);
9019 f->mouse_moved = 0;
9020 last_mouse_scroll_bar = Qnil;
9023 *time = last_mouse_movement_time;
9025 UNBLOCK_INPUT;
9029 /* The screen has been cleared so we may have changed foreground or
9030 background colors, and the scroll bars may need to be redrawn.
9031 Clear out the scroll bars, and ask for expose events, so we can
9032 redraw them. */
9034 void
9035 x_scroll_bar_clear (f)
9036 FRAME_PTR f;
9038 #ifndef USE_TOOLKIT_SCROLL_BARS
9039 Lisp_Object bar;
9041 /* We can have scroll bars even if this is 0,
9042 if we just turned off scroll bar mode.
9043 But in that case we should not clear them. */
9044 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9045 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9046 bar = XSCROLL_BAR (bar)->next)
9047 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9048 0, 0, 0, 0, True);
9049 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9052 /* This processes Expose events from the menu-bar specific X event
9053 loop in xmenu.c. This allows to redisplay the frame if necessary
9054 when handling menu-bar or pop-up items. */
9057 process_expose_from_menu (event)
9058 XEvent event;
9060 FRAME_PTR f;
9061 struct x_display_info *dpyinfo;
9062 int frame_exposed_p = 0;
9064 BLOCK_INPUT;
9066 dpyinfo = x_display_info_for_display (event.xexpose.display);
9067 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9068 if (f)
9070 if (f->async_visible == 0)
9072 f->async_visible = 1;
9073 f->async_iconified = 0;
9074 f->output_data.x->has_been_visible = 1;
9075 SET_FRAME_GARBAGED (f);
9077 else
9079 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9080 event.xexpose.x, event.xexpose.y,
9081 event.xexpose.width, event.xexpose.height);
9082 frame_exposed_p = 1;
9085 else
9087 struct scroll_bar *bar
9088 = x_window_to_scroll_bar (event.xexpose.window);
9090 if (bar)
9091 x_scroll_bar_expose (bar, &event);
9094 UNBLOCK_INPUT;
9095 return frame_exposed_p;
9098 /* Define a queue to save up SelectionRequest events for later handling. */
9100 struct selection_event_queue
9102 XEvent event;
9103 struct selection_event_queue *next;
9106 static struct selection_event_queue *queue;
9108 /* Nonzero means queue up certain events--don't process them yet. */
9110 static int x_queue_selection_requests;
9112 /* Queue up an X event *EVENT, to be processed later. */
9114 static void
9115 x_queue_event (f, event)
9116 FRAME_PTR f;
9117 XEvent *event;
9119 struct selection_event_queue *queue_tmp
9120 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9122 if (queue_tmp != NULL)
9124 queue_tmp->event = *event;
9125 queue_tmp->next = queue;
9126 queue = queue_tmp;
9130 /* Take all the queued events and put them back
9131 so that they get processed afresh. */
9133 static void
9134 x_unqueue_events (display)
9135 Display *display;
9137 while (queue != NULL)
9139 struct selection_event_queue *queue_tmp = queue;
9140 XPutBackEvent (display, &queue_tmp->event);
9141 queue = queue_tmp->next;
9142 xfree ((char *)queue_tmp);
9146 /* Start queuing SelectionRequest events. */
9148 void
9149 x_start_queuing_selection_requests (display)
9150 Display *display;
9152 x_queue_selection_requests++;
9155 /* Stop queuing SelectionRequest events. */
9157 void
9158 x_stop_queuing_selection_requests (display)
9159 Display *display;
9161 x_queue_selection_requests--;
9162 x_unqueue_events (display);
9165 /* The main X event-reading loop - XTread_socket. */
9167 /* Time stamp of enter window event. This is only used by XTread_socket,
9168 but we have to put it out here, since static variables within functions
9169 sometimes don't work. */
9171 static Time enter_timestamp;
9173 /* This holds the state XLookupString needs to implement dead keys
9174 and other tricks known as "compose processing". _X Window System_
9175 says that a portable program can't use this, but Stephen Gildea assures
9176 me that letting the compiler initialize it to zeros will work okay.
9178 This must be defined outside of XTread_socket, for the same reasons
9179 given for enter_time stamp, above. */
9181 static XComposeStatus compose_status;
9183 /* Record the last 100 characters stored
9184 to help debug the loss-of-chars-during-GC problem. */
9186 static int temp_index;
9187 static short temp_buffer[100];
9189 /* Set this to nonzero to fake an "X I/O error"
9190 on a particular display. */
9192 struct x_display_info *XTread_socket_fake_io_error;
9194 /* When we find no input here, we occasionally do a no-op command
9195 to verify that the X server is still running and we can still talk with it.
9196 We try all the open displays, one by one.
9197 This variable is used for cycling thru the displays. */
9199 static struct x_display_info *next_noop_dpyinfo;
9201 #define SET_SAVED_MENU_EVENT(size) \
9202 do \
9204 if (f->output_data.x->saved_menu_event == 0) \
9205 f->output_data.x->saved_menu_event \
9206 = (XEvent *) xmalloc (sizeof (XEvent)); \
9207 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9208 if (numchars >= 1) \
9210 bufp->kind = menu_bar_activate_event; \
9211 XSETFRAME (bufp->frame_or_window, f); \
9212 bufp->arg = Qnil; \
9213 bufp++; \
9214 count++; \
9215 numchars--; \
9218 while (0)
9220 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9221 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9223 /* Read events coming from the X server.
9224 This routine is called by the SIGIO handler.
9225 We return as soon as there are no more events to be read.
9227 Events representing keys are stored in buffer BUFP,
9228 which can hold up to NUMCHARS characters.
9229 We return the number of characters stored into the buffer,
9230 thus pretending to be `read'.
9232 EXPECTED is nonzero if the caller knows input is available. */
9235 XTread_socket (sd, bufp, numchars, expected)
9236 register int sd;
9237 /* register */ struct input_event *bufp;
9238 /* register */ int numchars;
9239 int expected;
9241 int count = 0;
9242 int nbytes = 0;
9243 XEvent event;
9244 struct frame *f;
9245 int event_found = 0;
9246 struct x_display_info *dpyinfo;
9247 struct coding_system coding;
9249 if (interrupt_input_blocked)
9251 interrupt_input_pending = 1;
9252 return -1;
9255 interrupt_input_pending = 0;
9256 BLOCK_INPUT;
9258 /* So people can tell when we have read the available input. */
9259 input_signal_count++;
9261 if (numchars <= 0)
9262 abort (); /* Don't think this happens. */
9264 ++handling_signal;
9266 /* The input should be decoded if it is from XIM. Currently the
9267 locale of XIM is the same as that of the system. So, we can use
9268 Vlocale_coding_system which is initialized properly at Emacs
9269 startup time. */
9270 setup_coding_system (Vlocale_coding_system, &coding);
9271 coding.src_multibyte = 0;
9272 coding.dst_multibyte = 1;
9273 /* The input is converted to events, thus we can't handle
9274 composition. Anyway, there's no XIM that gives us composition
9275 information. */
9276 coding.composing = COMPOSITION_DISABLED;
9278 /* Find the display we are supposed to read input for.
9279 It's the one communicating on descriptor SD. */
9280 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9282 #if 0 /* This ought to be unnecessary; let's verify it. */
9283 #ifdef FIOSNBIO
9284 /* If available, Xlib uses FIOSNBIO to make the socket
9285 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9286 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9287 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9288 fcntl (dpyinfo->connection, F_SETFL, 0);
9289 #endif /* ! defined (FIOSNBIO) */
9290 #endif
9292 #if 0 /* This code can't be made to work, with multiple displays,
9293 and appears not to be used on any system any more.
9294 Also keyboard.c doesn't turn O_NDELAY on and off
9295 for X connections. */
9296 #ifndef SIGIO
9297 #ifndef HAVE_SELECT
9298 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9300 extern int read_alarm_should_throw;
9301 read_alarm_should_throw = 1;
9302 XPeekEvent (dpyinfo->display, &event);
9303 read_alarm_should_throw = 0;
9305 #endif /* HAVE_SELECT */
9306 #endif /* SIGIO */
9307 #endif
9309 /* For debugging, this gives a way to fake an I/O error. */
9310 if (dpyinfo == XTread_socket_fake_io_error)
9312 XTread_socket_fake_io_error = 0;
9313 x_io_error_quitter (dpyinfo->display);
9316 while (XPending (dpyinfo->display))
9318 XNextEvent (dpyinfo->display, &event);
9320 #ifdef HAVE_X_I18N
9322 /* Filter events for the current X input method.
9323 XFilterEvent returns non-zero if the input method has
9324 consumed the event. We pass the frame's X window to
9325 XFilterEvent because that's the one for which the IC
9326 was created. */
9327 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9328 event.xclient.window);
9329 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9330 break;
9332 #endif
9333 event_found = 1;
9335 switch (event.type)
9337 case ClientMessage:
9339 if (event.xclient.message_type
9340 == dpyinfo->Xatom_wm_protocols
9341 && event.xclient.format == 32)
9343 if (event.xclient.data.l[0]
9344 == dpyinfo->Xatom_wm_take_focus)
9346 /* Use x_any_window_to_frame because this
9347 could be the shell widget window
9348 if the frame has no title bar. */
9349 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9350 #ifdef HAVE_X_I18N
9351 /* Not quite sure this is needed -pd */
9352 if (f && FRAME_XIC (f))
9353 XSetICFocus (FRAME_XIC (f));
9354 #endif
9355 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9356 instructs the WM to set the input focus automatically for
9357 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9358 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9359 it has set the focus. So, XSetInputFocus below is not
9360 needed.
9362 The call to XSetInputFocus below has also caused trouble. In
9363 cases where the XSetInputFocus done by the WM and the one
9364 below are temporally close (on a fast machine), the call
9365 below can generate additional FocusIn events which confuse
9366 Emacs. */
9368 /* Since we set WM_TAKE_FOCUS, we must call
9369 XSetInputFocus explicitly. But not if f is null,
9370 since that might be an event for a deleted frame. */
9371 if (f)
9373 Display *d = event.xclient.display;
9374 /* Catch and ignore errors, in case window has been
9375 iconified by a window manager such as GWM. */
9376 int count = x_catch_errors (d);
9377 XSetInputFocus (d, event.xclient.window,
9378 /* The ICCCM says this is
9379 the only valid choice. */
9380 RevertToParent,
9381 event.xclient.data.l[1]);
9382 /* This is needed to detect the error
9383 if there is an error. */
9384 XSync (d, False);
9385 x_uncatch_errors (d, count);
9387 /* Not certain about handling scroll bars here */
9388 #endif /* 0 */
9390 else if (event.xclient.data.l[0]
9391 == dpyinfo->Xatom_wm_save_yourself)
9393 /* Save state modify the WM_COMMAND property to
9394 something which can reinstate us. This notifies
9395 the session manager, who's looking for such a
9396 PropertyNotify. Can restart processing when
9397 a keyboard or mouse event arrives. */
9398 if (numchars > 0)
9400 f = x_top_window_to_frame (dpyinfo,
9401 event.xclient.window);
9403 /* This is just so we only give real data once
9404 for a single Emacs process. */
9405 if (f == SELECTED_FRAME ())
9406 XSetCommand (FRAME_X_DISPLAY (f),
9407 event.xclient.window,
9408 initial_argv, initial_argc);
9409 else if (f)
9410 XSetCommand (FRAME_X_DISPLAY (f),
9411 event.xclient.window,
9412 0, 0);
9415 else if (event.xclient.data.l[0]
9416 == dpyinfo->Xatom_wm_delete_window)
9418 struct frame *f
9419 = x_any_window_to_frame (dpyinfo,
9420 event.xclient.window);
9422 if (f)
9424 if (numchars == 0)
9425 abort ();
9427 bufp->kind = delete_window_event;
9428 XSETFRAME (bufp->frame_or_window, f);
9429 bufp->arg = Qnil;
9430 bufp++;
9432 count += 1;
9433 numchars -= 1;
9437 else if (event.xclient.message_type
9438 == dpyinfo->Xatom_wm_configure_denied)
9441 else if (event.xclient.message_type
9442 == dpyinfo->Xatom_wm_window_moved)
9444 int new_x, new_y;
9445 struct frame *f
9446 = x_window_to_frame (dpyinfo, event.xclient.window);
9448 new_x = event.xclient.data.s[0];
9449 new_y = event.xclient.data.s[1];
9451 if (f)
9453 f->output_data.x->left_pos = new_x;
9454 f->output_data.x->top_pos = new_y;
9457 #ifdef HACK_EDITRES
9458 else if (event.xclient.message_type
9459 == dpyinfo->Xatom_editres)
9461 struct frame *f
9462 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9463 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9464 &event, NULL);
9466 #endif /* HACK_EDITRES */
9467 else if ((event.xclient.message_type
9468 == dpyinfo->Xatom_DONE)
9469 || (event.xclient.message_type
9470 == dpyinfo->Xatom_PAGE))
9472 /* Ghostview job completed. Kill it. We could
9473 reply with "Next" if we received "Page", but we
9474 currently never do because we are interested in
9475 images, only, which should have 1 page. */
9476 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9477 struct frame *f
9478 = x_window_to_frame (dpyinfo, event.xclient.window);
9479 x_kill_gs_process (pixmap, f);
9480 expose_frame (f, 0, 0, 0, 0);
9482 #ifdef USE_TOOLKIT_SCROLL_BARS
9483 /* Scroll bar callbacks send a ClientMessage from which
9484 we construct an input_event. */
9485 else if (event.xclient.message_type
9486 == dpyinfo->Xatom_Scrollbar)
9488 x_scroll_bar_to_input_event (&event, bufp);
9489 ++bufp, ++count, --numchars;
9490 goto out;
9492 #endif /* USE_TOOLKIT_SCROLL_BARS */
9493 else
9494 goto OTHER;
9496 break;
9498 case SelectionNotify:
9499 #ifdef USE_X_TOOLKIT
9500 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9501 goto OTHER;
9502 #endif /* not USE_X_TOOLKIT */
9503 x_handle_selection_notify (&event.xselection);
9504 break;
9506 case SelectionClear: /* Someone has grabbed ownership. */
9507 #ifdef USE_X_TOOLKIT
9508 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9509 goto OTHER;
9510 #endif /* USE_X_TOOLKIT */
9512 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9514 if (numchars == 0)
9515 abort ();
9517 bufp->kind = selection_clear_event;
9518 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9519 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9520 SELECTION_EVENT_TIME (bufp) = eventp->time;
9521 bufp->frame_or_window = Qnil;
9522 bufp->arg = Qnil;
9523 bufp++;
9525 count += 1;
9526 numchars -= 1;
9528 break;
9530 case SelectionRequest: /* Someone wants our selection. */
9531 #ifdef USE_X_TOOLKIT
9532 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9533 goto OTHER;
9534 #endif /* USE_X_TOOLKIT */
9535 if (x_queue_selection_requests)
9536 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9537 &event);
9538 else
9540 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9542 if (numchars == 0)
9543 abort ();
9545 bufp->kind = selection_request_event;
9546 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9547 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9548 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9549 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9550 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9551 SELECTION_EVENT_TIME (bufp) = eventp->time;
9552 bufp->frame_or_window = Qnil;
9553 bufp->arg = Qnil;
9554 bufp++;
9556 count += 1;
9557 numchars -= 1;
9559 break;
9561 case PropertyNotify:
9562 #ifdef USE_X_TOOLKIT
9563 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9564 goto OTHER;
9565 #endif /* not USE_X_TOOLKIT */
9566 x_handle_property_notify (&event.xproperty);
9567 break;
9569 case ReparentNotify:
9570 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9571 if (f)
9573 int x, y;
9574 f->output_data.x->parent_desc = event.xreparent.parent;
9575 x_real_positions (f, &x, &y);
9576 f->output_data.x->left_pos = x;
9577 f->output_data.x->top_pos = y;
9579 break;
9581 case Expose:
9582 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9583 if (f)
9585 if (f->async_visible == 0)
9587 f->async_visible = 1;
9588 f->async_iconified = 0;
9589 f->output_data.x->has_been_visible = 1;
9590 SET_FRAME_GARBAGED (f);
9592 else
9593 expose_frame (x_window_to_frame (dpyinfo,
9594 event.xexpose.window),
9595 event.xexpose.x, event.xexpose.y,
9596 event.xexpose.width, event.xexpose.height);
9598 else
9600 #ifdef USE_TOOLKIT_SCROLL_BARS
9601 /* Dispatch event to the widget. */
9602 goto OTHER;
9603 #else /* not USE_TOOLKIT_SCROLL_BARS */
9604 struct scroll_bar *bar
9605 = x_window_to_scroll_bar (event.xexpose.window);
9607 if (bar)
9608 x_scroll_bar_expose (bar, &event);
9609 #ifdef USE_X_TOOLKIT
9610 else
9611 goto OTHER;
9612 #endif /* USE_X_TOOLKIT */
9613 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9615 break;
9617 case GraphicsExpose: /* This occurs when an XCopyArea's
9618 source area was obscured or not
9619 available.*/
9620 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9621 if (f)
9623 expose_frame (f,
9624 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9625 event.xgraphicsexpose.width,
9626 event.xgraphicsexpose.height);
9628 #ifdef USE_X_TOOLKIT
9629 else
9630 goto OTHER;
9631 #endif /* USE_X_TOOLKIT */
9632 break;
9634 case NoExpose: /* This occurs when an XCopyArea's
9635 source area was completely
9636 available */
9637 break;
9639 case UnmapNotify:
9640 /* Redo the mouse-highlight after the tooltip has gone. */
9641 if (event.xmap.window == tip_window)
9643 tip_window = 0;
9644 redo_mouse_highlight ();
9647 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9648 if (f) /* F may no longer exist if
9649 the frame was deleted. */
9651 /* While a frame is unmapped, display generation is
9652 disabled; you don't want to spend time updating a
9653 display that won't ever be seen. */
9654 f->async_visible = 0;
9655 /* We can't distinguish, from the event, whether the window
9656 has become iconified or invisible. So assume, if it
9657 was previously visible, than now it is iconified.
9658 But x_make_frame_invisible clears both
9659 the visible flag and the iconified flag;
9660 and that way, we know the window is not iconified now. */
9661 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9663 f->async_iconified = 1;
9665 bufp->kind = iconify_event;
9666 XSETFRAME (bufp->frame_or_window, f);
9667 bufp->arg = Qnil;
9668 bufp++;
9669 count++;
9670 numchars--;
9673 goto OTHER;
9675 case MapNotify:
9676 if (event.xmap.window == tip_window)
9677 /* The tooltip has been drawn already. Avoid
9678 the SET_FRAME_GARBAGED below. */
9679 goto OTHER;
9681 /* We use x_top_window_to_frame because map events can
9682 come for sub-windows and they don't mean that the
9683 frame is visible. */
9684 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9685 if (f)
9687 f->async_visible = 1;
9688 f->async_iconified = 0;
9689 f->output_data.x->has_been_visible = 1;
9691 /* wait_reading_process_input will notice this and update
9692 the frame's display structures. */
9693 SET_FRAME_GARBAGED (f);
9695 if (f->iconified)
9697 bufp->kind = deiconify_event;
9698 XSETFRAME (bufp->frame_or_window, f);
9699 bufp->arg = Qnil;
9700 bufp++;
9701 count++;
9702 numchars--;
9704 else if (! NILP (Vframe_list)
9705 && ! NILP (XCDR (Vframe_list)))
9706 /* Force a redisplay sooner or later
9707 to update the frame titles
9708 in case this is the second frame. */
9709 record_asynch_buffer_change ();
9711 goto OTHER;
9713 case KeyPress:
9714 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9716 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
9717 /* I couldn't find a way to prevent LessTif scroll bars
9718 from consuming key events. */
9719 if (f == 0)
9721 Widget widget = XtWindowToWidget (dpyinfo->display,
9722 event.xkey.window);
9723 if (widget && XmIsScrollBar (widget))
9725 widget = XtParent (widget);
9726 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9729 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
9731 if (f != 0)
9733 KeySym keysym, orig_keysym;
9734 /* al%imercury@uunet.uu.net says that making this 81
9735 instead of 80 fixed a bug whereby meta chars made
9736 his Emacs hang.
9738 It seems that some version of XmbLookupString has
9739 a bug of not returning XBufferOverflow in
9740 status_return even if the input is too long to
9741 fit in 81 bytes. So, we must prepare sufficient
9742 bytes for copy_buffer. 513 bytes (256 chars for
9743 two-byte character set) seems to be a faily good
9744 approximation. -- 2000.8.10 handa@etl.go.jp */
9745 unsigned char copy_buffer[513];
9746 unsigned char *copy_bufptr = copy_buffer;
9747 int copy_bufsiz = sizeof (copy_buffer);
9748 int modifiers;
9750 event.xkey.state
9751 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9752 extra_keyboard_modifiers);
9753 modifiers = event.xkey.state;
9755 /* This will have to go some day... */
9757 /* make_lispy_event turns chars into control chars.
9758 Don't do it here because XLookupString is too eager. */
9759 event.xkey.state &= ~ControlMask;
9760 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9761 | dpyinfo->super_mod_mask
9762 | dpyinfo->hyper_mod_mask
9763 | dpyinfo->alt_mod_mask);
9765 /* In case Meta is ComposeCharacter,
9766 clear its status. According to Markus Ehrnsperger
9767 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9768 this enables ComposeCharacter to work whether or
9769 not it is combined with Meta. */
9770 if (modifiers & dpyinfo->meta_mod_mask)
9771 bzero (&compose_status, sizeof (compose_status));
9773 #ifdef HAVE_X_I18N
9774 if (FRAME_XIC (f))
9776 Status status_return;
9778 nbytes = XmbLookupString (FRAME_XIC (f),
9779 &event.xkey, copy_bufptr,
9780 copy_bufsiz, &keysym,
9781 &status_return);
9782 if (status_return == XBufferOverflow)
9784 copy_bufsiz = nbytes + 1;
9785 copy_bufptr = (char *) alloca (copy_bufsiz);
9786 nbytes = XmbLookupString (FRAME_XIC (f),
9787 &event.xkey, copy_bufptr,
9788 copy_bufsiz, &keysym,
9789 &status_return);
9792 if (status_return == XLookupNone)
9793 break;
9794 else if (status_return == XLookupChars)
9796 keysym = NoSymbol;
9797 modifiers = 0;
9799 else if (status_return != XLookupKeySym
9800 && status_return != XLookupBoth)
9801 abort ();
9803 else
9804 nbytes = XLookupString (&event.xkey, copy_bufptr,
9805 copy_bufsiz, &keysym,
9806 &compose_status);
9807 #else
9808 nbytes = XLookupString (&event.xkey, copy_bufptr,
9809 copy_bufsiz, &keysym,
9810 &compose_status);
9811 #endif
9813 orig_keysym = keysym;
9815 if (numchars > 1)
9817 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9818 || keysym == XK_Delete
9819 #ifdef XK_ISO_Left_Tab
9820 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9821 #endif
9822 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9823 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9824 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9825 #ifdef HPUX
9826 /* This recognizes the "extended function keys".
9827 It seems there's no cleaner way.
9828 Test IsModifierKey to avoid handling mode_switch
9829 incorrectly. */
9830 || ((unsigned) (keysym) >= XK_Select
9831 && (unsigned)(keysym) < XK_KP_Space)
9832 #endif
9833 #ifdef XK_dead_circumflex
9834 || orig_keysym == XK_dead_circumflex
9835 #endif
9836 #ifdef XK_dead_grave
9837 || orig_keysym == XK_dead_grave
9838 #endif
9839 #ifdef XK_dead_tilde
9840 || orig_keysym == XK_dead_tilde
9841 #endif
9842 #ifdef XK_dead_diaeresis
9843 || orig_keysym == XK_dead_diaeresis
9844 #endif
9845 #ifdef XK_dead_macron
9846 || orig_keysym == XK_dead_macron
9847 #endif
9848 #ifdef XK_dead_degree
9849 || orig_keysym == XK_dead_degree
9850 #endif
9851 #ifdef XK_dead_acute
9852 || orig_keysym == XK_dead_acute
9853 #endif
9854 #ifdef XK_dead_cedilla
9855 || orig_keysym == XK_dead_cedilla
9856 #endif
9857 #ifdef XK_dead_breve
9858 || orig_keysym == XK_dead_breve
9859 #endif
9860 #ifdef XK_dead_ogonek
9861 || orig_keysym == XK_dead_ogonek
9862 #endif
9863 #ifdef XK_dead_caron
9864 || orig_keysym == XK_dead_caron
9865 #endif
9866 #ifdef XK_dead_doubleacute
9867 || orig_keysym == XK_dead_doubleacute
9868 #endif
9869 #ifdef XK_dead_abovedot
9870 || orig_keysym == XK_dead_abovedot
9871 #endif
9872 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9873 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9874 /* Any "vendor-specific" key is ok. */
9875 || (orig_keysym & (1 << 28)))
9876 && ! (IsModifierKey (orig_keysym)
9877 #ifndef HAVE_X11R5
9878 #ifdef XK_Mode_switch
9879 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9880 #endif
9881 #ifdef XK_Num_Lock
9882 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9883 #endif
9884 #endif /* not HAVE_X11R5 */
9887 if (temp_index == sizeof temp_buffer / sizeof (short))
9888 temp_index = 0;
9889 temp_buffer[temp_index++] = keysym;
9890 bufp->kind = non_ascii_keystroke;
9891 bufp->code = keysym;
9892 XSETFRAME (bufp->frame_or_window, f);
9893 bufp->arg = Qnil;
9894 bufp->modifiers
9895 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9896 modifiers);
9897 bufp->timestamp = event.xkey.time;
9898 bufp++;
9899 count++;
9900 numchars--;
9902 else if (numchars > nbytes)
9904 register int i;
9905 register int c;
9906 int nchars, len;
9908 for (i = 0; i < nbytes; i++)
9910 if (temp_index == (sizeof temp_buffer
9911 / sizeof (short)))
9912 temp_index = 0;
9913 temp_buffer[temp_index++] = copy_bufptr[i];
9916 if (/* If the event is not from XIM, */
9917 event.xkey.keycode != 0
9918 /* or the current locale doesn't request
9919 decoding of the intup data, ... */
9920 || coding.type == coding_type_raw_text
9921 || coding.type == coding_type_no_conversion)
9923 /* ... we can use the input data as is. */
9924 nchars = nbytes;
9926 else
9928 /* We have to decode the input data. */
9929 int require;
9930 unsigned char *p;
9932 require = decoding_buffer_size (&coding, nbytes);
9933 p = (unsigned char *) alloca (require);
9934 coding.mode |= CODING_MODE_LAST_BLOCK;
9935 decode_coding (&coding, copy_bufptr, p,
9936 nbytes, require);
9937 nbytes = coding.produced;
9938 nchars = coding.produced_char;
9939 copy_bufptr = p;
9942 /* Convert the input data to a sequence of
9943 character events. */
9944 for (i = 0; i < nbytes; i += len)
9946 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9947 nbytes - i, len);
9948 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9949 ? ascii_keystroke
9950 : multibyte_char_keystroke);
9951 bufp->code = c;
9952 XSETFRAME (bufp->frame_or_window, f);
9953 bufp->arg = Qnil;
9954 bufp->modifiers
9955 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9956 modifiers);
9957 bufp->timestamp = event.xkey.time;
9958 bufp++;
9961 count += nchars;
9962 numchars -= nchars;
9964 if (keysym == NoSymbol)
9965 break;
9967 else
9968 abort ();
9970 else
9971 abort ();
9973 #ifdef HAVE_X_I18N
9974 /* Don't dispatch this event since XtDispatchEvent calls
9975 XFilterEvent, and two calls in a row may freeze the
9976 client. */
9977 break;
9978 #else
9979 goto OTHER;
9980 #endif
9982 case KeyRelease:
9983 #ifdef HAVE_X_I18N
9984 /* Don't dispatch this event since XtDispatchEvent calls
9985 XFilterEvent, and two calls in a row may freeze the
9986 client. */
9987 break;
9988 #else
9989 goto OTHER;
9990 #endif
9992 /* Here's a possible interpretation of the whole
9993 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9994 you get a FocusIn event, you have to get a FocusOut
9995 event before you relinquish the focus. If you
9996 haven't received a FocusIn event, then a mere
9997 LeaveNotify is enough to free you. */
9999 case EnterNotify:
10001 int from_menu_bar_p = 0;
10003 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10005 #ifdef LESSTIF_VERSION
10006 /* When clicking outside of a menu bar popup to close
10007 it, we get a FocusIn/ EnterNotify sequence of
10008 events. The flag event.xcrossing.focus is not set
10009 in the EnterNotify event of that sequence because
10010 the focus is in the menu bar,
10011 event.xcrossing.window is the frame's X window.
10012 Unconditionally setting the focus frame to null in
10013 this case is not the right thing, because no event
10014 follows that could set the focus frame to the right
10015 value.
10017 This could be a LessTif bug, but I wasn't able to
10018 reproduce the behavior in a simple test program.
10020 (gerd, LessTif 0.88.1). */
10022 if (!event.xcrossing.focus
10023 && f
10024 && f->output_data.x->menubar_widget)
10026 Window focus;
10027 int revert;
10029 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10030 if (focus == XtWindow (f->output_data.x->menubar_widget))
10031 from_menu_bar_p = 1;
10033 #endif /* LESSTIF_VERSION */
10035 if (event.xcrossing.focus || from_menu_bar_p)
10037 /* Avoid nasty pop/raise loops. */
10038 if (f && (!(f->auto_raise)
10039 || !(f->auto_lower)
10040 || (event.xcrossing.time - enter_timestamp) > 500))
10042 x_new_focus_frame (dpyinfo, f);
10043 enter_timestamp = event.xcrossing.time;
10046 else if (f == dpyinfo->x_focus_frame)
10047 x_new_focus_frame (dpyinfo, 0);
10049 /* EnterNotify counts as mouse movement,
10050 so update things that depend on mouse position. */
10051 if (f && !f->output_data.x->busy_p)
10052 note_mouse_movement (f, &event.xmotion);
10053 goto OTHER;
10056 case FocusIn:
10057 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10058 if (event.xfocus.detail != NotifyPointer)
10059 dpyinfo->x_focus_event_frame = f;
10060 if (f)
10062 x_new_focus_frame (dpyinfo, f);
10064 /* Don't stop displaying the initial startup message
10065 for a switch-frame event we don't need. */
10066 if (GC_NILP (Vterminal_frame)
10067 && GC_CONSP (Vframe_list)
10068 && !GC_NILP (XCDR (Vframe_list)))
10070 bufp->kind = FOCUS_IN_EVENT;
10071 XSETFRAME (bufp->frame_or_window, f);
10072 bufp->arg = Qnil;
10073 ++bufp, ++count, --numchars;
10077 #ifdef HAVE_X_I18N
10078 if (f && FRAME_XIC (f))
10079 XSetICFocus (FRAME_XIC (f));
10080 #endif
10082 goto OTHER;
10084 case LeaveNotify:
10085 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10086 if (f)
10088 int from_menu_bar_p = 0;
10090 if (f == dpyinfo->mouse_face_mouse_frame)
10092 /* If we move outside the frame, then we're
10093 certainly no longer on any text in the frame. */
10094 clear_mouse_face (dpyinfo);
10095 dpyinfo->mouse_face_mouse_frame = 0;
10098 /* Generate a nil HELP_EVENT to cancel a help-echo.
10099 Do it only if there's something to cancel.
10100 Otherwise, the startup message is cleared when
10101 the mouse leaves the frame. */
10102 if (any_help_event_p)
10104 Lisp_Object frame;
10105 int n;
10107 XSETFRAME (frame, f);
10108 help_echo = Qnil;
10109 n = gen_help_event (bufp, numchars,
10110 Qnil, frame, Qnil, Qnil, 0);
10111 bufp += n, count += n, numchars -= n;
10114 #ifdef LESSTIF_VERSION
10115 /* Please see the comment at the start of the
10116 EnterNotify case. */
10117 if (!event.xcrossing.focus
10118 && f->output_data.x->menubar_widget)
10120 Window focus;
10121 int revert;
10122 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10123 if (focus == XtWindow (f->output_data.x->menubar_widget))
10124 from_menu_bar_p = 1;
10126 #endif /* LESSTIF_VERSION */
10128 if (event.xcrossing.focus || from_menu_bar_p)
10129 x_mouse_leave (dpyinfo);
10130 else
10132 if (f == dpyinfo->x_focus_event_frame)
10133 dpyinfo->x_focus_event_frame = 0;
10134 if (f == dpyinfo->x_focus_frame)
10135 x_new_focus_frame (dpyinfo, 0);
10138 goto OTHER;
10140 case FocusOut:
10141 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10142 if (event.xfocus.detail != NotifyPointer
10143 && f == dpyinfo->x_focus_event_frame)
10144 dpyinfo->x_focus_event_frame = 0;
10145 if (f && f == dpyinfo->x_focus_frame)
10146 x_new_focus_frame (dpyinfo, 0);
10148 #ifdef HAVE_X_I18N
10149 if (f && FRAME_XIC (f))
10150 XUnsetICFocus (FRAME_XIC (f));
10151 #endif
10153 goto OTHER;
10155 case MotionNotify:
10157 previous_help_echo = help_echo;
10158 help_echo = help_echo_object = help_echo_window = Qnil;
10159 help_echo_pos = -1;
10161 if (dpyinfo->grabbed && last_mouse_frame
10162 && FRAME_LIVE_P (last_mouse_frame))
10163 f = last_mouse_frame;
10164 else
10165 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10167 if (f)
10168 note_mouse_movement (f, &event.xmotion);
10169 else
10171 #ifndef USE_TOOLKIT_SCROLL_BARS
10172 struct scroll_bar *bar
10173 = x_window_to_scroll_bar (event.xmotion.window);
10175 if (bar)
10176 x_scroll_bar_note_movement (bar, &event);
10177 #endif /* USE_TOOLKIT_SCROLL_BARS */
10179 /* If we move outside the frame, then we're
10180 certainly no longer on any text in the frame. */
10181 clear_mouse_face (dpyinfo);
10184 /* If the contents of the global variable help_echo
10185 has changed, generate a HELP_EVENT. */
10186 if (!NILP (help_echo)
10187 || !NILP (previous_help_echo))
10189 Lisp_Object frame;
10190 int n;
10192 if (f)
10193 XSETFRAME (frame, f);
10194 else
10195 frame = Qnil;
10197 any_help_event_p = 1;
10198 n = gen_help_event (bufp, numchars, help_echo, frame,
10199 help_echo_window, help_echo_object,
10200 help_echo_pos);
10201 bufp += n, count += n, numchars -= n;
10204 goto OTHER;
10207 case ConfigureNotify:
10208 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10209 if (f)
10211 #ifndef USE_X_TOOLKIT
10212 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10213 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10215 /* In the toolkit version, change_frame_size
10216 is called by the code that handles resizing
10217 of the EmacsFrame widget. */
10219 /* Even if the number of character rows and columns has
10220 not changed, the font size may have changed, so we need
10221 to check the pixel dimensions as well. */
10222 if (columns != f->width
10223 || rows != f->height
10224 || event.xconfigure.width != f->output_data.x->pixel_width
10225 || event.xconfigure.height != f->output_data.x->pixel_height)
10227 change_frame_size (f, rows, columns, 0, 1, 0);
10228 SET_FRAME_GARBAGED (f);
10229 cancel_mouse_face (f);
10231 #endif
10233 f->output_data.x->pixel_width = event.xconfigure.width;
10234 f->output_data.x->pixel_height = event.xconfigure.height;
10236 /* What we have now is the position of Emacs's own window.
10237 Convert that to the position of the window manager window. */
10238 x_real_positions (f, &f->output_data.x->left_pos,
10239 &f->output_data.x->top_pos);
10241 #ifdef HAVE_X_I18N
10242 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10243 xic_set_statusarea (f);
10244 #endif
10246 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10248 /* Since the WM decorations come below top_pos now,
10249 we must put them below top_pos in the future. */
10250 f->output_data.x->win_gravity = NorthWestGravity;
10251 x_wm_set_size_hint (f, (long) 0, 0);
10253 #ifdef USE_MOTIF
10254 /* Some window managers pass (0,0) as the location of
10255 the window, and the Motif event handler stores it
10256 in the emacs widget, which messes up Motif menus. */
10257 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10259 event.xconfigure.x = f->output_data.x->widget->core.x;
10260 event.xconfigure.y = f->output_data.x->widget->core.y;
10262 #endif /* USE_MOTIF */
10264 goto OTHER;
10266 case ButtonPress:
10267 case ButtonRelease:
10269 /* If we decide we want to generate an event to be seen
10270 by the rest of Emacs, we put it here. */
10271 struct input_event emacs_event;
10272 int tool_bar_p = 0;
10274 emacs_event.kind = no_event;
10275 bzero (&compose_status, sizeof (compose_status));
10277 if (dpyinfo->grabbed
10278 && last_mouse_frame
10279 && FRAME_LIVE_P (last_mouse_frame))
10280 f = last_mouse_frame;
10281 else
10282 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10284 if (f)
10286 /* Is this in the tool-bar? */
10287 if (WINDOWP (f->tool_bar_window)
10288 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10290 Lisp_Object window;
10291 int p, x, y;
10293 x = event.xbutton.x;
10294 y = event.xbutton.y;
10296 /* Set x and y. */
10297 window = window_from_coordinates (f, x, y, &p, 1);
10298 if (EQ (window, f->tool_bar_window))
10300 x_handle_tool_bar_click (f, &event.xbutton);
10301 tool_bar_p = 1;
10305 if (!tool_bar_p)
10306 if (!dpyinfo->x_focus_frame
10307 || f == dpyinfo->x_focus_frame)
10308 construct_mouse_click (&emacs_event, &event, f);
10310 else
10312 #ifndef USE_TOOLKIT_SCROLL_BARS
10313 struct scroll_bar *bar
10314 = x_window_to_scroll_bar (event.xbutton.window);
10316 if (bar)
10317 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10318 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10321 if (event.type == ButtonPress)
10323 dpyinfo->grabbed |= (1 << event.xbutton.button);
10324 last_mouse_frame = f;
10325 /* Ignore any mouse motion that happened
10326 before this event; any subsequent mouse-movement
10327 Emacs events should reflect only motion after
10328 the ButtonPress. */
10329 if (f != 0)
10330 f->mouse_moved = 0;
10332 if (!tool_bar_p)
10333 last_tool_bar_item = -1;
10335 else
10337 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10340 if (numchars >= 1 && emacs_event.kind != no_event)
10342 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10343 bufp++;
10344 count++;
10345 numchars--;
10348 #ifdef USE_X_TOOLKIT
10349 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10350 /* For a down-event in the menu bar,
10351 don't pass it to Xt right now.
10352 Instead, save it away
10353 and we will pass it to Xt from kbd_buffer_get_event.
10354 That way, we can run some Lisp code first. */
10355 if (f && event.type == ButtonPress
10356 /* Verify the event is really within the menu bar
10357 and not just sent to it due to grabbing. */
10358 && event.xbutton.x >= 0
10359 && event.xbutton.x < f->output_data.x->pixel_width
10360 && event.xbutton.y >= 0
10361 && event.xbutton.y < f->output_data.x->menubar_height
10362 && event.xbutton.same_screen)
10364 SET_SAVED_BUTTON_EVENT;
10365 XSETFRAME (last_mouse_press_frame, f);
10367 else if (event.type == ButtonPress)
10369 last_mouse_press_frame = Qnil;
10370 goto OTHER;
10373 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10374 but I am trying to be cautious. */
10375 else if (event.type == ButtonRelease)
10377 if (!NILP (last_mouse_press_frame))
10379 f = XFRAME (last_mouse_press_frame);
10380 if (f->output_data.x)
10381 SET_SAVED_BUTTON_EVENT;
10383 else
10384 goto OTHER;
10386 #endif /* USE_MOTIF */
10387 else
10388 goto OTHER;
10389 #endif /* USE_X_TOOLKIT */
10391 break;
10393 case CirculateNotify:
10394 goto OTHER;
10396 case CirculateRequest:
10397 goto OTHER;
10399 case VisibilityNotify:
10400 goto OTHER;
10402 case MappingNotify:
10403 /* Someone has changed the keyboard mapping - update the
10404 local cache. */
10405 switch (event.xmapping.request)
10407 case MappingModifier:
10408 x_find_modifier_meanings (dpyinfo);
10409 /* This is meant to fall through. */
10410 case MappingKeyboard:
10411 XRefreshKeyboardMapping (&event.xmapping);
10413 goto OTHER;
10415 default:
10416 OTHER:
10417 #ifdef USE_X_TOOLKIT
10418 BLOCK_INPUT;
10419 XtDispatchEvent (&event);
10420 UNBLOCK_INPUT;
10421 #endif /* USE_X_TOOLKIT */
10422 break;
10427 out:;
10429 /* On some systems, an X bug causes Emacs to get no more events
10430 when the window is destroyed. Detect that. (1994.) */
10431 if (! event_found)
10433 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10434 One XNOOP in 100 loops will make Emacs terminate.
10435 B. Bretthauer, 1994 */
10436 x_noop_count++;
10437 if (x_noop_count >= 100)
10439 x_noop_count=0;
10441 if (next_noop_dpyinfo == 0)
10442 next_noop_dpyinfo = x_display_list;
10444 XNoOp (next_noop_dpyinfo->display);
10446 /* Each time we get here, cycle through the displays now open. */
10447 next_noop_dpyinfo = next_noop_dpyinfo->next;
10451 /* If the focus was just given to an auto-raising frame,
10452 raise it now. */
10453 /* ??? This ought to be able to handle more than one such frame. */
10454 if (pending_autoraise_frame)
10456 x_raise_frame (pending_autoraise_frame);
10457 pending_autoraise_frame = 0;
10460 UNBLOCK_INPUT;
10461 --handling_signal;
10462 return count;
10468 /***********************************************************************
10469 Text Cursor
10470 ***********************************************************************/
10472 /* Note if the text cursor of window W has been overwritten by a
10473 drawing operation that outputs N glyphs starting at HPOS in the
10474 line given by output_cursor.vpos. N < 0 means all the rest of the
10475 line after HPOS has been written. */
10477 static void
10478 note_overwritten_text_cursor (w, hpos, n)
10479 struct window *w;
10480 int hpos, n;
10482 if (updated_area == TEXT_AREA
10483 && output_cursor.vpos == w->phys_cursor.vpos
10484 && hpos <= w->phys_cursor.hpos
10485 && (n < 0
10486 || hpos + n > w->phys_cursor.hpos))
10487 w->phys_cursor_on_p = 0;
10491 /* Set clipping for output in glyph row ROW. W is the window in which
10492 we operate. GC is the graphics context to set clipping in.
10493 WHOLE_LINE_P non-zero means include the areas used for truncation
10494 mark display and alike in the clipping rectangle.
10496 ROW may be a text row or, e.g., a mode line. Text rows must be
10497 clipped to the interior of the window dedicated to text display,
10498 mode lines must be clipped to the whole window. */
10500 static void
10501 x_clip_to_row (w, row, gc, whole_line_p)
10502 struct window *w;
10503 struct glyph_row *row;
10504 GC gc;
10505 int whole_line_p;
10507 struct frame *f = XFRAME (WINDOW_FRAME (w));
10508 XRectangle clip_rect;
10509 int window_x, window_y, window_width, window_height;
10511 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10513 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10514 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10515 clip_rect.y = max (clip_rect.y, window_y);
10516 clip_rect.width = window_width;
10517 clip_rect.height = row->visible_height;
10519 /* If clipping to the whole line, including trunc marks, extend
10520 the rectangle to the left and increase its width. */
10521 if (whole_line_p)
10523 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10524 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10527 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10531 /* Draw a hollow box cursor on window W in glyph row ROW. */
10533 static void
10534 x_draw_hollow_cursor (w, row)
10535 struct window *w;
10536 struct glyph_row *row;
10538 struct frame *f = XFRAME (WINDOW_FRAME (w));
10539 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10540 Display *dpy = FRAME_X_DISPLAY (f);
10541 int x, y, wd, h;
10542 XGCValues xgcv;
10543 struct glyph *cursor_glyph;
10544 GC gc;
10546 /* Compute frame-relative coordinates from window-relative
10547 coordinates. */
10548 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10549 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10550 + row->ascent - w->phys_cursor_ascent);
10551 h = row->height - 1;
10553 /* Get the glyph the cursor is on. If we can't tell because
10554 the current matrix is invalid or such, give up. */
10555 cursor_glyph = get_phys_cursor_glyph (w);
10556 if (cursor_glyph == NULL)
10557 return;
10559 /* Compute the width of the rectangle to draw. If on a stretch
10560 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10561 rectangle as wide as the glyph, but use a canonical character
10562 width instead. */
10563 wd = cursor_glyph->pixel_width - 1;
10564 if (cursor_glyph->type == STRETCH_GLYPH
10565 && !x_stretch_cursor_p)
10566 wd = min (CANON_X_UNIT (f), wd);
10568 /* The foreground of cursor_gc is typically the same as the normal
10569 background color, which can cause the cursor box to be invisible. */
10570 xgcv.foreground = f->output_data.x->cursor_pixel;
10571 if (dpyinfo->scratch_cursor_gc)
10572 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10573 else
10574 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10575 GCForeground, &xgcv);
10576 gc = dpyinfo->scratch_cursor_gc;
10578 /* Set clipping, draw the rectangle, and reset clipping again. */
10579 x_clip_to_row (w, row, gc, 0);
10580 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10581 XSetClipMask (dpy, gc, None);
10585 /* Draw a bar cursor on window W in glyph row ROW.
10587 Implementation note: One would like to draw a bar cursor with an
10588 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10589 Unfortunately, I didn't find a font yet that has this property set.
10590 --gerd. */
10592 static void
10593 x_draw_bar_cursor (w, row, width)
10594 struct window *w;
10595 struct glyph_row *row;
10596 int width;
10598 struct frame *f = XFRAME (w->frame);
10599 struct glyph *cursor_glyph;
10600 GC gc;
10601 int x;
10602 unsigned long mask;
10603 XGCValues xgcv;
10604 Display *dpy;
10605 Window window;
10607 /* If cursor is out of bounds, don't draw garbage. This can happen
10608 in mini-buffer windows when switching between echo area glyphs
10609 and mini-buffer. */
10610 cursor_glyph = get_phys_cursor_glyph (w);
10611 if (cursor_glyph == NULL)
10612 return;
10614 /* If on an image, draw like a normal cursor. That's usually better
10615 visible than drawing a bar, esp. if the image is large so that
10616 the bar might not be in the window. */
10617 if (cursor_glyph->type == IMAGE_GLYPH)
10619 struct glyph_row *row;
10620 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10621 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10623 else
10625 xgcv.background = f->output_data.x->cursor_pixel;
10626 xgcv.foreground = f->output_data.x->cursor_pixel;
10627 xgcv.graphics_exposures = 0;
10628 mask = GCForeground | GCBackground | GCGraphicsExposures;
10629 dpy = FRAME_X_DISPLAY (f);
10630 window = FRAME_X_WINDOW (f);
10631 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10633 if (gc)
10634 XChangeGC (dpy, gc, mask, &xgcv);
10635 else
10637 gc = XCreateGC (dpy, window, mask, &xgcv);
10638 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10641 if (width < 0)
10642 width = f->output_data.x->cursor_width;
10644 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10645 x_clip_to_row (w, row, gc, 0);
10646 XFillRectangle (dpy, window, gc,
10648 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10649 min (cursor_glyph->pixel_width, width),
10650 row->height);
10651 XSetClipMask (dpy, gc, None);
10656 /* Clear the cursor of window W to background color, and mark the
10657 cursor as not shown. This is used when the text where the cursor
10658 is is about to be rewritten. */
10660 static void
10661 x_clear_cursor (w)
10662 struct window *w;
10664 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10665 x_update_window_cursor (w, 0);
10669 /* Draw the cursor glyph of window W in glyph row ROW. See the
10670 comment of x_draw_glyphs for the meaning of HL. */
10672 static void
10673 x_draw_phys_cursor_glyph (w, row, hl)
10674 struct window *w;
10675 struct glyph_row *row;
10676 enum draw_glyphs_face hl;
10678 /* If cursor hpos is out of bounds, don't draw garbage. This can
10679 happen in mini-buffer windows when switching between echo area
10680 glyphs and mini-buffer. */
10681 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10683 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10684 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10685 hl, 0, 0, 0);
10687 /* When we erase the cursor, and ROW is overlapped by other
10688 rows, make sure that these overlapping parts of other rows
10689 are redrawn. */
10690 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10692 if (row > w->current_matrix->rows
10693 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10694 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10696 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10697 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10698 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10704 /* Erase the image of a cursor of window W from the screen. */
10706 static void
10707 x_erase_phys_cursor (w)
10708 struct window *w;
10710 struct frame *f = XFRAME (w->frame);
10711 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10712 int hpos = w->phys_cursor.hpos;
10713 int vpos = w->phys_cursor.vpos;
10714 int mouse_face_here_p = 0;
10715 struct glyph_matrix *active_glyphs = w->current_matrix;
10716 struct glyph_row *cursor_row;
10717 struct glyph *cursor_glyph;
10718 enum draw_glyphs_face hl;
10720 /* No cursor displayed or row invalidated => nothing to do on the
10721 screen. */
10722 if (w->phys_cursor_type == NO_CURSOR)
10723 goto mark_cursor_off;
10725 /* VPOS >= active_glyphs->nrows means that window has been resized.
10726 Don't bother to erase the cursor. */
10727 if (vpos >= active_glyphs->nrows)
10728 goto mark_cursor_off;
10730 /* If row containing cursor is marked invalid, there is nothing we
10731 can do. */
10732 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10733 if (!cursor_row->enabled_p)
10734 goto mark_cursor_off;
10736 /* This can happen when the new row is shorter than the old one.
10737 In this case, either x_draw_glyphs or clear_end_of_line
10738 should have cleared the cursor. Note that we wouldn't be
10739 able to erase the cursor in this case because we don't have a
10740 cursor glyph at hand. */
10741 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10742 goto mark_cursor_off;
10744 /* If the cursor is in the mouse face area, redisplay that when
10745 we clear the cursor. */
10746 if (! NILP (dpyinfo->mouse_face_window)
10747 && w == XWINDOW (dpyinfo->mouse_face_window)
10748 && (vpos > dpyinfo->mouse_face_beg_row
10749 || (vpos == dpyinfo->mouse_face_beg_row
10750 && hpos >= dpyinfo->mouse_face_beg_col))
10751 && (vpos < dpyinfo->mouse_face_end_row
10752 || (vpos == dpyinfo->mouse_face_end_row
10753 && hpos < dpyinfo->mouse_face_end_col))
10754 /* Don't redraw the cursor's spot in mouse face if it is at the
10755 end of a line (on a newline). The cursor appears there, but
10756 mouse highlighting does not. */
10757 && cursor_row->used[TEXT_AREA] > hpos)
10758 mouse_face_here_p = 1;
10760 /* Maybe clear the display under the cursor. */
10761 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10763 int x;
10764 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10766 cursor_glyph = get_phys_cursor_glyph (w);
10767 if (cursor_glyph == NULL)
10768 goto mark_cursor_off;
10770 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10772 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10774 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10775 cursor_row->y)),
10776 cursor_glyph->pixel_width,
10777 cursor_row->visible_height,
10778 False);
10781 /* Erase the cursor by redrawing the character underneath it. */
10782 if (mouse_face_here_p)
10783 hl = DRAW_MOUSE_FACE;
10784 else if (cursor_row->inverse_p)
10785 hl = DRAW_INVERSE_VIDEO;
10786 else
10787 hl = DRAW_NORMAL_TEXT;
10788 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10790 mark_cursor_off:
10791 w->phys_cursor_on_p = 0;
10792 w->phys_cursor_type = NO_CURSOR;
10796 /* Display or clear cursor of window W. If ON is zero, clear the
10797 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10798 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10800 void
10801 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10802 struct window *w;
10803 int on, hpos, vpos, x, y;
10805 struct frame *f = XFRAME (w->frame);
10806 int new_cursor_type;
10807 int new_cursor_width;
10808 struct glyph_matrix *current_glyphs;
10809 struct glyph_row *glyph_row;
10810 struct glyph *glyph;
10812 /* This is pointless on invisible frames, and dangerous on garbaged
10813 windows and frames; in the latter case, the frame or window may
10814 be in the midst of changing its size, and x and y may be off the
10815 window. */
10816 if (! FRAME_VISIBLE_P (f)
10817 || FRAME_GARBAGED_P (f)
10818 || vpos >= w->current_matrix->nrows
10819 || hpos >= w->current_matrix->matrix_w)
10820 return;
10822 /* If cursor is off and we want it off, return quickly. */
10823 if (!on && !w->phys_cursor_on_p)
10824 return;
10826 current_glyphs = w->current_matrix;
10827 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10828 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10830 /* If cursor row is not enabled, we don't really know where to
10831 display the cursor. */
10832 if (!glyph_row->enabled_p)
10834 w->phys_cursor_on_p = 0;
10835 return;
10838 xassert (interrupt_input_blocked);
10840 /* Set new_cursor_type to the cursor we want to be displayed. In a
10841 mini-buffer window, we want the cursor only to appear if we are
10842 reading input from this window. For the selected window, we want
10843 the cursor type given by the frame parameter. If explicitly
10844 marked off, draw no cursor. In all other cases, we want a hollow
10845 box cursor. */
10846 new_cursor_width = -1;
10847 if (cursor_in_echo_area
10848 && FRAME_HAS_MINIBUF_P (f)
10849 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10851 if (w == XWINDOW (echo_area_window))
10852 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10853 else
10854 new_cursor_type = HOLLOW_BOX_CURSOR;
10856 else
10858 if (w != XWINDOW (selected_window)
10859 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10861 extern int cursor_in_non_selected_windows;
10863 if (MINI_WINDOW_P (w)
10864 || !cursor_in_non_selected_windows
10865 || NILP (XBUFFER (w->buffer)->cursor_type))
10866 new_cursor_type = NO_CURSOR;
10867 else
10868 new_cursor_type = HOLLOW_BOX_CURSOR;
10870 else if (w->cursor_off_p)
10871 new_cursor_type = NO_CURSOR;
10872 else
10874 struct buffer *b = XBUFFER (w->buffer);
10876 if (EQ (b->cursor_type, Qt))
10877 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10878 else
10879 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10880 &new_cursor_width);
10884 /* If cursor is currently being shown and we don't want it to be or
10885 it is in the wrong place, or the cursor type is not what we want,
10886 erase it. */
10887 if (w->phys_cursor_on_p
10888 && (!on
10889 || w->phys_cursor.x != x
10890 || w->phys_cursor.y != y
10891 || new_cursor_type != w->phys_cursor_type))
10892 x_erase_phys_cursor (w);
10894 /* If the cursor is now invisible and we want it to be visible,
10895 display it. */
10896 if (on && !w->phys_cursor_on_p)
10898 w->phys_cursor_ascent = glyph_row->ascent;
10899 w->phys_cursor_height = glyph_row->height;
10901 /* Set phys_cursor_.* before x_draw_.* is called because some
10902 of them may need the information. */
10903 w->phys_cursor.x = x;
10904 w->phys_cursor.y = glyph_row->y;
10905 w->phys_cursor.hpos = hpos;
10906 w->phys_cursor.vpos = vpos;
10907 w->phys_cursor_type = new_cursor_type;
10908 w->phys_cursor_on_p = 1;
10910 switch (new_cursor_type)
10912 case HOLLOW_BOX_CURSOR:
10913 x_draw_hollow_cursor (w, glyph_row);
10914 break;
10916 case FILLED_BOX_CURSOR:
10917 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10918 break;
10920 case BAR_CURSOR:
10921 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10922 break;
10924 case NO_CURSOR:
10925 break;
10927 default:
10928 abort ();
10931 #ifdef HAVE_X_I18N
10932 if (w == XWINDOW (f->selected_window))
10933 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10934 xic_set_preeditarea (w, x, y);
10935 #endif
10938 #ifndef XFlush
10939 if (updating_frame != f)
10940 XFlush (FRAME_X_DISPLAY (f));
10941 #endif
10945 /* Display the cursor on window W, or clear it. X and Y are window
10946 relative pixel coordinates. HPOS and VPOS are glyph matrix
10947 positions. If W is not the selected window, display a hollow
10948 cursor. ON non-zero means display the cursor at X, Y which
10949 correspond to HPOS, VPOS, otherwise it is cleared. */
10951 void
10952 x_display_cursor (w, on, hpos, vpos, x, y)
10953 struct window *w;
10954 int on, hpos, vpos, x, y;
10956 BLOCK_INPUT;
10957 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10958 UNBLOCK_INPUT;
10962 /* Display the cursor on window W, or clear it, according to ON_P.
10963 Don't change the cursor's position. */
10965 void
10966 x_update_cursor (f, on_p)
10967 struct frame *f;
10969 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10973 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10974 in the window tree rooted at W. */
10976 static void
10977 x_update_cursor_in_window_tree (w, on_p)
10978 struct window *w;
10979 int on_p;
10981 while (w)
10983 if (!NILP (w->hchild))
10984 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10985 else if (!NILP (w->vchild))
10986 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10987 else
10988 x_update_window_cursor (w, on_p);
10990 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10995 /* Switch the display of W's cursor on or off, according to the value
10996 of ON. */
10998 static void
10999 x_update_window_cursor (w, on)
11000 struct window *w;
11001 int on;
11003 /* Don't update cursor in windows whose frame is in the process
11004 of being deleted. */
11005 if (w->current_matrix)
11007 BLOCK_INPUT;
11008 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11009 w->phys_cursor.x, w->phys_cursor.y);
11010 UNBLOCK_INPUT;
11017 /* Icons. */
11019 /* Refresh bitmap kitchen sink icon for frame F
11020 when we get an expose event for it. */
11022 void
11023 refreshicon (f)
11024 struct frame *f;
11026 /* Normally, the window manager handles this function. */
11029 /* Make the x-window of frame F use the gnu icon bitmap. */
11032 x_bitmap_icon (f, file)
11033 struct frame *f;
11034 Lisp_Object file;
11036 int bitmap_id;
11038 if (FRAME_X_WINDOW (f) == 0)
11039 return 1;
11041 /* Free up our existing icon bitmap if any. */
11042 if (f->output_data.x->icon_bitmap > 0)
11043 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11044 f->output_data.x->icon_bitmap = 0;
11046 if (STRINGP (file))
11047 bitmap_id = x_create_bitmap_from_file (f, file);
11048 else
11050 /* Create the GNU bitmap if necessary. */
11051 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11052 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11053 = x_create_bitmap_from_data (f, gnu_bits,
11054 gnu_width, gnu_height);
11056 /* The first time we create the GNU bitmap,
11057 this increments the ref-count one extra time.
11058 As a result, the GNU bitmap is never freed.
11059 That way, we don't have to worry about allocating it again. */
11060 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11062 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11065 x_wm_set_icon_pixmap (f, bitmap_id);
11066 f->output_data.x->icon_bitmap = bitmap_id;
11068 return 0;
11072 /* Make the x-window of frame F use a rectangle with text.
11073 Use ICON_NAME as the text. */
11076 x_text_icon (f, icon_name)
11077 struct frame *f;
11078 char *icon_name;
11080 if (FRAME_X_WINDOW (f) == 0)
11081 return 1;
11083 #ifdef HAVE_X11R4
11085 XTextProperty text;
11086 text.value = (unsigned char *) icon_name;
11087 text.encoding = XA_STRING;
11088 text.format = 8;
11089 text.nitems = strlen (icon_name);
11090 #ifdef USE_X_TOOLKIT
11091 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11092 &text);
11093 #else /* not USE_X_TOOLKIT */
11094 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11095 #endif /* not USE_X_TOOLKIT */
11097 #else /* not HAVE_X11R4 */
11098 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11099 #endif /* not HAVE_X11R4 */
11101 if (f->output_data.x->icon_bitmap > 0)
11102 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11103 f->output_data.x->icon_bitmap = 0;
11104 x_wm_set_icon_pixmap (f, 0);
11106 return 0;
11109 #define X_ERROR_MESSAGE_SIZE 200
11111 /* If non-nil, this should be a string.
11112 It means catch X errors and store the error message in this string. */
11114 static Lisp_Object x_error_message_string;
11116 /* An X error handler which stores the error message in
11117 x_error_message_string. This is called from x_error_handler if
11118 x_catch_errors is in effect. */
11120 static void
11121 x_error_catcher (display, error)
11122 Display *display;
11123 XErrorEvent *error;
11125 XGetErrorText (display, error->error_code,
11126 XSTRING (x_error_message_string)->data,
11127 X_ERROR_MESSAGE_SIZE);
11130 /* Begin trapping X errors for display DPY. Actually we trap X errors
11131 for all displays, but DPY should be the display you are actually
11132 operating on.
11134 After calling this function, X protocol errors no longer cause
11135 Emacs to exit; instead, they are recorded in the string
11136 stored in x_error_message_string.
11138 Calling x_check_errors signals an Emacs error if an X error has
11139 occurred since the last call to x_catch_errors or x_check_errors.
11141 Calling x_uncatch_errors resumes the normal error handling. */
11143 void x_check_errors ();
11144 static Lisp_Object x_catch_errors_unwind ();
11147 x_catch_errors (dpy)
11148 Display *dpy;
11150 int count = specpdl_ptr - specpdl;
11152 /* Make sure any errors from previous requests have been dealt with. */
11153 XSync (dpy, False);
11155 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11157 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11158 XSTRING (x_error_message_string)->data[0] = 0;
11160 return count;
11163 /* Unbind the binding that we made to check for X errors. */
11165 static Lisp_Object
11166 x_catch_errors_unwind (old_val)
11167 Lisp_Object old_val;
11169 x_error_message_string = old_val;
11170 return Qnil;
11173 /* If any X protocol errors have arrived since the last call to
11174 x_catch_errors or x_check_errors, signal an Emacs error using
11175 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11177 void
11178 x_check_errors (dpy, format)
11179 Display *dpy;
11180 char *format;
11182 /* Make sure to catch any errors incurred so far. */
11183 XSync (dpy, False);
11185 if (XSTRING (x_error_message_string)->data[0])
11186 error (format, XSTRING (x_error_message_string)->data);
11189 /* Nonzero if we had any X protocol errors
11190 since we did x_catch_errors on DPY. */
11193 x_had_errors_p (dpy)
11194 Display *dpy;
11196 /* Make sure to catch any errors incurred so far. */
11197 XSync (dpy, False);
11199 return XSTRING (x_error_message_string)->data[0] != 0;
11202 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11204 void
11205 x_clear_errors (dpy)
11206 Display *dpy;
11208 XSTRING (x_error_message_string)->data[0] = 0;
11211 /* Stop catching X protocol errors and let them make Emacs die.
11212 DPY should be the display that was passed to x_catch_errors.
11213 COUNT should be the value that was returned by
11214 the corresponding call to x_catch_errors. */
11216 void
11217 x_uncatch_errors (dpy, count)
11218 Display *dpy;
11219 int count;
11221 unbind_to (count, Qnil);
11224 #if 0
11225 static unsigned int x_wire_count;
11226 x_trace_wire ()
11228 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11230 #endif /* ! 0 */
11233 /* Handle SIGPIPE, which can happen when the connection to a server
11234 simply goes away. SIGPIPE is handled by x_connection_signal.
11235 Don't need to do anything, because the write which caused the
11236 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11237 which will do the appropriate cleanup for us. */
11239 static SIGTYPE
11240 x_connection_signal (signalnum) /* If we don't have an argument, */
11241 int signalnum; /* some compilers complain in signal calls. */
11243 #ifdef USG
11244 /* USG systems forget handlers when they are used;
11245 must reestablish each time */
11246 signal (signalnum, x_connection_signal);
11247 #endif /* USG */
11251 /************************************************************************
11252 Handling X errors
11253 ************************************************************************/
11255 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
11256 the text of an error message that lead to the connection loss. */
11258 static SIGTYPE
11259 x_connection_closed (dpy, error_message)
11260 Display *dpy;
11261 char *error_message;
11263 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
11264 Lisp_Object frame, tail;
11265 int count;
11266 char *msg;
11268 msg = (char *) alloca (strlen (error_message) + 1);
11269 strcpy (msg, error_message);
11270 handling_signal = 0;
11272 /* Prevent being called recursively because of an error condition
11273 below. Otherwise, we might end up with printing ``can't find per
11274 display information'' in the recursive call instead of printing
11275 the original message here. */
11276 count = x_catch_errors (dpy);
11278 /* We have to close the display to inform Xt that it doesn't
11279 exist anymore. If we don't, Xt will continue to wait for
11280 events from the display. As a consequence, a sequence of
11282 M-x make-frame-on-display RET :1 RET
11283 ...kill the new frame, so that we get an IO error...
11284 M-x make-frame-on-display RET :1 RET
11286 will indefinitely wait in Xt for events for display `:1', opened
11287 in the first class to make-frame-on-display.
11289 Closing the display is reported to lead to a bus error on
11290 OpenWindows in certain situations. I suspect that is a bug
11291 in OpenWindows. I don't know how to cicumvent it here. */
11293 #ifdef USE_X_TOOLKIT
11294 /* If DPYINFO is null, this means we didn't open the display
11295 in the first place, so don't try to close it. */
11296 if (dpyinfo)
11297 XtCloseDisplay (dpy);
11298 #endif
11300 /* Indicate that this display is dead. */
11301 if (dpyinfo)
11302 dpyinfo->display = 0;
11304 /* First delete frames whose mini-buffers are on frames
11305 that are on the dead display. */
11306 FOR_EACH_FRAME (tail, frame)
11308 Lisp_Object minibuf_frame;
11309 minibuf_frame
11310 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11311 if (FRAME_X_P (XFRAME (frame))
11312 && FRAME_X_P (XFRAME (minibuf_frame))
11313 && ! EQ (frame, minibuf_frame)
11314 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11315 Fdelete_frame (frame, Qt);
11318 /* Now delete all remaining frames on the dead display.
11319 We are now sure none of these is used as the mini-buffer
11320 for another frame that we need to delete. */
11321 FOR_EACH_FRAME (tail, frame)
11322 if (FRAME_X_P (XFRAME (frame))
11323 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11325 /* Set this to t so that Fdelete_frame won't get confused
11326 trying to find a replacement. */
11327 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11328 Fdelete_frame (frame, Qt);
11331 if (dpyinfo)
11332 x_delete_display (dpyinfo);
11334 x_uncatch_errors (dpy, count);
11336 if (x_display_list == 0)
11338 fprintf (stderr, "%s\n", msg);
11339 shut_down_emacs (0, 0, Qnil);
11340 exit (70);
11343 /* Ordinary stack unwind doesn't deal with these. */
11344 #ifdef SIGIO
11345 sigunblock (sigmask (SIGIO));
11346 #endif
11347 sigunblock (sigmask (SIGALRM));
11348 TOTALLY_UNBLOCK_INPUT;
11350 clear_waiting_for_input ();
11351 error ("%s", msg);
11355 /* This is the usual handler for X protocol errors.
11356 It kills all frames on the display that we got the error for.
11357 If that was the only one, it prints an error message and kills Emacs. */
11359 static void
11360 x_error_quitter (display, error)
11361 Display *display;
11362 XErrorEvent *error;
11364 char buf[256], buf1[356];
11366 /* Note that there is no real way portable across R3/R4 to get the
11367 original error handler. */
11369 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11370 sprintf (buf1, "X protocol error: %s on protocol request %d",
11371 buf, error->request_code);
11372 x_connection_closed (display, buf1);
11376 /* This is the first-level handler for X protocol errors.
11377 It calls x_error_quitter or x_error_catcher. */
11379 static int
11380 x_error_handler (display, error)
11381 Display *display;
11382 XErrorEvent *error;
11384 if (! NILP (x_error_message_string))
11385 x_error_catcher (display, error);
11386 else
11387 x_error_quitter (display, error);
11388 return 0;
11391 /* This is the handler for X IO errors, always.
11392 It kills all frames on the display that we lost touch with.
11393 If that was the only one, it prints an error message and kills Emacs. */
11395 static int
11396 x_io_error_quitter (display)
11397 Display *display;
11399 char buf[256];
11401 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11402 x_connection_closed (display, buf);
11403 return 0;
11406 /* Changing the font of the frame. */
11408 /* Give frame F the font named FONTNAME as its default font, and
11409 return the full name of that font. FONTNAME may be a wildcard
11410 pattern; in that case, we choose some font that fits the pattern.
11411 The return value shows which font we chose. */
11413 Lisp_Object
11414 x_new_font (f, fontname)
11415 struct frame *f;
11416 register char *fontname;
11418 struct font_info *fontp
11419 = FS_LOAD_FONT (f, 0, fontname, -1);
11421 if (!fontp)
11422 return Qnil;
11424 f->output_data.x->font = (XFontStruct *) (fontp->font);
11425 f->output_data.x->baseline_offset = fontp->baseline_offset;
11426 f->output_data.x->fontset = -1;
11428 /* Compute the scroll bar width in character columns. */
11429 if (f->scroll_bar_pixel_width > 0)
11431 int wid = FONT_WIDTH (f->output_data.x->font);
11432 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11434 else
11436 int wid = FONT_WIDTH (f->output_data.x->font);
11437 f->scroll_bar_cols = (14 + wid - 1) / wid;
11440 /* Now make the frame display the given font. */
11441 if (FRAME_X_WINDOW (f) != 0)
11443 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11444 f->output_data.x->font->fid);
11445 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11446 f->output_data.x->font->fid);
11447 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11448 f->output_data.x->font->fid);
11450 frame_update_line_height (f);
11451 x_set_window_size (f, 0, f->width, f->height);
11453 else
11454 /* If we are setting a new frame's font for the first time,
11455 there are no faces yet, so this font's height is the line height. */
11456 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11458 return build_string (fontp->full_name);
11461 /* Give frame F the fontset named FONTSETNAME as its default font, and
11462 return the full name of that fontset. FONTSETNAME may be a wildcard
11463 pattern; in that case, we choose some fontset that fits the pattern.
11464 The return value shows which fontset we chose. */
11466 Lisp_Object
11467 x_new_fontset (f, fontsetname)
11468 struct frame *f;
11469 char *fontsetname;
11471 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11472 Lisp_Object result;
11474 if (fontset < 0)
11475 return Qnil;
11477 if (f->output_data.x->fontset == fontset)
11478 /* This fontset is already set in frame F. There's nothing more
11479 to do. */
11480 return fontset_name (fontset);
11482 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11484 if (!STRINGP (result))
11485 /* Can't load ASCII font. */
11486 return Qnil;
11488 /* Since x_new_font doesn't update any fontset information, do it now. */
11489 f->output_data.x->fontset = fontset;
11491 #ifdef HAVE_X_I18N
11492 if (FRAME_XIC (f)
11493 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11494 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11495 #endif
11497 return build_string (fontsetname);
11501 /***********************************************************************
11502 X Input Methods
11503 ***********************************************************************/
11505 #ifdef HAVE_X_I18N
11507 #ifdef HAVE_X11R6
11509 /* XIM destroy callback function, which is called whenever the
11510 connection to input method XIM dies. CLIENT_DATA contains a
11511 pointer to the x_display_info structure corresponding to XIM. */
11513 static void
11514 xim_destroy_callback (xim, client_data, call_data)
11515 XIM xim;
11516 XPointer client_data;
11517 XPointer call_data;
11519 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11520 Lisp_Object frame, tail;
11522 BLOCK_INPUT;
11524 /* No need to call XDestroyIC.. */
11525 FOR_EACH_FRAME (tail, frame)
11527 struct frame *f = XFRAME (frame);
11528 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11530 FRAME_XIC (f) = NULL;
11531 if (FRAME_XIC_FONTSET (f))
11533 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11534 FRAME_XIC_FONTSET (f) = NULL;
11539 /* No need to call XCloseIM. */
11540 dpyinfo->xim = NULL;
11541 XFree (dpyinfo->xim_styles);
11542 UNBLOCK_INPUT;
11545 #endif /* HAVE_X11R6 */
11547 /* Open the connection to the XIM server on display DPYINFO.
11548 RESOURCE_NAME is the resource name Emacs uses. */
11550 static void
11551 xim_open_dpy (dpyinfo, resource_name)
11552 struct x_display_info *dpyinfo;
11553 char *resource_name;
11555 #ifdef USE_XIM
11556 XIM xim;
11558 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11559 dpyinfo->xim = xim;
11561 if (xim)
11563 #ifdef HAVE_X11R6
11564 XIMCallback destroy;
11565 #endif
11567 /* Get supported styles and XIM values. */
11568 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11570 #ifdef HAVE_X11R6
11571 destroy.callback = xim_destroy_callback;
11572 destroy.client_data = (XPointer)dpyinfo;
11573 /* This isn't prptotyped in OSF 5.0. */
11574 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11575 #endif
11578 #else /* not USE_XIM */
11579 dpyinfo->xim = NULL;
11580 #endif /* not USE_XIM */
11584 #ifdef HAVE_X11R6_XIM
11586 struct xim_inst_t
11588 struct x_display_info *dpyinfo;
11589 char *resource_name;
11592 /* XIM instantiate callback function, which is called whenever an XIM
11593 server is available. DISPLAY is teh display of the XIM.
11594 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11595 when the callback was registered. */
11597 static void
11598 xim_instantiate_callback (display, client_data, call_data)
11599 Display *display;
11600 XPointer client_data;
11601 XPointer call_data;
11603 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11604 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11606 /* We don't support multiple XIM connections. */
11607 if (dpyinfo->xim)
11608 return;
11610 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11612 /* Create XIC for the existing frames on the same display, as long
11613 as they have no XIC. */
11614 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11616 Lisp_Object tail, frame;
11618 BLOCK_INPUT;
11619 FOR_EACH_FRAME (tail, frame)
11621 struct frame *f = XFRAME (frame);
11623 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11624 if (FRAME_XIC (f) == NULL)
11626 create_frame_xic (f);
11627 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11628 xic_set_statusarea (f);
11629 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11631 struct window *w = XWINDOW (f->selected_window);
11632 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11637 UNBLOCK_INPUT;
11641 #endif /* HAVE_X11R6_XIM */
11644 /* Open a connection to the XIM server on display DPYINFO.
11645 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11646 connection only at the first time. On X11R6, open the connection
11647 in the XIM instantiate callback function. */
11649 static void
11650 xim_initialize (dpyinfo, resource_name)
11651 struct x_display_info *dpyinfo;
11652 char *resource_name;
11654 #ifdef USE_XIM
11655 #ifdef HAVE_X11R6_XIM
11656 struct xim_inst_t *xim_inst;
11657 int len;
11659 dpyinfo->xim = NULL;
11660 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11661 xim_inst->dpyinfo = dpyinfo;
11662 len = strlen (resource_name);
11663 xim_inst->resource_name = (char *) xmalloc (len + 1);
11664 bcopy (resource_name, xim_inst->resource_name, len + 1);
11665 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11666 resource_name, EMACS_CLASS,
11667 xim_instantiate_callback,
11668 /* Fixme: This is XPointer in
11669 XFree86 but (XPointer *) on
11670 Tru64, at least. */
11671 (XPointer) xim_inst);
11672 #else /* not HAVE_X11R6_XIM */
11673 dpyinfo->xim = NULL;
11674 xim_open_dpy (dpyinfo, resource_name);
11675 #endif /* not HAVE_X11R6_XIM */
11677 #else /* not USE_XIM */
11678 dpyinfo->xim = NULL;
11679 #endif /* not USE_XIM */
11683 /* Close the connection to the XIM server on display DPYINFO. */
11685 static void
11686 xim_close_dpy (dpyinfo)
11687 struct x_display_info *dpyinfo;
11689 #ifdef USE_XIM
11690 #ifdef HAVE_X11R6_XIM
11691 if (dpyinfo->display)
11692 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11693 NULL, EMACS_CLASS,
11694 xim_instantiate_callback, NULL);
11695 #endif /* not HAVE_X11R6_XIM */
11696 if (dpyinfo->display)
11697 XCloseIM (dpyinfo->xim);
11698 dpyinfo->xim = NULL;
11699 XFree (dpyinfo->xim_styles);
11700 #endif /* USE_XIM */
11703 #endif /* not HAVE_X11R6_XIM */
11707 /* Calculate the absolute position in frame F
11708 from its current recorded position values and gravity. */
11710 void
11711 x_calc_absolute_position (f)
11712 struct frame *f;
11714 Window child;
11715 int win_x = 0, win_y = 0;
11716 int flags = f->output_data.x->size_hint_flags;
11717 int this_window;
11719 /* We have nothing to do if the current position
11720 is already for the top-left corner. */
11721 if (! ((flags & XNegative) || (flags & YNegative)))
11722 return;
11724 #ifdef USE_X_TOOLKIT
11725 this_window = XtWindow (f->output_data.x->widget);
11726 #else
11727 this_window = FRAME_X_WINDOW (f);
11728 #endif
11730 /* Find the position of the outside upper-left corner of
11731 the inner window, with respect to the outer window.
11732 But do this only if we will need the results. */
11733 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11735 int count;
11737 BLOCK_INPUT;
11738 count = x_catch_errors (FRAME_X_DISPLAY (f));
11739 while (1)
11741 x_clear_errors (FRAME_X_DISPLAY (f));
11742 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11744 /* From-window, to-window. */
11745 this_window,
11746 f->output_data.x->parent_desc,
11748 /* From-position, to-position. */
11749 0, 0, &win_x, &win_y,
11751 /* Child of win. */
11752 &child);
11753 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11755 Window newroot, newparent = 0xdeadbeef;
11756 Window *newchildren;
11757 unsigned int nchildren;
11759 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11760 &newparent, &newchildren, &nchildren))
11761 break;
11763 XFree ((char *) newchildren);
11765 f->output_data.x->parent_desc = newparent;
11767 else
11768 break;
11771 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11772 UNBLOCK_INPUT;
11775 /* Treat negative positions as relative to the leftmost bottommost
11776 position that fits on the screen. */
11777 if (flags & XNegative)
11778 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11779 - 2 * f->output_data.x->border_width - win_x
11780 - PIXEL_WIDTH (f)
11781 + f->output_data.x->left_pos);
11784 int height = PIXEL_HEIGHT (f);
11786 #if defined USE_X_TOOLKIT && defined USE_MOTIF
11787 /* Something is fishy here. When using Motif, starting Emacs with
11788 `-g -0-0', the frame appears too low by a few pixels.
11790 This seems to be so because initially, while Emacs is starting,
11791 the column widget's height and the frame's pixel height are
11792 different. The column widget's height is the right one. In
11793 later invocations, when Emacs is up, the frame's pixel height
11794 is right, though.
11796 It's not obvious where the initial small difference comes from.
11797 2000-12-01, gerd. */
11799 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
11800 #endif
11802 if (flags & YNegative)
11803 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11804 - 2 * f->output_data.x->border_width
11805 - win_y
11806 - height
11807 + f->output_data.x->top_pos);
11810 /* The left_pos and top_pos
11811 are now relative to the top and left screen edges,
11812 so the flags should correspond. */
11813 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11816 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11817 to really change the position, and 0 when calling from
11818 x_make_frame_visible (in that case, XOFF and YOFF are the current
11819 position values). It is -1 when calling from x_set_frame_parameters,
11820 which means, do adjust for borders but don't change the gravity. */
11822 void
11823 x_set_offset (f, xoff, yoff, change_gravity)
11824 struct frame *f;
11825 register int xoff, yoff;
11826 int change_gravity;
11828 int modified_top, modified_left;
11830 if (change_gravity > 0)
11832 f->output_data.x->top_pos = yoff;
11833 f->output_data.x->left_pos = xoff;
11834 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11835 if (xoff < 0)
11836 f->output_data.x->size_hint_flags |= XNegative;
11837 if (yoff < 0)
11838 f->output_data.x->size_hint_flags |= YNegative;
11839 f->output_data.x->win_gravity = NorthWestGravity;
11841 x_calc_absolute_position (f);
11843 BLOCK_INPUT;
11844 x_wm_set_size_hint (f, (long) 0, 0);
11846 modified_left = f->output_data.x->left_pos;
11847 modified_top = f->output_data.x->top_pos;
11848 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11849 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11850 /* It is a mystery why we need to add the border_width here
11851 when the frame is already visible, but experiment says we do. */
11852 if (change_gravity != 0)
11854 modified_left += f->output_data.x->border_width;
11855 modified_top += f->output_data.x->border_width;
11857 #endif
11859 #ifdef USE_X_TOOLKIT
11860 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11861 modified_left, modified_top);
11862 #else /* not USE_X_TOOLKIT */
11863 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11864 modified_left, modified_top);
11865 #endif /* not USE_X_TOOLKIT */
11866 UNBLOCK_INPUT;
11869 /* Call this to change the size of frame F's x-window.
11870 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11871 for this size change and subsequent size changes.
11872 Otherwise we leave the window gravity unchanged. */
11874 void
11875 x_set_window_size (f, change_gravity, cols, rows)
11876 struct frame *f;
11877 int change_gravity;
11878 int cols, rows;
11880 #ifndef USE_X_TOOLKIT
11881 int pixelwidth, pixelheight;
11882 #endif
11884 BLOCK_INPUT;
11886 #ifdef USE_X_TOOLKIT
11888 /* The x and y position of the widget is clobbered by the
11889 call to XtSetValues within EmacsFrameSetCharSize.
11890 This is a real kludge, but I don't understand Xt so I can't
11891 figure out a correct fix. Can anyone else tell me? -- rms. */
11892 int xpos = f->output_data.x->widget->core.x;
11893 int ypos = f->output_data.x->widget->core.y;
11894 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11895 f->output_data.x->widget->core.x = xpos;
11896 f->output_data.x->widget->core.y = ypos;
11899 #else /* not USE_X_TOOLKIT */
11901 check_frame_size (f, &rows, &cols);
11902 f->output_data.x->vertical_scroll_bar_extra
11903 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11905 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11906 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11907 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11908 f->output_data.x->flags_areas_extra
11909 = FRAME_FLAGS_AREA_WIDTH (f);
11910 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11911 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11913 f->output_data.x->win_gravity = NorthWestGravity;
11914 x_wm_set_size_hint (f, (long) 0, 0);
11916 XSync (FRAME_X_DISPLAY (f), False);
11917 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11918 pixelwidth, pixelheight);
11920 /* Now, strictly speaking, we can't be sure that this is accurate,
11921 but the window manager will get around to dealing with the size
11922 change request eventually, and we'll hear how it went when the
11923 ConfigureNotify event gets here.
11925 We could just not bother storing any of this information here,
11926 and let the ConfigureNotify event set everything up, but that
11927 might be kind of confusing to the Lisp code, since size changes
11928 wouldn't be reported in the frame parameters until some random
11929 point in the future when the ConfigureNotify event arrives.
11931 We pass 1 for DELAY since we can't run Lisp code inside of
11932 a BLOCK_INPUT. */
11933 change_frame_size (f, rows, cols, 0, 1, 0);
11934 PIXEL_WIDTH (f) = pixelwidth;
11935 PIXEL_HEIGHT (f) = pixelheight;
11937 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11938 receive in the ConfigureNotify event; if we get what we asked
11939 for, then the event won't cause the screen to become garbaged, so
11940 we have to make sure to do it here. */
11941 SET_FRAME_GARBAGED (f);
11943 XFlush (FRAME_X_DISPLAY (f));
11945 #endif /* not USE_X_TOOLKIT */
11947 /* If cursor was outside the new size, mark it as off. */
11948 mark_window_cursors_off (XWINDOW (f->root_window));
11950 /* Clear out any recollection of where the mouse highlighting was,
11951 since it might be in a place that's outside the new frame size.
11952 Actually checking whether it is outside is a pain in the neck,
11953 so don't try--just let the highlighting be done afresh with new size. */
11954 cancel_mouse_face (f);
11956 UNBLOCK_INPUT;
11959 /* Mouse warping. */
11961 void
11962 x_set_mouse_position (f, x, y)
11963 struct frame *f;
11964 int x, y;
11966 int pix_x, pix_y;
11968 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11969 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11971 if (pix_x < 0) pix_x = 0;
11972 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11974 if (pix_y < 0) pix_y = 0;
11975 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11977 BLOCK_INPUT;
11979 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11980 0, 0, 0, 0, pix_x, pix_y);
11981 UNBLOCK_INPUT;
11984 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11986 void
11987 x_set_mouse_pixel_position (f, pix_x, pix_y)
11988 struct frame *f;
11989 int pix_x, pix_y;
11991 BLOCK_INPUT;
11993 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11994 0, 0, 0, 0, pix_x, pix_y);
11995 UNBLOCK_INPUT;
11998 /* focus shifting, raising and lowering. */
12000 void
12001 x_focus_on_frame (f)
12002 struct frame *f;
12004 #if 0 /* This proves to be unpleasant. */
12005 x_raise_frame (f);
12006 #endif
12007 #if 0
12008 /* I don't think that the ICCCM allows programs to do things like this
12009 without the interaction of the window manager. Whatever you end up
12010 doing with this code, do it to x_unfocus_frame too. */
12011 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12012 RevertToPointerRoot, CurrentTime);
12013 #endif /* ! 0 */
12016 void
12017 x_unfocus_frame (f)
12018 struct frame *f;
12020 #if 0
12021 /* Look at the remarks in x_focus_on_frame. */
12022 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12023 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12024 RevertToPointerRoot, CurrentTime);
12025 #endif /* ! 0 */
12028 /* Raise frame F. */
12030 void
12031 x_raise_frame (f)
12032 struct frame *f;
12034 if (f->async_visible)
12036 BLOCK_INPUT;
12037 #ifdef USE_X_TOOLKIT
12038 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12039 #else /* not USE_X_TOOLKIT */
12040 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12041 #endif /* not USE_X_TOOLKIT */
12042 XFlush (FRAME_X_DISPLAY (f));
12043 UNBLOCK_INPUT;
12047 /* Lower frame F. */
12049 void
12050 x_lower_frame (f)
12051 struct frame *f;
12053 if (f->async_visible)
12055 BLOCK_INPUT;
12056 #ifdef USE_X_TOOLKIT
12057 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12058 #else /* not USE_X_TOOLKIT */
12059 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12060 #endif /* not USE_X_TOOLKIT */
12061 XFlush (FRAME_X_DISPLAY (f));
12062 UNBLOCK_INPUT;
12066 static void
12067 XTframe_raise_lower (f, raise_flag)
12068 FRAME_PTR f;
12069 int raise_flag;
12071 if (raise_flag)
12072 x_raise_frame (f);
12073 else
12074 x_lower_frame (f);
12077 /* Change of visibility. */
12079 /* This tries to wait until the frame is really visible.
12080 However, if the window manager asks the user where to position
12081 the frame, this will return before the user finishes doing that.
12082 The frame will not actually be visible at that time,
12083 but it will become visible later when the window manager
12084 finishes with it. */
12086 void
12087 x_make_frame_visible (f)
12088 struct frame *f;
12090 Lisp_Object type;
12091 int original_top, original_left;
12092 int retry_count = 2;
12094 retry:
12096 BLOCK_INPUT;
12098 type = x_icon_type (f);
12099 if (!NILP (type))
12100 x_bitmap_icon (f, type);
12102 if (! FRAME_VISIBLE_P (f))
12104 /* We test FRAME_GARBAGED_P here to make sure we don't
12105 call x_set_offset a second time
12106 if we get to x_make_frame_visible a second time
12107 before the window gets really visible. */
12108 if (! FRAME_ICONIFIED_P (f)
12109 && ! f->output_data.x->asked_for_visible)
12110 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12112 f->output_data.x->asked_for_visible = 1;
12114 if (! EQ (Vx_no_window_manager, Qt))
12115 x_wm_set_window_state (f, NormalState);
12116 #ifdef USE_X_TOOLKIT
12117 /* This was XtPopup, but that did nothing for an iconified frame. */
12118 XtMapWidget (f->output_data.x->widget);
12119 #else /* not USE_X_TOOLKIT */
12120 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12121 #endif /* not USE_X_TOOLKIT */
12122 #if 0 /* This seems to bring back scroll bars in the wrong places
12123 if the window configuration has changed. They seem
12124 to come back ok without this. */
12125 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12126 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12127 #endif
12130 XFlush (FRAME_X_DISPLAY (f));
12132 /* Synchronize to ensure Emacs knows the frame is visible
12133 before we do anything else. We do this loop with input not blocked
12134 so that incoming events are handled. */
12136 Lisp_Object frame;
12137 int count;
12138 /* This must be before UNBLOCK_INPUT
12139 since events that arrive in response to the actions above
12140 will set it when they are handled. */
12141 int previously_visible = f->output_data.x->has_been_visible;
12143 original_left = f->output_data.x->left_pos;
12144 original_top = f->output_data.x->top_pos;
12146 /* This must come after we set COUNT. */
12147 UNBLOCK_INPUT;
12149 /* We unblock here so that arriving X events are processed. */
12151 /* Now move the window back to where it was "supposed to be".
12152 But don't do it if the gravity is negative.
12153 When the gravity is negative, this uses a position
12154 that is 3 pixels too low. Perhaps that's really the border width.
12156 Don't do this if the window has never been visible before,
12157 because the window manager may choose the position
12158 and we don't want to override it. */
12160 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12161 && f->output_data.x->win_gravity == NorthWestGravity
12162 && previously_visible)
12164 Drawable rootw;
12165 int x, y;
12166 unsigned int width, height, border, depth;
12168 BLOCK_INPUT;
12170 /* On some window managers (such as FVWM) moving an existing
12171 window, even to the same place, causes the window manager
12172 to introduce an offset. This can cause the window to move
12173 to an unexpected location. Check the geometry (a little
12174 slow here) and then verify that the window is in the right
12175 place. If the window is not in the right place, move it
12176 there, and take the potential window manager hit. */
12177 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12178 &rootw, &x, &y, &width, &height, &border, &depth);
12180 if (original_left != x || original_top != y)
12181 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12182 original_left, original_top);
12184 UNBLOCK_INPUT;
12187 XSETFRAME (frame, f);
12189 /* Wait until the frame is visible. Process X events until a
12190 MapNotify event has been seen, or until we think we won't get a
12191 MapNotify at all.. */
12192 for (count = input_signal_count + 10;
12193 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12195 /* Force processing of queued events. */
12196 x_sync (f);
12198 /* Machines that do polling rather than SIGIO have been
12199 observed to go into a busy-wait here. So we'll fake an
12200 alarm signal to let the handler know that there's something
12201 to be read. We used to raise a real alarm, but it seems
12202 that the handler isn't always enabled here. This is
12203 probably a bug. */
12204 if (input_polling_used ())
12206 /* It could be confusing if a real alarm arrives while
12207 processing the fake one. Turn it off and let the
12208 handler reset it. */
12209 extern void poll_for_input_1 P_ ((void));
12210 int old_poll_suppress_count = poll_suppress_count;
12211 poll_suppress_count = 1;
12212 poll_for_input_1 ();
12213 poll_suppress_count = old_poll_suppress_count;
12216 /* See if a MapNotify event has been processed. */
12217 FRAME_SAMPLE_VISIBILITY (f);
12220 /* 2000-09-28: In
12222 (let ((f (selected-frame)))
12223 (iconify-frame f)
12224 (raise-frame f))
12226 the frame is not raised with various window managers on
12227 FreeBSD, Linux and Solaris. It turns out that, for some
12228 unknown reason, the call to XtMapWidget is completely ignored.
12229 Mapping the widget a second time works. */
12231 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12232 goto retry;
12236 /* Change from mapped state to withdrawn state. */
12238 /* Make the frame visible (mapped and not iconified). */
12240 void
12241 x_make_frame_invisible (f)
12242 struct frame *f;
12244 Window window;
12246 #ifdef USE_X_TOOLKIT
12247 /* Use the frame's outermost window, not the one we normally draw on. */
12248 window = XtWindow (f->output_data.x->widget);
12249 #else /* not USE_X_TOOLKIT */
12250 window = FRAME_X_WINDOW (f);
12251 #endif /* not USE_X_TOOLKIT */
12253 /* Don't keep the highlight on an invisible frame. */
12254 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12255 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12257 #if 0/* This might add unreliability; I don't trust it -- rms. */
12258 if (! f->async_visible && ! f->async_iconified)
12259 return;
12260 #endif
12262 BLOCK_INPUT;
12264 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12265 that the current position of the window is user-specified, rather than
12266 program-specified, so that when the window is mapped again, it will be
12267 placed at the same location, without forcing the user to position it
12268 by hand again (they have already done that once for this window.) */
12269 x_wm_set_size_hint (f, (long) 0, 1);
12271 #ifdef HAVE_X11R4
12273 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12274 DefaultScreen (FRAME_X_DISPLAY (f))))
12276 UNBLOCK_INPUT_RESIGNAL;
12277 error ("Can't notify window manager of window withdrawal");
12279 #else /* ! defined (HAVE_X11R4) */
12281 /* Tell the window manager what we're going to do. */
12282 if (! EQ (Vx_no_window_manager, Qt))
12284 XEvent unmap;
12286 unmap.xunmap.type = UnmapNotify;
12287 unmap.xunmap.window = window;
12288 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12289 unmap.xunmap.from_configure = False;
12290 if (! XSendEvent (FRAME_X_DISPLAY (f),
12291 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12292 False,
12293 SubstructureRedirectMaskSubstructureNotifyMask,
12294 &unmap))
12296 UNBLOCK_INPUT_RESIGNAL;
12297 error ("Can't notify window manager of withdrawal");
12301 /* Unmap the window ourselves. Cheeky! */
12302 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12303 #endif /* ! defined (HAVE_X11R4) */
12305 /* We can't distinguish this from iconification
12306 just by the event that we get from the server.
12307 So we can't win using the usual strategy of letting
12308 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12309 and synchronize with the server to make sure we agree. */
12310 f->visible = 0;
12311 FRAME_ICONIFIED_P (f) = 0;
12312 f->async_visible = 0;
12313 f->async_iconified = 0;
12315 x_sync (f);
12317 UNBLOCK_INPUT;
12320 /* Change window state from mapped to iconified. */
12322 void
12323 x_iconify_frame (f)
12324 struct frame *f;
12326 int result;
12327 Lisp_Object type;
12329 /* Don't keep the highlight on an invisible frame. */
12330 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12331 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12333 if (f->async_iconified)
12334 return;
12336 BLOCK_INPUT;
12338 FRAME_SAMPLE_VISIBILITY (f);
12340 type = x_icon_type (f);
12341 if (!NILP (type))
12342 x_bitmap_icon (f, type);
12344 #ifdef USE_X_TOOLKIT
12346 if (! FRAME_VISIBLE_P (f))
12348 if (! EQ (Vx_no_window_manager, Qt))
12349 x_wm_set_window_state (f, IconicState);
12350 /* This was XtPopup, but that did nothing for an iconified frame. */
12351 XtMapWidget (f->output_data.x->widget);
12352 /* The server won't give us any event to indicate
12353 that an invisible frame was changed to an icon,
12354 so we have to record it here. */
12355 f->iconified = 1;
12356 f->visible = 1;
12357 f->async_iconified = 1;
12358 f->async_visible = 0;
12359 UNBLOCK_INPUT;
12360 return;
12363 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12364 XtWindow (f->output_data.x->widget),
12365 DefaultScreen (FRAME_X_DISPLAY (f)));
12366 UNBLOCK_INPUT;
12368 if (!result)
12369 error ("Can't notify window manager of iconification");
12371 f->async_iconified = 1;
12372 f->async_visible = 0;
12375 BLOCK_INPUT;
12376 XFlush (FRAME_X_DISPLAY (f));
12377 UNBLOCK_INPUT;
12378 #else /* not USE_X_TOOLKIT */
12380 /* Make sure the X server knows where the window should be positioned,
12381 in case the user deiconifies with the window manager. */
12382 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12383 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12385 /* Since we don't know which revision of X we're running, we'll use both
12386 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12388 /* X11R4: send a ClientMessage to the window manager using the
12389 WM_CHANGE_STATE type. */
12391 XEvent message;
12393 message.xclient.window = FRAME_X_WINDOW (f);
12394 message.xclient.type = ClientMessage;
12395 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12396 message.xclient.format = 32;
12397 message.xclient.data.l[0] = IconicState;
12399 if (! XSendEvent (FRAME_X_DISPLAY (f),
12400 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12401 False,
12402 SubstructureRedirectMask | SubstructureNotifyMask,
12403 &message))
12405 UNBLOCK_INPUT_RESIGNAL;
12406 error ("Can't notify window manager of iconification");
12410 /* X11R3: set the initial_state field of the window manager hints to
12411 IconicState. */
12412 x_wm_set_window_state (f, IconicState);
12414 if (!FRAME_VISIBLE_P (f))
12416 /* If the frame was withdrawn, before, we must map it. */
12417 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12420 f->async_iconified = 1;
12421 f->async_visible = 0;
12423 XFlush (FRAME_X_DISPLAY (f));
12424 UNBLOCK_INPUT;
12425 #endif /* not USE_X_TOOLKIT */
12429 /* Free X resources of frame F. */
12431 void
12432 x_free_frame_resources (f)
12433 struct frame *f;
12435 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12437 BLOCK_INPUT;
12439 /* If a display connection is dead, don't try sending more
12440 commands to the X server. */
12441 if (dpyinfo->display)
12443 if (f->output_data.x->icon_desc)
12444 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12446 #ifdef HAVE_X_I18N
12447 if (FRAME_XIC (f))
12448 free_frame_xic (f);
12449 #endif
12451 if (FRAME_X_WINDOW (f))
12452 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12454 #ifdef USE_X_TOOLKIT
12455 if (f->output_data.x->widget)
12456 XtDestroyWidget (f->output_data.x->widget);
12457 free_frame_menubar (f);
12458 #endif /* USE_X_TOOLKIT */
12460 unload_color (f, f->output_data.x->foreground_pixel);
12461 unload_color (f, f->output_data.x->background_pixel);
12462 unload_color (f, f->output_data.x->cursor_pixel);
12463 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12464 unload_color (f, f->output_data.x->border_pixel);
12465 unload_color (f, f->output_data.x->mouse_pixel);
12467 if (f->output_data.x->scroll_bar_background_pixel != -1)
12468 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12469 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12470 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12471 if (f->output_data.x->white_relief.allocated_p)
12472 unload_color (f, f->output_data.x->white_relief.pixel);
12473 if (f->output_data.x->black_relief.allocated_p)
12474 unload_color (f, f->output_data.x->black_relief.pixel);
12476 if (FRAME_FACE_CACHE (f))
12477 free_frame_faces (f);
12479 x_free_gcs (f);
12480 XFlush (FRAME_X_DISPLAY (f));
12483 if (f->output_data.x->saved_menu_event)
12484 xfree (f->output_data.x->saved_menu_event);
12486 xfree (f->output_data.x);
12487 f->output_data.x = NULL;
12489 if (f == dpyinfo->x_focus_frame)
12490 dpyinfo->x_focus_frame = 0;
12491 if (f == dpyinfo->x_focus_event_frame)
12492 dpyinfo->x_focus_event_frame = 0;
12493 if (f == dpyinfo->x_highlight_frame)
12494 dpyinfo->x_highlight_frame = 0;
12496 if (f == dpyinfo->mouse_face_mouse_frame)
12498 dpyinfo->mouse_face_beg_row
12499 = dpyinfo->mouse_face_beg_col = -1;
12500 dpyinfo->mouse_face_end_row
12501 = dpyinfo->mouse_face_end_col = -1;
12502 dpyinfo->mouse_face_window = Qnil;
12503 dpyinfo->mouse_face_deferred_gc = 0;
12504 dpyinfo->mouse_face_mouse_frame = 0;
12507 UNBLOCK_INPUT;
12511 /* Destroy the X window of frame F. */
12513 void
12514 x_destroy_window (f)
12515 struct frame *f;
12517 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12519 /* If a display connection is dead, don't try sending more
12520 commands to the X server. */
12521 if (dpyinfo->display != 0)
12522 x_free_frame_resources (f);
12524 dpyinfo->reference_count--;
12528 /* Setting window manager hints. */
12530 /* Set the normal size hints for the window manager, for frame F.
12531 FLAGS is the flags word to use--or 0 meaning preserve the flags
12532 that the window now has.
12533 If USER_POSITION is nonzero, we set the USPosition
12534 flag (this is useful when FLAGS is 0). */
12536 void
12537 x_wm_set_size_hint (f, flags, user_position)
12538 struct frame *f;
12539 long flags;
12540 int user_position;
12542 XSizeHints size_hints;
12544 #ifdef USE_X_TOOLKIT
12545 Arg al[2];
12546 int ac = 0;
12547 Dimension widget_width, widget_height;
12548 Window window = XtWindow (f->output_data.x->widget);
12549 #else /* not USE_X_TOOLKIT */
12550 Window window = FRAME_X_WINDOW (f);
12551 #endif /* not USE_X_TOOLKIT */
12553 /* Setting PMaxSize caused various problems. */
12554 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12556 size_hints.x = f->output_data.x->left_pos;
12557 size_hints.y = f->output_data.x->top_pos;
12559 #ifdef USE_X_TOOLKIT
12560 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12561 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12562 XtGetValues (f->output_data.x->widget, al, ac);
12563 size_hints.height = widget_height;
12564 size_hints.width = widget_width;
12565 #else /* not USE_X_TOOLKIT */
12566 size_hints.height = PIXEL_HEIGHT (f);
12567 size_hints.width = PIXEL_WIDTH (f);
12568 #endif /* not USE_X_TOOLKIT */
12570 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12571 size_hints.height_inc = f->output_data.x->line_height;
12572 size_hints.max_width
12573 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12574 size_hints.max_height
12575 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12577 /* Calculate the base and minimum sizes.
12579 (When we use the X toolkit, we don't do it here.
12580 Instead we copy the values that the widgets are using, below.) */
12581 #ifndef USE_X_TOOLKIT
12583 int base_width, base_height;
12584 int min_rows = 0, min_cols = 0;
12586 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12587 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12589 check_frame_size (f, &min_rows, &min_cols);
12591 /* The window manager uses the base width hints to calculate the
12592 current number of rows and columns in the frame while
12593 resizing; min_width and min_height aren't useful for this
12594 purpose, since they might not give the dimensions for a
12595 zero-row, zero-column frame.
12597 We use the base_width and base_height members if we have
12598 them; otherwise, we set the min_width and min_height members
12599 to the size for a zero x zero frame. */
12601 #ifdef HAVE_X11R4
12602 size_hints.flags |= PBaseSize;
12603 size_hints.base_width = base_width;
12604 size_hints.base_height = base_height;
12605 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12606 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12607 #else
12608 size_hints.min_width = base_width;
12609 size_hints.min_height = base_height;
12610 #endif
12613 /* If we don't need the old flags, we don't need the old hint at all. */
12614 if (flags)
12616 size_hints.flags |= flags;
12617 goto no_read;
12619 #endif /* not USE_X_TOOLKIT */
12622 XSizeHints hints; /* Sometimes I hate X Windows... */
12623 long supplied_return;
12624 int value;
12626 #ifdef HAVE_X11R4
12627 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12628 &supplied_return);
12629 #else
12630 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12631 #endif
12633 #ifdef USE_X_TOOLKIT
12634 size_hints.base_height = hints.base_height;
12635 size_hints.base_width = hints.base_width;
12636 size_hints.min_height = hints.min_height;
12637 size_hints.min_width = hints.min_width;
12638 #endif
12640 if (flags)
12641 size_hints.flags |= flags;
12642 else
12644 if (value == 0)
12645 hints.flags = 0;
12646 if (hints.flags & PSize)
12647 size_hints.flags |= PSize;
12648 if (hints.flags & PPosition)
12649 size_hints.flags |= PPosition;
12650 if (hints.flags & USPosition)
12651 size_hints.flags |= USPosition;
12652 if (hints.flags & USSize)
12653 size_hints.flags |= USSize;
12657 #ifndef USE_X_TOOLKIT
12658 no_read:
12659 #endif
12661 #ifdef PWinGravity
12662 size_hints.win_gravity = f->output_data.x->win_gravity;
12663 size_hints.flags |= PWinGravity;
12665 if (user_position)
12667 size_hints.flags &= ~ PPosition;
12668 size_hints.flags |= USPosition;
12670 #endif /* PWinGravity */
12672 #ifdef HAVE_X11R4
12673 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12674 #else
12675 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12676 #endif
12679 /* Used for IconicState or NormalState */
12681 void
12682 x_wm_set_window_state (f, state)
12683 struct frame *f;
12684 int state;
12686 #ifdef USE_X_TOOLKIT
12687 Arg al[1];
12689 XtSetArg (al[0], XtNinitialState, state);
12690 XtSetValues (f->output_data.x->widget, al, 1);
12691 #else /* not USE_X_TOOLKIT */
12692 Window window = FRAME_X_WINDOW (f);
12694 f->output_data.x->wm_hints.flags |= StateHint;
12695 f->output_data.x->wm_hints.initial_state = state;
12697 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12698 #endif /* not USE_X_TOOLKIT */
12701 void
12702 x_wm_set_icon_pixmap (f, pixmap_id)
12703 struct frame *f;
12704 int pixmap_id;
12706 Pixmap icon_pixmap;
12708 #ifndef USE_X_TOOLKIT
12709 Window window = FRAME_X_WINDOW (f);
12710 #endif
12712 if (pixmap_id > 0)
12714 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12715 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12717 else
12719 /* It seems there is no way to turn off use of an icon pixmap.
12720 The following line does it, only if no icon has yet been created,
12721 for some window managers. But with mwm it crashes.
12722 Some people say it should clear the IconPixmapHint bit in this case,
12723 but that doesn't work, and the X consortium said it isn't the
12724 right thing at all. Since there is no way to win,
12725 best to explicitly give up. */
12726 #if 0
12727 f->output_data.x->wm_hints.icon_pixmap = None;
12728 #else
12729 return;
12730 #endif
12733 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12736 Arg al[1];
12737 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12738 XtSetValues (f->output_data.x->widget, al, 1);
12741 #else /* not USE_X_TOOLKIT */
12743 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12744 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12746 #endif /* not USE_X_TOOLKIT */
12749 void
12750 x_wm_set_icon_position (f, icon_x, icon_y)
12751 struct frame *f;
12752 int icon_x, icon_y;
12754 #ifdef USE_X_TOOLKIT
12755 Window window = XtWindow (f->output_data.x->widget);
12756 #else
12757 Window window = FRAME_X_WINDOW (f);
12758 #endif
12760 f->output_data.x->wm_hints.flags |= IconPositionHint;
12761 f->output_data.x->wm_hints.icon_x = icon_x;
12762 f->output_data.x->wm_hints.icon_y = icon_y;
12764 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12768 /***********************************************************************
12769 Fonts
12770 ***********************************************************************/
12772 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12774 struct font_info *
12775 x_get_font_info (f, font_idx)
12776 FRAME_PTR f;
12777 int font_idx;
12779 return (FRAME_X_FONT_TABLE (f) + font_idx);
12783 /* Return a list of names of available fonts matching PATTERN on frame
12784 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12785 to be listed. Frame F NULL means we have not yet created any
12786 frame on X, and consult the first display in x_display_list.
12787 MAXNAMES sets a limit on how many fonts to match. */
12789 Lisp_Object
12790 x_list_fonts (f, pattern, size, maxnames)
12791 FRAME_PTR f;
12792 Lisp_Object pattern;
12793 int size;
12794 int maxnames;
12796 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12797 Lisp_Object tem, second_best;
12798 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12799 int try_XLoadQueryFont = 0;
12800 int count;
12802 patterns = Fassoc (pattern, Valternate_fontname_alist);
12803 if (NILP (patterns))
12804 patterns = Fcons (pattern, Qnil);
12806 if (maxnames == 1 && !size)
12807 /* We can return any single font matching PATTERN. */
12808 try_XLoadQueryFont = 1;
12810 for (; CONSP (patterns); patterns = XCDR (patterns))
12812 int num_fonts;
12813 char **names = NULL;
12815 pattern = XCAR (patterns);
12816 /* See if we cached the result for this particular query.
12817 The cache is an alist of the form:
12818 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12820 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12821 key = Fcons (pattern, make_number (maxnames)),
12822 !NILP (list = Fassoc (key, tem))))
12824 list = Fcdr_safe (list);
12825 /* We have a cashed list. Don't have to get the list again. */
12826 goto label_cached;
12829 /* At first, put PATTERN in the cache. */
12831 BLOCK_INPUT;
12832 count = x_catch_errors (dpy);
12834 if (try_XLoadQueryFont)
12836 XFontStruct *font;
12837 unsigned long value;
12839 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12840 if (x_had_errors_p (dpy))
12842 /* This error is perhaps due to insufficient memory on X
12843 server. Let's just ignore it. */
12844 font = NULL;
12845 x_clear_errors (dpy);
12848 if (font
12849 && XGetFontProperty (font, XA_FONT, &value))
12851 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12852 int len = strlen (name);
12853 char *tmp;
12855 /* If DXPC (a Differential X Protocol Compressor)
12856 Ver.3.7 is running, XGetAtomName will return null
12857 string. We must avoid such a name. */
12858 if (len == 0)
12859 try_XLoadQueryFont = 0;
12860 else
12862 num_fonts = 1;
12863 names = (char **) alloca (sizeof (char *));
12864 /* Some systems only allow alloca assigned to a
12865 simple var. */
12866 tmp = (char *) alloca (len + 1); names[0] = tmp;
12867 bcopy (name, names[0], len + 1);
12868 XFree (name);
12871 else
12872 try_XLoadQueryFont = 0;
12874 if (font)
12875 XFreeFont (dpy, font);
12878 if (!try_XLoadQueryFont)
12880 /* We try at least 10 fonts because XListFonts will return
12881 auto-scaled fonts at the head. */
12882 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12883 &num_fonts);
12884 if (x_had_errors_p (dpy))
12886 /* This error is perhaps due to insufficient memory on X
12887 server. Let's just ignore it. */
12888 names = NULL;
12889 x_clear_errors (dpy);
12893 x_uncatch_errors (dpy, count);
12894 UNBLOCK_INPUT;
12896 if (names)
12898 int i;
12900 /* Make a list of all the fonts we got back.
12901 Store that in the font cache for the display. */
12902 for (i = 0; i < num_fonts; i++)
12904 int width = 0;
12905 char *p = names[i];
12906 int average_width = -1, dashes = 0;
12908 /* Count the number of dashes in NAMES[I]. If there are
12909 14 dashes, and the field value following 12th dash
12910 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12911 is usually too ugly to be used for editing. Let's
12912 ignore it. */
12913 while (*p)
12914 if (*p++ == '-')
12916 dashes++;
12917 if (dashes == 7) /* PIXEL_SIZE field */
12918 width = atoi (p);
12919 else if (dashes == 12) /* AVERAGE_WIDTH field */
12920 average_width = atoi (p);
12922 if (dashes < 14 || average_width != 0)
12924 tem = build_string (names[i]);
12925 if (NILP (Fassoc (tem, list)))
12927 if (STRINGP (Vx_pixel_size_width_font_regexp)
12928 && ((fast_c_string_match_ignore_case
12929 (Vx_pixel_size_width_font_regexp, names[i]))
12930 >= 0))
12931 /* We can set the value of PIXEL_SIZE to the
12932 width of this font. */
12933 list = Fcons (Fcons (tem, make_number (width)), list);
12934 else
12935 /* For the moment, width is not known. */
12936 list = Fcons (Fcons (tem, Qnil), list);
12940 if (!try_XLoadQueryFont)
12941 XFreeFontNames (names);
12944 /* Now store the result in the cache. */
12945 if (f != NULL)
12946 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12947 = Fcons (Fcons (key, list),
12948 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12950 label_cached:
12951 if (NILP (list)) continue; /* Try the remaining alternatives. */
12953 newlist = second_best = Qnil;
12954 /* Make a list of the fonts that have the right width. */
12955 for (; CONSP (list); list = XCDR (list))
12957 int found_size;
12959 tem = XCAR (list);
12961 if (!CONSP (tem) || NILP (XCAR (tem)))
12962 continue;
12963 if (!size)
12965 newlist = Fcons (XCAR (tem), newlist);
12966 continue;
12969 if (!INTEGERP (XCDR (tem)))
12971 /* Since we have not yet known the size of this font, we
12972 must try slow function call XLoadQueryFont. */
12973 XFontStruct *thisinfo;
12975 BLOCK_INPUT;
12976 count = x_catch_errors (dpy);
12977 thisinfo = XLoadQueryFont (dpy,
12978 XSTRING (XCAR (tem))->data);
12979 if (x_had_errors_p (dpy))
12981 /* This error is perhaps due to insufficient memory on X
12982 server. Let's just ignore it. */
12983 thisinfo = NULL;
12984 x_clear_errors (dpy);
12986 x_uncatch_errors (dpy, count);
12987 UNBLOCK_INPUT;
12989 if (thisinfo)
12991 XCDR (tem)
12992 = (thisinfo->min_bounds.width == 0
12993 ? make_number (0)
12994 : make_number (thisinfo->max_bounds.width));
12995 XFreeFont (dpy, thisinfo);
12997 else
12998 /* For unknown reason, the previous call of XListFont had
12999 returned a font which can't be opened. Record the size
13000 as 0 not to try to open it again. */
13001 XCDR (tem) = make_number (0);
13004 found_size = XINT (XCDR (tem));
13005 if (found_size == size)
13006 newlist = Fcons (XCAR (tem), newlist);
13007 else if (found_size > 0)
13009 if (NILP (second_best))
13010 second_best = tem;
13011 else if (found_size < size)
13013 if (XINT (XCDR (second_best)) > size
13014 || XINT (XCDR (second_best)) < found_size)
13015 second_best = tem;
13017 else
13019 if (XINT (XCDR (second_best)) > size
13020 && XINT (XCDR (second_best)) > found_size)
13021 second_best = tem;
13025 if (!NILP (newlist))
13026 break;
13027 else if (!NILP (second_best))
13029 newlist = Fcons (XCAR (second_best), Qnil);
13030 break;
13034 return newlist;
13038 #if GLYPH_DEBUG
13040 /* Check that FONT is valid on frame F. It is if it can be found in F's
13041 font table. */
13043 static void
13044 x_check_font (f, font)
13045 struct frame *f;
13046 XFontStruct *font;
13048 int i;
13049 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13051 xassert (font != NULL);
13053 for (i = 0; i < dpyinfo->n_fonts; i++)
13054 if (dpyinfo->font_table[i].name
13055 && font == dpyinfo->font_table[i].font)
13056 break;
13058 xassert (i < dpyinfo->n_fonts);
13061 #endif /* GLYPH_DEBUG != 0 */
13063 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13064 Note: There are (broken) X fonts out there with invalid XFontStruct
13065 min_bounds contents. For example, handa@etl.go.jp reports that
13066 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13067 have font->min_bounds.width == 0. */
13069 static INLINE void
13070 x_font_min_bounds (font, w, h)
13071 XFontStruct *font;
13072 int *w, *h;
13074 *h = FONT_HEIGHT (font);
13075 *w = font->min_bounds.width;
13077 /* Try to handle the case where FONT->min_bounds has invalid
13078 contents. Since the only font known to have invalid min_bounds
13079 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13080 if (*w <= 0)
13081 *w = font->max_bounds.width;
13085 /* Compute the smallest character width and smallest font height over
13086 all fonts available on frame F. Set the members smallest_char_width
13087 and smallest_font_height in F's x_display_info structure to
13088 the values computed. Value is non-zero if smallest_font_height or
13089 smallest_char_width become smaller than they were before. */
13091 static int
13092 x_compute_min_glyph_bounds (f)
13093 struct frame *f;
13095 int i;
13096 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13097 XFontStruct *font;
13098 int old_width = dpyinfo->smallest_char_width;
13099 int old_height = dpyinfo->smallest_font_height;
13101 dpyinfo->smallest_font_height = 100000;
13102 dpyinfo->smallest_char_width = 100000;
13104 for (i = 0; i < dpyinfo->n_fonts; ++i)
13105 if (dpyinfo->font_table[i].name)
13107 struct font_info *fontp = dpyinfo->font_table + i;
13108 int w, h;
13110 font = (XFontStruct *) fontp->font;
13111 xassert (font != (XFontStruct *) ~0);
13112 x_font_min_bounds (font, &w, &h);
13114 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13115 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13118 xassert (dpyinfo->smallest_char_width > 0
13119 && dpyinfo->smallest_font_height > 0);
13121 return (dpyinfo->n_fonts == 1
13122 || dpyinfo->smallest_char_width < old_width
13123 || dpyinfo->smallest_font_height < old_height);
13127 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13128 pointer to the structure font_info while allocating it dynamically.
13129 If SIZE is 0, load any size of font.
13130 If loading is failed, return NULL. */
13132 struct font_info *
13133 x_load_font (f, fontname, size)
13134 struct frame *f;
13135 register char *fontname;
13136 int size;
13138 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13139 Lisp_Object font_names;
13140 int count;
13142 /* Get a list of all the fonts that match this name. Once we
13143 have a list of matching fonts, we compare them against the fonts
13144 we already have by comparing names. */
13145 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13147 if (!NILP (font_names))
13149 Lisp_Object tail;
13150 int i;
13152 for (i = 0; i < dpyinfo->n_fonts; i++)
13153 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13154 if (dpyinfo->font_table[i].name
13155 && (!strcmp (dpyinfo->font_table[i].name,
13156 XSTRING (XCAR (tail))->data)
13157 || !strcmp (dpyinfo->font_table[i].full_name,
13158 XSTRING (XCAR (tail))->data)))
13159 return (dpyinfo->font_table + i);
13162 /* Load the font and add it to the table. */
13164 char *full_name;
13165 XFontStruct *font;
13166 struct font_info *fontp;
13167 unsigned long value;
13168 int i;
13170 /* If we have found fonts by x_list_font, load one of them. If
13171 not, we still try to load a font by the name given as FONTNAME
13172 because XListFonts (called in x_list_font) of some X server has
13173 a bug of not finding a font even if the font surely exists and
13174 is loadable by XLoadQueryFont. */
13175 if (size > 0 && !NILP (font_names))
13176 fontname = (char *) XSTRING (XCAR (font_names))->data;
13178 BLOCK_INPUT;
13179 count = x_catch_errors (FRAME_X_DISPLAY (f));
13180 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13181 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13183 /* This error is perhaps due to insufficient memory on X
13184 server. Let's just ignore it. */
13185 font = NULL;
13186 x_clear_errors (FRAME_X_DISPLAY (f));
13188 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13189 UNBLOCK_INPUT;
13190 if (!font)
13191 return NULL;
13193 /* Find a free slot in the font table. */
13194 for (i = 0; i < dpyinfo->n_fonts; ++i)
13195 if (dpyinfo->font_table[i].name == NULL)
13196 break;
13198 /* If no free slot found, maybe enlarge the font table. */
13199 if (i == dpyinfo->n_fonts
13200 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13202 int sz;
13203 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13204 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13205 dpyinfo->font_table
13206 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13209 fontp = dpyinfo->font_table + i;
13210 if (i == dpyinfo->n_fonts)
13211 ++dpyinfo->n_fonts;
13213 /* Now fill in the slots of *FONTP. */
13214 BLOCK_INPUT;
13215 fontp->font = font;
13216 fontp->font_idx = i;
13217 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13218 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13220 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13221 full_name = 0;
13222 if (XGetFontProperty (font, XA_FONT, &value))
13224 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13225 char *p = name;
13226 int dashes = 0;
13228 /* Count the number of dashes in the "full name".
13229 If it is too few, this isn't really the font's full name,
13230 so don't use it.
13231 In X11R4, the fonts did not come with their canonical names
13232 stored in them. */
13233 while (*p)
13235 if (*p == '-')
13236 dashes++;
13237 p++;
13240 if (dashes >= 13)
13242 full_name = (char *) xmalloc (p - name + 1);
13243 bcopy (name, full_name, p - name + 1);
13246 XFree (name);
13249 if (full_name != 0)
13250 fontp->full_name = full_name;
13251 else
13252 fontp->full_name = fontp->name;
13254 fontp->size = font->max_bounds.width;
13255 fontp->height = FONT_HEIGHT (font);
13257 if (NILP (font_names))
13259 /* We come here because of a bug of XListFonts mentioned at
13260 the head of this block. Let's store this information in
13261 the cache for x_list_fonts. */
13262 Lisp_Object lispy_name = build_string (fontname);
13263 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13265 XCDR (dpyinfo->name_list_element)
13266 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
13267 Fcons (Fcons (lispy_full_name,
13268 make_number (fontp->size)),
13269 Qnil)),
13270 XCDR (dpyinfo->name_list_element));
13271 if (full_name)
13272 XCDR (dpyinfo->name_list_element)
13273 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
13274 Fcons (Fcons (lispy_full_name,
13275 make_number (fontp->size)),
13276 Qnil)),
13277 XCDR (dpyinfo->name_list_element));
13280 /* The slot `encoding' specifies how to map a character
13281 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13282 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13283 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13284 2:0xA020..0xFF7F). For the moment, we don't know which charset
13285 uses this font. So, we set information in fontp->encoding[1]
13286 which is never used by any charset. If mapping can't be
13287 decided, set FONT_ENCODING_NOT_DECIDED. */
13288 fontp->encoding[1]
13289 = (font->max_byte1 == 0
13290 /* 1-byte font */
13291 ? (font->min_char_or_byte2 < 0x80
13292 ? (font->max_char_or_byte2 < 0x80
13293 ? 0 /* 0x20..0x7F */
13294 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13295 : 1) /* 0xA0..0xFF */
13296 /* 2-byte font */
13297 : (font->min_byte1 < 0x80
13298 ? (font->max_byte1 < 0x80
13299 ? (font->min_char_or_byte2 < 0x80
13300 ? (font->max_char_or_byte2 < 0x80
13301 ? 0 /* 0x2020..0x7F7F */
13302 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13303 : 3) /* 0x20A0..0x7FFF */
13304 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13305 : (font->min_char_or_byte2 < 0x80
13306 ? (font->max_char_or_byte2 < 0x80
13307 ? 2 /* 0xA020..0xFF7F */
13308 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13309 : 1))); /* 0xA0A0..0xFFFF */
13311 fontp->baseline_offset
13312 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13313 ? (long) value : 0);
13314 fontp->relative_compose
13315 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13316 ? (long) value : 0);
13317 fontp->default_ascent
13318 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13319 ? (long) value : 0);
13321 /* Set global flag fonts_changed_p to non-zero if the font loaded
13322 has a character with a smaller width than any other character
13323 before, or if the font loaded has a smalle>r height than any
13324 other font loaded before. If this happens, it will make a
13325 glyph matrix reallocation necessary. */
13326 fonts_changed_p = x_compute_min_glyph_bounds (f);
13327 UNBLOCK_INPUT;
13328 return fontp;
13333 /* Return a pointer to struct font_info of a font named FONTNAME for
13334 frame F. If no such font is loaded, return NULL. */
13336 struct font_info *
13337 x_query_font (f, fontname)
13338 struct frame *f;
13339 register char *fontname;
13341 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13342 int i;
13344 for (i = 0; i < dpyinfo->n_fonts; i++)
13345 if (dpyinfo->font_table[i].name
13346 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13347 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13348 return (dpyinfo->font_table + i);
13349 return NULL;
13353 /* Find a CCL program for a font specified by FONTP, and set the member
13354 `encoder' of the structure. */
13356 void
13357 x_find_ccl_program (fontp)
13358 struct font_info *fontp;
13360 Lisp_Object list, elt;
13362 elt = Qnil;
13363 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13365 elt = XCAR (list);
13366 if (CONSP (elt)
13367 && STRINGP (XCAR (elt))
13368 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13369 >= 0)
13370 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13371 >= 0)))
13372 break;
13375 if (! NILP (list))
13377 struct ccl_program *ccl
13378 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13380 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13381 xfree (ccl);
13382 else
13383 fontp->font_encoder = ccl;
13389 /***********************************************************************
13390 Initialization
13391 ***********************************************************************/
13393 #ifdef USE_X_TOOLKIT
13394 static XrmOptionDescRec emacs_options[] = {
13395 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13396 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13398 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13399 XrmoptionSepArg, NULL},
13400 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13402 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13403 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13404 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13405 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13406 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13407 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13408 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13410 #endif /* USE_X_TOOLKIT */
13412 static int x_initialized;
13414 #ifdef MULTI_KBOARD
13415 /* Test whether two display-name strings agree up to the dot that separates
13416 the screen number from the server number. */
13417 static int
13418 same_x_server (name1, name2)
13419 char *name1, *name2;
13421 int seen_colon = 0;
13422 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13423 int system_name_length = strlen (system_name);
13424 int length_until_period = 0;
13426 while (system_name[length_until_period] != 0
13427 && system_name[length_until_period] != '.')
13428 length_until_period++;
13430 /* Treat `unix' like an empty host name. */
13431 if (! strncmp (name1, "unix:", 5))
13432 name1 += 4;
13433 if (! strncmp (name2, "unix:", 5))
13434 name2 += 4;
13435 /* Treat this host's name like an empty host name. */
13436 if (! strncmp (name1, system_name, system_name_length)
13437 && name1[system_name_length] == ':')
13438 name1 += system_name_length;
13439 if (! strncmp (name2, system_name, system_name_length)
13440 && name2[system_name_length] == ':')
13441 name2 += system_name_length;
13442 /* Treat this host's domainless name like an empty host name. */
13443 if (! strncmp (name1, system_name, length_until_period)
13444 && name1[length_until_period] == ':')
13445 name1 += length_until_period;
13446 if (! strncmp (name2, system_name, length_until_period)
13447 && name2[length_until_period] == ':')
13448 name2 += length_until_period;
13450 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13452 if (*name1 == ':')
13453 seen_colon++;
13454 if (seen_colon && *name1 == '.')
13455 return 1;
13457 return (seen_colon
13458 && (*name1 == '.' || *name1 == '\0')
13459 && (*name2 == '.' || *name2 == '\0'));
13461 #endif
13463 struct x_display_info *
13464 x_term_init (display_name, xrm_option, resource_name)
13465 Lisp_Object display_name;
13466 char *xrm_option;
13467 char *resource_name;
13469 int connection;
13470 Display *dpy;
13471 struct x_display_info *dpyinfo;
13472 XrmDatabase xrdb;
13474 BLOCK_INPUT;
13476 if (!x_initialized)
13478 x_initialize ();
13479 x_initialized = 1;
13482 #ifdef USE_X_TOOLKIT
13483 /* weiner@footloose.sps.mot.com reports that this causes
13484 errors with X11R5:
13485 X protocol error: BadAtom (invalid Atom parameter)
13486 on protocol request 18skiloaf.
13487 So let's not use it until R6. */
13488 #ifdef HAVE_X11XTR6
13489 XtSetLanguageProc (NULL, NULL, NULL);
13490 #endif
13493 int argc = 0;
13494 char *argv[3];
13496 argv[0] = "";
13497 argc = 1;
13498 if (xrm_option)
13500 argv[argc++] = "-xrm";
13501 argv[argc++] = xrm_option;
13503 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13504 resource_name, EMACS_CLASS,
13505 emacs_options, XtNumber (emacs_options),
13506 &argc, argv);
13508 #ifdef HAVE_X11XTR6
13509 /* I think this is to compensate for XtSetLanguageProc. */
13510 fixup_locale ();
13511 #endif
13514 #else /* not USE_X_TOOLKIT */
13515 #ifdef HAVE_X11R5
13516 XSetLocaleModifiers ("");
13517 #endif
13518 dpy = XOpenDisplay (XSTRING (display_name)->data);
13519 #endif /* not USE_X_TOOLKIT */
13521 /* Detect failure. */
13522 if (dpy == 0)
13524 UNBLOCK_INPUT;
13525 return 0;
13528 /* We have definitely succeeded. Record the new connection. */
13530 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13531 bzero (dpyinfo, sizeof *dpyinfo);
13533 #ifdef MULTI_KBOARD
13535 struct x_display_info *share;
13536 Lisp_Object tail;
13538 for (share = x_display_list, tail = x_display_name_list; share;
13539 share = share->next, tail = XCDR (tail))
13540 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13541 XSTRING (display_name)->data))
13542 break;
13543 if (share)
13544 dpyinfo->kboard = share->kboard;
13545 else
13547 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13548 init_kboard (dpyinfo->kboard);
13549 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13551 char *vendor = ServerVendor (dpy);
13552 UNBLOCK_INPUT;
13553 dpyinfo->kboard->Vsystem_key_alist
13554 = call1 (Qvendor_specific_keysyms,
13555 build_string (vendor ? vendor : ""));
13556 BLOCK_INPUT;
13559 dpyinfo->kboard->next_kboard = all_kboards;
13560 all_kboards = dpyinfo->kboard;
13561 /* Don't let the initial kboard remain current longer than necessary.
13562 That would cause problems if a file loaded on startup tries to
13563 prompt in the mini-buffer. */
13564 if (current_kboard == initial_kboard)
13565 current_kboard = dpyinfo->kboard;
13567 dpyinfo->kboard->reference_count++;
13569 #endif
13571 /* Put this display on the chain. */
13572 dpyinfo->next = x_display_list;
13573 x_display_list = dpyinfo;
13575 /* Put it on x_display_name_list as well, to keep them parallel. */
13576 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13577 x_display_name_list);
13578 dpyinfo->name_list_element = XCAR (x_display_name_list);
13580 dpyinfo->display = dpy;
13582 #if 0
13583 XSetAfterFunction (x_current_display, x_trace_wire);
13584 #endif /* ! 0 */
13586 dpyinfo->x_id_name
13587 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13588 + STRING_BYTES (XSTRING (Vsystem_name))
13589 + 2);
13590 sprintf (dpyinfo->x_id_name, "%s@%s",
13591 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13593 /* Figure out which modifier bits mean what. */
13594 x_find_modifier_meanings (dpyinfo);
13596 /* Get the scroll bar cursor. */
13597 dpyinfo->vertical_scroll_bar_cursor
13598 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13600 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13601 resource_name, EMACS_CLASS);
13602 #ifdef HAVE_XRMSETDATABASE
13603 XrmSetDatabase (dpyinfo->display, xrdb);
13604 #else
13605 dpyinfo->display->db = xrdb;
13606 #endif
13607 /* Put the rdb where we can find it in a way that works on
13608 all versions. */
13609 dpyinfo->xrdb = xrdb;
13611 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13612 DefaultScreen (dpyinfo->display));
13613 select_visual (dpyinfo);
13614 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13615 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13616 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13617 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13618 dpyinfo->grabbed = 0;
13619 dpyinfo->reference_count = 0;
13620 dpyinfo->icon_bitmap_id = -1;
13621 dpyinfo->font_table = NULL;
13622 dpyinfo->n_fonts = 0;
13623 dpyinfo->font_table_size = 0;
13624 dpyinfo->bitmaps = 0;
13625 dpyinfo->bitmaps_size = 0;
13626 dpyinfo->bitmaps_last = 0;
13627 dpyinfo->scratch_cursor_gc = 0;
13628 dpyinfo->mouse_face_mouse_frame = 0;
13629 dpyinfo->mouse_face_deferred_gc = 0;
13630 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13631 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13632 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13633 dpyinfo->mouse_face_window = Qnil;
13634 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13635 dpyinfo->mouse_face_defer = 0;
13636 dpyinfo->x_focus_frame = 0;
13637 dpyinfo->x_focus_event_frame = 0;
13638 dpyinfo->x_highlight_frame = 0;
13639 dpyinfo->image_cache = make_image_cache ();
13641 /* See if a private colormap is requested. */
13642 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13644 if (dpyinfo->visual->class == PseudoColor)
13646 Lisp_Object value;
13647 value = display_x_get_resource (dpyinfo,
13648 build_string ("privateColormap"),
13649 build_string ("PrivateColormap"),
13650 Qnil, Qnil);
13651 if (STRINGP (value)
13652 && (!strcmp (XSTRING (value)->data, "true")
13653 || !strcmp (XSTRING (value)->data, "on")))
13654 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13657 else
13658 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13659 dpyinfo->visual, AllocNone);
13662 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13663 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13664 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13665 dpyinfo->resy = pixels * 25.4 / mm;
13666 pixels = DisplayWidth (dpyinfo->display, screen_number);
13667 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13668 dpyinfo->resx = pixels * 25.4 / mm;
13671 dpyinfo->Xatom_wm_protocols
13672 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13673 dpyinfo->Xatom_wm_take_focus
13674 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13675 dpyinfo->Xatom_wm_save_yourself
13676 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13677 dpyinfo->Xatom_wm_delete_window
13678 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13679 dpyinfo->Xatom_wm_change_state
13680 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13681 dpyinfo->Xatom_wm_configure_denied
13682 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13683 dpyinfo->Xatom_wm_window_moved
13684 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13685 dpyinfo->Xatom_editres
13686 = XInternAtom (dpyinfo->display, "Editres", False);
13687 dpyinfo->Xatom_CLIPBOARD
13688 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13689 dpyinfo->Xatom_TIMESTAMP
13690 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13691 dpyinfo->Xatom_TEXT
13692 = XInternAtom (dpyinfo->display, "TEXT", False);
13693 dpyinfo->Xatom_COMPOUND_TEXT
13694 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13695 dpyinfo->Xatom_DELETE
13696 = XInternAtom (dpyinfo->display, "DELETE", False);
13697 dpyinfo->Xatom_MULTIPLE
13698 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13699 dpyinfo->Xatom_INCR
13700 = XInternAtom (dpyinfo->display, "INCR", False);
13701 dpyinfo->Xatom_EMACS_TMP
13702 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13703 dpyinfo->Xatom_TARGETS
13704 = XInternAtom (dpyinfo->display, "TARGETS", False);
13705 dpyinfo->Xatom_NULL
13706 = XInternAtom (dpyinfo->display, "NULL", False);
13707 dpyinfo->Xatom_ATOM_PAIR
13708 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13709 /* For properties of font. */
13710 dpyinfo->Xatom_PIXEL_SIZE
13711 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13712 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13713 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13714 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13715 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13716 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13717 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13719 /* Ghostscript support. */
13720 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13721 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13723 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13724 False);
13726 dpyinfo->cut_buffers_initialized = 0;
13728 connection = ConnectionNumber (dpyinfo->display);
13729 dpyinfo->connection = connection;
13732 char null_bits[1];
13734 null_bits[0] = 0x00;
13736 dpyinfo->null_pixel
13737 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13738 null_bits, 1, 1, (long) 0, (long) 0,
13743 extern int gray_bitmap_width, gray_bitmap_height;
13744 extern char *gray_bitmap_bits;
13745 dpyinfo->gray
13746 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13747 gray_bitmap_bits,
13748 gray_bitmap_width, gray_bitmap_height,
13749 (unsigned long) 1, (unsigned long) 0, 1);
13752 #ifdef HAVE_X_I18N
13753 xim_initialize (dpyinfo, resource_name);
13754 #endif
13756 #ifdef subprocesses
13757 /* This is only needed for distinguishing keyboard and process input. */
13758 if (connection != 0)
13759 add_keyboard_wait_descriptor (connection);
13760 #endif
13762 #ifndef F_SETOWN_BUG
13763 #ifdef F_SETOWN
13764 #ifdef F_SETOWN_SOCK_NEG
13765 /* stdin is a socket here */
13766 fcntl (connection, F_SETOWN, -getpid ());
13767 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13768 fcntl (connection, F_SETOWN, getpid ());
13769 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13770 #endif /* ! defined (F_SETOWN) */
13771 #endif /* F_SETOWN_BUG */
13773 #ifdef SIGIO
13774 if (interrupt_input)
13775 init_sigio (connection);
13776 #endif /* ! defined (SIGIO) */
13778 #ifdef USE_LUCID
13779 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13780 /* Make sure that we have a valid font for dialog boxes
13781 so that Xt does not crash. */
13783 Display *dpy = dpyinfo->display;
13784 XrmValue d, fr, to;
13785 Font font;
13786 int count;
13788 d.addr = (XPointer)&dpy;
13789 d.size = sizeof (Display *);
13790 fr.addr = XtDefaultFont;
13791 fr.size = sizeof (XtDefaultFont);
13792 to.size = sizeof (Font *);
13793 to.addr = (XPointer)&font;
13794 count = x_catch_errors (dpy);
13795 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13796 abort ();
13797 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13798 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13799 x_uncatch_errors (dpy, count);
13801 #endif
13802 #endif
13804 /* See if we should run in synchronous mode. This is useful
13805 for debugging X code. */
13807 Lisp_Object value;
13808 value = display_x_get_resource (dpyinfo,
13809 build_string ("synchronous"),
13810 build_string ("Synchronous"),
13811 Qnil, Qnil);
13812 if (STRINGP (value)
13813 && (!strcmp (XSTRING (value)->data, "true")
13814 || !strcmp (XSTRING (value)->data, "on")))
13815 XSynchronize (dpyinfo->display, True);
13818 UNBLOCK_INPUT;
13820 return dpyinfo;
13823 /* Get rid of display DPYINFO, assuming all frames are already gone,
13824 and without sending any more commands to the X server. */
13826 void
13827 x_delete_display (dpyinfo)
13828 struct x_display_info *dpyinfo;
13830 delete_keyboard_wait_descriptor (dpyinfo->connection);
13832 /* Discard this display from x_display_name_list and x_display_list.
13833 We can't use Fdelq because that can quit. */
13834 if (! NILP (x_display_name_list)
13835 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13836 x_display_name_list = XCDR (x_display_name_list);
13837 else
13839 Lisp_Object tail;
13841 tail = x_display_name_list;
13842 while (CONSP (tail) && CONSP (XCDR (tail)))
13844 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13846 XCDR (tail) = XCDR (XCDR (tail));
13847 break;
13849 tail = XCDR (tail);
13853 if (next_noop_dpyinfo == dpyinfo)
13854 next_noop_dpyinfo = dpyinfo->next;
13856 if (x_display_list == dpyinfo)
13857 x_display_list = dpyinfo->next;
13858 else
13860 struct x_display_info *tail;
13862 for (tail = x_display_list; tail; tail = tail->next)
13863 if (tail->next == dpyinfo)
13864 tail->next = tail->next->next;
13867 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13868 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13869 XrmDestroyDatabase (dpyinfo->xrdb);
13870 #endif
13871 #endif
13872 #ifdef MULTI_KBOARD
13873 if (--dpyinfo->kboard->reference_count == 0)
13874 delete_kboard (dpyinfo->kboard);
13875 #endif
13876 #ifdef HAVE_X_I18N
13877 if (dpyinfo->xim)
13878 xim_close_dpy (dpyinfo);
13879 #endif
13881 xfree (dpyinfo->font_table);
13882 xfree (dpyinfo->x_id_name);
13883 xfree (dpyinfo->color_cells);
13884 xfree (dpyinfo);
13888 /* Set up use of X before we make the first connection. */
13890 static struct redisplay_interface x_redisplay_interface =
13892 x_produce_glyphs,
13893 x_write_glyphs,
13894 x_insert_glyphs,
13895 x_clear_end_of_line,
13896 x_scroll_run,
13897 x_after_update_window_line,
13898 x_update_window_begin,
13899 x_update_window_end,
13900 XTcursor_to,
13901 x_flush,
13902 x_clear_mouse_face,
13903 x_get_glyph_overhangs,
13904 x_fix_overlapping_area
13907 void
13908 x_initialize ()
13910 rif = &x_redisplay_interface;
13912 clear_frame_hook = x_clear_frame;
13913 ins_del_lines_hook = x_ins_del_lines;
13914 change_line_highlight_hook = x_change_line_highlight;
13915 delete_glyphs_hook = x_delete_glyphs;
13916 ring_bell_hook = XTring_bell;
13917 reset_terminal_modes_hook = XTreset_terminal_modes;
13918 set_terminal_modes_hook = XTset_terminal_modes;
13919 update_begin_hook = x_update_begin;
13920 update_end_hook = x_update_end;
13921 set_terminal_window_hook = XTset_terminal_window;
13922 read_socket_hook = XTread_socket;
13923 frame_up_to_date_hook = XTframe_up_to_date;
13924 reassert_line_highlight_hook = XTreassert_line_highlight;
13925 mouse_position_hook = XTmouse_position;
13926 frame_rehighlight_hook = XTframe_rehighlight;
13927 frame_raise_lower_hook = XTframe_raise_lower;
13928 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13929 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13930 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13931 judge_scroll_bars_hook = XTjudge_scroll_bars;
13932 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13934 scroll_region_ok = 1; /* we'll scroll partial frames */
13935 char_ins_del_ok = 0; /* just as fast to write the line */
13936 line_ins_del_ok = 1; /* we'll just blt 'em */
13937 fast_clear_end_of_line = 1; /* X does this well */
13938 memory_below_frame = 0; /* we don't remember what scrolls
13939 off the bottom */
13940 baud_rate = 19200;
13942 x_noop_count = 0;
13943 last_tool_bar_item = -1;
13944 any_help_event_p = 0;
13946 /* Try to use interrupt input; if we can't, then start polling. */
13947 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13949 #ifdef USE_X_TOOLKIT
13950 XtToolkitInitialize ();
13951 Xt_app_con = XtCreateApplicationContext ();
13952 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13954 /* Install an asynchronous timer that processes Xt timeout events
13955 every 0.1s. This is necessary because some widget sets use
13956 timeouts internally, for example the LessTif menu bar, or the
13957 Xaw3d scroll bar. When Xt timouts aren't processed, these
13958 widgets don't behave normally. */
13960 EMACS_TIME interval;
13961 EMACS_SET_SECS_USECS (interval, 0, 100000);
13962 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13964 #endif
13966 #ifdef USE_TOOLKIT_SCROLL_BARS
13967 xaw3d_arrow_scroll = False;
13968 xaw3d_pick_top = True;
13969 #endif
13971 /* Note that there is no real way portable across R3/R4 to get the
13972 original error handler. */
13973 XSetErrorHandler (x_error_handler);
13974 XSetIOErrorHandler (x_io_error_quitter);
13976 /* Disable Window Change signals; they are handled by X events. */
13977 #ifdef SIGWINCH
13978 signal (SIGWINCH, SIG_DFL);
13979 #endif /* ! defined (SIGWINCH) */
13981 signal (SIGPIPE, x_connection_signal);
13985 void
13986 syms_of_xterm ()
13988 staticpro (&x_error_message_string);
13989 x_error_message_string = Qnil;
13991 staticpro (&x_display_name_list);
13992 x_display_name_list = Qnil;
13994 staticpro (&last_mouse_scroll_bar);
13995 last_mouse_scroll_bar = Qnil;
13997 staticpro (&Qvendor_specific_keysyms);
13998 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
14000 staticpro (&last_mouse_press_frame);
14001 last_mouse_press_frame = Qnil;
14003 help_echo = Qnil;
14004 staticpro (&help_echo);
14005 help_echo_object = Qnil;
14006 staticpro (&help_echo_object);
14007 help_echo_window = Qnil;
14008 staticpro (&help_echo_window);
14009 previous_help_echo = Qnil;
14010 staticpro (&previous_help_echo);
14011 help_echo_pos = -1;
14013 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
14014 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
14015 For example, if a block cursor is over a tab, it will be drawn as\n\
14016 wide as that tab on the display.");
14017 x_stretch_cursor_p = 0;
14019 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
14020 "What X toolkit scroll bars Emacs uses.\n\
14021 A value of nil means Emacs doesn't use X toolkit scroll bars.\n\
14022 Otherwise, value is a symbol describing the X toolkit.");
14023 #ifdef USE_TOOLKIT_SCROLL_BARS
14024 #ifdef USE_MOTIF
14025 Vx_toolkit_scroll_bars = intern ("motif");
14026 #elif defined HAVE_XAW3D
14027 Vx_toolkit_scroll_bars = intern ("xaw3d");
14028 #else
14029 Vx_toolkit_scroll_bars = intern ("xaw");
14030 #endif
14031 #else
14032 Vx_toolkit_scroll_bars = Qnil;
14033 #endif
14035 staticpro (&last_mouse_motion_frame);
14036 last_mouse_motion_frame = Qnil;
14039 #endif /* not HAVE_X_WINDOWS */