Rename x_autoselect_window_p to autoselect_window_p.
[emacs.git] / src / xterm.c
blob73e987531a935afa2761d34d71d48192e18daa86
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001, 2002
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 #include "gnu.h"
80 #include "disptab.h"
81 #include "buffer.h"
82 #include "window.h"
83 #include "keyboard.h"
84 #include "intervals.h"
85 #include "process.h"
86 #include "atimer.h"
87 #include "keymap.h"
89 #ifdef USE_X_TOOLKIT
90 #include <X11/Shell.h>
91 #endif
93 #ifdef HAVE_SYS_TIME_H
94 #include <sys/time.h>
95 #endif
96 #ifdef HAVE_UNISTD_H
97 #include <unistd.h>
98 #endif
100 #ifdef USE_LUCID
101 extern int xlwmenu_window_p (Widget w, Window window);
102 extern void xlwmenu_redisplay P_ ((Widget));
103 #endif
105 #ifdef USE_X_TOOLKIT
107 extern void free_frame_menubar P_ ((struct frame *));
108 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
109 int));
111 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
112 #define HACK_EDITRES
113 extern void _XEditResCheckMessages ();
114 #endif /* not NO_EDITRES */
116 /* Include toolkit specific headers for the scroll bar widget. */
118 #ifdef USE_TOOLKIT_SCROLL_BARS
119 #if defined USE_MOTIF
120 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
121 #include <Xm/ScrollBar.h>
122 #else /* !USE_MOTIF i.e. use Xaw */
124 #ifdef HAVE_XAW3D
125 #include <X11/Xaw3d/Simple.h>
126 #include <X11/Xaw3d/Scrollbar.h>
127 #define ARROW_SCROLLBAR
128 #include <X11/Xaw3d/ScrollbarP.h>
129 #else /* !HAVE_XAW3D */
130 #include <X11/Xaw/Simple.h>
131 #include <X11/Xaw/Scrollbar.h>
132 #endif /* !HAVE_XAW3D */
133 #ifndef XtNpickTop
134 #define XtNpickTop "pickTop"
135 #endif /* !XtNpickTop */
136 #endif /* !USE_MOTIF */
137 #endif /* USE_TOOLKIT_SCROLL_BARS */
139 #endif /* USE_X_TOOLKIT */
141 #ifndef USE_X_TOOLKIT
142 #define x_any_window_to_frame x_window_to_frame
143 #define x_top_window_to_frame x_window_to_frame
144 #endif
146 #ifdef USE_X_TOOLKIT
147 #include "widget.h"
148 #ifndef XtNinitialState
149 #define XtNinitialState "initialState"
150 #endif
151 #endif
153 #define abs(x) ((x) < 0 ? -(x) : (x))
155 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
158 /* Fringe bitmaps. */
160 enum fringe_bitmap_type
162 NO_FRINGE_BITMAP,
163 LEFT_TRUNCATION_BITMAP,
164 RIGHT_TRUNCATION_BITMAP,
165 OVERLAY_ARROW_BITMAP,
166 CONTINUED_LINE_BITMAP,
167 CONTINUATION_LINE_BITMAP,
168 ZV_LINE_BITMAP
171 /* Bitmap drawn to indicate lines not displaying text if
172 `indicate-empty-lines' is non-nil. */
174 #define zv_width 8
175 #define zv_height 72
176 #define zv_period 3
177 static unsigned char zv_bits[] = {
178 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
179 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
180 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
181 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
182 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
183 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
187 /* An arrow like this: `<-'. */
189 #define left_width 8
190 #define left_height 8
191 static unsigned char left_bits[] = {
192 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
194 /* Right truncation arrow bitmap `->'. */
196 #define right_width 8
197 #define right_height 8
198 static unsigned char right_bits[] = {
199 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
201 /* Marker for continued lines. */
203 #define continued_width 8
204 #define continued_height 8
205 static unsigned char continued_bits[] = {
206 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
208 /* Marker for continuation lines. */
210 #define continuation_width 8
211 #define continuation_height 8
212 static unsigned char continuation_bits[] = {
213 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
215 /* Overlay arrow bitmap. */
217 #if 0
218 /* A bomb. */
219 #define ov_width 8
220 #define ov_height 8
221 static unsigned char ov_bits[] = {
222 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
223 #else
224 /* A triangular arrow. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
230 #endif
232 extern Lisp_Object Qhelp_echo;
235 /* Non-nil means Emacs uses toolkit scroll bars. */
237 Lisp_Object Vx_toolkit_scroll_bars;
239 /* If a string, XTread_socket generates an event to display that string.
240 (The display is done in read_char.) */
242 static Lisp_Object help_echo;
243 static Lisp_Object help_echo_window;
244 static Lisp_Object help_echo_object;
245 static int help_echo_pos;
247 /* Temporary variable for XTread_socket. */
249 static Lisp_Object previous_help_echo;
251 /* Non-zero means that a HELP_EVENT has been generated since Emacs
252 start. */
254 static int any_help_event_p;
256 /* Non-zero means autoselect window with the mouse cursor. */
258 int autoselect_window_p;
260 /* Last window where we saw the mouse. Used by autoselect-window. */
261 static Lisp_Object last_window;
263 /* Non-zero means draw block and hollow cursor as wide as the glyph
264 under it. For example, if a block cursor is over a tab, it will be
265 drawn as wide as that tab on the display. */
267 int x_stretch_cursor_p;
269 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
271 int x_use_underline_position_properties;
273 /* This is a chain of structures for all the X displays currently in
274 use. */
276 struct x_display_info *x_display_list;
278 /* This is a list of cons cells, each of the form (NAME
279 . FONT-LIST-CACHE), one for each element of x_display_list and in
280 the same order. NAME is the name of the frame. FONT-LIST-CACHE
281 records previous values returned by x-list-fonts. */
283 Lisp_Object x_display_name_list;
285 /* Frame being updated by update_frame. This is declared in term.c.
286 This is set by update_begin and looked at by all the XT functions.
287 It is zero while not inside an update. In that case, the XT
288 functions assume that `selected_frame' is the frame to apply to. */
290 extern struct frame *updating_frame;
292 /* This is a frame waiting to be auto-raised, within XTread_socket. */
294 struct frame *pending_autoraise_frame;
296 #ifdef USE_X_TOOLKIT
297 /* The application context for Xt use. */
298 XtAppContext Xt_app_con;
299 static String Xt_default_resources[] = {0};
300 #endif /* USE_X_TOOLKIT */
302 /* Nominal cursor position -- where to draw output.
303 HPOS and VPOS are window relative glyph matrix coordinates.
304 X and Y are window relative pixel coordinates. */
306 struct cursor_pos output_cursor;
308 /* Non-zero means user is interacting with a toolkit scroll bar. */
310 static int toolkit_scroll_bar_interaction;
312 /* Mouse movement.
314 Formerly, we used PointerMotionHintMask (in standard_event_mask)
315 so that we would have to call XQueryPointer after each MotionNotify
316 event to ask for another such event. However, this made mouse tracking
317 slow, and there was a bug that made it eventually stop.
319 Simply asking for MotionNotify all the time seems to work better.
321 In order to avoid asking for motion events and then throwing most
322 of them away or busy-polling the server for mouse positions, we ask
323 the server for pointer motion hints. This means that we get only
324 one event per group of mouse movements. "Groups" are delimited by
325 other kinds of events (focus changes and button clicks, for
326 example), or by XQueryPointer calls; when one of these happens, we
327 get another MotionNotify event the next time the mouse moves. This
328 is at least as efficient as getting motion events when mouse
329 tracking is on, and I suspect only negligibly worse when tracking
330 is off. */
332 /* Where the mouse was last time we reported a mouse event. */
334 FRAME_PTR last_mouse_frame;
335 static XRectangle last_mouse_glyph;
336 static Lisp_Object last_mouse_press_frame;
338 /* The scroll bar in which the last X motion event occurred.
340 If the last X motion event occurred in a scroll bar, we set this so
341 XTmouse_position can know whether to report a scroll bar motion or
342 an ordinary motion.
344 If the last X motion event didn't occur in a scroll bar, we set
345 this to Qnil, to tell XTmouse_position to return an ordinary motion
346 event. */
348 static Lisp_Object last_mouse_scroll_bar;
350 /* This is a hack. We would really prefer that XTmouse_position would
351 return the time associated with the position it returns, but there
352 doesn't seem to be any way to wrest the time-stamp from the server
353 along with the position query. So, we just keep track of the time
354 of the last movement we received, and return that in hopes that
355 it's somewhat accurate. */
357 static Time last_mouse_movement_time;
359 /* Incremented by XTread_socket whenever it really tries to read
360 events. */
362 #ifdef __STDC__
363 static int volatile input_signal_count;
364 #else
365 static int input_signal_count;
366 #endif
368 /* Used locally within XTread_socket. */
370 static int x_noop_count;
372 /* Initial values of argv and argc. */
374 extern char **initial_argv;
375 extern int initial_argc;
377 extern Lisp_Object Vcommand_line_args, Vsystem_name;
379 /* Tells if a window manager is present or not. */
381 extern Lisp_Object Vx_no_window_manager;
383 extern Lisp_Object Qface, Qmouse_face;
385 extern int errno;
387 /* A mask of extra modifier bits to put into every keyboard char. */
389 extern EMACS_INT extra_keyboard_modifiers;
391 /* The keysyms to use for the various modifiers. */
393 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
394 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
396 static Lisp_Object Qvendor_specific_keysyms;
398 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
399 extern Lisp_Object x_icon_type P_ ((struct frame *));
402 /* Enumeration for overriding/changing the face to use for drawing
403 glyphs in x_draw_glyphs. */
405 enum draw_glyphs_face
407 DRAW_NORMAL_TEXT,
408 DRAW_INVERSE_VIDEO,
409 DRAW_CURSOR,
410 DRAW_MOUSE_FACE,
411 DRAW_IMAGE_RAISED,
412 DRAW_IMAGE_SUNKEN
415 static int cursor_in_mouse_face_p P_ ((struct window *));
416 static int clear_mouse_face P_ ((struct x_display_info *));
417 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
418 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
419 static const XColor *x_color_cells P_ ((Display *, int *));
420 static void x_update_window_end P_ ((struct window *, int, int));
421 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
422 void x_delete_display P_ ((struct x_display_info *));
423 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
424 unsigned));
425 static int fast_find_position P_ ((struct window *, int, int *, int *,
426 int *, int *, Lisp_Object));
427 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
428 int *, int *, int *, int *, int));
429 static void set_output_cursor P_ ((struct cursor_pos *));
430 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
431 int *, int *, int *, int));
432 static void note_mode_line_highlight P_ ((struct window *, int, int));
433 static void note_mouse_highlight P_ ((struct frame *, int, int));
434 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
435 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
436 static void show_mouse_face P_ ((struct x_display_info *,
437 enum draw_glyphs_face));
438 static int x_io_error_quitter P_ ((Display *));
439 int x_catch_errors P_ ((Display *));
440 void x_uncatch_errors P_ ((Display *, int));
441 void x_lower_frame P_ ((struct frame *));
442 void x_scroll_bar_clear P_ ((struct frame *));
443 int x_had_errors_p P_ ((Display *));
444 void x_wm_set_size_hint P_ ((struct frame *, long, int));
445 void x_raise_frame P_ ((struct frame *));
446 void x_set_window_size P_ ((struct frame *, int, int, int));
447 void x_wm_set_window_state P_ ((struct frame *, int));
448 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
449 void x_initialize P_ ((void));
450 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
451 static int x_compute_min_glyph_bounds P_ ((struct frame *));
452 static void x_draw_phys_cursor_glyph P_ ((struct window *,
453 struct glyph_row *,
454 enum draw_glyphs_face));
455 static void x_update_end P_ ((struct frame *));
456 static void XTframe_up_to_date P_ ((struct frame *));
457 static void XTset_terminal_modes P_ ((void));
458 static void XTreset_terminal_modes P_ ((void));
459 static void XTcursor_to P_ ((int, int, int, int));
460 static void x_write_glyphs P_ ((struct glyph *, int));
461 static void x_clear_end_of_line P_ ((int));
462 static void x_clear_frame P_ ((void));
463 static void x_clear_cursor P_ ((struct window *));
464 static void frame_highlight P_ ((struct frame *));
465 static void frame_unhighlight P_ ((struct frame *));
466 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
467 static void XTframe_rehighlight P_ ((struct frame *));
468 static void x_frame_rehighlight P_ ((struct x_display_info *));
469 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
470 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
471 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
472 XRectangle *));
473 static void expose_frame P_ ((struct frame *, int, int, int, int));
474 static int expose_window_tree P_ ((struct window *, XRectangle *));
475 static int expose_window P_ ((struct window *, XRectangle *));
476 static void expose_area P_ ((struct window *, struct glyph_row *,
477 XRectangle *, enum glyph_row_area));
478 static int expose_line P_ ((struct window *, struct glyph_row *,
479 XRectangle *));
480 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
481 static void x_update_window_cursor P_ ((struct window *, int));
482 static void x_erase_phys_cursor P_ ((struct window *));
483 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
484 static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
485 enum fringe_bitmap_type, int left_p));
487 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
488 GC, int));
489 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
490 static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
491 static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
492 int, int, int, int));
493 static void x_flush P_ ((struct frame *f));
494 static void x_update_begin P_ ((struct frame *));
495 static void x_update_window_begin P_ ((struct window *));
496 static void x_draw_vertical_border P_ ((struct window *));
497 static void x_after_update_window_line P_ ((struct glyph_row *));
498 static INLINE void take_vertical_position_into_account P_ ((struct it *));
499 static void x_produce_stretch_glyph P_ ((struct it *));
500 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
501 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
502 enum scroll_bar_part *,
503 Lisp_Object *, Lisp_Object *,
504 unsigned long *));
505 static void x_check_fullscreen P_ ((struct frame *));
506 static void x_check_fullscreen_move P_ ((struct frame *));
508 /* Flush display of frame F, or of all frames if F is null. */
510 static void
511 x_flush (f)
512 struct frame *f;
514 BLOCK_INPUT;
515 if (f == NULL)
517 Lisp_Object rest, frame;
518 FOR_EACH_FRAME (rest, frame)
519 x_flush (XFRAME (frame));
521 else if (FRAME_X_P (f))
522 XFlush (FRAME_X_DISPLAY (f));
523 UNBLOCK_INPUT;
527 /* Remove calls to XFlush by defining XFlush to an empty replacement.
528 Calls to XFlush should be unnecessary because the X output buffer
529 is flushed automatically as needed by calls to XPending,
530 XNextEvent, or XWindowEvent according to the XFlush man page.
531 XTread_socket calls XPending. Removing XFlush improves
532 performance. */
534 #define XFlush(DISPLAY) (void) 0
537 /***********************************************************************
538 Debugging
539 ***********************************************************************/
541 #if 0
543 /* This is a function useful for recording debugging information about
544 the sequence of occurrences in this file. */
546 struct record
548 char *locus;
549 int type;
552 struct record event_record[100];
554 int event_record_index;
556 record_event (locus, type)
557 char *locus;
558 int type;
560 if (event_record_index == sizeof (event_record) / sizeof (struct record))
561 event_record_index = 0;
563 event_record[event_record_index].locus = locus;
564 event_record[event_record_index].type = type;
565 event_record_index++;
568 #endif /* 0 */
572 /* Return the struct x_display_info corresponding to DPY. */
574 struct x_display_info *
575 x_display_info_for_display (dpy)
576 Display *dpy;
578 struct x_display_info *dpyinfo;
580 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
581 if (dpyinfo->display == dpy)
582 return dpyinfo;
584 return 0;
589 /***********************************************************************
590 Starting and ending an update
591 ***********************************************************************/
593 /* Start an update of frame F. This function is installed as a hook
594 for update_begin, i.e. it is called when update_begin is called.
595 This function is called prior to calls to x_update_window_begin for
596 each window being updated. Currently, there is nothing to do here
597 because all interesting stuff is done on a window basis. */
599 static void
600 x_update_begin (f)
601 struct frame *f;
603 /* Nothing to do. */
607 /* Start update of window W. Set the global variable updated_window
608 to the window being updated and set output_cursor to the cursor
609 position of W. */
611 static void
612 x_update_window_begin (w)
613 struct window *w;
615 struct frame *f = XFRAME (WINDOW_FRAME (w));
616 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
618 updated_window = w;
619 set_output_cursor (&w->cursor);
621 BLOCK_INPUT;
623 if (f == display_info->mouse_face_mouse_frame)
625 /* Don't do highlighting for mouse motion during the update. */
626 display_info->mouse_face_defer = 1;
628 /* If F needs to be redrawn, simply forget about any prior mouse
629 highlighting. */
630 if (FRAME_GARBAGED_P (f))
631 display_info->mouse_face_window = Qnil;
633 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
634 their mouse_face_p flag set, which means that they are always
635 unequal to rows in a desired matrix which never have that
636 flag set. So, rows containing mouse-face glyphs are never
637 scrolled, and we don't have to switch the mouse highlight off
638 here to prevent it from being scrolled. */
640 /* Can we tell that this update does not affect the window
641 where the mouse highlight is? If so, no need to turn off.
642 Likewise, don't do anything if the frame is garbaged;
643 in that case, the frame's current matrix that we would use
644 is all wrong, and we will redisplay that line anyway. */
645 if (!NILP (display_info->mouse_face_window)
646 && w == XWINDOW (display_info->mouse_face_window))
648 int i;
650 for (i = 0; i < w->desired_matrix->nrows; ++i)
651 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
652 break;
654 if (i < w->desired_matrix->nrows)
655 clear_mouse_face (display_info);
657 #endif /* 0 */
660 UNBLOCK_INPUT;
664 /* Draw a vertical window border to the right of window W if W doesn't
665 have vertical scroll bars. */
667 static void
668 x_draw_vertical_border (w)
669 struct window *w;
671 struct frame *f = XFRAME (WINDOW_FRAME (w));
673 /* Redraw borders between horizontally adjacent windows. Don't
674 do it for frames with vertical scroll bars because either the
675 right scroll bar of a window, or the left scroll bar of its
676 neighbor will suffice as a border. */
677 if (!WINDOW_RIGHTMOST_P (w)
678 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
680 int x0, x1, y0, y1;
682 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
683 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
684 y1 -= 1;
686 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
687 f->output_data.x->normal_gc, x1, y0, x1, y1);
692 /* End update of window W (which is equal to updated_window).
694 Draw vertical borders between horizontally adjacent windows, and
695 display W's cursor if CURSOR_ON_P is non-zero.
697 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
698 glyphs in mouse-face were overwritten. In that case we have to
699 make sure that the mouse-highlight is properly redrawn.
701 W may be a menu bar pseudo-window in case we don't have X toolkit
702 support. Such windows don't have a cursor, so don't display it
703 here. */
705 static void
706 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
707 struct window *w;
708 int cursor_on_p, mouse_face_overwritten_p;
710 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
712 if (!w->pseudo_window_p)
714 BLOCK_INPUT;
716 if (cursor_on_p)
717 x_display_and_set_cursor (w, 1, output_cursor.hpos,
718 output_cursor.vpos,
719 output_cursor.x, output_cursor.y);
721 x_draw_vertical_border (w);
722 UNBLOCK_INPUT;
725 /* If a row with mouse-face was overwritten, arrange for
726 XTframe_up_to_date to redisplay the mouse highlight. */
727 if (mouse_face_overwritten_p)
729 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
730 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
731 dpyinfo->mouse_face_window = Qnil;
734 updated_window = NULL;
738 /* End update of frame F. This function is installed as a hook in
739 update_end. */
741 static void
742 x_update_end (f)
743 struct frame *f;
745 /* Mouse highlight may be displayed again. */
746 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
748 BLOCK_INPUT;
749 XFlush (FRAME_X_DISPLAY (f));
750 UNBLOCK_INPUT;
754 /* This function is called from various places in xdisp.c whenever a
755 complete update has been performed. The global variable
756 updated_window is not available here. */
758 static void
759 XTframe_up_to_date (f)
760 struct frame *f;
762 if (FRAME_X_P (f))
764 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
766 if (dpyinfo->mouse_face_deferred_gc
767 || f == dpyinfo->mouse_face_mouse_frame)
769 BLOCK_INPUT;
770 if (dpyinfo->mouse_face_mouse_frame)
771 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
772 dpyinfo->mouse_face_mouse_x,
773 dpyinfo->mouse_face_mouse_y);
774 dpyinfo->mouse_face_deferred_gc = 0;
775 UNBLOCK_INPUT;
781 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
782 arrow bitmaps, or clear the fringes if no bitmaps are required
783 before DESIRED_ROW is made current. The window being updated is
784 found in updated_window. This function It is called from
785 update_window_line only if it is known that there are differences
786 between bitmaps to be drawn between current row and DESIRED_ROW. */
788 static void
789 x_after_update_window_line (desired_row)
790 struct glyph_row *desired_row;
792 struct window *w = updated_window;
793 struct frame *f;
794 int width, height;
796 xassert (w);
798 if (!desired_row->mode_line_p && !w->pseudo_window_p)
800 BLOCK_INPUT;
801 x_draw_row_fringe_bitmaps (w, desired_row);
802 UNBLOCK_INPUT;
805 /* When a window has disappeared, make sure that no rest of
806 full-width rows stays visible in the internal border. Could
807 check here if updated_window is the leftmost/rightmost window,
808 but I guess it's not worth doing since vertically split windows
809 are almost never used, internal border is rarely set, and the
810 overhead is very small. */
811 if (windows_or_buffers_changed
812 && desired_row->full_width_p
813 && (f = XFRAME (w->frame),
814 width = FRAME_INTERNAL_BORDER_WIDTH (f),
815 width != 0)
816 && (height = desired_row->visible_height,
817 height > 0))
819 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
821 /* Internal border is drawn below the tool bar. */
822 if (WINDOWP (f->tool_bar_window)
823 && w == XWINDOW (f->tool_bar_window))
824 y -= width;
826 BLOCK_INPUT;
827 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
828 0, y, width, height, False);
829 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
830 f->output_data.x->pixel_width - width,
831 y, width, height, False);
832 UNBLOCK_INPUT;
837 /* Draw the bitmap WHICH in one of the left or right fringes of
838 window W. ROW is the glyph row for which to display the bitmap; it
839 determines the vertical position at which the bitmap has to be
840 drawn. */
842 static void
843 x_draw_fringe_bitmap (w, row, which, left_p)
844 struct window *w;
845 struct glyph_row *row;
846 enum fringe_bitmap_type which;
847 int left_p;
849 struct frame *f = XFRAME (WINDOW_FRAME (w));
850 Display *display = FRAME_X_DISPLAY (f);
851 Window window = FRAME_X_WINDOW (f);
852 int x, y, wd, h, dy;
853 int b1, b2;
854 unsigned char *bits;
855 Pixmap pixmap;
856 GC gc = f->output_data.x->normal_gc;
857 struct face *face;
858 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
860 /* Must clip because of partially visible lines. */
861 x_clip_to_row (w, row, gc, 1);
863 /* Convert row to frame coordinates. */
864 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
866 switch (which)
868 case NO_FRINGE_BITMAP:
869 wd = 0;
870 h = 0;
871 break;
873 case LEFT_TRUNCATION_BITMAP:
874 wd = left_width;
875 h = left_height;
876 bits = left_bits;
877 break;
879 case OVERLAY_ARROW_BITMAP:
880 wd = ov_width;
881 h = ov_height;
882 bits = ov_bits;
883 break;
885 case RIGHT_TRUNCATION_BITMAP:
886 wd = right_width;
887 h = right_height;
888 bits = right_bits;
889 break;
891 case CONTINUED_LINE_BITMAP:
892 wd = continued_width;
893 h = continued_height;
894 bits = continued_bits;
895 break;
897 case CONTINUATION_LINE_BITMAP:
898 wd = continuation_width;
899 h = continuation_height;
900 bits = continuation_bits;
901 break;
903 case ZV_LINE_BITMAP:
904 wd = zv_width;
905 h = zv_height - (y % zv_period);
906 bits = zv_bits + (y % zv_period);
907 break;
909 default:
910 abort ();
913 /* Clip bitmap if too high. */
914 if (h > row->height)
915 h = row->height;
917 /* Set dy to the offset in the row to start drawing the bitmap. */
918 dy = (row->height - h) / 2;
920 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
921 PREPARE_FACE_FOR_DISPLAY (f, face);
923 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
924 the fringe. */
925 b1 = b2 = -1;
926 if (left_p)
928 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
929 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
930 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
931 - wd
932 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
933 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
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 b1 = (window_box_left (w, -1)
940 - FRAME_X_LEFT_FRINGE_WIDTH (f)
941 + border);
942 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
945 else
947 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
948 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
949 x = (window_box_right (w, -1)
950 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
951 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
952 the fringe. */
953 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
955 b1 = window_box_right (w, -1);
956 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
960 if (b1 >= 0)
962 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
964 /* In case the same realized face is used for fringes and
965 for something displayed in the text (e.g. face `region' on
966 mono-displays, the fill style may have been changed to
967 FillSolid in x_draw_glyph_string_background. */
968 if (face->stipple)
969 XSetFillStyle (display, face->gc, FillOpaqueStippled);
970 else
971 XSetForeground (display, face->gc, face->background);
973 XFillRectangle (display, window, face->gc,
975 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
976 row->y)),
978 row->visible_height);
979 if (!face->stipple)
980 XSetForeground (display, face->gc, face->foreground);
983 if (which != NO_FRINGE_BITMAP)
985 /* Draw the bitmap. I believe these small pixmaps can be cached
986 by the server. */
987 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
988 face->foreground,
989 face->background, depth);
990 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
991 XFreePixmap (display, pixmap);
994 XSetClipMask (display, gc, None);
998 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
999 function with input blocked. */
1001 static void
1002 x_draw_row_fringe_bitmaps (w, row)
1003 struct window *w;
1004 struct glyph_row *row;
1006 struct frame *f = XFRAME (w->frame);
1007 enum fringe_bitmap_type bitmap;
1009 xassert (interrupt_input_blocked);
1011 /* If row is completely invisible, because of vscrolling, we
1012 don't have to draw anything. */
1013 if (row->visible_height <= 0)
1014 return;
1016 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1018 /* Decide which bitmap to draw in the left fringe. */
1019 if (row->overlay_arrow_p)
1020 bitmap = OVERLAY_ARROW_BITMAP;
1021 else if (row->truncated_on_left_p)
1022 bitmap = LEFT_TRUNCATION_BITMAP;
1023 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1024 bitmap = CONTINUATION_LINE_BITMAP;
1025 else if (row->indicate_empty_line_p)
1026 bitmap = ZV_LINE_BITMAP;
1027 else
1028 bitmap = NO_FRINGE_BITMAP;
1030 x_draw_fringe_bitmap (w, row, bitmap, 1);
1033 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1035 /* Decide which bitmap to draw in the right fringe. */
1036 if (row->truncated_on_right_p)
1037 bitmap = RIGHT_TRUNCATION_BITMAP;
1038 else if (row->continued_p)
1039 bitmap = CONTINUED_LINE_BITMAP;
1040 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1041 bitmap = ZV_LINE_BITMAP;
1042 else
1043 bitmap = NO_FRINGE_BITMAP;
1045 x_draw_fringe_bitmap (w, row, bitmap, 0);
1051 /* This is called when starting Emacs and when restarting after
1052 suspend. When starting Emacs, no X window is mapped. And nothing
1053 must be done to Emacs's own window if it is suspended (though that
1054 rarely happens). */
1056 static void
1057 XTset_terminal_modes ()
1061 /* This is called when exiting or suspending Emacs. Exiting will make
1062 the X-windows go away, and suspending requires no action. */
1064 static void
1065 XTreset_terminal_modes ()
1071 /***********************************************************************
1072 Output Cursor
1073 ***********************************************************************/
1075 /* Set the global variable output_cursor to CURSOR. All cursor
1076 positions are relative to updated_window. */
1078 static void
1079 set_output_cursor (cursor)
1080 struct cursor_pos *cursor;
1082 output_cursor.hpos = cursor->hpos;
1083 output_cursor.vpos = cursor->vpos;
1084 output_cursor.x = cursor->x;
1085 output_cursor.y = cursor->y;
1089 /* Set a nominal cursor position.
1091 HPOS and VPOS are column/row positions in a window glyph matrix. X
1092 and Y are window text area relative pixel positions.
1094 If this is done during an update, updated_window will contain the
1095 window that is being updated and the position is the future output
1096 cursor position for that window. If updated_window is null, use
1097 selected_window and display the cursor at the given position. */
1099 static void
1100 XTcursor_to (vpos, hpos, y, x)
1101 int vpos, hpos, y, x;
1103 struct window *w;
1105 /* If updated_window is not set, work on selected_window. */
1106 if (updated_window)
1107 w = updated_window;
1108 else
1109 w = XWINDOW (selected_window);
1111 /* Set the output cursor. */
1112 output_cursor.hpos = hpos;
1113 output_cursor.vpos = vpos;
1114 output_cursor.x = x;
1115 output_cursor.y = y;
1117 /* If not called as part of an update, really display the cursor.
1118 This will also set the cursor position of W. */
1119 if (updated_window == NULL)
1121 BLOCK_INPUT;
1122 x_display_cursor (w, 1, hpos, vpos, x, y);
1123 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1124 UNBLOCK_INPUT;
1130 /***********************************************************************
1131 Display Iterator
1132 ***********************************************************************/
1134 /* Function prototypes of this page. */
1136 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1137 struct glyph *,
1138 XChar2b *,
1139 int *));
1140 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1141 int, XChar2b *, int,
1142 int));
1143 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1144 static void x_encode_char P_ ((int, XChar2b *, struct font_info *));
1145 static void x_append_glyph P_ ((struct it *));
1146 static void x_append_composite_glyph P_ ((struct it *));
1147 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1148 int, int, double));
1149 static void x_produce_glyphs P_ ((struct it *));
1150 static void x_produce_image_glyph P_ ((struct it *it));
1153 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1154 is not contained in the font. */
1156 static INLINE XCharStruct *
1157 x_per_char_metric (font, char2b)
1158 XFontStruct *font;
1159 XChar2b *char2b;
1161 /* The result metric information. */
1162 XCharStruct *pcm = NULL;
1164 xassert (font && char2b);
1166 if (font->per_char != NULL)
1168 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1170 /* min_char_or_byte2 specifies the linear character index
1171 corresponding to the first element of the per_char array,
1172 max_char_or_byte2 is the index of the last character. A
1173 character with non-zero CHAR2B->byte1 is not in the font.
1174 A character with byte2 less than min_char_or_byte2 or
1175 greater max_char_or_byte2 is not in the font. */
1176 if (char2b->byte1 == 0
1177 && char2b->byte2 >= font->min_char_or_byte2
1178 && char2b->byte2 <= font->max_char_or_byte2)
1179 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1181 else
1183 /* If either min_byte1 or max_byte1 are nonzero, both
1184 min_char_or_byte2 and max_char_or_byte2 are less than
1185 256, and the 2-byte character index values corresponding
1186 to the per_char array element N (counting from 0) are:
1188 byte1 = N/D + min_byte1
1189 byte2 = N\D + min_char_or_byte2
1191 where:
1193 D = max_char_or_byte2 - min_char_or_byte2 + 1
1194 / = integer division
1195 \ = integer modulus */
1196 if (char2b->byte1 >= font->min_byte1
1197 && char2b->byte1 <= font->max_byte1
1198 && char2b->byte2 >= font->min_char_or_byte2
1199 && char2b->byte2 <= font->max_char_or_byte2)
1201 pcm = (font->per_char
1202 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1203 * (char2b->byte1 - font->min_byte1))
1204 + (char2b->byte2 - font->min_char_or_byte2));
1208 else
1210 /* If the per_char pointer is null, all glyphs between the first
1211 and last character indexes inclusive have the same
1212 information, as given by both min_bounds and max_bounds. */
1213 if (char2b->byte2 >= font->min_char_or_byte2
1214 && char2b->byte2 <= font->max_char_or_byte2)
1215 pcm = &font->max_bounds;
1218 return ((pcm == NULL
1219 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1220 ? NULL : pcm);
1224 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1225 the two-byte form of C. Encoding is returned in *CHAR2B. */
1227 static INLINE void
1228 x_encode_char (c, char2b, font_info)
1229 int c;
1230 XChar2b *char2b;
1231 struct font_info *font_info;
1233 int charset = CHAR_CHARSET (c);
1234 XFontStruct *font = font_info->font;
1236 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1237 This may be either a program in a special encoder language or a
1238 fixed encoding. */
1239 if (font_info->font_encoder)
1241 /* It's a program. */
1242 struct ccl_program *ccl = font_info->font_encoder;
1244 if (CHARSET_DIMENSION (charset) == 1)
1246 ccl->reg[0] = charset;
1247 ccl->reg[1] = char2b->byte2;
1249 else
1251 ccl->reg[0] = charset;
1252 ccl->reg[1] = char2b->byte1;
1253 ccl->reg[2] = char2b->byte2;
1256 ccl_driver (ccl, NULL, NULL, 0, 0, NULL);
1258 /* We assume that MSBs are appropriately set/reset by CCL
1259 program. */
1260 if (font->max_byte1 == 0) /* 1-byte font */
1261 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1262 else
1263 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1265 else if (font_info->encoding[charset])
1267 /* Fixed encoding scheme. See fontset.h for the meaning of the
1268 encoding numbers. */
1269 int enc = font_info->encoding[charset];
1271 if ((enc == 1 || enc == 2)
1272 && CHARSET_DIMENSION (charset) == 2)
1273 char2b->byte1 |= 0x80;
1275 if (enc == 1 || enc == 3)
1276 char2b->byte2 |= 0x80;
1281 /* Get face and two-byte form of character C in face FACE_ID on frame
1282 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1283 means we want to display multibyte text. DISPLAY_P non-zero means
1284 make sure that X resources for the face returned are allocated.
1285 Value is a pointer to a realized face that is ready for display if
1286 DISPLAY_P is non-zero. */
1288 static INLINE struct face *
1289 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p, display_p)
1290 struct frame *f;
1291 int c, face_id;
1292 XChar2b *char2b;
1293 int multibyte_p, display_p;
1295 struct face *face = FACE_FROM_ID (f, face_id);
1297 if (!multibyte_p)
1299 /* Unibyte case. We don't have to encode, but we have to make
1300 sure to use a face suitable for unibyte. */
1301 char2b->byte1 = 0;
1302 char2b->byte2 = c;
1303 face_id = FACE_FOR_CHAR (f, face, c);
1304 face = FACE_FROM_ID (f, face_id);
1306 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1308 /* Case of ASCII in a face known to fit ASCII. */
1309 char2b->byte1 = 0;
1310 char2b->byte2 = c;
1312 else
1314 int c1, c2, charset;
1316 /* Split characters into bytes. If c2 is -1 afterwards, C is
1317 really a one-byte character so that byte1 is zero. */
1318 SPLIT_CHAR (c, charset, c1, c2);
1319 if (c2 > 0)
1320 char2b->byte1 = c1, char2b->byte2 = c2;
1321 else
1322 char2b->byte1 = 0, char2b->byte2 = c1;
1324 /* Maybe encode the character in *CHAR2B. */
1325 if (face->font != NULL)
1327 struct font_info *font_info
1328 = FONT_INFO_FROM_ID (f, face->font_info_id);
1329 if (font_info)
1330 x_encode_char (c, char2b, font_info);
1334 /* Make sure X resources of the face are allocated. */
1335 if (display_p)
1337 xassert (face != NULL);
1338 PREPARE_FACE_FOR_DISPLAY (f, face);
1341 return face;
1345 /* Get face and two-byte form of character glyph GLYPH on frame F.
1346 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1347 a pointer to a realized face that is ready for display. */
1349 static INLINE struct face *
1350 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1351 struct frame *f;
1352 struct glyph *glyph;
1353 XChar2b *char2b;
1354 int *two_byte_p;
1356 struct face *face;
1358 xassert (glyph->type == CHAR_GLYPH);
1359 face = FACE_FROM_ID (f, glyph->face_id);
1361 if (two_byte_p)
1362 *two_byte_p = 0;
1364 if (!glyph->multibyte_p)
1366 /* Unibyte case. We don't have to encode, but we have to make
1367 sure to use a face suitable for unibyte. */
1368 char2b->byte1 = 0;
1369 char2b->byte2 = glyph->u.ch;
1371 else if (glyph->u.ch < 128
1372 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1374 /* Case of ASCII in a face known to fit ASCII. */
1375 char2b->byte1 = 0;
1376 char2b->byte2 = glyph->u.ch;
1378 else
1380 int c1, c2, charset;
1382 /* Split characters into bytes. If c2 is -1 afterwards, C is
1383 really a one-byte character so that byte1 is zero. */
1384 SPLIT_CHAR (glyph->u.ch, charset, c1, c2);
1385 if (c2 > 0)
1386 char2b->byte1 = c1, char2b->byte2 = c2;
1387 else
1388 char2b->byte1 = 0, char2b->byte2 = c1;
1390 /* Maybe encode the character in *CHAR2B. */
1391 if (charset != CHARSET_ASCII)
1393 struct font_info *font_info
1394 = FONT_INFO_FROM_ID (f, face->font_info_id);
1395 if (font_info)
1397 x_encode_char (glyph->u.ch, char2b, font_info);
1398 if (two_byte_p)
1399 *two_byte_p
1400 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1405 /* Make sure X resources of the face are allocated. */
1406 xassert (face != NULL);
1407 PREPARE_FACE_FOR_DISPLAY (f, face);
1408 return face;
1412 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1413 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1415 static INLINE void
1416 x_append_glyph (it)
1417 struct it *it;
1419 struct glyph *glyph;
1420 enum glyph_row_area area = it->area;
1422 xassert (it->glyph_row);
1423 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1425 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1426 if (glyph < it->glyph_row->glyphs[area + 1])
1428 glyph->charpos = CHARPOS (it->position);
1429 glyph->object = it->object;
1430 glyph->pixel_width = it->pixel_width;
1431 glyph->voffset = it->voffset;
1432 glyph->type = CHAR_GLYPH;
1433 glyph->multibyte_p = it->multibyte_p;
1434 glyph->left_box_line_p = it->start_of_box_run_p;
1435 glyph->right_box_line_p = it->end_of_box_run_p;
1436 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1437 || it->phys_descent > it->descent);
1438 glyph->padding_p = 0;
1439 glyph->glyph_not_available_p = it->glyph_not_available_p;
1440 glyph->face_id = it->face_id;
1441 glyph->u.ch = it->char_to_display;
1442 ++it->glyph_row->used[area];
1446 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1447 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1449 static INLINE void
1450 x_append_composite_glyph (it)
1451 struct it *it;
1453 struct glyph *glyph;
1454 enum glyph_row_area area = it->area;
1456 xassert (it->glyph_row);
1458 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1459 if (glyph < it->glyph_row->glyphs[area + 1])
1461 glyph->charpos = CHARPOS (it->position);
1462 glyph->object = it->object;
1463 glyph->pixel_width = it->pixel_width;
1464 glyph->voffset = it->voffset;
1465 glyph->type = COMPOSITE_GLYPH;
1466 glyph->multibyte_p = it->multibyte_p;
1467 glyph->left_box_line_p = it->start_of_box_run_p;
1468 glyph->right_box_line_p = it->end_of_box_run_p;
1469 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1470 || it->phys_descent > it->descent);
1471 glyph->padding_p = 0;
1472 glyph->glyph_not_available_p = 0;
1473 glyph->face_id = it->face_id;
1474 glyph->u.cmp_id = it->cmp_id;
1475 ++it->glyph_row->used[area];
1480 /* Change IT->ascent and IT->height according to the setting of
1481 IT->voffset. */
1483 static INLINE void
1484 take_vertical_position_into_account (it)
1485 struct it *it;
1487 if (it->voffset)
1489 if (it->voffset < 0)
1490 /* Increase the ascent so that we can display the text higher
1491 in the line. */
1492 it->ascent += abs (it->voffset);
1493 else
1494 /* Increase the descent so that we can display the text lower
1495 in the line. */
1496 it->descent += it->voffset;
1501 /* Produce glyphs/get display metrics for the image IT is loaded with.
1502 See the description of struct display_iterator in dispextern.h for
1503 an overview of struct display_iterator. */
1505 static void
1506 x_produce_image_glyph (it)
1507 struct it *it;
1509 struct image *img;
1510 struct face *face;
1512 xassert (it->what == IT_IMAGE);
1514 face = FACE_FROM_ID (it->f, it->face_id);
1515 img = IMAGE_FROM_ID (it->f, it->image_id);
1516 xassert (img);
1518 /* Make sure X resources of the face and image are loaded. */
1519 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1520 prepare_image_for_display (it->f, img);
1522 it->ascent = it->phys_ascent = image_ascent (img, face);
1523 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1524 it->pixel_width = img->width + 2 * img->hmargin;
1526 it->nglyphs = 1;
1528 if (face->box != FACE_NO_BOX)
1530 if (face->box_line_width > 0)
1532 it->ascent += face->box_line_width;
1533 it->descent += face->box_line_width;
1536 if (it->start_of_box_run_p)
1537 it->pixel_width += abs (face->box_line_width);
1538 if (it->end_of_box_run_p)
1539 it->pixel_width += abs (face->box_line_width);
1542 take_vertical_position_into_account (it);
1544 if (it->glyph_row)
1546 struct glyph *glyph;
1547 enum glyph_row_area area = it->area;
1549 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1550 if (glyph < it->glyph_row->glyphs[area + 1])
1552 glyph->charpos = CHARPOS (it->position);
1553 glyph->object = it->object;
1554 glyph->pixel_width = it->pixel_width;
1555 glyph->voffset = it->voffset;
1556 glyph->type = IMAGE_GLYPH;
1557 glyph->multibyte_p = it->multibyte_p;
1558 glyph->left_box_line_p = it->start_of_box_run_p;
1559 glyph->right_box_line_p = it->end_of_box_run_p;
1560 glyph->overlaps_vertically_p = 0;
1561 glyph->padding_p = 0;
1562 glyph->glyph_not_available_p = 0;
1563 glyph->face_id = it->face_id;
1564 glyph->u.img_id = img->id;
1565 ++it->glyph_row->used[area];
1571 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1572 of the glyph, WIDTH and HEIGHT are the width and height of the
1573 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1574 ascent of the glyph (0 <= ASCENT <= 1). */
1576 static void
1577 x_append_stretch_glyph (it, object, width, height, ascent)
1578 struct it *it;
1579 Lisp_Object object;
1580 int width, height;
1581 double ascent;
1583 struct glyph *glyph;
1584 enum glyph_row_area area = it->area;
1586 xassert (ascent >= 0 && ascent <= 1);
1588 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1589 if (glyph < it->glyph_row->glyphs[area + 1])
1591 glyph->charpos = CHARPOS (it->position);
1592 glyph->object = object;
1593 glyph->pixel_width = width;
1594 glyph->voffset = it->voffset;
1595 glyph->type = STRETCH_GLYPH;
1596 glyph->multibyte_p = it->multibyte_p;
1597 glyph->left_box_line_p = it->start_of_box_run_p;
1598 glyph->right_box_line_p = it->end_of_box_run_p;
1599 glyph->overlaps_vertically_p = 0;
1600 glyph->padding_p = 0;
1601 glyph->glyph_not_available_p = 0;
1602 glyph->face_id = it->face_id;
1603 glyph->u.stretch.ascent = height * ascent;
1604 glyph->u.stretch.height = height;
1605 ++it->glyph_row->used[area];
1610 /* Produce a stretch glyph for iterator IT. IT->object is the value
1611 of the glyph property displayed. The value must be a list
1612 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1613 being recognized:
1615 1. `:width WIDTH' specifies that the space should be WIDTH *
1616 canonical char width wide. WIDTH may be an integer or floating
1617 point number.
1619 2. `:relative-width FACTOR' specifies that the width of the stretch
1620 should be computed from the width of the first character having the
1621 `glyph' property, and should be FACTOR times that width.
1623 3. `:align-to HPOS' specifies that the space should be wide enough
1624 to reach HPOS, a value in canonical character units.
1626 Exactly one of the above pairs must be present.
1628 4. `:height HEIGHT' specifies that the height of the stretch produced
1629 should be HEIGHT, measured in canonical character units.
1631 5. `:relative-height FACTOR' specifies that the height of the
1632 stretch should be FACTOR times the height of the characters having
1633 the glyph property.
1635 Either none or exactly one of 4 or 5 must be present.
1637 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1638 of the stretch should be used for the ascent of the stretch.
1639 ASCENT must be in the range 0 <= ASCENT <= 100. */
1641 #define NUMVAL(X) \
1642 ((INTEGERP (X) || FLOATP (X)) \
1643 ? XFLOATINT (X) \
1644 : - 1)
1647 static void
1648 x_produce_stretch_glyph (it)
1649 struct it *it;
1651 /* (space :width WIDTH :height HEIGHT. */
1652 #if GLYPH_DEBUG
1653 extern Lisp_Object Qspace;
1654 #endif
1655 extern Lisp_Object QCwidth, QCheight, QCascent;
1656 extern Lisp_Object QCrelative_width, QCrelative_height;
1657 extern Lisp_Object QCalign_to;
1658 Lisp_Object prop, plist;
1659 double width = 0, height = 0, ascent = 0;
1660 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1661 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1663 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1665 /* List should start with `space'. */
1666 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1667 plist = XCDR (it->object);
1669 /* Compute the width of the stretch. */
1670 if (prop = Fplist_get (plist, QCwidth),
1671 NUMVAL (prop) > 0)
1672 /* Absolute width `:width WIDTH' specified and valid. */
1673 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1674 else if (prop = Fplist_get (plist, QCrelative_width),
1675 NUMVAL (prop) > 0)
1677 /* Relative width `:relative-width FACTOR' specified and valid.
1678 Compute the width of the characters having the `glyph'
1679 property. */
1680 struct it it2;
1681 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1683 it2 = *it;
1684 if (it->multibyte_p)
1686 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1687 - IT_BYTEPOS (*it));
1688 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1690 else
1691 it2.c = *p, it2.len = 1;
1693 it2.glyph_row = NULL;
1694 it2.what = IT_CHARACTER;
1695 x_produce_glyphs (&it2);
1696 width = NUMVAL (prop) * it2.pixel_width;
1698 else if (prop = Fplist_get (plist, QCalign_to),
1699 NUMVAL (prop) > 0)
1700 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1701 else
1702 /* Nothing specified -> width defaults to canonical char width. */
1703 width = CANON_X_UNIT (it->f);
1705 /* Compute height. */
1706 if (prop = Fplist_get (plist, QCheight),
1707 NUMVAL (prop) > 0)
1708 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1709 else if (prop = Fplist_get (plist, QCrelative_height),
1710 NUMVAL (prop) > 0)
1711 height = FONT_HEIGHT (font) * NUMVAL (prop);
1712 else
1713 height = FONT_HEIGHT (font);
1715 /* Compute percentage of height used for ascent. If
1716 `:ascent ASCENT' is present and valid, use that. Otherwise,
1717 derive the ascent from the font in use. */
1718 if (prop = Fplist_get (plist, QCascent),
1719 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1720 ascent = NUMVAL (prop) / 100.0;
1721 else
1722 ascent = (double) font->ascent / FONT_HEIGHT (font);
1724 if (width <= 0)
1725 width = 1;
1726 if (height <= 0)
1727 height = 1;
1729 if (it->glyph_row)
1731 Lisp_Object object = it->stack[it->sp - 1].string;
1732 if (!STRINGP (object))
1733 object = it->w->buffer;
1734 x_append_stretch_glyph (it, object, width, height, ascent);
1737 it->pixel_width = width;
1738 it->ascent = it->phys_ascent = height * ascent;
1739 it->descent = it->phys_descent = height - it->ascent;
1740 it->nglyphs = 1;
1742 if (face->box != FACE_NO_BOX)
1744 if (face->box_line_width > 0)
1746 it->ascent += face->box_line_width;
1747 it->descent += face->box_line_width;
1750 if (it->start_of_box_run_p)
1751 it->pixel_width += abs (face->box_line_width);
1752 if (it->end_of_box_run_p)
1753 it->pixel_width += abs (face->box_line_width);
1756 take_vertical_position_into_account (it);
1759 /* Return proper value to be used as baseline offset of font that has
1760 ASCENT and DESCENT to draw characters by the font at the vertical
1761 center of the line of frame F.
1763 Here, out task is to find the value of BOFF in the following figure;
1765 -------------------------+-----------+-
1766 -+-+---------+-+ | |
1767 | | | | | |
1768 | | | | F_ASCENT F_HEIGHT
1769 | | | ASCENT | |
1770 HEIGHT | | | | |
1771 | | |-|-+------+-----------|------- baseline
1772 | | | | BOFF | |
1773 | |---------|-+-+ | |
1774 | | | DESCENT | |
1775 -+-+---------+-+ F_DESCENT |
1776 -------------------------+-----------+-
1778 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1779 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1780 DESCENT = FONT->descent
1781 HEIGHT = FONT_HEIGHT (FONT)
1782 F_DESCENT = (F->output_data.x->font->descent
1783 - F->output_data.x->baseline_offset)
1784 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1787 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1788 ((FONT)->descent \
1789 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1790 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1791 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1793 /* Produce glyphs/get display metrics for the display element IT is
1794 loaded with. See the description of struct display_iterator in
1795 dispextern.h for an overview of struct display_iterator. */
1797 static void
1798 x_produce_glyphs (it)
1799 struct it *it;
1801 it->glyph_not_available_p = 0;
1803 if (it->what == IT_CHARACTER)
1805 XChar2b char2b;
1806 XFontStruct *font;
1807 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1808 XCharStruct *pcm;
1809 int font_not_found_p;
1810 struct font_info *font_info;
1811 int boff; /* baseline offset */
1812 /* We may change it->multibyte_p upon unibyte<->multibyte
1813 conversion. So, save the current value now and restore it
1814 later.
1816 Note: It seems that we don't have to record multibyte_p in
1817 struct glyph because the character code itself tells if or
1818 not the character is multibyte. Thus, in the future, we must
1819 consider eliminating the field `multibyte_p' in the struct
1820 glyph. */
1821 int saved_multibyte_p = it->multibyte_p;
1823 /* Maybe translate single-byte characters to multibyte, or the
1824 other way. */
1825 it->char_to_display = it->c;
1826 if (!ASCII_BYTE_P (it->c))
1828 if (unibyte_display_via_language_environment
1829 && SINGLE_BYTE_CHAR_P (it->c)
1830 && (it->c >= 0240
1831 || !NILP (Vnonascii_translation_table)))
1833 it->char_to_display = unibyte_char_to_multibyte (it->c);
1834 it->multibyte_p = 1;
1835 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1836 face = FACE_FROM_ID (it->f, it->face_id);
1838 else if (!SINGLE_BYTE_CHAR_P (it->c)
1839 && !it->multibyte_p)
1841 it->multibyte_p = 1;
1842 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1843 face = FACE_FROM_ID (it->f, it->face_id);
1847 /* Get font to use. Encode IT->char_to_display. */
1848 x_get_char_face_and_encoding (it->f, it->char_to_display,
1849 it->face_id, &char2b,
1850 it->multibyte_p, 0);
1851 font = face->font;
1853 /* When no suitable font found, use the default font. */
1854 font_not_found_p = font == NULL;
1855 if (font_not_found_p)
1857 font = FRAME_FONT (it->f);
1858 boff = it->f->output_data.x->baseline_offset;
1859 font_info = NULL;
1861 else
1863 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1864 boff = font_info->baseline_offset;
1865 if (font_info->vertical_centering)
1866 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1869 if (it->char_to_display >= ' '
1870 && (!it->multibyte_p || it->char_to_display < 128))
1872 /* Either unibyte or ASCII. */
1873 int stretched_p;
1875 it->nglyphs = 1;
1877 pcm = x_per_char_metric (font, &char2b);
1878 it->ascent = font->ascent + boff;
1879 it->descent = font->descent - boff;
1881 if (pcm)
1883 it->phys_ascent = pcm->ascent + boff;
1884 it->phys_descent = pcm->descent - boff;
1885 it->pixel_width = pcm->width;
1887 else
1889 it->glyph_not_available_p = 1;
1890 it->phys_ascent = font->ascent + boff;
1891 it->phys_descent = font->descent - boff;
1892 it->pixel_width = FONT_WIDTH (font);
1895 /* If this is a space inside a region of text with
1896 `space-width' property, change its width. */
1897 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1898 if (stretched_p)
1899 it->pixel_width *= XFLOATINT (it->space_width);
1901 /* If face has a box, add the box thickness to the character
1902 height. If character has a box line to the left and/or
1903 right, add the box line width to the character's width. */
1904 if (face->box != FACE_NO_BOX)
1906 int thick = face->box_line_width;
1908 if (thick > 0)
1910 it->ascent += thick;
1911 it->descent += thick;
1913 else
1914 thick = -thick;
1916 if (it->start_of_box_run_p)
1917 it->pixel_width += thick;
1918 if (it->end_of_box_run_p)
1919 it->pixel_width += thick;
1922 /* If face has an overline, add the height of the overline
1923 (1 pixel) and a 1 pixel margin to the character height. */
1924 if (face->overline_p)
1925 it->ascent += 2;
1927 take_vertical_position_into_account (it);
1929 /* If we have to actually produce glyphs, do it. */
1930 if (it->glyph_row)
1932 if (stretched_p)
1934 /* Translate a space with a `space-width' property
1935 into a stretch glyph. */
1936 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1937 x_append_stretch_glyph (it, it->object, it->pixel_width,
1938 it->ascent + it->descent, ascent);
1940 else
1941 x_append_glyph (it);
1943 /* If characters with lbearing or rbearing are displayed
1944 in this line, record that fact in a flag of the
1945 glyph row. This is used to optimize X output code. */
1946 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1947 it->glyph_row->contains_overlapping_glyphs_p = 1;
1950 else if (it->char_to_display == '\n')
1952 /* A newline has no width but we need the height of the line. */
1953 it->pixel_width = 0;
1954 it->nglyphs = 0;
1955 it->ascent = it->phys_ascent = font->ascent + boff;
1956 it->descent = it->phys_descent = font->descent - boff;
1958 if (face->box != FACE_NO_BOX
1959 && face->box_line_width > 0)
1961 it->ascent += face->box_line_width;
1962 it->descent += face->box_line_width;
1965 else if (it->char_to_display == '\t')
1967 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1968 int x = it->current_x + it->continuation_lines_width;
1969 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1971 /* If the distance from the current position to the next tab
1972 stop is less than a canonical character width, use the
1973 tab stop after that. */
1974 if (next_tab_x - x < CANON_X_UNIT (it->f))
1975 next_tab_x += tab_width;
1977 it->pixel_width = next_tab_x - x;
1978 it->nglyphs = 1;
1979 it->ascent = it->phys_ascent = font->ascent + boff;
1980 it->descent = it->phys_descent = font->descent - boff;
1982 if (it->glyph_row)
1984 double ascent = (double) it->ascent / (it->ascent + it->descent);
1985 x_append_stretch_glyph (it, it->object, it->pixel_width,
1986 it->ascent + it->descent, ascent);
1989 else
1991 /* A multi-byte character. Assume that the display width of the
1992 character is the width of the character multiplied by the
1993 width of the font. */
1995 /* If we found a font, this font should give us the right
1996 metrics. If we didn't find a font, use the frame's
1997 default font and calculate the width of the character
1998 from the charset width; this is what old redisplay code
1999 did. */
2000 pcm = x_per_char_metric (font, &char2b);
2001 if (font_not_found_p || !pcm)
2003 int charset = CHAR_CHARSET (it->char_to_display);
2005 it->glyph_not_available_p = 1;
2006 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
2007 * CHARSET_WIDTH (charset));
2008 it->phys_ascent = font->ascent + boff;
2009 it->phys_descent = font->descent - boff;
2011 else
2013 it->pixel_width = pcm->width;
2014 it->phys_ascent = pcm->ascent + boff;
2015 it->phys_descent = pcm->descent - boff;
2016 if (it->glyph_row
2017 && (pcm->lbearing < 0
2018 || pcm->rbearing > pcm->width))
2019 it->glyph_row->contains_overlapping_glyphs_p = 1;
2021 it->nglyphs = 1;
2022 it->ascent = font->ascent + boff;
2023 it->descent = font->descent - boff;
2024 if (face->box != FACE_NO_BOX)
2026 int thick = face->box_line_width;
2028 if (thick > 0)
2030 it->ascent += thick;
2031 it->descent += thick;
2033 else
2034 thick = - thick;
2036 if (it->start_of_box_run_p)
2037 it->pixel_width += thick;
2038 if (it->end_of_box_run_p)
2039 it->pixel_width += thick;
2042 /* If face has an overline, add the height of the overline
2043 (1 pixel) and a 1 pixel margin to the character height. */
2044 if (face->overline_p)
2045 it->ascent += 2;
2047 take_vertical_position_into_account (it);
2049 if (it->glyph_row)
2050 x_append_glyph (it);
2052 it->multibyte_p = saved_multibyte_p;
2054 else if (it->what == IT_COMPOSITION)
2056 /* Note: A composition is represented as one glyph in the
2057 glyph matrix. There are no padding glyphs. */
2058 XChar2b char2b;
2059 XFontStruct *font;
2060 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2061 XCharStruct *pcm;
2062 int font_not_found_p;
2063 struct font_info *font_info;
2064 int boff; /* baseline offset */
2065 struct composition *cmp = composition_table[it->cmp_id];
2067 /* Maybe translate single-byte characters to multibyte. */
2068 it->char_to_display = it->c;
2069 if (unibyte_display_via_language_environment
2070 && SINGLE_BYTE_CHAR_P (it->c)
2071 && (it->c >= 0240
2072 || (it->c >= 0200
2073 && !NILP (Vnonascii_translation_table))))
2075 it->char_to_display = unibyte_char_to_multibyte (it->c);
2078 /* Get face and font to use. Encode IT->char_to_display. */
2079 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2080 face = FACE_FROM_ID (it->f, it->face_id);
2081 x_get_char_face_and_encoding (it->f, it->char_to_display,
2082 it->face_id, &char2b, it->multibyte_p, 0);
2083 font = face->font;
2085 /* When no suitable font found, use the default font. */
2086 font_not_found_p = font == NULL;
2087 if (font_not_found_p)
2089 font = FRAME_FONT (it->f);
2090 boff = it->f->output_data.x->baseline_offset;
2091 font_info = NULL;
2093 else
2095 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2096 boff = font_info->baseline_offset;
2097 if (font_info->vertical_centering)
2098 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2101 /* There are no padding glyphs, so there is only one glyph to
2102 produce for the composition. Important is that pixel_width,
2103 ascent and descent are the values of what is drawn by
2104 draw_glyphs (i.e. the values of the overall glyphs composed). */
2105 it->nglyphs = 1;
2107 /* If we have not yet calculated pixel size data of glyphs of
2108 the composition for the current face font, calculate them
2109 now. Theoretically, we have to check all fonts for the
2110 glyphs, but that requires much time and memory space. So,
2111 here we check only the font of the first glyph. This leads
2112 to incorrect display very rarely, and C-l (recenter) can
2113 correct the display anyway. */
2114 if (cmp->font != (void *) font)
2116 /* Ascent and descent of the font of the first character of
2117 this composition (adjusted by baseline offset). Ascent
2118 and descent of overall glyphs should not be less than
2119 them respectively. */
2120 int font_ascent = font->ascent + boff;
2121 int font_descent = font->descent - boff;
2122 /* Bounding box of the overall glyphs. */
2123 int leftmost, rightmost, lowest, highest;
2124 int i, width, ascent, descent;
2126 cmp->font = (void *) font;
2128 /* Initialize the bounding box. */
2129 if (font_info
2130 && (pcm = x_per_char_metric (font, &char2b)))
2132 width = pcm->width;
2133 ascent = pcm->ascent;
2134 descent = pcm->descent;
2136 else
2138 width = FONT_WIDTH (font);
2139 ascent = font->ascent;
2140 descent = font->descent;
2143 rightmost = width;
2144 lowest = - descent + boff;
2145 highest = ascent + boff;
2146 leftmost = 0;
2148 if (font_info
2149 && font_info->default_ascent
2150 && CHAR_TABLE_P (Vuse_default_ascent)
2151 && !NILP (Faref (Vuse_default_ascent,
2152 make_number (it->char_to_display))))
2153 highest = font_info->default_ascent + boff;
2155 /* Draw the first glyph at the normal position. It may be
2156 shifted to right later if some other glyphs are drawn at
2157 the left. */
2158 cmp->offsets[0] = 0;
2159 cmp->offsets[1] = boff;
2161 /* Set cmp->offsets for the remaining glyphs. */
2162 for (i = 1; i < cmp->glyph_len; i++)
2164 int left, right, btm, top;
2165 int ch = COMPOSITION_GLYPH (cmp, i);
2166 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2168 face = FACE_FROM_ID (it->f, face_id);
2169 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2170 it->multibyte_p, 0);
2171 font = face->font;
2172 if (font == NULL)
2174 font = FRAME_FONT (it->f);
2175 boff = it->f->output_data.x->baseline_offset;
2176 font_info = NULL;
2178 else
2180 font_info
2181 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2182 boff = font_info->baseline_offset;
2183 if (font_info->vertical_centering)
2184 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2187 if (font_info
2188 && (pcm = x_per_char_metric (font, &char2b)))
2190 width = pcm->width;
2191 ascent = pcm->ascent;
2192 descent = pcm->descent;
2194 else
2196 width = FONT_WIDTH (font);
2197 ascent = 1;
2198 descent = 0;
2201 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2203 /* Relative composition with or without
2204 alternate chars. */
2205 left = (leftmost + rightmost - width) / 2;
2206 btm = - descent + boff;
2207 if (font_info && font_info->relative_compose
2208 && (! CHAR_TABLE_P (Vignore_relative_composition)
2209 || NILP (Faref (Vignore_relative_composition,
2210 make_number (ch)))))
2213 if (- descent >= font_info->relative_compose)
2214 /* One extra pixel between two glyphs. */
2215 btm = highest + 1;
2216 else if (ascent <= 0)
2217 /* One extra pixel between two glyphs. */
2218 btm = lowest - 1 - ascent - descent;
2221 else
2223 /* A composition rule is specified by an integer
2224 value that encodes global and new reference
2225 points (GREF and NREF). GREF and NREF are
2226 specified by numbers as below:
2228 0---1---2 -- ascent
2232 9--10--11 -- center
2234 ---3---4---5--- baseline
2236 6---7---8 -- descent
2238 int rule = COMPOSITION_RULE (cmp, i);
2239 int gref, nref, grefx, grefy, nrefx, nrefy;
2241 COMPOSITION_DECODE_RULE (rule, gref, nref);
2242 grefx = gref % 3, nrefx = nref % 3;
2243 grefy = gref / 3, nrefy = nref / 3;
2245 left = (leftmost
2246 + grefx * (rightmost - leftmost) / 2
2247 - nrefx * width / 2);
2248 btm = ((grefy == 0 ? highest
2249 : grefy == 1 ? 0
2250 : grefy == 2 ? lowest
2251 : (highest + lowest) / 2)
2252 - (nrefy == 0 ? ascent + descent
2253 : nrefy == 1 ? descent - boff
2254 : nrefy == 2 ? 0
2255 : (ascent + descent) / 2));
2258 cmp->offsets[i * 2] = left;
2259 cmp->offsets[i * 2 + 1] = btm + descent;
2261 /* Update the bounding box of the overall glyphs. */
2262 right = left + width;
2263 top = btm + descent + ascent;
2264 if (left < leftmost)
2265 leftmost = left;
2266 if (right > rightmost)
2267 rightmost = right;
2268 if (top > highest)
2269 highest = top;
2270 if (btm < lowest)
2271 lowest = btm;
2274 /* If there are glyphs whose x-offsets are negative,
2275 shift all glyphs to the right and make all x-offsets
2276 non-negative. */
2277 if (leftmost < 0)
2279 for (i = 0; i < cmp->glyph_len; i++)
2280 cmp->offsets[i * 2] -= leftmost;
2281 rightmost -= leftmost;
2284 cmp->pixel_width = rightmost;
2285 cmp->ascent = highest;
2286 cmp->descent = - lowest;
2287 if (cmp->ascent < font_ascent)
2288 cmp->ascent = font_ascent;
2289 if (cmp->descent < font_descent)
2290 cmp->descent = font_descent;
2293 it->pixel_width = cmp->pixel_width;
2294 it->ascent = it->phys_ascent = cmp->ascent;
2295 it->descent = it->phys_descent = cmp->descent;
2297 if (face->box != FACE_NO_BOX)
2299 int thick = face->box_line_width;
2301 if (thick > 0)
2303 it->ascent += thick;
2304 it->descent += thick;
2306 else
2307 thick = - thick;
2309 if (it->start_of_box_run_p)
2310 it->pixel_width += thick;
2311 if (it->end_of_box_run_p)
2312 it->pixel_width += thick;
2315 /* If face has an overline, add the height of the overline
2316 (1 pixel) and a 1 pixel margin to the character height. */
2317 if (face->overline_p)
2318 it->ascent += 2;
2320 take_vertical_position_into_account (it);
2322 if (it->glyph_row)
2323 x_append_composite_glyph (it);
2325 else if (it->what == IT_IMAGE)
2326 x_produce_image_glyph (it);
2327 else if (it->what == IT_STRETCH)
2328 x_produce_stretch_glyph (it);
2330 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2331 because this isn't true for images with `:ascent 100'. */
2332 xassert (it->ascent >= 0 && it->descent >= 0);
2333 if (it->area == TEXT_AREA)
2334 it->current_x += it->pixel_width;
2336 it->descent += it->extra_line_spacing;
2338 it->max_ascent = max (it->max_ascent, it->ascent);
2339 it->max_descent = max (it->max_descent, it->descent);
2340 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2341 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2345 /* Estimate the pixel height of the mode or top line on frame F.
2346 FACE_ID specifies what line's height to estimate. */
2349 x_estimate_mode_line_height (f, face_id)
2350 struct frame *f;
2351 enum face_id face_id;
2353 int height = FONT_HEIGHT (FRAME_FONT (f));
2355 /* This function is called so early when Emacs starts that the face
2356 cache and mode line face are not yet initialized. */
2357 if (FRAME_FACE_CACHE (f))
2359 struct face *face = FACE_FROM_ID (f, face_id);
2360 if (face)
2362 if (face->font)
2363 height = FONT_HEIGHT (face->font);
2364 if (face->box_line_width > 0)
2365 height += 2 * face->box_line_width;
2369 return height;
2373 /***********************************************************************
2374 Glyph display
2375 ***********************************************************************/
2377 /* A sequence of glyphs to be drawn in the same face.
2379 This data structure is not really completely X specific, so it
2380 could possibly, at least partially, be useful for other systems. It
2381 is currently not part of the external redisplay interface because
2382 it's not clear what other systems will need. */
2384 struct glyph_string
2386 /* X-origin of the string. */
2387 int x;
2389 /* Y-origin and y-position of the base line of this string. */
2390 int y, ybase;
2392 /* The width of the string, not including a face extension. */
2393 int width;
2395 /* The width of the string, including a face extension. */
2396 int background_width;
2398 /* The height of this string. This is the height of the line this
2399 string is drawn in, and can be different from the height of the
2400 font the string is drawn in. */
2401 int height;
2403 /* Number of pixels this string overwrites in front of its x-origin.
2404 This number is zero if the string has an lbearing >= 0; it is
2405 -lbearing, if the string has an lbearing < 0. */
2406 int left_overhang;
2408 /* Number of pixels this string overwrites past its right-most
2409 nominal x-position, i.e. x + width. Zero if the string's
2410 rbearing is <= its nominal width, rbearing - width otherwise. */
2411 int right_overhang;
2413 /* The frame on which the glyph string is drawn. */
2414 struct frame *f;
2416 /* The window on which the glyph string is drawn. */
2417 struct window *w;
2419 /* X display and window for convenience. */
2420 Display *display;
2421 Window window;
2423 /* The glyph row for which this string was built. It determines the
2424 y-origin and height of the string. */
2425 struct glyph_row *row;
2427 /* The area within row. */
2428 enum glyph_row_area area;
2430 /* Characters to be drawn, and number of characters. */
2431 XChar2b *char2b;
2432 int nchars;
2434 /* A face-override for drawing cursors, mouse face and similar. */
2435 enum draw_glyphs_face hl;
2437 /* Face in which this string is to be drawn. */
2438 struct face *face;
2440 /* Font in which this string is to be drawn. */
2441 XFontStruct *font;
2443 /* Font info for this string. */
2444 struct font_info *font_info;
2446 /* Non-null means this string describes (part of) a composition.
2447 All characters from char2b are drawn composed. */
2448 struct composition *cmp;
2450 /* Index of this glyph string's first character in the glyph
2451 definition of CMP. If this is zero, this glyph string describes
2452 the first character of a composition. */
2453 int gidx;
2455 /* 1 means this glyph strings face has to be drawn to the right end
2456 of the window's drawing area. */
2457 unsigned extends_to_end_of_line_p : 1;
2459 /* 1 means the background of this string has been drawn. */
2460 unsigned background_filled_p : 1;
2462 /* 1 means glyph string must be drawn with 16-bit functions. */
2463 unsigned two_byte_p : 1;
2465 /* 1 means that the original font determined for drawing this glyph
2466 string could not be loaded. The member `font' has been set to
2467 the frame's default font in this case. */
2468 unsigned font_not_found_p : 1;
2470 /* 1 means that the face in which this glyph string is drawn has a
2471 stipple pattern. */
2472 unsigned stippled_p : 1;
2474 /* 1 means only the foreground of this glyph string must be drawn,
2475 and we should use the physical height of the line this glyph
2476 string appears in as clip rect. */
2477 unsigned for_overlaps_p : 1;
2479 /* The GC to use for drawing this glyph string. */
2480 GC gc;
2482 /* A pointer to the first glyph in the string. This glyph
2483 corresponds to char2b[0]. Needed to draw rectangles if
2484 font_not_found_p is 1. */
2485 struct glyph *first_glyph;
2487 /* Image, if any. */
2488 struct image *img;
2490 struct glyph_string *next, *prev;
2494 #if GLYPH_DEBUG
2496 static void
2497 x_dump_glyph_string (s)
2498 struct glyph_string *s;
2500 fprintf (stderr, "glyph string\n");
2501 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2502 s->x, s->y, s->width, s->height);
2503 fprintf (stderr, " ybase = %d\n", s->ybase);
2504 fprintf (stderr, " hl = %d\n", s->hl);
2505 fprintf (stderr, " left overhang = %d, right = %d\n",
2506 s->left_overhang, s->right_overhang);
2507 fprintf (stderr, " nchars = %d\n", s->nchars);
2508 fprintf (stderr, " extends to end of line = %d\n",
2509 s->extends_to_end_of_line_p);
2510 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2511 fprintf (stderr, " bg width = %d\n", s->background_width);
2514 #endif /* GLYPH_DEBUG */
2518 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2519 struct glyph_string **,
2520 struct glyph_string *,
2521 struct glyph_string *));
2522 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2523 struct glyph_string **,
2524 struct glyph_string *,
2525 struct glyph_string *));
2526 static void x_append_glyph_string P_ ((struct glyph_string **,
2527 struct glyph_string **,
2528 struct glyph_string *));
2529 static int x_left_overwritten P_ ((struct glyph_string *));
2530 static int x_left_overwriting P_ ((struct glyph_string *));
2531 static int x_right_overwritten P_ ((struct glyph_string *));
2532 static int x_right_overwriting P_ ((struct glyph_string *));
2533 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2534 int));
2535 static void x_init_glyph_string P_ ((struct glyph_string *,
2536 XChar2b *, struct window *,
2537 struct glyph_row *,
2538 enum glyph_row_area, int,
2539 enum draw_glyphs_face));
2540 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2541 enum glyph_row_area, int, int,
2542 enum draw_glyphs_face, int));
2543 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2544 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2545 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2546 int));
2547 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2548 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2549 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2550 static void x_draw_glyph_string P_ ((struct glyph_string *));
2551 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2552 static void x_set_cursor_gc P_ ((struct glyph_string *));
2553 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2554 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2555 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2556 int *, int *));
2557 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2558 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2559 unsigned long *, double, int));
2560 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2561 double, int, unsigned long));
2562 static void x_setup_relief_colors P_ ((struct glyph_string *));
2563 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2564 static void x_draw_image_relief P_ ((struct glyph_string *));
2565 static void x_draw_image_foreground P_ ((struct glyph_string *));
2566 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2567 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2568 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2569 int, int, int));
2570 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2571 int, int, int, int, XRectangle *));
2572 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2573 int, int, int, XRectangle *));
2574 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2575 enum glyph_row_area));
2576 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2577 struct glyph_row *,
2578 enum glyph_row_area, int, int));
2580 #if GLYPH_DEBUG
2581 static void x_check_font P_ ((struct frame *, XFontStruct *));
2582 #endif
2585 /* Append the list of glyph strings with head H and tail T to the list
2586 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2588 static INLINE void
2589 x_append_glyph_string_lists (head, tail, h, t)
2590 struct glyph_string **head, **tail;
2591 struct glyph_string *h, *t;
2593 if (h)
2595 if (*head)
2596 (*tail)->next = h;
2597 else
2598 *head = h;
2599 h->prev = *tail;
2600 *tail = t;
2605 /* Prepend the list of glyph strings with head H and tail T to the
2606 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2607 result. */
2609 static INLINE void
2610 x_prepend_glyph_string_lists (head, tail, h, t)
2611 struct glyph_string **head, **tail;
2612 struct glyph_string *h, *t;
2614 if (h)
2616 if (*head)
2617 (*head)->prev = t;
2618 else
2619 *tail = t;
2620 t->next = *head;
2621 *head = h;
2626 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2627 Set *HEAD and *TAIL to the resulting list. */
2629 static INLINE void
2630 x_append_glyph_string (head, tail, s)
2631 struct glyph_string **head, **tail;
2632 struct glyph_string *s;
2634 s->next = s->prev = NULL;
2635 x_append_glyph_string_lists (head, tail, s, s);
2639 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2640 face. */
2642 static void
2643 x_set_cursor_gc (s)
2644 struct glyph_string *s;
2646 if (s->font == FRAME_FONT (s->f)
2647 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2648 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2649 && !s->cmp)
2650 s->gc = s->f->output_data.x->cursor_gc;
2651 else
2653 /* Cursor on non-default face: must merge. */
2654 XGCValues xgcv;
2655 unsigned long mask;
2657 xgcv.background = s->f->output_data.x->cursor_pixel;
2658 xgcv.foreground = s->face->background;
2660 /* If the glyph would be invisible, try a different foreground. */
2661 if (xgcv.foreground == xgcv.background)
2662 xgcv.foreground = s->face->foreground;
2663 if (xgcv.foreground == xgcv.background)
2664 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2665 if (xgcv.foreground == xgcv.background)
2666 xgcv.foreground = s->face->foreground;
2668 /* Make sure the cursor is distinct from text in this face. */
2669 if (xgcv.background == s->face->background
2670 && xgcv.foreground == s->face->foreground)
2672 xgcv.background = s->face->foreground;
2673 xgcv.foreground = s->face->background;
2676 IF_DEBUG (x_check_font (s->f, s->font));
2677 xgcv.font = s->font->fid;
2678 xgcv.graphics_exposures = False;
2679 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2681 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2682 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2683 mask, &xgcv);
2684 else
2685 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2686 = XCreateGC (s->display, s->window, mask, &xgcv);
2688 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2693 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2695 static void
2696 x_set_mouse_face_gc (s)
2697 struct glyph_string *s;
2699 int face_id;
2700 struct face *face;
2702 /* What face has to be used last for the mouse face? */
2703 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2704 face = FACE_FROM_ID (s->f, face_id);
2705 if (face == NULL)
2706 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2708 if (s->first_glyph->type == CHAR_GLYPH)
2709 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2710 else
2711 face_id = FACE_FOR_CHAR (s->f, face, 0);
2712 s->face = FACE_FROM_ID (s->f, face_id);
2713 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2715 /* If font in this face is same as S->font, use it. */
2716 if (s->font == s->face->font)
2717 s->gc = s->face->gc;
2718 else
2720 /* Otherwise construct scratch_cursor_gc with values from FACE
2721 but font FONT. */
2722 XGCValues xgcv;
2723 unsigned long mask;
2725 xgcv.background = s->face->background;
2726 xgcv.foreground = s->face->foreground;
2727 IF_DEBUG (x_check_font (s->f, s->font));
2728 xgcv.font = s->font->fid;
2729 xgcv.graphics_exposures = False;
2730 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2732 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2733 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2734 mask, &xgcv);
2735 else
2736 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2737 = XCreateGC (s->display, s->window, mask, &xgcv);
2739 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2742 xassert (s->gc != 0);
2746 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2747 Faces to use in the mode line have already been computed when the
2748 matrix was built, so there isn't much to do, here. */
2750 static INLINE void
2751 x_set_mode_line_face_gc (s)
2752 struct glyph_string *s;
2754 s->gc = s->face->gc;
2758 /* Set S->gc of glyph string S for drawing that glyph string. Set
2759 S->stippled_p to a non-zero value if the face of S has a stipple
2760 pattern. */
2762 static INLINE void
2763 x_set_glyph_string_gc (s)
2764 struct glyph_string *s;
2766 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2768 if (s->hl == DRAW_NORMAL_TEXT)
2770 s->gc = s->face->gc;
2771 s->stippled_p = s->face->stipple != 0;
2773 else if (s->hl == DRAW_INVERSE_VIDEO)
2775 x_set_mode_line_face_gc (s);
2776 s->stippled_p = s->face->stipple != 0;
2778 else if (s->hl == DRAW_CURSOR)
2780 x_set_cursor_gc (s);
2781 s->stippled_p = 0;
2783 else if (s->hl == DRAW_MOUSE_FACE)
2785 x_set_mouse_face_gc (s);
2786 s->stippled_p = s->face->stipple != 0;
2788 else if (s->hl == DRAW_IMAGE_RAISED
2789 || s->hl == DRAW_IMAGE_SUNKEN)
2791 s->gc = s->face->gc;
2792 s->stippled_p = s->face->stipple != 0;
2794 else
2796 s->gc = s->face->gc;
2797 s->stippled_p = s->face->stipple != 0;
2800 /* GC must have been set. */
2801 xassert (s->gc != 0);
2805 /* Return in *R the clipping rectangle for glyph string S. */
2807 static void
2808 x_get_glyph_string_clip_rect (s, r)
2809 struct glyph_string *s;
2810 XRectangle *r;
2812 if (s->row->full_width_p)
2814 /* Draw full-width. X coordinates are relative to S->w->left. */
2815 int canon_x = CANON_X_UNIT (s->f);
2817 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2818 r->width = XFASTINT (s->w->width) * canon_x;
2820 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2822 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2823 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2824 r->x -= width;
2827 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2829 /* Unless displaying a mode or menu bar line, which are always
2830 fully visible, clip to the visible part of the row. */
2831 if (s->w->pseudo_window_p)
2832 r->height = s->row->visible_height;
2833 else
2834 r->height = s->height;
2836 else
2838 /* This is a text line that may be partially visible. */
2839 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2840 r->width = window_box_width (s->w, s->area);
2841 r->height = s->row->visible_height;
2844 /* If S draws overlapping rows, it's sufficient to use the top and
2845 bottom of the window for clipping because this glyph string
2846 intentionally draws over other lines. */
2847 if (s->for_overlaps_p)
2849 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2850 r->height = window_text_bottom_y (s->w) - r->y;
2852 else
2854 /* Don't use S->y for clipping because it doesn't take partially
2855 visible lines into account. For example, it can be negative for
2856 partially visible lines at the top of a window. */
2857 if (!s->row->full_width_p
2858 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2859 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2860 else
2861 r->y = max (0, s->row->y);
2863 /* If drawing a tool-bar window, draw it over the internal border
2864 at the top of the window. */
2865 if (s->w == XWINDOW (s->f->tool_bar_window))
2866 r->y -= s->f->output_data.x->internal_border_width;
2869 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2873 /* Set clipping for output of glyph string S. S may be part of a mode
2874 line or menu if we don't have X toolkit support. */
2876 static INLINE void
2877 x_set_glyph_string_clipping (s)
2878 struct glyph_string *s;
2880 XRectangle r;
2881 x_get_glyph_string_clip_rect (s, &r);
2882 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2886 /* Compute left and right overhang of glyph string S. If S is a glyph
2887 string for a composition, assume overhangs don't exist. */
2889 static INLINE void
2890 x_compute_glyph_string_overhangs (s)
2891 struct glyph_string *s;
2893 if (s->cmp == NULL
2894 && s->first_glyph->type == CHAR_GLYPH)
2896 XCharStruct cs;
2897 int direction, font_ascent, font_descent;
2898 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2899 &font_ascent, &font_descent, &cs);
2900 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2901 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2906 /* Compute overhangs and x-positions for glyph string S and its
2907 predecessors, or successors. X is the starting x-position for S.
2908 BACKWARD_P non-zero means process predecessors. */
2910 static void
2911 x_compute_overhangs_and_x (s, x, backward_p)
2912 struct glyph_string *s;
2913 int x;
2914 int backward_p;
2916 if (backward_p)
2918 while (s)
2920 x_compute_glyph_string_overhangs (s);
2921 x -= s->width;
2922 s->x = x;
2923 s = s->prev;
2926 else
2928 while (s)
2930 x_compute_glyph_string_overhangs (s);
2931 s->x = x;
2932 x += s->width;
2933 s = s->next;
2939 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2940 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2941 assumed to be zero. */
2943 static void
2944 x_get_glyph_overhangs (glyph, f, left, right)
2945 struct glyph *glyph;
2946 struct frame *f;
2947 int *left, *right;
2949 *left = *right = 0;
2951 if (glyph->type == CHAR_GLYPH)
2953 XFontStruct *font;
2954 struct face *face;
2955 struct font_info *font_info;
2956 XChar2b char2b;
2957 XCharStruct *pcm;
2959 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2960 font = face->font;
2961 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2962 if (font
2963 && (pcm = x_per_char_metric (font, &char2b)))
2965 if (pcm->rbearing > pcm->width)
2966 *right = pcm->rbearing - pcm->width;
2967 if (pcm->lbearing < 0)
2968 *left = -pcm->lbearing;
2974 /* Return the index of the first glyph preceding glyph string S that
2975 is overwritten by S because of S's left overhang. Value is -1
2976 if no glyphs are overwritten. */
2978 static int
2979 x_left_overwritten (s)
2980 struct glyph_string *s;
2982 int k;
2984 if (s->left_overhang)
2986 int x = 0, i;
2987 struct glyph *glyphs = s->row->glyphs[s->area];
2988 int first = s->first_glyph - glyphs;
2990 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2991 x -= glyphs[i].pixel_width;
2993 k = i + 1;
2995 else
2996 k = -1;
2998 return k;
3002 /* Return the index of the first glyph preceding glyph string S that
3003 is overwriting S because of its right overhang. Value is -1 if no
3004 glyph in front of S overwrites S. */
3006 static int
3007 x_left_overwriting (s)
3008 struct glyph_string *s;
3010 int i, k, x;
3011 struct glyph *glyphs = s->row->glyphs[s->area];
3012 int first = s->first_glyph - glyphs;
3014 k = -1;
3015 x = 0;
3016 for (i = first - 1; i >= 0; --i)
3018 int left, right;
3019 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3020 if (x + right > 0)
3021 k = i;
3022 x -= glyphs[i].pixel_width;
3025 return k;
3029 /* Return the index of the last glyph following glyph string S that is
3030 not overwritten by S because of S's right overhang. Value is -1 if
3031 no such glyph is found. */
3033 static int
3034 x_right_overwritten (s)
3035 struct glyph_string *s;
3037 int k = -1;
3039 if (s->right_overhang)
3041 int x = 0, i;
3042 struct glyph *glyphs = s->row->glyphs[s->area];
3043 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3044 int end = s->row->used[s->area];
3046 for (i = first; i < end && s->right_overhang > x; ++i)
3047 x += glyphs[i].pixel_width;
3049 k = i;
3052 return k;
3056 /* Return the index of the last glyph following glyph string S that
3057 overwrites S because of its left overhang. Value is negative
3058 if no such glyph is found. */
3060 static int
3061 x_right_overwriting (s)
3062 struct glyph_string *s;
3064 int i, k, x;
3065 int end = s->row->used[s->area];
3066 struct glyph *glyphs = s->row->glyphs[s->area];
3067 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3069 k = -1;
3070 x = 0;
3071 for (i = first; i < end; ++i)
3073 int left, right;
3074 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3075 if (x - left < 0)
3076 k = i;
3077 x += glyphs[i].pixel_width;
3080 return k;
3084 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3086 static INLINE void
3087 x_clear_glyph_string_rect (s, x, y, w, h)
3088 struct glyph_string *s;
3089 int x, y, w, h;
3091 XGCValues xgcv;
3092 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3093 XSetForeground (s->display, s->gc, xgcv.background);
3094 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3095 XSetForeground (s->display, s->gc, xgcv.foreground);
3099 /* Draw the background of glyph_string S. If S->background_filled_p
3100 is non-zero don't draw it. FORCE_P non-zero means draw the
3101 background even if it wouldn't be drawn normally. This is used
3102 when a string preceding S draws into the background of S, or S
3103 contains the first component of a composition. */
3105 static void
3106 x_draw_glyph_string_background (s, force_p)
3107 struct glyph_string *s;
3108 int force_p;
3110 /* Nothing to do if background has already been drawn or if it
3111 shouldn't be drawn in the first place. */
3112 if (!s->background_filled_p)
3114 int box_line_width = max (s->face->box_line_width, 0);
3116 if (s->stippled_p)
3118 /* Fill background with a stipple pattern. */
3119 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3120 XFillRectangle (s->display, s->window, s->gc, s->x,
3121 s->y + box_line_width,
3122 s->background_width,
3123 s->height - 2 * box_line_width);
3124 XSetFillStyle (s->display, s->gc, FillSolid);
3125 s->background_filled_p = 1;
3127 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3128 || s->font_not_found_p
3129 || s->extends_to_end_of_line_p
3130 || force_p)
3132 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3133 s->background_width,
3134 s->height - 2 * box_line_width);
3135 s->background_filled_p = 1;
3141 /* Draw the foreground of glyph string S. */
3143 static void
3144 x_draw_glyph_string_foreground (s)
3145 struct glyph_string *s;
3147 int i, x;
3149 /* If first glyph of S has a left box line, start drawing the text
3150 of S to the right of that box line. */
3151 if (s->face->box != FACE_NO_BOX
3152 && s->first_glyph->left_box_line_p)
3153 x = s->x + abs (s->face->box_line_width);
3154 else
3155 x = s->x;
3157 /* Draw characters of S as rectangles if S's font could not be
3158 loaded. */
3159 if (s->font_not_found_p)
3161 for (i = 0; i < s->nchars; ++i)
3163 struct glyph *g = s->first_glyph + i;
3164 XDrawRectangle (s->display, s->window,
3165 s->gc, x, s->y, g->pixel_width - 1,
3166 s->height - 1);
3167 x += g->pixel_width;
3170 else
3172 char *char1b = (char *) s->char2b;
3173 int boff = s->font_info->baseline_offset;
3175 if (s->font_info->vertical_centering)
3176 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3178 /* If we can use 8-bit functions, condense S->char2b. */
3179 if (!s->two_byte_p)
3180 for (i = 0; i < s->nchars; ++i)
3181 char1b[i] = s->char2b[i].byte2;
3183 /* Draw text with XDrawString if background has already been
3184 filled. Otherwise, use XDrawImageString. (Note that
3185 XDrawImageString is usually faster than XDrawString.) Always
3186 use XDrawImageString when drawing the cursor so that there is
3187 no chance that characters under a box cursor are invisible. */
3188 if (s->for_overlaps_p
3189 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3191 /* Draw characters with 16-bit or 8-bit functions. */
3192 if (s->two_byte_p)
3193 XDrawString16 (s->display, s->window, s->gc, x,
3194 s->ybase - boff, s->char2b, s->nchars);
3195 else
3196 XDrawString (s->display, s->window, s->gc, x,
3197 s->ybase - boff, char1b, s->nchars);
3199 else
3201 if (s->two_byte_p)
3202 XDrawImageString16 (s->display, s->window, s->gc, x,
3203 s->ybase - boff, s->char2b, s->nchars);
3204 else
3205 XDrawImageString (s->display, s->window, s->gc, x,
3206 s->ybase - boff, char1b, s->nchars);
3211 /* Draw the foreground of composite glyph string S. */
3213 static void
3214 x_draw_composite_glyph_string_foreground (s)
3215 struct glyph_string *s;
3217 int i, x;
3219 /* If first glyph of S has a left box line, start drawing the text
3220 of S to the right of that box line. */
3221 if (s->face->box != FACE_NO_BOX
3222 && s->first_glyph->left_box_line_p)
3223 x = s->x + abs (s->face->box_line_width);
3224 else
3225 x = s->x;
3227 /* S is a glyph string for a composition. S->gidx is the index of
3228 the first character drawn for glyphs of this composition.
3229 S->gidx == 0 means we are drawing the very first character of
3230 this composition. */
3232 /* Draw a rectangle for the composition if the font for the very
3233 first character of the composition could not be loaded. */
3234 if (s->font_not_found_p)
3236 if (s->gidx == 0)
3237 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3238 s->width - 1, s->height - 1);
3240 else
3242 for (i = 0; i < s->nchars; i++, ++s->gidx)
3243 XDrawString16 (s->display, s->window, s->gc,
3244 x + s->cmp->offsets[s->gidx * 2],
3245 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3246 s->char2b + i, 1);
3251 #ifdef USE_X_TOOLKIT
3253 static struct frame *x_frame_of_widget P_ ((Widget));
3254 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3255 XrmValue *, XrmValue *, XtPointer *));
3256 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3257 XrmValue *, Cardinal *));
3260 /* Return the frame on which widget WIDGET is used.. Abort if frame
3261 cannot be determined. */
3263 static struct frame *
3264 x_frame_of_widget (widget)
3265 Widget widget;
3267 struct x_display_info *dpyinfo;
3268 Lisp_Object tail;
3269 struct frame *f;
3271 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3273 /* Find the top-level shell of the widget. Note that this function
3274 can be called when the widget is not yet realized, so XtWindow
3275 (widget) == 0. That's the reason we can't simply use
3276 x_any_window_to_frame. */
3277 while (!XtIsTopLevelShell (widget))
3278 widget = XtParent (widget);
3280 /* Look for a frame with that top-level widget. Allocate the color
3281 on that frame to get the right gamma correction value. */
3282 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3283 if (GC_FRAMEP (XCAR (tail))
3284 && (f = XFRAME (XCAR (tail)),
3285 (f->output_data.nothing != 1
3286 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3287 && f->output_data.x->widget == widget)
3288 return f;
3290 abort ();
3294 /* Allocate the color COLOR->pixel on the screen and display of
3295 widget WIDGET in colormap CMAP. If an exact match cannot be
3296 allocated, try the nearest color available. Value is non-zero
3297 if successful. This is called from lwlib. */
3300 x_alloc_nearest_color_for_widget (widget, cmap, color)
3301 Widget widget;
3302 Colormap cmap;
3303 XColor *color;
3305 struct frame *f = x_frame_of_widget (widget);
3306 return x_alloc_nearest_color (f, cmap, color);
3310 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3311 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3312 If this produces the same color as PIXEL, try a color where all RGB
3313 values have DELTA added. Return the allocated color in *PIXEL.
3314 DISPLAY is the X display, CMAP is the colormap to operate on.
3315 Value is non-zero if successful. */
3318 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3319 Widget widget;
3320 Display *display;
3321 Colormap cmap;
3322 unsigned long *pixel;
3323 double factor;
3324 int delta;
3326 struct frame *f = x_frame_of_widget (widget);
3327 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3331 /* Structure specifying which arguments should be passed by Xt to
3332 cvt_string_to_pixel. We want the widget's screen and colormap. */
3334 static XtConvertArgRec cvt_string_to_pixel_args[] =
3336 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3337 sizeof (Screen *)},
3338 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3339 sizeof (Colormap)}
3343 /* The address of this variable is returned by
3344 cvt_string_to_pixel. */
3346 static Pixel cvt_string_to_pixel_value;
3349 /* Convert a color name to a pixel color.
3351 DPY is the display we are working on.
3353 ARGS is an array of *NARGS XrmValue structures holding additional
3354 information about the widget for which the conversion takes place.
3355 The contents of this array are determined by the specification
3356 in cvt_string_to_pixel_args.
3358 FROM is a pointer to an XrmValue which points to the color name to
3359 convert. TO is an XrmValue in which to return the pixel color.
3361 CLOSURE_RET is a pointer to user-data, in which we record if
3362 we allocated the color or not.
3364 Value is True if successful, False otherwise. */
3366 static Boolean
3367 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3368 Display *dpy;
3369 XrmValue *args;
3370 Cardinal *nargs;
3371 XrmValue *from, *to;
3372 XtPointer *closure_ret;
3374 Screen *screen;
3375 Colormap cmap;
3376 Pixel pixel;
3377 String color_name;
3378 XColor color;
3380 if (*nargs != 2)
3382 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3383 "wrongParameters", "cvt_string_to_pixel",
3384 "XtToolkitError",
3385 "Screen and colormap args required", NULL, NULL);
3386 return False;
3389 screen = *(Screen **) args[0].addr;
3390 cmap = *(Colormap *) args[1].addr;
3391 color_name = (String) from->addr;
3393 if (strcmp (color_name, XtDefaultBackground) == 0)
3395 *closure_ret = (XtPointer) False;
3396 pixel = WhitePixelOfScreen (screen);
3398 else if (strcmp (color_name, XtDefaultForeground) == 0)
3400 *closure_ret = (XtPointer) False;
3401 pixel = BlackPixelOfScreen (screen);
3403 else if (XParseColor (dpy, cmap, color_name, &color)
3404 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3406 pixel = color.pixel;
3407 *closure_ret = (XtPointer) True;
3409 else
3411 String params[1];
3412 Cardinal nparams = 1;
3414 params[0] = color_name;
3415 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3416 "badValue", "cvt_string_to_pixel",
3417 "XtToolkitError", "Invalid color `%s'",
3418 params, &nparams);
3419 return False;
3422 if (to->addr != NULL)
3424 if (to->size < sizeof (Pixel))
3426 to->size = sizeof (Pixel);
3427 return False;
3430 *(Pixel *) to->addr = pixel;
3432 else
3434 cvt_string_to_pixel_value = pixel;
3435 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3438 to->size = sizeof (Pixel);
3439 return True;
3443 /* Free a pixel color which was previously allocated via
3444 cvt_string_to_pixel. This is registered as the destructor
3445 for this type of resource via XtSetTypeConverter.
3447 APP is the application context in which we work.
3449 TO is a pointer to an XrmValue holding the color to free.
3450 CLOSURE is the value we stored in CLOSURE_RET for this color
3451 in cvt_string_to_pixel.
3453 ARGS and NARGS are like for cvt_string_to_pixel. */
3455 static void
3456 cvt_pixel_dtor (app, to, closure, args, nargs)
3457 XtAppContext app;
3458 XrmValuePtr to;
3459 XtPointer closure;
3460 XrmValuePtr args;
3461 Cardinal *nargs;
3463 if (*nargs != 2)
3465 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3466 "XtToolkitError",
3467 "Screen and colormap arguments required",
3468 NULL, NULL);
3470 else if (closure != NULL)
3472 /* We did allocate the pixel, so free it. */
3473 Screen *screen = *(Screen **) args[0].addr;
3474 Colormap cmap = *(Colormap *) args[1].addr;
3475 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3476 (Pixel *) to->addr, 1);
3481 #endif /* USE_X_TOOLKIT */
3484 /* Value is an array of XColor structures for the contents of the
3485 color map of display DPY. Set *NCELLS to the size of the array.
3486 Note that this probably shouldn't be called for large color maps,
3487 say a 24-bit TrueColor map. */
3489 static const XColor *
3490 x_color_cells (dpy, ncells)
3491 Display *dpy;
3492 int *ncells;
3494 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3496 if (dpyinfo->color_cells == NULL)
3498 Screen *screen = dpyinfo->screen;
3499 int i;
3501 dpyinfo->ncolor_cells
3502 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3503 dpyinfo->color_cells
3504 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3505 * sizeof *dpyinfo->color_cells);
3507 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3508 dpyinfo->color_cells[i].pixel = i;
3510 XQueryColors (dpy, dpyinfo->cmap,
3511 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3514 *ncells = dpyinfo->ncolor_cells;
3515 return dpyinfo->color_cells;
3519 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3520 colors in COLORS. Use cached information, if available. */
3522 void
3523 x_query_colors (f, colors, ncolors)
3524 struct frame *f;
3525 XColor *colors;
3526 int ncolors;
3528 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3530 if (dpyinfo->color_cells)
3532 int i;
3533 for (i = 0; i < ncolors; ++i)
3535 unsigned long pixel = colors[i].pixel;
3536 xassert (pixel < dpyinfo->ncolor_cells);
3537 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3538 colors[i] = dpyinfo->color_cells[pixel];
3541 else
3542 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3546 /* On frame F, translate pixel color to RGB values for the color in
3547 COLOR. Use cached information, if available. */
3549 void
3550 x_query_color (f, color)
3551 struct frame *f;
3552 XColor *color;
3554 x_query_colors (f, color, 1);
3558 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3559 exact match can't be allocated, try the nearest color available.
3560 Value is non-zero if successful. Set *COLOR to the color
3561 allocated. */
3563 static int
3564 x_alloc_nearest_color_1 (dpy, cmap, color)
3565 Display *dpy;
3566 Colormap cmap;
3567 XColor *color;
3569 int rc;
3571 rc = XAllocColor (dpy, cmap, color);
3572 if (rc == 0)
3574 /* If we got to this point, the colormap is full, so we're going
3575 to try to get the next closest color. The algorithm used is
3576 a least-squares matching, which is what X uses for closest
3577 color matching with StaticColor visuals. */
3578 int nearest, i;
3579 unsigned long nearest_delta = ~0;
3580 int ncells;
3581 const XColor *cells = x_color_cells (dpy, &ncells);
3583 for (nearest = i = 0; i < ncells; ++i)
3585 long dred = (color->red >> 8) - (cells[i].red >> 8);
3586 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3587 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3588 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3590 if (delta < nearest_delta)
3592 nearest = i;
3593 nearest_delta = delta;
3597 color->red = cells[nearest].red;
3598 color->green = cells[nearest].green;
3599 color->blue = cells[nearest].blue;
3600 rc = XAllocColor (dpy, cmap, color);
3602 else
3604 /* If allocation succeeded, and the allocated pixel color is not
3605 equal to a cached pixel color recorded earlier, there was a
3606 change in the colormap, so clear the color cache. */
3607 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3608 XColor *cached_color;
3610 if (dpyinfo->color_cells
3611 && (cached_color = &dpyinfo->color_cells[color->pixel],
3612 (cached_color->red != color->red
3613 || cached_color->blue != color->blue
3614 || cached_color->green != color->green)))
3616 xfree (dpyinfo->color_cells);
3617 dpyinfo->color_cells = NULL;
3618 dpyinfo->ncolor_cells = 0;
3622 #ifdef DEBUG_X_COLORS
3623 if (rc)
3624 register_color (color->pixel);
3625 #endif /* DEBUG_X_COLORS */
3627 return rc;
3631 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3632 exact match can't be allocated, try the nearest color available.
3633 Value is non-zero if successful. Set *COLOR to the color
3634 allocated. */
3637 x_alloc_nearest_color (f, cmap, color)
3638 struct frame *f;
3639 Colormap cmap;
3640 XColor *color;
3642 gamma_correct (f, color);
3643 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3647 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3648 It's necessary to do this instead of just using PIXEL directly to
3649 get color reference counts right. */
3651 unsigned long
3652 x_copy_color (f, pixel)
3653 struct frame *f;
3654 unsigned long pixel;
3656 XColor color;
3658 color.pixel = pixel;
3659 BLOCK_INPUT;
3660 x_query_color (f, &color);
3661 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3662 UNBLOCK_INPUT;
3663 #ifdef DEBUG_X_COLORS
3664 register_color (pixel);
3665 #endif
3666 return color.pixel;
3670 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3671 It's necessary to do this instead of just using PIXEL directly to
3672 get color reference counts right. */
3674 unsigned long
3675 x_copy_dpy_color (dpy, cmap, pixel)
3676 Display *dpy;
3677 Colormap cmap;
3678 unsigned long pixel;
3680 XColor color;
3682 color.pixel = pixel;
3683 BLOCK_INPUT;
3684 XQueryColor (dpy, cmap, &color);
3685 XAllocColor (dpy, cmap, &color);
3686 UNBLOCK_INPUT;
3687 #ifdef DEBUG_X_COLORS
3688 register_color (pixel);
3689 #endif
3690 return color.pixel;
3694 /* Brightness beyond which a color won't have its highlight brightness
3695 boosted.
3697 Nominally, highlight colors for `3d' faces are calculated by
3698 brightening an object's color by a constant scale factor, but this
3699 doesn't yield good results for dark colors, so for colors who's
3700 brightness is less than this value (on a scale of 0-65535) have an
3701 use an additional additive factor.
3703 The value here is set so that the default menu-bar/mode-line color
3704 (grey75) will not have its highlights changed at all. */
3705 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3708 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3709 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3710 If this produces the same color as PIXEL, try a color where all RGB
3711 values have DELTA added. Return the allocated color in *PIXEL.
3712 DISPLAY is the X display, CMAP is the colormap to operate on.
3713 Value is non-zero if successful. */
3715 static int
3716 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3717 struct frame *f;
3718 Display *display;
3719 Colormap cmap;
3720 unsigned long *pixel;
3721 double factor;
3722 int delta;
3724 XColor color, new;
3725 long bright;
3726 int success_p;
3728 /* Get RGB color values. */
3729 color.pixel = *pixel;
3730 x_query_color (f, &color);
3732 /* Change RGB values by specified FACTOR. Avoid overflow! */
3733 xassert (factor >= 0);
3734 new.red = min (0xffff, factor * color.red);
3735 new.green = min (0xffff, factor * color.green);
3736 new.blue = min (0xffff, factor * color.blue);
3738 /* Calculate brightness of COLOR. */
3739 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3741 /* We only boost colors that are darker than
3742 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3743 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3744 /* Make an additive adjustment to NEW, because it's dark enough so
3745 that scaling by FACTOR alone isn't enough. */
3747 /* How far below the limit this color is (0 - 1, 1 being darker). */
3748 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3749 /* The additive adjustment. */
3750 int min_delta = delta * dimness * factor / 2;
3752 if (factor < 1)
3754 new.red = max (0, new.red - min_delta);
3755 new.green = max (0, new.green - min_delta);
3756 new.blue = max (0, new.blue - min_delta);
3758 else
3760 new.red = min (0xffff, min_delta + new.red);
3761 new.green = min (0xffff, min_delta + new.green);
3762 new.blue = min (0xffff, min_delta + new.blue);
3766 /* Try to allocate the color. */
3767 success_p = x_alloc_nearest_color (f, cmap, &new);
3768 if (success_p)
3770 if (new.pixel == *pixel)
3772 /* If we end up with the same color as before, try adding
3773 delta to the RGB values. */
3774 x_free_colors (f, &new.pixel, 1);
3776 new.red = min (0xffff, delta + color.red);
3777 new.green = min (0xffff, delta + color.green);
3778 new.blue = min (0xffff, delta + color.blue);
3779 success_p = x_alloc_nearest_color (f, cmap, &new);
3781 else
3782 success_p = 1;
3783 *pixel = new.pixel;
3786 return success_p;
3790 /* Set up the foreground color for drawing relief lines of glyph
3791 string S. RELIEF is a pointer to a struct relief containing the GC
3792 with which lines will be drawn. Use a color that is FACTOR or
3793 DELTA lighter or darker than the relief's background which is found
3794 in S->f->output_data.x->relief_background. If such a color cannot
3795 be allocated, use DEFAULT_PIXEL, instead. */
3797 static void
3798 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3799 struct frame *f;
3800 struct relief *relief;
3801 double factor;
3802 int delta;
3803 unsigned long default_pixel;
3805 XGCValues xgcv;
3806 struct x_output *di = f->output_data.x;
3807 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3808 unsigned long pixel;
3809 unsigned long background = di->relief_background;
3810 Colormap cmap = FRAME_X_COLORMAP (f);
3811 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3812 Display *dpy = FRAME_X_DISPLAY (f);
3814 xgcv.graphics_exposures = False;
3815 xgcv.line_width = 1;
3817 /* Free previously allocated color. The color cell will be reused
3818 when it has been freed as many times as it was allocated, so this
3819 doesn't affect faces using the same colors. */
3820 if (relief->gc
3821 && relief->allocated_p)
3823 x_free_colors (f, &relief->pixel, 1);
3824 relief->allocated_p = 0;
3827 /* Allocate new color. */
3828 xgcv.foreground = default_pixel;
3829 pixel = background;
3830 if (dpyinfo->n_planes != 1
3831 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3833 relief->allocated_p = 1;
3834 xgcv.foreground = relief->pixel = pixel;
3837 if (relief->gc == 0)
3839 xgcv.stipple = dpyinfo->gray;
3840 mask |= GCStipple;
3841 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3843 else
3844 XChangeGC (dpy, relief->gc, mask, &xgcv);
3848 /* Set up colors for the relief lines around glyph string S. */
3850 static void
3851 x_setup_relief_colors (s)
3852 struct glyph_string *s;
3854 struct x_output *di = s->f->output_data.x;
3855 unsigned long color;
3857 if (s->face->use_box_color_for_shadows_p)
3858 color = s->face->box_color;
3859 else if (s->first_glyph->type == IMAGE_GLYPH
3860 && s->img->pixmap
3861 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3862 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3863 else
3865 XGCValues xgcv;
3867 /* Get the background color of the face. */
3868 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3869 color = xgcv.background;
3872 if (di->white_relief.gc == 0
3873 || color != di->relief_background)
3875 di->relief_background = color;
3876 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3877 WHITE_PIX_DEFAULT (s->f));
3878 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3879 BLACK_PIX_DEFAULT (s->f));
3884 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3885 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3886 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3887 relief. LEFT_P non-zero means draw a relief on the left side of
3888 the rectangle. RIGHT_P non-zero means draw a relief on the right
3889 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3890 when drawing. */
3892 static void
3893 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3894 raised_p, left_p, right_p, clip_rect)
3895 struct frame *f;
3896 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3897 XRectangle *clip_rect;
3899 Display *dpy = FRAME_X_DISPLAY (f);
3900 Window window = FRAME_X_WINDOW (f);
3901 int i;
3902 GC gc;
3904 if (raised_p)
3905 gc = f->output_data.x->white_relief.gc;
3906 else
3907 gc = f->output_data.x->black_relief.gc;
3908 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3910 /* Top. */
3911 for (i = 0; i < width; ++i)
3912 XDrawLine (dpy, window, gc,
3913 left_x + i * left_p, top_y + i,
3914 right_x + 1 - i * right_p, top_y + i);
3916 /* Left. */
3917 if (left_p)
3918 for (i = 0; i < width; ++i)
3919 XDrawLine (dpy, window, gc,
3920 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3922 XSetClipMask (dpy, gc, None);
3923 if (raised_p)
3924 gc = f->output_data.x->black_relief.gc;
3925 else
3926 gc = f->output_data.x->white_relief.gc;
3927 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3929 /* Bottom. */
3930 for (i = 0; i < width; ++i)
3931 XDrawLine (dpy, window, gc,
3932 left_x + i * left_p, bottom_y - i,
3933 right_x + 1 - i * right_p, bottom_y - i);
3935 /* Right. */
3936 if (right_p)
3937 for (i = 0; i < width; ++i)
3938 XDrawLine (dpy, window, gc,
3939 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3941 XSetClipMask (dpy, gc, None);
3945 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3946 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3947 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3948 left side of the rectangle. RIGHT_P non-zero means draw a line
3949 on the right side of the rectangle. CLIP_RECT is the clipping
3950 rectangle to use when drawing. */
3952 static void
3953 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3954 left_p, right_p, clip_rect)
3955 struct glyph_string *s;
3956 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3957 XRectangle *clip_rect;
3959 XGCValues xgcv;
3961 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3962 XSetForeground (s->display, s->gc, s->face->box_color);
3963 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3965 /* Top. */
3966 XFillRectangle (s->display, s->window, s->gc,
3967 left_x, top_y, right_x - left_x + 1, width);
3969 /* Left. */
3970 if (left_p)
3971 XFillRectangle (s->display, s->window, s->gc,
3972 left_x, top_y, width, bottom_y - top_y + 1);
3974 /* Bottom. */
3975 XFillRectangle (s->display, s->window, s->gc,
3976 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3978 /* Right. */
3979 if (right_p)
3980 XFillRectangle (s->display, s->window, s->gc,
3981 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3983 XSetForeground (s->display, s->gc, xgcv.foreground);
3984 XSetClipMask (s->display, s->gc, None);
3988 /* Draw a box around glyph string S. */
3990 static void
3991 x_draw_glyph_string_box (s)
3992 struct glyph_string *s;
3994 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3995 int left_p, right_p;
3996 struct glyph *last_glyph;
3997 XRectangle clip_rect;
3999 last_x = window_box_right (s->w, s->area);
4000 if (s->row->full_width_p
4001 && !s->w->pseudo_window_p)
4003 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
4004 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
4005 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
4008 /* The glyph that may have a right box line. */
4009 last_glyph = (s->cmp || s->img
4010 ? s->first_glyph
4011 : s->first_glyph + s->nchars - 1);
4013 width = abs (s->face->box_line_width);
4014 raised_p = s->face->box == FACE_RAISED_BOX;
4015 left_x = s->x;
4016 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
4017 ? last_x - 1
4018 : min (last_x, s->x + s->background_width) - 1);
4019 top_y = s->y;
4020 bottom_y = top_y + s->height - 1;
4022 left_p = (s->first_glyph->left_box_line_p
4023 || (s->hl == DRAW_MOUSE_FACE
4024 && (s->prev == NULL
4025 || s->prev->hl != s->hl)));
4026 right_p = (last_glyph->right_box_line_p
4027 || (s->hl == DRAW_MOUSE_FACE
4028 && (s->next == NULL
4029 || s->next->hl != s->hl)));
4031 x_get_glyph_string_clip_rect (s, &clip_rect);
4033 if (s->face->box == FACE_SIMPLE_BOX)
4034 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4035 left_p, right_p, &clip_rect);
4036 else
4038 x_setup_relief_colors (s);
4039 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4040 width, raised_p, left_p, right_p, &clip_rect);
4045 /* Draw foreground of image glyph string S. */
4047 static void
4048 x_draw_image_foreground (s)
4049 struct glyph_string *s;
4051 int x;
4052 int y = s->ybase - image_ascent (s->img, s->face);
4054 /* If first glyph of S has a left box line, start drawing it to the
4055 right of that line. */
4056 if (s->face->box != FACE_NO_BOX
4057 && s->first_glyph->left_box_line_p)
4058 x = s->x + abs (s->face->box_line_width);
4059 else
4060 x = s->x;
4062 /* If there is a margin around the image, adjust x- and y-position
4063 by that margin. */
4064 x += s->img->hmargin;
4065 y += s->img->vmargin;
4067 if (s->img->pixmap)
4069 if (s->img->mask)
4071 /* We can't set both a clip mask and use XSetClipRectangles
4072 because the latter also sets a clip mask. We also can't
4073 trust on the shape extension to be available
4074 (XShapeCombineRegion). So, compute the rectangle to draw
4075 manually. */
4076 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4077 | GCFunction);
4078 XGCValues xgcv;
4079 XRectangle clip_rect, image_rect, r;
4081 xgcv.clip_mask = s->img->mask;
4082 xgcv.clip_x_origin = x;
4083 xgcv.clip_y_origin = y;
4084 xgcv.function = GXcopy;
4085 XChangeGC (s->display, s->gc, mask, &xgcv);
4087 x_get_glyph_string_clip_rect (s, &clip_rect);
4088 image_rect.x = x;
4089 image_rect.y = y;
4090 image_rect.width = s->img->width;
4091 image_rect.height = s->img->height;
4092 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4093 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4094 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4096 else
4098 XRectangle clip_rect, image_rect, r;
4100 x_get_glyph_string_clip_rect (s, &clip_rect);
4101 image_rect.x = x;
4102 image_rect.y = y;
4103 image_rect.width = s->img->width;
4104 image_rect.height = s->img->height;
4105 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4106 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4107 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4109 /* When the image has a mask, we can expect that at
4110 least part of a mouse highlight or a block cursor will
4111 be visible. If the image doesn't have a mask, make
4112 a block cursor visible by drawing a rectangle around
4113 the image. I believe it's looking better if we do
4114 nothing here for mouse-face. */
4115 if (s->hl == DRAW_CURSOR)
4116 XDrawRectangle (s->display, s->window, s->gc, x, y,
4117 s->img->width - 1, s->img->height - 1);
4120 else
4121 /* Draw a rectangle if image could not be loaded. */
4122 XDrawRectangle (s->display, s->window, s->gc, x, y,
4123 s->img->width - 1, s->img->height - 1);
4127 /* Draw a relief around the image glyph string S. */
4129 static void
4130 x_draw_image_relief (s)
4131 struct glyph_string *s;
4133 int x0, y0, x1, y1, thick, raised_p;
4134 XRectangle r;
4135 int x;
4136 int y = s->ybase - image_ascent (s->img, s->face);
4138 /* If first glyph of S has a left box line, start drawing it to the
4139 right of that line. */
4140 if (s->face->box != FACE_NO_BOX
4141 && s->first_glyph->left_box_line_p)
4142 x = s->x + abs (s->face->box_line_width);
4143 else
4144 x = s->x;
4146 /* If there is a margin around the image, adjust x- and y-position
4147 by that margin. */
4148 x += s->img->hmargin;
4149 y += s->img->vmargin;
4151 if (s->hl == DRAW_IMAGE_SUNKEN
4152 || s->hl == DRAW_IMAGE_RAISED)
4154 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4155 raised_p = s->hl == DRAW_IMAGE_RAISED;
4157 else
4159 thick = abs (s->img->relief);
4160 raised_p = s->img->relief > 0;
4163 x0 = x - thick;
4164 y0 = y - thick;
4165 x1 = x + s->img->width + thick - 1;
4166 y1 = y + s->img->height + thick - 1;
4168 x_setup_relief_colors (s);
4169 x_get_glyph_string_clip_rect (s, &r);
4170 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4174 /* Draw the foreground of image glyph string S to PIXMAP. */
4176 static void
4177 x_draw_image_foreground_1 (s, pixmap)
4178 struct glyph_string *s;
4179 Pixmap pixmap;
4181 int x;
4182 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4184 /* If first glyph of S has a left box line, start drawing it to the
4185 right of that line. */
4186 if (s->face->box != FACE_NO_BOX
4187 && s->first_glyph->left_box_line_p)
4188 x = abs (s->face->box_line_width);
4189 else
4190 x = 0;
4192 /* If there is a margin around the image, adjust x- and y-position
4193 by that margin. */
4194 x += s->img->hmargin;
4195 y += s->img->vmargin;
4197 if (s->img->pixmap)
4199 if (s->img->mask)
4201 /* We can't set both a clip mask and use XSetClipRectangles
4202 because the latter also sets a clip mask. We also can't
4203 trust on the shape extension to be available
4204 (XShapeCombineRegion). So, compute the rectangle to draw
4205 manually. */
4206 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4207 | GCFunction);
4208 XGCValues xgcv;
4210 xgcv.clip_mask = s->img->mask;
4211 xgcv.clip_x_origin = x;
4212 xgcv.clip_y_origin = y;
4213 xgcv.function = GXcopy;
4214 XChangeGC (s->display, s->gc, mask, &xgcv);
4216 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4217 0, 0, s->img->width, s->img->height, x, y);
4218 XSetClipMask (s->display, s->gc, None);
4220 else
4222 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4223 0, 0, s->img->width, s->img->height, x, y);
4225 /* When the image has a mask, we can expect that at
4226 least part of a mouse highlight or a block cursor will
4227 be visible. If the image doesn't have a mask, make
4228 a block cursor visible by drawing a rectangle around
4229 the image. I believe it's looking better if we do
4230 nothing here for mouse-face. */
4231 if (s->hl == DRAW_CURSOR)
4232 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4233 s->img->width - 1, s->img->height - 1);
4236 else
4237 /* Draw a rectangle if image could not be loaded. */
4238 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4239 s->img->width - 1, s->img->height - 1);
4243 /* Draw part of the background of glyph string S. X, Y, W, and H
4244 give the rectangle to draw. */
4246 static void
4247 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4248 struct glyph_string *s;
4249 int x, y, w, h;
4251 if (s->stippled_p)
4253 /* Fill background with a stipple pattern. */
4254 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4255 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4256 XSetFillStyle (s->display, s->gc, FillSolid);
4258 else
4259 x_clear_glyph_string_rect (s, x, y, w, h);
4263 /* Draw image glyph string S.
4265 s->y
4266 s->x +-------------------------
4267 | s->face->box
4269 | +-------------------------
4270 | | s->img->margin
4272 | | +-------------------
4273 | | | the image
4277 static void
4278 x_draw_image_glyph_string (s)
4279 struct glyph_string *s;
4281 int x, y;
4282 int box_line_hwidth = abs (s->face->box_line_width);
4283 int box_line_vwidth = max (s->face->box_line_width, 0);
4284 int height;
4285 Pixmap pixmap = None;
4287 height = s->height - 2 * box_line_vwidth;
4289 /* Fill background with face under the image. Do it only if row is
4290 taller than image or if image has a clip mask to reduce
4291 flickering. */
4292 s->stippled_p = s->face->stipple != 0;
4293 if (height > s->img->height
4294 || s->img->hmargin
4295 || s->img->vmargin
4296 || s->img->mask
4297 || s->img->pixmap == 0
4298 || s->width != s->background_width)
4300 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4301 x = s->x + box_line_hwidth;
4302 else
4303 x = s->x;
4305 y = s->y + box_line_vwidth;
4307 if (s->img->mask)
4309 /* Create a pixmap as large as the glyph string. Fill it
4310 with the background color. Copy the image to it, using
4311 its mask. Copy the temporary pixmap to the display. */
4312 Screen *screen = FRAME_X_SCREEN (s->f);
4313 int depth = DefaultDepthOfScreen (screen);
4315 /* Create a pixmap as large as the glyph string. */
4316 pixmap = XCreatePixmap (s->display, s->window,
4317 s->background_width,
4318 s->height, depth);
4320 /* Don't clip in the following because we're working on the
4321 pixmap. */
4322 XSetClipMask (s->display, s->gc, None);
4324 /* Fill the pixmap with the background color/stipple. */
4325 if (s->stippled_p)
4327 /* Fill background with a stipple pattern. */
4328 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4329 XFillRectangle (s->display, pixmap, s->gc,
4330 0, 0, s->background_width, s->height);
4331 XSetFillStyle (s->display, s->gc, FillSolid);
4333 else
4335 XGCValues xgcv;
4336 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4337 &xgcv);
4338 XSetForeground (s->display, s->gc, xgcv.background);
4339 XFillRectangle (s->display, pixmap, s->gc,
4340 0, 0, s->background_width, s->height);
4341 XSetForeground (s->display, s->gc, xgcv.foreground);
4344 else
4345 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4347 s->background_filled_p = 1;
4350 /* Draw the foreground. */
4351 if (pixmap != None)
4353 x_draw_image_foreground_1 (s, pixmap);
4354 x_set_glyph_string_clipping (s);
4355 XCopyArea (s->display, pixmap, s->window, s->gc,
4356 0, 0, s->background_width, s->height, s->x, s->y);
4357 XFreePixmap (s->display, pixmap);
4359 else
4360 x_draw_image_foreground (s);
4362 /* If we must draw a relief around the image, do it. */
4363 if (s->img->relief
4364 || s->hl == DRAW_IMAGE_RAISED
4365 || s->hl == DRAW_IMAGE_SUNKEN)
4366 x_draw_image_relief (s);
4370 /* Draw stretch glyph string S. */
4372 static void
4373 x_draw_stretch_glyph_string (s)
4374 struct glyph_string *s;
4376 xassert (s->first_glyph->type == STRETCH_GLYPH);
4377 s->stippled_p = s->face->stipple != 0;
4379 if (s->hl == DRAW_CURSOR
4380 && !x_stretch_cursor_p)
4382 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4383 as wide as the stretch glyph. */
4384 int width = min (CANON_X_UNIT (s->f), s->background_width);
4386 /* Draw cursor. */
4387 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4389 /* Clear rest using the GC of the original non-cursor face. */
4390 if (width < s->background_width)
4392 int x = s->x + width, y = s->y;
4393 int w = s->background_width - width, h = s->height;
4394 XRectangle r;
4395 GC gc;
4397 if (s->row->mouse_face_p
4398 && cursor_in_mouse_face_p (s->w))
4400 x_set_mouse_face_gc (s);
4401 gc = s->gc;
4403 else
4404 gc = s->face->gc;
4406 x_get_glyph_string_clip_rect (s, &r);
4407 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4409 if (s->face->stipple)
4411 /* Fill background with a stipple pattern. */
4412 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4413 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4414 XSetFillStyle (s->display, gc, FillSolid);
4416 else
4418 XGCValues xgcv;
4419 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4420 XSetForeground (s->display, gc, xgcv.background);
4421 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4422 XSetForeground (s->display, gc, xgcv.foreground);
4426 else if (!s->background_filled_p)
4427 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4428 s->height);
4430 s->background_filled_p = 1;
4434 /* Draw glyph string S. */
4436 static void
4437 x_draw_glyph_string (s)
4438 struct glyph_string *s;
4440 int relief_drawn_p = 0;
4442 /* If S draws into the background of its successor, draw the
4443 background of the successor first so that S can draw into it.
4444 This makes S->next use XDrawString instead of XDrawImageString. */
4445 if (s->next && s->right_overhang && !s->for_overlaps_p)
4447 xassert (s->next->img == NULL);
4448 x_set_glyph_string_gc (s->next);
4449 x_set_glyph_string_clipping (s->next);
4450 x_draw_glyph_string_background (s->next, 1);
4453 /* Set up S->gc, set clipping and draw S. */
4454 x_set_glyph_string_gc (s);
4456 /* Draw relief (if any) in advance for char/composition so that the
4457 glyph string can be drawn over it. */
4458 if (!s->for_overlaps_p
4459 && s->face->box != FACE_NO_BOX
4460 && (s->first_glyph->type == CHAR_GLYPH
4461 || s->first_glyph->type == COMPOSITE_GLYPH))
4464 x_set_glyph_string_clipping (s);
4465 x_draw_glyph_string_background (s, 1);
4466 x_draw_glyph_string_box (s);
4467 x_set_glyph_string_clipping (s);
4468 relief_drawn_p = 1;
4470 else
4471 x_set_glyph_string_clipping (s);
4473 switch (s->first_glyph->type)
4475 case IMAGE_GLYPH:
4476 x_draw_image_glyph_string (s);
4477 break;
4479 case STRETCH_GLYPH:
4480 x_draw_stretch_glyph_string (s);
4481 break;
4483 case CHAR_GLYPH:
4484 if (s->for_overlaps_p)
4485 s->background_filled_p = 1;
4486 else
4487 x_draw_glyph_string_background (s, 0);
4488 x_draw_glyph_string_foreground (s);
4489 break;
4491 case COMPOSITE_GLYPH:
4492 if (s->for_overlaps_p || s->gidx > 0)
4493 s->background_filled_p = 1;
4494 else
4495 x_draw_glyph_string_background (s, 1);
4496 x_draw_composite_glyph_string_foreground (s);
4497 break;
4499 default:
4500 abort ();
4503 if (!s->for_overlaps_p)
4505 /* Draw underline. */
4506 if (s->face->underline_p)
4508 unsigned long tem, h;
4509 int y;
4511 /* Get the underline thickness. Default is 1 pixel. */
4512 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4513 h = 1;
4515 /* Get the underline position. This is the recommended
4516 vertical offset in pixels from the baseline to the top of
4517 the underline. This is a signed value according to the
4518 specs, and its default is
4520 ROUND ((maximum descent) / 2), with
4521 ROUND(x) = floor (x + 0.5) */
4523 if (x_use_underline_position_properties
4524 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4525 y = s->ybase + (long) tem;
4526 else if (s->face->font)
4527 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4528 else
4529 y = s->y + s->height - h;
4531 if (s->face->underline_defaulted_p)
4532 XFillRectangle (s->display, s->window, s->gc,
4533 s->x, y, s->width, h);
4534 else
4536 XGCValues xgcv;
4537 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4538 XSetForeground (s->display, s->gc, s->face->underline_color);
4539 XFillRectangle (s->display, s->window, s->gc,
4540 s->x, y, s->width, h);
4541 XSetForeground (s->display, s->gc, xgcv.foreground);
4545 /* Draw overline. */
4546 if (s->face->overline_p)
4548 unsigned long dy = 0, h = 1;
4550 if (s->face->overline_color_defaulted_p)
4551 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4552 s->width, h);
4553 else
4555 XGCValues xgcv;
4556 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4557 XSetForeground (s->display, s->gc, s->face->overline_color);
4558 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4559 s->width, h);
4560 XSetForeground (s->display, s->gc, xgcv.foreground);
4564 /* Draw strike-through. */
4565 if (s->face->strike_through_p)
4567 unsigned long h = 1;
4568 unsigned long dy = (s->height - h) / 2;
4570 if (s->face->strike_through_color_defaulted_p)
4571 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4572 s->width, h);
4573 else
4575 XGCValues xgcv;
4576 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4577 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4578 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4579 s->width, h);
4580 XSetForeground (s->display, s->gc, xgcv.foreground);
4584 /* Draw relief if not yet drawn. */
4585 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4586 x_draw_glyph_string_box (s);
4589 /* Reset clipping. */
4590 XSetClipMask (s->display, s->gc, None);
4594 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4595 struct face **, int));
4598 /* Fill glyph string S with composition components specified by S->cmp.
4600 FACES is an array of faces for all components of this composition.
4601 S->gidx is the index of the first component for S.
4602 OVERLAPS_P non-zero means S should draw the foreground only, and
4603 use its physical height for clipping.
4605 Value is the index of a component not in S. */
4607 static int
4608 x_fill_composite_glyph_string (s, faces, overlaps_p)
4609 struct glyph_string *s;
4610 struct face **faces;
4611 int overlaps_p;
4613 int i;
4615 xassert (s);
4617 s->for_overlaps_p = overlaps_p;
4619 s->face = faces[s->gidx];
4620 s->font = s->face->font;
4621 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4623 /* For all glyphs of this composition, starting at the offset
4624 S->gidx, until we reach the end of the definition or encounter a
4625 glyph that requires the different face, add it to S. */
4626 ++s->nchars;
4627 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4628 ++s->nchars;
4630 /* All glyph strings for the same composition has the same width,
4631 i.e. the width set for the first component of the composition. */
4633 s->width = s->first_glyph->pixel_width;
4635 /* If the specified font could not be loaded, use the frame's
4636 default font, but record the fact that we couldn't load it in
4637 the glyph string so that we can draw rectangles for the
4638 characters of the glyph string. */
4639 if (s->font == NULL)
4641 s->font_not_found_p = 1;
4642 s->font = FRAME_FONT (s->f);
4645 /* Adjust base line for subscript/superscript text. */
4646 s->ybase += s->first_glyph->voffset;
4648 xassert (s->face && s->face->gc);
4650 /* This glyph string must always be drawn with 16-bit functions. */
4651 s->two_byte_p = 1;
4653 return s->gidx + s->nchars;
4657 /* Fill glyph string S from a sequence of character glyphs.
4659 FACE_ID is the face id of the string. START is the index of the
4660 first glyph to consider, END is the index of the last + 1.
4661 OVERLAPS_P non-zero means S should draw the foreground only, and
4662 use its physical height for clipping.
4664 Value is the index of the first glyph not in S. */
4666 static int
4667 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4668 struct glyph_string *s;
4669 int face_id;
4670 int start, end, overlaps_p;
4672 struct glyph *glyph, *last;
4673 int voffset;
4674 int glyph_not_available_p;
4676 xassert (s->f == XFRAME (s->w->frame));
4677 xassert (s->nchars == 0);
4678 xassert (start >= 0 && end > start);
4680 s->for_overlaps_p = overlaps_p,
4681 glyph = s->row->glyphs[s->area] + start;
4682 last = s->row->glyphs[s->area] + end;
4683 voffset = glyph->voffset;
4685 glyph_not_available_p = glyph->glyph_not_available_p;
4687 while (glyph < last
4688 && glyph->type == CHAR_GLYPH
4689 && glyph->voffset == voffset
4690 /* Same face id implies same font, nowadays. */
4691 && glyph->face_id == face_id
4692 && glyph->glyph_not_available_p == glyph_not_available_p)
4694 int two_byte_p;
4696 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4697 s->char2b + s->nchars,
4698 &two_byte_p);
4699 s->two_byte_p = two_byte_p;
4700 ++s->nchars;
4701 xassert (s->nchars <= end - start);
4702 s->width += glyph->pixel_width;
4703 ++glyph;
4706 s->font = s->face->font;
4707 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4709 /* If the specified font could not be loaded, use the frame's font,
4710 but record the fact that we couldn't load it in
4711 S->font_not_found_p so that we can draw rectangles for the
4712 characters of the glyph string. */
4713 if (s->font == NULL || glyph_not_available_p)
4715 s->font_not_found_p = 1;
4716 s->font = FRAME_FONT (s->f);
4719 /* Adjust base line for subscript/superscript text. */
4720 s->ybase += voffset;
4722 xassert (s->face && s->face->gc);
4723 return glyph - s->row->glyphs[s->area];
4727 /* Fill glyph string S from image glyph S->first_glyph. */
4729 static void
4730 x_fill_image_glyph_string (s)
4731 struct glyph_string *s;
4733 xassert (s->first_glyph->type == IMAGE_GLYPH);
4734 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4735 xassert (s->img);
4736 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4737 s->font = s->face->font;
4738 s->width = s->first_glyph->pixel_width;
4740 /* Adjust base line for subscript/superscript text. */
4741 s->ybase += s->first_glyph->voffset;
4745 /* Fill glyph string S from a sequence of stretch glyphs.
4747 ROW is the glyph row in which the glyphs are found, AREA is the
4748 area within the row. START is the index of the first glyph to
4749 consider, END is the index of the last + 1.
4751 Value is the index of the first glyph not in S. */
4753 static int
4754 x_fill_stretch_glyph_string (s, row, area, start, end)
4755 struct glyph_string *s;
4756 struct glyph_row *row;
4757 enum glyph_row_area area;
4758 int start, end;
4760 struct glyph *glyph, *last;
4761 int voffset, face_id;
4763 xassert (s->first_glyph->type == STRETCH_GLYPH);
4765 glyph = s->row->glyphs[s->area] + start;
4766 last = s->row->glyphs[s->area] + end;
4767 face_id = glyph->face_id;
4768 s->face = FACE_FROM_ID (s->f, face_id);
4769 s->font = s->face->font;
4770 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4771 s->width = glyph->pixel_width;
4772 voffset = glyph->voffset;
4774 for (++glyph;
4775 (glyph < last
4776 && glyph->type == STRETCH_GLYPH
4777 && glyph->voffset == voffset
4778 && glyph->face_id == face_id);
4779 ++glyph)
4780 s->width += glyph->pixel_width;
4782 /* Adjust base line for subscript/superscript text. */
4783 s->ybase += voffset;
4785 /* The case that face->gc == 0 is handled when drawing the glyph
4786 string by calling PREPARE_FACE_FOR_DISPLAY. */
4787 xassert (s->face);
4788 return glyph - s->row->glyphs[s->area];
4792 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4793 of XChar2b structures for S; it can't be allocated in
4794 x_init_glyph_string because it must be allocated via `alloca'. W
4795 is the window on which S is drawn. ROW and AREA are the glyph row
4796 and area within the row from which S is constructed. START is the
4797 index of the first glyph structure covered by S. HL is a
4798 face-override for drawing S. */
4800 static void
4801 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4802 struct glyph_string *s;
4803 XChar2b *char2b;
4804 struct window *w;
4805 struct glyph_row *row;
4806 enum glyph_row_area area;
4807 int start;
4808 enum draw_glyphs_face hl;
4810 bzero (s, sizeof *s);
4811 s->w = w;
4812 s->f = XFRAME (w->frame);
4813 s->display = FRAME_X_DISPLAY (s->f);
4814 s->window = FRAME_X_WINDOW (s->f);
4815 s->char2b = char2b;
4816 s->hl = hl;
4817 s->row = row;
4818 s->area = area;
4819 s->first_glyph = row->glyphs[area] + start;
4820 s->height = row->height;
4821 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4823 /* Display the internal border below the tool-bar window. */
4824 if (s->w == XWINDOW (s->f->tool_bar_window))
4825 s->y -= s->f->output_data.x->internal_border_width;
4827 s->ybase = s->y + row->ascent;
4831 /* Set background width of glyph string S. START is the index of the
4832 first glyph following S. LAST_X is the right-most x-position + 1
4833 in the drawing area. */
4835 static INLINE void
4836 x_set_glyph_string_background_width (s, start, last_x)
4837 struct glyph_string *s;
4838 int start;
4839 int last_x;
4841 /* If the face of this glyph string has to be drawn to the end of
4842 the drawing area, set S->extends_to_end_of_line_p. */
4843 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4845 if (start == s->row->used[s->area]
4846 && s->area == TEXT_AREA
4847 && ((s->hl == DRAW_NORMAL_TEXT
4848 && (s->row->fill_line_p
4849 || s->face->background != default_face->background
4850 || s->face->stipple != default_face->stipple
4851 || s->row->mouse_face_p))
4852 || s->hl == DRAW_MOUSE_FACE
4853 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4854 && s->row->fill_line_p)))
4855 s->extends_to_end_of_line_p = 1;
4857 /* If S extends its face to the end of the line, set its
4858 background_width to the distance to the right edge of the drawing
4859 area. */
4860 if (s->extends_to_end_of_line_p)
4861 s->background_width = last_x - s->x + 1;
4862 else
4863 s->background_width = s->width;
4867 /* Add a glyph string for a stretch glyph to the list of strings
4868 between HEAD and TAIL. START is the index of the stretch glyph in
4869 row area AREA of glyph row ROW. END is the index of the last glyph
4870 in that glyph row area. X is the current output position assigned
4871 to the new glyph string constructed. HL overrides that face of the
4872 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4873 is the right-most x-position of the drawing area. */
4875 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4876 and below -- keep them on one line. */
4877 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4878 do \
4880 s = (struct glyph_string *) alloca (sizeof *s); \
4881 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4882 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4883 x_append_glyph_string (&HEAD, &TAIL, s); \
4884 s->x = (X); \
4886 while (0)
4889 /* Add a glyph string for an image glyph to the list of strings
4890 between HEAD and TAIL. START is the index of the image glyph in
4891 row area AREA of glyph row ROW. END is the index of the last glyph
4892 in that glyph row area. X is the current output position assigned
4893 to the new glyph string constructed. HL overrides that face of the
4894 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4895 is the right-most x-position of the drawing area. */
4897 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4898 do \
4900 s = (struct glyph_string *) alloca (sizeof *s); \
4901 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4902 x_fill_image_glyph_string (s); \
4903 x_append_glyph_string (&HEAD, &TAIL, s); \
4904 ++START; \
4905 s->x = (X); \
4907 while (0)
4910 /* Add a glyph string for a sequence of character glyphs to the list
4911 of strings between HEAD and TAIL. START is the index of the first
4912 glyph in row area AREA of glyph row ROW that is part of the new
4913 glyph string. END is the index of the last glyph in that glyph row
4914 area. X is the current output position assigned to the new glyph
4915 string constructed. HL overrides that face of the glyph; e.g. it
4916 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4917 right-most x-position of the drawing area. */
4919 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4920 do \
4922 int c, face_id; \
4923 XChar2b *char2b; \
4925 c = (ROW)->glyphs[AREA][START].u.ch; \
4926 face_id = (ROW)->glyphs[AREA][START].face_id; \
4928 s = (struct glyph_string *) alloca (sizeof *s); \
4929 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4930 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4931 x_append_glyph_string (&HEAD, &TAIL, s); \
4932 s->x = (X); \
4933 START = x_fill_glyph_string (s, face_id, START, END, \
4934 OVERLAPS_P); \
4936 while (0)
4939 /* Add a glyph string for a composite sequence to the list of strings
4940 between HEAD and TAIL. START is the index of the first glyph in
4941 row area AREA of glyph row ROW that is part of the new glyph
4942 string. END is the index of the last glyph in that glyph row area.
4943 X is the current output position assigned to the new glyph string
4944 constructed. HL overrides that face of the glyph; e.g. it is
4945 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4946 x-position of the drawing area. */
4948 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4949 do { \
4950 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4951 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4952 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4953 struct composition *cmp = composition_table[cmp_id]; \
4954 int glyph_len = cmp->glyph_len; \
4955 XChar2b *char2b; \
4956 struct face **faces; \
4957 struct glyph_string *first_s = NULL; \
4958 int n; \
4960 base_face = base_face->ascii_face; \
4961 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4962 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4963 /* At first, fill in `char2b' and `faces'. */ \
4964 for (n = 0; n < glyph_len; n++) \
4966 int c = COMPOSITION_GLYPH (cmp, n); \
4967 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4968 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4969 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4970 this_face_id, char2b + n, 1, 1); \
4973 /* Make glyph_strings for each glyph sequence that is drawable by \
4974 the same face, and append them to HEAD/TAIL. */ \
4975 for (n = 0; n < cmp->glyph_len;) \
4977 s = (struct glyph_string *) alloca (sizeof *s); \
4978 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4979 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4980 s->cmp = cmp; \
4981 s->gidx = n; \
4982 s->x = (X); \
4984 if (n == 0) \
4985 first_s = s; \
4987 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4990 ++START; \
4991 s = first_s; \
4992 } while (0)
4995 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4996 of AREA of glyph row ROW on window W between indices START and END.
4997 HL overrides the face for drawing glyph strings, e.g. it is
4998 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4999 x-positions of the drawing area.
5001 This is an ugly monster macro construct because we must use alloca
5002 to allocate glyph strings (because x_draw_glyphs can be called
5003 asynchronously). */
5005 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
5006 do \
5008 HEAD = TAIL = NULL; \
5009 while (START < END) \
5011 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
5012 switch (first_glyph->type) \
5014 case CHAR_GLYPH: \
5015 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
5016 TAIL, HL, X, LAST_X, \
5017 OVERLAPS_P); \
5018 break; \
5020 case COMPOSITE_GLYPH: \
5021 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5022 HEAD, TAIL, HL, X, LAST_X,\
5023 OVERLAPS_P); \
5024 break; \
5026 case STRETCH_GLYPH: \
5027 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5028 HEAD, TAIL, HL, X, LAST_X); \
5029 break; \
5031 case IMAGE_GLYPH: \
5032 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5033 TAIL, HL, X, LAST_X); \
5034 break; \
5036 default: \
5037 abort (); \
5040 x_set_glyph_string_background_width (s, START, LAST_X); \
5041 (X) += s->width; \
5044 while (0)
5047 /* Draw glyphs between START and END in AREA of ROW on window W,
5048 starting at x-position X. X is relative to AREA in W. HL is a
5049 face-override with the following meaning:
5051 DRAW_NORMAL_TEXT draw normally
5052 DRAW_CURSOR draw in cursor face
5053 DRAW_MOUSE_FACE draw in mouse face.
5054 DRAW_INVERSE_VIDEO draw in mode line face
5055 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5056 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5058 If OVERLAPS_P is non-zero, draw only the foreground of characters
5059 and clip to the physical height of ROW.
5061 Value is the x-position reached, relative to AREA of W. */
5063 static int
5064 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5065 struct window *w;
5066 int x;
5067 struct glyph_row *row;
5068 enum glyph_row_area area;
5069 int start, end;
5070 enum draw_glyphs_face hl;
5071 int overlaps_p;
5073 struct glyph_string *head, *tail;
5074 struct glyph_string *s;
5075 int last_x, area_width;
5076 int x_reached;
5077 int i, j;
5079 /* Let's rather be paranoid than getting a SEGV. */
5080 end = min (end, row->used[area]);
5081 start = max (0, start);
5082 start = min (end, start);
5084 /* Translate X to frame coordinates. Set last_x to the right
5085 end of the drawing area. */
5086 if (row->full_width_p)
5088 /* X is relative to the left edge of W, without scroll bars
5089 or fringes. */
5090 struct frame *f = XFRAME (w->frame);
5091 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5093 x += window_left_x;
5094 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5095 last_x = window_left_x + area_width;
5097 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5099 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5100 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5101 last_x += width;
5102 else
5103 x -= width;
5106 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5107 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5109 else
5111 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5112 area_width = window_box_width (w, area);
5113 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5116 /* Build a doubly-linked list of glyph_string structures between
5117 head and tail from what we have to draw. Note that the macro
5118 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5119 the reason we use a separate variable `i'. */
5120 i = start;
5121 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5122 overlaps_p);
5123 if (tail)
5124 x_reached = tail->x + tail->background_width;
5125 else
5126 x_reached = x;
5128 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5129 the row, redraw some glyphs in front or following the glyph
5130 strings built above. */
5131 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5133 int dummy_x = 0;
5134 struct glyph_string *h, *t;
5136 /* Compute overhangs for all glyph strings. */
5137 for (s = head; s; s = s->next)
5138 x_compute_glyph_string_overhangs (s);
5140 /* Prepend glyph strings for glyphs in front of the first glyph
5141 string that are overwritten because of the first glyph
5142 string's left overhang. The background of all strings
5143 prepended must be drawn because the first glyph string
5144 draws over it. */
5145 i = x_left_overwritten (head);
5146 if (i >= 0)
5148 j = i;
5149 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5150 DRAW_NORMAL_TEXT, dummy_x, last_x,
5151 overlaps_p);
5152 start = i;
5153 x_compute_overhangs_and_x (t, head->x, 1);
5154 x_prepend_glyph_string_lists (&head, &tail, h, t);
5157 /* Prepend glyph strings for glyphs in front of the first glyph
5158 string that overwrite that glyph string because of their
5159 right overhang. For these strings, only the foreground must
5160 be drawn, because it draws over the glyph string at `head'.
5161 The background must not be drawn because this would overwrite
5162 right overhangs of preceding glyphs for which no glyph
5163 strings exist. */
5164 i = x_left_overwriting (head);
5165 if (i >= 0)
5167 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5168 DRAW_NORMAL_TEXT, dummy_x, last_x,
5169 overlaps_p);
5170 for (s = h; s; s = s->next)
5171 s->background_filled_p = 1;
5172 x_compute_overhangs_and_x (t, head->x, 1);
5173 x_prepend_glyph_string_lists (&head, &tail, h, t);
5176 /* Append glyphs strings for glyphs following the last glyph
5177 string tail that are overwritten by tail. The background of
5178 these strings has to be drawn because tail's foreground draws
5179 over it. */
5180 i = x_right_overwritten (tail);
5181 if (i >= 0)
5183 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5184 DRAW_NORMAL_TEXT, x, last_x,
5185 overlaps_p);
5186 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5187 x_append_glyph_string_lists (&head, &tail, h, t);
5190 /* Append glyph strings for glyphs following the last glyph
5191 string tail that overwrite tail. The foreground of such
5192 glyphs has to be drawn because it writes into the background
5193 of tail. The background must not be drawn because it could
5194 paint over the foreground of following glyphs. */
5195 i = x_right_overwriting (tail);
5196 if (i >= 0)
5198 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5199 DRAW_NORMAL_TEXT, x, last_x,
5200 overlaps_p);
5201 for (s = h; s; s = s->next)
5202 s->background_filled_p = 1;
5203 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5204 x_append_glyph_string_lists (&head, &tail, h, t);
5208 /* Draw all strings. */
5209 for (s = head; s; s = s->next)
5210 x_draw_glyph_string (s);
5212 if (area == TEXT_AREA
5213 && !row->full_width_p
5214 /* When drawing overlapping rows, only the glyph strings'
5215 foreground is drawn, which doesn't erase a cursor
5216 completely. */
5217 && !overlaps_p)
5219 int x0 = head ? head->x : x;
5220 int x1 = tail ? tail->x + tail->background_width : x;
5222 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5223 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5225 if (XFASTINT (w->left_margin_width) != 0)
5227 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5228 x0 -= left_area_width;
5229 x1 -= left_area_width;
5232 notice_overwritten_cursor (w, area, x0, x1,
5233 row->y, MATRIX_ROW_BOTTOM_Y (row));
5236 /* Value is the x-position up to which drawn, relative to AREA of W.
5237 This doesn't include parts drawn because of overhangs. */
5238 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5239 if (!row->full_width_p)
5241 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5242 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5243 if (area > TEXT_AREA)
5244 x_reached -= window_box_width (w, TEXT_AREA);
5247 return x_reached;
5251 /* Fix the display of area AREA of overlapping row ROW in window W. */
5253 static void
5254 x_fix_overlapping_area (w, row, area)
5255 struct window *w;
5256 struct glyph_row *row;
5257 enum glyph_row_area area;
5259 int i, x;
5261 BLOCK_INPUT;
5263 if (area == LEFT_MARGIN_AREA)
5264 x = 0;
5265 else if (area == TEXT_AREA)
5266 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5267 else
5268 x = (window_box_width (w, LEFT_MARGIN_AREA)
5269 + window_box_width (w, TEXT_AREA));
5271 for (i = 0; i < row->used[area];)
5273 if (row->glyphs[area][i].overlaps_vertically_p)
5275 int start = i, start_x = x;
5279 x += row->glyphs[area][i].pixel_width;
5280 ++i;
5282 while (i < row->used[area]
5283 && row->glyphs[area][i].overlaps_vertically_p);
5285 x_draw_glyphs (w, start_x, row, area, start, i,
5286 DRAW_NORMAL_TEXT, 1);
5288 else
5290 x += row->glyphs[area][i].pixel_width;
5291 ++i;
5295 UNBLOCK_INPUT;
5299 /* Output LEN glyphs starting at START at the nominal cursor position.
5300 Advance the nominal cursor over the text. The global variable
5301 updated_window contains the window being updated, updated_row is
5302 the glyph row being updated, and updated_area is the area of that
5303 row being updated. */
5305 static void
5306 x_write_glyphs (start, len)
5307 struct glyph *start;
5308 int len;
5310 int x, hpos;
5312 xassert (updated_window && updated_row);
5313 BLOCK_INPUT;
5315 /* Write glyphs. */
5317 hpos = start - updated_row->glyphs[updated_area];
5318 x = x_draw_glyphs (updated_window, output_cursor.x,
5319 updated_row, updated_area,
5320 hpos, hpos + len,
5321 DRAW_NORMAL_TEXT, 0);
5323 UNBLOCK_INPUT;
5325 /* Advance the output cursor. */
5326 output_cursor.hpos += len;
5327 output_cursor.x = x;
5331 /* Insert LEN glyphs from START at the nominal cursor position. */
5333 static void
5334 x_insert_glyphs (start, len)
5335 struct glyph *start;
5336 register int len;
5338 struct frame *f;
5339 struct window *w;
5340 int line_height, shift_by_width, shifted_region_width;
5341 struct glyph_row *row;
5342 struct glyph *glyph;
5343 int frame_x, frame_y, hpos;
5345 xassert (updated_window && updated_row);
5346 BLOCK_INPUT;
5347 w = updated_window;
5348 f = XFRAME (WINDOW_FRAME (w));
5350 /* Get the height of the line we are in. */
5351 row = updated_row;
5352 line_height = row->height;
5354 /* Get the width of the glyphs to insert. */
5355 shift_by_width = 0;
5356 for (glyph = start; glyph < start + len; ++glyph)
5357 shift_by_width += glyph->pixel_width;
5359 /* Get the width of the region to shift right. */
5360 shifted_region_width = (window_box_width (w, updated_area)
5361 - output_cursor.x
5362 - shift_by_width);
5364 /* Shift right. */
5365 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5366 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5367 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5368 f->output_data.x->normal_gc,
5369 frame_x, frame_y,
5370 shifted_region_width, line_height,
5371 frame_x + shift_by_width, frame_y);
5373 /* Write the glyphs. */
5374 hpos = start - row->glyphs[updated_area];
5375 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5376 DRAW_NORMAL_TEXT, 0);
5378 /* Advance the output cursor. */
5379 output_cursor.hpos += len;
5380 output_cursor.x += shift_by_width;
5381 UNBLOCK_INPUT;
5385 /* Delete N glyphs at the nominal cursor position. Not implemented
5386 for X frames. */
5388 static void
5389 x_delete_glyphs (n)
5390 register int n;
5392 abort ();
5396 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5397 If they are <= 0, this is probably an error. */
5399 void
5400 x_clear_area (dpy, window, x, y, width, height, exposures)
5401 Display *dpy;
5402 Window window;
5403 int x, y;
5404 int width, height;
5405 int exposures;
5407 xassert (width > 0 && height > 0);
5408 XClearArea (dpy, window, x, y, width, height, exposures);
5412 /* Erase the current text line from the nominal cursor position
5413 (inclusive) to pixel column TO_X (exclusive). The idea is that
5414 everything from TO_X onward is already erased.
5416 TO_X is a pixel position relative to updated_area of
5417 updated_window. TO_X == -1 means clear to the end of this area. */
5419 static void
5420 x_clear_end_of_line (to_x)
5421 int to_x;
5423 struct frame *f;
5424 struct window *w = updated_window;
5425 int max_x, min_y, max_y;
5426 int from_x, from_y, to_y;
5428 xassert (updated_window && updated_row);
5429 f = XFRAME (w->frame);
5431 if (updated_row->full_width_p)
5433 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5434 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5435 && !w->pseudo_window_p)
5436 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5438 else
5439 max_x = window_box_width (w, updated_area);
5440 max_y = window_text_bottom_y (w);
5442 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5443 of window. For TO_X > 0, truncate to end of drawing area. */
5444 if (to_x == 0)
5445 return;
5446 else if (to_x < 0)
5447 to_x = max_x;
5448 else
5449 to_x = min (to_x, max_x);
5451 to_y = min (max_y, output_cursor.y + updated_row->height);
5453 /* Notice if the cursor will be cleared by this operation. */
5454 if (!updated_row->full_width_p)
5455 notice_overwritten_cursor (w, updated_area,
5456 output_cursor.x, -1,
5457 updated_row->y,
5458 MATRIX_ROW_BOTTOM_Y (updated_row));
5460 from_x = output_cursor.x;
5462 /* Translate to frame coordinates. */
5463 if (updated_row->full_width_p)
5465 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5466 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5468 else
5470 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5471 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5474 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5475 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5476 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5478 /* Prevent inadvertently clearing to end of the X window. */
5479 if (to_x > from_x && to_y > from_y)
5481 BLOCK_INPUT;
5482 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5483 from_x, from_y, to_x - from_x, to_y - from_y,
5484 False);
5485 UNBLOCK_INPUT;
5490 /* Clear entire frame. If updating_frame is non-null, clear that
5491 frame. Otherwise clear the selected frame. */
5493 static void
5494 x_clear_frame ()
5496 struct frame *f;
5498 if (updating_frame)
5499 f = updating_frame;
5500 else
5501 f = SELECTED_FRAME ();
5503 /* Clearing the frame will erase any cursor, so mark them all as no
5504 longer visible. */
5505 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5506 output_cursor.hpos = output_cursor.vpos = 0;
5507 output_cursor.x = -1;
5509 /* We don't set the output cursor here because there will always
5510 follow an explicit cursor_to. */
5511 BLOCK_INPUT;
5512 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5514 /* We have to clear the scroll bars, too. If we have changed
5515 colors or something like that, then they should be notified. */
5516 x_scroll_bar_clear (f);
5518 XFlush (FRAME_X_DISPLAY (f));
5519 UNBLOCK_INPUT;
5524 /* Invert the middle quarter of the frame for .15 sec. */
5526 /* We use the select system call to do the waiting, so we have to make
5527 sure it's available. If it isn't, we just won't do visual bells. */
5529 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5532 /* Subtract the `struct timeval' values X and Y, storing the result in
5533 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5535 static int
5536 timeval_subtract (result, x, y)
5537 struct timeval *result, x, y;
5539 /* Perform the carry for the later subtraction by updating y. This
5540 is safer because on some systems the tv_sec member is unsigned. */
5541 if (x.tv_usec < y.tv_usec)
5543 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5544 y.tv_usec -= 1000000 * nsec;
5545 y.tv_sec += nsec;
5548 if (x.tv_usec - y.tv_usec > 1000000)
5550 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5551 y.tv_usec += 1000000 * nsec;
5552 y.tv_sec -= nsec;
5555 /* Compute the time remaining to wait. tv_usec is certainly
5556 positive. */
5557 result->tv_sec = x.tv_sec - y.tv_sec;
5558 result->tv_usec = x.tv_usec - y.tv_usec;
5560 /* Return indication of whether the result should be considered
5561 negative. */
5562 return x.tv_sec < y.tv_sec;
5565 void
5566 XTflash (f)
5567 struct frame *f;
5569 BLOCK_INPUT;
5572 GC gc;
5574 /* Create a GC that will use the GXxor function to flip foreground
5575 pixels into background pixels. */
5577 XGCValues values;
5579 values.function = GXxor;
5580 values.foreground = (f->output_data.x->foreground_pixel
5581 ^ f->output_data.x->background_pixel);
5583 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5584 GCFunction | GCForeground, &values);
5588 /* Get the height not including a menu bar widget. */
5589 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5590 /* Height of each line to flash. */
5591 int flash_height = FRAME_LINE_HEIGHT (f);
5592 /* These will be the left and right margins of the rectangles. */
5593 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5594 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5596 int width;
5598 /* Don't flash the area between a scroll bar and the frame
5599 edge it is next to. */
5600 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5602 case vertical_scroll_bar_left:
5603 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5604 break;
5606 case vertical_scroll_bar_right:
5607 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5608 break;
5610 default:
5611 break;
5614 width = flash_right - flash_left;
5616 /* If window is tall, flash top and bottom line. */
5617 if (height > 3 * FRAME_LINE_HEIGHT (f))
5619 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5620 flash_left,
5621 (FRAME_INTERNAL_BORDER_WIDTH (f)
5622 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5623 width, flash_height);
5624 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5625 flash_left,
5626 (height - flash_height
5627 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5628 width, flash_height);
5630 else
5631 /* If it is short, flash it all. */
5632 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5633 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5634 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5636 x_flush (f);
5639 struct timeval wakeup;
5641 EMACS_GET_TIME (wakeup);
5643 /* Compute time to wait until, propagating carry from usecs. */
5644 wakeup.tv_usec += 150000;
5645 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5646 wakeup.tv_usec %= 1000000;
5648 /* Keep waiting until past the time wakeup or any input gets
5649 available. */
5650 while (! detect_input_pending ())
5652 struct timeval current;
5653 struct timeval timeout;
5655 EMACS_GET_TIME (current);
5657 /* Break if result would be negative. */
5658 if (timeval_subtract (&current, wakeup, current))
5659 break;
5661 /* How long `select' should wait. */
5662 timeout.tv_sec = 0;
5663 timeout.tv_usec = 10000;
5665 /* Try to wait that long--but we might wake up sooner. */
5666 select (0, NULL, NULL, NULL, &timeout);
5670 /* If window is tall, flash top and bottom line. */
5671 if (height > 3 * FRAME_LINE_HEIGHT (f))
5673 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5674 flash_left,
5675 (FRAME_INTERNAL_BORDER_WIDTH (f)
5676 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5677 width, flash_height);
5678 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5679 flash_left,
5680 (height - flash_height
5681 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5682 width, flash_height);
5684 else
5685 /* If it is short, flash it all. */
5686 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5687 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5688 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5690 XFreeGC (FRAME_X_DISPLAY (f), gc);
5691 x_flush (f);
5695 UNBLOCK_INPUT;
5698 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5701 /* Make audible bell. */
5703 void
5704 XTring_bell ()
5706 struct frame *f = SELECTED_FRAME ();
5708 if (FRAME_X_DISPLAY (f))
5710 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5711 if (visible_bell)
5712 XTflash (f);
5713 else
5714 #endif
5716 BLOCK_INPUT;
5717 XBell (FRAME_X_DISPLAY (f), 0);
5718 XFlush (FRAME_X_DISPLAY (f));
5719 UNBLOCK_INPUT;
5725 /* Specify how many text lines, from the top of the window,
5726 should be affected by insert-lines and delete-lines operations.
5727 This, and those operations, are used only within an update
5728 that is bounded by calls to x_update_begin and x_update_end. */
5730 static void
5731 XTset_terminal_window (n)
5732 register int n;
5734 /* This function intentionally left blank. */
5739 /***********************************************************************
5740 Line Dance
5741 ***********************************************************************/
5743 /* Perform an insert-lines or delete-lines operation, inserting N
5744 lines or deleting -N lines at vertical position VPOS. */
5746 static void
5747 x_ins_del_lines (vpos, n)
5748 int vpos, n;
5750 abort ();
5754 /* Scroll part of the display as described by RUN. */
5756 static void
5757 x_scroll_run (w, run)
5758 struct window *w;
5759 struct run *run;
5761 struct frame *f = XFRAME (w->frame);
5762 int x, y, width, height, from_y, to_y, bottom_y;
5764 /* Get frame-relative bounding box of the text display area of W,
5765 without mode lines. Include in this box the left and right
5766 fringe of W. */
5767 window_box (w, -1, &x, &y, &width, &height);
5768 width += FRAME_X_FRINGE_WIDTH (f);
5769 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5771 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5772 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5773 bottom_y = y + height;
5775 if (to_y < from_y)
5777 /* Scrolling up. Make sure we don't copy part of the mode
5778 line at the bottom. */
5779 if (from_y + run->height > bottom_y)
5780 height = bottom_y - from_y;
5781 else
5782 height = run->height;
5784 else
5786 /* Scolling down. Make sure we don't copy over the mode line.
5787 at the bottom. */
5788 if (to_y + run->height > bottom_y)
5789 height = bottom_y - to_y;
5790 else
5791 height = run->height;
5794 BLOCK_INPUT;
5796 /* Cursor off. Will be switched on again in x_update_window_end. */
5797 updated_window = w;
5798 x_clear_cursor (w);
5800 XCopyArea (FRAME_X_DISPLAY (f),
5801 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5802 f->output_data.x->normal_gc,
5803 x, from_y,
5804 width, height,
5805 x, to_y);
5807 UNBLOCK_INPUT;
5812 /***********************************************************************
5813 Exposure Events
5814 ***********************************************************************/
5816 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5817 corner of the exposed rectangle. W and H are width and height of
5818 the exposed area. All are pixel values. W or H zero means redraw
5819 the entire frame. */
5821 static void
5822 expose_frame (f, x, y, w, h)
5823 struct frame *f;
5824 int x, y, w, h;
5826 XRectangle r;
5827 int mouse_face_overwritten_p = 0;
5829 TRACE ((stderr, "expose_frame "));
5831 /* No need to redraw if frame will be redrawn soon. */
5832 if (FRAME_GARBAGED_P (f))
5834 TRACE ((stderr, " garbaged\n"));
5835 return;
5838 /* If basic faces haven't been realized yet, there is no point in
5839 trying to redraw anything. This can happen when we get an expose
5840 event while Emacs is starting, e.g. by moving another window. */
5841 if (FRAME_FACE_CACHE (f) == NULL
5842 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5844 TRACE ((stderr, " no faces\n"));
5845 return;
5848 if (w == 0 || h == 0)
5850 r.x = r.y = 0;
5851 r.width = CANON_X_UNIT (f) * f->width;
5852 r.height = CANON_Y_UNIT (f) * f->height;
5854 else
5856 r.x = x;
5857 r.y = y;
5858 r.width = w;
5859 r.height = h;
5862 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5863 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5865 if (WINDOWP (f->tool_bar_window))
5866 mouse_face_overwritten_p
5867 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5869 #ifndef USE_X_TOOLKIT
5870 if (WINDOWP (f->menu_bar_window))
5871 mouse_face_overwritten_p
5872 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5873 #endif /* not USE_X_TOOLKIT */
5875 /* Some window managers support a focus-follows-mouse style with
5876 delayed raising of frames. Imagine a partially obscured frame,
5877 and moving the mouse into partially obscured mouse-face on that
5878 frame. The visible part of the mouse-face will be highlighted,
5879 then the WM raises the obscured frame. With at least one WM, KDE
5880 2.1, Emacs is not getting any event for the raising of the frame
5881 (even tried with SubstructureRedirectMask), only Expose events.
5882 These expose events will draw text normally, i.e. not
5883 highlighted. Which means we must redo the highlight here.
5884 Subsume it under ``we love X''. --gerd 2001-08-15 */
5885 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5887 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5888 if (f == dpyinfo->mouse_face_mouse_frame)
5890 int x = dpyinfo->mouse_face_mouse_x;
5891 int y = dpyinfo->mouse_face_mouse_y;
5892 clear_mouse_face (dpyinfo);
5893 note_mouse_highlight (f, x, y);
5899 /* Redraw (parts) of all windows in the window tree rooted at W that
5900 intersect R. R contains frame pixel coordinates. Value is
5901 non-zero if the exposure overwrites mouse-face. */
5903 static int
5904 expose_window_tree (w, r)
5905 struct window *w;
5906 XRectangle *r;
5908 struct frame *f = XFRAME (w->frame);
5909 int mouse_face_overwritten_p = 0;
5911 while (w && !FRAME_GARBAGED_P (f))
5913 if (!NILP (w->hchild))
5914 mouse_face_overwritten_p
5915 |= expose_window_tree (XWINDOW (w->hchild), r);
5916 else if (!NILP (w->vchild))
5917 mouse_face_overwritten_p
5918 |= expose_window_tree (XWINDOW (w->vchild), r);
5919 else
5920 mouse_face_overwritten_p |= expose_window (w, r);
5922 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5925 return mouse_face_overwritten_p;
5929 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5930 which intersects rectangle R. R is in window-relative coordinates. */
5932 static void
5933 expose_area (w, row, r, area)
5934 struct window *w;
5935 struct glyph_row *row;
5936 XRectangle *r;
5937 enum glyph_row_area area;
5939 struct glyph *first = row->glyphs[area];
5940 struct glyph *end = row->glyphs[area] + row->used[area];
5941 struct glyph *last;
5942 int first_x, start_x, x;
5944 if (area == TEXT_AREA && row->fill_line_p)
5945 /* If row extends face to end of line write the whole line. */
5946 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5947 DRAW_NORMAL_TEXT, 0);
5948 else
5950 /* Set START_X to the window-relative start position for drawing glyphs of
5951 AREA. The first glyph of the text area can be partially visible.
5952 The first glyphs of other areas cannot. */
5953 if (area == LEFT_MARGIN_AREA)
5954 start_x = 0;
5955 else if (area == TEXT_AREA)
5956 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5957 else
5958 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5959 + window_box_width (w, TEXT_AREA));
5960 x = start_x;
5962 /* Find the first glyph that must be redrawn. */
5963 while (first < end
5964 && x + first->pixel_width < r->x)
5966 x += first->pixel_width;
5967 ++first;
5970 /* Find the last one. */
5971 last = first;
5972 first_x = x;
5973 while (last < end
5974 && x < r->x + r->width)
5976 x += last->pixel_width;
5977 ++last;
5980 /* Repaint. */
5981 if (last > first)
5982 x_draw_glyphs (w, first_x - start_x, row, area,
5983 first - row->glyphs[area],
5984 last - row->glyphs[area],
5985 DRAW_NORMAL_TEXT, 0);
5990 /* Redraw the parts of the glyph row ROW on window W intersecting
5991 rectangle R. R is in window-relative coordinates. Value is
5992 non-zero if mouse-face was overwritten. */
5994 static int
5995 expose_line (w, row, r)
5996 struct window *w;
5997 struct glyph_row *row;
5998 XRectangle *r;
6000 xassert (row->enabled_p);
6002 if (row->mode_line_p || w->pseudo_window_p)
6003 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
6004 DRAW_NORMAL_TEXT, 0);
6005 else
6007 if (row->used[LEFT_MARGIN_AREA])
6008 expose_area (w, row, r, LEFT_MARGIN_AREA);
6009 if (row->used[TEXT_AREA])
6010 expose_area (w, row, r, TEXT_AREA);
6011 if (row->used[RIGHT_MARGIN_AREA])
6012 expose_area (w, row, r, RIGHT_MARGIN_AREA);
6013 x_draw_row_fringe_bitmaps (w, row);
6016 return row->mouse_face_p;
6020 /* Return non-zero if W's cursor intersects rectangle R. */
6022 static int
6023 x_phys_cursor_in_rect_p (w, r)
6024 struct window *w;
6025 XRectangle *r;
6027 XRectangle cr, result;
6028 struct glyph *cursor_glyph;
6030 cursor_glyph = get_phys_cursor_glyph (w);
6031 if (cursor_glyph)
6033 cr.x = w->phys_cursor.x;
6034 cr.y = w->phys_cursor.y;
6035 cr.width = cursor_glyph->pixel_width;
6036 cr.height = w->phys_cursor_height;
6037 return x_intersect_rectangles (&cr, r, &result);
6039 else
6040 return 0;
6044 /* Redraw the part of window W intersection rectangle FR. Pixel
6045 coordinates in FR are frame-relative. Call this function with
6046 input blocked. Value is non-zero if the exposure overwrites
6047 mouse-face. */
6049 static int
6050 expose_window (w, fr)
6051 struct window *w;
6052 XRectangle *fr;
6054 struct frame *f = XFRAME (w->frame);
6055 XRectangle wr, r;
6056 int mouse_face_overwritten_p = 0;
6058 /* If window is not yet fully initialized, do nothing. This can
6059 happen when toolkit scroll bars are used and a window is split.
6060 Reconfiguring the scroll bar will generate an expose for a newly
6061 created window. */
6062 if (w->current_matrix == NULL)
6063 return 0;
6065 /* When we're currently updating the window, display and current
6066 matrix usually don't agree. Arrange for a thorough display
6067 later. */
6068 if (w == updated_window)
6070 SET_FRAME_GARBAGED (f);
6071 return 0;
6074 /* Frame-relative pixel rectangle of W. */
6075 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6076 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6077 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6078 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6080 if (x_intersect_rectangles (fr, &wr, &r))
6082 int yb = window_text_bottom_y (w);
6083 struct glyph_row *row;
6084 int cursor_cleared_p;
6086 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6087 r.x, r.y, r.width, r.height));
6089 /* Convert to window coordinates. */
6090 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6091 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6093 /* Turn off the cursor. */
6094 if (!w->pseudo_window_p
6095 && x_phys_cursor_in_rect_p (w, &r))
6097 x_clear_cursor (w);
6098 cursor_cleared_p = 1;
6100 else
6101 cursor_cleared_p = 0;
6103 /* Find the first row intersecting the rectangle R. */
6104 for (row = w->current_matrix->rows;
6105 row->enabled_p;
6106 ++row)
6108 int y0 = row->y;
6109 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6111 if ((y0 >= r.y && y0 < r.y + r.height)
6112 || (y1 > r.y && y1 < r.y + r.height)
6113 || (r.y >= y0 && r.y < y1)
6114 || (r.y + r.height > y0 && r.y + r.height < y1))
6116 if (expose_line (w, row, &r))
6117 mouse_face_overwritten_p = 1;
6120 if (y1 >= yb)
6121 break;
6124 /* Display the mode line if there is one. */
6125 if (WINDOW_WANTS_MODELINE_P (w)
6126 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6127 row->enabled_p)
6128 && row->y < r.y + r.height)
6130 if (expose_line (w, row, &r))
6131 mouse_face_overwritten_p = 1;
6134 if (!w->pseudo_window_p)
6136 /* Draw border between windows. */
6137 x_draw_vertical_border (w);
6139 /* Turn the cursor on again. */
6140 if (cursor_cleared_p)
6141 x_update_window_cursor (w, 1);
6145 return mouse_face_overwritten_p;
6149 /* Determine the intersection of two rectangles R1 and R2. Return
6150 the intersection in *RESULT. Value is non-zero if RESULT is not
6151 empty. */
6153 static int
6154 x_intersect_rectangles (r1, r2, result)
6155 XRectangle *r1, *r2, *result;
6157 XRectangle *left, *right;
6158 XRectangle *upper, *lower;
6159 int intersection_p = 0;
6161 /* Rearrange so that R1 is the left-most rectangle. */
6162 if (r1->x < r2->x)
6163 left = r1, right = r2;
6164 else
6165 left = r2, right = r1;
6167 /* X0 of the intersection is right.x0, if this is inside R1,
6168 otherwise there is no intersection. */
6169 if (right->x <= left->x + left->width)
6171 result->x = right->x;
6173 /* The right end of the intersection is the minimum of the
6174 the right ends of left and right. */
6175 result->width = (min (left->x + left->width, right->x + right->width)
6176 - result->x);
6178 /* Same game for Y. */
6179 if (r1->y < r2->y)
6180 upper = r1, lower = r2;
6181 else
6182 upper = r2, lower = r1;
6184 /* The upper end of the intersection is lower.y0, if this is inside
6185 of upper. Otherwise, there is no intersection. */
6186 if (lower->y <= upper->y + upper->height)
6188 result->y = lower->y;
6190 /* The lower end of the intersection is the minimum of the lower
6191 ends of upper and lower. */
6192 result->height = (min (lower->y + lower->height,
6193 upper->y + upper->height)
6194 - result->y);
6195 intersection_p = 1;
6199 return intersection_p;
6206 static void
6207 frame_highlight (f)
6208 struct frame *f;
6210 /* We used to only do this if Vx_no_window_manager was non-nil, but
6211 the ICCCM (section 4.1.6) says that the window's border pixmap
6212 and border pixel are window attributes which are "private to the
6213 client", so we can always change it to whatever we want. */
6214 BLOCK_INPUT;
6215 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6216 f->output_data.x->border_pixel);
6217 UNBLOCK_INPUT;
6218 x_update_cursor (f, 1);
6221 static void
6222 frame_unhighlight (f)
6223 struct frame *f;
6225 /* We used to only do this if Vx_no_window_manager was non-nil, but
6226 the ICCCM (section 4.1.6) says that the window's border pixmap
6227 and border pixel are window attributes which are "private to the
6228 client", so we can always change it to whatever we want. */
6229 BLOCK_INPUT;
6230 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6231 f->output_data.x->border_tile);
6232 UNBLOCK_INPUT;
6233 x_update_cursor (f, 1);
6236 /* The focus has changed. Update the frames as necessary to reflect
6237 the new situation. Note that we can't change the selected frame
6238 here, because the Lisp code we are interrupting might become confused.
6239 Each event gets marked with the frame in which it occurred, so the
6240 Lisp code can tell when the switch took place by examining the events. */
6242 static void
6243 x_new_focus_frame (dpyinfo, frame)
6244 struct x_display_info *dpyinfo;
6245 struct frame *frame;
6247 struct frame *old_focus = dpyinfo->x_focus_frame;
6249 if (frame != dpyinfo->x_focus_frame)
6251 /* Set this before calling other routines, so that they see
6252 the correct value of x_focus_frame. */
6253 dpyinfo->x_focus_frame = frame;
6255 if (old_focus && old_focus->auto_lower)
6256 x_lower_frame (old_focus);
6258 #if 0
6259 selected_frame = frame;
6260 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6261 selected_frame);
6262 Fselect_window (selected_frame->selected_window);
6263 choose_minibuf_frame ();
6264 #endif /* ! 0 */
6266 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6267 pending_autoraise_frame = dpyinfo->x_focus_frame;
6268 else
6269 pending_autoraise_frame = 0;
6272 x_frame_rehighlight (dpyinfo);
6275 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6277 void
6278 x_mouse_leave (dpyinfo)
6279 struct x_display_info *dpyinfo;
6281 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6284 /* The focus has changed, or we have redirected a frame's focus to
6285 another frame (this happens when a frame uses a surrogate
6286 mini-buffer frame). Shift the highlight as appropriate.
6288 The FRAME argument doesn't necessarily have anything to do with which
6289 frame is being highlighted or un-highlighted; we only use it to find
6290 the appropriate X display info. */
6292 static void
6293 XTframe_rehighlight (frame)
6294 struct frame *frame;
6296 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6299 static void
6300 x_frame_rehighlight (dpyinfo)
6301 struct x_display_info *dpyinfo;
6303 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6305 if (dpyinfo->x_focus_frame)
6307 dpyinfo->x_highlight_frame
6308 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6309 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6310 : dpyinfo->x_focus_frame);
6311 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6313 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6314 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6317 else
6318 dpyinfo->x_highlight_frame = 0;
6320 if (dpyinfo->x_highlight_frame != old_highlight)
6322 if (old_highlight)
6323 frame_unhighlight (old_highlight);
6324 if (dpyinfo->x_highlight_frame)
6325 frame_highlight (dpyinfo->x_highlight_frame);
6331 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6333 /* Initialize mode_switch_bit and modifier_meaning. */
6334 static void
6335 x_find_modifier_meanings (dpyinfo)
6336 struct x_display_info *dpyinfo;
6338 int min_code, max_code;
6339 KeySym *syms;
6340 int syms_per_code;
6341 XModifierKeymap *mods;
6343 dpyinfo->meta_mod_mask = 0;
6344 dpyinfo->shift_lock_mask = 0;
6345 dpyinfo->alt_mod_mask = 0;
6346 dpyinfo->super_mod_mask = 0;
6347 dpyinfo->hyper_mod_mask = 0;
6349 #ifdef HAVE_X11R4
6350 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6351 #else
6352 min_code = dpyinfo->display->min_keycode;
6353 max_code = dpyinfo->display->max_keycode;
6354 #endif
6356 syms = XGetKeyboardMapping (dpyinfo->display,
6357 min_code, max_code - min_code + 1,
6358 &syms_per_code);
6359 mods = XGetModifierMapping (dpyinfo->display);
6361 /* Scan the modifier table to see which modifier bits the Meta and
6362 Alt keysyms are on. */
6364 int row, col; /* The row and column in the modifier table. */
6366 for (row = 3; row < 8; row++)
6367 for (col = 0; col < mods->max_keypermod; col++)
6369 KeyCode code
6370 = mods->modifiermap[(row * mods->max_keypermod) + col];
6372 /* Zeroes are used for filler. Skip them. */
6373 if (code == 0)
6374 continue;
6376 /* Are any of this keycode's keysyms a meta key? */
6378 int code_col;
6380 for (code_col = 0; code_col < syms_per_code; code_col++)
6382 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6384 switch (sym)
6386 case XK_Meta_L:
6387 case XK_Meta_R:
6388 dpyinfo->meta_mod_mask |= (1 << row);
6389 break;
6391 case XK_Alt_L:
6392 case XK_Alt_R:
6393 dpyinfo->alt_mod_mask |= (1 << row);
6394 break;
6396 case XK_Hyper_L:
6397 case XK_Hyper_R:
6398 dpyinfo->hyper_mod_mask |= (1 << row);
6399 break;
6401 case XK_Super_L:
6402 case XK_Super_R:
6403 dpyinfo->super_mod_mask |= (1 << row);
6404 break;
6406 case XK_Shift_Lock:
6407 /* Ignore this if it's not on the lock modifier. */
6408 if ((1 << row) == LockMask)
6409 dpyinfo->shift_lock_mask = LockMask;
6410 break;
6417 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6418 if (! dpyinfo->meta_mod_mask)
6420 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6421 dpyinfo->alt_mod_mask = 0;
6424 /* If some keys are both alt and meta,
6425 make them just meta, not alt. */
6426 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6428 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6431 XFree ((char *) syms);
6432 XFreeModifiermap (mods);
6435 /* Convert between the modifier bits X uses and the modifier bits
6436 Emacs uses. */
6438 static unsigned int
6439 x_x_to_emacs_modifiers (dpyinfo, state)
6440 struct x_display_info *dpyinfo;
6441 unsigned int state;
6443 EMACS_UINT mod_meta = meta_modifier;
6444 EMACS_UINT mod_alt = alt_modifier;
6445 EMACS_UINT mod_hyper = hyper_modifier;
6446 EMACS_UINT mod_super = super_modifier;
6447 Lisp_Object tem;
6449 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6450 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6451 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6452 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6453 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6454 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6455 tem = Fget (Vx_super_keysym, Qmodifier_value);
6456 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6459 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6460 | ((state & ControlMask) ? ctrl_modifier : 0)
6461 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6462 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6463 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6464 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
6467 static unsigned int
6468 x_emacs_to_x_modifiers (dpyinfo, state)
6469 struct x_display_info *dpyinfo;
6470 unsigned int state;
6472 EMACS_UINT mod_meta = meta_modifier;
6473 EMACS_UINT mod_alt = alt_modifier;
6474 EMACS_UINT mod_hyper = hyper_modifier;
6475 EMACS_UINT mod_super = super_modifier;
6477 Lisp_Object tem;
6479 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6480 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6481 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6482 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6483 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6484 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6485 tem = Fget (Vx_super_keysym, Qmodifier_value);
6486 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6489 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6490 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6491 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6492 | ((state & shift_modifier) ? ShiftMask : 0)
6493 | ((state & ctrl_modifier) ? ControlMask : 0)
6494 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
6497 /* Convert a keysym to its name. */
6499 char *
6500 x_get_keysym_name (keysym)
6501 KeySym keysym;
6503 char *value;
6505 BLOCK_INPUT;
6506 value = XKeysymToString (keysym);
6507 UNBLOCK_INPUT;
6509 return value;
6514 /* Mouse clicks and mouse movement. Rah. */
6516 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6517 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6518 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6519 not force the value into range. */
6521 void
6522 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6523 FRAME_PTR f;
6524 register int pix_x, pix_y;
6525 register int *x, *y;
6526 XRectangle *bounds;
6527 int noclip;
6529 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6530 even for negative values. */
6531 if (pix_x < 0)
6532 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6533 if (pix_y < 0)
6534 pix_y -= (f)->output_data.x->line_height - 1;
6536 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6537 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6539 if (bounds)
6541 bounds->width = FONT_WIDTH (f->output_data.x->font);
6542 bounds->height = f->output_data.x->line_height;
6543 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6544 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6547 if (!noclip)
6549 if (pix_x < 0)
6550 pix_x = 0;
6551 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6552 pix_x = FRAME_WINDOW_WIDTH (f);
6554 if (pix_y < 0)
6555 pix_y = 0;
6556 else if (pix_y > f->height)
6557 pix_y = f->height;
6560 *x = pix_x;
6561 *y = pix_y;
6565 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6566 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6567 can't tell the positions because W's display is not up to date,
6568 return 0. */
6571 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6572 struct window *w;
6573 int hpos, vpos;
6574 int *frame_x, *frame_y;
6576 int success_p;
6578 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6579 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6581 if (display_completed)
6583 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6584 struct glyph *glyph = row->glyphs[TEXT_AREA];
6585 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6587 *frame_y = row->y;
6588 *frame_x = row->x;
6589 while (glyph < end)
6591 *frame_x += glyph->pixel_width;
6592 ++glyph;
6595 success_p = 1;
6597 else
6599 *frame_y = *frame_x = 0;
6600 success_p = 0;
6603 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6604 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6605 return success_p;
6609 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6611 If the event is a button press, then note that we have grabbed
6612 the mouse. */
6614 static Lisp_Object
6615 construct_mouse_click (result, event, f)
6616 struct input_event *result;
6617 XButtonEvent *event;
6618 struct frame *f;
6620 /* Make the event type no_event; we'll change that when we decide
6621 otherwise. */
6622 result->kind = mouse_click;
6623 result->code = event->button - Button1;
6624 result->timestamp = event->time;
6625 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6626 event->state)
6627 | (event->type == ButtonRelease
6628 ? up_modifier
6629 : down_modifier));
6631 XSETINT (result->x, event->x);
6632 XSETINT (result->y, event->y);
6633 XSETFRAME (result->frame_or_window, f);
6634 result->arg = Qnil;
6635 return Qnil;
6639 /* Function to report a mouse movement to the mainstream Emacs code.
6640 The input handler calls this.
6642 We have received a mouse movement event, which is given in *event.
6643 If the mouse is over a different glyph than it was last time, tell
6644 the mainstream emacs code by setting mouse_moved. If not, ask for
6645 another motion event, so we can check again the next time it moves. */
6647 static XMotionEvent last_mouse_motion_event;
6648 static Lisp_Object last_mouse_motion_frame;
6650 static void
6651 note_mouse_movement (frame, event)
6652 FRAME_PTR frame;
6653 XMotionEvent *event;
6655 last_mouse_movement_time = event->time;
6656 last_mouse_motion_event = *event;
6657 XSETFRAME (last_mouse_motion_frame, frame);
6659 if (event->window != FRAME_X_WINDOW (frame))
6661 frame->mouse_moved = 1;
6662 last_mouse_scroll_bar = Qnil;
6663 note_mouse_highlight (frame, -1, -1);
6666 /* Has the mouse moved off the glyph it was on at the last sighting? */
6667 else if (event->x < last_mouse_glyph.x
6668 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6669 || event->y < last_mouse_glyph.y
6670 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6672 frame->mouse_moved = 1;
6673 last_mouse_scroll_bar = Qnil;
6674 note_mouse_highlight (frame, event->x, event->y);
6679 /************************************************************************
6680 Mouse Face
6681 ************************************************************************/
6683 /* Find the glyph under window-relative coordinates X/Y in window W.
6684 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6685 strings. Return in *HPOS and *VPOS the row and column number of
6686 the glyph found. Return in *AREA the glyph area containing X.
6687 Value is a pointer to the glyph found or null if X/Y is not on
6688 text, or we can't tell because W's current matrix is not up to
6689 date. */
6691 static struct glyph *
6692 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6693 struct window *w;
6694 int x, y;
6695 int *hpos, *vpos, *area;
6696 int buffer_only_p;
6698 struct glyph *glyph, *end;
6699 struct glyph_row *row = NULL;
6700 int x0, i, left_area_width;
6702 /* Find row containing Y. Give up if some row is not enabled. */
6703 for (i = 0; i < w->current_matrix->nrows; ++i)
6705 row = MATRIX_ROW (w->current_matrix, i);
6706 if (!row->enabled_p)
6707 return NULL;
6708 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6709 break;
6712 *vpos = i;
6713 *hpos = 0;
6715 /* Give up if Y is not in the window. */
6716 if (i == w->current_matrix->nrows)
6717 return NULL;
6719 /* Get the glyph area containing X. */
6720 if (w->pseudo_window_p)
6722 *area = TEXT_AREA;
6723 x0 = 0;
6725 else
6727 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6728 if (x < left_area_width)
6730 *area = LEFT_MARGIN_AREA;
6731 x0 = 0;
6733 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6735 *area = TEXT_AREA;
6736 x0 = row->x + left_area_width;
6738 else
6740 *area = RIGHT_MARGIN_AREA;
6741 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6745 /* Find glyph containing X. */
6746 glyph = row->glyphs[*area];
6747 end = glyph + row->used[*area];
6748 while (glyph < end)
6750 if (x < x0 + glyph->pixel_width)
6752 if (w->pseudo_window_p)
6753 break;
6754 else if (!buffer_only_p || BUFFERP (glyph->object))
6755 break;
6758 x0 += glyph->pixel_width;
6759 ++glyph;
6762 if (glyph == end)
6763 return NULL;
6765 *hpos = glyph - row->glyphs[*area];
6766 return glyph;
6770 /* Convert frame-relative x/y to coordinates relative to window W.
6771 Takes pseudo-windows into account. */
6773 static void
6774 frame_to_window_pixel_xy (w, x, y)
6775 struct window *w;
6776 int *x, *y;
6778 if (w->pseudo_window_p)
6780 /* A pseudo-window is always full-width, and starts at the
6781 left edge of the frame, plus a frame border. */
6782 struct frame *f = XFRAME (w->frame);
6783 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6784 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6786 else
6788 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6789 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6794 /* Take proper action when mouse has moved to the mode or header line of
6795 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6796 mode line. X is relative to the start of the text display area of
6797 W, so the width of fringes and scroll bars must be subtracted
6798 to get a position relative to the start of the mode line. */
6800 static void
6801 note_mode_line_highlight (w, x, mode_line_p)
6802 struct window *w;
6803 int x, mode_line_p;
6805 struct frame *f = XFRAME (w->frame);
6806 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6807 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6808 struct glyph_row *row;
6810 if (mode_line_p)
6811 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6812 else
6813 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6815 if (row->enabled_p)
6817 struct glyph *glyph, *end;
6818 Lisp_Object help, map;
6819 int x0;
6821 /* Find the glyph under X. */
6822 glyph = row->glyphs[TEXT_AREA];
6823 end = glyph + row->used[TEXT_AREA];
6824 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6825 + FRAME_X_LEFT_FRINGE_WIDTH (f));
6827 while (glyph < end
6828 && x >= x0 + glyph->pixel_width)
6830 x0 += glyph->pixel_width;
6831 ++glyph;
6834 if (glyph < end
6835 && STRINGP (glyph->object)
6836 && XSTRING (glyph->object)->intervals
6837 && glyph->charpos >= 0
6838 && glyph->charpos < XSTRING (glyph->object)->size)
6840 /* If we're on a string with `help-echo' text property,
6841 arrange for the help to be displayed. This is done by
6842 setting the global variable help_echo to the help string. */
6843 help = Fget_text_property (make_number (glyph->charpos),
6844 Qhelp_echo, glyph->object);
6845 if (!NILP (help))
6847 help_echo = help;
6848 XSETWINDOW (help_echo_window, w);
6849 help_echo_object = glyph->object;
6850 help_echo_pos = glyph->charpos;
6853 /* Change the mouse pointer according to what is under X/Y. */
6854 map = Fget_text_property (make_number (glyph->charpos),
6855 Qlocal_map, glyph->object);
6856 if (KEYMAPP (map))
6857 cursor = f->output_data.x->nontext_cursor;
6858 else
6860 map = Fget_text_property (make_number (glyph->charpos),
6861 Qkeymap, glyph->object);
6862 if (KEYMAPP (map))
6863 cursor = f->output_data.x->nontext_cursor;
6868 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6872 /* Take proper action when the mouse has moved to position X, Y on
6873 frame F as regards highlighting characters that have mouse-face
6874 properties. Also de-highlighting chars where the mouse was before.
6875 X and Y can be negative or out of range. */
6877 static void
6878 note_mouse_highlight (f, x, y)
6879 struct frame *f;
6880 int x, y;
6882 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6883 int portion;
6884 Lisp_Object window;
6885 struct window *w;
6886 Cursor cursor = None;
6887 struct buffer *b;
6889 /* When a menu is active, don't highlight because this looks odd. */
6890 #ifdef USE_X_TOOLKIT
6891 if (popup_activated ())
6892 return;
6893 #endif
6895 if (NILP (Vmouse_highlight)
6896 || !f->glyphs_initialized_p)
6897 return;
6899 dpyinfo->mouse_face_mouse_x = x;
6900 dpyinfo->mouse_face_mouse_y = y;
6901 dpyinfo->mouse_face_mouse_frame = f;
6903 if (dpyinfo->mouse_face_defer)
6904 return;
6906 if (gc_in_progress)
6908 dpyinfo->mouse_face_deferred_gc = 1;
6909 return;
6912 /* Which window is that in? */
6913 window = window_from_coordinates (f, x, y, &portion, 1);
6915 /* If we were displaying active text in another window, clear that. */
6916 if (! EQ (window, dpyinfo->mouse_face_window))
6917 clear_mouse_face (dpyinfo);
6919 /* Not on a window -> return. */
6920 if (!WINDOWP (window))
6921 return;
6923 /* Convert to window-relative pixel coordinates. */
6924 w = XWINDOW (window);
6925 frame_to_window_pixel_xy (w, &x, &y);
6927 /* Handle tool-bar window differently since it doesn't display a
6928 buffer. */
6929 if (EQ (window, f->tool_bar_window))
6931 note_tool_bar_highlight (f, x, y);
6932 return;
6935 /* Mouse is on the mode or header line? */
6936 if (portion == 1 || portion == 3)
6938 note_mode_line_highlight (w, x, portion == 1);
6939 return;
6942 if (portion == 2)
6943 cursor = f->output_data.x->horizontal_drag_cursor;
6944 else
6945 cursor = f->output_data.x->text_cursor;
6947 /* Are we in a window whose display is up to date?
6948 And verify the buffer's text has not changed. */
6949 b = XBUFFER (w->buffer);
6950 if (/* Within text portion of the window. */
6951 portion == 0
6952 && EQ (w->window_end_valid, w->buffer)
6953 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6954 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6956 int hpos, vpos, pos, i, area;
6957 struct glyph *glyph;
6958 Lisp_Object object;
6959 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6960 Lisp_Object *overlay_vec = NULL;
6961 int len, noverlays;
6962 struct buffer *obuf;
6963 int obegv, ozv, same_region;
6965 /* Find the glyph under X/Y. */
6966 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6968 /* Clear mouse face if X/Y not over text. */
6969 if (glyph == NULL
6970 || area != TEXT_AREA
6971 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6973 if (clear_mouse_face (dpyinfo))
6974 cursor = None;
6975 goto set_cursor;
6978 pos = glyph->charpos;
6979 object = glyph->object;
6980 if (!STRINGP (object) && !BUFFERP (object))
6981 goto set_cursor;
6983 /* If we get an out-of-range value, return now; avoid an error. */
6984 if (BUFFERP (object) && pos > BUF_Z (b))
6985 goto set_cursor;
6987 /* Make the window's buffer temporarily current for
6988 overlays_at and compute_char_face. */
6989 obuf = current_buffer;
6990 current_buffer = b;
6991 obegv = BEGV;
6992 ozv = ZV;
6993 BEGV = BEG;
6994 ZV = Z;
6996 /* Is this char mouse-active or does it have help-echo? */
6997 position = make_number (pos);
6999 if (BUFFERP (object))
7001 /* Put all the overlays we want in a vector in overlay_vec.
7002 Store the length in len. If there are more than 10, make
7003 enough space for all, and try again. */
7004 len = 10;
7005 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7006 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7007 if (noverlays > len)
7009 len = noverlays;
7010 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7011 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7014 /* Sort overlays into increasing priority order. */
7015 noverlays = sort_overlays (overlay_vec, noverlays, w);
7017 else
7018 noverlays = 0;
7020 same_region = (EQ (window, dpyinfo->mouse_face_window)
7021 && vpos >= dpyinfo->mouse_face_beg_row
7022 && vpos <= dpyinfo->mouse_face_end_row
7023 && (vpos > dpyinfo->mouse_face_beg_row
7024 || hpos >= dpyinfo->mouse_face_beg_col)
7025 && (vpos < dpyinfo->mouse_face_end_row
7026 || hpos < dpyinfo->mouse_face_end_col
7027 || dpyinfo->mouse_face_past_end));
7029 if (same_region)
7030 cursor = None;
7032 /* Check mouse-face highlighting. */
7033 if (! same_region
7034 /* If there exists an overlay with mouse-face overlapping
7035 the one we are currently highlighting, we have to
7036 check if we enter the overlapping overlay, and then
7037 highlight only that. */
7038 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7039 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7041 /* Find the highest priority overlay that has a mouse-face
7042 property. */
7043 overlay = Qnil;
7044 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7046 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7047 if (!NILP (mouse_face))
7048 overlay = overlay_vec[i];
7051 /* If we're actually highlighting the same overlay as
7052 before, there's no need to do that again. */
7053 if (!NILP (overlay)
7054 && EQ (overlay, dpyinfo->mouse_face_overlay))
7055 goto check_help_echo;
7057 dpyinfo->mouse_face_overlay = overlay;
7059 /* Clear the display of the old active region, if any. */
7060 if (clear_mouse_face (dpyinfo))
7061 cursor = None;
7063 /* If no overlay applies, get a text property. */
7064 if (NILP (overlay))
7065 mouse_face = Fget_text_property (position, Qmouse_face, object);
7067 /* Handle the overlay case. */
7068 if (!NILP (overlay))
7070 /* Find the range of text around this char that
7071 should be active. */
7072 Lisp_Object before, after;
7073 int ignore;
7075 before = Foverlay_start (overlay);
7076 after = Foverlay_end (overlay);
7077 /* Record this as the current active region. */
7078 fast_find_position (w, XFASTINT (before),
7079 &dpyinfo->mouse_face_beg_col,
7080 &dpyinfo->mouse_face_beg_row,
7081 &dpyinfo->mouse_face_beg_x,
7082 &dpyinfo->mouse_face_beg_y, Qnil);
7084 dpyinfo->mouse_face_past_end
7085 = !fast_find_position (w, XFASTINT (after),
7086 &dpyinfo->mouse_face_end_col,
7087 &dpyinfo->mouse_face_end_row,
7088 &dpyinfo->mouse_face_end_x,
7089 &dpyinfo->mouse_face_end_y, Qnil);
7090 dpyinfo->mouse_face_window = window;
7091 dpyinfo->mouse_face_face_id
7092 = face_at_buffer_position (w, pos, 0, 0,
7093 &ignore, pos + 1, 1);
7095 /* Display it as active. */
7096 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7097 cursor = None;
7099 /* Handle the text property case. */
7100 else if (!NILP (mouse_face) && BUFFERP (object))
7102 /* Find the range of text around this char that
7103 should be active. */
7104 Lisp_Object before, after, beginning, end;
7105 int ignore;
7107 beginning = Fmarker_position (w->start);
7108 end = make_number (BUF_Z (XBUFFER (object))
7109 - XFASTINT (w->window_end_pos));
7110 before
7111 = Fprevious_single_property_change (make_number (pos + 1),
7112 Qmouse_face,
7113 object, beginning);
7114 after
7115 = Fnext_single_property_change (position, Qmouse_face,
7116 object, end);
7118 /* Record this as the current active region. */
7119 fast_find_position (w, XFASTINT (before),
7120 &dpyinfo->mouse_face_beg_col,
7121 &dpyinfo->mouse_face_beg_row,
7122 &dpyinfo->mouse_face_beg_x,
7123 &dpyinfo->mouse_face_beg_y, Qnil);
7124 dpyinfo->mouse_face_past_end
7125 = !fast_find_position (w, XFASTINT (after),
7126 &dpyinfo->mouse_face_end_col,
7127 &dpyinfo->mouse_face_end_row,
7128 &dpyinfo->mouse_face_end_x,
7129 &dpyinfo->mouse_face_end_y, Qnil);
7130 dpyinfo->mouse_face_window = window;
7132 if (BUFFERP (object))
7133 dpyinfo->mouse_face_face_id
7134 = face_at_buffer_position (w, pos, 0, 0,
7135 &ignore, pos + 1, 1);
7137 /* Display it as active. */
7138 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7139 cursor = None;
7141 else if (!NILP (mouse_face) && STRINGP (object))
7143 Lisp_Object b, e;
7144 int ignore;
7146 b = Fprevious_single_property_change (make_number (pos + 1),
7147 Qmouse_face,
7148 object, Qnil);
7149 e = Fnext_single_property_change (position, Qmouse_face,
7150 object, Qnil);
7151 if (NILP (b))
7152 b = make_number (0);
7153 if (NILP (e))
7154 e = make_number (XSTRING (object)->size - 1);
7155 fast_find_string_pos (w, XINT (b), object,
7156 &dpyinfo->mouse_face_beg_col,
7157 &dpyinfo->mouse_face_beg_row,
7158 &dpyinfo->mouse_face_beg_x,
7159 &dpyinfo->mouse_face_beg_y, 0);
7160 fast_find_string_pos (w, XINT (e), object,
7161 &dpyinfo->mouse_face_end_col,
7162 &dpyinfo->mouse_face_end_row,
7163 &dpyinfo->mouse_face_end_x,
7164 &dpyinfo->mouse_face_end_y, 1);
7165 dpyinfo->mouse_face_past_end = 0;
7166 dpyinfo->mouse_face_window = window;
7167 dpyinfo->mouse_face_face_id
7168 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7169 glyph->face_id, 1);
7170 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7171 cursor = None;
7173 else if (STRINGP (object) && NILP (mouse_face))
7175 /* A string which doesn't have mouse-face, but
7176 the text ``under'' it might have. */
7177 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7178 int start = MATRIX_ROW_START_CHARPOS (r);
7180 pos = string_buffer_position (w, object, start);
7181 if (pos > 0)
7182 mouse_face = get_char_property_and_overlay (make_number (pos),
7183 Qmouse_face,
7184 w->buffer,
7185 &overlay);
7186 if (!NILP (mouse_face) && !NILP (overlay))
7188 Lisp_Object before = Foverlay_start (overlay);
7189 Lisp_Object after = Foverlay_end (overlay);
7190 int ignore;
7192 /* Note that we might not be able to find position
7193 BEFORE in the glyph matrix if the overlay is
7194 entirely covered by a `display' property. In
7195 this case, we overshoot. So let's stop in
7196 the glyph matrix before glyphs for OBJECT. */
7197 fast_find_position (w, XFASTINT (before),
7198 &dpyinfo->mouse_face_beg_col,
7199 &dpyinfo->mouse_face_beg_row,
7200 &dpyinfo->mouse_face_beg_x,
7201 &dpyinfo->mouse_face_beg_y,
7202 object);
7204 dpyinfo->mouse_face_past_end
7205 = !fast_find_position (w, XFASTINT (after),
7206 &dpyinfo->mouse_face_end_col,
7207 &dpyinfo->mouse_face_end_row,
7208 &dpyinfo->mouse_face_end_x,
7209 &dpyinfo->mouse_face_end_y,
7210 Qnil);
7211 dpyinfo->mouse_face_window = window;
7212 dpyinfo->mouse_face_face_id
7213 = face_at_buffer_position (w, pos, 0, 0,
7214 &ignore, pos + 1, 1);
7216 /* Display it as active. */
7217 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7218 cursor = None;
7223 check_help_echo:
7225 /* Look for a `help-echo' property. */
7227 Lisp_Object help, overlay;
7229 /* Check overlays first. */
7230 help = overlay = Qnil;
7231 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7233 overlay = overlay_vec[i];
7234 help = Foverlay_get (overlay, Qhelp_echo);
7237 if (!NILP (help))
7239 help_echo = help;
7240 help_echo_window = window;
7241 help_echo_object = overlay;
7242 help_echo_pos = pos;
7244 else
7246 Lisp_Object object = glyph->object;
7247 int charpos = glyph->charpos;
7249 /* Try text properties. */
7250 if (STRINGP (object)
7251 && charpos >= 0
7252 && charpos < XSTRING (object)->size)
7254 help = Fget_text_property (make_number (charpos),
7255 Qhelp_echo, object);
7256 if (NILP (help))
7258 /* If the string itself doesn't specify a help-echo,
7259 see if the buffer text ``under'' it does. */
7260 struct glyph_row *r
7261 = MATRIX_ROW (w->current_matrix, vpos);
7262 int start = MATRIX_ROW_START_CHARPOS (r);
7263 int pos = string_buffer_position (w, object, start);
7264 if (pos > 0)
7266 help = Fget_char_property (make_number (pos),
7267 Qhelp_echo, w->buffer);
7268 if (!NILP (help))
7270 charpos = pos;
7271 object = w->buffer;
7276 else if (BUFFERP (object)
7277 && charpos >= BEGV
7278 && charpos < ZV)
7279 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7280 object);
7282 if (!NILP (help))
7284 help_echo = help;
7285 help_echo_window = window;
7286 help_echo_object = object;
7287 help_echo_pos = charpos;
7292 BEGV = obegv;
7293 ZV = ozv;
7294 current_buffer = obuf;
7297 set_cursor:
7299 if (cursor != None)
7300 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7303 static void
7304 redo_mouse_highlight ()
7306 if (!NILP (last_mouse_motion_frame)
7307 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7308 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7309 last_mouse_motion_event.x,
7310 last_mouse_motion_event.y);
7315 /***********************************************************************
7316 Tool-bars
7317 ***********************************************************************/
7319 static int x_tool_bar_item P_ ((struct frame *, int, int,
7320 struct glyph **, int *, int *, int *));
7322 /* Tool-bar item index of the item on which a mouse button was pressed
7323 or -1. */
7325 static int last_tool_bar_item;
7328 /* Get information about the tool-bar item at position X/Y on frame F.
7329 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7330 the current matrix of the tool-bar window of F, or NULL if not
7331 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7332 item in F->tool_bar_items. Value is
7334 -1 if X/Y is not on a tool-bar item
7335 0 if X/Y is on the same item that was highlighted before.
7336 1 otherwise. */
7338 static int
7339 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7340 struct frame *f;
7341 int x, y;
7342 struct glyph **glyph;
7343 int *hpos, *vpos, *prop_idx;
7345 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7346 struct window *w = XWINDOW (f->tool_bar_window);
7347 int area;
7349 /* Find the glyph under X/Y. */
7350 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7351 if (*glyph == NULL)
7352 return -1;
7354 /* Get the start of this tool-bar item's properties in
7355 f->tool_bar_items. */
7356 if (!tool_bar_item_info (f, *glyph, prop_idx))
7357 return -1;
7359 /* Is mouse on the highlighted item? */
7360 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7361 && *vpos >= dpyinfo->mouse_face_beg_row
7362 && *vpos <= dpyinfo->mouse_face_end_row
7363 && (*vpos > dpyinfo->mouse_face_beg_row
7364 || *hpos >= dpyinfo->mouse_face_beg_col)
7365 && (*vpos < dpyinfo->mouse_face_end_row
7366 || *hpos < dpyinfo->mouse_face_end_col
7367 || dpyinfo->mouse_face_past_end))
7368 return 0;
7370 return 1;
7374 /* Handle mouse button event on the tool-bar of frame F, at
7375 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7376 or ButtonRelase. */
7378 static void
7379 x_handle_tool_bar_click (f, button_event)
7380 struct frame *f;
7381 XButtonEvent *button_event;
7383 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7384 struct window *w = XWINDOW (f->tool_bar_window);
7385 int hpos, vpos, prop_idx;
7386 struct glyph *glyph;
7387 Lisp_Object enabled_p;
7388 int x = button_event->x;
7389 int y = button_event->y;
7391 /* If not on the highlighted tool-bar item, return. */
7392 frame_to_window_pixel_xy (w, &x, &y);
7393 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7394 return;
7396 /* If item is disabled, do nothing. */
7397 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7398 if (NILP (enabled_p))
7399 return;
7401 if (button_event->type == ButtonPress)
7403 /* Show item in pressed state. */
7404 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7405 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7406 last_tool_bar_item = prop_idx;
7408 else
7410 Lisp_Object key, frame;
7411 struct input_event event;
7413 /* Show item in released state. */
7414 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7415 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7417 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7419 XSETFRAME (frame, f);
7420 event.kind = TOOL_BAR_EVENT;
7421 event.frame_or_window = frame;
7422 event.arg = frame;
7423 kbd_buffer_store_event (&event);
7425 event.kind = TOOL_BAR_EVENT;
7426 event.frame_or_window = frame;
7427 event.arg = key;
7428 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7429 button_event->state);
7430 kbd_buffer_store_event (&event);
7431 last_tool_bar_item = -1;
7436 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7437 tool-bar window-relative coordinates X/Y. Called from
7438 note_mouse_highlight. */
7440 static void
7441 note_tool_bar_highlight (f, x, y)
7442 struct frame *f;
7443 int x, y;
7445 Lisp_Object window = f->tool_bar_window;
7446 struct window *w = XWINDOW (window);
7447 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7448 int hpos, vpos;
7449 struct glyph *glyph;
7450 struct glyph_row *row;
7451 int i;
7452 Lisp_Object enabled_p;
7453 int prop_idx;
7454 enum draw_glyphs_face draw;
7455 int mouse_down_p, rc;
7457 /* Function note_mouse_highlight is called with negative x(y
7458 values when mouse moves outside of the frame. */
7459 if (x <= 0 || y <= 0)
7461 clear_mouse_face (dpyinfo);
7462 return;
7465 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7466 if (rc < 0)
7468 /* Not on tool-bar item. */
7469 clear_mouse_face (dpyinfo);
7470 return;
7472 else if (rc == 0)
7473 goto set_help_echo;
7475 clear_mouse_face (dpyinfo);
7477 /* Mouse is down, but on different tool-bar item? */
7478 mouse_down_p = (dpyinfo->grabbed
7479 && f == last_mouse_frame
7480 && FRAME_LIVE_P (f));
7481 if (mouse_down_p
7482 && last_tool_bar_item != prop_idx)
7483 return;
7485 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7486 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7488 /* If tool-bar item is not enabled, don't highlight it. */
7489 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7490 if (!NILP (enabled_p))
7492 /* Compute the x-position of the glyph. In front and past the
7493 image is a space. We include this is the highlighted area. */
7494 row = MATRIX_ROW (w->current_matrix, vpos);
7495 for (i = x = 0; i < hpos; ++i)
7496 x += row->glyphs[TEXT_AREA][i].pixel_width;
7498 /* Record this as the current active region. */
7499 dpyinfo->mouse_face_beg_col = hpos;
7500 dpyinfo->mouse_face_beg_row = vpos;
7501 dpyinfo->mouse_face_beg_x = x;
7502 dpyinfo->mouse_face_beg_y = row->y;
7503 dpyinfo->mouse_face_past_end = 0;
7505 dpyinfo->mouse_face_end_col = hpos + 1;
7506 dpyinfo->mouse_face_end_row = vpos;
7507 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7508 dpyinfo->mouse_face_end_y = row->y;
7509 dpyinfo->mouse_face_window = window;
7510 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7512 /* Display it as active. */
7513 show_mouse_face (dpyinfo, draw);
7514 dpyinfo->mouse_face_image_state = draw;
7517 set_help_echo:
7519 /* Set help_echo to a help string to display for this tool-bar item.
7520 XTread_socket does the rest. */
7521 help_echo_object = help_echo_window = Qnil;
7522 help_echo_pos = -1;
7523 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7524 if (NILP (help_echo))
7525 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7530 /* Find the glyph matrix position of buffer position CHARPOS in window
7531 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7532 current glyphs must be up to date. If CHARPOS is above window
7533 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7534 of last line in W. In the row containing CHARPOS, stop before glyphs
7535 having STOP as object. */
7537 #if 0 /* This is a version of fast_find_position that's more correct
7538 in the presence of hscrolling, for example. I didn't install
7539 it right away because the problem fixed is minor, it failed
7540 in 20.x as well, and I think it's too risky to install
7541 so near the release of 21.1. 2001-09-25 gerd. */
7543 static int
7544 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7545 struct window *w;
7546 int charpos;
7547 int *hpos, *vpos, *x, *y;
7548 Lisp_Object stop;
7550 struct glyph_row *row, *first;
7551 struct glyph *glyph, *end;
7552 int i, past_end = 0;
7554 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7555 row = row_containing_pos (w, charpos, first, NULL, 0);
7556 if (row == NULL)
7558 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7560 *x = *y = *hpos = *vpos = 0;
7561 return 0;
7563 else
7565 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7566 past_end = 1;
7570 *x = row->x;
7571 *y = row->y;
7572 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7574 glyph = row->glyphs[TEXT_AREA];
7575 end = glyph + row->used[TEXT_AREA];
7577 /* Skip over glyphs not having an object at the start of the row.
7578 These are special glyphs like truncation marks on terminal
7579 frames. */
7580 if (row->displays_text_p)
7581 while (glyph < end
7582 && INTEGERP (glyph->object)
7583 && !EQ (stop, glyph->object)
7584 && glyph->charpos < 0)
7586 *x += glyph->pixel_width;
7587 ++glyph;
7590 while (glyph < end
7591 && !INTEGERP (glyph->object)
7592 && !EQ (stop, glyph->object)
7593 && (!BUFFERP (glyph->object)
7594 || glyph->charpos < charpos))
7596 *x += glyph->pixel_width;
7597 ++glyph;
7600 *hpos = glyph - row->glyphs[TEXT_AREA];
7601 return past_end;
7604 #else /* not 0 */
7606 static int
7607 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7608 struct window *w;
7609 int pos;
7610 int *hpos, *vpos, *x, *y;
7611 Lisp_Object stop;
7613 int i;
7614 int lastcol;
7615 int maybe_next_line_p = 0;
7616 int line_start_position;
7617 int yb = window_text_bottom_y (w);
7618 struct glyph_row *row, *best_row;
7619 int row_vpos, best_row_vpos;
7620 int current_x;
7622 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7623 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7625 while (row->y < yb)
7627 if (row->used[TEXT_AREA])
7628 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7629 else
7630 line_start_position = 0;
7632 if (line_start_position > pos)
7633 break;
7634 /* If the position sought is the end of the buffer,
7635 don't include the blank lines at the bottom of the window. */
7636 else if (line_start_position == pos
7637 && pos == BUF_ZV (XBUFFER (w->buffer)))
7639 maybe_next_line_p = 1;
7640 break;
7642 else if (line_start_position > 0)
7644 best_row = row;
7645 best_row_vpos = row_vpos;
7648 if (row->y + row->height >= yb)
7649 break;
7651 ++row;
7652 ++row_vpos;
7655 /* Find the right column within BEST_ROW. */
7656 lastcol = 0;
7657 current_x = best_row->x;
7658 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7660 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7661 int charpos = glyph->charpos;
7663 if (BUFFERP (glyph->object))
7665 if (charpos == pos)
7667 *hpos = i;
7668 *vpos = best_row_vpos;
7669 *x = current_x;
7670 *y = best_row->y;
7671 return 1;
7673 else if (charpos > pos)
7674 break;
7676 else if (EQ (glyph->object, stop))
7677 break;
7679 if (charpos > 0)
7680 lastcol = i;
7681 current_x += glyph->pixel_width;
7684 /* If we're looking for the end of the buffer,
7685 and we didn't find it in the line we scanned,
7686 use the start of the following line. */
7687 if (maybe_next_line_p)
7689 ++best_row;
7690 ++best_row_vpos;
7691 lastcol = 0;
7692 current_x = best_row->x;
7695 *vpos = best_row_vpos;
7696 *hpos = lastcol + 1;
7697 *x = current_x;
7698 *y = best_row->y;
7699 return 0;
7702 #endif /* not 0 */
7705 /* Find the position of the glyph for position POS in OBJECT in
7706 window W's current matrix, and return in *X, *Y the pixel
7707 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7709 RIGHT_P non-zero means return the position of the right edge of the
7710 glyph, RIGHT_P zero means return the left edge position.
7712 If no glyph for POS exists in the matrix, return the position of
7713 the glyph with the next smaller position that is in the matrix, if
7714 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7715 exists in the matrix, return the position of the glyph with the
7716 next larger position in OBJECT.
7718 Value is non-zero if a glyph was found. */
7720 static int
7721 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7722 struct window *w;
7723 int pos;
7724 Lisp_Object object;
7725 int *hpos, *vpos, *x, *y;
7726 int right_p;
7728 int yb = window_text_bottom_y (w);
7729 struct glyph_row *r;
7730 struct glyph *best_glyph = NULL;
7731 struct glyph_row *best_row = NULL;
7732 int best_x = 0;
7734 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7735 r->enabled_p && r->y < yb;
7736 ++r)
7738 struct glyph *g = r->glyphs[TEXT_AREA];
7739 struct glyph *e = g + r->used[TEXT_AREA];
7740 int gx;
7742 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7743 if (EQ (g->object, object))
7745 if (g->charpos == pos)
7747 best_glyph = g;
7748 best_x = gx;
7749 best_row = r;
7750 goto found;
7752 else if (best_glyph == NULL
7753 || ((abs (g->charpos - pos)
7754 < abs (best_glyph->charpos - pos))
7755 && (right_p
7756 ? g->charpos < pos
7757 : g->charpos > pos)))
7759 best_glyph = g;
7760 best_x = gx;
7761 best_row = r;
7766 found:
7768 if (best_glyph)
7770 *x = best_x;
7771 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7773 if (right_p)
7775 *x += best_glyph->pixel_width;
7776 ++*hpos;
7779 *y = best_row->y;
7780 *vpos = best_row - w->current_matrix->rows;
7783 return best_glyph != NULL;
7787 /* Display the active region described by mouse_face_*
7788 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7790 static void
7791 show_mouse_face (dpyinfo, draw)
7792 struct x_display_info *dpyinfo;
7793 enum draw_glyphs_face draw;
7795 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7796 struct frame *f = XFRAME (WINDOW_FRAME (w));
7798 if (/* If window is in the process of being destroyed, don't bother
7799 to do anything. */
7800 w->current_matrix != NULL
7801 /* Don't update mouse highlight if hidden */
7802 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7803 /* Recognize when we are called to operate on rows that don't exist
7804 anymore. This can happen when a window is split. */
7805 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7807 int phys_cursor_on_p = w->phys_cursor_on_p;
7808 struct glyph_row *row, *first, *last;
7810 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7811 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7813 for (row = first; row <= last && row->enabled_p; ++row)
7815 int start_hpos, end_hpos, start_x;
7817 /* For all but the first row, the highlight starts at column 0. */
7818 if (row == first)
7820 start_hpos = dpyinfo->mouse_face_beg_col;
7821 start_x = dpyinfo->mouse_face_beg_x;
7823 else
7825 start_hpos = 0;
7826 start_x = 0;
7829 if (row == last)
7830 end_hpos = dpyinfo->mouse_face_end_col;
7831 else
7832 end_hpos = row->used[TEXT_AREA];
7834 if (end_hpos > start_hpos)
7836 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7837 start_hpos, end_hpos, draw, 0);
7839 row->mouse_face_p
7840 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7844 /* When we've written over the cursor, arrange for it to
7845 be displayed again. */
7846 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7847 x_display_cursor (w, 1,
7848 w->phys_cursor.hpos, w->phys_cursor.vpos,
7849 w->phys_cursor.x, w->phys_cursor.y);
7852 /* Change the mouse cursor. */
7853 if (draw == DRAW_NORMAL_TEXT)
7854 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7855 f->output_data.x->text_cursor);
7856 else if (draw == DRAW_MOUSE_FACE)
7857 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7858 f->output_data.x->cross_cursor);
7859 else
7860 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7861 f->output_data.x->nontext_cursor);
7864 /* Clear out the mouse-highlighted active region.
7865 Redraw it un-highlighted first. Value is non-zero if mouse
7866 face was actually drawn unhighlighted. */
7868 static int
7869 clear_mouse_face (dpyinfo)
7870 struct x_display_info *dpyinfo;
7872 int cleared = 0;
7874 if (!NILP (dpyinfo->mouse_face_window))
7876 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7877 cleared = 1;
7880 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7881 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7882 dpyinfo->mouse_face_window = Qnil;
7883 dpyinfo->mouse_face_overlay = Qnil;
7884 return cleared;
7888 /* Clear any mouse-face on window W. This function is part of the
7889 redisplay interface, and is called from try_window_id and similar
7890 functions to ensure the mouse-highlight is off. */
7892 static void
7893 x_clear_mouse_face (w)
7894 struct window *w;
7896 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7897 Lisp_Object window;
7899 BLOCK_INPUT;
7900 XSETWINDOW (window, w);
7901 if (EQ (window, dpyinfo->mouse_face_window))
7902 clear_mouse_face (dpyinfo);
7903 UNBLOCK_INPUT;
7907 /* Just discard the mouse face information for frame F, if any.
7908 This is used when the size of F is changed. */
7910 void
7911 cancel_mouse_face (f)
7912 FRAME_PTR f;
7914 Lisp_Object window;
7915 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7917 window = dpyinfo->mouse_face_window;
7918 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7920 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7921 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7922 dpyinfo->mouse_face_window = Qnil;
7927 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7930 /* Try to determine frame pixel position and size of the glyph under
7931 frame pixel coordinates X/Y on frame F . Return the position and
7932 size in *RECT. Value is non-zero if we could compute these
7933 values. */
7935 static int
7936 glyph_rect (f, x, y, rect)
7937 struct frame *f;
7938 int x, y;
7939 XRectangle *rect;
7941 Lisp_Object window;
7942 int part, found = 0;
7944 window = window_from_coordinates (f, x, y, &part, 0);
7945 if (!NILP (window))
7947 struct window *w = XWINDOW (window);
7948 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7949 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7951 frame_to_window_pixel_xy (w, &x, &y);
7953 for (; !found && r < end && r->enabled_p; ++r)
7954 if (r->y >= y)
7956 struct glyph *g = r->glyphs[TEXT_AREA];
7957 struct glyph *end = g + r->used[TEXT_AREA];
7958 int gx;
7960 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7961 if (gx >= x)
7963 rect->width = g->pixel_width;
7964 rect->height = r->height;
7965 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7966 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7967 found = 1;
7972 return found;
7976 /* Return the current position of the mouse.
7977 *FP should be a frame which indicates which display to ask about.
7979 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7980 and *PART to the frame, window, and scroll bar part that the mouse
7981 is over. Set *X and *Y to the portion and whole of the mouse's
7982 position on the scroll bar.
7984 If the mouse movement started elsewhere, set *FP to the frame the
7985 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7986 the mouse is over.
7988 Set *TIME to the server time-stamp for the time at which the mouse
7989 was at this position.
7991 Don't store anything if we don't have a valid set of values to report.
7993 This clears the mouse_moved flag, so we can wait for the next mouse
7994 movement. */
7996 static void
7997 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7998 FRAME_PTR *fp;
7999 int insist;
8000 Lisp_Object *bar_window;
8001 enum scroll_bar_part *part;
8002 Lisp_Object *x, *y;
8003 unsigned long *time;
8005 FRAME_PTR f1;
8007 BLOCK_INPUT;
8009 if (! NILP (last_mouse_scroll_bar) && insist == 0)
8010 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
8011 else
8013 Window root;
8014 int root_x, root_y;
8016 Window dummy_window;
8017 int dummy;
8019 Lisp_Object frame, tail;
8021 /* Clear the mouse-moved flag for every frame on this display. */
8022 FOR_EACH_FRAME (tail, frame)
8023 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8024 XFRAME (frame)->mouse_moved = 0;
8026 last_mouse_scroll_bar = Qnil;
8028 /* Figure out which root window we're on. */
8029 XQueryPointer (FRAME_X_DISPLAY (*fp),
8030 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
8032 /* The root window which contains the pointer. */
8033 &root,
8035 /* Trash which we can't trust if the pointer is on
8036 a different screen. */
8037 &dummy_window,
8039 /* The position on that root window. */
8040 &root_x, &root_y,
8042 /* More trash we can't trust. */
8043 &dummy, &dummy,
8045 /* Modifier keys and pointer buttons, about which
8046 we don't care. */
8047 (unsigned int *) &dummy);
8049 /* Now we have a position on the root; find the innermost window
8050 containing the pointer. */
8052 Window win, child;
8053 int win_x, win_y;
8054 int parent_x = 0, parent_y = 0;
8055 int count;
8057 win = root;
8059 /* XTranslateCoordinates can get errors if the window
8060 structure is changing at the same time this function
8061 is running. So at least we must not crash from them. */
8063 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8065 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8066 && FRAME_LIVE_P (last_mouse_frame))
8068 /* If mouse was grabbed on a frame, give coords for that frame
8069 even if the mouse is now outside it. */
8070 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8072 /* From-window, to-window. */
8073 root, FRAME_X_WINDOW (last_mouse_frame),
8075 /* From-position, to-position. */
8076 root_x, root_y, &win_x, &win_y,
8078 /* Child of win. */
8079 &child);
8080 f1 = last_mouse_frame;
8082 else
8084 while (1)
8086 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8088 /* From-window, to-window. */
8089 root, win,
8091 /* From-position, to-position. */
8092 root_x, root_y, &win_x, &win_y,
8094 /* Child of win. */
8095 &child);
8097 if (child == None || child == win)
8098 break;
8100 win = child;
8101 parent_x = win_x;
8102 parent_y = win_y;
8105 /* Now we know that:
8106 win is the innermost window containing the pointer
8107 (XTC says it has no child containing the pointer),
8108 win_x and win_y are the pointer's position in it
8109 (XTC did this the last time through), and
8110 parent_x and parent_y are the pointer's position in win's parent.
8111 (They are what win_x and win_y were when win was child.
8112 If win is the root window, it has no parent, and
8113 parent_{x,y} are invalid, but that's okay, because we'll
8114 never use them in that case.) */
8116 /* Is win one of our frames? */
8117 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8119 #ifdef USE_X_TOOLKIT
8120 /* If we end up with the menu bar window, say it's not
8121 on the frame. */
8122 if (f1 != NULL
8123 && f1->output_data.x->menubar_widget
8124 && win == XtWindow (f1->output_data.x->menubar_widget))
8125 f1 = NULL;
8126 #endif /* USE_X_TOOLKIT */
8129 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8130 f1 = 0;
8132 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8134 /* If not, is it one of our scroll bars? */
8135 if (! f1)
8137 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8139 if (bar)
8141 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8142 win_x = parent_x;
8143 win_y = parent_y;
8147 if (f1 == 0 && insist > 0)
8148 f1 = SELECTED_FRAME ();
8150 if (f1)
8152 /* Ok, we found a frame. Store all the values.
8153 last_mouse_glyph is a rectangle used to reduce the
8154 generation of mouse events. To not miss any motion
8155 events, we must divide the frame into rectangles of the
8156 size of the smallest character that could be displayed
8157 on it, i.e. into the same rectangles that matrices on
8158 the frame are divided into. */
8160 int width, height, gx, gy;
8161 XRectangle rect;
8163 if (glyph_rect (f1, win_x, win_y, &rect))
8164 last_mouse_glyph = rect;
8165 else
8167 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8168 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8169 gx = win_x;
8170 gy = win_y;
8172 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8173 round down even for negative values. */
8174 if (gx < 0)
8175 gx -= width - 1;
8176 if (gy < 0)
8177 gy -= height - 1;
8178 gx = (gx + width - 1) / width * width;
8179 gy = (gy + height - 1) / height * height;
8181 last_mouse_glyph.width = width;
8182 last_mouse_glyph.height = height;
8183 last_mouse_glyph.x = gx;
8184 last_mouse_glyph.y = gy;
8187 *bar_window = Qnil;
8188 *part = 0;
8189 *fp = f1;
8190 XSETINT (*x, win_x);
8191 XSETINT (*y, win_y);
8192 *time = last_mouse_movement_time;
8197 UNBLOCK_INPUT;
8201 #ifdef USE_X_TOOLKIT
8203 /* Atimer callback function for TIMER. Called every 0.1s to process
8204 Xt timeouts, if needed. We must avoid calling XtAppPending as
8205 much as possible because that function does an implicit XFlush
8206 that slows us down. */
8208 static void
8209 x_process_timeouts (timer)
8210 struct atimer *timer;
8212 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8214 BLOCK_INPUT;
8215 while (XtAppPending (Xt_app_con) & XtIMTimer)
8216 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8217 UNBLOCK_INPUT;
8221 #endif /* USE_X_TOOLKIT */
8224 /* Scroll bar support. */
8226 /* Given an X window ID, find the struct scroll_bar which manages it.
8227 This can be called in GC, so we have to make sure to strip off mark
8228 bits. */
8230 static struct scroll_bar *
8231 x_window_to_scroll_bar (window_id)
8232 Window window_id;
8234 Lisp_Object tail;
8236 for (tail = Vframe_list;
8237 XGCTYPE (tail) == Lisp_Cons;
8238 tail = XCDR (tail))
8240 Lisp_Object frame, bar, condemned;
8242 frame = XCAR (tail);
8243 /* All elements of Vframe_list should be frames. */
8244 if (! GC_FRAMEP (frame))
8245 abort ();
8247 /* Scan this frame's scroll bar list for a scroll bar with the
8248 right window ID. */
8249 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8250 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8251 /* This trick allows us to search both the ordinary and
8252 condemned scroll bar lists with one loop. */
8253 ! GC_NILP (bar) || (bar = condemned,
8254 condemned = Qnil,
8255 ! GC_NILP (bar));
8256 bar = XSCROLL_BAR (bar)->next)
8257 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8258 return XSCROLL_BAR (bar);
8261 return 0;
8265 #if defined USE_LUCID
8267 /* Return the Lucid menu bar WINDOW is part of. Return null
8268 if WINDOW is not part of a menu bar. */
8270 static Widget
8271 x_window_to_menu_bar (window)
8272 Window window;
8274 Lisp_Object tail;
8276 for (tail = Vframe_list;
8277 XGCTYPE (tail) == Lisp_Cons;
8278 tail = XCDR (tail))
8280 Lisp_Object frame = XCAR (tail);
8281 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8283 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8284 return menu_bar;
8287 return NULL;
8290 #endif /* USE_LUCID */
8293 /************************************************************************
8294 Toolkit scroll bars
8295 ************************************************************************/
8297 #ifdef USE_TOOLKIT_SCROLL_BARS
8299 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8300 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8301 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8302 struct scroll_bar *));
8303 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8304 int, int, int));
8307 /* Id of action hook installed for scroll bars. */
8309 static XtActionHookId action_hook_id;
8311 /* Lisp window being scrolled. Set when starting to interact with
8312 a toolkit scroll bar, reset to nil when ending the interaction. */
8314 static Lisp_Object window_being_scrolled;
8316 /* Last scroll bar part sent in xm_scroll_callback. */
8318 static int last_scroll_bar_part;
8320 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8321 that movements of 1/20 of the screen size are mapped to up/down. */
8323 static Boolean xaw3d_arrow_scroll;
8325 /* Whether the drag scrolling maintains the mouse at the top of the
8326 thumb. If not, resizing the thumb needs to be done more carefully
8327 to avoid jerkyness. */
8329 static Boolean xaw3d_pick_top;
8332 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8333 bars are used.. The hook is responsible for detecting when
8334 the user ends an interaction with the scroll bar, and generates
8335 a `end-scroll' scroll_bar_click' event if so. */
8337 static void
8338 xt_action_hook (widget, client_data, action_name, event, params,
8339 num_params)
8340 Widget widget;
8341 XtPointer client_data;
8342 String action_name;
8343 XEvent *event;
8344 String *params;
8345 Cardinal *num_params;
8347 int scroll_bar_p;
8348 char *end_action;
8350 #ifdef USE_MOTIF
8351 scroll_bar_p = XmIsScrollBar (widget);
8352 end_action = "Release";
8353 #else /* !USE_MOTIF i.e. use Xaw */
8354 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8355 end_action = "EndScroll";
8356 #endif /* USE_MOTIF */
8358 if (scroll_bar_p
8359 && strcmp (action_name, end_action) == 0
8360 && WINDOWP (window_being_scrolled))
8362 struct window *w;
8364 x_send_scroll_bar_event (window_being_scrolled,
8365 scroll_bar_end_scroll, 0, 0);
8366 w = XWINDOW (window_being_scrolled);
8367 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8368 window_being_scrolled = Qnil;
8369 last_scroll_bar_part = -1;
8371 /* Xt timeouts no longer needed. */
8372 toolkit_scroll_bar_interaction = 0;
8376 /* A vector of windows used for communication between
8377 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8379 static struct window **scroll_bar_windows;
8380 static int scroll_bar_windows_size;
8383 /* Send a client message with message type Xatom_Scrollbar for a
8384 scroll action to the frame of WINDOW. PART is a value identifying
8385 the part of the scroll bar that was clicked on. PORTION is the
8386 amount to scroll of a whole of WHOLE. */
8388 static void
8389 x_send_scroll_bar_event (window, part, portion, whole)
8390 Lisp_Object window;
8391 int part, portion, whole;
8393 XEvent event;
8394 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8395 struct window *w = XWINDOW (window);
8396 struct frame *f = XFRAME (w->frame);
8397 int i;
8399 BLOCK_INPUT;
8401 /* Construct a ClientMessage event to send to the frame. */
8402 ev->type = ClientMessage;
8403 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8404 ev->display = FRAME_X_DISPLAY (f);
8405 ev->window = FRAME_X_WINDOW (f);
8406 ev->format = 32;
8408 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8409 not enough to store a pointer or Lisp_Object on a 64 bit system.
8410 So, store the window in scroll_bar_windows and pass the index
8411 into that array in the event. */
8412 for (i = 0; i < scroll_bar_windows_size; ++i)
8413 if (scroll_bar_windows[i] == NULL)
8414 break;
8416 if (i == scroll_bar_windows_size)
8418 int new_size = max (10, 2 * scroll_bar_windows_size);
8419 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8420 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8422 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8423 nbytes);
8424 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8425 scroll_bar_windows_size = new_size;
8428 scroll_bar_windows[i] = w;
8429 ev->data.l[0] = (long) i;
8430 ev->data.l[1] = (long) part;
8431 ev->data.l[2] = (long) 0;
8432 ev->data.l[3] = (long) portion;
8433 ev->data.l[4] = (long) whole;
8435 /* Make Xt timeouts work while the scroll bar is active. */
8436 toolkit_scroll_bar_interaction = 1;
8438 /* Setting the event mask to zero means that the message will
8439 be sent to the client that created the window, and if that
8440 window no longer exists, no event will be sent. */
8441 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8442 UNBLOCK_INPUT;
8446 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8447 in *IEVENT. */
8449 static void
8450 x_scroll_bar_to_input_event (event, ievent)
8451 XEvent *event;
8452 struct input_event *ievent;
8454 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8455 Lisp_Object window;
8456 struct frame *f;
8457 struct window *w;
8459 w = scroll_bar_windows[ev->data.l[0]];
8460 scroll_bar_windows[ev->data.l[0]] = NULL;
8462 XSETWINDOW (window, w);
8463 f = XFRAME (w->frame);
8465 ievent->kind = scroll_bar_click;
8466 ievent->frame_or_window = window;
8467 ievent->arg = Qnil;
8468 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8469 ievent->part = ev->data.l[1];
8470 ievent->code = ev->data.l[2];
8471 ievent->x = make_number ((int) ev->data.l[3]);
8472 ievent->y = make_number ((int) ev->data.l[4]);
8473 ievent->modifiers = 0;
8477 #ifdef USE_MOTIF
8479 /* Minimum and maximum values used for Motif scroll bars. */
8481 #define XM_SB_MIN 1
8482 #define XM_SB_MAX 10000000
8483 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8486 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8487 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8488 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8490 static void
8491 xm_scroll_callback (widget, client_data, call_data)
8492 Widget widget;
8493 XtPointer client_data, call_data;
8495 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8496 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8497 int part = -1, whole = 0, portion = 0;
8499 switch (cs->reason)
8501 case XmCR_DECREMENT:
8502 bar->dragging = Qnil;
8503 part = scroll_bar_up_arrow;
8504 break;
8506 case XmCR_INCREMENT:
8507 bar->dragging = Qnil;
8508 part = scroll_bar_down_arrow;
8509 break;
8511 case XmCR_PAGE_DECREMENT:
8512 bar->dragging = Qnil;
8513 part = scroll_bar_above_handle;
8514 break;
8516 case XmCR_PAGE_INCREMENT:
8517 bar->dragging = Qnil;
8518 part = scroll_bar_below_handle;
8519 break;
8521 case XmCR_TO_TOP:
8522 bar->dragging = Qnil;
8523 part = scroll_bar_to_top;
8524 break;
8526 case XmCR_TO_BOTTOM:
8527 bar->dragging = Qnil;
8528 part = scroll_bar_to_bottom;
8529 break;
8531 case XmCR_DRAG:
8533 int slider_size;
8534 int dragging_down_p = (INTEGERP (bar->dragging)
8535 && XINT (bar->dragging) <= cs->value);
8537 /* Get the slider size. */
8538 BLOCK_INPUT;
8539 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8540 UNBLOCK_INPUT;
8542 whole = XM_SB_RANGE - slider_size;
8543 portion = min (cs->value - XM_SB_MIN, whole);
8544 part = scroll_bar_handle;
8545 bar->dragging = make_number (cs->value);
8547 break;
8549 case XmCR_VALUE_CHANGED:
8550 break;
8553 if (part >= 0)
8555 window_being_scrolled = bar->window;
8556 last_scroll_bar_part = part;
8557 x_send_scroll_bar_event (bar->window, part, portion, whole);
8562 #else /* !USE_MOTIF, i.e. Xaw. */
8565 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8566 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8567 scroll bar struct. CALL_DATA is a pointer to a float saying where
8568 the thumb is. */
8570 static void
8571 xaw_jump_callback (widget, client_data, call_data)
8572 Widget widget;
8573 XtPointer client_data, call_data;
8575 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8576 float top = *(float *) call_data;
8577 float shown;
8578 int whole, portion, height;
8579 int part;
8581 /* Get the size of the thumb, a value between 0 and 1. */
8582 BLOCK_INPUT;
8583 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8584 UNBLOCK_INPUT;
8586 whole = 10000000;
8587 portion = shown < 1 ? top * whole : 0;
8589 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8590 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8591 the bottom, so we force the scrolling whenever we see that we're
8592 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8593 we try to ensure that we always stay two pixels away from the
8594 bottom). */
8595 part = scroll_bar_down_arrow;
8596 else
8597 part = scroll_bar_handle;
8599 window_being_scrolled = bar->window;
8600 bar->dragging = make_number (portion);
8601 last_scroll_bar_part = part;
8602 x_send_scroll_bar_event (bar->window, part, portion, whole);
8606 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8607 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8608 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8609 the scroll bar. CALL_DATA is an integer specifying the action that
8610 has taken place. It's magnitude is in the range 0..height of the
8611 scroll bar. Negative values mean scroll towards buffer start.
8612 Values < height of scroll bar mean line-wise movement. */
8614 static void
8615 xaw_scroll_callback (widget, client_data, call_data)
8616 Widget widget;
8617 XtPointer client_data, call_data;
8619 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8620 int position = (int) call_data;
8621 Dimension height;
8622 int part;
8624 /* Get the height of the scroll bar. */
8625 BLOCK_INPUT;
8626 XtVaGetValues (widget, XtNheight, &height, NULL);
8627 UNBLOCK_INPUT;
8629 if (abs (position) >= height)
8630 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8632 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8633 it maps line-movement to call_data = max(5, height/20). */
8634 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8635 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8636 else
8637 part = scroll_bar_move_ratio;
8639 window_being_scrolled = bar->window;
8640 bar->dragging = Qnil;
8641 last_scroll_bar_part = part;
8642 x_send_scroll_bar_event (bar->window, part, position, height);
8646 #endif /* not USE_MOTIF */
8649 /* Create the widget for scroll bar BAR on frame F. Record the widget
8650 and X window of the scroll bar in BAR. */
8652 static void
8653 x_create_toolkit_scroll_bar (f, bar)
8654 struct frame *f;
8655 struct scroll_bar *bar;
8657 Window xwindow;
8658 Widget widget;
8659 Arg av[20];
8660 int ac = 0;
8661 char *scroll_bar_name = "verticalScrollBar";
8662 unsigned long pixel;
8664 BLOCK_INPUT;
8666 #ifdef USE_MOTIF
8667 /* Set resources. Create the widget. */
8668 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8669 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8670 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8671 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8672 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8673 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8674 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8676 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8677 if (pixel != -1)
8679 XtSetArg (av[ac], XmNforeground, pixel);
8680 ++ac;
8683 pixel = f->output_data.x->scroll_bar_background_pixel;
8684 if (pixel != -1)
8686 XtSetArg (av[ac], XmNbackground, pixel);
8687 ++ac;
8690 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8691 scroll_bar_name, av, ac);
8693 /* Add one callback for everything that can happen. */
8694 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8695 (XtPointer) bar);
8696 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8697 (XtPointer) bar);
8698 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8699 (XtPointer) bar);
8700 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8701 (XtPointer) bar);
8702 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8703 (XtPointer) bar);
8704 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8705 (XtPointer) bar);
8706 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8707 (XtPointer) bar);
8709 /* Realize the widget. Only after that is the X window created. */
8710 XtRealizeWidget (widget);
8712 /* Set the cursor to an arrow. I didn't find a resource to do that.
8713 And I'm wondering why it hasn't an arrow cursor by default. */
8714 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8715 f->output_data.x->nontext_cursor);
8717 #else /* !USE_MOTIF i.e. use Xaw */
8719 /* Set resources. Create the widget. The background of the
8720 Xaw3d scroll bar widget is a little bit light for my taste.
8721 We don't alter it here to let users change it according
8722 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8723 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8724 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8725 /* For smoother scrolling with Xaw3d -sm */
8726 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8728 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8729 if (pixel != -1)
8731 XtSetArg (av[ac], XtNforeground, pixel);
8732 ++ac;
8735 pixel = f->output_data.x->scroll_bar_background_pixel;
8736 if (pixel != -1)
8738 XtSetArg (av[ac], XtNbackground, pixel);
8739 ++ac;
8742 /* Top/bottom shadow colors. */
8744 /* Allocate them, if necessary. */
8745 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8747 pixel = f->output_data.x->scroll_bar_background_pixel;
8748 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8749 &pixel, 1.2, 0x8000))
8750 pixel = -1;
8751 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8753 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8755 pixel = f->output_data.x->scroll_bar_background_pixel;
8756 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8757 &pixel, 0.6, 0x4000))
8758 pixel = -1;
8759 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8762 /* Tell the toolkit about them. */
8763 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8764 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8765 /* We tried to allocate a color for the top/bottom shadow, and
8766 failed, so tell Xaw3d to use dithering instead. */
8768 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8769 ++ac;
8771 else
8772 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8773 be more consistent with other emacs 3d colors, and since Xaw3d is
8774 not good at dealing with allocation failure. */
8776 /* This tells Xaw3d to use real colors instead of dithering for
8777 the shadows. */
8778 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8779 ++ac;
8781 /* Specify the colors. */
8782 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8783 if (pixel != -1)
8785 XtSetArg (av[ac], "topShadowPixel", pixel);
8786 ++ac;
8788 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8789 if (pixel != -1)
8791 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8792 ++ac;
8796 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8797 f->output_data.x->edit_widget, av, ac);
8800 char *initial = "";
8801 char *val = initial;
8802 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8803 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8804 if (val == initial)
8805 { /* ARROW_SCROLL */
8806 xaw3d_arrow_scroll = True;
8807 /* Isn't that just a personal preference ? -sm */
8808 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8812 /* Define callbacks. */
8813 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8814 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8815 (XtPointer) bar);
8817 /* Realize the widget. Only after that is the X window created. */
8818 XtRealizeWidget (widget);
8820 #endif /* !USE_MOTIF */
8822 /* Install an action hook that let's us detect when the user
8823 finishes interacting with a scroll bar. */
8824 if (action_hook_id == 0)
8825 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8827 /* Remember X window and widget in the scroll bar vector. */
8828 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8829 xwindow = XtWindow (widget);
8830 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8832 UNBLOCK_INPUT;
8836 /* Set the thumb size and position of scroll bar BAR. We are currently
8837 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8839 static void
8840 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8841 struct scroll_bar *bar;
8842 int portion, position, whole;
8844 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8845 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8846 float top, shown;
8848 BLOCK_INPUT;
8850 #ifdef USE_MOTIF
8852 /* We use an estimate of 30 chars per line rather than the real
8853 `portion' value. This has the disadvantage that the thumb size
8854 is not very representative, but it makes our life a lot easier.
8855 Otherwise, we have to constantly adjust the thumb size, which
8856 we can't always do quickly enough: while dragging, the size of
8857 the thumb might prevent the user from dragging the thumb all the
8858 way to the end. but Motif and some versions of Xaw3d don't allow
8859 updating the thumb size while dragging. Also, even if we can update
8860 its size, the update will often happen too late.
8861 If you don't believe it, check out revision 1.650 of xterm.c to see
8862 what hoops we were going through and the still poor behavior we got. */
8863 portion = XFASTINT (XWINDOW (bar->window)->height) * 30;
8864 /* When the thumb is at the bottom, position == whole.
8865 So we need to increase `whole' to make space for the thumb. */
8866 whole += portion;
8868 if (whole <= 0)
8869 top = 0, shown = 1;
8870 else
8872 top = (float) position / whole;
8873 shown = (float) portion / whole;
8876 if (NILP (bar->dragging))
8878 int size, value;
8880 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8881 is the scroll bar's maximum and MIN is the scroll bar's minimum
8882 value. */
8883 size = shown * XM_SB_RANGE;
8884 size = min (size, XM_SB_RANGE);
8885 size = max (size, 1);
8887 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8888 value = top * XM_SB_RANGE;
8889 value = min (value, XM_SB_MAX - size);
8890 value = max (value, XM_SB_MIN);
8892 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8894 #else /* !USE_MOTIF i.e. use Xaw */
8896 if (whole == 0)
8897 top = 0, shown = 1;
8898 else
8900 top = (float) position / whole;
8901 shown = (float) portion / whole;
8905 float old_top, old_shown;
8906 Dimension height;
8907 XtVaGetValues (widget,
8908 XtNtopOfThumb, &old_top,
8909 XtNshown, &old_shown,
8910 XtNheight, &height,
8911 NULL);
8913 /* Massage the top+shown values. */
8914 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8915 top = max (0, min (1, top));
8916 else
8917 top = old_top;
8918 /* Keep two pixels available for moving the thumb down. */
8919 shown = max (0, min (1 - top - (2.0 / height), shown));
8921 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8922 check that your system's configuration file contains a define
8923 for `NARROWPROTO'. See s/freebsd.h for an example. */
8924 if (top != old_top || shown != old_shown)
8926 if (NILP (bar->dragging))
8927 XawScrollbarSetThumb (widget, top, shown);
8928 else
8930 #ifdef HAVE_XAW3D
8931 ScrollbarWidget sb = (ScrollbarWidget) widget;
8932 int scroll_mode = 0;
8934 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8935 if (xaw3d_arrow_scroll)
8937 /* Xaw3d stupidly ignores resize requests while dragging
8938 so we have to make it believe it's not in dragging mode. */
8939 scroll_mode = sb->scrollbar.scroll_mode;
8940 if (scroll_mode == 2)
8941 sb->scrollbar.scroll_mode = 0;
8943 #endif
8944 /* Try to make the scrolling a tad smoother. */
8945 if (!xaw3d_pick_top)
8946 shown = min (shown, old_shown);
8948 XawScrollbarSetThumb (widget, top, shown);
8950 #ifdef HAVE_XAW3D
8951 if (xaw3d_arrow_scroll && scroll_mode == 2)
8952 sb->scrollbar.scroll_mode = scroll_mode;
8953 #endif
8957 #endif /* !USE_MOTIF */
8959 UNBLOCK_INPUT;
8962 #endif /* USE_TOOLKIT_SCROLL_BARS */
8966 /************************************************************************
8967 Scroll bars, general
8968 ************************************************************************/
8970 /* Create a scroll bar and return the scroll bar vector for it. W is
8971 the Emacs window on which to create the scroll bar. TOP, LEFT,
8972 WIDTH and HEIGHT are the pixel coordinates and dimensions of the
8973 scroll bar. */
8975 static struct scroll_bar *
8976 x_scroll_bar_create (w, top, left, width, height)
8977 struct window *w;
8978 int top, left, width, height;
8980 struct frame *f = XFRAME (w->frame);
8981 struct scroll_bar *bar
8982 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8984 BLOCK_INPUT;
8986 #ifdef USE_TOOLKIT_SCROLL_BARS
8987 x_create_toolkit_scroll_bar (f, bar);
8988 #else /* not USE_TOOLKIT_SCROLL_BARS */
8990 XSetWindowAttributes a;
8991 unsigned long mask;
8992 Window window;
8994 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
8995 if (a.background_pixel == -1)
8996 a.background_pixel = f->output_data.x->background_pixel;
8998 a.event_mask = (ButtonPressMask | ButtonReleaseMask
8999 | ButtonMotionMask | PointerMotionHintMask
9000 | ExposureMask);
9001 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
9003 mask = (CWBackPixel | CWEventMask | CWCursor);
9005 /* Clear the area of W that will serve as a scroll bar. This is
9006 for the case that a window has been split horizontally. In
9007 this case, no clear_frame is generated to reduce flickering. */
9008 if (width > 0 && height > 0)
9009 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9010 left, top, width,
9011 window_box_height (w), False);
9013 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9014 /* Position and size of scroll bar. */
9015 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9016 top,
9017 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9018 height,
9019 /* Border width, depth, class, and visual. */
9021 CopyFromParent,
9022 CopyFromParent,
9023 CopyFromParent,
9024 /* Attributes. */
9025 mask, &a);
9026 SET_SCROLL_BAR_X_WINDOW (bar, window);
9028 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9030 XSETWINDOW (bar->window, w);
9031 XSETINT (bar->top, top);
9032 XSETINT (bar->left, left);
9033 XSETINT (bar->width, width);
9034 XSETINT (bar->height, height);
9035 XSETINT (bar->start, 0);
9036 XSETINT (bar->end, 0);
9037 bar->dragging = Qnil;
9039 /* Add bar to its frame's list of scroll bars. */
9040 bar->next = FRAME_SCROLL_BARS (f);
9041 bar->prev = Qnil;
9042 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9043 if (!NILP (bar->next))
9044 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9046 /* Map the window/widget. */
9047 #ifdef USE_TOOLKIT_SCROLL_BARS
9049 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9050 XtConfigureWidget (scroll_bar,
9051 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9052 top,
9053 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9054 max (height, 1), 0);
9055 XtMapWidget (scroll_bar);
9057 #else /* not USE_TOOLKIT_SCROLL_BARS */
9058 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9059 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9061 UNBLOCK_INPUT;
9062 return bar;
9066 /* Draw BAR's handle in the proper position.
9068 If the handle is already drawn from START to END, don't bother
9069 redrawing it, unless REBUILD is non-zero; in that case, always
9070 redraw it. (REBUILD is handy for drawing the handle after expose
9071 events.)
9073 Normally, we want to constrain the start and end of the handle to
9074 fit inside its rectangle, but if the user is dragging the scroll
9075 bar handle, we want to let them drag it down all the way, so that
9076 the bar's top is as far down as it goes; otherwise, there's no way
9077 to move to the very end of the buffer. */
9079 #ifndef USE_TOOLKIT_SCROLL_BARS
9081 static void
9082 x_scroll_bar_set_handle (bar, start, end, rebuild)
9083 struct scroll_bar *bar;
9084 int start, end;
9085 int rebuild;
9087 int dragging = ! NILP (bar->dragging);
9088 Window w = SCROLL_BAR_X_WINDOW (bar);
9089 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9090 GC gc = f->output_data.x->normal_gc;
9092 /* If the display is already accurate, do nothing. */
9093 if (! rebuild
9094 && start == XINT (bar->start)
9095 && end == XINT (bar->end))
9096 return;
9098 BLOCK_INPUT;
9101 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9102 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9103 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9105 /* Make sure the values are reasonable, and try to preserve
9106 the distance between start and end. */
9108 int length = end - start;
9110 if (start < 0)
9111 start = 0;
9112 else if (start > top_range)
9113 start = top_range;
9114 end = start + length;
9116 if (end < start)
9117 end = start;
9118 else if (end > top_range && ! dragging)
9119 end = top_range;
9122 /* Store the adjusted setting in the scroll bar. */
9123 XSETINT (bar->start, start);
9124 XSETINT (bar->end, end);
9126 /* Clip the end position, just for display. */
9127 if (end > top_range)
9128 end = top_range;
9130 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9131 below top positions, to make sure the handle is always at least
9132 that many pixels tall. */
9133 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9135 /* Draw the empty space above the handle. Note that we can't clear
9136 zero-height areas; that means "clear to end of window." */
9137 if (0 < start)
9138 x_clear_area (FRAME_X_DISPLAY (f), w,
9139 /* x, y, width, height, and exposures. */
9140 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9141 VERTICAL_SCROLL_BAR_TOP_BORDER,
9142 inside_width, start,
9143 False);
9145 /* Change to proper foreground color if one is specified. */
9146 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9147 XSetForeground (FRAME_X_DISPLAY (f), gc,
9148 f->output_data.x->scroll_bar_foreground_pixel);
9150 /* Draw the handle itself. */
9151 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9152 /* x, y, width, height */
9153 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9154 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9155 inside_width, end - start);
9157 /* Restore the foreground color of the GC if we changed it above. */
9158 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9159 XSetForeground (FRAME_X_DISPLAY (f), gc,
9160 f->output_data.x->foreground_pixel);
9162 /* Draw the empty space below the handle. Note that we can't
9163 clear zero-height areas; that means "clear to end of window." */
9164 if (end < inside_height)
9165 x_clear_area (FRAME_X_DISPLAY (f), w,
9166 /* x, y, width, height, and exposures. */
9167 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9168 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9169 inside_width, inside_height - end,
9170 False);
9174 UNBLOCK_INPUT;
9177 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9179 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9180 nil. */
9182 static void
9183 x_scroll_bar_remove (bar)
9184 struct scroll_bar *bar;
9186 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9187 BLOCK_INPUT;
9189 #ifdef USE_TOOLKIT_SCROLL_BARS
9190 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9191 #else
9192 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9193 #endif
9195 /* Disassociate this scroll bar from its window. */
9196 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9198 UNBLOCK_INPUT;
9202 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9203 that we are displaying PORTION characters out of a total of WHOLE
9204 characters, starting at POSITION. If WINDOW has no scroll bar,
9205 create one. */
9207 static void
9208 XTset_vertical_scroll_bar (w, portion, whole, position)
9209 struct window *w;
9210 int portion, whole, position;
9212 struct frame *f = XFRAME (w->frame);
9213 struct scroll_bar *bar;
9214 int top, height, left, sb_left, width, sb_width;
9215 int window_x, window_y, window_width, window_height;
9217 /* Get window dimensions. */
9218 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9219 top = window_y;
9220 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9221 height = window_height;
9223 /* Compute the left edge of the scroll bar area. */
9224 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9225 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9226 else
9227 left = XFASTINT (w->left);
9228 left *= CANON_X_UNIT (f);
9229 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9231 /* Compute the width of the scroll bar which might be less than
9232 the width of the area reserved for the scroll bar. */
9233 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9234 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9235 else
9236 sb_width = width;
9238 /* Compute the left edge of the scroll bar. */
9239 #ifdef USE_TOOLKIT_SCROLL_BARS
9240 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9241 sb_left = left + width - sb_width - (width - sb_width) / 2;
9242 else
9243 sb_left = left + (width - sb_width) / 2;
9244 #else
9245 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9246 sb_left = left + width - sb_width;
9247 else
9248 sb_left = left;
9249 #endif
9251 /* Does the scroll bar exist yet? */
9252 if (NILP (w->vertical_scroll_bar))
9254 if (width > 0 && height > 0)
9256 BLOCK_INPUT;
9257 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9258 left, top, width, height, False);
9259 UNBLOCK_INPUT;
9262 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9264 else
9266 /* It may just need to be moved and resized. */
9267 unsigned int mask = 0;
9269 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9271 BLOCK_INPUT;
9273 if (sb_left != XINT (bar->left))
9274 mask |= CWX;
9275 if (top != XINT (bar->top))
9276 mask |= CWY;
9277 if (sb_width != XINT (bar->width))
9278 mask |= CWWidth;
9279 if (height != XINT (bar->height))
9280 mask |= CWHeight;
9282 #ifdef USE_TOOLKIT_SCROLL_BARS
9284 /* Since toolkit scroll bars are smaller than the space reserved
9285 for them on the frame, we have to clear "under" them. */
9286 if (width > 0 && height > 0)
9287 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9288 left, top, width, height, False);
9290 /* Move/size the scroll bar widget. */
9291 if (mask)
9292 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9293 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9294 top,
9295 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9296 max (height, 1), 0);
9298 #else /* not USE_TOOLKIT_SCROLL_BARS */
9300 /* Clear areas not covered by the scroll bar because of
9301 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9302 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9304 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9305 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9306 height, False);
9307 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9308 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9309 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9310 height, False);
9313 /* Clear areas not covered by the scroll bar because it's not as
9314 wide as the area reserved for it. This makes sure a
9315 previous mode line display is cleared after C-x 2 C-x 1, for
9316 example. */
9318 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9319 int rest = area_width - sb_width;
9320 if (rest > 0 && height > 0)
9322 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9323 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9324 left + area_width - rest, top,
9325 rest, height, False);
9326 else
9327 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9328 left, top, rest, height, False);
9332 /* Move/size the scroll bar window. */
9333 if (mask)
9335 XWindowChanges wc;
9337 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9338 wc.y = top;
9339 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9340 wc.height = height;
9341 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9342 mask, &wc);
9345 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9347 /* Remember new settings. */
9348 XSETINT (bar->left, sb_left);
9349 XSETINT (bar->top, top);
9350 XSETINT (bar->width, sb_width);
9351 XSETINT (bar->height, height);
9353 UNBLOCK_INPUT;
9356 #ifdef USE_TOOLKIT_SCROLL_BARS
9357 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9358 #else /* not USE_TOOLKIT_SCROLL_BARS */
9359 /* Set the scroll bar's current state, unless we're currently being
9360 dragged. */
9361 if (NILP (bar->dragging))
9363 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9365 if (whole == 0)
9366 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9367 else
9369 int start = ((double) position * top_range) / whole;
9370 int end = ((double) (position + portion) * top_range) / whole;
9371 x_scroll_bar_set_handle (bar, start, end, 0);
9374 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9376 XSETVECTOR (w->vertical_scroll_bar, bar);
9380 /* The following three hooks are used when we're doing a thorough
9381 redisplay of the frame. We don't explicitly know which scroll bars
9382 are going to be deleted, because keeping track of when windows go
9383 away is a real pain - "Can you say set-window-configuration, boys
9384 and girls?" Instead, we just assert at the beginning of redisplay
9385 that *all* scroll bars are to be removed, and then save a scroll bar
9386 from the fiery pit when we actually redisplay its window. */
9388 /* Arrange for all scroll bars on FRAME to be removed at the next call
9389 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9390 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9392 static void
9393 XTcondemn_scroll_bars (frame)
9394 FRAME_PTR frame;
9396 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9397 while (! NILP (FRAME_SCROLL_BARS (frame)))
9399 Lisp_Object bar;
9400 bar = FRAME_SCROLL_BARS (frame);
9401 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9402 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9403 XSCROLL_BAR (bar)->prev = Qnil;
9404 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9405 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9406 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9411 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9412 Note that WINDOW isn't necessarily condemned at all. */
9414 static void
9415 XTredeem_scroll_bar (window)
9416 struct window *window;
9418 struct scroll_bar *bar;
9419 struct frame *f;
9421 /* We can't redeem this window's scroll bar if it doesn't have one. */
9422 if (NILP (window->vertical_scroll_bar))
9423 abort ();
9425 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9427 /* Unlink it from the condemned list. */
9428 f = XFRAME (WINDOW_FRAME (window));
9429 if (NILP (bar->prev))
9431 /* If the prev pointer is nil, it must be the first in one of
9432 the lists. */
9433 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9434 /* It's not condemned. Everything's fine. */
9435 return;
9436 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9437 window->vertical_scroll_bar))
9438 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9439 else
9440 /* If its prev pointer is nil, it must be at the front of
9441 one or the other! */
9442 abort ();
9444 else
9445 XSCROLL_BAR (bar->prev)->next = bar->next;
9447 if (! NILP (bar->next))
9448 XSCROLL_BAR (bar->next)->prev = bar->prev;
9450 bar->next = FRAME_SCROLL_BARS (f);
9451 bar->prev = Qnil;
9452 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9453 if (! NILP (bar->next))
9454 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9457 /* Remove all scroll bars on FRAME that haven't been saved since the
9458 last call to `*condemn_scroll_bars_hook'. */
9460 static void
9461 XTjudge_scroll_bars (f)
9462 FRAME_PTR f;
9464 Lisp_Object bar, next;
9466 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9468 /* Clear out the condemned list now so we won't try to process any
9469 more events on the hapless scroll bars. */
9470 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9472 for (; ! NILP (bar); bar = next)
9474 struct scroll_bar *b = XSCROLL_BAR (bar);
9476 x_scroll_bar_remove (b);
9478 next = b->next;
9479 b->next = b->prev = Qnil;
9482 /* Now there should be no references to the condemned scroll bars,
9483 and they should get garbage-collected. */
9487 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9488 is a no-op when using toolkit scroll bars.
9490 This may be called from a signal handler, so we have to ignore GC
9491 mark bits. */
9493 static void
9494 x_scroll_bar_expose (bar, event)
9495 struct scroll_bar *bar;
9496 XEvent *event;
9498 #ifndef USE_TOOLKIT_SCROLL_BARS
9500 Window w = SCROLL_BAR_X_WINDOW (bar);
9501 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9502 GC gc = f->output_data.x->normal_gc;
9503 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9505 BLOCK_INPUT;
9507 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9509 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9510 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9512 /* x, y, width, height */
9513 0, 0,
9514 XINT (bar->width) - 1 - width_trim - width_trim,
9515 XINT (bar->height) - 1);
9517 UNBLOCK_INPUT;
9519 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9522 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9523 is set to something other than no_event, it is enqueued.
9525 This may be called from a signal handler, so we have to ignore GC
9526 mark bits. */
9528 #ifndef USE_TOOLKIT_SCROLL_BARS
9530 static void
9531 x_scroll_bar_handle_click (bar, event, emacs_event)
9532 struct scroll_bar *bar;
9533 XEvent *event;
9534 struct input_event *emacs_event;
9536 if (! GC_WINDOWP (bar->window))
9537 abort ();
9539 emacs_event->kind = scroll_bar_click;
9540 emacs_event->code = event->xbutton.button - Button1;
9541 emacs_event->modifiers
9542 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9543 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9544 event->xbutton.state)
9545 | (event->type == ButtonRelease
9546 ? up_modifier
9547 : down_modifier));
9548 emacs_event->frame_or_window = bar->window;
9549 emacs_event->arg = Qnil;
9550 emacs_event->timestamp = event->xbutton.time;
9552 #if 0
9553 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9554 int internal_height
9555 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9556 #endif
9557 int top_range
9558 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9559 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9561 if (y < 0) y = 0;
9562 if (y > top_range) y = top_range;
9564 if (y < XINT (bar->start))
9565 emacs_event->part = scroll_bar_above_handle;
9566 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9567 emacs_event->part = scroll_bar_handle;
9568 else
9569 emacs_event->part = scroll_bar_below_handle;
9571 /* Just because the user has clicked on the handle doesn't mean
9572 they want to drag it. Lisp code needs to be able to decide
9573 whether or not we're dragging. */
9574 #if 0
9575 /* If the user has just clicked on the handle, record where they're
9576 holding it. */
9577 if (event->type == ButtonPress
9578 && emacs_event->part == scroll_bar_handle)
9579 XSETINT (bar->dragging, y - XINT (bar->start));
9580 #endif
9582 /* If the user has released the handle, set it to its final position. */
9583 if (event->type == ButtonRelease
9584 && ! NILP (bar->dragging))
9586 int new_start = y - XINT (bar->dragging);
9587 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9589 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9590 bar->dragging = Qnil;
9593 /* Same deal here as the other #if 0. */
9594 #if 0
9595 /* Clicks on the handle are always reported as occurring at the top of
9596 the handle. */
9597 if (emacs_event->part == scroll_bar_handle)
9598 emacs_event->x = bar->start;
9599 else
9600 XSETINT (emacs_event->x, y);
9601 #else
9602 XSETINT (emacs_event->x, y);
9603 #endif
9605 XSETINT (emacs_event->y, top_range);
9609 /* Handle some mouse motion while someone is dragging the scroll bar.
9611 This may be called from a signal handler, so we have to ignore GC
9612 mark bits. */
9614 static void
9615 x_scroll_bar_note_movement (bar, event)
9616 struct scroll_bar *bar;
9617 XEvent *event;
9619 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9621 last_mouse_movement_time = event->xmotion.time;
9623 f->mouse_moved = 1;
9624 XSETVECTOR (last_mouse_scroll_bar, bar);
9626 /* If we're dragging the bar, display it. */
9627 if (! GC_NILP (bar->dragging))
9629 /* Where should the handle be now? */
9630 int new_start = event->xmotion.y - XINT (bar->dragging);
9632 if (new_start != XINT (bar->start))
9634 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9636 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9641 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9643 /* Return information to the user about the current position of the mouse
9644 on the scroll bar. */
9646 static void
9647 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9648 FRAME_PTR *fp;
9649 Lisp_Object *bar_window;
9650 enum scroll_bar_part *part;
9651 Lisp_Object *x, *y;
9652 unsigned long *time;
9654 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9655 Window w = SCROLL_BAR_X_WINDOW (bar);
9656 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9657 int win_x, win_y;
9658 Window dummy_window;
9659 int dummy_coord;
9660 unsigned int dummy_mask;
9662 BLOCK_INPUT;
9664 /* Get the mouse's position relative to the scroll bar window, and
9665 report that. */
9666 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9668 /* Root, child, root x and root y. */
9669 &dummy_window, &dummy_window,
9670 &dummy_coord, &dummy_coord,
9672 /* Position relative to scroll bar. */
9673 &win_x, &win_y,
9675 /* Mouse buttons and modifier keys. */
9676 &dummy_mask))
9678 else
9680 #if 0
9681 int inside_height
9682 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9683 #endif
9684 int top_range
9685 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9687 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9689 if (! NILP (bar->dragging))
9690 win_y -= XINT (bar->dragging);
9692 if (win_y < 0)
9693 win_y = 0;
9694 if (win_y > top_range)
9695 win_y = top_range;
9697 *fp = f;
9698 *bar_window = bar->window;
9700 if (! NILP (bar->dragging))
9701 *part = scroll_bar_handle;
9702 else if (win_y < XINT (bar->start))
9703 *part = scroll_bar_above_handle;
9704 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9705 *part = scroll_bar_handle;
9706 else
9707 *part = scroll_bar_below_handle;
9709 XSETINT (*x, win_y);
9710 XSETINT (*y, top_range);
9712 f->mouse_moved = 0;
9713 last_mouse_scroll_bar = Qnil;
9716 *time = last_mouse_movement_time;
9718 UNBLOCK_INPUT;
9722 /* The screen has been cleared so we may have changed foreground or
9723 background colors, and the scroll bars may need to be redrawn.
9724 Clear out the scroll bars, and ask for expose events, so we can
9725 redraw them. */
9727 void
9728 x_scroll_bar_clear (f)
9729 FRAME_PTR f;
9731 #ifndef USE_TOOLKIT_SCROLL_BARS
9732 Lisp_Object bar;
9734 /* We can have scroll bars even if this is 0,
9735 if we just turned off scroll bar mode.
9736 But in that case we should not clear them. */
9737 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9738 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9739 bar = XSCROLL_BAR (bar)->next)
9740 XClearArea (FRAME_X_DISPLAY (f),
9741 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9742 0, 0, 0, 0, True);
9743 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9746 /* This processes Expose events from the menu-bar specific X event
9747 loop in xmenu.c. This allows to redisplay the frame if necessary
9748 when handling menu-bar or pop-up items. */
9751 process_expose_from_menu (event)
9752 XEvent event;
9754 FRAME_PTR f;
9755 struct x_display_info *dpyinfo;
9756 int frame_exposed_p = 0;
9758 BLOCK_INPUT;
9760 dpyinfo = x_display_info_for_display (event.xexpose.display);
9761 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9762 if (f)
9764 if (f->async_visible == 0)
9766 f->async_visible = 1;
9767 f->async_iconified = 0;
9768 f->output_data.x->has_been_visible = 1;
9769 SET_FRAME_GARBAGED (f);
9771 else
9773 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9774 event.xexpose.x, event.xexpose.y,
9775 event.xexpose.width, event.xexpose.height);
9776 frame_exposed_p = 1;
9779 else
9781 struct scroll_bar *bar
9782 = x_window_to_scroll_bar (event.xexpose.window);
9784 if (bar)
9785 x_scroll_bar_expose (bar, &event);
9788 UNBLOCK_INPUT;
9789 return frame_exposed_p;
9792 /* Define a queue to save up SelectionRequest events for later handling. */
9794 struct selection_event_queue
9796 XEvent event;
9797 struct selection_event_queue *next;
9800 static struct selection_event_queue *queue;
9802 /* Nonzero means queue up certain events--don't process them yet. */
9804 static int x_queue_selection_requests;
9806 /* Queue up an X event *EVENT, to be processed later. */
9808 static void
9809 x_queue_event (f, event)
9810 FRAME_PTR f;
9811 XEvent *event;
9813 struct selection_event_queue *queue_tmp
9814 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9816 if (queue_tmp != NULL)
9818 queue_tmp->event = *event;
9819 queue_tmp->next = queue;
9820 queue = queue_tmp;
9824 /* Take all the queued events and put them back
9825 so that they get processed afresh. */
9827 static void
9828 x_unqueue_events (display)
9829 Display *display;
9831 while (queue != NULL)
9833 struct selection_event_queue *queue_tmp = queue;
9834 XPutBackEvent (display, &queue_tmp->event);
9835 queue = queue_tmp->next;
9836 xfree ((char *)queue_tmp);
9840 /* Start queuing SelectionRequest events. */
9842 void
9843 x_start_queuing_selection_requests (display)
9844 Display *display;
9846 x_queue_selection_requests++;
9849 /* Stop queuing SelectionRequest events. */
9851 void
9852 x_stop_queuing_selection_requests (display)
9853 Display *display;
9855 x_queue_selection_requests--;
9856 x_unqueue_events (display);
9859 /* The main X event-reading loop - XTread_socket. */
9861 #if 0
9862 /* Time stamp of enter window event. This is only used by XTread_socket,
9863 but we have to put it out here, since static variables within functions
9864 sometimes don't work. */
9866 static Time enter_timestamp;
9867 #endif
9869 /* This holds the state XLookupString needs to implement dead keys
9870 and other tricks known as "compose processing". _X Window System_
9871 says that a portable program can't use this, but Stephen Gildea assures
9872 me that letting the compiler initialize it to zeros will work okay.
9874 This must be defined outside of XTread_socket, for the same reasons
9875 given for enter_timestamp, above. */
9877 static XComposeStatus compose_status;
9879 /* Record the last 100 characters stored
9880 to help debug the loss-of-chars-during-GC problem. */
9882 static int temp_index;
9883 static short temp_buffer[100];
9885 /* Set this to nonzero to fake an "X I/O error"
9886 on a particular display. */
9888 struct x_display_info *XTread_socket_fake_io_error;
9890 /* When we find no input here, we occasionally do a no-op command
9891 to verify that the X server is still running and we can still talk with it.
9892 We try all the open displays, one by one.
9893 This variable is used for cycling thru the displays. */
9895 static struct x_display_info *next_noop_dpyinfo;
9897 #define SET_SAVED_MENU_EVENT(size) \
9898 do \
9900 if (f->output_data.x->saved_menu_event == 0) \
9901 f->output_data.x->saved_menu_event \
9902 = (XEvent *) xmalloc (sizeof (XEvent)); \
9903 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9904 if (numchars >= 1) \
9906 bufp->kind = menu_bar_activate_event; \
9907 XSETFRAME (bufp->frame_or_window, f); \
9908 bufp->arg = Qnil; \
9909 bufp++; \
9910 count++; \
9911 numchars--; \
9914 while (0)
9916 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9917 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9919 /* Read events coming from the X server.
9920 This routine is called by the SIGIO handler.
9921 We return as soon as there are no more events to be read.
9923 Events representing keys are stored in buffer BUFP,
9924 which can hold up to NUMCHARS characters.
9925 We return the number of characters stored into the buffer,
9926 thus pretending to be `read'.
9928 EXPECTED is nonzero if the caller knows input is available. */
9930 static int
9931 XTread_socket (sd, bufp, numchars, expected)
9932 register int sd;
9933 /* register */ struct input_event *bufp;
9934 /* register */ int numchars;
9935 int expected;
9937 int count = 0;
9938 int nbytes = 0;
9939 XEvent event;
9940 struct frame *f;
9941 int event_found = 0;
9942 struct x_display_info *dpyinfo;
9943 struct coding_system coding;
9945 if (interrupt_input_blocked)
9947 interrupt_input_pending = 1;
9948 return -1;
9951 interrupt_input_pending = 0;
9952 BLOCK_INPUT;
9954 /* So people can tell when we have read the available input. */
9955 input_signal_count++;
9957 if (numchars <= 0)
9958 abort (); /* Don't think this happens. */
9960 ++handling_signal;
9962 /* The input should be decoded if it is from XIM. Currently the
9963 locale of XIM is the same as that of the system. So, we can use
9964 Vlocale_coding_system which is initialized properly at Emacs
9965 startup time. */
9966 setup_coding_system (Vlocale_coding_system, &coding);
9967 coding.src_multibyte = 0;
9968 coding.dst_multibyte = 1;
9969 /* The input is converted to events, thus we can't handle
9970 composition. Anyway, there's no XIM that gives us composition
9971 information. */
9972 coding.composing = COMPOSITION_DISABLED;
9974 /* Find the display we are supposed to read input for.
9975 It's the one communicating on descriptor SD. */
9976 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9978 #if 0 /* This ought to be unnecessary; let's verify it. */
9979 #ifdef FIOSNBIO
9980 /* If available, Xlib uses FIOSNBIO to make the socket
9981 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9982 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9983 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9984 fcntl (dpyinfo->connection, F_SETFL, 0);
9985 #endif /* ! defined (FIOSNBIO) */
9986 #endif
9988 #if 0 /* This code can't be made to work, with multiple displays,
9989 and appears not to be used on any system any more.
9990 Also keyboard.c doesn't turn O_NDELAY on and off
9991 for X connections. */
9992 #ifndef SIGIO
9993 #ifndef HAVE_SELECT
9994 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
9996 extern int read_alarm_should_throw;
9997 read_alarm_should_throw = 1;
9998 XPeekEvent (dpyinfo->display, &event);
9999 read_alarm_should_throw = 0;
10001 #endif /* HAVE_SELECT */
10002 #endif /* SIGIO */
10003 #endif
10005 /* For debugging, this gives a way to fake an I/O error. */
10006 if (dpyinfo == XTread_socket_fake_io_error)
10008 XTread_socket_fake_io_error = 0;
10009 x_io_error_quitter (dpyinfo->display);
10012 #ifdef HAVE_X_SM
10013 BLOCK_INPUT;
10014 count += x_session_check_input (bufp, &numchars);
10015 UNBLOCK_INPUT;
10016 #endif
10018 while (XPending (dpyinfo->display))
10020 XNextEvent (dpyinfo->display, &event);
10022 #ifdef HAVE_X_I18N
10024 /* Filter events for the current X input method.
10025 XFilterEvent returns non-zero if the input method has
10026 consumed the event. We pass the frame's X window to
10027 XFilterEvent because that's the one for which the IC
10028 was created. */
10029 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10030 event.xclient.window);
10031 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
10032 break;
10034 #endif
10035 event_found = 1;
10037 switch (event.type)
10039 case ClientMessage:
10041 if (event.xclient.message_type
10042 == dpyinfo->Xatom_wm_protocols
10043 && event.xclient.format == 32)
10045 if (event.xclient.data.l[0]
10046 == dpyinfo->Xatom_wm_take_focus)
10048 /* Use x_any_window_to_frame because this
10049 could be the shell widget window
10050 if the frame has no title bar. */
10051 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10052 #ifdef HAVE_X_I18N
10053 /* Not quite sure this is needed -pd */
10054 if (f && FRAME_XIC (f))
10055 XSetICFocus (FRAME_XIC (f));
10056 #endif
10057 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10058 instructs the WM to set the input focus automatically for
10059 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10060 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10061 it has set the focus. So, XSetInputFocus below is not
10062 needed.
10064 The call to XSetInputFocus below has also caused trouble. In
10065 cases where the XSetInputFocus done by the WM and the one
10066 below are temporally close (on a fast machine), the call
10067 below can generate additional FocusIn events which confuse
10068 Emacs. */
10070 /* Since we set WM_TAKE_FOCUS, we must call
10071 XSetInputFocus explicitly. But not if f is null,
10072 since that might be an event for a deleted frame. */
10073 if (f)
10075 Display *d = event.xclient.display;
10076 /* Catch and ignore errors, in case window has been
10077 iconified by a window manager such as GWM. */
10078 int count = x_catch_errors (d);
10079 XSetInputFocus (d, event.xclient.window,
10080 /* The ICCCM says this is
10081 the only valid choice. */
10082 RevertToParent,
10083 event.xclient.data.l[1]);
10084 /* This is needed to detect the error
10085 if there is an error. */
10086 XSync (d, False);
10087 x_uncatch_errors (d, count);
10089 /* Not certain about handling scroll bars here */
10090 #endif /* 0 */
10092 else if (event.xclient.data.l[0]
10093 == dpyinfo->Xatom_wm_save_yourself)
10095 /* Save state modify the WM_COMMAND property to
10096 something which can reinstate us. This notifies
10097 the session manager, who's looking for such a
10098 PropertyNotify. Can restart processing when
10099 a keyboard or mouse event arrives. */
10100 /* If we have a session manager, don't set this.
10101 KDE will then start two Emacsen, one for the
10102 session manager and one for this. */
10103 if (numchars > 0
10104 #ifdef HAVE_X_SM
10105 && ! x_session_have_connection ()
10106 #endif
10109 f = x_top_window_to_frame (dpyinfo,
10110 event.xclient.window);
10111 /* This is just so we only give real data once
10112 for a single Emacs process. */
10113 if (f == SELECTED_FRAME ())
10114 XSetCommand (FRAME_X_DISPLAY (f),
10115 event.xclient.window,
10116 initial_argv, initial_argc);
10117 else if (f)
10118 XSetCommand (FRAME_X_DISPLAY (f),
10119 event.xclient.window,
10120 0, 0);
10123 else if (event.xclient.data.l[0]
10124 == dpyinfo->Xatom_wm_delete_window)
10126 struct frame *f
10127 = x_any_window_to_frame (dpyinfo,
10128 event.xclient.window);
10130 if (f)
10132 if (numchars == 0)
10133 abort ();
10135 bufp->kind = delete_window_event;
10136 XSETFRAME (bufp->frame_or_window, f);
10137 bufp->arg = Qnil;
10138 bufp++;
10140 count += 1;
10141 numchars -= 1;
10145 else if (event.xclient.message_type
10146 == dpyinfo->Xatom_wm_configure_denied)
10149 else if (event.xclient.message_type
10150 == dpyinfo->Xatom_wm_window_moved)
10152 int new_x, new_y;
10153 struct frame *f
10154 = x_window_to_frame (dpyinfo, event.xclient.window);
10156 new_x = event.xclient.data.s[0];
10157 new_y = event.xclient.data.s[1];
10159 if (f)
10161 f->output_data.x->left_pos = new_x;
10162 f->output_data.x->top_pos = new_y;
10165 #ifdef HACK_EDITRES
10166 else if (event.xclient.message_type
10167 == dpyinfo->Xatom_editres)
10169 struct frame *f
10170 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10171 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10172 &event, NULL);
10174 #endif /* HACK_EDITRES */
10175 else if ((event.xclient.message_type
10176 == dpyinfo->Xatom_DONE)
10177 || (event.xclient.message_type
10178 == dpyinfo->Xatom_PAGE))
10180 /* Ghostview job completed. Kill it. We could
10181 reply with "Next" if we received "Page", but we
10182 currently never do because we are interested in
10183 images, only, which should have 1 page. */
10184 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10185 struct frame *f
10186 = x_window_to_frame (dpyinfo, event.xclient.window);
10187 x_kill_gs_process (pixmap, f);
10188 expose_frame (f, 0, 0, 0, 0);
10190 #ifdef USE_TOOLKIT_SCROLL_BARS
10191 /* Scroll bar callbacks send a ClientMessage from which
10192 we construct an input_event. */
10193 else if (event.xclient.message_type
10194 == dpyinfo->Xatom_Scrollbar)
10196 x_scroll_bar_to_input_event (&event, bufp);
10197 ++bufp, ++count, --numchars;
10198 goto out;
10200 #endif /* USE_TOOLKIT_SCROLL_BARS */
10201 else
10202 goto OTHER;
10204 break;
10206 case SelectionNotify:
10207 #ifdef USE_X_TOOLKIT
10208 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10209 goto OTHER;
10210 #endif /* not USE_X_TOOLKIT */
10211 x_handle_selection_notify (&event.xselection);
10212 break;
10214 case SelectionClear: /* Someone has grabbed ownership. */
10215 #ifdef USE_X_TOOLKIT
10216 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10217 goto OTHER;
10218 #endif /* USE_X_TOOLKIT */
10220 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10222 if (numchars == 0)
10223 abort ();
10225 bufp->kind = selection_clear_event;
10226 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10227 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10228 SELECTION_EVENT_TIME (bufp) = eventp->time;
10229 bufp->frame_or_window = Qnil;
10230 bufp->arg = Qnil;
10231 bufp++;
10233 count += 1;
10234 numchars -= 1;
10236 break;
10238 case SelectionRequest: /* Someone wants our selection. */
10239 #ifdef USE_X_TOOLKIT
10240 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10241 goto OTHER;
10242 #endif /* USE_X_TOOLKIT */
10243 if (x_queue_selection_requests)
10244 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10245 &event);
10246 else
10248 XSelectionRequestEvent *eventp
10249 = (XSelectionRequestEvent *) &event;
10251 if (numchars == 0)
10252 abort ();
10254 bufp->kind = selection_request_event;
10255 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10256 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10257 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10258 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10259 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10260 SELECTION_EVENT_TIME (bufp) = eventp->time;
10261 bufp->frame_or_window = Qnil;
10262 bufp->arg = Qnil;
10263 bufp++;
10265 count += 1;
10266 numchars -= 1;
10268 break;
10270 case PropertyNotify:
10271 #if 0 /* This is plain wrong. In the case that we are waiting for a
10272 PropertyNotify used as an ACK in incremental selection
10273 transfer, the property will be on the receiver's window. */
10274 #if defined USE_X_TOOLKIT
10275 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10276 goto OTHER;
10277 #endif
10278 #endif
10279 x_handle_property_notify (&event.xproperty);
10280 goto OTHER;
10282 case ReparentNotify:
10283 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10284 if (f)
10286 int x, y;
10287 f->output_data.x->parent_desc = event.xreparent.parent;
10288 x_real_positions (f, &x, &y);
10289 f->output_data.x->left_pos = x;
10290 f->output_data.x->top_pos = y;
10292 break;
10294 case Expose:
10295 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10296 if (f)
10298 x_check_fullscreen (f);
10300 if (f->async_visible == 0)
10302 f->async_visible = 1;
10303 f->async_iconified = 0;
10304 f->output_data.x->has_been_visible = 1;
10305 SET_FRAME_GARBAGED (f);
10307 else
10308 expose_frame (x_window_to_frame (dpyinfo,
10309 event.xexpose.window),
10310 event.xexpose.x, event.xexpose.y,
10311 event.xexpose.width, event.xexpose.height);
10313 else
10315 #ifndef USE_TOOLKIT_SCROLL_BARS
10316 struct scroll_bar *bar;
10317 #endif
10318 #if defined USE_LUCID
10319 /* Submenus of the Lucid menu bar aren't widgets
10320 themselves, so there's no way to dispatch events
10321 to them. Recognize this case separately. */
10323 Widget widget
10324 = x_window_to_menu_bar (event.xexpose.window);
10325 if (widget)
10326 xlwmenu_redisplay (widget);
10328 #endif /* USE_LUCID */
10330 #ifdef USE_TOOLKIT_SCROLL_BARS
10331 /* Dispatch event to the widget. */
10332 goto OTHER;
10333 #else /* not USE_TOOLKIT_SCROLL_BARS */
10334 bar = x_window_to_scroll_bar (event.xexpose.window);
10336 if (bar)
10337 x_scroll_bar_expose (bar, &event);
10338 #ifdef USE_X_TOOLKIT
10339 else
10340 goto OTHER;
10341 #endif /* USE_X_TOOLKIT */
10342 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10344 break;
10346 case GraphicsExpose: /* This occurs when an XCopyArea's
10347 source area was obscured or not
10348 available. */
10349 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10350 if (f)
10352 expose_frame (f,
10353 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10354 event.xgraphicsexpose.width,
10355 event.xgraphicsexpose.height);
10357 #ifdef USE_X_TOOLKIT
10358 else
10359 goto OTHER;
10360 #endif /* USE_X_TOOLKIT */
10361 break;
10363 case NoExpose: /* This occurs when an XCopyArea's
10364 source area was completely
10365 available. */
10366 break;
10368 case UnmapNotify:
10369 /* Redo the mouse-highlight after the tooltip has gone. */
10370 if (event.xmap.window == tip_window)
10372 tip_window = 0;
10373 redo_mouse_highlight ();
10376 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10377 if (f) /* F may no longer exist if
10378 the frame was deleted. */
10380 /* While a frame is unmapped, display generation is
10381 disabled; you don't want to spend time updating a
10382 display that won't ever be seen. */
10383 f->async_visible = 0;
10384 /* We can't distinguish, from the event, whether the window
10385 has become iconified or invisible. So assume, if it
10386 was previously visible, than now it is iconified.
10387 But x_make_frame_invisible clears both
10388 the visible flag and the iconified flag;
10389 and that way, we know the window is not iconified now. */
10390 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10392 f->async_iconified = 1;
10394 bufp->kind = iconify_event;
10395 XSETFRAME (bufp->frame_or_window, f);
10396 bufp->arg = Qnil;
10397 bufp++;
10398 count++;
10399 numchars--;
10402 goto OTHER;
10404 case MapNotify:
10405 if (event.xmap.window == tip_window)
10406 /* The tooltip has been drawn already. Avoid
10407 the SET_FRAME_GARBAGED below. */
10408 goto OTHER;
10410 /* We use x_top_window_to_frame because map events can
10411 come for sub-windows and they don't mean that the
10412 frame is visible. */
10413 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10414 if (f)
10416 f->async_visible = 1;
10417 f->async_iconified = 0;
10418 f->output_data.x->has_been_visible = 1;
10420 /* wait_reading_process_input will notice this and update
10421 the frame's display structures. */
10422 SET_FRAME_GARBAGED (f);
10424 if (f->iconified)
10426 bufp->kind = deiconify_event;
10427 XSETFRAME (bufp->frame_or_window, f);
10428 bufp->arg = Qnil;
10429 bufp++;
10430 count++;
10431 numchars--;
10433 else if (! NILP (Vframe_list)
10434 && ! NILP (XCDR (Vframe_list)))
10435 /* Force a redisplay sooner or later
10436 to update the frame titles
10437 in case this is the second frame. */
10438 record_asynch_buffer_change ();
10440 goto OTHER;
10442 case KeyPress:
10443 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10445 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10447 dpyinfo->mouse_face_hidden = 1;
10448 clear_mouse_face (dpyinfo);
10451 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10452 if (f == 0)
10454 /* Scroll bars consume key events, but we want
10455 the keys to go to the scroll bar's frame. */
10456 Widget widget = XtWindowToWidget (dpyinfo->display,
10457 event.xkey.window);
10458 if (widget && XmIsScrollBar (widget))
10460 widget = XtParent (widget);
10461 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10464 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10466 if (f != 0)
10468 KeySym keysym, orig_keysym;
10469 /* al%imercury@uunet.uu.net says that making this 81
10470 instead of 80 fixed a bug whereby meta chars made
10471 his Emacs hang.
10473 It seems that some version of XmbLookupString has
10474 a bug of not returning XBufferOverflow in
10475 status_return even if the input is too long to
10476 fit in 81 bytes. So, we must prepare sufficient
10477 bytes for copy_buffer. 513 bytes (256 chars for
10478 two-byte character set) seems to be a fairly good
10479 approximation. -- 2000.8.10 handa@etl.go.jp */
10480 unsigned char copy_buffer[513];
10481 unsigned char *copy_bufptr = copy_buffer;
10482 int copy_bufsiz = sizeof (copy_buffer);
10483 int modifiers;
10485 event.xkey.state
10486 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10487 extra_keyboard_modifiers);
10488 modifiers = event.xkey.state;
10490 /* This will have to go some day... */
10492 /* make_lispy_event turns chars into control chars.
10493 Don't do it here because XLookupString is too eager. */
10494 event.xkey.state &= ~ControlMask;
10495 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10496 | dpyinfo->super_mod_mask
10497 | dpyinfo->hyper_mod_mask
10498 | dpyinfo->alt_mod_mask);
10500 /* In case Meta is ComposeCharacter,
10501 clear its status. According to Markus Ehrnsperger
10502 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10503 this enables ComposeCharacter to work whether or
10504 not it is combined with Meta. */
10505 if (modifiers & dpyinfo->meta_mod_mask)
10506 bzero (&compose_status, sizeof (compose_status));
10508 #ifdef HAVE_X_I18N
10509 if (FRAME_XIC (f))
10511 Status status_return;
10513 nbytes = XmbLookupString (FRAME_XIC (f),
10514 &event.xkey, copy_bufptr,
10515 copy_bufsiz, &keysym,
10516 &status_return);
10517 if (status_return == XBufferOverflow)
10519 copy_bufsiz = nbytes + 1;
10520 copy_bufptr = (char *) alloca (copy_bufsiz);
10521 nbytes = XmbLookupString (FRAME_XIC (f),
10522 &event.xkey, copy_bufptr,
10523 copy_bufsiz, &keysym,
10524 &status_return);
10527 if (status_return == XLookupNone)
10528 break;
10529 else if (status_return == XLookupChars)
10531 keysym = NoSymbol;
10532 modifiers = 0;
10534 else if (status_return != XLookupKeySym
10535 && status_return != XLookupBoth)
10536 abort ();
10538 else
10539 nbytes = XLookupString (&event.xkey, copy_bufptr,
10540 copy_bufsiz, &keysym,
10541 &compose_status);
10542 #else
10543 nbytes = XLookupString (&event.xkey, copy_bufptr,
10544 copy_bufsiz, &keysym,
10545 &compose_status);
10546 #endif
10548 orig_keysym = keysym;
10550 if (numchars > 1)
10552 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10553 || keysym == XK_Delete
10554 #ifdef XK_ISO_Left_Tab
10555 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10556 #endif
10557 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10558 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10559 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10560 #ifdef HPUX
10561 /* This recognizes the "extended function keys".
10562 It seems there's no cleaner way.
10563 Test IsModifierKey to avoid handling mode_switch
10564 incorrectly. */
10565 || ((unsigned) (keysym) >= XK_Select
10566 && (unsigned)(keysym) < XK_KP_Space)
10567 #endif
10568 #ifdef XK_dead_circumflex
10569 || orig_keysym == XK_dead_circumflex
10570 #endif
10571 #ifdef XK_dead_grave
10572 || orig_keysym == XK_dead_grave
10573 #endif
10574 #ifdef XK_dead_tilde
10575 || orig_keysym == XK_dead_tilde
10576 #endif
10577 #ifdef XK_dead_diaeresis
10578 || orig_keysym == XK_dead_diaeresis
10579 #endif
10580 #ifdef XK_dead_macron
10581 || orig_keysym == XK_dead_macron
10582 #endif
10583 #ifdef XK_dead_degree
10584 || orig_keysym == XK_dead_degree
10585 #endif
10586 #ifdef XK_dead_acute
10587 || orig_keysym == XK_dead_acute
10588 #endif
10589 #ifdef XK_dead_cedilla
10590 || orig_keysym == XK_dead_cedilla
10591 #endif
10592 #ifdef XK_dead_breve
10593 || orig_keysym == XK_dead_breve
10594 #endif
10595 #ifdef XK_dead_ogonek
10596 || orig_keysym == XK_dead_ogonek
10597 #endif
10598 #ifdef XK_dead_caron
10599 || orig_keysym == XK_dead_caron
10600 #endif
10601 #ifdef XK_dead_doubleacute
10602 || orig_keysym == XK_dead_doubleacute
10603 #endif
10604 #ifdef XK_dead_abovedot
10605 || orig_keysym == XK_dead_abovedot
10606 #endif
10607 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10608 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10609 /* Any "vendor-specific" key is ok. */
10610 || (orig_keysym & (1 << 28))
10611 || (keysym != NoSymbol && nbytes == 0))
10612 && ! (IsModifierKey (orig_keysym)
10613 #ifndef HAVE_X11R5
10614 #ifdef XK_Mode_switch
10615 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10616 #endif
10617 #ifdef XK_Num_Lock
10618 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10619 #endif
10620 #endif /* not HAVE_X11R5 */
10621 /* The symbols from XK_ISO_Lock to
10622 XK_ISO_Last_Group_Lock doesn't have real
10623 modifiers but should be treated similarly
10624 to Mode_switch by Emacs. */
10625 #if defined XK_ISO_Lock && defined XK_ISO_Last_Group_Lock
10626 || ((unsigned)(orig_keysym) >= XK_ISO_Lock
10627 && (unsigned)(orig_keysym) <= XK_ISO_Last_Group_Lock)
10628 #endif
10631 if (temp_index == sizeof temp_buffer / sizeof (short))
10632 temp_index = 0;
10633 temp_buffer[temp_index++] = keysym;
10634 bufp->kind = non_ascii_keystroke;
10635 bufp->code = keysym;
10636 XSETFRAME (bufp->frame_or_window, f);
10637 bufp->arg = Qnil;
10638 bufp->modifiers
10639 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10640 modifiers);
10641 bufp->timestamp = event.xkey.time;
10642 bufp++;
10643 count++;
10644 numchars--;
10646 else if (numchars > nbytes)
10648 register int i;
10649 register int c;
10650 int nchars, len;
10652 for (i = 0; i < nbytes; i++)
10654 if (temp_index == (sizeof temp_buffer
10655 / sizeof (short)))
10656 temp_index = 0;
10657 temp_buffer[temp_index++] = copy_bufptr[i];
10660 if (/* If the event is not from XIM, */
10661 event.xkey.keycode != 0
10662 /* or the current locale doesn't request
10663 decoding of the intup data, ... */
10664 || coding.type == coding_type_raw_text
10665 || coding.type == coding_type_no_conversion)
10667 /* ... we can use the input data as is. */
10668 nchars = nbytes;
10670 else
10672 /* We have to decode the input data. */
10673 int require;
10674 unsigned char *p;
10676 require = decoding_buffer_size (&coding, nbytes);
10677 p = (unsigned char *) alloca (require);
10678 coding.mode |= CODING_MODE_LAST_BLOCK;
10679 decode_coding (&coding, copy_bufptr, p,
10680 nbytes, require);
10681 nbytes = coding.produced;
10682 nchars = coding.produced_char;
10683 copy_bufptr = p;
10686 /* Convert the input data to a sequence of
10687 character events. */
10688 for (i = 0; i < nbytes; i += len)
10690 if (nchars == nbytes)
10691 c = copy_bufptr[i], len = 1;
10692 else
10693 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10694 nbytes - i, len);
10696 bufp->kind = (SINGLE_BYTE_CHAR_P (c)
10697 ? ascii_keystroke
10698 : multibyte_char_keystroke);
10699 bufp->code = c;
10700 XSETFRAME (bufp->frame_or_window, f);
10701 bufp->arg = Qnil;
10702 bufp->modifiers
10703 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10704 modifiers);
10705 bufp->timestamp = event.xkey.time;
10706 bufp++;
10709 count += nchars;
10710 numchars -= nchars;
10712 if (keysym == NoSymbol)
10713 break;
10715 else
10716 abort ();
10718 else
10719 abort ();
10721 #ifdef HAVE_X_I18N
10722 /* Don't dispatch this event since XtDispatchEvent calls
10723 XFilterEvent, and two calls in a row may freeze the
10724 client. */
10725 break;
10726 #else
10727 goto OTHER;
10728 #endif
10730 case KeyRelease:
10731 #ifdef HAVE_X_I18N
10732 /* Don't dispatch this event since XtDispatchEvent calls
10733 XFilterEvent, and two calls in a row may freeze the
10734 client. */
10735 break;
10736 #else
10737 goto OTHER;
10738 #endif
10740 /* Here's a possible interpretation of the whole
10741 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10742 you get a FocusIn event, you have to get a FocusOut
10743 event before you relinquish the focus. If you
10744 haven't received a FocusIn event, then a mere
10745 LeaveNotify is enough to free you. */
10747 case EnterNotify:
10749 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10751 #if 0
10752 if (event.xcrossing.focus)
10754 /* Avoid nasty pop/raise loops. */
10755 if (f && (!(f->auto_raise)
10756 || !(f->auto_lower)
10757 || (event.xcrossing.time - enter_timestamp) > 500))
10759 x_new_focus_frame (dpyinfo, f);
10760 enter_timestamp = event.xcrossing.time;
10763 else if (f == dpyinfo->x_focus_frame)
10764 x_new_focus_frame (dpyinfo, 0);
10765 #endif
10767 /* EnterNotify counts as mouse movement,
10768 so update things that depend on mouse position. */
10769 if (f && !f->output_data.x->hourglass_p)
10770 note_mouse_movement (f, &event.xmotion);
10771 goto OTHER;
10774 case FocusIn:
10775 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10776 if (event.xfocus.detail != NotifyPointer)
10777 dpyinfo->x_focus_event_frame = f;
10778 if (f)
10780 x_new_focus_frame (dpyinfo, f);
10782 /* Don't stop displaying the initial startup message
10783 for a switch-frame event we don't need. */
10784 if (GC_NILP (Vterminal_frame)
10785 && GC_CONSP (Vframe_list)
10786 && !GC_NILP (XCDR (Vframe_list)))
10788 bufp->kind = FOCUS_IN_EVENT;
10789 XSETFRAME (bufp->frame_or_window, f);
10790 bufp->arg = Qnil;
10791 ++bufp, ++count, --numchars;
10795 #ifdef HAVE_X_I18N
10796 if (f && FRAME_XIC (f))
10797 XSetICFocus (FRAME_XIC (f));
10798 #endif
10800 goto OTHER;
10802 case LeaveNotify:
10803 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10804 if (f)
10806 if (f == dpyinfo->mouse_face_mouse_frame)
10808 /* If we move outside the frame, then we're
10809 certainly no longer on any text in the frame. */
10810 clear_mouse_face (dpyinfo);
10811 dpyinfo->mouse_face_mouse_frame = 0;
10814 /* Generate a nil HELP_EVENT to cancel a help-echo.
10815 Do it only if there's something to cancel.
10816 Otherwise, the startup message is cleared when
10817 the mouse leaves the frame. */
10818 if (any_help_event_p)
10820 Lisp_Object frame;
10821 int n;
10823 XSETFRAME (frame, f);
10824 help_echo = Qnil;
10825 n = gen_help_event (bufp, numchars,
10826 Qnil, frame, Qnil, Qnil, 0);
10827 bufp += n, count += n, numchars -= n;
10830 #if 0
10831 if (event.xcrossing.focus)
10832 x_mouse_leave (dpyinfo);
10833 else
10835 if (f == dpyinfo->x_focus_event_frame)
10836 dpyinfo->x_focus_event_frame = 0;
10837 if (f == dpyinfo->x_focus_frame)
10838 x_new_focus_frame (dpyinfo, 0);
10840 #endif
10842 goto OTHER;
10844 case FocusOut:
10845 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10846 if (event.xfocus.detail != NotifyPointer
10847 && f == dpyinfo->x_focus_event_frame)
10848 dpyinfo->x_focus_event_frame = 0;
10849 if (f && f == dpyinfo->x_focus_frame)
10850 x_new_focus_frame (dpyinfo, 0);
10852 #ifdef HAVE_X_I18N
10853 if (f && FRAME_XIC (f))
10854 XUnsetICFocus (FRAME_XIC (f));
10855 #endif
10857 goto OTHER;
10859 case MotionNotify:
10861 previous_help_echo = help_echo;
10862 help_echo = help_echo_object = help_echo_window = Qnil;
10863 help_echo_pos = -1;
10865 if (dpyinfo->grabbed && last_mouse_frame
10866 && FRAME_LIVE_P (last_mouse_frame))
10867 f = last_mouse_frame;
10868 else
10869 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10871 if (dpyinfo->mouse_face_hidden)
10873 dpyinfo->mouse_face_hidden = 0;
10874 clear_mouse_face (dpyinfo);
10877 if (f)
10880 /* Generate SELECT_WINDOW_EVENTs when needed. */
10881 if (autoselect_window_p)
10883 Lisp_Object window;
10884 int area;
10886 window = window_from_coordinates (f,
10887 XINT (event.xmotion.x), XINT (event.xmotion.y),
10888 &area, 0);
10890 /* Window will be selected only when it is not selected now and
10891 last mouse movement event was not in it. Minibuffer window
10892 will be selected iff it is active. */
10893 if (!EQ (window, last_window)
10894 && !EQ (window, selected_window)
10895 && numchars > 0)
10897 bufp->kind = SELECT_WINDOW_EVENT;
10898 bufp->frame_or_window = window;
10899 bufp->arg = Qnil;
10900 ++bufp, ++count, --numchars;
10903 last_window=window;
10905 note_mouse_movement (f, &event.xmotion);
10907 else
10909 #ifndef USE_TOOLKIT_SCROLL_BARS
10910 struct scroll_bar *bar
10911 = x_window_to_scroll_bar (event.xmotion.window);
10913 if (bar)
10914 x_scroll_bar_note_movement (bar, &event);
10915 #endif /* USE_TOOLKIT_SCROLL_BARS */
10917 /* If we move outside the frame, then we're
10918 certainly no longer on any text in the frame. */
10919 clear_mouse_face (dpyinfo);
10922 /* If the contents of the global variable help_echo
10923 has changed, generate a HELP_EVENT. */
10924 if (!NILP (help_echo)
10925 || !NILP (previous_help_echo))
10927 Lisp_Object frame;
10928 int n;
10930 if (f)
10931 XSETFRAME (frame, f);
10932 else
10933 frame = Qnil;
10935 any_help_event_p = 1;
10936 n = gen_help_event (bufp, numchars, help_echo, frame,
10937 help_echo_window, help_echo_object,
10938 help_echo_pos);
10939 bufp += n, count += n, numchars -= n;
10942 goto OTHER;
10945 case ConfigureNotify:
10946 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10947 if (f)
10949 #ifndef USE_X_TOOLKIT
10950 /* If there is a pending resize for fullscreen, don't
10951 do this one, the right one will come later.
10952 The toolkit version doesn't seem to need this, but we
10953 need to reset it below. */
10954 int dont_resize =
10955 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10956 && FRAME_NEW_WIDTH (f) != 0);
10957 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10958 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10959 if (dont_resize)
10960 goto OTHER;
10962 /* In the toolkit version, change_frame_size
10963 is called by the code that handles resizing
10964 of the EmacsFrame widget. */
10966 /* Even if the number of character rows and columns has
10967 not changed, the font size may have changed, so we need
10968 to check the pixel dimensions as well. */
10969 if (columns != f->width
10970 || rows != f->height
10971 || event.xconfigure.width != f->output_data.x->pixel_width
10972 || event.xconfigure.height != f->output_data.x->pixel_height)
10974 change_frame_size (f, rows, columns, 0, 1, 0);
10975 SET_FRAME_GARBAGED (f);
10976 cancel_mouse_face (f);
10978 #endif
10980 f->output_data.x->pixel_width = event.xconfigure.width;
10981 f->output_data.x->pixel_height = event.xconfigure.height;
10983 /* What we have now is the position of Emacs's own window.
10984 Convert that to the position of the window manager window. */
10985 x_real_positions (f, &f->output_data.x->left_pos,
10986 &f->output_data.x->top_pos);
10988 x_check_fullscreen_move(f);
10989 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10990 f->output_data.x->want_fullscreen &=
10991 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
10992 #ifdef HAVE_X_I18N
10993 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10994 xic_set_statusarea (f);
10995 #endif
10997 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10999 /* Since the WM decorations come below top_pos now,
11000 we must put them below top_pos in the future. */
11001 f->output_data.x->win_gravity = NorthWestGravity;
11002 x_wm_set_size_hint (f, (long) 0, 0);
11004 #ifdef USE_MOTIF
11005 /* Some window managers pass (0,0) as the location of
11006 the window, and the Motif event handler stores it
11007 in the emacs widget, which messes up Motif menus. */
11008 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
11010 event.xconfigure.x = f->output_data.x->widget->core.x;
11011 event.xconfigure.y = f->output_data.x->widget->core.y;
11013 #endif /* USE_MOTIF */
11015 goto OTHER;
11017 case ButtonPress:
11018 case ButtonRelease:
11020 /* If we decide we want to generate an event to be seen
11021 by the rest of Emacs, we put it here. */
11022 struct input_event emacs_event;
11023 int tool_bar_p = 0;
11025 emacs_event.kind = no_event;
11026 bzero (&compose_status, sizeof (compose_status));
11028 if (dpyinfo->grabbed
11029 && last_mouse_frame
11030 && FRAME_LIVE_P (last_mouse_frame))
11031 f = last_mouse_frame;
11032 else
11033 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11035 if (f)
11037 /* Is this in the tool-bar? */
11038 if (WINDOWP (f->tool_bar_window)
11039 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11041 Lisp_Object window;
11042 int p, x, y;
11044 x = event.xbutton.x;
11045 y = event.xbutton.y;
11047 /* Set x and y. */
11048 window = window_from_coordinates (f, x, y, &p, 1);
11049 if (EQ (window, f->tool_bar_window))
11051 x_handle_tool_bar_click (f, &event.xbutton);
11052 tool_bar_p = 1;
11056 if (!tool_bar_p)
11057 if (!dpyinfo->x_focus_frame
11058 || f == dpyinfo->x_focus_frame)
11059 construct_mouse_click (&emacs_event, &event, f);
11061 else
11063 #ifndef USE_TOOLKIT_SCROLL_BARS
11064 struct scroll_bar *bar
11065 = x_window_to_scroll_bar (event.xbutton.window);
11067 if (bar)
11068 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11069 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11072 if (event.type == ButtonPress)
11074 dpyinfo->grabbed |= (1 << event.xbutton.button);
11075 last_mouse_frame = f;
11076 /* Ignore any mouse motion that happened
11077 before this event; any subsequent mouse-movement
11078 Emacs events should reflect only motion after
11079 the ButtonPress. */
11080 if (f != 0)
11081 f->mouse_moved = 0;
11083 if (!tool_bar_p)
11084 last_tool_bar_item = -1;
11086 else
11088 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11091 if (numchars >= 1 && emacs_event.kind != no_event)
11093 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11094 bufp++;
11095 count++;
11096 numchars--;
11099 #ifdef USE_X_TOOLKIT
11100 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11101 /* For a down-event in the menu bar,
11102 don't pass it to Xt right now.
11103 Instead, save it away
11104 and we will pass it to Xt from kbd_buffer_get_event.
11105 That way, we can run some Lisp code first. */
11106 if (f && event.type == ButtonPress
11107 /* Verify the event is really within the menu bar
11108 and not just sent to it due to grabbing. */
11109 && event.xbutton.x >= 0
11110 && event.xbutton.x < f->output_data.x->pixel_width
11111 && event.xbutton.y >= 0
11112 && event.xbutton.y < f->output_data.x->menubar_height
11113 && event.xbutton.same_screen)
11115 SET_SAVED_BUTTON_EVENT;
11116 XSETFRAME (last_mouse_press_frame, f);
11118 else if (event.type == ButtonPress)
11120 last_mouse_press_frame = Qnil;
11121 goto OTHER;
11124 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11125 but I am trying to be cautious. */
11126 else if (event.type == ButtonRelease)
11128 if (!NILP (last_mouse_press_frame))
11130 f = XFRAME (last_mouse_press_frame);
11131 if (f->output_data.x)
11132 SET_SAVED_BUTTON_EVENT;
11134 else
11135 goto OTHER;
11137 #endif /* USE_MOTIF */
11138 else
11139 goto OTHER;
11140 #endif /* USE_X_TOOLKIT */
11142 break;
11144 case CirculateNotify:
11145 goto OTHER;
11147 case CirculateRequest:
11148 goto OTHER;
11150 case VisibilityNotify:
11151 goto OTHER;
11153 case MappingNotify:
11154 /* Someone has changed the keyboard mapping - update the
11155 local cache. */
11156 switch (event.xmapping.request)
11158 case MappingModifier:
11159 x_find_modifier_meanings (dpyinfo);
11160 /* This is meant to fall through. */
11161 case MappingKeyboard:
11162 XRefreshKeyboardMapping (&event.xmapping);
11164 goto OTHER;
11166 default:
11167 OTHER:
11168 #ifdef USE_X_TOOLKIT
11169 BLOCK_INPUT;
11170 XtDispatchEvent (&event);
11171 UNBLOCK_INPUT;
11172 #endif /* USE_X_TOOLKIT */
11173 break;
11178 out:;
11180 /* On some systems, an X bug causes Emacs to get no more events
11181 when the window is destroyed. Detect that. (1994.) */
11182 if (! event_found)
11184 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11185 One XNOOP in 100 loops will make Emacs terminate.
11186 B. Bretthauer, 1994 */
11187 x_noop_count++;
11188 if (x_noop_count >= 100)
11190 x_noop_count=0;
11192 if (next_noop_dpyinfo == 0)
11193 next_noop_dpyinfo = x_display_list;
11195 XNoOp (next_noop_dpyinfo->display);
11197 /* Each time we get here, cycle through the displays now open. */
11198 next_noop_dpyinfo = next_noop_dpyinfo->next;
11202 /* If the focus was just given to an auto-raising frame,
11203 raise it now. */
11204 /* ??? This ought to be able to handle more than one such frame. */
11205 if (pending_autoraise_frame)
11207 x_raise_frame (pending_autoraise_frame);
11208 pending_autoraise_frame = 0;
11211 UNBLOCK_INPUT;
11212 --handling_signal;
11213 return count;
11219 /***********************************************************************
11220 Text Cursor
11221 ***********************************************************************/
11223 /* Notice when the text cursor of window W has been completely
11224 overwritten by a drawing operation that outputs glyphs in AREA
11225 starting at X0 and ending at X1 in the line starting at Y0 and
11226 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11227 the rest of the line after X0 has been written. Y coordinates
11228 are window-relative. */
11230 static void
11231 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11232 struct window *w;
11233 enum glyph_row_area area;
11234 int x0, y0, x1, y1;
11236 if (area == TEXT_AREA
11237 && w->phys_cursor_on_p
11238 && y0 <= w->phys_cursor.y
11239 && y1 >= w->phys_cursor.y + w->phys_cursor_height
11240 && x0 <= w->phys_cursor.x
11241 && (x1 < 0 || x1 > w->phys_cursor.x))
11242 w->phys_cursor_on_p = 0;
11246 /* Set clipping for output in glyph row ROW. W is the window in which
11247 we operate. GC is the graphics context to set clipping in.
11248 WHOLE_LINE_P non-zero means include the areas used for truncation
11249 mark display and alike in the clipping rectangle.
11251 ROW may be a text row or, e.g., a mode line. Text rows must be
11252 clipped to the interior of the window dedicated to text display,
11253 mode lines must be clipped to the whole window. */
11255 static void
11256 x_clip_to_row (w, row, gc, whole_line_p)
11257 struct window *w;
11258 struct glyph_row *row;
11259 GC gc;
11260 int whole_line_p;
11262 struct frame *f = XFRAME (WINDOW_FRAME (w));
11263 XRectangle clip_rect;
11264 int window_x, window_y, window_width, window_height;
11266 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11268 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11269 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11270 clip_rect.y = max (clip_rect.y, window_y);
11271 clip_rect.width = window_width;
11272 clip_rect.height = row->visible_height;
11274 /* If clipping to the whole line, including trunc marks, extend
11275 the rectangle to the left and increase its width. */
11276 if (whole_line_p)
11278 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11279 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11282 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11286 /* Draw a hollow box cursor on window W in glyph row ROW. */
11288 static void
11289 x_draw_hollow_cursor (w, row)
11290 struct window *w;
11291 struct glyph_row *row;
11293 struct frame *f = XFRAME (WINDOW_FRAME (w));
11294 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11295 Display *dpy = FRAME_X_DISPLAY (f);
11296 int x, y, wd, h;
11297 XGCValues xgcv;
11298 struct glyph *cursor_glyph;
11299 GC gc;
11301 /* Compute frame-relative coordinates from window-relative
11302 coordinates. */
11303 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11304 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11305 + row->ascent - w->phys_cursor_ascent);
11306 h = row->height - 1;
11308 /* Get the glyph the cursor is on. If we can't tell because
11309 the current matrix is invalid or such, give up. */
11310 cursor_glyph = get_phys_cursor_glyph (w);
11311 if (cursor_glyph == NULL)
11312 return;
11314 /* Compute the width of the rectangle to draw. If on a stretch
11315 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11316 rectangle as wide as the glyph, but use a canonical character
11317 width instead. */
11318 wd = cursor_glyph->pixel_width - 1;
11319 if (cursor_glyph->type == STRETCH_GLYPH
11320 && !x_stretch_cursor_p)
11321 wd = min (CANON_X_UNIT (f), wd);
11323 /* The foreground of cursor_gc is typically the same as the normal
11324 background color, which can cause the cursor box to be invisible. */
11325 xgcv.foreground = f->output_data.x->cursor_pixel;
11326 if (dpyinfo->scratch_cursor_gc)
11327 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11328 else
11329 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11330 GCForeground, &xgcv);
11331 gc = dpyinfo->scratch_cursor_gc;
11333 /* Set clipping, draw the rectangle, and reset clipping again. */
11334 x_clip_to_row (w, row, gc, 0);
11335 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11336 XSetClipMask (dpy, gc, None);
11340 /* Draw a bar cursor on window W in glyph row ROW.
11342 Implementation note: One would like to draw a bar cursor with an
11343 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11344 Unfortunately, I didn't find a font yet that has this property set.
11345 --gerd. */
11347 static void
11348 x_draw_bar_cursor (w, row, width)
11349 struct window *w;
11350 struct glyph_row *row;
11351 int width;
11353 struct frame *f = XFRAME (w->frame);
11354 struct glyph *cursor_glyph;
11356 /* If cursor is out of bounds, don't draw garbage. This can happen
11357 in mini-buffer windows when switching between echo area glyphs
11358 and mini-buffer. */
11359 cursor_glyph = get_phys_cursor_glyph (w);
11360 if (cursor_glyph == NULL)
11361 return;
11363 /* If on an image, draw like a normal cursor. That's usually better
11364 visible than drawing a bar, esp. if the image is large so that
11365 the bar might not be in the window. */
11366 if (cursor_glyph->type == IMAGE_GLYPH)
11368 struct glyph_row *row;
11369 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11370 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11372 else
11374 Display *dpy = FRAME_X_DISPLAY (f);
11375 Window window = FRAME_X_WINDOW (f);
11376 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11377 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11378 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11379 XGCValues xgcv;
11381 /* If the glyph's background equals the color we normally draw
11382 the bar cursor in, the bar cursor in its normal color is
11383 invisible. Use the glyph's foreground color instead in this
11384 case, on the assumption that the glyph's colors are chosen so
11385 that the glyph is legible. */
11386 if (face->background == f->output_data.x->cursor_pixel)
11387 xgcv.background = xgcv.foreground = face->foreground;
11388 else
11389 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11390 xgcv.graphics_exposures = 0;
11392 if (gc)
11393 XChangeGC (dpy, gc, mask, &xgcv);
11394 else
11396 gc = XCreateGC (dpy, window, mask, &xgcv);
11397 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11400 if (width < 0)
11401 width = f->output_data.x->cursor_width;
11402 width = min (cursor_glyph->pixel_width, width);
11404 x_clip_to_row (w, row, gc, 0);
11405 XFillRectangle (dpy, window, gc,
11406 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11407 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11408 width, row->height);
11409 XSetClipMask (dpy, gc, None);
11414 /* Clear the cursor of window W to background color, and mark the
11415 cursor as not shown. This is used when the text where the cursor
11416 is is about to be rewritten. */
11418 static void
11419 x_clear_cursor (w)
11420 struct window *w;
11422 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11423 x_update_window_cursor (w, 0);
11427 /* Draw the cursor glyph of window W in glyph row ROW. See the
11428 comment of x_draw_glyphs for the meaning of HL. */
11430 static void
11431 x_draw_phys_cursor_glyph (w, row, hl)
11432 struct window *w;
11433 struct glyph_row *row;
11434 enum draw_glyphs_face hl;
11436 /* If cursor hpos is out of bounds, don't draw garbage. This can
11437 happen in mini-buffer windows when switching between echo area
11438 glyphs and mini-buffer. */
11439 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11441 int on_p = w->phys_cursor_on_p;
11443 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11444 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11445 hl, 0);
11446 w->phys_cursor_on_p = on_p;
11448 /* When we erase the cursor, and ROW is overlapped by other
11449 rows, make sure that these overlapping parts of other rows
11450 are redrawn. */
11451 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11453 if (row > w->current_matrix->rows
11454 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11455 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11457 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11458 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11459 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11465 /* Erase the image of a cursor of window W from the screen. */
11467 static void
11468 x_erase_phys_cursor (w)
11469 struct window *w;
11471 struct frame *f = XFRAME (w->frame);
11472 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11473 int hpos = w->phys_cursor.hpos;
11474 int vpos = w->phys_cursor.vpos;
11475 int mouse_face_here_p = 0;
11476 struct glyph_matrix *active_glyphs = w->current_matrix;
11477 struct glyph_row *cursor_row;
11478 struct glyph *cursor_glyph;
11479 enum draw_glyphs_face hl;
11481 /* No cursor displayed or row invalidated => nothing to do on the
11482 screen. */
11483 if (w->phys_cursor_type == NO_CURSOR)
11484 goto mark_cursor_off;
11486 /* VPOS >= active_glyphs->nrows means that window has been resized.
11487 Don't bother to erase the cursor. */
11488 if (vpos >= active_glyphs->nrows)
11489 goto mark_cursor_off;
11491 /* If row containing cursor is marked invalid, there is nothing we
11492 can do. */
11493 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11494 if (!cursor_row->enabled_p)
11495 goto mark_cursor_off;
11497 /* If row is completely invisible, don't attempt to delete a cursor which
11498 isn't there. This can happen if cursor is at top of a window, and
11499 we switch to a buffer with a header line in that window. */
11500 if (cursor_row->visible_height <= 0)
11501 goto mark_cursor_off;
11503 /* This can happen when the new row is shorter than the old one.
11504 In this case, either x_draw_glyphs or clear_end_of_line
11505 should have cleared the cursor. Note that we wouldn't be
11506 able to erase the cursor in this case because we don't have a
11507 cursor glyph at hand. */
11508 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11509 goto mark_cursor_off;
11511 /* If the cursor is in the mouse face area, redisplay that when
11512 we clear the cursor. */
11513 if (! NILP (dpyinfo->mouse_face_window)
11514 && w == XWINDOW (dpyinfo->mouse_face_window)
11515 && (vpos > dpyinfo->mouse_face_beg_row
11516 || (vpos == dpyinfo->mouse_face_beg_row
11517 && hpos >= dpyinfo->mouse_face_beg_col))
11518 && (vpos < dpyinfo->mouse_face_end_row
11519 || (vpos == dpyinfo->mouse_face_end_row
11520 && hpos < dpyinfo->mouse_face_end_col))
11521 /* Don't redraw the cursor's spot in mouse face if it is at the
11522 end of a line (on a newline). The cursor appears there, but
11523 mouse highlighting does not. */
11524 && cursor_row->used[TEXT_AREA] > hpos)
11525 mouse_face_here_p = 1;
11527 /* Maybe clear the display under the cursor. */
11528 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11530 int x;
11531 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11533 cursor_glyph = get_phys_cursor_glyph (w);
11534 if (cursor_glyph == NULL)
11535 goto mark_cursor_off;
11537 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11539 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11541 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11542 cursor_row->y)),
11543 cursor_glyph->pixel_width,
11544 cursor_row->visible_height,
11545 False);
11548 /* Erase the cursor by redrawing the character underneath it. */
11549 if (mouse_face_here_p)
11550 hl = DRAW_MOUSE_FACE;
11551 else
11552 hl = DRAW_NORMAL_TEXT;
11553 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11555 mark_cursor_off:
11556 w->phys_cursor_on_p = 0;
11557 w->phys_cursor_type = NO_CURSOR;
11561 /* Non-zero if physical cursor of window W is within mouse face. */
11563 static int
11564 cursor_in_mouse_face_p (w)
11565 struct window *w;
11567 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11568 int in_mouse_face = 0;
11570 if (WINDOWP (dpyinfo->mouse_face_window)
11571 && XWINDOW (dpyinfo->mouse_face_window) == w)
11573 int hpos = w->phys_cursor.hpos;
11574 int vpos = w->phys_cursor.vpos;
11576 if (vpos >= dpyinfo->mouse_face_beg_row
11577 && vpos <= dpyinfo->mouse_face_end_row
11578 && (vpos > dpyinfo->mouse_face_beg_row
11579 || hpos >= dpyinfo->mouse_face_beg_col)
11580 && (vpos < dpyinfo->mouse_face_end_row
11581 || hpos < dpyinfo->mouse_face_end_col
11582 || dpyinfo->mouse_face_past_end))
11583 in_mouse_face = 1;
11586 return in_mouse_face;
11590 /* Display or clear cursor of window W. If ON is zero, clear the
11591 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11592 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11594 void
11595 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11596 struct window *w;
11597 int on, hpos, vpos, x, y;
11599 struct frame *f = XFRAME (w->frame);
11600 int new_cursor_type;
11601 int new_cursor_width;
11602 struct glyph_matrix *current_glyphs;
11603 struct glyph_row *glyph_row;
11604 struct glyph *glyph;
11605 int cursor_non_selected;
11607 /* This is pointless on invisible frames, and dangerous on garbaged
11608 windows and frames; in the latter case, the frame or window may
11609 be in the midst of changing its size, and x and y may be off the
11610 window. */
11611 if (! FRAME_VISIBLE_P (f)
11612 || FRAME_GARBAGED_P (f)
11613 || vpos >= w->current_matrix->nrows
11614 || hpos >= w->current_matrix->matrix_w)
11615 return;
11617 /* If cursor is off and we want it off, return quickly. */
11618 if (!on && !w->phys_cursor_on_p)
11619 return;
11621 current_glyphs = w->current_matrix;
11622 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11623 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11625 /* If cursor row is not enabled, we don't really know where to
11626 display the cursor. */
11627 if (!glyph_row->enabled_p)
11629 w->phys_cursor_on_p = 0;
11630 return;
11633 xassert (interrupt_input_blocked);
11635 /* Set new_cursor_type to the cursor we want to be displayed. In a
11636 mini-buffer window, we want the cursor only to appear if we are
11637 reading input from this window. For the selected window, we want
11638 the cursor type given by the frame parameter. If explicitly
11639 marked off, draw no cursor. In all other cases, we want a hollow
11640 box cursor. */
11641 cursor_non_selected
11642 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11643 w->buffer));
11644 new_cursor_width = -1;
11645 if (cursor_in_echo_area
11646 && FRAME_HAS_MINIBUF_P (f)
11647 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11649 if (w == XWINDOW (echo_area_window))
11650 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11651 else if (cursor_non_selected)
11652 new_cursor_type = HOLLOW_BOX_CURSOR;
11653 else
11654 new_cursor_type = NO_CURSOR;
11656 else
11658 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11659 || w != XWINDOW (f->selected_window))
11661 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11662 || !cursor_non_selected
11663 || NILP (XBUFFER (w->buffer)->cursor_type))
11664 new_cursor_type = NO_CURSOR;
11665 else
11666 new_cursor_type = HOLLOW_BOX_CURSOR;
11668 else
11670 struct buffer *b = XBUFFER (w->buffer);
11672 if (EQ (b->cursor_type, Qt))
11673 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11674 else
11675 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11676 &new_cursor_width);
11677 if (w->cursor_off_p)
11679 if (new_cursor_type == FILLED_BOX_CURSOR)
11680 new_cursor_type = HOLLOW_BOX_CURSOR;
11681 else if (new_cursor_type == BAR_CURSOR && new_cursor_width > 1)
11682 new_cursor_width = 1;
11683 else
11684 new_cursor_type = NO_CURSOR;
11689 /* If cursor is currently being shown and we don't want it to be or
11690 it is in the wrong place, or the cursor type is not what we want,
11691 erase it. */
11692 if (w->phys_cursor_on_p
11693 && (!on
11694 || w->phys_cursor.x != x
11695 || w->phys_cursor.y != y
11696 || new_cursor_type != w->phys_cursor_type
11697 || (new_cursor_type == BAR_CURSOR
11698 && new_cursor_width != w->phys_cursor_width)))
11699 x_erase_phys_cursor (w);
11701 /* If the cursor is now invisible and we want it to be visible,
11702 display it. */
11703 if (on && !w->phys_cursor_on_p)
11705 w->phys_cursor_ascent = glyph_row->ascent;
11706 w->phys_cursor_height = glyph_row->height;
11708 /* Set phys_cursor_.* before x_draw_.* is called because some
11709 of them may need the information. */
11710 w->phys_cursor.x = x;
11711 w->phys_cursor.y = glyph_row->y;
11712 w->phys_cursor.hpos = hpos;
11713 w->phys_cursor.vpos = vpos;
11714 w->phys_cursor_type = new_cursor_type;
11715 w->phys_cursor_width = new_cursor_width;
11716 w->phys_cursor_on_p = 1;
11718 switch (new_cursor_type)
11720 case HOLLOW_BOX_CURSOR:
11721 x_draw_hollow_cursor (w, glyph_row);
11722 break;
11724 case FILLED_BOX_CURSOR:
11725 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11726 break;
11728 case BAR_CURSOR:
11729 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11730 break;
11732 case NO_CURSOR:
11733 break;
11735 default:
11736 abort ();
11739 #ifdef HAVE_X_I18N
11740 if (w == XWINDOW (f->selected_window))
11741 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11742 xic_set_preeditarea (w, x, y);
11743 #endif
11746 #ifndef XFlush
11747 if (updating_frame != f)
11748 XFlush (FRAME_X_DISPLAY (f));
11749 #endif
11753 /* Display the cursor on window W, or clear it. X and Y are window
11754 relative pixel coordinates. HPOS and VPOS are glyph matrix
11755 positions. If W is not the selected window, display a hollow
11756 cursor. ON non-zero means display the cursor at X, Y which
11757 correspond to HPOS, VPOS, otherwise it is cleared. */
11759 void
11760 x_display_cursor (w, on, hpos, vpos, x, y)
11761 struct window *w;
11762 int on, hpos, vpos, x, y;
11764 BLOCK_INPUT;
11765 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11766 UNBLOCK_INPUT;
11770 /* Display the cursor on window W, or clear it, according to ON_P.
11771 Don't change the cursor's position. */
11773 void
11774 x_update_cursor (f, on_p)
11775 struct frame *f;
11777 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11781 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11782 in the window tree rooted at W. */
11784 static void
11785 x_update_cursor_in_window_tree (w, on_p)
11786 struct window *w;
11787 int on_p;
11789 while (w)
11791 if (!NILP (w->hchild))
11792 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11793 else if (!NILP (w->vchild))
11794 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11795 else
11796 x_update_window_cursor (w, on_p);
11798 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11803 /* Switch the display of W's cursor on or off, according to the value
11804 of ON. */
11806 static void
11807 x_update_window_cursor (w, on)
11808 struct window *w;
11809 int on;
11811 /* Don't update cursor in windows whose frame is in the process
11812 of being deleted. */
11813 if (w->current_matrix)
11815 BLOCK_INPUT;
11816 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11817 w->phys_cursor.x, w->phys_cursor.y);
11818 UNBLOCK_INPUT;
11825 /* Icons. */
11827 /* Make the x-window of frame F use the gnu icon bitmap. */
11830 x_bitmap_icon (f, file)
11831 struct frame *f;
11832 Lisp_Object file;
11834 int bitmap_id;
11836 if (FRAME_X_WINDOW (f) == 0)
11837 return 1;
11839 /* Free up our existing icon bitmap if any. */
11840 if (f->output_data.x->icon_bitmap > 0)
11841 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11842 f->output_data.x->icon_bitmap = 0;
11844 if (STRINGP (file))
11845 bitmap_id = x_create_bitmap_from_file (f, file);
11846 else
11848 /* Create the GNU bitmap if necessary. */
11849 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11850 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11851 = x_create_bitmap_from_data (f, gnu_bits,
11852 gnu_width, gnu_height);
11854 /* The first time we create the GNU bitmap,
11855 this increments the ref-count one extra time.
11856 As a result, the GNU bitmap is never freed.
11857 That way, we don't have to worry about allocating it again. */
11858 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11860 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11863 x_wm_set_icon_pixmap (f, bitmap_id);
11864 f->output_data.x->icon_bitmap = bitmap_id;
11866 return 0;
11870 /* Make the x-window of frame F use a rectangle with text.
11871 Use ICON_NAME as the text. */
11874 x_text_icon (f, icon_name)
11875 struct frame *f;
11876 char *icon_name;
11878 if (FRAME_X_WINDOW (f) == 0)
11879 return 1;
11881 #ifdef HAVE_X11R4
11883 XTextProperty text;
11884 text.value = (unsigned char *) icon_name;
11885 text.encoding = XA_STRING;
11886 text.format = 8;
11887 text.nitems = strlen (icon_name);
11888 #ifdef USE_X_TOOLKIT
11889 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11890 &text);
11891 #else /* not USE_X_TOOLKIT */
11892 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11893 #endif /* not USE_X_TOOLKIT */
11895 #else /* not HAVE_X11R4 */
11896 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11897 #endif /* not HAVE_X11R4 */
11899 if (f->output_data.x->icon_bitmap > 0)
11900 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11901 f->output_data.x->icon_bitmap = 0;
11902 x_wm_set_icon_pixmap (f, 0);
11904 return 0;
11907 #define X_ERROR_MESSAGE_SIZE 200
11909 /* If non-nil, this should be a string.
11910 It means catch X errors and store the error message in this string. */
11912 static Lisp_Object x_error_message_string;
11914 /* An X error handler which stores the error message in
11915 x_error_message_string. This is called from x_error_handler if
11916 x_catch_errors is in effect. */
11918 static void
11919 x_error_catcher (display, error)
11920 Display *display;
11921 XErrorEvent *error;
11923 XGetErrorText (display, error->error_code,
11924 XSTRING (x_error_message_string)->data,
11925 X_ERROR_MESSAGE_SIZE);
11928 /* Begin trapping X errors for display DPY. Actually we trap X errors
11929 for all displays, but DPY should be the display you are actually
11930 operating on.
11932 After calling this function, X protocol errors no longer cause
11933 Emacs to exit; instead, they are recorded in the string
11934 stored in x_error_message_string.
11936 Calling x_check_errors signals an Emacs error if an X error has
11937 occurred since the last call to x_catch_errors or x_check_errors.
11939 Calling x_uncatch_errors resumes the normal error handling. */
11941 void x_check_errors ();
11942 static Lisp_Object x_catch_errors_unwind ();
11945 x_catch_errors (dpy)
11946 Display *dpy;
11948 int count = specpdl_ptr - specpdl;
11950 /* Make sure any errors from previous requests have been dealt with. */
11951 XSync (dpy, False);
11953 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11955 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11956 XSTRING (x_error_message_string)->data[0] = 0;
11958 return count;
11961 /* Unbind the binding that we made to check for X errors. */
11963 static Lisp_Object
11964 x_catch_errors_unwind (old_val)
11965 Lisp_Object old_val;
11967 x_error_message_string = old_val;
11968 return Qnil;
11971 /* If any X protocol errors have arrived since the last call to
11972 x_catch_errors or x_check_errors, signal an Emacs error using
11973 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11975 void
11976 x_check_errors (dpy, format)
11977 Display *dpy;
11978 char *format;
11980 /* Make sure to catch any errors incurred so far. */
11981 XSync (dpy, False);
11983 if (XSTRING (x_error_message_string)->data[0])
11984 error (format, XSTRING (x_error_message_string)->data);
11987 /* Nonzero if we had any X protocol errors
11988 since we did x_catch_errors on DPY. */
11991 x_had_errors_p (dpy)
11992 Display *dpy;
11994 /* Make sure to catch any errors incurred so far. */
11995 XSync (dpy, False);
11997 return XSTRING (x_error_message_string)->data[0] != 0;
12000 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
12002 void
12003 x_clear_errors (dpy)
12004 Display *dpy;
12006 XSTRING (x_error_message_string)->data[0] = 0;
12009 /* Stop catching X protocol errors and let them make Emacs die.
12010 DPY should be the display that was passed to x_catch_errors.
12011 COUNT should be the value that was returned by
12012 the corresponding call to x_catch_errors. */
12014 void
12015 x_uncatch_errors (dpy, count)
12016 Display *dpy;
12017 int count;
12019 unbind_to (count, Qnil);
12022 #if 0
12023 static unsigned int x_wire_count;
12024 x_trace_wire ()
12026 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12028 #endif /* ! 0 */
12031 /* Handle SIGPIPE, which can happen when the connection to a server
12032 simply goes away. SIGPIPE is handled by x_connection_signal.
12033 Don't need to do anything, because the write which caused the
12034 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12035 which will do the appropriate cleanup for us. */
12037 static SIGTYPE
12038 x_connection_signal (signalnum) /* If we don't have an argument, */
12039 int signalnum; /* some compilers complain in signal calls. */
12041 #ifdef USG
12042 /* USG systems forget handlers when they are used;
12043 must reestablish each time */
12044 signal (signalnum, x_connection_signal);
12045 #endif /* USG */
12049 /************************************************************************
12050 Handling X errors
12051 ************************************************************************/
12053 /* Error message passed to x_connection_closed. */
12055 static char *error_msg;
12057 /* Function installed as fatal_error_signal_hook in
12058 x_connection_closed. Print the X error message, and exit normally,
12059 instead of dumping core when XtCloseDisplay fails. */
12061 static void
12062 x_fatal_error_signal ()
12064 fprintf (stderr, "%s\n", error_msg);
12065 exit (70);
12068 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12069 the text of an error message that lead to the connection loss. */
12071 static SIGTYPE
12072 x_connection_closed (dpy, error_message)
12073 Display *dpy;
12074 char *error_message;
12076 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12077 Lisp_Object frame, tail;
12078 int count;
12080 error_msg = (char *) alloca (strlen (error_message) + 1);
12081 strcpy (error_msg, error_message);
12082 handling_signal = 0;
12084 /* Prevent being called recursively because of an error condition
12085 below. Otherwise, we might end up with printing ``can't find per
12086 display information'' in the recursive call instead of printing
12087 the original message here. */
12088 count = x_catch_errors (dpy);
12090 /* We have to close the display to inform Xt that it doesn't
12091 exist anymore. If we don't, Xt will continue to wait for
12092 events from the display. As a consequence, a sequence of
12094 M-x make-frame-on-display RET :1 RET
12095 ...kill the new frame, so that we get an IO error...
12096 M-x make-frame-on-display RET :1 RET
12098 will indefinitely wait in Xt for events for display `:1', opened
12099 in the first class to make-frame-on-display.
12101 Closing the display is reported to lead to a bus error on
12102 OpenWindows in certain situations. I suspect that is a bug
12103 in OpenWindows. I don't know how to cicumvent it here. */
12105 #ifdef USE_X_TOOLKIT
12106 /* If DPYINFO is null, this means we didn't open the display
12107 in the first place, so don't try to close it. */
12108 if (dpyinfo)
12110 extern void (*fatal_error_signal_hook) P_ ((void));
12111 fatal_error_signal_hook = x_fatal_error_signal;
12112 XtCloseDisplay (dpy);
12113 fatal_error_signal_hook = NULL;
12115 #endif
12117 /* Indicate that this display is dead. */
12118 if (dpyinfo)
12119 dpyinfo->display = 0;
12121 /* First delete frames whose mini-buffers are on frames
12122 that are on the dead display. */
12123 FOR_EACH_FRAME (tail, frame)
12125 Lisp_Object minibuf_frame;
12126 minibuf_frame
12127 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12128 if (FRAME_X_P (XFRAME (frame))
12129 && FRAME_X_P (XFRAME (minibuf_frame))
12130 && ! EQ (frame, minibuf_frame)
12131 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12132 Fdelete_frame (frame, Qt);
12135 /* Now delete all remaining frames on the dead display.
12136 We are now sure none of these is used as the mini-buffer
12137 for another frame that we need to delete. */
12138 FOR_EACH_FRAME (tail, frame)
12139 if (FRAME_X_P (XFRAME (frame))
12140 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12142 /* Set this to t so that Fdelete_frame won't get confused
12143 trying to find a replacement. */
12144 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12145 Fdelete_frame (frame, Qt);
12148 if (dpyinfo)
12149 x_delete_display (dpyinfo);
12151 x_uncatch_errors (dpy, count);
12153 if (x_display_list == 0)
12155 fprintf (stderr, "%s\n", error_msg);
12156 shut_down_emacs (0, 0, Qnil);
12157 exit (70);
12160 /* Ordinary stack unwind doesn't deal with these. */
12161 #ifdef SIGIO
12162 sigunblock (sigmask (SIGIO));
12163 #endif
12164 sigunblock (sigmask (SIGALRM));
12165 TOTALLY_UNBLOCK_INPUT;
12167 clear_waiting_for_input ();
12168 error ("%s", error_msg);
12172 /* This is the usual handler for X protocol errors.
12173 It kills all frames on the display that we got the error for.
12174 If that was the only one, it prints an error message and kills Emacs. */
12176 static void
12177 x_error_quitter (display, error)
12178 Display *display;
12179 XErrorEvent *error;
12181 char buf[256], buf1[356];
12183 /* Note that there is no real way portable across R3/R4 to get the
12184 original error handler. */
12186 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12187 sprintf (buf1, "X protocol error: %s on protocol request %d",
12188 buf, error->request_code);
12189 x_connection_closed (display, buf1);
12193 /* This is the first-level handler for X protocol errors.
12194 It calls x_error_quitter or x_error_catcher. */
12196 static int
12197 x_error_handler (display, error)
12198 Display *display;
12199 XErrorEvent *error;
12201 if (! NILP (x_error_message_string))
12202 x_error_catcher (display, error);
12203 else
12204 x_error_quitter (display, error);
12205 return 0;
12208 /* This is the handler for X IO errors, always.
12209 It kills all frames on the display that we lost touch with.
12210 If that was the only one, it prints an error message and kills Emacs. */
12212 static int
12213 x_io_error_quitter (display)
12214 Display *display;
12216 char buf[256];
12218 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12219 x_connection_closed (display, buf);
12220 return 0;
12223 /* Changing the font of the frame. */
12225 /* Give frame F the font named FONTNAME as its default font, and
12226 return the full name of that font. FONTNAME may be a wildcard
12227 pattern; in that case, we choose some font that fits the pattern.
12228 The return value shows which font we chose. */
12230 Lisp_Object
12231 x_new_font (f, fontname)
12232 struct frame *f;
12233 register char *fontname;
12235 struct font_info *fontp
12236 = FS_LOAD_FONT (f, 0, fontname, -1);
12238 if (!fontp)
12239 return Qnil;
12241 f->output_data.x->font = (XFontStruct *) (fontp->font);
12242 f->output_data.x->baseline_offset = fontp->baseline_offset;
12243 f->output_data.x->fontset = -1;
12245 x_compute_fringe_widths (f, 1);
12247 /* Compute the scroll bar width in character columns. */
12248 if (f->scroll_bar_pixel_width > 0)
12250 int wid = FONT_WIDTH (f->output_data.x->font);
12251 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12253 else
12255 int wid = FONT_WIDTH (f->output_data.x->font);
12256 f->scroll_bar_cols = (14 + wid - 1) / wid;
12259 /* Now make the frame display the given font. */
12260 if (FRAME_X_WINDOW (f) != 0)
12262 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12263 f->output_data.x->font->fid);
12264 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12265 f->output_data.x->font->fid);
12266 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12267 f->output_data.x->font->fid);
12269 frame_update_line_height (f);
12271 /* Don't change the size of a tip frame; there's no point in
12272 doing it because it's done in Fx_show_tip, and it leads to
12273 problems because the tip frame has no widget. */
12274 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12275 x_set_window_size (f, 0, f->width, f->height);
12277 else
12278 /* If we are setting a new frame's font for the first time,
12279 there are no faces yet, so this font's height is the line height. */
12280 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12282 return build_string (fontp->full_name);
12285 /* Give frame F the fontset named FONTSETNAME as its default font, and
12286 return the full name of that fontset. FONTSETNAME may be a wildcard
12287 pattern; in that case, we choose some fontset that fits the pattern.
12288 The return value shows which fontset we chose. */
12290 Lisp_Object
12291 x_new_fontset (f, fontsetname)
12292 struct frame *f;
12293 char *fontsetname;
12295 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12296 Lisp_Object result;
12298 if (fontset < 0)
12299 return Qnil;
12301 if (f->output_data.x->fontset == fontset)
12302 /* This fontset is already set in frame F. There's nothing more
12303 to do. */
12304 return fontset_name (fontset);
12306 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12308 if (!STRINGP (result))
12309 /* Can't load ASCII font. */
12310 return Qnil;
12312 /* Since x_new_font doesn't update any fontset information, do it now. */
12313 f->output_data.x->fontset = fontset;
12315 #ifdef HAVE_X_I18N
12316 if (FRAME_XIC (f)
12317 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12318 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12319 #endif
12321 return build_string (fontsetname);
12324 /* Compute actual fringe widths */
12326 void
12327 x_compute_fringe_widths (f, redraw)
12328 struct frame *f;
12329 int redraw;
12331 int o_left = f->output_data.x->left_fringe_width;
12332 int o_right = f->output_data.x->right_fringe_width;
12333 int o_cols = f->output_data.x->fringe_cols;
12335 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12336 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12337 int left_fringe_width, right_fringe_width;
12339 if (!NILP (left_fringe))
12340 left_fringe = Fcdr (left_fringe);
12341 if (!NILP (right_fringe))
12342 right_fringe = Fcdr (right_fringe);
12344 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12345 XINT (left_fringe));
12346 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12347 XINT (right_fringe));
12349 if (left_fringe_width || right_fringe_width)
12351 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12352 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12353 int conf_wid = left_wid + right_wid;
12354 int font_wid = FONT_WIDTH (f->output_data.x->font);
12355 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12356 int real_wid = cols * font_wid;
12357 if (left_wid && right_wid)
12359 if (left_fringe_width < 0)
12361 /* Left fringe width is fixed, adjust right fringe if necessary */
12362 f->output_data.x->left_fringe_width = left_wid;
12363 f->output_data.x->right_fringe_width = real_wid - left_wid;
12365 else if (right_fringe_width < 0)
12367 /* Right fringe width is fixed, adjust left fringe if necessary */
12368 f->output_data.x->left_fringe_width = real_wid - right_wid;
12369 f->output_data.x->right_fringe_width = right_wid;
12371 else
12373 /* Adjust both fringes with an equal amount.
12374 Note that we are doing integer arithmetic here, so don't
12375 lose a pixel if the total width is an odd number. */
12376 int fill = real_wid - conf_wid;
12377 f->output_data.x->left_fringe_width = left_wid + fill/2;
12378 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12381 else if (left_fringe_width)
12383 f->output_data.x->left_fringe_width = real_wid;
12384 f->output_data.x->right_fringe_width = 0;
12386 else
12388 f->output_data.x->left_fringe_width = 0;
12389 f->output_data.x->right_fringe_width = real_wid;
12391 f->output_data.x->fringe_cols = cols;
12392 f->output_data.x->fringes_extra = real_wid;
12394 else
12396 f->output_data.x->left_fringe_width = 0;
12397 f->output_data.x->right_fringe_width = 0;
12398 f->output_data.x->fringe_cols = 0;
12399 f->output_data.x->fringes_extra = 0;
12402 if (redraw && FRAME_VISIBLE_P (f))
12403 if (o_left != f->output_data.x->left_fringe_width ||
12404 o_right != f->output_data.x->right_fringe_width ||
12405 o_cols != f->output_data.x->fringe_cols)
12406 redraw_frame (f);
12409 /***********************************************************************
12410 X Input Methods
12411 ***********************************************************************/
12413 #ifdef HAVE_X_I18N
12415 #ifdef HAVE_X11R6
12417 /* XIM destroy callback function, which is called whenever the
12418 connection to input method XIM dies. CLIENT_DATA contains a
12419 pointer to the x_display_info structure corresponding to XIM. */
12421 static void
12422 xim_destroy_callback (xim, client_data, call_data)
12423 XIM xim;
12424 XPointer client_data;
12425 XPointer call_data;
12427 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12428 Lisp_Object frame, tail;
12430 BLOCK_INPUT;
12432 /* No need to call XDestroyIC.. */
12433 FOR_EACH_FRAME (tail, frame)
12435 struct frame *f = XFRAME (frame);
12436 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12438 FRAME_XIC (f) = NULL;
12439 if (FRAME_XIC_FONTSET (f))
12441 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12442 FRAME_XIC_FONTSET (f) = NULL;
12447 /* No need to call XCloseIM. */
12448 dpyinfo->xim = NULL;
12449 XFree (dpyinfo->xim_styles);
12450 UNBLOCK_INPUT;
12453 #endif /* HAVE_X11R6 */
12455 /* Open the connection to the XIM server on display DPYINFO.
12456 RESOURCE_NAME is the resource name Emacs uses. */
12458 static void
12459 xim_open_dpy (dpyinfo, resource_name)
12460 struct x_display_info *dpyinfo;
12461 char *resource_name;
12463 #ifdef USE_XIM
12464 XIM xim;
12466 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name, EMACS_CLASS);
12467 dpyinfo->xim = xim;
12469 if (xim)
12471 #ifdef HAVE_X11R6
12472 XIMCallback destroy;
12473 #endif
12475 /* Get supported styles and XIM values. */
12476 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12478 #ifdef HAVE_X11R6
12479 destroy.callback = xim_destroy_callback;
12480 destroy.client_data = (XPointer)dpyinfo;
12481 /* This isn't prototyped in OSF 5.0. */
12482 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12483 #endif
12486 #else /* not USE_XIM */
12487 dpyinfo->xim = NULL;
12488 #endif /* not USE_XIM */
12492 #ifdef HAVE_X11R6_XIM
12494 struct xim_inst_t
12496 struct x_display_info *dpyinfo;
12497 char *resource_name;
12500 /* XIM instantiate callback function, which is called whenever an XIM
12501 server is available. DISPLAY is teh display of the XIM.
12502 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12503 when the callback was registered. */
12505 static void
12506 xim_instantiate_callback (display, client_data, call_data)
12507 Display *display;
12508 XPointer client_data;
12509 XPointer call_data;
12511 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12512 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12514 /* We don't support multiple XIM connections. */
12515 if (dpyinfo->xim)
12516 return;
12518 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12520 /* Create XIC for the existing frames on the same display, as long
12521 as they have no XIC. */
12522 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12524 Lisp_Object tail, frame;
12526 BLOCK_INPUT;
12527 FOR_EACH_FRAME (tail, frame)
12529 struct frame *f = XFRAME (frame);
12531 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12532 if (FRAME_XIC (f) == NULL)
12534 create_frame_xic (f);
12535 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12536 xic_set_statusarea (f);
12537 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12539 struct window *w = XWINDOW (f->selected_window);
12540 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12545 UNBLOCK_INPUT;
12549 #endif /* HAVE_X11R6_XIM */
12552 /* Open a connection to the XIM server on display DPYINFO.
12553 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12554 connection only at the first time. On X11R6, open the connection
12555 in the XIM instantiate callback function. */
12557 static void
12558 xim_initialize (dpyinfo, resource_name)
12559 struct x_display_info *dpyinfo;
12560 char *resource_name;
12562 #ifdef USE_XIM
12563 #ifdef HAVE_X11R6_XIM
12564 struct xim_inst_t *xim_inst;
12565 int len;
12567 dpyinfo->xim = NULL;
12568 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12569 xim_inst->dpyinfo = dpyinfo;
12570 len = strlen (resource_name);
12571 xim_inst->resource_name = (char *) xmalloc (len + 1);
12572 bcopy (resource_name, xim_inst->resource_name, len + 1);
12573 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12574 resource_name, EMACS_CLASS,
12575 xim_instantiate_callback,
12576 /* Fixme: This is XPointer in
12577 XFree86 but (XPointer *) on
12578 Tru64, at least. */
12579 (XPointer) xim_inst);
12580 #else /* not HAVE_X11R6_XIM */
12581 dpyinfo->xim = NULL;
12582 xim_open_dpy (dpyinfo, resource_name);
12583 #endif /* not HAVE_X11R6_XIM */
12585 #else /* not USE_XIM */
12586 dpyinfo->xim = NULL;
12587 #endif /* not USE_XIM */
12591 /* Close the connection to the XIM server on display DPYINFO. */
12593 static void
12594 xim_close_dpy (dpyinfo)
12595 struct x_display_info *dpyinfo;
12597 #ifdef USE_XIM
12598 #ifdef HAVE_X11R6_XIM
12599 if (dpyinfo->display)
12600 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12601 NULL, EMACS_CLASS,
12602 xim_instantiate_callback, NULL);
12603 #endif /* not HAVE_X11R6_XIM */
12604 if (dpyinfo->display)
12605 XCloseIM (dpyinfo->xim);
12606 dpyinfo->xim = NULL;
12607 XFree (dpyinfo->xim_styles);
12608 #endif /* USE_XIM */
12611 #endif /* not HAVE_X11R6_XIM */
12615 /* Calculate the absolute position in frame F
12616 from its current recorded position values and gravity. */
12618 void
12619 x_calc_absolute_position (f)
12620 struct frame *f;
12622 Window child;
12623 int win_x = 0, win_y = 0;
12624 int flags = f->output_data.x->size_hint_flags;
12625 int this_window;
12627 /* We have nothing to do if the current position
12628 is already for the top-left corner. */
12629 if (! ((flags & XNegative) || (flags & YNegative)))
12630 return;
12632 #ifdef USE_X_TOOLKIT
12633 this_window = XtWindow (f->output_data.x->widget);
12634 #else
12635 this_window = FRAME_X_WINDOW (f);
12636 #endif
12638 /* Find the position of the outside upper-left corner of
12639 the inner window, with respect to the outer window.
12640 But do this only if we will need the results. */
12641 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12643 int count;
12645 BLOCK_INPUT;
12646 count = x_catch_errors (FRAME_X_DISPLAY (f));
12647 while (1)
12649 x_clear_errors (FRAME_X_DISPLAY (f));
12650 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12652 /* From-window, to-window. */
12653 this_window,
12654 f->output_data.x->parent_desc,
12656 /* From-position, to-position. */
12657 0, 0, &win_x, &win_y,
12659 /* Child of win. */
12660 &child);
12661 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12663 Window newroot, newparent = 0xdeadbeef;
12664 Window *newchildren;
12665 unsigned int nchildren;
12667 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12668 &newparent, &newchildren, &nchildren))
12669 break;
12671 XFree ((char *) newchildren);
12673 f->output_data.x->parent_desc = newparent;
12675 else
12676 break;
12679 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12680 UNBLOCK_INPUT;
12683 /* Treat negative positions as relative to the leftmost bottommost
12684 position that fits on the screen. */
12685 if (flags & XNegative)
12686 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12687 - 2 * f->output_data.x->border_width - win_x
12688 - PIXEL_WIDTH (f)
12689 + f->output_data.x->left_pos);
12692 int height = PIXEL_HEIGHT (f);
12694 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12695 /* Something is fishy here. When using Motif, starting Emacs with
12696 `-g -0-0', the frame appears too low by a few pixels.
12698 This seems to be so because initially, while Emacs is starting,
12699 the column widget's height and the frame's pixel height are
12700 different. The column widget's height is the right one. In
12701 later invocations, when Emacs is up, the frame's pixel height
12702 is right, though.
12704 It's not obvious where the initial small difference comes from.
12705 2000-12-01, gerd. */
12707 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12708 #endif
12710 if (flags & YNegative)
12711 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12712 - 2 * f->output_data.x->border_width
12713 - win_y
12714 - height
12715 + f->output_data.x->top_pos);
12718 /* The left_pos and top_pos
12719 are now relative to the top and left screen edges,
12720 so the flags should correspond. */
12721 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12724 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12725 to really change the position, and 0 when calling from
12726 x_make_frame_visible (in that case, XOFF and YOFF are the current
12727 position values). It is -1 when calling from x_set_frame_parameters,
12728 which means, do adjust for borders but don't change the gravity. */
12730 void
12731 x_set_offset (f, xoff, yoff, change_gravity)
12732 struct frame *f;
12733 register int xoff, yoff;
12734 int change_gravity;
12736 int modified_top, modified_left;
12738 if (change_gravity > 0)
12740 f->output_data.x->top_pos = yoff;
12741 f->output_data.x->left_pos = xoff;
12742 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12743 if (xoff < 0)
12744 f->output_data.x->size_hint_flags |= XNegative;
12745 if (yoff < 0)
12746 f->output_data.x->size_hint_flags |= YNegative;
12747 f->output_data.x->win_gravity = NorthWestGravity;
12749 x_calc_absolute_position (f);
12751 BLOCK_INPUT;
12752 x_wm_set_size_hint (f, (long) 0, 0);
12754 modified_left = f->output_data.x->left_pos;
12755 modified_top = f->output_data.x->top_pos;
12756 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12757 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12758 /* It is a mystery why we need to add the border_width here
12759 when the frame is already visible, but experiment says we do. */
12760 if (change_gravity != 0)
12762 modified_left += f->output_data.x->border_width;
12763 modified_top += f->output_data.x->border_width;
12765 #endif
12767 #ifdef USE_X_TOOLKIT
12768 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12769 modified_left, modified_top);
12770 #else /* not USE_X_TOOLKIT */
12771 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12772 modified_left, modified_top);
12773 #endif /* not USE_X_TOOLKIT */
12774 UNBLOCK_INPUT;
12777 /* Check if we need to resize the frame due to a fullscreen request.
12778 If so needed, resize the frame. */
12779 static void
12780 x_check_fullscreen (f)
12781 struct frame *f;
12783 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12785 int width, height, ign;
12787 x_real_positions (f, &f->output_data.x->left_pos,
12788 &f->output_data.x->top_pos);
12790 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12792 /* We do not need to move the window, it shall be taken care of
12793 when setting WM manager hints.
12794 If the frame is visible already, the position is checked by
12795 x_check_fullscreen_move. */
12796 if (f->width != width || f->height != height)
12798 change_frame_size (f, height, width, 0, 1, 0);
12799 SET_FRAME_GARBAGED (f);
12800 cancel_mouse_face (f);
12802 /* Wait for the change of frame size to occur */
12803 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12809 /* If frame parameters are set after the frame is mapped, we need to move
12810 the window. This is done in xfns.c.
12811 Some window managers moves the window to the right position, some
12812 moves the outer window manager window to the specified position.
12813 Here we check that we are in the right spot. If not, make a second
12814 move, assuming we are dealing with the second kind of window manager. */
12815 static void
12816 x_check_fullscreen_move (f)
12817 struct frame *f;
12819 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12821 int expect_top = f->output_data.x->top_pos;
12822 int expect_left = f->output_data.x->left_pos;
12824 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12825 expect_top = 0;
12826 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12827 expect_left = 0;
12829 if (expect_top != f->output_data.x->top_pos
12830 || expect_left != f->output_data.x->left_pos)
12831 x_set_offset (f, expect_left, expect_top, 1);
12833 /* Just do this once */
12834 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12839 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12840 wanted positions of the WM window (not emacs window).
12841 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12842 window (FRAME_X_WINDOW).
12844 void
12845 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12846 struct frame *f;
12847 int *width;
12848 int *height;
12849 int *top_pos;
12850 int *left_pos;
12852 int newwidth = f->width, newheight = f->height;
12854 *top_pos = f->output_data.x->top_pos;
12855 *left_pos = f->output_data.x->left_pos;
12857 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12859 int ph;
12861 ph = FRAME_X_DISPLAY_INFO (f)->height;
12862 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12863 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12864 - f->output_data.x->y_pixels_diff;
12865 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12866 *top_pos = 0;
12869 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12871 int pw;
12873 pw = FRAME_X_DISPLAY_INFO (f)->width;
12874 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12875 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12876 - f->output_data.x->x_pixels_diff;
12877 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12878 *left_pos = 0;
12881 *width = newwidth;
12882 *height = newheight;
12886 /* Change the size of frame F's X window to COLS/ROWS in the case F
12887 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12888 top-left-corner window gravity for this size change and subsequent
12889 size changes. Otherwise we leave the window gravity unchanged. */
12891 static void
12892 x_set_window_size_1 (f, change_gravity, cols, rows)
12893 struct frame *f;
12894 int change_gravity;
12895 int cols, rows;
12897 int pixelwidth, pixelheight;
12899 check_frame_size (f, &rows, &cols);
12900 f->output_data.x->vertical_scroll_bar_extra
12901 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12903 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12904 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12905 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12907 x_compute_fringe_widths (f, 0);
12909 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12910 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12912 f->output_data.x->win_gravity = NorthWestGravity;
12913 x_wm_set_size_hint (f, (long) 0, 0);
12915 XSync (FRAME_X_DISPLAY (f), False);
12916 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12917 pixelwidth, pixelheight);
12919 /* Now, strictly speaking, we can't be sure that this is accurate,
12920 but the window manager will get around to dealing with the size
12921 change request eventually, and we'll hear how it went when the
12922 ConfigureNotify event gets here.
12924 We could just not bother storing any of this information here,
12925 and let the ConfigureNotify event set everything up, but that
12926 might be kind of confusing to the Lisp code, since size changes
12927 wouldn't be reported in the frame parameters until some random
12928 point in the future when the ConfigureNotify event arrives.
12930 We pass 1 for DELAY since we can't run Lisp code inside of
12931 a BLOCK_INPUT. */
12932 change_frame_size (f, rows, cols, 0, 1, 0);
12933 PIXEL_WIDTH (f) = pixelwidth;
12934 PIXEL_HEIGHT (f) = pixelheight;
12936 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12937 receive in the ConfigureNotify event; if we get what we asked
12938 for, then the event won't cause the screen to become garbaged, so
12939 we have to make sure to do it here. */
12940 SET_FRAME_GARBAGED (f);
12942 XFlush (FRAME_X_DISPLAY (f));
12946 /* Call this to change the size of frame F's x-window.
12947 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12948 for this size change and subsequent size changes.
12949 Otherwise we leave the window gravity unchanged. */
12951 void
12952 x_set_window_size (f, change_gravity, cols, rows)
12953 struct frame *f;
12954 int change_gravity;
12955 int cols, rows;
12957 BLOCK_INPUT;
12959 #ifdef USE_X_TOOLKIT
12961 if (f->output_data.x->widget != NULL)
12963 /* The x and y position of the widget is clobbered by the
12964 call to XtSetValues within EmacsFrameSetCharSize.
12965 This is a real kludge, but I don't understand Xt so I can't
12966 figure out a correct fix. Can anyone else tell me? -- rms. */
12967 int xpos = f->output_data.x->widget->core.x;
12968 int ypos = f->output_data.x->widget->core.y;
12969 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12970 f->output_data.x->widget->core.x = xpos;
12971 f->output_data.x->widget->core.y = ypos;
12973 else
12974 x_set_window_size_1 (f, change_gravity, cols, rows);
12976 #else /* not USE_X_TOOLKIT */
12978 x_set_window_size_1 (f, change_gravity, cols, rows);
12980 #endif /* not USE_X_TOOLKIT */
12982 /* If cursor was outside the new size, mark it as off. */
12983 mark_window_cursors_off (XWINDOW (f->root_window));
12985 /* Clear out any recollection of where the mouse highlighting was,
12986 since it might be in a place that's outside the new frame size.
12987 Actually checking whether it is outside is a pain in the neck,
12988 so don't try--just let the highlighting be done afresh with new size. */
12989 cancel_mouse_face (f);
12991 UNBLOCK_INPUT;
12994 /* Mouse warping. */
12996 void
12997 x_set_mouse_position (f, x, y)
12998 struct frame *f;
12999 int x, y;
13001 int pix_x, pix_y;
13003 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13004 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
13006 if (pix_x < 0) pix_x = 0;
13007 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13009 if (pix_y < 0) pix_y = 0;
13010 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13012 BLOCK_INPUT;
13014 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13015 0, 0, 0, 0, pix_x, pix_y);
13016 UNBLOCK_INPUT;
13019 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13021 void
13022 x_set_mouse_pixel_position (f, pix_x, pix_y)
13023 struct frame *f;
13024 int pix_x, pix_y;
13026 BLOCK_INPUT;
13028 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13029 0, 0, 0, 0, pix_x, pix_y);
13030 UNBLOCK_INPUT;
13033 /* focus shifting, raising and lowering. */
13035 void
13036 x_focus_on_frame (f)
13037 struct frame *f;
13039 #if 0 /* This proves to be unpleasant. */
13040 x_raise_frame (f);
13041 #endif
13042 #if 0
13043 /* I don't think that the ICCCM allows programs to do things like this
13044 without the interaction of the window manager. Whatever you end up
13045 doing with this code, do it to x_unfocus_frame too. */
13046 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13047 RevertToPointerRoot, CurrentTime);
13048 #endif /* ! 0 */
13051 void
13052 x_unfocus_frame (f)
13053 struct frame *f;
13055 #if 0
13056 /* Look at the remarks in x_focus_on_frame. */
13057 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13058 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13059 RevertToPointerRoot, CurrentTime);
13060 #endif /* ! 0 */
13063 /* Raise frame F. */
13065 void
13066 x_raise_frame (f)
13067 struct frame *f;
13069 if (f->async_visible)
13071 BLOCK_INPUT;
13072 #ifdef USE_X_TOOLKIT
13073 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13074 #else /* not USE_X_TOOLKIT */
13075 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13076 #endif /* not USE_X_TOOLKIT */
13077 XFlush (FRAME_X_DISPLAY (f));
13078 UNBLOCK_INPUT;
13082 /* Lower frame F. */
13084 void
13085 x_lower_frame (f)
13086 struct frame *f;
13088 if (f->async_visible)
13090 BLOCK_INPUT;
13091 #ifdef USE_X_TOOLKIT
13092 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13093 #else /* not USE_X_TOOLKIT */
13094 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13095 #endif /* not USE_X_TOOLKIT */
13096 XFlush (FRAME_X_DISPLAY (f));
13097 UNBLOCK_INPUT;
13101 static void
13102 XTframe_raise_lower (f, raise_flag)
13103 FRAME_PTR f;
13104 int raise_flag;
13106 if (raise_flag)
13107 x_raise_frame (f);
13108 else
13109 x_lower_frame (f);
13112 /* Change of visibility. */
13114 /* This tries to wait until the frame is really visible.
13115 However, if the window manager asks the user where to position
13116 the frame, this will return before the user finishes doing that.
13117 The frame will not actually be visible at that time,
13118 but it will become visible later when the window manager
13119 finishes with it. */
13121 void
13122 x_make_frame_visible (f)
13123 struct frame *f;
13125 Lisp_Object type;
13126 int original_top, original_left;
13127 int retry_count = 2;
13129 retry:
13131 BLOCK_INPUT;
13133 type = x_icon_type (f);
13134 if (!NILP (type))
13135 x_bitmap_icon (f, type);
13137 if (! FRAME_VISIBLE_P (f))
13139 /* We test FRAME_GARBAGED_P here to make sure we don't
13140 call x_set_offset a second time
13141 if we get to x_make_frame_visible a second time
13142 before the window gets really visible. */
13143 if (! FRAME_ICONIFIED_P (f)
13144 && ! f->output_data.x->asked_for_visible)
13145 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13147 f->output_data.x->asked_for_visible = 1;
13149 if (! EQ (Vx_no_window_manager, Qt))
13150 x_wm_set_window_state (f, NormalState);
13151 #ifdef USE_X_TOOLKIT
13152 /* This was XtPopup, but that did nothing for an iconified frame. */
13153 XtMapWidget (f->output_data.x->widget);
13154 #else /* not USE_X_TOOLKIT */
13155 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13156 #endif /* not USE_X_TOOLKIT */
13157 #if 0 /* This seems to bring back scroll bars in the wrong places
13158 if the window configuration has changed. They seem
13159 to come back ok without this. */
13160 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13161 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13162 #endif
13165 XFlush (FRAME_X_DISPLAY (f));
13167 /* Synchronize to ensure Emacs knows the frame is visible
13168 before we do anything else. We do this loop with input not blocked
13169 so that incoming events are handled. */
13171 Lisp_Object frame;
13172 int count;
13173 /* This must be before UNBLOCK_INPUT
13174 since events that arrive in response to the actions above
13175 will set it when they are handled. */
13176 int previously_visible = f->output_data.x->has_been_visible;
13178 original_left = f->output_data.x->left_pos;
13179 original_top = f->output_data.x->top_pos;
13181 /* This must come after we set COUNT. */
13182 UNBLOCK_INPUT;
13184 /* We unblock here so that arriving X events are processed. */
13186 /* Now move the window back to where it was "supposed to be".
13187 But don't do it if the gravity is negative.
13188 When the gravity is negative, this uses a position
13189 that is 3 pixels too low. Perhaps that's really the border width.
13191 Don't do this if the window has never been visible before,
13192 because the window manager may choose the position
13193 and we don't want to override it. */
13195 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13196 && f->output_data.x->win_gravity == NorthWestGravity
13197 && previously_visible)
13199 Drawable rootw;
13200 int x, y;
13201 unsigned int width, height, border, depth;
13203 BLOCK_INPUT;
13205 /* On some window managers (such as FVWM) moving an existing
13206 window, even to the same place, causes the window manager
13207 to introduce an offset. This can cause the window to move
13208 to an unexpected location. Check the geometry (a little
13209 slow here) and then verify that the window is in the right
13210 place. If the window is not in the right place, move it
13211 there, and take the potential window manager hit. */
13212 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13213 &rootw, &x, &y, &width, &height, &border, &depth);
13215 if (original_left != x || original_top != y)
13216 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13217 original_left, original_top);
13219 UNBLOCK_INPUT;
13222 XSETFRAME (frame, f);
13224 /* Wait until the frame is visible. Process X events until a
13225 MapNotify event has been seen, or until we think we won't get a
13226 MapNotify at all.. */
13227 for (count = input_signal_count + 10;
13228 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13230 /* Force processing of queued events. */
13231 x_sync (f);
13233 /* Machines that do polling rather than SIGIO have been
13234 observed to go into a busy-wait here. So we'll fake an
13235 alarm signal to let the handler know that there's something
13236 to be read. We used to raise a real alarm, but it seems
13237 that the handler isn't always enabled here. This is
13238 probably a bug. */
13239 if (input_polling_used ())
13241 /* It could be confusing if a real alarm arrives while
13242 processing the fake one. Turn it off and let the
13243 handler reset it. */
13244 extern void poll_for_input_1 P_ ((void));
13245 int old_poll_suppress_count = poll_suppress_count;
13246 poll_suppress_count = 1;
13247 poll_for_input_1 ();
13248 poll_suppress_count = old_poll_suppress_count;
13251 /* See if a MapNotify event has been processed. */
13252 FRAME_SAMPLE_VISIBILITY (f);
13255 /* 2000-09-28: In
13257 (let ((f (selected-frame)))
13258 (iconify-frame f)
13259 (raise-frame f))
13261 the frame is not raised with various window managers on
13262 FreeBSD, Linux and Solaris. It turns out that, for some
13263 unknown reason, the call to XtMapWidget is completely ignored.
13264 Mapping the widget a second time works. */
13266 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13267 goto retry;
13271 /* Change from mapped state to withdrawn state. */
13273 /* Make the frame visible (mapped and not iconified). */
13275 void
13276 x_make_frame_invisible (f)
13277 struct frame *f;
13279 Window window;
13281 #ifdef USE_X_TOOLKIT
13282 /* Use the frame's outermost window, not the one we normally draw on. */
13283 window = XtWindow (f->output_data.x->widget);
13284 #else /* not USE_X_TOOLKIT */
13285 window = FRAME_X_WINDOW (f);
13286 #endif /* not USE_X_TOOLKIT */
13288 /* Don't keep the highlight on an invisible frame. */
13289 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13290 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13292 #if 0/* This might add unreliability; I don't trust it -- rms. */
13293 if (! f->async_visible && ! f->async_iconified)
13294 return;
13295 #endif
13297 BLOCK_INPUT;
13299 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13300 that the current position of the window is user-specified, rather than
13301 program-specified, so that when the window is mapped again, it will be
13302 placed at the same location, without forcing the user to position it
13303 by hand again (they have already done that once for this window.) */
13304 x_wm_set_size_hint (f, (long) 0, 1);
13306 #ifdef HAVE_X11R4
13308 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13309 DefaultScreen (FRAME_X_DISPLAY (f))))
13311 UNBLOCK_INPUT_RESIGNAL;
13312 error ("Can't notify window manager of window withdrawal");
13314 #else /* ! defined (HAVE_X11R4) */
13316 /* Tell the window manager what we're going to do. */
13317 if (! EQ (Vx_no_window_manager, Qt))
13319 XEvent unmap;
13321 unmap.xunmap.type = UnmapNotify;
13322 unmap.xunmap.window = window;
13323 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13324 unmap.xunmap.from_configure = False;
13325 if (! XSendEvent (FRAME_X_DISPLAY (f),
13326 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13327 False,
13328 SubstructureRedirectMaskSubstructureNotifyMask,
13329 &unmap))
13331 UNBLOCK_INPUT_RESIGNAL;
13332 error ("Can't notify window manager of withdrawal");
13336 /* Unmap the window ourselves. Cheeky! */
13337 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13338 #endif /* ! defined (HAVE_X11R4) */
13340 /* We can't distinguish this from iconification
13341 just by the event that we get from the server.
13342 So we can't win using the usual strategy of letting
13343 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13344 and synchronize with the server to make sure we agree. */
13345 f->visible = 0;
13346 FRAME_ICONIFIED_P (f) = 0;
13347 f->async_visible = 0;
13348 f->async_iconified = 0;
13350 x_sync (f);
13352 UNBLOCK_INPUT;
13355 /* Change window state from mapped to iconified. */
13357 void
13358 x_iconify_frame (f)
13359 struct frame *f;
13361 int result;
13362 Lisp_Object type;
13364 /* Don't keep the highlight on an invisible frame. */
13365 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13366 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13368 if (f->async_iconified)
13369 return;
13371 BLOCK_INPUT;
13373 FRAME_SAMPLE_VISIBILITY (f);
13375 type = x_icon_type (f);
13376 if (!NILP (type))
13377 x_bitmap_icon (f, type);
13379 #ifdef USE_X_TOOLKIT
13381 if (! FRAME_VISIBLE_P (f))
13383 if (! EQ (Vx_no_window_manager, Qt))
13384 x_wm_set_window_state (f, IconicState);
13385 /* This was XtPopup, but that did nothing for an iconified frame. */
13386 XtMapWidget (f->output_data.x->widget);
13387 /* The server won't give us any event to indicate
13388 that an invisible frame was changed to an icon,
13389 so we have to record it here. */
13390 f->iconified = 1;
13391 f->visible = 1;
13392 f->async_iconified = 1;
13393 f->async_visible = 0;
13394 UNBLOCK_INPUT;
13395 return;
13398 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13399 XtWindow (f->output_data.x->widget),
13400 DefaultScreen (FRAME_X_DISPLAY (f)));
13401 UNBLOCK_INPUT;
13403 if (!result)
13404 error ("Can't notify window manager of iconification");
13406 f->async_iconified = 1;
13407 f->async_visible = 0;
13410 BLOCK_INPUT;
13411 XFlush (FRAME_X_DISPLAY (f));
13412 UNBLOCK_INPUT;
13413 #else /* not USE_X_TOOLKIT */
13415 /* Make sure the X server knows where the window should be positioned,
13416 in case the user deiconifies with the window manager. */
13417 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13418 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13420 /* Since we don't know which revision of X we're running, we'll use both
13421 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13423 /* X11R4: send a ClientMessage to the window manager using the
13424 WM_CHANGE_STATE type. */
13426 XEvent message;
13428 message.xclient.window = FRAME_X_WINDOW (f);
13429 message.xclient.type = ClientMessage;
13430 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13431 message.xclient.format = 32;
13432 message.xclient.data.l[0] = IconicState;
13434 if (! XSendEvent (FRAME_X_DISPLAY (f),
13435 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13436 False,
13437 SubstructureRedirectMask | SubstructureNotifyMask,
13438 &message))
13440 UNBLOCK_INPUT_RESIGNAL;
13441 error ("Can't notify window manager of iconification");
13445 /* X11R3: set the initial_state field of the window manager hints to
13446 IconicState. */
13447 x_wm_set_window_state (f, IconicState);
13449 if (!FRAME_VISIBLE_P (f))
13451 /* If the frame was withdrawn, before, we must map it. */
13452 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13455 f->async_iconified = 1;
13456 f->async_visible = 0;
13458 XFlush (FRAME_X_DISPLAY (f));
13459 UNBLOCK_INPUT;
13460 #endif /* not USE_X_TOOLKIT */
13464 /* Free X resources of frame F. */
13466 void
13467 x_free_frame_resources (f)
13468 struct frame *f;
13470 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13471 Lisp_Object bar;
13472 struct scroll_bar *b;
13474 BLOCK_INPUT;
13476 /* If a display connection is dead, don't try sending more
13477 commands to the X server. */
13478 if (dpyinfo->display)
13480 if (f->output_data.x->icon_desc)
13481 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13483 #ifdef USE_X_TOOLKIT
13484 /* Explicitly destroy the scroll bars of the frame. Without
13485 this, we get "BadDrawable" errors from the toolkit later on,
13486 presumably from expose events generated for the disappearing
13487 toolkit scroll bars. */
13488 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13490 b = XSCROLL_BAR (bar);
13491 x_scroll_bar_remove (b);
13493 #endif
13495 #ifdef HAVE_X_I18N
13496 if (FRAME_XIC (f))
13497 free_frame_xic (f);
13498 #endif
13500 #ifdef USE_X_TOOLKIT
13501 if (f->output_data.x->widget)
13503 XtDestroyWidget (f->output_data.x->widget);
13504 f->output_data.x->widget = NULL;
13506 /* Tooltips don't have widgets, only a simple X window, even if
13507 we are using a toolkit. */
13508 else if (FRAME_X_WINDOW (f))
13509 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13511 free_frame_menubar (f);
13512 #else /* !USE_X_TOOLKIT */
13513 if (FRAME_X_WINDOW (f))
13514 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13515 #endif /* !USE_X_TOOLKIT */
13517 unload_color (f, f->output_data.x->foreground_pixel);
13518 unload_color (f, f->output_data.x->background_pixel);
13519 unload_color (f, f->output_data.x->cursor_pixel);
13520 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13521 unload_color (f, f->output_data.x->border_pixel);
13522 unload_color (f, f->output_data.x->mouse_pixel);
13524 if (f->output_data.x->scroll_bar_background_pixel != -1)
13525 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13526 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13527 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13528 #ifdef USE_TOOLKIT_SCROLL_BARS
13529 /* Scrollbar shadow colors. */
13530 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13531 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13532 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13533 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13534 #endif /* USE_TOOLKIT_SCROLL_BARS */
13535 if (f->output_data.x->white_relief.allocated_p)
13536 unload_color (f, f->output_data.x->white_relief.pixel);
13537 if (f->output_data.x->black_relief.allocated_p)
13538 unload_color (f, f->output_data.x->black_relief.pixel);
13540 if (FRAME_FACE_CACHE (f))
13541 free_frame_faces (f);
13543 x_free_gcs (f);
13544 XFlush (FRAME_X_DISPLAY (f));
13547 if (f->output_data.x->saved_menu_event)
13548 xfree (f->output_data.x->saved_menu_event);
13550 xfree (f->output_data.x);
13551 f->output_data.x = NULL;
13553 if (f == dpyinfo->x_focus_frame)
13554 dpyinfo->x_focus_frame = 0;
13555 if (f == dpyinfo->x_focus_event_frame)
13556 dpyinfo->x_focus_event_frame = 0;
13557 if (f == dpyinfo->x_highlight_frame)
13558 dpyinfo->x_highlight_frame = 0;
13560 if (f == dpyinfo->mouse_face_mouse_frame)
13562 dpyinfo->mouse_face_beg_row
13563 = dpyinfo->mouse_face_beg_col = -1;
13564 dpyinfo->mouse_face_end_row
13565 = dpyinfo->mouse_face_end_col = -1;
13566 dpyinfo->mouse_face_window = Qnil;
13567 dpyinfo->mouse_face_deferred_gc = 0;
13568 dpyinfo->mouse_face_mouse_frame = 0;
13571 UNBLOCK_INPUT;
13575 /* Destroy the X window of frame F. */
13577 void
13578 x_destroy_window (f)
13579 struct frame *f;
13581 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13583 /* If a display connection is dead, don't try sending more
13584 commands to the X server. */
13585 if (dpyinfo->display != 0)
13586 x_free_frame_resources (f);
13588 dpyinfo->reference_count--;
13592 /* Setting window manager hints. */
13594 /* Set the normal size hints for the window manager, for frame F.
13595 FLAGS is the flags word to use--or 0 meaning preserve the flags
13596 that the window now has.
13597 If USER_POSITION is nonzero, we set the USPosition
13598 flag (this is useful when FLAGS is 0). */
13600 void
13601 x_wm_set_size_hint (f, flags, user_position)
13602 struct frame *f;
13603 long flags;
13604 int user_position;
13606 XSizeHints size_hints;
13608 #ifdef USE_X_TOOLKIT
13609 Arg al[2];
13610 int ac = 0;
13611 Dimension widget_width, widget_height;
13612 Window window = XtWindow (f->output_data.x->widget);
13613 #else /* not USE_X_TOOLKIT */
13614 Window window = FRAME_X_WINDOW (f);
13615 #endif /* not USE_X_TOOLKIT */
13617 /* Setting PMaxSize caused various problems. */
13618 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13620 size_hints.x = f->output_data.x->left_pos;
13621 size_hints.y = f->output_data.x->top_pos;
13623 #ifdef USE_X_TOOLKIT
13624 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13625 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13626 XtGetValues (f->output_data.x->widget, al, ac);
13627 size_hints.height = widget_height;
13628 size_hints.width = widget_width;
13629 #else /* not USE_X_TOOLKIT */
13630 size_hints.height = PIXEL_HEIGHT (f);
13631 size_hints.width = PIXEL_WIDTH (f);
13632 #endif /* not USE_X_TOOLKIT */
13634 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13635 size_hints.height_inc = f->output_data.x->line_height;
13636 size_hints.max_width
13637 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13638 size_hints.max_height
13639 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13641 /* Calculate the base and minimum sizes.
13643 (When we use the X toolkit, we don't do it here.
13644 Instead we copy the values that the widgets are using, below.) */
13645 #ifndef USE_X_TOOLKIT
13647 int base_width, base_height;
13648 int min_rows = 0, min_cols = 0;
13650 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13651 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13653 check_frame_size (f, &min_rows, &min_cols);
13655 /* The window manager uses the base width hints to calculate the
13656 current number of rows and columns in the frame while
13657 resizing; min_width and min_height aren't useful for this
13658 purpose, since they might not give the dimensions for a
13659 zero-row, zero-column frame.
13661 We use the base_width and base_height members if we have
13662 them; otherwise, we set the min_width and min_height members
13663 to the size for a zero x zero frame. */
13665 #ifdef HAVE_X11R4
13666 size_hints.flags |= PBaseSize;
13667 size_hints.base_width = base_width;
13668 size_hints.base_height = base_height;
13669 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13670 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13671 #else
13672 size_hints.min_width = base_width;
13673 size_hints.min_height = base_height;
13674 #endif
13677 /* If we don't need the old flags, we don't need the old hint at all. */
13678 if (flags)
13680 size_hints.flags |= flags;
13681 goto no_read;
13683 #endif /* not USE_X_TOOLKIT */
13686 XSizeHints hints; /* Sometimes I hate X Windows... */
13687 long supplied_return;
13688 int value;
13690 #ifdef HAVE_X11R4
13691 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13692 &supplied_return);
13693 #else
13694 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13695 #endif
13697 #ifdef USE_X_TOOLKIT
13698 size_hints.base_height = hints.base_height;
13699 size_hints.base_width = hints.base_width;
13700 size_hints.min_height = hints.min_height;
13701 size_hints.min_width = hints.min_width;
13702 #endif
13704 if (flags)
13705 size_hints.flags |= flags;
13706 else
13708 if (value == 0)
13709 hints.flags = 0;
13710 if (hints.flags & PSize)
13711 size_hints.flags |= PSize;
13712 if (hints.flags & PPosition)
13713 size_hints.flags |= PPosition;
13714 if (hints.flags & USPosition)
13715 size_hints.flags |= USPosition;
13716 if (hints.flags & USSize)
13717 size_hints.flags |= USSize;
13721 #ifndef USE_X_TOOLKIT
13722 no_read:
13723 #endif
13725 #ifdef PWinGravity
13726 size_hints.win_gravity = f->output_data.x->win_gravity;
13727 size_hints.flags |= PWinGravity;
13729 if (user_position)
13731 size_hints.flags &= ~ PPosition;
13732 size_hints.flags |= USPosition;
13734 #endif /* PWinGravity */
13736 #ifdef HAVE_X11R4
13737 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13738 #else
13739 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13740 #endif
13743 /* Used for IconicState or NormalState */
13745 void
13746 x_wm_set_window_state (f, state)
13747 struct frame *f;
13748 int state;
13750 #ifdef USE_X_TOOLKIT
13751 Arg al[1];
13753 XtSetArg (al[0], XtNinitialState, state);
13754 XtSetValues (f->output_data.x->widget, al, 1);
13755 #else /* not USE_X_TOOLKIT */
13756 Window window = FRAME_X_WINDOW (f);
13758 f->output_data.x->wm_hints.flags |= StateHint;
13759 f->output_data.x->wm_hints.initial_state = state;
13761 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13762 #endif /* not USE_X_TOOLKIT */
13765 void
13766 x_wm_set_icon_pixmap (f, pixmap_id)
13767 struct frame *f;
13768 int pixmap_id;
13770 Pixmap icon_pixmap;
13772 #ifndef USE_X_TOOLKIT
13773 Window window = FRAME_X_WINDOW (f);
13774 #endif
13776 if (pixmap_id > 0)
13778 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13779 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13781 else
13783 /* It seems there is no way to turn off use of an icon pixmap.
13784 The following line does it, only if no icon has yet been created,
13785 for some window managers. But with mwm it crashes.
13786 Some people say it should clear the IconPixmapHint bit in this case,
13787 but that doesn't work, and the X consortium said it isn't the
13788 right thing at all. Since there is no way to win,
13789 best to explicitly give up. */
13790 #if 0
13791 f->output_data.x->wm_hints.icon_pixmap = None;
13792 #else
13793 return;
13794 #endif
13797 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13800 Arg al[1];
13801 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13802 XtSetValues (f->output_data.x->widget, al, 1);
13805 #else /* not USE_X_TOOLKIT */
13807 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13808 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13810 #endif /* not USE_X_TOOLKIT */
13813 void
13814 x_wm_set_icon_position (f, icon_x, icon_y)
13815 struct frame *f;
13816 int icon_x, icon_y;
13818 #ifdef USE_X_TOOLKIT
13819 Window window = XtWindow (f->output_data.x->widget);
13820 #else
13821 Window window = FRAME_X_WINDOW (f);
13822 #endif
13824 f->output_data.x->wm_hints.flags |= IconPositionHint;
13825 f->output_data.x->wm_hints.icon_x = icon_x;
13826 f->output_data.x->wm_hints.icon_y = icon_y;
13828 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13832 /***********************************************************************
13833 Fonts
13834 ***********************************************************************/
13836 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13838 struct font_info *
13839 x_get_font_info (f, font_idx)
13840 FRAME_PTR f;
13841 int font_idx;
13843 return (FRAME_X_FONT_TABLE (f) + font_idx);
13847 /* Return a list of names of available fonts matching PATTERN on frame F.
13849 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13850 to be listed.
13852 SIZE < 0 means include scalable fonts.
13854 Frame F null means we have not yet created any frame on X, and
13855 consult the first display in x_display_list. MAXNAMES sets a limit
13856 on how many fonts to match. */
13858 Lisp_Object
13859 x_list_fonts (f, pattern, size, maxnames)
13860 struct frame *f;
13861 Lisp_Object pattern;
13862 int size;
13863 int maxnames;
13865 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13866 Lisp_Object tem, second_best;
13867 struct x_display_info *dpyinfo
13868 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13869 Display *dpy = dpyinfo->display;
13870 int try_XLoadQueryFont = 0;
13871 int count;
13872 int allow_scalable_fonts_p = 0;
13874 if (size < 0)
13876 allow_scalable_fonts_p = 1;
13877 size = 0;
13880 patterns = Fassoc (pattern, Valternate_fontname_alist);
13881 if (NILP (patterns))
13882 patterns = Fcons (pattern, Qnil);
13884 if (maxnames == 1 && !size)
13885 /* We can return any single font matching PATTERN. */
13886 try_XLoadQueryFont = 1;
13888 for (; CONSP (patterns); patterns = XCDR (patterns))
13890 int num_fonts;
13891 char **names = NULL;
13893 pattern = XCAR (patterns);
13894 /* See if we cached the result for this particular query.
13895 The cache is an alist of the form:
13896 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13897 tem = XCDR (dpyinfo->name_list_element);
13898 key = Fcons (Fcons (pattern, make_number (maxnames)),
13899 allow_scalable_fonts_p ? Qt : Qnil);
13900 list = Fassoc (key, tem);
13901 if (!NILP (list))
13903 list = Fcdr_safe (list);
13904 /* We have a cashed list. Don't have to get the list again. */
13905 goto label_cached;
13908 /* At first, put PATTERN in the cache. */
13910 BLOCK_INPUT;
13911 count = x_catch_errors (dpy);
13913 if (try_XLoadQueryFont)
13915 XFontStruct *font;
13916 unsigned long value;
13918 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13919 if (x_had_errors_p (dpy))
13921 /* This error is perhaps due to insufficient memory on X
13922 server. Let's just ignore it. */
13923 font = NULL;
13924 x_clear_errors (dpy);
13927 if (font
13928 && XGetFontProperty (font, XA_FONT, &value))
13930 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13931 int len = strlen (name);
13932 char *tmp;
13934 /* If DXPC (a Differential X Protocol Compressor)
13935 Ver.3.7 is running, XGetAtomName will return null
13936 string. We must avoid such a name. */
13937 if (len == 0)
13938 try_XLoadQueryFont = 0;
13939 else
13941 num_fonts = 1;
13942 names = (char **) alloca (sizeof (char *));
13943 /* Some systems only allow alloca assigned to a
13944 simple var. */
13945 tmp = (char *) alloca (len + 1); names[0] = tmp;
13946 bcopy (name, names[0], len + 1);
13947 XFree (name);
13950 else
13951 try_XLoadQueryFont = 0;
13953 if (font)
13954 XFreeFont (dpy, font);
13957 if (!try_XLoadQueryFont)
13959 /* We try at least 10 fonts because XListFonts will return
13960 auto-scaled fonts at the head. */
13961 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13962 &num_fonts);
13963 if (x_had_errors_p (dpy))
13965 /* This error is perhaps due to insufficient memory on X
13966 server. Let's just ignore it. */
13967 names = NULL;
13968 x_clear_errors (dpy);
13972 x_uncatch_errors (dpy, count);
13973 UNBLOCK_INPUT;
13975 if (names)
13977 int i;
13979 /* Make a list of all the fonts we got back.
13980 Store that in the font cache for the display. */
13981 for (i = 0; i < num_fonts; i++)
13983 int width = 0;
13984 char *p = names[i];
13985 int average_width = -1, dashes = 0;
13987 /* Count the number of dashes in NAMES[I]. If there are
13988 14 dashes, and the field value following 12th dash
13989 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13990 is usually too ugly to be used for editing. Let's
13991 ignore it. */
13992 while (*p)
13993 if (*p++ == '-')
13995 dashes++;
13996 if (dashes == 7) /* PIXEL_SIZE field */
13997 width = atoi (p);
13998 else if (dashes == 12) /* AVERAGE_WIDTH field */
13999 average_width = atoi (p);
14002 if (allow_scalable_fonts_p
14003 || dashes < 14 || average_width != 0)
14005 tem = build_string (names[i]);
14006 if (NILP (Fassoc (tem, list)))
14008 if (STRINGP (Vx_pixel_size_width_font_regexp)
14009 && ((fast_c_string_match_ignore_case
14010 (Vx_pixel_size_width_font_regexp, names[i]))
14011 >= 0))
14012 /* We can set the value of PIXEL_SIZE to the
14013 width of this font. */
14014 list = Fcons (Fcons (tem, make_number (width)), list);
14015 else
14016 /* For the moment, width is not known. */
14017 list = Fcons (Fcons (tem, Qnil), list);
14022 if (!try_XLoadQueryFont)
14024 BLOCK_INPUT;
14025 XFreeFontNames (names);
14026 UNBLOCK_INPUT;
14030 /* Now store the result in the cache. */
14031 XSETCDR (dpyinfo->name_list_element,
14032 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14034 label_cached:
14035 if (NILP (list)) continue; /* Try the remaining alternatives. */
14037 newlist = second_best = Qnil;
14038 /* Make a list of the fonts that have the right width. */
14039 for (; CONSP (list); list = XCDR (list))
14041 int found_size;
14043 tem = XCAR (list);
14045 if (!CONSP (tem) || NILP (XCAR (tem)))
14046 continue;
14047 if (!size)
14049 newlist = Fcons (XCAR (tem), newlist);
14050 continue;
14053 if (!INTEGERP (XCDR (tem)))
14055 /* Since we have not yet known the size of this font, we
14056 must try slow function call XLoadQueryFont. */
14057 XFontStruct *thisinfo;
14059 BLOCK_INPUT;
14060 count = x_catch_errors (dpy);
14061 thisinfo = XLoadQueryFont (dpy,
14062 XSTRING (XCAR (tem))->data);
14063 if (x_had_errors_p (dpy))
14065 /* This error is perhaps due to insufficient memory on X
14066 server. Let's just ignore it. */
14067 thisinfo = NULL;
14068 x_clear_errors (dpy);
14070 x_uncatch_errors (dpy, count);
14071 UNBLOCK_INPUT;
14073 if (thisinfo)
14075 XSETCDR (tem,
14076 (thisinfo->min_bounds.width == 0
14077 ? make_number (0)
14078 : make_number (thisinfo->max_bounds.width)));
14079 BLOCK_INPUT;
14080 XFreeFont (dpy, thisinfo);
14081 UNBLOCK_INPUT;
14083 else
14084 /* For unknown reason, the previous call of XListFont had
14085 returned a font which can't be opened. Record the size
14086 as 0 not to try to open it again. */
14087 XSETCDR (tem, make_number (0));
14090 found_size = XINT (XCDR (tem));
14091 if (found_size == size)
14092 newlist = Fcons (XCAR (tem), newlist);
14093 else if (found_size > 0)
14095 if (NILP (second_best))
14096 second_best = tem;
14097 else if (found_size < size)
14099 if (XINT (XCDR (second_best)) > size
14100 || XINT (XCDR (second_best)) < found_size)
14101 second_best = tem;
14103 else
14105 if (XINT (XCDR (second_best)) > size
14106 && XINT (XCDR (second_best)) > found_size)
14107 second_best = tem;
14111 if (!NILP (newlist))
14112 break;
14113 else if (!NILP (second_best))
14115 newlist = Fcons (XCAR (second_best), Qnil);
14116 break;
14120 return newlist;
14124 #if GLYPH_DEBUG
14126 /* Check that FONT is valid on frame F. It is if it can be found in F's
14127 font table. */
14129 static void
14130 x_check_font (f, font)
14131 struct frame *f;
14132 XFontStruct *font;
14134 int i;
14135 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14137 xassert (font != NULL);
14139 for (i = 0; i < dpyinfo->n_fonts; i++)
14140 if (dpyinfo->font_table[i].name
14141 && font == dpyinfo->font_table[i].font)
14142 break;
14144 xassert (i < dpyinfo->n_fonts);
14147 #endif /* GLYPH_DEBUG != 0 */
14149 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14150 Note: There are (broken) X fonts out there with invalid XFontStruct
14151 min_bounds contents. For example, handa@etl.go.jp reports that
14152 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14153 have font->min_bounds.width == 0. */
14155 static INLINE void
14156 x_font_min_bounds (font, w, h)
14157 XFontStruct *font;
14158 int *w, *h;
14160 *h = FONT_HEIGHT (font);
14161 *w = font->min_bounds.width;
14163 /* Try to handle the case where FONT->min_bounds has invalid
14164 contents. Since the only font known to have invalid min_bounds
14165 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14166 if (*w <= 0)
14167 *w = font->max_bounds.width;
14171 /* Compute the smallest character width and smallest font height over
14172 all fonts available on frame F. Set the members smallest_char_width
14173 and smallest_font_height in F's x_display_info structure to
14174 the values computed. Value is non-zero if smallest_font_height or
14175 smallest_char_width become smaller than they were before. */
14177 static int
14178 x_compute_min_glyph_bounds (f)
14179 struct frame *f;
14181 int i;
14182 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14183 XFontStruct *font;
14184 int old_width = dpyinfo->smallest_char_width;
14185 int old_height = dpyinfo->smallest_font_height;
14187 dpyinfo->smallest_font_height = 100000;
14188 dpyinfo->smallest_char_width = 100000;
14190 for (i = 0; i < dpyinfo->n_fonts; ++i)
14191 if (dpyinfo->font_table[i].name)
14193 struct font_info *fontp = dpyinfo->font_table + i;
14194 int w, h;
14196 font = (XFontStruct *) fontp->font;
14197 xassert (font != (XFontStruct *) ~0);
14198 x_font_min_bounds (font, &w, &h);
14200 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14201 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14204 xassert (dpyinfo->smallest_char_width > 0
14205 && dpyinfo->smallest_font_height > 0);
14207 return (dpyinfo->n_fonts == 1
14208 || dpyinfo->smallest_char_width < old_width
14209 || dpyinfo->smallest_font_height < old_height);
14213 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14214 pointer to the structure font_info while allocating it dynamically.
14215 If SIZE is 0, load any size of font.
14216 If loading is failed, return NULL. */
14218 struct font_info *
14219 x_load_font (f, fontname, size)
14220 struct frame *f;
14221 register char *fontname;
14222 int size;
14224 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14225 Lisp_Object font_names;
14226 int count;
14228 /* Get a list of all the fonts that match this name. Once we
14229 have a list of matching fonts, we compare them against the fonts
14230 we already have by comparing names. */
14231 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14233 if (!NILP (font_names))
14235 Lisp_Object tail;
14236 int i;
14238 for (i = 0; i < dpyinfo->n_fonts; i++)
14239 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14240 if (dpyinfo->font_table[i].name
14241 && (!strcmp (dpyinfo->font_table[i].name,
14242 XSTRING (XCAR (tail))->data)
14243 || !strcmp (dpyinfo->font_table[i].full_name,
14244 XSTRING (XCAR (tail))->data)))
14245 return (dpyinfo->font_table + i);
14248 /* Load the font and add it to the table. */
14250 char *full_name;
14251 XFontStruct *font;
14252 struct font_info *fontp;
14253 unsigned long value;
14254 int i;
14256 /* If we have found fonts by x_list_font, load one of them. If
14257 not, we still try to load a font by the name given as FONTNAME
14258 because XListFonts (called in x_list_font) of some X server has
14259 a bug of not finding a font even if the font surely exists and
14260 is loadable by XLoadQueryFont. */
14261 if (size > 0 && !NILP (font_names))
14262 fontname = (char *) XSTRING (XCAR (font_names))->data;
14264 BLOCK_INPUT;
14265 count = x_catch_errors (FRAME_X_DISPLAY (f));
14266 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14267 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14269 /* This error is perhaps due to insufficient memory on X
14270 server. Let's just ignore it. */
14271 font = NULL;
14272 x_clear_errors (FRAME_X_DISPLAY (f));
14274 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14275 UNBLOCK_INPUT;
14276 if (!font)
14277 return NULL;
14279 /* Find a free slot in the font table. */
14280 for (i = 0; i < dpyinfo->n_fonts; ++i)
14281 if (dpyinfo->font_table[i].name == NULL)
14282 break;
14284 /* If no free slot found, maybe enlarge the font table. */
14285 if (i == dpyinfo->n_fonts
14286 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14288 int sz;
14289 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14290 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14291 dpyinfo->font_table
14292 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14295 fontp = dpyinfo->font_table + i;
14296 if (i == dpyinfo->n_fonts)
14297 ++dpyinfo->n_fonts;
14299 /* Now fill in the slots of *FONTP. */
14300 BLOCK_INPUT;
14301 fontp->font = font;
14302 fontp->font_idx = i;
14303 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14304 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14306 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14307 full_name = 0;
14308 if (XGetFontProperty (font, XA_FONT, &value))
14310 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14311 char *p = name;
14312 int dashes = 0;
14314 /* Count the number of dashes in the "full name".
14315 If it is too few, this isn't really the font's full name,
14316 so don't use it.
14317 In X11R4, the fonts did not come with their canonical names
14318 stored in them. */
14319 while (*p)
14321 if (*p == '-')
14322 dashes++;
14323 p++;
14326 if (dashes >= 13)
14328 full_name = (char *) xmalloc (p - name + 1);
14329 bcopy (name, full_name, p - name + 1);
14332 XFree (name);
14335 if (full_name != 0)
14336 fontp->full_name = full_name;
14337 else
14338 fontp->full_name = fontp->name;
14340 fontp->size = font->max_bounds.width;
14341 fontp->height = FONT_HEIGHT (font);
14343 if (NILP (font_names))
14345 /* We come here because of a bug of XListFonts mentioned at
14346 the head of this block. Let's store this information in
14347 the cache for x_list_fonts. */
14348 Lisp_Object lispy_name = build_string (fontname);
14349 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14350 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14351 Qnil);
14353 XSETCDR (dpyinfo->name_list_element,
14354 Fcons (Fcons (key,
14355 Fcons (Fcons (lispy_full_name,
14356 make_number (fontp->size)),
14357 Qnil)),
14358 XCDR (dpyinfo->name_list_element)));
14359 if (full_name)
14361 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14362 Qnil);
14363 XSETCDR (dpyinfo->name_list_element,
14364 Fcons (Fcons (key,
14365 Fcons (Fcons (lispy_full_name,
14366 make_number (fontp->size)),
14367 Qnil)),
14368 XCDR (dpyinfo->name_list_element)));
14372 /* The slot `encoding' specifies how to map a character
14373 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14374 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14375 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14376 2:0xA020..0xFF7F). For the moment, we don't know which charset
14377 uses this font. So, we set information in fontp->encoding[1]
14378 which is never used by any charset. If mapping can't be
14379 decided, set FONT_ENCODING_NOT_DECIDED. */
14380 fontp->encoding[1]
14381 = (font->max_byte1 == 0
14382 /* 1-byte font */
14383 ? (font->min_char_or_byte2 < 0x80
14384 ? (font->max_char_or_byte2 < 0x80
14385 ? 0 /* 0x20..0x7F */
14386 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14387 : 1) /* 0xA0..0xFF */
14388 /* 2-byte font */
14389 : (font->min_byte1 < 0x80
14390 ? (font->max_byte1 < 0x80
14391 ? (font->min_char_or_byte2 < 0x80
14392 ? (font->max_char_or_byte2 < 0x80
14393 ? 0 /* 0x2020..0x7F7F */
14394 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14395 : 3) /* 0x20A0..0x7FFF */
14396 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14397 : (font->min_char_or_byte2 < 0x80
14398 ? (font->max_char_or_byte2 < 0x80
14399 ? 2 /* 0xA020..0xFF7F */
14400 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14401 : 1))); /* 0xA0A0..0xFFFF */
14403 fontp->baseline_offset
14404 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14405 ? (long) value : 0);
14406 fontp->relative_compose
14407 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14408 ? (long) value : 0);
14409 fontp->default_ascent
14410 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14411 ? (long) value : 0);
14413 /* Set global flag fonts_changed_p to non-zero if the font loaded
14414 has a character with a smaller width than any other character
14415 before, or if the font loaded has a smaller height than any
14416 other font loaded before. If this happens, it will make a
14417 glyph matrix reallocation necessary. */
14418 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14419 UNBLOCK_INPUT;
14420 return fontp;
14425 /* Return a pointer to struct font_info of a font named FONTNAME for
14426 frame F. If no such font is loaded, return NULL. */
14428 struct font_info *
14429 x_query_font (f, fontname)
14430 struct frame *f;
14431 register char *fontname;
14433 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14434 int i;
14436 for (i = 0; i < dpyinfo->n_fonts; i++)
14437 if (dpyinfo->font_table[i].name
14438 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14439 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14440 return (dpyinfo->font_table + i);
14441 return NULL;
14445 /* Find a CCL program for a font specified by FONTP, and set the member
14446 `encoder' of the structure. */
14448 void
14449 x_find_ccl_program (fontp)
14450 struct font_info *fontp;
14452 Lisp_Object list, elt;
14454 elt = Qnil;
14455 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14457 elt = XCAR (list);
14458 if (CONSP (elt)
14459 && STRINGP (XCAR (elt))
14460 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14461 >= 0)
14462 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14463 >= 0)))
14464 break;
14467 if (! NILP (list))
14469 struct ccl_program *ccl
14470 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14472 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14473 xfree (ccl);
14474 else
14475 fontp->font_encoder = ccl;
14481 /***********************************************************************
14482 Initialization
14483 ***********************************************************************/
14485 #ifdef USE_X_TOOLKIT
14486 static XrmOptionDescRec emacs_options[] = {
14487 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14488 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14490 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14491 XrmoptionSepArg, NULL},
14492 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14494 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14495 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14496 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14497 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14498 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14499 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14500 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14502 #endif /* USE_X_TOOLKIT */
14504 static int x_initialized;
14506 #ifdef MULTI_KBOARD
14507 /* Test whether two display-name strings agree up to the dot that separates
14508 the screen number from the server number. */
14509 static int
14510 same_x_server (name1, name2)
14511 char *name1, *name2;
14513 int seen_colon = 0;
14514 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14515 int system_name_length = strlen (system_name);
14516 int length_until_period = 0;
14518 while (system_name[length_until_period] != 0
14519 && system_name[length_until_period] != '.')
14520 length_until_period++;
14522 /* Treat `unix' like an empty host name. */
14523 if (! strncmp (name1, "unix:", 5))
14524 name1 += 4;
14525 if (! strncmp (name2, "unix:", 5))
14526 name2 += 4;
14527 /* Treat this host's name like an empty host name. */
14528 if (! strncmp (name1, system_name, system_name_length)
14529 && name1[system_name_length] == ':')
14530 name1 += system_name_length;
14531 if (! strncmp (name2, system_name, system_name_length)
14532 && name2[system_name_length] == ':')
14533 name2 += system_name_length;
14534 /* Treat this host's domainless name like an empty host name. */
14535 if (! strncmp (name1, system_name, length_until_period)
14536 && name1[length_until_period] == ':')
14537 name1 += length_until_period;
14538 if (! strncmp (name2, system_name, length_until_period)
14539 && name2[length_until_period] == ':')
14540 name2 += length_until_period;
14542 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14544 if (*name1 == ':')
14545 seen_colon++;
14546 if (seen_colon && *name1 == '.')
14547 return 1;
14549 return (seen_colon
14550 && (*name1 == '.' || *name1 == '\0')
14551 && (*name2 == '.' || *name2 == '\0'));
14553 #endif
14555 struct x_display_info *
14556 x_term_init (display_name, xrm_option, resource_name)
14557 Lisp_Object display_name;
14558 char *xrm_option;
14559 char *resource_name;
14561 int connection;
14562 Display *dpy;
14563 struct x_display_info *dpyinfo;
14564 XrmDatabase xrdb;
14566 BLOCK_INPUT;
14568 if (!x_initialized)
14570 x_initialize ();
14571 x_initialized = 1;
14574 #ifdef USE_X_TOOLKIT
14575 /* weiner@footloose.sps.mot.com reports that this causes
14576 errors with X11R5:
14577 X protocol error: BadAtom (invalid Atom parameter)
14578 on protocol request 18skiloaf.
14579 So let's not use it until R6. */
14580 #ifdef HAVE_X11XTR6
14581 XtSetLanguageProc (NULL, NULL, NULL);
14582 #endif
14585 int argc = 0;
14586 char *argv[3];
14588 argv[0] = "";
14589 argc = 1;
14590 if (xrm_option)
14592 argv[argc++] = "-xrm";
14593 argv[argc++] = xrm_option;
14595 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14596 resource_name, EMACS_CLASS,
14597 emacs_options, XtNumber (emacs_options),
14598 &argc, argv);
14600 #ifdef HAVE_X11XTR6
14601 /* I think this is to compensate for XtSetLanguageProc. */
14602 fixup_locale ();
14603 #endif
14606 #else /* not USE_X_TOOLKIT */
14607 #ifdef HAVE_X11R5
14608 XSetLocaleModifiers ("");
14609 #endif
14610 dpy = XOpenDisplay (XSTRING (display_name)->data);
14611 #endif /* not USE_X_TOOLKIT */
14613 /* Detect failure. */
14614 if (dpy == 0)
14616 UNBLOCK_INPUT;
14617 return 0;
14620 /* We have definitely succeeded. Record the new connection. */
14622 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14623 bzero (dpyinfo, sizeof *dpyinfo);
14625 #ifdef MULTI_KBOARD
14627 struct x_display_info *share;
14628 Lisp_Object tail;
14630 for (share = x_display_list, tail = x_display_name_list; share;
14631 share = share->next, tail = XCDR (tail))
14632 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14633 XSTRING (display_name)->data))
14634 break;
14635 if (share)
14636 dpyinfo->kboard = share->kboard;
14637 else
14639 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14640 init_kboard (dpyinfo->kboard);
14641 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14643 char *vendor = ServerVendor (dpy);
14644 UNBLOCK_INPUT;
14645 dpyinfo->kboard->Vsystem_key_alist
14646 = call1 (Qvendor_specific_keysyms,
14647 build_string (vendor ? vendor : ""));
14648 BLOCK_INPUT;
14651 dpyinfo->kboard->next_kboard = all_kboards;
14652 all_kboards = dpyinfo->kboard;
14653 /* Don't let the initial kboard remain current longer than necessary.
14654 That would cause problems if a file loaded on startup tries to
14655 prompt in the mini-buffer. */
14656 if (current_kboard == initial_kboard)
14657 current_kboard = dpyinfo->kboard;
14659 dpyinfo->kboard->reference_count++;
14661 #endif
14663 /* Put this display on the chain. */
14664 dpyinfo->next = x_display_list;
14665 x_display_list = dpyinfo;
14667 /* Put it on x_display_name_list as well, to keep them parallel. */
14668 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14669 x_display_name_list);
14670 dpyinfo->name_list_element = XCAR (x_display_name_list);
14672 dpyinfo->display = dpy;
14674 #if 0
14675 XSetAfterFunction (x_current_display, x_trace_wire);
14676 #endif /* ! 0 */
14678 dpyinfo->x_id_name
14679 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14680 + STRING_BYTES (XSTRING (Vsystem_name))
14681 + 2);
14682 sprintf (dpyinfo->x_id_name, "%s@%s",
14683 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14685 /* Figure out which modifier bits mean what. */
14686 x_find_modifier_meanings (dpyinfo);
14688 /* Get the scroll bar cursor. */
14689 dpyinfo->vertical_scroll_bar_cursor
14690 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14692 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14693 resource_name, EMACS_CLASS);
14694 #ifdef HAVE_XRMSETDATABASE
14695 XrmSetDatabase (dpyinfo->display, xrdb);
14696 #else
14697 dpyinfo->display->db = xrdb;
14698 #endif
14699 /* Put the rdb where we can find it in a way that works on
14700 all versions. */
14701 dpyinfo->xrdb = xrdb;
14703 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14704 DefaultScreen (dpyinfo->display));
14705 select_visual (dpyinfo);
14706 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14707 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14708 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14709 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14710 dpyinfo->grabbed = 0;
14711 dpyinfo->reference_count = 0;
14712 dpyinfo->icon_bitmap_id = -1;
14713 dpyinfo->font_table = NULL;
14714 dpyinfo->n_fonts = 0;
14715 dpyinfo->font_table_size = 0;
14716 dpyinfo->bitmaps = 0;
14717 dpyinfo->bitmaps_size = 0;
14718 dpyinfo->bitmaps_last = 0;
14719 dpyinfo->scratch_cursor_gc = 0;
14720 dpyinfo->mouse_face_mouse_frame = 0;
14721 dpyinfo->mouse_face_deferred_gc = 0;
14722 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14723 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14724 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14725 dpyinfo->mouse_face_window = Qnil;
14726 dpyinfo->mouse_face_overlay = Qnil;
14727 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14728 dpyinfo->mouse_face_defer = 0;
14729 dpyinfo->mouse_face_hidden = 0;
14730 dpyinfo->x_focus_frame = 0;
14731 dpyinfo->x_focus_event_frame = 0;
14732 dpyinfo->x_highlight_frame = 0;
14733 dpyinfo->image_cache = make_image_cache ();
14735 /* See if a private colormap is requested. */
14736 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14738 if (dpyinfo->visual->class == PseudoColor)
14740 Lisp_Object value;
14741 value = display_x_get_resource (dpyinfo,
14742 build_string ("privateColormap"),
14743 build_string ("PrivateColormap"),
14744 Qnil, Qnil);
14745 if (STRINGP (value)
14746 && (!strcmp (XSTRING (value)->data, "true")
14747 || !strcmp (XSTRING (value)->data, "on")))
14748 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14751 else
14752 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14753 dpyinfo->visual, AllocNone);
14756 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14757 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14758 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14759 dpyinfo->resy = pixels * 25.4 / mm;
14760 pixels = DisplayWidth (dpyinfo->display, screen_number);
14761 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14762 dpyinfo->resx = pixels * 25.4 / mm;
14765 dpyinfo->Xatom_wm_protocols
14766 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14767 dpyinfo->Xatom_wm_take_focus
14768 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14769 dpyinfo->Xatom_wm_save_yourself
14770 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14771 dpyinfo->Xatom_wm_delete_window
14772 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14773 dpyinfo->Xatom_wm_change_state
14774 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14775 dpyinfo->Xatom_wm_configure_denied
14776 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14777 dpyinfo->Xatom_wm_window_moved
14778 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14779 dpyinfo->Xatom_editres
14780 = XInternAtom (dpyinfo->display, "Editres", False);
14781 dpyinfo->Xatom_CLIPBOARD
14782 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14783 dpyinfo->Xatom_TIMESTAMP
14784 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14785 dpyinfo->Xatom_TEXT
14786 = XInternAtom (dpyinfo->display, "TEXT", False);
14787 dpyinfo->Xatom_COMPOUND_TEXT
14788 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14789 dpyinfo->Xatom_DELETE
14790 = XInternAtom (dpyinfo->display, "DELETE", False);
14791 dpyinfo->Xatom_MULTIPLE
14792 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14793 dpyinfo->Xatom_INCR
14794 = XInternAtom (dpyinfo->display, "INCR", False);
14795 dpyinfo->Xatom_EMACS_TMP
14796 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14797 dpyinfo->Xatom_TARGETS
14798 = XInternAtom (dpyinfo->display, "TARGETS", False);
14799 dpyinfo->Xatom_NULL
14800 = XInternAtom (dpyinfo->display, "NULL", False);
14801 dpyinfo->Xatom_ATOM_PAIR
14802 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14803 /* For properties of font. */
14804 dpyinfo->Xatom_PIXEL_SIZE
14805 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14806 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14807 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14808 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14809 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14810 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14811 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14813 /* Ghostscript support. */
14814 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14815 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14817 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14818 False);
14820 dpyinfo->cut_buffers_initialized = 0;
14822 connection = ConnectionNumber (dpyinfo->display);
14823 dpyinfo->connection = connection;
14826 char null_bits[1];
14828 null_bits[0] = 0x00;
14830 dpyinfo->null_pixel
14831 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14832 null_bits, 1, 1, (long) 0, (long) 0,
14837 extern int gray_bitmap_width, gray_bitmap_height;
14838 extern char *gray_bitmap_bits;
14839 dpyinfo->gray
14840 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14841 gray_bitmap_bits,
14842 gray_bitmap_width, gray_bitmap_height,
14843 (unsigned long) 1, (unsigned long) 0, 1);
14846 #ifdef HAVE_X_I18N
14847 xim_initialize (dpyinfo, resource_name);
14848 #endif
14850 #ifdef subprocesses
14851 /* This is only needed for distinguishing keyboard and process input. */
14852 if (connection != 0)
14853 add_keyboard_wait_descriptor (connection);
14854 #endif
14856 #ifndef F_SETOWN_BUG
14857 #ifdef F_SETOWN
14858 #ifdef F_SETOWN_SOCK_NEG
14859 /* stdin is a socket here */
14860 fcntl (connection, F_SETOWN, -getpid ());
14861 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14862 fcntl (connection, F_SETOWN, getpid ());
14863 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14864 #endif /* ! defined (F_SETOWN) */
14865 #endif /* F_SETOWN_BUG */
14867 #ifdef SIGIO
14868 if (interrupt_input)
14869 init_sigio (connection);
14870 #endif /* ! defined (SIGIO) */
14872 #ifdef USE_LUCID
14873 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14874 /* Make sure that we have a valid font for dialog boxes
14875 so that Xt does not crash. */
14877 Display *dpy = dpyinfo->display;
14878 XrmValue d, fr, to;
14879 Font font;
14880 int count;
14882 d.addr = (XPointer)&dpy;
14883 d.size = sizeof (Display *);
14884 fr.addr = XtDefaultFont;
14885 fr.size = sizeof (XtDefaultFont);
14886 to.size = sizeof (Font *);
14887 to.addr = (XPointer)&font;
14888 count = x_catch_errors (dpy);
14889 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14890 abort ();
14891 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14892 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14893 x_uncatch_errors (dpy, count);
14895 #endif
14896 #endif
14898 /* See if we should run in synchronous mode. This is useful
14899 for debugging X code. */
14901 Lisp_Object value;
14902 value = display_x_get_resource (dpyinfo,
14903 build_string ("synchronous"),
14904 build_string ("Synchronous"),
14905 Qnil, Qnil);
14906 if (STRINGP (value)
14907 && (!strcmp (XSTRING (value)->data, "true")
14908 || !strcmp (XSTRING (value)->data, "on")))
14909 XSynchronize (dpyinfo->display, True);
14912 UNBLOCK_INPUT;
14914 return dpyinfo;
14917 /* Get rid of display DPYINFO, assuming all frames are already gone,
14918 and without sending any more commands to the X server. */
14920 void
14921 x_delete_display (dpyinfo)
14922 struct x_display_info *dpyinfo;
14924 delete_keyboard_wait_descriptor (dpyinfo->connection);
14926 /* Discard this display from x_display_name_list and x_display_list.
14927 We can't use Fdelq because that can quit. */
14928 if (! NILP (x_display_name_list)
14929 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
14930 x_display_name_list = XCDR (x_display_name_list);
14931 else
14933 Lisp_Object tail;
14935 tail = x_display_name_list;
14936 while (CONSP (tail) && CONSP (XCDR (tail)))
14938 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
14940 XSETCDR (tail, XCDR (XCDR (tail)));
14941 break;
14943 tail = XCDR (tail);
14947 if (next_noop_dpyinfo == dpyinfo)
14948 next_noop_dpyinfo = dpyinfo->next;
14950 if (x_display_list == dpyinfo)
14951 x_display_list = dpyinfo->next;
14952 else
14954 struct x_display_info *tail;
14956 for (tail = x_display_list; tail; tail = tail->next)
14957 if (tail->next == dpyinfo)
14958 tail->next = tail->next->next;
14961 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
14962 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
14963 XrmDestroyDatabase (dpyinfo->xrdb);
14964 #endif
14965 #endif
14966 #ifdef MULTI_KBOARD
14967 if (--dpyinfo->kboard->reference_count == 0)
14968 delete_kboard (dpyinfo->kboard);
14969 #endif
14970 #ifdef HAVE_X_I18N
14971 if (dpyinfo->xim)
14972 xim_close_dpy (dpyinfo);
14973 #endif
14975 xfree (dpyinfo->font_table);
14976 xfree (dpyinfo->x_id_name);
14977 xfree (dpyinfo->color_cells);
14978 xfree (dpyinfo);
14982 /* Set up use of X before we make the first connection. */
14984 static struct redisplay_interface x_redisplay_interface =
14986 x_produce_glyphs,
14987 x_write_glyphs,
14988 x_insert_glyphs,
14989 x_clear_end_of_line,
14990 x_scroll_run,
14991 x_after_update_window_line,
14992 x_update_window_begin,
14993 x_update_window_end,
14994 XTcursor_to,
14995 x_flush,
14996 x_clear_mouse_face,
14997 x_get_glyph_overhangs,
14998 x_fix_overlapping_area
15001 void
15002 x_initialize ()
15004 rif = &x_redisplay_interface;
15006 clear_frame_hook = x_clear_frame;
15007 ins_del_lines_hook = x_ins_del_lines;
15008 delete_glyphs_hook = x_delete_glyphs;
15009 ring_bell_hook = XTring_bell;
15010 reset_terminal_modes_hook = XTreset_terminal_modes;
15011 set_terminal_modes_hook = XTset_terminal_modes;
15012 update_begin_hook = x_update_begin;
15013 update_end_hook = x_update_end;
15014 set_terminal_window_hook = XTset_terminal_window;
15015 read_socket_hook = XTread_socket;
15016 frame_up_to_date_hook = XTframe_up_to_date;
15017 mouse_position_hook = XTmouse_position;
15018 frame_rehighlight_hook = XTframe_rehighlight;
15019 frame_raise_lower_hook = XTframe_raise_lower;
15020 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15021 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15022 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15023 judge_scroll_bars_hook = XTjudge_scroll_bars;
15024 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15026 scroll_region_ok = 1; /* we'll scroll partial frames */
15027 char_ins_del_ok = 1;
15028 line_ins_del_ok = 1; /* we'll just blt 'em */
15029 fast_clear_end_of_line = 1; /* X does this well */
15030 memory_below_frame = 0; /* we don't remember what scrolls
15031 off the bottom */
15032 baud_rate = 19200;
15034 x_noop_count = 0;
15035 last_tool_bar_item = -1;
15036 any_help_event_p = 0;
15038 /* Try to use interrupt input; if we can't, then start polling. */
15039 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15041 #ifdef USE_X_TOOLKIT
15042 XtToolkitInitialize ();
15044 Xt_app_con = XtCreateApplicationContext ();
15046 /* Register a converter from strings to pixels, which uses
15047 Emacs' color allocation infrastructure. */
15048 XtAppSetTypeConverter (Xt_app_con,
15049 XtRString, XtRPixel, cvt_string_to_pixel,
15050 cvt_string_to_pixel_args,
15051 XtNumber (cvt_string_to_pixel_args),
15052 XtCacheByDisplay, cvt_pixel_dtor);
15054 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15056 /* Install an asynchronous timer that processes Xt timeout events
15057 every 0.1s. This is necessary because some widget sets use
15058 timeouts internally, for example the LessTif menu bar, or the
15059 Xaw3d scroll bar. When Xt timouts aren't processed, these
15060 widgets don't behave normally. */
15062 EMACS_TIME interval;
15063 EMACS_SET_SECS_USECS (interval, 0, 100000);
15064 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15066 #endif
15068 #ifdef USE_TOOLKIT_SCROLL_BARS
15069 xaw3d_arrow_scroll = False;
15070 xaw3d_pick_top = True;
15071 #endif
15073 /* Note that there is no real way portable across R3/R4 to get the
15074 original error handler. */
15075 XSetErrorHandler (x_error_handler);
15076 XSetIOErrorHandler (x_io_error_quitter);
15078 /* Disable Window Change signals; they are handled by X events. */
15079 #ifdef SIGWINCH
15080 signal (SIGWINCH, SIG_DFL);
15081 #endif /* ! defined (SIGWINCH) */
15083 signal (SIGPIPE, x_connection_signal);
15085 #ifdef HAVE_X_SM
15086 x_session_initialize ();
15087 #endif
15091 void
15092 syms_of_xterm ()
15094 staticpro (&x_error_message_string);
15095 x_error_message_string = Qnil;
15097 staticpro (&x_display_name_list);
15098 x_display_name_list = Qnil;
15100 staticpro (&last_mouse_scroll_bar);
15101 last_mouse_scroll_bar = Qnil;
15103 staticpro (&Qvendor_specific_keysyms);
15104 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15106 staticpro (&last_mouse_press_frame);
15107 last_mouse_press_frame = Qnil;
15109 help_echo = Qnil;
15110 staticpro (&help_echo);
15111 help_echo_object = Qnil;
15112 staticpro (&help_echo_object);
15113 help_echo_window = Qnil;
15114 staticpro (&help_echo_window);
15115 previous_help_echo = Qnil;
15116 staticpro (&previous_help_echo);
15117 help_echo_pos = -1;
15119 DEFVAR_BOOL ("autoselect-window", &autoselect_window_p,
15120 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15121 autoselect_window_p = 0;
15123 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15124 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15125 For example, if a block cursor is over a tab, it will be drawn as
15126 wide as that tab on the display. */);
15127 x_stretch_cursor_p = 0;
15129 DEFVAR_BOOL ("x-use-underline-position-properties",
15130 &x_use_underline_position_properties,
15131 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15132 nil means ignore them. If you encounter fonts with bogus
15133 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15134 to 4.1, set this to nil. */);
15135 x_use_underline_position_properties = 1;
15137 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15138 doc: /* What X toolkit scroll bars Emacs uses.
15139 A value of nil means Emacs doesn't use X toolkit scroll bars.
15140 Otherwise, value is a symbol describing the X toolkit. */);
15141 #ifdef USE_TOOLKIT_SCROLL_BARS
15142 #ifdef USE_MOTIF
15143 Vx_toolkit_scroll_bars = intern ("motif");
15144 #elif defined HAVE_XAW3D
15145 Vx_toolkit_scroll_bars = intern ("xaw3d");
15146 #else
15147 Vx_toolkit_scroll_bars = intern ("xaw");
15148 #endif
15149 #else
15150 Vx_toolkit_scroll_bars = Qnil;
15151 #endif
15153 staticpro (&last_mouse_motion_frame);
15154 last_mouse_motion_frame = Qnil;
15156 Qmodifier_value = intern ("modifier-value");
15157 Qalt = intern ("alt");
15158 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15159 Qhyper = intern ("hyper");
15160 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15161 Qmeta = intern ("meta");
15162 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15163 Qsuper = intern ("super");
15164 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15166 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15167 doc: /* Which keys Emacs uses for the alt modifier.
15168 This should be one of the symbols `alt', `hyper', `meta', `super'.
15169 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15170 is nil, which is the same as `alt'. */);
15171 Vx_alt_keysym = Qnil;
15173 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15174 doc: /* Which keys Emacs uses for the hyper modifier.
15175 This should be one of the symbols `alt', `hyper', `meta', `super'.
15176 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15177 default is nil, which is the same as `hyper'. */);
15178 Vx_hyper_keysym = Qnil;
15180 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15181 doc: /* Which keys Emacs uses for the meta modifier.
15182 This should be one of the symbols `alt', `hyper', `meta', `super'.
15183 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15184 default is nil, which is the same as `meta'. */);
15185 Vx_meta_keysym = Qnil;
15187 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15188 doc: /* Which keys Emacs uses for the super modifier.
15189 This should be one of the symbols `alt', `hyper', `meta', `super'.
15190 For example, `super' means use the Super_L and Super_R keysyms. The
15191 default is nil, which is the same as `super'. */);
15192 Vx_super_keysym = Qnil;
15196 #endif /* HAVE_X_WINDOWS */