(x_free_frame_resources): New function.
[emacs.git] / src / xterm.c
blobdaf2e90ea95c1e17f0c49c4256b8e265f105d989
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "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-zero means Emacs uses toolkit scroll bars. */
235 int x_toolkit_scroll_bars_p;
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->margin - it->ascent;
1508 it->pixel_width = img->width + 2 * img->margin;
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 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 (s->first_glyph->type == CHAR_GLYPH)
2668 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2669 else
2670 face_id = FACE_FOR_CHAR (s->f, face, 0);
2671 s->face = FACE_FROM_ID (s->f, face_id);
2672 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2674 /* If font in this face is same as S->font, use it. */
2675 if (s->font == s->face->font)
2676 s->gc = s->face->gc;
2677 else
2679 /* Otherwise construct scratch_cursor_gc with values from FACE
2680 but font FONT. */
2681 XGCValues xgcv;
2682 unsigned long mask;
2684 xgcv.background = s->face->background;
2685 xgcv.foreground = s->face->foreground;
2686 IF_DEBUG (x_check_font (s->f, s->font));
2687 xgcv.font = s->font->fid;
2688 xgcv.graphics_exposures = False;
2689 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2691 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2692 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2693 mask, &xgcv);
2694 else
2695 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2696 = XCreateGC (s->display, s->window, mask, &xgcv);
2698 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2701 xassert (s->gc != 0);
2705 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2706 Faces to use in the mode line have already been computed when the
2707 matrix was built, so there isn't much to do, here. */
2709 static INLINE void
2710 x_set_mode_line_face_gc (s)
2711 struct glyph_string *s;
2713 s->gc = s->face->gc;
2717 /* Set S->gc of glyph string S for drawing that glyph string. Set
2718 S->stippled_p to a non-zero value if the face of S has a stipple
2719 pattern. */
2721 static INLINE void
2722 x_set_glyph_string_gc (s)
2723 struct glyph_string *s;
2725 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2727 if (s->hl == DRAW_NORMAL_TEXT)
2729 s->gc = s->face->gc;
2730 s->stippled_p = s->face->stipple != 0;
2732 else if (s->hl == DRAW_INVERSE_VIDEO)
2734 x_set_mode_line_face_gc (s);
2735 s->stippled_p = s->face->stipple != 0;
2737 else if (s->hl == DRAW_CURSOR)
2739 x_set_cursor_gc (s);
2740 s->stippled_p = 0;
2742 else if (s->hl == DRAW_MOUSE_FACE)
2744 x_set_mouse_face_gc (s);
2745 s->stippled_p = s->face->stipple != 0;
2747 else if (s->hl == DRAW_IMAGE_RAISED
2748 || s->hl == DRAW_IMAGE_SUNKEN)
2750 s->gc = s->face->gc;
2751 s->stippled_p = s->face->stipple != 0;
2753 else
2755 s->gc = s->face->gc;
2756 s->stippled_p = s->face->stipple != 0;
2759 /* GC must have been set. */
2760 xassert (s->gc != 0);
2764 /* Return in *R the clipping rectangle for glyph string S. */
2766 static void
2767 x_get_glyph_string_clip_rect (s, r)
2768 struct glyph_string *s;
2769 XRectangle *r;
2771 if (s->row->full_width_p)
2773 /* Draw full-width. X coordinates are relative to S->w->left. */
2774 int canon_x = CANON_X_UNIT (s->f);
2776 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2777 r->width = XFASTINT (s->w->width) * canon_x;
2779 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2781 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2782 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2783 r->x -= width;
2786 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2788 /* Unless displaying a mode or menu bar line, which are always
2789 fully visible, clip to the visible part of the row. */
2790 if (s->w->pseudo_window_p)
2791 r->height = s->row->visible_height;
2792 else
2793 r->height = s->height;
2795 else
2797 /* This is a text line that may be partially visible. */
2798 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2799 r->width = window_box_width (s->w, s->area);
2800 r->height = s->row->visible_height;
2803 /* Don't use S->y for clipping because it doesn't take partially
2804 visible lines into account. For example, it can be negative for
2805 partially visible lines at the top of a window. */
2806 if (!s->row->full_width_p
2807 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2808 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2809 else
2810 r->y = max (0, s->row->y);
2812 /* If drawing a tool-bar window, draw it over the internal border
2813 at the top of the window. */
2814 if (s->w == XWINDOW (s->f->tool_bar_window))
2815 r->y -= s->f->output_data.x->internal_border_width;
2817 /* If S draws overlapping rows, it's sufficient to use the top and
2818 bottom of the window for clipping because this glyph string
2819 intentionally draws over other lines. */
2820 if (s->for_overlaps_p)
2822 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2823 r->height = window_text_bottom_y (s->w) - r->y;
2826 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2830 /* Set clipping for output of glyph string S. S may be part of a mode
2831 line or menu if we don't have X toolkit support. */
2833 static INLINE void
2834 x_set_glyph_string_clipping (s)
2835 struct glyph_string *s;
2837 XRectangle r;
2838 x_get_glyph_string_clip_rect (s, &r);
2839 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2843 /* Compute left and right overhang of glyph string S. If S is a glyph
2844 string for a composition, assume overhangs don't exist. */
2846 static INLINE void
2847 x_compute_glyph_string_overhangs (s)
2848 struct glyph_string *s;
2850 if (s->cmp == NULL
2851 && s->first_glyph->type == CHAR_GLYPH)
2853 XCharStruct cs;
2854 int direction, font_ascent, font_descent;
2855 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2856 &font_ascent, &font_descent, &cs);
2857 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2858 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2863 /* Compute overhangs and x-positions for glyph string S and its
2864 predecessors, or successors. X is the starting x-position for S.
2865 BACKWARD_P non-zero means process predecessors. */
2867 static void
2868 x_compute_overhangs_and_x (s, x, backward_p)
2869 struct glyph_string *s;
2870 int x;
2871 int backward_p;
2873 if (backward_p)
2875 while (s)
2877 x_compute_glyph_string_overhangs (s);
2878 x -= s->width;
2879 s->x = x;
2880 s = s->prev;
2883 else
2885 while (s)
2887 x_compute_glyph_string_overhangs (s);
2888 s->x = x;
2889 x += s->width;
2890 s = s->next;
2896 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2897 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2898 assumed to be zero. */
2900 static void
2901 x_get_glyph_overhangs (glyph, f, left, right)
2902 struct glyph *glyph;
2903 struct frame *f;
2904 int *left, *right;
2906 *left = *right = 0;
2908 if (glyph->type == CHAR_GLYPH)
2910 XFontStruct *font;
2911 struct face *face;
2912 struct font_info *font_info;
2913 XChar2b char2b;
2914 XCharStruct *pcm;
2916 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2917 font = face->font;
2918 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2919 if (font
2920 && (pcm = x_per_char_metric (font, &char2b)))
2922 if (pcm->rbearing > pcm->width)
2923 *right = pcm->rbearing - pcm->width;
2924 if (pcm->lbearing < 0)
2925 *left = -pcm->lbearing;
2931 /* Return the index of the first glyph preceding glyph string S that
2932 is overwritten by S because of S's left overhang. Value is -1
2933 if no glyphs are overwritten. */
2935 static int
2936 x_left_overwritten (s)
2937 struct glyph_string *s;
2939 int k;
2941 if (s->left_overhang)
2943 int x = 0, i;
2944 struct glyph *glyphs = s->row->glyphs[s->area];
2945 int first = s->first_glyph - glyphs;
2947 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2948 x -= glyphs[i].pixel_width;
2950 k = i + 1;
2952 else
2953 k = -1;
2955 return k;
2959 /* Return the index of the first glyph preceding glyph string S that
2960 is overwriting S because of its right overhang. Value is -1 if no
2961 glyph in front of S overwrites S. */
2963 static int
2964 x_left_overwriting (s)
2965 struct glyph_string *s;
2967 int i, k, x;
2968 struct glyph *glyphs = s->row->glyphs[s->area];
2969 int first = s->first_glyph - glyphs;
2971 k = -1;
2972 x = 0;
2973 for (i = first - 1; i >= 0; --i)
2975 int left, right;
2976 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
2977 if (x + right > 0)
2978 k = i;
2979 x -= glyphs[i].pixel_width;
2982 return k;
2986 /* Return the index of the last glyph following glyph string S that is
2987 not overwritten by S because of S's right overhang. Value is -1 if
2988 no such glyph is found. */
2990 static int
2991 x_right_overwritten (s)
2992 struct glyph_string *s;
2994 int k = -1;
2996 if (s->right_overhang)
2998 int x = 0, i;
2999 struct glyph *glyphs = s->row->glyphs[s->area];
3000 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3001 int end = s->row->used[s->area];
3003 for (i = first; i < end && s->right_overhang > x; ++i)
3004 x += glyphs[i].pixel_width;
3006 k = i;
3009 return k;
3013 /* Return the index of the last glyph following glyph string S that
3014 overwrites S because of its left overhang. Value is negative
3015 if no such glyph is found. */
3017 static int
3018 x_right_overwriting (s)
3019 struct glyph_string *s;
3021 int i, k, x;
3022 int end = s->row->used[s->area];
3023 struct glyph *glyphs = s->row->glyphs[s->area];
3024 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3026 k = -1;
3027 x = 0;
3028 for (i = first; i < end; ++i)
3030 int left, right;
3031 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3032 if (x - left < 0)
3033 k = i;
3034 x += glyphs[i].pixel_width;
3037 return k;
3041 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3043 static INLINE void
3044 x_clear_glyph_string_rect (s, x, y, w, h)
3045 struct glyph_string *s;
3046 int x, y, w, h;
3048 XGCValues xgcv;
3049 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3050 XSetForeground (s->display, s->gc, xgcv.background);
3051 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3052 XSetForeground (s->display, s->gc, xgcv.foreground);
3056 /* Draw the background of glyph_string S. If S->background_filled_p
3057 is non-zero don't draw it. FORCE_P non-zero means draw the
3058 background even if it wouldn't be drawn normally. This is used
3059 when a string preceding S draws into the background of S, or S
3060 contains the first component of a composition. */
3062 static void
3063 x_draw_glyph_string_background (s, force_p)
3064 struct glyph_string *s;
3065 int force_p;
3067 /* Nothing to do if background has already been drawn or if it
3068 shouldn't be drawn in the first place. */
3069 if (!s->background_filled_p)
3071 if (s->stippled_p)
3073 /* Fill background with a stipple pattern. */
3074 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3075 XFillRectangle (s->display, s->window, s->gc, s->x,
3076 s->y + s->face->box_line_width,
3077 s->background_width,
3078 s->height - 2 * s->face->box_line_width);
3079 XSetFillStyle (s->display, s->gc, FillSolid);
3080 s->background_filled_p = 1;
3082 else if (FONT_HEIGHT (s->font) < s->height - 2 * s->face->box_line_width
3083 || s->font_not_found_p
3084 || s->extends_to_end_of_line_p
3085 || force_p)
3087 x_clear_glyph_string_rect (s, s->x, s->y + s->face->box_line_width,
3088 s->background_width,
3089 s->height - 2 * s->face->box_line_width);
3090 s->background_filled_p = 1;
3096 /* Draw the foreground of glyph string S. */
3098 static void
3099 x_draw_glyph_string_foreground (s)
3100 struct glyph_string *s;
3102 int i, x;
3104 /* If first glyph of S has a left box line, start drawing the text
3105 of S to the right of that box line. */
3106 if (s->face->box != FACE_NO_BOX
3107 && s->first_glyph->left_box_line_p)
3108 x = s->x + s->face->box_line_width;
3109 else
3110 x = s->x;
3112 /* Draw characters of S as rectangles if S's font could not be
3113 loaded. */
3114 if (s->font_not_found_p)
3116 for (i = 0; i < s->nchars; ++i)
3118 struct glyph *g = s->first_glyph + i;
3119 XDrawRectangle (s->display, s->window,
3120 s->gc, x, s->y, g->pixel_width - 1,
3121 s->height - 1);
3122 x += g->pixel_width;
3125 else
3127 char *char1b = (char *) s->char2b;
3128 int boff = s->font_info->baseline_offset;
3130 if (s->font_info->vertical_centering)
3131 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3133 /* If we can use 8-bit functions, condense S->char2b. */
3134 if (!s->two_byte_p)
3135 for (i = 0; i < s->nchars; ++i)
3136 char1b[i] = s->char2b[i].byte2;
3138 /* Draw text with XDrawString if background has already been
3139 filled. Otherwise, use XDrawImageString. (Note that
3140 XDrawImageString is usually faster than XDrawString.) Always
3141 use XDrawImageString when drawing the cursor so that there is
3142 no chance that characters under a box cursor are invisible. */
3143 if (s->for_overlaps_p
3144 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3146 /* Draw characters with 16-bit or 8-bit functions. */
3147 if (s->two_byte_p)
3148 XDrawString16 (s->display, s->window, s->gc, x,
3149 s->ybase - boff, s->char2b, s->nchars);
3150 else
3151 XDrawString (s->display, s->window, s->gc, x,
3152 s->ybase - boff, char1b, s->nchars);
3154 else
3156 if (s->two_byte_p)
3157 XDrawImageString16 (s->display, s->window, s->gc, x,
3158 s->ybase - boff, s->char2b, s->nchars);
3159 else
3160 XDrawImageString (s->display, s->window, s->gc, x,
3161 s->ybase - boff, char1b, s->nchars);
3166 /* Draw the foreground of composite glyph string S. */
3168 static void
3169 x_draw_composite_glyph_string_foreground (s)
3170 struct glyph_string *s;
3172 int i, x;
3174 /* If first glyph of S has a left box line, start drawing the text
3175 of S to the right of that box line. */
3176 if (s->face->box != FACE_NO_BOX
3177 && s->first_glyph->left_box_line_p)
3178 x = s->x + s->face->box_line_width;
3179 else
3180 x = s->x;
3182 /* S is a glyph string for a composition. S->gidx is the index of
3183 the first character drawn for glyphs of this composition.
3184 S->gidx == 0 means we are drawing the very first character of
3185 this composition. */
3187 /* Draw a rectangle for the composition if the font for the very
3188 first character of the composition could not be loaded. */
3189 if (s->font_not_found_p)
3191 if (s->gidx == 0)
3192 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3193 s->width - 1, s->height - 1);
3195 else
3197 for (i = 0; i < s->nchars; i++, ++s->gidx)
3198 XDrawString16 (s->display, s->window, s->gc,
3199 x + s->cmp->offsets[s->gidx * 2],
3200 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3201 s->char2b + i, 1);
3206 #ifdef USE_X_TOOLKIT
3208 static struct frame *x_frame_of_widget P_ ((Widget));
3211 /* Return the frame on which widget WIDGET is used.. Abort if frame
3212 cannot be determined. */
3214 static struct frame *
3215 x_frame_of_widget (widget)
3216 Widget widget;
3218 struct x_display_info *dpyinfo;
3219 Lisp_Object tail;
3220 struct frame *f;
3222 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3224 /* Find the top-level shell of the widget. Note that this function
3225 can be called when the widget is not yet realized, so XtWindow
3226 (widget) == 0. That's the reason we can't simply use
3227 x_any_window_to_frame. */
3228 while (!XtIsTopLevelShell (widget))
3229 widget = XtParent (widget);
3231 /* Look for a frame with that top-level widget. Allocate the color
3232 on that frame to get the right gamma correction value. */
3233 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3234 if (GC_FRAMEP (XCAR (tail))
3235 && (f = XFRAME (XCAR (tail)),
3236 (f->output_data.nothing != 1
3237 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3238 && f->output_data.x->widget == widget)
3239 return f;
3241 abort ();
3245 /* Allocate the color COLOR->pixel on the screen and display of
3246 widget WIDGET in colormap CMAP. If an exact match cannot be
3247 allocated, try the nearest color available. Value is non-zero
3248 if successful. This is called from lwlib. */
3251 x_alloc_nearest_color_for_widget (widget, cmap, color)
3252 Widget widget;
3253 Colormap cmap;
3254 XColor *color;
3256 struct frame *f = x_frame_of_widget (widget);
3257 return x_alloc_nearest_color (f, cmap, color);
3261 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3262 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3263 If this produces the same color as PIXEL, try a color where all RGB
3264 values have DELTA added. Return the allocated color in *PIXEL.
3265 DISPLAY is the X display, CMAP is the colormap to operate on.
3266 Value is non-zero if successful. */
3269 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3270 Widget widget;
3271 Display *display;
3272 Colormap cmap;
3273 unsigned long *pixel;
3274 double factor;
3275 int delta;
3277 struct frame *f = x_frame_of_widget (widget);
3278 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3282 #endif /* USE_X_TOOLKIT */
3285 /* Value is an array of XColor structures for the contents of the
3286 color map of frame F. Set *NCELLS to the size of the array.
3287 Note that this probably shouldn't be called for large color maps,
3288 say a 24-bit TrueColor map. */
3290 static const XColor *
3291 x_color_cells (f, ncells)
3292 struct frame *f;
3293 int *ncells;
3295 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3297 if (dpyinfo->color_cells == NULL)
3299 Display *display = FRAME_X_DISPLAY (f);
3300 Screen *screen = FRAME_X_SCREEN (f);
3301 int i;
3303 dpyinfo->ncolor_cells
3304 = XDisplayCells (display, XScreenNumberOfScreen (screen));
3305 dpyinfo->color_cells
3306 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3307 * sizeof *dpyinfo->color_cells);
3309 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3310 dpyinfo->color_cells[i].pixel = i;
3312 XQueryColors (display, FRAME_X_COLORMAP (f),
3313 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3316 *ncells = dpyinfo->ncolor_cells;
3317 return dpyinfo->color_cells;
3321 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3322 colors in COLORS. Use cached information, if available. */
3324 void
3325 x_query_colors (f, colors, ncolors)
3326 struct frame *f;
3327 XColor *colors;
3328 int ncolors;
3330 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3332 if (dpyinfo->color_cells)
3334 int i;
3335 for (i = 0; i < ncolors; ++i)
3337 unsigned long pixel = colors[i].pixel;
3338 xassert (pixel < dpyinfo->ncolor_cells);
3339 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3340 colors[i] = dpyinfo->color_cells[pixel];
3343 else
3344 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3348 /* On frame F, translate pixel color to RGB values for the color in
3349 COLOR. Use cached information, if available. */
3351 void
3352 x_query_color (f, color)
3353 struct frame *f;
3354 XColor *color;
3356 x_query_colors (f, color, 1);
3360 /* Allocate the color COLOR->pixel on SCREEN of DISPLAY, colormap
3361 CMAP. If an exact match can't be allocated, try the nearest color
3362 available. Value is non-zero if successful. Set *COLOR to the
3363 color allocated. */
3366 x_alloc_nearest_color (f, cmap, color)
3367 struct frame *f;
3368 Colormap cmap;
3369 XColor *color;
3371 Display *display = FRAME_X_DISPLAY (f);
3372 Screen *screen = FRAME_X_SCREEN (f);
3373 int rc;
3375 gamma_correct (f, color);
3376 rc = XAllocColor (display, cmap, color);
3377 if (rc == 0)
3379 /* If we got to this point, the colormap is full, so we're going
3380 to try to get the next closest color. The algorithm used is
3381 a least-squares matching, which is what X uses for closest
3382 color matching with StaticColor visuals. */
3383 int nearest, i;
3384 unsigned long nearest_delta = ~0;
3385 int ncells;
3386 const XColor *cells = x_color_cells (f, &ncells);
3388 for (nearest = i = 0; i < ncells; ++i)
3390 long dred = (color->red >> 8) - (cells[i].red >> 8);
3391 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3392 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3393 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3395 if (delta < nearest_delta)
3397 nearest = i;
3398 nearest_delta = delta;
3402 color->red = cells[nearest].red;
3403 color->green = cells[nearest].green;
3404 color->blue = cells[nearest].blue;
3405 rc = XAllocColor (display, cmap, color);
3407 else
3409 /* If allocation succeeded, and the allocated pixel color is not
3410 equal to a cached pixel color recorded earlier, there was a
3411 change in the colormap, so clear the color cache. */
3412 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3413 XColor *cached_color;
3415 if (dpyinfo->color_cells
3416 && (cached_color = &dpyinfo->color_cells[color->pixel],
3417 (cached_color->red != color->red
3418 || cached_color->blue != color->blue
3419 || cached_color->green != color->green)))
3421 xfree (dpyinfo->color_cells);
3422 dpyinfo->color_cells = NULL;
3423 dpyinfo->ncolor_cells = 0;
3427 #ifdef DEBUG_X_COLORS
3428 if (rc)
3429 register_color (color->pixel);
3430 #endif /* DEBUG_X_COLORS */
3432 return rc;
3436 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3437 It's necessary to do this instead of just using PIXEL directly to
3438 get color reference counts right. */
3440 unsigned long
3441 x_copy_color (f, pixel)
3442 struct frame *f;
3443 unsigned long pixel;
3445 XColor color;
3447 color.pixel = pixel;
3448 BLOCK_INPUT;
3449 x_query_color (f, &color);
3450 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3451 UNBLOCK_INPUT;
3452 #ifdef DEBUG_X_COLORS
3453 register_color (pixel);
3454 #endif
3455 return color.pixel;
3459 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3460 It's necessary to do this instead of just using PIXEL directly to
3461 get color reference counts right. */
3463 unsigned long
3464 x_copy_dpy_color (dpy, cmap, pixel)
3465 Display *dpy;
3466 Colormap cmap;
3467 unsigned long pixel;
3469 XColor color;
3471 color.pixel = pixel;
3472 BLOCK_INPUT;
3473 XQueryColor (dpy, cmap, &color);
3474 XAllocColor (dpy, cmap, &color);
3475 UNBLOCK_INPUT;
3476 #ifdef DEBUG_X_COLORS
3477 register_color (pixel);
3478 #endif
3479 return color.pixel;
3483 /* Brightness beyond which a color won't have its highlight brightness
3484 boosted.
3486 Nominally, highlight colors for `3d' faces are calculated by
3487 brightening an object's color by a constant scale factor, but this
3488 doesn't yield good results for dark colors, so for colors who's
3489 brightness is less than this value (on a scale of 0-65535) have an
3490 use an additional additive factor.
3492 The value here is set so that the default menu-bar/mode-line color
3493 (grey75) will not have its highlights changed at all. */
3494 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3497 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3498 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3499 If this produces the same color as PIXEL, try a color where all RGB
3500 values have DELTA added. Return the allocated color in *PIXEL.
3501 DISPLAY is the X display, CMAP is the colormap to operate on.
3502 Value is non-zero if successful. */
3504 static int
3505 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3506 struct frame *f;
3507 Display *display;
3508 Colormap cmap;
3509 unsigned long *pixel;
3510 double factor;
3511 int delta;
3513 XColor color, new;
3514 long bright;
3515 int success_p;
3517 /* Get RGB color values. */
3518 color.pixel = *pixel;
3519 x_query_color (f, &color);
3521 /* Change RGB values by specified FACTOR. Avoid overflow! */
3522 xassert (factor >= 0);
3523 new.red = min (0xffff, factor * color.red);
3524 new.green = min (0xffff, factor * color.green);
3525 new.blue = min (0xffff, factor * color.blue);
3527 /* Calculate brightness of COLOR. */
3528 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3530 /* We only boost colors that are darker than
3531 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3532 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3533 /* Make an additive adjustment to NEW, because it's dark enough so
3534 that scaling by FACTOR alone isn't enough. */
3536 /* How far below the limit this color is (0 - 1, 1 being darker). */
3537 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3538 /* The additive adjustment. */
3539 int min_delta = delta * dimness * factor / 2;
3541 if (factor < 1)
3543 new.red = max (0, new.red - min_delta);
3544 new.green = max (0, new.green - min_delta);
3545 new.blue = max (0, new.blue - min_delta);
3547 else
3549 new.red = min (0xffff, min_delta + new.red);
3550 new.green = min (0xffff, min_delta + new.green);
3551 new.blue = min (0xffff, min_delta + new.blue);
3555 /* Try to allocate the color. */
3556 success_p = x_alloc_nearest_color (f, cmap, &new);
3557 if (success_p)
3559 if (new.pixel == *pixel)
3561 /* If we end up with the same color as before, try adding
3562 delta to the RGB values. */
3563 x_free_colors (f, &new.pixel, 1);
3565 new.red = min (0xffff, delta + color.red);
3566 new.green = min (0xffff, delta + color.green);
3567 new.blue = min (0xffff, delta + color.blue);
3568 success_p = x_alloc_nearest_color (f, cmap, &new);
3570 else
3571 success_p = 1;
3572 *pixel = new.pixel;
3575 return success_p;
3579 /* Set up the foreground color for drawing relief lines of glyph
3580 string S. RELIEF is a pointer to a struct relief containing the GC
3581 with which lines will be drawn. Use a color that is FACTOR or
3582 DELTA lighter or darker than the relief's background which is found
3583 in S->f->output_data.x->relief_background. If such a color cannot
3584 be allocated, use DEFAULT_PIXEL, instead. */
3586 static void
3587 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3588 struct frame *f;
3589 struct relief *relief;
3590 double factor;
3591 int delta;
3592 unsigned long default_pixel;
3594 XGCValues xgcv;
3595 struct x_output *di = f->output_data.x;
3596 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3597 unsigned long pixel;
3598 unsigned long background = di->relief_background;
3599 Colormap cmap = FRAME_X_COLORMAP (f);
3600 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3601 Display *dpy = FRAME_X_DISPLAY (f);
3603 xgcv.graphics_exposures = False;
3604 xgcv.line_width = 1;
3606 /* Free previously allocated color. The color cell will be reused
3607 when it has been freed as many times as it was allocated, so this
3608 doesn't affect faces using the same colors. */
3609 if (relief->gc
3610 && relief->allocated_p)
3612 x_free_colors (f, &relief->pixel, 1);
3613 relief->allocated_p = 0;
3616 /* Allocate new color. */
3617 xgcv.foreground = default_pixel;
3618 pixel = background;
3619 if (dpyinfo->n_planes != 1
3620 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3622 relief->allocated_p = 1;
3623 xgcv.foreground = relief->pixel = pixel;
3626 if (relief->gc == 0)
3628 xgcv.stipple = dpyinfo->gray;
3629 mask |= GCStipple;
3630 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3632 else
3633 XChangeGC (dpy, relief->gc, mask, &xgcv);
3637 /* Set up colors for the relief lines around glyph string S. */
3639 static void
3640 x_setup_relief_colors (s)
3641 struct glyph_string *s;
3643 struct x_output *di = s->f->output_data.x;
3644 unsigned long color;
3646 if (s->face->use_box_color_for_shadows_p)
3647 color = s->face->box_color;
3648 else
3650 XGCValues xgcv;
3652 /* Get the background color of the face. */
3653 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3654 color = xgcv.background;
3657 if (di->white_relief.gc == 0
3658 || color != di->relief_background)
3660 di->relief_background = color;
3661 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3662 WHITE_PIX_DEFAULT (s->f));
3663 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3664 BLACK_PIX_DEFAULT (s->f));
3669 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3670 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3671 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3672 relief. LEFT_P non-zero means draw a relief on the left side of
3673 the rectangle. RIGHT_P non-zero means draw a relief on the right
3674 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3675 when drawing. */
3677 static void
3678 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3679 raised_p, left_p, right_p, clip_rect)
3680 struct frame *f;
3681 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3682 XRectangle *clip_rect;
3684 int i;
3685 GC gc;
3687 if (raised_p)
3688 gc = f->output_data.x->white_relief.gc;
3689 else
3690 gc = f->output_data.x->black_relief.gc;
3691 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3693 /* Top. */
3694 for (i = 0; i < width; ++i)
3695 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3696 left_x + i * left_p, top_y + i,
3697 right_x + 1 - i * right_p, top_y + i);
3699 /* Left. */
3700 if (left_p)
3701 for (i = 0; i < width; ++i)
3702 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3703 left_x + i, top_y + i, left_x + i, bottom_y - i);
3705 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3706 if (raised_p)
3707 gc = f->output_data.x->black_relief.gc;
3708 else
3709 gc = f->output_data.x->white_relief.gc;
3710 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, clip_rect, 1, Unsorted);
3712 /* Bottom. */
3713 for (i = 0; i < width; ++i)
3714 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3715 left_x + i * left_p, bottom_y - i,
3716 right_x + 1 - i * right_p, bottom_y - i);
3718 /* Right. */
3719 if (right_p)
3720 for (i = 0; i < width; ++i)
3721 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
3722 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3724 XSetClipMask (FRAME_X_DISPLAY (f), gc, None);
3728 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3729 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3730 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3731 left side of the rectangle. RIGHT_P non-zero means draw a line
3732 on the right side of the rectangle. CLIP_RECT is the clipping
3733 rectangle to use when drawing. */
3735 static void
3736 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3737 left_p, right_p, clip_rect)
3738 struct glyph_string *s;
3739 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3740 XRectangle *clip_rect;
3742 XGCValues xgcv;
3744 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3745 XSetForeground (s->display, s->gc, s->face->box_color);
3746 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3748 /* Top. */
3749 XFillRectangle (s->display, s->window, s->gc,
3750 left_x, top_y, right_x - left_x + 1, width);
3752 /* Left. */
3753 if (left_p)
3754 XFillRectangle (s->display, s->window, s->gc,
3755 left_x, top_y, width, bottom_y - top_y + 1);
3757 /* Bottom. */
3758 XFillRectangle (s->display, s->window, s->gc,
3759 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3761 /* Right. */
3762 if (right_p)
3763 XFillRectangle (s->display, s->window, s->gc,
3764 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3766 XSetForeground (s->display, s->gc, xgcv.foreground);
3767 XSetClipMask (s->display, s->gc, None);
3771 /* Draw a box around glyph string S. */
3773 static void
3774 x_draw_glyph_string_box (s)
3775 struct glyph_string *s;
3777 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3778 int left_p, right_p;
3779 struct glyph *last_glyph;
3780 XRectangle clip_rect;
3782 last_x = window_box_right (s->w, s->area);
3783 if (s->row->full_width_p
3784 && !s->w->pseudo_window_p)
3786 last_x += FRAME_X_RIGHT_FLAGS_AREA_WIDTH (s->f);
3787 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3788 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3791 /* The glyph that may have a right box line. */
3792 last_glyph = (s->cmp || s->img
3793 ? s->first_glyph
3794 : s->first_glyph + s->nchars - 1);
3796 width = s->face->box_line_width;
3797 raised_p = s->face->box == FACE_RAISED_BOX;
3798 left_x = s->x;
3799 right_x = ((s->row->full_width_p
3800 ? last_x - 1
3801 : min (last_x, s->x + s->background_width) - 1));
3802 top_y = s->y;
3803 bottom_y = top_y + s->height - 1;
3805 left_p = (s->first_glyph->left_box_line_p
3806 || (s->hl == DRAW_MOUSE_FACE
3807 && (s->prev == NULL
3808 || s->prev->hl != s->hl)));
3809 right_p = (last_glyph->right_box_line_p
3810 || (s->hl == DRAW_MOUSE_FACE
3811 && (s->next == NULL
3812 || s->next->hl != s->hl)));
3814 x_get_glyph_string_clip_rect (s, &clip_rect);
3816 if (s->face->box == FACE_SIMPLE_BOX)
3817 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3818 left_p, right_p, &clip_rect);
3819 else
3821 x_setup_relief_colors (s);
3822 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
3823 width, raised_p, left_p, right_p, &clip_rect);
3828 /* Draw foreground of image glyph string S. */
3830 static void
3831 x_draw_image_foreground (s)
3832 struct glyph_string *s;
3834 int x;
3835 int y = s->ybase - image_ascent (s->img, s->face);
3837 /* If first glyph of S has a left box line, start drawing it to the
3838 right of that line. */
3839 if (s->face->box != FACE_NO_BOX
3840 && s->first_glyph->left_box_line_p)
3841 x = s->x + s->face->box_line_width;
3842 else
3843 x = s->x;
3845 /* If there is a margin around the image, adjust x- and y-position
3846 by that margin. */
3847 if (s->img->margin)
3849 x += s->img->margin;
3850 y += s->img->margin;
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 if (s->img->margin)
3938 x += s->img->margin;
3939 y += s->img->margin;
3942 if (s->hl == DRAW_IMAGE_SUNKEN
3943 || s->hl == DRAW_IMAGE_RAISED)
3945 thick = tool_bar_button_relief > 0 ? tool_bar_button_relief : 3;
3946 raised_p = s->hl == DRAW_IMAGE_RAISED;
3948 else
3950 thick = abs (s->img->relief);
3951 raised_p = s->img->relief > 0;
3954 x0 = x - thick;
3955 y0 = y - thick;
3956 x1 = x + s->img->width + thick - 1;
3957 y1 = y + s->img->height + thick - 1;
3959 x_setup_relief_colors (s);
3960 x_get_glyph_string_clip_rect (s, &r);
3961 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
3965 /* Draw the foreground of image glyph string S to PIXMAP. */
3967 static void
3968 x_draw_image_foreground_1 (s, pixmap)
3969 struct glyph_string *s;
3970 Pixmap pixmap;
3972 int x;
3973 int y = s->ybase - s->y - image_ascent (s->img, s->face);
3975 /* If first glyph of S has a left box line, start drawing it to the
3976 right of that line. */
3977 if (s->face->box != FACE_NO_BOX
3978 && s->first_glyph->left_box_line_p)
3979 x = s->face->box_line_width;
3980 else
3981 x = 0;
3983 /* If there is a margin around the image, adjust x- and y-position
3984 by that margin. */
3985 if (s->img->margin)
3987 x += s->img->margin;
3988 y += s->img->margin;
3991 if (s->img->pixmap)
3993 if (s->img->mask)
3995 /* We can't set both a clip mask and use XSetClipRectangles
3996 because the latter also sets a clip mask. We also can't
3997 trust on the shape extension to be available
3998 (XShapeCombineRegion). So, compute the rectangle to draw
3999 manually. */
4000 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4001 | GCFunction);
4002 XGCValues xgcv;
4004 xgcv.clip_mask = s->img->mask;
4005 xgcv.clip_x_origin = x;
4006 xgcv.clip_y_origin = y;
4007 xgcv.function = GXcopy;
4008 XChangeGC (s->display, s->gc, mask, &xgcv);
4010 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4011 0, 0, s->img->width, s->img->height, x, y);
4012 XSetClipMask (s->display, s->gc, None);
4014 else
4016 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4017 0, 0, s->img->width, s->img->height, x, y);
4019 /* When the image has a mask, we can expect that at
4020 least part of a mouse highlight or a block cursor will
4021 be visible. If the image doesn't have a mask, make
4022 a block cursor visible by drawing a rectangle around
4023 the image. I believe it's looking better if we do
4024 nothing here for mouse-face. */
4025 if (s->hl == DRAW_CURSOR)
4026 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4027 s->img->width - 1, s->img->height - 1);
4030 else
4031 /* Draw a rectangle if image could not be loaded. */
4032 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4033 s->img->width - 1, s->img->height - 1);
4037 /* Draw part of the background of glyph string S. X, Y, W, and H
4038 give the rectangle to draw. */
4040 static void
4041 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4042 struct glyph_string *s;
4043 int x, y, w, h;
4045 if (s->stippled_p)
4047 /* Fill background with a stipple pattern. */
4048 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4049 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4050 XSetFillStyle (s->display, s->gc, FillSolid);
4052 else
4053 x_clear_glyph_string_rect (s, x, y, w, h);
4057 /* Draw image glyph string S.
4059 s->y
4060 s->x +-------------------------
4061 | s->face->box
4063 | +-------------------------
4064 | | s->img->margin
4066 | | +-------------------
4067 | | | the image
4071 static void
4072 x_draw_image_glyph_string (s)
4073 struct glyph_string *s;
4075 int x, y;
4076 int box_line_width = s->face->box_line_width;
4077 int margin = s->img->margin;
4078 int height;
4079 Pixmap pixmap = None;
4081 height = s->height - 2 * box_line_width;
4083 /* Fill background with face under the image. Do it only if row is
4084 taller than image or if image has a clip mask to reduce
4085 flickering. */
4086 s->stippled_p = s->face->stipple != 0;
4087 if (height > s->img->height
4088 || margin
4089 || s->img->mask
4090 || s->img->pixmap == 0
4091 || s->width != s->background_width)
4093 if (box_line_width && s->first_glyph->left_box_line_p)
4094 x = s->x + box_line_width;
4095 else
4096 x = s->x;
4098 y = s->y + box_line_width;
4100 if (s->img->mask)
4102 /* Create a pixmap as large as the glyph string. Fill it
4103 with the background color. Copy the image to it, using
4104 its mask. Copy the temporary pixmap to the display. */
4105 Screen *screen = FRAME_X_SCREEN (s->f);
4106 int depth = DefaultDepthOfScreen (screen);
4108 /* Create a pixmap as large as the glyph string. */
4109 pixmap = XCreatePixmap (s->display, s->window,
4110 s->background_width,
4111 s->height, depth);
4113 /* Don't clip in the following because we're working on the
4114 pixmap. */
4115 XSetClipMask (s->display, s->gc, None);
4117 /* Fill the pixmap with the background color/stipple. */
4118 if (s->stippled_p)
4120 /* Fill background with a stipple pattern. */
4121 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4122 XFillRectangle (s->display, pixmap, s->gc,
4123 0, 0, s->background_width, s->height);
4124 XSetFillStyle (s->display, s->gc, FillSolid);
4126 else
4128 XGCValues xgcv;
4129 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4130 &xgcv);
4131 XSetForeground (s->display, s->gc, xgcv.background);
4132 XFillRectangle (s->display, pixmap, s->gc,
4133 0, 0, s->background_width, s->height);
4134 XSetForeground (s->display, s->gc, xgcv.foreground);
4137 else
4138 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4140 s->background_filled_p = 1;
4143 /* Draw the foreground. */
4144 if (pixmap != None)
4146 x_draw_image_foreground_1 (s, pixmap);
4147 x_set_glyph_string_clipping (s);
4148 XCopyArea (s->display, pixmap, s->window, s->gc,
4149 0, 0, s->background_width, s->height, s->x, s->y);
4150 XFreePixmap (s->display, pixmap);
4152 else
4153 x_draw_image_foreground (s);
4155 /* If we must draw a relief around the image, do it. */
4156 if (s->img->relief
4157 || s->hl == DRAW_IMAGE_RAISED
4158 || s->hl == DRAW_IMAGE_SUNKEN)
4159 x_draw_image_relief (s);
4163 /* Draw stretch glyph string S. */
4165 static void
4166 x_draw_stretch_glyph_string (s)
4167 struct glyph_string *s;
4169 xassert (s->first_glyph->type == STRETCH_GLYPH);
4170 s->stippled_p = s->face->stipple != 0;
4172 if (s->hl == DRAW_CURSOR
4173 && !x_stretch_cursor_p)
4175 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4176 as wide as the stretch glyph. */
4177 int width = min (CANON_X_UNIT (s->f), s->background_width);
4179 /* Draw cursor. */
4180 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4182 /* Clear rest using the GC of the original non-cursor face. */
4183 if (width < s->background_width)
4185 GC gc = s->face->gc;
4186 int x = s->x + width, y = s->y;
4187 int w = s->background_width - width, h = s->height;
4188 XRectangle r;
4190 x_get_glyph_string_clip_rect (s, &r);
4191 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4193 if (s->face->stipple)
4195 /* Fill background with a stipple pattern. */
4196 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4197 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4198 XSetFillStyle (s->display, gc, FillSolid);
4200 else
4202 XGCValues xgcv;
4203 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4204 XSetForeground (s->display, gc, xgcv.background);
4205 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4206 XSetForeground (s->display, gc, xgcv.foreground);
4210 else
4211 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4212 s->height);
4214 s->background_filled_p = 1;
4218 /* Draw glyph string S. */
4220 static void
4221 x_draw_glyph_string (s)
4222 struct glyph_string *s;
4224 /* If S draws into the background of its successor, draw the
4225 background of the successor first so that S can draw into it.
4226 This makes S->next use XDrawString instead of XDrawImageString. */
4227 if (s->next && s->right_overhang && !s->for_overlaps_p)
4229 xassert (s->next->img == NULL);
4230 x_set_glyph_string_gc (s->next);
4231 x_set_glyph_string_clipping (s->next);
4232 x_draw_glyph_string_background (s->next, 1);
4235 /* Set up S->gc, set clipping and draw S. */
4236 x_set_glyph_string_gc (s);
4237 x_set_glyph_string_clipping (s);
4239 switch (s->first_glyph->type)
4241 case IMAGE_GLYPH:
4242 x_draw_image_glyph_string (s);
4243 break;
4245 case STRETCH_GLYPH:
4246 x_draw_stretch_glyph_string (s);
4247 break;
4249 case CHAR_GLYPH:
4250 if (s->for_overlaps_p)
4251 s->background_filled_p = 1;
4252 else
4253 x_draw_glyph_string_background (s, 0);
4254 x_draw_glyph_string_foreground (s);
4255 break;
4257 case COMPOSITE_GLYPH:
4258 if (s->for_overlaps_p || s->gidx > 0)
4259 s->background_filled_p = 1;
4260 else
4261 x_draw_glyph_string_background (s, 1);
4262 x_draw_composite_glyph_string_foreground (s);
4263 break;
4265 default:
4266 abort ();
4269 if (!s->for_overlaps_p)
4271 /* Draw underline. */
4272 if (s->face->underline_p)
4274 unsigned long tem, h;
4275 int y;
4277 /* Get the underline thickness. Default is 1 pixel. */
4278 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4279 h = 1;
4281 /* Get the underline position. This is the recommended
4282 vertical offset in pixels from the baseline to the top of
4283 the underline. This is a signed value according to the
4284 specs, and its default is
4286 ROUND ((maximum descent) / 2), with
4287 ROUND(x) = floor (x + 0.5) */
4289 if (XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4290 y = s->ybase + (long) tem;
4291 else if (s->face->font)
4292 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4293 else
4294 y = s->height - h;
4296 if (s->face->underline_defaulted_p)
4297 XFillRectangle (s->display, s->window, s->gc,
4298 s->x, y, s->width, h);
4299 else
4301 XGCValues xgcv;
4302 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4303 XSetForeground (s->display, s->gc, s->face->underline_color);
4304 XFillRectangle (s->display, s->window, s->gc,
4305 s->x, y, s->width, h);
4306 XSetForeground (s->display, s->gc, xgcv.foreground);
4310 /* Draw overline. */
4311 if (s->face->overline_p)
4313 unsigned long dy = 0, h = 1;
4315 if (s->face->overline_color_defaulted_p)
4316 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4317 s->width, h);
4318 else
4320 XGCValues xgcv;
4321 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4322 XSetForeground (s->display, s->gc, s->face->overline_color);
4323 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4324 s->width, h);
4325 XSetForeground (s->display, s->gc, xgcv.foreground);
4329 /* Draw strike-through. */
4330 if (s->face->strike_through_p)
4332 unsigned long h = 1;
4333 unsigned long dy = (s->height - h) / 2;
4335 if (s->face->strike_through_color_defaulted_p)
4336 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4337 s->width, h);
4338 else
4340 XGCValues xgcv;
4341 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4342 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4343 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4344 s->width, h);
4345 XSetForeground (s->display, s->gc, xgcv.foreground);
4349 /* Draw relief. */
4350 if (s->face->box != FACE_NO_BOX)
4351 x_draw_glyph_string_box (s);
4354 /* Reset clipping. */
4355 XSetClipMask (s->display, s->gc, None);
4359 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4360 struct face **, int));
4363 /* Fill glyph string S with composition components specified by S->cmp.
4365 FACES is an array of faces for all components of this composition.
4366 S->gidx is the index of the first component for S.
4367 OVERLAPS_P non-zero means S should draw the foreground only, and
4368 use its physical height for clipping.
4370 Value is the index of a component not in S. */
4372 static int
4373 x_fill_composite_glyph_string (s, faces, overlaps_p)
4374 struct glyph_string *s;
4375 struct face **faces;
4376 int overlaps_p;
4378 int i;
4380 xassert (s);
4382 s->for_overlaps_p = overlaps_p;
4384 s->face = faces[s->gidx];
4385 s->font = s->face->font;
4386 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4388 /* For all glyphs of this composition, starting at the offset
4389 S->gidx, until we reach the end of the definition or encounter a
4390 glyph that requires the different face, add it to S. */
4391 ++s->nchars;
4392 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4393 ++s->nchars;
4395 /* All glyph strings for the same composition has the same width,
4396 i.e. the width set for the first component of the composition. */
4398 s->width = s->first_glyph->pixel_width;
4400 /* If the specified font could not be loaded, use the frame's
4401 default font, but record the fact that we couldn't load it in
4402 the glyph string so that we can draw rectangles for the
4403 characters of the glyph string. */
4404 if (s->font == NULL)
4406 s->font_not_found_p = 1;
4407 s->font = FRAME_FONT (s->f);
4410 /* Adjust base line for subscript/superscript text. */
4411 s->ybase += s->first_glyph->voffset;
4413 xassert (s->face && s->face->gc);
4415 /* This glyph string must always be drawn with 16-bit functions. */
4416 s->two_byte_p = 1;
4418 return s->gidx + s->nchars;
4422 /* Fill glyph string S from a sequence of character glyphs.
4424 FACE_ID is the face id of the string. START is the index of the
4425 first glyph to consider, END is the index of the last + 1.
4426 OVERLAPS_P non-zero means S should draw the foreground only, and
4427 use its physical height for clipping.
4429 Value is the index of the first glyph not in S. */
4431 static int
4432 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4433 struct glyph_string *s;
4434 int face_id;
4435 int start, end, overlaps_p;
4437 struct glyph *glyph, *last;
4438 int voffset;
4439 int glyph_not_available_p;
4441 xassert (s->f == XFRAME (s->w->frame));
4442 xassert (s->nchars == 0);
4443 xassert (start >= 0 && end > start);
4445 s->for_overlaps_p = overlaps_p,
4446 glyph = s->row->glyphs[s->area] + start;
4447 last = s->row->glyphs[s->area] + end;
4448 voffset = glyph->voffset;
4450 glyph_not_available_p = glyph->glyph_not_available_p;
4452 while (glyph < last
4453 && glyph->type == CHAR_GLYPH
4454 && glyph->voffset == voffset
4455 /* Same face id implies same font, nowadays. */
4456 && glyph->face_id == face_id
4457 && glyph->glyph_not_available_p == glyph_not_available_p)
4459 int two_byte_p;
4461 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4462 s->char2b + s->nchars,
4463 &two_byte_p);
4464 s->two_byte_p = two_byte_p;
4465 ++s->nchars;
4466 xassert (s->nchars <= end - start);
4467 s->width += glyph->pixel_width;
4468 ++glyph;
4471 s->font = s->face->font;
4472 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4474 /* If the specified font could not be loaded, use the frame's font,
4475 but record the fact that we couldn't load it in
4476 S->font_not_found_p so that we can draw rectangles for the
4477 characters of the glyph string. */
4478 if (s->font == NULL || glyph_not_available_p)
4480 s->font_not_found_p = 1;
4481 s->font = FRAME_FONT (s->f);
4484 /* Adjust base line for subscript/superscript text. */
4485 s->ybase += voffset;
4487 xassert (s->face && s->face->gc);
4488 return glyph - s->row->glyphs[s->area];
4492 /* Fill glyph string S from image glyph S->first_glyph. */
4494 static void
4495 x_fill_image_glyph_string (s)
4496 struct glyph_string *s;
4498 xassert (s->first_glyph->type == IMAGE_GLYPH);
4499 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4500 xassert (s->img);
4501 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4502 s->font = s->face->font;
4503 s->width = s->first_glyph->pixel_width;
4505 /* Adjust base line for subscript/superscript text. */
4506 s->ybase += s->first_glyph->voffset;
4510 /* Fill glyph string S from a sequence of stretch glyphs.
4512 ROW is the glyph row in which the glyphs are found, AREA is the
4513 area within the row. START is the index of the first glyph to
4514 consider, END is the index of the last + 1.
4516 Value is the index of the first glyph not in S. */
4518 static int
4519 x_fill_stretch_glyph_string (s, row, area, start, end)
4520 struct glyph_string *s;
4521 struct glyph_row *row;
4522 enum glyph_row_area area;
4523 int start, end;
4525 struct glyph *glyph, *last;
4526 int voffset, face_id;
4528 xassert (s->first_glyph->type == STRETCH_GLYPH);
4530 glyph = s->row->glyphs[s->area] + start;
4531 last = s->row->glyphs[s->area] + end;
4532 face_id = glyph->face_id;
4533 s->face = FACE_FROM_ID (s->f, face_id);
4534 s->font = s->face->font;
4535 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4536 s->width = glyph->pixel_width;
4537 voffset = glyph->voffset;
4539 for (++glyph;
4540 (glyph < last
4541 && glyph->type == STRETCH_GLYPH
4542 && glyph->voffset == voffset
4543 && glyph->face_id == face_id);
4544 ++glyph)
4545 s->width += glyph->pixel_width;
4547 /* Adjust base line for subscript/superscript text. */
4548 s->ybase += voffset;
4550 xassert (s->face && s->face->gc);
4551 return glyph - s->row->glyphs[s->area];
4555 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4556 of XChar2b structures for S; it can't be allocated in
4557 x_init_glyph_string because it must be allocated via `alloca'. W
4558 is the window on which S is drawn. ROW and AREA are the glyph row
4559 and area within the row from which S is constructed. START is the
4560 index of the first glyph structure covered by S. HL is a
4561 face-override for drawing S. */
4563 static void
4564 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4565 struct glyph_string *s;
4566 XChar2b *char2b;
4567 struct window *w;
4568 struct glyph_row *row;
4569 enum glyph_row_area area;
4570 int start;
4571 enum draw_glyphs_face hl;
4573 bzero (s, sizeof *s);
4574 s->w = w;
4575 s->f = XFRAME (w->frame);
4576 s->display = FRAME_X_DISPLAY (s->f);
4577 s->window = FRAME_X_WINDOW (s->f);
4578 s->char2b = char2b;
4579 s->hl = hl;
4580 s->row = row;
4581 s->area = area;
4582 s->first_glyph = row->glyphs[area] + start;
4583 s->height = row->height;
4584 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4586 /* Display the internal border below the tool-bar window. */
4587 if (s->w == XWINDOW (s->f->tool_bar_window))
4588 s->y -= s->f->output_data.x->internal_border_width;
4590 s->ybase = s->y + row->ascent;
4594 /* Set background width of glyph string S. START is the index of the
4595 first glyph following S. LAST_X is the right-most x-position + 1
4596 in the drawing area. */
4598 static INLINE void
4599 x_set_glyph_string_background_width (s, start, last_x)
4600 struct glyph_string *s;
4601 int start;
4602 int last_x;
4604 /* If the face of this glyph string has to be drawn to the end of
4605 the drawing area, set S->extends_to_end_of_line_p. */
4606 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4608 if (start == s->row->used[s->area]
4609 && s->hl == DRAW_NORMAL_TEXT
4610 && ((s->area == TEXT_AREA && s->row->fill_line_p)
4611 || s->face->background != default_face->background
4612 || s->face->stipple != default_face->stipple))
4613 s->extends_to_end_of_line_p = 1;
4615 /* If S extends its face to the end of the line, set its
4616 background_width to the distance to the right edge of the drawing
4617 area. */
4618 if (s->extends_to_end_of_line_p)
4619 s->background_width = last_x - s->x + 1;
4620 else
4621 s->background_width = s->width;
4625 /* Add a glyph string for a stretch glyph to the list of strings
4626 between HEAD and TAIL. START is the index of the stretch glyph in
4627 row area AREA of glyph row ROW. END is the index of the last glyph
4628 in that glyph row area. X is the current output position assigned
4629 to the new glyph string constructed. HL overrides that face of the
4630 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4631 is the right-most x-position of the drawing area. */
4633 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4634 and below -- keep them on one line. */
4635 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4636 do \
4638 s = (struct glyph_string *) alloca (sizeof *s); \
4639 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4640 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4641 x_append_glyph_string (&HEAD, &TAIL, s); \
4642 s->x = (X); \
4644 while (0)
4647 /* Add a glyph string for an image glyph to the list of strings
4648 between HEAD and TAIL. START is the index of the image glyph in
4649 row area AREA of glyph row ROW. END is the index of the last glyph
4650 in that glyph row area. X is the current output position assigned
4651 to the new glyph string constructed. HL overrides that face of the
4652 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4653 is the right-most x-position of the drawing area. */
4655 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4656 do \
4658 s = (struct glyph_string *) alloca (sizeof *s); \
4659 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4660 x_fill_image_glyph_string (s); \
4661 x_append_glyph_string (&HEAD, &TAIL, s); \
4662 ++START; \
4663 s->x = (X); \
4665 while (0)
4668 /* Add a glyph string for a sequence of character glyphs to the list
4669 of strings between HEAD and TAIL. START is the index of the first
4670 glyph in row area AREA of glyph row ROW that is part of the new
4671 glyph string. END is the index of the last glyph in that glyph row
4672 area. X is the current output position assigned to the new glyph
4673 string constructed. HL overrides that face of the glyph; e.g. it
4674 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4675 right-most x-position of the drawing area. */
4677 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4678 do \
4680 int c, face_id; \
4681 XChar2b *char2b; \
4683 c = (ROW)->glyphs[AREA][START].u.ch; \
4684 face_id = (ROW)->glyphs[AREA][START].face_id; \
4686 s = (struct glyph_string *) alloca (sizeof *s); \
4687 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4688 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4689 x_append_glyph_string (&HEAD, &TAIL, s); \
4690 s->x = (X); \
4691 START = x_fill_glyph_string (s, face_id, START, END, \
4692 OVERLAPS_P); \
4694 while (0)
4697 /* Add a glyph string for a composite sequence to the list of strings
4698 between HEAD and TAIL. START is the index of the first glyph in
4699 row area AREA of glyph row ROW that is part of the new glyph
4700 string. END is the index of the last glyph in that glyph row area.
4701 X is the current output position assigned to the new glyph string
4702 constructed. HL overrides that face of the glyph; e.g. it is
4703 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4704 x-position of the drawing area. */
4706 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4707 do { \
4708 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4709 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4710 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4711 struct composition *cmp = composition_table[cmp_id]; \
4712 int glyph_len = cmp->glyph_len; \
4713 XChar2b *char2b; \
4714 struct face **faces; \
4715 struct glyph_string *first_s = NULL; \
4716 int n; \
4718 base_face = base_face->ascii_face; \
4719 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4720 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4721 /* At first, fill in `char2b' and `faces'. */ \
4722 for (n = 0; n < glyph_len; n++) \
4724 int c = COMPOSITION_GLYPH (cmp, n); \
4725 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4726 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4727 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4728 this_face_id, char2b + n, 1); \
4731 /* Make glyph_strings for each glyph sequence that is drawable by \
4732 the same face, and append them to HEAD/TAIL. */ \
4733 for (n = 0; n < cmp->glyph_len;) \
4735 s = (struct glyph_string *) alloca (sizeof *s); \
4736 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4737 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4738 s->cmp = cmp; \
4739 s->gidx = n; \
4740 s->x = (X); \
4742 if (n == 0) \
4743 first_s = s; \
4745 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4748 ++START; \
4749 s = first_s; \
4750 } while (0)
4753 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4754 of AREA of glyph row ROW on window W between indices START and END.
4755 HL overrides the face for drawing glyph strings, e.g. it is
4756 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4757 x-positions of the drawing area.
4759 This is an ugly monster macro construct because we must use alloca
4760 to allocate glyph strings (because x_draw_glyphs can be called
4761 asynchronously). */
4763 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4764 do \
4766 HEAD = TAIL = NULL; \
4767 while (START < END) \
4769 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4770 switch (first_glyph->type) \
4772 case CHAR_GLYPH: \
4773 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4774 TAIL, HL, X, LAST_X, \
4775 OVERLAPS_P); \
4776 break; \
4778 case COMPOSITE_GLYPH: \
4779 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
4780 HEAD, TAIL, HL, X, LAST_X,\
4781 OVERLAPS_P); \
4782 break; \
4784 case STRETCH_GLYPH: \
4785 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
4786 HEAD, TAIL, HL, X, LAST_X); \
4787 break; \
4789 case IMAGE_GLYPH: \
4790 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
4791 TAIL, HL, X, LAST_X); \
4792 break; \
4794 default: \
4795 abort (); \
4798 x_set_glyph_string_background_width (s, START, LAST_X); \
4799 (X) += s->width; \
4802 while (0)
4805 /* Draw glyphs between START and END in AREA of ROW on window W,
4806 starting at x-position X. X is relative to AREA in W. HL is a
4807 face-override with the following meaning:
4809 DRAW_NORMAL_TEXT draw normally
4810 DRAW_CURSOR draw in cursor face
4811 DRAW_MOUSE_FACE draw in mouse face.
4812 DRAW_INVERSE_VIDEO draw in mode line face
4813 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
4814 DRAW_IMAGE_RAISED draw an image with a raised relief around it
4816 If REAL_START is non-null, return in *REAL_START the real starting
4817 position for display. This can be different from START in case
4818 overlapping glyphs must be displayed. If REAL_END is non-null,
4819 return in *REAL_END the real end position for display. This can be
4820 different from END in case overlapping glyphs must be displayed.
4822 If OVERLAPS_P is non-zero, draw only the foreground of characters
4823 and clip to the physical height of ROW.
4825 Value is the x-position reached, relative to AREA of W. */
4827 static int
4828 x_draw_glyphs (w, x, row, area, start, end, hl, real_start, real_end,
4829 overlaps_p)
4830 struct window *w;
4831 int x;
4832 struct glyph_row *row;
4833 enum glyph_row_area area;
4834 int start, end;
4835 enum draw_glyphs_face hl;
4836 int *real_start, *real_end;
4837 int overlaps_p;
4839 struct glyph_string *head, *tail;
4840 struct glyph_string *s;
4841 int last_x, area_width;
4842 int x_reached;
4843 int i, j;
4845 /* Let's rather be paranoid than getting a SEGV. */
4846 end = min (end, row->used[area]);
4847 start = max (0, start);
4848 start = min (end, start);
4849 if (real_start)
4850 *real_start = start;
4851 if (real_end)
4852 *real_end = end;
4854 /* Translate X to frame coordinates. Set last_x to the right
4855 end of the drawing area. */
4856 if (row->full_width_p)
4858 /* X is relative to the left edge of W, without scroll bars
4859 or flag areas. */
4860 struct frame *f = XFRAME (w->frame);
4861 /* int width = FRAME_FLAGS_AREA_WIDTH (f); */
4862 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
4864 x += window_left_x;
4865 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
4866 last_x = window_left_x + area_width;
4868 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
4870 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
4871 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
4872 last_x += width;
4873 else
4874 x -= width;
4877 x += FRAME_INTERNAL_BORDER_WIDTH (f);
4878 last_x -= FRAME_INTERNAL_BORDER_WIDTH (f);
4880 else
4882 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
4883 area_width = window_box_width (w, area);
4884 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
4887 /* Build a doubly-linked list of glyph_string structures between
4888 head and tail from what we have to draw. Note that the macro
4889 BUILD_GLYPH_STRINGS will modify its start parameter. That's
4890 the reason we use a separate variable `i'. */
4891 i = start;
4892 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
4893 overlaps_p);
4894 if (tail)
4895 x_reached = tail->x + tail->background_width;
4896 else
4897 x_reached = x;
4899 /* If there are any glyphs with lbearing < 0 or rbearing > width in
4900 the row, redraw some glyphs in front or following the glyph
4901 strings built above. */
4902 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
4904 int dummy_x = 0;
4905 struct glyph_string *h, *t;
4907 /* Compute overhangs for all glyph strings. */
4908 for (s = head; s; s = s->next)
4909 x_compute_glyph_string_overhangs (s);
4911 /* Prepend glyph strings for glyphs in front of the first glyph
4912 string that are overwritten because of the first glyph
4913 string's left overhang. The background of all strings
4914 prepended must be drawn because the first glyph string
4915 draws over it. */
4916 i = x_left_overwritten (head);
4917 if (i >= 0)
4919 j = i;
4920 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
4921 DRAW_NORMAL_TEXT, dummy_x, last_x,
4922 overlaps_p);
4923 start = i;
4924 if (real_start)
4925 *real_start = start;
4926 x_compute_overhangs_and_x (t, head->x, 1);
4927 x_prepend_glyph_string_lists (&head, &tail, h, t);
4930 /* Prepend glyph strings for glyphs in front of the first glyph
4931 string that overwrite that glyph string because of their
4932 right overhang. For these strings, only the foreground must
4933 be drawn, because it draws over the glyph string at `head'.
4934 The background must not be drawn because this would overwrite
4935 right overhangs of preceding glyphs for which no glyph
4936 strings exist. */
4937 i = x_left_overwriting (head);
4938 if (i >= 0)
4940 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
4941 DRAW_NORMAL_TEXT, dummy_x, last_x,
4942 overlaps_p);
4943 for (s = h; s; s = s->next)
4944 s->background_filled_p = 1;
4945 if (real_start)
4946 *real_start = i;
4947 x_compute_overhangs_and_x (t, head->x, 1);
4948 x_prepend_glyph_string_lists (&head, &tail, h, t);
4951 /* Append glyphs strings for glyphs following the last glyph
4952 string tail that are overwritten by tail. The background of
4953 these strings has to be drawn because tail's foreground draws
4954 over it. */
4955 i = x_right_overwritten (tail);
4956 if (i >= 0)
4958 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4959 DRAW_NORMAL_TEXT, x, last_x,
4960 overlaps_p);
4961 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4962 x_append_glyph_string_lists (&head, &tail, h, t);
4963 if (real_end)
4964 *real_end = i;
4967 /* Append glyph strings for glyphs following the last glyph
4968 string tail that overwrite tail. The foreground of such
4969 glyphs has to be drawn because it writes into the background
4970 of tail. The background must not be drawn because it could
4971 paint over the foreground of following glyphs. */
4972 i = x_right_overwriting (tail);
4973 if (i >= 0)
4975 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
4976 DRAW_NORMAL_TEXT, x, last_x,
4977 overlaps_p);
4978 for (s = h; s; s = s->next)
4979 s->background_filled_p = 1;
4980 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
4981 x_append_glyph_string_lists (&head, &tail, h, t);
4982 if (real_end)
4983 *real_end = i;
4987 /* Draw all strings. */
4988 for (s = head; s; s = s->next)
4989 x_draw_glyph_string (s);
4991 /* Value is the x-position up to which drawn, relative to AREA of W.
4992 This doesn't include parts drawn because of overhangs. */
4993 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
4994 if (!row->full_width_p)
4996 if (area > LEFT_MARGIN_AREA)
4997 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
4998 if (area > TEXT_AREA)
4999 x_reached -= window_box_width (w, TEXT_AREA);
5002 return x_reached;
5006 /* Fix the display of area AREA of overlapping row ROW in window W. */
5008 static void
5009 x_fix_overlapping_area (w, row, area)
5010 struct window *w;
5011 struct glyph_row *row;
5012 enum glyph_row_area area;
5014 int i, x;
5016 BLOCK_INPUT;
5018 if (area == LEFT_MARGIN_AREA)
5019 x = 0;
5020 else if (area == TEXT_AREA)
5021 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5022 else
5023 x = (window_box_width (w, LEFT_MARGIN_AREA)
5024 + window_box_width (w, TEXT_AREA));
5026 for (i = 0; i < row->used[area];)
5028 if (row->glyphs[area][i].overlaps_vertically_p)
5030 int start = i, start_x = x;
5034 x += row->glyphs[area][i].pixel_width;
5035 ++i;
5037 while (i < row->used[area]
5038 && row->glyphs[area][i].overlaps_vertically_p);
5040 x_draw_glyphs (w, start_x, row, area, start, i,
5041 (row->inverse_p
5042 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5043 NULL, NULL, 1);
5045 else
5047 x += row->glyphs[area][i].pixel_width;
5048 ++i;
5052 UNBLOCK_INPUT;
5056 /* Output LEN glyphs starting at START at the nominal cursor position.
5057 Advance the nominal cursor over the text. The global variable
5058 updated_window contains the window being updated, updated_row is
5059 the glyph row being updated, and updated_area is the area of that
5060 row being updated. */
5062 static void
5063 x_write_glyphs (start, len)
5064 struct glyph *start;
5065 int len;
5067 int x, hpos, real_start, real_end;
5069 xassert (updated_window && updated_row);
5070 BLOCK_INPUT;
5072 /* Write glyphs. */
5074 hpos = start - updated_row->glyphs[updated_area];
5075 x = x_draw_glyphs (updated_window, output_cursor.x,
5076 updated_row, updated_area,
5077 hpos, hpos + len,
5078 (updated_row->inverse_p
5079 ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT),
5080 &real_start, &real_end, 0);
5082 /* If we drew over the cursor, note that it is not visible any more. */
5083 note_overwritten_text_cursor (updated_window, real_start,
5084 real_end - real_start);
5086 UNBLOCK_INPUT;
5088 /* Advance the output cursor. */
5089 output_cursor.hpos += len;
5090 output_cursor.x = x;
5094 /* Insert LEN glyphs from START at the nominal cursor position. */
5096 static void
5097 x_insert_glyphs (start, len)
5098 struct glyph *start;
5099 register int len;
5101 struct frame *f;
5102 struct window *w;
5103 int line_height, shift_by_width, shifted_region_width;
5104 struct glyph_row *row;
5105 struct glyph *glyph;
5106 int frame_x, frame_y, hpos, real_start, real_end;
5108 xassert (updated_window && updated_row);
5109 BLOCK_INPUT;
5110 w = updated_window;
5111 f = XFRAME (WINDOW_FRAME (w));
5113 /* Get the height of the line we are in. */
5114 row = updated_row;
5115 line_height = row->height;
5117 /* Get the width of the glyphs to insert. */
5118 shift_by_width = 0;
5119 for (glyph = start; glyph < start + len; ++glyph)
5120 shift_by_width += glyph->pixel_width;
5122 /* Get the width of the region to shift right. */
5123 shifted_region_width = (window_box_width (w, updated_area)
5124 - output_cursor.x
5125 - shift_by_width);
5127 /* Shift right. */
5128 frame_x = WINDOW_TO_FRAME_PIXEL_X (w, output_cursor.x);
5129 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5130 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5131 f->output_data.x->normal_gc,
5132 frame_x, frame_y,
5133 shifted_region_width, line_height,
5134 frame_x + shift_by_width, frame_y);
5136 /* Write the glyphs. */
5137 hpos = start - row->glyphs[updated_area];
5138 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5139 DRAW_NORMAL_TEXT, &real_start, &real_end, 0);
5140 note_overwritten_text_cursor (w, real_start, real_end - real_start);
5142 /* Advance the output cursor. */
5143 output_cursor.hpos += len;
5144 output_cursor.x += shift_by_width;
5145 UNBLOCK_INPUT;
5149 /* Delete N glyphs at the nominal cursor position. Not implemented
5150 for X frames. */
5152 static void
5153 x_delete_glyphs (n)
5154 register int n;
5156 abort ();
5160 /* Erase the current text line from the nominal cursor position
5161 (inclusive) to pixel column TO_X (exclusive). The idea is that
5162 everything from TO_X onward is already erased.
5164 TO_X is a pixel position relative to updated_area of
5165 updated_window. TO_X == -1 means clear to the end of this area. */
5167 static void
5168 x_clear_end_of_line (to_x)
5169 int to_x;
5171 struct frame *f;
5172 struct window *w = updated_window;
5173 int max_x, min_y, max_y;
5174 int from_x, from_y, to_y;
5176 xassert (updated_window && updated_row);
5177 f = XFRAME (w->frame);
5179 if (updated_row->full_width_p)
5181 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5182 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5183 && !w->pseudo_window_p)
5184 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5186 else
5187 max_x = window_box_width (w, updated_area);
5188 max_y = window_text_bottom_y (w);
5190 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5191 of window. For TO_X > 0, truncate to end of drawing area. */
5192 if (to_x == 0)
5193 return;
5194 else if (to_x < 0)
5195 to_x = max_x;
5196 else
5197 to_x = min (to_x, max_x);
5199 to_y = min (max_y, output_cursor.y + updated_row->height);
5201 /* Notice if the cursor will be cleared by this operation. */
5202 if (!updated_row->full_width_p)
5203 note_overwritten_text_cursor (w, output_cursor.hpos, -1);
5205 from_x = output_cursor.x;
5207 /* Translate to frame coordinates. */
5208 if (updated_row->full_width_p)
5210 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5211 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5213 else
5215 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5216 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5219 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5220 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5221 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5223 /* Prevent inadvertently clearing to end of the X window. */
5224 if (to_x > from_x && to_y > from_y)
5226 BLOCK_INPUT;
5227 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5228 from_x, from_y, to_x - from_x, to_y - from_y,
5229 False);
5230 UNBLOCK_INPUT;
5235 /* Clear entire frame. If updating_frame is non-null, clear that
5236 frame. Otherwise clear the selected frame. */
5238 static void
5239 x_clear_frame ()
5241 struct frame *f;
5243 if (updating_frame)
5244 f = updating_frame;
5245 else
5246 f = SELECTED_FRAME ();
5248 /* Clearing the frame will erase any cursor, so mark them all as no
5249 longer visible. */
5250 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5251 output_cursor.hpos = output_cursor.vpos = 0;
5252 output_cursor.x = -1;
5254 /* We don't set the output cursor here because there will always
5255 follow an explicit cursor_to. */
5256 BLOCK_INPUT;
5257 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5259 /* We have to clear the scroll bars, too. If we have changed
5260 colors or something like that, then they should be notified. */
5261 x_scroll_bar_clear (f);
5263 XFlush (FRAME_X_DISPLAY (f));
5264 UNBLOCK_INPUT;
5269 /* Invert the middle quarter of the frame for .15 sec. */
5271 /* We use the select system call to do the waiting, so we have to make
5272 sure it's available. If it isn't, we just won't do visual bells. */
5274 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5277 /* Subtract the `struct timeval' values X and Y, storing the result in
5278 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5280 static int
5281 timeval_subtract (result, x, y)
5282 struct timeval *result, x, y;
5284 /* Perform the carry for the later subtraction by updating y. This
5285 is safer because on some systems the tv_sec member is unsigned. */
5286 if (x.tv_usec < y.tv_usec)
5288 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5289 y.tv_usec -= 1000000 * nsec;
5290 y.tv_sec += nsec;
5293 if (x.tv_usec - y.tv_usec > 1000000)
5295 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5296 y.tv_usec += 1000000 * nsec;
5297 y.tv_sec -= nsec;
5300 /* Compute the time remaining to wait. tv_usec is certainly
5301 positive. */
5302 result->tv_sec = x.tv_sec - y.tv_sec;
5303 result->tv_usec = x.tv_usec - y.tv_usec;
5305 /* Return indication of whether the result should be considered
5306 negative. */
5307 return x.tv_sec < y.tv_sec;
5310 void
5311 XTflash (f)
5312 struct frame *f;
5314 BLOCK_INPUT;
5317 GC gc;
5319 /* Create a GC that will use the GXxor function to flip foreground
5320 pixels into background pixels. */
5322 XGCValues values;
5324 values.function = GXxor;
5325 values.foreground = (f->output_data.x->foreground_pixel
5326 ^ f->output_data.x->background_pixel);
5328 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5329 GCFunction | GCForeground, &values);
5333 /* Get the height not including a menu bar widget. */
5334 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5335 /* Height of each line to flash. */
5336 int flash_height = FRAME_LINE_HEIGHT (f);
5337 /* These will be the left and right margins of the rectangles. */
5338 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5339 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5341 int width;
5343 /* Don't flash the area between a scroll bar and the frame
5344 edge it is next to. */
5345 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5347 case vertical_scroll_bar_left:
5348 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5349 break;
5351 case vertical_scroll_bar_right:
5352 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5353 break;
5355 default:
5356 break;
5359 width = flash_right - flash_left;
5361 /* If window is tall, flash top and bottom line. */
5362 if (height > 3 * FRAME_LINE_HEIGHT (f))
5364 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5365 flash_left,
5366 (FRAME_INTERNAL_BORDER_WIDTH (f)
5367 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5368 width, flash_height);
5369 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5370 flash_left,
5371 (height - flash_height
5372 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5373 width, flash_height);
5375 else
5376 /* If it is short, flash it all. */
5377 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5378 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5379 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5381 x_flush (f);
5384 struct timeval wakeup;
5386 EMACS_GET_TIME (wakeup);
5388 /* Compute time to wait until, propagating carry from usecs. */
5389 wakeup.tv_usec += 150000;
5390 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5391 wakeup.tv_usec %= 1000000;
5393 /* Keep waiting until past the time wakeup. */
5394 while (1)
5396 struct timeval timeout;
5398 EMACS_GET_TIME (timeout);
5400 /* In effect, timeout = wakeup - timeout.
5401 Break if result would be negative. */
5402 if (timeval_subtract (&timeout, wakeup, timeout))
5403 break;
5405 /* Try to wait that long--but we might wake up sooner. */
5406 select (0, NULL, NULL, NULL, &timeout);
5410 /* If window is tall, flash top and bottom line. */
5411 if (height > 3 * FRAME_LINE_HEIGHT (f))
5413 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5414 flash_left,
5415 (FRAME_INTERNAL_BORDER_WIDTH (f)
5416 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5417 width, flash_height);
5418 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5419 flash_left,
5420 (height - flash_height
5421 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5422 width, flash_height);
5424 else
5425 /* If it is short, flash it all. */
5426 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5427 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5428 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5430 XFreeGC (FRAME_X_DISPLAY (f), gc);
5431 x_flush (f);
5435 UNBLOCK_INPUT;
5438 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5441 /* Make audible bell. */
5443 void
5444 XTring_bell ()
5446 struct frame *f = SELECTED_FRAME ();
5448 if (FRAME_X_DISPLAY (f))
5450 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5451 if (visible_bell)
5452 XTflash (f);
5453 else
5454 #endif
5456 BLOCK_INPUT;
5457 XBell (FRAME_X_DISPLAY (f), 0);
5458 XFlush (FRAME_X_DISPLAY (f));
5459 UNBLOCK_INPUT;
5465 /* Specify how many text lines, from the top of the window,
5466 should be affected by insert-lines and delete-lines operations.
5467 This, and those operations, are used only within an update
5468 that is bounded by calls to x_update_begin and x_update_end. */
5470 static void
5471 XTset_terminal_window (n)
5472 register int n;
5474 /* This function intentionally left blank. */
5479 /***********************************************************************
5480 Line Dance
5481 ***********************************************************************/
5483 /* Perform an insert-lines or delete-lines operation, inserting N
5484 lines or deleting -N lines at vertical position VPOS. */
5486 static void
5487 x_ins_del_lines (vpos, n)
5488 int vpos, n;
5490 abort ();
5494 /* Scroll part of the display as described by RUN. */
5496 static void
5497 x_scroll_run (w, run)
5498 struct window *w;
5499 struct run *run;
5501 struct frame *f = XFRAME (w->frame);
5502 int x, y, width, height, from_y, to_y, bottom_y;
5504 /* Get frame-relative bounding box of the text display area of W,
5505 without mode lines. Include in this box the flags areas to the
5506 left and right of W. */
5507 window_box (w, -1, &x, &y, &width, &height);
5508 width += FRAME_X_FLAGS_AREA_WIDTH (f);
5509 x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
5511 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5512 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5513 bottom_y = y + height;
5515 if (to_y < from_y)
5517 /* Scrolling up. Make sure we don't copy part of the mode
5518 line at the bottom. */
5519 if (from_y + run->height > bottom_y)
5520 height = bottom_y - from_y;
5521 else
5522 height = run->height;
5524 else
5526 /* Scolling down. Make sure we don't copy over the mode line.
5527 at the bottom. */
5528 if (to_y + run->height > bottom_y)
5529 height = bottom_y - to_y;
5530 else
5531 height = run->height;
5534 BLOCK_INPUT;
5536 /* Cursor off. Will be switched on again in x_update_window_end. */
5537 updated_window = w;
5538 x_clear_cursor (w);
5540 XCopyArea (FRAME_X_DISPLAY (f),
5541 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5542 f->output_data.x->normal_gc,
5543 x, from_y,
5544 width, height,
5545 x, to_y);
5547 UNBLOCK_INPUT;
5552 /***********************************************************************
5553 Exposure Events
5554 ***********************************************************************/
5556 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5557 corner of the exposed rectangle. W and H are width and height of
5558 the exposed area. All are pixel values. W or H zero means redraw
5559 the entire frame. */
5561 static void
5562 expose_frame (f, x, y, w, h)
5563 struct frame *f;
5564 int x, y, w, h;
5566 XRectangle r;
5568 TRACE ((stderr, "expose_frame "));
5570 /* No need to redraw if frame will be redrawn soon. */
5571 if (FRAME_GARBAGED_P (f))
5573 TRACE ((stderr, " garbaged\n"));
5574 return;
5577 /* If basic faces haven't been realized yet, there is no point in
5578 trying to redraw anything. This can happen when we get an expose
5579 event while Emacs is starting, e.g. by moving another window. */
5580 if (FRAME_FACE_CACHE (f) == NULL
5581 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5583 TRACE ((stderr, " no faces\n"));
5584 return;
5587 if (w == 0 || h == 0)
5589 r.x = r.y = 0;
5590 r.width = CANON_X_UNIT (f) * f->width;
5591 r.height = CANON_Y_UNIT (f) * f->height;
5593 else
5595 r.x = x;
5596 r.y = y;
5597 r.width = w;
5598 r.height = h;
5601 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5602 expose_window_tree (XWINDOW (f->root_window), &r);
5604 if (WINDOWP (f->tool_bar_window))
5606 struct window *w = XWINDOW (f->tool_bar_window);
5607 XRectangle window_rect;
5608 XRectangle intersection_rect;
5609 int window_x, window_y, window_width, window_height;
5612 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5613 window_rect.x = window_x;
5614 window_rect.y = window_y;
5615 window_rect.width = window_width;
5616 window_rect.height = window_height;
5618 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5619 expose_window (w, &intersection_rect);
5622 #ifndef USE_X_TOOLKIT
5623 if (WINDOWP (f->menu_bar_window))
5625 struct window *w = XWINDOW (f->menu_bar_window);
5626 XRectangle window_rect;
5627 XRectangle intersection_rect;
5628 int window_x, window_y, window_width, window_height;
5631 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
5632 window_rect.x = window_x;
5633 window_rect.y = window_y;
5634 window_rect.width = window_width;
5635 window_rect.height = window_height;
5637 if (x_intersect_rectangles (&r, &window_rect, &intersection_rect))
5638 expose_window (w, &intersection_rect);
5640 #endif /* not USE_X_TOOLKIT */
5644 /* Redraw (parts) of all windows in the window tree rooted at W that
5645 intersect R. R contains frame pixel coordinates. */
5647 static void
5648 expose_window_tree (w, r)
5649 struct window *w;
5650 XRectangle *r;
5652 while (w)
5654 if (!NILP (w->hchild))
5655 expose_window_tree (XWINDOW (w->hchild), r);
5656 else if (!NILP (w->vchild))
5657 expose_window_tree (XWINDOW (w->vchild), r);
5658 else
5660 XRectangle window_rect;
5661 XRectangle intersection_rect;
5662 struct frame *f = XFRAME (w->frame);
5663 int window_x, window_y, window_width, window_height;
5665 /* Frame-relative pixel rectangle of W. */
5666 window_box (w, -1, &window_x, &window_y, &window_width,
5667 &window_height);
5668 window_rect.x
5669 = (window_x
5670 - FRAME_X_LEFT_FLAGS_AREA_WIDTH (f)
5671 - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5672 window_rect.y = window_y;
5673 window_rect.width
5674 = (window_width
5675 + FRAME_X_FLAGS_AREA_WIDTH (f)
5676 + FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f));
5677 window_rect.height
5678 = window_height + CURRENT_MODE_LINE_HEIGHT (w);
5680 if (x_intersect_rectangles (r, &window_rect, &intersection_rect))
5681 expose_window (w, &intersection_rect);
5684 w = NILP (w->next) ? 0 : XWINDOW (w->next);
5689 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5690 which intersects rectangle R. R is in window-relative coordinates. */
5692 static void
5693 expose_area (w, row, r, area)
5694 struct window *w;
5695 struct glyph_row *row;
5696 XRectangle *r;
5697 enum glyph_row_area area;
5699 struct glyph *first = row->glyphs[area];
5700 struct glyph *end = row->glyphs[area] + row->used[area];
5701 struct glyph *last;
5702 int first_x, start_x, x;
5704 if (area == TEXT_AREA && row->fill_line_p)
5705 /* If row extends face to end of line write the whole line. */
5706 x_draw_glyphs (w, 0, row, area,
5707 0, row->used[area],
5708 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5709 NULL, NULL, 0);
5710 else
5712 /* Set START_X to the window-relative start position for drawing glyphs of
5713 AREA. The first glyph of the text area can be partially visible.
5714 The first glyphs of other areas cannot. */
5715 if (area == LEFT_MARGIN_AREA)
5716 start_x = 0;
5717 else if (area == TEXT_AREA)
5718 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5719 else
5720 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5721 + window_box_width (w, TEXT_AREA));
5722 x = start_x;
5724 /* Find the first glyph that must be redrawn. */
5725 while (first < end
5726 && x + first->pixel_width < r->x)
5728 x += first->pixel_width;
5729 ++first;
5732 /* Find the last one. */
5733 last = first;
5734 first_x = x;
5735 while (last < end
5736 && x < r->x + r->width)
5738 x += last->pixel_width;
5739 ++last;
5742 /* Repaint. */
5743 if (last > first)
5744 x_draw_glyphs (w, first_x - start_x, row, area,
5745 first - row->glyphs[area],
5746 last - row->glyphs[area],
5747 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5748 NULL, NULL, 0);
5753 /* Redraw the parts of the glyph row ROW on window W intersecting
5754 rectangle R. R is in window-relative coordinates. */
5756 static void
5757 expose_line (w, row, r)
5758 struct window *w;
5759 struct glyph_row *row;
5760 XRectangle *r;
5762 xassert (row->enabled_p);
5764 if (row->mode_line_p || w->pseudo_window_p)
5765 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5766 row->inverse_p ? DRAW_INVERSE_VIDEO : DRAW_NORMAL_TEXT,
5767 NULL, NULL, 0);
5768 else
5770 if (row->used[LEFT_MARGIN_AREA])
5771 expose_area (w, row, r, LEFT_MARGIN_AREA);
5772 if (row->used[TEXT_AREA])
5773 expose_area (w, row, r, TEXT_AREA);
5774 if (row->used[RIGHT_MARGIN_AREA])
5775 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5776 x_draw_row_bitmaps (w, row);
5781 /* Return non-zero if W's cursor intersects rectangle R. */
5783 static int
5784 x_phys_cursor_in_rect_p (w, r)
5785 struct window *w;
5786 XRectangle *r;
5788 XRectangle cr, result;
5789 struct glyph *cursor_glyph;
5791 cursor_glyph = get_phys_cursor_glyph (w);
5792 if (cursor_glyph)
5794 cr.x = w->phys_cursor.x;
5795 cr.y = w->phys_cursor.y;
5796 cr.width = cursor_glyph->pixel_width;
5797 cr.height = w->phys_cursor_height;
5798 return x_intersect_rectangles (&cr, r, &result);
5800 else
5801 return 0;
5805 /* Redraw a rectangle of window W. R is a rectangle in window
5806 relative coordinates. Call this function with input blocked. */
5808 static void
5809 expose_window (w, r)
5810 struct window *w;
5811 XRectangle *r;
5813 struct glyph_row *row;
5814 int y;
5815 int yb = window_text_bottom_y (w);
5816 int cursor_cleared_p;
5818 /* If window is not yet fully initialized, do nothing. This can
5819 happen when toolkit scroll bars are used and a window is split.
5820 Reconfiguring the scroll bar will generate an expose for a newly
5821 created window. */
5822 if (w->current_matrix == NULL)
5823 return;
5825 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
5826 r->x, r->y, r->width, r->height));
5828 /* Convert to window coordinates. */
5829 r->x = FRAME_TO_WINDOW_PIXEL_X (w, r->x);
5830 r->y = FRAME_TO_WINDOW_PIXEL_Y (w, r->y);
5832 /* Turn off the cursor. */
5833 if (!w->pseudo_window_p
5834 && x_phys_cursor_in_rect_p (w, r))
5836 x_clear_cursor (w);
5837 cursor_cleared_p = 1;
5839 else
5840 cursor_cleared_p = 0;
5842 /* Find the first row intersecting the rectangle R. */
5843 row = w->current_matrix->rows;
5844 y = 0;
5845 while (row->enabled_p
5846 && y < yb
5847 && y + row->height < r->y)
5849 y += row->height;
5850 ++row;
5853 /* Display the text in the rectangle, one text line at a time. */
5854 while (row->enabled_p
5855 && y < yb
5856 && y < r->y + r->height)
5858 expose_line (w, row, r);
5859 y += row->height;
5860 ++row;
5863 /* Display the mode line if there is one. */
5864 if (WINDOW_WANTS_MODELINE_P (w)
5865 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
5866 row->enabled_p)
5867 && row->y < r->y + r->height)
5868 expose_line (w, row, r);
5870 if (!w->pseudo_window_p)
5872 /* Draw border between windows. */
5873 x_draw_vertical_border (w);
5875 /* Turn the cursor on again. */
5876 if (cursor_cleared_p)
5877 x_update_window_cursor (w, 1);
5882 /* Determine the intersection of two rectangles R1 and R2. Return
5883 the intersection in *RESULT. Value is non-zero if RESULT is not
5884 empty. */
5886 static int
5887 x_intersect_rectangles (r1, r2, result)
5888 XRectangle *r1, *r2, *result;
5890 XRectangle *left, *right;
5891 XRectangle *upper, *lower;
5892 int intersection_p = 0;
5894 /* Rearrange so that R1 is the left-most rectangle. */
5895 if (r1->x < r2->x)
5896 left = r1, right = r2;
5897 else
5898 left = r2, right = r1;
5900 /* X0 of the intersection is right.x0, if this is inside R1,
5901 otherwise there is no intersection. */
5902 if (right->x <= left->x + left->width)
5904 result->x = right->x;
5906 /* The right end of the intersection is the minimum of the
5907 the right ends of left and right. */
5908 result->width = (min (left->x + left->width, right->x + right->width)
5909 - result->x);
5911 /* Same game for Y. */
5912 if (r1->y < r2->y)
5913 upper = r1, lower = r2;
5914 else
5915 upper = r2, lower = r1;
5917 /* The upper end of the intersection is lower.y0, if this is inside
5918 of upper. Otherwise, there is no intersection. */
5919 if (lower->y <= upper->y + upper->height)
5921 result->y = lower->y;
5923 /* The lower end of the intersection is the minimum of the lower
5924 ends of upper and lower. */
5925 result->height = (min (lower->y + lower->height,
5926 upper->y + upper->height)
5927 - result->y);
5928 intersection_p = 1;
5932 return intersection_p;
5939 static void
5940 frame_highlight (f)
5941 struct frame *f;
5943 /* We used to only do this if Vx_no_window_manager was non-nil, but
5944 the ICCCM (section 4.1.6) says that the window's border pixmap
5945 and border pixel are window attributes which are "private to the
5946 client", so we can always change it to whatever we want. */
5947 BLOCK_INPUT;
5948 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5949 f->output_data.x->border_pixel);
5950 UNBLOCK_INPUT;
5951 x_update_cursor (f, 1);
5954 static void
5955 frame_unhighlight (f)
5956 struct frame *f;
5958 /* We used to only do this if Vx_no_window_manager was non-nil, but
5959 the ICCCM (section 4.1.6) says that the window's border pixmap
5960 and border pixel are window attributes which are "private to the
5961 client", so we can always change it to whatever we want. */
5962 BLOCK_INPUT;
5963 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5964 f->output_data.x->border_tile);
5965 UNBLOCK_INPUT;
5966 x_update_cursor (f, 1);
5969 /* The focus has changed. Update the frames as necessary to reflect
5970 the new situation. Note that we can't change the selected frame
5971 here, because the Lisp code we are interrupting might become confused.
5972 Each event gets marked with the frame in which it occurred, so the
5973 Lisp code can tell when the switch took place by examining the events. */
5975 static void
5976 x_new_focus_frame (dpyinfo, frame)
5977 struct x_display_info *dpyinfo;
5978 struct frame *frame;
5980 struct frame *old_focus = dpyinfo->x_focus_frame;
5982 if (frame != dpyinfo->x_focus_frame)
5984 /* Set this before calling other routines, so that they see
5985 the correct value of x_focus_frame. */
5986 dpyinfo->x_focus_frame = frame;
5988 if (old_focus && old_focus->auto_lower)
5989 x_lower_frame (old_focus);
5991 #if 0
5992 selected_frame = frame;
5993 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
5994 selected_frame);
5995 Fselect_window (selected_frame->selected_window);
5996 choose_minibuf_frame ();
5997 #endif /* ! 0 */
5999 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6000 pending_autoraise_frame = dpyinfo->x_focus_frame;
6001 else
6002 pending_autoraise_frame = 0;
6005 x_frame_rehighlight (dpyinfo);
6008 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6010 void
6011 x_mouse_leave (dpyinfo)
6012 struct x_display_info *dpyinfo;
6014 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6017 /* The focus has changed, or we have redirected a frame's focus to
6018 another frame (this happens when a frame uses a surrogate
6019 mini-buffer frame). Shift the highlight as appropriate.
6021 The FRAME argument doesn't necessarily have anything to do with which
6022 frame is being highlighted or un-highlighted; we only use it to find
6023 the appropriate X display info. */
6025 static void
6026 XTframe_rehighlight (frame)
6027 struct frame *frame;
6029 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6032 static void
6033 x_frame_rehighlight (dpyinfo)
6034 struct x_display_info *dpyinfo;
6036 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6038 if (dpyinfo->x_focus_frame)
6040 dpyinfo->x_highlight_frame
6041 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6042 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6043 : dpyinfo->x_focus_frame);
6044 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6046 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6047 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6050 else
6051 dpyinfo->x_highlight_frame = 0;
6053 if (dpyinfo->x_highlight_frame != old_highlight)
6055 if (old_highlight)
6056 frame_unhighlight (old_highlight);
6057 if (dpyinfo->x_highlight_frame)
6058 frame_highlight (dpyinfo->x_highlight_frame);
6064 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6066 /* Initialize mode_switch_bit and modifier_meaning. */
6067 static void
6068 x_find_modifier_meanings (dpyinfo)
6069 struct x_display_info *dpyinfo;
6071 int min_code, max_code;
6072 KeySym *syms;
6073 int syms_per_code;
6074 XModifierKeymap *mods;
6076 dpyinfo->meta_mod_mask = 0;
6077 dpyinfo->shift_lock_mask = 0;
6078 dpyinfo->alt_mod_mask = 0;
6079 dpyinfo->super_mod_mask = 0;
6080 dpyinfo->hyper_mod_mask = 0;
6082 #ifdef HAVE_X11R4
6083 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6084 #else
6085 min_code = dpyinfo->display->min_keycode;
6086 max_code = dpyinfo->display->max_keycode;
6087 #endif
6089 syms = XGetKeyboardMapping (dpyinfo->display,
6090 min_code, max_code - min_code + 1,
6091 &syms_per_code);
6092 mods = XGetModifierMapping (dpyinfo->display);
6094 /* Scan the modifier table to see which modifier bits the Meta and
6095 Alt keysyms are on. */
6097 int row, col; /* The row and column in the modifier table. */
6099 for (row = 3; row < 8; row++)
6100 for (col = 0; col < mods->max_keypermod; col++)
6102 KeyCode code
6103 = mods->modifiermap[(row * mods->max_keypermod) + col];
6105 /* Zeroes are used for filler. Skip them. */
6106 if (code == 0)
6107 continue;
6109 /* Are any of this keycode's keysyms a meta key? */
6111 int code_col;
6113 for (code_col = 0; code_col < syms_per_code; code_col++)
6115 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6117 switch (sym)
6119 case XK_Meta_L:
6120 case XK_Meta_R:
6121 dpyinfo->meta_mod_mask |= (1 << row);
6122 break;
6124 case XK_Alt_L:
6125 case XK_Alt_R:
6126 dpyinfo->alt_mod_mask |= (1 << row);
6127 break;
6129 case XK_Hyper_L:
6130 case XK_Hyper_R:
6131 dpyinfo->hyper_mod_mask |= (1 << row);
6132 break;
6134 case XK_Super_L:
6135 case XK_Super_R:
6136 dpyinfo->super_mod_mask |= (1 << row);
6137 break;
6139 case XK_Shift_Lock:
6140 /* Ignore this if it's not on the lock modifier. */
6141 if ((1 << row) == LockMask)
6142 dpyinfo->shift_lock_mask = LockMask;
6143 break;
6150 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6151 if (! dpyinfo->meta_mod_mask)
6153 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6154 dpyinfo->alt_mod_mask = 0;
6157 /* If some keys are both alt and meta,
6158 make them just meta, not alt. */
6159 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6161 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6164 XFree ((char *) syms);
6165 XFreeModifiermap (mods);
6168 /* Convert between the modifier bits X uses and the modifier bits
6169 Emacs uses. */
6171 static unsigned int
6172 x_x_to_emacs_modifiers (dpyinfo, state)
6173 struct x_display_info *dpyinfo;
6174 unsigned int state;
6176 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6177 | ((state & ControlMask) ? ctrl_modifier : 0)
6178 | ((state & dpyinfo->meta_mod_mask) ? meta_modifier : 0)
6179 | ((state & dpyinfo->alt_mod_mask) ? alt_modifier : 0)
6180 | ((state & dpyinfo->super_mod_mask) ? super_modifier : 0)
6181 | ((state & dpyinfo->hyper_mod_mask) ? hyper_modifier : 0));
6184 static unsigned int
6185 x_emacs_to_x_modifiers (dpyinfo, state)
6186 struct x_display_info *dpyinfo;
6187 unsigned int state;
6189 return ( ((state & alt_modifier) ? dpyinfo->alt_mod_mask : 0)
6190 | ((state & super_modifier) ? dpyinfo->super_mod_mask : 0)
6191 | ((state & hyper_modifier) ? dpyinfo->hyper_mod_mask : 0)
6192 | ((state & shift_modifier) ? ShiftMask : 0)
6193 | ((state & ctrl_modifier) ? ControlMask : 0)
6194 | ((state & meta_modifier) ? dpyinfo->meta_mod_mask : 0));
6197 /* Convert a keysym to its name. */
6199 char *
6200 x_get_keysym_name (keysym)
6201 KeySym keysym;
6203 char *value;
6205 BLOCK_INPUT;
6206 value = XKeysymToString (keysym);
6207 UNBLOCK_INPUT;
6209 return value;
6214 /* Mouse clicks and mouse movement. Rah. */
6216 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6217 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6218 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6219 not force the value into range. */
6221 void
6222 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6223 FRAME_PTR f;
6224 register int pix_x, pix_y;
6225 register int *x, *y;
6226 XRectangle *bounds;
6227 int noclip;
6229 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6230 even for negative values. */
6231 if (pix_x < 0)
6232 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6233 if (pix_y < 0)
6234 pix_y -= (f)->output_data.x->line_height - 1;
6236 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6237 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6239 if (bounds)
6241 bounds->width = FONT_WIDTH (f->output_data.x->font);
6242 bounds->height = f->output_data.x->line_height;
6243 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6244 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6247 if (!noclip)
6249 if (pix_x < 0)
6250 pix_x = 0;
6251 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6252 pix_x = FRAME_WINDOW_WIDTH (f);
6254 if (pix_y < 0)
6255 pix_y = 0;
6256 else if (pix_y > f->height)
6257 pix_y = f->height;
6260 *x = pix_x;
6261 *y = pix_y;
6265 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6266 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6267 can't tell the positions because W's display is not up to date,
6268 return 0. */
6271 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6272 struct window *w;
6273 int hpos, vpos;
6274 int *frame_x, *frame_y;
6276 int success_p;
6278 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6279 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6281 if (display_completed)
6283 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6284 struct glyph *glyph = row->glyphs[TEXT_AREA];
6285 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6287 *frame_y = row->y;
6288 *frame_x = row->x;
6289 while (glyph < end)
6291 *frame_x += glyph->pixel_width;
6292 ++glyph;
6295 success_p = 1;
6297 else
6299 *frame_y = *frame_x = 0;
6300 success_p = 0;
6303 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6304 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6305 return success_p;
6309 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6311 If the event is a button press, then note that we have grabbed
6312 the mouse. */
6314 static Lisp_Object
6315 construct_mouse_click (result, event, f)
6316 struct input_event *result;
6317 XButtonEvent *event;
6318 struct frame *f;
6320 /* Make the event type no_event; we'll change that when we decide
6321 otherwise. */
6322 result->kind = mouse_click;
6323 result->code = event->button - Button1;
6324 result->timestamp = event->time;
6325 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6326 event->state)
6327 | (event->type == ButtonRelease
6328 ? up_modifier
6329 : down_modifier));
6331 XSETINT (result->x, event->x);
6332 XSETINT (result->y, event->y);
6333 XSETFRAME (result->frame_or_window, f);
6334 result->arg = Qnil;
6335 return Qnil;
6339 /* Function to report a mouse movement to the mainstream Emacs code.
6340 The input handler calls this.
6342 We have received a mouse movement event, which is given in *event.
6343 If the mouse is over a different glyph than it was last time, tell
6344 the mainstream emacs code by setting mouse_moved. If not, ask for
6345 another motion event, so we can check again the next time it moves. */
6347 static XMotionEvent last_mouse_motion_event;
6348 static Lisp_Object last_mouse_motion_frame;
6350 static void
6351 note_mouse_movement (frame, event)
6352 FRAME_PTR frame;
6353 XMotionEvent *event;
6355 last_mouse_movement_time = event->time;
6356 last_mouse_motion_event = *event;
6357 XSETFRAME (last_mouse_motion_frame, frame);
6359 if (event->window != FRAME_X_WINDOW (frame))
6361 frame->mouse_moved = 1;
6362 last_mouse_scroll_bar = Qnil;
6363 note_mouse_highlight (frame, -1, -1);
6366 /* Has the mouse moved off the glyph it was on at the last sighting? */
6367 else if (event->x < last_mouse_glyph.x
6368 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6369 || event->y < last_mouse_glyph.y
6370 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6372 frame->mouse_moved = 1;
6373 last_mouse_scroll_bar = Qnil;
6374 note_mouse_highlight (frame, event->x, event->y);
6378 /* This is used for debugging, to turn off note_mouse_highlight. */
6380 int disable_mouse_highlight;
6384 /************************************************************************
6385 Mouse Face
6386 ************************************************************************/
6388 /* Find the glyph under window-relative coordinates X/Y in window W.
6389 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6390 strings. Return in *HPOS and *VPOS the row and column number of
6391 the glyph found. Return in *AREA the glyph area containing X.
6392 Value is a pointer to the glyph found or null if X/Y is not on
6393 text, or we can't tell because W's current matrix is not up to
6394 date. */
6396 static struct glyph *
6397 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area)
6398 struct window *w;
6399 int x, y;
6400 int *hpos, *vpos, *area;
6402 struct glyph *glyph, *end;
6403 struct glyph_row *row = NULL;
6404 int x0, i, left_area_width;
6406 /* Find row containing Y. Give up if some row is not enabled. */
6407 for (i = 0; i < w->current_matrix->nrows; ++i)
6409 row = MATRIX_ROW (w->current_matrix, i);
6410 if (!row->enabled_p)
6411 return NULL;
6412 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6413 break;
6416 *vpos = i;
6417 *hpos = 0;
6419 /* Give up if Y is not in the window. */
6420 if (i == w->current_matrix->nrows)
6421 return NULL;
6423 /* Get the glyph area containing X. */
6424 if (w->pseudo_window_p)
6426 *area = TEXT_AREA;
6427 x0 = 0;
6429 else
6431 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6432 if (x < left_area_width)
6434 *area = LEFT_MARGIN_AREA;
6435 x0 = 0;
6437 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6439 *area = TEXT_AREA;
6440 x0 = row->x + left_area_width;
6442 else
6444 *area = RIGHT_MARGIN_AREA;
6445 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6449 /* Find glyph containing X. */
6450 glyph = row->glyphs[*area];
6451 end = glyph + row->used[*area];
6452 while (glyph < end)
6454 if (x < x0 + glyph->pixel_width)
6456 if (w->pseudo_window_p)
6457 break;
6458 else if (BUFFERP (glyph->object))
6459 break;
6462 x0 += glyph->pixel_width;
6463 ++glyph;
6466 if (glyph == end)
6467 return NULL;
6469 *hpos = glyph - row->glyphs[*area];
6470 return glyph;
6474 /* Convert frame-relative x/y to coordinates relative to window W.
6475 Takes pseudo-windows into account. */
6477 static void
6478 frame_to_window_pixel_xy (w, x, y)
6479 struct window *w;
6480 int *x, *y;
6482 if (w->pseudo_window_p)
6484 /* A pseudo-window is always full-width, and starts at the
6485 left edge of the frame, plus a frame border. */
6486 struct frame *f = XFRAME (w->frame);
6487 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6488 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6490 else
6492 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6493 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6498 /* Take proper action when mouse has moved to the mode or header line of
6499 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6500 mode line. X is relative to the start of the text display area of
6501 W, so the width of bitmap areas and scroll bars must be subtracted
6502 to get a position relative to the start of the mode line. */
6504 static void
6505 note_mode_line_highlight (w, x, mode_line_p)
6506 struct window *w;
6507 int x, mode_line_p;
6509 struct frame *f = XFRAME (w->frame);
6510 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6511 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6512 struct glyph_row *row;
6514 if (mode_line_p)
6515 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6516 else
6517 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6519 if (row->enabled_p)
6521 struct glyph *glyph, *end;
6522 Lisp_Object help, map;
6523 int x0;
6525 /* Find the glyph under X. */
6526 glyph = row->glyphs[TEXT_AREA];
6527 end = glyph + row->used[TEXT_AREA];
6528 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6529 + FRAME_X_LEFT_FLAGS_AREA_WIDTH (f));
6531 while (glyph < end
6532 && x >= x0 + glyph->pixel_width)
6534 x0 += glyph->pixel_width;
6535 ++glyph;
6538 if (glyph < end
6539 && STRINGP (glyph->object)
6540 && XSTRING (glyph->object)->intervals
6541 && glyph->charpos >= 0
6542 && glyph->charpos < XSTRING (glyph->object)->size)
6544 /* If we're on a string with `help-echo' text property,
6545 arrange for the help to be displayed. This is done by
6546 setting the global variable help_echo to the help string. */
6547 help = Fget_text_property (make_number (glyph->charpos),
6548 Qhelp_echo, glyph->object);
6549 if (!NILP (help))
6551 help_echo = help;
6552 XSETWINDOW (help_echo_window, w);
6553 help_echo_object = glyph->object;
6554 help_echo_pos = glyph->charpos;
6557 /* Change the mouse pointer according to what is under X/Y. */
6558 map = Fget_text_property (make_number (glyph->charpos),
6559 Qlocal_map, glyph->object);
6560 if (KEYMAPP (map))
6561 cursor = f->output_data.x->nontext_cursor;
6562 else
6564 map = Fget_text_property (make_number (glyph->charpos),
6565 Qkeymap, glyph->object);
6566 if (KEYMAPP (map))
6567 cursor = f->output_data.x->nontext_cursor;
6572 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6576 /* Take proper action when the mouse has moved to position X, Y on
6577 frame F as regards highlighting characters that have mouse-face
6578 properties. Also de-highlighting chars where the mouse was before.
6579 X and Y can be negative or out of range. */
6581 static void
6582 note_mouse_highlight (f, x, y)
6583 struct frame *f;
6584 int x, y;
6586 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6587 int portion;
6588 Lisp_Object window;
6589 struct window *w;
6591 /* When a menu is active, don't highlight because this looks odd. */
6592 #ifdef USE_X_TOOLKIT
6593 if (popup_activated ())
6594 return;
6595 #endif
6597 if (disable_mouse_highlight
6598 || !f->glyphs_initialized_p)
6599 return;
6601 dpyinfo->mouse_face_mouse_x = x;
6602 dpyinfo->mouse_face_mouse_y = y;
6603 dpyinfo->mouse_face_mouse_frame = f;
6605 if (dpyinfo->mouse_face_defer)
6606 return;
6608 if (gc_in_progress)
6610 dpyinfo->mouse_face_deferred_gc = 1;
6611 return;
6614 /* Which window is that in? */
6615 window = window_from_coordinates (f, x, y, &portion, 1);
6617 /* If we were displaying active text in another window, clear that. */
6618 if (! EQ (window, dpyinfo->mouse_face_window))
6619 clear_mouse_face (dpyinfo);
6621 /* Not on a window -> return. */
6622 if (!WINDOWP (window))
6623 return;
6625 /* Convert to window-relative pixel coordinates. */
6626 w = XWINDOW (window);
6627 frame_to_window_pixel_xy (w, &x, &y);
6629 /* Handle tool-bar window differently since it doesn't display a
6630 buffer. */
6631 if (EQ (window, f->tool_bar_window))
6633 note_tool_bar_highlight (f, x, y);
6634 return;
6637 if (portion == 1 || portion == 3)
6639 /* Mouse is on the mode or top line. */
6640 note_mode_line_highlight (w, x, portion == 1);
6641 return;
6643 else if (portion == 2)
6644 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6645 f->output_data.x->horizontal_drag_cursor);
6646 else
6647 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6648 f->output_data.x->text_cursor);
6650 /* Are we in a window whose display is up to date?
6651 And verify the buffer's text has not changed. */
6652 if (/* Within text portion of the window. */
6653 portion == 0
6654 && EQ (w->window_end_valid, w->buffer)
6655 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
6656 && (XFASTINT (w->last_overlay_modified)
6657 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
6659 int hpos, vpos, pos, i, area;
6660 struct glyph *glyph;
6662 /* Find the glyph under X/Y. */
6663 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area);
6665 /* Clear mouse face if X/Y not over text. */
6666 if (glyph == NULL
6667 || area != TEXT_AREA
6668 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6670 clear_mouse_face (dpyinfo);
6671 return;
6674 pos = glyph->charpos;
6675 xassert (w->pseudo_window_p || BUFFERP (glyph->object));
6677 /* Check for mouse-face and help-echo. */
6679 Lisp_Object mouse_face, overlay, position;
6680 Lisp_Object *overlay_vec;
6681 int len, noverlays;
6682 struct buffer *obuf;
6683 int obegv, ozv;
6685 /* If we get an out-of-range value, return now; avoid an error. */
6686 if (pos > BUF_Z (XBUFFER (w->buffer)))
6687 return;
6689 /* Make the window's buffer temporarily current for
6690 overlays_at and compute_char_face. */
6691 obuf = current_buffer;
6692 current_buffer = XBUFFER (w->buffer);
6693 obegv = BEGV;
6694 ozv = ZV;
6695 BEGV = BEG;
6696 ZV = Z;
6698 /* Is this char mouse-active or does it have help-echo? */
6699 XSETINT (position, pos);
6701 /* Put all the overlays we want in a vector in overlay_vec.
6702 Store the length in len. If there are more than 10, make
6703 enough space for all, and try again. */
6704 len = 10;
6705 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6706 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
6707 if (noverlays > len)
6709 len = noverlays;
6710 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
6711 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
6714 /* Sort overlays into increasing priority order. */
6715 noverlays = sort_overlays (overlay_vec, noverlays, w);
6717 /* Check mouse-face highlighting. */
6718 if (! (EQ (window, dpyinfo->mouse_face_window)
6719 && vpos >= dpyinfo->mouse_face_beg_row
6720 && vpos <= dpyinfo->mouse_face_end_row
6721 && (vpos > dpyinfo->mouse_face_beg_row
6722 || hpos >= dpyinfo->mouse_face_beg_col)
6723 && (vpos < dpyinfo->mouse_face_end_row
6724 || hpos < dpyinfo->mouse_face_end_col
6725 || dpyinfo->mouse_face_past_end)))
6727 /* Clear the display of the old active region, if any. */
6728 clear_mouse_face (dpyinfo);
6730 /* Find the highest priority overlay that has a mouse-face prop. */
6731 overlay = Qnil;
6732 for (i = noverlays - 1; i >= 0; --i)
6734 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
6735 if (!NILP (mouse_face))
6737 overlay = overlay_vec[i];
6738 break;
6742 /* If no overlay applies, get a text property. */
6743 if (NILP (overlay))
6744 mouse_face = Fget_text_property (position, Qmouse_face, w->buffer);
6746 /* Handle the overlay case. */
6747 if (! NILP (overlay))
6749 /* Find the range of text around this char that
6750 should be active. */
6751 Lisp_Object before, after;
6752 int ignore;
6754 before = Foverlay_start (overlay);
6755 after = Foverlay_end (overlay);
6756 /* Record this as the current active region. */
6757 fast_find_position (w, XFASTINT (before),
6758 &dpyinfo->mouse_face_beg_col,
6759 &dpyinfo->mouse_face_beg_row,
6760 &dpyinfo->mouse_face_beg_x,
6761 &dpyinfo->mouse_face_beg_y);
6762 dpyinfo->mouse_face_past_end
6763 = !fast_find_position (w, XFASTINT (after),
6764 &dpyinfo->mouse_face_end_col,
6765 &dpyinfo->mouse_face_end_row,
6766 &dpyinfo->mouse_face_end_x,
6767 &dpyinfo->mouse_face_end_y);
6768 dpyinfo->mouse_face_window = window;
6769 dpyinfo->mouse_face_face_id
6770 = face_at_buffer_position (w, pos, 0, 0,
6771 &ignore, pos + 1, 1);
6773 /* Display it as active. */
6774 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6776 /* Handle the text property case. */
6777 else if (! NILP (mouse_face))
6779 /* Find the range of text around this char that
6780 should be active. */
6781 Lisp_Object before, after, beginning, end;
6782 int ignore;
6784 beginning = Fmarker_position (w->start);
6785 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
6786 - XFASTINT (w->window_end_pos)));
6787 before
6788 = Fprevious_single_property_change (make_number (pos + 1),
6789 Qmouse_face,
6790 w->buffer, beginning);
6791 after
6792 = Fnext_single_property_change (position, Qmouse_face,
6793 w->buffer, end);
6794 /* Record this as the current active region. */
6795 fast_find_position (w, XFASTINT (before),
6796 &dpyinfo->mouse_face_beg_col,
6797 &dpyinfo->mouse_face_beg_row,
6798 &dpyinfo->mouse_face_beg_x,
6799 &dpyinfo->mouse_face_beg_y);
6800 dpyinfo->mouse_face_past_end
6801 = !fast_find_position (w, XFASTINT (after),
6802 &dpyinfo->mouse_face_end_col,
6803 &dpyinfo->mouse_face_end_row,
6804 &dpyinfo->mouse_face_end_x,
6805 &dpyinfo->mouse_face_end_y);
6806 dpyinfo->mouse_face_window = window;
6807 dpyinfo->mouse_face_face_id
6808 = face_at_buffer_position (w, pos, 0, 0,
6809 &ignore, pos + 1, 1);
6811 /* Display it as active. */
6812 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
6816 /* Look for a `help-echo' property. */
6818 Lisp_Object help, overlay;
6820 /* Check overlays first. */
6821 help = overlay = Qnil;
6822 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
6824 overlay = overlay_vec[i];
6825 help = Foverlay_get (overlay, Qhelp_echo);
6828 if (!NILP (help))
6830 help_echo = help;
6831 help_echo_window = window;
6832 help_echo_object = overlay;
6833 help_echo_pos = pos;
6835 else
6837 /* Try text properties. */
6838 if ((STRINGP (glyph->object)
6839 && glyph->charpos >= 0
6840 && glyph->charpos < XSTRING (glyph->object)->size)
6841 || (BUFFERP (glyph->object)
6842 && glyph->charpos >= BEGV
6843 && glyph->charpos < ZV))
6844 help = Fget_text_property (make_number (glyph->charpos),
6845 Qhelp_echo, glyph->object);
6847 if (!NILP (help))
6849 help_echo = help;
6850 help_echo_window = window;
6851 help_echo_object = glyph->object;
6852 help_echo_pos = glyph->charpos;
6857 BEGV = obegv;
6858 ZV = ozv;
6859 current_buffer = obuf;
6864 static void
6865 redo_mouse_highlight ()
6867 if (!NILP (last_mouse_motion_frame)
6868 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
6869 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
6870 last_mouse_motion_event.x,
6871 last_mouse_motion_event.y);
6876 /***********************************************************************
6877 Tool-bars
6878 ***********************************************************************/
6880 static int x_tool_bar_item P_ ((struct frame *, int, int,
6881 struct glyph **, int *, int *, int *));
6883 /* Tool-bar item index of the item on which a mouse button was pressed
6884 or -1. */
6886 static int last_tool_bar_item;
6889 /* Get information about the tool-bar item at position X/Y on frame F.
6890 Return in *GLYPH a pointer to the glyph of the tool-bar item in
6891 the current matrix of the tool-bar window of F, or NULL if not
6892 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
6893 item in F->tool_bar_items. Value is
6895 -1 if X/Y is not on a tool-bar item
6896 0 if X/Y is on the same item that was highlighted before.
6897 1 otherwise. */
6899 static int
6900 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
6901 struct frame *f;
6902 int x, y;
6903 struct glyph **glyph;
6904 int *hpos, *vpos, *prop_idx;
6906 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6907 struct window *w = XWINDOW (f->tool_bar_window);
6908 int area;
6910 /* Find the glyph under X/Y. */
6911 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area);
6912 if (*glyph == NULL)
6913 return -1;
6915 /* Get the start of this tool-bar item's properties in
6916 f->tool_bar_items. */
6917 if (!tool_bar_item_info (f, *glyph, prop_idx))
6918 return -1;
6920 /* Is mouse on the highlighted item? */
6921 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
6922 && *vpos >= dpyinfo->mouse_face_beg_row
6923 && *vpos <= dpyinfo->mouse_face_end_row
6924 && (*vpos > dpyinfo->mouse_face_beg_row
6925 || *hpos >= dpyinfo->mouse_face_beg_col)
6926 && (*vpos < dpyinfo->mouse_face_end_row
6927 || *hpos < dpyinfo->mouse_face_end_col
6928 || dpyinfo->mouse_face_past_end))
6929 return 0;
6931 return 1;
6935 /* Handle mouse button event on the tool-bar of frame F, at
6936 frame-relative coordinates X/Y. EVENT_TYPE is either ButtionPress
6937 or ButtonRelase. */
6939 static void
6940 x_handle_tool_bar_click (f, button_event)
6941 struct frame *f;
6942 XButtonEvent *button_event;
6944 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6945 struct window *w = XWINDOW (f->tool_bar_window);
6946 int hpos, vpos, prop_idx;
6947 struct glyph *glyph;
6948 Lisp_Object enabled_p;
6949 int x = button_event->x;
6950 int y = button_event->y;
6952 /* If not on the highlighted tool-bar item, return. */
6953 frame_to_window_pixel_xy (w, &x, &y);
6954 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
6955 return;
6957 /* If item is disabled, do nothing. */
6958 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
6959 if (NILP (enabled_p))
6960 return;
6962 if (button_event->type == ButtonPress)
6964 /* Show item in pressed state. */
6965 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
6966 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
6967 last_tool_bar_item = prop_idx;
6969 else
6971 Lisp_Object key, frame;
6972 struct input_event event;
6974 /* Show item in released state. */
6975 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
6976 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
6978 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
6980 XSETFRAME (frame, f);
6981 event.kind = TOOL_BAR_EVENT;
6982 event.frame_or_window = frame;
6983 event.arg = frame;
6984 kbd_buffer_store_event (&event);
6986 event.kind = TOOL_BAR_EVENT;
6987 event.frame_or_window = frame;
6988 event.arg = key;
6989 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6990 button_event->state);
6991 kbd_buffer_store_event (&event);
6992 last_tool_bar_item = -1;
6997 /* Possibly highlight a tool-bar item on frame F when mouse moves to
6998 tool-bar window-relative coordinates X/Y. Called from
6999 note_mouse_highlight. */
7001 static void
7002 note_tool_bar_highlight (f, x, y)
7003 struct frame *f;
7004 int x, y;
7006 Lisp_Object window = f->tool_bar_window;
7007 struct window *w = XWINDOW (window);
7008 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7009 int hpos, vpos;
7010 struct glyph *glyph;
7011 struct glyph_row *row;
7012 int i;
7013 Lisp_Object enabled_p;
7014 int prop_idx;
7015 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
7016 int mouse_down_p, rc;
7018 /* Function note_mouse_highlight is called with negative x(y
7019 values when mouse moves outside of the frame. */
7020 if (x <= 0 || y <= 0)
7022 clear_mouse_face (dpyinfo);
7023 return;
7026 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7027 if (rc < 0)
7029 /* Not on tool-bar item. */
7030 clear_mouse_face (dpyinfo);
7031 return;
7033 else if (rc == 0)
7034 /* On same tool-bar item as before. */
7035 goto set_help_echo;
7037 clear_mouse_face (dpyinfo);
7039 /* Mouse is down, but on different tool-bar item? */
7040 mouse_down_p = (dpyinfo->grabbed
7041 && f == last_mouse_frame
7042 && FRAME_LIVE_P (f));
7043 if (mouse_down_p
7044 && last_tool_bar_item != prop_idx)
7045 return;
7047 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7048 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7050 /* If tool-bar item is not enabled, don't highlight it. */
7051 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7052 if (!NILP (enabled_p))
7054 /* Compute the x-position of the glyph. In front and past the
7055 image is a space. We include this is the highlighted area. */
7056 row = MATRIX_ROW (w->current_matrix, vpos);
7057 for (i = x = 0; i < hpos; ++i)
7058 x += row->glyphs[TEXT_AREA][i].pixel_width;
7060 /* Record this as the current active region. */
7061 dpyinfo->mouse_face_beg_col = hpos;
7062 dpyinfo->mouse_face_beg_row = vpos;
7063 dpyinfo->mouse_face_beg_x = x;
7064 dpyinfo->mouse_face_beg_y = row->y;
7065 dpyinfo->mouse_face_past_end = 0;
7067 dpyinfo->mouse_face_end_col = hpos + 1;
7068 dpyinfo->mouse_face_end_row = vpos;
7069 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7070 dpyinfo->mouse_face_end_y = row->y;
7071 dpyinfo->mouse_face_window = window;
7072 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7074 /* Display it as active. */
7075 show_mouse_face (dpyinfo, draw);
7076 dpyinfo->mouse_face_image_state = draw;
7079 set_help_echo:
7081 /* Set help_echo to a help string.to display for this tool-bar item.
7082 XTread_socket does the rest. */
7083 help_echo_object = help_echo_window = Qnil;
7084 help_echo_pos = -1;
7085 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7086 if (NILP (help_echo))
7087 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7092 /* Find the glyph matrix position of buffer position POS in window W.
7093 *HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7094 current glyphs must be up to date. If POS is above window start
7095 return (0, 0, 0, 0). If POS is after end of W, return end of
7096 last line in W. */
7098 static int
7099 fast_find_position (w, pos, hpos, vpos, x, y)
7100 struct window *w;
7101 int pos;
7102 int *hpos, *vpos, *x, *y;
7104 int i;
7105 int lastcol;
7106 int maybe_next_line_p = 0;
7107 int line_start_position;
7108 int yb = window_text_bottom_y (w);
7109 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
7110 struct glyph_row *best_row = row;
7111 int row_vpos = 0, best_row_vpos = 0;
7112 int current_x;
7114 while (row->y < yb)
7116 if (row->used[TEXT_AREA])
7117 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7118 else
7119 line_start_position = 0;
7121 if (line_start_position > pos)
7122 break;
7123 /* If the position sought is the end of the buffer,
7124 don't include the blank lines at the bottom of the window. */
7125 else if (line_start_position == pos
7126 && pos == BUF_ZV (XBUFFER (w->buffer)))
7128 maybe_next_line_p = 1;
7129 break;
7131 else if (line_start_position > 0)
7133 best_row = row;
7134 best_row_vpos = row_vpos;
7137 if (row->y + row->height >= yb)
7138 break;
7140 ++row;
7141 ++row_vpos;
7144 /* Find the right column within BEST_ROW. */
7145 lastcol = 0;
7146 current_x = best_row->x;
7147 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7149 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7150 int charpos;
7152 charpos = glyph->charpos;
7153 if (charpos == pos)
7155 *hpos = i;
7156 *vpos = best_row_vpos;
7157 *x = current_x;
7158 *y = best_row->y;
7159 return 1;
7161 else if (charpos > pos)
7162 break;
7163 else if (charpos > 0)
7164 lastcol = i;
7166 current_x += glyph->pixel_width;
7169 /* If we're looking for the end of the buffer,
7170 and we didn't find it in the line we scanned,
7171 use the start of the following line. */
7172 if (maybe_next_line_p)
7174 ++best_row;
7175 ++best_row_vpos;
7176 lastcol = 0;
7177 current_x = best_row->x;
7180 *vpos = best_row_vpos;
7181 *hpos = lastcol + 1;
7182 *x = current_x;
7183 *y = best_row->y;
7184 return 0;
7188 /* Display the active region described by mouse_face_*
7189 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7191 static void
7192 show_mouse_face (dpyinfo, draw)
7193 struct x_display_info *dpyinfo;
7194 enum draw_glyphs_face draw;
7196 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7197 struct frame *f = XFRAME (WINDOW_FRAME (w));
7198 int i;
7199 int cursor_off_p = 0;
7200 struct cursor_pos saved_cursor;
7202 saved_cursor = output_cursor;
7204 /* If window is in the process of being destroyed, don't bother
7205 to do anything. */
7206 if (w->current_matrix == NULL)
7207 goto set_x_cursor;
7209 /* Recognize when we are called to operate on rows that don't exist
7210 anymore. This can happen when a window is split. */
7211 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
7212 goto set_x_cursor;
7214 set_output_cursor (&w->phys_cursor);
7216 /* Note that mouse_face_beg_row etc. are window relative. */
7217 for (i = dpyinfo->mouse_face_beg_row;
7218 i <= dpyinfo->mouse_face_end_row;
7219 i++)
7221 int start_hpos, end_hpos, start_x;
7222 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
7224 /* Don't do anything if row doesn't have valid contents. */
7225 if (!row->enabled_p)
7226 continue;
7228 /* For all but the first row, the highlight starts at column 0. */
7229 if (i == dpyinfo->mouse_face_beg_row)
7231 start_hpos = dpyinfo->mouse_face_beg_col;
7232 start_x = dpyinfo->mouse_face_beg_x;
7234 else
7236 start_hpos = 0;
7237 start_x = 0;
7240 if (i == dpyinfo->mouse_face_end_row)
7241 end_hpos = dpyinfo->mouse_face_end_col;
7242 else
7243 end_hpos = row->used[TEXT_AREA];
7245 /* If the cursor's in the text we are about to rewrite, turn the
7246 cursor off. */
7247 if (!w->pseudo_window_p
7248 && i == output_cursor.vpos
7249 && output_cursor.hpos >= start_hpos - 1
7250 && output_cursor.hpos <= end_hpos)
7252 x_update_window_cursor (w, 0);
7253 cursor_off_p = 1;
7256 if (end_hpos > start_hpos)
7258 row->mouse_face_p = draw == DRAW_MOUSE_FACE;
7259 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7260 start_hpos, end_hpos, draw, NULL, NULL, 0);
7264 /* If we turned the cursor off, turn it back on. */
7265 if (cursor_off_p)
7266 x_display_cursor (w, 1,
7267 output_cursor.hpos, output_cursor.vpos,
7268 output_cursor.x, output_cursor.y);
7270 output_cursor = saved_cursor;
7272 set_x_cursor:
7274 /* Change the mouse cursor. */
7275 if (draw == DRAW_NORMAL_TEXT)
7276 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7277 f->output_data.x->text_cursor);
7278 else if (draw == DRAW_MOUSE_FACE)
7279 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7280 f->output_data.x->cross_cursor);
7281 else
7282 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7283 f->output_data.x->nontext_cursor);
7286 /* Clear out the mouse-highlighted active region.
7287 Redraw it un-highlighted first. */
7289 void
7290 clear_mouse_face (dpyinfo)
7291 struct x_display_info *dpyinfo;
7293 if (!NILP (tip_frame))
7294 return;
7296 if (! NILP (dpyinfo->mouse_face_window))
7297 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7299 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7300 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7301 dpyinfo->mouse_face_window = Qnil;
7305 /* Clear any mouse-face on window W. This function is part of the
7306 redisplay interface, and is called from try_window_id and similar
7307 functions to ensure the mouse-highlight is off. */
7309 static void
7310 x_clear_mouse_face (w)
7311 struct window *w;
7313 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7314 Lisp_Object window;
7316 BLOCK_INPUT;
7317 XSETWINDOW (window, w);
7318 if (EQ (window, dpyinfo->mouse_face_window))
7319 clear_mouse_face (dpyinfo);
7320 UNBLOCK_INPUT;
7324 /* Just discard the mouse face information for frame F, if any.
7325 This is used when the size of F is changed. */
7327 void
7328 cancel_mouse_face (f)
7329 FRAME_PTR f;
7331 Lisp_Object window;
7332 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7334 window = dpyinfo->mouse_face_window;
7335 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7337 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7338 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7339 dpyinfo->mouse_face_window = Qnil;
7343 static struct scroll_bar *x_window_to_scroll_bar ();
7344 static void x_scroll_bar_report_motion ();
7346 /* Return the current position of the mouse.
7347 *fp should be a frame which indicates which display to ask about.
7349 If the mouse movement started in a scroll bar, set *fp, *bar_window,
7350 and *part to the frame, window, and scroll bar part that the mouse
7351 is over. Set *x and *y to the portion and whole of the mouse's
7352 position on the scroll bar.
7354 If the mouse movement started elsewhere, set *fp to the frame the
7355 mouse is on, *bar_window to nil, and *x and *y to the character cell
7356 the mouse is over.
7358 Set *time to the server time-stamp for the time at which the mouse
7359 was at this position.
7361 Don't store anything if we don't have a valid set of values to report.
7363 This clears the mouse_moved flag, so we can wait for the next mouse
7364 movement. */
7366 static void
7367 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7368 FRAME_PTR *fp;
7369 int insist;
7370 Lisp_Object *bar_window;
7371 enum scroll_bar_part *part;
7372 Lisp_Object *x, *y;
7373 unsigned long *time;
7375 FRAME_PTR f1;
7377 BLOCK_INPUT;
7379 if (! NILP (last_mouse_scroll_bar) && insist == 0)
7380 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
7381 else
7383 Window root;
7384 int root_x, root_y;
7386 Window dummy_window;
7387 int dummy;
7389 Lisp_Object frame, tail;
7391 /* Clear the mouse-moved flag for every frame on this display. */
7392 FOR_EACH_FRAME (tail, frame)
7393 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
7394 XFRAME (frame)->mouse_moved = 0;
7396 last_mouse_scroll_bar = Qnil;
7398 /* Figure out which root window we're on. */
7399 XQueryPointer (FRAME_X_DISPLAY (*fp),
7400 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
7402 /* The root window which contains the pointer. */
7403 &root,
7405 /* Trash which we can't trust if the pointer is on
7406 a different screen. */
7407 &dummy_window,
7409 /* The position on that root window. */
7410 &root_x, &root_y,
7412 /* More trash we can't trust. */
7413 &dummy, &dummy,
7415 /* Modifier keys and pointer buttons, about which
7416 we don't care. */
7417 (unsigned int *) &dummy);
7419 /* Now we have a position on the root; find the innermost window
7420 containing the pointer. */
7422 Window win, child;
7423 int win_x, win_y;
7424 int parent_x = 0, parent_y = 0;
7425 int count;
7427 win = root;
7429 /* XTranslateCoordinates can get errors if the window
7430 structure is changing at the same time this function
7431 is running. So at least we must not crash from them. */
7433 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
7435 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
7436 && FRAME_LIVE_P (last_mouse_frame))
7438 /* If mouse was grabbed on a frame, give coords for that frame
7439 even if the mouse is now outside it. */
7440 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7442 /* From-window, to-window. */
7443 root, FRAME_X_WINDOW (last_mouse_frame),
7445 /* From-position, to-position. */
7446 root_x, root_y, &win_x, &win_y,
7448 /* Child of win. */
7449 &child);
7450 f1 = last_mouse_frame;
7452 else
7454 while (1)
7456 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
7458 /* From-window, to-window. */
7459 root, win,
7461 /* From-position, to-position. */
7462 root_x, root_y, &win_x, &win_y,
7464 /* Child of win. */
7465 &child);
7467 if (child == None || child == win)
7468 break;
7470 win = child;
7471 parent_x = win_x;
7472 parent_y = win_y;
7475 /* Now we know that:
7476 win is the innermost window containing the pointer
7477 (XTC says it has no child containing the pointer),
7478 win_x and win_y are the pointer's position in it
7479 (XTC did this the last time through), and
7480 parent_x and parent_y are the pointer's position in win's parent.
7481 (They are what win_x and win_y were when win was child.
7482 If win is the root window, it has no parent, and
7483 parent_{x,y} are invalid, but that's okay, because we'll
7484 never use them in that case.) */
7486 /* Is win one of our frames? */
7487 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
7489 #ifdef USE_X_TOOLKIT
7490 /* If we end up with the menu bar window, say it's not
7491 on the frame. */
7492 if (f1 != NULL
7493 && f1->output_data.x->menubar_widget
7494 && win == XtWindow (f1->output_data.x->menubar_widget))
7495 f1 = NULL;
7496 #endif /* USE_X_TOOLKIT */
7499 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
7500 f1 = 0;
7502 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
7504 /* If not, is it one of our scroll bars? */
7505 if (! f1)
7507 struct scroll_bar *bar = x_window_to_scroll_bar (win);
7509 if (bar)
7511 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
7512 win_x = parent_x;
7513 win_y = parent_y;
7517 if (f1 == 0 && insist > 0)
7518 f1 = SELECTED_FRAME ();
7520 if (f1)
7522 /* Ok, we found a frame. Store all the values.
7523 last_mouse_glyph is a rectangle used to reduce the
7524 generation of mouse events. To not miss any motion
7525 events, we must divide the frame into rectangles of the
7526 size of the smallest character that could be displayed
7527 on it, i.e. into the same rectangles that matrices on
7528 the frame are divided into. */
7530 #if OLD_REDISPLAY_CODE
7531 int ignore1, ignore2;
7532 pixel_to_glyph_coords (f1, win_x, win_y, &ignore1, &ignore2,
7533 &last_mouse_glyph,
7534 FRAME_X_DISPLAY_INFO (f1)->grabbed
7535 || insist);
7536 #else
7538 int width = FRAME_SMALLEST_CHAR_WIDTH (f1);
7539 int height = FRAME_SMALLEST_FONT_HEIGHT (f1);
7540 int x = win_x;
7541 int y = win_y;
7543 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
7544 round down even for negative values. */
7545 if (x < 0)
7546 x -= width - 1;
7547 if (y < 0)
7548 y -= height - 1;
7550 last_mouse_glyph.width = width;
7551 last_mouse_glyph.height = height;
7552 last_mouse_glyph.x = (x + width - 1) / width * width;
7553 last_mouse_glyph.y = (y + height - 1) / height * height;
7555 #endif
7557 *bar_window = Qnil;
7558 *part = 0;
7559 *fp = f1;
7560 XSETINT (*x, win_x);
7561 XSETINT (*y, win_y);
7562 *time = last_mouse_movement_time;
7567 UNBLOCK_INPUT;
7571 #ifdef USE_X_TOOLKIT
7573 /* Atimer callback function for TIMER. Called every 0.1s to process
7574 Xt timeouts, if needed. We must avoid calling XtAppPending as
7575 much as possible because that function does an implicit XFlush
7576 that slows us down. */
7578 static void
7579 x_process_timeouts (timer)
7580 struct atimer *timer;
7582 if (toolkit_scroll_bar_interaction || popup_activated_flag)
7584 BLOCK_INPUT;
7585 while (XtAppPending (Xt_app_con) & XtIMTimer)
7586 XtAppProcessEvent (Xt_app_con, XtIMTimer);
7587 UNBLOCK_INPUT;
7591 #endif /* USE_X_TOOLKIT */
7594 /* Scroll bar support. */
7596 /* Given an X window ID, find the struct scroll_bar which manages it.
7597 This can be called in GC, so we have to make sure to strip off mark
7598 bits. */
7600 static struct scroll_bar *
7601 x_window_to_scroll_bar (window_id)
7602 Window window_id;
7604 Lisp_Object tail;
7606 for (tail = Vframe_list;
7607 XGCTYPE (tail) == Lisp_Cons;
7608 tail = XCDR (tail))
7610 Lisp_Object frame, bar, condemned;
7612 frame = XCAR (tail);
7613 /* All elements of Vframe_list should be frames. */
7614 if (! GC_FRAMEP (frame))
7615 abort ();
7617 /* Scan this frame's scroll bar list for a scroll bar with the
7618 right window ID. */
7619 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
7620 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
7621 /* This trick allows us to search both the ordinary and
7622 condemned scroll bar lists with one loop. */
7623 ! GC_NILP (bar) || (bar = condemned,
7624 condemned = Qnil,
7625 ! GC_NILP (bar));
7626 bar = XSCROLL_BAR (bar)->next)
7627 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
7628 return XSCROLL_BAR (bar);
7631 return 0;
7636 /************************************************************************
7637 Toolkit scroll bars
7638 ************************************************************************/
7640 #ifdef USE_TOOLKIT_SCROLL_BARS
7642 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
7643 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
7644 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
7645 struct scroll_bar *));
7646 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
7647 int, int, int));
7650 /* Id of action hook installed for scroll bars. */
7652 static XtActionHookId action_hook_id;
7654 /* Lisp window being scrolled. Set when starting to interact with
7655 a toolkit scroll bar, reset to nil when ending the interaction. */
7657 static Lisp_Object window_being_scrolled;
7659 /* Last scroll bar part sent in xm_scroll_callback. */
7661 static int last_scroll_bar_part;
7663 /* Whether this is an Xaw with arrow-scrollbars. This should imply
7664 that movements of 1/20 of the screen size are mapped to up/down. */
7666 static Boolean xaw3d_arrow_scroll;
7668 /* Whether the drag scrolling maintains the mouse at the top of the
7669 thumb. If not, resizing the thumb needs to be done more carefully
7670 to avoid jerkyness. */
7672 static Boolean xaw3d_pick_top;
7675 /* Action hook installed via XtAppAddActionHook when toolkit scroll
7676 bars are used.. The hook is responsible for detecting when
7677 the user ends an interaction with the scroll bar, and generates
7678 a `end-scroll' scroll_bar_click' event if so. */
7680 static void
7681 xt_action_hook (widget, client_data, action_name, event, params,
7682 num_params)
7683 Widget widget;
7684 XtPointer client_data;
7685 String action_name;
7686 XEvent *event;
7687 String *params;
7688 Cardinal *num_params;
7690 int scroll_bar_p;
7691 char *end_action;
7693 #ifdef USE_MOTIF
7694 scroll_bar_p = XmIsScrollBar (widget);
7695 end_action = "Release";
7696 #else /* !USE_MOTIF i.e. use Xaw */
7697 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
7698 end_action = "EndScroll";
7699 #endif /* USE_MOTIF */
7701 if (scroll_bar_p
7702 && strcmp (action_name, end_action) == 0
7703 && WINDOWP (window_being_scrolled))
7705 struct window *w;
7707 x_send_scroll_bar_event (window_being_scrolled,
7708 scroll_bar_end_scroll, 0, 0);
7709 w = XWINDOW (window_being_scrolled);
7710 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
7711 window_being_scrolled = Qnil;
7712 last_scroll_bar_part = -1;
7714 /* Xt timeouts no longer needed. */
7715 toolkit_scroll_bar_interaction = 0;
7719 /* A vector of windows used for communication between
7720 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
7722 static struct window **scroll_bar_windows;
7723 static int scroll_bar_windows_size;
7726 /* Send a client message with message type Xatom_Scrollbar for a
7727 scroll action to the frame of WINDOW. PART is a value identifying
7728 the part of the scroll bar that was clicked on. PORTION is the
7729 amount to scroll of a whole of WHOLE. */
7731 static void
7732 x_send_scroll_bar_event (window, part, portion, whole)
7733 Lisp_Object window;
7734 int part, portion, whole;
7736 XEvent event;
7737 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
7738 struct window *w = XWINDOW (window);
7739 struct frame *f = XFRAME (w->frame);
7740 int i;
7742 BLOCK_INPUT;
7744 /* Construct a ClientMessage event to send to the frame. */
7745 ev->type = ClientMessage;
7746 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
7747 ev->display = FRAME_X_DISPLAY (f);
7748 ev->window = FRAME_X_WINDOW (f);
7749 ev->format = 32;
7751 /* We can only transfer 32 bits in the XClientMessageEvent, which is
7752 not enough to store a pointer or Lisp_Object on a 64 bit system.
7753 So, store the window in scroll_bar_windows and pass the index
7754 into that array in the event. */
7755 for (i = 0; i < scroll_bar_windows_size; ++i)
7756 if (scroll_bar_windows[i] == NULL)
7757 break;
7759 if (i == scroll_bar_windows_size)
7761 int new_size = max (10, 2 * scroll_bar_windows_size);
7762 size_t nbytes = new_size * sizeof *scroll_bar_windows;
7763 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
7765 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
7766 nbytes);
7767 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
7768 scroll_bar_windows_size = new_size;
7771 scroll_bar_windows[i] = w;
7772 ev->data.l[0] = (long) i;
7773 ev->data.l[1] = (long) part;
7774 ev->data.l[2] = (long) 0;
7775 ev->data.l[3] = (long) portion;
7776 ev->data.l[4] = (long) whole;
7778 /* Make Xt timeouts work while the scroll bar is active. */
7779 toolkit_scroll_bar_interaction = 1;
7781 /* Setting the event mask to zero means that the message will
7782 be sent to the client that created the window, and if that
7783 window no longer exists, no event will be sent. */
7784 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
7785 UNBLOCK_INPUT;
7789 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
7790 in *IEVENT. */
7792 static void
7793 x_scroll_bar_to_input_event (event, ievent)
7794 XEvent *event;
7795 struct input_event *ievent;
7797 XClientMessageEvent *ev = (XClientMessageEvent *) event;
7798 Lisp_Object window;
7799 struct frame *f;
7800 struct window *w;
7802 w = scroll_bar_windows[ev->data.l[0]];
7803 scroll_bar_windows[ev->data.l[0]] = NULL;
7805 XSETWINDOW (window, w);
7806 f = XFRAME (w->frame);
7808 ievent->kind = scroll_bar_click;
7809 ievent->frame_or_window = window;
7810 ievent->arg = Qnil;
7811 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
7812 ievent->part = ev->data.l[1];
7813 ievent->code = ev->data.l[2];
7814 ievent->x = make_number ((int) ev->data.l[3]);
7815 ievent->y = make_number ((int) ev->data.l[4]);
7816 ievent->modifiers = 0;
7820 #ifdef USE_MOTIF
7822 /* Minimum and maximum values used for Motif scroll bars. */
7824 #define XM_SB_MIN 1
7825 #define XM_SB_MAX 10000000
7826 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
7829 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
7830 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
7831 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
7833 static void
7834 xm_scroll_callback (widget, client_data, call_data)
7835 Widget widget;
7836 XtPointer client_data, call_data;
7838 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7839 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
7840 double percent;
7841 int part = -1, whole = 0, portion = 0;
7843 switch (cs->reason)
7845 case XmCR_DECREMENT:
7846 bar->dragging = Qnil;
7847 part = scroll_bar_up_arrow;
7848 break;
7850 case XmCR_INCREMENT:
7851 bar->dragging = Qnil;
7852 part = scroll_bar_down_arrow;
7853 break;
7855 case XmCR_PAGE_DECREMENT:
7856 bar->dragging = Qnil;
7857 part = scroll_bar_above_handle;
7858 break;
7860 case XmCR_PAGE_INCREMENT:
7861 bar->dragging = Qnil;
7862 part = scroll_bar_below_handle;
7863 break;
7865 case XmCR_TO_TOP:
7866 bar->dragging = Qnil;
7867 part = scroll_bar_to_top;
7868 break;
7870 case XmCR_TO_BOTTOM:
7871 bar->dragging = Qnil;
7872 part = scroll_bar_to_bottom;
7873 break;
7875 case XmCR_DRAG:
7877 int slider_size;
7878 int dragging_down_p = (INTEGERP (bar->dragging)
7879 && XINT (bar->dragging) <= cs->value);
7881 /* Get the slider size. */
7882 BLOCK_INPUT;
7883 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
7884 UNBLOCK_INPUT;
7886 /* At the max position of the scroll bar, do a line-wise
7887 movement. Without doing anything, the LessTif scroll bar
7888 calls us with the same cs->value again and again. If we
7889 want to make sure that we can reach the end of the buffer,
7890 we have to do something.
7892 Implementation note: setting bar->dragging always to
7893 cs->value gives a smoother movement at the max position.
7894 Setting it to nil when doing line-wise movement gives
7895 a better slider behavior. */
7897 if (cs->value + slider_size == XM_SB_MAX
7898 || (dragging_down_p
7899 && last_scroll_bar_part == scroll_bar_down_arrow))
7901 part = scroll_bar_down_arrow;
7902 bar->dragging = Qnil;
7904 else
7906 whole = XM_SB_RANGE;
7907 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
7908 part = scroll_bar_handle;
7909 bar->dragging = make_number (cs->value);
7912 break;
7914 case XmCR_VALUE_CHANGED:
7915 break;
7918 if (part >= 0)
7920 window_being_scrolled = bar->window;
7921 last_scroll_bar_part = part;
7922 x_send_scroll_bar_event (bar->window, part, portion, whole);
7927 #else /* !USE_MOTIF, i.e. Xaw. */
7930 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
7931 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
7932 scroll bar struct. CALL_DATA is a pointer to a float saying where
7933 the thumb is. */
7935 static void
7936 xaw_jump_callback (widget, client_data, call_data)
7937 Widget widget;
7938 XtPointer client_data, call_data;
7940 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7941 float top = *(float *) call_data;
7942 float shown;
7943 int whole, portion, height;
7944 int part;
7946 /* Get the size of the thumb, a value between 0 and 1. */
7947 BLOCK_INPUT;
7948 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
7949 UNBLOCK_INPUT;
7951 whole = 10000000;
7952 portion = shown < 1 ? top * whole : 0;
7954 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
7955 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
7956 the bottom, so we force the scrolling whenever we see that we're
7957 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
7958 we try to ensure that we always stay two pixels away from the
7959 bottom). */
7960 part = scroll_bar_down_arrow;
7961 else
7962 part = scroll_bar_handle;
7964 window_being_scrolled = bar->window;
7965 bar->dragging = make_number (portion);
7966 last_scroll_bar_part = part;
7967 x_send_scroll_bar_event (bar->window, part, portion, whole);
7971 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
7972 i.e. line or page up or down. WIDGET is the Xaw scroll bar
7973 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
7974 the scroll bar. CALL_DATA is an integer specifying the action that
7975 has taken place. It's magnitude is in the range 0..height of the
7976 scroll bar. Negative values mean scroll towards buffer start.
7977 Values < height of scroll bar mean line-wise movement. */
7979 static void
7980 xaw_scroll_callback (widget, client_data, call_data)
7981 Widget widget;
7982 XtPointer client_data, call_data;
7984 struct scroll_bar *bar = (struct scroll_bar *) client_data;
7985 int position = (int) call_data;
7986 Dimension height;
7987 int part;
7989 /* Get the height of the scroll bar. */
7990 BLOCK_INPUT;
7991 XtVaGetValues (widget, XtNheight, &height, NULL);
7992 UNBLOCK_INPUT;
7994 if (abs (position) >= height)
7995 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
7997 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
7998 it maps line-movement to call_data = max(5, height/20). */
7999 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8000 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8001 else
8002 part = scroll_bar_move_ratio;
8004 window_being_scrolled = bar->window;
8005 bar->dragging = Qnil;
8006 last_scroll_bar_part = part;
8007 x_send_scroll_bar_event (bar->window, part, position, height);
8011 #endif /* not USE_MOTIF */
8014 /* Create the widget for scroll bar BAR on frame F. Record the widget
8015 and X window of the scroll bar in BAR. */
8017 static void
8018 x_create_toolkit_scroll_bar (f, bar)
8019 struct frame *f;
8020 struct scroll_bar *bar;
8022 Window xwindow;
8023 Widget widget;
8024 Arg av[20];
8025 int ac = 0;
8026 char *scroll_bar_name = "verticalScrollBar";
8027 unsigned long pixel;
8029 BLOCK_INPUT;
8031 #ifdef USE_MOTIF
8032 /* LessTif 0.85, problems:
8034 1. When the mouse if over the scroll bar, the scroll bar will
8035 get keyboard events. I didn't find a way to turn this off.
8037 2. Do we have to explicitly set the cursor to get an arrow
8038 cursor (see below)? */
8040 /* Set resources. Create the widget. */
8041 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8042 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8043 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8044 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8045 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8046 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8047 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8049 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8050 if (pixel != -1)
8052 XtSetArg (av[ac], XmNforeground, pixel);
8053 ++ac;
8056 pixel = f->output_data.x->scroll_bar_background_pixel;
8057 if (pixel != -1)
8059 XtSetArg (av[ac], XmNbackground, pixel);
8060 ++ac;
8063 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8064 scroll_bar_name, av, ac);
8066 /* Add one callback for everything that can happen. */
8067 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8068 (XtPointer) bar);
8069 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8070 (XtPointer) bar);
8071 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8072 (XtPointer) bar);
8073 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8074 (XtPointer) bar);
8075 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8076 (XtPointer) bar);
8077 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8078 (XtPointer) bar);
8079 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8080 (XtPointer) bar);
8082 /* Realize the widget. Only after that is the X window created. */
8083 XtRealizeWidget (widget);
8085 /* Set the cursor to an arrow. I didn't find a resource to do that.
8086 And I'm wondering why it hasn't an arrow cursor by default. */
8087 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8088 f->output_data.x->nontext_cursor);
8090 #else /* !USE_MOTIF i.e. use Xaw */
8092 /* Set resources. Create the widget. The background of the
8093 Xaw3d scroll bar widget is a little bit light for my taste.
8094 We don't alter it here to let users change it according
8095 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8096 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8097 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8098 /* For smoother scrolling with Xaw3d -sm */
8099 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8100 /* XtSetArg (av[ac], XtNbeNiceToColormap, True); ++ac; */
8102 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8103 if (pixel != -1)
8105 XtSetArg (av[ac], XtNforeground, pixel);
8106 ++ac;
8109 pixel = f->output_data.x->scroll_bar_background_pixel;
8110 if (pixel != -1)
8112 XtSetArg (av[ac], XtNbackground, pixel);
8113 ++ac;
8116 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8117 f->output_data.x->edit_widget, av, ac);
8120 char *initial = "";
8121 char *val = initial;
8122 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8123 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8124 if (val == initial)
8125 { /* ARROW_SCROLL */
8126 xaw3d_arrow_scroll = True;
8127 /* Isn't that just a personal preference ? -sm */
8128 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8132 /* Define callbacks. */
8133 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8134 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8135 (XtPointer) bar);
8137 /* Realize the widget. Only after that is the X window created. */
8138 XtRealizeWidget (widget);
8140 #endif /* !USE_MOTIF */
8142 /* Install an action hook that let's us detect when the user
8143 finishes interacting with a scroll bar. */
8144 if (action_hook_id == 0)
8145 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8147 /* Remember X window and widget in the scroll bar vector. */
8148 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8149 xwindow = XtWindow (widget);
8150 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8152 UNBLOCK_INPUT;
8156 /* Set the thumb size and position of scroll bar BAR. We are currently
8157 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8159 static void
8160 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8161 struct scroll_bar *bar;
8162 int portion, position, whole;
8164 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8165 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8166 float top, shown;
8168 if (whole == 0)
8169 top = 0, shown = 1;
8170 else
8172 top = (float) position / whole;
8173 shown = (float) portion / whole;
8176 BLOCK_INPUT;
8178 #ifdef USE_MOTIF
8180 int size, value;
8181 Boolean arrow1_selected, arrow2_selected;
8182 unsigned char flags;
8183 XmScrollBarWidget sb;
8185 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8186 is the scroll bar's maximum and MIN is the scroll bar's minimum
8187 value. */
8188 size = shown * XM_SB_RANGE;
8189 size = min (size, XM_SB_RANGE);
8190 size = max (size, 1);
8192 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8193 value = top * XM_SB_RANGE;
8194 value = min (value, XM_SB_MAX - size);
8195 value = max (value, XM_SB_MIN);
8197 /* LessTif: Calling XmScrollBarSetValues after an increment or
8198 decrement turns off auto-repeat LessTif-internally. This can
8199 be seen in ScrollBar.c which resets Arrow1Selected and
8200 Arrow2Selected. It also sets internal flags so that LessTif
8201 believes the mouse is in the slider. We either have to change
8202 our code, or work around that by accessing private data. */
8204 sb = (XmScrollBarWidget) widget;
8205 arrow1_selected = sb->scrollBar.arrow1_selected;
8206 arrow2_selected = sb->scrollBar.arrow2_selected;
8207 flags = sb->scrollBar.flags;
8209 if (NILP (bar->dragging))
8210 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8211 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8212 /* This has the negative side effect that the slider value is
8213 not what it would be if we scrolled here using line-wise or
8214 page-wise movement. */
8215 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8216 else
8218 /* If currently dragging, only update the slider size.
8219 This reduces flicker effects. */
8220 int old_value, old_size, increment, page_increment;
8222 XmScrollBarGetValues (widget, &old_value, &old_size,
8223 &increment, &page_increment);
8224 XmScrollBarSetValues (widget, old_value,
8225 min (size, XM_SB_RANGE - old_value),
8226 0, 0, False);
8229 sb->scrollBar.arrow1_selected = arrow1_selected;
8230 sb->scrollBar.arrow2_selected = arrow2_selected;
8231 sb->scrollBar.flags = flags;
8233 #else /* !USE_MOTIF i.e. use Xaw */
8235 float old_top, old_shown;
8236 Dimension height;
8237 XtVaGetValues (widget,
8238 XtNtopOfThumb, &old_top,
8239 XtNshown, &old_shown,
8240 XtNheight, &height,
8241 NULL);
8243 /* Massage the top+shown values. */
8244 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8245 top = max (0, min (1, top));
8246 else
8247 top = old_top;
8248 /* Keep two pixels available for moving the thumb down. */
8249 shown = max (0, min (1 - top - (2.0 / height), shown));
8251 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8252 check that your system's configuration file contains a define
8253 for `NARROWPROTO'. See s/freebsd.h for an example. */
8254 if (top != old_top || shown != old_shown)
8256 if (NILP (bar->dragging))
8257 XawScrollbarSetThumb (widget, top, shown);
8258 else
8260 #ifdef HAVE_XAW3D
8261 ScrollbarWidget sb = (ScrollbarWidget) widget;
8262 int scroll_mode = 0;
8264 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8265 if (xaw3d_arrow_scroll)
8267 /* Xaw3d stupidly ignores resize requests while dragging
8268 so we have to make it believe it's not in dragging mode. */
8269 scroll_mode = sb->scrollbar.scroll_mode;
8270 if (scroll_mode == 2)
8271 sb->scrollbar.scroll_mode = 0;
8273 #endif
8274 /* Try to make the scrolling a tad smoother. */
8275 if (!xaw3d_pick_top)
8276 shown = min (shown, old_shown);
8278 XawScrollbarSetThumb (widget, top, shown);
8280 #ifdef HAVE_XAW3D
8281 if (xaw3d_arrow_scroll && scroll_mode == 2)
8282 sb->scrollbar.scroll_mode = scroll_mode;
8283 #endif
8287 #endif /* !USE_MOTIF */
8289 UNBLOCK_INPUT;
8292 #endif /* USE_TOOLKIT_SCROLL_BARS */
8296 /************************************************************************
8297 Scroll bars, general
8298 ************************************************************************/
8300 /* Create a scroll bar and return the scroll bar vector for it. W is
8301 the Emacs window on which to create the scroll bar. TOP, LEFT,
8302 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8303 scroll bar. */
8305 static struct scroll_bar *
8306 x_scroll_bar_create (w, top, left, width, height)
8307 struct window *w;
8308 int top, left, width, height;
8310 struct frame *f = XFRAME (w->frame);
8311 struct scroll_bar *bar
8312 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8314 BLOCK_INPUT;
8316 #ifdef USE_TOOLKIT_SCROLL_BARS
8317 x_create_toolkit_scroll_bar (f, bar);
8318 #else /* not USE_TOOLKIT_SCROLL_BARS */
8320 XSetWindowAttributes a;
8321 unsigned long mask;
8322 Window window;
8324 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8325 if (a.background_pixel == -1)
8326 a.background_pixel = f->output_data.x->background_pixel;
8328 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8329 | ButtonMotionMask | PointerMotionHintMask
8330 | ExposureMask);
8331 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
8333 mask = (CWBackPixel | CWEventMask | CWCursor);
8335 /* Clear the area of W that will serve as a scroll bar. This is
8336 for the case that a window has been split horizontally. In
8337 this case, no clear_frame is generated to reduce flickering. */
8338 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8339 left, top, width,
8340 window_box_height (w), False);
8342 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8343 /* Position and size of scroll bar. */
8344 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8345 top,
8346 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8347 height,
8348 /* Border width, depth, class, and visual. */
8350 CopyFromParent,
8351 CopyFromParent,
8352 CopyFromParent,
8353 /* Attributes. */
8354 mask, &a);
8355 SET_SCROLL_BAR_X_WINDOW (bar, window);
8357 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8359 XSETWINDOW (bar->window, w);
8360 XSETINT (bar->top, top);
8361 XSETINT (bar->left, left);
8362 XSETINT (bar->width, width);
8363 XSETINT (bar->height, height);
8364 XSETINT (bar->start, 0);
8365 XSETINT (bar->end, 0);
8366 bar->dragging = Qnil;
8368 /* Add bar to its frame's list of scroll bars. */
8369 bar->next = FRAME_SCROLL_BARS (f);
8370 bar->prev = Qnil;
8371 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8372 if (!NILP (bar->next))
8373 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8375 /* Map the window/widget. */
8376 #ifdef USE_TOOLKIT_SCROLL_BARS
8378 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8379 XtConfigureWidget (scroll_bar,
8380 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8381 top,
8382 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8383 height, 0);
8384 XtMapWidget (scroll_bar);
8386 #else /* not USE_TOOLKIT_SCROLL_BARS */
8387 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8388 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8390 UNBLOCK_INPUT;
8391 return bar;
8395 /* Draw BAR's handle in the proper position.
8397 If the handle is already drawn from START to END, don't bother
8398 redrawing it, unless REBUILD is non-zero; in that case, always
8399 redraw it. (REBUILD is handy for drawing the handle after expose
8400 events.)
8402 Normally, we want to constrain the start and end of the handle to
8403 fit inside its rectangle, but if the user is dragging the scroll
8404 bar handle, we want to let them drag it down all the way, so that
8405 the bar's top is as far down as it goes; otherwise, there's no way
8406 to move to the very end of the buffer. */
8408 #ifndef USE_TOOLKIT_SCROLL_BARS
8410 static void
8411 x_scroll_bar_set_handle (bar, start, end, rebuild)
8412 struct scroll_bar *bar;
8413 int start, end;
8414 int rebuild;
8416 int dragging = ! NILP (bar->dragging);
8417 Window w = SCROLL_BAR_X_WINDOW (bar);
8418 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8419 GC gc = f->output_data.x->normal_gc;
8421 /* If the display is already accurate, do nothing. */
8422 if (! rebuild
8423 && start == XINT (bar->start)
8424 && end == XINT (bar->end))
8425 return;
8427 BLOCK_INPUT;
8430 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
8431 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8432 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8434 /* Make sure the values are reasonable, and try to preserve
8435 the distance between start and end. */
8437 int length = end - start;
8439 if (start < 0)
8440 start = 0;
8441 else if (start > top_range)
8442 start = top_range;
8443 end = start + length;
8445 if (end < start)
8446 end = start;
8447 else if (end > top_range && ! dragging)
8448 end = top_range;
8451 /* Store the adjusted setting in the scroll bar. */
8452 XSETINT (bar->start, start);
8453 XSETINT (bar->end, end);
8455 /* Clip the end position, just for display. */
8456 if (end > top_range)
8457 end = top_range;
8459 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
8460 below top positions, to make sure the handle is always at least
8461 that many pixels tall. */
8462 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
8464 /* Draw the empty space above the handle. Note that we can't clear
8465 zero-height areas; that means "clear to end of window." */
8466 if (0 < start)
8467 XClearArea (FRAME_X_DISPLAY (f), w,
8469 /* x, y, width, height, and exposures. */
8470 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8471 VERTICAL_SCROLL_BAR_TOP_BORDER,
8472 inside_width, start,
8473 False);
8475 /* Change to proper foreground color if one is specified. */
8476 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8477 XSetForeground (FRAME_X_DISPLAY (f), gc,
8478 f->output_data.x->scroll_bar_foreground_pixel);
8480 /* Draw the handle itself. */
8481 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
8483 /* x, y, width, height */
8484 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8485 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
8486 inside_width, end - start);
8488 /* Restore the foreground color of the GC if we changed it above. */
8489 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
8490 XSetForeground (FRAME_X_DISPLAY (f), gc,
8491 f->output_data.x->foreground_pixel);
8493 /* Draw the empty space below the handle. Note that we can't
8494 clear zero-height areas; that means "clear to end of window." */
8495 if (end < inside_height)
8496 XClearArea (FRAME_X_DISPLAY (f), w,
8498 /* x, y, width, height, and exposures. */
8499 VERTICAL_SCROLL_BAR_LEFT_BORDER,
8500 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
8501 inside_width, inside_height - end,
8502 False);
8506 UNBLOCK_INPUT;
8509 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8511 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
8512 nil. */
8514 static void
8515 x_scroll_bar_remove (bar)
8516 struct scroll_bar *bar;
8518 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8519 BLOCK_INPUT;
8521 #ifdef USE_TOOLKIT_SCROLL_BARS
8522 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
8523 #else
8524 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
8525 #endif
8527 /* Disassociate this scroll bar from its window. */
8528 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
8530 UNBLOCK_INPUT;
8534 /* Set the handle of the vertical scroll bar for WINDOW to indicate
8535 that we are displaying PORTION characters out of a total of WHOLE
8536 characters, starting at POSITION. If WINDOW has no scroll bar,
8537 create one. */
8539 static void
8540 XTset_vertical_scroll_bar (w, portion, whole, position)
8541 struct window *w;
8542 int portion, whole, position;
8544 struct frame *f = XFRAME (w->frame);
8545 struct scroll_bar *bar;
8546 int top, height, left, sb_left, width, sb_width;
8547 int window_x, window_y, window_width, window_height;
8549 /* Get window dimensions. */
8550 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
8551 top = window_y;
8552 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
8553 height = window_height;
8555 /* Compute the left edge of the scroll bar area. */
8556 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8557 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
8558 else
8559 left = XFASTINT (w->left);
8560 left *= CANON_X_UNIT (f);
8561 left += FRAME_INTERNAL_BORDER_WIDTH (f);
8563 /* Compute the width of the scroll bar which might be less than
8564 the width of the area reserved for the scroll bar. */
8565 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
8566 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
8567 else
8568 sb_width = width;
8570 /* Compute the left edge of the scroll bar. */
8571 #ifdef USE_TOOLKIT_SCROLL_BARS
8572 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8573 sb_left = left + width - sb_width - (width - sb_width) / 2;
8574 else
8575 sb_left = left + (width - sb_width) / 2;
8576 #else
8577 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
8578 sb_left = left + width - sb_width;
8579 else
8580 sb_left = left;
8581 #endif
8583 /* Does the scroll bar exist yet? */
8584 if (NILP (w->vertical_scroll_bar))
8586 BLOCK_INPUT;
8587 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8588 left, top, width, height, False);
8589 UNBLOCK_INPUT;
8590 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
8592 else
8594 /* It may just need to be moved and resized. */
8595 unsigned int mask = 0;
8597 bar = XSCROLL_BAR (w->vertical_scroll_bar);
8599 BLOCK_INPUT;
8601 if (sb_left != XINT (bar->left))
8602 mask |= CWX;
8603 if (top != XINT (bar->top))
8604 mask |= CWY;
8605 if (sb_width != XINT (bar->width))
8606 mask |= CWWidth;
8607 if (height != XINT (bar->height))
8608 mask |= CWHeight;
8610 #ifdef USE_TOOLKIT_SCROLL_BARS
8612 /* Since toolkit scroll bars are smaller than the space reserved
8613 for them on the frame, we have to clear "under" them. */
8614 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8615 left, top, width, height, False);
8617 /* Move/size the scroll bar widget. */
8618 if (mask)
8619 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
8620 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8621 top,
8622 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
8623 height, 0);
8625 #else /* not USE_TOOLKIT_SCROLL_BARS */
8627 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
8629 /* Clear areas not covered by the scroll bar. This makes sure a
8630 previous mode line display is cleared after C-x 2 C-x 1, for
8631 example. Non-toolkit scroll bars are as wide as the area
8632 reserved for scroll bars - trim at both sides. */
8633 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8634 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8635 height, False);
8636 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
8637 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8638 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
8639 height, False);
8642 /* Move/size the scroll bar window. */
8643 if (mask)
8645 XWindowChanges wc;
8647 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8648 wc.y = top;
8649 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
8650 wc.height = height;
8651 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
8652 mask, &wc);
8655 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8657 /* Remember new settings. */
8658 XSETINT (bar->left, sb_left);
8659 XSETINT (bar->top, top);
8660 XSETINT (bar->width, sb_width);
8661 XSETINT (bar->height, height);
8663 UNBLOCK_INPUT;
8666 #ifdef USE_TOOLKIT_SCROLL_BARS
8667 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
8668 #else /* not USE_TOOLKIT_SCROLL_BARS */
8669 /* Set the scroll bar's current state, unless we're currently being
8670 dragged. */
8671 if (NILP (bar->dragging))
8673 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
8675 if (whole == 0)
8676 x_scroll_bar_set_handle (bar, 0, top_range, 0);
8677 else
8679 int start = ((double) position * top_range) / whole;
8680 int end = ((double) (position + portion) * top_range) / whole;
8681 x_scroll_bar_set_handle (bar, start, end, 0);
8684 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8686 XSETVECTOR (w->vertical_scroll_bar, bar);
8690 /* The following three hooks are used when we're doing a thorough
8691 redisplay of the frame. We don't explicitly know which scroll bars
8692 are going to be deleted, because keeping track of when windows go
8693 away is a real pain - "Can you say set-window-configuration, boys
8694 and girls?" Instead, we just assert at the beginning of redisplay
8695 that *all* scroll bars are to be removed, and then save a scroll bar
8696 from the fiery pit when we actually redisplay its window. */
8698 /* Arrange for all scroll bars on FRAME to be removed at the next call
8699 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
8700 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
8702 static void
8703 XTcondemn_scroll_bars (frame)
8704 FRAME_PTR frame;
8706 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
8707 while (! NILP (FRAME_SCROLL_BARS (frame)))
8709 Lisp_Object bar;
8710 bar = FRAME_SCROLL_BARS (frame);
8711 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
8712 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
8713 XSCROLL_BAR (bar)->prev = Qnil;
8714 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
8715 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
8716 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
8721 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
8722 Note that WINDOW isn't necessarily condemned at all. */
8724 static void
8725 XTredeem_scroll_bar (window)
8726 struct window *window;
8728 struct scroll_bar *bar;
8729 struct frame *f;
8731 /* We can't redeem this window's scroll bar if it doesn't have one. */
8732 if (NILP (window->vertical_scroll_bar))
8733 abort ();
8735 bar = XSCROLL_BAR (window->vertical_scroll_bar);
8737 /* Unlink it from the condemned list. */
8738 f = XFRAME (WINDOW_FRAME (window));
8739 if (NILP (bar->prev))
8741 /* If the prev pointer is nil, it must be the first in one of
8742 the lists. */
8743 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
8744 /* It's not condemned. Everything's fine. */
8745 return;
8746 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
8747 window->vertical_scroll_bar))
8748 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
8749 else
8750 /* If its prev pointer is nil, it must be at the front of
8751 one or the other! */
8752 abort ();
8754 else
8755 XSCROLL_BAR (bar->prev)->next = bar->next;
8757 if (! NILP (bar->next))
8758 XSCROLL_BAR (bar->next)->prev = bar->prev;
8760 bar->next = FRAME_SCROLL_BARS (f);
8761 bar->prev = Qnil;
8762 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
8763 if (! NILP (bar->next))
8764 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
8767 /* Remove all scroll bars on FRAME that haven't been saved since the
8768 last call to `*condemn_scroll_bars_hook'. */
8770 static void
8771 XTjudge_scroll_bars (f)
8772 FRAME_PTR f;
8774 Lisp_Object bar, next;
8776 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
8778 /* Clear out the condemned list now so we won't try to process any
8779 more events on the hapless scroll bars. */
8780 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
8782 for (; ! NILP (bar); bar = next)
8784 struct scroll_bar *b = XSCROLL_BAR (bar);
8786 x_scroll_bar_remove (b);
8788 next = b->next;
8789 b->next = b->prev = Qnil;
8792 /* Now there should be no references to the condemned scroll bars,
8793 and they should get garbage-collected. */
8797 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
8798 is a no-op when using toolkit scroll bars.
8800 This may be called from a signal handler, so we have to ignore GC
8801 mark bits. */
8803 static void
8804 x_scroll_bar_expose (bar, event)
8805 struct scroll_bar *bar;
8806 XEvent *event;
8808 #ifndef USE_TOOLKIT_SCROLL_BARS
8810 Window w = SCROLL_BAR_X_WINDOW (bar);
8811 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8812 GC gc = f->output_data.x->normal_gc;
8813 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
8815 BLOCK_INPUT;
8817 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
8819 /* Draw a one-pixel border just inside the edges of the scroll bar. */
8820 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
8822 /* x, y, width, height */
8823 0, 0,
8824 XINT (bar->width) - 1 - width_trim - width_trim,
8825 XINT (bar->height) - 1);
8827 UNBLOCK_INPUT;
8829 #endif /* not USE_TOOLKIT_SCROLL_BARS */
8832 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
8833 is set to something other than no_event, it is enqueued.
8835 This may be called from a signal handler, so we have to ignore GC
8836 mark bits. */
8838 #ifndef USE_TOOLKIT_SCROLL_BARS
8840 static void
8841 x_scroll_bar_handle_click (bar, event, emacs_event)
8842 struct scroll_bar *bar;
8843 XEvent *event;
8844 struct input_event *emacs_event;
8846 if (! GC_WINDOWP (bar->window))
8847 abort ();
8849 emacs_event->kind = scroll_bar_click;
8850 emacs_event->code = event->xbutton.button - Button1;
8851 emacs_event->modifiers
8852 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
8853 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
8854 event->xbutton.state)
8855 | (event->type == ButtonRelease
8856 ? up_modifier
8857 : down_modifier));
8858 emacs_event->frame_or_window = bar->window;
8859 emacs_event->arg = Qnil;
8860 emacs_event->timestamp = event->xbutton.time;
8862 #if 0
8863 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8864 int internal_height
8865 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8866 #endif
8867 int top_range
8868 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8869 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
8871 if (y < 0) y = 0;
8872 if (y > top_range) y = top_range;
8874 if (y < XINT (bar->start))
8875 emacs_event->part = scroll_bar_above_handle;
8876 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
8877 emacs_event->part = scroll_bar_handle;
8878 else
8879 emacs_event->part = scroll_bar_below_handle;
8881 /* Just because the user has clicked on the handle doesn't mean
8882 they want to drag it. Lisp code needs to be able to decide
8883 whether or not we're dragging. */
8884 #if 0
8885 /* If the user has just clicked on the handle, record where they're
8886 holding it. */
8887 if (event->type == ButtonPress
8888 && emacs_event->part == scroll_bar_handle)
8889 XSETINT (bar->dragging, y - XINT (bar->start));
8890 #endif
8892 /* If the user has released the handle, set it to its final position. */
8893 if (event->type == ButtonRelease
8894 && ! NILP (bar->dragging))
8896 int new_start = y - XINT (bar->dragging);
8897 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8899 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8900 bar->dragging = Qnil;
8903 /* Same deal here as the other #if 0. */
8904 #if 0
8905 /* Clicks on the handle are always reported as occurring at the top of
8906 the handle. */
8907 if (emacs_event->part == scroll_bar_handle)
8908 emacs_event->x = bar->start;
8909 else
8910 XSETINT (emacs_event->x, y);
8911 #else
8912 XSETINT (emacs_event->x, y);
8913 #endif
8915 XSETINT (emacs_event->y, top_range);
8919 /* Handle some mouse motion while someone is dragging the scroll bar.
8921 This may be called from a signal handler, so we have to ignore GC
8922 mark bits. */
8924 static void
8925 x_scroll_bar_note_movement (bar, event)
8926 struct scroll_bar *bar;
8927 XEvent *event;
8929 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
8931 last_mouse_movement_time = event->xmotion.time;
8933 f->mouse_moved = 1;
8934 XSETVECTOR (last_mouse_scroll_bar, bar);
8936 /* If we're dragging the bar, display it. */
8937 if (! GC_NILP (bar->dragging))
8939 /* Where should the handle be now? */
8940 int new_start = event->xmotion.y - XINT (bar->dragging);
8942 if (new_start != XINT (bar->start))
8944 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
8946 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
8951 #endif /* !USE_TOOLKIT_SCROLL_BARS */
8953 /* Return information to the user about the current position of the mouse
8954 on the scroll bar. */
8956 static void
8957 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
8958 FRAME_PTR *fp;
8959 Lisp_Object *bar_window;
8960 enum scroll_bar_part *part;
8961 Lisp_Object *x, *y;
8962 unsigned long *time;
8964 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
8965 Window w = SCROLL_BAR_X_WINDOW (bar);
8966 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8967 int win_x, win_y;
8968 Window dummy_window;
8969 int dummy_coord;
8970 unsigned int dummy_mask;
8972 BLOCK_INPUT;
8974 /* Get the mouse's position relative to the scroll bar window, and
8975 report that. */
8976 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
8978 /* Root, child, root x and root y. */
8979 &dummy_window, &dummy_window,
8980 &dummy_coord, &dummy_coord,
8982 /* Position relative to scroll bar. */
8983 &win_x, &win_y,
8985 /* Mouse buttons and modifier keys. */
8986 &dummy_mask))
8988 else
8990 #if 0
8991 int inside_height
8992 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
8993 #endif
8994 int top_range
8995 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
8997 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
8999 if (! NILP (bar->dragging))
9000 win_y -= XINT (bar->dragging);
9002 if (win_y < 0)
9003 win_y = 0;
9004 if (win_y > top_range)
9005 win_y = top_range;
9007 *fp = f;
9008 *bar_window = bar->window;
9010 if (! NILP (bar->dragging))
9011 *part = scroll_bar_handle;
9012 else if (win_y < XINT (bar->start))
9013 *part = scroll_bar_above_handle;
9014 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9015 *part = scroll_bar_handle;
9016 else
9017 *part = scroll_bar_below_handle;
9019 XSETINT (*x, win_y);
9020 XSETINT (*y, top_range);
9022 f->mouse_moved = 0;
9023 last_mouse_scroll_bar = Qnil;
9026 *time = last_mouse_movement_time;
9028 UNBLOCK_INPUT;
9032 /* The screen has been cleared so we may have changed foreground or
9033 background colors, and the scroll bars may need to be redrawn.
9034 Clear out the scroll bars, and ask for expose events, so we can
9035 redraw them. */
9037 void
9038 x_scroll_bar_clear (f)
9039 FRAME_PTR f;
9041 #ifndef USE_TOOLKIT_SCROLL_BARS
9042 Lisp_Object bar;
9044 /* We can have scroll bars even if this is 0,
9045 if we just turned off scroll bar mode.
9046 But in that case we should not clear them. */
9047 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9048 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9049 bar = XSCROLL_BAR (bar)->next)
9050 XClearArea (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9051 0, 0, 0, 0, True);
9052 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9055 /* This processes Expose events from the menu-bar specific X event
9056 loop in xmenu.c. This allows to redisplay the frame if necessary
9057 when handling menu-bar or pop-up items. */
9060 process_expose_from_menu (event)
9061 XEvent event;
9063 FRAME_PTR f;
9064 struct x_display_info *dpyinfo;
9065 int frame_exposed_p = 0;
9067 BLOCK_INPUT;
9069 dpyinfo = x_display_info_for_display (event.xexpose.display);
9070 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9071 if (f)
9073 if (f->async_visible == 0)
9075 f->async_visible = 1;
9076 f->async_iconified = 0;
9077 f->output_data.x->has_been_visible = 1;
9078 SET_FRAME_GARBAGED (f);
9080 else
9082 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9083 event.xexpose.x, event.xexpose.y,
9084 event.xexpose.width, event.xexpose.height);
9085 frame_exposed_p = 1;
9088 else
9090 struct scroll_bar *bar
9091 = x_window_to_scroll_bar (event.xexpose.window);
9093 if (bar)
9094 x_scroll_bar_expose (bar, &event);
9097 UNBLOCK_INPUT;
9098 return frame_exposed_p;
9101 /* Define a queue to save up SelectionRequest events for later handling. */
9103 struct selection_event_queue
9105 XEvent event;
9106 struct selection_event_queue *next;
9109 static struct selection_event_queue *queue;
9111 /* Nonzero means queue up certain events--don't process them yet. */
9113 static int x_queue_selection_requests;
9115 /* Queue up an X event *EVENT, to be processed later. */
9117 static void
9118 x_queue_event (f, event)
9119 FRAME_PTR f;
9120 XEvent *event;
9122 struct selection_event_queue *queue_tmp
9123 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9125 if (queue_tmp != NULL)
9127 queue_tmp->event = *event;
9128 queue_tmp->next = queue;
9129 queue = queue_tmp;
9133 /* Take all the queued events and put them back
9134 so that they get processed afresh. */
9136 static void
9137 x_unqueue_events (display)
9138 Display *display;
9140 while (queue != NULL)
9142 struct selection_event_queue *queue_tmp = queue;
9143 XPutBackEvent (display, &queue_tmp->event);
9144 queue = queue_tmp->next;
9145 xfree ((char *)queue_tmp);
9149 /* Start queuing SelectionRequest events. */
9151 void
9152 x_start_queuing_selection_requests (display)
9153 Display *display;
9155 x_queue_selection_requests++;
9158 /* Stop queuing SelectionRequest events. */
9160 void
9161 x_stop_queuing_selection_requests (display)
9162 Display *display;
9164 x_queue_selection_requests--;
9165 x_unqueue_events (display);
9168 /* The main X event-reading loop - XTread_socket. */
9170 /* Time stamp of enter window event. This is only used by XTread_socket,
9171 but we have to put it out here, since static variables within functions
9172 sometimes don't work. */
9174 static Time enter_timestamp;
9176 /* This holds the state XLookupString needs to implement dead keys
9177 and other tricks known as "compose processing". _X Window System_
9178 says that a portable program can't use this, but Stephen Gildea assures
9179 me that letting the compiler initialize it to zeros will work okay.
9181 This must be defined outside of XTread_socket, for the same reasons
9182 given for enter_time stamp, above. */
9184 static XComposeStatus compose_status;
9186 /* Record the last 100 characters stored
9187 to help debug the loss-of-chars-during-GC problem. */
9189 static int temp_index;
9190 static short temp_buffer[100];
9192 /* Set this to nonzero to fake an "X I/O error"
9193 on a particular display. */
9195 struct x_display_info *XTread_socket_fake_io_error;
9197 /* When we find no input here, we occasionally do a no-op command
9198 to verify that the X server is still running and we can still talk with it.
9199 We try all the open displays, one by one.
9200 This variable is used for cycling thru the displays. */
9202 static struct x_display_info *next_noop_dpyinfo;
9204 #define SET_SAVED_MENU_EVENT(size) \
9205 do \
9207 if (f->output_data.x->saved_menu_event == 0) \
9208 f->output_data.x->saved_menu_event \
9209 = (XEvent *) xmalloc (sizeof (XEvent)); \
9210 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9211 if (numchars >= 1) \
9213 bufp->kind = menu_bar_activate_event; \
9214 XSETFRAME (bufp->frame_or_window, f); \
9215 bufp->arg = Qnil; \
9216 bufp++; \
9217 count++; \
9218 numchars--; \
9221 while (0)
9223 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9224 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9226 /* Read events coming from the X server.
9227 This routine is called by the SIGIO handler.
9228 We return as soon as there are no more events to be read.
9230 Events representing keys are stored in buffer BUFP,
9231 which can hold up to NUMCHARS characters.
9232 We return the number of characters stored into the buffer,
9233 thus pretending to be `read'.
9235 EXPECTED is nonzero if the caller knows input is available. */
9238 XTread_socket (sd, bufp, numchars, expected)
9239 register int sd;
9240 /* register */ struct input_event *bufp;
9241 /* register */ int numchars;
9242 int expected;
9244 int count = 0;
9245 int nbytes = 0;
9246 XEvent event;
9247 struct frame *f;
9248 int event_found = 0;
9249 struct x_display_info *dpyinfo;
9250 struct coding_system coding;
9252 if (interrupt_input_blocked)
9254 interrupt_input_pending = 1;
9255 return -1;
9258 interrupt_input_pending = 0;
9259 BLOCK_INPUT;
9261 /* So people can tell when we have read the available input. */
9262 input_signal_count++;
9264 if (numchars <= 0)
9265 abort (); /* Don't think this happens. */
9267 ++handling_signal;
9269 /* The input should be decoded if it is from XIM. Currently the
9270 locale of XIM is the same as that of the system. So, we can use
9271 Vlocale_coding_system which is initialized properly at Emacs
9272 startup time. */
9273 setup_coding_system (Vlocale_coding_system, &coding);
9274 coding.src_multibyte = 0;
9275 coding.dst_multibyte = 1;
9276 /* The input is converted to events, thus we can't handle
9277 composition. Anyway, there's no XIM that gives us composition
9278 information. */
9279 coding.composing = COMPOSITION_DISABLED;
9281 /* Find the display we are supposed to read input for.
9282 It's the one communicating on descriptor SD. */
9283 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9285 #if 0 /* This ought to be unnecessary; let's verify it. */
9286 #ifdef FIOSNBIO
9287 /* If available, Xlib uses FIOSNBIO to make the socket
9288 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9289 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9290 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9291 fcntl (dpyinfo->connection, F_SETFL, 0);
9292 #endif /* ! defined (FIOSNBIO) */
9293 #endif
9295 #if 0 /* This code can't be made to work, with multiple displays,
9296 and appears not to be used on any system any more.
9297 Also keyboard.c doesn't turn O_NDELAY on and off
9298 for X connections. */
9299 #ifndef SIGIO
9300 #ifndef HAVE_SELECT
9301 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9303 extern int read_alarm_should_throw;
9304 read_alarm_should_throw = 1;
9305 XPeekEvent (dpyinfo->display, &event);
9306 read_alarm_should_throw = 0;
9308 #endif /* HAVE_SELECT */
9309 #endif /* SIGIO */
9310 #endif
9312 /* For debugging, this gives a way to fake an I/O error. */
9313 if (dpyinfo == XTread_socket_fake_io_error)
9315 XTread_socket_fake_io_error = 0;
9316 x_io_error_quitter (dpyinfo->display);
9319 while (XPending (dpyinfo->display))
9321 XNextEvent (dpyinfo->display, &event);
9323 #ifdef HAVE_X_I18N
9325 /* Filter events for the current X input method.
9326 XFilterEvent returns non-zero if the input method has
9327 consumed the event. We pass the frame's X window to
9328 XFilterEvent because that's the one for which the IC
9329 was created. */
9330 struct frame *f1 = x_any_window_to_frame (dpyinfo,
9331 event.xclient.window);
9332 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
9333 break;
9335 #endif
9336 event_found = 1;
9338 switch (event.type)
9340 case ClientMessage:
9342 if (event.xclient.message_type
9343 == dpyinfo->Xatom_wm_protocols
9344 && event.xclient.format == 32)
9346 if (event.xclient.data.l[0]
9347 == dpyinfo->Xatom_wm_take_focus)
9349 /* Use x_any_window_to_frame because this
9350 could be the shell widget window
9351 if the frame has no title bar. */
9352 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
9353 #ifdef HAVE_X_I18N
9354 /* Not quite sure this is needed -pd */
9355 if (f && FRAME_XIC (f))
9356 XSetICFocus (FRAME_XIC (f));
9357 #endif
9358 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
9359 instructs the WM to set the input focus automatically for
9360 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
9361 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
9362 it has set the focus. So, XSetInputFocus below is not
9363 needed.
9365 The call to XSetInputFocus below has also caused trouble. In
9366 cases where the XSetInputFocus done by the WM and the one
9367 below are temporally close (on a fast machine), the call
9368 below can generate additional FocusIn events which confuse
9369 Emacs. */
9371 /* Since we set WM_TAKE_FOCUS, we must call
9372 XSetInputFocus explicitly. But not if f is null,
9373 since that might be an event for a deleted frame. */
9374 if (f)
9376 Display *d = event.xclient.display;
9377 /* Catch and ignore errors, in case window has been
9378 iconified by a window manager such as GWM. */
9379 int count = x_catch_errors (d);
9380 XSetInputFocus (d, event.xclient.window,
9381 /* The ICCCM says this is
9382 the only valid choice. */
9383 RevertToParent,
9384 event.xclient.data.l[1]);
9385 /* This is needed to detect the error
9386 if there is an error. */
9387 XSync (d, False);
9388 x_uncatch_errors (d, count);
9390 /* Not certain about handling scroll bars here */
9391 #endif /* 0 */
9393 else if (event.xclient.data.l[0]
9394 == dpyinfo->Xatom_wm_save_yourself)
9396 /* Save state modify the WM_COMMAND property to
9397 something which can reinstate us. This notifies
9398 the session manager, who's looking for such a
9399 PropertyNotify. Can restart processing when
9400 a keyboard or mouse event arrives. */
9401 if (numchars > 0)
9403 f = x_top_window_to_frame (dpyinfo,
9404 event.xclient.window);
9406 /* This is just so we only give real data once
9407 for a single Emacs process. */
9408 if (f == SELECTED_FRAME ())
9409 XSetCommand (FRAME_X_DISPLAY (f),
9410 event.xclient.window,
9411 initial_argv, initial_argc);
9412 else if (f)
9413 XSetCommand (FRAME_X_DISPLAY (f),
9414 event.xclient.window,
9415 0, 0);
9418 else if (event.xclient.data.l[0]
9419 == dpyinfo->Xatom_wm_delete_window)
9421 struct frame *f
9422 = x_any_window_to_frame (dpyinfo,
9423 event.xclient.window);
9425 if (f)
9427 if (numchars == 0)
9428 abort ();
9430 bufp->kind = delete_window_event;
9431 XSETFRAME (bufp->frame_or_window, f);
9432 bufp->arg = Qnil;
9433 bufp++;
9435 count += 1;
9436 numchars -= 1;
9440 else if (event.xclient.message_type
9441 == dpyinfo->Xatom_wm_configure_denied)
9444 else if (event.xclient.message_type
9445 == dpyinfo->Xatom_wm_window_moved)
9447 int new_x, new_y;
9448 struct frame *f
9449 = x_window_to_frame (dpyinfo, event.xclient.window);
9451 new_x = event.xclient.data.s[0];
9452 new_y = event.xclient.data.s[1];
9454 if (f)
9456 f->output_data.x->left_pos = new_x;
9457 f->output_data.x->top_pos = new_y;
9460 #ifdef HACK_EDITRES
9461 else if (event.xclient.message_type
9462 == dpyinfo->Xatom_editres)
9464 struct frame *f
9465 = x_any_window_to_frame (dpyinfo, event.xclient.window);
9466 _XEditResCheckMessages (f->output_data.x->widget, NULL,
9467 &event, NULL);
9469 #endif /* HACK_EDITRES */
9470 else if ((event.xclient.message_type
9471 == dpyinfo->Xatom_DONE)
9472 || (event.xclient.message_type
9473 == dpyinfo->Xatom_PAGE))
9475 /* Ghostview job completed. Kill it. We could
9476 reply with "Next" if we received "Page", but we
9477 currently never do because we are interested in
9478 images, only, which should have 1 page. */
9479 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
9480 struct frame *f
9481 = x_window_to_frame (dpyinfo, event.xclient.window);
9482 x_kill_gs_process (pixmap, f);
9483 expose_frame (f, 0, 0, 0, 0);
9485 #ifdef USE_TOOLKIT_SCROLL_BARS
9486 /* Scroll bar callbacks send a ClientMessage from which
9487 we construct an input_event. */
9488 else if (event.xclient.message_type
9489 == dpyinfo->Xatom_Scrollbar)
9491 x_scroll_bar_to_input_event (&event, bufp);
9492 ++bufp, ++count, --numchars;
9493 goto out;
9495 #endif /* USE_TOOLKIT_SCROLL_BARS */
9496 else
9497 goto OTHER;
9499 break;
9501 case SelectionNotify:
9502 #ifdef USE_X_TOOLKIT
9503 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
9504 goto OTHER;
9505 #endif /* not USE_X_TOOLKIT */
9506 x_handle_selection_notify (&event.xselection);
9507 break;
9509 case SelectionClear: /* Someone has grabbed ownership. */
9510 #ifdef USE_X_TOOLKIT
9511 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
9512 goto OTHER;
9513 #endif /* USE_X_TOOLKIT */
9515 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
9517 if (numchars == 0)
9518 abort ();
9520 bufp->kind = selection_clear_event;
9521 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9522 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9523 SELECTION_EVENT_TIME (bufp) = eventp->time;
9524 bufp->frame_or_window = Qnil;
9525 bufp->arg = Qnil;
9526 bufp++;
9528 count += 1;
9529 numchars -= 1;
9531 break;
9533 case SelectionRequest: /* Someone wants our selection. */
9534 #ifdef USE_X_TOOLKIT
9535 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
9536 goto OTHER;
9537 #endif /* USE_X_TOOLKIT */
9538 if (x_queue_selection_requests)
9539 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
9540 &event);
9541 else
9543 XSelectionRequestEvent *eventp = (XSelectionRequestEvent *) &event;
9545 if (numchars == 0)
9546 abort ();
9548 bufp->kind = selection_request_event;
9549 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
9550 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
9551 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
9552 SELECTION_EVENT_TARGET (bufp) = eventp->target;
9553 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
9554 SELECTION_EVENT_TIME (bufp) = eventp->time;
9555 bufp->frame_or_window = Qnil;
9556 bufp->arg = Qnil;
9557 bufp++;
9559 count += 1;
9560 numchars -= 1;
9562 break;
9564 case PropertyNotify:
9565 #ifdef USE_X_TOOLKIT
9566 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
9567 goto OTHER;
9568 #endif /* not USE_X_TOOLKIT */
9569 x_handle_property_notify (&event.xproperty);
9570 break;
9572 case ReparentNotify:
9573 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
9574 if (f)
9576 int x, y;
9577 f->output_data.x->parent_desc = event.xreparent.parent;
9578 x_real_positions (f, &x, &y);
9579 f->output_data.x->left_pos = x;
9580 f->output_data.x->top_pos = y;
9582 break;
9584 case Expose:
9585 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9586 if (f)
9588 if (f->async_visible == 0)
9590 f->async_visible = 1;
9591 f->async_iconified = 0;
9592 f->output_data.x->has_been_visible = 1;
9593 SET_FRAME_GARBAGED (f);
9595 else
9596 expose_frame (x_window_to_frame (dpyinfo,
9597 event.xexpose.window),
9598 event.xexpose.x, event.xexpose.y,
9599 event.xexpose.width, event.xexpose.height);
9601 else
9603 #ifdef USE_TOOLKIT_SCROLL_BARS
9604 /* Dispatch event to the widget. */
9605 goto OTHER;
9606 #else /* not USE_TOOLKIT_SCROLL_BARS */
9607 struct scroll_bar *bar
9608 = x_window_to_scroll_bar (event.xexpose.window);
9610 if (bar)
9611 x_scroll_bar_expose (bar, &event);
9612 #ifdef USE_X_TOOLKIT
9613 else
9614 goto OTHER;
9615 #endif /* USE_X_TOOLKIT */
9616 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9618 break;
9620 case GraphicsExpose: /* This occurs when an XCopyArea's
9621 source area was obscured or not
9622 available.*/
9623 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
9624 if (f)
9626 expose_frame (f,
9627 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
9628 event.xgraphicsexpose.width,
9629 event.xgraphicsexpose.height);
9631 #ifdef USE_X_TOOLKIT
9632 else
9633 goto OTHER;
9634 #endif /* USE_X_TOOLKIT */
9635 break;
9637 case NoExpose: /* This occurs when an XCopyArea's
9638 source area was completely
9639 available */
9640 break;
9642 case UnmapNotify:
9643 /* Redo the mouse-highlight after the tooltip has gone. */
9644 if (event.xmap.window == tip_window)
9646 tip_window = 0;
9647 redo_mouse_highlight ();
9650 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
9651 if (f) /* F may no longer exist if
9652 the frame was deleted. */
9654 /* While a frame is unmapped, display generation is
9655 disabled; you don't want to spend time updating a
9656 display that won't ever be seen. */
9657 f->async_visible = 0;
9658 /* We can't distinguish, from the event, whether the window
9659 has become iconified or invisible. So assume, if it
9660 was previously visible, than now it is iconified.
9661 But x_make_frame_invisible clears both
9662 the visible flag and the iconified flag;
9663 and that way, we know the window is not iconified now. */
9664 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
9666 f->async_iconified = 1;
9668 bufp->kind = iconify_event;
9669 XSETFRAME (bufp->frame_or_window, f);
9670 bufp->arg = Qnil;
9671 bufp++;
9672 count++;
9673 numchars--;
9676 goto OTHER;
9678 case MapNotify:
9679 if (event.xmap.window == tip_window)
9680 /* The tooltip has been drawn already. Avoid
9681 the SET_FRAME_GARBAGED below. */
9682 goto OTHER;
9684 /* We use x_top_window_to_frame because map events can
9685 come for sub-windows and they don't mean that the
9686 frame is visible. */
9687 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
9688 if (f)
9690 f->async_visible = 1;
9691 f->async_iconified = 0;
9692 f->output_data.x->has_been_visible = 1;
9694 /* wait_reading_process_input will notice this and update
9695 the frame's display structures. */
9696 SET_FRAME_GARBAGED (f);
9698 if (f->iconified)
9700 bufp->kind = deiconify_event;
9701 XSETFRAME (bufp->frame_or_window, f);
9702 bufp->arg = Qnil;
9703 bufp++;
9704 count++;
9705 numchars--;
9707 else if (! NILP (Vframe_list)
9708 && ! NILP (XCDR (Vframe_list)))
9709 /* Force a redisplay sooner or later
9710 to update the frame titles
9711 in case this is the second frame. */
9712 record_asynch_buffer_change ();
9714 goto OTHER;
9716 case KeyPress:
9717 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
9719 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
9720 /* I couldn't find a way to prevent LessTif scroll bars
9721 from consuming key events. */
9722 if (f == 0)
9724 Widget widget = XtWindowToWidget (dpyinfo->display,
9725 event.xkey.window);
9726 if (widget && XmIsScrollBar (widget))
9728 widget = XtParent (widget);
9729 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
9732 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
9734 if (f != 0)
9736 KeySym keysym, orig_keysym;
9737 /* al%imercury@uunet.uu.net says that making this 81
9738 instead of 80 fixed a bug whereby meta chars made
9739 his Emacs hang.
9741 It seems that some version of XmbLookupString has
9742 a bug of not returning XBufferOverflow in
9743 status_return even if the input is too long to
9744 fit in 81 bytes. So, we must prepare sufficient
9745 bytes for copy_buffer. 513 bytes (256 chars for
9746 two-byte character set) seems to be a faily good
9747 approximation. -- 2000.8.10 handa@etl.go.jp */
9748 unsigned char copy_buffer[513];
9749 unsigned char *copy_bufptr = copy_buffer;
9750 int copy_bufsiz = sizeof (copy_buffer);
9751 int modifiers;
9753 event.xkey.state
9754 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
9755 extra_keyboard_modifiers);
9756 modifiers = event.xkey.state;
9758 /* This will have to go some day... */
9760 /* make_lispy_event turns chars into control chars.
9761 Don't do it here because XLookupString is too eager. */
9762 event.xkey.state &= ~ControlMask;
9763 event.xkey.state &= ~(dpyinfo->meta_mod_mask
9764 | dpyinfo->super_mod_mask
9765 | dpyinfo->hyper_mod_mask
9766 | dpyinfo->alt_mod_mask);
9768 /* In case Meta is ComposeCharacter,
9769 clear its status. According to Markus Ehrnsperger
9770 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
9771 this enables ComposeCharacter to work whether or
9772 not it is combined with Meta. */
9773 if (modifiers & dpyinfo->meta_mod_mask)
9774 bzero (&compose_status, sizeof (compose_status));
9776 #ifdef HAVE_X_I18N
9777 if (FRAME_XIC (f))
9779 Status status_return;
9781 nbytes = XmbLookupString (FRAME_XIC (f),
9782 &event.xkey, copy_bufptr,
9783 copy_bufsiz, &keysym,
9784 &status_return);
9785 if (status_return == XBufferOverflow)
9787 copy_bufsiz = nbytes + 1;
9788 copy_bufptr = (char *) alloca (copy_bufsiz);
9789 nbytes = XmbLookupString (FRAME_XIC (f),
9790 &event.xkey, copy_bufptr,
9791 copy_bufsiz, &keysym,
9792 &status_return);
9795 if (status_return == XLookupNone)
9796 break;
9797 else if (status_return == XLookupChars)
9799 keysym = NoSymbol;
9800 modifiers = 0;
9802 else if (status_return != XLookupKeySym
9803 && status_return != XLookupBoth)
9804 abort ();
9806 else
9807 nbytes = XLookupString (&event.xkey, copy_bufptr,
9808 copy_bufsiz, &keysym,
9809 &compose_status);
9810 #else
9811 nbytes = XLookupString (&event.xkey, copy_bufptr,
9812 copy_bufsiz, &keysym,
9813 &compose_status);
9814 #endif
9816 orig_keysym = keysym;
9818 if (numchars > 1)
9820 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
9821 || keysym == XK_Delete
9822 #ifdef XK_ISO_Left_Tab
9823 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
9824 #endif
9825 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
9826 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
9827 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
9828 #ifdef HPUX
9829 /* This recognizes the "extended function keys".
9830 It seems there's no cleaner way.
9831 Test IsModifierKey to avoid handling mode_switch
9832 incorrectly. */
9833 || ((unsigned) (keysym) >= XK_Select
9834 && (unsigned)(keysym) < XK_KP_Space)
9835 #endif
9836 #ifdef XK_dead_circumflex
9837 || orig_keysym == XK_dead_circumflex
9838 #endif
9839 #ifdef XK_dead_grave
9840 || orig_keysym == XK_dead_grave
9841 #endif
9842 #ifdef XK_dead_tilde
9843 || orig_keysym == XK_dead_tilde
9844 #endif
9845 #ifdef XK_dead_diaeresis
9846 || orig_keysym == XK_dead_diaeresis
9847 #endif
9848 #ifdef XK_dead_macron
9849 || orig_keysym == XK_dead_macron
9850 #endif
9851 #ifdef XK_dead_degree
9852 || orig_keysym == XK_dead_degree
9853 #endif
9854 #ifdef XK_dead_acute
9855 || orig_keysym == XK_dead_acute
9856 #endif
9857 #ifdef XK_dead_cedilla
9858 || orig_keysym == XK_dead_cedilla
9859 #endif
9860 #ifdef XK_dead_breve
9861 || orig_keysym == XK_dead_breve
9862 #endif
9863 #ifdef XK_dead_ogonek
9864 || orig_keysym == XK_dead_ogonek
9865 #endif
9866 #ifdef XK_dead_caron
9867 || orig_keysym == XK_dead_caron
9868 #endif
9869 #ifdef XK_dead_doubleacute
9870 || orig_keysym == XK_dead_doubleacute
9871 #endif
9872 #ifdef XK_dead_abovedot
9873 || orig_keysym == XK_dead_abovedot
9874 #endif
9875 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
9876 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
9877 /* Any "vendor-specific" key is ok. */
9878 || (orig_keysym & (1 << 28)))
9879 && ! (IsModifierKey (orig_keysym)
9880 #ifndef HAVE_X11R5
9881 #ifdef XK_Mode_switch
9882 || ((unsigned)(orig_keysym) == XK_Mode_switch)
9883 #endif
9884 #ifdef XK_Num_Lock
9885 || ((unsigned)(orig_keysym) == XK_Num_Lock)
9886 #endif
9887 #endif /* not HAVE_X11R5 */
9890 if (temp_index == sizeof temp_buffer / sizeof (short))
9891 temp_index = 0;
9892 temp_buffer[temp_index++] = keysym;
9893 bufp->kind = non_ascii_keystroke;
9894 bufp->code = keysym;
9895 XSETFRAME (bufp->frame_or_window, f);
9896 bufp->arg = Qnil;
9897 bufp->modifiers
9898 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9899 modifiers);
9900 bufp->timestamp = event.xkey.time;
9901 bufp++;
9902 count++;
9903 numchars--;
9905 else if (numchars > nbytes)
9907 register int i;
9908 register int c;
9909 unsigned char *p, *pend;
9910 int nchars, len;
9912 for (i = 0; i < nbytes; i++)
9914 if (temp_index == (sizeof temp_buffer
9915 / sizeof (short)))
9916 temp_index = 0;
9917 temp_buffer[temp_index++] = copy_bufptr[i];
9920 if (/* If the event is not from XIM, */
9921 event.xkey.keycode != 0
9922 /* or the current locale doesn't request
9923 decoding of the intup data, ... */
9924 || coding.type == coding_type_raw_text
9925 || coding.type == coding_type_no_conversion)
9927 /* ... we can use the input data as is. */
9928 nchars = nbytes;
9930 else
9932 /* We have to decode the input data. */
9933 int require;
9934 unsigned char *p;
9936 require = decoding_buffer_size (&coding, nbytes);
9937 p = (unsigned char *) alloca (require);
9938 coding.mode |= CODING_MODE_LAST_BLOCK;
9939 decode_coding (&coding, copy_bufptr, p,
9940 nbytes, require);
9941 nbytes = coding.produced;
9942 nchars = coding.produced_char;
9943 copy_bufptr = p;
9946 /* Convert the input data to a sequence of
9947 character events. */
9948 for (i = 0; i < nbytes; i += len)
9950 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
9951 nbytes - i, len);
9952 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
9953 ? ascii_keystroke
9954 : multibyte_char_keystroke);
9955 bufp->code = c;
9956 XSETFRAME (bufp->frame_or_window, f);
9957 bufp->arg = Qnil;
9958 bufp->modifiers
9959 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
9960 modifiers);
9961 bufp->timestamp = event.xkey.time;
9962 bufp++;
9965 count += nchars;
9966 numchars -= nchars;
9968 if (keysym == NoSymbol)
9969 break;
9971 else
9972 abort ();
9974 else
9975 abort ();
9977 #ifdef HAVE_X_I18N
9978 /* Don't dispatch this event since XtDispatchEvent calls
9979 XFilterEvent, and two calls in a row may freeze the
9980 client. */
9981 break;
9982 #else
9983 goto OTHER;
9984 #endif
9986 case KeyRelease:
9987 #ifdef HAVE_X_I18N
9988 /* Don't dispatch this event since XtDispatchEvent calls
9989 XFilterEvent, and two calls in a row may freeze the
9990 client. */
9991 break;
9992 #else
9993 goto OTHER;
9994 #endif
9996 /* Here's a possible interpretation of the whole
9997 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
9998 you get a FocusIn event, you have to get a FocusOut
9999 event before you relinquish the focus. If you
10000 haven't received a FocusIn event, then a mere
10001 LeaveNotify is enough to free you. */
10003 case EnterNotify:
10005 int from_menu_bar_p = 0;
10007 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10009 #ifdef LESSTIF_VERSION
10010 /* When clicking outside of a menu bar popup to close
10011 it, we get a FocusIn/ EnterNotify sequence of
10012 events. The flag event.xcrossing.focus is not set
10013 in the EnterNotify event of that sequence because
10014 the focus is in the menu bar,
10015 event.xcrossing.window is the frame's X window.
10016 Unconditionally setting the focus frame to null in
10017 this case is not the right thing, because no event
10018 follows that could set the focus frame to the right
10019 value.
10021 This could be a LessTif bug, but I wasn't able to
10022 reproduce the behavior in a simple test program.
10024 (gerd, LessTif 0.88.1). */
10026 if (!event.xcrossing.focus
10027 && f
10028 && f->output_data.x->menubar_widget)
10030 Window focus;
10031 int revert;
10033 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10034 if (focus == XtWindow (f->output_data.x->menubar_widget))
10035 from_menu_bar_p = 1;
10037 #endif /* LESSTIF_VERSION */
10039 if (event.xcrossing.focus || from_menu_bar_p)
10041 /* Avoid nasty pop/raise loops. */
10042 if (f && (!(f->auto_raise)
10043 || !(f->auto_lower)
10044 || (event.xcrossing.time - enter_timestamp) > 500))
10046 x_new_focus_frame (dpyinfo, f);
10047 enter_timestamp = event.xcrossing.time;
10050 else if (f == dpyinfo->x_focus_frame)
10051 x_new_focus_frame (dpyinfo, 0);
10053 /* EnterNotify counts as mouse movement,
10054 so update things that depend on mouse position. */
10055 if (f && !f->output_data.x->busy_p)
10056 note_mouse_movement (f, &event.xmotion);
10057 goto OTHER;
10060 case FocusIn:
10061 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10062 if (event.xfocus.detail != NotifyPointer)
10063 dpyinfo->x_focus_event_frame = f;
10064 if (f)
10066 x_new_focus_frame (dpyinfo, f);
10068 /* Don't stop displaying the initial startup message
10069 for a switch-frame event we don't need. */
10070 if (GC_NILP (Vterminal_frame)
10071 && GC_CONSP (Vframe_list)
10072 && !GC_NILP (XCDR (Vframe_list)))
10074 bufp->kind = FOCUS_IN_EVENT;
10075 XSETFRAME (bufp->frame_or_window, f);
10076 bufp->arg = Qnil;
10077 ++bufp, ++count, --numchars;
10081 #ifdef HAVE_X_I18N
10082 if (f && FRAME_XIC (f))
10083 XSetICFocus (FRAME_XIC (f));
10084 #endif
10086 goto OTHER;
10088 case LeaveNotify:
10089 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10090 if (f)
10092 Lisp_Object frame;
10093 int from_menu_bar_p = 0;
10095 if (f == dpyinfo->mouse_face_mouse_frame)
10097 /* If we move outside the frame, then we're
10098 certainly no longer on any text in the frame. */
10099 clear_mouse_face (dpyinfo);
10100 dpyinfo->mouse_face_mouse_frame = 0;
10103 /* Generate a nil HELP_EVENT to cancel a help-echo.
10104 Do it only if there's something to cancel.
10105 Otherwise, the startup message is cleared when
10106 the mouse leaves the frame. */
10107 if (any_help_event_p)
10109 Lisp_Object frame;
10110 int n;
10112 XSETFRAME (frame, f);
10113 n = gen_help_event (bufp, numchars,
10114 Qnil, frame, Qnil, Qnil, 0);
10115 bufp += n, count += n, numchars -= n;
10118 #ifdef LESSTIF_VERSION
10119 /* Please see the comment at the start of the
10120 EnterNotify case. */
10121 if (!event.xcrossing.focus
10122 && f->output_data.x->menubar_widget)
10124 Window focus;
10125 int revert;
10126 XGetInputFocus (FRAME_X_DISPLAY (f), &focus, &revert);
10127 if (focus == XtWindow (f->output_data.x->menubar_widget))
10128 from_menu_bar_p = 1;
10130 #endif /* LESSTIF_VERSION */
10132 if (event.xcrossing.focus || from_menu_bar_p)
10133 x_mouse_leave (dpyinfo);
10134 else
10136 if (f == dpyinfo->x_focus_event_frame)
10137 dpyinfo->x_focus_event_frame = 0;
10138 if (f == dpyinfo->x_focus_frame)
10139 x_new_focus_frame (dpyinfo, 0);
10142 goto OTHER;
10144 case FocusOut:
10145 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10146 if (event.xfocus.detail != NotifyPointer
10147 && f == dpyinfo->x_focus_event_frame)
10148 dpyinfo->x_focus_event_frame = 0;
10149 if (f && f == dpyinfo->x_focus_frame)
10150 x_new_focus_frame (dpyinfo, 0);
10152 #ifdef HAVE_X_I18N
10153 if (f && FRAME_XIC (f))
10154 XUnsetICFocus (FRAME_XIC (f));
10155 #endif
10157 goto OTHER;
10159 case MotionNotify:
10161 previous_help_echo = help_echo;
10162 help_echo = help_echo_object = help_echo_window = Qnil;
10163 help_echo_pos = -1;
10165 if (dpyinfo->grabbed && last_mouse_frame
10166 && FRAME_LIVE_P (last_mouse_frame))
10167 f = last_mouse_frame;
10168 else
10169 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10171 if (f)
10172 note_mouse_movement (f, &event.xmotion);
10173 else
10175 #ifndef USE_TOOLKIT_SCROLL_BARS
10176 struct scroll_bar *bar
10177 = x_window_to_scroll_bar (event.xmotion.window);
10179 if (bar)
10180 x_scroll_bar_note_movement (bar, &event);
10181 #endif /* USE_TOOLKIT_SCROLL_BARS */
10183 /* If we move outside the frame, then we're
10184 certainly no longer on any text in the frame. */
10185 clear_mouse_face (dpyinfo);
10188 /* If the contents of the global variable help_echo
10189 has changed, generate a HELP_EVENT. */
10190 if (!NILP (help_echo)
10191 || !NILP (previous_help_echo))
10193 Lisp_Object frame;
10194 int n;
10196 if (f)
10197 XSETFRAME (frame, f);
10198 else
10199 frame = Qnil;
10201 any_help_event_p = 1;
10202 n = gen_help_event (bufp, numchars, help_echo, frame,
10203 help_echo_window, help_echo_object,
10204 help_echo_pos);
10205 bufp += n, count += n, numchars -= n;
10208 goto OTHER;
10211 case ConfigureNotify:
10212 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10213 if (f)
10215 #ifndef USE_X_TOOLKIT
10216 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10217 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10219 /* In the toolkit version, change_frame_size
10220 is called by the code that handles resizing
10221 of the EmacsFrame widget. */
10223 /* Even if the number of character rows and columns has
10224 not changed, the font size may have changed, so we need
10225 to check the pixel dimensions as well. */
10226 if (columns != f->width
10227 || rows != f->height
10228 || event.xconfigure.width != f->output_data.x->pixel_width
10229 || event.xconfigure.height != f->output_data.x->pixel_height)
10231 change_frame_size (f, rows, columns, 0, 1, 0);
10232 SET_FRAME_GARBAGED (f);
10233 cancel_mouse_face (f);
10235 #endif
10237 f->output_data.x->pixel_width = event.xconfigure.width;
10238 f->output_data.x->pixel_height = event.xconfigure.height;
10240 /* What we have now is the position of Emacs's own window.
10241 Convert that to the position of the window manager window. */
10242 x_real_positions (f, &f->output_data.x->left_pos,
10243 &f->output_data.x->top_pos);
10245 #ifdef HAVE_X_I18N
10246 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10247 xic_set_statusarea (f);
10248 #endif
10250 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10252 /* Since the WM decorations come below top_pos now,
10253 we must put them below top_pos in the future. */
10254 f->output_data.x->win_gravity = NorthWestGravity;
10255 x_wm_set_size_hint (f, (long) 0, 0);
10257 #ifdef USE_MOTIF
10258 /* Some window managers pass (0,0) as the location of
10259 the window, and the Motif event handler stores it
10260 in the emacs widget, which messes up Motif menus. */
10261 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
10263 event.xconfigure.x = f->output_data.x->widget->core.x;
10264 event.xconfigure.y = f->output_data.x->widget->core.y;
10266 #endif /* USE_MOTIF */
10268 goto OTHER;
10270 case ButtonPress:
10271 case ButtonRelease:
10273 /* If we decide we want to generate an event to be seen
10274 by the rest of Emacs, we put it here. */
10275 struct input_event emacs_event;
10276 int tool_bar_p = 0;
10278 emacs_event.kind = no_event;
10279 bzero (&compose_status, sizeof (compose_status));
10281 if (dpyinfo->grabbed
10282 && last_mouse_frame
10283 && FRAME_LIVE_P (last_mouse_frame))
10284 f = last_mouse_frame;
10285 else
10286 f = x_window_to_frame (dpyinfo, event.xbutton.window);
10288 if (f)
10290 /* Is this in the tool-bar? */
10291 if (WINDOWP (f->tool_bar_window)
10292 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
10294 Lisp_Object window;
10295 int p, x, y;
10297 x = event.xbutton.x;
10298 y = event.xbutton.y;
10300 /* Set x and y. */
10301 window = window_from_coordinates (f, x, y, &p, 1);
10302 if (EQ (window, f->tool_bar_window))
10304 x_handle_tool_bar_click (f, &event.xbutton);
10305 tool_bar_p = 1;
10309 if (!tool_bar_p)
10310 if (!dpyinfo->x_focus_frame
10311 || f == dpyinfo->x_focus_frame)
10312 construct_mouse_click (&emacs_event, &event, f);
10314 else
10316 #ifndef USE_TOOLKIT_SCROLL_BARS
10317 struct scroll_bar *bar
10318 = x_window_to_scroll_bar (event.xbutton.window);
10320 if (bar)
10321 x_scroll_bar_handle_click (bar, &event, &emacs_event);
10322 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10325 if (event.type == ButtonPress)
10327 dpyinfo->grabbed |= (1 << event.xbutton.button);
10328 last_mouse_frame = f;
10329 /* Ignore any mouse motion that happened
10330 before this event; any subsequent mouse-movement
10331 Emacs events should reflect only motion after
10332 the ButtonPress. */
10333 if (f != 0)
10334 f->mouse_moved = 0;
10336 if (!tool_bar_p)
10337 last_tool_bar_item = -1;
10339 else
10341 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
10344 if (numchars >= 1 && emacs_event.kind != no_event)
10346 bcopy (&emacs_event, bufp, sizeof (struct input_event));
10347 bufp++;
10348 count++;
10349 numchars--;
10352 #ifdef USE_X_TOOLKIT
10353 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
10354 /* For a down-event in the menu bar,
10355 don't pass it to Xt right now.
10356 Instead, save it away
10357 and we will pass it to Xt from kbd_buffer_get_event.
10358 That way, we can run some Lisp code first. */
10359 if (f && event.type == ButtonPress
10360 /* Verify the event is really within the menu bar
10361 and not just sent to it due to grabbing. */
10362 && event.xbutton.x >= 0
10363 && event.xbutton.x < f->output_data.x->pixel_width
10364 && event.xbutton.y >= 0
10365 && event.xbutton.y < f->output_data.x->menubar_height
10366 && event.xbutton.same_screen)
10368 SET_SAVED_BUTTON_EVENT;
10369 XSETFRAME (last_mouse_press_frame, f);
10371 else if (event.type == ButtonPress)
10373 last_mouse_press_frame = Qnil;
10374 goto OTHER;
10377 #ifdef USE_MOTIF /* This should do not harm for Lucid,
10378 but I am trying to be cautious. */
10379 else if (event.type == ButtonRelease)
10381 if (!NILP (last_mouse_press_frame))
10383 f = XFRAME (last_mouse_press_frame);
10384 if (f->output_data.x)
10385 SET_SAVED_BUTTON_EVENT;
10387 else
10388 goto OTHER;
10390 #endif /* USE_MOTIF */
10391 else
10392 goto OTHER;
10393 #endif /* USE_X_TOOLKIT */
10395 break;
10397 case CirculateNotify:
10398 goto OTHER;
10400 case CirculateRequest:
10401 goto OTHER;
10403 case VisibilityNotify:
10404 goto OTHER;
10406 case MappingNotify:
10407 /* Someone has changed the keyboard mapping - update the
10408 local cache. */
10409 switch (event.xmapping.request)
10411 case MappingModifier:
10412 x_find_modifier_meanings (dpyinfo);
10413 /* This is meant to fall through. */
10414 case MappingKeyboard:
10415 XRefreshKeyboardMapping (&event.xmapping);
10417 goto OTHER;
10419 default:
10420 OTHER:
10421 #ifdef USE_X_TOOLKIT
10422 BLOCK_INPUT;
10423 XtDispatchEvent (&event);
10424 UNBLOCK_INPUT;
10425 #endif /* USE_X_TOOLKIT */
10426 break;
10431 out:;
10433 /* On some systems, an X bug causes Emacs to get no more events
10434 when the window is destroyed. Detect that. (1994.) */
10435 if (! event_found)
10437 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
10438 One XNOOP in 100 loops will make Emacs terminate.
10439 B. Bretthauer, 1994 */
10440 x_noop_count++;
10441 if (x_noop_count >= 100)
10443 x_noop_count=0;
10445 if (next_noop_dpyinfo == 0)
10446 next_noop_dpyinfo = x_display_list;
10448 XNoOp (next_noop_dpyinfo->display);
10450 /* Each time we get here, cycle through the displays now open. */
10451 next_noop_dpyinfo = next_noop_dpyinfo->next;
10455 /* If the focus was just given to an auto-raising frame,
10456 raise it now. */
10457 /* ??? This ought to be able to handle more than one such frame. */
10458 if (pending_autoraise_frame)
10460 x_raise_frame (pending_autoraise_frame);
10461 pending_autoraise_frame = 0;
10464 UNBLOCK_INPUT;
10465 --handling_signal;
10466 return count;
10472 /***********************************************************************
10473 Text Cursor
10474 ***********************************************************************/
10476 /* Note if the text cursor of window W has been overwritten by a
10477 drawing operation that outputs N glyphs starting at HPOS in the
10478 line given by output_cursor.vpos. N < 0 means all the rest of the
10479 line after HPOS has been written. */
10481 static void
10482 note_overwritten_text_cursor (w, hpos, n)
10483 struct window *w;
10484 int hpos, n;
10486 if (updated_area == TEXT_AREA
10487 && output_cursor.vpos == w->phys_cursor.vpos
10488 && hpos <= w->phys_cursor.hpos
10489 && (n < 0
10490 || hpos + n > w->phys_cursor.hpos))
10491 w->phys_cursor_on_p = 0;
10495 /* Set clipping for output in glyph row ROW. W is the window in which
10496 we operate. GC is the graphics context to set clipping in.
10497 WHOLE_LINE_P non-zero means include the areas used for truncation
10498 mark display and alike in the clipping rectangle.
10500 ROW may be a text row or, e.g., a mode line. Text rows must be
10501 clipped to the interior of the window dedicated to text display,
10502 mode lines must be clipped to the whole window. */
10504 static void
10505 x_clip_to_row (w, row, gc, whole_line_p)
10506 struct window *w;
10507 struct glyph_row *row;
10508 GC gc;
10509 int whole_line_p;
10511 struct frame *f = XFRAME (WINDOW_FRAME (w));
10512 XRectangle clip_rect;
10513 int window_x, window_y, window_width, window_height;
10515 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
10517 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
10518 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
10519 clip_rect.y = max (clip_rect.y, window_y);
10520 clip_rect.width = window_width;
10521 clip_rect.height = row->visible_height;
10523 /* If clipping to the whole line, including trunc marks, extend
10524 the rectangle to the left and increase its width. */
10525 if (whole_line_p)
10527 clip_rect.x -= FRAME_X_LEFT_FLAGS_AREA_WIDTH (f);
10528 clip_rect.width += FRAME_X_FLAGS_AREA_WIDTH (f);
10531 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
10535 /* Draw a hollow box cursor on window W in glyph row ROW. */
10537 static void
10538 x_draw_hollow_cursor (w, row)
10539 struct window *w;
10540 struct glyph_row *row;
10542 struct frame *f = XFRAME (WINDOW_FRAME (w));
10543 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10544 Display *dpy = FRAME_X_DISPLAY (f);
10545 int x, y, wd, h;
10546 XGCValues xgcv;
10547 struct glyph *cursor_glyph;
10548 GC gc;
10550 /* Compute frame-relative coordinates from window-relative
10551 coordinates. */
10552 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10553 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
10554 + row->ascent - w->phys_cursor_ascent);
10555 h = row->height - 1;
10557 /* Get the glyph the cursor is on. If we can't tell because
10558 the current matrix is invalid or such, give up. */
10559 cursor_glyph = get_phys_cursor_glyph (w);
10560 if (cursor_glyph == NULL)
10561 return;
10563 /* Compute the width of the rectangle to draw. If on a stretch
10564 glyph, and `x-stretch-block-cursor' is nil, don't draw a
10565 rectangle as wide as the glyph, but use a canonical character
10566 width instead. */
10567 wd = cursor_glyph->pixel_width - 1;
10568 if (cursor_glyph->type == STRETCH_GLYPH
10569 && !x_stretch_cursor_p)
10570 wd = min (CANON_X_UNIT (f), wd);
10572 /* The foreground of cursor_gc is typically the same as the normal
10573 background color, which can cause the cursor box to be invisible. */
10574 xgcv.foreground = f->output_data.x->cursor_pixel;
10575 if (dpyinfo->scratch_cursor_gc)
10576 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
10577 else
10578 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
10579 GCForeground, &xgcv);
10580 gc = dpyinfo->scratch_cursor_gc;
10582 /* Set clipping, draw the rectangle, and reset clipping again. */
10583 x_clip_to_row (w, row, gc, 0);
10584 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
10585 XSetClipMask (dpy, gc, None);
10589 /* Draw a bar cursor on window W in glyph row ROW.
10591 Implementation note: One would like to draw a bar cursor with an
10592 angle equal to the one given by the font property XA_ITALIC_ANGLE.
10593 Unfortunately, I didn't find a font yet that has this property set.
10594 --gerd. */
10596 static void
10597 x_draw_bar_cursor (w, row, width)
10598 struct window *w;
10599 struct glyph_row *row;
10600 int width;
10602 struct frame *f = XFRAME (w->frame);
10603 struct glyph *cursor_glyph;
10604 GC gc;
10605 int x;
10606 unsigned long mask;
10607 XGCValues xgcv;
10608 Display *dpy;
10609 Window window;
10611 /* If cursor is out of bounds, don't draw garbage. This can happen
10612 in mini-buffer windows when switching between echo area glyphs
10613 and mini-buffer. */
10614 cursor_glyph = get_phys_cursor_glyph (w);
10615 if (cursor_glyph == NULL)
10616 return;
10618 /* If on an image, draw like a normal cursor. That's usually better
10619 visible than drawing a bar, esp. if the image is large so that
10620 the bar might not be in the window. */
10621 if (cursor_glyph->type == IMAGE_GLYPH)
10623 struct glyph_row *row;
10624 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
10625 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
10627 else
10629 xgcv.background = f->output_data.x->cursor_pixel;
10630 xgcv.foreground = f->output_data.x->cursor_pixel;
10631 xgcv.graphics_exposures = 0;
10632 mask = GCForeground | GCBackground | GCGraphicsExposures;
10633 dpy = FRAME_X_DISPLAY (f);
10634 window = FRAME_X_WINDOW (f);
10635 gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
10637 if (gc)
10638 XChangeGC (dpy, gc, mask, &xgcv);
10639 else
10641 gc = XCreateGC (dpy, window, mask, &xgcv);
10642 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
10645 if (width < 0)
10646 width = f->output_data.x->cursor_width;
10648 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
10649 x_clip_to_row (w, row, gc, 0);
10650 XFillRectangle (dpy, window, gc,
10652 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
10653 min (cursor_glyph->pixel_width, width),
10654 row->height);
10655 XSetClipMask (dpy, gc, None);
10660 /* Clear the cursor of window W to background color, and mark the
10661 cursor as not shown. This is used when the text where the cursor
10662 is is about to be rewritten. */
10664 static void
10665 x_clear_cursor (w)
10666 struct window *w;
10668 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
10669 x_update_window_cursor (w, 0);
10673 /* Draw the cursor glyph of window W in glyph row ROW. See the
10674 comment of x_draw_glyphs for the meaning of HL. */
10676 static void
10677 x_draw_phys_cursor_glyph (w, row, hl)
10678 struct window *w;
10679 struct glyph_row *row;
10680 enum draw_glyphs_face hl;
10682 /* If cursor hpos is out of bounds, don't draw garbage. This can
10683 happen in mini-buffer windows when switching between echo area
10684 glyphs and mini-buffer. */
10685 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
10687 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
10688 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
10689 hl, 0, 0, 0);
10691 /* When we erase the cursor, and ROW is overlapped by other
10692 rows, make sure that these overlapping parts of other rows
10693 are redrawn. */
10694 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
10696 if (row > w->current_matrix->rows
10697 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
10698 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
10700 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
10701 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
10702 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
10708 /* Erase the image of a cursor of window W from the screen. */
10710 static void
10711 x_erase_phys_cursor (w)
10712 struct window *w;
10714 struct frame *f = XFRAME (w->frame);
10715 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
10716 int hpos = w->phys_cursor.hpos;
10717 int vpos = w->phys_cursor.vpos;
10718 int mouse_face_here_p = 0;
10719 struct glyph_matrix *active_glyphs = w->current_matrix;
10720 struct glyph_row *cursor_row;
10721 struct glyph *cursor_glyph;
10722 enum draw_glyphs_face hl;
10724 /* No cursor displayed or row invalidated => nothing to do on the
10725 screen. */
10726 if (w->phys_cursor_type == NO_CURSOR)
10727 goto mark_cursor_off;
10729 /* VPOS >= active_glyphs->nrows means that window has been resized.
10730 Don't bother to erase the cursor. */
10731 if (vpos >= active_glyphs->nrows)
10732 goto mark_cursor_off;
10734 /* If row containing cursor is marked invalid, there is nothing we
10735 can do. */
10736 cursor_row = MATRIX_ROW (active_glyphs, vpos);
10737 if (!cursor_row->enabled_p)
10738 goto mark_cursor_off;
10740 /* This can happen when the new row is shorter than the old one.
10741 In this case, either x_draw_glyphs or clear_end_of_line
10742 should have cleared the cursor. Note that we wouldn't be
10743 able to erase the cursor in this case because we don't have a
10744 cursor glyph at hand. */
10745 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
10746 goto mark_cursor_off;
10748 /* If the cursor is in the mouse face area, redisplay that when
10749 we clear the cursor. */
10750 if (! NILP (dpyinfo->mouse_face_window)
10751 && w == XWINDOW (dpyinfo->mouse_face_window)
10752 && (vpos > dpyinfo->mouse_face_beg_row
10753 || (vpos == dpyinfo->mouse_face_beg_row
10754 && hpos >= dpyinfo->mouse_face_beg_col))
10755 && (vpos < dpyinfo->mouse_face_end_row
10756 || (vpos == dpyinfo->mouse_face_end_row
10757 && hpos < dpyinfo->mouse_face_end_col))
10758 /* Don't redraw the cursor's spot in mouse face if it is at the
10759 end of a line (on a newline). The cursor appears there, but
10760 mouse highlighting does not. */
10761 && cursor_row->used[TEXT_AREA] > hpos)
10762 mouse_face_here_p = 1;
10764 /* Maybe clear the display under the cursor. */
10765 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
10767 int x;
10768 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
10770 cursor_glyph = get_phys_cursor_glyph (w);
10771 if (cursor_glyph == NULL)
10772 goto mark_cursor_off;
10774 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
10776 XClearArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
10778 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
10779 cursor_row->y)),
10780 cursor_glyph->pixel_width,
10781 cursor_row->visible_height,
10782 False);
10785 /* Erase the cursor by redrawing the character underneath it. */
10786 if (mouse_face_here_p)
10787 hl = DRAW_MOUSE_FACE;
10788 else if (cursor_row->inverse_p)
10789 hl = DRAW_INVERSE_VIDEO;
10790 else
10791 hl = DRAW_NORMAL_TEXT;
10792 x_draw_phys_cursor_glyph (w, cursor_row, hl);
10794 mark_cursor_off:
10795 w->phys_cursor_on_p = 0;
10796 w->phys_cursor_type = NO_CURSOR;
10800 /* Display or clear cursor of window W. If ON is zero, clear the
10801 cursor. If it is non-zero, display the cursor. If ON is nonzero,
10802 where to put the cursor is specified by HPOS, VPOS, X and Y. */
10804 void
10805 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
10806 struct window *w;
10807 int on, hpos, vpos, x, y;
10809 struct frame *f = XFRAME (w->frame);
10810 int new_cursor_type;
10811 int new_cursor_width;
10812 struct glyph_matrix *current_glyphs;
10813 struct glyph_row *glyph_row;
10814 struct glyph *glyph;
10816 /* This is pointless on invisible frames, and dangerous on garbaged
10817 windows and frames; in the latter case, the frame or window may
10818 be in the midst of changing its size, and x and y may be off the
10819 window. */
10820 if (! FRAME_VISIBLE_P (f)
10821 || FRAME_GARBAGED_P (f)
10822 || vpos >= w->current_matrix->nrows
10823 || hpos >= w->current_matrix->matrix_w)
10824 return;
10826 /* If cursor is off and we want it off, return quickly. */
10827 if (!on && !w->phys_cursor_on_p)
10828 return;
10830 current_glyphs = w->current_matrix;
10831 glyph_row = MATRIX_ROW (current_glyphs, vpos);
10832 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
10834 /* If cursor row is not enabled, we don't really know where to
10835 display the cursor. */
10836 if (!glyph_row->enabled_p)
10838 w->phys_cursor_on_p = 0;
10839 return;
10842 xassert (interrupt_input_blocked);
10844 /* Set new_cursor_type to the cursor we want to be displayed. In a
10845 mini-buffer window, we want the cursor only to appear if we are
10846 reading input from this window. For the selected window, we want
10847 the cursor type given by the frame parameter. If explicitly
10848 marked off, draw no cursor. In all other cases, we want a hollow
10849 box cursor. */
10850 new_cursor_width = -1;
10851 if (cursor_in_echo_area
10852 && FRAME_HAS_MINIBUF_P (f)
10853 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
10855 if (w == XWINDOW (echo_area_window))
10856 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10857 else
10858 new_cursor_type = HOLLOW_BOX_CURSOR;
10860 else
10862 if (w != XWINDOW (selected_window)
10863 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
10865 extern int cursor_in_non_selected_windows;
10867 if (MINI_WINDOW_P (w)
10868 || !cursor_in_non_selected_windows
10869 || NILP (XBUFFER (w->buffer)->cursor_type))
10870 new_cursor_type = NO_CURSOR;
10871 else
10872 new_cursor_type = HOLLOW_BOX_CURSOR;
10874 else if (w->cursor_off_p)
10875 new_cursor_type = NO_CURSOR;
10876 else
10878 struct buffer *b = XBUFFER (w->buffer);
10880 if (EQ (b->cursor_type, Qt))
10881 new_cursor_type = FRAME_DESIRED_CURSOR (f);
10882 else
10883 new_cursor_type = x_specified_cursor_type (b->cursor_type,
10884 &new_cursor_width);
10888 /* If cursor is currently being shown and we don't want it to be or
10889 it is in the wrong place, or the cursor type is not what we want,
10890 erase it. */
10891 if (w->phys_cursor_on_p
10892 && (!on
10893 || w->phys_cursor.x != x
10894 || w->phys_cursor.y != y
10895 || new_cursor_type != w->phys_cursor_type))
10896 x_erase_phys_cursor (w);
10898 /* If the cursor is now invisible and we want it to be visible,
10899 display it. */
10900 if (on && !w->phys_cursor_on_p)
10902 w->phys_cursor_ascent = glyph_row->ascent;
10903 w->phys_cursor_height = glyph_row->height;
10905 /* Set phys_cursor_.* before x_draw_.* is called because some
10906 of them may need the information. */
10907 w->phys_cursor.x = x;
10908 w->phys_cursor.y = glyph_row->y;
10909 w->phys_cursor.hpos = hpos;
10910 w->phys_cursor.vpos = vpos;
10911 w->phys_cursor_type = new_cursor_type;
10912 w->phys_cursor_on_p = 1;
10914 switch (new_cursor_type)
10916 case HOLLOW_BOX_CURSOR:
10917 x_draw_hollow_cursor (w, glyph_row);
10918 break;
10920 case FILLED_BOX_CURSOR:
10921 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
10922 break;
10924 case BAR_CURSOR:
10925 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
10926 break;
10928 case NO_CURSOR:
10929 break;
10931 default:
10932 abort ();
10935 #ifdef HAVE_X_I18N
10936 if (w == XWINDOW (f->selected_window))
10937 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
10938 xic_set_preeditarea (w, x, y);
10939 #endif
10942 #ifndef XFlush
10943 if (updating_frame != f)
10944 XFlush (FRAME_X_DISPLAY (f));
10945 #endif
10949 /* Display the cursor on window W, or clear it. X and Y are window
10950 relative pixel coordinates. HPOS and VPOS are glyph matrix
10951 positions. If W is not the selected window, display a hollow
10952 cursor. ON non-zero means display the cursor at X, Y which
10953 correspond to HPOS, VPOS, otherwise it is cleared. */
10955 void
10956 x_display_cursor (w, on, hpos, vpos, x, y)
10957 struct window *w;
10958 int on, hpos, vpos, x, y;
10960 BLOCK_INPUT;
10961 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
10962 UNBLOCK_INPUT;
10966 /* Display the cursor on window W, or clear it, according to ON_P.
10967 Don't change the cursor's position. */
10969 void
10970 x_update_cursor (f, on_p)
10971 struct frame *f;
10973 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
10977 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
10978 in the window tree rooted at W. */
10980 static void
10981 x_update_cursor_in_window_tree (w, on_p)
10982 struct window *w;
10983 int on_p;
10985 while (w)
10987 if (!NILP (w->hchild))
10988 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
10989 else if (!NILP (w->vchild))
10990 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
10991 else
10992 x_update_window_cursor (w, on_p);
10994 w = NILP (w->next) ? 0 : XWINDOW (w->next);
10999 /* Switch the display of W's cursor on or off, according to the value
11000 of ON. */
11002 static void
11003 x_update_window_cursor (w, on)
11004 struct window *w;
11005 int on;
11007 /* Don't update cursor in windows whose frame is in the process
11008 of being deleted. */
11009 if (w->current_matrix)
11011 BLOCK_INPUT;
11012 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11013 w->phys_cursor.x, w->phys_cursor.y);
11014 UNBLOCK_INPUT;
11021 /* Icons. */
11023 /* Refresh bitmap kitchen sink icon for frame F
11024 when we get an expose event for it. */
11026 void
11027 refreshicon (f)
11028 struct frame *f;
11030 /* Normally, the window manager handles this function. */
11033 /* Make the x-window of frame F use the gnu icon bitmap. */
11036 x_bitmap_icon (f, file)
11037 struct frame *f;
11038 Lisp_Object file;
11040 int bitmap_id;
11042 if (FRAME_X_WINDOW (f) == 0)
11043 return 1;
11045 /* Free up our existing icon bitmap if any. */
11046 if (f->output_data.x->icon_bitmap > 0)
11047 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11048 f->output_data.x->icon_bitmap = 0;
11050 if (STRINGP (file))
11051 bitmap_id = x_create_bitmap_from_file (f, file);
11052 else
11054 /* Create the GNU bitmap if necessary. */
11055 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11056 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11057 = x_create_bitmap_from_data (f, gnu_bits,
11058 gnu_width, gnu_height);
11060 /* The first time we create the GNU bitmap,
11061 this increments the ref-count one extra time.
11062 As a result, the GNU bitmap is never freed.
11063 That way, we don't have to worry about allocating it again. */
11064 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11066 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11069 x_wm_set_icon_pixmap (f, bitmap_id);
11070 f->output_data.x->icon_bitmap = bitmap_id;
11072 return 0;
11076 /* Make the x-window of frame F use a rectangle with text.
11077 Use ICON_NAME as the text. */
11080 x_text_icon (f, icon_name)
11081 struct frame *f;
11082 char *icon_name;
11084 if (FRAME_X_WINDOW (f) == 0)
11085 return 1;
11087 #ifdef HAVE_X11R4
11089 XTextProperty text;
11090 text.value = (unsigned char *) icon_name;
11091 text.encoding = XA_STRING;
11092 text.format = 8;
11093 text.nitems = strlen (icon_name);
11094 #ifdef USE_X_TOOLKIT
11095 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11096 &text);
11097 #else /* not USE_X_TOOLKIT */
11098 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11099 #endif /* not USE_X_TOOLKIT */
11101 #else /* not HAVE_X11R4 */
11102 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11103 #endif /* not HAVE_X11R4 */
11105 if (f->output_data.x->icon_bitmap > 0)
11106 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11107 f->output_data.x->icon_bitmap = 0;
11108 x_wm_set_icon_pixmap (f, 0);
11110 return 0;
11113 #define X_ERROR_MESSAGE_SIZE 200
11115 /* If non-nil, this should be a string.
11116 It means catch X errors and store the error message in this string. */
11118 static Lisp_Object x_error_message_string;
11120 /* An X error handler which stores the error message in
11121 x_error_message_string. This is called from x_error_handler if
11122 x_catch_errors is in effect. */
11124 static void
11125 x_error_catcher (display, error)
11126 Display *display;
11127 XErrorEvent *error;
11129 XGetErrorText (display, error->error_code,
11130 XSTRING (x_error_message_string)->data,
11131 X_ERROR_MESSAGE_SIZE);
11134 /* Begin trapping X errors for display DPY. Actually we trap X errors
11135 for all displays, but DPY should be the display you are actually
11136 operating on.
11138 After calling this function, X protocol errors no longer cause
11139 Emacs to exit; instead, they are recorded in the string
11140 stored in x_error_message_string.
11142 Calling x_check_errors signals an Emacs error if an X error has
11143 occurred since the last call to x_catch_errors or x_check_errors.
11145 Calling x_uncatch_errors resumes the normal error handling. */
11147 void x_check_errors ();
11148 static Lisp_Object x_catch_errors_unwind ();
11151 x_catch_errors (dpy)
11152 Display *dpy;
11154 int count = specpdl_ptr - specpdl;
11156 /* Make sure any errors from previous requests have been dealt with. */
11157 XSync (dpy, False);
11159 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11161 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11162 XSTRING (x_error_message_string)->data[0] = 0;
11164 return count;
11167 /* Unbind the binding that we made to check for X errors. */
11169 static Lisp_Object
11170 x_catch_errors_unwind (old_val)
11171 Lisp_Object old_val;
11173 x_error_message_string = old_val;
11174 return Qnil;
11177 /* If any X protocol errors have arrived since the last call to
11178 x_catch_errors or x_check_errors, signal an Emacs error using
11179 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11181 void
11182 x_check_errors (dpy, format)
11183 Display *dpy;
11184 char *format;
11186 /* Make sure to catch any errors incurred so far. */
11187 XSync (dpy, False);
11189 if (XSTRING (x_error_message_string)->data[0])
11190 error (format, XSTRING (x_error_message_string)->data);
11193 /* Nonzero if we had any X protocol errors
11194 since we did x_catch_errors on DPY. */
11197 x_had_errors_p (dpy)
11198 Display *dpy;
11200 /* Make sure to catch any errors incurred so far. */
11201 XSync (dpy, False);
11203 return XSTRING (x_error_message_string)->data[0] != 0;
11206 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11208 void
11209 x_clear_errors (dpy)
11210 Display *dpy;
11212 XSTRING (x_error_message_string)->data[0] = 0;
11215 /* Stop catching X protocol errors and let them make Emacs die.
11216 DPY should be the display that was passed to x_catch_errors.
11217 COUNT should be the value that was returned by
11218 the corresponding call to x_catch_errors. */
11220 void
11221 x_uncatch_errors (dpy, count)
11222 Display *dpy;
11223 int count;
11225 unbind_to (count, Qnil);
11228 #if 0
11229 static unsigned int x_wire_count;
11230 x_trace_wire ()
11232 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
11234 #endif /* ! 0 */
11237 /* Handle SIGPIPE, which can happen when the connection to a server
11238 simply goes away. SIGPIPE is handled by x_connection_signal.
11239 Don't need to do anything, because the write which caused the
11240 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
11241 which will do the appropriate cleanup for us. */
11243 static SIGTYPE
11244 x_connection_signal (signalnum) /* If we don't have an argument, */
11245 int signalnum; /* some compilers complain in signal calls. */
11247 #ifdef USG
11248 /* USG systems forget handlers when they are used;
11249 must reestablish each time */
11250 signal (signalnum, x_connection_signal);
11251 #endif /* USG */
11254 /* Handling X errors. */
11256 /* Handle the loss of connection to display DISPLAY. */
11258 static SIGTYPE
11259 x_connection_closed (display, error_message)
11260 Display *display;
11261 char *error_message;
11263 struct x_display_info *dpyinfo = x_display_info_for_display (display);
11264 Lisp_Object frame, tail;
11266 /* We have to close the display to inform Xt that it doesn't
11267 exist anymore. If we don't, Xt will continue to wait for
11268 events from the display. As a consequence, a sequence of
11270 M-x make-frame-on-display RET :1 RET
11271 ...kill the new frame, so that we get an IO error...
11272 M-x make-frame-on-display RET :1 RET
11274 will indefinitely wait in Xt for events for display `:1', opened
11275 in the first class to make-frame-on-display.
11277 Closing the display is reported to lead to a bus error on
11278 OpenWindows in certain situations. I suspect that is a bug
11279 in OpenWindows. I don't know how to cicumvent it here. */
11281 #ifdef USE_X_TOOLKIT
11282 XtCloseDisplay (display);
11283 #endif
11285 /* Indicate that this display is dead. */
11286 if (dpyinfo)
11287 dpyinfo->display = 0;
11289 /* First delete frames whose mini-buffers are on frames
11290 that are on the dead display. */
11291 FOR_EACH_FRAME (tail, frame)
11293 Lisp_Object minibuf_frame;
11294 minibuf_frame
11295 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
11296 if (FRAME_X_P (XFRAME (frame))
11297 && FRAME_X_P (XFRAME (minibuf_frame))
11298 && ! EQ (frame, minibuf_frame)
11299 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
11300 Fdelete_frame (frame, Qt);
11303 /* Now delete all remaining frames on the dead display.
11304 We are now sure none of these is used as the mini-buffer
11305 for another frame that we need to delete. */
11306 FOR_EACH_FRAME (tail, frame)
11307 if (FRAME_X_P (XFRAME (frame))
11308 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
11310 /* Set this to t so that Fdelete_frame won't get confused
11311 trying to find a replacement. */
11312 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
11313 Fdelete_frame (frame, Qt);
11316 if (dpyinfo)
11317 x_delete_display (dpyinfo);
11319 if (x_display_list == 0)
11321 fprintf (stderr, "%s\n", error_message);
11322 shut_down_emacs (0, 0, Qnil);
11323 exit (70);
11326 /* Ordinary stack unwind doesn't deal with these. */
11327 #ifdef SIGIO
11328 sigunblock (sigmask (SIGIO));
11329 #endif
11330 sigunblock (sigmask (SIGALRM));
11331 TOTALLY_UNBLOCK_INPUT;
11333 clear_waiting_for_input ();
11334 handling_signal = 0;
11335 error ("%s", error_message);
11338 /* This is the usual handler for X protocol errors.
11339 It kills all frames on the display that we got the error for.
11340 If that was the only one, it prints an error message and kills Emacs. */
11342 static void
11343 x_error_quitter (display, error)
11344 Display *display;
11345 XErrorEvent *error;
11347 char buf[256], buf1[356];
11349 /* Note that there is no real way portable across R3/R4 to get the
11350 original error handler. */
11352 XGetErrorText (display, error->error_code, buf, sizeof (buf));
11353 sprintf (buf1, "X protocol error: %s on protocol request %d",
11354 buf, error->request_code);
11355 x_connection_closed (display, buf1);
11358 /* This is the first-level handler for X protocol errors.
11359 It calls x_error_quitter or x_error_catcher. */
11361 static int
11362 x_error_handler (display, error)
11363 Display *display;
11364 XErrorEvent *error;
11366 if (! NILP (x_error_message_string))
11367 x_error_catcher (display, error);
11368 else
11369 x_error_quitter (display, error);
11370 return 0;
11373 /* This is the handler for X IO errors, always.
11374 It kills all frames on the display that we lost touch with.
11375 If that was the only one, it prints an error message and kills Emacs. */
11377 static int
11378 x_io_error_quitter (display)
11379 Display *display;
11381 char buf[256];
11383 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
11384 x_connection_closed (display, buf);
11385 return 0;
11388 /* Changing the font of the frame. */
11390 /* Give frame F the font named FONTNAME as its default font, and
11391 return the full name of that font. FONTNAME may be a wildcard
11392 pattern; in that case, we choose some font that fits the pattern.
11393 The return value shows which font we chose. */
11395 Lisp_Object
11396 x_new_font (f, fontname)
11397 struct frame *f;
11398 register char *fontname;
11400 struct font_info *fontp
11401 = FS_LOAD_FONT (f, 0, fontname, -1);
11403 if (!fontp)
11404 return Qnil;
11406 f->output_data.x->font = (XFontStruct *) (fontp->font);
11407 f->output_data.x->baseline_offset = fontp->baseline_offset;
11408 f->output_data.x->fontset = -1;
11410 /* Compute the scroll bar width in character columns. */
11411 if (f->scroll_bar_pixel_width > 0)
11413 int wid = FONT_WIDTH (f->output_data.x->font);
11414 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
11416 else
11418 int wid = FONT_WIDTH (f->output_data.x->font);
11419 f->scroll_bar_cols = (14 + wid - 1) / wid;
11422 /* Now make the frame display the given font. */
11423 if (FRAME_X_WINDOW (f) != 0)
11425 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
11426 f->output_data.x->font->fid);
11427 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
11428 f->output_data.x->font->fid);
11429 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
11430 f->output_data.x->font->fid);
11432 frame_update_line_height (f);
11433 x_set_window_size (f, 0, f->width, f->height);
11435 else
11436 /* If we are setting a new frame's font for the first time,
11437 there are no faces yet, so this font's height is the line height. */
11438 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
11440 return build_string (fontp->full_name);
11443 /* Give frame F the fontset named FONTSETNAME as its default font, and
11444 return the full name of that fontset. FONTSETNAME may be a wildcard
11445 pattern; in that case, we choose some fontset that fits the pattern.
11446 The return value shows which fontset we chose. */
11448 Lisp_Object
11449 x_new_fontset (f, fontsetname)
11450 struct frame *f;
11451 char *fontsetname;
11453 int fontset = fs_query_fontset (build_string (fontsetname), 0);
11454 Lisp_Object result;
11456 if (fontset < 0)
11457 return Qnil;
11459 if (f->output_data.x->fontset == fontset)
11460 /* This fontset is already set in frame F. There's nothing more
11461 to do. */
11462 return fontset_name (fontset);
11464 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
11466 if (!STRINGP (result))
11467 /* Can't load ASCII font. */
11468 return Qnil;
11470 /* Since x_new_font doesn't update any fontset information, do it now. */
11471 f->output_data.x->fontset = fontset;
11473 #ifdef HAVE_X_I18N
11474 if (FRAME_XIC (f)
11475 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
11476 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
11477 #endif
11479 return build_string (fontsetname);
11483 /***********************************************************************
11484 X Input Methods
11485 ***********************************************************************/
11487 #ifdef HAVE_X_I18N
11489 #ifdef HAVE_X11R6
11491 /* XIM destroy callback function, which is called whenever the
11492 connection to input method XIM dies. CLIENT_DATA contains a
11493 pointer to the x_display_info structure corresponding to XIM. */
11495 static void
11496 xim_destroy_callback (xim, client_data, call_data)
11497 XIM xim;
11498 XPointer client_data;
11499 XPointer call_data;
11501 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
11502 Lisp_Object frame, tail;
11504 BLOCK_INPUT;
11506 /* No need to call XDestroyIC.. */
11507 FOR_EACH_FRAME (tail, frame)
11509 struct frame *f = XFRAME (frame);
11510 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
11512 FRAME_XIC (f) = NULL;
11513 if (FRAME_XIC_FONTSET (f))
11515 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
11516 FRAME_XIC_FONTSET (f) = NULL;
11521 /* No need to call XCloseIM. */
11522 dpyinfo->xim = NULL;
11523 XFree (dpyinfo->xim_styles);
11524 UNBLOCK_INPUT;
11527 #endif /* HAVE_X11R6 */
11529 /* Open the connection to the XIM server on display DPYINFO.
11530 RESOURCE_NAME is the resource name Emacs uses. */
11532 static void
11533 xim_open_dpy (dpyinfo, resource_name)
11534 struct x_display_info *dpyinfo;
11535 char *resource_name;
11537 #ifdef USE_XIM
11538 XIM xim;
11540 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
11541 dpyinfo->xim = xim;
11543 if (xim)
11545 #ifdef HAVE_X11R6
11546 XIMCallback destroy;
11547 #endif
11549 /* Get supported styles and XIM values. */
11550 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
11552 #ifdef HAVE_X11R6
11553 destroy.callback = xim_destroy_callback;
11554 destroy.client_data = (XPointer)dpyinfo;
11555 /* This isn't prptotyped in OSF 5.0. */
11556 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
11557 #endif
11560 #else /* not USE_XIM */
11561 dpyinfo->xim = NULL;
11562 #endif /* not USE_XIM */
11566 #ifdef HAVE_X11R6_XIM
11568 struct xim_inst_t
11570 struct x_display_info *dpyinfo;
11571 char *resource_name;
11574 /* XIM instantiate callback function, which is called whenever an XIM
11575 server is available. DISPLAY is teh display of the XIM.
11576 CLIENT_DATA contains a pointer to an xim_inst_t structure created
11577 when the callback was registered. */
11579 static void
11580 xim_instantiate_callback (display, client_data, call_data)
11581 Display *display;
11582 XPointer client_data;
11583 XPointer call_data;
11585 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
11586 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
11588 /* We don't support multiple XIM connections. */
11589 if (dpyinfo->xim)
11590 return;
11592 xim_open_dpy (dpyinfo, xim_inst->resource_name);
11594 /* Create XIC for the existing frames on the same display, as long
11595 as they have no XIC. */
11596 if (dpyinfo->xim && dpyinfo->reference_count > 0)
11598 Lisp_Object tail, frame;
11600 BLOCK_INPUT;
11601 FOR_EACH_FRAME (tail, frame)
11603 struct frame *f = XFRAME (frame);
11605 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
11606 if (FRAME_XIC (f) == NULL)
11608 create_frame_xic (f);
11609 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
11610 xic_set_statusarea (f);
11611 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
11613 struct window *w = XWINDOW (f->selected_window);
11614 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
11619 UNBLOCK_INPUT;
11623 #endif /* HAVE_X11R6_XIM */
11626 /* Open a connection to the XIM server on display DPYINFO.
11627 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
11628 connection only at the first time. On X11R6, open the connection
11629 in the XIM instantiate callback function. */
11631 static void
11632 xim_initialize (dpyinfo, resource_name)
11633 struct x_display_info *dpyinfo;
11634 char *resource_name;
11636 #ifdef USE_XIM
11637 #ifdef HAVE_X11R6_XIM
11638 struct xim_inst_t *xim_inst;
11639 int len;
11641 dpyinfo->xim = NULL;
11642 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
11643 xim_inst->dpyinfo = dpyinfo;
11644 len = strlen (resource_name);
11645 xim_inst->resource_name = (char *) xmalloc (len + 1);
11646 bcopy (resource_name, xim_inst->resource_name, len + 1);
11647 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11648 resource_name, EMACS_CLASS,
11649 xim_instantiate_callback,
11650 /* Fixme: This is XPointer in
11651 XFree86 but (XPointer *) on
11652 Tru64, at least. */
11653 (XPointer) xim_inst);
11654 #else /* not HAVE_X11R6_XIM */
11655 dpyinfo->xim = NULL;
11656 xim_open_dpy (dpyinfo, resource_name);
11657 #endif /* not HAVE_X11R6_XIM */
11659 #else /* not USE_XIM */
11660 dpyinfo->xim = NULL;
11661 #endif /* not USE_XIM */
11665 /* Close the connection to the XIM server on display DPYINFO. */
11667 static void
11668 xim_close_dpy (dpyinfo)
11669 struct x_display_info *dpyinfo;
11671 #ifdef USE_XIM
11672 #ifdef HAVE_X11R6_XIM
11673 if (dpyinfo->display)
11674 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
11675 NULL, EMACS_CLASS,
11676 xim_instantiate_callback, NULL);
11677 #endif /* not HAVE_X11R6_XIM */
11678 if (dpyinfo->display)
11679 XCloseIM (dpyinfo->xim);
11680 dpyinfo->xim = NULL;
11681 XFree (dpyinfo->xim_styles);
11682 #endif /* USE_XIM */
11685 #endif /* not HAVE_X11R6_XIM */
11689 /* Calculate the absolute position in frame F
11690 from its current recorded position values and gravity. */
11692 void
11693 x_calc_absolute_position (f)
11694 struct frame *f;
11696 Window child;
11697 int win_x = 0, win_y = 0;
11698 int flags = f->output_data.x->size_hint_flags;
11699 int this_window;
11701 /* We have nothing to do if the current position
11702 is already for the top-left corner. */
11703 if (! ((flags & XNegative) || (flags & YNegative)))
11704 return;
11706 #ifdef USE_X_TOOLKIT
11707 this_window = XtWindow (f->output_data.x->widget);
11708 #else
11709 this_window = FRAME_X_WINDOW (f);
11710 #endif
11712 /* Find the position of the outside upper-left corner of
11713 the inner window, with respect to the outer window.
11714 But do this only if we will need the results. */
11715 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
11717 int count;
11719 BLOCK_INPUT;
11720 count = x_catch_errors (FRAME_X_DISPLAY (f));
11721 while (1)
11723 x_clear_errors (FRAME_X_DISPLAY (f));
11724 XTranslateCoordinates (FRAME_X_DISPLAY (f),
11726 /* From-window, to-window. */
11727 this_window,
11728 f->output_data.x->parent_desc,
11730 /* From-position, to-position. */
11731 0, 0, &win_x, &win_y,
11733 /* Child of win. */
11734 &child);
11735 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
11737 Window newroot, newparent = 0xdeadbeef;
11738 Window *newchildren;
11739 unsigned int nchildren;
11741 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
11742 &newparent, &newchildren, &nchildren))
11743 break;
11745 XFree ((char *) newchildren);
11747 f->output_data.x->parent_desc = newparent;
11749 else
11750 break;
11753 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
11754 UNBLOCK_INPUT;
11757 /* Treat negative positions as relative to the leftmost bottommost
11758 position that fits on the screen. */
11759 if (flags & XNegative)
11760 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
11761 - 2 * f->output_data.x->border_width - win_x
11762 - PIXEL_WIDTH (f)
11763 + f->output_data.x->left_pos);
11766 int height = PIXEL_HEIGHT (f);
11768 #if defined USE_X_TOOLKIT && defined USE_MOTIF
11769 /* Something is fishy here. When using Motif, starting Emacs with
11770 `-g -0-0', the frame appears too low by a few pixels.
11772 This seems to be so because initially, while Emacs is starting,
11773 the column widget's height and the frame's pixel height are
11774 different. The column widget's height is the right one. In
11775 later invocations, when Emacs is up, the frame's pixel height
11776 is right, though.
11778 It's not obvious where the initial small difference comes from.
11779 2000-12-01, gerd. */
11781 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
11782 #endif
11784 if (flags & YNegative)
11785 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
11786 - 2 * f->output_data.x->border_width
11787 - win_y
11788 - height
11789 + f->output_data.x->top_pos);
11792 /* The left_pos and top_pos
11793 are now relative to the top and left screen edges,
11794 so the flags should correspond. */
11795 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11798 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
11799 to really change the position, and 0 when calling from
11800 x_make_frame_visible (in that case, XOFF and YOFF are the current
11801 position values). It is -1 when calling from x_set_frame_parameters,
11802 which means, do adjust for borders but don't change the gravity. */
11804 void
11805 x_set_offset (f, xoff, yoff, change_gravity)
11806 struct frame *f;
11807 register int xoff, yoff;
11808 int change_gravity;
11810 int modified_top, modified_left;
11812 if (change_gravity > 0)
11814 f->output_data.x->top_pos = yoff;
11815 f->output_data.x->left_pos = xoff;
11816 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
11817 if (xoff < 0)
11818 f->output_data.x->size_hint_flags |= XNegative;
11819 if (yoff < 0)
11820 f->output_data.x->size_hint_flags |= YNegative;
11821 f->output_data.x->win_gravity = NorthWestGravity;
11823 x_calc_absolute_position (f);
11825 BLOCK_INPUT;
11826 x_wm_set_size_hint (f, (long) 0, 0);
11828 modified_left = f->output_data.x->left_pos;
11829 modified_top = f->output_data.x->top_pos;
11830 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
11831 this seems to be unnecessary and incorrect. rms, 4/17/97. */
11832 /* It is a mystery why we need to add the border_width here
11833 when the frame is already visible, but experiment says we do. */
11834 if (change_gravity != 0)
11836 modified_left += f->output_data.x->border_width;
11837 modified_top += f->output_data.x->border_width;
11839 #endif
11841 #ifdef USE_X_TOOLKIT
11842 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11843 modified_left, modified_top);
11844 #else /* not USE_X_TOOLKIT */
11845 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11846 modified_left, modified_top);
11847 #endif /* not USE_X_TOOLKIT */
11848 UNBLOCK_INPUT;
11851 /* Call this to change the size of frame F's x-window.
11852 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
11853 for this size change and subsequent size changes.
11854 Otherwise we leave the window gravity unchanged. */
11856 void
11857 x_set_window_size (f, change_gravity, cols, rows)
11858 struct frame *f;
11859 int change_gravity;
11860 int cols, rows;
11862 #ifndef USE_X_TOOLKIT
11863 int pixelwidth, pixelheight;
11864 #endif
11866 BLOCK_INPUT;
11868 #ifdef USE_X_TOOLKIT
11870 /* The x and y position of the widget is clobbered by the
11871 call to XtSetValues within EmacsFrameSetCharSize.
11872 This is a real kludge, but I don't understand Xt so I can't
11873 figure out a correct fix. Can anyone else tell me? -- rms. */
11874 int xpos = f->output_data.x->widget->core.x;
11875 int ypos = f->output_data.x->widget->core.y;
11876 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
11877 f->output_data.x->widget->core.x = xpos;
11878 f->output_data.x->widget->core.y = ypos;
11881 #else /* not USE_X_TOOLKIT */
11883 check_frame_size (f, &rows, &cols);
11884 f->output_data.x->vertical_scroll_bar_extra
11885 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
11887 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
11888 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
11889 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
11890 f->output_data.x->flags_areas_extra
11891 = FRAME_FLAGS_AREA_WIDTH (f);
11892 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
11893 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
11895 f->output_data.x->win_gravity = NorthWestGravity;
11896 x_wm_set_size_hint (f, (long) 0, 0);
11898 XSync (FRAME_X_DISPLAY (f), False);
11899 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11900 pixelwidth, pixelheight);
11902 /* Now, strictly speaking, we can't be sure that this is accurate,
11903 but the window manager will get around to dealing with the size
11904 change request eventually, and we'll hear how it went when the
11905 ConfigureNotify event gets here.
11907 We could just not bother storing any of this information here,
11908 and let the ConfigureNotify event set everything up, but that
11909 might be kind of confusing to the Lisp code, since size changes
11910 wouldn't be reported in the frame parameters until some random
11911 point in the future when the ConfigureNotify event arrives.
11913 We pass 1 for DELAY since we can't run Lisp code inside of
11914 a BLOCK_INPUT. */
11915 change_frame_size (f, rows, cols, 0, 1, 0);
11916 PIXEL_WIDTH (f) = pixelwidth;
11917 PIXEL_HEIGHT (f) = pixelheight;
11919 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
11920 receive in the ConfigureNotify event; if we get what we asked
11921 for, then the event won't cause the screen to become garbaged, so
11922 we have to make sure to do it here. */
11923 SET_FRAME_GARBAGED (f);
11925 XFlush (FRAME_X_DISPLAY (f));
11927 #endif /* not USE_X_TOOLKIT */
11929 /* If cursor was outside the new size, mark it as off. */
11930 mark_window_cursors_off (XWINDOW (f->root_window));
11932 /* Clear out any recollection of where the mouse highlighting was,
11933 since it might be in a place that's outside the new frame size.
11934 Actually checking whether it is outside is a pain in the neck,
11935 so don't try--just let the highlighting be done afresh with new size. */
11936 cancel_mouse_face (f);
11938 UNBLOCK_INPUT;
11941 /* Mouse warping. */
11943 void
11944 x_set_mouse_position (f, x, y)
11945 struct frame *f;
11946 int x, y;
11948 int pix_x, pix_y;
11950 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
11951 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
11953 if (pix_x < 0) pix_x = 0;
11954 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
11956 if (pix_y < 0) pix_y = 0;
11957 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
11959 BLOCK_INPUT;
11961 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11962 0, 0, 0, 0, pix_x, pix_y);
11963 UNBLOCK_INPUT;
11966 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
11968 void
11969 x_set_mouse_pixel_position (f, pix_x, pix_y)
11970 struct frame *f;
11971 int pix_x, pix_y;
11973 BLOCK_INPUT;
11975 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
11976 0, 0, 0, 0, pix_x, pix_y);
11977 UNBLOCK_INPUT;
11980 /* focus shifting, raising and lowering. */
11982 void
11983 x_focus_on_frame (f)
11984 struct frame *f;
11986 #if 0 /* This proves to be unpleasant. */
11987 x_raise_frame (f);
11988 #endif
11989 #if 0
11990 /* I don't think that the ICCCM allows programs to do things like this
11991 without the interaction of the window manager. Whatever you end up
11992 doing with this code, do it to x_unfocus_frame too. */
11993 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11994 RevertToPointerRoot, CurrentTime);
11995 #endif /* ! 0 */
11998 void
11999 x_unfocus_frame (f)
12000 struct frame *f;
12002 #if 0
12003 /* Look at the remarks in x_focus_on_frame. */
12004 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
12005 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
12006 RevertToPointerRoot, CurrentTime);
12007 #endif /* ! 0 */
12010 /* Raise frame F. */
12012 void
12013 x_raise_frame (f)
12014 struct frame *f;
12016 if (f->async_visible)
12018 BLOCK_INPUT;
12019 #ifdef USE_X_TOOLKIT
12020 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12021 #else /* not USE_X_TOOLKIT */
12022 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12023 #endif /* not USE_X_TOOLKIT */
12024 XFlush (FRAME_X_DISPLAY (f));
12025 UNBLOCK_INPUT;
12029 /* Lower frame F. */
12031 void
12032 x_lower_frame (f)
12033 struct frame *f;
12035 if (f->async_visible)
12037 BLOCK_INPUT;
12038 #ifdef USE_X_TOOLKIT
12039 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
12040 #else /* not USE_X_TOOLKIT */
12041 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12042 #endif /* not USE_X_TOOLKIT */
12043 XFlush (FRAME_X_DISPLAY (f));
12044 UNBLOCK_INPUT;
12048 static void
12049 XTframe_raise_lower (f, raise_flag)
12050 FRAME_PTR f;
12051 int raise_flag;
12053 if (raise_flag)
12054 x_raise_frame (f);
12055 else
12056 x_lower_frame (f);
12059 /* Change of visibility. */
12061 /* This tries to wait until the frame is really visible.
12062 However, if the window manager asks the user where to position
12063 the frame, this will return before the user finishes doing that.
12064 The frame will not actually be visible at that time,
12065 but it will become visible later when the window manager
12066 finishes with it. */
12068 void
12069 x_make_frame_visible (f)
12070 struct frame *f;
12072 Lisp_Object type;
12073 int original_top, original_left;
12074 int retry_count = 2;
12076 retry:
12078 BLOCK_INPUT;
12080 type = x_icon_type (f);
12081 if (!NILP (type))
12082 x_bitmap_icon (f, type);
12084 if (! FRAME_VISIBLE_P (f))
12086 /* We test FRAME_GARBAGED_P here to make sure we don't
12087 call x_set_offset a second time
12088 if we get to x_make_frame_visible a second time
12089 before the window gets really visible. */
12090 if (! FRAME_ICONIFIED_P (f)
12091 && ! f->output_data.x->asked_for_visible)
12092 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12094 f->output_data.x->asked_for_visible = 1;
12096 if (! EQ (Vx_no_window_manager, Qt))
12097 x_wm_set_window_state (f, NormalState);
12098 #ifdef USE_X_TOOLKIT
12099 /* This was XtPopup, but that did nothing for an iconified frame. */
12100 XtMapWidget (f->output_data.x->widget);
12101 #else /* not USE_X_TOOLKIT */
12102 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12103 #endif /* not USE_X_TOOLKIT */
12104 #if 0 /* This seems to bring back scroll bars in the wrong places
12105 if the window configuration has changed. They seem
12106 to come back ok without this. */
12107 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
12108 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12109 #endif
12112 XFlush (FRAME_X_DISPLAY (f));
12114 /* Synchronize to ensure Emacs knows the frame is visible
12115 before we do anything else. We do this loop with input not blocked
12116 so that incoming events are handled. */
12118 Lisp_Object frame;
12119 int count;
12120 /* This must be before UNBLOCK_INPUT
12121 since events that arrive in response to the actions above
12122 will set it when they are handled. */
12123 int previously_visible = f->output_data.x->has_been_visible;
12125 original_left = f->output_data.x->left_pos;
12126 original_top = f->output_data.x->top_pos;
12128 /* This must come after we set COUNT. */
12129 UNBLOCK_INPUT;
12131 /* We unblock here so that arriving X events are processed. */
12133 /* Now move the window back to where it was "supposed to be".
12134 But don't do it if the gravity is negative.
12135 When the gravity is negative, this uses a position
12136 that is 3 pixels too low. Perhaps that's really the border width.
12138 Don't do this if the window has never been visible before,
12139 because the window manager may choose the position
12140 and we don't want to override it. */
12142 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
12143 && f->output_data.x->win_gravity == NorthWestGravity
12144 && previously_visible)
12146 Drawable rootw;
12147 int x, y;
12148 unsigned int width, height, border, depth;
12150 BLOCK_INPUT;
12152 /* On some window managers (such as FVWM) moving an existing
12153 window, even to the same place, causes the window manager
12154 to introduce an offset. This can cause the window to move
12155 to an unexpected location. Check the geometry (a little
12156 slow here) and then verify that the window is in the right
12157 place. If the window is not in the right place, move it
12158 there, and take the potential window manager hit. */
12159 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12160 &rootw, &x, &y, &width, &height, &border, &depth);
12162 if (original_left != x || original_top != y)
12163 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
12164 original_left, original_top);
12166 UNBLOCK_INPUT;
12169 XSETFRAME (frame, f);
12171 /* Wait until the frame is visible. Process X events until a
12172 MapNotify event has been seen, or until we think we won't get a
12173 MapNotify at all.. */
12174 for (count = input_signal_count + 10;
12175 input_signal_count < count && !FRAME_VISIBLE_P (f);)
12177 /* Force processing of queued events. */
12178 x_sync (f);
12180 /* Machines that do polling rather than SIGIO have been
12181 observed to go into a busy-wait here. So we'll fake an
12182 alarm signal to let the handler know that there's something
12183 to be read. We used to raise a real alarm, but it seems
12184 that the handler isn't always enabled here. This is
12185 probably a bug. */
12186 if (input_polling_used ())
12188 /* It could be confusing if a real alarm arrives while
12189 processing the fake one. Turn it off and let the
12190 handler reset it. */
12191 extern void poll_for_input_1 P_ ((void));
12192 int old_poll_suppress_count = poll_suppress_count;
12193 poll_suppress_count = 1;
12194 poll_for_input_1 ();
12195 poll_suppress_count = old_poll_suppress_count;
12198 /* See if a MapNotify event has been processed. */
12199 FRAME_SAMPLE_VISIBILITY (f);
12202 /* 2000-09-28: In
12204 (let ((f (selected-frame)))
12205 (iconify-frame f)
12206 (raise-frame f))
12208 the frame is not raised with various window managers on
12209 FreeBSD, Linux and Solaris. It turns out that, for some
12210 unknown reason, the call to XtMapWidget is completely ignored.
12211 Mapping the widget a second time works. */
12213 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
12214 goto retry;
12218 /* Change from mapped state to withdrawn state. */
12220 /* Make the frame visible (mapped and not iconified). */
12222 void
12223 x_make_frame_invisible (f)
12224 struct frame *f;
12226 Window window;
12228 #ifdef USE_X_TOOLKIT
12229 /* Use the frame's outermost window, not the one we normally draw on. */
12230 window = XtWindow (f->output_data.x->widget);
12231 #else /* not USE_X_TOOLKIT */
12232 window = FRAME_X_WINDOW (f);
12233 #endif /* not USE_X_TOOLKIT */
12235 /* Don't keep the highlight on an invisible frame. */
12236 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12237 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12239 #if 0/* This might add unreliability; I don't trust it -- rms. */
12240 if (! f->async_visible && ! f->async_iconified)
12241 return;
12242 #endif
12244 BLOCK_INPUT;
12246 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
12247 that the current position of the window is user-specified, rather than
12248 program-specified, so that when the window is mapped again, it will be
12249 placed at the same location, without forcing the user to position it
12250 by hand again (they have already done that once for this window.) */
12251 x_wm_set_size_hint (f, (long) 0, 1);
12253 #ifdef HAVE_X11R4
12255 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
12256 DefaultScreen (FRAME_X_DISPLAY (f))))
12258 UNBLOCK_INPUT_RESIGNAL;
12259 error ("Can't notify window manager of window withdrawal");
12261 #else /* ! defined (HAVE_X11R4) */
12263 /* Tell the window manager what we're going to do. */
12264 if (! EQ (Vx_no_window_manager, Qt))
12266 XEvent unmap;
12268 unmap.xunmap.type = UnmapNotify;
12269 unmap.xunmap.window = window;
12270 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
12271 unmap.xunmap.from_configure = False;
12272 if (! XSendEvent (FRAME_X_DISPLAY (f),
12273 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12274 False,
12275 SubstructureRedirectMaskSubstructureNotifyMask,
12276 &unmap))
12278 UNBLOCK_INPUT_RESIGNAL;
12279 error ("Can't notify window manager of withdrawal");
12283 /* Unmap the window ourselves. Cheeky! */
12284 XUnmapWindow (FRAME_X_DISPLAY (f), window);
12285 #endif /* ! defined (HAVE_X11R4) */
12287 /* We can't distinguish this from iconification
12288 just by the event that we get from the server.
12289 So we can't win using the usual strategy of letting
12290 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
12291 and synchronize with the server to make sure we agree. */
12292 f->visible = 0;
12293 FRAME_ICONIFIED_P (f) = 0;
12294 f->async_visible = 0;
12295 f->async_iconified = 0;
12297 x_sync (f);
12299 UNBLOCK_INPUT;
12302 /* Change window state from mapped to iconified. */
12304 void
12305 x_iconify_frame (f)
12306 struct frame *f;
12308 int result;
12309 Lisp_Object type;
12311 /* Don't keep the highlight on an invisible frame. */
12312 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
12313 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
12315 if (f->async_iconified)
12316 return;
12318 BLOCK_INPUT;
12320 FRAME_SAMPLE_VISIBILITY (f);
12322 type = x_icon_type (f);
12323 if (!NILP (type))
12324 x_bitmap_icon (f, type);
12326 #ifdef USE_X_TOOLKIT
12328 if (! FRAME_VISIBLE_P (f))
12330 if (! EQ (Vx_no_window_manager, Qt))
12331 x_wm_set_window_state (f, IconicState);
12332 /* This was XtPopup, but that did nothing for an iconified frame. */
12333 XtMapWidget (f->output_data.x->widget);
12334 /* The server won't give us any event to indicate
12335 that an invisible frame was changed to an icon,
12336 so we have to record it here. */
12337 f->iconified = 1;
12338 f->visible = 1;
12339 f->async_iconified = 1;
12340 f->async_visible = 0;
12341 UNBLOCK_INPUT;
12342 return;
12345 result = XIconifyWindow (FRAME_X_DISPLAY (f),
12346 XtWindow (f->output_data.x->widget),
12347 DefaultScreen (FRAME_X_DISPLAY (f)));
12348 UNBLOCK_INPUT;
12350 if (!result)
12351 error ("Can't notify window manager of iconification");
12353 f->async_iconified = 1;
12354 f->async_visible = 0;
12357 BLOCK_INPUT;
12358 XFlush (FRAME_X_DISPLAY (f));
12359 UNBLOCK_INPUT;
12360 #else /* not USE_X_TOOLKIT */
12362 /* Make sure the X server knows where the window should be positioned,
12363 in case the user deiconifies with the window manager. */
12364 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
12365 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
12367 /* Since we don't know which revision of X we're running, we'll use both
12368 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
12370 /* X11R4: send a ClientMessage to the window manager using the
12371 WM_CHANGE_STATE type. */
12373 XEvent message;
12375 message.xclient.window = FRAME_X_WINDOW (f);
12376 message.xclient.type = ClientMessage;
12377 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
12378 message.xclient.format = 32;
12379 message.xclient.data.l[0] = IconicState;
12381 if (! XSendEvent (FRAME_X_DISPLAY (f),
12382 DefaultRootWindow (FRAME_X_DISPLAY (f)),
12383 False,
12384 SubstructureRedirectMask | SubstructureNotifyMask,
12385 &message))
12387 UNBLOCK_INPUT_RESIGNAL;
12388 error ("Can't notify window manager of iconification");
12392 /* X11R3: set the initial_state field of the window manager hints to
12393 IconicState. */
12394 x_wm_set_window_state (f, IconicState);
12396 if (!FRAME_VISIBLE_P (f))
12398 /* If the frame was withdrawn, before, we must map it. */
12399 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12402 f->async_iconified = 1;
12403 f->async_visible = 0;
12405 XFlush (FRAME_X_DISPLAY (f));
12406 UNBLOCK_INPUT;
12407 #endif /* not USE_X_TOOLKIT */
12411 /* Free X resources of frame F. */
12413 void
12414 x_free_frame_resources (f)
12415 struct frame *f;
12417 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12419 BLOCK_INPUT;
12421 /* If a display connection is dead, don't try sending more
12422 commands to the X server. */
12423 if (dpyinfo->display)
12425 if (f->output_data.x->icon_desc)
12426 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
12428 #ifdef HAVE_X_I18N
12429 if (FRAME_XIC (f))
12430 free_frame_xic (f);
12431 #endif
12433 if (FRAME_X_WINDOW (f))
12434 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
12436 #ifdef USE_X_TOOLKIT
12437 if (f->output_data.x->widget)
12438 XtDestroyWidget (f->output_data.x->widget);
12439 free_frame_menubar (f);
12440 #endif /* USE_X_TOOLKIT */
12442 unload_color (f, f->output_data.x->foreground_pixel);
12443 unload_color (f, f->output_data.x->background_pixel);
12444 unload_color (f, f->output_data.x->cursor_pixel);
12445 unload_color (f, f->output_data.x->cursor_foreground_pixel);
12446 unload_color (f, f->output_data.x->border_pixel);
12447 unload_color (f, f->output_data.x->mouse_pixel);
12449 if (f->output_data.x->scroll_bar_background_pixel != -1)
12450 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
12451 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
12452 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
12453 if (f->output_data.x->white_relief.allocated_p)
12454 unload_color (f, f->output_data.x->white_relief.pixel);
12455 if (f->output_data.x->black_relief.allocated_p)
12456 unload_color (f, f->output_data.x->black_relief.pixel);
12458 if (FRAME_FACE_CACHE (f))
12459 free_frame_faces (f);
12461 x_free_gcs (f);
12462 XFlush (FRAME_X_DISPLAY (f));
12465 if (f->output_data.x->saved_menu_event)
12466 xfree (f->output_data.x->saved_menu_event);
12468 xfree (f->output_data.x);
12469 f->output_data.x = NULL;
12471 if (f == dpyinfo->x_focus_frame)
12472 dpyinfo->x_focus_frame = 0;
12473 if (f == dpyinfo->x_focus_event_frame)
12474 dpyinfo->x_focus_event_frame = 0;
12475 if (f == dpyinfo->x_highlight_frame)
12476 dpyinfo->x_highlight_frame = 0;
12478 if (f == dpyinfo->mouse_face_mouse_frame)
12480 dpyinfo->mouse_face_beg_row
12481 = dpyinfo->mouse_face_beg_col = -1;
12482 dpyinfo->mouse_face_end_row
12483 = dpyinfo->mouse_face_end_col = -1;
12484 dpyinfo->mouse_face_window = Qnil;
12485 dpyinfo->mouse_face_deferred_gc = 0;
12486 dpyinfo->mouse_face_mouse_frame = 0;
12489 UNBLOCK_INPUT;
12493 /* Destroy the X window of frame F. */
12495 void
12496 x_destroy_window (f)
12497 struct frame *f;
12499 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12501 /* If a display connection is dead, don't try sending more
12502 commands to the X server. */
12503 if (dpyinfo->display != 0)
12504 x_free_frame_resources (f);
12506 dpyinfo->reference_count--;
12510 /* Setting window manager hints. */
12512 /* Set the normal size hints for the window manager, for frame F.
12513 FLAGS is the flags word to use--or 0 meaning preserve the flags
12514 that the window now has.
12515 If USER_POSITION is nonzero, we set the USPosition
12516 flag (this is useful when FLAGS is 0). */
12518 void
12519 x_wm_set_size_hint (f, flags, user_position)
12520 struct frame *f;
12521 long flags;
12522 int user_position;
12524 XSizeHints size_hints;
12526 #ifdef USE_X_TOOLKIT
12527 Arg al[2];
12528 int ac = 0;
12529 Dimension widget_width, widget_height;
12530 Window window = XtWindow (f->output_data.x->widget);
12531 #else /* not USE_X_TOOLKIT */
12532 Window window = FRAME_X_WINDOW (f);
12533 #endif /* not USE_X_TOOLKIT */
12535 /* Setting PMaxSize caused various problems. */
12536 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
12538 size_hints.x = f->output_data.x->left_pos;
12539 size_hints.y = f->output_data.x->top_pos;
12541 #ifdef USE_X_TOOLKIT
12542 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
12543 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
12544 XtGetValues (f->output_data.x->widget, al, ac);
12545 size_hints.height = widget_height;
12546 size_hints.width = widget_width;
12547 #else /* not USE_X_TOOLKIT */
12548 size_hints.height = PIXEL_HEIGHT (f);
12549 size_hints.width = PIXEL_WIDTH (f);
12550 #endif /* not USE_X_TOOLKIT */
12552 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
12553 size_hints.height_inc = f->output_data.x->line_height;
12554 size_hints.max_width
12555 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
12556 size_hints.max_height
12557 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
12559 /* Calculate the base and minimum sizes.
12561 (When we use the X toolkit, we don't do it here.
12562 Instead we copy the values that the widgets are using, below.) */
12563 #ifndef USE_X_TOOLKIT
12565 int base_width, base_height;
12566 int min_rows = 0, min_cols = 0;
12568 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
12569 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
12571 check_frame_size (f, &min_rows, &min_cols);
12573 /* The window manager uses the base width hints to calculate the
12574 current number of rows and columns in the frame while
12575 resizing; min_width and min_height aren't useful for this
12576 purpose, since they might not give the dimensions for a
12577 zero-row, zero-column frame.
12579 We use the base_width and base_height members if we have
12580 them; otherwise, we set the min_width and min_height members
12581 to the size for a zero x zero frame. */
12583 #ifdef HAVE_X11R4
12584 size_hints.flags |= PBaseSize;
12585 size_hints.base_width = base_width;
12586 size_hints.base_height = base_height;
12587 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
12588 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
12589 #else
12590 size_hints.min_width = base_width;
12591 size_hints.min_height = base_height;
12592 #endif
12595 /* If we don't need the old flags, we don't need the old hint at all. */
12596 if (flags)
12598 size_hints.flags |= flags;
12599 goto no_read;
12601 #endif /* not USE_X_TOOLKIT */
12604 XSizeHints hints; /* Sometimes I hate X Windows... */
12605 long supplied_return;
12606 int value;
12608 #ifdef HAVE_X11R4
12609 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
12610 &supplied_return);
12611 #else
12612 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
12613 #endif
12615 #ifdef USE_X_TOOLKIT
12616 size_hints.base_height = hints.base_height;
12617 size_hints.base_width = hints.base_width;
12618 size_hints.min_height = hints.min_height;
12619 size_hints.min_width = hints.min_width;
12620 #endif
12622 if (flags)
12623 size_hints.flags |= flags;
12624 else
12626 if (value == 0)
12627 hints.flags = 0;
12628 if (hints.flags & PSize)
12629 size_hints.flags |= PSize;
12630 if (hints.flags & PPosition)
12631 size_hints.flags |= PPosition;
12632 if (hints.flags & USPosition)
12633 size_hints.flags |= USPosition;
12634 if (hints.flags & USSize)
12635 size_hints.flags |= USSize;
12639 #ifndef USE_X_TOOLKIT
12640 no_read:
12641 #endif
12643 #ifdef PWinGravity
12644 size_hints.win_gravity = f->output_data.x->win_gravity;
12645 size_hints.flags |= PWinGravity;
12647 if (user_position)
12649 size_hints.flags &= ~ PPosition;
12650 size_hints.flags |= USPosition;
12652 #endif /* PWinGravity */
12654 #ifdef HAVE_X11R4
12655 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12656 #else
12657 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
12658 #endif
12661 /* Used for IconicState or NormalState */
12663 void
12664 x_wm_set_window_state (f, state)
12665 struct frame *f;
12666 int state;
12668 #ifdef USE_X_TOOLKIT
12669 Arg al[1];
12671 XtSetArg (al[0], XtNinitialState, state);
12672 XtSetValues (f->output_data.x->widget, al, 1);
12673 #else /* not USE_X_TOOLKIT */
12674 Window window = FRAME_X_WINDOW (f);
12676 f->output_data.x->wm_hints.flags |= StateHint;
12677 f->output_data.x->wm_hints.initial_state = state;
12679 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12680 #endif /* not USE_X_TOOLKIT */
12683 void
12684 x_wm_set_icon_pixmap (f, pixmap_id)
12685 struct frame *f;
12686 int pixmap_id;
12688 Pixmap icon_pixmap;
12690 #ifndef USE_X_TOOLKIT
12691 Window window = FRAME_X_WINDOW (f);
12692 #endif
12694 if (pixmap_id > 0)
12696 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
12697 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
12699 else
12701 /* It seems there is no way to turn off use of an icon pixmap.
12702 The following line does it, only if no icon has yet been created,
12703 for some window managers. But with mwm it crashes.
12704 Some people say it should clear the IconPixmapHint bit in this case,
12705 but that doesn't work, and the X consortium said it isn't the
12706 right thing at all. Since there is no way to win,
12707 best to explicitly give up. */
12708 #if 0
12709 f->output_data.x->wm_hints.icon_pixmap = None;
12710 #else
12711 return;
12712 #endif
12715 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
12718 Arg al[1];
12719 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
12720 XtSetValues (f->output_data.x->widget, al, 1);
12723 #else /* not USE_X_TOOLKIT */
12725 f->output_data.x->wm_hints.flags |= IconPixmapHint;
12726 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12728 #endif /* not USE_X_TOOLKIT */
12731 void
12732 x_wm_set_icon_position (f, icon_x, icon_y)
12733 struct frame *f;
12734 int icon_x, icon_y;
12736 #ifdef USE_X_TOOLKIT
12737 Window window = XtWindow (f->output_data.x->widget);
12738 #else
12739 Window window = FRAME_X_WINDOW (f);
12740 #endif
12742 f->output_data.x->wm_hints.flags |= IconPositionHint;
12743 f->output_data.x->wm_hints.icon_x = icon_x;
12744 f->output_data.x->wm_hints.icon_y = icon_y;
12746 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
12750 /***********************************************************************
12751 Fonts
12752 ***********************************************************************/
12754 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
12756 struct font_info *
12757 x_get_font_info (f, font_idx)
12758 FRAME_PTR f;
12759 int font_idx;
12761 return (FRAME_X_FONT_TABLE (f) + font_idx);
12765 /* Return a list of names of available fonts matching PATTERN on frame
12766 F. If SIZE is not 0, it is the size (maximum bound width) of fonts
12767 to be listed. Frame F NULL means we have not yet created any
12768 frame on X, and consult the first display in x_display_list.
12769 MAXNAMES sets a limit on how many fonts to match. */
12771 Lisp_Object
12772 x_list_fonts (f, pattern, size, maxnames)
12773 FRAME_PTR f;
12774 Lisp_Object pattern;
12775 int size;
12776 int maxnames;
12778 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
12779 Lisp_Object tem, second_best;
12780 Display *dpy = f != NULL ? FRAME_X_DISPLAY (f) : x_display_list->display;
12781 int try_XLoadQueryFont = 0;
12782 int count;
12784 patterns = Fassoc (pattern, Valternate_fontname_alist);
12785 if (NILP (patterns))
12786 patterns = Fcons (pattern, Qnil);
12788 if (maxnames == 1 && !size)
12789 /* We can return any single font matching PATTERN. */
12790 try_XLoadQueryFont = 1;
12792 for (; CONSP (patterns); patterns = XCDR (patterns))
12794 int num_fonts;
12795 char **names = NULL;
12797 pattern = XCAR (patterns);
12798 /* See if we cached the result for this particular query.
12799 The cache is an alist of the form:
12800 (((PATTERN . MAXNAMES) (FONTNAME . WIDTH) ...) ...)
12802 if (f && (tem = XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element),
12803 key = Fcons (pattern, make_number (maxnames)),
12804 !NILP (list = Fassoc (key, tem))))
12806 list = Fcdr_safe (list);
12807 /* We have a cashed list. Don't have to get the list again. */
12808 goto label_cached;
12811 /* At first, put PATTERN in the cache. */
12813 BLOCK_INPUT;
12814 count = x_catch_errors (dpy);
12816 if (try_XLoadQueryFont)
12818 XFontStruct *font;
12819 unsigned long value;
12821 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
12822 if (x_had_errors_p (dpy))
12824 /* This error is perhaps due to insufficient memory on X
12825 server. Let's just ignore it. */
12826 font = NULL;
12827 x_clear_errors (dpy);
12830 if (font
12831 && XGetFontProperty (font, XA_FONT, &value))
12833 char *name = (char *) XGetAtomName (dpy, (Atom) value);
12834 int len = strlen (name);
12835 char *tmp;
12837 /* If DXPC (a Differential X Protocol Compressor)
12838 Ver.3.7 is running, XGetAtomName will return null
12839 string. We must avoid such a name. */
12840 if (len == 0)
12841 try_XLoadQueryFont = 0;
12842 else
12844 num_fonts = 1;
12845 names = (char **) alloca (sizeof (char *));
12846 /* Some systems only allow alloca assigned to a
12847 simple var. */
12848 tmp = (char *) alloca (len + 1); names[0] = tmp;
12849 bcopy (name, names[0], len + 1);
12850 XFree (name);
12853 else
12854 try_XLoadQueryFont = 0;
12856 if (font)
12857 XFreeFont (dpy, font);
12860 if (!try_XLoadQueryFont)
12862 /* We try at least 10 fonts because XListFonts will return
12863 auto-scaled fonts at the head. */
12864 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
12865 &num_fonts);
12866 if (x_had_errors_p (dpy))
12868 /* This error is perhaps due to insufficient memory on X
12869 server. Let's just ignore it. */
12870 names = NULL;
12871 x_clear_errors (dpy);
12875 x_uncatch_errors (dpy, count);
12876 UNBLOCK_INPUT;
12878 if (names)
12880 int i;
12882 /* Make a list of all the fonts we got back.
12883 Store that in the font cache for the display. */
12884 for (i = 0; i < num_fonts; i++)
12886 int width = 0;
12887 char *p = names[i];
12888 int average_width = -1, dashes = 0;
12890 /* Count the number of dashes in NAMES[I]. If there are
12891 14 dashes, and the field value following 12th dash
12892 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
12893 is usually too ugly to be used for editing. Let's
12894 ignore it. */
12895 while (*p)
12896 if (*p++ == '-')
12898 dashes++;
12899 if (dashes == 7) /* PIXEL_SIZE field */
12900 width = atoi (p);
12901 else if (dashes == 12) /* AVERAGE_WIDTH field */
12902 average_width = atoi (p);
12904 if (dashes < 14 || average_width != 0)
12906 tem = build_string (names[i]);
12907 if (NILP (Fassoc (tem, list)))
12909 if (STRINGP (Vx_pixel_size_width_font_regexp)
12910 && ((fast_c_string_match_ignore_case
12911 (Vx_pixel_size_width_font_regexp, names[i]))
12912 >= 0))
12913 /* We can set the value of PIXEL_SIZE to the
12914 width of this font. */
12915 list = Fcons (Fcons (tem, make_number (width)), list);
12916 else
12917 /* For the moment, width is not known. */
12918 list = Fcons (Fcons (tem, Qnil), list);
12922 if (!try_XLoadQueryFont)
12923 XFreeFontNames (names);
12926 /* Now store the result in the cache. */
12927 if (f != NULL)
12928 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element)
12929 = Fcons (Fcons (key, list),
12930 XCDR (FRAME_X_DISPLAY_INFO (f)->name_list_element));
12932 label_cached:
12933 if (NILP (list)) continue; /* Try the remaining alternatives. */
12935 newlist = second_best = Qnil;
12936 /* Make a list of the fonts that have the right width. */
12937 for (; CONSP (list); list = XCDR (list))
12939 int found_size;
12941 tem = XCAR (list);
12943 if (!CONSP (tem) || NILP (XCAR (tem)))
12944 continue;
12945 if (!size)
12947 newlist = Fcons (XCAR (tem), newlist);
12948 continue;
12951 if (!INTEGERP (XCDR (tem)))
12953 /* Since we have not yet known the size of this font, we
12954 must try slow function call XLoadQueryFont. */
12955 XFontStruct *thisinfo;
12957 BLOCK_INPUT;
12958 count = x_catch_errors (dpy);
12959 thisinfo = XLoadQueryFont (dpy,
12960 XSTRING (XCAR (tem))->data);
12961 if (x_had_errors_p (dpy))
12963 /* This error is perhaps due to insufficient memory on X
12964 server. Let's just ignore it. */
12965 thisinfo = NULL;
12966 x_clear_errors (dpy);
12968 x_uncatch_errors (dpy, count);
12969 UNBLOCK_INPUT;
12971 if (thisinfo)
12973 XCDR (tem)
12974 = (thisinfo->min_bounds.width == 0
12975 ? make_number (0)
12976 : make_number (thisinfo->max_bounds.width));
12977 XFreeFont (dpy, thisinfo);
12979 else
12980 /* For unknown reason, the previous call of XListFont had
12981 returned a font which can't be opened. Record the size
12982 as 0 not to try to open it again. */
12983 XCDR (tem) = make_number (0);
12986 found_size = XINT (XCDR (tem));
12987 if (found_size == size)
12988 newlist = Fcons (XCAR (tem), newlist);
12989 else if (found_size > 0)
12991 if (NILP (second_best))
12992 second_best = tem;
12993 else if (found_size < size)
12995 if (XINT (XCDR (second_best)) > size
12996 || XINT (XCDR (second_best)) < found_size)
12997 second_best = tem;
12999 else
13001 if (XINT (XCDR (second_best)) > size
13002 && XINT (XCDR (second_best)) > found_size)
13003 second_best = tem;
13007 if (!NILP (newlist))
13008 break;
13009 else if (!NILP (second_best))
13011 newlist = Fcons (XCAR (second_best), Qnil);
13012 break;
13016 return newlist;
13020 #if GLYPH_DEBUG
13022 /* Check that FONT is valid on frame F. It is if it can be found in F's
13023 font table. */
13025 static void
13026 x_check_font (f, font)
13027 struct frame *f;
13028 XFontStruct *font;
13030 int i;
13031 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13033 xassert (font != NULL);
13035 for (i = 0; i < dpyinfo->n_fonts; i++)
13036 if (dpyinfo->font_table[i].name
13037 && font == dpyinfo->font_table[i].font)
13038 break;
13040 xassert (i < dpyinfo->n_fonts);
13043 #endif /* GLYPH_DEBUG != 0 */
13045 /* Set *W to the minimum width, *H to the minimum font height of FONT.
13046 Note: There are (broken) X fonts out there with invalid XFontStruct
13047 min_bounds contents. For example, handa@etl.go.jp reports that
13048 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
13049 have font->min_bounds.width == 0. */
13051 static INLINE void
13052 x_font_min_bounds (font, w, h)
13053 XFontStruct *font;
13054 int *w, *h;
13056 *h = FONT_HEIGHT (font);
13057 *w = font->min_bounds.width;
13059 /* Try to handle the case where FONT->min_bounds has invalid
13060 contents. Since the only font known to have invalid min_bounds
13061 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
13062 if (*w <= 0)
13063 *w = font->max_bounds.width;
13067 /* Compute the smallest character width and smallest font height over
13068 all fonts available on frame F. Set the members smallest_char_width
13069 and smallest_font_height in F's x_display_info structure to
13070 the values computed. Value is non-zero if smallest_font_height or
13071 smallest_char_width become smaller than they were before. */
13073 static int
13074 x_compute_min_glyph_bounds (f)
13075 struct frame *f;
13077 int i;
13078 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13079 XFontStruct *font;
13080 int old_width = dpyinfo->smallest_char_width;
13081 int old_height = dpyinfo->smallest_font_height;
13083 dpyinfo->smallest_font_height = 100000;
13084 dpyinfo->smallest_char_width = 100000;
13086 for (i = 0; i < dpyinfo->n_fonts; ++i)
13087 if (dpyinfo->font_table[i].name)
13089 struct font_info *fontp = dpyinfo->font_table + i;
13090 int w, h;
13092 font = (XFontStruct *) fontp->font;
13093 xassert (font != (XFontStruct *) ~0);
13094 x_font_min_bounds (font, &w, &h);
13096 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
13097 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
13100 xassert (dpyinfo->smallest_char_width > 0
13101 && dpyinfo->smallest_font_height > 0);
13103 return (dpyinfo->n_fonts == 1
13104 || dpyinfo->smallest_char_width < old_width
13105 || dpyinfo->smallest_font_height < old_height);
13109 /* Load font named FONTNAME of the size SIZE for frame F, and return a
13110 pointer to the structure font_info while allocating it dynamically.
13111 If SIZE is 0, load any size of font.
13112 If loading is failed, return NULL. */
13114 struct font_info *
13115 x_load_font (f, fontname, size)
13116 struct frame *f;
13117 register char *fontname;
13118 int size;
13120 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13121 Lisp_Object font_names;
13122 int count;
13124 /* Get a list of all the fonts that match this name. Once we
13125 have a list of matching fonts, we compare them against the fonts
13126 we already have by comparing names. */
13127 font_names = x_list_fonts (f, build_string (fontname), size, 1);
13129 if (!NILP (font_names))
13131 Lisp_Object tail;
13132 int i;
13134 for (i = 0; i < dpyinfo->n_fonts; i++)
13135 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
13136 if (dpyinfo->font_table[i].name
13137 && (!strcmp (dpyinfo->font_table[i].name,
13138 XSTRING (XCAR (tail))->data)
13139 || !strcmp (dpyinfo->font_table[i].full_name,
13140 XSTRING (XCAR (tail))->data)))
13141 return (dpyinfo->font_table + i);
13144 /* Load the font and add it to the table. */
13146 char *full_name;
13147 XFontStruct *font;
13148 struct font_info *fontp;
13149 unsigned long value;
13150 int i;
13152 /* If we have found fonts by x_list_font, load one of them. If
13153 not, we still try to load a font by the name given as FONTNAME
13154 because XListFonts (called in x_list_font) of some X server has
13155 a bug of not finding a font even if the font surely exists and
13156 is loadable by XLoadQueryFont. */
13157 if (size > 0 && !NILP (font_names))
13158 fontname = (char *) XSTRING (XCAR (font_names))->data;
13160 BLOCK_INPUT;
13161 count = x_catch_errors (FRAME_X_DISPLAY (f));
13162 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
13163 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
13165 /* This error is perhaps due to insufficient memory on X
13166 server. Let's just ignore it. */
13167 font = NULL;
13168 x_clear_errors (FRAME_X_DISPLAY (f));
13170 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
13171 UNBLOCK_INPUT;
13172 if (!font)
13173 return NULL;
13175 /* Find a free slot in the font table. */
13176 for (i = 0; i < dpyinfo->n_fonts; ++i)
13177 if (dpyinfo->font_table[i].name == NULL)
13178 break;
13180 /* If no free slot found, maybe enlarge the font table. */
13181 if (i == dpyinfo->n_fonts
13182 && dpyinfo->n_fonts == dpyinfo->font_table_size)
13184 int sz;
13185 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
13186 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
13187 dpyinfo->font_table
13188 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
13191 fontp = dpyinfo->font_table + i;
13192 if (i == dpyinfo->n_fonts)
13193 ++dpyinfo->n_fonts;
13195 /* Now fill in the slots of *FONTP. */
13196 BLOCK_INPUT;
13197 fontp->font = font;
13198 fontp->font_idx = i;
13199 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
13200 bcopy (fontname, fontp->name, strlen (fontname) + 1);
13202 /* Try to get the full name of FONT. Put it in FULL_NAME. */
13203 full_name = 0;
13204 if (XGetFontProperty (font, XA_FONT, &value))
13206 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
13207 char *p = name;
13208 int dashes = 0;
13210 /* Count the number of dashes in the "full name".
13211 If it is too few, this isn't really the font's full name,
13212 so don't use it.
13213 In X11R4, the fonts did not come with their canonical names
13214 stored in them. */
13215 while (*p)
13217 if (*p == '-')
13218 dashes++;
13219 p++;
13222 if (dashes >= 13)
13224 full_name = (char *) xmalloc (p - name + 1);
13225 bcopy (name, full_name, p - name + 1);
13228 XFree (name);
13231 if (full_name != 0)
13232 fontp->full_name = full_name;
13233 else
13234 fontp->full_name = fontp->name;
13236 fontp->size = font->max_bounds.width;
13237 fontp->height = FONT_HEIGHT (font);
13239 if (NILP (font_names))
13241 /* We come here because of a bug of XListFonts mentioned at
13242 the head of this block. Let's store this information in
13243 the cache for x_list_fonts. */
13244 Lisp_Object lispy_name = build_string (fontname);
13245 Lisp_Object lispy_full_name = build_string (fontp->full_name);
13247 XCDR (dpyinfo->name_list_element)
13248 = Fcons (Fcons (Fcons (lispy_name, make_number (256)),
13249 Fcons (Fcons (lispy_full_name,
13250 make_number (fontp->size)),
13251 Qnil)),
13252 XCDR (dpyinfo->name_list_element));
13253 if (full_name)
13254 XCDR (dpyinfo->name_list_element)
13255 = Fcons (Fcons (Fcons (lispy_full_name, make_number (256)),
13256 Fcons (Fcons (lispy_full_name,
13257 make_number (fontp->size)),
13258 Qnil)),
13259 XCDR (dpyinfo->name_list_element));
13262 /* The slot `encoding' specifies how to map a character
13263 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
13264 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
13265 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
13266 2:0xA020..0xFF7F). For the moment, we don't know which charset
13267 uses this font. So, we set information in fontp->encoding[1]
13268 which is never used by any charset. If mapping can't be
13269 decided, set FONT_ENCODING_NOT_DECIDED. */
13270 fontp->encoding[1]
13271 = (font->max_byte1 == 0
13272 /* 1-byte font */
13273 ? (font->min_char_or_byte2 < 0x80
13274 ? (font->max_char_or_byte2 < 0x80
13275 ? 0 /* 0x20..0x7F */
13276 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
13277 : 1) /* 0xA0..0xFF */
13278 /* 2-byte font */
13279 : (font->min_byte1 < 0x80
13280 ? (font->max_byte1 < 0x80
13281 ? (font->min_char_or_byte2 < 0x80
13282 ? (font->max_char_or_byte2 < 0x80
13283 ? 0 /* 0x2020..0x7F7F */
13284 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
13285 : 3) /* 0x20A0..0x7FFF */
13286 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
13287 : (font->min_char_or_byte2 < 0x80
13288 ? (font->max_char_or_byte2 < 0x80
13289 ? 2 /* 0xA020..0xFF7F */
13290 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
13291 : 1))); /* 0xA0A0..0xFFFF */
13293 fontp->baseline_offset
13294 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
13295 ? (long) value : 0);
13296 fontp->relative_compose
13297 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
13298 ? (long) value : 0);
13299 fontp->default_ascent
13300 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
13301 ? (long) value : 0);
13303 /* Set global flag fonts_changed_p to non-zero if the font loaded
13304 has a character with a smaller width than any other character
13305 before, or if the font loaded has a smalle>r height than any
13306 other font loaded before. If this happens, it will make a
13307 glyph matrix reallocation necessary. */
13308 fonts_changed_p = x_compute_min_glyph_bounds (f);
13309 UNBLOCK_INPUT;
13310 return fontp;
13315 /* Return a pointer to struct font_info of a font named FONTNAME for
13316 frame F. If no such font is loaded, return NULL. */
13318 struct font_info *
13319 x_query_font (f, fontname)
13320 struct frame *f;
13321 register char *fontname;
13323 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13324 int i;
13326 for (i = 0; i < dpyinfo->n_fonts; i++)
13327 if (dpyinfo->font_table[i].name
13328 && (!strcmp (dpyinfo->font_table[i].name, fontname)
13329 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
13330 return (dpyinfo->font_table + i);
13331 return NULL;
13335 /* Find a CCL program for a font specified by FONTP, and set the member
13336 `encoder' of the structure. */
13338 void
13339 x_find_ccl_program (fontp)
13340 struct font_info *fontp;
13342 Lisp_Object list, elt;
13344 elt = Qnil;
13345 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
13347 elt = XCAR (list);
13348 if (CONSP (elt)
13349 && STRINGP (XCAR (elt))
13350 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
13351 >= 0)
13352 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
13353 >= 0)))
13354 break;
13357 if (! NILP (list))
13359 struct ccl_program *ccl
13360 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
13362 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
13363 xfree (ccl);
13364 else
13365 fontp->font_encoder = ccl;
13371 /***********************************************************************
13372 Initialization
13373 ***********************************************************************/
13375 #ifdef USE_X_TOOLKIT
13376 static XrmOptionDescRec emacs_options[] = {
13377 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
13378 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
13380 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
13381 XrmoptionSepArg, NULL},
13382 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
13384 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13385 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13386 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
13387 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13388 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
13389 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
13390 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
13392 #endif /* USE_X_TOOLKIT */
13394 static int x_initialized;
13396 #ifdef MULTI_KBOARD
13397 /* Test whether two display-name strings agree up to the dot that separates
13398 the screen number from the server number. */
13399 static int
13400 same_x_server (name1, name2)
13401 char *name1, *name2;
13403 int seen_colon = 0;
13404 unsigned char *system_name = XSTRING (Vsystem_name)->data;
13405 int system_name_length = strlen (system_name);
13406 int length_until_period = 0;
13408 while (system_name[length_until_period] != 0
13409 && system_name[length_until_period] != '.')
13410 length_until_period++;
13412 /* Treat `unix' like an empty host name. */
13413 if (! strncmp (name1, "unix:", 5))
13414 name1 += 4;
13415 if (! strncmp (name2, "unix:", 5))
13416 name2 += 4;
13417 /* Treat this host's name like an empty host name. */
13418 if (! strncmp (name1, system_name, system_name_length)
13419 && name1[system_name_length] == ':')
13420 name1 += system_name_length;
13421 if (! strncmp (name2, system_name, system_name_length)
13422 && name2[system_name_length] == ':')
13423 name2 += system_name_length;
13424 /* Treat this host's domainless name like an empty host name. */
13425 if (! strncmp (name1, system_name, length_until_period)
13426 && name1[length_until_period] == ':')
13427 name1 += length_until_period;
13428 if (! strncmp (name2, system_name, length_until_period)
13429 && name2[length_until_period] == ':')
13430 name2 += length_until_period;
13432 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
13434 if (*name1 == ':')
13435 seen_colon++;
13436 if (seen_colon && *name1 == '.')
13437 return 1;
13439 return (seen_colon
13440 && (*name1 == '.' || *name1 == '\0')
13441 && (*name2 == '.' || *name2 == '\0'));
13443 #endif
13445 struct x_display_info *
13446 x_term_init (display_name, xrm_option, resource_name)
13447 Lisp_Object display_name;
13448 char *xrm_option;
13449 char *resource_name;
13451 int connection;
13452 Display *dpy;
13453 struct x_display_info *dpyinfo;
13454 XrmDatabase xrdb;
13456 BLOCK_INPUT;
13458 if (!x_initialized)
13460 x_initialize ();
13461 x_initialized = 1;
13464 #ifdef USE_X_TOOLKIT
13465 /* weiner@footloose.sps.mot.com reports that this causes
13466 errors with X11R5:
13467 X protocol error: BadAtom (invalid Atom parameter)
13468 on protocol request 18skiloaf.
13469 So let's not use it until R6. */
13470 #ifdef HAVE_X11XTR6
13471 XtSetLanguageProc (NULL, NULL, NULL);
13472 #endif
13475 int argc = 0;
13476 char *argv[3];
13478 argv[0] = "";
13479 argc = 1;
13480 if (xrm_option)
13482 argv[argc++] = "-xrm";
13483 argv[argc++] = xrm_option;
13485 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
13486 resource_name, EMACS_CLASS,
13487 emacs_options, XtNumber (emacs_options),
13488 &argc, argv);
13490 #ifdef HAVE_X11XTR6
13491 /* I think this is to compensate for XtSetLanguageProc. */
13492 fixup_locale ();
13493 #endif
13496 #else /* not USE_X_TOOLKIT */
13497 #ifdef HAVE_X11R5
13498 XSetLocaleModifiers ("");
13499 #endif
13500 dpy = XOpenDisplay (XSTRING (display_name)->data);
13501 #endif /* not USE_X_TOOLKIT */
13503 /* Detect failure. */
13504 if (dpy == 0)
13506 UNBLOCK_INPUT;
13507 return 0;
13510 /* We have definitely succeeded. Record the new connection. */
13512 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
13513 bzero (dpyinfo, sizeof *dpyinfo);
13515 #ifdef MULTI_KBOARD
13517 struct x_display_info *share;
13518 Lisp_Object tail;
13520 for (share = x_display_list, tail = x_display_name_list; share;
13521 share = share->next, tail = XCDR (tail))
13522 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
13523 XSTRING (display_name)->data))
13524 break;
13525 if (share)
13526 dpyinfo->kboard = share->kboard;
13527 else
13529 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
13530 init_kboard (dpyinfo->kboard);
13531 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
13533 char *vendor = ServerVendor (dpy);
13534 UNBLOCK_INPUT;
13535 dpyinfo->kboard->Vsystem_key_alist
13536 = call1 (Qvendor_specific_keysyms,
13537 build_string (vendor ? vendor : ""));
13538 BLOCK_INPUT;
13541 dpyinfo->kboard->next_kboard = all_kboards;
13542 all_kboards = dpyinfo->kboard;
13543 /* Don't let the initial kboard remain current longer than necessary.
13544 That would cause problems if a file loaded on startup tries to
13545 prompt in the mini-buffer. */
13546 if (current_kboard == initial_kboard)
13547 current_kboard = dpyinfo->kboard;
13549 dpyinfo->kboard->reference_count++;
13551 #endif
13553 /* Put this display on the chain. */
13554 dpyinfo->next = x_display_list;
13555 x_display_list = dpyinfo;
13557 /* Put it on x_display_name_list as well, to keep them parallel. */
13558 x_display_name_list = Fcons (Fcons (display_name, Qnil),
13559 x_display_name_list);
13560 dpyinfo->name_list_element = XCAR (x_display_name_list);
13562 dpyinfo->display = dpy;
13564 #if 0
13565 XSetAfterFunction (x_current_display, x_trace_wire);
13566 #endif /* ! 0 */
13568 dpyinfo->x_id_name
13569 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
13570 + STRING_BYTES (XSTRING (Vsystem_name))
13571 + 2);
13572 sprintf (dpyinfo->x_id_name, "%s@%s",
13573 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
13575 /* Figure out which modifier bits mean what. */
13576 x_find_modifier_meanings (dpyinfo);
13578 /* Get the scroll bar cursor. */
13579 dpyinfo->vertical_scroll_bar_cursor
13580 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
13582 xrdb = x_load_resources (dpyinfo->display, xrm_option,
13583 resource_name, EMACS_CLASS);
13584 #ifdef HAVE_XRMSETDATABASE
13585 XrmSetDatabase (dpyinfo->display, xrdb);
13586 #else
13587 dpyinfo->display->db = xrdb;
13588 #endif
13589 /* Put the rdb where we can find it in a way that works on
13590 all versions. */
13591 dpyinfo->xrdb = xrdb;
13593 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
13594 DefaultScreen (dpyinfo->display));
13595 select_visual (dpyinfo);
13596 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
13597 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
13598 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
13599 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
13600 dpyinfo->grabbed = 0;
13601 dpyinfo->reference_count = 0;
13602 dpyinfo->icon_bitmap_id = -1;
13603 dpyinfo->font_table = NULL;
13604 dpyinfo->n_fonts = 0;
13605 dpyinfo->font_table_size = 0;
13606 dpyinfo->bitmaps = 0;
13607 dpyinfo->bitmaps_size = 0;
13608 dpyinfo->bitmaps_last = 0;
13609 dpyinfo->scratch_cursor_gc = 0;
13610 dpyinfo->mouse_face_mouse_frame = 0;
13611 dpyinfo->mouse_face_deferred_gc = 0;
13612 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
13613 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
13614 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
13615 dpyinfo->mouse_face_window = Qnil;
13616 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
13617 dpyinfo->mouse_face_defer = 0;
13618 dpyinfo->x_focus_frame = 0;
13619 dpyinfo->x_focus_event_frame = 0;
13620 dpyinfo->x_highlight_frame = 0;
13621 dpyinfo->image_cache = make_image_cache ();
13623 /* See if a private colormap is requested. */
13624 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
13626 if (dpyinfo->visual->class == PseudoColor)
13628 Lisp_Object value;
13629 value = display_x_get_resource (dpyinfo,
13630 build_string ("privateColormap"),
13631 build_string ("PrivateColormap"),
13632 Qnil, Qnil);
13633 if (STRINGP (value)
13634 && (!strcmp (XSTRING (value)->data, "true")
13635 || !strcmp (XSTRING (value)->data, "on")))
13636 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
13639 else
13640 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
13641 dpyinfo->visual, AllocNone);
13644 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
13645 double pixels = DisplayHeight (dpyinfo->display, screen_number);
13646 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
13647 dpyinfo->resy = pixels * 25.4 / mm;
13648 pixels = DisplayWidth (dpyinfo->display, screen_number);
13649 mm = DisplayWidthMM (dpyinfo->display, screen_number);
13650 dpyinfo->resx = pixels * 25.4 / mm;
13653 dpyinfo->Xatom_wm_protocols
13654 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
13655 dpyinfo->Xatom_wm_take_focus
13656 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
13657 dpyinfo->Xatom_wm_save_yourself
13658 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
13659 dpyinfo->Xatom_wm_delete_window
13660 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
13661 dpyinfo->Xatom_wm_change_state
13662 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
13663 dpyinfo->Xatom_wm_configure_denied
13664 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
13665 dpyinfo->Xatom_wm_window_moved
13666 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
13667 dpyinfo->Xatom_editres
13668 = XInternAtom (dpyinfo->display, "Editres", False);
13669 dpyinfo->Xatom_CLIPBOARD
13670 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
13671 dpyinfo->Xatom_TIMESTAMP
13672 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
13673 dpyinfo->Xatom_TEXT
13674 = XInternAtom (dpyinfo->display, "TEXT", False);
13675 dpyinfo->Xatom_COMPOUND_TEXT
13676 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
13677 dpyinfo->Xatom_DELETE
13678 = XInternAtom (dpyinfo->display, "DELETE", False);
13679 dpyinfo->Xatom_MULTIPLE
13680 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
13681 dpyinfo->Xatom_INCR
13682 = XInternAtom (dpyinfo->display, "INCR", False);
13683 dpyinfo->Xatom_EMACS_TMP
13684 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
13685 dpyinfo->Xatom_TARGETS
13686 = XInternAtom (dpyinfo->display, "TARGETS", False);
13687 dpyinfo->Xatom_NULL
13688 = XInternAtom (dpyinfo->display, "NULL", False);
13689 dpyinfo->Xatom_ATOM_PAIR
13690 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
13691 /* For properties of font. */
13692 dpyinfo->Xatom_PIXEL_SIZE
13693 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
13694 dpyinfo->Xatom_MULE_BASELINE_OFFSET
13695 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
13696 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
13697 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
13698 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
13699 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
13701 /* Ghostscript support. */
13702 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
13703 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
13705 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
13706 False);
13708 dpyinfo->cut_buffers_initialized = 0;
13710 connection = ConnectionNumber (dpyinfo->display);
13711 dpyinfo->connection = connection;
13714 char null_bits[1];
13716 null_bits[0] = 0x00;
13718 dpyinfo->null_pixel
13719 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13720 null_bits, 1, 1, (long) 0, (long) 0,
13725 extern int gray_bitmap_width, gray_bitmap_height;
13726 extern char *gray_bitmap_bits;
13727 dpyinfo->gray
13728 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
13729 gray_bitmap_bits,
13730 gray_bitmap_width, gray_bitmap_height,
13731 (unsigned long) 1, (unsigned long) 0, 1);
13734 #ifdef HAVE_X_I18N
13735 xim_initialize (dpyinfo, resource_name);
13736 #endif
13738 #ifdef subprocesses
13739 /* This is only needed for distinguishing keyboard and process input. */
13740 if (connection != 0)
13741 add_keyboard_wait_descriptor (connection);
13742 #endif
13744 #ifndef F_SETOWN_BUG
13745 #ifdef F_SETOWN
13746 #ifdef F_SETOWN_SOCK_NEG
13747 /* stdin is a socket here */
13748 fcntl (connection, F_SETOWN, -getpid ());
13749 #else /* ! defined (F_SETOWN_SOCK_NEG) */
13750 fcntl (connection, F_SETOWN, getpid ());
13751 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
13752 #endif /* ! defined (F_SETOWN) */
13753 #endif /* F_SETOWN_BUG */
13755 #ifdef SIGIO
13756 if (interrupt_input)
13757 init_sigio (connection);
13758 #endif /* ! defined (SIGIO) */
13760 #ifdef USE_LUCID
13761 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
13762 /* Make sure that we have a valid font for dialog boxes
13763 so that Xt does not crash. */
13765 Display *dpy = dpyinfo->display;
13766 XrmValue d, fr, to;
13767 Font font;
13768 int count;
13770 d.addr = (XPointer)&dpy;
13771 d.size = sizeof (Display *);
13772 fr.addr = XtDefaultFont;
13773 fr.size = sizeof (XtDefaultFont);
13774 to.size = sizeof (Font *);
13775 to.addr = (XPointer)&font;
13776 count = x_catch_errors (dpy);
13777 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
13778 abort ();
13779 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
13780 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
13781 x_uncatch_errors (dpy, count);
13783 #endif
13784 #endif
13786 /* See if we should run in synchronous mode. This is useful
13787 for debugging X code. */
13789 Lisp_Object value;
13790 value = display_x_get_resource (dpyinfo,
13791 build_string ("synchronous"),
13792 build_string ("Synchronous"),
13793 Qnil, Qnil);
13794 if (STRINGP (value)
13795 && (!strcmp (XSTRING (value)->data, "true")
13796 || !strcmp (XSTRING (value)->data, "on")))
13797 XSynchronize (dpyinfo->display, True);
13800 UNBLOCK_INPUT;
13802 return dpyinfo;
13805 /* Get rid of display DPYINFO, assuming all frames are already gone,
13806 and without sending any more commands to the X server. */
13808 void
13809 x_delete_display (dpyinfo)
13810 struct x_display_info *dpyinfo;
13812 delete_keyboard_wait_descriptor (dpyinfo->connection);
13814 /* Discard this display from x_display_name_list and x_display_list.
13815 We can't use Fdelq because that can quit. */
13816 if (! NILP (x_display_name_list)
13817 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
13818 x_display_name_list = XCDR (x_display_name_list);
13819 else
13821 Lisp_Object tail;
13823 tail = x_display_name_list;
13824 while (CONSP (tail) && CONSP (XCDR (tail)))
13826 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
13828 XCDR (tail) = XCDR (XCDR (tail));
13829 break;
13831 tail = XCDR (tail);
13835 if (next_noop_dpyinfo == dpyinfo)
13836 next_noop_dpyinfo = dpyinfo->next;
13838 if (x_display_list == dpyinfo)
13839 x_display_list = dpyinfo->next;
13840 else
13842 struct x_display_info *tail;
13844 for (tail = x_display_list; tail; tail = tail->next)
13845 if (tail->next == dpyinfo)
13846 tail->next = tail->next->next;
13849 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
13850 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
13851 XrmDestroyDatabase (dpyinfo->xrdb);
13852 #endif
13853 #endif
13854 #ifdef MULTI_KBOARD
13855 if (--dpyinfo->kboard->reference_count == 0)
13856 delete_kboard (dpyinfo->kboard);
13857 #endif
13858 #ifdef HAVE_X_I18N
13859 if (dpyinfo->xim)
13860 xim_close_dpy (dpyinfo);
13861 #endif
13863 xfree (dpyinfo->font_table);
13864 xfree (dpyinfo->x_id_name);
13865 xfree (dpyinfo->color_cells);
13866 xfree (dpyinfo);
13870 /* Set up use of X before we make the first connection. */
13872 static struct redisplay_interface x_redisplay_interface =
13874 x_produce_glyphs,
13875 x_write_glyphs,
13876 x_insert_glyphs,
13877 x_clear_end_of_line,
13878 x_scroll_run,
13879 x_after_update_window_line,
13880 x_update_window_begin,
13881 x_update_window_end,
13882 XTcursor_to,
13883 x_flush,
13884 x_clear_mouse_face,
13885 x_get_glyph_overhangs,
13886 x_fix_overlapping_area
13889 void
13890 x_initialize ()
13892 rif = &x_redisplay_interface;
13894 clear_frame_hook = x_clear_frame;
13895 ins_del_lines_hook = x_ins_del_lines;
13896 change_line_highlight_hook = x_change_line_highlight;
13897 delete_glyphs_hook = x_delete_glyphs;
13898 ring_bell_hook = XTring_bell;
13899 reset_terminal_modes_hook = XTreset_terminal_modes;
13900 set_terminal_modes_hook = XTset_terminal_modes;
13901 update_begin_hook = x_update_begin;
13902 update_end_hook = x_update_end;
13903 set_terminal_window_hook = XTset_terminal_window;
13904 read_socket_hook = XTread_socket;
13905 frame_up_to_date_hook = XTframe_up_to_date;
13906 reassert_line_highlight_hook = XTreassert_line_highlight;
13907 mouse_position_hook = XTmouse_position;
13908 frame_rehighlight_hook = XTframe_rehighlight;
13909 frame_raise_lower_hook = XTframe_raise_lower;
13910 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
13911 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
13912 redeem_scroll_bar_hook = XTredeem_scroll_bar;
13913 judge_scroll_bars_hook = XTjudge_scroll_bars;
13914 estimate_mode_line_height_hook = x_estimate_mode_line_height;
13916 scroll_region_ok = 1; /* we'll scroll partial frames */
13917 char_ins_del_ok = 0; /* just as fast to write the line */
13918 line_ins_del_ok = 1; /* we'll just blt 'em */
13919 fast_clear_end_of_line = 1; /* X does this well */
13920 memory_below_frame = 0; /* we don't remember what scrolls
13921 off the bottom */
13922 baud_rate = 19200;
13924 x_noop_count = 0;
13925 last_tool_bar_item = -1;
13926 any_help_event_p = 0;
13928 /* Try to use interrupt input; if we can't, then start polling. */
13929 Fset_input_mode (Qt, Qnil, Qt, Qnil);
13931 #ifdef USE_X_TOOLKIT
13932 XtToolkitInitialize ();
13933 Xt_app_con = XtCreateApplicationContext ();
13934 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
13936 /* Install an asynchronous timer that processes Xt timeout events
13937 every 0.1s. This is necessary because some widget sets use
13938 timeouts internally, for example the LessTif menu bar, or the
13939 Xaw3d scroll bar. When Xt timouts aren't processed, these
13940 widgets don't behave normally. */
13942 EMACS_TIME interval;
13943 EMACS_SET_SECS_USECS (interval, 0, 100000);
13944 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
13946 #endif
13948 #ifdef USE_TOOLKIT_SCROLL_BARS
13949 xaw3d_arrow_scroll = False;
13950 xaw3d_pick_top = True;
13951 #endif
13953 /* Note that there is no real way portable across R3/R4 to get the
13954 original error handler. */
13955 XSetErrorHandler (x_error_handler);
13956 XSetIOErrorHandler (x_io_error_quitter);
13958 /* Disable Window Change signals; they are handled by X events. */
13959 #ifdef SIGWINCH
13960 signal (SIGWINCH, SIG_DFL);
13961 #endif /* ! defined (SIGWINCH) */
13963 signal (SIGPIPE, x_connection_signal);
13967 void
13968 syms_of_xterm ()
13970 staticpro (&x_error_message_string);
13971 x_error_message_string = Qnil;
13973 staticpro (&x_display_name_list);
13974 x_display_name_list = Qnil;
13976 staticpro (&last_mouse_scroll_bar);
13977 last_mouse_scroll_bar = Qnil;
13979 staticpro (&Qvendor_specific_keysyms);
13980 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
13982 staticpro (&last_mouse_press_frame);
13983 last_mouse_press_frame = Qnil;
13985 help_echo = Qnil;
13986 staticpro (&help_echo);
13987 help_echo_object = Qnil;
13988 staticpro (&help_echo_object);
13989 help_echo_window = Qnil;
13990 staticpro (&help_echo_window);
13991 previous_help_echo = Qnil;
13992 staticpro (&previous_help_echo);
13993 help_echo_pos = -1;
13995 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
13996 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
13997 For example, if a block cursor is over a tab, it will be drawn as\n\
13998 wide as that tab on the display.");
13999 x_stretch_cursor_p = 0;
14001 DEFVAR_BOOL ("x-toolkit-scroll-bars-p", &x_toolkit_scroll_bars_p,
14002 "If not nil, Emacs uses toolkit scroll bars.");
14003 #ifdef USE_TOOLKIT_SCROLL_BARS
14004 x_toolkit_scroll_bars_p = 1;
14005 #else
14006 x_toolkit_scroll_bars_p = 0;
14007 #endif
14009 staticpro (&last_mouse_motion_frame);
14010 last_mouse_motion_frame = Qnil;
14013 #endif /* not HAVE_X_WINDOWS */