(Vx_keysym_table): New.
[emacs.git] / src / xterm.c
blob2c9ae41764b66bf13cc2529b9838507230a2c369
1 /* X Communication module for terminals which understand the X protocol.
2 Copyright (C) 1989, 93, 94, 95, 96, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* New display code by Gerd Moellmann <gerd@gnu.org>. */
23 /* Xt features made by Fred Pierresteguy. */
25 #include <config.h>
27 /* On 4.3 these lose if they come after xterm.h. */
28 /* Putting these at the beginning seems to be standard for other .c files. */
29 #include <signal.h>
31 #include <stdio.h>
33 #ifdef HAVE_X_WINDOWS
35 #include "lisp.h"
36 #include "blockinput.h"
38 /* Need syssignal.h for various externs and definitions that may be required
39 by some configurations for calls to signal later in this source file. */
40 #include "syssignal.h"
42 /* This may include sys/types.h, and that somehow loses
43 if this is not done before the other system files. */
44 #include "xterm.h"
45 #include <X11/cursorfont.h>
47 /* Load sys/types.h if not already loaded.
48 In some systems loading it twice is suicidal. */
49 #ifndef makedev
50 #include <sys/types.h>
51 #endif /* makedev */
53 #ifdef BSD_SYSTEM
54 #include <sys/ioctl.h>
55 #endif /* ! defined (BSD_SYSTEM) */
57 #include "systty.h"
58 #include "systime.h"
60 #ifndef INCLUDED_FCNTL
61 #include <fcntl.h>
62 #endif
63 #include <ctype.h>
64 #include <errno.h>
65 #include <setjmp.h>
66 #include <sys/stat.h>
67 /* Caused redefinition of DBL_DIG on Netbsd; seems not to be needed. */
68 /* #include <sys/param.h> */
70 #include "charset.h"
71 #include "character.h"
72 #include "coding.h"
73 #include "ccl.h"
74 #include "frame.h"
75 #include "dispextern.h"
76 #include "fontset.h"
77 #include "termhooks.h"
78 #include "termopts.h"
79 #include "termchar.h"
80 #include "gnu.h"
81 #include "disptab.h"
82 #include "buffer.h"
83 #include "window.h"
84 #include "keyboard.h"
85 #include "intervals.h"
86 #include "process.h"
87 #include "atimer.h"
88 #include "keymap.h"
90 #ifdef USE_X_TOOLKIT
91 #include <X11/Shell.h>
92 #endif
94 #ifdef HAVE_SYS_TIME_H
95 #include <sys/time.h>
96 #endif
97 #ifdef HAVE_UNISTD_H
98 #include <unistd.h>
99 #endif
101 #ifdef USE_LUCID
102 extern int xlwmenu_window_p P_ ((Widget w, Window window));
103 extern void xlwmenu_redisplay P_ ((Widget));
104 #endif
106 #ifdef USE_X_TOOLKIT
108 extern void free_frame_menubar P_ ((struct frame *));
109 extern struct frame *x_menubar_window_to_frame P_ ((struct x_display_info *,
110 int));
112 #if (XtSpecificationRelease >= 5) && !defined(NO_EDITRES)
113 #define HACK_EDITRES
114 extern void _XEditResCheckMessages ();
115 #endif /* not NO_EDITRES */
117 /* Include toolkit specific headers for the scroll bar widget. */
119 #ifdef USE_TOOLKIT_SCROLL_BARS
120 #if defined USE_MOTIF
121 #include <Xm/Xm.h> /* for LESSTIF_VERSION */
122 #include <Xm/ScrollBar.h>
123 #else /* !USE_MOTIF i.e. use Xaw */
125 #ifdef HAVE_XAW3D
126 #include <X11/Xaw3d/Simple.h>
127 #include <X11/Xaw3d/Scrollbar.h>
128 #define ARROW_SCROLLBAR
129 #include <X11/Xaw3d/ScrollbarP.h>
130 #else /* !HAVE_XAW3D */
131 #include <X11/Xaw/Simple.h>
132 #include <X11/Xaw/Scrollbar.h>
133 #endif /* !HAVE_XAW3D */
134 #ifndef XtNpickTop
135 #define XtNpickTop "pickTop"
136 #endif /* !XtNpickTop */
137 #endif /* !USE_MOTIF */
138 #endif /* USE_TOOLKIT_SCROLL_BARS */
140 #endif /* USE_X_TOOLKIT */
142 #ifndef USE_X_TOOLKIT
143 #define x_any_window_to_frame x_window_to_frame
144 #define x_top_window_to_frame x_window_to_frame
145 #endif
147 #ifdef USE_X_TOOLKIT
148 #include "widget.h"
149 #ifndef XtNinitialState
150 #define XtNinitialState "initialState"
151 #endif
152 #endif
154 #ifdef HAVE_X11R6
155 /* This isn't prototyped in OSF 5.0 or or XFree 4.1. */
156 extern char * XSetIMValues P_ ((XIM, ...));
157 #endif
159 #define abs(x) ((x) < 0 ? -(x) : (x))
161 #define BETWEEN(X, LOWER, UPPER) ((X) >= (LOWER) && (X) < (UPPER))
164 /* Fringe bitmaps. */
166 enum fringe_bitmap_type
168 NO_FRINGE_BITMAP,
169 LEFT_TRUNCATION_BITMAP,
170 RIGHT_TRUNCATION_BITMAP,
171 OVERLAY_ARROW_BITMAP,
172 CONTINUED_LINE_BITMAP,
173 CONTINUATION_LINE_BITMAP,
174 ZV_LINE_BITMAP
177 /* Bitmap drawn to indicate lines not displaying text if
178 `indicate-empty-lines' is non-nil. */
180 #define zv_width 8
181 #define zv_height 72
182 #define zv_period 3
183 static unsigned char zv_bits[] = {
184 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
185 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
186 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
187 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
188 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
189 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
190 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
191 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};
193 /* An arrow like this: `<-'. */
195 #define left_width 8
196 #define left_height 8
197 static unsigned char left_bits[] = {
198 0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};
200 /* Right truncation arrow bitmap `->'. */
202 #define right_width 8
203 #define right_height 8
204 static unsigned char right_bits[] = {
205 0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};
207 /* Marker for continued lines. */
209 #define continued_width 8
210 #define continued_height 8
211 static unsigned char continued_bits[] = {
212 0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};
214 /* Marker for continuation lines. */
216 #define continuation_width 8
217 #define continuation_height 8
218 static unsigned char continuation_bits[] = {
219 0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
221 /* Overlay arrow bitmap. */
223 #if 0
224 /* A bomb. */
225 #define ov_width 8
226 #define ov_height 8
227 static unsigned char ov_bits[] = {
228 0x30, 0x08, 0x3c, 0x7e, 0x7a, 0x7a, 0x62, 0x3c};
229 #else
230 /* A triangular arrow. */
231 #define ov_width 8
232 #define ov_height 8
233 static unsigned char ov_bits[] = {
234 0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
236 #endif
238 extern Lisp_Object Qhelp_echo;
240 /* Default to using XIM if available. */
241 int use_xim = 1;
243 /* Non-nil means Emacs uses toolkit scroll bars. */
245 Lisp_Object Vx_toolkit_scroll_bars;
247 /* If a string, XTread_socket generates an event to display that string.
248 (The display is done in read_char.) */
250 static Lisp_Object help_echo;
251 static Lisp_Object help_echo_window;
252 static Lisp_Object help_echo_object;
253 static int help_echo_pos;
255 /* Temporary variable for XTread_socket. */
257 static Lisp_Object previous_help_echo;
259 /* Non-zero means that a HELP_EVENT has been generated since Emacs
260 start. */
262 static int any_help_event_p;
264 /* Non-zero means autoselect window with the mouse cursor. */
266 int x_autoselect_window_p;
268 /* Non-zero means draw block and hollow cursor as wide as the glyph
269 under it. For example, if a block cursor is over a tab, it will be
270 drawn as wide as that tab on the display. */
272 int x_stretch_cursor_p;
274 /* Non-zero means make use of UNDERLINE_POSITION font properties. */
276 int x_use_underline_position_properties;
278 /* This is a chain of structures for all the X displays currently in
279 use. */
281 struct x_display_info *x_display_list;
283 /* This is a list of cons cells, each of the form (NAME
284 . FONT-LIST-CACHE), one for each element of x_display_list and in
285 the same order. NAME is the name of the frame. FONT-LIST-CACHE
286 records previous values returned by x-list-fonts. */
288 Lisp_Object x_display_name_list;
290 /* Frame being updated by update_frame. This is declared in term.c.
291 This is set by update_begin and looked at by all the XT functions.
292 It is zero while not inside an update. In that case, the XT
293 functions assume that `selected_frame' is the frame to apply to. */
295 extern struct frame *updating_frame;
297 /* This is a frame waiting to be auto-raised, within XTread_socket. */
299 struct frame *pending_autoraise_frame;
301 #ifdef USE_X_TOOLKIT
302 /* The application context for Xt use. */
303 XtAppContext Xt_app_con;
304 static String Xt_default_resources[] = {0};
305 #endif /* USE_X_TOOLKIT */
307 /* Nominal cursor position -- where to draw output.
308 HPOS and VPOS are window relative glyph matrix coordinates.
309 X and Y are window relative pixel coordinates. */
311 struct cursor_pos output_cursor;
313 /* Non-zero means user is interacting with a toolkit scroll bar. */
315 static int toolkit_scroll_bar_interaction;
317 /* Mouse movement.
319 Formerly, we used PointerMotionHintMask (in standard_event_mask)
320 so that we would have to call XQueryPointer after each MotionNotify
321 event to ask for another such event. However, this made mouse tracking
322 slow, and there was a bug that made it eventually stop.
324 Simply asking for MotionNotify all the time seems to work better.
326 In order to avoid asking for motion events and then throwing most
327 of them away or busy-polling the server for mouse positions, we ask
328 the server for pointer motion hints. This means that we get only
329 one event per group of mouse movements. "Groups" are delimited by
330 other kinds of events (focus changes and button clicks, for
331 example), or by XQueryPointer calls; when one of these happens, we
332 get another MotionNotify event the next time the mouse moves. This
333 is at least as efficient as getting motion events when mouse
334 tracking is on, and I suspect only negligibly worse when tracking
335 is off. */
337 /* Where the mouse was last time we reported a mouse event. */
339 FRAME_PTR last_mouse_frame;
340 static XRectangle last_mouse_glyph;
341 static Lisp_Object last_mouse_press_frame;
343 /* The scroll bar in which the last X motion event occurred.
345 If the last X motion event occurred in a scroll bar, we set this so
346 XTmouse_position can know whether to report a scroll bar motion or
347 an ordinary motion.
349 If the last X motion event didn't occur in a scroll bar, we set
350 this to Qnil, to tell XTmouse_position to return an ordinary motion
351 event. */
353 static Lisp_Object last_mouse_scroll_bar;
355 /* This is a hack. We would really prefer that XTmouse_position would
356 return the time associated with the position it returns, but there
357 doesn't seem to be any way to wrest the time-stamp from the server
358 along with the position query. So, we just keep track of the time
359 of the last movement we received, and return that in hopes that
360 it's somewhat accurate. */
362 static Time last_mouse_movement_time;
364 /* Incremented by XTread_socket whenever it really tries to read
365 events. */
367 #ifdef __STDC__
368 static int volatile input_signal_count;
369 #else
370 static int input_signal_count;
371 #endif
373 /* Used locally within XTread_socket. */
375 static int x_noop_count;
377 /* Initial values of argv and argc. */
379 extern char **initial_argv;
380 extern int initial_argc;
382 extern Lisp_Object Vcommand_line_args, Vsystem_name;
384 /* Tells if a window manager is present or not. */
386 extern Lisp_Object Vx_no_window_manager;
388 extern Lisp_Object Qface, Qmouse_face, Qeql;
390 extern int errno;
392 /* A mask of extra modifier bits to put into every keyboard char. */
394 extern int extra_keyboard_modifiers;
396 /* The keysyms to use for the various modifiers. */
398 Lisp_Object Vx_alt_keysym, Vx_hyper_keysym, Vx_meta_keysym, Vx_super_keysym;
399 Lisp_Object Vx_keysym_table;
400 static Lisp_Object Qalt, Qhyper, Qmeta, Qsuper, Qmodifier_value;
402 static Lisp_Object Qvendor_specific_keysyms;
404 extern XrmDatabase x_load_resources P_ ((Display *, char *, char *, char *));
405 extern Lisp_Object x_icon_type P_ ((struct frame *));
408 /* Enumeration for overriding/changing the face to use for drawing
409 glyphs in x_draw_glyphs. */
411 enum draw_glyphs_face
413 DRAW_NORMAL_TEXT,
414 DRAW_INVERSE_VIDEO,
415 DRAW_CURSOR,
416 DRAW_MOUSE_FACE,
417 DRAW_IMAGE_RAISED,
418 DRAW_IMAGE_SUNKEN
421 static int cursor_in_mouse_face_p P_ ((struct window *));
422 static int clear_mouse_face P_ ((struct x_display_info *));
423 static int x_alloc_nearest_color_1 P_ ((Display *, Colormap, XColor *));
424 static void x_set_window_size_1 P_ ((struct frame *, int, int, int));
425 static const XColor *x_color_cells P_ ((Display *, int *));
426 static void x_update_window_end P_ ((struct window *, int, int));
427 static void frame_to_window_pixel_xy P_ ((struct window *, int *, int *));
428 void x_delete_display P_ ((struct x_display_info *));
429 static unsigned int x_x_to_emacs_modifiers P_ ((struct x_display_info *,
430 unsigned));
431 static int fast_find_position P_ ((struct window *, int, int *, int *,
432 int *, int *, Lisp_Object));
433 static int fast_find_string_pos P_ ((struct window *, int, Lisp_Object,
434 int *, int *, int *, int *, int));
435 static void set_output_cursor P_ ((struct cursor_pos *));
436 static struct glyph *x_y_to_hpos_vpos P_ ((struct window *, int, int,
437 int *, int *, int *, int));
438 static void note_mode_line_highlight P_ ((struct window *, int, int));
439 static void note_mouse_highlight P_ ((struct frame *, int, int));
440 static void note_tool_bar_highlight P_ ((struct frame *f, int, int));
441 static void x_handle_tool_bar_click P_ ((struct frame *, XButtonEvent *));
442 static void show_mouse_face P_ ((struct x_display_info *,
443 enum draw_glyphs_face));
444 static int x_io_error_quitter P_ ((Display *));
445 int x_catch_errors P_ ((Display *));
446 void x_uncatch_errors P_ ((Display *, int));
447 void x_lower_frame P_ ((struct frame *));
448 void x_scroll_bar_clear P_ ((struct frame *));
449 int x_had_errors_p P_ ((Display *));
450 void x_wm_set_size_hint P_ ((struct frame *, long, int));
451 void x_raise_frame P_ ((struct frame *));
452 void x_set_window_size P_ ((struct frame *, int, int, int));
453 void x_wm_set_window_state P_ ((struct frame *, int));
454 void x_wm_set_icon_pixmap P_ ((struct frame *, int));
455 void x_initialize P_ ((void));
456 static void x_font_min_bounds P_ ((XFontStruct *, int *, int *));
457 static int x_compute_min_glyph_bounds P_ ((struct frame *));
458 static void x_draw_phys_cursor_glyph P_ ((struct window *,
459 struct glyph_row *,
460 enum draw_glyphs_face));
461 static void x_update_end P_ ((struct frame *));
462 static void XTframe_up_to_date P_ ((struct frame *));
463 static void XTset_terminal_modes P_ ((void));
464 static void XTreset_terminal_modes P_ ((void));
465 static void XTcursor_to P_ ((int, int, int, int));
466 static void x_write_glyphs P_ ((struct glyph *, int));
467 static void x_clear_end_of_line P_ ((int));
468 static void x_clear_frame P_ ((void));
469 static void x_clear_cursor P_ ((struct window *));
470 static void frame_highlight P_ ((struct frame *));
471 static void frame_unhighlight P_ ((struct frame *));
472 static void x_new_focus_frame P_ ((struct x_display_info *, struct frame *));
473 static void XTframe_rehighlight P_ ((struct frame *));
474 static void x_frame_rehighlight P_ ((struct x_display_info *));
475 static void x_draw_hollow_cursor P_ ((struct window *, struct glyph_row *));
476 static void x_draw_bar_cursor P_ ((struct window *, struct glyph_row *, int));
477 static int x_intersect_rectangles P_ ((XRectangle *, XRectangle *,
478 XRectangle *));
479 static void expose_frame P_ ((struct frame *, int, int, int, int));
480 static int expose_window_tree P_ ((struct window *, XRectangle *));
481 static int expose_window P_ ((struct window *, XRectangle *));
482 static void expose_area P_ ((struct window *, struct glyph_row *,
483 XRectangle *, enum glyph_row_area));
484 static int expose_line P_ ((struct window *, struct glyph_row *,
485 XRectangle *));
486 static void x_update_cursor_in_window_tree P_ ((struct window *, int));
487 static void x_update_window_cursor P_ ((struct window *, int));
488 static void x_erase_phys_cursor P_ ((struct window *));
489 void x_display_and_set_cursor P_ ((struct window *, int, int, int, int, int));
490 static void x_draw_fringe_bitmap P_ ((struct window *, struct glyph_row *,
491 enum fringe_bitmap_type, int left_p));
493 static void x_clip_to_row P_ ((struct window *, struct glyph_row *,
494 GC, int));
495 static int x_phys_cursor_in_rect_p P_ ((struct window *, XRectangle *));
496 static void x_draw_row_fringe_bitmaps P_ ((struct window *, struct glyph_row *));
497 static void notice_overwritten_cursor P_ ((struct window *, enum glyph_row_area,
498 int, int, int, int));
499 static void x_flush P_ ((struct frame *f));
500 static void x_update_begin P_ ((struct frame *));
501 static void x_update_window_begin P_ ((struct window *));
502 static void x_draw_vertical_border P_ ((struct window *));
503 static void x_after_update_window_line P_ ((struct glyph_row *));
504 static INLINE void take_vertical_position_into_account P_ ((struct it *));
505 static void x_produce_stretch_glyph P_ ((struct it *));
506 static struct scroll_bar *x_window_to_scroll_bar P_ ((Window));
507 static void x_scroll_bar_report_motion P_ ((struct frame **, Lisp_Object *,
508 enum scroll_bar_part *,
509 Lisp_Object *, Lisp_Object *,
510 unsigned long *));
511 static void x_check_fullscreen P_ ((struct frame *));
512 static void x_check_fullscreen_move P_ ((struct frame *));
514 /* Flush display of frame F, or of all frames if F is null. */
516 static void
517 x_flush (f)
518 struct frame *f;
520 BLOCK_INPUT;
521 if (f == NULL)
523 Lisp_Object rest, frame;
524 FOR_EACH_FRAME (rest, frame)
525 x_flush (XFRAME (frame));
527 else if (FRAME_X_P (f))
528 XFlush (FRAME_X_DISPLAY (f));
529 UNBLOCK_INPUT;
533 /* Remove calls to XFlush by defining XFlush to an empty replacement.
534 Calls to XFlush should be unnecessary because the X output buffer
535 is flushed automatically as needed by calls to XPending,
536 XNextEvent, or XWindowEvent according to the XFlush man page.
537 XTread_socket calls XPending. Removing XFlush improves
538 performance. */
540 #define XFlush(DISPLAY) (void) 0
543 /***********************************************************************
544 Debugging
545 ***********************************************************************/
547 #if 0
549 /* This is a function useful for recording debugging information about
550 the sequence of occurrences in this file. */
552 struct record
554 char *locus;
555 int type;
558 struct record event_record[100];
560 int event_record_index;
562 record_event (locus, type)
563 char *locus;
564 int type;
566 if (event_record_index == sizeof (event_record) / sizeof (struct record))
567 event_record_index = 0;
569 event_record[event_record_index].locus = locus;
570 event_record[event_record_index].type = type;
571 event_record_index++;
574 #endif /* 0 */
578 /* Return the struct x_display_info corresponding to DPY. */
580 struct x_display_info *
581 x_display_info_for_display (dpy)
582 Display *dpy;
584 struct x_display_info *dpyinfo;
586 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
587 if (dpyinfo->display == dpy)
588 return dpyinfo;
590 return 0;
595 /***********************************************************************
596 Starting and ending an update
597 ***********************************************************************/
599 /* Start an update of frame F. This function is installed as a hook
600 for update_begin, i.e. it is called when update_begin is called.
601 This function is called prior to calls to x_update_window_begin for
602 each window being updated. Currently, there is nothing to do here
603 because all interesting stuff is done on a window basis. */
605 static void
606 x_update_begin (f)
607 struct frame *f;
609 /* Nothing to do. */
613 /* Start update of window W. Set the global variable updated_window
614 to the window being updated and set output_cursor to the cursor
615 position of W. */
617 static void
618 x_update_window_begin (w)
619 struct window *w;
621 struct frame *f = XFRAME (WINDOW_FRAME (w));
622 struct x_display_info *display_info = FRAME_X_DISPLAY_INFO (f);
624 updated_window = w;
625 set_output_cursor (&w->cursor);
627 BLOCK_INPUT;
629 if (f == display_info->mouse_face_mouse_frame)
631 /* Don't do highlighting for mouse motion during the update. */
632 display_info->mouse_face_defer = 1;
634 /* If F needs to be redrawn, simply forget about any prior mouse
635 highlighting. */
636 if (FRAME_GARBAGED_P (f))
637 display_info->mouse_face_window = Qnil;
639 #if 0 /* Rows in a current matrix containing glyphs in mouse-face have
640 their mouse_face_p flag set, which means that they are always
641 unequal to rows in a desired matrix which never have that
642 flag set. So, rows containing mouse-face glyphs are never
643 scrolled, and we don't have to switch the mouse highlight off
644 here to prevent it from being scrolled. */
646 /* Can we tell that this update does not affect the window
647 where the mouse highlight is? If so, no need to turn off.
648 Likewise, don't do anything if the frame is garbaged;
649 in that case, the frame's current matrix that we would use
650 is all wrong, and we will redisplay that line anyway. */
651 if (!NILP (display_info->mouse_face_window)
652 && w == XWINDOW (display_info->mouse_face_window))
654 int i;
656 for (i = 0; i < w->desired_matrix->nrows; ++i)
657 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
658 break;
660 if (i < w->desired_matrix->nrows)
661 clear_mouse_face (display_info);
663 #endif /* 0 */
666 UNBLOCK_INPUT;
670 /* Draw a vertical window border to the right of window W if W doesn't
671 have vertical scroll bars. */
673 static void
674 x_draw_vertical_border (w)
675 struct window *w;
677 struct frame *f = XFRAME (WINDOW_FRAME (w));
679 /* Redraw borders between horizontally adjacent windows. Don't
680 do it for frames with vertical scroll bars because either the
681 right scroll bar of a window, or the left scroll bar of its
682 neighbor will suffice as a border. */
683 if (!WINDOW_RIGHTMOST_P (w)
684 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
686 int x0, x1, y0, y1;
688 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
689 x1 += FRAME_X_RIGHT_FRINGE_WIDTH (f);
690 y1 -= 1;
692 XDrawLine (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
693 f->output_data.x->normal_gc, x1, y0, x1, y1);
698 /* End update of window W (which is equal to updated_window).
700 Draw vertical borders between horizontally adjacent windows, and
701 display W's cursor if CURSOR_ON_P is non-zero.
703 MOUSE_FACE_OVERWRITTEN_P non-zero means that some row containing
704 glyphs in mouse-face were overwritten. In that case we have to
705 make sure that the mouse-highlight is properly redrawn.
707 W may be a menu bar pseudo-window in case we don't have X toolkit
708 support. Such windows don't have a cursor, so don't display it
709 here. */
711 static void
712 x_update_window_end (w, cursor_on_p, mouse_face_overwritten_p)
713 struct window *w;
714 int cursor_on_p, mouse_face_overwritten_p;
716 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
718 if (!w->pseudo_window_p)
720 BLOCK_INPUT;
722 if (cursor_on_p)
723 x_display_and_set_cursor (w, 1, output_cursor.hpos,
724 output_cursor.vpos,
725 output_cursor.x, output_cursor.y);
727 x_draw_vertical_border (w);
728 UNBLOCK_INPUT;
731 /* If a row with mouse-face was overwritten, arrange for
732 XTframe_up_to_date to redisplay the mouse highlight. */
733 if (mouse_face_overwritten_p)
735 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
736 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
737 dpyinfo->mouse_face_window = Qnil;
740 updated_window = NULL;
744 /* End update of frame F. This function is installed as a hook in
745 update_end. */
747 static void
748 x_update_end (f)
749 struct frame *f;
751 /* Mouse highlight may be displayed again. */
752 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
754 BLOCK_INPUT;
755 XFlush (FRAME_X_DISPLAY (f));
756 UNBLOCK_INPUT;
760 /* This function is called from various places in xdisp.c whenever a
761 complete update has been performed. The global variable
762 updated_window is not available here. */
764 static void
765 XTframe_up_to_date (f)
766 struct frame *f;
768 if (FRAME_X_P (f))
770 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
772 if (dpyinfo->mouse_face_deferred_gc
773 || f == dpyinfo->mouse_face_mouse_frame)
775 BLOCK_INPUT;
776 if (dpyinfo->mouse_face_mouse_frame)
777 note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
778 dpyinfo->mouse_face_mouse_x,
779 dpyinfo->mouse_face_mouse_y);
780 dpyinfo->mouse_face_deferred_gc = 0;
781 UNBLOCK_INPUT;
787 /* Draw truncation mark bitmaps, continuation mark bitmaps, overlay
788 arrow bitmaps, or clear the fringes if no bitmaps are required
789 before DESIRED_ROW is made current. The window being updated is
790 found in updated_window. This function It is called from
791 update_window_line only if it is known that there are differences
792 between bitmaps to be drawn between current row and DESIRED_ROW. */
794 static void
795 x_after_update_window_line (desired_row)
796 struct glyph_row *desired_row;
798 struct window *w = updated_window;
799 struct frame *f;
800 int width, height;
802 xassert (w);
804 if (!desired_row->mode_line_p && !w->pseudo_window_p)
806 BLOCK_INPUT;
807 x_draw_row_fringe_bitmaps (w, desired_row);
808 UNBLOCK_INPUT;
811 /* When a window has disappeared, make sure that no rest of
812 full-width rows stays visible in the internal border. Could
813 check here if updated_window is the leftmost/rightmost window,
814 but I guess it's not worth doing since vertically split windows
815 are almost never used, internal border is rarely set, and the
816 overhead is very small. */
817 if (windows_or_buffers_changed
818 && desired_row->full_width_p
819 && (f = XFRAME (w->frame),
820 width = FRAME_INTERNAL_BORDER_WIDTH (f),
821 width != 0)
822 && (height = desired_row->visible_height,
823 height > 0))
825 int y = WINDOW_TO_FRAME_PIXEL_Y (w, max (0, desired_row->y));
827 /* Internal border is drawn below the tool bar. */
828 if (WINDOWP (f->tool_bar_window)
829 && w == XWINDOW (f->tool_bar_window))
830 y -= width;
832 BLOCK_INPUT;
833 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
834 0, y, width, height, False);
835 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
836 f->output_data.x->pixel_width - width,
837 y, width, height, False);
838 UNBLOCK_INPUT;
843 /* Draw the bitmap WHICH in one of the left or right fringes of
844 window W. ROW is the glyph row for which to display the bitmap; it
845 determines the vertical position at which the bitmap has to be
846 drawn. */
848 static void
849 x_draw_fringe_bitmap (w, row, which, left_p)
850 struct window *w;
851 struct glyph_row *row;
852 enum fringe_bitmap_type which;
853 int left_p;
855 struct frame *f = XFRAME (WINDOW_FRAME (w));
856 Display *display = FRAME_X_DISPLAY (f);
857 Window window = FRAME_X_WINDOW (f);
858 int x, y, wd, h, dy;
859 int b1, b2;
860 unsigned char *bits;
861 Pixmap pixmap;
862 GC gc = f->output_data.x->normal_gc;
863 struct face *face;
864 int depth = DefaultDepthOfScreen (FRAME_X_SCREEN (f));
866 /* Must clip because of partially visible lines. */
867 x_clip_to_row (w, row, gc, 1);
869 /* Convert row to frame coordinates. */
870 y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
872 switch (which)
874 case NO_FRINGE_BITMAP:
875 wd = 0;
876 h = 0;
877 break;
879 case LEFT_TRUNCATION_BITMAP:
880 wd = left_width;
881 h = left_height;
882 bits = left_bits;
883 break;
885 case OVERLAY_ARROW_BITMAP:
886 wd = ov_width;
887 h = ov_height;
888 bits = ov_bits;
889 break;
891 case RIGHT_TRUNCATION_BITMAP:
892 wd = right_width;
893 h = right_height;
894 bits = right_bits;
895 break;
897 case CONTINUED_LINE_BITMAP:
898 wd = continued_width;
899 h = continued_height;
900 bits = continued_bits;
901 break;
903 case CONTINUATION_LINE_BITMAP:
904 wd = continuation_width;
905 h = continuation_height;
906 bits = continuation_bits;
907 break;
909 case ZV_LINE_BITMAP:
910 wd = zv_width;
911 h = zv_height - (y % zv_period);
912 bits = zv_bits + (y % zv_period);
913 break;
915 default:
916 abort ();
919 /* Clip bitmap if too high. */
920 if (h > row->height)
921 h = row->height;
923 /* Set dy to the offset in the row to start drawing the bitmap. */
924 dy = (row->height - h) / 2;
926 face = FACE_FROM_ID (f, FRINGE_FACE_ID);
927 PREPARE_FACE_FOR_DISPLAY (f, face);
929 /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
930 the fringe. */
931 b1 = b2 = -1;
932 if (left_p)
934 if (wd > FRAME_X_LEFT_FRINGE_WIDTH (f))
935 wd = FRAME_X_LEFT_FRINGE_WIDTH (f);
936 x = (WINDOW_TO_FRAME_PIXEL_X (w, 0)
937 - wd
938 - (FRAME_X_LEFT_FRINGE_WIDTH (f) - wd) / 2);
939 if (wd < FRAME_X_LEFT_FRINGE_WIDTH (f) || row->height > h)
941 /* If W has a vertical border to its left, don't draw over it. */
942 int border = ((XFASTINT (w->left) > 0
943 && !FRAME_HAS_VERTICAL_SCROLL_BARS (f))
944 ? 1 : 0);
945 b1 = (window_box_left (w, -1)
946 - FRAME_X_LEFT_FRINGE_WIDTH (f)
947 + border);
948 b2 = (FRAME_X_LEFT_FRINGE_WIDTH (f) - border);
951 else
953 if (wd > FRAME_X_RIGHT_FRINGE_WIDTH (f))
954 wd = FRAME_X_RIGHT_FRINGE_WIDTH (f);
955 x = (window_box_right (w, -1)
956 + (FRAME_X_RIGHT_FRINGE_WIDTH (f) - wd) / 2);
957 /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
958 the fringe. */
959 if (wd < FRAME_X_RIGHT_FRINGE_WIDTH (f) || row->height > h)
961 b1 = window_box_right (w, -1);
962 b2 = FRAME_X_RIGHT_FRINGE_WIDTH (f);
966 if (b1 >= 0)
968 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
970 /* In case the same realized face is used for fringes and
971 for something displayed in the text (e.g. face `region' on
972 mono-displays, the fill style may have been changed to
973 FillSolid in x_draw_glyph_string_background. */
974 if (face->stipple)
975 XSetFillStyle (display, face->gc, FillOpaqueStippled);
976 else
977 XSetForeground (display, face->gc, face->background);
979 XFillRectangle (display, window, face->gc,
981 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
982 row->y)),
984 row->visible_height);
985 if (!face->stipple)
986 XSetForeground (display, face->gc, face->foreground);
989 if (which != NO_FRINGE_BITMAP)
991 /* Draw the bitmap. I believe these small pixmaps can be cached
992 by the server. */
993 pixmap = XCreatePixmapFromBitmapData (display, window, bits, wd, h,
994 face->foreground,
995 face->background, depth);
996 XCopyArea (display, pixmap, window, gc, 0, 0, wd, h, x, y + dy);
997 XFreePixmap (display, pixmap);
1000 XSetClipMask (display, gc, None);
1004 /* Draw fringe bitmaps for glyph row ROW on window W. Call this
1005 function with input blocked. */
1007 static void
1008 x_draw_row_fringe_bitmaps (w, row)
1009 struct window *w;
1010 struct glyph_row *row;
1012 struct frame *f = XFRAME (w->frame);
1013 enum fringe_bitmap_type bitmap;
1015 xassert (interrupt_input_blocked);
1017 /* If row is completely invisible, because of vscrolling, we
1018 don't have to draw anything. */
1019 if (row->visible_height <= 0)
1020 return;
1022 if (FRAME_X_LEFT_FRINGE_WIDTH (f) != 0)
1024 /* Decide which bitmap to draw in the left fringe. */
1025 if (row->overlay_arrow_p)
1026 bitmap = OVERLAY_ARROW_BITMAP;
1027 else if (row->truncated_on_left_p)
1028 bitmap = LEFT_TRUNCATION_BITMAP;
1029 else if (MATRIX_ROW_CONTINUATION_LINE_P (row))
1030 bitmap = CONTINUATION_LINE_BITMAP;
1031 else if (row->indicate_empty_line_p)
1032 bitmap = ZV_LINE_BITMAP;
1033 else
1034 bitmap = NO_FRINGE_BITMAP;
1036 x_draw_fringe_bitmap (w, row, bitmap, 1);
1039 if (FRAME_X_RIGHT_FRINGE_WIDTH (f) != 0)
1041 /* Decide which bitmap to draw in the right fringe. */
1042 if (row->truncated_on_right_p)
1043 bitmap = RIGHT_TRUNCATION_BITMAP;
1044 else if (row->continued_p)
1045 bitmap = CONTINUED_LINE_BITMAP;
1046 else if (row->indicate_empty_line_p && FRAME_X_LEFT_FRINGE_WIDTH (f) == 0)
1047 bitmap = ZV_LINE_BITMAP;
1048 else
1049 bitmap = NO_FRINGE_BITMAP;
1051 x_draw_fringe_bitmap (w, row, bitmap, 0);
1057 /* This is called when starting Emacs and when restarting after
1058 suspend. When starting Emacs, no X window is mapped. And nothing
1059 must be done to Emacs's own window if it is suspended (though that
1060 rarely happens). */
1062 static void
1063 XTset_terminal_modes ()
1067 /* This is called when exiting or suspending Emacs. Exiting will make
1068 the X-windows go away, and suspending requires no action. */
1070 static void
1071 XTreset_terminal_modes ()
1077 /***********************************************************************
1078 Output Cursor
1079 ***********************************************************************/
1081 /* Set the global variable output_cursor to CURSOR. All cursor
1082 positions are relative to updated_window. */
1084 static void
1085 set_output_cursor (cursor)
1086 struct cursor_pos *cursor;
1088 output_cursor.hpos = cursor->hpos;
1089 output_cursor.vpos = cursor->vpos;
1090 output_cursor.x = cursor->x;
1091 output_cursor.y = cursor->y;
1095 /* Set a nominal cursor position.
1097 HPOS and VPOS are column/row positions in a window glyph matrix. X
1098 and Y are window text area relative pixel positions.
1100 If this is done during an update, updated_window will contain the
1101 window that is being updated and the position is the future output
1102 cursor position for that window. If updated_window is null, use
1103 selected_window and display the cursor at the given position. */
1105 static void
1106 XTcursor_to (vpos, hpos, y, x)
1107 int vpos, hpos, y, x;
1109 struct window *w;
1111 /* If updated_window is not set, work on selected_window. */
1112 if (updated_window)
1113 w = updated_window;
1114 else
1115 w = XWINDOW (selected_window);
1117 /* Set the output cursor. */
1118 output_cursor.hpos = hpos;
1119 output_cursor.vpos = vpos;
1120 output_cursor.x = x;
1121 output_cursor.y = y;
1123 /* If not called as part of an update, really display the cursor.
1124 This will also set the cursor position of W. */
1125 if (updated_window == NULL)
1127 BLOCK_INPUT;
1128 x_display_cursor (w, 1, hpos, vpos, x, y);
1129 XFlush (FRAME_X_DISPLAY (SELECTED_FRAME ()));
1130 UNBLOCK_INPUT;
1136 /***********************************************************************
1137 Display Iterator
1138 ***********************************************************************/
1140 /* Function prototypes of this page. */
1142 static struct face *x_get_glyph_face_and_encoding P_ ((struct frame *,
1143 struct glyph *,
1144 XChar2b *,
1145 int *));
1146 static struct face *x_get_char_face_and_encoding P_ ((struct frame *, int,
1147 int, XChar2b *, int));
1148 static XCharStruct *x_per_char_metric P_ ((XFontStruct *, XChar2b *));
1149 static void x_encode_char P_ ((int, XChar2b *, struct font_info *,
1150 struct charset *));
1151 static void x_append_glyph P_ ((struct it *));
1152 static void x_append_composite_glyph P_ ((struct it *));
1153 static void x_append_stretch_glyph P_ ((struct it *it, Lisp_Object,
1154 int, int, double));
1155 static void x_produce_glyphs P_ ((struct it *));
1156 static void x_produce_image_glyph P_ ((struct it *it));
1159 /* Get metrics of character CHAR2B in FONT. Value is null if CHAR2B
1160 is not contained in the font. */
1162 static INLINE XCharStruct *
1163 x_per_char_metric (font, char2b)
1164 XFontStruct *font;
1165 XChar2b *char2b;
1167 /* The result metric information. */
1168 XCharStruct *pcm = NULL;
1170 xassert (font && char2b);
1172 if (font->per_char != NULL)
1174 if (font->min_byte1 == 0 && font->max_byte1 == 0)
1176 /* min_char_or_byte2 specifies the linear character index
1177 corresponding to the first element of the per_char array,
1178 max_char_or_byte2 is the index of the last character. A
1179 character with non-zero CHAR2B->byte1 is not in the font.
1180 A character with byte2 less than min_char_or_byte2 or
1181 greater max_char_or_byte2 is not in the font. */
1182 if (char2b->byte1 == 0
1183 && char2b->byte2 >= font->min_char_or_byte2
1184 && char2b->byte2 <= font->max_char_or_byte2)
1185 pcm = font->per_char + char2b->byte2 - font->min_char_or_byte2;
1187 else
1189 /* If either min_byte1 or max_byte1 are nonzero, both
1190 min_char_or_byte2 and max_char_or_byte2 are less than
1191 256, and the 2-byte character index values corresponding
1192 to the per_char array element N (counting from 0) are:
1194 byte1 = N/D + min_byte1
1195 byte2 = N\D + min_char_or_byte2
1197 where:
1199 D = max_char_or_byte2 - min_char_or_byte2 + 1
1200 / = integer division
1201 \ = integer modulus */
1202 if (char2b->byte1 >= font->min_byte1
1203 && char2b->byte1 <= font->max_byte1
1204 && char2b->byte2 >= font->min_char_or_byte2
1205 && char2b->byte2 <= font->max_char_or_byte2)
1207 pcm = (font->per_char
1208 + ((font->max_char_or_byte2 - font->min_char_or_byte2 + 1)
1209 * (char2b->byte1 - font->min_byte1))
1210 + (char2b->byte2 - font->min_char_or_byte2));
1214 else
1216 /* If the per_char pointer is null, all glyphs between the first
1217 and last character indexes inclusive have the same
1218 information, as given by both min_bounds and max_bounds. */
1219 if (char2b->byte2 >= font->min_char_or_byte2
1220 && char2b->byte2 <= font->max_char_or_byte2)
1221 pcm = &font->max_bounds;
1224 return ((pcm == NULL
1225 || (pcm->width == 0 && (pcm->rbearing - pcm->lbearing) == 0))
1226 ? NULL : pcm);
1230 /* Encode CHAR2B using encoding information from FONT_INFO. CHAR2B is
1231 the two-byte form of C. Encoding is returned in *CHAR2B. */
1233 static INLINE void
1234 x_encode_char (c, char2b, font_info, charset)
1235 int c;
1236 XChar2b *char2b;
1237 struct font_info *font_info;
1238 struct charset *charset;
1240 XFontStruct *font = font_info->font;
1242 /* FONT_INFO may define a scheme by which to encode byte1 and byte2.
1243 This may be either a program in a special encoder language or a
1244 fixed encoding. */
1245 if (font_info->font_encoder)
1247 /* It's a program. */
1248 struct ccl_program *ccl = font_info->font_encoder;
1250 if (CHARSET_DIMENSION (charset) == 1)
1252 ccl->reg[0] = CHARSET_ID (charset);
1253 ccl->reg[1] = char2b->byte2;
1255 else
1257 ccl->reg[0] = CHARSET_ID (charset);
1258 ccl->reg[1] = char2b->byte1;
1259 ccl->reg[2] = char2b->byte2;
1262 ccl_driver (ccl, NULL, NULL, 0, 0);
1264 /* We assume that MSBs are appropriately set/reset by CCL
1265 program. */
1266 if (font->max_byte1 == 0) /* 1-byte font */
1267 char2b->byte1 = 0, char2b->byte2 = ccl->reg[1];
1268 else
1269 char2b->byte1 = ccl->reg[1], char2b->byte2 = ccl->reg[2];
1271 else if (font_info->encoding_type)
1273 /* Fixed encoding scheme. See fontset.h for the meaning of the
1274 encoding numbers. */
1275 unsigned char enc = font_info->encoding_type;
1277 if ((enc == 1 || enc == 2)
1278 && CHARSET_DIMENSION (charset) == 2)
1279 char2b->byte1 |= 0x80;
1281 if (enc == 1 || enc == 3)
1282 char2b->byte2 |= 0x80;
1287 /* Get face and two-byte form of character C in face FACE_ID on frame
1288 F. The encoding of C is returned in *CHAR2B. MULTIBYTE_P non-zero
1289 means we want to display multibyte text. Value is a pointer to a
1290 realized face that is ready for display. */
1292 static INLINE struct face *
1293 x_get_char_face_and_encoding (f, c, face_id, char2b, multibyte_p)
1294 struct frame *f;
1295 int c, face_id;
1296 XChar2b *char2b;
1297 int multibyte_p;
1299 struct face *face = FACE_FROM_ID (f, face_id);
1301 if (!multibyte_p)
1303 /* Unibyte case. We don't have to encode, but we have to make
1304 sure to use a face suitable for unibyte. */
1305 char2b->byte1 = 0;
1306 char2b->byte2 = c;
1307 face_id = FACE_FOR_CHAR (f, face, c);
1308 face = FACE_FROM_ID (f, face_id);
1310 else if (c < 128 && face_id < BASIC_FACE_ID_SENTINEL)
1312 /* Case of ASCII in a face known to fit ASCII. */
1313 char2b->byte1 = 0;
1314 char2b->byte2 = c;
1316 else if (face->font != NULL)
1318 struct font_info *font_info
1319 = FONT_INFO_FROM_ID (f, face->font_info_id);
1320 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
1321 unsigned code = ENCODE_CHAR (charset, c);
1323 if (CHARSET_DIMENSION (charset) == 1)
1324 char2b->byte1 = 0, char2b->byte2 = code;
1325 else
1326 char2b->byte1 = code >> 8, char2b->byte2 = code & 0xFF;
1328 /* Maybe encode the character in *CHAR2B. */
1329 x_encode_char (c, char2b, font_info, charset);
1332 /* Make sure X resources of the face are allocated. */
1333 xassert (face != NULL);
1334 PREPARE_FACE_FOR_DISPLAY (f, face);
1336 return face;
1340 /* Get face and two-byte form of character glyph GLYPH on frame F.
1341 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
1342 a pointer to a realized face that is ready for display. */
1344 static INLINE struct face *
1345 x_get_glyph_face_and_encoding (f, glyph, char2b, two_byte_p)
1346 struct frame *f;
1347 struct glyph *glyph;
1348 XChar2b *char2b;
1349 int *two_byte_p;
1351 struct face *face;
1353 xassert (glyph->type == CHAR_GLYPH);
1354 face = FACE_FROM_ID (f, glyph->face_id);
1356 if (two_byte_p)
1357 *two_byte_p = 0;
1359 if (!glyph->multibyte_p)
1361 /* Unibyte case. We don't have to encode, but we have to make
1362 sure to use a face suitable for unibyte. */
1363 char2b->byte1 = 0;
1364 char2b->byte2 = glyph->u.ch;
1366 else if (glyph->u.ch < 128
1367 && glyph->face_id < BASIC_FACE_ID_SENTINEL)
1369 /* Case of ASCII in a face known to fit ASCII. */
1370 char2b->byte1 = 0;
1371 char2b->byte2 = glyph->u.ch;
1373 else
1375 struct font_info *font_info
1376 = FONT_INFO_FROM_ID (f, face->font_info_id);
1377 if (font_info)
1379 struct charset *charset = CHARSET_FROM_ID (font_info->charset);
1380 unsigned code = ENCODE_CHAR (charset, glyph->u.ch);
1382 if (CHARSET_DIMENSION (charset) == 1)
1383 char2b->byte1 = 0, char2b->byte2 = code;
1384 else
1385 char2b->byte1 = code >> 8, char2b->byte2 = code & 0xFF;
1387 /* Maybe encode the character in *CHAR2B. */
1388 if (CHARSET_ID (charset) != charset_ascii)
1390 x_encode_char (glyph->u.ch, char2b, font_info, charset);
1391 if (two_byte_p)
1392 *two_byte_p
1393 = ((XFontStruct *) (font_info->font))->max_byte1 > 0;
1398 /* Make sure X resources of the face are allocated. */
1399 xassert (face != NULL);
1400 PREPARE_FACE_FOR_DISPLAY (f, face);
1401 return face;
1405 /* Store one glyph for IT->char_to_display in IT->glyph_row.
1406 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1408 static INLINE void
1409 x_append_glyph (it)
1410 struct it *it;
1412 struct glyph *glyph;
1413 enum glyph_row_area area = it->area;
1415 xassert (it->glyph_row);
1416 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
1418 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1419 if (glyph < it->glyph_row->glyphs[area + 1])
1421 glyph->charpos = CHARPOS (it->position);
1422 glyph->object = it->object;
1423 glyph->pixel_width = it->pixel_width;
1424 glyph->voffset = it->voffset;
1425 glyph->type = CHAR_GLYPH;
1426 glyph->multibyte_p = it->multibyte_p;
1427 glyph->left_box_line_p = it->start_of_box_run_p;
1428 glyph->right_box_line_p = it->end_of_box_run_p;
1429 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1430 || it->phys_descent > it->descent);
1431 glyph->padding_p = 0;
1432 glyph->glyph_not_available_p = it->glyph_not_available_p;
1433 glyph->face_id = it->face_id;
1434 glyph->u.ch = it->char_to_display;
1435 ++it->glyph_row->used[area];
1439 /* Store one glyph for the composition IT->cmp_id in IT->glyph_row.
1440 Called from x_produce_glyphs when IT->glyph_row is non-null. */
1442 static INLINE void
1443 x_append_composite_glyph (it)
1444 struct it *it;
1446 struct glyph *glyph;
1447 enum glyph_row_area area = it->area;
1449 xassert (it->glyph_row);
1451 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1452 if (glyph < it->glyph_row->glyphs[area + 1])
1454 glyph->charpos = CHARPOS (it->position);
1455 glyph->object = it->object;
1456 glyph->pixel_width = it->pixel_width;
1457 glyph->voffset = it->voffset;
1458 glyph->type = COMPOSITE_GLYPH;
1459 glyph->multibyte_p = it->multibyte_p;
1460 glyph->left_box_line_p = it->start_of_box_run_p;
1461 glyph->right_box_line_p = it->end_of_box_run_p;
1462 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
1463 || it->phys_descent > it->descent);
1464 glyph->padding_p = 0;
1465 glyph->glyph_not_available_p = 0;
1466 glyph->face_id = it->face_id;
1467 glyph->u.cmp_id = it->cmp_id;
1468 ++it->glyph_row->used[area];
1473 /* Change IT->ascent and IT->height according to the setting of
1474 IT->voffset. */
1476 static INLINE void
1477 take_vertical_position_into_account (it)
1478 struct it *it;
1480 if (it->voffset)
1482 if (it->voffset < 0)
1483 /* Increase the ascent so that we can display the text higher
1484 in the line. */
1485 it->ascent += abs (it->voffset);
1486 else
1487 /* Increase the descent so that we can display the text lower
1488 in the line. */
1489 it->descent += it->voffset;
1494 /* Produce glyphs/get display metrics for the image IT is loaded with.
1495 See the description of struct display_iterator in dispextern.h for
1496 an overview of struct display_iterator. */
1498 static void
1499 x_produce_image_glyph (it)
1500 struct it *it;
1502 struct image *img;
1503 struct face *face;
1505 xassert (it->what == IT_IMAGE);
1507 face = FACE_FROM_ID (it->f, it->face_id);
1508 img = IMAGE_FROM_ID (it->f, it->image_id);
1509 xassert (img);
1511 /* Make sure X resources of the face and image are loaded. */
1512 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1513 prepare_image_for_display (it->f, img);
1515 it->ascent = it->phys_ascent = image_ascent (img, face);
1516 it->descent = it->phys_descent = img->height + 2 * img->vmargin - it->ascent;
1517 it->pixel_width = img->width + 2 * img->hmargin;
1519 it->nglyphs = 1;
1521 if (face->box != FACE_NO_BOX)
1523 if (face->box_line_width > 0)
1525 it->ascent += face->box_line_width;
1526 it->descent += face->box_line_width;
1529 if (it->start_of_box_run_p)
1530 it->pixel_width += abs (face->box_line_width);
1531 if (it->end_of_box_run_p)
1532 it->pixel_width += abs (face->box_line_width);
1535 take_vertical_position_into_account (it);
1537 if (it->glyph_row)
1539 struct glyph *glyph;
1540 enum glyph_row_area area = it->area;
1542 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1543 if (glyph < it->glyph_row->glyphs[area + 1])
1545 glyph->charpos = CHARPOS (it->position);
1546 glyph->object = it->object;
1547 glyph->pixel_width = it->pixel_width;
1548 glyph->voffset = it->voffset;
1549 glyph->type = IMAGE_GLYPH;
1550 glyph->multibyte_p = it->multibyte_p;
1551 glyph->left_box_line_p = it->start_of_box_run_p;
1552 glyph->right_box_line_p = it->end_of_box_run_p;
1553 glyph->overlaps_vertically_p = 0;
1554 glyph->padding_p = 0;
1555 glyph->glyph_not_available_p = 0;
1556 glyph->face_id = it->face_id;
1557 glyph->u.img_id = img->id;
1558 ++it->glyph_row->used[area];
1564 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
1565 of the glyph, WIDTH and HEIGHT are the width and height of the
1566 stretch. ASCENT is the percentage/100 of HEIGHT to use for the
1567 ascent of the glyph (0 <= ASCENT <= 1). */
1569 static void
1570 x_append_stretch_glyph (it, object, width, height, ascent)
1571 struct it *it;
1572 Lisp_Object object;
1573 int width, height;
1574 double ascent;
1576 struct glyph *glyph;
1577 enum glyph_row_area area = it->area;
1579 xassert (ascent >= 0 && ascent <= 1);
1581 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
1582 if (glyph < it->glyph_row->glyphs[area + 1])
1584 glyph->charpos = CHARPOS (it->position);
1585 glyph->object = object;
1586 glyph->pixel_width = width;
1587 glyph->voffset = it->voffset;
1588 glyph->type = STRETCH_GLYPH;
1589 glyph->multibyte_p = it->multibyte_p;
1590 glyph->left_box_line_p = it->start_of_box_run_p;
1591 glyph->right_box_line_p = it->end_of_box_run_p;
1592 glyph->overlaps_vertically_p = 0;
1593 glyph->padding_p = 0;
1594 glyph->glyph_not_available_p = 0;
1595 glyph->face_id = it->face_id;
1596 glyph->u.stretch.ascent = height * ascent;
1597 glyph->u.stretch.height = height;
1598 ++it->glyph_row->used[area];
1603 /* Produce a stretch glyph for iterator IT. IT->object is the value
1604 of the glyph property displayed. The value must be a list
1605 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
1606 being recognized:
1608 1. `:width WIDTH' specifies that the space should be WIDTH *
1609 canonical char width wide. WIDTH may be an integer or floating
1610 point number.
1612 2. `:relative-width FACTOR' specifies that the width of the stretch
1613 should be computed from the width of the first character having the
1614 `glyph' property, and should be FACTOR times that width.
1616 3. `:align-to HPOS' specifies that the space should be wide enough
1617 to reach HPOS, a value in canonical character units.
1619 Exactly one of the above pairs must be present.
1621 4. `:height HEIGHT' specifies that the height of the stretch produced
1622 should be HEIGHT, measured in canonical character units.
1624 5. `:relative-height FACTOR' specifies that the height of the
1625 stretch should be FACTOR times the height of the characters having
1626 the glyph property.
1628 Either none or exactly one of 4 or 5 must be present.
1630 6. `:ascent ASCENT' specifies that ASCENT percent of the height
1631 of the stretch should be used for the ascent of the stretch.
1632 ASCENT must be in the range 0 <= ASCENT <= 100. */
1634 #define NUMVAL(X) \
1635 ((INTEGERP (X) || FLOATP (X)) \
1636 ? XFLOATINT (X) \
1637 : - 1)
1640 static void
1641 x_produce_stretch_glyph (it)
1642 struct it *it;
1644 /* (space :width WIDTH :height HEIGHT. */
1645 #if GLYPH_DEBUG
1646 extern Lisp_Object Qspace;
1647 #endif
1648 extern Lisp_Object QCwidth, QCheight, QCascent;
1649 extern Lisp_Object QCrelative_width, QCrelative_height;
1650 extern Lisp_Object QCalign_to;
1651 Lisp_Object prop, plist;
1652 double width = 0, height = 0, ascent = 0;
1653 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1654 XFontStruct *font = face->font ? face->font : FRAME_FONT (it->f);
1656 PREPARE_FACE_FOR_DISPLAY (it->f, face);
1658 /* List should start with `space'. */
1659 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
1660 plist = XCDR (it->object);
1662 /* Compute the width of the stretch. */
1663 if (prop = Fplist_get (plist, QCwidth),
1664 NUMVAL (prop) > 0)
1665 /* Absolute width `:width WIDTH' specified and valid. */
1666 width = NUMVAL (prop) * CANON_X_UNIT (it->f);
1667 else if (prop = Fplist_get (plist, QCrelative_width),
1668 NUMVAL (prop) > 0)
1670 /* Relative width `:relative-width FACTOR' specified and valid.
1671 Compute the width of the characters having the `glyph'
1672 property. */
1673 struct it it2;
1674 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
1676 it2 = *it;
1677 if (it->multibyte_p)
1679 int maxlen = ((IT_BYTEPOS (*it) >= GPT ? ZV : GPT)
1680 - IT_BYTEPOS (*it));
1681 it2.c = STRING_CHAR_AND_LENGTH (p, maxlen, it2.len);
1683 else
1684 it2.c = *p, it2.len = 1;
1686 it2.glyph_row = NULL;
1687 it2.what = IT_CHARACTER;
1688 x_produce_glyphs (&it2);
1689 width = NUMVAL (prop) * it2.pixel_width;
1691 else if (prop = Fplist_get (plist, QCalign_to),
1692 NUMVAL (prop) > 0)
1693 width = NUMVAL (prop) * CANON_X_UNIT (it->f) - it->current_x;
1694 else
1695 /* Nothing specified -> width defaults to canonical char width. */
1696 width = CANON_X_UNIT (it->f);
1698 /* Compute height. */
1699 if (prop = Fplist_get (plist, QCheight),
1700 NUMVAL (prop) > 0)
1701 height = NUMVAL (prop) * CANON_Y_UNIT (it->f);
1702 else if (prop = Fplist_get (plist, QCrelative_height),
1703 NUMVAL (prop) > 0)
1704 height = FONT_HEIGHT (font) * NUMVAL (prop);
1705 else
1706 height = FONT_HEIGHT (font);
1708 /* Compute percentage of height used for ascent. If
1709 `:ascent ASCENT' is present and valid, use that. Otherwise,
1710 derive the ascent from the font in use. */
1711 if (prop = Fplist_get (plist, QCascent),
1712 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
1713 ascent = NUMVAL (prop) / 100.0;
1714 else
1715 ascent = (double) font->ascent / FONT_HEIGHT (font);
1717 if (width <= 0)
1718 width = 1;
1719 if (height <= 0)
1720 height = 1;
1722 if (it->glyph_row)
1724 Lisp_Object object = it->stack[it->sp - 1].string;
1725 if (!STRINGP (object))
1726 object = it->w->buffer;
1727 x_append_stretch_glyph (it, object, width, height, ascent);
1730 it->pixel_width = width;
1731 it->ascent = it->phys_ascent = height * ascent;
1732 it->descent = it->phys_descent = height - it->ascent;
1733 it->nglyphs = 1;
1735 if (face->box != FACE_NO_BOX)
1737 if (face->box_line_width > 0)
1739 it->ascent += face->box_line_width;
1740 it->descent += face->box_line_width;
1743 if (it->start_of_box_run_p)
1744 it->pixel_width += abs (face->box_line_width);
1745 if (it->end_of_box_run_p)
1746 it->pixel_width += abs (face->box_line_width);
1749 take_vertical_position_into_account (it);
1752 /* Return proper value to be used as baseline offset of font that has
1753 ASCENT and DESCENT to draw characters by the font at the vertical
1754 center of the line of frame F.
1756 Here, out task is to find the value of BOFF in the following figure;
1758 -------------------------+-----------+-
1759 -+-+---------+-+ | |
1760 | | | | | |
1761 | | | | F_ASCENT F_HEIGHT
1762 | | | ASCENT | |
1763 HEIGHT | | | | |
1764 | | |-|-+------+-----------|------- baseline
1765 | | | | BOFF | |
1766 | |---------|-+-+ | |
1767 | | | DESCENT | |
1768 -+-+---------+-+ F_DESCENT |
1769 -------------------------+-----------+-
1771 -BOFF + DESCENT + (F_HEIGHT - HEIGHT) / 2 = F_DESCENT
1772 BOFF = DESCENT + (F_HEIGHT - HEIGHT) / 2 - F_DESCENT
1773 DESCENT = FONT->descent
1774 HEIGHT = FONT_HEIGHT (FONT)
1775 F_DESCENT = (F->output_data.x->font->descent
1776 - F->output_data.x->baseline_offset)
1777 F_HEIGHT = FRAME_LINE_HEIGHT (F)
1780 #define VCENTER_BASELINE_OFFSET(FONT, F) \
1781 ((FONT)->descent \
1782 + (FRAME_LINE_HEIGHT ((F)) - FONT_HEIGHT ((FONT)) \
1783 + (FRAME_LINE_HEIGHT ((F)) > FONT_HEIGHT ((FONT)))) / 2 \
1784 - ((F)->output_data.x->font->descent - (F)->output_data.x->baseline_offset))
1786 /* Produce glyphs/get display metrics for the display element IT is
1787 loaded with. See the description of struct display_iterator in
1788 dispextern.h for an overview of struct display_iterator. */
1790 static void
1791 x_produce_glyphs (it)
1792 struct it *it;
1794 it->glyph_not_available_p = 0;
1796 if (it->what == IT_CHARACTER)
1798 XChar2b char2b;
1799 XFontStruct *font;
1800 struct face *face = FACE_FROM_ID (it->f, it->face_id);
1801 XCharStruct *pcm;
1802 int font_not_found_p;
1803 struct font_info *font_info;
1804 int boff; /* baseline offset */
1805 /* We may change it->multibyte_p upon unibyte<->multibyte
1806 conversion. So, save the current value now and restore it
1807 later.
1809 Note: It seems that we don't have to record multibyte_p in
1810 struct glyph because the character code itself tells if or
1811 not the character is multibyte. Thus, in the future, we must
1812 consider eliminating the field `multibyte_p' in the struct
1813 glyph. */
1814 int saved_multibyte_p = it->multibyte_p;
1816 /* Maybe translate single-byte characters to multibyte, or the
1817 other way. */
1818 it->char_to_display = it->c;
1819 if (! ASCII_CHAR_P (it->c)
1820 && ! it->multibyte_p)
1822 if (SINGLE_BYTE_CHAR_P (it->c)
1823 && unibyte_display_via_language_environment)
1824 it->char_to_display = unibyte_char_to_multibyte (it->c);
1825 if (! SINGLE_BYTE_CHAR_P (it->c))
1827 it->multibyte_p = 1;
1828 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
1829 face = FACE_FROM_ID (it->f, it->face_id);
1833 /* Get font to use. Encode IT->char_to_display. */
1834 x_get_char_face_and_encoding (it->f, it->char_to_display,
1835 it->face_id, &char2b,
1836 it->multibyte_p);
1837 font = face->font;
1839 /* When no suitable font found, use the default font. */
1840 font_not_found_p = font == NULL;
1841 if (font_not_found_p)
1843 font = FRAME_FONT (it->f);
1844 boff = it->f->output_data.x->baseline_offset;
1845 font_info = NULL;
1847 else
1849 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
1850 boff = font_info->baseline_offset;
1851 if (font_info->vertical_centering)
1852 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
1855 if (it->char_to_display >= ' '
1856 && (!it->multibyte_p || it->char_to_display < 128))
1858 /* Either unibyte or ASCII. */
1859 int stretched_p;
1861 it->nglyphs = 1;
1863 pcm = x_per_char_metric (font, &char2b);
1864 it->ascent = font->ascent + boff;
1865 it->descent = font->descent - boff;
1867 if (pcm)
1869 it->phys_ascent = pcm->ascent + boff;
1870 it->phys_descent = pcm->descent - boff;
1871 it->pixel_width = pcm->width;
1873 else
1875 it->glyph_not_available_p = 1;
1876 it->phys_ascent = font->ascent + boff;
1877 it->phys_descent = font->descent - boff;
1878 it->pixel_width = FONT_WIDTH (font);
1881 /* If this is a space inside a region of text with
1882 `space-width' property, change its width. */
1883 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
1884 if (stretched_p)
1885 it->pixel_width *= XFLOATINT (it->space_width);
1887 /* If face has a box, add the box thickness to the character
1888 height. If character has a box line to the left and/or
1889 right, add the box line width to the character's width. */
1890 if (face->box != FACE_NO_BOX)
1892 int thick = face->box_line_width;
1894 if (thick > 0)
1896 it->ascent += thick;
1897 it->descent += thick;
1899 else
1900 thick = -thick;
1902 if (it->start_of_box_run_p)
1903 it->pixel_width += thick;
1904 if (it->end_of_box_run_p)
1905 it->pixel_width += thick;
1908 /* If face has an overline, add the height of the overline
1909 (1 pixel) and a 1 pixel margin to the character height. */
1910 if (face->overline_p)
1911 it->ascent += 2;
1913 take_vertical_position_into_account (it);
1915 /* If we have to actually produce glyphs, do it. */
1916 if (it->glyph_row)
1918 if (stretched_p)
1920 /* Translate a space with a `space-width' property
1921 into a stretch glyph. */
1922 double ascent = (double) font->ascent / FONT_HEIGHT (font);
1923 x_append_stretch_glyph (it, it->object, it->pixel_width,
1924 it->ascent + it->descent, ascent);
1926 else
1927 x_append_glyph (it);
1929 /* If characters with lbearing or rbearing are displayed
1930 in this line, record that fact in a flag of the
1931 glyph row. This is used to optimize X output code. */
1932 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
1933 it->glyph_row->contains_overlapping_glyphs_p = 1;
1936 else if (it->char_to_display == '\n')
1938 /* A newline has no width but we need the height of the line. */
1939 it->pixel_width = 0;
1940 it->nglyphs = 0;
1941 it->ascent = it->phys_ascent = font->ascent + boff;
1942 it->descent = it->phys_descent = font->descent - boff;
1944 if (face->box != FACE_NO_BOX
1945 && face->box_line_width > 0)
1947 it->ascent += face->box_line_width;
1948 it->descent += face->box_line_width;
1951 else if (it->char_to_display == '\t')
1953 int tab_width = it->tab_width * CANON_X_UNIT (it->f);
1954 int x = it->current_x + it->continuation_lines_width;
1955 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
1957 /* If the distance from the current position to the next tab
1958 stop is less than a canonical character width, use the
1959 tab stop after that. */
1960 if (next_tab_x - x < CANON_X_UNIT (it->f))
1961 next_tab_x += tab_width;
1963 it->pixel_width = next_tab_x - x;
1964 it->nglyphs = 1;
1965 it->ascent = it->phys_ascent = font->ascent + boff;
1966 it->descent = it->phys_descent = font->descent - boff;
1968 if (it->glyph_row)
1970 double ascent = (double) it->ascent / (it->ascent + it->descent);
1971 x_append_stretch_glyph (it, it->object, it->pixel_width,
1972 it->ascent + it->descent, ascent);
1975 else
1977 /* A multi-byte character. Assume that the display width of the
1978 character is the width of the character multiplied by the
1979 width of the font. */
1981 /* If we found a font, this font should give us the right
1982 metrics. If we didn't find a font, use the frame's
1983 default font and calculate the width of the character by
1984 multiplying the width of font by the width of the
1985 character. */
1986 pcm = x_per_char_metric (font, &char2b);
1987 if (font_not_found_p || !pcm)
1989 it->glyph_not_available_p = 1;
1990 it->pixel_width = (FONT_WIDTH (FRAME_FONT (it->f))
1991 * CHAR_WIDTH (it->char_to_display));
1992 it->phys_ascent = font->ascent + boff;
1993 it->phys_descent = font->descent - boff;
1995 else
1997 it->pixel_width = pcm->width;
1998 it->phys_ascent = pcm->ascent + boff;
1999 it->phys_descent = pcm->descent - boff;
2000 if (it->glyph_row
2001 && (pcm->lbearing < 0
2002 || pcm->rbearing > pcm->width))
2003 it->glyph_row->contains_overlapping_glyphs_p = 1;
2005 it->nglyphs = 1;
2006 it->ascent = font->ascent + boff;
2007 it->descent = font->descent - boff;
2008 if (face->box != FACE_NO_BOX)
2010 int thick = face->box_line_width;
2012 if (thick > 0)
2014 it->ascent += thick;
2015 it->descent += thick;
2017 else
2018 thick = - thick;
2020 if (it->start_of_box_run_p)
2021 it->pixel_width += thick;
2022 if (it->end_of_box_run_p)
2023 it->pixel_width += thick;
2026 /* If face has an overline, add the height of the overline
2027 (1 pixel) and a 1 pixel margin to the character height. */
2028 if (face->overline_p)
2029 it->ascent += 2;
2031 take_vertical_position_into_account (it);
2033 if (it->glyph_row)
2034 x_append_glyph (it);
2036 it->multibyte_p = saved_multibyte_p;
2038 else if (it->what == IT_COMPOSITION)
2040 /* Note: A composition is represented as one glyph in the
2041 glyph matrix. There are no padding glyphs. */
2042 XChar2b char2b;
2043 XFontStruct *font;
2044 struct face *face = FACE_FROM_ID (it->f, it->face_id);
2045 XCharStruct *pcm;
2046 int font_not_found_p;
2047 struct font_info *font_info;
2048 int boff; /* baseline offset */
2049 struct composition *cmp = composition_table[it->cmp_id];
2051 /* Maybe translate single-byte characters to multibyte. */
2052 it->char_to_display = it->c;
2053 if (unibyte_display_via_language_environment
2054 && it->c >= 0200)
2056 it->char_to_display = unibyte_char_to_multibyte (it->c);
2059 /* Get face and font to use. Encode IT->char_to_display. */
2060 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display);
2061 face = FACE_FROM_ID (it->f, it->face_id);
2062 x_get_char_face_and_encoding (it->f, it->char_to_display,
2063 it->face_id, &char2b, it->multibyte_p);
2064 font = face->font;
2066 /* When no suitable font found, use the default font. */
2067 font_not_found_p = font == NULL;
2068 if (font_not_found_p)
2070 font = FRAME_FONT (it->f);
2071 boff = it->f->output_data.x->baseline_offset;
2072 font_info = NULL;
2074 else
2076 font_info = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2077 boff = font_info->baseline_offset;
2078 if (font_info->vertical_centering)
2079 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2082 /* There are no padding glyphs, so there is only one glyph to
2083 produce for the composition. Important is that pixel_width,
2084 ascent and descent are the values of what is drawn by
2085 draw_glyphs (i.e. the values of the overall glyphs composed). */
2086 it->nglyphs = 1;
2088 /* If we have not yet calculated pixel size data of glyphs of
2089 the composition for the current face font, calculate them
2090 now. Theoretically, we have to check all fonts for the
2091 glyphs, but that requires much time and memory space. So,
2092 here we check only the font of the first glyph. This leads
2093 to incorrect display very rarely, and C-l (recenter) can
2094 correct the display anyway. */
2095 if (cmp->font != (void *) font)
2097 /* Ascent and descent of the font of the first character of
2098 this composition (adjusted by baseline offset). Ascent
2099 and descent of overall glyphs should not be less than
2100 them respectively. */
2101 int font_ascent = font->ascent + boff;
2102 int font_descent = font->descent - boff;
2103 /* Bounding box of the overall glyphs. */
2104 int leftmost, rightmost, lowest, highest;
2105 int i, width, ascent, descent;
2107 cmp->font = (void *) font;
2109 /* Initialize the bounding box. */
2110 if (font_info
2111 && (pcm = x_per_char_metric (font, &char2b)))
2113 width = pcm->width;
2114 ascent = pcm->ascent;
2115 descent = pcm->descent;
2117 else
2119 width = FONT_WIDTH (font);
2120 ascent = font->ascent;
2121 descent = font->descent;
2124 rightmost = width;
2125 lowest = - descent + boff;
2126 highest = ascent + boff;
2127 leftmost = 0;
2129 if (font_info
2130 && font_info->default_ascent
2131 && CHAR_TABLE_P (Vuse_default_ascent)
2132 && !NILP (Faref (Vuse_default_ascent,
2133 make_number (it->char_to_display))))
2134 highest = font_info->default_ascent + boff;
2136 /* Draw the first glyph at the normal position. It may be
2137 shifted to right later if some other glyphs are drawn at
2138 the left. */
2139 cmp->offsets[0] = 0;
2140 cmp->offsets[1] = boff;
2142 /* Set cmp->offsets for the remaining glyphs. */
2143 for (i = 1; i < cmp->glyph_len; i++)
2145 int left, right, btm, top;
2146 int ch = COMPOSITION_GLYPH (cmp, i);
2147 int face_id = FACE_FOR_CHAR (it->f, face, ch);
2149 face = FACE_FROM_ID (it->f, face_id);
2150 x_get_char_face_and_encoding (it->f, ch, face->id, &char2b,
2151 it->multibyte_p);
2152 font = face->font;
2153 if (font == NULL)
2155 font = FRAME_FONT (it->f);
2156 boff = it->f->output_data.x->baseline_offset;
2157 font_info = NULL;
2159 else
2161 font_info
2162 = FONT_INFO_FROM_ID (it->f, face->font_info_id);
2163 boff = font_info->baseline_offset;
2164 if (font_info->vertical_centering)
2165 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
2168 if (font_info
2169 && (pcm = x_per_char_metric (font, &char2b)))
2171 width = pcm->width;
2172 ascent = pcm->ascent;
2173 descent = pcm->descent;
2175 else
2177 width = FONT_WIDTH (font);
2178 ascent = 1;
2179 descent = 0;
2182 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
2184 /* Relative composition with or without
2185 alternate chars. */
2186 left = (leftmost + rightmost - width) / 2;
2187 btm = - descent + boff;
2188 if (font_info && font_info->relative_compose
2189 && (! CHAR_TABLE_P (Vignore_relative_composition)
2190 || NILP (Faref (Vignore_relative_composition,
2191 make_number (ch)))))
2194 if (- descent >= font_info->relative_compose)
2195 /* One extra pixel between two glyphs. */
2196 btm = highest + 1;
2197 else if (ascent <= 0)
2198 /* One extra pixel between two glyphs. */
2199 btm = lowest - 1 - ascent - descent;
2202 else
2204 /* A composition rule is specified by an integer
2205 value that encodes global and new reference
2206 points (GREF and NREF). GREF and NREF are
2207 specified by numbers as below:
2209 0---1---2 -- ascent
2213 9--10--11 -- center
2215 ---3---4---5--- baseline
2217 6---7---8 -- descent
2219 int rule = COMPOSITION_RULE (cmp, i);
2220 int gref, nref, grefx, grefy, nrefx, nrefy;
2222 COMPOSITION_DECODE_RULE (rule, gref, nref);
2223 grefx = gref % 3, nrefx = nref % 3;
2224 grefy = gref / 3, nrefy = nref / 3;
2226 left = (leftmost
2227 + grefx * (rightmost - leftmost) / 2
2228 - nrefx * width / 2);
2229 btm = ((grefy == 0 ? highest
2230 : grefy == 1 ? 0
2231 : grefy == 2 ? lowest
2232 : (highest + lowest) / 2)
2233 - (nrefy == 0 ? ascent + descent
2234 : nrefy == 1 ? descent - boff
2235 : nrefy == 2 ? 0
2236 : (ascent + descent) / 2));
2239 cmp->offsets[i * 2] = left;
2240 cmp->offsets[i * 2 + 1] = btm + descent;
2242 /* Update the bounding box of the overall glyphs. */
2243 right = left + width;
2244 top = btm + descent + ascent;
2245 if (left < leftmost)
2246 leftmost = left;
2247 if (right > rightmost)
2248 rightmost = right;
2249 if (top > highest)
2250 highest = top;
2251 if (btm < lowest)
2252 lowest = btm;
2255 /* If there are glyphs whose x-offsets are negative,
2256 shift all glyphs to the right and make all x-offsets
2257 non-negative. */
2258 if (leftmost < 0)
2260 for (i = 0; i < cmp->glyph_len; i++)
2261 cmp->offsets[i * 2] -= leftmost;
2262 rightmost -= leftmost;
2265 cmp->pixel_width = rightmost;
2266 cmp->ascent = highest;
2267 cmp->descent = - lowest;
2268 if (cmp->ascent < font_ascent)
2269 cmp->ascent = font_ascent;
2270 if (cmp->descent < font_descent)
2271 cmp->descent = font_descent;
2274 it->pixel_width = cmp->pixel_width;
2275 it->ascent = it->phys_ascent = cmp->ascent;
2276 it->descent = it->phys_descent = cmp->descent;
2278 if (face->box != FACE_NO_BOX)
2280 int thick = face->box_line_width;
2282 if (thick > 0)
2284 it->ascent += thick;
2285 it->descent += thick;
2287 else
2288 thick = - thick;
2290 if (it->start_of_box_run_p)
2291 it->pixel_width += thick;
2292 if (it->end_of_box_run_p)
2293 it->pixel_width += thick;
2296 /* If face has an overline, add the height of the overline
2297 (1 pixel) and a 1 pixel margin to the character height. */
2298 if (face->overline_p)
2299 it->ascent += 2;
2301 take_vertical_position_into_account (it);
2303 if (it->glyph_row)
2304 x_append_composite_glyph (it);
2306 else if (it->what == IT_IMAGE)
2307 x_produce_image_glyph (it);
2308 else if (it->what == IT_STRETCH)
2309 x_produce_stretch_glyph (it);
2311 /* Accumulate dimensions. Note: can't assume that it->descent > 0
2312 because this isn't true for images with `:ascent 100'. */
2313 xassert (it->ascent >= 0 && it->descent >= 0);
2314 if (it->area == TEXT_AREA)
2315 it->current_x += it->pixel_width;
2317 it->descent += it->extra_line_spacing;
2319 it->max_ascent = max (it->max_ascent, it->ascent);
2320 it->max_descent = max (it->max_descent, it->descent);
2321 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
2322 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
2326 /* Estimate the pixel height of the mode or top line on frame F.
2327 FACE_ID specifies what line's height to estimate. */
2330 x_estimate_mode_line_height (f, face_id)
2331 struct frame *f;
2332 enum face_id face_id;
2334 int height = FONT_HEIGHT (FRAME_FONT (f));
2336 /* This function is called so early when Emacs starts that the face
2337 cache and mode line face are not yet initialized. */
2338 if (FRAME_FACE_CACHE (f))
2340 struct face *face = FACE_FROM_ID (f, face_id);
2341 if (face)
2343 if (face->font)
2344 height = FONT_HEIGHT (face->font);
2345 if (face->box_line_width > 0)
2346 height += 2 * face->box_line_width;
2350 return height;
2354 /***********************************************************************
2355 Glyph display
2356 ***********************************************************************/
2358 /* A sequence of glyphs to be drawn in the same face.
2360 This data structure is not really completely X specific, so it
2361 could possibly, at least partially, be useful for other systems. It
2362 is currently not part of the external redisplay interface because
2363 it's not clear what other systems will need. */
2365 struct glyph_string
2367 /* X-origin of the string. */
2368 int x;
2370 /* Y-origin and y-position of the base line of this string. */
2371 int y, ybase;
2373 /* The width of the string, not including a face extension. */
2374 int width;
2376 /* The width of the string, including a face extension. */
2377 int background_width;
2379 /* The height of this string. This is the height of the line this
2380 string is drawn in, and can be different from the height of the
2381 font the string is drawn in. */
2382 int height;
2384 /* Number of pixels this string overwrites in front of its x-origin.
2385 This number is zero if the string has an lbearing >= 0; it is
2386 -lbearing, if the string has an lbearing < 0. */
2387 int left_overhang;
2389 /* Number of pixels this string overwrites past its right-most
2390 nominal x-position, i.e. x + width. Zero if the string's
2391 rbearing is <= its nominal width, rbearing - width otherwise. */
2392 int right_overhang;
2394 /* The frame on which the glyph string is drawn. */
2395 struct frame *f;
2397 /* The window on which the glyph string is drawn. */
2398 struct window *w;
2400 /* X display and window for convenience. */
2401 Display *display;
2402 Window window;
2404 /* The glyph row for which this string was built. It determines the
2405 y-origin and height of the string. */
2406 struct glyph_row *row;
2408 /* The area within row. */
2409 enum glyph_row_area area;
2411 /* Characters to be drawn, and number of characters. */
2412 XChar2b *char2b;
2413 int nchars;
2415 /* A face-override for drawing cursors, mouse face and similar. */
2416 enum draw_glyphs_face hl;
2418 /* Face in which this string is to be drawn. */
2419 struct face *face;
2421 /* Font in which this string is to be drawn. */
2422 XFontStruct *font;
2424 /* Font info for this string. */
2425 struct font_info *font_info;
2427 /* Non-null means this string describes (part of) a composition.
2428 All characters from char2b are drawn composed. */
2429 struct composition *cmp;
2431 /* Index of this glyph string's first character in the glyph
2432 definition of CMP. If this is zero, this glyph string describes
2433 the first character of a composition. */
2434 int gidx;
2436 /* 1 means this glyph strings face has to be drawn to the right end
2437 of the window's drawing area. */
2438 unsigned extends_to_end_of_line_p : 1;
2440 /* 1 means the background of this string has been drawn. */
2441 unsigned background_filled_p : 1;
2443 /* 1 means glyph string must be drawn with 16-bit functions. */
2444 unsigned two_byte_p : 1;
2446 /* 1 means that the original font determined for drawing this glyph
2447 string could not be loaded. The member `font' has been set to
2448 the frame's default font in this case. */
2449 unsigned font_not_found_p : 1;
2451 /* 1 means that the face in which this glyph string is drawn has a
2452 stipple pattern. */
2453 unsigned stippled_p : 1;
2455 /* 1 means only the foreground of this glyph string must be drawn,
2456 and we should use the physical height of the line this glyph
2457 string appears in as clip rect. */
2458 unsigned for_overlaps_p : 1;
2460 /* The GC to use for drawing this glyph string. */
2461 GC gc;
2463 /* A pointer to the first glyph in the string. This glyph
2464 corresponds to char2b[0]. Needed to draw rectangles if
2465 font_not_found_p is 1. */
2466 struct glyph *first_glyph;
2468 /* Image, if any. */
2469 struct image *img;
2471 struct glyph_string *next, *prev;
2475 #if GLYPH_DEBUG
2477 static void
2478 x_dump_glyph_string (s)
2479 struct glyph_string *s;
2481 fprintf (stderr, "glyph string\n");
2482 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
2483 s->x, s->y, s->width, s->height);
2484 fprintf (stderr, " ybase = %d\n", s->ybase);
2485 fprintf (stderr, " hl = %d\n", s->hl);
2486 fprintf (stderr, " left overhang = %d, right = %d\n",
2487 s->left_overhang, s->right_overhang);
2488 fprintf (stderr, " nchars = %d\n", s->nchars);
2489 fprintf (stderr, " extends to end of line = %d\n",
2490 s->extends_to_end_of_line_p);
2491 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
2492 fprintf (stderr, " bg width = %d\n", s->background_width);
2495 #endif /* GLYPH_DEBUG */
2499 static void x_append_glyph_string_lists P_ ((struct glyph_string **,
2500 struct glyph_string **,
2501 struct glyph_string *,
2502 struct glyph_string *));
2503 static void x_prepend_glyph_string_lists P_ ((struct glyph_string **,
2504 struct glyph_string **,
2505 struct glyph_string *,
2506 struct glyph_string *));
2507 static void x_append_glyph_string P_ ((struct glyph_string **,
2508 struct glyph_string **,
2509 struct glyph_string *));
2510 static int x_left_overwritten P_ ((struct glyph_string *));
2511 static int x_left_overwriting P_ ((struct glyph_string *));
2512 static int x_right_overwritten P_ ((struct glyph_string *));
2513 static int x_right_overwriting P_ ((struct glyph_string *));
2514 static int x_fill_glyph_string P_ ((struct glyph_string *, int, int, int,
2515 int));
2516 static void x_init_glyph_string P_ ((struct glyph_string *,
2517 XChar2b *, struct window *,
2518 struct glyph_row *,
2519 enum glyph_row_area, int,
2520 enum draw_glyphs_face));
2521 static int x_draw_glyphs P_ ((struct window *, int , struct glyph_row *,
2522 enum glyph_row_area, int, int,
2523 enum draw_glyphs_face, int));
2524 static void x_set_glyph_string_clipping P_ ((struct glyph_string *));
2525 static void x_set_glyph_string_gc P_ ((struct glyph_string *));
2526 static void x_draw_glyph_string_background P_ ((struct glyph_string *,
2527 int));
2528 static void x_draw_glyph_string_foreground P_ ((struct glyph_string *));
2529 static void x_draw_composite_glyph_string_foreground P_ ((struct glyph_string *));
2530 static void x_draw_glyph_string_box P_ ((struct glyph_string *));
2531 static void x_draw_glyph_string P_ ((struct glyph_string *));
2532 static void x_compute_glyph_string_overhangs P_ ((struct glyph_string *));
2533 static void x_set_cursor_gc P_ ((struct glyph_string *));
2534 static void x_set_mode_line_face_gc P_ ((struct glyph_string *));
2535 static void x_set_mouse_face_gc P_ ((struct glyph_string *));
2536 static void x_get_glyph_overhangs P_ ((struct glyph *, struct frame *,
2537 int *, int *));
2538 static void x_compute_overhangs_and_x P_ ((struct glyph_string *, int, int));
2539 static int x_alloc_lighter_color P_ ((struct frame *, Display *, Colormap,
2540 unsigned long *, double, int));
2541 static void x_setup_relief_color P_ ((struct frame *, struct relief *,
2542 double, int, unsigned long));
2543 static void x_setup_relief_colors P_ ((struct glyph_string *));
2544 static void x_draw_image_glyph_string P_ ((struct glyph_string *));
2545 static void x_draw_image_relief P_ ((struct glyph_string *));
2546 static void x_draw_image_foreground P_ ((struct glyph_string *));
2547 static void x_draw_image_foreground_1 P_ ((struct glyph_string *, Pixmap));
2548 static void x_fill_image_glyph_string P_ ((struct glyph_string *));
2549 static void x_clear_glyph_string_rect P_ ((struct glyph_string *, int,
2550 int, int, int));
2551 static void x_draw_relief_rect P_ ((struct frame *, int, int, int, int,
2552 int, int, int, int, XRectangle *));
2553 static void x_draw_box_rect P_ ((struct glyph_string *, int, int, int, int,
2554 int, int, int, XRectangle *));
2555 static void x_fix_overlapping_area P_ ((struct window *, struct glyph_row *,
2556 enum glyph_row_area));
2557 static int x_fill_stretch_glyph_string P_ ((struct glyph_string *,
2558 struct glyph_row *,
2559 enum glyph_row_area, int, int));
2561 #if GLYPH_DEBUG
2562 static void x_check_font P_ ((struct frame *, XFontStruct *));
2563 #endif
2566 /* Append the list of glyph strings with head H and tail T to the list
2567 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
2569 static INLINE void
2570 x_append_glyph_string_lists (head, tail, h, t)
2571 struct glyph_string **head, **tail;
2572 struct glyph_string *h, *t;
2574 if (h)
2576 if (*head)
2577 (*tail)->next = h;
2578 else
2579 *head = h;
2580 h->prev = *tail;
2581 *tail = t;
2586 /* Prepend the list of glyph strings with head H and tail T to the
2587 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
2588 result. */
2590 static INLINE void
2591 x_prepend_glyph_string_lists (head, tail, h, t)
2592 struct glyph_string **head, **tail;
2593 struct glyph_string *h, *t;
2595 if (h)
2597 if (*head)
2598 (*head)->prev = t;
2599 else
2600 *tail = t;
2601 t->next = *head;
2602 *head = h;
2607 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
2608 Set *HEAD and *TAIL to the resulting list. */
2610 static INLINE void
2611 x_append_glyph_string (head, tail, s)
2612 struct glyph_string **head, **tail;
2613 struct glyph_string *s;
2615 s->next = s->prev = NULL;
2616 x_append_glyph_string_lists (head, tail, s, s);
2620 /* Set S->gc to a suitable GC for drawing glyph string S in cursor
2621 face. */
2623 static void
2624 x_set_cursor_gc (s)
2625 struct glyph_string *s;
2627 if (s->font == FRAME_FONT (s->f)
2628 && s->face->background == FRAME_BACKGROUND_PIXEL (s->f)
2629 && s->face->foreground == FRAME_FOREGROUND_PIXEL (s->f)
2630 && !s->cmp)
2631 s->gc = s->f->output_data.x->cursor_gc;
2632 else
2634 /* Cursor on non-default face: must merge. */
2635 XGCValues xgcv;
2636 unsigned long mask;
2638 xgcv.background = s->f->output_data.x->cursor_pixel;
2639 xgcv.foreground = s->face->background;
2641 /* If the glyph would be invisible, try a different foreground. */
2642 if (xgcv.foreground == xgcv.background)
2643 xgcv.foreground = s->face->foreground;
2644 if (xgcv.foreground == xgcv.background)
2645 xgcv.foreground = s->f->output_data.x->cursor_foreground_pixel;
2646 if (xgcv.foreground == xgcv.background)
2647 xgcv.foreground = s->face->foreground;
2649 /* Make sure the cursor is distinct from text in this face. */
2650 if (xgcv.background == s->face->background
2651 && xgcv.foreground == s->face->foreground)
2653 xgcv.background = s->face->foreground;
2654 xgcv.foreground = s->face->background;
2657 IF_DEBUG (x_check_font (s->f, s->font));
2658 xgcv.font = s->font->fid;
2659 xgcv.graphics_exposures = False;
2660 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2662 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2663 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2664 mask, &xgcv);
2665 else
2666 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2667 = XCreateGC (s->display, s->window, mask, &xgcv);
2669 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2674 /* Set up S->gc of glyph string S for drawing text in mouse face. */
2676 static void
2677 x_set_mouse_face_gc (s)
2678 struct glyph_string *s;
2680 int face_id;
2681 struct face *face;
2683 /* What face has to be used last for the mouse face? */
2684 face_id = FRAME_X_DISPLAY_INFO (s->f)->mouse_face_face_id;
2685 face = FACE_FROM_ID (s->f, face_id);
2686 if (face == NULL)
2687 face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
2689 if (s->first_glyph->type == CHAR_GLYPH)
2690 face_id = FACE_FOR_CHAR (s->f, face, s->first_glyph->u.ch);
2691 else
2692 face_id = FACE_FOR_CHAR (s->f, face, 0);
2693 s->face = FACE_FROM_ID (s->f, face_id);
2694 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2696 /* If font in this face is same as S->font, use it. */
2697 if (s->font == s->face->font)
2698 s->gc = s->face->gc;
2699 else
2701 /* Otherwise construct scratch_cursor_gc with values from FACE
2702 but font FONT. */
2703 XGCValues xgcv;
2704 unsigned long mask;
2706 xgcv.background = s->face->background;
2707 xgcv.foreground = s->face->foreground;
2708 IF_DEBUG (x_check_font (s->f, s->font));
2709 xgcv.font = s->font->fid;
2710 xgcv.graphics_exposures = False;
2711 mask = GCForeground | GCBackground | GCFont | GCGraphicsExposures;
2713 if (FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc)
2714 XChangeGC (s->display, FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc,
2715 mask, &xgcv);
2716 else
2717 FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc
2718 = XCreateGC (s->display, s->window, mask, &xgcv);
2720 s->gc = FRAME_X_DISPLAY_INFO (s->f)->scratch_cursor_gc;
2723 xassert (s->gc != 0);
2727 /* Set S->gc of glyph string S to a GC suitable for drawing a mode line.
2728 Faces to use in the mode line have already been computed when the
2729 matrix was built, so there isn't much to do, here. */
2731 static INLINE void
2732 x_set_mode_line_face_gc (s)
2733 struct glyph_string *s;
2735 s->gc = s->face->gc;
2739 /* Set S->gc of glyph string S for drawing that glyph string. Set
2740 S->stippled_p to a non-zero value if the face of S has a stipple
2741 pattern. */
2743 static INLINE void
2744 x_set_glyph_string_gc (s)
2745 struct glyph_string *s;
2747 PREPARE_FACE_FOR_DISPLAY (s->f, s->face);
2749 if (s->hl == DRAW_NORMAL_TEXT)
2751 s->gc = s->face->gc;
2752 s->stippled_p = s->face->stipple != 0;
2754 else if (s->hl == DRAW_INVERSE_VIDEO)
2756 x_set_mode_line_face_gc (s);
2757 s->stippled_p = s->face->stipple != 0;
2759 else if (s->hl == DRAW_CURSOR)
2761 x_set_cursor_gc (s);
2762 s->stippled_p = 0;
2764 else if (s->hl == DRAW_MOUSE_FACE)
2766 x_set_mouse_face_gc (s);
2767 s->stippled_p = s->face->stipple != 0;
2769 else if (s->hl == DRAW_IMAGE_RAISED
2770 || s->hl == DRAW_IMAGE_SUNKEN)
2772 s->gc = s->face->gc;
2773 s->stippled_p = s->face->stipple != 0;
2775 else
2777 s->gc = s->face->gc;
2778 s->stippled_p = s->face->stipple != 0;
2781 /* GC must have been set. */
2782 xassert (s->gc != 0);
2786 /* Return in *R the clipping rectangle for glyph string S. */
2788 static void
2789 x_get_glyph_string_clip_rect (s, r)
2790 struct glyph_string *s;
2791 XRectangle *r;
2793 if (s->row->full_width_p)
2795 /* Draw full-width. X coordinates are relative to S->w->left. */
2796 int canon_x = CANON_X_UNIT (s->f);
2798 r->x = WINDOW_LEFT_MARGIN (s->w) * canon_x;
2799 r->width = XFASTINT (s->w->width) * canon_x;
2801 if (FRAME_HAS_VERTICAL_SCROLL_BARS (s->f))
2803 int width = FRAME_SCROLL_BAR_WIDTH (s->f) * canon_x;
2804 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (s->f))
2805 r->x -= width;
2808 r->x += FRAME_INTERNAL_BORDER_WIDTH (s->f);
2810 /* Unless displaying a mode or menu bar line, which are always
2811 fully visible, clip to the visible part of the row. */
2812 if (s->w->pseudo_window_p)
2813 r->height = s->row->visible_height;
2814 else
2815 r->height = s->height;
2817 else
2819 /* This is a text line that may be partially visible. */
2820 r->x = WINDOW_AREA_TO_FRAME_PIXEL_X (s->w, s->area, 0);
2821 r->width = window_box_width (s->w, s->area);
2822 r->height = s->row->visible_height;
2825 /* If S draws overlapping rows, it's sufficient to use the top and
2826 bottom of the window for clipping because this glyph string
2827 intentionally draws over other lines. */
2828 if (s->for_overlaps_p)
2830 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2831 r->height = window_text_bottom_y (s->w) - r->y;
2833 else
2835 /* Don't use S->y for clipping because it doesn't take partially
2836 visible lines into account. For example, it can be negative for
2837 partially visible lines at the top of a window. */
2838 if (!s->row->full_width_p
2839 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2840 r->y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (s->w);
2841 else
2842 r->y = max (0, s->row->y);
2844 /* If drawing a tool-bar window, draw it over the internal border
2845 at the top of the window. */
2846 if (s->w == XWINDOW (s->f->tool_bar_window))
2847 r->y -= s->f->output_data.x->internal_border_width;
2850 r->y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r->y);
2854 /* Set clipping for output of glyph string S. S may be part of a mode
2855 line or menu if we don't have X toolkit support. */
2857 static INLINE void
2858 x_set_glyph_string_clipping (s)
2859 struct glyph_string *s;
2861 XRectangle r;
2862 x_get_glyph_string_clip_rect (s, &r);
2863 XSetClipRectangles (s->display, s->gc, 0, 0, &r, 1, Unsorted);
2867 /* Compute left and right overhang of glyph string S. If S is a glyph
2868 string for a composition, assume overhangs don't exist. */
2870 static INLINE void
2871 x_compute_glyph_string_overhangs (s)
2872 struct glyph_string *s;
2874 if (s->cmp == NULL
2875 && s->first_glyph->type == CHAR_GLYPH)
2877 XCharStruct cs;
2878 int direction, font_ascent, font_descent;
2879 XTextExtents16 (s->font, s->char2b, s->nchars, &direction,
2880 &font_ascent, &font_descent, &cs);
2881 s->right_overhang = cs.rbearing > cs.width ? cs.rbearing - cs.width : 0;
2882 s->left_overhang = cs.lbearing < 0 ? -cs.lbearing : 0;
2887 /* Compute overhangs and x-positions for glyph string S and its
2888 predecessors, or successors. X is the starting x-position for S.
2889 BACKWARD_P non-zero means process predecessors. */
2891 static void
2892 x_compute_overhangs_and_x (s, x, backward_p)
2893 struct glyph_string *s;
2894 int x;
2895 int backward_p;
2897 if (backward_p)
2899 while (s)
2901 x_compute_glyph_string_overhangs (s);
2902 x -= s->width;
2903 s->x = x;
2904 s = s->prev;
2907 else
2909 while (s)
2911 x_compute_glyph_string_overhangs (s);
2912 s->x = x;
2913 x += s->width;
2914 s = s->next;
2920 /* Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
2921 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
2922 assumed to be zero. */
2924 static void
2925 x_get_glyph_overhangs (glyph, f, left, right)
2926 struct glyph *glyph;
2927 struct frame *f;
2928 int *left, *right;
2930 *left = *right = 0;
2932 if (glyph->type == CHAR_GLYPH)
2934 XFontStruct *font;
2935 struct face *face;
2936 struct font_info *font_info;
2937 XChar2b char2b;
2938 XCharStruct *pcm;
2940 face = x_get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
2941 font = face->font;
2942 font_info = FONT_INFO_FROM_ID (f, face->font_info_id);
2943 if (font
2944 && (pcm = x_per_char_metric (font, &char2b)))
2946 if (pcm->rbearing > pcm->width)
2947 *right = pcm->rbearing - pcm->width;
2948 if (pcm->lbearing < 0)
2949 *left = -pcm->lbearing;
2955 /* Return the index of the first glyph preceding glyph string S that
2956 is overwritten by S because of S's left overhang. Value is -1
2957 if no glyphs are overwritten. */
2959 static int
2960 x_left_overwritten (s)
2961 struct glyph_string *s;
2963 int k;
2965 if (s->left_overhang)
2967 int x = 0, i;
2968 struct glyph *glyphs = s->row->glyphs[s->area];
2969 int first = s->first_glyph - glyphs;
2971 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
2972 x -= glyphs[i].pixel_width;
2974 k = i + 1;
2976 else
2977 k = -1;
2979 return k;
2983 /* Return the index of the first glyph preceding glyph string S that
2984 is overwriting S because of its right overhang. Value is -1 if no
2985 glyph in front of S overwrites S. */
2987 static int
2988 x_left_overwriting (s)
2989 struct glyph_string *s;
2991 int i, k, x;
2992 struct glyph *glyphs = s->row->glyphs[s->area];
2993 int first = s->first_glyph - glyphs;
2995 k = -1;
2996 x = 0;
2997 for (i = first - 1; i >= 0; --i)
2999 int left, right;
3000 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3001 if (x + right > 0)
3002 k = i;
3003 x -= glyphs[i].pixel_width;
3006 return k;
3010 /* Return the index of the last glyph following glyph string S that is
3011 not overwritten by S because of S's right overhang. Value is -1 if
3012 no such glyph is found. */
3014 static int
3015 x_right_overwritten (s)
3016 struct glyph_string *s;
3018 int k = -1;
3020 if (s->right_overhang)
3022 int x = 0, i;
3023 struct glyph *glyphs = s->row->glyphs[s->area];
3024 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3025 int end = s->row->used[s->area];
3027 for (i = first; i < end && s->right_overhang > x; ++i)
3028 x += glyphs[i].pixel_width;
3030 k = i;
3033 return k;
3037 /* Return the index of the last glyph following glyph string S that
3038 overwrites S because of its left overhang. Value is negative
3039 if no such glyph is found. */
3041 static int
3042 x_right_overwriting (s)
3043 struct glyph_string *s;
3045 int i, k, x;
3046 int end = s->row->used[s->area];
3047 struct glyph *glyphs = s->row->glyphs[s->area];
3048 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
3050 k = -1;
3051 x = 0;
3052 for (i = first; i < end; ++i)
3054 int left, right;
3055 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
3056 if (x - left < 0)
3057 k = i;
3058 x += glyphs[i].pixel_width;
3061 return k;
3065 /* Fill rectangle X, Y, W, H with background color of glyph string S. */
3067 static INLINE void
3068 x_clear_glyph_string_rect (s, x, y, w, h)
3069 struct glyph_string *s;
3070 int x, y, w, h;
3072 XGCValues xgcv;
3073 XGetGCValues (s->display, s->gc, GCForeground | GCBackground, &xgcv);
3074 XSetForeground (s->display, s->gc, xgcv.background);
3075 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
3076 XSetForeground (s->display, s->gc, xgcv.foreground);
3080 /* Draw the background of glyph_string S. If S->background_filled_p
3081 is non-zero don't draw it. FORCE_P non-zero means draw the
3082 background even if it wouldn't be drawn normally. This is used
3083 when a string preceding S draws into the background of S, or S
3084 contains the first component of a composition. */
3086 static void
3087 x_draw_glyph_string_background (s, force_p)
3088 struct glyph_string *s;
3089 int force_p;
3091 /* Nothing to do if background has already been drawn or if it
3092 shouldn't be drawn in the first place. */
3093 if (!s->background_filled_p)
3095 int box_line_width = max (s->face->box_line_width, 0);
3097 if (s->stippled_p)
3099 /* Fill background with a stipple pattern. */
3100 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
3101 XFillRectangle (s->display, s->window, s->gc, s->x,
3102 s->y + box_line_width,
3103 s->background_width,
3104 s->height - 2 * box_line_width);
3105 XSetFillStyle (s->display, s->gc, FillSolid);
3106 s->background_filled_p = 1;
3108 else if (FONT_HEIGHT (s->font) < s->height - 2 * box_line_width
3109 || s->font_not_found_p
3110 || s->extends_to_end_of_line_p
3111 || force_p)
3113 x_clear_glyph_string_rect (s, s->x, s->y + box_line_width,
3114 s->background_width,
3115 s->height - 2 * box_line_width);
3116 s->background_filled_p = 1;
3122 /* Draw the foreground of glyph string S. */
3124 static void
3125 x_draw_glyph_string_foreground (s)
3126 struct glyph_string *s;
3128 int i, x;
3130 /* If first glyph of S has a left box line, start drawing the text
3131 of S to the right of that box line. */
3132 if (s->face->box != FACE_NO_BOX
3133 && s->first_glyph->left_box_line_p)
3134 x = s->x + abs (s->face->box_line_width);
3135 else
3136 x = s->x;
3138 /* Draw characters of S as rectangles if S's font could not be
3139 loaded. */
3140 if (s->font_not_found_p)
3142 for (i = 0; i < s->nchars; ++i)
3144 struct glyph *g = s->first_glyph + i;
3145 XDrawRectangle (s->display, s->window,
3146 s->gc, x, s->y, g->pixel_width - 1,
3147 s->height - 1);
3148 x += g->pixel_width;
3151 else
3153 char *char1b = (char *) s->char2b;
3154 int boff = s->font_info->baseline_offset;
3156 if (s->font_info->vertical_centering)
3157 boff = VCENTER_BASELINE_OFFSET (s->font, s->f) - boff;
3159 /* If we can use 8-bit functions, condense S->char2b. */
3160 if (!s->two_byte_p)
3161 for (i = 0; i < s->nchars; ++i)
3162 char1b[i] = s->char2b[i].byte2;
3164 /* Draw text with XDrawString if background has already been
3165 filled. Otherwise, use XDrawImageString. (Note that
3166 XDrawImageString is usually faster than XDrawString.) Always
3167 use XDrawImageString when drawing the cursor so that there is
3168 no chance that characters under a box cursor are invisible. */
3169 if (s->for_overlaps_p
3170 || (s->background_filled_p && s->hl != DRAW_CURSOR))
3172 /* Draw characters with 16-bit or 8-bit functions. */
3173 if (s->two_byte_p)
3174 XDrawString16 (s->display, s->window, s->gc, x,
3175 s->ybase - boff, s->char2b, s->nchars);
3176 else
3177 XDrawString (s->display, s->window, s->gc, x,
3178 s->ybase - boff, char1b, s->nchars);
3180 else
3182 if (s->two_byte_p)
3183 XDrawImageString16 (s->display, s->window, s->gc, x,
3184 s->ybase - boff, s->char2b, s->nchars);
3185 else
3186 XDrawImageString (s->display, s->window, s->gc, x,
3187 s->ybase - boff, char1b, s->nchars);
3192 /* Draw the foreground of composite glyph string S. */
3194 static void
3195 x_draw_composite_glyph_string_foreground (s)
3196 struct glyph_string *s;
3198 int i, x;
3200 /* If first glyph of S has a left box line, start drawing the text
3201 of S to the right of that box line. */
3202 if (s->face->box != FACE_NO_BOX
3203 && s->first_glyph->left_box_line_p)
3204 x = s->x + abs (s->face->box_line_width);
3205 else
3206 x = s->x;
3208 /* S is a glyph string for a composition. S->gidx is the index of
3209 the first character drawn for glyphs of this composition.
3210 S->gidx == 0 means we are drawing the very first character of
3211 this composition. */
3213 /* Draw a rectangle for the composition if the font for the very
3214 first character of the composition could not be loaded. */
3215 if (s->font_not_found_p)
3217 if (s->gidx == 0)
3218 XDrawRectangle (s->display, s->window, s->gc, x, s->y,
3219 s->width - 1, s->height - 1);
3221 else
3223 for (i = 0; i < s->nchars; i++, ++s->gidx)
3224 XDrawString16 (s->display, s->window, s->gc,
3225 x + s->cmp->offsets[s->gidx * 2],
3226 s->ybase - s->cmp->offsets[s->gidx * 2 + 1],
3227 s->char2b + i, 1);
3232 #ifdef USE_X_TOOLKIT
3234 static struct frame *x_frame_of_widget P_ ((Widget));
3235 static Boolean cvt_string_to_pixel P_ ((Display *, XrmValue *, Cardinal *,
3236 XrmValue *, XrmValue *, XtPointer *));
3237 static void cvt_pixel_dtor P_ ((XtAppContext, XrmValue *, XtPointer,
3238 XrmValue *, Cardinal *));
3241 /* Return the frame on which widget WIDGET is used.. Abort if frame
3242 cannot be determined. */
3244 static struct frame *
3245 x_frame_of_widget (widget)
3246 Widget widget;
3248 struct x_display_info *dpyinfo;
3249 Lisp_Object tail;
3250 struct frame *f;
3252 dpyinfo = x_display_info_for_display (XtDisplay (widget));
3254 /* Find the top-level shell of the widget. Note that this function
3255 can be called when the widget is not yet realized, so XtWindow
3256 (widget) == 0. That's the reason we can't simply use
3257 x_any_window_to_frame. */
3258 while (!XtIsTopLevelShell (widget))
3259 widget = XtParent (widget);
3261 /* Look for a frame with that top-level widget. Allocate the color
3262 on that frame to get the right gamma correction value. */
3263 for (tail = Vframe_list; GC_CONSP (tail); tail = XCDR (tail))
3264 if (GC_FRAMEP (XCAR (tail))
3265 && (f = XFRAME (XCAR (tail)),
3266 (f->output_data.nothing != 1
3267 && FRAME_X_DISPLAY_INFO (f) == dpyinfo))
3268 && f->output_data.x->widget == widget)
3269 return f;
3271 abort ();
3275 /* Allocate the color COLOR->pixel on the screen and display of
3276 widget WIDGET in colormap CMAP. If an exact match cannot be
3277 allocated, try the nearest color available. Value is non-zero
3278 if successful. This is called from lwlib. */
3281 x_alloc_nearest_color_for_widget (widget, cmap, color)
3282 Widget widget;
3283 Colormap cmap;
3284 XColor *color;
3286 struct frame *f = x_frame_of_widget (widget);
3287 return x_alloc_nearest_color (f, cmap, color);
3291 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3292 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3293 If this produces the same color as PIXEL, try a color where all RGB
3294 values have DELTA added. Return the allocated color in *PIXEL.
3295 DISPLAY is the X display, CMAP is the colormap to operate on.
3296 Value is non-zero if successful. */
3299 x_alloc_lighter_color_for_widget (widget, display, cmap, pixel, factor, delta)
3300 Widget widget;
3301 Display *display;
3302 Colormap cmap;
3303 unsigned long *pixel;
3304 double factor;
3305 int delta;
3307 struct frame *f = x_frame_of_widget (widget);
3308 return x_alloc_lighter_color (f, display, cmap, pixel, factor, delta);
3312 /* Structure specifying which arguments should be passed by Xt to
3313 cvt_string_to_pixel. We want the widget's screen and colormap. */
3315 static XtConvertArgRec cvt_string_to_pixel_args[] =
3317 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.screen),
3318 sizeof (Screen *)},
3319 {XtWidgetBaseOffset, (XtPointer) XtOffset (Widget, core.colormap),
3320 sizeof (Colormap)}
3324 /* The address of this variable is returned by
3325 cvt_string_to_pixel. */
3327 static Pixel cvt_string_to_pixel_value;
3330 /* Convert a color name to a pixel color.
3332 DPY is the display we are working on.
3334 ARGS is an array of *NARGS XrmValue structures holding additional
3335 information about the widget for which the conversion takes place.
3336 The contents of this array are determined by the specification
3337 in cvt_string_to_pixel_args.
3339 FROM is a pointer to an XrmValue which points to the color name to
3340 convert. TO is an XrmValue in which to return the pixel color.
3342 CLOSURE_RET is a pointer to user-data, in which we record if
3343 we allocated the color or not.
3345 Value is True if successful, False otherwise. */
3347 static Boolean
3348 cvt_string_to_pixel (dpy, args, nargs, from, to, closure_ret)
3349 Display *dpy;
3350 XrmValue *args;
3351 Cardinal *nargs;
3352 XrmValue *from, *to;
3353 XtPointer *closure_ret;
3355 Screen *screen;
3356 Colormap cmap;
3357 Pixel pixel;
3358 String color_name;
3359 XColor color;
3361 if (*nargs != 2)
3363 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3364 "wrongParameters", "cvt_string_to_pixel",
3365 "XtToolkitError",
3366 "Screen and colormap args required", NULL, NULL);
3367 return False;
3370 screen = *(Screen **) args[0].addr;
3371 cmap = *(Colormap *) args[1].addr;
3372 color_name = (String) from->addr;
3374 if (strcmp (color_name, XtDefaultBackground) == 0)
3376 *closure_ret = (XtPointer) False;
3377 pixel = WhitePixelOfScreen (screen);
3379 else if (strcmp (color_name, XtDefaultForeground) == 0)
3381 *closure_ret = (XtPointer) False;
3382 pixel = BlackPixelOfScreen (screen);
3384 else if (XParseColor (dpy, cmap, color_name, &color)
3385 && x_alloc_nearest_color_1 (dpy, cmap, &color))
3387 pixel = color.pixel;
3388 *closure_ret = (XtPointer) True;
3390 else
3392 String params[1];
3393 Cardinal nparams = 1;
3395 params[0] = color_name;
3396 XtAppWarningMsg (XtDisplayToApplicationContext (dpy),
3397 "badValue", "cvt_string_to_pixel",
3398 "XtToolkitError", "Invalid color `%s'",
3399 params, &nparams);
3400 return False;
3403 if (to->addr != NULL)
3405 if (to->size < sizeof (Pixel))
3407 to->size = sizeof (Pixel);
3408 return False;
3411 *(Pixel *) to->addr = pixel;
3413 else
3415 cvt_string_to_pixel_value = pixel;
3416 to->addr = (XtPointer) &cvt_string_to_pixel_value;
3419 to->size = sizeof (Pixel);
3420 return True;
3424 /* Free a pixel color which was previously allocated via
3425 cvt_string_to_pixel. This is registered as the destructor
3426 for this type of resource via XtSetTypeConverter.
3428 APP is the application context in which we work.
3430 TO is a pointer to an XrmValue holding the color to free.
3431 CLOSURE is the value we stored in CLOSURE_RET for this color
3432 in cvt_string_to_pixel.
3434 ARGS and NARGS are like for cvt_string_to_pixel. */
3436 static void
3437 cvt_pixel_dtor (app, to, closure, args, nargs)
3438 XtAppContext app;
3439 XrmValuePtr to;
3440 XtPointer closure;
3441 XrmValuePtr args;
3442 Cardinal *nargs;
3444 if (*nargs != 2)
3446 XtAppWarningMsg (app, "wrongParameters", "cvt_pixel_dtor",
3447 "XtToolkitError",
3448 "Screen and colormap arguments required",
3449 NULL, NULL);
3451 else if (closure != NULL)
3453 /* We did allocate the pixel, so free it. */
3454 Screen *screen = *(Screen **) args[0].addr;
3455 Colormap cmap = *(Colormap *) args[1].addr;
3456 x_free_dpy_colors (DisplayOfScreen (screen), screen, cmap,
3457 (Pixel *) to->addr, 1);
3462 #endif /* USE_X_TOOLKIT */
3465 /* Value is an array of XColor structures for the contents of the
3466 color map of display DPY. Set *NCELLS to the size of the array.
3467 Note that this probably shouldn't be called for large color maps,
3468 say a 24-bit TrueColor map. */
3470 static const XColor *
3471 x_color_cells (dpy, ncells)
3472 Display *dpy;
3473 int *ncells;
3475 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3477 if (dpyinfo->color_cells == NULL)
3479 Screen *screen = dpyinfo->screen;
3480 int i;
3482 dpyinfo->ncolor_cells
3483 = XDisplayCells (dpy, XScreenNumberOfScreen (screen));
3484 dpyinfo->color_cells
3485 = (XColor *) xmalloc (dpyinfo->ncolor_cells
3486 * sizeof *dpyinfo->color_cells);
3488 for (i = 0; i < dpyinfo->ncolor_cells; ++i)
3489 dpyinfo->color_cells[i].pixel = i;
3491 XQueryColors (dpy, dpyinfo->cmap,
3492 dpyinfo->color_cells, dpyinfo->ncolor_cells);
3495 *ncells = dpyinfo->ncolor_cells;
3496 return dpyinfo->color_cells;
3500 /* On frame F, translate pixel colors to RGB values for the NCOLORS
3501 colors in COLORS. Use cached information, if available. */
3503 void
3504 x_query_colors (f, colors, ncolors)
3505 struct frame *f;
3506 XColor *colors;
3507 int ncolors;
3509 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3511 if (dpyinfo->color_cells)
3513 int i;
3514 for (i = 0; i < ncolors; ++i)
3516 unsigned long pixel = colors[i].pixel;
3517 xassert (pixel < dpyinfo->ncolor_cells);
3518 xassert (dpyinfo->color_cells[pixel].pixel == pixel);
3519 colors[i] = dpyinfo->color_cells[pixel];
3522 else
3523 XQueryColors (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), colors, ncolors);
3527 /* On frame F, translate pixel color to RGB values for the color in
3528 COLOR. Use cached information, if available. */
3530 void
3531 x_query_color (f, color)
3532 struct frame *f;
3533 XColor *color;
3535 x_query_colors (f, color, 1);
3539 /* Allocate the color COLOR->pixel on DISPLAY, colormap CMAP. If an
3540 exact match can't be allocated, try the nearest color available.
3541 Value is non-zero if successful. Set *COLOR to the color
3542 allocated. */
3544 static int
3545 x_alloc_nearest_color_1 (dpy, cmap, color)
3546 Display *dpy;
3547 Colormap cmap;
3548 XColor *color;
3550 int rc;
3552 rc = XAllocColor (dpy, cmap, color);
3553 if (rc == 0)
3555 /* If we got to this point, the colormap is full, so we're going
3556 to try to get the next closest color. The algorithm used is
3557 a least-squares matching, which is what X uses for closest
3558 color matching with StaticColor visuals. */
3559 int nearest, i;
3560 unsigned long nearest_delta = ~0;
3561 int ncells;
3562 const XColor *cells = x_color_cells (dpy, &ncells);
3564 for (nearest = i = 0; i < ncells; ++i)
3566 long dred = (color->red >> 8) - (cells[i].red >> 8);
3567 long dgreen = (color->green >> 8) - (cells[i].green >> 8);
3568 long dblue = (color->blue >> 8) - (cells[i].blue >> 8);
3569 unsigned long delta = dred * dred + dgreen * dgreen + dblue * dblue;
3571 if (delta < nearest_delta)
3573 nearest = i;
3574 nearest_delta = delta;
3578 color->red = cells[nearest].red;
3579 color->green = cells[nearest].green;
3580 color->blue = cells[nearest].blue;
3581 rc = XAllocColor (dpy, cmap, color);
3583 else
3585 /* If allocation succeeded, and the allocated pixel color is not
3586 equal to a cached pixel color recorded earlier, there was a
3587 change in the colormap, so clear the color cache. */
3588 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
3589 XColor *cached_color;
3591 if (dpyinfo->color_cells
3592 && (cached_color = &dpyinfo->color_cells[color->pixel],
3593 (cached_color->red != color->red
3594 || cached_color->blue != color->blue
3595 || cached_color->green != color->green)))
3597 xfree (dpyinfo->color_cells);
3598 dpyinfo->color_cells = NULL;
3599 dpyinfo->ncolor_cells = 0;
3603 #ifdef DEBUG_X_COLORS
3604 if (rc)
3605 register_color (color->pixel);
3606 #endif /* DEBUG_X_COLORS */
3608 return rc;
3612 /* Allocate the color COLOR->pixel on frame F, colormap CMAP. If an
3613 exact match can't be allocated, try the nearest color available.
3614 Value is non-zero if successful. Set *COLOR to the color
3615 allocated. */
3618 x_alloc_nearest_color (f, cmap, color)
3619 struct frame *f;
3620 Colormap cmap;
3621 XColor *color;
3623 gamma_correct (f, color);
3624 return x_alloc_nearest_color_1 (FRAME_X_DISPLAY (f), cmap, color);
3628 /* Allocate color PIXEL on frame F. PIXEL must already be allocated.
3629 It's necessary to do this instead of just using PIXEL directly to
3630 get color reference counts right. */
3632 unsigned long
3633 x_copy_color (f, pixel)
3634 struct frame *f;
3635 unsigned long pixel;
3637 XColor color;
3639 color.pixel = pixel;
3640 BLOCK_INPUT;
3641 x_query_color (f, &color);
3642 XAllocColor (FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f), &color);
3643 UNBLOCK_INPUT;
3644 #ifdef DEBUG_X_COLORS
3645 register_color (pixel);
3646 #endif
3647 return color.pixel;
3651 /* Allocate color PIXEL on display DPY. PIXEL must already be allocated.
3652 It's necessary to do this instead of just using PIXEL directly to
3653 get color reference counts right. */
3655 unsigned long
3656 x_copy_dpy_color (dpy, cmap, pixel)
3657 Display *dpy;
3658 Colormap cmap;
3659 unsigned long pixel;
3661 XColor color;
3663 color.pixel = pixel;
3664 BLOCK_INPUT;
3665 XQueryColor (dpy, cmap, &color);
3666 XAllocColor (dpy, cmap, &color);
3667 UNBLOCK_INPUT;
3668 #ifdef DEBUG_X_COLORS
3669 register_color (pixel);
3670 #endif
3671 return color.pixel;
3675 /* Brightness beyond which a color won't have its highlight brightness
3676 boosted.
3678 Nominally, highlight colors for `3d' faces are calculated by
3679 brightening an object's color by a constant scale factor, but this
3680 doesn't yield good results for dark colors, so for colors who's
3681 brightness is less than this value (on a scale of 0-65535) have an
3682 use an additional additive factor.
3684 The value here is set so that the default menu-bar/mode-line color
3685 (grey75) will not have its highlights changed at all. */
3686 #define HIGHLIGHT_COLOR_DARK_BOOST_LIMIT 48000
3689 /* Allocate a color which is lighter or darker than *PIXEL by FACTOR
3690 or DELTA. Try a color with RGB values multiplied by FACTOR first.
3691 If this produces the same color as PIXEL, try a color where all RGB
3692 values have DELTA added. Return the allocated color in *PIXEL.
3693 DISPLAY is the X display, CMAP is the colormap to operate on.
3694 Value is non-zero if successful. */
3696 static int
3697 x_alloc_lighter_color (f, display, cmap, pixel, factor, delta)
3698 struct frame *f;
3699 Display *display;
3700 Colormap cmap;
3701 unsigned long *pixel;
3702 double factor;
3703 int delta;
3705 XColor color, new;
3706 long bright;
3707 int success_p;
3709 /* Get RGB color values. */
3710 color.pixel = *pixel;
3711 x_query_color (f, &color);
3713 /* Change RGB values by specified FACTOR. Avoid overflow! */
3714 xassert (factor >= 0);
3715 new.red = min (0xffff, factor * color.red);
3716 new.green = min (0xffff, factor * color.green);
3717 new.blue = min (0xffff, factor * color.blue);
3719 /* Calculate brightness of COLOR. */
3720 bright = (2 * color.red + 3 * color.green + color.blue) / 6;
3722 /* We only boost colors that are darker than
3723 HIGHLIGHT_COLOR_DARK_BOOST_LIMIT. */
3724 if (bright < HIGHLIGHT_COLOR_DARK_BOOST_LIMIT)
3725 /* Make an additive adjustment to NEW, because it's dark enough so
3726 that scaling by FACTOR alone isn't enough. */
3728 /* How far below the limit this color is (0 - 1, 1 being darker). */
3729 double dimness = 1 - (double)bright / HIGHLIGHT_COLOR_DARK_BOOST_LIMIT;
3730 /* The additive adjustment. */
3731 int min_delta = delta * dimness * factor / 2;
3733 if (factor < 1)
3735 new.red = max (0, new.red - min_delta);
3736 new.green = max (0, new.green - min_delta);
3737 new.blue = max (0, new.blue - min_delta);
3739 else
3741 new.red = min (0xffff, min_delta + new.red);
3742 new.green = min (0xffff, min_delta + new.green);
3743 new.blue = min (0xffff, min_delta + new.blue);
3747 /* Try to allocate the color. */
3748 success_p = x_alloc_nearest_color (f, cmap, &new);
3749 if (success_p)
3751 if (new.pixel == *pixel)
3753 /* If we end up with the same color as before, try adding
3754 delta to the RGB values. */
3755 x_free_colors (f, &new.pixel, 1);
3757 new.red = min (0xffff, delta + color.red);
3758 new.green = min (0xffff, delta + color.green);
3759 new.blue = min (0xffff, delta + color.blue);
3760 success_p = x_alloc_nearest_color (f, cmap, &new);
3762 else
3763 success_p = 1;
3764 *pixel = new.pixel;
3767 return success_p;
3771 /* Set up the foreground color for drawing relief lines of glyph
3772 string S. RELIEF is a pointer to a struct relief containing the GC
3773 with which lines will be drawn. Use a color that is FACTOR or
3774 DELTA lighter or darker than the relief's background which is found
3775 in S->f->output_data.x->relief_background. If such a color cannot
3776 be allocated, use DEFAULT_PIXEL, instead. */
3778 static void
3779 x_setup_relief_color (f, relief, factor, delta, default_pixel)
3780 struct frame *f;
3781 struct relief *relief;
3782 double factor;
3783 int delta;
3784 unsigned long default_pixel;
3786 XGCValues xgcv;
3787 struct x_output *di = f->output_data.x;
3788 unsigned long mask = GCForeground | GCLineWidth | GCGraphicsExposures;
3789 unsigned long pixel;
3790 unsigned long background = di->relief_background;
3791 Colormap cmap = FRAME_X_COLORMAP (f);
3792 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
3793 Display *dpy = FRAME_X_DISPLAY (f);
3795 xgcv.graphics_exposures = False;
3796 xgcv.line_width = 1;
3798 /* Free previously allocated color. The color cell will be reused
3799 when it has been freed as many times as it was allocated, so this
3800 doesn't affect faces using the same colors. */
3801 if (relief->gc
3802 && relief->allocated_p)
3804 x_free_colors (f, &relief->pixel, 1);
3805 relief->allocated_p = 0;
3808 /* Allocate new color. */
3809 xgcv.foreground = default_pixel;
3810 pixel = background;
3811 if (dpyinfo->n_planes != 1
3812 && x_alloc_lighter_color (f, dpy, cmap, &pixel, factor, delta))
3814 relief->allocated_p = 1;
3815 xgcv.foreground = relief->pixel = pixel;
3818 if (relief->gc == 0)
3820 xgcv.stipple = dpyinfo->gray;
3821 mask |= GCStipple;
3822 relief->gc = XCreateGC (dpy, FRAME_X_WINDOW (f), mask, &xgcv);
3824 else
3825 XChangeGC (dpy, relief->gc, mask, &xgcv);
3829 /* Set up colors for the relief lines around glyph string S. */
3831 static void
3832 x_setup_relief_colors (s)
3833 struct glyph_string *s;
3835 struct x_output *di = s->f->output_data.x;
3836 unsigned long color;
3838 if (s->face->use_box_color_for_shadows_p)
3839 color = s->face->box_color;
3840 else if (s->first_glyph->type == IMAGE_GLYPH
3841 && s->img->pixmap
3842 && !IMAGE_BACKGROUND_TRANSPARENT (s->img, s->f, 0))
3843 color = IMAGE_BACKGROUND (s->img, s->f, 0);
3844 else
3846 XGCValues xgcv;
3848 /* Get the background color of the face. */
3849 XGetGCValues (s->display, s->gc, GCBackground, &xgcv);
3850 color = xgcv.background;
3853 if (di->white_relief.gc == 0
3854 || color != di->relief_background)
3856 di->relief_background = color;
3857 x_setup_relief_color (s->f, &di->white_relief, 1.2, 0x8000,
3858 WHITE_PIX_DEFAULT (s->f));
3859 x_setup_relief_color (s->f, &di->black_relief, 0.6, 0x4000,
3860 BLACK_PIX_DEFAULT (s->f));
3865 /* Draw a relief on frame F inside the rectangle given by LEFT_X,
3866 TOP_Y, RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the relief
3867 to draw, it must be >= 0. RAISED_P non-zero means draw a raised
3868 relief. LEFT_P non-zero means draw a relief on the left side of
3869 the rectangle. RIGHT_P non-zero means draw a relief on the right
3870 side of the rectangle. CLIP_RECT is the clipping rectangle to use
3871 when drawing. */
3873 static void
3874 x_draw_relief_rect (f, left_x, top_y, right_x, bottom_y, width,
3875 raised_p, left_p, right_p, clip_rect)
3876 struct frame *f;
3877 int left_x, top_y, right_x, bottom_y, left_p, right_p, raised_p;
3878 XRectangle *clip_rect;
3880 Display *dpy = FRAME_X_DISPLAY (f);
3881 Window window = FRAME_X_WINDOW (f);
3882 int i;
3883 GC gc;
3885 if (raised_p)
3886 gc = f->output_data.x->white_relief.gc;
3887 else
3888 gc = f->output_data.x->black_relief.gc;
3889 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3891 /* Top. */
3892 for (i = 0; i < width; ++i)
3893 XDrawLine (dpy, window, gc,
3894 left_x + i * left_p, top_y + i,
3895 right_x + 1 - i * right_p, top_y + i);
3897 /* Left. */
3898 if (left_p)
3899 for (i = 0; i < width; ++i)
3900 XDrawLine (dpy, window, gc,
3901 left_x + i, top_y + i, left_x + i, bottom_y - i + 1);
3903 XSetClipMask (dpy, gc, None);
3904 if (raised_p)
3905 gc = f->output_data.x->black_relief.gc;
3906 else
3907 gc = f->output_data.x->white_relief.gc;
3908 XSetClipRectangles (dpy, gc, 0, 0, clip_rect, 1, Unsorted);
3910 /* Bottom. */
3911 for (i = 0; i < width; ++i)
3912 XDrawLine (dpy, window, gc,
3913 left_x + i * left_p, bottom_y - i,
3914 right_x + 1 - i * right_p, bottom_y - i);
3916 /* Right. */
3917 if (right_p)
3918 for (i = 0; i < width; ++i)
3919 XDrawLine (dpy, window, gc,
3920 right_x - i, top_y + i + 1, right_x - i, bottom_y - i);
3922 XSetClipMask (dpy, gc, None);
3926 /* Draw a box on frame F inside the rectangle given by LEFT_X, TOP_Y,
3927 RIGHT_X, and BOTTOM_Y. WIDTH is the thickness of the lines to
3928 draw, it must be >= 0. LEFT_P non-zero means draw a line on the
3929 left side of the rectangle. RIGHT_P non-zero means draw a line
3930 on the right side of the rectangle. CLIP_RECT is the clipping
3931 rectangle to use when drawing. */
3933 static void
3934 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
3935 left_p, right_p, clip_rect)
3936 struct glyph_string *s;
3937 int left_x, top_y, right_x, bottom_y, left_p, right_p;
3938 XRectangle *clip_rect;
3940 XGCValues xgcv;
3942 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
3943 XSetForeground (s->display, s->gc, s->face->box_color);
3944 XSetClipRectangles (s->display, s->gc, 0, 0, clip_rect, 1, Unsorted);
3946 /* Top. */
3947 XFillRectangle (s->display, s->window, s->gc,
3948 left_x, top_y, right_x - left_x + 1, width);
3950 /* Left. */
3951 if (left_p)
3952 XFillRectangle (s->display, s->window, s->gc,
3953 left_x, top_y, width, bottom_y - top_y + 1);
3955 /* Bottom. */
3956 XFillRectangle (s->display, s->window, s->gc,
3957 left_x, bottom_y - width + 1, right_x - left_x + 1, width);
3959 /* Right. */
3960 if (right_p)
3961 XFillRectangle (s->display, s->window, s->gc,
3962 right_x - width + 1, top_y, width, bottom_y - top_y + 1);
3964 XSetForeground (s->display, s->gc, xgcv.foreground);
3965 XSetClipMask (s->display, s->gc, None);
3969 /* Draw a box around glyph string S. */
3971 static void
3972 x_draw_glyph_string_box (s)
3973 struct glyph_string *s;
3975 int width, left_x, right_x, top_y, bottom_y, last_x, raised_p;
3976 int left_p, right_p;
3977 struct glyph *last_glyph;
3978 XRectangle clip_rect;
3980 last_x = window_box_right (s->w, s->area);
3981 if (s->row->full_width_p
3982 && !s->w->pseudo_window_p)
3984 last_x += FRAME_X_RIGHT_FRINGE_WIDTH (s->f);
3985 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (s->f))
3986 last_x += FRAME_SCROLL_BAR_WIDTH (s->f) * CANON_X_UNIT (s->f);
3989 /* The glyph that may have a right box line. */
3990 last_glyph = (s->cmp || s->img
3991 ? s->first_glyph
3992 : s->first_glyph + s->nchars - 1);
3994 width = abs (s->face->box_line_width);
3995 raised_p = s->face->box == FACE_RAISED_BOX;
3996 left_x = s->x;
3997 right_x = (s->row->full_width_p && s->extends_to_end_of_line_p
3998 ? last_x - 1
3999 : min (last_x, s->x + s->background_width) - 1);
4000 top_y = s->y;
4001 bottom_y = top_y + s->height - 1;
4003 left_p = (s->first_glyph->left_box_line_p
4004 || (s->hl == DRAW_MOUSE_FACE
4005 && (s->prev == NULL
4006 || s->prev->hl != s->hl)));
4007 right_p = (last_glyph->right_box_line_p
4008 || (s->hl == DRAW_MOUSE_FACE
4009 && (s->next == NULL
4010 || s->next->hl != s->hl)));
4012 x_get_glyph_string_clip_rect (s, &clip_rect);
4014 if (s->face->box == FACE_SIMPLE_BOX)
4015 x_draw_box_rect (s, left_x, top_y, right_x, bottom_y, width,
4016 left_p, right_p, &clip_rect);
4017 else
4019 x_setup_relief_colors (s);
4020 x_draw_relief_rect (s->f, left_x, top_y, right_x, bottom_y,
4021 width, raised_p, left_p, right_p, &clip_rect);
4026 /* Draw foreground of image glyph string S. */
4028 static void
4029 x_draw_image_foreground (s)
4030 struct glyph_string *s;
4032 int x;
4033 int y = s->ybase - image_ascent (s->img, s->face);
4035 /* If first glyph of S has a left box line, start drawing it to the
4036 right of that line. */
4037 if (s->face->box != FACE_NO_BOX
4038 && s->first_glyph->left_box_line_p)
4039 x = s->x + abs (s->face->box_line_width);
4040 else
4041 x = s->x;
4043 /* If there is a margin around the image, adjust x- and y-position
4044 by that margin. */
4045 x += s->img->hmargin;
4046 y += s->img->vmargin;
4048 if (s->img->pixmap)
4050 if (s->img->mask)
4052 /* We can't set both a clip mask and use XSetClipRectangles
4053 because the latter also sets a clip mask. We also can't
4054 trust on the shape extension to be available
4055 (XShapeCombineRegion). So, compute the rectangle to draw
4056 manually. */
4057 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4058 | GCFunction);
4059 XGCValues xgcv;
4060 XRectangle clip_rect, image_rect, r;
4062 xgcv.clip_mask = s->img->mask;
4063 xgcv.clip_x_origin = x;
4064 xgcv.clip_y_origin = y;
4065 xgcv.function = GXcopy;
4066 XChangeGC (s->display, s->gc, mask, &xgcv);
4068 x_get_glyph_string_clip_rect (s, &clip_rect);
4069 image_rect.x = x;
4070 image_rect.y = y;
4071 image_rect.width = s->img->width;
4072 image_rect.height = s->img->height;
4073 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4074 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4075 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4077 else
4079 XRectangle clip_rect, image_rect, r;
4081 x_get_glyph_string_clip_rect (s, &clip_rect);
4082 image_rect.x = x;
4083 image_rect.y = y;
4084 image_rect.width = s->img->width;
4085 image_rect.height = s->img->height;
4086 if (x_intersect_rectangles (&clip_rect, &image_rect, &r))
4087 XCopyArea (s->display, s->img->pixmap, s->window, s->gc,
4088 r.x - x, r.y - y, r.width, r.height, r.x, r.y);
4090 /* When the image has a mask, we can expect that at
4091 least part of a mouse highlight or a block cursor will
4092 be visible. If the image doesn't have a mask, make
4093 a block cursor visible by drawing a rectangle around
4094 the image. I believe it's looking better if we do
4095 nothing here for mouse-face. */
4096 if (s->hl == DRAW_CURSOR)
4097 XDrawRectangle (s->display, s->window, s->gc, x, y,
4098 s->img->width - 1, s->img->height - 1);
4101 else
4102 /* Draw a rectangle if image could not be loaded. */
4103 XDrawRectangle (s->display, s->window, s->gc, x, y,
4104 s->img->width - 1, s->img->height - 1);
4108 /* Draw a relief around the image glyph string S. */
4110 static void
4111 x_draw_image_relief (s)
4112 struct glyph_string *s;
4114 int x0, y0, x1, y1, thick, raised_p;
4115 XRectangle r;
4116 int x;
4117 int y = s->ybase - image_ascent (s->img, s->face);
4119 /* If first glyph of S has a left box line, start drawing it to the
4120 right of that line. */
4121 if (s->face->box != FACE_NO_BOX
4122 && s->first_glyph->left_box_line_p)
4123 x = s->x + abs (s->face->box_line_width);
4124 else
4125 x = s->x;
4127 /* If there is a margin around the image, adjust x- and y-position
4128 by that margin. */
4129 x += s->img->hmargin;
4130 y += s->img->vmargin;
4132 if (s->hl == DRAW_IMAGE_SUNKEN
4133 || s->hl == DRAW_IMAGE_RAISED)
4135 thick = tool_bar_button_relief >= 0 ? tool_bar_button_relief : DEFAULT_TOOL_BAR_BUTTON_RELIEF;
4136 raised_p = s->hl == DRAW_IMAGE_RAISED;
4138 else
4140 thick = abs (s->img->relief);
4141 raised_p = s->img->relief > 0;
4144 x0 = x - thick;
4145 y0 = y - thick;
4146 x1 = x + s->img->width + thick - 1;
4147 y1 = y + s->img->height + thick - 1;
4149 x_setup_relief_colors (s);
4150 x_get_glyph_string_clip_rect (s, &r);
4151 x_draw_relief_rect (s->f, x0, y0, x1, y1, thick, raised_p, 1, 1, &r);
4155 /* Draw the foreground of image glyph string S to PIXMAP. */
4157 static void
4158 x_draw_image_foreground_1 (s, pixmap)
4159 struct glyph_string *s;
4160 Pixmap pixmap;
4162 int x;
4163 int y = s->ybase - s->y - image_ascent (s->img, s->face);
4165 /* If first glyph of S has a left box line, start drawing it to the
4166 right of that line. */
4167 if (s->face->box != FACE_NO_BOX
4168 && s->first_glyph->left_box_line_p)
4169 x = abs (s->face->box_line_width);
4170 else
4171 x = 0;
4173 /* If there is a margin around the image, adjust x- and y-position
4174 by that margin. */
4175 x += s->img->hmargin;
4176 y += s->img->vmargin;
4178 if (s->img->pixmap)
4180 if (s->img->mask)
4182 /* We can't set both a clip mask and use XSetClipRectangles
4183 because the latter also sets a clip mask. We also can't
4184 trust on the shape extension to be available
4185 (XShapeCombineRegion). So, compute the rectangle to draw
4186 manually. */
4187 unsigned long mask = (GCClipMask | GCClipXOrigin | GCClipYOrigin
4188 | GCFunction);
4189 XGCValues xgcv;
4191 xgcv.clip_mask = s->img->mask;
4192 xgcv.clip_x_origin = x;
4193 xgcv.clip_y_origin = y;
4194 xgcv.function = GXcopy;
4195 XChangeGC (s->display, s->gc, mask, &xgcv);
4197 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4198 0, 0, s->img->width, s->img->height, x, y);
4199 XSetClipMask (s->display, s->gc, None);
4201 else
4203 XCopyArea (s->display, s->img->pixmap, pixmap, s->gc,
4204 0, 0, s->img->width, s->img->height, x, y);
4206 /* When the image has a mask, we can expect that at
4207 least part of a mouse highlight or a block cursor will
4208 be visible. If the image doesn't have a mask, make
4209 a block cursor visible by drawing a rectangle around
4210 the image. I believe it's looking better if we do
4211 nothing here for mouse-face. */
4212 if (s->hl == DRAW_CURSOR)
4213 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4214 s->img->width - 1, s->img->height - 1);
4217 else
4218 /* Draw a rectangle if image could not be loaded. */
4219 XDrawRectangle (s->display, pixmap, s->gc, x, y,
4220 s->img->width - 1, s->img->height - 1);
4224 /* Draw part of the background of glyph string S. X, Y, W, and H
4225 give the rectangle to draw. */
4227 static void
4228 x_draw_glyph_string_bg_rect (s, x, y, w, h)
4229 struct glyph_string *s;
4230 int x, y, w, h;
4232 if (s->stippled_p)
4234 /* Fill background with a stipple pattern. */
4235 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4236 XFillRectangle (s->display, s->window, s->gc, x, y, w, h);
4237 XSetFillStyle (s->display, s->gc, FillSolid);
4239 else
4240 x_clear_glyph_string_rect (s, x, y, w, h);
4244 /* Draw image glyph string S.
4246 s->y
4247 s->x +-------------------------
4248 | s->face->box
4250 | +-------------------------
4251 | | s->img->margin
4253 | | +-------------------
4254 | | | the image
4258 static void
4259 x_draw_image_glyph_string (s)
4260 struct glyph_string *s;
4262 int x, y;
4263 int box_line_hwidth = abs (s->face->box_line_width);
4264 int box_line_vwidth = max (s->face->box_line_width, 0);
4265 int height;
4266 Pixmap pixmap = None;
4268 height = s->height - 2 * box_line_vwidth;
4270 /* Fill background with face under the image. Do it only if row is
4271 taller than image or if image has a clip mask to reduce
4272 flickering. */
4273 s->stippled_p = s->face->stipple != 0;
4274 if (height > s->img->height
4275 || s->img->hmargin
4276 || s->img->vmargin
4277 || s->img->mask
4278 || s->img->pixmap == 0
4279 || s->width != s->background_width)
4281 if (box_line_hwidth && s->first_glyph->left_box_line_p)
4282 x = s->x + box_line_hwidth;
4283 else
4284 x = s->x;
4286 y = s->y + box_line_vwidth;
4288 if (s->img->mask)
4290 /* Create a pixmap as large as the glyph string. Fill it
4291 with the background color. Copy the image to it, using
4292 its mask. Copy the temporary pixmap to the display. */
4293 Screen *screen = FRAME_X_SCREEN (s->f);
4294 int depth = DefaultDepthOfScreen (screen);
4296 /* Create a pixmap as large as the glyph string. */
4297 pixmap = XCreatePixmap (s->display, s->window,
4298 s->background_width,
4299 s->height, depth);
4301 /* Don't clip in the following because we're working on the
4302 pixmap. */
4303 XSetClipMask (s->display, s->gc, None);
4305 /* Fill the pixmap with the background color/stipple. */
4306 if (s->stippled_p)
4308 /* Fill background with a stipple pattern. */
4309 XSetFillStyle (s->display, s->gc, FillOpaqueStippled);
4310 XFillRectangle (s->display, pixmap, s->gc,
4311 0, 0, s->background_width, s->height);
4312 XSetFillStyle (s->display, s->gc, FillSolid);
4314 else
4316 XGCValues xgcv;
4317 XGetGCValues (s->display, s->gc, GCForeground | GCBackground,
4318 &xgcv);
4319 XSetForeground (s->display, s->gc, xgcv.background);
4320 XFillRectangle (s->display, pixmap, s->gc,
4321 0, 0, s->background_width, s->height);
4322 XSetForeground (s->display, s->gc, xgcv.foreground);
4325 else
4326 x_draw_glyph_string_bg_rect (s, x, y, s->background_width, height);
4328 s->background_filled_p = 1;
4331 /* Draw the foreground. */
4332 if (pixmap != None)
4334 x_draw_image_foreground_1 (s, pixmap);
4335 x_set_glyph_string_clipping (s);
4336 XCopyArea (s->display, pixmap, s->window, s->gc,
4337 0, 0, s->background_width, s->height, s->x, s->y);
4338 XFreePixmap (s->display, pixmap);
4340 else
4341 x_draw_image_foreground (s);
4343 /* If we must draw a relief around the image, do it. */
4344 if (s->img->relief
4345 || s->hl == DRAW_IMAGE_RAISED
4346 || s->hl == DRAW_IMAGE_SUNKEN)
4347 x_draw_image_relief (s);
4351 /* Draw stretch glyph string S. */
4353 static void
4354 x_draw_stretch_glyph_string (s)
4355 struct glyph_string *s;
4357 xassert (s->first_glyph->type == STRETCH_GLYPH);
4358 s->stippled_p = s->face->stipple != 0;
4360 if (s->hl == DRAW_CURSOR
4361 && !x_stretch_cursor_p)
4363 /* If `x-stretch-block-cursor' is nil, don't draw a block cursor
4364 as wide as the stretch glyph. */
4365 int width = min (CANON_X_UNIT (s->f), s->background_width);
4367 /* Draw cursor. */
4368 x_draw_glyph_string_bg_rect (s, s->x, s->y, width, s->height);
4370 /* Clear rest using the GC of the original non-cursor face. */
4371 if (width < s->background_width)
4373 int x = s->x + width, y = s->y;
4374 int w = s->background_width - width, h = s->height;
4375 XRectangle r;
4376 GC gc;
4378 if (s->row->mouse_face_p
4379 && cursor_in_mouse_face_p (s->w))
4381 x_set_mouse_face_gc (s);
4382 gc = s->gc;
4384 else
4385 gc = s->face->gc;
4387 x_get_glyph_string_clip_rect (s, &r);
4388 XSetClipRectangles (s->display, gc, 0, 0, &r, 1, Unsorted);
4390 if (s->face->stipple)
4392 /* Fill background with a stipple pattern. */
4393 XSetFillStyle (s->display, gc, FillOpaqueStippled);
4394 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4395 XSetFillStyle (s->display, gc, FillSolid);
4397 else
4399 XGCValues xgcv;
4400 XGetGCValues (s->display, gc, GCForeground | GCBackground, &xgcv);
4401 XSetForeground (s->display, gc, xgcv.background);
4402 XFillRectangle (s->display, s->window, gc, x, y, w, h);
4403 XSetForeground (s->display, gc, xgcv.foreground);
4407 else if (!s->background_filled_p)
4408 x_draw_glyph_string_bg_rect (s, s->x, s->y, s->background_width,
4409 s->height);
4411 s->background_filled_p = 1;
4415 /* Draw glyph string S. */
4417 static void
4418 x_draw_glyph_string (s)
4419 struct glyph_string *s;
4421 int relief_drawn_p = 0;
4423 /* If S draws into the background of its successor, draw the
4424 background of the successor first so that S can draw into it.
4425 This makes S->next use XDrawString instead of XDrawImageString. */
4426 if (s->next && s->right_overhang && !s->for_overlaps_p)
4428 xassert (s->next->img == NULL);
4429 x_set_glyph_string_gc (s->next);
4430 x_set_glyph_string_clipping (s->next);
4431 x_draw_glyph_string_background (s->next, 1);
4434 /* Set up S->gc, set clipping and draw S. */
4435 x_set_glyph_string_gc (s);
4437 /* Draw relief (if any) in advance for char/composition so that the
4438 glyph string can be drawn over it. */
4439 if (!s->for_overlaps_p
4440 && s->face->box != FACE_NO_BOX
4441 && (s->first_glyph->type == CHAR_GLYPH
4442 || s->first_glyph->type == COMPOSITE_GLYPH))
4445 x_set_glyph_string_clipping (s);
4446 x_draw_glyph_string_background (s, 1);
4447 x_draw_glyph_string_box (s);
4448 x_set_glyph_string_clipping (s);
4449 relief_drawn_p = 1;
4451 else
4452 x_set_glyph_string_clipping (s);
4454 switch (s->first_glyph->type)
4456 case IMAGE_GLYPH:
4457 x_draw_image_glyph_string (s);
4458 break;
4460 case STRETCH_GLYPH:
4461 x_draw_stretch_glyph_string (s);
4462 break;
4464 case CHAR_GLYPH:
4465 if (s->for_overlaps_p)
4466 s->background_filled_p = 1;
4467 else
4468 x_draw_glyph_string_background (s, 0);
4469 x_draw_glyph_string_foreground (s);
4470 break;
4472 case COMPOSITE_GLYPH:
4473 if (s->for_overlaps_p || s->gidx > 0)
4474 s->background_filled_p = 1;
4475 else
4476 x_draw_glyph_string_background (s, 1);
4477 x_draw_composite_glyph_string_foreground (s);
4478 break;
4480 default:
4481 abort ();
4484 if (!s->for_overlaps_p)
4486 /* Draw underline. */
4487 if (s->face->underline_p)
4489 unsigned long tem, h;
4490 int y;
4492 /* Get the underline thickness. Default is 1 pixel. */
4493 if (!XGetFontProperty (s->font, XA_UNDERLINE_THICKNESS, &h))
4494 h = 1;
4496 /* Get the underline position. This is the recommended
4497 vertical offset in pixels from the baseline to the top of
4498 the underline. This is a signed value according to the
4499 specs, and its default is
4501 ROUND ((maximum descent) / 2), with
4502 ROUND(x) = floor (x + 0.5) */
4504 if (x_use_underline_position_properties
4505 && XGetFontProperty (s->font, XA_UNDERLINE_POSITION, &tem))
4506 y = s->ybase + (long) tem;
4507 else if (s->face->font)
4508 y = s->ybase + (s->face->font->max_bounds.descent + 1) / 2;
4509 else
4510 y = s->y + s->height - h;
4512 if (s->face->underline_defaulted_p)
4513 XFillRectangle (s->display, s->window, s->gc,
4514 s->x, y, s->width, h);
4515 else
4517 XGCValues xgcv;
4518 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4519 XSetForeground (s->display, s->gc, s->face->underline_color);
4520 XFillRectangle (s->display, s->window, s->gc,
4521 s->x, y, s->width, h);
4522 XSetForeground (s->display, s->gc, xgcv.foreground);
4526 /* Draw overline. */
4527 if (s->face->overline_p)
4529 unsigned long dy = 0, h = 1;
4531 if (s->face->overline_color_defaulted_p)
4532 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4533 s->width, h);
4534 else
4536 XGCValues xgcv;
4537 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4538 XSetForeground (s->display, s->gc, s->face->overline_color);
4539 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4540 s->width, h);
4541 XSetForeground (s->display, s->gc, xgcv.foreground);
4545 /* Draw strike-through. */
4546 if (s->face->strike_through_p)
4548 unsigned long h = 1;
4549 unsigned long dy = (s->height - h) / 2;
4551 if (s->face->strike_through_color_defaulted_p)
4552 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4553 s->width, h);
4554 else
4556 XGCValues xgcv;
4557 XGetGCValues (s->display, s->gc, GCForeground, &xgcv);
4558 XSetForeground (s->display, s->gc, s->face->strike_through_color);
4559 XFillRectangle (s->display, s->window, s->gc, s->x, s->y + dy,
4560 s->width, h);
4561 XSetForeground (s->display, s->gc, xgcv.foreground);
4565 /* Draw relief if not yet drawn. */
4566 if (!relief_drawn_p && s->face->box != FACE_NO_BOX)
4567 x_draw_glyph_string_box (s);
4570 /* Reset clipping. */
4571 XSetClipMask (s->display, s->gc, None);
4575 static int x_fill_composite_glyph_string P_ ((struct glyph_string *,
4576 struct face **, int));
4579 /* Fill glyph string S with composition components specified by S->cmp.
4581 FACES is an array of faces for all components of this composition.
4582 S->gidx is the index of the first component for S.
4583 OVERLAPS_P non-zero means S should draw the foreground only, and
4584 use its physical height for clipping.
4586 Value is the index of a component not in S. */
4588 static int
4589 x_fill_composite_glyph_string (s, faces, overlaps_p)
4590 struct glyph_string *s;
4591 struct face **faces;
4592 int overlaps_p;
4594 int i;
4596 xassert (s);
4598 s->for_overlaps_p = overlaps_p;
4600 s->face = faces[s->gidx];
4601 s->font = s->face->font;
4602 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4604 /* For all glyphs of this composition, starting at the offset
4605 S->gidx, until we reach the end of the definition or encounter a
4606 glyph that requires the different face, add it to S. */
4607 ++s->nchars;
4608 for (i = s->gidx + 1; i < s->cmp->glyph_len && faces[i] == s->face; ++i)
4609 ++s->nchars;
4611 /* All glyph strings for the same composition has the same width,
4612 i.e. the width set for the first component of the composition. */
4614 s->width = s->first_glyph->pixel_width;
4616 /* If the specified font could not be loaded, use the frame's
4617 default font, but record the fact that we couldn't load it in
4618 the glyph string so that we can draw rectangles for the
4619 characters of the glyph string. */
4620 if (s->font == NULL)
4622 s->font_not_found_p = 1;
4623 s->font = FRAME_FONT (s->f);
4626 /* Adjust base line for subscript/superscript text. */
4627 s->ybase += s->first_glyph->voffset;
4629 xassert (s->face && s->face->gc);
4631 /* This glyph string must always be drawn with 16-bit functions. */
4632 s->two_byte_p = 1;
4634 return s->gidx + s->nchars;
4638 /* Fill glyph string S from a sequence of character glyphs.
4640 FACE_ID is the face id of the string. START is the index of the
4641 first glyph to consider, END is the index of the last + 1.
4642 OVERLAPS_P non-zero means S should draw the foreground only, and
4643 use its physical height for clipping.
4645 Value is the index of the first glyph not in S. */
4647 static int
4648 x_fill_glyph_string (s, face_id, start, end, overlaps_p)
4649 struct glyph_string *s;
4650 int face_id;
4651 int start, end, overlaps_p;
4653 struct glyph *glyph, *last;
4654 int voffset;
4655 int glyph_not_available_p;
4657 xassert (s->f == XFRAME (s->w->frame));
4658 xassert (s->nchars == 0);
4659 xassert (start >= 0 && end > start);
4661 s->for_overlaps_p = overlaps_p,
4662 glyph = s->row->glyphs[s->area] + start;
4663 last = s->row->glyphs[s->area] + end;
4664 voffset = glyph->voffset;
4666 glyph_not_available_p = glyph->glyph_not_available_p;
4668 while (glyph < last
4669 && glyph->type == CHAR_GLYPH
4670 && glyph->voffset == voffset
4671 /* Same face id implies same font, nowadays. */
4672 && glyph->face_id == face_id
4673 && glyph->glyph_not_available_p == glyph_not_available_p)
4675 int two_byte_p;
4677 s->face = x_get_glyph_face_and_encoding (s->f, glyph,
4678 s->char2b + s->nchars,
4679 &two_byte_p);
4680 s->two_byte_p = two_byte_p;
4681 ++s->nchars;
4682 xassert (s->nchars <= end - start);
4683 s->width += glyph->pixel_width;
4684 ++glyph;
4687 s->font = s->face->font;
4688 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4690 /* If the specified font could not be loaded, use the frame's font,
4691 but record the fact that we couldn't load it in
4692 S->font_not_found_p so that we can draw rectangles for the
4693 characters of the glyph string. */
4694 if (s->font == NULL || glyph_not_available_p)
4696 s->font_not_found_p = 1;
4697 s->font = FRAME_FONT (s->f);
4700 /* Adjust base line for subscript/superscript text. */
4701 s->ybase += voffset;
4703 xassert (s->face && s->face->gc);
4704 return glyph - s->row->glyphs[s->area];
4708 /* Fill glyph string S from image glyph S->first_glyph. */
4710 static void
4711 x_fill_image_glyph_string (s)
4712 struct glyph_string *s;
4714 xassert (s->first_glyph->type == IMAGE_GLYPH);
4715 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
4716 xassert (s->img);
4717 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
4718 s->font = s->face->font;
4719 s->width = s->first_glyph->pixel_width;
4721 /* Adjust base line for subscript/superscript text. */
4722 s->ybase += s->first_glyph->voffset;
4726 /* Fill glyph string S from a sequence of stretch glyphs.
4728 ROW is the glyph row in which the glyphs are found, AREA is the
4729 area within the row. START is the index of the first glyph to
4730 consider, END is the index of the last + 1.
4732 Value is the index of the first glyph not in S. */
4734 static int
4735 x_fill_stretch_glyph_string (s, row, area, start, end)
4736 struct glyph_string *s;
4737 struct glyph_row *row;
4738 enum glyph_row_area area;
4739 int start, end;
4741 struct glyph *glyph, *last;
4742 int voffset, face_id;
4744 xassert (s->first_glyph->type == STRETCH_GLYPH);
4746 glyph = s->row->glyphs[s->area] + start;
4747 last = s->row->glyphs[s->area] + end;
4748 face_id = glyph->face_id;
4749 s->face = FACE_FROM_ID (s->f, face_id);
4750 s->font = s->face->font;
4751 s->font_info = FONT_INFO_FROM_ID (s->f, s->face->font_info_id);
4752 s->width = glyph->pixel_width;
4753 voffset = glyph->voffset;
4755 for (++glyph;
4756 (glyph < last
4757 && glyph->type == STRETCH_GLYPH
4758 && glyph->voffset == voffset
4759 && glyph->face_id == face_id);
4760 ++glyph)
4761 s->width += glyph->pixel_width;
4763 /* Adjust base line for subscript/superscript text. */
4764 s->ybase += voffset;
4766 /* The case that face->gc == 0 is handled when drawing the glyph
4767 string by calling PREPARE_FACE_FOR_DISPLAY. */
4768 xassert (s->face);
4769 return glyph - s->row->glyphs[s->area];
4773 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
4774 of XChar2b structures for S; it can't be allocated in
4775 x_init_glyph_string because it must be allocated via `alloca'. W
4776 is the window on which S is drawn. ROW and AREA are the glyph row
4777 and area within the row from which S is constructed. START is the
4778 index of the first glyph structure covered by S. HL is a
4779 face-override for drawing S. */
4781 static void
4782 x_init_glyph_string (s, char2b, w, row, area, start, hl)
4783 struct glyph_string *s;
4784 XChar2b *char2b;
4785 struct window *w;
4786 struct glyph_row *row;
4787 enum glyph_row_area area;
4788 int start;
4789 enum draw_glyphs_face hl;
4791 bzero (s, sizeof *s);
4792 s->w = w;
4793 s->f = XFRAME (w->frame);
4794 s->display = FRAME_X_DISPLAY (s->f);
4795 s->window = FRAME_X_WINDOW (s->f);
4796 s->char2b = char2b;
4797 s->hl = hl;
4798 s->row = row;
4799 s->area = area;
4800 s->first_glyph = row->glyphs[area] + start;
4801 s->height = row->height;
4802 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
4804 /* Display the internal border below the tool-bar window. */
4805 if (s->w == XWINDOW (s->f->tool_bar_window))
4806 s->y -= s->f->output_data.x->internal_border_width;
4808 s->ybase = s->y + row->ascent;
4812 /* Set background width of glyph string S. START is the index of the
4813 first glyph following S. LAST_X is the right-most x-position + 1
4814 in the drawing area. */
4816 static INLINE void
4817 x_set_glyph_string_background_width (s, start, last_x)
4818 struct glyph_string *s;
4819 int start;
4820 int last_x;
4822 /* If the face of this glyph string has to be drawn to the end of
4823 the drawing area, set S->extends_to_end_of_line_p. */
4824 struct face *default_face = FACE_FROM_ID (s->f, DEFAULT_FACE_ID);
4826 if (start == s->row->used[s->area]
4827 && s->area == TEXT_AREA
4828 && ((s->hl == DRAW_NORMAL_TEXT
4829 && (s->row->fill_line_p
4830 || s->face->background != default_face->background
4831 || s->face->stipple != default_face->stipple
4832 || s->row->mouse_face_p))
4833 || s->hl == DRAW_MOUSE_FACE
4834 || ((s->hl == DRAW_IMAGE_RAISED || s->hl == DRAW_IMAGE_SUNKEN)
4835 && s->row->fill_line_p)))
4836 s->extends_to_end_of_line_p = 1;
4838 /* If S extends its face to the end of the line, set its
4839 background_width to the distance to the right edge of the drawing
4840 area. */
4841 if (s->extends_to_end_of_line_p)
4842 s->background_width = last_x - s->x + 1;
4843 else
4844 s->background_width = s->width;
4848 /* Add a glyph string for a stretch glyph to the list of strings
4849 between HEAD and TAIL. START is the index of the stretch glyph in
4850 row area AREA of glyph row ROW. END is the index of the last glyph
4851 in that glyph row area. X is the current output position assigned
4852 to the new glyph string constructed. HL overrides that face of the
4853 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4854 is the right-most x-position of the drawing area. */
4856 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
4857 and below -- keep them on one line. */
4858 #define BUILD_STRETCH_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4859 do \
4861 s = (struct glyph_string *) alloca (sizeof *s); \
4862 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4863 START = x_fill_stretch_glyph_string (s, ROW, AREA, START, END); \
4864 x_append_glyph_string (&HEAD, &TAIL, s); \
4865 s->x = (X); \
4867 while (0)
4870 /* Add a glyph string for an image glyph to the list of strings
4871 between HEAD and TAIL. START is the index of the image glyph in
4872 row area AREA of glyph row ROW. END is the index of the last glyph
4873 in that glyph row area. X is the current output position assigned
4874 to the new glyph string constructed. HL overrides that face of the
4875 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
4876 is the right-most x-position of the drawing area. */
4878 #define BUILD_IMAGE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X) \
4879 do \
4881 s = (struct glyph_string *) alloca (sizeof *s); \
4882 x_init_glyph_string (s, NULL, W, ROW, AREA, START, HL); \
4883 x_fill_image_glyph_string (s); \
4884 x_append_glyph_string (&HEAD, &TAIL, s); \
4885 ++START; \
4886 s->x = (X); \
4888 while (0)
4891 /* Add a glyph string for a sequence of character glyphs to the list
4892 of strings between HEAD and TAIL. START is the index of the first
4893 glyph in row area AREA of glyph row ROW that is part of the new
4894 glyph string. END is the index of the last glyph in that glyph row
4895 area. X is the current output position assigned to the new glyph
4896 string constructed. HL overrides that face of the glyph; e.g. it
4897 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
4898 right-most x-position of the drawing area. */
4900 #define BUILD_CHAR_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4901 do \
4903 int c, face_id; \
4904 XChar2b *char2b; \
4906 c = (ROW)->glyphs[AREA][START].u.ch; \
4907 face_id = (ROW)->glyphs[AREA][START].face_id; \
4909 s = (struct glyph_string *) alloca (sizeof *s); \
4910 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
4911 x_init_glyph_string (s, char2b, W, ROW, AREA, START, HL); \
4912 x_append_glyph_string (&HEAD, &TAIL, s); \
4913 s->x = (X); \
4914 START = x_fill_glyph_string (s, face_id, START, END, \
4915 OVERLAPS_P); \
4917 while (0)
4920 /* Add a glyph string for a composite sequence to the list of strings
4921 between HEAD and TAIL. START is the index of the first glyph in
4922 row area AREA of glyph row ROW that is part of the new glyph
4923 string. END is the index of the last glyph in that glyph row area.
4924 X is the current output position assigned to the new glyph string
4925 constructed. HL overrides that face of the glyph; e.g. it is
4926 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
4927 x-position of the drawing area. */
4929 #define BUILD_COMPOSITE_GLYPH_STRING(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4930 do { \
4931 int cmp_id = (ROW)->glyphs[AREA][START].u.cmp_id; \
4932 int face_id = (ROW)->glyphs[AREA][START].face_id; \
4933 struct face *base_face = FACE_FROM_ID (XFRAME (w->frame), face_id); \
4934 struct composition *cmp = composition_table[cmp_id]; \
4935 int glyph_len = cmp->glyph_len; \
4936 XChar2b *char2b; \
4937 struct face **faces; \
4938 struct glyph_string *first_s = NULL; \
4939 int n; \
4941 base_face = base_face->ascii_face; \
4942 char2b = (XChar2b *) alloca ((sizeof *char2b) * glyph_len); \
4943 faces = (struct face **) alloca ((sizeof *faces) * glyph_len); \
4944 /* At first, fill in `char2b' and `faces'. */ \
4945 for (n = 0; n < glyph_len; n++) \
4947 int c = COMPOSITION_GLYPH (cmp, n); \
4948 int this_face_id = FACE_FOR_CHAR (XFRAME (w->frame), base_face, c); \
4949 faces[n] = FACE_FROM_ID (XFRAME (w->frame), this_face_id); \
4950 x_get_char_face_and_encoding (XFRAME (w->frame), c, \
4951 this_face_id, char2b + n, 1); \
4954 /* Make glyph_strings for each glyph sequence that is drawable by \
4955 the same face, and append them to HEAD/TAIL. */ \
4956 for (n = 0; n < cmp->glyph_len;) \
4958 s = (struct glyph_string *) alloca (sizeof *s); \
4959 x_init_glyph_string (s, char2b + n, W, ROW, AREA, START, HL); \
4960 x_append_glyph_string (&(HEAD), &(TAIL), s); \
4961 s->cmp = cmp; \
4962 s->gidx = n; \
4963 s->x = (X); \
4965 if (n == 0) \
4966 first_s = s; \
4968 n = x_fill_composite_glyph_string (s, faces, OVERLAPS_P); \
4971 ++START; \
4972 s = first_s; \
4973 } while (0)
4976 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
4977 of AREA of glyph row ROW on window W between indices START and END.
4978 HL overrides the face for drawing glyph strings, e.g. it is
4979 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
4980 x-positions of the drawing area.
4982 This is an ugly monster macro construct because we must use alloca
4983 to allocate glyph strings (because x_draw_glyphs can be called
4984 asynchronously). */
4986 #define BUILD_GLYPH_STRINGS(W, ROW, AREA, START, END, HEAD, TAIL, HL, X, LAST_X, OVERLAPS_P) \
4987 do \
4989 HEAD = TAIL = NULL; \
4990 while (START < END) \
4992 struct glyph *first_glyph = (ROW)->glyphs[AREA] + START; \
4993 switch (first_glyph->type) \
4995 case CHAR_GLYPH: \
4996 BUILD_CHAR_GLYPH_STRINGS (W, ROW, AREA, START, END, HEAD, \
4997 TAIL, HL, X, LAST_X, \
4998 OVERLAPS_P); \
4999 break; \
5001 case COMPOSITE_GLYPH: \
5002 BUILD_COMPOSITE_GLYPH_STRING (W, ROW, AREA, START, END, \
5003 HEAD, TAIL, HL, X, LAST_X,\
5004 OVERLAPS_P); \
5005 break; \
5007 case STRETCH_GLYPH: \
5008 BUILD_STRETCH_GLYPH_STRING (W, ROW, AREA, START, END, \
5009 HEAD, TAIL, HL, X, LAST_X); \
5010 break; \
5012 case IMAGE_GLYPH: \
5013 BUILD_IMAGE_GLYPH_STRING (W, ROW, AREA, START, END, HEAD, \
5014 TAIL, HL, X, LAST_X); \
5015 break; \
5017 default: \
5018 abort (); \
5021 x_set_glyph_string_background_width (s, START, LAST_X); \
5022 (X) += s->width; \
5025 while (0)
5028 /* Draw glyphs between START and END in AREA of ROW on window W,
5029 starting at x-position X. X is relative to AREA in W. HL is a
5030 face-override with the following meaning:
5032 DRAW_NORMAL_TEXT draw normally
5033 DRAW_CURSOR draw in cursor face
5034 DRAW_MOUSE_FACE draw in mouse face.
5035 DRAW_INVERSE_VIDEO draw in mode line face
5036 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
5037 DRAW_IMAGE_RAISED draw an image with a raised relief around it
5039 If OVERLAPS_P is non-zero, draw only the foreground of characters
5040 and clip to the physical height of ROW.
5042 Value is the x-position reached, relative to AREA of W. */
5044 static int
5045 x_draw_glyphs (w, x, row, area, start, end, hl, overlaps_p)
5046 struct window *w;
5047 int x;
5048 struct glyph_row *row;
5049 enum glyph_row_area area;
5050 int start, end;
5051 enum draw_glyphs_face hl;
5052 int overlaps_p;
5054 struct glyph_string *head, *tail;
5055 struct glyph_string *s;
5056 int last_x, area_width;
5057 int x_reached;
5058 int i, j;
5060 /* Let's rather be paranoid than getting a SEGV. */
5061 end = min (end, row->used[area]);
5062 start = max (0, start);
5063 start = min (end, start);
5065 /* Translate X to frame coordinates. Set last_x to the right
5066 end of the drawing area. */
5067 if (row->full_width_p)
5069 /* X is relative to the left edge of W, without scroll bars
5070 or fringes. */
5071 struct frame *f = XFRAME (w->frame);
5072 int window_left_x = WINDOW_LEFT_MARGIN (w) * CANON_X_UNIT (f);
5074 x += window_left_x;
5075 area_width = XFASTINT (w->width) * CANON_X_UNIT (f);
5076 last_x = window_left_x + area_width;
5078 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
5080 int width = FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5081 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
5082 last_x += width;
5083 else
5084 x -= width;
5087 x += FRAME_INTERNAL_BORDER_WIDTH (f);
5088 last_x += FRAME_INTERNAL_BORDER_WIDTH (f);
5090 else
5092 x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, x);
5093 area_width = window_box_width (w, area);
5094 last_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, area, area_width);
5097 /* Build a doubly-linked list of glyph_string structures between
5098 head and tail from what we have to draw. Note that the macro
5099 BUILD_GLYPH_STRINGS will modify its start parameter. That's
5100 the reason we use a separate variable `i'. */
5101 i = start;
5102 BUILD_GLYPH_STRINGS (w, row, area, i, end, head, tail, hl, x, last_x,
5103 overlaps_p);
5104 if (tail)
5105 x_reached = tail->x + tail->background_width;
5106 else
5107 x_reached = x;
5109 /* If there are any glyphs with lbearing < 0 or rbearing > width in
5110 the row, redraw some glyphs in front or following the glyph
5111 strings built above. */
5112 if (head && !overlaps_p && row->contains_overlapping_glyphs_p)
5114 int dummy_x = 0;
5115 struct glyph_string *h, *t;
5117 /* Compute overhangs for all glyph strings. */
5118 for (s = head; s; s = s->next)
5119 x_compute_glyph_string_overhangs (s);
5121 /* Prepend glyph strings for glyphs in front of the first glyph
5122 string that are overwritten because of the first glyph
5123 string's left overhang. The background of all strings
5124 prepended must be drawn because the first glyph string
5125 draws over it. */
5126 i = x_left_overwritten (head);
5127 if (i >= 0)
5129 j = i;
5130 BUILD_GLYPH_STRINGS (w, row, area, j, start, h, t,
5131 DRAW_NORMAL_TEXT, dummy_x, last_x,
5132 overlaps_p);
5133 start = i;
5134 x_compute_overhangs_and_x (t, head->x, 1);
5135 x_prepend_glyph_string_lists (&head, &tail, h, t);
5138 /* Prepend glyph strings for glyphs in front of the first glyph
5139 string that overwrite that glyph string because of their
5140 right overhang. For these strings, only the foreground must
5141 be drawn, because it draws over the glyph string at `head'.
5142 The background must not be drawn because this would overwrite
5143 right overhangs of preceding glyphs for which no glyph
5144 strings exist. */
5145 i = x_left_overwriting (head);
5146 if (i >= 0)
5148 BUILD_GLYPH_STRINGS (w, row, area, i, start, h, t,
5149 DRAW_NORMAL_TEXT, dummy_x, last_x,
5150 overlaps_p);
5151 for (s = h; s; s = s->next)
5152 s->background_filled_p = 1;
5153 x_compute_overhangs_and_x (t, head->x, 1);
5154 x_prepend_glyph_string_lists (&head, &tail, h, t);
5157 /* Append glyphs strings for glyphs following the last glyph
5158 string tail that are overwritten by tail. The background of
5159 these strings has to be drawn because tail's foreground draws
5160 over it. */
5161 i = x_right_overwritten (tail);
5162 if (i >= 0)
5164 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5165 DRAW_NORMAL_TEXT, x, last_x,
5166 overlaps_p);
5167 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5168 x_append_glyph_string_lists (&head, &tail, h, t);
5171 /* Append glyph strings for glyphs following the last glyph
5172 string tail that overwrite tail. The foreground of such
5173 glyphs has to be drawn because it writes into the background
5174 of tail. The background must not be drawn because it could
5175 paint over the foreground of following glyphs. */
5176 i = x_right_overwriting (tail);
5177 if (i >= 0)
5179 BUILD_GLYPH_STRINGS (w, row, area, end, i, h, t,
5180 DRAW_NORMAL_TEXT, x, last_x,
5181 overlaps_p);
5182 for (s = h; s; s = s->next)
5183 s->background_filled_p = 1;
5184 x_compute_overhangs_and_x (h, tail->x + tail->width, 0);
5185 x_append_glyph_string_lists (&head, &tail, h, t);
5189 /* Draw all strings. */
5190 for (s = head; s; s = s->next)
5191 x_draw_glyph_string (s);
5193 if (area == TEXT_AREA
5194 && !row->full_width_p
5195 /* When drawing overlapping rows, only the glyph strings'
5196 foreground is drawn, which doesn't erase a cursor
5197 completely. */
5198 && !overlaps_p)
5200 int x0 = head ? head->x : x;
5201 int x1 = tail ? tail->x + tail->background_width : x;
5203 x0 = FRAME_TO_WINDOW_PIXEL_X (w, x0);
5204 x1 = FRAME_TO_WINDOW_PIXEL_X (w, x1);
5206 if (XFASTINT (w->left_margin_width) != 0)
5208 int left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
5209 x0 -= left_area_width;
5210 x1 -= left_area_width;
5213 notice_overwritten_cursor (w, area, x0, x1,
5214 row->y, MATRIX_ROW_BOTTOM_Y (row));
5217 /* Value is the x-position up to which drawn, relative to AREA of W.
5218 This doesn't include parts drawn because of overhangs. */
5219 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
5220 if (!row->full_width_p)
5222 if (area > LEFT_MARGIN_AREA && XFASTINT (w->left_margin_width) != 0)
5223 x_reached -= window_box_width (w, LEFT_MARGIN_AREA);
5224 if (area > TEXT_AREA)
5225 x_reached -= window_box_width (w, TEXT_AREA);
5228 return x_reached;
5232 /* Fix the display of area AREA of overlapping row ROW in window W. */
5234 static void
5235 x_fix_overlapping_area (w, row, area)
5236 struct window *w;
5237 struct glyph_row *row;
5238 enum glyph_row_area area;
5240 int i, x;
5242 BLOCK_INPUT;
5244 if (area == LEFT_MARGIN_AREA)
5245 x = 0;
5246 else if (area == TEXT_AREA)
5247 x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5248 else
5249 x = (window_box_width (w, LEFT_MARGIN_AREA)
5250 + window_box_width (w, TEXT_AREA));
5252 for (i = 0; i < row->used[area];)
5254 if (row->glyphs[area][i].overlaps_vertically_p)
5256 int start = i, start_x = x;
5260 x += row->glyphs[area][i].pixel_width;
5261 ++i;
5263 while (i < row->used[area]
5264 && row->glyphs[area][i].overlaps_vertically_p);
5266 x_draw_glyphs (w, start_x, row, area, start, i,
5267 DRAW_NORMAL_TEXT, 1);
5269 else
5271 x += row->glyphs[area][i].pixel_width;
5272 ++i;
5276 UNBLOCK_INPUT;
5280 /* Output LEN glyphs starting at START at the nominal cursor position.
5281 Advance the nominal cursor over the text. The global variable
5282 updated_window contains the window being updated, updated_row is
5283 the glyph row being updated, and updated_area is the area of that
5284 row being updated. */
5286 static void
5287 x_write_glyphs (start, len)
5288 struct glyph *start;
5289 int len;
5291 int x, hpos;
5293 xassert (updated_window && updated_row);
5294 BLOCK_INPUT;
5296 /* Write glyphs. */
5298 hpos = start - updated_row->glyphs[updated_area];
5299 x = x_draw_glyphs (updated_window, output_cursor.x,
5300 updated_row, updated_area,
5301 hpos, hpos + len,
5302 DRAW_NORMAL_TEXT, 0);
5304 UNBLOCK_INPUT;
5306 /* Advance the output cursor. */
5307 output_cursor.hpos += len;
5308 output_cursor.x = x;
5312 /* Insert LEN glyphs from START at the nominal cursor position. */
5314 static void
5315 x_insert_glyphs (start, len)
5316 struct glyph *start;
5317 register int len;
5319 struct frame *f;
5320 struct window *w;
5321 int line_height, shift_by_width, shifted_region_width;
5322 struct glyph_row *row;
5323 struct glyph *glyph;
5324 int frame_x, frame_y, hpos;
5326 xassert (updated_window && updated_row);
5327 BLOCK_INPUT;
5328 w = updated_window;
5329 f = XFRAME (WINDOW_FRAME (w));
5331 /* Get the height of the line we are in. */
5332 row = updated_row;
5333 line_height = row->height;
5335 /* Get the width of the glyphs to insert. */
5336 shift_by_width = 0;
5337 for (glyph = start; glyph < start + len; ++glyph)
5338 shift_by_width += glyph->pixel_width;
5340 /* Get the width of the region to shift right. */
5341 shifted_region_width = (window_box_width (w, updated_area)
5342 - output_cursor.x
5343 - shift_by_width);
5345 /* Shift right. */
5346 frame_x = window_box_left (w, updated_area) + output_cursor.x;
5347 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
5348 XCopyArea (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5349 f->output_data.x->normal_gc,
5350 frame_x, frame_y,
5351 shifted_region_width, line_height,
5352 frame_x + shift_by_width, frame_y);
5354 /* Write the glyphs. */
5355 hpos = start - row->glyphs[updated_area];
5356 x_draw_glyphs (w, output_cursor.x, row, updated_area, hpos, hpos + len,
5357 DRAW_NORMAL_TEXT, 0);
5359 /* Advance the output cursor. */
5360 output_cursor.hpos += len;
5361 output_cursor.x += shift_by_width;
5362 UNBLOCK_INPUT;
5366 /* Delete N glyphs at the nominal cursor position. Not implemented
5367 for X frames. */
5369 static void
5370 x_delete_glyphs (n)
5371 register int n;
5373 abort ();
5377 /* Like XClearArea, but check that WIDTH and HEIGHT are reasonable.
5378 If they are <= 0, this is probably an error. */
5380 void
5381 x_clear_area (dpy, window, x, y, width, height, exposures)
5382 Display *dpy;
5383 Window window;
5384 int x, y;
5385 int width, height;
5386 int exposures;
5388 xassert (width > 0 && height > 0);
5389 XClearArea (dpy, window, x, y, width, height, exposures);
5393 /* Erase the current text line from the nominal cursor position
5394 (inclusive) to pixel column TO_X (exclusive). The idea is that
5395 everything from TO_X onward is already erased.
5397 TO_X is a pixel position relative to updated_area of
5398 updated_window. TO_X == -1 means clear to the end of this area. */
5400 static void
5401 x_clear_end_of_line (to_x)
5402 int to_x;
5404 struct frame *f;
5405 struct window *w = updated_window;
5406 int max_x, min_y, max_y;
5407 int from_x, from_y, to_y;
5409 xassert (updated_window && updated_row);
5410 f = XFRAME (w->frame);
5412 if (updated_row->full_width_p)
5414 max_x = XFASTINT (w->width) * CANON_X_UNIT (f);
5415 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f)
5416 && !w->pseudo_window_p)
5417 max_x += FRAME_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
5419 else
5420 max_x = window_box_width (w, updated_area);
5421 max_y = window_text_bottom_y (w);
5423 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
5424 of window. For TO_X > 0, truncate to end of drawing area. */
5425 if (to_x == 0)
5426 return;
5427 else if (to_x < 0)
5428 to_x = max_x;
5429 else
5430 to_x = min (to_x, max_x);
5432 to_y = min (max_y, output_cursor.y + updated_row->height);
5434 /* Notice if the cursor will be cleared by this operation. */
5435 if (!updated_row->full_width_p)
5436 notice_overwritten_cursor (w, updated_area,
5437 output_cursor.x, -1,
5438 updated_row->y,
5439 MATRIX_ROW_BOTTOM_Y (updated_row));
5441 from_x = output_cursor.x;
5443 /* Translate to frame coordinates. */
5444 if (updated_row->full_width_p)
5446 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
5447 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
5449 else
5451 from_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, from_x);
5452 to_x = WINDOW_AREA_TO_FRAME_PIXEL_X (w, updated_area, to_x);
5455 min_y = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
5456 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
5457 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
5459 /* Prevent inadvertently clearing to end of the X window. */
5460 if (to_x > from_x && to_y > from_y)
5462 BLOCK_INPUT;
5463 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5464 from_x, from_y, to_x - from_x, to_y - from_y,
5465 False);
5466 UNBLOCK_INPUT;
5471 /* Clear entire frame. If updating_frame is non-null, clear that
5472 frame. Otherwise clear the selected frame. */
5474 static void
5475 x_clear_frame ()
5477 struct frame *f;
5479 if (updating_frame)
5480 f = updating_frame;
5481 else
5482 f = SELECTED_FRAME ();
5484 /* Clearing the frame will erase any cursor, so mark them all as no
5485 longer visible. */
5486 mark_window_cursors_off (XWINDOW (FRAME_ROOT_WINDOW (f)));
5487 output_cursor.hpos = output_cursor.vpos = 0;
5488 output_cursor.x = -1;
5490 /* We don't set the output cursor here because there will always
5491 follow an explicit cursor_to. */
5492 BLOCK_INPUT;
5493 XClearWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
5495 /* We have to clear the scroll bars, too. If we have changed
5496 colors or something like that, then they should be notified. */
5497 x_scroll_bar_clear (f);
5499 XFlush (FRAME_X_DISPLAY (f));
5500 UNBLOCK_INPUT;
5505 /* Invert the middle quarter of the frame for .15 sec. */
5507 /* We use the select system call to do the waiting, so we have to make
5508 sure it's available. If it isn't, we just won't do visual bells. */
5510 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5513 /* Subtract the `struct timeval' values X and Y, storing the result in
5514 *RESULT. Return 1 if the difference is negative, otherwise 0. */
5516 static int
5517 timeval_subtract (result, x, y)
5518 struct timeval *result, x, y;
5520 /* Perform the carry for the later subtraction by updating y. This
5521 is safer because on some systems the tv_sec member is unsigned. */
5522 if (x.tv_usec < y.tv_usec)
5524 int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
5525 y.tv_usec -= 1000000 * nsec;
5526 y.tv_sec += nsec;
5529 if (x.tv_usec - y.tv_usec > 1000000)
5531 int nsec = (y.tv_usec - x.tv_usec) / 1000000;
5532 y.tv_usec += 1000000 * nsec;
5533 y.tv_sec -= nsec;
5536 /* Compute the time remaining to wait. tv_usec is certainly
5537 positive. */
5538 result->tv_sec = x.tv_sec - y.tv_sec;
5539 result->tv_usec = x.tv_usec - y.tv_usec;
5541 /* Return indication of whether the result should be considered
5542 negative. */
5543 return x.tv_sec < y.tv_sec;
5546 void
5547 XTflash (f)
5548 struct frame *f;
5550 BLOCK_INPUT;
5553 GC gc;
5555 /* Create a GC that will use the GXxor function to flip foreground
5556 pixels into background pixels. */
5558 XGCValues values;
5560 values.function = GXxor;
5561 values.foreground = (f->output_data.x->foreground_pixel
5562 ^ f->output_data.x->background_pixel);
5564 gc = XCreateGC (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
5565 GCFunction | GCForeground, &values);
5569 /* Get the height not including a menu bar widget. */
5570 int height = CHAR_TO_PIXEL_HEIGHT (f, FRAME_HEIGHT (f));
5571 /* Height of each line to flash. */
5572 int flash_height = FRAME_LINE_HEIGHT (f);
5573 /* These will be the left and right margins of the rectangles. */
5574 int flash_left = FRAME_INTERNAL_BORDER_WIDTH (f);
5575 int flash_right = PIXEL_WIDTH (f) - FRAME_INTERNAL_BORDER_WIDTH (f);
5577 int width;
5579 /* Don't flash the area between a scroll bar and the frame
5580 edge it is next to. */
5581 switch (FRAME_VERTICAL_SCROLL_BAR_TYPE (f))
5583 case vertical_scroll_bar_left:
5584 flash_left += VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5585 break;
5587 case vertical_scroll_bar_right:
5588 flash_right -= VERTICAL_SCROLL_BAR_WIDTH_TRIM;
5589 break;
5591 default:
5592 break;
5595 width = flash_right - flash_left;
5597 /* If window is tall, flash top and bottom line. */
5598 if (height > 3 * FRAME_LINE_HEIGHT (f))
5600 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5601 flash_left,
5602 (FRAME_INTERNAL_BORDER_WIDTH (f)
5603 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5604 width, flash_height);
5605 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5606 flash_left,
5607 (height - flash_height
5608 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5609 width, flash_height);
5611 else
5612 /* If it is short, flash it all. */
5613 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5614 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5615 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5617 x_flush (f);
5620 struct timeval wakeup;
5622 EMACS_GET_TIME (wakeup);
5624 /* Compute time to wait until, propagating carry from usecs. */
5625 wakeup.tv_usec += 150000;
5626 wakeup.tv_sec += (wakeup.tv_usec / 1000000);
5627 wakeup.tv_usec %= 1000000;
5629 /* Keep waiting until past the time wakeup or any input gets
5630 available. */
5631 while (! detect_input_pending ())
5633 struct timeval current;
5634 struct timeval timeout;
5636 EMACS_GET_TIME (current);
5638 /* Break if result would be negative. */
5639 if (timeval_subtract (&current, wakeup, current))
5640 break;
5642 /* How long `select' should wait. */
5643 timeout.tv_sec = 0;
5644 timeout.tv_usec = 10000;
5646 /* Try to wait that long--but we might wake up sooner. */
5647 select (0, NULL, NULL, NULL, &timeout);
5651 /* If window is tall, flash top and bottom line. */
5652 if (height > 3 * FRAME_LINE_HEIGHT (f))
5654 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5655 flash_left,
5656 (FRAME_INTERNAL_BORDER_WIDTH (f)
5657 + FRAME_TOOL_BAR_LINES (f) * CANON_Y_UNIT (f)),
5658 width, flash_height);
5659 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5660 flash_left,
5661 (height - flash_height
5662 - FRAME_INTERNAL_BORDER_WIDTH (f)),
5663 width, flash_height);
5665 else
5666 /* If it is short, flash it all. */
5667 XFillRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), gc,
5668 flash_left, FRAME_INTERNAL_BORDER_WIDTH (f),
5669 width, height - 2 * FRAME_INTERNAL_BORDER_WIDTH (f));
5671 XFreeGC (FRAME_X_DISPLAY (f), gc);
5672 x_flush (f);
5676 UNBLOCK_INPUT;
5679 #endif /* defined (HAVE_TIMEVAL) && defined (HAVE_SELECT) */
5682 /* Make audible bell. */
5684 void
5685 XTring_bell ()
5687 struct frame *f = SELECTED_FRAME ();
5689 if (FRAME_X_DISPLAY (f))
5691 #if defined (HAVE_TIMEVAL) && defined (HAVE_SELECT)
5692 if (visible_bell)
5693 XTflash (f);
5694 else
5695 #endif
5697 BLOCK_INPUT;
5698 XBell (FRAME_X_DISPLAY (f), 0);
5699 XFlush (FRAME_X_DISPLAY (f));
5700 UNBLOCK_INPUT;
5706 /* Specify how many text lines, from the top of the window,
5707 should be affected by insert-lines and delete-lines operations.
5708 This, and those operations, are used only within an update
5709 that is bounded by calls to x_update_begin and x_update_end. */
5711 static void
5712 XTset_terminal_window (n)
5713 register int n;
5715 /* This function intentionally left blank. */
5720 /***********************************************************************
5721 Line Dance
5722 ***********************************************************************/
5724 /* Perform an insert-lines or delete-lines operation, inserting N
5725 lines or deleting -N lines at vertical position VPOS. */
5727 static void
5728 x_ins_del_lines (vpos, n)
5729 int vpos, n;
5731 abort ();
5735 /* Scroll part of the display as described by RUN. */
5737 static void
5738 x_scroll_run (w, run)
5739 struct window *w;
5740 struct run *run;
5742 struct frame *f = XFRAME (w->frame);
5743 int x, y, width, height, from_y, to_y, bottom_y;
5745 /* Get frame-relative bounding box of the text display area of W,
5746 without mode lines. Include in this box the left and right
5747 fringe of W. */
5748 window_box (w, -1, &x, &y, &width, &height);
5749 width += FRAME_X_FRINGE_WIDTH (f);
5750 x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
5752 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->current_y);
5753 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, run->desired_y);
5754 bottom_y = y + height;
5756 if (to_y < from_y)
5758 /* Scrolling up. Make sure we don't copy part of the mode
5759 line at the bottom. */
5760 if (from_y + run->height > bottom_y)
5761 height = bottom_y - from_y;
5762 else
5763 height = run->height;
5765 else
5767 /* Scolling down. Make sure we don't copy over the mode line.
5768 at the bottom. */
5769 if (to_y + run->height > bottom_y)
5770 height = bottom_y - to_y;
5771 else
5772 height = run->height;
5775 BLOCK_INPUT;
5777 /* Cursor off. Will be switched on again in x_update_window_end. */
5778 updated_window = w;
5779 x_clear_cursor (w);
5781 XCopyArea (FRAME_X_DISPLAY (f),
5782 FRAME_X_WINDOW (f), FRAME_X_WINDOW (f),
5783 f->output_data.x->normal_gc,
5784 x, from_y,
5785 width, height,
5786 x, to_y);
5788 UNBLOCK_INPUT;
5793 /***********************************************************************
5794 Exposure Events
5795 ***********************************************************************/
5797 /* Redisplay an exposed area of frame F. X and Y are the upper-left
5798 corner of the exposed rectangle. W and H are width and height of
5799 the exposed area. All are pixel values. W or H zero means redraw
5800 the entire frame. */
5802 static void
5803 expose_frame (f, x, y, w, h)
5804 struct frame *f;
5805 int x, y, w, h;
5807 XRectangle r;
5808 int mouse_face_overwritten_p = 0;
5810 TRACE ((stderr, "expose_frame "));
5812 /* No need to redraw if frame will be redrawn soon. */
5813 if (FRAME_GARBAGED_P (f))
5815 TRACE ((stderr, " garbaged\n"));
5816 return;
5819 /* If basic faces haven't been realized yet, there is no point in
5820 trying to redraw anything. This can happen when we get an expose
5821 event while Emacs is starting, e.g. by moving another window. */
5822 if (FRAME_FACE_CACHE (f) == NULL
5823 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
5825 TRACE ((stderr, " no faces\n"));
5826 return;
5829 if (w == 0 || h == 0)
5831 r.x = r.y = 0;
5832 r.width = CANON_X_UNIT (f) * f->width;
5833 r.height = CANON_Y_UNIT (f) * f->height;
5835 else
5837 r.x = x;
5838 r.y = y;
5839 r.width = w;
5840 r.height = h;
5843 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
5844 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
5846 if (WINDOWP (f->tool_bar_window))
5847 mouse_face_overwritten_p
5848 |= expose_window (XWINDOW (f->tool_bar_window), &r);
5850 #ifndef USE_X_TOOLKIT
5851 if (WINDOWP (f->menu_bar_window))
5852 mouse_face_overwritten_p
5853 |= expose_window (XWINDOW (f->menu_bar_window), &r);
5854 #endif /* not USE_X_TOOLKIT */
5856 /* Some window managers support a focus-follows-mouse style with
5857 delayed raising of frames. Imagine a partially obscured frame,
5858 and moving the mouse into partially obscured mouse-face on that
5859 frame. The visible part of the mouse-face will be highlighted,
5860 then the WM raises the obscured frame. With at least one WM, KDE
5861 2.1, Emacs is not getting any event for the raising of the frame
5862 (even tried with SubstructureRedirectMask), only Expose events.
5863 These expose events will draw text normally, i.e. not
5864 highlighted. Which means we must redo the highlight here.
5865 Subsume it under ``we love X''. --gerd 2001-08-15 */
5866 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
5868 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
5869 if (f == dpyinfo->mouse_face_mouse_frame)
5871 int x = dpyinfo->mouse_face_mouse_x;
5872 int y = dpyinfo->mouse_face_mouse_y;
5873 clear_mouse_face (dpyinfo);
5874 note_mouse_highlight (f, x, y);
5880 /* Redraw (parts) of all windows in the window tree rooted at W that
5881 intersect R. R contains frame pixel coordinates. Value is
5882 non-zero if the exposure overwrites mouse-face. */
5884 static int
5885 expose_window_tree (w, r)
5886 struct window *w;
5887 XRectangle *r;
5889 struct frame *f = XFRAME (w->frame);
5890 int mouse_face_overwritten_p = 0;
5892 while (w && !FRAME_GARBAGED_P (f))
5894 if (!NILP (w->hchild))
5895 mouse_face_overwritten_p
5896 |= expose_window_tree (XWINDOW (w->hchild), r);
5897 else if (!NILP (w->vchild))
5898 mouse_face_overwritten_p
5899 |= expose_window_tree (XWINDOW (w->vchild), r);
5900 else
5901 mouse_face_overwritten_p |= expose_window (w, r);
5903 w = NILP (w->next) ? NULL : XWINDOW (w->next);
5906 return mouse_face_overwritten_p;
5910 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
5911 which intersects rectangle R. R is in window-relative coordinates. */
5913 static void
5914 expose_area (w, row, r, area)
5915 struct window *w;
5916 struct glyph_row *row;
5917 XRectangle *r;
5918 enum glyph_row_area area;
5920 struct glyph *first = row->glyphs[area];
5921 struct glyph *end = row->glyphs[area] + row->used[area];
5922 struct glyph *last;
5923 int first_x, start_x, x;
5925 if (area == TEXT_AREA && row->fill_line_p)
5926 /* If row extends face to end of line write the whole line. */
5927 x_draw_glyphs (w, 0, row, area, 0, row->used[area],
5928 DRAW_NORMAL_TEXT, 0);
5929 else
5931 /* Set START_X to the window-relative start position for drawing glyphs of
5932 AREA. The first glyph of the text area can be partially visible.
5933 The first glyphs of other areas cannot. */
5934 if (area == LEFT_MARGIN_AREA)
5935 start_x = 0;
5936 else if (area == TEXT_AREA)
5937 start_x = row->x + window_box_width (w, LEFT_MARGIN_AREA);
5938 else
5939 start_x = (window_box_width (w, LEFT_MARGIN_AREA)
5940 + window_box_width (w, TEXT_AREA));
5941 x = start_x;
5943 /* Find the first glyph that must be redrawn. */
5944 while (first < end
5945 && x + first->pixel_width < r->x)
5947 x += first->pixel_width;
5948 ++first;
5951 /* Find the last one. */
5952 last = first;
5953 first_x = x;
5954 while (last < end
5955 && x < r->x + r->width)
5957 x += last->pixel_width;
5958 ++last;
5961 /* Repaint. */
5962 if (last > first)
5963 x_draw_glyphs (w, first_x - start_x, row, area,
5964 first - row->glyphs[area],
5965 last - row->glyphs[area],
5966 DRAW_NORMAL_TEXT, 0);
5971 /* Redraw the parts of the glyph row ROW on window W intersecting
5972 rectangle R. R is in window-relative coordinates. Value is
5973 non-zero if mouse-face was overwritten. */
5975 static int
5976 expose_line (w, row, r)
5977 struct window *w;
5978 struct glyph_row *row;
5979 XRectangle *r;
5981 xassert (row->enabled_p);
5983 if (row->mode_line_p || w->pseudo_window_p)
5984 x_draw_glyphs (w, 0, row, TEXT_AREA, 0, row->used[TEXT_AREA],
5985 DRAW_NORMAL_TEXT, 0);
5986 else
5988 if (row->used[LEFT_MARGIN_AREA])
5989 expose_area (w, row, r, LEFT_MARGIN_AREA);
5990 if (row->used[TEXT_AREA])
5991 expose_area (w, row, r, TEXT_AREA);
5992 if (row->used[RIGHT_MARGIN_AREA])
5993 expose_area (w, row, r, RIGHT_MARGIN_AREA);
5994 x_draw_row_fringe_bitmaps (w, row);
5997 return row->mouse_face_p;
6001 /* Return non-zero if W's cursor intersects rectangle R. */
6003 static int
6004 x_phys_cursor_in_rect_p (w, r)
6005 struct window *w;
6006 XRectangle *r;
6008 XRectangle cr, result;
6009 struct glyph *cursor_glyph;
6011 cursor_glyph = get_phys_cursor_glyph (w);
6012 if (cursor_glyph)
6014 cr.x = w->phys_cursor.x;
6015 cr.y = w->phys_cursor.y;
6016 cr.width = cursor_glyph->pixel_width;
6017 cr.height = w->phys_cursor_height;
6018 return x_intersect_rectangles (&cr, r, &result);
6020 else
6021 return 0;
6025 /* Redraw the part of window W intersection rectangle FR. Pixel
6026 coordinates in FR are frame-relative. Call this function with
6027 input blocked. Value is non-zero if the exposure overwrites
6028 mouse-face. */
6030 static int
6031 expose_window (w, fr)
6032 struct window *w;
6033 XRectangle *fr;
6035 struct frame *f = XFRAME (w->frame);
6036 XRectangle wr, r;
6037 int mouse_face_overwritten_p = 0;
6039 /* If window is not yet fully initialized, do nothing. This can
6040 happen when toolkit scroll bars are used and a window is split.
6041 Reconfiguring the scroll bar will generate an expose for a newly
6042 created window. */
6043 if (w->current_matrix == NULL)
6044 return 0;
6046 /* When we're currently updating the window, display and current
6047 matrix usually don't agree. Arrange for a thorough display
6048 later. */
6049 if (w == updated_window)
6051 SET_FRAME_GARBAGED (f);
6052 return 0;
6055 /* Frame-relative pixel rectangle of W. */
6056 wr.x = XFASTINT (w->left) * CANON_X_UNIT (f);
6057 wr.y = XFASTINT (w->top) * CANON_Y_UNIT (f);
6058 wr.width = XFASTINT (w->width) * CANON_X_UNIT (f);
6059 wr.height = XFASTINT (w->height) * CANON_Y_UNIT (f);
6061 if (x_intersect_rectangles (fr, &wr, &r))
6063 int yb = window_text_bottom_y (w);
6064 struct glyph_row *row;
6065 int cursor_cleared_p;
6067 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
6068 r.x, r.y, r.width, r.height));
6070 /* Convert to window coordinates. */
6071 r.x = FRAME_TO_WINDOW_PIXEL_X (w, r.x);
6072 r.y = FRAME_TO_WINDOW_PIXEL_Y (w, r.y);
6074 /* Turn off the cursor. */
6075 if (!w->pseudo_window_p
6076 && x_phys_cursor_in_rect_p (w, &r))
6078 x_clear_cursor (w);
6079 cursor_cleared_p = 1;
6081 else
6082 cursor_cleared_p = 0;
6084 /* Find the first row intersecting the rectangle R. */
6085 for (row = w->current_matrix->rows;
6086 row->enabled_p;
6087 ++row)
6089 int y0 = row->y;
6090 int y1 = MATRIX_ROW_BOTTOM_Y (row);
6092 if ((y0 >= r.y && y0 < r.y + r.height)
6093 || (y1 > r.y && y1 < r.y + r.height)
6094 || (r.y >= y0 && r.y < y1)
6095 || (r.y + r.height > y0 && r.y + r.height < y1))
6097 if (expose_line (w, row, &r))
6098 mouse_face_overwritten_p = 1;
6101 if (y1 >= yb)
6102 break;
6105 /* Display the mode line if there is one. */
6106 if (WINDOW_WANTS_MODELINE_P (w)
6107 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
6108 row->enabled_p)
6109 && row->y < r.y + r.height)
6111 if (expose_line (w, row, &r))
6112 mouse_face_overwritten_p = 1;
6115 if (!w->pseudo_window_p)
6117 /* Draw border between windows. */
6118 x_draw_vertical_border (w);
6120 /* Turn the cursor on again. */
6121 if (cursor_cleared_p)
6122 x_update_window_cursor (w, 1);
6126 return mouse_face_overwritten_p;
6130 /* Determine the intersection of two rectangles R1 and R2. Return
6131 the intersection in *RESULT. Value is non-zero if RESULT is not
6132 empty. */
6134 static int
6135 x_intersect_rectangles (r1, r2, result)
6136 XRectangle *r1, *r2, *result;
6138 XRectangle *left, *right;
6139 XRectangle *upper, *lower;
6140 int intersection_p = 0;
6142 /* Rearrange so that R1 is the left-most rectangle. */
6143 if (r1->x < r2->x)
6144 left = r1, right = r2;
6145 else
6146 left = r2, right = r1;
6148 /* X0 of the intersection is right.x0, if this is inside R1,
6149 otherwise there is no intersection. */
6150 if (right->x <= left->x + left->width)
6152 result->x = right->x;
6154 /* The right end of the intersection is the minimum of the
6155 the right ends of left and right. */
6156 result->width = (min (left->x + left->width, right->x + right->width)
6157 - result->x);
6159 /* Same game for Y. */
6160 if (r1->y < r2->y)
6161 upper = r1, lower = r2;
6162 else
6163 upper = r2, lower = r1;
6165 /* The upper end of the intersection is lower.y0, if this is inside
6166 of upper. Otherwise, there is no intersection. */
6167 if (lower->y <= upper->y + upper->height)
6169 result->y = lower->y;
6171 /* The lower end of the intersection is the minimum of the lower
6172 ends of upper and lower. */
6173 result->height = (min (lower->y + lower->height,
6174 upper->y + upper->height)
6175 - result->y);
6176 intersection_p = 1;
6180 return intersection_p;
6187 static void
6188 frame_highlight (f)
6189 struct frame *f;
6191 /* We used to only do this if Vx_no_window_manager was non-nil, but
6192 the ICCCM (section 4.1.6) says that the window's border pixmap
6193 and border pixel are window attributes which are "private to the
6194 client", so we can always change it to whatever we want. */
6195 BLOCK_INPUT;
6196 XSetWindowBorder (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6197 f->output_data.x->border_pixel);
6198 UNBLOCK_INPUT;
6199 x_update_cursor (f, 1);
6202 static void
6203 frame_unhighlight (f)
6204 struct frame *f;
6206 /* We used to only do this if Vx_no_window_manager was non-nil, but
6207 the ICCCM (section 4.1.6) says that the window's border pixmap
6208 and border pixel are window attributes which are "private to the
6209 client", so we can always change it to whatever we want. */
6210 BLOCK_INPUT;
6211 XSetWindowBorderPixmap (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
6212 f->output_data.x->border_tile);
6213 UNBLOCK_INPUT;
6214 x_update_cursor (f, 1);
6217 /* The focus has changed. Update the frames as necessary to reflect
6218 the new situation. Note that we can't change the selected frame
6219 here, because the Lisp code we are interrupting might become confused.
6220 Each event gets marked with the frame in which it occurred, so the
6221 Lisp code can tell when the switch took place by examining the events. */
6223 static void
6224 x_new_focus_frame (dpyinfo, frame)
6225 struct x_display_info *dpyinfo;
6226 struct frame *frame;
6228 struct frame *old_focus = dpyinfo->x_focus_frame;
6230 if (frame != dpyinfo->x_focus_frame)
6232 /* Set this before calling other routines, so that they see
6233 the correct value of x_focus_frame. */
6234 dpyinfo->x_focus_frame = frame;
6236 if (old_focus && old_focus->auto_lower)
6237 x_lower_frame (old_focus);
6239 #if 0
6240 selected_frame = frame;
6241 XSETFRAME (XWINDOW (selected_frame->selected_window)->frame,
6242 selected_frame);
6243 Fselect_window (selected_frame->selected_window);
6244 choose_minibuf_frame ();
6245 #endif /* ! 0 */
6247 if (dpyinfo->x_focus_frame && dpyinfo->x_focus_frame->auto_raise)
6248 pending_autoraise_frame = dpyinfo->x_focus_frame;
6249 else
6250 pending_autoraise_frame = 0;
6253 x_frame_rehighlight (dpyinfo);
6256 /* Handle an event saying the mouse has moved out of an Emacs frame. */
6258 void
6259 x_mouse_leave (dpyinfo)
6260 struct x_display_info *dpyinfo;
6262 x_new_focus_frame (dpyinfo, dpyinfo->x_focus_event_frame);
6265 /* The focus has changed, or we have redirected a frame's focus to
6266 another frame (this happens when a frame uses a surrogate
6267 mini-buffer frame). Shift the highlight as appropriate.
6269 The FRAME argument doesn't necessarily have anything to do with which
6270 frame is being highlighted or un-highlighted; we only use it to find
6271 the appropriate X display info. */
6273 static void
6274 XTframe_rehighlight (frame)
6275 struct frame *frame;
6277 x_frame_rehighlight (FRAME_X_DISPLAY_INFO (frame));
6280 static void
6281 x_frame_rehighlight (dpyinfo)
6282 struct x_display_info *dpyinfo;
6284 struct frame *old_highlight = dpyinfo->x_highlight_frame;
6286 if (dpyinfo->x_focus_frame)
6288 dpyinfo->x_highlight_frame
6289 = ((GC_FRAMEP (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame)))
6290 ? XFRAME (FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame))
6291 : dpyinfo->x_focus_frame);
6292 if (! FRAME_LIVE_P (dpyinfo->x_highlight_frame))
6294 FRAME_FOCUS_FRAME (dpyinfo->x_focus_frame) = Qnil;
6295 dpyinfo->x_highlight_frame = dpyinfo->x_focus_frame;
6298 else
6299 dpyinfo->x_highlight_frame = 0;
6301 if (dpyinfo->x_highlight_frame != old_highlight)
6303 if (old_highlight)
6304 frame_unhighlight (old_highlight);
6305 if (dpyinfo->x_highlight_frame)
6306 frame_highlight (dpyinfo->x_highlight_frame);
6312 /* Keyboard processing - modifier keys, vendor-specific keysyms, etc. */
6314 /* Initialize mode_switch_bit and modifier_meaning. */
6315 static void
6316 x_find_modifier_meanings (dpyinfo)
6317 struct x_display_info *dpyinfo;
6319 int min_code, max_code;
6320 KeySym *syms;
6321 int syms_per_code;
6322 XModifierKeymap *mods;
6324 dpyinfo->meta_mod_mask = 0;
6325 dpyinfo->shift_lock_mask = 0;
6326 dpyinfo->alt_mod_mask = 0;
6327 dpyinfo->super_mod_mask = 0;
6328 dpyinfo->hyper_mod_mask = 0;
6330 #ifdef HAVE_X11R4
6331 XDisplayKeycodes (dpyinfo->display, &min_code, &max_code);
6332 #else
6333 min_code = dpyinfo->display->min_keycode;
6334 max_code = dpyinfo->display->max_keycode;
6335 #endif
6337 syms = XGetKeyboardMapping (dpyinfo->display,
6338 min_code, max_code - min_code + 1,
6339 &syms_per_code);
6340 mods = XGetModifierMapping (dpyinfo->display);
6342 /* Scan the modifier table to see which modifier bits the Meta and
6343 Alt keysyms are on. */
6345 int row, col; /* The row and column in the modifier table. */
6347 for (row = 3; row < 8; row++)
6348 for (col = 0; col < mods->max_keypermod; col++)
6350 KeyCode code
6351 = mods->modifiermap[(row * mods->max_keypermod) + col];
6353 /* Zeroes are used for filler. Skip them. */
6354 if (code == 0)
6355 continue;
6357 /* Are any of this keycode's keysyms a meta key? */
6359 int code_col;
6361 for (code_col = 0; code_col < syms_per_code; code_col++)
6363 int sym = syms[((code - min_code) * syms_per_code) + code_col];
6365 switch (sym)
6367 case XK_Meta_L:
6368 case XK_Meta_R:
6369 dpyinfo->meta_mod_mask |= (1 << row);
6370 break;
6372 case XK_Alt_L:
6373 case XK_Alt_R:
6374 dpyinfo->alt_mod_mask |= (1 << row);
6375 break;
6377 case XK_Hyper_L:
6378 case XK_Hyper_R:
6379 dpyinfo->hyper_mod_mask |= (1 << row);
6380 break;
6382 case XK_Super_L:
6383 case XK_Super_R:
6384 dpyinfo->super_mod_mask |= (1 << row);
6385 break;
6387 case XK_Shift_Lock:
6388 /* Ignore this if it's not on the lock modifier. */
6389 if ((1 << row) == LockMask)
6390 dpyinfo->shift_lock_mask = LockMask;
6391 break;
6398 /* If we couldn't find any meta keys, accept any alt keys as meta keys. */
6399 if (! dpyinfo->meta_mod_mask)
6401 dpyinfo->meta_mod_mask = dpyinfo->alt_mod_mask;
6402 dpyinfo->alt_mod_mask = 0;
6405 /* If some keys are both alt and meta,
6406 make them just meta, not alt. */
6407 if (dpyinfo->alt_mod_mask & dpyinfo->meta_mod_mask)
6409 dpyinfo->alt_mod_mask &= ~dpyinfo->meta_mod_mask;
6412 XFree ((char *) syms);
6413 XFreeModifiermap (mods);
6416 /* Convert between the modifier bits X uses and the modifier bits
6417 Emacs uses. */
6419 static unsigned int
6420 x_x_to_emacs_modifiers (dpyinfo, state)
6421 struct x_display_info *dpyinfo;
6422 unsigned int state;
6424 EMACS_UINT mod_meta = meta_modifier;
6425 EMACS_UINT mod_alt = alt_modifier;
6426 EMACS_UINT mod_hyper = hyper_modifier;
6427 EMACS_UINT mod_super = super_modifier;
6428 Lisp_Object tem;
6430 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6431 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6432 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6433 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6434 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6435 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6436 tem = Fget (Vx_super_keysym, Qmodifier_value);
6437 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6440 return ( ((state & (ShiftMask | dpyinfo->shift_lock_mask)) ? shift_modifier : 0)
6441 | ((state & ControlMask) ? ctrl_modifier : 0)
6442 | ((state & dpyinfo->meta_mod_mask) ? mod_meta : 0)
6443 | ((state & dpyinfo->alt_mod_mask) ? mod_alt : 0)
6444 | ((state & dpyinfo->super_mod_mask) ? mod_super : 0)
6445 | ((state & dpyinfo->hyper_mod_mask) ? mod_hyper : 0));
6448 static unsigned int
6449 x_emacs_to_x_modifiers (dpyinfo, state)
6450 struct x_display_info *dpyinfo;
6451 unsigned int state;
6453 EMACS_UINT mod_meta = meta_modifier;
6454 EMACS_UINT mod_alt = alt_modifier;
6455 EMACS_UINT mod_hyper = hyper_modifier;
6456 EMACS_UINT mod_super = super_modifier;
6458 Lisp_Object tem;
6460 tem = Fget (Vx_alt_keysym, Qmodifier_value);
6461 if (! EQ (tem, Qnil)) mod_alt = XUINT (tem);
6462 tem = Fget (Vx_meta_keysym, Qmodifier_value);
6463 if (! EQ (tem, Qnil)) mod_meta = XUINT (tem);
6464 tem = Fget (Vx_hyper_keysym, Qmodifier_value);
6465 if (! EQ (tem, Qnil)) mod_hyper = XUINT (tem);
6466 tem = Fget (Vx_super_keysym, Qmodifier_value);
6467 if (! EQ (tem, Qnil)) mod_super = XUINT (tem);
6470 return ( ((state & mod_alt) ? dpyinfo->alt_mod_mask : 0)
6471 | ((state & mod_super) ? dpyinfo->super_mod_mask : 0)
6472 | ((state & mod_hyper) ? dpyinfo->hyper_mod_mask : 0)
6473 | ((state & shift_modifier) ? ShiftMask : 0)
6474 | ((state & ctrl_modifier) ? ControlMask : 0)
6475 | ((state & mod_meta) ? dpyinfo->meta_mod_mask : 0));
6478 /* Convert a keysym to its name. */
6480 char *
6481 x_get_keysym_name (keysym)
6482 KeySym keysym;
6484 char *value;
6486 BLOCK_INPUT;
6487 value = XKeysymToString (keysym);
6488 UNBLOCK_INPUT;
6490 return value;
6495 /* Mouse clicks and mouse movement. Rah. */
6497 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
6498 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
6499 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
6500 not force the value into range. */
6502 void
6503 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
6504 FRAME_PTR f;
6505 register int pix_x, pix_y;
6506 register int *x, *y;
6507 XRectangle *bounds;
6508 int noclip;
6510 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to round down
6511 even for negative values. */
6512 if (pix_x < 0)
6513 pix_x -= FONT_WIDTH ((f)->output_data.x->font) - 1;
6514 if (pix_y < 0)
6515 pix_y -= (f)->output_data.x->line_height - 1;
6517 pix_x = PIXEL_TO_CHAR_COL (f, pix_x);
6518 pix_y = PIXEL_TO_CHAR_ROW (f, pix_y);
6520 if (bounds)
6522 bounds->width = FONT_WIDTH (f->output_data.x->font);
6523 bounds->height = f->output_data.x->line_height;
6524 bounds->x = CHAR_TO_PIXEL_COL (f, pix_x);
6525 bounds->y = CHAR_TO_PIXEL_ROW (f, pix_y);
6528 if (!noclip)
6530 if (pix_x < 0)
6531 pix_x = 0;
6532 else if (pix_x > FRAME_WINDOW_WIDTH (f))
6533 pix_x = FRAME_WINDOW_WIDTH (f);
6535 if (pix_y < 0)
6536 pix_y = 0;
6537 else if (pix_y > f->height)
6538 pix_y = f->height;
6541 *x = pix_x;
6542 *y = pix_y;
6546 /* Given HPOS/VPOS in the current matrix of W, return corresponding
6547 frame-relative pixel positions in *FRAME_X and *FRAME_Y. If we
6548 can't tell the positions because W's display is not up to date,
6549 return 0. */
6552 glyph_to_pixel_coords (w, hpos, vpos, frame_x, frame_y)
6553 struct window *w;
6554 int hpos, vpos;
6555 int *frame_x, *frame_y;
6557 int success_p;
6559 xassert (hpos >= 0 && hpos < w->current_matrix->matrix_w);
6560 xassert (vpos >= 0 && vpos < w->current_matrix->matrix_h);
6562 if (display_completed)
6564 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
6565 struct glyph *glyph = row->glyphs[TEXT_AREA];
6566 struct glyph *end = glyph + min (hpos, row->used[TEXT_AREA]);
6568 *frame_y = row->y;
6569 *frame_x = row->x;
6570 while (glyph < end)
6572 *frame_x += glyph->pixel_width;
6573 ++glyph;
6576 success_p = 1;
6578 else
6580 *frame_y = *frame_x = 0;
6581 success_p = 0;
6584 *frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, *frame_y);
6585 *frame_x = WINDOW_TO_FRAME_PIXEL_X (w, *frame_x);
6586 return success_p;
6590 /* Prepare a mouse-event in *RESULT for placement in the input queue.
6592 If the event is a button press, then note that we have grabbed
6593 the mouse. */
6595 static Lisp_Object
6596 construct_mouse_click (result, event, f)
6597 struct input_event *result;
6598 XButtonEvent *event;
6599 struct frame *f;
6601 /* Make the event type no_event; we'll change that when we decide
6602 otherwise. */
6603 result->kind = mouse_click;
6604 result->code = event->button - Button1;
6605 result->timestamp = event->time;
6606 result->modifiers = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
6607 event->state)
6608 | (event->type == ButtonRelease
6609 ? up_modifier
6610 : down_modifier));
6612 XSETINT (result->x, event->x);
6613 XSETINT (result->y, event->y);
6614 XSETFRAME (result->frame_or_window, f);
6615 result->arg = Qnil;
6616 return Qnil;
6620 /* Function to report a mouse movement to the mainstream Emacs code.
6621 The input handler calls this.
6623 We have received a mouse movement event, which is given in *event.
6624 If the mouse is over a different glyph than it was last time, tell
6625 the mainstream emacs code by setting mouse_moved. If not, ask for
6626 another motion event, so we can check again the next time it moves. */
6628 static XMotionEvent last_mouse_motion_event;
6629 static Lisp_Object last_mouse_motion_frame;
6631 static void
6632 note_mouse_movement (frame, event)
6633 FRAME_PTR frame;
6634 XMotionEvent *event;
6636 last_mouse_movement_time = event->time;
6637 last_mouse_motion_event = *event;
6638 XSETFRAME (last_mouse_motion_frame, frame);
6640 if (x_autoselect_window_p)
6642 int area;
6643 Lisp_Object window;
6644 static Lisp_Object last_window;
6646 window = window_from_coordinates (frame, event->x, event->y, &area, 0);
6648 /* Window will be selected only when it is not selected now and
6649 last mouse movement event was not in it. Minubuffer window
6650 will be selected iff it is active. */
6651 if (!EQ (window, last_window)
6652 && !EQ (window, selected_window)
6653 && (!MINI_WINDOW_P (XWINDOW (window))
6654 || (EQ (window, minibuf_window) && minibuf_level > 0)))
6655 Fselect_window (window);
6657 last_window=window;
6660 if (event->window != FRAME_X_WINDOW (frame))
6662 frame->mouse_moved = 1;
6663 last_mouse_scroll_bar = Qnil;
6664 note_mouse_highlight (frame, -1, -1);
6667 /* Has the mouse moved off the glyph it was on at the last sighting? */
6668 else if (event->x < last_mouse_glyph.x
6669 || event->x >= last_mouse_glyph.x + last_mouse_glyph.width
6670 || event->y < last_mouse_glyph.y
6671 || event->y >= last_mouse_glyph.y + last_mouse_glyph.height)
6673 frame->mouse_moved = 1;
6674 last_mouse_scroll_bar = Qnil;
6675 note_mouse_highlight (frame, event->x, event->y);
6680 /************************************************************************
6681 Mouse Face
6682 ************************************************************************/
6684 /* Find the glyph under window-relative coordinates X/Y in window W.
6685 Consider only glyphs from buffer text, i.e. no glyphs from overlay
6686 strings. Return in *HPOS and *VPOS the row and column number of
6687 the glyph found. Return in *AREA the glyph area containing X.
6688 Value is a pointer to the glyph found or null if X/Y is not on
6689 text, or we can't tell because W's current matrix is not up to
6690 date. */
6692 static struct glyph *
6693 x_y_to_hpos_vpos (w, x, y, hpos, vpos, area, buffer_only_p)
6694 struct window *w;
6695 int x, y;
6696 int *hpos, *vpos, *area;
6697 int buffer_only_p;
6699 struct glyph *glyph, *end;
6700 struct glyph_row *row = NULL;
6701 int x0, i, left_area_width;
6703 /* Find row containing Y. Give up if some row is not enabled. */
6704 for (i = 0; i < w->current_matrix->nrows; ++i)
6706 row = MATRIX_ROW (w->current_matrix, i);
6707 if (!row->enabled_p)
6708 return NULL;
6709 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
6710 break;
6713 *vpos = i;
6714 *hpos = 0;
6716 /* Give up if Y is not in the window. */
6717 if (i == w->current_matrix->nrows)
6718 return NULL;
6720 /* Get the glyph area containing X. */
6721 if (w->pseudo_window_p)
6723 *area = TEXT_AREA;
6724 x0 = 0;
6726 else
6728 left_area_width = window_box_width (w, LEFT_MARGIN_AREA);
6729 if (x < left_area_width)
6731 *area = LEFT_MARGIN_AREA;
6732 x0 = 0;
6734 else if (x < left_area_width + window_box_width (w, TEXT_AREA))
6736 *area = TEXT_AREA;
6737 x0 = row->x + left_area_width;
6739 else
6741 *area = RIGHT_MARGIN_AREA;
6742 x0 = left_area_width + window_box_width (w, TEXT_AREA);
6746 /* Find glyph containing X. */
6747 glyph = row->glyphs[*area];
6748 end = glyph + row->used[*area];
6749 while (glyph < end)
6751 if (x < x0 + glyph->pixel_width)
6753 if (w->pseudo_window_p)
6754 break;
6755 else if (!buffer_only_p || BUFFERP (glyph->object))
6756 break;
6759 x0 += glyph->pixel_width;
6760 ++glyph;
6763 if (glyph == end)
6764 return NULL;
6766 *hpos = glyph - row->glyphs[*area];
6767 return glyph;
6771 /* Convert frame-relative x/y to coordinates relative to window W.
6772 Takes pseudo-windows into account. */
6774 static void
6775 frame_to_window_pixel_xy (w, x, y)
6776 struct window *w;
6777 int *x, *y;
6779 if (w->pseudo_window_p)
6781 /* A pseudo-window is always full-width, and starts at the
6782 left edge of the frame, plus a frame border. */
6783 struct frame *f = XFRAME (w->frame);
6784 *x -= FRAME_INTERNAL_BORDER_WIDTH_SAFE (f);
6785 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6787 else
6789 *x = FRAME_TO_WINDOW_PIXEL_X (w, *x);
6790 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
6795 /* Take proper action when mouse has moved to the mode or header line of
6796 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
6797 mode line. X is relative to the start of the text display area of
6798 W, so the width of fringes and scroll bars must be subtracted
6799 to get a position relative to the start of the mode line. */
6801 static void
6802 note_mode_line_highlight (w, x, mode_line_p)
6803 struct window *w;
6804 int x, mode_line_p;
6806 struct frame *f = XFRAME (w->frame);
6807 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6808 Cursor cursor = dpyinfo->vertical_scroll_bar_cursor;
6809 struct glyph_row *row;
6811 if (mode_line_p)
6812 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
6813 else
6814 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
6816 if (row->enabled_p)
6818 struct glyph *glyph, *end;
6819 Lisp_Object help, map;
6820 int x0;
6822 /* Find the glyph under X. */
6823 glyph = row->glyphs[TEXT_AREA];
6824 end = glyph + row->used[TEXT_AREA];
6825 x0 = - (FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f)
6826 + FRAME_X_LEFT_FRINGE_WIDTH (f));
6828 while (glyph < end
6829 && x >= x0 + glyph->pixel_width)
6831 x0 += glyph->pixel_width;
6832 ++glyph;
6835 if (glyph < end
6836 && STRINGP (glyph->object)
6837 && XSTRING (glyph->object)->intervals
6838 && glyph->charpos >= 0
6839 && glyph->charpos < XSTRING (glyph->object)->size)
6841 /* If we're on a string with `help-echo' text property,
6842 arrange for the help to be displayed. This is done by
6843 setting the global variable help_echo to the help string. */
6844 help = Fget_text_property (make_number (glyph->charpos),
6845 Qhelp_echo, glyph->object);
6846 if (!NILP (help))
6848 help_echo = help;
6849 XSETWINDOW (help_echo_window, w);
6850 help_echo_object = glyph->object;
6851 help_echo_pos = glyph->charpos;
6854 /* Change the mouse pointer according to what is under X/Y. */
6855 map = Fget_text_property (make_number (glyph->charpos),
6856 Qlocal_map, glyph->object);
6857 if (KEYMAPP (map))
6858 cursor = f->output_data.x->nontext_cursor;
6859 else
6861 map = Fget_text_property (make_number (glyph->charpos),
6862 Qkeymap, glyph->object);
6863 if (KEYMAPP (map))
6864 cursor = f->output_data.x->nontext_cursor;
6869 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
6873 /* Take proper action when the mouse has moved to position X, Y on
6874 frame F as regards highlighting characters that have mouse-face
6875 properties. Also de-highlighting chars where the mouse was before.
6876 X and Y can be negative or out of range. */
6878 static void
6879 note_mouse_highlight (f, x, y)
6880 struct frame *f;
6881 int x, y;
6883 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
6884 int portion;
6885 Lisp_Object window;
6886 struct window *w;
6887 Cursor cursor = None;
6888 struct buffer *b;
6890 /* When a menu is active, don't highlight because this looks odd. */
6891 #ifdef USE_X_TOOLKIT
6892 if (popup_activated ())
6893 return;
6894 #endif
6896 if (NILP (Vmouse_highlight)
6897 || !f->glyphs_initialized_p)
6898 return;
6900 dpyinfo->mouse_face_mouse_x = x;
6901 dpyinfo->mouse_face_mouse_y = y;
6902 dpyinfo->mouse_face_mouse_frame = f;
6904 if (dpyinfo->mouse_face_defer)
6905 return;
6907 if (gc_in_progress)
6909 dpyinfo->mouse_face_deferred_gc = 1;
6910 return;
6913 /* Which window is that in? */
6914 window = window_from_coordinates (f, x, y, &portion, 1);
6916 /* If we were displaying active text in another window, clear that. */
6917 if (! EQ (window, dpyinfo->mouse_face_window))
6918 clear_mouse_face (dpyinfo);
6920 /* Not on a window -> return. */
6921 if (!WINDOWP (window))
6922 return;
6924 /* Convert to window-relative pixel coordinates. */
6925 w = XWINDOW (window);
6926 frame_to_window_pixel_xy (w, &x, &y);
6928 /* Handle tool-bar window differently since it doesn't display a
6929 buffer. */
6930 if (EQ (window, f->tool_bar_window))
6932 note_tool_bar_highlight (f, x, y);
6933 return;
6936 /* Mouse is on the mode or header line? */
6937 if (portion == 1 || portion == 3)
6939 note_mode_line_highlight (w, x, portion == 1);
6940 return;
6943 if (portion == 2)
6944 cursor = f->output_data.x->horizontal_drag_cursor;
6945 else
6946 cursor = f->output_data.x->text_cursor;
6948 /* Are we in a window whose display is up to date?
6949 And verify the buffer's text has not changed. */
6950 b = XBUFFER (w->buffer);
6951 if (/* Within text portion of the window. */
6952 portion == 0
6953 && EQ (w->window_end_valid, w->buffer)
6954 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
6955 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
6957 int hpos, vpos, pos, i, area;
6958 struct glyph *glyph;
6959 Lisp_Object object;
6960 Lisp_Object mouse_face = Qnil, overlay = Qnil, position;
6961 Lisp_Object *overlay_vec = NULL;
6962 int len, noverlays;
6963 struct buffer *obuf;
6964 int obegv, ozv, same_region;
6966 /* Find the glyph under X/Y. */
6967 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &area, 0);
6969 /* Clear mouse face if X/Y not over text. */
6970 if (glyph == NULL
6971 || area != TEXT_AREA
6972 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p)
6974 if (clear_mouse_face (dpyinfo))
6975 cursor = None;
6976 goto set_cursor;
6979 pos = glyph->charpos;
6980 object = glyph->object;
6981 if (!STRINGP (object) && !BUFFERP (object))
6982 goto set_cursor;
6984 /* If we get an out-of-range value, return now; avoid an error. */
6985 if (BUFFERP (object) && pos > BUF_Z (b))
6986 goto set_cursor;
6988 /* Make the window's buffer temporarily current for
6989 overlays_at and compute_char_face. */
6990 obuf = current_buffer;
6991 current_buffer = b;
6992 obegv = BEGV;
6993 ozv = ZV;
6994 BEGV = BEG;
6995 ZV = Z;
6997 /* Is this char mouse-active or does it have help-echo? */
6998 position = make_number (pos);
7000 if (BUFFERP (object))
7002 /* Put all the overlays we want in a vector in overlay_vec.
7003 Store the length in len. If there are more than 10, make
7004 enough space for all, and try again. */
7005 len = 10;
7006 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7007 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
7008 if (noverlays > len)
7010 len = noverlays;
7011 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
7012 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL,0);
7015 /* Sort overlays into increasing priority order. */
7016 noverlays = sort_overlays (overlay_vec, noverlays, w);
7018 else
7019 noverlays = 0;
7021 same_region = (EQ (window, dpyinfo->mouse_face_window)
7022 && vpos >= dpyinfo->mouse_face_beg_row
7023 && vpos <= dpyinfo->mouse_face_end_row
7024 && (vpos > dpyinfo->mouse_face_beg_row
7025 || hpos >= dpyinfo->mouse_face_beg_col)
7026 && (vpos < dpyinfo->mouse_face_end_row
7027 || hpos < dpyinfo->mouse_face_end_col
7028 || dpyinfo->mouse_face_past_end));
7030 if (same_region)
7031 cursor = None;
7033 /* Check mouse-face highlighting. */
7034 if (! same_region
7035 /* If there exists an overlay with mouse-face overlapping
7036 the one we are currently highlighting, we have to
7037 check if we enter the overlapping overlay, and then
7038 highlight only that. */
7039 || (OVERLAYP (dpyinfo->mouse_face_overlay)
7040 && mouse_face_overlay_overlaps (dpyinfo->mouse_face_overlay)))
7042 /* Find the highest priority overlay that has a mouse-face
7043 property. */
7044 overlay = Qnil;
7045 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
7047 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
7048 if (!NILP (mouse_face))
7049 overlay = overlay_vec[i];
7052 /* If we're actually highlighting the same overlay as
7053 before, there's no need to do that again. */
7054 if (!NILP (overlay)
7055 && EQ (overlay, dpyinfo->mouse_face_overlay))
7056 goto check_help_echo;
7058 dpyinfo->mouse_face_overlay = overlay;
7060 /* Clear the display of the old active region, if any. */
7061 if (clear_mouse_face (dpyinfo))
7062 cursor = None;
7064 /* If no overlay applies, get a text property. */
7065 if (NILP (overlay))
7066 mouse_face = Fget_text_property (position, Qmouse_face, object);
7068 /* Handle the overlay case. */
7069 if (!NILP (overlay))
7071 /* Find the range of text around this char that
7072 should be active. */
7073 Lisp_Object before, after;
7074 int ignore;
7076 before = Foverlay_start (overlay);
7077 after = Foverlay_end (overlay);
7078 /* Record this as the current active region. */
7079 fast_find_position (w, XFASTINT (before),
7080 &dpyinfo->mouse_face_beg_col,
7081 &dpyinfo->mouse_face_beg_row,
7082 &dpyinfo->mouse_face_beg_x,
7083 &dpyinfo->mouse_face_beg_y, Qnil);
7085 dpyinfo->mouse_face_past_end
7086 = !fast_find_position (w, XFASTINT (after),
7087 &dpyinfo->mouse_face_end_col,
7088 &dpyinfo->mouse_face_end_row,
7089 &dpyinfo->mouse_face_end_x,
7090 &dpyinfo->mouse_face_end_y, Qnil);
7091 dpyinfo->mouse_face_window = window;
7092 dpyinfo->mouse_face_face_id
7093 = face_at_buffer_position (w, pos, 0, 0,
7094 &ignore, pos + 1, 1);
7096 /* Display it as active. */
7097 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7098 cursor = None;
7100 /* Handle the text property case. */
7101 else if (!NILP (mouse_face) && BUFFERP (object))
7103 /* Find the range of text around this char that
7104 should be active. */
7105 Lisp_Object before, after, beginning, end;
7106 int ignore;
7108 beginning = Fmarker_position (w->start);
7109 end = make_number (BUF_Z (XBUFFER (object))
7110 - XFASTINT (w->window_end_pos));
7111 before
7112 = Fprevious_single_property_change (make_number (pos + 1),
7113 Qmouse_face,
7114 object, beginning);
7115 after
7116 = Fnext_single_property_change (position, Qmouse_face,
7117 object, end);
7119 /* Record this as the current active region. */
7120 fast_find_position (w, XFASTINT (before),
7121 &dpyinfo->mouse_face_beg_col,
7122 &dpyinfo->mouse_face_beg_row,
7123 &dpyinfo->mouse_face_beg_x,
7124 &dpyinfo->mouse_face_beg_y, Qnil);
7125 dpyinfo->mouse_face_past_end
7126 = !fast_find_position (w, XFASTINT (after),
7127 &dpyinfo->mouse_face_end_col,
7128 &dpyinfo->mouse_face_end_row,
7129 &dpyinfo->mouse_face_end_x,
7130 &dpyinfo->mouse_face_end_y, Qnil);
7131 dpyinfo->mouse_face_window = window;
7133 if (BUFFERP (object))
7134 dpyinfo->mouse_face_face_id
7135 = face_at_buffer_position (w, pos, 0, 0,
7136 &ignore, pos + 1, 1);
7138 /* Display it as active. */
7139 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7140 cursor = None;
7142 else if (!NILP (mouse_face) && STRINGP (object))
7144 Lisp_Object b, e;
7145 int ignore;
7147 b = Fprevious_single_property_change (make_number (pos + 1),
7148 Qmouse_face,
7149 object, Qnil);
7150 e = Fnext_single_property_change (position, Qmouse_face,
7151 object, Qnil);
7152 if (NILP (b))
7153 b = make_number (0);
7154 if (NILP (e))
7155 e = make_number (XSTRING (object)->size - 1);
7156 fast_find_string_pos (w, XINT (b), object,
7157 &dpyinfo->mouse_face_beg_col,
7158 &dpyinfo->mouse_face_beg_row,
7159 &dpyinfo->mouse_face_beg_x,
7160 &dpyinfo->mouse_face_beg_y, 0);
7161 fast_find_string_pos (w, XINT (e), object,
7162 &dpyinfo->mouse_face_end_col,
7163 &dpyinfo->mouse_face_end_row,
7164 &dpyinfo->mouse_face_end_x,
7165 &dpyinfo->mouse_face_end_y, 1);
7166 dpyinfo->mouse_face_past_end = 0;
7167 dpyinfo->mouse_face_window = window;
7168 dpyinfo->mouse_face_face_id
7169 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
7170 glyph->face_id, 1);
7171 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7172 cursor = None;
7174 else if (STRINGP (object) && NILP (mouse_face))
7176 /* A string which doesn't have mouse-face, but
7177 the text ``under'' it might have. */
7178 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
7179 int start = MATRIX_ROW_START_CHARPOS (r);
7181 pos = string_buffer_position (w, object, start);
7182 if (pos > 0)
7183 mouse_face = get_char_property_and_overlay (make_number (pos),
7184 Qmouse_face,
7185 w->buffer,
7186 &overlay);
7187 if (!NILP (mouse_face) && !NILP (overlay))
7189 Lisp_Object before = Foverlay_start (overlay);
7190 Lisp_Object after = Foverlay_end (overlay);
7191 int ignore;
7193 /* Note that we might not be able to find position
7194 BEFORE in the glyph matrix if the overlay is
7195 entirely covered by a `display' property. In
7196 this case, we overshoot. So let's stop in
7197 the glyph matrix before glyphs for OBJECT. */
7198 fast_find_position (w, XFASTINT (before),
7199 &dpyinfo->mouse_face_beg_col,
7200 &dpyinfo->mouse_face_beg_row,
7201 &dpyinfo->mouse_face_beg_x,
7202 &dpyinfo->mouse_face_beg_y,
7203 object);
7205 dpyinfo->mouse_face_past_end
7206 = !fast_find_position (w, XFASTINT (after),
7207 &dpyinfo->mouse_face_end_col,
7208 &dpyinfo->mouse_face_end_row,
7209 &dpyinfo->mouse_face_end_x,
7210 &dpyinfo->mouse_face_end_y,
7211 Qnil);
7212 dpyinfo->mouse_face_window = window;
7213 dpyinfo->mouse_face_face_id
7214 = face_at_buffer_position (w, pos, 0, 0,
7215 &ignore, pos + 1, 1);
7217 /* Display it as active. */
7218 show_mouse_face (dpyinfo, DRAW_MOUSE_FACE);
7219 cursor = None;
7224 check_help_echo:
7226 /* Look for a `help-echo' property. */
7228 Lisp_Object help, overlay;
7230 /* Check overlays first. */
7231 help = overlay = Qnil;
7232 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
7234 overlay = overlay_vec[i];
7235 help = Foverlay_get (overlay, Qhelp_echo);
7238 if (!NILP (help))
7240 help_echo = help;
7241 help_echo_window = window;
7242 help_echo_object = overlay;
7243 help_echo_pos = pos;
7245 else
7247 Lisp_Object object = glyph->object;
7248 int charpos = glyph->charpos;
7250 /* Try text properties. */
7251 if (STRINGP (object)
7252 && charpos >= 0
7253 && charpos < XSTRING (object)->size)
7255 help = Fget_text_property (make_number (charpos),
7256 Qhelp_echo, object);
7257 if (NILP (help))
7259 /* If the string itself doesn't specify a help-echo,
7260 see if the buffer text ``under'' it does. */
7261 struct glyph_row *r
7262 = MATRIX_ROW (w->current_matrix, vpos);
7263 int start = MATRIX_ROW_START_CHARPOS (r);
7264 int pos = string_buffer_position (w, object, start);
7265 if (pos > 0)
7267 help = Fget_char_property (make_number (pos),
7268 Qhelp_echo, w->buffer);
7269 if (!NILP (help))
7271 charpos = pos;
7272 object = w->buffer;
7277 else if (BUFFERP (object)
7278 && charpos >= BEGV
7279 && charpos < ZV)
7280 help = Fget_text_property (make_number (charpos), Qhelp_echo,
7281 object);
7283 if (!NILP (help))
7285 help_echo = help;
7286 help_echo_window = window;
7287 help_echo_object = object;
7288 help_echo_pos = charpos;
7293 BEGV = obegv;
7294 ZV = ozv;
7295 current_buffer = obuf;
7298 set_cursor:
7300 if (cursor != None)
7301 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), cursor);
7304 static void
7305 redo_mouse_highlight ()
7307 if (!NILP (last_mouse_motion_frame)
7308 && FRAME_LIVE_P (XFRAME (last_mouse_motion_frame)))
7309 note_mouse_highlight (XFRAME (last_mouse_motion_frame),
7310 last_mouse_motion_event.x,
7311 last_mouse_motion_event.y);
7316 /***********************************************************************
7317 Tool-bars
7318 ***********************************************************************/
7320 static int x_tool_bar_item P_ ((struct frame *, int, int,
7321 struct glyph **, int *, int *, int *));
7323 /* Tool-bar item index of the item on which a mouse button was pressed
7324 or -1. */
7326 static int last_tool_bar_item;
7329 /* Get information about the tool-bar item at position X/Y on frame F.
7330 Return in *GLYPH a pointer to the glyph of the tool-bar item in
7331 the current matrix of the tool-bar window of F, or NULL if not
7332 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
7333 item in F->tool_bar_items. Value is
7335 -1 if X/Y is not on a tool-bar item
7336 0 if X/Y is on the same item that was highlighted before.
7337 1 otherwise. */
7339 static int
7340 x_tool_bar_item (f, x, y, glyph, hpos, vpos, prop_idx)
7341 struct frame *f;
7342 int x, y;
7343 struct glyph **glyph;
7344 int *hpos, *vpos, *prop_idx;
7346 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7347 struct window *w = XWINDOW (f->tool_bar_window);
7348 int area;
7350 /* Find the glyph under X/Y. */
7351 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, &area, 0);
7352 if (*glyph == NULL)
7353 return -1;
7355 /* Get the start of this tool-bar item's properties in
7356 f->tool_bar_items. */
7357 if (!tool_bar_item_info (f, *glyph, prop_idx))
7358 return -1;
7360 /* Is mouse on the highlighted item? */
7361 if (EQ (f->tool_bar_window, dpyinfo->mouse_face_window)
7362 && *vpos >= dpyinfo->mouse_face_beg_row
7363 && *vpos <= dpyinfo->mouse_face_end_row
7364 && (*vpos > dpyinfo->mouse_face_beg_row
7365 || *hpos >= dpyinfo->mouse_face_beg_col)
7366 && (*vpos < dpyinfo->mouse_face_end_row
7367 || *hpos < dpyinfo->mouse_face_end_col
7368 || dpyinfo->mouse_face_past_end))
7369 return 0;
7371 return 1;
7375 /* Handle mouse button event on the tool-bar of frame F, at
7376 frame-relative coordinates X/Y. EVENT_TYPE is either ButtonPress
7377 or ButtonRelase. */
7379 static void
7380 x_handle_tool_bar_click (f, button_event)
7381 struct frame *f;
7382 XButtonEvent *button_event;
7384 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7385 struct window *w = XWINDOW (f->tool_bar_window);
7386 int hpos, vpos, prop_idx;
7387 struct glyph *glyph;
7388 Lisp_Object enabled_p;
7389 int x = button_event->x;
7390 int y = button_event->y;
7392 /* If not on the highlighted tool-bar item, return. */
7393 frame_to_window_pixel_xy (w, &x, &y);
7394 if (x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
7395 return;
7397 /* If item is disabled, do nothing. */
7398 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7399 if (NILP (enabled_p))
7400 return;
7402 if (button_event->type == ButtonPress)
7404 /* Show item in pressed state. */
7405 show_mouse_face (dpyinfo, DRAW_IMAGE_SUNKEN);
7406 dpyinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
7407 last_tool_bar_item = prop_idx;
7409 else
7411 Lisp_Object key, frame;
7412 struct input_event event;
7414 /* Show item in released state. */
7415 show_mouse_face (dpyinfo, DRAW_IMAGE_RAISED);
7416 dpyinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
7418 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
7420 XSETFRAME (frame, f);
7421 event.kind = TOOL_BAR_EVENT;
7422 event.frame_or_window = frame;
7423 event.arg = frame;
7424 kbd_buffer_store_event (&event);
7426 event.kind = TOOL_BAR_EVENT;
7427 event.frame_or_window = frame;
7428 event.arg = key;
7429 event.modifiers = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
7430 button_event->state);
7431 kbd_buffer_store_event (&event);
7432 last_tool_bar_item = -1;
7437 /* Possibly highlight a tool-bar item on frame F when mouse moves to
7438 tool-bar window-relative coordinates X/Y. Called from
7439 note_mouse_highlight. */
7441 static void
7442 note_tool_bar_highlight (f, x, y)
7443 struct frame *f;
7444 int x, y;
7446 Lisp_Object window = f->tool_bar_window;
7447 struct window *w = XWINDOW (window);
7448 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7449 int hpos, vpos;
7450 struct glyph *glyph;
7451 struct glyph_row *row;
7452 int i;
7453 Lisp_Object enabled_p;
7454 int prop_idx;
7455 enum draw_glyphs_face draw;
7456 int mouse_down_p, rc;
7458 /* Function note_mouse_highlight is called with negative x(y
7459 values when mouse moves outside of the frame. */
7460 if (x <= 0 || y <= 0)
7462 clear_mouse_face (dpyinfo);
7463 return;
7466 rc = x_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
7467 if (rc < 0)
7469 /* Not on tool-bar item. */
7470 clear_mouse_face (dpyinfo);
7471 return;
7473 else if (rc == 0)
7474 goto set_help_echo;
7476 clear_mouse_face (dpyinfo);
7478 /* Mouse is down, but on different tool-bar item? */
7479 mouse_down_p = (dpyinfo->grabbed
7480 && f == last_mouse_frame
7481 && FRAME_LIVE_P (f));
7482 if (mouse_down_p
7483 && last_tool_bar_item != prop_idx)
7484 return;
7486 dpyinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
7487 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
7489 /* If tool-bar item is not enabled, don't highlight it. */
7490 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
7491 if (!NILP (enabled_p))
7493 /* Compute the x-position of the glyph. In front and past the
7494 image is a space. We include this is the highlighted area. */
7495 row = MATRIX_ROW (w->current_matrix, vpos);
7496 for (i = x = 0; i < hpos; ++i)
7497 x += row->glyphs[TEXT_AREA][i].pixel_width;
7499 /* Record this as the current active region. */
7500 dpyinfo->mouse_face_beg_col = hpos;
7501 dpyinfo->mouse_face_beg_row = vpos;
7502 dpyinfo->mouse_face_beg_x = x;
7503 dpyinfo->mouse_face_beg_y = row->y;
7504 dpyinfo->mouse_face_past_end = 0;
7506 dpyinfo->mouse_face_end_col = hpos + 1;
7507 dpyinfo->mouse_face_end_row = vpos;
7508 dpyinfo->mouse_face_end_x = x + glyph->pixel_width;
7509 dpyinfo->mouse_face_end_y = row->y;
7510 dpyinfo->mouse_face_window = window;
7511 dpyinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
7513 /* Display it as active. */
7514 show_mouse_face (dpyinfo, draw);
7515 dpyinfo->mouse_face_image_state = draw;
7518 set_help_echo:
7520 /* Set help_echo to a help string to display for this tool-bar item.
7521 XTread_socket does the rest. */
7522 help_echo_object = help_echo_window = Qnil;
7523 help_echo_pos = -1;
7524 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
7525 if (NILP (help_echo))
7526 help_echo = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
7531 /* Find the glyph matrix position of buffer position CHARPOS in window
7532 *W. HPOS, *VPOS, *X, and *Y are set to the positions found. W's
7533 current glyphs must be up to date. If CHARPOS is above window
7534 start return (0, 0, 0, 0). If CHARPOS is after end of W, return end
7535 of last line in W. In the row containing CHARPOS, stop before glyphs
7536 having STOP as object. */
7538 #if 0 /* This is a version of fast_find_position that's more correct
7539 in the presence of hscrolling, for example. I didn't install
7540 it right away because the problem fixed is minor, it failed
7541 in 20.x as well, and I think it's too risky to install
7542 so near the release of 21.1. 2001-09-25 gerd. */
7544 static int
7545 fast_find_position (w, charpos, hpos, vpos, x, y, stop)
7546 struct window *w;
7547 int charpos;
7548 int *hpos, *vpos, *x, *y;
7549 Lisp_Object stop;
7551 struct glyph_row *row, *first;
7552 struct glyph *glyph, *end;
7553 int i, past_end = 0;
7555 first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7556 row = row_containing_pos (w, charpos, first, NULL, 0);
7557 if (row == NULL)
7559 if (charpos < MATRIX_ROW_START_CHARPOS (first))
7561 *x = *y = *hpos = *vpos = 0;
7562 return 0;
7564 else
7566 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
7567 past_end = 1;
7571 *x = row->x;
7572 *y = row->y;
7573 *vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7575 glyph = row->glyphs[TEXT_AREA];
7576 end = glyph + row->used[TEXT_AREA];
7578 /* Skip over glyphs not having an object at the start of the row.
7579 These are special glyphs like truncation marks on terminal
7580 frames. */
7581 if (row->displays_text_p)
7582 while (glyph < end
7583 && INTEGERP (glyph->object)
7584 && !EQ (stop, glyph->object)
7585 && glyph->charpos < 0)
7587 *x += glyph->pixel_width;
7588 ++glyph;
7591 while (glyph < end
7592 && !INTEGERP (glyph->object)
7593 && !EQ (stop, glyph->object)
7594 && (!BUFFERP (glyph->object)
7595 || glyph->charpos < charpos))
7597 *x += glyph->pixel_width;
7598 ++glyph;
7601 *hpos = glyph - row->glyphs[TEXT_AREA];
7602 return past_end;
7605 #else /* not 0 */
7607 static int
7608 fast_find_position (w, pos, hpos, vpos, x, y, stop)
7609 struct window *w;
7610 int pos;
7611 int *hpos, *vpos, *x, *y;
7612 Lisp_Object stop;
7614 int i;
7615 int lastcol;
7616 int maybe_next_line_p = 0;
7617 int line_start_position;
7618 int yb = window_text_bottom_y (w);
7619 struct glyph_row *row, *best_row;
7620 int row_vpos, best_row_vpos;
7621 int current_x;
7623 row = best_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7624 row_vpos = best_row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
7626 while (row->y < yb)
7628 if (row->used[TEXT_AREA])
7629 line_start_position = row->glyphs[TEXT_AREA]->charpos;
7630 else
7631 line_start_position = 0;
7633 if (line_start_position > pos)
7634 break;
7635 /* If the position sought is the end of the buffer,
7636 don't include the blank lines at the bottom of the window. */
7637 else if (line_start_position == pos
7638 && pos == BUF_ZV (XBUFFER (w->buffer)))
7640 maybe_next_line_p = 1;
7641 break;
7643 else if (line_start_position > 0)
7645 best_row = row;
7646 best_row_vpos = row_vpos;
7649 if (row->y + row->height >= yb)
7650 break;
7652 ++row;
7653 ++row_vpos;
7656 /* Find the right column within BEST_ROW. */
7657 lastcol = 0;
7658 current_x = best_row->x;
7659 for (i = 0; i < best_row->used[TEXT_AREA]; i++)
7661 struct glyph *glyph = best_row->glyphs[TEXT_AREA] + i;
7662 int charpos = glyph->charpos;
7664 if (BUFFERP (glyph->object))
7666 if (charpos == pos)
7668 *hpos = i;
7669 *vpos = best_row_vpos;
7670 *x = current_x;
7671 *y = best_row->y;
7672 return 1;
7674 else if (charpos > pos)
7675 break;
7677 else if (EQ (glyph->object, stop))
7678 break;
7680 if (charpos > 0)
7681 lastcol = i;
7682 current_x += glyph->pixel_width;
7685 /* If we're looking for the end of the buffer,
7686 and we didn't find it in the line we scanned,
7687 use the start of the following line. */
7688 if (maybe_next_line_p)
7690 ++best_row;
7691 ++best_row_vpos;
7692 lastcol = 0;
7693 current_x = best_row->x;
7696 *vpos = best_row_vpos;
7697 *hpos = lastcol + 1;
7698 *x = current_x;
7699 *y = best_row->y;
7700 return 0;
7703 #endif /* not 0 */
7706 /* Find the position of the glyph for position POS in OBJECT in
7707 window W's current matrix, and return in *X, *Y the pixel
7708 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
7710 RIGHT_P non-zero means return the position of the right edge of the
7711 glyph, RIGHT_P zero means return the left edge position.
7713 If no glyph for POS exists in the matrix, return the position of
7714 the glyph with the next smaller position that is in the matrix, if
7715 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
7716 exists in the matrix, return the position of the glyph with the
7717 next larger position in OBJECT.
7719 Value is non-zero if a glyph was found. */
7721 static int
7722 fast_find_string_pos (w, pos, object, hpos, vpos, x, y, right_p)
7723 struct window *w;
7724 int pos;
7725 Lisp_Object object;
7726 int *hpos, *vpos, *x, *y;
7727 int right_p;
7729 int yb = window_text_bottom_y (w);
7730 struct glyph_row *r;
7731 struct glyph *best_glyph = NULL;
7732 struct glyph_row *best_row = NULL;
7733 int best_x = 0;
7735 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7736 r->enabled_p && r->y < yb;
7737 ++r)
7739 struct glyph *g = r->glyphs[TEXT_AREA];
7740 struct glyph *e = g + r->used[TEXT_AREA];
7741 int gx;
7743 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
7744 if (EQ (g->object, object))
7746 if (g->charpos == pos)
7748 best_glyph = g;
7749 best_x = gx;
7750 best_row = r;
7751 goto found;
7753 else if (best_glyph == NULL
7754 || ((abs (g->charpos - pos)
7755 < abs (best_glyph->charpos - pos))
7756 && (right_p
7757 ? g->charpos < pos
7758 : g->charpos > pos)))
7760 best_glyph = g;
7761 best_x = gx;
7762 best_row = r;
7767 found:
7769 if (best_glyph)
7771 *x = best_x;
7772 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
7774 if (right_p)
7776 *x += best_glyph->pixel_width;
7777 ++*hpos;
7780 *y = best_row->y;
7781 *vpos = best_row - w->current_matrix->rows;
7784 return best_glyph != NULL;
7788 /* Display the active region described by mouse_face_*
7789 in its mouse-face if HL > 0, in its normal face if HL = 0. */
7791 static void
7792 show_mouse_face (dpyinfo, draw)
7793 struct x_display_info *dpyinfo;
7794 enum draw_glyphs_face draw;
7796 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
7797 struct frame *f = XFRAME (WINDOW_FRAME (w));
7799 if (/* If window is in the process of being destroyed, don't bother
7800 to do anything. */
7801 w->current_matrix != NULL
7802 /* Don't update mouse highlight if hidden */
7803 && (draw != DRAW_MOUSE_FACE || !dpyinfo->mouse_face_hidden)
7804 /* Recognize when we are called to operate on rows that don't exist
7805 anymore. This can happen when a window is split. */
7806 && dpyinfo->mouse_face_end_row < w->current_matrix->nrows)
7808 int phys_cursor_on_p = w->phys_cursor_on_p;
7809 struct glyph_row *row, *first, *last;
7811 first = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_beg_row);
7812 last = MATRIX_ROW (w->current_matrix, dpyinfo->mouse_face_end_row);
7814 for (row = first; row <= last && row->enabled_p; ++row)
7816 int start_hpos, end_hpos, start_x;
7818 /* For all but the first row, the highlight starts at column 0. */
7819 if (row == first)
7821 start_hpos = dpyinfo->mouse_face_beg_col;
7822 start_x = dpyinfo->mouse_face_beg_x;
7824 else
7826 start_hpos = 0;
7827 start_x = 0;
7830 if (row == last)
7831 end_hpos = dpyinfo->mouse_face_end_col;
7832 else
7833 end_hpos = row->used[TEXT_AREA];
7835 if (end_hpos > start_hpos)
7837 x_draw_glyphs (w, start_x, row, TEXT_AREA,
7838 start_hpos, end_hpos, draw, 0);
7840 row->mouse_face_p
7841 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
7845 /* When we've written over the cursor, arrange for it to
7846 be displayed again. */
7847 if (phys_cursor_on_p && !w->phys_cursor_on_p)
7848 x_display_cursor (w, 1,
7849 w->phys_cursor.hpos, w->phys_cursor.vpos,
7850 w->phys_cursor.x, w->phys_cursor.y);
7853 /* Change the mouse cursor. */
7854 if (draw == DRAW_NORMAL_TEXT)
7855 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7856 f->output_data.x->text_cursor);
7857 else if (draw == DRAW_MOUSE_FACE)
7858 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7859 f->output_data.x->cross_cursor);
7860 else
7861 XDefineCursor (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
7862 f->output_data.x->nontext_cursor);
7865 /* Clear out the mouse-highlighted active region.
7866 Redraw it un-highlighted first. Value is non-zero if mouse
7867 face was actually drawn unhighlighted. */
7869 static int
7870 clear_mouse_face (dpyinfo)
7871 struct x_display_info *dpyinfo;
7873 int cleared = 0;
7875 if (!NILP (dpyinfo->mouse_face_window))
7877 show_mouse_face (dpyinfo, DRAW_NORMAL_TEXT);
7878 cleared = 1;
7881 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7882 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7883 dpyinfo->mouse_face_window = Qnil;
7884 dpyinfo->mouse_face_overlay = Qnil;
7885 return cleared;
7889 /* Clear any mouse-face on window W. This function is part of the
7890 redisplay interface, and is called from try_window_id and similar
7891 functions to ensure the mouse-highlight is off. */
7893 static void
7894 x_clear_mouse_face (w)
7895 struct window *w;
7897 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
7898 Lisp_Object window;
7900 BLOCK_INPUT;
7901 XSETWINDOW (window, w);
7902 if (EQ (window, dpyinfo->mouse_face_window))
7903 clear_mouse_face (dpyinfo);
7904 UNBLOCK_INPUT;
7908 /* Just discard the mouse face information for frame F, if any.
7909 This is used when the size of F is changed. */
7911 void
7912 cancel_mouse_face (f)
7913 FRAME_PTR f;
7915 Lisp_Object window;
7916 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
7918 window = dpyinfo->mouse_face_window;
7919 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
7921 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
7922 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
7923 dpyinfo->mouse_face_window = Qnil;
7928 static int glyph_rect P_ ((struct frame *f, int, int, XRectangle *));
7931 /* Try to determine frame pixel position and size of the glyph under
7932 frame pixel coordinates X/Y on frame F . Return the position and
7933 size in *RECT. Value is non-zero if we could compute these
7934 values. */
7936 static int
7937 glyph_rect (f, x, y, rect)
7938 struct frame *f;
7939 int x, y;
7940 XRectangle *rect;
7942 Lisp_Object window;
7943 int part, found = 0;
7945 window = window_from_coordinates (f, x, y, &part, 0);
7946 if (!NILP (window))
7948 struct window *w = XWINDOW (window);
7949 struct glyph_row *r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
7950 struct glyph_row *end = r + w->current_matrix->nrows - 1;
7952 frame_to_window_pixel_xy (w, &x, &y);
7954 for (; !found && r < end && r->enabled_p; ++r)
7955 if (r->y >= y)
7957 struct glyph *g = r->glyphs[TEXT_AREA];
7958 struct glyph *end = g + r->used[TEXT_AREA];
7959 int gx;
7961 for (gx = r->x; !found && g < end; gx += g->pixel_width, ++g)
7962 if (gx >= x)
7964 rect->width = g->pixel_width;
7965 rect->height = r->height;
7966 rect->x = WINDOW_TO_FRAME_PIXEL_X (w, gx);
7967 rect->y = WINDOW_TO_FRAME_PIXEL_Y (w, r->y);
7968 found = 1;
7973 return found;
7977 /* Return the current position of the mouse.
7978 *FP should be a frame which indicates which display to ask about.
7980 If the mouse movement started in a scroll bar, set *FP, *BAR_WINDOW,
7981 and *PART to the frame, window, and scroll bar part that the mouse
7982 is over. Set *X and *Y to the portion and whole of the mouse's
7983 position on the scroll bar.
7985 If the mouse movement started elsewhere, set *FP to the frame the
7986 mouse is on, *BAR_WINDOW to nil, and *X and *Y to the character cell
7987 the mouse is over.
7989 Set *TIME to the server time-stamp for the time at which the mouse
7990 was at this position.
7992 Don't store anything if we don't have a valid set of values to report.
7994 This clears the mouse_moved flag, so we can wait for the next mouse
7995 movement. */
7997 static void
7998 XTmouse_position (fp, insist, bar_window, part, x, y, time)
7999 FRAME_PTR *fp;
8000 int insist;
8001 Lisp_Object *bar_window;
8002 enum scroll_bar_part *part;
8003 Lisp_Object *x, *y;
8004 unsigned long *time;
8006 FRAME_PTR f1;
8008 BLOCK_INPUT;
8010 if (! NILP (last_mouse_scroll_bar) && insist == 0)
8011 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time);
8012 else
8014 Window root;
8015 int root_x, root_y;
8017 Window dummy_window;
8018 int dummy;
8020 Lisp_Object frame, tail;
8022 /* Clear the mouse-moved flag for every frame on this display. */
8023 FOR_EACH_FRAME (tail, frame)
8024 if (FRAME_X_DISPLAY (XFRAME (frame)) == FRAME_X_DISPLAY (*fp))
8025 XFRAME (frame)->mouse_moved = 0;
8027 last_mouse_scroll_bar = Qnil;
8029 /* Figure out which root window we're on. */
8030 XQueryPointer (FRAME_X_DISPLAY (*fp),
8031 DefaultRootWindow (FRAME_X_DISPLAY (*fp)),
8033 /* The root window which contains the pointer. */
8034 &root,
8036 /* Trash which we can't trust if the pointer is on
8037 a different screen. */
8038 &dummy_window,
8040 /* The position on that root window. */
8041 &root_x, &root_y,
8043 /* More trash we can't trust. */
8044 &dummy, &dummy,
8046 /* Modifier keys and pointer buttons, about which
8047 we don't care. */
8048 (unsigned int *) &dummy);
8050 /* Now we have a position on the root; find the innermost window
8051 containing the pointer. */
8053 Window win, child;
8054 int win_x, win_y;
8055 int parent_x = 0, parent_y = 0;
8056 int count;
8058 win = root;
8060 /* XTranslateCoordinates can get errors if the window
8061 structure is changing at the same time this function
8062 is running. So at least we must not crash from them. */
8064 count = x_catch_errors (FRAME_X_DISPLAY (*fp));
8066 if (FRAME_X_DISPLAY_INFO (*fp)->grabbed && last_mouse_frame
8067 && FRAME_LIVE_P (last_mouse_frame))
8069 /* If mouse was grabbed on a frame, give coords for that frame
8070 even if the mouse is now outside it. */
8071 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8073 /* From-window, to-window. */
8074 root, FRAME_X_WINDOW (last_mouse_frame),
8076 /* From-position, to-position. */
8077 root_x, root_y, &win_x, &win_y,
8079 /* Child of win. */
8080 &child);
8081 f1 = last_mouse_frame;
8083 else
8085 while (1)
8087 XTranslateCoordinates (FRAME_X_DISPLAY (*fp),
8089 /* From-window, to-window. */
8090 root, win,
8092 /* From-position, to-position. */
8093 root_x, root_y, &win_x, &win_y,
8095 /* Child of win. */
8096 &child);
8098 if (child == None || child == win)
8099 break;
8101 win = child;
8102 parent_x = win_x;
8103 parent_y = win_y;
8106 /* Now we know that:
8107 win is the innermost window containing the pointer
8108 (XTC says it has no child containing the pointer),
8109 win_x and win_y are the pointer's position in it
8110 (XTC did this the last time through), and
8111 parent_x and parent_y are the pointer's position in win's parent.
8112 (They are what win_x and win_y were when win was child.
8113 If win is the root window, it has no parent, and
8114 parent_{x,y} are invalid, but that's okay, because we'll
8115 never use them in that case.) */
8117 /* Is win one of our frames? */
8118 f1 = x_any_window_to_frame (FRAME_X_DISPLAY_INFO (*fp), win);
8120 #ifdef USE_X_TOOLKIT
8121 /* If we end up with the menu bar window, say it's not
8122 on the frame. */
8123 if (f1 != NULL
8124 && f1->output_data.x->menubar_widget
8125 && win == XtWindow (f1->output_data.x->menubar_widget))
8126 f1 = NULL;
8127 #endif /* USE_X_TOOLKIT */
8130 if (x_had_errors_p (FRAME_X_DISPLAY (*fp)))
8131 f1 = 0;
8133 x_uncatch_errors (FRAME_X_DISPLAY (*fp), count);
8135 /* If not, is it one of our scroll bars? */
8136 if (! f1)
8138 struct scroll_bar *bar = x_window_to_scroll_bar (win);
8140 if (bar)
8142 f1 = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8143 win_x = parent_x;
8144 win_y = parent_y;
8148 if (f1 == 0 && insist > 0)
8149 f1 = SELECTED_FRAME ();
8151 if (f1)
8153 /* Ok, we found a frame. Store all the values.
8154 last_mouse_glyph is a rectangle used to reduce the
8155 generation of mouse events. To not miss any motion
8156 events, we must divide the frame into rectangles of the
8157 size of the smallest character that could be displayed
8158 on it, i.e. into the same rectangles that matrices on
8159 the frame are divided into. */
8161 int width, height, gx, gy;
8162 XRectangle rect;
8164 if (glyph_rect (f1, win_x, win_y, &rect))
8165 last_mouse_glyph = rect;
8166 else
8168 width = FRAME_SMALLEST_CHAR_WIDTH (f1);
8169 height = FRAME_SMALLEST_FONT_HEIGHT (f1);
8170 gx = win_x;
8171 gy = win_y;
8173 /* Arrange for the division in PIXEL_TO_CHAR_COL etc. to
8174 round down even for negative values. */
8175 if (gx < 0)
8176 gx -= width - 1;
8177 if (gy < 0)
8178 gy -= height - 1;
8179 gx = (gx + width - 1) / width * width;
8180 gy = (gy + height - 1) / height * height;
8182 last_mouse_glyph.width = width;
8183 last_mouse_glyph.height = height;
8184 last_mouse_glyph.x = gx;
8185 last_mouse_glyph.y = gy;
8188 *bar_window = Qnil;
8189 *part = 0;
8190 *fp = f1;
8191 XSETINT (*x, win_x);
8192 XSETINT (*y, win_y);
8193 *time = last_mouse_movement_time;
8198 UNBLOCK_INPUT;
8202 #ifdef USE_X_TOOLKIT
8204 /* Atimer callback function for TIMER. Called every 0.1s to process
8205 Xt timeouts, if needed. We must avoid calling XtAppPending as
8206 much as possible because that function does an implicit XFlush
8207 that slows us down. */
8209 static void
8210 x_process_timeouts (timer)
8211 struct atimer *timer;
8213 if (toolkit_scroll_bar_interaction || popup_activated_flag)
8215 BLOCK_INPUT;
8216 while (XtAppPending (Xt_app_con) & XtIMTimer)
8217 XtAppProcessEvent (Xt_app_con, XtIMTimer);
8218 UNBLOCK_INPUT;
8222 #endif /* USE_X_TOOLKIT */
8225 /* Scroll bar support. */
8227 /* Given an X window ID, find the struct scroll_bar which manages it.
8228 This can be called in GC, so we have to make sure to strip off mark
8229 bits. */
8231 static struct scroll_bar *
8232 x_window_to_scroll_bar (window_id)
8233 Window window_id;
8235 Lisp_Object tail;
8237 for (tail = Vframe_list;
8238 XGCTYPE (tail) == Lisp_Cons;
8239 tail = XCDR (tail))
8241 Lisp_Object frame, bar, condemned;
8243 frame = XCAR (tail);
8244 /* All elements of Vframe_list should be frames. */
8245 if (! GC_FRAMEP (frame))
8246 abort ();
8248 /* Scan this frame's scroll bar list for a scroll bar with the
8249 right window ID. */
8250 condemned = FRAME_CONDEMNED_SCROLL_BARS (XFRAME (frame));
8251 for (bar = FRAME_SCROLL_BARS (XFRAME (frame));
8252 /* This trick allows us to search both the ordinary and
8253 condemned scroll bar lists with one loop. */
8254 ! GC_NILP (bar) || (bar = condemned,
8255 condemned = Qnil,
8256 ! GC_NILP (bar));
8257 bar = XSCROLL_BAR (bar)->next)
8258 if (SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)) == window_id)
8259 return XSCROLL_BAR (bar);
8262 return 0;
8266 #if defined USE_LUCID
8268 /* Return the Lucid menu bar WINDOW is part of. Return null
8269 if WINDOW is not part of a menu bar. */
8271 static Widget
8272 x_window_to_menu_bar (window)
8273 Window window;
8275 Lisp_Object tail;
8277 for (tail = Vframe_list;
8278 XGCTYPE (tail) == Lisp_Cons;
8279 tail = XCDR (tail))
8281 Lisp_Object frame = XCAR (tail);
8282 Widget menu_bar = XFRAME (frame)->output_data.x->menubar_widget;
8284 if (menu_bar && xlwmenu_window_p (menu_bar, window))
8285 return menu_bar;
8288 return NULL;
8291 #endif /* USE_LUCID */
8294 /************************************************************************
8295 Toolkit scroll bars
8296 ************************************************************************/
8298 #ifdef USE_TOOLKIT_SCROLL_BARS
8300 static void x_scroll_bar_to_input_event P_ ((XEvent *, struct input_event *));
8301 static void x_send_scroll_bar_event P_ ((Lisp_Object, int, int, int));
8302 static void x_create_toolkit_scroll_bar P_ ((struct frame *,
8303 struct scroll_bar *));
8304 static void x_set_toolkit_scroll_bar_thumb P_ ((struct scroll_bar *,
8305 int, int, int));
8308 /* Id of action hook installed for scroll bars. */
8310 static XtActionHookId action_hook_id;
8312 /* Lisp window being scrolled. Set when starting to interact with
8313 a toolkit scroll bar, reset to nil when ending the interaction. */
8315 static Lisp_Object window_being_scrolled;
8317 /* Last scroll bar part sent in xm_scroll_callback. */
8319 static int last_scroll_bar_part;
8321 /* Whether this is an Xaw with arrow-scrollbars. This should imply
8322 that movements of 1/20 of the screen size are mapped to up/down. */
8324 static Boolean xaw3d_arrow_scroll;
8326 /* Whether the drag scrolling maintains the mouse at the top of the
8327 thumb. If not, resizing the thumb needs to be done more carefully
8328 to avoid jerkyness. */
8330 static Boolean xaw3d_pick_top;
8333 /* Action hook installed via XtAppAddActionHook when toolkit scroll
8334 bars are used.. The hook is responsible for detecting when
8335 the user ends an interaction with the scroll bar, and generates
8336 a `end-scroll' scroll_bar_click' event if so. */
8338 static void
8339 xt_action_hook (widget, client_data, action_name, event, params,
8340 num_params)
8341 Widget widget;
8342 XtPointer client_data;
8343 String action_name;
8344 XEvent *event;
8345 String *params;
8346 Cardinal *num_params;
8348 int scroll_bar_p;
8349 char *end_action;
8351 #ifdef USE_MOTIF
8352 scroll_bar_p = XmIsScrollBar (widget);
8353 end_action = "Release";
8354 #else /* !USE_MOTIF i.e. use Xaw */
8355 scroll_bar_p = XtIsSubclass (widget, scrollbarWidgetClass);
8356 end_action = "EndScroll";
8357 #endif /* USE_MOTIF */
8359 if (scroll_bar_p
8360 && strcmp (action_name, end_action) == 0
8361 && WINDOWP (window_being_scrolled))
8363 struct window *w;
8365 x_send_scroll_bar_event (window_being_scrolled,
8366 scroll_bar_end_scroll, 0, 0);
8367 w = XWINDOW (window_being_scrolled);
8368 XSCROLL_BAR (w->vertical_scroll_bar)->dragging = Qnil;
8369 window_being_scrolled = Qnil;
8370 last_scroll_bar_part = -1;
8372 /* Xt timeouts no longer needed. */
8373 toolkit_scroll_bar_interaction = 0;
8377 /* A vector of windows used for communication between
8378 x_send_scroll_bar_event and x_scroll_bar_to_input_event. */
8380 static struct window **scroll_bar_windows;
8381 static int scroll_bar_windows_size;
8384 /* Send a client message with message type Xatom_Scrollbar for a
8385 scroll action to the frame of WINDOW. PART is a value identifying
8386 the part of the scroll bar that was clicked on. PORTION is the
8387 amount to scroll of a whole of WHOLE. */
8389 static void
8390 x_send_scroll_bar_event (window, part, portion, whole)
8391 Lisp_Object window;
8392 int part, portion, whole;
8394 XEvent event;
8395 XClientMessageEvent *ev = (XClientMessageEvent *) &event;
8396 struct window *w = XWINDOW (window);
8397 struct frame *f = XFRAME (w->frame);
8398 int i;
8400 BLOCK_INPUT;
8402 /* Construct a ClientMessage event to send to the frame. */
8403 ev->type = ClientMessage;
8404 ev->message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_Scrollbar;
8405 ev->display = FRAME_X_DISPLAY (f);
8406 ev->window = FRAME_X_WINDOW (f);
8407 ev->format = 32;
8409 /* We can only transfer 32 bits in the XClientMessageEvent, which is
8410 not enough to store a pointer or Lisp_Object on a 64 bit system.
8411 So, store the window in scroll_bar_windows and pass the index
8412 into that array in the event. */
8413 for (i = 0; i < scroll_bar_windows_size; ++i)
8414 if (scroll_bar_windows[i] == NULL)
8415 break;
8417 if (i == scroll_bar_windows_size)
8419 int new_size = max (10, 2 * scroll_bar_windows_size);
8420 size_t nbytes = new_size * sizeof *scroll_bar_windows;
8421 size_t old_nbytes = scroll_bar_windows_size * sizeof *scroll_bar_windows;
8423 scroll_bar_windows = (struct window **) xrealloc (scroll_bar_windows,
8424 nbytes);
8425 bzero (&scroll_bar_windows[i], nbytes - old_nbytes);
8426 scroll_bar_windows_size = new_size;
8429 scroll_bar_windows[i] = w;
8430 ev->data.l[0] = (long) i;
8431 ev->data.l[1] = (long) part;
8432 ev->data.l[2] = (long) 0;
8433 ev->data.l[3] = (long) portion;
8434 ev->data.l[4] = (long) whole;
8436 /* Make Xt timeouts work while the scroll bar is active. */
8437 toolkit_scroll_bar_interaction = 1;
8439 /* Setting the event mask to zero means that the message will
8440 be sent to the client that created the window, and if that
8441 window no longer exists, no event will be sent. */
8442 XSendEvent (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), False, 0, &event);
8443 UNBLOCK_INPUT;
8447 /* Transform a scroll bar ClientMessage EVENT to an Emacs input event
8448 in *IEVENT. */
8450 static void
8451 x_scroll_bar_to_input_event (event, ievent)
8452 XEvent *event;
8453 struct input_event *ievent;
8455 XClientMessageEvent *ev = (XClientMessageEvent *) event;
8456 Lisp_Object window;
8457 struct frame *f;
8458 struct window *w;
8460 w = scroll_bar_windows[ev->data.l[0]];
8461 scroll_bar_windows[ev->data.l[0]] = NULL;
8463 XSETWINDOW (window, w);
8464 f = XFRAME (w->frame);
8466 ievent->kind = scroll_bar_click;
8467 ievent->frame_or_window = window;
8468 ievent->arg = Qnil;
8469 ievent->timestamp = XtLastTimestampProcessed (FRAME_X_DISPLAY (f));
8470 ievent->part = ev->data.l[1];
8471 ievent->code = ev->data.l[2];
8472 ievent->x = make_number ((int) ev->data.l[3]);
8473 ievent->y = make_number ((int) ev->data.l[4]);
8474 ievent->modifiers = 0;
8478 #ifdef USE_MOTIF
8480 /* Minimum and maximum values used for Motif scroll bars. */
8482 #define XM_SB_MIN 1
8483 #define XM_SB_MAX 10000000
8484 #define XM_SB_RANGE (XM_SB_MAX - XM_SB_MIN)
8487 /* Scroll bar callback for Motif scroll bars. WIDGET is the scroll
8488 bar widget. CLIENT_DATA is a pointer to the scroll_bar structure.
8489 CALL_DATA is a pointer a a XmScrollBarCallbackStruct. */
8491 static void
8492 xm_scroll_callback (widget, client_data, call_data)
8493 Widget widget;
8494 XtPointer client_data, call_data;
8496 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8497 XmScrollBarCallbackStruct *cs = (XmScrollBarCallbackStruct *) call_data;
8498 int part = -1, whole = 0, portion = 0;
8500 switch (cs->reason)
8502 case XmCR_DECREMENT:
8503 bar->dragging = Qnil;
8504 part = scroll_bar_up_arrow;
8505 break;
8507 case XmCR_INCREMENT:
8508 bar->dragging = Qnil;
8509 part = scroll_bar_down_arrow;
8510 break;
8512 case XmCR_PAGE_DECREMENT:
8513 bar->dragging = Qnil;
8514 part = scroll_bar_above_handle;
8515 break;
8517 case XmCR_PAGE_INCREMENT:
8518 bar->dragging = Qnil;
8519 part = scroll_bar_below_handle;
8520 break;
8522 case XmCR_TO_TOP:
8523 bar->dragging = Qnil;
8524 part = scroll_bar_to_top;
8525 break;
8527 case XmCR_TO_BOTTOM:
8528 bar->dragging = Qnil;
8529 part = scroll_bar_to_bottom;
8530 break;
8532 case XmCR_DRAG:
8534 int slider_size;
8535 int dragging_down_p = (INTEGERP (bar->dragging)
8536 && XINT (bar->dragging) <= cs->value);
8538 /* Get the slider size. */
8539 BLOCK_INPUT;
8540 XtVaGetValues (widget, XmNsliderSize, &slider_size, NULL);
8541 UNBLOCK_INPUT;
8543 /* At the max position of the scroll bar, do a line-wise
8544 movement. Without doing anything, we would be called with
8545 the same cs->value again and again. If we want to make
8546 sure that we can reach the end of the buffer, we have to do
8547 something.
8549 Implementation note: setting bar->dragging always to
8550 cs->value gives a smoother movement at the max position.
8551 Setting it to nil when doing line-wise movement gives
8552 a better slider behavior. */
8554 if (cs->value + slider_size == XM_SB_MAX
8555 || (dragging_down_p
8556 && last_scroll_bar_part == scroll_bar_down_arrow))
8558 part = scroll_bar_down_arrow;
8559 bar->dragging = Qnil;
8561 else
8563 whole = XM_SB_RANGE;
8564 portion = min (cs->value - XM_SB_MIN, XM_SB_MAX - slider_size);
8565 part = scroll_bar_handle;
8566 bar->dragging = make_number (cs->value);
8569 break;
8571 case XmCR_VALUE_CHANGED:
8572 break;
8575 if (part >= 0)
8577 window_being_scrolled = bar->window;
8578 last_scroll_bar_part = part;
8579 x_send_scroll_bar_event (bar->window, part, portion, whole);
8584 #else /* !USE_MOTIF, i.e. Xaw. */
8587 /* Xaw scroll bar callback. Invoked when the thumb is dragged.
8588 WIDGET is the scroll bar widget. CLIENT_DATA is a pointer to the
8589 scroll bar struct. CALL_DATA is a pointer to a float saying where
8590 the thumb is. */
8592 static void
8593 xaw_jump_callback (widget, client_data, call_data)
8594 Widget widget;
8595 XtPointer client_data, call_data;
8597 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8598 float top = *(float *) call_data;
8599 float shown;
8600 int whole, portion, height;
8601 int part;
8603 /* Get the size of the thumb, a value between 0 and 1. */
8604 BLOCK_INPUT;
8605 XtVaGetValues (widget, XtNshown, &shown, XtNheight, &height, NULL);
8606 UNBLOCK_INPUT;
8608 whole = 10000000;
8609 portion = shown < 1 ? top * whole : 0;
8611 if (shown < 1 && (abs (top + shown - 1) < 1.0/height))
8612 /* Some derivatives of Xaw refuse to shrink the thumb when you reach
8613 the bottom, so we force the scrolling whenever we see that we're
8614 too close to the bottom (in x_set_toolkit_scroll_bar_thumb
8615 we try to ensure that we always stay two pixels away from the
8616 bottom). */
8617 part = scroll_bar_down_arrow;
8618 else
8619 part = scroll_bar_handle;
8621 window_being_scrolled = bar->window;
8622 bar->dragging = make_number (portion);
8623 last_scroll_bar_part = part;
8624 x_send_scroll_bar_event (bar->window, part, portion, whole);
8628 /* Xaw scroll bar callback. Invoked for incremental scrolling.,
8629 i.e. line or page up or down. WIDGET is the Xaw scroll bar
8630 widget. CLIENT_DATA is a pointer to the scroll_bar structure for
8631 the scroll bar. CALL_DATA is an integer specifying the action that
8632 has taken place. It's magnitude is in the range 0..height of the
8633 scroll bar. Negative values mean scroll towards buffer start.
8634 Values < height of scroll bar mean line-wise movement. */
8636 static void
8637 xaw_scroll_callback (widget, client_data, call_data)
8638 Widget widget;
8639 XtPointer client_data, call_data;
8641 struct scroll_bar *bar = (struct scroll_bar *) client_data;
8642 int position = (int) call_data;
8643 Dimension height;
8644 int part;
8646 /* Get the height of the scroll bar. */
8647 BLOCK_INPUT;
8648 XtVaGetValues (widget, XtNheight, &height, NULL);
8649 UNBLOCK_INPUT;
8651 if (abs (position) >= height)
8652 part = (position < 0) ? scroll_bar_above_handle : scroll_bar_below_handle;
8654 /* If Xaw3d was compiled with ARROW_SCROLLBAR,
8655 it maps line-movement to call_data = max(5, height/20). */
8656 else if (xaw3d_arrow_scroll && abs (position) <= max (5, height / 20))
8657 part = (position < 0) ? scroll_bar_up_arrow : scroll_bar_down_arrow;
8658 else
8659 part = scroll_bar_move_ratio;
8661 window_being_scrolled = bar->window;
8662 bar->dragging = Qnil;
8663 last_scroll_bar_part = part;
8664 x_send_scroll_bar_event (bar->window, part, position, height);
8668 #endif /* not USE_MOTIF */
8671 /* Create the widget for scroll bar BAR on frame F. Record the widget
8672 and X window of the scroll bar in BAR. */
8674 static void
8675 x_create_toolkit_scroll_bar (f, bar)
8676 struct frame *f;
8677 struct scroll_bar *bar;
8679 Window xwindow;
8680 Widget widget;
8681 Arg av[20];
8682 int ac = 0;
8683 char *scroll_bar_name = "verticalScrollBar";
8684 unsigned long pixel;
8686 BLOCK_INPUT;
8688 #ifdef USE_MOTIF
8689 /* Set resources. Create the widget. */
8690 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8691 XtSetArg (av[ac], XmNminimum, XM_SB_MIN); ++ac;
8692 XtSetArg (av[ac], XmNmaximum, XM_SB_MAX); ++ac;
8693 XtSetArg (av[ac], XmNorientation, XmVERTICAL); ++ac;
8694 XtSetArg (av[ac], XmNprocessingDirection, XmMAX_ON_BOTTOM), ++ac;
8695 XtSetArg (av[ac], XmNincrement, 1); ++ac;
8696 XtSetArg (av[ac], XmNpageIncrement, 1); ++ac;
8698 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8699 if (pixel != -1)
8701 XtSetArg (av[ac], XmNforeground, pixel);
8702 ++ac;
8705 pixel = f->output_data.x->scroll_bar_background_pixel;
8706 if (pixel != -1)
8708 XtSetArg (av[ac], XmNbackground, pixel);
8709 ++ac;
8712 widget = XmCreateScrollBar (f->output_data.x->edit_widget,
8713 scroll_bar_name, av, ac);
8715 /* Add one callback for everything that can happen. */
8716 XtAddCallback (widget, XmNdecrementCallback, xm_scroll_callback,
8717 (XtPointer) bar);
8718 XtAddCallback (widget, XmNdragCallback, xm_scroll_callback,
8719 (XtPointer) bar);
8720 XtAddCallback (widget, XmNincrementCallback, xm_scroll_callback,
8721 (XtPointer) bar);
8722 XtAddCallback (widget, XmNpageDecrementCallback, xm_scroll_callback,
8723 (XtPointer) bar);
8724 XtAddCallback (widget, XmNpageIncrementCallback, xm_scroll_callback,
8725 (XtPointer) bar);
8726 XtAddCallback (widget, XmNtoBottomCallback, xm_scroll_callback,
8727 (XtPointer) bar);
8728 XtAddCallback (widget, XmNtoTopCallback, xm_scroll_callback,
8729 (XtPointer) bar);
8731 /* Realize the widget. Only after that is the X window created. */
8732 XtRealizeWidget (widget);
8734 /* Set the cursor to an arrow. I didn't find a resource to do that.
8735 And I'm wondering why it hasn't an arrow cursor by default. */
8736 XDefineCursor (XtDisplay (widget), XtWindow (widget),
8737 f->output_data.x->nontext_cursor);
8739 #else /* !USE_MOTIF i.e. use Xaw */
8741 /* Set resources. Create the widget. The background of the
8742 Xaw3d scroll bar widget is a little bit light for my taste.
8743 We don't alter it here to let users change it according
8744 to their taste with `emacs*verticalScrollBar.background: xxx'. */
8745 XtSetArg (av[ac], XtNmappedWhenManaged, False); ++ac;
8746 XtSetArg (av[ac], XtNorientation, XtorientVertical); ++ac;
8747 /* For smoother scrolling with Xaw3d -sm */
8748 /* XtSetArg (av[ac], XtNpickTop, True); ++ac; */
8750 pixel = f->output_data.x->scroll_bar_foreground_pixel;
8751 if (pixel != -1)
8753 XtSetArg (av[ac], XtNforeground, pixel);
8754 ++ac;
8757 pixel = f->output_data.x->scroll_bar_background_pixel;
8758 if (pixel != -1)
8760 XtSetArg (av[ac], XtNbackground, pixel);
8761 ++ac;
8764 /* Top/bottom shadow colors. */
8766 /* Allocate them, if necessary. */
8767 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1)
8769 pixel = f->output_data.x->scroll_bar_background_pixel;
8770 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8771 &pixel, 1.2, 0x8000))
8772 pixel = -1;
8773 f->output_data.x->scroll_bar_top_shadow_pixel = pixel;
8775 if (f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8777 pixel = f->output_data.x->scroll_bar_background_pixel;
8778 if (!x_alloc_lighter_color (f, FRAME_X_DISPLAY (f), FRAME_X_COLORMAP (f),
8779 &pixel, 0.6, 0x4000))
8780 pixel = -1;
8781 f->output_data.x->scroll_bar_bottom_shadow_pixel = pixel;
8784 /* Tell the toolkit about them. */
8785 if (f->output_data.x->scroll_bar_top_shadow_pixel == -1
8786 || f->output_data.x->scroll_bar_bottom_shadow_pixel == -1)
8787 /* We tried to allocate a color for the top/bottom shadow, and
8788 failed, so tell Xaw3d to use dithering instead. */
8790 XtSetArg (av[ac], XtNbeNiceToColormap, True);
8791 ++ac;
8793 else
8794 /* Tell what colors Xaw3d should use for the top/bottom shadow, to
8795 be more consistent with other emacs 3d colors, and since Xaw3d is
8796 not good at dealing with allocation failure. */
8798 /* This tells Xaw3d to use real colors instead of dithering for
8799 the shadows. */
8800 XtSetArg (av[ac], XtNbeNiceToColormap, False);
8801 ++ac;
8803 /* Specify the colors. */
8804 pixel = f->output_data.x->scroll_bar_top_shadow_pixel;
8805 if (pixel != -1)
8807 XtSetArg (av[ac], "topShadowPixel", pixel);
8808 ++ac;
8810 pixel = f->output_data.x->scroll_bar_bottom_shadow_pixel;
8811 if (pixel != -1)
8813 XtSetArg (av[ac], "bottomShadowPixel", pixel);
8814 ++ac;
8818 widget = XtCreateWidget (scroll_bar_name, scrollbarWidgetClass,
8819 f->output_data.x->edit_widget, av, ac);
8822 char *initial = "";
8823 char *val = initial;
8824 XtVaGetValues (widget, XtNscrollVCursor, (XtPointer) &val,
8825 XtNpickTop, (XtPointer) &xaw3d_pick_top, NULL);
8826 if (val == initial)
8827 { /* ARROW_SCROLL */
8828 xaw3d_arrow_scroll = True;
8829 /* Isn't that just a personal preference ? -sm */
8830 XtVaSetValues (widget, XtNcursorName, "top_left_arrow", NULL);
8834 /* Define callbacks. */
8835 XtAddCallback (widget, XtNjumpProc, xaw_jump_callback, (XtPointer) bar);
8836 XtAddCallback (widget, XtNscrollProc, xaw_scroll_callback,
8837 (XtPointer) bar);
8839 /* Realize the widget. Only after that is the X window created. */
8840 XtRealizeWidget (widget);
8842 #endif /* !USE_MOTIF */
8844 /* Install an action hook that let's us detect when the user
8845 finishes interacting with a scroll bar. */
8846 if (action_hook_id == 0)
8847 action_hook_id = XtAppAddActionHook (Xt_app_con, xt_action_hook, 0);
8849 /* Remember X window and widget in the scroll bar vector. */
8850 SET_SCROLL_BAR_X_WIDGET (bar, widget);
8851 xwindow = XtWindow (widget);
8852 SET_SCROLL_BAR_X_WINDOW (bar, xwindow);
8854 UNBLOCK_INPUT;
8858 /* Set the thumb size and position of scroll bar BAR. We are currently
8859 displaying PORTION out of a whole WHOLE, and our position POSITION. */
8861 static void
8862 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole)
8863 struct scroll_bar *bar;
8864 int portion, position, whole;
8866 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
8867 Widget widget = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
8868 float top, shown;
8870 if (whole == 0)
8871 top = 0, shown = 1;
8872 else
8874 top = (float) position / whole;
8875 shown = (float) portion / whole;
8878 BLOCK_INPUT;
8880 #ifdef USE_MOTIF
8882 int size, value;
8884 /* Slider size. Must be in the range [1 .. MAX - MIN] where MAX
8885 is the scroll bar's maximum and MIN is the scroll bar's minimum
8886 value. */
8887 size = shown * XM_SB_RANGE;
8888 size = min (size, XM_SB_RANGE);
8889 size = max (size, 1);
8891 /* Position. Must be in the range [MIN .. MAX - SLIDER_SIZE]. */
8892 value = top * XM_SB_RANGE;
8893 value = min (value, XM_SB_MAX - size);
8894 value = max (value, XM_SB_MIN);
8896 if (NILP (bar->dragging))
8897 XmScrollBarSetValues (widget, value, size, 0, 0, False);
8898 else if (last_scroll_bar_part == scroll_bar_down_arrow)
8899 /* This has the negative side effect that the slider value is
8900 not what it would be if we scrolled here using line-wise or
8901 page-wise movement. */
8902 XmScrollBarSetValues (widget, value, XM_SB_RANGE - value, 0, 0, False);
8903 else
8905 /* If currently dragging, only update the slider size.
8906 This reduces flicker effects. */
8907 int old_value, old_size, increment, page_increment;
8909 XmScrollBarGetValues (widget, &old_value, &old_size,
8910 &increment, &page_increment);
8911 XmScrollBarSetValues (widget, old_value,
8912 min (size, XM_SB_RANGE - old_value),
8913 0, 0, False);
8916 #else /* !USE_MOTIF i.e. use Xaw */
8918 float old_top, old_shown;
8919 Dimension height;
8920 XtVaGetValues (widget,
8921 XtNtopOfThumb, &old_top,
8922 XtNshown, &old_shown,
8923 XtNheight, &height,
8924 NULL);
8926 /* Massage the top+shown values. */
8927 if (NILP (bar->dragging) || last_scroll_bar_part == scroll_bar_down_arrow)
8928 top = max (0, min (1, top));
8929 else
8930 top = old_top;
8931 /* Keep two pixels available for moving the thumb down. */
8932 shown = max (0, min (1 - top - (2.0 / height), shown));
8934 /* If the call to XawScrollbarSetThumb below doesn't seem to work,
8935 check that your system's configuration file contains a define
8936 for `NARROWPROTO'. See s/freebsd.h for an example. */
8937 if (top != old_top || shown != old_shown)
8939 if (NILP (bar->dragging))
8940 XawScrollbarSetThumb (widget, top, shown);
8941 else
8943 #ifdef HAVE_XAW3D
8944 ScrollbarWidget sb = (ScrollbarWidget) widget;
8945 int scroll_mode = 0;
8947 /* `scroll_mode' only exists with Xaw3d + ARROW_SCROLLBAR. */
8948 if (xaw3d_arrow_scroll)
8950 /* Xaw3d stupidly ignores resize requests while dragging
8951 so we have to make it believe it's not in dragging mode. */
8952 scroll_mode = sb->scrollbar.scroll_mode;
8953 if (scroll_mode == 2)
8954 sb->scrollbar.scroll_mode = 0;
8956 #endif
8957 /* Try to make the scrolling a tad smoother. */
8958 if (!xaw3d_pick_top)
8959 shown = min (shown, old_shown);
8961 XawScrollbarSetThumb (widget, top, shown);
8963 #ifdef HAVE_XAW3D
8964 if (xaw3d_arrow_scroll && scroll_mode == 2)
8965 sb->scrollbar.scroll_mode = scroll_mode;
8966 #endif
8970 #endif /* !USE_MOTIF */
8972 UNBLOCK_INPUT;
8975 #endif /* USE_TOOLKIT_SCROLL_BARS */
8979 /************************************************************************
8980 Scroll bars, general
8981 ************************************************************************/
8983 /* Create a scroll bar and return the scroll bar vector for it. W is
8984 the Emacs window on which to create the scroll bar. TOP, LEFT,
8985 WIDTH and HEIGHT are.the pixel coordinates and dimensions of the
8986 scroll bar. */
8988 static struct scroll_bar *
8989 x_scroll_bar_create (w, top, left, width, height)
8990 struct window *w;
8991 int top, left, width, height;
8993 struct frame *f = XFRAME (w->frame);
8994 struct scroll_bar *bar
8995 = XSCROLL_BAR (Fmake_vector (make_number (SCROLL_BAR_VEC_SIZE), Qnil));
8997 BLOCK_INPUT;
8999 #ifdef USE_TOOLKIT_SCROLL_BARS
9000 x_create_toolkit_scroll_bar (f, bar);
9001 #else /* not USE_TOOLKIT_SCROLL_BARS */
9003 XSetWindowAttributes a;
9004 unsigned long mask;
9005 Window window;
9007 a.background_pixel = f->output_data.x->scroll_bar_background_pixel;
9008 if (a.background_pixel == -1)
9009 a.background_pixel = f->output_data.x->background_pixel;
9011 a.event_mask = (ButtonPressMask | ButtonReleaseMask
9012 | ButtonMotionMask | PointerMotionHintMask
9013 | ExposureMask);
9014 a.cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
9016 mask = (CWBackPixel | CWEventMask | CWCursor);
9018 /* Clear the area of W that will serve as a scroll bar. This is
9019 for the case that a window has been split horizontally. In
9020 this case, no clear_frame is generated to reduce flickering. */
9021 if (width > 0 && height > 0)
9022 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9023 left, top, width,
9024 window_box_height (w), False);
9026 window = XCreateWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9027 /* Position and size of scroll bar. */
9028 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9029 top,
9030 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9031 height,
9032 /* Border width, depth, class, and visual. */
9034 CopyFromParent,
9035 CopyFromParent,
9036 CopyFromParent,
9037 /* Attributes. */
9038 mask, &a);
9039 SET_SCROLL_BAR_X_WINDOW (bar, window);
9041 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9043 XSETWINDOW (bar->window, w);
9044 XSETINT (bar->top, top);
9045 XSETINT (bar->left, left);
9046 XSETINT (bar->width, width);
9047 XSETINT (bar->height, height);
9048 XSETINT (bar->start, 0);
9049 XSETINT (bar->end, 0);
9050 bar->dragging = Qnil;
9052 /* Add bar to its frame's list of scroll bars. */
9053 bar->next = FRAME_SCROLL_BARS (f);
9054 bar->prev = Qnil;
9055 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9056 if (!NILP (bar->next))
9057 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9059 /* Map the window/widget. */
9060 #ifdef USE_TOOLKIT_SCROLL_BARS
9062 Widget scroll_bar = SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar);
9063 XtConfigureWidget (scroll_bar,
9064 left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9065 top,
9066 width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9067 max (height, 1), 0);
9068 XtMapWidget (scroll_bar);
9070 #else /* not USE_TOOLKIT_SCROLL_BARS */
9071 XMapRaised (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9072 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9074 UNBLOCK_INPUT;
9075 return bar;
9079 /* Draw BAR's handle in the proper position.
9081 If the handle is already drawn from START to END, don't bother
9082 redrawing it, unless REBUILD is non-zero; in that case, always
9083 redraw it. (REBUILD is handy for drawing the handle after expose
9084 events.)
9086 Normally, we want to constrain the start and end of the handle to
9087 fit inside its rectangle, but if the user is dragging the scroll
9088 bar handle, we want to let them drag it down all the way, so that
9089 the bar's top is as far down as it goes; otherwise, there's no way
9090 to move to the very end of the buffer. */
9092 #ifndef USE_TOOLKIT_SCROLL_BARS
9094 static void
9095 x_scroll_bar_set_handle (bar, start, end, rebuild)
9096 struct scroll_bar *bar;
9097 int start, end;
9098 int rebuild;
9100 int dragging = ! NILP (bar->dragging);
9101 Window w = SCROLL_BAR_X_WINDOW (bar);
9102 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9103 GC gc = f->output_data.x->normal_gc;
9105 /* If the display is already accurate, do nothing. */
9106 if (! rebuild
9107 && start == XINT (bar->start)
9108 && end == XINT (bar->end))
9109 return;
9111 BLOCK_INPUT;
9114 int inside_width = VERTICAL_SCROLL_BAR_INSIDE_WIDTH (f, XINT (bar->width));
9115 int inside_height = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9116 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9118 /* Make sure the values are reasonable, and try to preserve
9119 the distance between start and end. */
9121 int length = end - start;
9123 if (start < 0)
9124 start = 0;
9125 else if (start > top_range)
9126 start = top_range;
9127 end = start + length;
9129 if (end < start)
9130 end = start;
9131 else if (end > top_range && ! dragging)
9132 end = top_range;
9135 /* Store the adjusted setting in the scroll bar. */
9136 XSETINT (bar->start, start);
9137 XSETINT (bar->end, end);
9139 /* Clip the end position, just for display. */
9140 if (end > top_range)
9141 end = top_range;
9143 /* Draw bottom positions VERTICAL_SCROLL_BAR_MIN_HANDLE pixels
9144 below top positions, to make sure the handle is always at least
9145 that many pixels tall. */
9146 end += VERTICAL_SCROLL_BAR_MIN_HANDLE;
9148 /* Draw the empty space above the handle. Note that we can't clear
9149 zero-height areas; that means "clear to end of window." */
9150 if (0 < start)
9151 x_clear_area (FRAME_X_DISPLAY (f), w,
9152 /* x, y, width, height, and exposures. */
9153 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9154 VERTICAL_SCROLL_BAR_TOP_BORDER,
9155 inside_width, start,
9156 False);
9158 /* Change to proper foreground color if one is specified. */
9159 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9160 XSetForeground (FRAME_X_DISPLAY (f), gc,
9161 f->output_data.x->scroll_bar_foreground_pixel);
9163 /* Draw the handle itself. */
9164 XFillRectangle (FRAME_X_DISPLAY (f), w, gc,
9165 /* x, y, width, height */
9166 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9167 VERTICAL_SCROLL_BAR_TOP_BORDER + start,
9168 inside_width, end - start);
9170 /* Restore the foreground color of the GC if we changed it above. */
9171 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
9172 XSetForeground (FRAME_X_DISPLAY (f), gc,
9173 f->output_data.x->foreground_pixel);
9175 /* Draw the empty space below the handle. Note that we can't
9176 clear zero-height areas; that means "clear to end of window." */
9177 if (end < inside_height)
9178 x_clear_area (FRAME_X_DISPLAY (f), w,
9179 /* x, y, width, height, and exposures. */
9180 VERTICAL_SCROLL_BAR_LEFT_BORDER,
9181 VERTICAL_SCROLL_BAR_TOP_BORDER + end,
9182 inside_width, inside_height - end,
9183 False);
9187 UNBLOCK_INPUT;
9190 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9192 /* Destroy scroll bar BAR, and set its Emacs window's scroll bar to
9193 nil. */
9195 static void
9196 x_scroll_bar_remove (bar)
9197 struct scroll_bar *bar;
9199 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9200 BLOCK_INPUT;
9202 #ifdef USE_TOOLKIT_SCROLL_BARS
9203 XtDestroyWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar));
9204 #else
9205 XDestroyWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar));
9206 #endif
9208 /* Disassociate this scroll bar from its window. */
9209 XWINDOW (bar->window)->vertical_scroll_bar = Qnil;
9211 UNBLOCK_INPUT;
9215 /* Set the handle of the vertical scroll bar for WINDOW to indicate
9216 that we are displaying PORTION characters out of a total of WHOLE
9217 characters, starting at POSITION. If WINDOW has no scroll bar,
9218 create one. */
9220 static void
9221 XTset_vertical_scroll_bar (w, portion, whole, position)
9222 struct window *w;
9223 int portion, whole, position;
9225 struct frame *f = XFRAME (w->frame);
9226 struct scroll_bar *bar;
9227 int top, height, left, sb_left, width, sb_width;
9228 int window_x, window_y, window_width, window_height;
9230 /* Get window dimensions. */
9231 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
9232 top = window_y;
9233 width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9234 height = window_height;
9236 /* Compute the left edge of the scroll bar area. */
9237 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9238 left = XINT (w->left) + XINT (w->width) - FRAME_SCROLL_BAR_COLS (f);
9239 else
9240 left = XFASTINT (w->left);
9241 left *= CANON_X_UNIT (f);
9242 left += FRAME_INTERNAL_BORDER_WIDTH (f);
9244 /* Compute the width of the scroll bar which might be less than
9245 the width of the area reserved for the scroll bar. */
9246 if (FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0)
9247 sb_width = FRAME_SCROLL_BAR_PIXEL_WIDTH (f);
9248 else
9249 sb_width = width;
9251 /* Compute the left edge of the scroll bar. */
9252 #ifdef USE_TOOLKIT_SCROLL_BARS
9253 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9254 sb_left = left + width - sb_width - (width - sb_width) / 2;
9255 else
9256 sb_left = left + (width - sb_width) / 2;
9257 #else
9258 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_RIGHT (f))
9259 sb_left = left + width - sb_width;
9260 else
9261 sb_left = left;
9262 #endif
9264 /* Does the scroll bar exist yet? */
9265 if (NILP (w->vertical_scroll_bar))
9267 if (width > 0 && height > 0)
9269 BLOCK_INPUT;
9270 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9271 left, top, width, height, False);
9272 UNBLOCK_INPUT;
9275 bar = x_scroll_bar_create (w, top, sb_left, sb_width, height);
9277 else
9279 /* It may just need to be moved and resized. */
9280 unsigned int mask = 0;
9282 bar = XSCROLL_BAR (w->vertical_scroll_bar);
9284 BLOCK_INPUT;
9286 if (sb_left != XINT (bar->left))
9287 mask |= CWX;
9288 if (top != XINT (bar->top))
9289 mask |= CWY;
9290 if (sb_width != XINT (bar->width))
9291 mask |= CWWidth;
9292 if (height != XINT (bar->height))
9293 mask |= CWHeight;
9295 #ifdef USE_TOOLKIT_SCROLL_BARS
9297 /* Since toolkit scroll bars are smaller than the space reserved
9298 for them on the frame, we have to clear "under" them. */
9299 if (width > 0 && height > 0)
9300 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9301 left, top, width, height, False);
9303 /* Move/size the scroll bar widget. */
9304 if (mask)
9305 XtConfigureWidget (SCROLL_BAR_X_WIDGET (FRAME_X_DISPLAY (f), bar),
9306 sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9307 top,
9308 sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2,
9309 max (height, 1), 0);
9311 #else /* not USE_TOOLKIT_SCROLL_BARS */
9313 /* Clear areas not covered by the scroll bar because of
9314 VERTICAL_SCROLL_BAR_WIDTH_TRIM. */
9315 if (VERTICAL_SCROLL_BAR_WIDTH_TRIM)
9317 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9318 left, top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9319 height, False);
9320 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9321 left + width - VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9322 top, VERTICAL_SCROLL_BAR_WIDTH_TRIM,
9323 height, False);
9326 /* Clear areas not covered by the scroll bar because it's not as
9327 wide as the area reserved for it . This makes sure a
9328 previous mode line display is cleared after C-x 2 C-x 1, for
9329 example. */
9331 int area_width = FRAME_SCROLL_BAR_COLS (f) * CANON_X_UNIT (f);
9332 int rest = area_width - sb_width;
9333 if (rest > 0 && height > 0)
9335 if (FRAME_HAS_VERTICAL_SCROLL_BARS_ON_LEFT (f))
9336 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9337 left + area_width - rest, top,
9338 rest, height, False);
9339 else
9340 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
9341 left, top, rest, height, False);
9345 /* Move/size the scroll bar window. */
9346 if (mask)
9348 XWindowChanges wc;
9350 wc.x = sb_left + VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9351 wc.y = top;
9352 wc.width = sb_width - VERTICAL_SCROLL_BAR_WIDTH_TRIM * 2;
9353 wc.height = height;
9354 XConfigureWindow (FRAME_X_DISPLAY (f), SCROLL_BAR_X_WINDOW (bar),
9355 mask, &wc);
9358 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9360 /* Remember new settings. */
9361 XSETINT (bar->left, sb_left);
9362 XSETINT (bar->top, top);
9363 XSETINT (bar->width, sb_width);
9364 XSETINT (bar->height, height);
9366 UNBLOCK_INPUT;
9369 #ifdef USE_TOOLKIT_SCROLL_BARS
9370 x_set_toolkit_scroll_bar_thumb (bar, portion, position, whole);
9371 #else /* not USE_TOOLKIT_SCROLL_BARS */
9372 /* Set the scroll bar's current state, unless we're currently being
9373 dragged. */
9374 if (NILP (bar->dragging))
9376 int top_range = VERTICAL_SCROLL_BAR_TOP_RANGE (f, height);
9378 if (whole == 0)
9379 x_scroll_bar_set_handle (bar, 0, top_range, 0);
9380 else
9382 int start = ((double) position * top_range) / whole;
9383 int end = ((double) (position + portion) * top_range) / whole;
9384 x_scroll_bar_set_handle (bar, start, end, 0);
9387 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9389 XSETVECTOR (w->vertical_scroll_bar, bar);
9393 /* The following three hooks are used when we're doing a thorough
9394 redisplay of the frame. We don't explicitly know which scroll bars
9395 are going to be deleted, because keeping track of when windows go
9396 away is a real pain - "Can you say set-window-configuration, boys
9397 and girls?" Instead, we just assert at the beginning of redisplay
9398 that *all* scroll bars are to be removed, and then save a scroll bar
9399 from the fiery pit when we actually redisplay its window. */
9401 /* Arrange for all scroll bars on FRAME to be removed at the next call
9402 to `*judge_scroll_bars_hook'. A scroll bar may be spared if
9403 `*redeem_scroll_bar_hook' is applied to its window before the judgment. */
9405 static void
9406 XTcondemn_scroll_bars (frame)
9407 FRAME_PTR frame;
9409 /* Transfer all the scroll bars to FRAME_CONDEMNED_SCROLL_BARS. */
9410 while (! NILP (FRAME_SCROLL_BARS (frame)))
9412 Lisp_Object bar;
9413 bar = FRAME_SCROLL_BARS (frame);
9414 FRAME_SCROLL_BARS (frame) = XSCROLL_BAR (bar)->next;
9415 XSCROLL_BAR (bar)->next = FRAME_CONDEMNED_SCROLL_BARS (frame);
9416 XSCROLL_BAR (bar)->prev = Qnil;
9417 if (! NILP (FRAME_CONDEMNED_SCROLL_BARS (frame)))
9418 XSCROLL_BAR (FRAME_CONDEMNED_SCROLL_BARS (frame))->prev = bar;
9419 FRAME_CONDEMNED_SCROLL_BARS (frame) = bar;
9424 /* Un-mark WINDOW's scroll bar for deletion in this judgment cycle.
9425 Note that WINDOW isn't necessarily condemned at all. */
9427 static void
9428 XTredeem_scroll_bar (window)
9429 struct window *window;
9431 struct scroll_bar *bar;
9432 struct frame *f;
9434 /* We can't redeem this window's scroll bar if it doesn't have one. */
9435 if (NILP (window->vertical_scroll_bar))
9436 abort ();
9438 bar = XSCROLL_BAR (window->vertical_scroll_bar);
9440 /* Unlink it from the condemned list. */
9441 f = XFRAME (WINDOW_FRAME (window));
9442 if (NILP (bar->prev))
9444 /* If the prev pointer is nil, it must be the first in one of
9445 the lists. */
9446 if (EQ (FRAME_SCROLL_BARS (f), window->vertical_scroll_bar))
9447 /* It's not condemned. Everything's fine. */
9448 return;
9449 else if (EQ (FRAME_CONDEMNED_SCROLL_BARS (f),
9450 window->vertical_scroll_bar))
9451 FRAME_CONDEMNED_SCROLL_BARS (f) = bar->next;
9452 else
9453 /* If its prev pointer is nil, it must be at the front of
9454 one or the other! */
9455 abort ();
9457 else
9458 XSCROLL_BAR (bar->prev)->next = bar->next;
9460 if (! NILP (bar->next))
9461 XSCROLL_BAR (bar->next)->prev = bar->prev;
9463 bar->next = FRAME_SCROLL_BARS (f);
9464 bar->prev = Qnil;
9465 XSETVECTOR (FRAME_SCROLL_BARS (f), bar);
9466 if (! NILP (bar->next))
9467 XSETVECTOR (XSCROLL_BAR (bar->next)->prev, bar);
9470 /* Remove all scroll bars on FRAME that haven't been saved since the
9471 last call to `*condemn_scroll_bars_hook'. */
9473 static void
9474 XTjudge_scroll_bars (f)
9475 FRAME_PTR f;
9477 Lisp_Object bar, next;
9479 bar = FRAME_CONDEMNED_SCROLL_BARS (f);
9481 /* Clear out the condemned list now so we won't try to process any
9482 more events on the hapless scroll bars. */
9483 FRAME_CONDEMNED_SCROLL_BARS (f) = Qnil;
9485 for (; ! NILP (bar); bar = next)
9487 struct scroll_bar *b = XSCROLL_BAR (bar);
9489 x_scroll_bar_remove (b);
9491 next = b->next;
9492 b->next = b->prev = Qnil;
9495 /* Now there should be no references to the condemned scroll bars,
9496 and they should get garbage-collected. */
9500 /* Handle an Expose or GraphicsExpose event on a scroll bar. This
9501 is a no-op when using toolkit scroll bars.
9503 This may be called from a signal handler, so we have to ignore GC
9504 mark bits. */
9506 static void
9507 x_scroll_bar_expose (bar, event)
9508 struct scroll_bar *bar;
9509 XEvent *event;
9511 #ifndef USE_TOOLKIT_SCROLL_BARS
9513 Window w = SCROLL_BAR_X_WINDOW (bar);
9514 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9515 GC gc = f->output_data.x->normal_gc;
9516 int width_trim = VERTICAL_SCROLL_BAR_WIDTH_TRIM;
9518 BLOCK_INPUT;
9520 x_scroll_bar_set_handle (bar, XINT (bar->start), XINT (bar->end), 1);
9522 /* Draw a one-pixel border just inside the edges of the scroll bar. */
9523 XDrawRectangle (FRAME_X_DISPLAY (f), w, gc,
9525 /* x, y, width, height */
9526 0, 0,
9527 XINT (bar->width) - 1 - width_trim - width_trim,
9528 XINT (bar->height) - 1);
9530 UNBLOCK_INPUT;
9532 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9535 /* Handle a mouse click on the scroll bar BAR. If *EMACS_EVENT's kind
9536 is set to something other than no_event, it is enqueued.
9538 This may be called from a signal handler, so we have to ignore GC
9539 mark bits. */
9541 #ifndef USE_TOOLKIT_SCROLL_BARS
9543 static void
9544 x_scroll_bar_handle_click (bar, event, emacs_event)
9545 struct scroll_bar *bar;
9546 XEvent *event;
9547 struct input_event *emacs_event;
9549 if (! GC_WINDOWP (bar->window))
9550 abort ();
9552 emacs_event->kind = scroll_bar_click;
9553 emacs_event->code = event->xbutton.button - Button1;
9554 emacs_event->modifiers
9555 = (x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO
9556 (XFRAME (WINDOW_FRAME (XWINDOW (bar->window)))),
9557 event->xbutton.state)
9558 | (event->type == ButtonRelease
9559 ? up_modifier
9560 : down_modifier));
9561 emacs_event->frame_or_window = bar->window;
9562 emacs_event->arg = Qnil;
9563 emacs_event->timestamp = event->xbutton.time;
9565 #if 0
9566 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9567 int internal_height
9568 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9569 #endif
9570 int top_range
9571 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9572 int y = event->xbutton.y - VERTICAL_SCROLL_BAR_TOP_BORDER;
9574 if (y < 0) y = 0;
9575 if (y > top_range) y = top_range;
9577 if (y < XINT (bar->start))
9578 emacs_event->part = scroll_bar_above_handle;
9579 else if (y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9580 emacs_event->part = scroll_bar_handle;
9581 else
9582 emacs_event->part = scroll_bar_below_handle;
9584 /* Just because the user has clicked on the handle doesn't mean
9585 they want to drag it. Lisp code needs to be able to decide
9586 whether or not we're dragging. */
9587 #if 0
9588 /* If the user has just clicked on the handle, record where they're
9589 holding it. */
9590 if (event->type == ButtonPress
9591 && emacs_event->part == scroll_bar_handle)
9592 XSETINT (bar->dragging, y - XINT (bar->start));
9593 #endif
9595 /* If the user has released the handle, set it to its final position. */
9596 if (event->type == ButtonRelease
9597 && ! NILP (bar->dragging))
9599 int new_start = y - XINT (bar->dragging);
9600 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9602 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9603 bar->dragging = Qnil;
9606 /* Same deal here as the other #if 0. */
9607 #if 0
9608 /* Clicks on the handle are always reported as occurring at the top of
9609 the handle. */
9610 if (emacs_event->part == scroll_bar_handle)
9611 emacs_event->x = bar->start;
9612 else
9613 XSETINT (emacs_event->x, y);
9614 #else
9615 XSETINT (emacs_event->x, y);
9616 #endif
9618 XSETINT (emacs_event->y, top_range);
9622 /* Handle some mouse motion while someone is dragging the scroll bar.
9624 This may be called from a signal handler, so we have to ignore GC
9625 mark bits. */
9627 static void
9628 x_scroll_bar_note_movement (bar, event)
9629 struct scroll_bar *bar;
9630 XEvent *event;
9632 FRAME_PTR f = XFRAME (XWINDOW (bar->window)->frame);
9634 last_mouse_movement_time = event->xmotion.time;
9636 f->mouse_moved = 1;
9637 XSETVECTOR (last_mouse_scroll_bar, bar);
9639 /* If we're dragging the bar, display it. */
9640 if (! GC_NILP (bar->dragging))
9642 /* Where should the handle be now? */
9643 int new_start = event->xmotion.y - XINT (bar->dragging);
9645 if (new_start != XINT (bar->start))
9647 int new_end = new_start + (XINT (bar->end) - XINT (bar->start));
9649 x_scroll_bar_set_handle (bar, new_start, new_end, 0);
9654 #endif /* !USE_TOOLKIT_SCROLL_BARS */
9656 /* Return information to the user about the current position of the mouse
9657 on the scroll bar. */
9659 static void
9660 x_scroll_bar_report_motion (fp, bar_window, part, x, y, time)
9661 FRAME_PTR *fp;
9662 Lisp_Object *bar_window;
9663 enum scroll_bar_part *part;
9664 Lisp_Object *x, *y;
9665 unsigned long *time;
9667 struct scroll_bar *bar = XSCROLL_BAR (last_mouse_scroll_bar);
9668 Window w = SCROLL_BAR_X_WINDOW (bar);
9669 FRAME_PTR f = XFRAME (WINDOW_FRAME (XWINDOW (bar->window)));
9670 int win_x, win_y;
9671 Window dummy_window;
9672 int dummy_coord;
9673 unsigned int dummy_mask;
9675 BLOCK_INPUT;
9677 /* Get the mouse's position relative to the scroll bar window, and
9678 report that. */
9679 if (! XQueryPointer (FRAME_X_DISPLAY (f), w,
9681 /* Root, child, root x and root y. */
9682 &dummy_window, &dummy_window,
9683 &dummy_coord, &dummy_coord,
9685 /* Position relative to scroll bar. */
9686 &win_x, &win_y,
9688 /* Mouse buttons and modifier keys. */
9689 &dummy_mask))
9691 else
9693 #if 0
9694 int inside_height
9695 = VERTICAL_SCROLL_BAR_INSIDE_HEIGHT (f, XINT (bar->height));
9696 #endif
9697 int top_range
9698 = VERTICAL_SCROLL_BAR_TOP_RANGE (f, XINT (bar->height));
9700 win_y -= VERTICAL_SCROLL_BAR_TOP_BORDER;
9702 if (! NILP (bar->dragging))
9703 win_y -= XINT (bar->dragging);
9705 if (win_y < 0)
9706 win_y = 0;
9707 if (win_y > top_range)
9708 win_y = top_range;
9710 *fp = f;
9711 *bar_window = bar->window;
9713 if (! NILP (bar->dragging))
9714 *part = scroll_bar_handle;
9715 else if (win_y < XINT (bar->start))
9716 *part = scroll_bar_above_handle;
9717 else if (win_y < XINT (bar->end) + VERTICAL_SCROLL_BAR_MIN_HANDLE)
9718 *part = scroll_bar_handle;
9719 else
9720 *part = scroll_bar_below_handle;
9722 XSETINT (*x, win_y);
9723 XSETINT (*y, top_range);
9725 f->mouse_moved = 0;
9726 last_mouse_scroll_bar = Qnil;
9729 *time = last_mouse_movement_time;
9731 UNBLOCK_INPUT;
9735 /* The screen has been cleared so we may have changed foreground or
9736 background colors, and the scroll bars may need to be redrawn.
9737 Clear out the scroll bars, and ask for expose events, so we can
9738 redraw them. */
9740 void
9741 x_scroll_bar_clear (f)
9742 FRAME_PTR f;
9744 #ifndef USE_TOOLKIT_SCROLL_BARS
9745 Lisp_Object bar;
9747 /* We can have scroll bars even if this is 0,
9748 if we just turned off scroll bar mode.
9749 But in that case we should not clear them. */
9750 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
9751 for (bar = FRAME_SCROLL_BARS (f); VECTORP (bar);
9752 bar = XSCROLL_BAR (bar)->next)
9753 XClearArea (FRAME_X_DISPLAY (f),
9754 SCROLL_BAR_X_WINDOW (XSCROLL_BAR (bar)),
9755 0, 0, 0, 0, True);
9756 #endif /* not USE_TOOLKIT_SCROLL_BARS */
9759 /* This processes Expose events from the menu-bar specific X event
9760 loop in xmenu.c. This allows to redisplay the frame if necessary
9761 when handling menu-bar or pop-up items. */
9764 process_expose_from_menu (event)
9765 XEvent event;
9767 FRAME_PTR f;
9768 struct x_display_info *dpyinfo;
9769 int frame_exposed_p = 0;
9771 BLOCK_INPUT;
9773 dpyinfo = x_display_info_for_display (event.xexpose.display);
9774 f = x_window_to_frame (dpyinfo, event.xexpose.window);
9775 if (f)
9777 if (f->async_visible == 0)
9779 f->async_visible = 1;
9780 f->async_iconified = 0;
9781 f->output_data.x->has_been_visible = 1;
9782 SET_FRAME_GARBAGED (f);
9784 else
9786 expose_frame (x_window_to_frame (dpyinfo, event.xexpose.window),
9787 event.xexpose.x, event.xexpose.y,
9788 event.xexpose.width, event.xexpose.height);
9789 frame_exposed_p = 1;
9792 else
9794 struct scroll_bar *bar
9795 = x_window_to_scroll_bar (event.xexpose.window);
9797 if (bar)
9798 x_scroll_bar_expose (bar, &event);
9801 UNBLOCK_INPUT;
9802 return frame_exposed_p;
9805 /* Define a queue to save up SelectionRequest events for later handling. */
9807 struct selection_event_queue
9809 XEvent event;
9810 struct selection_event_queue *next;
9813 static struct selection_event_queue *queue;
9815 /* Nonzero means queue up certain events--don't process them yet. */
9817 static int x_queue_selection_requests;
9819 /* Queue up an X event *EVENT, to be processed later. */
9821 static void
9822 x_queue_event (f, event)
9823 FRAME_PTR f;
9824 XEvent *event;
9826 struct selection_event_queue *queue_tmp
9827 = (struct selection_event_queue *) xmalloc (sizeof (struct selection_event_queue));
9829 if (queue_tmp != NULL)
9831 queue_tmp->event = *event;
9832 queue_tmp->next = queue;
9833 queue = queue_tmp;
9837 /* Take all the queued events and put them back
9838 so that they get processed afresh. */
9840 static void
9841 x_unqueue_events (display)
9842 Display *display;
9844 while (queue != NULL)
9846 struct selection_event_queue *queue_tmp = queue;
9847 XPutBackEvent (display, &queue_tmp->event);
9848 queue = queue_tmp->next;
9849 xfree ((char *)queue_tmp);
9853 /* Start queuing SelectionRequest events. */
9855 void
9856 x_start_queuing_selection_requests (display)
9857 Display *display;
9859 x_queue_selection_requests++;
9862 /* Stop queuing SelectionRequest events. */
9864 void
9865 x_stop_queuing_selection_requests (display)
9866 Display *display;
9868 x_queue_selection_requests--;
9869 x_unqueue_events (display);
9872 /* The main X event-reading loop - XTread_socket. */
9874 #if 0
9875 /* Time stamp of enter window event. This is only used by XTread_socket,
9876 but we have to put it out here, since static variables within functions
9877 sometimes don't work. */
9879 static Time enter_timestamp;
9880 #endif
9882 /* This holds the state XLookupString needs to implement dead keys
9883 and other tricks known as "compose processing". _X Window System_
9884 says that a portable program can't use this, but Stephen Gildea assures
9885 me that letting the compiler initialize it to zeros will work okay.
9887 This must be defined outside of XTread_socket, for the same reasons
9888 given for enter_timestamp, above. */
9890 static XComposeStatus compose_status;
9892 /* Record the last 100 characters stored
9893 to help debug the loss-of-chars-during-GC problem. */
9895 static int temp_index;
9896 static short temp_buffer[100];
9898 /* Set this to nonzero to fake an "X I/O error"
9899 on a particular display. */
9901 struct x_display_info *XTread_socket_fake_io_error;
9903 /* When we find no input here, we occasionally do a no-op command
9904 to verify that the X server is still running and we can still talk with it.
9905 We try all the open displays, one by one.
9906 This variable is used for cycling thru the displays. */
9908 static struct x_display_info *next_noop_dpyinfo;
9910 #define SET_SAVED_MENU_EVENT(size) \
9911 do \
9913 if (f->output_data.x->saved_menu_event == 0) \
9914 f->output_data.x->saved_menu_event \
9915 = (XEvent *) xmalloc (sizeof (XEvent)); \
9916 bcopy (&event, f->output_data.x->saved_menu_event, size); \
9917 if (numchars >= 1) \
9919 bufp->kind = menu_bar_activate_event; \
9920 XSETFRAME (bufp->frame_or_window, f); \
9921 bufp->arg = Qnil; \
9922 bufp++; \
9923 count++; \
9924 numchars--; \
9927 while (0)
9929 #define SET_SAVED_BUTTON_EVENT SET_SAVED_MENU_EVENT (sizeof (XButtonEvent))
9930 #define SET_SAVED_KEY_EVENT SET_SAVED_MENU_EVENT (sizeof (XKeyEvent))
9932 /* Read events coming from the X server.
9933 This routine is called by the SIGIO handler.
9934 We return as soon as there are no more events to be read.
9936 Events representing keys are stored in buffer BUFP,
9937 which can hold up to NUMCHARS characters.
9938 We return the number of characters stored into the buffer,
9939 thus pretending to be `read'.
9941 EXPECTED is nonzero if the caller knows input is available. */
9944 XTread_socket (sd, bufp, numchars, expected)
9945 register int sd;
9946 /* register */ struct input_event *bufp;
9947 /* register */ int numchars;
9948 int expected;
9950 int count = 0;
9951 int nbytes = 0;
9952 XEvent event;
9953 struct frame *f;
9954 int event_found = 0;
9955 struct x_display_info *dpyinfo;
9956 struct coding_system coding;
9958 if (interrupt_input_blocked)
9960 interrupt_input_pending = 1;
9961 return -1;
9964 interrupt_input_pending = 0;
9965 BLOCK_INPUT;
9967 /* So people can tell when we have read the available input. */
9968 input_signal_count++;
9970 if (numchars <= 0)
9971 abort (); /* Don't think this happens. */
9973 ++handling_signal;
9975 /* The input should be decoded if it is from XIM. Currently the
9976 locale of XIM is the same as that of the system. So, we can use
9977 Vlocale_coding_system which is initialized properly at Emacs
9978 startup time. */
9979 setup_coding_system (Vlocale_coding_system, &coding);
9980 coding.src_multibyte = 0;
9981 coding.dst_multibyte = 1;
9982 /* The input is converted to events, thus we can't handle
9983 composition. Anyway, there's no XIM that gives us composition
9984 information. */
9985 coding.common_flags &= ~CODING_ANNOTATION_MASK;
9987 /* Find the display we are supposed to read input for.
9988 It's the one communicating on descriptor SD. */
9989 for (dpyinfo = x_display_list; dpyinfo; dpyinfo = dpyinfo->next)
9991 #if 0 /* This ought to be unnecessary; let's verify it. */
9992 #ifdef FIOSNBIO
9993 /* If available, Xlib uses FIOSNBIO to make the socket
9994 non-blocking, and then looks for EWOULDBLOCK. If O_NDELAY is set,
9995 FIOSNBIO is ignored, and instead of signaling EWOULDBLOCK,
9996 a read returns 0, which Xlib interprets as equivalent to EPIPE. */
9997 fcntl (dpyinfo->connection, F_SETFL, 0);
9998 #endif /* ! defined (FIOSNBIO) */
9999 #endif
10001 #if 0 /* This code can't be made to work, with multiple displays,
10002 and appears not to be used on any system any more.
10003 Also keyboard.c doesn't turn O_NDELAY on and off
10004 for X connections. */
10005 #ifndef SIGIO
10006 #ifndef HAVE_SELECT
10007 if (! (fcntl (dpyinfo->connection, F_GETFL, 0) & O_NDELAY))
10009 extern int read_alarm_should_throw;
10010 read_alarm_should_throw = 1;
10011 XPeekEvent (dpyinfo->display, &event);
10012 read_alarm_should_throw = 0;
10014 #endif /* HAVE_SELECT */
10015 #endif /* SIGIO */
10016 #endif
10018 /* For debugging, this gives a way to fake an I/O error. */
10019 if (dpyinfo == XTread_socket_fake_io_error)
10021 XTread_socket_fake_io_error = 0;
10022 x_io_error_quitter (dpyinfo->display);
10025 while (XPending (dpyinfo->display))
10027 XNextEvent (dpyinfo->display, &event);
10029 #ifdef HAVE_X_I18N
10031 /* Filter events for the current X input method.
10032 XFilterEvent returns non-zero if the input method has
10033 consumed the event. We pass the frame's X window to
10034 XFilterEvent because that's the one for which the IC
10035 was created. */
10036 struct frame *f1 = x_any_window_to_frame (dpyinfo,
10037 event.xclient.window);
10038 if (XFilterEvent (&event, f1 ? FRAME_X_WINDOW (f1) : None))
10039 break;
10041 #endif
10042 event_found = 1;
10044 switch (event.type)
10046 case ClientMessage:
10048 if (event.xclient.message_type
10049 == dpyinfo->Xatom_wm_protocols
10050 && event.xclient.format == 32)
10052 if (event.xclient.data.l[0]
10053 == dpyinfo->Xatom_wm_take_focus)
10055 /* Use x_any_window_to_frame because this
10056 could be the shell widget window
10057 if the frame has no title bar. */
10058 f = x_any_window_to_frame (dpyinfo, event.xclient.window);
10059 #ifdef HAVE_X_I18N
10060 /* Not quite sure this is needed -pd */
10061 if (f && FRAME_XIC (f))
10062 XSetICFocus (FRAME_XIC (f));
10063 #endif
10064 #if 0 /* Emacs sets WM hints whose `input' field is `true'. This
10065 instructs the WM to set the input focus automatically for
10066 Emacs with a call to XSetInputFocus. Setting WM_TAKE_FOCUS
10067 tells the WM to send us a ClientMessage WM_TAKE_FOCUS after
10068 it has set the focus. So, XSetInputFocus below is not
10069 needed.
10071 The call to XSetInputFocus below has also caused trouble. In
10072 cases where the XSetInputFocus done by the WM and the one
10073 below are temporally close (on a fast machine), the call
10074 below can generate additional FocusIn events which confuse
10075 Emacs. */
10077 /* Since we set WM_TAKE_FOCUS, we must call
10078 XSetInputFocus explicitly. But not if f is null,
10079 since that might be an event for a deleted frame. */
10080 if (f)
10082 Display *d = event.xclient.display;
10083 /* Catch and ignore errors, in case window has been
10084 iconified by a window manager such as GWM. */
10085 int count = x_catch_errors (d);
10086 XSetInputFocus (d, event.xclient.window,
10087 /* The ICCCM says this is
10088 the only valid choice. */
10089 RevertToParent,
10090 event.xclient.data.l[1]);
10091 /* This is needed to detect the error
10092 if there is an error. */
10093 XSync (d, False);
10094 x_uncatch_errors (d, count);
10096 /* Not certain about handling scroll bars here */
10097 #endif /* 0 */
10099 else if (event.xclient.data.l[0]
10100 == dpyinfo->Xatom_wm_save_yourself)
10102 /* Save state modify the WM_COMMAND property to
10103 something which can reinstate us. This notifies
10104 the session manager, who's looking for such a
10105 PropertyNotify. Can restart processing when
10106 a keyboard or mouse event arrives. */
10107 if (numchars > 0)
10109 f = x_top_window_to_frame (dpyinfo,
10110 event.xclient.window);
10112 /* This is just so we only give real data once
10113 for a single Emacs process. */
10114 if (f == SELECTED_FRAME ())
10115 XSetCommand (FRAME_X_DISPLAY (f),
10116 event.xclient.window,
10117 initial_argv, initial_argc);
10118 else if (f)
10119 XSetCommand (FRAME_X_DISPLAY (f),
10120 event.xclient.window,
10121 0, 0);
10124 else if (event.xclient.data.l[0]
10125 == dpyinfo->Xatom_wm_delete_window)
10127 struct frame *f
10128 = x_any_window_to_frame (dpyinfo,
10129 event.xclient.window);
10131 if (f)
10133 if (numchars == 0)
10134 abort ();
10136 bufp->kind = delete_window_event;
10137 XSETFRAME (bufp->frame_or_window, f);
10138 bufp->arg = Qnil;
10139 bufp++;
10141 count += 1;
10142 numchars -= 1;
10146 else if (event.xclient.message_type
10147 == dpyinfo->Xatom_wm_configure_denied)
10150 else if (event.xclient.message_type
10151 == dpyinfo->Xatom_wm_window_moved)
10153 int new_x, new_y;
10154 struct frame *f
10155 = x_window_to_frame (dpyinfo, event.xclient.window);
10157 new_x = event.xclient.data.s[0];
10158 new_y = event.xclient.data.s[1];
10160 if (f)
10162 f->output_data.x->left_pos = new_x;
10163 f->output_data.x->top_pos = new_y;
10166 #ifdef HACK_EDITRES
10167 else if (event.xclient.message_type
10168 == dpyinfo->Xatom_editres)
10170 struct frame *f
10171 = x_any_window_to_frame (dpyinfo, event.xclient.window);
10172 _XEditResCheckMessages (f->output_data.x->widget, NULL,
10173 &event, NULL);
10175 #endif /* HACK_EDITRES */
10176 else if ((event.xclient.message_type
10177 == dpyinfo->Xatom_DONE)
10178 || (event.xclient.message_type
10179 == dpyinfo->Xatom_PAGE))
10181 /* Ghostview job completed. Kill it. We could
10182 reply with "Next" if we received "Page", but we
10183 currently never do because we are interested in
10184 images, only, which should have 1 page. */
10185 Pixmap pixmap = (Pixmap) event.xclient.data.l[1];
10186 struct frame *f
10187 = x_window_to_frame (dpyinfo, event.xclient.window);
10188 x_kill_gs_process (pixmap, f);
10189 expose_frame (f, 0, 0, 0, 0);
10191 #ifdef USE_TOOLKIT_SCROLL_BARS
10192 /* Scroll bar callbacks send a ClientMessage from which
10193 we construct an input_event. */
10194 else if (event.xclient.message_type
10195 == dpyinfo->Xatom_Scrollbar)
10197 x_scroll_bar_to_input_event (&event, bufp);
10198 ++bufp, ++count, --numchars;
10199 goto out;
10201 #endif /* USE_TOOLKIT_SCROLL_BARS */
10202 else
10203 goto OTHER;
10205 break;
10207 case SelectionNotify:
10208 #ifdef USE_X_TOOLKIT
10209 if (! x_window_to_frame (dpyinfo, event.xselection.requestor))
10210 goto OTHER;
10211 #endif /* not USE_X_TOOLKIT */
10212 x_handle_selection_notify (&event.xselection);
10213 break;
10215 case SelectionClear: /* Someone has grabbed ownership. */
10216 #ifdef USE_X_TOOLKIT
10217 if (! x_window_to_frame (dpyinfo, event.xselectionclear.window))
10218 goto OTHER;
10219 #endif /* USE_X_TOOLKIT */
10221 XSelectionClearEvent *eventp = (XSelectionClearEvent *) &event;
10223 if (numchars == 0)
10224 abort ();
10226 bufp->kind = selection_clear_event;
10227 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10228 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10229 SELECTION_EVENT_TIME (bufp) = eventp->time;
10230 bufp->frame_or_window = Qnil;
10231 bufp->arg = Qnil;
10232 bufp++;
10234 count += 1;
10235 numchars -= 1;
10237 break;
10239 case SelectionRequest: /* Someone wants our selection. */
10240 #ifdef USE_X_TOOLKIT
10241 if (!x_window_to_frame (dpyinfo, event.xselectionrequest.owner))
10242 goto OTHER;
10243 #endif /* USE_X_TOOLKIT */
10244 if (x_queue_selection_requests)
10245 x_queue_event (x_window_to_frame (dpyinfo, event.xselectionrequest.owner),
10246 &event);
10247 else
10249 XSelectionRequestEvent *eventp
10250 = (XSelectionRequestEvent *) &event;
10252 if (numchars == 0)
10253 abort ();
10255 bufp->kind = selection_request_event;
10256 SELECTION_EVENT_DISPLAY (bufp) = eventp->display;
10257 SELECTION_EVENT_REQUESTOR (bufp) = eventp->requestor;
10258 SELECTION_EVENT_SELECTION (bufp) = eventp->selection;
10259 SELECTION_EVENT_TARGET (bufp) = eventp->target;
10260 SELECTION_EVENT_PROPERTY (bufp) = eventp->property;
10261 SELECTION_EVENT_TIME (bufp) = eventp->time;
10262 bufp->frame_or_window = Qnil;
10263 bufp->arg = Qnil;
10264 bufp++;
10266 count += 1;
10267 numchars -= 1;
10269 break;
10271 case PropertyNotify:
10272 #if 0 /* This is plain wrong. In the case that we are waiting for a
10273 PropertyNotify used as an ACK in incremental selection
10274 transfer, the property will be on the receiver's window. */
10275 #if defined USE_X_TOOLKIT
10276 if (!x_any_window_to_frame (dpyinfo, event.xproperty.window))
10277 goto OTHER;
10278 #endif
10279 #endif
10280 x_handle_property_notify (&event.xproperty);
10281 goto OTHER;
10283 case ReparentNotify:
10284 f = x_top_window_to_frame (dpyinfo, event.xreparent.window);
10285 if (f)
10287 int x, y;
10288 f->output_data.x->parent_desc = event.xreparent.parent;
10289 x_real_positions (f, &x, &y);
10290 f->output_data.x->left_pos = x;
10291 f->output_data.x->top_pos = y;
10293 break;
10295 case Expose:
10296 f = x_window_to_frame (dpyinfo, event.xexpose.window);
10297 if (f)
10299 x_check_fullscreen (f);
10301 if (f->async_visible == 0)
10303 f->async_visible = 1;
10304 f->async_iconified = 0;
10305 f->output_data.x->has_been_visible = 1;
10306 SET_FRAME_GARBAGED (f);
10308 else
10309 expose_frame (x_window_to_frame (dpyinfo,
10310 event.xexpose.window),
10311 event.xexpose.x, event.xexpose.y,
10312 event.xexpose.width, event.xexpose.height);
10314 else
10316 #ifndef USE_TOOLKIT_SCROLL_BARS
10317 struct scroll_bar *bar;
10318 #endif
10319 #if defined USE_LUCID
10320 /* Submenus of the Lucid menu bar aren't widgets
10321 themselves, so there's no way to dispatch events
10322 to them. Recognize this case separately. */
10324 Widget widget
10325 = x_window_to_menu_bar (event.xexpose.window);
10326 if (widget)
10327 xlwmenu_redisplay (widget);
10329 #endif /* USE_LUCID */
10331 #ifdef USE_TOOLKIT_SCROLL_BARS
10332 /* Dispatch event to the widget. */
10333 goto OTHER;
10334 #else /* not USE_TOOLKIT_SCROLL_BARS */
10335 bar = x_window_to_scroll_bar (event.xexpose.window);
10337 if (bar)
10338 x_scroll_bar_expose (bar, &event);
10339 #ifdef USE_X_TOOLKIT
10340 else
10341 goto OTHER;
10342 #endif /* USE_X_TOOLKIT */
10343 #endif /* not USE_TOOLKIT_SCROLL_BARS */
10345 break;
10347 case GraphicsExpose: /* This occurs when an XCopyArea's
10348 source area was obscured or not
10349 available. */
10350 f = x_window_to_frame (dpyinfo, event.xgraphicsexpose.drawable);
10351 if (f)
10353 expose_frame (f,
10354 event.xgraphicsexpose.x, event.xgraphicsexpose.y,
10355 event.xgraphicsexpose.width,
10356 event.xgraphicsexpose.height);
10358 #ifdef USE_X_TOOLKIT
10359 else
10360 goto OTHER;
10361 #endif /* USE_X_TOOLKIT */
10362 break;
10364 case NoExpose: /* This occurs when an XCopyArea's
10365 source area was completely
10366 available. */
10367 break;
10369 case UnmapNotify:
10370 /* Redo the mouse-highlight after the tooltip has gone. */
10371 if (event.xmap.window == tip_window)
10373 tip_window = 0;
10374 redo_mouse_highlight ();
10377 f = x_top_window_to_frame (dpyinfo, event.xunmap.window);
10378 if (f) /* F may no longer exist if
10379 the frame was deleted. */
10381 /* While a frame is unmapped, display generation is
10382 disabled; you don't want to spend time updating a
10383 display that won't ever be seen. */
10384 f->async_visible = 0;
10385 /* We can't distinguish, from the event, whether the window
10386 has become iconified or invisible. So assume, if it
10387 was previously visible, than now it is iconified.
10388 But x_make_frame_invisible clears both
10389 the visible flag and the iconified flag;
10390 and that way, we know the window is not iconified now. */
10391 if (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f))
10393 f->async_iconified = 1;
10395 bufp->kind = iconify_event;
10396 XSETFRAME (bufp->frame_or_window, f);
10397 bufp->arg = Qnil;
10398 bufp++;
10399 count++;
10400 numchars--;
10403 goto OTHER;
10405 case MapNotify:
10406 if (event.xmap.window == tip_window)
10407 /* The tooltip has been drawn already. Avoid
10408 the SET_FRAME_GARBAGED below. */
10409 goto OTHER;
10411 /* We use x_top_window_to_frame because map events can
10412 come for sub-windows and they don't mean that the
10413 frame is visible. */
10414 f = x_top_window_to_frame (dpyinfo, event.xmap.window);
10415 if (f)
10417 f->async_visible = 1;
10418 f->async_iconified = 0;
10419 f->output_data.x->has_been_visible = 1;
10421 /* wait_reading_process_input will notice this and update
10422 the frame's display structures. */
10423 SET_FRAME_GARBAGED (f);
10425 if (f->iconified)
10427 bufp->kind = deiconify_event;
10428 XSETFRAME (bufp->frame_or_window, f);
10429 bufp->arg = Qnil;
10430 bufp++;
10431 count++;
10432 numchars--;
10434 else if (! NILP (Vframe_list)
10435 && ! NILP (XCDR (Vframe_list)))
10436 /* Force a redisplay sooner or later
10437 to update the frame titles
10438 in case this is the second frame. */
10439 record_asynch_buffer_change ();
10441 goto OTHER;
10443 case KeyPress:
10444 f = x_any_window_to_frame (dpyinfo, event.xkey.window);
10446 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
10448 dpyinfo->mouse_face_hidden = 1;
10449 clear_mouse_face (dpyinfo);
10452 #if defined USE_MOTIF && defined USE_TOOLKIT_SCROLL_BARS
10453 if (f == 0)
10455 /* Scroll bars consume key events, but we want
10456 the keys to go to the scroll bar's frame. */
10457 Widget widget = XtWindowToWidget (dpyinfo->display,
10458 event.xkey.window);
10459 if (widget && XmIsScrollBar (widget))
10461 widget = XtParent (widget);
10462 f = x_any_window_to_frame (dpyinfo, XtWindow (widget));
10465 #endif /* USE_MOTIF and USE_TOOLKIT_SCROLL_BARS */
10467 if (f != 0)
10469 KeySym keysym, orig_keysym;
10470 /* al%imercury@uunet.uu.net says that making this 81
10471 instead of 80 fixed a bug whereby meta chars made
10472 his Emacs hang.
10474 It seems that some version of XmbLookupString has
10475 a bug of not returning XBufferOverflow in
10476 status_return even if the input is too long to
10477 fit in 81 bytes. So, we must prepare sufficient
10478 bytes for copy_buffer. 513 bytes (256 chars for
10479 two-byte character set) seems to be a fairly good
10480 approximation. -- 2000.8.10 handa@etl.go.jp */
10481 unsigned char copy_buffer[513];
10482 unsigned char *copy_bufptr = copy_buffer;
10483 int copy_bufsiz = sizeof (copy_buffer);
10484 int modifiers;
10486 event.xkey.state
10487 |= x_emacs_to_x_modifiers (FRAME_X_DISPLAY_INFO (f),
10488 extra_keyboard_modifiers);
10489 modifiers = event.xkey.state;
10491 /* This will have to go some day... */
10493 /* make_lispy_event turns chars into control chars.
10494 Don't do it here because XLookupString is too eager. */
10495 event.xkey.state &= ~ControlMask;
10496 event.xkey.state &= ~(dpyinfo->meta_mod_mask
10497 | dpyinfo->super_mod_mask
10498 | dpyinfo->hyper_mod_mask
10499 | dpyinfo->alt_mod_mask);
10501 /* In case Meta is ComposeCharacter,
10502 clear its status. According to Markus Ehrnsperger
10503 Markus.Ehrnsperger@lehrstuhl-bross.physik.uni-muenchen.de
10504 this enables ComposeCharacter to work whether or
10505 not it is combined with Meta. */
10506 if (modifiers & dpyinfo->meta_mod_mask)
10507 bzero (&compose_status, sizeof (compose_status));
10509 #ifdef HAVE_X_I18N
10510 if (FRAME_XIC (f))
10512 Status status_return;
10514 nbytes = XmbLookupString (FRAME_XIC (f),
10515 &event.xkey, copy_bufptr,
10516 copy_bufsiz, &keysym,
10517 &status_return);
10518 if (status_return == XBufferOverflow)
10520 copy_bufsiz = nbytes + 1;
10521 copy_bufptr = (char *) alloca (copy_bufsiz);
10522 nbytes = XmbLookupString (FRAME_XIC (f),
10523 &event.xkey, copy_bufptr,
10524 copy_bufsiz, &keysym,
10525 &status_return);
10528 if (status_return == XLookupNone)
10529 break;
10530 else if (status_return == XLookupChars)
10532 keysym = NoSymbol;
10533 modifiers = 0;
10535 else if (status_return != XLookupKeySym
10536 && status_return != XLookupBoth)
10537 abort ();
10539 else
10540 nbytes = XLookupString (&event.xkey, copy_bufptr,
10541 copy_bufsiz, &keysym,
10542 &compose_status);
10543 #else
10544 nbytes = XLookupString (&event.xkey, copy_bufptr,
10545 copy_bufsiz, &keysym,
10546 &compose_status);
10547 #endif
10549 orig_keysym = keysym;
10551 if (numchars > 1)
10553 if (((keysym >= XK_BackSpace && keysym <= XK_Escape)
10554 || keysym == XK_Delete
10555 #ifdef XK_ISO_Left_Tab
10556 || (keysym >= XK_ISO_Left_Tab && keysym <= XK_ISO_Enter)
10557 #endif
10558 || (keysym >= XK_Kanji && keysym <= XK_Eisu_toggle)
10559 || IsCursorKey (keysym) /* 0xff50 <= x < 0xff60 */
10560 || IsMiscFunctionKey (keysym) /* 0xff60 <= x < VARIES */
10561 #ifdef HPUX
10562 /* This recognizes the "extended function keys".
10563 It seems there's no cleaner way.
10564 Test IsModifierKey to avoid handling mode_switch
10565 incorrectly. */
10566 || ((unsigned) (keysym) >= XK_Select
10567 && (unsigned)(keysym) < XK_KP_Space)
10568 #endif
10569 #ifdef XK_dead_circumflex
10570 || orig_keysym == XK_dead_circumflex
10571 #endif
10572 #ifdef XK_dead_grave
10573 || orig_keysym == XK_dead_grave
10574 #endif
10575 #ifdef XK_dead_tilde
10576 || orig_keysym == XK_dead_tilde
10577 #endif
10578 #ifdef XK_dead_diaeresis
10579 || orig_keysym == XK_dead_diaeresis
10580 #endif
10581 #ifdef XK_dead_macron
10582 || orig_keysym == XK_dead_macron
10583 #endif
10584 #ifdef XK_dead_degree
10585 || orig_keysym == XK_dead_degree
10586 #endif
10587 #ifdef XK_dead_acute
10588 || orig_keysym == XK_dead_acute
10589 #endif
10590 #ifdef XK_dead_cedilla
10591 || orig_keysym == XK_dead_cedilla
10592 #endif
10593 #ifdef XK_dead_breve
10594 || orig_keysym == XK_dead_breve
10595 #endif
10596 #ifdef XK_dead_ogonek
10597 || orig_keysym == XK_dead_ogonek
10598 #endif
10599 #ifdef XK_dead_caron
10600 || orig_keysym == XK_dead_caron
10601 #endif
10602 #ifdef XK_dead_doubleacute
10603 || orig_keysym == XK_dead_doubleacute
10604 #endif
10605 #ifdef XK_dead_abovedot
10606 || orig_keysym == XK_dead_abovedot
10607 #endif
10608 #ifdef XK_dead_abovering
10609 || orig_keysym == XK_dead_abovering
10610 #endif
10611 #ifdef XK_dead_iota
10612 || orig_keysym == XK_dead_iota
10613 #endif
10614 #ifdef XK_dead_belowdot
10615 || orig_keysym == XK_dead_belowdot
10616 #endif
10617 #ifdef XK_dead_voiced_sound
10618 || orig_keysym == XK_dead_voiced_sound
10619 #endif
10620 #ifdef XK_dead_semivoiced_sound
10621 || orig_keysym == XK_dead_semivoiced_sound
10622 #endif
10623 #ifdef XK_dead_hook
10624 || orig_keysym == XK_dead_hook
10625 #endif
10626 #ifdef XK_dead_horn
10627 || orig_keysym == XK_dead_horn
10628 #endif
10629 || IsKeypadKey (keysym) /* 0xff80 <= x < 0xffbe */
10630 || IsFunctionKey (keysym) /* 0xffbe <= x < 0xffe1 */
10631 /* Any "vendor-specific" key is ok. */
10632 || (orig_keysym & (1 << 28))
10633 || (keysym != NoSymbol && nbytes == 0))
10634 && ! (IsModifierKey (orig_keysym)
10635 #ifndef HAVE_X11R5
10636 #ifdef XK_Mode_switch
10637 || ((unsigned)(orig_keysym) == XK_Mode_switch)
10638 #endif
10639 #ifdef XK_Num_Lock
10640 || ((unsigned)(orig_keysym) == XK_Num_Lock)
10641 #endif
10642 #endif /* not HAVE_X11R5 */
10645 Lisp_Object c;
10647 if (temp_index == sizeof temp_buffer / sizeof (short))
10648 temp_index = 0;
10649 temp_buffer[temp_index++] = keysym;
10650 if (! EQ ((c = Fgethash (make_number (keysym),
10651 Vx_keysym_table, Qnil)),
10652 Qnil))
10654 bufp->kind = (ASCII_CHAR_P (c)
10655 ? ascii_keystroke
10656 : multibyte_char_keystroke);
10657 bufp->code = c;
10659 else
10661 bufp->kind = non_ascii_keystroke;
10662 bufp->code = keysym;
10664 XSETFRAME (bufp->frame_or_window, f);
10665 bufp->arg = Qnil;
10666 bufp->modifiers
10667 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10668 modifiers);
10669 bufp->timestamp = event.xkey.time;
10670 bufp++;
10671 count++;
10672 numchars--;
10674 else if (numchars > nbytes)
10676 register int i;
10677 register int c;
10678 int nchars, len;
10680 for (i = 0; i < nbytes; i++)
10682 if (temp_index == (sizeof temp_buffer
10683 / sizeof (short)))
10684 temp_index = 0;
10685 temp_buffer[temp_index++] = copy_bufptr[i];
10689 /* Decode the input data. */
10690 coding.destination
10691 = (unsigned char *) malloc (nbytes);
10692 if (! coding.destination)
10693 break;
10694 coding.dst_bytes = nbytes;
10695 coding.mode |= CODING_MODE_LAST_BLOCK;
10696 decode_coding_c_string (&coding, copy_bufptr,
10697 nbytes, Qnil);
10698 nbytes = coding.produced;
10699 nchars = coding.produced_char;
10700 if (copy_bufsiz < nbytes)
10702 copy_bufsiz = nbytes;
10703 copy_bufptr = (char *) alloca (nbytes);
10705 bcopy (coding.destination, copy_bufptr, nbytes);
10706 free (coding.destination);
10709 /* Convert the input data to a sequence of
10710 character events. */
10711 for (i = 0; i < nbytes; i += len)
10713 if (nchars == nbytes)
10714 c = copy_bufptr[i], len = 1;
10715 else
10716 c = STRING_CHAR_AND_LENGTH (copy_bufptr + i,
10717 nbytes - i, len);
10719 bufp->kind = (ASCII_CHAR_P (c)
10720 ? ascii_keystroke
10721 : multibyte_char_keystroke);
10722 bufp->code = c;
10723 XSETFRAME (bufp->frame_or_window, f);
10724 bufp->arg = Qnil;
10725 bufp->modifiers
10726 = x_x_to_emacs_modifiers (FRAME_X_DISPLAY_INFO (f),
10727 modifiers);
10728 bufp->timestamp = event.xkey.time;
10729 bufp++;
10732 count += nchars;
10733 numchars -= nchars;
10735 if (keysym == NoSymbol)
10736 break;
10738 else
10739 abort ();
10741 else
10742 abort ();
10744 #ifdef HAVE_X_I18N
10745 /* Don't dispatch this event since XtDispatchEvent calls
10746 XFilterEvent, and two calls in a row may freeze the
10747 client. */
10748 break;
10749 #else
10750 goto OTHER;
10751 #endif
10753 case KeyRelease:
10754 #ifdef HAVE_X_I18N
10755 /* Don't dispatch this event since XtDispatchEvent calls
10756 XFilterEvent, and two calls in a row may freeze the
10757 client. */
10758 break;
10759 #else
10760 goto OTHER;
10761 #endif
10763 /* Here's a possible interpretation of the whole
10764 FocusIn-EnterNotify FocusOut-LeaveNotify mess. If
10765 you get a FocusIn event, you have to get a FocusOut
10766 event before you relinquish the focus. If you
10767 haven't received a FocusIn event, then a mere
10768 LeaveNotify is enough to free you. */
10770 case EnterNotify:
10772 f = x_any_window_to_frame (dpyinfo, event.xcrossing.window);
10774 #if 0
10775 if (event.xcrossing.focus)
10777 /* Avoid nasty pop/raise loops. */
10778 if (f && (!(f->auto_raise)
10779 || !(f->auto_lower)
10780 || (event.xcrossing.time - enter_timestamp) > 500))
10782 x_new_focus_frame (dpyinfo, f);
10783 enter_timestamp = event.xcrossing.time;
10786 else if (f == dpyinfo->x_focus_frame)
10787 x_new_focus_frame (dpyinfo, 0);
10788 #endif
10790 /* EnterNotify counts as mouse movement,
10791 so update things that depend on mouse position. */
10792 if (f && !f->output_data.x->hourglass_p)
10793 note_mouse_movement (f, &event.xmotion);
10794 goto OTHER;
10797 case FocusIn:
10798 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10799 if (event.xfocus.detail != NotifyPointer)
10800 dpyinfo->x_focus_event_frame = f;
10801 if (f)
10803 x_new_focus_frame (dpyinfo, f);
10805 /* Don't stop displaying the initial startup message
10806 for a switch-frame event we don't need. */
10807 if (GC_NILP (Vterminal_frame)
10808 && GC_CONSP (Vframe_list)
10809 && !GC_NILP (XCDR (Vframe_list)))
10811 bufp->kind = FOCUS_IN_EVENT;
10812 XSETFRAME (bufp->frame_or_window, f);
10813 bufp->arg = Qnil;
10814 ++bufp, ++count, --numchars;
10818 #ifdef HAVE_X_I18N
10819 if (f && FRAME_XIC (f))
10820 XSetICFocus (FRAME_XIC (f));
10821 #endif
10823 goto OTHER;
10825 case LeaveNotify:
10826 f = x_top_window_to_frame (dpyinfo, event.xcrossing.window);
10827 if (f)
10829 if (f == dpyinfo->mouse_face_mouse_frame)
10831 /* If we move outside the frame, then we're
10832 certainly no longer on any text in the frame. */
10833 clear_mouse_face (dpyinfo);
10834 dpyinfo->mouse_face_mouse_frame = 0;
10837 /* Generate a nil HELP_EVENT to cancel a help-echo.
10838 Do it only if there's something to cancel.
10839 Otherwise, the startup message is cleared when
10840 the mouse leaves the frame. */
10841 if (any_help_event_p)
10843 Lisp_Object frame;
10844 int n;
10846 XSETFRAME (frame, f);
10847 help_echo = Qnil;
10848 n = gen_help_event (bufp, numchars,
10849 Qnil, frame, Qnil, Qnil, 0);
10850 bufp += n, count += n, numchars -= n;
10853 #if 0
10854 if (event.xcrossing.focus)
10855 x_mouse_leave (dpyinfo);
10856 else
10858 if (f == dpyinfo->x_focus_event_frame)
10859 dpyinfo->x_focus_event_frame = 0;
10860 if (f == dpyinfo->x_focus_frame)
10861 x_new_focus_frame (dpyinfo, 0);
10863 #endif
10865 goto OTHER;
10867 case FocusOut:
10868 f = x_any_window_to_frame (dpyinfo, event.xfocus.window);
10869 if (event.xfocus.detail != NotifyPointer
10870 && f == dpyinfo->x_focus_event_frame)
10871 dpyinfo->x_focus_event_frame = 0;
10872 if (f && f == dpyinfo->x_focus_frame)
10873 x_new_focus_frame (dpyinfo, 0);
10875 #ifdef HAVE_X_I18N
10876 if (f && FRAME_XIC (f))
10877 XUnsetICFocus (FRAME_XIC (f));
10878 #endif
10880 goto OTHER;
10882 case MotionNotify:
10884 previous_help_echo = help_echo;
10885 help_echo = help_echo_object = help_echo_window = Qnil;
10886 help_echo_pos = -1;
10888 if (dpyinfo->grabbed && last_mouse_frame
10889 && FRAME_LIVE_P (last_mouse_frame))
10890 f = last_mouse_frame;
10891 else
10892 f = x_window_to_frame (dpyinfo, event.xmotion.window);
10894 if (dpyinfo->mouse_face_hidden)
10896 dpyinfo->mouse_face_hidden = 0;
10897 clear_mouse_face (dpyinfo);
10900 if (f)
10901 note_mouse_movement (f, &event.xmotion);
10902 else
10904 #ifndef USE_TOOLKIT_SCROLL_BARS
10905 struct scroll_bar *bar
10906 = x_window_to_scroll_bar (event.xmotion.window);
10908 if (bar)
10909 x_scroll_bar_note_movement (bar, &event);
10910 #endif /* USE_TOOLKIT_SCROLL_BARS */
10912 /* If we move outside the frame, then we're
10913 certainly no longer on any text in the frame. */
10914 clear_mouse_face (dpyinfo);
10917 /* If the contents of the global variable help_echo
10918 has changed, generate a HELP_EVENT. */
10919 if (!NILP (help_echo)
10920 || !NILP (previous_help_echo))
10922 Lisp_Object frame;
10923 int n;
10925 if (f)
10926 XSETFRAME (frame, f);
10927 else
10928 frame = Qnil;
10930 any_help_event_p = 1;
10931 n = gen_help_event (bufp, numchars, help_echo, frame,
10932 help_echo_window, help_echo_object,
10933 help_echo_pos);
10934 bufp += n, count += n, numchars -= n;
10937 goto OTHER;
10940 case ConfigureNotify:
10941 f = x_top_window_to_frame (dpyinfo, event.xconfigure.window);
10942 if (f)
10944 #ifndef USE_X_TOOLKIT
10945 /* If there is a pending resize for fullscreen, don't
10946 do this one, the right one will come later.
10947 The toolkit version doesn't seem to need this, but we
10948 need to reset it below. */
10949 int dont_resize =
10950 ((f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10951 && FRAME_NEW_WIDTH (f) != 0);
10952 int rows = PIXEL_TO_CHAR_HEIGHT (f, event.xconfigure.height);
10953 int columns = PIXEL_TO_CHAR_WIDTH (f, event.xconfigure.width);
10954 if (dont_resize)
10955 goto OTHER;
10957 /* In the toolkit version, change_frame_size
10958 is called by the code that handles resizing
10959 of the EmacsFrame widget. */
10961 /* Even if the number of character rows and columns has
10962 not changed, the font size may have changed, so we need
10963 to check the pixel dimensions as well. */
10964 if (columns != f->width
10965 || rows != f->height
10966 || event.xconfigure.width != f->output_data.x->pixel_width
10967 || event.xconfigure.height != f->output_data.x->pixel_height)
10969 change_frame_size (f, rows, columns, 0, 1, 0);
10970 SET_FRAME_GARBAGED (f);
10971 cancel_mouse_face (f);
10973 #endif
10975 f->output_data.x->pixel_width = event.xconfigure.width;
10976 f->output_data.x->pixel_height = event.xconfigure.height;
10978 /* What we have now is the position of Emacs's own window.
10979 Convert that to the position of the window manager window. */
10980 x_real_positions (f, &f->output_data.x->left_pos,
10981 &f->output_data.x->top_pos);
10983 x_check_fullscreen_move(f);
10984 if (f->output_data.x->want_fullscreen & FULLSCREEN_WAIT)
10985 f->output_data.x->want_fullscreen &=
10986 ~(FULLSCREEN_WAIT|FULLSCREEN_BOTH);
10987 #ifdef HAVE_X_I18N
10988 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMStatusArea))
10989 xic_set_statusarea (f);
10990 #endif
10992 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
10994 /* Since the WM decorations come below top_pos now,
10995 we must put them below top_pos in the future. */
10996 f->output_data.x->win_gravity = NorthWestGravity;
10997 x_wm_set_size_hint (f, (long) 0, 0);
10999 #ifdef USE_MOTIF
11000 /* Some window managers pass (0,0) as the location of
11001 the window, and the Motif event handler stores it
11002 in the emacs widget, which messes up Motif menus. */
11003 if (event.xconfigure.x == 0 && event.xconfigure.y == 0)
11005 event.xconfigure.x = f->output_data.x->widget->core.x;
11006 event.xconfigure.y = f->output_data.x->widget->core.y;
11008 #endif /* USE_MOTIF */
11010 goto OTHER;
11012 case ButtonPress:
11013 case ButtonRelease:
11015 /* If we decide we want to generate an event to be seen
11016 by the rest of Emacs, we put it here. */
11017 struct input_event emacs_event;
11018 int tool_bar_p = 0;
11020 emacs_event.kind = no_event;
11021 bzero (&compose_status, sizeof (compose_status));
11023 if (dpyinfo->grabbed
11024 && last_mouse_frame
11025 && FRAME_LIVE_P (last_mouse_frame))
11026 f = last_mouse_frame;
11027 else
11028 f = x_window_to_frame (dpyinfo, event.xbutton.window);
11030 if (f)
11032 /* Is this in the tool-bar? */
11033 if (WINDOWP (f->tool_bar_window)
11034 && XFASTINT (XWINDOW (f->tool_bar_window)->height))
11036 Lisp_Object window;
11037 int p, x, y;
11039 x = event.xbutton.x;
11040 y = event.xbutton.y;
11042 /* Set x and y. */
11043 window = window_from_coordinates (f, x, y, &p, 1);
11044 if (EQ (window, f->tool_bar_window))
11046 x_handle_tool_bar_click (f, &event.xbutton);
11047 tool_bar_p = 1;
11051 if (!tool_bar_p)
11052 if (!dpyinfo->x_focus_frame
11053 || f == dpyinfo->x_focus_frame)
11054 construct_mouse_click (&emacs_event, &event, f);
11056 else
11058 #ifndef USE_TOOLKIT_SCROLL_BARS
11059 struct scroll_bar *bar
11060 = x_window_to_scroll_bar (event.xbutton.window);
11062 if (bar)
11063 x_scroll_bar_handle_click (bar, &event, &emacs_event);
11064 #endif /* not USE_TOOLKIT_SCROLL_BARS */
11067 if (event.type == ButtonPress)
11069 dpyinfo->grabbed |= (1 << event.xbutton.button);
11070 last_mouse_frame = f;
11071 /* Ignore any mouse motion that happened
11072 before this event; any subsequent mouse-movement
11073 Emacs events should reflect only motion after
11074 the ButtonPress. */
11075 if (f != 0)
11076 f->mouse_moved = 0;
11078 if (!tool_bar_p)
11079 last_tool_bar_item = -1;
11081 else
11083 dpyinfo->grabbed &= ~(1 << event.xbutton.button);
11086 if (numchars >= 1 && emacs_event.kind != no_event)
11088 bcopy (&emacs_event, bufp, sizeof (struct input_event));
11089 bufp++;
11090 count++;
11091 numchars--;
11094 #ifdef USE_X_TOOLKIT
11095 f = x_menubar_window_to_frame (dpyinfo, event.xbutton.window);
11096 /* For a down-event in the menu bar,
11097 don't pass it to Xt right now.
11098 Instead, save it away
11099 and we will pass it to Xt from kbd_buffer_get_event.
11100 That way, we can run some Lisp code first. */
11101 if (f && event.type == ButtonPress
11102 /* Verify the event is really within the menu bar
11103 and not just sent to it due to grabbing. */
11104 && event.xbutton.x >= 0
11105 && event.xbutton.x < f->output_data.x->pixel_width
11106 && event.xbutton.y >= 0
11107 && event.xbutton.y < f->output_data.x->menubar_height
11108 && event.xbutton.same_screen)
11110 SET_SAVED_BUTTON_EVENT;
11111 XSETFRAME (last_mouse_press_frame, f);
11113 else if (event.type == ButtonPress)
11115 last_mouse_press_frame = Qnil;
11116 goto OTHER;
11119 #ifdef USE_MOTIF /* This should do not harm for Lucid,
11120 but I am trying to be cautious. */
11121 else if (event.type == ButtonRelease)
11123 if (!NILP (last_mouse_press_frame))
11125 f = XFRAME (last_mouse_press_frame);
11126 if (f->output_data.x)
11127 SET_SAVED_BUTTON_EVENT;
11129 else
11130 goto OTHER;
11132 #endif /* USE_MOTIF */
11133 else
11134 goto OTHER;
11135 #endif /* USE_X_TOOLKIT */
11137 break;
11139 case CirculateNotify:
11140 goto OTHER;
11142 case CirculateRequest:
11143 goto OTHER;
11145 case VisibilityNotify:
11146 goto OTHER;
11148 case MappingNotify:
11149 /* Someone has changed the keyboard mapping - update the
11150 local cache. */
11151 switch (event.xmapping.request)
11153 case MappingModifier:
11154 x_find_modifier_meanings (dpyinfo);
11155 /* This is meant to fall through. */
11156 case MappingKeyboard:
11157 XRefreshKeyboardMapping (&event.xmapping);
11159 goto OTHER;
11161 default:
11162 OTHER:
11163 #ifdef USE_X_TOOLKIT
11164 BLOCK_INPUT;
11165 XtDispatchEvent (&event);
11166 UNBLOCK_INPUT;
11167 #endif /* USE_X_TOOLKIT */
11168 break;
11173 out:;
11175 /* On some systems, an X bug causes Emacs to get no more events
11176 when the window is destroyed. Detect that. (1994.) */
11177 if (! event_found)
11179 /* Emacs and the X Server eats up CPU time if XNoOp is done every time.
11180 One XNOOP in 100 loops will make Emacs terminate.
11181 B. Bretthauer, 1994 */
11182 x_noop_count++;
11183 if (x_noop_count >= 100)
11185 x_noop_count=0;
11187 if (next_noop_dpyinfo == 0)
11188 next_noop_dpyinfo = x_display_list;
11190 XNoOp (next_noop_dpyinfo->display);
11192 /* Each time we get here, cycle through the displays now open. */
11193 next_noop_dpyinfo = next_noop_dpyinfo->next;
11197 /* If the focus was just given to an auto-raising frame,
11198 raise it now. */
11199 /* ??? This ought to be able to handle more than one such frame. */
11200 if (pending_autoraise_frame)
11202 x_raise_frame (pending_autoraise_frame);
11203 pending_autoraise_frame = 0;
11206 UNBLOCK_INPUT;
11207 --handling_signal;
11208 return count;
11214 /***********************************************************************
11215 Text Cursor
11216 ***********************************************************************/
11218 /* Notice when the text cursor of window W has been completely
11219 overwritten by a drawing operation that outputs glyphs in AREA
11220 starting at X0 and ending at X1 in the line starting at Y0 and
11221 ending at Y1. X coordinates are area-relative. X1 < 0 means all
11222 the rest of the line after X0 has been written. Y coordinates
11223 are window-relative. */
11225 static void
11226 notice_overwritten_cursor (w, area, x0, x1, y0, y1)
11227 struct window *w;
11228 enum glyph_row_area area;
11229 int x0, y0, x1, y1;
11231 if (area == TEXT_AREA
11232 && w->phys_cursor_on_p
11233 && y0 <= w->phys_cursor.y
11234 && y1 >= w->phys_cursor.y + w->phys_cursor_height
11235 && x0 <= w->phys_cursor.x
11236 && (x1 < 0 || x1 > w->phys_cursor.x))
11237 w->phys_cursor_on_p = 0;
11241 /* Set clipping for output in glyph row ROW. W is the window in which
11242 we operate. GC is the graphics context to set clipping in.
11243 WHOLE_LINE_P non-zero means include the areas used for truncation
11244 mark display and alike in the clipping rectangle.
11246 ROW may be a text row or, e.g., a mode line. Text rows must be
11247 clipped to the interior of the window dedicated to text display,
11248 mode lines must be clipped to the whole window. */
11250 static void
11251 x_clip_to_row (w, row, gc, whole_line_p)
11252 struct window *w;
11253 struct glyph_row *row;
11254 GC gc;
11255 int whole_line_p;
11257 struct frame *f = XFRAME (WINDOW_FRAME (w));
11258 XRectangle clip_rect;
11259 int window_x, window_y, window_width, window_height;
11261 window_box (w, -1, &window_x, &window_y, &window_width, &window_height);
11263 clip_rect.x = WINDOW_TO_FRAME_PIXEL_X (w, 0);
11264 clip_rect.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
11265 clip_rect.y = max (clip_rect.y, window_y);
11266 clip_rect.width = window_width;
11267 clip_rect.height = row->visible_height;
11269 /* If clipping to the whole line, including trunc marks, extend
11270 the rectangle to the left and increase its width. */
11271 if (whole_line_p)
11273 clip_rect.x -= FRAME_X_LEFT_FRINGE_WIDTH (f);
11274 clip_rect.width += FRAME_X_FRINGE_WIDTH (f);
11277 XSetClipRectangles (FRAME_X_DISPLAY (f), gc, 0, 0, &clip_rect, 1, Unsorted);
11281 /* Draw a hollow box cursor on window W in glyph row ROW. */
11283 static void
11284 x_draw_hollow_cursor (w, row)
11285 struct window *w;
11286 struct glyph_row *row;
11288 struct frame *f = XFRAME (WINDOW_FRAME (w));
11289 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11290 Display *dpy = FRAME_X_DISPLAY (f);
11291 int x, y, wd, h;
11292 XGCValues xgcv;
11293 struct glyph *cursor_glyph;
11294 GC gc;
11296 /* Compute frame-relative coordinates from window-relative
11297 coordinates. */
11298 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11299 y = (WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y)
11300 + row->ascent - w->phys_cursor_ascent);
11301 h = row->height - 1;
11303 /* Get the glyph the cursor is on. If we can't tell because
11304 the current matrix is invalid or such, give up. */
11305 cursor_glyph = get_phys_cursor_glyph (w);
11306 if (cursor_glyph == NULL)
11307 return;
11309 /* Compute the width of the rectangle to draw. If on a stretch
11310 glyph, and `x-stretch-block-cursor' is nil, don't draw a
11311 rectangle as wide as the glyph, but use a canonical character
11312 width instead. */
11313 wd = cursor_glyph->pixel_width - 1;
11314 if (cursor_glyph->type == STRETCH_GLYPH
11315 && !x_stretch_cursor_p)
11316 wd = min (CANON_X_UNIT (f), wd);
11318 /* The foreground of cursor_gc is typically the same as the normal
11319 background color, which can cause the cursor box to be invisible. */
11320 xgcv.foreground = f->output_data.x->cursor_pixel;
11321 if (dpyinfo->scratch_cursor_gc)
11322 XChangeGC (dpy, dpyinfo->scratch_cursor_gc, GCForeground, &xgcv);
11323 else
11324 dpyinfo->scratch_cursor_gc = XCreateGC (dpy, FRAME_X_WINDOW (f),
11325 GCForeground, &xgcv);
11326 gc = dpyinfo->scratch_cursor_gc;
11328 /* Set clipping, draw the rectangle, and reset clipping again. */
11329 x_clip_to_row (w, row, gc, 0);
11330 XDrawRectangle (dpy, FRAME_X_WINDOW (f), gc, x, y, wd, h);
11331 XSetClipMask (dpy, gc, None);
11335 /* Draw a bar cursor on window W in glyph row ROW.
11337 Implementation note: One would like to draw a bar cursor with an
11338 angle equal to the one given by the font property XA_ITALIC_ANGLE.
11339 Unfortunately, I didn't find a font yet that has this property set.
11340 --gerd. */
11342 static void
11343 x_draw_bar_cursor (w, row, width)
11344 struct window *w;
11345 struct glyph_row *row;
11346 int width;
11348 struct frame *f = XFRAME (w->frame);
11349 struct glyph *cursor_glyph;
11351 /* If cursor is out of bounds, don't draw garbage. This can happen
11352 in mini-buffer windows when switching between echo area glyphs
11353 and mini-buffer. */
11354 cursor_glyph = get_phys_cursor_glyph (w);
11355 if (cursor_glyph == NULL)
11356 return;
11358 /* If on an image, draw like a normal cursor. That's usually better
11359 visible than drawing a bar, esp. if the image is large so that
11360 the bar might not be in the window. */
11361 if (cursor_glyph->type == IMAGE_GLYPH)
11363 struct glyph_row *row;
11364 row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos);
11365 x_draw_phys_cursor_glyph (w, row, DRAW_CURSOR);
11367 else
11369 Display *dpy = FRAME_X_DISPLAY (f);
11370 Window window = FRAME_X_WINDOW (f);
11371 GC gc = FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc;
11372 unsigned long mask = GCForeground | GCBackground | GCGraphicsExposures;
11373 struct face *face = FACE_FROM_ID (f, cursor_glyph->face_id);
11374 XGCValues xgcv;
11376 /* If the glyph's background equals the color we normally draw
11377 the bar cursor in, the bar cursor in its normal color is
11378 invisible. Use the glyph's foreground color instead in this
11379 case, on the assumption that the glyph's colors are chosen so
11380 that the glyph is legible. */
11381 if (face->background == f->output_data.x->cursor_pixel)
11382 xgcv.background = xgcv.foreground = face->foreground;
11383 else
11384 xgcv.background = xgcv.foreground = f->output_data.x->cursor_pixel;
11385 xgcv.graphics_exposures = 0;
11387 if (gc)
11388 XChangeGC (dpy, gc, mask, &xgcv);
11389 else
11391 gc = XCreateGC (dpy, window, mask, &xgcv);
11392 FRAME_X_DISPLAY_INFO (f)->scratch_cursor_gc = gc;
11395 if (width < 0)
11396 width = f->output_data.x->cursor_width;
11397 width = min (cursor_glyph->pixel_width, width);
11399 x_clip_to_row (w, row, gc, 0);
11400 XFillRectangle (dpy, window, gc,
11401 WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x),
11402 WINDOW_TO_FRAME_PIXEL_Y (w, w->phys_cursor.y),
11403 width, row->height);
11404 XSetClipMask (dpy, gc, None);
11409 /* Clear the cursor of window W to background color, and mark the
11410 cursor as not shown. This is used when the text where the cursor
11411 is is about to be rewritten. */
11413 static void
11414 x_clear_cursor (w)
11415 struct window *w;
11417 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
11418 x_update_window_cursor (w, 0);
11422 /* Draw the cursor glyph of window W in glyph row ROW. See the
11423 comment of x_draw_glyphs for the meaning of HL. */
11425 static void
11426 x_draw_phys_cursor_glyph (w, row, hl)
11427 struct window *w;
11428 struct glyph_row *row;
11429 enum draw_glyphs_face hl;
11431 /* If cursor hpos is out of bounds, don't draw garbage. This can
11432 happen in mini-buffer windows when switching between echo area
11433 glyphs and mini-buffer. */
11434 if (w->phys_cursor.hpos < row->used[TEXT_AREA])
11436 int on_p = w->phys_cursor_on_p;
11438 x_draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
11439 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
11440 hl, 0);
11441 w->phys_cursor_on_p = on_p;
11443 /* When we erase the cursor, and ROW is overlapped by other
11444 rows, make sure that these overlapping parts of other rows
11445 are redrawn. */
11446 if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
11448 if (row > w->current_matrix->rows
11449 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
11450 x_fix_overlapping_area (w, row - 1, TEXT_AREA);
11452 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
11453 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
11454 x_fix_overlapping_area (w, row + 1, TEXT_AREA);
11460 /* Erase the image of a cursor of window W from the screen. */
11462 static void
11463 x_erase_phys_cursor (w)
11464 struct window *w;
11466 struct frame *f = XFRAME (w->frame);
11467 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11468 int hpos = w->phys_cursor.hpos;
11469 int vpos = w->phys_cursor.vpos;
11470 int mouse_face_here_p = 0;
11471 struct glyph_matrix *active_glyphs = w->current_matrix;
11472 struct glyph_row *cursor_row;
11473 struct glyph *cursor_glyph;
11474 enum draw_glyphs_face hl;
11476 /* No cursor displayed or row invalidated => nothing to do on the
11477 screen. */
11478 if (w->phys_cursor_type == NO_CURSOR)
11479 goto mark_cursor_off;
11481 /* VPOS >= active_glyphs->nrows means that window has been resized.
11482 Don't bother to erase the cursor. */
11483 if (vpos >= active_glyphs->nrows)
11484 goto mark_cursor_off;
11486 /* If row containing cursor is marked invalid, there is nothing we
11487 can do. */
11488 cursor_row = MATRIX_ROW (active_glyphs, vpos);
11489 if (!cursor_row->enabled_p)
11490 goto mark_cursor_off;
11492 /* If row is completely invisible, don't attempt to delete a cursor which
11493 isn't there. This can happen if cursor is at top of a window, and
11494 we switch to a buffer with a header line in that window. */
11495 if (cursor_row->visible_height <= 0)
11496 goto mark_cursor_off;
11498 /* This can happen when the new row is shorter than the old one.
11499 In this case, either x_draw_glyphs or clear_end_of_line
11500 should have cleared the cursor. Note that we wouldn't be
11501 able to erase the cursor in this case because we don't have a
11502 cursor glyph at hand. */
11503 if (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])
11504 goto mark_cursor_off;
11506 /* If the cursor is in the mouse face area, redisplay that when
11507 we clear the cursor. */
11508 if (! NILP (dpyinfo->mouse_face_window)
11509 && w == XWINDOW (dpyinfo->mouse_face_window)
11510 && (vpos > dpyinfo->mouse_face_beg_row
11511 || (vpos == dpyinfo->mouse_face_beg_row
11512 && hpos >= dpyinfo->mouse_face_beg_col))
11513 && (vpos < dpyinfo->mouse_face_end_row
11514 || (vpos == dpyinfo->mouse_face_end_row
11515 && hpos < dpyinfo->mouse_face_end_col))
11516 /* Don't redraw the cursor's spot in mouse face if it is at the
11517 end of a line (on a newline). The cursor appears there, but
11518 mouse highlighting does not. */
11519 && cursor_row->used[TEXT_AREA] > hpos)
11520 mouse_face_here_p = 1;
11522 /* Maybe clear the display under the cursor. */
11523 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
11525 int x;
11526 int header_line_height = WINDOW_DISPLAY_HEADER_LINE_HEIGHT (w);
11528 cursor_glyph = get_phys_cursor_glyph (w);
11529 if (cursor_glyph == NULL)
11530 goto mark_cursor_off;
11532 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, w->phys_cursor.x);
11534 x_clear_area (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
11536 WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height,
11537 cursor_row->y)),
11538 cursor_glyph->pixel_width,
11539 cursor_row->visible_height,
11540 False);
11543 /* Erase the cursor by redrawing the character underneath it. */
11544 if (mouse_face_here_p)
11545 hl = DRAW_MOUSE_FACE;
11546 else
11547 hl = DRAW_NORMAL_TEXT;
11548 x_draw_phys_cursor_glyph (w, cursor_row, hl);
11550 mark_cursor_off:
11551 w->phys_cursor_on_p = 0;
11552 w->phys_cursor_type = NO_CURSOR;
11556 /* Non-zero if physical cursor of window W is within mouse face. */
11558 static int
11559 cursor_in_mouse_face_p (w)
11560 struct window *w;
11562 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (XFRAME (w->frame));
11563 int in_mouse_face = 0;
11565 if (WINDOWP (dpyinfo->mouse_face_window)
11566 && XWINDOW (dpyinfo->mouse_face_window) == w)
11568 int hpos = w->phys_cursor.hpos;
11569 int vpos = w->phys_cursor.vpos;
11571 if (vpos >= dpyinfo->mouse_face_beg_row
11572 && vpos <= dpyinfo->mouse_face_end_row
11573 && (vpos > dpyinfo->mouse_face_beg_row
11574 || hpos >= dpyinfo->mouse_face_beg_col)
11575 && (vpos < dpyinfo->mouse_face_end_row
11576 || hpos < dpyinfo->mouse_face_end_col
11577 || dpyinfo->mouse_face_past_end))
11578 in_mouse_face = 1;
11581 return in_mouse_face;
11585 /* Display or clear cursor of window W. If ON is zero, clear the
11586 cursor. If it is non-zero, display the cursor. If ON is nonzero,
11587 where to put the cursor is specified by HPOS, VPOS, X and Y. */
11589 void
11590 x_display_and_set_cursor (w, on, hpos, vpos, x, y)
11591 struct window *w;
11592 int on, hpos, vpos, x, y;
11594 struct frame *f = XFRAME (w->frame);
11595 int new_cursor_type;
11596 int new_cursor_width;
11597 struct glyph_matrix *current_glyphs;
11598 struct glyph_row *glyph_row;
11599 struct glyph *glyph;
11600 int cursor_non_selected;
11602 /* This is pointless on invisible frames, and dangerous on garbaged
11603 windows and frames; in the latter case, the frame or window may
11604 be in the midst of changing its size, and x and y may be off the
11605 window. */
11606 if (! FRAME_VISIBLE_P (f)
11607 || FRAME_GARBAGED_P (f)
11608 || vpos >= w->current_matrix->nrows
11609 || hpos >= w->current_matrix->matrix_w)
11610 return;
11612 /* If cursor is off and we want it off, return quickly. */
11613 if (!on && !w->phys_cursor_on_p)
11614 return;
11616 current_glyphs = w->current_matrix;
11617 glyph_row = MATRIX_ROW (current_glyphs, vpos);
11618 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
11620 /* If cursor row is not enabled, we don't really know where to
11621 display the cursor. */
11622 if (!glyph_row->enabled_p)
11624 w->phys_cursor_on_p = 0;
11625 return;
11628 xassert (interrupt_input_blocked);
11630 /* Set new_cursor_type to the cursor we want to be displayed. In a
11631 mini-buffer window, we want the cursor only to appear if we are
11632 reading input from this window. For the selected window, we want
11633 the cursor type given by the frame parameter. If explicitly
11634 marked off, draw no cursor. In all other cases, we want a hollow
11635 box cursor. */
11636 cursor_non_selected
11637 = !NILP (Fbuffer_local_value (Qcursor_in_non_selected_windows,
11638 w->buffer));
11639 new_cursor_width = -1;
11640 if (cursor_in_echo_area
11641 && FRAME_HAS_MINIBUF_P (f)
11642 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
11644 if (w == XWINDOW (echo_area_window))
11645 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11646 else if (cursor_non_selected)
11647 new_cursor_type = HOLLOW_BOX_CURSOR;
11648 else
11649 new_cursor_type = NO_CURSOR;
11651 else
11653 if (f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame
11654 || w != XWINDOW (f->selected_window))
11656 if ((MINI_WINDOW_P (w) && minibuf_level == 0)
11657 || !cursor_non_selected
11658 || NILP (XBUFFER (w->buffer)->cursor_type))
11659 new_cursor_type = NO_CURSOR;
11660 else
11661 new_cursor_type = HOLLOW_BOX_CURSOR;
11663 else if (w->cursor_off_p)
11664 new_cursor_type = NO_CURSOR;
11665 else
11667 struct buffer *b = XBUFFER (w->buffer);
11669 if (EQ (b->cursor_type, Qt))
11670 new_cursor_type = FRAME_DESIRED_CURSOR (f);
11671 else
11672 new_cursor_type = x_specified_cursor_type (b->cursor_type,
11673 &new_cursor_width);
11677 /* If cursor is currently being shown and we don't want it to be or
11678 it is in the wrong place, or the cursor type is not what we want,
11679 erase it. */
11680 if (w->phys_cursor_on_p
11681 && (!on
11682 || w->phys_cursor.x != x
11683 || w->phys_cursor.y != y
11684 || new_cursor_type != w->phys_cursor_type))
11685 x_erase_phys_cursor (w);
11687 /* If the cursor is now invisible and we want it to be visible,
11688 display it. */
11689 if (on && !w->phys_cursor_on_p)
11691 w->phys_cursor_ascent = glyph_row->ascent;
11692 w->phys_cursor_height = glyph_row->height;
11694 /* Set phys_cursor_.* before x_draw_.* is called because some
11695 of them may need the information. */
11696 w->phys_cursor.x = x;
11697 w->phys_cursor.y = glyph_row->y;
11698 w->phys_cursor.hpos = hpos;
11699 w->phys_cursor.vpos = vpos;
11700 w->phys_cursor_type = new_cursor_type;
11701 w->phys_cursor_on_p = 1;
11703 switch (new_cursor_type)
11705 case HOLLOW_BOX_CURSOR:
11706 x_draw_hollow_cursor (w, glyph_row);
11707 break;
11709 case FILLED_BOX_CURSOR:
11710 x_draw_phys_cursor_glyph (w, glyph_row, DRAW_CURSOR);
11711 break;
11713 case BAR_CURSOR:
11714 x_draw_bar_cursor (w, glyph_row, new_cursor_width);
11715 break;
11717 case NO_CURSOR:
11718 break;
11720 default:
11721 abort ();
11724 #ifdef HAVE_X_I18N
11725 if (w == XWINDOW (f->selected_window))
11726 if (FRAME_XIC (f) && (FRAME_XIC_STYLE (f) & XIMPreeditPosition))
11727 xic_set_preeditarea (w, x, y);
11728 #endif
11731 #ifndef XFlush
11732 if (updating_frame != f)
11733 XFlush (FRAME_X_DISPLAY (f));
11734 #endif
11738 /* Display the cursor on window W, or clear it. X and Y are window
11739 relative pixel coordinates. HPOS and VPOS are glyph matrix
11740 positions. If W is not the selected window, display a hollow
11741 cursor. ON non-zero means display the cursor at X, Y which
11742 correspond to HPOS, VPOS, otherwise it is cleared. */
11744 void
11745 x_display_cursor (w, on, hpos, vpos, x, y)
11746 struct window *w;
11747 int on, hpos, vpos, x, y;
11749 BLOCK_INPUT;
11750 x_display_and_set_cursor (w, on, hpos, vpos, x, y);
11751 UNBLOCK_INPUT;
11755 /* Display the cursor on window W, or clear it, according to ON_P.
11756 Don't change the cursor's position. */
11758 void
11759 x_update_cursor (f, on_p)
11760 struct frame *f;
11762 x_update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
11766 /* Call x_update_window_cursor with parameter ON_P on all leaf windows
11767 in the window tree rooted at W. */
11769 static void
11770 x_update_cursor_in_window_tree (w, on_p)
11771 struct window *w;
11772 int on_p;
11774 while (w)
11776 if (!NILP (w->hchild))
11777 x_update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
11778 else if (!NILP (w->vchild))
11779 x_update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
11780 else
11781 x_update_window_cursor (w, on_p);
11783 w = NILP (w->next) ? 0 : XWINDOW (w->next);
11788 /* Switch the display of W's cursor on or off, according to the value
11789 of ON. */
11791 static void
11792 x_update_window_cursor (w, on)
11793 struct window *w;
11794 int on;
11796 /* Don't update cursor in windows whose frame is in the process
11797 of being deleted. */
11798 if (w->current_matrix)
11800 BLOCK_INPUT;
11801 x_display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
11802 w->phys_cursor.x, w->phys_cursor.y);
11803 UNBLOCK_INPUT;
11810 /* Icons. */
11812 /* Make the x-window of frame F use the gnu icon bitmap. */
11815 x_bitmap_icon (f, file)
11816 struct frame *f;
11817 Lisp_Object file;
11819 int bitmap_id;
11821 if (FRAME_X_WINDOW (f) == 0)
11822 return 1;
11824 /* Free up our existing icon bitmap if any. */
11825 if (f->output_data.x->icon_bitmap > 0)
11826 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11827 f->output_data.x->icon_bitmap = 0;
11829 if (STRINGP (file))
11830 bitmap_id = x_create_bitmap_from_file (f, file);
11831 else
11833 /* Create the GNU bitmap if necessary. */
11834 if (FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id < 0)
11835 FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id
11836 = x_create_bitmap_from_data (f, gnu_bits,
11837 gnu_width, gnu_height);
11839 /* The first time we create the GNU bitmap,
11840 this increments the ref-count one extra time.
11841 As a result, the GNU bitmap is never freed.
11842 That way, we don't have to worry about allocating it again. */
11843 x_reference_bitmap (f, FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id);
11845 bitmap_id = FRAME_X_DISPLAY_INFO (f)->icon_bitmap_id;
11848 x_wm_set_icon_pixmap (f, bitmap_id);
11849 f->output_data.x->icon_bitmap = bitmap_id;
11851 return 0;
11855 /* Make the x-window of frame F use a rectangle with text.
11856 Use ICON_NAME as the text. */
11859 x_text_icon (f, icon_name)
11860 struct frame *f;
11861 char *icon_name;
11863 if (FRAME_X_WINDOW (f) == 0)
11864 return 1;
11866 #ifdef HAVE_X11R4
11868 XTextProperty text;
11869 text.value = (unsigned char *) icon_name;
11870 text.encoding = XA_STRING;
11871 text.format = 8;
11872 text.nitems = strlen (icon_name);
11873 #ifdef USE_X_TOOLKIT
11874 XSetWMIconName (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
11875 &text);
11876 #else /* not USE_X_TOOLKIT */
11877 XSetWMIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), &text);
11878 #endif /* not USE_X_TOOLKIT */
11880 #else /* not HAVE_X11R4 */
11881 XSetIconName (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f), icon_name);
11882 #endif /* not HAVE_X11R4 */
11884 if (f->output_data.x->icon_bitmap > 0)
11885 x_destroy_bitmap (f, f->output_data.x->icon_bitmap);
11886 f->output_data.x->icon_bitmap = 0;
11887 x_wm_set_icon_pixmap (f, 0);
11889 return 0;
11892 #define X_ERROR_MESSAGE_SIZE 200
11894 /* If non-nil, this should be a string.
11895 It means catch X errors and store the error message in this string. */
11897 static Lisp_Object x_error_message_string;
11899 /* An X error handler which stores the error message in
11900 x_error_message_string. This is called from x_error_handler if
11901 x_catch_errors is in effect. */
11903 static void
11904 x_error_catcher (display, error)
11905 Display *display;
11906 XErrorEvent *error;
11908 XGetErrorText (display, error->error_code,
11909 XSTRING (x_error_message_string)->data,
11910 X_ERROR_MESSAGE_SIZE);
11913 /* Begin trapping X errors for display DPY. Actually we trap X errors
11914 for all displays, but DPY should be the display you are actually
11915 operating on.
11917 After calling this function, X protocol errors no longer cause
11918 Emacs to exit; instead, they are recorded in the string
11919 stored in x_error_message_string.
11921 Calling x_check_errors signals an Emacs error if an X error has
11922 occurred since the last call to x_catch_errors or x_check_errors.
11924 Calling x_uncatch_errors resumes the normal error handling. */
11926 void x_check_errors ();
11927 static Lisp_Object x_catch_errors_unwind ();
11930 x_catch_errors (dpy)
11931 Display *dpy;
11933 int count = specpdl_ptr - specpdl;
11935 /* Make sure any errors from previous requests have been dealt with. */
11936 XSync (dpy, False);
11938 record_unwind_protect (x_catch_errors_unwind, x_error_message_string);
11940 x_error_message_string = make_uninit_string (X_ERROR_MESSAGE_SIZE);
11941 XSTRING (x_error_message_string)->data[0] = 0;
11943 return count;
11946 /* Unbind the binding that we made to check for X errors. */
11948 static Lisp_Object
11949 x_catch_errors_unwind (old_val)
11950 Lisp_Object old_val;
11952 x_error_message_string = old_val;
11953 return Qnil;
11956 /* If any X protocol errors have arrived since the last call to
11957 x_catch_errors or x_check_errors, signal an Emacs error using
11958 sprintf (a buffer, FORMAT, the x error message text) as the text. */
11960 void
11961 x_check_errors (dpy, format)
11962 Display *dpy;
11963 char *format;
11965 /* Make sure to catch any errors incurred so far. */
11966 XSync (dpy, False);
11968 if (XSTRING (x_error_message_string)->data[0])
11969 error (format, XSTRING (x_error_message_string)->data);
11972 /* Nonzero if we had any X protocol errors
11973 since we did x_catch_errors on DPY. */
11976 x_had_errors_p (dpy)
11977 Display *dpy;
11979 /* Make sure to catch any errors incurred so far. */
11980 XSync (dpy, False);
11982 return XSTRING (x_error_message_string)->data[0] != 0;
11985 /* Forget about any errors we have had, since we did x_catch_errors on DPY. */
11987 void
11988 x_clear_errors (dpy)
11989 Display *dpy;
11991 XSTRING (x_error_message_string)->data[0] = 0;
11994 /* Stop catching X protocol errors and let them make Emacs die.
11995 DPY should be the display that was passed to x_catch_errors.
11996 COUNT should be the value that was returned by
11997 the corresponding call to x_catch_errors. */
11999 void
12000 x_uncatch_errors (dpy, count)
12001 Display *dpy;
12002 int count;
12004 unbind_to (count, Qnil);
12007 #if 0
12008 static unsigned int x_wire_count;
12009 x_trace_wire ()
12011 fprintf (stderr, "Lib call: %d\n", ++x_wire_count);
12013 #endif /* ! 0 */
12016 /* Handle SIGPIPE, which can happen when the connection to a server
12017 simply goes away. SIGPIPE is handled by x_connection_signal.
12018 Don't need to do anything, because the write which caused the
12019 SIGPIPE will fail, causing Xlib to invoke the X IO error handler,
12020 which will do the appropriate cleanup for us. */
12022 static SIGTYPE
12023 x_connection_signal (signalnum) /* If we don't have an argument, */
12024 int signalnum; /* some compilers complain in signal calls. */
12026 #ifdef USG
12027 /* USG systems forget handlers when they are used;
12028 must reestablish each time */
12029 signal (signalnum, x_connection_signal);
12030 #endif /* USG */
12034 /************************************************************************
12035 Handling X errors
12036 ************************************************************************/
12038 /* Error message passed to x_connection_closed. */
12040 static char *error_msg;
12042 /* Function installed as fatal_error_signal_hook in
12043 x_connection_closed. Print the X error message, and exit normally,
12044 instead of dumping core when XtCloseDisplay fails. */
12046 static void
12047 x_fatal_error_signal ()
12049 fprintf (stderr, "%s\n", error_msg);
12050 exit (70);
12053 /* Handle the loss of connection to display DPY. ERROR_MESSAGE is
12054 the text of an error message that lead to the connection loss. */
12056 static SIGTYPE
12057 x_connection_closed (dpy, error_message)
12058 Display *dpy;
12059 char *error_message;
12061 struct x_display_info *dpyinfo = x_display_info_for_display (dpy);
12062 Lisp_Object frame, tail;
12063 int count;
12065 error_msg = (char *) alloca (strlen (error_message) + 1);
12066 strcpy (error_msg, error_message);
12067 handling_signal = 0;
12069 /* Prevent being called recursively because of an error condition
12070 below. Otherwise, we might end up with printing ``can't find per
12071 display information'' in the recursive call instead of printing
12072 the original message here. */
12073 count = x_catch_errors (dpy);
12075 /* We have to close the display to inform Xt that it doesn't
12076 exist anymore. If we don't, Xt will continue to wait for
12077 events from the display. As a consequence, a sequence of
12079 M-x make-frame-on-display RET :1 RET
12080 ...kill the new frame, so that we get an IO error...
12081 M-x make-frame-on-display RET :1 RET
12083 will indefinitely wait in Xt for events for display `:1', opened
12084 in the first class to make-frame-on-display.
12086 Closing the display is reported to lead to a bus error on
12087 OpenWindows in certain situations. I suspect that is a bug
12088 in OpenWindows. I don't know how to cicumvent it here. */
12090 #ifdef USE_X_TOOLKIT
12091 /* If DPYINFO is null, this means we didn't open the display
12092 in the first place, so don't try to close it. */
12093 if (dpyinfo)
12095 extern void (*fatal_error_signal_hook) P_ ((void));
12096 fatal_error_signal_hook = x_fatal_error_signal;
12097 XtCloseDisplay (dpy);
12098 fatal_error_signal_hook = NULL;
12100 #endif
12102 /* Indicate that this display is dead. */
12103 if (dpyinfo)
12104 dpyinfo->display = 0;
12106 /* First delete frames whose mini-buffers are on frames
12107 that are on the dead display. */
12108 FOR_EACH_FRAME (tail, frame)
12110 Lisp_Object minibuf_frame;
12111 minibuf_frame
12112 = WINDOW_FRAME (XWINDOW (FRAME_MINIBUF_WINDOW (XFRAME (frame))));
12113 if (FRAME_X_P (XFRAME (frame))
12114 && FRAME_X_P (XFRAME (minibuf_frame))
12115 && ! EQ (frame, minibuf_frame)
12116 && FRAME_X_DISPLAY_INFO (XFRAME (minibuf_frame)) == dpyinfo)
12117 Fdelete_frame (frame, Qt);
12120 /* Now delete all remaining frames on the dead display.
12121 We are now sure none of these is used as the mini-buffer
12122 for another frame that we need to delete. */
12123 FOR_EACH_FRAME (tail, frame)
12124 if (FRAME_X_P (XFRAME (frame))
12125 && FRAME_X_DISPLAY_INFO (XFRAME (frame)) == dpyinfo)
12127 /* Set this to t so that Fdelete_frame won't get confused
12128 trying to find a replacement. */
12129 FRAME_KBOARD (XFRAME (frame))->Vdefault_minibuffer_frame = Qt;
12130 Fdelete_frame (frame, Qt);
12133 if (dpyinfo)
12134 x_delete_display (dpyinfo);
12136 x_uncatch_errors (dpy, count);
12138 if (x_display_list == 0)
12140 fprintf (stderr, "%s\n", error_msg);
12141 shut_down_emacs (0, 0, Qnil);
12142 exit (70);
12145 /* Ordinary stack unwind doesn't deal with these. */
12146 #ifdef SIGIO
12147 sigunblock (sigmask (SIGIO));
12148 #endif
12149 sigunblock (sigmask (SIGALRM));
12150 TOTALLY_UNBLOCK_INPUT;
12152 clear_waiting_for_input ();
12153 error ("%s", error_msg);
12157 /* This is the usual handler for X protocol errors.
12158 It kills all frames on the display that we got the error for.
12159 If that was the only one, it prints an error message and kills Emacs. */
12161 static void
12162 x_error_quitter (display, error)
12163 Display *display;
12164 XErrorEvent *error;
12166 char buf[256], buf1[356];
12168 /* Note that there is no real way portable across R3/R4 to get the
12169 original error handler. */
12171 XGetErrorText (display, error->error_code, buf, sizeof (buf));
12172 sprintf (buf1, "X protocol error: %s on protocol request %d",
12173 buf, error->request_code);
12174 x_connection_closed (display, buf1);
12178 /* This is the first-level handler for X protocol errors.
12179 It calls x_error_quitter or x_error_catcher. */
12181 static int
12182 x_error_handler (display, error)
12183 Display *display;
12184 XErrorEvent *error;
12186 if (! NILP (x_error_message_string))
12187 x_error_catcher (display, error);
12188 else
12189 x_error_quitter (display, error);
12190 return 0;
12193 /* This is the handler for X IO errors, always.
12194 It kills all frames on the display that we lost touch with.
12195 If that was the only one, it prints an error message and kills Emacs. */
12197 static int
12198 x_io_error_quitter (display)
12199 Display *display;
12201 char buf[256];
12203 sprintf (buf, "Connection lost to X server `%s'", DisplayString (display));
12204 x_connection_closed (display, buf);
12205 return 0;
12208 /* Changing the font of the frame. */
12210 /* Give frame F the font named FONTNAME as its default font, and
12211 return the full name of that font. FONTNAME may be a wildcard
12212 pattern; in that case, we choose some font that fits the pattern.
12213 The return value shows which font we chose. */
12215 Lisp_Object
12216 x_new_font (f, fontname)
12217 struct frame *f;
12218 register char *fontname;
12220 struct font_info *fontp
12221 = FS_LOAD_FONT (f, fontname);
12223 if (!fontp)
12224 return Qnil;
12226 f->output_data.x->font = (XFontStruct *) (fontp->font);
12227 f->output_data.x->baseline_offset = fontp->baseline_offset;
12228 f->output_data.x->fontset = -1;
12230 x_compute_fringe_widths (f, 1);
12232 /* Compute the scroll bar width in character columns. */
12233 if (f->scroll_bar_pixel_width > 0)
12235 int wid = FONT_WIDTH (f->output_data.x->font);
12236 f->scroll_bar_cols = (f->scroll_bar_pixel_width + wid-1) / wid;
12238 else
12240 int wid = FONT_WIDTH (f->output_data.x->font);
12241 f->scroll_bar_cols = (14 + wid - 1) / wid;
12244 /* Now make the frame display the given font. */
12245 if (FRAME_X_WINDOW (f) != 0)
12247 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->normal_gc,
12248 f->output_data.x->font->fid);
12249 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->reverse_gc,
12250 f->output_data.x->font->fid);
12251 XSetFont (FRAME_X_DISPLAY (f), f->output_data.x->cursor_gc,
12252 f->output_data.x->font->fid);
12254 frame_update_line_height (f);
12256 /* Don't change the size of a tip frame; there's no point in
12257 doing it because it's done in Fx_show_tip, and it leads to
12258 problems because the tip frame has no widget. */
12259 if (NILP (tip_frame) || XFRAME (tip_frame) != f)
12260 x_set_window_size (f, 0, f->width, f->height);
12262 else
12263 /* If we are setting a new frame's font for the first time,
12264 there are no faces yet, so this font's height is the line height. */
12265 f->output_data.x->line_height = FONT_HEIGHT (f->output_data.x->font);
12267 return build_string (fontp->full_name);
12270 /* Give frame F the fontset named FONTSETNAME as its default font, and
12271 return the full name of that fontset. FONTSETNAME may be a wildcard
12272 pattern; in that case, we choose some fontset that fits the pattern.
12273 The return value shows which fontset we chose.
12274 If FONTSETNAME specifies the default fontset, return Qt.
12275 If an ASCII font specified in the specified fontset can't be
12276 loaded, return Qnil. */
12278 Lisp_Object
12279 x_new_fontset (f, fontsetname)
12280 struct frame *f;
12281 char *fontsetname;
12283 int fontset = fs_query_fontset (build_string (fontsetname), 0);
12284 Lisp_Object result;
12286 if (fontset > 0 && f->output_data.x->fontset == fontset)
12287 /* This fontset is already set in frame F. There's nothing more
12288 to do. */
12289 return fontset_name (fontset);
12290 else if (fontset == 0)
12291 /* The default fontset can't be the default font. */
12292 return Qt;
12294 if (fontset >= 0)
12295 result = x_new_font (f, (XSTRING (fontset_ascii (fontset))->data));
12296 else
12297 result = x_new_font (f, fontsetname);
12299 if (!STRINGP (result))
12300 /* Can't load ASCII font. */
12301 return Qnil;
12303 if (fontset < 0)
12305 Lisp_Object func;
12307 func = intern ("create-fontset-from-ascii-font");
12308 if (! NILP (Ffboundp (func)))
12309 result = call2 (func, result, result);
12310 else
12311 Fnew_fontset (result,
12312 Fcons (Fcons (Qascii, Fcons (result, Qnil)), Qnil));
12313 fontset = fs_query_fontset (result, 0);
12316 /* Since x_new_font doesn't update any fontset information, do it now. */
12317 f->output_data.x->fontset = fontset;
12319 #ifdef HAVE_X_I18N
12320 if (FRAME_XIC (f)
12321 && (FRAME_XIC_STYLE (f) & (XIMPreeditPosition | XIMStatusArea)))
12322 xic_set_xfontset (f, XSTRING (fontset_ascii (fontset))->data);
12323 #endif
12325 return fontset_name (fontset);
12328 /* Compute actual fringe widths */
12330 void
12331 x_compute_fringe_widths (f, redraw)
12332 struct frame *f;
12333 int redraw;
12335 int o_left = f->output_data.x->left_fringe_width;
12336 int o_right = f->output_data.x->right_fringe_width;
12337 int o_cols = f->output_data.x->fringe_cols;
12339 Lisp_Object left_fringe = Fassq (Qleft_fringe, f->param_alist);
12340 Lisp_Object right_fringe = Fassq (Qright_fringe, f->param_alist);
12341 int left_fringe_width, right_fringe_width;
12343 if (!NILP (left_fringe))
12344 left_fringe = Fcdr (left_fringe);
12345 if (!NILP (right_fringe))
12346 right_fringe = Fcdr (right_fringe);
12348 left_fringe_width = ((NILP (left_fringe) || !INTEGERP (left_fringe)) ? 8 :
12349 XINT (left_fringe));
12350 right_fringe_width = ((NILP (right_fringe) || !INTEGERP (right_fringe)) ? 8 :
12351 XINT (right_fringe));
12353 if (left_fringe_width || right_fringe_width)
12355 int left_wid = left_fringe_width >= 0 ? left_fringe_width : -left_fringe_width;
12356 int right_wid = right_fringe_width >= 0 ? right_fringe_width : -right_fringe_width;
12357 int conf_wid = left_wid + right_wid;
12358 int font_wid = FONT_WIDTH (f->output_data.x->font);
12359 int cols = (left_wid + right_wid + font_wid-1) / font_wid;
12360 int real_wid = cols * font_wid;
12361 if (left_wid && right_wid)
12363 if (left_fringe_width < 0)
12365 /* Left fringe width is fixed, adjust right fringe if necessary */
12366 f->output_data.x->left_fringe_width = left_wid;
12367 f->output_data.x->right_fringe_width = real_wid - left_wid;
12369 else if (right_fringe_width < 0)
12371 /* Right fringe width is fixed, adjust left fringe if necessary */
12372 f->output_data.x->left_fringe_width = real_wid - right_wid;
12373 f->output_data.x->right_fringe_width = right_wid;
12375 else
12377 /* Adjust both fringes with an equal amount.
12378 Note that we are doing integer arithmetic here, so don't
12379 lose a pixel if the total width is an odd number. */
12380 int fill = real_wid - conf_wid;
12381 f->output_data.x->left_fringe_width = left_wid + fill/2;
12382 f->output_data.x->right_fringe_width = right_wid + fill - fill/2;
12385 else if (left_fringe_width)
12387 f->output_data.x->left_fringe_width = real_wid;
12388 f->output_data.x->right_fringe_width = 0;
12390 else
12392 f->output_data.x->left_fringe_width = 0;
12393 f->output_data.x->right_fringe_width = real_wid;
12395 f->output_data.x->fringe_cols = cols;
12396 f->output_data.x->fringes_extra = real_wid;
12398 else
12400 f->output_data.x->left_fringe_width = 0;
12401 f->output_data.x->right_fringe_width = 0;
12402 f->output_data.x->fringe_cols = 0;
12403 f->output_data.x->fringes_extra = 0;
12406 if (redraw && FRAME_VISIBLE_P (f))
12407 if (o_left != f->output_data.x->left_fringe_width ||
12408 o_right != f->output_data.x->right_fringe_width ||
12409 o_cols != f->output_data.x->fringe_cols)
12410 redraw_frame (f);
12413 /***********************************************************************
12414 X Input Methods
12415 ***********************************************************************/
12417 #ifdef HAVE_X_I18N
12419 #ifdef HAVE_X11R6
12421 /* XIM destroy callback function, which is called whenever the
12422 connection to input method XIM dies. CLIENT_DATA contains a
12423 pointer to the x_display_info structure corresponding to XIM. */
12425 static void
12426 xim_destroy_callback (xim, client_data, call_data)
12427 XIM xim;
12428 XPointer client_data;
12429 XPointer call_data;
12431 struct x_display_info *dpyinfo = (struct x_display_info *) client_data;
12432 Lisp_Object frame, tail;
12434 BLOCK_INPUT;
12436 /* No need to call XDestroyIC.. */
12437 FOR_EACH_FRAME (tail, frame)
12439 struct frame *f = XFRAME (frame);
12440 if (FRAME_X_DISPLAY_INFO (f) == dpyinfo)
12442 FRAME_XIC (f) = NULL;
12443 if (FRAME_XIC_FONTSET (f))
12445 XFreeFontSet (FRAME_X_DISPLAY (f), FRAME_XIC_FONTSET (f));
12446 FRAME_XIC_FONTSET (f) = NULL;
12451 /* No need to call XCloseIM. */
12452 dpyinfo->xim = NULL;
12453 XFree (dpyinfo->xim_styles);
12454 UNBLOCK_INPUT;
12457 #endif /* HAVE_X11R6 */
12459 /* Open the connection to the XIM server on display DPYINFO.
12460 RESOURCE_NAME is the resource name Emacs uses. */
12462 static void
12463 xim_open_dpy (dpyinfo, resource_name)
12464 struct x_display_info *dpyinfo;
12465 char *resource_name;
12467 XIM xim;
12469 if (use_xim)
12471 xim = XOpenIM (dpyinfo->display, dpyinfo->xrdb, resource_name,
12472 EMACS_CLASS);
12473 dpyinfo->xim = xim;
12475 if (xim)
12477 #ifdef HAVE_X11R6
12478 XIMCallback destroy;
12479 #endif
12481 /* Get supported styles and XIM values. */
12482 XGetIMValues (xim, XNQueryInputStyle, &dpyinfo->xim_styles, NULL);
12484 #ifdef HAVE_X11R6
12485 destroy.callback = xim_destroy_callback;
12486 destroy.client_data = (XPointer)dpyinfo;
12487 XSetIMValues (xim, XNDestroyCallback, &destroy, NULL);
12488 #endif
12491 else
12492 dpyinfo->xim = NULL;
12496 #ifdef HAVE_X11R6_XIM
12498 struct xim_inst_t
12500 struct x_display_info *dpyinfo;
12501 char *resource_name;
12504 /* XIM instantiate callback function, which is called whenever an XIM
12505 server is available. DISPLAY is teh display of the XIM.
12506 CLIENT_DATA contains a pointer to an xim_inst_t structure created
12507 when the callback was registered. */
12509 static void
12510 xim_instantiate_callback (display, client_data, call_data)
12511 Display *display;
12512 XPointer client_data;
12513 XPointer call_data;
12515 struct xim_inst_t *xim_inst = (struct xim_inst_t *) client_data;
12516 struct x_display_info *dpyinfo = xim_inst->dpyinfo;
12518 /* We don't support multiple XIM connections. */
12519 if (dpyinfo->xim)
12520 return;
12522 xim_open_dpy (dpyinfo, xim_inst->resource_name);
12524 /* Create XIC for the existing frames on the same display, as long
12525 as they have no XIC. */
12526 if (dpyinfo->xim && dpyinfo->reference_count > 0)
12528 Lisp_Object tail, frame;
12530 BLOCK_INPUT;
12531 FOR_EACH_FRAME (tail, frame)
12533 struct frame *f = XFRAME (frame);
12535 if (FRAME_X_DISPLAY_INFO (f) == xim_inst->dpyinfo)
12536 if (FRAME_XIC (f) == NULL)
12538 create_frame_xic (f);
12539 if (FRAME_XIC_STYLE (f) & XIMStatusArea)
12540 xic_set_statusarea (f);
12541 if (FRAME_XIC_STYLE (f) & XIMPreeditPosition)
12543 struct window *w = XWINDOW (f->selected_window);
12544 xic_set_preeditarea (w, w->cursor.x, w->cursor.y);
12549 UNBLOCK_INPUT;
12553 #endif /* HAVE_X11R6_XIM */
12556 /* Open a connection to the XIM server on display DPYINFO.
12557 RESOURCE_NAME is the resource name for Emacs. On X11R5, open the
12558 connection only at the first time. On X11R6, open the connection
12559 in the XIM instantiate callback function. */
12561 static void
12562 xim_initialize (dpyinfo, resource_name)
12563 struct x_display_info *dpyinfo;
12564 char *resource_name;
12566 if (use_xim)
12568 #ifdef HAVE_X11R6_XIM
12569 struct xim_inst_t *xim_inst;
12570 int len;
12572 dpyinfo->xim = NULL;
12573 xim_inst = (struct xim_inst_t *) xmalloc (sizeof (struct xim_inst_t));
12574 xim_inst->dpyinfo = dpyinfo;
12575 len = strlen (resource_name);
12576 xim_inst->resource_name = (char *) xmalloc (len + 1);
12577 bcopy (resource_name, xim_inst->resource_name, len + 1);
12578 XRegisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12579 resource_name, EMACS_CLASS,
12580 xim_instantiate_callback,
12581 /* Fixme: This is XPointer in
12582 XFree86 but (XPointer *) on
12583 Tru64, at least. */
12584 (XPointer) xim_inst);
12585 #else /* not HAVE_X11R6_XIM */
12586 dpyinfo->xim = NULL;
12587 xim_open_dpy (dpyinfo, resource_name);
12588 #endif /* not HAVE_X11R6_XIM */
12590 else
12591 dpyinfo->xim = NULL;
12595 /* Close the connection to the XIM server on display DPYINFO. */
12597 static void
12598 xim_close_dpy (dpyinfo)
12599 struct x_display_info *dpyinfo;
12601 if (use_xim)
12603 #ifdef HAVE_X11R6_XIM
12604 if (dpyinfo->display)
12605 XUnregisterIMInstantiateCallback (dpyinfo->display, dpyinfo->xrdb,
12606 NULL, EMACS_CLASS,
12607 xim_instantiate_callback, NULL);
12608 #endif /* not HAVE_X11R6_XIM */
12609 if (dpyinfo->display)
12610 XCloseIM (dpyinfo->xim);
12611 dpyinfo->xim = NULL;
12612 XFree (dpyinfo->xim_styles);
12616 #endif /* not HAVE_X11R6_XIM */
12620 /* Calculate the absolute position in frame F
12621 from its current recorded position values and gravity. */
12623 void
12624 x_calc_absolute_position (f)
12625 struct frame *f;
12627 Window child;
12628 int win_x = 0, win_y = 0;
12629 int flags = f->output_data.x->size_hint_flags;
12630 int this_window;
12632 /* We have nothing to do if the current position
12633 is already for the top-left corner. */
12634 if (! ((flags & XNegative) || (flags & YNegative)))
12635 return;
12637 #ifdef USE_X_TOOLKIT
12638 this_window = XtWindow (f->output_data.x->widget);
12639 #else
12640 this_window = FRAME_X_WINDOW (f);
12641 #endif
12643 /* Find the position of the outside upper-left corner of
12644 the inner window, with respect to the outer window.
12645 But do this only if we will need the results. */
12646 if (f->output_data.x->parent_desc != FRAME_X_DISPLAY_INFO (f)->root_window)
12648 int count;
12650 BLOCK_INPUT;
12651 count = x_catch_errors (FRAME_X_DISPLAY (f));
12652 while (1)
12654 x_clear_errors (FRAME_X_DISPLAY (f));
12655 XTranslateCoordinates (FRAME_X_DISPLAY (f),
12657 /* From-window, to-window. */
12658 this_window,
12659 f->output_data.x->parent_desc,
12661 /* From-position, to-position. */
12662 0, 0, &win_x, &win_y,
12664 /* Child of win. */
12665 &child);
12666 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
12668 Window newroot, newparent = 0xdeadbeef;
12669 Window *newchildren;
12670 unsigned int nchildren;
12672 if (! XQueryTree (FRAME_X_DISPLAY (f), this_window, &newroot,
12673 &newparent, &newchildren, &nchildren))
12674 break;
12676 XFree ((char *) newchildren);
12678 f->output_data.x->parent_desc = newparent;
12680 else
12681 break;
12684 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
12685 UNBLOCK_INPUT;
12688 /* Treat negative positions as relative to the leftmost bottommost
12689 position that fits on the screen. */
12690 if (flags & XNegative)
12691 f->output_data.x->left_pos = (FRAME_X_DISPLAY_INFO (f)->width
12692 - 2 * f->output_data.x->border_width - win_x
12693 - PIXEL_WIDTH (f)
12694 + f->output_data.x->left_pos);
12697 int height = PIXEL_HEIGHT (f);
12699 #if defined USE_X_TOOLKIT && defined USE_MOTIF
12700 /* Something is fishy here. When using Motif, starting Emacs with
12701 `-g -0-0', the frame appears too low by a few pixels.
12703 This seems to be so because initially, while Emacs is starting,
12704 the column widget's height and the frame's pixel height are
12705 different. The column widget's height is the right one. In
12706 later invocations, when Emacs is up, the frame's pixel height
12707 is right, though.
12709 It's not obvious where the initial small difference comes from.
12710 2000-12-01, gerd. */
12712 XtVaGetValues (f->output_data.x->column_widget, XtNheight, &height, NULL);
12713 #endif
12715 if (flags & YNegative)
12716 f->output_data.x->top_pos = (FRAME_X_DISPLAY_INFO (f)->height
12717 - 2 * f->output_data.x->border_width
12718 - win_y
12719 - height
12720 + f->output_data.x->top_pos);
12723 /* The left_pos and top_pos
12724 are now relative to the top and left screen edges,
12725 so the flags should correspond. */
12726 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12729 /* CHANGE_GRAVITY is 1 when calling from Fset_frame_position,
12730 to really change the position, and 0 when calling from
12731 x_make_frame_visible (in that case, XOFF and YOFF are the current
12732 position values). It is -1 when calling from x_set_frame_parameters,
12733 which means, do adjust for borders but don't change the gravity. */
12735 void
12736 x_set_offset (f, xoff, yoff, change_gravity)
12737 struct frame *f;
12738 register int xoff, yoff;
12739 int change_gravity;
12741 int modified_top, modified_left;
12743 if (change_gravity > 0)
12745 f->output_data.x->top_pos = yoff;
12746 f->output_data.x->left_pos = xoff;
12747 f->output_data.x->size_hint_flags &= ~ (XNegative | YNegative);
12748 if (xoff < 0)
12749 f->output_data.x->size_hint_flags |= XNegative;
12750 if (yoff < 0)
12751 f->output_data.x->size_hint_flags |= YNegative;
12752 f->output_data.x->win_gravity = NorthWestGravity;
12754 x_calc_absolute_position (f);
12756 BLOCK_INPUT;
12757 x_wm_set_size_hint (f, (long) 0, 0);
12759 modified_left = f->output_data.x->left_pos;
12760 modified_top = f->output_data.x->top_pos;
12761 #if 0 /* Running on psilocin (Debian), and displaying on the NCD X-terminal,
12762 this seems to be unnecessary and incorrect. rms, 4/17/97. */
12763 /* It is a mystery why we need to add the border_width here
12764 when the frame is already visible, but experiment says we do. */
12765 if (change_gravity != 0)
12767 modified_left += f->output_data.x->border_width;
12768 modified_top += f->output_data.x->border_width;
12770 #endif
12772 #ifdef USE_X_TOOLKIT
12773 XMoveWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget),
12774 modified_left, modified_top);
12775 #else /* not USE_X_TOOLKIT */
12776 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12777 modified_left, modified_top);
12778 #endif /* not USE_X_TOOLKIT */
12779 UNBLOCK_INPUT;
12782 /* Check if we need to resize the frame due to a fullscreen request.
12783 If so needed, resize the frame. */
12784 static void
12785 x_check_fullscreen (f)
12786 struct frame *f;
12788 if (f->output_data.x->want_fullscreen & FULLSCREEN_BOTH)
12790 int width, height, ign;
12792 x_real_positions (f, &f->output_data.x->left_pos,
12793 &f->output_data.x->top_pos);
12795 x_fullscreen_adjust (f, &width, &height, &ign, &ign);
12797 /* We do not need to move the window, it shall be taken care of
12798 when setting WM manager hints.
12799 If the frame is visible already, the position is checked by
12800 x_check_fullscreen_move. */
12801 if (f->width != width || f->height != height)
12803 change_frame_size (f, height, width, 0, 1, 0);
12804 SET_FRAME_GARBAGED (f);
12805 cancel_mouse_face (f);
12807 /* Wait for the change of frame size to occur */
12808 f->output_data.x->want_fullscreen |= FULLSCREEN_WAIT;
12814 /* If frame parameters are set after the frame is mapped, we need to move
12815 the window. This is done in xfns.c.
12816 Some window managers moves the window to the right position, some
12817 moves the outer window manager window to the specified position.
12818 Here we check that we are in the right spot. If not, make a second
12819 move, assuming we are dealing with the second kind of window manager. */
12820 static void
12821 x_check_fullscreen_move (f)
12822 struct frame *f;
12824 if (f->output_data.x->want_fullscreen & FULLSCREEN_MOVE_WAIT)
12826 int expect_top = f->output_data.x->top_pos;
12827 int expect_left = f->output_data.x->left_pos;
12829 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12830 expect_top = 0;
12831 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12832 expect_left = 0;
12834 if (expect_top != f->output_data.x->top_pos
12835 || expect_left != f->output_data.x->left_pos)
12836 x_set_offset (f, expect_left, expect_top, 1);
12838 /* Just do this once */
12839 f->output_data.x->want_fullscreen &= ~FULLSCREEN_MOVE_WAIT;
12844 /* Calculate fullscreen size. Return in *TOP_POS and *LEFT_POS the
12845 wanted positions of the WM window (not emacs window).
12846 Return in *WIDTH and *HEIGHT the wanted width and height of Emacs
12847 window (FRAME_X_WINDOW).
12849 void
12850 x_fullscreen_adjust (f, width, height, top_pos, left_pos)
12851 struct frame *f;
12852 int *width;
12853 int *height;
12854 int *top_pos;
12855 int *left_pos;
12857 int newwidth = f->width, newheight = f->height;
12859 *top_pos = f->output_data.x->top_pos;
12860 *left_pos = f->output_data.x->left_pos;
12862 if (f->output_data.x->want_fullscreen & FULLSCREEN_HEIGHT)
12864 int ph;
12866 ph = FRAME_X_DISPLAY_INFO (f)->height;
12867 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12868 ph = CHAR_TO_PIXEL_HEIGHT (f, newheight)
12869 - f->output_data.x->y_pixels_diff;
12870 newheight = PIXEL_TO_CHAR_HEIGHT (f, ph);
12871 *top_pos = 0;
12874 if (f->output_data.x->want_fullscreen & FULLSCREEN_WIDTH)
12876 int pw;
12878 pw = FRAME_X_DISPLAY_INFO (f)->width;
12879 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12880 pw = CHAR_TO_PIXEL_WIDTH (f, newwidth)
12881 - f->output_data.x->x_pixels_diff;
12882 newwidth = PIXEL_TO_CHAR_WIDTH (f, pw);
12883 *left_pos = 0;
12886 *width = newwidth;
12887 *height = newheight;
12891 /* Change the size of frame F's X window to COLS/ROWS in the case F
12892 doesn't have a widget. If CHANGE_GRAVITY is 1, we change to
12893 top-left-corner window gravity for this size change and subsequent
12894 size changes. Otherwise we leave the window gravity unchanged. */
12896 static void
12897 x_set_window_size_1 (f, change_gravity, cols, rows)
12898 struct frame *f;
12899 int change_gravity;
12900 int cols, rows;
12902 int pixelwidth, pixelheight;
12904 check_frame_size (f, &rows, &cols);
12905 f->output_data.x->vertical_scroll_bar_extra
12906 = (!FRAME_HAS_VERTICAL_SCROLL_BARS (f)
12908 : FRAME_SCROLL_BAR_PIXEL_WIDTH (f) > 0
12909 ? FRAME_SCROLL_BAR_PIXEL_WIDTH (f)
12910 : (FRAME_SCROLL_BAR_COLS (f) * FONT_WIDTH (f->output_data.x->font)));
12912 x_compute_fringe_widths (f, 0);
12914 pixelwidth = CHAR_TO_PIXEL_WIDTH (f, cols);
12915 pixelheight = CHAR_TO_PIXEL_HEIGHT (f, rows);
12917 f->output_data.x->win_gravity = NorthWestGravity;
12918 x_wm_set_size_hint (f, (long) 0, 0);
12920 XSync (FRAME_X_DISPLAY (f), False);
12921 XResizeWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
12922 pixelwidth, pixelheight);
12924 /* Now, strictly speaking, we can't be sure that this is accurate,
12925 but the window manager will get around to dealing with the size
12926 change request eventually, and we'll hear how it went when the
12927 ConfigureNotify event gets here.
12929 We could just not bother storing any of this information here,
12930 and let the ConfigureNotify event set everything up, but that
12931 might be kind of confusing to the Lisp code, since size changes
12932 wouldn't be reported in the frame parameters until some random
12933 point in the future when the ConfigureNotify event arrives.
12935 We pass 1 for DELAY since we can't run Lisp code inside of
12936 a BLOCK_INPUT. */
12937 change_frame_size (f, rows, cols, 0, 1, 0);
12938 PIXEL_WIDTH (f) = pixelwidth;
12939 PIXEL_HEIGHT (f) = pixelheight;
12941 /* We've set {FRAME,PIXEL}_{WIDTH,HEIGHT} to the values we hope to
12942 receive in the ConfigureNotify event; if we get what we asked
12943 for, then the event won't cause the screen to become garbaged, so
12944 we have to make sure to do it here. */
12945 SET_FRAME_GARBAGED (f);
12947 XFlush (FRAME_X_DISPLAY (f));
12951 /* Call this to change the size of frame F's x-window.
12952 If CHANGE_GRAVITY is 1, we change to top-left-corner window gravity
12953 for this size change and subsequent size changes.
12954 Otherwise we leave the window gravity unchanged. */
12956 void
12957 x_set_window_size (f, change_gravity, cols, rows)
12958 struct frame *f;
12959 int change_gravity;
12960 int cols, rows;
12962 BLOCK_INPUT;
12964 #ifdef USE_X_TOOLKIT
12966 if (f->output_data.x->widget != NULL)
12968 /* The x and y position of the widget is clobbered by the
12969 call to XtSetValues within EmacsFrameSetCharSize.
12970 This is a real kludge, but I don't understand Xt so I can't
12971 figure out a correct fix. Can anyone else tell me? -- rms. */
12972 int xpos = f->output_data.x->widget->core.x;
12973 int ypos = f->output_data.x->widget->core.y;
12974 EmacsFrameSetCharSize (f->output_data.x->edit_widget, cols, rows);
12975 f->output_data.x->widget->core.x = xpos;
12976 f->output_data.x->widget->core.y = ypos;
12978 else
12979 x_set_window_size_1 (f, change_gravity, cols, rows);
12981 #else /* not USE_X_TOOLKIT */
12983 x_set_window_size_1 (f, change_gravity, cols, rows);
12985 #endif /* not USE_X_TOOLKIT */
12987 /* If cursor was outside the new size, mark it as off. */
12988 mark_window_cursors_off (XWINDOW (f->root_window));
12990 /* Clear out any recollection of where the mouse highlighting was,
12991 since it might be in a place that's outside the new frame size.
12992 Actually checking whether it is outside is a pain in the neck,
12993 so don't try--just let the highlighting be done afresh with new size. */
12994 cancel_mouse_face (f);
12996 UNBLOCK_INPUT;
12999 /* Mouse warping. */
13001 void
13002 x_set_mouse_position (f, x, y)
13003 struct frame *f;
13004 int x, y;
13006 int pix_x, pix_y;
13008 pix_x = CHAR_TO_PIXEL_COL (f, x) + FONT_WIDTH (f->output_data.x->font) / 2;
13009 pix_y = CHAR_TO_PIXEL_ROW (f, y) + f->output_data.x->line_height / 2;
13011 if (pix_x < 0) pix_x = 0;
13012 if (pix_x > PIXEL_WIDTH (f)) pix_x = PIXEL_WIDTH (f);
13014 if (pix_y < 0) pix_y = 0;
13015 if (pix_y > PIXEL_HEIGHT (f)) pix_y = PIXEL_HEIGHT (f);
13017 BLOCK_INPUT;
13019 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13020 0, 0, 0, 0, pix_x, pix_y);
13021 UNBLOCK_INPUT;
13024 /* Move the mouse to position pixel PIX_X, PIX_Y relative to frame F. */
13026 void
13027 x_set_mouse_pixel_position (f, pix_x, pix_y)
13028 struct frame *f;
13029 int pix_x, pix_y;
13031 BLOCK_INPUT;
13033 XWarpPointer (FRAME_X_DISPLAY (f), None, FRAME_X_WINDOW (f),
13034 0, 0, 0, 0, pix_x, pix_y);
13035 UNBLOCK_INPUT;
13038 /* focus shifting, raising and lowering. */
13040 void
13041 x_focus_on_frame (f)
13042 struct frame *f;
13044 #if 0 /* This proves to be unpleasant. */
13045 x_raise_frame (f);
13046 #endif
13047 #if 0
13048 /* I don't think that the ICCCM allows programs to do things like this
13049 without the interaction of the window manager. Whatever you end up
13050 doing with this code, do it to x_unfocus_frame too. */
13051 XSetInputFocus (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
13052 RevertToPointerRoot, CurrentTime);
13053 #endif /* ! 0 */
13056 void
13057 x_unfocus_frame (f)
13058 struct frame *f;
13060 #if 0
13061 /* Look at the remarks in x_focus_on_frame. */
13062 if (FRAME_X_DISPLAY_INFO (f)->x_focus_frame == f)
13063 XSetInputFocus (FRAME_X_DISPLAY (f), PointerRoot,
13064 RevertToPointerRoot, CurrentTime);
13065 #endif /* ! 0 */
13068 /* Raise frame F. */
13070 void
13071 x_raise_frame (f)
13072 struct frame *f;
13074 if (f->async_visible)
13076 BLOCK_INPUT;
13077 #ifdef USE_X_TOOLKIT
13078 XRaiseWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13079 #else /* not USE_X_TOOLKIT */
13080 XRaiseWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13081 #endif /* not USE_X_TOOLKIT */
13082 XFlush (FRAME_X_DISPLAY (f));
13083 UNBLOCK_INPUT;
13087 /* Lower frame F. */
13089 void
13090 x_lower_frame (f)
13091 struct frame *f;
13093 if (f->async_visible)
13095 BLOCK_INPUT;
13096 #ifdef USE_X_TOOLKIT
13097 XLowerWindow (FRAME_X_DISPLAY (f), XtWindow (f->output_data.x->widget));
13098 #else /* not USE_X_TOOLKIT */
13099 XLowerWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13100 #endif /* not USE_X_TOOLKIT */
13101 XFlush (FRAME_X_DISPLAY (f));
13102 UNBLOCK_INPUT;
13106 static void
13107 XTframe_raise_lower (f, raise_flag)
13108 FRAME_PTR f;
13109 int raise_flag;
13111 if (raise_flag)
13112 x_raise_frame (f);
13113 else
13114 x_lower_frame (f);
13117 /* Change of visibility. */
13119 /* This tries to wait until the frame is really visible.
13120 However, if the window manager asks the user where to position
13121 the frame, this will return before the user finishes doing that.
13122 The frame will not actually be visible at that time,
13123 but it will become visible later when the window manager
13124 finishes with it. */
13126 void
13127 x_make_frame_visible (f)
13128 struct frame *f;
13130 Lisp_Object type;
13131 int original_top, original_left;
13132 int retry_count = 2;
13134 retry:
13136 BLOCK_INPUT;
13138 type = x_icon_type (f);
13139 if (!NILP (type))
13140 x_bitmap_icon (f, type);
13142 if (! FRAME_VISIBLE_P (f))
13144 /* We test FRAME_GARBAGED_P here to make sure we don't
13145 call x_set_offset a second time
13146 if we get to x_make_frame_visible a second time
13147 before the window gets really visible. */
13148 if (! FRAME_ICONIFIED_P (f)
13149 && ! f->output_data.x->asked_for_visible)
13150 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13152 f->output_data.x->asked_for_visible = 1;
13154 if (! EQ (Vx_no_window_manager, Qt))
13155 x_wm_set_window_state (f, NormalState);
13156 #ifdef USE_X_TOOLKIT
13157 /* This was XtPopup, but that did nothing for an iconified frame. */
13158 XtMapWidget (f->output_data.x->widget);
13159 #else /* not USE_X_TOOLKIT */
13160 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13161 #endif /* not USE_X_TOOLKIT */
13162 #if 0 /* This seems to bring back scroll bars in the wrong places
13163 if the window configuration has changed. They seem
13164 to come back ok without this. */
13165 if (FRAME_HAS_VERTICAL_SCROLL_BARS (f))
13166 XMapSubwindows (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13167 #endif
13170 XFlush (FRAME_X_DISPLAY (f));
13172 /* Synchronize to ensure Emacs knows the frame is visible
13173 before we do anything else. We do this loop with input not blocked
13174 so that incoming events are handled. */
13176 Lisp_Object frame;
13177 int count;
13178 /* This must be before UNBLOCK_INPUT
13179 since events that arrive in response to the actions above
13180 will set it when they are handled. */
13181 int previously_visible = f->output_data.x->has_been_visible;
13183 original_left = f->output_data.x->left_pos;
13184 original_top = f->output_data.x->top_pos;
13186 /* This must come after we set COUNT. */
13187 UNBLOCK_INPUT;
13189 /* We unblock here so that arriving X events are processed. */
13191 /* Now move the window back to where it was "supposed to be".
13192 But don't do it if the gravity is negative.
13193 When the gravity is negative, this uses a position
13194 that is 3 pixels too low. Perhaps that's really the border width.
13196 Don't do this if the window has never been visible before,
13197 because the window manager may choose the position
13198 and we don't want to override it. */
13200 if (! FRAME_VISIBLE_P (f) && ! FRAME_ICONIFIED_P (f)
13201 && f->output_data.x->win_gravity == NorthWestGravity
13202 && previously_visible)
13204 Drawable rootw;
13205 int x, y;
13206 unsigned int width, height, border, depth;
13208 BLOCK_INPUT;
13210 /* On some window managers (such as FVWM) moving an existing
13211 window, even to the same place, causes the window manager
13212 to introduce an offset. This can cause the window to move
13213 to an unexpected location. Check the geometry (a little
13214 slow here) and then verify that the window is in the right
13215 place. If the window is not in the right place, move it
13216 there, and take the potential window manager hit. */
13217 XGetGeometry (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13218 &rootw, &x, &y, &width, &height, &border, &depth);
13220 if (original_left != x || original_top != y)
13221 XMoveWindow (FRAME_X_DISPLAY (f), FRAME_OUTER_WINDOW (f),
13222 original_left, original_top);
13224 UNBLOCK_INPUT;
13227 XSETFRAME (frame, f);
13229 /* Wait until the frame is visible. Process X events until a
13230 MapNotify event has been seen, or until we think we won't get a
13231 MapNotify at all.. */
13232 for (count = input_signal_count + 10;
13233 input_signal_count < count && !FRAME_VISIBLE_P (f);)
13235 /* Force processing of queued events. */
13236 x_sync (f);
13238 /* Machines that do polling rather than SIGIO have been
13239 observed to go into a busy-wait here. So we'll fake an
13240 alarm signal to let the handler know that there's something
13241 to be read. We used to raise a real alarm, but it seems
13242 that the handler isn't always enabled here. This is
13243 probably a bug. */
13244 if (input_polling_used ())
13246 /* It could be confusing if a real alarm arrives while
13247 processing the fake one. Turn it off and let the
13248 handler reset it. */
13249 extern void poll_for_input_1 P_ ((void));
13250 int old_poll_suppress_count = poll_suppress_count;
13251 poll_suppress_count = 1;
13252 poll_for_input_1 ();
13253 poll_suppress_count = old_poll_suppress_count;
13256 /* See if a MapNotify event has been processed. */
13257 FRAME_SAMPLE_VISIBILITY (f);
13260 /* 2000-09-28: In
13262 (let ((f (selected-frame)))
13263 (iconify-frame f)
13264 (raise-frame f))
13266 the frame is not raised with various window managers on
13267 FreeBSD, Linux and Solaris. It turns out that, for some
13268 unknown reason, the call to XtMapWidget is completely ignored.
13269 Mapping the widget a second time works. */
13271 if (!FRAME_VISIBLE_P (f) && --retry_count > 0)
13272 goto retry;
13276 /* Change from mapped state to withdrawn state. */
13278 /* Make the frame visible (mapped and not iconified). */
13280 void
13281 x_make_frame_invisible (f)
13282 struct frame *f;
13284 Window window;
13286 #ifdef USE_X_TOOLKIT
13287 /* Use the frame's outermost window, not the one we normally draw on. */
13288 window = XtWindow (f->output_data.x->widget);
13289 #else /* not USE_X_TOOLKIT */
13290 window = FRAME_X_WINDOW (f);
13291 #endif /* not USE_X_TOOLKIT */
13293 /* Don't keep the highlight on an invisible frame. */
13294 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13295 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13297 #if 0/* This might add unreliability; I don't trust it -- rms. */
13298 if (! f->async_visible && ! f->async_iconified)
13299 return;
13300 #endif
13302 BLOCK_INPUT;
13304 /* Before unmapping the window, update the WM_SIZE_HINTS property to claim
13305 that the current position of the window is user-specified, rather than
13306 program-specified, so that when the window is mapped again, it will be
13307 placed at the same location, without forcing the user to position it
13308 by hand again (they have already done that once for this window.) */
13309 x_wm_set_size_hint (f, (long) 0, 1);
13311 #ifdef HAVE_X11R4
13313 if (! XWithdrawWindow (FRAME_X_DISPLAY (f), window,
13314 DefaultScreen (FRAME_X_DISPLAY (f))))
13316 UNBLOCK_INPUT_RESIGNAL;
13317 error ("Can't notify window manager of window withdrawal");
13319 #else /* ! defined (HAVE_X11R4) */
13321 /* Tell the window manager what we're going to do. */
13322 if (! EQ (Vx_no_window_manager, Qt))
13324 XEvent unmap;
13326 unmap.xunmap.type = UnmapNotify;
13327 unmap.xunmap.window = window;
13328 unmap.xunmap.event = DefaultRootWindow (FRAME_X_DISPLAY (f));
13329 unmap.xunmap.from_configure = False;
13330 if (! XSendEvent (FRAME_X_DISPLAY (f),
13331 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13332 False,
13333 SubstructureRedirectMaskSubstructureNotifyMask,
13334 &unmap))
13336 UNBLOCK_INPUT_RESIGNAL;
13337 error ("Can't notify window manager of withdrawal");
13341 /* Unmap the window ourselves. Cheeky! */
13342 XUnmapWindow (FRAME_X_DISPLAY (f), window);
13343 #endif /* ! defined (HAVE_X11R4) */
13345 /* We can't distinguish this from iconification
13346 just by the event that we get from the server.
13347 So we can't win using the usual strategy of letting
13348 FRAME_SAMPLE_VISIBILITY set this. So do it by hand,
13349 and synchronize with the server to make sure we agree. */
13350 f->visible = 0;
13351 FRAME_ICONIFIED_P (f) = 0;
13352 f->async_visible = 0;
13353 f->async_iconified = 0;
13355 x_sync (f);
13357 UNBLOCK_INPUT;
13360 /* Change window state from mapped to iconified. */
13362 void
13363 x_iconify_frame (f)
13364 struct frame *f;
13366 int result;
13367 Lisp_Object type;
13369 /* Don't keep the highlight on an invisible frame. */
13370 if (FRAME_X_DISPLAY_INFO (f)->x_highlight_frame == f)
13371 FRAME_X_DISPLAY_INFO (f)->x_highlight_frame = 0;
13373 if (f->async_iconified)
13374 return;
13376 BLOCK_INPUT;
13378 FRAME_SAMPLE_VISIBILITY (f);
13380 type = x_icon_type (f);
13381 if (!NILP (type))
13382 x_bitmap_icon (f, type);
13384 #ifdef USE_X_TOOLKIT
13386 if (! FRAME_VISIBLE_P (f))
13388 if (! EQ (Vx_no_window_manager, Qt))
13389 x_wm_set_window_state (f, IconicState);
13390 /* This was XtPopup, but that did nothing for an iconified frame. */
13391 XtMapWidget (f->output_data.x->widget);
13392 /* The server won't give us any event to indicate
13393 that an invisible frame was changed to an icon,
13394 so we have to record it here. */
13395 f->iconified = 1;
13396 f->visible = 1;
13397 f->async_iconified = 1;
13398 f->async_visible = 0;
13399 UNBLOCK_INPUT;
13400 return;
13403 result = XIconifyWindow (FRAME_X_DISPLAY (f),
13404 XtWindow (f->output_data.x->widget),
13405 DefaultScreen (FRAME_X_DISPLAY (f)));
13406 UNBLOCK_INPUT;
13408 if (!result)
13409 error ("Can't notify window manager of iconification");
13411 f->async_iconified = 1;
13412 f->async_visible = 0;
13415 BLOCK_INPUT;
13416 XFlush (FRAME_X_DISPLAY (f));
13417 UNBLOCK_INPUT;
13418 #else /* not USE_X_TOOLKIT */
13420 /* Make sure the X server knows where the window should be positioned,
13421 in case the user deiconifies with the window manager. */
13422 if (! FRAME_VISIBLE_P (f) && !FRAME_ICONIFIED_P (f))
13423 x_set_offset (f, f->output_data.x->left_pos, f->output_data.x->top_pos, 0);
13425 /* Since we don't know which revision of X we're running, we'll use both
13426 the X11R3 and X11R4 techniques. I don't know if this is a good idea. */
13428 /* X11R4: send a ClientMessage to the window manager using the
13429 WM_CHANGE_STATE type. */
13431 XEvent message;
13433 message.xclient.window = FRAME_X_WINDOW (f);
13434 message.xclient.type = ClientMessage;
13435 message.xclient.message_type = FRAME_X_DISPLAY_INFO (f)->Xatom_wm_change_state;
13436 message.xclient.format = 32;
13437 message.xclient.data.l[0] = IconicState;
13439 if (! XSendEvent (FRAME_X_DISPLAY (f),
13440 DefaultRootWindow (FRAME_X_DISPLAY (f)),
13441 False,
13442 SubstructureRedirectMask | SubstructureNotifyMask,
13443 &message))
13445 UNBLOCK_INPUT_RESIGNAL;
13446 error ("Can't notify window manager of iconification");
13450 /* X11R3: set the initial_state field of the window manager hints to
13451 IconicState. */
13452 x_wm_set_window_state (f, IconicState);
13454 if (!FRAME_VISIBLE_P (f))
13456 /* If the frame was withdrawn, before, we must map it. */
13457 XMapRaised (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13460 f->async_iconified = 1;
13461 f->async_visible = 0;
13463 XFlush (FRAME_X_DISPLAY (f));
13464 UNBLOCK_INPUT;
13465 #endif /* not USE_X_TOOLKIT */
13469 /* Free X resources of frame F. */
13471 void
13472 x_free_frame_resources (f)
13473 struct frame *f;
13475 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13476 Lisp_Object bar;
13477 struct scroll_bar *b;
13479 BLOCK_INPUT;
13481 /* If a display connection is dead, don't try sending more
13482 commands to the X server. */
13483 if (dpyinfo->display)
13485 if (f->output_data.x->icon_desc)
13486 XDestroyWindow (FRAME_X_DISPLAY (f), f->output_data.x->icon_desc);
13488 #ifdef USE_X_TOOLKIT
13489 /* Explicitly destroy the scroll bars of the frame. Without
13490 this, we get "BadDrawable" errors from the toolkit later on,
13491 presumably from expose events generated for the disappearing
13492 toolkit scroll bars. */
13493 for (bar = FRAME_SCROLL_BARS (f); !NILP (bar); bar = b->next)
13495 b = XSCROLL_BAR (bar);
13496 x_scroll_bar_remove (b);
13498 #endif
13500 #ifdef HAVE_X_I18N
13501 if (FRAME_XIC (f))
13502 free_frame_xic (f);
13503 #endif
13505 #ifdef USE_X_TOOLKIT
13506 if (f->output_data.x->widget)
13508 XtDestroyWidget (f->output_data.x->widget);
13509 f->output_data.x->widget = NULL;
13511 /* Tooltips don't have widgets, only a simple X window, even if
13512 we are using a toolkit. */
13513 else if (FRAME_X_WINDOW (f))
13514 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13516 free_frame_menubar (f);
13517 #else /* !USE_X_TOOLKIT */
13518 if (FRAME_X_WINDOW (f))
13519 XDestroyWindow (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f));
13520 #endif /* !USE_X_TOOLKIT */
13522 unload_color (f, f->output_data.x->foreground_pixel);
13523 unload_color (f, f->output_data.x->background_pixel);
13524 unload_color (f, f->output_data.x->cursor_pixel);
13525 unload_color (f, f->output_data.x->cursor_foreground_pixel);
13526 unload_color (f, f->output_data.x->border_pixel);
13527 unload_color (f, f->output_data.x->mouse_pixel);
13529 if (f->output_data.x->scroll_bar_background_pixel != -1)
13530 unload_color (f, f->output_data.x->scroll_bar_background_pixel);
13531 if (f->output_data.x->scroll_bar_foreground_pixel != -1)
13532 unload_color (f, f->output_data.x->scroll_bar_foreground_pixel);
13533 #ifdef USE_TOOLKIT_SCROLL_BARS
13534 /* Scrollbar shadow colors. */
13535 if (f->output_data.x->scroll_bar_top_shadow_pixel != -1)
13536 unload_color (f, f->output_data.x->scroll_bar_top_shadow_pixel);
13537 if (f->output_data.x->scroll_bar_bottom_shadow_pixel != -1)
13538 unload_color (f, f->output_data.x->scroll_bar_bottom_shadow_pixel);
13539 #endif /* USE_TOOLKIT_SCROLL_BARS */
13540 if (f->output_data.x->white_relief.allocated_p)
13541 unload_color (f, f->output_data.x->white_relief.pixel);
13542 if (f->output_data.x->black_relief.allocated_p)
13543 unload_color (f, f->output_data.x->black_relief.pixel);
13545 if (FRAME_FACE_CACHE (f))
13546 free_frame_faces (f);
13548 x_free_gcs (f);
13549 XFlush (FRAME_X_DISPLAY (f));
13552 if (f->output_data.x->saved_menu_event)
13553 xfree (f->output_data.x->saved_menu_event);
13555 xfree (f->output_data.x);
13556 f->output_data.x = NULL;
13558 if (f == dpyinfo->x_focus_frame)
13559 dpyinfo->x_focus_frame = 0;
13560 if (f == dpyinfo->x_focus_event_frame)
13561 dpyinfo->x_focus_event_frame = 0;
13562 if (f == dpyinfo->x_highlight_frame)
13563 dpyinfo->x_highlight_frame = 0;
13565 if (f == dpyinfo->mouse_face_mouse_frame)
13567 dpyinfo->mouse_face_beg_row
13568 = dpyinfo->mouse_face_beg_col = -1;
13569 dpyinfo->mouse_face_end_row
13570 = dpyinfo->mouse_face_end_col = -1;
13571 dpyinfo->mouse_face_window = Qnil;
13572 dpyinfo->mouse_face_deferred_gc = 0;
13573 dpyinfo->mouse_face_mouse_frame = 0;
13576 UNBLOCK_INPUT;
13580 /* Destroy the X window of frame F. */
13582 void
13583 x_destroy_window (f)
13584 struct frame *f;
13586 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
13588 /* If a display connection is dead, don't try sending more
13589 commands to the X server. */
13590 if (dpyinfo->display != 0)
13591 x_free_frame_resources (f);
13593 dpyinfo->reference_count--;
13597 /* Setting window manager hints. */
13599 /* Set the normal size hints for the window manager, for frame F.
13600 FLAGS is the flags word to use--or 0 meaning preserve the flags
13601 that the window now has.
13602 If USER_POSITION is nonzero, we set the USPosition
13603 flag (this is useful when FLAGS is 0). */
13605 void
13606 x_wm_set_size_hint (f, flags, user_position)
13607 struct frame *f;
13608 long flags;
13609 int user_position;
13611 XSizeHints size_hints;
13613 #ifdef USE_X_TOOLKIT
13614 Arg al[2];
13615 int ac = 0;
13616 Dimension widget_width, widget_height;
13617 Window window = XtWindow (f->output_data.x->widget);
13618 #else /* not USE_X_TOOLKIT */
13619 Window window = FRAME_X_WINDOW (f);
13620 #endif /* not USE_X_TOOLKIT */
13622 /* Setting PMaxSize caused various problems. */
13623 size_hints.flags = PResizeInc | PMinSize /* | PMaxSize */;
13625 size_hints.x = f->output_data.x->left_pos;
13626 size_hints.y = f->output_data.x->top_pos;
13628 #ifdef USE_X_TOOLKIT
13629 XtSetArg (al[ac], XtNwidth, &widget_width); ac++;
13630 XtSetArg (al[ac], XtNheight, &widget_height); ac++;
13631 XtGetValues (f->output_data.x->widget, al, ac);
13632 size_hints.height = widget_height;
13633 size_hints.width = widget_width;
13634 #else /* not USE_X_TOOLKIT */
13635 size_hints.height = PIXEL_HEIGHT (f);
13636 size_hints.width = PIXEL_WIDTH (f);
13637 #endif /* not USE_X_TOOLKIT */
13639 size_hints.width_inc = FONT_WIDTH (f->output_data.x->font);
13640 size_hints.height_inc = f->output_data.x->line_height;
13641 size_hints.max_width
13642 = FRAME_X_DISPLAY_INFO (f)->width - CHAR_TO_PIXEL_WIDTH (f, 0);
13643 size_hints.max_height
13644 = FRAME_X_DISPLAY_INFO (f)->height - CHAR_TO_PIXEL_HEIGHT (f, 0);
13646 /* Calculate the base and minimum sizes.
13648 (When we use the X toolkit, we don't do it here.
13649 Instead we copy the values that the widgets are using, below.) */
13650 #ifndef USE_X_TOOLKIT
13652 int base_width, base_height;
13653 int min_rows = 0, min_cols = 0;
13655 base_width = CHAR_TO_PIXEL_WIDTH (f, 0);
13656 base_height = CHAR_TO_PIXEL_HEIGHT (f, 0);
13658 check_frame_size (f, &min_rows, &min_cols);
13660 /* The window manager uses the base width hints to calculate the
13661 current number of rows and columns in the frame while
13662 resizing; min_width and min_height aren't useful for this
13663 purpose, since they might not give the dimensions for a
13664 zero-row, zero-column frame.
13666 We use the base_width and base_height members if we have
13667 them; otherwise, we set the min_width and min_height members
13668 to the size for a zero x zero frame. */
13670 #ifdef HAVE_X11R4
13671 size_hints.flags |= PBaseSize;
13672 size_hints.base_width = base_width;
13673 size_hints.base_height = base_height;
13674 size_hints.min_width = base_width + min_cols * size_hints.width_inc;
13675 size_hints.min_height = base_height + min_rows * size_hints.height_inc;
13676 #else
13677 size_hints.min_width = base_width;
13678 size_hints.min_height = base_height;
13679 #endif
13682 /* If we don't need the old flags, we don't need the old hint at all. */
13683 if (flags)
13685 size_hints.flags |= flags;
13686 goto no_read;
13688 #endif /* not USE_X_TOOLKIT */
13691 XSizeHints hints; /* Sometimes I hate X Windows... */
13692 long supplied_return;
13693 int value;
13695 #ifdef HAVE_X11R4
13696 value = XGetWMNormalHints (FRAME_X_DISPLAY (f), window, &hints,
13697 &supplied_return);
13698 #else
13699 value = XGetNormalHints (FRAME_X_DISPLAY (f), window, &hints);
13700 #endif
13702 #ifdef USE_X_TOOLKIT
13703 size_hints.base_height = hints.base_height;
13704 size_hints.base_width = hints.base_width;
13705 size_hints.min_height = hints.min_height;
13706 size_hints.min_width = hints.min_width;
13707 #endif
13709 if (flags)
13710 size_hints.flags |= flags;
13711 else
13713 if (value == 0)
13714 hints.flags = 0;
13715 if (hints.flags & PSize)
13716 size_hints.flags |= PSize;
13717 if (hints.flags & PPosition)
13718 size_hints.flags |= PPosition;
13719 if (hints.flags & USPosition)
13720 size_hints.flags |= USPosition;
13721 if (hints.flags & USSize)
13722 size_hints.flags |= USSize;
13726 #ifndef USE_X_TOOLKIT
13727 no_read:
13728 #endif
13730 #ifdef PWinGravity
13731 size_hints.win_gravity = f->output_data.x->win_gravity;
13732 size_hints.flags |= PWinGravity;
13734 if (user_position)
13736 size_hints.flags &= ~ PPosition;
13737 size_hints.flags |= USPosition;
13739 #endif /* PWinGravity */
13741 #ifdef HAVE_X11R4
13742 XSetWMNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13743 #else
13744 XSetNormalHints (FRAME_X_DISPLAY (f), window, &size_hints);
13745 #endif
13748 /* Used for IconicState or NormalState */
13750 void
13751 x_wm_set_window_state (f, state)
13752 struct frame *f;
13753 int state;
13755 #ifdef USE_X_TOOLKIT
13756 Arg al[1];
13758 XtSetArg (al[0], XtNinitialState, state);
13759 XtSetValues (f->output_data.x->widget, al, 1);
13760 #else /* not USE_X_TOOLKIT */
13761 Window window = FRAME_X_WINDOW (f);
13763 f->output_data.x->wm_hints.flags |= StateHint;
13764 f->output_data.x->wm_hints.initial_state = state;
13766 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13767 #endif /* not USE_X_TOOLKIT */
13770 void
13771 x_wm_set_icon_pixmap (f, pixmap_id)
13772 struct frame *f;
13773 int pixmap_id;
13775 Pixmap icon_pixmap;
13777 #ifndef USE_X_TOOLKIT
13778 Window window = FRAME_X_WINDOW (f);
13779 #endif
13781 if (pixmap_id > 0)
13783 icon_pixmap = x_bitmap_pixmap (f, pixmap_id);
13784 f->output_data.x->wm_hints.icon_pixmap = icon_pixmap;
13786 else
13788 /* It seems there is no way to turn off use of an icon pixmap.
13789 The following line does it, only if no icon has yet been created,
13790 for some window managers. But with mwm it crashes.
13791 Some people say it should clear the IconPixmapHint bit in this case,
13792 but that doesn't work, and the X consortium said it isn't the
13793 right thing at all. Since there is no way to win,
13794 best to explicitly give up. */
13795 #if 0
13796 f->output_data.x->wm_hints.icon_pixmap = None;
13797 #else
13798 return;
13799 #endif
13802 #ifdef USE_X_TOOLKIT /* same as in x_wm_set_window_state. */
13805 Arg al[1];
13806 XtSetArg (al[0], XtNiconPixmap, icon_pixmap);
13807 XtSetValues (f->output_data.x->widget, al, 1);
13810 #else /* not USE_X_TOOLKIT */
13812 f->output_data.x->wm_hints.flags |= IconPixmapHint;
13813 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13815 #endif /* not USE_X_TOOLKIT */
13818 void
13819 x_wm_set_icon_position (f, icon_x, icon_y)
13820 struct frame *f;
13821 int icon_x, icon_y;
13823 #ifdef USE_X_TOOLKIT
13824 Window window = XtWindow (f->output_data.x->widget);
13825 #else
13826 Window window = FRAME_X_WINDOW (f);
13827 #endif
13829 f->output_data.x->wm_hints.flags |= IconPositionHint;
13830 f->output_data.x->wm_hints.icon_x = icon_x;
13831 f->output_data.x->wm_hints.icon_y = icon_y;
13833 XSetWMHints (FRAME_X_DISPLAY (f), window, &f->output_data.x->wm_hints);
13837 /***********************************************************************
13838 Fonts
13839 ***********************************************************************/
13841 /* Return a pointer to struct font_info of font FONT_IDX of frame F. */
13843 struct font_info *
13844 x_get_font_info (f, font_idx)
13845 FRAME_PTR f;
13846 int font_idx;
13848 return (FRAME_X_FONT_TABLE (f) + font_idx);
13852 /* Return a list of names of available fonts matching PATTERN on frame F.
13854 If SIZE is > 0, it is the size (maximum bounds width) of fonts
13855 to be listed.
13857 SIZE < 0 means include scalable fonts.
13859 Frame F null means we have not yet created any frame on X, and
13860 consult the first display in x_display_list. MAXNAMES sets a limit
13861 on how many fonts to match. */
13863 Lisp_Object
13864 x_list_fonts (f, pattern, size, maxnames)
13865 struct frame *f;
13866 Lisp_Object pattern;
13867 int size;
13868 int maxnames;
13870 Lisp_Object list = Qnil, patterns, newlist = Qnil, key = Qnil;
13871 Lisp_Object tem, second_best;
13872 struct x_display_info *dpyinfo
13873 = f ? FRAME_X_DISPLAY_INFO (f) : x_display_list;
13874 Display *dpy = dpyinfo->display;
13875 int try_XLoadQueryFont = 0;
13876 int count;
13877 int allow_scalable_fonts_p = 0;
13879 if (size < 0)
13881 allow_scalable_fonts_p = 1;
13882 size = 0;
13885 patterns = Fassoc (pattern, Valternate_fontname_alist);
13886 if (NILP (patterns))
13887 patterns = Fcons (pattern, Qnil);
13889 if (maxnames == 1 && !size)
13890 /* We can return any single font matching PATTERN. */
13891 try_XLoadQueryFont = 1;
13893 for (; CONSP (patterns); patterns = XCDR (patterns))
13895 int num_fonts;
13896 char **names = NULL;
13898 pattern = XCAR (patterns);
13899 /* See if we cached the result for this particular query.
13900 The cache is an alist of the form:
13901 ((((PATTERN . MAXNAMES) . SCALABLE) (FONTNAME . WIDTH) ...) ...) */
13902 tem = XCDR (dpyinfo->name_list_element);
13903 key = Fcons (Fcons (pattern, make_number (maxnames)),
13904 allow_scalable_fonts_p ? Qt : Qnil);
13905 list = Fassoc (key, tem);
13906 if (!NILP (list))
13908 list = Fcdr_safe (list);
13909 /* We have a cashed list. Don't have to get the list again. */
13910 goto label_cached;
13913 /* At first, put PATTERN in the cache. */
13915 BLOCK_INPUT;
13916 count = x_catch_errors (dpy);
13918 if (try_XLoadQueryFont)
13920 XFontStruct *font;
13921 unsigned long value;
13923 font = XLoadQueryFont (dpy, XSTRING (pattern)->data);
13924 if (x_had_errors_p (dpy))
13926 /* This error is perhaps due to insufficient memory on X
13927 server. Let's just ignore it. */
13928 font = NULL;
13929 x_clear_errors (dpy);
13932 if (font
13933 && XGetFontProperty (font, XA_FONT, &value))
13935 char *name = (char *) XGetAtomName (dpy, (Atom) value);
13936 int len = strlen (name);
13937 char *tmp;
13939 /* If DXPC (a Differential X Protocol Compressor)
13940 Ver.3.7 is running, XGetAtomName will return null
13941 string. We must avoid such a name. */
13942 if (len == 0)
13943 try_XLoadQueryFont = 0;
13944 else
13946 num_fonts = 1;
13947 names = (char **) alloca (sizeof (char *));
13948 /* Some systems only allow alloca assigned to a
13949 simple var. */
13950 tmp = (char *) alloca (len + 1); names[0] = tmp;
13951 bcopy (name, names[0], len + 1);
13952 XFree (name);
13955 else
13956 try_XLoadQueryFont = 0;
13958 if (font)
13959 XFreeFont (dpy, font);
13962 if (!try_XLoadQueryFont)
13964 /* We try at least 10 fonts because XListFonts will return
13965 auto-scaled fonts at the head. */
13966 names = XListFonts (dpy, XSTRING (pattern)->data, max (maxnames, 10),
13967 &num_fonts);
13968 if (x_had_errors_p (dpy))
13970 /* This error is perhaps due to insufficient memory on X
13971 server. Let's just ignore it. */
13972 names = NULL;
13973 x_clear_errors (dpy);
13977 x_uncatch_errors (dpy, count);
13978 UNBLOCK_INPUT;
13980 if (names)
13982 int i;
13984 /* Make a list of all the fonts we got back.
13985 Store that in the font cache for the display. */
13986 for (i = 0; i < num_fonts; i++)
13988 int width = 0;
13989 char *p = names[i];
13990 int average_width = -1, dashes = 0;
13992 /* Count the number of dashes in NAMES[I]. If there are
13993 14 dashes, and the field value following 12th dash
13994 (AVERAGE_WIDTH) is 0, this is a auto-scaled font which
13995 is usually too ugly to be used for editing. Let's
13996 ignore it. */
13997 while (*p)
13998 if (*p++ == '-')
14000 dashes++;
14001 if (dashes == 7) /* PIXEL_SIZE field */
14002 width = atoi (p);
14003 else if (dashes == 12) /* AVERAGE_WIDTH field */
14004 average_width = atoi (p);
14007 if (allow_scalable_fonts_p
14008 || dashes < 14 || average_width != 0)
14010 tem = build_string (names[i]);
14011 if (NILP (Fassoc (tem, list)))
14013 if (STRINGP (Vx_pixel_size_width_font_regexp)
14014 && ((fast_c_string_match_ignore_case
14015 (Vx_pixel_size_width_font_regexp, names[i]))
14016 >= 0))
14017 /* We can set the value of PIXEL_SIZE to the
14018 width of this font. */
14019 list = Fcons (Fcons (tem, make_number (width)), list);
14020 else
14021 /* For the moment, width is not known. */
14022 list = Fcons (Fcons (tem, Qnil), list);
14027 if (!try_XLoadQueryFont)
14029 BLOCK_INPUT;
14030 XFreeFontNames (names);
14031 UNBLOCK_INPUT;
14035 /* Now store the result in the cache. */
14036 XSETCDR (dpyinfo->name_list_element,
14037 Fcons (Fcons (key, list), XCDR (dpyinfo->name_list_element)));
14039 label_cached:
14040 if (NILP (list)) continue; /* Try the remaining alternatives. */
14042 newlist = second_best = Qnil;
14043 /* Make a list of the fonts that have the right width. */
14044 for (; CONSP (list); list = XCDR (list))
14046 int found_size;
14048 tem = XCAR (list);
14050 if (!CONSP (tem) || NILP (XCAR (tem)))
14051 continue;
14052 if (!size)
14054 newlist = Fcons (XCAR (tem), newlist);
14055 continue;
14058 if (!INTEGERP (XCDR (tem)))
14060 /* Since we have not yet known the size of this font, we
14061 must try slow function call XLoadQueryFont. */
14062 XFontStruct *thisinfo;
14064 BLOCK_INPUT;
14065 count = x_catch_errors (dpy);
14066 thisinfo = XLoadQueryFont (dpy,
14067 XSTRING (XCAR (tem))->data);
14068 if (x_had_errors_p (dpy))
14070 /* This error is perhaps due to insufficient memory on X
14071 server. Let's just ignore it. */
14072 thisinfo = NULL;
14073 x_clear_errors (dpy);
14075 x_uncatch_errors (dpy, count);
14076 UNBLOCK_INPUT;
14078 if (thisinfo)
14080 XSETCDR (tem,
14081 (thisinfo->min_bounds.width == 0
14082 ? make_number (0)
14083 : make_number (thisinfo->max_bounds.width)));
14084 BLOCK_INPUT;
14085 XFreeFont (dpy, thisinfo);
14086 UNBLOCK_INPUT;
14088 else
14089 /* For unknown reason, the previous call of XListFont had
14090 returned a font which can't be opened. Record the size
14091 as 0 not to try to open it again. */
14092 XSETCDR (tem, make_number (0));
14095 found_size = XINT (XCDR (tem));
14096 if (found_size == size)
14097 newlist = Fcons (XCAR (tem), newlist);
14098 else if (found_size > 0)
14100 if (NILP (second_best))
14101 second_best = tem;
14102 else if (found_size < size)
14104 if (XINT (XCDR (second_best)) > size
14105 || XINT (XCDR (second_best)) < found_size)
14106 second_best = tem;
14108 else
14110 if (XINT (XCDR (second_best)) > size
14111 && XINT (XCDR (second_best)) > found_size)
14112 second_best = tem;
14116 if (!NILP (newlist))
14117 break;
14118 else if (!NILP (second_best))
14120 newlist = Fcons (XCAR (second_best), Qnil);
14121 break;
14125 return newlist;
14129 #if GLYPH_DEBUG
14131 /* Check that FONT is valid on frame F. It is if it can be found in F's
14132 font table. */
14134 static void
14135 x_check_font (f, font)
14136 struct frame *f;
14137 XFontStruct *font;
14139 int i;
14140 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14142 xassert (font != NULL);
14144 for (i = 0; i < dpyinfo->n_fonts; i++)
14145 if (dpyinfo->font_table[i].name
14146 && font == dpyinfo->font_table[i].font)
14147 break;
14149 xassert (i < dpyinfo->n_fonts);
14152 #endif /* GLYPH_DEBUG != 0 */
14154 /* Set *W to the minimum width, *H to the minimum font height of FONT.
14155 Note: There are (broken) X fonts out there with invalid XFontStruct
14156 min_bounds contents. For example, handa@etl.go.jp reports that
14157 "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1" fonts
14158 have font->min_bounds.width == 0. */
14160 static INLINE void
14161 x_font_min_bounds (font, w, h)
14162 XFontStruct *font;
14163 int *w, *h;
14165 *h = FONT_HEIGHT (font);
14166 *w = font->min_bounds.width;
14168 /* Try to handle the case where FONT->min_bounds has invalid
14169 contents. Since the only font known to have invalid min_bounds
14170 is fixed-width, use max_bounds if min_bounds seems to be invalid. */
14171 if (*w <= 0)
14172 *w = font->max_bounds.width;
14176 /* Compute the smallest character width and smallest font height over
14177 all fonts available on frame F. Set the members smallest_char_width
14178 and smallest_font_height in F's x_display_info structure to
14179 the values computed. Value is non-zero if smallest_font_height or
14180 smallest_char_width become smaller than they were before. */
14182 static int
14183 x_compute_min_glyph_bounds (f)
14184 struct frame *f;
14186 int i;
14187 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14188 XFontStruct *font;
14189 int old_width = dpyinfo->smallest_char_width;
14190 int old_height = dpyinfo->smallest_font_height;
14192 dpyinfo->smallest_font_height = 100000;
14193 dpyinfo->smallest_char_width = 100000;
14195 for (i = 0; i < dpyinfo->n_fonts; ++i)
14196 if (dpyinfo->font_table[i].name)
14198 struct font_info *fontp = dpyinfo->font_table + i;
14199 int w, h;
14201 font = (XFontStruct *) fontp->font;
14202 xassert (font != (XFontStruct *) ~0);
14203 x_font_min_bounds (font, &w, &h);
14205 dpyinfo->smallest_font_height = min (dpyinfo->smallest_font_height, h);
14206 dpyinfo->smallest_char_width = min (dpyinfo->smallest_char_width, w);
14209 xassert (dpyinfo->smallest_char_width > 0
14210 && dpyinfo->smallest_font_height > 0);
14212 return (dpyinfo->n_fonts == 1
14213 || dpyinfo->smallest_char_width < old_width
14214 || dpyinfo->smallest_font_height < old_height);
14218 /* Load font named FONTNAME of the size SIZE for frame F, and return a
14219 pointer to the structure font_info while allocating it dynamically.
14220 If SIZE is 0, load any size of font.
14221 If loading is failed, return NULL. */
14223 struct font_info *
14224 x_load_font (f, fontname, size)
14225 struct frame *f;
14226 register char *fontname;
14227 int size;
14229 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14230 Lisp_Object font_names;
14231 int count;
14233 /* Get a list of all the fonts that match this name. Once we
14234 have a list of matching fonts, we compare them against the fonts
14235 we already have by comparing names. */
14236 font_names = x_list_fonts (f, build_string (fontname), size, 1);
14238 if (!NILP (font_names))
14240 Lisp_Object tail;
14241 int i;
14243 for (i = 0; i < dpyinfo->n_fonts; i++)
14244 for (tail = font_names; CONSP (tail); tail = XCDR (tail))
14245 if (dpyinfo->font_table[i].name
14246 && (!strcmp (dpyinfo->font_table[i].name,
14247 XSTRING (XCAR (tail))->data)
14248 || !strcmp (dpyinfo->font_table[i].full_name,
14249 XSTRING (XCAR (tail))->data)))
14250 return (dpyinfo->font_table + i);
14253 /* Load the font and add it to the table. */
14255 char *full_name;
14256 XFontStruct *font;
14257 struct font_info *fontp;
14258 unsigned long value;
14259 int i;
14261 /* If we have found fonts by x_list_font, load one of them. If
14262 not, we still try to load a font by the name given as FONTNAME
14263 because XListFonts (called in x_list_font) of some X server has
14264 a bug of not finding a font even if the font surely exists and
14265 is loadable by XLoadQueryFont. */
14266 if (size > 0 && !NILP (font_names))
14267 fontname = (char *) XSTRING (XCAR (font_names))->data;
14269 BLOCK_INPUT;
14270 count = x_catch_errors (FRAME_X_DISPLAY (f));
14271 font = (XFontStruct *) XLoadQueryFont (FRAME_X_DISPLAY (f), fontname);
14272 if (x_had_errors_p (FRAME_X_DISPLAY (f)))
14274 /* This error is perhaps due to insufficient memory on X
14275 server. Let's just ignore it. */
14276 font = NULL;
14277 x_clear_errors (FRAME_X_DISPLAY (f));
14279 x_uncatch_errors (FRAME_X_DISPLAY (f), count);
14280 UNBLOCK_INPUT;
14281 if (!font)
14282 return NULL;
14284 /* Find a free slot in the font table. */
14285 for (i = 0; i < dpyinfo->n_fonts; ++i)
14286 if (dpyinfo->font_table[i].name == NULL)
14287 break;
14289 /* If no free slot found, maybe enlarge the font table. */
14290 if (i == dpyinfo->n_fonts
14291 && dpyinfo->n_fonts == dpyinfo->font_table_size)
14293 int sz;
14294 dpyinfo->font_table_size = max (16, 2 * dpyinfo->font_table_size);
14295 sz = dpyinfo->font_table_size * sizeof *dpyinfo->font_table;
14296 dpyinfo->font_table
14297 = (struct font_info *) xrealloc (dpyinfo->font_table, sz);
14300 fontp = dpyinfo->font_table + i;
14301 if (i == dpyinfo->n_fonts)
14302 ++dpyinfo->n_fonts;
14304 /* Now fill in the slots of *FONTP. */
14305 BLOCK_INPUT;
14306 fontp->font = font;
14307 fontp->font_idx = i;
14308 fontp->charset = -1; /* fs_load_font sets it. */
14309 fontp->name = (char *) xmalloc (strlen (fontname) + 1);
14310 bcopy (fontname, fontp->name, strlen (fontname) + 1);
14312 /* Try to get the full name of FONT. Put it in FULL_NAME. */
14313 full_name = 0;
14314 if (XGetFontProperty (font, XA_FONT, &value))
14316 char *name = (char *) XGetAtomName (FRAME_X_DISPLAY (f), (Atom) value);
14317 char *p = name;
14318 int dashes = 0;
14320 /* Count the number of dashes in the "full name".
14321 If it is too few, this isn't really the font's full name,
14322 so don't use it.
14323 In X11R4, the fonts did not come with their canonical names
14324 stored in them. */
14325 while (*p)
14327 if (*p == '-')
14328 dashes++;
14329 p++;
14332 if (dashes >= 13)
14334 full_name = (char *) xmalloc (p - name + 1);
14335 bcopy (name, full_name, p - name + 1);
14338 XFree (name);
14341 if (full_name != 0)
14342 fontp->full_name = full_name;
14343 else
14344 fontp->full_name = fontp->name;
14346 fontp->size = font->max_bounds.width;
14347 fontp->height = FONT_HEIGHT (font);
14349 if (NILP (font_names))
14351 /* We come here because of a bug of XListFonts mentioned at
14352 the head of this block. Let's store this information in
14353 the cache for x_list_fonts. */
14354 Lisp_Object lispy_name = build_string (fontname);
14355 Lisp_Object lispy_full_name = build_string (fontp->full_name);
14356 Lisp_Object key = Fcons (Fcons (lispy_name, make_number (256)),
14357 Qnil);
14359 XSETCDR (dpyinfo->name_list_element,
14360 Fcons (Fcons (key,
14361 Fcons (Fcons (lispy_full_name,
14362 make_number (fontp->size)),
14363 Qnil)),
14364 XCDR (dpyinfo->name_list_element)));
14365 if (full_name)
14367 key = Fcons (Fcons (lispy_full_name, make_number (256)),
14368 Qnil);
14369 XSETCDR (dpyinfo->name_list_element,
14370 Fcons (Fcons (key,
14371 Fcons (Fcons (lispy_full_name,
14372 make_number (fontp->size)),
14373 Qnil)),
14374 XCDR (dpyinfo->name_list_element)));
14378 /* The slot `encoding' specifies how to map a character
14379 code-points (0x20..0x7F or 0x2020..0x7F7F) of each charset to
14380 the font code-points (0:0x20..0x7F, 1:0xA0..0xFF), or
14381 (0:0x2020..0x7F7F, 1:0xA0A0..0xFFFF, 3:0x20A0..0x7FFF,
14382 2:0xA020..0xFF7F). For the moment, we don't know which charset
14383 uses this font. So, we set information in fontp->encoding[1]
14384 which is never used by any charset. If mapping can't be
14385 decided, set FONT_ENCODING_NOT_DECIDED. */
14386 fontp->encoding_type
14387 = (font->max_byte1 == 0
14388 /* 1-byte font */
14389 ? (font->min_char_or_byte2 < 0x80
14390 ? (font->max_char_or_byte2 < 0x80
14391 ? 0 /* 0x20..0x7F */
14392 : FONT_ENCODING_NOT_DECIDED) /* 0x20..0xFF */
14393 : 1) /* 0xA0..0xFF */
14394 /* 2-byte font */
14395 : (font->min_byte1 < 0x80
14396 ? (font->max_byte1 < 0x80
14397 ? (font->min_char_or_byte2 < 0x80
14398 ? (font->max_char_or_byte2 < 0x80
14399 ? 0 /* 0x2020..0x7F7F */
14400 : FONT_ENCODING_NOT_DECIDED) /* 0x2020..0x7FFF */
14401 : 3) /* 0x20A0..0x7FFF */
14402 : FONT_ENCODING_NOT_DECIDED) /* 0x20??..0xA0?? */
14403 : (font->min_char_or_byte2 < 0x80
14404 ? (font->max_char_or_byte2 < 0x80
14405 ? 2 /* 0xA020..0xFF7F */
14406 : FONT_ENCODING_NOT_DECIDED) /* 0xA020..0xFFFF */
14407 : 1))); /* 0xA0A0..0xFFFF */
14409 fontp->baseline_offset
14410 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_BASELINE_OFFSET, &value)
14411 ? (long) value : 0);
14412 fontp->relative_compose
14413 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_RELATIVE_COMPOSE, &value)
14414 ? (long) value : 0);
14415 fontp->default_ascent
14416 = (XGetFontProperty (font, dpyinfo->Xatom_MULE_DEFAULT_ASCENT, &value)
14417 ? (long) value : 0);
14419 /* Set global flag fonts_changed_p to non-zero if the font loaded
14420 has a character with a smaller width than any other character
14421 before, or if the font loaded has a smaller height than any
14422 other font loaded before. If this happens, it will make a
14423 glyph matrix reallocation necessary. */
14424 fonts_changed_p |= x_compute_min_glyph_bounds (f);
14425 UNBLOCK_INPUT;
14426 return fontp;
14431 /* Return a pointer to struct font_info of a font named FONTNAME for
14432 frame F. If no such font is loaded, return NULL. */
14434 struct font_info *
14435 x_query_font (f, fontname)
14436 struct frame *f;
14437 register char *fontname;
14439 struct x_display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
14440 int i;
14442 for (i = 0; i < dpyinfo->n_fonts; i++)
14443 if (dpyinfo->font_table[i].name
14444 && (!strcmp (dpyinfo->font_table[i].name, fontname)
14445 || !strcmp (dpyinfo->font_table[i].full_name, fontname)))
14446 return (dpyinfo->font_table + i);
14447 return NULL;
14451 /* Find a CCL program for a font specified by FONTP, and set the member
14452 `encoder' of the structure. */
14454 void
14455 x_find_ccl_program (fontp)
14456 struct font_info *fontp;
14458 Lisp_Object list, elt;
14460 elt = Qnil;
14461 for (list = Vfont_ccl_encoder_alist; CONSP (list); list = XCDR (list))
14463 elt = XCAR (list);
14464 if (CONSP (elt)
14465 && STRINGP (XCAR (elt))
14466 && ((fast_c_string_match_ignore_case (XCAR (elt), fontp->name)
14467 >= 0)
14468 || (fast_c_string_match_ignore_case (XCAR (elt), fontp->full_name)
14469 >= 0)))
14470 break;
14473 if (! NILP (list))
14475 struct ccl_program *ccl
14476 = (struct ccl_program *) xmalloc (sizeof (struct ccl_program));
14478 if (setup_ccl_program (ccl, XCDR (elt)) < 0)
14479 xfree (ccl);
14480 else
14481 fontp->font_encoder = ccl;
14486 /* Return a char-table whose elements are t if the font FONT_INFO
14487 contains a glyph for the corresponding character, and nil if not.
14489 Fixme: For the moment, this function works only for fonts whose
14490 glyph encoding is the same as Unicode (e.g. ISO10646-1 fonts). */
14492 Lisp_Object
14493 x_get_font_repertory (f, font_info)
14494 FRAME_PTR f;
14495 struct font_info *font_info;
14497 XFontStruct *font = (XFontStruct *) font_info->font;
14498 Lisp_Object table;
14499 int min_byte1, max_byte1, min_byte2, max_byte2;
14501 table = Fmake_char_table (Qnil, Qnil);
14503 min_byte1 = font->min_byte1;
14504 max_byte1 = font->max_byte1;
14505 min_byte2 = font->min_char_or_byte2;
14506 max_byte2 = font->max_char_or_byte2;
14507 if (min_byte1 == 0 && max_byte1 == 0)
14509 if (! font->per_char || font->all_chars_exist == True)
14510 char_table_set_range (table, min_byte2, max_byte2, Qt);
14511 else
14513 XCharStruct *pcm = font->per_char;
14514 int from = -1;
14515 int i;
14517 for (i = min_byte2; i <= max_byte2; i++, pcm++)
14519 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
14521 if (from >= 0)
14523 char_table_set_range (table, from, i - 1, Qt);
14524 from = -1;
14527 else if (from < 0)
14528 from = i;
14530 if (from >= 0)
14531 char_table_set_range (table, from, i - 1, Qt);
14534 else
14536 if (! font->per_char || font->all_chars_exist == True)
14538 int i;
14540 for (i = min_byte1; i <= max_byte1; i++)
14541 char_table_set_range (table,
14542 (i << 8) | min_byte2, (i << 8) | max_byte2,
14543 Qt);
14545 else
14547 XCharStruct *pcm = font->per_char;
14548 int i;
14550 for (i = min_byte1; i <= max_byte1; i++)
14552 int from = -1;
14553 int j;
14555 for (j = min_byte2; j <= max_byte2; j++, pcm++)
14557 if (pcm->width == 0 && pcm->rbearing == pcm->lbearing)
14559 if (from >= 0)
14561 char_table_set_range (table, (i << 8) | from,
14562 (i << 8) | (j - 1), Qt);
14563 from = -1;
14566 else if (from < 0)
14567 from = j;
14569 if (from >= 0)
14570 char_table_set_range (table, (i << 8) | from,
14571 (i << 8) | (j - 1), Qt);
14576 return table;
14581 /***********************************************************************
14582 Initialization
14583 ***********************************************************************/
14585 #ifdef USE_X_TOOLKIT
14586 static XrmOptionDescRec emacs_options[] = {
14587 {"-geometry", ".geometry", XrmoptionSepArg, NULL},
14588 {"-iconic", ".iconic", XrmoptionNoArg, (XtPointer) "yes"},
14590 {"-internal-border-width", "*EmacsScreen.internalBorderWidth",
14591 XrmoptionSepArg, NULL},
14592 {"-ib", "*EmacsScreen.internalBorderWidth", XrmoptionSepArg, NULL},
14594 {"-T", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14595 {"-wn", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14596 {"-title", "*EmacsShell.title", XrmoptionSepArg, (XtPointer) NULL},
14597 {"-iconname", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14598 {"-in", "*EmacsShell.iconName", XrmoptionSepArg, (XtPointer) NULL},
14599 {"-mc", "*pointerColor", XrmoptionSepArg, (XtPointer) NULL},
14600 {"-cr", "*cursorColor", XrmoptionSepArg, (XtPointer) NULL}
14602 #endif /* USE_X_TOOLKIT */
14604 static int x_initialized;
14606 #ifdef MULTI_KBOARD
14607 /* Test whether two display-name strings agree up to the dot that separates
14608 the screen number from the server number. */
14609 static int
14610 same_x_server (name1, name2)
14611 char *name1, *name2;
14613 int seen_colon = 0;
14614 unsigned char *system_name = XSTRING (Vsystem_name)->data;
14615 int system_name_length = strlen (system_name);
14616 int length_until_period = 0;
14618 while (system_name[length_until_period] != 0
14619 && system_name[length_until_period] != '.')
14620 length_until_period++;
14622 /* Treat `unix' like an empty host name. */
14623 if (! strncmp (name1, "unix:", 5))
14624 name1 += 4;
14625 if (! strncmp (name2, "unix:", 5))
14626 name2 += 4;
14627 /* Treat this host's name like an empty host name. */
14628 if (! strncmp (name1, system_name, system_name_length)
14629 && name1[system_name_length] == ':')
14630 name1 += system_name_length;
14631 if (! strncmp (name2, system_name, system_name_length)
14632 && name2[system_name_length] == ':')
14633 name2 += system_name_length;
14634 /* Treat this host's domainless name like an empty host name. */
14635 if (! strncmp (name1, system_name, length_until_period)
14636 && name1[length_until_period] == ':')
14637 name1 += length_until_period;
14638 if (! strncmp (name2, system_name, length_until_period)
14639 && name2[length_until_period] == ':')
14640 name2 += length_until_period;
14642 for (; *name1 != '\0' && *name1 == *name2; name1++, name2++)
14644 if (*name1 == ':')
14645 seen_colon++;
14646 if (seen_colon && *name1 == '.')
14647 return 1;
14649 return (seen_colon
14650 && (*name1 == '.' || *name1 == '\0')
14651 && (*name2 == '.' || *name2 == '\0'));
14653 #endif
14655 struct x_display_info *
14656 x_term_init (display_name, xrm_option, resource_name)
14657 Lisp_Object display_name;
14658 char *xrm_option;
14659 char *resource_name;
14661 int connection;
14662 Display *dpy;
14663 struct x_display_info *dpyinfo;
14664 XrmDatabase xrdb;
14666 BLOCK_INPUT;
14668 if (!x_initialized)
14670 x_initialize ();
14671 x_initialized = 1;
14674 #ifdef USE_X_TOOLKIT
14675 /* weiner@footloose.sps.mot.com reports that this causes
14676 errors with X11R5:
14677 X protocol error: BadAtom (invalid Atom parameter)
14678 on protocol request 18skiloaf.
14679 So let's not use it until R6. */
14680 #ifdef HAVE_X11XTR6
14681 XtSetLanguageProc (NULL, NULL, NULL);
14682 #endif
14685 int argc = 0;
14686 char *argv[3];
14688 argv[0] = "";
14689 argc = 1;
14690 if (xrm_option)
14692 argv[argc++] = "-xrm";
14693 argv[argc++] = xrm_option;
14695 dpy = XtOpenDisplay (Xt_app_con, XSTRING (display_name)->data,
14696 resource_name, EMACS_CLASS,
14697 emacs_options, XtNumber (emacs_options),
14698 &argc, argv);
14700 #ifdef HAVE_X11XTR6
14701 /* I think this is to compensate for XtSetLanguageProc. */
14702 fixup_locale ();
14703 #endif
14706 #else /* not USE_X_TOOLKIT */
14707 #ifdef HAVE_X11R5
14708 XSetLocaleModifiers ("");
14709 #endif
14710 dpy = XOpenDisplay (XSTRING (display_name)->data);
14711 #endif /* not USE_X_TOOLKIT */
14713 /* Detect failure. */
14714 if (dpy == 0)
14716 UNBLOCK_INPUT;
14717 return 0;
14720 /* We have definitely succeeded. Record the new connection. */
14722 dpyinfo = (struct x_display_info *) xmalloc (sizeof (struct x_display_info));
14723 bzero (dpyinfo, sizeof *dpyinfo);
14725 #ifdef MULTI_KBOARD
14727 struct x_display_info *share;
14728 Lisp_Object tail;
14730 for (share = x_display_list, tail = x_display_name_list; share;
14731 share = share->next, tail = XCDR (tail))
14732 if (same_x_server (XSTRING (XCAR (XCAR (tail)))->data,
14733 XSTRING (display_name)->data))
14734 break;
14735 if (share)
14736 dpyinfo->kboard = share->kboard;
14737 else
14739 dpyinfo->kboard = (KBOARD *) xmalloc (sizeof (KBOARD));
14740 init_kboard (dpyinfo->kboard);
14741 if (!EQ (XSYMBOL (Qvendor_specific_keysyms)->function, Qunbound))
14743 char *vendor = ServerVendor (dpy);
14744 UNBLOCK_INPUT;
14745 dpyinfo->kboard->Vsystem_key_alist
14746 = call1 (Qvendor_specific_keysyms,
14747 build_string (vendor ? vendor : ""));
14748 BLOCK_INPUT;
14751 dpyinfo->kboard->next_kboard = all_kboards;
14752 all_kboards = dpyinfo->kboard;
14753 /* Don't let the initial kboard remain current longer than necessary.
14754 That would cause problems if a file loaded on startup tries to
14755 prompt in the mini-buffer. */
14756 if (current_kboard == initial_kboard)
14757 current_kboard = dpyinfo->kboard;
14759 dpyinfo->kboard->reference_count++;
14761 #endif
14763 /* Put this display on the chain. */
14764 dpyinfo->next = x_display_list;
14765 x_display_list = dpyinfo;
14767 /* Put it on x_display_name_list as well, to keep them parallel. */
14768 x_display_name_list = Fcons (Fcons (display_name, Qnil),
14769 x_display_name_list);
14770 dpyinfo->name_list_element = XCAR (x_display_name_list);
14772 dpyinfo->display = dpy;
14774 #if 0
14775 XSetAfterFunction (x_current_display, x_trace_wire);
14776 #endif /* ! 0 */
14778 dpyinfo->x_id_name
14779 = (char *) xmalloc (STRING_BYTES (XSTRING (Vinvocation_name))
14780 + STRING_BYTES (XSTRING (Vsystem_name))
14781 + 2);
14782 sprintf (dpyinfo->x_id_name, "%s@%s",
14783 XSTRING (Vinvocation_name)->data, XSTRING (Vsystem_name)->data);
14785 /* Figure out which modifier bits mean what. */
14786 x_find_modifier_meanings (dpyinfo);
14788 /* Get the scroll bar cursor. */
14789 dpyinfo->vertical_scroll_bar_cursor
14790 = XCreateFontCursor (dpyinfo->display, XC_sb_v_double_arrow);
14792 xrdb = x_load_resources (dpyinfo->display, xrm_option,
14793 resource_name, EMACS_CLASS);
14794 #ifdef HAVE_XRMSETDATABASE
14795 XrmSetDatabase (dpyinfo->display, xrdb);
14796 #else
14797 dpyinfo->display->db = xrdb;
14798 #endif
14799 /* Put the rdb where we can find it in a way that works on
14800 all versions. */
14801 dpyinfo->xrdb = xrdb;
14803 dpyinfo->screen = ScreenOfDisplay (dpyinfo->display,
14804 DefaultScreen (dpyinfo->display));
14805 select_visual (dpyinfo);
14806 dpyinfo->cmap = DefaultColormapOfScreen (dpyinfo->screen);
14807 dpyinfo->height = HeightOfScreen (dpyinfo->screen);
14808 dpyinfo->width = WidthOfScreen (dpyinfo->screen);
14809 dpyinfo->root_window = RootWindowOfScreen (dpyinfo->screen);
14810 dpyinfo->grabbed = 0;
14811 dpyinfo->reference_count = 0;
14812 dpyinfo->icon_bitmap_id = -1;
14813 dpyinfo->font_table = NULL;
14814 dpyinfo->n_fonts = 0;
14815 dpyinfo->font_table_size = 0;
14816 dpyinfo->bitmaps = 0;
14817 dpyinfo->bitmaps_size = 0;
14818 dpyinfo->bitmaps_last = 0;
14819 dpyinfo->scratch_cursor_gc = 0;
14820 dpyinfo->mouse_face_mouse_frame = 0;
14821 dpyinfo->mouse_face_deferred_gc = 0;
14822 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
14823 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
14824 dpyinfo->mouse_face_face_id = DEFAULT_FACE_ID;
14825 dpyinfo->mouse_face_window = Qnil;
14826 dpyinfo->mouse_face_overlay = Qnil;
14827 dpyinfo->mouse_face_mouse_x = dpyinfo->mouse_face_mouse_y = 0;
14828 dpyinfo->mouse_face_defer = 0;
14829 dpyinfo->mouse_face_hidden = 0;
14830 dpyinfo->x_focus_frame = 0;
14831 dpyinfo->x_focus_event_frame = 0;
14832 dpyinfo->x_highlight_frame = 0;
14833 dpyinfo->image_cache = make_image_cache ();
14835 /* See if a private colormap is requested. */
14836 if (dpyinfo->visual == DefaultVisualOfScreen (dpyinfo->screen))
14838 if (dpyinfo->visual->class == PseudoColor)
14840 Lisp_Object value;
14841 value = display_x_get_resource (dpyinfo,
14842 build_string ("privateColormap"),
14843 build_string ("PrivateColormap"),
14844 Qnil, Qnil);
14845 if (STRINGP (value)
14846 && (!strcmp (XSTRING (value)->data, "true")
14847 || !strcmp (XSTRING (value)->data, "on")))
14848 dpyinfo->cmap = XCopyColormapAndFree (dpyinfo->display, dpyinfo->cmap);
14851 else
14852 dpyinfo->cmap = XCreateColormap (dpyinfo->display, dpyinfo->root_window,
14853 dpyinfo->visual, AllocNone);
14856 int screen_number = XScreenNumberOfScreen (dpyinfo->screen);
14857 double pixels = DisplayHeight (dpyinfo->display, screen_number);
14858 double mm = DisplayHeightMM (dpyinfo->display, screen_number);
14859 dpyinfo->resy = pixels * 25.4 / mm;
14860 pixels = DisplayWidth (dpyinfo->display, screen_number);
14861 mm = DisplayWidthMM (dpyinfo->display, screen_number);
14862 dpyinfo->resx = pixels * 25.4 / mm;
14865 dpyinfo->Xatom_wm_protocols
14866 = XInternAtom (dpyinfo->display, "WM_PROTOCOLS", False);
14867 dpyinfo->Xatom_wm_take_focus
14868 = XInternAtom (dpyinfo->display, "WM_TAKE_FOCUS", False);
14869 dpyinfo->Xatom_wm_save_yourself
14870 = XInternAtom (dpyinfo->display, "WM_SAVE_YOURSELF", False);
14871 dpyinfo->Xatom_wm_delete_window
14872 = XInternAtom (dpyinfo->display, "WM_DELETE_WINDOW", False);
14873 dpyinfo->Xatom_wm_change_state
14874 = XInternAtom (dpyinfo->display, "WM_CHANGE_STATE", False);
14875 dpyinfo->Xatom_wm_configure_denied
14876 = XInternAtom (dpyinfo->display, "WM_CONFIGURE_DENIED", False);
14877 dpyinfo->Xatom_wm_window_moved
14878 = XInternAtom (dpyinfo->display, "WM_MOVED", False);
14879 dpyinfo->Xatom_editres
14880 = XInternAtom (dpyinfo->display, "Editres", False);
14881 dpyinfo->Xatom_CLIPBOARD
14882 = XInternAtom (dpyinfo->display, "CLIPBOARD", False);
14883 dpyinfo->Xatom_TIMESTAMP
14884 = XInternAtom (dpyinfo->display, "TIMESTAMP", False);
14885 dpyinfo->Xatom_TEXT
14886 = XInternAtom (dpyinfo->display, "TEXT", False);
14887 dpyinfo->Xatom_COMPOUND_TEXT
14888 = XInternAtom (dpyinfo->display, "COMPOUND_TEXT", False);
14889 dpyinfo->Xatom_DELETE
14890 = XInternAtom (dpyinfo->display, "DELETE", False);
14891 dpyinfo->Xatom_MULTIPLE
14892 = XInternAtom (dpyinfo->display, "MULTIPLE", False);
14893 dpyinfo->Xatom_INCR
14894 = XInternAtom (dpyinfo->display, "INCR", False);
14895 dpyinfo->Xatom_EMACS_TMP
14896 = XInternAtom (dpyinfo->display, "_EMACS_TMP_", False);
14897 dpyinfo->Xatom_TARGETS
14898 = XInternAtom (dpyinfo->display, "TARGETS", False);
14899 dpyinfo->Xatom_NULL
14900 = XInternAtom (dpyinfo->display, "NULL", False);
14901 dpyinfo->Xatom_ATOM_PAIR
14902 = XInternAtom (dpyinfo->display, "ATOM_PAIR", False);
14903 /* For properties of font. */
14904 dpyinfo->Xatom_PIXEL_SIZE
14905 = XInternAtom (dpyinfo->display, "PIXEL_SIZE", False);
14906 dpyinfo->Xatom_MULE_BASELINE_OFFSET
14907 = XInternAtom (dpyinfo->display, "_MULE_BASELINE_OFFSET", False);
14908 dpyinfo->Xatom_MULE_RELATIVE_COMPOSE
14909 = XInternAtom (dpyinfo->display, "_MULE_RELATIVE_COMPOSE", False);
14910 dpyinfo->Xatom_MULE_DEFAULT_ASCENT
14911 = XInternAtom (dpyinfo->display, "_MULE_DEFAULT_ASCENT", False);
14913 /* Ghostscript support. */
14914 dpyinfo->Xatom_PAGE = XInternAtom (dpyinfo->display, "PAGE", False);
14915 dpyinfo->Xatom_DONE = XInternAtom (dpyinfo->display, "DONE", False);
14917 dpyinfo->Xatom_Scrollbar = XInternAtom (dpyinfo->display, "SCROLLBAR",
14918 False);
14920 dpyinfo->cut_buffers_initialized = 0;
14922 connection = ConnectionNumber (dpyinfo->display);
14923 dpyinfo->connection = connection;
14926 char null_bits[1];
14928 null_bits[0] = 0x00;
14930 dpyinfo->null_pixel
14931 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14932 null_bits, 1, 1, (long) 0, (long) 0,
14937 extern int gray_bitmap_width, gray_bitmap_height;
14938 extern char *gray_bitmap_bits;
14939 dpyinfo->gray
14940 = XCreatePixmapFromBitmapData (dpyinfo->display, dpyinfo->root_window,
14941 gray_bitmap_bits,
14942 gray_bitmap_width, gray_bitmap_height,
14943 (unsigned long) 1, (unsigned long) 0, 1);
14946 #ifdef HAVE_X_I18N
14947 xim_initialize (dpyinfo, resource_name);
14948 #endif
14950 #ifdef subprocesses
14951 /* This is only needed for distinguishing keyboard and process input. */
14952 if (connection != 0)
14953 add_keyboard_wait_descriptor (connection);
14954 #endif
14956 #ifndef F_SETOWN_BUG
14957 #ifdef F_SETOWN
14958 #ifdef F_SETOWN_SOCK_NEG
14959 /* stdin is a socket here */
14960 fcntl (connection, F_SETOWN, -getpid ());
14961 #else /* ! defined (F_SETOWN_SOCK_NEG) */
14962 fcntl (connection, F_SETOWN, getpid ());
14963 #endif /* ! defined (F_SETOWN_SOCK_NEG) */
14964 #endif /* ! defined (F_SETOWN) */
14965 #endif /* F_SETOWN_BUG */
14967 #ifdef SIGIO
14968 if (interrupt_input)
14969 init_sigio (connection);
14970 #endif /* ! defined (SIGIO) */
14972 #ifdef USE_LUCID
14973 #ifdef HAVE_X11R5 /* It seems X11R4 lacks XtCvtStringToFont, and XPointer. */
14974 /* Make sure that we have a valid font for dialog boxes
14975 so that Xt does not crash. */
14977 Display *dpy = dpyinfo->display;
14978 XrmValue d, fr, to;
14979 Font font;
14980 int count;
14982 d.addr = (XPointer)&dpy;
14983 d.size = sizeof (Display *);
14984 fr.addr = XtDefaultFont;
14985 fr.size = sizeof (XtDefaultFont);
14986 to.size = sizeof (Font *);
14987 to.addr = (XPointer)&font;
14988 count = x_catch_errors (dpy);
14989 if (!XtCallConverter (dpy, XtCvtStringToFont, &d, 1, &fr, &to, NULL))
14990 abort ();
14991 if (x_had_errors_p (dpy) || !XQueryFont (dpy, font))
14992 XrmPutLineResource (&xrdb, "Emacs.dialog.*.font: 9x15");
14993 x_uncatch_errors (dpy, count);
14995 #endif
14996 #endif
14998 /* See if we should run in synchronous mode. This is useful
14999 for debugging X code. */
15001 Lisp_Object value;
15002 value = display_x_get_resource (dpyinfo,
15003 build_string ("synchronous"),
15004 build_string ("Synchronous"),
15005 Qnil, Qnil);
15006 if (STRINGP (value)
15007 && (!strcmp (XSTRING (value)->data, "true")
15008 || !strcmp (XSTRING (value)->data, "on")))
15009 XSynchronize (dpyinfo->display, True);
15013 Lisp_Object value;
15014 value = display_x_get_resource (dpyinfo,
15015 build_string ("useXIM"),
15016 build_string ("UseXIM"),
15017 Qnil, Qnil);
15018 if (STRINGP (value)
15019 && (!strcmp (XSTRING (value)->data, "false")
15020 || !strcmp (XSTRING (value)->data, "off")))
15021 use_xim = 0;
15024 UNBLOCK_INPUT;
15026 return dpyinfo;
15029 /* Get rid of display DPYINFO, assuming all frames are already gone,
15030 and without sending any more commands to the X server. */
15032 void
15033 x_delete_display (dpyinfo)
15034 struct x_display_info *dpyinfo;
15036 delete_keyboard_wait_descriptor (dpyinfo->connection);
15038 /* Discard this display from x_display_name_list and x_display_list.
15039 We can't use Fdelq because that can quit. */
15040 if (! NILP (x_display_name_list)
15041 && EQ (XCAR (x_display_name_list), dpyinfo->name_list_element))
15042 x_display_name_list = XCDR (x_display_name_list);
15043 else
15045 Lisp_Object tail;
15047 tail = x_display_name_list;
15048 while (CONSP (tail) && CONSP (XCDR (tail)))
15050 if (EQ (XCAR (XCDR (tail)), dpyinfo->name_list_element))
15052 XSETCDR (tail, XCDR (XCDR (tail)));
15053 break;
15055 tail = XCDR (tail);
15059 if (next_noop_dpyinfo == dpyinfo)
15060 next_noop_dpyinfo = dpyinfo->next;
15062 if (x_display_list == dpyinfo)
15063 x_display_list = dpyinfo->next;
15064 else
15066 struct x_display_info *tail;
15068 for (tail = x_display_list; tail; tail = tail->next)
15069 if (tail->next == dpyinfo)
15070 tail->next = tail->next->next;
15073 #ifndef USE_X_TOOLKIT /* I'm told Xt does this itself. */
15074 #ifndef AIX /* On AIX, XCloseDisplay calls this. */
15075 XrmDestroyDatabase (dpyinfo->xrdb);
15076 #endif
15077 #endif
15078 #ifdef MULTI_KBOARD
15079 if (--dpyinfo->kboard->reference_count == 0)
15080 delete_kboard (dpyinfo->kboard);
15081 #endif
15082 #ifdef HAVE_X_I18N
15083 if (dpyinfo->xim)
15084 xim_close_dpy (dpyinfo);
15085 #endif
15087 xfree (dpyinfo->font_table);
15088 xfree (dpyinfo->x_id_name);
15089 xfree (dpyinfo->color_cells);
15090 xfree (dpyinfo);
15094 /* Set up use of X before we make the first connection. */
15096 static struct redisplay_interface x_redisplay_interface =
15098 x_produce_glyphs,
15099 x_write_glyphs,
15100 x_insert_glyphs,
15101 x_clear_end_of_line,
15102 x_scroll_run,
15103 x_after_update_window_line,
15104 x_update_window_begin,
15105 x_update_window_end,
15106 XTcursor_to,
15107 x_flush,
15108 x_clear_mouse_face,
15109 x_get_glyph_overhangs,
15110 x_fix_overlapping_area
15113 void
15114 x_initialize ()
15116 rif = &x_redisplay_interface;
15118 clear_frame_hook = x_clear_frame;
15119 ins_del_lines_hook = x_ins_del_lines;
15120 delete_glyphs_hook = x_delete_glyphs;
15121 ring_bell_hook = XTring_bell;
15122 reset_terminal_modes_hook = XTreset_terminal_modes;
15123 set_terminal_modes_hook = XTset_terminal_modes;
15124 update_begin_hook = x_update_begin;
15125 update_end_hook = x_update_end;
15126 set_terminal_window_hook = XTset_terminal_window;
15127 read_socket_hook = XTread_socket;
15128 frame_up_to_date_hook = XTframe_up_to_date;
15129 mouse_position_hook = XTmouse_position;
15130 frame_rehighlight_hook = XTframe_rehighlight;
15131 frame_raise_lower_hook = XTframe_raise_lower;
15132 set_vertical_scroll_bar_hook = XTset_vertical_scroll_bar;
15133 condemn_scroll_bars_hook = XTcondemn_scroll_bars;
15134 redeem_scroll_bar_hook = XTredeem_scroll_bar;
15135 judge_scroll_bars_hook = XTjudge_scroll_bars;
15136 estimate_mode_line_height_hook = x_estimate_mode_line_height;
15138 scroll_region_ok = 1; /* we'll scroll partial frames */
15139 char_ins_del_ok = 1;
15140 line_ins_del_ok = 1; /* we'll just blt 'em */
15141 fast_clear_end_of_line = 1; /* X does this well */
15142 memory_below_frame = 0; /* we don't remember what scrolls
15143 off the bottom */
15144 baud_rate = 19200;
15146 x_noop_count = 0;
15147 last_tool_bar_item = -1;
15148 any_help_event_p = 0;
15150 /* Try to use interrupt input; if we can't, then start polling. */
15151 Fset_input_mode (Qt, Qnil, Qt, Qnil);
15153 #ifdef USE_X_TOOLKIT
15154 XtToolkitInitialize ();
15156 Xt_app_con = XtCreateApplicationContext ();
15158 /* Register a converter from strings to pixels, which uses
15159 Emacs' color allocation infrastructure. */
15160 XtAppSetTypeConverter (Xt_app_con,
15161 XtRString, XtRPixel, cvt_string_to_pixel,
15162 cvt_string_to_pixel_args,
15163 XtNumber (cvt_string_to_pixel_args),
15164 XtCacheByDisplay, cvt_pixel_dtor);
15166 XtAppSetFallbackResources (Xt_app_con, Xt_default_resources);
15168 /* Install an asynchronous timer that processes Xt timeout events
15169 every 0.1s. This is necessary because some widget sets use
15170 timeouts internally, for example the LessTif menu bar, or the
15171 Xaw3d scroll bar. When Xt timouts aren't processed, these
15172 widgets don't behave normally. */
15174 EMACS_TIME interval;
15175 EMACS_SET_SECS_USECS (interval, 0, 100000);
15176 start_atimer (ATIMER_CONTINUOUS, interval, x_process_timeouts, 0);
15178 #endif
15180 #ifdef USE_TOOLKIT_SCROLL_BARS
15181 xaw3d_arrow_scroll = False;
15182 xaw3d_pick_top = True;
15183 #endif
15185 /* Note that there is no real way portable across R3/R4 to get the
15186 original error handler. */
15187 XSetErrorHandler (x_error_handler);
15188 XSetIOErrorHandler (x_io_error_quitter);
15190 /* Disable Window Change signals; they are handled by X events. */
15191 #ifdef SIGWINCH
15192 signal (SIGWINCH, SIG_DFL);
15193 #endif /* ! defined (SIGWINCH) */
15195 signal (SIGPIPE, x_connection_signal);
15199 void
15200 syms_of_xterm ()
15202 staticpro (&x_error_message_string);
15203 x_error_message_string = Qnil;
15205 staticpro (&x_display_name_list);
15206 x_display_name_list = Qnil;
15208 staticpro (&last_mouse_scroll_bar);
15209 last_mouse_scroll_bar = Qnil;
15211 staticpro (&Qvendor_specific_keysyms);
15212 Qvendor_specific_keysyms = intern ("vendor-specific-keysyms");
15214 staticpro (&last_mouse_press_frame);
15215 last_mouse_press_frame = Qnil;
15217 help_echo = Qnil;
15218 staticpro (&help_echo);
15219 help_echo_object = Qnil;
15220 staticpro (&help_echo_object);
15221 help_echo_window = Qnil;
15222 staticpro (&help_echo_window);
15223 previous_help_echo = Qnil;
15224 staticpro (&previous_help_echo);
15225 help_echo_pos = -1;
15227 DEFVAR_BOOL ("x-autoselect-window", &x_autoselect_window_p,
15228 doc: /* *Non-nil means autoselect window with mouse pointer. */);
15229 x_autoselect_window_p = 0;
15231 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
15232 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
15233 For example, if a block cursor is over a tab, it will be drawn as
15234 wide as that tab on the display. */);
15235 x_stretch_cursor_p = 0;
15237 DEFVAR_BOOL ("x-use-underline-position-properties",
15238 &x_use_underline_position_properties,
15239 doc: /* *Non-nil means make use of UNDERLINE_POSITION font properties.
15240 nil means ignore them. If you encounter fonts with bogus
15241 UNDERLINE_POSITION font properties, for example 7x13 on XFree prior
15242 to 4.1, set this to nil. */);
15243 x_use_underline_position_properties = 1;
15245 DEFVAR_LISP ("x-toolkit-scroll-bars", &Vx_toolkit_scroll_bars,
15246 doc: /* What X toolkit scroll bars Emacs uses.
15247 A value of nil means Emacs doesn't use X toolkit scroll bars.
15248 Otherwise, value is a symbol describing the X toolkit. */);
15249 #ifdef USE_TOOLKIT_SCROLL_BARS
15250 #ifdef USE_MOTIF
15251 Vx_toolkit_scroll_bars = intern ("motif");
15252 #elif defined HAVE_XAW3D
15253 Vx_toolkit_scroll_bars = intern ("xaw3d");
15254 #else
15255 Vx_toolkit_scroll_bars = intern ("xaw");
15256 #endif
15257 #else
15258 Vx_toolkit_scroll_bars = Qnil;
15259 #endif
15261 staticpro (&last_mouse_motion_frame);
15262 last_mouse_motion_frame = Qnil;
15264 Qmodifier_value = intern ("modifier-value");
15265 Qalt = intern ("alt");
15266 Fput (Qalt, Qmodifier_value, make_number (alt_modifier));
15267 Qhyper = intern ("hyper");
15268 Fput (Qhyper, Qmodifier_value, make_number (hyper_modifier));
15269 Qmeta = intern ("meta");
15270 Fput (Qmeta, Qmodifier_value, make_number (meta_modifier));
15271 Qsuper = intern ("super");
15272 Fput (Qsuper, Qmodifier_value, make_number (super_modifier));
15274 DEFVAR_LISP ("x-alt-keysym", &Vx_alt_keysym,
15275 doc: /* Which keys Emacs uses for the alt modifier.
15276 This should be one of the symbols `alt', `hyper', `meta', `super'.
15277 For example, `alt' means use the Alt_L and Alt_R keysyms. The default
15278 is nil, which is the same as `alt'. */);
15279 Vx_alt_keysym = Qnil;
15281 DEFVAR_LISP ("x-hyper-keysym", &Vx_hyper_keysym,
15282 doc: /* Which keys Emacs uses for the hyper modifier.
15283 This should be one of the symbols `alt', `hyper', `meta', `super'.
15284 For example, `hyper' means use the Hyper_L and Hyper_R keysyms. The
15285 default is nil, which is the same as `hyper'. */);
15286 Vx_hyper_keysym = Qnil;
15288 DEFVAR_LISP ("x-meta-keysym", &Vx_meta_keysym,
15289 doc: /* Which keys Emacs uses for the meta modifier.
15290 This should be one of the symbols `alt', `hyper', `meta', `super'.
15291 For example, `meta' means use the Meta_L and Meta_R keysyms. The
15292 default is nil, which is the same as `meta'. */);
15293 Vx_meta_keysym = Qnil;
15295 DEFVAR_LISP ("x-super-keysym", &Vx_super_keysym,
15296 doc: /* Which keys Emacs uses for the super modifier.
15297 This should be one of the symbols `alt', `hyper', `meta', `super'.
15298 For example, `super' means use the Super_L and Super_R keysyms. The
15299 default is nil, which is the same as `super'. */);
15300 Vx_super_keysym = Qnil;
15302 DEFVAR_LISP ("x-keysym-table", &Vx_keysym_table,
15303 doc: /* Hash table of character codes indexed by X keysym codes. */);
15304 Vx_keysym_table = make_hash_table (Qeql, make_number (800),
15305 make_number (DEFAULT_REHASH_SIZE),
15306 make_number (DEFAULT_REHASH_THRESHOLD),
15307 Qnil, Qnil, Qnil);
15310 #endif /* HAVE_X_WINDOWS */